xref: /freebsd/sys/vm/vm_pageout.c (revision 45ae1d91477404078b5690db64522b6a5eeaf3dd)
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.
67df8bae1dSRodney W. Grimes  */
68df8bae1dSRodney W. Grimes 
69df8bae1dSRodney W. Grimes /*
70df8bae1dSRodney W. Grimes  *	The proverbial page-out daemon.
71df8bae1dSRodney W. Grimes  */
72df8bae1dSRodney W. Grimes 
73874651b1SDavid E. O'Brien #include <sys/cdefs.h>
74874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
75874651b1SDavid E. O'Brien 
76faa5f8d8SAndrzej Bialecki #include "opt_vm.h"
77df8bae1dSRodney W. Grimes #include <sys/param.h>
7826f9a767SRodney W. Grimes #include <sys/systm.h>
79b5e8ce9fSBruce Evans #include <sys/kernel.h>
80855a310fSJeff Roberson #include <sys/eventhandler.h>
81fb919e4dSMark Murray #include <sys/lock.h>
82fb919e4dSMark Murray #include <sys/mutex.h>
8326f9a767SRodney W. Grimes #include <sys/proc.h>
849c8b8baaSPeter Wemm #include <sys/kthread.h>
850384fff8SJason Evans #include <sys/ktr.h>
8626f9a767SRodney W. Grimes #include <sys/resourcevar.h>
87b43179fbSJeff Roberson #include <sys/sched.h>
88d2fc5315SPoul-Henning Kamp #include <sys/signalvar.h>
89f6b04d2bSDavid Greenman #include <sys/vnode.h>
90efeaf95aSDavid Greenman #include <sys/vmmeter.h>
911005a129SJohn Baldwin #include <sys/sx.h>
9238efa82bSJohn Dyson #include <sys/sysctl.h>
93df8bae1dSRodney W. Grimes 
94df8bae1dSRodney W. Grimes #include <vm/vm.h>
95efeaf95aSDavid Greenman #include <vm/vm_param.h>
96efeaf95aSDavid Greenman #include <vm/vm_object.h>
97df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
98efeaf95aSDavid Greenman #include <vm/vm_map.h>
99df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
10024a1cce3SDavid Greenman #include <vm/vm_pager.h>
10105f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h>
102efeaf95aSDavid Greenman #include <vm/vm_extern.h>
103670d17b5SJeff Roberson #include <vm/uma.h>
104df8bae1dSRodney W. Grimes 
1050384fff8SJason Evans #include <machine/mutex.h>
1060384fff8SJason Evans 
1072b14f991SJulian Elischer /*
1082b14f991SJulian Elischer  * System initialization
1092b14f991SJulian Elischer  */
1102b14f991SJulian Elischer 
1112b14f991SJulian Elischer /* the kernel process "vm_pageout"*/
11211caded3SAlfred Perlstein static void vm_pageout(void);
11311caded3SAlfred Perlstein static int vm_pageout_clean(vm_page_t);
114ff2023a5SAlan Cox static void vm_pageout_page_free(vm_page_t);
115eea85e9bSAlan Cox static void vm_pageout_pmap_collect(void);
11611caded3SAlfred Perlstein static void vm_pageout_scan(int pass);
11745ae1d91SAlan Cox 
1182b14f991SJulian Elischer struct proc *pageproc;
1192b14f991SJulian Elischer 
1202b14f991SJulian Elischer static struct kproc_desc page_kp = {
1212b14f991SJulian Elischer 	"pagedaemon",
1222b14f991SJulian Elischer 	vm_pageout,
1232b14f991SJulian Elischer 	&pageproc
1242b14f991SJulian Elischer };
1259c8b8baaSPeter Wemm SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, &page_kp)
1262b14f991SJulian Elischer 
12738efa82bSJohn Dyson #if !defined(NO_SWAPPING)
1282b14f991SJulian Elischer /* the kernel process "vm_daemon"*/
12911caded3SAlfred Perlstein static void vm_daemon(void);
130f708ef1bSPoul-Henning Kamp static struct	proc *vmproc;
1312b14f991SJulian Elischer 
1322b14f991SJulian Elischer static struct kproc_desc vm_kp = {
1332b14f991SJulian Elischer 	"vmdaemon",
1342b14f991SJulian Elischer 	vm_daemon,
1352b14f991SJulian Elischer 	&vmproc
1362b14f991SJulian Elischer };
1379c8b8baaSPeter Wemm SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp)
13838efa82bSJohn Dyson #endif
1392b14f991SJulian Elischer 
1402b14f991SJulian Elischer 
1418b245767SAlan Cox int vm_pages_needed;		/* Event on which pageout daemon sleeps */
1428b245767SAlan Cox int vm_pageout_deficit;		/* Estimated number of pages deficit */
1438b245767SAlan Cox int vm_pageout_pages_needed;	/* flag saying that the pageout daemon needs pages */
14426f9a767SRodney W. Grimes 
14538efa82bSJohn Dyson #if !defined(NO_SWAPPING)
146f708ef1bSPoul-Henning Kamp static int vm_pageout_req_swapout;	/* XXX */
147f708ef1bSPoul-Henning Kamp static int vm_daemon_needed;
14838efa82bSJohn Dyson #endif
1492b6b0df7SMatthew Dillon static int vm_max_launder = 32;
150303b270bSEivind Eklund static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0;
151303b270bSEivind Eklund static int vm_pageout_full_stats_interval = 0;
1522b6b0df7SMatthew Dillon static int vm_pageout_stats_free_max=0, vm_pageout_algorithm=0;
153303b270bSEivind Eklund static int defer_swap_pageouts=0;
154303b270bSEivind Eklund static int disable_swap_pageouts=0;
15570111b90SJohn Dyson 
15638efa82bSJohn Dyson #if defined(NO_SWAPPING)
157303b270bSEivind Eklund static int vm_swap_enabled=0;
158303b270bSEivind Eklund static int vm_swap_idle_enabled=0;
15938efa82bSJohn Dyson #else
160303b270bSEivind Eklund static int vm_swap_enabled=1;
161303b270bSEivind Eklund static int vm_swap_idle_enabled=0;
16238efa82bSJohn Dyson #endif
16338efa82bSJohn Dyson 
16438efa82bSJohn Dyson SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm,
1652b6b0df7SMatthew Dillon 	CTLFLAG_RW, &vm_pageout_algorithm, 0, "LRU page mgmt");
1662b6b0df7SMatthew Dillon 
1672b6b0df7SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, max_launder,
1682b6b0df7SMatthew Dillon 	CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout");
16938efa82bSJohn Dyson 
170dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max,
171b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length");
172dc2efb27SJohn Dyson 
173dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval,
174b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan");
175dc2efb27SJohn Dyson 
176dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval,
177b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan");
178dc2efb27SJohn Dyson 
179dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_free_max,
180b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_pageout_stats_free_max, 0, "Not implemented");
181dc2efb27SJohn Dyson 
18238efa82bSJohn Dyson #if defined(NO_SWAPPING)
183ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
184ceb0cf87SJohn Dyson 	CTLFLAG_RD, &vm_swap_enabled, 0, "");
185ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
186ceb0cf87SJohn Dyson 	CTLFLAG_RD, &vm_swap_idle_enabled, 0, "");
18738efa82bSJohn Dyson #else
188ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
189b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout");
190ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
191b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
19238efa82bSJohn Dyson #endif
19326f9a767SRodney W. Grimes 
194ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts,
195b0359e2cSPeter Wemm 	CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem");
19612ac6a1dSJohn Dyson 
197ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts,
198b0359e2cSPeter Wemm 	CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages");
19912ac6a1dSJohn Dyson 
20023b59018SMatthew Dillon static int pageout_lock_miss;
20123b59018SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss,
20223b59018SMatthew Dillon 	CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout");
20323b59018SMatthew Dillon 
204ffc82b0aSJohn Dyson #define VM_PAGEOUT_PAGE_COUNT 16
205bbc0ec52SDavid Greenman int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT;
206df8bae1dSRodney W. Grimes 
207c3cb3e12SDavid Greenman int vm_page_max_wired;		/* XXX max # of wired pages system-wide */
208df8bae1dSRodney W. Grimes 
20938efa82bSJohn Dyson #if !defined(NO_SWAPPING)
210ecf6279fSAlan Cox static void vm_pageout_map_deactivate_pages(vm_map_t, long);
211ecf6279fSAlan Cox static void vm_pageout_object_deactivate_pages(pmap_t, vm_object_t, long);
21211caded3SAlfred Perlstein static void vm_req_vmdaemon(void);
21338efa82bSJohn Dyson #endif
214dc2efb27SJohn Dyson static void vm_pageout_page_stats(void);
215cd41fc12SDavid Greenman 
21626f9a767SRodney W. Grimes /*
21726f9a767SRodney W. Grimes  * vm_pageout_clean:
21824a1cce3SDavid Greenman  *
2190d94caffSDavid Greenman  * Clean the page and remove it from the laundry.
22026f9a767SRodney W. Grimes  *
2210d94caffSDavid Greenman  * We set the busy bit to cause potential page faults on this page to
2221c7c3c6aSMatthew Dillon  * block.  Note the careful timing, however, the busy bit isn't set till
2231c7c3c6aSMatthew Dillon  * late and we cannot do anything that will mess with the page.
22426f9a767SRodney W. Grimes  */
2253af76890SPoul-Henning Kamp static int
2268f9110f6SJohn Dyson vm_pageout_clean(m)
22724a1cce3SDavid Greenman 	vm_page_t m;
22824a1cce3SDavid Greenman {
22954d92145SMatthew Dillon 	vm_object_t object;
230f35329acSJohn Dyson 	vm_page_t mc[2*vm_pageout_page_count];
2313562af12SAlan Cox 	int pageout_count;
23290ecac61SMatthew Dillon 	int ib, is, page_base;
233a316d390SJohn Dyson 	vm_pindex_t pindex = m->pindex;
23426f9a767SRodney W. Grimes 
23555df3298SAlan Cox 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2363562af12SAlan Cox 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2370cddd8f0SMatthew Dillon 
23826f9a767SRodney W. Grimes 	/*
2391c7c3c6aSMatthew Dillon 	 * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP
2401c7c3c6aSMatthew Dillon 	 * with the new swapper, but we could have serious problems paging
2411c7c3c6aSMatthew Dillon 	 * out other object types if there is insufficient memory.
2421c7c3c6aSMatthew Dillon 	 *
2431c7c3c6aSMatthew Dillon 	 * Unfortunately, checking free memory here is far too late, so the
2441c7c3c6aSMatthew Dillon 	 * check has been moved up a procedural level.
2451c7c3c6aSMatthew Dillon 	 */
2461c7c3c6aSMatthew Dillon 
24724a1cce3SDavid Greenman 	/*
2488b03c8edSMatthew Dillon 	 * Don't mess with the page if it's busy, held, or special
24924a1cce3SDavid Greenman 	 */
2508f9110f6SJohn Dyson 	if ((m->hold_count != 0) ||
2513562af12SAlan Cox 	    ((m->busy != 0) || (m->flags & (PG_BUSY|PG_UNMANAGED)))) {
2520d94caffSDavid Greenman 		return 0;
2538b03c8edSMatthew Dillon 	}
2540d94caffSDavid Greenman 
255f35329acSJohn Dyson 	mc[vm_pageout_page_count] = m;
25626f9a767SRodney W. Grimes 	pageout_count = 1;
257f35329acSJohn Dyson 	page_base = vm_pageout_page_count;
25890ecac61SMatthew Dillon 	ib = 1;
25990ecac61SMatthew Dillon 	is = 1;
26090ecac61SMatthew Dillon 
26124a1cce3SDavid Greenman 	/*
26224a1cce3SDavid Greenman 	 * Scan object for clusterable pages.
26324a1cce3SDavid Greenman 	 *
26424a1cce3SDavid Greenman 	 * We can cluster ONLY if: ->> the page is NOT
26524a1cce3SDavid Greenman 	 * clean, wired, busy, held, or mapped into a
26624a1cce3SDavid Greenman 	 * buffer, and one of the following:
26724a1cce3SDavid Greenman 	 * 1) The page is inactive, or a seldom used
26824a1cce3SDavid Greenman 	 *    active page.
26924a1cce3SDavid Greenman 	 * -or-
27024a1cce3SDavid Greenman 	 * 2) we force the issue.
27190ecac61SMatthew Dillon 	 *
27290ecac61SMatthew Dillon 	 * During heavy mmap/modification loads the pageout
27390ecac61SMatthew Dillon 	 * daemon can really fragment the underlying file
27490ecac61SMatthew Dillon 	 * due to flushing pages out of order and not trying
27590ecac61SMatthew Dillon 	 * align the clusters (which leave sporatic out-of-order
27690ecac61SMatthew Dillon 	 * holes).  To solve this problem we do the reverse scan
27790ecac61SMatthew Dillon 	 * first and attempt to align our cluster, then do a
27890ecac61SMatthew Dillon 	 * forward scan if room remains.
27924a1cce3SDavid Greenman 	 */
2805163584cSAlan Cox 	object = m->object;
28190ecac61SMatthew Dillon more:
28290ecac61SMatthew Dillon 	while (ib && pageout_count < vm_pageout_page_count) {
28324a1cce3SDavid Greenman 		vm_page_t p;
284f6b04d2bSDavid Greenman 
28590ecac61SMatthew Dillon 		if (ib > pindex) {
28690ecac61SMatthew Dillon 			ib = 0;
28790ecac61SMatthew Dillon 			break;
288f6b04d2bSDavid Greenman 		}
28990ecac61SMatthew Dillon 
29090ecac61SMatthew Dillon 		if ((p = vm_page_lookup(object, pindex - ib)) == NULL) {
29190ecac61SMatthew Dillon 			ib = 0;
29290ecac61SMatthew Dillon 			break;
29390ecac61SMatthew Dillon 		}
2945070c7f8SJohn Dyson 		if (((p->queue - p->pc) == PQ_CACHE) ||
2958b03c8edSMatthew Dillon 		    (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) {
29690ecac61SMatthew Dillon 			ib = 0;
29790ecac61SMatthew Dillon 			break;
298f6b04d2bSDavid Greenman 		}
29924a1cce3SDavid Greenman 		vm_page_test_dirty(p);
30090ecac61SMatthew Dillon 		if ((p->dirty & p->valid) == 0 ||
30190ecac61SMatthew Dillon 		    p->queue != PQ_INACTIVE ||
30257601bcbSMatthew Dillon 		    p->wire_count != 0 ||	/* may be held by buf cache */
30357601bcbSMatthew Dillon 		    p->hold_count != 0) {	/* may be undergoing I/O */
30490ecac61SMatthew Dillon 			ib = 0;
30524a1cce3SDavid Greenman 			break;
306f6b04d2bSDavid Greenman 		}
30790ecac61SMatthew Dillon 		mc[--page_base] = p;
30890ecac61SMatthew Dillon 		++pageout_count;
30990ecac61SMatthew Dillon 		++ib;
31024a1cce3SDavid Greenman 		/*
31190ecac61SMatthew Dillon 		 * alignment boundry, stop here and switch directions.  Do
31290ecac61SMatthew Dillon 		 * not clear ib.
31324a1cce3SDavid Greenman 		 */
31490ecac61SMatthew Dillon 		if ((pindex - (ib - 1)) % vm_pageout_page_count == 0)
31590ecac61SMatthew Dillon 			break;
31624a1cce3SDavid Greenman 	}
31790ecac61SMatthew Dillon 
31890ecac61SMatthew Dillon 	while (pageout_count < vm_pageout_page_count &&
31990ecac61SMatthew Dillon 	    pindex + is < object->size) {
32090ecac61SMatthew Dillon 		vm_page_t p;
32190ecac61SMatthew Dillon 
32290ecac61SMatthew Dillon 		if ((p = vm_page_lookup(object, pindex + is)) == NULL)
32390ecac61SMatthew Dillon 			break;
3245070c7f8SJohn Dyson 		if (((p->queue - p->pc) == PQ_CACHE) ||
3258b03c8edSMatthew Dillon 		    (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) {
32690ecac61SMatthew Dillon 			break;
32724a1cce3SDavid Greenman 		}
32824a1cce3SDavid Greenman 		vm_page_test_dirty(p);
32990ecac61SMatthew Dillon 		if ((p->dirty & p->valid) == 0 ||
33090ecac61SMatthew Dillon 		    p->queue != PQ_INACTIVE ||
33157601bcbSMatthew Dillon 		    p->wire_count != 0 ||	/* may be held by buf cache */
33257601bcbSMatthew Dillon 		    p->hold_count != 0) {	/* may be undergoing I/O */
33324a1cce3SDavid Greenman 			break;
33424a1cce3SDavid Greenman 		}
33590ecac61SMatthew Dillon 		mc[page_base + pageout_count] = p;
33690ecac61SMatthew Dillon 		++pageout_count;
33790ecac61SMatthew Dillon 		++is;
33824a1cce3SDavid Greenman 	}
33990ecac61SMatthew Dillon 
34090ecac61SMatthew Dillon 	/*
34190ecac61SMatthew Dillon 	 * If we exhausted our forward scan, continue with the reverse scan
34290ecac61SMatthew Dillon 	 * when possible, even past a page boundry.  This catches boundry
34390ecac61SMatthew Dillon 	 * conditions.
34490ecac61SMatthew Dillon 	 */
34590ecac61SMatthew Dillon 	if (ib && pageout_count < vm_pageout_page_count)
34690ecac61SMatthew Dillon 		goto more;
347f6b04d2bSDavid Greenman 
34867bf6868SJohn Dyson 	/*
34967bf6868SJohn Dyson 	 * we allow reads during pageouts...
35067bf6868SJohn Dyson 	 */
3513562af12SAlan Cox 	return (vm_pageout_flush(&mc[page_base], pageout_count, 0, TRUE));
352aef922f5SJohn Dyson }
353aef922f5SJohn Dyson 
3541c7c3c6aSMatthew Dillon /*
3551c7c3c6aSMatthew Dillon  * vm_pageout_flush() - launder the given pages
3561c7c3c6aSMatthew Dillon  *
3571c7c3c6aSMatthew Dillon  *	The given pages are laundered.  Note that we setup for the start of
3581c7c3c6aSMatthew Dillon  *	I/O ( i.e. busy the page ), mark it read-only, and bump the object
3591c7c3c6aSMatthew Dillon  *	reference count all in here rather then in the parent.  If we want
3601c7c3c6aSMatthew Dillon  *	the parent to do more sophisticated things we may have to change
3611c7c3c6aSMatthew Dillon  *	the ordering.
3621c7c3c6aSMatthew Dillon  */
363aef922f5SJohn Dyson int
364b6e48e03SAlan Cox vm_pageout_flush(mc, count, flags, is_object_locked)
365aef922f5SJohn Dyson 	vm_page_t *mc;
366aef922f5SJohn Dyson 	int count;
3678f9110f6SJohn Dyson 	int flags;
368b6e48e03SAlan Cox 	int is_object_locked;
369aef922f5SJohn Dyson {
37054d92145SMatthew Dillon 	vm_object_t object;
371aef922f5SJohn Dyson 	int pageout_status[count];
37295461b45SJohn Dyson 	int numpagedout = 0;
373aef922f5SJohn Dyson 	int i;
374aef922f5SJohn Dyson 
37555df3298SAlan Cox 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3761c7c3c6aSMatthew Dillon 	/*
3771c7c3c6aSMatthew Dillon 	 * Initiate I/O.  Bump the vm_page_t->busy counter and
3781c7c3c6aSMatthew Dillon 	 * mark the pages read-only.
3791c7c3c6aSMatthew Dillon 	 *
3801c7c3c6aSMatthew Dillon 	 * We do not have to fixup the clean/dirty bits here... we can
3811c7c3c6aSMatthew Dillon 	 * allow the pager to do it after the I/O completes.
38202fa91d3SMatthew Dillon 	 *
38302fa91d3SMatthew Dillon 	 * NOTE! mc[i]->dirty may be partial or fragmented due to an
38402fa91d3SMatthew Dillon 	 * edge case with file fragments.
3851c7c3c6aSMatthew Dillon 	 */
3868f9110f6SJohn Dyson 	for (i = 0; i < count; i++) {
38702fa91d3SMatthew Dillon 		KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL, ("vm_pageout_flush page %p index %d/%d: partially invalid page", mc[i], i, count));
388e69763a3SDoug Rabson 		vm_page_io_start(mc[i]);
389a12cc0e4SAlan Cox 		pmap_page_protect(mc[i], VM_PROT_READ);
3908f9110f6SJohn Dyson 	}
391aef922f5SJohn Dyson 	object = mc[0]->object;
39255df3298SAlan Cox 	vm_page_unlock_queues();
393b6e48e03SAlan Cox 	if (!is_object_locked)
394d22bc710SAlan Cox 		VM_OBJECT_LOCK(object);
395d474eaaaSDoug Rabson 	vm_object_pip_add(object, count);
396d22bc710SAlan Cox 	VM_OBJECT_UNLOCK(object);
397aef922f5SJohn Dyson 
398aef922f5SJohn Dyson 	vm_pager_put_pages(object, mc, count,
39940bb4f4bSMatthew Dillon 	    (flags | ((object == kernel_object) ? VM_PAGER_PUT_SYNC : 0)),
40026f9a767SRodney W. Grimes 	    pageout_status);
40126f9a767SRodney W. Grimes 
40272ba747dSAlan Cox 	VM_OBJECT_LOCK(object);
40340eab1e9SAlan Cox 	vm_page_lock_queues();
404aef922f5SJohn Dyson 	for (i = 0; i < count; i++) {
405aef922f5SJohn Dyson 		vm_page_t mt = mc[i];
40624a1cce3SDavid Greenman 
40726f9a767SRodney W. Grimes 		switch (pageout_status[i]) {
40826f9a767SRodney W. Grimes 		case VM_PAGER_OK:
40926f9a767SRodney W. Grimes 		case VM_PAGER_PEND:
41095461b45SJohn Dyson 			numpagedout++;
41126f9a767SRodney W. Grimes 			break;
41226f9a767SRodney W. Grimes 		case VM_PAGER_BAD:
41326f9a767SRodney W. Grimes 			/*
4140d94caffSDavid Greenman 			 * Page outside of range of object. Right now we
4150d94caffSDavid Greenman 			 * essentially lose the changes by pretending it
4160d94caffSDavid Greenman 			 * worked.
41726f9a767SRodney W. Grimes 			 */
4180385347cSPeter Wemm 			pmap_clear_modify(mt);
41990ecac61SMatthew Dillon 			vm_page_undirty(mt);
42026f9a767SRodney W. Grimes 			break;
42126f9a767SRodney W. Grimes 		case VM_PAGER_ERROR:
42226f9a767SRodney W. Grimes 		case VM_PAGER_FAIL:
42326f9a767SRodney W. Grimes 			/*
4240d94caffSDavid Greenman 			 * If page couldn't be paged out, then reactivate the
4250d94caffSDavid Greenman 			 * page so it doesn't clog the inactive list.  (We
4260d94caffSDavid Greenman 			 * will try paging out it again later).
42726f9a767SRodney W. Grimes 			 */
42824a1cce3SDavid Greenman 			vm_page_activate(mt);
42926f9a767SRodney W. Grimes 			break;
43026f9a767SRodney W. Grimes 		case VM_PAGER_AGAIN:
43126f9a767SRodney W. Grimes 			break;
43226f9a767SRodney W. Grimes 		}
43326f9a767SRodney W. Grimes 
43426f9a767SRodney W. Grimes 		/*
4350d94caffSDavid Greenman 		 * If the operation is still going, leave the page busy to
4360d94caffSDavid Greenman 		 * block all other accesses. Also, leave the paging in
4370d94caffSDavid Greenman 		 * progress indicator set so that we don't attempt an object
4380d94caffSDavid Greenman 		 * collapse.
43926f9a767SRodney W. Grimes 		 */
44026f9a767SRodney W. Grimes 		if (pageout_status[i] != VM_PAGER_PEND) {
441f919ebdeSDavid Greenman 			vm_object_pip_wakeup(object);
442e69763a3SDoug Rabson 			vm_page_io_finish(mt);
443936524aaSMatthew Dillon 			if (!vm_page_count_severe() || !vm_page_try_to_cache(mt))
444a12cc0e4SAlan Cox 				pmap_page_protect(mt, VM_PROT_READ);
44526f9a767SRodney W. Grimes 		}
44626f9a767SRodney W. Grimes 	}
447b6e48e03SAlan Cox 	if (!is_object_locked)
44872ba747dSAlan Cox 		VM_OBJECT_UNLOCK(object);
44995461b45SJohn Dyson 	return numpagedout;
45026f9a767SRodney W. Grimes }
45126f9a767SRodney W. Grimes 
45238efa82bSJohn Dyson #if !defined(NO_SWAPPING)
45326f9a767SRodney W. Grimes /*
45426f9a767SRodney W. Grimes  *	vm_pageout_object_deactivate_pages
45526f9a767SRodney W. Grimes  *
45626f9a767SRodney W. Grimes  *	deactivate enough pages to satisfy the inactive target
45726f9a767SRodney W. Grimes  *	requirements or if vm_page_proc_limit is set, then
45826f9a767SRodney W. Grimes  *	deactivate all of the pages in the object and its
45924a1cce3SDavid Greenman  *	backing_objects.
46026f9a767SRodney W. Grimes  *
46126f9a767SRodney W. Grimes  *	The object and map must be locked.
46226f9a767SRodney W. Grimes  */
46338efa82bSJohn Dyson static void
464ecf6279fSAlan Cox vm_pageout_object_deactivate_pages(pmap, first_object, desired)
465ecf6279fSAlan Cox 	pmap_t pmap;
466ecf6279fSAlan Cox 	vm_object_t first_object;
467ecf6279fSAlan Cox 	long desired;
46826f9a767SRodney W. Grimes {
469ecf6279fSAlan Cox 	vm_object_t backing_object, object;
47054d92145SMatthew Dillon 	vm_page_t p, next;
471ce18aebdSAlan Cox 	int actcount, rcount, remove_mode;
47226f9a767SRodney W. Grimes 
473ecf6279fSAlan Cox 	VM_OBJECT_LOCK_ASSERT(first_object, MA_OWNED);
474ecf6279fSAlan Cox 	if (first_object->type == OBJT_DEVICE || first_object->type == OBJT_PHYS)
47538efa82bSJohn Dyson 		return;
476ecf6279fSAlan Cox 	for (object = first_object;; object = backing_object) {
477ecf6279fSAlan Cox 		if (pmap_resident_count(pmap) <= desired)
478ecf6279fSAlan Cox 			goto unlock_return;
47924a1cce3SDavid Greenman 		if (object->paging_in_progress)
480ecf6279fSAlan Cox 			goto unlock_return;
48126f9a767SRodney W. Grimes 
48285b1dc89SAlan Cox 		remove_mode = 0;
48338efa82bSJohn Dyson 		if (object->shadow_count > 1)
48438efa82bSJohn Dyson 			remove_mode = 1;
48526f9a767SRodney W. Grimes 		/*
48626f9a767SRodney W. Grimes 		 * scan the objects entire memory queue
48726f9a767SRodney W. Grimes 		 */
48826f9a767SRodney W. Grimes 		rcount = object->resident_page_count;
489b18bfc3dSJohn Dyson 		p = TAILQ_FIRST(&object->memq);
490ce18aebdSAlan Cox 		vm_page_lock_queues();
49126f9a767SRodney W. Grimes 		while (p && (rcount-- > 0)) {
492ecf6279fSAlan Cox 			if (pmap_resident_count(pmap) <= desired) {
493ce18aebdSAlan Cox 				vm_page_unlock_queues();
494ecf6279fSAlan Cox 				goto unlock_return;
495ce18aebdSAlan Cox 			}
496b18bfc3dSJohn Dyson 			next = TAILQ_NEXT(p, listq);
497a58d1fa1SDavid Greenman 			cnt.v_pdpages++;
4980d94caffSDavid Greenman 			if (p->wire_count != 0 ||
4990d94caffSDavid Greenman 			    p->hold_count != 0 ||
5000d94caffSDavid Greenman 			    p->busy != 0 ||
5018b03c8edSMatthew Dillon 			    (p->flags & (PG_BUSY|PG_UNMANAGED)) ||
502ecf6279fSAlan Cox 			    !pmap_page_exists_quick(pmap, p)) {
5030d94caffSDavid Greenman 				p = next;
5040d94caffSDavid Greenman 				continue;
5050d94caffSDavid Greenman 			}
5060385347cSPeter Wemm 			actcount = pmap_ts_referenced(p);
5077e006499SJohn Dyson 			if (actcount) {
508e69763a3SDoug Rabson 				vm_page_flag_set(p, PG_REFERENCED);
509c8c4b40cSJohn Dyson 			} else if (p->flags & PG_REFERENCED) {
5107e006499SJohn Dyson 				actcount = 1;
511ef743ce6SJohn Dyson 			}
51238efa82bSJohn Dyson 			if ((p->queue != PQ_ACTIVE) &&
51338efa82bSJohn Dyson 				(p->flags & PG_REFERENCED)) {
514ef743ce6SJohn Dyson 				vm_page_activate(p);
5157e006499SJohn Dyson 				p->act_count += actcount;
516e69763a3SDoug Rabson 				vm_page_flag_clear(p, PG_REFERENCED);
517c8c4b40cSJohn Dyson 			} else if (p->queue == PQ_ACTIVE) {
518ef743ce6SJohn Dyson 				if ((p->flags & PG_REFERENCED) == 0) {
519c8c4b40cSJohn Dyson 					p->act_count -= min(p->act_count, ACT_DECLINE);
5202b6b0df7SMatthew Dillon 					if (!remove_mode && (vm_pageout_algorithm || (p->act_count == 0))) {
5214fec79beSAlan Cox 						pmap_remove_all(p);
52226f9a767SRodney W. Grimes 						vm_page_deactivate(p);
52326f9a767SRodney W. Grimes 					} else {
5246d03d577SMatthew Dillon 						vm_pageq_requeue(p);
525c8c4b40cSJohn Dyson 					}
526c8c4b40cSJohn Dyson 				} else {
527eaf13dd7SJohn Dyson 					vm_page_activate(p);
528e69763a3SDoug Rabson 					vm_page_flag_clear(p, PG_REFERENCED);
52938efa82bSJohn Dyson 					if (p->act_count < (ACT_MAX - ACT_ADVANCE))
53038efa82bSJohn Dyson 						p->act_count += ACT_ADVANCE;
5316d03d577SMatthew Dillon 					vm_pageq_requeue(p);
53226f9a767SRodney W. Grimes 				}
533bd7e5f99SJohn Dyson 			} else if (p->queue == PQ_INACTIVE) {
5344fec79beSAlan Cox 				pmap_remove_all(p);
53526f9a767SRodney W. Grimes 			}
53626f9a767SRodney W. Grimes 			p = next;
53726f9a767SRodney W. Grimes 		}
538ce18aebdSAlan Cox 		vm_page_unlock_queues();
539ecf6279fSAlan Cox 		if ((backing_object = object->backing_object) == NULL)
540ecf6279fSAlan Cox 			goto unlock_return;
541ecf6279fSAlan Cox 		VM_OBJECT_LOCK(backing_object);
542ecf6279fSAlan Cox 		if (object != first_object)
543ecf6279fSAlan Cox 			VM_OBJECT_UNLOCK(object);
54438efa82bSJohn Dyson 	}
545ecf6279fSAlan Cox unlock_return:
546ecf6279fSAlan Cox 	if (object != first_object)
547ecf6279fSAlan Cox 		VM_OBJECT_UNLOCK(object);
54826f9a767SRodney W. Grimes }
54926f9a767SRodney W. Grimes 
55026f9a767SRodney W. Grimes /*
55126f9a767SRodney W. Grimes  * deactivate some number of pages in a map, try to do it fairly, but
55226f9a767SRodney W. Grimes  * that is really hard to do.
55326f9a767SRodney W. Grimes  */
554cd41fc12SDavid Greenman static void
55538efa82bSJohn Dyson vm_pageout_map_deactivate_pages(map, desired)
55626f9a767SRodney W. Grimes 	vm_map_t map;
557ecf6279fSAlan Cox 	long desired;
55826f9a767SRodney W. Grimes {
55926f9a767SRodney W. Grimes 	vm_map_entry_t tmpe;
56038efa82bSJohn Dyson 	vm_object_t obj, bigobj;
56130105b9eSTor Egge 	int nothingwired;
5620d94caffSDavid Greenman 
563d974f03cSAlan Cox 	if (!vm_map_trylock(map))
56426f9a767SRodney W. Grimes 		return;
56538efa82bSJohn Dyson 
56638efa82bSJohn Dyson 	bigobj = NULL;
56730105b9eSTor Egge 	nothingwired = TRUE;
56838efa82bSJohn Dyson 
56938efa82bSJohn Dyson 	/*
57038efa82bSJohn Dyson 	 * first, search out the biggest object, and try to free pages from
57138efa82bSJohn Dyson 	 * that.
57238efa82bSJohn Dyson 	 */
57326f9a767SRodney W. Grimes 	tmpe = map->header.next;
57438efa82bSJohn Dyson 	while (tmpe != &map->header) {
5759fdfe602SMatthew Dillon 		if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
57638efa82bSJohn Dyson 			obj = tmpe->object.vm_object;
5770774dfb3SAlan Cox 			if (obj != NULL && VM_OBJECT_TRYLOCK(obj)) {
5780774dfb3SAlan Cox 				if (obj->shadow_count <= 1 &&
5790774dfb3SAlan Cox 				    (bigobj == NULL ||
5800774dfb3SAlan Cox 				     bigobj->resident_page_count < obj->resident_page_count)) {
5810774dfb3SAlan Cox 					if (bigobj != NULL)
5820774dfb3SAlan Cox 						VM_OBJECT_UNLOCK(bigobj);
58338efa82bSJohn Dyson 					bigobj = obj;
5840774dfb3SAlan Cox 				} else
5850774dfb3SAlan Cox 					VM_OBJECT_UNLOCK(obj);
58638efa82bSJohn Dyson 			}
58738efa82bSJohn Dyson 		}
58830105b9eSTor Egge 		if (tmpe->wired_count > 0)
58930105b9eSTor Egge 			nothingwired = FALSE;
59038efa82bSJohn Dyson 		tmpe = tmpe->next;
59138efa82bSJohn Dyson 	}
59238efa82bSJohn Dyson 
5930774dfb3SAlan Cox 	if (bigobj != NULL) {
594ecf6279fSAlan Cox 		vm_pageout_object_deactivate_pages(map->pmap, bigobj, desired);
5950774dfb3SAlan Cox 		VM_OBJECT_UNLOCK(bigobj);
5960774dfb3SAlan Cox 	}
59738efa82bSJohn Dyson 	/*
59838efa82bSJohn Dyson 	 * Next, hunt around for other pages to deactivate.  We actually
59938efa82bSJohn Dyson 	 * do this search sort of wrong -- .text first is not the best idea.
60038efa82bSJohn Dyson 	 */
60138efa82bSJohn Dyson 	tmpe = map->header.next;
60238efa82bSJohn Dyson 	while (tmpe != &map->header) {
603b1028ad1SLuoqi Chen 		if (pmap_resident_count(vm_map_pmap(map)) <= desired)
60438efa82bSJohn Dyson 			break;
6059fdfe602SMatthew Dillon 		if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
60638efa82bSJohn Dyson 			obj = tmpe->object.vm_object;
6070774dfb3SAlan Cox 			if (obj != NULL) {
6080774dfb3SAlan Cox 				VM_OBJECT_LOCK(obj);
609ecf6279fSAlan Cox 				vm_pageout_object_deactivate_pages(map->pmap, obj, desired);
6100774dfb3SAlan Cox 				VM_OBJECT_UNLOCK(obj);
6110774dfb3SAlan Cox 			}
61238efa82bSJohn Dyson 		}
61326f9a767SRodney W. Grimes 		tmpe = tmpe->next;
61438857e7fSAlan Cox 	}
61538efa82bSJohn Dyson 
61638efa82bSJohn Dyson 	/*
61738efa82bSJohn Dyson 	 * Remove all mappings if a process is swapped out, this will free page
61838efa82bSJohn Dyson 	 * table pages.
61938efa82bSJohn Dyson 	 */
62038857e7fSAlan Cox 	if (desired == 0 && nothingwired) {
6210774dfb3SAlan Cox 		GIANT_REQUIRED;
62238857e7fSAlan Cox 		vm_page_lock_queues();
62305ba50f5SJake Burkholder 		pmap_remove(vm_map_pmap(map), vm_map_min(map),
62405ba50f5SJake Burkholder 		    vm_map_max(map));
62538857e7fSAlan Cox 		vm_page_unlock_queues();
62638857e7fSAlan Cox 	}
62738efa82bSJohn Dyson 	vm_map_unlock(map);
62826f9a767SRodney W. Grimes }
629a1287949SEivind Eklund #endif		/* !defined(NO_SWAPPING) */
630df8bae1dSRodney W. Grimes 
6311c7c3c6aSMatthew Dillon /*
632c4a1d732SAlan Cox  * Warning! The page queue lock is released and reacquired.
6331c7c3c6aSMatthew Dillon  */
634ff2023a5SAlan Cox static void
635ba0208b9SAlan Cox vm_pageout_page_free(vm_page_t m)
636ba0208b9SAlan Cox {
6371c7c3c6aSMatthew Dillon 	vm_object_t object = m->object;
638925a3a41SJohn Dyson 
639ba0208b9SAlan Cox 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
640e69763a3SDoug Rabson 	vm_page_busy(m);
641c4a1d732SAlan Cox 	vm_page_unlock_queues();
642c4a1d732SAlan Cox 	/*
643c4a1d732SAlan Cox 	 * Avoid a lock order reversal.  The page must be busy.
644c4a1d732SAlan Cox 	 */
645c4a1d732SAlan Cox 	VM_OBJECT_LOCK(object);
646c4a1d732SAlan Cox 	vm_page_lock_queues();
6474fec79beSAlan Cox 	pmap_remove_all(m);
648925a3a41SJohn Dyson 	vm_page_free(m);
649c4a1d732SAlan Cox 	VM_OBJECT_UNLOCK(object);
650299018d3SAlan Cox 	cnt.v_dfree++;
651925a3a41SJohn Dyson }
652925a3a41SJohn Dyson 
653df8bae1dSRodney W. Grimes /*
654eea85e9bSAlan Cox  * This routine is very drastic, but can save the system
655eea85e9bSAlan Cox  * in a pinch.
656eea85e9bSAlan Cox  */
657eea85e9bSAlan Cox static void
658eea85e9bSAlan Cox vm_pageout_pmap_collect(void)
659eea85e9bSAlan Cox {
660eea85e9bSAlan Cox 	int i;
661eea85e9bSAlan Cox 	vm_page_t m;
662eea85e9bSAlan Cox 	static int warningdone;
663eea85e9bSAlan Cox 
664eea85e9bSAlan Cox 	if (pmap_pagedaemon_waken == 0)
665eea85e9bSAlan Cox 		return;
666eea85e9bSAlan Cox 	if (warningdone < 5) {
667eea85e9bSAlan Cox 		printf("collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
668eea85e9bSAlan Cox 		warningdone++;
669eea85e9bSAlan Cox 	}
670eea85e9bSAlan Cox 	vm_page_lock_queues();
671eea85e9bSAlan Cox 	for (i = 0; i < vm_page_array_size; i++) {
672eea85e9bSAlan Cox 		m = &vm_page_array[i];
673eea85e9bSAlan Cox 		if (m->wire_count || m->hold_count || m->busy ||
674eea85e9bSAlan Cox 		    (m->flags & (PG_BUSY | PG_UNMANAGED)))
675eea85e9bSAlan Cox 			continue;
676eea85e9bSAlan Cox 		pmap_remove_all(m);
677eea85e9bSAlan Cox 	}
678eea85e9bSAlan Cox 	vm_page_unlock_queues();
679eea85e9bSAlan Cox 	pmap_pagedaemon_waken = 0;
680eea85e9bSAlan Cox }
681eea85e9bSAlan Cox 
682eea85e9bSAlan Cox /*
683df8bae1dSRodney W. Grimes  *	vm_pageout_scan does the dirty work for the pageout daemon.
684df8bae1dSRodney W. Grimes  */
6852b6b0df7SMatthew Dillon static void
6862b6b0df7SMatthew Dillon vm_pageout_scan(int pass)
687df8bae1dSRodney W. Grimes {
688502ba6e4SJohn Dyson 	vm_page_t m, next;
689936524aaSMatthew Dillon 	struct vm_page marker;
6901c7c3c6aSMatthew Dillon 	int page_shortage, maxscan, pcount;
6911c7c3c6aSMatthew Dillon 	int addl_page_shortage, addl_page_shortage_init;
6925663e6deSDavid Greenman 	struct proc *p, *bigproc;
6935663e6deSDavid Greenman 	vm_offset_t size, bigsize;
694df8bae1dSRodney W. Grimes 	vm_object_t object;
6957e006499SJohn Dyson 	int actcount;
696f6b04d2bSDavid Greenman 	int vnodes_skipped = 0;
6972b6b0df7SMatthew Dillon 	int maxlaunder;
6981eeaa1e3SJohn Dyson 	int s;
699e602ba25SJulian Elischer 	struct thread *td;
7000d94caffSDavid Greenman 
7010cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
702df8bae1dSRodney W. Grimes 	/*
703855a310fSJeff Roberson 	 * Decrease registered cache sizes.
704855a310fSJeff Roberson 	 */
705855a310fSJeff Roberson 	EVENTHANDLER_INVOKE(vm_lowmem, 0);
706855a310fSJeff Roberson 	/*
707855a310fSJeff Roberson 	 * We do this explicitly after the caches have been drained above.
708855a310fSJeff Roberson 	 */
709855a310fSJeff Roberson 	uma_reclaim();
710855a310fSJeff Roberson 	/*
7115985940eSJohn Dyson 	 * Do whatever cleanup that the pmap code can.
7125985940eSJohn Dyson 	 */
713eea85e9bSAlan Cox 	vm_pageout_pmap_collect();
7145985940eSJohn Dyson 
715b0ef8c5fSAlan Cox 	addl_page_shortage_init = atomic_readandclear_int(&vm_pageout_deficit);
716b182ec9eSJohn Dyson 
7171c7c3c6aSMatthew Dillon 	/*
7181c7c3c6aSMatthew Dillon 	 * Calculate the number of pages we want to either free or move
7192b6b0df7SMatthew Dillon 	 * to the cache.
7201c7c3c6aSMatthew Dillon 	 */
7212b6b0df7SMatthew Dillon 	page_shortage = vm_paging_target() + addl_page_shortage_init;
7221c7c3c6aSMatthew Dillon 
7231c7c3c6aSMatthew Dillon 	/*
724936524aaSMatthew Dillon 	 * Initialize our marker
725936524aaSMatthew Dillon 	 */
726936524aaSMatthew Dillon 	bzero(&marker, sizeof(marker));
727936524aaSMatthew Dillon 	marker.flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER;
728936524aaSMatthew Dillon 	marker.queue = PQ_INACTIVE;
729936524aaSMatthew Dillon 	marker.wire_count = 1;
730936524aaSMatthew Dillon 
731936524aaSMatthew Dillon 	/*
7321c7c3c6aSMatthew Dillon 	 * Start scanning the inactive queue for pages we can move to the
7331c7c3c6aSMatthew Dillon 	 * cache or free.  The scan will stop when the target is reached or
734936524aaSMatthew Dillon 	 * we have scanned the entire inactive queue.  Note that m->act_count
735936524aaSMatthew Dillon 	 * is not used to form decisions for the inactive queue, only for the
736936524aaSMatthew Dillon 	 * active queue.
7372b6b0df7SMatthew Dillon 	 *
7382b6b0df7SMatthew Dillon 	 * maxlaunder limits the number of dirty pages we flush per scan.
7392b6b0df7SMatthew Dillon 	 * For most systems a smaller value (16 or 32) is more robust under
7402b6b0df7SMatthew Dillon 	 * extreme memory and disk pressure because any unnecessary writes
7412b6b0df7SMatthew Dillon 	 * to disk can result in extreme performance degredation.  However,
7422b6b0df7SMatthew Dillon 	 * systems with excessive dirty pages (especially when MAP_NOSYNC is
7432b6b0df7SMatthew Dillon 	 * used) will die horribly with limited laundering.  If the pageout
7442b6b0df7SMatthew Dillon 	 * daemon cannot clean enough pages in the first pass, we let it go
7452b6b0df7SMatthew Dillon 	 * all out in succeeding passes.
7461c7c3c6aSMatthew Dillon 	 */
7472b6b0df7SMatthew Dillon 	if ((maxlaunder = vm_max_launder) <= 1)
7482b6b0df7SMatthew Dillon 		maxlaunder = 1;
7492b6b0df7SMatthew Dillon 	if (pass)
7502b6b0df7SMatthew Dillon 		maxlaunder = 10000;
7513e1b578aSAlan Cox 	vm_page_lock_queues();
75267bf6868SJohn Dyson rescan0:
7531c7c3c6aSMatthew Dillon 	addl_page_shortage = addl_page_shortage_init;
754f6b04d2bSDavid Greenman 	maxscan = cnt.v_inactive_count;
7556d03d577SMatthew Dillon 
756be72f788SAlan Cox 	for (m = TAILQ_FIRST(&vm_page_queues[PQ_INACTIVE].pl);
7571c7c3c6aSMatthew Dillon 	     m != NULL && maxscan-- > 0 && page_shortage > 0;
758e929c00dSKirk McKusick 	     m = next) {
759df8bae1dSRodney W. Grimes 
760a58d1fa1SDavid Greenman 		cnt.v_pdpages++;
761b182ec9eSJohn Dyson 
762f35329acSJohn Dyson 		if (m->queue != PQ_INACTIVE) {
76367bf6868SJohn Dyson 			goto rescan0;
764f35329acSJohn Dyson 		}
765b182ec9eSJohn Dyson 
766b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(m, pageq);
767df8bae1dSRodney W. Grimes 
768936524aaSMatthew Dillon 		/*
769936524aaSMatthew Dillon 		 * skip marker pages
770936524aaSMatthew Dillon 		 */
771936524aaSMatthew Dillon 		if (m->flags & PG_MARKER)
772936524aaSMatthew Dillon 			continue;
773936524aaSMatthew Dillon 
77457601bcbSMatthew Dillon 		/*
77557601bcbSMatthew Dillon 		 * A held page may be undergoing I/O, so skip it.
77657601bcbSMatthew Dillon 		 */
777b182ec9eSJohn Dyson 		if (m->hold_count) {
7786d03d577SMatthew Dillon 			vm_pageq_requeue(m);
779b182ec9eSJohn Dyson 			addl_page_shortage++;
780b182ec9eSJohn Dyson 			continue;
781df8bae1dSRodney W. Grimes 		}
78226f9a767SRodney W. Grimes 		/*
783a1287949SEivind Eklund 		 * Don't mess with busy pages, keep in the front of the
784b18bfc3dSJohn Dyson 		 * queue, most likely are being paged out.
78526f9a767SRodney W. Grimes 		 */
786bd7e5f99SJohn Dyson 		if (m->busy || (m->flags & PG_BUSY)) {
787b182ec9eSJohn Dyson 			addl_page_shortage++;
78826f9a767SRodney W. Grimes 			continue;
78926f9a767SRodney W. Grimes 		}
790bd7e5f99SJohn Dyson 
7917e006499SJohn Dyson 		/*
7921c7c3c6aSMatthew Dillon 		 * If the object is not being used, we ignore previous
7931c7c3c6aSMatthew Dillon 		 * references.
7947e006499SJohn Dyson 		 */
7950d94caffSDavid Greenman 		if (m->object->ref_count == 0) {
796e69763a3SDoug Rabson 			vm_page_flag_clear(m, PG_REFERENCED);
7970385347cSPeter Wemm 			pmap_clear_reference(m);
7987e006499SJohn Dyson 
7997e006499SJohn Dyson 		/*
8001c7c3c6aSMatthew Dillon 		 * Otherwise, if the page has been referenced while in the
8011c7c3c6aSMatthew Dillon 		 * inactive queue, we bump the "activation count" upwards,
8021c7c3c6aSMatthew Dillon 		 * making it less likely that the page will be added back to
8031c7c3c6aSMatthew Dillon 		 * the inactive queue prematurely again.  Here we check the
8041c7c3c6aSMatthew Dillon 		 * page tables (or emulated bits, if any), given the upper
8051c7c3c6aSMatthew Dillon 		 * level VM system not knowing anything about existing
8061c7c3c6aSMatthew Dillon 		 * references.
8077e006499SJohn Dyson 		 */
808ef743ce6SJohn Dyson 		} else if (((m->flags & PG_REFERENCED) == 0) &&
8090385347cSPeter Wemm 			(actcount = pmap_ts_referenced(m))) {
810ef743ce6SJohn Dyson 			vm_page_activate(m);
8117e006499SJohn Dyson 			m->act_count += (actcount + ACT_ADVANCE);
812ef743ce6SJohn Dyson 			continue;
8132fe6e4d7SDavid Greenman 		}
814ef743ce6SJohn Dyson 
8157e006499SJohn Dyson 		/*
8161c7c3c6aSMatthew Dillon 		 * If the upper level VM system knows about any page
8171c7c3c6aSMatthew Dillon 		 * references, we activate the page.  We also set the
8181c7c3c6aSMatthew Dillon 		 * "activation count" higher than normal so that we will less
8191c7c3c6aSMatthew Dillon 		 * likely place pages back onto the inactive queue again.
8207e006499SJohn Dyson 		 */
821bd7e5f99SJohn Dyson 		if ((m->flags & PG_REFERENCED) != 0) {
822e69763a3SDoug Rabson 			vm_page_flag_clear(m, PG_REFERENCED);
8230385347cSPeter Wemm 			actcount = pmap_ts_referenced(m);
82426f9a767SRodney W. Grimes 			vm_page_activate(m);
8257e006499SJohn Dyson 			m->act_count += (actcount + ACT_ADVANCE + 1);
8260d94caffSDavid Greenman 			continue;
8270d94caffSDavid Greenman 		}
82867bf6868SJohn Dyson 
8297e006499SJohn Dyson 		/*
8301c7c3c6aSMatthew Dillon 		 * If the upper level VM system doesn't know anything about
8311c7c3c6aSMatthew Dillon 		 * the page being dirty, we have to check for it again.  As
8321c7c3c6aSMatthew Dillon 		 * far as the VM code knows, any partially dirty pages are
8331c7c3c6aSMatthew Dillon 		 * fully dirty.
8347e006499SJohn Dyson 		 */
835f6b04d2bSDavid Greenman 		if (m->dirty == 0) {
836bd7e5f99SJohn Dyson 			vm_page_test_dirty(m);
837427e99a0SAlexander Langer 		} else {
8387dbf82dcSMatthew Dillon 			vm_page_dirty(m);
83930dcfc09SJohn Dyson 		}
840ef743ce6SJohn Dyson 
8417e006499SJohn Dyson 		/*
8427e006499SJohn Dyson 		 * Invalid pages can be easily freed
8437e006499SJohn Dyson 		 */
8446d40c3d3SDavid Greenman 		if (m->valid == 0) {
845925a3a41SJohn Dyson 			vm_pageout_page_free(m);
8461c7c3c6aSMatthew Dillon 			--page_shortage;
8477e006499SJohn Dyson 
8487e006499SJohn Dyson 		/*
849936524aaSMatthew Dillon 		 * Clean pages can be placed onto the cache queue.  This
850936524aaSMatthew Dillon 		 * effectively frees them.
8517e006499SJohn Dyson 		 */
852bd7e5f99SJohn Dyson 		} else if (m->dirty == 0) {
853bd7e5f99SJohn Dyson 			vm_page_cache(m);
8541c7c3c6aSMatthew Dillon 			--page_shortage;
8552b6b0df7SMatthew Dillon 		} else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) {
8567e006499SJohn Dyson 			/*
8572b6b0df7SMatthew Dillon 			 * Dirty pages need to be paged out, but flushing
8582b6b0df7SMatthew Dillon 			 * a page is extremely expensive verses freeing
8592b6b0df7SMatthew Dillon 			 * a clean page.  Rather then artificially limiting
8602b6b0df7SMatthew Dillon 			 * the number of pages we can flush, we instead give
8612b6b0df7SMatthew Dillon 			 * dirty pages extra priority on the inactive queue
8622b6b0df7SMatthew Dillon 			 * by forcing them to be cycled through the queue
8632b6b0df7SMatthew Dillon 			 * twice before being flushed, after which the
8642b6b0df7SMatthew Dillon 			 * (now clean) page will cycle through once more
8652b6b0df7SMatthew Dillon 			 * before being freed.  This significantly extends
8662b6b0df7SMatthew Dillon 			 * the thrash point for a heavily loaded machine.
8677e006499SJohn Dyson 			 */
8682b6b0df7SMatthew Dillon 			vm_page_flag_set(m, PG_WINATCFLS);
8696d03d577SMatthew Dillon 			vm_pageq_requeue(m);
8700d94caffSDavid Greenman 		} else if (maxlaunder > 0) {
8712b6b0df7SMatthew Dillon 			/*
8722b6b0df7SMatthew Dillon 			 * We always want to try to flush some dirty pages if
8732b6b0df7SMatthew Dillon 			 * we encounter them, to keep the system stable.
8742b6b0df7SMatthew Dillon 			 * Normally this number is small, but under extreme
8752b6b0df7SMatthew Dillon 			 * pressure where there are insufficient clean pages
8762b6b0df7SMatthew Dillon 			 * on the inactive queue, we may have to go all out.
8772b6b0df7SMatthew Dillon 			 */
87812ac6a1dSJohn Dyson 			int swap_pageouts_ok;
879f6b04d2bSDavid Greenman 			struct vnode *vp = NULL;
880f2a2857bSKirk McKusick 			struct mount *mp;
8810d94caffSDavid Greenman 
8820d94caffSDavid Greenman 			object = m->object;
8833562af12SAlan Cox 			if (!VM_OBJECT_TRYLOCK(object))
8843562af12SAlan Cox 				continue;
88512ac6a1dSJohn Dyson 			if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) {
88612ac6a1dSJohn Dyson 				swap_pageouts_ok = 1;
88712ac6a1dSJohn Dyson 			} else {
88812ac6a1dSJohn Dyson 				swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts);
88912ac6a1dSJohn Dyson 				swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts &&
89090ecac61SMatthew Dillon 				vm_page_count_min());
89112ac6a1dSJohn Dyson 
89212ac6a1dSJohn Dyson 			}
89370111b90SJohn Dyson 
89470111b90SJohn Dyson 			/*
8951c7c3c6aSMatthew Dillon 			 * We don't bother paging objects that are "dead".
8961c7c3c6aSMatthew Dillon 			 * Those objects are in a "rundown" state.
89770111b90SJohn Dyson 			 */
89870111b90SJohn Dyson 			if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) {
8993562af12SAlan Cox 				VM_OBJECT_UNLOCK(object);
9006d03d577SMatthew Dillon 				vm_pageq_requeue(m);
90112ac6a1dSJohn Dyson 				continue;
90212ac6a1dSJohn Dyson 			}
90312ac6a1dSJohn Dyson 
9041c7c3c6aSMatthew Dillon 			/*
9052b6b0df7SMatthew Dillon 			 * The object is already known NOT to be dead.   It
9062b6b0df7SMatthew Dillon 			 * is possible for the vget() to block the whole
9072b6b0df7SMatthew Dillon 			 * pageout daemon, but the new low-memory handling
9082b6b0df7SMatthew Dillon 			 * code should prevent it.
9091c7c3c6aSMatthew Dillon 			 *
9102b6b0df7SMatthew Dillon 			 * The previous code skipped locked vnodes and, worse,
9112b6b0df7SMatthew Dillon 			 * reordered pages in the queue.  This results in
9122b6b0df7SMatthew Dillon 			 * completely non-deterministic operation and, on a
9132b6b0df7SMatthew Dillon 			 * busy system, can lead to extremely non-optimal
9142b6b0df7SMatthew Dillon 			 * pageouts.  For example, it can cause clean pages
9152b6b0df7SMatthew Dillon 			 * to be freed and dirty pages to be moved to the end
9162b6b0df7SMatthew Dillon 			 * of the queue.  Since dirty pages are also moved to
9172b6b0df7SMatthew Dillon 			 * the end of the queue once-cleaned, this gives
9182b6b0df7SMatthew Dillon 			 * way too large a weighting to defering the freeing
9192b6b0df7SMatthew Dillon 			 * of dirty pages.
9201c7c3c6aSMatthew Dillon 			 *
92123b59018SMatthew Dillon 			 * We can't wait forever for the vnode lock, we might
92223b59018SMatthew Dillon 			 * deadlock due to a vn_read() getting stuck in
92323b59018SMatthew Dillon 			 * vm_wait while holding this vnode.  We skip the
92423b59018SMatthew Dillon 			 * vnode if we can't get it in a reasonable amount
92523b59018SMatthew Dillon 			 * of time.
9261c7c3c6aSMatthew Dillon 			 */
9271c7c3c6aSMatthew Dillon 			if (object->type == OBJT_VNODE) {
92824a1cce3SDavid Greenman 				vp = object->handle;
929f2a2857bSKirk McKusick 				mp = NULL;
930f2a2857bSKirk McKusick 				if (vp->v_type == VREG)
931f2a2857bSKirk McKusick 					vn_start_write(vp, &mp, V_NOWAIT);
9323e1b578aSAlan Cox 				vm_page_unlock_queues();
9334b5f5531SAlan Cox 				VI_LOCK(vp);
9343562af12SAlan Cox 				VM_OBJECT_UNLOCK(object);
9354b5f5531SAlan Cox 				if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK |
9364b5f5531SAlan Cox 				    LK_TIMELOCK, curthread)) {
9373562af12SAlan Cox 					VM_OBJECT_LOCK(object);
9383e1b578aSAlan Cox 					vm_page_lock_queues();
93923b59018SMatthew Dillon 					++pageout_lock_miss;
940f2a2857bSKirk McKusick 					vn_finished_write(mp);
941aef922f5SJohn Dyson 					if (object->flags & OBJ_MIGHTBEDIRTY)
942925a3a41SJohn Dyson 						vnodes_skipped++;
9433562af12SAlan Cox 					VM_OBJECT_UNLOCK(object);
944b182ec9eSJohn Dyson 					continue;
94585a376ebSJohn Dyson 				}
9463562af12SAlan Cox 				VM_OBJECT_LOCK(object);
9473e1b578aSAlan Cox 				vm_page_lock_queues();
948f35329acSJohn Dyson 				/*
949936524aaSMatthew Dillon 				 * The page might have been moved to another
950936524aaSMatthew Dillon 				 * queue during potential blocking in vget()
951936524aaSMatthew Dillon 				 * above.  The page might have been freed and
952936524aaSMatthew Dillon 				 * reused for another vnode.  The object might
953936524aaSMatthew Dillon 				 * have been reused for another vnode.
954f35329acSJohn Dyson 				 */
955936524aaSMatthew Dillon 				if (m->queue != PQ_INACTIVE ||
956936524aaSMatthew Dillon 				    m->object != object ||
957936524aaSMatthew Dillon 				    object->handle != vp) {
958b182ec9eSJohn Dyson 					if (object->flags & OBJ_MIGHTBEDIRTY)
959925a3a41SJohn Dyson 						vnodes_skipped++;
9603562af12SAlan Cox 					goto unlock_and_continue;
961b182ec9eSJohn Dyson 				}
962b182ec9eSJohn Dyson 
963f35329acSJohn Dyson 				/*
964936524aaSMatthew Dillon 				 * The page may have been busied during the
965936524aaSMatthew Dillon 				 * blocking in vput();  We don't move the
966936524aaSMatthew Dillon 				 * page back onto the end of the queue so that
967936524aaSMatthew Dillon 				 * statistics are more correct if we don't.
968f35329acSJohn Dyson 				 */
969b182ec9eSJohn Dyson 				if (m->busy || (m->flags & PG_BUSY)) {
9703562af12SAlan Cox 					goto unlock_and_continue;
971b182ec9eSJohn Dyson 				}
972b182ec9eSJohn Dyson 
973f35329acSJohn Dyson 				/*
97457601bcbSMatthew Dillon 				 * If the page has become held it might
97557601bcbSMatthew Dillon 				 * be undergoing I/O, so skip it
976f35329acSJohn Dyson 				 */
977b182ec9eSJohn Dyson 				if (m->hold_count) {
9786d03d577SMatthew Dillon 					vm_pageq_requeue(m);
979b182ec9eSJohn Dyson 					if (object->flags & OBJ_MIGHTBEDIRTY)
980925a3a41SJohn Dyson 						vnodes_skipped++;
9813562af12SAlan Cox 					goto unlock_and_continue;
982f6b04d2bSDavid Greenman 				}
983f6b04d2bSDavid Greenman 			}
984f6b04d2bSDavid Greenman 
9850d94caffSDavid Greenman 			/*
9860d94caffSDavid Greenman 			 * If a page is dirty, then it is either being washed
9870d94caffSDavid Greenman 			 * (but not yet cleaned) or it is still in the
9880d94caffSDavid Greenman 			 * laundry.  If it is still in the laundry, then we
9892b6b0df7SMatthew Dillon 			 * start the cleaning operation.
990936524aaSMatthew Dillon 			 *
991936524aaSMatthew Dillon 			 * This operation may cluster, invalidating the 'next'
992936524aaSMatthew Dillon 			 * pointer.  To prevent an inordinate number of
993936524aaSMatthew Dillon 			 * restarts we use our marker to remember our place.
9942b6b0df7SMatthew Dillon 			 *
9952b6b0df7SMatthew Dillon 			 * decrement page_shortage on success to account for
9962b6b0df7SMatthew Dillon 			 * the (future) cleaned page.  Otherwise we could wind
9972b6b0df7SMatthew Dillon 			 * up laundering or cleaning too many pages.
9980d94caffSDavid Greenman 			 */
999936524aaSMatthew Dillon 			s = splvm();
1000936524aaSMatthew Dillon 			TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE].pl, m, &marker, pageq);
1001936524aaSMatthew Dillon 			splx(s);
10022b6b0df7SMatthew Dillon 			if (vm_pageout_clean(m) != 0) {
10032b6b0df7SMatthew Dillon 				--page_shortage;
1004936524aaSMatthew Dillon 				--maxlaunder;
10052b6b0df7SMatthew Dillon 			}
1006936524aaSMatthew Dillon 			s = splvm();
1007936524aaSMatthew Dillon 			next = TAILQ_NEXT(&marker, pageq);
1008936524aaSMatthew Dillon 			TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, &marker, pageq);
1009936524aaSMatthew Dillon 			splx(s);
10103562af12SAlan Cox unlock_and_continue:
1011f2a2857bSKirk McKusick 			if (vp) {
1012f6b04d2bSDavid Greenman 				vput(vp);
1013f2a2857bSKirk McKusick 				vn_finished_write(mp);
1014f2a2857bSKirk McKusick 			}
10153562af12SAlan Cox 			VM_OBJECT_UNLOCK(object);
10160d94caffSDavid Greenman 		}
1017df8bae1dSRodney W. Grimes 	}
101826f9a767SRodney W. Grimes 
1019df8bae1dSRodney W. Grimes 	/*
1020936524aaSMatthew Dillon 	 * Compute the number of pages we want to try to move from the
1021936524aaSMatthew Dillon 	 * active queue to the inactive queue.
10221c7c3c6aSMatthew Dillon 	 */
1023936524aaSMatthew Dillon 	page_shortage = vm_paging_target() +
1024936524aaSMatthew Dillon 		cnt.v_inactive_target - cnt.v_inactive_count;
1025b182ec9eSJohn Dyson 	page_shortage += addl_page_shortage;
10261c7c3c6aSMatthew Dillon 
10271c7c3c6aSMatthew Dillon 	/*
1028936524aaSMatthew Dillon 	 * Scan the active queue for things we can deactivate. We nominally
1029936524aaSMatthew Dillon 	 * track the per-page activity counter and use it to locate
1030936524aaSMatthew Dillon 	 * deactivation candidates.
10311c7c3c6aSMatthew Dillon 	 */
1032b18bfc3dSJohn Dyson 	pcount = cnt.v_active_count;
1033be72f788SAlan Cox 	m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
10341c7c3c6aSMatthew Dillon 
1035b18bfc3dSJohn Dyson 	while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) {
1036f35329acSJohn Dyson 
10377e006499SJohn Dyson 		/*
1038956f3135SPhilippe Charnier 		 * This is a consistency check, and should likely be a panic
10397e006499SJohn Dyson 		 * or warning.
10407e006499SJohn Dyson 		 */
1041f35329acSJohn Dyson 		if (m->queue != PQ_ACTIVE) {
104238efa82bSJohn Dyson 			break;
1043f35329acSJohn Dyson 		}
1044f35329acSJohn Dyson 
1045b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(m, pageq);
1046df8bae1dSRodney W. Grimes 		/*
104726f9a767SRodney W. Grimes 		 * Don't deactivate pages that are busy.
1048df8bae1dSRodney W. Grimes 		 */
1049a647a309SDavid Greenman 		if ((m->busy != 0) ||
10500d94caffSDavid Greenman 		    (m->flags & PG_BUSY) ||
1051f6b04d2bSDavid Greenman 		    (m->hold_count != 0)) {
10526d03d577SMatthew Dillon 			vm_pageq_requeue(m);
105326f9a767SRodney W. Grimes 			m = next;
105426f9a767SRodney W. Grimes 			continue;
1055df8bae1dSRodney W. Grimes 		}
1056b18bfc3dSJohn Dyson 
1057b18bfc3dSJohn Dyson 		/*
1058b18bfc3dSJohn Dyson 		 * The count for pagedaemon pages is done after checking the
1059956f3135SPhilippe Charnier 		 * page for eligibility...
1060b18bfc3dSJohn Dyson 		 */
1061b18bfc3dSJohn Dyson 		cnt.v_pdpages++;
1062ef743ce6SJohn Dyson 
10637e006499SJohn Dyson 		/*
10647e006499SJohn Dyson 		 * Check to see "how much" the page has been used.
10657e006499SJohn Dyson 		 */
10667e006499SJohn Dyson 		actcount = 0;
1067ef743ce6SJohn Dyson 		if (m->object->ref_count != 0) {
1068ef743ce6SJohn Dyson 			if (m->flags & PG_REFERENCED) {
10697e006499SJohn Dyson 				actcount += 1;
10700d94caffSDavid Greenman 			}
10710385347cSPeter Wemm 			actcount += pmap_ts_referenced(m);
10727e006499SJohn Dyson 			if (actcount) {
10737e006499SJohn Dyson 				m->act_count += ACT_ADVANCE + actcount;
107438efa82bSJohn Dyson 				if (m->act_count > ACT_MAX)
107538efa82bSJohn Dyson 					m->act_count = ACT_MAX;
107638efa82bSJohn Dyson 			}
1077b18bfc3dSJohn Dyson 		}
1078ef743ce6SJohn Dyson 
10797e006499SJohn Dyson 		/*
10807e006499SJohn Dyson 		 * Since we have "tested" this bit, we need to clear it now.
10817e006499SJohn Dyson 		 */
1082e69763a3SDoug Rabson 		vm_page_flag_clear(m, PG_REFERENCED);
1083ef743ce6SJohn Dyson 
10847e006499SJohn Dyson 		/*
10857e006499SJohn Dyson 		 * Only if an object is currently being used, do we use the
10867e006499SJohn Dyson 		 * page activation count stats.
10877e006499SJohn Dyson 		 */
10887e006499SJohn Dyson 		if (actcount && (m->object->ref_count != 0)) {
10896d03d577SMatthew Dillon 			vm_pageq_requeue(m);
109026f9a767SRodney W. Grimes 		} else {
109138efa82bSJohn Dyson 			m->act_count -= min(m->act_count, ACT_DECLINE);
10922b6b0df7SMatthew Dillon 			if (vm_pageout_algorithm ||
10932b6b0df7SMatthew Dillon 			    m->object->ref_count == 0 ||
10942b6b0df7SMatthew Dillon 			    m->act_count == 0) {
1095925a3a41SJohn Dyson 				page_shortage--;
1096d4a272dbSJohn Dyson 				if (m->object->ref_count == 0) {
10974fec79beSAlan Cox 					pmap_remove_all(m);
1098d4a272dbSJohn Dyson 					if (m->dirty == 0)
10990d94caffSDavid Greenman 						vm_page_cache(m);
1100d4a272dbSJohn Dyson 					else
1101d4a272dbSJohn Dyson 						vm_page_deactivate(m);
11020d94caffSDavid Greenman 				} else {
110326f9a767SRodney W. Grimes 					vm_page_deactivate(m);
1104df8bae1dSRodney W. Grimes 				}
110538efa82bSJohn Dyson 			} else {
11066d03d577SMatthew Dillon 				vm_pageq_requeue(m);
110738efa82bSJohn Dyson 			}
1108df8bae1dSRodney W. Grimes 		}
110926f9a767SRodney W. Grimes 		m = next;
111026f9a767SRodney W. Grimes 	}
1111f35329acSJohn Dyson 	s = splvm();
11121c7c3c6aSMatthew Dillon 
1113df8bae1dSRodney W. Grimes 	/*
11140d94caffSDavid Greenman 	 * We try to maintain some *really* free pages, this allows interrupt
11151c7c3c6aSMatthew Dillon 	 * code to be guaranteed space.  Since both cache and free queues
11161c7c3c6aSMatthew Dillon 	 * are considered basically 'free', moving pages from cache to free
11171c7c3c6aSMatthew Dillon 	 * does not effect other calculations.
1118df8bae1dSRodney W. Grimes 	 */
1119a1f6d91cSDavid Greenman 	while (cnt.v_free_count < cnt.v_free_reserved) {
11205070c7f8SJohn Dyson 		static int cache_rover = 0;
11216d03d577SMatthew Dillon 		m = vm_pageq_find(PQ_CACHE, cache_rover, FALSE);
11220d94caffSDavid Greenman 		if (!m)
11230d94caffSDavid Greenman 			break;
11248b03c8edSMatthew Dillon 		if ((m->flags & (PG_BUSY|PG_UNMANAGED)) ||
11258b03c8edSMatthew Dillon 		    m->busy ||
11268b03c8edSMatthew Dillon 		    m->hold_count ||
11278b03c8edSMatthew Dillon 		    m->wire_count) {
1128d044d7bfSMatthew Dillon #ifdef INVARIANTS
1129d044d7bfSMatthew Dillon 			printf("Warning: busy page %p found in cache\n", m);
1130d044d7bfSMatthew Dillon #endif
1131aaba53daSMatthew Dillon 			vm_page_deactivate(m);
1132aaba53daSMatthew Dillon 			continue;
1133aaba53daSMatthew Dillon 		}
11345070c7f8SJohn Dyson 		cache_rover = (cache_rover + PQ_PRIME2) & PQ_L2_MASK;
1135925a3a41SJohn Dyson 		vm_pageout_page_free(m);
113626f9a767SRodney W. Grimes 	}
1137f35329acSJohn Dyson 	splx(s);
11388ffc1519SAlan Cox 	vm_page_unlock_queues();
1139ceb0cf87SJohn Dyson #if !defined(NO_SWAPPING)
1140ceb0cf87SJohn Dyson 	/*
1141ceb0cf87SJohn Dyson 	 * Idle process swapout -- run once per second.
1142ceb0cf87SJohn Dyson 	 */
1143ceb0cf87SJohn Dyson 	if (vm_swap_idle_enabled) {
1144ceb0cf87SJohn Dyson 		static long lsec;
1145227ee8a1SPoul-Henning Kamp 		if (time_second != lsec) {
1146ceb0cf87SJohn Dyson 			vm_pageout_req_swapout |= VM_SWAP_IDLE;
1147ceb0cf87SJohn Dyson 			vm_req_vmdaemon();
1148227ee8a1SPoul-Henning Kamp 			lsec = time_second;
1149ceb0cf87SJohn Dyson 		}
1150ceb0cf87SJohn Dyson 	}
1151ceb0cf87SJohn Dyson #endif
1152ceb0cf87SJohn Dyson 
11535663e6deSDavid Greenman 	/*
1154f6b04d2bSDavid Greenman 	 * If we didn't get enough free pages, and we have skipped a vnode
11554c1f8ee9SDavid Greenman 	 * in a writeable object, wakeup the sync daemon.  And kick swapout
11564c1f8ee9SDavid Greenman 	 * if we did not get enough free pages.
1157f6b04d2bSDavid Greenman 	 */
115890ecac61SMatthew Dillon 	if (vm_paging_target() > 0) {
115990ecac61SMatthew Dillon 		if (vnodes_skipped && vm_page_count_min())
1160d50c1994SPeter Wemm 			(void) speedup_syncer();
116138efa82bSJohn Dyson #if !defined(NO_SWAPPING)
116290ecac61SMatthew Dillon 		if (vm_swap_enabled && vm_page_count_target()) {
11634c1f8ee9SDavid Greenman 			vm_req_vmdaemon();
1164ceb0cf87SJohn Dyson 			vm_pageout_req_swapout |= VM_SWAP_NORMAL;
11654c1f8ee9SDavid Greenman 		}
11665afce282SDavid Greenman #endif
11674c1f8ee9SDavid Greenman 	}
11684c1f8ee9SDavid Greenman 
1169f6b04d2bSDavid Greenman 	/*
1170e92686d0SDavid Schultz 	 * If we are critically low on one of RAM or swap and low on
1171e92686d0SDavid Schultz 	 * the other, kill the largest process.  However, we avoid
1172e92686d0SDavid Schultz 	 * doing this on the first pass in order to give ourselves a
1173e92686d0SDavid Schultz 	 * chance to flush out dirty vnode-backed pages and to allow
1174e92686d0SDavid Schultz 	 * active pages to be moved to the inactive queue and reclaimed.
11751c58e4e5SJohn Baldwin 	 *
11761c58e4e5SJohn Baldwin 	 * We keep the process bigproc locked once we find it to keep anyone
11771c58e4e5SJohn Baldwin 	 * from messing with it; however, there is a possibility of
11781c58e4e5SJohn Baldwin 	 * deadlock if process B is bigproc and one of it's child processes
11791c58e4e5SJohn Baldwin 	 * attempts to propagate a signal to B while we are waiting for A's
11801c58e4e5SJohn Baldwin 	 * lock while walking this list.  To avoid this, we don't block on
11811c58e4e5SJohn Baldwin 	 * the process lock but just skip a process if it is already locked.
11825663e6deSDavid Greenman 	 */
1183e92686d0SDavid Schultz 	if (pass != 0 &&
11848f60c087SPoul-Henning Kamp 	    ((swap_pager_avail < 64 && vm_page_count_min()) ||
1185e92686d0SDavid Schultz 	     (swap_pager_full && vm_paging_target() > 0))) {
11865663e6deSDavid Greenman 		bigproc = NULL;
11875663e6deSDavid Greenman 		bigsize = 0;
11881005a129SJohn Baldwin 		sx_slock(&allproc_lock);
1189e602ba25SJulian Elischer 		FOREACH_PROC_IN_SYSTEM(p) {
1190e602ba25SJulian Elischer 			int breakout;
11915663e6deSDavid Greenman 			/*
11921c58e4e5SJohn Baldwin 			 * If this process is already locked, skip it.
11931c58e4e5SJohn Baldwin 			 */
11941c58e4e5SJohn Baldwin 			if (PROC_TRYLOCK(p) == 0)
11951c58e4e5SJohn Baldwin 				continue;
11961c58e4e5SJohn Baldwin 			/*
1197f4cf2141SWes Peters 			 * If this is a system or protected process, skip it.
11985663e6deSDavid Greenman 			 */
1199ef6020d1SMike Silbersack 			if ((p->p_flag & P_SYSTEM) || (p->p_pid == 1) ||
1200f4cf2141SWes Peters 			    (p->p_flag & P_PROTECTED) ||
12018f60c087SPoul-Henning Kamp 			    ((p->p_pid < 48) && (swap_pager_avail != 0))) {
12028606d880SJohn Baldwin 				PROC_UNLOCK(p);
12035663e6deSDavid Greenman 				continue;
12045663e6deSDavid Greenman 			}
12055663e6deSDavid Greenman 			/*
12065663e6deSDavid Greenman 			 * if the process is in a non-running type state,
1207e602ba25SJulian Elischer 			 * don't touch it. Check all the threads individually.
12085663e6deSDavid Greenman 			 */
12099ed346baSBosko Milekic 			mtx_lock_spin(&sched_lock);
1210e602ba25SJulian Elischer 			breakout = 0;
1211e602ba25SJulian Elischer 			FOREACH_THREAD_IN_PROC(p, td) {
121271fad9fdSJulian Elischer 				if (!TD_ON_RUNQ(td) &&
121371fad9fdSJulian Elischer 				    !TD_IS_RUNNING(td) &&
121471fad9fdSJulian Elischer 				    !TD_IS_SLEEPING(td)) {
1215e602ba25SJulian Elischer 					breakout = 1;
1216e602ba25SJulian Elischer 					break;
1217e602ba25SJulian Elischer 				}
1218e602ba25SJulian Elischer 			}
1219e602ba25SJulian Elischer 			if (breakout) {
12209ed346baSBosko Milekic 				mtx_unlock_spin(&sched_lock);
12211c58e4e5SJohn Baldwin 				PROC_UNLOCK(p);
12225663e6deSDavid Greenman 				continue;
12235663e6deSDavid Greenman 			}
12249ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
12255663e6deSDavid Greenman 			/*
12265663e6deSDavid Greenman 			 * get the process size
12275663e6deSDavid Greenman 			 */
122872d97679SDavid Schultz 			if (!vm_map_trylock_read(&p->p_vmspace->vm_map)) {
122972d97679SDavid Schultz 				PROC_UNLOCK(p);
123072d97679SDavid Schultz 				continue;
123172d97679SDavid Schultz 			}
123272d97679SDavid Schultz 			size = vmspace_swap_count(p->p_vmspace);
123372d97679SDavid Schultz 			vm_map_unlock_read(&p->p_vmspace->vm_map);
123472d97679SDavid Schultz 			size += vmspace_resident_count(p->p_vmspace);
12355663e6deSDavid Greenman 			/*
12365663e6deSDavid Greenman 			 * if the this process is bigger than the biggest one
12375663e6deSDavid Greenman 			 * remember it.
12385663e6deSDavid Greenman 			 */
12395663e6deSDavid Greenman 			if (size > bigsize) {
12401c58e4e5SJohn Baldwin 				if (bigproc != NULL)
12411c58e4e5SJohn Baldwin 					PROC_UNLOCK(bigproc);
12425663e6deSDavid Greenman 				bigproc = p;
12435663e6deSDavid Greenman 				bigsize = size;
12441c58e4e5SJohn Baldwin 			} else
12451c58e4e5SJohn Baldwin 				PROC_UNLOCK(p);
12465663e6deSDavid Greenman 		}
12471005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
12485663e6deSDavid Greenman 		if (bigproc != NULL) {
1249b40ce416SJulian Elischer 			struct ksegrp *kg;
1250729b1e51SDavid Greenman 			killproc(bigproc, "out of swap space");
12519ed346baSBosko Milekic 			mtx_lock_spin(&sched_lock);
1252b40ce416SJulian Elischer 			FOREACH_KSEGRP_IN_PROC(bigproc, kg) {
1253b43179fbSJeff Roberson 				sched_nice(kg, PRIO_MIN); /* XXXKSE ??? */
1254b40ce416SJulian Elischer 			}
12559ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
12561c58e4e5SJohn Baldwin 			PROC_UNLOCK(bigproc);
125724a1cce3SDavid Greenman 			wakeup(&cnt.v_free_count);
12585663e6deSDavid Greenman 		}
12595663e6deSDavid Greenman 	}
126026f9a767SRodney W. Grimes }
126126f9a767SRodney W. Grimes 
1262dc2efb27SJohn Dyson /*
1263dc2efb27SJohn Dyson  * This routine tries to maintain the pseudo LRU active queue,
1264dc2efb27SJohn Dyson  * so that during long periods of time where there is no paging,
1265956f3135SPhilippe Charnier  * that some statistic accumulation still occurs.  This code
1266dc2efb27SJohn Dyson  * helps the situation where paging just starts to occur.
1267dc2efb27SJohn Dyson  */
1268dc2efb27SJohn Dyson static void
1269dc2efb27SJohn Dyson vm_pageout_page_stats()
1270dc2efb27SJohn Dyson {
1271dc2efb27SJohn Dyson 	vm_page_t m,next;
1272dc2efb27SJohn Dyson 	int pcount,tpcount;		/* Number of pages to check */
1273dc2efb27SJohn Dyson 	static int fullintervalcount = 0;
1274bef608bdSJohn Dyson 	int page_shortage;
127525db2c54SMatthew Dillon 	int s0;
1276bef608bdSJohn Dyson 
127790ecac61SMatthew Dillon 	page_shortage =
127890ecac61SMatthew Dillon 	    (cnt.v_inactive_target + cnt.v_cache_max + cnt.v_free_min) -
1279bef608bdSJohn Dyson 	    (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count);
128090ecac61SMatthew Dillon 
1281bef608bdSJohn Dyson 	if (page_shortage <= 0)
1282bef608bdSJohn Dyson 		return;
1283dc2efb27SJohn Dyson 
128425db2c54SMatthew Dillon 	s0 = splvm();
128548c0444cSAlan Cox 	vm_page_lock_queues();
1286dc2efb27SJohn Dyson 	pcount = cnt.v_active_count;
1287dc2efb27SJohn Dyson 	fullintervalcount += vm_pageout_stats_interval;
1288dc2efb27SJohn Dyson 	if (fullintervalcount < vm_pageout_full_stats_interval) {
1289dc2efb27SJohn Dyson 		tpcount = (vm_pageout_stats_max * cnt.v_active_count) / cnt.v_page_count;
1290dc2efb27SJohn Dyson 		if (pcount > tpcount)
1291dc2efb27SJohn Dyson 			pcount = tpcount;
1292883f3caaSMatthew Dillon 	} else {
1293883f3caaSMatthew Dillon 		fullintervalcount = 0;
1294dc2efb27SJohn Dyson 	}
1295dc2efb27SJohn Dyson 
1296be72f788SAlan Cox 	m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
1297dc2efb27SJohn Dyson 	while ((m != NULL) && (pcount-- > 0)) {
12987e006499SJohn Dyson 		int actcount;
1299dc2efb27SJohn Dyson 
1300dc2efb27SJohn Dyson 		if (m->queue != PQ_ACTIVE) {
1301dc2efb27SJohn Dyson 			break;
1302dc2efb27SJohn Dyson 		}
1303dc2efb27SJohn Dyson 
1304dc2efb27SJohn Dyson 		next = TAILQ_NEXT(m, pageq);
1305dc2efb27SJohn Dyson 		/*
1306dc2efb27SJohn Dyson 		 * Don't deactivate pages that are busy.
1307dc2efb27SJohn Dyson 		 */
1308dc2efb27SJohn Dyson 		if ((m->busy != 0) ||
1309dc2efb27SJohn Dyson 		    (m->flags & PG_BUSY) ||
1310dc2efb27SJohn Dyson 		    (m->hold_count != 0)) {
13116d03d577SMatthew Dillon 			vm_pageq_requeue(m);
1312dc2efb27SJohn Dyson 			m = next;
1313dc2efb27SJohn Dyson 			continue;
1314dc2efb27SJohn Dyson 		}
1315dc2efb27SJohn Dyson 
13167e006499SJohn Dyson 		actcount = 0;
1317dc2efb27SJohn Dyson 		if (m->flags & PG_REFERENCED) {
1318e69763a3SDoug Rabson 			vm_page_flag_clear(m, PG_REFERENCED);
13197e006499SJohn Dyson 			actcount += 1;
1320dc2efb27SJohn Dyson 		}
1321dc2efb27SJohn Dyson 
13220385347cSPeter Wemm 		actcount += pmap_ts_referenced(m);
13237e006499SJohn Dyson 		if (actcount) {
13247e006499SJohn Dyson 			m->act_count += ACT_ADVANCE + actcount;
1325dc2efb27SJohn Dyson 			if (m->act_count > ACT_MAX)
1326dc2efb27SJohn Dyson 				m->act_count = ACT_MAX;
13276d03d577SMatthew Dillon 			vm_pageq_requeue(m);
1328dc2efb27SJohn Dyson 		} else {
1329dc2efb27SJohn Dyson 			if (m->act_count == 0) {
13307e006499SJohn Dyson 				/*
13312b6b0df7SMatthew Dillon 				 * We turn off page access, so that we have
13322b6b0df7SMatthew Dillon 				 * more accurate RSS stats.  We don't do this
13332b6b0df7SMatthew Dillon 				 * in the normal page deactivation when the
13342b6b0df7SMatthew Dillon 				 * system is loaded VM wise, because the
13352b6b0df7SMatthew Dillon 				 * cost of the large number of page protect
13362b6b0df7SMatthew Dillon 				 * operations would be higher than the value
13372b6b0df7SMatthew Dillon 				 * of doing the operation.
13387e006499SJohn Dyson 				 */
13394fec79beSAlan Cox 				pmap_remove_all(m);
1340dc2efb27SJohn Dyson 				vm_page_deactivate(m);
1341dc2efb27SJohn Dyson 			} else {
1342dc2efb27SJohn Dyson 				m->act_count -= min(m->act_count, ACT_DECLINE);
13436d03d577SMatthew Dillon 				vm_pageq_requeue(m);
1344dc2efb27SJohn Dyson 			}
1345dc2efb27SJohn Dyson 		}
1346dc2efb27SJohn Dyson 
1347dc2efb27SJohn Dyson 		m = next;
1348dc2efb27SJohn Dyson 	}
134948c0444cSAlan Cox 	vm_page_unlock_queues();
135025db2c54SMatthew Dillon 	splx(s0);
1351dc2efb27SJohn Dyson }
1352dc2efb27SJohn Dyson 
1353df8bae1dSRodney W. Grimes /*
1354df8bae1dSRodney W. Grimes  *	vm_pageout is the high level pageout daemon.
1355df8bae1dSRodney W. Grimes  */
13562b14f991SJulian Elischer static void
135726f9a767SRodney W. Grimes vm_pageout()
1358df8bae1dSRodney W. Grimes {
13598b245767SAlan Cox 	int error, pass, s;
13600384fff8SJason Evans 
136169a78d46SJohn Baldwin 	mtx_lock(&Giant);
13620384fff8SJason Evans 
1363df8bae1dSRodney W. Grimes 	/*
1364df8bae1dSRodney W. Grimes 	 * Initialize some paging parameters.
1365df8bae1dSRodney W. Grimes 	 */
1366f6b04d2bSDavid Greenman 	cnt.v_interrupt_free_min = 2;
1367f35329acSJohn Dyson 	if (cnt.v_page_count < 2000)
1368f35329acSJohn Dyson 		vm_pageout_page_count = 8;
1369f6b04d2bSDavid Greenman 
137045ae1d91SAlan Cox 	/*
137145ae1d91SAlan Cox 	 * v_free_reserved needs to include enough for the largest
137245ae1d91SAlan Cox 	 * swap pager structures plus enough for any pv_entry structs
137345ae1d91SAlan Cox 	 * when paging.
137445ae1d91SAlan Cox 	 */
137545ae1d91SAlan Cox 	if (cnt.v_page_count > 1024)
137645ae1d91SAlan Cox 		cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200;
137745ae1d91SAlan Cox 	else
137845ae1d91SAlan Cox 		cnt.v_free_min = 4;
137945ae1d91SAlan Cox 	cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE +
138045ae1d91SAlan Cox 	    cnt.v_interrupt_free_min;
138145ae1d91SAlan Cox 	cnt.v_free_reserved = vm_pageout_page_count +
138245ae1d91SAlan Cox 	    cnt.v_pageout_free_min + (cnt.v_page_count / 768) + PQ_L2_SIZE;
138345ae1d91SAlan Cox 	cnt.v_free_severe = cnt.v_free_min / 2;
138445ae1d91SAlan Cox 	cnt.v_free_min += cnt.v_free_reserved;
138545ae1d91SAlan Cox 	cnt.v_free_severe += cnt.v_free_reserved;
138645ae1d91SAlan Cox 
1387ed74321bSDavid Greenman 	/*
13882b6b0df7SMatthew Dillon 	 * v_free_target and v_cache_min control pageout hysteresis.  Note
13892b6b0df7SMatthew Dillon 	 * that these are more a measure of the VM cache queue hysteresis
13902b6b0df7SMatthew Dillon 	 * then the VM free queue.  Specifically, v_free_target is the
13912b6b0df7SMatthew Dillon 	 * high water mark (free+cache pages).
13922b6b0df7SMatthew Dillon 	 *
13932b6b0df7SMatthew Dillon 	 * v_free_reserved + v_cache_min (mostly means v_cache_min) is the
13942b6b0df7SMatthew Dillon 	 * low water mark, while v_free_min is the stop.  v_cache_min must
13952b6b0df7SMatthew Dillon 	 * be big enough to handle memory needs while the pageout daemon
13962b6b0df7SMatthew Dillon 	 * is signalled and run to free more pages.
1397ed74321bSDavid Greenman 	 */
1398a15403deSJohn Dyson 	if (cnt.v_free_count > 6144)
13992b6b0df7SMatthew Dillon 		cnt.v_free_target = 4 * cnt.v_free_min + cnt.v_free_reserved;
1400a15403deSJohn Dyson 	else
1401a15403deSJohn Dyson 		cnt.v_free_target = 2 * cnt.v_free_min + cnt.v_free_reserved;
14026f2b142eSDavid Greenman 
1403a15403deSJohn Dyson 	if (cnt.v_free_count > 2048) {
1404a15403deSJohn Dyson 		cnt.v_cache_min = cnt.v_free_target;
1405a15403deSJohn Dyson 		cnt.v_cache_max = 2 * cnt.v_cache_min;
1406a15403deSJohn Dyson 		cnt.v_inactive_target = (3 * cnt.v_free_target) / 2;
14070d94caffSDavid Greenman 	} else {
14080d94caffSDavid Greenman 		cnt.v_cache_min = 0;
14090d94caffSDavid Greenman 		cnt.v_cache_max = 0;
14106f2b142eSDavid Greenman 		cnt.v_inactive_target = cnt.v_free_count / 4;
14110d94caffSDavid Greenman 	}
1412e47ed70bSJohn Dyson 	if (cnt.v_inactive_target > cnt.v_free_count / 3)
1413e47ed70bSJohn Dyson 		cnt.v_inactive_target = cnt.v_free_count / 3;
1414df8bae1dSRodney W. Grimes 
1415df8bae1dSRodney W. Grimes 	/* XXX does not really belong here */
1416df8bae1dSRodney W. Grimes 	if (vm_page_max_wired == 0)
1417df8bae1dSRodney W. Grimes 		vm_page_max_wired = cnt.v_free_count / 3;
1418df8bae1dSRodney W. Grimes 
1419dc2efb27SJohn Dyson 	if (vm_pageout_stats_max == 0)
1420dc2efb27SJohn Dyson 		vm_pageout_stats_max = cnt.v_free_target;
1421dc2efb27SJohn Dyson 
1422dc2efb27SJohn Dyson 	/*
1423dc2efb27SJohn Dyson 	 * Set interval in seconds for stats scan.
1424dc2efb27SJohn Dyson 	 */
1425dc2efb27SJohn Dyson 	if (vm_pageout_stats_interval == 0)
1426bef608bdSJohn Dyson 		vm_pageout_stats_interval = 5;
1427dc2efb27SJohn Dyson 	if (vm_pageout_full_stats_interval == 0)
1428dc2efb27SJohn Dyson 		vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4;
1429dc2efb27SJohn Dyson 
1430dc2efb27SJohn Dyson 	/*
1431dc2efb27SJohn Dyson 	 * Set maximum free per pass
1432dc2efb27SJohn Dyson 	 */
1433dc2efb27SJohn Dyson 	if (vm_pageout_stats_free_max == 0)
1434bef608bdSJohn Dyson 		vm_pageout_stats_free_max = 5;
1435dc2efb27SJohn Dyson 
143624a1cce3SDavid Greenman 	swap_pager_swap_init();
14372b6b0df7SMatthew Dillon 	pass = 0;
1438df8bae1dSRodney W. Grimes 	/*
14390d94caffSDavid Greenman 	 * The pageout daemon is never done, so loop forever.
1440df8bae1dSRodney W. Grimes 	 */
1441df8bae1dSRodney W. Grimes 	while (TRUE) {
14428b245767SAlan Cox 		s = splvm();
14438e1d8de5SAlan Cox 		vm_page_lock_queues();
1444936524aaSMatthew Dillon 		/*
1445936524aaSMatthew Dillon 		 * If we have enough free memory, wakeup waiters.  Do
1446936524aaSMatthew Dillon 		 * not clear vm_pages_needed until we reach our target,
1447936524aaSMatthew Dillon 		 * otherwise we may be woken up over and over again and
1448936524aaSMatthew Dillon 		 * waste a lot of cpu.
1449936524aaSMatthew Dillon 		 */
1450936524aaSMatthew Dillon 		if (vm_pages_needed && !vm_page_count_min()) {
1451a1c0a785SAlan Cox 			if (!vm_paging_needed())
1452936524aaSMatthew Dillon 				vm_pages_needed = 0;
1453936524aaSMatthew Dillon 			wakeup(&cnt.v_free_count);
1454936524aaSMatthew Dillon 		}
1455936524aaSMatthew Dillon 		if (vm_pages_needed) {
145690ecac61SMatthew Dillon 			/*
14572b6b0df7SMatthew Dillon 			 * Still not done, take a second pass without waiting
14582b6b0df7SMatthew Dillon 			 * (unlimited dirty cleaning), otherwise sleep a bit
14592b6b0df7SMatthew Dillon 			 * and try again.
146090ecac61SMatthew Dillon 			 */
14612b6b0df7SMatthew Dillon 			++pass;
14622b6b0df7SMatthew Dillon 			if (pass > 1)
14638e1d8de5SAlan Cox 				msleep(&vm_pages_needed, &vm_page_queue_mtx, PVM,
146423955314SAlfred Perlstein 				       "psleep", hz/2);
146590ecac61SMatthew Dillon 		} else {
146690ecac61SMatthew Dillon 			/*
14672b6b0df7SMatthew Dillon 			 * Good enough, sleep & handle stats.  Prime the pass
14682b6b0df7SMatthew Dillon 			 * for the next run.
146990ecac61SMatthew Dillon 			 */
14702b6b0df7SMatthew Dillon 			if (pass > 1)
14712b6b0df7SMatthew Dillon 				pass = 1;
14722b6b0df7SMatthew Dillon 			else
14732b6b0df7SMatthew Dillon 				pass = 0;
14748e1d8de5SAlan Cox 			error = msleep(&vm_pages_needed, &vm_page_queue_mtx, PVM,
14750cddd8f0SMatthew Dillon 				    "psleep", vm_pageout_stats_interval * hz);
1476dc2efb27SJohn Dyson 			if (error && !vm_pages_needed) {
14778e1d8de5SAlan Cox 				vm_page_unlock_queues();
1478dc2efb27SJohn Dyson 				splx(s);
14792b6b0df7SMatthew Dillon 				pass = 0;
1480dc2efb27SJohn Dyson 				vm_pageout_page_stats();
1481dc2efb27SJohn Dyson 				continue;
1482dc2efb27SJohn Dyson 			}
1483f919ebdeSDavid Greenman 		}
1484b18bfc3dSJohn Dyson 		if (vm_pages_needed)
1485b18bfc3dSJohn Dyson 			cnt.v_pdwakeups++;
14868e1d8de5SAlan Cox 		vm_page_unlock_queues();
1487f919ebdeSDavid Greenman 		splx(s);
14882b6b0df7SMatthew Dillon 		vm_pageout_scan(pass);
1489df8bae1dSRodney W. Grimes 	}
1490df8bae1dSRodney W. Grimes }
149126f9a767SRodney W. Grimes 
14926b4b77adSAlan Cox /*
14936b4b77adSAlan Cox  * Unless the page queue lock is held by the caller, this function
14946b4b77adSAlan Cox  * should be regarded as advisory.  Specifically, the caller should
14956b4b77adSAlan Cox  * not msleep() on &cnt.v_free_count following this function unless
14966b4b77adSAlan Cox  * the page queue lock is held until the msleep() is performed.
14976b4b77adSAlan Cox  */
1498e0c5a895SJohn Dyson void
1499e0c5a895SJohn Dyson pagedaemon_wakeup()
1500e0c5a895SJohn Dyson {
1501a1c0a785SAlan Cox 
1502b40ce416SJulian Elischer 	if (!vm_pages_needed && curthread->td_proc != pageproc) {
1503a1c0a785SAlan Cox 		vm_pages_needed = 1;
1504e0c5a895SJohn Dyson 		wakeup(&vm_pages_needed);
1505e0c5a895SJohn Dyson 	}
1506e0c5a895SJohn Dyson }
1507e0c5a895SJohn Dyson 
150838efa82bSJohn Dyson #if !defined(NO_SWAPPING)
15095afce282SDavid Greenman static void
15105afce282SDavid Greenman vm_req_vmdaemon()
15115afce282SDavid Greenman {
15125afce282SDavid Greenman 	static int lastrun = 0;
15135afce282SDavid Greenman 
1514b18bfc3dSJohn Dyson 	if ((ticks > (lastrun + hz)) || (ticks < lastrun)) {
15155afce282SDavid Greenman 		wakeup(&vm_daemon_needed);
15165afce282SDavid Greenman 		lastrun = ticks;
15175afce282SDavid Greenman 	}
15185afce282SDavid Greenman }
15195afce282SDavid Greenman 
15202b14f991SJulian Elischer static void
15214f9fb771SBruce Evans vm_daemon()
15220d94caffSDavid Greenman {
15232fe6e4d7SDavid Greenman 	struct proc *p;
1524e602ba25SJulian Elischer 	int breakout;
1525e602ba25SJulian Elischer 	struct thread *td;
15260d94caffSDavid Greenman 
15273614c6fcSJohn Baldwin 	mtx_lock(&Giant);
15282fe6e4d7SDavid Greenman 	while (TRUE) {
15290cddd8f0SMatthew Dillon 		tsleep(&vm_daemon_needed, PPAUSE, "psleep", 0);
15304c1f8ee9SDavid Greenman 		if (vm_pageout_req_swapout) {
1531ceb0cf87SJohn Dyson 			swapout_procs(vm_pageout_req_swapout);
15324c1f8ee9SDavid Greenman 			vm_pageout_req_swapout = 0;
15334c1f8ee9SDavid Greenman 		}
15342fe6e4d7SDavid Greenman 		/*
15350d94caffSDavid Greenman 		 * scan the processes for exceeding their rlimits or if
15360d94caffSDavid Greenman 		 * process is swapped out -- deactivate pages
15372fe6e4d7SDavid Greenman 		 */
15381005a129SJohn Baldwin 		sx_slock(&allproc_lock);
1539fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
1540fe2144fdSLuoqi Chen 			vm_pindex_t limit, size;
15412fe6e4d7SDavid Greenman 
15422fe6e4d7SDavid Greenman 			/*
15432fe6e4d7SDavid Greenman 			 * if this is a system process or if we have already
15442fe6e4d7SDavid Greenman 			 * looked at this process, skip it.
15452fe6e4d7SDavid Greenman 			 */
1546897ecacdSJohn Baldwin 			PROC_LOCK(p);
15472fe6e4d7SDavid Greenman 			if (p->p_flag & (P_SYSTEM | P_WEXIT)) {
1548897ecacdSJohn Baldwin 				PROC_UNLOCK(p);
15492fe6e4d7SDavid Greenman 				continue;
15502fe6e4d7SDavid Greenman 			}
15512fe6e4d7SDavid Greenman 			/*
15522fe6e4d7SDavid Greenman 			 * if the process is in a non-running type state,
15532fe6e4d7SDavid Greenman 			 * don't touch it.
15542fe6e4d7SDavid Greenman 			 */
15559ed346baSBosko Milekic 			mtx_lock_spin(&sched_lock);
1556e602ba25SJulian Elischer 			breakout = 0;
1557e602ba25SJulian Elischer 			FOREACH_THREAD_IN_PROC(p, td) {
155871fad9fdSJulian Elischer 				if (!TD_ON_RUNQ(td) &&
155971fad9fdSJulian Elischer 				    !TD_IS_RUNNING(td) &&
156071fad9fdSJulian Elischer 				    !TD_IS_SLEEPING(td)) {
1561e602ba25SJulian Elischer 					breakout = 1;
1562e602ba25SJulian Elischer 					break;
1563e602ba25SJulian Elischer 				}
1564e602ba25SJulian Elischer 			}
15659ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
1566897ecacdSJohn Baldwin 			if (breakout) {
1567897ecacdSJohn Baldwin 				PROC_UNLOCK(p);
15682fe6e4d7SDavid Greenman 				continue;
15692fe6e4d7SDavid Greenman 			}
15702fe6e4d7SDavid Greenman 			/*
15712fe6e4d7SDavid Greenman 			 * get a limit
15722fe6e4d7SDavid Greenman 			 */
1573fe2144fdSLuoqi Chen 			limit = OFF_TO_IDX(
1574fe2144fdSLuoqi Chen 			    qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur,
1575fe2144fdSLuoqi Chen 				p->p_rlimit[RLIMIT_RSS].rlim_max));
15762fe6e4d7SDavid Greenman 
15772fe6e4d7SDavid Greenman 			/*
15780d94caffSDavid Greenman 			 * let processes that are swapped out really be
15790d94caffSDavid Greenman 			 * swapped out set the limit to nothing (will force a
15800d94caffSDavid Greenman 			 * swap-out.)
15812fe6e4d7SDavid Greenman 			 */
15828606d880SJohn Baldwin 			if ((p->p_sflag & PS_INMEM) == 0)
15830d94caffSDavid Greenman 				limit = 0;	/* XXX */
1584897ecacdSJohn Baldwin 			PROC_UNLOCK(p);
15852fe6e4d7SDavid Greenman 
1586fe2144fdSLuoqi Chen 			size = vmspace_resident_count(p->p_vmspace);
15872fe6e4d7SDavid Greenman 			if (limit >= 0 && size >= limit) {
1588fe2144fdSLuoqi Chen 				vm_pageout_map_deactivate_pages(
1589fe2144fdSLuoqi Chen 				    &p->p_vmspace->vm_map, limit);
15902fe6e4d7SDavid Greenman 			}
15912fe6e4d7SDavid Greenman 		}
15921005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
159324a1cce3SDavid Greenman 	}
15942fe6e4d7SDavid Greenman }
1595a1287949SEivind Eklund #endif			/* !defined(NO_SWAPPING) */
1596