xref: /openbsd-src/sys/uvm/uvm_pdaemon.c (revision de8cc8edbc71bd3e3bc7fbffa27ba0e564c37d8b)
1 /*	$OpenBSD: uvm_pdaemon.c,v 1.89 2021/03/01 09:13:33 mpi Exp $	*/
2 /*	$NetBSD: uvm_pdaemon.c,v 1.23 2000/08/20 10:24:14 bjh21 Exp $	*/
3 
4 /*
5  * Copyright (c) 1997 Charles D. Cranor and Washington University.
6  * Copyright (c) 1991, 1993, The Regents of the University of California.
7  *
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to Berkeley by
11  * The Mach Operating System project at Carnegie-Mellon University.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)vm_pageout.c        8.5 (Berkeley) 2/14/94
38  * from: Id: uvm_pdaemon.c,v 1.1.2.32 1998/02/06 05:26:30 chs Exp
39  *
40  *
41  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
42  * All rights reserved.
43  *
44  * Permission to use, copy, modify and distribute this software and
45  * its documentation is hereby granted, provided that both the copyright
46  * notice and this permission notice appear in all copies of the
47  * software, derivative works or modified versions, and any portions
48  * thereof, and that both notices appear in supporting documentation.
49  *
50  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
51  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
52  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
53  *
54  * Carnegie Mellon requests users of this software to return to
55  *
56  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
57  *  School of Computer Science
58  *  Carnegie Mellon University
59  *  Pittsburgh PA 15213-3890
60  *
61  * any improvements or extensions that they make and grant Carnegie the
62  * rights to redistribute these changes.
63  */
64 
65 /*
66  * uvm_pdaemon.c: the page daemon
67  */
68 
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/kernel.h>
72 #include <sys/pool.h>
73 #include <sys/proc.h>
74 #include <sys/buf.h>
75 #include <sys/mount.h>
76 #include <sys/atomic.h>
77 
78 #ifdef HIBERNATE
79 #include <sys/hibernate.h>
80 #endif
81 
82 #include <uvm/uvm.h>
83 
84 #if defined(__amd64__) || defined(__arm64__) || \
85     defined(__i386__) || defined(__loongson__) || \
86     defined(__macppc__) || defined(__sparc64__)
87 #include "drm.h"
88 #endif
89 
90 #if NDRM > 0
91 extern void drmbackoff(long);
92 #endif
93 
94 /*
95  * UVMPD_NUMDIRTYREACTS is how many dirty pages the pagedaemon will reactivate
96  * in a pass thru the inactive list when swap is full.  the value should be
97  * "small"... if it's too large we'll cycle the active pages thru the inactive
98  * queue too quickly to for them to be referenced and avoid being freed.
99  */
100 
101 #define UVMPD_NUMDIRTYREACTS 16
102 
103 
104 /*
105  * local prototypes
106  */
107 
108 void		uvmpd_scan(void);
109 boolean_t	uvmpd_scan_inactive(struct pglist *);
110 void		uvmpd_tune(void);
111 void		uvmpd_drop(struct pglist *);
112 
113 /*
114  * uvm_wait: wait (sleep) for the page daemon to free some pages
115  *
116  * => should be called with all locks released
117  * => should _not_ be called by the page daemon (to avoid deadlock)
118  */
119 
120 void
121 uvm_wait(const char *wmsg)
122 {
123 	uint64_t timo = INFSLP;
124 
125 #ifdef DIAGNOSTIC
126 	if (curproc == &proc0)
127 		panic("%s: cannot sleep for memory during boot", __func__);
128 #endif
129 
130 	/* check for page daemon going to sleep (waiting for itself) */
131 	if (curproc == uvm.pagedaemon_proc) {
132 		printf("uvm_wait emergency bufbackoff\n");
133 		if (bufbackoff(NULL, 4) == 0)
134 			return;
135 		/*
136 		 * now we have a problem: the pagedaemon wants to go to
137 		 * sleep until it frees more memory.   but how can it
138 		 * free more memory if it is asleep?  that is a deadlock.
139 		 * we have two options:
140 		 *  [1] panic now
141 		 *  [2] put a timeout on the sleep, thus causing the
142 		 *      pagedaemon to only pause (rather than sleep forever)
143 		 *
144 		 * note that option [2] will only help us if we get lucky
145 		 * and some other process on the system breaks the deadlock
146 		 * by exiting or freeing memory (thus allowing the pagedaemon
147 		 * to continue).  for now we panic if DEBUG is defined,
148 		 * otherwise we hope for the best with option [2] (better
149 		 * yet, this should never happen in the first place!).
150 		 */
151 
152 		printf("pagedaemon: deadlock detected!\n");
153 		timo = MSEC_TO_NSEC(125);	/* set timeout */
154 #if defined(DEBUG)
155 		/* DEBUG: panic so we can debug it */
156 		panic("pagedaemon deadlock");
157 #endif
158 	}
159 
160 	uvm_lock_fpageq();
161 	wakeup(&uvm.pagedaemon);		/* wake the daemon! */
162 	msleep_nsec(&uvmexp.free, &uvm.fpageqlock, PVM | PNORELOCK, wmsg, timo);
163 }
164 
165 /*
166  * uvmpd_tune: tune paging parameters
167  *
168  * => called whenever memory is added to (or removed from?) the system
169  * => caller must call with page queues locked
170  */
171 
172 void
173 uvmpd_tune(void)
174 {
175 
176 	uvmexp.freemin = uvmexp.npages / 30;
177 
178 	/* between 16k and 512k */
179 	/* XXX:  what are these values good for? */
180 	uvmexp.freemin = max(uvmexp.freemin, (16*1024) >> PAGE_SHIFT);
181 #if 0
182 	uvmexp.freemin = min(uvmexp.freemin, (512*1024) >> PAGE_SHIFT);
183 #endif
184 
185 	/* Make sure there's always a user page free. */
186 	if (uvmexp.freemin < uvmexp.reserve_kernel + 1)
187 		uvmexp.freemin = uvmexp.reserve_kernel + 1;
188 
189 	uvmexp.freetarg = (uvmexp.freemin * 4) / 3;
190 	if (uvmexp.freetarg <= uvmexp.freemin)
191 		uvmexp.freetarg = uvmexp.freemin + 1;
192 
193 	/* uvmexp.inactarg: computed in main daemon loop */
194 
195 	uvmexp.wiredmax = uvmexp.npages / 3;
196 }
197 
198 /*
199  * Indicate to the page daemon that a nowait call failed and it should
200  * recover at least some memory in the most restricted region (assumed
201  * to be dma_constraint).
202  */
203 volatile int uvm_nowait_failed;
204 
205 /*
206  * uvm_pageout: the main loop for the pagedaemon
207  */
208 void
209 uvm_pageout(void *arg)
210 {
211 	struct uvm_constraint_range constraint;
212 	struct uvm_pmalloc *pma;
213 	int npages = 0;
214 
215 	/* ensure correct priority and set paging parameters... */
216 	uvm.pagedaemon_proc = curproc;
217 	(void) spl0();
218 	uvm_lock_pageq();
219 	npages = uvmexp.npages;
220 	uvmpd_tune();
221 	uvm_unlock_pageq();
222 
223 	for (;;) {
224 		long size;
225 
226 		uvm_lock_fpageq();
227 		if (!uvm_nowait_failed && TAILQ_EMPTY(&uvm.pmr_control.allocs)) {
228 			msleep_nsec(&uvm.pagedaemon, &uvm.fpageqlock, PVM,
229 			    "pgdaemon", INFSLP);
230 			uvmexp.pdwoke++;
231 		}
232 
233 		if ((pma = TAILQ_FIRST(&uvm.pmr_control.allocs)) != NULL) {
234 			pma->pm_flags |= UVM_PMA_BUSY;
235 			constraint = pma->pm_constraint;
236 		} else {
237 			if (uvm_nowait_failed) {
238 				/*
239 				 * XXX realisticly, this is what our
240 				 * nowait callers probably care about
241 				 */
242 				constraint = dma_constraint;
243 				uvm_nowait_failed = 0;
244 			} else
245 				constraint = no_constraint;
246 		}
247 
248 		uvm_unlock_fpageq();
249 
250 		/* now lock page queues and recompute inactive count */
251 		uvm_lock_pageq();
252 		if (npages != uvmexp.npages) {	/* check for new pages? */
253 			npages = uvmexp.npages;
254 			uvmpd_tune();
255 		}
256 
257 		uvmexp.inactarg = (uvmexp.active + uvmexp.inactive) / 3;
258 		if (uvmexp.inactarg <= uvmexp.freetarg) {
259 			uvmexp.inactarg = uvmexp.freetarg + 1;
260 		}
261 
262 		/* Reclaim pages from the buffer cache if possible. */
263 		size = 0;
264 		if (pma != NULL)
265 			size += pma->pm_size >> PAGE_SHIFT;
266 		if (uvmexp.free - BUFPAGES_DEFICIT < uvmexp.freetarg)
267 			size += uvmexp.freetarg - (uvmexp.free -
268 			    BUFPAGES_DEFICIT);
269 		if (size == 0)
270 			size = 16; /* XXX */
271 		uvm_unlock_pageq();
272 		(void) bufbackoff(&constraint, size * 2);
273 #if NDRM > 0
274 		drmbackoff(size * 2);
275 #endif
276 		uvm_lock_pageq();
277 
278 		/* Scan if needed to meet our targets. */
279 		if (pma != NULL ||
280 		    ((uvmexp.free - BUFPAGES_DEFICIT) < uvmexp.freetarg) ||
281 		    ((uvmexp.inactive + BUFPAGES_INACT) < uvmexp.inactarg)) {
282 			uvmpd_scan();
283 		}
284 
285 		/*
286 		 * if there's any free memory to be had,
287 		 * wake up any waiters.
288 		 */
289 		uvm_lock_fpageq();
290 		if (uvmexp.free > uvmexp.reserve_kernel ||
291 		    uvmexp.paging == 0) {
292 			wakeup(&uvmexp.free);
293 		}
294 
295 		if (pma != NULL) {
296 			/*
297 			 * XXX If UVM_PMA_FREED isn't set, no pages
298 			 * were freed.  Should we set UVM_PMA_FAIL in
299 			 * that case?
300 			 */
301 			pma->pm_flags &= ~UVM_PMA_BUSY;
302 			if (pma->pm_flags & UVM_PMA_FREED) {
303 				pma->pm_flags &= ~UVM_PMA_LINKED;
304 				TAILQ_REMOVE(&uvm.pmr_control.allocs, pma,
305 				    pmq);
306 				wakeup(pma);
307 			}
308 		}
309 		uvm_unlock_fpageq();
310 
311 		/* scan done. unlock page queues (only lock we are holding) */
312 		uvm_unlock_pageq();
313 
314 		sched_pause(yield);
315 	}
316 	/*NOTREACHED*/
317 }
318 
319 
320 /*
321  * uvm_aiodone_daemon:  main loop for the aiodone daemon.
322  */
323 void
324 uvm_aiodone_daemon(void *arg)
325 {
326 	int s, free;
327 	struct buf *bp, *nbp;
328 
329 	uvm.aiodoned_proc = curproc;
330 
331 	for (;;) {
332 		/*
333 		 * Check for done aio structures. If we've got structures to
334 		 * process, do so. Otherwise sleep while avoiding races.
335 		 */
336 		mtx_enter(&uvm.aiodoned_lock);
337 		while ((bp = TAILQ_FIRST(&uvm.aio_done)) == NULL)
338 			msleep_nsec(&uvm.aiodoned, &uvm.aiodoned_lock,
339 			    PVM, "aiodoned", INFSLP);
340 		/* Take the list for ourselves. */
341 		TAILQ_INIT(&uvm.aio_done);
342 		mtx_leave(&uvm.aiodoned_lock);
343 
344 		/* process each i/o that's done. */
345 		free = uvmexp.free;
346 		while (bp != NULL) {
347 			if (bp->b_flags & B_PDAEMON) {
348 				uvmexp.paging -= bp->b_bufsize >> PAGE_SHIFT;
349 			}
350 			nbp = TAILQ_NEXT(bp, b_freelist);
351 			s = splbio();	/* b_iodone must by called at splbio */
352 			(*bp->b_iodone)(bp);
353 			splx(s);
354 			bp = nbp;
355 
356 			sched_pause(yield);
357 		}
358 		uvm_lock_fpageq();
359 		wakeup(free <= uvmexp.reserve_kernel ? &uvm.pagedaemon :
360 		    &uvmexp.free);
361 		uvm_unlock_fpageq();
362 	}
363 }
364 
365 
366 
367 /*
368  * uvmpd_scan_inactive: scan an inactive list for pages to clean or free.
369  *
370  * => called with page queues locked
371  * => we work on meeting our free target by converting inactive pages
372  *    into free pages.
373  * => we handle the building of swap-backed clusters
374  * => we return TRUE if we are exiting because we met our target
375  */
376 
377 boolean_t
378 uvmpd_scan_inactive(struct pglist *pglst)
379 {
380 	boolean_t retval = FALSE;	/* assume we haven't hit target */
381 	int free, result;
382 	struct vm_page *p, *nextpg;
383 	struct uvm_object *uobj;
384 	struct vm_page *pps[MAXBSIZE >> PAGE_SHIFT], **ppsp;
385 	int npages;
386 	struct vm_page *swpps[MAXBSIZE >> PAGE_SHIFT]; 	/* XXX: see below */
387 	int swnpages, swcpages;				/* XXX: see below */
388 	int swslot;
389 	struct vm_anon *anon;
390 	boolean_t swap_backed;
391 	vaddr_t start;
392 	int dirtyreacts;
393 
394 	/*
395 	 * note: we currently keep swap-backed pages on a separate inactive
396 	 * list from object-backed pages.   however, merging the two lists
397 	 * back together again hasn't been ruled out.   thus, we keep our
398 	 * swap cluster in "swpps" rather than in pps (allows us to mix
399 	 * clustering types in the event of a mixed inactive queue).
400 	 */
401 	/*
402 	 * swslot is non-zero if we are building a swap cluster.  we want
403 	 * to stay in the loop while we have a page to scan or we have
404 	 * a swap-cluster to build.
405 	 */
406 	swslot = 0;
407 	swnpages = swcpages = 0;
408 	free = 0;
409 	dirtyreacts = 0;
410 
411 	for (p = TAILQ_FIRST(pglst); p != NULL || swslot != 0; p = nextpg) {
412 		/*
413 		 * note that p can be NULL iff we have traversed the whole
414 		 * list and need to do one final swap-backed clustered pageout.
415 		 */
416 		uobj = NULL;
417 		anon = NULL;
418 
419 		if (p) {
420 			/*
421 			 * update our copy of "free" and see if we've met
422 			 * our target
423 			 */
424 			free = uvmexp.free - BUFPAGES_DEFICIT;
425 
426 			if (free + uvmexp.paging >= uvmexp.freetarg << 2 ||
427 			    dirtyreacts == UVMPD_NUMDIRTYREACTS) {
428 				retval = TRUE;
429 
430 				if (swslot == 0) {
431 					/* exit now if no swap-i/o pending */
432 					break;
433 				}
434 
435 				/* set p to null to signal final swap i/o */
436 				p = NULL;
437 			}
438 		}
439 
440 		if (p) {	/* if (we have a new page to consider) */
441 			/*
442 			 * we are below target and have a new page to consider.
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 (unlikely to happen since
450 			 * inactive pages shouldn't have any valid mappings
451 			 * and we cleared reference before deactivating).
452 			 */
453 
454 			if (pmap_is_referenced(p)) {
455 				uvm_pageactivate(p);
456 				uvmexp.pdreact++;
457 				continue;
458 			}
459 
460 			if (p->pg_flags & PQ_ANON) {
461 				anon = p->uanon;
462 				KASSERT(anon != NULL);
463 				if (rw_enter(anon->an_lock,
464 				    RW_WRITE|RW_NOSLEEP)) {
465 					/* lock failed, skip this page */
466 					continue;
467 				}
468 				if (p->pg_flags & PG_BUSY) {
469 					rw_exit(anon->an_lock);
470 					uvmexp.pdbusy++;
471 					/* someone else owns page, skip it */
472 					continue;
473 				}
474 				uvmexp.pdanscan++;
475 			} else {
476 				uobj = p->uobject;
477 				KASSERT(uobj != NULL);
478 				if (p->pg_flags & PG_BUSY) {
479 					uvmexp.pdbusy++;
480 					/* someone else owns page, skip it */
481 					continue;
482 				}
483 				uvmexp.pdobscan++;
484 			}
485 
486 			/*
487 			 * we now have the page queues locked.
488 			 * the page is not busy.   if the page is clean we
489 			 * can free it now and continue.
490 			 */
491 			if (p->pg_flags & PG_CLEAN) {
492 				if (p->pg_flags & PQ_SWAPBACKED) {
493 					/* this page now lives only in swap */
494 					uvmexp.swpgonly++;
495 				}
496 
497 				/* zap all mappings with pmap_page_protect... */
498 				pmap_page_protect(p, PROT_NONE);
499 				uvm_pagefree(p);
500 				uvmexp.pdfreed++;
501 
502 				if (anon) {
503 
504 					/*
505 					 * an anonymous page can only be clean
506 					 * if it has backing store assigned.
507 					 */
508 
509 					KASSERT(anon->an_swslot != 0);
510 
511 					/* remove from object */
512 					anon->an_page = NULL;
513 					rw_exit(anon->an_lock);
514 				}
515 				continue;
516 			}
517 
518 			/*
519 			 * this page is dirty, skip it if we'll have met our
520 			 * free target when all the current pageouts complete.
521 			 */
522 			if (free + uvmexp.paging > uvmexp.freetarg << 2) {
523 				if (anon) {
524 					rw_exit(anon->an_lock);
525 				}
526 				continue;
527 			}
528 
529 			/*
530 			 * this page is dirty, but we can't page it out
531 			 * since all pages in swap are only in swap.
532 			 * reactivate it so that we eventually cycle
533 			 * all pages thru the inactive queue.
534 			 */
535 			if ((p->pg_flags & PQ_SWAPBACKED) && uvm_swapisfull()) {
536 				dirtyreacts++;
537 				uvm_pageactivate(p);
538 				if (anon) {
539 					rw_exit(anon->an_lock);
540 				}
541 				continue;
542 			}
543 
544 			/*
545 			 * if the page is swap-backed and dirty and swap space
546 			 * is full, free any swap allocated to the page
547 			 * so that other pages can be paged out.
548 			 */
549 			KASSERT(uvmexp.swpginuse <= uvmexp.swpages);
550 			if ((p->pg_flags & PQ_SWAPBACKED) &&
551 			    uvmexp.swpginuse == uvmexp.swpages) {
552 
553 				if ((p->pg_flags & PQ_ANON) &&
554 				    p->uanon->an_swslot) {
555 					uvm_swap_free(p->uanon->an_swslot, 1);
556 					p->uanon->an_swslot = 0;
557 				}
558 				if (p->pg_flags & PQ_AOBJ) {
559 					uao_dropswap(p->uobject,
560 						     p->offset >> PAGE_SHIFT);
561 				}
562 			}
563 
564 			/*
565 			 * the page we are looking at is dirty.   we must
566 			 * clean it before it can be freed.  to do this we
567 			 * first mark the page busy so that no one else will
568 			 * touch the page.   we write protect all the mappings
569 			 * of the page so that no one touches it while it is
570 			 * in I/O.
571 			 */
572 
573 			swap_backed = ((p->pg_flags & PQ_SWAPBACKED) != 0);
574 			atomic_setbits_int(&p->pg_flags, PG_BUSY);
575 			UVM_PAGE_OWN(p, "scan_inactive");
576 			pmap_page_protect(p, PROT_READ);
577 			uvmexp.pgswapout++;
578 
579 			/*
580 			 * for swap-backed pages we need to (re)allocate
581 			 * swap space.
582 			 */
583 			if (swap_backed) {
584 				/* free old swap slot (if any) */
585 				if (anon) {
586 					if (anon->an_swslot) {
587 						uvm_swap_free(anon->an_swslot,
588 						    1);
589 						anon->an_swslot = 0;
590 					}
591 				} else {
592 					uao_dropswap(uobj,
593 						     p->offset >> PAGE_SHIFT);
594 				}
595 
596 				/* start new cluster (if necessary) */
597 				if (swslot == 0) {
598 					swnpages = MAXBSIZE >> PAGE_SHIFT;
599 					swslot = uvm_swap_alloc(&swnpages,
600 					    TRUE);
601 					if (swslot == 0) {
602 						/* no swap?  give up! */
603 						atomic_clearbits_int(
604 						    &p->pg_flags,
605 						    PG_BUSY);
606 						UVM_PAGE_OWN(p, NULL);
607 						if (anon)
608 							rw_exit(anon->an_lock);
609 						continue;
610 					}
611 					swcpages = 0;	/* cluster is empty */
612 				}
613 
614 				/* add block to cluster */
615 				swpps[swcpages] = p;
616 				if (anon)
617 					anon->an_swslot = swslot + swcpages;
618 				else
619 					uao_set_swslot(uobj,
620 					    p->offset >> PAGE_SHIFT,
621 					    swslot + swcpages);
622 				swcpages++;
623 			}
624 		} else {
625 			/* if p == NULL we must be doing a last swap i/o */
626 			swap_backed = TRUE;
627 		}
628 
629 		/*
630 		 * now consider doing the pageout.
631 		 *
632 		 * for swap-backed pages, we do the pageout if we have either
633 		 * filled the cluster (in which case (swnpages == swcpages) or
634 		 * run out of pages (p == NULL).
635 		 *
636 		 * for object pages, we always do the pageout.
637 		 */
638 		if (swap_backed) {
639 			if (p) {	/* if we just added a page to cluster */
640 				if (anon)
641 					rw_exit(anon->an_lock);
642 
643 				/* cluster not full yet? */
644 				if (swcpages < swnpages)
645 					continue;
646 			}
647 
648 			/* starting I/O now... set up for it */
649 			npages = swcpages;
650 			ppsp = swpps;
651 			/* for swap-backed pages only */
652 			start = (vaddr_t) swslot;
653 
654 			/* if this is final pageout we could have a few
655 			 * extra swap blocks */
656 			if (swcpages < swnpages) {
657 				uvm_swap_free(swslot + swcpages,
658 				    (swnpages - swcpages));
659 			}
660 		} else {
661 			/* normal object pageout */
662 			ppsp = pps;
663 			npages = sizeof(pps) / sizeof(struct vm_page *);
664 			/* not looked at because PGO_ALLPAGES is set */
665 			start = 0;
666 		}
667 
668 		/*
669 		 * now do the pageout.
670 		 *
671 		 * for swap_backed pages we have already built the cluster.
672 		 * for !swap_backed pages, uvm_pager_put will call the object's
673 		 * "make put cluster" function to build a cluster on our behalf.
674 		 *
675 		 * we pass the PGO_PDFREECLUST flag to uvm_pager_put to instruct
676 		 * it to free the cluster pages for us on a successful I/O (it
677 		 * always does this for un-successful I/O requests).  this
678 		 * allows us to do clustered pageout without having to deal
679 		 * with cluster pages at this level.
680 		 *
681 		 * note locking semantics of uvm_pager_put with PGO_PDFREECLUST:
682 		 *  IN: locked: page queues
683 		 * OUT: locked:
684 		 *     !locked: pageqs
685 		 */
686 
687 		uvmexp.pdpageouts++;
688 		result = uvm_pager_put(swap_backed ? NULL : uobj, p,
689 		    &ppsp, &npages, PGO_ALLPAGES|PGO_PDFREECLUST, start, 0);
690 
691 		/*
692 		 * if we did i/o to swap, zero swslot to indicate that we are
693 		 * no longer building a swap-backed cluster.
694 		 */
695 
696 		if (swap_backed)
697 			swslot = 0;		/* done with this cluster */
698 
699 		/*
700 		 * first, we check for VM_PAGER_PEND which means that the
701 		 * async I/O is in progress and the async I/O done routine
702 		 * will clean up after us.   in this case we move on to the
703 		 * next page.
704 		 *
705 		 * there is a very remote chance that the pending async i/o can
706 		 * finish _before_ we get here.   if that happens, our page "p"
707 		 * may no longer be on the inactive queue.   so we verify this
708 		 * when determining the next page (starting over at the head if
709 		 * we've lost our inactive page).
710 		 */
711 
712 		if (result == VM_PAGER_PEND) {
713 			uvmexp.paging += npages;
714 			uvm_lock_pageq();
715 			uvmexp.pdpending++;
716 			if (p) {
717 				if (p->pg_flags & PQ_INACTIVE)
718 					nextpg = TAILQ_NEXT(p, pageq);
719 				else
720 					nextpg = TAILQ_FIRST(pglst);
721 			} else {
722 				nextpg = NULL;
723 			}
724 			continue;
725 		}
726 
727 		/* clean up "p" if we have one */
728 		if (p) {
729 			/*
730 			 * the I/O request to "p" is done and uvm_pager_put
731 			 * has freed any cluster pages it may have allocated
732 			 * during I/O.  all that is left for us to do is
733 			 * clean up page "p" (which is still PG_BUSY).
734 			 *
735 			 * our result could be one of the following:
736 			 *   VM_PAGER_OK: successful pageout
737 			 *
738 			 *   VM_PAGER_AGAIN: tmp resource shortage, we skip
739 			 *     to next page
740 			 *   VM_PAGER_{FAIL,ERROR,BAD}: an error.   we
741 			 *     "reactivate" page to get it out of the way (it
742 			 *     will eventually drift back into the inactive
743 			 *     queue for a retry).
744 			 *   VM_PAGER_UNLOCK: should never see this as it is
745 			 *     only valid for "get" operations
746 			 */
747 
748 			/* relock p's object: page queues not lock yet, so
749 			 * no need for "try" */
750 
751 			/* !swap_backed case: already locked... */
752 			if (swap_backed) {
753 				if (anon)
754 					rw_enter(anon->an_lock, RW_WRITE);
755 			}
756 
757 #ifdef DIAGNOSTIC
758 			if (result == VM_PAGER_UNLOCK)
759 				panic("pagedaemon: pageout returned "
760 				    "invalid 'unlock' code");
761 #endif
762 
763 			/* handle PG_WANTED now */
764 			if (p->pg_flags & PG_WANTED)
765 				wakeup(p);
766 
767 			atomic_clearbits_int(&p->pg_flags, PG_BUSY|PG_WANTED);
768 			UVM_PAGE_OWN(p, NULL);
769 
770 			/* released during I/O? Can only happen for anons */
771 			if (p->pg_flags & PG_RELEASED) {
772 				KASSERT(anon != NULL);
773 				/*
774 				 * remove page so we can get nextpg,
775 				 * also zero out anon so we don't use
776 				 * it after the free.
777 				 */
778 				anon->an_page = NULL;
779 				p->uanon = NULL;
780 
781 				rw_exit(anon->an_lock);
782 				uvm_anfree(anon);	/* kills anon */
783 				pmap_page_protect(p, PROT_NONE);
784 				anon = NULL;
785 				uvm_lock_pageq();
786 				nextpg = TAILQ_NEXT(p, pageq);
787 				/* free released page */
788 				uvm_pagefree(p);
789 			} else {	/* page was not released during I/O */
790 				uvm_lock_pageq();
791 				nextpg = TAILQ_NEXT(p, pageq);
792 				if (result != VM_PAGER_OK) {
793 					/* pageout was a failure... */
794 					if (result != VM_PAGER_AGAIN)
795 						uvm_pageactivate(p);
796 					pmap_clear_reference(p);
797 					/* XXXCDC: if (swap_backed) FREE p's
798 					 * swap block? */
799 				} else {
800 					/* pageout was a success... */
801 					pmap_clear_reference(p);
802 					pmap_clear_modify(p);
803 					atomic_setbits_int(&p->pg_flags,
804 					    PG_CLEAN);
805 				}
806 			}
807 
808 			/*
809 			 * drop object lock (if there is an object left).   do
810 			 * a safety check of nextpg to make sure it is on the
811 			 * inactive queue (it should be since PG_BUSY pages on
812 			 * the inactive queue can't be re-queued [note: not
813 			 * true for active queue]).
814 			 */
815 			if (anon)
816 				rw_exit(anon->an_lock);
817 
818 			if (nextpg && (nextpg->pg_flags & PQ_INACTIVE) == 0) {
819 				nextpg = TAILQ_FIRST(pglst);	/* reload! */
820 			}
821 		} else {
822 			/*
823 			 * if p is null in this loop, make sure it stays null
824 			 * in the next loop.
825 			 */
826 			nextpg = NULL;
827 
828 			/*
829 			 * lock page queues here just so they're always locked
830 			 * at the end of the loop.
831 			 */
832 			uvm_lock_pageq();
833 		}
834 	}
835 	return (retval);
836 }
837 
838 /*
839  * uvmpd_scan: scan the page queues and attempt to meet our targets.
840  *
841  * => called with pageq's locked
842  */
843 
844 void
845 uvmpd_scan(void)
846 {
847 	int free, inactive_shortage, swap_shortage, pages_freed;
848 	struct vm_page *p, *nextpg;
849 	struct uvm_object *uobj;
850 	boolean_t got_it;
851 
852 	MUTEX_ASSERT_LOCKED(&uvm.pageqlock);
853 
854 	uvmexp.pdrevs++;		/* counter */
855 	uobj = NULL;
856 
857 	/*
858 	 * get current "free" page count
859 	 */
860 	free = uvmexp.free - BUFPAGES_DEFICIT;
861 
862 #ifndef __SWAP_BROKEN
863 	/*
864 	 * swap out some processes if we are below our free target.
865 	 * we need to unlock the page queues for this.
866 	 */
867 	if (free < uvmexp.freetarg) {
868 		uvmexp.pdswout++;
869 		uvm_unlock_pageq();
870 		uvm_swapout_threads();
871 		uvm_lock_pageq();
872 	}
873 #endif
874 
875 	/*
876 	 * now we want to work on meeting our targets.   first we work on our
877 	 * free target by converting inactive pages into free pages.  then
878 	 * we work on meeting our inactive target by converting active pages
879 	 * to inactive ones.
880 	 */
881 
882 	/*
883 	 * alternate starting queue between swap and object based on the
884 	 * low bit of uvmexp.pdrevs (which we bump by one each call).
885 	 */
886 	got_it = FALSE;
887 	pages_freed = uvmexp.pdfreed;	/* XXX - int */
888 	if ((uvmexp.pdrevs & 1) != 0 && uvmexp.nswapdev != 0)
889 		got_it = uvmpd_scan_inactive(&uvm.page_inactive_swp);
890 	if (!got_it)
891 		got_it = uvmpd_scan_inactive(&uvm.page_inactive_obj);
892 	if (!got_it && (uvmexp.pdrevs & 1) == 0 && uvmexp.nswapdev != 0)
893 		(void) uvmpd_scan_inactive(&uvm.page_inactive_swp);
894 	pages_freed = uvmexp.pdfreed - pages_freed;
895 
896 	/*
897 	 * we have done the scan to get free pages.   now we work on meeting
898 	 * our inactive target.
899 	 */
900 	inactive_shortage = uvmexp.inactarg - uvmexp.inactive - BUFPAGES_INACT;
901 
902 	/*
903 	 * detect if we're not going to be able to page anything out
904 	 * until we free some swap resources from active pages.
905 	 */
906 	swap_shortage = 0;
907 	if (uvmexp.free < uvmexp.freetarg &&
908 	    uvmexp.swpginuse == uvmexp.swpages &&
909 	    !uvm_swapisfull() &&
910 	    pages_freed == 0) {
911 		swap_shortage = uvmexp.freetarg - uvmexp.free;
912 	}
913 
914 	for (p = TAILQ_FIRST(&uvm.page_active);
915 	     p != NULL && (inactive_shortage > 0 || swap_shortage > 0);
916 	     p = nextpg) {
917 		nextpg = TAILQ_NEXT(p, pageq);
918 
919 		/* skip this page if it's busy. */
920 		if (p->pg_flags & PG_BUSY)
921 			continue;
922 
923 		if (p->pg_flags & PQ_ANON) {
924 			KASSERT(p->uanon != NULL);
925 			if (rw_enter(p->uanon->an_lock, RW_WRITE|RW_NOSLEEP))
926 				continue;
927 		} else
928 			KASSERT(p->uobject != NULL);
929 
930 		/*
931 		 * if there's a shortage of swap, free any swap allocated
932 		 * to this page so that other pages can be paged out.
933 		 */
934 		if (swap_shortage > 0) {
935 			if ((p->pg_flags & PQ_ANON) && p->uanon->an_swslot) {
936 				uvm_swap_free(p->uanon->an_swslot, 1);
937 				p->uanon->an_swslot = 0;
938 				atomic_clearbits_int(&p->pg_flags, PG_CLEAN);
939 				swap_shortage--;
940 			}
941 			if (p->pg_flags & PQ_AOBJ) {
942 				int slot = uao_set_swslot(p->uobject,
943 					p->offset >> PAGE_SHIFT, 0);
944 				if (slot) {
945 					uvm_swap_free(slot, 1);
946 					atomic_clearbits_int(&p->pg_flags,
947 					    PG_CLEAN);
948 					swap_shortage--;
949 				}
950 			}
951 		}
952 
953 		/*
954 		 * deactivate this page if there's a shortage of
955 		 * inactive pages.
956 		 */
957 		if (inactive_shortage > 0) {
958 			pmap_page_protect(p, PROT_NONE);
959 			/* no need to check wire_count as pg is "active" */
960 			uvm_pagedeactivate(p);
961 			uvmexp.pddeact++;
962 			inactive_shortage--;
963 		}
964 		if (p->pg_flags & PQ_ANON)
965 			rw_exit(p->uanon->an_lock);
966 	}
967 }
968 
969 #ifdef HIBERNATE
970 
971 /*
972  * uvmpd_drop: drop clean pages from list
973  */
974 void
975 uvmpd_drop(struct pglist *pglst)
976 {
977 	struct vm_page *p, *nextpg;
978 
979 	for (p = TAILQ_FIRST(pglst); p != NULL; p = nextpg) {
980 		nextpg = TAILQ_NEXT(p, pageq);
981 
982 		if (p->pg_flags & PQ_ANON || p->uobject == NULL)
983 			continue;
984 
985 		if (p->pg_flags & PG_BUSY)
986 			continue;
987 
988 		if (p->pg_flags & PG_CLEAN) {
989 			/*
990 			 * we now have the page queues locked.
991 			 * the page is not busy.   if the page is clean we
992 			 * can free it now and continue.
993 			 */
994 			if (p->pg_flags & PG_CLEAN) {
995 				if (p->pg_flags & PQ_SWAPBACKED) {
996 					/* this page now lives only in swap */
997 					uvmexp.swpgonly++;
998 				}
999 
1000 				/* zap all mappings with pmap_page_protect... */
1001 				pmap_page_protect(p, PROT_NONE);
1002 				uvm_pagefree(p);
1003 			}
1004 		}
1005 	}
1006 }
1007 
1008 void
1009 uvmpd_hibernate(void)
1010 {
1011 	uvm_lock_pageq();
1012 
1013 	uvmpd_drop(&uvm.page_inactive_swp);
1014 	uvmpd_drop(&uvm.page_inactive_obj);
1015 	uvmpd_drop(&uvm.page_active);
1016 
1017 	uvm_unlock_pageq();
1018 }
1019 
1020 #endif
1021