xref: /netbsd-src/sys/uvm/uvm_pdaemon.c (revision e5548b402ae4c44fb816de42c7bba9581ce23ef5)
1 /*	$NetBSD: uvm_pdaemon.c,v 1.69 2005/11/29 15:45:28 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.69 2005/11/29 15:45:28 yamt Exp $");
75 
76 #include "opt_uvmhist.h"
77 #include "opt_readahead.h"
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 #include <sys/buf.h>
85 #include <sys/vnode.h>
86 
87 #include <uvm/uvm.h>
88 
89 /*
90  * UVMPD_NUMDIRTYREACTS is how many dirty pages the pagedaemon will reactivate
91  * in a pass thru the inactive list when swap is full.  the value should be
92  * "small"... if it's too large we'll cycle the active pages thru the inactive
93  * queue too quickly to for them to be referenced and avoid being freed.
94  */
95 
96 #define UVMPD_NUMDIRTYREACTS 16
97 
98 
99 /*
100  * local prototypes
101  */
102 
103 static void	uvmpd_scan(void);
104 static void	uvmpd_scan_inactive(struct pglist *);
105 static void	uvmpd_tune(void);
106 
107 /*
108  * XXX hack to avoid hangs when large processes fork.
109  */
110 int uvm_extrapages;
111 
112 /*
113  * uvm_wait: wait (sleep) for the page daemon to free some pages
114  *
115  * => should be called with all locks released
116  * => should _not_ be called by the page daemon (to avoid deadlock)
117  */
118 
119 void
120 uvm_wait(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 static 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 static void
384 uvmpd_scan_inactive(struct pglist *pglst)
385 {
386 	struct vm_page *p, *nextpg = NULL; /* Quell compiler warning */
387 	struct uvm_object *uobj;
388 	struct vm_anon *anon;
389 #if defined(VMSWAP)
390 	struct vm_page *swpps[round_page(MAXPHYS) >> PAGE_SHIFT];
391 	int error;
392 	int result;
393 #endif /* defined(VMSWAP) */
394 	struct simplelock *slock;
395 	int swnpages, swcpages;
396 	int swslot;
397 	int dirtyreacts, t;
398 	boolean_t anonunder, fileunder, execunder;
399 	boolean_t anonover, fileover, execover;
400 	boolean_t anonreact, filereact, execreact;
401 	UVMHIST_FUNC("uvmpd_scan_inactive"); UVMHIST_CALLED(pdhist);
402 
403 	/*
404 	 * swslot is non-zero if we are building a swap cluster.  we want
405 	 * to stay in the loop while we have a page to scan or we have
406 	 * a swap-cluster to build.
407 	 */
408 
409 	swslot = 0;
410 	swnpages = swcpages = 0;
411 	dirtyreacts = 0;
412 
413 	/*
414 	 * decide which types of pages we want to reactivate instead of freeing
415 	 * to keep usage within the minimum and maximum usage limits.
416 	 */
417 
418 	t = uvmexp.active + uvmexp.inactive + uvmexp.free;
419 	anonunder = (uvmexp.anonpages <= (t * uvmexp.anonmin) >> 8);
420 	fileunder = (uvmexp.filepages <= (t * uvmexp.filemin) >> 8);
421 	execunder = (uvmexp.execpages <= (t * uvmexp.execmin) >> 8);
422 	anonover = uvmexp.anonpages > ((t * uvmexp.anonmax) >> 8);
423 	fileover = uvmexp.filepages > ((t * uvmexp.filemax) >> 8);
424 	execover = uvmexp.execpages > ((t * uvmexp.execmax) >> 8);
425 	anonreact = anonunder || (!anonover && (fileover || execover));
426 	filereact = fileunder || (!fileover && (anonover || execover));
427 	execreact = execunder || (!execover && (anonover || fileover));
428 	if (filereact && execreact && (anonreact || uvm_swapisfull())) {
429 		anonreact = filereact = execreact = FALSE;
430 	}
431 #if !defined(VMSWAP)
432 	/*
433 	 * XXX no point to put swap-backed pages on the page queue.
434 	 */
435 
436 	anonreact = TRUE;
437 #endif /* !defined(VMSWAP) */
438 	for (p = TAILQ_FIRST(pglst); p != NULL || swslot != 0; p = nextpg) {
439 		uobj = NULL;
440 		anon = NULL;
441 		if (p) {
442 
443 			/*
444 			 * see if we've met the free target.
445 			 */
446 
447 			if (uvmexp.free + uvmexp.paging >=
448 			    uvmexp.freetarg << 2 ||
449 			    dirtyreacts == UVMPD_NUMDIRTYREACTS) {
450 				UVMHIST_LOG(pdhist,"  met free target: "
451 					    "exit loop", 0, 0, 0, 0);
452 
453 				if (swslot == 0) {
454 					/* exit now if no swap-i/o pending */
455 					break;
456 				}
457 
458 				/* set p to null to signal final swap i/o */
459 				p = NULL;
460 				nextpg = NULL;
461 			}
462 		}
463 		if (p) {	/* if (we have a new page to consider) */
464 
465 			/*
466 			 * we are below target and have a new page to consider.
467 			 */
468 
469 			uvmexp.pdscans++;
470 			nextpg = TAILQ_NEXT(p, pageq);
471 
472 			/*
473 			 * move referenced pages back to active queue and
474 			 * skip to next page.
475 			 */
476 
477 			if (pmap_clear_reference(p)) {
478 				uvm_pageactivate(p);
479 				uvmexp.pdreact++;
480 				continue;
481 			}
482 			anon = p->uanon;
483 			uobj = p->uobject;
484 
485 			/*
486 			 * enforce the minimum thresholds on different
487 			 * types of memory usage.  if reusing the current
488 			 * page would reduce that type of usage below its
489 			 * minimum, reactivate the page instead and move
490 			 * on to the next page.
491 			 */
492 
493 			if (uobj && UVM_OBJ_IS_VTEXT(uobj) && execreact) {
494 				uvm_pageactivate(p);
495 				uvmexp.pdreexec++;
496 				continue;
497 			}
498 			if (uobj && UVM_OBJ_IS_VNODE(uobj) &&
499 			    !UVM_OBJ_IS_VTEXT(uobj) && filereact) {
500 				uvm_pageactivate(p);
501 				uvmexp.pdrefile++;
502 				continue;
503 			}
504 			if ((anon || UVM_OBJ_IS_AOBJ(uobj)) && anonreact) {
505 				uvm_pageactivate(p);
506 				uvmexp.pdreanon++;
507 				continue;
508 			}
509 
510 			/*
511 			 * first we attempt to lock the object that this page
512 			 * belongs to.  if our attempt fails we skip on to
513 			 * the next page (no harm done).  it is important to
514 			 * "try" locking the object as we are locking in the
515 			 * wrong order (pageq -> object) and we don't want to
516 			 * deadlock.
517 			 *
518 			 * the only time we expect to see an ownerless page
519 			 * (i.e. a page with no uobject and !PQ_ANON) is if an
520 			 * anon has loaned a page from a uvm_object and the
521 			 * uvm_object has dropped the ownership.  in that
522 			 * case, the anon can "take over" the loaned page
523 			 * and make it its own.
524 			 */
525 
526 			/* does the page belong to an object? */
527 			if (uobj != NULL) {
528 				slock = &uobj->vmobjlock;
529 				if (!simple_lock_try(slock)) {
530 					continue;
531 				}
532 				if (p->flags & PG_BUSY) {
533 					simple_unlock(slock);
534 					uvmexp.pdbusy++;
535 					continue;
536 				}
537 				uvmexp.pdobscan++;
538 			} else {
539 #if defined(VMSWAP)
540 				KASSERT(anon != NULL);
541 				slock = &anon->an_lock;
542 				if (!simple_lock_try(slock)) {
543 					continue;
544 				}
545 
546 				/*
547 				 * set PQ_ANON if it isn't set already.
548 				 */
549 
550 				if ((p->pqflags & PQ_ANON) == 0) {
551 					KASSERT(p->loan_count > 0);
552 					p->loan_count--;
553 					p->pqflags |= PQ_ANON;
554 					/* anon now owns it */
555 				}
556 				if (p->flags & PG_BUSY) {
557 					simple_unlock(slock);
558 					uvmexp.pdbusy++;
559 					continue;
560 				}
561 				uvmexp.pdanscan++;
562 #else /* defined(VMSWAP) */
563 				panic("%s: anon", __func__);
564 #endif /* defined(VMSWAP) */
565 			}
566 
567 
568 			/*
569 			 * we now have the object and the page queues locked.
570 			 * if the page is not swap-backed, call the object's
571 			 * pager to flush and free the page.
572 			 */
573 
574 #if defined(READAHEAD_STATS)
575 			if ((p->flags & PG_SPECULATIVE) != 0) {
576 				p->flags &= ~PG_SPECULATIVE;
577 				uvm_ra_miss.ev_count++;
578 			}
579 #endif /* defined(READAHEAD_STATS) */
580 
581 			if ((p->pqflags & PQ_SWAPBACKED) == 0) {
582 				uvm_unlock_pageq();
583 				(void) (uobj->pgops->pgo_put)(uobj, p->offset,
584 				    p->offset + PAGE_SIZE,
585 				    PGO_CLEANIT|PGO_FREE);
586 				uvm_lock_pageq();
587 				if (nextpg &&
588 				    (nextpg->pqflags & PQ_INACTIVE) == 0) {
589 					nextpg = TAILQ_FIRST(pglst);
590 				}
591 				continue;
592 			}
593 
594 #if defined(VMSWAP)
595 			/*
596 			 * the page is swap-backed.  remove all the permissions
597 			 * from the page so we can sync the modified info
598 			 * without any race conditions.  if the page is clean
599 			 * we can free it now and continue.
600 			 */
601 
602 			pmap_page_protect(p, VM_PROT_NONE);
603 			if ((p->flags & PG_CLEAN) && pmap_clear_modify(p)) {
604 				p->flags &= ~(PG_CLEAN);
605 			}
606 			if (p->flags & PG_CLEAN) {
607 				int slot;
608 				int pageidx;
609 
610 				pageidx = p->offset >> PAGE_SHIFT;
611 				uvm_pagefree(p);
612 				uvmexp.pdfreed++;
613 
614 				/*
615 				 * for anons, we need to remove the page
616 				 * from the anon ourselves.  for aobjs,
617 				 * pagefree did that for us.
618 				 */
619 
620 				if (anon) {
621 					KASSERT(anon->an_swslot != 0);
622 					anon->an_page = NULL;
623 					slot = anon->an_swslot;
624 				} else {
625 					slot = uao_find_swslot(uobj, pageidx);
626 				}
627 				simple_unlock(slock);
628 
629 				if (slot > 0) {
630 					/* this page is now only in swap. */
631 					simple_lock(&uvm.swap_data_lock);
632 					KASSERT(uvmexp.swpgonly <
633 						uvmexp.swpginuse);
634 					uvmexp.swpgonly++;
635 					simple_unlock(&uvm.swap_data_lock);
636 				}
637 				continue;
638 			}
639 
640 			/*
641 			 * this page is dirty, skip it if we'll have met our
642 			 * free target when all the current pageouts complete.
643 			 */
644 
645 			if (uvmexp.free + uvmexp.paging >
646 			    uvmexp.freetarg << 2) {
647 				simple_unlock(slock);
648 				continue;
649 			}
650 
651 			/*
652 			 * free any swap space allocated to the page since
653 			 * we'll have to write it again with its new data.
654 			 */
655 
656 			if ((p->pqflags & PQ_ANON) && anon->an_swslot) {
657 				uvm_swap_free(anon->an_swslot, 1);
658 				anon->an_swslot = 0;
659 			} else if (p->pqflags & PQ_AOBJ) {
660 				uao_dropswap(uobj, p->offset >> PAGE_SHIFT);
661 			}
662 
663 			/*
664 			 * if all pages in swap are only in swap,
665 			 * the swap space is full and we can't page out
666 			 * any more swap-backed pages.  reactivate this page
667 			 * so that we eventually cycle all pages through
668 			 * the inactive queue.
669 			 */
670 
671 			if (uvm_swapisfull()) {
672 				dirtyreacts++;
673 				uvm_pageactivate(p);
674 				simple_unlock(slock);
675 				continue;
676 			}
677 
678 			/*
679 			 * start new swap pageout cluster (if necessary).
680 			 */
681 
682 			if (swslot == 0) {
683 				/* Even with strange MAXPHYS, the shift
684 				   implicitly rounds down to a page. */
685 				swnpages = MAXPHYS >> PAGE_SHIFT;
686 				swslot = uvm_swap_alloc(&swnpages, TRUE);
687 				if (swslot == 0) {
688 					simple_unlock(slock);
689 					continue;
690 				}
691 				swcpages = 0;
692 			}
693 
694 			/*
695 			 * at this point, we're definitely going reuse this
696 			 * page.  mark the page busy and delayed-free.
697 			 * we should remove the page from the page queues
698 			 * so we don't ever look at it again.
699 			 * adjust counters and such.
700 			 */
701 
702 			p->flags |= PG_BUSY;
703 			UVM_PAGE_OWN(p, "scan_inactive");
704 
705 			p->flags |= PG_PAGEOUT;
706 			uvmexp.paging++;
707 			uvm_pagedequeue(p);
708 
709 			uvmexp.pgswapout++;
710 
711 			/*
712 			 * add the new page to the cluster.
713 			 */
714 
715 			if (anon) {
716 				anon->an_swslot = swslot + swcpages;
717 				simple_unlock(slock);
718 			} else {
719 				result = uao_set_swslot(uobj,
720 				    p->offset >> PAGE_SHIFT, swslot + swcpages);
721 				if (result == -1) {
722 					p->flags &= ~(PG_BUSY|PG_PAGEOUT);
723 					UVM_PAGE_OWN(p, NULL);
724 					uvmexp.paging--;
725 					uvm_pageactivate(p);
726 					simple_unlock(slock);
727 					continue;
728 				}
729 				simple_unlock(slock);
730 			}
731 			swpps[swcpages] = p;
732 			swcpages++;
733 
734 			/*
735 			 * if the cluster isn't full, look for more pages
736 			 * before starting the i/o.
737 			 */
738 
739 			if (swcpages < swnpages) {
740 				continue;
741 			}
742 #else /* defined(VMSWAP) */
743 			panic("%s: swap-backed", __func__);
744 #endif /* defined(VMSWAP) */
745 
746 		}
747 
748 #if defined(VMSWAP)
749 		/*
750 		 * if this is the final pageout we could have a few
751 		 * unused swap blocks.  if so, free them now.
752 		 */
753 
754 		if (swcpages < swnpages) {
755 			uvm_swap_free(swslot + swcpages, (swnpages - swcpages));
756 		}
757 
758 		/*
759 		 * now start the pageout.
760 		 */
761 
762 		uvm_unlock_pageq();
763 		uvmexp.pdpageouts++;
764 		error = uvm_swap_put(swslot, swpps, swcpages, 0);
765 		KASSERT(error == 0);
766 		uvm_lock_pageq();
767 
768 		/*
769 		 * zero swslot to indicate that we are
770 		 * no longer building a swap-backed cluster.
771 		 */
772 
773 		swslot = 0;
774 
775 		/*
776 		 * the pageout is in progress.  bump counters and set up
777 		 * for the next loop.
778 		 */
779 
780 		uvmexp.pdpending++;
781 		if (nextpg && (nextpg->pqflags & PQ_INACTIVE) == 0) {
782 			nextpg = TAILQ_FIRST(pglst);
783 		}
784 #endif /* defined(VMSWAP) */
785 	}
786 }
787 
788 /*
789  * uvmpd_scan: scan the page queues and attempt to meet our targets.
790  *
791  * => called with pageq's locked
792  */
793 
794 static void
795 uvmpd_scan(void)
796 {
797 	int inactive_shortage, swap_shortage, pages_freed;
798 	struct vm_page *p, *nextpg;
799 	struct uvm_object *uobj;
800 	struct vm_anon *anon;
801 	struct simplelock *slock;
802 	UVMHIST_FUNC("uvmpd_scan"); UVMHIST_CALLED(pdhist);
803 
804 	uvmexp.pdrevs++;
805 	uobj = NULL;
806 	anon = NULL;
807 
808 #ifndef __SWAP_BROKEN
809 
810 	/*
811 	 * swap out some processes if we are below our free target.
812 	 * we need to unlock the page queues for this.
813 	 */
814 
815 	if (uvmexp.free < uvmexp.freetarg && uvmexp.nswapdev != 0) {
816 		uvmexp.pdswout++;
817 		UVMHIST_LOG(pdhist,"  free %d < target %d: swapout",
818 		    uvmexp.free, uvmexp.freetarg, 0, 0);
819 		uvm_unlock_pageq();
820 		uvm_swapout_threads();
821 		uvm_lock_pageq();
822 
823 	}
824 #endif
825 
826 	/*
827 	 * now we want to work on meeting our targets.   first we work on our
828 	 * free target by converting inactive pages into free pages.  then
829 	 * we work on meeting our inactive target by converting active pages
830 	 * to inactive ones.
831 	 */
832 
833 	UVMHIST_LOG(pdhist, "  starting 'free' loop",0,0,0,0);
834 
835 	pages_freed = uvmexp.pdfreed;
836 	uvmpd_scan_inactive(&uvm.page_inactive);
837 	pages_freed = uvmexp.pdfreed - pages_freed;
838 
839 	/*
840 	 * we have done the scan to get free pages.   now we work on meeting
841 	 * our inactive target.
842 	 */
843 
844 	inactive_shortage = uvmexp.inactarg - uvmexp.inactive;
845 
846 	/*
847 	 * detect if we're not going to be able to page anything out
848 	 * until we free some swap resources from active pages.
849 	 */
850 
851 	swap_shortage = 0;
852 	if (uvmexp.free < uvmexp.freetarg &&
853 	    uvmexp.swpginuse >= uvmexp.swpgavail &&
854 	    !uvm_swapisfull() &&
855 	    pages_freed == 0) {
856 		swap_shortage = uvmexp.freetarg - uvmexp.free;
857 	}
858 
859 	UVMHIST_LOG(pdhist, "  loop 2: inactive_shortage=%d swap_shortage=%d",
860 		    inactive_shortage, swap_shortage,0,0);
861 	for (p = TAILQ_FIRST(&uvm.page_active);
862 	     p != NULL && (inactive_shortage > 0 || swap_shortage > 0);
863 	     p = nextpg) {
864 		nextpg = TAILQ_NEXT(p, pageq);
865 		if (p->flags & PG_BUSY) {
866 			continue;
867 		}
868 
869 		/*
870 		 * lock the page's owner.
871 		 */
872 
873 		if (p->uobject != NULL) {
874 			uobj = p->uobject;
875 			slock = &uobj->vmobjlock;
876 			if (!simple_lock_try(slock)) {
877 				continue;
878 			}
879 		} else {
880 			anon = p->uanon;
881 			KASSERT(anon != NULL);
882 			slock = &anon->an_lock;
883 			if (!simple_lock_try(slock)) {
884 				continue;
885 			}
886 
887 			/* take over the page? */
888 			if ((p->pqflags & PQ_ANON) == 0) {
889 				KASSERT(p->loan_count > 0);
890 				p->loan_count--;
891 				p->pqflags |= PQ_ANON;
892 			}
893 		}
894 
895 		/*
896 		 * skip this page if it's busy.
897 		 */
898 
899 		if ((p->flags & PG_BUSY) != 0) {
900 			simple_unlock(slock);
901 			continue;
902 		}
903 
904 #if defined(VMSWAP)
905 		/*
906 		 * if there's a shortage of swap, free any swap allocated
907 		 * to this page so that other pages can be paged out.
908 		 */
909 
910 		if (swap_shortage > 0) {
911 			if ((p->pqflags & PQ_ANON) && anon->an_swslot) {
912 				uvm_swap_free(anon->an_swslot, 1);
913 				anon->an_swslot = 0;
914 				p->flags &= ~PG_CLEAN;
915 				swap_shortage--;
916 			} else if (p->pqflags & PQ_AOBJ) {
917 				int slot = uao_set_swslot(uobj,
918 					p->offset >> PAGE_SHIFT, 0);
919 				if (slot) {
920 					uvm_swap_free(slot, 1);
921 					p->flags &= ~PG_CLEAN;
922 					swap_shortage--;
923 				}
924 			}
925 		}
926 #endif /* defined(VMSWAP) */
927 
928 		/*
929 		 * if there's a shortage of inactive pages, deactivate.
930 		 */
931 
932 		if (inactive_shortage > 0) {
933 			/* no need to check wire_count as pg is "active" */
934 			uvm_pagedeactivate(p);
935 			uvmexp.pddeact++;
936 			inactive_shortage--;
937 		}
938 
939 		/*
940 		 * we're done with this page.
941 		 */
942 
943 		simple_unlock(slock);
944 	}
945 }
946 
947 /*
948  * uvm_reclaimable: decide whether to wait for pagedaemon.
949  *
950  * => return TRUE if it seems to be worth to do uvm_wait.
951  *
952  * XXX should be tunable.
953  * XXX should consider pools, etc?
954  */
955 
956 boolean_t
957 uvm_reclaimable(void)
958 {
959 	int filepages;
960 
961 	/*
962 	 * if swap is not full, no problem.
963 	 */
964 
965 	if (!uvm_swapisfull()) {
966 		return TRUE;
967 	}
968 
969 	/*
970 	 * file-backed pages can be reclaimed even when swap is full.
971 	 * if we have more than 1/16 of pageable memory or 5MB, try to reclaim.
972 	 *
973 	 * XXX assume the worst case, ie. all wired pages are file-backed.
974 	 *
975 	 * XXX should consider about other reclaimable memory.
976 	 * XXX ie. pools, traditional buffer cache.
977 	 */
978 
979 	filepages = uvmexp.filepages + uvmexp.execpages - uvmexp.wired;
980 	if (filepages >= MIN((uvmexp.active + uvmexp.inactive) >> 4,
981 	    5 * 1024 * 1024 >> PAGE_SHIFT)) {
982 		return TRUE;
983 	}
984 
985 	/*
986 	 * kill the process, fail allocation, etc..
987 	 */
988 
989 	return FALSE;
990 }
991