xref: /freebsd/sys/vm/vm_pageout.c (revision 2025d69ba7a68a5af173007a8072c45ad797ea23)
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 };
126237fdd78SRobert Watson SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start,
127237fdd78SRobert Watson     &page_kp);
1282b14f991SJulian Elischer 
12938efa82bSJohn Dyson #if !defined(NO_SWAPPING)
1302b14f991SJulian Elischer /* the kernel process "vm_daemon"*/
13111caded3SAlfred Perlstein static void vm_daemon(void);
132f708ef1bSPoul-Henning Kamp static struct	proc *vmproc;
1332b14f991SJulian Elischer 
1342b14f991SJulian Elischer static struct kproc_desc vm_kp = {
1352b14f991SJulian Elischer 	"vmdaemon",
1362b14f991SJulian Elischer 	vm_daemon,
1372b14f991SJulian Elischer 	&vmproc
1382b14f991SJulian Elischer };
139237fdd78SRobert Watson SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp);
14038efa82bSJohn Dyson #endif
1412b14f991SJulian Elischer 
1422b14f991SJulian Elischer 
1438b245767SAlan Cox int vm_pages_needed;		/* Event on which pageout daemon sleeps */
1448b245767SAlan Cox int vm_pageout_deficit;		/* Estimated number of pages deficit */
1458b245767SAlan Cox int vm_pageout_pages_needed;	/* flag saying that the pageout daemon needs pages */
14626f9a767SRodney W. Grimes 
14738efa82bSJohn Dyson #if !defined(NO_SWAPPING)
148f708ef1bSPoul-Henning Kamp static int vm_pageout_req_swapout;	/* XXX */
149f708ef1bSPoul-Henning Kamp static int vm_daemon_needed;
15097824da3SAlan Cox static struct mtx vm_daemon_mtx;
15197824da3SAlan Cox /* Allow for use by vm_pageout before vm_daemon is initialized. */
15297824da3SAlan Cox MTX_SYSINIT(vm_daemon, &vm_daemon_mtx, "vm daemon", MTX_DEF);
15338efa82bSJohn Dyson #endif
1542b6b0df7SMatthew Dillon static int vm_max_launder = 32;
155303b270bSEivind Eklund static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0;
156303b270bSEivind Eklund static int vm_pageout_full_stats_interval = 0;
15726354d4cSAlan Cox static int vm_pageout_algorithm=0;
158303b270bSEivind Eklund static int defer_swap_pageouts=0;
159303b270bSEivind Eklund static int disable_swap_pageouts=0;
16070111b90SJohn Dyson 
16138efa82bSJohn Dyson #if defined(NO_SWAPPING)
162303b270bSEivind Eklund static int vm_swap_enabled=0;
163303b270bSEivind Eklund static int vm_swap_idle_enabled=0;
16438efa82bSJohn Dyson #else
165303b270bSEivind Eklund static int vm_swap_enabled=1;
166303b270bSEivind Eklund static int vm_swap_idle_enabled=0;
16738efa82bSJohn Dyson #endif
16838efa82bSJohn Dyson 
16938efa82bSJohn Dyson SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm,
1702b6b0df7SMatthew Dillon 	CTLFLAG_RW, &vm_pageout_algorithm, 0, "LRU page mgmt");
1712b6b0df7SMatthew Dillon 
1722b6b0df7SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, max_launder,
1732b6b0df7SMatthew Dillon 	CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout");
17438efa82bSJohn Dyson 
175dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max,
176b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length");
177dc2efb27SJohn Dyson 
178dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval,
179b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan");
180dc2efb27SJohn Dyson 
181dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval,
182b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan");
183dc2efb27SJohn Dyson 
18438efa82bSJohn Dyson #if defined(NO_SWAPPING)
185ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
1866bd9cb1cSTom Rhodes 	CTLFLAG_RD, &vm_swap_enabled, 0, "Enable entire process swapout");
187ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
1886bd9cb1cSTom Rhodes 	CTLFLAG_RD, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
18938efa82bSJohn Dyson #else
190ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
191b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout");
192ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
193b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
19438efa82bSJohn Dyson #endif
19526f9a767SRodney W. Grimes 
196ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts,
197b0359e2cSPeter Wemm 	CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem");
19812ac6a1dSJohn Dyson 
199ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts,
200b0359e2cSPeter Wemm 	CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages");
20112ac6a1dSJohn Dyson 
20223b59018SMatthew Dillon static int pageout_lock_miss;
20323b59018SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss,
20423b59018SMatthew Dillon 	CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout");
20523b59018SMatthew Dillon 
206ffc82b0aSJohn Dyson #define VM_PAGEOUT_PAGE_COUNT 16
207bbc0ec52SDavid Greenman int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT;
208df8bae1dSRodney W. Grimes 
209c3cb3e12SDavid Greenman int vm_page_max_wired;		/* XXX max # of wired pages system-wide */
2105dfc2870SAlan Cox SYSCTL_INT(_vm, OID_AUTO, max_wired,
2115dfc2870SAlan Cox 	CTLFLAG_RW, &vm_page_max_wired, 0, "System-wide limit to wired page count");
212df8bae1dSRodney W. Grimes 
21338efa82bSJohn Dyson #if !defined(NO_SWAPPING)
214ecf6279fSAlan Cox static void vm_pageout_map_deactivate_pages(vm_map_t, long);
215ecf6279fSAlan Cox static void vm_pageout_object_deactivate_pages(pmap_t, vm_object_t, long);
21697824da3SAlan Cox static void vm_req_vmdaemon(int req);
21738efa82bSJohn Dyson #endif
218dc2efb27SJohn Dyson static void vm_pageout_page_stats(void);
219cd41fc12SDavid Greenman 
22026f9a767SRodney W. Grimes /*
2218dbca793STor Egge  * vm_pageout_fallback_object_lock:
2228dbca793STor Egge  *
2238dbca793STor Egge  * Lock vm object currently associated with `m'. VM_OBJECT_TRYLOCK is
2248dbca793STor Egge  * known to have failed and page queue must be either PQ_ACTIVE or
2258dbca793STor Egge  * PQ_INACTIVE.  To avoid lock order violation, unlock the page queues
2268dbca793STor Egge  * while locking the vm object.  Use marker page to detect page queue
2278dbca793STor Egge  * changes and maintain notion of next page on page queue.  Return
2288dbca793STor Egge  * TRUE if no changes were detected, FALSE otherwise.  vm object is
2298dbca793STor Egge  * locked on return.
2308dbca793STor Egge  *
2318dbca793STor Egge  * This function depends on both the lock portion of struct vm_object
2328dbca793STor Egge  * and normal struct vm_page being type stable.
2338dbca793STor Egge  */
234da31e3aaSAlan Cox boolean_t
2358dbca793STor Egge vm_pageout_fallback_object_lock(vm_page_t m, vm_page_t *next)
2368dbca793STor Egge {
2378dbca793STor Egge 	struct vm_page marker;
2388dbca793STor Egge 	boolean_t unchanged;
2398dbca793STor Egge 	u_short queue;
2408dbca793STor Egge 	vm_object_t object;
2418dbca793STor Egge 
2428dbca793STor Egge 	/*
2438dbca793STor Egge 	 * Initialize our marker
2448dbca793STor Egge 	 */
2458dbca793STor Egge 	bzero(&marker, sizeof(marker));
2469af80719SAlan Cox 	marker.flags = PG_FICTITIOUS | PG_MARKER;
2479af80719SAlan Cox 	marker.oflags = VPO_BUSY;
2488dbca793STor Egge 	marker.queue = m->queue;
2498dbca793STor Egge 	marker.wire_count = 1;
2508dbca793STor Egge 
2518dbca793STor Egge 	queue = m->queue;
2528dbca793STor Egge 	object = m->object;
2538dbca793STor Egge 
2548dbca793STor Egge 	TAILQ_INSERT_AFTER(&vm_page_queues[queue].pl,
2558dbca793STor Egge 			   m, &marker, pageq);
2568dbca793STor Egge 	vm_page_unlock_queues();
2578dbca793STor Egge 	VM_OBJECT_LOCK(object);
2588dbca793STor Egge 	vm_page_lock_queues();
2598dbca793STor Egge 
2608dbca793STor Egge 	/* Page queue might have changed. */
2618dbca793STor Egge 	*next = TAILQ_NEXT(&marker, pageq);
2628dbca793STor Egge 	unchanged = (m->queue == queue &&
2638dbca793STor Egge 		     m->object == object &&
2648dbca793STor Egge 		     &marker == TAILQ_NEXT(m, pageq));
2658dbca793STor Egge 	TAILQ_REMOVE(&vm_page_queues[queue].pl,
2668dbca793STor Egge 		     &marker, pageq);
2678dbca793STor Egge 	return (unchanged);
2688dbca793STor Egge }
2698dbca793STor Egge 
2708dbca793STor Egge /*
27126f9a767SRodney W. Grimes  * vm_pageout_clean:
27224a1cce3SDavid Greenman  *
2730d94caffSDavid Greenman  * Clean the page and remove it from the laundry.
27426f9a767SRodney W. Grimes  *
2750d94caffSDavid Greenman  * We set the busy bit to cause potential page faults on this page to
2761c7c3c6aSMatthew Dillon  * block.  Note the careful timing, however, the busy bit isn't set till
2771c7c3c6aSMatthew Dillon  * late and we cannot do anything that will mess with the page.
27826f9a767SRodney W. Grimes  */
2793af76890SPoul-Henning Kamp static int
2808f9110f6SJohn Dyson vm_pageout_clean(m)
28124a1cce3SDavid Greenman 	vm_page_t m;
28224a1cce3SDavid Greenman {
28354d92145SMatthew Dillon 	vm_object_t object;
284f35329acSJohn Dyson 	vm_page_t mc[2*vm_pageout_page_count];
2853562af12SAlan Cox 	int pageout_count;
28690ecac61SMatthew Dillon 	int ib, is, page_base;
287a316d390SJohn Dyson 	vm_pindex_t pindex = m->pindex;
28826f9a767SRodney W. Grimes 
28955df3298SAlan Cox 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2903562af12SAlan Cox 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2910cddd8f0SMatthew Dillon 
29226f9a767SRodney W. Grimes 	/*
2931c7c3c6aSMatthew Dillon 	 * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP
2941c7c3c6aSMatthew Dillon 	 * with the new swapper, but we could have serious problems paging
2951c7c3c6aSMatthew Dillon 	 * out other object types if there is insufficient memory.
2961c7c3c6aSMatthew Dillon 	 *
2971c7c3c6aSMatthew Dillon 	 * Unfortunately, checking free memory here is far too late, so the
2981c7c3c6aSMatthew Dillon 	 * check has been moved up a procedural level.
2991c7c3c6aSMatthew Dillon 	 */
3001c7c3c6aSMatthew Dillon 
30124a1cce3SDavid Greenman 	/*
3029e897b1bSAlan Cox 	 * Can't clean the page if it's busy or held.
30324a1cce3SDavid Greenman 	 */
3048f9110f6SJohn Dyson 	if ((m->hold_count != 0) ||
3059e897b1bSAlan Cox 	    ((m->busy != 0) || (m->oflags & VPO_BUSY))) {
3060d94caffSDavid Greenman 		return 0;
3078b03c8edSMatthew Dillon 	}
3080d94caffSDavid Greenman 
309f35329acSJohn Dyson 	mc[vm_pageout_page_count] = m;
31026f9a767SRodney W. Grimes 	pageout_count = 1;
311f35329acSJohn Dyson 	page_base = vm_pageout_page_count;
31290ecac61SMatthew Dillon 	ib = 1;
31390ecac61SMatthew Dillon 	is = 1;
31490ecac61SMatthew Dillon 
31524a1cce3SDavid Greenman 	/*
31624a1cce3SDavid Greenman 	 * Scan object for clusterable pages.
31724a1cce3SDavid Greenman 	 *
31824a1cce3SDavid Greenman 	 * We can cluster ONLY if: ->> the page is NOT
31924a1cce3SDavid Greenman 	 * clean, wired, busy, held, or mapped into a
32024a1cce3SDavid Greenman 	 * buffer, and one of the following:
32124a1cce3SDavid Greenman 	 * 1) The page is inactive, or a seldom used
32224a1cce3SDavid Greenman 	 *    active page.
32324a1cce3SDavid Greenman 	 * -or-
32424a1cce3SDavid Greenman 	 * 2) we force the issue.
32590ecac61SMatthew Dillon 	 *
32690ecac61SMatthew Dillon 	 * During heavy mmap/modification loads the pageout
32790ecac61SMatthew Dillon 	 * daemon can really fragment the underlying file
32890ecac61SMatthew Dillon 	 * due to flushing pages out of order and not trying
32990ecac61SMatthew Dillon 	 * align the clusters (which leave sporatic out-of-order
33090ecac61SMatthew Dillon 	 * holes).  To solve this problem we do the reverse scan
33190ecac61SMatthew Dillon 	 * first and attempt to align our cluster, then do a
33290ecac61SMatthew Dillon 	 * forward scan if room remains.
33324a1cce3SDavid Greenman 	 */
3345163584cSAlan Cox 	object = m->object;
33590ecac61SMatthew Dillon more:
33690ecac61SMatthew Dillon 	while (ib && pageout_count < vm_pageout_page_count) {
33724a1cce3SDavid Greenman 		vm_page_t p;
338f6b04d2bSDavid Greenman 
33990ecac61SMatthew Dillon 		if (ib > pindex) {
34090ecac61SMatthew Dillon 			ib = 0;
34190ecac61SMatthew Dillon 			break;
342f6b04d2bSDavid Greenman 		}
34390ecac61SMatthew Dillon 
34490ecac61SMatthew Dillon 		if ((p = vm_page_lookup(object, pindex - ib)) == NULL) {
34590ecac61SMatthew Dillon 			ib = 0;
34690ecac61SMatthew Dillon 			break;
34790ecac61SMatthew Dillon 		}
3487bfda801SAlan Cox 		if ((p->oflags & VPO_BUSY) || p->busy) {
34990ecac61SMatthew Dillon 			ib = 0;
35090ecac61SMatthew Dillon 			break;
351f6b04d2bSDavid Greenman 		}
35224a1cce3SDavid Greenman 		vm_page_test_dirty(p);
35390ecac61SMatthew Dillon 		if ((p->dirty & p->valid) == 0 ||
35490ecac61SMatthew Dillon 		    p->queue != PQ_INACTIVE ||
35557601bcbSMatthew Dillon 		    p->wire_count != 0 ||	/* may be held by buf cache */
35657601bcbSMatthew Dillon 		    p->hold_count != 0) {	/* may be undergoing I/O */
35790ecac61SMatthew Dillon 			ib = 0;
35824a1cce3SDavid Greenman 			break;
359f6b04d2bSDavid Greenman 		}
36090ecac61SMatthew Dillon 		mc[--page_base] = p;
36190ecac61SMatthew Dillon 		++pageout_count;
36290ecac61SMatthew Dillon 		++ib;
36324a1cce3SDavid Greenman 		/*
36490ecac61SMatthew Dillon 		 * alignment boundry, stop here and switch directions.  Do
36590ecac61SMatthew Dillon 		 * not clear ib.
36624a1cce3SDavid Greenman 		 */
36790ecac61SMatthew Dillon 		if ((pindex - (ib - 1)) % vm_pageout_page_count == 0)
36890ecac61SMatthew Dillon 			break;
36924a1cce3SDavid Greenman 	}
37090ecac61SMatthew Dillon 
37190ecac61SMatthew Dillon 	while (pageout_count < vm_pageout_page_count &&
37290ecac61SMatthew Dillon 	    pindex + is < object->size) {
37390ecac61SMatthew Dillon 		vm_page_t p;
37490ecac61SMatthew Dillon 
37590ecac61SMatthew Dillon 		if ((p = vm_page_lookup(object, pindex + is)) == NULL)
37690ecac61SMatthew Dillon 			break;
3777bfda801SAlan Cox 		if ((p->oflags & VPO_BUSY) || p->busy) {
37890ecac61SMatthew Dillon 			break;
37924a1cce3SDavid Greenman 		}
38024a1cce3SDavid Greenman 		vm_page_test_dirty(p);
38190ecac61SMatthew Dillon 		if ((p->dirty & p->valid) == 0 ||
38290ecac61SMatthew Dillon 		    p->queue != PQ_INACTIVE ||
38357601bcbSMatthew Dillon 		    p->wire_count != 0 ||	/* may be held by buf cache */
38457601bcbSMatthew Dillon 		    p->hold_count != 0) {	/* may be undergoing I/O */
38524a1cce3SDavid Greenman 			break;
38624a1cce3SDavid Greenman 		}
38790ecac61SMatthew Dillon 		mc[page_base + pageout_count] = p;
38890ecac61SMatthew Dillon 		++pageout_count;
38990ecac61SMatthew Dillon 		++is;
39024a1cce3SDavid Greenman 	}
39190ecac61SMatthew Dillon 
39290ecac61SMatthew Dillon 	/*
39390ecac61SMatthew Dillon 	 * If we exhausted our forward scan, continue with the reverse scan
39490ecac61SMatthew Dillon 	 * when possible, even past a page boundry.  This catches boundry
39590ecac61SMatthew Dillon 	 * conditions.
39690ecac61SMatthew Dillon 	 */
39790ecac61SMatthew Dillon 	if (ib && pageout_count < vm_pageout_page_count)
39890ecac61SMatthew Dillon 		goto more;
399f6b04d2bSDavid Greenman 
40067bf6868SJohn Dyson 	/*
40167bf6868SJohn Dyson 	 * we allow reads during pageouts...
40267bf6868SJohn Dyson 	 */
4037a935082SAlan Cox 	return (vm_pageout_flush(&mc[page_base], pageout_count, 0));
404aef922f5SJohn Dyson }
405aef922f5SJohn Dyson 
4061c7c3c6aSMatthew Dillon /*
4071c7c3c6aSMatthew Dillon  * vm_pageout_flush() - launder the given pages
4081c7c3c6aSMatthew Dillon  *
4091c7c3c6aSMatthew Dillon  *	The given pages are laundered.  Note that we setup for the start of
4101c7c3c6aSMatthew Dillon  *	I/O ( i.e. busy the page ), mark it read-only, and bump the object
4111c7c3c6aSMatthew Dillon  *	reference count all in here rather then in the parent.  If we want
4121c7c3c6aSMatthew Dillon  *	the parent to do more sophisticated things we may have to change
4131c7c3c6aSMatthew Dillon  *	the ordering.
4141c7c3c6aSMatthew Dillon  */
415aef922f5SJohn Dyson int
4167a935082SAlan Cox vm_pageout_flush(vm_page_t *mc, int count, int flags)
417aef922f5SJohn Dyson {
4182e3b314dSAlan Cox 	vm_object_t object = mc[0]->object;
419aef922f5SJohn Dyson 	int pageout_status[count];
42095461b45SJohn Dyson 	int numpagedout = 0;
421aef922f5SJohn Dyson 	int i;
422aef922f5SJohn Dyson 
42355df3298SAlan Cox 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
4242e3b314dSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
4251c7c3c6aSMatthew Dillon 	/*
4261c7c3c6aSMatthew Dillon 	 * Initiate I/O.  Bump the vm_page_t->busy counter and
4271c7c3c6aSMatthew Dillon 	 * mark the pages read-only.
4281c7c3c6aSMatthew Dillon 	 *
4291c7c3c6aSMatthew Dillon 	 * We do not have to fixup the clean/dirty bits here... we can
4301c7c3c6aSMatthew Dillon 	 * allow the pager to do it after the I/O completes.
43102fa91d3SMatthew Dillon 	 *
43202fa91d3SMatthew Dillon 	 * NOTE! mc[i]->dirty may be partial or fragmented due to an
43302fa91d3SMatthew Dillon 	 * edge case with file fragments.
4341c7c3c6aSMatthew Dillon 	 */
4358f9110f6SJohn Dyson 	for (i = 0; i < count; i++) {
4367a935082SAlan Cox 		KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL,
4377a935082SAlan Cox 		    ("vm_pageout_flush: partially invalid page %p index %d/%d",
4387a935082SAlan Cox 			mc[i], i, count));
439e69763a3SDoug Rabson 		vm_page_io_start(mc[i]);
44078985e42SAlan Cox 		pmap_remove_write(mc[i]);
4418f9110f6SJohn Dyson 	}
44255df3298SAlan Cox 	vm_page_unlock_queues();
443d474eaaaSDoug Rabson 	vm_object_pip_add(object, count);
444aef922f5SJohn Dyson 
445d076fbeaSAlan Cox 	vm_pager_put_pages(object, mc, count, flags, pageout_status);
44626f9a767SRodney W. Grimes 
44740eab1e9SAlan Cox 	vm_page_lock_queues();
448aef922f5SJohn Dyson 	for (i = 0; i < count; i++) {
449aef922f5SJohn Dyson 		vm_page_t mt = mc[i];
45024a1cce3SDavid Greenman 
4514cd45723SAlan Cox 		KASSERT(pageout_status[i] == VM_PAGER_PEND ||
4524cd45723SAlan Cox 		    (mt->flags & PG_WRITEABLE) == 0,
4539ea8d1a6SAlan Cox 		    ("vm_pageout_flush: page %p is not write protected", mt));
45426f9a767SRodney W. Grimes 		switch (pageout_status[i]) {
45526f9a767SRodney W. Grimes 		case VM_PAGER_OK:
45626f9a767SRodney W. Grimes 		case VM_PAGER_PEND:
45795461b45SJohn Dyson 			numpagedout++;
45826f9a767SRodney W. Grimes 			break;
45926f9a767SRodney W. Grimes 		case VM_PAGER_BAD:
46026f9a767SRodney W. Grimes 			/*
4610d94caffSDavid Greenman 			 * Page outside of range of object. Right now we
4620d94caffSDavid Greenman 			 * essentially lose the changes by pretending it
4630d94caffSDavid Greenman 			 * worked.
46426f9a767SRodney W. Grimes 			 */
4650385347cSPeter Wemm 			pmap_clear_modify(mt);
46690ecac61SMatthew Dillon 			vm_page_undirty(mt);
46726f9a767SRodney W. Grimes 			break;
46826f9a767SRodney W. Grimes 		case VM_PAGER_ERROR:
46926f9a767SRodney W. Grimes 		case VM_PAGER_FAIL:
47026f9a767SRodney W. Grimes 			/*
4710d94caffSDavid Greenman 			 * If page couldn't be paged out, then reactivate the
4720d94caffSDavid Greenman 			 * page so it doesn't clog the inactive list.  (We
4730d94caffSDavid Greenman 			 * will try paging out it again later).
47426f9a767SRodney W. Grimes 			 */
47524a1cce3SDavid Greenman 			vm_page_activate(mt);
47626f9a767SRodney W. Grimes 			break;
47726f9a767SRodney W. Grimes 		case VM_PAGER_AGAIN:
47826f9a767SRodney W. Grimes 			break;
47926f9a767SRodney W. Grimes 		}
48026f9a767SRodney W. Grimes 
48126f9a767SRodney W. Grimes 		/*
4820d94caffSDavid Greenman 		 * If the operation is still going, leave the page busy to
4830d94caffSDavid Greenman 		 * block all other accesses. Also, leave the paging in
4840d94caffSDavid Greenman 		 * progress indicator set so that we don't attempt an object
4850d94caffSDavid Greenman 		 * collapse.
48626f9a767SRodney W. Grimes 		 */
48726f9a767SRodney W. Grimes 		if (pageout_status[i] != VM_PAGER_PEND) {
488f919ebdeSDavid Greenman 			vm_object_pip_wakeup(object);
489e69763a3SDoug Rabson 			vm_page_io_finish(mt);
4909ea8d1a6SAlan Cox 			if (vm_page_count_severe())
4919ea8d1a6SAlan Cox 				vm_page_try_to_cache(mt);
49226f9a767SRodney W. Grimes 		}
49326f9a767SRodney W. Grimes 	}
49495461b45SJohn Dyson 	return numpagedout;
49526f9a767SRodney W. Grimes }
49626f9a767SRodney W. Grimes 
49738efa82bSJohn Dyson #if !defined(NO_SWAPPING)
49826f9a767SRodney W. Grimes /*
49926f9a767SRodney W. Grimes  *	vm_pageout_object_deactivate_pages
50026f9a767SRodney W. Grimes  *
50126f9a767SRodney W. Grimes  *	deactivate enough pages to satisfy the inactive target
50226f9a767SRodney W. Grimes  *	requirements or if vm_page_proc_limit is set, then
50326f9a767SRodney W. Grimes  *	deactivate all of the pages in the object and its
50424a1cce3SDavid Greenman  *	backing_objects.
50526f9a767SRodney W. Grimes  *
50626f9a767SRodney W. Grimes  *	The object and map must be locked.
50726f9a767SRodney W. Grimes  */
50838efa82bSJohn Dyson static void
509ecf6279fSAlan Cox vm_pageout_object_deactivate_pages(pmap, first_object, desired)
510ecf6279fSAlan Cox 	pmap_t pmap;
511ecf6279fSAlan Cox 	vm_object_t first_object;
512ecf6279fSAlan Cox 	long desired;
51326f9a767SRodney W. Grimes {
514ecf6279fSAlan Cox 	vm_object_t backing_object, object;
51554d92145SMatthew Dillon 	vm_page_t p, next;
516ce18aebdSAlan Cox 	int actcount, rcount, remove_mode;
51726f9a767SRodney W. Grimes 
518ecf6279fSAlan Cox 	VM_OBJECT_LOCK_ASSERT(first_object, MA_OWNED);
519ecf6279fSAlan Cox 	if (first_object->type == OBJT_DEVICE || first_object->type == OBJT_PHYS)
52038efa82bSJohn Dyson 		return;
521ecf6279fSAlan Cox 	for (object = first_object;; object = backing_object) {
522ecf6279fSAlan Cox 		if (pmap_resident_count(pmap) <= desired)
523ecf6279fSAlan Cox 			goto unlock_return;
52424a1cce3SDavid Greenman 		if (object->paging_in_progress)
525ecf6279fSAlan Cox 			goto unlock_return;
52626f9a767SRodney W. Grimes 
52785b1dc89SAlan Cox 		remove_mode = 0;
52838efa82bSJohn Dyson 		if (object->shadow_count > 1)
52938efa82bSJohn Dyson 			remove_mode = 1;
53026f9a767SRodney W. Grimes 		/*
53126f9a767SRodney W. Grimes 		 * scan the objects entire memory queue
53226f9a767SRodney W. Grimes 		 */
53326f9a767SRodney W. Grimes 		rcount = object->resident_page_count;
534b18bfc3dSJohn Dyson 		p = TAILQ_FIRST(&object->memq);
535ce18aebdSAlan Cox 		vm_page_lock_queues();
53626f9a767SRodney W. Grimes 		while (p && (rcount-- > 0)) {
537ecf6279fSAlan Cox 			if (pmap_resident_count(pmap) <= desired) {
538ce18aebdSAlan Cox 				vm_page_unlock_queues();
539ecf6279fSAlan Cox 				goto unlock_return;
540ce18aebdSAlan Cox 			}
541b18bfc3dSJohn Dyson 			next = TAILQ_NEXT(p, listq);
542393a081dSAttilio Rao 			cnt.v_pdpages++;
5430d94caffSDavid Greenman 			if (p->wire_count != 0 ||
5440d94caffSDavid Greenman 			    p->hold_count != 0 ||
5450d94caffSDavid Greenman 			    p->busy != 0 ||
5469af80719SAlan Cox 			    (p->oflags & VPO_BUSY) ||
5479af80719SAlan Cox 			    (p->flags & PG_UNMANAGED) ||
548ecf6279fSAlan Cox 			    !pmap_page_exists_quick(pmap, p)) {
5490d94caffSDavid Greenman 				p = next;
5500d94caffSDavid Greenman 				continue;
5510d94caffSDavid Greenman 			}
5520385347cSPeter Wemm 			actcount = pmap_ts_referenced(p);
5537e006499SJohn Dyson 			if (actcount) {
554e69763a3SDoug Rabson 				vm_page_flag_set(p, PG_REFERENCED);
555c8c4b40cSJohn Dyson 			} else if (p->flags & PG_REFERENCED) {
5567e006499SJohn Dyson 				actcount = 1;
557ef743ce6SJohn Dyson 			}
55838efa82bSJohn Dyson 			if ((p->queue != PQ_ACTIVE) &&
55938efa82bSJohn Dyson 				(p->flags & PG_REFERENCED)) {
560ef743ce6SJohn Dyson 				vm_page_activate(p);
5617e006499SJohn Dyson 				p->act_count += actcount;
562e69763a3SDoug Rabson 				vm_page_flag_clear(p, PG_REFERENCED);
563c8c4b40cSJohn Dyson 			} else if (p->queue == PQ_ACTIVE) {
564ef743ce6SJohn Dyson 				if ((p->flags & PG_REFERENCED) == 0) {
565c8c4b40cSJohn Dyson 					p->act_count -= min(p->act_count, ACT_DECLINE);
5662b6b0df7SMatthew Dillon 					if (!remove_mode && (vm_pageout_algorithm || (p->act_count == 0))) {
5674fec79beSAlan Cox 						pmap_remove_all(p);
56826f9a767SRodney W. Grimes 						vm_page_deactivate(p);
56926f9a767SRodney W. Grimes 					} else {
570e5b006ffSAlan Cox 						vm_page_requeue(p);
571c8c4b40cSJohn Dyson 					}
572c8c4b40cSJohn Dyson 				} else {
573eaf13dd7SJohn Dyson 					vm_page_activate(p);
574e69763a3SDoug Rabson 					vm_page_flag_clear(p, PG_REFERENCED);
57538efa82bSJohn Dyson 					if (p->act_count < (ACT_MAX - ACT_ADVANCE))
57638efa82bSJohn Dyson 						p->act_count += ACT_ADVANCE;
577e5b006ffSAlan Cox 					vm_page_requeue(p);
57826f9a767SRodney W. Grimes 				}
579bd7e5f99SJohn Dyson 			} else if (p->queue == PQ_INACTIVE) {
5804fec79beSAlan Cox 				pmap_remove_all(p);
58126f9a767SRodney W. Grimes 			}
58226f9a767SRodney W. Grimes 			p = next;
58326f9a767SRodney W. Grimes 		}
584ce18aebdSAlan Cox 		vm_page_unlock_queues();
585ecf6279fSAlan Cox 		if ((backing_object = object->backing_object) == NULL)
586ecf6279fSAlan Cox 			goto unlock_return;
587ecf6279fSAlan Cox 		VM_OBJECT_LOCK(backing_object);
588ecf6279fSAlan Cox 		if (object != first_object)
589ecf6279fSAlan Cox 			VM_OBJECT_UNLOCK(object);
59038efa82bSJohn Dyson 	}
591ecf6279fSAlan Cox unlock_return:
592ecf6279fSAlan Cox 	if (object != first_object)
593ecf6279fSAlan Cox 		VM_OBJECT_UNLOCK(object);
59426f9a767SRodney W. Grimes }
59526f9a767SRodney W. Grimes 
59626f9a767SRodney W. Grimes /*
59726f9a767SRodney W. Grimes  * deactivate some number of pages in a map, try to do it fairly, but
59826f9a767SRodney W. Grimes  * that is really hard to do.
59926f9a767SRodney W. Grimes  */
600cd41fc12SDavid Greenman static void
60138efa82bSJohn Dyson vm_pageout_map_deactivate_pages(map, desired)
60226f9a767SRodney W. Grimes 	vm_map_t map;
603ecf6279fSAlan Cox 	long desired;
60426f9a767SRodney W. Grimes {
60526f9a767SRodney W. Grimes 	vm_map_entry_t tmpe;
60638efa82bSJohn Dyson 	vm_object_t obj, bigobj;
60730105b9eSTor Egge 	int nothingwired;
6080d94caffSDavid Greenman 
609d974f03cSAlan Cox 	if (!vm_map_trylock(map))
61026f9a767SRodney W. Grimes 		return;
61138efa82bSJohn Dyson 
61238efa82bSJohn Dyson 	bigobj = NULL;
61330105b9eSTor Egge 	nothingwired = TRUE;
61438efa82bSJohn Dyson 
61538efa82bSJohn Dyson 	/*
61638efa82bSJohn Dyson 	 * first, search out the biggest object, and try to free pages from
61738efa82bSJohn Dyson 	 * that.
61838efa82bSJohn Dyson 	 */
61926f9a767SRodney W. Grimes 	tmpe = map->header.next;
62038efa82bSJohn Dyson 	while (tmpe != &map->header) {
6219fdfe602SMatthew Dillon 		if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
62238efa82bSJohn Dyson 			obj = tmpe->object.vm_object;
6230774dfb3SAlan Cox 			if (obj != NULL && VM_OBJECT_TRYLOCK(obj)) {
6240774dfb3SAlan Cox 				if (obj->shadow_count <= 1 &&
6250774dfb3SAlan Cox 				    (bigobj == NULL ||
6260774dfb3SAlan Cox 				     bigobj->resident_page_count < obj->resident_page_count)) {
6270774dfb3SAlan Cox 					if (bigobj != NULL)
6280774dfb3SAlan Cox 						VM_OBJECT_UNLOCK(bigobj);
62938efa82bSJohn Dyson 					bigobj = obj;
6300774dfb3SAlan Cox 				} else
6310774dfb3SAlan Cox 					VM_OBJECT_UNLOCK(obj);
63238efa82bSJohn Dyson 			}
63338efa82bSJohn Dyson 		}
63430105b9eSTor Egge 		if (tmpe->wired_count > 0)
63530105b9eSTor Egge 			nothingwired = FALSE;
63638efa82bSJohn Dyson 		tmpe = tmpe->next;
63738efa82bSJohn Dyson 	}
63838efa82bSJohn Dyson 
6390774dfb3SAlan Cox 	if (bigobj != NULL) {
640ecf6279fSAlan Cox 		vm_pageout_object_deactivate_pages(map->pmap, bigobj, desired);
6410774dfb3SAlan Cox 		VM_OBJECT_UNLOCK(bigobj);
6420774dfb3SAlan Cox 	}
64338efa82bSJohn Dyson 	/*
64438efa82bSJohn Dyson 	 * Next, hunt around for other pages to deactivate.  We actually
64538efa82bSJohn Dyson 	 * do this search sort of wrong -- .text first is not the best idea.
64638efa82bSJohn Dyson 	 */
64738efa82bSJohn Dyson 	tmpe = map->header.next;
64838efa82bSJohn Dyson 	while (tmpe != &map->header) {
649b1028ad1SLuoqi Chen 		if (pmap_resident_count(vm_map_pmap(map)) <= desired)
65038efa82bSJohn Dyson 			break;
6519fdfe602SMatthew Dillon 		if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
65238efa82bSJohn Dyson 			obj = tmpe->object.vm_object;
6530774dfb3SAlan Cox 			if (obj != NULL) {
6540774dfb3SAlan Cox 				VM_OBJECT_LOCK(obj);
655ecf6279fSAlan Cox 				vm_pageout_object_deactivate_pages(map->pmap, obj, desired);
6560774dfb3SAlan Cox 				VM_OBJECT_UNLOCK(obj);
6570774dfb3SAlan Cox 			}
65838efa82bSJohn Dyson 		}
65926f9a767SRodney W. Grimes 		tmpe = tmpe->next;
66038857e7fSAlan Cox 	}
66138efa82bSJohn Dyson 
66238efa82bSJohn Dyson 	/*
66338efa82bSJohn Dyson 	 * Remove all mappings if a process is swapped out, this will free page
66438efa82bSJohn Dyson 	 * table pages.
66538efa82bSJohn Dyson 	 */
66638857e7fSAlan Cox 	if (desired == 0 && nothingwired) {
66705ba50f5SJake Burkholder 		pmap_remove(vm_map_pmap(map), vm_map_min(map),
66805ba50f5SJake Burkholder 		    vm_map_max(map));
66938857e7fSAlan Cox 	}
67038efa82bSJohn Dyson 	vm_map_unlock(map);
67126f9a767SRodney W. Grimes }
672a1287949SEivind Eklund #endif		/* !defined(NO_SWAPPING) */
673df8bae1dSRodney W. Grimes 
6741c7c3c6aSMatthew Dillon /*
675df8bae1dSRodney W. Grimes  *	vm_pageout_scan does the dirty work for the pageout daemon.
676df8bae1dSRodney W. Grimes  */
6772b6b0df7SMatthew Dillon static void
6782b6b0df7SMatthew Dillon vm_pageout_scan(int pass)
679df8bae1dSRodney W. Grimes {
680502ba6e4SJohn Dyson 	vm_page_t m, next;
681936524aaSMatthew Dillon 	struct vm_page marker;
6821c7c3c6aSMatthew Dillon 	int page_shortage, maxscan, pcount;
6831c7c3c6aSMatthew Dillon 	int addl_page_shortage, addl_page_shortage_init;
684df8bae1dSRodney W. Grimes 	vm_object_t object;
6852446e4f0SAlan Cox 	int actcount;
686f6b04d2bSDavid Greenman 	int vnodes_skipped = 0;
6872b6b0df7SMatthew Dillon 	int maxlaunder;
6880d94caffSDavid Greenman 
689df8bae1dSRodney W. Grimes 	/*
690855a310fSJeff Roberson 	 * Decrease registered cache sizes.
691855a310fSJeff Roberson 	 */
692855a310fSJeff Roberson 	EVENTHANDLER_INVOKE(vm_lowmem, 0);
693855a310fSJeff Roberson 	/*
694855a310fSJeff Roberson 	 * We do this explicitly after the caches have been drained above.
695855a310fSJeff Roberson 	 */
696855a310fSJeff Roberson 	uma_reclaim();
6975985940eSJohn Dyson 
698b0ef8c5fSAlan Cox 	addl_page_shortage_init = atomic_readandclear_int(&vm_pageout_deficit);
699b182ec9eSJohn Dyson 
7001c7c3c6aSMatthew Dillon 	/*
7011c7c3c6aSMatthew Dillon 	 * Calculate the number of pages we want to either free or move
7022b6b0df7SMatthew Dillon 	 * to the cache.
7031c7c3c6aSMatthew Dillon 	 */
7042b6b0df7SMatthew Dillon 	page_shortage = vm_paging_target() + addl_page_shortage_init;
7051c7c3c6aSMatthew Dillon 
7061c7c3c6aSMatthew Dillon 	/*
707936524aaSMatthew Dillon 	 * Initialize our marker
708936524aaSMatthew Dillon 	 */
709936524aaSMatthew Dillon 	bzero(&marker, sizeof(marker));
7109af80719SAlan Cox 	marker.flags = PG_FICTITIOUS | PG_MARKER;
7119af80719SAlan Cox 	marker.oflags = VPO_BUSY;
712936524aaSMatthew Dillon 	marker.queue = PQ_INACTIVE;
713936524aaSMatthew Dillon 	marker.wire_count = 1;
714936524aaSMatthew Dillon 
715936524aaSMatthew Dillon 	/*
7161c7c3c6aSMatthew Dillon 	 * Start scanning the inactive queue for pages we can move to the
7171c7c3c6aSMatthew Dillon 	 * cache or free.  The scan will stop when the target is reached or
718936524aaSMatthew Dillon 	 * we have scanned the entire inactive queue.  Note that m->act_count
719936524aaSMatthew Dillon 	 * is not used to form decisions for the inactive queue, only for the
720936524aaSMatthew Dillon 	 * active queue.
7212b6b0df7SMatthew Dillon 	 *
7222b6b0df7SMatthew Dillon 	 * maxlaunder limits the number of dirty pages we flush per scan.
7232b6b0df7SMatthew Dillon 	 * For most systems a smaller value (16 or 32) is more robust under
7242b6b0df7SMatthew Dillon 	 * extreme memory and disk pressure because any unnecessary writes
7252b6b0df7SMatthew Dillon 	 * to disk can result in extreme performance degredation.  However,
7262b6b0df7SMatthew Dillon 	 * systems with excessive dirty pages (especially when MAP_NOSYNC is
7272b6b0df7SMatthew Dillon 	 * used) will die horribly with limited laundering.  If the pageout
7282b6b0df7SMatthew Dillon 	 * daemon cannot clean enough pages in the first pass, we let it go
7292b6b0df7SMatthew Dillon 	 * all out in succeeding passes.
7301c7c3c6aSMatthew Dillon 	 */
7312b6b0df7SMatthew Dillon 	if ((maxlaunder = vm_max_launder) <= 1)
7322b6b0df7SMatthew Dillon 		maxlaunder = 1;
7332b6b0df7SMatthew Dillon 	if (pass)
7342b6b0df7SMatthew Dillon 		maxlaunder = 10000;
7353e1b578aSAlan Cox 	vm_page_lock_queues();
73667bf6868SJohn Dyson rescan0:
7371c7c3c6aSMatthew Dillon 	addl_page_shortage = addl_page_shortage_init;
7382feb50bfSAttilio Rao 	maxscan = cnt.v_inactive_count;
7396d03d577SMatthew Dillon 
740be72f788SAlan Cox 	for (m = TAILQ_FIRST(&vm_page_queues[PQ_INACTIVE].pl);
7411c7c3c6aSMatthew Dillon 	     m != NULL && maxscan-- > 0 && page_shortage > 0;
742e929c00dSKirk McKusick 	     m = next) {
743df8bae1dSRodney W. Grimes 
744393a081dSAttilio Rao 		cnt.v_pdpages++;
745b182ec9eSJohn Dyson 
746ef39c05bSAlexander Leidinger 		if (VM_PAGE_GETQUEUE(m) != PQ_INACTIVE) {
74767bf6868SJohn Dyson 			goto rescan0;
748f35329acSJohn Dyson 		}
749b182ec9eSJohn Dyson 
750b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(m, pageq);
75134d9e6fdSAlan Cox 		object = m->object;
752df8bae1dSRodney W. Grimes 
753936524aaSMatthew Dillon 		/*
754936524aaSMatthew Dillon 		 * skip marker pages
755936524aaSMatthew Dillon 		 */
756936524aaSMatthew Dillon 		if (m->flags & PG_MARKER)
757936524aaSMatthew Dillon 			continue;
758936524aaSMatthew Dillon 
75957601bcbSMatthew Dillon 		/*
76057601bcbSMatthew Dillon 		 * A held page may be undergoing I/O, so skip it.
76157601bcbSMatthew Dillon 		 */
762b182ec9eSJohn Dyson 		if (m->hold_count) {
763e5b006ffSAlan Cox 			vm_page_requeue(m);
764b182ec9eSJohn Dyson 			addl_page_shortage++;
765b182ec9eSJohn Dyson 			continue;
766df8bae1dSRodney W. Grimes 		}
76726f9a767SRodney W. Grimes 		/*
768a1287949SEivind Eklund 		 * Don't mess with busy pages, keep in the front of the
769b18bfc3dSJohn Dyson 		 * queue, most likely are being paged out.
77026f9a767SRodney W. Grimes 		 */
7718dbca793STor Egge 		if (!VM_OBJECT_TRYLOCK(object) &&
7728dbca793STor Egge 		    (!vm_pageout_fallback_object_lock(m, &next) ||
7738dbca793STor Egge 		     m->hold_count != 0)) {
7748dbca793STor Egge 			VM_OBJECT_UNLOCK(object);
77534d9e6fdSAlan Cox 			addl_page_shortage++;
77634d9e6fdSAlan Cox 			continue;
77734d9e6fdSAlan Cox 		}
7789af80719SAlan Cox 		if (m->busy || (m->oflags & VPO_BUSY)) {
77934d9e6fdSAlan Cox 			VM_OBJECT_UNLOCK(object);
780b182ec9eSJohn Dyson 			addl_page_shortage++;
78126f9a767SRodney W. Grimes 			continue;
78226f9a767SRodney W. Grimes 		}
783bd7e5f99SJohn Dyson 
7847e006499SJohn Dyson 		/*
7851c7c3c6aSMatthew Dillon 		 * If the object is not being used, we ignore previous
7861c7c3c6aSMatthew Dillon 		 * references.
7877e006499SJohn Dyson 		 */
78834d9e6fdSAlan Cox 		if (object->ref_count == 0) {
789e69763a3SDoug Rabson 			vm_page_flag_clear(m, PG_REFERENCED);
7900385347cSPeter Wemm 			pmap_clear_reference(m);
7917e006499SJohn Dyson 
7927e006499SJohn Dyson 		/*
7931c7c3c6aSMatthew Dillon 		 * Otherwise, if the page has been referenced while in the
7941c7c3c6aSMatthew Dillon 		 * inactive queue, we bump the "activation count" upwards,
7951c7c3c6aSMatthew Dillon 		 * making it less likely that the page will be added back to
7961c7c3c6aSMatthew Dillon 		 * the inactive queue prematurely again.  Here we check the
7971c7c3c6aSMatthew Dillon 		 * page tables (or emulated bits, if any), given the upper
7981c7c3c6aSMatthew Dillon 		 * level VM system not knowing anything about existing
7991c7c3c6aSMatthew Dillon 		 * references.
8007e006499SJohn Dyson 		 */
801ef743ce6SJohn Dyson 		} else if (((m->flags & PG_REFERENCED) == 0) &&
8020385347cSPeter Wemm 			(actcount = pmap_ts_referenced(m))) {
803ef743ce6SJohn Dyson 			vm_page_activate(m);
80434d9e6fdSAlan Cox 			VM_OBJECT_UNLOCK(object);
8057e006499SJohn Dyson 			m->act_count += (actcount + ACT_ADVANCE);
806ef743ce6SJohn Dyson 			continue;
8072fe6e4d7SDavid Greenman 		}
808ef743ce6SJohn Dyson 
8097e006499SJohn Dyson 		/*
8101c7c3c6aSMatthew Dillon 		 * If the upper level VM system knows about any page
8111c7c3c6aSMatthew Dillon 		 * references, we activate the page.  We also set the
8121c7c3c6aSMatthew Dillon 		 * "activation count" higher than normal so that we will less
8131c7c3c6aSMatthew Dillon 		 * likely place pages back onto the inactive queue again.
8147e006499SJohn Dyson 		 */
815bd7e5f99SJohn Dyson 		if ((m->flags & PG_REFERENCED) != 0) {
816e69763a3SDoug Rabson 			vm_page_flag_clear(m, PG_REFERENCED);
8170385347cSPeter Wemm 			actcount = pmap_ts_referenced(m);
81826f9a767SRodney W. Grimes 			vm_page_activate(m);
81934d9e6fdSAlan Cox 			VM_OBJECT_UNLOCK(object);
8207e006499SJohn Dyson 			m->act_count += (actcount + ACT_ADVANCE + 1);
8210d94caffSDavid Greenman 			continue;
8220d94caffSDavid Greenman 		}
82367bf6868SJohn Dyson 
8247e006499SJohn Dyson 		/*
8251c7c3c6aSMatthew Dillon 		 * If the upper level VM system doesn't know anything about
8261c7c3c6aSMatthew Dillon 		 * the page being dirty, we have to check for it again.  As
8271c7c3c6aSMatthew Dillon 		 * far as the VM code knows, any partially dirty pages are
8281c7c3c6aSMatthew Dillon 		 * fully dirty.
8297e006499SJohn Dyson 		 */
83084d98bf6SAlan Cox 		if (m->dirty == 0 && !pmap_is_modified(m)) {
831a3dfacb5SAlan Cox 			/*
832a3dfacb5SAlan Cox 			 * Avoid a race condition: Unless write access is
833a3dfacb5SAlan Cox 			 * removed from the page, another processor could
834a3dfacb5SAlan Cox 			 * modify it before all access is removed by the call
835a3dfacb5SAlan Cox 			 * to vm_page_cache() below.  If vm_page_cache() finds
836a3dfacb5SAlan Cox 			 * that the page has been modified when it removes all
837a3dfacb5SAlan Cox 			 * access, it panics because it cannot cache dirty
838a3dfacb5SAlan Cox 			 * pages.  In principle, we could eliminate just write
839a3dfacb5SAlan Cox 			 * access here rather than all access.  In the expected
840a3dfacb5SAlan Cox 			 * case, when there are no last instant modifications
841a3dfacb5SAlan Cox 			 * to the page, removing all access will be cheaper
842a3dfacb5SAlan Cox 			 * overall.
843a3dfacb5SAlan Cox 			 */
84484d98bf6SAlan Cox 			if ((m->flags & PG_WRITEABLE) != 0)
845a3dfacb5SAlan Cox 				pmap_remove_all(m);
846427e99a0SAlexander Langer 		} else {
8477dbf82dcSMatthew Dillon 			vm_page_dirty(m);
84830dcfc09SJohn Dyson 		}
849dcbcd518SBruce Evans 
8506989c456SAlan Cox 		if (m->valid == 0) {
8517e006499SJohn Dyson 			/*
8527e006499SJohn Dyson 			 * Invalid pages can be easily freed
8537e006499SJohn Dyson 			 */
8546989c456SAlan Cox 			vm_page_free(m);
855393a081dSAttilio Rao 			cnt.v_dfree++;
8561c7c3c6aSMatthew Dillon 			--page_shortage;
857bd7e5f99SJohn Dyson 		} else if (m->dirty == 0) {
8586989c456SAlan Cox 			/*
8596989c456SAlan Cox 			 * Clean pages can be placed onto the cache queue.
8606989c456SAlan Cox 			 * This effectively frees them.
8616989c456SAlan Cox 			 */
862bd7e5f99SJohn Dyson 			vm_page_cache(m);
8631c7c3c6aSMatthew Dillon 			--page_shortage;
8642b6b0df7SMatthew Dillon 		} else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) {
8657e006499SJohn Dyson 			/*
8662b6b0df7SMatthew Dillon 			 * Dirty pages need to be paged out, but flushing
8672b6b0df7SMatthew Dillon 			 * a page is extremely expensive verses freeing
8682b6b0df7SMatthew Dillon 			 * a clean page.  Rather then artificially limiting
8692b6b0df7SMatthew Dillon 			 * the number of pages we can flush, we instead give
8702b6b0df7SMatthew Dillon 			 * dirty pages extra priority on the inactive queue
8712b6b0df7SMatthew Dillon 			 * by forcing them to be cycled through the queue
8722b6b0df7SMatthew Dillon 			 * twice before being flushed, after which the
8732b6b0df7SMatthew Dillon 			 * (now clean) page will cycle through once more
8742b6b0df7SMatthew Dillon 			 * before being freed.  This significantly extends
8752b6b0df7SMatthew Dillon 			 * the thrash point for a heavily loaded machine.
8767e006499SJohn Dyson 			 */
8772b6b0df7SMatthew Dillon 			vm_page_flag_set(m, PG_WINATCFLS);
878e5b006ffSAlan Cox 			vm_page_requeue(m);
8790d94caffSDavid Greenman 		} else if (maxlaunder > 0) {
8802b6b0df7SMatthew Dillon 			/*
8812b6b0df7SMatthew Dillon 			 * We always want to try to flush some dirty pages if
8822b6b0df7SMatthew Dillon 			 * we encounter them, to keep the system stable.
8832b6b0df7SMatthew Dillon 			 * Normally this number is small, but under extreme
8842b6b0df7SMatthew Dillon 			 * pressure where there are insufficient clean pages
8852b6b0df7SMatthew Dillon 			 * on the inactive queue, we may have to go all out.
8862b6b0df7SMatthew Dillon 			 */
88797824da3SAlan Cox 			int swap_pageouts_ok, vfslocked = 0;
888f6b04d2bSDavid Greenman 			struct vnode *vp = NULL;
88914137dc0SAlan Cox 			struct mount *mp = NULL;
8900d94caffSDavid Greenman 
89112ac6a1dSJohn Dyson 			if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) {
89212ac6a1dSJohn Dyson 				swap_pageouts_ok = 1;
89312ac6a1dSJohn Dyson 			} else {
89412ac6a1dSJohn Dyson 				swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts);
89512ac6a1dSJohn Dyson 				swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts &&
89690ecac61SMatthew Dillon 				vm_page_count_min());
89712ac6a1dSJohn Dyson 
89812ac6a1dSJohn Dyson 			}
89970111b90SJohn Dyson 
90070111b90SJohn Dyson 			/*
9011c7c3c6aSMatthew Dillon 			 * We don't bother paging objects that are "dead".
9021c7c3c6aSMatthew Dillon 			 * Those objects are in a "rundown" state.
90370111b90SJohn Dyson 			 */
90470111b90SJohn Dyson 			if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) {
9053562af12SAlan Cox 				VM_OBJECT_UNLOCK(object);
906e5b006ffSAlan Cox 				vm_page_requeue(m);
90712ac6a1dSJohn Dyson 				continue;
90812ac6a1dSJohn Dyson 			}
90912ac6a1dSJohn Dyson 
9101c7c3c6aSMatthew Dillon 			/*
911625e6c0aSTor Egge 			 * Following operations may unlock
912625e6c0aSTor Egge 			 * vm_page_queue_mtx, invalidating the 'next'
913625e6c0aSTor Egge 			 * pointer.  To prevent an inordinate number
914625e6c0aSTor Egge 			 * of restarts we use our marker to remember
915625e6c0aSTor Egge 			 * our place.
916625e6c0aSTor Egge 			 *
917625e6c0aSTor Egge 			 */
918625e6c0aSTor Egge 			TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE].pl,
919625e6c0aSTor Egge 					   m, &marker, pageq);
920625e6c0aSTor Egge 			/*
9212b6b0df7SMatthew Dillon 			 * The object is already known NOT to be dead.   It
9222b6b0df7SMatthew Dillon 			 * is possible for the vget() to block the whole
9232b6b0df7SMatthew Dillon 			 * pageout daemon, but the new low-memory handling
9242b6b0df7SMatthew Dillon 			 * code should prevent it.
9251c7c3c6aSMatthew Dillon 			 *
9262b6b0df7SMatthew Dillon 			 * The previous code skipped locked vnodes and, worse,
9272b6b0df7SMatthew Dillon 			 * reordered pages in the queue.  This results in
9282b6b0df7SMatthew Dillon 			 * completely non-deterministic operation and, on a
9292b6b0df7SMatthew Dillon 			 * busy system, can lead to extremely non-optimal
9302b6b0df7SMatthew Dillon 			 * pageouts.  For example, it can cause clean pages
9312b6b0df7SMatthew Dillon 			 * to be freed and dirty pages to be moved to the end
9322b6b0df7SMatthew Dillon 			 * of the queue.  Since dirty pages are also moved to
9332b6b0df7SMatthew Dillon 			 * the end of the queue once-cleaned, this gives
9342b6b0df7SMatthew Dillon 			 * way too large a weighting to defering the freeing
9352b6b0df7SMatthew Dillon 			 * of dirty pages.
9361c7c3c6aSMatthew Dillon 			 *
93723b59018SMatthew Dillon 			 * We can't wait forever for the vnode lock, we might
93823b59018SMatthew Dillon 			 * deadlock due to a vn_read() getting stuck in
93923b59018SMatthew Dillon 			 * vm_wait while holding this vnode.  We skip the
94023b59018SMatthew Dillon 			 * vnode if we can't get it in a reasonable amount
94123b59018SMatthew Dillon 			 * of time.
9421c7c3c6aSMatthew Dillon 			 */
9431c7c3c6aSMatthew Dillon 			if (object->type == OBJT_VNODE) {
94424a1cce3SDavid Greenman 				vp = object->handle;
945db27dcc0STor Egge 				if (vp->v_type == VREG &&
946db27dcc0STor Egge 				    vn_start_write(vp, &mp, V_NOWAIT) != 0) {
94714137dc0SAlan Cox 					KASSERT(mp == NULL,
94814137dc0SAlan Cox 					    ("vm_pageout_scan: mp != NULL"));
949db27dcc0STor Egge 					++pageout_lock_miss;
950db27dcc0STor Egge 					if (object->flags & OBJ_MIGHTBEDIRTY)
951db27dcc0STor Egge 						vnodes_skipped++;
952625e6c0aSTor Egge 					goto unlock_and_continue;
953db27dcc0STor Egge 				}
9543e1b578aSAlan Cox 				vm_page_unlock_queues();
95514137dc0SAlan Cox 				vm_object_reference_locked(object);
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)) {
9603562af12SAlan Cox 					VM_OBJECT_LOCK(object);
9613e1b578aSAlan Cox 					vm_page_lock_queues();
96223b59018SMatthew Dillon 					++pageout_lock_miss;
963aef922f5SJohn Dyson 					if (object->flags & OBJ_MIGHTBEDIRTY)
964925a3a41SJohn Dyson 						vnodes_skipped++;
965625e6c0aSTor Egge 					vp = NULL;
966625e6c0aSTor Egge 					goto unlock_and_continue;
96785a376ebSJohn Dyson 				}
9683562af12SAlan Cox 				VM_OBJECT_LOCK(object);
9693e1b578aSAlan Cox 				vm_page_lock_queues();
970f35329acSJohn Dyson 				/*
971936524aaSMatthew Dillon 				 * The page might have been moved to another
972936524aaSMatthew Dillon 				 * queue during potential blocking in vget()
973936524aaSMatthew Dillon 				 * above.  The page might have been freed and
97414137dc0SAlan Cox 				 * reused for another vnode.
975f35329acSJohn Dyson 				 */
976ef39c05bSAlexander Leidinger 				if (VM_PAGE_GETQUEUE(m) != PQ_INACTIVE ||
977936524aaSMatthew Dillon 				    m->object != object ||
978625e6c0aSTor Egge 				    TAILQ_NEXT(m, pageq) != &marker) {
979b182ec9eSJohn Dyson 					if (object->flags & OBJ_MIGHTBEDIRTY)
980925a3a41SJohn Dyson 						vnodes_skipped++;
9813562af12SAlan Cox 					goto unlock_and_continue;
982b182ec9eSJohn Dyson 				}
983b182ec9eSJohn Dyson 
984f35329acSJohn Dyson 				/*
985936524aaSMatthew Dillon 				 * The page may have been busied during the
98614137dc0SAlan Cox 				 * blocking in vget().  We don't move the
987936524aaSMatthew Dillon 				 * page back onto the end of the queue so that
988936524aaSMatthew Dillon 				 * statistics are more correct if we don't.
989f35329acSJohn Dyson 				 */
9909af80719SAlan Cox 				if (m->busy || (m->oflags & VPO_BUSY)) {
9913562af12SAlan Cox 					goto unlock_and_continue;
992b182ec9eSJohn Dyson 				}
993b182ec9eSJohn Dyson 
994f35329acSJohn Dyson 				/*
99557601bcbSMatthew Dillon 				 * If the page has become held it might
99657601bcbSMatthew Dillon 				 * be undergoing I/O, so skip it
997f35329acSJohn Dyson 				 */
998b182ec9eSJohn Dyson 				if (m->hold_count) {
999e5b006ffSAlan Cox 					vm_page_requeue(m);
1000b182ec9eSJohn Dyson 					if (object->flags & OBJ_MIGHTBEDIRTY)
1001925a3a41SJohn Dyson 						vnodes_skipped++;
10023562af12SAlan Cox 					goto unlock_and_continue;
1003f6b04d2bSDavid Greenman 				}
1004f6b04d2bSDavid Greenman 			}
1005f6b04d2bSDavid Greenman 
10060d94caffSDavid Greenman 			/*
10070d94caffSDavid Greenman 			 * If a page is dirty, then it is either being washed
10080d94caffSDavid Greenman 			 * (but not yet cleaned) or it is still in the
10090d94caffSDavid Greenman 			 * laundry.  If it is still in the laundry, then we
10102b6b0df7SMatthew Dillon 			 * start the cleaning operation.
1011936524aaSMatthew Dillon 			 *
10122b6b0df7SMatthew Dillon 			 * decrement page_shortage on success to account for
10132b6b0df7SMatthew Dillon 			 * the (future) cleaned page.  Otherwise we could wind
10142b6b0df7SMatthew Dillon 			 * up laundering or cleaning too many pages.
10150d94caffSDavid Greenman 			 */
10162b6b0df7SMatthew Dillon 			if (vm_pageout_clean(m) != 0) {
10172b6b0df7SMatthew Dillon 				--page_shortage;
1018936524aaSMatthew Dillon 				--maxlaunder;
10192b6b0df7SMatthew Dillon 			}
10203562af12SAlan Cox unlock_and_continue:
10216989c456SAlan Cox 			VM_OBJECT_UNLOCK(object);
102214137dc0SAlan Cox 			if (mp != NULL) {
10236989c456SAlan Cox 				vm_page_unlock_queues();
102414137dc0SAlan Cox 				if (vp != NULL)
1025f6b04d2bSDavid Greenman 					vput(vp);
102697824da3SAlan Cox 				VFS_UNLOCK_GIANT(vfslocked);
102714137dc0SAlan Cox 				vm_object_deallocate(object);
1028f2a2857bSKirk McKusick 				vn_finished_write(mp);
10296989c456SAlan Cox 				vm_page_lock_queues();
10306989c456SAlan Cox 			}
1031625e6c0aSTor Egge 			next = TAILQ_NEXT(&marker, pageq);
1032625e6c0aSTor Egge 			TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl,
1033625e6c0aSTor Egge 				     &marker, pageq);
10346989c456SAlan Cox 			continue;
1035f2a2857bSKirk McKusick 		}
10363562af12SAlan Cox 		VM_OBJECT_UNLOCK(object);
10370d94caffSDavid Greenman 	}
103826f9a767SRodney W. Grimes 
1039df8bae1dSRodney W. Grimes 	/*
1040936524aaSMatthew Dillon 	 * Compute the number of pages we want to try to move from the
1041936524aaSMatthew Dillon 	 * active queue to the inactive queue.
10421c7c3c6aSMatthew Dillon 	 */
10432feb50bfSAttilio Rao 	page_shortage = vm_paging_target() +
10442feb50bfSAttilio Rao 		cnt.v_inactive_target - cnt.v_inactive_count;
1045b182ec9eSJohn Dyson 	page_shortage += addl_page_shortage;
10461c7c3c6aSMatthew Dillon 
10471c7c3c6aSMatthew Dillon 	/*
1048936524aaSMatthew Dillon 	 * Scan the active queue for things we can deactivate. We nominally
1049936524aaSMatthew Dillon 	 * track the per-page activity counter and use it to locate
1050936524aaSMatthew Dillon 	 * deactivation candidates.
10511c7c3c6aSMatthew Dillon 	 */
10522feb50bfSAttilio Rao 	pcount = cnt.v_active_count;
1053be72f788SAlan Cox 	m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
10541c7c3c6aSMatthew Dillon 
1055b18bfc3dSJohn Dyson 	while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) {
1056f35329acSJohn Dyson 
1057ef39c05bSAlexander Leidinger 		KASSERT(VM_PAGE_INQUEUE2(m, PQ_ACTIVE),
1058d3c09dd7SAlan Cox 		    ("vm_pageout_scan: page %p isn't active", m));
1059f35329acSJohn Dyson 
1060b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(m, pageq);
1061b08abf6cSAlan Cox 		object = m->object;
10628dbca793STor Egge 		if ((m->flags & PG_MARKER) != 0) {
10638dbca793STor Egge 			m = next;
10648dbca793STor Egge 			continue;
10658dbca793STor Egge 		}
10668dbca793STor Egge 		if (!VM_OBJECT_TRYLOCK(object) &&
10678dbca793STor Egge 		    !vm_pageout_fallback_object_lock(m, &next)) {
10688dbca793STor Egge 			VM_OBJECT_UNLOCK(object);
10694b8a5c40SAlan Cox 			m = next;
1070b08abf6cSAlan Cox 			continue;
1071b08abf6cSAlan Cox 		}
1072b08abf6cSAlan Cox 
1073df8bae1dSRodney W. Grimes 		/*
107426f9a767SRodney W. Grimes 		 * Don't deactivate pages that are busy.
1075df8bae1dSRodney W. Grimes 		 */
1076a647a309SDavid Greenman 		if ((m->busy != 0) ||
10779af80719SAlan Cox 		    (m->oflags & VPO_BUSY) ||
1078f6b04d2bSDavid Greenman 		    (m->hold_count != 0)) {
1079b08abf6cSAlan Cox 			VM_OBJECT_UNLOCK(object);
1080e5b006ffSAlan Cox 			vm_page_requeue(m);
108126f9a767SRodney W. Grimes 			m = next;
108226f9a767SRodney W. Grimes 			continue;
1083df8bae1dSRodney W. Grimes 		}
1084b18bfc3dSJohn Dyson 
1085b18bfc3dSJohn Dyson 		/*
1086b18bfc3dSJohn Dyson 		 * The count for pagedaemon pages is done after checking the
1087956f3135SPhilippe Charnier 		 * page for eligibility...
1088b18bfc3dSJohn Dyson 		 */
1089393a081dSAttilio Rao 		cnt.v_pdpages++;
1090ef743ce6SJohn Dyson 
10917e006499SJohn Dyson 		/*
10927e006499SJohn Dyson 		 * Check to see "how much" the page has been used.
10937e006499SJohn Dyson 		 */
10947e006499SJohn Dyson 		actcount = 0;
1095b08abf6cSAlan Cox 		if (object->ref_count != 0) {
1096ef743ce6SJohn Dyson 			if (m->flags & PG_REFERENCED) {
10977e006499SJohn Dyson 				actcount += 1;
10980d94caffSDavid Greenman 			}
10990385347cSPeter Wemm 			actcount += pmap_ts_referenced(m);
11007e006499SJohn Dyson 			if (actcount) {
11017e006499SJohn Dyson 				m->act_count += ACT_ADVANCE + actcount;
110238efa82bSJohn Dyson 				if (m->act_count > ACT_MAX)
110338efa82bSJohn Dyson 					m->act_count = ACT_MAX;
110438efa82bSJohn Dyson 			}
1105b18bfc3dSJohn Dyson 		}
1106ef743ce6SJohn Dyson 
11077e006499SJohn Dyson 		/*
11087e006499SJohn Dyson 		 * Since we have "tested" this bit, we need to clear it now.
11097e006499SJohn Dyson 		 */
1110e69763a3SDoug Rabson 		vm_page_flag_clear(m, PG_REFERENCED);
1111ef743ce6SJohn Dyson 
11127e006499SJohn Dyson 		/*
11137e006499SJohn Dyson 		 * Only if an object is currently being used, do we use the
11147e006499SJohn Dyson 		 * page activation count stats.
11157e006499SJohn Dyson 		 */
1116b08abf6cSAlan Cox 		if (actcount && (object->ref_count != 0)) {
1117e5b006ffSAlan Cox 			vm_page_requeue(m);
111826f9a767SRodney W. Grimes 		} else {
111938efa82bSJohn Dyson 			m->act_count -= min(m->act_count, ACT_DECLINE);
11202b6b0df7SMatthew Dillon 			if (vm_pageout_algorithm ||
1121b08abf6cSAlan Cox 			    object->ref_count == 0 ||
11222b6b0df7SMatthew Dillon 			    m->act_count == 0) {
1123925a3a41SJohn Dyson 				page_shortage--;
1124b08abf6cSAlan Cox 				if (object->ref_count == 0) {
11254fec79beSAlan Cox 					pmap_remove_all(m);
1126d4a272dbSJohn Dyson 					if (m->dirty == 0)
11270d94caffSDavid Greenman 						vm_page_cache(m);
1128d4a272dbSJohn Dyson 					else
1129d4a272dbSJohn Dyson 						vm_page_deactivate(m);
11300d94caffSDavid Greenman 				} else {
113126f9a767SRodney W. Grimes 					vm_page_deactivate(m);
1132df8bae1dSRodney W. Grimes 				}
113338efa82bSJohn Dyson 			} else {
1134e5b006ffSAlan Cox 				vm_page_requeue(m);
113538efa82bSJohn Dyson 			}
1136df8bae1dSRodney W. Grimes 		}
1137b08abf6cSAlan Cox 		VM_OBJECT_UNLOCK(object);
113826f9a767SRodney W. Grimes 		m = next;
113926f9a767SRodney W. Grimes 	}
11408ffc1519SAlan Cox 	vm_page_unlock_queues();
1141ceb0cf87SJohn Dyson #if !defined(NO_SWAPPING)
1142ceb0cf87SJohn Dyson 	/*
1143ceb0cf87SJohn Dyson 	 * Idle process swapout -- run once per second.
1144ceb0cf87SJohn Dyson 	 */
1145ceb0cf87SJohn Dyson 	if (vm_swap_idle_enabled) {
1146ceb0cf87SJohn Dyson 		static long lsec;
1147227ee8a1SPoul-Henning Kamp 		if (time_second != lsec) {
114897824da3SAlan Cox 			vm_req_vmdaemon(VM_SWAP_IDLE);
1149227ee8a1SPoul-Henning Kamp 			lsec = time_second;
1150ceb0cf87SJohn Dyson 		}
1151ceb0cf87SJohn Dyson 	}
1152ceb0cf87SJohn Dyson #endif
1153ceb0cf87SJohn Dyson 
11545663e6deSDavid Greenman 	/*
1155f6b04d2bSDavid Greenman 	 * If we didn't get enough free pages, and we have skipped a vnode
11564c1f8ee9SDavid Greenman 	 * in a writeable object, wakeup the sync daemon.  And kick swapout
11574c1f8ee9SDavid Greenman 	 * if we did not get enough free pages.
1158f6b04d2bSDavid Greenman 	 */
115990ecac61SMatthew Dillon 	if (vm_paging_target() > 0) {
116090ecac61SMatthew Dillon 		if (vnodes_skipped && vm_page_count_min())
1161d50c1994SPeter Wemm 			(void) speedup_syncer();
116238efa82bSJohn Dyson #if !defined(NO_SWAPPING)
116397824da3SAlan Cox 		if (vm_swap_enabled && vm_page_count_target())
116497824da3SAlan Cox 			vm_req_vmdaemon(VM_SWAP_NORMAL);
11655afce282SDavid Greenman #endif
11664c1f8ee9SDavid Greenman 	}
11674c1f8ee9SDavid Greenman 
1168f6b04d2bSDavid Greenman 	/*
1169e92686d0SDavid Schultz 	 * If we are critically low on one of RAM or swap and low on
1170e92686d0SDavid Schultz 	 * the other, kill the largest process.  However, we avoid
1171e92686d0SDavid Schultz 	 * doing this on the first pass in order to give ourselves a
1172e92686d0SDavid Schultz 	 * chance to flush out dirty vnode-backed pages and to allow
1173e92686d0SDavid Schultz 	 * active pages to be moved to the inactive queue and reclaimed.
11742025d69bSKonstantin Belousov 	 */
11752025d69bSKonstantin Belousov 	if (pass != 0 &&
11762025d69bSKonstantin Belousov 	    ((swap_pager_avail < 64 && vm_page_count_min()) ||
11772025d69bSKonstantin Belousov 	     (swap_pager_full && vm_paging_target() > 0)))
11782025d69bSKonstantin Belousov 		vm_pageout_oom(VM_OOM_MEM);
11792025d69bSKonstantin Belousov }
11802025d69bSKonstantin Belousov 
11812025d69bSKonstantin Belousov 
11822025d69bSKonstantin Belousov void
11832025d69bSKonstantin Belousov vm_pageout_oom(int shortage)
11842025d69bSKonstantin Belousov {
11852025d69bSKonstantin Belousov 	struct proc *p, *bigproc;
11862025d69bSKonstantin Belousov 	vm_offset_t size, bigsize;
11872025d69bSKonstantin Belousov 	struct thread *td;
11882025d69bSKonstantin Belousov 
11892025d69bSKonstantin Belousov 	/*
11901c58e4e5SJohn Baldwin 	 * We keep the process bigproc locked once we find it to keep anyone
11911c58e4e5SJohn Baldwin 	 * from messing with it; however, there is a possibility of
11921c58e4e5SJohn Baldwin 	 * deadlock if process B is bigproc and one of it's child processes
11931c58e4e5SJohn Baldwin 	 * attempts to propagate a signal to B while we are waiting for A's
11941c58e4e5SJohn Baldwin 	 * lock while walking this list.  To avoid this, we don't block on
11951c58e4e5SJohn Baldwin 	 * the process lock but just skip a process if it is already locked.
11965663e6deSDavid Greenman 	 */
11975663e6deSDavid Greenman 	bigproc = NULL;
11985663e6deSDavid Greenman 	bigsize = 0;
11991005a129SJohn Baldwin 	sx_slock(&allproc_lock);
1200e602ba25SJulian Elischer 	FOREACH_PROC_IN_SYSTEM(p) {
1201e602ba25SJulian Elischer 		int breakout;
1202dcbcd518SBruce Evans 
12031c58e4e5SJohn Baldwin 		if (PROC_TRYLOCK(p) == 0)
12041c58e4e5SJohn Baldwin 			continue;
12051c58e4e5SJohn Baldwin 		/*
1206f4cf2141SWes Peters 		 * If this is a system or protected process, skip it.
12075663e6deSDavid Greenman 		 */
1208ef6020d1SMike Silbersack 		if ((p->p_flag & P_SYSTEM) || (p->p_pid == 1) ||
1209f4cf2141SWes Peters 		    (p->p_flag & P_PROTECTED) ||
12108f60c087SPoul-Henning Kamp 		    ((p->p_pid < 48) && (swap_pager_avail != 0))) {
12118606d880SJohn Baldwin 			PROC_UNLOCK(p);
12125663e6deSDavid Greenman 			continue;
12135663e6deSDavid Greenman 		}
12145663e6deSDavid Greenman 		/*
1215dcbcd518SBruce Evans 		 * If the process is in a non-running type state,
1216e602ba25SJulian Elischer 		 * don't touch it.  Check all the threads individually.
12175663e6deSDavid Greenman 		 */
1218e602ba25SJulian Elischer 		breakout = 0;
1219e602ba25SJulian Elischer 		FOREACH_THREAD_IN_PROC(p, td) {
1220982d11f8SJeff Roberson 			thread_lock(td);
122171fad9fdSJulian Elischer 			if (!TD_ON_RUNQ(td) &&
122271fad9fdSJulian Elischer 			    !TD_IS_RUNNING(td) &&
122371fad9fdSJulian Elischer 			    !TD_IS_SLEEPING(td)) {
1224982d11f8SJeff Roberson 				thread_unlock(td);
1225e602ba25SJulian Elischer 				breakout = 1;
1226e602ba25SJulian Elischer 				break;
1227e602ba25SJulian Elischer 			}
1228982d11f8SJeff Roberson 			thread_unlock(td);
1229e602ba25SJulian Elischer 		}
1230e602ba25SJulian Elischer 		if (breakout) {
12311c58e4e5SJohn Baldwin 			PROC_UNLOCK(p);
12325663e6deSDavid Greenman 			continue;
12335663e6deSDavid Greenman 		}
12345663e6deSDavid Greenman 		/*
12355663e6deSDavid Greenman 		 * get the process size
12365663e6deSDavid Greenman 		 */
123772d97679SDavid Schultz 		if (!vm_map_trylock_read(&p->p_vmspace->vm_map)) {
123872d97679SDavid Schultz 			PROC_UNLOCK(p);
123972d97679SDavid Schultz 			continue;
124072d97679SDavid Schultz 		}
124172d97679SDavid Schultz 		size = vmspace_swap_count(p->p_vmspace);
124272d97679SDavid Schultz 		vm_map_unlock_read(&p->p_vmspace->vm_map);
12432025d69bSKonstantin Belousov 		if (shortage == VM_OOM_MEM)
124472d97679SDavid Schultz 			size += vmspace_resident_count(p->p_vmspace);
12455663e6deSDavid Greenman 		/*
12465663e6deSDavid Greenman 		 * if the this process is bigger than the biggest one
12475663e6deSDavid Greenman 		 * remember it.
12485663e6deSDavid Greenman 		 */
12495663e6deSDavid Greenman 		if (size > bigsize) {
12501c58e4e5SJohn Baldwin 			if (bigproc != NULL)
12511c58e4e5SJohn Baldwin 				PROC_UNLOCK(bigproc);
12525663e6deSDavid Greenman 			bigproc = p;
12535663e6deSDavid Greenman 			bigsize = size;
12541c58e4e5SJohn Baldwin 		} else
12551c58e4e5SJohn Baldwin 			PROC_UNLOCK(p);
12565663e6deSDavid Greenman 	}
12571005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
12585663e6deSDavid Greenman 	if (bigproc != NULL) {
1259729b1e51SDavid Greenman 		killproc(bigproc, "out of swap space");
1260fa885116SJulian Elischer 		sched_nice(bigproc, PRIO_MIN);
12611c58e4e5SJohn Baldwin 		PROC_UNLOCK(bigproc);
12622feb50bfSAttilio Rao 		wakeup(&cnt.v_free_count);
12635663e6deSDavid Greenman 	}
12645663e6deSDavid Greenman }
126526f9a767SRodney W. Grimes 
1266dc2efb27SJohn Dyson /*
1267dc2efb27SJohn Dyson  * This routine tries to maintain the pseudo LRU active queue,
1268dc2efb27SJohn Dyson  * so that during long periods of time where there is no paging,
1269956f3135SPhilippe Charnier  * that some statistic accumulation still occurs.  This code
1270dc2efb27SJohn Dyson  * helps the situation where paging just starts to occur.
1271dc2efb27SJohn Dyson  */
1272dc2efb27SJohn Dyson static void
1273dc2efb27SJohn Dyson vm_pageout_page_stats()
1274dc2efb27SJohn Dyson {
1275b86e6ec0SAlan Cox 	vm_object_t object;
1276dc2efb27SJohn Dyson 	vm_page_t m,next;
1277dc2efb27SJohn Dyson 	int pcount,tpcount;		/* Number of pages to check */
1278dc2efb27SJohn Dyson 	static int fullintervalcount = 0;
1279bef608bdSJohn Dyson 	int page_shortage;
1280bef608bdSJohn Dyson 
12815e609009SAlan Cox 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
128290ecac61SMatthew Dillon 	page_shortage =
12832feb50bfSAttilio Rao 	    (cnt.v_inactive_target + cnt.v_cache_max + cnt.v_free_min) -
12842feb50bfSAttilio Rao 	    (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count);
128590ecac61SMatthew Dillon 
1286bef608bdSJohn Dyson 	if (page_shortage <= 0)
1287bef608bdSJohn Dyson 		return;
1288dc2efb27SJohn Dyson 
12892feb50bfSAttilio Rao 	pcount = cnt.v_active_count;
1290dc2efb27SJohn Dyson 	fullintervalcount += vm_pageout_stats_interval;
1291dc2efb27SJohn Dyson 	if (fullintervalcount < vm_pageout_full_stats_interval) {
12928d28bf04SAlan Cox 		tpcount = (int64_t)vm_pageout_stats_max * cnt.v_active_count /
12938d28bf04SAlan Cox 		    cnt.v_page_count;
1294dc2efb27SJohn Dyson 		if (pcount > tpcount)
1295dc2efb27SJohn Dyson 			pcount = tpcount;
1296883f3caaSMatthew Dillon 	} else {
1297883f3caaSMatthew Dillon 		fullintervalcount = 0;
1298dc2efb27SJohn Dyson 	}
1299dc2efb27SJohn Dyson 
1300be72f788SAlan Cox 	m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
1301dc2efb27SJohn Dyson 	while ((m != NULL) && (pcount-- > 0)) {
13027e006499SJohn Dyson 		int actcount;
1303dc2efb27SJohn Dyson 
1304ef39c05bSAlexander Leidinger 		KASSERT(VM_PAGE_INQUEUE2(m, PQ_ACTIVE),
1305ab42316cSAlan Cox 		    ("vm_pageout_page_stats: page %p isn't active", m));
1306dc2efb27SJohn Dyson 
1307dc2efb27SJohn Dyson 		next = TAILQ_NEXT(m, pageq);
1308b86e6ec0SAlan Cox 		object = m->object;
13098dbca793STor Egge 
13108dbca793STor Egge 		if ((m->flags & PG_MARKER) != 0) {
13118dbca793STor Egge 			m = next;
13128dbca793STor Egge 			continue;
13138dbca793STor Egge 		}
13148dbca793STor Egge 		if (!VM_OBJECT_TRYLOCK(object) &&
13158dbca793STor Egge 		    !vm_pageout_fallback_object_lock(m, &next)) {
13168dbca793STor Egge 			VM_OBJECT_UNLOCK(object);
1317b86e6ec0SAlan Cox 			m = next;
1318b86e6ec0SAlan Cox 			continue;
1319b86e6ec0SAlan Cox 		}
1320b86e6ec0SAlan Cox 
1321dc2efb27SJohn Dyson 		/*
1322dc2efb27SJohn Dyson 		 * Don't deactivate pages that are busy.
1323dc2efb27SJohn Dyson 		 */
1324dc2efb27SJohn Dyson 		if ((m->busy != 0) ||
13259af80719SAlan Cox 		    (m->oflags & VPO_BUSY) ||
1326dc2efb27SJohn Dyson 		    (m->hold_count != 0)) {
1327b86e6ec0SAlan Cox 			VM_OBJECT_UNLOCK(object);
1328e5b006ffSAlan Cox 			vm_page_requeue(m);
1329dc2efb27SJohn Dyson 			m = next;
1330dc2efb27SJohn Dyson 			continue;
1331dc2efb27SJohn Dyson 		}
1332dc2efb27SJohn Dyson 
13337e006499SJohn Dyson 		actcount = 0;
1334dc2efb27SJohn Dyson 		if (m->flags & PG_REFERENCED) {
1335e69763a3SDoug Rabson 			vm_page_flag_clear(m, PG_REFERENCED);
13367e006499SJohn Dyson 			actcount += 1;
1337dc2efb27SJohn Dyson 		}
1338dc2efb27SJohn Dyson 
13390385347cSPeter Wemm 		actcount += pmap_ts_referenced(m);
13407e006499SJohn Dyson 		if (actcount) {
13417e006499SJohn Dyson 			m->act_count += ACT_ADVANCE + actcount;
1342dc2efb27SJohn Dyson 			if (m->act_count > ACT_MAX)
1343dc2efb27SJohn Dyson 				m->act_count = ACT_MAX;
1344e5b006ffSAlan Cox 			vm_page_requeue(m);
1345dc2efb27SJohn Dyson 		} else {
1346dc2efb27SJohn Dyson 			if (m->act_count == 0) {
13477e006499SJohn Dyson 				/*
13482b6b0df7SMatthew Dillon 				 * We turn off page access, so that we have
13492b6b0df7SMatthew Dillon 				 * more accurate RSS stats.  We don't do this
13502b6b0df7SMatthew Dillon 				 * in the normal page deactivation when the
13512b6b0df7SMatthew Dillon 				 * system is loaded VM wise, because the
13522b6b0df7SMatthew Dillon 				 * cost of the large number of page protect
13532b6b0df7SMatthew Dillon 				 * operations would be higher than the value
13542b6b0df7SMatthew Dillon 				 * of doing the operation.
13557e006499SJohn Dyson 				 */
13564fec79beSAlan Cox 				pmap_remove_all(m);
1357dc2efb27SJohn Dyson 				vm_page_deactivate(m);
1358dc2efb27SJohn Dyson 			} else {
1359dc2efb27SJohn Dyson 				m->act_count -= min(m->act_count, ACT_DECLINE);
1360e5b006ffSAlan Cox 				vm_page_requeue(m);
1361dc2efb27SJohn Dyson 			}
1362dc2efb27SJohn Dyson 		}
1363b86e6ec0SAlan Cox 		VM_OBJECT_UNLOCK(object);
1364dc2efb27SJohn Dyson 		m = next;
1365dc2efb27SJohn Dyson 	}
1366dc2efb27SJohn Dyson }
1367dc2efb27SJohn Dyson 
1368df8bae1dSRodney W. Grimes /*
1369df8bae1dSRodney W. Grimes  *	vm_pageout is the high level pageout daemon.
1370df8bae1dSRodney W. Grimes  */
13712b14f991SJulian Elischer static void
137226f9a767SRodney W. Grimes vm_pageout()
1373df8bae1dSRodney W. Grimes {
13741aab16a6SAlan Cox 	int error, pass;
13750384fff8SJason Evans 
1376df8bae1dSRodney W. Grimes 	/*
1377df8bae1dSRodney W. Grimes 	 * Initialize some paging parameters.
1378df8bae1dSRodney W. Grimes 	 */
13792feb50bfSAttilio Rao 	cnt.v_interrupt_free_min = 2;
13802feb50bfSAttilio Rao 	if (cnt.v_page_count < 2000)
1381f35329acSJohn Dyson 		vm_pageout_page_count = 8;
1382f6b04d2bSDavid Greenman 
138345ae1d91SAlan Cox 	/*
138445ae1d91SAlan Cox 	 * v_free_reserved needs to include enough for the largest
138545ae1d91SAlan Cox 	 * swap pager structures plus enough for any pv_entry structs
138645ae1d91SAlan Cox 	 * when paging.
138745ae1d91SAlan Cox 	 */
13882feb50bfSAttilio Rao 	if (cnt.v_page_count > 1024)
13892feb50bfSAttilio Rao 		cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200;
13902feb50bfSAttilio Rao 	else
13912feb50bfSAttilio Rao 		cnt.v_free_min = 4;
13922feb50bfSAttilio Rao 	cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE +
13932feb50bfSAttilio Rao 	    cnt.v_interrupt_free_min;
13942feb50bfSAttilio Rao 	cnt.v_free_reserved = vm_pageout_page_count +
13952446e4f0SAlan Cox 	    cnt.v_pageout_free_min + (cnt.v_page_count / 768);
13962feb50bfSAttilio Rao 	cnt.v_free_severe = cnt.v_free_min / 2;
13972feb50bfSAttilio Rao 	cnt.v_free_min += cnt.v_free_reserved;
13982feb50bfSAttilio Rao 	cnt.v_free_severe += cnt.v_free_reserved;
139945ae1d91SAlan Cox 
1400ed74321bSDavid Greenman 	/*
14012b6b0df7SMatthew Dillon 	 * v_free_target and v_cache_min control pageout hysteresis.  Note
14022b6b0df7SMatthew Dillon 	 * that these are more a measure of the VM cache queue hysteresis
14032b6b0df7SMatthew Dillon 	 * then the VM free queue.  Specifically, v_free_target is the
14042b6b0df7SMatthew Dillon 	 * high water mark (free+cache pages).
14052b6b0df7SMatthew Dillon 	 *
14062b6b0df7SMatthew Dillon 	 * v_free_reserved + v_cache_min (mostly means v_cache_min) is the
14072b6b0df7SMatthew Dillon 	 * low water mark, while v_free_min is the stop.  v_cache_min must
14082b6b0df7SMatthew Dillon 	 * be big enough to handle memory needs while the pageout daemon
14092b6b0df7SMatthew Dillon 	 * is signalled and run to free more pages.
1410ed74321bSDavid Greenman 	 */
14112feb50bfSAttilio Rao 	if (cnt.v_free_count > 6144)
14122feb50bfSAttilio Rao 		cnt.v_free_target = 4 * cnt.v_free_min + cnt.v_free_reserved;
14132feb50bfSAttilio Rao 	else
14142feb50bfSAttilio Rao 		cnt.v_free_target = 2 * cnt.v_free_min + cnt.v_free_reserved;
14156f2b142eSDavid Greenman 
14162feb50bfSAttilio Rao 	if (cnt.v_free_count > 2048) {
14172feb50bfSAttilio Rao 		cnt.v_cache_min = cnt.v_free_target;
14182feb50bfSAttilio Rao 		cnt.v_cache_max = 2 * cnt.v_cache_min;
14192feb50bfSAttilio Rao 		cnt.v_inactive_target = (3 * cnt.v_free_target) / 2;
14200d94caffSDavid Greenman 	} else {
14212feb50bfSAttilio Rao 		cnt.v_cache_min = 0;
14222feb50bfSAttilio Rao 		cnt.v_cache_max = 0;
14232feb50bfSAttilio Rao 		cnt.v_inactive_target = cnt.v_free_count / 4;
14240d94caffSDavid Greenman 	}
14252feb50bfSAttilio Rao 	if (cnt.v_inactive_target > cnt.v_free_count / 3)
14262feb50bfSAttilio Rao 		cnt.v_inactive_target = cnt.v_free_count / 3;
1427df8bae1dSRodney W. Grimes 
1428df8bae1dSRodney W. Grimes 	/* XXX does not really belong here */
1429df8bae1dSRodney W. Grimes 	if (vm_page_max_wired == 0)
14302feb50bfSAttilio Rao 		vm_page_max_wired = cnt.v_free_count / 3;
1431df8bae1dSRodney W. Grimes 
1432dc2efb27SJohn Dyson 	if (vm_pageout_stats_max == 0)
14332feb50bfSAttilio Rao 		vm_pageout_stats_max = cnt.v_free_target;
1434dc2efb27SJohn Dyson 
1435dc2efb27SJohn Dyson 	/*
1436dc2efb27SJohn Dyson 	 * Set interval in seconds for stats scan.
1437dc2efb27SJohn Dyson 	 */
1438dc2efb27SJohn Dyson 	if (vm_pageout_stats_interval == 0)
1439bef608bdSJohn Dyson 		vm_pageout_stats_interval = 5;
1440dc2efb27SJohn Dyson 	if (vm_pageout_full_stats_interval == 0)
1441dc2efb27SJohn Dyson 		vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4;
1442dc2efb27SJohn Dyson 
144324a1cce3SDavid Greenman 	swap_pager_swap_init();
14442b6b0df7SMatthew Dillon 	pass = 0;
1445df8bae1dSRodney W. Grimes 	/*
14460d94caffSDavid Greenman 	 * The pageout daemon is never done, so loop forever.
1447df8bae1dSRodney W. Grimes 	 */
1448df8bae1dSRodney W. Grimes 	while (TRUE) {
1449936524aaSMatthew Dillon 		/*
1450936524aaSMatthew Dillon 		 * If we have enough free memory, wakeup waiters.  Do
1451936524aaSMatthew Dillon 		 * not clear vm_pages_needed until we reach our target,
1452936524aaSMatthew Dillon 		 * otherwise we may be woken up over and over again and
1453936524aaSMatthew Dillon 		 * waste a lot of cpu.
1454936524aaSMatthew Dillon 		 */
1455e9f995d8SAlan Cox 		mtx_lock(&vm_page_queue_free_mtx);
1456936524aaSMatthew Dillon 		if (vm_pages_needed && !vm_page_count_min()) {
1457a1c0a785SAlan Cox 			if (!vm_paging_needed())
1458936524aaSMatthew Dillon 				vm_pages_needed = 0;
14592feb50bfSAttilio Rao 			wakeup(&cnt.v_free_count);
1460936524aaSMatthew Dillon 		}
1461936524aaSMatthew Dillon 		if (vm_pages_needed) {
146290ecac61SMatthew Dillon 			/*
14632b6b0df7SMatthew Dillon 			 * Still not done, take a second pass without waiting
14642b6b0df7SMatthew Dillon 			 * (unlimited dirty cleaning), otherwise sleep a bit
14652b6b0df7SMatthew Dillon 			 * and try again.
146690ecac61SMatthew Dillon 			 */
14672b6b0df7SMatthew Dillon 			++pass;
14682b6b0df7SMatthew Dillon 			if (pass > 1)
1469e9f995d8SAlan Cox 				msleep(&vm_pages_needed,
1470e9f995d8SAlan Cox 				    &vm_page_queue_free_mtx, PVM, "psleep",
1471e9f995d8SAlan Cox 				    hz / 2);
147290ecac61SMatthew Dillon 		} else {
147390ecac61SMatthew Dillon 			/*
14742b6b0df7SMatthew Dillon 			 * Good enough, sleep & handle stats.  Prime the pass
14752b6b0df7SMatthew Dillon 			 * for the next run.
147690ecac61SMatthew Dillon 			 */
14772b6b0df7SMatthew Dillon 			if (pass > 1)
14782b6b0df7SMatthew Dillon 				pass = 1;
14792b6b0df7SMatthew Dillon 			else
14802b6b0df7SMatthew Dillon 				pass = 0;
1481e9f995d8SAlan Cox 			error = msleep(&vm_pages_needed,
1482e9f995d8SAlan Cox 			    &vm_page_queue_free_mtx, PVM, "psleep",
1483e9f995d8SAlan Cox 			    vm_pageout_stats_interval * hz);
1484dc2efb27SJohn Dyson 			if (error && !vm_pages_needed) {
1485e9f995d8SAlan Cox 				mtx_unlock(&vm_page_queue_free_mtx);
14862b6b0df7SMatthew Dillon 				pass = 0;
1487e9f995d8SAlan Cox 				vm_page_lock_queues();
1488dc2efb27SJohn Dyson 				vm_pageout_page_stats();
14895e609009SAlan Cox 				vm_page_unlock_queues();
1490dc2efb27SJohn Dyson 				continue;
1491dc2efb27SJohn Dyson 			}
1492f919ebdeSDavid Greenman 		}
1493b18bfc3dSJohn Dyson 		if (vm_pages_needed)
1494393a081dSAttilio Rao 			cnt.v_pdwakeups++;
1495e9f995d8SAlan Cox 		mtx_unlock(&vm_page_queue_free_mtx);
14962b6b0df7SMatthew Dillon 		vm_pageout_scan(pass);
1497df8bae1dSRodney W. Grimes 	}
1498df8bae1dSRodney W. Grimes }
149926f9a767SRodney W. Grimes 
15006b4b77adSAlan Cox /*
1501e9f995d8SAlan Cox  * Unless the free page queue lock is held by the caller, this function
15026b4b77adSAlan Cox  * should be regarded as advisory.  Specifically, the caller should
15036b4b77adSAlan Cox  * not msleep() on &cnt.v_free_count following this function unless
1504e9f995d8SAlan Cox  * the free page queue lock is held until the msleep() is performed.
15056b4b77adSAlan Cox  */
1506e0c5a895SJohn Dyson void
1507e0c5a895SJohn Dyson pagedaemon_wakeup()
1508e0c5a895SJohn Dyson {
1509a1c0a785SAlan Cox 
1510b40ce416SJulian Elischer 	if (!vm_pages_needed && curthread->td_proc != pageproc) {
1511a1c0a785SAlan Cox 		vm_pages_needed = 1;
1512e0c5a895SJohn Dyson 		wakeup(&vm_pages_needed);
1513e0c5a895SJohn Dyson 	}
1514e0c5a895SJohn Dyson }
1515e0c5a895SJohn Dyson 
151638efa82bSJohn Dyson #if !defined(NO_SWAPPING)
15175afce282SDavid Greenman static void
151897824da3SAlan Cox vm_req_vmdaemon(int req)
15195afce282SDavid Greenman {
15205afce282SDavid Greenman 	static int lastrun = 0;
15215afce282SDavid Greenman 
152297824da3SAlan Cox 	mtx_lock(&vm_daemon_mtx);
152397824da3SAlan Cox 	vm_pageout_req_swapout |= req;
1524b18bfc3dSJohn Dyson 	if ((ticks > (lastrun + hz)) || (ticks < lastrun)) {
15255afce282SDavid Greenman 		wakeup(&vm_daemon_needed);
15265afce282SDavid Greenman 		lastrun = ticks;
15275afce282SDavid Greenman 	}
152897824da3SAlan Cox 	mtx_unlock(&vm_daemon_mtx);
15295afce282SDavid Greenman }
15305afce282SDavid Greenman 
15312b14f991SJulian Elischer static void
15324f9fb771SBruce Evans vm_daemon()
15330d94caffSDavid Greenman {
153491d5354aSJohn Baldwin 	struct rlimit rsslim;
1535dcbcd518SBruce Evans 	struct proc *p;
1536dcbcd518SBruce Evans 	struct thread *td;
153797824da3SAlan Cox 	int breakout, swapout_flags;
15380d94caffSDavid Greenman 
15392fe6e4d7SDavid Greenman 	while (TRUE) {
154097824da3SAlan Cox 		mtx_lock(&vm_daemon_mtx);
154197824da3SAlan Cox 		msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", 0);
154297824da3SAlan Cox 		swapout_flags = vm_pageout_req_swapout;
15434c1f8ee9SDavid Greenman 		vm_pageout_req_swapout = 0;
154497824da3SAlan Cox 		mtx_unlock(&vm_daemon_mtx);
154597824da3SAlan Cox 		if (swapout_flags)
154697824da3SAlan Cox 			swapout_procs(swapout_flags);
154797824da3SAlan Cox 
15482fe6e4d7SDavid Greenman 		/*
15490d94caffSDavid Greenman 		 * scan the processes for exceeding their rlimits or if
15500d94caffSDavid Greenman 		 * process is swapped out -- deactivate pages
15512fe6e4d7SDavid Greenman 		 */
15521005a129SJohn Baldwin 		sx_slock(&allproc_lock);
1553f67af5c9SXin LI 		FOREACH_PROC_IN_SYSTEM(p) {
1554fe2144fdSLuoqi Chen 			vm_pindex_t limit, size;
15552fe6e4d7SDavid Greenman 
15562fe6e4d7SDavid Greenman 			/*
15572fe6e4d7SDavid Greenman 			 * if this is a system process or if we have already
15582fe6e4d7SDavid Greenman 			 * looked at this process, skip it.
15592fe6e4d7SDavid Greenman 			 */
1560897ecacdSJohn Baldwin 			PROC_LOCK(p);
15612fe6e4d7SDavid Greenman 			if (p->p_flag & (P_SYSTEM | P_WEXIT)) {
1562897ecacdSJohn Baldwin 				PROC_UNLOCK(p);
15632fe6e4d7SDavid Greenman 				continue;
15642fe6e4d7SDavid Greenman 			}
15652fe6e4d7SDavid Greenman 			/*
15662fe6e4d7SDavid Greenman 			 * if the process is in a non-running type state,
15672fe6e4d7SDavid Greenman 			 * don't touch it.
15682fe6e4d7SDavid Greenman 			 */
1569e602ba25SJulian Elischer 			breakout = 0;
1570e602ba25SJulian Elischer 			FOREACH_THREAD_IN_PROC(p, td) {
1571982d11f8SJeff Roberson 				thread_lock(td);
157271fad9fdSJulian Elischer 				if (!TD_ON_RUNQ(td) &&
157371fad9fdSJulian Elischer 				    !TD_IS_RUNNING(td) &&
157471fad9fdSJulian Elischer 				    !TD_IS_SLEEPING(td)) {
1575982d11f8SJeff Roberson 					thread_unlock(td);
1576e602ba25SJulian Elischer 					breakout = 1;
1577e602ba25SJulian Elischer 					break;
1578e602ba25SJulian Elischer 				}
1579982d11f8SJeff Roberson 				thread_unlock(td);
1580e602ba25SJulian Elischer 			}
1581897ecacdSJohn Baldwin 			if (breakout) {
1582897ecacdSJohn Baldwin 				PROC_UNLOCK(p);
15832fe6e4d7SDavid Greenman 				continue;
15842fe6e4d7SDavid Greenman 			}
15852fe6e4d7SDavid Greenman 			/*
15862fe6e4d7SDavid Greenman 			 * get a limit
15872fe6e4d7SDavid Greenman 			 */
1588dcbcd518SBruce Evans 			lim_rlimit(p, RLIMIT_RSS, &rsslim);
1589fe2144fdSLuoqi Chen 			limit = OFF_TO_IDX(
159091d5354aSJohn Baldwin 			    qmin(rsslim.rlim_cur, rsslim.rlim_max));
15912fe6e4d7SDavid Greenman 
15922fe6e4d7SDavid Greenman 			/*
15930d94caffSDavid Greenman 			 * let processes that are swapped out really be
15940d94caffSDavid Greenman 			 * swapped out set the limit to nothing (will force a
15950d94caffSDavid Greenman 			 * swap-out.)
15962fe6e4d7SDavid Greenman 			 */
1597b61ce5b0SJeff Roberson 			if ((p->p_flag & P_INMEM) == 0)
15980d94caffSDavid Greenman 				limit = 0;	/* XXX */
1599897ecacdSJohn Baldwin 			PROC_UNLOCK(p);
16002fe6e4d7SDavid Greenman 
1601fe2144fdSLuoqi Chen 			size = vmspace_resident_count(p->p_vmspace);
16022fe6e4d7SDavid Greenman 			if (limit >= 0 && size >= limit) {
1603fe2144fdSLuoqi Chen 				vm_pageout_map_deactivate_pages(
1604fe2144fdSLuoqi Chen 				    &p->p_vmspace->vm_map, limit);
16052fe6e4d7SDavid Greenman 			}
16062fe6e4d7SDavid Greenman 		}
16071005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
160824a1cce3SDavid Greenman 	}
16092fe6e4d7SDavid Greenman }
1610a1287949SEivind Eklund #endif			/* !defined(NO_SWAPPING) */
1611