Home | History | Annotate | Line # | Download | only in uvm
uvm_anon.c revision 1.68
      1 /*	$NetBSD: uvm_anon.c,v 1.68 2019/12/02 20:02:02 chs 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.68 2019/12/02 20:02:02 chs 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 #include <sys/atomic.h>
     42 
     43 #include <uvm/uvm.h>
     44 #include <uvm/uvm_swap.h>
     45 #include <uvm/uvm_pdpolicy.h>
     46 
     47 static struct pool_cache	uvm_anon_cache;
     48 
     49 static int			uvm_anon_ctor(void *, void *, int);
     50 
     51 void
     52 uvm_anon_init(void)
     53 {
     54 
     55 	pool_cache_bootstrap(&uvm_anon_cache, sizeof(struct vm_anon), 0, 0,
     56 	    PR_LARGECACHE, "anonpl", NULL, IPL_NONE, uvm_anon_ctor,
     57 	    NULL, NULL);
     58 }
     59 
     60 static int
     61 uvm_anon_ctor(void *arg, void *object, int flags)
     62 {
     63 	struct vm_anon *anon = object;
     64 
     65 	anon->an_ref = 0;
     66 	anon->an_lock = NULL;
     67 	anon->an_page = NULL;
     68 #if defined(VMSWAP)
     69 	anon->an_swslot = 0;
     70 #endif
     71 	return 0;
     72 }
     73 
     74 /*
     75  * uvm_analloc: allocate a new anon.
     76  *
     77  * => anon will have no lock associated.
     78  */
     79 struct vm_anon *
     80 uvm_analloc(void)
     81 {
     82 	struct vm_anon *anon;
     83 
     84 	anon = pool_cache_get(&uvm_anon_cache, PR_NOWAIT);
     85 	if (anon) {
     86 		KASSERT(anon->an_ref == 0);
     87 		KASSERT(anon->an_lock == NULL);
     88 		KASSERT(anon->an_page == NULL);
     89 #if defined(VMSWAP)
     90 		KASSERT(anon->an_swslot == 0);
     91 #endif
     92 		anon->an_ref = 1;
     93 	}
     94 	return anon;
     95 }
     96 
     97 /*
     98  * uvm_anon_dispose: break loans and remove pmap mapping
     99  *
    100  * => anon must be removed from the amap (if anon was in an amap).
    101  * => amap must be locked; we may drop and re-acquire the lock here.
    102  */
    103 static bool
    104 uvm_anon_dispose(struct vm_anon *anon)
    105 {
    106 	struct vm_page *pg = anon->an_page;
    107 
    108 	UVMHIST_FUNC("uvm_anon_dispose"); UVMHIST_CALLED(maphist);
    109 	UVMHIST_LOG(maphist,"(anon=0x%#jx)", (uintptr_t)anon, 0,0,0);
    110 
    111 	KASSERT(mutex_owned(anon->an_lock));
    112 
    113 	/*
    114 	 * Dispose the page, if it is resident.
    115 	 */
    116 
    117 	if (pg) {
    118 		KASSERT(anon->an_lock != NULL);
    119 
    120 		/*
    121 		 * If there is a resident page and it is loaned, then anon
    122 		 * may not own it.  Call out to uvm_anon_lockloanpg() to
    123 		 * identify and lock the real owner of the page.
    124 		 */
    125 
    126 		if (pg->loan_count) {
    127 			pg = uvm_anon_lockloanpg(anon);
    128 		}
    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 		}
    164 	}
    165 
    166 #if defined(VMSWAP)
    167 	if (pg == NULL && anon->an_swslot > 0) {
    168 		/* This page is no longer only in swap. */
    169 		KASSERT(uvmexp.swpgonly > 0);
    170 		atomic_dec_uint(&uvmexp.swpgonly);
    171 	}
    172 #endif
    173 
    174 	UVMHIST_LOG(maphist,"<- done!",0,0,0,0);
    175 	return true;
    176 }
    177 
    178 /*
    179  * uvm_anon_free: free a single anon.
    180  *
    181  * => anon must be already disposed.
    182  */
    183 void
    184 uvm_anon_free(struct vm_anon *anon)
    185 {
    186 
    187 #if defined(VMSWAP)
    188 	/* Free any dangling swap slot. */
    189 	uvm_anon_dropswap(anon);
    190 #endif
    191 	KASSERT(anon->an_ref == 0);
    192 	KASSERT(anon->an_lock == NULL);
    193 	KASSERT(anon->an_page == NULL);
    194 #if defined(VMSWAP)
    195 	KASSERT(anon->an_swslot == 0);
    196 #endif
    197 	pool_cache_put(&uvm_anon_cache, anon);
    198 }
    199 
    200 /*
    201  * uvm_anon_freelst: free a linked list of anon structures.
    202  *
    203  * => amap must be locked, we will unlock it.
    204  */
    205 void
    206 uvm_anon_freelst(struct vm_amap *amap, struct vm_anon *anonlst)
    207 {
    208 	struct vm_anon *anon;
    209 	struct vm_anon **anonp = &anonlst;
    210 	struct vm_page *pg;
    211 
    212 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
    213 
    214 	KASSERT(mutex_owned(amap->am_lock));
    215 
    216 	if (anonlst == NULL) {
    217 		amap_unlock(amap);
    218 		return;
    219 	}
    220 
    221 	/* Break loans and hardware mappings.  Defer release of busy pages. */
    222 	while ((anon = *anonp) != NULL) {
    223 		if (!uvm_anon_dispose(anon)) {
    224 			/* Do not free this anon. */
    225 			*anonp = anon->an_link;
    226 			/* Note: clears an_ref as well. */
    227 			anon->an_link = NULL;
    228 		} else {
    229 			anonp = &anon->an_link;
    230 		}
    231 	}
    232 
    233 	/* Free pages and leave a page replacement hint. */
    234 	mutex_enter(&uvm_pageqlock);
    235 	for (anon = anonlst; anon != NULL; anon = anon->an_link) {
    236 		UVMHIST_LOG(maphist, "anon 0x%#jx, page 0x%#jx: "
    237 		    "releasing now!", (uintptr_t)anon,
    238 		    (uintptr_t)anon->an_page, 0, 0);
    239 		if ((pg = anon->an_page) != NULL) {
    240 			uvm_pagefree(pg);
    241 		}
    242 		uvmpdpol_anfree(anon);
    243 	}
    244 	mutex_exit(&uvm_pageqlock);
    245 	amap_unlock(amap);
    246 
    247 	/* Free swap space, pages and vm_anon. */
    248 	while (anonlst) {
    249 		anon = anonlst->an_link;
    250 		/* Note: clears an_ref as well. */
    251 		anonlst->an_link = NULL;
    252 		anonlst->an_lock = NULL;
    253 		uvm_anon_free(anonlst);
    254 		anonlst = anon;
    255 	}
    256 }
    257 
    258 /*
    259  * uvm_anon_lockloanpg: given a locked anon, lock its resident page owner.
    260  *
    261  * => anon is locked by caller
    262  * => on return: anon is locked
    263  *		 if there is a resident page:
    264  *			if it has a uobject, it is locked by us
    265  *			if it is ownerless, we take over as owner
    266  *		 we return the resident page (it can change during
    267  *		 this function)
    268  * => note that the only time an anon has an ownerless resident page
    269  *	is if the page was loaned from a uvm_object and the uvm_object
    270  *	disowned it
    271  * => this only needs to be called when you want to do an operation
    272  *	on an anon's resident page and that page has a non-zero loan
    273  *	count.
    274  */
    275 struct vm_page *
    276 uvm_anon_lockloanpg(struct vm_anon *anon)
    277 {
    278 	struct vm_page *pg;
    279 	bool locked = false;
    280 
    281 	KASSERT(mutex_owned(anon->an_lock));
    282 
    283 	/*
    284 	 * loop while we have a resident page that has a non-zero loan count.
    285 	 * if we successfully get our lock, we will "break" the loop.
    286 	 * note that the test for pg->loan_count is not protected -- this
    287 	 * may produce false positive results.   note that a false positive
    288 	 * result may cause us to do more work than we need to, but it will
    289 	 * not produce an incorrect result.
    290 	 */
    291 
    292 	while (((pg = anon->an_page) != NULL) && pg->loan_count != 0) {
    293 
    294 		/*
    295 		 * quickly check to see if the page has an object before
    296 		 * bothering to lock the page queues.   this may also produce
    297 		 * a false positive result, but that's ok because we do a real
    298 		 * check after that.
    299 		 */
    300 
    301 		if (pg->uobject) {
    302 			mutex_enter(&uvm_pageqlock);
    303 			if (pg->uobject) {
    304 				locked =
    305 				    mutex_tryenter(pg->uobject->vmobjlock);
    306 			} else {
    307 				/* object disowned before we got PQ lock */
    308 				locked = true;
    309 			}
    310 			mutex_exit(&uvm_pageqlock);
    311 
    312 			/*
    313 			 * if we didn't get a lock (try lock failed), then we
    314 			 * toggle our anon lock and try again
    315 			 */
    316 
    317 			if (!locked) {
    318 				/*
    319 				 * someone locking the object has a chance to
    320 				 * lock us right now
    321 				 *
    322 				 * XXX Better than yielding but inadequate.
    323 				 */
    324 				kpause("livelock", false, 1, anon->an_lock);
    325 				continue;
    326 			}
    327 		}
    328 
    329 		/*
    330 		 * If page is un-owned i.e. the object dropped its ownership,
    331 		 * then we have to take the ownership.
    332 		 */
    333 
    334 		if (pg->uobject == NULL && (pg->pqflags & PQ_ANON) == 0) {
    335 			mutex_enter(&uvm_pageqlock);
    336 			pg->pqflags |= PQ_ANON;
    337 			pg->loan_count--;
    338 			mutex_exit(&uvm_pageqlock);
    339 		}
    340 		break;
    341 	}
    342 	return pg;
    343 }
    344 
    345 #if defined(VMSWAP)
    346 
    347 /*
    348  * uvm_anon_pagein: fetch an anon's page.
    349  *
    350  * => anon must be locked, and is unlocked upon return.
    351  * => returns true if pagein was aborted due to lack of memory.
    352  */
    353 
    354 bool
    355 uvm_anon_pagein(struct vm_amap *amap, struct vm_anon *anon)
    356 {
    357 	struct vm_page *pg;
    358 	struct uvm_object *uobj;
    359 
    360 	KASSERT(mutex_owned(anon->an_lock));
    361 	KASSERT(anon->an_lock == amap->am_lock);
    362 
    363 	/*
    364 	 * Get the page of the anon.
    365 	 */
    366 
    367 	switch (uvmfault_anonget(NULL, amap, anon)) {
    368 	case 0:
    369 		/* Success - we have the page. */
    370 		KASSERT(mutex_owned(anon->an_lock));
    371 		break;
    372 	case EIO:
    373 	case ERESTART:
    374 		/*
    375 		 * Nothing more to do on errors.  ERESTART means that the
    376 		 * anon was freed.
    377 		 */
    378 		return false;
    379 	default:
    380 		return true;
    381 	}
    382 
    383 	/*
    384 	 * Mark the page as dirty, clear its swslot and un-busy it.
    385 	 */
    386 
    387 	pg = anon->an_page;
    388 	uobj = pg->uobject;
    389 	if (anon->an_swslot > 0) {
    390 		uvm_swap_free(anon->an_swslot, 1);
    391 	}
    392 	anon->an_swslot = 0;
    393 	pg->flags &= ~PG_CLEAN;
    394 
    395 	/*
    396 	 * Deactivate the page (to put it on a page queue).
    397 	 */
    398 
    399 	mutex_enter(&uvm_pageqlock);
    400 	if (pg->wire_count == 0) {
    401 		uvm_pagedeactivate(pg);
    402 	}
    403 	mutex_exit(&uvm_pageqlock);
    404 
    405 	if (pg->flags & PG_WANTED) {
    406 		pg->flags &= ~PG_WANTED;
    407 		wakeup(pg);
    408 	}
    409 
    410 	mutex_exit(anon->an_lock);
    411 	if (uobj) {
    412 		mutex_exit(uobj->vmobjlock);
    413 	}
    414 	return false;
    415 }
    416 
    417 /*
    418  * uvm_anon_dropswap: release any swap resources from this anon.
    419  *
    420  * => anon must be locked or have a reference count of 0.
    421  */
    422 void
    423 uvm_anon_dropswap(struct vm_anon *anon)
    424 {
    425 	UVMHIST_FUNC("uvm_anon_dropswap"); UVMHIST_CALLED(maphist);
    426 
    427 	if (anon->an_swslot == 0)
    428 		return;
    429 
    430 	UVMHIST_LOG(maphist,"freeing swap for anon %#jx, paged to swslot 0x%jx",
    431 		    (uintptr_t)anon, anon->an_swslot, 0, 0);
    432 	uvm_swap_free(anon->an_swslot, 1);
    433 	anon->an_swslot = 0;
    434 }
    435 
    436 #endif
    437 
    438 /*
    439  * uvm_anon_release: release an anon and its page.
    440  *
    441  * => anon should not have any references.
    442  * => anon must be locked.
    443  */
    444 
    445 void
    446 uvm_anon_release(struct vm_anon *anon)
    447 {
    448 	struct vm_page *pg = anon->an_page;
    449 	bool success __diagused;
    450 
    451 	KASSERT(mutex_owned(anon->an_lock));
    452 	KASSERT(pg != NULL);
    453 	KASSERT((pg->flags & PG_RELEASED) != 0);
    454 	KASSERT((pg->flags & PG_BUSY) != 0);
    455 	KASSERT(pg->uobject == NULL);
    456 	KASSERT(pg->uanon == anon);
    457 	KASSERT(pg->loan_count == 0);
    458 	KASSERT(anon->an_ref == 0);
    459 
    460 	mutex_enter(&uvm_pageqlock);
    461 	uvm_pagefree(pg);
    462 	mutex_exit(&uvm_pageqlock);
    463 	KASSERT(anon->an_page == NULL);
    464 	/* dispose should succeed as no one can reach this anon anymore. */
    465 	success = uvm_anon_dispose(anon);
    466 	KASSERT(success);
    467 	mutex_exit(anon->an_lock);
    468 	/* Note: extra reference is held for PG_RELEASED case. */
    469 	mutex_obj_free(anon->an_lock);
    470 	anon->an_lock = NULL;
    471 	uvm_anon_free(anon);
    472 }
    473