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