Home | History | Annotate | Line # | Download | only in uvm
uvm_anon.c revision 1.55
      1  1.55    rmind /*	$NetBSD: uvm_anon.c,v 1.55 2011/06/17 02:12:35 rmind Exp $	*/
      2   1.1    chuck 
      3   1.1    chuck /*
      4   1.1    chuck  * Copyright (c) 1997 Charles D. Cranor and Washington University.
      5   1.1    chuck  * All rights reserved.
      6   1.1    chuck  *
      7   1.1    chuck  * Redistribution and use in source and binary forms, with or without
      8   1.1    chuck  * modification, are permitted provided that the following conditions
      9   1.1    chuck  * are met:
     10   1.1    chuck  * 1. Redistributions of source code must retain the above copyright
     11   1.1    chuck  *    notice, this list of conditions and the following disclaimer.
     12   1.1    chuck  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1    chuck  *    notice, this list of conditions and the following disclaimer in the
     14   1.1    chuck  *    documentation and/or other materials provided with the distribution.
     15   1.1    chuck  *
     16   1.1    chuck  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17   1.1    chuck  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18   1.1    chuck  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19   1.1    chuck  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20   1.1    chuck  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21   1.1    chuck  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22   1.1    chuck  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23   1.1    chuck  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24   1.1    chuck  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25   1.1    chuck  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26   1.1    chuck  */
     27   1.1    chuck 
     28   1.1    chuck /*
     29   1.1    chuck  * uvm_anon.c: uvm anon ops
     30   1.1    chuck  */
     31  1.21    lukem 
     32  1.21    lukem #include <sys/cdefs.h>
     33  1.55    rmind __KERNEL_RCSID(0, "$NetBSD: uvm_anon.c,v 1.55 2011/06/17 02:12:35 rmind Exp $");
     34   1.1    chuck 
     35   1.1    chuck #include "opt_uvmhist.h"
     36   1.1    chuck 
     37   1.1    chuck #include <sys/param.h>
     38   1.1    chuck #include <sys/systm.h>
     39   1.1    chuck #include <sys/pool.h>
     40   1.5      chs #include <sys/kernel.h>
     41   1.1    chuck 
     42   1.1    chuck #include <uvm/uvm.h>
     43   1.1    chuck #include <uvm/uvm_swap.h>
     44  1.39     yamt #include <uvm/uvm_pdpolicy.h>
     45   1.1    chuck 
     46  1.55    rmind static struct pool_cache	uvm_anon_cache;
     47   1.5      chs 
     48  1.55    rmind static int			uvm_anon_ctor(void *, void *, int);
     49   1.5      chs 
     50   1.1    chuck void
     51  1.34  thorpej uvm_anon_init(void)
     52   1.1    chuck {
     53   1.5      chs 
     54  1.49       ad 	pool_cache_bootstrap(&uvm_anon_cache, sizeof(struct vm_anon), 0, 0,
     55  1.49       ad 	    PR_LARGECACHE, "anonpl", NULL, IPL_NONE, uvm_anon_ctor,
     56  1.55    rmind 	    NULL, NULL);
     57   1.1    chuck }
     58   1.1    chuck 
     59  1.33     yamt static int
     60  1.41     yamt uvm_anon_ctor(void *arg, void *object, int flags)
     61   1.1    chuck {
     62  1.33     yamt 	struct vm_anon *anon = object;
     63   1.1    chuck 
     64  1.33     yamt 	anon->an_ref = 0;
     65  1.33     yamt 	anon->an_page = NULL;
     66  1.37     yamt #if defined(VMSWAP)
     67  1.33     yamt 	anon->an_swslot = 0;
     68  1.55    rmind #endif
     69  1.11      chs 	return 0;
     70   1.1    chuck }
     71   1.1    chuck 
     72   1.1    chuck /*
     73  1.55    rmind  * uvm_analloc: allocate a new anon.
     74  1.13  thorpej  *
     75  1.55    rmind  * => anon will have no lock associated.
     76   1.1    chuck  */
     77   1.1    chuck struct vm_anon *
     78  1.34  thorpej uvm_analloc(void)
     79   1.1    chuck {
     80  1.33     yamt 	struct vm_anon *anon;
     81   1.1    chuck 
     82  1.47       ad 	anon = pool_cache_get(&uvm_anon_cache, PR_NOWAIT);
     83  1.33     yamt 	if (anon) {
     84  1.33     yamt 		KASSERT(anon->an_ref == 0);
     85  1.33     yamt 		KASSERT(anon->an_page == NULL);
     86  1.37     yamt #if defined(VMSWAP)
     87  1.33     yamt 		KASSERT(anon->an_swslot == 0);
     88  1.55    rmind #endif
     89  1.33     yamt 		anon->an_ref = 1;
     90  1.54    rmind 		anon->an_lock = NULL;
     91   1.1    chuck 	}
     92  1.33     yamt 	return anon;
     93   1.1    chuck }
     94   1.1    chuck 
     95   1.1    chuck /*
     96  1.55    rmind  * uvm_anfree1: free a single anon.
     97   1.1    chuck  *
     98  1.55    rmind  * => anon must be removed from the amap (if anon was in an amap).
     99  1.55    rmind  * => amap must be locked or anon must not be associated.
    100  1.55    rmind  * => amap lock may be dropped and re-acquired here.
    101   1.1    chuck  */
    102  1.20      chs 
    103  1.54    rmind static void
    104  1.54    rmind uvm_anfree1(struct vm_anon *anon)
    105   1.1    chuck {
    106  1.55    rmind 	struct vm_page *pg = anon->an_page;
    107  1.55    rmind 
    108   1.1    chuck 	UVMHIST_FUNC("uvm_anfree"); UVMHIST_CALLED(maphist);
    109   1.1    chuck 	UVMHIST_LOG(maphist,"(anon=0x%x)", anon, 0,0,0);
    110   1.1    chuck 
    111  1.54    rmind 	KASSERT(anon->an_lock == NULL || mutex_owned(anon->an_lock));
    112  1.12  thorpej 
    113   1.1    chuck 	/*
    114  1.55    rmind 	 * If there is a resident page and it is loaned, then anon may not
    115  1.55    rmind 	 * own it.  Call out to uvm_anon_lockloanpg() to identify and lock
    116  1.55    rmind 	 * the real owner of the page.
    117   1.1    chuck 	 */
    118   1.1    chuck 
    119  1.22      chs 	if (pg && pg->loan_count) {
    120  1.54    rmind 		KASSERT(anon->an_lock != NULL);
    121   1.1    chuck 		pg = uvm_anon_lockloanpg(anon);
    122  1.22      chs 	}
    123   1.1    chuck 
    124   1.1    chuck 	/*
    125  1.55    rmind 	 * Dispose the page, if it is resident.
    126   1.1    chuck 	 */
    127   1.1    chuck 
    128   1.1    chuck 	if (pg) {
    129  1.54    rmind 		KASSERT(anon->an_lock != NULL);
    130   1.1    chuck 
    131   1.1    chuck 		/*
    132  1.55    rmind 		 * If the page is owned by a UVM object (now locked),
    133  1.55    rmind 		 * then kill the loan on the page rather than free it,
    134  1.55    rmind 		 * and release the object lock.
    135   1.1    chuck 		 */
    136   1.1    chuck 
    137   1.1    chuck 		if (pg->uobject) {
    138  1.50       ad 			mutex_enter(&uvm_pageqlock);
    139  1.10      chs 			KASSERT(pg->loan_count > 0);
    140   1.1    chuck 			pg->loan_count--;
    141   1.1    chuck 			pg->uanon = NULL;
    142  1.50       ad 			mutex_exit(&uvm_pageqlock);
    143  1.54    rmind 			mutex_exit(pg->uobject->vmobjlock);
    144   1.1    chuck 		} else {
    145   1.1    chuck 
    146   1.1    chuck 			/*
    147  1.55    rmind 			 * If page has no UVM object, then anon is the owner,
    148  1.55    rmind 			 * and it is already locked.
    149   1.1    chuck 			 */
    150   1.1    chuck 
    151  1.18      chs 			KASSERT((pg->flags & PG_RELEASED) == 0);
    152  1.18      chs 			pmap_page_protect(pg, VM_PROT_NONE);
    153  1.29     yamt 
    154  1.29     yamt 			/*
    155  1.55    rmind 			 * If the page is busy, mark it as PG_RELEASED, so
    156  1.55    rmind 			 * that uvm_anon_release(9) would release it later.
    157  1.29     yamt 			 */
    158  1.29     yamt 
    159  1.29     yamt 			if (pg->flags & PG_BUSY) {
    160  1.29     yamt 				pg->flags |= PG_RELEASED;
    161  1.29     yamt 				return;
    162  1.17      chs 			}
    163  1.50       ad 			mutex_enter(&uvm_pageqlock);
    164  1.29     yamt 			uvm_pagefree(pg);
    165  1.50       ad 			mutex_exit(&uvm_pageqlock);
    166  1.18      chs 			UVMHIST_LOG(maphist, "anon 0x%x, page 0x%x: "
    167  1.18      chs 				    "freed now!", anon, pg, 0, 0);
    168   1.1    chuck 		}
    169   1.1    chuck 	}
    170  1.55    rmind 
    171  1.37     yamt #if defined(VMSWAP)
    172  1.26       pk 	if (pg == NULL && anon->an_swslot > 0) {
    173  1.55    rmind 		/* This page is no longer only in swap. */
    174  1.45       ad 		mutex_enter(&uvm_swap_data_lock);
    175  1.20      chs 		KASSERT(uvmexp.swpgonly > 0);
    176  1.20      chs 		uvmexp.swpgonly--;
    177  1.45       ad 		mutex_exit(&uvm_swap_data_lock);
    178  1.20      chs 	}
    179  1.55    rmind #endif
    180   1.1    chuck 
    181   1.1    chuck 	/*
    182  1.55    rmind 	 * Free any swap resources, leave a page replacement hint, drop
    183  1.55    rmind 	 * the reference on lock and finally destroy the anon itself.
    184   1.1    chuck 	 */
    185  1.18      chs 
    186   1.2      chs 	uvm_anon_dropswap(anon);
    187  1.39     yamt 	uvmpdpol_anfree(anon);
    188  1.39     yamt 
    189  1.33     yamt 	KASSERT(anon->an_page == NULL);
    190  1.37     yamt #if defined(VMSWAP)
    191  1.31     yamt 	KASSERT(anon->an_swslot == 0);
    192  1.55    rmind #endif
    193  1.31     yamt 
    194  1.54    rmind 	if (anon->an_lock != NULL) {
    195  1.54    rmind 		mutex_obj_free(anon->an_lock);
    196  1.54    rmind 	}
    197  1.47       ad 	pool_cache_put(&uvm_anon_cache, anon);
    198   1.1    chuck 	UVMHIST_LOG(maphist,"<- done!",0,0,0,0);
    199   1.2      chs }
    200   1.2      chs 
    201  1.55    rmind /*
    202  1.55    rmind  * uvm_anfree: free a linked list of anon structures.
    203  1.55    rmind  */
    204  1.54    rmind void
    205  1.54    rmind uvm_anfree(struct vm_anon *anon)
    206  1.54    rmind {
    207  1.54    rmind 	struct vm_anon *next;
    208  1.54    rmind 
    209  1.54    rmind 	for (; anon != NULL; anon = next) {
    210  1.55    rmind 		/* Note: clearing an_link also clears a reference count. */
    211  1.54    rmind 		next = anon->an_link;
    212  1.55    rmind 		anon->an_link = NULL;
    213  1.54    rmind 		uvm_anfree1(anon);
    214  1.54    rmind 	}
    215  1.54    rmind }
    216  1.54    rmind 
    217   1.2      chs /*
    218  1.55    rmind  * uvm_anon_lockloanpg: given a locked anon, lock its resident page owner.
    219   1.1    chuck  *
    220   1.1    chuck  * => anon is locked by caller
    221   1.1    chuck  * => on return: anon is locked
    222   1.1    chuck  *		 if there is a resident page:
    223   1.1    chuck  *			if it has a uobject, it is locked by us
    224   1.1    chuck  *			if it is ownerless, we take over as owner
    225   1.1    chuck  *		 we return the resident page (it can change during
    226   1.1    chuck  *		 this function)
    227   1.1    chuck  * => note that the only time an anon has an ownerless resident page
    228   1.1    chuck  *	is if the page was loaned from a uvm_object and the uvm_object
    229   1.1    chuck  *	disowned it
    230   1.1    chuck  * => this only needs to be called when you want to do an operation
    231   1.1    chuck  *	on an anon's resident page and that page has a non-zero loan
    232   1.1    chuck  *	count.
    233   1.1    chuck  */
    234   1.1    chuck struct vm_page *
    235  1.34  thorpej uvm_anon_lockloanpg(struct vm_anon *anon)
    236   1.1    chuck {
    237   1.1    chuck 	struct vm_page *pg;
    238  1.43  thorpej 	bool locked = false;
    239   1.1    chuck 
    240  1.54    rmind 	KASSERT(mutex_owned(anon->an_lock));
    241  1.12  thorpej 
    242   1.1    chuck 	/*
    243   1.1    chuck 	 * loop while we have a resident page that has a non-zero loan count.
    244   1.1    chuck 	 * if we successfully get our lock, we will "break" the loop.
    245   1.1    chuck 	 * note that the test for pg->loan_count is not protected -- this
    246   1.1    chuck 	 * may produce false positive results.   note that a false positive
    247   1.1    chuck 	 * result may cause us to do more work than we need to, but it will
    248   1.1    chuck 	 * not produce an incorrect result.
    249   1.1    chuck 	 */
    250   1.1    chuck 
    251  1.33     yamt 	while (((pg = anon->an_page) != NULL) && pg->loan_count != 0) {
    252   1.1    chuck 
    253   1.1    chuck 		/*
    254   1.1    chuck 		 * quickly check to see if the page has an object before
    255   1.1    chuck 		 * bothering to lock the page queues.   this may also produce
    256   1.1    chuck 		 * a false positive result, but that's ok because we do a real
    257   1.1    chuck 		 * check after that.
    258   1.1    chuck 		 */
    259   1.1    chuck 
    260   1.1    chuck 		if (pg->uobject) {
    261  1.50       ad 			mutex_enter(&uvm_pageqlock);
    262  1.18      chs 			if (pg->uobject) {
    263   1.1    chuck 				locked =
    264  1.54    rmind 				    mutex_tryenter(pg->uobject->vmobjlock);
    265   1.1    chuck 			} else {
    266   1.1    chuck 				/* object disowned before we got PQ lock */
    267  1.43  thorpej 				locked = true;
    268   1.1    chuck 			}
    269  1.50       ad 			mutex_exit(&uvm_pageqlock);
    270   1.1    chuck 
    271   1.1    chuck 			/*
    272   1.1    chuck 			 * if we didn't get a lock (try lock failed), then we
    273   1.1    chuck 			 * toggle our anon lock and try again
    274   1.1    chuck 			 */
    275   1.1    chuck 
    276   1.1    chuck 			if (!locked) {
    277   1.1    chuck 				/*
    278   1.1    chuck 				 * someone locking the object has a chance to
    279   1.1    chuck 				 * lock us right now
    280  1.54    rmind 				 *
    281  1.54    rmind 				 * XXX Better than yielding but inadequate.
    282   1.1    chuck 				 */
    283  1.54    rmind 				kpause("livelock", false, 1, anon->an_lock);
    284  1.10      chs 				continue;
    285   1.1    chuck 			}
    286   1.1    chuck 		}
    287   1.1    chuck 
    288   1.1    chuck 		/*
    289  1.55    rmind 		 * If page is un-owned i.e. the object dropped its ownership,
    290  1.55    rmind 		 * then we have to take the ownership.
    291   1.1    chuck 		 */
    292   1.1    chuck 
    293   1.1    chuck 		if (pg->uobject == NULL && (pg->pqflags & PQ_ANON) == 0) {
    294  1.50       ad 			mutex_enter(&uvm_pageqlock);
    295  1.18      chs 			pg->pqflags |= PQ_ANON;
    296  1.18      chs 			pg->loan_count--;
    297  1.50       ad 			mutex_exit(&uvm_pageqlock);
    298   1.1    chuck 		}
    299   1.1    chuck 		break;
    300   1.1    chuck 	}
    301  1.55    rmind 	return pg;
    302   1.5      chs }
    303   1.5      chs 
    304  1.37     yamt #if defined(VMSWAP)
    305  1.37     yamt 
    306   1.5      chs /*
    307  1.55    rmind  * uvm_anon_pagein: fetch an anon's page.
    308   1.5      chs  *
    309   1.5      chs  * => anon must be locked, and is unlocked upon return.
    310  1.43  thorpej  * => returns true if pagein was aborted due to lack of memory.
    311   1.5      chs  */
    312   1.5      chs 
    313  1.42  thorpej bool
    314  1.34  thorpej uvm_anon_pagein(struct vm_anon *anon)
    315   1.5      chs {
    316   1.5      chs 	struct vm_page *pg;
    317   1.5      chs 	struct uvm_object *uobj;
    318   1.8  thorpej 
    319  1.54    rmind 	KASSERT(mutex_owned(anon->an_lock));
    320  1.12  thorpej 
    321   1.8  thorpej 	/*
    322  1.55    rmind 	 * Get the page of the anon.
    323   1.8  thorpej 	 */
    324   1.5      chs 
    325  1.55    rmind 	switch (uvmfault_anonget(NULL, NULL, anon)) {
    326  1.16      chs 	case 0:
    327  1.55    rmind 		/* Success - we have the page. */
    328  1.55    rmind 		KASSERT(mutex_owned(anon->an_lock));
    329   1.5      chs 		break;
    330  1.16      chs 	case EIO:
    331  1.16      chs 	case ERESTART:
    332   1.5      chs 		/*
    333  1.55    rmind 		 * Nothing more to do on errors.  ERESTART means that the
    334  1.55    rmind 		 * anon was freed.
    335   1.5      chs 		 */
    336  1.43  thorpej 		return false;
    337  1.25       pk 	default:
    338  1.43  thorpej 		return true;
    339   1.5      chs 	}
    340   1.5      chs 
    341   1.5      chs 	/*
    342  1.55    rmind 	 * Mark the page as dirty, clear its swslot and un-busy it.
    343   1.5      chs 	 */
    344   1.5      chs 
    345  1.33     yamt 	pg = anon->an_page;
    346   1.5      chs 	uobj = pg->uobject;
    347  1.55    rmind 	if (anon->an_swslot > 0) {
    348  1.23       pk 		uvm_swap_free(anon->an_swslot, 1);
    349  1.55    rmind 	}
    350   1.5      chs 	anon->an_swslot = 0;
    351  1.55    rmind 	pg->flags &= ~PG_CLEAN;
    352   1.5      chs 
    353   1.5      chs 	/*
    354  1.55    rmind 	 * Deactivate the page (to put it on a page queue).
    355   1.5      chs 	 */
    356   1.5      chs 
    357  1.50       ad 	mutex_enter(&uvm_pageqlock);
    358  1.55    rmind 	if (pg->wire_count == 0) {
    359  1.24       pk 		uvm_pagedeactivate(pg);
    360  1.55    rmind 	}
    361  1.50       ad 	mutex_exit(&uvm_pageqlock);
    362  1.25       pk 
    363  1.25       pk 	if (pg->flags & PG_WANTED) {
    364  1.55    rmind 		pg->flags &= ~PG_WANTED;
    365  1.25       pk 		wakeup(pg);
    366  1.25       pk 	}
    367   1.5      chs 
    368  1.54    rmind 	mutex_exit(anon->an_lock);
    369   1.5      chs 	if (uobj) {
    370  1.54    rmind 		mutex_exit(uobj->vmobjlock);
    371   1.5      chs 	}
    372  1.43  thorpej 	return false;
    373   1.1    chuck }
    374  1.29     yamt 
    375  1.55    rmind /*
    376  1.55    rmind  * uvm_anon_dropswap: release any swap resources from this anon.
    377  1.55    rmind  *
    378  1.55    rmind  * => anon must be locked or have a reference count of 0.
    379  1.55    rmind  */
    380  1.55    rmind void
    381  1.55    rmind uvm_anon_dropswap(struct vm_anon *anon)
    382  1.55    rmind {
    383  1.55    rmind 	UVMHIST_FUNC("uvm_anon_dropswap"); UVMHIST_CALLED(maphist);
    384  1.55    rmind 
    385  1.55    rmind 	if (anon->an_swslot == 0)
    386  1.55    rmind 		return;
    387  1.55    rmind 
    388  1.55    rmind 	UVMHIST_LOG(maphist,"freeing swap for anon %p, paged to swslot 0x%x",
    389  1.55    rmind 		    anon, anon->an_swslot, 0, 0);
    390  1.55    rmind 	uvm_swap_free(anon->an_swslot, 1);
    391  1.55    rmind 	anon->an_swslot = 0;
    392  1.55    rmind }
    393  1.55    rmind 
    394  1.55    rmind #endif
    395  1.37     yamt 
    396  1.29     yamt /*
    397  1.29     yamt  * uvm_anon_release: release an anon and its page.
    398  1.29     yamt  *
    399  1.55    rmind  * => anon should not have any references.
    400  1.55    rmind  * => anon must be locked.
    401  1.29     yamt  */
    402  1.29     yamt 
    403  1.29     yamt void
    404  1.34  thorpej uvm_anon_release(struct vm_anon *anon)
    405  1.29     yamt {
    406  1.33     yamt 	struct vm_page *pg = anon->an_page;
    407  1.29     yamt 
    408  1.54    rmind 	KASSERT(mutex_owned(anon->an_lock));
    409  1.29     yamt 	KASSERT(pg != NULL);
    410  1.29     yamt 	KASSERT((pg->flags & PG_RELEASED) != 0);
    411  1.29     yamt 	KASSERT((pg->flags & PG_BUSY) != 0);
    412  1.29     yamt 	KASSERT(pg->uobject == NULL);
    413  1.29     yamt 	KASSERT(pg->uanon == anon);
    414  1.29     yamt 	KASSERT(pg->loan_count == 0);
    415  1.29     yamt 	KASSERT(anon->an_ref == 0);
    416  1.29     yamt 
    417  1.50       ad 	mutex_enter(&uvm_pageqlock);
    418  1.29     yamt 	uvm_pagefree(pg);
    419  1.50       ad 	mutex_exit(&uvm_pageqlock);
    420  1.54    rmind 	mutex_exit(anon->an_lock);
    421  1.29     yamt 
    422  1.33     yamt 	KASSERT(anon->an_page == NULL);
    423  1.29     yamt 
    424  1.29     yamt 	uvm_anfree(anon);
    425  1.29     yamt }
    426