xref: /freebsd/sys/vm/vm_pageout.c (revision dcbcd518e0df84c367a755a3fe5b420948b1ebdf)
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);
114eea85e9bSAlan Cox static void vm_pageout_pmap_collect(void);
11511caded3SAlfred Perlstein static void vm_pageout_scan(int pass);
11645ae1d91SAlan Cox 
1172b14f991SJulian Elischer struct proc *pageproc;
1182b14f991SJulian Elischer 
1192b14f991SJulian Elischer static struct kproc_desc page_kp = {
1202b14f991SJulian Elischer 	"pagedaemon",
1212b14f991SJulian Elischer 	vm_pageout,
1222b14f991SJulian Elischer 	&pageproc
1232b14f991SJulian Elischer };
1249c8b8baaSPeter Wemm SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, &page_kp)
1252b14f991SJulian Elischer 
12638efa82bSJohn Dyson #if !defined(NO_SWAPPING)
1272b14f991SJulian Elischer /* the kernel process "vm_daemon"*/
12811caded3SAlfred Perlstein static void vm_daemon(void);
129f708ef1bSPoul-Henning Kamp static struct	proc *vmproc;
1302b14f991SJulian Elischer 
1312b14f991SJulian Elischer static struct kproc_desc vm_kp = {
1322b14f991SJulian Elischer 	"vmdaemon",
1332b14f991SJulian Elischer 	vm_daemon,
1342b14f991SJulian Elischer 	&vmproc
1352b14f991SJulian Elischer };
1369c8b8baaSPeter Wemm SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp)
13738efa82bSJohn Dyson #endif
1382b14f991SJulian Elischer 
1392b14f991SJulian Elischer 
1408b245767SAlan Cox int vm_pages_needed;		/* Event on which pageout daemon sleeps */
1418b245767SAlan Cox int vm_pageout_deficit;		/* Estimated number of pages deficit */
1428b245767SAlan Cox int vm_pageout_pages_needed;	/* flag saying that the pageout daemon needs pages */
14326f9a767SRodney W. Grimes 
14438efa82bSJohn Dyson #if !defined(NO_SWAPPING)
145f708ef1bSPoul-Henning Kamp static int vm_pageout_req_swapout;	/* XXX */
146f708ef1bSPoul-Henning Kamp static int vm_daemon_needed;
14738efa82bSJohn Dyson #endif
1482b6b0df7SMatthew Dillon static int vm_max_launder = 32;
149303b270bSEivind Eklund static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0;
150303b270bSEivind Eklund static int vm_pageout_full_stats_interval = 0;
1512b6b0df7SMatthew Dillon static int vm_pageout_stats_free_max=0, vm_pageout_algorithm=0;
152303b270bSEivind Eklund static int defer_swap_pageouts=0;
153303b270bSEivind Eklund static int disable_swap_pageouts=0;
15470111b90SJohn Dyson 
15538efa82bSJohn Dyson #if defined(NO_SWAPPING)
156303b270bSEivind Eklund static int vm_swap_enabled=0;
157303b270bSEivind Eklund static int vm_swap_idle_enabled=0;
15838efa82bSJohn Dyson #else
159303b270bSEivind Eklund static int vm_swap_enabled=1;
160303b270bSEivind Eklund static int vm_swap_idle_enabled=0;
16138efa82bSJohn Dyson #endif
16238efa82bSJohn Dyson 
16338efa82bSJohn Dyson SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm,
1642b6b0df7SMatthew Dillon 	CTLFLAG_RW, &vm_pageout_algorithm, 0, "LRU page mgmt");
1652b6b0df7SMatthew Dillon 
1662b6b0df7SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, max_launder,
1672b6b0df7SMatthew Dillon 	CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout");
16838efa82bSJohn Dyson 
169dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max,
170b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length");
171dc2efb27SJohn Dyson 
172dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval,
173b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan");
174dc2efb27SJohn Dyson 
175dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval,
176b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan");
177dc2efb27SJohn Dyson 
178dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_free_max,
179b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_pageout_stats_free_max, 0, "Not implemented");
180dc2efb27SJohn Dyson 
18138efa82bSJohn Dyson #if defined(NO_SWAPPING)
182ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
183ceb0cf87SJohn Dyson 	CTLFLAG_RD, &vm_swap_enabled, 0, "");
184ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
185ceb0cf87SJohn Dyson 	CTLFLAG_RD, &vm_swap_idle_enabled, 0, "");
18638efa82bSJohn Dyson #else
187ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
188b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout");
189ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
190b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
19138efa82bSJohn Dyson #endif
19226f9a767SRodney W. Grimes 
193ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts,
194b0359e2cSPeter Wemm 	CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem");
19512ac6a1dSJohn Dyson 
196ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts,
197b0359e2cSPeter Wemm 	CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages");
19812ac6a1dSJohn Dyson 
19923b59018SMatthew Dillon static int pageout_lock_miss;
20023b59018SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss,
20123b59018SMatthew Dillon 	CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout");
20223b59018SMatthew Dillon 
203ffc82b0aSJohn Dyson #define VM_PAGEOUT_PAGE_COUNT 16
204bbc0ec52SDavid Greenman int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT;
205df8bae1dSRodney W. Grimes 
206c3cb3e12SDavid Greenman int vm_page_max_wired;		/* XXX max # of wired pages system-wide */
207df8bae1dSRodney W. Grimes 
20838efa82bSJohn Dyson #if !defined(NO_SWAPPING)
209ecf6279fSAlan Cox static void vm_pageout_map_deactivate_pages(vm_map_t, long);
210ecf6279fSAlan Cox static void vm_pageout_object_deactivate_pages(pmap_t, vm_object_t, long);
21111caded3SAlfred Perlstein static void vm_req_vmdaemon(void);
21238efa82bSJohn Dyson #endif
213dc2efb27SJohn Dyson static void vm_pageout_page_stats(void);
214cd41fc12SDavid Greenman 
21526f9a767SRodney W. Grimes /*
21626f9a767SRodney W. Grimes  * vm_pageout_clean:
21724a1cce3SDavid Greenman  *
2180d94caffSDavid Greenman  * Clean the page and remove it from the laundry.
21926f9a767SRodney W. Grimes  *
2200d94caffSDavid Greenman  * We set the busy bit to cause potential page faults on this page to
2211c7c3c6aSMatthew Dillon  * block.  Note the careful timing, however, the busy bit isn't set till
2221c7c3c6aSMatthew Dillon  * late and we cannot do anything that will mess with the page.
22326f9a767SRodney W. Grimes  */
2243af76890SPoul-Henning Kamp static int
2258f9110f6SJohn Dyson vm_pageout_clean(m)
22624a1cce3SDavid Greenman 	vm_page_t m;
22724a1cce3SDavid Greenman {
22854d92145SMatthew Dillon 	vm_object_t object;
229f35329acSJohn Dyson 	vm_page_t mc[2*vm_pageout_page_count];
2303562af12SAlan Cox 	int pageout_count;
23190ecac61SMatthew Dillon 	int ib, is, page_base;
232a316d390SJohn Dyson 	vm_pindex_t pindex = m->pindex;
23326f9a767SRodney W. Grimes 
23455df3298SAlan Cox 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2353562af12SAlan Cox 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2360cddd8f0SMatthew Dillon 
23726f9a767SRodney W. Grimes 	/*
2381c7c3c6aSMatthew Dillon 	 * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP
2391c7c3c6aSMatthew Dillon 	 * with the new swapper, but we could have serious problems paging
2401c7c3c6aSMatthew Dillon 	 * out other object types if there is insufficient memory.
2411c7c3c6aSMatthew Dillon 	 *
2421c7c3c6aSMatthew Dillon 	 * Unfortunately, checking free memory here is far too late, so the
2431c7c3c6aSMatthew Dillon 	 * check has been moved up a procedural level.
2441c7c3c6aSMatthew Dillon 	 */
2451c7c3c6aSMatthew Dillon 
24624a1cce3SDavid Greenman 	/*
2478b03c8edSMatthew Dillon 	 * Don't mess with the page if it's busy, held, or special
24824a1cce3SDavid Greenman 	 */
2498f9110f6SJohn Dyson 	if ((m->hold_count != 0) ||
2503562af12SAlan Cox 	    ((m->busy != 0) || (m->flags & (PG_BUSY|PG_UNMANAGED)))) {
2510d94caffSDavid Greenman 		return 0;
2528b03c8edSMatthew Dillon 	}
2530d94caffSDavid Greenman 
254f35329acSJohn Dyson 	mc[vm_pageout_page_count] = m;
25526f9a767SRodney W. Grimes 	pageout_count = 1;
256f35329acSJohn Dyson 	page_base = vm_pageout_page_count;
25790ecac61SMatthew Dillon 	ib = 1;
25890ecac61SMatthew Dillon 	is = 1;
25990ecac61SMatthew Dillon 
26024a1cce3SDavid Greenman 	/*
26124a1cce3SDavid Greenman 	 * Scan object for clusterable pages.
26224a1cce3SDavid Greenman 	 *
26324a1cce3SDavid Greenman 	 * We can cluster ONLY if: ->> the page is NOT
26424a1cce3SDavid Greenman 	 * clean, wired, busy, held, or mapped into a
26524a1cce3SDavid Greenman 	 * buffer, and one of the following:
26624a1cce3SDavid Greenman 	 * 1) The page is inactive, or a seldom used
26724a1cce3SDavid Greenman 	 *    active page.
26824a1cce3SDavid Greenman 	 * -or-
26924a1cce3SDavid Greenman 	 * 2) we force the issue.
27090ecac61SMatthew Dillon 	 *
27190ecac61SMatthew Dillon 	 * During heavy mmap/modification loads the pageout
27290ecac61SMatthew Dillon 	 * daemon can really fragment the underlying file
27390ecac61SMatthew Dillon 	 * due to flushing pages out of order and not trying
27490ecac61SMatthew Dillon 	 * align the clusters (which leave sporatic out-of-order
27590ecac61SMatthew Dillon 	 * holes).  To solve this problem we do the reverse scan
27690ecac61SMatthew Dillon 	 * first and attempt to align our cluster, then do a
27790ecac61SMatthew Dillon 	 * forward scan if room remains.
27824a1cce3SDavid Greenman 	 */
2795163584cSAlan Cox 	object = m->object;
28090ecac61SMatthew Dillon more:
28190ecac61SMatthew Dillon 	while (ib && pageout_count < vm_pageout_page_count) {
28224a1cce3SDavid Greenman 		vm_page_t p;
283f6b04d2bSDavid Greenman 
28490ecac61SMatthew Dillon 		if (ib > pindex) {
28590ecac61SMatthew Dillon 			ib = 0;
28690ecac61SMatthew Dillon 			break;
287f6b04d2bSDavid Greenman 		}
28890ecac61SMatthew Dillon 
28990ecac61SMatthew Dillon 		if ((p = vm_page_lookup(object, pindex - ib)) == NULL) {
29090ecac61SMatthew Dillon 			ib = 0;
29190ecac61SMatthew Dillon 			break;
29290ecac61SMatthew Dillon 		}
2935070c7f8SJohn Dyson 		if (((p->queue - p->pc) == PQ_CACHE) ||
2948b03c8edSMatthew Dillon 		    (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) {
29590ecac61SMatthew Dillon 			ib = 0;
29690ecac61SMatthew Dillon 			break;
297f6b04d2bSDavid Greenman 		}
29824a1cce3SDavid Greenman 		vm_page_test_dirty(p);
29990ecac61SMatthew Dillon 		if ((p->dirty & p->valid) == 0 ||
30090ecac61SMatthew Dillon 		    p->queue != PQ_INACTIVE ||
30157601bcbSMatthew Dillon 		    p->wire_count != 0 ||	/* may be held by buf cache */
30257601bcbSMatthew Dillon 		    p->hold_count != 0) {	/* may be undergoing I/O */
30390ecac61SMatthew Dillon 			ib = 0;
30424a1cce3SDavid Greenman 			break;
305f6b04d2bSDavid Greenman 		}
30690ecac61SMatthew Dillon 		mc[--page_base] = p;
30790ecac61SMatthew Dillon 		++pageout_count;
30890ecac61SMatthew Dillon 		++ib;
30924a1cce3SDavid Greenman 		/*
31090ecac61SMatthew Dillon 		 * alignment boundry, stop here and switch directions.  Do
31190ecac61SMatthew Dillon 		 * not clear ib.
31224a1cce3SDavid Greenman 		 */
31390ecac61SMatthew Dillon 		if ((pindex - (ib - 1)) % vm_pageout_page_count == 0)
31490ecac61SMatthew Dillon 			break;
31524a1cce3SDavid Greenman 	}
31690ecac61SMatthew Dillon 
31790ecac61SMatthew Dillon 	while (pageout_count < vm_pageout_page_count &&
31890ecac61SMatthew Dillon 	    pindex + is < object->size) {
31990ecac61SMatthew Dillon 		vm_page_t p;
32090ecac61SMatthew Dillon 
32190ecac61SMatthew Dillon 		if ((p = vm_page_lookup(object, pindex + is)) == NULL)
32290ecac61SMatthew Dillon 			break;
3235070c7f8SJohn Dyson 		if (((p->queue - p->pc) == PQ_CACHE) ||
3248b03c8edSMatthew Dillon 		    (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) {
32590ecac61SMatthew Dillon 			break;
32624a1cce3SDavid Greenman 		}
32724a1cce3SDavid Greenman 		vm_page_test_dirty(p);
32890ecac61SMatthew Dillon 		if ((p->dirty & p->valid) == 0 ||
32990ecac61SMatthew Dillon 		    p->queue != PQ_INACTIVE ||
33057601bcbSMatthew Dillon 		    p->wire_count != 0 ||	/* may be held by buf cache */
33157601bcbSMatthew Dillon 		    p->hold_count != 0) {	/* may be undergoing I/O */
33224a1cce3SDavid Greenman 			break;
33324a1cce3SDavid Greenman 		}
33490ecac61SMatthew Dillon 		mc[page_base + pageout_count] = p;
33590ecac61SMatthew Dillon 		++pageout_count;
33690ecac61SMatthew Dillon 		++is;
33724a1cce3SDavid Greenman 	}
33890ecac61SMatthew Dillon 
33990ecac61SMatthew Dillon 	/*
34090ecac61SMatthew Dillon 	 * If we exhausted our forward scan, continue with the reverse scan
34190ecac61SMatthew Dillon 	 * when possible, even past a page boundry.  This catches boundry
34290ecac61SMatthew Dillon 	 * conditions.
34390ecac61SMatthew Dillon 	 */
34490ecac61SMatthew Dillon 	if (ib && pageout_count < vm_pageout_page_count)
34590ecac61SMatthew Dillon 		goto more;
346f6b04d2bSDavid Greenman 
34767bf6868SJohn Dyson 	/*
34867bf6868SJohn Dyson 	 * we allow reads during pageouts...
34967bf6868SJohn Dyson 	 */
3507a935082SAlan Cox 	return (vm_pageout_flush(&mc[page_base], pageout_count, 0));
351aef922f5SJohn Dyson }
352aef922f5SJohn Dyson 
3531c7c3c6aSMatthew Dillon /*
3541c7c3c6aSMatthew Dillon  * vm_pageout_flush() - launder the given pages
3551c7c3c6aSMatthew Dillon  *
3561c7c3c6aSMatthew Dillon  *	The given pages are laundered.  Note that we setup for the start of
3571c7c3c6aSMatthew Dillon  *	I/O ( i.e. busy the page ), mark it read-only, and bump the object
3581c7c3c6aSMatthew Dillon  *	reference count all in here rather then in the parent.  If we want
3591c7c3c6aSMatthew Dillon  *	the parent to do more sophisticated things we may have to change
3601c7c3c6aSMatthew Dillon  *	the ordering.
3611c7c3c6aSMatthew Dillon  */
362aef922f5SJohn Dyson int
3637a935082SAlan Cox vm_pageout_flush(vm_page_t *mc, int count, int flags)
364aef922f5SJohn Dyson {
3652e3b314dSAlan Cox 	vm_object_t object = mc[0]->object;
366aef922f5SJohn Dyson 	int pageout_status[count];
36795461b45SJohn Dyson 	int numpagedout = 0;
368aef922f5SJohn Dyson 	int i;
369aef922f5SJohn Dyson 
37055df3298SAlan Cox 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3712e3b314dSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
3721c7c3c6aSMatthew Dillon 	/*
3731c7c3c6aSMatthew Dillon 	 * Initiate I/O.  Bump the vm_page_t->busy counter and
3741c7c3c6aSMatthew Dillon 	 * mark the pages read-only.
3751c7c3c6aSMatthew Dillon 	 *
3761c7c3c6aSMatthew Dillon 	 * We do not have to fixup the clean/dirty bits here... we can
3771c7c3c6aSMatthew Dillon 	 * allow the pager to do it after the I/O completes.
37802fa91d3SMatthew Dillon 	 *
37902fa91d3SMatthew Dillon 	 * NOTE! mc[i]->dirty may be partial or fragmented due to an
38002fa91d3SMatthew Dillon 	 * edge case with file fragments.
3811c7c3c6aSMatthew Dillon 	 */
3828f9110f6SJohn Dyson 	for (i = 0; i < count; i++) {
3837a935082SAlan Cox 		KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL,
3847a935082SAlan Cox 		    ("vm_pageout_flush: partially invalid page %p index %d/%d",
3857a935082SAlan Cox 			mc[i], i, count));
386e69763a3SDoug Rabson 		vm_page_io_start(mc[i]);
387a12cc0e4SAlan Cox 		pmap_page_protect(mc[i], VM_PROT_READ);
3888f9110f6SJohn Dyson 	}
38955df3298SAlan Cox 	vm_page_unlock_queues();
390d474eaaaSDoug Rabson 	vm_object_pip_add(object, count);
391aef922f5SJohn Dyson 
392aef922f5SJohn Dyson 	vm_pager_put_pages(object, mc, count,
39340bb4f4bSMatthew Dillon 	    (flags | ((object == kernel_object) ? VM_PAGER_PUT_SYNC : 0)),
39426f9a767SRodney W. Grimes 	    pageout_status);
39526f9a767SRodney W. Grimes 
39640eab1e9SAlan Cox 	vm_page_lock_queues();
397aef922f5SJohn Dyson 	for (i = 0; i < count; i++) {
398aef922f5SJohn Dyson 		vm_page_t mt = mc[i];
39924a1cce3SDavid Greenman 
4009ea8d1a6SAlan Cox 		KASSERT((mt->flags & PG_WRITEABLE) == 0,
4019ea8d1a6SAlan Cox 		    ("vm_pageout_flush: page %p is not write protected", mt));
40226f9a767SRodney W. Grimes 		switch (pageout_status[i]) {
40326f9a767SRodney W. Grimes 		case VM_PAGER_OK:
40426f9a767SRodney W. Grimes 		case VM_PAGER_PEND:
40595461b45SJohn Dyson 			numpagedout++;
40626f9a767SRodney W. Grimes 			break;
40726f9a767SRodney W. Grimes 		case VM_PAGER_BAD:
40826f9a767SRodney W. Grimes 			/*
4090d94caffSDavid Greenman 			 * Page outside of range of object. Right now we
4100d94caffSDavid Greenman 			 * essentially lose the changes by pretending it
4110d94caffSDavid Greenman 			 * worked.
41226f9a767SRodney W. Grimes 			 */
4130385347cSPeter Wemm 			pmap_clear_modify(mt);
41490ecac61SMatthew Dillon 			vm_page_undirty(mt);
41526f9a767SRodney W. Grimes 			break;
41626f9a767SRodney W. Grimes 		case VM_PAGER_ERROR:
41726f9a767SRodney W. Grimes 		case VM_PAGER_FAIL:
41826f9a767SRodney W. Grimes 			/*
4190d94caffSDavid Greenman 			 * If page couldn't be paged out, then reactivate the
4200d94caffSDavid Greenman 			 * page so it doesn't clog the inactive list.  (We
4210d94caffSDavid Greenman 			 * will try paging out it again later).
42226f9a767SRodney W. Grimes 			 */
42324a1cce3SDavid Greenman 			vm_page_activate(mt);
42426f9a767SRodney W. Grimes 			break;
42526f9a767SRodney W. Grimes 		case VM_PAGER_AGAIN:
42626f9a767SRodney W. Grimes 			break;
42726f9a767SRodney W. Grimes 		}
42826f9a767SRodney W. Grimes 
42926f9a767SRodney W. Grimes 		/*
4300d94caffSDavid Greenman 		 * If the operation is still going, leave the page busy to
4310d94caffSDavid Greenman 		 * block all other accesses. Also, leave the paging in
4320d94caffSDavid Greenman 		 * progress indicator set so that we don't attempt an object
4330d94caffSDavid Greenman 		 * collapse.
43426f9a767SRodney W. Grimes 		 */
43526f9a767SRodney W. Grimes 		if (pageout_status[i] != VM_PAGER_PEND) {
436f919ebdeSDavid Greenman 			vm_object_pip_wakeup(object);
437e69763a3SDoug Rabson 			vm_page_io_finish(mt);
4389ea8d1a6SAlan Cox 			if (vm_page_count_severe())
4399ea8d1a6SAlan Cox 				vm_page_try_to_cache(mt);
44026f9a767SRodney W. Grimes 		}
44126f9a767SRodney W. Grimes 	}
44295461b45SJohn Dyson 	return numpagedout;
44326f9a767SRodney W. Grimes }
44426f9a767SRodney W. Grimes 
44538efa82bSJohn Dyson #if !defined(NO_SWAPPING)
44626f9a767SRodney W. Grimes /*
44726f9a767SRodney W. Grimes  *	vm_pageout_object_deactivate_pages
44826f9a767SRodney W. Grimes  *
44926f9a767SRodney W. Grimes  *	deactivate enough pages to satisfy the inactive target
45026f9a767SRodney W. Grimes  *	requirements or if vm_page_proc_limit is set, then
45126f9a767SRodney W. Grimes  *	deactivate all of the pages in the object and its
45224a1cce3SDavid Greenman  *	backing_objects.
45326f9a767SRodney W. Grimes  *
45426f9a767SRodney W. Grimes  *	The object and map must be locked.
45526f9a767SRodney W. Grimes  */
45638efa82bSJohn Dyson static void
457ecf6279fSAlan Cox vm_pageout_object_deactivate_pages(pmap, first_object, desired)
458ecf6279fSAlan Cox 	pmap_t pmap;
459ecf6279fSAlan Cox 	vm_object_t first_object;
460ecf6279fSAlan Cox 	long desired;
46126f9a767SRodney W. Grimes {
462ecf6279fSAlan Cox 	vm_object_t backing_object, object;
46354d92145SMatthew Dillon 	vm_page_t p, next;
464ce18aebdSAlan Cox 	int actcount, rcount, remove_mode;
46526f9a767SRodney W. Grimes 
466ecf6279fSAlan Cox 	VM_OBJECT_LOCK_ASSERT(first_object, MA_OWNED);
467ecf6279fSAlan Cox 	if (first_object->type == OBJT_DEVICE || first_object->type == OBJT_PHYS)
46838efa82bSJohn Dyson 		return;
469ecf6279fSAlan Cox 	for (object = first_object;; object = backing_object) {
470ecf6279fSAlan Cox 		if (pmap_resident_count(pmap) <= desired)
471ecf6279fSAlan Cox 			goto unlock_return;
47224a1cce3SDavid Greenman 		if (object->paging_in_progress)
473ecf6279fSAlan Cox 			goto unlock_return;
47426f9a767SRodney W. Grimes 
47585b1dc89SAlan Cox 		remove_mode = 0;
47638efa82bSJohn Dyson 		if (object->shadow_count > 1)
47738efa82bSJohn Dyson 			remove_mode = 1;
47826f9a767SRodney W. Grimes 		/*
47926f9a767SRodney W. Grimes 		 * scan the objects entire memory queue
48026f9a767SRodney W. Grimes 		 */
48126f9a767SRodney W. Grimes 		rcount = object->resident_page_count;
482b18bfc3dSJohn Dyson 		p = TAILQ_FIRST(&object->memq);
483ce18aebdSAlan Cox 		vm_page_lock_queues();
48426f9a767SRodney W. Grimes 		while (p && (rcount-- > 0)) {
485ecf6279fSAlan Cox 			if (pmap_resident_count(pmap) <= desired) {
486ce18aebdSAlan Cox 				vm_page_unlock_queues();
487ecf6279fSAlan Cox 				goto unlock_return;
488ce18aebdSAlan Cox 			}
489b18bfc3dSJohn Dyson 			next = TAILQ_NEXT(p, listq);
490a58d1fa1SDavid Greenman 			cnt.v_pdpages++;
4910d94caffSDavid Greenman 			if (p->wire_count != 0 ||
4920d94caffSDavid Greenman 			    p->hold_count != 0 ||
4930d94caffSDavid Greenman 			    p->busy != 0 ||
4948b03c8edSMatthew Dillon 			    (p->flags & (PG_BUSY|PG_UNMANAGED)) ||
495ecf6279fSAlan Cox 			    !pmap_page_exists_quick(pmap, p)) {
4960d94caffSDavid Greenman 				p = next;
4970d94caffSDavid Greenman 				continue;
4980d94caffSDavid Greenman 			}
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 			}
50538efa82bSJohn Dyson 			if ((p->queue != PQ_ACTIVE) &&
50638efa82bSJohn Dyson 				(p->flags & PG_REFERENCED)) {
507ef743ce6SJohn Dyson 				vm_page_activate(p);
5087e006499SJohn Dyson 				p->act_count += actcount;
509e69763a3SDoug Rabson 				vm_page_flag_clear(p, PG_REFERENCED);
510c8c4b40cSJohn Dyson 			} else if (p->queue == PQ_ACTIVE) {
511ef743ce6SJohn Dyson 				if ((p->flags & PG_REFERENCED) == 0) {
512c8c4b40cSJohn Dyson 					p->act_count -= min(p->act_count, ACT_DECLINE);
5132b6b0df7SMatthew Dillon 					if (!remove_mode && (vm_pageout_algorithm || (p->act_count == 0))) {
5144fec79beSAlan Cox 						pmap_remove_all(p);
51526f9a767SRodney W. Grimes 						vm_page_deactivate(p);
51626f9a767SRodney W. Grimes 					} else {
5176d03d577SMatthew Dillon 						vm_pageq_requeue(p);
518c8c4b40cSJohn Dyson 					}
519c8c4b40cSJohn Dyson 				} else {
520eaf13dd7SJohn Dyson 					vm_page_activate(p);
521e69763a3SDoug Rabson 					vm_page_flag_clear(p, PG_REFERENCED);
52238efa82bSJohn Dyson 					if (p->act_count < (ACT_MAX - ACT_ADVANCE))
52338efa82bSJohn Dyson 						p->act_count += ACT_ADVANCE;
5246d03d577SMatthew Dillon 					vm_pageq_requeue(p);
52526f9a767SRodney W. Grimes 				}
526bd7e5f99SJohn Dyson 			} else if (p->queue == PQ_INACTIVE) {
5274fec79beSAlan Cox 				pmap_remove_all(p);
52826f9a767SRodney W. Grimes 			}
52926f9a767SRodney W. Grimes 			p = next;
53026f9a767SRodney W. Grimes 		}
531ce18aebdSAlan Cox 		vm_page_unlock_queues();
532ecf6279fSAlan Cox 		if ((backing_object = object->backing_object) == NULL)
533ecf6279fSAlan Cox 			goto unlock_return;
534ecf6279fSAlan Cox 		VM_OBJECT_LOCK(backing_object);
535ecf6279fSAlan Cox 		if (object != first_object)
536ecf6279fSAlan Cox 			VM_OBJECT_UNLOCK(object);
53738efa82bSJohn Dyson 	}
538ecf6279fSAlan Cox unlock_return:
539ecf6279fSAlan Cox 	if (object != first_object)
540ecf6279fSAlan Cox 		VM_OBJECT_UNLOCK(object);
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;
550ecf6279fSAlan Cox 	long desired;
55126f9a767SRodney W. Grimes {
55226f9a767SRodney W. Grimes 	vm_map_entry_t tmpe;
55338efa82bSJohn Dyson 	vm_object_t obj, bigobj;
55430105b9eSTor Egge 	int nothingwired;
5550d94caffSDavid Greenman 
556d974f03cSAlan Cox 	if (!vm_map_trylock(map))
55726f9a767SRodney W. Grimes 		return;
55838efa82bSJohn Dyson 
55938efa82bSJohn Dyson 	bigobj = NULL;
56030105b9eSTor Egge 	nothingwired = TRUE;
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;
5700774dfb3SAlan Cox 			if (obj != NULL && VM_OBJECT_TRYLOCK(obj)) {
5710774dfb3SAlan Cox 				if (obj->shadow_count <= 1 &&
5720774dfb3SAlan Cox 				    (bigobj == NULL ||
5730774dfb3SAlan Cox 				     bigobj->resident_page_count < obj->resident_page_count)) {
5740774dfb3SAlan Cox 					if (bigobj != NULL)
5750774dfb3SAlan Cox 						VM_OBJECT_UNLOCK(bigobj);
57638efa82bSJohn Dyson 					bigobj = obj;
5770774dfb3SAlan Cox 				} else
5780774dfb3SAlan Cox 					VM_OBJECT_UNLOCK(obj);
57938efa82bSJohn Dyson 			}
58038efa82bSJohn Dyson 		}
58130105b9eSTor Egge 		if (tmpe->wired_count > 0)
58230105b9eSTor Egge 			nothingwired = FALSE;
58338efa82bSJohn Dyson 		tmpe = tmpe->next;
58438efa82bSJohn Dyson 	}
58538efa82bSJohn Dyson 
5860774dfb3SAlan Cox 	if (bigobj != NULL) {
587ecf6279fSAlan Cox 		vm_pageout_object_deactivate_pages(map->pmap, bigobj, desired);
5880774dfb3SAlan Cox 		VM_OBJECT_UNLOCK(bigobj);
5890774dfb3SAlan Cox 	}
59038efa82bSJohn Dyson 	/*
59138efa82bSJohn Dyson 	 * Next, hunt around for other pages to deactivate.  We actually
59238efa82bSJohn Dyson 	 * do this search sort of wrong -- .text first is not the best idea.
59338efa82bSJohn Dyson 	 */
59438efa82bSJohn Dyson 	tmpe = map->header.next;
59538efa82bSJohn Dyson 	while (tmpe != &map->header) {
596b1028ad1SLuoqi Chen 		if (pmap_resident_count(vm_map_pmap(map)) <= desired)
59738efa82bSJohn Dyson 			break;
5989fdfe602SMatthew Dillon 		if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
59938efa82bSJohn Dyson 			obj = tmpe->object.vm_object;
6000774dfb3SAlan Cox 			if (obj != NULL) {
6010774dfb3SAlan Cox 				VM_OBJECT_LOCK(obj);
602ecf6279fSAlan Cox 				vm_pageout_object_deactivate_pages(map->pmap, obj, desired);
6030774dfb3SAlan Cox 				VM_OBJECT_UNLOCK(obj);
6040774dfb3SAlan Cox 			}
60538efa82bSJohn Dyson 		}
60626f9a767SRodney W. Grimes 		tmpe = tmpe->next;
60738857e7fSAlan Cox 	}
60838efa82bSJohn Dyson 
60938efa82bSJohn Dyson 	/*
61038efa82bSJohn Dyson 	 * Remove all mappings if a process is swapped out, this will free page
61138efa82bSJohn Dyson 	 * table pages.
61238efa82bSJohn Dyson 	 */
61338857e7fSAlan Cox 	if (desired == 0 && nothingwired) {
6140774dfb3SAlan Cox 		GIANT_REQUIRED;
61538857e7fSAlan Cox 		vm_page_lock_queues();
61605ba50f5SJake Burkholder 		pmap_remove(vm_map_pmap(map), vm_map_min(map),
61705ba50f5SJake Burkholder 		    vm_map_max(map));
61838857e7fSAlan Cox 		vm_page_unlock_queues();
61938857e7fSAlan Cox 	}
62038efa82bSJohn Dyson 	vm_map_unlock(map);
62126f9a767SRodney W. Grimes }
622a1287949SEivind Eklund #endif		/* !defined(NO_SWAPPING) */
623df8bae1dSRodney W. Grimes 
6241c7c3c6aSMatthew Dillon /*
625eea85e9bSAlan Cox  * This routine is very drastic, but can save the system
626eea85e9bSAlan Cox  * in a pinch.
627eea85e9bSAlan Cox  */
628eea85e9bSAlan Cox static void
629eea85e9bSAlan Cox vm_pageout_pmap_collect(void)
630eea85e9bSAlan Cox {
631eea85e9bSAlan Cox 	int i;
632eea85e9bSAlan Cox 	vm_page_t m;
633eea85e9bSAlan Cox 	static int warningdone;
634eea85e9bSAlan Cox 
635eea85e9bSAlan Cox 	if (pmap_pagedaemon_waken == 0)
636eea85e9bSAlan Cox 		return;
637eea85e9bSAlan Cox 	if (warningdone < 5) {
638eea85e9bSAlan Cox 		printf("collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
639eea85e9bSAlan Cox 		warningdone++;
640eea85e9bSAlan Cox 	}
641eea85e9bSAlan Cox 	vm_page_lock_queues();
642eea85e9bSAlan Cox 	for (i = 0; i < vm_page_array_size; i++) {
643eea85e9bSAlan Cox 		m = &vm_page_array[i];
644eea85e9bSAlan Cox 		if (m->wire_count || m->hold_count || m->busy ||
645eea85e9bSAlan Cox 		    (m->flags & (PG_BUSY | PG_UNMANAGED)))
646eea85e9bSAlan Cox 			continue;
647eea85e9bSAlan Cox 		pmap_remove_all(m);
648eea85e9bSAlan Cox 	}
649eea85e9bSAlan Cox 	vm_page_unlock_queues();
650eea85e9bSAlan Cox 	pmap_pagedaemon_waken = 0;
651eea85e9bSAlan Cox }
652eea85e9bSAlan Cox 
653eea85e9bSAlan Cox /*
654df8bae1dSRodney W. Grimes  *	vm_pageout_scan does the dirty work for the pageout daemon.
655df8bae1dSRodney W. Grimes  */
6562b6b0df7SMatthew Dillon static void
6572b6b0df7SMatthew Dillon vm_pageout_scan(int pass)
658df8bae1dSRodney W. Grimes {
659502ba6e4SJohn Dyson 	vm_page_t m, next;
660936524aaSMatthew Dillon 	struct vm_page marker;
6611c7c3c6aSMatthew Dillon 	int page_shortage, maxscan, pcount;
6621c7c3c6aSMatthew Dillon 	int addl_page_shortage, addl_page_shortage_init;
6635663e6deSDavid Greenman 	struct proc *p, *bigproc;
664dcbcd518SBruce Evans 	struct thread *td;
6655663e6deSDavid Greenman 	vm_offset_t size, bigsize;
666df8bae1dSRodney W. Grimes 	vm_object_t object;
6677e006499SJohn Dyson 	int actcount;
668f6b04d2bSDavid Greenman 	int vnodes_skipped = 0;
6692b6b0df7SMatthew Dillon 	int maxlaunder;
6701eeaa1e3SJohn Dyson 	int s;
6710d94caffSDavid Greenman 
6722e3b314dSAlan Cox 	mtx_lock(&Giant);
673df8bae1dSRodney W. Grimes 	/*
674855a310fSJeff Roberson 	 * Decrease registered cache sizes.
675855a310fSJeff Roberson 	 */
676855a310fSJeff Roberson 	EVENTHANDLER_INVOKE(vm_lowmem, 0);
677855a310fSJeff Roberson 	/*
678855a310fSJeff Roberson 	 * We do this explicitly after the caches have been drained above.
679855a310fSJeff Roberson 	 */
680855a310fSJeff Roberson 	uma_reclaim();
681855a310fSJeff Roberson 	/*
6825985940eSJohn Dyson 	 * Do whatever cleanup that the pmap code can.
6835985940eSJohn Dyson 	 */
684eea85e9bSAlan Cox 	vm_pageout_pmap_collect();
6855985940eSJohn Dyson 
686b0ef8c5fSAlan Cox 	addl_page_shortage_init = atomic_readandclear_int(&vm_pageout_deficit);
687b182ec9eSJohn Dyson 
6881c7c3c6aSMatthew Dillon 	/*
6891c7c3c6aSMatthew Dillon 	 * Calculate the number of pages we want to either free or move
6902b6b0df7SMatthew Dillon 	 * to the cache.
6911c7c3c6aSMatthew Dillon 	 */
6922b6b0df7SMatthew Dillon 	page_shortage = vm_paging_target() + addl_page_shortage_init;
6931c7c3c6aSMatthew Dillon 
6941c7c3c6aSMatthew Dillon 	/*
695936524aaSMatthew Dillon 	 * Initialize our marker
696936524aaSMatthew Dillon 	 */
697936524aaSMatthew Dillon 	bzero(&marker, sizeof(marker));
698936524aaSMatthew Dillon 	marker.flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER;
699936524aaSMatthew Dillon 	marker.queue = PQ_INACTIVE;
700936524aaSMatthew Dillon 	marker.wire_count = 1;
701936524aaSMatthew Dillon 
702936524aaSMatthew Dillon 	/*
7031c7c3c6aSMatthew Dillon 	 * Start scanning the inactive queue for pages we can move to the
7041c7c3c6aSMatthew Dillon 	 * cache or free.  The scan will stop when the target is reached or
705936524aaSMatthew Dillon 	 * we have scanned the entire inactive queue.  Note that m->act_count
706936524aaSMatthew Dillon 	 * is not used to form decisions for the inactive queue, only for the
707936524aaSMatthew Dillon 	 * active queue.
7082b6b0df7SMatthew Dillon 	 *
7092b6b0df7SMatthew Dillon 	 * maxlaunder limits the number of dirty pages we flush per scan.
7102b6b0df7SMatthew Dillon 	 * For most systems a smaller value (16 or 32) is more robust under
7112b6b0df7SMatthew Dillon 	 * extreme memory and disk pressure because any unnecessary writes
7122b6b0df7SMatthew Dillon 	 * to disk can result in extreme performance degredation.  However,
7132b6b0df7SMatthew Dillon 	 * systems with excessive dirty pages (especially when MAP_NOSYNC is
7142b6b0df7SMatthew Dillon 	 * used) will die horribly with limited laundering.  If the pageout
7152b6b0df7SMatthew Dillon 	 * daemon cannot clean enough pages in the first pass, we let it go
7162b6b0df7SMatthew Dillon 	 * all out in succeeding passes.
7171c7c3c6aSMatthew Dillon 	 */
7182b6b0df7SMatthew Dillon 	if ((maxlaunder = vm_max_launder) <= 1)
7192b6b0df7SMatthew Dillon 		maxlaunder = 1;
7202b6b0df7SMatthew Dillon 	if (pass)
7212b6b0df7SMatthew Dillon 		maxlaunder = 10000;
7223e1b578aSAlan Cox 	vm_page_lock_queues();
72367bf6868SJohn Dyson rescan0:
7241c7c3c6aSMatthew Dillon 	addl_page_shortage = addl_page_shortage_init;
725f6b04d2bSDavid Greenman 	maxscan = cnt.v_inactive_count;
7266d03d577SMatthew Dillon 
727be72f788SAlan Cox 	for (m = TAILQ_FIRST(&vm_page_queues[PQ_INACTIVE].pl);
7281c7c3c6aSMatthew Dillon 	     m != NULL && maxscan-- > 0 && page_shortage > 0;
729e929c00dSKirk McKusick 	     m = next) {
730df8bae1dSRodney W. Grimes 
731a58d1fa1SDavid Greenman 		cnt.v_pdpages++;
732b182ec9eSJohn Dyson 
733f35329acSJohn Dyson 		if (m->queue != PQ_INACTIVE) {
73467bf6868SJohn Dyson 			goto rescan0;
735f35329acSJohn Dyson 		}
736b182ec9eSJohn Dyson 
737b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(m, pageq);
738df8bae1dSRodney W. Grimes 
739936524aaSMatthew Dillon 		/*
740936524aaSMatthew Dillon 		 * skip marker pages
741936524aaSMatthew Dillon 		 */
742936524aaSMatthew Dillon 		if (m->flags & PG_MARKER)
743936524aaSMatthew Dillon 			continue;
744936524aaSMatthew Dillon 
74557601bcbSMatthew Dillon 		/*
74657601bcbSMatthew Dillon 		 * A held page may be undergoing I/O, so skip it.
74757601bcbSMatthew Dillon 		 */
748b182ec9eSJohn Dyson 		if (m->hold_count) {
7496d03d577SMatthew Dillon 			vm_pageq_requeue(m);
750b182ec9eSJohn Dyson 			addl_page_shortage++;
751b182ec9eSJohn Dyson 			continue;
752df8bae1dSRodney W. Grimes 		}
75326f9a767SRodney W. Grimes 		/*
754a1287949SEivind Eklund 		 * Don't mess with busy pages, keep in the front of the
755b18bfc3dSJohn Dyson 		 * queue, most likely are being paged out.
75626f9a767SRodney W. Grimes 		 */
757bd7e5f99SJohn Dyson 		if (m->busy || (m->flags & PG_BUSY)) {
758b182ec9eSJohn Dyson 			addl_page_shortage++;
75926f9a767SRodney W. Grimes 			continue;
76026f9a767SRodney W. Grimes 		}
761bd7e5f99SJohn Dyson 
7627e006499SJohn Dyson 		/*
7631c7c3c6aSMatthew Dillon 		 * If the object is not being used, we ignore previous
7641c7c3c6aSMatthew Dillon 		 * references.
7657e006499SJohn Dyson 		 */
7660d94caffSDavid Greenman 		if (m->object->ref_count == 0) {
767e69763a3SDoug Rabson 			vm_page_flag_clear(m, PG_REFERENCED);
7680385347cSPeter Wemm 			pmap_clear_reference(m);
7697e006499SJohn Dyson 
7707e006499SJohn Dyson 		/*
7711c7c3c6aSMatthew Dillon 		 * Otherwise, if the page has been referenced while in the
7721c7c3c6aSMatthew Dillon 		 * inactive queue, we bump the "activation count" upwards,
7731c7c3c6aSMatthew Dillon 		 * making it less likely that the page will be added back to
7741c7c3c6aSMatthew Dillon 		 * the inactive queue prematurely again.  Here we check the
7751c7c3c6aSMatthew Dillon 		 * page tables (or emulated bits, if any), given the upper
7761c7c3c6aSMatthew Dillon 		 * level VM system not knowing anything about existing
7771c7c3c6aSMatthew Dillon 		 * references.
7787e006499SJohn Dyson 		 */
779ef743ce6SJohn Dyson 		} else if (((m->flags & PG_REFERENCED) == 0) &&
7800385347cSPeter Wemm 			(actcount = pmap_ts_referenced(m))) {
781ef743ce6SJohn Dyson 			vm_page_activate(m);
7827e006499SJohn Dyson 			m->act_count += (actcount + ACT_ADVANCE);
783ef743ce6SJohn Dyson 			continue;
7842fe6e4d7SDavid Greenman 		}
785ef743ce6SJohn Dyson 
7867e006499SJohn Dyson 		/*
7871c7c3c6aSMatthew Dillon 		 * If the upper level VM system knows about any page
7881c7c3c6aSMatthew Dillon 		 * references, we activate the page.  We also set the
7891c7c3c6aSMatthew Dillon 		 * "activation count" higher than normal so that we will less
7901c7c3c6aSMatthew Dillon 		 * likely place pages back onto the inactive queue again.
7917e006499SJohn Dyson 		 */
792bd7e5f99SJohn Dyson 		if ((m->flags & PG_REFERENCED) != 0) {
793e69763a3SDoug Rabson 			vm_page_flag_clear(m, PG_REFERENCED);
7940385347cSPeter Wemm 			actcount = pmap_ts_referenced(m);
79526f9a767SRodney W. Grimes 			vm_page_activate(m);
7967e006499SJohn Dyson 			m->act_count += (actcount + ACT_ADVANCE + 1);
7970d94caffSDavid Greenman 			continue;
7980d94caffSDavid Greenman 		}
79967bf6868SJohn Dyson 
8007e006499SJohn Dyson 		/*
8011c7c3c6aSMatthew Dillon 		 * If the upper level VM system doesn't know anything about
8021c7c3c6aSMatthew Dillon 		 * the page being dirty, we have to check for it again.  As
8031c7c3c6aSMatthew Dillon 		 * far as the VM code knows, any partially dirty pages are
8041c7c3c6aSMatthew Dillon 		 * fully dirty.
8057e006499SJohn Dyson 		 */
80684d98bf6SAlan Cox 		if (m->dirty == 0 && !pmap_is_modified(m)) {
807a3dfacb5SAlan Cox 			/*
808a3dfacb5SAlan Cox 			 * Avoid a race condition: Unless write access is
809a3dfacb5SAlan Cox 			 * removed from the page, another processor could
810a3dfacb5SAlan Cox 			 * modify it before all access is removed by the call
811a3dfacb5SAlan Cox 			 * to vm_page_cache() below.  If vm_page_cache() finds
812a3dfacb5SAlan Cox 			 * that the page has been modified when it removes all
813a3dfacb5SAlan Cox 			 * access, it panics because it cannot cache dirty
814a3dfacb5SAlan Cox 			 * pages.  In principle, we could eliminate just write
815a3dfacb5SAlan Cox 			 * access here rather than all access.  In the expected
816a3dfacb5SAlan Cox 			 * case, when there are no last instant modifications
817a3dfacb5SAlan Cox 			 * to the page, removing all access will be cheaper
818a3dfacb5SAlan Cox 			 * overall.
819a3dfacb5SAlan Cox 			 */
82084d98bf6SAlan Cox 			if ((m->flags & PG_WRITEABLE) != 0)
821a3dfacb5SAlan Cox 				pmap_remove_all(m);
822427e99a0SAlexander Langer 		} else {
8237dbf82dcSMatthew Dillon 			vm_page_dirty(m);
82430dcfc09SJohn Dyson 		}
825dcbcd518SBruce Evans 
8266989c456SAlan Cox 		object = m->object;
8276989c456SAlan Cox 		if (!VM_OBJECT_TRYLOCK(object))
8286989c456SAlan Cox 			continue;
8296989c456SAlan Cox 		if (m->valid == 0) {
8307e006499SJohn Dyson 			/*
8317e006499SJohn Dyson 			 * Invalid pages can be easily freed
8327e006499SJohn Dyson 			 */
8336989c456SAlan Cox 			vm_page_busy(m);
8346989c456SAlan Cox 			pmap_remove_all(m);
8356989c456SAlan Cox 			vm_page_free(m);
8366989c456SAlan Cox 			cnt.v_dfree++;
8371c7c3c6aSMatthew Dillon 			--page_shortage;
838bd7e5f99SJohn Dyson 		} else if (m->dirty == 0) {
8396989c456SAlan Cox 			/*
8406989c456SAlan Cox 			 * Clean pages can be placed onto the cache queue.
8416989c456SAlan Cox 			 * This effectively frees them.
8426989c456SAlan Cox 			 */
843bd7e5f99SJohn Dyson 			vm_page_cache(m);
8441c7c3c6aSMatthew Dillon 			--page_shortage;
8452b6b0df7SMatthew Dillon 		} else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) {
8467e006499SJohn Dyson 			/*
8472b6b0df7SMatthew Dillon 			 * Dirty pages need to be paged out, but flushing
8482b6b0df7SMatthew Dillon 			 * a page is extremely expensive verses freeing
8492b6b0df7SMatthew Dillon 			 * a clean page.  Rather then artificially limiting
8502b6b0df7SMatthew Dillon 			 * the number of pages we can flush, we instead give
8512b6b0df7SMatthew Dillon 			 * dirty pages extra priority on the inactive queue
8522b6b0df7SMatthew Dillon 			 * by forcing them to be cycled through the queue
8532b6b0df7SMatthew Dillon 			 * twice before being flushed, after which the
8542b6b0df7SMatthew Dillon 			 * (now clean) page will cycle through once more
8552b6b0df7SMatthew Dillon 			 * before being freed.  This significantly extends
8562b6b0df7SMatthew Dillon 			 * the thrash point for a heavily loaded machine.
8577e006499SJohn Dyson 			 */
8582b6b0df7SMatthew Dillon 			vm_page_flag_set(m, PG_WINATCFLS);
8596d03d577SMatthew Dillon 			vm_pageq_requeue(m);
8600d94caffSDavid Greenman 		} else if (maxlaunder > 0) {
8612b6b0df7SMatthew Dillon 			/*
8622b6b0df7SMatthew Dillon 			 * We always want to try to flush some dirty pages if
8632b6b0df7SMatthew Dillon 			 * we encounter them, to keep the system stable.
8642b6b0df7SMatthew Dillon 			 * Normally this number is small, but under extreme
8652b6b0df7SMatthew Dillon 			 * pressure where there are insufficient clean pages
8662b6b0df7SMatthew Dillon 			 * on the inactive queue, we may have to go all out.
8672b6b0df7SMatthew Dillon 			 */
86812ac6a1dSJohn Dyson 			int swap_pageouts_ok;
869f6b04d2bSDavid Greenman 			struct vnode *vp = NULL;
870f2a2857bSKirk McKusick 			struct mount *mp;
8710d94caffSDavid Greenman 
87212ac6a1dSJohn Dyson 			if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) {
87312ac6a1dSJohn Dyson 				swap_pageouts_ok = 1;
87412ac6a1dSJohn Dyson 			} else {
87512ac6a1dSJohn Dyson 				swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts);
87612ac6a1dSJohn Dyson 				swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts &&
87790ecac61SMatthew Dillon 				vm_page_count_min());
87812ac6a1dSJohn Dyson 
87912ac6a1dSJohn Dyson 			}
88070111b90SJohn Dyson 
88170111b90SJohn Dyson 			/*
8821c7c3c6aSMatthew Dillon 			 * We don't bother paging objects that are "dead".
8831c7c3c6aSMatthew Dillon 			 * Those objects are in a "rundown" state.
88470111b90SJohn Dyson 			 */
88570111b90SJohn Dyson 			if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) {
8863562af12SAlan Cox 				VM_OBJECT_UNLOCK(object);
8876d03d577SMatthew Dillon 				vm_pageq_requeue(m);
88812ac6a1dSJohn Dyson 				continue;
88912ac6a1dSJohn Dyson 			}
89012ac6a1dSJohn Dyson 
8911c7c3c6aSMatthew Dillon 			/*
8922b6b0df7SMatthew Dillon 			 * The object is already known NOT to be dead.   It
8932b6b0df7SMatthew Dillon 			 * is possible for the vget() to block the whole
8942b6b0df7SMatthew Dillon 			 * pageout daemon, but the new low-memory handling
8952b6b0df7SMatthew Dillon 			 * code should prevent it.
8961c7c3c6aSMatthew Dillon 			 *
8972b6b0df7SMatthew Dillon 			 * The previous code skipped locked vnodes and, worse,
8982b6b0df7SMatthew Dillon 			 * reordered pages in the queue.  This results in
8992b6b0df7SMatthew Dillon 			 * completely non-deterministic operation and, on a
9002b6b0df7SMatthew Dillon 			 * busy system, can lead to extremely non-optimal
9012b6b0df7SMatthew Dillon 			 * pageouts.  For example, it can cause clean pages
9022b6b0df7SMatthew Dillon 			 * to be freed and dirty pages to be moved to the end
9032b6b0df7SMatthew Dillon 			 * of the queue.  Since dirty pages are also moved to
9042b6b0df7SMatthew Dillon 			 * the end of the queue once-cleaned, this gives
9052b6b0df7SMatthew Dillon 			 * way too large a weighting to defering the freeing
9062b6b0df7SMatthew Dillon 			 * of dirty pages.
9071c7c3c6aSMatthew Dillon 			 *
90823b59018SMatthew Dillon 			 * We can't wait forever for the vnode lock, we might
90923b59018SMatthew Dillon 			 * deadlock due to a vn_read() getting stuck in
91023b59018SMatthew Dillon 			 * vm_wait while holding this vnode.  We skip the
91123b59018SMatthew Dillon 			 * vnode if we can't get it in a reasonable amount
91223b59018SMatthew Dillon 			 * of time.
9131c7c3c6aSMatthew Dillon 			 */
9141c7c3c6aSMatthew Dillon 			if (object->type == OBJT_VNODE) {
91524a1cce3SDavid Greenman 				vp = object->handle;
916f2a2857bSKirk McKusick 				mp = NULL;
917f2a2857bSKirk McKusick 				if (vp->v_type == VREG)
918f2a2857bSKirk McKusick 					vn_start_write(vp, &mp, V_NOWAIT);
9193e1b578aSAlan Cox 				vm_page_unlock_queues();
9204b5f5531SAlan Cox 				VI_LOCK(vp);
9213562af12SAlan Cox 				VM_OBJECT_UNLOCK(object);
9224b5f5531SAlan Cox 				if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK |
9234b5f5531SAlan Cox 				    LK_TIMELOCK, curthread)) {
9243562af12SAlan Cox 					VM_OBJECT_LOCK(object);
9253e1b578aSAlan Cox 					vm_page_lock_queues();
92623b59018SMatthew Dillon 					++pageout_lock_miss;
927f2a2857bSKirk McKusick 					vn_finished_write(mp);
928aef922f5SJohn Dyson 					if (object->flags & OBJ_MIGHTBEDIRTY)
929925a3a41SJohn Dyson 						vnodes_skipped++;
9303562af12SAlan Cox 					VM_OBJECT_UNLOCK(object);
931b182ec9eSJohn Dyson 					continue;
93285a376ebSJohn Dyson 				}
9333562af12SAlan Cox 				VM_OBJECT_LOCK(object);
9343e1b578aSAlan Cox 				vm_page_lock_queues();
935f35329acSJohn Dyson 				/*
936936524aaSMatthew Dillon 				 * The page might have been moved to another
937936524aaSMatthew Dillon 				 * queue during potential blocking in vget()
938936524aaSMatthew Dillon 				 * above.  The page might have been freed and
939936524aaSMatthew Dillon 				 * reused for another vnode.  The object might
940936524aaSMatthew Dillon 				 * have been reused for another vnode.
941f35329acSJohn Dyson 				 */
942936524aaSMatthew Dillon 				if (m->queue != PQ_INACTIVE ||
943936524aaSMatthew Dillon 				    m->object != object ||
944936524aaSMatthew Dillon 				    object->handle != vp) {
945b182ec9eSJohn Dyson 					if (object->flags & OBJ_MIGHTBEDIRTY)
946925a3a41SJohn Dyson 						vnodes_skipped++;
9473562af12SAlan Cox 					goto unlock_and_continue;
948b182ec9eSJohn Dyson 				}
949b182ec9eSJohn Dyson 
950f35329acSJohn Dyson 				/*
951936524aaSMatthew Dillon 				 * The page may have been busied during the
952936524aaSMatthew Dillon 				 * blocking in vput();  We don't move the
953936524aaSMatthew Dillon 				 * page back onto the end of the queue so that
954936524aaSMatthew Dillon 				 * statistics are more correct if we don't.
955f35329acSJohn Dyson 				 */
956b182ec9eSJohn Dyson 				if (m->busy || (m->flags & PG_BUSY)) {
9573562af12SAlan Cox 					goto unlock_and_continue;
958b182ec9eSJohn Dyson 				}
959b182ec9eSJohn Dyson 
960f35329acSJohn Dyson 				/*
96157601bcbSMatthew Dillon 				 * If the page has become held it might
96257601bcbSMatthew Dillon 				 * be undergoing I/O, so skip it
963f35329acSJohn Dyson 				 */
964b182ec9eSJohn Dyson 				if (m->hold_count) {
9656d03d577SMatthew Dillon 					vm_pageq_requeue(m);
966b182ec9eSJohn Dyson 					if (object->flags & OBJ_MIGHTBEDIRTY)
967925a3a41SJohn Dyson 						vnodes_skipped++;
9683562af12SAlan Cox 					goto unlock_and_continue;
969f6b04d2bSDavid Greenman 				}
970f6b04d2bSDavid Greenman 			}
971f6b04d2bSDavid Greenman 
9720d94caffSDavid Greenman 			/*
9730d94caffSDavid Greenman 			 * If a page is dirty, then it is either being washed
9740d94caffSDavid Greenman 			 * (but not yet cleaned) or it is still in the
9750d94caffSDavid Greenman 			 * laundry.  If it is still in the laundry, then we
9762b6b0df7SMatthew Dillon 			 * start the cleaning operation.
977936524aaSMatthew Dillon 			 *
978936524aaSMatthew Dillon 			 * This operation may cluster, invalidating the 'next'
979936524aaSMatthew Dillon 			 * pointer.  To prevent an inordinate number of
980936524aaSMatthew Dillon 			 * restarts we use our marker to remember our place.
9812b6b0df7SMatthew Dillon 			 *
9822b6b0df7SMatthew Dillon 			 * decrement page_shortage on success to account for
9832b6b0df7SMatthew Dillon 			 * the (future) cleaned page.  Otherwise we could wind
9842b6b0df7SMatthew Dillon 			 * up laundering or cleaning too many pages.
9850d94caffSDavid Greenman 			 */
986936524aaSMatthew Dillon 			s = splvm();
987936524aaSMatthew Dillon 			TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE].pl, m, &marker, pageq);
988936524aaSMatthew Dillon 			splx(s);
9892b6b0df7SMatthew Dillon 			if (vm_pageout_clean(m) != 0) {
9902b6b0df7SMatthew Dillon 				--page_shortage;
991936524aaSMatthew Dillon 				--maxlaunder;
9922b6b0df7SMatthew Dillon 			}
993936524aaSMatthew Dillon 			s = splvm();
994936524aaSMatthew Dillon 			next = TAILQ_NEXT(&marker, pageq);
995936524aaSMatthew Dillon 			TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, &marker, pageq);
996936524aaSMatthew Dillon 			splx(s);
9973562af12SAlan Cox unlock_and_continue:
9986989c456SAlan Cox 			VM_OBJECT_UNLOCK(object);
999f2a2857bSKirk McKusick 			if (vp) {
10006989c456SAlan Cox 				vm_page_unlock_queues();
1001f6b04d2bSDavid Greenman 				vput(vp);
1002f2a2857bSKirk McKusick 				vn_finished_write(mp);
10036989c456SAlan Cox 				vm_page_lock_queues();
10046989c456SAlan Cox 			}
10056989c456SAlan Cox 			continue;
1006f2a2857bSKirk McKusick 		}
10073562af12SAlan Cox 		VM_OBJECT_UNLOCK(object);
10080d94caffSDavid Greenman 	}
100926f9a767SRodney W. Grimes 
1010df8bae1dSRodney W. Grimes 	/*
1011936524aaSMatthew Dillon 	 * Compute the number of pages we want to try to move from the
1012936524aaSMatthew Dillon 	 * active queue to the inactive queue.
10131c7c3c6aSMatthew Dillon 	 */
1014936524aaSMatthew Dillon 	page_shortage = vm_paging_target() +
1015936524aaSMatthew Dillon 		cnt.v_inactive_target - cnt.v_inactive_count;
1016b182ec9eSJohn Dyson 	page_shortage += addl_page_shortage;
10171c7c3c6aSMatthew Dillon 
10181c7c3c6aSMatthew Dillon 	/*
1019936524aaSMatthew Dillon 	 * Scan the active queue for things we can deactivate. We nominally
1020936524aaSMatthew Dillon 	 * track the per-page activity counter and use it to locate
1021936524aaSMatthew Dillon 	 * deactivation candidates.
10221c7c3c6aSMatthew Dillon 	 */
1023b18bfc3dSJohn Dyson 	pcount = cnt.v_active_count;
1024be72f788SAlan Cox 	m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
10251c7c3c6aSMatthew Dillon 
1026b18bfc3dSJohn Dyson 	while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) {
1027f35329acSJohn Dyson 
1028d3c09dd7SAlan Cox 		KASSERT(m->queue == PQ_ACTIVE,
1029d3c09dd7SAlan Cox 		    ("vm_pageout_scan: page %p isn't active", m));
1030f35329acSJohn Dyson 
1031b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(m, pageq);
1032df8bae1dSRodney W. Grimes 		/*
103326f9a767SRodney W. Grimes 		 * Don't deactivate pages that are busy.
1034df8bae1dSRodney W. Grimes 		 */
1035a647a309SDavid Greenman 		if ((m->busy != 0) ||
10360d94caffSDavid Greenman 		    (m->flags & PG_BUSY) ||
1037f6b04d2bSDavid Greenman 		    (m->hold_count != 0)) {
10386d03d577SMatthew Dillon 			vm_pageq_requeue(m);
103926f9a767SRodney W. Grimes 			m = next;
104026f9a767SRodney W. Grimes 			continue;
1041df8bae1dSRodney W. Grimes 		}
1042b18bfc3dSJohn Dyson 
1043b18bfc3dSJohn Dyson 		/*
1044b18bfc3dSJohn Dyson 		 * The count for pagedaemon pages is done after checking the
1045956f3135SPhilippe Charnier 		 * page for eligibility...
1046b18bfc3dSJohn Dyson 		 */
1047b18bfc3dSJohn Dyson 		cnt.v_pdpages++;
1048ef743ce6SJohn Dyson 
10497e006499SJohn Dyson 		/*
10507e006499SJohn Dyson 		 * Check to see "how much" the page has been used.
10517e006499SJohn Dyson 		 */
10527e006499SJohn Dyson 		actcount = 0;
1053ef743ce6SJohn Dyson 		if (m->object->ref_count != 0) {
1054ef743ce6SJohn Dyson 			if (m->flags & PG_REFERENCED) {
10557e006499SJohn Dyson 				actcount += 1;
10560d94caffSDavid Greenman 			}
10570385347cSPeter Wemm 			actcount += pmap_ts_referenced(m);
10587e006499SJohn Dyson 			if (actcount) {
10597e006499SJohn Dyson 				m->act_count += ACT_ADVANCE + actcount;
106038efa82bSJohn Dyson 				if (m->act_count > ACT_MAX)
106138efa82bSJohn Dyson 					m->act_count = ACT_MAX;
106238efa82bSJohn Dyson 			}
1063b18bfc3dSJohn Dyson 		}
1064ef743ce6SJohn Dyson 
10657e006499SJohn Dyson 		/*
10667e006499SJohn Dyson 		 * Since we have "tested" this bit, we need to clear it now.
10677e006499SJohn Dyson 		 */
1068e69763a3SDoug Rabson 		vm_page_flag_clear(m, PG_REFERENCED);
1069ef743ce6SJohn Dyson 
10707e006499SJohn Dyson 		/*
10717e006499SJohn Dyson 		 * Only if an object is currently being used, do we use the
10727e006499SJohn Dyson 		 * page activation count stats.
10737e006499SJohn Dyson 		 */
10747e006499SJohn Dyson 		if (actcount && (m->object->ref_count != 0)) {
10756d03d577SMatthew Dillon 			vm_pageq_requeue(m);
107626f9a767SRodney W. Grimes 		} else {
107738efa82bSJohn Dyson 			m->act_count -= min(m->act_count, ACT_DECLINE);
10782b6b0df7SMatthew Dillon 			if (vm_pageout_algorithm ||
10792b6b0df7SMatthew Dillon 			    m->object->ref_count == 0 ||
10802b6b0df7SMatthew Dillon 			    m->act_count == 0) {
1081925a3a41SJohn Dyson 				page_shortage--;
1082d4a272dbSJohn Dyson 				if (m->object->ref_count == 0) {
10834fec79beSAlan Cox 					pmap_remove_all(m);
1084d4a272dbSJohn Dyson 					if (m->dirty == 0)
10850d94caffSDavid Greenman 						vm_page_cache(m);
1086d4a272dbSJohn Dyson 					else
1087d4a272dbSJohn Dyson 						vm_page_deactivate(m);
10880d94caffSDavid Greenman 				} else {
108926f9a767SRodney W. Grimes 					vm_page_deactivate(m);
1090df8bae1dSRodney W. Grimes 				}
109138efa82bSJohn Dyson 			} else {
10926d03d577SMatthew Dillon 				vm_pageq_requeue(m);
109338efa82bSJohn Dyson 			}
1094df8bae1dSRodney W. Grimes 		}
109526f9a767SRodney W. Grimes 		m = next;
109626f9a767SRodney W. Grimes 	}
1097f35329acSJohn Dyson 	s = splvm();
10981c7c3c6aSMatthew Dillon 
1099df8bae1dSRodney W. Grimes 	/*
11000d94caffSDavid Greenman 	 * We try to maintain some *really* free pages, this allows interrupt
11011c7c3c6aSMatthew Dillon 	 * code to be guaranteed space.  Since both cache and free queues
11021c7c3c6aSMatthew Dillon 	 * are considered basically 'free', moving pages from cache to free
11031c7c3c6aSMatthew Dillon 	 * does not effect other calculations.
1104df8bae1dSRodney W. Grimes 	 */
1105a1f6d91cSDavid Greenman 	while (cnt.v_free_count < cnt.v_free_reserved) {
11065070c7f8SJohn Dyson 		static int cache_rover = 0;
1107ab42316cSAlan Cox 
1108ab42316cSAlan Cox 		if ((m = vm_page_select_cache(cache_rover)) == NULL)
11090d94caffSDavid Greenman 			break;
1110ab42316cSAlan Cox 		cache_rover = (m->pc + PQ_PRIME2) & PQ_L2_MASK;
1111ab42316cSAlan Cox 		object = m->object;
1112ab42316cSAlan Cox 		VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1113ab42316cSAlan Cox 		vm_page_busy(m);
1114ab42316cSAlan Cox 		pmap_remove_all(m);
1115ab42316cSAlan Cox 		vm_page_free(m);
1116ab42316cSAlan Cox 		VM_OBJECT_UNLOCK(object);
1117ab42316cSAlan Cox 		cnt.v_dfree++;
111826f9a767SRodney W. Grimes 	}
1119f35329acSJohn Dyson 	splx(s);
11208ffc1519SAlan Cox 	vm_page_unlock_queues();
1121ceb0cf87SJohn Dyson #if !defined(NO_SWAPPING)
1122ceb0cf87SJohn Dyson 	/*
1123ceb0cf87SJohn Dyson 	 * Idle process swapout -- run once per second.
1124ceb0cf87SJohn Dyson 	 */
1125ceb0cf87SJohn Dyson 	if (vm_swap_idle_enabled) {
1126ceb0cf87SJohn Dyson 		static long lsec;
1127227ee8a1SPoul-Henning Kamp 		if (time_second != lsec) {
1128ceb0cf87SJohn Dyson 			vm_pageout_req_swapout |= VM_SWAP_IDLE;
1129ceb0cf87SJohn Dyson 			vm_req_vmdaemon();
1130227ee8a1SPoul-Henning Kamp 			lsec = time_second;
1131ceb0cf87SJohn Dyson 		}
1132ceb0cf87SJohn Dyson 	}
1133ceb0cf87SJohn Dyson #endif
1134ceb0cf87SJohn Dyson 
11355663e6deSDavid Greenman 	/*
1136f6b04d2bSDavid Greenman 	 * If we didn't get enough free pages, and we have skipped a vnode
11374c1f8ee9SDavid Greenman 	 * in a writeable object, wakeup the sync daemon.  And kick swapout
11384c1f8ee9SDavid Greenman 	 * if we did not get enough free pages.
1139f6b04d2bSDavid Greenman 	 */
114090ecac61SMatthew Dillon 	if (vm_paging_target() > 0) {
114190ecac61SMatthew Dillon 		if (vnodes_skipped && vm_page_count_min())
1142d50c1994SPeter Wemm 			(void) speedup_syncer();
114338efa82bSJohn Dyson #if !defined(NO_SWAPPING)
114490ecac61SMatthew Dillon 		if (vm_swap_enabled && vm_page_count_target()) {
11454c1f8ee9SDavid Greenman 			vm_req_vmdaemon();
1146ceb0cf87SJohn Dyson 			vm_pageout_req_swapout |= VM_SWAP_NORMAL;
11474c1f8ee9SDavid Greenman 		}
11485afce282SDavid Greenman #endif
11494c1f8ee9SDavid Greenman 	}
11504c1f8ee9SDavid Greenman 
1151f6b04d2bSDavid Greenman 	/*
1152e92686d0SDavid Schultz 	 * If we are critically low on one of RAM or swap and low on
1153e92686d0SDavid Schultz 	 * the other, kill the largest process.  However, we avoid
1154e92686d0SDavid Schultz 	 * doing this on the first pass in order to give ourselves a
1155e92686d0SDavid Schultz 	 * chance to flush out dirty vnode-backed pages and to allow
1156e92686d0SDavid Schultz 	 * active pages to be moved to the inactive queue and reclaimed.
11571c58e4e5SJohn Baldwin 	 *
11581c58e4e5SJohn Baldwin 	 * We keep the process bigproc locked once we find it to keep anyone
11591c58e4e5SJohn Baldwin 	 * from messing with it; however, there is a possibility of
11601c58e4e5SJohn Baldwin 	 * deadlock if process B is bigproc and one of it's child processes
11611c58e4e5SJohn Baldwin 	 * attempts to propagate a signal to B while we are waiting for A's
11621c58e4e5SJohn Baldwin 	 * lock while walking this list.  To avoid this, we don't block on
11631c58e4e5SJohn Baldwin 	 * the process lock but just skip a process if it is already locked.
11645663e6deSDavid Greenman 	 */
1165e92686d0SDavid Schultz 	if (pass != 0 &&
11668f60c087SPoul-Henning Kamp 	    ((swap_pager_avail < 64 && vm_page_count_min()) ||
1167e92686d0SDavid Schultz 	     (swap_pager_full && vm_paging_target() > 0))) {
11685663e6deSDavid Greenman 		bigproc = NULL;
11695663e6deSDavid Greenman 		bigsize = 0;
11701005a129SJohn Baldwin 		sx_slock(&allproc_lock);
1171e602ba25SJulian Elischer 		FOREACH_PROC_IN_SYSTEM(p) {
1172e602ba25SJulian Elischer 			int breakout;
1173dcbcd518SBruce Evans 
11741c58e4e5SJohn Baldwin 			if (PROC_TRYLOCK(p) == 0)
11751c58e4e5SJohn Baldwin 				continue;
11761c58e4e5SJohn Baldwin 			/*
1177f4cf2141SWes Peters 			 * If this is a system or protected process, skip it.
11785663e6deSDavid Greenman 			 */
1179ef6020d1SMike Silbersack 			if ((p->p_flag & P_SYSTEM) || (p->p_pid == 1) ||
1180f4cf2141SWes Peters 			    (p->p_flag & P_PROTECTED) ||
11818f60c087SPoul-Henning Kamp 			    ((p->p_pid < 48) && (swap_pager_avail != 0))) {
11828606d880SJohn Baldwin 				PROC_UNLOCK(p);
11835663e6deSDavid Greenman 				continue;
11845663e6deSDavid Greenman 			}
11855663e6deSDavid Greenman 			/*
1186dcbcd518SBruce Evans 			 * If the process is in a non-running type state,
1187e602ba25SJulian Elischer 			 * don't touch it.  Check all the threads individually.
11885663e6deSDavid Greenman 			 */
11899ed346baSBosko Milekic 			mtx_lock_spin(&sched_lock);
1190e602ba25SJulian Elischer 			breakout = 0;
1191e602ba25SJulian Elischer 			FOREACH_THREAD_IN_PROC(p, td) {
119271fad9fdSJulian Elischer 				if (!TD_ON_RUNQ(td) &&
119371fad9fdSJulian Elischer 				    !TD_IS_RUNNING(td) &&
119471fad9fdSJulian Elischer 				    !TD_IS_SLEEPING(td)) {
1195e602ba25SJulian Elischer 					breakout = 1;
1196e602ba25SJulian Elischer 					break;
1197e602ba25SJulian Elischer 				}
1198e602ba25SJulian Elischer 			}
1199e602ba25SJulian Elischer 			if (breakout) {
12009ed346baSBosko Milekic 				mtx_unlock_spin(&sched_lock);
12011c58e4e5SJohn Baldwin 				PROC_UNLOCK(p);
12025663e6deSDavid Greenman 				continue;
12035663e6deSDavid Greenman 			}
12049ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
12055663e6deSDavid Greenman 			/*
12065663e6deSDavid Greenman 			 * get the process size
12075663e6deSDavid Greenman 			 */
120872d97679SDavid Schultz 			if (!vm_map_trylock_read(&p->p_vmspace->vm_map)) {
120972d97679SDavid Schultz 				PROC_UNLOCK(p);
121072d97679SDavid Schultz 				continue;
121172d97679SDavid Schultz 			}
121272d97679SDavid Schultz 			size = vmspace_swap_count(p->p_vmspace);
121372d97679SDavid Schultz 			vm_map_unlock_read(&p->p_vmspace->vm_map);
121472d97679SDavid Schultz 			size += vmspace_resident_count(p->p_vmspace);
12155663e6deSDavid Greenman 			/*
12165663e6deSDavid Greenman 			 * if the this process is bigger than the biggest one
12175663e6deSDavid Greenman 			 * remember it.
12185663e6deSDavid Greenman 			 */
12195663e6deSDavid Greenman 			if (size > bigsize) {
12201c58e4e5SJohn Baldwin 				if (bigproc != NULL)
12211c58e4e5SJohn Baldwin 					PROC_UNLOCK(bigproc);
12225663e6deSDavid Greenman 				bigproc = p;
12235663e6deSDavid Greenman 				bigsize = size;
12241c58e4e5SJohn Baldwin 			} else
12251c58e4e5SJohn Baldwin 				PROC_UNLOCK(p);
12265663e6deSDavid Greenman 		}
12271005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
12285663e6deSDavid Greenman 		if (bigproc != NULL) {
1229b40ce416SJulian Elischer 			struct ksegrp *kg;
1230729b1e51SDavid Greenman 			killproc(bigproc, "out of swap space");
12319ed346baSBosko Milekic 			mtx_lock_spin(&sched_lock);
1232b40ce416SJulian Elischer 			FOREACH_KSEGRP_IN_PROC(bigproc, kg) {
1233b43179fbSJeff Roberson 				sched_nice(kg, PRIO_MIN); /* XXXKSE ??? */
1234b40ce416SJulian Elischer 			}
12359ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
12361c58e4e5SJohn Baldwin 			PROC_UNLOCK(bigproc);
123724a1cce3SDavid Greenman 			wakeup(&cnt.v_free_count);
12385663e6deSDavid Greenman 		}
12395663e6deSDavid Greenman 	}
12402e3b314dSAlan Cox 	mtx_unlock(&Giant);
124126f9a767SRodney W. Grimes }
124226f9a767SRodney W. Grimes 
1243dc2efb27SJohn Dyson /*
1244dc2efb27SJohn Dyson  * This routine tries to maintain the pseudo LRU active queue,
1245dc2efb27SJohn Dyson  * so that during long periods of time where there is no paging,
1246956f3135SPhilippe Charnier  * that some statistic accumulation still occurs.  This code
1247dc2efb27SJohn Dyson  * helps the situation where paging just starts to occur.
1248dc2efb27SJohn Dyson  */
1249dc2efb27SJohn Dyson static void
1250dc2efb27SJohn Dyson vm_pageout_page_stats()
1251dc2efb27SJohn Dyson {
1252dc2efb27SJohn Dyson 	vm_page_t m,next;
1253dc2efb27SJohn Dyson 	int pcount,tpcount;		/* Number of pages to check */
1254dc2efb27SJohn Dyson 	static int fullintervalcount = 0;
1255bef608bdSJohn Dyson 	int page_shortage;
125625db2c54SMatthew Dillon 	int s0;
1257bef608bdSJohn Dyson 
125890ecac61SMatthew Dillon 	page_shortage =
125990ecac61SMatthew Dillon 	    (cnt.v_inactive_target + cnt.v_cache_max + cnt.v_free_min) -
1260bef608bdSJohn Dyson 	    (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count);
126190ecac61SMatthew Dillon 
1262bef608bdSJohn Dyson 	if (page_shortage <= 0)
1263bef608bdSJohn Dyson 		return;
1264dc2efb27SJohn Dyson 
126525db2c54SMatthew Dillon 	s0 = splvm();
126648c0444cSAlan Cox 	vm_page_lock_queues();
1267dc2efb27SJohn Dyson 	pcount = cnt.v_active_count;
1268dc2efb27SJohn Dyson 	fullintervalcount += vm_pageout_stats_interval;
1269dc2efb27SJohn Dyson 	if (fullintervalcount < vm_pageout_full_stats_interval) {
1270dc2efb27SJohn Dyson 		tpcount = (vm_pageout_stats_max * cnt.v_active_count) / cnt.v_page_count;
1271dc2efb27SJohn Dyson 		if (pcount > tpcount)
1272dc2efb27SJohn Dyson 			pcount = tpcount;
1273883f3caaSMatthew Dillon 	} else {
1274883f3caaSMatthew Dillon 		fullintervalcount = 0;
1275dc2efb27SJohn Dyson 	}
1276dc2efb27SJohn Dyson 
1277be72f788SAlan Cox 	m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
1278dc2efb27SJohn Dyson 	while ((m != NULL) && (pcount-- > 0)) {
12797e006499SJohn Dyson 		int actcount;
1280dc2efb27SJohn Dyson 
1281ab42316cSAlan Cox 		KASSERT(m->queue == PQ_ACTIVE,
1282ab42316cSAlan Cox 		    ("vm_pageout_page_stats: page %p isn't active", m));
1283dc2efb27SJohn Dyson 
1284dc2efb27SJohn Dyson 		next = TAILQ_NEXT(m, pageq);
1285dc2efb27SJohn Dyson 		/*
1286dc2efb27SJohn Dyson 		 * Don't deactivate pages that are busy.
1287dc2efb27SJohn Dyson 		 */
1288dc2efb27SJohn Dyson 		if ((m->busy != 0) ||
1289dc2efb27SJohn Dyson 		    (m->flags & PG_BUSY) ||
1290dc2efb27SJohn Dyson 		    (m->hold_count != 0)) {
12916d03d577SMatthew Dillon 			vm_pageq_requeue(m);
1292dc2efb27SJohn Dyson 			m = next;
1293dc2efb27SJohn Dyson 			continue;
1294dc2efb27SJohn Dyson 		}
1295dc2efb27SJohn Dyson 
12967e006499SJohn Dyson 		actcount = 0;
1297dc2efb27SJohn Dyson 		if (m->flags & PG_REFERENCED) {
1298e69763a3SDoug Rabson 			vm_page_flag_clear(m, PG_REFERENCED);
12997e006499SJohn Dyson 			actcount += 1;
1300dc2efb27SJohn Dyson 		}
1301dc2efb27SJohn Dyson 
13020385347cSPeter Wemm 		actcount += pmap_ts_referenced(m);
13037e006499SJohn Dyson 		if (actcount) {
13047e006499SJohn Dyson 			m->act_count += ACT_ADVANCE + actcount;
1305dc2efb27SJohn Dyson 			if (m->act_count > ACT_MAX)
1306dc2efb27SJohn Dyson 				m->act_count = ACT_MAX;
13076d03d577SMatthew Dillon 			vm_pageq_requeue(m);
1308dc2efb27SJohn Dyson 		} else {
1309dc2efb27SJohn Dyson 			if (m->act_count == 0) {
13107e006499SJohn Dyson 				/*
13112b6b0df7SMatthew Dillon 				 * We turn off page access, so that we have
13122b6b0df7SMatthew Dillon 				 * more accurate RSS stats.  We don't do this
13132b6b0df7SMatthew Dillon 				 * in the normal page deactivation when the
13142b6b0df7SMatthew Dillon 				 * system is loaded VM wise, because the
13152b6b0df7SMatthew Dillon 				 * cost of the large number of page protect
13162b6b0df7SMatthew Dillon 				 * operations would be higher than the value
13172b6b0df7SMatthew Dillon 				 * of doing the operation.
13187e006499SJohn Dyson 				 */
13194fec79beSAlan Cox 				pmap_remove_all(m);
1320dc2efb27SJohn Dyson 				vm_page_deactivate(m);
1321dc2efb27SJohn Dyson 			} else {
1322dc2efb27SJohn Dyson 				m->act_count -= min(m->act_count, ACT_DECLINE);
13236d03d577SMatthew Dillon 				vm_pageq_requeue(m);
1324dc2efb27SJohn Dyson 			}
1325dc2efb27SJohn Dyson 		}
1326dc2efb27SJohn Dyson 
1327dc2efb27SJohn Dyson 		m = next;
1328dc2efb27SJohn Dyson 	}
132948c0444cSAlan Cox 	vm_page_unlock_queues();
133025db2c54SMatthew Dillon 	splx(s0);
1331dc2efb27SJohn Dyson }
1332dc2efb27SJohn Dyson 
1333df8bae1dSRodney W. Grimes /*
1334df8bae1dSRodney W. Grimes  *	vm_pageout is the high level pageout daemon.
1335df8bae1dSRodney W. Grimes  */
13362b14f991SJulian Elischer static void
133726f9a767SRodney W. Grimes vm_pageout()
1338df8bae1dSRodney W. Grimes {
13398b245767SAlan Cox 	int error, pass, s;
13400384fff8SJason Evans 
1341df8bae1dSRodney W. Grimes 	/*
1342df8bae1dSRodney W. Grimes 	 * Initialize some paging parameters.
1343df8bae1dSRodney W. Grimes 	 */
1344f6b04d2bSDavid Greenman 	cnt.v_interrupt_free_min = 2;
1345f35329acSJohn Dyson 	if (cnt.v_page_count < 2000)
1346f35329acSJohn Dyson 		vm_pageout_page_count = 8;
1347f6b04d2bSDavid Greenman 
134845ae1d91SAlan Cox 	/*
134945ae1d91SAlan Cox 	 * v_free_reserved needs to include enough for the largest
135045ae1d91SAlan Cox 	 * swap pager structures plus enough for any pv_entry structs
135145ae1d91SAlan Cox 	 * when paging.
135245ae1d91SAlan Cox 	 */
135345ae1d91SAlan Cox 	if (cnt.v_page_count > 1024)
135445ae1d91SAlan Cox 		cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200;
135545ae1d91SAlan Cox 	else
135645ae1d91SAlan Cox 		cnt.v_free_min = 4;
135745ae1d91SAlan Cox 	cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE +
135845ae1d91SAlan Cox 	    cnt.v_interrupt_free_min;
135945ae1d91SAlan Cox 	cnt.v_free_reserved = vm_pageout_page_count +
136045ae1d91SAlan Cox 	    cnt.v_pageout_free_min + (cnt.v_page_count / 768) + PQ_L2_SIZE;
136145ae1d91SAlan Cox 	cnt.v_free_severe = cnt.v_free_min / 2;
136245ae1d91SAlan Cox 	cnt.v_free_min += cnt.v_free_reserved;
136345ae1d91SAlan Cox 	cnt.v_free_severe += cnt.v_free_reserved;
136445ae1d91SAlan Cox 
1365ed74321bSDavid Greenman 	/*
13662b6b0df7SMatthew Dillon 	 * v_free_target and v_cache_min control pageout hysteresis.  Note
13672b6b0df7SMatthew Dillon 	 * that these are more a measure of the VM cache queue hysteresis
13682b6b0df7SMatthew Dillon 	 * then the VM free queue.  Specifically, v_free_target is the
13692b6b0df7SMatthew Dillon 	 * high water mark (free+cache pages).
13702b6b0df7SMatthew Dillon 	 *
13712b6b0df7SMatthew Dillon 	 * v_free_reserved + v_cache_min (mostly means v_cache_min) is the
13722b6b0df7SMatthew Dillon 	 * low water mark, while v_free_min is the stop.  v_cache_min must
13732b6b0df7SMatthew Dillon 	 * be big enough to handle memory needs while the pageout daemon
13742b6b0df7SMatthew Dillon 	 * is signalled and run to free more pages.
1375ed74321bSDavid Greenman 	 */
1376a15403deSJohn Dyson 	if (cnt.v_free_count > 6144)
13772b6b0df7SMatthew Dillon 		cnt.v_free_target = 4 * cnt.v_free_min + cnt.v_free_reserved;
1378a15403deSJohn Dyson 	else
1379a15403deSJohn Dyson 		cnt.v_free_target = 2 * cnt.v_free_min + cnt.v_free_reserved;
13806f2b142eSDavid Greenman 
1381a15403deSJohn Dyson 	if (cnt.v_free_count > 2048) {
1382a15403deSJohn Dyson 		cnt.v_cache_min = cnt.v_free_target;
1383a15403deSJohn Dyson 		cnt.v_cache_max = 2 * cnt.v_cache_min;
1384a15403deSJohn Dyson 		cnt.v_inactive_target = (3 * cnt.v_free_target) / 2;
13850d94caffSDavid Greenman 	} else {
13860d94caffSDavid Greenman 		cnt.v_cache_min = 0;
13870d94caffSDavid Greenman 		cnt.v_cache_max = 0;
13886f2b142eSDavid Greenman 		cnt.v_inactive_target = cnt.v_free_count / 4;
13890d94caffSDavid Greenman 	}
1390e47ed70bSJohn Dyson 	if (cnt.v_inactive_target > cnt.v_free_count / 3)
1391e47ed70bSJohn Dyson 		cnt.v_inactive_target = cnt.v_free_count / 3;
1392df8bae1dSRodney W. Grimes 
1393df8bae1dSRodney W. Grimes 	/* XXX does not really belong here */
1394df8bae1dSRodney W. Grimes 	if (vm_page_max_wired == 0)
1395df8bae1dSRodney W. Grimes 		vm_page_max_wired = cnt.v_free_count / 3;
1396df8bae1dSRodney W. Grimes 
1397dc2efb27SJohn Dyson 	if (vm_pageout_stats_max == 0)
1398dc2efb27SJohn Dyson 		vm_pageout_stats_max = cnt.v_free_target;
1399dc2efb27SJohn Dyson 
1400dc2efb27SJohn Dyson 	/*
1401dc2efb27SJohn Dyson 	 * Set interval in seconds for stats scan.
1402dc2efb27SJohn Dyson 	 */
1403dc2efb27SJohn Dyson 	if (vm_pageout_stats_interval == 0)
1404bef608bdSJohn Dyson 		vm_pageout_stats_interval = 5;
1405dc2efb27SJohn Dyson 	if (vm_pageout_full_stats_interval == 0)
1406dc2efb27SJohn Dyson 		vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4;
1407dc2efb27SJohn Dyson 
1408dc2efb27SJohn Dyson 	/*
1409dc2efb27SJohn Dyson 	 * Set maximum free per pass
1410dc2efb27SJohn Dyson 	 */
1411dc2efb27SJohn Dyson 	if (vm_pageout_stats_free_max == 0)
1412bef608bdSJohn Dyson 		vm_pageout_stats_free_max = 5;
1413dc2efb27SJohn Dyson 
141424a1cce3SDavid Greenman 	swap_pager_swap_init();
14152b6b0df7SMatthew Dillon 	pass = 0;
1416df8bae1dSRodney W. Grimes 	/*
14170d94caffSDavid Greenman 	 * The pageout daemon is never done, so loop forever.
1418df8bae1dSRodney W. Grimes 	 */
1419df8bae1dSRodney W. Grimes 	while (TRUE) {
14208b245767SAlan Cox 		s = splvm();
14218e1d8de5SAlan Cox 		vm_page_lock_queues();
1422936524aaSMatthew Dillon 		/*
1423936524aaSMatthew Dillon 		 * If we have enough free memory, wakeup waiters.  Do
1424936524aaSMatthew Dillon 		 * not clear vm_pages_needed until we reach our target,
1425936524aaSMatthew Dillon 		 * otherwise we may be woken up over and over again and
1426936524aaSMatthew Dillon 		 * waste a lot of cpu.
1427936524aaSMatthew Dillon 		 */
1428936524aaSMatthew Dillon 		if (vm_pages_needed && !vm_page_count_min()) {
1429a1c0a785SAlan Cox 			if (!vm_paging_needed())
1430936524aaSMatthew Dillon 				vm_pages_needed = 0;
1431936524aaSMatthew Dillon 			wakeup(&cnt.v_free_count);
1432936524aaSMatthew Dillon 		}
1433936524aaSMatthew Dillon 		if (vm_pages_needed) {
143490ecac61SMatthew Dillon 			/*
14352b6b0df7SMatthew Dillon 			 * Still not done, take a second pass without waiting
14362b6b0df7SMatthew Dillon 			 * (unlimited dirty cleaning), otherwise sleep a bit
14372b6b0df7SMatthew Dillon 			 * and try again.
143890ecac61SMatthew Dillon 			 */
14392b6b0df7SMatthew Dillon 			++pass;
14402b6b0df7SMatthew Dillon 			if (pass > 1)
14418e1d8de5SAlan Cox 				msleep(&vm_pages_needed, &vm_page_queue_mtx, PVM,
144223955314SAlfred Perlstein 				       "psleep", hz/2);
144390ecac61SMatthew Dillon 		} else {
144490ecac61SMatthew Dillon 			/*
14452b6b0df7SMatthew Dillon 			 * Good enough, sleep & handle stats.  Prime the pass
14462b6b0df7SMatthew Dillon 			 * for the next run.
144790ecac61SMatthew Dillon 			 */
14482b6b0df7SMatthew Dillon 			if (pass > 1)
14492b6b0df7SMatthew Dillon 				pass = 1;
14502b6b0df7SMatthew Dillon 			else
14512b6b0df7SMatthew Dillon 				pass = 0;
14528e1d8de5SAlan Cox 			error = msleep(&vm_pages_needed, &vm_page_queue_mtx, PVM,
14530cddd8f0SMatthew Dillon 				    "psleep", vm_pageout_stats_interval * hz);
1454dc2efb27SJohn Dyson 			if (error && !vm_pages_needed) {
14558e1d8de5SAlan Cox 				vm_page_unlock_queues();
1456dc2efb27SJohn Dyson 				splx(s);
14572b6b0df7SMatthew Dillon 				pass = 0;
1458dc2efb27SJohn Dyson 				vm_pageout_page_stats();
1459dc2efb27SJohn Dyson 				continue;
1460dc2efb27SJohn Dyson 			}
1461f919ebdeSDavid Greenman 		}
1462b18bfc3dSJohn Dyson 		if (vm_pages_needed)
1463b18bfc3dSJohn Dyson 			cnt.v_pdwakeups++;
14648e1d8de5SAlan Cox 		vm_page_unlock_queues();
1465f919ebdeSDavid Greenman 		splx(s);
14662b6b0df7SMatthew Dillon 		vm_pageout_scan(pass);
1467df8bae1dSRodney W. Grimes 	}
1468df8bae1dSRodney W. Grimes }
146926f9a767SRodney W. Grimes 
14706b4b77adSAlan Cox /*
14716b4b77adSAlan Cox  * Unless the page queue lock is held by the caller, this function
14726b4b77adSAlan Cox  * should be regarded as advisory.  Specifically, the caller should
14736b4b77adSAlan Cox  * not msleep() on &cnt.v_free_count following this function unless
14746b4b77adSAlan Cox  * the page queue lock is held until the msleep() is performed.
14756b4b77adSAlan Cox  */
1476e0c5a895SJohn Dyson void
1477e0c5a895SJohn Dyson pagedaemon_wakeup()
1478e0c5a895SJohn Dyson {
1479a1c0a785SAlan Cox 
1480b40ce416SJulian Elischer 	if (!vm_pages_needed && curthread->td_proc != pageproc) {
1481a1c0a785SAlan Cox 		vm_pages_needed = 1;
1482e0c5a895SJohn Dyson 		wakeup(&vm_pages_needed);
1483e0c5a895SJohn Dyson 	}
1484e0c5a895SJohn Dyson }
1485e0c5a895SJohn Dyson 
148638efa82bSJohn Dyson #if !defined(NO_SWAPPING)
14875afce282SDavid Greenman static void
14885afce282SDavid Greenman vm_req_vmdaemon()
14895afce282SDavid Greenman {
14905afce282SDavid Greenman 	static int lastrun = 0;
14915afce282SDavid Greenman 
1492b18bfc3dSJohn Dyson 	if ((ticks > (lastrun + hz)) || (ticks < lastrun)) {
14935afce282SDavid Greenman 		wakeup(&vm_daemon_needed);
14945afce282SDavid Greenman 		lastrun = ticks;
14955afce282SDavid Greenman 	}
14965afce282SDavid Greenman }
14975afce282SDavid Greenman 
14982b14f991SJulian Elischer static void
14994f9fb771SBruce Evans vm_daemon()
15000d94caffSDavid Greenman {
150191d5354aSJohn Baldwin 	struct rlimit rsslim;
1502dcbcd518SBruce Evans 	struct proc *p;
1503dcbcd518SBruce Evans 	struct thread *td;
1504dcbcd518SBruce Evans 	int breakout;
15050d94caffSDavid Greenman 
15063614c6fcSJohn Baldwin 	mtx_lock(&Giant);
15072fe6e4d7SDavid Greenman 	while (TRUE) {
15080cddd8f0SMatthew Dillon 		tsleep(&vm_daemon_needed, PPAUSE, "psleep", 0);
15094c1f8ee9SDavid Greenman 		if (vm_pageout_req_swapout) {
1510ceb0cf87SJohn Dyson 			swapout_procs(vm_pageout_req_swapout);
15114c1f8ee9SDavid Greenman 			vm_pageout_req_swapout = 0;
15124c1f8ee9SDavid Greenman 		}
15132fe6e4d7SDavid Greenman 		/*
15140d94caffSDavid Greenman 		 * scan the processes for exceeding their rlimits or if
15150d94caffSDavid Greenman 		 * process is swapped out -- deactivate pages
15162fe6e4d7SDavid Greenman 		 */
15171005a129SJohn Baldwin 		sx_slock(&allproc_lock);
1518fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
1519fe2144fdSLuoqi Chen 			vm_pindex_t limit, size;
15202fe6e4d7SDavid Greenman 
15212fe6e4d7SDavid Greenman 			/*
15222fe6e4d7SDavid Greenman 			 * if this is a system process or if we have already
15232fe6e4d7SDavid Greenman 			 * looked at this process, skip it.
15242fe6e4d7SDavid Greenman 			 */
1525897ecacdSJohn Baldwin 			PROC_LOCK(p);
15262fe6e4d7SDavid Greenman 			if (p->p_flag & (P_SYSTEM | P_WEXIT)) {
1527897ecacdSJohn Baldwin 				PROC_UNLOCK(p);
15282fe6e4d7SDavid Greenman 				continue;
15292fe6e4d7SDavid Greenman 			}
15302fe6e4d7SDavid Greenman 			/*
15312fe6e4d7SDavid Greenman 			 * if the process is in a non-running type state,
15322fe6e4d7SDavid Greenman 			 * don't touch it.
15332fe6e4d7SDavid Greenman 			 */
15349ed346baSBosko Milekic 			mtx_lock_spin(&sched_lock);
1535e602ba25SJulian Elischer 			breakout = 0;
1536e602ba25SJulian Elischer 			FOREACH_THREAD_IN_PROC(p, td) {
153771fad9fdSJulian Elischer 				if (!TD_ON_RUNQ(td) &&
153871fad9fdSJulian Elischer 				    !TD_IS_RUNNING(td) &&
153971fad9fdSJulian Elischer 				    !TD_IS_SLEEPING(td)) {
1540e602ba25SJulian Elischer 					breakout = 1;
1541e602ba25SJulian Elischer 					break;
1542e602ba25SJulian Elischer 				}
1543e602ba25SJulian Elischer 			}
15449ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
1545897ecacdSJohn Baldwin 			if (breakout) {
1546897ecacdSJohn Baldwin 				PROC_UNLOCK(p);
15472fe6e4d7SDavid Greenman 				continue;
15482fe6e4d7SDavid Greenman 			}
15492fe6e4d7SDavid Greenman 			/*
15502fe6e4d7SDavid Greenman 			 * get a limit
15512fe6e4d7SDavid Greenman 			 */
1552dcbcd518SBruce Evans 			lim_rlimit(p, RLIMIT_RSS, &rsslim);
1553fe2144fdSLuoqi Chen 			limit = OFF_TO_IDX(
155491d5354aSJohn Baldwin 			    qmin(rsslim.rlim_cur, rsslim.rlim_max));
15552fe6e4d7SDavid Greenman 
15562fe6e4d7SDavid Greenman 			/*
15570d94caffSDavid Greenman 			 * let processes that are swapped out really be
15580d94caffSDavid Greenman 			 * swapped out set the limit to nothing (will force a
15590d94caffSDavid Greenman 			 * swap-out.)
15602fe6e4d7SDavid Greenman 			 */
15618606d880SJohn Baldwin 			if ((p->p_sflag & PS_INMEM) == 0)
15620d94caffSDavid Greenman 				limit = 0;	/* XXX */
1563897ecacdSJohn Baldwin 			PROC_UNLOCK(p);
15642fe6e4d7SDavid Greenman 
1565fe2144fdSLuoqi Chen 			size = vmspace_resident_count(p->p_vmspace);
15662fe6e4d7SDavid Greenman 			if (limit >= 0 && size >= limit) {
1567fe2144fdSLuoqi Chen 				vm_pageout_map_deactivate_pages(
1568fe2144fdSLuoqi Chen 				    &p->p_vmspace->vm_map, limit);
15692fe6e4d7SDavid Greenman 			}
15702fe6e4d7SDavid Greenman 		}
15711005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
157224a1cce3SDavid Greenman 	}
15732fe6e4d7SDavid Greenman }
1574a1287949SEivind Eklund #endif			/* !defined(NO_SWAPPING) */
1575