xref: /freebsd/sys/vm/vm_pageout.c (revision 982d11f836278f1e95ae1ae398aa4d1d07a19006)
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>
8826f9a767SRodney W. Grimes #include <sys/resourcevar.h>
89b43179fbSJeff Roberson #include <sys/sched.h>
90d2fc5315SPoul-Henning Kamp #include <sys/signalvar.h>
91f6b04d2bSDavid Greenman #include <sys/vnode.h>
92efeaf95aSDavid Greenman #include <sys/vmmeter.h>
931005a129SJohn Baldwin #include <sys/sx.h>
9438efa82bSJohn Dyson #include <sys/sysctl.h>
95df8bae1dSRodney W. Grimes 
96df8bae1dSRodney W. Grimes #include <vm/vm.h>
97efeaf95aSDavid Greenman #include <vm/vm_param.h>
98efeaf95aSDavid Greenman #include <vm/vm_object.h>
99df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
100efeaf95aSDavid Greenman #include <vm/vm_map.h>
101df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
10224a1cce3SDavid Greenman #include <vm/vm_pager.h>
10305f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h>
104efeaf95aSDavid Greenman #include <vm/vm_extern.h>
105670d17b5SJeff Roberson #include <vm/uma.h>
106df8bae1dSRodney W. Grimes 
1070384fff8SJason Evans #include <machine/mutex.h>
1080384fff8SJason Evans 
1092b14f991SJulian Elischer /*
1102b14f991SJulian Elischer  * System initialization
1112b14f991SJulian Elischer  */
1122b14f991SJulian Elischer 
1132b14f991SJulian Elischer /* the kernel process "vm_pageout"*/
11411caded3SAlfred Perlstein static void vm_pageout(void);
11511caded3SAlfred Perlstein static int vm_pageout_clean(vm_page_t);
11611caded3SAlfred Perlstein static void vm_pageout_scan(int pass);
11745ae1d91SAlan Cox 
1182b14f991SJulian Elischer struct proc *pageproc;
1192b14f991SJulian Elischer 
1202b14f991SJulian Elischer static struct kproc_desc page_kp = {
1212b14f991SJulian Elischer 	"pagedaemon",
1222b14f991SJulian Elischer 	vm_pageout,
1232b14f991SJulian Elischer 	&pageproc
1242b14f991SJulian Elischer };
1259c8b8baaSPeter Wemm SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, &page_kp)
1262b14f991SJulian Elischer 
12738efa82bSJohn Dyson #if !defined(NO_SWAPPING)
1282b14f991SJulian Elischer /* the kernel process "vm_daemon"*/
12911caded3SAlfred Perlstein static void vm_daemon(void);
130f708ef1bSPoul-Henning Kamp static struct	proc *vmproc;
1312b14f991SJulian Elischer 
1322b14f991SJulian Elischer static struct kproc_desc vm_kp = {
1332b14f991SJulian Elischer 	"vmdaemon",
1342b14f991SJulian Elischer 	vm_daemon,
1352b14f991SJulian Elischer 	&vmproc
1362b14f991SJulian Elischer };
1379c8b8baaSPeter Wemm SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp)
13838efa82bSJohn Dyson #endif
1392b14f991SJulian Elischer 
1402b14f991SJulian Elischer 
1418b245767SAlan Cox int vm_pages_needed;		/* Event on which pageout daemon sleeps */
1428b245767SAlan Cox int vm_pageout_deficit;		/* Estimated number of pages deficit */
1438b245767SAlan Cox int vm_pageout_pages_needed;	/* flag saying that the pageout daemon needs pages */
14426f9a767SRodney W. Grimes 
14538efa82bSJohn Dyson #if !defined(NO_SWAPPING)
146f708ef1bSPoul-Henning Kamp static int vm_pageout_req_swapout;	/* XXX */
147f708ef1bSPoul-Henning Kamp static int vm_daemon_needed;
14838efa82bSJohn Dyson #endif
1492b6b0df7SMatthew Dillon static int vm_max_launder = 32;
150303b270bSEivind Eklund static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0;
151303b270bSEivind Eklund static int vm_pageout_full_stats_interval = 0;
15226354d4cSAlan Cox static int vm_pageout_algorithm=0;
153303b270bSEivind Eklund static int defer_swap_pageouts=0;
154303b270bSEivind Eklund static int disable_swap_pageouts=0;
15570111b90SJohn Dyson 
15638efa82bSJohn Dyson #if defined(NO_SWAPPING)
157303b270bSEivind Eklund static int vm_swap_enabled=0;
158303b270bSEivind Eklund static int vm_swap_idle_enabled=0;
15938efa82bSJohn Dyson #else
160303b270bSEivind Eklund static int vm_swap_enabled=1;
161303b270bSEivind Eklund static int vm_swap_idle_enabled=0;
16238efa82bSJohn Dyson #endif
16338efa82bSJohn Dyson 
16438efa82bSJohn Dyson SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm,
1652b6b0df7SMatthew Dillon 	CTLFLAG_RW, &vm_pageout_algorithm, 0, "LRU page mgmt");
1662b6b0df7SMatthew Dillon 
1672b6b0df7SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, max_launder,
1682b6b0df7SMatthew Dillon 	CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout");
16938efa82bSJohn Dyson 
170dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max,
171b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length");
172dc2efb27SJohn Dyson 
173dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval,
174b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan");
175dc2efb27SJohn Dyson 
176dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval,
177b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan");
178dc2efb27SJohn Dyson 
17938efa82bSJohn Dyson #if defined(NO_SWAPPING)
180ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
181ceb0cf87SJohn Dyson 	CTLFLAG_RD, &vm_swap_enabled, 0, "");
182ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
183ceb0cf87SJohn Dyson 	CTLFLAG_RD, &vm_swap_idle_enabled, 0, "");
18438efa82bSJohn Dyson #else
185ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
186b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout");
187ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
188b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
18938efa82bSJohn Dyson #endif
19026f9a767SRodney W. Grimes 
191ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts,
192b0359e2cSPeter Wemm 	CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem");
19312ac6a1dSJohn Dyson 
194ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts,
195b0359e2cSPeter Wemm 	CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages");
19612ac6a1dSJohn Dyson 
19723b59018SMatthew Dillon static int pageout_lock_miss;
19823b59018SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss,
19923b59018SMatthew Dillon 	CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout");
20023b59018SMatthew Dillon 
201ffc82b0aSJohn Dyson #define VM_PAGEOUT_PAGE_COUNT 16
202bbc0ec52SDavid Greenman int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT;
203df8bae1dSRodney W. Grimes 
204c3cb3e12SDavid Greenman int vm_page_max_wired;		/* XXX max # of wired pages system-wide */
205df8bae1dSRodney W. Grimes 
20638efa82bSJohn Dyson #if !defined(NO_SWAPPING)
207ecf6279fSAlan Cox static void vm_pageout_map_deactivate_pages(vm_map_t, long);
208ecf6279fSAlan Cox static void vm_pageout_object_deactivate_pages(pmap_t, vm_object_t, long);
20911caded3SAlfred Perlstein static void vm_req_vmdaemon(void);
21038efa82bSJohn Dyson #endif
211dc2efb27SJohn Dyson static void vm_pageout_page_stats(void);
212cd41fc12SDavid Greenman 
21326f9a767SRodney W. Grimes /*
2148dbca793STor Egge  * vm_pageout_fallback_object_lock:
2158dbca793STor Egge  *
2168dbca793STor Egge  * Lock vm object currently associated with `m'. VM_OBJECT_TRYLOCK is
2178dbca793STor Egge  * known to have failed and page queue must be either PQ_ACTIVE or
2188dbca793STor Egge  * PQ_INACTIVE.  To avoid lock order violation, unlock the page queues
2198dbca793STor Egge  * while locking the vm object.  Use marker page to detect page queue
2208dbca793STor Egge  * changes and maintain notion of next page on page queue.  Return
2218dbca793STor Egge  * TRUE if no changes were detected, FALSE otherwise.  vm object is
2228dbca793STor Egge  * locked on return.
2238dbca793STor Egge  *
2248dbca793STor Egge  * This function depends on both the lock portion of struct vm_object
2258dbca793STor Egge  * and normal struct vm_page being type stable.
2268dbca793STor Egge  */
2278dbca793STor Egge static boolean_t
2288dbca793STor Egge vm_pageout_fallback_object_lock(vm_page_t m, vm_page_t *next)
2298dbca793STor Egge {
2308dbca793STor Egge 	struct vm_page marker;
2318dbca793STor Egge 	boolean_t unchanged;
2328dbca793STor Egge 	u_short queue;
2338dbca793STor Egge 	vm_object_t object;
2348dbca793STor Egge 
2358dbca793STor Egge 	/*
2368dbca793STor Egge 	 * Initialize our marker
2378dbca793STor Egge 	 */
2388dbca793STor Egge 	bzero(&marker, sizeof(marker));
2399af80719SAlan Cox 	marker.flags = PG_FICTITIOUS | PG_MARKER;
2409af80719SAlan Cox 	marker.oflags = VPO_BUSY;
2418dbca793STor Egge 	marker.queue = m->queue;
2428dbca793STor Egge 	marker.wire_count = 1;
2438dbca793STor Egge 
2448dbca793STor Egge 	queue = m->queue;
2458dbca793STor Egge 	object = m->object;
2468dbca793STor Egge 
2478dbca793STor Egge 	TAILQ_INSERT_AFTER(&vm_page_queues[queue].pl,
2488dbca793STor Egge 			   m, &marker, pageq);
2498dbca793STor Egge 	vm_page_unlock_queues();
2508dbca793STor Egge 	VM_OBJECT_LOCK(object);
2518dbca793STor Egge 	vm_page_lock_queues();
2528dbca793STor Egge 
2538dbca793STor Egge 	/* Page queue might have changed. */
2548dbca793STor Egge 	*next = TAILQ_NEXT(&marker, pageq);
2558dbca793STor Egge 	unchanged = (m->queue == queue &&
2568dbca793STor Egge 		     m->object == object &&
2578dbca793STor Egge 		     &marker == TAILQ_NEXT(m, pageq));
2588dbca793STor Egge 	TAILQ_REMOVE(&vm_page_queues[queue].pl,
2598dbca793STor Egge 		     &marker, pageq);
2608dbca793STor Egge 	return (unchanged);
2618dbca793STor Egge }
2628dbca793STor Egge 
2638dbca793STor Egge /*
26426f9a767SRodney W. Grimes  * vm_pageout_clean:
26524a1cce3SDavid Greenman  *
2660d94caffSDavid Greenman  * Clean the page and remove it from the laundry.
26726f9a767SRodney W. Grimes  *
2680d94caffSDavid Greenman  * We set the busy bit to cause potential page faults on this page to
2691c7c3c6aSMatthew Dillon  * block.  Note the careful timing, however, the busy bit isn't set till
2701c7c3c6aSMatthew Dillon  * late and we cannot do anything that will mess with the page.
27126f9a767SRodney W. Grimes  */
2723af76890SPoul-Henning Kamp static int
2738f9110f6SJohn Dyson vm_pageout_clean(m)
27424a1cce3SDavid Greenman 	vm_page_t m;
27524a1cce3SDavid Greenman {
27654d92145SMatthew Dillon 	vm_object_t object;
277f35329acSJohn Dyson 	vm_page_t mc[2*vm_pageout_page_count];
2783562af12SAlan Cox 	int pageout_count;
27990ecac61SMatthew Dillon 	int ib, is, page_base;
280a316d390SJohn Dyson 	vm_pindex_t pindex = m->pindex;
28126f9a767SRodney W. Grimes 
28255df3298SAlan Cox 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2833562af12SAlan Cox 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2840cddd8f0SMatthew Dillon 
28526f9a767SRodney W. Grimes 	/*
2861c7c3c6aSMatthew Dillon 	 * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP
2871c7c3c6aSMatthew Dillon 	 * with the new swapper, but we could have serious problems paging
2881c7c3c6aSMatthew Dillon 	 * out other object types if there is insufficient memory.
2891c7c3c6aSMatthew Dillon 	 *
2901c7c3c6aSMatthew Dillon 	 * Unfortunately, checking free memory here is far too late, so the
2911c7c3c6aSMatthew Dillon 	 * check has been moved up a procedural level.
2921c7c3c6aSMatthew Dillon 	 */
2931c7c3c6aSMatthew Dillon 
29424a1cce3SDavid Greenman 	/*
2958b03c8edSMatthew Dillon 	 * Don't mess with the page if it's busy, held, or special
29624a1cce3SDavid Greenman 	 */
2978f9110f6SJohn Dyson 	if ((m->hold_count != 0) ||
2989af80719SAlan Cox 	    ((m->busy != 0) || (m->oflags & VPO_BUSY) ||
2999af80719SAlan Cox 	     (m->flags & PG_UNMANAGED))) {
3000d94caffSDavid Greenman 		return 0;
3018b03c8edSMatthew Dillon 	}
3020d94caffSDavid Greenman 
303f35329acSJohn Dyson 	mc[vm_pageout_page_count] = m;
30426f9a767SRodney W. Grimes 	pageout_count = 1;
305f35329acSJohn Dyson 	page_base = vm_pageout_page_count;
30690ecac61SMatthew Dillon 	ib = 1;
30790ecac61SMatthew Dillon 	is = 1;
30890ecac61SMatthew Dillon 
30924a1cce3SDavid Greenman 	/*
31024a1cce3SDavid Greenman 	 * Scan object for clusterable pages.
31124a1cce3SDavid Greenman 	 *
31224a1cce3SDavid Greenman 	 * We can cluster ONLY if: ->> the page is NOT
31324a1cce3SDavid Greenman 	 * clean, wired, busy, held, or mapped into a
31424a1cce3SDavid Greenman 	 * buffer, and one of the following:
31524a1cce3SDavid Greenman 	 * 1) The page is inactive, or a seldom used
31624a1cce3SDavid Greenman 	 *    active page.
31724a1cce3SDavid Greenman 	 * -or-
31824a1cce3SDavid Greenman 	 * 2) we force the issue.
31990ecac61SMatthew Dillon 	 *
32090ecac61SMatthew Dillon 	 * During heavy mmap/modification loads the pageout
32190ecac61SMatthew Dillon 	 * daemon can really fragment the underlying file
32290ecac61SMatthew Dillon 	 * due to flushing pages out of order and not trying
32390ecac61SMatthew Dillon 	 * align the clusters (which leave sporatic out-of-order
32490ecac61SMatthew Dillon 	 * holes).  To solve this problem we do the reverse scan
32590ecac61SMatthew Dillon 	 * first and attempt to align our cluster, then do a
32690ecac61SMatthew Dillon 	 * forward scan if room remains.
32724a1cce3SDavid Greenman 	 */
3285163584cSAlan Cox 	object = m->object;
32990ecac61SMatthew Dillon more:
33090ecac61SMatthew Dillon 	while (ib && pageout_count < vm_pageout_page_count) {
33124a1cce3SDavid Greenman 		vm_page_t p;
332f6b04d2bSDavid Greenman 
33390ecac61SMatthew Dillon 		if (ib > pindex) {
33490ecac61SMatthew Dillon 			ib = 0;
33590ecac61SMatthew Dillon 			break;
336f6b04d2bSDavid Greenman 		}
33790ecac61SMatthew Dillon 
33890ecac61SMatthew Dillon 		if ((p = vm_page_lookup(object, pindex - ib)) == NULL) {
33990ecac61SMatthew Dillon 			ib = 0;
34090ecac61SMatthew Dillon 			break;
34190ecac61SMatthew Dillon 		}
342997e1c25SAlan Cox 		if (VM_PAGE_INQUEUE1(p, PQ_CACHE) ||
3439af80719SAlan Cox 		    (p->oflags & VPO_BUSY) || p->busy ||
3449af80719SAlan Cox 		    (p->flags & PG_UNMANAGED)) {
34590ecac61SMatthew Dillon 			ib = 0;
34690ecac61SMatthew Dillon 			break;
347f6b04d2bSDavid Greenman 		}
34824a1cce3SDavid Greenman 		vm_page_test_dirty(p);
34990ecac61SMatthew Dillon 		if ((p->dirty & p->valid) == 0 ||
35090ecac61SMatthew Dillon 		    p->queue != PQ_INACTIVE ||
35157601bcbSMatthew Dillon 		    p->wire_count != 0 ||	/* may be held by buf cache */
35257601bcbSMatthew Dillon 		    p->hold_count != 0) {	/* may be undergoing I/O */
35390ecac61SMatthew Dillon 			ib = 0;
35424a1cce3SDavid Greenman 			break;
355f6b04d2bSDavid Greenman 		}
35690ecac61SMatthew Dillon 		mc[--page_base] = p;
35790ecac61SMatthew Dillon 		++pageout_count;
35890ecac61SMatthew Dillon 		++ib;
35924a1cce3SDavid Greenman 		/*
36090ecac61SMatthew Dillon 		 * alignment boundry, stop here and switch directions.  Do
36190ecac61SMatthew Dillon 		 * not clear ib.
36224a1cce3SDavid Greenman 		 */
36390ecac61SMatthew Dillon 		if ((pindex - (ib - 1)) % vm_pageout_page_count == 0)
36490ecac61SMatthew Dillon 			break;
36524a1cce3SDavid Greenman 	}
36690ecac61SMatthew Dillon 
36790ecac61SMatthew Dillon 	while (pageout_count < vm_pageout_page_count &&
36890ecac61SMatthew Dillon 	    pindex + is < object->size) {
36990ecac61SMatthew Dillon 		vm_page_t p;
37090ecac61SMatthew Dillon 
37190ecac61SMatthew Dillon 		if ((p = vm_page_lookup(object, pindex + is)) == NULL)
37290ecac61SMatthew Dillon 			break;
373997e1c25SAlan Cox 		if (VM_PAGE_INQUEUE1(p, PQ_CACHE) ||
3749af80719SAlan Cox 		    (p->oflags & VPO_BUSY) || p->busy ||
3759af80719SAlan Cox 		    (p->flags & PG_UNMANAGED)) {
37690ecac61SMatthew Dillon 			break;
37724a1cce3SDavid Greenman 		}
37824a1cce3SDavid Greenman 		vm_page_test_dirty(p);
37990ecac61SMatthew Dillon 		if ((p->dirty & p->valid) == 0 ||
38090ecac61SMatthew Dillon 		    p->queue != PQ_INACTIVE ||
38157601bcbSMatthew Dillon 		    p->wire_count != 0 ||	/* may be held by buf cache */
38257601bcbSMatthew Dillon 		    p->hold_count != 0) {	/* may be undergoing I/O */
38324a1cce3SDavid Greenman 			break;
38424a1cce3SDavid Greenman 		}
38590ecac61SMatthew Dillon 		mc[page_base + pageout_count] = p;
38690ecac61SMatthew Dillon 		++pageout_count;
38790ecac61SMatthew Dillon 		++is;
38824a1cce3SDavid Greenman 	}
38990ecac61SMatthew Dillon 
39090ecac61SMatthew Dillon 	/*
39190ecac61SMatthew Dillon 	 * If we exhausted our forward scan, continue with the reverse scan
39290ecac61SMatthew Dillon 	 * when possible, even past a page boundry.  This catches boundry
39390ecac61SMatthew Dillon 	 * conditions.
39490ecac61SMatthew Dillon 	 */
39590ecac61SMatthew Dillon 	if (ib && pageout_count < vm_pageout_page_count)
39690ecac61SMatthew Dillon 		goto more;
397f6b04d2bSDavid Greenman 
39867bf6868SJohn Dyson 	/*
39967bf6868SJohn Dyson 	 * we allow reads during pageouts...
40067bf6868SJohn Dyson 	 */
4017a935082SAlan Cox 	return (vm_pageout_flush(&mc[page_base], pageout_count, 0));
402aef922f5SJohn Dyson }
403aef922f5SJohn Dyson 
4041c7c3c6aSMatthew Dillon /*
4051c7c3c6aSMatthew Dillon  * vm_pageout_flush() - launder the given pages
4061c7c3c6aSMatthew Dillon  *
4071c7c3c6aSMatthew Dillon  *	The given pages are laundered.  Note that we setup for the start of
4081c7c3c6aSMatthew Dillon  *	I/O ( i.e. busy the page ), mark it read-only, and bump the object
4091c7c3c6aSMatthew Dillon  *	reference count all in here rather then in the parent.  If we want
4101c7c3c6aSMatthew Dillon  *	the parent to do more sophisticated things we may have to change
4111c7c3c6aSMatthew Dillon  *	the ordering.
4121c7c3c6aSMatthew Dillon  */
413aef922f5SJohn Dyson int
4147a935082SAlan Cox vm_pageout_flush(vm_page_t *mc, int count, int flags)
415aef922f5SJohn Dyson {
4162e3b314dSAlan Cox 	vm_object_t object = mc[0]->object;
417aef922f5SJohn Dyson 	int pageout_status[count];
41895461b45SJohn Dyson 	int numpagedout = 0;
419aef922f5SJohn Dyson 	int i;
420aef922f5SJohn Dyson 
42155df3298SAlan Cox 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
4222e3b314dSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
4231c7c3c6aSMatthew Dillon 	/*
4241c7c3c6aSMatthew Dillon 	 * Initiate I/O.  Bump the vm_page_t->busy counter and
4251c7c3c6aSMatthew Dillon 	 * mark the pages read-only.
4261c7c3c6aSMatthew Dillon 	 *
4271c7c3c6aSMatthew Dillon 	 * We do not have to fixup the clean/dirty bits here... we can
4281c7c3c6aSMatthew Dillon 	 * allow the pager to do it after the I/O completes.
42902fa91d3SMatthew Dillon 	 *
43002fa91d3SMatthew Dillon 	 * NOTE! mc[i]->dirty may be partial or fragmented due to an
43102fa91d3SMatthew Dillon 	 * edge case with file fragments.
4321c7c3c6aSMatthew Dillon 	 */
4338f9110f6SJohn Dyson 	for (i = 0; i < count; i++) {
4347a935082SAlan Cox 		KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL,
4357a935082SAlan Cox 		    ("vm_pageout_flush: partially invalid page %p index %d/%d",
4367a935082SAlan Cox 			mc[i], i, count));
437e69763a3SDoug Rabson 		vm_page_io_start(mc[i]);
43878985e42SAlan Cox 		pmap_remove_write(mc[i]);
4398f9110f6SJohn Dyson 	}
44055df3298SAlan Cox 	vm_page_unlock_queues();
441d474eaaaSDoug Rabson 	vm_object_pip_add(object, count);
442aef922f5SJohn Dyson 
443aef922f5SJohn Dyson 	vm_pager_put_pages(object, mc, count,
44440bb4f4bSMatthew Dillon 	    (flags | ((object == kernel_object) ? VM_PAGER_PUT_SYNC : 0)),
44526f9a767SRodney W. Grimes 	    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 
4519ea8d1a6SAlan Cox 		KASSERT((mt->flags & PG_WRITEABLE) == 0,
4529ea8d1a6SAlan Cox 		    ("vm_pageout_flush: page %p is not write protected", mt));
45326f9a767SRodney W. Grimes 		switch (pageout_status[i]) {
45426f9a767SRodney W. Grimes 		case VM_PAGER_OK:
45526f9a767SRodney W. Grimes 		case VM_PAGER_PEND:
45695461b45SJohn Dyson 			numpagedout++;
45726f9a767SRodney W. Grimes 			break;
45826f9a767SRodney W. Grimes 		case VM_PAGER_BAD:
45926f9a767SRodney W. Grimes 			/*
4600d94caffSDavid Greenman 			 * Page outside of range of object. Right now we
4610d94caffSDavid Greenman 			 * essentially lose the changes by pretending it
4620d94caffSDavid Greenman 			 * worked.
46326f9a767SRodney W. Grimes 			 */
4640385347cSPeter Wemm 			pmap_clear_modify(mt);
46590ecac61SMatthew Dillon 			vm_page_undirty(mt);
46626f9a767SRodney W. Grimes 			break;
46726f9a767SRodney W. Grimes 		case VM_PAGER_ERROR:
46826f9a767SRodney W. Grimes 		case VM_PAGER_FAIL:
46926f9a767SRodney W. Grimes 			/*
4700d94caffSDavid Greenman 			 * If page couldn't be paged out, then reactivate the
4710d94caffSDavid Greenman 			 * page so it doesn't clog the inactive list.  (We
4720d94caffSDavid Greenman 			 * will try paging out it again later).
47326f9a767SRodney W. Grimes 			 */
47424a1cce3SDavid Greenman 			vm_page_activate(mt);
47526f9a767SRodney W. Grimes 			break;
47626f9a767SRodney W. Grimes 		case VM_PAGER_AGAIN:
47726f9a767SRodney W. Grimes 			break;
47826f9a767SRodney W. Grimes 		}
47926f9a767SRodney W. Grimes 
48026f9a767SRodney W. Grimes 		/*
4810d94caffSDavid Greenman 		 * If the operation is still going, leave the page busy to
4820d94caffSDavid Greenman 		 * block all other accesses. Also, leave the paging in
4830d94caffSDavid Greenman 		 * progress indicator set so that we don't attempt an object
4840d94caffSDavid Greenman 		 * collapse.
48526f9a767SRodney W. Grimes 		 */
48626f9a767SRodney W. Grimes 		if (pageout_status[i] != VM_PAGER_PEND) {
487f919ebdeSDavid Greenman 			vm_object_pip_wakeup(object);
488e69763a3SDoug Rabson 			vm_page_io_finish(mt);
4899ea8d1a6SAlan Cox 			if (vm_page_count_severe())
4909ea8d1a6SAlan Cox 				vm_page_try_to_cache(mt);
49126f9a767SRodney W. Grimes 		}
49226f9a767SRodney W. Grimes 	}
49395461b45SJohn Dyson 	return numpagedout;
49426f9a767SRodney W. Grimes }
49526f9a767SRodney W. Grimes 
49638efa82bSJohn Dyson #if !defined(NO_SWAPPING)
49726f9a767SRodney W. Grimes /*
49826f9a767SRodney W. Grimes  *	vm_pageout_object_deactivate_pages
49926f9a767SRodney W. Grimes  *
50026f9a767SRodney W. Grimes  *	deactivate enough pages to satisfy the inactive target
50126f9a767SRodney W. Grimes  *	requirements or if vm_page_proc_limit is set, then
50226f9a767SRodney W. Grimes  *	deactivate all of the pages in the object and its
50324a1cce3SDavid Greenman  *	backing_objects.
50426f9a767SRodney W. Grimes  *
50526f9a767SRodney W. Grimes  *	The object and map must be locked.
50626f9a767SRodney W. Grimes  */
50738efa82bSJohn Dyson static void
508ecf6279fSAlan Cox vm_pageout_object_deactivate_pages(pmap, first_object, desired)
509ecf6279fSAlan Cox 	pmap_t pmap;
510ecf6279fSAlan Cox 	vm_object_t first_object;
511ecf6279fSAlan Cox 	long desired;
51226f9a767SRodney W. Grimes {
513ecf6279fSAlan Cox 	vm_object_t backing_object, object;
51454d92145SMatthew Dillon 	vm_page_t p, next;
515ce18aebdSAlan Cox 	int actcount, rcount, remove_mode;
51626f9a767SRodney W. Grimes 
517ecf6279fSAlan Cox 	VM_OBJECT_LOCK_ASSERT(first_object, MA_OWNED);
518ecf6279fSAlan Cox 	if (first_object->type == OBJT_DEVICE || first_object->type == OBJT_PHYS)
51938efa82bSJohn Dyson 		return;
520ecf6279fSAlan Cox 	for (object = first_object;; object = backing_object) {
521ecf6279fSAlan Cox 		if (pmap_resident_count(pmap) <= desired)
522ecf6279fSAlan Cox 			goto unlock_return;
52324a1cce3SDavid Greenman 		if (object->paging_in_progress)
524ecf6279fSAlan Cox 			goto unlock_return;
52526f9a767SRodney W. Grimes 
52685b1dc89SAlan Cox 		remove_mode = 0;
52738efa82bSJohn Dyson 		if (object->shadow_count > 1)
52838efa82bSJohn Dyson 			remove_mode = 1;
52926f9a767SRodney W. Grimes 		/*
53026f9a767SRodney W. Grimes 		 * scan the objects entire memory queue
53126f9a767SRodney W. Grimes 		 */
53226f9a767SRodney W. Grimes 		rcount = object->resident_page_count;
533b18bfc3dSJohn Dyson 		p = TAILQ_FIRST(&object->memq);
534ce18aebdSAlan Cox 		vm_page_lock_queues();
53526f9a767SRodney W. Grimes 		while (p && (rcount-- > 0)) {
536ecf6279fSAlan Cox 			if (pmap_resident_count(pmap) <= desired) {
537ce18aebdSAlan Cox 				vm_page_unlock_queues();
538ecf6279fSAlan Cox 				goto unlock_return;
539ce18aebdSAlan Cox 			}
540b18bfc3dSJohn Dyson 			next = TAILQ_NEXT(p, listq);
541b4b70819SAttilio Rao 			PCPU_INC(cnt.v_pdpages);
5420d94caffSDavid Greenman 			if (p->wire_count != 0 ||
5430d94caffSDavid Greenman 			    p->hold_count != 0 ||
5440d94caffSDavid Greenman 			    p->busy != 0 ||
5459af80719SAlan Cox 			    (p->oflags & VPO_BUSY) ||
5469af80719SAlan Cox 			    (p->flags & PG_UNMANAGED) ||
547ecf6279fSAlan Cox 			    !pmap_page_exists_quick(pmap, p)) {
5480d94caffSDavid Greenman 				p = next;
5490d94caffSDavid Greenman 				continue;
5500d94caffSDavid Greenman 			}
5510385347cSPeter Wemm 			actcount = pmap_ts_referenced(p);
5527e006499SJohn Dyson 			if (actcount) {
553e69763a3SDoug Rabson 				vm_page_flag_set(p, PG_REFERENCED);
554c8c4b40cSJohn Dyson 			} else if (p->flags & PG_REFERENCED) {
5557e006499SJohn Dyson 				actcount = 1;
556ef743ce6SJohn Dyson 			}
55738efa82bSJohn Dyson 			if ((p->queue != PQ_ACTIVE) &&
55838efa82bSJohn Dyson 				(p->flags & PG_REFERENCED)) {
559ef743ce6SJohn Dyson 				vm_page_activate(p);
5607e006499SJohn Dyson 				p->act_count += actcount;
561e69763a3SDoug Rabson 				vm_page_flag_clear(p, PG_REFERENCED);
562c8c4b40cSJohn Dyson 			} else if (p->queue == PQ_ACTIVE) {
563ef743ce6SJohn Dyson 				if ((p->flags & PG_REFERENCED) == 0) {
564c8c4b40cSJohn Dyson 					p->act_count -= min(p->act_count, ACT_DECLINE);
5652b6b0df7SMatthew Dillon 					if (!remove_mode && (vm_pageout_algorithm || (p->act_count == 0))) {
5664fec79beSAlan Cox 						pmap_remove_all(p);
56726f9a767SRodney W. Grimes 						vm_page_deactivate(p);
56826f9a767SRodney W. Grimes 					} else {
5696d03d577SMatthew Dillon 						vm_pageq_requeue(p);
570c8c4b40cSJohn Dyson 					}
571c8c4b40cSJohn Dyson 				} else {
572eaf13dd7SJohn Dyson 					vm_page_activate(p);
573e69763a3SDoug Rabson 					vm_page_flag_clear(p, PG_REFERENCED);
57438efa82bSJohn Dyson 					if (p->act_count < (ACT_MAX - ACT_ADVANCE))
57538efa82bSJohn Dyson 						p->act_count += ACT_ADVANCE;
5766d03d577SMatthew Dillon 					vm_pageq_requeue(p);
57726f9a767SRodney W. Grimes 				}
578bd7e5f99SJohn Dyson 			} else if (p->queue == PQ_INACTIVE) {
5794fec79beSAlan Cox 				pmap_remove_all(p);
58026f9a767SRodney W. Grimes 			}
58126f9a767SRodney W. Grimes 			p = next;
58226f9a767SRodney W. Grimes 		}
583ce18aebdSAlan Cox 		vm_page_unlock_queues();
584ecf6279fSAlan Cox 		if ((backing_object = object->backing_object) == NULL)
585ecf6279fSAlan Cox 			goto unlock_return;
586ecf6279fSAlan Cox 		VM_OBJECT_LOCK(backing_object);
587ecf6279fSAlan Cox 		if (object != first_object)
588ecf6279fSAlan Cox 			VM_OBJECT_UNLOCK(object);
58938efa82bSJohn Dyson 	}
590ecf6279fSAlan Cox unlock_return:
591ecf6279fSAlan Cox 	if (object != first_object)
592ecf6279fSAlan Cox 		VM_OBJECT_UNLOCK(object);
59326f9a767SRodney W. Grimes }
59426f9a767SRodney W. Grimes 
59526f9a767SRodney W. Grimes /*
59626f9a767SRodney W. Grimes  * deactivate some number of pages in a map, try to do it fairly, but
59726f9a767SRodney W. Grimes  * that is really hard to do.
59826f9a767SRodney W. Grimes  */
599cd41fc12SDavid Greenman static void
60038efa82bSJohn Dyson vm_pageout_map_deactivate_pages(map, desired)
60126f9a767SRodney W. Grimes 	vm_map_t map;
602ecf6279fSAlan Cox 	long desired;
60326f9a767SRodney W. Grimes {
60426f9a767SRodney W. Grimes 	vm_map_entry_t tmpe;
60538efa82bSJohn Dyson 	vm_object_t obj, bigobj;
60630105b9eSTor Egge 	int nothingwired;
6070d94caffSDavid Greenman 
608d974f03cSAlan Cox 	if (!vm_map_trylock(map))
60926f9a767SRodney W. Grimes 		return;
61038efa82bSJohn Dyson 
61138efa82bSJohn Dyson 	bigobj = NULL;
61230105b9eSTor Egge 	nothingwired = TRUE;
61338efa82bSJohn Dyson 
61438efa82bSJohn Dyson 	/*
61538efa82bSJohn Dyson 	 * first, search out the biggest object, and try to free pages from
61638efa82bSJohn Dyson 	 * that.
61738efa82bSJohn Dyson 	 */
61826f9a767SRodney W. Grimes 	tmpe = map->header.next;
61938efa82bSJohn Dyson 	while (tmpe != &map->header) {
6209fdfe602SMatthew Dillon 		if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
62138efa82bSJohn Dyson 			obj = tmpe->object.vm_object;
6220774dfb3SAlan Cox 			if (obj != NULL && VM_OBJECT_TRYLOCK(obj)) {
6230774dfb3SAlan Cox 				if (obj->shadow_count <= 1 &&
6240774dfb3SAlan Cox 				    (bigobj == NULL ||
6250774dfb3SAlan Cox 				     bigobj->resident_page_count < obj->resident_page_count)) {
6260774dfb3SAlan Cox 					if (bigobj != NULL)
6270774dfb3SAlan Cox 						VM_OBJECT_UNLOCK(bigobj);
62838efa82bSJohn Dyson 					bigobj = obj;
6290774dfb3SAlan Cox 				} else
6300774dfb3SAlan Cox 					VM_OBJECT_UNLOCK(obj);
63138efa82bSJohn Dyson 			}
63238efa82bSJohn Dyson 		}
63330105b9eSTor Egge 		if (tmpe->wired_count > 0)
63430105b9eSTor Egge 			nothingwired = FALSE;
63538efa82bSJohn Dyson 		tmpe = tmpe->next;
63638efa82bSJohn Dyson 	}
63738efa82bSJohn Dyson 
6380774dfb3SAlan Cox 	if (bigobj != NULL) {
639ecf6279fSAlan Cox 		vm_pageout_object_deactivate_pages(map->pmap, bigobj, desired);
6400774dfb3SAlan Cox 		VM_OBJECT_UNLOCK(bigobj);
6410774dfb3SAlan Cox 	}
64238efa82bSJohn Dyson 	/*
64338efa82bSJohn Dyson 	 * Next, hunt around for other pages to deactivate.  We actually
64438efa82bSJohn Dyson 	 * do this search sort of wrong -- .text first is not the best idea.
64538efa82bSJohn Dyson 	 */
64638efa82bSJohn Dyson 	tmpe = map->header.next;
64738efa82bSJohn Dyson 	while (tmpe != &map->header) {
648b1028ad1SLuoqi Chen 		if (pmap_resident_count(vm_map_pmap(map)) <= desired)
64938efa82bSJohn Dyson 			break;
6509fdfe602SMatthew Dillon 		if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
65138efa82bSJohn Dyson 			obj = tmpe->object.vm_object;
6520774dfb3SAlan Cox 			if (obj != NULL) {
6530774dfb3SAlan Cox 				VM_OBJECT_LOCK(obj);
654ecf6279fSAlan Cox 				vm_pageout_object_deactivate_pages(map->pmap, obj, desired);
6550774dfb3SAlan Cox 				VM_OBJECT_UNLOCK(obj);
6560774dfb3SAlan Cox 			}
65738efa82bSJohn Dyson 		}
65826f9a767SRodney W. Grimes 		tmpe = tmpe->next;
65938857e7fSAlan Cox 	}
66038efa82bSJohn Dyson 
66138efa82bSJohn Dyson 	/*
66238efa82bSJohn Dyson 	 * Remove all mappings if a process is swapped out, this will free page
66338efa82bSJohn Dyson 	 * table pages.
66438efa82bSJohn Dyson 	 */
66538857e7fSAlan Cox 	if (desired == 0 && nothingwired) {
66605ba50f5SJake Burkholder 		pmap_remove(vm_map_pmap(map), vm_map_min(map),
66705ba50f5SJake Burkholder 		    vm_map_max(map));
66838857e7fSAlan Cox 	}
66938efa82bSJohn Dyson 	vm_map_unlock(map);
67026f9a767SRodney W. Grimes }
671a1287949SEivind Eklund #endif		/* !defined(NO_SWAPPING) */
672df8bae1dSRodney W. Grimes 
6731c7c3c6aSMatthew Dillon /*
674df8bae1dSRodney W. Grimes  *	vm_pageout_scan does the dirty work for the pageout daemon.
675df8bae1dSRodney W. Grimes  */
6762b6b0df7SMatthew Dillon static void
6772b6b0df7SMatthew Dillon vm_pageout_scan(int pass)
678df8bae1dSRodney W. Grimes {
679502ba6e4SJohn Dyson 	vm_page_t m, next;
680936524aaSMatthew Dillon 	struct vm_page marker;
6811c7c3c6aSMatthew Dillon 	int page_shortage, maxscan, pcount;
6821c7c3c6aSMatthew Dillon 	int addl_page_shortage, addl_page_shortage_init;
6835663e6deSDavid Greenman 	struct proc *p, *bigproc;
684dcbcd518SBruce Evans 	struct thread *td;
6855663e6deSDavid Greenman 	vm_offset_t size, bigsize;
686df8bae1dSRodney W. Grimes 	vm_object_t object;
687df2e33bfSAlan Cox 	int actcount, cache_cur, cache_first_failure;
688df2e33bfSAlan Cox 	static int cache_last_free;
689f6b04d2bSDavid Greenman 	int vnodes_skipped = 0;
6902b6b0df7SMatthew Dillon 	int maxlaunder;
6910d94caffSDavid Greenman 
6922e3b314dSAlan Cox 	mtx_lock(&Giant);
693df8bae1dSRodney W. Grimes 	/*
694855a310fSJeff Roberson 	 * Decrease registered cache sizes.
695855a310fSJeff Roberson 	 */
696855a310fSJeff Roberson 	EVENTHANDLER_INVOKE(vm_lowmem, 0);
697855a310fSJeff Roberson 	/*
698855a310fSJeff Roberson 	 * We do this explicitly after the caches have been drained above.
699855a310fSJeff Roberson 	 */
700855a310fSJeff Roberson 	uma_reclaim();
7015985940eSJohn Dyson 
702b0ef8c5fSAlan Cox 	addl_page_shortage_init = atomic_readandclear_int(&vm_pageout_deficit);
703b182ec9eSJohn Dyson 
7041c7c3c6aSMatthew Dillon 	/*
7051c7c3c6aSMatthew Dillon 	 * Calculate the number of pages we want to either free or move
7062b6b0df7SMatthew Dillon 	 * to the cache.
7071c7c3c6aSMatthew Dillon 	 */
7082b6b0df7SMatthew Dillon 	page_shortage = vm_paging_target() + addl_page_shortage_init;
7091c7c3c6aSMatthew Dillon 
7101c7c3c6aSMatthew Dillon 	/*
711936524aaSMatthew Dillon 	 * Initialize our marker
712936524aaSMatthew Dillon 	 */
713936524aaSMatthew Dillon 	bzero(&marker, sizeof(marker));
7149af80719SAlan Cox 	marker.flags = PG_FICTITIOUS | PG_MARKER;
7159af80719SAlan Cox 	marker.oflags = VPO_BUSY;
716936524aaSMatthew Dillon 	marker.queue = PQ_INACTIVE;
717936524aaSMatthew Dillon 	marker.wire_count = 1;
718936524aaSMatthew Dillon 
719936524aaSMatthew Dillon 	/*
7201c7c3c6aSMatthew Dillon 	 * Start scanning the inactive queue for pages we can move to the
7211c7c3c6aSMatthew Dillon 	 * cache or free.  The scan will stop when the target is reached or
722936524aaSMatthew Dillon 	 * we have scanned the entire inactive queue.  Note that m->act_count
723936524aaSMatthew Dillon 	 * is not used to form decisions for the inactive queue, only for the
724936524aaSMatthew Dillon 	 * active queue.
7252b6b0df7SMatthew Dillon 	 *
7262b6b0df7SMatthew Dillon 	 * maxlaunder limits the number of dirty pages we flush per scan.
7272b6b0df7SMatthew Dillon 	 * For most systems a smaller value (16 or 32) is more robust under
7282b6b0df7SMatthew Dillon 	 * extreme memory and disk pressure because any unnecessary writes
7292b6b0df7SMatthew Dillon 	 * to disk can result in extreme performance degredation.  However,
7302b6b0df7SMatthew Dillon 	 * systems with excessive dirty pages (especially when MAP_NOSYNC is
7312b6b0df7SMatthew Dillon 	 * used) will die horribly with limited laundering.  If the pageout
7322b6b0df7SMatthew Dillon 	 * daemon cannot clean enough pages in the first pass, we let it go
7332b6b0df7SMatthew Dillon 	 * all out in succeeding passes.
7341c7c3c6aSMatthew Dillon 	 */
7352b6b0df7SMatthew Dillon 	if ((maxlaunder = vm_max_launder) <= 1)
7362b6b0df7SMatthew Dillon 		maxlaunder = 1;
7372b6b0df7SMatthew Dillon 	if (pass)
7382b6b0df7SMatthew Dillon 		maxlaunder = 10000;
7393e1b578aSAlan Cox 	vm_page_lock_queues();
74067bf6868SJohn Dyson rescan0:
7411c7c3c6aSMatthew Dillon 	addl_page_shortage = addl_page_shortage_init;
7422feb50bfSAttilio Rao 	maxscan = cnt.v_inactive_count;
7436d03d577SMatthew Dillon 
744be72f788SAlan Cox 	for (m = TAILQ_FIRST(&vm_page_queues[PQ_INACTIVE].pl);
7451c7c3c6aSMatthew Dillon 	     m != NULL && maxscan-- > 0 && page_shortage > 0;
746e929c00dSKirk McKusick 	     m = next) {
747df8bae1dSRodney W. Grimes 
748b4b70819SAttilio Rao 		PCPU_INC(cnt.v_pdpages);
749b182ec9eSJohn Dyson 
750ef39c05bSAlexander Leidinger 		if (VM_PAGE_GETQUEUE(m) != PQ_INACTIVE) {
75167bf6868SJohn Dyson 			goto rescan0;
752f35329acSJohn Dyson 		}
753b182ec9eSJohn Dyson 
754b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(m, pageq);
75534d9e6fdSAlan Cox 		object = m->object;
756df8bae1dSRodney W. Grimes 
757936524aaSMatthew Dillon 		/*
758936524aaSMatthew Dillon 		 * skip marker pages
759936524aaSMatthew Dillon 		 */
760936524aaSMatthew Dillon 		if (m->flags & PG_MARKER)
761936524aaSMatthew Dillon 			continue;
762936524aaSMatthew Dillon 
76357601bcbSMatthew Dillon 		/*
76457601bcbSMatthew Dillon 		 * A held page may be undergoing I/O, so skip it.
76557601bcbSMatthew Dillon 		 */
766b182ec9eSJohn Dyson 		if (m->hold_count) {
7676d03d577SMatthew Dillon 			vm_pageq_requeue(m);
768b182ec9eSJohn Dyson 			addl_page_shortage++;
769b182ec9eSJohn Dyson 			continue;
770df8bae1dSRodney W. Grimes 		}
77126f9a767SRodney W. Grimes 		/*
772a1287949SEivind Eklund 		 * Don't mess with busy pages, keep in the front of the
773b18bfc3dSJohn Dyson 		 * queue, most likely are being paged out.
77426f9a767SRodney W. Grimes 		 */
7758dbca793STor Egge 		if (!VM_OBJECT_TRYLOCK(object) &&
7768dbca793STor Egge 		    (!vm_pageout_fallback_object_lock(m, &next) ||
7778dbca793STor Egge 		     m->hold_count != 0)) {
7788dbca793STor Egge 			VM_OBJECT_UNLOCK(object);
77934d9e6fdSAlan Cox 			addl_page_shortage++;
78034d9e6fdSAlan Cox 			continue;
78134d9e6fdSAlan Cox 		}
7829af80719SAlan Cox 		if (m->busy || (m->oflags & VPO_BUSY)) {
78334d9e6fdSAlan Cox 			VM_OBJECT_UNLOCK(object);
784b182ec9eSJohn Dyson 			addl_page_shortage++;
78526f9a767SRodney W. Grimes 			continue;
78626f9a767SRodney W. Grimes 		}
787bd7e5f99SJohn Dyson 
7887e006499SJohn Dyson 		/*
7891c7c3c6aSMatthew Dillon 		 * If the object is not being used, we ignore previous
7901c7c3c6aSMatthew Dillon 		 * references.
7917e006499SJohn Dyson 		 */
79234d9e6fdSAlan Cox 		if (object->ref_count == 0) {
793e69763a3SDoug Rabson 			vm_page_flag_clear(m, PG_REFERENCED);
7940385347cSPeter Wemm 			pmap_clear_reference(m);
7957e006499SJohn Dyson 
7967e006499SJohn Dyson 		/*
7971c7c3c6aSMatthew Dillon 		 * Otherwise, if the page has been referenced while in the
7981c7c3c6aSMatthew Dillon 		 * inactive queue, we bump the "activation count" upwards,
7991c7c3c6aSMatthew Dillon 		 * making it less likely that the page will be added back to
8001c7c3c6aSMatthew Dillon 		 * the inactive queue prematurely again.  Here we check the
8011c7c3c6aSMatthew Dillon 		 * page tables (or emulated bits, if any), given the upper
8021c7c3c6aSMatthew Dillon 		 * level VM system not knowing anything about existing
8031c7c3c6aSMatthew Dillon 		 * references.
8047e006499SJohn Dyson 		 */
805ef743ce6SJohn Dyson 		} else if (((m->flags & PG_REFERENCED) == 0) &&
8060385347cSPeter Wemm 			(actcount = pmap_ts_referenced(m))) {
807ef743ce6SJohn Dyson 			vm_page_activate(m);
80834d9e6fdSAlan Cox 			VM_OBJECT_UNLOCK(object);
8097e006499SJohn Dyson 			m->act_count += (actcount + ACT_ADVANCE);
810ef743ce6SJohn Dyson 			continue;
8112fe6e4d7SDavid Greenman 		}
812ef743ce6SJohn Dyson 
8137e006499SJohn Dyson 		/*
8141c7c3c6aSMatthew Dillon 		 * If the upper level VM system knows about any page
8151c7c3c6aSMatthew Dillon 		 * references, we activate the page.  We also set the
8161c7c3c6aSMatthew Dillon 		 * "activation count" higher than normal so that we will less
8171c7c3c6aSMatthew Dillon 		 * likely place pages back onto the inactive queue again.
8187e006499SJohn Dyson 		 */
819bd7e5f99SJohn Dyson 		if ((m->flags & PG_REFERENCED) != 0) {
820e69763a3SDoug Rabson 			vm_page_flag_clear(m, PG_REFERENCED);
8210385347cSPeter Wemm 			actcount = pmap_ts_referenced(m);
82226f9a767SRodney W. Grimes 			vm_page_activate(m);
82334d9e6fdSAlan Cox 			VM_OBJECT_UNLOCK(object);
8247e006499SJohn Dyson 			m->act_count += (actcount + ACT_ADVANCE + 1);
8250d94caffSDavid Greenman 			continue;
8260d94caffSDavid Greenman 		}
82767bf6868SJohn Dyson 
8287e006499SJohn Dyson 		/*
8291c7c3c6aSMatthew Dillon 		 * If the upper level VM system doesn't know anything about
8301c7c3c6aSMatthew Dillon 		 * the page being dirty, we have to check for it again.  As
8311c7c3c6aSMatthew Dillon 		 * far as the VM code knows, any partially dirty pages are
8321c7c3c6aSMatthew Dillon 		 * fully dirty.
8337e006499SJohn Dyson 		 */
83484d98bf6SAlan Cox 		if (m->dirty == 0 && !pmap_is_modified(m)) {
835a3dfacb5SAlan Cox 			/*
836a3dfacb5SAlan Cox 			 * Avoid a race condition: Unless write access is
837a3dfacb5SAlan Cox 			 * removed from the page, another processor could
838a3dfacb5SAlan Cox 			 * modify it before all access is removed by the call
839a3dfacb5SAlan Cox 			 * to vm_page_cache() below.  If vm_page_cache() finds
840a3dfacb5SAlan Cox 			 * that the page has been modified when it removes all
841a3dfacb5SAlan Cox 			 * access, it panics because it cannot cache dirty
842a3dfacb5SAlan Cox 			 * pages.  In principle, we could eliminate just write
843a3dfacb5SAlan Cox 			 * access here rather than all access.  In the expected
844a3dfacb5SAlan Cox 			 * case, when there are no last instant modifications
845a3dfacb5SAlan Cox 			 * to the page, removing all access will be cheaper
846a3dfacb5SAlan Cox 			 * overall.
847a3dfacb5SAlan Cox 			 */
84884d98bf6SAlan Cox 			if ((m->flags & PG_WRITEABLE) != 0)
849a3dfacb5SAlan Cox 				pmap_remove_all(m);
850427e99a0SAlexander Langer 		} else {
8517dbf82dcSMatthew Dillon 			vm_page_dirty(m);
85230dcfc09SJohn Dyson 		}
853dcbcd518SBruce Evans 
8546989c456SAlan Cox 		if (m->valid == 0) {
8557e006499SJohn Dyson 			/*
8567e006499SJohn Dyson 			 * Invalid pages can be easily freed
8577e006499SJohn Dyson 			 */
8586989c456SAlan Cox 			vm_page_free(m);
859b4b70819SAttilio Rao 			PCPU_INC(cnt.v_dfree);
8601c7c3c6aSMatthew Dillon 			--page_shortage;
861bd7e5f99SJohn Dyson 		} else if (m->dirty == 0) {
8626989c456SAlan Cox 			/*
8636989c456SAlan Cox 			 * Clean pages can be placed onto the cache queue.
8646989c456SAlan Cox 			 * This effectively frees them.
8656989c456SAlan Cox 			 */
866bd7e5f99SJohn Dyson 			vm_page_cache(m);
8671c7c3c6aSMatthew Dillon 			--page_shortage;
8682b6b0df7SMatthew Dillon 		} else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) {
8697e006499SJohn Dyson 			/*
8702b6b0df7SMatthew Dillon 			 * Dirty pages need to be paged out, but flushing
8712b6b0df7SMatthew Dillon 			 * a page is extremely expensive verses freeing
8722b6b0df7SMatthew Dillon 			 * a clean page.  Rather then artificially limiting
8732b6b0df7SMatthew Dillon 			 * the number of pages we can flush, we instead give
8742b6b0df7SMatthew Dillon 			 * dirty pages extra priority on the inactive queue
8752b6b0df7SMatthew Dillon 			 * by forcing them to be cycled through the queue
8762b6b0df7SMatthew Dillon 			 * twice before being flushed, after which the
8772b6b0df7SMatthew Dillon 			 * (now clean) page will cycle through once more
8782b6b0df7SMatthew Dillon 			 * before being freed.  This significantly extends
8792b6b0df7SMatthew Dillon 			 * the thrash point for a heavily loaded machine.
8807e006499SJohn Dyson 			 */
8812b6b0df7SMatthew Dillon 			vm_page_flag_set(m, PG_WINATCFLS);
8826d03d577SMatthew Dillon 			vm_pageq_requeue(m);
8830d94caffSDavid Greenman 		} else if (maxlaunder > 0) {
8842b6b0df7SMatthew Dillon 			/*
8852b6b0df7SMatthew Dillon 			 * We always want to try to flush some dirty pages if
8862b6b0df7SMatthew Dillon 			 * we encounter them, to keep the system stable.
8872b6b0df7SMatthew Dillon 			 * Normally this number is small, but under extreme
8882b6b0df7SMatthew Dillon 			 * pressure where there are insufficient clean pages
8892b6b0df7SMatthew Dillon 			 * on the inactive queue, we may have to go all out.
8902b6b0df7SMatthew Dillon 			 */
89112ac6a1dSJohn Dyson 			int swap_pageouts_ok;
892f6b04d2bSDavid Greenman 			struct vnode *vp = NULL;
893f2a2857bSKirk McKusick 			struct mount *mp;
8940d94caffSDavid Greenman 
89512ac6a1dSJohn Dyson 			if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) {
89612ac6a1dSJohn Dyson 				swap_pageouts_ok = 1;
89712ac6a1dSJohn Dyson 			} else {
89812ac6a1dSJohn Dyson 				swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts);
89912ac6a1dSJohn Dyson 				swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts &&
90090ecac61SMatthew Dillon 				vm_page_count_min());
90112ac6a1dSJohn Dyson 
90212ac6a1dSJohn Dyson 			}
90370111b90SJohn Dyson 
90470111b90SJohn Dyson 			/*
9051c7c3c6aSMatthew Dillon 			 * We don't bother paging objects that are "dead".
9061c7c3c6aSMatthew Dillon 			 * Those objects are in a "rundown" state.
90770111b90SJohn Dyson 			 */
90870111b90SJohn Dyson 			if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) {
9093562af12SAlan Cox 				VM_OBJECT_UNLOCK(object);
9106d03d577SMatthew Dillon 				vm_pageq_requeue(m);
91112ac6a1dSJohn Dyson 				continue;
91212ac6a1dSJohn Dyson 			}
91312ac6a1dSJohn Dyson 
9141c7c3c6aSMatthew Dillon 			/*
915625e6c0aSTor Egge 			 * Following operations may unlock
916625e6c0aSTor Egge 			 * vm_page_queue_mtx, invalidating the 'next'
917625e6c0aSTor Egge 			 * pointer.  To prevent an inordinate number
918625e6c0aSTor Egge 			 * of restarts we use our marker to remember
919625e6c0aSTor Egge 			 * our place.
920625e6c0aSTor Egge 			 *
921625e6c0aSTor Egge 			 */
922625e6c0aSTor Egge 			TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE].pl,
923625e6c0aSTor Egge 					   m, &marker, pageq);
924625e6c0aSTor Egge 			/*
9252b6b0df7SMatthew Dillon 			 * The object is already known NOT to be dead.   It
9262b6b0df7SMatthew Dillon 			 * is possible for the vget() to block the whole
9272b6b0df7SMatthew Dillon 			 * pageout daemon, but the new low-memory handling
9282b6b0df7SMatthew Dillon 			 * code should prevent it.
9291c7c3c6aSMatthew Dillon 			 *
9302b6b0df7SMatthew Dillon 			 * The previous code skipped locked vnodes and, worse,
9312b6b0df7SMatthew Dillon 			 * reordered pages in the queue.  This results in
9322b6b0df7SMatthew Dillon 			 * completely non-deterministic operation and, on a
9332b6b0df7SMatthew Dillon 			 * busy system, can lead to extremely non-optimal
9342b6b0df7SMatthew Dillon 			 * pageouts.  For example, it can cause clean pages
9352b6b0df7SMatthew Dillon 			 * to be freed and dirty pages to be moved to the end
9362b6b0df7SMatthew Dillon 			 * of the queue.  Since dirty pages are also moved to
9372b6b0df7SMatthew Dillon 			 * the end of the queue once-cleaned, this gives
9382b6b0df7SMatthew Dillon 			 * way too large a weighting to defering the freeing
9392b6b0df7SMatthew Dillon 			 * of dirty pages.
9401c7c3c6aSMatthew Dillon 			 *
94123b59018SMatthew Dillon 			 * We can't wait forever for the vnode lock, we might
94223b59018SMatthew Dillon 			 * deadlock due to a vn_read() getting stuck in
94323b59018SMatthew Dillon 			 * vm_wait while holding this vnode.  We skip the
94423b59018SMatthew Dillon 			 * vnode if we can't get it in a reasonable amount
94523b59018SMatthew Dillon 			 * of time.
9461c7c3c6aSMatthew Dillon 			 */
9471c7c3c6aSMatthew Dillon 			if (object->type == OBJT_VNODE) {
94824a1cce3SDavid Greenman 				vp = object->handle;
949f2a2857bSKirk McKusick 				mp = NULL;
950db27dcc0STor Egge 				if (vp->v_type == VREG &&
951db27dcc0STor Egge 				    vn_start_write(vp, &mp, V_NOWAIT) != 0) {
952db27dcc0STor Egge 					++pageout_lock_miss;
953db27dcc0STor Egge 					if (object->flags & OBJ_MIGHTBEDIRTY)
954db27dcc0STor Egge 						vnodes_skipped++;
955625e6c0aSTor Egge 					vp = NULL;
956625e6c0aSTor Egge 					goto unlock_and_continue;
957db27dcc0STor Egge 				}
9583e1b578aSAlan Cox 				vm_page_unlock_queues();
9594b5f5531SAlan Cox 				VI_LOCK(vp);
9603562af12SAlan Cox 				VM_OBJECT_UNLOCK(object);
9614b5f5531SAlan Cox 				if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK |
9624b5f5531SAlan Cox 				    LK_TIMELOCK, curthread)) {
9633562af12SAlan Cox 					VM_OBJECT_LOCK(object);
9643e1b578aSAlan Cox 					vm_page_lock_queues();
96523b59018SMatthew Dillon 					++pageout_lock_miss;
966f2a2857bSKirk McKusick 					vn_finished_write(mp);
967aef922f5SJohn Dyson 					if (object->flags & OBJ_MIGHTBEDIRTY)
968925a3a41SJohn Dyson 						vnodes_skipped++;
969625e6c0aSTor Egge 					vp = NULL;
970625e6c0aSTor Egge 					goto unlock_and_continue;
97185a376ebSJohn Dyson 				}
9723562af12SAlan Cox 				VM_OBJECT_LOCK(object);
9733e1b578aSAlan Cox 				vm_page_lock_queues();
974f35329acSJohn Dyson 				/*
975936524aaSMatthew Dillon 				 * The page might have been moved to another
976936524aaSMatthew Dillon 				 * queue during potential blocking in vget()
977936524aaSMatthew Dillon 				 * above.  The page might have been freed and
978936524aaSMatthew Dillon 				 * reused for another vnode.  The object might
979936524aaSMatthew Dillon 				 * have been reused for another vnode.
980f35329acSJohn Dyson 				 */
981ef39c05bSAlexander Leidinger 				if (VM_PAGE_GETQUEUE(m) != PQ_INACTIVE ||
982936524aaSMatthew Dillon 				    m->object != object ||
983625e6c0aSTor Egge 				    object->handle != vp ||
984625e6c0aSTor Egge 				    TAILQ_NEXT(m, pageq) != &marker) {
985b182ec9eSJohn Dyson 					if (object->flags & OBJ_MIGHTBEDIRTY)
986925a3a41SJohn Dyson 						vnodes_skipped++;
9873562af12SAlan Cox 					goto unlock_and_continue;
988b182ec9eSJohn Dyson 				}
989b182ec9eSJohn Dyson 
990f35329acSJohn Dyson 				/*
991936524aaSMatthew Dillon 				 * The page may have been busied during the
992936524aaSMatthew Dillon 				 * blocking in vput();  We don't move the
993936524aaSMatthew Dillon 				 * page back onto the end of the queue so that
994936524aaSMatthew Dillon 				 * statistics are more correct if we don't.
995f35329acSJohn Dyson 				 */
9969af80719SAlan Cox 				if (m->busy || (m->oflags & VPO_BUSY)) {
9973562af12SAlan Cox 					goto unlock_and_continue;
998b182ec9eSJohn Dyson 				}
999b182ec9eSJohn Dyson 
1000f35329acSJohn Dyson 				/*
100157601bcbSMatthew Dillon 				 * If the page has become held it might
100257601bcbSMatthew Dillon 				 * be undergoing I/O, so skip it
1003f35329acSJohn Dyson 				 */
1004b182ec9eSJohn Dyson 				if (m->hold_count) {
10056d03d577SMatthew Dillon 					vm_pageq_requeue(m);
1006b182ec9eSJohn Dyson 					if (object->flags & OBJ_MIGHTBEDIRTY)
1007925a3a41SJohn Dyson 						vnodes_skipped++;
10083562af12SAlan Cox 					goto unlock_and_continue;
1009f6b04d2bSDavid Greenman 				}
1010f6b04d2bSDavid Greenman 			}
1011f6b04d2bSDavid Greenman 
10120d94caffSDavid Greenman 			/*
10130d94caffSDavid Greenman 			 * If a page is dirty, then it is either being washed
10140d94caffSDavid Greenman 			 * (but not yet cleaned) or it is still in the
10150d94caffSDavid Greenman 			 * laundry.  If it is still in the laundry, then we
10162b6b0df7SMatthew Dillon 			 * start the cleaning operation.
1017936524aaSMatthew Dillon 			 *
10182b6b0df7SMatthew Dillon 			 * decrement page_shortage on success to account for
10192b6b0df7SMatthew Dillon 			 * the (future) cleaned page.  Otherwise we could wind
10202b6b0df7SMatthew Dillon 			 * up laundering or cleaning too many pages.
10210d94caffSDavid Greenman 			 */
10222b6b0df7SMatthew Dillon 			if (vm_pageout_clean(m) != 0) {
10232b6b0df7SMatthew Dillon 				--page_shortage;
1024936524aaSMatthew Dillon 				--maxlaunder;
10252b6b0df7SMatthew Dillon 			}
10263562af12SAlan Cox unlock_and_continue:
10276989c456SAlan Cox 			VM_OBJECT_UNLOCK(object);
1028f2a2857bSKirk McKusick 			if (vp) {
10296989c456SAlan Cox 				vm_page_unlock_queues();
1030f6b04d2bSDavid Greenman 				vput(vp);
1031f2a2857bSKirk McKusick 				vn_finished_write(mp);
10326989c456SAlan Cox 				vm_page_lock_queues();
10336989c456SAlan Cox 			}
1034625e6c0aSTor Egge 			next = TAILQ_NEXT(&marker, pageq);
1035625e6c0aSTor Egge 			TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl,
1036625e6c0aSTor Egge 				     &marker, pageq);
10376989c456SAlan Cox 			continue;
1038f2a2857bSKirk McKusick 		}
10393562af12SAlan Cox 		VM_OBJECT_UNLOCK(object);
10400d94caffSDavid Greenman 	}
104126f9a767SRodney W. Grimes 
1042df8bae1dSRodney W. Grimes 	/*
1043936524aaSMatthew Dillon 	 * Compute the number of pages we want to try to move from the
1044936524aaSMatthew Dillon 	 * active queue to the inactive queue.
10451c7c3c6aSMatthew Dillon 	 */
10462feb50bfSAttilio Rao 	page_shortage = vm_paging_target() +
10472feb50bfSAttilio Rao 		cnt.v_inactive_target - cnt.v_inactive_count;
1048b182ec9eSJohn Dyson 	page_shortage += addl_page_shortage;
10491c7c3c6aSMatthew Dillon 
10501c7c3c6aSMatthew Dillon 	/*
1051936524aaSMatthew Dillon 	 * Scan the active queue for things we can deactivate. We nominally
1052936524aaSMatthew Dillon 	 * track the per-page activity counter and use it to locate
1053936524aaSMatthew Dillon 	 * deactivation candidates.
10541c7c3c6aSMatthew Dillon 	 */
10552feb50bfSAttilio Rao 	pcount = cnt.v_active_count;
1056be72f788SAlan Cox 	m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
10571c7c3c6aSMatthew Dillon 
1058b18bfc3dSJohn Dyson 	while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) {
1059f35329acSJohn Dyson 
1060ef39c05bSAlexander Leidinger 		KASSERT(VM_PAGE_INQUEUE2(m, PQ_ACTIVE),
1061d3c09dd7SAlan Cox 		    ("vm_pageout_scan: page %p isn't active", m));
1062f35329acSJohn Dyson 
1063b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(m, pageq);
1064b08abf6cSAlan Cox 		object = m->object;
10658dbca793STor Egge 		if ((m->flags & PG_MARKER) != 0) {
10668dbca793STor Egge 			m = next;
10678dbca793STor Egge 			continue;
10688dbca793STor Egge 		}
10698dbca793STor Egge 		if (!VM_OBJECT_TRYLOCK(object) &&
10708dbca793STor Egge 		    !vm_pageout_fallback_object_lock(m, &next)) {
10718dbca793STor Egge 			VM_OBJECT_UNLOCK(object);
10724b8a5c40SAlan Cox 			m = next;
1073b08abf6cSAlan Cox 			continue;
1074b08abf6cSAlan Cox 		}
1075b08abf6cSAlan Cox 
1076df8bae1dSRodney W. Grimes 		/*
107726f9a767SRodney W. Grimes 		 * Don't deactivate pages that are busy.
1078df8bae1dSRodney W. Grimes 		 */
1079a647a309SDavid Greenman 		if ((m->busy != 0) ||
10809af80719SAlan Cox 		    (m->oflags & VPO_BUSY) ||
1081f6b04d2bSDavid Greenman 		    (m->hold_count != 0)) {
1082b08abf6cSAlan Cox 			VM_OBJECT_UNLOCK(object);
10836d03d577SMatthew Dillon 			vm_pageq_requeue(m);
108426f9a767SRodney W. Grimes 			m = next;
108526f9a767SRodney W. Grimes 			continue;
1086df8bae1dSRodney W. Grimes 		}
1087b18bfc3dSJohn Dyson 
1088b18bfc3dSJohn Dyson 		/*
1089b18bfc3dSJohn Dyson 		 * The count for pagedaemon pages is done after checking the
1090956f3135SPhilippe Charnier 		 * page for eligibility...
1091b18bfc3dSJohn Dyson 		 */
1092b4b70819SAttilio Rao 		PCPU_INC(cnt.v_pdpages);
1093ef743ce6SJohn Dyson 
10947e006499SJohn Dyson 		/*
10957e006499SJohn Dyson 		 * Check to see "how much" the page has been used.
10967e006499SJohn Dyson 		 */
10977e006499SJohn Dyson 		actcount = 0;
1098b08abf6cSAlan Cox 		if (object->ref_count != 0) {
1099ef743ce6SJohn Dyson 			if (m->flags & PG_REFERENCED) {
11007e006499SJohn Dyson 				actcount += 1;
11010d94caffSDavid Greenman 			}
11020385347cSPeter Wemm 			actcount += pmap_ts_referenced(m);
11037e006499SJohn Dyson 			if (actcount) {
11047e006499SJohn Dyson 				m->act_count += ACT_ADVANCE + actcount;
110538efa82bSJohn Dyson 				if (m->act_count > ACT_MAX)
110638efa82bSJohn Dyson 					m->act_count = ACT_MAX;
110738efa82bSJohn Dyson 			}
1108b18bfc3dSJohn Dyson 		}
1109ef743ce6SJohn Dyson 
11107e006499SJohn Dyson 		/*
11117e006499SJohn Dyson 		 * Since we have "tested" this bit, we need to clear it now.
11127e006499SJohn Dyson 		 */
1113e69763a3SDoug Rabson 		vm_page_flag_clear(m, PG_REFERENCED);
1114ef743ce6SJohn Dyson 
11157e006499SJohn Dyson 		/*
11167e006499SJohn Dyson 		 * Only if an object is currently being used, do we use the
11177e006499SJohn Dyson 		 * page activation count stats.
11187e006499SJohn Dyson 		 */
1119b08abf6cSAlan Cox 		if (actcount && (object->ref_count != 0)) {
11206d03d577SMatthew Dillon 			vm_pageq_requeue(m);
112126f9a767SRodney W. Grimes 		} else {
112238efa82bSJohn Dyson 			m->act_count -= min(m->act_count, ACT_DECLINE);
11232b6b0df7SMatthew Dillon 			if (vm_pageout_algorithm ||
1124b08abf6cSAlan Cox 			    object->ref_count == 0 ||
11252b6b0df7SMatthew Dillon 			    m->act_count == 0) {
1126925a3a41SJohn Dyson 				page_shortage--;
1127b08abf6cSAlan Cox 				if (object->ref_count == 0) {
11284fec79beSAlan Cox 					pmap_remove_all(m);
1129d4a272dbSJohn Dyson 					if (m->dirty == 0)
11300d94caffSDavid Greenman 						vm_page_cache(m);
1131d4a272dbSJohn Dyson 					else
1132d4a272dbSJohn Dyson 						vm_page_deactivate(m);
11330d94caffSDavid Greenman 				} else {
113426f9a767SRodney W. Grimes 					vm_page_deactivate(m);
1135df8bae1dSRodney W. Grimes 				}
113638efa82bSJohn Dyson 			} else {
11376d03d577SMatthew Dillon 				vm_pageq_requeue(m);
113838efa82bSJohn Dyson 			}
1139df8bae1dSRodney W. Grimes 		}
1140b08abf6cSAlan Cox 		VM_OBJECT_UNLOCK(object);
114126f9a767SRodney W. Grimes 		m = next;
114226f9a767SRodney W. Grimes 	}
11431c7c3c6aSMatthew Dillon 
1144df8bae1dSRodney W. Grimes 	/*
11450d94caffSDavid Greenman 	 * We try to maintain some *really* free pages, this allows interrupt
11461c7c3c6aSMatthew Dillon 	 * code to be guaranteed space.  Since both cache and free queues
11471c7c3c6aSMatthew Dillon 	 * are considered basically 'free', moving pages from cache to free
11481c7c3c6aSMatthew Dillon 	 * does not effect other calculations.
1149df8bae1dSRodney W. Grimes 	 */
1150df2e33bfSAlan Cox 	cache_cur = cache_last_free;
1151df2e33bfSAlan Cox 	cache_first_failure = -1;
11522feb50bfSAttilio Rao 	while (cnt.v_free_count < cnt.v_free_reserved && (cache_cur =
11532feb50bfSAttilio Rao 	    (cache_cur + PQ_PRIME2) & PQ_COLORMASK) != cache_first_failure) {
1154df2e33bfSAlan Cox 		TAILQ_FOREACH(m, &vm_page_queues[PQ_CACHE + cache_cur].pl,
1155df2e33bfSAlan Cox 		    pageq) {
1156df2e33bfSAlan Cox 			KASSERT(m->dirty == 0,
1157df2e33bfSAlan Cox 			    ("Found dirty cache page %p", m));
1158df2e33bfSAlan Cox 			KASSERT(!pmap_page_is_mapped(m),
1159df2e33bfSAlan Cox 			    ("Found mapped cache page %p", m));
1160df2e33bfSAlan Cox 			KASSERT((m->flags & PG_UNMANAGED) == 0,
1161df2e33bfSAlan Cox 			    ("Found unmanaged cache page %p", m));
1162df2e33bfSAlan Cox 			KASSERT(m->wire_count == 0,
1163df2e33bfSAlan Cox 			    ("Found wired cache page %p", m));
1164df2e33bfSAlan Cox 			if (m->hold_count == 0 && VM_OBJECT_TRYLOCK(object =
1165df2e33bfSAlan Cox 			    m->object)) {
11669af80719SAlan Cox 				KASSERT((m->oflags & VPO_BUSY) == 0 &&
1167df2e33bfSAlan Cox 				    m->busy == 0, ("Found busy cache page %p",
1168df2e33bfSAlan Cox 				    m));
1169ab42316cSAlan Cox 				vm_page_free(m);
1170ab42316cSAlan Cox 				VM_OBJECT_UNLOCK(object);
1171b4b70819SAttilio Rao 				PCPU_INC(cnt.v_dfree);
1172df2e33bfSAlan Cox 				cache_last_free = cache_cur;
1173df2e33bfSAlan Cox 				cache_first_failure = -1;
1174df2e33bfSAlan Cox 				break;
1175df2e33bfSAlan Cox 			}
1176df2e33bfSAlan Cox 		}
1177df2e33bfSAlan Cox 		if (m == NULL && cache_first_failure == -1)
1178df2e33bfSAlan Cox 			cache_first_failure = cache_cur;
117926f9a767SRodney W. Grimes 	}
11808ffc1519SAlan Cox 	vm_page_unlock_queues();
1181ceb0cf87SJohn Dyson #if !defined(NO_SWAPPING)
1182ceb0cf87SJohn Dyson 	/*
1183ceb0cf87SJohn Dyson 	 * Idle process swapout -- run once per second.
1184ceb0cf87SJohn Dyson 	 */
1185ceb0cf87SJohn Dyson 	if (vm_swap_idle_enabled) {
1186ceb0cf87SJohn Dyson 		static long lsec;
1187227ee8a1SPoul-Henning Kamp 		if (time_second != lsec) {
1188ceb0cf87SJohn Dyson 			vm_pageout_req_swapout |= VM_SWAP_IDLE;
1189ceb0cf87SJohn Dyson 			vm_req_vmdaemon();
1190227ee8a1SPoul-Henning Kamp 			lsec = time_second;
1191ceb0cf87SJohn Dyson 		}
1192ceb0cf87SJohn Dyson 	}
1193ceb0cf87SJohn Dyson #endif
1194ceb0cf87SJohn Dyson 
11955663e6deSDavid Greenman 	/*
1196f6b04d2bSDavid Greenman 	 * If we didn't get enough free pages, and we have skipped a vnode
11974c1f8ee9SDavid Greenman 	 * in a writeable object, wakeup the sync daemon.  And kick swapout
11984c1f8ee9SDavid Greenman 	 * if we did not get enough free pages.
1199f6b04d2bSDavid Greenman 	 */
120090ecac61SMatthew Dillon 	if (vm_paging_target() > 0) {
120190ecac61SMatthew Dillon 		if (vnodes_skipped && vm_page_count_min())
1202d50c1994SPeter Wemm 			(void) speedup_syncer();
120338efa82bSJohn Dyson #if !defined(NO_SWAPPING)
120490ecac61SMatthew Dillon 		if (vm_swap_enabled && vm_page_count_target()) {
12054c1f8ee9SDavid Greenman 			vm_req_vmdaemon();
1206ceb0cf87SJohn Dyson 			vm_pageout_req_swapout |= VM_SWAP_NORMAL;
12074c1f8ee9SDavid Greenman 		}
12085afce282SDavid Greenman #endif
12094c1f8ee9SDavid Greenman 	}
12104c1f8ee9SDavid Greenman 
1211f6b04d2bSDavid Greenman 	/*
1212e92686d0SDavid Schultz 	 * If we are critically low on one of RAM or swap and low on
1213e92686d0SDavid Schultz 	 * the other, kill the largest process.  However, we avoid
1214e92686d0SDavid Schultz 	 * doing this on the first pass in order to give ourselves a
1215e92686d0SDavid Schultz 	 * chance to flush out dirty vnode-backed pages and to allow
1216e92686d0SDavid Schultz 	 * active pages to be moved to the inactive queue and reclaimed.
12171c58e4e5SJohn Baldwin 	 *
12181c58e4e5SJohn Baldwin 	 * We keep the process bigproc locked once we find it to keep anyone
12191c58e4e5SJohn Baldwin 	 * from messing with it; however, there is a possibility of
12201c58e4e5SJohn Baldwin 	 * deadlock if process B is bigproc and one of it's child processes
12211c58e4e5SJohn Baldwin 	 * attempts to propagate a signal to B while we are waiting for A's
12221c58e4e5SJohn Baldwin 	 * lock while walking this list.  To avoid this, we don't block on
12231c58e4e5SJohn Baldwin 	 * the process lock but just skip a process if it is already locked.
12245663e6deSDavid Greenman 	 */
1225e92686d0SDavid Schultz 	if (pass != 0 &&
12268f60c087SPoul-Henning Kamp 	    ((swap_pager_avail < 64 && vm_page_count_min()) ||
1227e92686d0SDavid Schultz 	     (swap_pager_full && vm_paging_target() > 0))) {
12285663e6deSDavid Greenman 		bigproc = NULL;
12295663e6deSDavid Greenman 		bigsize = 0;
12301005a129SJohn Baldwin 		sx_slock(&allproc_lock);
1231e602ba25SJulian Elischer 		FOREACH_PROC_IN_SYSTEM(p) {
1232e602ba25SJulian Elischer 			int breakout;
1233dcbcd518SBruce Evans 
12341c58e4e5SJohn Baldwin 			if (PROC_TRYLOCK(p) == 0)
12351c58e4e5SJohn Baldwin 				continue;
12361c58e4e5SJohn Baldwin 			/*
1237f4cf2141SWes Peters 			 * If this is a system or protected process, skip it.
12385663e6deSDavid Greenman 			 */
1239ef6020d1SMike Silbersack 			if ((p->p_flag & P_SYSTEM) || (p->p_pid == 1) ||
1240f4cf2141SWes Peters 			    (p->p_flag & P_PROTECTED) ||
12418f60c087SPoul-Henning Kamp 			    ((p->p_pid < 48) && (swap_pager_avail != 0))) {
12428606d880SJohn Baldwin 				PROC_UNLOCK(p);
12435663e6deSDavid Greenman 				continue;
12445663e6deSDavid Greenman 			}
12455663e6deSDavid Greenman 			/*
1246dcbcd518SBruce Evans 			 * If the process is in a non-running type state,
1247e602ba25SJulian Elischer 			 * don't touch it.  Check all the threads individually.
12485663e6deSDavid Greenman 			 */
1249982d11f8SJeff Roberson 			PROC_SLOCK(p);
1250e602ba25SJulian Elischer 			breakout = 0;
1251e602ba25SJulian Elischer 			FOREACH_THREAD_IN_PROC(p, td) {
1252982d11f8SJeff Roberson 				thread_lock(td);
125371fad9fdSJulian Elischer 				if (!TD_ON_RUNQ(td) &&
125471fad9fdSJulian Elischer 				    !TD_IS_RUNNING(td) &&
125571fad9fdSJulian Elischer 				    !TD_IS_SLEEPING(td)) {
1256982d11f8SJeff Roberson 					thread_unlock(td);
1257e602ba25SJulian Elischer 					breakout = 1;
1258e602ba25SJulian Elischer 					break;
1259e602ba25SJulian Elischer 				}
1260982d11f8SJeff Roberson 				thread_unlock(td);
1261e602ba25SJulian Elischer 			}
1262982d11f8SJeff Roberson 			PROC_SUNLOCK(p);
1263e602ba25SJulian Elischer 			if (breakout) {
12641c58e4e5SJohn Baldwin 				PROC_UNLOCK(p);
12655663e6deSDavid Greenman 				continue;
12665663e6deSDavid Greenman 			}
12675663e6deSDavid Greenman 			/*
12685663e6deSDavid Greenman 			 * get the process size
12695663e6deSDavid Greenman 			 */
127072d97679SDavid Schultz 			if (!vm_map_trylock_read(&p->p_vmspace->vm_map)) {
127172d97679SDavid Schultz 				PROC_UNLOCK(p);
127272d97679SDavid Schultz 				continue;
127372d97679SDavid Schultz 			}
127472d97679SDavid Schultz 			size = vmspace_swap_count(p->p_vmspace);
127572d97679SDavid Schultz 			vm_map_unlock_read(&p->p_vmspace->vm_map);
127672d97679SDavid Schultz 			size += vmspace_resident_count(p->p_vmspace);
12775663e6deSDavid Greenman 			/*
12785663e6deSDavid Greenman 			 * if the this process is bigger than the biggest one
12795663e6deSDavid Greenman 			 * remember it.
12805663e6deSDavid Greenman 			 */
12815663e6deSDavid Greenman 			if (size > bigsize) {
12821c58e4e5SJohn Baldwin 				if (bigproc != NULL)
12831c58e4e5SJohn Baldwin 					PROC_UNLOCK(bigproc);
12845663e6deSDavid Greenman 				bigproc = p;
12855663e6deSDavid Greenman 				bigsize = size;
12861c58e4e5SJohn Baldwin 			} else
12871c58e4e5SJohn Baldwin 				PROC_UNLOCK(p);
12885663e6deSDavid Greenman 		}
12891005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
12905663e6deSDavid Greenman 		if (bigproc != NULL) {
1291729b1e51SDavid Greenman 			killproc(bigproc, "out of swap space");
1292982d11f8SJeff Roberson 			PROC_SLOCK(bigproc);
1293fa885116SJulian Elischer 			sched_nice(bigproc, PRIO_MIN);
1294982d11f8SJeff Roberson 			PROC_SUNLOCK(bigproc);
12951c58e4e5SJohn Baldwin 			PROC_UNLOCK(bigproc);
12962feb50bfSAttilio Rao 			wakeup(&cnt.v_free_count);
12975663e6deSDavid Greenman 		}
12985663e6deSDavid Greenman 	}
12992e3b314dSAlan Cox 	mtx_unlock(&Giant);
130026f9a767SRodney W. Grimes }
130126f9a767SRodney W. Grimes 
1302dc2efb27SJohn Dyson /*
1303dc2efb27SJohn Dyson  * This routine tries to maintain the pseudo LRU active queue,
1304dc2efb27SJohn Dyson  * so that during long periods of time where there is no paging,
1305956f3135SPhilippe Charnier  * that some statistic accumulation still occurs.  This code
1306dc2efb27SJohn Dyson  * helps the situation where paging just starts to occur.
1307dc2efb27SJohn Dyson  */
1308dc2efb27SJohn Dyson static void
1309dc2efb27SJohn Dyson vm_pageout_page_stats()
1310dc2efb27SJohn Dyson {
1311b86e6ec0SAlan Cox 	vm_object_t object;
1312dc2efb27SJohn Dyson 	vm_page_t m,next;
1313dc2efb27SJohn Dyson 	int pcount,tpcount;		/* Number of pages to check */
1314dc2efb27SJohn Dyson 	static int fullintervalcount = 0;
1315bef608bdSJohn Dyson 	int page_shortage;
1316bef608bdSJohn Dyson 
13175e609009SAlan Cox 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
131890ecac61SMatthew Dillon 	page_shortage =
13192feb50bfSAttilio Rao 	    (cnt.v_inactive_target + cnt.v_cache_max + cnt.v_free_min) -
13202feb50bfSAttilio Rao 	    (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count);
132190ecac61SMatthew Dillon 
1322bef608bdSJohn Dyson 	if (page_shortage <= 0)
1323bef608bdSJohn Dyson 		return;
1324dc2efb27SJohn Dyson 
13252feb50bfSAttilio Rao 	pcount = cnt.v_active_count;
1326dc2efb27SJohn Dyson 	fullintervalcount += vm_pageout_stats_interval;
1327dc2efb27SJohn Dyson 	if (fullintervalcount < vm_pageout_full_stats_interval) {
13282feb50bfSAttilio Rao 		tpcount = (vm_pageout_stats_max * cnt.v_active_count) / cnt.v_page_count;
1329dc2efb27SJohn Dyson 		if (pcount > tpcount)
1330dc2efb27SJohn Dyson 			pcount = tpcount;
1331883f3caaSMatthew Dillon 	} else {
1332883f3caaSMatthew Dillon 		fullintervalcount = 0;
1333dc2efb27SJohn Dyson 	}
1334dc2efb27SJohn Dyson 
1335be72f788SAlan Cox 	m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
1336dc2efb27SJohn Dyson 	while ((m != NULL) && (pcount-- > 0)) {
13377e006499SJohn Dyson 		int actcount;
1338dc2efb27SJohn Dyson 
1339ef39c05bSAlexander Leidinger 		KASSERT(VM_PAGE_INQUEUE2(m, PQ_ACTIVE),
1340ab42316cSAlan Cox 		    ("vm_pageout_page_stats: page %p isn't active", m));
1341dc2efb27SJohn Dyson 
1342dc2efb27SJohn Dyson 		next = TAILQ_NEXT(m, pageq);
1343b86e6ec0SAlan Cox 		object = m->object;
13448dbca793STor Egge 
13458dbca793STor Egge 		if ((m->flags & PG_MARKER) != 0) {
13468dbca793STor Egge 			m = next;
13478dbca793STor Egge 			continue;
13488dbca793STor Egge 		}
13498dbca793STor Egge 		if (!VM_OBJECT_TRYLOCK(object) &&
13508dbca793STor Egge 		    !vm_pageout_fallback_object_lock(m, &next)) {
13518dbca793STor Egge 			VM_OBJECT_UNLOCK(object);
1352b86e6ec0SAlan Cox 			m = next;
1353b86e6ec0SAlan Cox 			continue;
1354b86e6ec0SAlan Cox 		}
1355b86e6ec0SAlan Cox 
1356dc2efb27SJohn Dyson 		/*
1357dc2efb27SJohn Dyson 		 * Don't deactivate pages that are busy.
1358dc2efb27SJohn Dyson 		 */
1359dc2efb27SJohn Dyson 		if ((m->busy != 0) ||
13609af80719SAlan Cox 		    (m->oflags & VPO_BUSY) ||
1361dc2efb27SJohn Dyson 		    (m->hold_count != 0)) {
1362b86e6ec0SAlan Cox 			VM_OBJECT_UNLOCK(object);
13636d03d577SMatthew Dillon 			vm_pageq_requeue(m);
1364dc2efb27SJohn Dyson 			m = next;
1365dc2efb27SJohn Dyson 			continue;
1366dc2efb27SJohn Dyson 		}
1367dc2efb27SJohn Dyson 
13687e006499SJohn Dyson 		actcount = 0;
1369dc2efb27SJohn Dyson 		if (m->flags & PG_REFERENCED) {
1370e69763a3SDoug Rabson 			vm_page_flag_clear(m, PG_REFERENCED);
13717e006499SJohn Dyson 			actcount += 1;
1372dc2efb27SJohn Dyson 		}
1373dc2efb27SJohn Dyson 
13740385347cSPeter Wemm 		actcount += pmap_ts_referenced(m);
13757e006499SJohn Dyson 		if (actcount) {
13767e006499SJohn Dyson 			m->act_count += ACT_ADVANCE + actcount;
1377dc2efb27SJohn Dyson 			if (m->act_count > ACT_MAX)
1378dc2efb27SJohn Dyson 				m->act_count = ACT_MAX;
13796d03d577SMatthew Dillon 			vm_pageq_requeue(m);
1380dc2efb27SJohn Dyson 		} else {
1381dc2efb27SJohn Dyson 			if (m->act_count == 0) {
13827e006499SJohn Dyson 				/*
13832b6b0df7SMatthew Dillon 				 * We turn off page access, so that we have
13842b6b0df7SMatthew Dillon 				 * more accurate RSS stats.  We don't do this
13852b6b0df7SMatthew Dillon 				 * in the normal page deactivation when the
13862b6b0df7SMatthew Dillon 				 * system is loaded VM wise, because the
13872b6b0df7SMatthew Dillon 				 * cost of the large number of page protect
13882b6b0df7SMatthew Dillon 				 * operations would be higher than the value
13892b6b0df7SMatthew Dillon 				 * of doing the operation.
13907e006499SJohn Dyson 				 */
13914fec79beSAlan Cox 				pmap_remove_all(m);
1392dc2efb27SJohn Dyson 				vm_page_deactivate(m);
1393dc2efb27SJohn Dyson 			} else {
1394dc2efb27SJohn Dyson 				m->act_count -= min(m->act_count, ACT_DECLINE);
13956d03d577SMatthew Dillon 				vm_pageq_requeue(m);
1396dc2efb27SJohn Dyson 			}
1397dc2efb27SJohn Dyson 		}
1398b86e6ec0SAlan Cox 		VM_OBJECT_UNLOCK(object);
1399dc2efb27SJohn Dyson 		m = next;
1400dc2efb27SJohn Dyson 	}
1401dc2efb27SJohn Dyson }
1402dc2efb27SJohn Dyson 
1403df8bae1dSRodney W. Grimes /*
1404df8bae1dSRodney W. Grimes  *	vm_pageout is the high level pageout daemon.
1405df8bae1dSRodney W. Grimes  */
14062b14f991SJulian Elischer static void
140726f9a767SRodney W. Grimes vm_pageout()
1408df8bae1dSRodney W. Grimes {
14091aab16a6SAlan Cox 	int error, pass;
14100384fff8SJason Evans 
1411df8bae1dSRodney W. Grimes 	/*
1412df8bae1dSRodney W. Grimes 	 * Initialize some paging parameters.
1413df8bae1dSRodney W. Grimes 	 */
14142feb50bfSAttilio Rao 	cnt.v_interrupt_free_min = 2;
14152feb50bfSAttilio Rao 	if (cnt.v_page_count < 2000)
1416f35329acSJohn Dyson 		vm_pageout_page_count = 8;
1417f6b04d2bSDavid Greenman 
141845ae1d91SAlan Cox 	/*
141945ae1d91SAlan Cox 	 * v_free_reserved needs to include enough for the largest
142045ae1d91SAlan Cox 	 * swap pager structures plus enough for any pv_entry structs
142145ae1d91SAlan Cox 	 * when paging.
142245ae1d91SAlan Cox 	 */
14232feb50bfSAttilio Rao 	if (cnt.v_page_count > 1024)
14242feb50bfSAttilio Rao 		cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200;
14252feb50bfSAttilio Rao 	else
14262feb50bfSAttilio Rao 		cnt.v_free_min = 4;
14272feb50bfSAttilio Rao 	cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE +
14282feb50bfSAttilio Rao 	    cnt.v_interrupt_free_min;
14292feb50bfSAttilio Rao 	cnt.v_free_reserved = vm_pageout_page_count +
14302feb50bfSAttilio Rao 	    cnt.v_pageout_free_min + (cnt.v_page_count / 768) + PQ_NUMCOLORS;
14312feb50bfSAttilio Rao 	cnt.v_free_severe = cnt.v_free_min / 2;
1432b4b70819SAttilio Rao 
1433b4b70819SAttilio Rao 	/*
1434b4b70819SAttilio Rao 	 * Here adds don't need to be atomic since we are only initializing
1435b4b70819SAttilio Rao 	 * v_free_min and v_free_severe.
1436b4b70819SAttilio Rao 	 */
14372feb50bfSAttilio Rao 	cnt.v_free_min += cnt.v_free_reserved;
14382feb50bfSAttilio Rao 	cnt.v_free_severe += cnt.v_free_reserved;
143945ae1d91SAlan Cox 
1440ed74321bSDavid Greenman 	/*
14412b6b0df7SMatthew Dillon 	 * v_free_target and v_cache_min control pageout hysteresis.  Note
14422b6b0df7SMatthew Dillon 	 * that these are more a measure of the VM cache queue hysteresis
14432b6b0df7SMatthew Dillon 	 * then the VM free queue.  Specifically, v_free_target is the
14442b6b0df7SMatthew Dillon 	 * high water mark (free+cache pages).
14452b6b0df7SMatthew Dillon 	 *
14462b6b0df7SMatthew Dillon 	 * v_free_reserved + v_cache_min (mostly means v_cache_min) is the
14472b6b0df7SMatthew Dillon 	 * low water mark, while v_free_min is the stop.  v_cache_min must
14482b6b0df7SMatthew Dillon 	 * be big enough to handle memory needs while the pageout daemon
14492b6b0df7SMatthew Dillon 	 * is signalled and run to free more pages.
1450ed74321bSDavid Greenman 	 */
14512feb50bfSAttilio Rao 	if (cnt.v_free_count > 6144)
14522feb50bfSAttilio Rao 		cnt.v_free_target = 4 * cnt.v_free_min + cnt.v_free_reserved;
14532feb50bfSAttilio Rao 	else
14542feb50bfSAttilio Rao 		cnt.v_free_target = 2 * cnt.v_free_min + cnt.v_free_reserved;
14556f2b142eSDavid Greenman 
14562feb50bfSAttilio Rao 	if (cnt.v_free_count > 2048) {
14572feb50bfSAttilio Rao 		cnt.v_cache_min = cnt.v_free_target;
14582feb50bfSAttilio Rao 		cnt.v_cache_max = 2 * cnt.v_cache_min;
14592feb50bfSAttilio Rao 		cnt.v_inactive_target = (3 * cnt.v_free_target) / 2;
14600d94caffSDavid Greenman 	} else {
14612feb50bfSAttilio Rao 		cnt.v_cache_min = 0;
14622feb50bfSAttilio Rao 		cnt.v_cache_max = 0;
14632feb50bfSAttilio Rao 		cnt.v_inactive_target = cnt.v_free_count / 4;
14640d94caffSDavid Greenman 	}
14652feb50bfSAttilio Rao 	if (cnt.v_inactive_target > cnt.v_free_count / 3)
14662feb50bfSAttilio Rao 		cnt.v_inactive_target = cnt.v_free_count / 3;
1467df8bae1dSRodney W. Grimes 
1468df8bae1dSRodney W. Grimes 	/* XXX does not really belong here */
1469df8bae1dSRodney W. Grimes 	if (vm_page_max_wired == 0)
14702feb50bfSAttilio Rao 		vm_page_max_wired = cnt.v_free_count / 3;
1471df8bae1dSRodney W. Grimes 
1472dc2efb27SJohn Dyson 	if (vm_pageout_stats_max == 0)
14732feb50bfSAttilio Rao 		vm_pageout_stats_max = cnt.v_free_target;
1474dc2efb27SJohn Dyson 
1475dc2efb27SJohn Dyson 	/*
1476dc2efb27SJohn Dyson 	 * Set interval in seconds for stats scan.
1477dc2efb27SJohn Dyson 	 */
1478dc2efb27SJohn Dyson 	if (vm_pageout_stats_interval == 0)
1479bef608bdSJohn Dyson 		vm_pageout_stats_interval = 5;
1480dc2efb27SJohn Dyson 	if (vm_pageout_full_stats_interval == 0)
1481dc2efb27SJohn Dyson 		vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4;
1482dc2efb27SJohn Dyson 
148324a1cce3SDavid Greenman 	swap_pager_swap_init();
14842b6b0df7SMatthew Dillon 	pass = 0;
1485df8bae1dSRodney W. Grimes 	/*
14860d94caffSDavid Greenman 	 * The pageout daemon is never done, so loop forever.
1487df8bae1dSRodney W. Grimes 	 */
1488df8bae1dSRodney W. Grimes 	while (TRUE) {
1489936524aaSMatthew Dillon 		/*
1490936524aaSMatthew Dillon 		 * If we have enough free memory, wakeup waiters.  Do
1491936524aaSMatthew Dillon 		 * not clear vm_pages_needed until we reach our target,
1492936524aaSMatthew Dillon 		 * otherwise we may be woken up over and over again and
1493936524aaSMatthew Dillon 		 * waste a lot of cpu.
1494936524aaSMatthew Dillon 		 */
1495e9f995d8SAlan Cox 		mtx_lock(&vm_page_queue_free_mtx);
1496936524aaSMatthew Dillon 		if (vm_pages_needed && !vm_page_count_min()) {
1497a1c0a785SAlan Cox 			if (!vm_paging_needed())
1498936524aaSMatthew Dillon 				vm_pages_needed = 0;
14992feb50bfSAttilio Rao 			wakeup(&cnt.v_free_count);
1500936524aaSMatthew Dillon 		}
1501936524aaSMatthew Dillon 		if (vm_pages_needed) {
150290ecac61SMatthew Dillon 			/*
15032b6b0df7SMatthew Dillon 			 * Still not done, take a second pass without waiting
15042b6b0df7SMatthew Dillon 			 * (unlimited dirty cleaning), otherwise sleep a bit
15052b6b0df7SMatthew Dillon 			 * and try again.
150690ecac61SMatthew Dillon 			 */
15072b6b0df7SMatthew Dillon 			++pass;
15082b6b0df7SMatthew Dillon 			if (pass > 1)
1509e9f995d8SAlan Cox 				msleep(&vm_pages_needed,
1510e9f995d8SAlan Cox 				    &vm_page_queue_free_mtx, PVM, "psleep",
1511e9f995d8SAlan Cox 				    hz / 2);
151290ecac61SMatthew Dillon 		} else {
151390ecac61SMatthew Dillon 			/*
15142b6b0df7SMatthew Dillon 			 * Good enough, sleep & handle stats.  Prime the pass
15152b6b0df7SMatthew Dillon 			 * for the next run.
151690ecac61SMatthew Dillon 			 */
15172b6b0df7SMatthew Dillon 			if (pass > 1)
15182b6b0df7SMatthew Dillon 				pass = 1;
15192b6b0df7SMatthew Dillon 			else
15202b6b0df7SMatthew Dillon 				pass = 0;
1521e9f995d8SAlan Cox 			error = msleep(&vm_pages_needed,
1522e9f995d8SAlan Cox 			    &vm_page_queue_free_mtx, PVM, "psleep",
1523e9f995d8SAlan Cox 			    vm_pageout_stats_interval * hz);
1524dc2efb27SJohn Dyson 			if (error && !vm_pages_needed) {
1525e9f995d8SAlan Cox 				mtx_unlock(&vm_page_queue_free_mtx);
15262b6b0df7SMatthew Dillon 				pass = 0;
1527e9f995d8SAlan Cox 				vm_page_lock_queues();
1528dc2efb27SJohn Dyson 				vm_pageout_page_stats();
15295e609009SAlan Cox 				vm_page_unlock_queues();
1530dc2efb27SJohn Dyson 				continue;
1531dc2efb27SJohn Dyson 			}
1532f919ebdeSDavid Greenman 		}
1533b18bfc3dSJohn Dyson 		if (vm_pages_needed)
1534b4b70819SAttilio Rao 			PCPU_INC(cnt.v_pdwakeups);
1535e9f995d8SAlan Cox 		mtx_unlock(&vm_page_queue_free_mtx);
15362b6b0df7SMatthew Dillon 		vm_pageout_scan(pass);
1537df8bae1dSRodney W. Grimes 	}
1538df8bae1dSRodney W. Grimes }
153926f9a767SRodney W. Grimes 
15406b4b77adSAlan Cox /*
1541e9f995d8SAlan Cox  * Unless the free page queue lock is held by the caller, this function
15426b4b77adSAlan Cox  * should be regarded as advisory.  Specifically, the caller should
15436b4b77adSAlan Cox  * not msleep() on &cnt.v_free_count following this function unless
1544e9f995d8SAlan Cox  * the free page queue lock is held until the msleep() is performed.
15456b4b77adSAlan Cox  */
1546e0c5a895SJohn Dyson void
1547e0c5a895SJohn Dyson pagedaemon_wakeup()
1548e0c5a895SJohn Dyson {
1549a1c0a785SAlan Cox 
1550b40ce416SJulian Elischer 	if (!vm_pages_needed && curthread->td_proc != pageproc) {
1551a1c0a785SAlan Cox 		vm_pages_needed = 1;
1552e0c5a895SJohn Dyson 		wakeup(&vm_pages_needed);
1553e0c5a895SJohn Dyson 	}
1554e0c5a895SJohn Dyson }
1555e0c5a895SJohn Dyson 
155638efa82bSJohn Dyson #if !defined(NO_SWAPPING)
15575afce282SDavid Greenman static void
15585afce282SDavid Greenman vm_req_vmdaemon()
15595afce282SDavid Greenman {
15605afce282SDavid Greenman 	static int lastrun = 0;
15615afce282SDavid Greenman 
1562b18bfc3dSJohn Dyson 	if ((ticks > (lastrun + hz)) || (ticks < lastrun)) {
15635afce282SDavid Greenman 		wakeup(&vm_daemon_needed);
15645afce282SDavid Greenman 		lastrun = ticks;
15655afce282SDavid Greenman 	}
15665afce282SDavid Greenman }
15675afce282SDavid Greenman 
15682b14f991SJulian Elischer static void
15694f9fb771SBruce Evans vm_daemon()
15700d94caffSDavid Greenman {
157191d5354aSJohn Baldwin 	struct rlimit rsslim;
1572dcbcd518SBruce Evans 	struct proc *p;
1573dcbcd518SBruce Evans 	struct thread *td;
1574dcbcd518SBruce Evans 	int breakout;
15750d94caffSDavid Greenman 
15763614c6fcSJohn Baldwin 	mtx_lock(&Giant);
15772fe6e4d7SDavid Greenman 	while (TRUE) {
15780cddd8f0SMatthew Dillon 		tsleep(&vm_daemon_needed, PPAUSE, "psleep", 0);
15794c1f8ee9SDavid Greenman 		if (vm_pageout_req_swapout) {
1580ceb0cf87SJohn Dyson 			swapout_procs(vm_pageout_req_swapout);
15814c1f8ee9SDavid Greenman 			vm_pageout_req_swapout = 0;
15824c1f8ee9SDavid Greenman 		}
15832fe6e4d7SDavid Greenman 		/*
15840d94caffSDavid Greenman 		 * scan the processes for exceeding their rlimits or if
15850d94caffSDavid Greenman 		 * process is swapped out -- deactivate pages
15862fe6e4d7SDavid Greenman 		 */
15871005a129SJohn Baldwin 		sx_slock(&allproc_lock);
1588f67af5c9SXin LI 		FOREACH_PROC_IN_SYSTEM(p) {
1589fe2144fdSLuoqi Chen 			vm_pindex_t limit, size;
15902fe6e4d7SDavid Greenman 
15912fe6e4d7SDavid Greenman 			/*
15922fe6e4d7SDavid Greenman 			 * if this is a system process or if we have already
15932fe6e4d7SDavid Greenman 			 * looked at this process, skip it.
15942fe6e4d7SDavid Greenman 			 */
1595897ecacdSJohn Baldwin 			PROC_LOCK(p);
15962fe6e4d7SDavid Greenman 			if (p->p_flag & (P_SYSTEM | P_WEXIT)) {
1597897ecacdSJohn Baldwin 				PROC_UNLOCK(p);
15982fe6e4d7SDavid Greenman 				continue;
15992fe6e4d7SDavid Greenman 			}
16002fe6e4d7SDavid Greenman 			/*
16012fe6e4d7SDavid Greenman 			 * if the process is in a non-running type state,
16022fe6e4d7SDavid Greenman 			 * don't touch it.
16032fe6e4d7SDavid Greenman 			 */
1604982d11f8SJeff Roberson 			PROC_SLOCK(p);
1605e602ba25SJulian Elischer 			breakout = 0;
1606e602ba25SJulian Elischer 			FOREACH_THREAD_IN_PROC(p, td) {
1607982d11f8SJeff Roberson 				thread_lock(td);
160871fad9fdSJulian Elischer 				if (!TD_ON_RUNQ(td) &&
160971fad9fdSJulian Elischer 				    !TD_IS_RUNNING(td) &&
161071fad9fdSJulian Elischer 				    !TD_IS_SLEEPING(td)) {
1611982d11f8SJeff Roberson 					thread_unlock(td);
1612e602ba25SJulian Elischer 					breakout = 1;
1613e602ba25SJulian Elischer 					break;
1614e602ba25SJulian Elischer 				}
1615982d11f8SJeff Roberson 				thread_unlock(td);
1616e602ba25SJulian Elischer 			}
1617982d11f8SJeff Roberson 			PROC_SUNLOCK(p);
1618897ecacdSJohn Baldwin 			if (breakout) {
1619897ecacdSJohn Baldwin 				PROC_UNLOCK(p);
16202fe6e4d7SDavid Greenman 				continue;
16212fe6e4d7SDavid Greenman 			}
16222fe6e4d7SDavid Greenman 			/*
16232fe6e4d7SDavid Greenman 			 * get a limit
16242fe6e4d7SDavid Greenman 			 */
1625dcbcd518SBruce Evans 			lim_rlimit(p, RLIMIT_RSS, &rsslim);
1626fe2144fdSLuoqi Chen 			limit = OFF_TO_IDX(
162791d5354aSJohn Baldwin 			    qmin(rsslim.rlim_cur, rsslim.rlim_max));
16282fe6e4d7SDavid Greenman 
16292fe6e4d7SDavid Greenman 			/*
16300d94caffSDavid Greenman 			 * let processes that are swapped out really be
16310d94caffSDavid Greenman 			 * swapped out set the limit to nothing (will force a
16320d94caffSDavid Greenman 			 * swap-out.)
16332fe6e4d7SDavid Greenman 			 */
16348606d880SJohn Baldwin 			if ((p->p_sflag & PS_INMEM) == 0)
16350d94caffSDavid Greenman 				limit = 0;	/* XXX */
1636897ecacdSJohn Baldwin 			PROC_UNLOCK(p);
16372fe6e4d7SDavid Greenman 
1638fe2144fdSLuoqi Chen 			size = vmspace_resident_count(p->p_vmspace);
16392fe6e4d7SDavid Greenman 			if (limit >= 0 && size >= limit) {
1640fe2144fdSLuoqi Chen 				vm_pageout_map_deactivate_pages(
1641fe2144fdSLuoqi Chen 				    &p->p_vmspace->vm_map, limit);
16422fe6e4d7SDavid Greenman 			}
16432fe6e4d7SDavid Greenman 		}
16441005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
164524a1cce3SDavid Greenman 	}
16462fe6e4d7SDavid Greenman }
1647a1287949SEivind Eklund #endif			/* !defined(NO_SWAPPING) */
1648