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