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