xref: /freebsd/sys/vm/vm_pageout.c (revision e0c5a895f10159ad75f2fdc76804a8eeb4298655)
1df8bae1dSRodney W. Grimes /*
226f9a767SRodney W. Grimes  * Copyright (c) 1991 Regents of the University of California.
326f9a767SRodney W. Grimes  * All rights reserved.
426f9a767SRodney W. Grimes  * Copyright (c) 1994 John S. Dyson
526f9a767SRodney W. Grimes  * All rights reserved.
626f9a767SRodney W. Grimes  * Copyright (c) 1994 David Greenman
726f9a767SRodney W. Grimes  * All rights reserved.
8df8bae1dSRodney W. Grimes  *
9df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
10df8bae1dSRodney W. Grimes  * The Mach Operating System project at Carnegie-Mellon University.
11df8bae1dSRodney W. Grimes  *
12df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
13df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
14df8bae1dSRodney W. Grimes  * are met:
15df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
17df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
18df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
19df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
20df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
21df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
22df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
23df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
24df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
25df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
26df8bae1dSRodney W. Grimes  *    without specific prior written permission.
27df8bae1dSRodney W. Grimes  *
28df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
39df8bae1dSRodney W. Grimes  *
403c4dd356SDavid Greenman  *	from: @(#)vm_pageout.c	7.4 (Berkeley) 5/7/91
41df8bae1dSRodney W. Grimes  *
42df8bae1dSRodney W. Grimes  *
43df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
44df8bae1dSRodney W. Grimes  * All rights reserved.
45df8bae1dSRodney W. Grimes  *
46df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
47df8bae1dSRodney W. Grimes  *
48df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
49df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
50df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
51df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
52df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
53df8bae1dSRodney W. Grimes  *
54df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
55df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
56df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
57df8bae1dSRodney W. Grimes  *
58df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
59df8bae1dSRodney W. Grimes  *
60df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
61df8bae1dSRodney W. Grimes  *  School of Computer Science
62df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
63df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
64df8bae1dSRodney W. Grimes  *
65df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
66df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
6726f9a767SRodney W. Grimes  *
68e0c5a895SJohn Dyson  * $Id: vm_pageout.c,v 1.86 1996/09/28 03:33:40 dyson Exp $
69df8bae1dSRodney W. Grimes  */
70df8bae1dSRodney W. Grimes 
71df8bae1dSRodney W. Grimes /*
72df8bae1dSRodney W. Grimes  *	The proverbial page-out daemon.
73df8bae1dSRodney W. Grimes  */
74df8bae1dSRodney W. Grimes 
75df8bae1dSRodney W. Grimes #include <sys/param.h>
7626f9a767SRodney W. Grimes #include <sys/systm.h>
77b5e8ce9fSBruce Evans #include <sys/kernel.h>
7826f9a767SRodney W. Grimes #include <sys/proc.h>
7926f9a767SRodney W. Grimes #include <sys/resourcevar.h>
8026f9a767SRodney W. Grimes #include <sys/malloc.h>
810d94caffSDavid Greenman #include <sys/kernel.h>
82d2fc5315SPoul-Henning Kamp #include <sys/signalvar.h>
83f6b04d2bSDavid Greenman #include <sys/vnode.h>
84efeaf95aSDavid Greenman #include <sys/vmmeter.h>
8538efa82bSJohn Dyson #include <sys/sysctl.h>
86df8bae1dSRodney W. Grimes 
87df8bae1dSRodney W. Grimes #include <vm/vm.h>
88efeaf95aSDavid Greenman #include <vm/vm_param.h>
89efeaf95aSDavid Greenman #include <vm/vm_prot.h>
90efeaf95aSDavid Greenman #include <vm/lock.h>
91efeaf95aSDavid Greenman #include <vm/vm_object.h>
92df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
93efeaf95aSDavid Greenman #include <vm/vm_map.h>
94df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
957c0414d0SDavid Greenman #include <vm/vm_kern.h>
9624a1cce3SDavid Greenman #include <vm/vm_pager.h>
9705f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h>
98efeaf95aSDavid Greenman #include <vm/vm_extern.h>
99df8bae1dSRodney W. Grimes 
1002b14f991SJulian Elischer /*
1012b14f991SJulian Elischer  * System initialization
1022b14f991SJulian Elischer  */
1032b14f991SJulian Elischer 
1042b14f991SJulian Elischer /* the kernel process "vm_pageout"*/
1052b14f991SJulian Elischer static void vm_pageout __P((void));
1063af76890SPoul-Henning Kamp static int vm_pageout_clean __P((vm_page_t, int));
1073af76890SPoul-Henning Kamp static int vm_pageout_scan __P((void));
108f35329acSJohn Dyson static int vm_pageout_free_page_calc __P((vm_size_t count));
1092b14f991SJulian Elischer struct proc *pageproc;
1102b14f991SJulian Elischer 
1112b14f991SJulian Elischer static struct kproc_desc page_kp = {
1122b14f991SJulian Elischer 	"pagedaemon",
1132b14f991SJulian Elischer 	vm_pageout,
1142b14f991SJulian Elischer 	&pageproc
1152b14f991SJulian Elischer };
1164590fd3aSDavid Greenman SYSINIT_KT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, &page_kp)
1172b14f991SJulian Elischer 
11838efa82bSJohn Dyson #if !defined(NO_SWAPPING)
1192b14f991SJulian Elischer /* the kernel process "vm_daemon"*/
1202b14f991SJulian Elischer static void vm_daemon __P((void));
121f708ef1bSPoul-Henning Kamp static struct	proc *vmproc;
1222b14f991SJulian Elischer 
1232b14f991SJulian Elischer static struct kproc_desc vm_kp = {
1242b14f991SJulian Elischer 	"vmdaemon",
1252b14f991SJulian Elischer 	vm_daemon,
1262b14f991SJulian Elischer 	&vmproc
1272b14f991SJulian Elischer };
1284590fd3aSDavid Greenman SYSINIT_KT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp)
12938efa82bSJohn Dyson #endif
1302b14f991SJulian Elischer 
1312b14f991SJulian Elischer 
132df8bae1dSRodney W. Grimes int vm_pages_needed;		/* Event on which pageout daemon sleeps */
13326f9a767SRodney W. Grimes 
134c3cb3e12SDavid Greenman int vm_pageout_pages_needed;	/* flag saying that the pageout daemon needs pages */
13526f9a767SRodney W. Grimes 
13626f9a767SRodney W. Grimes extern int npendingio;
13738efa82bSJohn Dyson #if !defined(NO_SWAPPING)
138f708ef1bSPoul-Henning Kamp static int vm_pageout_req_swapout;	/* XXX */
139f708ef1bSPoul-Henning Kamp static int vm_daemon_needed;
14038efa82bSJohn Dyson #endif
14126f9a767SRodney W. Grimes extern int nswiodone;
1425663e6deSDavid Greenman extern int vm_swap_size;
143f6b04d2bSDavid Greenman extern int vfs_update_wakeup;
14438efa82bSJohn Dyson int vm_pageout_algorithm_lru=0;
14538efa82bSJohn Dyson #if defined(NO_SWAPPING)
14638efa82bSJohn Dyson int vm_swapping_enabled=0;
14738efa82bSJohn Dyson #else
14838efa82bSJohn Dyson int vm_swapping_enabled=1;
14938efa82bSJohn Dyson #endif
15038efa82bSJohn Dyson 
15138efa82bSJohn Dyson SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm,
15238efa82bSJohn Dyson 	CTLFLAG_RW, &vm_pageout_algorithm_lru, 0, "");
15338efa82bSJohn Dyson 
15438efa82bSJohn Dyson #if defined(NO_SWAPPING)
15538efa82bSJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swapping_enabled,
15638efa82bSJohn Dyson 	CTLFLAG_RD, &vm_swapping_enabled, 0, "");
15738efa82bSJohn Dyson #else
15838efa82bSJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swapping_enabled,
15938efa82bSJohn Dyson 	CTLFLAG_RW, &vm_swapping_enabled, 0, "");
16038efa82bSJohn Dyson #endif
16126f9a767SRodney W. Grimes 
1620d94caffSDavid Greenman #define MAXLAUNDER (cnt.v_page_count > 1800 ? 32 : 16)
16326f9a767SRodney W. Grimes 
164a316d390SJohn Dyson #define VM_PAGEOUT_PAGE_COUNT 16
165bbc0ec52SDavid Greenman int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT;
166df8bae1dSRodney W. Grimes 
167c3cb3e12SDavid Greenman int vm_page_max_wired;		/* XXX max # of wired pages system-wide */
168df8bae1dSRodney W. Grimes 
16938efa82bSJohn Dyson #if !defined(NO_SWAPPING)
17038efa82bSJohn Dyson typedef void freeer_fcn_t __P((vm_map_t, vm_object_t, vm_pindex_t, int));
17138efa82bSJohn Dyson static void vm_pageout_map_deactivate_pages __P((vm_map_t, vm_pindex_t));
172cd41fc12SDavid Greenman static freeer_fcn_t vm_pageout_object_deactivate_pages;
173cd41fc12SDavid Greenman static void vm_req_vmdaemon __P((void));
17438efa82bSJohn Dyson #endif
175cd41fc12SDavid Greenman 
17626f9a767SRodney W. Grimes /*
17726f9a767SRodney W. Grimes  * vm_pageout_clean:
17824a1cce3SDavid Greenman  *
1790d94caffSDavid Greenman  * Clean the page and remove it from the laundry.
18026f9a767SRodney W. Grimes  *
1810d94caffSDavid Greenman  * We set the busy bit to cause potential page faults on this page to
18226f9a767SRodney W. Grimes  * block.
18326f9a767SRodney W. Grimes  *
1840d94caffSDavid Greenman  * And we set pageout-in-progress to keep the object from disappearing
1850d94caffSDavid Greenman  * during pageout.  This guarantees that the page won't move from the
1860d94caffSDavid Greenman  * inactive queue.  (However, any other page on the inactive queue may
1870d94caffSDavid Greenman  * move!)
18826f9a767SRodney W. Grimes  */
1893af76890SPoul-Henning Kamp static int
19024a1cce3SDavid Greenman vm_pageout_clean(m, sync)
19124a1cce3SDavid Greenman 	vm_page_t m;
19224a1cce3SDavid Greenman 	int sync;
19324a1cce3SDavid Greenman {
19426f9a767SRodney W. Grimes 	register vm_object_t object;
195f35329acSJohn Dyson 	vm_page_t mc[2*vm_pageout_page_count];
19624a1cce3SDavid Greenman 	int pageout_count;
19724a1cce3SDavid Greenman 	int i, forward_okay, backward_okay, page_base;
198a316d390SJohn Dyson 	vm_pindex_t pindex = m->pindex;
19926f9a767SRodney W. Grimes 
20026f9a767SRodney W. Grimes 	object = m->object;
20124a1cce3SDavid Greenman 
20226f9a767SRodney W. Grimes 	/*
20324a1cce3SDavid Greenman 	 * If not OBJT_SWAP, additional memory may be needed to do the pageout.
20424a1cce3SDavid Greenman 	 * Try to avoid the deadlock.
20526f9a767SRodney W. Grimes 	 */
20624a1cce3SDavid Greenman 	if ((sync != VM_PAGEOUT_FORCE) &&
207f35329acSJohn Dyson 	    (object->type == OBJT_DEFAULT) &&
20824a1cce3SDavid Greenman 	    ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min))
20926f9a767SRodney W. Grimes 		return 0;
21026f9a767SRodney W. Grimes 
21124a1cce3SDavid Greenman 	/*
21224a1cce3SDavid Greenman 	 * Don't mess with the page if it's busy.
21324a1cce3SDavid Greenman 	 */
214f6b04d2bSDavid Greenman 	if ((!sync && m->hold_count != 0) ||
2150d94caffSDavid Greenman 	    ((m->busy != 0) || (m->flags & PG_BUSY)))
2160d94caffSDavid Greenman 		return 0;
2170d94caffSDavid Greenman 
21824a1cce3SDavid Greenman 	/*
21924a1cce3SDavid Greenman 	 * Try collapsing before it's too late.
22024a1cce3SDavid Greenman 	 */
22124a1cce3SDavid Greenman 	if (!sync && object->backing_object) {
22226f9a767SRodney W. Grimes 		vm_object_collapse(object);
22326f9a767SRodney W. Grimes 	}
224f35329acSJohn Dyson 	mc[vm_pageout_page_count] = m;
22526f9a767SRodney W. Grimes 	pageout_count = 1;
226f35329acSJohn Dyson 	page_base = vm_pageout_page_count;
22724a1cce3SDavid Greenman 	forward_okay = TRUE;
228a316d390SJohn Dyson 	if (pindex != 0)
22924a1cce3SDavid Greenman 		backward_okay = TRUE;
23026f9a767SRodney W. Grimes 	else
23124a1cce3SDavid Greenman 		backward_okay = FALSE;
23224a1cce3SDavid Greenman 	/*
23324a1cce3SDavid Greenman 	 * Scan object for clusterable pages.
23424a1cce3SDavid Greenman 	 *
23524a1cce3SDavid Greenman 	 * We can cluster ONLY if: ->> the page is NOT
23624a1cce3SDavid Greenman 	 * clean, wired, busy, held, or mapped into a
23724a1cce3SDavid Greenman 	 * buffer, and one of the following:
23824a1cce3SDavid Greenman 	 * 1) The page is inactive, or a seldom used
23924a1cce3SDavid Greenman 	 *    active page.
24024a1cce3SDavid Greenman 	 * -or-
24124a1cce3SDavid Greenman 	 * 2) we force the issue.
24224a1cce3SDavid Greenman 	 */
24324a1cce3SDavid Greenman 	for (i = 1; (i < vm_pageout_page_count) && (forward_okay || backward_okay); i++) {
24424a1cce3SDavid Greenman 		vm_page_t p;
245f6b04d2bSDavid Greenman 
24624a1cce3SDavid Greenman 		/*
24724a1cce3SDavid Greenman 		 * See if forward page is clusterable.
24824a1cce3SDavid Greenman 		 */
24924a1cce3SDavid Greenman 		if (forward_okay) {
25024a1cce3SDavid Greenman 			/*
25124a1cce3SDavid Greenman 			 * Stop forward scan at end of object.
25224a1cce3SDavid Greenman 			 */
253a316d390SJohn Dyson 			if ((pindex + i) > object->size) {
25424a1cce3SDavid Greenman 				forward_okay = FALSE;
25524a1cce3SDavid Greenman 				goto do_backward;
256f6b04d2bSDavid Greenman 			}
257a316d390SJohn Dyson 			p = vm_page_lookup(object, pindex + i);
25824a1cce3SDavid Greenman 			if (p) {
2595070c7f8SJohn Dyson 				if (((p->queue - p->pc) == PQ_CACHE) ||
2605070c7f8SJohn Dyson 					(p->flags & PG_BUSY) || p->busy) {
26124a1cce3SDavid Greenman 					forward_okay = FALSE;
26224a1cce3SDavid Greenman 					goto do_backward;
263f6b04d2bSDavid Greenman 				}
26424a1cce3SDavid Greenman 				vm_page_test_dirty(p);
26524a1cce3SDavid Greenman 				if ((p->dirty & p->valid) != 0 &&
266bd7e5f99SJohn Dyson 				    ((p->queue == PQ_INACTIVE) ||
26724a1cce3SDavid Greenman 				     (sync == VM_PAGEOUT_FORCE)) &&
26824a1cce3SDavid Greenman 				    (p->wire_count == 0) &&
26924a1cce3SDavid Greenman 				    (p->hold_count == 0)) {
270f35329acSJohn Dyson 					mc[vm_pageout_page_count + i] = p;
27124a1cce3SDavid Greenman 					pageout_count++;
27224a1cce3SDavid Greenman 					if (pageout_count == vm_pageout_page_count)
27324a1cce3SDavid Greenman 						break;
27424a1cce3SDavid Greenman 				} else {
27524a1cce3SDavid Greenman 					forward_okay = FALSE;
276f6b04d2bSDavid Greenman 				}
27724a1cce3SDavid Greenman 			} else {
27824a1cce3SDavid Greenman 				forward_okay = FALSE;
27924a1cce3SDavid Greenman 			}
28024a1cce3SDavid Greenman 		}
28124a1cce3SDavid Greenman do_backward:
28224a1cce3SDavid Greenman 		/*
28324a1cce3SDavid Greenman 		 * See if backward page is clusterable.
28424a1cce3SDavid Greenman 		 */
28524a1cce3SDavid Greenman 		if (backward_okay) {
28624a1cce3SDavid Greenman 			/*
28724a1cce3SDavid Greenman 			 * Stop backward scan at beginning of object.
28824a1cce3SDavid Greenman 			 */
289a316d390SJohn Dyson 			if ((pindex - i) == 0) {
29024a1cce3SDavid Greenman 				backward_okay = FALSE;
29124a1cce3SDavid Greenman 			}
292a316d390SJohn Dyson 			p = vm_page_lookup(object, pindex - i);
29324a1cce3SDavid Greenman 			if (p) {
2945070c7f8SJohn Dyson 				if (((p->queue - p->pc) == PQ_CACHE) ||
2955070c7f8SJohn Dyson 					(p->flags & PG_BUSY) || p->busy) {
29624a1cce3SDavid Greenman 					backward_okay = FALSE;
29724a1cce3SDavid Greenman 					continue;
29824a1cce3SDavid Greenman 				}
29924a1cce3SDavid Greenman 				vm_page_test_dirty(p);
30024a1cce3SDavid Greenman 				if ((p->dirty & p->valid) != 0 &&
301bd7e5f99SJohn Dyson 				    ((p->queue == PQ_INACTIVE) ||
30224a1cce3SDavid Greenman 				     (sync == VM_PAGEOUT_FORCE)) &&
30324a1cce3SDavid Greenman 				    (p->wire_count == 0) &&
30424a1cce3SDavid Greenman 				    (p->hold_count == 0)) {
305f35329acSJohn Dyson 					mc[vm_pageout_page_count - i] = p;
30624a1cce3SDavid Greenman 					pageout_count++;
30724a1cce3SDavid Greenman 					page_base--;
30824a1cce3SDavid Greenman 					if (pageout_count == vm_pageout_page_count)
30924a1cce3SDavid Greenman 						break;
31024a1cce3SDavid Greenman 				} else {
31124a1cce3SDavid Greenman 					backward_okay = FALSE;
31224a1cce3SDavid Greenman 				}
31324a1cce3SDavid Greenman 			} else {
31424a1cce3SDavid Greenman 				backward_okay = FALSE;
31524a1cce3SDavid Greenman 			}
316f6b04d2bSDavid Greenman 		}
317f6b04d2bSDavid Greenman 	}
318f6b04d2bSDavid Greenman 
31967bf6868SJohn Dyson 	/*
32067bf6868SJohn Dyson 	 * we allow reads during pageouts...
32167bf6868SJohn Dyson 	 */
32224a1cce3SDavid Greenman 	for (i = page_base; i < (page_base + pageout_count); i++) {
32324a1cce3SDavid Greenman 		mc[i]->flags |= PG_BUSY;
32467bf6868SJohn Dyson 		vm_page_protect(mc[i], VM_PROT_READ);
32526f9a767SRodney W. Grimes 	}
32626f9a767SRodney W. Grimes 
327aef922f5SJohn Dyson 	return vm_pageout_flush(&mc[page_base], pageout_count, sync);
328aef922f5SJohn Dyson }
329aef922f5SJohn Dyson 
330aef922f5SJohn Dyson int
331aef922f5SJohn Dyson vm_pageout_flush(mc, count, sync)
332aef922f5SJohn Dyson 	vm_page_t *mc;
333aef922f5SJohn Dyson 	int count;
334aef922f5SJohn Dyson 	int sync;
335aef922f5SJohn Dyson {
336aef922f5SJohn Dyson 	register vm_object_t object;
337aef922f5SJohn Dyson 	int pageout_status[count];
338aef922f5SJohn Dyson 	int anyok = 0;
339aef922f5SJohn Dyson 	int i;
340aef922f5SJohn Dyson 
341aef922f5SJohn Dyson 	object = mc[0]->object;
342aef922f5SJohn Dyson 	object->paging_in_progress += count;
343aef922f5SJohn Dyson 
344aef922f5SJohn Dyson 	vm_pager_put_pages(object, mc, count,
34526f9a767SRodney W. Grimes 	    ((sync || (object == kernel_object)) ? TRUE : FALSE),
34626f9a767SRodney W. Grimes 	    pageout_status);
34726f9a767SRodney W. Grimes 
348aef922f5SJohn Dyson 	for (i = 0; i < count; i++) {
349aef922f5SJohn Dyson 		vm_page_t mt = mc[i];
35024a1cce3SDavid Greenman 
35126f9a767SRodney W. Grimes 		switch (pageout_status[i]) {
35226f9a767SRodney W. Grimes 		case VM_PAGER_OK:
35326f9a767SRodney W. Grimes 			++anyok;
35426f9a767SRodney W. Grimes 			break;
35526f9a767SRodney W. Grimes 		case VM_PAGER_PEND:
35626f9a767SRodney W. Grimes 			++anyok;
35726f9a767SRodney W. Grimes 			break;
35826f9a767SRodney W. Grimes 		case VM_PAGER_BAD:
35926f9a767SRodney W. Grimes 			/*
3600d94caffSDavid Greenman 			 * Page outside of range of object. Right now we
3610d94caffSDavid Greenman 			 * essentially lose the changes by pretending it
3620d94caffSDavid Greenman 			 * worked.
36326f9a767SRodney W. Grimes 			 */
36467bf6868SJohn Dyson 			pmap_clear_modify(VM_PAGE_TO_PHYS(mt));
36524a1cce3SDavid Greenman 			mt->dirty = 0;
36626f9a767SRodney W. Grimes 			break;
36726f9a767SRodney W. Grimes 		case VM_PAGER_ERROR:
36826f9a767SRodney W. Grimes 		case VM_PAGER_FAIL:
36926f9a767SRodney W. Grimes 			/*
3700d94caffSDavid Greenman 			 * If page couldn't be paged out, then reactivate the
3710d94caffSDavid Greenman 			 * page so it doesn't clog the inactive list.  (We
3720d94caffSDavid Greenman 			 * will try paging out it again later).
37326f9a767SRodney W. Grimes 			 */
374bd7e5f99SJohn Dyson 			if (mt->queue == PQ_INACTIVE)
37524a1cce3SDavid Greenman 				vm_page_activate(mt);
37626f9a767SRodney W. Grimes 			break;
37726f9a767SRodney W. Grimes 		case VM_PAGER_AGAIN:
37826f9a767SRodney W. Grimes 			break;
37926f9a767SRodney W. Grimes 		}
38026f9a767SRodney W. Grimes 
38126f9a767SRodney W. Grimes 
38226f9a767SRodney W. Grimes 		/*
3830d94caffSDavid Greenman 		 * If the operation is still going, leave the page busy to
3840d94caffSDavid Greenman 		 * block all other accesses. Also, leave the paging in
3850d94caffSDavid Greenman 		 * progress indicator set so that we don't attempt an object
3860d94caffSDavid Greenman 		 * collapse.
38726f9a767SRodney W. Grimes 		 */
38826f9a767SRodney W. Grimes 		if (pageout_status[i] != VM_PAGER_PEND) {
389f919ebdeSDavid Greenman 			vm_object_pip_wakeup(object);
39024a1cce3SDavid Greenman 			PAGE_WAKEUP(mt);
39126f9a767SRodney W. Grimes 		}
39226f9a767SRodney W. Grimes 	}
39326f9a767SRodney W. Grimes 	return anyok;
39426f9a767SRodney W. Grimes }
39526f9a767SRodney W. Grimes 
39638efa82bSJohn Dyson #if !defined(NO_SWAPPING)
39726f9a767SRodney W. Grimes /*
39826f9a767SRodney W. Grimes  *	vm_pageout_object_deactivate_pages
39926f9a767SRodney W. Grimes  *
40026f9a767SRodney W. Grimes  *	deactivate enough pages to satisfy the inactive target
40126f9a767SRodney W. Grimes  *	requirements or if vm_page_proc_limit is set, then
40226f9a767SRodney W. Grimes  *	deactivate all of the pages in the object and its
40324a1cce3SDavid Greenman  *	backing_objects.
40426f9a767SRodney W. Grimes  *
40526f9a767SRodney W. Grimes  *	The object and map must be locked.
40626f9a767SRodney W. Grimes  */
40738efa82bSJohn Dyson static void
40838efa82bSJohn Dyson vm_pageout_object_deactivate_pages(map, object, desired, map_remove_only)
40926f9a767SRodney W. Grimes 	vm_map_t map;
41026f9a767SRodney W. Grimes 	vm_object_t object;
41138efa82bSJohn Dyson 	vm_pindex_t desired;
4120d94caffSDavid Greenman 	int map_remove_only;
41326f9a767SRodney W. Grimes {
41426f9a767SRodney W. Grimes 	register vm_page_t p, next;
41526f9a767SRodney W. Grimes 	int rcount;
41638efa82bSJohn Dyson 	int remove_mode;
4171eeaa1e3SJohn Dyson 	int s;
41826f9a767SRodney W. Grimes 
41924a1cce3SDavid Greenman 	if (object->type == OBJT_DEVICE)
42038efa82bSJohn Dyson 		return;
4218f895206SDavid Greenman 
42238efa82bSJohn Dyson 	while (object) {
42338efa82bSJohn Dyson 		if (vm_map_pmap(map)->pm_stats.resident_count <= desired)
42438efa82bSJohn Dyson 			return;
42524a1cce3SDavid Greenman 		if (object->paging_in_progress)
42638efa82bSJohn Dyson 			return;
42726f9a767SRodney W. Grimes 
42838efa82bSJohn Dyson 		remove_mode = map_remove_only;
42938efa82bSJohn Dyson 		if (object->shadow_count > 1)
43038efa82bSJohn Dyson 			remove_mode = 1;
43126f9a767SRodney W. Grimes 	/*
43226f9a767SRodney W. Grimes 	 * scan the objects entire memory queue
43326f9a767SRodney W. Grimes 	 */
43426f9a767SRodney W. Grimes 		rcount = object->resident_page_count;
435b18bfc3dSJohn Dyson 		p = TAILQ_FIRST(&object->memq);
43626f9a767SRodney W. Grimes 		while (p && (rcount-- > 0)) {
437ef743ce6SJohn Dyson 			int refcount;
43838efa82bSJohn Dyson 			if (vm_map_pmap(map)->pm_stats.resident_count <= desired)
43938efa82bSJohn Dyson 				return;
440b18bfc3dSJohn Dyson 			next = TAILQ_NEXT(p, listq);
441a58d1fa1SDavid Greenman 			cnt.v_pdpages++;
4420d94caffSDavid Greenman 			if (p->wire_count != 0 ||
4430d94caffSDavid Greenman 			    p->hold_count != 0 ||
4440d94caffSDavid Greenman 			    p->busy != 0 ||
445bd7e5f99SJohn Dyson 			    (p->flags & PG_BUSY) ||
4460d94caffSDavid Greenman 			    !pmap_page_exists(vm_map_pmap(map), VM_PAGE_TO_PHYS(p))) {
4470d94caffSDavid Greenman 				p = next;
4480d94caffSDavid Greenman 				continue;
4490d94caffSDavid Greenman 			}
450ef743ce6SJohn Dyson 
45167bf6868SJohn Dyson 			refcount = pmap_ts_referenced(VM_PAGE_TO_PHYS(p));
452ef743ce6SJohn Dyson 			if (refcount) {
453ef743ce6SJohn Dyson 				p->flags |= PG_REFERENCED;
454c8c4b40cSJohn Dyson 			} else if (p->flags & PG_REFERENCED) {
455c8c4b40cSJohn Dyson 				refcount = 1;
456ef743ce6SJohn Dyson 			}
457ef743ce6SJohn Dyson 
45838efa82bSJohn Dyson 			if ((p->queue != PQ_ACTIVE) &&
45938efa82bSJohn Dyson 				(p->flags & PG_REFERENCED)) {
460ef743ce6SJohn Dyson 				vm_page_activate(p);
461c8c4b40cSJohn Dyson 				p->act_count += refcount;
462c8c4b40cSJohn Dyson 				p->flags &= ~PG_REFERENCED;
463c8c4b40cSJohn Dyson 			} else if (p->queue == PQ_ACTIVE) {
464ef743ce6SJohn Dyson 				if ((p->flags & PG_REFERENCED) == 0) {
465c8c4b40cSJohn Dyson 					p->act_count -= min(p->act_count, ACT_DECLINE);
466c8c4b40cSJohn Dyson 					if (!remove_mode && (vm_pageout_algorithm_lru || (p->act_count == 0))) {
467b18bfc3dSJohn Dyson 						vm_page_protect(p, VM_PROT_NONE);
46826f9a767SRodney W. Grimes 						vm_page_deactivate(p);
46926f9a767SRodney W. Grimes 					} else {
470c8c4b40cSJohn Dyson 						s = splvm();
471c8c4b40cSJohn Dyson 						TAILQ_REMOVE(&vm_page_queue_active, p, pageq);
472c8c4b40cSJohn Dyson 						TAILQ_INSERT_TAIL(&vm_page_queue_active, p, pageq);
473c8c4b40cSJohn Dyson 						splx(s);
474c8c4b40cSJohn Dyson 					}
475c8c4b40cSJohn Dyson 				} else {
476a647a309SDavid Greenman 					p->flags &= ~PG_REFERENCED;
47738efa82bSJohn Dyson 					if (p->act_count < (ACT_MAX - ACT_ADVANCE))
47838efa82bSJohn Dyson 						p->act_count += ACT_ADVANCE;
4791eeaa1e3SJohn Dyson 					s = splvm();
48026f9a767SRodney W. Grimes 					TAILQ_REMOVE(&vm_page_queue_active, p, pageq);
48126f9a767SRodney W. Grimes 					TAILQ_INSERT_TAIL(&vm_page_queue_active, p, pageq);
4821eeaa1e3SJohn Dyson 					splx(s);
48326f9a767SRodney W. Grimes 				}
484bd7e5f99SJohn Dyson 			} else if (p->queue == PQ_INACTIVE) {
485f919ebdeSDavid Greenman 				vm_page_protect(p, VM_PROT_NONE);
48626f9a767SRodney W. Grimes 			}
48726f9a767SRodney W. Grimes 			p = next;
48826f9a767SRodney W. Grimes 		}
48938efa82bSJohn Dyson 		object = object->backing_object;
49038efa82bSJohn Dyson 	}
49138efa82bSJohn Dyson 	return;
49226f9a767SRodney W. Grimes }
49326f9a767SRodney W. Grimes 
49426f9a767SRodney W. Grimes /*
49526f9a767SRodney W. Grimes  * deactivate some number of pages in a map, try to do it fairly, but
49626f9a767SRodney W. Grimes  * that is really hard to do.
49726f9a767SRodney W. Grimes  */
498cd41fc12SDavid Greenman static void
49938efa82bSJohn Dyson vm_pageout_map_deactivate_pages(map, desired)
50026f9a767SRodney W. Grimes 	vm_map_t map;
50138efa82bSJohn Dyson 	vm_pindex_t desired;
50226f9a767SRodney W. Grimes {
50326f9a767SRodney W. Grimes 	vm_map_entry_t tmpe;
50438efa82bSJohn Dyson 	vm_object_t obj, bigobj;
5050d94caffSDavid Greenman 
50626f9a767SRodney W. Grimes 	vm_map_reference(map);
50738efa82bSJohn Dyson 	if (!lock_try_write(&map->lock)) {
50826f9a767SRodney W. Grimes 		vm_map_deallocate(map);
50926f9a767SRodney W. Grimes 		return;
51026f9a767SRodney W. Grimes 	}
51138efa82bSJohn Dyson 
51238efa82bSJohn Dyson 	bigobj = NULL;
51338efa82bSJohn Dyson 
51438efa82bSJohn Dyson 	/*
51538efa82bSJohn Dyson 	 * first, search out the biggest object, and try to free pages from
51638efa82bSJohn Dyson 	 * that.
51738efa82bSJohn Dyson 	 */
51826f9a767SRodney W. Grimes 	tmpe = map->header.next;
51938efa82bSJohn Dyson 	while (tmpe != &map->header) {
52038efa82bSJohn Dyson 		if ((tmpe->is_sub_map == 0) && (tmpe->is_a_map == 0)) {
52138efa82bSJohn Dyson 			obj = tmpe->object.vm_object;
52238efa82bSJohn Dyson 			if ((obj != NULL) && (obj->shadow_count <= 1) &&
52338efa82bSJohn Dyson 				((bigobj == NULL) ||
52438efa82bSJohn Dyson 				 (bigobj->resident_page_count < obj->resident_page_count))) {
52538efa82bSJohn Dyson 				bigobj = obj;
52638efa82bSJohn Dyson 			}
52738efa82bSJohn Dyson 		}
52838efa82bSJohn Dyson 		tmpe = tmpe->next;
52938efa82bSJohn Dyson 	}
53038efa82bSJohn Dyson 
53138efa82bSJohn Dyson 	if (bigobj)
53238efa82bSJohn Dyson 		vm_pageout_object_deactivate_pages(map, bigobj, desired, 0);
53338efa82bSJohn Dyson 
53438efa82bSJohn Dyson 	/*
53538efa82bSJohn Dyson 	 * Next, hunt around for other pages to deactivate.  We actually
53638efa82bSJohn Dyson 	 * do this search sort of wrong -- .text first is not the best idea.
53738efa82bSJohn Dyson 	 */
53838efa82bSJohn Dyson 	tmpe = map->header.next;
53938efa82bSJohn Dyson 	while (tmpe != &map->header) {
54038efa82bSJohn Dyson 		if (vm_map_pmap(map)->pm_stats.resident_count <= desired)
54138efa82bSJohn Dyson 			break;
54238efa82bSJohn Dyson 		if ((tmpe->is_sub_map == 0) && (tmpe->is_a_map == 0)) {
54338efa82bSJohn Dyson 			obj = tmpe->object.vm_object;
54401155bd7SDavid Greenman 			if (obj)
54538efa82bSJohn Dyson 				vm_pageout_object_deactivate_pages(map, obj, desired, 0);
54638efa82bSJohn Dyson 		}
54726f9a767SRodney W. Grimes 		tmpe = tmpe->next;
54826f9a767SRodney W. Grimes 	};
54938efa82bSJohn Dyson 
55038efa82bSJohn Dyson 	/*
55138efa82bSJohn Dyson 	 * Remove all mappings if a process is swapped out, this will free page
55238efa82bSJohn Dyson 	 * table pages.
55338efa82bSJohn Dyson 	 */
55438efa82bSJohn Dyson 	if (desired == 0)
55538efa82bSJohn Dyson 		pmap_remove(vm_map_pmap(map),
55638efa82bSJohn Dyson 			VM_MIN_ADDRESS, VM_MAXUSER_ADDRESS);
55738efa82bSJohn Dyson 	vm_map_unlock(map);
55826f9a767SRodney W. Grimes 	vm_map_deallocate(map);
55926f9a767SRodney W. Grimes 	return;
56026f9a767SRodney W. Grimes }
56138efa82bSJohn Dyson #endif
562df8bae1dSRodney W. Grimes 
563df8bae1dSRodney W. Grimes /*
564df8bae1dSRodney W. Grimes  *	vm_pageout_scan does the dirty work for the pageout daemon.
565df8bae1dSRodney W. Grimes  */
5663af76890SPoul-Henning Kamp static int
567df8bae1dSRodney W. Grimes vm_pageout_scan()
568df8bae1dSRodney W. Grimes {
569502ba6e4SJohn Dyson 	vm_page_t m, next;
570b182ec9eSJohn Dyson 	int page_shortage, addl_page_shortage, maxscan, maxlaunder, pcount;
5714e39a515SPoul-Henning Kamp 	int pages_freed;
5725663e6deSDavid Greenman 	struct proc *p, *bigproc;
5735663e6deSDavid Greenman 	vm_offset_t size, bigsize;
574df8bae1dSRodney W. Grimes 	vm_object_t object;
57526f9a767SRodney W. Grimes 	int force_wakeup = 0;
576f6b04d2bSDavid Greenman 	int vnodes_skipped = 0;
5771eeaa1e3SJohn Dyson 	int s;
5780d94caffSDavid Greenman 
579df8bae1dSRodney W. Grimes 	/*
5800d94caffSDavid Greenman 	 * Start scanning the inactive queue for pages we can free. We keep
5810d94caffSDavid Greenman 	 * scanning until we have enough free pages or we have scanned through
5820d94caffSDavid Greenman 	 * the entire queue.  If we encounter dirty pages, we start cleaning
5830d94caffSDavid Greenman 	 * them.
584df8bae1dSRodney W. Grimes 	 */
585df8bae1dSRodney W. Grimes 
586b182ec9eSJohn Dyson 	pages_freed = 0;
587f35329acSJohn Dyson 	addl_page_shortage = 0;
588b182ec9eSJohn Dyson 
5890d94caffSDavid Greenman 	maxlaunder = (cnt.v_inactive_target > MAXLAUNDER) ?
5900d94caffSDavid Greenman 	    MAXLAUNDER : cnt.v_inactive_target;
59167bf6868SJohn Dyson rescan0:
592f6b04d2bSDavid Greenman 	maxscan = cnt.v_inactive_count;
593b182ec9eSJohn Dyson 	for( m = TAILQ_FIRST(&vm_page_queue_inactive);
594b182ec9eSJohn Dyson 
595b182ec9eSJohn Dyson 		(m != NULL) && (maxscan-- > 0) &&
596b18bfc3dSJohn Dyson 			((cnt.v_cache_count + cnt.v_free_count) <
597b182ec9eSJohn Dyson 			(cnt.v_cache_min + cnt.v_free_target));
598b182ec9eSJohn Dyson 
599b182ec9eSJohn Dyson 		m = next) {
600df8bae1dSRodney W. Grimes 
601a58d1fa1SDavid Greenman 		cnt.v_pdpages++;
602b182ec9eSJohn Dyson 
603f35329acSJohn Dyson 		if (m->queue != PQ_INACTIVE) {
60467bf6868SJohn Dyson 			goto rescan0;
605f35329acSJohn Dyson 		}
606b182ec9eSJohn Dyson 
607b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(m, pageq);
608df8bae1dSRodney W. Grimes 
609b182ec9eSJohn Dyson 		if (m->hold_count) {
610f35329acSJohn Dyson 			s = splvm();
611b182ec9eSJohn Dyson 			TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq);
612b182ec9eSJohn Dyson 			TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq);
613f35329acSJohn Dyson 			splx(s);
614b182ec9eSJohn Dyson 			addl_page_shortage++;
615b182ec9eSJohn Dyson 			continue;
616df8bae1dSRodney W. Grimes 		}
61726f9a767SRodney W. Grimes 		/*
618b18bfc3dSJohn Dyson 		 * Dont mess with busy pages, keep in the front of the
619b18bfc3dSJohn Dyson 		 * queue, most likely are being paged out.
62026f9a767SRodney W. Grimes 		 */
621bd7e5f99SJohn Dyson 		if (m->busy || (m->flags & PG_BUSY)) {
622b182ec9eSJohn Dyson 			addl_page_shortage++;
62326f9a767SRodney W. Grimes 			continue;
62426f9a767SRodney W. Grimes 		}
625bd7e5f99SJohn Dyson 
6260d94caffSDavid Greenman 		if (m->object->ref_count == 0) {
6270d94caffSDavid Greenman 			m->flags &= ~PG_REFERENCED;
62867bf6868SJohn Dyson 			pmap_clear_reference(VM_PAGE_TO_PHYS(m));
629ef743ce6SJohn Dyson 		} else if (((m->flags & PG_REFERENCED) == 0) &&
63067bf6868SJohn Dyson 			pmap_ts_referenced(VM_PAGE_TO_PHYS(m))) {
631ef743ce6SJohn Dyson 			vm_page_activate(m);
632ef743ce6SJohn Dyson 			continue;
6332fe6e4d7SDavid Greenman 		}
634ef743ce6SJohn Dyson 
635bd7e5f99SJohn Dyson 		if ((m->flags & PG_REFERENCED) != 0) {
636a647a309SDavid Greenman 			m->flags &= ~PG_REFERENCED;
63767bf6868SJohn Dyson 			pmap_clear_reference(VM_PAGE_TO_PHYS(m));
63826f9a767SRodney W. Grimes 			vm_page_activate(m);
6390d94caffSDavid Greenman 			continue;
6400d94caffSDavid Greenman 		}
64167bf6868SJohn Dyson 
642f6b04d2bSDavid Greenman 		if (m->dirty == 0) {
643bd7e5f99SJohn Dyson 			vm_page_test_dirty(m);
64430dcfc09SJohn Dyson 		} else if (m->dirty != 0) {
645bd7e5f99SJohn Dyson 			m->dirty = VM_PAGE_BITS_ALL;
64630dcfc09SJohn Dyson 		}
647ef743ce6SJohn Dyson 
6486d40c3d3SDavid Greenman 		if (m->valid == 0) {
649bd7e5f99SJohn Dyson 			vm_page_protect(m, VM_PROT_NONE);
6506d40c3d3SDavid Greenman 			vm_page_free(m);
65167bf6868SJohn Dyson 			cnt.v_dfree++;
652f6b04d2bSDavid Greenman 			++pages_freed;
653bd7e5f99SJohn Dyson 		} else if (m->dirty == 0) {
654bd7e5f99SJohn Dyson 			vm_page_cache(m);
655bd7e5f99SJohn Dyson 			++pages_freed;
6560d94caffSDavid Greenman 		} else if (maxlaunder > 0) {
6570d94caffSDavid Greenman 			int written;
658f6b04d2bSDavid Greenman 			struct vnode *vp = NULL;
6590d94caffSDavid Greenman 
6600d94caffSDavid Greenman 			object = m->object;
66124a1cce3SDavid Greenman 			if (object->flags & OBJ_DEAD) {
662f35329acSJohn Dyson 				s = splvm();
66385a376ebSJohn Dyson 				TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq);
66485a376ebSJohn Dyson 				TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq);
665f35329acSJohn Dyson 				splx(s);
6660d94caffSDavid Greenman 				continue;
6670d94caffSDavid Greenman 			}
668f6b04d2bSDavid Greenman 
66924a1cce3SDavid Greenman 			if (object->type == OBJT_VNODE) {
67024a1cce3SDavid Greenman 				vp = object->handle;
671f6b04d2bSDavid Greenman 				if (VOP_ISLOCKED(vp) || vget(vp, 1)) {
672b182ec9eSJohn Dyson 					if ((m->queue == PQ_INACTIVE) &&
673b182ec9eSJohn Dyson 						(m->hold_count == 0) &&
674b182ec9eSJohn Dyson 						(m->busy == 0) &&
675b182ec9eSJohn Dyson 						(m->flags & PG_BUSY) == 0) {
676f35329acSJohn Dyson 						s = splvm();
67785a376ebSJohn Dyson 						TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq);
67885a376ebSJohn Dyson 						TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq);
679f35329acSJohn Dyson 						splx(s);
68085a376ebSJohn Dyson 					}
681aef922f5SJohn Dyson 					if (object->flags & OBJ_MIGHTBEDIRTY)
682f6b04d2bSDavid Greenman 						++vnodes_skipped;
683b182ec9eSJohn Dyson 					continue;
68485a376ebSJohn Dyson 				}
685b182ec9eSJohn Dyson 
686f35329acSJohn Dyson 				/*
687f35329acSJohn Dyson 				 * The page might have been moved to another queue
688f35329acSJohn Dyson 				 * during potential blocking in vget() above.
689f35329acSJohn Dyson 				 */
690b182ec9eSJohn Dyson 				if (m->queue != PQ_INACTIVE) {
691b182ec9eSJohn Dyson 					if (object->flags & OBJ_MIGHTBEDIRTY)
692b182ec9eSJohn Dyson 						++vnodes_skipped;
693b182ec9eSJohn Dyson 					vput(vp);
694b182ec9eSJohn Dyson 					continue;
695b182ec9eSJohn Dyson 				}
696b182ec9eSJohn Dyson 
697f35329acSJohn Dyson 				/*
698f35329acSJohn Dyson 				 * The page may have been busied during the blocking in
699f35329acSJohn Dyson 				 * vput();  We don't move the page back onto the end of
700f35329acSJohn Dyson 				 * the queue so that statistics are more correct if we don't.
701f35329acSJohn Dyson 				 */
702b182ec9eSJohn Dyson 				if (m->busy || (m->flags & PG_BUSY)) {
703b182ec9eSJohn Dyson 					vput(vp);
704b182ec9eSJohn Dyson 					continue;
705b182ec9eSJohn Dyson 				}
706b182ec9eSJohn Dyson 
707f35329acSJohn Dyson 				/*
708f35329acSJohn Dyson 				 * If the page has become held, then skip it
709f35329acSJohn Dyson 				 */
710b182ec9eSJohn Dyson 				if (m->hold_count) {
711f35329acSJohn Dyson 					s = splvm();
712b182ec9eSJohn Dyson 					TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq);
713b182ec9eSJohn Dyson 					TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq);
714f35329acSJohn Dyson 					splx(s);
715b182ec9eSJohn Dyson 					if (object->flags & OBJ_MIGHTBEDIRTY)
716b182ec9eSJohn Dyson 						++vnodes_skipped;
717b182ec9eSJohn Dyson 					vput(vp);
718f6b04d2bSDavid Greenman 					continue;
719f6b04d2bSDavid Greenman 				}
720f6b04d2bSDavid Greenman 			}
721f6b04d2bSDavid Greenman 
7220d94caffSDavid Greenman 			/*
7230d94caffSDavid Greenman 			 * If a page is dirty, then it is either being washed
7240d94caffSDavid Greenman 			 * (but not yet cleaned) or it is still in the
7250d94caffSDavid Greenman 			 * laundry.  If it is still in the laundry, then we
7260d94caffSDavid Greenman 			 * start the cleaning operation.
7270d94caffSDavid Greenman 			 */
7280d94caffSDavid Greenman 			written = vm_pageout_clean(m, 0);
729f6b04d2bSDavid Greenman 
730f6b04d2bSDavid Greenman 			if (vp)
731f6b04d2bSDavid Greenman 				vput(vp);
732f6b04d2bSDavid Greenman 
7330d94caffSDavid Greenman 			maxlaunder -= written;
7340d94caffSDavid Greenman 		}
735df8bae1dSRodney W. Grimes 	}
73626f9a767SRodney W. Grimes 
737df8bae1dSRodney W. Grimes 	/*
7380d94caffSDavid Greenman 	 * Compute the page shortage.  If we are still very low on memory be
7390d94caffSDavid Greenman 	 * sure that we will move a minimal amount of pages from active to
7400d94caffSDavid Greenman 	 * inactive.
741df8bae1dSRodney W. Grimes 	 */
742df8bae1dSRodney W. Grimes 
743b182ec9eSJohn Dyson 	page_shortage = (cnt.v_inactive_target + cnt.v_cache_min) -
7440d94caffSDavid Greenman 	    (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count);
74526f9a767SRodney W. Grimes 	if (page_shortage <= 0) {
74626f9a767SRodney W. Grimes 		if (pages_freed == 0) {
74717c4c408SDavid Greenman 			page_shortage = cnt.v_free_min - cnt.v_free_count;
748f6b04d2bSDavid Greenman 		} else {
749f6b04d2bSDavid Greenman 			page_shortage = 1;
75026f9a767SRodney W. Grimes 		}
751df8bae1dSRodney W. Grimes 	}
752b182ec9eSJohn Dyson 	if (addl_page_shortage) {
753b182ec9eSJohn Dyson 		if (page_shortage < 0)
754b182ec9eSJohn Dyson 			page_shortage = 0;
755b182ec9eSJohn Dyson 		page_shortage += addl_page_shortage;
756b182ec9eSJohn Dyson 	}
75726f9a767SRodney W. Grimes 
758b18bfc3dSJohn Dyson 	pcount = cnt.v_active_count;
759b18bfc3dSJohn Dyson 	m = TAILQ_FIRST(&vm_page_queue_active);
760b18bfc3dSJohn Dyson 	while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) {
761ef743ce6SJohn Dyson 		int refcount;
762f35329acSJohn Dyson 
763f35329acSJohn Dyson 		if (m->queue != PQ_ACTIVE) {
76438efa82bSJohn Dyson 			break;
765f35329acSJohn Dyson 		}
766f35329acSJohn Dyson 
767b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(m, pageq);
768df8bae1dSRodney W. Grimes 		/*
76926f9a767SRodney W. Grimes 		 * Don't deactivate pages that are busy.
770df8bae1dSRodney W. Grimes 		 */
771a647a309SDavid Greenman 		if ((m->busy != 0) ||
7720d94caffSDavid Greenman 		    (m->flags & PG_BUSY) ||
773f6b04d2bSDavid Greenman 		    (m->hold_count != 0)) {
774f35329acSJohn Dyson 			s = splvm();
7756d40c3d3SDavid Greenman 			TAILQ_REMOVE(&vm_page_queue_active, m, pageq);
7766d40c3d3SDavid Greenman 			TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq);
777f35329acSJohn Dyson 			splx(s);
77826f9a767SRodney W. Grimes 			m = next;
77926f9a767SRodney W. Grimes 			continue;
780df8bae1dSRodney W. Grimes 		}
781b18bfc3dSJohn Dyson 
782b18bfc3dSJohn Dyson 		/*
783b18bfc3dSJohn Dyson 		 * The count for pagedaemon pages is done after checking the
784b18bfc3dSJohn Dyson 		 * page for eligbility...
785b18bfc3dSJohn Dyson 		 */
786b18bfc3dSJohn Dyson 		cnt.v_pdpages++;
787ef743ce6SJohn Dyson 
788ef743ce6SJohn Dyson 		refcount = 0;
789ef743ce6SJohn Dyson 		if (m->object->ref_count != 0) {
790ef743ce6SJohn Dyson 			if (m->flags & PG_REFERENCED) {
791ef743ce6SJohn Dyson 				refcount += 1;
7920d94caffSDavid Greenman 			}
79367bf6868SJohn Dyson 			refcount += pmap_ts_referenced(VM_PAGE_TO_PHYS(m));
79438efa82bSJohn Dyson 			if (refcount) {
79538efa82bSJohn Dyson 				m->act_count += ACT_ADVANCE + refcount;
79638efa82bSJohn Dyson 				if (m->act_count > ACT_MAX)
79738efa82bSJohn Dyson 					m->act_count = ACT_MAX;
79838efa82bSJohn Dyson 			}
799b18bfc3dSJohn Dyson 		}
800ef743ce6SJohn Dyson 
801b18bfc3dSJohn Dyson 		m->flags &= ~PG_REFERENCED;
802ef743ce6SJohn Dyson 
80338efa82bSJohn Dyson 		if (refcount && (m->object->ref_count != 0)) {
804f35329acSJohn Dyson 			s = splvm();
80526f9a767SRodney W. Grimes 			TAILQ_REMOVE(&vm_page_queue_active, m, pageq);
80626f9a767SRodney W. Grimes 			TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq);
807f35329acSJohn Dyson 			splx(s);
80826f9a767SRodney W. Grimes 		} else {
80938efa82bSJohn Dyson 			m->act_count -= min(m->act_count, ACT_DECLINE);
81038efa82bSJohn Dyson 			if (vm_pageout_algorithm_lru ||
81138efa82bSJohn Dyson 				(m->object->ref_count == 0) || (m->act_count == 0)) {
8120d94caffSDavid Greenman 				--page_shortage;
813ef743ce6SJohn Dyson 				vm_page_protect(m, VM_PROT_NONE);
81438efa82bSJohn Dyson 				if ((m->dirty == 0) &&
81538efa82bSJohn Dyson 					(m->object->ref_count == 0)) {
8160d94caffSDavid Greenman 					vm_page_cache(m);
8170d94caffSDavid Greenman 				} else {
81826f9a767SRodney W. Grimes 					vm_page_deactivate(m);
819df8bae1dSRodney W. Grimes 				}
82038efa82bSJohn Dyson 			} else {
82138efa82bSJohn Dyson 				s = splvm();
82238efa82bSJohn Dyson 				TAILQ_REMOVE(&vm_page_queue_active, m, pageq);
82338efa82bSJohn Dyson 				TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq);
82438efa82bSJohn Dyson 				splx(s);
82538efa82bSJohn Dyson 			}
826df8bae1dSRodney W. Grimes 		}
82726f9a767SRodney W. Grimes 		m = next;
82826f9a767SRodney W. Grimes 	}
829df8bae1dSRodney W. Grimes 
830f35329acSJohn Dyson 	s = splvm();
831df8bae1dSRodney W. Grimes 	/*
8320d94caffSDavid Greenman 	 * We try to maintain some *really* free pages, this allows interrupt
8330d94caffSDavid Greenman 	 * code to be guaranteed space.
834df8bae1dSRodney W. Grimes 	 */
835a1f6d91cSDavid Greenman 	while (cnt.v_free_count < cnt.v_free_reserved) {
8365070c7f8SJohn Dyson 		static int cache_rover = 0;
8375070c7f8SJohn Dyson 		m = vm_page_list_find(PQ_CACHE, cache_rover);
8380d94caffSDavid Greenman 		if (!m)
8390d94caffSDavid Greenman 			break;
8405070c7f8SJohn Dyson 		cache_rover = (cache_rover + PQ_PRIME2) & PQ_L2_MASK;
8410d94caffSDavid Greenman 		vm_page_free(m);
8420bb3a0d2SDavid Greenman 		cnt.v_dfree++;
84326f9a767SRodney W. Grimes 	}
844f35329acSJohn Dyson 	splx(s);
8455663e6deSDavid Greenman 
8465663e6deSDavid Greenman 	/*
847f6b04d2bSDavid Greenman 	 * If we didn't get enough free pages, and we have skipped a vnode
8484c1f8ee9SDavid Greenman 	 * in a writeable object, wakeup the sync daemon.  And kick swapout
8494c1f8ee9SDavid Greenman 	 * if we did not get enough free pages.
850f6b04d2bSDavid Greenman 	 */
851bd7e5f99SJohn Dyson 	if ((cnt.v_cache_count + cnt.v_free_count) <
852bd7e5f99SJohn Dyson 		(cnt.v_free_target + cnt.v_cache_min) ) {
853f6b04d2bSDavid Greenman 		if (vnodes_skipped &&
854f6b04d2bSDavid Greenman 		    (cnt.v_cache_count + cnt.v_free_count) < cnt.v_free_min) {
855f6b04d2bSDavid Greenman 			if (!vfs_update_wakeup) {
856f6b04d2bSDavid Greenman 				vfs_update_wakeup = 1;
85724a1cce3SDavid Greenman 				wakeup(&vfs_update_wakeup);
858f6b04d2bSDavid Greenman 			}
859f6b04d2bSDavid Greenman 		}
86038efa82bSJohn Dyson #if !defined(NO_SWAPPING)
86138efa82bSJohn Dyson 		if (vm_swapping_enabled &&
86238efa82bSJohn Dyson 			(cnt.v_free_count + cnt.v_cache_count < cnt.v_free_target)) {
8634c1f8ee9SDavid Greenman 			vm_req_vmdaemon();
864b18bfc3dSJohn Dyson 			vm_pageout_req_swapout = 1;
8654c1f8ee9SDavid Greenman 		}
8665afce282SDavid Greenman #endif
8674c1f8ee9SDavid Greenman 	}
8684c1f8ee9SDavid Greenman 
869f6b04d2bSDavid Greenman 
870f6b04d2bSDavid Greenman 	/*
8710d94caffSDavid Greenman 	 * make sure that we have swap space -- if we are low on memory and
8720d94caffSDavid Greenman 	 * swap -- then kill the biggest process.
8735663e6deSDavid Greenman 	 */
8745663e6deSDavid Greenman 	if ((vm_swap_size == 0 || swap_pager_full) &&
8750d94caffSDavid Greenman 	    ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_free_min)) {
8765663e6deSDavid Greenman 		bigproc = NULL;
8775663e6deSDavid Greenman 		bigsize = 0;
8781b67ec6dSJeffrey Hsu 		for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
8795663e6deSDavid Greenman 			/*
8805663e6deSDavid Greenman 			 * if this is a system process, skip it
8815663e6deSDavid Greenman 			 */
88279221631SDavid Greenman 			if ((p->p_flag & P_SYSTEM) || (p->p_pid == 1) ||
88379221631SDavid Greenman 			    ((p->p_pid < 48) && (vm_swap_size != 0))) {
8845663e6deSDavid Greenman 				continue;
8855663e6deSDavid Greenman 			}
8865663e6deSDavid Greenman 			/*
8875663e6deSDavid Greenman 			 * if the process is in a non-running type state,
8885663e6deSDavid Greenman 			 * don't touch it.
8895663e6deSDavid Greenman 			 */
8905663e6deSDavid Greenman 			if (p->p_stat != SRUN && p->p_stat != SSLEEP) {
8915663e6deSDavid Greenman 				continue;
8925663e6deSDavid Greenman 			}
8935663e6deSDavid Greenman 			/*
8945663e6deSDavid Greenman 			 * get the process size
8955663e6deSDavid Greenman 			 */
8965663e6deSDavid Greenman 			size = p->p_vmspace->vm_pmap.pm_stats.resident_count;
8975663e6deSDavid Greenman 			/*
8985663e6deSDavid Greenman 			 * if the this process is bigger than the biggest one
8995663e6deSDavid Greenman 			 * remember it.
9005663e6deSDavid Greenman 			 */
9015663e6deSDavid Greenman 			if (size > bigsize) {
9025663e6deSDavid Greenman 				bigproc = p;
9035663e6deSDavid Greenman 				bigsize = size;
9045663e6deSDavid Greenman 			}
9055663e6deSDavid Greenman 		}
9065663e6deSDavid Greenman 		if (bigproc != NULL) {
907729b1e51SDavid Greenman 			killproc(bigproc, "out of swap space");
9085663e6deSDavid Greenman 			bigproc->p_estcpu = 0;
9095663e6deSDavid Greenman 			bigproc->p_nice = PRIO_MIN;
9105663e6deSDavid Greenman 			resetpriority(bigproc);
91124a1cce3SDavid Greenman 			wakeup(&cnt.v_free_count);
9125663e6deSDavid Greenman 		}
9135663e6deSDavid Greenman 	}
91426f9a767SRodney W. Grimes 	return force_wakeup;
91526f9a767SRodney W. Grimes }
91626f9a767SRodney W. Grimes 
917b182ec9eSJohn Dyson static int
918b182ec9eSJohn Dyson vm_pageout_free_page_calc(count)
919b182ec9eSJohn Dyson vm_size_t count;
920b182ec9eSJohn Dyson {
921b182ec9eSJohn Dyson 	if (count < cnt.v_page_count)
922b182ec9eSJohn Dyson 		 return 0;
923b182ec9eSJohn Dyson 	/*
924b182ec9eSJohn Dyson 	 * free_reserved needs to include enough for the largest swap pager
925b182ec9eSJohn Dyson 	 * structures plus enough for any pv_entry structs when paging.
926b182ec9eSJohn Dyson 	 */
927b182ec9eSJohn Dyson 	if (cnt.v_page_count > 1024)
928b182ec9eSJohn Dyson 		cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200;
929b182ec9eSJohn Dyson 	else
930b182ec9eSJohn Dyson 		cnt.v_free_min = 4;
931f35329acSJohn Dyson 	cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE +
932f35329acSJohn Dyson 		cnt.v_interrupt_free_min;
933f35329acSJohn Dyson 	cnt.v_free_reserved = vm_pageout_page_count +
934a2f4a846SJohn Dyson 		cnt.v_pageout_free_min + (count / 768) + PQ_L2_SIZE;
935a2f4a846SJohn Dyson 	cnt.v_free_min += cnt.v_free_reserved;
936b182ec9eSJohn Dyson 	return 1;
937b182ec9eSJohn Dyson }
938b182ec9eSJohn Dyson 
939b182ec9eSJohn Dyson 
940f35329acSJohn Dyson #ifdef unused
941b182ec9eSJohn Dyson int
942b182ec9eSJohn Dyson vm_pageout_free_pages(object, add)
943b182ec9eSJohn Dyson vm_object_t object;
944b182ec9eSJohn Dyson int add;
945b182ec9eSJohn Dyson {
946b182ec9eSJohn Dyson 	return vm_pageout_free_page_calc(object->size);
947b182ec9eSJohn Dyson }
948f35329acSJohn Dyson #endif
949b182ec9eSJohn Dyson 
950df8bae1dSRodney W. Grimes /*
951df8bae1dSRodney W. Grimes  *	vm_pageout is the high level pageout daemon.
952df8bae1dSRodney W. Grimes  */
9532b14f991SJulian Elischer static void
95426f9a767SRodney W. Grimes vm_pageout()
955df8bae1dSRodney W. Grimes {
956df8bae1dSRodney W. Grimes 	(void) spl0();
957df8bae1dSRodney W. Grimes 
958df8bae1dSRodney W. Grimes 	/*
959df8bae1dSRodney W. Grimes 	 * Initialize some paging parameters.
960df8bae1dSRodney W. Grimes 	 */
961df8bae1dSRodney W. Grimes 
962f6b04d2bSDavid Greenman 	cnt.v_interrupt_free_min = 2;
963f35329acSJohn Dyson 	if (cnt.v_page_count < 2000)
964f35329acSJohn Dyson 		vm_pageout_page_count = 8;
965f6b04d2bSDavid Greenman 
966b182ec9eSJohn Dyson 	vm_pageout_free_page_calc(cnt.v_page_count);
967ed74321bSDavid Greenman 	/*
9680d94caffSDavid Greenman 	 * free_reserved needs to include enough for the largest swap pager
9690d94caffSDavid Greenman 	 * structures plus enough for any pv_entry structs when paging.
970ed74321bSDavid Greenman 	 */
9710d94caffSDavid Greenman 	cnt.v_free_target = 3 * cnt.v_free_min + cnt.v_free_reserved;
9726f2b142eSDavid Greenman 
9736ac5bfdbSJohn Dyson 	if (cnt.v_free_count > 1024) {
9740d94caffSDavid Greenman 		cnt.v_cache_max = (cnt.v_free_count - 1024) / 2;
9756f2b142eSDavid Greenman 		cnt.v_cache_min = (cnt.v_free_count - 1024) / 8;
9766f2b142eSDavid Greenman 		cnt.v_inactive_target = 2*cnt.v_cache_min + 192;
9770d94caffSDavid Greenman 	} else {
9780d94caffSDavid Greenman 		cnt.v_cache_min = 0;
9790d94caffSDavid Greenman 		cnt.v_cache_max = 0;
9806f2b142eSDavid Greenman 		cnt.v_inactive_target = cnt.v_free_count / 4;
9810d94caffSDavid Greenman 	}
982df8bae1dSRodney W. Grimes 
983df8bae1dSRodney W. Grimes 	/* XXX does not really belong here */
984df8bae1dSRodney W. Grimes 	if (vm_page_max_wired == 0)
985df8bae1dSRodney W. Grimes 		vm_page_max_wired = cnt.v_free_count / 3;
986df8bae1dSRodney W. Grimes 
98726f9a767SRodney W. Grimes 
98824a1cce3SDavid Greenman 	swap_pager_swap_init();
989df8bae1dSRodney W. Grimes 	/*
9900d94caffSDavid Greenman 	 * The pageout daemon is never done, so loop forever.
991df8bae1dSRodney W. Grimes 	 */
992df8bae1dSRodney W. Grimes 	while (TRUE) {
99385a376ebSJohn Dyson 		int inactive_target;
994b18bfc3dSJohn Dyson 		int s = splvm();
995f919ebdeSDavid Greenman 		if (!vm_pages_needed ||
996545901f7SJohn Dyson 			((cnt.v_free_count + cnt.v_cache_count) > cnt.v_free_min)) {
997f919ebdeSDavid Greenman 			vm_pages_needed = 0;
99824a1cce3SDavid Greenman 			tsleep(&vm_pages_needed, PVM, "psleep", 0);
999b18bfc3dSJohn Dyson 		} else if (!vm_pages_needed) {
100038efa82bSJohn Dyson 			tsleep(&vm_pages_needed, PVM, "psleep", hz/10);
1001f919ebdeSDavid Greenman 		}
1002b182ec9eSJohn Dyson 		inactive_target =
1003b182ec9eSJohn Dyson 			(cnt.v_page_count - cnt.v_wire_count) / 4;
1004b182ec9eSJohn Dyson 		if (inactive_target < 2*cnt.v_free_min)
1005b182ec9eSJohn Dyson 			inactive_target = 2*cnt.v_free_min;
1006b182ec9eSJohn Dyson 		cnt.v_inactive_target = inactive_target;
1007b18bfc3dSJohn Dyson 		if (vm_pages_needed)
1008b18bfc3dSJohn Dyson 			cnt.v_pdwakeups++;
1009f919ebdeSDavid Greenman 		vm_pages_needed = 0;
1010f919ebdeSDavid Greenman 		splx(s);
1011df8bae1dSRodney W. Grimes 		vm_pager_sync();
10120d94caffSDavid Greenman 		vm_pageout_scan();
101326f9a767SRodney W. Grimes 		vm_pager_sync();
101424a1cce3SDavid Greenman 		wakeup(&cnt.v_free_count);
1015df8bae1dSRodney W. Grimes 	}
1016df8bae1dSRodney W. Grimes }
101726f9a767SRodney W. Grimes 
1018e0c5a895SJohn Dyson void
1019e0c5a895SJohn Dyson pagedaemon_wakeup()
1020e0c5a895SJohn Dyson {
1021e0c5a895SJohn Dyson 	if (!vm_pages_needed && curproc != pageproc) {
1022e0c5a895SJohn Dyson 		vm_pages_needed++;
1023e0c5a895SJohn Dyson 		wakeup(&vm_pages_needed);
1024e0c5a895SJohn Dyson 	}
1025e0c5a895SJohn Dyson }
1026e0c5a895SJohn Dyson 
102738efa82bSJohn Dyson #if !defined(NO_SWAPPING)
10285afce282SDavid Greenman static void
10295afce282SDavid Greenman vm_req_vmdaemon()
10305afce282SDavid Greenman {
10315afce282SDavid Greenman 	static int lastrun = 0;
10325afce282SDavid Greenman 
1033b18bfc3dSJohn Dyson 	if ((ticks > (lastrun + hz)) || (ticks < lastrun)) {
10345afce282SDavid Greenman 		wakeup(&vm_daemon_needed);
10355afce282SDavid Greenman 		lastrun = ticks;
10365afce282SDavid Greenman 	}
10375afce282SDavid Greenman }
10385afce282SDavid Greenman 
10392b14f991SJulian Elischer static void
10404f9fb771SBruce Evans vm_daemon()
10410d94caffSDavid Greenman {
10422fe6e4d7SDavid Greenman 	vm_object_t object;
10432fe6e4d7SDavid Greenman 	struct proc *p;
10440d94caffSDavid Greenman 
1045bd105bb7SBruce Evans 	(void) spl0();
1046bd105bb7SBruce Evans 
10472fe6e4d7SDavid Greenman 	while (TRUE) {
104824a1cce3SDavid Greenman 		tsleep(&vm_daemon_needed, PUSER, "psleep", 0);
10494c1f8ee9SDavid Greenman 		if (vm_pageout_req_swapout) {
10506306c897SDavid Greenman 			swapout_procs();
10514c1f8ee9SDavid Greenman 			vm_pageout_req_swapout = 0;
10524c1f8ee9SDavid Greenman 		}
10532fe6e4d7SDavid Greenman 		/*
10540d94caffSDavid Greenman 		 * scan the processes for exceeding their rlimits or if
10550d94caffSDavid Greenman 		 * process is swapped out -- deactivate pages
10562fe6e4d7SDavid Greenman 		 */
10572fe6e4d7SDavid Greenman 
10581b67ec6dSJeffrey Hsu 		for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
10592fe6e4d7SDavid Greenman 			quad_t limit;
10602fe6e4d7SDavid Greenman 			vm_offset_t size;
10612fe6e4d7SDavid Greenman 
10622fe6e4d7SDavid Greenman 			/*
10632fe6e4d7SDavid Greenman 			 * if this is a system process or if we have already
10642fe6e4d7SDavid Greenman 			 * looked at this process, skip it.
10652fe6e4d7SDavid Greenman 			 */
10662fe6e4d7SDavid Greenman 			if (p->p_flag & (P_SYSTEM | P_WEXIT)) {
10672fe6e4d7SDavid Greenman 				continue;
10682fe6e4d7SDavid Greenman 			}
10692fe6e4d7SDavid Greenman 			/*
10702fe6e4d7SDavid Greenman 			 * if the process is in a non-running type state,
10712fe6e4d7SDavid Greenman 			 * don't touch it.
10722fe6e4d7SDavid Greenman 			 */
10732fe6e4d7SDavid Greenman 			if (p->p_stat != SRUN && p->p_stat != SSLEEP) {
10742fe6e4d7SDavid Greenman 				continue;
10752fe6e4d7SDavid Greenman 			}
10762fe6e4d7SDavid Greenman 			/*
10772fe6e4d7SDavid Greenman 			 * get a limit
10782fe6e4d7SDavid Greenman 			 */
10792fe6e4d7SDavid Greenman 			limit = qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur,
10802fe6e4d7SDavid Greenman 			    p->p_rlimit[RLIMIT_RSS].rlim_max);
10812fe6e4d7SDavid Greenman 
10822fe6e4d7SDavid Greenman 			/*
10830d94caffSDavid Greenman 			 * let processes that are swapped out really be
10840d94caffSDavid Greenman 			 * swapped out set the limit to nothing (will force a
10850d94caffSDavid Greenman 			 * swap-out.)
10862fe6e4d7SDavid Greenman 			 */
10872fe6e4d7SDavid Greenman 			if ((p->p_flag & P_INMEM) == 0)
10880d94caffSDavid Greenman 				limit = 0;	/* XXX */
10892fe6e4d7SDavid Greenman 
1090a91c5a7eSJohn Dyson 			size = p->p_vmspace->vm_pmap.pm_stats.resident_count * PAGE_SIZE;
10912fe6e4d7SDavid Greenman 			if (limit >= 0 && size >= limit) {
10922fe6e4d7SDavid Greenman 				vm_pageout_map_deactivate_pages(&p->p_vmspace->vm_map,
109338efa82bSJohn Dyson 				    (vm_pindex_t)(limit >> PAGE_SHIFT) );
10942fe6e4d7SDavid Greenman 			}
10952fe6e4d7SDavid Greenman 		}
10962fe6e4d7SDavid Greenman 
10970d94caffSDavid Greenman 		/*
10980d94caffSDavid Greenman 		 * we remove cached objects that have no RSS...
10990d94caffSDavid Greenman 		 */
11000d94caffSDavid Greenman restart:
1101b18bfc3dSJohn Dyson 		object = TAILQ_FIRST(&vm_object_cached_list);
11022fe6e4d7SDavid Greenman 		while (object) {
11032fe6e4d7SDavid Greenman 			/*
11042fe6e4d7SDavid Greenman 			 * if there are no resident pages -- get rid of the object
11052fe6e4d7SDavid Greenman 			 */
11062fe6e4d7SDavid Greenman 			if (object->resident_page_count == 0) {
110724a1cce3SDavid Greenman 				vm_object_reference(object);
11082fe6e4d7SDavid Greenman 				pager_cache(object, FALSE);
11092fe6e4d7SDavid Greenman 				goto restart;
11102fe6e4d7SDavid Greenman 			}
1111b18bfc3dSJohn Dyson 			object = TAILQ_NEXT(object, cached_list);
11122fe6e4d7SDavid Greenman 		}
111324a1cce3SDavid Greenman 	}
11142fe6e4d7SDavid Greenman }
111538efa82bSJohn Dyson #endif
1116