Home | History | Annotate | Line # | Download | only in uvm
uvm_pdaemon.c revision 1.55
      1 /*	$NetBSD: uvm_pdaemon.c,v 1.55 2003/09/26 04:03:39 chs 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.55 2003/09/26 04:03:39 chs Exp $");
     75 
     76 #include "opt_uvmhist.h"
     77 
     78 #include <sys/param.h>
     79 #include <sys/proc.h>
     80 #include <sys/systm.h>
     81 #include <sys/kernel.h>
     82 #include <sys/pool.h>
     83 #include <sys/buf.h>
     84 #include <sys/vnode.h>
     85 
     86 #include <uvm/uvm.h>
     87 
     88 /*
     89  * UVMPD_NUMDIRTYREACTS is how many dirty pages the pagedaemon will reactivate
     90  * in a pass thru the inactive list when swap is full.  the value should be
     91  * "small"... if it's too large we'll cycle the active pages thru the inactive
     92  * queue too quickly to for them to be referenced and avoid being freed.
     93  */
     94 
     95 #define UVMPD_NUMDIRTYREACTS 16
     96 
     97 
     98 /*
     99  * local prototypes
    100  */
    101 
    102 void		uvmpd_scan __P((void));
    103 void		uvmpd_scan_inactive __P((struct pglist *));
    104 void		uvmpd_tune __P((void));
    105 
    106 /*
    107  * uvm_wait: wait (sleep) for the page daemon to free some pages
    108  *
    109  * => should be called with all locks released
    110  * => should _not_ be called by the page daemon (to avoid deadlock)
    111  */
    112 
    113 void
    114 uvm_wait(wmsg)
    115 	const char *wmsg;
    116 {
    117 	int timo = 0;
    118 	int s = splbio();
    119 
    120 	/*
    121 	 * check for page daemon going to sleep (waiting for itself)
    122 	 */
    123 
    124 	if (curproc == uvm.pagedaemon_proc && uvmexp.paging == 0) {
    125 		/*
    126 		 * now we have a problem: the pagedaemon wants to go to
    127 		 * sleep until it frees more memory.   but how can it
    128 		 * free more memory if it is asleep?  that is a deadlock.
    129 		 * we have two options:
    130 		 *  [1] panic now
    131 		 *  [2] put a timeout on the sleep, thus causing the
    132 		 *      pagedaemon to only pause (rather than sleep forever)
    133 		 *
    134 		 * note that option [2] will only help us if we get lucky
    135 		 * and some other process on the system breaks the deadlock
    136 		 * by exiting or freeing memory (thus allowing the pagedaemon
    137 		 * to continue).  for now we panic if DEBUG is defined,
    138 		 * otherwise we hope for the best with option [2] (better
    139 		 * yet, this should never happen in the first place!).
    140 		 */
    141 
    142 		printf("pagedaemon: deadlock detected!\n");
    143 		timo = hz >> 3;		/* set timeout */
    144 #if defined(DEBUG)
    145 		/* DEBUG: panic so we can debug it */
    146 		panic("pagedaemon deadlock");
    147 #endif
    148 	}
    149 
    150 	simple_lock(&uvm.pagedaemon_lock);
    151 	wakeup(&uvm.pagedaemon);		/* wake the daemon! */
    152 	UVM_UNLOCK_AND_WAIT(&uvmexp.free, &uvm.pagedaemon_lock, FALSE, wmsg,
    153 	    timo);
    154 
    155 	splx(s);
    156 }
    157 
    158 
    159 /*
    160  * uvmpd_tune: tune paging parameters
    161  *
    162  * => called when ever memory is added (or removed?) to the system
    163  * => caller must call with page queues locked
    164  */
    165 
    166 void
    167 uvmpd_tune(void)
    168 {
    169 	UVMHIST_FUNC("uvmpd_tune"); UVMHIST_CALLED(pdhist);
    170 
    171 	uvmexp.freemin = uvmexp.npages / 20;
    172 
    173 	/* between 16k and 256k */
    174 	/* XXX:  what are these values good for? */
    175 	uvmexp.freemin = MAX(uvmexp.freemin, (16*1024) >> PAGE_SHIFT);
    176 	uvmexp.freemin = MIN(uvmexp.freemin, (256*1024) >> PAGE_SHIFT);
    177 
    178 	/* Make sure there's always a user page free. */
    179 	if (uvmexp.freemin < uvmexp.reserve_kernel + 1)
    180 		uvmexp.freemin = uvmexp.reserve_kernel + 1;
    181 
    182 	uvmexp.freetarg = (uvmexp.freemin * 4) / 3;
    183 	if (uvmexp.freetarg <= uvmexp.freemin)
    184 		uvmexp.freetarg = uvmexp.freemin + 1;
    185 
    186 	/* uvmexp.inactarg: computed in main daemon loop */
    187 
    188 	uvmexp.wiredmax = uvmexp.npages / 3;
    189 	UVMHIST_LOG(pdhist, "<- done, freemin=%d, freetarg=%d, wiredmax=%d",
    190 	      uvmexp.freemin, uvmexp.freetarg, uvmexp.wiredmax, 0);
    191 }
    192 
    193 /*
    194  * uvm_pageout: the main loop for the pagedaemon
    195  */
    196 
    197 void
    198 uvm_pageout(void *arg)
    199 {
    200 	int npages = 0;
    201 	UVMHIST_FUNC("uvm_pageout"); UVMHIST_CALLED(pdhist);
    202 
    203 	UVMHIST_LOG(pdhist,"<starting uvm pagedaemon>", 0, 0, 0, 0);
    204 
    205 	/*
    206 	 * ensure correct priority and set paging parameters...
    207 	 */
    208 
    209 	uvm.pagedaemon_proc = curproc;
    210 	uvm_lock_pageq();
    211 	npages = uvmexp.npages;
    212 	uvmpd_tune();
    213 	uvm_unlock_pageq();
    214 
    215 	/*
    216 	 * main loop
    217 	 */
    218 
    219 	for (;;) {
    220 		simple_lock(&uvm.pagedaemon_lock);
    221 
    222 		UVMHIST_LOG(pdhist,"  <<SLEEPING>>",0,0,0,0);
    223 		UVM_UNLOCK_AND_WAIT(&uvm.pagedaemon,
    224 		    &uvm.pagedaemon_lock, FALSE, "pgdaemon", 0);
    225 		uvmexp.pdwoke++;
    226 		UVMHIST_LOG(pdhist,"  <<WOKE UP>>",0,0,0,0);
    227 
    228 		/*
    229 		 * now lock page queues and recompute inactive count
    230 		 */
    231 
    232 		uvm_lock_pageq();
    233 		if (npages != uvmexp.npages) {	/* check for new pages? */
    234 			npages = uvmexp.npages;
    235 			uvmpd_tune();
    236 		}
    237 
    238 		uvmexp.inactarg = (uvmexp.active + uvmexp.inactive) / 3;
    239 		if (uvmexp.inactarg <= uvmexp.freetarg) {
    240 			uvmexp.inactarg = uvmexp.freetarg + 1;
    241 		}
    242 
    243 		UVMHIST_LOG(pdhist,"  free/ftarg=%d/%d, inact/itarg=%d/%d",
    244 		    uvmexp.free, uvmexp.freetarg, uvmexp.inactive,
    245 		    uvmexp.inactarg);
    246 
    247 		/*
    248 		 * scan if needed
    249 		 */
    250 
    251 		if (uvmexp.free + uvmexp.paging < uvmexp.freetarg ||
    252 		    uvmexp.inactive < uvmexp.inactarg) {
    253 			uvmpd_scan();
    254 		}
    255 
    256 		/*
    257 		 * if there's any free memory to be had,
    258 		 * wake up any waiters.
    259 		 */
    260 
    261 		if (uvmexp.free > uvmexp.reserve_kernel ||
    262 		    uvmexp.paging == 0) {
    263 			wakeup(&uvmexp.free);
    264 		}
    265 
    266 		/*
    267 		 * scan done.  unlock page queues (the only lock we are holding)
    268 		 */
    269 
    270 		uvm_unlock_pageq();
    271 
    272 		/*
    273 		 * drain pool resources now that we're not holding any locks
    274 		 */
    275 
    276 		pool_drain(0);
    277 	}
    278 	/*NOTREACHED*/
    279 }
    280 
    281 
    282 /*
    283  * uvm_aiodone_daemon:  main loop for the aiodone daemon.
    284  */
    285 
    286 void
    287 uvm_aiodone_daemon(void *arg)
    288 {
    289 	int s, free;
    290 	struct buf *bp, *nbp;
    291 	UVMHIST_FUNC("uvm_aiodoned"); UVMHIST_CALLED(pdhist);
    292 
    293 	for (;;) {
    294 
    295 		/*
    296 		 * carefully attempt to go to sleep (without losing "wakeups"!).
    297 		 * we need splbio because we want to make sure the aio_done list
    298 		 * is totally empty before we go to sleep.
    299 		 */
    300 
    301 		s = splbio();
    302 		simple_lock(&uvm.aiodoned_lock);
    303 		if (TAILQ_FIRST(&uvm.aio_done) == NULL) {
    304 			UVMHIST_LOG(pdhist,"  <<SLEEPING>>",0,0,0,0);
    305 			UVM_UNLOCK_AND_WAIT(&uvm.aiodoned,
    306 			    &uvm.aiodoned_lock, FALSE, "aiodoned", 0);
    307 			UVMHIST_LOG(pdhist,"  <<WOKE UP>>",0,0,0,0);
    308 
    309 			/* relock aiodoned_lock, still at splbio */
    310 			simple_lock(&uvm.aiodoned_lock);
    311 		}
    312 
    313 		/*
    314 		 * check for done aio structures
    315 		 */
    316 
    317 		bp = TAILQ_FIRST(&uvm.aio_done);
    318 		if (bp) {
    319 			TAILQ_INIT(&uvm.aio_done);
    320 		}
    321 
    322 		simple_unlock(&uvm.aiodoned_lock);
    323 		splx(s);
    324 
    325 		/*
    326 		 * process each i/o that's done.
    327 		 */
    328 
    329 		free = uvmexp.free;
    330 		while (bp != NULL) {
    331 			nbp = TAILQ_NEXT(bp, b_freelist);
    332 			(*bp->b_iodone)(bp);
    333 			bp = nbp;
    334 		}
    335 		if (free <= uvmexp.reserve_kernel) {
    336 			s = uvm_lock_fpageq();
    337 			wakeup(&uvm.pagedaemon);
    338 			uvm_unlock_fpageq(s);
    339 		} else {
    340 			simple_lock(&uvm.pagedaemon_lock);
    341 			wakeup(&uvmexp.free);
    342 			simple_unlock(&uvm.pagedaemon_lock);
    343 		}
    344 	}
    345 }
    346 
    347 /*
    348  * uvmpd_scan_inactive: scan an inactive list for pages to clean or free.
    349  *
    350  * => called with page queues locked
    351  * => we work on meeting our free target by converting inactive pages
    352  *    into free pages.
    353  * => we handle the building of swap-backed clusters
    354  * => we return TRUE if we are exiting because we met our target
    355  */
    356 
    357 void
    358 uvmpd_scan_inactive(pglst)
    359 	struct pglist *pglst;
    360 {
    361 	int error;
    362 	struct vm_page *p, *nextpg = NULL; /* Quell compiler warning */
    363 	struct uvm_object *uobj;
    364 	struct vm_anon *anon;
    365 	struct vm_page *swpps[round_page(MAXPHYS) >> PAGE_SHIFT];
    366 	struct simplelock *slock;
    367 	int swnpages, swcpages;
    368 	int swslot;
    369 	int dirtyreacts, t, result;
    370 	boolean_t anonunder, fileunder, execunder;
    371 	boolean_t anonover, fileover, execover;
    372 	boolean_t anonreact, filereact, execreact;
    373 	UVMHIST_FUNC("uvmpd_scan_inactive"); UVMHIST_CALLED(pdhist);
    374 
    375 	/*
    376 	 * swslot is non-zero if we are building a swap cluster.  we want
    377 	 * to stay in the loop while we have a page to scan or we have
    378 	 * a swap-cluster to build.
    379 	 */
    380 
    381 	swslot = 0;
    382 	swnpages = swcpages = 0;
    383 	dirtyreacts = 0;
    384 
    385 	/*
    386 	 * decide which types of pages we want to reactivate instead of freeing
    387 	 * to keep usage within the minimum and maximum usage limits.
    388 	 */
    389 
    390 	t = uvmexp.active + uvmexp.inactive + uvmexp.free;
    391 	anonunder = (uvmexp.anonpages <= (t * uvmexp.anonmin) >> 8);
    392 	fileunder = (uvmexp.filepages <= (t * uvmexp.filemin) >> 8);
    393 	execunder = (uvmexp.execpages <= (t * uvmexp.execmin) >> 8);
    394 	anonover = uvmexp.anonpages > ((t * uvmexp.anonmax) >> 8);
    395 	fileover = uvmexp.filepages > ((t * uvmexp.filemax) >> 8);
    396 	execover = uvmexp.execpages > ((t * uvmexp.execmax) >> 8);
    397 	anonreact = anonunder || (!anonover && (fileover || execover));
    398 	filereact = fileunder || (!fileover && (anonover || execover));
    399 	execreact = execunder || (!execover && (anonover || fileover));
    400 	for (p = TAILQ_FIRST(pglst); p != NULL || swslot != 0; p = nextpg) {
    401 		uobj = NULL;
    402 		anon = NULL;
    403 		if (p) {
    404 
    405 			/*
    406 			 * see if we've met the free target.
    407 			 */
    408 
    409 			if (uvmexp.free + uvmexp.paging >=
    410 			    uvmexp.freetarg << 2 ||
    411 			    dirtyreacts == UVMPD_NUMDIRTYREACTS) {
    412 				UVMHIST_LOG(pdhist,"  met free target: "
    413 					    "exit loop", 0, 0, 0, 0);
    414 
    415 				if (swslot == 0) {
    416 					/* exit now if no swap-i/o pending */
    417 					break;
    418 				}
    419 
    420 				/* set p to null to signal final swap i/o */
    421 				p = NULL;
    422 				nextpg = NULL;
    423 			}
    424 		}
    425 		if (p) {	/* if (we have a new page to consider) */
    426 
    427 			/*
    428 			 * we are below target and have a new page to consider.
    429 			 */
    430 
    431 			uvmexp.pdscans++;
    432 			nextpg = TAILQ_NEXT(p, pageq);
    433 
    434 			/*
    435 			 * move referenced pages back to active queue and
    436 			 * skip to next page.
    437 			 */
    438 
    439 			if (pmap_clear_reference(p)) {
    440 				uvm_pageactivate(p);
    441 				uvmexp.pdreact++;
    442 				continue;
    443 			}
    444 			anon = p->uanon;
    445 			uobj = p->uobject;
    446 
    447 			/*
    448 			 * enforce the minimum thresholds on different
    449 			 * types of memory usage.  if reusing the current
    450 			 * page would reduce that type of usage below its
    451 			 * minimum, reactivate the page instead and move
    452 			 * on to the next page.
    453 			 */
    454 
    455 			if (uobj && UVM_OBJ_IS_VTEXT(uobj) && execreact) {
    456 				uvm_pageactivate(p);
    457 				uvmexp.pdreexec++;
    458 				continue;
    459 			}
    460 			if (uobj && UVM_OBJ_IS_VNODE(uobj) &&
    461 			    !UVM_OBJ_IS_VTEXT(uobj) && filereact) {
    462 				uvm_pageactivate(p);
    463 				uvmexp.pdrefile++;
    464 				continue;
    465 			}
    466 			if ((anon || UVM_OBJ_IS_AOBJ(uobj)) && anonreact) {
    467 				uvm_pageactivate(p);
    468 				uvmexp.pdreanon++;
    469 				continue;
    470 			}
    471 
    472 			/*
    473 			 * first we attempt to lock the object that this page
    474 			 * belongs to.  if our attempt fails we skip on to
    475 			 * the next page (no harm done).  it is important to
    476 			 * "try" locking the object as we are locking in the
    477 			 * wrong order (pageq -> object) and we don't want to
    478 			 * deadlock.
    479 			 *
    480 			 * the only time we expect to see an ownerless page
    481 			 * (i.e. a page with no uobject and !PQ_ANON) is if an
    482 			 * anon has loaned a page from a uvm_object and the
    483 			 * uvm_object has dropped the ownership.  in that
    484 			 * case, the anon can "take over" the loaned page
    485 			 * and make it its own.
    486 			 */
    487 
    488 			/* does the page belong to an object? */
    489 			if (uobj != NULL) {
    490 				slock = &uobj->vmobjlock;
    491 				if (!simple_lock_try(slock)) {
    492 					continue;
    493 				}
    494 				if (p->flags & PG_BUSY) {
    495 					simple_unlock(slock);
    496 					uvmexp.pdbusy++;
    497 					continue;
    498 				}
    499 				uvmexp.pdobscan++;
    500 			} else {
    501 				KASSERT(anon != NULL);
    502 				slock = &anon->an_lock;
    503 				if (!simple_lock_try(slock)) {
    504 					continue;
    505 				}
    506 
    507 				/*
    508 				 * set PQ_ANON if it isn't set already.
    509 				 */
    510 
    511 				if ((p->pqflags & PQ_ANON) == 0) {
    512 					KASSERT(p->loan_count > 0);
    513 					p->loan_count--;
    514 					p->pqflags |= PQ_ANON;
    515 					/* anon now owns it */
    516 				}
    517 				if (p->flags & PG_BUSY) {
    518 					simple_unlock(slock);
    519 					uvmexp.pdbusy++;
    520 					continue;
    521 				}
    522 				uvmexp.pdanscan++;
    523 			}
    524 
    525 
    526 			/*
    527 			 * we now have the object and the page queues locked.
    528 			 * if the page is not swap-backed, call the object's
    529 			 * pager to flush and free the page.
    530 			 */
    531 
    532 			if ((p->pqflags & PQ_SWAPBACKED) == 0) {
    533 				uvm_unlock_pageq();
    534 				(void) (uobj->pgops->pgo_put)(uobj, p->offset,
    535 				    p->offset + PAGE_SIZE,
    536 				    PGO_CLEANIT|PGO_FREE);
    537 				uvm_lock_pageq();
    538 				if (nextpg &&
    539 				    (nextpg->pqflags & PQ_INACTIVE) == 0) {
    540 					nextpg = TAILQ_FIRST(pglst);
    541 				}
    542 				continue;
    543 			}
    544 
    545 			/*
    546 			 * the page is swap-backed.  remove all the permissions
    547 			 * from the page so we can sync the modified info
    548 			 * without any race conditions.  if the page is clean
    549 			 * we can free it now and continue.
    550 			 */
    551 
    552 			pmap_page_protect(p, VM_PROT_NONE);
    553 			if ((p->flags & PG_CLEAN) && pmap_clear_modify(p)) {
    554 				p->flags &= ~(PG_CLEAN);
    555 			}
    556 			if (p->flags & PG_CLEAN) {
    557 				int slot;
    558 				int pageidx;
    559 
    560 				pageidx = p->offset >> PAGE_SHIFT;
    561 				uvm_pagefree(p);
    562 				uvmexp.pdfreed++;
    563 
    564 				/*
    565 				 * for anons, we need to remove the page
    566 				 * from the anon ourselves.  for aobjs,
    567 				 * pagefree did that for us.
    568 				 */
    569 
    570 				if (anon) {
    571 					KASSERT(anon->an_swslot != 0);
    572 					anon->u.an_page = NULL;
    573 					slot = anon->an_swslot;
    574 				} else {
    575 					slot = uao_find_swslot(uobj, pageidx);
    576 				}
    577 				simple_unlock(slock);
    578 
    579 				if (slot > 0) {
    580 					/* this page is now only in swap. */
    581 					simple_lock(&uvm.swap_data_lock);
    582 					KASSERT(uvmexp.swpgonly <
    583 						uvmexp.swpginuse);
    584 					uvmexp.swpgonly++;
    585 					simple_unlock(&uvm.swap_data_lock);
    586 				}
    587 				continue;
    588 			}
    589 
    590 			/*
    591 			 * this page is dirty, skip it if we'll have met our
    592 			 * free target when all the current pageouts complete.
    593 			 */
    594 
    595 			if (uvmexp.free + uvmexp.paging >
    596 			    uvmexp.freetarg << 2) {
    597 				simple_unlock(slock);
    598 				continue;
    599 			}
    600 
    601 			/*
    602 			 * free any swap space allocated to the page since
    603 			 * we'll have to write it again with its new data.
    604 			 */
    605 
    606 			if ((p->pqflags & PQ_ANON) && anon->an_swslot) {
    607 				uvm_swap_free(anon->an_swslot, 1);
    608 				anon->an_swslot = 0;
    609 			} else if (p->pqflags & PQ_AOBJ) {
    610 				uao_dropswap(uobj, p->offset >> PAGE_SHIFT);
    611 			}
    612 
    613 			/*
    614 			 * if all pages in swap are only in swap,
    615 			 * the swap space is full and we can't page out
    616 			 * any more swap-backed pages.  reactivate this page
    617 			 * so that we eventually cycle all pages through
    618 			 * the inactive queue.
    619 			 */
    620 
    621 			if (uvm_swapisfull()) {
    622 				dirtyreacts++;
    623 				uvm_pageactivate(p);
    624 				simple_unlock(slock);
    625 				continue;
    626 			}
    627 
    628 			/*
    629 			 * start new swap pageout cluster (if necessary).
    630 			 */
    631 
    632 			if (swslot == 0) {
    633 				/* Even with strange MAXPHYS, the shift
    634 				   implicitly rounds down to a page. */
    635 				swnpages = MAXPHYS >> PAGE_SHIFT;
    636 				swslot = uvm_swap_alloc(&swnpages, TRUE);
    637 				if (swslot == 0) {
    638 					simple_unlock(slock);
    639 					continue;
    640 				}
    641 				swcpages = 0;
    642 			}
    643 
    644 			/*
    645 			 * at this point, we're definitely going reuse this
    646 			 * page.  mark the page busy and delayed-free.
    647 			 * we should remove the page from the page queues
    648 			 * so we don't ever look at it again.
    649 			 * adjust counters and such.
    650 			 */
    651 
    652 			p->flags |= PG_BUSY;
    653 			UVM_PAGE_OWN(p, "scan_inactive");
    654 
    655 			p->flags |= PG_PAGEOUT;
    656 			uvmexp.paging++;
    657 			uvm_pagedequeue(p);
    658 
    659 			uvmexp.pgswapout++;
    660 
    661 			/*
    662 			 * add the new page to the cluster.
    663 			 */
    664 
    665 			if (anon) {
    666 				anon->an_swslot = swslot + swcpages;
    667 				simple_unlock(slock);
    668 			} else {
    669 				result = uao_set_swslot(uobj,
    670 				    p->offset >> PAGE_SHIFT, swslot + swcpages);
    671 				if (result == -1) {
    672 					p->flags &= ~(PG_BUSY|PG_PAGEOUT);
    673 					UVM_PAGE_OWN(p, NULL);
    674 					uvmexp.paging--;
    675 					uvm_pageactivate(p);
    676 					simple_unlock(slock);
    677 					continue;
    678 				}
    679 				simple_unlock(slock);
    680 			}
    681 			swpps[swcpages] = p;
    682 			swcpages++;
    683 
    684 			/*
    685 			 * if the cluster isn't full, look for more pages
    686 			 * before starting the i/o.
    687 			 */
    688 
    689 			if (swcpages < swnpages) {
    690 				continue;
    691 			}
    692 		}
    693 
    694 		/*
    695 		 * if this is the final pageout we could have a few
    696 		 * unused swap blocks.  if so, free them now.
    697 		 */
    698 
    699 		if (swcpages < swnpages) {
    700 			uvm_swap_free(swslot + swcpages, (swnpages - swcpages));
    701 		}
    702 
    703 		/*
    704 		 * now start the pageout.
    705 		 */
    706 
    707 		uvm_unlock_pageq();
    708 		uvmexp.pdpageouts++;
    709 		error = uvm_swap_put(swslot, swpps, swcpages, 0);
    710 		KASSERT(error == 0);
    711 		uvm_lock_pageq();
    712 
    713 		/*
    714 		 * zero swslot to indicate that we are
    715 		 * no longer building a swap-backed cluster.
    716 		 */
    717 
    718 		swslot = 0;
    719 
    720 		/*
    721 		 * the pageout is in progress.  bump counters and set up
    722 		 * for the next loop.
    723 		 */
    724 
    725 		uvmexp.pdpending++;
    726 		if (nextpg && (nextpg->pqflags & PQ_INACTIVE) == 0) {
    727 			nextpg = TAILQ_FIRST(pglst);
    728 		}
    729 	}
    730 }
    731 
    732 /*
    733  * uvmpd_scan: scan the page queues and attempt to meet our targets.
    734  *
    735  * => called with pageq's locked
    736  */
    737 
    738 void
    739 uvmpd_scan(void)
    740 {
    741 	int inactive_shortage, swap_shortage, pages_freed;
    742 	struct vm_page *p, *nextpg;
    743 	struct uvm_object *uobj;
    744 	struct vm_anon *anon;
    745 	struct simplelock *slock;
    746 	UVMHIST_FUNC("uvmpd_scan"); UVMHIST_CALLED(pdhist);
    747 
    748 	uvmexp.pdrevs++;
    749 	uobj = NULL;
    750 	anon = NULL;
    751 
    752 #ifndef __SWAP_BROKEN
    753 
    754 	/*
    755 	 * swap out some processes if we are below our free target.
    756 	 * we need to unlock the page queues for this.
    757 	 */
    758 
    759 	if (uvmexp.free < uvmexp.freetarg && uvmexp.nswapdev != 0) {
    760 		uvmexp.pdswout++;
    761 		UVMHIST_LOG(pdhist,"  free %d < target %d: swapout",
    762 		    uvmexp.free, uvmexp.freetarg, 0, 0);
    763 		uvm_unlock_pageq();
    764 		uvm_swapout_threads();
    765 		uvm_lock_pageq();
    766 
    767 	}
    768 #endif
    769 
    770 	/*
    771 	 * now we want to work on meeting our targets.   first we work on our
    772 	 * free target by converting inactive pages into free pages.  then
    773 	 * we work on meeting our inactive target by converting active pages
    774 	 * to inactive ones.
    775 	 */
    776 
    777 	UVMHIST_LOG(pdhist, "  starting 'free' loop",0,0,0,0);
    778 
    779 	pages_freed = uvmexp.pdfreed;
    780 	uvmpd_scan_inactive(&uvm.page_inactive);
    781 	pages_freed = uvmexp.pdfreed - pages_freed;
    782 
    783 	/*
    784 	 * we have done the scan to get free pages.   now we work on meeting
    785 	 * our inactive target.
    786 	 */
    787 
    788 	inactive_shortage = uvmexp.inactarg - uvmexp.inactive;
    789 
    790 	/*
    791 	 * detect if we're not going to be able to page anything out
    792 	 * until we free some swap resources from active pages.
    793 	 */
    794 
    795 	swap_shortage = 0;
    796 	if (uvmexp.free < uvmexp.freetarg &&
    797 	    uvmexp.swpginuse >= uvmexp.swpgavail &&
    798 	    !uvm_swapisfull() &&
    799 	    pages_freed == 0) {
    800 		swap_shortage = uvmexp.freetarg - uvmexp.free;
    801 	}
    802 
    803 	UVMHIST_LOG(pdhist, "  loop 2: inactive_shortage=%d swap_shortage=%d",
    804 		    inactive_shortage, swap_shortage,0,0);
    805 	for (p = TAILQ_FIRST(&uvm.page_active);
    806 	     p != NULL && (inactive_shortage > 0 || swap_shortage > 0);
    807 	     p = nextpg) {
    808 		nextpg = TAILQ_NEXT(p, pageq);
    809 		if (p->flags & PG_BUSY) {
    810 			continue;
    811 		}
    812 
    813 		/*
    814 		 * lock the page's owner.
    815 		 */
    816 
    817 		if (p->uobject != NULL) {
    818 			uobj = p->uobject;
    819 			slock = &uobj->vmobjlock;
    820 			if (!simple_lock_try(slock)) {
    821 				continue;
    822 			}
    823 		} else {
    824 			anon = p->uanon;
    825 			KASSERT(anon != NULL);
    826 			slock = &anon->an_lock;
    827 			if (!simple_lock_try(slock)) {
    828 				continue;
    829 			}
    830 
    831 			/* take over the page? */
    832 			if ((p->pqflags & PQ_ANON) == 0) {
    833 				KASSERT(p->loan_count > 0);
    834 				p->loan_count--;
    835 				p->pqflags |= PQ_ANON;
    836 			}
    837 		}
    838 
    839 		/*
    840 		 * skip this page if it's busy.
    841 		 */
    842 
    843 		if ((p->flags & PG_BUSY) != 0) {
    844 			simple_unlock(slock);
    845 			continue;
    846 		}
    847 
    848 		/*
    849 		 * if there's a shortage of swap, free any swap allocated
    850 		 * to this page so that other pages can be paged out.
    851 		 */
    852 
    853 		if (swap_shortage > 0) {
    854 			if ((p->pqflags & PQ_ANON) && anon->an_swslot) {
    855 				uvm_swap_free(anon->an_swslot, 1);
    856 				anon->an_swslot = 0;
    857 				p->flags &= ~PG_CLEAN;
    858 				swap_shortage--;
    859 			} else if (p->pqflags & PQ_AOBJ) {
    860 				int slot = uao_set_swslot(uobj,
    861 					p->offset >> PAGE_SHIFT, 0);
    862 				if (slot) {
    863 					uvm_swap_free(slot, 1);
    864 					p->flags &= ~PG_CLEAN;
    865 					swap_shortage--;
    866 				}
    867 			}
    868 		}
    869 
    870 		/*
    871 		 * if there's a shortage of inactive pages, deactivate.
    872 		 */
    873 
    874 		if (inactive_shortage > 0) {
    875 			/* no need to check wire_count as pg is "active" */
    876 			uvm_pagedeactivate(p);
    877 			uvmexp.pddeact++;
    878 			inactive_shortage--;
    879 		}
    880 
    881 		/*
    882 		 * we're done with this page.
    883 		 */
    884 
    885 		simple_unlock(slock);
    886 	}
    887 }
    888