Home | History | Annotate | Line # | Download | only in uvm
uvm_pdaemon.c revision 1.103.2.6
      1 /*	$NetBSD: uvm_pdaemon.c,v 1.103.2.6 2014/05/22 11:41:19 yamt Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997 Charles D. Cranor and Washington University.
      5  * Copyright (c) 1991, 1993, The Regents of the University of California.
      6  *
      7  * All rights reserved.
      8  *
      9  * This code is derived from software contributed to Berkeley by
     10  * The Mach Operating System project at Carnegie-Mellon University.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	@(#)vm_pageout.c        8.5 (Berkeley) 2/14/94
     37  * from: Id: uvm_pdaemon.c,v 1.1.2.32 1998/02/06 05:26:30 chs Exp
     38  *
     39  *
     40  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
     41  * All rights reserved.
     42  *
     43  * Permission to use, copy, modify and distribute this software and
     44  * its documentation is hereby granted, provided that both the copyright
     45  * notice and this permission notice appear in all copies of the
     46  * software, derivative works or modified versions, and any portions
     47  * thereof, and that both notices appear in supporting documentation.
     48  *
     49  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     50  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     51  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     52  *
     53  * Carnegie Mellon requests users of this software to return to
     54  *
     55  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     56  *  School of Computer Science
     57  *  Carnegie Mellon University
     58  *  Pittsburgh PA 15213-3890
     59  *
     60  * any improvements or extensions that they make and grant Carnegie the
     61  * rights to redistribute these changes.
     62  */
     63 
     64 /*
     65  * uvm_pdaemon.c: the page daemon
     66  */
     67 
     68 #include <sys/cdefs.h>
     69 __KERNEL_RCSID(0, "$NetBSD: uvm_pdaemon.c,v 1.103.2.6 2014/05/22 11:41:19 yamt Exp $");
     70 
     71 #include "opt_uvmhist.h"
     72 #include "opt_readahead.h"
     73 
     74 #include <sys/param.h>
     75 #include <sys/proc.h>
     76 #include <sys/systm.h>
     77 #include <sys/kernel.h>
     78 #include <sys/pool.h>
     79 #include <sys/buf.h>
     80 #include <sys/module.h>
     81 #include <sys/atomic.h>
     82 
     83 #include <uvm/uvm.h>
     84 #include <uvm/uvm_pdpolicy.h>
     85 
     86 #ifdef UVMHIST
     87 UVMHIST_DEFINE(pdhist);
     88 #endif
     89 
     90 /*
     91  * UVMPD_NUMDIRTYREACTS is how many dirty pages the pagedaemon will reactivate
     92  * in a pass thru the inactive list when swap is full.  the value should be
     93  * "small"... if it's too large we'll cycle the active pages thru the inactive
     94  * queue too quickly to for them to be referenced and avoid being freed.
     95  */
     96 
     97 #define	UVMPD_NUMDIRTYREACTS	16
     98 
     99 #define	UVMPD_NUMTRYLOCKOWNER	16
    100 
    101 /*
    102  * local prototypes
    103  */
    104 
    105 static void	uvmpd_scan(void);
    106 static void	uvmpd_scan_queue(void);
    107 static void	uvmpd_tune(void);
    108 
    109 static unsigned int uvm_pagedaemon_waiters;
    110 
    111 /*
    112  * XXX hack to avoid hangs when large processes fork.
    113  */
    114 u_int uvm_extrapages;
    115 
    116 /*
    117  * uvm_wait: wait (sleep) for the page daemon to free some pages
    118  *
    119  * => should be called with all locks released
    120  * => should _not_ be called by the page daemon (to avoid deadlock)
    121  */
    122 
    123 void
    124 uvm_wait(const char *wmsg)
    125 {
    126 	int timo = 0;
    127 
    128 	mutex_spin_enter(&uvm_fpageqlock);
    129 
    130 	/*
    131 	 * check for page daemon going to sleep (waiting for itself)
    132 	 */
    133 
    134 	if (curlwp == uvm.pagedaemon_lwp && uvmexp.paging == 0) {
    135 		/*
    136 		 * now we have a problem: the pagedaemon wants to go to
    137 		 * sleep until it frees more memory.   but how can it
    138 		 * free more memory if it is asleep?  that is a deadlock.
    139 		 * we have two options:
    140 		 *  [1] panic now
    141 		 *  [2] put a timeout on the sleep, thus causing the
    142 		 *      pagedaemon to only pause (rather than sleep forever)
    143 		 *
    144 		 * note that option [2] will only help us if we get lucky
    145 		 * and some other process on the system breaks the deadlock
    146 		 * by exiting or freeing memory (thus allowing the pagedaemon
    147 		 * to continue).  for now we panic if DEBUG is defined,
    148 		 * otherwise we hope for the best with option [2] (better
    149 		 * yet, this should never happen in the first place!).
    150 		 */
    151 
    152 		printf("pagedaemon: deadlock detected!\n");
    153 		timo = hz >> 3;		/* set timeout */
    154 #if defined(DEBUG)
    155 		/* DEBUG: panic so we can debug it */
    156 		panic("pagedaemon deadlock");
    157 #endif
    158 	}
    159 
    160 	uvm_pagedaemon_waiters++;
    161 	wakeup(&uvm.pagedaemon);		/* wake the daemon! */
    162 	UVM_UNLOCK_AND_WAIT(&uvmexp.free, &uvm_fpageqlock, false, wmsg, timo);
    163 }
    164 
    165 /*
    166  * uvm_kick_pdaemon: perform checks to determine if we need to
    167  * give the pagedaemon a nudge, and do so if necessary.
    168  *
    169  * => called with uvm_fpageqlock held.
    170  */
    171 
    172 void
    173 uvm_kick_pdaemon(void)
    174 {
    175 
    176 	KASSERT(mutex_owned(&uvm_fpageqlock));
    177 
    178 	if (uvmexp.free + uvmexp.paging < uvmexp.freemin ||
    179 	    (uvmexp.free + uvmexp.paging < uvmexp.freetarg &&
    180 	     uvmpdpol_needsscan_p()) ||
    181 	     uvm_km_va_starved_p()) {
    182 		wakeup(&uvm.pagedaemon);
    183 	}
    184 }
    185 
    186 /*
    187  * uvmpd_tune: tune paging parameters
    188  *
    189  * => called when ever memory is added (or removed?) to the system
    190  * => caller must call with page queues locked
    191  */
    192 
    193 static void
    194 uvmpd_tune(void)
    195 {
    196 	int val;
    197 
    198 	UVMHIST_FUNC("uvmpd_tune"); UVMHIST_CALLED(pdhist);
    199 
    200 	/*
    201 	 * try to keep 0.5% of available RAM free, but limit to between
    202 	 * 128k and 1024k per-CPU.  XXX: what are these values good for?
    203 	 */
    204 	val = uvmexp.npages / 200;
    205 	val = MAX(val, (128*1024) >> PAGE_SHIFT);
    206 	val = MIN(val, (1024*1024) >> PAGE_SHIFT);
    207 	val *= ncpu;
    208 
    209 	/* Make sure there's always a user page free. */
    210 	if (val < uvmexp.reserve_kernel + 1)
    211 		val = uvmexp.reserve_kernel + 1;
    212 	uvmexp.freemin = val;
    213 
    214 	/* Calculate free target. */
    215 	val = (uvmexp.freemin * 4) / 3;
    216 	if (val <= uvmexp.freemin)
    217 		val = uvmexp.freemin + 1;
    218 	uvmexp.freetarg = val + atomic_swap_uint(&uvm_extrapages, 0);
    219 
    220 	uvmexp.wiredmax = uvmexp.npages / 3;
    221 	UVMHIST_LOG(pdhist, "<- done, freemin=%d, freetarg=%d, wiredmax=%d",
    222 	      uvmexp.freemin, uvmexp.freetarg, uvmexp.wiredmax, 0);
    223 }
    224 
    225 /*
    226  * uvm_pageout: the main loop for the pagedaemon
    227  */
    228 
    229 void
    230 uvm_pageout(void *arg)
    231 {
    232 	int bufcnt, npages = 0;
    233 	int extrapages = 0;
    234 	struct pool *pp;
    235 
    236 	UVMHIST_FUNC("uvm_pageout"); UVMHIST_CALLED(pdhist);
    237 
    238 	UVMHIST_LOG(pdhist,"<starting uvm pagedaemon>", 0, 0, 0, 0);
    239 
    240 	/*
    241 	 * ensure correct priority and set paging parameters...
    242 	 */
    243 
    244 	uvm.pagedaemon_lwp = curlwp;
    245 	mutex_enter(&uvm_pageqlock);
    246 	npages = uvmexp.npages;
    247 	uvmpd_tune();
    248 	mutex_exit(&uvm_pageqlock);
    249 
    250 	/*
    251 	 * main loop
    252 	 */
    253 
    254 	for (;;) {
    255 		bool needsscan, needsfree, kmem_va_starved;
    256 
    257 		kmem_va_starved = uvm_km_va_starved_p();
    258 
    259 		mutex_spin_enter(&uvm_fpageqlock);
    260 		if ((uvm_pagedaemon_waiters == 0 || uvmexp.paging > 0) &&
    261 		    !kmem_va_starved) {
    262 			UVMHIST_LOG(pdhist,"  <<SLEEPING>>",0,0,0,0);
    263 			UVM_UNLOCK_AND_WAIT(&uvm.pagedaemon,
    264 			    &uvm_fpageqlock, false, "pgdaemon", 0);
    265 			uvmexp.pdwoke++;
    266 			UVMHIST_LOG(pdhist,"  <<WOKE UP>>",0,0,0,0);
    267 		} else {
    268 			mutex_spin_exit(&uvm_fpageqlock);
    269 		}
    270 
    271 		/*
    272 		 * now lock page queues and recompute inactive count
    273 		 */
    274 
    275 		mutex_enter(&uvm_pageqlock);
    276 		if (npages != uvmexp.npages || extrapages != uvm_extrapages) {
    277 			npages = uvmexp.npages;
    278 			extrapages = uvm_extrapages;
    279 			mutex_spin_enter(&uvm_fpageqlock);
    280 			uvmpd_tune();
    281 			mutex_spin_exit(&uvm_fpageqlock);
    282 		}
    283 
    284 		uvmpdpol_tune();
    285 
    286 		/*
    287 		 * Estimate a hint.  Note that bufmem are returned to
    288 		 * system only when entire pool page is empty.
    289 		 */
    290 		mutex_spin_enter(&uvm_fpageqlock);
    291 		bufcnt = uvmexp.freetarg - uvmexp.free;
    292 		if (bufcnt < 0)
    293 			bufcnt = 0;
    294 
    295 		UVMHIST_LOG(pdhist,"  free/ftarg=%d/%d",
    296 		    uvmexp.free, uvmexp.freetarg, 0,0);
    297 
    298 		needsfree = uvmexp.free + uvmexp.paging < uvmexp.freetarg;
    299 		needsscan = needsfree || uvmpdpol_needsscan_p();
    300 
    301 		/*
    302 		 * scan if needed
    303 		 */
    304 		if (needsscan) {
    305 			mutex_spin_exit(&uvm_fpageqlock);
    306 			uvmpd_scan();
    307 			mutex_spin_enter(&uvm_fpageqlock);
    308 		}
    309 
    310 		/*
    311 		 * if there's any free memory to be had,
    312 		 * wake up any waiters.
    313 		 */
    314 		if (uvmexp.free > uvmexp.reserve_kernel ||
    315 		    uvmexp.paging == 0) {
    316 			wakeup(&uvmexp.free);
    317 			uvm_pagedaemon_waiters = 0;
    318 		}
    319 		mutex_spin_exit(&uvm_fpageqlock);
    320 
    321 		/*
    322 		 * scan done.  unlock page queues (the only lock we are holding)
    323 		 */
    324 		mutex_exit(&uvm_pageqlock);
    325 
    326 		/*
    327 		 * if we don't need free memory, we're done.
    328 		 */
    329 
    330 		if (!needsfree && !kmem_va_starved)
    331 			continue;
    332 
    333 		/*
    334 		 * kill unused metadata buffers.
    335 		 */
    336 		mutex_enter(&bufcache_lock);
    337 		buf_drain(bufcnt << PAGE_SHIFT);
    338 		mutex_exit(&bufcache_lock);
    339 
    340 		/*
    341 		 * drain the pools.
    342 		 */
    343 		pool_drain(&pp);
    344 	}
    345 	/*NOTREACHED*/
    346 }
    347 
    348 
    349 /*
    350  * uvm_aiodone_worker: a workqueue callback for the aiodone daemon.
    351  */
    352 
    353 void
    354 uvm_aiodone_worker(struct work *wk, void *dummy)
    355 {
    356 	struct buf *bp = (void *)wk;
    357 
    358 	KASSERT(&bp->b_work == wk);
    359 
    360 	/*
    361 	 * process an i/o that's done.
    362 	 */
    363 
    364 	(*bp->b_iodone)(bp);
    365 }
    366 
    367 void
    368 uvm_pageout_start(int npages)
    369 {
    370 
    371 	mutex_spin_enter(&uvm_fpageqlock);
    372 	uvmexp.paging += npages;
    373 	mutex_spin_exit(&uvm_fpageqlock);
    374 }
    375 
    376 void
    377 uvm_pageout_done(int npages)
    378 {
    379 
    380 	mutex_spin_enter(&uvm_fpageqlock);
    381 	KASSERT(uvmexp.paging >= npages);
    382 	uvmexp.paging -= npages;
    383 
    384 	/*
    385 	 * wake up either of pagedaemon or LWPs waiting for it.
    386 	 */
    387 
    388 	if (uvmexp.free <= uvmexp.reserve_kernel) {
    389 		wakeup(&uvm.pagedaemon);
    390 	} else {
    391 		wakeup(&uvmexp.free);
    392 		uvm_pagedaemon_waiters = 0;
    393 	}
    394 	mutex_spin_exit(&uvm_fpageqlock);
    395 }
    396 
    397 /*
    398  * uvmpd_trylockowner: trylock the page's owner.
    399  *
    400  * => called with pageq locked.
    401  * => resolve orphaned O->A loaned page.
    402  * => return the locked mutex on success.  otherwise, return NULL.
    403  */
    404 
    405 kmutex_t *
    406 uvmpd_trylockowner(struct vm_page *pg)
    407 {
    408 	kmutex_t *lock;
    409 
    410 	KASSERT(mutex_owned(&uvm_pageqlock));
    411 	lock = uvm_page_getlock(pg);
    412 	KASSERT(lock != NULL);
    413 	if (!mutex_tryenter(lock)) {
    414 		return NULL;
    415 	}
    416 	uvm_loan_resolve_orphan(pg, true);
    417 	return lock;
    418 }
    419 
    420 #if defined(VMSWAP)
    421 struct swapcluster {
    422 	int swc_slot;
    423 	int swc_nallocated;
    424 	int swc_nused;
    425 	struct vm_page *swc_pages[howmany(MAXPHYS, MIN_PAGE_SIZE)];
    426 };
    427 
    428 static void
    429 swapcluster_init(struct swapcluster *swc)
    430 {
    431 
    432 	swc->swc_slot = 0;
    433 	swc->swc_nused = 0;
    434 }
    435 
    436 static int
    437 swapcluster_allocslots(struct swapcluster *swc)
    438 {
    439 	int slot;
    440 	int npages;
    441 
    442 	if (swc->swc_slot != 0) {
    443 		return 0;
    444 	}
    445 
    446 	/* Even with strange MAXPHYS, the shift
    447 	   implicitly rounds down to a page. */
    448 	npages = MAXPHYS >> PAGE_SHIFT;
    449 	slot = uvm_swap_alloc(&npages, true);
    450 	if (slot == 0) {
    451 		return ENOMEM;
    452 	}
    453 	swc->swc_slot = slot;
    454 	swc->swc_nallocated = npages;
    455 	swc->swc_nused = 0;
    456 
    457 	return 0;
    458 }
    459 
    460 static int
    461 swapcluster_add(struct swapcluster *swc, struct vm_page *pg)
    462 {
    463 	int slot;
    464 	struct uvm_object *uobj;
    465 
    466 	KASSERT(swc->swc_slot != 0);
    467 	KASSERT(swc->swc_nused < swc->swc_nallocated);
    468 	KASSERT((pg->pqflags & PQ_SWAPBACKED) != 0);
    469 
    470 	slot = swc->swc_slot + swc->swc_nused;
    471 	uobj = pg->uobject;
    472 	if (uobj == NULL) {
    473 		KASSERT(mutex_owned(pg->uanon->an_lock));
    474 		pg->uanon->an_swslot = slot;
    475 	} else {
    476 		int result;
    477 
    478 		KASSERT(mutex_owned(uobj->vmobjlock));
    479 		result = uao_set_swslot(uobj, pg->offset >> PAGE_SHIFT, slot);
    480 		if (result == -1) {
    481 			return ENOMEM;
    482 		}
    483 	}
    484 	swc->swc_pages[swc->swc_nused] = pg;
    485 	swc->swc_nused++;
    486 
    487 	return 0;
    488 }
    489 
    490 static void
    491 swapcluster_flush(struct swapcluster *swc, bool now)
    492 {
    493 	int slot;
    494 	int nused;
    495 	int nallocated;
    496 	int error __diagused;
    497 
    498 	if (swc->swc_slot == 0) {
    499 		return;
    500 	}
    501 	KASSERT(swc->swc_nused <= swc->swc_nallocated);
    502 
    503 	slot = swc->swc_slot;
    504 	nused = swc->swc_nused;
    505 	nallocated = swc->swc_nallocated;
    506 
    507 	/*
    508 	 * if this is the final pageout we could have a few
    509 	 * unused swap blocks.  if so, free them now.
    510 	 */
    511 
    512 	if (nused < nallocated) {
    513 		if (!now) {
    514 			return;
    515 		}
    516 		uvm_swap_free(slot + nused, nallocated - nused);
    517 	}
    518 
    519 	/*
    520 	 * now start the pageout.
    521 	 */
    522 
    523 	if (nused > 0) {
    524 		uvmexp.pdpageouts++;
    525 		uvm_pageout_start(nused);
    526 		error = uvm_swap_put(slot, swc->swc_pages, nused, 0);
    527 		KASSERT(error == 0 || error == ENOMEM);
    528 	}
    529 
    530 	/*
    531 	 * zero swslot to indicate that we are
    532 	 * no longer building a swap-backed cluster.
    533 	 */
    534 
    535 	swc->swc_slot = 0;
    536 	swc->swc_nused = 0;
    537 }
    538 
    539 static int
    540 swapcluster_nused(struct swapcluster *swc)
    541 {
    542 
    543 	return swc->swc_nused;
    544 }
    545 
    546 /*
    547  * uvmpd_dropswap: free any swap allocated to this page.
    548  *
    549  * => called with owner locked.
    550  * => return true if a page had an associated slot.
    551  */
    552 
    553 static bool
    554 uvmpd_dropswap(struct vm_page *pg)
    555 {
    556 	bool result = false;
    557 	struct vm_anon *anon = pg->uanon;
    558 
    559 	if ((pg->pqflags & PQ_ANON) && anon->an_swslot) {
    560 		uvm_swap_free(anon->an_swslot, 1);
    561 		anon->an_swslot = 0;
    562 		uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_DIRTY);
    563 		result = true;
    564 	} else if (pg->pqflags & PQ_AOBJ) {
    565 		int slot = uao_set_swslot(pg->uobject,
    566 		    pg->offset >> PAGE_SHIFT, 0);
    567 		if (slot) {
    568 			uvm_swap_free(slot, 1);
    569 			uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_DIRTY);
    570 			result = true;
    571 		}
    572 	}
    573 
    574 	return result;
    575 }
    576 
    577 /*
    578  * uvmpd_trydropswap: try to free any swap allocated to this page.
    579  *
    580  * => return true if a slot is successfully freed.
    581  */
    582 
    583 bool
    584 uvmpd_trydropswap(struct vm_page *pg)
    585 {
    586 	kmutex_t *slock;
    587 	bool result;
    588 
    589 	if ((pg->flags & PG_BUSY) != 0) {
    590 		return false;
    591 	}
    592 
    593 	/*
    594 	 * lock the page's owner.
    595 	 */
    596 
    597 	slock = uvmpd_trylockowner(pg);
    598 	if (slock == NULL) {
    599 		return false;
    600 	}
    601 
    602 	/*
    603 	 * skip this page if it's busy.
    604 	 */
    605 
    606 	if ((pg->flags & PG_BUSY) != 0) {
    607 		mutex_exit(slock);
    608 		return false;
    609 	}
    610 
    611 	result = uvmpd_dropswap(pg);
    612 
    613 	mutex_exit(slock);
    614 
    615 	return result;
    616 }
    617 
    618 #endif /* defined(VMSWAP) */
    619 
    620 /*
    621  * uvmpd_scan_queue: scan an replace candidate list for pages
    622  * to clean or free.
    623  *
    624  * => called with page queues locked
    625  * => we work on meeting our free target by converting inactive pages
    626  *    into free pages.
    627  * => we handle the building of swap-backed clusters
    628  */
    629 
    630 static void
    631 uvmpd_scan_queue(void)
    632 {
    633 	struct vm_page *p;
    634 	struct uvm_object *uobj;
    635 	struct vm_anon *anon;
    636 #if defined(VMSWAP)
    637 	struct swapcluster swc;
    638 #endif /* defined(VMSWAP) */
    639 	int dirtyreacts;
    640 	int lockownerfail;
    641 	kmutex_t *slock;
    642 	UVMHIST_FUNC("uvmpd_scan_queue"); UVMHIST_CALLED(pdhist);
    643 
    644 #if defined(VMSWAP)
    645 	swapcluster_init(&swc);
    646 #endif /* defined(VMSWAP) */
    647 
    648 	dirtyreacts = 0;
    649 	lockownerfail = 0;
    650 	uvmpdpol_scaninit();
    651 
    652 	while (/* CONSTCOND */ 1) {
    653 
    654 		/*
    655 		 * see if we've met the free target.
    656 		 */
    657 
    658 		if (uvmexp.free + uvmexp.paging
    659 #if defined(VMSWAP)
    660 		    + swapcluster_nused(&swc)
    661 #endif /* defined(VMSWAP) */
    662 		    >= uvmexp.freetarg << 2 ||
    663 		    dirtyreacts == UVMPD_NUMDIRTYREACTS) {
    664 			UVMHIST_LOG(pdhist,"  met free target: "
    665 				    "exit loop", 0, 0, 0, 0);
    666 			break;
    667 		}
    668 
    669 		p = uvmpdpol_selectvictim();
    670 		if (p == NULL) {
    671 			break;
    672 		}
    673 		KASSERT(uvmpdpol_pageisqueued_p(p));
    674 		KASSERT(p->wire_count == 0);
    675 
    676 		/*
    677 		 * we are below target and have a new page to consider.
    678 		 */
    679 
    680 		anon = p->uanon;
    681 		uobj = p->uobject;
    682 
    683 		/*
    684 		 * first we attempt to lock the object that this page
    685 		 * belongs to.  if our attempt fails we skip on to
    686 		 * the next page (no harm done).  it is important to
    687 		 * "try" locking the object as we are locking in the
    688 		 * wrong order (pageq -> object) and we don't want to
    689 		 * deadlock.
    690 		 *
    691 		 * the only time we expect to see an ownerless page
    692 		 * (i.e. a page with no uobject and !PQ_ANON) is if an
    693 		 * anon has loaned a page from a uvm_object and the
    694 		 * uvm_object has dropped the ownership.  in that
    695 		 * case, the anon can "take over" the loaned page
    696 		 * and make it its own.
    697 		 */
    698 
    699 		slock = uvmpd_trylockowner(p);
    700 		if (slock == NULL) {
    701 			/*
    702 			 * yield cpu to make a chance for an LWP holding
    703 			 * the lock run.  otherwise we can busy-loop too long
    704 			 * if the page queue is filled with a lot of pages
    705 			 * from few objects.
    706 			 */
    707 			lockownerfail++;
    708 			if (lockownerfail > UVMPD_NUMTRYLOCKOWNER) {
    709 				mutex_obj_pause(uvm_page_getlock(p),
    710 				    &uvm_pageqlock);
    711 				lockownerfail = 0;
    712 			}
    713 			continue;
    714 		}
    715 		if (p->flags & PG_BUSY) {
    716 			mutex_exit(slock);
    717 			uvmexp.pdbusy++;
    718 			continue;
    719 		}
    720 
    721 		/* does the page belong to an object? */
    722 		if (uobj != NULL) {
    723 			uvmexp.pdobscan++;
    724 		} else {
    725 #if defined(VMSWAP)
    726 			KASSERT(anon != NULL);
    727 			uvmexp.pdanscan++;
    728 #else /* defined(VMSWAP) */
    729 			panic("%s: anon", __func__);
    730 #endif /* defined(VMSWAP) */
    731 		}
    732 
    733 
    734 		/*
    735 		 * we now have the object and the page queues locked.
    736 		 * if the page is not swap-backed, call the object's
    737 		 * pager to flush and free the page.
    738 		 */
    739 
    740 #if defined(READAHEAD_STATS)
    741 		if ((p->pqflags & PQ_READAHEAD) != 0) {
    742 			p->pqflags &= ~PQ_READAHEAD;
    743 			uvm_ra_miss.ev_count++;
    744 		}
    745 #endif /* defined(READAHEAD_STATS) */
    746 
    747 		if ((p->pqflags & PQ_SWAPBACKED) == 0) {
    748 			KASSERT(uobj != NULL);
    749 			mutex_exit(&uvm_pageqlock);
    750 			(void) (uobj->pgops->pgo_put)(uobj, p->offset,
    751 			    p->offset + PAGE_SIZE, PGO_CLEANIT|PGO_FREE);
    752 			mutex_enter(&uvm_pageqlock);
    753 			continue;
    754 		}
    755 
    756 		/*
    757 		 * the page is swap-backed.  remove all the permissions
    758 		 * from the page so we can sync the modified info
    759 		 * without any race conditions.  if the page is clean
    760 		 * we can free it now and continue.
    761 		 */
    762 
    763 		pmap_page_protect(p, VM_PROT_NONE);
    764 		if (uvm_pagegetdirty(p) == UVM_PAGE_STATUS_UNKNOWN) {
    765 			if (pmap_clear_modify(p)) {
    766 				uvm_pagemarkdirty(p, UVM_PAGE_STATUS_DIRTY);
    767 			} else {
    768 				uvm_pagemarkdirty(p, UVM_PAGE_STATUS_CLEAN);
    769 			}
    770 		}
    771 		if (uvm_pagegetdirty(p) != UVM_PAGE_STATUS_DIRTY) {
    772 			int slot;
    773 			int pageidx;
    774 
    775 			pageidx = p->offset >> PAGE_SHIFT;
    776 			uvm_pagefree(p);
    777 			uvmexp.pdfreed++;
    778 
    779 			/*
    780 			 * for anons, we need to remove the page
    781 			 * from the anon ourselves.  for aobjs,
    782 			 * pagefree did that for us.
    783 			 */
    784 
    785 			if (anon) {
    786 				KASSERT(anon->an_swslot != 0);
    787 				anon->an_page = NULL;
    788 				slot = anon->an_swslot;
    789 			} else {
    790 				slot = uao_find_swslot(uobj, pageidx);
    791 			}
    792 			mutex_exit(slock);
    793 
    794 			if (slot > 0) {
    795 				/* this page is now only in swap. */
    796 				mutex_enter(&uvm_swap_data_lock);
    797 				KASSERT(uvmexp.swpgonly < uvmexp.swpginuse);
    798 				uvmexp.swpgonly++;
    799 				mutex_exit(&uvm_swap_data_lock);
    800 			}
    801 			continue;
    802 		}
    803 
    804 #if defined(VMSWAP)
    805 		/*
    806 		 * this page is dirty, skip it if we'll have met our
    807 		 * free target when all the current pageouts complete.
    808 		 */
    809 
    810 		if (uvmexp.free + uvmexp.paging > uvmexp.freetarg << 2) {
    811 			mutex_exit(slock);
    812 			continue;
    813 		}
    814 
    815 		/*
    816 		 * free any swap space allocated to the page since
    817 		 * we'll have to write it again with its new data.
    818 		 */
    819 
    820 		uvmpd_dropswap(p);
    821 
    822 		/*
    823 		 * start new swap pageout cluster (if necessary).
    824 		 *
    825 		 * if swap is full reactivate this page so that
    826 		 * we eventually cycle all pages through the
    827 		 * inactive queue.
    828 		 */
    829 
    830 		if (swapcluster_allocslots(&swc)) {
    831 			dirtyreacts++;
    832 			uvm_pageactivate(p);
    833 			mutex_exit(slock);
    834 			continue;
    835 		}
    836 
    837 		/*
    838 		 * at this point, we're definitely going reuse this
    839 		 * page.  mark the page busy and delayed-free.
    840 		 * we should remove the page from the page queues
    841 		 * so we don't ever look at it again.
    842 		 * adjust counters and such.
    843 		 */
    844 
    845 		p->flags |= PG_BUSY;
    846 		UVM_PAGE_OWN(p, "scan_queue");
    847 
    848 		p->flags |= PG_PAGEOUT;
    849 		uvm_pagedequeue(p);
    850 
    851 		uvmexp.pgswapout++;
    852 		mutex_exit(&uvm_pageqlock);
    853 
    854 		/*
    855 		 * add the new page to the cluster.
    856 		 */
    857 
    858 		if (swapcluster_add(&swc, p)) {
    859 			p->flags &= ~(PG_BUSY|PG_PAGEOUT);
    860 			UVM_PAGE_OWN(p, NULL);
    861 			mutex_enter(&uvm_pageqlock);
    862 			dirtyreacts++;
    863 			uvm_pageactivate(p);
    864 			mutex_exit(slock);
    865 			continue;
    866 		}
    867 		mutex_exit(slock);
    868 
    869 		swapcluster_flush(&swc, false);
    870 		mutex_enter(&uvm_pageqlock);
    871 
    872 		/*
    873 		 * the pageout is in progress.  bump counters and set up
    874 		 * for the next loop.
    875 		 */
    876 
    877 		uvmexp.pdpending++;
    878 
    879 #else /* defined(VMSWAP) */
    880 		uvm_pageactivate(p);
    881 		mutex_exit(slock);
    882 #endif /* defined(VMSWAP) */
    883 	}
    884 
    885 #if defined(VMSWAP)
    886 	mutex_exit(&uvm_pageqlock);
    887 	swapcluster_flush(&swc, true);
    888 	mutex_enter(&uvm_pageqlock);
    889 #endif /* defined(VMSWAP) */
    890 }
    891 
    892 /*
    893  * uvmpd_scan: scan the page queues and attempt to meet our targets.
    894  *
    895  * => called with pageq's locked
    896  */
    897 
    898 static void
    899 uvmpd_scan(void)
    900 {
    901 	int swap_shortage, pages_freed;
    902 	UVMHIST_FUNC("uvmpd_scan"); UVMHIST_CALLED(pdhist);
    903 
    904 	uvmexp.pdrevs++;
    905 
    906 	/*
    907 	 * work on meeting our targets.   first we work on our free target
    908 	 * by converting inactive pages into free pages.  then we work on
    909 	 * meeting our inactive target by converting active pages to
    910 	 * inactive ones.
    911 	 */
    912 
    913 	UVMHIST_LOG(pdhist, "  starting 'free' loop",0,0,0,0);
    914 
    915 	pages_freed = uvmexp.pdfreed;
    916 	uvmpd_scan_queue();
    917 	pages_freed = uvmexp.pdfreed - pages_freed;
    918 
    919 	/*
    920 	 * detect if we're not going to be able to page anything out
    921 	 * until we free some swap resources from active pages.
    922 	 */
    923 
    924 	swap_shortage = 0;
    925 	if (uvmexp.free < uvmexp.freetarg &&
    926 	    uvmexp.swpginuse >= uvmexp.swpgavail &&
    927 	    !uvm_swapisfull() &&
    928 	    pages_freed == 0) {
    929 		swap_shortage = uvmexp.freetarg - uvmexp.free;
    930 	}
    931 
    932 	uvmpdpol_balancequeue(swap_shortage);
    933 
    934 	/*
    935 	 * if still below the minimum target, try unloading kernel
    936 	 * modules.
    937 	 */
    938 
    939 	if (uvmexp.free < uvmexp.freemin) {
    940 		module_thread_kick();
    941 	}
    942 }
    943 
    944 /*
    945  * uvm_reclaimable: decide whether to wait for pagedaemon.
    946  *
    947  * => return true if it seems to be worth to do uvm_wait.
    948  *
    949  * XXX should be tunable.
    950  * XXX should consider pools, etc?
    951  */
    952 
    953 bool
    954 uvm_reclaimable(void)
    955 {
    956 	int filepages;
    957 	int active, inactive;
    958 
    959 	/*
    960 	 * if swap is not full, no problem.
    961 	 */
    962 
    963 	if (!uvm_swapisfull()) {
    964 		return true;
    965 	}
    966 
    967 	/*
    968 	 * file-backed pages can be reclaimed even when swap is full.
    969 	 * if we have more than 1/16 of pageable memory or 5MB, try to reclaim.
    970 	 *
    971 	 * XXX assume the worst case, ie. all wired pages are file-backed.
    972 	 *
    973 	 * XXX should consider about other reclaimable memory.
    974 	 * XXX ie. pools, traditional buffer cache.
    975 	 */
    976 
    977 	filepages = uvmexp.filepages + uvmexp.execpages - uvmexp.wired;
    978 	uvm_estimatepageable(&active, &inactive);
    979 	if (filepages >= MIN((active + inactive) >> 4,
    980 	    5 * 1024 * 1024 >> PAGE_SHIFT)) {
    981 		return true;
    982 	}
    983 
    984 	/*
    985 	 * kill the process, fail allocation, etc..
    986 	 */
    987 
    988 	return false;
    989 }
    990 
    991 void
    992 uvm_estimatepageable(int *active, int *inactive)
    993 {
    994 
    995 	uvmpdpol_estimatepageable(active, inactive);
    996 }
    997 
    998