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