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