Home | History | Annotate | Line # | Download | only in uvm
uvm_pdaemon.c revision 1.84.4.7
      1 /*	$NetBSD: uvm_pdaemon.c,v 1.84.4.7 2007/08/24 23:28:49 ad 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.7 2007/08/24 23:28:49 ad 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 		mutex_enter(&bufcache_lock);
    316 		buf_drain(bufcnt << PAGE_SHIFT);
    317 		mutex_exit(&bufcache_lock);
    318 
    319 		/*
    320 		 * drain pool resources now that we're not holding any locks
    321 		 */
    322 
    323 		pool_drain(0);
    324 
    325 		/*
    326 		 * free any cached u-areas we don't need
    327 		 */
    328 		uvm_uarea_drain(true);
    329 
    330 	}
    331 	/*NOTREACHED*/
    332 }
    333 
    334 
    335 /*
    336  * uvm_aiodone_worker: a workqueue callback for the aiodone daemon.
    337  */
    338 
    339 void
    340 uvm_aiodone_worker(struct work *wk, void *dummy)
    341 {
    342 	struct buf *bp = (void *)wk;
    343 
    344 	KASSERT(&bp->b_work == wk);
    345 
    346 	/*
    347 	 * process an i/o that's done.
    348 	 */
    349 
    350 	(*bp->b_iodone)(bp);
    351 }
    352 
    353 void
    354 uvm_pageout_start(int npages)
    355 {
    356 
    357 	mutex_spin_enter(&uvm_fpageqlock);
    358 	uvmexp.paging += npages;
    359 	mutex_spin_exit(&uvm_fpageqlock);
    360 }
    361 
    362 void
    363 uvm_pageout_done(int npages)
    364 {
    365 
    366 	mutex_spin_enter(&uvm_fpageqlock);
    367 	KASSERT(uvmexp.paging >= npages);
    368 	uvmexp.paging -= npages;
    369 
    370 	/*
    371 	 * wake up either of pagedaemon or LWPs waiting for it.
    372 	 */
    373 
    374 	if (uvmexp.free <= uvmexp.reserve_kernel) {
    375 		wakeup(&uvm.pagedaemon);
    376 	} else {
    377 		wakeup(&uvmexp.free);
    378 		uvm_pagedaemon_waiters = 0;
    379 	}
    380 	mutex_spin_exit(&uvm_fpageqlock);
    381 }
    382 
    383 /*
    384  * uvmpd_trylockowner: trylock the page's owner.
    385  *
    386  * => called with pageq locked.
    387  * => resolve orphaned O->A loaned page.
    388  * => return the locked mutex on success.  otherwise, return NULL.
    389  */
    390 
    391 kmutex_t *
    392 uvmpd_trylockowner(struct vm_page *pg)
    393 {
    394 	struct uvm_object *uobj = pg->uobject;
    395 	kmutex_t *slock;
    396 
    397 	KASSERT(mutex_owned(&uvm_pageqlock));
    398 
    399 	if (uobj != NULL) {
    400 		slock = &uobj->vmobjlock;
    401 	} else {
    402 		struct vm_anon *anon = pg->uanon;
    403 
    404 		KASSERT(anon != NULL);
    405 		slock = &anon->an_lock;
    406 	}
    407 
    408 	if (!mutex_tryenter(slock)) {
    409 		return NULL;
    410 	}
    411 
    412 	if (uobj == NULL) {
    413 
    414 		/*
    415 		 * set PQ_ANON if it isn't set already.
    416 		 */
    417 
    418 		if ((pg->pqflags & PQ_ANON) == 0) {
    419 			KASSERT(pg->loan_count > 0);
    420 			pg->loan_count--;
    421 			pg->pqflags |= PQ_ANON;
    422 			/* anon now owns it */
    423 		}
    424 	}
    425 
    426 	return slock;
    427 }
    428 
    429 #if defined(VMSWAP)
    430 struct swapcluster {
    431 	int swc_slot;
    432 	int swc_nallocated;
    433 	int swc_nused;
    434 	struct vm_page *swc_pages[howmany(MAXPHYS, MIN_PAGE_SIZE)];
    435 };
    436 
    437 static void
    438 swapcluster_init(struct swapcluster *swc)
    439 {
    440 
    441 	swc->swc_slot = 0;
    442 }
    443 
    444 static int
    445 swapcluster_allocslots(struct swapcluster *swc)
    446 {
    447 	int slot;
    448 	int npages;
    449 
    450 	if (swc->swc_slot != 0) {
    451 		return 0;
    452 	}
    453 
    454 	/* Even with strange MAXPHYS, the shift
    455 	   implicitly rounds down to a page. */
    456 	npages = MAXPHYS >> PAGE_SHIFT;
    457 	slot = uvm_swap_alloc(&npages, true);
    458 	if (slot == 0) {
    459 		return ENOMEM;
    460 	}
    461 	swc->swc_slot = slot;
    462 	swc->swc_nallocated = npages;
    463 	swc->swc_nused = 0;
    464 
    465 	return 0;
    466 }
    467 
    468 static int
    469 swapcluster_add(struct swapcluster *swc, struct vm_page *pg)
    470 {
    471 	int slot;
    472 	struct uvm_object *uobj;
    473 
    474 	KASSERT(swc->swc_slot != 0);
    475 	KASSERT(swc->swc_nused < swc->swc_nallocated);
    476 	KASSERT((pg->pqflags & PQ_SWAPBACKED) != 0);
    477 
    478 	slot = swc->swc_slot + swc->swc_nused;
    479 	uobj = pg->uobject;
    480 	if (uobj == NULL) {
    481 		KASSERT(mutex_owned(&pg->uanon->an_lock));
    482 		pg->uanon->an_swslot = slot;
    483 	} else {
    484 		int result;
    485 
    486 		KASSERT(mutex_owned(&uobj->vmobjlock));
    487 		result = uao_set_swslot(uobj, pg->offset >> PAGE_SHIFT, slot);
    488 		if (result == -1) {
    489 			return ENOMEM;
    490 		}
    491 	}
    492 	swc->swc_pages[swc->swc_nused] = pg;
    493 	swc->swc_nused++;
    494 
    495 	return 0;
    496 }
    497 
    498 static void
    499 swapcluster_flush(struct swapcluster *swc, bool now)
    500 {
    501 	int slot;
    502 	int nused;
    503 	int nallocated;
    504 	int error;
    505 
    506 	if (swc->swc_slot == 0) {
    507 		return;
    508 	}
    509 	KASSERT(swc->swc_nused <= swc->swc_nallocated);
    510 
    511 	slot = swc->swc_slot;
    512 	nused = swc->swc_nused;
    513 	nallocated = swc->swc_nallocated;
    514 
    515 	/*
    516 	 * if this is the final pageout we could have a few
    517 	 * unused swap blocks.  if so, free them now.
    518 	 */
    519 
    520 	if (nused < nallocated) {
    521 		if (!now) {
    522 			return;
    523 		}
    524 		uvm_swap_free(slot + nused, nallocated - nused);
    525 	}
    526 
    527 	/*
    528 	 * now start the pageout.
    529 	 */
    530 
    531 	uvmexp.pdpageouts++;
    532 	uvm_pageout_start(nused);
    533 	error = uvm_swap_put(slot, swc->swc_pages, nused, 0);
    534 	KASSERT(error == 0);
    535 
    536 	/*
    537 	 * zero swslot to indicate that we are
    538 	 * no longer building a swap-backed cluster.
    539 	 */
    540 
    541 	swc->swc_slot = 0;
    542 }
    543 
    544 static int
    545 swapcluster_nused(struct swapcluster *swc)
    546 {
    547 
    548 	return swc->swc_nused;
    549 }
    550 
    551 /*
    552  * uvmpd_dropswap: free any swap allocated to this page.
    553  *
    554  * => called with owner locked.
    555  * => return true if a page had an associated slot.
    556  */
    557 
    558 static bool
    559 uvmpd_dropswap(struct vm_page *pg)
    560 {
    561 	bool result = false;
    562 	struct vm_anon *anon = pg->uanon;
    563 
    564 	if ((pg->pqflags & PQ_ANON) && anon->an_swslot) {
    565 		uvm_swap_free(anon->an_swslot, 1);
    566 		anon->an_swslot = 0;
    567 		pg->flags &= ~PG_CLEAN;
    568 		result = true;
    569 	} else if (pg->pqflags & PQ_AOBJ) {
    570 		int slot = uao_set_swslot(pg->uobject,
    571 		    pg->offset >> PAGE_SHIFT, 0);
    572 		if (slot) {
    573 			uvm_swap_free(slot, 1);
    574 			pg->flags &= ~PG_CLEAN;
    575 			result = true;
    576 		}
    577 	}
    578 
    579 	return result;
    580 }
    581 
    582 /*
    583  * uvmpd_trydropswap: try to free any swap allocated to this page.
    584  *
    585  * => return true if a slot is successfully freed.
    586  */
    587 
    588 bool
    589 uvmpd_trydropswap(struct vm_page *pg)
    590 {
    591 	kmutex_t *slock;
    592 	bool result;
    593 
    594 	if ((pg->flags & PG_BUSY) != 0) {
    595 		return false;
    596 	}
    597 
    598 	/*
    599 	 * lock the page's owner.
    600 	 */
    601 
    602 	slock = uvmpd_trylockowner(pg);
    603 	if (slock == NULL) {
    604 		return false;
    605 	}
    606 
    607 	/*
    608 	 * skip this page if it's busy.
    609 	 */
    610 
    611 	if ((pg->flags & PG_BUSY) != 0) {
    612 		mutex_exit(slock);
    613 		return false;
    614 	}
    615 
    616 	result = uvmpd_dropswap(pg);
    617 
    618 	mutex_exit(slock);
    619 
    620 	return result;
    621 }
    622 
    623 #endif /* defined(VMSWAP) */
    624 
    625 /*
    626  * uvmpd_scan_queue: scan an replace candidate list for pages
    627  * to clean or free.
    628  *
    629  * => called with page queues locked
    630  * => we work on meeting our free target by converting inactive pages
    631  *    into free pages.
    632  * => we handle the building of swap-backed clusters
    633  */
    634 
    635 static void
    636 uvmpd_scan_queue(void)
    637 {
    638 	struct vm_page *p;
    639 	struct uvm_object *uobj;
    640 	struct vm_anon *anon;
    641 #if defined(VMSWAP)
    642 	struct swapcluster swc;
    643 #endif /* defined(VMSWAP) */
    644 	int dirtyreacts;
    645 	kmutex_t *slock;
    646 	UVMHIST_FUNC("uvmpd_scan_queue"); UVMHIST_CALLED(pdhist);
    647 
    648 	/*
    649 	 * swslot is non-zero if we are building a swap cluster.  we want
    650 	 * to stay in the loop while we have a page to scan or we have
    651 	 * a swap-cluster to build.
    652 	 */
    653 
    654 #if defined(VMSWAP)
    655 	swapcluster_init(&swc);
    656 #endif /* defined(VMSWAP) */
    657 
    658 	dirtyreacts = 0;
    659 	uvmpdpol_scaninit();
    660 
    661 	while (/* CONSTCOND */ 1) {
    662 
    663 		/*
    664 		 * see if we've met the free target.
    665 		 */
    666 
    667 		if (uvmexp.free + uvmexp.paging
    668 #if defined(VMSWAP)
    669 		    + swapcluster_nused(&swc)
    670 #endif /* defined(VMSWAP) */
    671 		    >= uvmexp.freetarg << 2 ||
    672 		    dirtyreacts == UVMPD_NUMDIRTYREACTS) {
    673 			UVMHIST_LOG(pdhist,"  met free target: "
    674 				    "exit loop", 0, 0, 0, 0);
    675 			break;
    676 		}
    677 
    678 		p = uvmpdpol_selectvictim();
    679 		if (p == NULL) {
    680 			break;
    681 		}
    682 		KASSERT(uvmpdpol_pageisqueued_p(p));
    683 		KASSERT(p->wire_count == 0);
    684 
    685 		/*
    686 		 * we are below target and have a new page to consider.
    687 		 */
    688 
    689 		anon = p->uanon;
    690 		uobj = p->uobject;
    691 
    692 		/*
    693 		 * first we attempt to lock the object that this page
    694 		 * belongs to.  if our attempt fails we skip on to
    695 		 * the next page (no harm done).  it is important to
    696 		 * "try" locking the object as we are locking in the
    697 		 * wrong order (pageq -> object) and we don't want to
    698 		 * deadlock.
    699 		 *
    700 		 * the only time we expect to see an ownerless page
    701 		 * (i.e. a page with no uobject and !PQ_ANON) is if an
    702 		 * anon has loaned a page from a uvm_object and the
    703 		 * uvm_object has dropped the ownership.  in that
    704 		 * case, the anon can "take over" the loaned page
    705 		 * and make it its own.
    706 		 */
    707 
    708 		slock = uvmpd_trylockowner(p);
    709 		if (slock == NULL) {
    710 			continue;
    711 		}
    712 		if (p->flags & PG_BUSY) {
    713 			mutex_exit(slock);
    714 			uvmexp.pdbusy++;
    715 			continue;
    716 		}
    717 
    718 		/* does the page belong to an object? */
    719 		if (uobj != NULL) {
    720 			uvmexp.pdobscan++;
    721 		} else {
    722 #if defined(VMSWAP)
    723 			KASSERT(anon != NULL);
    724 			uvmexp.pdanscan++;
    725 #else /* defined(VMSWAP) */
    726 			panic("%s: anon", __func__);
    727 #endif /* defined(VMSWAP) */
    728 		}
    729 
    730 
    731 		/*
    732 		 * we now have the object and the page queues locked.
    733 		 * if the page is not swap-backed, call the object's
    734 		 * pager to flush and free the page.
    735 		 */
    736 
    737 #if defined(READAHEAD_STATS)
    738 		if ((p->pqflags & PQ_READAHEAD) != 0) {
    739 			p->pqflags &= ~PQ_READAHEAD;
    740 			uvm_ra_miss.ev_count++;
    741 		}
    742 #endif /* defined(READAHEAD_STATS) */
    743 
    744 		if ((p->pqflags & PQ_SWAPBACKED) == 0) {
    745 			KASSERT(uobj != NULL);
    746 			mutex_exit(&uvm_pageqlock);
    747 			(void) (uobj->pgops->pgo_put)(uobj, p->offset,
    748 			    p->offset + PAGE_SIZE, PGO_CLEANIT|PGO_FREE);
    749 			mutex_enter(&uvm_pageqlock);
    750 			continue;
    751 		}
    752 
    753 		/*
    754 		 * the page is swap-backed.  remove all the permissions
    755 		 * from the page so we can sync the modified info
    756 		 * without any race conditions.  if the page is clean
    757 		 * we can free it now and continue.
    758 		 */
    759 
    760 		pmap_page_protect(p, VM_PROT_NONE);
    761 		if ((p->flags & PG_CLEAN) && pmap_clear_modify(p)) {
    762 			p->flags &= ~(PG_CLEAN);
    763 		}
    764 		if (p->flags & PG_CLEAN) {
    765 			int slot;
    766 			int pageidx;
    767 
    768 			pageidx = p->offset >> PAGE_SHIFT;
    769 			uvm_pagefree(p);
    770 			uvmexp.pdfreed++;
    771 
    772 			/*
    773 			 * for anons, we need to remove the page
    774 			 * from the anon ourselves.  for aobjs,
    775 			 * pagefree did that for us.
    776 			 */
    777 
    778 			if (anon) {
    779 				KASSERT(anon->an_swslot != 0);
    780 				anon->an_page = NULL;
    781 				slot = anon->an_swslot;
    782 			} else {
    783 				slot = uao_find_swslot(uobj, pageidx);
    784 			}
    785 			mutex_exit(slock);
    786 
    787 			if (slot > 0) {
    788 				/* this page is now only in swap. */
    789 				mutex_enter(&uvm_swap_data_lock);
    790 				KASSERT(uvmexp.swpgonly < uvmexp.swpginuse);
    791 				uvmexp.swpgonly++;
    792 				mutex_exit(&uvm_swap_data_lock);
    793 			}
    794 			continue;
    795 		}
    796 
    797 #if defined(VMSWAP)
    798 		/*
    799 		 * this page is dirty, skip it if we'll have met our
    800 		 * free target when all the current pageouts complete.
    801 		 */
    802 
    803 		if (uvmexp.free + uvmexp.paging > uvmexp.freetarg << 2) {
    804 			mutex_exit(slock);
    805 			continue;
    806 		}
    807 
    808 		/*
    809 		 * free any swap space allocated to the page since
    810 		 * we'll have to write it again with its new data.
    811 		 */
    812 
    813 		uvmpd_dropswap(p);
    814 
    815 		/*
    816 		 * if all pages in swap are only in swap,
    817 		 * the swap space is full and we can't page out
    818 		 * any more swap-backed pages.  reactivate this page
    819 		 * so that we eventually cycle all pages through
    820 		 * the inactive queue.
    821 		 */
    822 
    823 		if (uvm_swapisfull()) {
    824 			dirtyreacts++;
    825 			uvm_pageactivate(p);
    826 			mutex_exit(slock);
    827 			continue;
    828 		}
    829 
    830 		/*
    831 		 * start new swap pageout cluster (if necessary).
    832 		 */
    833 
    834 		if (swapcluster_allocslots(&swc)) {
    835 			mutex_exit(slock);
    836 			dirtyreacts++; /* XXX */
    837 			continue;
    838 		}
    839 
    840 		/*
    841 		 * at this point, we're definitely going reuse this
    842 		 * page.  mark the page busy and delayed-free.
    843 		 * we should remove the page from the page queues
    844 		 * so we don't ever look at it again.
    845 		 * adjust counters and such.
    846 		 */
    847 
    848 		p->flags |= PG_BUSY;
    849 		UVM_PAGE_OWN(p, "scan_queue");
    850 
    851 		p->flags |= PG_PAGEOUT;
    852 		uvm_pagedequeue(p);
    853 
    854 		uvmexp.pgswapout++;
    855 		mutex_exit(&uvm_pageqlock);
    856 
    857 		/*
    858 		 * add the new page to the cluster.
    859 		 */
    860 
    861 		if (swapcluster_add(&swc, p)) {
    862 			p->flags &= ~(PG_BUSY|PG_PAGEOUT);
    863 			UVM_PAGE_OWN(p, NULL);
    864 			mutex_enter(&uvm_pageqlock);
    865 			dirtyreacts++;
    866 			uvm_pageactivate(p);
    867 			mutex_exit(slock);
    868 			continue;
    869 		}
    870 		mutex_exit(slock);
    871 
    872 		swapcluster_flush(&swc, false);
    873 		mutex_enter(&uvm_pageqlock);
    874 
    875 		/*
    876 		 * the pageout is in progress.  bump counters and set up
    877 		 * for the next loop.
    878 		 */
    879 
    880 		uvmexp.pdpending++;
    881 
    882 #else /* defined(VMSWAP) */
    883 		uvm_pageactivate(p);
    884 		mutex_exit(slock);
    885 #endif /* defined(VMSWAP) */
    886 	}
    887 
    888 #if defined(VMSWAP)
    889 	mutex_exit(&uvm_pageqlock);
    890 	swapcluster_flush(&swc, true);
    891 	mutex_enter(&uvm_pageqlock);
    892 #endif /* defined(VMSWAP) */
    893 }
    894 
    895 /*
    896  * uvmpd_scan: scan the page queues and attempt to meet our targets.
    897  *
    898  * => called with pageq's locked
    899  */
    900 
    901 static void
    902 uvmpd_scan(void)
    903 {
    904 	int swap_shortage, pages_freed;
    905 	UVMHIST_FUNC("uvmpd_scan"); UVMHIST_CALLED(pdhist);
    906 
    907 	uvmexp.pdrevs++;
    908 
    909 #ifndef __SWAP_BROKEN
    910 
    911 	/*
    912 	 * swap out some processes if we are below our free target.
    913 	 * we need to unlock the page queues for this.
    914 	 */
    915 
    916 	if (uvmexp.free < uvmexp.freetarg && uvmexp.nswapdev != 0 &&
    917 	    uvm.swapout_enabled) {
    918 		uvmexp.pdswout++;
    919 		UVMHIST_LOG(pdhist,"  free %d < target %d: swapout",
    920 		    uvmexp.free, uvmexp.freetarg, 0, 0);
    921 		mutex_exit(&uvm_pageqlock);
    922 		uvm_swapout_threads();
    923 		mutex_enter(&uvm_pageqlock);
    924 
    925 	}
    926 #endif
    927 
    928 	/*
    929 	 * now we want to work on meeting our targets.   first we work on our
    930 	 * free target by converting inactive pages into free pages.  then
    931 	 * we work on meeting our inactive target by converting active pages
    932 	 * to inactive ones.
    933 	 */
    934 
    935 	UVMHIST_LOG(pdhist, "  starting 'free' loop",0,0,0,0);
    936 
    937 	pages_freed = uvmexp.pdfreed;
    938 	uvmpd_scan_queue();
    939 	pages_freed = uvmexp.pdfreed - pages_freed;
    940 
    941 	/*
    942 	 * detect if we're not going to be able to page anything out
    943 	 * until we free some swap resources from active pages.
    944 	 */
    945 
    946 	swap_shortage = 0;
    947 	if (uvmexp.free < uvmexp.freetarg &&
    948 	    uvmexp.swpginuse >= uvmexp.swpgavail &&
    949 	    !uvm_swapisfull() &&
    950 	    pages_freed == 0) {
    951 		swap_shortage = uvmexp.freetarg - uvmexp.free;
    952 	}
    953 
    954 	uvmpdpol_balancequeue(swap_shortage);
    955 }
    956 
    957 /*
    958  * uvm_reclaimable: decide whether to wait for pagedaemon.
    959  *
    960  * => return true if it seems to be worth to do uvm_wait.
    961  *
    962  * XXX should be tunable.
    963  * XXX should consider pools, etc?
    964  */
    965 
    966 bool
    967 uvm_reclaimable(void)
    968 {
    969 	int filepages;
    970 	int active, inactive;
    971 
    972 	/*
    973 	 * if swap is not full, no problem.
    974 	 */
    975 
    976 	if (!uvm_swapisfull()) {
    977 		return true;
    978 	}
    979 
    980 	/*
    981 	 * file-backed pages can be reclaimed even when swap is full.
    982 	 * if we have more than 1/16 of pageable memory or 5MB, try to reclaim.
    983 	 *
    984 	 * XXX assume the worst case, ie. all wired pages are file-backed.
    985 	 *
    986 	 * XXX should consider about other reclaimable memory.
    987 	 * XXX ie. pools, traditional buffer cache.
    988 	 */
    989 
    990 	filepages = uvmexp.filepages + uvmexp.execpages - uvmexp.wired;
    991 	uvm_estimatepageable(&active, &inactive);
    992 	if (filepages >= MIN((active + inactive) >> 4,
    993 	    5 * 1024 * 1024 >> PAGE_SHIFT)) {
    994 		return true;
    995 	}
    996 
    997 	/*
    998 	 * kill the process, fail allocation, etc..
    999 	 */
   1000 
   1001 	return false;
   1002 }
   1003 
   1004 void
   1005 uvm_estimatepageable(int *active, int *inactive)
   1006 {
   1007 
   1008 	uvmpdpol_estimatepageable(active, inactive);
   1009 }
   1010