xref: /freebsd/sys/vm/vm_pageout.c (revision aef922f514e25254bdc9f55422f00a0217ed7458)
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  *
68aef922f5SJohn Dyson  * $Id: vm_pageout.c,v 1.58 1995/10/23 05:35:48 dyson 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>
8026f9a767SRodney W. Grimes #include <sys/malloc.h>
810d94caffSDavid Greenman #include <sys/kernel.h>
82d2fc5315SPoul-Henning Kamp #include <sys/signalvar.h>
83f6b04d2bSDavid Greenman #include <sys/vnode.h>
84df8bae1dSRodney W. Grimes 
85df8bae1dSRodney W. Grimes #include <vm/vm.h>
86df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
87df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
887c0414d0SDavid Greenman #include <vm/vm_kern.h>
8924a1cce3SDavid Greenman #include <vm/vm_pager.h>
9005f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h>
91df8bae1dSRodney W. Grimes 
922b14f991SJulian Elischer /*
932b14f991SJulian Elischer  * System initialization
942b14f991SJulian Elischer  */
952b14f991SJulian Elischer 
962b14f991SJulian Elischer /* the kernel process "vm_pageout"*/
972b14f991SJulian Elischer static void vm_pageout __P((void));
982b14f991SJulian Elischer struct proc *pageproc;
992b14f991SJulian Elischer 
1002b14f991SJulian Elischer static struct kproc_desc page_kp = {
1012b14f991SJulian Elischer 	"pagedaemon",
1022b14f991SJulian Elischer 	vm_pageout,
1032b14f991SJulian Elischer 	&pageproc
1042b14f991SJulian Elischer };
1054590fd3aSDavid Greenman SYSINIT_KT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, &page_kp)
1062b14f991SJulian Elischer 
1072b14f991SJulian Elischer /* the kernel process "vm_daemon"*/
1082b14f991SJulian Elischer static void vm_daemon __P((void));
1092b14f991SJulian Elischer struct	proc *vmproc;
1102b14f991SJulian Elischer 
1112b14f991SJulian Elischer static struct kproc_desc vm_kp = {
1122b14f991SJulian Elischer 	"vmdaemon",
1132b14f991SJulian Elischer 	vm_daemon,
1142b14f991SJulian Elischer 	&vmproc
1152b14f991SJulian Elischer };
1164590fd3aSDavid Greenman SYSINIT_KT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp)
1172b14f991SJulian Elischer 
1182b14f991SJulian Elischer 
119df8bae1dSRodney W. Grimes int vm_pages_needed;		/* Event on which pageout daemon sleeps */
12026f9a767SRodney W. Grimes 
121c3cb3e12SDavid Greenman int vm_pageout_pages_needed;	/* flag saying that the pageout daemon needs pages */
12226f9a767SRodney W. Grimes 
12326f9a767SRodney W. Grimes extern int npendingio;
1247c0414d0SDavid Greenman int vm_pageout_req_swapout;	/* XXX */
1252fe6e4d7SDavid Greenman int vm_daemon_needed;
12626f9a767SRodney W. Grimes extern int nswiodone;
1275663e6deSDavid Greenman extern int vm_swap_size;
128f6b04d2bSDavid Greenman extern int vfs_update_wakeup;
12926f9a767SRodney W. Grimes 
1301ed81ef2SDavid Greenman #define MAXSCAN 1024		/* maximum number of pages to scan in queues */
13126f9a767SRodney W. Grimes 
1320d94caffSDavid Greenman #define MAXLAUNDER (cnt.v_page_count > 1800 ? 32 : 16)
13326f9a767SRodney W. Grimes 
13426f9a767SRodney W. Grimes #define VM_PAGEOUT_PAGE_COUNT 8
135bbc0ec52SDavid Greenman int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT;
136df8bae1dSRodney W. Grimes 
137c3cb3e12SDavid Greenman int vm_page_max_wired;		/* XXX max # of wired pages system-wide */
138df8bae1dSRodney W. Grimes 
139cd41fc12SDavid Greenman typedef int freeer_fcn_t __P((vm_map_t, vm_object_t, int, int));
140cd41fc12SDavid Greenman static void vm_pageout_map_deactivate_pages __P((vm_map_t, vm_map_entry_t,
141cd41fc12SDavid Greenman 						 int *, freeer_fcn_t *));
142cd41fc12SDavid Greenman static freeer_fcn_t vm_pageout_object_deactivate_pages;
143cd41fc12SDavid Greenman static void vm_req_vmdaemon __P((void));
144cd41fc12SDavid Greenman 
14526f9a767SRodney W. Grimes /*
14626f9a767SRodney W. Grimes  * vm_pageout_clean:
14724a1cce3SDavid Greenman  *
1480d94caffSDavid Greenman  * Clean the page and remove it from the laundry.
14926f9a767SRodney W. Grimes  *
1500d94caffSDavid Greenman  * We set the busy bit to cause potential page faults on this page to
15126f9a767SRodney W. Grimes  * block.
15226f9a767SRodney W. Grimes  *
1530d94caffSDavid Greenman  * And we set pageout-in-progress to keep the object from disappearing
1540d94caffSDavid Greenman  * during pageout.  This guarantees that the page won't move from the
1550d94caffSDavid Greenman  * inactive queue.  (However, any other page on the inactive queue may
1560d94caffSDavid Greenman  * move!)
15726f9a767SRodney W. Grimes  */
15824a1cce3SDavid Greenman int
15924a1cce3SDavid Greenman vm_pageout_clean(m, sync)
16024a1cce3SDavid Greenman 	vm_page_t m;
16124a1cce3SDavid Greenman 	int sync;
16224a1cce3SDavid Greenman {
16326f9a767SRodney W. Grimes 	register vm_object_t object;
16426f9a767SRodney W. Grimes 	int pageout_status[VM_PAGEOUT_PAGE_COUNT];
16524a1cce3SDavid Greenman 	vm_page_t mc[2*VM_PAGEOUT_PAGE_COUNT];
16624a1cce3SDavid Greenman 	int pageout_count;
16724a1cce3SDavid Greenman 	int i, forward_okay, backward_okay, page_base;
16826f9a767SRodney W. Grimes 	vm_offset_t offset = m->offset;
16926f9a767SRodney W. Grimes 
17026f9a767SRodney W. Grimes 	object = m->object;
17124a1cce3SDavid Greenman 
17226f9a767SRodney W. Grimes 	/*
17324a1cce3SDavid Greenman 	 * If not OBJT_SWAP, additional memory may be needed to do the pageout.
17424a1cce3SDavid Greenman 	 * Try to avoid the deadlock.
17526f9a767SRodney W. Grimes 	 */
17624a1cce3SDavid Greenman 	if ((sync != VM_PAGEOUT_FORCE) &&
17724a1cce3SDavid Greenman 	    (object->type != OBJT_SWAP) &&
17824a1cce3SDavid Greenman 	    ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min))
17926f9a767SRodney W. Grimes 		return 0;
18026f9a767SRodney W. Grimes 
18124a1cce3SDavid Greenman 	/*
18224a1cce3SDavid Greenman 	 * Don't mess with the page if it's busy.
18324a1cce3SDavid Greenman 	 */
184f6b04d2bSDavid Greenman 	if ((!sync && m->hold_count != 0) ||
1850d94caffSDavid Greenman 	    ((m->busy != 0) || (m->flags & PG_BUSY)))
1860d94caffSDavid Greenman 		return 0;
1870d94caffSDavid Greenman 
18824a1cce3SDavid Greenman 	/*
18924a1cce3SDavid Greenman 	 * Try collapsing before it's too late.
19024a1cce3SDavid Greenman 	 */
19124a1cce3SDavid Greenman 	if (!sync && object->backing_object) {
19226f9a767SRodney W. Grimes 		vm_object_collapse(object);
19326f9a767SRodney W. Grimes 	}
19424a1cce3SDavid Greenman 	mc[VM_PAGEOUT_PAGE_COUNT] = m;
19526f9a767SRodney W. Grimes 	pageout_count = 1;
19624a1cce3SDavid Greenman 	page_base = VM_PAGEOUT_PAGE_COUNT;
19724a1cce3SDavid Greenman 	forward_okay = TRUE;
19824a1cce3SDavid Greenman 	if (offset != 0)
19924a1cce3SDavid Greenman 		backward_okay = TRUE;
20026f9a767SRodney W. Grimes 	else
20124a1cce3SDavid Greenman 		backward_okay = FALSE;
20224a1cce3SDavid Greenman 	/*
20324a1cce3SDavid Greenman 	 * Scan object for clusterable pages.
20424a1cce3SDavid Greenman 	 *
20524a1cce3SDavid Greenman 	 * We can cluster ONLY if: ->> the page is NOT
20624a1cce3SDavid Greenman 	 * clean, wired, busy, held, or mapped into a
20724a1cce3SDavid Greenman 	 * buffer, and one of the following:
20824a1cce3SDavid Greenman 	 * 1) The page is inactive, or a seldom used
20924a1cce3SDavid Greenman 	 *    active page.
21024a1cce3SDavid Greenman 	 * -or-
21124a1cce3SDavid Greenman 	 * 2) we force the issue.
21224a1cce3SDavid Greenman 	 */
21324a1cce3SDavid Greenman 	for (i = 1; (i < vm_pageout_page_count) && (forward_okay || backward_okay); i++) {
21424a1cce3SDavid Greenman 		vm_page_t p;
215f6b04d2bSDavid Greenman 
21624a1cce3SDavid Greenman 		/*
21724a1cce3SDavid Greenman 		 * See if forward page is clusterable.
21824a1cce3SDavid Greenman 		 */
21924a1cce3SDavid Greenman 		if (forward_okay) {
22024a1cce3SDavid Greenman 			/*
22124a1cce3SDavid Greenman 			 * Stop forward scan at end of object.
22224a1cce3SDavid Greenman 			 */
22324a1cce3SDavid Greenman 			if ((offset + i * PAGE_SIZE) > object->size) {
22424a1cce3SDavid Greenman 				forward_okay = FALSE;
22524a1cce3SDavid Greenman 				goto do_backward;
226f6b04d2bSDavid Greenman 			}
22724a1cce3SDavid Greenman 			p = vm_page_lookup(object, offset + i * PAGE_SIZE);
22824a1cce3SDavid Greenman 			if (p) {
22924a1cce3SDavid Greenman 				if ((p->flags & (PG_BUSY|PG_CACHE)) || p->busy) {
23024a1cce3SDavid Greenman 					forward_okay = FALSE;
23124a1cce3SDavid Greenman 					goto do_backward;
232f6b04d2bSDavid Greenman 				}
23324a1cce3SDavid Greenman 				vm_page_test_dirty(p);
23424a1cce3SDavid Greenman 				if ((p->dirty & p->valid) != 0 &&
23524a1cce3SDavid Greenman 				    ((p->flags & PG_INACTIVE) ||
23624a1cce3SDavid Greenman 				     (sync == VM_PAGEOUT_FORCE)) &&
23724a1cce3SDavid Greenman 				    (p->wire_count == 0) &&
23824a1cce3SDavid Greenman 				    (p->hold_count == 0)) {
23924a1cce3SDavid Greenman 					mc[VM_PAGEOUT_PAGE_COUNT + i] = p;
24024a1cce3SDavid Greenman 					pageout_count++;
24124a1cce3SDavid Greenman 					if (pageout_count == vm_pageout_page_count)
24224a1cce3SDavid Greenman 						break;
24324a1cce3SDavid Greenman 				} else {
24424a1cce3SDavid Greenman 					forward_okay = FALSE;
245f6b04d2bSDavid Greenman 				}
24624a1cce3SDavid Greenman 			} else {
24724a1cce3SDavid Greenman 				forward_okay = FALSE;
24824a1cce3SDavid Greenman 			}
24924a1cce3SDavid Greenman 		}
25024a1cce3SDavid Greenman do_backward:
25124a1cce3SDavid Greenman 		/*
25224a1cce3SDavid Greenman 		 * See if backward page is clusterable.
25324a1cce3SDavid Greenman 		 */
25424a1cce3SDavid Greenman 		if (backward_okay) {
25524a1cce3SDavid Greenman 			/*
25624a1cce3SDavid Greenman 			 * Stop backward scan at beginning of object.
25724a1cce3SDavid Greenman 			 */
25824a1cce3SDavid Greenman 			if ((offset - i * PAGE_SIZE) == 0) {
25924a1cce3SDavid Greenman 				backward_okay = FALSE;
26024a1cce3SDavid Greenman 			}
26124a1cce3SDavid Greenman 			p = vm_page_lookup(object, offset - i * PAGE_SIZE);
26224a1cce3SDavid Greenman 			if (p) {
26324a1cce3SDavid Greenman 				if ((p->flags & (PG_BUSY|PG_CACHE)) || p->busy) {
26424a1cce3SDavid Greenman 					backward_okay = FALSE;
26524a1cce3SDavid Greenman 					continue;
26624a1cce3SDavid Greenman 				}
26724a1cce3SDavid Greenman 				vm_page_test_dirty(p);
26824a1cce3SDavid Greenman 				if ((p->dirty & p->valid) != 0 &&
26924a1cce3SDavid Greenman 				    ((p->flags & PG_INACTIVE) ||
27024a1cce3SDavid Greenman 				     (sync == VM_PAGEOUT_FORCE)) &&
27124a1cce3SDavid Greenman 				    (p->wire_count == 0) &&
27224a1cce3SDavid Greenman 				    (p->hold_count == 0)) {
27324a1cce3SDavid Greenman 					mc[VM_PAGEOUT_PAGE_COUNT - i] = p;
27424a1cce3SDavid Greenman 					pageout_count++;
27524a1cce3SDavid Greenman 					page_base--;
27624a1cce3SDavid Greenman 					if (pageout_count == vm_pageout_page_count)
27724a1cce3SDavid Greenman 						break;
27824a1cce3SDavid Greenman 				} else {
27924a1cce3SDavid Greenman 					backward_okay = FALSE;
28024a1cce3SDavid Greenman 				}
28124a1cce3SDavid Greenman 			} else {
28224a1cce3SDavid Greenman 				backward_okay = FALSE;
28324a1cce3SDavid Greenman 			}
284f6b04d2bSDavid Greenman 		}
285f6b04d2bSDavid Greenman 	}
286f6b04d2bSDavid Greenman 
2870d94caffSDavid Greenman 	/*
2880d94caffSDavid Greenman 	 * we allow reads during pageouts...
2890d94caffSDavid Greenman 	 */
29024a1cce3SDavid Greenman 	for (i = page_base; i < (page_base + pageout_count); i++) {
29124a1cce3SDavid Greenman 		mc[i]->flags |= PG_BUSY;
29224a1cce3SDavid Greenman 		vm_page_protect(mc[i], VM_PROT_READ);
29326f9a767SRodney W. Grimes 	}
29426f9a767SRodney W. Grimes 
295aef922f5SJohn Dyson 	return vm_pageout_flush(&mc[page_base], pageout_count, sync);
296aef922f5SJohn Dyson }
297aef922f5SJohn Dyson 
298aef922f5SJohn Dyson int
299aef922f5SJohn Dyson vm_pageout_flush(mc, count, sync)
300aef922f5SJohn Dyson 	vm_page_t *mc;
301aef922f5SJohn Dyson 	int count;
302aef922f5SJohn Dyson 	int sync;
303aef922f5SJohn Dyson {
304aef922f5SJohn Dyson 	register vm_object_t object;
305aef922f5SJohn Dyson 	int pageout_status[count];
306aef922f5SJohn Dyson 	int anyok = 0;
307aef922f5SJohn Dyson 	int i;
308aef922f5SJohn Dyson 
309aef922f5SJohn Dyson 	object = mc[0]->object;
310aef922f5SJohn Dyson 	object->paging_in_progress += count;
311aef922f5SJohn Dyson 
312aef922f5SJohn Dyson 	vm_pager_put_pages(object, mc, count,
31326f9a767SRodney W. Grimes 	    ((sync || (object == kernel_object)) ? TRUE : FALSE),
31426f9a767SRodney W. Grimes 	    pageout_status);
31526f9a767SRodney W. Grimes 
316aef922f5SJohn Dyson 
317aef922f5SJohn Dyson 	for (i = 0; i < count; i++) {
318aef922f5SJohn Dyson 		vm_page_t mt = mc[i];
31924a1cce3SDavid Greenman 
32026f9a767SRodney W. Grimes 		switch (pageout_status[i]) {
32126f9a767SRodney W. Grimes 		case VM_PAGER_OK:
32226f9a767SRodney W. Grimes 			++anyok;
32326f9a767SRodney W. Grimes 			break;
32426f9a767SRodney W. Grimes 		case VM_PAGER_PEND:
32526f9a767SRodney W. Grimes 			++anyok;
32626f9a767SRodney W. Grimes 			break;
32726f9a767SRodney W. Grimes 		case VM_PAGER_BAD:
32826f9a767SRodney W. Grimes 			/*
3290d94caffSDavid Greenman 			 * Page outside of range of object. Right now we
3300d94caffSDavid Greenman 			 * essentially lose the changes by pretending it
3310d94caffSDavid Greenman 			 * worked.
33226f9a767SRodney W. Grimes 			 */
33324a1cce3SDavid Greenman 			pmap_clear_modify(VM_PAGE_TO_PHYS(mt));
33424a1cce3SDavid Greenman 			mt->dirty = 0;
33526f9a767SRodney W. Grimes 			break;
33626f9a767SRodney W. Grimes 		case VM_PAGER_ERROR:
33726f9a767SRodney W. Grimes 		case VM_PAGER_FAIL:
33826f9a767SRodney W. Grimes 			/*
3390d94caffSDavid Greenman 			 * If page couldn't be paged out, then reactivate the
3400d94caffSDavid Greenman 			 * page so it doesn't clog the inactive list.  (We
3410d94caffSDavid Greenman 			 * will try paging out it again later).
34226f9a767SRodney W. Grimes 			 */
34324a1cce3SDavid Greenman 			if (mt->flags & PG_INACTIVE)
34424a1cce3SDavid Greenman 				vm_page_activate(mt);
34526f9a767SRodney W. Grimes 			break;
34626f9a767SRodney W. Grimes 		case VM_PAGER_AGAIN:
34726f9a767SRodney W. Grimes 			break;
34826f9a767SRodney W. Grimes 		}
34926f9a767SRodney W. Grimes 
35026f9a767SRodney W. Grimes 
35126f9a767SRodney W. Grimes 		/*
3520d94caffSDavid Greenman 		 * If the operation is still going, leave the page busy to
3530d94caffSDavid Greenman 		 * block all other accesses. Also, leave the paging in
3540d94caffSDavid Greenman 		 * progress indicator set so that we don't attempt an object
3550d94caffSDavid Greenman 		 * collapse.
35626f9a767SRodney W. Grimes 		 */
35726f9a767SRodney W. Grimes 		if (pageout_status[i] != VM_PAGER_PEND) {
358f919ebdeSDavid Greenman 			vm_object_pip_wakeup(object);
35924a1cce3SDavid Greenman 			if ((mt->flags & (PG_REFERENCED|PG_WANTED)) ||
36024a1cce3SDavid Greenman 			    pmap_is_referenced(VM_PAGE_TO_PHYS(mt))) {
36124a1cce3SDavid Greenman 				pmap_clear_reference(VM_PAGE_TO_PHYS(mt));
36224a1cce3SDavid Greenman 				mt->flags &= ~PG_REFERENCED;
36324a1cce3SDavid Greenman 				if (mt->flags & PG_INACTIVE)
36424a1cce3SDavid Greenman 					vm_page_activate(mt);
36526f9a767SRodney W. Grimes 			}
36624a1cce3SDavid Greenman 			PAGE_WAKEUP(mt);
36726f9a767SRodney W. Grimes 		}
36826f9a767SRodney W. Grimes 	}
36926f9a767SRodney W. Grimes 	return anyok;
37026f9a767SRodney W. Grimes }
37126f9a767SRodney W. Grimes 
37226f9a767SRodney W. Grimes /*
37326f9a767SRodney W. Grimes  *	vm_pageout_object_deactivate_pages
37426f9a767SRodney W. Grimes  *
37526f9a767SRodney W. Grimes  *	deactivate enough pages to satisfy the inactive target
37626f9a767SRodney W. Grimes  *	requirements or if vm_page_proc_limit is set, then
37726f9a767SRodney W. Grimes  *	deactivate all of the pages in the object and its
37824a1cce3SDavid Greenman  *	backing_objects.
37926f9a767SRodney W. Grimes  *
38026f9a767SRodney W. Grimes  *	The object and map must be locked.
38126f9a767SRodney W. Grimes  */
382cd41fc12SDavid Greenman static int
3830d94caffSDavid Greenman vm_pageout_object_deactivate_pages(map, object, count, map_remove_only)
38426f9a767SRodney W. Grimes 	vm_map_t map;
38526f9a767SRodney W. Grimes 	vm_object_t object;
38626f9a767SRodney W. Grimes 	int count;
3870d94caffSDavid Greenman 	int map_remove_only;
38826f9a767SRodney W. Grimes {
38926f9a767SRodney W. Grimes 	register vm_page_t p, next;
39026f9a767SRodney W. Grimes 	int rcount;
39126f9a767SRodney W. Grimes 	int dcount;
39226f9a767SRodney W. Grimes 
39326f9a767SRodney W. Grimes 	dcount = 0;
39426f9a767SRodney W. Grimes 	if (count == 0)
39526f9a767SRodney W. Grimes 		count = 1;
39626f9a767SRodney W. Grimes 
39724a1cce3SDavid Greenman 	if (object->type == OBJT_DEVICE)
3988f895206SDavid Greenman 		return 0;
3998f895206SDavid Greenman 
40024a1cce3SDavid Greenman 	if (object->backing_object) {
40124a1cce3SDavid Greenman 		if (object->backing_object->ref_count == 1)
40224a1cce3SDavid Greenman 			dcount += vm_pageout_object_deactivate_pages(map,
40324a1cce3SDavid Greenman 			    object->backing_object, count / 2 + 1, map_remove_only);
4040d94caffSDavid Greenman 		else
40524a1cce3SDavid Greenman 			vm_pageout_object_deactivate_pages(map,
40624a1cce3SDavid Greenman 			    object->backing_object, count, 1);
4072fe6e4d7SDavid Greenman 	}
40824a1cce3SDavid Greenman 	if (object->paging_in_progress)
40926f9a767SRodney W. Grimes 		return dcount;
41026f9a767SRodney W. Grimes 
41126f9a767SRodney W. Grimes 	/*
41226f9a767SRodney W. Grimes 	 * scan the objects entire memory queue
41326f9a767SRodney W. Grimes 	 */
41426f9a767SRodney W. Grimes 	rcount = object->resident_page_count;
41526f9a767SRodney W. Grimes 	p = object->memq.tqh_first;
41626f9a767SRodney W. Grimes 	while (p && (rcount-- > 0)) {
41726f9a767SRodney W. Grimes 		next = p->listq.tqe_next;
418a58d1fa1SDavid Greenman 		cnt.v_pdpages++;
4190d94caffSDavid Greenman 		if (p->wire_count != 0 ||
4200d94caffSDavid Greenman 		    p->hold_count != 0 ||
4210d94caffSDavid Greenman 		    p->busy != 0 ||
4220d94caffSDavid Greenman 		    !pmap_page_exists(vm_map_pmap(map), VM_PAGE_TO_PHYS(p))) {
4230d94caffSDavid Greenman 			p = next;
4240d94caffSDavid Greenman 			continue;
4250d94caffSDavid Greenman 		}
42626f9a767SRodney W. Grimes 		/*
4270d94caffSDavid Greenman 		 * if a page is active, not wired and is in the processes
4280d94caffSDavid Greenman 		 * pmap, then deactivate the page.
42926f9a767SRodney W. Grimes 		 */
4300d94caffSDavid Greenman 		if ((p->flags & (PG_ACTIVE | PG_BUSY)) == PG_ACTIVE) {
431a647a309SDavid Greenman 			if (!pmap_is_referenced(VM_PAGE_TO_PHYS(p)) &&
4321ed81ef2SDavid Greenman 			    (p->flags & (PG_REFERENCED|PG_WANTED)) == 0) {
43326f9a767SRodney W. Grimes 				p->act_count -= min(p->act_count, ACT_DECLINE);
43426f9a767SRodney W. Grimes 				/*
4350d94caffSDavid Greenman 				 * if the page act_count is zero -- then we
4360d94caffSDavid Greenman 				 * deactivate
43726f9a767SRodney W. Grimes 				 */
43826f9a767SRodney W. Grimes 				if (!p->act_count) {
4390d94caffSDavid Greenman 					if (!map_remove_only)
44026f9a767SRodney W. Grimes 						vm_page_deactivate(p);
441f919ebdeSDavid Greenman 					vm_page_protect(p, VM_PROT_NONE);
44226f9a767SRodney W. Grimes 					/*
4430d94caffSDavid Greenman 					 * else if on the next go-around we
4440d94caffSDavid Greenman 					 * will deactivate the page we need to
4450d94caffSDavid Greenman 					 * place the page on the end of the
4460d94caffSDavid Greenman 					 * queue to age the other pages in
4470d94caffSDavid Greenman 					 * memory.
44826f9a767SRodney W. Grimes 					 */
44926f9a767SRodney W. Grimes 				} else {
45026f9a767SRodney W. Grimes 					TAILQ_REMOVE(&vm_page_queue_active, p, pageq);
45126f9a767SRodney W. Grimes 					TAILQ_INSERT_TAIL(&vm_page_queue_active, p, pageq);
45226f9a767SRodney W. Grimes 				}
45326f9a767SRodney W. Grimes 				/*
45426f9a767SRodney W. Grimes 				 * see if we are done yet
45526f9a767SRodney W. Grimes 				 */
45626f9a767SRodney W. Grimes 				if (p->flags & PG_INACTIVE) {
45726f9a767SRodney W. Grimes 					--count;
45826f9a767SRodney W. Grimes 					++dcount;
45926f9a767SRodney W. Grimes 					if (count <= 0 &&
46026f9a767SRodney W. Grimes 					    cnt.v_inactive_count > cnt.v_inactive_target) {
46126f9a767SRodney W. Grimes 						return dcount;
46226f9a767SRodney W. Grimes 					}
46326f9a767SRodney W. Grimes 				}
46426f9a767SRodney W. Grimes 			} else {
46526f9a767SRodney W. Grimes 				/*
46626f9a767SRodney W. Grimes 				 * Move the page to the bottom of the queue.
46726f9a767SRodney W. Grimes 				 */
46826f9a767SRodney W. Grimes 				pmap_clear_reference(VM_PAGE_TO_PHYS(p));
469a647a309SDavid Greenman 				p->flags &= ~PG_REFERENCED;
47026f9a767SRodney W. Grimes 				if (p->act_count < ACT_MAX)
47126f9a767SRodney W. Grimes 					p->act_count += ACT_ADVANCE;
47226f9a767SRodney W. Grimes 
47326f9a767SRodney W. Grimes 				TAILQ_REMOVE(&vm_page_queue_active, p, pageq);
47426f9a767SRodney W. Grimes 				TAILQ_INSERT_TAIL(&vm_page_queue_active, p, pageq);
47526f9a767SRodney W. Grimes 			}
4760d94caffSDavid Greenman 		} else if ((p->flags & (PG_INACTIVE | PG_BUSY)) == PG_INACTIVE) {
477f919ebdeSDavid Greenman 			vm_page_protect(p, VM_PROT_NONE);
47826f9a767SRodney W. Grimes 		}
47926f9a767SRodney W. Grimes 		p = next;
48026f9a767SRodney W. Grimes 	}
48126f9a767SRodney W. Grimes 	return dcount;
48226f9a767SRodney W. Grimes }
48326f9a767SRodney W. Grimes 
48426f9a767SRodney W. Grimes 
48526f9a767SRodney W. Grimes /*
48626f9a767SRodney W. Grimes  * deactivate some number of pages in a map, try to do it fairly, but
48726f9a767SRodney W. Grimes  * that is really hard to do.
48826f9a767SRodney W. Grimes  */
48926f9a767SRodney W. Grimes 
490cd41fc12SDavid Greenman static void
49126f9a767SRodney W. Grimes vm_pageout_map_deactivate_pages(map, entry, count, freeer)
49226f9a767SRodney W. Grimes 	vm_map_t map;
49326f9a767SRodney W. Grimes 	vm_map_entry_t entry;
49426f9a767SRodney W. Grimes 	int *count;
495cd41fc12SDavid Greenman 	freeer_fcn_t *freeer;
49626f9a767SRodney W. Grimes {
49726f9a767SRodney W. Grimes 	vm_map_t tmpm;
49826f9a767SRodney W. Grimes 	vm_map_entry_t tmpe;
49926f9a767SRodney W. Grimes 	vm_object_t obj;
5000d94caffSDavid Greenman 
50126f9a767SRodney W. Grimes 	if (*count <= 0)
50226f9a767SRodney W. Grimes 		return;
50326f9a767SRodney W. Grimes 	vm_map_reference(map);
50426f9a767SRodney W. Grimes 	if (!lock_try_read(&map->lock)) {
50526f9a767SRodney W. Grimes 		vm_map_deallocate(map);
50626f9a767SRodney W. Grimes 		return;
50726f9a767SRodney W. Grimes 	}
50826f9a767SRodney W. Grimes 	if (entry == 0) {
50926f9a767SRodney W. Grimes 		tmpe = map->header.next;
51026f9a767SRodney W. Grimes 		while (tmpe != &map->header && *count > 0) {
511cd41fc12SDavid Greenman 			vm_pageout_map_deactivate_pages(map, tmpe, count, freeer);
51226f9a767SRodney W. Grimes 			tmpe = tmpe->next;
51326f9a767SRodney W. Grimes 		};
51426f9a767SRodney W. Grimes 	} else if (entry->is_sub_map || entry->is_a_map) {
51526f9a767SRodney W. Grimes 		tmpm = entry->object.share_map;
51626f9a767SRodney W. Grimes 		tmpe = tmpm->header.next;
51726f9a767SRodney W. Grimes 		while (tmpe != &tmpm->header && *count > 0) {
518cd41fc12SDavid Greenman 			vm_pageout_map_deactivate_pages(tmpm, tmpe, count, freeer);
51926f9a767SRodney W. Grimes 			tmpe = tmpe->next;
52026f9a767SRodney W. Grimes 		};
5214e39a515SPoul-Henning Kamp 	} else if ((obj = entry->object.vm_object) != 0) {
522cd41fc12SDavid Greenman 		*count -= (*freeer) (map, obj, *count, TRUE);
52326f9a767SRodney W. Grimes 	}
52426f9a767SRodney W. Grimes 	lock_read_done(&map->lock);
52526f9a767SRodney W. Grimes 	vm_map_deallocate(map);
52626f9a767SRodney W. Grimes 	return;
52726f9a767SRodney W. Grimes }
528df8bae1dSRodney W. Grimes 
529cd41fc12SDavid Greenman static void
5300d94caffSDavid Greenman vm_req_vmdaemon()
5310d94caffSDavid Greenman {
5320d94caffSDavid Greenman 	static int lastrun = 0;
5330d94caffSDavid Greenman 
5342fe6e4d7SDavid Greenman 	if ((ticks > (lastrun + hz / 10)) || (ticks < lastrun)) {
53524a1cce3SDavid Greenman 		wakeup(&vm_daemon_needed);
5362fe6e4d7SDavid Greenman 		lastrun = ticks;
5372fe6e4d7SDavid Greenman 	}
5382fe6e4d7SDavid Greenman }
5392fe6e4d7SDavid Greenman 
540df8bae1dSRodney W. Grimes /*
541df8bae1dSRodney W. Grimes  *	vm_pageout_scan does the dirty work for the pageout daemon.
542df8bae1dSRodney W. Grimes  */
54326f9a767SRodney W. Grimes int
544df8bae1dSRodney W. Grimes vm_pageout_scan()
545df8bae1dSRodney W. Grimes {
54626f9a767SRodney W. Grimes 	vm_page_t m;
547f6b04d2bSDavid Greenman 	int page_shortage, maxscan, maxlaunder, pcount;
5484e39a515SPoul-Henning Kamp 	int pages_freed;
54926f9a767SRodney W. Grimes 	vm_page_t next;
5505663e6deSDavid Greenman 	struct proc *p, *bigproc;
5515663e6deSDavid Greenman 	vm_offset_t size, bigsize;
552df8bae1dSRodney W. Grimes 	vm_object_t object;
55326f9a767SRodney W. Grimes 	int force_wakeup = 0;
554f6b04d2bSDavid Greenman 	int vnodes_skipped = 0;
5550d94caffSDavid Greenman 
55626f9a767SRodney W. Grimes 	pages_freed = 0;
557df8bae1dSRodney W. Grimes 
558df8bae1dSRodney W. Grimes 	/*
5590d94caffSDavid Greenman 	 * Start scanning the inactive queue for pages we can free. We keep
5600d94caffSDavid Greenman 	 * scanning until we have enough free pages or we have scanned through
5610d94caffSDavid Greenman 	 * the entire queue.  If we encounter dirty pages, we start cleaning
5620d94caffSDavid Greenman 	 * them.
563df8bae1dSRodney W. Grimes 	 */
564df8bae1dSRodney W. Grimes 
5650d94caffSDavid Greenman 	maxlaunder = (cnt.v_inactive_target > MAXLAUNDER) ?
5660d94caffSDavid Greenman 	    MAXLAUNDER : cnt.v_inactive_target;
5670d94caffSDavid Greenman 
56826f9a767SRodney W. Grimes rescan1:
569f6b04d2bSDavid Greenman 	maxscan = cnt.v_inactive_count;
57026f9a767SRodney W. Grimes 	m = vm_page_queue_inactive.tqh_first;
571f6b04d2bSDavid Greenman 	while ((m != NULL) && (maxscan-- > 0) &&
572f6b04d2bSDavid Greenman 	    ((cnt.v_cache_count + cnt.v_free_count) < (cnt.v_cache_min + cnt.v_free_target))) {
57326f9a767SRodney W. Grimes 		vm_page_t next;
574df8bae1dSRodney W. Grimes 
575a58d1fa1SDavid Greenman 		cnt.v_pdpages++;
576df8bae1dSRodney W. Grimes 		next = m->pageq.tqe_next;
577df8bae1dSRodney W. Grimes 
5780d94caffSDavid Greenman #if defined(VM_DIAGNOSE)
57926f9a767SRodney W. Grimes 		if ((m->flags & PG_INACTIVE) == 0) {
580832f3afdSAndreas Schulz 			printf("vm_pageout_scan: page not inactive?\n");
5810d94caffSDavid Greenman 			break;
582df8bae1dSRodney W. Grimes 		}
5830d94caffSDavid Greenman #endif
58426f9a767SRodney W. Grimes 
58526f9a767SRodney W. Grimes 		/*
58626f9a767SRodney W. Grimes 		 * dont mess with busy pages
58726f9a767SRodney W. Grimes 		 */
588f6b04d2bSDavid Greenman 		if (m->hold_count || m->busy || (m->flags & PG_BUSY)) {
5890d94caffSDavid Greenman 			TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq);
5900d94caffSDavid Greenman 			TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq);
59126f9a767SRodney W. Grimes 			m = next;
59226f9a767SRodney W. Grimes 			continue;
59326f9a767SRodney W. Grimes 		}
5940d94caffSDavid Greenman 		if (((m->flags & PG_REFERENCED) == 0) &&
5950d94caffSDavid Greenman 		    pmap_is_referenced(VM_PAGE_TO_PHYS(m))) {
5962fe6e4d7SDavid Greenman 			m->flags |= PG_REFERENCED;
5970d94caffSDavid Greenman 		}
5980d94caffSDavid Greenman 		if (m->object->ref_count == 0) {
5990d94caffSDavid Greenman 			m->flags &= ~PG_REFERENCED;
6002fe6e4d7SDavid Greenman 			pmap_clear_reference(VM_PAGE_TO_PHYS(m));
6012fe6e4d7SDavid Greenman 		}
6021ed81ef2SDavid Greenman 		if ((m->flags & (PG_REFERENCED|PG_WANTED)) != 0) {
603a647a309SDavid Greenman 			m->flags &= ~PG_REFERENCED;
6040d94caffSDavid Greenman 			pmap_clear_reference(VM_PAGE_TO_PHYS(m));
60526f9a767SRodney W. Grimes 			vm_page_activate(m);
6066d40c3d3SDavid Greenman 			if (m->act_count < ACT_MAX)
6076d40c3d3SDavid Greenman 				m->act_count += ACT_ADVANCE;
6080d94caffSDavid Greenman 			m = next;
6090d94caffSDavid Greenman 			continue;
6100d94caffSDavid Greenman 		}
6110d94caffSDavid Greenman 
612f6b04d2bSDavid Greenman 		vm_page_test_dirty(m);
613f6b04d2bSDavid Greenman 		if (m->dirty == 0) {
614f6b04d2bSDavid Greenman 			if (m->bmapped == 0) {
6156d40c3d3SDavid Greenman 				if (m->valid == 0) {
6166d40c3d3SDavid Greenman 					pmap_page_protect(VM_PAGE_TO_PHYS(m), VM_PROT_NONE);
6176d40c3d3SDavid Greenman 					vm_page_free(m);
6180bb3a0d2SDavid Greenman 					cnt.v_dfree++;
6196f2b142eSDavid Greenman 				} else {
6200d94caffSDavid Greenman 					vm_page_cache(m);
6216d40c3d3SDavid Greenman 				}
622f6b04d2bSDavid Greenman 				++pages_freed;
623f6b04d2bSDavid Greenman 			} else {
624f6b04d2bSDavid Greenman 				m = next;
625f6b04d2bSDavid Greenman 				continue;
626f6b04d2bSDavid Greenman 			}
6270d94caffSDavid Greenman 		} else if (maxlaunder > 0) {
6280d94caffSDavid Greenman 			int written;
629f6b04d2bSDavid Greenman 			struct vnode *vp = NULL;
6300d94caffSDavid Greenman 
6310d94caffSDavid Greenman 			object = m->object;
63224a1cce3SDavid Greenman 			if (object->flags & OBJ_DEAD) {
6330d94caffSDavid Greenman 				m = next;
6340d94caffSDavid Greenman 				continue;
6350d94caffSDavid Greenman 			}
636f6b04d2bSDavid Greenman 
63724a1cce3SDavid Greenman 			if (object->type == OBJT_VNODE) {
63824a1cce3SDavid Greenman 				vp = object->handle;
639f6b04d2bSDavid Greenman 				if (VOP_ISLOCKED(vp) || vget(vp, 1)) {
640aef922f5SJohn Dyson 					if (object->flags & OBJ_MIGHTBEDIRTY)
641f6b04d2bSDavid Greenman 						++vnodes_skipped;
642f6b04d2bSDavid Greenman 					m = next;
643f6b04d2bSDavid Greenman 					continue;
644f6b04d2bSDavid Greenman 				}
645f6b04d2bSDavid Greenman 			}
646f6b04d2bSDavid Greenman 
6470d94caffSDavid Greenman 			/*
6480d94caffSDavid Greenman 			 * If a page is dirty, then it is either being washed
6490d94caffSDavid Greenman 			 * (but not yet cleaned) or it is still in the
6500d94caffSDavid Greenman 			 * laundry.  If it is still in the laundry, then we
6510d94caffSDavid Greenman 			 * start the cleaning operation.
6520d94caffSDavid Greenman 			 */
6530d94caffSDavid Greenman 			written = vm_pageout_clean(m, 0);
654f6b04d2bSDavid Greenman 
655f6b04d2bSDavid Greenman 			if (vp)
656f6b04d2bSDavid Greenman 				vput(vp);
657f6b04d2bSDavid Greenman 
6580d94caffSDavid Greenman 			if (!next) {
6590d94caffSDavid Greenman 				break;
6600d94caffSDavid Greenman 			}
6610d94caffSDavid Greenman 			maxlaunder -= written;
6620d94caffSDavid Greenman 			/*
6630d94caffSDavid Greenman 			 * if the next page has been re-activated, start
6640d94caffSDavid Greenman 			 * scanning again
6650d94caffSDavid Greenman 			 */
6660d94caffSDavid Greenman 			if ((next->flags & PG_INACTIVE) == 0) {
6670d94caffSDavid Greenman 				goto rescan1;
6680d94caffSDavid Greenman 			}
669df8bae1dSRodney W. Grimes 		}
67026f9a767SRodney W. Grimes 		m = next;
67126f9a767SRodney W. Grimes 	}
67226f9a767SRodney W. Grimes 
673df8bae1dSRodney W. Grimes 	/*
6740d94caffSDavid Greenman 	 * Compute the page shortage.  If we are still very low on memory be
6750d94caffSDavid Greenman 	 * sure that we will move a minimal amount of pages from active to
6760d94caffSDavid Greenman 	 * inactive.
677df8bae1dSRodney W. Grimes 	 */
678df8bae1dSRodney W. Grimes 
67926f9a767SRodney W. Grimes 	page_shortage = cnt.v_inactive_target -
6800d94caffSDavid Greenman 	    (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count);
68126f9a767SRodney W. Grimes 	if (page_shortage <= 0) {
68226f9a767SRodney W. Grimes 		if (pages_freed == 0) {
68317c4c408SDavid Greenman 			page_shortage = cnt.v_free_min - cnt.v_free_count;
684f6b04d2bSDavid Greenman 		} else {
685f6b04d2bSDavid Greenman 			page_shortage = 1;
68626f9a767SRodney W. Grimes 		}
687df8bae1dSRodney W. Grimes 	}
688f6b04d2bSDavid Greenman 	maxscan = MAXSCAN;
689f6b04d2bSDavid Greenman 	pcount = cnt.v_active_count;
69026f9a767SRodney W. Grimes 	m = vm_page_queue_active.tqh_first;
691f6b04d2bSDavid Greenman 	while ((m != NULL) && (maxscan > 0) && (pcount-- > 0) && (page_shortage > 0)) {
69226f9a767SRodney W. Grimes 
693a58d1fa1SDavid Greenman 		cnt.v_pdpages++;
69426f9a767SRodney W. Grimes 		next = m->pageq.tqe_next;
695df8bae1dSRodney W. Grimes 
696df8bae1dSRodney W. Grimes 		/*
69726f9a767SRodney W. Grimes 		 * Don't deactivate pages that are busy.
698df8bae1dSRodney W. Grimes 		 */
699a647a309SDavid Greenman 		if ((m->busy != 0) ||
7000d94caffSDavid Greenman 		    (m->flags & PG_BUSY) ||
701f6b04d2bSDavid Greenman 		    (m->hold_count != 0)) {
7026d40c3d3SDavid Greenman 			TAILQ_REMOVE(&vm_page_queue_active, m, pageq);
7036d40c3d3SDavid Greenman 			TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq);
70426f9a767SRodney W. Grimes 			m = next;
70526f9a767SRodney W. Grimes 			continue;
706df8bae1dSRodney W. Grimes 		}
7071ed81ef2SDavid Greenman 		if (m->object->ref_count && ((m->flags & (PG_REFERENCED|PG_WANTED)) ||
7080d94caffSDavid Greenman 			pmap_is_referenced(VM_PAGE_TO_PHYS(m)))) {
7090d94caffSDavid Greenman 			int s;
710df8bae1dSRodney W. Grimes 
711df8bae1dSRodney W. Grimes 			pmap_clear_reference(VM_PAGE_TO_PHYS(m));
712a647a309SDavid Greenman 			m->flags &= ~PG_REFERENCED;
7130d94caffSDavid Greenman 			if (m->act_count < ACT_MAX) {
71426f9a767SRodney W. Grimes 				m->act_count += ACT_ADVANCE;
7150d94caffSDavid Greenman 			}
71626f9a767SRodney W. Grimes 			TAILQ_REMOVE(&vm_page_queue_active, m, pageq);
71726f9a767SRodney W. Grimes 			TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq);
71826f9a767SRodney W. Grimes 		} else {
7196d40c3d3SDavid Greenman 			m->flags &= ~PG_REFERENCED;
7206d40c3d3SDavid Greenman 			pmap_clear_reference(VM_PAGE_TO_PHYS(m));
72126f9a767SRodney W. Grimes 			m->act_count -= min(m->act_count, ACT_DECLINE);
722df8bae1dSRodney W. Grimes 
723df8bae1dSRodney W. Grimes 			/*
72426f9a767SRodney W. Grimes 			 * if the page act_count is zero -- then we deactivate
725df8bae1dSRodney W. Grimes 			 */
7260d94caffSDavid Greenman 			if (!m->act_count && (page_shortage > 0)) {
7270d94caffSDavid Greenman 				if (m->object->ref_count == 0) {
7280d94caffSDavid Greenman 					--page_shortage;
729f6b04d2bSDavid Greenman 					vm_page_test_dirty(m);
730f6b04d2bSDavid Greenman 					if ((m->bmapped == 0) && (m->dirty == 0) ) {
7310d94caffSDavid Greenman 						m->act_count = 0;
7320d94caffSDavid Greenman 						vm_page_cache(m);
7330d94caffSDavid Greenman 					} else {
7340d94caffSDavid Greenman 						vm_page_deactivate(m);
7350d94caffSDavid Greenman 					}
7360d94caffSDavid Greenman 				} else {
73726f9a767SRodney W. Grimes 					vm_page_deactivate(m);
73826f9a767SRodney W. Grimes 					--page_shortage;
7390d94caffSDavid Greenman 				}
7406d40c3d3SDavid Greenman 			} else if (m->act_count) {
74126f9a767SRodney W. Grimes 				TAILQ_REMOVE(&vm_page_queue_active, m, pageq);
74226f9a767SRodney W. Grimes 				TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq);
743df8bae1dSRodney W. Grimes 			}
744df8bae1dSRodney W. Grimes 		}
745f6b04d2bSDavid Greenman 		maxscan--;
74626f9a767SRodney W. Grimes 		m = next;
74726f9a767SRodney W. Grimes 	}
748df8bae1dSRodney W. Grimes 
749df8bae1dSRodney W. Grimes 	/*
7500d94caffSDavid Greenman 	 * We try to maintain some *really* free pages, this allows interrupt
7510d94caffSDavid Greenman 	 * code to be guaranteed space.
752df8bae1dSRodney W. Grimes 	 */
753a1f6d91cSDavid Greenman 	while (cnt.v_free_count < cnt.v_free_reserved) {
7540d94caffSDavid Greenman 		m = vm_page_queue_cache.tqh_first;
7550d94caffSDavid Greenman 		if (!m)
7560d94caffSDavid Greenman 			break;
7570d94caffSDavid Greenman 		vm_page_free(m);
7580bb3a0d2SDavid Greenman 		cnt.v_dfree++;
75926f9a767SRodney W. Grimes 	}
7605663e6deSDavid Greenman 
7615663e6deSDavid Greenman 	/*
762f6b04d2bSDavid Greenman 	 * If we didn't get enough free pages, and we have skipped a vnode
7634c1f8ee9SDavid Greenman 	 * in a writeable object, wakeup the sync daemon.  And kick swapout
7644c1f8ee9SDavid Greenman 	 * if we did not get enough free pages.
765f6b04d2bSDavid Greenman 	 */
7664c1f8ee9SDavid Greenman 	if ((cnt.v_cache_count + cnt.v_free_count) < cnt.v_free_target) {
767f6b04d2bSDavid Greenman 		if (vnodes_skipped &&
768f6b04d2bSDavid Greenman 		    (cnt.v_cache_count + cnt.v_free_count) < cnt.v_free_min) {
769f6b04d2bSDavid Greenman 			if (!vfs_update_wakeup) {
770f6b04d2bSDavid Greenman 				vfs_update_wakeup = 1;
77124a1cce3SDavid Greenman 				wakeup(&vfs_update_wakeup);
772f6b04d2bSDavid Greenman 			}
773f6b04d2bSDavid Greenman 		}
7744c1f8ee9SDavid Greenman 		/*
7754c1f8ee9SDavid Greenman 		 * now swap processes out if we are in low memory conditions
7764c1f8ee9SDavid Greenman 		 */
7774c1f8ee9SDavid Greenman 		if (!swap_pager_full && vm_swap_size &&
7784c1f8ee9SDavid Greenman 			vm_pageout_req_swapout == 0) {
7794c1f8ee9SDavid Greenman 			vm_pageout_req_swapout = 1;
7804c1f8ee9SDavid Greenman 			vm_req_vmdaemon();
7814c1f8ee9SDavid Greenman 		}
7824c1f8ee9SDavid Greenman 	}
7834c1f8ee9SDavid Greenman 
7844c1f8ee9SDavid Greenman 	if ((cnt.v_inactive_count + cnt.v_free_count + cnt.v_cache_count) <
7854c1f8ee9SDavid Greenman 	    (cnt.v_inactive_target + cnt.v_free_min)) {
7864c1f8ee9SDavid Greenman 		vm_req_vmdaemon();
7874c1f8ee9SDavid Greenman 	}
788f6b04d2bSDavid Greenman 
789f6b04d2bSDavid Greenman 	/*
7900d94caffSDavid Greenman 	 * make sure that we have swap space -- if we are low on memory and
7910d94caffSDavid Greenman 	 * swap -- then kill the biggest process.
7925663e6deSDavid Greenman 	 */
7935663e6deSDavid Greenman 	if ((vm_swap_size == 0 || swap_pager_full) &&
7940d94caffSDavid Greenman 	    ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_free_min)) {
7955663e6deSDavid Greenman 		bigproc = NULL;
7965663e6deSDavid Greenman 		bigsize = 0;
7975663e6deSDavid Greenman 		for (p = (struct proc *) allproc; p != NULL; p = p->p_next) {
7985663e6deSDavid Greenman 			/*
7995663e6deSDavid Greenman 			 * if this is a system process, skip it
8005663e6deSDavid Greenman 			 */
80179221631SDavid Greenman 			if ((p->p_flag & P_SYSTEM) || (p->p_pid == 1) ||
80279221631SDavid Greenman 			    ((p->p_pid < 48) && (vm_swap_size != 0))) {
8035663e6deSDavid Greenman 				continue;
8045663e6deSDavid Greenman 			}
8055663e6deSDavid Greenman 			/*
8065663e6deSDavid Greenman 			 * if the process is in a non-running type state,
8075663e6deSDavid Greenman 			 * don't touch it.
8085663e6deSDavid Greenman 			 */
8095663e6deSDavid Greenman 			if (p->p_stat != SRUN && p->p_stat != SSLEEP) {
8105663e6deSDavid Greenman 				continue;
8115663e6deSDavid Greenman 			}
8125663e6deSDavid Greenman 			/*
8135663e6deSDavid Greenman 			 * get the process size
8145663e6deSDavid Greenman 			 */
8155663e6deSDavid Greenman 			size = p->p_vmspace->vm_pmap.pm_stats.resident_count;
8165663e6deSDavid Greenman 			/*
8175663e6deSDavid Greenman 			 * if the this process is bigger than the biggest one
8185663e6deSDavid Greenman 			 * remember it.
8195663e6deSDavid Greenman 			 */
8205663e6deSDavid Greenman 			if (size > bigsize) {
8215663e6deSDavid Greenman 				bigproc = p;
8225663e6deSDavid Greenman 				bigsize = size;
8235663e6deSDavid Greenman 			}
8245663e6deSDavid Greenman 		}
8255663e6deSDavid Greenman 		if (bigproc != NULL) {
8265663e6deSDavid Greenman 			printf("Process %lu killed by vm_pageout -- out of swap\n", (u_long) bigproc->p_pid);
8275663e6deSDavid Greenman 			psignal(bigproc, SIGKILL);
8285663e6deSDavid Greenman 			bigproc->p_estcpu = 0;
8295663e6deSDavid Greenman 			bigproc->p_nice = PRIO_MIN;
8305663e6deSDavid Greenman 			resetpriority(bigproc);
83124a1cce3SDavid Greenman 			wakeup(&cnt.v_free_count);
8325663e6deSDavid Greenman 		}
8335663e6deSDavid Greenman 	}
83426f9a767SRodney W. Grimes 	return force_wakeup;
83526f9a767SRodney W. Grimes }
83626f9a767SRodney W. Grimes 
837df8bae1dSRodney W. Grimes /*
838df8bae1dSRodney W. Grimes  *	vm_pageout is the high level pageout daemon.
839df8bae1dSRodney W. Grimes  */
8402b14f991SJulian Elischer static void
84126f9a767SRodney W. Grimes vm_pageout()
842df8bae1dSRodney W. Grimes {
843df8bae1dSRodney W. Grimes 	(void) spl0();
844df8bae1dSRodney W. Grimes 
845df8bae1dSRodney W. Grimes 	/*
846df8bae1dSRodney W. Grimes 	 * Initialize some paging parameters.
847df8bae1dSRodney W. Grimes 	 */
848df8bae1dSRodney W. Grimes 
849f6b04d2bSDavid Greenman 	cnt.v_interrupt_free_min = 2;
850f6b04d2bSDavid Greenman 
8510d94caffSDavid Greenman 	if (cnt.v_page_count > 1024)
8520d94caffSDavid Greenman 		cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200;
8530d94caffSDavid Greenman 	else
8540d94caffSDavid Greenman 		cnt.v_free_min = 4;
855ed74321bSDavid Greenman 	/*
8560d94caffSDavid Greenman 	 * free_reserved needs to include enough for the largest swap pager
8570d94caffSDavid Greenman 	 * structures plus enough for any pv_entry structs when paging.
858ed74321bSDavid Greenman 	 */
859f6b04d2bSDavid Greenman 	cnt.v_pageout_free_min = 6 + cnt.v_page_count / 1024 +
860f6b04d2bSDavid Greenman 				cnt.v_interrupt_free_min;
86161f5d510SDavid Greenman 	cnt.v_free_reserved = cnt.v_pageout_free_min + 6;
8620d94caffSDavid Greenman 	cnt.v_free_target = 3 * cnt.v_free_min + cnt.v_free_reserved;
86326f9a767SRodney W. Grimes 	cnt.v_free_min += cnt.v_free_reserved;
8646f2b142eSDavid Greenman 
8650d94caffSDavid Greenman 	if (cnt.v_page_count > 1024) {
8660d94caffSDavid Greenman 		cnt.v_cache_max = (cnt.v_free_count - 1024) / 2;
8676f2b142eSDavid Greenman 		cnt.v_cache_min = (cnt.v_free_count - 1024) / 8;
8686f2b142eSDavid Greenman 		cnt.v_inactive_target = 2*cnt.v_cache_min + 192;
8690d94caffSDavid Greenman 	} else {
8700d94caffSDavid Greenman 		cnt.v_cache_min = 0;
8710d94caffSDavid Greenman 		cnt.v_cache_max = 0;
8726f2b142eSDavid Greenman 		cnt.v_inactive_target = cnt.v_free_count / 4;
8730d94caffSDavid Greenman 	}
874df8bae1dSRodney W. Grimes 
875df8bae1dSRodney W. Grimes 	/* XXX does not really belong here */
876df8bae1dSRodney W. Grimes 	if (vm_page_max_wired == 0)
877df8bae1dSRodney W. Grimes 		vm_page_max_wired = cnt.v_free_count / 3;
878df8bae1dSRodney W. Grimes 
87926f9a767SRodney W. Grimes 
88024a1cce3SDavid Greenman 	swap_pager_swap_init();
881df8bae1dSRodney W. Grimes 	/*
8820d94caffSDavid Greenman 	 * The pageout daemon is never done, so loop forever.
883df8bae1dSRodney W. Grimes 	 */
884df8bae1dSRodney W. Grimes 	while (TRUE) {
885f919ebdeSDavid Greenman 		int s = splhigh();
886f919ebdeSDavid Greenman 
887f919ebdeSDavid Greenman 		if (!vm_pages_needed ||
888f919ebdeSDavid Greenman 			((cnt.v_free_count >= cnt.v_free_reserved) &&
889f919ebdeSDavid Greenman 			 (cnt.v_free_count + cnt.v_cache_count >= cnt.v_free_min))) {
890f919ebdeSDavid Greenman 			vm_pages_needed = 0;
89124a1cce3SDavid Greenman 			tsleep(&vm_pages_needed, PVM, "psleep", 0);
892f919ebdeSDavid Greenman 		}
893f919ebdeSDavid Greenman 		vm_pages_needed = 0;
894f919ebdeSDavid Greenman 		splx(s);
895a58d1fa1SDavid Greenman 		cnt.v_pdwakeups++;
896df8bae1dSRodney W. Grimes 		vm_pager_sync();
8970d94caffSDavid Greenman 		vm_pageout_scan();
89826f9a767SRodney W. Grimes 		vm_pager_sync();
89924a1cce3SDavid Greenman 		wakeup(&cnt.v_free_count);
90024a1cce3SDavid Greenman 		wakeup(kmem_map);
901df8bae1dSRodney W. Grimes 	}
902df8bae1dSRodney W. Grimes }
90326f9a767SRodney W. Grimes 
9042b14f991SJulian Elischer static void
9054f9fb771SBruce Evans vm_daemon()
9060d94caffSDavid Greenman {
9072fe6e4d7SDavid Greenman 	vm_object_t object;
9082fe6e4d7SDavid Greenman 	struct proc *p;
9090d94caffSDavid Greenman 
9102fe6e4d7SDavid Greenman 	while (TRUE) {
91124a1cce3SDavid Greenman 		tsleep(&vm_daemon_needed, PUSER, "psleep", 0);
9124c1f8ee9SDavid Greenman 		if (vm_pageout_req_swapout) {
9136306c897SDavid Greenman 			swapout_procs();
9144c1f8ee9SDavid Greenman 			vm_pageout_req_swapout = 0;
9154c1f8ee9SDavid Greenman 		}
9162fe6e4d7SDavid Greenman 		/*
9170d94caffSDavid Greenman 		 * scan the processes for exceeding their rlimits or if
9180d94caffSDavid Greenman 		 * process is swapped out -- deactivate pages
9192fe6e4d7SDavid Greenman 		 */
9202fe6e4d7SDavid Greenman 
9212fe6e4d7SDavid Greenman 		for (p = (struct proc *) allproc; p != NULL; p = p->p_next) {
9222fe6e4d7SDavid Greenman 			int overage;
9232fe6e4d7SDavid Greenman 			quad_t limit;
9242fe6e4d7SDavid Greenman 			vm_offset_t size;
9252fe6e4d7SDavid Greenman 
9262fe6e4d7SDavid Greenman 			/*
9272fe6e4d7SDavid Greenman 			 * if this is a system process or if we have already
9282fe6e4d7SDavid Greenman 			 * looked at this process, skip it.
9292fe6e4d7SDavid Greenman 			 */
9302fe6e4d7SDavid Greenman 			if (p->p_flag & (P_SYSTEM | P_WEXIT)) {
9312fe6e4d7SDavid Greenman 				continue;
9322fe6e4d7SDavid Greenman 			}
9332fe6e4d7SDavid Greenman 			/*
9342fe6e4d7SDavid Greenman 			 * if the process is in a non-running type state,
9352fe6e4d7SDavid Greenman 			 * don't touch it.
9362fe6e4d7SDavid Greenman 			 */
9372fe6e4d7SDavid Greenman 			if (p->p_stat != SRUN && p->p_stat != SSLEEP) {
9382fe6e4d7SDavid Greenman 				continue;
9392fe6e4d7SDavid Greenman 			}
9402fe6e4d7SDavid Greenman 			/*
9412fe6e4d7SDavid Greenman 			 * get a limit
9422fe6e4d7SDavid Greenman 			 */
9432fe6e4d7SDavid Greenman 			limit = qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur,
9442fe6e4d7SDavid Greenman 			    p->p_rlimit[RLIMIT_RSS].rlim_max);
9452fe6e4d7SDavid Greenman 
9462fe6e4d7SDavid Greenman 			/*
9470d94caffSDavid Greenman 			 * let processes that are swapped out really be
9480d94caffSDavid Greenman 			 * swapped out set the limit to nothing (will force a
9490d94caffSDavid Greenman 			 * swap-out.)
9502fe6e4d7SDavid Greenman 			 */
9512fe6e4d7SDavid Greenman 			if ((p->p_flag & P_INMEM) == 0)
9520d94caffSDavid Greenman 				limit = 0;	/* XXX */
9532fe6e4d7SDavid Greenman 
954a91c5a7eSJohn Dyson 			size = p->p_vmspace->vm_pmap.pm_stats.resident_count * PAGE_SIZE;
9552fe6e4d7SDavid Greenman 			if (limit >= 0 && size >= limit) {
956a5eb0e27SPoul-Henning Kamp 				overage = (size - limit) >> PAGE_SHIFT;
9572fe6e4d7SDavid Greenman 				vm_pageout_map_deactivate_pages(&p->p_vmspace->vm_map,
9582fe6e4d7SDavid Greenman 				    (vm_map_entry_t) 0, &overage, vm_pageout_object_deactivate_pages);
9592fe6e4d7SDavid Greenman 			}
9602fe6e4d7SDavid Greenman 		}
9612fe6e4d7SDavid Greenman 
9620d94caffSDavid Greenman 		/*
9630d94caffSDavid Greenman 		 * we remove cached objects that have no RSS...
9640d94caffSDavid Greenman 		 */
9650d94caffSDavid Greenman restart:
9662fe6e4d7SDavid Greenman 		object = vm_object_cached_list.tqh_first;
9672fe6e4d7SDavid Greenman 		while (object) {
9682fe6e4d7SDavid Greenman 			/*
9692fe6e4d7SDavid Greenman 			 * if there are no resident pages -- get rid of the object
9702fe6e4d7SDavid Greenman 			 */
9712fe6e4d7SDavid Greenman 			if (object->resident_page_count == 0) {
97224a1cce3SDavid Greenman 				vm_object_reference(object);
9732fe6e4d7SDavid Greenman 				pager_cache(object, FALSE);
9742fe6e4d7SDavid Greenman 				goto restart;
9752fe6e4d7SDavid Greenman 			}
9762fe6e4d7SDavid Greenman 			object = object->cached_list.tqe_next;
9772fe6e4d7SDavid Greenman 		}
97824a1cce3SDavid Greenman 	}
9792fe6e4d7SDavid Greenman }
980