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