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