xref: /freebsd/sys/vm/vm_pageout.c (revision 311e34e2601d3c29f51d47f733da61f8bb86910c)
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>
89099e7e95SEdward Tomasz Napierala #include <sys/racct.h>
9026f9a767SRodney W. Grimes #include <sys/resourcevar.h>
91b43179fbSJeff Roberson #include <sys/sched.h>
92d2fc5315SPoul-Henning Kamp #include <sys/signalvar.h>
93f6b04d2bSDavid Greenman #include <sys/vnode.h>
94efeaf95aSDavid Greenman #include <sys/vmmeter.h>
951005a129SJohn Baldwin #include <sys/sx.h>
9638efa82bSJohn Dyson #include <sys/sysctl.h>
97df8bae1dSRodney W. Grimes 
98df8bae1dSRodney W. Grimes #include <vm/vm.h>
99efeaf95aSDavid Greenman #include <vm/vm_param.h>
100efeaf95aSDavid Greenman #include <vm/vm_object.h>
101df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
102efeaf95aSDavid Greenman #include <vm/vm_map.h>
103df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
10424a1cce3SDavid Greenman #include <vm/vm_pager.h>
10505f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h>
106efeaf95aSDavid Greenman #include <vm/vm_extern.h>
107670d17b5SJeff Roberson #include <vm/uma.h>
108df8bae1dSRodney W. Grimes 
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 };
125237fdd78SRobert Watson SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start,
126237fdd78SRobert Watson     &page_kp);
1272b14f991SJulian Elischer 
12838efa82bSJohn Dyson #if !defined(NO_SWAPPING)
1292b14f991SJulian Elischer /* the kernel process "vm_daemon"*/
13011caded3SAlfred Perlstein static void vm_daemon(void);
131f708ef1bSPoul-Henning Kamp static struct	proc *vmproc;
1322b14f991SJulian Elischer 
1332b14f991SJulian Elischer static struct kproc_desc vm_kp = {
1342b14f991SJulian Elischer 	"vmdaemon",
1352b14f991SJulian Elischer 	vm_daemon,
1362b14f991SJulian Elischer 	&vmproc
1372b14f991SJulian Elischer };
138237fdd78SRobert Watson SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp);
13938efa82bSJohn Dyson #endif
1402b14f991SJulian Elischer 
1412b14f991SJulian Elischer 
1428b245767SAlan Cox int vm_pages_needed;		/* Event on which pageout daemon sleeps */
1438b245767SAlan Cox int vm_pageout_deficit;		/* Estimated number of pages deficit */
1448b245767SAlan Cox int vm_pageout_pages_needed;	/* flag saying that the pageout daemon needs pages */
14526f9a767SRodney W. Grimes 
14638efa82bSJohn Dyson #if !defined(NO_SWAPPING)
147f708ef1bSPoul-Henning Kamp static int vm_pageout_req_swapout;	/* XXX */
148f708ef1bSPoul-Henning Kamp static int vm_daemon_needed;
14997824da3SAlan Cox static struct mtx vm_daemon_mtx;
15097824da3SAlan Cox /* Allow for use by vm_pageout before vm_daemon is initialized. */
15197824da3SAlan Cox MTX_SYSINIT(vm_daemon, &vm_daemon_mtx, "vm daemon", MTX_DEF);
15238efa82bSJohn Dyson #endif
1532b6b0df7SMatthew Dillon static int vm_max_launder = 32;
154303b270bSEivind Eklund static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0;
155303b270bSEivind Eklund static int vm_pageout_full_stats_interval = 0;
15626354d4cSAlan Cox static int vm_pageout_algorithm=0;
157303b270bSEivind Eklund static int defer_swap_pageouts=0;
158303b270bSEivind Eklund static int disable_swap_pageouts=0;
15970111b90SJohn Dyson 
16038efa82bSJohn Dyson #if defined(NO_SWAPPING)
161303b270bSEivind Eklund static int vm_swap_enabled=0;
162303b270bSEivind Eklund static int vm_swap_idle_enabled=0;
16338efa82bSJohn Dyson #else
164303b270bSEivind Eklund static int vm_swap_enabled=1;
165303b270bSEivind Eklund static int vm_swap_idle_enabled=0;
16638efa82bSJohn Dyson #endif
16738efa82bSJohn Dyson 
16838efa82bSJohn Dyson SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm,
1692b6b0df7SMatthew Dillon 	CTLFLAG_RW, &vm_pageout_algorithm, 0, "LRU page mgmt");
1702b6b0df7SMatthew Dillon 
1712b6b0df7SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, max_launder,
1722b6b0df7SMatthew Dillon 	CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout");
17338efa82bSJohn Dyson 
174dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max,
175b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length");
176dc2efb27SJohn Dyson 
177dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval,
178b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan");
179dc2efb27SJohn Dyson 
180dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval,
181b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan");
182dc2efb27SJohn Dyson 
18338efa82bSJohn Dyson #if defined(NO_SWAPPING)
184ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
1856bd9cb1cSTom Rhodes 	CTLFLAG_RD, &vm_swap_enabled, 0, "Enable entire process swapout");
186ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
1876bd9cb1cSTom Rhodes 	CTLFLAG_RD, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
18838efa82bSJohn Dyson #else
189ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
190b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout");
191ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
192b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
19338efa82bSJohn Dyson #endif
19426f9a767SRodney W. Grimes 
195ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts,
196b0359e2cSPeter Wemm 	CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem");
19712ac6a1dSJohn Dyson 
198ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts,
199b0359e2cSPeter Wemm 	CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages");
20012ac6a1dSJohn Dyson 
20123b59018SMatthew Dillon static int pageout_lock_miss;
20223b59018SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss,
20323b59018SMatthew Dillon 	CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout");
20423b59018SMatthew Dillon 
205ffc82b0aSJohn Dyson #define VM_PAGEOUT_PAGE_COUNT 16
206bbc0ec52SDavid Greenman int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT;
207df8bae1dSRodney W. Grimes 
208c3cb3e12SDavid Greenman int vm_page_max_wired;		/* XXX max # of wired pages system-wide */
2095dfc2870SAlan Cox SYSCTL_INT(_vm, OID_AUTO, max_wired,
2105dfc2870SAlan Cox 	CTLFLAG_RW, &vm_page_max_wired, 0, "System-wide limit to wired page count");
211df8bae1dSRodney W. Grimes 
21285eeca35SAlan Cox static boolean_t vm_pageout_fallback_object_lock(vm_page_t, vm_page_t *);
21385eeca35SAlan Cox static boolean_t vm_pageout_launder(int, int, vm_paddr_t, vm_paddr_t);
21438efa82bSJohn Dyson #if !defined(NO_SWAPPING)
215ecf6279fSAlan Cox static void vm_pageout_map_deactivate_pages(vm_map_t, long);
216ecf6279fSAlan Cox static void vm_pageout_object_deactivate_pages(pmap_t, vm_object_t, long);
21797824da3SAlan Cox static void vm_req_vmdaemon(int req);
21838efa82bSJohn Dyson #endif
21985eeca35SAlan Cox static boolean_t vm_pageout_page_lock(vm_page_t, vm_page_t *);
220dc2efb27SJohn Dyson static void vm_pageout_page_stats(void);
221cd41fc12SDavid Greenman 
222a8229fa3SAlan Cox /*
223a8229fa3SAlan Cox  * Initialize a dummy page for marking the caller's place in the specified
224a8229fa3SAlan Cox  * paging queue.  In principle, this function only needs to set the flag
225a8229fa3SAlan Cox  * PG_MARKER.  Nonetheless, it sets the flag VPO_BUSY and initializes the hold
226a8229fa3SAlan Cox  * count to one as safety precautions.
227a8229fa3SAlan Cox  */
2288c616246SKonstantin Belousov static void
2298c616246SKonstantin Belousov vm_pageout_init_marker(vm_page_t marker, u_short queue)
2308c616246SKonstantin Belousov {
2318c616246SKonstantin Belousov 
2328c616246SKonstantin Belousov 	bzero(marker, sizeof(*marker));
233a8229fa3SAlan Cox 	marker->flags = PG_MARKER;
2348c616246SKonstantin Belousov 	marker->oflags = VPO_BUSY;
2358c616246SKonstantin Belousov 	marker->queue = queue;
236a8229fa3SAlan Cox 	marker->hold_count = 1;
2378c616246SKonstantin Belousov }
2388c616246SKonstantin Belousov 
23926f9a767SRodney W. Grimes /*
2408dbca793STor Egge  * vm_pageout_fallback_object_lock:
2418dbca793STor Egge  *
2428dbca793STor Egge  * Lock vm object currently associated with `m'. VM_OBJECT_TRYLOCK is
2438dbca793STor Egge  * known to have failed and page queue must be either PQ_ACTIVE or
2448dbca793STor Egge  * PQ_INACTIVE.  To avoid lock order violation, unlock the page queues
2458dbca793STor Egge  * while locking the vm object.  Use marker page to detect page queue
2468dbca793STor Egge  * changes and maintain notion of next page on page queue.  Return
2478dbca793STor Egge  * TRUE if no changes were detected, FALSE otherwise.  vm object is
2488dbca793STor Egge  * locked on return.
2498dbca793STor Egge  *
2508dbca793STor Egge  * This function depends on both the lock portion of struct vm_object
2518dbca793STor Egge  * and normal struct vm_page being type stable.
2528dbca793STor Egge  */
25385eeca35SAlan Cox static boolean_t
2548dbca793STor Egge vm_pageout_fallback_object_lock(vm_page_t m, vm_page_t *next)
2558dbca793STor Egge {
2568dbca793STor Egge 	struct vm_page marker;
2578dbca793STor Egge 	boolean_t unchanged;
2588dbca793STor Egge 	u_short queue;
2598dbca793STor Egge 	vm_object_t object;
2608dbca793STor Egge 
2618dbca793STor Egge 	queue = m->queue;
2628c616246SKonstantin Belousov 	vm_pageout_init_marker(&marker, queue);
2638dbca793STor Egge 	object = m->object;
2648dbca793STor Egge 
2658dbca793STor Egge 	TAILQ_INSERT_AFTER(&vm_page_queues[queue].pl,
2668dbca793STor Egge 			   m, &marker, pageq);
2678dbca793STor Egge 	vm_page_unlock_queues();
2682965a453SKip Macy 	vm_page_unlock(m);
2698dbca793STor Egge 	VM_OBJECT_LOCK(object);
2702965a453SKip Macy 	vm_page_lock(m);
2718dbca793STor Egge 	vm_page_lock_queues();
2728dbca793STor Egge 
2738dbca793STor Egge 	/* Page queue might have changed. */
2748dbca793STor Egge 	*next = TAILQ_NEXT(&marker, pageq);
2758dbca793STor Egge 	unchanged = (m->queue == queue &&
2768dbca793STor Egge 		     m->object == object &&
2778dbca793STor Egge 		     &marker == TAILQ_NEXT(m, pageq));
2788dbca793STor Egge 	TAILQ_REMOVE(&vm_page_queues[queue].pl,
2798dbca793STor Egge 		     &marker, pageq);
2808dbca793STor Egge 	return (unchanged);
2818dbca793STor Egge }
2828dbca793STor Egge 
2838dbca793STor Egge /*
2848c616246SKonstantin Belousov  * Lock the page while holding the page queue lock.  Use marker page
2858c616246SKonstantin Belousov  * to detect page queue changes and maintain notion of next page on
2868c616246SKonstantin Belousov  * page queue.  Return TRUE if no changes were detected, FALSE
2878c616246SKonstantin Belousov  * otherwise.  The page is locked on return. The page queue lock might
2888c616246SKonstantin Belousov  * be dropped and reacquired.
2898c616246SKonstantin Belousov  *
2908c616246SKonstantin Belousov  * This function depends on normal struct vm_page being type stable.
2918c616246SKonstantin Belousov  */
29285eeca35SAlan Cox static boolean_t
2938c616246SKonstantin Belousov vm_pageout_page_lock(vm_page_t m, vm_page_t *next)
2948c616246SKonstantin Belousov {
2958c616246SKonstantin Belousov 	struct vm_page marker;
2968c616246SKonstantin Belousov 	boolean_t unchanged;
2978c616246SKonstantin Belousov 	u_short queue;
2988c616246SKonstantin Belousov 
2998c616246SKonstantin Belousov 	vm_page_lock_assert(m, MA_NOTOWNED);
3008c616246SKonstantin Belousov 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3018c616246SKonstantin Belousov 
3028c616246SKonstantin Belousov 	if (vm_page_trylock(m))
3038c616246SKonstantin Belousov 		return (TRUE);
3048c616246SKonstantin Belousov 
3058c616246SKonstantin Belousov 	queue = m->queue;
3068c616246SKonstantin Belousov 	vm_pageout_init_marker(&marker, queue);
3078c616246SKonstantin Belousov 
3088c616246SKonstantin Belousov 	TAILQ_INSERT_AFTER(&vm_page_queues[queue].pl, m, &marker, pageq);
3098c616246SKonstantin Belousov 	vm_page_unlock_queues();
3108c616246SKonstantin Belousov 	vm_page_lock(m);
3118c616246SKonstantin Belousov 	vm_page_lock_queues();
3128c616246SKonstantin Belousov 
3138c616246SKonstantin Belousov 	/* Page queue might have changed. */
3148c616246SKonstantin Belousov 	*next = TAILQ_NEXT(&marker, pageq);
3158c616246SKonstantin Belousov 	unchanged = (m->queue == queue && &marker == TAILQ_NEXT(m, pageq));
3168c616246SKonstantin Belousov 	TAILQ_REMOVE(&vm_page_queues[queue].pl, &marker, pageq);
3178c616246SKonstantin Belousov 	return (unchanged);
3188c616246SKonstantin Belousov }
3198c616246SKonstantin Belousov 
3208c616246SKonstantin Belousov /*
32126f9a767SRodney W. Grimes  * vm_pageout_clean:
32224a1cce3SDavid Greenman  *
3230d94caffSDavid Greenman  * Clean the page and remove it from the laundry.
32426f9a767SRodney W. Grimes  *
3250d94caffSDavid Greenman  * We set the busy bit to cause potential page faults on this page to
3261c7c3c6aSMatthew Dillon  * block.  Note the careful timing, however, the busy bit isn't set till
3271c7c3c6aSMatthew Dillon  * late and we cannot do anything that will mess with the page.
32826f9a767SRodney W. Grimes  */
3293af76890SPoul-Henning Kamp static int
3302965a453SKip Macy vm_pageout_clean(vm_page_t m)
33124a1cce3SDavid Greenman {
33254d92145SMatthew Dillon 	vm_object_t object;
33391b4f427SAlan Cox 	vm_page_t mc[2*vm_pageout_page_count], pb, ps;
3343562af12SAlan Cox 	int pageout_count;
33590ecac61SMatthew Dillon 	int ib, is, page_base;
336a316d390SJohn Dyson 	vm_pindex_t pindex = m->pindex;
33726f9a767SRodney W. Grimes 
33895976f3fSAlan Cox 	vm_page_lock_assert(m, MA_OWNED);
33917f6a17bSAlan Cox 	object = m->object;
34017f6a17bSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
3410cddd8f0SMatthew Dillon 
34226f9a767SRodney W. Grimes 	/*
3431c7c3c6aSMatthew Dillon 	 * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP
3441c7c3c6aSMatthew Dillon 	 * with the new swapper, but we could have serious problems paging
3451c7c3c6aSMatthew Dillon 	 * out other object types if there is insufficient memory.
3461c7c3c6aSMatthew Dillon 	 *
3471c7c3c6aSMatthew Dillon 	 * Unfortunately, checking free memory here is far too late, so the
3481c7c3c6aSMatthew Dillon 	 * check has been moved up a procedural level.
3491c7c3c6aSMatthew Dillon 	 */
3501c7c3c6aSMatthew Dillon 
35124a1cce3SDavid Greenman 	/*
3529e897b1bSAlan Cox 	 * Can't clean the page if it's busy or held.
35324a1cce3SDavid Greenman 	 */
35495976f3fSAlan Cox 	KASSERT(m->busy == 0 && (m->oflags & VPO_BUSY) == 0,
35595976f3fSAlan Cox 	    ("vm_pageout_clean: page %p is busy", m));
35695976f3fSAlan Cox 	KASSERT(m->hold_count == 0, ("vm_pageout_clean: page %p is held", m));
35717f6a17bSAlan Cox 	vm_page_unlock(m);
3580d94caffSDavid Greenman 
35991b4f427SAlan Cox 	mc[vm_pageout_page_count] = pb = ps = m;
36026f9a767SRodney W. Grimes 	pageout_count = 1;
361f35329acSJohn Dyson 	page_base = vm_pageout_page_count;
36290ecac61SMatthew Dillon 	ib = 1;
36390ecac61SMatthew Dillon 	is = 1;
36490ecac61SMatthew Dillon 
36524a1cce3SDavid Greenman 	/*
36624a1cce3SDavid Greenman 	 * Scan object for clusterable pages.
36724a1cce3SDavid Greenman 	 *
36824a1cce3SDavid Greenman 	 * We can cluster ONLY if: ->> the page is NOT
36924a1cce3SDavid Greenman 	 * clean, wired, busy, held, or mapped into a
37024a1cce3SDavid Greenman 	 * buffer, and one of the following:
37124a1cce3SDavid Greenman 	 * 1) The page is inactive, or a seldom used
37224a1cce3SDavid Greenman 	 *    active page.
37324a1cce3SDavid Greenman 	 * -or-
37424a1cce3SDavid Greenman 	 * 2) we force the issue.
37590ecac61SMatthew Dillon 	 *
37690ecac61SMatthew Dillon 	 * During heavy mmap/modification loads the pageout
37790ecac61SMatthew Dillon 	 * daemon can really fragment the underlying file
37890ecac61SMatthew Dillon 	 * due to flushing pages out of order and not trying
37990ecac61SMatthew Dillon 	 * align the clusters (which leave sporatic out-of-order
38090ecac61SMatthew Dillon 	 * holes).  To solve this problem we do the reverse scan
38190ecac61SMatthew Dillon 	 * first and attempt to align our cluster, then do a
38290ecac61SMatthew Dillon 	 * forward scan if room remains.
38324a1cce3SDavid Greenman 	 */
38490ecac61SMatthew Dillon more:
38590ecac61SMatthew Dillon 	while (ib && pageout_count < vm_pageout_page_count) {
38624a1cce3SDavid Greenman 		vm_page_t p;
387f6b04d2bSDavid Greenman 
38890ecac61SMatthew Dillon 		if (ib > pindex) {
38990ecac61SMatthew Dillon 			ib = 0;
39090ecac61SMatthew Dillon 			break;
391f6b04d2bSDavid Greenman 		}
39290ecac61SMatthew Dillon 
39391b4f427SAlan Cox 		if ((p = vm_page_prev(pb)) == NULL ||
39491b4f427SAlan Cox 		    (p->oflags & VPO_BUSY) != 0 || p->busy != 0) {
39590ecac61SMatthew Dillon 			ib = 0;
39690ecac61SMatthew Dillon 			break;
397f6b04d2bSDavid Greenman 		}
3982965a453SKip Macy 		vm_page_lock(p);
39924a1cce3SDavid Greenman 		vm_page_test_dirty(p);
40026f4eea5SAlan Cox 		if (p->dirty == 0 ||
40190ecac61SMatthew Dillon 		    p->queue != PQ_INACTIVE ||
40257601bcbSMatthew Dillon 		    p->hold_count != 0) {	/* may be undergoing I/O */
4032965a453SKip Macy 			vm_page_unlock(p);
40490ecac61SMatthew Dillon 			ib = 0;
40524a1cce3SDavid Greenman 			break;
406f6b04d2bSDavid Greenman 		}
4072965a453SKip Macy 		vm_page_unlock(p);
40891b4f427SAlan Cox 		mc[--page_base] = pb = p;
40990ecac61SMatthew Dillon 		++pageout_count;
41090ecac61SMatthew Dillon 		++ib;
41124a1cce3SDavid Greenman 		/*
41290ecac61SMatthew Dillon 		 * alignment boundry, stop here and switch directions.  Do
41390ecac61SMatthew Dillon 		 * not clear ib.
41424a1cce3SDavid Greenman 		 */
41590ecac61SMatthew Dillon 		if ((pindex - (ib - 1)) % vm_pageout_page_count == 0)
41690ecac61SMatthew Dillon 			break;
41724a1cce3SDavid Greenman 	}
41890ecac61SMatthew Dillon 
41990ecac61SMatthew Dillon 	while (pageout_count < vm_pageout_page_count &&
42090ecac61SMatthew Dillon 	    pindex + is < object->size) {
42190ecac61SMatthew Dillon 		vm_page_t p;
42290ecac61SMatthew Dillon 
42391b4f427SAlan Cox 		if ((p = vm_page_next(ps)) == NULL ||
42491b4f427SAlan Cox 		    (p->oflags & VPO_BUSY) != 0 || p->busy != 0)
42590ecac61SMatthew Dillon 			break;
4262965a453SKip Macy 		vm_page_lock(p);
42724a1cce3SDavid Greenman 		vm_page_test_dirty(p);
42826f4eea5SAlan Cox 		if (p->dirty == 0 ||
42990ecac61SMatthew Dillon 		    p->queue != PQ_INACTIVE ||
43057601bcbSMatthew Dillon 		    p->hold_count != 0) {	/* may be undergoing I/O */
4312965a453SKip Macy 			vm_page_unlock(p);
43224a1cce3SDavid Greenman 			break;
43324a1cce3SDavid Greenman 		}
4342965a453SKip Macy 		vm_page_unlock(p);
43591b4f427SAlan Cox 		mc[page_base + pageout_count] = ps = p;
43690ecac61SMatthew Dillon 		++pageout_count;
43790ecac61SMatthew Dillon 		++is;
43824a1cce3SDavid Greenman 	}
43990ecac61SMatthew Dillon 
44090ecac61SMatthew Dillon 	/*
44190ecac61SMatthew Dillon 	 * If we exhausted our forward scan, continue with the reverse scan
44290ecac61SMatthew Dillon 	 * when possible, even past a page boundry.  This catches boundry
44390ecac61SMatthew Dillon 	 * conditions.
44490ecac61SMatthew Dillon 	 */
44590ecac61SMatthew Dillon 	if (ib && pageout_count < vm_pageout_page_count)
44690ecac61SMatthew Dillon 		goto more;
447f6b04d2bSDavid Greenman 
44867bf6868SJohn Dyson 	/*
44967bf6868SJohn Dyson 	 * we allow reads during pageouts...
45067bf6868SJohn Dyson 	 */
451126d6082SKonstantin Belousov 	return (vm_pageout_flush(&mc[page_base], pageout_count, 0, 0, NULL,
452126d6082SKonstantin Belousov 	    NULL));
453aef922f5SJohn Dyson }
454aef922f5SJohn Dyson 
4551c7c3c6aSMatthew Dillon /*
4561c7c3c6aSMatthew Dillon  * vm_pageout_flush() - launder the given pages
4571c7c3c6aSMatthew Dillon  *
4581c7c3c6aSMatthew Dillon  *	The given pages are laundered.  Note that we setup for the start of
4591c7c3c6aSMatthew Dillon  *	I/O ( i.e. busy the page ), mark it read-only, and bump the object
4601c7c3c6aSMatthew Dillon  *	reference count all in here rather then in the parent.  If we want
4611c7c3c6aSMatthew Dillon  *	the parent to do more sophisticated things we may have to change
4621c7c3c6aSMatthew Dillon  *	the ordering.
4631e8a675cSKonstantin Belousov  *
4641e8a675cSKonstantin Belousov  *	Returned runlen is the count of pages between mreq and first
4651e8a675cSKonstantin Belousov  *	page after mreq with status VM_PAGER_AGAIN.
466126d6082SKonstantin Belousov  *	*eio is set to TRUE if pager returned VM_PAGER_ERROR or VM_PAGER_FAIL
467126d6082SKonstantin Belousov  *	for any page in runlen set.
4681c7c3c6aSMatthew Dillon  */
469aef922f5SJohn Dyson int
470126d6082SKonstantin Belousov vm_pageout_flush(vm_page_t *mc, int count, int flags, int mreq, int *prunlen,
471126d6082SKonstantin Belousov     boolean_t *eio)
472aef922f5SJohn Dyson {
4732e3b314dSAlan Cox 	vm_object_t object = mc[0]->object;
474aef922f5SJohn Dyson 	int pageout_status[count];
47595461b45SJohn Dyson 	int numpagedout = 0;
4761e8a675cSKonstantin Belousov 	int i, runlen;
477aef922f5SJohn Dyson 
4782e3b314dSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
4797bec141bSKip Macy 	mtx_assert(&vm_page_queue_mtx, MA_NOTOWNED);
4807bec141bSKip Macy 
4811c7c3c6aSMatthew Dillon 	/*
4821c7c3c6aSMatthew Dillon 	 * Initiate I/O.  Bump the vm_page_t->busy counter and
4831c7c3c6aSMatthew Dillon 	 * mark the pages read-only.
4841c7c3c6aSMatthew Dillon 	 *
4851c7c3c6aSMatthew Dillon 	 * We do not have to fixup the clean/dirty bits here... we can
4861c7c3c6aSMatthew Dillon 	 * allow the pager to do it after the I/O completes.
48702fa91d3SMatthew Dillon 	 *
48802fa91d3SMatthew Dillon 	 * NOTE! mc[i]->dirty may be partial or fragmented due to an
48902fa91d3SMatthew Dillon 	 * edge case with file fragments.
4901c7c3c6aSMatthew Dillon 	 */
4918f9110f6SJohn Dyson 	for (i = 0; i < count; i++) {
4927a935082SAlan Cox 		KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL,
4937a935082SAlan Cox 		    ("vm_pageout_flush: partially invalid page %p index %d/%d",
4947a935082SAlan Cox 			mc[i], i, count));
495e69763a3SDoug Rabson 		vm_page_io_start(mc[i]);
49678985e42SAlan Cox 		pmap_remove_write(mc[i]);
4972965a453SKip Macy 	}
498d474eaaaSDoug Rabson 	vm_object_pip_add(object, count);
499aef922f5SJohn Dyson 
500d076fbeaSAlan Cox 	vm_pager_put_pages(object, mc, count, flags, pageout_status);
50126f9a767SRodney W. Grimes 
5021e8a675cSKonstantin Belousov 	runlen = count - mreq;
503126d6082SKonstantin Belousov 	if (eio != NULL)
504126d6082SKonstantin Belousov 		*eio = FALSE;
505aef922f5SJohn Dyson 	for (i = 0; i < count; i++) {
506aef922f5SJohn Dyson 		vm_page_t mt = mc[i];
50724a1cce3SDavid Greenman 
5084cd45723SAlan Cox 		KASSERT(pageout_status[i] == VM_PAGER_PEND ||
5096031c68dSAlan Cox 		    !pmap_page_is_write_mapped(mt),
5109ea8d1a6SAlan Cox 		    ("vm_pageout_flush: page %p is not write protected", mt));
51126f9a767SRodney W. Grimes 		switch (pageout_status[i]) {
51226f9a767SRodney W. Grimes 		case VM_PAGER_OK:
51326f9a767SRodney W. Grimes 		case VM_PAGER_PEND:
51495461b45SJohn Dyson 			numpagedout++;
51526f9a767SRodney W. Grimes 			break;
51626f9a767SRodney W. Grimes 		case VM_PAGER_BAD:
51726f9a767SRodney W. Grimes 			/*
5180d94caffSDavid Greenman 			 * Page outside of range of object. Right now we
5190d94caffSDavid Greenman 			 * essentially lose the changes by pretending it
5200d94caffSDavid Greenman 			 * worked.
52126f9a767SRodney W. Grimes 			 */
52290ecac61SMatthew Dillon 			vm_page_undirty(mt);
52326f9a767SRodney W. Grimes 			break;
52426f9a767SRodney W. Grimes 		case VM_PAGER_ERROR:
52526f9a767SRodney W. Grimes 		case VM_PAGER_FAIL:
52626f9a767SRodney W. Grimes 			/*
5270d94caffSDavid Greenman 			 * If page couldn't be paged out, then reactivate the
5280d94caffSDavid Greenman 			 * page so it doesn't clog the inactive list.  (We
5290d94caffSDavid Greenman 			 * will try paging out it again later).
53026f9a767SRodney W. Grimes 			 */
5313c4a2440SAlan Cox 			vm_page_lock(mt);
53224a1cce3SDavid Greenman 			vm_page_activate(mt);
5333c4a2440SAlan Cox 			vm_page_unlock(mt);
534126d6082SKonstantin Belousov 			if (eio != NULL && i >= mreq && i - mreq < runlen)
535126d6082SKonstantin Belousov 				*eio = TRUE;
53626f9a767SRodney W. Grimes 			break;
53726f9a767SRodney W. Grimes 		case VM_PAGER_AGAIN:
5381e8a675cSKonstantin Belousov 			if (i >= mreq && i - mreq < runlen)
5391e8a675cSKonstantin Belousov 				runlen = i - mreq;
54026f9a767SRodney W. Grimes 			break;
54126f9a767SRodney W. Grimes 		}
54226f9a767SRodney W. Grimes 
54326f9a767SRodney W. Grimes 		/*
5440d94caffSDavid Greenman 		 * If the operation is still going, leave the page busy to
5450d94caffSDavid Greenman 		 * block all other accesses. Also, leave the paging in
5460d94caffSDavid Greenman 		 * progress indicator set so that we don't attempt an object
5470d94caffSDavid Greenman 		 * collapse.
54826f9a767SRodney W. Grimes 		 */
54926f9a767SRodney W. Grimes 		if (pageout_status[i] != VM_PAGER_PEND) {
550f919ebdeSDavid Greenman 			vm_object_pip_wakeup(object);
551e69763a3SDoug Rabson 			vm_page_io_finish(mt);
5523c4a2440SAlan Cox 			if (vm_page_count_severe()) {
5533c4a2440SAlan Cox 				vm_page_lock(mt);
5549ea8d1a6SAlan Cox 				vm_page_try_to_cache(mt);
5552965a453SKip Macy 				vm_page_unlock(mt);
55626f9a767SRodney W. Grimes 			}
5573c4a2440SAlan Cox 		}
5583c4a2440SAlan Cox 	}
5591e8a675cSKonstantin Belousov 	if (prunlen != NULL)
5601e8a675cSKonstantin Belousov 		*prunlen = runlen;
5613c4a2440SAlan Cox 	return (numpagedout);
56226f9a767SRodney W. Grimes }
56326f9a767SRodney W. Grimes 
56485eeca35SAlan Cox static boolean_t
56585eeca35SAlan Cox vm_pageout_launder(int queue, int tries, vm_paddr_t low, vm_paddr_t high)
56685eeca35SAlan Cox {
56785eeca35SAlan Cox 	struct mount *mp;
56885eeca35SAlan Cox 	struct vnode *vp;
56985eeca35SAlan Cox 	vm_object_t object;
57085eeca35SAlan Cox 	vm_paddr_t pa;
57185eeca35SAlan Cox 	vm_page_t m, m_tmp, next;
57285eeca35SAlan Cox 	int vfslocked;
57385eeca35SAlan Cox 
57485eeca35SAlan Cox 	vm_page_lock_queues();
57585eeca35SAlan Cox 	TAILQ_FOREACH_SAFE(m, &vm_page_queues[queue].pl, pageq, next) {
57685eeca35SAlan Cox 		KASSERT(m->queue == queue,
57785eeca35SAlan Cox 		    ("vm_pageout_launder: page %p's queue is not %d", m,
57885eeca35SAlan Cox 		    queue));
57985eeca35SAlan Cox 		if ((m->flags & PG_MARKER) != 0)
58085eeca35SAlan Cox 			continue;
58185eeca35SAlan Cox 		pa = VM_PAGE_TO_PHYS(m);
58285eeca35SAlan Cox 		if (pa < low || pa + PAGE_SIZE > high)
58385eeca35SAlan Cox 			continue;
58485eeca35SAlan Cox 		if (!vm_pageout_page_lock(m, &next) || m->hold_count != 0) {
58585eeca35SAlan Cox 			vm_page_unlock(m);
58685eeca35SAlan Cox 			continue;
58785eeca35SAlan Cox 		}
58885eeca35SAlan Cox 		object = m->object;
58985eeca35SAlan Cox 		if (!VM_OBJECT_TRYLOCK(object) &&
59085eeca35SAlan Cox 		    (!vm_pageout_fallback_object_lock(m, &next) ||
59185eeca35SAlan Cox 		    m->hold_count != 0)) {
59285eeca35SAlan Cox 			vm_page_unlock(m);
59385eeca35SAlan Cox 			VM_OBJECT_UNLOCK(object);
59485eeca35SAlan Cox 			continue;
59585eeca35SAlan Cox 		}
59685eeca35SAlan Cox 		if ((m->oflags & VPO_BUSY) != 0 || m->busy != 0) {
59785eeca35SAlan Cox 			if (tries == 0) {
59885eeca35SAlan Cox 				vm_page_unlock(m);
59985eeca35SAlan Cox 				VM_OBJECT_UNLOCK(object);
60085eeca35SAlan Cox 				continue;
60185eeca35SAlan Cox 			}
60285eeca35SAlan Cox 			vm_page_sleep(m, "vpctw0");
60385eeca35SAlan Cox 			VM_OBJECT_UNLOCK(object);
60485eeca35SAlan Cox 			return (FALSE);
60585eeca35SAlan Cox 		}
60685eeca35SAlan Cox 		vm_page_test_dirty(m);
60785eeca35SAlan Cox 		if (m->dirty == 0)
60885eeca35SAlan Cox 			pmap_remove_all(m);
60985eeca35SAlan Cox 		if (m->dirty != 0) {
61085eeca35SAlan Cox 			vm_page_unlock(m);
61185eeca35SAlan Cox 			if (tries == 0 || (object->flags & OBJ_DEAD) != 0) {
61285eeca35SAlan Cox 				VM_OBJECT_UNLOCK(object);
61385eeca35SAlan Cox 				continue;
61485eeca35SAlan Cox 			}
61585eeca35SAlan Cox 			if (object->type == OBJT_VNODE) {
61685eeca35SAlan Cox 				vm_page_unlock_queues();
61785eeca35SAlan Cox 				vp = object->handle;
61885eeca35SAlan Cox 				vm_object_reference_locked(object);
61985eeca35SAlan Cox 				VM_OBJECT_UNLOCK(object);
62085eeca35SAlan Cox 				(void)vn_start_write(vp, &mp, V_WAIT);
62185eeca35SAlan Cox 				vfslocked = VFS_LOCK_GIANT(vp->v_mount);
62285eeca35SAlan Cox 				vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
62385eeca35SAlan Cox 				VM_OBJECT_LOCK(object);
62485eeca35SAlan Cox 				vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
62585eeca35SAlan Cox 				VM_OBJECT_UNLOCK(object);
62685eeca35SAlan Cox 				VOP_UNLOCK(vp, 0);
62785eeca35SAlan Cox 				VFS_UNLOCK_GIANT(vfslocked);
62885eeca35SAlan Cox 				vm_object_deallocate(object);
62985eeca35SAlan Cox 				vn_finished_write(mp);
63085eeca35SAlan Cox 				return (TRUE);
63185eeca35SAlan Cox 			} else if (object->type == OBJT_SWAP ||
63285eeca35SAlan Cox 			    object->type == OBJT_DEFAULT) {
63385eeca35SAlan Cox 				vm_page_unlock_queues();
63485eeca35SAlan Cox 				m_tmp = m;
63585eeca35SAlan Cox 				vm_pageout_flush(&m_tmp, 1, VM_PAGER_PUT_SYNC,
63685eeca35SAlan Cox 				    0, NULL, NULL);
63785eeca35SAlan Cox 				VM_OBJECT_UNLOCK(object);
63885eeca35SAlan Cox 				return (TRUE);
63985eeca35SAlan Cox 			}
64085eeca35SAlan Cox 		} else {
64185eeca35SAlan Cox 			vm_page_cache(m);
64285eeca35SAlan Cox 			vm_page_unlock(m);
64385eeca35SAlan Cox 		}
64485eeca35SAlan Cox 		VM_OBJECT_UNLOCK(object);
64585eeca35SAlan Cox 	}
64685eeca35SAlan Cox 	vm_page_unlock_queues();
64785eeca35SAlan Cox 	return (FALSE);
64885eeca35SAlan Cox }
64985eeca35SAlan Cox 
65085eeca35SAlan Cox /*
65185eeca35SAlan Cox  * Increase the number of cached pages.  The specified value, "tries",
65285eeca35SAlan Cox  * determines which categories of pages are cached:
65385eeca35SAlan Cox  *
65485eeca35SAlan Cox  *  0: All clean, inactive pages within the specified physical address range
65585eeca35SAlan Cox  *     are cached.  Will not sleep.
65685eeca35SAlan Cox  *  1: The vm_lowmem handlers are called.  All inactive pages within
65785eeca35SAlan Cox  *     the specified physical address range are cached.  May sleep.
65885eeca35SAlan Cox  *  2: The vm_lowmem handlers are called.  All inactive and active pages
65985eeca35SAlan Cox  *     within the specified physical address range are cached.  May sleep.
66085eeca35SAlan Cox  */
66185eeca35SAlan Cox void
66285eeca35SAlan Cox vm_pageout_grow_cache(int tries, vm_paddr_t low, vm_paddr_t high)
66385eeca35SAlan Cox {
66485eeca35SAlan Cox 	int actl, actmax, inactl, inactmax;
66585eeca35SAlan Cox 
66685eeca35SAlan Cox 	if (tries > 0) {
66785eeca35SAlan Cox 		/*
66885eeca35SAlan Cox 		 * Decrease registered cache sizes.  The vm_lowmem handlers
66985eeca35SAlan Cox 		 * may acquire locks and/or sleep, so they can only be invoked
67085eeca35SAlan Cox 		 * when "tries" is greater than zero.
67185eeca35SAlan Cox 		 */
67285eeca35SAlan Cox 		EVENTHANDLER_INVOKE(vm_lowmem, 0);
67385eeca35SAlan Cox 
67485eeca35SAlan Cox 		/*
67585eeca35SAlan Cox 		 * We do this explicitly after the caches have been drained
67685eeca35SAlan Cox 		 * above.
67785eeca35SAlan Cox 		 */
67885eeca35SAlan Cox 		uma_reclaim();
67985eeca35SAlan Cox 	}
68085eeca35SAlan Cox 	inactl = 0;
68185eeca35SAlan Cox 	inactmax = cnt.v_inactive_count;
68285eeca35SAlan Cox 	actl = 0;
68385eeca35SAlan Cox 	actmax = tries < 2 ? 0 : cnt.v_active_count;
68485eeca35SAlan Cox again:
68585eeca35SAlan Cox 	if (inactl < inactmax && vm_pageout_launder(PQ_INACTIVE, tries, low,
68685eeca35SAlan Cox 	    high)) {
68785eeca35SAlan Cox 		inactl++;
68885eeca35SAlan Cox 		goto again;
68985eeca35SAlan Cox 	}
69085eeca35SAlan Cox 	if (actl < actmax && vm_pageout_launder(PQ_ACTIVE, tries, low, high)) {
69185eeca35SAlan Cox 		actl++;
69285eeca35SAlan Cox 		goto again;
69385eeca35SAlan Cox 	}
69485eeca35SAlan Cox }
69585eeca35SAlan Cox 
69638efa82bSJohn Dyson #if !defined(NO_SWAPPING)
69726f9a767SRodney W. Grimes /*
69826f9a767SRodney W. Grimes  *	vm_pageout_object_deactivate_pages
69926f9a767SRodney W. Grimes  *
700ce186587SAlan Cox  *	Deactivate enough pages to satisfy the inactive target
701ce186587SAlan Cox  *	requirements.
70226f9a767SRodney W. Grimes  *
70326f9a767SRodney W. Grimes  *	The object and map must be locked.
70426f9a767SRodney W. Grimes  */
70538efa82bSJohn Dyson static void
706ce186587SAlan Cox vm_pageout_object_deactivate_pages(pmap_t pmap, vm_object_t first_object,
707ce186587SAlan Cox     long desired)
70826f9a767SRodney W. Grimes {
709ecf6279fSAlan Cox 	vm_object_t backing_object, object;
710ce186587SAlan Cox 	vm_page_t p;
71182bfb965SAlan Cox 	int actcount, remove_mode;
71226f9a767SRodney W. Grimes 
713ecf6279fSAlan Cox 	VM_OBJECT_LOCK_ASSERT(first_object, MA_OWNED);
71401381811SJohn Baldwin 	if (first_object->type == OBJT_DEVICE ||
71582bfb965SAlan Cox 	    first_object->type == OBJT_SG)
71638efa82bSJohn Dyson 		return;
717ecf6279fSAlan Cox 	for (object = first_object;; object = backing_object) {
718ecf6279fSAlan Cox 		if (pmap_resident_count(pmap) <= desired)
719ecf6279fSAlan Cox 			goto unlock_return;
720447fe2a4SAlan Cox 		VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
72182bfb965SAlan Cox 		if (object->type == OBJT_PHYS || object->paging_in_progress)
722ecf6279fSAlan Cox 			goto unlock_return;
72326f9a767SRodney W. Grimes 
72485b1dc89SAlan Cox 		remove_mode = 0;
72538efa82bSJohn Dyson 		if (object->shadow_count > 1)
72638efa82bSJohn Dyson 			remove_mode = 1;
72726f9a767SRodney W. Grimes 		/*
728ce186587SAlan Cox 		 * Scan the object's entire memory queue.
72926f9a767SRodney W. Grimes 		 */
730ce186587SAlan Cox 		TAILQ_FOREACH(p, &object->memq, listq) {
731447fe2a4SAlan Cox 			if (pmap_resident_count(pmap) <= desired)
732447fe2a4SAlan Cox 				goto unlock_return;
733ce186587SAlan Cox 			if ((p->oflags & VPO_BUSY) != 0 || p->busy != 0)
734447fe2a4SAlan Cox 				continue;
735ce186587SAlan Cox 			PCPU_INC(cnt.v_pdpages);
7362965a453SKip Macy 			vm_page_lock(p);
737ce186587SAlan Cox 			if (p->wire_count != 0 || p->hold_count != 0 ||
738ecf6279fSAlan Cox 			    !pmap_page_exists_quick(pmap, p)) {
7392965a453SKip Macy 				vm_page_unlock(p);
7400d94caffSDavid Greenman 				continue;
7410d94caffSDavid Greenman 			}
7420385347cSPeter Wemm 			actcount = pmap_ts_referenced(p);
7433407fefeSKonstantin Belousov 			if ((p->aflags & PGA_REFERENCED) != 0) {
744ce186587SAlan Cox 				if (actcount == 0)
7457e006499SJohn Dyson 					actcount = 1;
7463407fefeSKonstantin Belousov 				vm_page_aflag_clear(p, PGA_REFERENCED);
747ef743ce6SJohn Dyson 			}
748ce186587SAlan Cox 			if (p->queue != PQ_ACTIVE && actcount != 0) {
749ef743ce6SJohn Dyson 				vm_page_activate(p);
7507e006499SJohn Dyson 				p->act_count += actcount;
751c8c4b40cSJohn Dyson 			} else if (p->queue == PQ_ACTIVE) {
752ce186587SAlan Cox 				if (actcount == 0) {
753ce186587SAlan Cox 					p->act_count -= min(p->act_count,
754ce186587SAlan Cox 					    ACT_DECLINE);
755ce186587SAlan Cox 					if (!remove_mode &&
756ce186587SAlan Cox 					    (vm_pageout_algorithm ||
757ce186587SAlan Cox 					    p->act_count == 0)) {
7584fec79beSAlan Cox 						pmap_remove_all(p);
75926f9a767SRodney W. Grimes 						vm_page_deactivate(p);
76026f9a767SRodney W. Grimes 					} else {
761ce186587SAlan Cox 						vm_page_lock_queues();
762e5b006ffSAlan Cox 						vm_page_requeue(p);
763ce186587SAlan Cox 						vm_page_unlock_queues();
764c8c4b40cSJohn Dyson 					}
765c8c4b40cSJohn Dyson 				} else {
766eaf13dd7SJohn Dyson 					vm_page_activate(p);
767ce186587SAlan Cox 					if (p->act_count < ACT_MAX -
768ce186587SAlan Cox 					    ACT_ADVANCE)
76938efa82bSJohn Dyson 						p->act_count += ACT_ADVANCE;
770ce186587SAlan Cox 					vm_page_lock_queues();
771e5b006ffSAlan Cox 					vm_page_requeue(p);
7722965a453SKip Macy 					vm_page_unlock_queues();
773ce186587SAlan Cox 				}
774ce186587SAlan Cox 			} else if (p->queue == PQ_INACTIVE)
775ce186587SAlan Cox 				pmap_remove_all(p);
7762965a453SKip Macy 			vm_page_unlock(p);
77726f9a767SRodney W. Grimes 		}
778ecf6279fSAlan Cox 		if ((backing_object = object->backing_object) == NULL)
779ecf6279fSAlan Cox 			goto unlock_return;
780ecf6279fSAlan Cox 		VM_OBJECT_LOCK(backing_object);
781ecf6279fSAlan Cox 		if (object != first_object)
782ecf6279fSAlan Cox 			VM_OBJECT_UNLOCK(object);
78338efa82bSJohn Dyson 	}
784ecf6279fSAlan Cox unlock_return:
785ecf6279fSAlan Cox 	if (object != first_object)
786ecf6279fSAlan Cox 		VM_OBJECT_UNLOCK(object);
78726f9a767SRodney W. Grimes }
78826f9a767SRodney W. Grimes 
78926f9a767SRodney W. Grimes /*
79026f9a767SRodney W. Grimes  * deactivate some number of pages in a map, try to do it fairly, but
79126f9a767SRodney W. Grimes  * that is really hard to do.
79226f9a767SRodney W. Grimes  */
793cd41fc12SDavid Greenman static void
79438efa82bSJohn Dyson vm_pageout_map_deactivate_pages(map, desired)
79526f9a767SRodney W. Grimes 	vm_map_t map;
796ecf6279fSAlan Cox 	long desired;
79726f9a767SRodney W. Grimes {
79826f9a767SRodney W. Grimes 	vm_map_entry_t tmpe;
79938efa82bSJohn Dyson 	vm_object_t obj, bigobj;
80030105b9eSTor Egge 	int nothingwired;
8010d94caffSDavid Greenman 
802d974f03cSAlan Cox 	if (!vm_map_trylock(map))
80326f9a767SRodney W. Grimes 		return;
80438efa82bSJohn Dyson 
80538efa82bSJohn Dyson 	bigobj = NULL;
80630105b9eSTor Egge 	nothingwired = TRUE;
80738efa82bSJohn Dyson 
80838efa82bSJohn Dyson 	/*
80938efa82bSJohn Dyson 	 * first, search out the biggest object, and try to free pages from
81038efa82bSJohn Dyson 	 * that.
81138efa82bSJohn Dyson 	 */
81226f9a767SRodney W. Grimes 	tmpe = map->header.next;
81338efa82bSJohn Dyson 	while (tmpe != &map->header) {
8149fdfe602SMatthew Dillon 		if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
81538efa82bSJohn Dyson 			obj = tmpe->object.vm_object;
8160774dfb3SAlan Cox 			if (obj != NULL && VM_OBJECT_TRYLOCK(obj)) {
8170774dfb3SAlan Cox 				if (obj->shadow_count <= 1 &&
8180774dfb3SAlan Cox 				    (bigobj == NULL ||
8190774dfb3SAlan Cox 				     bigobj->resident_page_count < obj->resident_page_count)) {
8200774dfb3SAlan Cox 					if (bigobj != NULL)
8210774dfb3SAlan Cox 						VM_OBJECT_UNLOCK(bigobj);
82238efa82bSJohn Dyson 					bigobj = obj;
8230774dfb3SAlan Cox 				} else
8240774dfb3SAlan Cox 					VM_OBJECT_UNLOCK(obj);
82538efa82bSJohn Dyson 			}
82638efa82bSJohn Dyson 		}
82730105b9eSTor Egge 		if (tmpe->wired_count > 0)
82830105b9eSTor Egge 			nothingwired = FALSE;
82938efa82bSJohn Dyson 		tmpe = tmpe->next;
83038efa82bSJohn Dyson 	}
83138efa82bSJohn Dyson 
8320774dfb3SAlan Cox 	if (bigobj != NULL) {
833ecf6279fSAlan Cox 		vm_pageout_object_deactivate_pages(map->pmap, bigobj, desired);
8340774dfb3SAlan Cox 		VM_OBJECT_UNLOCK(bigobj);
8350774dfb3SAlan Cox 	}
83638efa82bSJohn Dyson 	/*
83738efa82bSJohn Dyson 	 * Next, hunt around for other pages to deactivate.  We actually
83838efa82bSJohn Dyson 	 * do this search sort of wrong -- .text first is not the best idea.
83938efa82bSJohn Dyson 	 */
84038efa82bSJohn Dyson 	tmpe = map->header.next;
84138efa82bSJohn Dyson 	while (tmpe != &map->header) {
842b1028ad1SLuoqi Chen 		if (pmap_resident_count(vm_map_pmap(map)) <= desired)
84338efa82bSJohn Dyson 			break;
8449fdfe602SMatthew Dillon 		if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
84538efa82bSJohn Dyson 			obj = tmpe->object.vm_object;
8460774dfb3SAlan Cox 			if (obj != NULL) {
8470774dfb3SAlan Cox 				VM_OBJECT_LOCK(obj);
848ecf6279fSAlan Cox 				vm_pageout_object_deactivate_pages(map->pmap, obj, desired);
8490774dfb3SAlan Cox 				VM_OBJECT_UNLOCK(obj);
8500774dfb3SAlan Cox 			}
85138efa82bSJohn Dyson 		}
85226f9a767SRodney W. Grimes 		tmpe = tmpe->next;
85338857e7fSAlan Cox 	}
85438efa82bSJohn Dyson 
85538efa82bSJohn Dyson 	/*
85638efa82bSJohn Dyson 	 * Remove all mappings if a process is swapped out, this will free page
85738efa82bSJohn Dyson 	 * table pages.
85838efa82bSJohn Dyson 	 */
85938857e7fSAlan Cox 	if (desired == 0 && nothingwired) {
8608d01a3b2SNathan Whitehorn 		pmap_remove(vm_map_pmap(map), vm_map_min(map),
8618d01a3b2SNathan Whitehorn 		    vm_map_max(map));
86238857e7fSAlan Cox 	}
86338efa82bSJohn Dyson 	vm_map_unlock(map);
86426f9a767SRodney W. Grimes }
865a1287949SEivind Eklund #endif		/* !defined(NO_SWAPPING) */
866df8bae1dSRodney W. Grimes 
8671c7c3c6aSMatthew Dillon /*
868df8bae1dSRodney W. Grimes  *	vm_pageout_scan does the dirty work for the pageout daemon.
869df8bae1dSRodney W. Grimes  */
8702b6b0df7SMatthew Dillon static void
8712b6b0df7SMatthew Dillon vm_pageout_scan(int pass)
872df8bae1dSRodney W. Grimes {
873502ba6e4SJohn Dyson 	vm_page_t m, next;
874936524aaSMatthew Dillon 	struct vm_page marker;
8751c7c3c6aSMatthew Dillon 	int page_shortage, maxscan, pcount;
8762cba1ccdSAlan Cox 	int addl_page_shortage;
877df8bae1dSRodney W. Grimes 	vm_object_t object;
8782446e4f0SAlan Cox 	int actcount;
879f6b04d2bSDavid Greenman 	int vnodes_skipped = 0;
8802b6b0df7SMatthew Dillon 	int maxlaunder;
88148cc2fc7SKonstantin Belousov 	boolean_t queues_locked;
8820d94caffSDavid Greenman 
883df8bae1dSRodney W. Grimes 	/*
884855a310fSJeff Roberson 	 * Decrease registered cache sizes.
885855a310fSJeff Roberson 	 */
886855a310fSJeff Roberson 	EVENTHANDLER_INVOKE(vm_lowmem, 0);
887855a310fSJeff Roberson 	/*
888855a310fSJeff Roberson 	 * We do this explicitly after the caches have been drained above.
889855a310fSJeff Roberson 	 */
890855a310fSJeff Roberson 	uma_reclaim();
8915985940eSJohn Dyson 
892*311e34e2SKonstantin Belousov 	/*
893*311e34e2SKonstantin Belousov 	 * The addl_page_shortage is the the number of temporarily
894*311e34e2SKonstantin Belousov 	 * stuck pages in the inactive queue.  In other words, the
895*311e34e2SKonstantin Belousov 	 * number of pages from cnt.v_inactive_count that should be
896*311e34e2SKonstantin Belousov 	 * discounted in setting the target for the active queue scan.
897*311e34e2SKonstantin Belousov 	 */
8982cba1ccdSAlan Cox 	addl_page_shortage = atomic_readandclear_int(&vm_pageout_deficit);
899b182ec9eSJohn Dyson 
9001c7c3c6aSMatthew Dillon 	/*
9011c7c3c6aSMatthew Dillon 	 * Calculate the number of pages we want to either free or move
9022b6b0df7SMatthew Dillon 	 * to the cache.
9031c7c3c6aSMatthew Dillon 	 */
9042cba1ccdSAlan Cox 	page_shortage = vm_paging_target() + addl_page_shortage;
9051c7c3c6aSMatthew Dillon 
906af394cfaSJung-uk Kim 	vm_pageout_init_marker(&marker, PQ_INACTIVE);
907936524aaSMatthew Dillon 
908936524aaSMatthew Dillon 	/*
9091c7c3c6aSMatthew Dillon 	 * Start scanning the inactive queue for pages we can move to the
9101c7c3c6aSMatthew Dillon 	 * cache or free.  The scan will stop when the target is reached or
911936524aaSMatthew Dillon 	 * we have scanned the entire inactive queue.  Note that m->act_count
912936524aaSMatthew Dillon 	 * is not used to form decisions for the inactive queue, only for the
913936524aaSMatthew Dillon 	 * active queue.
9142b6b0df7SMatthew Dillon 	 *
9152b6b0df7SMatthew Dillon 	 * maxlaunder limits the number of dirty pages we flush per scan.
9162b6b0df7SMatthew Dillon 	 * For most systems a smaller value (16 or 32) is more robust under
9172b6b0df7SMatthew Dillon 	 * extreme memory and disk pressure because any unnecessary writes
9182b6b0df7SMatthew Dillon 	 * to disk can result in extreme performance degredation.  However,
9192b6b0df7SMatthew Dillon 	 * systems with excessive dirty pages (especially when MAP_NOSYNC is
9202b6b0df7SMatthew Dillon 	 * used) will die horribly with limited laundering.  If the pageout
9212b6b0df7SMatthew Dillon 	 * daemon cannot clean enough pages in the first pass, we let it go
9222b6b0df7SMatthew Dillon 	 * all out in succeeding passes.
9231c7c3c6aSMatthew Dillon 	 */
9242b6b0df7SMatthew Dillon 	if ((maxlaunder = vm_max_launder) <= 1)
9252b6b0df7SMatthew Dillon 		maxlaunder = 1;
9262b6b0df7SMatthew Dillon 	if (pass)
9272b6b0df7SMatthew Dillon 		maxlaunder = 10000;
9283e1b578aSAlan Cox 	vm_page_lock_queues();
92948cc2fc7SKonstantin Belousov 	queues_locked = TRUE;
9302feb50bfSAttilio Rao 	maxscan = cnt.v_inactive_count;
9316d03d577SMatthew Dillon 
932be72f788SAlan Cox 	for (m = TAILQ_FIRST(&vm_page_queues[PQ_INACTIVE].pl);
9331c7c3c6aSMatthew Dillon 	     m != NULL && maxscan-- > 0 && page_shortage > 0;
934e929c00dSKirk McKusick 	     m = next) {
93548cc2fc7SKonstantin Belousov 		KASSERT(queues_locked, ("unlocked queues"));
93648cc2fc7SKonstantin Belousov 		mtx_assert(&vm_page_queue_mtx, MA_OWNED);
937d4961bcbSKonstantin Belousov 		KASSERT(m->queue == PQ_INACTIVE, ("Inactive queue %p", m));
938df8bae1dSRodney W. Grimes 
939393a081dSAttilio Rao 		cnt.v_pdpages++;
940b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(m, pageq);
941df8bae1dSRodney W. Grimes 
942936524aaSMatthew Dillon 		/*
943936524aaSMatthew Dillon 		 * skip marker pages
944936524aaSMatthew Dillon 		 */
945936524aaSMatthew Dillon 		if (m->flags & PG_MARKER)
946936524aaSMatthew Dillon 			continue;
947936524aaSMatthew Dillon 
9487900f95dSKonstantin Belousov 		KASSERT((m->flags & PG_FICTITIOUS) == 0,
9497900f95dSKonstantin Belousov 		    ("Fictitious page %p cannot be in inactive queue", m));
9507900f95dSKonstantin Belousov 		KASSERT((m->oflags & VPO_UNMANAGED) == 0,
9517900f95dSKonstantin Belousov 		    ("Unmanaged page %p cannot be in inactive queue", m));
9527900f95dSKonstantin Belousov 
9538c616246SKonstantin Belousov 		/*
954*311e34e2SKonstantin Belousov 		 * The page or object lock acquisitions fail if the
955*311e34e2SKonstantin Belousov 		 * page was removed from the queue or moved to a
956*311e34e2SKonstantin Belousov 		 * different position within the queue.  In either
957*311e34e2SKonstantin Belousov 		 * case, addl_page_shortage should not be incremented.
9588c616246SKonstantin Belousov 		 */
9598c616246SKonstantin Belousov 		if (!vm_pageout_page_lock(m, &next)) {
9608c616246SKonstantin Belousov 			vm_page_unlock(m);
961b182ec9eSJohn Dyson 			continue;
962df8bae1dSRodney W. Grimes 		}
9639ee2165fSAlan Cox 		object = m->object;
9648dbca793STor Egge 		if (!VM_OBJECT_TRYLOCK(object) &&
965*311e34e2SKonstantin Belousov 		    !vm_pageout_fallback_object_lock(m, &next)) {
9662965a453SKip Macy 			vm_page_unlock(m);
967*311e34e2SKonstantin Belousov 			VM_OBJECT_UNLOCK(object);
96834d9e6fdSAlan Cox 			continue;
96934d9e6fdSAlan Cox 		}
970*311e34e2SKonstantin Belousov 
971*311e34e2SKonstantin Belousov 		/*
972*311e34e2SKonstantin Belousov 		 * Don't mess with busy pages, keep them at at the
973*311e34e2SKonstantin Belousov 		 * front of the queue, most likely they are being
974*311e34e2SKonstantin Belousov 		 * paged out.  Increment addl_page_shortage for busy
975*311e34e2SKonstantin Belousov 		 * pages, because they may leave the inactive queue
976*311e34e2SKonstantin Belousov 		 * shortly after page scan is finished.
977*311e34e2SKonstantin Belousov 		 */
978*311e34e2SKonstantin Belousov 		if (m->busy != 0 || (m->oflags & VPO_BUSY) != 0) {
9792965a453SKip Macy 			vm_page_unlock(m);
98034d9e6fdSAlan Cox 			VM_OBJECT_UNLOCK(object);
981b182ec9eSJohn Dyson 			addl_page_shortage++;
98226f9a767SRodney W. Grimes 			continue;
98326f9a767SRodney W. Grimes 		}
984bd7e5f99SJohn Dyson 
9857e006499SJohn Dyson 		/*
98648cc2fc7SKonstantin Belousov 		 * We unlock vm_page_queue_mtx, invalidating the
98748cc2fc7SKonstantin Belousov 		 * 'next' pointer.  Use our marker to remember our
98848cc2fc7SKonstantin Belousov 		 * place.
98948cc2fc7SKonstantin Belousov 		 */
99048cc2fc7SKonstantin Belousov 		TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE].pl,
99148cc2fc7SKonstantin Belousov 		    m, &marker, pageq);
99248cc2fc7SKonstantin Belousov 		vm_page_unlock_queues();
99348cc2fc7SKonstantin Belousov 		queues_locked = FALSE;
99448cc2fc7SKonstantin Belousov 
99548cc2fc7SKonstantin Belousov 		/*
9961c7c3c6aSMatthew Dillon 		 * If the object is not being used, we ignore previous
9971c7c3c6aSMatthew Dillon 		 * references.
9987e006499SJohn Dyson 		 */
99934d9e6fdSAlan Cox 		if (object->ref_count == 0) {
10003407fefeSKonstantin Belousov 			vm_page_aflag_clear(m, PGA_REFERENCED);
100147916d0cSAlan Cox 			KASSERT(!pmap_page_is_mapped(m),
100247916d0cSAlan Cox 			    ("vm_pageout_scan: page %p is mapped", m));
10037e006499SJohn Dyson 
10047e006499SJohn Dyson 		/*
10051c7c3c6aSMatthew Dillon 		 * Otherwise, if the page has been referenced while in the
10061c7c3c6aSMatthew Dillon 		 * inactive queue, we bump the "activation count" upwards,
10071c7c3c6aSMatthew Dillon 		 * making it less likely that the page will be added back to
10081c7c3c6aSMatthew Dillon 		 * the inactive queue prematurely again.  Here we check the
10091c7c3c6aSMatthew Dillon 		 * page tables (or emulated bits, if any), given the upper
10101c7c3c6aSMatthew Dillon 		 * level VM system not knowing anything about existing
10111c7c3c6aSMatthew Dillon 		 * references.
10127e006499SJohn Dyson 		 */
10135d10ef20SKonstantin Belousov 		} else if ((m->aflags & PGA_REFERENCED) == 0 &&
10145d10ef20SKonstantin Belousov 		    (actcount = pmap_ts_referenced(m)) != 0) {
1015ef743ce6SJohn Dyson 			vm_page_activate(m);
10162965a453SKip Macy 			vm_page_unlock(m);
10174c6a2e7aSAlan Cox 			m->act_count += actcount + ACT_ADVANCE;
10184c6a2e7aSAlan Cox 			VM_OBJECT_UNLOCK(object);
101948cc2fc7SKonstantin Belousov 			goto relock_queues;
10202fe6e4d7SDavid Greenman 		}
1021ef743ce6SJohn Dyson 
10227e006499SJohn Dyson 		/*
10231c7c3c6aSMatthew Dillon 		 * If the upper level VM system knows about any page
10241c7c3c6aSMatthew Dillon 		 * references, we activate the page.  We also set the
10251c7c3c6aSMatthew Dillon 		 * "activation count" higher than normal so that we will less
10261c7c3c6aSMatthew Dillon 		 * likely place pages back onto the inactive queue again.
10277e006499SJohn Dyson 		 */
10283407fefeSKonstantin Belousov 		if ((m->aflags & PGA_REFERENCED) != 0) {
10293407fefeSKonstantin Belousov 			vm_page_aflag_clear(m, PGA_REFERENCED);
10300385347cSPeter Wemm 			actcount = pmap_ts_referenced(m);
103126f9a767SRodney W. Grimes 			vm_page_activate(m);
10322965a453SKip Macy 			vm_page_unlock(m);
10334c6a2e7aSAlan Cox 			m->act_count += actcount + ACT_ADVANCE + 1;
10344c6a2e7aSAlan Cox 			VM_OBJECT_UNLOCK(object);
103548cc2fc7SKonstantin Belousov 			goto relock_queues;
10360d94caffSDavid Greenman 		}
103767bf6868SJohn Dyson 
1038*311e34e2SKonstantin Belousov 		if (m->hold_count != 0) {
1039*311e34e2SKonstantin Belousov 			vm_page_unlock(m);
1040*311e34e2SKonstantin Belousov 			VM_OBJECT_UNLOCK(object);
1041*311e34e2SKonstantin Belousov 
1042*311e34e2SKonstantin Belousov 			/*
1043*311e34e2SKonstantin Belousov 			 * Held pages are essentially stuck in the
1044*311e34e2SKonstantin Belousov 			 * queue.  So, they ought to be discounted
1045*311e34e2SKonstantin Belousov 			 * from cnt.v_inactive_count.  See the
1046*311e34e2SKonstantin Belousov 			 * calculation of the page_shortage for the
1047*311e34e2SKonstantin Belousov 			 * loop over the active queue below.
1048*311e34e2SKonstantin Belousov 			 */
1049*311e34e2SKonstantin Belousov 			addl_page_shortage++;
1050*311e34e2SKonstantin Belousov 			goto relock_queues;
1051*311e34e2SKonstantin Belousov 		}
1052*311e34e2SKonstantin Belousov 
10537e006499SJohn Dyson 		/*
1054b78ddb0bSAlan Cox 		 * If the upper level VM system does not believe that the page
1055b78ddb0bSAlan Cox 		 * is fully dirty, but it is mapped for write access, then we
1056b78ddb0bSAlan Cox 		 * consult the pmap to see if the page's dirty status should
1057b78ddb0bSAlan Cox 		 * be updated.
10587e006499SJohn Dyson 		 */
1059b78ddb0bSAlan Cox 		if (m->dirty != VM_PAGE_BITS_ALL &&
10606031c68dSAlan Cox 		    pmap_page_is_write_mapped(m)) {
1061a3dfacb5SAlan Cox 			/*
1062a3dfacb5SAlan Cox 			 * Avoid a race condition: Unless write access is
1063a3dfacb5SAlan Cox 			 * removed from the page, another processor could
1064a3dfacb5SAlan Cox 			 * modify it before all access is removed by the call
1065a3dfacb5SAlan Cox 			 * to vm_page_cache() below.  If vm_page_cache() finds
1066a3dfacb5SAlan Cox 			 * that the page has been modified when it removes all
1067a3dfacb5SAlan Cox 			 * access, it panics because it cannot cache dirty
1068a3dfacb5SAlan Cox 			 * pages.  In principle, we could eliminate just write
1069a3dfacb5SAlan Cox 			 * access here rather than all access.  In the expected
1070a3dfacb5SAlan Cox 			 * case, when there are no last instant modifications
1071a3dfacb5SAlan Cox 			 * to the page, removing all access will be cheaper
1072a3dfacb5SAlan Cox 			 * overall.
1073a3dfacb5SAlan Cox 			 */
1074b78ddb0bSAlan Cox 			if (pmap_is_modified(m))
10757dbf82dcSMatthew Dillon 				vm_page_dirty(m);
1076b78ddb0bSAlan Cox 			else if (m->dirty == 0)
1077b78ddb0bSAlan Cox 				pmap_remove_all(m);
107830dcfc09SJohn Dyson 		}
1079dcbcd518SBruce Evans 
10806989c456SAlan Cox 		if (m->valid == 0) {
10817e006499SJohn Dyson 			/*
10827e006499SJohn Dyson 			 * Invalid pages can be easily freed
10837e006499SJohn Dyson 			 */
10846989c456SAlan Cox 			vm_page_free(m);
108548cc2fc7SKonstantin Belousov 			PCPU_INC(cnt.v_dfree);
10861c7c3c6aSMatthew Dillon 			--page_shortage;
1087bd7e5f99SJohn Dyson 		} else if (m->dirty == 0) {
10886989c456SAlan Cox 			/*
10896989c456SAlan Cox 			 * Clean pages can be placed onto the cache queue.
10906989c456SAlan Cox 			 * This effectively frees them.
10916989c456SAlan Cox 			 */
1092bd7e5f99SJohn Dyson 			vm_page_cache(m);
10931c7c3c6aSMatthew Dillon 			--page_shortage;
10942b6b0df7SMatthew Dillon 		} else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) {
10957e006499SJohn Dyson 			/*
10962b6b0df7SMatthew Dillon 			 * Dirty pages need to be paged out, but flushing
10972b6b0df7SMatthew Dillon 			 * a page is extremely expensive verses freeing
10982b6b0df7SMatthew Dillon 			 * a clean page.  Rather then artificially limiting
10992b6b0df7SMatthew Dillon 			 * the number of pages we can flush, we instead give
11002b6b0df7SMatthew Dillon 			 * dirty pages extra priority on the inactive queue
11012b6b0df7SMatthew Dillon 			 * by forcing them to be cycled through the queue
11022b6b0df7SMatthew Dillon 			 * twice before being flushed, after which the
11032b6b0df7SMatthew Dillon 			 * (now clean) page will cycle through once more
11042b6b0df7SMatthew Dillon 			 * before being freed.  This significantly extends
11052b6b0df7SMatthew Dillon 			 * the thrash point for a heavily loaded machine.
11067e006499SJohn Dyson 			 */
11073407fefeSKonstantin Belousov 			m->flags |= PG_WINATCFLS;
110848cc2fc7SKonstantin Belousov 			vm_page_lock_queues();
110948cc2fc7SKonstantin Belousov 			queues_locked = TRUE;
1110e5b006ffSAlan Cox 			vm_page_requeue(m);
11110d94caffSDavid Greenman 		} else if (maxlaunder > 0) {
11122b6b0df7SMatthew Dillon 			/*
11132b6b0df7SMatthew Dillon 			 * We always want to try to flush some dirty pages if
11142b6b0df7SMatthew Dillon 			 * we encounter them, to keep the system stable.
11152b6b0df7SMatthew Dillon 			 * Normally this number is small, but under extreme
11162b6b0df7SMatthew Dillon 			 * pressure where there are insufficient clean pages
11172b6b0df7SMatthew Dillon 			 * on the inactive queue, we may have to go all out.
11182b6b0df7SMatthew Dillon 			 */
111997824da3SAlan Cox 			int swap_pageouts_ok, vfslocked = 0;
1120f6b04d2bSDavid Greenman 			struct vnode *vp = NULL;
112114137dc0SAlan Cox 			struct mount *mp = NULL;
11220d94caffSDavid Greenman 
112312ac6a1dSJohn Dyson 			if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) {
112412ac6a1dSJohn Dyson 				swap_pageouts_ok = 1;
112512ac6a1dSJohn Dyson 			} else {
112612ac6a1dSJohn Dyson 				swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts);
112712ac6a1dSJohn Dyson 				swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts &&
112890ecac61SMatthew Dillon 				vm_page_count_min());
112912ac6a1dSJohn Dyson 
113012ac6a1dSJohn Dyson 			}
113170111b90SJohn Dyson 
113270111b90SJohn Dyson 			/*
11331c7c3c6aSMatthew Dillon 			 * We don't bother paging objects that are "dead".
11341c7c3c6aSMatthew Dillon 			 * Those objects are in a "rundown" state.
113570111b90SJohn Dyson 			 */
113670111b90SJohn Dyson 			if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) {
11372965a453SKip Macy 				vm_page_unlock(m);
11383562af12SAlan Cox 				VM_OBJECT_UNLOCK(object);
113948cc2fc7SKonstantin Belousov 				vm_page_lock_queues();
114048cc2fc7SKonstantin Belousov 				queues_locked = TRUE;
1141e5b006ffSAlan Cox 				vm_page_requeue(m);
114248cc2fc7SKonstantin Belousov 				goto relock_queues;
114312ac6a1dSJohn Dyson 			}
114412ac6a1dSJohn Dyson 
11451c7c3c6aSMatthew Dillon 			/*
11462b6b0df7SMatthew Dillon 			 * The object is already known NOT to be dead.   It
11472b6b0df7SMatthew Dillon 			 * is possible for the vget() to block the whole
11482b6b0df7SMatthew Dillon 			 * pageout daemon, but the new low-memory handling
11492b6b0df7SMatthew Dillon 			 * code should prevent it.
11501c7c3c6aSMatthew Dillon 			 *
11512b6b0df7SMatthew Dillon 			 * The previous code skipped locked vnodes and, worse,
11522b6b0df7SMatthew Dillon 			 * reordered pages in the queue.  This results in
11532b6b0df7SMatthew Dillon 			 * completely non-deterministic operation and, on a
11542b6b0df7SMatthew Dillon 			 * busy system, can lead to extremely non-optimal
11552b6b0df7SMatthew Dillon 			 * pageouts.  For example, it can cause clean pages
11562b6b0df7SMatthew Dillon 			 * to be freed and dirty pages to be moved to the end
11572b6b0df7SMatthew Dillon 			 * of the queue.  Since dirty pages are also moved to
11582b6b0df7SMatthew Dillon 			 * the end of the queue once-cleaned, this gives
11592b6b0df7SMatthew Dillon 			 * way too large a weighting to defering the freeing
11602b6b0df7SMatthew Dillon 			 * of dirty pages.
11611c7c3c6aSMatthew Dillon 			 *
116223b59018SMatthew Dillon 			 * We can't wait forever for the vnode lock, we might
116323b59018SMatthew Dillon 			 * deadlock due to a vn_read() getting stuck in
116423b59018SMatthew Dillon 			 * vm_wait while holding this vnode.  We skip the
116523b59018SMatthew Dillon 			 * vnode if we can't get it in a reasonable amount
116623b59018SMatthew Dillon 			 * of time.
11671c7c3c6aSMatthew Dillon 			 */
11681c7c3c6aSMatthew Dillon 			if (object->type == OBJT_VNODE) {
11692965a453SKip Macy 				vm_page_unlock(m);
117024a1cce3SDavid Greenman 				vp = object->handle;
1171db27dcc0STor Egge 				if (vp->v_type == VREG &&
1172db27dcc0STor Egge 				    vn_start_write(vp, &mp, V_NOWAIT) != 0) {
11736129343dSKonstantin Belousov 					mp = NULL;
1174db27dcc0STor Egge 					++pageout_lock_miss;
1175db27dcc0STor Egge 					if (object->flags & OBJ_MIGHTBEDIRTY)
1176db27dcc0STor Egge 						vnodes_skipped++;
1177625e6c0aSTor Egge 					goto unlock_and_continue;
1178db27dcc0STor Egge 				}
1179b9f180d1SKonstantin Belousov 				KASSERT(mp != NULL,
1180b9f180d1SKonstantin Belousov 				    ("vp %p with NULL v_mount", vp));
118114137dc0SAlan Cox 				vm_object_reference_locked(object);
11823562af12SAlan Cox 				VM_OBJECT_UNLOCK(object);
118397824da3SAlan Cox 				vfslocked = VFS_LOCK_GIANT(vp->v_mount);
118497824da3SAlan Cox 				if (vget(vp, LK_EXCLUSIVE | LK_TIMELOCK,
118597824da3SAlan Cox 				    curthread)) {
11863562af12SAlan Cox 					VM_OBJECT_LOCK(object);
118723b59018SMatthew Dillon 					++pageout_lock_miss;
1188aef922f5SJohn Dyson 					if (object->flags & OBJ_MIGHTBEDIRTY)
1189925a3a41SJohn Dyson 						vnodes_skipped++;
1190625e6c0aSTor Egge 					vp = NULL;
1191625e6c0aSTor Egge 					goto unlock_and_continue;
119285a376ebSJohn Dyson 				}
11933562af12SAlan Cox 				VM_OBJECT_LOCK(object);
11942965a453SKip Macy 				vm_page_lock(m);
11953e1b578aSAlan Cox 				vm_page_lock_queues();
119648cc2fc7SKonstantin Belousov 				queues_locked = TRUE;
1197f35329acSJohn Dyson 				/*
1198936524aaSMatthew Dillon 				 * The page might have been moved to another
1199936524aaSMatthew Dillon 				 * queue during potential blocking in vget()
1200936524aaSMatthew Dillon 				 * above.  The page might have been freed and
120114137dc0SAlan Cox 				 * reused for another vnode.
1202f35329acSJohn Dyson 				 */
12039cf51988SAlan Cox 				if (m->queue != PQ_INACTIVE ||
1204936524aaSMatthew Dillon 				    m->object != object ||
1205625e6c0aSTor Egge 				    TAILQ_NEXT(m, pageq) != &marker) {
12062965a453SKip Macy 					vm_page_unlock(m);
1207b182ec9eSJohn Dyson 					if (object->flags & OBJ_MIGHTBEDIRTY)
1208925a3a41SJohn Dyson 						vnodes_skipped++;
12093562af12SAlan Cox 					goto unlock_and_continue;
1210b182ec9eSJohn Dyson 				}
1211b182ec9eSJohn Dyson 
1212f35329acSJohn Dyson 				/*
1213936524aaSMatthew Dillon 				 * The page may have been busied during the
121414137dc0SAlan Cox 				 * blocking in vget().  We don't move the
1215936524aaSMatthew Dillon 				 * page back onto the end of the queue so that
1216936524aaSMatthew Dillon 				 * statistics are more correct if we don't.
1217f35329acSJohn Dyson 				 */
12189af80719SAlan Cox 				if (m->busy || (m->oflags & VPO_BUSY)) {
12192965a453SKip Macy 					vm_page_unlock(m);
12203562af12SAlan Cox 					goto unlock_and_continue;
1221b182ec9eSJohn Dyson 				}
1222b182ec9eSJohn Dyson 
1223f35329acSJohn Dyson 				/*
122457601bcbSMatthew Dillon 				 * If the page has become held it might
122557601bcbSMatthew Dillon 				 * be undergoing I/O, so skip it
1226f35329acSJohn Dyson 				 */
1227b182ec9eSJohn Dyson 				if (m->hold_count) {
12282965a453SKip Macy 					vm_page_unlock(m);
1229e5b006ffSAlan Cox 					vm_page_requeue(m);
1230b182ec9eSJohn Dyson 					if (object->flags & OBJ_MIGHTBEDIRTY)
1231925a3a41SJohn Dyson 						vnodes_skipped++;
12323562af12SAlan Cox 					goto unlock_and_continue;
1233f6b04d2bSDavid Greenman 				}
1234a4156419SKonstantin Belousov 				vm_page_unlock_queues();
1235a4156419SKonstantin Belousov 				queues_locked = FALSE;
1236f6b04d2bSDavid Greenman 			}
1237f6b04d2bSDavid Greenman 
12380d94caffSDavid Greenman 			/*
12390d94caffSDavid Greenman 			 * If a page is dirty, then it is either being washed
12400d94caffSDavid Greenman 			 * (but not yet cleaned) or it is still in the
12410d94caffSDavid Greenman 			 * laundry.  If it is still in the laundry, then we
12422b6b0df7SMatthew Dillon 			 * start the cleaning operation.
1243936524aaSMatthew Dillon 			 *
12442b6b0df7SMatthew Dillon 			 * decrement page_shortage on success to account for
12452b6b0df7SMatthew Dillon 			 * the (future) cleaned page.  Otherwise we could wind
12462b6b0df7SMatthew Dillon 			 * up laundering or cleaning too many pages.
12470d94caffSDavid Greenman 			 */
12482b6b0df7SMatthew Dillon 			if (vm_pageout_clean(m) != 0) {
12492b6b0df7SMatthew Dillon 				--page_shortage;
1250936524aaSMatthew Dillon 				--maxlaunder;
12512b6b0df7SMatthew Dillon 			}
12523562af12SAlan Cox unlock_and_continue:
12532965a453SKip Macy 			vm_page_lock_assert(m, MA_NOTOWNED);
12546989c456SAlan Cox 			VM_OBJECT_UNLOCK(object);
125514137dc0SAlan Cox 			if (mp != NULL) {
125648cc2fc7SKonstantin Belousov 				if (queues_locked) {
12576989c456SAlan Cox 					vm_page_unlock_queues();
125848cc2fc7SKonstantin Belousov 					queues_locked = FALSE;
125948cc2fc7SKonstantin Belousov 				}
126014137dc0SAlan Cox 				if (vp != NULL)
1261f6b04d2bSDavid Greenman 					vput(vp);
126297824da3SAlan Cox 				VFS_UNLOCK_GIANT(vfslocked);
126314137dc0SAlan Cox 				vm_object_deallocate(object);
1264f2a2857bSKirk McKusick 				vn_finished_write(mp);
126548cc2fc7SKonstantin Belousov 			}
126648cc2fc7SKonstantin Belousov 			vm_page_lock_assert(m, MA_NOTOWNED);
126748cc2fc7SKonstantin Belousov 			goto relock_queues;
126848cc2fc7SKonstantin Belousov 		}
126948cc2fc7SKonstantin Belousov 		vm_page_unlock(m);
127048cc2fc7SKonstantin Belousov 		VM_OBJECT_UNLOCK(object);
127148cc2fc7SKonstantin Belousov relock_queues:
127248cc2fc7SKonstantin Belousov 		if (!queues_locked) {
12736989c456SAlan Cox 			vm_page_lock_queues();
127448cc2fc7SKonstantin Belousov 			queues_locked = TRUE;
12756989c456SAlan Cox 		}
1276625e6c0aSTor Egge 		next = TAILQ_NEXT(&marker, pageq);
1277625e6c0aSTor Egge 		TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl,
1278625e6c0aSTor Egge 		    &marker, pageq);
12790d94caffSDavid Greenman 	}
128026f9a767SRodney W. Grimes 
1281df8bae1dSRodney W. Grimes 	/*
1282936524aaSMatthew Dillon 	 * Compute the number of pages we want to try to move from the
1283936524aaSMatthew Dillon 	 * active queue to the inactive queue.
12841c7c3c6aSMatthew Dillon 	 */
12852feb50bfSAttilio Rao 	page_shortage = vm_paging_target() +
12862feb50bfSAttilio Rao 		cnt.v_inactive_target - cnt.v_inactive_count;
1287b182ec9eSJohn Dyson 	page_shortage += addl_page_shortage;
12881c7c3c6aSMatthew Dillon 
12891c7c3c6aSMatthew Dillon 	/*
1290936524aaSMatthew Dillon 	 * Scan the active queue for things we can deactivate. We nominally
1291936524aaSMatthew Dillon 	 * track the per-page activity counter and use it to locate
1292936524aaSMatthew Dillon 	 * deactivation candidates.
12931c7c3c6aSMatthew Dillon 	 */
12942feb50bfSAttilio Rao 	pcount = cnt.v_active_count;
1295be72f788SAlan Cox 	m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
12962965a453SKip Macy 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
12971c7c3c6aSMatthew Dillon 
1298b18bfc3dSJohn Dyson 	while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) {
1299f35329acSJohn Dyson 
13009cf51988SAlan Cox 		KASSERT(m->queue == PQ_ACTIVE,
1301d3c09dd7SAlan Cox 		    ("vm_pageout_scan: page %p isn't active", m));
1302f35329acSJohn Dyson 
1303b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(m, pageq);
13048dbca793STor Egge 		if ((m->flags & PG_MARKER) != 0) {
13058dbca793STor Egge 			m = next;
13068dbca793STor Egge 			continue;
13078dbca793STor Egge 		}
13087900f95dSKonstantin Belousov 		KASSERT((m->flags & PG_FICTITIOUS) == 0,
13097900f95dSKonstantin Belousov 		    ("Fictitious page %p cannot be in active queue", m));
13107900f95dSKonstantin Belousov 		KASSERT((m->oflags & VPO_UNMANAGED) == 0,
13117900f95dSKonstantin Belousov 		    ("Unmanaged page %p cannot be in active queue", m));
13129ee2165fSAlan Cox 		if (!vm_pageout_page_lock(m, &next)) {
13138c616246SKonstantin Belousov 			vm_page_unlock(m);
13142965a453SKip Macy 			m = next;
13152965a453SKip Macy 			continue;
13162965a453SKip Macy 		}
13179ee2165fSAlan Cox 		object = m->object;
13188dbca793STor Egge 		if (!VM_OBJECT_TRYLOCK(object) &&
13198dbca793STor Egge 		    !vm_pageout_fallback_object_lock(m, &next)) {
13208dbca793STor Egge 			VM_OBJECT_UNLOCK(object);
13212965a453SKip Macy 			vm_page_unlock(m);
13224b8a5c40SAlan Cox 			m = next;
1323b08abf6cSAlan Cox 			continue;
1324b08abf6cSAlan Cox 		}
1325b08abf6cSAlan Cox 
1326df8bae1dSRodney W. Grimes 		/*
132726f9a767SRodney W. Grimes 		 * Don't deactivate pages that are busy.
1328df8bae1dSRodney W. Grimes 		 */
1329a647a309SDavid Greenman 		if ((m->busy != 0) ||
13309af80719SAlan Cox 		    (m->oflags & VPO_BUSY) ||
1331f6b04d2bSDavid Greenman 		    (m->hold_count != 0)) {
13322965a453SKip Macy 			vm_page_unlock(m);
1333b08abf6cSAlan Cox 			VM_OBJECT_UNLOCK(object);
1334e5b006ffSAlan Cox 			vm_page_requeue(m);
133526f9a767SRodney W. Grimes 			m = next;
133626f9a767SRodney W. Grimes 			continue;
1337df8bae1dSRodney W. Grimes 		}
1338b18bfc3dSJohn Dyson 
1339b18bfc3dSJohn Dyson 		/*
1340b18bfc3dSJohn Dyson 		 * The count for pagedaemon pages is done after checking the
1341956f3135SPhilippe Charnier 		 * page for eligibility...
1342b18bfc3dSJohn Dyson 		 */
1343393a081dSAttilio Rao 		cnt.v_pdpages++;
1344ef743ce6SJohn Dyson 
13457e006499SJohn Dyson 		/*
13467e006499SJohn Dyson 		 * Check to see "how much" the page has been used.
13477e006499SJohn Dyson 		 */
13487e006499SJohn Dyson 		actcount = 0;
1349b08abf6cSAlan Cox 		if (object->ref_count != 0) {
13503407fefeSKonstantin Belousov 			if (m->aflags & PGA_REFERENCED) {
13517e006499SJohn Dyson 				actcount += 1;
13520d94caffSDavid Greenman 			}
13530385347cSPeter Wemm 			actcount += pmap_ts_referenced(m);
13547e006499SJohn Dyson 			if (actcount) {
13557e006499SJohn Dyson 				m->act_count += ACT_ADVANCE + actcount;
135638efa82bSJohn Dyson 				if (m->act_count > ACT_MAX)
135738efa82bSJohn Dyson 					m->act_count = ACT_MAX;
135838efa82bSJohn Dyson 			}
1359b18bfc3dSJohn Dyson 		}
1360ef743ce6SJohn Dyson 
13617e006499SJohn Dyson 		/*
13627e006499SJohn Dyson 		 * Since we have "tested" this bit, we need to clear it now.
13637e006499SJohn Dyson 		 */
13643407fefeSKonstantin Belousov 		vm_page_aflag_clear(m, PGA_REFERENCED);
1365ef743ce6SJohn Dyson 
13667e006499SJohn Dyson 		/*
13677e006499SJohn Dyson 		 * Only if an object is currently being used, do we use the
13687e006499SJohn Dyson 		 * page activation count stats.
13697e006499SJohn Dyson 		 */
1370b08abf6cSAlan Cox 		if (actcount && (object->ref_count != 0)) {
1371e5b006ffSAlan Cox 			vm_page_requeue(m);
137226f9a767SRodney W. Grimes 		} else {
137338efa82bSJohn Dyson 			m->act_count -= min(m->act_count, ACT_DECLINE);
13742b6b0df7SMatthew Dillon 			if (vm_pageout_algorithm ||
1375b08abf6cSAlan Cox 			    object->ref_count == 0 ||
13762b6b0df7SMatthew Dillon 			    m->act_count == 0) {
1377925a3a41SJohn Dyson 				page_shortage--;
1378b08abf6cSAlan Cox 				if (object->ref_count == 0) {
13795d4a7b79SAlan Cox 					KASSERT(!pmap_page_is_mapped(m),
13805d4a7b79SAlan Cox 				    ("vm_pageout_scan: page %p is mapped", m));
1381d4a272dbSJohn Dyson 					if (m->dirty == 0)
13820d94caffSDavid Greenman 						vm_page_cache(m);
1383d4a272dbSJohn Dyson 					else
1384d4a272dbSJohn Dyson 						vm_page_deactivate(m);
13850d94caffSDavid Greenman 				} else {
138626f9a767SRodney W. Grimes 					vm_page_deactivate(m);
1387df8bae1dSRodney W. Grimes 				}
138838efa82bSJohn Dyson 			} else {
1389e5b006ffSAlan Cox 				vm_page_requeue(m);
139038efa82bSJohn Dyson 			}
1391df8bae1dSRodney W. Grimes 		}
13922965a453SKip Macy 		vm_page_unlock(m);
1393b08abf6cSAlan Cox 		VM_OBJECT_UNLOCK(object);
139426f9a767SRodney W. Grimes 		m = next;
139526f9a767SRodney W. Grimes 	}
13968ffc1519SAlan Cox 	vm_page_unlock_queues();
1397ceb0cf87SJohn Dyson #if !defined(NO_SWAPPING)
1398ceb0cf87SJohn Dyson 	/*
1399ceb0cf87SJohn Dyson 	 * Idle process swapout -- run once per second.
1400ceb0cf87SJohn Dyson 	 */
1401ceb0cf87SJohn Dyson 	if (vm_swap_idle_enabled) {
1402ceb0cf87SJohn Dyson 		static long lsec;
1403227ee8a1SPoul-Henning Kamp 		if (time_second != lsec) {
140497824da3SAlan Cox 			vm_req_vmdaemon(VM_SWAP_IDLE);
1405227ee8a1SPoul-Henning Kamp 			lsec = time_second;
1406ceb0cf87SJohn Dyson 		}
1407ceb0cf87SJohn Dyson 	}
1408ceb0cf87SJohn Dyson #endif
1409ceb0cf87SJohn Dyson 
14105663e6deSDavid Greenman 	/*
1411f6b04d2bSDavid Greenman 	 * If we didn't get enough free pages, and we have skipped a vnode
14124c1f8ee9SDavid Greenman 	 * in a writeable object, wakeup the sync daemon.  And kick swapout
14134c1f8ee9SDavid Greenman 	 * if we did not get enough free pages.
1414f6b04d2bSDavid Greenman 	 */
141590ecac61SMatthew Dillon 	if (vm_paging_target() > 0) {
141690ecac61SMatthew Dillon 		if (vnodes_skipped && vm_page_count_min())
1417d50c1994SPeter Wemm 			(void) speedup_syncer();
141838efa82bSJohn Dyson #if !defined(NO_SWAPPING)
141997824da3SAlan Cox 		if (vm_swap_enabled && vm_page_count_target())
142097824da3SAlan Cox 			vm_req_vmdaemon(VM_SWAP_NORMAL);
14215afce282SDavid Greenman #endif
14224c1f8ee9SDavid Greenman 	}
14234c1f8ee9SDavid Greenman 
1424f6b04d2bSDavid Greenman 	/*
1425e92686d0SDavid Schultz 	 * If we are critically low on one of RAM or swap and low on
1426e92686d0SDavid Schultz 	 * the other, kill the largest process.  However, we avoid
1427e92686d0SDavid Schultz 	 * doing this on the first pass in order to give ourselves a
1428e92686d0SDavid Schultz 	 * chance to flush out dirty vnode-backed pages and to allow
1429e92686d0SDavid Schultz 	 * active pages to be moved to the inactive queue and reclaimed.
14302025d69bSKonstantin Belousov 	 */
14312025d69bSKonstantin Belousov 	if (pass != 0 &&
14322025d69bSKonstantin Belousov 	    ((swap_pager_avail < 64 && vm_page_count_min()) ||
14332025d69bSKonstantin Belousov 	     (swap_pager_full && vm_paging_target() > 0)))
14342025d69bSKonstantin Belousov 		vm_pageout_oom(VM_OOM_MEM);
14352025d69bSKonstantin Belousov }
14362025d69bSKonstantin Belousov 
14372025d69bSKonstantin Belousov 
14382025d69bSKonstantin Belousov void
14392025d69bSKonstantin Belousov vm_pageout_oom(int shortage)
14402025d69bSKonstantin Belousov {
14412025d69bSKonstantin Belousov 	struct proc *p, *bigproc;
14422025d69bSKonstantin Belousov 	vm_offset_t size, bigsize;
14432025d69bSKonstantin Belousov 	struct thread *td;
14446bed074cSKonstantin Belousov 	struct vmspace *vm;
14452025d69bSKonstantin Belousov 
14462025d69bSKonstantin Belousov 	/*
14471c58e4e5SJohn Baldwin 	 * We keep the process bigproc locked once we find it to keep anyone
14481c58e4e5SJohn Baldwin 	 * from messing with it; however, there is a possibility of
14491c58e4e5SJohn Baldwin 	 * deadlock if process B is bigproc and one of it's child processes
14501c58e4e5SJohn Baldwin 	 * attempts to propagate a signal to B while we are waiting for A's
14511c58e4e5SJohn Baldwin 	 * lock while walking this list.  To avoid this, we don't block on
14521c58e4e5SJohn Baldwin 	 * the process lock but just skip a process if it is already locked.
14535663e6deSDavid Greenman 	 */
14545663e6deSDavid Greenman 	bigproc = NULL;
14555663e6deSDavid Greenman 	bigsize = 0;
14561005a129SJohn Baldwin 	sx_slock(&allproc_lock);
1457e602ba25SJulian Elischer 	FOREACH_PROC_IN_SYSTEM(p) {
1458e602ba25SJulian Elischer 		int breakout;
1459dcbcd518SBruce Evans 
14601c58e4e5SJohn Baldwin 		if (PROC_TRYLOCK(p) == 0)
14611c58e4e5SJohn Baldwin 			continue;
14621c58e4e5SJohn Baldwin 		/*
14633f1c4c4fSKonstantin Belousov 		 * If this is a system, protected or killed process, skip it.
14645663e6deSDavid Greenman 		 */
14658e6fa660SJohn Baldwin 		if (p->p_state != PRS_NORMAL ||
14668e6fa660SJohn Baldwin 		    (p->p_flag & (P_INEXEC | P_PROTECTED | P_SYSTEM)) ||
14673f1c4c4fSKonstantin Belousov 		    (p->p_pid == 1) || P_KILLED(p) ||
14688f60c087SPoul-Henning Kamp 		    ((p->p_pid < 48) && (swap_pager_avail != 0))) {
14698606d880SJohn Baldwin 			PROC_UNLOCK(p);
14705663e6deSDavid Greenman 			continue;
14715663e6deSDavid Greenman 		}
14725663e6deSDavid Greenman 		/*
1473dcbcd518SBruce Evans 		 * If the process is in a non-running type state,
1474e602ba25SJulian Elischer 		 * don't touch it.  Check all the threads individually.
14755663e6deSDavid Greenman 		 */
1476e602ba25SJulian Elischer 		breakout = 0;
1477e602ba25SJulian Elischer 		FOREACH_THREAD_IN_PROC(p, td) {
1478982d11f8SJeff Roberson 			thread_lock(td);
147971fad9fdSJulian Elischer 			if (!TD_ON_RUNQ(td) &&
148071fad9fdSJulian Elischer 			    !TD_IS_RUNNING(td) &&
1481f497cda2SEdward Tomasz Napierala 			    !TD_IS_SLEEPING(td) &&
1482f497cda2SEdward Tomasz Napierala 			    !TD_IS_SUSPENDED(td)) {
1483982d11f8SJeff Roberson 				thread_unlock(td);
1484e602ba25SJulian Elischer 				breakout = 1;
1485e602ba25SJulian Elischer 				break;
1486e602ba25SJulian Elischer 			}
1487982d11f8SJeff Roberson 			thread_unlock(td);
1488e602ba25SJulian Elischer 		}
1489e602ba25SJulian Elischer 		if (breakout) {
14901c58e4e5SJohn Baldwin 			PROC_UNLOCK(p);
14915663e6deSDavid Greenman 			continue;
14925663e6deSDavid Greenman 		}
14935663e6deSDavid Greenman 		/*
14945663e6deSDavid Greenman 		 * get the process size
14955663e6deSDavid Greenman 		 */
14966bed074cSKonstantin Belousov 		vm = vmspace_acquire_ref(p);
14976bed074cSKonstantin Belousov 		if (vm == NULL) {
14986bed074cSKonstantin Belousov 			PROC_UNLOCK(p);
14996bed074cSKonstantin Belousov 			continue;
15006bed074cSKonstantin Belousov 		}
15016bed074cSKonstantin Belousov 		if (!vm_map_trylock_read(&vm->vm_map)) {
15026bed074cSKonstantin Belousov 			vmspace_free(vm);
150372d97679SDavid Schultz 			PROC_UNLOCK(p);
150472d97679SDavid Schultz 			continue;
150572d97679SDavid Schultz 		}
15067981aa24SKonstantin Belousov 		size = vmspace_swap_count(vm);
15076bed074cSKonstantin Belousov 		vm_map_unlock_read(&vm->vm_map);
15082025d69bSKonstantin Belousov 		if (shortage == VM_OOM_MEM)
15096bed074cSKonstantin Belousov 			size += vmspace_resident_count(vm);
15106bed074cSKonstantin Belousov 		vmspace_free(vm);
15115663e6deSDavid Greenman 		/*
15125663e6deSDavid Greenman 		 * if the this process is bigger than the biggest one
15135663e6deSDavid Greenman 		 * remember it.
15145663e6deSDavid Greenman 		 */
15155663e6deSDavid Greenman 		if (size > bigsize) {
15161c58e4e5SJohn Baldwin 			if (bigproc != NULL)
15171c58e4e5SJohn Baldwin 				PROC_UNLOCK(bigproc);
15185663e6deSDavid Greenman 			bigproc = p;
15195663e6deSDavid Greenman 			bigsize = size;
15201c58e4e5SJohn Baldwin 		} else
15211c58e4e5SJohn Baldwin 			PROC_UNLOCK(p);
15225663e6deSDavid Greenman 	}
15231005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
15245663e6deSDavid Greenman 	if (bigproc != NULL) {
1525729b1e51SDavid Greenman 		killproc(bigproc, "out of swap space");
1526fa885116SJulian Elischer 		sched_nice(bigproc, PRIO_MIN);
15271c58e4e5SJohn Baldwin 		PROC_UNLOCK(bigproc);
15282feb50bfSAttilio Rao 		wakeup(&cnt.v_free_count);
15295663e6deSDavid Greenman 	}
15305663e6deSDavid Greenman }
153126f9a767SRodney W. Grimes 
1532dc2efb27SJohn Dyson /*
1533dc2efb27SJohn Dyson  * This routine tries to maintain the pseudo LRU active queue,
1534dc2efb27SJohn Dyson  * so that during long periods of time where there is no paging,
1535956f3135SPhilippe Charnier  * that some statistic accumulation still occurs.  This code
1536dc2efb27SJohn Dyson  * helps the situation where paging just starts to occur.
1537dc2efb27SJohn Dyson  */
1538dc2efb27SJohn Dyson static void
1539dc2efb27SJohn Dyson vm_pageout_page_stats()
1540dc2efb27SJohn Dyson {
1541b86e6ec0SAlan Cox 	vm_object_t object;
1542dc2efb27SJohn Dyson 	vm_page_t m,next;
1543dc2efb27SJohn Dyson 	int pcount,tpcount;		/* Number of pages to check */
1544dc2efb27SJohn Dyson 	static int fullintervalcount = 0;
1545bef608bdSJohn Dyson 	int page_shortage;
1546bef608bdSJohn Dyson 
154790ecac61SMatthew Dillon 	page_shortage =
15482feb50bfSAttilio Rao 	    (cnt.v_inactive_target + cnt.v_cache_max + cnt.v_free_min) -
15492feb50bfSAttilio Rao 	    (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count);
155090ecac61SMatthew Dillon 
1551bef608bdSJohn Dyson 	if (page_shortage <= 0)
1552bef608bdSJohn Dyson 		return;
1553dc2efb27SJohn Dyson 
1554cdaba1f2SAlan Cox 	vm_page_lock_queues();
15552feb50bfSAttilio Rao 	pcount = cnt.v_active_count;
1556dc2efb27SJohn Dyson 	fullintervalcount += vm_pageout_stats_interval;
1557dc2efb27SJohn Dyson 	if (fullintervalcount < vm_pageout_full_stats_interval) {
15588d28bf04SAlan Cox 		tpcount = (int64_t)vm_pageout_stats_max * cnt.v_active_count /
15598d28bf04SAlan Cox 		    cnt.v_page_count;
1560dc2efb27SJohn Dyson 		if (pcount > tpcount)
1561dc2efb27SJohn Dyson 			pcount = tpcount;
1562883f3caaSMatthew Dillon 	} else {
1563883f3caaSMatthew Dillon 		fullintervalcount = 0;
1564dc2efb27SJohn Dyson 	}
1565dc2efb27SJohn Dyson 
1566be72f788SAlan Cox 	m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
1567dc2efb27SJohn Dyson 	while ((m != NULL) && (pcount-- > 0)) {
15687e006499SJohn Dyson 		int actcount;
1569dc2efb27SJohn Dyson 
15709cf51988SAlan Cox 		KASSERT(m->queue == PQ_ACTIVE,
1571ab42316cSAlan Cox 		    ("vm_pageout_page_stats: page %p isn't active", m));
1572dc2efb27SJohn Dyson 
1573dc2efb27SJohn Dyson 		next = TAILQ_NEXT(m, pageq);
15748dbca793STor Egge 		if ((m->flags & PG_MARKER) != 0) {
15758dbca793STor Egge 			m = next;
15768dbca793STor Egge 			continue;
15778dbca793STor Egge 		}
15782965a453SKip Macy 		vm_page_lock_assert(m, MA_NOTOWNED);
15799ee2165fSAlan Cox 		if (!vm_pageout_page_lock(m, &next)) {
15808c616246SKonstantin Belousov 			vm_page_unlock(m);
15812965a453SKip Macy 			m = next;
15822965a453SKip Macy 			continue;
15832965a453SKip Macy 		}
15849ee2165fSAlan Cox 		object = m->object;
15858dbca793STor Egge 		if (!VM_OBJECT_TRYLOCK(object) &&
15868dbca793STor Egge 		    !vm_pageout_fallback_object_lock(m, &next)) {
15878dbca793STor Egge 			VM_OBJECT_UNLOCK(object);
15882965a453SKip Macy 			vm_page_unlock(m);
1589b86e6ec0SAlan Cox 			m = next;
1590b86e6ec0SAlan Cox 			continue;
1591b86e6ec0SAlan Cox 		}
1592b86e6ec0SAlan Cox 
1593dc2efb27SJohn Dyson 		/*
1594dc2efb27SJohn Dyson 		 * Don't deactivate pages that are busy.
1595dc2efb27SJohn Dyson 		 */
1596dc2efb27SJohn Dyson 		if ((m->busy != 0) ||
15979af80719SAlan Cox 		    (m->oflags & VPO_BUSY) ||
1598dc2efb27SJohn Dyson 		    (m->hold_count != 0)) {
15992965a453SKip Macy 			vm_page_unlock(m);
1600b86e6ec0SAlan Cox 			VM_OBJECT_UNLOCK(object);
1601e5b006ffSAlan Cox 			vm_page_requeue(m);
1602dc2efb27SJohn Dyson 			m = next;
1603dc2efb27SJohn Dyson 			continue;
1604dc2efb27SJohn Dyson 		}
1605dc2efb27SJohn Dyson 
16067e006499SJohn Dyson 		actcount = 0;
16073407fefeSKonstantin Belousov 		if (m->aflags & PGA_REFERENCED) {
16083407fefeSKonstantin Belousov 			vm_page_aflag_clear(m, PGA_REFERENCED);
16097e006499SJohn Dyson 			actcount += 1;
1610dc2efb27SJohn Dyson 		}
1611dc2efb27SJohn Dyson 
16120385347cSPeter Wemm 		actcount += pmap_ts_referenced(m);
16137e006499SJohn Dyson 		if (actcount) {
16147e006499SJohn Dyson 			m->act_count += ACT_ADVANCE + actcount;
1615dc2efb27SJohn Dyson 			if (m->act_count > ACT_MAX)
1616dc2efb27SJohn Dyson 				m->act_count = ACT_MAX;
1617e5b006ffSAlan Cox 			vm_page_requeue(m);
1618dc2efb27SJohn Dyson 		} else {
1619dc2efb27SJohn Dyson 			if (m->act_count == 0) {
16207e006499SJohn Dyson 				/*
16212b6b0df7SMatthew Dillon 				 * We turn off page access, so that we have
16222b6b0df7SMatthew Dillon 				 * more accurate RSS stats.  We don't do this
16232b6b0df7SMatthew Dillon 				 * in the normal page deactivation when the
16242b6b0df7SMatthew Dillon 				 * system is loaded VM wise, because the
16252b6b0df7SMatthew Dillon 				 * cost of the large number of page protect
16262b6b0df7SMatthew Dillon 				 * operations would be higher than the value
16272b6b0df7SMatthew Dillon 				 * of doing the operation.
16287e006499SJohn Dyson 				 */
16294fec79beSAlan Cox 				pmap_remove_all(m);
1630dc2efb27SJohn Dyson 				vm_page_deactivate(m);
1631dc2efb27SJohn Dyson 			} else {
1632dc2efb27SJohn Dyson 				m->act_count -= min(m->act_count, ACT_DECLINE);
1633e5b006ffSAlan Cox 				vm_page_requeue(m);
1634dc2efb27SJohn Dyson 			}
1635dc2efb27SJohn Dyson 		}
16362965a453SKip Macy 		vm_page_unlock(m);
1637b86e6ec0SAlan Cox 		VM_OBJECT_UNLOCK(object);
1638dc2efb27SJohn Dyson 		m = next;
1639dc2efb27SJohn Dyson 	}
1640cdaba1f2SAlan Cox 	vm_page_unlock_queues();
1641dc2efb27SJohn Dyson }
1642dc2efb27SJohn Dyson 
1643df8bae1dSRodney W. Grimes /*
1644df8bae1dSRodney W. Grimes  *	vm_pageout is the high level pageout daemon.
1645df8bae1dSRodney W. Grimes  */
16462b14f991SJulian Elischer static void
164726f9a767SRodney W. Grimes vm_pageout()
1648df8bae1dSRodney W. Grimes {
16491aab16a6SAlan Cox 	int error, pass;
16500384fff8SJason Evans 
1651df8bae1dSRodney W. Grimes 	/*
1652df8bae1dSRodney W. Grimes 	 * Initialize some paging parameters.
1653df8bae1dSRodney W. Grimes 	 */
16542feb50bfSAttilio Rao 	cnt.v_interrupt_free_min = 2;
16552feb50bfSAttilio Rao 	if (cnt.v_page_count < 2000)
1656f35329acSJohn Dyson 		vm_pageout_page_count = 8;
1657f6b04d2bSDavid Greenman 
165845ae1d91SAlan Cox 	/*
165945ae1d91SAlan Cox 	 * v_free_reserved needs to include enough for the largest
166045ae1d91SAlan Cox 	 * swap pager structures plus enough for any pv_entry structs
166145ae1d91SAlan Cox 	 * when paging.
166245ae1d91SAlan Cox 	 */
16632feb50bfSAttilio Rao 	if (cnt.v_page_count > 1024)
16642feb50bfSAttilio Rao 		cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200;
16652feb50bfSAttilio Rao 	else
16662feb50bfSAttilio Rao 		cnt.v_free_min = 4;
16672feb50bfSAttilio Rao 	cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE +
16682feb50bfSAttilio Rao 	    cnt.v_interrupt_free_min;
16692feb50bfSAttilio Rao 	cnt.v_free_reserved = vm_pageout_page_count +
16702446e4f0SAlan Cox 	    cnt.v_pageout_free_min + (cnt.v_page_count / 768);
16712feb50bfSAttilio Rao 	cnt.v_free_severe = cnt.v_free_min / 2;
16722feb50bfSAttilio Rao 	cnt.v_free_min += cnt.v_free_reserved;
16732feb50bfSAttilio Rao 	cnt.v_free_severe += cnt.v_free_reserved;
167445ae1d91SAlan Cox 
1675ed74321bSDavid Greenman 	/*
16762b6b0df7SMatthew Dillon 	 * v_free_target and v_cache_min control pageout hysteresis.  Note
16772b6b0df7SMatthew Dillon 	 * that these are more a measure of the VM cache queue hysteresis
16782b6b0df7SMatthew Dillon 	 * then the VM free queue.  Specifically, v_free_target is the
16792b6b0df7SMatthew Dillon 	 * high water mark (free+cache pages).
16802b6b0df7SMatthew Dillon 	 *
16812b6b0df7SMatthew Dillon 	 * v_free_reserved + v_cache_min (mostly means v_cache_min) is the
16822b6b0df7SMatthew Dillon 	 * low water mark, while v_free_min is the stop.  v_cache_min must
16832b6b0df7SMatthew Dillon 	 * be big enough to handle memory needs while the pageout daemon
16842b6b0df7SMatthew Dillon 	 * is signalled and run to free more pages.
1685ed74321bSDavid Greenman 	 */
16862feb50bfSAttilio Rao 	if (cnt.v_free_count > 6144)
16872feb50bfSAttilio Rao 		cnt.v_free_target = 4 * cnt.v_free_min + cnt.v_free_reserved;
16882feb50bfSAttilio Rao 	else
16892feb50bfSAttilio Rao 		cnt.v_free_target = 2 * cnt.v_free_min + cnt.v_free_reserved;
16906f2b142eSDavid Greenman 
16912feb50bfSAttilio Rao 	if (cnt.v_free_count > 2048) {
16922feb50bfSAttilio Rao 		cnt.v_cache_min = cnt.v_free_target;
16932feb50bfSAttilio Rao 		cnt.v_cache_max = 2 * cnt.v_cache_min;
16942feb50bfSAttilio Rao 		cnt.v_inactive_target = (3 * cnt.v_free_target) / 2;
16950d94caffSDavid Greenman 	} else {
16962feb50bfSAttilio Rao 		cnt.v_cache_min = 0;
16972feb50bfSAttilio Rao 		cnt.v_cache_max = 0;
16982feb50bfSAttilio Rao 		cnt.v_inactive_target = cnt.v_free_count / 4;
16990d94caffSDavid Greenman 	}
17002feb50bfSAttilio Rao 	if (cnt.v_inactive_target > cnt.v_free_count / 3)
17012feb50bfSAttilio Rao 		cnt.v_inactive_target = cnt.v_free_count / 3;
1702df8bae1dSRodney W. Grimes 
1703df8bae1dSRodney W. Grimes 	/* XXX does not really belong here */
1704df8bae1dSRodney W. Grimes 	if (vm_page_max_wired == 0)
17052feb50bfSAttilio Rao 		vm_page_max_wired = cnt.v_free_count / 3;
1706df8bae1dSRodney W. Grimes 
1707dc2efb27SJohn Dyson 	if (vm_pageout_stats_max == 0)
17082feb50bfSAttilio Rao 		vm_pageout_stats_max = cnt.v_free_target;
1709dc2efb27SJohn Dyson 
1710dc2efb27SJohn Dyson 	/*
1711dc2efb27SJohn Dyson 	 * Set interval in seconds for stats scan.
1712dc2efb27SJohn Dyson 	 */
1713dc2efb27SJohn Dyson 	if (vm_pageout_stats_interval == 0)
1714bef608bdSJohn Dyson 		vm_pageout_stats_interval = 5;
1715dc2efb27SJohn Dyson 	if (vm_pageout_full_stats_interval == 0)
1716dc2efb27SJohn Dyson 		vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4;
1717dc2efb27SJohn Dyson 
171824a1cce3SDavid Greenman 	swap_pager_swap_init();
17192b6b0df7SMatthew Dillon 	pass = 0;
1720df8bae1dSRodney W. Grimes 	/*
17210d94caffSDavid Greenman 	 * The pageout daemon is never done, so loop forever.
1722df8bae1dSRodney W. Grimes 	 */
1723df8bae1dSRodney W. Grimes 	while (TRUE) {
1724936524aaSMatthew Dillon 		/*
1725936524aaSMatthew Dillon 		 * If we have enough free memory, wakeup waiters.  Do
1726936524aaSMatthew Dillon 		 * not clear vm_pages_needed until we reach our target,
1727936524aaSMatthew Dillon 		 * otherwise we may be woken up over and over again and
1728936524aaSMatthew Dillon 		 * waste a lot of cpu.
1729936524aaSMatthew Dillon 		 */
1730e9f995d8SAlan Cox 		mtx_lock(&vm_page_queue_free_mtx);
1731936524aaSMatthew Dillon 		if (vm_pages_needed && !vm_page_count_min()) {
1732a1c0a785SAlan Cox 			if (!vm_paging_needed())
1733936524aaSMatthew Dillon 				vm_pages_needed = 0;
17342feb50bfSAttilio Rao 			wakeup(&cnt.v_free_count);
1735936524aaSMatthew Dillon 		}
1736936524aaSMatthew Dillon 		if (vm_pages_needed) {
173790ecac61SMatthew Dillon 			/*
17382b6b0df7SMatthew Dillon 			 * Still not done, take a second pass without waiting
17392b6b0df7SMatthew Dillon 			 * (unlimited dirty cleaning), otherwise sleep a bit
17402b6b0df7SMatthew Dillon 			 * and try again.
174190ecac61SMatthew Dillon 			 */
17422b6b0df7SMatthew Dillon 			++pass;
17432b6b0df7SMatthew Dillon 			if (pass > 1)
1744e9f995d8SAlan Cox 				msleep(&vm_pages_needed,
1745e9f995d8SAlan Cox 				    &vm_page_queue_free_mtx, PVM, "psleep",
1746e9f995d8SAlan Cox 				    hz / 2);
174790ecac61SMatthew Dillon 		} else {
174890ecac61SMatthew Dillon 			/*
17492b6b0df7SMatthew Dillon 			 * Good enough, sleep & handle stats.  Prime the pass
17502b6b0df7SMatthew Dillon 			 * for the next run.
175190ecac61SMatthew Dillon 			 */
17522b6b0df7SMatthew Dillon 			if (pass > 1)
17532b6b0df7SMatthew Dillon 				pass = 1;
17542b6b0df7SMatthew Dillon 			else
17552b6b0df7SMatthew Dillon 				pass = 0;
1756e9f995d8SAlan Cox 			error = msleep(&vm_pages_needed,
1757e9f995d8SAlan Cox 			    &vm_page_queue_free_mtx, PVM, "psleep",
1758e9f995d8SAlan Cox 			    vm_pageout_stats_interval * hz);
1759dc2efb27SJohn Dyson 			if (error && !vm_pages_needed) {
1760e9f995d8SAlan Cox 				mtx_unlock(&vm_page_queue_free_mtx);
17612b6b0df7SMatthew Dillon 				pass = 0;
1762dc2efb27SJohn Dyson 				vm_pageout_page_stats();
1763dc2efb27SJohn Dyson 				continue;
1764dc2efb27SJohn Dyson 			}
1765f919ebdeSDavid Greenman 		}
1766b18bfc3dSJohn Dyson 		if (vm_pages_needed)
1767393a081dSAttilio Rao 			cnt.v_pdwakeups++;
1768e9f995d8SAlan Cox 		mtx_unlock(&vm_page_queue_free_mtx);
17692b6b0df7SMatthew Dillon 		vm_pageout_scan(pass);
1770df8bae1dSRodney W. Grimes 	}
1771df8bae1dSRodney W. Grimes }
177226f9a767SRodney W. Grimes 
17736b4b77adSAlan Cox /*
1774e9f995d8SAlan Cox  * Unless the free page queue lock is held by the caller, this function
17756b4b77adSAlan Cox  * should be regarded as advisory.  Specifically, the caller should
17766b4b77adSAlan Cox  * not msleep() on &cnt.v_free_count following this function unless
1777e9f995d8SAlan Cox  * the free page queue lock is held until the msleep() is performed.
17786b4b77adSAlan Cox  */
1779e0c5a895SJohn Dyson void
1780e0c5a895SJohn Dyson pagedaemon_wakeup()
1781e0c5a895SJohn Dyson {
1782a1c0a785SAlan Cox 
1783b40ce416SJulian Elischer 	if (!vm_pages_needed && curthread->td_proc != pageproc) {
1784a1c0a785SAlan Cox 		vm_pages_needed = 1;
1785e0c5a895SJohn Dyson 		wakeup(&vm_pages_needed);
1786e0c5a895SJohn Dyson 	}
1787e0c5a895SJohn Dyson }
1788e0c5a895SJohn Dyson 
178938efa82bSJohn Dyson #if !defined(NO_SWAPPING)
17905afce282SDavid Greenman static void
179197824da3SAlan Cox vm_req_vmdaemon(int req)
17925afce282SDavid Greenman {
17935afce282SDavid Greenman 	static int lastrun = 0;
17945afce282SDavid Greenman 
179597824da3SAlan Cox 	mtx_lock(&vm_daemon_mtx);
179697824da3SAlan Cox 	vm_pageout_req_swapout |= req;
1797b18bfc3dSJohn Dyson 	if ((ticks > (lastrun + hz)) || (ticks < lastrun)) {
17985afce282SDavid Greenman 		wakeup(&vm_daemon_needed);
17995afce282SDavid Greenman 		lastrun = ticks;
18005afce282SDavid Greenman 	}
180197824da3SAlan Cox 	mtx_unlock(&vm_daemon_mtx);
18025afce282SDavid Greenman }
18035afce282SDavid Greenman 
18042b14f991SJulian Elischer static void
18054f9fb771SBruce Evans vm_daemon()
18060d94caffSDavid Greenman {
180791d5354aSJohn Baldwin 	struct rlimit rsslim;
1808dcbcd518SBruce Evans 	struct proc *p;
1809dcbcd518SBruce Evans 	struct thread *td;
18106bed074cSKonstantin Belousov 	struct vmspace *vm;
1811099e7e95SEdward Tomasz Napierala 	int breakout, swapout_flags, tryagain, attempts;
1812afcc55f3SEdward Tomasz Napierala #ifdef RACCT
1813099e7e95SEdward Tomasz Napierala 	uint64_t rsize, ravailable;
1814afcc55f3SEdward Tomasz Napierala #endif
18150d94caffSDavid Greenman 
18162fe6e4d7SDavid Greenman 	while (TRUE) {
181797824da3SAlan Cox 		mtx_lock(&vm_daemon_mtx);
1818099e7e95SEdward Tomasz Napierala #ifdef RACCT
1819099e7e95SEdward Tomasz Napierala 		msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", hz);
1820099e7e95SEdward Tomasz Napierala #else
182197824da3SAlan Cox 		msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", 0);
1822099e7e95SEdward Tomasz Napierala #endif
182397824da3SAlan Cox 		swapout_flags = vm_pageout_req_swapout;
18244c1f8ee9SDavid Greenman 		vm_pageout_req_swapout = 0;
182597824da3SAlan Cox 		mtx_unlock(&vm_daemon_mtx);
182697824da3SAlan Cox 		if (swapout_flags)
182797824da3SAlan Cox 			swapout_procs(swapout_flags);
182897824da3SAlan Cox 
18292fe6e4d7SDavid Greenman 		/*
18300d94caffSDavid Greenman 		 * scan the processes for exceeding their rlimits or if
18310d94caffSDavid Greenman 		 * process is swapped out -- deactivate pages
18322fe6e4d7SDavid Greenman 		 */
1833099e7e95SEdward Tomasz Napierala 		tryagain = 0;
1834099e7e95SEdward Tomasz Napierala 		attempts = 0;
1835099e7e95SEdward Tomasz Napierala again:
1836099e7e95SEdward Tomasz Napierala 		attempts++;
18371005a129SJohn Baldwin 		sx_slock(&allproc_lock);
1838f67af5c9SXin LI 		FOREACH_PROC_IN_SYSTEM(p) {
1839fe2144fdSLuoqi Chen 			vm_pindex_t limit, size;
18402fe6e4d7SDavid Greenman 
18412fe6e4d7SDavid Greenman 			/*
18422fe6e4d7SDavid Greenman 			 * if this is a system process or if we have already
18432fe6e4d7SDavid Greenman 			 * looked at this process, skip it.
18442fe6e4d7SDavid Greenman 			 */
1845897ecacdSJohn Baldwin 			PROC_LOCK(p);
18468e6fa660SJohn Baldwin 			if (p->p_state != PRS_NORMAL ||
18478e6fa660SJohn Baldwin 			    p->p_flag & (P_INEXEC | P_SYSTEM | P_WEXIT)) {
1848897ecacdSJohn Baldwin 				PROC_UNLOCK(p);
18492fe6e4d7SDavid Greenman 				continue;
18502fe6e4d7SDavid Greenman 			}
18512fe6e4d7SDavid Greenman 			/*
18522fe6e4d7SDavid Greenman 			 * if the process is in a non-running type state,
18532fe6e4d7SDavid Greenman 			 * don't touch it.
18542fe6e4d7SDavid Greenman 			 */
1855e602ba25SJulian Elischer 			breakout = 0;
1856e602ba25SJulian Elischer 			FOREACH_THREAD_IN_PROC(p, td) {
1857982d11f8SJeff Roberson 				thread_lock(td);
185871fad9fdSJulian Elischer 				if (!TD_ON_RUNQ(td) &&
185971fad9fdSJulian Elischer 				    !TD_IS_RUNNING(td) &&
1860f497cda2SEdward Tomasz Napierala 				    !TD_IS_SLEEPING(td) &&
1861f497cda2SEdward Tomasz Napierala 				    !TD_IS_SUSPENDED(td)) {
1862982d11f8SJeff Roberson 					thread_unlock(td);
1863e602ba25SJulian Elischer 					breakout = 1;
1864e602ba25SJulian Elischer 					break;
1865e602ba25SJulian Elischer 				}
1866982d11f8SJeff Roberson 				thread_unlock(td);
1867e602ba25SJulian Elischer 			}
1868897ecacdSJohn Baldwin 			if (breakout) {
1869897ecacdSJohn Baldwin 				PROC_UNLOCK(p);
18702fe6e4d7SDavid Greenman 				continue;
18712fe6e4d7SDavid Greenman 			}
18722fe6e4d7SDavid Greenman 			/*
18732fe6e4d7SDavid Greenman 			 * get a limit
18742fe6e4d7SDavid Greenman 			 */
1875dcbcd518SBruce Evans 			lim_rlimit(p, RLIMIT_RSS, &rsslim);
1876fe2144fdSLuoqi Chen 			limit = OFF_TO_IDX(
187791d5354aSJohn Baldwin 			    qmin(rsslim.rlim_cur, rsslim.rlim_max));
18782fe6e4d7SDavid Greenman 
18792fe6e4d7SDavid Greenman 			/*
18800d94caffSDavid Greenman 			 * let processes that are swapped out really be
18810d94caffSDavid Greenman 			 * swapped out set the limit to nothing (will force a
18820d94caffSDavid Greenman 			 * swap-out.)
18832fe6e4d7SDavid Greenman 			 */
1884b61ce5b0SJeff Roberson 			if ((p->p_flag & P_INMEM) == 0)
18850d94caffSDavid Greenman 				limit = 0;	/* XXX */
18866bed074cSKonstantin Belousov 			vm = vmspace_acquire_ref(p);
1887897ecacdSJohn Baldwin 			PROC_UNLOCK(p);
18886bed074cSKonstantin Belousov 			if (vm == NULL)
18896bed074cSKonstantin Belousov 				continue;
18902fe6e4d7SDavid Greenman 
18916bed074cSKonstantin Belousov 			size = vmspace_resident_count(vm);
18922fe6e4d7SDavid Greenman 			if (limit >= 0 && size >= limit) {
1893fe2144fdSLuoqi Chen 				vm_pageout_map_deactivate_pages(
18946bed074cSKonstantin Belousov 				    &vm->vm_map, limit);
18952fe6e4d7SDavid Greenman 			}
1896afcc55f3SEdward Tomasz Napierala #ifdef RACCT
1897099e7e95SEdward Tomasz Napierala 			rsize = IDX_TO_OFF(size);
1898099e7e95SEdward Tomasz Napierala 			PROC_LOCK(p);
1899099e7e95SEdward Tomasz Napierala 			racct_set(p, RACCT_RSS, rsize);
1900099e7e95SEdward Tomasz Napierala 			ravailable = racct_get_available(p, RACCT_RSS);
1901099e7e95SEdward Tomasz Napierala 			PROC_UNLOCK(p);
1902099e7e95SEdward Tomasz Napierala 			if (rsize > ravailable) {
1903099e7e95SEdward Tomasz Napierala 				/*
1904099e7e95SEdward Tomasz Napierala 				 * Don't be overly aggressive; this might be
1905099e7e95SEdward Tomasz Napierala 				 * an innocent process, and the limit could've
1906099e7e95SEdward Tomasz Napierala 				 * been exceeded by some memory hog.  Don't
1907099e7e95SEdward Tomasz Napierala 				 * try to deactivate more than 1/4th of process'
1908099e7e95SEdward Tomasz Napierala 				 * resident set size.
1909099e7e95SEdward Tomasz Napierala 				 */
1910099e7e95SEdward Tomasz Napierala 				if (attempts <= 8) {
1911099e7e95SEdward Tomasz Napierala 					if (ravailable < rsize - (rsize / 4))
1912099e7e95SEdward Tomasz Napierala 						ravailable = rsize - (rsize / 4);
1913099e7e95SEdward Tomasz Napierala 				}
1914099e7e95SEdward Tomasz Napierala 				vm_pageout_map_deactivate_pages(
1915099e7e95SEdward Tomasz Napierala 				    &vm->vm_map, OFF_TO_IDX(ravailable));
1916099e7e95SEdward Tomasz Napierala 				/* Update RSS usage after paging out. */
1917099e7e95SEdward Tomasz Napierala 				size = vmspace_resident_count(vm);
1918099e7e95SEdward Tomasz Napierala 				rsize = IDX_TO_OFF(size);
1919099e7e95SEdward Tomasz Napierala 				PROC_LOCK(p);
1920099e7e95SEdward Tomasz Napierala 				racct_set(p, RACCT_RSS, rsize);
1921099e7e95SEdward Tomasz Napierala 				PROC_UNLOCK(p);
1922099e7e95SEdward Tomasz Napierala 				if (rsize > ravailable)
1923099e7e95SEdward Tomasz Napierala 					tryagain = 1;
1924099e7e95SEdward Tomasz Napierala 			}
1925afcc55f3SEdward Tomasz Napierala #endif
19266bed074cSKonstantin Belousov 			vmspace_free(vm);
19272fe6e4d7SDavid Greenman 		}
19281005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
1929099e7e95SEdward Tomasz Napierala 		if (tryagain != 0 && attempts <= 10)
1930099e7e95SEdward Tomasz Napierala 			goto again;
193124a1cce3SDavid Greenman 	}
19322fe6e4d7SDavid Greenman }
1933a1287949SEivind Eklund #endif			/* !defined(NO_SWAPPING) */
1934