xref: /freebsd/sys/vm/vm_pageout.c (revision 724b4bfdf1306e4f2c451b6d146fe0fe0353b2c8)
1 /*-
2  * Copyright (c) 1991 Regents of the University of California.
3  * All rights reserved.
4  * Copyright (c) 1994 John S. Dyson
5  * All rights reserved.
6  * Copyright (c) 1994 David Greenman
7  * All rights reserved.
8  * Copyright (c) 2005 Yahoo! Technologies Norway AS
9  * All rights reserved.
10  *
11  * This code is derived from software contributed to Berkeley by
12  * The Mach Operating System project at Carnegie-Mellon University.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. All advertising materials mentioning features or use of this software
23  *    must display the following acknowledgement:
24  *	This product includes software developed by the University of
25  *	California, Berkeley and its contributors.
26  * 4. Neither the name of the University nor the names of its contributors
27  *    may be used to endorse or promote products derived from this software
28  *    without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40  * SUCH DAMAGE.
41  *
42  *	from: @(#)vm_pageout.c	7.4 (Berkeley) 5/7/91
43  *
44  *
45  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
46  * All rights reserved.
47  *
48  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
49  *
50  * Permission to use, copy, modify and distribute this software and
51  * its documentation is hereby granted, provided that both the copyright
52  * notice and this permission notice appear in all copies of the
53  * software, derivative works or modified versions, and any portions
54  * thereof, and that both notices appear in supporting documentation.
55  *
56  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
57  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
58  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
59  *
60  * Carnegie Mellon requests users of this software to return to
61  *
62  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
63  *  School of Computer Science
64  *  Carnegie Mellon University
65  *  Pittsburgh PA 15213-3890
66  *
67  * any improvements or extensions that they make and grant Carnegie the
68  * rights to redistribute these changes.
69  */
70 
71 /*
72  *	The proverbial page-out daemon.
73  */
74 
75 #include <sys/cdefs.h>
76 __FBSDID("$FreeBSD$");
77 
78 #include "opt_vm.h"
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/kernel.h>
82 #include <sys/eventhandler.h>
83 #include <sys/lock.h>
84 #include <sys/mutex.h>
85 #include <sys/proc.h>
86 #include <sys/kthread.h>
87 #include <sys/ktr.h>
88 #include <sys/mount.h>
89 #include <sys/racct.h>
90 #include <sys/resourcevar.h>
91 #include <sys/sched.h>
92 #include <sys/signalvar.h>
93 #include <sys/vnode.h>
94 #include <sys/vmmeter.h>
95 #include <sys/sx.h>
96 #include <sys/sysctl.h>
97 
98 #include <vm/vm.h>
99 #include <vm/vm_param.h>
100 #include <vm/vm_object.h>
101 #include <vm/vm_page.h>
102 #include <vm/vm_map.h>
103 #include <vm/vm_pageout.h>
104 #include <vm/vm_pager.h>
105 #include <vm/swap_pager.h>
106 #include <vm/vm_extern.h>
107 #include <vm/uma.h>
108 
109 /*
110  * System initialization
111  */
112 
113 /* the kernel process "vm_pageout"*/
114 static void vm_pageout(void);
115 static int vm_pageout_clean(vm_page_t);
116 static void vm_pageout_scan(int pass);
117 
118 struct proc *pageproc;
119 
120 static struct kproc_desc page_kp = {
121 	"pagedaemon",
122 	vm_pageout,
123 	&pageproc
124 };
125 SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start,
126     &page_kp);
127 
128 #if !defined(NO_SWAPPING)
129 /* the kernel process "vm_daemon"*/
130 static void vm_daemon(void);
131 static struct	proc *vmproc;
132 
133 static struct kproc_desc vm_kp = {
134 	"vmdaemon",
135 	vm_daemon,
136 	&vmproc
137 };
138 SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp);
139 #endif
140 
141 
142 int vm_pages_needed;		/* Event on which pageout daemon sleeps */
143 int vm_pageout_deficit;		/* Estimated number of pages deficit */
144 int vm_pageout_pages_needed;	/* flag saying that the pageout daemon needs pages */
145 
146 #if !defined(NO_SWAPPING)
147 static int vm_pageout_req_swapout;	/* XXX */
148 static int vm_daemon_needed;
149 static struct mtx vm_daemon_mtx;
150 /* Allow for use by vm_pageout before vm_daemon is initialized. */
151 MTX_SYSINIT(vm_daemon, &vm_daemon_mtx, "vm daemon", MTX_DEF);
152 #endif
153 static int vm_max_launder = 32;
154 static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0;
155 static int vm_pageout_full_stats_interval = 0;
156 static int vm_pageout_algorithm=0;
157 static int defer_swap_pageouts=0;
158 static int disable_swap_pageouts=0;
159 
160 #if defined(NO_SWAPPING)
161 static int vm_swap_enabled=0;
162 static int vm_swap_idle_enabled=0;
163 #else
164 static int vm_swap_enabled=1;
165 static int vm_swap_idle_enabled=0;
166 #endif
167 
168 SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm,
169 	CTLFLAG_RW, &vm_pageout_algorithm, 0, "LRU page mgmt");
170 
171 SYSCTL_INT(_vm, OID_AUTO, max_launder,
172 	CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout");
173 
174 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max,
175 	CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length");
176 
177 SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval,
178 	CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan");
179 
180 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval,
181 	CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan");
182 
183 #if defined(NO_SWAPPING)
184 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
185 	CTLFLAG_RD, &vm_swap_enabled, 0, "Enable entire process swapout");
186 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
187 	CTLFLAG_RD, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
188 #else
189 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
190 	CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout");
191 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
192 	CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
193 #endif
194 
195 SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts,
196 	CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem");
197 
198 SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts,
199 	CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages");
200 
201 static int pageout_lock_miss;
202 SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss,
203 	CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout");
204 
205 #define VM_PAGEOUT_PAGE_COUNT 16
206 int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT;
207 
208 int vm_page_max_wired;		/* XXX max # of wired pages system-wide */
209 SYSCTL_INT(_vm, OID_AUTO, max_wired,
210 	CTLFLAG_RW, &vm_page_max_wired, 0, "System-wide limit to wired page count");
211 
212 static boolean_t vm_pageout_fallback_object_lock(vm_page_t, vm_page_t *);
213 static boolean_t vm_pageout_launder(int, int, vm_paddr_t, vm_paddr_t);
214 #if !defined(NO_SWAPPING)
215 static void vm_pageout_map_deactivate_pages(vm_map_t, long);
216 static void vm_pageout_object_deactivate_pages(pmap_t, vm_object_t, long);
217 static void vm_req_vmdaemon(int req);
218 #endif
219 static boolean_t vm_pageout_page_lock(vm_page_t, vm_page_t *);
220 static void vm_pageout_page_stats(void);
221 static void vm_pageout_requeue(vm_page_t m);
222 
223 /*
224  * Initialize a dummy page for marking the caller's place in the specified
225  * paging queue.  In principle, this function only needs to set the flag
226  * PG_MARKER.  Nonetheless, it sets the flag VPO_BUSY and initializes the hold
227  * count to one as safety precautions.
228  */
229 static void
230 vm_pageout_init_marker(vm_page_t marker, u_short queue)
231 {
232 
233 	bzero(marker, sizeof(*marker));
234 	marker->flags = PG_MARKER;
235 	marker->oflags = VPO_BUSY;
236 	marker->queue = queue;
237 	marker->hold_count = 1;
238 }
239 
240 /*
241  * vm_pageout_fallback_object_lock:
242  *
243  * Lock vm object currently associated with `m'. VM_OBJECT_TRYLOCK is
244  * known to have failed and page queue must be either PQ_ACTIVE or
245  * PQ_INACTIVE.  To avoid lock order violation, unlock the page queues
246  * while locking the vm object.  Use marker page to detect page queue
247  * changes and maintain notion of next page on page queue.  Return
248  * TRUE if no changes were detected, FALSE otherwise.  vm object is
249  * locked on return.
250  *
251  * This function depends on both the lock portion of struct vm_object
252  * and normal struct vm_page being type stable.
253  */
254 static boolean_t
255 vm_pageout_fallback_object_lock(vm_page_t m, vm_page_t *next)
256 {
257 	struct vm_page marker;
258 	boolean_t unchanged;
259 	u_short queue;
260 	vm_object_t object;
261 
262 	queue = m->queue;
263 	vm_pageout_init_marker(&marker, queue);
264 	object = m->object;
265 
266 	TAILQ_INSERT_AFTER(&vm_page_queues[queue].pl,
267 			   m, &marker, pageq);
268 	vm_page_unlock_queues();
269 	vm_page_unlock(m);
270 	VM_OBJECT_LOCK(object);
271 	vm_page_lock(m);
272 	vm_page_lock_queues();
273 
274 	/* Page queue might have changed. */
275 	*next = TAILQ_NEXT(&marker, pageq);
276 	unchanged = (m->queue == queue &&
277 		     m->object == object &&
278 		     &marker == TAILQ_NEXT(m, pageq));
279 	TAILQ_REMOVE(&vm_page_queues[queue].pl,
280 		     &marker, pageq);
281 	return (unchanged);
282 }
283 
284 /*
285  * Lock the page while holding the page queue lock.  Use marker page
286  * to detect page queue changes and maintain notion of next page on
287  * page queue.  Return TRUE if no changes were detected, FALSE
288  * otherwise.  The page is locked on return. The page queue lock might
289  * be dropped and reacquired.
290  *
291  * This function depends on normal struct vm_page being type stable.
292  */
293 static boolean_t
294 vm_pageout_page_lock(vm_page_t m, vm_page_t *next)
295 {
296 	struct vm_page marker;
297 	boolean_t unchanged;
298 	u_short queue;
299 
300 	vm_page_lock_assert(m, MA_NOTOWNED);
301 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
302 
303 	if (vm_page_trylock(m))
304 		return (TRUE);
305 
306 	queue = m->queue;
307 	vm_pageout_init_marker(&marker, queue);
308 
309 	TAILQ_INSERT_AFTER(&vm_page_queues[queue].pl, m, &marker, pageq);
310 	vm_page_unlock_queues();
311 	vm_page_lock(m);
312 	vm_page_lock_queues();
313 
314 	/* Page queue might have changed. */
315 	*next = TAILQ_NEXT(&marker, pageq);
316 	unchanged = (m->queue == queue && &marker == TAILQ_NEXT(m, pageq));
317 	TAILQ_REMOVE(&vm_page_queues[queue].pl, &marker, pageq);
318 	return (unchanged);
319 }
320 
321 /*
322  * vm_pageout_clean:
323  *
324  * Clean the page and remove it from the laundry.
325  *
326  * We set the busy bit to cause potential page faults on this page to
327  * block.  Note the careful timing, however, the busy bit isn't set till
328  * late and we cannot do anything that will mess with the page.
329  */
330 static int
331 vm_pageout_clean(vm_page_t m)
332 {
333 	vm_object_t object;
334 	vm_page_t mc[2*vm_pageout_page_count], pb, ps;
335 	int pageout_count;
336 	int ib, is, page_base;
337 	vm_pindex_t pindex = m->pindex;
338 
339 	vm_page_lock_assert(m, MA_OWNED);
340 	object = m->object;
341 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
342 
343 	/*
344 	 * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP
345 	 * with the new swapper, but we could have serious problems paging
346 	 * out other object types if there is insufficient memory.
347 	 *
348 	 * Unfortunately, checking free memory here is far too late, so the
349 	 * check has been moved up a procedural level.
350 	 */
351 
352 	/*
353 	 * Can't clean the page if it's busy or held.
354 	 */
355 	KASSERT(m->busy == 0 && (m->oflags & VPO_BUSY) == 0,
356 	    ("vm_pageout_clean: page %p is busy", m));
357 	KASSERT(m->hold_count == 0, ("vm_pageout_clean: page %p is held", m));
358 	vm_page_unlock(m);
359 
360 	mc[vm_pageout_page_count] = pb = ps = m;
361 	pageout_count = 1;
362 	page_base = vm_pageout_page_count;
363 	ib = 1;
364 	is = 1;
365 
366 	/*
367 	 * Scan object for clusterable pages.
368 	 *
369 	 * We can cluster ONLY if: ->> the page is NOT
370 	 * clean, wired, busy, held, or mapped into a
371 	 * buffer, and one of the following:
372 	 * 1) The page is inactive, or a seldom used
373 	 *    active page.
374 	 * -or-
375 	 * 2) we force the issue.
376 	 *
377 	 * During heavy mmap/modification loads the pageout
378 	 * daemon can really fragment the underlying file
379 	 * due to flushing pages out of order and not trying
380 	 * align the clusters (which leave sporatic out-of-order
381 	 * holes).  To solve this problem we do the reverse scan
382 	 * first and attempt to align our cluster, then do a
383 	 * forward scan if room remains.
384 	 */
385 more:
386 	while (ib && pageout_count < vm_pageout_page_count) {
387 		vm_page_t p;
388 
389 		if (ib > pindex) {
390 			ib = 0;
391 			break;
392 		}
393 
394 		if ((p = vm_page_prev(pb)) == NULL ||
395 		    (p->oflags & VPO_BUSY) != 0 || p->busy != 0) {
396 			ib = 0;
397 			break;
398 		}
399 		vm_page_lock(p);
400 		vm_page_test_dirty(p);
401 		if (p->dirty == 0 ||
402 		    p->queue != PQ_INACTIVE ||
403 		    p->hold_count != 0) {	/* may be undergoing I/O */
404 			vm_page_unlock(p);
405 			ib = 0;
406 			break;
407 		}
408 		vm_page_unlock(p);
409 		mc[--page_base] = pb = p;
410 		++pageout_count;
411 		++ib;
412 		/*
413 		 * alignment boundry, stop here and switch directions.  Do
414 		 * not clear ib.
415 		 */
416 		if ((pindex - (ib - 1)) % vm_pageout_page_count == 0)
417 			break;
418 	}
419 
420 	while (pageout_count < vm_pageout_page_count &&
421 	    pindex + is < object->size) {
422 		vm_page_t p;
423 
424 		if ((p = vm_page_next(ps)) == NULL ||
425 		    (p->oflags & VPO_BUSY) != 0 || p->busy != 0)
426 			break;
427 		vm_page_lock(p);
428 		vm_page_test_dirty(p);
429 		if (p->dirty == 0 ||
430 		    p->queue != PQ_INACTIVE ||
431 		    p->hold_count != 0) {	/* may be undergoing I/O */
432 			vm_page_unlock(p);
433 			break;
434 		}
435 		vm_page_unlock(p);
436 		mc[page_base + pageout_count] = ps = p;
437 		++pageout_count;
438 		++is;
439 	}
440 
441 	/*
442 	 * If we exhausted our forward scan, continue with the reverse scan
443 	 * when possible, even past a page boundry.  This catches boundry
444 	 * conditions.
445 	 */
446 	if (ib && pageout_count < vm_pageout_page_count)
447 		goto more;
448 
449 	/*
450 	 * we allow reads during pageouts...
451 	 */
452 	return (vm_pageout_flush(&mc[page_base], pageout_count, 0, 0, NULL,
453 	    NULL));
454 }
455 
456 /*
457  * vm_pageout_flush() - launder the given pages
458  *
459  *	The given pages are laundered.  Note that we setup for the start of
460  *	I/O ( i.e. busy the page ), mark it read-only, and bump the object
461  *	reference count all in here rather then in the parent.  If we want
462  *	the parent to do more sophisticated things we may have to change
463  *	the ordering.
464  *
465  *	Returned runlen is the count of pages between mreq and first
466  *	page after mreq with status VM_PAGER_AGAIN.
467  *	*eio is set to TRUE if pager returned VM_PAGER_ERROR or VM_PAGER_FAIL
468  *	for any page in runlen set.
469  */
470 int
471 vm_pageout_flush(vm_page_t *mc, int count, int flags, int mreq, int *prunlen,
472     boolean_t *eio)
473 {
474 	vm_object_t object = mc[0]->object;
475 	int pageout_status[count];
476 	int numpagedout = 0;
477 	int i, runlen;
478 
479 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
480 
481 	/*
482 	 * Initiate I/O.  Bump the vm_page_t->busy counter and
483 	 * mark the pages read-only.
484 	 *
485 	 * We do not have to fixup the clean/dirty bits here... we can
486 	 * allow the pager to do it after the I/O completes.
487 	 *
488 	 * NOTE! mc[i]->dirty may be partial or fragmented due to an
489 	 * edge case with file fragments.
490 	 */
491 	for (i = 0; i < count; i++) {
492 		KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL,
493 		    ("vm_pageout_flush: partially invalid page %p index %d/%d",
494 			mc[i], i, count));
495 		vm_page_io_start(mc[i]);
496 		pmap_remove_write(mc[i]);
497 	}
498 	vm_object_pip_add(object, count);
499 
500 	vm_pager_put_pages(object, mc, count, flags, pageout_status);
501 
502 	runlen = count - mreq;
503 	if (eio != NULL)
504 		*eio = FALSE;
505 	for (i = 0; i < count; i++) {
506 		vm_page_t mt = mc[i];
507 
508 		KASSERT(pageout_status[i] == VM_PAGER_PEND ||
509 		    !pmap_page_is_write_mapped(mt),
510 		    ("vm_pageout_flush: page %p is not write protected", mt));
511 		switch (pageout_status[i]) {
512 		case VM_PAGER_OK:
513 		case VM_PAGER_PEND:
514 			numpagedout++;
515 			break;
516 		case VM_PAGER_BAD:
517 			/*
518 			 * Page outside of range of object. Right now we
519 			 * essentially lose the changes by pretending it
520 			 * worked.
521 			 */
522 			vm_page_undirty(mt);
523 			break;
524 		case VM_PAGER_ERROR:
525 		case VM_PAGER_FAIL:
526 			/*
527 			 * If page couldn't be paged out, then reactivate the
528 			 * page so it doesn't clog the inactive list.  (We
529 			 * will try paging out it again later).
530 			 */
531 			vm_page_lock(mt);
532 			vm_page_activate(mt);
533 			vm_page_unlock(mt);
534 			if (eio != NULL && i >= mreq && i - mreq < runlen)
535 				*eio = TRUE;
536 			break;
537 		case VM_PAGER_AGAIN:
538 			if (i >= mreq && i - mreq < runlen)
539 				runlen = i - mreq;
540 			break;
541 		}
542 
543 		/*
544 		 * If the operation is still going, leave the page busy to
545 		 * block all other accesses. Also, leave the paging in
546 		 * progress indicator set so that we don't attempt an object
547 		 * collapse.
548 		 */
549 		if (pageout_status[i] != VM_PAGER_PEND) {
550 			vm_object_pip_wakeup(object);
551 			vm_page_io_finish(mt);
552 			if (vm_page_count_severe()) {
553 				vm_page_lock(mt);
554 				vm_page_try_to_cache(mt);
555 				vm_page_unlock(mt);
556 			}
557 		}
558 	}
559 	if (prunlen != NULL)
560 		*prunlen = runlen;
561 	return (numpagedout);
562 }
563 
564 static boolean_t
565 vm_pageout_launder(int queue, int tries, vm_paddr_t low, vm_paddr_t high)
566 {
567 	struct mount *mp;
568 	struct vnode *vp;
569 	vm_object_t object;
570 	vm_paddr_t pa;
571 	vm_page_t m, m_tmp, next;
572 
573 	vm_page_lock_queues();
574 	TAILQ_FOREACH_SAFE(m, &vm_page_queues[queue].pl, pageq, next) {
575 		KASSERT(m->queue == queue,
576 		    ("vm_pageout_launder: page %p's queue is not %d", m,
577 		    queue));
578 		if ((m->flags & PG_MARKER) != 0)
579 			continue;
580 		pa = VM_PAGE_TO_PHYS(m);
581 		if (pa < low || pa + PAGE_SIZE > high)
582 			continue;
583 		if (!vm_pageout_page_lock(m, &next) || m->hold_count != 0) {
584 			vm_page_unlock(m);
585 			continue;
586 		}
587 		object = m->object;
588 		if ((!VM_OBJECT_TRYLOCK(object) &&
589 		    (!vm_pageout_fallback_object_lock(m, &next) ||
590 		    m->hold_count != 0)) || (m->oflags & VPO_BUSY) != 0 ||
591 		    m->busy != 0) {
592 			vm_page_unlock(m);
593 			VM_OBJECT_UNLOCK(object);
594 			continue;
595 		}
596 		vm_page_test_dirty(m);
597 		if (m->dirty == 0)
598 			pmap_remove_all(m);
599 		if (m->dirty != 0) {
600 			vm_page_unlock(m);
601 			if (tries == 0 || (object->flags & OBJ_DEAD) != 0) {
602 				VM_OBJECT_UNLOCK(object);
603 				continue;
604 			}
605 			if (object->type == OBJT_VNODE) {
606 				vm_page_unlock_queues();
607 				vp = object->handle;
608 				vm_object_reference_locked(object);
609 				VM_OBJECT_UNLOCK(object);
610 				(void)vn_start_write(vp, &mp, V_WAIT);
611 				vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
612 				VM_OBJECT_LOCK(object);
613 				vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
614 				VM_OBJECT_UNLOCK(object);
615 				VOP_UNLOCK(vp, 0);
616 				vm_object_deallocate(object);
617 				vn_finished_write(mp);
618 				return (TRUE);
619 			} else if (object->type == OBJT_SWAP ||
620 			    object->type == OBJT_DEFAULT) {
621 				vm_page_unlock_queues();
622 				m_tmp = m;
623 				vm_pageout_flush(&m_tmp, 1, VM_PAGER_PUT_SYNC,
624 				    0, NULL, NULL);
625 				VM_OBJECT_UNLOCK(object);
626 				return (TRUE);
627 			}
628 		} else {
629 			vm_page_cache(m);
630 			vm_page_unlock(m);
631 		}
632 		VM_OBJECT_UNLOCK(object);
633 	}
634 	vm_page_unlock_queues();
635 	return (FALSE);
636 }
637 
638 /*
639  * Increase the number of cached pages.  The specified value, "tries",
640  * determines which categories of pages are cached:
641  *
642  *  0: All clean, inactive pages within the specified physical address range
643  *     are cached.  Will not sleep.
644  *  1: The vm_lowmem handlers are called.  All inactive pages within
645  *     the specified physical address range are cached.  May sleep.
646  *  2: The vm_lowmem handlers are called.  All inactive and active pages
647  *     within the specified physical address range are cached.  May sleep.
648  */
649 void
650 vm_pageout_grow_cache(int tries, vm_paddr_t low, vm_paddr_t high)
651 {
652 	int actl, actmax, inactl, inactmax;
653 
654 	if (tries > 0) {
655 		/*
656 		 * Decrease registered cache sizes.  The vm_lowmem handlers
657 		 * may acquire locks and/or sleep, so they can only be invoked
658 		 * when "tries" is greater than zero.
659 		 */
660 		EVENTHANDLER_INVOKE(vm_lowmem, 0);
661 
662 		/*
663 		 * We do this explicitly after the caches have been drained
664 		 * above.
665 		 */
666 		uma_reclaim();
667 	}
668 	inactl = 0;
669 	inactmax = cnt.v_inactive_count;
670 	actl = 0;
671 	actmax = tries < 2 ? 0 : cnt.v_active_count;
672 again:
673 	if (inactl < inactmax && vm_pageout_launder(PQ_INACTIVE, tries, low,
674 	    high)) {
675 		inactl++;
676 		goto again;
677 	}
678 	if (actl < actmax && vm_pageout_launder(PQ_ACTIVE, tries, low, high)) {
679 		actl++;
680 		goto again;
681 	}
682 }
683 
684 #if !defined(NO_SWAPPING)
685 /*
686  *	vm_pageout_object_deactivate_pages
687  *
688  *	Deactivate enough pages to satisfy the inactive target
689  *	requirements.
690  *
691  *	The object and map must be locked.
692  */
693 static void
694 vm_pageout_object_deactivate_pages(pmap_t pmap, vm_object_t first_object,
695     long desired)
696 {
697 	vm_object_t backing_object, object;
698 	vm_page_t p;
699 	int actcount, remove_mode;
700 
701 	VM_OBJECT_LOCK_ASSERT(first_object, MA_OWNED);
702 	if (first_object->type == OBJT_DEVICE ||
703 	    first_object->type == OBJT_SG)
704 		return;
705 	for (object = first_object;; object = backing_object) {
706 		if (pmap_resident_count(pmap) <= desired)
707 			goto unlock_return;
708 		VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
709 		if (object->type == OBJT_PHYS || object->paging_in_progress)
710 			goto unlock_return;
711 
712 		remove_mode = 0;
713 		if (object->shadow_count > 1)
714 			remove_mode = 1;
715 		/*
716 		 * Scan the object's entire memory queue.
717 		 */
718 		TAILQ_FOREACH(p, &object->memq, listq) {
719 			if (pmap_resident_count(pmap) <= desired)
720 				goto unlock_return;
721 			if ((p->oflags & VPO_BUSY) != 0 || p->busy != 0)
722 				continue;
723 			PCPU_INC(cnt.v_pdpages);
724 			vm_page_lock(p);
725 			if (p->wire_count != 0 || p->hold_count != 0 ||
726 			    !pmap_page_exists_quick(pmap, p)) {
727 				vm_page_unlock(p);
728 				continue;
729 			}
730 			actcount = pmap_ts_referenced(p);
731 			if ((p->aflags & PGA_REFERENCED) != 0) {
732 				if (actcount == 0)
733 					actcount = 1;
734 				vm_page_aflag_clear(p, PGA_REFERENCED);
735 			}
736 			if (p->queue != PQ_ACTIVE && actcount != 0) {
737 				vm_page_activate(p);
738 				p->act_count += actcount;
739 			} else if (p->queue == PQ_ACTIVE) {
740 				if (actcount == 0) {
741 					p->act_count -= min(p->act_count,
742 					    ACT_DECLINE);
743 					if (!remove_mode &&
744 					    (vm_pageout_algorithm ||
745 					    p->act_count == 0)) {
746 						pmap_remove_all(p);
747 						vm_page_deactivate(p);
748 					} else {
749 						vm_page_lock_queues();
750 						vm_pageout_requeue(p);
751 						vm_page_unlock_queues();
752 					}
753 				} else {
754 					vm_page_activate(p);
755 					if (p->act_count < ACT_MAX -
756 					    ACT_ADVANCE)
757 						p->act_count += ACT_ADVANCE;
758 					vm_page_lock_queues();
759 					vm_pageout_requeue(p);
760 					vm_page_unlock_queues();
761 				}
762 			} else if (p->queue == PQ_INACTIVE)
763 				pmap_remove_all(p);
764 			vm_page_unlock(p);
765 		}
766 		if ((backing_object = object->backing_object) == NULL)
767 			goto unlock_return;
768 		VM_OBJECT_LOCK(backing_object);
769 		if (object != first_object)
770 			VM_OBJECT_UNLOCK(object);
771 	}
772 unlock_return:
773 	if (object != first_object)
774 		VM_OBJECT_UNLOCK(object);
775 }
776 
777 /*
778  * deactivate some number of pages in a map, try to do it fairly, but
779  * that is really hard to do.
780  */
781 static void
782 vm_pageout_map_deactivate_pages(map, desired)
783 	vm_map_t map;
784 	long desired;
785 {
786 	vm_map_entry_t tmpe;
787 	vm_object_t obj, bigobj;
788 	int nothingwired;
789 
790 	if (!vm_map_trylock(map))
791 		return;
792 
793 	bigobj = NULL;
794 	nothingwired = TRUE;
795 
796 	/*
797 	 * first, search out the biggest object, and try to free pages from
798 	 * that.
799 	 */
800 	tmpe = map->header.next;
801 	while (tmpe != &map->header) {
802 		if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
803 			obj = tmpe->object.vm_object;
804 			if (obj != NULL && VM_OBJECT_TRYLOCK(obj)) {
805 				if (obj->shadow_count <= 1 &&
806 				    (bigobj == NULL ||
807 				     bigobj->resident_page_count < obj->resident_page_count)) {
808 					if (bigobj != NULL)
809 						VM_OBJECT_UNLOCK(bigobj);
810 					bigobj = obj;
811 				} else
812 					VM_OBJECT_UNLOCK(obj);
813 			}
814 		}
815 		if (tmpe->wired_count > 0)
816 			nothingwired = FALSE;
817 		tmpe = tmpe->next;
818 	}
819 
820 	if (bigobj != NULL) {
821 		vm_pageout_object_deactivate_pages(map->pmap, bigobj, desired);
822 		VM_OBJECT_UNLOCK(bigobj);
823 	}
824 	/*
825 	 * Next, hunt around for other pages to deactivate.  We actually
826 	 * do this search sort of wrong -- .text first is not the best idea.
827 	 */
828 	tmpe = map->header.next;
829 	while (tmpe != &map->header) {
830 		if (pmap_resident_count(vm_map_pmap(map)) <= desired)
831 			break;
832 		if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
833 			obj = tmpe->object.vm_object;
834 			if (obj != NULL) {
835 				VM_OBJECT_LOCK(obj);
836 				vm_pageout_object_deactivate_pages(map->pmap, obj, desired);
837 				VM_OBJECT_UNLOCK(obj);
838 			}
839 		}
840 		tmpe = tmpe->next;
841 	}
842 
843 	/*
844 	 * Remove all mappings if a process is swapped out, this will free page
845 	 * table pages.
846 	 */
847 	if (desired == 0 && nothingwired) {
848 		pmap_remove(vm_map_pmap(map), vm_map_min(map),
849 		    vm_map_max(map));
850 	}
851 	vm_map_unlock(map);
852 }
853 #endif		/* !defined(NO_SWAPPING) */
854 
855 /*
856  *	vm_pageout_requeue:
857  *
858  *	Move the specified page to the tail of its present page queue.
859  *
860  *	The page queues must be locked.
861  */
862 static void
863 vm_pageout_requeue(vm_page_t m)
864 {
865 	struct vpgqueues *vpq;
866 
867 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
868 	KASSERT(m->queue != PQ_NONE,
869 	    ("vm_pageout_requeue: page %p is not queued", m));
870 	vpq = &vm_page_queues[m->queue];
871 	TAILQ_REMOVE(&vpq->pl, m, pageq);
872 	TAILQ_INSERT_TAIL(&vpq->pl, m, pageq);
873 }
874 
875 /*
876  *	vm_pageout_scan does the dirty work for the pageout daemon.
877  */
878 static void
879 vm_pageout_scan(int pass)
880 {
881 	vm_page_t m, next;
882 	struct vm_page marker;
883 	int page_shortage, maxscan, pcount;
884 	int addl_page_shortage;
885 	vm_object_t object;
886 	int actcount;
887 	int vnodes_skipped = 0;
888 	int maxlaunder;
889 	boolean_t queues_locked;
890 
891 	/*
892 	 * Decrease registered cache sizes.
893 	 */
894 	EVENTHANDLER_INVOKE(vm_lowmem, 0);
895 	/*
896 	 * We do this explicitly after the caches have been drained above.
897 	 */
898 	uma_reclaim();
899 
900 	/*
901 	 * The addl_page_shortage is the number of temporarily
902 	 * stuck pages in the inactive queue.  In other words, the
903 	 * number of pages from cnt.v_inactive_count that should be
904 	 * discounted in setting the target for the active queue scan.
905 	 */
906 	addl_page_shortage = atomic_readandclear_int(&vm_pageout_deficit);
907 
908 	/*
909 	 * Calculate the number of pages we want to either free or move
910 	 * to the cache.
911 	 */
912 	page_shortage = vm_paging_target() + addl_page_shortage;
913 
914 	vm_pageout_init_marker(&marker, PQ_INACTIVE);
915 
916 	/*
917 	 * Start scanning the inactive queue for pages we can move to the
918 	 * cache or free.  The scan will stop when the target is reached or
919 	 * we have scanned the entire inactive queue.  Note that m->act_count
920 	 * is not used to form decisions for the inactive queue, only for the
921 	 * active queue.
922 	 *
923 	 * maxlaunder limits the number of dirty pages we flush per scan.
924 	 * For most systems a smaller value (16 or 32) is more robust under
925 	 * extreme memory and disk pressure because any unnecessary writes
926 	 * to disk can result in extreme performance degredation.  However,
927 	 * systems with excessive dirty pages (especially when MAP_NOSYNC is
928 	 * used) will die horribly with limited laundering.  If the pageout
929 	 * daemon cannot clean enough pages in the first pass, we let it go
930 	 * all out in succeeding passes.
931 	 */
932 	if ((maxlaunder = vm_max_launder) <= 1)
933 		maxlaunder = 1;
934 	if (pass)
935 		maxlaunder = 10000;
936 	vm_page_lock_queues();
937 	queues_locked = TRUE;
938 	maxscan = cnt.v_inactive_count;
939 
940 	for (m = TAILQ_FIRST(&vm_page_queues[PQ_INACTIVE].pl);
941 	     m != NULL && maxscan-- > 0 && page_shortage > 0;
942 	     m = next) {
943 		KASSERT(queues_locked, ("unlocked queues"));
944 		mtx_assert(&vm_page_queue_mtx, MA_OWNED);
945 		KASSERT(m->queue == PQ_INACTIVE, ("Inactive queue %p", m));
946 
947 		cnt.v_pdpages++;
948 		next = TAILQ_NEXT(m, pageq);
949 
950 		/*
951 		 * skip marker pages
952 		 */
953 		if (m->flags & PG_MARKER)
954 			continue;
955 
956 		KASSERT((m->flags & PG_FICTITIOUS) == 0,
957 		    ("Fictitious page %p cannot be in inactive queue", m));
958 		KASSERT((m->oflags & VPO_UNMANAGED) == 0,
959 		    ("Unmanaged page %p cannot be in inactive queue", m));
960 
961 		/*
962 		 * The page or object lock acquisitions fail if the
963 		 * page was removed from the queue or moved to a
964 		 * different position within the queue.  In either
965 		 * case, addl_page_shortage should not be incremented.
966 		 */
967 		if (!vm_pageout_page_lock(m, &next)) {
968 			vm_page_unlock(m);
969 			continue;
970 		}
971 		object = m->object;
972 		if (!VM_OBJECT_TRYLOCK(object) &&
973 		    !vm_pageout_fallback_object_lock(m, &next)) {
974 			vm_page_unlock(m);
975 			VM_OBJECT_UNLOCK(object);
976 			continue;
977 		}
978 
979 		/*
980 		 * Don't mess with busy pages, keep them at at the
981 		 * front of the queue, most likely they are being
982 		 * paged out.  Increment addl_page_shortage for busy
983 		 * pages, because they may leave the inactive queue
984 		 * shortly after page scan is finished.
985 		 */
986 		if (m->busy != 0 || (m->oflags & VPO_BUSY) != 0) {
987 			vm_page_unlock(m);
988 			VM_OBJECT_UNLOCK(object);
989 			addl_page_shortage++;
990 			continue;
991 		}
992 
993 		/*
994 		 * We unlock vm_page_queue_mtx, invalidating the
995 		 * 'next' pointer.  Use our marker to remember our
996 		 * place.
997 		 */
998 		TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE].pl,
999 		    m, &marker, pageq);
1000 		vm_page_unlock_queues();
1001 		queues_locked = FALSE;
1002 
1003 		/*
1004 		 * If the object is not being used, we ignore previous
1005 		 * references.
1006 		 */
1007 		if (object->ref_count == 0) {
1008 			vm_page_aflag_clear(m, PGA_REFERENCED);
1009 			KASSERT(!pmap_page_is_mapped(m),
1010 			    ("vm_pageout_scan: page %p is mapped", m));
1011 
1012 		/*
1013 		 * Otherwise, if the page has been referenced while in the
1014 		 * inactive queue, we bump the "activation count" upwards,
1015 		 * making it less likely that the page will be added back to
1016 		 * the inactive queue prematurely again.  Here we check the
1017 		 * page tables (or emulated bits, if any), given the upper
1018 		 * level VM system not knowing anything about existing
1019 		 * references.
1020 		 */
1021 		} else if ((m->aflags & PGA_REFERENCED) == 0 &&
1022 		    (actcount = pmap_ts_referenced(m)) != 0) {
1023 			vm_page_activate(m);
1024 			vm_page_unlock(m);
1025 			m->act_count += actcount + ACT_ADVANCE;
1026 			VM_OBJECT_UNLOCK(object);
1027 			goto relock_queues;
1028 		}
1029 
1030 		/*
1031 		 * If the upper level VM system knows about any page
1032 		 * references, we activate the page.  We also set the
1033 		 * "activation count" higher than normal so that we will less
1034 		 * likely place pages back onto the inactive queue again.
1035 		 */
1036 		if ((m->aflags & PGA_REFERENCED) != 0) {
1037 			vm_page_aflag_clear(m, PGA_REFERENCED);
1038 			actcount = pmap_ts_referenced(m);
1039 			vm_page_activate(m);
1040 			vm_page_unlock(m);
1041 			m->act_count += actcount + ACT_ADVANCE + 1;
1042 			VM_OBJECT_UNLOCK(object);
1043 			goto relock_queues;
1044 		}
1045 
1046 		if (m->hold_count != 0) {
1047 			vm_page_unlock(m);
1048 			VM_OBJECT_UNLOCK(object);
1049 
1050 			/*
1051 			 * Held pages are essentially stuck in the
1052 			 * queue.  So, they ought to be discounted
1053 			 * from cnt.v_inactive_count.  See the
1054 			 * calculation of the page_shortage for the
1055 			 * loop over the active queue below.
1056 			 */
1057 			addl_page_shortage++;
1058 			goto relock_queues;
1059 		}
1060 
1061 		/*
1062 		 * If the upper level VM system does not believe that the page
1063 		 * is fully dirty, but it is mapped for write access, then we
1064 		 * consult the pmap to see if the page's dirty status should
1065 		 * be updated.
1066 		 */
1067 		if (m->dirty != VM_PAGE_BITS_ALL &&
1068 		    pmap_page_is_write_mapped(m)) {
1069 			/*
1070 			 * Avoid a race condition: Unless write access is
1071 			 * removed from the page, another processor could
1072 			 * modify it before all access is removed by the call
1073 			 * to vm_page_cache() below.  If vm_page_cache() finds
1074 			 * that the page has been modified when it removes all
1075 			 * access, it panics because it cannot cache dirty
1076 			 * pages.  In principle, we could eliminate just write
1077 			 * access here rather than all access.  In the expected
1078 			 * case, when there are no last instant modifications
1079 			 * to the page, removing all access will be cheaper
1080 			 * overall.
1081 			 */
1082 			if (pmap_is_modified(m))
1083 				vm_page_dirty(m);
1084 			else if (m->dirty == 0)
1085 				pmap_remove_all(m);
1086 		}
1087 
1088 		if (m->valid == 0) {
1089 			/*
1090 			 * Invalid pages can be easily freed
1091 			 */
1092 			vm_page_free(m);
1093 			PCPU_INC(cnt.v_dfree);
1094 			--page_shortage;
1095 		} else if (m->dirty == 0) {
1096 			/*
1097 			 * Clean pages can be placed onto the cache queue.
1098 			 * This effectively frees them.
1099 			 */
1100 			vm_page_cache(m);
1101 			--page_shortage;
1102 		} else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) {
1103 			/*
1104 			 * Dirty pages need to be paged out, but flushing
1105 			 * a page is extremely expensive verses freeing
1106 			 * a clean page.  Rather then artificially limiting
1107 			 * the number of pages we can flush, we instead give
1108 			 * dirty pages extra priority on the inactive queue
1109 			 * by forcing them to be cycled through the queue
1110 			 * twice before being flushed, after which the
1111 			 * (now clean) page will cycle through once more
1112 			 * before being freed.  This significantly extends
1113 			 * the thrash point for a heavily loaded machine.
1114 			 */
1115 			m->flags |= PG_WINATCFLS;
1116 			vm_page_lock_queues();
1117 			queues_locked = TRUE;
1118 			vm_pageout_requeue(m);
1119 		} else if (maxlaunder > 0) {
1120 			/*
1121 			 * We always want to try to flush some dirty pages if
1122 			 * we encounter them, to keep the system stable.
1123 			 * Normally this number is small, but under extreme
1124 			 * pressure where there are insufficient clean pages
1125 			 * on the inactive queue, we may have to go all out.
1126 			 */
1127 			int swap_pageouts_ok;
1128 			struct vnode *vp = NULL;
1129 			struct mount *mp = NULL;
1130 
1131 			if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) {
1132 				swap_pageouts_ok = 1;
1133 			} else {
1134 				swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts);
1135 				swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts &&
1136 				vm_page_count_min());
1137 
1138 			}
1139 
1140 			/*
1141 			 * We don't bother paging objects that are "dead".
1142 			 * Those objects are in a "rundown" state.
1143 			 */
1144 			if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) {
1145 				vm_page_lock_queues();
1146 				vm_page_unlock(m);
1147 				VM_OBJECT_UNLOCK(object);
1148 				queues_locked = TRUE;
1149 				vm_pageout_requeue(m);
1150 				goto relock_queues;
1151 			}
1152 
1153 			/*
1154 			 * The object is already known NOT to be dead.   It
1155 			 * is possible for the vget() to block the whole
1156 			 * pageout daemon, but the new low-memory handling
1157 			 * code should prevent it.
1158 			 *
1159 			 * The previous code skipped locked vnodes and, worse,
1160 			 * reordered pages in the queue.  This results in
1161 			 * completely non-deterministic operation and, on a
1162 			 * busy system, can lead to extremely non-optimal
1163 			 * pageouts.  For example, it can cause clean pages
1164 			 * to be freed and dirty pages to be moved to the end
1165 			 * of the queue.  Since dirty pages are also moved to
1166 			 * the end of the queue once-cleaned, this gives
1167 			 * way too large a weighting to defering the freeing
1168 			 * of dirty pages.
1169 			 *
1170 			 * We can't wait forever for the vnode lock, we might
1171 			 * deadlock due to a vn_read() getting stuck in
1172 			 * vm_wait while holding this vnode.  We skip the
1173 			 * vnode if we can't get it in a reasonable amount
1174 			 * of time.
1175 			 */
1176 			if (object->type == OBJT_VNODE) {
1177 				vm_page_unlock(m);
1178 				vp = object->handle;
1179 				if (vp->v_type == VREG &&
1180 				    vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1181 					mp = NULL;
1182 					++pageout_lock_miss;
1183 					if (object->flags & OBJ_MIGHTBEDIRTY)
1184 						vnodes_skipped++;
1185 					goto unlock_and_continue;
1186 				}
1187 				KASSERT(mp != NULL,
1188 				    ("vp %p with NULL v_mount", vp));
1189 				vm_object_reference_locked(object);
1190 				VM_OBJECT_UNLOCK(object);
1191 				if (vget(vp, LK_EXCLUSIVE | LK_TIMELOCK,
1192 				    curthread)) {
1193 					VM_OBJECT_LOCK(object);
1194 					++pageout_lock_miss;
1195 					if (object->flags & OBJ_MIGHTBEDIRTY)
1196 						vnodes_skipped++;
1197 					vp = NULL;
1198 					goto unlock_and_continue;
1199 				}
1200 				VM_OBJECT_LOCK(object);
1201 				vm_page_lock(m);
1202 				vm_page_lock_queues();
1203 				queues_locked = TRUE;
1204 				/*
1205 				 * The page might have been moved to another
1206 				 * queue during potential blocking in vget()
1207 				 * above.  The page might have been freed and
1208 				 * reused for another vnode.
1209 				 */
1210 				if (m->queue != PQ_INACTIVE ||
1211 				    m->object != object ||
1212 				    TAILQ_NEXT(m, pageq) != &marker) {
1213 					vm_page_unlock(m);
1214 					if (object->flags & OBJ_MIGHTBEDIRTY)
1215 						vnodes_skipped++;
1216 					goto unlock_and_continue;
1217 				}
1218 
1219 				/*
1220 				 * The page may have been busied during the
1221 				 * blocking in vget().  We don't move the
1222 				 * page back onto the end of the queue so that
1223 				 * statistics are more correct if we don't.
1224 				 */
1225 				if (m->busy || (m->oflags & VPO_BUSY)) {
1226 					vm_page_unlock(m);
1227 					goto unlock_and_continue;
1228 				}
1229 
1230 				/*
1231 				 * If the page has become held it might
1232 				 * be undergoing I/O, so skip it
1233 				 */
1234 				if (m->hold_count) {
1235 					vm_page_unlock(m);
1236 					vm_pageout_requeue(m);
1237 					if (object->flags & OBJ_MIGHTBEDIRTY)
1238 						vnodes_skipped++;
1239 					goto unlock_and_continue;
1240 				}
1241 				vm_page_unlock_queues();
1242 				queues_locked = FALSE;
1243 			}
1244 
1245 			/*
1246 			 * If a page is dirty, then it is either being washed
1247 			 * (but not yet cleaned) or it is still in the
1248 			 * laundry.  If it is still in the laundry, then we
1249 			 * start the cleaning operation.
1250 			 *
1251 			 * decrement page_shortage on success to account for
1252 			 * the (future) cleaned page.  Otherwise we could wind
1253 			 * up laundering or cleaning too many pages.
1254 			 */
1255 			if (vm_pageout_clean(m) != 0) {
1256 				--page_shortage;
1257 				--maxlaunder;
1258 			}
1259 unlock_and_continue:
1260 			vm_page_lock_assert(m, MA_NOTOWNED);
1261 			VM_OBJECT_UNLOCK(object);
1262 			if (mp != NULL) {
1263 				if (queues_locked) {
1264 					vm_page_unlock_queues();
1265 					queues_locked = FALSE;
1266 				}
1267 				if (vp != NULL)
1268 					vput(vp);
1269 				vm_object_deallocate(object);
1270 				vn_finished_write(mp);
1271 			}
1272 			vm_page_lock_assert(m, MA_NOTOWNED);
1273 			goto relock_queues;
1274 		}
1275 		vm_page_unlock(m);
1276 		VM_OBJECT_UNLOCK(object);
1277 relock_queues:
1278 		if (!queues_locked) {
1279 			vm_page_lock_queues();
1280 			queues_locked = TRUE;
1281 		}
1282 		next = TAILQ_NEXT(&marker, pageq);
1283 		TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl,
1284 		    &marker, pageq);
1285 	}
1286 
1287 	/*
1288 	 * Compute the number of pages we want to try to move from the
1289 	 * active queue to the inactive queue.
1290 	 */
1291 	page_shortage = vm_paging_target() +
1292 		cnt.v_inactive_target - cnt.v_inactive_count;
1293 	page_shortage += addl_page_shortage;
1294 
1295 	/*
1296 	 * Scan the active queue for things we can deactivate. We nominally
1297 	 * track the per-page activity counter and use it to locate
1298 	 * deactivation candidates.
1299 	 */
1300 	pcount = cnt.v_active_count;
1301 	m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
1302 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1303 
1304 	while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) {
1305 
1306 		KASSERT(m->queue == PQ_ACTIVE,
1307 		    ("vm_pageout_scan: page %p isn't active", m));
1308 
1309 		next = TAILQ_NEXT(m, pageq);
1310 		if ((m->flags & PG_MARKER) != 0) {
1311 			m = next;
1312 			continue;
1313 		}
1314 		KASSERT((m->flags & PG_FICTITIOUS) == 0,
1315 		    ("Fictitious page %p cannot be in active queue", m));
1316 		KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1317 		    ("Unmanaged page %p cannot be in active queue", m));
1318 		if (!vm_pageout_page_lock(m, &next)) {
1319 			vm_page_unlock(m);
1320 			m = next;
1321 			continue;
1322 		}
1323 		object = m->object;
1324 		if (!VM_OBJECT_TRYLOCK(object) &&
1325 		    !vm_pageout_fallback_object_lock(m, &next)) {
1326 			VM_OBJECT_UNLOCK(object);
1327 			vm_page_unlock(m);
1328 			m = next;
1329 			continue;
1330 		}
1331 
1332 		/*
1333 		 * Don't deactivate pages that are busy.
1334 		 */
1335 		if ((m->busy != 0) ||
1336 		    (m->oflags & VPO_BUSY) ||
1337 		    (m->hold_count != 0)) {
1338 			vm_page_unlock(m);
1339 			VM_OBJECT_UNLOCK(object);
1340 			vm_pageout_requeue(m);
1341 			m = next;
1342 			continue;
1343 		}
1344 
1345 		/*
1346 		 * The count for pagedaemon pages is done after checking the
1347 		 * page for eligibility...
1348 		 */
1349 		cnt.v_pdpages++;
1350 
1351 		/*
1352 		 * Check to see "how much" the page has been used.
1353 		 */
1354 		actcount = 0;
1355 		if (object->ref_count != 0) {
1356 			if (m->aflags & PGA_REFERENCED) {
1357 				actcount += 1;
1358 			}
1359 			actcount += pmap_ts_referenced(m);
1360 			if (actcount) {
1361 				m->act_count += ACT_ADVANCE + actcount;
1362 				if (m->act_count > ACT_MAX)
1363 					m->act_count = ACT_MAX;
1364 			}
1365 		}
1366 
1367 		/*
1368 		 * Since we have "tested" this bit, we need to clear it now.
1369 		 */
1370 		vm_page_aflag_clear(m, PGA_REFERENCED);
1371 
1372 		/*
1373 		 * Only if an object is currently being used, do we use the
1374 		 * page activation count stats.
1375 		 */
1376 		if (actcount && (object->ref_count != 0)) {
1377 			vm_pageout_requeue(m);
1378 		} else {
1379 			m->act_count -= min(m->act_count, ACT_DECLINE);
1380 			if (vm_pageout_algorithm ||
1381 			    object->ref_count == 0 ||
1382 			    m->act_count == 0) {
1383 				page_shortage--;
1384 				if (object->ref_count == 0) {
1385 					KASSERT(!pmap_page_is_mapped(m),
1386 				    ("vm_pageout_scan: page %p is mapped", m));
1387 					if (m->dirty == 0)
1388 						vm_page_cache(m);
1389 					else
1390 						vm_page_deactivate(m);
1391 				} else {
1392 					vm_page_deactivate(m);
1393 				}
1394 			} else {
1395 				vm_pageout_requeue(m);
1396 			}
1397 		}
1398 		vm_page_unlock(m);
1399 		VM_OBJECT_UNLOCK(object);
1400 		m = next;
1401 	}
1402 	vm_page_unlock_queues();
1403 #if !defined(NO_SWAPPING)
1404 	/*
1405 	 * Idle process swapout -- run once per second.
1406 	 */
1407 	if (vm_swap_idle_enabled) {
1408 		static long lsec;
1409 		if (time_second != lsec) {
1410 			vm_req_vmdaemon(VM_SWAP_IDLE);
1411 			lsec = time_second;
1412 		}
1413 	}
1414 #endif
1415 
1416 	/*
1417 	 * If we didn't get enough free pages, and we have skipped a vnode
1418 	 * in a writeable object, wakeup the sync daemon.  And kick swapout
1419 	 * if we did not get enough free pages.
1420 	 */
1421 	if (vm_paging_target() > 0) {
1422 		if (vnodes_skipped && vm_page_count_min())
1423 			(void) speedup_syncer();
1424 #if !defined(NO_SWAPPING)
1425 		if (vm_swap_enabled && vm_page_count_target())
1426 			vm_req_vmdaemon(VM_SWAP_NORMAL);
1427 #endif
1428 	}
1429 
1430 	/*
1431 	 * If we are critically low on one of RAM or swap and low on
1432 	 * the other, kill the largest process.  However, we avoid
1433 	 * doing this on the first pass in order to give ourselves a
1434 	 * chance to flush out dirty vnode-backed pages and to allow
1435 	 * active pages to be moved to the inactive queue and reclaimed.
1436 	 */
1437 	if (pass != 0 &&
1438 	    ((swap_pager_avail < 64 && vm_page_count_min()) ||
1439 	     (swap_pager_full && vm_paging_target() > 0)))
1440 		vm_pageout_oom(VM_OOM_MEM);
1441 }
1442 
1443 
1444 void
1445 vm_pageout_oom(int shortage)
1446 {
1447 	struct proc *p, *bigproc;
1448 	vm_offset_t size, bigsize;
1449 	struct thread *td;
1450 	struct vmspace *vm;
1451 
1452 	/*
1453 	 * We keep the process bigproc locked once we find it to keep anyone
1454 	 * from messing with it; however, there is a possibility of
1455 	 * deadlock if process B is bigproc and one of it's child processes
1456 	 * attempts to propagate a signal to B while we are waiting for A's
1457 	 * lock while walking this list.  To avoid this, we don't block on
1458 	 * the process lock but just skip a process if it is already locked.
1459 	 */
1460 	bigproc = NULL;
1461 	bigsize = 0;
1462 	sx_slock(&allproc_lock);
1463 	FOREACH_PROC_IN_SYSTEM(p) {
1464 		int breakout;
1465 
1466 		if (PROC_TRYLOCK(p) == 0)
1467 			continue;
1468 		/*
1469 		 * If this is a system, protected or killed process, skip it.
1470 		 */
1471 		if (p->p_state != PRS_NORMAL ||
1472 		    (p->p_flag & (P_INEXEC | P_PROTECTED | P_SYSTEM)) ||
1473 		    (p->p_pid == 1) || P_KILLED(p) ||
1474 		    ((p->p_pid < 48) && (swap_pager_avail != 0))) {
1475 			PROC_UNLOCK(p);
1476 			continue;
1477 		}
1478 		/*
1479 		 * If the process is in a non-running type state,
1480 		 * don't touch it.  Check all the threads individually.
1481 		 */
1482 		breakout = 0;
1483 		FOREACH_THREAD_IN_PROC(p, td) {
1484 			thread_lock(td);
1485 			if (!TD_ON_RUNQ(td) &&
1486 			    !TD_IS_RUNNING(td) &&
1487 			    !TD_IS_SLEEPING(td) &&
1488 			    !TD_IS_SUSPENDED(td)) {
1489 				thread_unlock(td);
1490 				breakout = 1;
1491 				break;
1492 			}
1493 			thread_unlock(td);
1494 		}
1495 		if (breakout) {
1496 			PROC_UNLOCK(p);
1497 			continue;
1498 		}
1499 		/*
1500 		 * get the process size
1501 		 */
1502 		vm = vmspace_acquire_ref(p);
1503 		if (vm == NULL) {
1504 			PROC_UNLOCK(p);
1505 			continue;
1506 		}
1507 		if (!vm_map_trylock_read(&vm->vm_map)) {
1508 			vmspace_free(vm);
1509 			PROC_UNLOCK(p);
1510 			continue;
1511 		}
1512 		size = vmspace_swap_count(vm);
1513 		vm_map_unlock_read(&vm->vm_map);
1514 		if (shortage == VM_OOM_MEM)
1515 			size += vmspace_resident_count(vm);
1516 		vmspace_free(vm);
1517 		/*
1518 		 * if the this process is bigger than the biggest one
1519 		 * remember it.
1520 		 */
1521 		if (size > bigsize) {
1522 			if (bigproc != NULL)
1523 				PROC_UNLOCK(bigproc);
1524 			bigproc = p;
1525 			bigsize = size;
1526 		} else
1527 			PROC_UNLOCK(p);
1528 	}
1529 	sx_sunlock(&allproc_lock);
1530 	if (bigproc != NULL) {
1531 		killproc(bigproc, "out of swap space");
1532 		sched_nice(bigproc, PRIO_MIN);
1533 		PROC_UNLOCK(bigproc);
1534 		wakeup(&cnt.v_free_count);
1535 	}
1536 }
1537 
1538 /*
1539  * This routine tries to maintain the pseudo LRU active queue,
1540  * so that during long periods of time where there is no paging,
1541  * that some statistic accumulation still occurs.  This code
1542  * helps the situation where paging just starts to occur.
1543  */
1544 static void
1545 vm_pageout_page_stats()
1546 {
1547 	vm_object_t object;
1548 	vm_page_t m,next;
1549 	int pcount,tpcount;		/* Number of pages to check */
1550 	static int fullintervalcount = 0;
1551 	int page_shortage;
1552 
1553 	page_shortage =
1554 	    (cnt.v_inactive_target + cnt.v_cache_max + cnt.v_free_min) -
1555 	    (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count);
1556 
1557 	if (page_shortage <= 0)
1558 		return;
1559 
1560 	vm_page_lock_queues();
1561 	pcount = cnt.v_active_count;
1562 	fullintervalcount += vm_pageout_stats_interval;
1563 	if (fullintervalcount < vm_pageout_full_stats_interval) {
1564 		tpcount = (int64_t)vm_pageout_stats_max * cnt.v_active_count /
1565 		    cnt.v_page_count;
1566 		if (pcount > tpcount)
1567 			pcount = tpcount;
1568 	} else {
1569 		fullintervalcount = 0;
1570 	}
1571 
1572 	m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
1573 	while ((m != NULL) && (pcount-- > 0)) {
1574 		int actcount;
1575 
1576 		KASSERT(m->queue == PQ_ACTIVE,
1577 		    ("vm_pageout_page_stats: page %p isn't active", m));
1578 
1579 		next = TAILQ_NEXT(m, pageq);
1580 		if ((m->flags & PG_MARKER) != 0) {
1581 			m = next;
1582 			continue;
1583 		}
1584 		vm_page_lock_assert(m, MA_NOTOWNED);
1585 		if (!vm_pageout_page_lock(m, &next)) {
1586 			vm_page_unlock(m);
1587 			m = next;
1588 			continue;
1589 		}
1590 		object = m->object;
1591 		if (!VM_OBJECT_TRYLOCK(object) &&
1592 		    !vm_pageout_fallback_object_lock(m, &next)) {
1593 			VM_OBJECT_UNLOCK(object);
1594 			vm_page_unlock(m);
1595 			m = next;
1596 			continue;
1597 		}
1598 
1599 		/*
1600 		 * Don't deactivate pages that are busy.
1601 		 */
1602 		if ((m->busy != 0) ||
1603 		    (m->oflags & VPO_BUSY) ||
1604 		    (m->hold_count != 0)) {
1605 			vm_page_unlock(m);
1606 			VM_OBJECT_UNLOCK(object);
1607 			vm_pageout_requeue(m);
1608 			m = next;
1609 			continue;
1610 		}
1611 
1612 		actcount = 0;
1613 		if (m->aflags & PGA_REFERENCED) {
1614 			vm_page_aflag_clear(m, PGA_REFERENCED);
1615 			actcount += 1;
1616 		}
1617 
1618 		actcount += pmap_ts_referenced(m);
1619 		if (actcount) {
1620 			m->act_count += ACT_ADVANCE + actcount;
1621 			if (m->act_count > ACT_MAX)
1622 				m->act_count = ACT_MAX;
1623 			vm_pageout_requeue(m);
1624 		} else {
1625 			if (m->act_count == 0) {
1626 				/*
1627 				 * We turn off page access, so that we have
1628 				 * more accurate RSS stats.  We don't do this
1629 				 * in the normal page deactivation when the
1630 				 * system is loaded VM wise, because the
1631 				 * cost of the large number of page protect
1632 				 * operations would be higher than the value
1633 				 * of doing the operation.
1634 				 */
1635 				pmap_remove_all(m);
1636 				vm_page_deactivate(m);
1637 			} else {
1638 				m->act_count -= min(m->act_count, ACT_DECLINE);
1639 				vm_pageout_requeue(m);
1640 			}
1641 		}
1642 		vm_page_unlock(m);
1643 		VM_OBJECT_UNLOCK(object);
1644 		m = next;
1645 	}
1646 	vm_page_unlock_queues();
1647 }
1648 
1649 /*
1650  *	vm_pageout is the high level pageout daemon.
1651  */
1652 static void
1653 vm_pageout()
1654 {
1655 	int error, pass;
1656 
1657 	/*
1658 	 * Initialize some paging parameters.
1659 	 */
1660 	cnt.v_interrupt_free_min = 2;
1661 	if (cnt.v_page_count < 2000)
1662 		vm_pageout_page_count = 8;
1663 
1664 	/*
1665 	 * v_free_reserved needs to include enough for the largest
1666 	 * swap pager structures plus enough for any pv_entry structs
1667 	 * when paging.
1668 	 */
1669 	if (cnt.v_page_count > 1024)
1670 		cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200;
1671 	else
1672 		cnt.v_free_min = 4;
1673 	cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE +
1674 	    cnt.v_interrupt_free_min;
1675 	cnt.v_free_reserved = vm_pageout_page_count +
1676 	    cnt.v_pageout_free_min + (cnt.v_page_count / 768);
1677 	cnt.v_free_severe = cnt.v_free_min / 2;
1678 	cnt.v_free_min += cnt.v_free_reserved;
1679 	cnt.v_free_severe += cnt.v_free_reserved;
1680 
1681 	/*
1682 	 * v_free_target and v_cache_min control pageout hysteresis.  Note
1683 	 * that these are more a measure of the VM cache queue hysteresis
1684 	 * then the VM free queue.  Specifically, v_free_target is the
1685 	 * high water mark (free+cache pages).
1686 	 *
1687 	 * v_free_reserved + v_cache_min (mostly means v_cache_min) is the
1688 	 * low water mark, while v_free_min is the stop.  v_cache_min must
1689 	 * be big enough to handle memory needs while the pageout daemon
1690 	 * is signalled and run to free more pages.
1691 	 */
1692 	if (cnt.v_free_count > 6144)
1693 		cnt.v_free_target = 4 * cnt.v_free_min + cnt.v_free_reserved;
1694 	else
1695 		cnt.v_free_target = 2 * cnt.v_free_min + cnt.v_free_reserved;
1696 
1697 	if (cnt.v_free_count > 2048) {
1698 		cnt.v_cache_min = cnt.v_free_target;
1699 		cnt.v_cache_max = 2 * cnt.v_cache_min;
1700 		cnt.v_inactive_target = (3 * cnt.v_free_target) / 2;
1701 	} else {
1702 		cnt.v_cache_min = 0;
1703 		cnt.v_cache_max = 0;
1704 		cnt.v_inactive_target = cnt.v_free_count / 4;
1705 	}
1706 	if (cnt.v_inactive_target > cnt.v_free_count / 3)
1707 		cnt.v_inactive_target = cnt.v_free_count / 3;
1708 
1709 	/* XXX does not really belong here */
1710 	if (vm_page_max_wired == 0)
1711 		vm_page_max_wired = cnt.v_free_count / 3;
1712 
1713 	if (vm_pageout_stats_max == 0)
1714 		vm_pageout_stats_max = cnt.v_free_target;
1715 
1716 	/*
1717 	 * Set interval in seconds for stats scan.
1718 	 */
1719 	if (vm_pageout_stats_interval == 0)
1720 		vm_pageout_stats_interval = 5;
1721 	if (vm_pageout_full_stats_interval == 0)
1722 		vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4;
1723 
1724 	swap_pager_swap_init();
1725 	pass = 0;
1726 	/*
1727 	 * The pageout daemon is never done, so loop forever.
1728 	 */
1729 	while (TRUE) {
1730 		/*
1731 		 * If we have enough free memory, wakeup waiters.  Do
1732 		 * not clear vm_pages_needed until we reach our target,
1733 		 * otherwise we may be woken up over and over again and
1734 		 * waste a lot of cpu.
1735 		 */
1736 		mtx_lock(&vm_page_queue_free_mtx);
1737 		if (vm_pages_needed && !vm_page_count_min()) {
1738 			if (!vm_paging_needed())
1739 				vm_pages_needed = 0;
1740 			wakeup(&cnt.v_free_count);
1741 		}
1742 		if (vm_pages_needed) {
1743 			/*
1744 			 * Still not done, take a second pass without waiting
1745 			 * (unlimited dirty cleaning), otherwise sleep a bit
1746 			 * and try again.
1747 			 */
1748 			++pass;
1749 			if (pass > 1)
1750 				msleep(&vm_pages_needed,
1751 				    &vm_page_queue_free_mtx, PVM, "psleep",
1752 				    hz / 2);
1753 		} else {
1754 			/*
1755 			 * Good enough, sleep & handle stats.  Prime the pass
1756 			 * for the next run.
1757 			 */
1758 			if (pass > 1)
1759 				pass = 1;
1760 			else
1761 				pass = 0;
1762 			error = msleep(&vm_pages_needed,
1763 			    &vm_page_queue_free_mtx, PVM, "psleep",
1764 			    vm_pageout_stats_interval * hz);
1765 			if (error && !vm_pages_needed) {
1766 				mtx_unlock(&vm_page_queue_free_mtx);
1767 				pass = 0;
1768 				vm_pageout_page_stats();
1769 				continue;
1770 			}
1771 		}
1772 		if (vm_pages_needed)
1773 			cnt.v_pdwakeups++;
1774 		mtx_unlock(&vm_page_queue_free_mtx);
1775 		vm_pageout_scan(pass);
1776 	}
1777 }
1778 
1779 /*
1780  * Unless the free page queue lock is held by the caller, this function
1781  * should be regarded as advisory.  Specifically, the caller should
1782  * not msleep() on &cnt.v_free_count following this function unless
1783  * the free page queue lock is held until the msleep() is performed.
1784  */
1785 void
1786 pagedaemon_wakeup()
1787 {
1788 
1789 	if (!vm_pages_needed && curthread->td_proc != pageproc) {
1790 		vm_pages_needed = 1;
1791 		wakeup(&vm_pages_needed);
1792 	}
1793 }
1794 
1795 #if !defined(NO_SWAPPING)
1796 static void
1797 vm_req_vmdaemon(int req)
1798 {
1799 	static int lastrun = 0;
1800 
1801 	mtx_lock(&vm_daemon_mtx);
1802 	vm_pageout_req_swapout |= req;
1803 	if ((ticks > (lastrun + hz)) || (ticks < lastrun)) {
1804 		wakeup(&vm_daemon_needed);
1805 		lastrun = ticks;
1806 	}
1807 	mtx_unlock(&vm_daemon_mtx);
1808 }
1809 
1810 static void
1811 vm_daemon()
1812 {
1813 	struct rlimit rsslim;
1814 	struct proc *p;
1815 	struct thread *td;
1816 	struct vmspace *vm;
1817 	int breakout, swapout_flags, tryagain, attempts;
1818 #ifdef RACCT
1819 	uint64_t rsize, ravailable;
1820 #endif
1821 
1822 	while (TRUE) {
1823 		mtx_lock(&vm_daemon_mtx);
1824 #ifdef RACCT
1825 		msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", hz);
1826 #else
1827 		msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", 0);
1828 #endif
1829 		swapout_flags = vm_pageout_req_swapout;
1830 		vm_pageout_req_swapout = 0;
1831 		mtx_unlock(&vm_daemon_mtx);
1832 		if (swapout_flags)
1833 			swapout_procs(swapout_flags);
1834 
1835 		/*
1836 		 * scan the processes for exceeding their rlimits or if
1837 		 * process is swapped out -- deactivate pages
1838 		 */
1839 		tryagain = 0;
1840 		attempts = 0;
1841 again:
1842 		attempts++;
1843 		sx_slock(&allproc_lock);
1844 		FOREACH_PROC_IN_SYSTEM(p) {
1845 			vm_pindex_t limit, size;
1846 
1847 			/*
1848 			 * if this is a system process or if we have already
1849 			 * looked at this process, skip it.
1850 			 */
1851 			PROC_LOCK(p);
1852 			if (p->p_state != PRS_NORMAL ||
1853 			    p->p_flag & (P_INEXEC | P_SYSTEM | P_WEXIT)) {
1854 				PROC_UNLOCK(p);
1855 				continue;
1856 			}
1857 			/*
1858 			 * if the process is in a non-running type state,
1859 			 * don't touch it.
1860 			 */
1861 			breakout = 0;
1862 			FOREACH_THREAD_IN_PROC(p, td) {
1863 				thread_lock(td);
1864 				if (!TD_ON_RUNQ(td) &&
1865 				    !TD_IS_RUNNING(td) &&
1866 				    !TD_IS_SLEEPING(td) &&
1867 				    !TD_IS_SUSPENDED(td)) {
1868 					thread_unlock(td);
1869 					breakout = 1;
1870 					break;
1871 				}
1872 				thread_unlock(td);
1873 			}
1874 			if (breakout) {
1875 				PROC_UNLOCK(p);
1876 				continue;
1877 			}
1878 			/*
1879 			 * get a limit
1880 			 */
1881 			lim_rlimit(p, RLIMIT_RSS, &rsslim);
1882 			limit = OFF_TO_IDX(
1883 			    qmin(rsslim.rlim_cur, rsslim.rlim_max));
1884 
1885 			/*
1886 			 * let processes that are swapped out really be
1887 			 * swapped out set the limit to nothing (will force a
1888 			 * swap-out.)
1889 			 */
1890 			if ((p->p_flag & P_INMEM) == 0)
1891 				limit = 0;	/* XXX */
1892 			vm = vmspace_acquire_ref(p);
1893 			PROC_UNLOCK(p);
1894 			if (vm == NULL)
1895 				continue;
1896 
1897 			size = vmspace_resident_count(vm);
1898 			if (limit >= 0 && size >= limit) {
1899 				vm_pageout_map_deactivate_pages(
1900 				    &vm->vm_map, limit);
1901 			}
1902 #ifdef RACCT
1903 			rsize = IDX_TO_OFF(size);
1904 			PROC_LOCK(p);
1905 			racct_set(p, RACCT_RSS, rsize);
1906 			ravailable = racct_get_available(p, RACCT_RSS);
1907 			PROC_UNLOCK(p);
1908 			if (rsize > ravailable) {
1909 				/*
1910 				 * Don't be overly aggressive; this might be
1911 				 * an innocent process, and the limit could've
1912 				 * been exceeded by some memory hog.  Don't
1913 				 * try to deactivate more than 1/4th of process'
1914 				 * resident set size.
1915 				 */
1916 				if (attempts <= 8) {
1917 					if (ravailable < rsize - (rsize / 4))
1918 						ravailable = rsize - (rsize / 4);
1919 				}
1920 				vm_pageout_map_deactivate_pages(
1921 				    &vm->vm_map, OFF_TO_IDX(ravailable));
1922 				/* Update RSS usage after paging out. */
1923 				size = vmspace_resident_count(vm);
1924 				rsize = IDX_TO_OFF(size);
1925 				PROC_LOCK(p);
1926 				racct_set(p, RACCT_RSS, rsize);
1927 				PROC_UNLOCK(p);
1928 				if (rsize > ravailable)
1929 					tryagain = 1;
1930 			}
1931 #endif
1932 			vmspace_free(vm);
1933 		}
1934 		sx_sunlock(&allproc_lock);
1935 		if (tryagain != 0 && attempts <= 10)
1936 			goto again;
1937 	}
1938 }
1939 #endif			/* !defined(NO_SWAPPING) */
1940