xref: /freebsd/sys/vm/vm_pageout.c (revision 97824da38216ef556db4667ecd8966c0703e42ce)
160727d8bSWarner Losh /*-
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.
88dbca793STor Egge  * Copyright (c) 2005 Yahoo! Technologies Norway AS
98dbca793STor Egge  * All rights reserved.
10df8bae1dSRodney W. Grimes  *
11df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
12df8bae1dSRodney W. Grimes  * The Mach Operating System project at Carnegie-Mellon University.
13df8bae1dSRodney W. Grimes  *
14df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
15df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
16df8bae1dSRodney W. Grimes  * are met:
17df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
18df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
19df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
20df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
21df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
22df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
235929bcfaSPhilippe Charnier  *    must display the following acknowledgement:
24df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
25df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
26df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
27df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
28df8bae1dSRodney W. Grimes  *    without specific prior written permission.
29df8bae1dSRodney W. Grimes  *
30df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
41df8bae1dSRodney W. Grimes  *
423c4dd356SDavid Greenman  *	from: @(#)vm_pageout.c	7.4 (Berkeley) 5/7/91
43df8bae1dSRodney W. Grimes  *
44df8bae1dSRodney W. Grimes  *
45df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
46df8bae1dSRodney W. Grimes  * All rights reserved.
47df8bae1dSRodney W. Grimes  *
48df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
49df8bae1dSRodney W. Grimes  *
50df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
51df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
52df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
53df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
54df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
55df8bae1dSRodney W. Grimes  *
56df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
57df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
58df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
59df8bae1dSRodney W. Grimes  *
60df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
61df8bae1dSRodney W. Grimes  *
62df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
63df8bae1dSRodney W. Grimes  *  School of Computer Science
64df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
65df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
66df8bae1dSRodney W. Grimes  *
67df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
68df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
69df8bae1dSRodney W. Grimes  */
70df8bae1dSRodney W. Grimes 
71df8bae1dSRodney W. Grimes /*
72df8bae1dSRodney W. Grimes  *	The proverbial page-out daemon.
73df8bae1dSRodney W. Grimes  */
74df8bae1dSRodney W. Grimes 
75874651b1SDavid E. O'Brien #include <sys/cdefs.h>
76874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
77874651b1SDavid E. O'Brien 
78faa5f8d8SAndrzej Bialecki #include "opt_vm.h"
79df8bae1dSRodney W. Grimes #include <sys/param.h>
8026f9a767SRodney W. Grimes #include <sys/systm.h>
81b5e8ce9fSBruce Evans #include <sys/kernel.h>
82855a310fSJeff Roberson #include <sys/eventhandler.h>
83fb919e4dSMark Murray #include <sys/lock.h>
84fb919e4dSMark Murray #include <sys/mutex.h>
8526f9a767SRodney W. Grimes #include <sys/proc.h>
869c8b8baaSPeter Wemm #include <sys/kthread.h>
870384fff8SJason Evans #include <sys/ktr.h>
8897824da3SAlan Cox #include <sys/mount.h>
8926f9a767SRodney W. Grimes #include <sys/resourcevar.h>
90b43179fbSJeff Roberson #include <sys/sched.h>
91d2fc5315SPoul-Henning Kamp #include <sys/signalvar.h>
92f6b04d2bSDavid Greenman #include <sys/vnode.h>
93efeaf95aSDavid Greenman #include <sys/vmmeter.h>
941005a129SJohn Baldwin #include <sys/sx.h>
9538efa82bSJohn Dyson #include <sys/sysctl.h>
96df8bae1dSRodney W. Grimes 
97df8bae1dSRodney W. Grimes #include <vm/vm.h>
98efeaf95aSDavid Greenman #include <vm/vm_param.h>
99efeaf95aSDavid Greenman #include <vm/vm_object.h>
100df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
101efeaf95aSDavid Greenman #include <vm/vm_map.h>
102df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
10324a1cce3SDavid Greenman #include <vm/vm_pager.h>
10405f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h>
105efeaf95aSDavid Greenman #include <vm/vm_extern.h>
106670d17b5SJeff Roberson #include <vm/uma.h>
107df8bae1dSRodney W. Grimes 
1080384fff8SJason Evans #include <machine/mutex.h>
1090384fff8SJason Evans 
1102b14f991SJulian Elischer /*
1112b14f991SJulian Elischer  * System initialization
1122b14f991SJulian Elischer  */
1132b14f991SJulian Elischer 
1142b14f991SJulian Elischer /* the kernel process "vm_pageout"*/
11511caded3SAlfred Perlstein static void vm_pageout(void);
11611caded3SAlfred Perlstein static int vm_pageout_clean(vm_page_t);
11711caded3SAlfred Perlstein static void vm_pageout_scan(int pass);
11845ae1d91SAlan Cox 
1192b14f991SJulian Elischer struct proc *pageproc;
1202b14f991SJulian Elischer 
1212b14f991SJulian Elischer static struct kproc_desc page_kp = {
1222b14f991SJulian Elischer 	"pagedaemon",
1232b14f991SJulian Elischer 	vm_pageout,
1242b14f991SJulian Elischer 	&pageproc
1252b14f991SJulian Elischer };
1269c8b8baaSPeter Wemm SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, &page_kp)
1272b14f991SJulian Elischer 
12838efa82bSJohn Dyson #if !defined(NO_SWAPPING)
1292b14f991SJulian Elischer /* the kernel process "vm_daemon"*/
13011caded3SAlfred Perlstein static void vm_daemon(void);
131f708ef1bSPoul-Henning Kamp static struct	proc *vmproc;
1322b14f991SJulian Elischer 
1332b14f991SJulian Elischer static struct kproc_desc vm_kp = {
1342b14f991SJulian Elischer 	"vmdaemon",
1352b14f991SJulian Elischer 	vm_daemon,
1362b14f991SJulian Elischer 	&vmproc
1372b14f991SJulian Elischer };
1389c8b8baaSPeter Wemm SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp)
13938efa82bSJohn Dyson #endif
1402b14f991SJulian Elischer 
1412b14f991SJulian Elischer 
1428b245767SAlan Cox int vm_pages_needed;		/* Event on which pageout daemon sleeps */
1438b245767SAlan Cox int vm_pageout_deficit;		/* Estimated number of pages deficit */
1448b245767SAlan Cox int vm_pageout_pages_needed;	/* flag saying that the pageout daemon needs pages */
14526f9a767SRodney W. Grimes 
14638efa82bSJohn Dyson #if !defined(NO_SWAPPING)
147f708ef1bSPoul-Henning Kamp static int vm_pageout_req_swapout;	/* XXX */
148f708ef1bSPoul-Henning Kamp static int vm_daemon_needed;
14997824da3SAlan Cox static struct mtx vm_daemon_mtx;
15097824da3SAlan Cox /* Allow for use by vm_pageout before vm_daemon is initialized. */
15197824da3SAlan Cox MTX_SYSINIT(vm_daemon, &vm_daemon_mtx, "vm daemon", MTX_DEF);
15238efa82bSJohn Dyson #endif
1532b6b0df7SMatthew Dillon static int vm_max_launder = 32;
154303b270bSEivind Eklund static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0;
155303b270bSEivind Eklund static int vm_pageout_full_stats_interval = 0;
15626354d4cSAlan Cox static int vm_pageout_algorithm=0;
157303b270bSEivind Eklund static int defer_swap_pageouts=0;
158303b270bSEivind Eklund static int disable_swap_pageouts=0;
15970111b90SJohn Dyson 
16038efa82bSJohn Dyson #if defined(NO_SWAPPING)
161303b270bSEivind Eklund static int vm_swap_enabled=0;
162303b270bSEivind Eklund static int vm_swap_idle_enabled=0;
16338efa82bSJohn Dyson #else
164303b270bSEivind Eklund static int vm_swap_enabled=1;
165303b270bSEivind Eklund static int vm_swap_idle_enabled=0;
16638efa82bSJohn Dyson #endif
16738efa82bSJohn Dyson 
16838efa82bSJohn Dyson SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm,
1692b6b0df7SMatthew Dillon 	CTLFLAG_RW, &vm_pageout_algorithm, 0, "LRU page mgmt");
1702b6b0df7SMatthew Dillon 
1712b6b0df7SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, max_launder,
1722b6b0df7SMatthew Dillon 	CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout");
17338efa82bSJohn Dyson 
174dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max,
175b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length");
176dc2efb27SJohn Dyson 
177dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval,
178b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan");
179dc2efb27SJohn Dyson 
180dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval,
181b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan");
182dc2efb27SJohn Dyson 
18338efa82bSJohn Dyson #if defined(NO_SWAPPING)
184ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
185ceb0cf87SJohn Dyson 	CTLFLAG_RD, &vm_swap_enabled, 0, "");
186ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
187ceb0cf87SJohn Dyson 	CTLFLAG_RD, &vm_swap_idle_enabled, 0, "");
18838efa82bSJohn Dyson #else
189ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
190b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout");
191ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
192b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
19338efa82bSJohn Dyson #endif
19426f9a767SRodney W. Grimes 
195ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts,
196b0359e2cSPeter Wemm 	CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem");
19712ac6a1dSJohn Dyson 
198ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts,
199b0359e2cSPeter Wemm 	CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages");
20012ac6a1dSJohn Dyson 
20123b59018SMatthew Dillon static int pageout_lock_miss;
20223b59018SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss,
20323b59018SMatthew Dillon 	CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout");
20423b59018SMatthew Dillon 
205ffc82b0aSJohn Dyson #define VM_PAGEOUT_PAGE_COUNT 16
206bbc0ec52SDavid Greenman int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT;
207df8bae1dSRodney W. Grimes 
208c3cb3e12SDavid Greenman int vm_page_max_wired;		/* XXX max # of wired pages system-wide */
209df8bae1dSRodney W. Grimes 
21038efa82bSJohn Dyson #if !defined(NO_SWAPPING)
211ecf6279fSAlan Cox static void vm_pageout_map_deactivate_pages(vm_map_t, long);
212ecf6279fSAlan Cox static void vm_pageout_object_deactivate_pages(pmap_t, vm_object_t, long);
21397824da3SAlan Cox static void vm_req_vmdaemon(int req);
21438efa82bSJohn Dyson #endif
215dc2efb27SJohn Dyson static void vm_pageout_page_stats(void);
216cd41fc12SDavid Greenman 
21726f9a767SRodney W. Grimes /*
2188dbca793STor Egge  * vm_pageout_fallback_object_lock:
2198dbca793STor Egge  *
2208dbca793STor Egge  * Lock vm object currently associated with `m'. VM_OBJECT_TRYLOCK is
2218dbca793STor Egge  * known to have failed and page queue must be either PQ_ACTIVE or
2228dbca793STor Egge  * PQ_INACTIVE.  To avoid lock order violation, unlock the page queues
2238dbca793STor Egge  * while locking the vm object.  Use marker page to detect page queue
2248dbca793STor Egge  * changes and maintain notion of next page on page queue.  Return
2258dbca793STor Egge  * TRUE if no changes were detected, FALSE otherwise.  vm object is
2268dbca793STor Egge  * locked on return.
2278dbca793STor Egge  *
2288dbca793STor Egge  * This function depends on both the lock portion of struct vm_object
2298dbca793STor Egge  * and normal struct vm_page being type stable.
2308dbca793STor Egge  */
2318dbca793STor Egge static boolean_t
2328dbca793STor Egge vm_pageout_fallback_object_lock(vm_page_t m, vm_page_t *next)
2338dbca793STor Egge {
2348dbca793STor Egge 	struct vm_page marker;
2358dbca793STor Egge 	boolean_t unchanged;
2368dbca793STor Egge 	u_short queue;
2378dbca793STor Egge 	vm_object_t object;
2388dbca793STor Egge 
2398dbca793STor Egge 	/*
2408dbca793STor Egge 	 * Initialize our marker
2418dbca793STor Egge 	 */
2428dbca793STor Egge 	bzero(&marker, sizeof(marker));
2439af80719SAlan Cox 	marker.flags = PG_FICTITIOUS | PG_MARKER;
2449af80719SAlan Cox 	marker.oflags = VPO_BUSY;
2458dbca793STor Egge 	marker.queue = m->queue;
2468dbca793STor Egge 	marker.wire_count = 1;
2478dbca793STor Egge 
2488dbca793STor Egge 	queue = m->queue;
2498dbca793STor Egge 	object = m->object;
2508dbca793STor Egge 
2518dbca793STor Egge 	TAILQ_INSERT_AFTER(&vm_page_queues[queue].pl,
2528dbca793STor Egge 			   m, &marker, pageq);
2538dbca793STor Egge 	vm_page_unlock_queues();
2548dbca793STor Egge 	VM_OBJECT_LOCK(object);
2558dbca793STor Egge 	vm_page_lock_queues();
2568dbca793STor Egge 
2578dbca793STor Egge 	/* Page queue might have changed. */
2588dbca793STor Egge 	*next = TAILQ_NEXT(&marker, pageq);
2598dbca793STor Egge 	unchanged = (m->queue == queue &&
2608dbca793STor Egge 		     m->object == object &&
2618dbca793STor Egge 		     &marker == TAILQ_NEXT(m, pageq));
2628dbca793STor Egge 	TAILQ_REMOVE(&vm_page_queues[queue].pl,
2638dbca793STor Egge 		     &marker, pageq);
2648dbca793STor Egge 	return (unchanged);
2658dbca793STor Egge }
2668dbca793STor Egge 
2678dbca793STor Egge /*
26826f9a767SRodney W. Grimes  * vm_pageout_clean:
26924a1cce3SDavid Greenman  *
2700d94caffSDavid Greenman  * Clean the page and remove it from the laundry.
27126f9a767SRodney W. Grimes  *
2720d94caffSDavid Greenman  * We set the busy bit to cause potential page faults on this page to
2731c7c3c6aSMatthew Dillon  * block.  Note the careful timing, however, the busy bit isn't set till
2741c7c3c6aSMatthew Dillon  * late and we cannot do anything that will mess with the page.
27526f9a767SRodney W. Grimes  */
2763af76890SPoul-Henning Kamp static int
2778f9110f6SJohn Dyson vm_pageout_clean(m)
27824a1cce3SDavid Greenman 	vm_page_t m;
27924a1cce3SDavid Greenman {
28054d92145SMatthew Dillon 	vm_object_t object;
281f35329acSJohn Dyson 	vm_page_t mc[2*vm_pageout_page_count];
2823562af12SAlan Cox 	int pageout_count;
28390ecac61SMatthew Dillon 	int ib, is, page_base;
284a316d390SJohn Dyson 	vm_pindex_t pindex = m->pindex;
28526f9a767SRodney W. Grimes 
28655df3298SAlan Cox 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2873562af12SAlan Cox 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2880cddd8f0SMatthew Dillon 
28926f9a767SRodney W. Grimes 	/*
2901c7c3c6aSMatthew Dillon 	 * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP
2911c7c3c6aSMatthew Dillon 	 * with the new swapper, but we could have serious problems paging
2921c7c3c6aSMatthew Dillon 	 * out other object types if there is insufficient memory.
2931c7c3c6aSMatthew Dillon 	 *
2941c7c3c6aSMatthew Dillon 	 * Unfortunately, checking free memory here is far too late, so the
2951c7c3c6aSMatthew Dillon 	 * check has been moved up a procedural level.
2961c7c3c6aSMatthew Dillon 	 */
2971c7c3c6aSMatthew Dillon 
29824a1cce3SDavid Greenman 	/*
2999e897b1bSAlan Cox 	 * Can't clean the page if it's busy or held.
30024a1cce3SDavid Greenman 	 */
3018f9110f6SJohn Dyson 	if ((m->hold_count != 0) ||
3029e897b1bSAlan Cox 	    ((m->busy != 0) || (m->oflags & VPO_BUSY))) {
3030d94caffSDavid Greenman 		return 0;
3048b03c8edSMatthew Dillon 	}
3050d94caffSDavid Greenman 
306f35329acSJohn Dyson 	mc[vm_pageout_page_count] = m;
30726f9a767SRodney W. Grimes 	pageout_count = 1;
308f35329acSJohn Dyson 	page_base = vm_pageout_page_count;
30990ecac61SMatthew Dillon 	ib = 1;
31090ecac61SMatthew Dillon 	is = 1;
31190ecac61SMatthew Dillon 
31224a1cce3SDavid Greenman 	/*
31324a1cce3SDavid Greenman 	 * Scan object for clusterable pages.
31424a1cce3SDavid Greenman 	 *
31524a1cce3SDavid Greenman 	 * We can cluster ONLY if: ->> the page is NOT
31624a1cce3SDavid Greenman 	 * clean, wired, busy, held, or mapped into a
31724a1cce3SDavid Greenman 	 * buffer, and one of the following:
31824a1cce3SDavid Greenman 	 * 1) The page is inactive, or a seldom used
31924a1cce3SDavid Greenman 	 *    active page.
32024a1cce3SDavid Greenman 	 * -or-
32124a1cce3SDavid Greenman 	 * 2) we force the issue.
32290ecac61SMatthew Dillon 	 *
32390ecac61SMatthew Dillon 	 * During heavy mmap/modification loads the pageout
32490ecac61SMatthew Dillon 	 * daemon can really fragment the underlying file
32590ecac61SMatthew Dillon 	 * due to flushing pages out of order and not trying
32690ecac61SMatthew Dillon 	 * align the clusters (which leave sporatic out-of-order
32790ecac61SMatthew Dillon 	 * holes).  To solve this problem we do the reverse scan
32890ecac61SMatthew Dillon 	 * first and attempt to align our cluster, then do a
32990ecac61SMatthew Dillon 	 * forward scan if room remains.
33024a1cce3SDavid Greenman 	 */
3315163584cSAlan Cox 	object = m->object;
33290ecac61SMatthew Dillon more:
33390ecac61SMatthew Dillon 	while (ib && pageout_count < vm_pageout_page_count) {
33424a1cce3SDavid Greenman 		vm_page_t p;
335f6b04d2bSDavid Greenman 
33690ecac61SMatthew Dillon 		if (ib > pindex) {
33790ecac61SMatthew Dillon 			ib = 0;
33890ecac61SMatthew Dillon 			break;
339f6b04d2bSDavid Greenman 		}
34090ecac61SMatthew Dillon 
34190ecac61SMatthew Dillon 		if ((p = vm_page_lookup(object, pindex - ib)) == NULL) {
34290ecac61SMatthew Dillon 			ib = 0;
34390ecac61SMatthew Dillon 			break;
34490ecac61SMatthew Dillon 		}
345997e1c25SAlan Cox 		if (VM_PAGE_INQUEUE1(p, PQ_CACHE) ||
3469e897b1bSAlan Cox 		    (p->oflags & VPO_BUSY) || p->busy) {
34790ecac61SMatthew Dillon 			ib = 0;
34890ecac61SMatthew Dillon 			break;
349f6b04d2bSDavid Greenman 		}
35024a1cce3SDavid Greenman 		vm_page_test_dirty(p);
35190ecac61SMatthew Dillon 		if ((p->dirty & p->valid) == 0 ||
35290ecac61SMatthew Dillon 		    p->queue != PQ_INACTIVE ||
35357601bcbSMatthew Dillon 		    p->wire_count != 0 ||	/* may be held by buf cache */
35457601bcbSMatthew Dillon 		    p->hold_count != 0) {	/* may be undergoing I/O */
35590ecac61SMatthew Dillon 			ib = 0;
35624a1cce3SDavid Greenman 			break;
357f6b04d2bSDavid Greenman 		}
35890ecac61SMatthew Dillon 		mc[--page_base] = p;
35990ecac61SMatthew Dillon 		++pageout_count;
36090ecac61SMatthew Dillon 		++ib;
36124a1cce3SDavid Greenman 		/*
36290ecac61SMatthew Dillon 		 * alignment boundry, stop here and switch directions.  Do
36390ecac61SMatthew Dillon 		 * not clear ib.
36424a1cce3SDavid Greenman 		 */
36590ecac61SMatthew Dillon 		if ((pindex - (ib - 1)) % vm_pageout_page_count == 0)
36690ecac61SMatthew Dillon 			break;
36724a1cce3SDavid Greenman 	}
36890ecac61SMatthew Dillon 
36990ecac61SMatthew Dillon 	while (pageout_count < vm_pageout_page_count &&
37090ecac61SMatthew Dillon 	    pindex + is < object->size) {
37190ecac61SMatthew Dillon 		vm_page_t p;
37290ecac61SMatthew Dillon 
37390ecac61SMatthew Dillon 		if ((p = vm_page_lookup(object, pindex + is)) == NULL)
37490ecac61SMatthew Dillon 			break;
375997e1c25SAlan Cox 		if (VM_PAGE_INQUEUE1(p, PQ_CACHE) ||
3769e897b1bSAlan Cox 		    (p->oflags & VPO_BUSY) || p->busy) {
37790ecac61SMatthew Dillon 			break;
37824a1cce3SDavid Greenman 		}
37924a1cce3SDavid Greenman 		vm_page_test_dirty(p);
38090ecac61SMatthew Dillon 		if ((p->dirty & p->valid) == 0 ||
38190ecac61SMatthew Dillon 		    p->queue != PQ_INACTIVE ||
38257601bcbSMatthew Dillon 		    p->wire_count != 0 ||	/* may be held by buf cache */
38357601bcbSMatthew Dillon 		    p->hold_count != 0) {	/* may be undergoing I/O */
38424a1cce3SDavid Greenman 			break;
38524a1cce3SDavid Greenman 		}
38690ecac61SMatthew Dillon 		mc[page_base + pageout_count] = p;
38790ecac61SMatthew Dillon 		++pageout_count;
38890ecac61SMatthew Dillon 		++is;
38924a1cce3SDavid Greenman 	}
39090ecac61SMatthew Dillon 
39190ecac61SMatthew Dillon 	/*
39290ecac61SMatthew Dillon 	 * If we exhausted our forward scan, continue with the reverse scan
39390ecac61SMatthew Dillon 	 * when possible, even past a page boundry.  This catches boundry
39490ecac61SMatthew Dillon 	 * conditions.
39590ecac61SMatthew Dillon 	 */
39690ecac61SMatthew Dillon 	if (ib && pageout_count < vm_pageout_page_count)
39790ecac61SMatthew Dillon 		goto more;
398f6b04d2bSDavid Greenman 
39967bf6868SJohn Dyson 	/*
40067bf6868SJohn Dyson 	 * we allow reads during pageouts...
40167bf6868SJohn Dyson 	 */
4027a935082SAlan Cox 	return (vm_pageout_flush(&mc[page_base], pageout_count, 0));
403aef922f5SJohn Dyson }
404aef922f5SJohn Dyson 
4051c7c3c6aSMatthew Dillon /*
4061c7c3c6aSMatthew Dillon  * vm_pageout_flush() - launder the given pages
4071c7c3c6aSMatthew Dillon  *
4081c7c3c6aSMatthew Dillon  *	The given pages are laundered.  Note that we setup for the start of
4091c7c3c6aSMatthew Dillon  *	I/O ( i.e. busy the page ), mark it read-only, and bump the object
4101c7c3c6aSMatthew Dillon  *	reference count all in here rather then in the parent.  If we want
4111c7c3c6aSMatthew Dillon  *	the parent to do more sophisticated things we may have to change
4121c7c3c6aSMatthew Dillon  *	the ordering.
4131c7c3c6aSMatthew Dillon  */
414aef922f5SJohn Dyson int
4157a935082SAlan Cox vm_pageout_flush(vm_page_t *mc, int count, int flags)
416aef922f5SJohn Dyson {
4172e3b314dSAlan Cox 	vm_object_t object = mc[0]->object;
418aef922f5SJohn Dyson 	int pageout_status[count];
41995461b45SJohn Dyson 	int numpagedout = 0;
420aef922f5SJohn Dyson 	int i;
421aef922f5SJohn Dyson 
42255df3298SAlan Cox 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
4232e3b314dSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
4241c7c3c6aSMatthew Dillon 	/*
4251c7c3c6aSMatthew Dillon 	 * Initiate I/O.  Bump the vm_page_t->busy counter and
4261c7c3c6aSMatthew Dillon 	 * mark the pages read-only.
4271c7c3c6aSMatthew Dillon 	 *
4281c7c3c6aSMatthew Dillon 	 * We do not have to fixup the clean/dirty bits here... we can
4291c7c3c6aSMatthew Dillon 	 * allow the pager to do it after the I/O completes.
43002fa91d3SMatthew Dillon 	 *
43102fa91d3SMatthew Dillon 	 * NOTE! mc[i]->dirty may be partial or fragmented due to an
43202fa91d3SMatthew Dillon 	 * edge case with file fragments.
4331c7c3c6aSMatthew Dillon 	 */
4348f9110f6SJohn Dyson 	for (i = 0; i < count; i++) {
4357a935082SAlan Cox 		KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL,
4367a935082SAlan Cox 		    ("vm_pageout_flush: partially invalid page %p index %d/%d",
4377a935082SAlan Cox 			mc[i], i, count));
438e69763a3SDoug Rabson 		vm_page_io_start(mc[i]);
43978985e42SAlan Cox 		pmap_remove_write(mc[i]);
4408f9110f6SJohn Dyson 	}
44155df3298SAlan Cox 	vm_page_unlock_queues();
442d474eaaaSDoug Rabson 	vm_object_pip_add(object, count);
443aef922f5SJohn Dyson 
444d076fbeaSAlan Cox 	vm_pager_put_pages(object, mc, count, flags, pageout_status);
44526f9a767SRodney W. Grimes 
44640eab1e9SAlan Cox 	vm_page_lock_queues();
447aef922f5SJohn Dyson 	for (i = 0; i < count; i++) {
448aef922f5SJohn Dyson 		vm_page_t mt = mc[i];
44924a1cce3SDavid Greenman 
4509ea8d1a6SAlan Cox 		KASSERT((mt->flags & PG_WRITEABLE) == 0,
4519ea8d1a6SAlan Cox 		    ("vm_pageout_flush: page %p is not write protected", mt));
45226f9a767SRodney W. Grimes 		switch (pageout_status[i]) {
45326f9a767SRodney W. Grimes 		case VM_PAGER_OK:
45426f9a767SRodney W. Grimes 		case VM_PAGER_PEND:
45595461b45SJohn Dyson 			numpagedout++;
45626f9a767SRodney W. Grimes 			break;
45726f9a767SRodney W. Grimes 		case VM_PAGER_BAD:
45826f9a767SRodney W. Grimes 			/*
4590d94caffSDavid Greenman 			 * Page outside of range of object. Right now we
4600d94caffSDavid Greenman 			 * essentially lose the changes by pretending it
4610d94caffSDavid Greenman 			 * worked.
46226f9a767SRodney W. Grimes 			 */
4630385347cSPeter Wemm 			pmap_clear_modify(mt);
46490ecac61SMatthew Dillon 			vm_page_undirty(mt);
46526f9a767SRodney W. Grimes 			break;
46626f9a767SRodney W. Grimes 		case VM_PAGER_ERROR:
46726f9a767SRodney W. Grimes 		case VM_PAGER_FAIL:
46826f9a767SRodney W. Grimes 			/*
4690d94caffSDavid Greenman 			 * If page couldn't be paged out, then reactivate the
4700d94caffSDavid Greenman 			 * page so it doesn't clog the inactive list.  (We
4710d94caffSDavid Greenman 			 * will try paging out it again later).
47226f9a767SRodney W. Grimes 			 */
47324a1cce3SDavid Greenman 			vm_page_activate(mt);
47426f9a767SRodney W. Grimes 			break;
47526f9a767SRodney W. Grimes 		case VM_PAGER_AGAIN:
47626f9a767SRodney W. Grimes 			break;
47726f9a767SRodney W. Grimes 		}
47826f9a767SRodney W. Grimes 
47926f9a767SRodney W. Grimes 		/*
4800d94caffSDavid Greenman 		 * If the operation is still going, leave the page busy to
4810d94caffSDavid Greenman 		 * block all other accesses. Also, leave the paging in
4820d94caffSDavid Greenman 		 * progress indicator set so that we don't attempt an object
4830d94caffSDavid Greenman 		 * collapse.
48426f9a767SRodney W. Grimes 		 */
48526f9a767SRodney W. Grimes 		if (pageout_status[i] != VM_PAGER_PEND) {
486f919ebdeSDavid Greenman 			vm_object_pip_wakeup(object);
487e69763a3SDoug Rabson 			vm_page_io_finish(mt);
4889ea8d1a6SAlan Cox 			if (vm_page_count_severe())
4899ea8d1a6SAlan Cox 				vm_page_try_to_cache(mt);
49026f9a767SRodney W. Grimes 		}
49126f9a767SRodney W. Grimes 	}
49295461b45SJohn Dyson 	return numpagedout;
49326f9a767SRodney W. Grimes }
49426f9a767SRodney W. Grimes 
49538efa82bSJohn Dyson #if !defined(NO_SWAPPING)
49626f9a767SRodney W. Grimes /*
49726f9a767SRodney W. Grimes  *	vm_pageout_object_deactivate_pages
49826f9a767SRodney W. Grimes  *
49926f9a767SRodney W. Grimes  *	deactivate enough pages to satisfy the inactive target
50026f9a767SRodney W. Grimes  *	requirements or if vm_page_proc_limit is set, then
50126f9a767SRodney W. Grimes  *	deactivate all of the pages in the object and its
50224a1cce3SDavid Greenman  *	backing_objects.
50326f9a767SRodney W. Grimes  *
50426f9a767SRodney W. Grimes  *	The object and map must be locked.
50526f9a767SRodney W. Grimes  */
50638efa82bSJohn Dyson static void
507ecf6279fSAlan Cox vm_pageout_object_deactivate_pages(pmap, first_object, desired)
508ecf6279fSAlan Cox 	pmap_t pmap;
509ecf6279fSAlan Cox 	vm_object_t first_object;
510ecf6279fSAlan Cox 	long desired;
51126f9a767SRodney W. Grimes {
512ecf6279fSAlan Cox 	vm_object_t backing_object, object;
51354d92145SMatthew Dillon 	vm_page_t p, next;
514ce18aebdSAlan Cox 	int actcount, rcount, remove_mode;
51526f9a767SRodney W. Grimes 
516ecf6279fSAlan Cox 	VM_OBJECT_LOCK_ASSERT(first_object, MA_OWNED);
517ecf6279fSAlan Cox 	if (first_object->type == OBJT_DEVICE || first_object->type == OBJT_PHYS)
51838efa82bSJohn Dyson 		return;
519ecf6279fSAlan Cox 	for (object = first_object;; object = backing_object) {
520ecf6279fSAlan Cox 		if (pmap_resident_count(pmap) <= desired)
521ecf6279fSAlan Cox 			goto unlock_return;
52224a1cce3SDavid Greenman 		if (object->paging_in_progress)
523ecf6279fSAlan Cox 			goto unlock_return;
52426f9a767SRodney W. Grimes 
52585b1dc89SAlan Cox 		remove_mode = 0;
52638efa82bSJohn Dyson 		if (object->shadow_count > 1)
52738efa82bSJohn Dyson 			remove_mode = 1;
52826f9a767SRodney W. Grimes 		/*
52926f9a767SRodney W. Grimes 		 * scan the objects entire memory queue
53026f9a767SRodney W. Grimes 		 */
53126f9a767SRodney W. Grimes 		rcount = object->resident_page_count;
532b18bfc3dSJohn Dyson 		p = TAILQ_FIRST(&object->memq);
533ce18aebdSAlan Cox 		vm_page_lock_queues();
53426f9a767SRodney W. Grimes 		while (p && (rcount-- > 0)) {
535ecf6279fSAlan Cox 			if (pmap_resident_count(pmap) <= desired) {
536ce18aebdSAlan Cox 				vm_page_unlock_queues();
537ecf6279fSAlan Cox 				goto unlock_return;
538ce18aebdSAlan Cox 			}
539b18bfc3dSJohn Dyson 			next = TAILQ_NEXT(p, listq);
540393a081dSAttilio Rao 			cnt.v_pdpages++;
5410d94caffSDavid Greenman 			if (p->wire_count != 0 ||
5420d94caffSDavid Greenman 			    p->hold_count != 0 ||
5430d94caffSDavid Greenman 			    p->busy != 0 ||
5449af80719SAlan Cox 			    (p->oflags & VPO_BUSY) ||
5459af80719SAlan Cox 			    (p->flags & PG_UNMANAGED) ||
546ecf6279fSAlan Cox 			    !pmap_page_exists_quick(pmap, p)) {
5470d94caffSDavid Greenman 				p = next;
5480d94caffSDavid Greenman 				continue;
5490d94caffSDavid Greenman 			}
5500385347cSPeter Wemm 			actcount = pmap_ts_referenced(p);
5517e006499SJohn Dyson 			if (actcount) {
552e69763a3SDoug Rabson 				vm_page_flag_set(p, PG_REFERENCED);
553c8c4b40cSJohn Dyson 			} else if (p->flags & PG_REFERENCED) {
5547e006499SJohn Dyson 				actcount = 1;
555ef743ce6SJohn Dyson 			}
55638efa82bSJohn Dyson 			if ((p->queue != PQ_ACTIVE) &&
55738efa82bSJohn Dyson 				(p->flags & PG_REFERENCED)) {
558ef743ce6SJohn Dyson 				vm_page_activate(p);
5597e006499SJohn Dyson 				p->act_count += actcount;
560e69763a3SDoug Rabson 				vm_page_flag_clear(p, PG_REFERENCED);
561c8c4b40cSJohn Dyson 			} else if (p->queue == PQ_ACTIVE) {
562ef743ce6SJohn Dyson 				if ((p->flags & PG_REFERENCED) == 0) {
563c8c4b40cSJohn Dyson 					p->act_count -= min(p->act_count, ACT_DECLINE);
5642b6b0df7SMatthew Dillon 					if (!remove_mode && (vm_pageout_algorithm || (p->act_count == 0))) {
5654fec79beSAlan Cox 						pmap_remove_all(p);
56626f9a767SRodney W. Grimes 						vm_page_deactivate(p);
56726f9a767SRodney W. Grimes 					} else {
5686d03d577SMatthew Dillon 						vm_pageq_requeue(p);
569c8c4b40cSJohn Dyson 					}
570c8c4b40cSJohn Dyson 				} else {
571eaf13dd7SJohn Dyson 					vm_page_activate(p);
572e69763a3SDoug Rabson 					vm_page_flag_clear(p, PG_REFERENCED);
57338efa82bSJohn Dyson 					if (p->act_count < (ACT_MAX - ACT_ADVANCE))
57438efa82bSJohn Dyson 						p->act_count += ACT_ADVANCE;
5756d03d577SMatthew Dillon 					vm_pageq_requeue(p);
57626f9a767SRodney W. Grimes 				}
577bd7e5f99SJohn Dyson 			} else if (p->queue == PQ_INACTIVE) {
5784fec79beSAlan Cox 				pmap_remove_all(p);
57926f9a767SRodney W. Grimes 			}
58026f9a767SRodney W. Grimes 			p = next;
58126f9a767SRodney W. Grimes 		}
582ce18aebdSAlan Cox 		vm_page_unlock_queues();
583ecf6279fSAlan Cox 		if ((backing_object = object->backing_object) == NULL)
584ecf6279fSAlan Cox 			goto unlock_return;
585ecf6279fSAlan Cox 		VM_OBJECT_LOCK(backing_object);
586ecf6279fSAlan Cox 		if (object != first_object)
587ecf6279fSAlan Cox 			VM_OBJECT_UNLOCK(object);
58838efa82bSJohn Dyson 	}
589ecf6279fSAlan Cox unlock_return:
590ecf6279fSAlan Cox 	if (object != first_object)
591ecf6279fSAlan Cox 		VM_OBJECT_UNLOCK(object);
59226f9a767SRodney W. Grimes }
59326f9a767SRodney W. Grimes 
59426f9a767SRodney W. Grimes /*
59526f9a767SRodney W. Grimes  * deactivate some number of pages in a map, try to do it fairly, but
59626f9a767SRodney W. Grimes  * that is really hard to do.
59726f9a767SRodney W. Grimes  */
598cd41fc12SDavid Greenman static void
59938efa82bSJohn Dyson vm_pageout_map_deactivate_pages(map, desired)
60026f9a767SRodney W. Grimes 	vm_map_t map;
601ecf6279fSAlan Cox 	long desired;
60226f9a767SRodney W. Grimes {
60326f9a767SRodney W. Grimes 	vm_map_entry_t tmpe;
60438efa82bSJohn Dyson 	vm_object_t obj, bigobj;
60530105b9eSTor Egge 	int nothingwired;
6060d94caffSDavid Greenman 
607d974f03cSAlan Cox 	if (!vm_map_trylock(map))
60826f9a767SRodney W. Grimes 		return;
60938efa82bSJohn Dyson 
61038efa82bSJohn Dyson 	bigobj = NULL;
61130105b9eSTor Egge 	nothingwired = TRUE;
61238efa82bSJohn Dyson 
61338efa82bSJohn Dyson 	/*
61438efa82bSJohn Dyson 	 * first, search out the biggest object, and try to free pages from
61538efa82bSJohn Dyson 	 * that.
61638efa82bSJohn Dyson 	 */
61726f9a767SRodney W. Grimes 	tmpe = map->header.next;
61838efa82bSJohn Dyson 	while (tmpe != &map->header) {
6199fdfe602SMatthew Dillon 		if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
62038efa82bSJohn Dyson 			obj = tmpe->object.vm_object;
6210774dfb3SAlan Cox 			if (obj != NULL && VM_OBJECT_TRYLOCK(obj)) {
6220774dfb3SAlan Cox 				if (obj->shadow_count <= 1 &&
6230774dfb3SAlan Cox 				    (bigobj == NULL ||
6240774dfb3SAlan Cox 				     bigobj->resident_page_count < obj->resident_page_count)) {
6250774dfb3SAlan Cox 					if (bigobj != NULL)
6260774dfb3SAlan Cox 						VM_OBJECT_UNLOCK(bigobj);
62738efa82bSJohn Dyson 					bigobj = obj;
6280774dfb3SAlan Cox 				} else
6290774dfb3SAlan Cox 					VM_OBJECT_UNLOCK(obj);
63038efa82bSJohn Dyson 			}
63138efa82bSJohn Dyson 		}
63230105b9eSTor Egge 		if (tmpe->wired_count > 0)
63330105b9eSTor Egge 			nothingwired = FALSE;
63438efa82bSJohn Dyson 		tmpe = tmpe->next;
63538efa82bSJohn Dyson 	}
63638efa82bSJohn Dyson 
6370774dfb3SAlan Cox 	if (bigobj != NULL) {
638ecf6279fSAlan Cox 		vm_pageout_object_deactivate_pages(map->pmap, bigobj, desired);
6390774dfb3SAlan Cox 		VM_OBJECT_UNLOCK(bigobj);
6400774dfb3SAlan Cox 	}
64138efa82bSJohn Dyson 	/*
64238efa82bSJohn Dyson 	 * Next, hunt around for other pages to deactivate.  We actually
64338efa82bSJohn Dyson 	 * do this search sort of wrong -- .text first is not the best idea.
64438efa82bSJohn Dyson 	 */
64538efa82bSJohn Dyson 	tmpe = map->header.next;
64638efa82bSJohn Dyson 	while (tmpe != &map->header) {
647b1028ad1SLuoqi Chen 		if (pmap_resident_count(vm_map_pmap(map)) <= desired)
64838efa82bSJohn Dyson 			break;
6499fdfe602SMatthew Dillon 		if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
65038efa82bSJohn Dyson 			obj = tmpe->object.vm_object;
6510774dfb3SAlan Cox 			if (obj != NULL) {
6520774dfb3SAlan Cox 				VM_OBJECT_LOCK(obj);
653ecf6279fSAlan Cox 				vm_pageout_object_deactivate_pages(map->pmap, obj, desired);
6540774dfb3SAlan Cox 				VM_OBJECT_UNLOCK(obj);
6550774dfb3SAlan Cox 			}
65638efa82bSJohn Dyson 		}
65726f9a767SRodney W. Grimes 		tmpe = tmpe->next;
65838857e7fSAlan Cox 	}
65938efa82bSJohn Dyson 
66038efa82bSJohn Dyson 	/*
66138efa82bSJohn Dyson 	 * Remove all mappings if a process is swapped out, this will free page
66238efa82bSJohn Dyson 	 * table pages.
66338efa82bSJohn Dyson 	 */
66438857e7fSAlan Cox 	if (desired == 0 && nothingwired) {
66505ba50f5SJake Burkholder 		pmap_remove(vm_map_pmap(map), vm_map_min(map),
66605ba50f5SJake Burkholder 		    vm_map_max(map));
66738857e7fSAlan Cox 	}
66838efa82bSJohn Dyson 	vm_map_unlock(map);
66926f9a767SRodney W. Grimes }
670a1287949SEivind Eklund #endif		/* !defined(NO_SWAPPING) */
671df8bae1dSRodney W. Grimes 
6721c7c3c6aSMatthew Dillon /*
673df8bae1dSRodney W. Grimes  *	vm_pageout_scan does the dirty work for the pageout daemon.
674df8bae1dSRodney W. Grimes  */
6752b6b0df7SMatthew Dillon static void
6762b6b0df7SMatthew Dillon vm_pageout_scan(int pass)
677df8bae1dSRodney W. Grimes {
678502ba6e4SJohn Dyson 	vm_page_t m, next;
679936524aaSMatthew Dillon 	struct vm_page marker;
6801c7c3c6aSMatthew Dillon 	int page_shortage, maxscan, pcount;
6811c7c3c6aSMatthew Dillon 	int addl_page_shortage, addl_page_shortage_init;
6825663e6deSDavid Greenman 	struct proc *p, *bigproc;
683dcbcd518SBruce Evans 	struct thread *td;
6845663e6deSDavid Greenman 	vm_offset_t size, bigsize;
685df8bae1dSRodney W. Grimes 	vm_object_t object;
6862446e4f0SAlan Cox 	int actcount;
687f6b04d2bSDavid Greenman 	int vnodes_skipped = 0;
6882b6b0df7SMatthew Dillon 	int maxlaunder;
6890d94caffSDavid Greenman 
690df8bae1dSRodney W. Grimes 	/*
691855a310fSJeff Roberson 	 * Decrease registered cache sizes.
692855a310fSJeff Roberson 	 */
693855a310fSJeff Roberson 	EVENTHANDLER_INVOKE(vm_lowmem, 0);
694855a310fSJeff Roberson 	/*
695855a310fSJeff Roberson 	 * We do this explicitly after the caches have been drained above.
696855a310fSJeff Roberson 	 */
697855a310fSJeff Roberson 	uma_reclaim();
6985985940eSJohn Dyson 
699b0ef8c5fSAlan Cox 	addl_page_shortage_init = atomic_readandclear_int(&vm_pageout_deficit);
700b182ec9eSJohn Dyson 
7011c7c3c6aSMatthew Dillon 	/*
7021c7c3c6aSMatthew Dillon 	 * Calculate the number of pages we want to either free or move
7032b6b0df7SMatthew Dillon 	 * to the cache.
7041c7c3c6aSMatthew Dillon 	 */
7052b6b0df7SMatthew Dillon 	page_shortage = vm_paging_target() + addl_page_shortage_init;
7061c7c3c6aSMatthew Dillon 
7071c7c3c6aSMatthew Dillon 	/*
708936524aaSMatthew Dillon 	 * Initialize our marker
709936524aaSMatthew Dillon 	 */
710936524aaSMatthew Dillon 	bzero(&marker, sizeof(marker));
7119af80719SAlan Cox 	marker.flags = PG_FICTITIOUS | PG_MARKER;
7129af80719SAlan Cox 	marker.oflags = VPO_BUSY;
713936524aaSMatthew Dillon 	marker.queue = PQ_INACTIVE;
714936524aaSMatthew Dillon 	marker.wire_count = 1;
715936524aaSMatthew Dillon 
716936524aaSMatthew Dillon 	/*
7171c7c3c6aSMatthew Dillon 	 * Start scanning the inactive queue for pages we can move to the
7181c7c3c6aSMatthew Dillon 	 * cache or free.  The scan will stop when the target is reached or
719936524aaSMatthew Dillon 	 * we have scanned the entire inactive queue.  Note that m->act_count
720936524aaSMatthew Dillon 	 * is not used to form decisions for the inactive queue, only for the
721936524aaSMatthew Dillon 	 * active queue.
7222b6b0df7SMatthew Dillon 	 *
7232b6b0df7SMatthew Dillon 	 * maxlaunder limits the number of dirty pages we flush per scan.
7242b6b0df7SMatthew Dillon 	 * For most systems a smaller value (16 or 32) is more robust under
7252b6b0df7SMatthew Dillon 	 * extreme memory and disk pressure because any unnecessary writes
7262b6b0df7SMatthew Dillon 	 * to disk can result in extreme performance degredation.  However,
7272b6b0df7SMatthew Dillon 	 * systems with excessive dirty pages (especially when MAP_NOSYNC is
7282b6b0df7SMatthew Dillon 	 * used) will die horribly with limited laundering.  If the pageout
7292b6b0df7SMatthew Dillon 	 * daemon cannot clean enough pages in the first pass, we let it go
7302b6b0df7SMatthew Dillon 	 * all out in succeeding passes.
7311c7c3c6aSMatthew Dillon 	 */
7322b6b0df7SMatthew Dillon 	if ((maxlaunder = vm_max_launder) <= 1)
7332b6b0df7SMatthew Dillon 		maxlaunder = 1;
7342b6b0df7SMatthew Dillon 	if (pass)
7352b6b0df7SMatthew Dillon 		maxlaunder = 10000;
7363e1b578aSAlan Cox 	vm_page_lock_queues();
73767bf6868SJohn Dyson rescan0:
7381c7c3c6aSMatthew Dillon 	addl_page_shortage = addl_page_shortage_init;
7392feb50bfSAttilio Rao 	maxscan = cnt.v_inactive_count;
7406d03d577SMatthew Dillon 
741be72f788SAlan Cox 	for (m = TAILQ_FIRST(&vm_page_queues[PQ_INACTIVE].pl);
7421c7c3c6aSMatthew Dillon 	     m != NULL && maxscan-- > 0 && page_shortage > 0;
743e929c00dSKirk McKusick 	     m = next) {
744df8bae1dSRodney W. Grimes 
745393a081dSAttilio Rao 		cnt.v_pdpages++;
746b182ec9eSJohn Dyson 
747ef39c05bSAlexander Leidinger 		if (VM_PAGE_GETQUEUE(m) != PQ_INACTIVE) {
74867bf6868SJohn Dyson 			goto rescan0;
749f35329acSJohn Dyson 		}
750b182ec9eSJohn Dyson 
751b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(m, pageq);
75234d9e6fdSAlan Cox 		object = m->object;
753df8bae1dSRodney W. Grimes 
754936524aaSMatthew Dillon 		/*
755936524aaSMatthew Dillon 		 * skip marker pages
756936524aaSMatthew Dillon 		 */
757936524aaSMatthew Dillon 		if (m->flags & PG_MARKER)
758936524aaSMatthew Dillon 			continue;
759936524aaSMatthew Dillon 
76057601bcbSMatthew Dillon 		/*
76157601bcbSMatthew Dillon 		 * A held page may be undergoing I/O, so skip it.
76257601bcbSMatthew Dillon 		 */
763b182ec9eSJohn Dyson 		if (m->hold_count) {
7646d03d577SMatthew Dillon 			vm_pageq_requeue(m);
765b182ec9eSJohn Dyson 			addl_page_shortage++;
766b182ec9eSJohn Dyson 			continue;
767df8bae1dSRodney W. Grimes 		}
76826f9a767SRodney W. Grimes 		/*
769a1287949SEivind Eklund 		 * Don't mess with busy pages, keep in the front of the
770b18bfc3dSJohn Dyson 		 * queue, most likely are being paged out.
77126f9a767SRodney W. Grimes 		 */
7728dbca793STor Egge 		if (!VM_OBJECT_TRYLOCK(object) &&
7738dbca793STor Egge 		    (!vm_pageout_fallback_object_lock(m, &next) ||
7748dbca793STor Egge 		     m->hold_count != 0)) {
7758dbca793STor Egge 			VM_OBJECT_UNLOCK(object);
77634d9e6fdSAlan Cox 			addl_page_shortage++;
77734d9e6fdSAlan Cox 			continue;
77834d9e6fdSAlan Cox 		}
7799af80719SAlan Cox 		if (m->busy || (m->oflags & VPO_BUSY)) {
78034d9e6fdSAlan Cox 			VM_OBJECT_UNLOCK(object);
781b182ec9eSJohn Dyson 			addl_page_shortage++;
78226f9a767SRodney W. Grimes 			continue;
78326f9a767SRodney W. Grimes 		}
784bd7e5f99SJohn Dyson 
7857e006499SJohn Dyson 		/*
7861c7c3c6aSMatthew Dillon 		 * If the object is not being used, we ignore previous
7871c7c3c6aSMatthew Dillon 		 * references.
7887e006499SJohn Dyson 		 */
78934d9e6fdSAlan Cox 		if (object->ref_count == 0) {
790e69763a3SDoug Rabson 			vm_page_flag_clear(m, PG_REFERENCED);
7910385347cSPeter Wemm 			pmap_clear_reference(m);
7927e006499SJohn Dyson 
7937e006499SJohn Dyson 		/*
7941c7c3c6aSMatthew Dillon 		 * Otherwise, if the page has been referenced while in the
7951c7c3c6aSMatthew Dillon 		 * inactive queue, we bump the "activation count" upwards,
7961c7c3c6aSMatthew Dillon 		 * making it less likely that the page will be added back to
7971c7c3c6aSMatthew Dillon 		 * the inactive queue prematurely again.  Here we check the
7981c7c3c6aSMatthew Dillon 		 * page tables (or emulated bits, if any), given the upper
7991c7c3c6aSMatthew Dillon 		 * level VM system not knowing anything about existing
8001c7c3c6aSMatthew Dillon 		 * references.
8017e006499SJohn Dyson 		 */
802ef743ce6SJohn Dyson 		} else if (((m->flags & PG_REFERENCED) == 0) &&
8030385347cSPeter Wemm 			(actcount = pmap_ts_referenced(m))) {
804ef743ce6SJohn Dyson 			vm_page_activate(m);
80534d9e6fdSAlan Cox 			VM_OBJECT_UNLOCK(object);
8067e006499SJohn Dyson 			m->act_count += (actcount + ACT_ADVANCE);
807ef743ce6SJohn Dyson 			continue;
8082fe6e4d7SDavid Greenman 		}
809ef743ce6SJohn Dyson 
8107e006499SJohn Dyson 		/*
8111c7c3c6aSMatthew Dillon 		 * If the upper level VM system knows about any page
8121c7c3c6aSMatthew Dillon 		 * references, we activate the page.  We also set the
8131c7c3c6aSMatthew Dillon 		 * "activation count" higher than normal so that we will less
8141c7c3c6aSMatthew Dillon 		 * likely place pages back onto the inactive queue again.
8157e006499SJohn Dyson 		 */
816bd7e5f99SJohn Dyson 		if ((m->flags & PG_REFERENCED) != 0) {
817e69763a3SDoug Rabson 			vm_page_flag_clear(m, PG_REFERENCED);
8180385347cSPeter Wemm 			actcount = pmap_ts_referenced(m);
81926f9a767SRodney W. Grimes 			vm_page_activate(m);
82034d9e6fdSAlan Cox 			VM_OBJECT_UNLOCK(object);
8217e006499SJohn Dyson 			m->act_count += (actcount + ACT_ADVANCE + 1);
8220d94caffSDavid Greenman 			continue;
8230d94caffSDavid Greenman 		}
82467bf6868SJohn Dyson 
8257e006499SJohn Dyson 		/*
8261c7c3c6aSMatthew Dillon 		 * If the upper level VM system doesn't know anything about
8271c7c3c6aSMatthew Dillon 		 * the page being dirty, we have to check for it again.  As
8281c7c3c6aSMatthew Dillon 		 * far as the VM code knows, any partially dirty pages are
8291c7c3c6aSMatthew Dillon 		 * fully dirty.
8307e006499SJohn Dyson 		 */
83184d98bf6SAlan Cox 		if (m->dirty == 0 && !pmap_is_modified(m)) {
832a3dfacb5SAlan Cox 			/*
833a3dfacb5SAlan Cox 			 * Avoid a race condition: Unless write access is
834a3dfacb5SAlan Cox 			 * removed from the page, another processor could
835a3dfacb5SAlan Cox 			 * modify it before all access is removed by the call
836a3dfacb5SAlan Cox 			 * to vm_page_cache() below.  If vm_page_cache() finds
837a3dfacb5SAlan Cox 			 * that the page has been modified when it removes all
838a3dfacb5SAlan Cox 			 * access, it panics because it cannot cache dirty
839a3dfacb5SAlan Cox 			 * pages.  In principle, we could eliminate just write
840a3dfacb5SAlan Cox 			 * access here rather than all access.  In the expected
841a3dfacb5SAlan Cox 			 * case, when there are no last instant modifications
842a3dfacb5SAlan Cox 			 * to the page, removing all access will be cheaper
843a3dfacb5SAlan Cox 			 * overall.
844a3dfacb5SAlan Cox 			 */
84584d98bf6SAlan Cox 			if ((m->flags & PG_WRITEABLE) != 0)
846a3dfacb5SAlan Cox 				pmap_remove_all(m);
847427e99a0SAlexander Langer 		} else {
8487dbf82dcSMatthew Dillon 			vm_page_dirty(m);
84930dcfc09SJohn Dyson 		}
850dcbcd518SBruce Evans 
8516989c456SAlan Cox 		if (m->valid == 0) {
8527e006499SJohn Dyson 			/*
8537e006499SJohn Dyson 			 * Invalid pages can be easily freed
8547e006499SJohn Dyson 			 */
8556989c456SAlan Cox 			vm_page_free(m);
856393a081dSAttilio Rao 			cnt.v_dfree++;
8571c7c3c6aSMatthew Dillon 			--page_shortage;
858bd7e5f99SJohn Dyson 		} else if (m->dirty == 0) {
8596989c456SAlan Cox 			/*
8606989c456SAlan Cox 			 * Clean pages can be placed onto the cache queue.
8616989c456SAlan Cox 			 * This effectively frees them.
8626989c456SAlan Cox 			 */
863bd7e5f99SJohn Dyson 			vm_page_cache(m);
8641c7c3c6aSMatthew Dillon 			--page_shortage;
8652b6b0df7SMatthew Dillon 		} else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) {
8667e006499SJohn Dyson 			/*
8672b6b0df7SMatthew Dillon 			 * Dirty pages need to be paged out, but flushing
8682b6b0df7SMatthew Dillon 			 * a page is extremely expensive verses freeing
8692b6b0df7SMatthew Dillon 			 * a clean page.  Rather then artificially limiting
8702b6b0df7SMatthew Dillon 			 * the number of pages we can flush, we instead give
8712b6b0df7SMatthew Dillon 			 * dirty pages extra priority on the inactive queue
8722b6b0df7SMatthew Dillon 			 * by forcing them to be cycled through the queue
8732b6b0df7SMatthew Dillon 			 * twice before being flushed, after which the
8742b6b0df7SMatthew Dillon 			 * (now clean) page will cycle through once more
8752b6b0df7SMatthew Dillon 			 * before being freed.  This significantly extends
8762b6b0df7SMatthew Dillon 			 * the thrash point for a heavily loaded machine.
8777e006499SJohn Dyson 			 */
8782b6b0df7SMatthew Dillon 			vm_page_flag_set(m, PG_WINATCFLS);
8796d03d577SMatthew Dillon 			vm_pageq_requeue(m);
8800d94caffSDavid Greenman 		} else if (maxlaunder > 0) {
8812b6b0df7SMatthew Dillon 			/*
8822b6b0df7SMatthew Dillon 			 * We always want to try to flush some dirty pages if
8832b6b0df7SMatthew Dillon 			 * we encounter them, to keep the system stable.
8842b6b0df7SMatthew Dillon 			 * Normally this number is small, but under extreme
8852b6b0df7SMatthew Dillon 			 * pressure where there are insufficient clean pages
8862b6b0df7SMatthew Dillon 			 * on the inactive queue, we may have to go all out.
8872b6b0df7SMatthew Dillon 			 */
88897824da3SAlan Cox 			int swap_pageouts_ok, vfslocked = 0;
889f6b04d2bSDavid Greenman 			struct vnode *vp = NULL;
890f2a2857bSKirk McKusick 			struct mount *mp;
8910d94caffSDavid Greenman 
89212ac6a1dSJohn Dyson 			if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) {
89312ac6a1dSJohn Dyson 				swap_pageouts_ok = 1;
89412ac6a1dSJohn Dyson 			} else {
89512ac6a1dSJohn Dyson 				swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts);
89612ac6a1dSJohn Dyson 				swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts &&
89790ecac61SMatthew Dillon 				vm_page_count_min());
89812ac6a1dSJohn Dyson 
89912ac6a1dSJohn Dyson 			}
90070111b90SJohn Dyson 
90170111b90SJohn Dyson 			/*
9021c7c3c6aSMatthew Dillon 			 * We don't bother paging objects that are "dead".
9031c7c3c6aSMatthew Dillon 			 * Those objects are in a "rundown" state.
90470111b90SJohn Dyson 			 */
90570111b90SJohn Dyson 			if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) {
9063562af12SAlan Cox 				VM_OBJECT_UNLOCK(object);
9076d03d577SMatthew Dillon 				vm_pageq_requeue(m);
90812ac6a1dSJohn Dyson 				continue;
90912ac6a1dSJohn Dyson 			}
91012ac6a1dSJohn Dyson 
9111c7c3c6aSMatthew Dillon 			/*
912625e6c0aSTor Egge 			 * Following operations may unlock
913625e6c0aSTor Egge 			 * vm_page_queue_mtx, invalidating the 'next'
914625e6c0aSTor Egge 			 * pointer.  To prevent an inordinate number
915625e6c0aSTor Egge 			 * of restarts we use our marker to remember
916625e6c0aSTor Egge 			 * our place.
917625e6c0aSTor Egge 			 *
918625e6c0aSTor Egge 			 */
919625e6c0aSTor Egge 			TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE].pl,
920625e6c0aSTor Egge 					   m, &marker, pageq);
921625e6c0aSTor Egge 			/*
9222b6b0df7SMatthew Dillon 			 * The object is already known NOT to be dead.   It
9232b6b0df7SMatthew Dillon 			 * is possible for the vget() to block the whole
9242b6b0df7SMatthew Dillon 			 * pageout daemon, but the new low-memory handling
9252b6b0df7SMatthew Dillon 			 * code should prevent it.
9261c7c3c6aSMatthew Dillon 			 *
9272b6b0df7SMatthew Dillon 			 * The previous code skipped locked vnodes and, worse,
9282b6b0df7SMatthew Dillon 			 * reordered pages in the queue.  This results in
9292b6b0df7SMatthew Dillon 			 * completely non-deterministic operation and, on a
9302b6b0df7SMatthew Dillon 			 * busy system, can lead to extremely non-optimal
9312b6b0df7SMatthew Dillon 			 * pageouts.  For example, it can cause clean pages
9322b6b0df7SMatthew Dillon 			 * to be freed and dirty pages to be moved to the end
9332b6b0df7SMatthew Dillon 			 * of the queue.  Since dirty pages are also moved to
9342b6b0df7SMatthew Dillon 			 * the end of the queue once-cleaned, this gives
9352b6b0df7SMatthew Dillon 			 * way too large a weighting to defering the freeing
9362b6b0df7SMatthew Dillon 			 * of dirty pages.
9371c7c3c6aSMatthew Dillon 			 *
93823b59018SMatthew Dillon 			 * We can't wait forever for the vnode lock, we might
93923b59018SMatthew Dillon 			 * deadlock due to a vn_read() getting stuck in
94023b59018SMatthew Dillon 			 * vm_wait while holding this vnode.  We skip the
94123b59018SMatthew Dillon 			 * vnode if we can't get it in a reasonable amount
94223b59018SMatthew Dillon 			 * of time.
9431c7c3c6aSMatthew Dillon 			 */
9441c7c3c6aSMatthew Dillon 			if (object->type == OBJT_VNODE) {
94524a1cce3SDavid Greenman 				vp = object->handle;
946f2a2857bSKirk McKusick 				mp = NULL;
947db27dcc0STor Egge 				if (vp->v_type == VREG &&
948db27dcc0STor Egge 				    vn_start_write(vp, &mp, V_NOWAIT) != 0) {
949db27dcc0STor Egge 					++pageout_lock_miss;
950db27dcc0STor Egge 					if (object->flags & OBJ_MIGHTBEDIRTY)
951db27dcc0STor Egge 						vnodes_skipped++;
952625e6c0aSTor Egge 					vp = NULL;
953625e6c0aSTor Egge 					goto unlock_and_continue;
954db27dcc0STor Egge 				}
9553e1b578aSAlan Cox 				vm_page_unlock_queues();
9563562af12SAlan Cox 				VM_OBJECT_UNLOCK(object);
95797824da3SAlan Cox 				vfslocked = VFS_LOCK_GIANT(vp->v_mount);
95897824da3SAlan Cox 				if (vget(vp, LK_EXCLUSIVE | LK_TIMELOCK,
95997824da3SAlan Cox 				    curthread)) {
96097824da3SAlan Cox 					VFS_UNLOCK_GIANT(vfslocked);
9613562af12SAlan Cox 					VM_OBJECT_LOCK(object);
9623e1b578aSAlan Cox 					vm_page_lock_queues();
96323b59018SMatthew Dillon 					++pageout_lock_miss;
964f2a2857bSKirk McKusick 					vn_finished_write(mp);
965aef922f5SJohn Dyson 					if (object->flags & OBJ_MIGHTBEDIRTY)
966925a3a41SJohn Dyson 						vnodes_skipped++;
967625e6c0aSTor Egge 					vp = NULL;
968625e6c0aSTor Egge 					goto unlock_and_continue;
96985a376ebSJohn Dyson 				}
9703562af12SAlan Cox 				VM_OBJECT_LOCK(object);
9713e1b578aSAlan Cox 				vm_page_lock_queues();
972f35329acSJohn Dyson 				/*
973936524aaSMatthew Dillon 				 * The page might have been moved to another
974936524aaSMatthew Dillon 				 * queue during potential blocking in vget()
975936524aaSMatthew Dillon 				 * above.  The page might have been freed and
976936524aaSMatthew Dillon 				 * reused for another vnode.  The object might
977936524aaSMatthew Dillon 				 * have been reused for another vnode.
978f35329acSJohn Dyson 				 */
979ef39c05bSAlexander Leidinger 				if (VM_PAGE_GETQUEUE(m) != PQ_INACTIVE ||
980936524aaSMatthew Dillon 				    m->object != object ||
981625e6c0aSTor Egge 				    object->handle != vp ||
982625e6c0aSTor Egge 				    TAILQ_NEXT(m, pageq) != &marker) {
983b182ec9eSJohn Dyson 					if (object->flags & OBJ_MIGHTBEDIRTY)
984925a3a41SJohn Dyson 						vnodes_skipped++;
9853562af12SAlan Cox 					goto unlock_and_continue;
986b182ec9eSJohn Dyson 				}
987b182ec9eSJohn Dyson 
988f35329acSJohn Dyson 				/*
989936524aaSMatthew Dillon 				 * The page may have been busied during the
990936524aaSMatthew Dillon 				 * blocking in vput();  We don't move the
991936524aaSMatthew Dillon 				 * page back onto the end of the queue so that
992936524aaSMatthew Dillon 				 * statistics are more correct if we don't.
993f35329acSJohn Dyson 				 */
9949af80719SAlan Cox 				if (m->busy || (m->oflags & VPO_BUSY)) {
9953562af12SAlan Cox 					goto unlock_and_continue;
996b182ec9eSJohn Dyson 				}
997b182ec9eSJohn Dyson 
998f35329acSJohn Dyson 				/*
99957601bcbSMatthew Dillon 				 * If the page has become held it might
100057601bcbSMatthew Dillon 				 * be undergoing I/O, so skip it
1001f35329acSJohn Dyson 				 */
1002b182ec9eSJohn Dyson 				if (m->hold_count) {
10036d03d577SMatthew Dillon 					vm_pageq_requeue(m);
1004b182ec9eSJohn Dyson 					if (object->flags & OBJ_MIGHTBEDIRTY)
1005925a3a41SJohn Dyson 						vnodes_skipped++;
10063562af12SAlan Cox 					goto unlock_and_continue;
1007f6b04d2bSDavid Greenman 				}
1008f6b04d2bSDavid Greenman 			}
1009f6b04d2bSDavid Greenman 
10100d94caffSDavid Greenman 			/*
10110d94caffSDavid Greenman 			 * If a page is dirty, then it is either being washed
10120d94caffSDavid Greenman 			 * (but not yet cleaned) or it is still in the
10130d94caffSDavid Greenman 			 * laundry.  If it is still in the laundry, then we
10142b6b0df7SMatthew Dillon 			 * start the cleaning operation.
1015936524aaSMatthew Dillon 			 *
10162b6b0df7SMatthew Dillon 			 * decrement page_shortage on success to account for
10172b6b0df7SMatthew Dillon 			 * the (future) cleaned page.  Otherwise we could wind
10182b6b0df7SMatthew Dillon 			 * up laundering or cleaning too many pages.
10190d94caffSDavid Greenman 			 */
10202b6b0df7SMatthew Dillon 			if (vm_pageout_clean(m) != 0) {
10212b6b0df7SMatthew Dillon 				--page_shortage;
1022936524aaSMatthew Dillon 				--maxlaunder;
10232b6b0df7SMatthew Dillon 			}
10243562af12SAlan Cox unlock_and_continue:
10256989c456SAlan Cox 			VM_OBJECT_UNLOCK(object);
1026f2a2857bSKirk McKusick 			if (vp) {
10276989c456SAlan Cox 				vm_page_unlock_queues();
1028f6b04d2bSDavid Greenman 				vput(vp);
102997824da3SAlan Cox 				VFS_UNLOCK_GIANT(vfslocked);
1030f2a2857bSKirk McKusick 				vn_finished_write(mp);
10316989c456SAlan Cox 				vm_page_lock_queues();
10326989c456SAlan Cox 			}
1033625e6c0aSTor Egge 			next = TAILQ_NEXT(&marker, pageq);
1034625e6c0aSTor Egge 			TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl,
1035625e6c0aSTor Egge 				     &marker, pageq);
10366989c456SAlan Cox 			continue;
1037f2a2857bSKirk McKusick 		}
10383562af12SAlan Cox 		VM_OBJECT_UNLOCK(object);
10390d94caffSDavid Greenman 	}
104026f9a767SRodney W. Grimes 
1041df8bae1dSRodney W. Grimes 	/*
1042936524aaSMatthew Dillon 	 * Compute the number of pages we want to try to move from the
1043936524aaSMatthew Dillon 	 * active queue to the inactive queue.
10441c7c3c6aSMatthew Dillon 	 */
10452feb50bfSAttilio Rao 	page_shortage = vm_paging_target() +
10462feb50bfSAttilio Rao 		cnt.v_inactive_target - cnt.v_inactive_count;
1047b182ec9eSJohn Dyson 	page_shortage += addl_page_shortage;
10481c7c3c6aSMatthew Dillon 
10491c7c3c6aSMatthew Dillon 	/*
1050936524aaSMatthew Dillon 	 * Scan the active queue for things we can deactivate. We nominally
1051936524aaSMatthew Dillon 	 * track the per-page activity counter and use it to locate
1052936524aaSMatthew Dillon 	 * deactivation candidates.
10531c7c3c6aSMatthew Dillon 	 */
10542feb50bfSAttilio Rao 	pcount = cnt.v_active_count;
1055be72f788SAlan Cox 	m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
10561c7c3c6aSMatthew Dillon 
1057b18bfc3dSJohn Dyson 	while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) {
1058f35329acSJohn Dyson 
1059ef39c05bSAlexander Leidinger 		KASSERT(VM_PAGE_INQUEUE2(m, PQ_ACTIVE),
1060d3c09dd7SAlan Cox 		    ("vm_pageout_scan: page %p isn't active", m));
1061f35329acSJohn Dyson 
1062b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(m, pageq);
1063b08abf6cSAlan Cox 		object = m->object;
10648dbca793STor Egge 		if ((m->flags & PG_MARKER) != 0) {
10658dbca793STor Egge 			m = next;
10668dbca793STor Egge 			continue;
10678dbca793STor Egge 		}
10688dbca793STor Egge 		if (!VM_OBJECT_TRYLOCK(object) &&
10698dbca793STor Egge 		    !vm_pageout_fallback_object_lock(m, &next)) {
10708dbca793STor Egge 			VM_OBJECT_UNLOCK(object);
10714b8a5c40SAlan Cox 			m = next;
1072b08abf6cSAlan Cox 			continue;
1073b08abf6cSAlan Cox 		}
1074b08abf6cSAlan Cox 
1075df8bae1dSRodney W. Grimes 		/*
107626f9a767SRodney W. Grimes 		 * Don't deactivate pages that are busy.
1077df8bae1dSRodney W. Grimes 		 */
1078a647a309SDavid Greenman 		if ((m->busy != 0) ||
10799af80719SAlan Cox 		    (m->oflags & VPO_BUSY) ||
1080f6b04d2bSDavid Greenman 		    (m->hold_count != 0)) {
1081b08abf6cSAlan Cox 			VM_OBJECT_UNLOCK(object);
10826d03d577SMatthew Dillon 			vm_pageq_requeue(m);
108326f9a767SRodney W. Grimes 			m = next;
108426f9a767SRodney W. Grimes 			continue;
1085df8bae1dSRodney W. Grimes 		}
1086b18bfc3dSJohn Dyson 
1087b18bfc3dSJohn Dyson 		/*
1088b18bfc3dSJohn Dyson 		 * The count for pagedaemon pages is done after checking the
1089956f3135SPhilippe Charnier 		 * page for eligibility...
1090b18bfc3dSJohn Dyson 		 */
1091393a081dSAttilio Rao 		cnt.v_pdpages++;
1092ef743ce6SJohn Dyson 
10937e006499SJohn Dyson 		/*
10947e006499SJohn Dyson 		 * Check to see "how much" the page has been used.
10957e006499SJohn Dyson 		 */
10967e006499SJohn Dyson 		actcount = 0;
1097b08abf6cSAlan Cox 		if (object->ref_count != 0) {
1098ef743ce6SJohn Dyson 			if (m->flags & PG_REFERENCED) {
10997e006499SJohn Dyson 				actcount += 1;
11000d94caffSDavid Greenman 			}
11010385347cSPeter Wemm 			actcount += pmap_ts_referenced(m);
11027e006499SJohn Dyson 			if (actcount) {
11037e006499SJohn Dyson 				m->act_count += ACT_ADVANCE + actcount;
110438efa82bSJohn Dyson 				if (m->act_count > ACT_MAX)
110538efa82bSJohn Dyson 					m->act_count = ACT_MAX;
110638efa82bSJohn Dyson 			}
1107b18bfc3dSJohn Dyson 		}
1108ef743ce6SJohn Dyson 
11097e006499SJohn Dyson 		/*
11107e006499SJohn Dyson 		 * Since we have "tested" this bit, we need to clear it now.
11117e006499SJohn Dyson 		 */
1112e69763a3SDoug Rabson 		vm_page_flag_clear(m, PG_REFERENCED);
1113ef743ce6SJohn Dyson 
11147e006499SJohn Dyson 		/*
11157e006499SJohn Dyson 		 * Only if an object is currently being used, do we use the
11167e006499SJohn Dyson 		 * page activation count stats.
11177e006499SJohn Dyson 		 */
1118b08abf6cSAlan Cox 		if (actcount && (object->ref_count != 0)) {
11196d03d577SMatthew Dillon 			vm_pageq_requeue(m);
112026f9a767SRodney W. Grimes 		} else {
112138efa82bSJohn Dyson 			m->act_count -= min(m->act_count, ACT_DECLINE);
11222b6b0df7SMatthew Dillon 			if (vm_pageout_algorithm ||
1123b08abf6cSAlan Cox 			    object->ref_count == 0 ||
11242b6b0df7SMatthew Dillon 			    m->act_count == 0) {
1125925a3a41SJohn Dyson 				page_shortage--;
1126b08abf6cSAlan Cox 				if (object->ref_count == 0) {
11274fec79beSAlan Cox 					pmap_remove_all(m);
1128d4a272dbSJohn Dyson 					if (m->dirty == 0)
11290d94caffSDavid Greenman 						vm_page_cache(m);
1130d4a272dbSJohn Dyson 					else
1131d4a272dbSJohn Dyson 						vm_page_deactivate(m);
11320d94caffSDavid Greenman 				} else {
113326f9a767SRodney W. Grimes 					vm_page_deactivate(m);
1134df8bae1dSRodney W. Grimes 				}
113538efa82bSJohn Dyson 			} else {
11366d03d577SMatthew Dillon 				vm_pageq_requeue(m);
113738efa82bSJohn Dyson 			}
1138df8bae1dSRodney W. Grimes 		}
1139b08abf6cSAlan Cox 		VM_OBJECT_UNLOCK(object);
114026f9a767SRodney W. Grimes 		m = next;
114126f9a767SRodney W. Grimes 	}
11421c7c3c6aSMatthew Dillon 
1143df8bae1dSRodney W. Grimes 	/*
11440d94caffSDavid Greenman 	 * We try to maintain some *really* free pages, this allows interrupt
11451c7c3c6aSMatthew Dillon 	 * code to be guaranteed space.  Since both cache and free queues
11461c7c3c6aSMatthew Dillon 	 * are considered basically 'free', moving pages from cache to free
11471c7c3c6aSMatthew Dillon 	 * does not effect other calculations.
1148df8bae1dSRodney W. Grimes 	 */
11492446e4f0SAlan Cox 	while (cnt.v_free_count < cnt.v_free_reserved) {
11502446e4f0SAlan Cox 		TAILQ_FOREACH(m, &vm_page_queues[PQ_CACHE].pl, pageq) {
1151df2e33bfSAlan Cox 			KASSERT(m->dirty == 0,
1152df2e33bfSAlan Cox 			    ("Found dirty cache page %p", m));
1153df2e33bfSAlan Cox 			KASSERT(!pmap_page_is_mapped(m),
1154df2e33bfSAlan Cox 			    ("Found mapped cache page %p", m));
1155df2e33bfSAlan Cox 			KASSERT((m->flags & PG_UNMANAGED) == 0,
1156df2e33bfSAlan Cox 			    ("Found unmanaged cache page %p", m));
1157df2e33bfSAlan Cox 			KASSERT(m->wire_count == 0,
1158df2e33bfSAlan Cox 			    ("Found wired cache page %p", m));
1159df2e33bfSAlan Cox 			if (m->hold_count == 0 && VM_OBJECT_TRYLOCK(object =
1160df2e33bfSAlan Cox 			    m->object)) {
11619af80719SAlan Cox 				KASSERT((m->oflags & VPO_BUSY) == 0 &&
1162df2e33bfSAlan Cox 				    m->busy == 0, ("Found busy cache page %p",
1163df2e33bfSAlan Cox 				    m));
1164ab42316cSAlan Cox 				vm_page_free(m);
1165ab42316cSAlan Cox 				VM_OBJECT_UNLOCK(object);
1166393a081dSAttilio Rao 				cnt.v_dfree++;
1167df2e33bfSAlan Cox 				break;
1168df2e33bfSAlan Cox 			}
1169df2e33bfSAlan Cox 		}
11702446e4f0SAlan Cox 		if (m == NULL)
11712446e4f0SAlan Cox 			break;
117226f9a767SRodney W. Grimes 	}
11738ffc1519SAlan Cox 	vm_page_unlock_queues();
1174ceb0cf87SJohn Dyson #if !defined(NO_SWAPPING)
1175ceb0cf87SJohn Dyson 	/*
1176ceb0cf87SJohn Dyson 	 * Idle process swapout -- run once per second.
1177ceb0cf87SJohn Dyson 	 */
1178ceb0cf87SJohn Dyson 	if (vm_swap_idle_enabled) {
1179ceb0cf87SJohn Dyson 		static long lsec;
1180227ee8a1SPoul-Henning Kamp 		if (time_second != lsec) {
118197824da3SAlan Cox 			vm_req_vmdaemon(VM_SWAP_IDLE);
1182227ee8a1SPoul-Henning Kamp 			lsec = time_second;
1183ceb0cf87SJohn Dyson 		}
1184ceb0cf87SJohn Dyson 	}
1185ceb0cf87SJohn Dyson #endif
1186ceb0cf87SJohn Dyson 
11875663e6deSDavid Greenman 	/*
1188f6b04d2bSDavid Greenman 	 * If we didn't get enough free pages, and we have skipped a vnode
11894c1f8ee9SDavid Greenman 	 * in a writeable object, wakeup the sync daemon.  And kick swapout
11904c1f8ee9SDavid Greenman 	 * if we did not get enough free pages.
1191f6b04d2bSDavid Greenman 	 */
119290ecac61SMatthew Dillon 	if (vm_paging_target() > 0) {
119390ecac61SMatthew Dillon 		if (vnodes_skipped && vm_page_count_min())
1194d50c1994SPeter Wemm 			(void) speedup_syncer();
119538efa82bSJohn Dyson #if !defined(NO_SWAPPING)
119697824da3SAlan Cox 		if (vm_swap_enabled && vm_page_count_target())
119797824da3SAlan Cox 			vm_req_vmdaemon(VM_SWAP_NORMAL);
11985afce282SDavid Greenman #endif
11994c1f8ee9SDavid Greenman 	}
12004c1f8ee9SDavid Greenman 
1201f6b04d2bSDavid Greenman 	/*
1202e92686d0SDavid Schultz 	 * If we are critically low on one of RAM or swap and low on
1203e92686d0SDavid Schultz 	 * the other, kill the largest process.  However, we avoid
1204e92686d0SDavid Schultz 	 * doing this on the first pass in order to give ourselves a
1205e92686d0SDavid Schultz 	 * chance to flush out dirty vnode-backed pages and to allow
1206e92686d0SDavid Schultz 	 * active pages to be moved to the inactive queue and reclaimed.
12071c58e4e5SJohn Baldwin 	 *
12081c58e4e5SJohn Baldwin 	 * We keep the process bigproc locked once we find it to keep anyone
12091c58e4e5SJohn Baldwin 	 * from messing with it; however, there is a possibility of
12101c58e4e5SJohn Baldwin 	 * deadlock if process B is bigproc and one of it's child processes
12111c58e4e5SJohn Baldwin 	 * attempts to propagate a signal to B while we are waiting for A's
12121c58e4e5SJohn Baldwin 	 * lock while walking this list.  To avoid this, we don't block on
12131c58e4e5SJohn Baldwin 	 * the process lock but just skip a process if it is already locked.
12145663e6deSDavid Greenman 	 */
1215e92686d0SDavid Schultz 	if (pass != 0 &&
12168f60c087SPoul-Henning Kamp 	    ((swap_pager_avail < 64 && vm_page_count_min()) ||
1217e92686d0SDavid Schultz 	     (swap_pager_full && vm_paging_target() > 0))) {
12185663e6deSDavid Greenman 		bigproc = NULL;
12195663e6deSDavid Greenman 		bigsize = 0;
12201005a129SJohn Baldwin 		sx_slock(&allproc_lock);
1221e602ba25SJulian Elischer 		FOREACH_PROC_IN_SYSTEM(p) {
1222e602ba25SJulian Elischer 			int breakout;
1223dcbcd518SBruce Evans 
12241c58e4e5SJohn Baldwin 			if (PROC_TRYLOCK(p) == 0)
12251c58e4e5SJohn Baldwin 				continue;
12261c58e4e5SJohn Baldwin 			/*
1227f4cf2141SWes Peters 			 * If this is a system or protected process, skip it.
12285663e6deSDavid Greenman 			 */
1229ef6020d1SMike Silbersack 			if ((p->p_flag & P_SYSTEM) || (p->p_pid == 1) ||
1230f4cf2141SWes Peters 			    (p->p_flag & P_PROTECTED) ||
12318f60c087SPoul-Henning Kamp 			    ((p->p_pid < 48) && (swap_pager_avail != 0))) {
12328606d880SJohn Baldwin 				PROC_UNLOCK(p);
12335663e6deSDavid Greenman 				continue;
12345663e6deSDavid Greenman 			}
12355663e6deSDavid Greenman 			/*
1236dcbcd518SBruce Evans 			 * If the process is in a non-running type state,
1237e602ba25SJulian Elischer 			 * don't touch it.  Check all the threads individually.
12385663e6deSDavid Greenman 			 */
1239982d11f8SJeff Roberson 			PROC_SLOCK(p);
1240e602ba25SJulian Elischer 			breakout = 0;
1241e602ba25SJulian Elischer 			FOREACH_THREAD_IN_PROC(p, td) {
1242982d11f8SJeff Roberson 				thread_lock(td);
124371fad9fdSJulian Elischer 				if (!TD_ON_RUNQ(td) &&
124471fad9fdSJulian Elischer 				    !TD_IS_RUNNING(td) &&
124571fad9fdSJulian Elischer 				    !TD_IS_SLEEPING(td)) {
1246982d11f8SJeff Roberson 					thread_unlock(td);
1247e602ba25SJulian Elischer 					breakout = 1;
1248e602ba25SJulian Elischer 					break;
1249e602ba25SJulian Elischer 				}
1250982d11f8SJeff Roberson 				thread_unlock(td);
1251e602ba25SJulian Elischer 			}
1252982d11f8SJeff Roberson 			PROC_SUNLOCK(p);
1253e602ba25SJulian Elischer 			if (breakout) {
12541c58e4e5SJohn Baldwin 				PROC_UNLOCK(p);
12555663e6deSDavid Greenman 				continue;
12565663e6deSDavid Greenman 			}
12575663e6deSDavid Greenman 			/*
12585663e6deSDavid Greenman 			 * get the process size
12595663e6deSDavid Greenman 			 */
126072d97679SDavid Schultz 			if (!vm_map_trylock_read(&p->p_vmspace->vm_map)) {
126172d97679SDavid Schultz 				PROC_UNLOCK(p);
126272d97679SDavid Schultz 				continue;
126372d97679SDavid Schultz 			}
126472d97679SDavid Schultz 			size = vmspace_swap_count(p->p_vmspace);
126572d97679SDavid Schultz 			vm_map_unlock_read(&p->p_vmspace->vm_map);
126672d97679SDavid Schultz 			size += vmspace_resident_count(p->p_vmspace);
12675663e6deSDavid Greenman 			/*
12685663e6deSDavid Greenman 			 * if the this process is bigger than the biggest one
12695663e6deSDavid Greenman 			 * remember it.
12705663e6deSDavid Greenman 			 */
12715663e6deSDavid Greenman 			if (size > bigsize) {
12721c58e4e5SJohn Baldwin 				if (bigproc != NULL)
12731c58e4e5SJohn Baldwin 					PROC_UNLOCK(bigproc);
12745663e6deSDavid Greenman 				bigproc = p;
12755663e6deSDavid Greenman 				bigsize = size;
12761c58e4e5SJohn Baldwin 			} else
12771c58e4e5SJohn Baldwin 				PROC_UNLOCK(p);
12785663e6deSDavid Greenman 		}
12791005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
12805663e6deSDavid Greenman 		if (bigproc != NULL) {
1281729b1e51SDavid Greenman 			killproc(bigproc, "out of swap space");
1282982d11f8SJeff Roberson 			PROC_SLOCK(bigproc);
1283fa885116SJulian Elischer 			sched_nice(bigproc, PRIO_MIN);
1284982d11f8SJeff Roberson 			PROC_SUNLOCK(bigproc);
12851c58e4e5SJohn Baldwin 			PROC_UNLOCK(bigproc);
12862feb50bfSAttilio Rao 			wakeup(&cnt.v_free_count);
12875663e6deSDavid Greenman 		}
12885663e6deSDavid Greenman 	}
128926f9a767SRodney W. Grimes }
129026f9a767SRodney W. Grimes 
1291dc2efb27SJohn Dyson /*
1292dc2efb27SJohn Dyson  * This routine tries to maintain the pseudo LRU active queue,
1293dc2efb27SJohn Dyson  * so that during long periods of time where there is no paging,
1294956f3135SPhilippe Charnier  * that some statistic accumulation still occurs.  This code
1295dc2efb27SJohn Dyson  * helps the situation where paging just starts to occur.
1296dc2efb27SJohn Dyson  */
1297dc2efb27SJohn Dyson static void
1298dc2efb27SJohn Dyson vm_pageout_page_stats()
1299dc2efb27SJohn Dyson {
1300b86e6ec0SAlan Cox 	vm_object_t object;
1301dc2efb27SJohn Dyson 	vm_page_t m,next;
1302dc2efb27SJohn Dyson 	int pcount,tpcount;		/* Number of pages to check */
1303dc2efb27SJohn Dyson 	static int fullintervalcount = 0;
1304bef608bdSJohn Dyson 	int page_shortage;
1305bef608bdSJohn Dyson 
13065e609009SAlan Cox 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
130790ecac61SMatthew Dillon 	page_shortage =
13082feb50bfSAttilio Rao 	    (cnt.v_inactive_target + cnt.v_cache_max + cnt.v_free_min) -
13092feb50bfSAttilio Rao 	    (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count);
131090ecac61SMatthew Dillon 
1311bef608bdSJohn Dyson 	if (page_shortage <= 0)
1312bef608bdSJohn Dyson 		return;
1313dc2efb27SJohn Dyson 
13142feb50bfSAttilio Rao 	pcount = cnt.v_active_count;
1315dc2efb27SJohn Dyson 	fullintervalcount += vm_pageout_stats_interval;
1316dc2efb27SJohn Dyson 	if (fullintervalcount < vm_pageout_full_stats_interval) {
13172feb50bfSAttilio Rao 		tpcount = (vm_pageout_stats_max * cnt.v_active_count) / cnt.v_page_count;
1318dc2efb27SJohn Dyson 		if (pcount > tpcount)
1319dc2efb27SJohn Dyson 			pcount = tpcount;
1320883f3caaSMatthew Dillon 	} else {
1321883f3caaSMatthew Dillon 		fullintervalcount = 0;
1322dc2efb27SJohn Dyson 	}
1323dc2efb27SJohn Dyson 
1324be72f788SAlan Cox 	m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
1325dc2efb27SJohn Dyson 	while ((m != NULL) && (pcount-- > 0)) {
13267e006499SJohn Dyson 		int actcount;
1327dc2efb27SJohn Dyson 
1328ef39c05bSAlexander Leidinger 		KASSERT(VM_PAGE_INQUEUE2(m, PQ_ACTIVE),
1329ab42316cSAlan Cox 		    ("vm_pageout_page_stats: page %p isn't active", m));
1330dc2efb27SJohn Dyson 
1331dc2efb27SJohn Dyson 		next = TAILQ_NEXT(m, pageq);
1332b86e6ec0SAlan Cox 		object = m->object;
13338dbca793STor Egge 
13348dbca793STor Egge 		if ((m->flags & PG_MARKER) != 0) {
13358dbca793STor Egge 			m = next;
13368dbca793STor Egge 			continue;
13378dbca793STor Egge 		}
13388dbca793STor Egge 		if (!VM_OBJECT_TRYLOCK(object) &&
13398dbca793STor Egge 		    !vm_pageout_fallback_object_lock(m, &next)) {
13408dbca793STor Egge 			VM_OBJECT_UNLOCK(object);
1341b86e6ec0SAlan Cox 			m = next;
1342b86e6ec0SAlan Cox 			continue;
1343b86e6ec0SAlan Cox 		}
1344b86e6ec0SAlan Cox 
1345dc2efb27SJohn Dyson 		/*
1346dc2efb27SJohn Dyson 		 * Don't deactivate pages that are busy.
1347dc2efb27SJohn Dyson 		 */
1348dc2efb27SJohn Dyson 		if ((m->busy != 0) ||
13499af80719SAlan Cox 		    (m->oflags & VPO_BUSY) ||
1350dc2efb27SJohn Dyson 		    (m->hold_count != 0)) {
1351b86e6ec0SAlan Cox 			VM_OBJECT_UNLOCK(object);
13526d03d577SMatthew Dillon 			vm_pageq_requeue(m);
1353dc2efb27SJohn Dyson 			m = next;
1354dc2efb27SJohn Dyson 			continue;
1355dc2efb27SJohn Dyson 		}
1356dc2efb27SJohn Dyson 
13577e006499SJohn Dyson 		actcount = 0;
1358dc2efb27SJohn Dyson 		if (m->flags & PG_REFERENCED) {
1359e69763a3SDoug Rabson 			vm_page_flag_clear(m, PG_REFERENCED);
13607e006499SJohn Dyson 			actcount += 1;
1361dc2efb27SJohn Dyson 		}
1362dc2efb27SJohn Dyson 
13630385347cSPeter Wemm 		actcount += pmap_ts_referenced(m);
13647e006499SJohn Dyson 		if (actcount) {
13657e006499SJohn Dyson 			m->act_count += ACT_ADVANCE + actcount;
1366dc2efb27SJohn Dyson 			if (m->act_count > ACT_MAX)
1367dc2efb27SJohn Dyson 				m->act_count = ACT_MAX;
13686d03d577SMatthew Dillon 			vm_pageq_requeue(m);
1369dc2efb27SJohn Dyson 		} else {
1370dc2efb27SJohn Dyson 			if (m->act_count == 0) {
13717e006499SJohn Dyson 				/*
13722b6b0df7SMatthew Dillon 				 * We turn off page access, so that we have
13732b6b0df7SMatthew Dillon 				 * more accurate RSS stats.  We don't do this
13742b6b0df7SMatthew Dillon 				 * in the normal page deactivation when the
13752b6b0df7SMatthew Dillon 				 * system is loaded VM wise, because the
13762b6b0df7SMatthew Dillon 				 * cost of the large number of page protect
13772b6b0df7SMatthew Dillon 				 * operations would be higher than the value
13782b6b0df7SMatthew Dillon 				 * of doing the operation.
13797e006499SJohn Dyson 				 */
13804fec79beSAlan Cox 				pmap_remove_all(m);
1381dc2efb27SJohn Dyson 				vm_page_deactivate(m);
1382dc2efb27SJohn Dyson 			} else {
1383dc2efb27SJohn Dyson 				m->act_count -= min(m->act_count, ACT_DECLINE);
13846d03d577SMatthew Dillon 				vm_pageq_requeue(m);
1385dc2efb27SJohn Dyson 			}
1386dc2efb27SJohn Dyson 		}
1387b86e6ec0SAlan Cox 		VM_OBJECT_UNLOCK(object);
1388dc2efb27SJohn Dyson 		m = next;
1389dc2efb27SJohn Dyson 	}
1390dc2efb27SJohn Dyson }
1391dc2efb27SJohn Dyson 
1392df8bae1dSRodney W. Grimes /*
1393df8bae1dSRodney W. Grimes  *	vm_pageout is the high level pageout daemon.
1394df8bae1dSRodney W. Grimes  */
13952b14f991SJulian Elischer static void
139626f9a767SRodney W. Grimes vm_pageout()
1397df8bae1dSRodney W. Grimes {
13981aab16a6SAlan Cox 	int error, pass;
13990384fff8SJason Evans 
1400df8bae1dSRodney W. Grimes 	/*
1401df8bae1dSRodney W. Grimes 	 * Initialize some paging parameters.
1402df8bae1dSRodney W. Grimes 	 */
14032feb50bfSAttilio Rao 	cnt.v_interrupt_free_min = 2;
14042feb50bfSAttilio Rao 	if (cnt.v_page_count < 2000)
1405f35329acSJohn Dyson 		vm_pageout_page_count = 8;
1406f6b04d2bSDavid Greenman 
140745ae1d91SAlan Cox 	/*
140845ae1d91SAlan Cox 	 * v_free_reserved needs to include enough for the largest
140945ae1d91SAlan Cox 	 * swap pager structures plus enough for any pv_entry structs
141045ae1d91SAlan Cox 	 * when paging.
141145ae1d91SAlan Cox 	 */
14122feb50bfSAttilio Rao 	if (cnt.v_page_count > 1024)
14132feb50bfSAttilio Rao 		cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200;
14142feb50bfSAttilio Rao 	else
14152feb50bfSAttilio Rao 		cnt.v_free_min = 4;
14162feb50bfSAttilio Rao 	cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE +
14172feb50bfSAttilio Rao 	    cnt.v_interrupt_free_min;
14182feb50bfSAttilio Rao 	cnt.v_free_reserved = vm_pageout_page_count +
14192446e4f0SAlan Cox 	    cnt.v_pageout_free_min + (cnt.v_page_count / 768);
14202feb50bfSAttilio Rao 	cnt.v_free_severe = cnt.v_free_min / 2;
14212feb50bfSAttilio Rao 	cnt.v_free_min += cnt.v_free_reserved;
14222feb50bfSAttilio Rao 	cnt.v_free_severe += cnt.v_free_reserved;
142345ae1d91SAlan Cox 
1424ed74321bSDavid Greenman 	/*
14252b6b0df7SMatthew Dillon 	 * v_free_target and v_cache_min control pageout hysteresis.  Note
14262b6b0df7SMatthew Dillon 	 * that these are more a measure of the VM cache queue hysteresis
14272b6b0df7SMatthew Dillon 	 * then the VM free queue.  Specifically, v_free_target is the
14282b6b0df7SMatthew Dillon 	 * high water mark (free+cache pages).
14292b6b0df7SMatthew Dillon 	 *
14302b6b0df7SMatthew Dillon 	 * v_free_reserved + v_cache_min (mostly means v_cache_min) is the
14312b6b0df7SMatthew Dillon 	 * low water mark, while v_free_min is the stop.  v_cache_min must
14322b6b0df7SMatthew Dillon 	 * be big enough to handle memory needs while the pageout daemon
14332b6b0df7SMatthew Dillon 	 * is signalled and run to free more pages.
1434ed74321bSDavid Greenman 	 */
14352feb50bfSAttilio Rao 	if (cnt.v_free_count > 6144)
14362feb50bfSAttilio Rao 		cnt.v_free_target = 4 * cnt.v_free_min + cnt.v_free_reserved;
14372feb50bfSAttilio Rao 	else
14382feb50bfSAttilio Rao 		cnt.v_free_target = 2 * cnt.v_free_min + cnt.v_free_reserved;
14396f2b142eSDavid Greenman 
14402feb50bfSAttilio Rao 	if (cnt.v_free_count > 2048) {
14412feb50bfSAttilio Rao 		cnt.v_cache_min = cnt.v_free_target;
14422feb50bfSAttilio Rao 		cnt.v_cache_max = 2 * cnt.v_cache_min;
14432feb50bfSAttilio Rao 		cnt.v_inactive_target = (3 * cnt.v_free_target) / 2;
14440d94caffSDavid Greenman 	} else {
14452feb50bfSAttilio Rao 		cnt.v_cache_min = 0;
14462feb50bfSAttilio Rao 		cnt.v_cache_max = 0;
14472feb50bfSAttilio Rao 		cnt.v_inactive_target = cnt.v_free_count / 4;
14480d94caffSDavid Greenman 	}
14492feb50bfSAttilio Rao 	if (cnt.v_inactive_target > cnt.v_free_count / 3)
14502feb50bfSAttilio Rao 		cnt.v_inactive_target = cnt.v_free_count / 3;
1451df8bae1dSRodney W. Grimes 
1452df8bae1dSRodney W. Grimes 	/* XXX does not really belong here */
1453df8bae1dSRodney W. Grimes 	if (vm_page_max_wired == 0)
14542feb50bfSAttilio Rao 		vm_page_max_wired = cnt.v_free_count / 3;
1455df8bae1dSRodney W. Grimes 
1456dc2efb27SJohn Dyson 	if (vm_pageout_stats_max == 0)
14572feb50bfSAttilio Rao 		vm_pageout_stats_max = cnt.v_free_target;
1458dc2efb27SJohn Dyson 
1459dc2efb27SJohn Dyson 	/*
1460dc2efb27SJohn Dyson 	 * Set interval in seconds for stats scan.
1461dc2efb27SJohn Dyson 	 */
1462dc2efb27SJohn Dyson 	if (vm_pageout_stats_interval == 0)
1463bef608bdSJohn Dyson 		vm_pageout_stats_interval = 5;
1464dc2efb27SJohn Dyson 	if (vm_pageout_full_stats_interval == 0)
1465dc2efb27SJohn Dyson 		vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4;
1466dc2efb27SJohn Dyson 
146724a1cce3SDavid Greenman 	swap_pager_swap_init();
14682b6b0df7SMatthew Dillon 	pass = 0;
1469df8bae1dSRodney W. Grimes 	/*
14700d94caffSDavid Greenman 	 * The pageout daemon is never done, so loop forever.
1471df8bae1dSRodney W. Grimes 	 */
1472df8bae1dSRodney W. Grimes 	while (TRUE) {
1473936524aaSMatthew Dillon 		/*
1474936524aaSMatthew Dillon 		 * If we have enough free memory, wakeup waiters.  Do
1475936524aaSMatthew Dillon 		 * not clear vm_pages_needed until we reach our target,
1476936524aaSMatthew Dillon 		 * otherwise we may be woken up over and over again and
1477936524aaSMatthew Dillon 		 * waste a lot of cpu.
1478936524aaSMatthew Dillon 		 */
1479e9f995d8SAlan Cox 		mtx_lock(&vm_page_queue_free_mtx);
1480936524aaSMatthew Dillon 		if (vm_pages_needed && !vm_page_count_min()) {
1481a1c0a785SAlan Cox 			if (!vm_paging_needed())
1482936524aaSMatthew Dillon 				vm_pages_needed = 0;
14832feb50bfSAttilio Rao 			wakeup(&cnt.v_free_count);
1484936524aaSMatthew Dillon 		}
1485936524aaSMatthew Dillon 		if (vm_pages_needed) {
148690ecac61SMatthew Dillon 			/*
14872b6b0df7SMatthew Dillon 			 * Still not done, take a second pass without waiting
14882b6b0df7SMatthew Dillon 			 * (unlimited dirty cleaning), otherwise sleep a bit
14892b6b0df7SMatthew Dillon 			 * and try again.
149090ecac61SMatthew Dillon 			 */
14912b6b0df7SMatthew Dillon 			++pass;
14922b6b0df7SMatthew Dillon 			if (pass > 1)
1493e9f995d8SAlan Cox 				msleep(&vm_pages_needed,
1494e9f995d8SAlan Cox 				    &vm_page_queue_free_mtx, PVM, "psleep",
1495e9f995d8SAlan Cox 				    hz / 2);
149690ecac61SMatthew Dillon 		} else {
149790ecac61SMatthew Dillon 			/*
14982b6b0df7SMatthew Dillon 			 * Good enough, sleep & handle stats.  Prime the pass
14992b6b0df7SMatthew Dillon 			 * for the next run.
150090ecac61SMatthew Dillon 			 */
15012b6b0df7SMatthew Dillon 			if (pass > 1)
15022b6b0df7SMatthew Dillon 				pass = 1;
15032b6b0df7SMatthew Dillon 			else
15042b6b0df7SMatthew Dillon 				pass = 0;
1505e9f995d8SAlan Cox 			error = msleep(&vm_pages_needed,
1506e9f995d8SAlan Cox 			    &vm_page_queue_free_mtx, PVM, "psleep",
1507e9f995d8SAlan Cox 			    vm_pageout_stats_interval * hz);
1508dc2efb27SJohn Dyson 			if (error && !vm_pages_needed) {
1509e9f995d8SAlan Cox 				mtx_unlock(&vm_page_queue_free_mtx);
15102b6b0df7SMatthew Dillon 				pass = 0;
1511e9f995d8SAlan Cox 				vm_page_lock_queues();
1512dc2efb27SJohn Dyson 				vm_pageout_page_stats();
15135e609009SAlan Cox 				vm_page_unlock_queues();
1514dc2efb27SJohn Dyson 				continue;
1515dc2efb27SJohn Dyson 			}
1516f919ebdeSDavid Greenman 		}
1517b18bfc3dSJohn Dyson 		if (vm_pages_needed)
1518393a081dSAttilio Rao 			cnt.v_pdwakeups++;
1519e9f995d8SAlan Cox 		mtx_unlock(&vm_page_queue_free_mtx);
15202b6b0df7SMatthew Dillon 		vm_pageout_scan(pass);
1521df8bae1dSRodney W. Grimes 	}
1522df8bae1dSRodney W. Grimes }
152326f9a767SRodney W. Grimes 
15246b4b77adSAlan Cox /*
1525e9f995d8SAlan Cox  * Unless the free page queue lock is held by the caller, this function
15266b4b77adSAlan Cox  * should be regarded as advisory.  Specifically, the caller should
15276b4b77adSAlan Cox  * not msleep() on &cnt.v_free_count following this function unless
1528e9f995d8SAlan Cox  * the free page queue lock is held until the msleep() is performed.
15296b4b77adSAlan Cox  */
1530e0c5a895SJohn Dyson void
1531e0c5a895SJohn Dyson pagedaemon_wakeup()
1532e0c5a895SJohn Dyson {
1533a1c0a785SAlan Cox 
1534b40ce416SJulian Elischer 	if (!vm_pages_needed && curthread->td_proc != pageproc) {
1535a1c0a785SAlan Cox 		vm_pages_needed = 1;
1536e0c5a895SJohn Dyson 		wakeup(&vm_pages_needed);
1537e0c5a895SJohn Dyson 	}
1538e0c5a895SJohn Dyson }
1539e0c5a895SJohn Dyson 
154038efa82bSJohn Dyson #if !defined(NO_SWAPPING)
15415afce282SDavid Greenman static void
154297824da3SAlan Cox vm_req_vmdaemon(int req)
15435afce282SDavid Greenman {
15445afce282SDavid Greenman 	static int lastrun = 0;
15455afce282SDavid Greenman 
154697824da3SAlan Cox 	mtx_lock(&vm_daemon_mtx);
154797824da3SAlan Cox 	vm_pageout_req_swapout |= req;
1548b18bfc3dSJohn Dyson 	if ((ticks > (lastrun + hz)) || (ticks < lastrun)) {
15495afce282SDavid Greenman 		wakeup(&vm_daemon_needed);
15505afce282SDavid Greenman 		lastrun = ticks;
15515afce282SDavid Greenman 	}
155297824da3SAlan Cox 	mtx_unlock(&vm_daemon_mtx);
15535afce282SDavid Greenman }
15545afce282SDavid Greenman 
15552b14f991SJulian Elischer static void
15564f9fb771SBruce Evans vm_daemon()
15570d94caffSDavid Greenman {
155891d5354aSJohn Baldwin 	struct rlimit rsslim;
1559dcbcd518SBruce Evans 	struct proc *p;
1560dcbcd518SBruce Evans 	struct thread *td;
156197824da3SAlan Cox 	int breakout, swapout_flags;
15620d94caffSDavid Greenman 
15632fe6e4d7SDavid Greenman 	while (TRUE) {
156497824da3SAlan Cox 		mtx_lock(&vm_daemon_mtx);
156597824da3SAlan Cox 		msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", 0);
156697824da3SAlan Cox 		swapout_flags = vm_pageout_req_swapout;
15674c1f8ee9SDavid Greenman 		vm_pageout_req_swapout = 0;
156897824da3SAlan Cox 		mtx_unlock(&vm_daemon_mtx);
156997824da3SAlan Cox 		if (swapout_flags)
157097824da3SAlan Cox 			swapout_procs(swapout_flags);
157197824da3SAlan Cox 
15722fe6e4d7SDavid Greenman 		/*
15730d94caffSDavid Greenman 		 * scan the processes for exceeding their rlimits or if
15740d94caffSDavid Greenman 		 * process is swapped out -- deactivate pages
15752fe6e4d7SDavid Greenman 		 */
15761005a129SJohn Baldwin 		sx_slock(&allproc_lock);
1577f67af5c9SXin LI 		FOREACH_PROC_IN_SYSTEM(p) {
1578fe2144fdSLuoqi Chen 			vm_pindex_t limit, size;
15792fe6e4d7SDavid Greenman 
15802fe6e4d7SDavid Greenman 			/*
15812fe6e4d7SDavid Greenman 			 * if this is a system process or if we have already
15822fe6e4d7SDavid Greenman 			 * looked at this process, skip it.
15832fe6e4d7SDavid Greenman 			 */
1584897ecacdSJohn Baldwin 			PROC_LOCK(p);
15852fe6e4d7SDavid Greenman 			if (p->p_flag & (P_SYSTEM | P_WEXIT)) {
1586897ecacdSJohn Baldwin 				PROC_UNLOCK(p);
15872fe6e4d7SDavid Greenman 				continue;
15882fe6e4d7SDavid Greenman 			}
15892fe6e4d7SDavid Greenman 			/*
15902fe6e4d7SDavid Greenman 			 * if the process is in a non-running type state,
15912fe6e4d7SDavid Greenman 			 * don't touch it.
15922fe6e4d7SDavid Greenman 			 */
1593982d11f8SJeff Roberson 			PROC_SLOCK(p);
1594e602ba25SJulian Elischer 			breakout = 0;
1595e602ba25SJulian Elischer 			FOREACH_THREAD_IN_PROC(p, td) {
1596982d11f8SJeff Roberson 				thread_lock(td);
159771fad9fdSJulian Elischer 				if (!TD_ON_RUNQ(td) &&
159871fad9fdSJulian Elischer 				    !TD_IS_RUNNING(td) &&
159971fad9fdSJulian Elischer 				    !TD_IS_SLEEPING(td)) {
1600982d11f8SJeff Roberson 					thread_unlock(td);
1601e602ba25SJulian Elischer 					breakout = 1;
1602e602ba25SJulian Elischer 					break;
1603e602ba25SJulian Elischer 				}
1604982d11f8SJeff Roberson 				thread_unlock(td);
1605e602ba25SJulian Elischer 			}
1606982d11f8SJeff Roberson 			PROC_SUNLOCK(p);
1607897ecacdSJohn Baldwin 			if (breakout) {
1608897ecacdSJohn Baldwin 				PROC_UNLOCK(p);
16092fe6e4d7SDavid Greenman 				continue;
16102fe6e4d7SDavid Greenman 			}
16112fe6e4d7SDavid Greenman 			/*
16122fe6e4d7SDavid Greenman 			 * get a limit
16132fe6e4d7SDavid Greenman 			 */
1614dcbcd518SBruce Evans 			lim_rlimit(p, RLIMIT_RSS, &rsslim);
1615fe2144fdSLuoqi Chen 			limit = OFF_TO_IDX(
161691d5354aSJohn Baldwin 			    qmin(rsslim.rlim_cur, rsslim.rlim_max));
16172fe6e4d7SDavid Greenman 
16182fe6e4d7SDavid Greenman 			/*
16190d94caffSDavid Greenman 			 * let processes that are swapped out really be
16200d94caffSDavid Greenman 			 * swapped out set the limit to nothing (will force a
16210d94caffSDavid Greenman 			 * swap-out.)
16222fe6e4d7SDavid Greenman 			 */
16238606d880SJohn Baldwin 			if ((p->p_sflag & PS_INMEM) == 0)
16240d94caffSDavid Greenman 				limit = 0;	/* XXX */
1625897ecacdSJohn Baldwin 			PROC_UNLOCK(p);
16262fe6e4d7SDavid Greenman 
1627fe2144fdSLuoqi Chen 			size = vmspace_resident_count(p->p_vmspace);
16282fe6e4d7SDavid Greenman 			if (limit >= 0 && size >= limit) {
1629fe2144fdSLuoqi Chen 				vm_pageout_map_deactivate_pages(
1630fe2144fdSLuoqi Chen 				    &p->p_vmspace->vm_map, limit);
16312fe6e4d7SDavid Greenman 			}
16322fe6e4d7SDavid Greenman 		}
16331005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
163424a1cce3SDavid Greenman 	}
16352fe6e4d7SDavid Greenman }
1636a1287949SEivind Eklund #endif			/* !defined(NO_SWAPPING) */
1637