xref: /freebsd/sys/vm/vm_pageout.c (revision 1e8a675c73702efc66d49e1f40c1b957f5d61549)
160727d8bSWarner Losh /*-
226f9a767SRodney W. Grimes  * Copyright (c) 1991 Regents of the University of California.
326f9a767SRodney W. Grimes  * All rights reserved.
426f9a767SRodney W. Grimes  * Copyright (c) 1994 John S. Dyson
526f9a767SRodney W. Grimes  * All rights reserved.
626f9a767SRodney W. Grimes  * Copyright (c) 1994 David Greenman
726f9a767SRodney W. Grimes  * All rights reserved.
88dbca793STor Egge  * Copyright (c) 2005 Yahoo! Technologies Norway AS
98dbca793STor Egge  * All rights reserved.
10df8bae1dSRodney W. Grimes  *
11df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
12df8bae1dSRodney W. Grimes  * The Mach Operating System project at Carnegie-Mellon University.
13df8bae1dSRodney W. Grimes  *
14df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
15df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
16df8bae1dSRodney W. Grimes  * are met:
17df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
18df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
19df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
20df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
21df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
22df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
235929bcfaSPhilippe Charnier  *    must display the following acknowledgement:
24df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
25df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
26df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
27df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
28df8bae1dSRodney W. Grimes  *    without specific prior written permission.
29df8bae1dSRodney W. Grimes  *
30df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
41df8bae1dSRodney W. Grimes  *
423c4dd356SDavid Greenman  *	from: @(#)vm_pageout.c	7.4 (Berkeley) 5/7/91
43df8bae1dSRodney W. Grimes  *
44df8bae1dSRodney W. Grimes  *
45df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
46df8bae1dSRodney W. Grimes  * All rights reserved.
47df8bae1dSRodney W. Grimes  *
48df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
49df8bae1dSRodney W. Grimes  *
50df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
51df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
52df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
53df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
54df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
55df8bae1dSRodney W. Grimes  *
56df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
57df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
58df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
59df8bae1dSRodney W. Grimes  *
60df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
61df8bae1dSRodney W. Grimes  *
62df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
63df8bae1dSRodney W. Grimes  *  School of Computer Science
64df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
65df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
66df8bae1dSRodney W. Grimes  *
67df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
68df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
69df8bae1dSRodney W. Grimes  */
70df8bae1dSRodney W. Grimes 
71df8bae1dSRodney W. Grimes /*
72df8bae1dSRodney W. Grimes  *	The proverbial page-out daemon.
73df8bae1dSRodney W. Grimes  */
74df8bae1dSRodney W. Grimes 
75874651b1SDavid E. O'Brien #include <sys/cdefs.h>
76874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
77874651b1SDavid E. O'Brien 
78faa5f8d8SAndrzej Bialecki #include "opt_vm.h"
79df8bae1dSRodney W. Grimes #include <sys/param.h>
8026f9a767SRodney W. Grimes #include <sys/systm.h>
81b5e8ce9fSBruce Evans #include <sys/kernel.h>
82855a310fSJeff Roberson #include <sys/eventhandler.h>
83fb919e4dSMark Murray #include <sys/lock.h>
84fb919e4dSMark Murray #include <sys/mutex.h>
8526f9a767SRodney W. Grimes #include <sys/proc.h>
869c8b8baaSPeter Wemm #include <sys/kthread.h>
870384fff8SJason Evans #include <sys/ktr.h>
8897824da3SAlan Cox #include <sys/mount.h>
8926f9a767SRodney W. Grimes #include <sys/resourcevar.h>
90b43179fbSJeff Roberson #include <sys/sched.h>
91d2fc5315SPoul-Henning Kamp #include <sys/signalvar.h>
92f6b04d2bSDavid Greenman #include <sys/vnode.h>
93efeaf95aSDavid Greenman #include <sys/vmmeter.h>
941005a129SJohn Baldwin #include <sys/sx.h>
9538efa82bSJohn Dyson #include <sys/sysctl.h>
96df8bae1dSRodney W. Grimes 
97df8bae1dSRodney W. Grimes #include <vm/vm.h>
98efeaf95aSDavid Greenman #include <vm/vm_param.h>
99efeaf95aSDavid Greenman #include <vm/vm_object.h>
100df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
101efeaf95aSDavid Greenman #include <vm/vm_map.h>
102df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
10324a1cce3SDavid Greenman #include <vm/vm_pager.h>
10405f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h>
105efeaf95aSDavid Greenman #include <vm/vm_extern.h>
106670d17b5SJeff Roberson #include <vm/uma.h>
107df8bae1dSRodney W. Grimes 
1082b14f991SJulian Elischer /*
1092b14f991SJulian Elischer  * System initialization
1102b14f991SJulian Elischer  */
1112b14f991SJulian Elischer 
1122b14f991SJulian Elischer /* the kernel process "vm_pageout"*/
11311caded3SAlfred Perlstein static void vm_pageout(void);
11411caded3SAlfred Perlstein static int vm_pageout_clean(vm_page_t);
11511caded3SAlfred Perlstein static void vm_pageout_scan(int pass);
11645ae1d91SAlan Cox 
1172b14f991SJulian Elischer struct proc *pageproc;
1182b14f991SJulian Elischer 
1192b14f991SJulian Elischer static struct kproc_desc page_kp = {
1202b14f991SJulian Elischer 	"pagedaemon",
1212b14f991SJulian Elischer 	vm_pageout,
1222b14f991SJulian Elischer 	&pageproc
1232b14f991SJulian Elischer };
124237fdd78SRobert Watson SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start,
125237fdd78SRobert Watson     &page_kp);
1262b14f991SJulian Elischer 
12738efa82bSJohn Dyson #if !defined(NO_SWAPPING)
1282b14f991SJulian Elischer /* the kernel process "vm_daemon"*/
12911caded3SAlfred Perlstein static void vm_daemon(void);
130f708ef1bSPoul-Henning Kamp static struct	proc *vmproc;
1312b14f991SJulian Elischer 
1322b14f991SJulian Elischer static struct kproc_desc vm_kp = {
1332b14f991SJulian Elischer 	"vmdaemon",
1342b14f991SJulian Elischer 	vm_daemon,
1352b14f991SJulian Elischer 	&vmproc
1362b14f991SJulian Elischer };
137237fdd78SRobert Watson SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp);
13838efa82bSJohn Dyson #endif
1392b14f991SJulian Elischer 
1402b14f991SJulian Elischer 
1418b245767SAlan Cox int vm_pages_needed;		/* Event on which pageout daemon sleeps */
1428b245767SAlan Cox int vm_pageout_deficit;		/* Estimated number of pages deficit */
1438b245767SAlan Cox int vm_pageout_pages_needed;	/* flag saying that the pageout daemon needs pages */
14426f9a767SRodney W. Grimes 
14538efa82bSJohn Dyson #if !defined(NO_SWAPPING)
146f708ef1bSPoul-Henning Kamp static int vm_pageout_req_swapout;	/* XXX */
147f708ef1bSPoul-Henning Kamp static int vm_daemon_needed;
14897824da3SAlan Cox static struct mtx vm_daemon_mtx;
14997824da3SAlan Cox /* Allow for use by vm_pageout before vm_daemon is initialized. */
15097824da3SAlan Cox MTX_SYSINIT(vm_daemon, &vm_daemon_mtx, "vm daemon", MTX_DEF);
15138efa82bSJohn Dyson #endif
1522b6b0df7SMatthew Dillon static int vm_max_launder = 32;
153303b270bSEivind Eklund static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0;
154303b270bSEivind Eklund static int vm_pageout_full_stats_interval = 0;
15526354d4cSAlan Cox static int vm_pageout_algorithm=0;
156303b270bSEivind Eklund static int defer_swap_pageouts=0;
157303b270bSEivind Eklund static int disable_swap_pageouts=0;
15870111b90SJohn Dyson 
15938efa82bSJohn Dyson #if defined(NO_SWAPPING)
160303b270bSEivind Eklund static int vm_swap_enabled=0;
161303b270bSEivind Eklund static int vm_swap_idle_enabled=0;
16238efa82bSJohn Dyson #else
163303b270bSEivind Eklund static int vm_swap_enabled=1;
164303b270bSEivind Eklund static int vm_swap_idle_enabled=0;
16538efa82bSJohn Dyson #endif
16638efa82bSJohn Dyson 
16738efa82bSJohn Dyson SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm,
1682b6b0df7SMatthew Dillon 	CTLFLAG_RW, &vm_pageout_algorithm, 0, "LRU page mgmt");
1692b6b0df7SMatthew Dillon 
1702b6b0df7SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, max_launder,
1712b6b0df7SMatthew Dillon 	CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout");
17238efa82bSJohn Dyson 
173dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max,
174b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length");
175dc2efb27SJohn Dyson 
176dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval,
177b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan");
178dc2efb27SJohn Dyson 
179dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval,
180b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan");
181dc2efb27SJohn Dyson 
18238efa82bSJohn Dyson #if defined(NO_SWAPPING)
183ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
1846bd9cb1cSTom Rhodes 	CTLFLAG_RD, &vm_swap_enabled, 0, "Enable entire process swapout");
185ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
1866bd9cb1cSTom Rhodes 	CTLFLAG_RD, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
18738efa82bSJohn Dyson #else
188ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
189b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout");
190ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
191b0359e2cSPeter Wemm 	CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
19238efa82bSJohn Dyson #endif
19326f9a767SRodney W. Grimes 
194ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts,
195b0359e2cSPeter Wemm 	CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem");
19612ac6a1dSJohn Dyson 
197ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts,
198b0359e2cSPeter Wemm 	CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages");
19912ac6a1dSJohn Dyson 
20023b59018SMatthew Dillon static int pageout_lock_miss;
20123b59018SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss,
20223b59018SMatthew Dillon 	CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout");
20323b59018SMatthew Dillon 
204ffc82b0aSJohn Dyson #define VM_PAGEOUT_PAGE_COUNT 16
205bbc0ec52SDavid Greenman int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT;
206df8bae1dSRodney W. Grimes 
207c3cb3e12SDavid Greenman int vm_page_max_wired;		/* XXX max # of wired pages system-wide */
2085dfc2870SAlan Cox SYSCTL_INT(_vm, OID_AUTO, max_wired,
2095dfc2870SAlan Cox 	CTLFLAG_RW, &vm_page_max_wired, 0, "System-wide limit to wired page count");
210df8bae1dSRodney W. Grimes 
21138efa82bSJohn Dyson #if !defined(NO_SWAPPING)
212ecf6279fSAlan Cox static void vm_pageout_map_deactivate_pages(vm_map_t, long);
213ecf6279fSAlan Cox static void vm_pageout_object_deactivate_pages(pmap_t, vm_object_t, long);
21497824da3SAlan Cox static void vm_req_vmdaemon(int req);
21538efa82bSJohn Dyson #endif
216dc2efb27SJohn Dyson static void vm_pageout_page_stats(void);
217cd41fc12SDavid Greenman 
2188c616246SKonstantin Belousov static void
2198c616246SKonstantin Belousov vm_pageout_init_marker(vm_page_t marker, u_short queue)
2208c616246SKonstantin Belousov {
2218c616246SKonstantin Belousov 
2228c616246SKonstantin Belousov 	bzero(marker, sizeof(*marker));
2238c616246SKonstantin Belousov 	marker->flags = PG_FICTITIOUS | PG_MARKER;
2248c616246SKonstantin Belousov 	marker->oflags = VPO_BUSY;
2258c616246SKonstantin Belousov 	marker->queue = queue;
2268c616246SKonstantin Belousov 	marker->wire_count = 1;
2278c616246SKonstantin Belousov }
2288c616246SKonstantin Belousov 
22926f9a767SRodney W. Grimes /*
2308dbca793STor Egge  * vm_pageout_fallback_object_lock:
2318dbca793STor Egge  *
2328dbca793STor Egge  * Lock vm object currently associated with `m'. VM_OBJECT_TRYLOCK is
2338dbca793STor Egge  * known to have failed and page queue must be either PQ_ACTIVE or
2348dbca793STor Egge  * PQ_INACTIVE.  To avoid lock order violation, unlock the page queues
2358dbca793STor Egge  * while locking the vm object.  Use marker page to detect page queue
2368dbca793STor Egge  * changes and maintain notion of next page on page queue.  Return
2378dbca793STor Egge  * TRUE if no changes were detected, FALSE otherwise.  vm object is
2388dbca793STor Egge  * locked on return.
2398dbca793STor Egge  *
2408dbca793STor Egge  * This function depends on both the lock portion of struct vm_object
2418dbca793STor Egge  * and normal struct vm_page being type stable.
2428dbca793STor Egge  */
243da31e3aaSAlan Cox boolean_t
2448dbca793STor Egge vm_pageout_fallback_object_lock(vm_page_t m, vm_page_t *next)
2458dbca793STor Egge {
2468dbca793STor Egge 	struct vm_page marker;
2478dbca793STor Egge 	boolean_t unchanged;
2488dbca793STor Egge 	u_short queue;
2498dbca793STor Egge 	vm_object_t object;
2508dbca793STor Egge 
2518dbca793STor Egge 	queue = m->queue;
2528c616246SKonstantin Belousov 	vm_pageout_init_marker(&marker, queue);
2538dbca793STor Egge 	object = m->object;
2548dbca793STor Egge 
2558dbca793STor Egge 	TAILQ_INSERT_AFTER(&vm_page_queues[queue].pl,
2568dbca793STor Egge 			   m, &marker, pageq);
2578dbca793STor Egge 	vm_page_unlock_queues();
2582965a453SKip Macy 	vm_page_unlock(m);
2598dbca793STor Egge 	VM_OBJECT_LOCK(object);
2602965a453SKip Macy 	vm_page_lock(m);
2618dbca793STor Egge 	vm_page_lock_queues();
2628dbca793STor Egge 
2638dbca793STor Egge 	/* Page queue might have changed. */
2648dbca793STor Egge 	*next = TAILQ_NEXT(&marker, pageq);
2658dbca793STor Egge 	unchanged = (m->queue == queue &&
2668dbca793STor Egge 		     m->object == object &&
2678dbca793STor Egge 		     &marker == TAILQ_NEXT(m, pageq));
2688dbca793STor Egge 	TAILQ_REMOVE(&vm_page_queues[queue].pl,
2698dbca793STor Egge 		     &marker, pageq);
2708dbca793STor Egge 	return (unchanged);
2718dbca793STor Egge }
2728dbca793STor Egge 
2738dbca793STor Egge /*
2748c616246SKonstantin Belousov  * Lock the page while holding the page queue lock.  Use marker page
2758c616246SKonstantin Belousov  * to detect page queue changes and maintain notion of next page on
2768c616246SKonstantin Belousov  * page queue.  Return TRUE if no changes were detected, FALSE
2778c616246SKonstantin Belousov  * otherwise.  The page is locked on return. The page queue lock might
2788c616246SKonstantin Belousov  * be dropped and reacquired.
2798c616246SKonstantin Belousov  *
2808c616246SKonstantin Belousov  * This function depends on normal struct vm_page being type stable.
2818c616246SKonstantin Belousov  */
2828c616246SKonstantin Belousov boolean_t
2838c616246SKonstantin Belousov vm_pageout_page_lock(vm_page_t m, vm_page_t *next)
2848c616246SKonstantin Belousov {
2858c616246SKonstantin Belousov 	struct vm_page marker;
2868c616246SKonstantin Belousov 	boolean_t unchanged;
2878c616246SKonstantin Belousov 	u_short queue;
2888c616246SKonstantin Belousov 
2898c616246SKonstantin Belousov 	vm_page_lock_assert(m, MA_NOTOWNED);
2908c616246SKonstantin Belousov 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2918c616246SKonstantin Belousov 
2928c616246SKonstantin Belousov 	if (vm_page_trylock(m))
2938c616246SKonstantin Belousov 		return (TRUE);
2948c616246SKonstantin Belousov 
2958c616246SKonstantin Belousov 	queue = m->queue;
2968c616246SKonstantin Belousov 	vm_pageout_init_marker(&marker, queue);
2978c616246SKonstantin Belousov 
2988c616246SKonstantin Belousov 	TAILQ_INSERT_AFTER(&vm_page_queues[queue].pl, m, &marker, pageq);
2998c616246SKonstantin Belousov 	vm_page_unlock_queues();
3008c616246SKonstantin Belousov 	vm_page_lock(m);
3018c616246SKonstantin Belousov 	vm_page_lock_queues();
3028c616246SKonstantin Belousov 
3038c616246SKonstantin Belousov 	/* Page queue might have changed. */
3048c616246SKonstantin Belousov 	*next = TAILQ_NEXT(&marker, pageq);
3058c616246SKonstantin Belousov 	unchanged = (m->queue == queue && &marker == TAILQ_NEXT(m, pageq));
3068c616246SKonstantin Belousov 	TAILQ_REMOVE(&vm_page_queues[queue].pl, &marker, pageq);
3078c616246SKonstantin Belousov 	return (unchanged);
3088c616246SKonstantin Belousov }
3098c616246SKonstantin Belousov 
3108c616246SKonstantin Belousov /*
31126f9a767SRodney W. Grimes  * vm_pageout_clean:
31224a1cce3SDavid Greenman  *
3130d94caffSDavid Greenman  * Clean the page and remove it from the laundry.
31426f9a767SRodney W. Grimes  *
3150d94caffSDavid Greenman  * We set the busy bit to cause potential page faults on this page to
3161c7c3c6aSMatthew Dillon  * block.  Note the careful timing, however, the busy bit isn't set till
3171c7c3c6aSMatthew Dillon  * late and we cannot do anything that will mess with the page.
31826f9a767SRodney W. Grimes  */
3193af76890SPoul-Henning Kamp static int
3202965a453SKip Macy vm_pageout_clean(vm_page_t m)
32124a1cce3SDavid Greenman {
32254d92145SMatthew Dillon 	vm_object_t object;
32391b4f427SAlan Cox 	vm_page_t mc[2*vm_pageout_page_count], pb, ps;
3243562af12SAlan Cox 	int pageout_count;
32590ecac61SMatthew Dillon 	int ib, is, page_base;
326a316d390SJohn Dyson 	vm_pindex_t pindex = m->pindex;
32726f9a767SRodney W. Grimes 
32895976f3fSAlan Cox 	vm_page_lock_assert(m, MA_OWNED);
3293562af12SAlan Cox 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
3300cddd8f0SMatthew Dillon 
33126f9a767SRodney W. Grimes 	/*
3321c7c3c6aSMatthew Dillon 	 * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP
3331c7c3c6aSMatthew Dillon 	 * with the new swapper, but we could have serious problems paging
3341c7c3c6aSMatthew Dillon 	 * out other object types if there is insufficient memory.
3351c7c3c6aSMatthew Dillon 	 *
3361c7c3c6aSMatthew Dillon 	 * Unfortunately, checking free memory here is far too late, so the
3371c7c3c6aSMatthew Dillon 	 * check has been moved up a procedural level.
3381c7c3c6aSMatthew Dillon 	 */
3391c7c3c6aSMatthew Dillon 
34024a1cce3SDavid Greenman 	/*
3419e897b1bSAlan Cox 	 * Can't clean the page if it's busy or held.
34224a1cce3SDavid Greenman 	 */
34395976f3fSAlan Cox 	KASSERT(m->busy == 0 && (m->oflags & VPO_BUSY) == 0,
34495976f3fSAlan Cox 	    ("vm_pageout_clean: page %p is busy", m));
34595976f3fSAlan Cox 	KASSERT(m->hold_count == 0, ("vm_pageout_clean: page %p is held", m));
3460d94caffSDavid Greenman 
34791b4f427SAlan Cox 	mc[vm_pageout_page_count] = pb = ps = m;
34826f9a767SRodney W. Grimes 	pageout_count = 1;
349f35329acSJohn Dyson 	page_base = vm_pageout_page_count;
35090ecac61SMatthew Dillon 	ib = 1;
35190ecac61SMatthew Dillon 	is = 1;
35290ecac61SMatthew Dillon 
35324a1cce3SDavid Greenman 	/*
35424a1cce3SDavid Greenman 	 * Scan object for clusterable pages.
35524a1cce3SDavid Greenman 	 *
35624a1cce3SDavid Greenman 	 * We can cluster ONLY if: ->> the page is NOT
35724a1cce3SDavid Greenman 	 * clean, wired, busy, held, or mapped into a
35824a1cce3SDavid Greenman 	 * buffer, and one of the following:
35924a1cce3SDavid Greenman 	 * 1) The page is inactive, or a seldom used
36024a1cce3SDavid Greenman 	 *    active page.
36124a1cce3SDavid Greenman 	 * -or-
36224a1cce3SDavid Greenman 	 * 2) we force the issue.
36390ecac61SMatthew Dillon 	 *
36490ecac61SMatthew Dillon 	 * During heavy mmap/modification loads the pageout
36590ecac61SMatthew Dillon 	 * daemon can really fragment the underlying file
36690ecac61SMatthew Dillon 	 * due to flushing pages out of order and not trying
36790ecac61SMatthew Dillon 	 * align the clusters (which leave sporatic out-of-order
36890ecac61SMatthew Dillon 	 * holes).  To solve this problem we do the reverse scan
36990ecac61SMatthew Dillon 	 * first and attempt to align our cluster, then do a
37090ecac61SMatthew Dillon 	 * forward scan if room remains.
37124a1cce3SDavid Greenman 	 */
3725163584cSAlan Cox 	object = m->object;
37390ecac61SMatthew Dillon more:
37490ecac61SMatthew Dillon 	while (ib && pageout_count < vm_pageout_page_count) {
37524a1cce3SDavid Greenman 		vm_page_t p;
376f6b04d2bSDavid Greenman 
37790ecac61SMatthew Dillon 		if (ib > pindex) {
37890ecac61SMatthew Dillon 			ib = 0;
37990ecac61SMatthew Dillon 			break;
380f6b04d2bSDavid Greenman 		}
38190ecac61SMatthew Dillon 
38291b4f427SAlan Cox 		if ((p = vm_page_prev(pb)) == NULL ||
38391b4f427SAlan Cox 		    (p->oflags & VPO_BUSY) != 0 || p->busy != 0) {
38490ecac61SMatthew Dillon 			ib = 0;
38590ecac61SMatthew Dillon 			break;
386f6b04d2bSDavid Greenman 		}
3872965a453SKip Macy 		vm_page_lock(p);
38824a1cce3SDavid Greenman 		vm_page_test_dirty(p);
38926f4eea5SAlan Cox 		if (p->dirty == 0 ||
39090ecac61SMatthew Dillon 		    p->queue != PQ_INACTIVE ||
39157601bcbSMatthew Dillon 		    p->hold_count != 0) {	/* may be undergoing I/O */
3922965a453SKip Macy 			vm_page_unlock(p);
39390ecac61SMatthew Dillon 			ib = 0;
39424a1cce3SDavid Greenman 			break;
395f6b04d2bSDavid Greenman 		}
3962965a453SKip Macy 		vm_page_unlock(p);
39791b4f427SAlan Cox 		mc[--page_base] = pb = p;
39890ecac61SMatthew Dillon 		++pageout_count;
39990ecac61SMatthew Dillon 		++ib;
40024a1cce3SDavid Greenman 		/*
40190ecac61SMatthew Dillon 		 * alignment boundry, stop here and switch directions.  Do
40290ecac61SMatthew Dillon 		 * not clear ib.
40324a1cce3SDavid Greenman 		 */
40490ecac61SMatthew Dillon 		if ((pindex - (ib - 1)) % vm_pageout_page_count == 0)
40590ecac61SMatthew Dillon 			break;
40624a1cce3SDavid Greenman 	}
40790ecac61SMatthew Dillon 
40890ecac61SMatthew Dillon 	while (pageout_count < vm_pageout_page_count &&
40990ecac61SMatthew Dillon 	    pindex + is < object->size) {
41090ecac61SMatthew Dillon 		vm_page_t p;
41190ecac61SMatthew Dillon 
41291b4f427SAlan Cox 		if ((p = vm_page_next(ps)) == NULL ||
41391b4f427SAlan Cox 		    (p->oflags & VPO_BUSY) != 0 || p->busy != 0)
41490ecac61SMatthew Dillon 			break;
4152965a453SKip Macy 		vm_page_lock(p);
41624a1cce3SDavid Greenman 		vm_page_test_dirty(p);
41726f4eea5SAlan Cox 		if (p->dirty == 0 ||
41890ecac61SMatthew Dillon 		    p->queue != PQ_INACTIVE ||
41957601bcbSMatthew Dillon 		    p->hold_count != 0) {	/* may be undergoing I/O */
4202965a453SKip Macy 			vm_page_unlock(p);
42124a1cce3SDavid Greenman 			break;
42224a1cce3SDavid Greenman 		}
4232965a453SKip Macy 		vm_page_unlock(p);
42491b4f427SAlan Cox 		mc[page_base + pageout_count] = ps = p;
42590ecac61SMatthew Dillon 		++pageout_count;
42690ecac61SMatthew Dillon 		++is;
42724a1cce3SDavid Greenman 	}
42890ecac61SMatthew Dillon 
42990ecac61SMatthew Dillon 	/*
43090ecac61SMatthew Dillon 	 * If we exhausted our forward scan, continue with the reverse scan
43190ecac61SMatthew Dillon 	 * when possible, even past a page boundry.  This catches boundry
43290ecac61SMatthew Dillon 	 * conditions.
43390ecac61SMatthew Dillon 	 */
43490ecac61SMatthew Dillon 	if (ib && pageout_count < vm_pageout_page_count)
43590ecac61SMatthew Dillon 		goto more;
436f6b04d2bSDavid Greenman 
4372965a453SKip Macy 	vm_page_unlock(m);
43867bf6868SJohn Dyson 	/*
43967bf6868SJohn Dyson 	 * we allow reads during pageouts...
44067bf6868SJohn Dyson 	 */
441*1e8a675cSKonstantin Belousov 	return (vm_pageout_flush(&mc[page_base], pageout_count, 0, 0, NULL));
442aef922f5SJohn Dyson }
443aef922f5SJohn Dyson 
4441c7c3c6aSMatthew Dillon /*
4451c7c3c6aSMatthew Dillon  * vm_pageout_flush() - launder the given pages
4461c7c3c6aSMatthew Dillon  *
4471c7c3c6aSMatthew Dillon  *	The given pages are laundered.  Note that we setup for the start of
4481c7c3c6aSMatthew Dillon  *	I/O ( i.e. busy the page ), mark it read-only, and bump the object
4491c7c3c6aSMatthew Dillon  *	reference count all in here rather then in the parent.  If we want
4501c7c3c6aSMatthew Dillon  *	the parent to do more sophisticated things we may have to change
4511c7c3c6aSMatthew Dillon  *	the ordering.
452*1e8a675cSKonstantin Belousov  *
453*1e8a675cSKonstantin Belousov  *	Returned runlen is the count of pages between mreq and first
454*1e8a675cSKonstantin Belousov  *	page after mreq with status VM_PAGER_AGAIN.
4551c7c3c6aSMatthew Dillon  */
456aef922f5SJohn Dyson int
457*1e8a675cSKonstantin Belousov vm_pageout_flush(vm_page_t *mc, int count, int flags, int mreq, int *prunlen)
458aef922f5SJohn Dyson {
4592e3b314dSAlan Cox 	vm_object_t object = mc[0]->object;
460aef922f5SJohn Dyson 	int pageout_status[count];
46195461b45SJohn Dyson 	int numpagedout = 0;
462*1e8a675cSKonstantin Belousov 	int i, runlen;
463aef922f5SJohn Dyson 
4642e3b314dSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
4657bec141bSKip Macy 	mtx_assert(&vm_page_queue_mtx, MA_NOTOWNED);
4667bec141bSKip Macy 
4671c7c3c6aSMatthew Dillon 	/*
4681c7c3c6aSMatthew Dillon 	 * Initiate I/O.  Bump the vm_page_t->busy counter and
4691c7c3c6aSMatthew Dillon 	 * mark the pages read-only.
4701c7c3c6aSMatthew Dillon 	 *
4711c7c3c6aSMatthew Dillon 	 * We do not have to fixup the clean/dirty bits here... we can
4721c7c3c6aSMatthew Dillon 	 * allow the pager to do it after the I/O completes.
47302fa91d3SMatthew Dillon 	 *
47402fa91d3SMatthew Dillon 	 * NOTE! mc[i]->dirty may be partial or fragmented due to an
47502fa91d3SMatthew Dillon 	 * edge case with file fragments.
4761c7c3c6aSMatthew Dillon 	 */
4778f9110f6SJohn Dyson 	for (i = 0; i < count; i++) {
4787a935082SAlan Cox 		KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL,
4797a935082SAlan Cox 		    ("vm_pageout_flush: partially invalid page %p index %d/%d",
4807a935082SAlan Cox 			mc[i], i, count));
481e69763a3SDoug Rabson 		vm_page_io_start(mc[i]);
48278985e42SAlan Cox 		pmap_remove_write(mc[i]);
4832965a453SKip Macy 	}
484d474eaaaSDoug Rabson 	vm_object_pip_add(object, count);
485aef922f5SJohn Dyson 
486d076fbeaSAlan Cox 	vm_pager_put_pages(object, mc, count, flags, pageout_status);
48726f9a767SRodney W. Grimes 
488*1e8a675cSKonstantin Belousov 	runlen = count - mreq;
489aef922f5SJohn Dyson 	for (i = 0; i < count; i++) {
490aef922f5SJohn Dyson 		vm_page_t mt = mc[i];
49124a1cce3SDavid Greenman 
4924cd45723SAlan Cox 		KASSERT(pageout_status[i] == VM_PAGER_PEND ||
4934cd45723SAlan Cox 		    (mt->flags & PG_WRITEABLE) == 0,
4949ea8d1a6SAlan Cox 		    ("vm_pageout_flush: page %p is not write protected", mt));
49526f9a767SRodney W. Grimes 		switch (pageout_status[i]) {
49626f9a767SRodney W. Grimes 		case VM_PAGER_OK:
49726f9a767SRodney W. Grimes 		case VM_PAGER_PEND:
49895461b45SJohn Dyson 			numpagedout++;
49926f9a767SRodney W. Grimes 			break;
50026f9a767SRodney W. Grimes 		case VM_PAGER_BAD:
50126f9a767SRodney W. Grimes 			/*
5020d94caffSDavid Greenman 			 * Page outside of range of object. Right now we
5030d94caffSDavid Greenman 			 * essentially lose the changes by pretending it
5040d94caffSDavid Greenman 			 * worked.
50526f9a767SRodney W. Grimes 			 */
50690ecac61SMatthew Dillon 			vm_page_undirty(mt);
50726f9a767SRodney W. Grimes 			break;
50826f9a767SRodney W. Grimes 		case VM_PAGER_ERROR:
50926f9a767SRodney W. Grimes 		case VM_PAGER_FAIL:
51026f9a767SRodney W. Grimes 			/*
5110d94caffSDavid Greenman 			 * If page couldn't be paged out, then reactivate the
5120d94caffSDavid Greenman 			 * page so it doesn't clog the inactive list.  (We
5130d94caffSDavid Greenman 			 * will try paging out it again later).
51426f9a767SRodney W. Grimes 			 */
5153c4a2440SAlan Cox 			vm_page_lock(mt);
51624a1cce3SDavid Greenman 			vm_page_activate(mt);
5173c4a2440SAlan Cox 			vm_page_unlock(mt);
51826f9a767SRodney W. Grimes 			break;
51926f9a767SRodney W. Grimes 		case VM_PAGER_AGAIN:
520*1e8a675cSKonstantin Belousov 			if (i >= mreq && i - mreq < runlen)
521*1e8a675cSKonstantin Belousov 				runlen = i - mreq;
52226f9a767SRodney W. Grimes 			break;
52326f9a767SRodney W. Grimes 		}
52426f9a767SRodney W. Grimes 
52526f9a767SRodney W. Grimes 		/*
5260d94caffSDavid Greenman 		 * If the operation is still going, leave the page busy to
5270d94caffSDavid Greenman 		 * block all other accesses. Also, leave the paging in
5280d94caffSDavid Greenman 		 * progress indicator set so that we don't attempt an object
5290d94caffSDavid Greenman 		 * collapse.
53026f9a767SRodney W. Grimes 		 */
53126f9a767SRodney W. Grimes 		if (pageout_status[i] != VM_PAGER_PEND) {
532f919ebdeSDavid Greenman 			vm_object_pip_wakeup(object);
533e69763a3SDoug Rabson 			vm_page_io_finish(mt);
5343c4a2440SAlan Cox 			if (vm_page_count_severe()) {
5353c4a2440SAlan Cox 				vm_page_lock(mt);
5369ea8d1a6SAlan Cox 				vm_page_try_to_cache(mt);
5372965a453SKip Macy 				vm_page_unlock(mt);
53826f9a767SRodney W. Grimes 			}
5393c4a2440SAlan Cox 		}
5403c4a2440SAlan Cox 	}
541*1e8a675cSKonstantin Belousov 	if (prunlen != NULL)
542*1e8a675cSKonstantin Belousov 		*prunlen = runlen;
5433c4a2440SAlan Cox 	return (numpagedout);
54426f9a767SRodney W. Grimes }
54526f9a767SRodney W. Grimes 
54638efa82bSJohn Dyson #if !defined(NO_SWAPPING)
54726f9a767SRodney W. Grimes /*
54826f9a767SRodney W. Grimes  *	vm_pageout_object_deactivate_pages
54926f9a767SRodney W. Grimes  *
550ce186587SAlan Cox  *	Deactivate enough pages to satisfy the inactive target
551ce186587SAlan Cox  *	requirements.
55226f9a767SRodney W. Grimes  *
55326f9a767SRodney W. Grimes  *	The object and map must be locked.
55426f9a767SRodney W. Grimes  */
55538efa82bSJohn Dyson static void
556ce186587SAlan Cox vm_pageout_object_deactivate_pages(pmap_t pmap, vm_object_t first_object,
557ce186587SAlan Cox     long desired)
55826f9a767SRodney W. Grimes {
559ecf6279fSAlan Cox 	vm_object_t backing_object, object;
560ce186587SAlan Cox 	vm_page_t p;
56182bfb965SAlan Cox 	int actcount, remove_mode;
56226f9a767SRodney W. Grimes 
563ecf6279fSAlan Cox 	VM_OBJECT_LOCK_ASSERT(first_object, MA_OWNED);
56401381811SJohn Baldwin 	if (first_object->type == OBJT_DEVICE ||
56582bfb965SAlan Cox 	    first_object->type == OBJT_SG)
56638efa82bSJohn Dyson 		return;
567ecf6279fSAlan Cox 	for (object = first_object;; object = backing_object) {
568ecf6279fSAlan Cox 		if (pmap_resident_count(pmap) <= desired)
569ecf6279fSAlan Cox 			goto unlock_return;
570447fe2a4SAlan Cox 		VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
57182bfb965SAlan Cox 		if (object->type == OBJT_PHYS || object->paging_in_progress)
572ecf6279fSAlan Cox 			goto unlock_return;
57326f9a767SRodney W. Grimes 
57485b1dc89SAlan Cox 		remove_mode = 0;
57538efa82bSJohn Dyson 		if (object->shadow_count > 1)
57638efa82bSJohn Dyson 			remove_mode = 1;
57726f9a767SRodney W. Grimes 		/*
578ce186587SAlan Cox 		 * Scan the object's entire memory queue.
57926f9a767SRodney W. Grimes 		 */
580ce186587SAlan Cox 		TAILQ_FOREACH(p, &object->memq, listq) {
581447fe2a4SAlan Cox 			if (pmap_resident_count(pmap) <= desired)
582447fe2a4SAlan Cox 				goto unlock_return;
583ce186587SAlan Cox 			if ((p->oflags & VPO_BUSY) != 0 || p->busy != 0)
584447fe2a4SAlan Cox 				continue;
585ce186587SAlan Cox 			PCPU_INC(cnt.v_pdpages);
5862965a453SKip Macy 			vm_page_lock(p);
587ce186587SAlan Cox 			if (p->wire_count != 0 || p->hold_count != 0 ||
588ecf6279fSAlan Cox 			    !pmap_page_exists_quick(pmap, p)) {
5892965a453SKip Macy 				vm_page_unlock(p);
5900d94caffSDavid Greenman 				continue;
5910d94caffSDavid Greenman 			}
5920385347cSPeter Wemm 			actcount = pmap_ts_referenced(p);
593ce186587SAlan Cox 			if ((p->flags & PG_REFERENCED) != 0) {
594ce186587SAlan Cox 				if (actcount == 0)
5957e006499SJohn Dyson 					actcount = 1;
596ce186587SAlan Cox 				vm_page_lock_queues();
597ce186587SAlan Cox 				vm_page_flag_clear(p, PG_REFERENCED);
598ce186587SAlan Cox 				vm_page_unlock_queues();
599ef743ce6SJohn Dyson 			}
600ce186587SAlan Cox 			if (p->queue != PQ_ACTIVE && actcount != 0) {
601ef743ce6SJohn Dyson 				vm_page_activate(p);
6027e006499SJohn Dyson 				p->act_count += actcount;
603c8c4b40cSJohn Dyson 			} else if (p->queue == PQ_ACTIVE) {
604ce186587SAlan Cox 				if (actcount == 0) {
605ce186587SAlan Cox 					p->act_count -= min(p->act_count,
606ce186587SAlan Cox 					    ACT_DECLINE);
607ce186587SAlan Cox 					if (!remove_mode &&
608ce186587SAlan Cox 					    (vm_pageout_algorithm ||
609ce186587SAlan Cox 					    p->act_count == 0)) {
6104fec79beSAlan Cox 						pmap_remove_all(p);
61126f9a767SRodney W. Grimes 						vm_page_deactivate(p);
61226f9a767SRodney W. Grimes 					} else {
613ce186587SAlan Cox 						vm_page_lock_queues();
614e5b006ffSAlan Cox 						vm_page_requeue(p);
615ce186587SAlan Cox 						vm_page_unlock_queues();
616c8c4b40cSJohn Dyson 					}
617c8c4b40cSJohn Dyson 				} else {
618eaf13dd7SJohn Dyson 					vm_page_activate(p);
619ce186587SAlan Cox 					if (p->act_count < ACT_MAX -
620ce186587SAlan Cox 					    ACT_ADVANCE)
62138efa82bSJohn Dyson 						p->act_count += ACT_ADVANCE;
622ce186587SAlan Cox 					vm_page_lock_queues();
623e5b006ffSAlan Cox 					vm_page_requeue(p);
6242965a453SKip Macy 					vm_page_unlock_queues();
625ce186587SAlan Cox 				}
626ce186587SAlan Cox 			} else if (p->queue == PQ_INACTIVE)
627ce186587SAlan Cox 				pmap_remove_all(p);
6282965a453SKip Macy 			vm_page_unlock(p);
62926f9a767SRodney W. Grimes 		}
630ecf6279fSAlan Cox 		if ((backing_object = object->backing_object) == NULL)
631ecf6279fSAlan Cox 			goto unlock_return;
632ecf6279fSAlan Cox 		VM_OBJECT_LOCK(backing_object);
633ecf6279fSAlan Cox 		if (object != first_object)
634ecf6279fSAlan Cox 			VM_OBJECT_UNLOCK(object);
63538efa82bSJohn Dyson 	}
636ecf6279fSAlan Cox unlock_return:
637ecf6279fSAlan Cox 	if (object != first_object)
638ecf6279fSAlan Cox 		VM_OBJECT_UNLOCK(object);
63926f9a767SRodney W. Grimes }
64026f9a767SRodney W. Grimes 
64126f9a767SRodney W. Grimes /*
64226f9a767SRodney W. Grimes  * deactivate some number of pages in a map, try to do it fairly, but
64326f9a767SRodney W. Grimes  * that is really hard to do.
64426f9a767SRodney W. Grimes  */
645cd41fc12SDavid Greenman static void
64638efa82bSJohn Dyson vm_pageout_map_deactivate_pages(map, desired)
64726f9a767SRodney W. Grimes 	vm_map_t map;
648ecf6279fSAlan Cox 	long desired;
64926f9a767SRodney W. Grimes {
65026f9a767SRodney W. Grimes 	vm_map_entry_t tmpe;
65138efa82bSJohn Dyson 	vm_object_t obj, bigobj;
65230105b9eSTor Egge 	int nothingwired;
6530d94caffSDavid Greenman 
654d974f03cSAlan Cox 	if (!vm_map_trylock(map))
65526f9a767SRodney W. Grimes 		return;
65638efa82bSJohn Dyson 
65738efa82bSJohn Dyson 	bigobj = NULL;
65830105b9eSTor Egge 	nothingwired = TRUE;
65938efa82bSJohn Dyson 
66038efa82bSJohn Dyson 	/*
66138efa82bSJohn Dyson 	 * first, search out the biggest object, and try to free pages from
66238efa82bSJohn Dyson 	 * that.
66338efa82bSJohn Dyson 	 */
66426f9a767SRodney W. Grimes 	tmpe = map->header.next;
66538efa82bSJohn Dyson 	while (tmpe != &map->header) {
6669fdfe602SMatthew Dillon 		if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
66738efa82bSJohn Dyson 			obj = tmpe->object.vm_object;
6680774dfb3SAlan Cox 			if (obj != NULL && VM_OBJECT_TRYLOCK(obj)) {
6690774dfb3SAlan Cox 				if (obj->shadow_count <= 1 &&
6700774dfb3SAlan Cox 				    (bigobj == NULL ||
6710774dfb3SAlan Cox 				     bigobj->resident_page_count < obj->resident_page_count)) {
6720774dfb3SAlan Cox 					if (bigobj != NULL)
6730774dfb3SAlan Cox 						VM_OBJECT_UNLOCK(bigobj);
67438efa82bSJohn Dyson 					bigobj = obj;
6750774dfb3SAlan Cox 				} else
6760774dfb3SAlan Cox 					VM_OBJECT_UNLOCK(obj);
67738efa82bSJohn Dyson 			}
67838efa82bSJohn Dyson 		}
67930105b9eSTor Egge 		if (tmpe->wired_count > 0)
68030105b9eSTor Egge 			nothingwired = FALSE;
68138efa82bSJohn Dyson 		tmpe = tmpe->next;
68238efa82bSJohn Dyson 	}
68338efa82bSJohn Dyson 
6840774dfb3SAlan Cox 	if (bigobj != NULL) {
685ecf6279fSAlan Cox 		vm_pageout_object_deactivate_pages(map->pmap, bigobj, desired);
6860774dfb3SAlan Cox 		VM_OBJECT_UNLOCK(bigobj);
6870774dfb3SAlan Cox 	}
68838efa82bSJohn Dyson 	/*
68938efa82bSJohn Dyson 	 * Next, hunt around for other pages to deactivate.  We actually
69038efa82bSJohn Dyson 	 * do this search sort of wrong -- .text first is not the best idea.
69138efa82bSJohn Dyson 	 */
69238efa82bSJohn Dyson 	tmpe = map->header.next;
69338efa82bSJohn Dyson 	while (tmpe != &map->header) {
694b1028ad1SLuoqi Chen 		if (pmap_resident_count(vm_map_pmap(map)) <= desired)
69538efa82bSJohn Dyson 			break;
6969fdfe602SMatthew Dillon 		if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
69738efa82bSJohn Dyson 			obj = tmpe->object.vm_object;
6980774dfb3SAlan Cox 			if (obj != NULL) {
6990774dfb3SAlan Cox 				VM_OBJECT_LOCK(obj);
700ecf6279fSAlan Cox 				vm_pageout_object_deactivate_pages(map->pmap, obj, desired);
7010774dfb3SAlan Cox 				VM_OBJECT_UNLOCK(obj);
7020774dfb3SAlan Cox 			}
70338efa82bSJohn Dyson 		}
70426f9a767SRodney W. Grimes 		tmpe = tmpe->next;
70538857e7fSAlan Cox 	}
70638efa82bSJohn Dyson 
70738efa82bSJohn Dyson 	/*
70838efa82bSJohn Dyson 	 * Remove all mappings if a process is swapped out, this will free page
70938efa82bSJohn Dyson 	 * table pages.
71038efa82bSJohn Dyson 	 */
71138857e7fSAlan Cox 	if (desired == 0 && nothingwired) {
71242768fecSNathan Whitehorn 		tmpe = map->header.next;
71342768fecSNathan Whitehorn 		while (tmpe != &map->header) {
71442768fecSNathan Whitehorn 			pmap_remove(vm_map_pmap(map), tmpe->start, tmpe->end);
71542768fecSNathan Whitehorn 			tmpe = tmpe->next;
71642768fecSNathan Whitehorn 		}
71738857e7fSAlan Cox 	}
71838efa82bSJohn Dyson 	vm_map_unlock(map);
71926f9a767SRodney W. Grimes }
720a1287949SEivind Eklund #endif		/* !defined(NO_SWAPPING) */
721df8bae1dSRodney W. Grimes 
7221c7c3c6aSMatthew Dillon /*
723df8bae1dSRodney W. Grimes  *	vm_pageout_scan does the dirty work for the pageout daemon.
724df8bae1dSRodney W. Grimes  */
7252b6b0df7SMatthew Dillon static void
7262b6b0df7SMatthew Dillon vm_pageout_scan(int pass)
727df8bae1dSRodney W. Grimes {
728502ba6e4SJohn Dyson 	vm_page_t m, next;
729936524aaSMatthew Dillon 	struct vm_page marker;
7301c7c3c6aSMatthew Dillon 	int page_shortage, maxscan, pcount;
7311c7c3c6aSMatthew Dillon 	int addl_page_shortage, addl_page_shortage_init;
732df8bae1dSRodney W. Grimes 	vm_object_t object;
7332446e4f0SAlan Cox 	int actcount;
734f6b04d2bSDavid Greenman 	int vnodes_skipped = 0;
7352b6b0df7SMatthew Dillon 	int maxlaunder;
7360d94caffSDavid Greenman 
737df8bae1dSRodney W. Grimes 	/*
738855a310fSJeff Roberson 	 * Decrease registered cache sizes.
739855a310fSJeff Roberson 	 */
740855a310fSJeff Roberson 	EVENTHANDLER_INVOKE(vm_lowmem, 0);
741855a310fSJeff Roberson 	/*
742855a310fSJeff Roberson 	 * We do this explicitly after the caches have been drained above.
743855a310fSJeff Roberson 	 */
744855a310fSJeff Roberson 	uma_reclaim();
7455985940eSJohn Dyson 
746b0ef8c5fSAlan Cox 	addl_page_shortage_init = atomic_readandclear_int(&vm_pageout_deficit);
747b182ec9eSJohn Dyson 
7481c7c3c6aSMatthew Dillon 	/*
7491c7c3c6aSMatthew Dillon 	 * Calculate the number of pages we want to either free or move
7502b6b0df7SMatthew Dillon 	 * to the cache.
7511c7c3c6aSMatthew Dillon 	 */
7522b6b0df7SMatthew Dillon 	page_shortage = vm_paging_target() + addl_page_shortage_init;
7531c7c3c6aSMatthew Dillon 
754af394cfaSJung-uk Kim 	vm_pageout_init_marker(&marker, PQ_INACTIVE);
755936524aaSMatthew Dillon 
756936524aaSMatthew Dillon 	/*
7571c7c3c6aSMatthew Dillon 	 * Start scanning the inactive queue for pages we can move to the
7581c7c3c6aSMatthew Dillon 	 * cache or free.  The scan will stop when the target is reached or
759936524aaSMatthew Dillon 	 * we have scanned the entire inactive queue.  Note that m->act_count
760936524aaSMatthew Dillon 	 * is not used to form decisions for the inactive queue, only for the
761936524aaSMatthew Dillon 	 * active queue.
7622b6b0df7SMatthew Dillon 	 *
7632b6b0df7SMatthew Dillon 	 * maxlaunder limits the number of dirty pages we flush per scan.
7642b6b0df7SMatthew Dillon 	 * For most systems a smaller value (16 or 32) is more robust under
7652b6b0df7SMatthew Dillon 	 * extreme memory and disk pressure because any unnecessary writes
7662b6b0df7SMatthew Dillon 	 * to disk can result in extreme performance degredation.  However,
7672b6b0df7SMatthew Dillon 	 * systems with excessive dirty pages (especially when MAP_NOSYNC is
7682b6b0df7SMatthew Dillon 	 * used) will die horribly with limited laundering.  If the pageout
7692b6b0df7SMatthew Dillon 	 * daemon cannot clean enough pages in the first pass, we let it go
7702b6b0df7SMatthew Dillon 	 * all out in succeeding passes.
7711c7c3c6aSMatthew Dillon 	 */
7722b6b0df7SMatthew Dillon 	if ((maxlaunder = vm_max_launder) <= 1)
7732b6b0df7SMatthew Dillon 		maxlaunder = 1;
7742b6b0df7SMatthew Dillon 	if (pass)
7752b6b0df7SMatthew Dillon 		maxlaunder = 10000;
7763e1b578aSAlan Cox 	vm_page_lock_queues();
77767bf6868SJohn Dyson rescan0:
7781c7c3c6aSMatthew Dillon 	addl_page_shortage = addl_page_shortage_init;
7792feb50bfSAttilio Rao 	maxscan = cnt.v_inactive_count;
7806d03d577SMatthew Dillon 
781be72f788SAlan Cox 	for (m = TAILQ_FIRST(&vm_page_queues[PQ_INACTIVE].pl);
7821c7c3c6aSMatthew Dillon 	     m != NULL && maxscan-- > 0 && page_shortage > 0;
783e929c00dSKirk McKusick 	     m = next) {
784df8bae1dSRodney W. Grimes 
785393a081dSAttilio Rao 		cnt.v_pdpages++;
786b182ec9eSJohn Dyson 
7879cf51988SAlan Cox 		if (m->queue != PQ_INACTIVE)
78867bf6868SJohn Dyson 			goto rescan0;
789b182ec9eSJohn Dyson 
790b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(m, pageq);
791df8bae1dSRodney W. Grimes 
792936524aaSMatthew Dillon 		/*
793936524aaSMatthew Dillon 		 * skip marker pages
794936524aaSMatthew Dillon 		 */
795936524aaSMatthew Dillon 		if (m->flags & PG_MARKER)
796936524aaSMatthew Dillon 			continue;
797936524aaSMatthew Dillon 
7988c616246SKonstantin Belousov 		/*
7998c616246SKonstantin Belousov 		 * Lock the page.
8008c616246SKonstantin Belousov 		 */
8018c616246SKonstantin Belousov 		if (!vm_pageout_page_lock(m, &next)) {
8028c616246SKonstantin Belousov 			vm_page_unlock(m);
803b182ec9eSJohn Dyson 			addl_page_shortage++;
804b182ec9eSJohn Dyson 			continue;
805df8bae1dSRodney W. Grimes 		}
8062965a453SKip Macy 
807e8f26319SKip Macy 		/*
808e8f26319SKip Macy 		 * A held page may be undergoing I/O, so skip it.
809e8f26319SKip Macy 		 */
8109ee2165fSAlan Cox 		if (m->hold_count) {
811e8f26319SKip Macy 			vm_page_unlock(m);
812e8f26319SKip Macy 			vm_page_requeue(m);
8132965a453SKip Macy 			addl_page_shortage++;
8142965a453SKip Macy 			continue;
8152965a453SKip Macy 		}
8162965a453SKip Macy 
81726f9a767SRodney W. Grimes 		/*
818a1287949SEivind Eklund 		 * Don't mess with busy pages, keep in the front of the
819b18bfc3dSJohn Dyson 		 * queue, most likely are being paged out.
82026f9a767SRodney W. Grimes 		 */
8219ee2165fSAlan Cox 		object = m->object;
8228dbca793STor Egge 		if (!VM_OBJECT_TRYLOCK(object) &&
8238dbca793STor Egge 		    (!vm_pageout_fallback_object_lock(m, &next) ||
8248dbca793STor Egge 			m->hold_count != 0)) {
8258dbca793STor Egge 			VM_OBJECT_UNLOCK(object);
8262965a453SKip Macy 			vm_page_unlock(m);
82734d9e6fdSAlan Cox 			addl_page_shortage++;
82834d9e6fdSAlan Cox 			continue;
82934d9e6fdSAlan Cox 		}
8309af80719SAlan Cox 		if (m->busy || (m->oflags & VPO_BUSY)) {
8312965a453SKip Macy 			vm_page_unlock(m);
83234d9e6fdSAlan Cox 			VM_OBJECT_UNLOCK(object);
833b182ec9eSJohn Dyson 			addl_page_shortage++;
83426f9a767SRodney W. Grimes 			continue;
83526f9a767SRodney W. Grimes 		}
836bd7e5f99SJohn Dyson 
8377e006499SJohn Dyson 		/*
8381c7c3c6aSMatthew Dillon 		 * If the object is not being used, we ignore previous
8391c7c3c6aSMatthew Dillon 		 * references.
8407e006499SJohn Dyson 		 */
84134d9e6fdSAlan Cox 		if (object->ref_count == 0) {
842e69763a3SDoug Rabson 			vm_page_flag_clear(m, PG_REFERENCED);
84347916d0cSAlan Cox 			KASSERT(!pmap_page_is_mapped(m),
84447916d0cSAlan Cox 			    ("vm_pageout_scan: page %p is mapped", m));
8457e006499SJohn Dyson 
8467e006499SJohn Dyson 		/*
8471c7c3c6aSMatthew Dillon 		 * Otherwise, if the page has been referenced while in the
8481c7c3c6aSMatthew Dillon 		 * inactive queue, we bump the "activation count" upwards,
8491c7c3c6aSMatthew Dillon 		 * making it less likely that the page will be added back to
8501c7c3c6aSMatthew Dillon 		 * the inactive queue prematurely again.  Here we check the
8511c7c3c6aSMatthew Dillon 		 * page tables (or emulated bits, if any), given the upper
8521c7c3c6aSMatthew Dillon 		 * level VM system not knowing anything about existing
8531c7c3c6aSMatthew Dillon 		 * references.
8547e006499SJohn Dyson 		 */
855ef743ce6SJohn Dyson 		} else if (((m->flags & PG_REFERENCED) == 0) &&
8560385347cSPeter Wemm 			(actcount = pmap_ts_referenced(m))) {
857ef743ce6SJohn Dyson 			vm_page_activate(m);
85834d9e6fdSAlan Cox 			VM_OBJECT_UNLOCK(object);
8597e006499SJohn Dyson 			m->act_count += (actcount + ACT_ADVANCE);
8602965a453SKip Macy 			vm_page_unlock(m);
861ef743ce6SJohn Dyson 			continue;
8622fe6e4d7SDavid Greenman 		}
863ef743ce6SJohn Dyson 
8647e006499SJohn Dyson 		/*
8651c7c3c6aSMatthew Dillon 		 * If the upper level VM system knows about any page
8661c7c3c6aSMatthew Dillon 		 * references, we activate the page.  We also set the
8671c7c3c6aSMatthew Dillon 		 * "activation count" higher than normal so that we will less
8681c7c3c6aSMatthew Dillon 		 * likely place pages back onto the inactive queue again.
8697e006499SJohn Dyson 		 */
870bd7e5f99SJohn Dyson 		if ((m->flags & PG_REFERENCED) != 0) {
871e69763a3SDoug Rabson 			vm_page_flag_clear(m, PG_REFERENCED);
8720385347cSPeter Wemm 			actcount = pmap_ts_referenced(m);
87326f9a767SRodney W. Grimes 			vm_page_activate(m);
87434d9e6fdSAlan Cox 			VM_OBJECT_UNLOCK(object);
8757e006499SJohn Dyson 			m->act_count += (actcount + ACT_ADVANCE + 1);
8762965a453SKip Macy 			vm_page_unlock(m);
8770d94caffSDavid Greenman 			continue;
8780d94caffSDavid Greenman 		}
87967bf6868SJohn Dyson 
8807e006499SJohn Dyson 		/*
881b78ddb0bSAlan Cox 		 * If the upper level VM system does not believe that the page
882b78ddb0bSAlan Cox 		 * is fully dirty, but it is mapped for write access, then we
883b78ddb0bSAlan Cox 		 * consult the pmap to see if the page's dirty status should
884b78ddb0bSAlan Cox 		 * be updated.
8857e006499SJohn Dyson 		 */
886b78ddb0bSAlan Cox 		if (m->dirty != VM_PAGE_BITS_ALL &&
887b78ddb0bSAlan Cox 		    (m->flags & PG_WRITEABLE) != 0) {
888a3dfacb5SAlan Cox 			/*
889a3dfacb5SAlan Cox 			 * Avoid a race condition: Unless write access is
890a3dfacb5SAlan Cox 			 * removed from the page, another processor could
891a3dfacb5SAlan Cox 			 * modify it before all access is removed by the call
892a3dfacb5SAlan Cox 			 * to vm_page_cache() below.  If vm_page_cache() finds
893a3dfacb5SAlan Cox 			 * that the page has been modified when it removes all
894a3dfacb5SAlan Cox 			 * access, it panics because it cannot cache dirty
895a3dfacb5SAlan Cox 			 * pages.  In principle, we could eliminate just write
896a3dfacb5SAlan Cox 			 * access here rather than all access.  In the expected
897a3dfacb5SAlan Cox 			 * case, when there are no last instant modifications
898a3dfacb5SAlan Cox 			 * to the page, removing all access will be cheaper
899a3dfacb5SAlan Cox 			 * overall.
900a3dfacb5SAlan Cox 			 */
901b78ddb0bSAlan Cox 			if (pmap_is_modified(m))
9027dbf82dcSMatthew Dillon 				vm_page_dirty(m);
903b78ddb0bSAlan Cox 			else if (m->dirty == 0)
904b78ddb0bSAlan Cox 				pmap_remove_all(m);
90530dcfc09SJohn Dyson 		}
906dcbcd518SBruce Evans 
9076989c456SAlan Cox 		if (m->valid == 0) {
9087e006499SJohn Dyson 			/*
9097e006499SJohn Dyson 			 * Invalid pages can be easily freed
9107e006499SJohn Dyson 			 */
9116989c456SAlan Cox 			vm_page_free(m);
912393a081dSAttilio Rao 			cnt.v_dfree++;
9131c7c3c6aSMatthew Dillon 			--page_shortage;
914bd7e5f99SJohn Dyson 		} else if (m->dirty == 0) {
9156989c456SAlan Cox 			/*
9166989c456SAlan Cox 			 * Clean pages can be placed onto the cache queue.
9176989c456SAlan Cox 			 * This effectively frees them.
9186989c456SAlan Cox 			 */
919bd7e5f99SJohn Dyson 			vm_page_cache(m);
9201c7c3c6aSMatthew Dillon 			--page_shortage;
9212b6b0df7SMatthew Dillon 		} else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) {
9227e006499SJohn Dyson 			/*
9232b6b0df7SMatthew Dillon 			 * Dirty pages need to be paged out, but flushing
9242b6b0df7SMatthew Dillon 			 * a page is extremely expensive verses freeing
9252b6b0df7SMatthew Dillon 			 * a clean page.  Rather then artificially limiting
9262b6b0df7SMatthew Dillon 			 * the number of pages we can flush, we instead give
9272b6b0df7SMatthew Dillon 			 * dirty pages extra priority on the inactive queue
9282b6b0df7SMatthew Dillon 			 * by forcing them to be cycled through the queue
9292b6b0df7SMatthew Dillon 			 * twice before being flushed, after which the
9302b6b0df7SMatthew Dillon 			 * (now clean) page will cycle through once more
9312b6b0df7SMatthew Dillon 			 * before being freed.  This significantly extends
9322b6b0df7SMatthew Dillon 			 * the thrash point for a heavily loaded machine.
9337e006499SJohn Dyson 			 */
9342b6b0df7SMatthew Dillon 			vm_page_flag_set(m, PG_WINATCFLS);
935e5b006ffSAlan Cox 			vm_page_requeue(m);
9360d94caffSDavid Greenman 		} else if (maxlaunder > 0) {
9372b6b0df7SMatthew Dillon 			/*
9382b6b0df7SMatthew Dillon 			 * We always want to try to flush some dirty pages if
9392b6b0df7SMatthew Dillon 			 * we encounter them, to keep the system stable.
9402b6b0df7SMatthew Dillon 			 * Normally this number is small, but under extreme
9412b6b0df7SMatthew Dillon 			 * pressure where there are insufficient clean pages
9422b6b0df7SMatthew Dillon 			 * on the inactive queue, we may have to go all out.
9432b6b0df7SMatthew Dillon 			 */
94497824da3SAlan Cox 			int swap_pageouts_ok, vfslocked = 0;
945f6b04d2bSDavid Greenman 			struct vnode *vp = NULL;
94614137dc0SAlan Cox 			struct mount *mp = NULL;
9470d94caffSDavid Greenman 
94812ac6a1dSJohn Dyson 			if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) {
94912ac6a1dSJohn Dyson 				swap_pageouts_ok = 1;
95012ac6a1dSJohn Dyson 			} else {
95112ac6a1dSJohn Dyson 				swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts);
95212ac6a1dSJohn Dyson 				swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts &&
95390ecac61SMatthew Dillon 				vm_page_count_min());
95412ac6a1dSJohn Dyson 
95512ac6a1dSJohn Dyson 			}
95670111b90SJohn Dyson 
95770111b90SJohn Dyson 			/*
9581c7c3c6aSMatthew Dillon 			 * We don't bother paging objects that are "dead".
9591c7c3c6aSMatthew Dillon 			 * Those objects are in a "rundown" state.
96070111b90SJohn Dyson 			 */
96170111b90SJohn Dyson 			if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) {
9622965a453SKip Macy 				vm_page_unlock(m);
9633562af12SAlan Cox 				VM_OBJECT_UNLOCK(object);
964e5b006ffSAlan Cox 				vm_page_requeue(m);
96512ac6a1dSJohn Dyson 				continue;
96612ac6a1dSJohn Dyson 			}
96712ac6a1dSJohn Dyson 
9681c7c3c6aSMatthew Dillon 			/*
969625e6c0aSTor Egge 			 * Following operations may unlock
970625e6c0aSTor Egge 			 * vm_page_queue_mtx, invalidating the 'next'
971625e6c0aSTor Egge 			 * pointer.  To prevent an inordinate number
972625e6c0aSTor Egge 			 * of restarts we use our marker to remember
973625e6c0aSTor Egge 			 * our place.
974625e6c0aSTor Egge 			 *
975625e6c0aSTor Egge 			 */
976625e6c0aSTor Egge 			TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE].pl,
977625e6c0aSTor Egge 					   m, &marker, pageq);
978625e6c0aSTor Egge 			/*
9792b6b0df7SMatthew Dillon 			 * The object is already known NOT to be dead.   It
9802b6b0df7SMatthew Dillon 			 * is possible for the vget() to block the whole
9812b6b0df7SMatthew Dillon 			 * pageout daemon, but the new low-memory handling
9822b6b0df7SMatthew Dillon 			 * code should prevent it.
9831c7c3c6aSMatthew Dillon 			 *
9842b6b0df7SMatthew Dillon 			 * The previous code skipped locked vnodes and, worse,
9852b6b0df7SMatthew Dillon 			 * reordered pages in the queue.  This results in
9862b6b0df7SMatthew Dillon 			 * completely non-deterministic operation and, on a
9872b6b0df7SMatthew Dillon 			 * busy system, can lead to extremely non-optimal
9882b6b0df7SMatthew Dillon 			 * pageouts.  For example, it can cause clean pages
9892b6b0df7SMatthew Dillon 			 * to be freed and dirty pages to be moved to the end
9902b6b0df7SMatthew Dillon 			 * of the queue.  Since dirty pages are also moved to
9912b6b0df7SMatthew Dillon 			 * the end of the queue once-cleaned, this gives
9922b6b0df7SMatthew Dillon 			 * way too large a weighting to defering the freeing
9932b6b0df7SMatthew Dillon 			 * of dirty pages.
9941c7c3c6aSMatthew Dillon 			 *
99523b59018SMatthew Dillon 			 * We can't wait forever for the vnode lock, we might
99623b59018SMatthew Dillon 			 * deadlock due to a vn_read() getting stuck in
99723b59018SMatthew Dillon 			 * vm_wait while holding this vnode.  We skip the
99823b59018SMatthew Dillon 			 * vnode if we can't get it in a reasonable amount
99923b59018SMatthew Dillon 			 * of time.
10001c7c3c6aSMatthew Dillon 			 */
10011c7c3c6aSMatthew Dillon 			if (object->type == OBJT_VNODE) {
10022965a453SKip Macy 				vm_page_unlock_queues();
10032965a453SKip Macy 				vm_page_unlock(m);
100424a1cce3SDavid Greenman 				vp = object->handle;
1005db27dcc0STor Egge 				if (vp->v_type == VREG &&
1006db27dcc0STor Egge 				    vn_start_write(vp, &mp, V_NOWAIT) != 0) {
10076129343dSKonstantin Belousov 					mp = NULL;
1008db27dcc0STor Egge 					++pageout_lock_miss;
1009db27dcc0STor Egge 					if (object->flags & OBJ_MIGHTBEDIRTY)
1010db27dcc0STor Egge 						vnodes_skipped++;
10112965a453SKip Macy 					vm_page_lock_queues();
1012625e6c0aSTor Egge 					goto unlock_and_continue;
1013db27dcc0STor Egge 				}
1014b9f180d1SKonstantin Belousov 				KASSERT(mp != NULL,
1015b9f180d1SKonstantin Belousov 				    ("vp %p with NULL v_mount", vp));
101614137dc0SAlan Cox 				vm_object_reference_locked(object);
10173562af12SAlan Cox 				VM_OBJECT_UNLOCK(object);
101897824da3SAlan Cox 				vfslocked = VFS_LOCK_GIANT(vp->v_mount);
101997824da3SAlan Cox 				if (vget(vp, LK_EXCLUSIVE | LK_TIMELOCK,
102097824da3SAlan Cox 				    curthread)) {
10213562af12SAlan Cox 					VM_OBJECT_LOCK(object);
10223e1b578aSAlan Cox 					vm_page_lock_queues();
102323b59018SMatthew Dillon 					++pageout_lock_miss;
1024aef922f5SJohn Dyson 					if (object->flags & OBJ_MIGHTBEDIRTY)
1025925a3a41SJohn Dyson 						vnodes_skipped++;
1026625e6c0aSTor Egge 					vp = NULL;
1027625e6c0aSTor Egge 					goto unlock_and_continue;
102885a376ebSJohn Dyson 				}
10293562af12SAlan Cox 				VM_OBJECT_LOCK(object);
10302965a453SKip Macy 				vm_page_lock(m);
10313e1b578aSAlan Cox 				vm_page_lock_queues();
1032f35329acSJohn Dyson 				/*
1033936524aaSMatthew Dillon 				 * The page might have been moved to another
1034936524aaSMatthew Dillon 				 * queue during potential blocking in vget()
1035936524aaSMatthew Dillon 				 * above.  The page might have been freed and
103614137dc0SAlan Cox 				 * reused for another vnode.
1037f35329acSJohn Dyson 				 */
10389cf51988SAlan Cox 				if (m->queue != PQ_INACTIVE ||
1039936524aaSMatthew Dillon 				    m->object != object ||
1040625e6c0aSTor Egge 				    TAILQ_NEXT(m, pageq) != &marker) {
10412965a453SKip Macy 					vm_page_unlock(m);
1042b182ec9eSJohn Dyson 					if (object->flags & OBJ_MIGHTBEDIRTY)
1043925a3a41SJohn Dyson 						vnodes_skipped++;
10443562af12SAlan Cox 					goto unlock_and_continue;
1045b182ec9eSJohn Dyson 				}
1046b182ec9eSJohn Dyson 
1047f35329acSJohn Dyson 				/*
1048936524aaSMatthew Dillon 				 * The page may have been busied during the
104914137dc0SAlan Cox 				 * blocking in vget().  We don't move the
1050936524aaSMatthew Dillon 				 * page back onto the end of the queue so that
1051936524aaSMatthew Dillon 				 * statistics are more correct if we don't.
1052f35329acSJohn Dyson 				 */
10539af80719SAlan Cox 				if (m->busy || (m->oflags & VPO_BUSY)) {
10542965a453SKip Macy 					vm_page_unlock(m);
10553562af12SAlan Cox 					goto unlock_and_continue;
1056b182ec9eSJohn Dyson 				}
1057b182ec9eSJohn Dyson 
1058f35329acSJohn Dyson 				/*
105957601bcbSMatthew Dillon 				 * If the page has become held it might
106057601bcbSMatthew Dillon 				 * be undergoing I/O, so skip it
1061f35329acSJohn Dyson 				 */
1062b182ec9eSJohn Dyson 				if (m->hold_count) {
10632965a453SKip Macy 					vm_page_unlock(m);
1064e5b006ffSAlan Cox 					vm_page_requeue(m);
1065b182ec9eSJohn Dyson 					if (object->flags & OBJ_MIGHTBEDIRTY)
1066925a3a41SJohn Dyson 						vnodes_skipped++;
10673562af12SAlan Cox 					goto unlock_and_continue;
1068f6b04d2bSDavid Greenman 				}
1069f6b04d2bSDavid Greenman 			}
1070f6b04d2bSDavid Greenman 
10710d94caffSDavid Greenman 			/*
10720d94caffSDavid Greenman 			 * If a page is dirty, then it is either being washed
10730d94caffSDavid Greenman 			 * (but not yet cleaned) or it is still in the
10740d94caffSDavid Greenman 			 * laundry.  If it is still in the laundry, then we
10752b6b0df7SMatthew Dillon 			 * start the cleaning operation.
1076936524aaSMatthew Dillon 			 *
10772b6b0df7SMatthew Dillon 			 * decrement page_shortage on success to account for
10782b6b0df7SMatthew Dillon 			 * the (future) cleaned page.  Otherwise we could wind
10792b6b0df7SMatthew Dillon 			 * up laundering or cleaning too many pages.
10800d94caffSDavid Greenman 			 */
10812965a453SKip Macy 			vm_page_unlock_queues();
10822b6b0df7SMatthew Dillon 			if (vm_pageout_clean(m) != 0) {
10832b6b0df7SMatthew Dillon 				--page_shortage;
1084936524aaSMatthew Dillon 				--maxlaunder;
10852b6b0df7SMatthew Dillon 			}
10862965a453SKip Macy 			vm_page_lock_queues();
10873562af12SAlan Cox unlock_and_continue:
10882965a453SKip Macy 			vm_page_lock_assert(m, MA_NOTOWNED);
10896989c456SAlan Cox 			VM_OBJECT_UNLOCK(object);
109014137dc0SAlan Cox 			if (mp != NULL) {
10916989c456SAlan Cox 				vm_page_unlock_queues();
109214137dc0SAlan Cox 				if (vp != NULL)
1093f6b04d2bSDavid Greenman 					vput(vp);
109497824da3SAlan Cox 				VFS_UNLOCK_GIANT(vfslocked);
109514137dc0SAlan Cox 				vm_object_deallocate(object);
1096f2a2857bSKirk McKusick 				vn_finished_write(mp);
10976989c456SAlan Cox 				vm_page_lock_queues();
10986989c456SAlan Cox 			}
1099625e6c0aSTor Egge 			next = TAILQ_NEXT(&marker, pageq);
1100625e6c0aSTor Egge 			TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl,
1101625e6c0aSTor Egge 				     &marker, pageq);
11022965a453SKip Macy 			vm_page_lock_assert(m, MA_NOTOWNED);
11036989c456SAlan Cox 			continue;
1104f2a2857bSKirk McKusick 		}
11052965a453SKip Macy 		vm_page_unlock(m);
11063562af12SAlan Cox 		VM_OBJECT_UNLOCK(object);
11070d94caffSDavid Greenman 	}
110826f9a767SRodney W. Grimes 
1109df8bae1dSRodney W. Grimes 	/*
1110936524aaSMatthew Dillon 	 * Compute the number of pages we want to try to move from the
1111936524aaSMatthew Dillon 	 * active queue to the inactive queue.
11121c7c3c6aSMatthew Dillon 	 */
11132feb50bfSAttilio Rao 	page_shortage = vm_paging_target() +
11142feb50bfSAttilio Rao 		cnt.v_inactive_target - cnt.v_inactive_count;
1115b182ec9eSJohn Dyson 	page_shortage += addl_page_shortage;
11161c7c3c6aSMatthew Dillon 
11171c7c3c6aSMatthew Dillon 	/*
1118936524aaSMatthew Dillon 	 * Scan the active queue for things we can deactivate. We nominally
1119936524aaSMatthew Dillon 	 * track the per-page activity counter and use it to locate
1120936524aaSMatthew Dillon 	 * deactivation candidates.
11211c7c3c6aSMatthew Dillon 	 */
11222feb50bfSAttilio Rao 	pcount = cnt.v_active_count;
1123be72f788SAlan Cox 	m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
11242965a453SKip Macy 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
11251c7c3c6aSMatthew Dillon 
1126b18bfc3dSJohn Dyson 	while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) {
1127f35329acSJohn Dyson 
11289cf51988SAlan Cox 		KASSERT(m->queue == PQ_ACTIVE,
1129d3c09dd7SAlan Cox 		    ("vm_pageout_scan: page %p isn't active", m));
1130f35329acSJohn Dyson 
1131b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(m, pageq);
11328dbca793STor Egge 		if ((m->flags & PG_MARKER) != 0) {
11338dbca793STor Egge 			m = next;
11348dbca793STor Egge 			continue;
11358dbca793STor Egge 		}
11369ee2165fSAlan Cox 		if (!vm_pageout_page_lock(m, &next)) {
11378c616246SKonstantin Belousov 			vm_page_unlock(m);
11382965a453SKip Macy 			m = next;
11392965a453SKip Macy 			continue;
11402965a453SKip Macy 		}
11419ee2165fSAlan Cox 		object = m->object;
11428dbca793STor Egge 		if (!VM_OBJECT_TRYLOCK(object) &&
11438dbca793STor Egge 		    !vm_pageout_fallback_object_lock(m, &next)) {
11448dbca793STor Egge 			VM_OBJECT_UNLOCK(object);
11452965a453SKip Macy 			vm_page_unlock(m);
11464b8a5c40SAlan Cox 			m = next;
1147b08abf6cSAlan Cox 			continue;
1148b08abf6cSAlan Cox 		}
1149b08abf6cSAlan Cox 
1150df8bae1dSRodney W. Grimes 		/*
115126f9a767SRodney W. Grimes 		 * Don't deactivate pages that are busy.
1152df8bae1dSRodney W. Grimes 		 */
1153a647a309SDavid Greenman 		if ((m->busy != 0) ||
11549af80719SAlan Cox 		    (m->oflags & VPO_BUSY) ||
1155f6b04d2bSDavid Greenman 		    (m->hold_count != 0)) {
11562965a453SKip Macy 			vm_page_unlock(m);
1157b08abf6cSAlan Cox 			VM_OBJECT_UNLOCK(object);
1158e5b006ffSAlan Cox 			vm_page_requeue(m);
115926f9a767SRodney W. Grimes 			m = next;
116026f9a767SRodney W. Grimes 			continue;
1161df8bae1dSRodney W. Grimes 		}
1162b18bfc3dSJohn Dyson 
1163b18bfc3dSJohn Dyson 		/*
1164b18bfc3dSJohn Dyson 		 * The count for pagedaemon pages is done after checking the
1165956f3135SPhilippe Charnier 		 * page for eligibility...
1166b18bfc3dSJohn Dyson 		 */
1167393a081dSAttilio Rao 		cnt.v_pdpages++;
1168ef743ce6SJohn Dyson 
11697e006499SJohn Dyson 		/*
11707e006499SJohn Dyson 		 * Check to see "how much" the page has been used.
11717e006499SJohn Dyson 		 */
11727e006499SJohn Dyson 		actcount = 0;
1173b08abf6cSAlan Cox 		if (object->ref_count != 0) {
1174ef743ce6SJohn Dyson 			if (m->flags & PG_REFERENCED) {
11757e006499SJohn Dyson 				actcount += 1;
11760d94caffSDavid Greenman 			}
11770385347cSPeter Wemm 			actcount += pmap_ts_referenced(m);
11787e006499SJohn Dyson 			if (actcount) {
11797e006499SJohn Dyson 				m->act_count += ACT_ADVANCE + actcount;
118038efa82bSJohn Dyson 				if (m->act_count > ACT_MAX)
118138efa82bSJohn Dyson 					m->act_count = ACT_MAX;
118238efa82bSJohn Dyson 			}
1183b18bfc3dSJohn Dyson 		}
1184ef743ce6SJohn Dyson 
11857e006499SJohn Dyson 		/*
11867e006499SJohn Dyson 		 * Since we have "tested" this bit, we need to clear it now.
11877e006499SJohn Dyson 		 */
1188e69763a3SDoug Rabson 		vm_page_flag_clear(m, PG_REFERENCED);
1189ef743ce6SJohn Dyson 
11907e006499SJohn Dyson 		/*
11917e006499SJohn Dyson 		 * Only if an object is currently being used, do we use the
11927e006499SJohn Dyson 		 * page activation count stats.
11937e006499SJohn Dyson 		 */
1194b08abf6cSAlan Cox 		if (actcount && (object->ref_count != 0)) {
1195e5b006ffSAlan Cox 			vm_page_requeue(m);
119626f9a767SRodney W. Grimes 		} else {
119738efa82bSJohn Dyson 			m->act_count -= min(m->act_count, ACT_DECLINE);
11982b6b0df7SMatthew Dillon 			if (vm_pageout_algorithm ||
1199b08abf6cSAlan Cox 			    object->ref_count == 0 ||
12002b6b0df7SMatthew Dillon 			    m->act_count == 0) {
1201925a3a41SJohn Dyson 				page_shortage--;
1202b08abf6cSAlan Cox 				if (object->ref_count == 0) {
12035d4a7b79SAlan Cox 					KASSERT(!pmap_page_is_mapped(m),
12045d4a7b79SAlan Cox 				    ("vm_pageout_scan: page %p is mapped", m));
1205d4a272dbSJohn Dyson 					if (m->dirty == 0)
12060d94caffSDavid Greenman 						vm_page_cache(m);
1207d4a272dbSJohn Dyson 					else
1208d4a272dbSJohn Dyson 						vm_page_deactivate(m);
12090d94caffSDavid Greenman 				} else {
121026f9a767SRodney W. Grimes 					vm_page_deactivate(m);
1211df8bae1dSRodney W. Grimes 				}
121238efa82bSJohn Dyson 			} else {
1213e5b006ffSAlan Cox 				vm_page_requeue(m);
121438efa82bSJohn Dyson 			}
1215df8bae1dSRodney W. Grimes 		}
12162965a453SKip Macy 		vm_page_unlock(m);
1217b08abf6cSAlan Cox 		VM_OBJECT_UNLOCK(object);
121826f9a767SRodney W. Grimes 		m = next;
121926f9a767SRodney W. Grimes 	}
12208ffc1519SAlan Cox 	vm_page_unlock_queues();
1221ceb0cf87SJohn Dyson #if !defined(NO_SWAPPING)
1222ceb0cf87SJohn Dyson 	/*
1223ceb0cf87SJohn Dyson 	 * Idle process swapout -- run once per second.
1224ceb0cf87SJohn Dyson 	 */
1225ceb0cf87SJohn Dyson 	if (vm_swap_idle_enabled) {
1226ceb0cf87SJohn Dyson 		static long lsec;
1227227ee8a1SPoul-Henning Kamp 		if (time_second != lsec) {
122897824da3SAlan Cox 			vm_req_vmdaemon(VM_SWAP_IDLE);
1229227ee8a1SPoul-Henning Kamp 			lsec = time_second;
1230ceb0cf87SJohn Dyson 		}
1231ceb0cf87SJohn Dyson 	}
1232ceb0cf87SJohn Dyson #endif
1233ceb0cf87SJohn Dyson 
12345663e6deSDavid Greenman 	/*
1235f6b04d2bSDavid Greenman 	 * If we didn't get enough free pages, and we have skipped a vnode
12364c1f8ee9SDavid Greenman 	 * in a writeable object, wakeup the sync daemon.  And kick swapout
12374c1f8ee9SDavid Greenman 	 * if we did not get enough free pages.
1238f6b04d2bSDavid Greenman 	 */
123990ecac61SMatthew Dillon 	if (vm_paging_target() > 0) {
124090ecac61SMatthew Dillon 		if (vnodes_skipped && vm_page_count_min())
1241d50c1994SPeter Wemm 			(void) speedup_syncer();
124238efa82bSJohn Dyson #if !defined(NO_SWAPPING)
124397824da3SAlan Cox 		if (vm_swap_enabled && vm_page_count_target())
124497824da3SAlan Cox 			vm_req_vmdaemon(VM_SWAP_NORMAL);
12455afce282SDavid Greenman #endif
12464c1f8ee9SDavid Greenman 	}
12474c1f8ee9SDavid Greenman 
1248f6b04d2bSDavid Greenman 	/*
1249e92686d0SDavid Schultz 	 * If we are critically low on one of RAM or swap and low on
1250e92686d0SDavid Schultz 	 * the other, kill the largest process.  However, we avoid
1251e92686d0SDavid Schultz 	 * doing this on the first pass in order to give ourselves a
1252e92686d0SDavid Schultz 	 * chance to flush out dirty vnode-backed pages and to allow
1253e92686d0SDavid Schultz 	 * active pages to be moved to the inactive queue and reclaimed.
12542025d69bSKonstantin Belousov 	 */
12552025d69bSKonstantin Belousov 	if (pass != 0 &&
12562025d69bSKonstantin Belousov 	    ((swap_pager_avail < 64 && vm_page_count_min()) ||
12572025d69bSKonstantin Belousov 	     (swap_pager_full && vm_paging_target() > 0)))
12582025d69bSKonstantin Belousov 		vm_pageout_oom(VM_OOM_MEM);
12592025d69bSKonstantin Belousov }
12602025d69bSKonstantin Belousov 
12612025d69bSKonstantin Belousov 
12622025d69bSKonstantin Belousov void
12632025d69bSKonstantin Belousov vm_pageout_oom(int shortage)
12642025d69bSKonstantin Belousov {
12652025d69bSKonstantin Belousov 	struct proc *p, *bigproc;
12662025d69bSKonstantin Belousov 	vm_offset_t size, bigsize;
12672025d69bSKonstantin Belousov 	struct thread *td;
12686bed074cSKonstantin Belousov 	struct vmspace *vm;
12692025d69bSKonstantin Belousov 
12702025d69bSKonstantin Belousov 	/*
12711c58e4e5SJohn Baldwin 	 * We keep the process bigproc locked once we find it to keep anyone
12721c58e4e5SJohn Baldwin 	 * from messing with it; however, there is a possibility of
12731c58e4e5SJohn Baldwin 	 * deadlock if process B is bigproc and one of it's child processes
12741c58e4e5SJohn Baldwin 	 * attempts to propagate a signal to B while we are waiting for A's
12751c58e4e5SJohn Baldwin 	 * lock while walking this list.  To avoid this, we don't block on
12761c58e4e5SJohn Baldwin 	 * the process lock but just skip a process if it is already locked.
12775663e6deSDavid Greenman 	 */
12785663e6deSDavid Greenman 	bigproc = NULL;
12795663e6deSDavid Greenman 	bigsize = 0;
12801005a129SJohn Baldwin 	sx_slock(&allproc_lock);
1281e602ba25SJulian Elischer 	FOREACH_PROC_IN_SYSTEM(p) {
1282e602ba25SJulian Elischer 		int breakout;
1283dcbcd518SBruce Evans 
12841c58e4e5SJohn Baldwin 		if (PROC_TRYLOCK(p) == 0)
12851c58e4e5SJohn Baldwin 			continue;
12861c58e4e5SJohn Baldwin 		/*
12873f1c4c4fSKonstantin Belousov 		 * If this is a system, protected or killed process, skip it.
12885663e6deSDavid Greenman 		 */
12896bed074cSKonstantin Belousov 		if ((p->p_flag & (P_INEXEC | P_PROTECTED | P_SYSTEM)) ||
12903f1c4c4fSKonstantin Belousov 		    (p->p_pid == 1) || P_KILLED(p) ||
12918f60c087SPoul-Henning Kamp 		    ((p->p_pid < 48) && (swap_pager_avail != 0))) {
12928606d880SJohn Baldwin 			PROC_UNLOCK(p);
12935663e6deSDavid Greenman 			continue;
12945663e6deSDavid Greenman 		}
12955663e6deSDavid Greenman 		/*
1296dcbcd518SBruce Evans 		 * If the process is in a non-running type state,
1297e602ba25SJulian Elischer 		 * don't touch it.  Check all the threads individually.
12985663e6deSDavid Greenman 		 */
1299e602ba25SJulian Elischer 		breakout = 0;
1300e602ba25SJulian Elischer 		FOREACH_THREAD_IN_PROC(p, td) {
1301982d11f8SJeff Roberson 			thread_lock(td);
130271fad9fdSJulian Elischer 			if (!TD_ON_RUNQ(td) &&
130371fad9fdSJulian Elischer 			    !TD_IS_RUNNING(td) &&
130471fad9fdSJulian Elischer 			    !TD_IS_SLEEPING(td)) {
1305982d11f8SJeff Roberson 				thread_unlock(td);
1306e602ba25SJulian Elischer 				breakout = 1;
1307e602ba25SJulian Elischer 				break;
1308e602ba25SJulian Elischer 			}
1309982d11f8SJeff Roberson 			thread_unlock(td);
1310e602ba25SJulian Elischer 		}
1311e602ba25SJulian Elischer 		if (breakout) {
13121c58e4e5SJohn Baldwin 			PROC_UNLOCK(p);
13135663e6deSDavid Greenman 			continue;
13145663e6deSDavid Greenman 		}
13155663e6deSDavid Greenman 		/*
13165663e6deSDavid Greenman 		 * get the process size
13175663e6deSDavid Greenman 		 */
13186bed074cSKonstantin Belousov 		vm = vmspace_acquire_ref(p);
13196bed074cSKonstantin Belousov 		if (vm == NULL) {
13206bed074cSKonstantin Belousov 			PROC_UNLOCK(p);
13216bed074cSKonstantin Belousov 			continue;
13226bed074cSKonstantin Belousov 		}
13236bed074cSKonstantin Belousov 		if (!vm_map_trylock_read(&vm->vm_map)) {
13246bed074cSKonstantin Belousov 			vmspace_free(vm);
132572d97679SDavid Schultz 			PROC_UNLOCK(p);
132672d97679SDavid Schultz 			continue;
132772d97679SDavid Schultz 		}
13287981aa24SKonstantin Belousov 		size = vmspace_swap_count(vm);
13296bed074cSKonstantin Belousov 		vm_map_unlock_read(&vm->vm_map);
13302025d69bSKonstantin Belousov 		if (shortage == VM_OOM_MEM)
13316bed074cSKonstantin Belousov 			size += vmspace_resident_count(vm);
13326bed074cSKonstantin Belousov 		vmspace_free(vm);
13335663e6deSDavid Greenman 		/*
13345663e6deSDavid Greenman 		 * if the this process is bigger than the biggest one
13355663e6deSDavid Greenman 		 * remember it.
13365663e6deSDavid Greenman 		 */
13375663e6deSDavid Greenman 		if (size > bigsize) {
13381c58e4e5SJohn Baldwin 			if (bigproc != NULL)
13391c58e4e5SJohn Baldwin 				PROC_UNLOCK(bigproc);
13405663e6deSDavid Greenman 			bigproc = p;
13415663e6deSDavid Greenman 			bigsize = size;
13421c58e4e5SJohn Baldwin 		} else
13431c58e4e5SJohn Baldwin 			PROC_UNLOCK(p);
13445663e6deSDavid Greenman 	}
13451005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
13465663e6deSDavid Greenman 	if (bigproc != NULL) {
1347729b1e51SDavid Greenman 		killproc(bigproc, "out of swap space");
1348fa885116SJulian Elischer 		sched_nice(bigproc, PRIO_MIN);
13491c58e4e5SJohn Baldwin 		PROC_UNLOCK(bigproc);
13502feb50bfSAttilio Rao 		wakeup(&cnt.v_free_count);
13515663e6deSDavid Greenman 	}
13525663e6deSDavid Greenman }
135326f9a767SRodney W. Grimes 
1354dc2efb27SJohn Dyson /*
1355dc2efb27SJohn Dyson  * This routine tries to maintain the pseudo LRU active queue,
1356dc2efb27SJohn Dyson  * so that during long periods of time where there is no paging,
1357956f3135SPhilippe Charnier  * that some statistic accumulation still occurs.  This code
1358dc2efb27SJohn Dyson  * helps the situation where paging just starts to occur.
1359dc2efb27SJohn Dyson  */
1360dc2efb27SJohn Dyson static void
1361dc2efb27SJohn Dyson vm_pageout_page_stats()
1362dc2efb27SJohn Dyson {
1363b86e6ec0SAlan Cox 	vm_object_t object;
1364dc2efb27SJohn Dyson 	vm_page_t m,next;
1365dc2efb27SJohn Dyson 	int pcount,tpcount;		/* Number of pages to check */
1366dc2efb27SJohn Dyson 	static int fullintervalcount = 0;
1367bef608bdSJohn Dyson 	int page_shortage;
1368bef608bdSJohn Dyson 
136990ecac61SMatthew Dillon 	page_shortage =
13702feb50bfSAttilio Rao 	    (cnt.v_inactive_target + cnt.v_cache_max + cnt.v_free_min) -
13712feb50bfSAttilio Rao 	    (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count);
137290ecac61SMatthew Dillon 
1373bef608bdSJohn Dyson 	if (page_shortage <= 0)
1374bef608bdSJohn Dyson 		return;
1375dc2efb27SJohn Dyson 
1376cdaba1f2SAlan Cox 	vm_page_lock_queues();
13772feb50bfSAttilio Rao 	pcount = cnt.v_active_count;
1378dc2efb27SJohn Dyson 	fullintervalcount += vm_pageout_stats_interval;
1379dc2efb27SJohn Dyson 	if (fullintervalcount < vm_pageout_full_stats_interval) {
13808d28bf04SAlan Cox 		tpcount = (int64_t)vm_pageout_stats_max * cnt.v_active_count /
13818d28bf04SAlan Cox 		    cnt.v_page_count;
1382dc2efb27SJohn Dyson 		if (pcount > tpcount)
1383dc2efb27SJohn Dyson 			pcount = tpcount;
1384883f3caaSMatthew Dillon 	} else {
1385883f3caaSMatthew Dillon 		fullintervalcount = 0;
1386dc2efb27SJohn Dyson 	}
1387dc2efb27SJohn Dyson 
1388be72f788SAlan Cox 	m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
1389dc2efb27SJohn Dyson 	while ((m != NULL) && (pcount-- > 0)) {
13907e006499SJohn Dyson 		int actcount;
1391dc2efb27SJohn Dyson 
13929cf51988SAlan Cox 		KASSERT(m->queue == PQ_ACTIVE,
1393ab42316cSAlan Cox 		    ("vm_pageout_page_stats: page %p isn't active", m));
1394dc2efb27SJohn Dyson 
1395dc2efb27SJohn Dyson 		next = TAILQ_NEXT(m, pageq);
13968dbca793STor Egge 		if ((m->flags & PG_MARKER) != 0) {
13978dbca793STor Egge 			m = next;
13988dbca793STor Egge 			continue;
13998dbca793STor Egge 		}
14002965a453SKip Macy 		vm_page_lock_assert(m, MA_NOTOWNED);
14019ee2165fSAlan Cox 		if (!vm_pageout_page_lock(m, &next)) {
14028c616246SKonstantin Belousov 			vm_page_unlock(m);
14032965a453SKip Macy 			m = next;
14042965a453SKip Macy 			continue;
14052965a453SKip Macy 		}
14069ee2165fSAlan Cox 		object = m->object;
14078dbca793STor Egge 		if (!VM_OBJECT_TRYLOCK(object) &&
14088dbca793STor Egge 		    !vm_pageout_fallback_object_lock(m, &next)) {
14098dbca793STor Egge 			VM_OBJECT_UNLOCK(object);
14102965a453SKip Macy 			vm_page_unlock(m);
1411b86e6ec0SAlan Cox 			m = next;
1412b86e6ec0SAlan Cox 			continue;
1413b86e6ec0SAlan Cox 		}
1414b86e6ec0SAlan Cox 
1415dc2efb27SJohn Dyson 		/*
1416dc2efb27SJohn Dyson 		 * Don't deactivate pages that are busy.
1417dc2efb27SJohn Dyson 		 */
1418dc2efb27SJohn Dyson 		if ((m->busy != 0) ||
14199af80719SAlan Cox 		    (m->oflags & VPO_BUSY) ||
1420dc2efb27SJohn Dyson 		    (m->hold_count != 0)) {
14212965a453SKip Macy 			vm_page_unlock(m);
1422b86e6ec0SAlan Cox 			VM_OBJECT_UNLOCK(object);
1423e5b006ffSAlan Cox 			vm_page_requeue(m);
1424dc2efb27SJohn Dyson 			m = next;
1425dc2efb27SJohn Dyson 			continue;
1426dc2efb27SJohn Dyson 		}
1427dc2efb27SJohn Dyson 
14287e006499SJohn Dyson 		actcount = 0;
1429dc2efb27SJohn Dyson 		if (m->flags & PG_REFERENCED) {
1430e69763a3SDoug Rabson 			vm_page_flag_clear(m, PG_REFERENCED);
14317e006499SJohn Dyson 			actcount += 1;
1432dc2efb27SJohn Dyson 		}
1433dc2efb27SJohn Dyson 
14340385347cSPeter Wemm 		actcount += pmap_ts_referenced(m);
14357e006499SJohn Dyson 		if (actcount) {
14367e006499SJohn Dyson 			m->act_count += ACT_ADVANCE + actcount;
1437dc2efb27SJohn Dyson 			if (m->act_count > ACT_MAX)
1438dc2efb27SJohn Dyson 				m->act_count = ACT_MAX;
1439e5b006ffSAlan Cox 			vm_page_requeue(m);
1440dc2efb27SJohn Dyson 		} else {
1441dc2efb27SJohn Dyson 			if (m->act_count == 0) {
14427e006499SJohn Dyson 				/*
14432b6b0df7SMatthew Dillon 				 * We turn off page access, so that we have
14442b6b0df7SMatthew Dillon 				 * more accurate RSS stats.  We don't do this
14452b6b0df7SMatthew Dillon 				 * in the normal page deactivation when the
14462b6b0df7SMatthew Dillon 				 * system is loaded VM wise, because the
14472b6b0df7SMatthew Dillon 				 * cost of the large number of page protect
14482b6b0df7SMatthew Dillon 				 * operations would be higher than the value
14492b6b0df7SMatthew Dillon 				 * of doing the operation.
14507e006499SJohn Dyson 				 */
14514fec79beSAlan Cox 				pmap_remove_all(m);
1452dc2efb27SJohn Dyson 				vm_page_deactivate(m);
1453dc2efb27SJohn Dyson 			} else {
1454dc2efb27SJohn Dyson 				m->act_count -= min(m->act_count, ACT_DECLINE);
1455e5b006ffSAlan Cox 				vm_page_requeue(m);
1456dc2efb27SJohn Dyson 			}
1457dc2efb27SJohn Dyson 		}
14582965a453SKip Macy 		vm_page_unlock(m);
1459b86e6ec0SAlan Cox 		VM_OBJECT_UNLOCK(object);
1460dc2efb27SJohn Dyson 		m = next;
1461dc2efb27SJohn Dyson 	}
1462cdaba1f2SAlan Cox 	vm_page_unlock_queues();
1463dc2efb27SJohn Dyson }
1464dc2efb27SJohn Dyson 
1465df8bae1dSRodney W. Grimes /*
1466df8bae1dSRodney W. Grimes  *	vm_pageout is the high level pageout daemon.
1467df8bae1dSRodney W. Grimes  */
14682b14f991SJulian Elischer static void
146926f9a767SRodney W. Grimes vm_pageout()
1470df8bae1dSRodney W. Grimes {
14711aab16a6SAlan Cox 	int error, pass;
14720384fff8SJason Evans 
1473df8bae1dSRodney W. Grimes 	/*
1474df8bae1dSRodney W. Grimes 	 * Initialize some paging parameters.
1475df8bae1dSRodney W. Grimes 	 */
14762feb50bfSAttilio Rao 	cnt.v_interrupt_free_min = 2;
14772feb50bfSAttilio Rao 	if (cnt.v_page_count < 2000)
1478f35329acSJohn Dyson 		vm_pageout_page_count = 8;
1479f6b04d2bSDavid Greenman 
148045ae1d91SAlan Cox 	/*
148145ae1d91SAlan Cox 	 * v_free_reserved needs to include enough for the largest
148245ae1d91SAlan Cox 	 * swap pager structures plus enough for any pv_entry structs
148345ae1d91SAlan Cox 	 * when paging.
148445ae1d91SAlan Cox 	 */
14852feb50bfSAttilio Rao 	if (cnt.v_page_count > 1024)
14862feb50bfSAttilio Rao 		cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200;
14872feb50bfSAttilio Rao 	else
14882feb50bfSAttilio Rao 		cnt.v_free_min = 4;
14892feb50bfSAttilio Rao 	cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE +
14902feb50bfSAttilio Rao 	    cnt.v_interrupt_free_min;
14912feb50bfSAttilio Rao 	cnt.v_free_reserved = vm_pageout_page_count +
14922446e4f0SAlan Cox 	    cnt.v_pageout_free_min + (cnt.v_page_count / 768);
14932feb50bfSAttilio Rao 	cnt.v_free_severe = cnt.v_free_min / 2;
14942feb50bfSAttilio Rao 	cnt.v_free_min += cnt.v_free_reserved;
14952feb50bfSAttilio Rao 	cnt.v_free_severe += cnt.v_free_reserved;
149645ae1d91SAlan Cox 
1497ed74321bSDavid Greenman 	/*
14982b6b0df7SMatthew Dillon 	 * v_free_target and v_cache_min control pageout hysteresis.  Note
14992b6b0df7SMatthew Dillon 	 * that these are more a measure of the VM cache queue hysteresis
15002b6b0df7SMatthew Dillon 	 * then the VM free queue.  Specifically, v_free_target is the
15012b6b0df7SMatthew Dillon 	 * high water mark (free+cache pages).
15022b6b0df7SMatthew Dillon 	 *
15032b6b0df7SMatthew Dillon 	 * v_free_reserved + v_cache_min (mostly means v_cache_min) is the
15042b6b0df7SMatthew Dillon 	 * low water mark, while v_free_min is the stop.  v_cache_min must
15052b6b0df7SMatthew Dillon 	 * be big enough to handle memory needs while the pageout daemon
15062b6b0df7SMatthew Dillon 	 * is signalled and run to free more pages.
1507ed74321bSDavid Greenman 	 */
15082feb50bfSAttilio Rao 	if (cnt.v_free_count > 6144)
15092feb50bfSAttilio Rao 		cnt.v_free_target = 4 * cnt.v_free_min + cnt.v_free_reserved;
15102feb50bfSAttilio Rao 	else
15112feb50bfSAttilio Rao 		cnt.v_free_target = 2 * cnt.v_free_min + cnt.v_free_reserved;
15126f2b142eSDavid Greenman 
15132feb50bfSAttilio Rao 	if (cnt.v_free_count > 2048) {
15142feb50bfSAttilio Rao 		cnt.v_cache_min = cnt.v_free_target;
15152feb50bfSAttilio Rao 		cnt.v_cache_max = 2 * cnt.v_cache_min;
15162feb50bfSAttilio Rao 		cnt.v_inactive_target = (3 * cnt.v_free_target) / 2;
15170d94caffSDavid Greenman 	} else {
15182feb50bfSAttilio Rao 		cnt.v_cache_min = 0;
15192feb50bfSAttilio Rao 		cnt.v_cache_max = 0;
15202feb50bfSAttilio Rao 		cnt.v_inactive_target = cnt.v_free_count / 4;
15210d94caffSDavid Greenman 	}
15222feb50bfSAttilio Rao 	if (cnt.v_inactive_target > cnt.v_free_count / 3)
15232feb50bfSAttilio Rao 		cnt.v_inactive_target = cnt.v_free_count / 3;
1524df8bae1dSRodney W. Grimes 
1525df8bae1dSRodney W. Grimes 	/* XXX does not really belong here */
1526df8bae1dSRodney W. Grimes 	if (vm_page_max_wired == 0)
15272feb50bfSAttilio Rao 		vm_page_max_wired = cnt.v_free_count / 3;
1528df8bae1dSRodney W. Grimes 
1529dc2efb27SJohn Dyson 	if (vm_pageout_stats_max == 0)
15302feb50bfSAttilio Rao 		vm_pageout_stats_max = cnt.v_free_target;
1531dc2efb27SJohn Dyson 
1532dc2efb27SJohn Dyson 	/*
1533dc2efb27SJohn Dyson 	 * Set interval in seconds for stats scan.
1534dc2efb27SJohn Dyson 	 */
1535dc2efb27SJohn Dyson 	if (vm_pageout_stats_interval == 0)
1536bef608bdSJohn Dyson 		vm_pageout_stats_interval = 5;
1537dc2efb27SJohn Dyson 	if (vm_pageout_full_stats_interval == 0)
1538dc2efb27SJohn Dyson 		vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4;
1539dc2efb27SJohn Dyson 
154024a1cce3SDavid Greenman 	swap_pager_swap_init();
15412b6b0df7SMatthew Dillon 	pass = 0;
1542df8bae1dSRodney W. Grimes 	/*
15430d94caffSDavid Greenman 	 * The pageout daemon is never done, so loop forever.
1544df8bae1dSRodney W. Grimes 	 */
1545df8bae1dSRodney W. Grimes 	while (TRUE) {
1546936524aaSMatthew Dillon 		/*
1547936524aaSMatthew Dillon 		 * If we have enough free memory, wakeup waiters.  Do
1548936524aaSMatthew Dillon 		 * not clear vm_pages_needed until we reach our target,
1549936524aaSMatthew Dillon 		 * otherwise we may be woken up over and over again and
1550936524aaSMatthew Dillon 		 * waste a lot of cpu.
1551936524aaSMatthew Dillon 		 */
1552e9f995d8SAlan Cox 		mtx_lock(&vm_page_queue_free_mtx);
1553936524aaSMatthew Dillon 		if (vm_pages_needed && !vm_page_count_min()) {
1554a1c0a785SAlan Cox 			if (!vm_paging_needed())
1555936524aaSMatthew Dillon 				vm_pages_needed = 0;
15562feb50bfSAttilio Rao 			wakeup(&cnt.v_free_count);
1557936524aaSMatthew Dillon 		}
1558936524aaSMatthew Dillon 		if (vm_pages_needed) {
155990ecac61SMatthew Dillon 			/*
15602b6b0df7SMatthew Dillon 			 * Still not done, take a second pass without waiting
15612b6b0df7SMatthew Dillon 			 * (unlimited dirty cleaning), otherwise sleep a bit
15622b6b0df7SMatthew Dillon 			 * and try again.
156390ecac61SMatthew Dillon 			 */
15642b6b0df7SMatthew Dillon 			++pass;
15652b6b0df7SMatthew Dillon 			if (pass > 1)
1566e9f995d8SAlan Cox 				msleep(&vm_pages_needed,
1567e9f995d8SAlan Cox 				    &vm_page_queue_free_mtx, PVM, "psleep",
1568e9f995d8SAlan Cox 				    hz / 2);
156990ecac61SMatthew Dillon 		} else {
157090ecac61SMatthew Dillon 			/*
15712b6b0df7SMatthew Dillon 			 * Good enough, sleep & handle stats.  Prime the pass
15722b6b0df7SMatthew Dillon 			 * for the next run.
157390ecac61SMatthew Dillon 			 */
15742b6b0df7SMatthew Dillon 			if (pass > 1)
15752b6b0df7SMatthew Dillon 				pass = 1;
15762b6b0df7SMatthew Dillon 			else
15772b6b0df7SMatthew Dillon 				pass = 0;
1578e9f995d8SAlan Cox 			error = msleep(&vm_pages_needed,
1579e9f995d8SAlan Cox 			    &vm_page_queue_free_mtx, PVM, "psleep",
1580e9f995d8SAlan Cox 			    vm_pageout_stats_interval * hz);
1581dc2efb27SJohn Dyson 			if (error && !vm_pages_needed) {
1582e9f995d8SAlan Cox 				mtx_unlock(&vm_page_queue_free_mtx);
15832b6b0df7SMatthew Dillon 				pass = 0;
1584dc2efb27SJohn Dyson 				vm_pageout_page_stats();
1585dc2efb27SJohn Dyson 				continue;
1586dc2efb27SJohn Dyson 			}
1587f919ebdeSDavid Greenman 		}
1588b18bfc3dSJohn Dyson 		if (vm_pages_needed)
1589393a081dSAttilio Rao 			cnt.v_pdwakeups++;
1590e9f995d8SAlan Cox 		mtx_unlock(&vm_page_queue_free_mtx);
15912b6b0df7SMatthew Dillon 		vm_pageout_scan(pass);
1592df8bae1dSRodney W. Grimes 	}
1593df8bae1dSRodney W. Grimes }
159426f9a767SRodney W. Grimes 
15956b4b77adSAlan Cox /*
1596e9f995d8SAlan Cox  * Unless the free page queue lock is held by the caller, this function
15976b4b77adSAlan Cox  * should be regarded as advisory.  Specifically, the caller should
15986b4b77adSAlan Cox  * not msleep() on &cnt.v_free_count following this function unless
1599e9f995d8SAlan Cox  * the free page queue lock is held until the msleep() is performed.
16006b4b77adSAlan Cox  */
1601e0c5a895SJohn Dyson void
1602e0c5a895SJohn Dyson pagedaemon_wakeup()
1603e0c5a895SJohn Dyson {
1604a1c0a785SAlan Cox 
1605b40ce416SJulian Elischer 	if (!vm_pages_needed && curthread->td_proc != pageproc) {
1606a1c0a785SAlan Cox 		vm_pages_needed = 1;
1607e0c5a895SJohn Dyson 		wakeup(&vm_pages_needed);
1608e0c5a895SJohn Dyson 	}
1609e0c5a895SJohn Dyson }
1610e0c5a895SJohn Dyson 
161138efa82bSJohn Dyson #if !defined(NO_SWAPPING)
16125afce282SDavid Greenman static void
161397824da3SAlan Cox vm_req_vmdaemon(int req)
16145afce282SDavid Greenman {
16155afce282SDavid Greenman 	static int lastrun = 0;
16165afce282SDavid Greenman 
161797824da3SAlan Cox 	mtx_lock(&vm_daemon_mtx);
161897824da3SAlan Cox 	vm_pageout_req_swapout |= req;
1619b18bfc3dSJohn Dyson 	if ((ticks > (lastrun + hz)) || (ticks < lastrun)) {
16205afce282SDavid Greenman 		wakeup(&vm_daemon_needed);
16215afce282SDavid Greenman 		lastrun = ticks;
16225afce282SDavid Greenman 	}
162397824da3SAlan Cox 	mtx_unlock(&vm_daemon_mtx);
16245afce282SDavid Greenman }
16255afce282SDavid Greenman 
16262b14f991SJulian Elischer static void
16274f9fb771SBruce Evans vm_daemon()
16280d94caffSDavid Greenman {
162991d5354aSJohn Baldwin 	struct rlimit rsslim;
1630dcbcd518SBruce Evans 	struct proc *p;
1631dcbcd518SBruce Evans 	struct thread *td;
16326bed074cSKonstantin Belousov 	struct vmspace *vm;
163397824da3SAlan Cox 	int breakout, swapout_flags;
16340d94caffSDavid Greenman 
16352fe6e4d7SDavid Greenman 	while (TRUE) {
163697824da3SAlan Cox 		mtx_lock(&vm_daemon_mtx);
163797824da3SAlan Cox 		msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", 0);
163897824da3SAlan Cox 		swapout_flags = vm_pageout_req_swapout;
16394c1f8ee9SDavid Greenman 		vm_pageout_req_swapout = 0;
164097824da3SAlan Cox 		mtx_unlock(&vm_daemon_mtx);
164197824da3SAlan Cox 		if (swapout_flags)
164297824da3SAlan Cox 			swapout_procs(swapout_flags);
164397824da3SAlan Cox 
16442fe6e4d7SDavid Greenman 		/*
16450d94caffSDavid Greenman 		 * scan the processes for exceeding their rlimits or if
16460d94caffSDavid Greenman 		 * process is swapped out -- deactivate pages
16472fe6e4d7SDavid Greenman 		 */
16481005a129SJohn Baldwin 		sx_slock(&allproc_lock);
1649f67af5c9SXin LI 		FOREACH_PROC_IN_SYSTEM(p) {
1650fe2144fdSLuoqi Chen 			vm_pindex_t limit, size;
16512fe6e4d7SDavid Greenman 
16522fe6e4d7SDavid Greenman 			/*
16532fe6e4d7SDavid Greenman 			 * if this is a system process or if we have already
16542fe6e4d7SDavid Greenman 			 * looked at this process, skip it.
16552fe6e4d7SDavid Greenman 			 */
1656897ecacdSJohn Baldwin 			PROC_LOCK(p);
16576bed074cSKonstantin Belousov 			if (p->p_flag & (P_INEXEC | P_SYSTEM | P_WEXIT)) {
1658897ecacdSJohn Baldwin 				PROC_UNLOCK(p);
16592fe6e4d7SDavid Greenman 				continue;
16602fe6e4d7SDavid Greenman 			}
16612fe6e4d7SDavid Greenman 			/*
16622fe6e4d7SDavid Greenman 			 * if the process is in a non-running type state,
16632fe6e4d7SDavid Greenman 			 * don't touch it.
16642fe6e4d7SDavid Greenman 			 */
1665e602ba25SJulian Elischer 			breakout = 0;
1666e602ba25SJulian Elischer 			FOREACH_THREAD_IN_PROC(p, td) {
1667982d11f8SJeff Roberson 				thread_lock(td);
166871fad9fdSJulian Elischer 				if (!TD_ON_RUNQ(td) &&
166971fad9fdSJulian Elischer 				    !TD_IS_RUNNING(td) &&
167071fad9fdSJulian Elischer 				    !TD_IS_SLEEPING(td)) {
1671982d11f8SJeff Roberson 					thread_unlock(td);
1672e602ba25SJulian Elischer 					breakout = 1;
1673e602ba25SJulian Elischer 					break;
1674e602ba25SJulian Elischer 				}
1675982d11f8SJeff Roberson 				thread_unlock(td);
1676e602ba25SJulian Elischer 			}
1677897ecacdSJohn Baldwin 			if (breakout) {
1678897ecacdSJohn Baldwin 				PROC_UNLOCK(p);
16792fe6e4d7SDavid Greenman 				continue;
16802fe6e4d7SDavid Greenman 			}
16812fe6e4d7SDavid Greenman 			/*
16822fe6e4d7SDavid Greenman 			 * get a limit
16832fe6e4d7SDavid Greenman 			 */
1684dcbcd518SBruce Evans 			lim_rlimit(p, RLIMIT_RSS, &rsslim);
1685fe2144fdSLuoqi Chen 			limit = OFF_TO_IDX(
168691d5354aSJohn Baldwin 			    qmin(rsslim.rlim_cur, rsslim.rlim_max));
16872fe6e4d7SDavid Greenman 
16882fe6e4d7SDavid Greenman 			/*
16890d94caffSDavid Greenman 			 * let processes that are swapped out really be
16900d94caffSDavid Greenman 			 * swapped out set the limit to nothing (will force a
16910d94caffSDavid Greenman 			 * swap-out.)
16922fe6e4d7SDavid Greenman 			 */
1693b61ce5b0SJeff Roberson 			if ((p->p_flag & P_INMEM) == 0)
16940d94caffSDavid Greenman 				limit = 0;	/* XXX */
16956bed074cSKonstantin Belousov 			vm = vmspace_acquire_ref(p);
1696897ecacdSJohn Baldwin 			PROC_UNLOCK(p);
16976bed074cSKonstantin Belousov 			if (vm == NULL)
16986bed074cSKonstantin Belousov 				continue;
16992fe6e4d7SDavid Greenman 
17006bed074cSKonstantin Belousov 			size = vmspace_resident_count(vm);
17012fe6e4d7SDavid Greenman 			if (limit >= 0 && size >= limit) {
1702fe2144fdSLuoqi Chen 				vm_pageout_map_deactivate_pages(
17036bed074cSKonstantin Belousov 				    &vm->vm_map, limit);
17042fe6e4d7SDavid Greenman 			}
17056bed074cSKonstantin Belousov 			vmspace_free(vm);
17062fe6e4d7SDavid Greenman 		}
17071005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
170824a1cce3SDavid Greenman 	}
17092fe6e4d7SDavid Greenman }
1710a1287949SEivind Eklund #endif			/* !defined(NO_SWAPPING) */
1711