Home | History | Annotate | Line # | Download | only in uvm
uvm_anon.c revision 1.34
      1 /*	$NetBSD: uvm_anon.c,v 1.34 2005/06/27 02:19:48 thorpej 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.34 2005/06/27 02:19:48 thorpej 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 static POOL_INIT(uvm_anon_pool, sizeof(struct vm_anon), 0, 0, 0, "anonpl",
     55     &pool_allocator_nointr);
     56 static struct pool_cache uvm_anon_pool_cache;
     57 
     58 static int uvm_anon_ctor(void *, void *, int);
     59 
     60 /*
     61  * allocate anons
     62  */
     63 void
     64 uvm_anon_init(void)
     65 {
     66 
     67 	pool_cache_init(&uvm_anon_pool_cache, &uvm_anon_pool,
     68 	    uvm_anon_ctor, NULL, NULL);
     69 }
     70 
     71 static int
     72 uvm_anon_ctor(void *arg, void *object, int flags)
     73 {
     74 	struct vm_anon *anon = object;
     75 
     76 	anon->an_ref = 0;
     77 	simple_lock_init(&anon->an_lock);
     78 	anon->an_page = NULL;
     79 	anon->an_swslot = 0;
     80 
     81 	return 0;
     82 }
     83 
     84 /*
     85  * allocate an anon
     86  *
     87  * => new anon is returned locked!
     88  */
     89 struct vm_anon *
     90 uvm_analloc(void)
     91 {
     92 	struct vm_anon *anon;
     93 
     94 	anon = pool_cache_get(&uvm_anon_pool_cache, PR_NOWAIT);
     95 	if (anon) {
     96 		KASSERT(anon->an_ref == 0);
     97 		LOCK_ASSERT(simple_lock_held(&anon->an_lock) == 0);
     98 		KASSERT(anon->an_page == NULL);
     99 		KASSERT(anon->an_swslot == 0);
    100 		anon->an_ref = 1;
    101 		simple_lock(&anon->an_lock);
    102 	}
    103 	return anon;
    104 }
    105 
    106 /*
    107  * uvm_anfree: free a single anon structure
    108  *
    109  * => caller must remove anon from its amap before calling (if it was in
    110  *	an amap).
    111  * => anon must be unlocked and have a zero reference count.
    112  * => we may lock the pageq's.
    113  */
    114 
    115 void
    116 uvm_anfree(struct vm_anon *anon)
    117 {
    118 	struct vm_page *pg;
    119 	UVMHIST_FUNC("uvm_anfree"); UVMHIST_CALLED(maphist);
    120 	UVMHIST_LOG(maphist,"(anon=0x%x)", anon, 0,0,0);
    121 
    122 	KASSERT(anon->an_ref == 0);
    123 	LOCK_ASSERT(!simple_lock_held(&anon->an_lock));
    124 
    125 	/*
    126 	 * get page
    127 	 */
    128 
    129 	pg = anon->an_page;
    130 
    131 	/*
    132 	 * if there is a resident page and it is loaned, then anon may not
    133 	 * own it.   call out to uvm_anon_lockpage() to ensure the real owner
    134  	 * of the page has been identified and locked.
    135 	 */
    136 
    137 	if (pg && pg->loan_count) {
    138 		simple_lock(&anon->an_lock);
    139 		pg = uvm_anon_lockloanpg(anon);
    140 		simple_unlock(&anon->an_lock);
    141 	}
    142 
    143 	/*
    144 	 * if we have a resident page, we must dispose of it before freeing
    145 	 * the anon.
    146 	 */
    147 
    148 	if (pg) {
    149 
    150 		/*
    151 		 * if the page is owned by a uobject (now locked), then we must
    152 		 * kill the loan on the page rather than free it.
    153 		 */
    154 
    155 		if (pg->uobject) {
    156 			uvm_lock_pageq();
    157 			KASSERT(pg->loan_count > 0);
    158 			pg->loan_count--;
    159 			pg->uanon = NULL;
    160 			uvm_unlock_pageq();
    161 			simple_unlock(&pg->uobject->vmobjlock);
    162 		} else {
    163 
    164 			/*
    165 			 * page has no uobject, so we must be the owner of it.
    166 			 */
    167 
    168 			KASSERT((pg->flags & PG_RELEASED) == 0);
    169 			simple_lock(&anon->an_lock);
    170 			pmap_page_protect(pg, VM_PROT_NONE);
    171 
    172 			/*
    173 			 * if the page is busy, mark it as PG_RELEASED
    174 			 * so that uvm_anon_release will release it later.
    175 			 */
    176 
    177 			if (pg->flags & PG_BUSY) {
    178 				pg->flags |= PG_RELEASED;
    179 				simple_unlock(&anon->an_lock);
    180 				return;
    181 			}
    182 			uvm_lock_pageq();
    183 			uvm_pagefree(pg);
    184 			uvm_unlock_pageq();
    185 			simple_unlock(&anon->an_lock);
    186 			UVMHIST_LOG(maphist, "anon 0x%x, page 0x%x: "
    187 				    "freed now!", anon, pg, 0, 0);
    188 		}
    189 	}
    190 	if (pg == NULL && anon->an_swslot > 0) {
    191 		/* this page is no longer only in swap. */
    192 		simple_lock(&uvm.swap_data_lock);
    193 		KASSERT(uvmexp.swpgonly > 0);
    194 		uvmexp.swpgonly--;
    195 		simple_unlock(&uvm.swap_data_lock);
    196 	}
    197 
    198 	/*
    199 	 * free any swap resources.
    200 	 */
    201 
    202 	uvm_anon_dropswap(anon);
    203 
    204 	/*
    205 	 * now that we've stripped the data areas from the anon,
    206 	 * free the anon itself.
    207 	 */
    208 
    209 	KASSERT(anon->an_page == NULL);
    210 	KASSERT(anon->an_swslot == 0);
    211 
    212 	pool_cache_put(&uvm_anon_pool_cache, anon);
    213 	UVMHIST_LOG(maphist,"<- done!",0,0,0,0);
    214 }
    215 
    216 /*
    217  * uvm_anon_dropswap:  release any swap resources from this anon.
    218  *
    219  * => anon must be locked or have a reference count of 0.
    220  */
    221 void
    222 uvm_anon_dropswap(struct vm_anon *anon)
    223 {
    224 	UVMHIST_FUNC("uvm_anon_dropswap"); UVMHIST_CALLED(maphist);
    225 
    226 	if (anon->an_swslot == 0)
    227 		return;
    228 
    229 	UVMHIST_LOG(maphist,"freeing swap for anon %p, paged to swslot 0x%x",
    230 		    anon, anon->an_swslot, 0, 0);
    231 	uvm_swap_free(anon->an_swslot, 1);
    232 	anon->an_swslot = 0;
    233 }
    234 
    235 /*
    236  * uvm_anon_lockloanpg: given a locked anon, lock its resident page
    237  *
    238  * => anon is locked by caller
    239  * => on return: anon is locked
    240  *		 if there is a resident page:
    241  *			if it has a uobject, it is locked by us
    242  *			if it is ownerless, we take over as owner
    243  *		 we return the resident page (it can change during
    244  *		 this function)
    245  * => note that the only time an anon has an ownerless resident page
    246  *	is if the page was loaned from a uvm_object and the uvm_object
    247  *	disowned it
    248  * => this only needs to be called when you want to do an operation
    249  *	on an anon's resident page and that page has a non-zero loan
    250  *	count.
    251  */
    252 struct vm_page *
    253 uvm_anon_lockloanpg(struct vm_anon *anon)
    254 {
    255 	struct vm_page *pg;
    256 	boolean_t locked = FALSE;
    257 
    258 	LOCK_ASSERT(simple_lock_held(&anon->an_lock));
    259 
    260 	/*
    261 	 * loop while we have a resident page that has a non-zero loan count.
    262 	 * if we successfully get our lock, we will "break" the loop.
    263 	 * note that the test for pg->loan_count is not protected -- this
    264 	 * may produce false positive results.   note that a false positive
    265 	 * result may cause us to do more work than we need to, but it will
    266 	 * not produce an incorrect result.
    267 	 */
    268 
    269 	while (((pg = anon->an_page) != NULL) && pg->loan_count != 0) {
    270 
    271 		/*
    272 		 * quickly check to see if the page has an object before
    273 		 * bothering to lock the page queues.   this may also produce
    274 		 * a false positive result, but that's ok because we do a real
    275 		 * check after that.
    276 		 */
    277 
    278 		if (pg->uobject) {
    279 			uvm_lock_pageq();
    280 			if (pg->uobject) {
    281 				locked =
    282 				    simple_lock_try(&pg->uobject->vmobjlock);
    283 			} else {
    284 				/* object disowned before we got PQ lock */
    285 				locked = TRUE;
    286 			}
    287 			uvm_unlock_pageq();
    288 
    289 			/*
    290 			 * if we didn't get a lock (try lock failed), then we
    291 			 * toggle our anon lock and try again
    292 			 */
    293 
    294 			if (!locked) {
    295 				simple_unlock(&anon->an_lock);
    296 
    297 				/*
    298 				 * someone locking the object has a chance to
    299 				 * lock us right now
    300 				 */
    301 
    302 				simple_lock(&anon->an_lock);
    303 				continue;
    304 			}
    305 		}
    306 
    307 		/*
    308 		 * if page is un-owned [i.e. the object dropped its ownership],
    309 		 * then we can take over as owner!
    310 		 */
    311 
    312 		if (pg->uobject == NULL && (pg->pqflags & PQ_ANON) == 0) {
    313 			uvm_lock_pageq();
    314 			pg->pqflags |= PQ_ANON;
    315 			pg->loan_count--;
    316 			uvm_unlock_pageq();
    317 		}
    318 		break;
    319 	}
    320 	return(pg);
    321 }
    322 
    323 /*
    324  * fetch an anon's page.
    325  *
    326  * => anon must be locked, and is unlocked upon return.
    327  * => returns TRUE if pagein was aborted due to lack of memory.
    328  */
    329 
    330 boolean_t
    331 uvm_anon_pagein(struct vm_anon *anon)
    332 {
    333 	struct vm_page *pg;
    334 	struct uvm_object *uobj;
    335 	int rv;
    336 
    337 	/* locked: anon */
    338 	LOCK_ASSERT(simple_lock_held(&anon->an_lock));
    339 
    340 	rv = uvmfault_anonget(NULL, NULL, anon);
    341 
    342 	/*
    343 	 * if rv == 0, anon is still locked, else anon
    344 	 * is unlocked
    345 	 */
    346 
    347 	switch (rv) {
    348 	case 0:
    349 		break;
    350 
    351 	case EIO:
    352 	case ERESTART:
    353 
    354 		/*
    355 		 * nothing more to do on errors.
    356 		 * ERESTART can only mean that the anon was freed,
    357 		 * so again there's nothing to do.
    358 		 */
    359 
    360 		return FALSE;
    361 
    362 	default:
    363 		return TRUE;
    364 	}
    365 
    366 	/*
    367 	 * ok, we've got the page now.
    368 	 * mark it as dirty, clear its swslot and un-busy it.
    369 	 */
    370 
    371 	pg = anon->an_page;
    372 	uobj = pg->uobject;
    373 	if (anon->an_swslot > 0)
    374 		uvm_swap_free(anon->an_swslot, 1);
    375 	anon->an_swslot = 0;
    376 	pg->flags &= ~(PG_CLEAN);
    377 
    378 	/*
    379 	 * deactivate the page (to put it on a page queue)
    380 	 */
    381 
    382 	pmap_clear_reference(pg);
    383 	uvm_lock_pageq();
    384 	if (pg->wire_count == 0)
    385 		uvm_pagedeactivate(pg);
    386 	uvm_unlock_pageq();
    387 
    388 	if (pg->flags & PG_WANTED) {
    389 		wakeup(pg);
    390 		pg->flags &= ~(PG_WANTED);
    391 	}
    392 
    393 	/*
    394 	 * unlock the anon and we're done.
    395 	 */
    396 
    397 	simple_unlock(&anon->an_lock);
    398 	if (uobj) {
    399 		simple_unlock(&uobj->vmobjlock);
    400 	}
    401 	return FALSE;
    402 }
    403 
    404 /*
    405  * uvm_anon_release: release an anon and its page.
    406  *
    407  * => caller must lock the anon.
    408  */
    409 
    410 void
    411 uvm_anon_release(struct vm_anon *anon)
    412 {
    413 	struct vm_page *pg = anon->an_page;
    414 
    415 	LOCK_ASSERT(simple_lock_held(&anon->an_lock));
    416 
    417 	KASSERT(pg != NULL);
    418 	KASSERT((pg->flags & PG_RELEASED) != 0);
    419 	KASSERT((pg->flags & PG_BUSY) != 0);
    420 	KASSERT(pg->uobject == NULL);
    421 	KASSERT(pg->uanon == anon);
    422 	KASSERT(pg->loan_count == 0);
    423 	KASSERT(anon->an_ref == 0);
    424 
    425 	uvm_lock_pageq();
    426 	uvm_pagefree(pg);
    427 	uvm_unlock_pageq();
    428 	simple_unlock(&anon->an_lock);
    429 
    430 	KASSERT(anon->an_page == NULL);
    431 
    432 	uvm_anfree(anon);
    433 }
    434