xref: /freebsd/sys/vm/vm_pageout.c (revision 0cddd8f02397756cbeca5c5777d5895f3e419d88)
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
215929bcfaSPhilippe Charnier  *    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  *
68c3aac50fSPeter Wemm  * $FreeBSD$
69df8bae1dSRodney W. Grimes  */
70df8bae1dSRodney W. Grimes 
71df8bae1dSRodney W. Grimes /*
72df8bae1dSRodney W. Grimes  *	The proverbial page-out daemon.
73df8bae1dSRodney W. Grimes  */
74df8bae1dSRodney W. Grimes 
75faa5f8d8SAndrzej Bialecki #include "opt_vm.h"
76df8bae1dSRodney W. Grimes #include <sys/param.h>
7726f9a767SRodney W. Grimes #include <sys/systm.h>
78b5e8ce9fSBruce Evans #include <sys/kernel.h>
79fb919e4dSMark Murray #include <sys/lock.h>
80fb919e4dSMark Murray #include <sys/mutex.h>
8126f9a767SRodney W. Grimes #include <sys/proc.h>
829c8b8baaSPeter Wemm #include <sys/kthread.h>
830384fff8SJason Evans #include <sys/ktr.h>
8426f9a767SRodney W. Grimes #include <sys/resourcevar.h>
85d2fc5315SPoul-Henning Kamp #include <sys/signalvar.h>
86f6b04d2bSDavid Greenman #include <sys/vnode.h>
87efeaf95aSDavid Greenman #include <sys/vmmeter.h>
881005a129SJohn Baldwin #include <sys/sx.h>
8938efa82bSJohn Dyson #include <sys/sysctl.h>
90df8bae1dSRodney W. Grimes 
91df8bae1dSRodney W. Grimes #include <vm/vm.h>
92efeaf95aSDavid Greenman #include <vm/vm_param.h>
93efeaf95aSDavid Greenman #include <vm/vm_object.h>
94df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
95efeaf95aSDavid Greenman #include <vm/vm_map.h>
96df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
9724a1cce3SDavid Greenman #include <vm/vm_pager.h>
9821cd6e62SSeigo Tanimura #include <vm/vm_zone.h>
9905f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h>
100efeaf95aSDavid Greenman #include <vm/vm_extern.h>
101df8bae1dSRodney W. Grimes 
1020384fff8SJason Evans #include <machine/mutex.h>
1030384fff8SJason Evans 
1042b14f991SJulian Elischer /*
1052b14f991SJulian Elischer  * System initialization
1062b14f991SJulian Elischer  */
1072b14f991SJulian Elischer 
1082b14f991SJulian Elischer /* the kernel process "vm_pageout"*/
1092b14f991SJulian Elischer static void vm_pageout __P((void));
1108f9110f6SJohn Dyson static int vm_pageout_clean __P((vm_page_t));
1112b6b0df7SMatthew Dillon static void vm_pageout_scan __P((int pass));
112f35329acSJohn Dyson static int vm_pageout_free_page_calc __P((vm_size_t count));
1132b14f991SJulian Elischer struct proc *pageproc;
1142b14f991SJulian Elischer 
1152b14f991SJulian Elischer static struct kproc_desc page_kp = {
1162b14f991SJulian Elischer 	"pagedaemon",
1172b14f991SJulian Elischer 	vm_pageout,
1182b14f991SJulian Elischer 	&pageproc
1192b14f991SJulian Elischer };
1209c8b8baaSPeter Wemm SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, &page_kp)
1212b14f991SJulian Elischer 
12238efa82bSJohn Dyson #if !defined(NO_SWAPPING)
1232b14f991SJulian Elischer /* the kernel process "vm_daemon"*/
1242b14f991SJulian Elischer static void vm_daemon __P((void));
125f708ef1bSPoul-Henning Kamp static struct	proc *vmproc;
1262b14f991SJulian Elischer 
1272b14f991SJulian Elischer static struct kproc_desc vm_kp = {
1282b14f991SJulian Elischer 	"vmdaemon",
1292b14f991SJulian Elischer 	vm_daemon,
1302b14f991SJulian Elischer 	&vmproc
1312b14f991SJulian Elischer };
1329c8b8baaSPeter Wemm SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp)
13338efa82bSJohn Dyson #endif
1342b14f991SJulian Elischer 
1352b14f991SJulian Elischer 
1362d8acc0fSJohn Dyson int vm_pages_needed=0;		/* Event on which pageout daemon sleeps */
1372d8acc0fSJohn Dyson int vm_pageout_deficit=0;	/* Estimated number of pages deficit */
1382d8acc0fSJohn Dyson int vm_pageout_pages_needed=0;	/* flag saying that the pageout daemon needs pages */
13926f9a767SRodney W. Grimes 
14038efa82bSJohn Dyson #if !defined(NO_SWAPPING)
141f708ef1bSPoul-Henning Kamp static int vm_pageout_req_swapout;	/* XXX */
142f708ef1bSPoul-Henning Kamp static int vm_daemon_needed;
14338efa82bSJohn Dyson #endif
1445663e6deSDavid Greenman extern int vm_swap_size;
1452b6b0df7SMatthew Dillon static int vm_max_launder = 32;
146303b270bSEivind Eklund static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0;
147303b270bSEivind Eklund static int vm_pageout_full_stats_interval = 0;
1482b6b0df7SMatthew Dillon static int vm_pageout_stats_free_max=0, vm_pageout_algorithm=0;
149303b270bSEivind Eklund static int defer_swap_pageouts=0;
150303b270bSEivind Eklund static int disable_swap_pageouts=0;
15170111b90SJohn Dyson 
15238efa82bSJohn Dyson #if defined(NO_SWAPPING)
153303b270bSEivind Eklund static int vm_swap_enabled=0;
154303b270bSEivind Eklund static int vm_swap_idle_enabled=0;
15538efa82bSJohn Dyson #else
156303b270bSEivind Eklund static int vm_swap_enabled=1;
157303b270bSEivind Eklund static int vm_swap_idle_enabled=0;
15838efa82bSJohn Dyson #endif
15938efa82bSJohn Dyson 
16038efa82bSJohn Dyson SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm,
1612b6b0df7SMatthew Dillon 	CTLFLAG_RW, &vm_pageout_algorithm, 0, "LRU page mgmt");
1622b6b0df7SMatthew Dillon 
1632b6b0df7SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, max_launder,
1642b6b0df7SMatthew Dillon 	CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout");
16538efa82bSJohn Dyson 
166dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max,
167b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length");
168dc2efb27SJohn Dyson 
169dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval,
170b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan");
171dc2efb27SJohn Dyson 
172dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval,
173b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan");
174dc2efb27SJohn Dyson 
175dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_free_max,
176b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_pageout_stats_free_max, 0, "Not implemented");
177dc2efb27SJohn Dyson 
17838efa82bSJohn Dyson #if defined(NO_SWAPPING)
179ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
180ceb0cf87SJohn Dyson 	CTLFLAG_RD, &vm_swap_enabled, 0, "");
181ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
182ceb0cf87SJohn Dyson 	CTLFLAG_RD, &vm_swap_idle_enabled, 0, "");
18338efa82bSJohn Dyson #else
184ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
185b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout");
186ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
187b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
18838efa82bSJohn Dyson #endif
18926f9a767SRodney W. Grimes 
190ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts,
191b0359e2cSPeter Wemm 	CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem");
19212ac6a1dSJohn Dyson 
193ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts,
194b0359e2cSPeter Wemm 	CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages");
19512ac6a1dSJohn Dyson 
196ffc82b0aSJohn Dyson #define VM_PAGEOUT_PAGE_COUNT 16
197bbc0ec52SDavid Greenman int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT;
198df8bae1dSRodney W. Grimes 
199c3cb3e12SDavid Greenman int vm_page_max_wired;		/* XXX max # of wired pages system-wide */
200df8bae1dSRodney W. Grimes 
20138efa82bSJohn Dyson #if !defined(NO_SWAPPING)
20238efa82bSJohn Dyson typedef void freeer_fcn_t __P((vm_map_t, vm_object_t, vm_pindex_t, int));
20338efa82bSJohn Dyson static void vm_pageout_map_deactivate_pages __P((vm_map_t, vm_pindex_t));
204cd41fc12SDavid Greenman static freeer_fcn_t vm_pageout_object_deactivate_pages;
205cd41fc12SDavid Greenman static void vm_req_vmdaemon __P((void));
20638efa82bSJohn Dyson #endif
207dc2efb27SJohn Dyson static void vm_pageout_page_stats(void);
208cd41fc12SDavid Greenman 
20926f9a767SRodney W. Grimes /*
21026f9a767SRodney W. Grimes  * vm_pageout_clean:
21124a1cce3SDavid Greenman  *
2120d94caffSDavid Greenman  * Clean the page and remove it from the laundry.
21326f9a767SRodney W. Grimes  *
2140d94caffSDavid Greenman  * We set the busy bit to cause potential page faults on this page to
2151c7c3c6aSMatthew Dillon  * block.  Note the careful timing, however, the busy bit isn't set till
2161c7c3c6aSMatthew Dillon  * late and we cannot do anything that will mess with the page.
21726f9a767SRodney W. Grimes  */
2181c7c3c6aSMatthew Dillon 
2193af76890SPoul-Henning Kamp static int
2208f9110f6SJohn Dyson vm_pageout_clean(m)
22124a1cce3SDavid Greenman 	vm_page_t m;
22224a1cce3SDavid Greenman {
22326f9a767SRodney W. Grimes 	register vm_object_t object;
224f35329acSJohn Dyson 	vm_page_t mc[2*vm_pageout_page_count];
22524a1cce3SDavid Greenman 	int pageout_count;
22690ecac61SMatthew Dillon 	int ib, is, page_base;
227a316d390SJohn Dyson 	vm_pindex_t pindex = m->pindex;
22826f9a767SRodney W. Grimes 
2290cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
2300cddd8f0SMatthew Dillon 
23126f9a767SRodney W. Grimes 	object = m->object;
23224a1cce3SDavid Greenman 
23326f9a767SRodney W. Grimes 	/*
2341c7c3c6aSMatthew Dillon 	 * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP
2351c7c3c6aSMatthew Dillon 	 * with the new swapper, but we could have serious problems paging
2361c7c3c6aSMatthew Dillon 	 * out other object types if there is insufficient memory.
2371c7c3c6aSMatthew Dillon 	 *
2381c7c3c6aSMatthew Dillon 	 * Unfortunately, checking free memory here is far too late, so the
2391c7c3c6aSMatthew Dillon 	 * check has been moved up a procedural level.
2401c7c3c6aSMatthew Dillon 	 */
2411c7c3c6aSMatthew Dillon 
24224a1cce3SDavid Greenman 	/*
2438b03c8edSMatthew Dillon 	 * Don't mess with the page if it's busy, held, or special
24424a1cce3SDavid Greenman 	 */
2458f9110f6SJohn Dyson 	if ((m->hold_count != 0) ||
2468b03c8edSMatthew Dillon 	    ((m->busy != 0) || (m->flags & (PG_BUSY|PG_UNMANAGED)))) {
2470d94caffSDavid Greenman 		return 0;
2488b03c8edSMatthew Dillon 	}
2490d94caffSDavid Greenman 
250f35329acSJohn Dyson 	mc[vm_pageout_page_count] = m;
25126f9a767SRodney W. Grimes 	pageout_count = 1;
252f35329acSJohn Dyson 	page_base = vm_pageout_page_count;
25390ecac61SMatthew Dillon 	ib = 1;
25490ecac61SMatthew Dillon 	is = 1;
25590ecac61SMatthew Dillon 
25624a1cce3SDavid Greenman 	/*
25724a1cce3SDavid Greenman 	 * Scan object for clusterable pages.
25824a1cce3SDavid Greenman 	 *
25924a1cce3SDavid Greenman 	 * We can cluster ONLY if: ->> the page is NOT
26024a1cce3SDavid Greenman 	 * clean, wired, busy, held, or mapped into a
26124a1cce3SDavid Greenman 	 * buffer, and one of the following:
26224a1cce3SDavid Greenman 	 * 1) The page is inactive, or a seldom used
26324a1cce3SDavid Greenman 	 *    active page.
26424a1cce3SDavid Greenman 	 * -or-
26524a1cce3SDavid Greenman 	 * 2) we force the issue.
26690ecac61SMatthew Dillon 	 *
26790ecac61SMatthew Dillon 	 * During heavy mmap/modification loads the pageout
26890ecac61SMatthew Dillon 	 * daemon can really fragment the underlying file
26990ecac61SMatthew Dillon 	 * due to flushing pages out of order and not trying
27090ecac61SMatthew Dillon 	 * align the clusters (which leave sporatic out-of-order
27190ecac61SMatthew Dillon 	 * holes).  To solve this problem we do the reverse scan
27290ecac61SMatthew Dillon 	 * first and attempt to align our cluster, then do a
27390ecac61SMatthew Dillon 	 * forward scan if room remains.
27424a1cce3SDavid Greenman 	 */
27590ecac61SMatthew Dillon 
27690ecac61SMatthew Dillon more:
27790ecac61SMatthew Dillon 	while (ib && pageout_count < vm_pageout_page_count) {
27824a1cce3SDavid Greenman 		vm_page_t p;
279f6b04d2bSDavid Greenman 
28090ecac61SMatthew Dillon 		if (ib > pindex) {
28190ecac61SMatthew Dillon 			ib = 0;
28290ecac61SMatthew Dillon 			break;
283f6b04d2bSDavid Greenman 		}
28490ecac61SMatthew Dillon 
28590ecac61SMatthew Dillon 		if ((p = vm_page_lookup(object, pindex - ib)) == NULL) {
28690ecac61SMatthew Dillon 			ib = 0;
28790ecac61SMatthew Dillon 			break;
28890ecac61SMatthew Dillon 		}
2895070c7f8SJohn Dyson 		if (((p->queue - p->pc) == PQ_CACHE) ||
2908b03c8edSMatthew Dillon 		    (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) {
29190ecac61SMatthew Dillon 			ib = 0;
29290ecac61SMatthew Dillon 			break;
293f6b04d2bSDavid Greenman 		}
29424a1cce3SDavid Greenman 		vm_page_test_dirty(p);
29590ecac61SMatthew Dillon 		if ((p->dirty & p->valid) == 0 ||
29690ecac61SMatthew Dillon 		    p->queue != PQ_INACTIVE ||
29790ecac61SMatthew Dillon 		    p->wire_count != 0 ||
29890ecac61SMatthew Dillon 		    p->hold_count != 0) {
29990ecac61SMatthew Dillon 			ib = 0;
30024a1cce3SDavid Greenman 			break;
301f6b04d2bSDavid Greenman 		}
30290ecac61SMatthew Dillon 		mc[--page_base] = p;
30390ecac61SMatthew Dillon 		++pageout_count;
30490ecac61SMatthew Dillon 		++ib;
30524a1cce3SDavid Greenman 		/*
30690ecac61SMatthew Dillon 		 * alignment boundry, stop here and switch directions.  Do
30790ecac61SMatthew Dillon 		 * not clear ib.
30824a1cce3SDavid Greenman 		 */
30990ecac61SMatthew Dillon 		if ((pindex - (ib - 1)) % vm_pageout_page_count == 0)
31090ecac61SMatthew Dillon 			break;
31124a1cce3SDavid Greenman 	}
31290ecac61SMatthew Dillon 
31390ecac61SMatthew Dillon 	while (pageout_count < vm_pageout_page_count &&
31490ecac61SMatthew Dillon 	    pindex + is < object->size) {
31590ecac61SMatthew Dillon 		vm_page_t p;
31690ecac61SMatthew Dillon 
31790ecac61SMatthew Dillon 		if ((p = vm_page_lookup(object, pindex + is)) == NULL)
31890ecac61SMatthew Dillon 			break;
3195070c7f8SJohn Dyson 		if (((p->queue - p->pc) == PQ_CACHE) ||
3208b03c8edSMatthew Dillon 		    (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) {
32190ecac61SMatthew Dillon 			break;
32224a1cce3SDavid Greenman 		}
32324a1cce3SDavid Greenman 		vm_page_test_dirty(p);
32490ecac61SMatthew Dillon 		if ((p->dirty & p->valid) == 0 ||
32590ecac61SMatthew Dillon 		    p->queue != PQ_INACTIVE ||
32690ecac61SMatthew Dillon 		    p->wire_count != 0 ||
32790ecac61SMatthew Dillon 		    p->hold_count != 0) {
32824a1cce3SDavid Greenman 			break;
32924a1cce3SDavid Greenman 		}
33090ecac61SMatthew Dillon 		mc[page_base + pageout_count] = p;
33190ecac61SMatthew Dillon 		++pageout_count;
33290ecac61SMatthew Dillon 		++is;
33324a1cce3SDavid Greenman 	}
33490ecac61SMatthew Dillon 
33590ecac61SMatthew Dillon 	/*
33690ecac61SMatthew Dillon 	 * If we exhausted our forward scan, continue with the reverse scan
33790ecac61SMatthew Dillon 	 * when possible, even past a page boundry.  This catches boundry
33890ecac61SMatthew Dillon 	 * conditions.
33990ecac61SMatthew Dillon 	 */
34090ecac61SMatthew Dillon 	if (ib && pageout_count < vm_pageout_page_count)
34190ecac61SMatthew Dillon 		goto more;
342f6b04d2bSDavid Greenman 
34367bf6868SJohn Dyson 	/*
34467bf6868SJohn Dyson 	 * we allow reads during pageouts...
34567bf6868SJohn Dyson 	 */
3468f9110f6SJohn Dyson 	return vm_pageout_flush(&mc[page_base], pageout_count, 0);
347aef922f5SJohn Dyson }
348aef922f5SJohn Dyson 
3491c7c3c6aSMatthew Dillon /*
3501c7c3c6aSMatthew Dillon  * vm_pageout_flush() - launder the given pages
3511c7c3c6aSMatthew Dillon  *
3521c7c3c6aSMatthew Dillon  *	The given pages are laundered.  Note that we setup for the start of
3531c7c3c6aSMatthew Dillon  *	I/O ( i.e. busy the page ), mark it read-only, and bump the object
3541c7c3c6aSMatthew Dillon  *	reference count all in here rather then in the parent.  If we want
3551c7c3c6aSMatthew Dillon  *	the parent to do more sophisticated things we may have to change
3561c7c3c6aSMatthew Dillon  *	the ordering.
3571c7c3c6aSMatthew Dillon  */
3581c7c3c6aSMatthew Dillon 
359aef922f5SJohn Dyson int
3608f9110f6SJohn Dyson vm_pageout_flush(mc, count, flags)
361aef922f5SJohn Dyson 	vm_page_t *mc;
362aef922f5SJohn Dyson 	int count;
3638f9110f6SJohn Dyson 	int flags;
364aef922f5SJohn Dyson {
365aef922f5SJohn Dyson 	register vm_object_t object;
366aef922f5SJohn Dyson 	int pageout_status[count];
36795461b45SJohn Dyson 	int numpagedout = 0;
368aef922f5SJohn Dyson 	int i;
369aef922f5SJohn Dyson 
3700cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
3711c7c3c6aSMatthew Dillon 	/*
3721c7c3c6aSMatthew Dillon 	 * Initiate I/O.  Bump the vm_page_t->busy counter and
3731c7c3c6aSMatthew Dillon 	 * mark the pages read-only.
3741c7c3c6aSMatthew Dillon 	 *
3751c7c3c6aSMatthew Dillon 	 * We do not have to fixup the clean/dirty bits here... we can
3761c7c3c6aSMatthew Dillon 	 * allow the pager to do it after the I/O completes.
37702fa91d3SMatthew Dillon 	 *
37802fa91d3SMatthew Dillon 	 * NOTE! mc[i]->dirty may be partial or fragmented due to an
37902fa91d3SMatthew Dillon 	 * edge case with file fragments.
3801c7c3c6aSMatthew Dillon 	 */
3811c7c3c6aSMatthew Dillon 
3828f9110f6SJohn Dyson 	for (i = 0; i < count; i++) {
38302fa91d3SMatthew Dillon 		KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL, ("vm_pageout_flush page %p index %d/%d: partially invalid page", mc[i], i, count));
384e69763a3SDoug Rabson 		vm_page_io_start(mc[i]);
3858f9110f6SJohn Dyson 		vm_page_protect(mc[i], VM_PROT_READ);
3868f9110f6SJohn Dyson 	}
3878f9110f6SJohn Dyson 
388aef922f5SJohn Dyson 	object = mc[0]->object;
389d474eaaaSDoug Rabson 	vm_object_pip_add(object, count);
390aef922f5SJohn Dyson 
391aef922f5SJohn Dyson 	vm_pager_put_pages(object, mc, count,
3928f9110f6SJohn Dyson 	    (flags | ((object == kernel_object) ? OBJPC_SYNC : 0)),
39326f9a767SRodney W. Grimes 	    pageout_status);
39426f9a767SRodney W. Grimes 
395aef922f5SJohn Dyson 	for (i = 0; i < count; i++) {
396aef922f5SJohn Dyson 		vm_page_t mt = mc[i];
39724a1cce3SDavid Greenman 
39826f9a767SRodney W. Grimes 		switch (pageout_status[i]) {
39926f9a767SRodney W. Grimes 		case VM_PAGER_OK:
40095461b45SJohn Dyson 			numpagedout++;
40126f9a767SRodney W. Grimes 			break;
40226f9a767SRodney W. Grimes 		case VM_PAGER_PEND:
40395461b45SJohn Dyson 			numpagedout++;
40426f9a767SRodney W. Grimes 			break;
40526f9a767SRodney W. Grimes 		case VM_PAGER_BAD:
40626f9a767SRodney W. Grimes 			/*
4070d94caffSDavid Greenman 			 * Page outside of range of object. Right now we
4080d94caffSDavid Greenman 			 * essentially lose the changes by pretending it
4090d94caffSDavid Greenman 			 * worked.
41026f9a767SRodney W. Grimes 			 */
4110385347cSPeter Wemm 			pmap_clear_modify(mt);
41290ecac61SMatthew Dillon 			vm_page_undirty(mt);
41326f9a767SRodney W. Grimes 			break;
41426f9a767SRodney W. Grimes 		case VM_PAGER_ERROR:
41526f9a767SRodney W. Grimes 		case VM_PAGER_FAIL:
41626f9a767SRodney W. Grimes 			/*
4170d94caffSDavid Greenman 			 * If page couldn't be paged out, then reactivate the
4180d94caffSDavid Greenman 			 * page so it doesn't clog the inactive list.  (We
4190d94caffSDavid Greenman 			 * will try paging out it again later).
42026f9a767SRodney W. Grimes 			 */
42124a1cce3SDavid Greenman 			vm_page_activate(mt);
42226f9a767SRodney W. Grimes 			break;
42326f9a767SRodney W. Grimes 		case VM_PAGER_AGAIN:
42426f9a767SRodney W. Grimes 			break;
42526f9a767SRodney W. Grimes 		}
42626f9a767SRodney W. Grimes 
42726f9a767SRodney W. Grimes 		/*
4280d94caffSDavid Greenman 		 * If the operation is still going, leave the page busy to
4290d94caffSDavid Greenman 		 * block all other accesses. Also, leave the paging in
4300d94caffSDavid Greenman 		 * progress indicator set so that we don't attempt an object
4310d94caffSDavid Greenman 		 * collapse.
43226f9a767SRodney W. Grimes 		 */
43326f9a767SRodney W. Grimes 		if (pageout_status[i] != VM_PAGER_PEND) {
434f919ebdeSDavid Greenman 			vm_object_pip_wakeup(object);
435e69763a3SDoug Rabson 			vm_page_io_finish(mt);
436936524aaSMatthew Dillon 			if (!vm_page_count_severe() || !vm_page_try_to_cache(mt))
437936524aaSMatthew Dillon 				vm_page_protect(mt, VM_PROT_READ);
43826f9a767SRodney W. Grimes 		}
43926f9a767SRodney W. Grimes 	}
44095461b45SJohn Dyson 	return numpagedout;
44126f9a767SRodney W. Grimes }
44226f9a767SRodney W. Grimes 
44338efa82bSJohn Dyson #if !defined(NO_SWAPPING)
44426f9a767SRodney W. Grimes /*
44526f9a767SRodney W. Grimes  *	vm_pageout_object_deactivate_pages
44626f9a767SRodney W. Grimes  *
44726f9a767SRodney W. Grimes  *	deactivate enough pages to satisfy the inactive target
44826f9a767SRodney W. Grimes  *	requirements or if vm_page_proc_limit is set, then
44926f9a767SRodney W. Grimes  *	deactivate all of the pages in the object and its
45024a1cce3SDavid Greenman  *	backing_objects.
45126f9a767SRodney W. Grimes  *
45226f9a767SRodney W. Grimes  *	The object and map must be locked.
45326f9a767SRodney W. Grimes  */
45438efa82bSJohn Dyson static void
45538efa82bSJohn Dyson vm_pageout_object_deactivate_pages(map, object, desired, map_remove_only)
45626f9a767SRodney W. Grimes 	vm_map_t map;
45726f9a767SRodney W. Grimes 	vm_object_t object;
45838efa82bSJohn Dyson 	vm_pindex_t desired;
4590d94caffSDavid Greenman 	int map_remove_only;
46026f9a767SRodney W. Grimes {
46126f9a767SRodney W. Grimes 	register vm_page_t p, next;
46226f9a767SRodney W. Grimes 	int rcount;
46338efa82bSJohn Dyson 	int remove_mode;
4641eeaa1e3SJohn Dyson 	int s;
46526f9a767SRodney W. Grimes 
4660cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
46724964514SPeter Wemm 	if (object->type == OBJT_DEVICE || object->type == OBJT_PHYS)
46838efa82bSJohn Dyson 		return;
4698f895206SDavid Greenman 
47038efa82bSJohn Dyson 	while (object) {
471b1028ad1SLuoqi Chen 		if (pmap_resident_count(vm_map_pmap(map)) <= desired)
47238efa82bSJohn Dyson 			return;
47324a1cce3SDavid Greenman 		if (object->paging_in_progress)
47438efa82bSJohn Dyson 			return;
47526f9a767SRodney W. Grimes 
47638efa82bSJohn Dyson 		remove_mode = map_remove_only;
47738efa82bSJohn Dyson 		if (object->shadow_count > 1)
47838efa82bSJohn Dyson 			remove_mode = 1;
47926f9a767SRodney W. Grimes 	/*
48026f9a767SRodney W. Grimes 	 * scan the objects entire memory queue
48126f9a767SRodney W. Grimes 	 */
48226f9a767SRodney W. Grimes 		rcount = object->resident_page_count;
483b18bfc3dSJohn Dyson 		p = TAILQ_FIRST(&object->memq);
48426f9a767SRodney W. Grimes 		while (p && (rcount-- > 0)) {
4857e006499SJohn Dyson 			int actcount;
486b1028ad1SLuoqi Chen 			if (pmap_resident_count(vm_map_pmap(map)) <= desired)
48738efa82bSJohn Dyson 				return;
488b18bfc3dSJohn Dyson 			next = TAILQ_NEXT(p, listq);
489a58d1fa1SDavid Greenman 			cnt.v_pdpages++;
4900d94caffSDavid Greenman 			if (p->wire_count != 0 ||
4910d94caffSDavid Greenman 			    p->hold_count != 0 ||
4920d94caffSDavid Greenman 			    p->busy != 0 ||
4938b03c8edSMatthew Dillon 			    (p->flags & (PG_BUSY|PG_UNMANAGED)) ||
4940385347cSPeter Wemm 			    !pmap_page_exists(vm_map_pmap(map), p)) {
4950d94caffSDavid Greenman 				p = next;
4960d94caffSDavid Greenman 				continue;
4970d94caffSDavid Greenman 			}
498ef743ce6SJohn Dyson 
4990385347cSPeter Wemm 			actcount = pmap_ts_referenced(p);
5007e006499SJohn Dyson 			if (actcount) {
501e69763a3SDoug Rabson 				vm_page_flag_set(p, PG_REFERENCED);
502c8c4b40cSJohn Dyson 			} else if (p->flags & PG_REFERENCED) {
5037e006499SJohn Dyson 				actcount = 1;
504ef743ce6SJohn Dyson 			}
505ef743ce6SJohn Dyson 
50638efa82bSJohn Dyson 			if ((p->queue != PQ_ACTIVE) &&
50738efa82bSJohn Dyson 				(p->flags & PG_REFERENCED)) {
508ef743ce6SJohn Dyson 				vm_page_activate(p);
5097e006499SJohn Dyson 				p->act_count += actcount;
510e69763a3SDoug Rabson 				vm_page_flag_clear(p, PG_REFERENCED);
511c8c4b40cSJohn Dyson 			} else if (p->queue == PQ_ACTIVE) {
512ef743ce6SJohn Dyson 				if ((p->flags & PG_REFERENCED) == 0) {
513c8c4b40cSJohn Dyson 					p->act_count -= min(p->act_count, ACT_DECLINE);
5142b6b0df7SMatthew Dillon 					if (!remove_mode && (vm_pageout_algorithm || (p->act_count == 0))) {
515b18bfc3dSJohn Dyson 						vm_page_protect(p, VM_PROT_NONE);
51626f9a767SRodney W. Grimes 						vm_page_deactivate(p);
51726f9a767SRodney W. Grimes 					} else {
518c8c4b40cSJohn Dyson 						s = splvm();
519be72f788SAlan Cox 						TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
520be72f788SAlan Cox 						TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
521c8c4b40cSJohn Dyson 						splx(s);
522c8c4b40cSJohn Dyson 					}
523c8c4b40cSJohn Dyson 				} else {
524eaf13dd7SJohn Dyson 					vm_page_activate(p);
525e69763a3SDoug Rabson 					vm_page_flag_clear(p, PG_REFERENCED);
52638efa82bSJohn Dyson 					if (p->act_count < (ACT_MAX - ACT_ADVANCE))
52738efa82bSJohn Dyson 						p->act_count += ACT_ADVANCE;
5281eeaa1e3SJohn Dyson 					s = splvm();
529be72f788SAlan Cox 					TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
530be72f788SAlan Cox 					TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
5311eeaa1e3SJohn Dyson 					splx(s);
53226f9a767SRodney W. Grimes 				}
533bd7e5f99SJohn Dyson 			} else if (p->queue == PQ_INACTIVE) {
534f919ebdeSDavid Greenman 				vm_page_protect(p, VM_PROT_NONE);
53526f9a767SRodney W. Grimes 			}
53626f9a767SRodney W. Grimes 			p = next;
53726f9a767SRodney W. Grimes 		}
53838efa82bSJohn Dyson 		object = object->backing_object;
53938efa82bSJohn Dyson 	}
54038efa82bSJohn Dyson 	return;
54126f9a767SRodney W. Grimes }
54226f9a767SRodney W. Grimes 
54326f9a767SRodney W. Grimes /*
54426f9a767SRodney W. Grimes  * deactivate some number of pages in a map, try to do it fairly, but
54526f9a767SRodney W. Grimes  * that is really hard to do.
54626f9a767SRodney W. Grimes  */
547cd41fc12SDavid Greenman static void
54838efa82bSJohn Dyson vm_pageout_map_deactivate_pages(map, desired)
54926f9a767SRodney W. Grimes 	vm_map_t map;
55038efa82bSJohn Dyson 	vm_pindex_t desired;
55126f9a767SRodney W. Grimes {
55226f9a767SRodney W. Grimes 	vm_map_entry_t tmpe;
55338efa82bSJohn Dyson 	vm_object_t obj, bigobj;
5540d94caffSDavid Greenman 
5550cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
556996c772fSJohn Dyson 	if (lockmgr(&map->lock, LK_EXCLUSIVE | LK_NOWAIT, (void *)0, curproc)) {
55726f9a767SRodney W. Grimes 		return;
55826f9a767SRodney W. Grimes 	}
55938efa82bSJohn Dyson 
56038efa82bSJohn Dyson 	bigobj = NULL;
56138efa82bSJohn Dyson 
56238efa82bSJohn Dyson 	/*
56338efa82bSJohn Dyson 	 * first, search out the biggest object, and try to free pages from
56438efa82bSJohn Dyson 	 * that.
56538efa82bSJohn Dyson 	 */
56626f9a767SRodney W. Grimes 	tmpe = map->header.next;
56738efa82bSJohn Dyson 	while (tmpe != &map->header) {
5689fdfe602SMatthew Dillon 		if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
56938efa82bSJohn Dyson 			obj = tmpe->object.vm_object;
57038efa82bSJohn Dyson 			if ((obj != NULL) && (obj->shadow_count <= 1) &&
57138efa82bSJohn Dyson 				((bigobj == NULL) ||
57238efa82bSJohn Dyson 				 (bigobj->resident_page_count < obj->resident_page_count))) {
57338efa82bSJohn Dyson 				bigobj = obj;
57438efa82bSJohn Dyson 			}
57538efa82bSJohn Dyson 		}
57638efa82bSJohn Dyson 		tmpe = tmpe->next;
57738efa82bSJohn Dyson 	}
57838efa82bSJohn Dyson 
57938efa82bSJohn Dyson 	if (bigobj)
58038efa82bSJohn Dyson 		vm_pageout_object_deactivate_pages(map, bigobj, desired, 0);
58138efa82bSJohn Dyson 
58238efa82bSJohn Dyson 	/*
58338efa82bSJohn Dyson 	 * Next, hunt around for other pages to deactivate.  We actually
58438efa82bSJohn Dyson 	 * do this search sort of wrong -- .text first is not the best idea.
58538efa82bSJohn Dyson 	 */
58638efa82bSJohn Dyson 	tmpe = map->header.next;
58738efa82bSJohn Dyson 	while (tmpe != &map->header) {
588b1028ad1SLuoqi Chen 		if (pmap_resident_count(vm_map_pmap(map)) <= desired)
58938efa82bSJohn Dyson 			break;
5909fdfe602SMatthew Dillon 		if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
59138efa82bSJohn Dyson 			obj = tmpe->object.vm_object;
59201155bd7SDavid Greenman 			if (obj)
59338efa82bSJohn Dyson 				vm_pageout_object_deactivate_pages(map, obj, desired, 0);
59438efa82bSJohn Dyson 		}
59526f9a767SRodney W. Grimes 		tmpe = tmpe->next;
59626f9a767SRodney W. Grimes 	};
59738efa82bSJohn Dyson 
59838efa82bSJohn Dyson 	/*
59938efa82bSJohn Dyson 	 * Remove all mappings if a process is swapped out, this will free page
60038efa82bSJohn Dyson 	 * table pages.
60138efa82bSJohn Dyson 	 */
60238efa82bSJohn Dyson 	if (desired == 0)
60338efa82bSJohn Dyson 		pmap_remove(vm_map_pmap(map),
60438efa82bSJohn Dyson 			VM_MIN_ADDRESS, VM_MAXUSER_ADDRESS);
60538efa82bSJohn Dyson 	vm_map_unlock(map);
60626f9a767SRodney W. Grimes 	return;
60726f9a767SRodney W. Grimes }
60838efa82bSJohn Dyson #endif
609df8bae1dSRodney W. Grimes 
6101c7c3c6aSMatthew Dillon /*
6111c7c3c6aSMatthew Dillon  * Don't try to be fancy - being fancy can lead to VOP_LOCK's and therefore
6121c7c3c6aSMatthew Dillon  * to vnode deadlocks.  We only do it for OBJT_DEFAULT and OBJT_SWAP objects
6131c7c3c6aSMatthew Dillon  * which we know can be trivially freed.
6141c7c3c6aSMatthew Dillon  */
6151c7c3c6aSMatthew Dillon 
616925a3a41SJohn Dyson void
617925a3a41SJohn Dyson vm_pageout_page_free(vm_page_t m) {
6181c7c3c6aSMatthew Dillon 	vm_object_t object = m->object;
6191c7c3c6aSMatthew Dillon 	int type = object->type;
620925a3a41SJohn Dyson 
6210cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
6221c7c3c6aSMatthew Dillon 	if (type == OBJT_SWAP || type == OBJT_DEFAULT)
6231c7c3c6aSMatthew Dillon 		vm_object_reference(object);
624e69763a3SDoug Rabson 	vm_page_busy(m);
625925a3a41SJohn Dyson 	vm_page_protect(m, VM_PROT_NONE);
626925a3a41SJohn Dyson 	vm_page_free(m);
6271c7c3c6aSMatthew Dillon 	if (type == OBJT_SWAP || type == OBJT_DEFAULT)
62847221757SJohn Dyson 		vm_object_deallocate(object);
629925a3a41SJohn Dyson }
630925a3a41SJohn Dyson 
631df8bae1dSRodney W. Grimes /*
632df8bae1dSRodney W. Grimes  *	vm_pageout_scan does the dirty work for the pageout daemon.
633df8bae1dSRodney W. Grimes  */
6342b6b0df7SMatthew Dillon static void
6352b6b0df7SMatthew Dillon vm_pageout_scan(int pass)
636df8bae1dSRodney W. Grimes {
637502ba6e4SJohn Dyson 	vm_page_t m, next;
638936524aaSMatthew Dillon 	struct vm_page marker;
6392b6b0df7SMatthew Dillon 	int save_page_shortage;
6402b6b0df7SMatthew Dillon 	int save_inactive_count;
6411c7c3c6aSMatthew Dillon 	int page_shortage, maxscan, pcount;
6421c7c3c6aSMatthew Dillon 	int addl_page_shortage, addl_page_shortage_init;
6435663e6deSDavid Greenman 	struct proc *p, *bigproc;
6445663e6deSDavid Greenman 	vm_offset_t size, bigsize;
645df8bae1dSRodney W. Grimes 	vm_object_t object;
6467e006499SJohn Dyson 	int actcount;
647f6b04d2bSDavid Greenman 	int vnodes_skipped = 0;
6482b6b0df7SMatthew Dillon 	int maxlaunder;
6491eeaa1e3SJohn Dyson 	int s;
6500d94caffSDavid Greenman 
6510cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
652df8bae1dSRodney W. Grimes 	/*
6535985940eSJohn Dyson 	 * Do whatever cleanup that the pmap code can.
6545985940eSJohn Dyson 	 */
6555985940eSJohn Dyson 	pmap_collect();
6565985940eSJohn Dyson 
6571c7c3c6aSMatthew Dillon 	addl_page_shortage_init = vm_pageout_deficit;
65895461b45SJohn Dyson 	vm_pageout_deficit = 0;
659b182ec9eSJohn Dyson 
6601c7c3c6aSMatthew Dillon 	/*
6611c7c3c6aSMatthew Dillon 	 * Calculate the number of pages we want to either free or move
6622b6b0df7SMatthew Dillon 	 * to the cache.
6631c7c3c6aSMatthew Dillon 	 */
6642b6b0df7SMatthew Dillon 	page_shortage = vm_paging_target() + addl_page_shortage_init;
6652b6b0df7SMatthew Dillon 	save_page_shortage = page_shortage;
6662b6b0df7SMatthew Dillon 	save_inactive_count = cnt.v_inactive_count;
6671c7c3c6aSMatthew Dillon 
6681c7c3c6aSMatthew Dillon 	/*
669936524aaSMatthew Dillon 	 * Initialize our marker
670936524aaSMatthew Dillon 	 */
671936524aaSMatthew Dillon 	bzero(&marker, sizeof(marker));
672936524aaSMatthew Dillon 	marker.flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER;
673936524aaSMatthew Dillon 	marker.queue = PQ_INACTIVE;
674936524aaSMatthew Dillon 	marker.wire_count = 1;
675936524aaSMatthew Dillon 
676936524aaSMatthew Dillon 	/*
6771c7c3c6aSMatthew Dillon 	 * Start scanning the inactive queue for pages we can move to the
6781c7c3c6aSMatthew Dillon 	 * cache or free.  The scan will stop when the target is reached or
679936524aaSMatthew Dillon 	 * we have scanned the entire inactive queue.  Note that m->act_count
680936524aaSMatthew Dillon 	 * is not used to form decisions for the inactive queue, only for the
681936524aaSMatthew Dillon 	 * active queue.
6822b6b0df7SMatthew Dillon 	 *
6832b6b0df7SMatthew Dillon 	 * maxlaunder limits the number of dirty pages we flush per scan.
6842b6b0df7SMatthew Dillon 	 * For most systems a smaller value (16 or 32) is more robust under
6852b6b0df7SMatthew Dillon 	 * extreme memory and disk pressure because any unnecessary writes
6862b6b0df7SMatthew Dillon 	 * to disk can result in extreme performance degredation.  However,
6872b6b0df7SMatthew Dillon 	 * systems with excessive dirty pages (especially when MAP_NOSYNC is
6882b6b0df7SMatthew Dillon 	 * used) will die horribly with limited laundering.  If the pageout
6892b6b0df7SMatthew Dillon 	 * daemon cannot clean enough pages in the first pass, we let it go
6902b6b0df7SMatthew Dillon 	 * all out in succeeding passes.
6911c7c3c6aSMatthew Dillon 	 */
69270111b90SJohn Dyson 
6932b6b0df7SMatthew Dillon 	if ((maxlaunder = vm_max_launder) <= 1)
6942b6b0df7SMatthew Dillon 		maxlaunder = 1;
6952b6b0df7SMatthew Dillon 	if (pass)
6962b6b0df7SMatthew Dillon 		maxlaunder = 10000;
6972b6b0df7SMatthew Dillon 
69867bf6868SJohn Dyson rescan0:
6991c7c3c6aSMatthew Dillon 	addl_page_shortage = addl_page_shortage_init;
700f6b04d2bSDavid Greenman 	maxscan = cnt.v_inactive_count;
701be72f788SAlan Cox 	for (m = TAILQ_FIRST(&vm_page_queues[PQ_INACTIVE].pl);
7021c7c3c6aSMatthew Dillon 	     m != NULL && maxscan-- > 0 && page_shortage > 0;
703e929c00dSKirk McKusick 	     m = next) {
704df8bae1dSRodney W. Grimes 
705a58d1fa1SDavid Greenman 		cnt.v_pdpages++;
706b182ec9eSJohn Dyson 
707f35329acSJohn Dyson 		if (m->queue != PQ_INACTIVE) {
70867bf6868SJohn Dyson 			goto rescan0;
709f35329acSJohn Dyson 		}
710b182ec9eSJohn Dyson 
711b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(m, pageq);
712df8bae1dSRodney W. Grimes 
713936524aaSMatthew Dillon 		/*
714936524aaSMatthew Dillon 		 * skip marker pages
715936524aaSMatthew Dillon 		 */
716936524aaSMatthew Dillon 		if (m->flags & PG_MARKER)
717936524aaSMatthew Dillon 			continue;
718936524aaSMatthew Dillon 
719b182ec9eSJohn Dyson 		if (m->hold_count) {
720f35329acSJohn Dyson 			s = splvm();
721be72f788SAlan Cox 			TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
722be72f788SAlan Cox 			TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
723f35329acSJohn Dyson 			splx(s);
724b182ec9eSJohn Dyson 			addl_page_shortage++;
725b182ec9eSJohn Dyson 			continue;
726df8bae1dSRodney W. Grimes 		}
72726f9a767SRodney W. Grimes 		/*
728b18bfc3dSJohn Dyson 		 * Dont mess with busy pages, keep in the front of the
729b18bfc3dSJohn Dyson 		 * queue, most likely are being paged out.
73026f9a767SRodney W. Grimes 		 */
731bd7e5f99SJohn Dyson 		if (m->busy || (m->flags & PG_BUSY)) {
732b182ec9eSJohn Dyson 			addl_page_shortage++;
73326f9a767SRodney W. Grimes 			continue;
73426f9a767SRodney W. Grimes 		}
735bd7e5f99SJohn Dyson 
7367e006499SJohn Dyson 		/*
7371c7c3c6aSMatthew Dillon 		 * If the object is not being used, we ignore previous
7381c7c3c6aSMatthew Dillon 		 * references.
7397e006499SJohn Dyson 		 */
7400d94caffSDavid Greenman 		if (m->object->ref_count == 0) {
741e69763a3SDoug Rabson 			vm_page_flag_clear(m, PG_REFERENCED);
7420385347cSPeter Wemm 			pmap_clear_reference(m);
7437e006499SJohn Dyson 
7447e006499SJohn Dyson 		/*
7451c7c3c6aSMatthew Dillon 		 * Otherwise, if the page has been referenced while in the
7461c7c3c6aSMatthew Dillon 		 * inactive queue, we bump the "activation count" upwards,
7471c7c3c6aSMatthew Dillon 		 * making it less likely that the page will be added back to
7481c7c3c6aSMatthew Dillon 		 * the inactive queue prematurely again.  Here we check the
7491c7c3c6aSMatthew Dillon 		 * page tables (or emulated bits, if any), given the upper
7501c7c3c6aSMatthew Dillon 		 * level VM system not knowing anything about existing
7511c7c3c6aSMatthew Dillon 		 * references.
7527e006499SJohn Dyson 		 */
753ef743ce6SJohn Dyson 		} else if (((m->flags & PG_REFERENCED) == 0) &&
7540385347cSPeter Wemm 			(actcount = pmap_ts_referenced(m))) {
755ef743ce6SJohn Dyson 			vm_page_activate(m);
7567e006499SJohn Dyson 			m->act_count += (actcount + ACT_ADVANCE);
757ef743ce6SJohn Dyson 			continue;
7582fe6e4d7SDavid Greenman 		}
759ef743ce6SJohn Dyson 
7607e006499SJohn Dyson 		/*
7611c7c3c6aSMatthew Dillon 		 * If the upper level VM system knows about any page
7621c7c3c6aSMatthew Dillon 		 * references, we activate the page.  We also set the
7631c7c3c6aSMatthew Dillon 		 * "activation count" higher than normal so that we will less
7641c7c3c6aSMatthew Dillon 		 * likely place pages back onto the inactive queue again.
7657e006499SJohn Dyson 		 */
766bd7e5f99SJohn Dyson 		if ((m->flags & PG_REFERENCED) != 0) {
767e69763a3SDoug Rabson 			vm_page_flag_clear(m, PG_REFERENCED);
7680385347cSPeter Wemm 			actcount = pmap_ts_referenced(m);
76926f9a767SRodney W. Grimes 			vm_page_activate(m);
7707e006499SJohn Dyson 			m->act_count += (actcount + ACT_ADVANCE + 1);
7710d94caffSDavid Greenman 			continue;
7720d94caffSDavid Greenman 		}
77367bf6868SJohn Dyson 
7747e006499SJohn Dyson 		/*
7751c7c3c6aSMatthew Dillon 		 * If the upper level VM system doesn't know anything about
7761c7c3c6aSMatthew Dillon 		 * the page being dirty, we have to check for it again.  As
7771c7c3c6aSMatthew Dillon 		 * far as the VM code knows, any partially dirty pages are
7781c7c3c6aSMatthew Dillon 		 * fully dirty.
7797e006499SJohn Dyson 		 */
780f6b04d2bSDavid Greenman 		if (m->dirty == 0) {
781bd7e5f99SJohn Dyson 			vm_page_test_dirty(m);
782427e99a0SAlexander Langer 		} else {
7837dbf82dcSMatthew Dillon 			vm_page_dirty(m);
78430dcfc09SJohn Dyson 		}
785ef743ce6SJohn Dyson 
7867e006499SJohn Dyson 		/*
7877e006499SJohn Dyson 		 * Invalid pages can be easily freed
7887e006499SJohn Dyson 		 */
7896d40c3d3SDavid Greenman 		if (m->valid == 0) {
790925a3a41SJohn Dyson 			vm_pageout_page_free(m);
79167bf6868SJohn Dyson 			cnt.v_dfree++;
7921c7c3c6aSMatthew Dillon 			--page_shortage;
7937e006499SJohn Dyson 
7947e006499SJohn Dyson 		/*
795936524aaSMatthew Dillon 		 * Clean pages can be placed onto the cache queue.  This
796936524aaSMatthew Dillon 		 * effectively frees them.
7977e006499SJohn Dyson 		 */
798bd7e5f99SJohn Dyson 		} else if (m->dirty == 0) {
799bd7e5f99SJohn Dyson 			vm_page_cache(m);
8001c7c3c6aSMatthew Dillon 			--page_shortage;
8012b6b0df7SMatthew Dillon 		} else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) {
8027e006499SJohn Dyson 			/*
8032b6b0df7SMatthew Dillon 			 * Dirty pages need to be paged out, but flushing
8042b6b0df7SMatthew Dillon 			 * a page is extremely expensive verses freeing
8052b6b0df7SMatthew Dillon 			 * a clean page.  Rather then artificially limiting
8062b6b0df7SMatthew Dillon 			 * the number of pages we can flush, we instead give
8072b6b0df7SMatthew Dillon 			 * dirty pages extra priority on the inactive queue
8082b6b0df7SMatthew Dillon 			 * by forcing them to be cycled through the queue
8092b6b0df7SMatthew Dillon 			 * twice before being flushed, after which the
8102b6b0df7SMatthew Dillon 			 * (now clean) page will cycle through once more
8112b6b0df7SMatthew Dillon 			 * before being freed.  This significantly extends
8122b6b0df7SMatthew Dillon 			 * the thrash point for a heavily loaded machine.
8137e006499SJohn Dyson 			 */
8142b6b0df7SMatthew Dillon 			s = splvm();
8152b6b0df7SMatthew Dillon 			vm_page_flag_set(m, PG_WINATCFLS);
8162b6b0df7SMatthew Dillon 			TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
8172b6b0df7SMatthew Dillon 			TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
8182b6b0df7SMatthew Dillon 			splx(s);
8190d94caffSDavid Greenman 		} else if (maxlaunder > 0) {
8202b6b0df7SMatthew Dillon 			/*
8212b6b0df7SMatthew Dillon 			 * We always want to try to flush some dirty pages if
8222b6b0df7SMatthew Dillon 			 * we encounter them, to keep the system stable.
8232b6b0df7SMatthew Dillon 			 * Normally this number is small, but under extreme
8242b6b0df7SMatthew Dillon 			 * pressure where there are insufficient clean pages
8252b6b0df7SMatthew Dillon 			 * on the inactive queue, we may have to go all out.
8262b6b0df7SMatthew Dillon 			 */
82712ac6a1dSJohn Dyson 			int swap_pageouts_ok;
828f6b04d2bSDavid Greenman 			struct vnode *vp = NULL;
829f2a2857bSKirk McKusick 			struct mount *mp;
8300d94caffSDavid Greenman 
8310d94caffSDavid Greenman 			object = m->object;
8327e006499SJohn Dyson 
83312ac6a1dSJohn Dyson 			if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) {
83412ac6a1dSJohn Dyson 				swap_pageouts_ok = 1;
83512ac6a1dSJohn Dyson 			} else {
83612ac6a1dSJohn Dyson 				swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts);
83712ac6a1dSJohn Dyson 				swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts &&
83890ecac61SMatthew Dillon 				vm_page_count_min());
83912ac6a1dSJohn Dyson 
84012ac6a1dSJohn Dyson 			}
84170111b90SJohn Dyson 
84270111b90SJohn Dyson 			/*
8431c7c3c6aSMatthew Dillon 			 * We don't bother paging objects that are "dead".
8441c7c3c6aSMatthew Dillon 			 * Those objects are in a "rundown" state.
84570111b90SJohn Dyson 			 */
84670111b90SJohn Dyson 			if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) {
84712ac6a1dSJohn Dyson 				s = splvm();
848be72f788SAlan Cox 				TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
849be72f788SAlan Cox 				TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
85012ac6a1dSJohn Dyson 				splx(s);
85112ac6a1dSJohn Dyson 				continue;
85212ac6a1dSJohn Dyson 			}
85312ac6a1dSJohn Dyson 
8541c7c3c6aSMatthew Dillon 			/*
8552b6b0df7SMatthew Dillon 			 * The object is already known NOT to be dead.   It
8562b6b0df7SMatthew Dillon 			 * is possible for the vget() to block the whole
8572b6b0df7SMatthew Dillon 			 * pageout daemon, but the new low-memory handling
8582b6b0df7SMatthew Dillon 			 * code should prevent it.
8591c7c3c6aSMatthew Dillon 			 *
8602b6b0df7SMatthew Dillon 			 * The previous code skipped locked vnodes and, worse,
8612b6b0df7SMatthew Dillon 			 * reordered pages in the queue.  This results in
8622b6b0df7SMatthew Dillon 			 * completely non-deterministic operation and, on a
8632b6b0df7SMatthew Dillon 			 * busy system, can lead to extremely non-optimal
8642b6b0df7SMatthew Dillon 			 * pageouts.  For example, it can cause clean pages
8652b6b0df7SMatthew Dillon 			 * to be freed and dirty pages to be moved to the end
8662b6b0df7SMatthew Dillon 			 * of the queue.  Since dirty pages are also moved to
8672b6b0df7SMatthew Dillon 			 * the end of the queue once-cleaned, this gives
8682b6b0df7SMatthew Dillon 			 * way too large a weighting to defering the freeing
8692b6b0df7SMatthew Dillon 			 * of dirty pages.
8701c7c3c6aSMatthew Dillon 			 *
8712b6b0df7SMatthew Dillon 			 * XXX we need to be able to apply a timeout to the
8722b6b0df7SMatthew Dillon 			 * vget() lock attempt.
8731c7c3c6aSMatthew Dillon 			 */
8741c7c3c6aSMatthew Dillon 
8751c7c3c6aSMatthew Dillon 			if (object->type == OBJT_VNODE) {
87624a1cce3SDavid Greenman 				vp = object->handle;
8771c7c3c6aSMatthew Dillon 
878f2a2857bSKirk McKusick 				mp = NULL;
879f2a2857bSKirk McKusick 				if (vp->v_type == VREG)
880f2a2857bSKirk McKusick 					vn_start_write(vp, &mp, V_NOWAIT);
8812b6b0df7SMatthew Dillon 				if (vget(vp, LK_EXCLUSIVE|LK_NOOBJ, curproc)) {
882f2a2857bSKirk McKusick 					vn_finished_write(mp);
883aef922f5SJohn Dyson 					if (object->flags & OBJ_MIGHTBEDIRTY)
884925a3a41SJohn Dyson 						vnodes_skipped++;
885b182ec9eSJohn Dyson 					continue;
88685a376ebSJohn Dyson 				}
887b182ec9eSJohn Dyson 
888f35329acSJohn Dyson 				/*
889936524aaSMatthew Dillon 				 * The page might have been moved to another
890936524aaSMatthew Dillon 				 * queue during potential blocking in vget()
891936524aaSMatthew Dillon 				 * above.  The page might have been freed and
892936524aaSMatthew Dillon 				 * reused for another vnode.  The object might
893936524aaSMatthew Dillon 				 * have been reused for another vnode.
894f35329acSJohn Dyson 				 */
895936524aaSMatthew Dillon 				if (m->queue != PQ_INACTIVE ||
896936524aaSMatthew Dillon 				    m->object != object ||
897936524aaSMatthew Dillon 				    object->handle != vp) {
898b182ec9eSJohn Dyson 					if (object->flags & OBJ_MIGHTBEDIRTY)
899925a3a41SJohn Dyson 						vnodes_skipped++;
900b182ec9eSJohn Dyson 					vput(vp);
901f2a2857bSKirk McKusick 					vn_finished_write(mp);
902b182ec9eSJohn Dyson 					continue;
903b182ec9eSJohn Dyson 				}
904b182ec9eSJohn Dyson 
905f35329acSJohn Dyson 				/*
906936524aaSMatthew Dillon 				 * The page may have been busied during the
907936524aaSMatthew Dillon 				 * blocking in vput();  We don't move the
908936524aaSMatthew Dillon 				 * page back onto the end of the queue so that
909936524aaSMatthew Dillon 				 * statistics are more correct if we don't.
910f35329acSJohn Dyson 				 */
911b182ec9eSJohn Dyson 				if (m->busy || (m->flags & PG_BUSY)) {
912b182ec9eSJohn Dyson 					vput(vp);
913f2a2857bSKirk McKusick 					vn_finished_write(mp);
914b182ec9eSJohn Dyson 					continue;
915b182ec9eSJohn Dyson 				}
916b182ec9eSJohn Dyson 
917f35329acSJohn Dyson 				/*
918f35329acSJohn Dyson 				 * If the page has become held, then skip it
919f35329acSJohn Dyson 				 */
920b182ec9eSJohn Dyson 				if (m->hold_count) {
921f35329acSJohn Dyson 					s = splvm();
922be72f788SAlan Cox 					TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
923be72f788SAlan Cox 					TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
924f35329acSJohn Dyson 					splx(s);
925b182ec9eSJohn Dyson 					if (object->flags & OBJ_MIGHTBEDIRTY)
926925a3a41SJohn Dyson 						vnodes_skipped++;
927b182ec9eSJohn Dyson 					vput(vp);
928f2a2857bSKirk McKusick 					vn_finished_write(mp);
929f6b04d2bSDavid Greenman 					continue;
930f6b04d2bSDavid Greenman 				}
931f6b04d2bSDavid Greenman 			}
932f6b04d2bSDavid Greenman 
9330d94caffSDavid Greenman 			/*
9340d94caffSDavid Greenman 			 * If a page is dirty, then it is either being washed
9350d94caffSDavid Greenman 			 * (but not yet cleaned) or it is still in the
9360d94caffSDavid Greenman 			 * laundry.  If it is still in the laundry, then we
9372b6b0df7SMatthew Dillon 			 * start the cleaning operation.
938936524aaSMatthew Dillon 			 *
939936524aaSMatthew Dillon 			 * This operation may cluster, invalidating the 'next'
940936524aaSMatthew Dillon 			 * pointer.  To prevent an inordinate number of
941936524aaSMatthew Dillon 			 * restarts we use our marker to remember our place.
9422b6b0df7SMatthew Dillon 			 *
9432b6b0df7SMatthew Dillon 			 * decrement page_shortage on success to account for
9442b6b0df7SMatthew Dillon 			 * the (future) cleaned page.  Otherwise we could wind
9452b6b0df7SMatthew Dillon 			 * up laundering or cleaning too many pages.
9460d94caffSDavid Greenman 			 */
947936524aaSMatthew Dillon 			s = splvm();
948936524aaSMatthew Dillon 			TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE].pl, m, &marker, pageq);
949936524aaSMatthew Dillon 			splx(s);
9502b6b0df7SMatthew Dillon 			if (vm_pageout_clean(m) != 0) {
9512b6b0df7SMatthew Dillon 				--page_shortage;
952936524aaSMatthew Dillon 				--maxlaunder;
9532b6b0df7SMatthew Dillon 			}
954936524aaSMatthew Dillon 			s = splvm();
955936524aaSMatthew Dillon 			next = TAILQ_NEXT(&marker, pageq);
956936524aaSMatthew Dillon 			TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, &marker, pageq);
957936524aaSMatthew Dillon 			splx(s);
958f2a2857bSKirk McKusick 			if (vp) {
959f6b04d2bSDavid Greenman 				vput(vp);
960f2a2857bSKirk McKusick 				vn_finished_write(mp);
961f2a2857bSKirk McKusick 			}
9620d94caffSDavid Greenman 		}
963df8bae1dSRodney W. Grimes 	}
96426f9a767SRodney W. Grimes 
965df8bae1dSRodney W. Grimes 	/*
966936524aaSMatthew Dillon 	 * Compute the number of pages we want to try to move from the
967936524aaSMatthew Dillon 	 * active queue to the inactive queue.
9681c7c3c6aSMatthew Dillon 	 */
969936524aaSMatthew Dillon 	page_shortage = vm_paging_target() +
970936524aaSMatthew Dillon 		cnt.v_inactive_target - cnt.v_inactive_count;
971b182ec9eSJohn Dyson 	page_shortage += addl_page_shortage;
9721c7c3c6aSMatthew Dillon 
9731c7c3c6aSMatthew Dillon 	/*
974936524aaSMatthew Dillon 	 * Scan the active queue for things we can deactivate. We nominally
975936524aaSMatthew Dillon 	 * track the per-page activity counter and use it to locate
976936524aaSMatthew Dillon 	 * deactivation candidates.
9771c7c3c6aSMatthew Dillon 	 */
97826f9a767SRodney W. Grimes 
979b18bfc3dSJohn Dyson 	pcount = cnt.v_active_count;
980be72f788SAlan Cox 	m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
9811c7c3c6aSMatthew Dillon 
982b18bfc3dSJohn Dyson 	while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) {
983f35329acSJohn Dyson 
9847e006499SJohn Dyson 		/*
985956f3135SPhilippe Charnier 		 * This is a consistency check, and should likely be a panic
9867e006499SJohn Dyson 		 * or warning.
9877e006499SJohn Dyson 		 */
988f35329acSJohn Dyson 		if (m->queue != PQ_ACTIVE) {
98938efa82bSJohn Dyson 			break;
990f35329acSJohn Dyson 		}
991f35329acSJohn Dyson 
992b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(m, pageq);
993df8bae1dSRodney W. Grimes 		/*
99426f9a767SRodney W. Grimes 		 * Don't deactivate pages that are busy.
995df8bae1dSRodney W. Grimes 		 */
996a647a309SDavid Greenman 		if ((m->busy != 0) ||
9970d94caffSDavid Greenman 		    (m->flags & PG_BUSY) ||
998f6b04d2bSDavid Greenman 		    (m->hold_count != 0)) {
999f35329acSJohn Dyson 			s = splvm();
1000be72f788SAlan Cox 			TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1001be72f788SAlan Cox 			TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1002f35329acSJohn Dyson 			splx(s);
100326f9a767SRodney W. Grimes 			m = next;
100426f9a767SRodney W. Grimes 			continue;
1005df8bae1dSRodney W. Grimes 		}
1006b18bfc3dSJohn Dyson 
1007b18bfc3dSJohn Dyson 		/*
1008b18bfc3dSJohn Dyson 		 * The count for pagedaemon pages is done after checking the
1009956f3135SPhilippe Charnier 		 * page for eligibility...
1010b18bfc3dSJohn Dyson 		 */
1011b18bfc3dSJohn Dyson 		cnt.v_pdpages++;
1012ef743ce6SJohn Dyson 
10137e006499SJohn Dyson 		/*
10147e006499SJohn Dyson 		 * Check to see "how much" the page has been used.
10157e006499SJohn Dyson 		 */
10167e006499SJohn Dyson 		actcount = 0;
1017ef743ce6SJohn Dyson 		if (m->object->ref_count != 0) {
1018ef743ce6SJohn Dyson 			if (m->flags & PG_REFERENCED) {
10197e006499SJohn Dyson 				actcount += 1;
10200d94caffSDavid Greenman 			}
10210385347cSPeter Wemm 			actcount += pmap_ts_referenced(m);
10227e006499SJohn Dyson 			if (actcount) {
10237e006499SJohn Dyson 				m->act_count += ACT_ADVANCE + actcount;
102438efa82bSJohn Dyson 				if (m->act_count > ACT_MAX)
102538efa82bSJohn Dyson 					m->act_count = ACT_MAX;
102638efa82bSJohn Dyson 			}
1027b18bfc3dSJohn Dyson 		}
1028ef743ce6SJohn Dyson 
10297e006499SJohn Dyson 		/*
10307e006499SJohn Dyson 		 * Since we have "tested" this bit, we need to clear it now.
10317e006499SJohn Dyson 		 */
1032e69763a3SDoug Rabson 		vm_page_flag_clear(m, PG_REFERENCED);
1033ef743ce6SJohn Dyson 
10347e006499SJohn Dyson 		/*
10357e006499SJohn Dyson 		 * Only if an object is currently being used, do we use the
10367e006499SJohn Dyson 		 * page activation count stats.
10377e006499SJohn Dyson 		 */
10387e006499SJohn Dyson 		if (actcount && (m->object->ref_count != 0)) {
1039f35329acSJohn Dyson 			s = splvm();
1040be72f788SAlan Cox 			TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1041be72f788SAlan Cox 			TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1042f35329acSJohn Dyson 			splx(s);
104326f9a767SRodney W. Grimes 		} else {
104438efa82bSJohn Dyson 			m->act_count -= min(m->act_count, ACT_DECLINE);
10452b6b0df7SMatthew Dillon 			if (vm_pageout_algorithm ||
10462b6b0df7SMatthew Dillon 			    m->object->ref_count == 0 ||
10472b6b0df7SMatthew Dillon 			    m->act_count == 0) {
1048925a3a41SJohn Dyson 				page_shortage--;
1049d4a272dbSJohn Dyson 				if (m->object->ref_count == 0) {
1050ef743ce6SJohn Dyson 					vm_page_protect(m, VM_PROT_NONE);
1051d4a272dbSJohn Dyson 					if (m->dirty == 0)
10520d94caffSDavid Greenman 						vm_page_cache(m);
1053d4a272dbSJohn Dyson 					else
1054d4a272dbSJohn Dyson 						vm_page_deactivate(m);
10550d94caffSDavid Greenman 				} else {
105626f9a767SRodney W. Grimes 					vm_page_deactivate(m);
1057df8bae1dSRodney W. Grimes 				}
105838efa82bSJohn Dyson 			} else {
105938efa82bSJohn Dyson 				s = splvm();
1060be72f788SAlan Cox 				TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1061be72f788SAlan Cox 				TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
106238efa82bSJohn Dyson 				splx(s);
106338efa82bSJohn Dyson 			}
1064df8bae1dSRodney W. Grimes 		}
106526f9a767SRodney W. Grimes 		m = next;
106626f9a767SRodney W. Grimes 	}
1067df8bae1dSRodney W. Grimes 
1068f35329acSJohn Dyson 	s = splvm();
10691c7c3c6aSMatthew Dillon 
1070df8bae1dSRodney W. Grimes 	/*
10710d94caffSDavid Greenman 	 * We try to maintain some *really* free pages, this allows interrupt
10721c7c3c6aSMatthew Dillon 	 * code to be guaranteed space.  Since both cache and free queues
10731c7c3c6aSMatthew Dillon 	 * are considered basically 'free', moving pages from cache to free
10741c7c3c6aSMatthew Dillon 	 * does not effect other calculations.
1075df8bae1dSRodney W. Grimes 	 */
10761c7c3c6aSMatthew Dillon 
1077a1f6d91cSDavid Greenman 	while (cnt.v_free_count < cnt.v_free_reserved) {
10785070c7f8SJohn Dyson 		static int cache_rover = 0;
1079faa273d5SMatthew Dillon 		m = vm_page_list_find(PQ_CACHE, cache_rover, FALSE);
10800d94caffSDavid Greenman 		if (!m)
10810d94caffSDavid Greenman 			break;
10828b03c8edSMatthew Dillon 		if ((m->flags & (PG_BUSY|PG_UNMANAGED)) ||
10838b03c8edSMatthew Dillon 		    m->busy ||
10848b03c8edSMatthew Dillon 		    m->hold_count ||
10858b03c8edSMatthew Dillon 		    m->wire_count) {
1086d044d7bfSMatthew Dillon #ifdef INVARIANTS
1087d044d7bfSMatthew Dillon 			printf("Warning: busy page %p found in cache\n", m);
1088d044d7bfSMatthew Dillon #endif
1089aaba53daSMatthew Dillon 			vm_page_deactivate(m);
1090aaba53daSMatthew Dillon 			continue;
1091aaba53daSMatthew Dillon 		}
10925070c7f8SJohn Dyson 		cache_rover = (cache_rover + PQ_PRIME2) & PQ_L2_MASK;
1093925a3a41SJohn Dyson 		vm_pageout_page_free(m);
10940bb3a0d2SDavid Greenman 		cnt.v_dfree++;
109526f9a767SRodney W. Grimes 	}
1096f35329acSJohn Dyson 	splx(s);
10975663e6deSDavid Greenman 
1098ceb0cf87SJohn Dyson #if !defined(NO_SWAPPING)
1099ceb0cf87SJohn Dyson 	/*
1100ceb0cf87SJohn Dyson 	 * Idle process swapout -- run once per second.
1101ceb0cf87SJohn Dyson 	 */
1102ceb0cf87SJohn Dyson 	if (vm_swap_idle_enabled) {
1103ceb0cf87SJohn Dyson 		static long lsec;
1104227ee8a1SPoul-Henning Kamp 		if (time_second != lsec) {
1105ceb0cf87SJohn Dyson 			vm_pageout_req_swapout |= VM_SWAP_IDLE;
1106ceb0cf87SJohn Dyson 			vm_req_vmdaemon();
1107227ee8a1SPoul-Henning Kamp 			lsec = time_second;
1108ceb0cf87SJohn Dyson 		}
1109ceb0cf87SJohn Dyson 	}
1110ceb0cf87SJohn Dyson #endif
1111ceb0cf87SJohn Dyson 
11125663e6deSDavid Greenman 	/*
1113f6b04d2bSDavid Greenman 	 * If we didn't get enough free pages, and we have skipped a vnode
11144c1f8ee9SDavid Greenman 	 * in a writeable object, wakeup the sync daemon.  And kick swapout
11154c1f8ee9SDavid Greenman 	 * if we did not get enough free pages.
1116f6b04d2bSDavid Greenman 	 */
111790ecac61SMatthew Dillon 	if (vm_paging_target() > 0) {
111890ecac61SMatthew Dillon 		if (vnodes_skipped && vm_page_count_min())
1119d50c1994SPeter Wemm 			(void) speedup_syncer();
112038efa82bSJohn Dyson #if !defined(NO_SWAPPING)
112190ecac61SMatthew Dillon 		if (vm_swap_enabled && vm_page_count_target()) {
11224c1f8ee9SDavid Greenman 			vm_req_vmdaemon();
1123ceb0cf87SJohn Dyson 			vm_pageout_req_swapout |= VM_SWAP_NORMAL;
11244c1f8ee9SDavid Greenman 		}
11255afce282SDavid Greenman #endif
11264c1f8ee9SDavid Greenman 	}
11274c1f8ee9SDavid Greenman 
1128f6b04d2bSDavid Greenman 	/*
1129ff2b5645SMatthew Dillon 	 * If we are out of swap and were not able to reach our paging
1130ff2b5645SMatthew Dillon 	 * target, kill the largest process.
11311c58e4e5SJohn Baldwin 	 *
11321c58e4e5SJohn Baldwin 	 * We keep the process bigproc locked once we find it to keep anyone
11331c58e4e5SJohn Baldwin 	 * from messing with it; however, there is a possibility of
11341c58e4e5SJohn Baldwin 	 * deadlock if process B is bigproc and one of it's child processes
11351c58e4e5SJohn Baldwin 	 * attempts to propagate a signal to B while we are waiting for A's
11361c58e4e5SJohn Baldwin 	 * lock while walking this list.  To avoid this, we don't block on
11371c58e4e5SJohn Baldwin 	 * the process lock but just skip a process if it is already locked.
11385663e6deSDavid Greenman 	 */
1139ff2b5645SMatthew Dillon 	if ((vm_swap_size < 64 && vm_page_count_min()) ||
1140ff2b5645SMatthew Dillon 	    (swap_pager_full && vm_paging_target() > 0)) {
1141ff2b5645SMatthew Dillon #if 0
1142936524aaSMatthew Dillon 	if ((vm_swap_size < 64 || swap_pager_full) && vm_page_count_min()) {
1143ff2b5645SMatthew Dillon #endif
11445663e6deSDavid Greenman 		bigproc = NULL;
11455663e6deSDavid Greenman 		bigsize = 0;
11461005a129SJohn Baldwin 		sx_slock(&allproc_lock);
11478606d880SJohn Baldwin 		LIST_FOREACH(p, &allproc, p_list) {
11485663e6deSDavid Greenman 			/*
11491c58e4e5SJohn Baldwin 			 * If this process is already locked, skip it.
11501c58e4e5SJohn Baldwin 			 */
11511c58e4e5SJohn Baldwin 			if (PROC_TRYLOCK(p) == 0)
11521c58e4e5SJohn Baldwin 				continue;
11531c58e4e5SJohn Baldwin 			/*
11545663e6deSDavid Greenman 			 * if this is a system process, skip it
11555663e6deSDavid Greenman 			 */
1156c8da68e9SPeter Wemm 			if ((p->p_flag & P_SYSTEM) || (p->p_lock > 0) ||
1157c8da68e9SPeter Wemm 			    (p->p_pid == 1) ||
115879221631SDavid Greenman 			    ((p->p_pid < 48) && (vm_swap_size != 0))) {
11598606d880SJohn Baldwin 				PROC_UNLOCK(p);
11605663e6deSDavid Greenman 				continue;
11615663e6deSDavid Greenman 			}
11625663e6deSDavid Greenman 			/*
11635663e6deSDavid Greenman 			 * if the process is in a non-running type state,
11645663e6deSDavid Greenman 			 * don't touch it.
11655663e6deSDavid Greenman 			 */
11669ed346baSBosko Milekic 			mtx_lock_spin(&sched_lock);
11675663e6deSDavid Greenman 			if (p->p_stat != SRUN && p->p_stat != SSLEEP) {
11689ed346baSBosko Milekic 				mtx_unlock_spin(&sched_lock);
11691c58e4e5SJohn Baldwin 				PROC_UNLOCK(p);
11705663e6deSDavid Greenman 				continue;
11715663e6deSDavid Greenman 			}
11729ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
11735663e6deSDavid Greenman 			/*
11745663e6deSDavid Greenman 			 * get the process size
11755663e6deSDavid Greenman 			 */
1176ff2b5645SMatthew Dillon 			size = vmspace_resident_count(p->p_vmspace) +
1177ff2b5645SMatthew Dillon 				vmspace_swap_count(p->p_vmspace);
11785663e6deSDavid Greenman 			/*
11795663e6deSDavid Greenman 			 * if the this process is bigger than the biggest one
11805663e6deSDavid Greenman 			 * remember it.
11815663e6deSDavid Greenman 			 */
11825663e6deSDavid Greenman 			if (size > bigsize) {
11831c58e4e5SJohn Baldwin 				if (bigproc != NULL)
11841c58e4e5SJohn Baldwin 					PROC_UNLOCK(bigproc);
11855663e6deSDavid Greenman 				bigproc = p;
11865663e6deSDavid Greenman 				bigsize = size;
11871c58e4e5SJohn Baldwin 			} else
11881c58e4e5SJohn Baldwin 				PROC_UNLOCK(p);
11895663e6deSDavid Greenman 		}
11901005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
11915663e6deSDavid Greenman 		if (bigproc != NULL) {
1192729b1e51SDavid Greenman 			killproc(bigproc, "out of swap space");
11939ed346baSBosko Milekic 			mtx_lock_spin(&sched_lock);
11945663e6deSDavid Greenman 			bigproc->p_estcpu = 0;
11955663e6deSDavid Greenman 			bigproc->p_nice = PRIO_MIN;
11965663e6deSDavid Greenman 			resetpriority(bigproc);
11979ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
11981c58e4e5SJohn Baldwin 			PROC_UNLOCK(bigproc);
119924a1cce3SDavid Greenman 			wakeup(&cnt.v_free_count);
12005663e6deSDavid Greenman 		}
12015663e6deSDavid Greenman 	}
120226f9a767SRodney W. Grimes }
120326f9a767SRodney W. Grimes 
1204dc2efb27SJohn Dyson /*
1205dc2efb27SJohn Dyson  * This routine tries to maintain the pseudo LRU active queue,
1206dc2efb27SJohn Dyson  * so that during long periods of time where there is no paging,
1207956f3135SPhilippe Charnier  * that some statistic accumulation still occurs.  This code
1208dc2efb27SJohn Dyson  * helps the situation where paging just starts to occur.
1209dc2efb27SJohn Dyson  */
1210dc2efb27SJohn Dyson static void
1211dc2efb27SJohn Dyson vm_pageout_page_stats()
1212dc2efb27SJohn Dyson {
1213dc2efb27SJohn Dyson 	int s;
1214dc2efb27SJohn Dyson 	vm_page_t m,next;
1215dc2efb27SJohn Dyson 	int pcount,tpcount;		/* Number of pages to check */
1216dc2efb27SJohn Dyson 	static int fullintervalcount = 0;
1217bef608bdSJohn Dyson 	int page_shortage;
121825db2c54SMatthew Dillon 	int s0;
1219bef608bdSJohn Dyson 
122090ecac61SMatthew Dillon 	page_shortage =
122190ecac61SMatthew Dillon 	    (cnt.v_inactive_target + cnt.v_cache_max + cnt.v_free_min) -
1222bef608bdSJohn Dyson 	    (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count);
122390ecac61SMatthew Dillon 
1224bef608bdSJohn Dyson 	if (page_shortage <= 0)
1225bef608bdSJohn Dyson 		return;
1226dc2efb27SJohn Dyson 
122725db2c54SMatthew Dillon 	s0 = splvm();
122825db2c54SMatthew Dillon 
1229dc2efb27SJohn Dyson 	pcount = cnt.v_active_count;
1230dc2efb27SJohn Dyson 	fullintervalcount += vm_pageout_stats_interval;
1231dc2efb27SJohn Dyson 	if (fullintervalcount < vm_pageout_full_stats_interval) {
1232dc2efb27SJohn Dyson 		tpcount = (vm_pageout_stats_max * cnt.v_active_count) / cnt.v_page_count;
1233dc2efb27SJohn Dyson 		if (pcount > tpcount)
1234dc2efb27SJohn Dyson 			pcount = tpcount;
1235883f3caaSMatthew Dillon 	} else {
1236883f3caaSMatthew Dillon 		fullintervalcount = 0;
1237dc2efb27SJohn Dyson 	}
1238dc2efb27SJohn Dyson 
1239be72f788SAlan Cox 	m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
1240dc2efb27SJohn Dyson 	while ((m != NULL) && (pcount-- > 0)) {
12417e006499SJohn Dyson 		int actcount;
1242dc2efb27SJohn Dyson 
1243dc2efb27SJohn Dyson 		if (m->queue != PQ_ACTIVE) {
1244dc2efb27SJohn Dyson 			break;
1245dc2efb27SJohn Dyson 		}
1246dc2efb27SJohn Dyson 
1247dc2efb27SJohn Dyson 		next = TAILQ_NEXT(m, pageq);
1248dc2efb27SJohn Dyson 		/*
1249dc2efb27SJohn Dyson 		 * Don't deactivate pages that are busy.
1250dc2efb27SJohn Dyson 		 */
1251dc2efb27SJohn Dyson 		if ((m->busy != 0) ||
1252dc2efb27SJohn Dyson 		    (m->flags & PG_BUSY) ||
1253dc2efb27SJohn Dyson 		    (m->hold_count != 0)) {
1254dc2efb27SJohn Dyson 			s = splvm();
1255be72f788SAlan Cox 			TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1256be72f788SAlan Cox 			TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1257dc2efb27SJohn Dyson 			splx(s);
1258dc2efb27SJohn Dyson 			m = next;
1259dc2efb27SJohn Dyson 			continue;
1260dc2efb27SJohn Dyson 		}
1261dc2efb27SJohn Dyson 
12627e006499SJohn Dyson 		actcount = 0;
1263dc2efb27SJohn Dyson 		if (m->flags & PG_REFERENCED) {
1264e69763a3SDoug Rabson 			vm_page_flag_clear(m, PG_REFERENCED);
12657e006499SJohn Dyson 			actcount += 1;
1266dc2efb27SJohn Dyson 		}
1267dc2efb27SJohn Dyson 
12680385347cSPeter Wemm 		actcount += pmap_ts_referenced(m);
12697e006499SJohn Dyson 		if (actcount) {
12707e006499SJohn Dyson 			m->act_count += ACT_ADVANCE + actcount;
1271dc2efb27SJohn Dyson 			if (m->act_count > ACT_MAX)
1272dc2efb27SJohn Dyson 				m->act_count = ACT_MAX;
1273dc2efb27SJohn Dyson 			s = splvm();
1274be72f788SAlan Cox 			TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1275be72f788SAlan Cox 			TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1276dc2efb27SJohn Dyson 			splx(s);
1277dc2efb27SJohn Dyson 		} else {
1278dc2efb27SJohn Dyson 			if (m->act_count == 0) {
12797e006499SJohn Dyson 				/*
12802b6b0df7SMatthew Dillon 				 * We turn off page access, so that we have
12812b6b0df7SMatthew Dillon 				 * more accurate RSS stats.  We don't do this
12822b6b0df7SMatthew Dillon 				 * in the normal page deactivation when the
12832b6b0df7SMatthew Dillon 				 * system is loaded VM wise, because the
12842b6b0df7SMatthew Dillon 				 * cost of the large number of page protect
12852b6b0df7SMatthew Dillon 				 * operations would be higher than the value
12862b6b0df7SMatthew Dillon 				 * of doing the operation.
12877e006499SJohn Dyson 				 */
1288dc2efb27SJohn Dyson 				vm_page_protect(m, VM_PROT_NONE);
1289dc2efb27SJohn Dyson 				vm_page_deactivate(m);
1290dc2efb27SJohn Dyson 			} else {
1291dc2efb27SJohn Dyson 				m->act_count -= min(m->act_count, ACT_DECLINE);
1292dc2efb27SJohn Dyson 				s = splvm();
1293be72f788SAlan Cox 				TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1294be72f788SAlan Cox 				TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1295dc2efb27SJohn Dyson 				splx(s);
1296dc2efb27SJohn Dyson 			}
1297dc2efb27SJohn Dyson 		}
1298dc2efb27SJohn Dyson 
1299dc2efb27SJohn Dyson 		m = next;
1300dc2efb27SJohn Dyson 	}
130125db2c54SMatthew Dillon 	splx(s0);
1302dc2efb27SJohn Dyson }
1303dc2efb27SJohn Dyson 
1304b182ec9eSJohn Dyson static int
1305b182ec9eSJohn Dyson vm_pageout_free_page_calc(count)
1306b182ec9eSJohn Dyson vm_size_t count;
1307b182ec9eSJohn Dyson {
1308b182ec9eSJohn Dyson 	if (count < cnt.v_page_count)
1309b182ec9eSJohn Dyson 		 return 0;
1310b182ec9eSJohn Dyson 	/*
1311b182ec9eSJohn Dyson 	 * free_reserved needs to include enough for the largest swap pager
1312b182ec9eSJohn Dyson 	 * structures plus enough for any pv_entry structs when paging.
1313b182ec9eSJohn Dyson 	 */
1314b182ec9eSJohn Dyson 	if (cnt.v_page_count > 1024)
1315b182ec9eSJohn Dyson 		cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200;
1316b182ec9eSJohn Dyson 	else
1317b182ec9eSJohn Dyson 		cnt.v_free_min = 4;
1318f35329acSJohn Dyson 	cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE +
1319f35329acSJohn Dyson 		cnt.v_interrupt_free_min;
1320f35329acSJohn Dyson 	cnt.v_free_reserved = vm_pageout_page_count +
1321a15403deSJohn Dyson 		cnt.v_pageout_free_min + (count / 768) + PQ_L2_SIZE;
132290ecac61SMatthew Dillon 	cnt.v_free_severe = cnt.v_free_min / 2;
1323a2f4a846SJohn Dyson 	cnt.v_free_min += cnt.v_free_reserved;
132490ecac61SMatthew Dillon 	cnt.v_free_severe += cnt.v_free_reserved;
1325b182ec9eSJohn Dyson 	return 1;
1326b182ec9eSJohn Dyson }
1327b182ec9eSJohn Dyson 
1328b182ec9eSJohn Dyson 
1329df8bae1dSRodney W. Grimes /*
1330df8bae1dSRodney W. Grimes  *	vm_pageout is the high level pageout daemon.
1331df8bae1dSRodney W. Grimes  */
13322b14f991SJulian Elischer static void
133326f9a767SRodney W. Grimes vm_pageout()
1334df8bae1dSRodney W. Grimes {
13352b6b0df7SMatthew Dillon 	int pass;
13360384fff8SJason Evans 
133769a78d46SJohn Baldwin 	mtx_lock(&Giant);
13380384fff8SJason Evans 
1339df8bae1dSRodney W. Grimes 	/*
1340df8bae1dSRodney W. Grimes 	 * Initialize some paging parameters.
1341df8bae1dSRodney W. Grimes 	 */
1342df8bae1dSRodney W. Grimes 
1343f6b04d2bSDavid Greenman 	cnt.v_interrupt_free_min = 2;
1344f35329acSJohn Dyson 	if (cnt.v_page_count < 2000)
1345f35329acSJohn Dyson 		vm_pageout_page_count = 8;
1346f6b04d2bSDavid Greenman 
1347b182ec9eSJohn Dyson 	vm_pageout_free_page_calc(cnt.v_page_count);
1348ed74321bSDavid Greenman 	/*
13492b6b0df7SMatthew Dillon 	 * v_free_target and v_cache_min control pageout hysteresis.  Note
13502b6b0df7SMatthew Dillon 	 * that these are more a measure of the VM cache queue hysteresis
13512b6b0df7SMatthew Dillon 	 * then the VM free queue.  Specifically, v_free_target is the
13522b6b0df7SMatthew Dillon 	 * high water mark (free+cache pages).
13532b6b0df7SMatthew Dillon 	 *
13542b6b0df7SMatthew Dillon 	 * v_free_reserved + v_cache_min (mostly means v_cache_min) is the
13552b6b0df7SMatthew Dillon 	 * low water mark, while v_free_min is the stop.  v_cache_min must
13562b6b0df7SMatthew Dillon 	 * be big enough to handle memory needs while the pageout daemon
13572b6b0df7SMatthew Dillon 	 * is signalled and run to free more pages.
1358ed74321bSDavid Greenman 	 */
1359a15403deSJohn Dyson 	if (cnt.v_free_count > 6144)
13602b6b0df7SMatthew Dillon 		cnt.v_free_target = 4 * cnt.v_free_min + cnt.v_free_reserved;
1361a15403deSJohn Dyson 	else
1362a15403deSJohn Dyson 		cnt.v_free_target = 2 * cnt.v_free_min + cnt.v_free_reserved;
13636f2b142eSDavid Greenman 
1364a15403deSJohn Dyson 	if (cnt.v_free_count > 2048) {
1365a15403deSJohn Dyson 		cnt.v_cache_min = cnt.v_free_target;
1366a15403deSJohn Dyson 		cnt.v_cache_max = 2 * cnt.v_cache_min;
1367a15403deSJohn Dyson 		cnt.v_inactive_target = (3 * cnt.v_free_target) / 2;
13680d94caffSDavid Greenman 	} else {
13690d94caffSDavid Greenman 		cnt.v_cache_min = 0;
13700d94caffSDavid Greenman 		cnt.v_cache_max = 0;
13716f2b142eSDavid Greenman 		cnt.v_inactive_target = cnt.v_free_count / 4;
13720d94caffSDavid Greenman 	}
1373e47ed70bSJohn Dyson 	if (cnt.v_inactive_target > cnt.v_free_count / 3)
1374e47ed70bSJohn Dyson 		cnt.v_inactive_target = cnt.v_free_count / 3;
1375df8bae1dSRodney W. Grimes 
1376df8bae1dSRodney W. Grimes 	/* XXX does not really belong here */
1377df8bae1dSRodney W. Grimes 	if (vm_page_max_wired == 0)
1378df8bae1dSRodney W. Grimes 		vm_page_max_wired = cnt.v_free_count / 3;
1379df8bae1dSRodney W. Grimes 
1380dc2efb27SJohn Dyson 	if (vm_pageout_stats_max == 0)
1381dc2efb27SJohn Dyson 		vm_pageout_stats_max = cnt.v_free_target;
1382dc2efb27SJohn Dyson 
1383dc2efb27SJohn Dyson 	/*
1384dc2efb27SJohn Dyson 	 * Set interval in seconds for stats scan.
1385dc2efb27SJohn Dyson 	 */
1386dc2efb27SJohn Dyson 	if (vm_pageout_stats_interval == 0)
1387bef608bdSJohn Dyson 		vm_pageout_stats_interval = 5;
1388dc2efb27SJohn Dyson 	if (vm_pageout_full_stats_interval == 0)
1389dc2efb27SJohn Dyson 		vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4;
1390dc2efb27SJohn Dyson 
1391dc2efb27SJohn Dyson 
1392dc2efb27SJohn Dyson 	/*
1393dc2efb27SJohn Dyson 	 * Set maximum free per pass
1394dc2efb27SJohn Dyson 	 */
1395dc2efb27SJohn Dyson 	if (vm_pageout_stats_free_max == 0)
1396bef608bdSJohn Dyson 		vm_pageout_stats_free_max = 5;
1397dc2efb27SJohn Dyson 
13983614c6fcSJohn Baldwin 	PROC_LOCK(curproc);
1399e929c00dSKirk McKusick 	curproc->p_flag |= P_BUFEXHAUST;
14003614c6fcSJohn Baldwin 	PROC_UNLOCK(curproc);
140124a1cce3SDavid Greenman 	swap_pager_swap_init();
14022b6b0df7SMatthew Dillon 	pass = 0;
1403df8bae1dSRodney W. Grimes 	/*
14040d94caffSDavid Greenman 	 * The pageout daemon is never done, so loop forever.
1405df8bae1dSRodney W. Grimes 	 */
1406df8bae1dSRodney W. Grimes 	while (TRUE) {
1407dc2efb27SJohn Dyson 		int error;
1408b18bfc3dSJohn Dyson 		int s = splvm();
140990ecac61SMatthew Dillon 
1410936524aaSMatthew Dillon 		/*
1411936524aaSMatthew Dillon 		 * If we have enough free memory, wakeup waiters.  Do
1412936524aaSMatthew Dillon 		 * not clear vm_pages_needed until we reach our target,
1413936524aaSMatthew Dillon 		 * otherwise we may be woken up over and over again and
1414936524aaSMatthew Dillon 		 * waste a lot of cpu.
1415936524aaSMatthew Dillon 		 */
1416936524aaSMatthew Dillon 		if (vm_pages_needed && !vm_page_count_min()) {
1417936524aaSMatthew Dillon 			if (vm_paging_needed() <= 0)
1418936524aaSMatthew Dillon 				vm_pages_needed = 0;
1419936524aaSMatthew Dillon 			wakeup(&cnt.v_free_count);
1420936524aaSMatthew Dillon 		}
1421936524aaSMatthew Dillon 		if (vm_pages_needed) {
142290ecac61SMatthew Dillon 			/*
14232b6b0df7SMatthew Dillon 			 * Still not done, take a second pass without waiting
14242b6b0df7SMatthew Dillon 			 * (unlimited dirty cleaning), otherwise sleep a bit
14252b6b0df7SMatthew Dillon 			 * and try again.
142690ecac61SMatthew Dillon 			 */
14272b6b0df7SMatthew Dillon 			++pass;
14282b6b0df7SMatthew Dillon 			if (pass > 1)
14290cddd8f0SMatthew Dillon 				tsleep(&vm_pages_needed, PVM,
143023955314SAlfred Perlstein 				       "psleep", hz/2);
143190ecac61SMatthew Dillon 		} else {
143290ecac61SMatthew Dillon 			/*
14332b6b0df7SMatthew Dillon 			 * Good enough, sleep & handle stats.  Prime the pass
14342b6b0df7SMatthew Dillon 			 * for the next run.
143590ecac61SMatthew Dillon 			 */
14362b6b0df7SMatthew Dillon 			if (pass > 1)
14372b6b0df7SMatthew Dillon 				pass = 1;
14382b6b0df7SMatthew Dillon 			else
14392b6b0df7SMatthew Dillon 				pass = 0;
14400cddd8f0SMatthew Dillon 			error = tsleep(&vm_pages_needed, PVM,
14410cddd8f0SMatthew Dillon 				    "psleep", vm_pageout_stats_interval * hz);
1442dc2efb27SJohn Dyson 			if (error && !vm_pages_needed) {
1443dc2efb27SJohn Dyson 				splx(s);
14442b6b0df7SMatthew Dillon 				pass = 0;
1445dc2efb27SJohn Dyson 				vm_pageout_page_stats();
1446dc2efb27SJohn Dyson 				continue;
1447dc2efb27SJohn Dyson 			}
1448f919ebdeSDavid Greenman 		}
1449e47ed70bSJohn Dyson 
1450b18bfc3dSJohn Dyson 		if (vm_pages_needed)
1451b18bfc3dSJohn Dyson 			cnt.v_pdwakeups++;
1452f919ebdeSDavid Greenman 		splx(s);
14532b6b0df7SMatthew Dillon 		vm_pageout_scan(pass);
14542d8acc0fSJohn Dyson 		vm_pageout_deficit = 0;
1455df8bae1dSRodney W. Grimes 	}
1456df8bae1dSRodney W. Grimes }
145726f9a767SRodney W. Grimes 
1458e0c5a895SJohn Dyson void
1459e0c5a895SJohn Dyson pagedaemon_wakeup()
1460e0c5a895SJohn Dyson {
1461e0c5a895SJohn Dyson 	if (!vm_pages_needed && curproc != pageproc) {
1462e0c5a895SJohn Dyson 		vm_pages_needed++;
1463e0c5a895SJohn Dyson 		wakeup(&vm_pages_needed);
1464e0c5a895SJohn Dyson 	}
1465e0c5a895SJohn Dyson }
1466e0c5a895SJohn Dyson 
146738efa82bSJohn Dyson #if !defined(NO_SWAPPING)
14685afce282SDavid Greenman static void
14695afce282SDavid Greenman vm_req_vmdaemon()
14705afce282SDavid Greenman {
14715afce282SDavid Greenman 	static int lastrun = 0;
14725afce282SDavid Greenman 
1473b18bfc3dSJohn Dyson 	if ((ticks > (lastrun + hz)) || (ticks < lastrun)) {
14745afce282SDavid Greenman 		wakeup(&vm_daemon_needed);
14755afce282SDavid Greenman 		lastrun = ticks;
14765afce282SDavid Greenman 	}
14775afce282SDavid Greenman }
14785afce282SDavid Greenman 
14792b14f991SJulian Elischer static void
14804f9fb771SBruce Evans vm_daemon()
14810d94caffSDavid Greenman {
14822fe6e4d7SDavid Greenman 	struct proc *p;
14830d94caffSDavid Greenman 
14843614c6fcSJohn Baldwin 	mtx_lock(&Giant);
14852fe6e4d7SDavid Greenman 	while (TRUE) {
14860cddd8f0SMatthew Dillon 		tsleep(&vm_daemon_needed, PPAUSE, "psleep", 0);
14874c1f8ee9SDavid Greenman 		if (vm_pageout_req_swapout) {
1488ceb0cf87SJohn Dyson 			swapout_procs(vm_pageout_req_swapout);
14894c1f8ee9SDavid Greenman 			vm_pageout_req_swapout = 0;
14904c1f8ee9SDavid Greenman 		}
14912fe6e4d7SDavid Greenman 		/*
14920d94caffSDavid Greenman 		 * scan the processes for exceeding their rlimits or if
14930d94caffSDavid Greenman 		 * process is swapped out -- deactivate pages
14942fe6e4d7SDavid Greenman 		 */
14952fe6e4d7SDavid Greenman 
14961005a129SJohn Baldwin 		sx_slock(&allproc_lock);
1497fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
1498fe2144fdSLuoqi Chen 			vm_pindex_t limit, size;
14992fe6e4d7SDavid Greenman 
15002fe6e4d7SDavid Greenman 			/*
15012fe6e4d7SDavid Greenman 			 * if this is a system process or if we have already
15022fe6e4d7SDavid Greenman 			 * looked at this process, skip it.
15032fe6e4d7SDavid Greenman 			 */
15042fe6e4d7SDavid Greenman 			if (p->p_flag & (P_SYSTEM | P_WEXIT)) {
15052fe6e4d7SDavid Greenman 				continue;
15062fe6e4d7SDavid Greenman 			}
15072fe6e4d7SDavid Greenman 			/*
15082fe6e4d7SDavid Greenman 			 * if the process is in a non-running type state,
15092fe6e4d7SDavid Greenman 			 * don't touch it.
15102fe6e4d7SDavid Greenman 			 */
15119ed346baSBosko Milekic 			mtx_lock_spin(&sched_lock);
15122fe6e4d7SDavid Greenman 			if (p->p_stat != SRUN && p->p_stat != SSLEEP) {
15139ed346baSBosko Milekic 				mtx_unlock_spin(&sched_lock);
15142fe6e4d7SDavid Greenman 				continue;
15152fe6e4d7SDavid Greenman 			}
15162fe6e4d7SDavid Greenman 			/*
15172fe6e4d7SDavid Greenman 			 * get a limit
15182fe6e4d7SDavid Greenman 			 */
1519fe2144fdSLuoqi Chen 			limit = OFF_TO_IDX(
1520fe2144fdSLuoqi Chen 			    qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur,
1521fe2144fdSLuoqi Chen 				p->p_rlimit[RLIMIT_RSS].rlim_max));
15222fe6e4d7SDavid Greenman 
15232fe6e4d7SDavid Greenman 			/*
15240d94caffSDavid Greenman 			 * let processes that are swapped out really be
15250d94caffSDavid Greenman 			 * swapped out set the limit to nothing (will force a
15260d94caffSDavid Greenman 			 * swap-out.)
15272fe6e4d7SDavid Greenman 			 */
15288606d880SJohn Baldwin 			if ((p->p_sflag & PS_INMEM) == 0)
15290d94caffSDavid Greenman 				limit = 0;	/* XXX */
15309ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
15312fe6e4d7SDavid Greenman 
1532fe2144fdSLuoqi Chen 			size = vmspace_resident_count(p->p_vmspace);
15332fe6e4d7SDavid Greenman 			if (limit >= 0 && size >= limit) {
1534fe2144fdSLuoqi Chen 				vm_pageout_map_deactivate_pages(
1535fe2144fdSLuoqi Chen 				    &p->p_vmspace->vm_map, limit);
15362fe6e4d7SDavid Greenman 			}
15372fe6e4d7SDavid Greenman 		}
15381005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
153924a1cce3SDavid Greenman 	}
15402fe6e4d7SDavid Greenman }
154138efa82bSJohn Dyson #endif
1542