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