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