xref: /netbsd-src/sys/uvm/uvm_pdaemon.c (revision 17dd36da8292193180754d5047c0926dbb56818c)
1 /*	$NetBSD: uvm_pdaemon.c,v 1.31 2001/03/10 22:46:50 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 static void		uvmpd_scan __P((void));
100 static boolean_t	uvmpd_scan_inactive __P((struct pglist *));
101 static 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) {
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 static void
164 uvmpd_tune()
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 	(void) spl0();
208 	uvm_lock_pageq();
209 	npages = uvmexp.npages;
210 	uvmpd_tune();
211 	uvm_unlock_pageq();
212 
213 	/*
214 	 * main loop
215 	 */
216 
217 	for (;;) {
218 		simple_lock(&uvm.pagedaemon_lock);
219 
220 		UVMHIST_LOG(pdhist,"  <<SLEEPING>>",0,0,0,0);
221 		UVM_UNLOCK_AND_WAIT(&uvm.pagedaemon,
222 		    &uvm.pagedaemon_lock, FALSE, "pgdaemon", 0);
223 		uvmexp.pdwoke++;
224 		UVMHIST_LOG(pdhist,"  <<WOKE UP>>",0,0,0,0);
225 
226 		/* drain pool resources */
227 		pool_drain(0);
228 
229 		/*
230 		 * now lock page queues and recompute inactive count
231 		 */
232 
233 		uvm_lock_pageq();
234 		if (npages != uvmexp.npages) {	/* check for new pages? */
235 			npages = uvmexp.npages;
236 			uvmpd_tune();
237 		}
238 
239 		uvmexp.inactarg = (uvmexp.active + uvmexp.inactive) / 3;
240 		if (uvmexp.inactarg <= uvmexp.freetarg) {
241 			uvmexp.inactarg = uvmexp.freetarg + 1;
242 		}
243 
244 		UVMHIST_LOG(pdhist,"  free/ftarg=%d/%d, inact/itarg=%d/%d",
245 		    uvmexp.free, uvmexp.freetarg, uvmexp.inactive,
246 		    uvmexp.inactarg);
247 
248 		/*
249 		 * scan if needed
250 		 */
251 
252 		if (uvmexp.free + uvmexp.paging < uvmexp.freetarg ||
253 		    uvmexp.inactive < uvmexp.inactarg) {
254 			uvmpd_scan();
255 		}
256 
257 		/*
258 		 * if there's any free memory to be had,
259 		 * wake up any waiters.
260 		 */
261 
262 		if (uvmexp.free > uvmexp.reserve_kernel ||
263 		    uvmexp.paging == 0) {
264 			wakeup(&uvmexp.free);
265 		}
266 
267 		/*
268 		 * scan done.  unlock page queues (the only lock we are holding)
269 		 */
270 
271 		uvm_unlock_pageq();
272 	}
273 	/*NOTREACHED*/
274 }
275 
276 
277 /*
278  * uvm_aiodone_daemon:  main loop for the aiodone daemon.
279  */
280 
281 void
282 uvm_aiodone_daemon(void *arg)
283 {
284 	int s, free;
285 	struct buf *bp, *nbp;
286 	UVMHIST_FUNC("uvm_aiodoned"); UVMHIST_CALLED(pdhist);
287 
288 	for (;;) {
289 
290 		/*
291 		 * carefully attempt to go to sleep (without losing "wakeups"!).
292 		 * we need splbio because we want to make sure the aio_done list
293 		 * is totally empty before we go to sleep.
294 		 */
295 
296 		s = splbio();
297 		simple_lock(&uvm.aiodoned_lock);
298 		if (TAILQ_FIRST(&uvm.aio_done) == NULL) {
299 			UVMHIST_LOG(pdhist,"  <<SLEEPING>>",0,0,0,0);
300 			UVM_UNLOCK_AND_WAIT(&uvm.aiodoned,
301 			    &uvm.aiodoned_lock, FALSE, "aiodoned", 0);
302 			UVMHIST_LOG(pdhist,"  <<WOKE UP>>",0,0,0,0);
303 
304 			/* relock aiodoned_lock, still at splbio */
305 			simple_lock(&uvm.aiodoned_lock);
306 		}
307 
308 		/*
309 		 * check for done aio structures
310 		 */
311 
312 		bp = TAILQ_FIRST(&uvm.aio_done);
313 		if (bp) {
314 			TAILQ_INIT(&uvm.aio_done);
315 		}
316 
317 		simple_unlock(&uvm.aiodoned_lock);
318 		splx(s);
319 
320 		/*
321 		 * process each i/o that's done.
322 		 */
323 
324 		free = uvmexp.free;
325 		while (bp != NULL) {
326 			if (bp->b_flags & B_PDAEMON) {
327 				uvmexp.paging -= bp->b_bufsize >> PAGE_SHIFT;
328 			}
329 			nbp = TAILQ_NEXT(bp, b_freelist);
330 			(*bp->b_iodone)(bp);
331 			bp = nbp;
332 		}
333 		if (free <= uvmexp.reserve_kernel) {
334 			s = uvm_lock_fpageq();
335 			wakeup(&uvm.pagedaemon);
336 			uvm_unlock_fpageq(s);
337 		} else {
338 			simple_lock(&uvm.pagedaemon_lock);
339 			wakeup(&uvmexp.free);
340 			simple_unlock(&uvm.pagedaemon_lock);
341 		}
342 	}
343 }
344 
345 
346 
347 /*
348  * uvmpd_scan_inactive: scan an inactive list for pages to clean or free.
349  *
350  * => called with page queues locked
351  * => we work on meeting our free target by converting inactive pages
352  *    into free pages.
353  * => we handle the building of swap-backed clusters
354  * => we return TRUE if we are exiting because we met our target
355  */
356 
357 static boolean_t
358 uvmpd_scan_inactive(pglst)
359 	struct pglist *pglst;
360 {
361 	boolean_t retval = FALSE;	/* assume we haven't hit target */
362 	int s, free, result;
363 	struct vm_page *p, *nextpg;
364 	struct uvm_object *uobj;
365 	struct vm_page *pps[MAXBSIZE >> PAGE_SHIFT], **ppsp;
366 	int npages;
367 	struct vm_page *swpps[MAXBSIZE >> PAGE_SHIFT]; 	/* XXX: see below */
368 	int swnpages, swcpages;				/* XXX: see below */
369 	int swslot;
370 	struct vm_anon *anon;
371 	boolean_t swap_backed;
372 	vaddr_t start;
373 	int dirtyreacts, t;
374 	UVMHIST_FUNC("uvmpd_scan_inactive"); UVMHIST_CALLED(pdhist);
375 
376 	/*
377 	 * note: we currently keep swap-backed pages on a seperate inactive
378 	 * list from object-backed pages.   however, merging the two lists
379 	 * back together again hasn't been ruled out.   thus, we keep our
380 	 * swap cluster in "swpps" rather than in pps (allows us to mix
381 	 * clustering types in the event of a mixed inactive queue).
382 	 */
383 
384 	/*
385 	 * swslot is non-zero if we are building a swap cluster.  we want
386 	 * to stay in the loop while we have a page to scan or we have
387 	 * a swap-cluster to build.
388 	 */
389 
390 	swslot = 0;
391 	swnpages = swcpages = 0;
392 	free = 0;
393 	dirtyreacts = 0;
394 
395 	for (p = TAILQ_FIRST(pglst); p != NULL || swslot != 0; p = nextpg) {
396 
397 		/*
398 		 * note that p can be NULL iff we have traversed the whole
399 		 * list and need to do one final swap-backed clustered pageout.
400 		 */
401 
402 		uobj = NULL;
403 		anon = NULL;
404 
405 		if (p) {
406 
407 			/*
408 			 * update our copy of "free" and see if we've met
409 			 * our target
410 			 */
411 
412 			s = uvm_lock_fpageq();
413 			free = uvmexp.free;
414 			uvm_unlock_fpageq(s);
415 
416 			if (free + uvmexp.paging >= uvmexp.freetarg << 2 ||
417 			    dirtyreacts == UVMPD_NUMDIRTYREACTS) {
418 				UVMHIST_LOG(pdhist,"  met free target: "
419 					    "exit loop", 0, 0, 0, 0);
420 				retval = TRUE;
421 
422 				if (swslot == 0) {
423 					/* exit now if no swap-i/o pending */
424 					break;
425 				}
426 
427 				/* set p to null to signal final swap i/o */
428 				p = NULL;
429 			}
430 		}
431 
432 		if (p) {	/* if (we have a new page to consider) */
433 
434 			/*
435 			 * we are below target and have a new page to consider.
436 			 */
437 			uvmexp.pdscans++;
438 			nextpg = TAILQ_NEXT(p, pageq);
439 
440 			/*
441 			 * move referenced pages back to active queue and
442 			 * skip to next page.
443 			 */
444 
445 			if (pmap_is_referenced(p)) {
446 				uvm_pageactivate(p);
447 				uvmexp.pdreact++;
448 				continue;
449 			}
450 
451 			/*
452 			 * enforce the minimum thresholds on different
453 			 * types of memory usage.  if reusing the current
454 			 * page would reduce that type of usage below its
455 			 * minimum, reactivate the page instead and move
456 			 * on to the next page.
457 			 */
458 
459 			t = uvmexp.active + uvmexp.inactive + uvmexp.free;
460 			if (p->uanon &&
461 			    uvmexp.anonpages <= (t * uvmexp.anonmin) >> 8) {
462 				uvm_pageactivate(p);
463 				uvmexp.pdreanon++;
464 				continue;
465 			}
466 			if (p->uobject && UVM_OBJ_IS_VTEXT(p->uobject) &&
467 			    uvmexp.vtextpages <= (t * uvmexp.vtextmin) >> 8) {
468 				uvm_pageactivate(p);
469 				uvmexp.pdrevtext++;
470 				continue;
471 			}
472 			if (p->uobject && UVM_OBJ_IS_VNODE(p->uobject) &&
473 			    !UVM_OBJ_IS_VTEXT(p->uobject) &&
474 			    uvmexp.vnodepages <= (t * uvmexp.vnodemin) >> 8) {
475 				uvm_pageactivate(p);
476 				uvmexp.pdrevnode++;
477 				continue;
478 			}
479 
480 			/*
481 			 * first we attempt to lock the object that this page
482 			 * belongs to.  if our attempt fails we skip on to
483 			 * the next page (no harm done).  it is important to
484 			 * "try" locking the object as we are locking in the
485 			 * wrong order (pageq -> object) and we don't want to
486 			 * deadlock.
487 			 *
488 			 * the only time we expect to see an ownerless page
489 			 * (i.e. a page with no uobject and !PQ_ANON) is if an
490 			 * anon has loaned a page from a uvm_object and the
491 			 * uvm_object has dropped the ownership.  in that
492 			 * case, the anon can "take over" the loaned page
493 			 * and make it its own.
494 			 */
495 
496 			/* is page part of an anon or ownerless ? */
497 			if ((p->pqflags & PQ_ANON) || p->uobject == NULL) {
498 				anon = p->uanon;
499 				KASSERT(anon != NULL);
500 				if (!simple_lock_try(&anon->an_lock)) {
501 					/* lock failed, skip this page */
502 					continue;
503 				}
504 
505 				/*
506 				 * if the page is ownerless, claim it in the
507 				 * name of "anon"!
508 				 */
509 
510 				if ((p->pqflags & PQ_ANON) == 0) {
511 					KASSERT(p->loan_count > 0);
512 					p->loan_count--;
513 					p->pqflags |= PQ_ANON;
514 					/* anon now owns it */
515 				}
516 				if (p->flags & PG_BUSY) {
517 					simple_unlock(&anon->an_lock);
518 					uvmexp.pdbusy++;
519 					/* someone else owns page, skip it */
520 					continue;
521 				}
522 				uvmexp.pdanscan++;
523 			} else {
524 				uobj = p->uobject;
525 				KASSERT(uobj != NULL);
526 				if (!simple_lock_try(&uobj->vmobjlock)) {
527 					/* lock failed, skip this page */
528 					continue;
529 				}
530 				if (p->flags & PG_BUSY) {
531 					simple_unlock(&uobj->vmobjlock);
532 					uvmexp.pdbusy++;
533 					/* someone else owns page, skip it */
534 					continue;
535 				}
536 				uvmexp.pdobscan++;
537 			}
538 
539 			/*
540 			 * we now have the object and the page queues locked.
541 			 * the page is not busy.  remove all the permissions
542 			 * from the page so we can sync the modified info
543 			 * without any race conditions.  if the page is clean
544 			 * we can free it now and continue.
545 			 */
546 
547 			pmap_page_protect(p, VM_PROT_NONE);
548 			if ((p->flags & PG_CLEAN) != 0 && pmap_is_modified(p)) {
549 				p->flags &= ~PG_CLEAN;
550 			}
551 
552 			if (p->flags & PG_CLEAN) {
553 				if (p->pqflags & PQ_SWAPBACKED) {
554 					/* this page now lives only in swap */
555 					simple_lock(&uvm.swap_data_lock);
556 					uvmexp.swpgonly++;
557 					simple_unlock(&uvm.swap_data_lock);
558 				}
559 
560 				uvm_pagefree(p);
561 				uvmexp.pdfreed++;
562 
563 				if (anon) {
564 
565 					/*
566 					 * an anonymous page can only be clean
567 					 * if it has backing store assigned.
568 					 */
569 
570 					KASSERT(anon->an_swslot != 0);
571 
572 					/* remove from object */
573 					anon->u.an_page = NULL;
574 					simple_unlock(&anon->an_lock);
575 				} else {
576 					/* pagefree has already removed the
577 					 * page from the object */
578 					simple_unlock(&uobj->vmobjlock);
579 				}
580 				continue;
581 			}
582 
583 			/*
584 			 * this page is dirty, skip it if we'll have met our
585 			 * free target when all the current pageouts complete.
586 			 */
587 
588 			if (free + uvmexp.paging > uvmexp.freetarg << 2) {
589 				if (anon) {
590 					simple_unlock(&anon->an_lock);
591 				} else {
592 					simple_unlock(&uobj->vmobjlock);
593 				}
594 				continue;
595 			}
596 
597 			/*
598 			 * this page is dirty, but we can't page it out
599 			 * since all pages in swap are only in swap.
600 			 * reactivate it so that we eventually cycle
601 			 * all pages thru the inactive queue.
602 			 */
603 
604 			KASSERT(uvmexp.swpgonly <= uvmexp.swpages);
605 			if ((p->pqflags & PQ_SWAPBACKED) &&
606 			    uvmexp.swpgonly == uvmexp.swpages) {
607 				dirtyreacts++;
608 				uvm_pageactivate(p);
609 				if (anon) {
610 					simple_unlock(&anon->an_lock);
611 				} else {
612 					simple_unlock(&uobj->vmobjlock);
613 				}
614 				continue;
615 			}
616 
617 			/*
618 			 * if the page is swap-backed and dirty and swap space
619 			 * is full, free any swap allocated to the page
620 			 * so that other pages can be paged out.
621 			 */
622 
623 			KASSERT(uvmexp.swpginuse <= uvmexp.swpages);
624 			if ((p->pqflags & PQ_SWAPBACKED) &&
625 			    uvmexp.swpginuse == uvmexp.swpages) {
626 
627 				if ((p->pqflags & PQ_ANON) &&
628 				    p->uanon->an_swslot) {
629 					uvm_swap_free(p->uanon->an_swslot, 1);
630 					p->uanon->an_swslot = 0;
631 				}
632 				if (p->pqflags & PQ_AOBJ) {
633 					uao_dropswap(p->uobject,
634 						     p->offset >> PAGE_SHIFT);
635 				}
636 			}
637 
638 			/*
639 			 * the page we are looking at is dirty.   we must
640 			 * clean it before it can be freed.  to do this we
641 			 * first mark the page busy so that no one else will
642 			 * touch the page.
643 			 */
644 
645 			swap_backed = ((p->pqflags & PQ_SWAPBACKED) != 0);
646 			p->flags |= PG_BUSY;		/* now we own it */
647 			UVM_PAGE_OWN(p, "scan_inactive");
648 			uvmexp.pgswapout++;
649 
650 			/*
651 			 * for swap-backed pages we need to (re)allocate
652 			 * swap space.
653 			 */
654 
655 			if (swap_backed) {
656 
657 				/*
658 				 * free old swap slot (if any)
659 				 */
660 
661 				if (anon) {
662 					if (anon->an_swslot) {
663 						uvm_swap_free(anon->an_swslot,
664 						    1);
665 						anon->an_swslot = 0;
666 					}
667 				} else {
668 					uao_dropswap(uobj,
669 						     p->offset >> PAGE_SHIFT);
670 				}
671 
672 				/*
673 				 * start new cluster (if necessary)
674 				 */
675 
676 				if (swslot == 0) {
677 					swnpages = MAXBSIZE >> PAGE_SHIFT;
678 					swslot = uvm_swap_alloc(&swnpages,
679 					    TRUE);
680 					if (swslot == 0) {
681 						/* no swap?  give up! */
682 						p->flags &= ~PG_BUSY;
683 						UVM_PAGE_OWN(p, NULL);
684 						if (anon)
685 							simple_unlock(
686 							    &anon->an_lock);
687 						else
688 							simple_unlock(
689 							    &uobj->vmobjlock);
690 						continue;
691 					}
692 					swcpages = 0;	/* cluster is empty */
693 				}
694 
695 				/*
696 				 * add block to cluster
697 				 */
698 
699 				swpps[swcpages] = p;
700 				if (anon)
701 					anon->an_swslot = swslot + swcpages;
702 				else
703 					uao_set_swslot(uobj,
704 					    p->offset >> PAGE_SHIFT,
705 					    swslot + swcpages);
706 				swcpages++;
707 			}
708 		} else {
709 
710 			/* if p == NULL we must be doing a last swap i/o */
711 			swap_backed = TRUE;
712 		}
713 
714 		/*
715 		 * now consider doing the pageout.
716 		 *
717 		 * for swap-backed pages, we do the pageout if we have either
718 		 * filled the cluster (in which case (swnpages == swcpages) or
719 		 * run out of pages (p == NULL).
720 		 *
721 		 * for object pages, we always do the pageout.
722 		 */
723 
724 		if (swap_backed) {
725 			if (p) {	/* if we just added a page to cluster */
726 				if (anon)
727 					simple_unlock(&anon->an_lock);
728 				else
729 					simple_unlock(&uobj->vmobjlock);
730 
731 				/* cluster not full yet? */
732 				if (swcpages < swnpages)
733 					continue;
734 			}
735 
736 			/* starting I/O now... set up for it */
737 			npages = swcpages;
738 			ppsp = swpps;
739 			/* for swap-backed pages only */
740 			start = (vaddr_t) swslot;
741 
742 			/* if this is final pageout we could have a few
743 			 * extra swap blocks */
744 			if (swcpages < swnpages) {
745 				uvm_swap_free(swslot + swcpages,
746 				    (swnpages - swcpages));
747 			}
748 		} else {
749 			/* normal object pageout */
750 			ppsp = pps;
751 			npages = sizeof(pps) / sizeof(struct vm_page *);
752 			/* not looked at because PGO_ALLPAGES is set */
753 			start = 0;
754 		}
755 
756 		/*
757 		 * now do the pageout.
758 		 *
759 		 * for swap_backed pages we have already built the cluster.
760 		 * for !swap_backed pages, uvm_pager_put will call the object's
761 		 * "make put cluster" function to build a cluster on our behalf.
762 		 *
763 		 * we pass the PGO_PDFREECLUST flag to uvm_pager_put to instruct
764 		 * it to free the cluster pages for us on a successful I/O (it
765 		 * always does this for un-successful I/O requests).  this
766 		 * allows us to do clustered pageout without having to deal
767 		 * with cluster pages at this level.
768 		 *
769 		 * note locking semantics of uvm_pager_put with PGO_PDFREECLUST:
770 		 *  IN: locked: uobj (if !swap_backed), page queues
771 		 * OUT:!locked: pageqs, uobj
772 		 */
773 
774 		/* locked: uobj (if !swap_backed), page queues */
775 		uvmexp.pdpageouts++;
776 		result = uvm_pager_put(swap_backed ? NULL : uobj, p,
777 		    &ppsp, &npages, PGO_ALLPAGES|PGO_PDFREECLUST, start, 0);
778 		/* unlocked: pageqs, uobj */
779 
780 		/*
781 		 * if we did i/o to swap, zero swslot to indicate that we are
782 		 * no longer building a swap-backed cluster.
783 		 */
784 
785 		if (swap_backed)
786 			swslot = 0;		/* done with this cluster */
787 
788 		/*
789 		 * if the pageout failed, reactivate the page and continue.
790 		 */
791 
792 		if (result == EIO && curproc == uvm.pagedaemon_proc) {
793 			uvm_lock_pageq();
794 			nextpg = TAILQ_NEXT(p, pageq);
795 			uvm_pageactivate(p);
796 			continue;
797 		}
798 
799 		/*
800 		 * the pageout is in progress.  bump counters and set up
801 		 * for the next loop.
802 		 */
803 
804 		uvm_lock_pageq();
805 		uvmexp.paging += npages;
806 		uvmexp.pdpending++;
807 		if (p) {
808 			if (p->pqflags & PQ_INACTIVE)
809 				nextpg = TAILQ_NEXT(p, pageq);
810 			else
811 				nextpg = TAILQ_FIRST(pglst);
812 		} else {
813 			nextpg = NULL;
814 		}
815 	}
816 	return (retval);
817 }
818 
819 /*
820  * uvmpd_scan: scan the page queues and attempt to meet our targets.
821  *
822  * => called with pageq's locked
823  */
824 
825 void
826 uvmpd_scan()
827 {
828 	int s, free, inactive_shortage, swap_shortage, pages_freed;
829 	struct vm_page *p, *nextpg;
830 	struct uvm_object *uobj;
831 	boolean_t got_it;
832 	UVMHIST_FUNC("uvmpd_scan"); UVMHIST_CALLED(pdhist);
833 
834 	uvmexp.pdrevs++;		/* counter */
835 	uobj = NULL;
836 
837 	/*
838 	 * get current "free" page count
839 	 */
840 	s = uvm_lock_fpageq();
841 	free = uvmexp.free;
842 	uvm_unlock_fpageq(s);
843 
844 #ifndef __SWAP_BROKEN
845 	/*
846 	 * swap out some processes if we are below our free target.
847 	 * we need to unlock the page queues for this.
848 	 */
849 	if (free < uvmexp.freetarg) {
850 		uvmexp.pdswout++;
851 		UVMHIST_LOG(pdhist,"  free %d < target %d: swapout", free,
852 		    uvmexp.freetarg, 0, 0);
853 		uvm_unlock_pageq();
854 		uvm_swapout_threads();
855 		uvm_lock_pageq();
856 
857 	}
858 #endif
859 
860 	/*
861 	 * now we want to work on meeting our targets.   first we work on our
862 	 * free target by converting inactive pages into free pages.  then
863 	 * we work on meeting our inactive target by converting active pages
864 	 * to inactive ones.
865 	 */
866 
867 	UVMHIST_LOG(pdhist, "  starting 'free' loop",0,0,0,0);
868 
869 	/*
870 	 * alternate starting queue between swap and object based on the
871 	 * low bit of uvmexp.pdrevs (which we bump by one each call).
872 	 */
873 
874 	got_it = FALSE;
875 	pages_freed = uvmexp.pdfreed;
876 	if ((uvmexp.pdrevs & 1) != 0 && uvmexp.nswapdev != 0)
877 		got_it = uvmpd_scan_inactive(&uvm.page_inactive_swp);
878 	if (!got_it)
879 		got_it = uvmpd_scan_inactive(&uvm.page_inactive_obj);
880 	if (!got_it && (uvmexp.pdrevs & 1) == 0 && uvmexp.nswapdev != 0)
881 		(void) uvmpd_scan_inactive(&uvm.page_inactive_swp);
882 	pages_freed = uvmexp.pdfreed - pages_freed;
883 
884 	/*
885 	 * we have done the scan to get free pages.   now we work on meeting
886 	 * our inactive target.
887 	 */
888 
889 	inactive_shortage = uvmexp.inactarg - uvmexp.inactive;
890 
891 	/*
892 	 * detect if we're not going to be able to page anything out
893 	 * until we free some swap resources from active pages.
894 	 */
895 
896 	swap_shortage = 0;
897 	if (uvmexp.free < uvmexp.freetarg &&
898 	    uvmexp.swpginuse == uvmexp.swpages &&
899 	    uvmexp.swpgonly < uvmexp.swpages &&
900 	    pages_freed == 0) {
901 		swap_shortage = uvmexp.freetarg - uvmexp.free;
902 	}
903 
904 	UVMHIST_LOG(pdhist, "  loop 2: inactive_shortage=%d swap_shortage=%d",
905 		    inactive_shortage, swap_shortage,0,0);
906 	for (p = TAILQ_FIRST(&uvm.page_active);
907 	     p != NULL && (inactive_shortage > 0 || swap_shortage > 0);
908 	     p = nextpg) {
909 		nextpg = TAILQ_NEXT(p, pageq);
910 		if (p->flags & PG_BUSY)
911 			continue;	/* quick check before trying to lock */
912 
913 		/*
914 		 * lock the page's owner.
915 		 */
916 		/* is page anon owned or ownerless? */
917 		if ((p->pqflags & PQ_ANON) || p->uobject == NULL) {
918 			KASSERT(p->uanon != NULL);
919 			if (!simple_lock_try(&p->uanon->an_lock))
920 				continue;
921 
922 			/* take over the page? */
923 			if ((p->pqflags & PQ_ANON) == 0) {
924 				KASSERT(p->loan_count > 0);
925 				p->loan_count--;
926 				p->pqflags |= PQ_ANON;
927 			}
928 		} else {
929 			if (!simple_lock_try(&p->uobject->vmobjlock))
930 				continue;
931 		}
932 
933 		/*
934 		 * skip this page if it's busy.
935 		 */
936 
937 		if ((p->flags & PG_BUSY) != 0) {
938 			if (p->pqflags & PQ_ANON)
939 				simple_unlock(&p->uanon->an_lock);
940 			else
941 				simple_unlock(&p->uobject->vmobjlock);
942 			continue;
943 		}
944 
945 		/*
946 		 * if there's a shortage of swap, free any swap allocated
947 		 * to this page so that other pages can be paged out.
948 		 */
949 
950 		if (swap_shortage > 0) {
951 			if ((p->pqflags & PQ_ANON) && p->uanon->an_swslot) {
952 				uvm_swap_free(p->uanon->an_swslot, 1);
953 				p->uanon->an_swslot = 0;
954 				p->flags &= ~PG_CLEAN;
955 				swap_shortage--;
956 			}
957 			if (p->pqflags & PQ_AOBJ) {
958 				int slot = uao_set_swslot(p->uobject,
959 					p->offset >> PAGE_SHIFT, 0);
960 				if (slot) {
961 					uvm_swap_free(slot, 1);
962 					p->flags &= ~PG_CLEAN;
963 					swap_shortage--;
964 				}
965 			}
966 		}
967 
968 		/*
969 		 * If the page has not been referenced since the
970 		 * last scan, deactivate the page if there is a
971 		 * shortage of inactive pages.
972 		 */
973 
974 		if (inactive_shortage > 0 &&
975 		    pmap_clear_reference(p) == FALSE) {
976 			/* no need to check wire_count as pg is "active" */
977 			uvm_pagedeactivate(p);
978 			uvmexp.pddeact++;
979 			inactive_shortage--;
980 		}
981 		if (p->pqflags & PQ_ANON)
982 			simple_unlock(&p->uanon->an_lock);
983 		else
984 			simple_unlock(&p->uobject->vmobjlock);
985 	}
986 }
987