xref: /netbsd-src/sys/uvm/uvm_pdaemon.c (revision 8a8f936f250a330d54f8a24ed0e92aadf9743a7b)
1 /*	$NetBSD: uvm_pdaemon.c,v 1.39 2001/09/30 02:57:34 chs Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 Charles D. Cranor and Washington University.
5  * Copyright (c) 1991, 1993, The Regents of the University of California.
6  *
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * The Mach Operating System project at Carnegie-Mellon University.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by Charles D. Cranor,
23  *      Washington University, the University of California, Berkeley and
24  *      its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  *	@(#)vm_pageout.c        8.5 (Berkeley) 2/14/94
42  * from: Id: uvm_pdaemon.c,v 1.1.2.32 1998/02/06 05:26:30 chs Exp
43  *
44  *
45  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
46  * All rights reserved.
47  *
48  * Permission to use, copy, modify and distribute this software and
49  * its documentation is hereby granted, provided that both the copyright
50  * notice and this permission notice appear in all copies of the
51  * software, derivative works or modified versions, and any portions
52  * thereof, and that both notices appear in supporting documentation.
53  *
54  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
55  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
56  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
57  *
58  * Carnegie Mellon requests users of this software to return to
59  *
60  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
61  *  School of Computer Science
62  *  Carnegie Mellon University
63  *  Pittsburgh PA 15213-3890
64  *
65  * any improvements or extensions that they make and grant Carnegie the
66  * rights to redistribute these changes.
67  */
68 
69 #include "opt_uvmhist.h"
70 
71 /*
72  * uvm_pdaemon.c: the page daemon
73  */
74 
75 #include <sys/param.h>
76 #include <sys/proc.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
79 #include <sys/pool.h>
80 #include <sys/buf.h>
81 #include <sys/vnode.h>
82 
83 #include <uvm/uvm.h>
84 
85 /*
86  * UVMPD_NUMDIRTYREACTS is how many dirty pages the pagedeamon will reactivate
87  * in a pass thru the inactive list when swap is full.  the value should be
88  * "small"... if it's too large we'll cycle the active pages thru the inactive
89  * queue too quickly to for them to be referenced and avoid being freed.
90  */
91 
92 #define UVMPD_NUMDIRTYREACTS 16
93 
94 
95 /*
96  * local prototypes
97  */
98 
99 void		uvmpd_scan __P((void));
100 boolean_t	uvmpd_scan_inactive __P((struct pglist *));
101 void		uvmpd_tune __P((void));
102 
103 /*
104  * uvm_wait: wait (sleep) for the page daemon to free some pages
105  *
106  * => should be called with all locks released
107  * => should _not_ be called by the page daemon (to avoid deadlock)
108  */
109 
110 void
111 uvm_wait(wmsg)
112 	const char *wmsg;
113 {
114 	int timo = 0;
115 	int s = splbio();
116 
117 	/*
118 	 * check for page daemon going to sleep (waiting for itself)
119 	 */
120 
121 	if (curproc == uvm.pagedaemon_proc && uvmexp.paging == 0) {
122 		/*
123 		 * now we have a problem: the pagedaemon wants to go to
124 		 * sleep until it frees more memory.   but how can it
125 		 * free more memory if it is asleep?  that is a deadlock.
126 		 * we have two options:
127 		 *  [1] panic now
128 		 *  [2] put a timeout on the sleep, thus causing the
129 		 *      pagedaemon to only pause (rather than sleep forever)
130 		 *
131 		 * note that option [2] will only help us if we get lucky
132 		 * and some other process on the system breaks the deadlock
133 		 * by exiting or freeing memory (thus allowing the pagedaemon
134 		 * to continue).  for now we panic if DEBUG is defined,
135 		 * otherwise we hope for the best with option [2] (better
136 		 * yet, this should never happen in the first place!).
137 		 */
138 
139 		printf("pagedaemon: deadlock detected!\n");
140 		timo = hz >> 3;		/* set timeout */
141 #if defined(DEBUG)
142 		/* DEBUG: panic so we can debug it */
143 		panic("pagedaemon deadlock");
144 #endif
145 	}
146 
147 	simple_lock(&uvm.pagedaemon_lock);
148 	wakeup(&uvm.pagedaemon);		/* wake the daemon! */
149 	UVM_UNLOCK_AND_WAIT(&uvmexp.free, &uvm.pagedaemon_lock, FALSE, wmsg,
150 	    timo);
151 
152 	splx(s);
153 }
154 
155 
156 /*
157  * uvmpd_tune: tune paging parameters
158  *
159  * => called when ever memory is added (or removed?) to the system
160  * => caller must call with page queues locked
161  */
162 
163 void
164 uvmpd_tune(void)
165 {
166 	UVMHIST_FUNC("uvmpd_tune"); UVMHIST_CALLED(pdhist);
167 
168 	uvmexp.freemin = uvmexp.npages / 20;
169 
170 	/* between 16k and 256k */
171 	/* XXX:  what are these values good for? */
172 	uvmexp.freemin = MAX(uvmexp.freemin, (16*1024) >> PAGE_SHIFT);
173 	uvmexp.freemin = MIN(uvmexp.freemin, (256*1024) >> PAGE_SHIFT);
174 
175 	/* Make sure there's always a user page free. */
176 	if (uvmexp.freemin < uvmexp.reserve_kernel + 1)
177 		uvmexp.freemin = uvmexp.reserve_kernel + 1;
178 
179 	uvmexp.freetarg = (uvmexp.freemin * 4) / 3;
180 	if (uvmexp.freetarg <= uvmexp.freemin)
181 		uvmexp.freetarg = uvmexp.freemin + 1;
182 
183 	/* uvmexp.inactarg: computed in main daemon loop */
184 
185 	uvmexp.wiredmax = uvmexp.npages / 3;
186 	UVMHIST_LOG(pdhist, "<- done, freemin=%d, freetarg=%d, wiredmax=%d",
187 	      uvmexp.freemin, uvmexp.freetarg, uvmexp.wiredmax, 0);
188 }
189 
190 /*
191  * uvm_pageout: the main loop for the pagedaemon
192  */
193 
194 void
195 uvm_pageout(void *arg)
196 {
197 	int npages = 0;
198 	UVMHIST_FUNC("uvm_pageout"); UVMHIST_CALLED(pdhist);
199 
200 	UVMHIST_LOG(pdhist,"<starting uvm pagedaemon>", 0, 0, 0, 0);
201 
202 	/*
203 	 * ensure correct priority and set paging parameters...
204 	 */
205 
206 	uvm.pagedaemon_proc = curproc;
207 	uvm_lock_pageq();
208 	npages = uvmexp.npages;
209 	uvmpd_tune();
210 	uvm_unlock_pageq();
211 
212 	/*
213 	 * main loop
214 	 */
215 
216 	for (;;) {
217 		simple_lock(&uvm.pagedaemon_lock);
218 
219 		UVMHIST_LOG(pdhist,"  <<SLEEPING>>",0,0,0,0);
220 		UVM_UNLOCK_AND_WAIT(&uvm.pagedaemon,
221 		    &uvm.pagedaemon_lock, FALSE, "pgdaemon", 0);
222 		uvmexp.pdwoke++;
223 		UVMHIST_LOG(pdhist,"  <<WOKE UP>>",0,0,0,0);
224 
225 		/*
226 		 * now lock page queues and recompute inactive count
227 		 */
228 
229 		uvm_lock_pageq();
230 		if (npages != uvmexp.npages) {	/* check for new pages? */
231 			npages = uvmexp.npages;
232 			uvmpd_tune();
233 		}
234 
235 		uvmexp.inactarg = (uvmexp.active + uvmexp.inactive) / 3;
236 		if (uvmexp.inactarg <= uvmexp.freetarg) {
237 			uvmexp.inactarg = uvmexp.freetarg + 1;
238 		}
239 
240 		UVMHIST_LOG(pdhist,"  free/ftarg=%d/%d, inact/itarg=%d/%d",
241 		    uvmexp.free, uvmexp.freetarg, uvmexp.inactive,
242 		    uvmexp.inactarg);
243 
244 		/*
245 		 * scan if needed
246 		 */
247 
248 		if (uvmexp.free + uvmexp.paging < uvmexp.freetarg ||
249 		    uvmexp.inactive < uvmexp.inactarg) {
250 			uvmpd_scan();
251 		}
252 
253 		/*
254 		 * if there's any free memory to be had,
255 		 * wake up any waiters.
256 		 */
257 
258 		if (uvmexp.free > uvmexp.reserve_kernel ||
259 		    uvmexp.paging == 0) {
260 			wakeup(&uvmexp.free);
261 		}
262 
263 		/*
264 		 * scan done.  unlock page queues (the only lock we are holding)
265 		 */
266 
267 		uvm_unlock_pageq();
268 
269 		/*
270 		 * drain pool resources now that we're not holding any locks
271 		 */
272 
273 		pool_drain(0);
274 	}
275 	/*NOTREACHED*/
276 }
277 
278 
279 /*
280  * uvm_aiodone_daemon:  main loop for the aiodone daemon.
281  */
282 
283 void
284 uvm_aiodone_daemon(void *arg)
285 {
286 	int s, free;
287 	struct buf *bp, *nbp;
288 	UVMHIST_FUNC("uvm_aiodoned"); UVMHIST_CALLED(pdhist);
289 
290 	for (;;) {
291 
292 		/*
293 		 * carefully attempt to go to sleep (without losing "wakeups"!).
294 		 * we need splbio because we want to make sure the aio_done list
295 		 * is totally empty before we go to sleep.
296 		 */
297 
298 		s = splbio();
299 		simple_lock(&uvm.aiodoned_lock);
300 		if (TAILQ_FIRST(&uvm.aio_done) == NULL) {
301 			UVMHIST_LOG(pdhist,"  <<SLEEPING>>",0,0,0,0);
302 			UVM_UNLOCK_AND_WAIT(&uvm.aiodoned,
303 			    &uvm.aiodoned_lock, FALSE, "aiodoned", 0);
304 			UVMHIST_LOG(pdhist,"  <<WOKE UP>>",0,0,0,0);
305 
306 			/* relock aiodoned_lock, still at splbio */
307 			simple_lock(&uvm.aiodoned_lock);
308 		}
309 
310 		/*
311 		 * check for done aio structures
312 		 */
313 
314 		bp = TAILQ_FIRST(&uvm.aio_done);
315 		if (bp) {
316 			TAILQ_INIT(&uvm.aio_done);
317 		}
318 
319 		simple_unlock(&uvm.aiodoned_lock);
320 		splx(s);
321 
322 		/*
323 		 * process each i/o that's done.
324 		 */
325 
326 		free = uvmexp.free;
327 		while (bp != NULL) {
328 			nbp = TAILQ_NEXT(bp, b_freelist);
329 			(*bp->b_iodone)(bp);
330 			bp = nbp;
331 		}
332 		if (free <= uvmexp.reserve_kernel) {
333 			s = uvm_lock_fpageq();
334 			wakeup(&uvm.pagedaemon);
335 			uvm_unlock_fpageq(s);
336 		} else {
337 			simple_lock(&uvm.pagedaemon_lock);
338 			wakeup(&uvmexp.free);
339 			simple_unlock(&uvm.pagedaemon_lock);
340 		}
341 	}
342 }
343 
344 /*
345  * uvmpd_scan_inactive: scan an inactive list for pages to clean or free.
346  *
347  * => called with page queues locked
348  * => we work on meeting our free target by converting inactive pages
349  *    into free pages.
350  * => we handle the building of swap-backed clusters
351  * => we return TRUE if we are exiting because we met our target
352  */
353 
354 boolean_t
355 uvmpd_scan_inactive(pglst)
356 	struct pglist *pglst;
357 {
358 	boolean_t retval = FALSE;	/* assume we haven't hit target */
359 	int error;
360 	struct vm_page *p, *nextpg;
361 	struct uvm_object *uobj;
362 	struct vm_anon *anon;
363 	struct vm_page *swpps[MAXBSIZE >> PAGE_SHIFT];
364 	struct simplelock *slock;
365 	int swnpages, swcpages;
366 	int swslot;
367 	int dirtyreacts, t, result;
368 	UVMHIST_FUNC("uvmpd_scan_inactive"); UVMHIST_CALLED(pdhist);
369 
370 	/*
371 	 * swslot is non-zero if we are building a swap cluster.  we want
372 	 * to stay in the loop while we have a page to scan or we have
373 	 * a swap-cluster to build.
374 	 */
375 
376 	swslot = 0;
377 	swnpages = swcpages = 0;
378 	dirtyreacts = 0;
379 	for (p = TAILQ_FIRST(pglst); p != NULL || swslot != 0; p = nextpg) {
380 		uobj = NULL;
381 		anon = NULL;
382 		if (p) {
383 
384 			/*
385 			 * see if we've met the free target.
386 			 */
387 
388 			if (uvmexp.free + uvmexp.paging >=
389 			    uvmexp.freetarg << 2 ||
390 			    dirtyreacts == UVMPD_NUMDIRTYREACTS) {
391 				UVMHIST_LOG(pdhist,"  met free target: "
392 					    "exit loop", 0, 0, 0, 0);
393 				retval = TRUE;
394 
395 				if (swslot == 0) {
396 					/* exit now if no swap-i/o pending */
397 					break;
398 				}
399 
400 				/* set p to null to signal final swap i/o */
401 				p = NULL;
402 				nextpg = NULL;
403 			}
404 		}
405 		if (p) {	/* if (we have a new page to consider) */
406 
407 			/*
408 			 * we are below target and have a new page to consider.
409 			 */
410 
411 			uvmexp.pdscans++;
412 			nextpg = TAILQ_NEXT(p, pageq);
413 
414 			/*
415 			 * move referenced pages back to active queue and
416 			 * skip to next page.
417 			 */
418 
419 			if (pmap_clear_reference(p)) {
420 				uvm_pageactivate(p);
421 				uvmexp.pdreact++;
422 				continue;
423 			}
424 			anon = p->uanon;
425 			uobj = p->uobject;
426 
427 			/*
428 			 * enforce the minimum thresholds on different
429 			 * types of memory usage.  if reusing the current
430 			 * page would reduce that type of usage below its
431 			 * minimum, reactivate the page instead and move
432 			 * on to the next page.
433 			 */
434 
435 			t = uvmexp.active + uvmexp.inactive + uvmexp.free;
436 			if (anon &&
437 			    uvmexp.anonpages <= (t * uvmexp.anonmin) >> 8) {
438 				uvm_pageactivate(p);
439 				uvmexp.pdreanon++;
440 				continue;
441 			}
442 			if (uobj && UVM_OBJ_IS_VTEXT(uobj) &&
443 			    uvmexp.vtextpages <= (t * uvmexp.vtextmin) >> 8) {
444 				uvm_pageactivate(p);
445 				uvmexp.pdrevtext++;
446 				continue;
447 			}
448 			if (uobj && UVM_OBJ_IS_VNODE(uobj) &&
449 			    !UVM_OBJ_IS_VTEXT(uobj) &&
450 			    uvmexp.vnodepages <= (t * uvmexp.vnodemin) >> 8) {
451 				uvm_pageactivate(p);
452 				uvmexp.pdrevnode++;
453 				continue;
454 			}
455 
456 			/*
457 			 * first we attempt to lock the object that this page
458 			 * belongs to.  if our attempt fails we skip on to
459 			 * the next page (no harm done).  it is important to
460 			 * "try" locking the object as we are locking in the
461 			 * wrong order (pageq -> object) and we don't want to
462 			 * deadlock.
463 			 *
464 			 * the only time we expect to see an ownerless page
465 			 * (i.e. a page with no uobject and !PQ_ANON) is if an
466 			 * anon has loaned a page from a uvm_object and the
467 			 * uvm_object has dropped the ownership.  in that
468 			 * case, the anon can "take over" the loaned page
469 			 * and make it its own.
470 			 */
471 
472 			/* is page part of an anon or ownerless ? */
473 			if ((p->pqflags & PQ_ANON) || uobj == NULL) {
474 				KASSERT(anon != NULL);
475 				slock = &anon->an_lock;
476 				if (!simple_lock_try(slock)) {
477 					/* lock failed, skip this page */
478 					continue;
479 				}
480 
481 				/*
482 				 * if the page is ownerless, claim it in the
483 				 * name of "anon"!
484 				 */
485 
486 				if ((p->pqflags & PQ_ANON) == 0) {
487 					KASSERT(p->loan_count > 0);
488 					p->loan_count--;
489 					p->pqflags |= PQ_ANON;
490 					/* anon now owns it */
491 				}
492 				if (p->flags & PG_BUSY) {
493 					simple_unlock(slock);
494 					uvmexp.pdbusy++;
495 					continue;
496 				}
497 				uvmexp.pdanscan++;
498 			} else {
499 				KASSERT(uobj != NULL);
500 				slock = &uobj->vmobjlock;
501 				if (!simple_lock_try(slock)) {
502 					continue;
503 				}
504 				if (p->flags & PG_BUSY) {
505 					simple_unlock(slock);
506 					uvmexp.pdbusy++;
507 					continue;
508 				}
509 				uvmexp.pdobscan++;
510 			}
511 
512 
513 			/*
514 			 * we now have the object and the page queues locked.
515 			 * if the page is not swap-backed, call the object's
516 			 * pager to flush and free the page.
517 			 */
518 
519 			if ((p->pqflags & PQ_SWAPBACKED) == 0) {
520 				uvm_unlock_pageq();
521 				error = (uobj->pgops->pgo_put)(uobj, p->offset,
522 				    p->offset + PAGE_SIZE,
523 				    PGO_CLEANIT|PGO_FREE);
524 				uvm_lock_pageq();
525 				if (nextpg &&
526 				    (nextpg->flags & PQ_INACTIVE) == 0) {
527 					nextpg = TAILQ_FIRST(pglst);
528 				}
529 				continue;
530 			}
531 
532 			/*
533 			 * the page is swap-backed.  remove all the permissions
534 			 * from the page so we can sync the modified info
535 			 * without any race conditions.  if the page is clean
536 			 * we can free it now and continue.
537 			 */
538 
539 			pmap_page_protect(p, VM_PROT_NONE);
540 			if ((p->flags & PG_CLEAN) && pmap_clear_modify(p)) {
541 				p->flags &= ~(PG_CLEAN);
542 			}
543 			if (p->flags & PG_CLEAN) {
544 				uvm_pagefree(p);
545 				uvmexp.pdfreed++;
546 
547 				/*
548 				 * for anons, we need to remove the page
549 				 * from the anon ourselves.  for aobjs,
550 				 * pagefree did that for us.
551 				 */
552 
553 				if (anon) {
554 					KASSERT(anon->an_swslot != 0);
555 					anon->u.an_page = NULL;
556 				}
557 				simple_unlock(slock);
558 				continue;
559 			}
560 
561 			/*
562 			 * this page is dirty, skip it if we'll have met our
563 			 * free target when all the current pageouts complete.
564 			 */
565 
566 			if (uvmexp.free + uvmexp.paging >
567 			    uvmexp.freetarg << 2) {
568 				simple_unlock(slock);
569 				continue;
570 			}
571 
572 			/*
573 			 * free any swap space allocated to the page since
574 			 * we'll have to write it again with its new data.
575 			 */
576 
577 			if ((p->pqflags & PQ_ANON) && anon->an_swslot) {
578 				uvm_swap_free(anon->an_swslot, 1);
579 				anon->an_swslot = 0;
580 			} else if (p->pqflags & PQ_AOBJ) {
581 				uao_dropswap(uobj, p->offset >> PAGE_SHIFT);
582 			}
583 
584 			/*
585 			 * if all pages in swap are only in swap,
586 			 * the swap space is full and we can't page out
587 			 * any more swap-backed pages.  reactivate this page
588 			 * so that we eventually cycle all pages through
589 			 * the inactive queue.
590 			 */
591 
592 			KASSERT(uvmexp.swpgonly <= uvmexp.swpages);
593 			if (uvmexp.swpgonly == uvmexp.swpages) {
594 				dirtyreacts++;
595 				uvm_pageactivate(p);
596 				simple_unlock(slock);
597 				continue;
598 			}
599 
600 			/*
601 			 * start new swap pageout cluster (if necessary).
602 			 */
603 
604 			if (swslot == 0) {
605 				swnpages = MAXBSIZE >> PAGE_SHIFT;
606 				swslot = uvm_swap_alloc(&swnpages, TRUE);
607 				if (swslot == 0) {
608 					simple_unlock(slock);
609 					continue;
610 				}
611 				swcpages = 0;
612 			}
613 
614 			/*
615 			 * at this point, we're definitely going reuse this
616 			 * page.  mark the page busy and delayed-free.
617 			 * we should remove the page from the page queues
618 			 * so we don't ever look at it again.
619 			 * adjust counters and such.
620 			 */
621 
622 			p->flags |= PG_BUSY;
623 			UVM_PAGE_OWN(p, "scan_inactive");
624 
625 			p->flags |= PG_PAGEOUT;
626 			uvmexp.paging++;
627 			uvm_pagedequeue(p);
628 
629 			uvmexp.pgswapout++;
630 
631 			/*
632 			 * add the new page to the cluster.
633 			 */
634 
635 			if (anon) {
636 				anon->an_swslot = swslot + swcpages;
637 				simple_unlock(slock);
638 			} else {
639 				result = uao_set_swslot(uobj,
640 				    p->offset >> PAGE_SHIFT, swslot + swcpages);
641 				if (result == -1) {
642 					p->flags &= ~(PG_BUSY|PG_PAGEOUT);
643 					UVM_PAGE_OWN(p, NULL);
644 					uvmexp.paging--;
645 					uvm_pageactivate(p);
646 					simple_unlock(slock);
647 					continue;
648 				}
649 				simple_unlock(slock);
650 			}
651 			swpps[swcpages] = p;
652 			swcpages++;
653 
654 			/*
655 			 * if the cluster isn't full, look for more pages
656 			 * before starting the i/o.
657 			 */
658 
659 			if (swcpages < swnpages) {
660 				continue;
661 			}
662 		}
663 
664 		/*
665 		 * if this is the final pageout we could have a few
666 		 * unused swap blocks.  if so, free them now.
667 		 */
668 
669 		if (swcpages < swnpages) {
670 			uvm_swap_free(swslot + swcpages, (swnpages - swcpages));
671 		}
672 
673 		/*
674 		 * now start the pageout.
675 		 */
676 
677 		uvm_unlock_pageq();
678 		uvmexp.pdpageouts++;
679 		error = uvm_swap_put(swslot, swpps, swcpages, 0);
680 		KASSERT(error == 0);
681 		uvm_lock_pageq();
682 
683 		/*
684 		 * zero swslot to indicate that we are
685 		 * no longer building a swap-backed cluster.
686 		 */
687 
688 		swslot = 0;
689 
690 		/*
691 		 * the pageout is in progress.  bump counters and set up
692 		 * for the next loop.
693 		 */
694 
695 		uvmexp.pdpending++;
696 		if (nextpg && (nextpg->pqflags & PQ_INACTIVE) == 0) {
697 			nextpg = TAILQ_FIRST(pglst);
698 		}
699 	}
700 	return (error);
701 }
702 
703 /*
704  * uvmpd_scan: scan the page queues and attempt to meet our targets.
705  *
706  * => called with pageq's locked
707  */
708 
709 void
710 uvmpd_scan(void)
711 {
712 	int inactive_shortage, swap_shortage, pages_freed;
713 	struct vm_page *p, *nextpg;
714 	struct uvm_object *uobj;
715 	struct vm_anon *anon;
716 	boolean_t got_it;
717 	UVMHIST_FUNC("uvmpd_scan"); UVMHIST_CALLED(pdhist);
718 
719 	uvmexp.pdrevs++;
720 	uobj = NULL;
721 	anon = NULL;
722 
723 #ifndef __SWAP_BROKEN
724 
725 	/*
726 	 * swap out some processes if we are below our free target.
727 	 * we need to unlock the page queues for this.
728 	 */
729 
730 	if (uvmexp.free < uvmexp.freetarg && uvmexp.nswapdev != 0) {
731 		uvmexp.pdswout++;
732 		UVMHIST_LOG(pdhist,"  free %d < target %d: swapout",
733 		    uvmexp.free, uvmexp.freetarg, 0, 0);
734 		uvm_unlock_pageq();
735 		uvm_swapout_threads();
736 		uvm_lock_pageq();
737 
738 	}
739 #endif
740 
741 	/*
742 	 * now we want to work on meeting our targets.   first we work on our
743 	 * free target by converting inactive pages into free pages.  then
744 	 * we work on meeting our inactive target by converting active pages
745 	 * to inactive ones.
746 	 */
747 
748 	UVMHIST_LOG(pdhist, "  starting 'free' loop",0,0,0,0);
749 
750 	/*
751 	 * alternate starting queue between swap and object based on the
752 	 * low bit of uvmexp.pdrevs (which we bump by one each call).
753 	 */
754 
755 	got_it = FALSE;
756 	pages_freed = uvmexp.pdfreed;
757 	(void) uvmpd_scan_inactive(&uvm.page_inactive);
758 	pages_freed = uvmexp.pdfreed - pages_freed;
759 
760 	/*
761 	 * we have done the scan to get free pages.   now we work on meeting
762 	 * our inactive target.
763 	 */
764 
765 	inactive_shortage = uvmexp.inactarg - uvmexp.inactive;
766 
767 	/*
768 	 * detect if we're not going to be able to page anything out
769 	 * until we free some swap resources from active pages.
770 	 */
771 
772 	swap_shortage = 0;
773 	if (uvmexp.free < uvmexp.freetarg &&
774 	    uvmexp.swpginuse == uvmexp.swpages &&
775 	    uvmexp.swpgonly < uvmexp.swpages &&
776 	    pages_freed == 0) {
777 		swap_shortage = uvmexp.freetarg - uvmexp.free;
778 	}
779 
780 	UVMHIST_LOG(pdhist, "  loop 2: inactive_shortage=%d swap_shortage=%d",
781 		    inactive_shortage, swap_shortage,0,0);
782 	for (p = TAILQ_FIRST(&uvm.page_active);
783 	     p != NULL && (inactive_shortage > 0 || swap_shortage > 0);
784 	     p = nextpg) {
785 		nextpg = TAILQ_NEXT(p, pageq);
786 		if (p->flags & PG_BUSY) {
787 			continue;
788 		}
789 
790 		/*
791 		 * lock the page's owner.
792 		 */
793 		/* is page anon owned or ownerless? */
794 		if ((p->pqflags & PQ_ANON) || p->uobject == NULL) {
795 			anon = p->uanon;
796 			KASSERT(anon != NULL);
797 			if (!simple_lock_try(&anon->an_lock)) {
798 				continue;
799 			}
800 
801 			/* take over the page? */
802 			if ((p->pqflags & PQ_ANON) == 0) {
803 				KASSERT(p->loan_count > 0);
804 				p->loan_count--;
805 				p->pqflags |= PQ_ANON;
806 			}
807 		} else {
808 			uobj = p->uobject;
809 			if (!simple_lock_try(&uobj->vmobjlock)) {
810 				continue;
811 			}
812 		}
813 
814 		/*
815 		 * skip this page if it's busy.
816 		 */
817 
818 		if ((p->flags & PG_BUSY) != 0) {
819 			if (p->pqflags & PQ_ANON)
820 				simple_unlock(&anon->an_lock);
821 			else
822 				simple_unlock(&uobj->vmobjlock);
823 			continue;
824 		}
825 
826 		/*
827 		 * if there's a shortage of swap, free any swap allocated
828 		 * to this page so that other pages can be paged out.
829 		 */
830 
831 		if (swap_shortage > 0) {
832 			if ((p->pqflags & PQ_ANON) && anon->an_swslot) {
833 				uvm_swap_free(anon->an_swslot, 1);
834 				anon->an_swslot = 0;
835 				p->flags &= ~PG_CLEAN;
836 				swap_shortage--;
837 			} else if (p->pqflags & PQ_AOBJ) {
838 				int slot = uao_set_swslot(uobj,
839 					p->offset >> PAGE_SHIFT, 0);
840 				if (slot) {
841 					uvm_swap_free(slot, 1);
842 					p->flags &= ~PG_CLEAN;
843 					swap_shortage--;
844 				}
845 			}
846 		}
847 
848 		/*
849 		 * if there's a shortage of inactive pages, deactivate.
850 		 */
851 
852 		if (inactive_shortage > 0) {
853 			/* no need to check wire_count as pg is "active" */
854 			uvm_pagedeactivate(p);
855 			uvmexp.pddeact++;
856 			inactive_shortage--;
857 		}
858 
859 		/*
860 		 * we're done with this page.
861 		 */
862 
863 		if (p->pqflags & PQ_ANON)
864 			simple_unlock(&anon->an_lock);
865 		else
866 			simple_unlock(&uobj->vmobjlock);
867 	}
868 }
869