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