xref: /freebsd/sys/vm/vm_pageout.c (revision d474eaaa5fa26915fc4389a789e833f4e0159c0c)
1df8bae1dSRodney W. Grimes /*
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.
8df8bae1dSRodney W. Grimes  *
9df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
10df8bae1dSRodney W. Grimes  * The Mach Operating System project at Carnegie-Mellon University.
11df8bae1dSRodney W. Grimes  *
12df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
13df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
14df8bae1dSRodney W. Grimes  * are met:
15df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
17df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
18df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
19df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
20df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
21df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
22df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
23df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
24df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
25df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
26df8bae1dSRodney W. Grimes  *    without specific prior written permission.
27df8bae1dSRodney W. Grimes  *
28df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
39df8bae1dSRodney W. Grimes  *
403c4dd356SDavid Greenman  *	from: @(#)vm_pageout.c	7.4 (Berkeley) 5/7/91
41df8bae1dSRodney W. Grimes  *
42df8bae1dSRodney W. Grimes  *
43df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
44df8bae1dSRodney W. Grimes  * All rights reserved.
45df8bae1dSRodney W. Grimes  *
46df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
47df8bae1dSRodney W. Grimes  *
48df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
49df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
50df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
51df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
52df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
53df8bae1dSRodney W. Grimes  *
54df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
55df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
56df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
57df8bae1dSRodney W. Grimes  *
58df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
59df8bae1dSRodney W. Grimes  *
60df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
61df8bae1dSRodney W. Grimes  *  School of Computer Science
62df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
63df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
64df8bae1dSRodney W. Grimes  *
65df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
66df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
6726f9a767SRodney W. Grimes  *
68d474eaaaSDoug Rabson  * $Id: vm_pageout.c,v 1.123 1998/07/10 17:58:35 alex Exp $
69df8bae1dSRodney W. Grimes  */
70df8bae1dSRodney W. Grimes 
71df8bae1dSRodney W. Grimes /*
72df8bae1dSRodney W. Grimes  *	The proverbial page-out daemon.
73df8bae1dSRodney W. Grimes  */
74df8bae1dSRodney W. Grimes 
75df8bae1dSRodney W. Grimes #include <sys/param.h>
7626f9a767SRodney W. Grimes #include <sys/systm.h>
77b5e8ce9fSBruce Evans #include <sys/kernel.h>
7826f9a767SRodney W. Grimes #include <sys/proc.h>
7926f9a767SRodney W. Grimes #include <sys/resourcevar.h>
80d2fc5315SPoul-Henning Kamp #include <sys/signalvar.h>
81f6b04d2bSDavid Greenman #include <sys/vnode.h>
82efeaf95aSDavid Greenman #include <sys/vmmeter.h>
8338efa82bSJohn Dyson #include <sys/sysctl.h>
84df8bae1dSRodney W. Grimes 
85df8bae1dSRodney W. Grimes #include <vm/vm.h>
86efeaf95aSDavid Greenman #include <vm/vm_param.h>
87efeaf95aSDavid Greenman #include <vm/vm_prot.h>
88996c772fSJohn Dyson #include <sys/lock.h>
89efeaf95aSDavid Greenman #include <vm/vm_object.h>
90df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
91efeaf95aSDavid Greenman #include <vm/vm_map.h>
92df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
9324a1cce3SDavid Greenman #include <vm/vm_pager.h>
9405f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h>
95efeaf95aSDavid Greenman #include <vm/vm_extern.h>
96df8bae1dSRodney W. Grimes 
972b14f991SJulian Elischer /*
982b14f991SJulian Elischer  * System initialization
992b14f991SJulian Elischer  */
1002b14f991SJulian Elischer 
1012b14f991SJulian Elischer /* the kernel process "vm_pageout"*/
1022b14f991SJulian Elischer static void vm_pageout __P((void));
1038f9110f6SJohn Dyson static int vm_pageout_clean __P((vm_page_t));
1043af76890SPoul-Henning Kamp static int vm_pageout_scan __P((void));
105f35329acSJohn Dyson static int vm_pageout_free_page_calc __P((vm_size_t count));
1062b14f991SJulian Elischer struct proc *pageproc;
1072b14f991SJulian Elischer 
1082b14f991SJulian Elischer static struct kproc_desc page_kp = {
1092b14f991SJulian Elischer 	"pagedaemon",
1102b14f991SJulian Elischer 	vm_pageout,
1112b14f991SJulian Elischer 	&pageproc
1122b14f991SJulian Elischer };
1134590fd3aSDavid Greenman SYSINIT_KT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, &page_kp)
1142b14f991SJulian Elischer 
11538efa82bSJohn Dyson #if !defined(NO_SWAPPING)
1162b14f991SJulian Elischer /* the kernel process "vm_daemon"*/
1172b14f991SJulian Elischer static void vm_daemon __P((void));
118f708ef1bSPoul-Henning Kamp static struct	proc *vmproc;
1192b14f991SJulian Elischer 
1202b14f991SJulian Elischer static struct kproc_desc vm_kp = {
1212b14f991SJulian Elischer 	"vmdaemon",
1222b14f991SJulian Elischer 	vm_daemon,
1232b14f991SJulian Elischer 	&vmproc
1242b14f991SJulian Elischer };
1254590fd3aSDavid Greenman SYSINIT_KT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp)
12638efa82bSJohn Dyson #endif
1272b14f991SJulian Elischer 
1282b14f991SJulian Elischer 
1292d8acc0fSJohn Dyson int vm_pages_needed=0;		/* Event on which pageout daemon sleeps */
1302d8acc0fSJohn Dyson int vm_pageout_deficit=0;	/* Estimated number of pages deficit */
1312d8acc0fSJohn Dyson int vm_pageout_pages_needed=0;	/* flag saying that the pageout daemon needs pages */
13226f9a767SRodney W. Grimes 
13326f9a767SRodney W. Grimes extern int npendingio;
13438efa82bSJohn Dyson #if !defined(NO_SWAPPING)
135f708ef1bSPoul-Henning Kamp static int vm_pageout_req_swapout;	/* XXX */
136f708ef1bSPoul-Henning Kamp static int vm_daemon_needed;
13738efa82bSJohn Dyson #endif
13826f9a767SRodney W. Grimes extern int nswiodone;
1395663e6deSDavid Greenman extern int vm_swap_size;
140f6b04d2bSDavid Greenman extern int vfs_update_wakeup;
141303b270bSEivind Eklund static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0;
142303b270bSEivind Eklund static int vm_pageout_full_stats_interval = 0;
143303b270bSEivind Eklund static int vm_pageout_stats_free_max=0, vm_pageout_algorithm_lru=0;
144303b270bSEivind Eklund static int defer_swap_pageouts=0;
145303b270bSEivind Eklund static int disable_swap_pageouts=0;
14670111b90SJohn Dyson 
147303b270bSEivind Eklund static int max_page_launder=100;
14838efa82bSJohn Dyson #if defined(NO_SWAPPING)
149303b270bSEivind Eklund static int vm_swap_enabled=0;
150303b270bSEivind Eklund static int vm_swap_idle_enabled=0;
15138efa82bSJohn Dyson #else
152303b270bSEivind Eklund static int vm_swap_enabled=1;
153303b270bSEivind Eklund static int vm_swap_idle_enabled=0;
15438efa82bSJohn Dyson #endif
15538efa82bSJohn Dyson 
15638efa82bSJohn Dyson SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm,
15738efa82bSJohn Dyson 	CTLFLAG_RW, &vm_pageout_algorithm_lru, 0, "");
15838efa82bSJohn Dyson 
159dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max,
160dc2efb27SJohn Dyson 	CTLFLAG_RW, &vm_pageout_stats_max, 0, "");
161dc2efb27SJohn Dyson 
162dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval,
163dc2efb27SJohn Dyson 	CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "");
164dc2efb27SJohn Dyson 
165dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval,
166dc2efb27SJohn Dyson 	CTLFLAG_RW, &vm_pageout_stats_interval, 0, "");
167dc2efb27SJohn Dyson 
168dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_free_max,
169dc2efb27SJohn Dyson 	CTLFLAG_RW, &vm_pageout_stats_free_max, 0, "");
170dc2efb27SJohn Dyson 
17138efa82bSJohn Dyson #if defined(NO_SWAPPING)
172ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
173ceb0cf87SJohn Dyson 	CTLFLAG_RD, &vm_swap_enabled, 0, "");
174ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
175ceb0cf87SJohn Dyson 	CTLFLAG_RD, &vm_swap_idle_enabled, 0, "");
17638efa82bSJohn Dyson #else
177ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
178ceb0cf87SJohn Dyson 	CTLFLAG_RW, &vm_swap_enabled, 0, "");
179ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
180ceb0cf87SJohn Dyson 	CTLFLAG_RW, &vm_swap_idle_enabled, 0, "");
18138efa82bSJohn Dyson #endif
18226f9a767SRodney W. Grimes 
183ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts,
18412ac6a1dSJohn Dyson 	CTLFLAG_RW, &defer_swap_pageouts, 0, "");
18512ac6a1dSJohn Dyson 
186ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts,
18712ac6a1dSJohn Dyson 	CTLFLAG_RW, &disable_swap_pageouts, 0, "");
18812ac6a1dSJohn Dyson 
189ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, max_page_launder,
190ceb0cf87SJohn Dyson 	CTLFLAG_RW, &max_page_launder, 0, "");
19170111b90SJohn Dyson 
19226f9a767SRodney W. Grimes 
193ffc82b0aSJohn Dyson #define VM_PAGEOUT_PAGE_COUNT 16
194bbc0ec52SDavid Greenman int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT;
195df8bae1dSRodney W. Grimes 
196c3cb3e12SDavid Greenman int vm_page_max_wired;		/* XXX max # of wired pages system-wide */
197df8bae1dSRodney W. Grimes 
19838efa82bSJohn Dyson #if !defined(NO_SWAPPING)
19938efa82bSJohn Dyson typedef void freeer_fcn_t __P((vm_map_t, vm_object_t, vm_pindex_t, int));
20038efa82bSJohn Dyson static void vm_pageout_map_deactivate_pages __P((vm_map_t, vm_pindex_t));
201cd41fc12SDavid Greenman static freeer_fcn_t vm_pageout_object_deactivate_pages;
202cd41fc12SDavid Greenman static void vm_req_vmdaemon __P((void));
20338efa82bSJohn Dyson #endif
204dc2efb27SJohn Dyson static void vm_pageout_page_stats(void);
2055985940eSJohn Dyson void pmap_collect(void);
206cd41fc12SDavid Greenman 
20726f9a767SRodney W. Grimes /*
20826f9a767SRodney W. Grimes  * vm_pageout_clean:
20924a1cce3SDavid Greenman  *
2100d94caffSDavid Greenman  * Clean the page and remove it from the laundry.
21126f9a767SRodney W. Grimes  *
2120d94caffSDavid Greenman  * We set the busy bit to cause potential page faults on this page to
21326f9a767SRodney W. Grimes  * block.
21426f9a767SRodney W. Grimes  *
2150d94caffSDavid Greenman  * And we set pageout-in-progress to keep the object from disappearing
2160d94caffSDavid Greenman  * during pageout.  This guarantees that the page won't move from the
2170d94caffSDavid Greenman  * inactive queue.  (However, any other page on the inactive queue may
2180d94caffSDavid Greenman  * move!)
21926f9a767SRodney W. Grimes  */
2203af76890SPoul-Henning Kamp static int
2218f9110f6SJohn Dyson vm_pageout_clean(m)
22224a1cce3SDavid Greenman 	vm_page_t m;
22324a1cce3SDavid Greenman {
22426f9a767SRodney W. Grimes 	register vm_object_t object;
225f35329acSJohn Dyson 	vm_page_t mc[2*vm_pageout_page_count];
22624a1cce3SDavid Greenman 	int pageout_count;
22724a1cce3SDavid Greenman 	int i, forward_okay, backward_okay, page_base;
228a316d390SJohn Dyson 	vm_pindex_t pindex = m->pindex;
22926f9a767SRodney W. Grimes 
23026f9a767SRodney W. Grimes 	object = m->object;
23124a1cce3SDavid Greenman 
23226f9a767SRodney W. Grimes 	/*
23324a1cce3SDavid Greenman 	 * If not OBJT_SWAP, additional memory may be needed to do the pageout.
23424a1cce3SDavid Greenman 	 * Try to avoid the deadlock.
23526f9a767SRodney W. Grimes 	 */
2368f9110f6SJohn Dyson 	if ((object->type == OBJT_DEFAULT) &&
23724a1cce3SDavid Greenman 	    ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min))
23826f9a767SRodney W. Grimes 		return 0;
23926f9a767SRodney W. Grimes 
24024a1cce3SDavid Greenman 	/*
24124a1cce3SDavid Greenman 	 * Don't mess with the page if it's busy.
24224a1cce3SDavid Greenman 	 */
2438f9110f6SJohn Dyson 	if ((m->hold_count != 0) ||
2440d94caffSDavid Greenman 	    ((m->busy != 0) || (m->flags & PG_BUSY)))
2450d94caffSDavid Greenman 		return 0;
2460d94caffSDavid Greenman 
24724a1cce3SDavid Greenman 	/*
24824a1cce3SDavid Greenman 	 * Try collapsing before it's too late.
24924a1cce3SDavid Greenman 	 */
2508f9110f6SJohn Dyson 	if (object->backing_object) {
25126f9a767SRodney W. Grimes 		vm_object_collapse(object);
25226f9a767SRodney W. Grimes 	}
2533c018e72SJohn Dyson 
254f35329acSJohn Dyson 	mc[vm_pageout_page_count] = m;
25526f9a767SRodney W. Grimes 	pageout_count = 1;
256f35329acSJohn Dyson 	page_base = vm_pageout_page_count;
25724a1cce3SDavid Greenman 	forward_okay = TRUE;
258a316d390SJohn Dyson 	if (pindex != 0)
25924a1cce3SDavid Greenman 		backward_okay = TRUE;
26026f9a767SRodney W. Grimes 	else
26124a1cce3SDavid Greenman 		backward_okay = FALSE;
26224a1cce3SDavid Greenman 	/*
26324a1cce3SDavid Greenman 	 * Scan object for clusterable pages.
26424a1cce3SDavid Greenman 	 *
26524a1cce3SDavid Greenman 	 * We can cluster ONLY if: ->> the page is NOT
26624a1cce3SDavid Greenman 	 * clean, wired, busy, held, or mapped into a
26724a1cce3SDavid Greenman 	 * buffer, and one of the following:
26824a1cce3SDavid Greenman 	 * 1) The page is inactive, or a seldom used
26924a1cce3SDavid Greenman 	 *    active page.
27024a1cce3SDavid Greenman 	 * -or-
27124a1cce3SDavid Greenman 	 * 2) we force the issue.
27224a1cce3SDavid Greenman 	 */
27324a1cce3SDavid Greenman 	for (i = 1; (i < vm_pageout_page_count) && (forward_okay || backward_okay); i++) {
27424a1cce3SDavid Greenman 		vm_page_t p;
275f6b04d2bSDavid Greenman 
27624a1cce3SDavid Greenman 		/*
27724a1cce3SDavid Greenman 		 * See if forward page is clusterable.
27824a1cce3SDavid Greenman 		 */
27924a1cce3SDavid Greenman 		if (forward_okay) {
28024a1cce3SDavid Greenman 			/*
28124a1cce3SDavid Greenman 			 * Stop forward scan at end of object.
28224a1cce3SDavid Greenman 			 */
283a316d390SJohn Dyson 			if ((pindex + i) > object->size) {
28424a1cce3SDavid Greenman 				forward_okay = FALSE;
28524a1cce3SDavid Greenman 				goto do_backward;
286f6b04d2bSDavid Greenman 			}
287a316d390SJohn Dyson 			p = vm_page_lookup(object, pindex + i);
28824a1cce3SDavid Greenman 			if (p) {
2895070c7f8SJohn Dyson 				if (((p->queue - p->pc) == PQ_CACHE) ||
2905070c7f8SJohn Dyson 					(p->flags & PG_BUSY) || p->busy) {
29124a1cce3SDavid Greenman 					forward_okay = FALSE;
29224a1cce3SDavid Greenman 					goto do_backward;
293f6b04d2bSDavid Greenman 				}
29424a1cce3SDavid Greenman 				vm_page_test_dirty(p);
29524a1cce3SDavid Greenman 				if ((p->dirty & p->valid) != 0 &&
2968f9110f6SJohn Dyson 				    (p->queue == PQ_INACTIVE) &&
29724a1cce3SDavid Greenman 				    (p->wire_count == 0) &&
29824a1cce3SDavid Greenman 				    (p->hold_count == 0)) {
299f35329acSJohn Dyson 					mc[vm_pageout_page_count + i] = p;
30024a1cce3SDavid Greenman 					pageout_count++;
30124a1cce3SDavid Greenman 					if (pageout_count == vm_pageout_page_count)
30224a1cce3SDavid Greenman 						break;
30324a1cce3SDavid Greenman 				} else {
30424a1cce3SDavid Greenman 					forward_okay = FALSE;
305f6b04d2bSDavid Greenman 				}
30624a1cce3SDavid Greenman 			} else {
30724a1cce3SDavid Greenman 				forward_okay = FALSE;
30824a1cce3SDavid Greenman 			}
30924a1cce3SDavid Greenman 		}
31024a1cce3SDavid Greenman do_backward:
31124a1cce3SDavid Greenman 		/*
31224a1cce3SDavid Greenman 		 * See if backward page is clusterable.
31324a1cce3SDavid Greenman 		 */
31424a1cce3SDavid Greenman 		if (backward_okay) {
31524a1cce3SDavid Greenman 			/*
31624a1cce3SDavid Greenman 			 * Stop backward scan at beginning of object.
31724a1cce3SDavid Greenman 			 */
318a316d390SJohn Dyson 			if ((pindex - i) == 0) {
31924a1cce3SDavid Greenman 				backward_okay = FALSE;
32024a1cce3SDavid Greenman 			}
321a316d390SJohn Dyson 			p = vm_page_lookup(object, pindex - i);
32224a1cce3SDavid Greenman 			if (p) {
3235070c7f8SJohn Dyson 				if (((p->queue - p->pc) == PQ_CACHE) ||
3245070c7f8SJohn Dyson 					(p->flags & PG_BUSY) || p->busy) {
32524a1cce3SDavid Greenman 					backward_okay = FALSE;
32624a1cce3SDavid Greenman 					continue;
32724a1cce3SDavid Greenman 				}
32824a1cce3SDavid Greenman 				vm_page_test_dirty(p);
32924a1cce3SDavid Greenman 				if ((p->dirty & p->valid) != 0 &&
3308f9110f6SJohn Dyson 				    (p->queue == PQ_INACTIVE) &&
33124a1cce3SDavid Greenman 				    (p->wire_count == 0) &&
33224a1cce3SDavid Greenman 				    (p->hold_count == 0)) {
333f35329acSJohn Dyson 					mc[vm_pageout_page_count - i] = p;
33424a1cce3SDavid Greenman 					pageout_count++;
33524a1cce3SDavid Greenman 					page_base--;
33624a1cce3SDavid Greenman 					if (pageout_count == vm_pageout_page_count)
33724a1cce3SDavid Greenman 						break;
33824a1cce3SDavid Greenman 				} else {
33924a1cce3SDavid Greenman 					backward_okay = FALSE;
34024a1cce3SDavid Greenman 				}
34124a1cce3SDavid Greenman 			} else {
34224a1cce3SDavid Greenman 				backward_okay = FALSE;
34324a1cce3SDavid Greenman 			}
344f6b04d2bSDavid Greenman 		}
345f6b04d2bSDavid Greenman 	}
346f6b04d2bSDavid Greenman 
34767bf6868SJohn Dyson 	/*
34867bf6868SJohn Dyson 	 * we allow reads during pageouts...
34967bf6868SJohn Dyson 	 */
3508f9110f6SJohn Dyson 	return vm_pageout_flush(&mc[page_base], pageout_count, 0);
351aef922f5SJohn Dyson }
352aef922f5SJohn Dyson 
353aef922f5SJohn Dyson int
3548f9110f6SJohn Dyson vm_pageout_flush(mc, count, flags)
355aef922f5SJohn Dyson 	vm_page_t *mc;
356aef922f5SJohn Dyson 	int count;
3578f9110f6SJohn Dyson 	int flags;
358aef922f5SJohn Dyson {
359aef922f5SJohn Dyson 	register vm_object_t object;
360aef922f5SJohn Dyson 	int pageout_status[count];
36195461b45SJohn Dyson 	int numpagedout = 0;
362aef922f5SJohn Dyson 	int i;
363aef922f5SJohn Dyson 
3648f9110f6SJohn Dyson 	for (i = 0; i < count; i++) {
3658f9110f6SJohn Dyson 		mc[i]->busy++;
3668f9110f6SJohn Dyson 		vm_page_protect(mc[i], VM_PROT_READ);
3678f9110f6SJohn Dyson 	}
3688f9110f6SJohn Dyson 
369aef922f5SJohn Dyson 	object = mc[0]->object;
370d474eaaaSDoug Rabson 	vm_object_pip_add(object, count);
371aef922f5SJohn Dyson 
372aef922f5SJohn Dyson 	vm_pager_put_pages(object, mc, count,
3738f9110f6SJohn Dyson 	    (flags | ((object == kernel_object) ? OBJPC_SYNC : 0)),
37426f9a767SRodney W. Grimes 	    pageout_status);
37526f9a767SRodney W. Grimes 
376aef922f5SJohn Dyson 	for (i = 0; i < count; i++) {
377aef922f5SJohn Dyson 		vm_page_t mt = mc[i];
37824a1cce3SDavid Greenman 
37926f9a767SRodney W. Grimes 		switch (pageout_status[i]) {
38026f9a767SRodney W. Grimes 		case VM_PAGER_OK:
38195461b45SJohn Dyson 			numpagedout++;
38226f9a767SRodney W. Grimes 			break;
38326f9a767SRodney W. Grimes 		case VM_PAGER_PEND:
38495461b45SJohn Dyson 			numpagedout++;
38526f9a767SRodney W. Grimes 			break;
38626f9a767SRodney W. Grimes 		case VM_PAGER_BAD:
38726f9a767SRodney W. Grimes 			/*
3880d94caffSDavid Greenman 			 * Page outside of range of object. Right now we
3890d94caffSDavid Greenman 			 * essentially lose the changes by pretending it
3900d94caffSDavid Greenman 			 * worked.
39126f9a767SRodney W. Grimes 			 */
39267bf6868SJohn Dyson 			pmap_clear_modify(VM_PAGE_TO_PHYS(mt));
39324a1cce3SDavid Greenman 			mt->dirty = 0;
39426f9a767SRodney W. Grimes 			break;
39526f9a767SRodney W. Grimes 		case VM_PAGER_ERROR:
39626f9a767SRodney W. Grimes 		case VM_PAGER_FAIL:
39726f9a767SRodney W. Grimes 			/*
3980d94caffSDavid Greenman 			 * If page couldn't be paged out, then reactivate the
3990d94caffSDavid Greenman 			 * page so it doesn't clog the inactive list.  (We
4000d94caffSDavid Greenman 			 * will try paging out it again later).
40126f9a767SRodney W. Grimes 			 */
40224a1cce3SDavid Greenman 			vm_page_activate(mt);
40326f9a767SRodney W. Grimes 			break;
40426f9a767SRodney W. Grimes 		case VM_PAGER_AGAIN:
40526f9a767SRodney W. Grimes 			break;
40626f9a767SRodney W. Grimes 		}
40726f9a767SRodney W. Grimes 
40826f9a767SRodney W. Grimes 		/*
4090d94caffSDavid Greenman 		 * If the operation is still going, leave the page busy to
4100d94caffSDavid Greenman 		 * block all other accesses. Also, leave the paging in
4110d94caffSDavid Greenman 		 * progress indicator set so that we don't attempt an object
4120d94caffSDavid Greenman 		 * collapse.
41326f9a767SRodney W. Grimes 		 */
41426f9a767SRodney W. Grimes 		if (pageout_status[i] != VM_PAGER_PEND) {
415f919ebdeSDavid Greenman 			vm_object_pip_wakeup(object);
4168f9110f6SJohn Dyson 			PAGE_BWAKEUP(mt);
41726f9a767SRodney W. Grimes 		}
41826f9a767SRodney W. Grimes 	}
41995461b45SJohn Dyson 	return numpagedout;
42026f9a767SRodney W. Grimes }
42126f9a767SRodney W. Grimes 
42238efa82bSJohn Dyson #if !defined(NO_SWAPPING)
42326f9a767SRodney W. Grimes /*
42426f9a767SRodney W. Grimes  *	vm_pageout_object_deactivate_pages
42526f9a767SRodney W. Grimes  *
42626f9a767SRodney W. Grimes  *	deactivate enough pages to satisfy the inactive target
42726f9a767SRodney W. Grimes  *	requirements or if vm_page_proc_limit is set, then
42826f9a767SRodney W. Grimes  *	deactivate all of the pages in the object and its
42924a1cce3SDavid Greenman  *	backing_objects.
43026f9a767SRodney W. Grimes  *
43126f9a767SRodney W. Grimes  *	The object and map must be locked.
43226f9a767SRodney W. Grimes  */
43338efa82bSJohn Dyson static void
43438efa82bSJohn Dyson vm_pageout_object_deactivate_pages(map, object, desired, map_remove_only)
43526f9a767SRodney W. Grimes 	vm_map_t map;
43626f9a767SRodney W. Grimes 	vm_object_t object;
43738efa82bSJohn Dyson 	vm_pindex_t desired;
4380d94caffSDavid Greenman 	int map_remove_only;
43926f9a767SRodney W. Grimes {
44026f9a767SRodney W. Grimes 	register vm_page_t p, next;
44126f9a767SRodney W. Grimes 	int rcount;
44238efa82bSJohn Dyson 	int remove_mode;
4431eeaa1e3SJohn Dyson 	int s;
44426f9a767SRodney W. Grimes 
44524a1cce3SDavid Greenman 	if (object->type == OBJT_DEVICE)
44638efa82bSJohn Dyson 		return;
4478f895206SDavid Greenman 
44838efa82bSJohn Dyson 	while (object) {
44938efa82bSJohn Dyson 		if (vm_map_pmap(map)->pm_stats.resident_count <= desired)
45038efa82bSJohn Dyson 			return;
45124a1cce3SDavid Greenman 		if (object->paging_in_progress)
45238efa82bSJohn Dyson 			return;
45326f9a767SRodney W. Grimes 
45438efa82bSJohn Dyson 		remove_mode = map_remove_only;
45538efa82bSJohn Dyson 		if (object->shadow_count > 1)
45638efa82bSJohn Dyson 			remove_mode = 1;
45726f9a767SRodney W. Grimes 	/*
45826f9a767SRodney W. Grimes 	 * scan the objects entire memory queue
45926f9a767SRodney W. Grimes 	 */
46026f9a767SRodney W. Grimes 		rcount = object->resident_page_count;
461b18bfc3dSJohn Dyson 		p = TAILQ_FIRST(&object->memq);
46226f9a767SRodney W. Grimes 		while (p && (rcount-- > 0)) {
4637e006499SJohn Dyson 			int actcount;
46438efa82bSJohn Dyson 			if (vm_map_pmap(map)->pm_stats.resident_count <= desired)
46538efa82bSJohn Dyson 				return;
466b18bfc3dSJohn Dyson 			next = TAILQ_NEXT(p, listq);
467a58d1fa1SDavid Greenman 			cnt.v_pdpages++;
4680d94caffSDavid Greenman 			if (p->wire_count != 0 ||
4690d94caffSDavid Greenman 			    p->hold_count != 0 ||
4700d94caffSDavid Greenman 			    p->busy != 0 ||
471bd7e5f99SJohn Dyson 			    (p->flags & PG_BUSY) ||
4720d94caffSDavid Greenman 			    !pmap_page_exists(vm_map_pmap(map), VM_PAGE_TO_PHYS(p))) {
4730d94caffSDavid Greenman 				p = next;
4740d94caffSDavid Greenman 				continue;
4750d94caffSDavid Greenman 			}
476ef743ce6SJohn Dyson 
4777e006499SJohn Dyson 			actcount = pmap_ts_referenced(VM_PAGE_TO_PHYS(p));
4787e006499SJohn Dyson 			if (actcount) {
479ef743ce6SJohn Dyson 				p->flags |= PG_REFERENCED;
480c8c4b40cSJohn Dyson 			} else if (p->flags & PG_REFERENCED) {
4817e006499SJohn Dyson 				actcount = 1;
482ef743ce6SJohn Dyson 			}
483ef743ce6SJohn Dyson 
48438efa82bSJohn Dyson 			if ((p->queue != PQ_ACTIVE) &&
48538efa82bSJohn Dyson 				(p->flags & PG_REFERENCED)) {
486ef743ce6SJohn Dyson 				vm_page_activate(p);
4877e006499SJohn Dyson 				p->act_count += actcount;
488c8c4b40cSJohn Dyson 				p->flags &= ~PG_REFERENCED;
489c8c4b40cSJohn Dyson 			} else if (p->queue == PQ_ACTIVE) {
490ef743ce6SJohn Dyson 				if ((p->flags & PG_REFERENCED) == 0) {
491c8c4b40cSJohn Dyson 					p->act_count -= min(p->act_count, ACT_DECLINE);
492c8c4b40cSJohn Dyson 					if (!remove_mode && (vm_pageout_algorithm_lru || (p->act_count == 0))) {
493b18bfc3dSJohn Dyson 						vm_page_protect(p, VM_PROT_NONE);
49426f9a767SRodney W. Grimes 						vm_page_deactivate(p);
49526f9a767SRodney W. Grimes 					} else {
496c8c4b40cSJohn Dyson 						s = splvm();
497c8c4b40cSJohn Dyson 						TAILQ_REMOVE(&vm_page_queue_active, p, pageq);
498c8c4b40cSJohn Dyson 						TAILQ_INSERT_TAIL(&vm_page_queue_active, p, pageq);
499c8c4b40cSJohn Dyson 						splx(s);
500c8c4b40cSJohn Dyson 					}
501c8c4b40cSJohn Dyson 				} else {
502eaf13dd7SJohn Dyson 					vm_page_activate(p);
503a647a309SDavid Greenman 					p->flags &= ~PG_REFERENCED;
50438efa82bSJohn Dyson 					if (p->act_count < (ACT_MAX - ACT_ADVANCE))
50538efa82bSJohn Dyson 						p->act_count += ACT_ADVANCE;
5061eeaa1e3SJohn Dyson 					s = splvm();
50726f9a767SRodney W. Grimes 					TAILQ_REMOVE(&vm_page_queue_active, p, pageq);
50826f9a767SRodney W. Grimes 					TAILQ_INSERT_TAIL(&vm_page_queue_active, p, pageq);
5091eeaa1e3SJohn Dyson 					splx(s);
51026f9a767SRodney W. Grimes 				}
511bd7e5f99SJohn Dyson 			} else if (p->queue == PQ_INACTIVE) {
512f919ebdeSDavid Greenman 				vm_page_protect(p, VM_PROT_NONE);
51326f9a767SRodney W. Grimes 			}
51426f9a767SRodney W. Grimes 			p = next;
51526f9a767SRodney W. Grimes 		}
51638efa82bSJohn Dyson 		object = object->backing_object;
51738efa82bSJohn Dyson 	}
51838efa82bSJohn Dyson 	return;
51926f9a767SRodney W. Grimes }
52026f9a767SRodney W. Grimes 
52126f9a767SRodney W. Grimes /*
52226f9a767SRodney W. Grimes  * deactivate some number of pages in a map, try to do it fairly, but
52326f9a767SRodney W. Grimes  * that is really hard to do.
52426f9a767SRodney W. Grimes  */
525cd41fc12SDavid Greenman static void
52638efa82bSJohn Dyson vm_pageout_map_deactivate_pages(map, desired)
52726f9a767SRodney W. Grimes 	vm_map_t map;
52838efa82bSJohn Dyson 	vm_pindex_t desired;
52926f9a767SRodney W. Grimes {
53026f9a767SRodney W. Grimes 	vm_map_entry_t tmpe;
53138efa82bSJohn Dyson 	vm_object_t obj, bigobj;
5320d94caffSDavid Greenman 
533996c772fSJohn Dyson 	if (lockmgr(&map->lock, LK_EXCLUSIVE | LK_NOWAIT, (void *)0, curproc)) {
53426f9a767SRodney W. Grimes 		return;
53526f9a767SRodney W. Grimes 	}
53638efa82bSJohn Dyson 
53738efa82bSJohn Dyson 	bigobj = NULL;
53838efa82bSJohn Dyson 
53938efa82bSJohn Dyson 	/*
54038efa82bSJohn Dyson 	 * first, search out the biggest object, and try to free pages from
54138efa82bSJohn Dyson 	 * that.
54238efa82bSJohn Dyson 	 */
54326f9a767SRodney W. Grimes 	tmpe = map->header.next;
54438efa82bSJohn Dyson 	while (tmpe != &map->header) {
545afa07f7eSJohn Dyson 		if ((tmpe->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) == 0) {
54638efa82bSJohn Dyson 			obj = tmpe->object.vm_object;
54738efa82bSJohn Dyson 			if ((obj != NULL) && (obj->shadow_count <= 1) &&
54838efa82bSJohn Dyson 				((bigobj == NULL) ||
54938efa82bSJohn Dyson 				 (bigobj->resident_page_count < obj->resident_page_count))) {
55038efa82bSJohn Dyson 				bigobj = obj;
55138efa82bSJohn Dyson 			}
55238efa82bSJohn Dyson 		}
55338efa82bSJohn Dyson 		tmpe = tmpe->next;
55438efa82bSJohn Dyson 	}
55538efa82bSJohn Dyson 
55638efa82bSJohn Dyson 	if (bigobj)
55738efa82bSJohn Dyson 		vm_pageout_object_deactivate_pages(map, bigobj, desired, 0);
55838efa82bSJohn Dyson 
55938efa82bSJohn Dyson 	/*
56038efa82bSJohn Dyson 	 * Next, hunt around for other pages to deactivate.  We actually
56138efa82bSJohn Dyson 	 * do this search sort of wrong -- .text first is not the best idea.
56238efa82bSJohn Dyson 	 */
56338efa82bSJohn Dyson 	tmpe = map->header.next;
56438efa82bSJohn Dyson 	while (tmpe != &map->header) {
56538efa82bSJohn Dyson 		if (vm_map_pmap(map)->pm_stats.resident_count <= desired)
56638efa82bSJohn Dyson 			break;
567afa07f7eSJohn Dyson 		if ((tmpe->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) == 0) {
56838efa82bSJohn Dyson 			obj = tmpe->object.vm_object;
56901155bd7SDavid Greenman 			if (obj)
57038efa82bSJohn Dyson 				vm_pageout_object_deactivate_pages(map, obj, desired, 0);
57138efa82bSJohn Dyson 		}
57226f9a767SRodney W. Grimes 		tmpe = tmpe->next;
57326f9a767SRodney W. Grimes 	};
57438efa82bSJohn Dyson 
57538efa82bSJohn Dyson 	/*
57638efa82bSJohn Dyson 	 * Remove all mappings if a process is swapped out, this will free page
57738efa82bSJohn Dyson 	 * table pages.
57838efa82bSJohn Dyson 	 */
57938efa82bSJohn Dyson 	if (desired == 0)
58038efa82bSJohn Dyson 		pmap_remove(vm_map_pmap(map),
58138efa82bSJohn Dyson 			VM_MIN_ADDRESS, VM_MAXUSER_ADDRESS);
58238efa82bSJohn Dyson 	vm_map_unlock(map);
58326f9a767SRodney W. Grimes 	return;
58426f9a767SRodney W. Grimes }
58538efa82bSJohn Dyson #endif
586df8bae1dSRodney W. Grimes 
587925a3a41SJohn Dyson void
588925a3a41SJohn Dyson vm_pageout_page_free(vm_page_t m) {
58947221757SJohn Dyson 	struct vnode *vp;
59047221757SJohn Dyson 	vm_object_t object;
591925a3a41SJohn Dyson 
59247221757SJohn Dyson 	object = m->object;
59347221757SJohn Dyson 	object->ref_count++;
594eaf13dd7SJohn Dyson 
59547221757SJohn Dyson 	if (object->type == OBJT_VNODE) {
59647221757SJohn Dyson 		vp = object->handle;
59747221757SJohn Dyson 		vp->v_usecount++;
59847221757SJohn Dyson 		if (VSHOULDBUSY(vp))
59947221757SJohn Dyson 			vbusy(vp);
600925a3a41SJohn Dyson 	}
601eaf13dd7SJohn Dyson 
60247221757SJohn Dyson 	m->flags |= PG_BUSY;
603925a3a41SJohn Dyson 	vm_page_protect(m, VM_PROT_NONE);
604925a3a41SJohn Dyson 	vm_page_free(m);
60547221757SJohn Dyson 	vm_object_deallocate(object);
606925a3a41SJohn Dyson }
607925a3a41SJohn Dyson 
608df8bae1dSRodney W. Grimes /*
609df8bae1dSRodney W. Grimes  *	vm_pageout_scan does the dirty work for the pageout daemon.
610df8bae1dSRodney W. Grimes  */
6113af76890SPoul-Henning Kamp static int
612df8bae1dSRodney W. Grimes vm_pageout_scan()
613df8bae1dSRodney W. Grimes {
614502ba6e4SJohn Dyson 	vm_page_t m, next;
61570111b90SJohn Dyson 	int page_shortage, addl_page_shortage, maxscan, pcount;
61670111b90SJohn Dyson 	int maxlaunder;
6174e39a515SPoul-Henning Kamp 	int pages_freed;
6185663e6deSDavid Greenman 	struct proc *p, *bigproc;
6195663e6deSDavid Greenman 	vm_offset_t size, bigsize;
620df8bae1dSRodney W. Grimes 	vm_object_t object;
62126f9a767SRodney W. Grimes 	int force_wakeup = 0;
6227e006499SJohn Dyson 	int actcount;
623f6b04d2bSDavid Greenman 	int vnodes_skipped = 0;
6241eeaa1e3SJohn Dyson 	int s;
6250d94caffSDavid Greenman 
626df8bae1dSRodney W. Grimes 	/*
6275985940eSJohn Dyson 	 * Do whatever cleanup that the pmap code can.
6285985940eSJohn Dyson 	 */
6295985940eSJohn Dyson 	pmap_collect();
6305985940eSJohn Dyson 
6315985940eSJohn Dyson 	/*
6320d94caffSDavid Greenman 	 * Start scanning the inactive queue for pages we can free. We keep
6330d94caffSDavid Greenman 	 * scanning until we have enough free pages or we have scanned through
6340d94caffSDavid Greenman 	 * the entire queue.  If we encounter dirty pages, we start cleaning
6350d94caffSDavid Greenman 	 * them.
636df8bae1dSRodney W. Grimes 	 */
637df8bae1dSRodney W. Grimes 
638b182ec9eSJohn Dyson 	pages_freed = 0;
6392d8acc0fSJohn Dyson 	addl_page_shortage = vm_pageout_deficit;
64095461b45SJohn Dyson 	vm_pageout_deficit = 0;
641b182ec9eSJohn Dyson 
642ceb0cf87SJohn Dyson 	if (max_page_launder == 0)
643ceb0cf87SJohn Dyson 		max_page_launder = 1;
644ceb0cf87SJohn Dyson 	maxlaunder = (cnt.v_inactive_target > max_page_launder) ?
645ceb0cf87SJohn Dyson 	    max_page_launder : cnt.v_inactive_target;
64670111b90SJohn Dyson 
64767bf6868SJohn Dyson rescan0:
648f6b04d2bSDavid Greenman 	maxscan = cnt.v_inactive_count;
649b182ec9eSJohn Dyson 	for( m = TAILQ_FIRST(&vm_page_queue_inactive);
650b182ec9eSJohn Dyson 
651b182ec9eSJohn Dyson 		(m != NULL) && (maxscan-- > 0) &&
652b18bfc3dSJohn Dyson 			((cnt.v_cache_count + cnt.v_free_count) <
653b182ec9eSJohn Dyson 			(cnt.v_cache_min + cnt.v_free_target));
654b182ec9eSJohn Dyson 
655b182ec9eSJohn Dyson 		m = next) {
656df8bae1dSRodney W. Grimes 
657a58d1fa1SDavid Greenman 		cnt.v_pdpages++;
658b182ec9eSJohn Dyson 
659f35329acSJohn Dyson 		if (m->queue != PQ_INACTIVE) {
66067bf6868SJohn Dyson 			goto rescan0;
661f35329acSJohn Dyson 		}
662b182ec9eSJohn Dyson 
663b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(m, pageq);
664df8bae1dSRodney W. Grimes 
665b182ec9eSJohn Dyson 		if (m->hold_count) {
666f35329acSJohn Dyson 			s = splvm();
667b182ec9eSJohn Dyson 			TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq);
668b182ec9eSJohn Dyson 			TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq);
669f35329acSJohn Dyson 			splx(s);
670b182ec9eSJohn Dyson 			addl_page_shortage++;
671b182ec9eSJohn Dyson 			continue;
672df8bae1dSRodney W. Grimes 		}
67326f9a767SRodney W. Grimes 		/*
674b18bfc3dSJohn Dyson 		 * Dont mess with busy pages, keep in the front of the
675b18bfc3dSJohn Dyson 		 * queue, most likely are being paged out.
67626f9a767SRodney W. Grimes 		 */
677bd7e5f99SJohn Dyson 		if (m->busy || (m->flags & PG_BUSY)) {
678b182ec9eSJohn Dyson 			addl_page_shortage++;
67926f9a767SRodney W. Grimes 			continue;
68026f9a767SRodney W. Grimes 		}
681bd7e5f99SJohn Dyson 
6827e006499SJohn Dyson 		/*
6837e006499SJohn Dyson 		 * If the object is not being used, we ignore previous references.
6847e006499SJohn Dyson 		 */
6850d94caffSDavid Greenman 		if (m->object->ref_count == 0) {
6860d94caffSDavid Greenman 			m->flags &= ~PG_REFERENCED;
68767bf6868SJohn Dyson 			pmap_clear_reference(VM_PAGE_TO_PHYS(m));
6887e006499SJohn Dyson 
6897e006499SJohn Dyson 		/*
6907e006499SJohn Dyson 		 * Otherwise, if the page has been referenced while in the inactive
6917e006499SJohn Dyson 		 * queue, we bump the "activation count" upwards, making it less
6927e006499SJohn Dyson 		 * likely that the page will be added back to the inactive queue
6937e006499SJohn Dyson 		 * prematurely again.  Here we check the page tables (or emulated
6947e006499SJohn Dyson 		 * bits, if any), given the upper level VM system not knowing anything
6957e006499SJohn Dyson 		 * about existing references.
6967e006499SJohn Dyson 		 */
697ef743ce6SJohn Dyson 		} else if (((m->flags & PG_REFERENCED) == 0) &&
6987e006499SJohn Dyson 			(actcount = pmap_ts_referenced(VM_PAGE_TO_PHYS(m)))) {
699ef743ce6SJohn Dyson 			vm_page_activate(m);
7007e006499SJohn Dyson 			m->act_count += (actcount + ACT_ADVANCE);
701ef743ce6SJohn Dyson 			continue;
7022fe6e4d7SDavid Greenman 		}
703ef743ce6SJohn Dyson 
7047e006499SJohn Dyson 		/*
7057e006499SJohn Dyson 		 * If the upper level VM system knows about any page references,
7067e006499SJohn Dyson 		 * we activate the page.  We also set the "activation count" higher
7077e006499SJohn Dyson 		 * than normal so that we will less likely place pages back onto the
7087e006499SJohn Dyson 		 * inactive queue again.
7097e006499SJohn Dyson 		 */
710bd7e5f99SJohn Dyson 		if ((m->flags & PG_REFERENCED) != 0) {
711a647a309SDavid Greenman 			m->flags &= ~PG_REFERENCED;
7127e006499SJohn Dyson 			actcount = pmap_ts_referenced(VM_PAGE_TO_PHYS(m));
71326f9a767SRodney W. Grimes 			vm_page_activate(m);
7147e006499SJohn Dyson 			m->act_count += (actcount + ACT_ADVANCE + 1);
7150d94caffSDavid Greenman 			continue;
7160d94caffSDavid Greenman 		}
71767bf6868SJohn Dyson 
7187e006499SJohn Dyson 		/*
7197e006499SJohn Dyson 		 * If the upper level VM system doesn't know anything about the
7207e006499SJohn Dyson 		 * page being dirty, we have to check for it again.  As far as the
7217e006499SJohn Dyson 		 * VM code knows, any partially dirty pages are fully dirty.
7227e006499SJohn Dyson 		 */
723f6b04d2bSDavid Greenman 		if (m->dirty == 0) {
724bd7e5f99SJohn Dyson 			vm_page_test_dirty(m);
725427e99a0SAlexander Langer 		} else {
726bd7e5f99SJohn Dyson 			m->dirty = VM_PAGE_BITS_ALL;
72730dcfc09SJohn Dyson 		}
728ef743ce6SJohn Dyson 
7297e006499SJohn Dyson 		/*
7307e006499SJohn Dyson 		 * Invalid pages can be easily freed
7317e006499SJohn Dyson 		 */
7326d40c3d3SDavid Greenman 		if (m->valid == 0) {
733925a3a41SJohn Dyson 			vm_pageout_page_free(m);
73467bf6868SJohn Dyson 			cnt.v_dfree++;
735925a3a41SJohn Dyson 			pages_freed++;
7367e006499SJohn Dyson 
7377e006499SJohn Dyson 		/*
7387e006499SJohn Dyson 		 * Clean pages can be placed onto the cache queue.
7397e006499SJohn Dyson 		 */
740bd7e5f99SJohn Dyson 		} else if (m->dirty == 0) {
741bd7e5f99SJohn Dyson 			vm_page_cache(m);
742925a3a41SJohn Dyson 			pages_freed++;
7437e006499SJohn Dyson 
7447e006499SJohn Dyson 		/*
7457e006499SJohn Dyson 		 * Dirty pages need to be paged out.  Note that we clean
7467e006499SJohn Dyson 		 * only a limited number of pages per pagedaemon pass.
7477e006499SJohn Dyson 		 */
7480d94caffSDavid Greenman 		} else if (maxlaunder > 0) {
7490d94caffSDavid Greenman 			int written;
75012ac6a1dSJohn Dyson 			int swap_pageouts_ok;
751f6b04d2bSDavid Greenman 			struct vnode *vp = NULL;
7520d94caffSDavid Greenman 
7530d94caffSDavid Greenman 			object = m->object;
7547e006499SJohn Dyson 
75512ac6a1dSJohn Dyson 			if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) {
75612ac6a1dSJohn Dyson 				swap_pageouts_ok = 1;
75712ac6a1dSJohn Dyson 			} else {
75812ac6a1dSJohn Dyson 				swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts);
75912ac6a1dSJohn Dyson 				swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts &&
76012ac6a1dSJohn Dyson 					(cnt.v_free_count + cnt.v_cache_count) < cnt.v_free_min);
76112ac6a1dSJohn Dyson 
76212ac6a1dSJohn Dyson 			}
76370111b90SJohn Dyson 
76470111b90SJohn Dyson 			/*
76570111b90SJohn Dyson 			 * We don't bother paging objects that are "dead".  Those
76670111b90SJohn Dyson 			 * objects are in a "rundown" state.
76770111b90SJohn Dyson 			 */
76870111b90SJohn Dyson 			if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) {
76912ac6a1dSJohn Dyson 				s = splvm();
77012ac6a1dSJohn Dyson 				TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq);
77112ac6a1dSJohn Dyson 				TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq);
77212ac6a1dSJohn Dyson 				splx(s);
77312ac6a1dSJohn Dyson 				continue;
77412ac6a1dSJohn Dyson 			}
77512ac6a1dSJohn Dyson 
776eaf13dd7SJohn Dyson 			if ((object->type == OBJT_VNODE) &&
777eaf13dd7SJohn Dyson 				(object->flags & OBJ_DEAD) == 0) {
77824a1cce3SDavid Greenman 				vp = object->handle;
779996c772fSJohn Dyson 				if (VOP_ISLOCKED(vp) ||
78047221757SJohn Dyson 				    vget(vp, LK_EXCLUSIVE|LK_NOOBJ, curproc)) {
781b182ec9eSJohn Dyson 					if ((m->queue == PQ_INACTIVE) &&
782b182ec9eSJohn Dyson 						(m->hold_count == 0) &&
783b182ec9eSJohn Dyson 						(m->busy == 0) &&
784b182ec9eSJohn Dyson 						(m->flags & PG_BUSY) == 0) {
785f35329acSJohn Dyson 						s = splvm();
78685a376ebSJohn Dyson 						TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq);
78785a376ebSJohn Dyson 						TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq);
788f35329acSJohn Dyson 						splx(s);
78985a376ebSJohn Dyson 					}
790aef922f5SJohn Dyson 					if (object->flags & OBJ_MIGHTBEDIRTY)
791925a3a41SJohn Dyson 						vnodes_skipped++;
792b182ec9eSJohn Dyson 					continue;
79385a376ebSJohn Dyson 				}
794b182ec9eSJohn Dyson 
795f35329acSJohn Dyson 				/*
796f35329acSJohn Dyson 				 * The page might have been moved to another queue
797f35329acSJohn Dyson 				 * during potential blocking in vget() above.
798f35329acSJohn Dyson 				 */
799b182ec9eSJohn Dyson 				if (m->queue != PQ_INACTIVE) {
800b182ec9eSJohn Dyson 					if (object->flags & OBJ_MIGHTBEDIRTY)
801925a3a41SJohn Dyson 						vnodes_skipped++;
802b182ec9eSJohn Dyson 					vput(vp);
803b182ec9eSJohn Dyson 					continue;
804b182ec9eSJohn Dyson 				}
805b182ec9eSJohn Dyson 
806f35329acSJohn Dyson 				/*
807f35329acSJohn Dyson 				 * The page may have been busied during the blocking in
808f35329acSJohn Dyson 				 * vput();  We don't move the page back onto the end of
809f35329acSJohn Dyson 				 * the queue so that statistics are more correct if we don't.
810f35329acSJohn Dyson 				 */
811b182ec9eSJohn Dyson 				if (m->busy || (m->flags & PG_BUSY)) {
812b182ec9eSJohn Dyson 					vput(vp);
813b182ec9eSJohn Dyson 					continue;
814b182ec9eSJohn Dyson 				}
815b182ec9eSJohn Dyson 
816f35329acSJohn Dyson 				/*
817f35329acSJohn Dyson 				 * If the page has become held, then skip it
818f35329acSJohn Dyson 				 */
819b182ec9eSJohn Dyson 				if (m->hold_count) {
820f35329acSJohn Dyson 					s = splvm();
821b182ec9eSJohn Dyson 					TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq);
822b182ec9eSJohn Dyson 					TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq);
823f35329acSJohn Dyson 					splx(s);
824b182ec9eSJohn Dyson 					if (object->flags & OBJ_MIGHTBEDIRTY)
825925a3a41SJohn Dyson 						vnodes_skipped++;
826b182ec9eSJohn Dyson 					vput(vp);
827f6b04d2bSDavid Greenman 					continue;
828f6b04d2bSDavid Greenman 				}
829f6b04d2bSDavid Greenman 			}
830f6b04d2bSDavid Greenman 
8310d94caffSDavid Greenman 			/*
8320d94caffSDavid Greenman 			 * If a page is dirty, then it is either being washed
8330d94caffSDavid Greenman 			 * (but not yet cleaned) or it is still in the
8340d94caffSDavid Greenman 			 * laundry.  If it is still in the laundry, then we
8350d94caffSDavid Greenman 			 * start the cleaning operation.
8360d94caffSDavid Greenman 			 */
8378f9110f6SJohn Dyson 			written = vm_pageout_clean(m);
838f6b04d2bSDavid Greenman 			if (vp)
839f6b04d2bSDavid Greenman 				vput(vp);
840f6b04d2bSDavid Greenman 
8410d94caffSDavid Greenman 			maxlaunder -= written;
8420d94caffSDavid Greenman 		}
843df8bae1dSRodney W. Grimes 	}
84426f9a767SRodney W. Grimes 
845df8bae1dSRodney W. Grimes 	/*
8460d94caffSDavid Greenman 	 * Compute the page shortage.  If we are still very low on memory be
8470d94caffSDavid Greenman 	 * sure that we will move a minimal amount of pages from active to
8480d94caffSDavid Greenman 	 * inactive.
849df8bae1dSRodney W. Grimes 	 */
850b182ec9eSJohn Dyson 	page_shortage = (cnt.v_inactive_target + cnt.v_cache_min) -
8510d94caffSDavid Greenman 	    (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count);
852b182ec9eSJohn Dyson 	page_shortage += addl_page_shortage;
853bef608bdSJohn Dyson 	if (page_shortage <= 0) {
854bef608bdSJohn Dyson 		page_shortage = 0;
855b182ec9eSJohn Dyson 	}
85626f9a767SRodney W. Grimes 
857b18bfc3dSJohn Dyson 	pcount = cnt.v_active_count;
858b18bfc3dSJohn Dyson 	m = TAILQ_FIRST(&vm_page_queue_active);
859b18bfc3dSJohn Dyson 	while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) {
860f35329acSJohn Dyson 
8617e006499SJohn Dyson 		/*
8627e006499SJohn Dyson 		 * This is a consistancy check, and should likely be a panic
8637e006499SJohn Dyson 		 * or warning.
8647e006499SJohn Dyson 		 */
865f35329acSJohn Dyson 		if (m->queue != PQ_ACTIVE) {
86638efa82bSJohn Dyson 			break;
867f35329acSJohn Dyson 		}
868f35329acSJohn Dyson 
869b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(m, pageq);
870df8bae1dSRodney W. Grimes 		/*
87126f9a767SRodney W. Grimes 		 * Don't deactivate pages that are busy.
872df8bae1dSRodney W. Grimes 		 */
873a647a309SDavid Greenman 		if ((m->busy != 0) ||
8740d94caffSDavid Greenman 		    (m->flags & PG_BUSY) ||
875f6b04d2bSDavid Greenman 		    (m->hold_count != 0)) {
876f35329acSJohn Dyson 			s = splvm();
8776d40c3d3SDavid Greenman 			TAILQ_REMOVE(&vm_page_queue_active, m, pageq);
8786d40c3d3SDavid Greenman 			TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq);
879f35329acSJohn Dyson 			splx(s);
88026f9a767SRodney W. Grimes 			m = next;
88126f9a767SRodney W. Grimes 			continue;
882df8bae1dSRodney W. Grimes 		}
883b18bfc3dSJohn Dyson 
884b18bfc3dSJohn Dyson 		/*
885b18bfc3dSJohn Dyson 		 * The count for pagedaemon pages is done after checking the
886b18bfc3dSJohn Dyson 		 * page for eligbility...
887b18bfc3dSJohn Dyson 		 */
888b18bfc3dSJohn Dyson 		cnt.v_pdpages++;
889ef743ce6SJohn Dyson 
8907e006499SJohn Dyson 		/*
8917e006499SJohn Dyson 		 * Check to see "how much" the page has been used.
8927e006499SJohn Dyson 		 */
8937e006499SJohn Dyson 		actcount = 0;
894ef743ce6SJohn Dyson 		if (m->object->ref_count != 0) {
895ef743ce6SJohn Dyson 			if (m->flags & PG_REFERENCED) {
8967e006499SJohn Dyson 				actcount += 1;
8970d94caffSDavid Greenman 			}
8987e006499SJohn Dyson 			actcount += pmap_ts_referenced(VM_PAGE_TO_PHYS(m));
8997e006499SJohn Dyson 			if (actcount) {
9007e006499SJohn Dyson 				m->act_count += ACT_ADVANCE + actcount;
90138efa82bSJohn Dyson 				if (m->act_count > ACT_MAX)
90238efa82bSJohn Dyson 					m->act_count = ACT_MAX;
90338efa82bSJohn Dyson 			}
904b18bfc3dSJohn Dyson 		}
905ef743ce6SJohn Dyson 
9067e006499SJohn Dyson 		/*
9077e006499SJohn Dyson 		 * Since we have "tested" this bit, we need to clear it now.
9087e006499SJohn Dyson 		 */
909b18bfc3dSJohn Dyson 		m->flags &= ~PG_REFERENCED;
910ef743ce6SJohn Dyson 
9117e006499SJohn Dyson 		/*
9127e006499SJohn Dyson 		 * Only if an object is currently being used, do we use the
9137e006499SJohn Dyson 		 * page activation count stats.
9147e006499SJohn Dyson 		 */
9157e006499SJohn Dyson 		if (actcount && (m->object->ref_count != 0)) {
916f35329acSJohn Dyson 			s = splvm();
91726f9a767SRodney W. Grimes 			TAILQ_REMOVE(&vm_page_queue_active, m, pageq);
91826f9a767SRodney W. Grimes 			TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq);
919f35329acSJohn Dyson 			splx(s);
92026f9a767SRodney W. Grimes 		} else {
92138efa82bSJohn Dyson 			m->act_count -= min(m->act_count, ACT_DECLINE);
92238efa82bSJohn Dyson 			if (vm_pageout_algorithm_lru ||
92338efa82bSJohn Dyson 				(m->object->ref_count == 0) || (m->act_count == 0)) {
924925a3a41SJohn Dyson 				page_shortage--;
925d4a272dbSJohn Dyson 				if (m->object->ref_count == 0) {
926ef743ce6SJohn Dyson 					vm_page_protect(m, VM_PROT_NONE);
927d4a272dbSJohn Dyson 					if (m->dirty == 0)
9280d94caffSDavid Greenman 						vm_page_cache(m);
929d4a272dbSJohn Dyson 					else
930d4a272dbSJohn Dyson 						vm_page_deactivate(m);
9310d94caffSDavid Greenman 				} else {
93226f9a767SRodney W. Grimes 					vm_page_deactivate(m);
933df8bae1dSRodney W. Grimes 				}
93438efa82bSJohn Dyson 			} else {
93538efa82bSJohn Dyson 				s = splvm();
93638efa82bSJohn Dyson 				TAILQ_REMOVE(&vm_page_queue_active, m, pageq);
93738efa82bSJohn Dyson 				TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq);
93838efa82bSJohn Dyson 				splx(s);
93938efa82bSJohn Dyson 			}
940df8bae1dSRodney W. Grimes 		}
94126f9a767SRodney W. Grimes 		m = next;
94226f9a767SRodney W. Grimes 	}
943df8bae1dSRodney W. Grimes 
944f35329acSJohn Dyson 	s = splvm();
945df8bae1dSRodney W. Grimes 	/*
9460d94caffSDavid Greenman 	 * We try to maintain some *really* free pages, this allows interrupt
9470d94caffSDavid Greenman 	 * code to be guaranteed space.
948df8bae1dSRodney W. Grimes 	 */
949a1f6d91cSDavid Greenman 	while (cnt.v_free_count < cnt.v_free_reserved) {
9505070c7f8SJohn Dyson 		static int cache_rover = 0;
9515070c7f8SJohn Dyson 		m = vm_page_list_find(PQ_CACHE, cache_rover);
9520d94caffSDavid Greenman 		if (!m)
9530d94caffSDavid Greenman 			break;
9545070c7f8SJohn Dyson 		cache_rover = (cache_rover + PQ_PRIME2) & PQ_L2_MASK;
955925a3a41SJohn Dyson 		vm_pageout_page_free(m);
9560bb3a0d2SDavid Greenman 		cnt.v_dfree++;
95726f9a767SRodney W. Grimes 	}
958f35329acSJohn Dyson 	splx(s);
9595663e6deSDavid Greenman 
960ceb0cf87SJohn Dyson #if !defined(NO_SWAPPING)
961ceb0cf87SJohn Dyson 	/*
962ceb0cf87SJohn Dyson 	 * Idle process swapout -- run once per second.
963ceb0cf87SJohn Dyson 	 */
964ceb0cf87SJohn Dyson 	if (vm_swap_idle_enabled) {
965ceb0cf87SJohn Dyson 		static long lsec;
966227ee8a1SPoul-Henning Kamp 		if (time_second != lsec) {
967ceb0cf87SJohn Dyson 			vm_pageout_req_swapout |= VM_SWAP_IDLE;
968ceb0cf87SJohn Dyson 			vm_req_vmdaemon();
969227ee8a1SPoul-Henning Kamp 			lsec = time_second;
970ceb0cf87SJohn Dyson 		}
971ceb0cf87SJohn Dyson 	}
972ceb0cf87SJohn Dyson #endif
973ceb0cf87SJohn Dyson 
9745663e6deSDavid Greenman 	/*
975f6b04d2bSDavid Greenman 	 * If we didn't get enough free pages, and we have skipped a vnode
9764c1f8ee9SDavid Greenman 	 * in a writeable object, wakeup the sync daemon.  And kick swapout
9774c1f8ee9SDavid Greenman 	 * if we did not get enough free pages.
978f6b04d2bSDavid Greenman 	 */
979bd7e5f99SJohn Dyson 	if ((cnt.v_cache_count + cnt.v_free_count) <
980bd7e5f99SJohn Dyson 		(cnt.v_free_target + cnt.v_cache_min) ) {
981f6b04d2bSDavid Greenman 		if (vnodes_skipped &&
982f6b04d2bSDavid Greenman 		    (cnt.v_cache_count + cnt.v_free_count) < cnt.v_free_min) {
983f6b04d2bSDavid Greenman 			if (!vfs_update_wakeup) {
984f6b04d2bSDavid Greenman 				vfs_update_wakeup = 1;
98524a1cce3SDavid Greenman 				wakeup(&vfs_update_wakeup);
986f6b04d2bSDavid Greenman 			}
987f6b04d2bSDavid Greenman 		}
98838efa82bSJohn Dyson #if !defined(NO_SWAPPING)
989ceb0cf87SJohn Dyson 		if (vm_swap_enabled &&
99038efa82bSJohn Dyson 			(cnt.v_free_count + cnt.v_cache_count < cnt.v_free_target)) {
9914c1f8ee9SDavid Greenman 			vm_req_vmdaemon();
992ceb0cf87SJohn Dyson 			vm_pageout_req_swapout |= VM_SWAP_NORMAL;
9934c1f8ee9SDavid Greenman 		}
9945afce282SDavid Greenman #endif
9954c1f8ee9SDavid Greenman 	}
9964c1f8ee9SDavid Greenman 
997f6b04d2bSDavid Greenman 
998f6b04d2bSDavid Greenman 	/*
9990d94caffSDavid Greenman 	 * make sure that we have swap space -- if we are low on memory and
10000d94caffSDavid Greenman 	 * swap -- then kill the biggest process.
10015663e6deSDavid Greenman 	 */
10025663e6deSDavid Greenman 	if ((vm_swap_size == 0 || swap_pager_full) &&
10030d94caffSDavid Greenman 	    ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_free_min)) {
10045663e6deSDavid Greenman 		bigproc = NULL;
10055663e6deSDavid Greenman 		bigsize = 0;
10061b67ec6dSJeffrey Hsu 		for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
10075663e6deSDavid Greenman 			/*
10085663e6deSDavid Greenman 			 * if this is a system process, skip it
10095663e6deSDavid Greenman 			 */
101079221631SDavid Greenman 			if ((p->p_flag & P_SYSTEM) || (p->p_pid == 1) ||
101179221631SDavid Greenman 			    ((p->p_pid < 48) && (vm_swap_size != 0))) {
10125663e6deSDavid Greenman 				continue;
10135663e6deSDavid Greenman 			}
10145663e6deSDavid Greenman 			/*
10155663e6deSDavid Greenman 			 * if the process is in a non-running type state,
10165663e6deSDavid Greenman 			 * don't touch it.
10175663e6deSDavid Greenman 			 */
10185663e6deSDavid Greenman 			if (p->p_stat != SRUN && p->p_stat != SSLEEP) {
10195663e6deSDavid Greenman 				continue;
10205663e6deSDavid Greenman 			}
10215663e6deSDavid Greenman 			/*
10225663e6deSDavid Greenman 			 * get the process size
10235663e6deSDavid Greenman 			 */
10245663e6deSDavid Greenman 			size = p->p_vmspace->vm_pmap.pm_stats.resident_count;
10255663e6deSDavid Greenman 			/*
10265663e6deSDavid Greenman 			 * if the this process is bigger than the biggest one
10275663e6deSDavid Greenman 			 * remember it.
10285663e6deSDavid Greenman 			 */
10295663e6deSDavid Greenman 			if (size > bigsize) {
10305663e6deSDavid Greenman 				bigproc = p;
10315663e6deSDavid Greenman 				bigsize = size;
10325663e6deSDavid Greenman 			}
10335663e6deSDavid Greenman 		}
10345663e6deSDavid Greenman 		if (bigproc != NULL) {
1035729b1e51SDavid Greenman 			killproc(bigproc, "out of swap space");
10365663e6deSDavid Greenman 			bigproc->p_estcpu = 0;
10375663e6deSDavid Greenman 			bigproc->p_nice = PRIO_MIN;
10385663e6deSDavid Greenman 			resetpriority(bigproc);
103924a1cce3SDavid Greenman 			wakeup(&cnt.v_free_count);
10405663e6deSDavid Greenman 		}
10415663e6deSDavid Greenman 	}
104226f9a767SRodney W. Grimes 	return force_wakeup;
104326f9a767SRodney W. Grimes }
104426f9a767SRodney W. Grimes 
1045dc2efb27SJohn Dyson /*
1046dc2efb27SJohn Dyson  * This routine tries to maintain the pseudo LRU active queue,
1047dc2efb27SJohn Dyson  * so that during long periods of time where there is no paging,
1048dc2efb27SJohn Dyson  * that some statistic accumlation still occurs.  This code
1049dc2efb27SJohn Dyson  * helps the situation where paging just starts to occur.
1050dc2efb27SJohn Dyson  */
1051dc2efb27SJohn Dyson static void
1052dc2efb27SJohn Dyson vm_pageout_page_stats()
1053dc2efb27SJohn Dyson {
1054dc2efb27SJohn Dyson 	int s;
1055dc2efb27SJohn Dyson 	vm_page_t m,next;
1056dc2efb27SJohn Dyson 	int pcount,tpcount;		/* Number of pages to check */
1057dc2efb27SJohn Dyson 	static int fullintervalcount = 0;
1058bef608bdSJohn Dyson 	int page_shortage;
1059bef608bdSJohn Dyson 
1060bef608bdSJohn Dyson 	page_shortage = (cnt.v_inactive_target + cnt.v_cache_max + cnt.v_free_min) -
1061bef608bdSJohn Dyson 	    (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count);
1062bef608bdSJohn Dyson 	if (page_shortage <= 0)
1063bef608bdSJohn Dyson 		return;
1064dc2efb27SJohn Dyson 
1065dc2efb27SJohn Dyson 	pcount = cnt.v_active_count;
1066dc2efb27SJohn Dyson 	fullintervalcount += vm_pageout_stats_interval;
1067dc2efb27SJohn Dyson 	if (fullintervalcount < vm_pageout_full_stats_interval) {
1068dc2efb27SJohn Dyson 		tpcount = (vm_pageout_stats_max * cnt.v_active_count) / cnt.v_page_count;
1069dc2efb27SJohn Dyson 		if (pcount > tpcount)
1070dc2efb27SJohn Dyson 			pcount = tpcount;
1071dc2efb27SJohn Dyson 	}
1072dc2efb27SJohn Dyson 
1073dc2efb27SJohn Dyson 	m = TAILQ_FIRST(&vm_page_queue_active);
1074dc2efb27SJohn Dyson 	while ((m != NULL) && (pcount-- > 0)) {
10757e006499SJohn Dyson 		int actcount;
1076dc2efb27SJohn Dyson 
1077dc2efb27SJohn Dyson 		if (m->queue != PQ_ACTIVE) {
1078dc2efb27SJohn Dyson 			break;
1079dc2efb27SJohn Dyson 		}
1080dc2efb27SJohn Dyson 
1081dc2efb27SJohn Dyson 		next = TAILQ_NEXT(m, pageq);
1082dc2efb27SJohn Dyson 		/*
1083dc2efb27SJohn Dyson 		 * Don't deactivate pages that are busy.
1084dc2efb27SJohn Dyson 		 */
1085dc2efb27SJohn Dyson 		if ((m->busy != 0) ||
1086dc2efb27SJohn Dyson 		    (m->flags & PG_BUSY) ||
1087dc2efb27SJohn Dyson 		    (m->hold_count != 0)) {
1088dc2efb27SJohn Dyson 			s = splvm();
1089dc2efb27SJohn Dyson 			TAILQ_REMOVE(&vm_page_queue_active, m, pageq);
1090dc2efb27SJohn Dyson 			TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq);
1091dc2efb27SJohn Dyson 			splx(s);
1092dc2efb27SJohn Dyson 			m = next;
1093dc2efb27SJohn Dyson 			continue;
1094dc2efb27SJohn Dyson 		}
1095dc2efb27SJohn Dyson 
10967e006499SJohn Dyson 		actcount = 0;
1097dc2efb27SJohn Dyson 		if (m->flags & PG_REFERENCED) {
1098dc2efb27SJohn Dyson 			m->flags &= ~PG_REFERENCED;
10997e006499SJohn Dyson 			actcount += 1;
1100dc2efb27SJohn Dyson 		}
1101dc2efb27SJohn Dyson 
11027e006499SJohn Dyson 		actcount += pmap_ts_referenced(VM_PAGE_TO_PHYS(m));
11037e006499SJohn Dyson 		if (actcount) {
11047e006499SJohn Dyson 			m->act_count += ACT_ADVANCE + actcount;
1105dc2efb27SJohn Dyson 			if (m->act_count > ACT_MAX)
1106dc2efb27SJohn Dyson 				m->act_count = ACT_MAX;
1107dc2efb27SJohn Dyson 			s = splvm();
1108dc2efb27SJohn Dyson 			TAILQ_REMOVE(&vm_page_queue_active, m, pageq);
1109dc2efb27SJohn Dyson 			TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq);
1110dc2efb27SJohn Dyson 			splx(s);
1111dc2efb27SJohn Dyson 		} else {
1112dc2efb27SJohn Dyson 			if (m->act_count == 0) {
11137e006499SJohn Dyson 				/*
11147e006499SJohn Dyson 				 * We turn off page access, so that we have more accurate
11157e006499SJohn Dyson 				 * RSS stats.  We don't do this in the normal page deactivation
11167e006499SJohn Dyson 				 * when the system is loaded VM wise, because the cost of
11177e006499SJohn Dyson 				 * the large number of page protect operations would be higher
11187e006499SJohn Dyson 				 * than the value of doing the operation.
11197e006499SJohn Dyson 				 */
1120dc2efb27SJohn Dyson 				vm_page_protect(m, VM_PROT_NONE);
1121dc2efb27SJohn Dyson 				vm_page_deactivate(m);
1122dc2efb27SJohn Dyson 			} else {
1123dc2efb27SJohn Dyson 				m->act_count -= min(m->act_count, ACT_DECLINE);
1124dc2efb27SJohn Dyson 				s = splvm();
1125dc2efb27SJohn Dyson 				TAILQ_REMOVE(&vm_page_queue_active, m, pageq);
1126dc2efb27SJohn Dyson 				TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq);
1127dc2efb27SJohn Dyson 				splx(s);
1128dc2efb27SJohn Dyson 			}
1129dc2efb27SJohn Dyson 		}
1130dc2efb27SJohn Dyson 
1131dc2efb27SJohn Dyson 		m = next;
1132dc2efb27SJohn Dyson 	}
1133dc2efb27SJohn Dyson }
1134dc2efb27SJohn Dyson 
1135b182ec9eSJohn Dyson static int
1136b182ec9eSJohn Dyson vm_pageout_free_page_calc(count)
1137b182ec9eSJohn Dyson vm_size_t count;
1138b182ec9eSJohn Dyson {
1139b182ec9eSJohn Dyson 	if (count < cnt.v_page_count)
1140b182ec9eSJohn Dyson 		 return 0;
1141b182ec9eSJohn Dyson 	/*
1142b182ec9eSJohn Dyson 	 * free_reserved needs to include enough for the largest swap pager
1143b182ec9eSJohn Dyson 	 * structures plus enough for any pv_entry structs when paging.
1144b182ec9eSJohn Dyson 	 */
1145b182ec9eSJohn Dyson 	if (cnt.v_page_count > 1024)
1146b182ec9eSJohn Dyson 		cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200;
1147b182ec9eSJohn Dyson 	else
1148b182ec9eSJohn Dyson 		cnt.v_free_min = 4;
1149f35329acSJohn Dyson 	cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE +
1150f35329acSJohn Dyson 		cnt.v_interrupt_free_min;
1151f35329acSJohn Dyson 	cnt.v_free_reserved = vm_pageout_page_count +
1152a15403deSJohn Dyson 		cnt.v_pageout_free_min + (count / 768) + PQ_L2_SIZE;
1153a2f4a846SJohn Dyson 	cnt.v_free_min += cnt.v_free_reserved;
1154b182ec9eSJohn Dyson 	return 1;
1155b182ec9eSJohn Dyson }
1156b182ec9eSJohn Dyson 
1157b182ec9eSJohn Dyson 
1158df8bae1dSRodney W. Grimes /*
1159df8bae1dSRodney W. Grimes  *	vm_pageout is the high level pageout daemon.
1160df8bae1dSRodney W. Grimes  */
11612b14f991SJulian Elischer static void
116226f9a767SRodney W. Grimes vm_pageout()
1163df8bae1dSRodney W. Grimes {
1164df8bae1dSRodney W. Grimes 	/*
1165df8bae1dSRodney W. Grimes 	 * Initialize some paging parameters.
1166df8bae1dSRodney W. Grimes 	 */
1167df8bae1dSRodney W. Grimes 
1168f6b04d2bSDavid Greenman 	cnt.v_interrupt_free_min = 2;
1169f35329acSJohn Dyson 	if (cnt.v_page_count < 2000)
1170f35329acSJohn Dyson 		vm_pageout_page_count = 8;
1171f6b04d2bSDavid Greenman 
1172b182ec9eSJohn Dyson 	vm_pageout_free_page_calc(cnt.v_page_count);
1173ed74321bSDavid Greenman 	/*
11740d94caffSDavid Greenman 	 * free_reserved needs to include enough for the largest swap pager
11750d94caffSDavid Greenman 	 * structures plus enough for any pv_entry structs when paging.
1176ed74321bSDavid Greenman 	 */
1177a15403deSJohn Dyson 	if (cnt.v_free_count > 6144)
11780d94caffSDavid Greenman 		cnt.v_free_target = 3 * cnt.v_free_min + cnt.v_free_reserved;
1179a15403deSJohn Dyson 	else
1180a15403deSJohn Dyson 		cnt.v_free_target = 2 * cnt.v_free_min + cnt.v_free_reserved;
11816f2b142eSDavid Greenman 
1182a15403deSJohn Dyson 	if (cnt.v_free_count > 2048) {
1183a15403deSJohn Dyson 		cnt.v_cache_min = cnt.v_free_target;
1184a15403deSJohn Dyson 		cnt.v_cache_max = 2 * cnt.v_cache_min;
1185a15403deSJohn Dyson 		cnt.v_inactive_target = (3 * cnt.v_free_target) / 2;
11860d94caffSDavid Greenman 	} else {
11870d94caffSDavid Greenman 		cnt.v_cache_min = 0;
11880d94caffSDavid Greenman 		cnt.v_cache_max = 0;
11896f2b142eSDavid Greenman 		cnt.v_inactive_target = cnt.v_free_count / 4;
11900d94caffSDavid Greenman 	}
1191e47ed70bSJohn Dyson 	if (cnt.v_inactive_target > cnt.v_free_count / 3)
1192e47ed70bSJohn Dyson 		cnt.v_inactive_target = cnt.v_free_count / 3;
1193df8bae1dSRodney W. Grimes 
1194df8bae1dSRodney W. Grimes 	/* XXX does not really belong here */
1195df8bae1dSRodney W. Grimes 	if (vm_page_max_wired == 0)
1196df8bae1dSRodney W. Grimes 		vm_page_max_wired = cnt.v_free_count / 3;
1197df8bae1dSRodney W. Grimes 
1198dc2efb27SJohn Dyson 	if (vm_pageout_stats_max == 0)
1199dc2efb27SJohn Dyson 		vm_pageout_stats_max = cnt.v_free_target;
1200dc2efb27SJohn Dyson 
1201dc2efb27SJohn Dyson 	/*
1202dc2efb27SJohn Dyson 	 * Set interval in seconds for stats scan.
1203dc2efb27SJohn Dyson 	 */
1204dc2efb27SJohn Dyson 	if (vm_pageout_stats_interval == 0)
1205bef608bdSJohn Dyson 		vm_pageout_stats_interval = 5;
1206dc2efb27SJohn Dyson 	if (vm_pageout_full_stats_interval == 0)
1207dc2efb27SJohn Dyson 		vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4;
1208dc2efb27SJohn Dyson 
1209dc2efb27SJohn Dyson 
1210dc2efb27SJohn Dyson 	/*
1211dc2efb27SJohn Dyson 	 * Set maximum free per pass
1212dc2efb27SJohn Dyson 	 */
1213dc2efb27SJohn Dyson 	if (vm_pageout_stats_free_max == 0)
1214bef608bdSJohn Dyson 		vm_pageout_stats_free_max = 5;
1215dc2efb27SJohn Dyson 
1216ceb0cf87SJohn Dyson 	max_page_launder = (cnt.v_page_count > 1800 ? 32 : 16);
121726f9a767SRodney W. Grimes 
121824a1cce3SDavid Greenman 	swap_pager_swap_init();
1219df8bae1dSRodney W. Grimes 	/*
12200d94caffSDavid Greenman 	 * The pageout daemon is never done, so loop forever.
1221df8bae1dSRodney W. Grimes 	 */
1222df8bae1dSRodney W. Grimes 	while (TRUE) {
122385a376ebSJohn Dyson 		int inactive_target;
1224dc2efb27SJohn Dyson 		int error;
1225b18bfc3dSJohn Dyson 		int s = splvm();
1226f919ebdeSDavid Greenman 		if (!vm_pages_needed ||
1227545901f7SJohn Dyson 			((cnt.v_free_count + cnt.v_cache_count) > cnt.v_free_min)) {
1228f919ebdeSDavid Greenman 			vm_pages_needed = 0;
1229dc2efb27SJohn Dyson 			error = tsleep(&vm_pages_needed,
1230dc2efb27SJohn Dyson 				PVM, "psleep", vm_pageout_stats_interval * hz);
1231dc2efb27SJohn Dyson 			if (error && !vm_pages_needed) {
1232dc2efb27SJohn Dyson 				splx(s);
1233dc2efb27SJohn Dyson 				vm_pageout_page_stats();
1234dc2efb27SJohn Dyson 				continue;
1235dc2efb27SJohn Dyson 			}
1236dc2efb27SJohn Dyson 		} else if (vm_pages_needed) {
1237e47ed70bSJohn Dyson 			vm_pages_needed = 0;
1238e47ed70bSJohn Dyson 			tsleep(&vm_pages_needed, PVM, "psleep", hz/2);
1239f919ebdeSDavid Greenman 		}
1240e47ed70bSJohn Dyson 
1241b18bfc3dSJohn Dyson 		if (vm_pages_needed)
1242b18bfc3dSJohn Dyson 			cnt.v_pdwakeups++;
1243f919ebdeSDavid Greenman 		vm_pages_needed = 0;
1244f919ebdeSDavid Greenman 		splx(s);
1245df8bae1dSRodney W. Grimes 		vm_pager_sync();
12460d94caffSDavid Greenman 		vm_pageout_scan();
12472d8acc0fSJohn Dyson 		vm_pageout_deficit = 0;
124826f9a767SRodney W. Grimes 		vm_pager_sync();
124924a1cce3SDavid Greenman 		wakeup(&cnt.v_free_count);
1250df8bae1dSRodney W. Grimes 	}
1251df8bae1dSRodney W. Grimes }
125226f9a767SRodney W. Grimes 
1253e0c5a895SJohn Dyson void
1254e0c5a895SJohn Dyson pagedaemon_wakeup()
1255e0c5a895SJohn Dyson {
1256e0c5a895SJohn Dyson 	if (!vm_pages_needed && curproc != pageproc) {
1257e0c5a895SJohn Dyson 		vm_pages_needed++;
1258e0c5a895SJohn Dyson 		wakeup(&vm_pages_needed);
1259e0c5a895SJohn Dyson 	}
1260e0c5a895SJohn Dyson }
1261e0c5a895SJohn Dyson 
126238efa82bSJohn Dyson #if !defined(NO_SWAPPING)
12635afce282SDavid Greenman static void
12645afce282SDavid Greenman vm_req_vmdaemon()
12655afce282SDavid Greenman {
12665afce282SDavid Greenman 	static int lastrun = 0;
12675afce282SDavid Greenman 
1268b18bfc3dSJohn Dyson 	if ((ticks > (lastrun + hz)) || (ticks < lastrun)) {
12695afce282SDavid Greenman 		wakeup(&vm_daemon_needed);
12705afce282SDavid Greenman 		lastrun = ticks;
12715afce282SDavid Greenman 	}
12725afce282SDavid Greenman }
12735afce282SDavid Greenman 
12742b14f991SJulian Elischer static void
12754f9fb771SBruce Evans vm_daemon()
12760d94caffSDavid Greenman {
12772fe6e4d7SDavid Greenman 	vm_object_t object;
12782fe6e4d7SDavid Greenman 	struct proc *p;
12790d94caffSDavid Greenman 
12802fe6e4d7SDavid Greenman 	while (TRUE) {
1281e8f36785SJohn Dyson 		tsleep(&vm_daemon_needed, PPAUSE, "psleep", 0);
12824c1f8ee9SDavid Greenman 		if (vm_pageout_req_swapout) {
1283ceb0cf87SJohn Dyson 			swapout_procs(vm_pageout_req_swapout);
12844c1f8ee9SDavid Greenman 			vm_pageout_req_swapout = 0;
12854c1f8ee9SDavid Greenman 		}
12862fe6e4d7SDavid Greenman 		/*
12870d94caffSDavid Greenman 		 * scan the processes for exceeding their rlimits or if
12880d94caffSDavid Greenman 		 * process is swapped out -- deactivate pages
12892fe6e4d7SDavid Greenman 		 */
12902fe6e4d7SDavid Greenman 
12911b67ec6dSJeffrey Hsu 		for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
12922fe6e4d7SDavid Greenman 			quad_t limit;
12932fe6e4d7SDavid Greenman 			vm_offset_t size;
12942fe6e4d7SDavid Greenman 
12952fe6e4d7SDavid Greenman 			/*
12962fe6e4d7SDavid Greenman 			 * if this is a system process or if we have already
12972fe6e4d7SDavid Greenman 			 * looked at this process, skip it.
12982fe6e4d7SDavid Greenman 			 */
12992fe6e4d7SDavid Greenman 			if (p->p_flag & (P_SYSTEM | P_WEXIT)) {
13002fe6e4d7SDavid Greenman 				continue;
13012fe6e4d7SDavid Greenman 			}
13022fe6e4d7SDavid Greenman 			/*
13032fe6e4d7SDavid Greenman 			 * if the process is in a non-running type state,
13042fe6e4d7SDavid Greenman 			 * don't touch it.
13052fe6e4d7SDavid Greenman 			 */
13062fe6e4d7SDavid Greenman 			if (p->p_stat != SRUN && p->p_stat != SSLEEP) {
13072fe6e4d7SDavid Greenman 				continue;
13082fe6e4d7SDavid Greenman 			}
13092fe6e4d7SDavid Greenman 			/*
13102fe6e4d7SDavid Greenman 			 * get a limit
13112fe6e4d7SDavid Greenman 			 */
13122fe6e4d7SDavid Greenman 			limit = qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur,
13132fe6e4d7SDavid Greenman 			    p->p_rlimit[RLIMIT_RSS].rlim_max);
13142fe6e4d7SDavid Greenman 
13152fe6e4d7SDavid Greenman 			/*
13160d94caffSDavid Greenman 			 * let processes that are swapped out really be
13170d94caffSDavid Greenman 			 * swapped out set the limit to nothing (will force a
13180d94caffSDavid Greenman 			 * swap-out.)
13192fe6e4d7SDavid Greenman 			 */
13202fe6e4d7SDavid Greenman 			if ((p->p_flag & P_INMEM) == 0)
13210d94caffSDavid Greenman 				limit = 0;	/* XXX */
13222fe6e4d7SDavid Greenman 
1323a91c5a7eSJohn Dyson 			size = p->p_vmspace->vm_pmap.pm_stats.resident_count * PAGE_SIZE;
13242fe6e4d7SDavid Greenman 			if (limit >= 0 && size >= limit) {
13252fe6e4d7SDavid Greenman 				vm_pageout_map_deactivate_pages(&p->p_vmspace->vm_map,
132638efa82bSJohn Dyson 				    (vm_pindex_t)(limit >> PAGE_SHIFT) );
13272fe6e4d7SDavid Greenman 			}
13282fe6e4d7SDavid Greenman 		}
132924a1cce3SDavid Greenman 	}
13302fe6e4d7SDavid Greenman }
133138efa82bSJohn Dyson #endif
1332