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