Home | History | Annotate | Line # | Download | only in uvm
uvm_pdaemon.c revision 1.29
      1 /*	$NetBSD: uvm_pdaemon.c,v 1.29 2001/01/28 23:30:46 thorpej 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 
     82 #include <uvm/uvm.h>
     83 
     84 /*
     85  * UVMPD_NUMDIRTYREACTS is how many dirty pages the pagedeamon will reactivate
     86  * in a pass thru the inactive list when swap is full.  the value should be
     87  * "small"... if it's too large we'll cycle the active pages thru the inactive
     88  * queue too quickly to for them to be referenced and avoid being freed.
     89  */
     90 
     91 #define UVMPD_NUMDIRTYREACTS 16
     92 
     93 
     94 /*
     95  * local prototypes
     96  */
     97 
     98 static void		uvmpd_scan __P((void));
     99 static boolean_t	uvmpd_scan_inactive __P((struct pglist *));
    100 static void		uvmpd_tune __P((void));
    101 
    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) {
    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 static void
    164 uvmpd_tune()
    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 	(void) spl0();
    208 	uvm_lock_pageq();
    209 	npages = uvmexp.npages;
    210 	uvmpd_tune();
    211 	uvm_unlock_pageq();
    212 
    213 	/*
    214 	 * main loop
    215 	 */
    216 
    217 	for (;;) {
    218 		simple_lock(&uvm.pagedaemon_lock);
    219 
    220 		UVMHIST_LOG(pdhist,"  <<SLEEPING>>",0,0,0,0);
    221 		UVM_UNLOCK_AND_WAIT(&uvm.pagedaemon,
    222 		    &uvm.pagedaemon_lock, FALSE, "pgdaemon", 0);
    223 		uvmexp.pdwoke++;
    224 		UVMHIST_LOG(pdhist,"  <<WOKE UP>>",0,0,0,0);
    225 
    226 		/* drain pool resources */
    227 		pool_drain(0);
    228 
    229 		/*
    230 		 * now lock page queues and recompute inactive count
    231 		 */
    232 
    233 		uvm_lock_pageq();
    234 		if (npages != uvmexp.npages) {	/* check for new pages? */
    235 			npages = uvmexp.npages;
    236 			uvmpd_tune();
    237 		}
    238 
    239 		uvmexp.inactarg = (uvmexp.active + uvmexp.inactive) / 3;
    240 		if (uvmexp.inactarg <= uvmexp.freetarg) {
    241 			uvmexp.inactarg = uvmexp.freetarg + 1;
    242 		}
    243 
    244 		UVMHIST_LOG(pdhist,"  free/ftarg=%d/%d, inact/itarg=%d/%d",
    245 		    uvmexp.free, uvmexp.freetarg, uvmexp.inactive,
    246 		    uvmexp.inactarg);
    247 
    248 		/*
    249 		 * scan if needed
    250 		 */
    251 
    252 		if (uvmexp.free + uvmexp.paging < uvmexp.freetarg ||
    253 		    uvmexp.inactive < uvmexp.inactarg ||
    254 		    uvmexp.vnodepages >
    255 		    (uvmexp.active + uvmexp.inactive + uvmexp.wired +
    256 		     uvmexp.free) * 13 / 16) {
    257 			uvmpd_scan();
    258 		}
    259 
    260 		/*
    261 		 * if there's any free memory to be had,
    262 		 * wake up any waiters.
    263 		 */
    264 
    265 		if (uvmexp.free > uvmexp.reserve_kernel ||
    266 		    uvmexp.paging == 0) {
    267 			wakeup(&uvmexp.free);
    268 		}
    269 
    270 		/*
    271 		 * scan done.  unlock page queues (the only lock we are holding)
    272 		 */
    273 
    274 		uvm_unlock_pageq();
    275 	}
    276 	/*NOTREACHED*/
    277 }
    278 
    279 
    280 /*
    281  * uvm_aiodone_daemon:  main loop for the aiodone daemon.
    282  */
    283 
    284 void
    285 uvm_aiodone_daemon(void *arg)
    286 {
    287 	int s, free;
    288 	struct buf *bp, *nbp;
    289 	UVMHIST_FUNC("uvm_aiodoned"); UVMHIST_CALLED(pdhist);
    290 
    291 	for (;;) {
    292 
    293 		/*
    294 		 * carefully attempt to go to sleep (without losing "wakeups"!).
    295 		 * we need splbio because we want to make sure the aio_done list
    296 		 * is totally empty before we go to sleep.
    297 		 */
    298 
    299 		s = splbio();
    300 		simple_lock(&uvm.aiodoned_lock);
    301 		if (TAILQ_FIRST(&uvm.aio_done) == NULL) {
    302 			UVMHIST_LOG(pdhist,"  <<SLEEPING>>",0,0,0,0);
    303 			UVM_UNLOCK_AND_WAIT(&uvm.aiodoned,
    304 			    &uvm.aiodoned_lock, FALSE, "aiodoned", 0);
    305 			UVMHIST_LOG(pdhist,"  <<WOKE UP>>",0,0,0,0);
    306 
    307 			/* relock aiodoned_lock, still at splbio */
    308 			simple_lock(&uvm.aiodoned_lock);
    309 		}
    310 
    311 		/*
    312 		 * check for done aio structures
    313 		 */
    314 
    315 		bp = TAILQ_FIRST(&uvm.aio_done);
    316 		if (bp) {
    317 			TAILQ_INIT(&uvm.aio_done);
    318 		}
    319 
    320 		simple_unlock(&uvm.aiodoned_lock);
    321 		splx(s);
    322 
    323 		/*
    324 		 * process each i/o that's done.
    325 		 */
    326 
    327 		free = uvmexp.free;
    328 		while (bp != NULL) {
    329 			if (bp->b_flags & B_PDAEMON) {
    330 				uvmexp.paging -= bp->b_bufsize >> PAGE_SHIFT;
    331 			}
    332 			nbp = TAILQ_NEXT(bp, b_freelist);
    333 			(*bp->b_iodone)(bp);
    334 			bp = nbp;
    335 		}
    336 		if (free <= uvmexp.reserve_kernel) {
    337 			s = uvm_lock_fpageq();
    338 			wakeup(&uvm.pagedaemon);
    339 			uvm_unlock_fpageq(s);
    340 		} else {
    341 			simple_lock(&uvm.pagedaemon_lock);
    342 			wakeup(&uvmexp.free);
    343 			simple_unlock(&uvm.pagedaemon_lock);
    344 		}
    345 	}
    346 }
    347 
    348 
    349 
    350 /*
    351  * uvmpd_scan_inactive: scan an inactive list for pages to clean or free.
    352  *
    353  * => called with page queues locked
    354  * => we work on meeting our free target by converting inactive pages
    355  *    into free pages.
    356  * => we handle the building of swap-backed clusters
    357  * => we return TRUE if we are exiting because we met our target
    358  */
    359 
    360 static boolean_t
    361 uvmpd_scan_inactive(pglst)
    362 	struct pglist *pglst;
    363 {
    364 	boolean_t retval = FALSE;	/* assume we haven't hit target */
    365 	int s, free, result;
    366 	struct vm_page *p, *nextpg;
    367 	struct uvm_object *uobj;
    368 	struct vm_page *pps[MAXBSIZE >> PAGE_SHIFT], **ppsp;
    369 	int npages;
    370 	struct vm_page *swpps[MAXBSIZE >> PAGE_SHIFT]; 	/* XXX: see below */
    371 	int swnpages, swcpages;				/* XXX: see below */
    372 	int swslot;
    373 	struct vm_anon *anon;
    374 	boolean_t swap_backed, vnode_only;
    375 	vaddr_t start;
    376 	int dirtyreacts, vpgs;
    377 	UVMHIST_FUNC("uvmpd_scan_inactive"); UVMHIST_CALLED(pdhist);
    378 
    379 	/*
    380 	 * note: we currently keep swap-backed pages on a seperate inactive
    381 	 * list from object-backed pages.   however, merging the two lists
    382 	 * back together again hasn't been ruled out.   thus, we keep our
    383 	 * swap cluster in "swpps" rather than in pps (allows us to mix
    384 	 * clustering types in the event of a mixed inactive queue).
    385 	 */
    386 
    387 	/*
    388 	 * swslot is non-zero if we are building a swap cluster.  we want
    389 	 * to stay in the loop while we have a page to scan or we have
    390 	 * a swap-cluster to build.
    391 	 */
    392 
    393 	swslot = 0;
    394 	swnpages = swcpages = 0;
    395 	free = 0;
    396 	dirtyreacts = 0;
    397 	vnode_only = FALSE;
    398 
    399 	for (p = TAILQ_FIRST(pglst); p != NULL || swslot != 0; p = nextpg) {
    400 
    401 		/*
    402 		 * note that p can be NULL iff we have traversed the whole
    403 		 * list and need to do one final swap-backed clustered pageout.
    404 		 */
    405 
    406 		uobj = NULL;
    407 		anon = NULL;
    408 
    409 		if (p) {
    410 
    411 			/*
    412 			 * update our copy of "free" and see if we've met
    413 			 * our target
    414 			 */
    415 
    416 			s = uvm_lock_fpageq();
    417 			free = uvmexp.free;
    418 			uvm_unlock_fpageq(s);
    419 
    420 			/* XXXUBC */
    421 			vpgs = uvmexp.vnodepages -
    422 				(uvmexp.active + uvmexp.inactive +
    423 				 uvmexp.wired + uvmexp.free) * 13 / 16;
    424 
    425 			if (free + uvmexp.paging >= uvmexp.freetarg << 2 ||
    426 			    vpgs > 0 || dirtyreacts == UVMPD_NUMDIRTYREACTS) {
    427 				if (vpgs <= 0) {
    428 					UVMHIST_LOG(pdhist,"  met free target: "
    429 						    "exit loop", 0, 0, 0, 0);
    430 					retval = TRUE;
    431 
    432 					if (swslot == 0)
    433 						/* exit now if no
    434                                                    swap-i/o pending */
    435 						break;
    436 
    437 					/* set p to null to signal final
    438                                            swap i/o */
    439 					p = NULL;
    440 				} else {
    441 					vnode_only = TRUE;
    442 				}
    443 			}
    444 		}
    445 
    446 		if (p) {	/* if (we have a new page to consider) */
    447 			/*
    448 			 * we are below target and have a new page to consider.
    449 			 */
    450 			uvmexp.pdscans++;
    451 			nextpg = TAILQ_NEXT(p, pageq);
    452 
    453 			/*
    454 			 * move referenced pages back to active queue and
    455 			 * skip to next page (unlikely to happen since
    456 			 * inactive pages shouldn't have any valid mappings
    457 			 * and we cleared reference before deactivating).
    458 			 */
    459 			if (pmap_is_referenced(p)) {
    460 				uvm_pageactivate(p);
    461 				uvmexp.pdreact++;
    462 				continue;
    463 			}
    464 
    465 			/*
    466 			 * first we attempt to lock the object that this page
    467 			 * belongs to.  if our attempt fails we skip on to
    468 			 * the next page (no harm done).  it is important to
    469 			 * "try" locking the object as we are locking in the
    470 			 * wrong order (pageq -> object) and we don't want to
    471 			 * deadlock.
    472 			 *
    473 			 * the only time we expect to see an ownerless page
    474 			 * (i.e. a page with no uobject and !PQ_ANON) is if an
    475 			 * anon has loaned a page from a uvm_object and the
    476 			 * uvm_object has dropped the ownership.  in that
    477 			 * case, the anon can "take over" the loaned page
    478 			 * and make it its own.
    479 			 */
    480 
    481 			/* is page part of an anon or ownerless ? */
    482 			if ((p->pqflags & PQ_ANON) || p->uobject == NULL) {
    483 				if (vnode_only) {
    484 					uvm_pageactivate(p);
    485 					continue;
    486 				}
    487 				anon = p->uanon;
    488 				KASSERT(anon != NULL);
    489 				if (!simple_lock_try(&anon->an_lock))
    490 					/* lock failed, skip this page */
    491 					continue;
    492 
    493 				/*
    494 				 * if the page is ownerless, claim it in the
    495 				 * name of "anon"!
    496 				 */
    497 
    498 				if ((p->pqflags & PQ_ANON) == 0) {
    499 					KASSERT(p->loan_count > 0);
    500 					p->loan_count--;
    501 					p->pqflags |= PQ_ANON;
    502 					/* anon now owns it */
    503 				}
    504 				if (p->flags & PG_BUSY) {
    505 					simple_unlock(&anon->an_lock);
    506 					uvmexp.pdbusy++;
    507 					/* someone else owns page, skip it */
    508 					continue;
    509 				}
    510 				uvmexp.pdanscan++;
    511 			} else {
    512 				uobj = p->uobject;
    513 				KASSERT(uobj != NULL);
    514 				if (vnode_only &&
    515 				    UVM_OBJ_IS_VNODE(uobj) == 0) {
    516 					uvm_pageactivate(p);
    517 					continue;
    518 				}
    519 				if (!simple_lock_try(&uobj->vmobjlock))
    520 					/* lock failed, skip this page */
    521 					continue;
    522 
    523 				if (p->flags & PG_BUSY) {
    524 					simple_unlock(&uobj->vmobjlock);
    525 					uvmexp.pdbusy++;
    526 					/* someone else owns page, skip it */
    527 					continue;
    528 				}
    529 				uvmexp.pdobscan++;
    530 			}
    531 
    532 			/*
    533 			 * we now have the object and the page queues locked.
    534 			 * the page is not busy.  remove all the permissions
    535 			 * from the page so we can sync the modified info
    536 			 * without any race conditions.  if the page is clean
    537 			 * we can free it now and continue.
    538 			 */
    539 
    540 			pmap_page_protect(p, VM_PROT_NONE);
    541 			if ((p->flags & PG_CLEAN) != 0 && pmap_is_modified(p))
    542 				p->flags &= ~PG_CLEAN;
    543 
    544 			if (p->flags & PG_CLEAN) {
    545 				if (p->pqflags & PQ_SWAPBACKED) {
    546 					/* this page now lives only in swap */
    547 					simple_lock(&uvm.swap_data_lock);
    548 					uvmexp.swpgonly++;
    549 					simple_unlock(&uvm.swap_data_lock);
    550 				}
    551 
    552 				uvm_pagefree(p);
    553 				uvmexp.pdfreed++;
    554 
    555 				if (anon) {
    556 
    557 					/*
    558 					 * an anonymous page can only be clean
    559 					 * if it has backing store assigned.
    560 					 */
    561 
    562 					KASSERT(anon->an_swslot != 0);
    563 
    564 					/* remove from object */
    565 					anon->u.an_page = NULL;
    566 					simple_unlock(&anon->an_lock);
    567 				} else {
    568 					/* pagefree has already removed the
    569 					 * page from the object */
    570 					simple_unlock(&uobj->vmobjlock);
    571 				}
    572 				continue;
    573 			}
    574 
    575 			/*
    576 			 * this page is dirty, skip it if we'll have met our
    577 			 * free target when all the current pageouts complete.
    578 			 */
    579 
    580 			if (free + uvmexp.paging > uvmexp.freetarg << 2 &&
    581 			    !vnode_only) {
    582 				if (anon) {
    583 					simple_unlock(&anon->an_lock);
    584 				} else {
    585 					simple_unlock(&uobj->vmobjlock);
    586 				}
    587 				continue;
    588 			}
    589 
    590 			/*
    591 			 * this page is dirty, but we can't page it out
    592 			 * since all pages in swap are only in swap.
    593 			 * reactivate it so that we eventually cycle
    594 			 * all pages thru the inactive queue.
    595 			 */
    596 
    597 			KASSERT(uvmexp.swpgonly <= uvmexp.swpages);
    598 			if ((p->pqflags & PQ_SWAPBACKED) &&
    599 			    uvmexp.swpgonly == uvmexp.swpages) {
    600 				dirtyreacts++;
    601 				uvm_pageactivate(p);
    602 				if (anon) {
    603 					simple_unlock(&anon->an_lock);
    604 				} else {
    605 					simple_unlock(&uobj->vmobjlock);
    606 				}
    607 				continue;
    608 			}
    609 
    610 			/*
    611 			 * if the page is swap-backed and dirty and swap space
    612 			 * is full, free any swap allocated to the page
    613 			 * so that other pages can be paged out.
    614 			 */
    615 
    616 			KASSERT(uvmexp.swpginuse <= uvmexp.swpages);
    617 			if ((p->pqflags & PQ_SWAPBACKED) &&
    618 			    uvmexp.swpginuse == uvmexp.swpages) {
    619 
    620 				if ((p->pqflags & PQ_ANON) &&
    621 				    p->uanon->an_swslot) {
    622 					uvm_swap_free(p->uanon->an_swslot, 1);
    623 					p->uanon->an_swslot = 0;
    624 				}
    625 				if (p->pqflags & PQ_AOBJ) {
    626 					uao_dropswap(p->uobject,
    627 						     p->offset >> PAGE_SHIFT);
    628 				}
    629 			}
    630 
    631 			/*
    632 			 * the page we are looking at is dirty.   we must
    633 			 * clean it before it can be freed.  to do this we
    634 			 * first mark the page busy so that no one else will
    635 			 * touch the page.
    636 			 */
    637 
    638 			swap_backed = ((p->pqflags & PQ_SWAPBACKED) != 0);
    639 			p->flags |= PG_BUSY;		/* now we own it */
    640 			UVM_PAGE_OWN(p, "scan_inactive");
    641 			uvmexp.pgswapout++;
    642 
    643 			/*
    644 			 * for swap-backed pages we need to (re)allocate
    645 			 * swap space.
    646 			 */
    647 
    648 			if (swap_backed) {
    649 
    650 				/*
    651 				 * free old swap slot (if any)
    652 				 */
    653 
    654 				if (anon) {
    655 					if (anon->an_swslot) {
    656 						uvm_swap_free(anon->an_swslot,
    657 						    1);
    658 						anon->an_swslot = 0;
    659 					}
    660 				} else {
    661 					uao_dropswap(uobj,
    662 						     p->offset >> PAGE_SHIFT);
    663 				}
    664 
    665 				/*
    666 				 * start new cluster (if necessary)
    667 				 */
    668 
    669 				if (swslot == 0) {
    670 					swnpages = MAXBSIZE >> PAGE_SHIFT;
    671 					swslot = uvm_swap_alloc(&swnpages,
    672 					    TRUE);
    673 					if (swslot == 0) {
    674 						/* no swap?  give up! */
    675 						p->flags &= ~PG_BUSY;
    676 						UVM_PAGE_OWN(p, NULL);
    677 						if (anon)
    678 							simple_unlock(
    679 							    &anon->an_lock);
    680 						else
    681 							simple_unlock(
    682 							    &uobj->vmobjlock);
    683 						continue;
    684 					}
    685 					swcpages = 0;	/* cluster is empty */
    686 				}
    687 
    688 				/*
    689 				 * add block to cluster
    690 				 */
    691 
    692 				swpps[swcpages] = p;
    693 				if (anon)
    694 					anon->an_swslot = swslot + swcpages;
    695 				else
    696 					uao_set_swslot(uobj,
    697 					    p->offset >> PAGE_SHIFT,
    698 					    swslot + swcpages);
    699 				swcpages++;
    700 			}
    701 		} else {
    702 
    703 			/* if p == NULL we must be doing a last swap i/o */
    704 			swap_backed = TRUE;
    705 		}
    706 
    707 		/*
    708 		 * now consider doing the pageout.
    709 		 *
    710 		 * for swap-backed pages, we do the pageout if we have either
    711 		 * filled the cluster (in which case (swnpages == swcpages) or
    712 		 * run out of pages (p == NULL).
    713 		 *
    714 		 * for object pages, we always do the pageout.
    715 		 */
    716 
    717 		if (swap_backed) {
    718 			if (p) {	/* if we just added a page to cluster */
    719 				if (anon)
    720 					simple_unlock(&anon->an_lock);
    721 				else
    722 					simple_unlock(&uobj->vmobjlock);
    723 
    724 				/* cluster not full yet? */
    725 				if (swcpages < swnpages)
    726 					continue;
    727 			}
    728 
    729 			/* starting I/O now... set up for it */
    730 			npages = swcpages;
    731 			ppsp = swpps;
    732 			/* for swap-backed pages only */
    733 			start = (vaddr_t) swslot;
    734 
    735 			/* if this is final pageout we could have a few
    736 			 * extra swap blocks */
    737 			if (swcpages < swnpages) {
    738 				uvm_swap_free(swslot + swcpages,
    739 				    (swnpages - swcpages));
    740 			}
    741 		} else {
    742 			/* normal object pageout */
    743 			ppsp = pps;
    744 			npages = sizeof(pps) / sizeof(struct vm_page *);
    745 			/* not looked at because PGO_ALLPAGES is set */
    746 			start = 0;
    747 		}
    748 
    749 		/*
    750 		 * now do the pageout.
    751 		 *
    752 		 * for swap_backed pages we have already built the cluster.
    753 		 * for !swap_backed pages, uvm_pager_put will call the object's
    754 		 * "make put cluster" function to build a cluster on our behalf.
    755 		 *
    756 		 * we pass the PGO_PDFREECLUST flag to uvm_pager_put to instruct
    757 		 * it to free the cluster pages for us on a successful I/O (it
    758 		 * always does this for un-successful I/O requests).  this
    759 		 * allows us to do clustered pageout without having to deal
    760 		 * with cluster pages at this level.
    761 		 *
    762 		 * note locking semantics of uvm_pager_put with PGO_PDFREECLUST:
    763 		 *  IN: locked: uobj (if !swap_backed), page queues
    764 		 * OUT: locked: uobj (if !swap_backed && result !=VM_PAGER_PEND)
    765 		 *     !locked: pageqs, uobj (if swap_backed || VM_PAGER_PEND)
    766 		 *
    767 		 * [the bit about VM_PAGER_PEND saves us one lock-unlock pair]
    768 		 */
    769 
    770 		/* locked: uobj (if !swap_backed), page queues */
    771 		uvmexp.pdpageouts++;
    772 		result = uvm_pager_put(swap_backed ? NULL : uobj, p,
    773 		    &ppsp, &npages, PGO_ALLPAGES|PGO_PDFREECLUST, start, 0);
    774 		/* locked: uobj (if !swap_backed && result != PEND) */
    775 		/* unlocked: pageqs, object (if swap_backed ||result == PEND) */
    776 
    777 		/*
    778 		 * if we did i/o to swap, zero swslot to indicate that we are
    779 		 * no longer building a swap-backed cluster.
    780 		 */
    781 
    782 		if (swap_backed)
    783 			swslot = 0;		/* done with this cluster */
    784 
    785 		/*
    786 		 * first, we check for VM_PAGER_PEND which means that the
    787 		 * async I/O is in progress and the async I/O done routine
    788 		 * will clean up after us.   in this case we move on to the
    789 		 * next page.
    790 		 *
    791 		 * there is a very remote chance that the pending async i/o can
    792 		 * finish _before_ we get here.   if that happens, our page "p"
    793 		 * may no longer be on the inactive queue.   so we verify this
    794 		 * when determining the next page (starting over at the head if
    795 		 * we've lost our inactive page).
    796 		 */
    797 
    798 		if (result == VM_PAGER_PEND) {
    799 			uvmexp.paging += npages;
    800 			uvm_lock_pageq();
    801 			uvmexp.pdpending++;
    802 			if (p) {
    803 				if (p->pqflags & PQ_INACTIVE)
    804 					nextpg = TAILQ_NEXT(p, pageq);
    805 				else
    806 					nextpg = TAILQ_FIRST(pglst);
    807 			} else {
    808 				nextpg = NULL;
    809 			}
    810 			continue;
    811 		}
    812 
    813 		if (result == VM_PAGER_ERROR &&
    814 		    curproc == uvm.pagedaemon_proc) {
    815 			uvm_lock_pageq();
    816 			nextpg = TAILQ_NEXT(p, pageq);
    817 			uvm_pageactivate(p);
    818 			continue;
    819 		}
    820 
    821 		/*
    822 		 * clean up "p" if we have one
    823 		 */
    824 
    825 		if (p) {
    826 			/*
    827 			 * the I/O request to "p" is done and uvm_pager_put
    828 			 * has freed any cluster pages it may have allocated
    829 			 * during I/O.  all that is left for us to do is
    830 			 * clean up page "p" (which is still PG_BUSY).
    831 			 *
    832 			 * our result could be one of the following:
    833 			 *   VM_PAGER_OK: successful pageout
    834 			 *
    835 			 *   VM_PAGER_AGAIN: tmp resource shortage, we skip
    836 			 *     to next page
    837 			 *   VM_PAGER_{FAIL,ERROR,BAD}: an error.   we
    838 			 *     "reactivate" page to get it out of the way (it
    839 			 *     will eventually drift back into the inactive
    840 			 *     queue for a retry).
    841 			 *   VM_PAGER_UNLOCK: should never see this as it is
    842 			 *     only valid for "get" operations
    843 			 */
    844 
    845 			/* relock p's object: page queues not lock yet, so
    846 			 * no need for "try" */
    847 
    848 			/* !swap_backed case: already locked... */
    849 			if (swap_backed) {
    850 				if (anon)
    851 					simple_lock(&anon->an_lock);
    852 				else
    853 					simple_lock(&uobj->vmobjlock);
    854 			}
    855 
    856 			/* handle PG_WANTED now */
    857 			if (p->flags & PG_WANTED)
    858 				/* still holding object lock */
    859 				wakeup(p);
    860 
    861 			p->flags &= ~(PG_BUSY|PG_WANTED);
    862 			UVM_PAGE_OWN(p, NULL);
    863 
    864 			/* released during I/O? */
    865 			if (p->flags & PG_RELEASED) {
    866 				if (anon) {
    867 					/* remove page so we can get nextpg */
    868 					anon->u.an_page = NULL;
    869 
    870 					simple_unlock(&anon->an_lock);
    871 					uvm_anfree(anon);	/* kills anon */
    872 					pmap_page_protect(p, VM_PROT_NONE);
    873 					anon = NULL;
    874 					uvm_lock_pageq();
    875 					nextpg = TAILQ_NEXT(p, pageq);
    876 					/* free released page */
    877 					uvm_pagefree(p);
    878 
    879 				} else {
    880 
    881 					/*
    882 					 * pgo_releasepg nukes the page and
    883 					 * gets "nextpg" for us.  it returns
    884 					 * with the page queues locked (when
    885 					 * given nextpg ptr).
    886 					 */
    887 
    888 					if (!uobj->pgops->pgo_releasepg(p,
    889 					    &nextpg))
    890 						/* uobj died after release */
    891 						uobj = NULL;
    892 
    893 					/*
    894 					 * lock page queues here so that they're
    895 					 * always locked at the end of the loop.
    896 					 */
    897 
    898 					uvm_lock_pageq();
    899 				}
    900 			} else {	/* page was not released during I/O */
    901 				uvm_lock_pageq();
    902 				nextpg = TAILQ_NEXT(p, pageq);
    903 				if (result != VM_PAGER_OK) {
    904 					/* pageout was a failure... */
    905 					if (result != VM_PAGER_AGAIN)
    906 						uvm_pageactivate(p);
    907 					pmap_clear_reference(p);
    908 					/* XXXCDC: if (swap_backed) FREE p's
    909 					 * swap block? */
    910 				} else {
    911 					/* pageout was a success... */
    912 					pmap_clear_reference(p);
    913 					pmap_clear_modify(p);
    914 					p->flags |= PG_CLEAN;
    915 				}
    916 			}
    917 
    918 			/*
    919 			 * drop object lock (if there is an object left).   do
    920 			 * a safety check of nextpg to make sure it is on the
    921 			 * inactive queue (it should be since PG_BUSY pages on
    922 			 * the inactive queue can't be re-queued [note: not
    923 			 * true for active queue]).
    924 			 */
    925 
    926 			if (anon)
    927 				simple_unlock(&anon->an_lock);
    928 			else if (uobj)
    929 				simple_unlock(&uobj->vmobjlock);
    930 
    931 		} else {
    932 
    933 			/*
    934 			 * if p is null in this loop, make sure it stays null
    935 			 * in the next loop.
    936 			 */
    937 
    938 			nextpg = NULL;
    939 
    940 			/*
    941 			 * lock page queues here just so they're always locked
    942 			 * at the end of the loop.
    943 			 */
    944 
    945 			uvm_lock_pageq();
    946 		}
    947 
    948 		if (nextpg && (nextpg->pqflags & PQ_INACTIVE) == 0) {
    949 			nextpg = TAILQ_FIRST(pglst);	/* reload! */
    950 		}
    951 	}
    952 	return (retval);
    953 }
    954 
    955 /*
    956  * uvmpd_scan: scan the page queues and attempt to meet our targets.
    957  *
    958  * => called with pageq's locked
    959  */
    960 
    961 void
    962 uvmpd_scan()
    963 {
    964 	int s, free, inactive_shortage, swap_shortage, pages_freed;
    965 	struct vm_page *p, *nextpg;
    966 	struct uvm_object *uobj;
    967 	boolean_t got_it;
    968 	UVMHIST_FUNC("uvmpd_scan"); UVMHIST_CALLED(pdhist);
    969 
    970 	uvmexp.pdrevs++;		/* counter */
    971 	uobj = NULL;
    972 
    973 	/*
    974 	 * get current "free" page count
    975 	 */
    976 	s = uvm_lock_fpageq();
    977 	free = uvmexp.free;
    978 	uvm_unlock_fpageq(s);
    979 
    980 #ifndef __SWAP_BROKEN
    981 	/*
    982 	 * swap out some processes if we are below our free target.
    983 	 * we need to unlock the page queues for this.
    984 	 */
    985 	if (free < uvmexp.freetarg) {
    986 		uvmexp.pdswout++;
    987 		UVMHIST_LOG(pdhist,"  free %d < target %d: swapout", free,
    988 		    uvmexp.freetarg, 0, 0);
    989 		uvm_unlock_pageq();
    990 		uvm_swapout_threads();
    991 		uvm_lock_pageq();
    992 
    993 	}
    994 #endif
    995 
    996 	/*
    997 	 * now we want to work on meeting our targets.   first we work on our
    998 	 * free target by converting inactive pages into free pages.  then
    999 	 * we work on meeting our inactive target by converting active pages
   1000 	 * to inactive ones.
   1001 	 */
   1002 
   1003 	UVMHIST_LOG(pdhist, "  starting 'free' loop",0,0,0,0);
   1004 
   1005 	/*
   1006 	 * alternate starting queue between swap and object based on the
   1007 	 * low bit of uvmexp.pdrevs (which we bump by one each call).
   1008 	 */
   1009 
   1010 	got_it = FALSE;
   1011 	pages_freed = uvmexp.pdfreed;
   1012 	if ((uvmexp.pdrevs & 1) != 0 && uvmexp.nswapdev != 0)
   1013 		got_it = uvmpd_scan_inactive(&uvm.page_inactive_swp);
   1014 	if (!got_it)
   1015 		got_it = uvmpd_scan_inactive(&uvm.page_inactive_obj);
   1016 	if (!got_it && (uvmexp.pdrevs & 1) == 0 && uvmexp.nswapdev != 0)
   1017 		(void) uvmpd_scan_inactive(&uvm.page_inactive_swp);
   1018 	pages_freed = uvmexp.pdfreed - pages_freed;
   1019 
   1020 	/*
   1021 	 * we have done the scan to get free pages.   now we work on meeting
   1022 	 * our inactive target.
   1023 	 */
   1024 
   1025 	inactive_shortage = uvmexp.inactarg - uvmexp.inactive;
   1026 
   1027 	/*
   1028 	 * detect if we're not going to be able to page anything out
   1029 	 * until we free some swap resources from active pages.
   1030 	 */
   1031 
   1032 	swap_shortage = 0;
   1033 	if (uvmexp.free < uvmexp.freetarg &&
   1034 	    uvmexp.swpginuse == uvmexp.swpages &&
   1035 	    uvmexp.swpgonly < uvmexp.swpages &&
   1036 	    pages_freed == 0) {
   1037 		swap_shortage = uvmexp.freetarg - uvmexp.free;
   1038 	}
   1039 
   1040 	UVMHIST_LOG(pdhist, "  loop 2: inactive_shortage=%d swap_shortage=%d",
   1041 		    inactive_shortage, swap_shortage,0,0);
   1042 	for (p = TAILQ_FIRST(&uvm.page_active);
   1043 	     p != NULL && (inactive_shortage > 0 || swap_shortage > 0);
   1044 	     p = nextpg) {
   1045 		nextpg = TAILQ_NEXT(p, pageq);
   1046 		if (p->flags & PG_BUSY)
   1047 			continue;	/* quick check before trying to lock */
   1048 
   1049 		/*
   1050 		 * lock the page's owner.
   1051 		 */
   1052 		/* is page anon owned or ownerless? */
   1053 		if ((p->pqflags & PQ_ANON) || p->uobject == NULL) {
   1054 			KASSERT(p->uanon != NULL);
   1055 			if (!simple_lock_try(&p->uanon->an_lock))
   1056 				continue;
   1057 
   1058 			/* take over the page? */
   1059 			if ((p->pqflags & PQ_ANON) == 0) {
   1060 				KASSERT(p->loan_count > 0);
   1061 				p->loan_count--;
   1062 				p->pqflags |= PQ_ANON;
   1063 			}
   1064 		} else {
   1065 			if (!simple_lock_try(&p->uobject->vmobjlock))
   1066 				continue;
   1067 		}
   1068 
   1069 		/*
   1070 		 * skip this page if it's busy.
   1071 		 */
   1072 
   1073 		if ((p->flags & PG_BUSY) != 0) {
   1074 			if (p->pqflags & PQ_ANON)
   1075 				simple_unlock(&p->uanon->an_lock);
   1076 			else
   1077 				simple_unlock(&p->uobject->vmobjlock);
   1078 			continue;
   1079 		}
   1080 
   1081 		/*
   1082 		 * if there's a shortage of swap, free any swap allocated
   1083 		 * to this page so that other pages can be paged out.
   1084 		 */
   1085 
   1086 		if (swap_shortage > 0) {
   1087 			if ((p->pqflags & PQ_ANON) && p->uanon->an_swslot) {
   1088 				uvm_swap_free(p->uanon->an_swslot, 1);
   1089 				p->uanon->an_swslot = 0;
   1090 				p->flags &= ~PG_CLEAN;
   1091 				swap_shortage--;
   1092 			}
   1093 			if (p->pqflags & PQ_AOBJ) {
   1094 				int slot = uao_set_swslot(p->uobject,
   1095 					p->offset >> PAGE_SHIFT, 0);
   1096 				if (slot) {
   1097 					uvm_swap_free(slot, 1);
   1098 					p->flags &= ~PG_CLEAN;
   1099 					swap_shortage--;
   1100 				}
   1101 			}
   1102 		}
   1103 
   1104 		/*
   1105 		 * If the page has not been referenced since the
   1106 		 * last scan, deactivate the page if there is a
   1107 		 * shortage of inactive pages.
   1108 		 */
   1109 
   1110 		if (inactive_shortage > 0 &&
   1111 		    pmap_clear_reference(p) == FALSE) {
   1112 			/* no need to check wire_count as pg is "active" */
   1113 			uvm_pagedeactivate(p);
   1114 			uvmexp.pddeact++;
   1115 			inactive_shortage--;
   1116 		}
   1117 		if (p->pqflags & PQ_ANON)
   1118 			simple_unlock(&p->uanon->an_lock);
   1119 		else
   1120 			simple_unlock(&p->uobject->vmobjlock);
   1121 	}
   1122 }
   1123