Home | History | Annotate | Line # | Download | only in uvm
uvm_anon.c revision 1.28
      1 /*	$NetBSD: uvm_anon.c,v 1.28 2004/03/24 07:55:01 junyoung Exp $	*/
      2 
      3 /*
      4  *
      5  * Copyright (c) 1997 Charles D. Cranor and Washington University.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *      This product includes software developed by Charles D. Cranor and
     19  *      Washington University.
     20  * 4. The name of the author may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * uvm_anon.c: uvm anon ops
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: uvm_anon.c,v 1.28 2004/03/24 07:55:01 junyoung Exp $");
     41 
     42 #include "opt_uvmhist.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/proc.h>
     47 #include <sys/malloc.h>
     48 #include <sys/pool.h>
     49 #include <sys/kernel.h>
     50 
     51 #include <uvm/uvm.h>
     52 #include <uvm/uvm_swap.h>
     53 
     54 /*
     55  * anonblock_list: global list of anon blocks,
     56  * locked by swap_syscall_lock (since we never remove
     57  * anything from this list and we only add to it via swapctl(2)).
     58  */
     59 
     60 struct uvm_anonblock {
     61 	LIST_ENTRY(uvm_anonblock) list;
     62 	int count;
     63 	struct vm_anon *anons;
     64 };
     65 static LIST_HEAD(anonlist, uvm_anonblock) anonblock_list;
     66 
     67 
     68 static boolean_t anon_pagein(struct vm_anon *);
     69 
     70 
     71 /*
     72  * allocate anons
     73  */
     74 void
     75 uvm_anon_init()
     76 {
     77 	int nanon = uvmexp.free - (uvmexp.free / 16); /* XXXCDC ??? */
     78 
     79 	simple_lock_init(&uvm.afreelock);
     80 	LIST_INIT(&anonblock_list);
     81 
     82 	/*
     83 	 * Allocate the initial anons.
     84 	 */
     85 	uvm_anon_add(nanon);
     86 }
     87 
     88 /*
     89  * add some more anons to the free pool.  called when we add
     90  * more swap space.
     91  *
     92  * => swap_syscall_lock should be held (protects anonblock_list).
     93  */
     94 int
     95 uvm_anon_add(count)
     96 	int	count;
     97 {
     98 	struct uvm_anonblock *anonblock;
     99 	struct vm_anon *anon;
    100 	int lcv, needed;
    101 
    102 	simple_lock(&uvm.afreelock);
    103 	uvmexp.nanonneeded += count;
    104 	needed = uvmexp.nanonneeded - uvmexp.nanon;
    105 	simple_unlock(&uvm.afreelock);
    106 
    107 	if (needed <= 0) {
    108 		return 0;
    109 	}
    110 	anon = (void *)uvm_km_alloc(kernel_map, sizeof(*anon) * needed);
    111 	if (anon == NULL) {
    112 		simple_lock(&uvm.afreelock);
    113 		uvmexp.nanonneeded -= count;
    114 		simple_unlock(&uvm.afreelock);
    115 		return ENOMEM;
    116 	}
    117 	MALLOC(anonblock, void *, sizeof(*anonblock), M_UVMAMAP, M_WAITOK);
    118 
    119 	anonblock->count = needed;
    120 	anonblock->anons = anon;
    121 	LIST_INSERT_HEAD(&anonblock_list, anonblock, list);
    122 	memset(anon, 0, sizeof(*anon) * needed);
    123 
    124 	simple_lock(&uvm.afreelock);
    125 	uvmexp.nanon += needed;
    126 	uvmexp.nfreeanon += needed;
    127 	for (lcv = 0; lcv < needed; lcv++) {
    128 		simple_lock_init(&anon[lcv].an_lock);
    129 		anon[lcv].u.an_nxt = uvm.afree;
    130 		uvm.afree = &anon[lcv];
    131 	}
    132 	simple_unlock(&uvm.afreelock);
    133 	return 0;
    134 }
    135 
    136 /*
    137  * remove anons from the free pool.
    138  */
    139 void
    140 uvm_anon_remove(count)
    141 	int count;
    142 {
    143 	/*
    144 	 * we never actually free any anons, to avoid allocation overhead.
    145 	 * XXX someday we might want to try to free anons.
    146 	 */
    147 
    148 	simple_lock(&uvm.afreelock);
    149 	uvmexp.nanonneeded -= count;
    150 	simple_unlock(&uvm.afreelock);
    151 }
    152 
    153 /*
    154  * allocate an anon
    155  *
    156  * => new anon is returned locked!
    157  */
    158 struct vm_anon *
    159 uvm_analloc()
    160 {
    161 	struct vm_anon *a;
    162 
    163 	simple_lock(&uvm.afreelock);
    164 	a = uvm.afree;
    165 	if (a) {
    166 		uvm.afree = a->u.an_nxt;
    167 		uvmexp.nfreeanon--;
    168 		a->an_ref = 1;
    169 		a->an_swslot = 0;
    170 		a->u.an_page = NULL;		/* so we can free quickly */
    171 		LOCK_ASSERT(simple_lock_held(&a->an_lock) == 0);
    172 		simple_lock(&a->an_lock);
    173 	}
    174 	simple_unlock(&uvm.afreelock);
    175 	return(a);
    176 }
    177 
    178 /*
    179  * uvm_anfree: free a single anon structure
    180  *
    181  * => caller must remove anon from its amap before calling (if it was in
    182  *	an amap).
    183  * => anon must be unlocked and have a zero reference count.
    184  * => we may lock the pageq's.
    185  */
    186 
    187 void
    188 uvm_anfree(anon)
    189 	struct vm_anon *anon;
    190 {
    191 	struct vm_page *pg;
    192 	UVMHIST_FUNC("uvm_anfree"); UVMHIST_CALLED(maphist);
    193 	UVMHIST_LOG(maphist,"(anon=0x%x)", anon, 0,0,0);
    194 
    195 	KASSERT(anon->an_ref == 0);
    196 	LOCK_ASSERT(!simple_lock_held(&anon->an_lock));
    197 
    198 	/*
    199 	 * get page
    200 	 */
    201 
    202 	pg = anon->u.an_page;
    203 
    204 	/*
    205 	 * if there is a resident page and it is loaned, then anon may not
    206 	 * own it.   call out to uvm_anon_lockpage() to ensure the real owner
    207  	 * of the page has been identified and locked.
    208 	 */
    209 
    210 	if (pg && pg->loan_count) {
    211 		simple_lock(&anon->an_lock);
    212 		pg = uvm_anon_lockloanpg(anon);
    213 		simple_unlock(&anon->an_lock);
    214 	}
    215 
    216 	/*
    217 	 * if we have a resident page, we must dispose of it before freeing
    218 	 * the anon.
    219 	 */
    220 
    221 	if (pg) {
    222 
    223 		/*
    224 		 * if the page is owned by a uobject (now locked), then we must
    225 		 * kill the loan on the page rather than free it.
    226 		 */
    227 
    228 		if (pg->uobject) {
    229 			uvm_lock_pageq();
    230 			KASSERT(pg->loan_count > 0);
    231 			pg->loan_count--;
    232 			pg->uanon = NULL;
    233 			uvm_unlock_pageq();
    234 			simple_unlock(&pg->uobject->vmobjlock);
    235 		} else {
    236 
    237 			/*
    238 			 * page has no uobject, so we must be the owner of it.
    239 			 * if page is busy then we wait until it is not busy,
    240 			 * and then free it.
    241 			 */
    242 
    243 			KASSERT((pg->flags & PG_RELEASED) == 0);
    244 			simple_lock(&anon->an_lock);
    245 			pmap_page_protect(pg, VM_PROT_NONE);
    246 			while ((pg = anon->u.an_page) &&
    247 			       (pg->flags & PG_BUSY) != 0) {
    248 				pg->flags |= PG_WANTED;
    249 				UVM_UNLOCK_AND_WAIT(pg, &anon->an_lock, 0,
    250 				    "anfree", 0);
    251 				simple_lock(&anon->an_lock);
    252 			}
    253 			if (pg) {
    254 				uvm_lock_pageq();
    255 				uvm_pagefree(pg);
    256 				uvm_unlock_pageq();
    257 			}
    258 			simple_unlock(&anon->an_lock);
    259 			UVMHIST_LOG(maphist, "anon 0x%x, page 0x%x: "
    260 				    "freed now!", anon, pg, 0, 0);
    261 		}
    262 	}
    263 	if (pg == NULL && anon->an_swslot > 0) {
    264 		/* this page is no longer only in swap. */
    265 		simple_lock(&uvm.swap_data_lock);
    266 		KASSERT(uvmexp.swpgonly > 0);
    267 		uvmexp.swpgonly--;
    268 		simple_unlock(&uvm.swap_data_lock);
    269 	}
    270 
    271 	/*
    272 	 * free any swap resources.
    273 	 */
    274 
    275 	uvm_anon_dropswap(anon);
    276 
    277 	/*
    278 	 * now that we've stripped the data areas from the anon,
    279 	 * free the anon itself.
    280 	 */
    281 
    282 	simple_lock(&uvm.afreelock);
    283 	anon->u.an_nxt = uvm.afree;
    284 	uvm.afree = anon;
    285 	uvmexp.nfreeanon++;
    286 	simple_unlock(&uvm.afreelock);
    287 	UVMHIST_LOG(maphist,"<- done!",0,0,0,0);
    288 }
    289 
    290 /*
    291  * uvm_anon_dropswap:  release any swap resources from this anon.
    292  *
    293  * => anon must be locked or have a reference count of 0.
    294  */
    295 void
    296 uvm_anon_dropswap(anon)
    297 	struct vm_anon *anon;
    298 {
    299 	UVMHIST_FUNC("uvm_anon_dropswap"); UVMHIST_CALLED(maphist);
    300 
    301 	if (anon->an_swslot == 0)
    302 		return;
    303 
    304 	UVMHIST_LOG(maphist,"freeing swap for anon %p, paged to swslot 0x%x",
    305 		    anon, anon->an_swslot, 0, 0);
    306 	uvm_swap_free(anon->an_swslot, 1);
    307 	anon->an_swslot = 0;
    308 }
    309 
    310 /*
    311  * uvm_anon_lockloanpg: given a locked anon, lock its resident page
    312  *
    313  * => anon is locked by caller
    314  * => on return: anon is locked
    315  *		 if there is a resident page:
    316  *			if it has a uobject, it is locked by us
    317  *			if it is ownerless, we take over as owner
    318  *		 we return the resident page (it can change during
    319  *		 this function)
    320  * => note that the only time an anon has an ownerless resident page
    321  *	is if the page was loaned from a uvm_object and the uvm_object
    322  *	disowned it
    323  * => this only needs to be called when you want to do an operation
    324  *	on an anon's resident page and that page has a non-zero loan
    325  *	count.
    326  */
    327 struct vm_page *
    328 uvm_anon_lockloanpg(anon)
    329 	struct vm_anon *anon;
    330 {
    331 	struct vm_page *pg;
    332 	boolean_t locked = FALSE;
    333 
    334 	LOCK_ASSERT(simple_lock_held(&anon->an_lock));
    335 
    336 	/*
    337 	 * loop while we have a resident page that has a non-zero loan count.
    338 	 * if we successfully get our lock, we will "break" the loop.
    339 	 * note that the test for pg->loan_count is not protected -- this
    340 	 * may produce false positive results.   note that a false positive
    341 	 * result may cause us to do more work than we need to, but it will
    342 	 * not produce an incorrect result.
    343 	 */
    344 
    345 	while (((pg = anon->u.an_page) != NULL) && pg->loan_count != 0) {
    346 
    347 		/*
    348 		 * quickly check to see if the page has an object before
    349 		 * bothering to lock the page queues.   this may also produce
    350 		 * a false positive result, but that's ok because we do a real
    351 		 * check after that.
    352 		 */
    353 
    354 		if (pg->uobject) {
    355 			uvm_lock_pageq();
    356 			if (pg->uobject) {
    357 				locked =
    358 				    simple_lock_try(&pg->uobject->vmobjlock);
    359 			} else {
    360 				/* object disowned before we got PQ lock */
    361 				locked = TRUE;
    362 			}
    363 			uvm_unlock_pageq();
    364 
    365 			/*
    366 			 * if we didn't get a lock (try lock failed), then we
    367 			 * toggle our anon lock and try again
    368 			 */
    369 
    370 			if (!locked) {
    371 				simple_unlock(&anon->an_lock);
    372 
    373 				/*
    374 				 * someone locking the object has a chance to
    375 				 * lock us right now
    376 				 */
    377 
    378 				simple_lock(&anon->an_lock);
    379 				continue;
    380 			}
    381 		}
    382 
    383 		/*
    384 		 * if page is un-owned [i.e. the object dropped its ownership],
    385 		 * then we can take over as owner!
    386 		 */
    387 
    388 		if (pg->uobject == NULL && (pg->pqflags & PQ_ANON) == 0) {
    389 			uvm_lock_pageq();
    390 			pg->pqflags |= PQ_ANON;
    391 			pg->loan_count--;
    392 			uvm_unlock_pageq();
    393 		}
    394 		break;
    395 	}
    396 	return(pg);
    397 }
    398 
    399 
    400 
    401 /*
    402  * page in every anon that is paged out to a range of swslots.
    403  *
    404  * swap_syscall_lock should be held (protects anonblock_list).
    405  */
    406 
    407 boolean_t
    408 anon_swap_off(startslot, endslot)
    409 	int startslot, endslot;
    410 {
    411 	struct uvm_anonblock *anonblock;
    412 
    413 	LIST_FOREACH(anonblock, &anonblock_list, list) {
    414 		int i;
    415 
    416 		/*
    417 		 * loop thru all the anons in the anonblock,
    418 		 * paging in where needed.
    419 		 */
    420 
    421 		for (i = 0; i < anonblock->count; i++) {
    422 			struct vm_anon *anon = &anonblock->anons[i];
    423 			int slot;
    424 
    425 			/*
    426 			 * lock anon to work on it.
    427 			 */
    428 
    429 			simple_lock(&anon->an_lock);
    430 
    431 			/*
    432 			 * is this anon's swap slot in range?
    433 			 */
    434 
    435 			slot = anon->an_swslot;
    436 			if (slot >= startslot && slot < endslot) {
    437 				boolean_t rv;
    438 
    439 				/*
    440 				 * yup, page it in.
    441 				 */
    442 
    443 				/* locked: anon */
    444 				rv = anon_pagein(anon);
    445 				/* unlocked: anon */
    446 
    447 				if (rv) {
    448 					return rv;
    449 				}
    450 			} else {
    451 
    452 				/*
    453 				 * nope, unlock and proceed.
    454 				 */
    455 
    456 				simple_unlock(&anon->an_lock);
    457 			}
    458 		}
    459 	}
    460 	return FALSE;
    461 }
    462 
    463 
    464 /*
    465  * fetch an anon's page.
    466  *
    467  * => anon must be locked, and is unlocked upon return.
    468  * => returns TRUE if pagein was aborted due to lack of memory.
    469  */
    470 
    471 static boolean_t
    472 anon_pagein(anon)
    473 	struct vm_anon *anon;
    474 {
    475 	struct vm_page *pg;
    476 	struct uvm_object *uobj;
    477 	int rv;
    478 
    479 	/* locked: anon */
    480 	LOCK_ASSERT(simple_lock_held(&anon->an_lock));
    481 
    482 	rv = uvmfault_anonget(NULL, NULL, anon);
    483 
    484 	/*
    485 	 * if rv == 0, anon is still locked, else anon
    486 	 * is unlocked
    487 	 */
    488 
    489 	switch (rv) {
    490 	case 0:
    491 		break;
    492 
    493 	case EIO:
    494 	case ERESTART:
    495 
    496 		/*
    497 		 * nothing more to do on errors.
    498 		 * ERESTART can only mean that the anon was freed,
    499 		 * so again there's nothing to do.
    500 		 */
    501 
    502 		return FALSE;
    503 
    504 	default:
    505 		return TRUE;
    506 	}
    507 
    508 	/*
    509 	 * ok, we've got the page now.
    510 	 * mark it as dirty, clear its swslot and un-busy it.
    511 	 */
    512 
    513 	pg = anon->u.an_page;
    514 	uobj = pg->uobject;
    515 	if (anon->an_swslot > 0)
    516 		uvm_swap_free(anon->an_swslot, 1);
    517 	anon->an_swslot = 0;
    518 	pg->flags &= ~(PG_CLEAN);
    519 
    520 	/*
    521 	 * deactivate the page (to put it on a page queue)
    522 	 */
    523 
    524 	pmap_clear_reference(pg);
    525 	uvm_lock_pageq();
    526 	if (pg->wire_count == 0)
    527 		uvm_pagedeactivate(pg);
    528 	uvm_unlock_pageq();
    529 
    530 	if (pg->flags & PG_WANTED) {
    531 		wakeup(pg);
    532 		pg->flags &= ~(PG_WANTED);
    533 	}
    534 
    535 	/*
    536 	 * unlock the anon and we're done.
    537 	 */
    538 
    539 	simple_unlock(&anon->an_lock);
    540 	if (uobj) {
    541 		simple_unlock(&uobj->vmobjlock);
    542 	}
    543 	return FALSE;
    544 }
    545