vm_pageout.c revision 216899
1284677Sdim/*-
2284677Sdim * Copyright (c) 1991 Regents of the University of California.
3284677Sdim * All rights reserved.
4284677Sdim * Copyright (c) 1994 John S. Dyson
5284677Sdim * All rights reserved.
6284677Sdim * Copyright (c) 1994 David Greenman
7284677Sdim * All rights reserved.
8284677Sdim * Copyright (c) 2005 Yahoo! Technologies Norway AS
9284677Sdim * All rights reserved.
10284677Sdim *
11341825Sdim * This code is derived from software contributed to Berkeley by
12284677Sdim * The Mach Operating System project at Carnegie-Mellon University.
13284677Sdim *
14284677Sdim * Redistribution and use in source and binary forms, with or without
15284677Sdim * modification, are permitted provided that the following conditions
16284677Sdim * are met:
17284677Sdim * 1. Redistributions of source code must retain the above copyright
18284677Sdim *    notice, this list of conditions and the following disclaimer.
19314564Sdim * 2. Redistributions in binary form must reproduce the above copyright
20284677Sdim *    notice, this list of conditions and the following disclaimer in the
21284677Sdim *    documentation and/or other materials provided with the distribution.
22341825Sdim * 3. All advertising materials mentioning features or use of this software
23314564Sdim *    must display the following acknowledgement:
24314564Sdim *	This product includes software developed by the University of
25314564Sdim *	California, Berkeley and its contributors.
26314564Sdim * 4. Neither the name of the University nor the names of its contributors
27314564Sdim *    may be used to endorse or promote products derived from this software
28314564Sdim *    without specific prior written permission.
29284677Sdim *
30314564Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31314564Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32314564Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33314564Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34314564Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35284677Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36314564Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37284677Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38284677Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39314564Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40314564Sdim * SUCH DAMAGE.
41314564Sdim *
42314564Sdim *	from: @(#)vm_pageout.c	7.4 (Berkeley) 5/7/91
43314564Sdim *
44341825Sdim *
45314564Sdim * Copyright (c) 1987, 1990 Carnegie-Mellon University.
46314564Sdim * All rights reserved.
47314564Sdim *
48314564Sdim * Authors: Avadis Tevanian, Jr., Michael Wayne Young
49314564Sdim *
50284677Sdim * Permission to use, copy, modify and distribute this software and
51284677Sdim * its documentation is hereby granted, provided that both the copyright
52284677Sdim * notice and this permission notice appear in all copies of the
53341825Sdim * software, derivative works or modified versions, and any portions
54341825Sdim * thereof, and that both notices appear in supporting documentation.
55309124Sdim *
56309124Sdim * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
57341825Sdim * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
58341825Sdim * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
59341825Sdim *
60341825Sdim * Carnegie Mellon requests users of this software to return to
61341825Sdim *
62341825Sdim *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
63341825Sdim *  School of Computer Science
64284677Sdim *  Carnegie Mellon University
65341825Sdim *  Pittsburgh PA 15213-3890
66284677Sdim *
67309124Sdim * any improvements or extensions that they make and grant Carnegie the
68309124Sdim * rights to redistribute these changes.
69309124Sdim */
70309124Sdim
71309124Sdim/*
72309124Sdim *	The proverbial page-out daemon.
73309124Sdim */
74309124Sdim
75309124Sdim#include <sys/cdefs.h>
76309124Sdim__FBSDID("$FreeBSD: head/sys/vm/vm_pageout.c 216899 2011-01-03 00:41:56Z alc $");
77309124Sdim
78309124Sdim#include "opt_vm.h"
79309124Sdim#include <sys/param.h>
80309124Sdim#include <sys/systm.h>
81309124Sdim#include <sys/kernel.h>
82309124Sdim#include <sys/eventhandler.h>
83309124Sdim#include <sys/lock.h>
84309124Sdim#include <sys/mutex.h>
85309124Sdim#include <sys/proc.h>
86309124Sdim#include <sys/kthread.h>
87309124Sdim#include <sys/ktr.h>
88309124Sdim#include <sys/mount.h>
89309124Sdim#include <sys/resourcevar.h>
90309124Sdim#include <sys/sched.h>
91309124Sdim#include <sys/signalvar.h>
92309124Sdim#include <sys/vnode.h>
93309124Sdim#include <sys/vmmeter.h>
94309124Sdim#include <sys/sx.h>
95309124Sdim#include <sys/sysctl.h>
96309124Sdim
97309124Sdim#include <vm/vm.h>
98309124Sdim#include <vm/vm_param.h>
99309124Sdim#include <vm/vm_object.h>
100309124Sdim#include <vm/vm_page.h>
101309124Sdim#include <vm/vm_map.h>
102309124Sdim#include <vm/vm_pageout.h>
103309124Sdim#include <vm/vm_pager.h>
104314564Sdim#include <vm/swap_pager.h>
105314564Sdim#include <vm/vm_extern.h>
106314564Sdim#include <vm/uma.h>
107314564Sdim
108314564Sdim/*
109314564Sdim * System initialization
110314564Sdim */
111314564Sdim
112314564Sdim/* the kernel process "vm_pageout"*/
113314564Sdimstatic void vm_pageout(void);
114314564Sdimstatic int vm_pageout_clean(vm_page_t);
115314564Sdimstatic void vm_pageout_scan(int pass);
116309124Sdim
117309124Sdimstruct proc *pageproc;
118309124Sdim
119309124Sdimstatic struct kproc_desc page_kp = {
120309124Sdim	"pagedaemon",
121284677Sdim	vm_pageout,
122284677Sdim	&pageproc
123284677Sdim};
124284677SdimSYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start,
125284677Sdim    &page_kp);
126284677Sdim
127284677Sdim#if !defined(NO_SWAPPING)
128284677Sdim/* the kernel process "vm_daemon"*/
129284677Sdimstatic void vm_daemon(void);
130284677Sdimstatic struct	proc *vmproc;
131284677Sdim
132284677Sdimstatic struct kproc_desc vm_kp = {
133284677Sdim	"vmdaemon",
134284677Sdim	vm_daemon,
135284677Sdim	&vmproc
136284677Sdim};
137284677SdimSYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp);
138284677Sdim#endif
139284677Sdim
140284677Sdim
141284677Sdimint vm_pages_needed;		/* Event on which pageout daemon sleeps */
142284677Sdimint vm_pageout_deficit;		/* Estimated number of pages deficit */
143284677Sdimint vm_pageout_pages_needed;	/* flag saying that the pageout daemon needs pages */
144284677Sdim
145284677Sdim#if !defined(NO_SWAPPING)
146284677Sdimstatic int vm_pageout_req_swapout;	/* XXX */
147284677Sdimstatic int vm_daemon_needed;
148284677Sdimstatic struct mtx vm_daemon_mtx;
149284677Sdim/* Allow for use by vm_pageout before vm_daemon is initialized. */
150284677SdimMTX_SYSINIT(vm_daemon, &vm_daemon_mtx, "vm daemon", MTX_DEF);
151284677Sdim#endif
152341825Sdimstatic int vm_max_launder = 32;
153341825Sdimstatic int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0;
154341825Sdimstatic int vm_pageout_full_stats_interval = 0;
155341825Sdimstatic int vm_pageout_algorithm=0;
156341825Sdimstatic int defer_swap_pageouts=0;
157284677Sdimstatic int disable_swap_pageouts=0;
158284677Sdim
159284677Sdim#if defined(NO_SWAPPING)
160284677Sdimstatic int vm_swap_enabled=0;
161284677Sdimstatic int vm_swap_idle_enabled=0;
162284677Sdim#else
163309124Sdimstatic int vm_swap_enabled=1;
164284677Sdimstatic int vm_swap_idle_enabled=0;
165284677Sdim#endif
166284677Sdim
167284677SdimSYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm,
168284677Sdim	CTLFLAG_RW, &vm_pageout_algorithm, 0, "LRU page mgmt");
169284677Sdim
170284677SdimSYSCTL_INT(_vm, OID_AUTO, max_launder,
171284677Sdim	CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout");
172284677Sdim
173284677SdimSYSCTL_INT(_vm, OID_AUTO, pageout_stats_max,
174284677Sdim	CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length");
175284677Sdim
176284677SdimSYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval,
177284677Sdim	CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan");
178284677Sdim
179284677SdimSYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval,
180284677Sdim	CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan");
181284677Sdim
182284677Sdim#if defined(NO_SWAPPING)
183284677SdimSYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
184284677Sdim	CTLFLAG_RD, &vm_swap_enabled, 0, "Enable entire process swapout");
185284677SdimSYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
186284677Sdim	CTLFLAG_RD, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
187284677Sdim#else
188284677SdimSYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
189284677Sdim	CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout");
190284677SdimSYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
191284677Sdim	CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
192284677Sdim#endif
193284677Sdim
194284677SdimSYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts,
195284677Sdim	CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem");
196284677Sdim
197284677SdimSYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts,
198284677Sdim	CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages");
199284677Sdim
200284677Sdimstatic int pageout_lock_miss;
201284677SdimSYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss,
202284677Sdim	CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout");
203284677Sdim
204284677Sdim#define VM_PAGEOUT_PAGE_COUNT 16
205284677Sdimint vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT;
206284677Sdim
207284677Sdimint vm_page_max_wired;		/* XXX max # of wired pages system-wide */
208284677SdimSYSCTL_INT(_vm, OID_AUTO, max_wired,
209284677Sdim	CTLFLAG_RW, &vm_page_max_wired, 0, "System-wide limit to wired page count");
210284677Sdim
211284677Sdim#if !defined(NO_SWAPPING)
212284677Sdimstatic void vm_pageout_map_deactivate_pages(vm_map_t, long);
213284677Sdimstatic void vm_pageout_object_deactivate_pages(pmap_t, vm_object_t, long);
214284677Sdimstatic void vm_req_vmdaemon(int req);
215284677Sdim#endif
216284677Sdimstatic void vm_pageout_page_stats(void);
217284677Sdim
218284677Sdimstatic void
219284677Sdimvm_pageout_init_marker(vm_page_t marker, u_short queue)
220284677Sdim{
221327952Sdim
222327952Sdim	bzero(marker, sizeof(*marker));
223327952Sdim	marker->flags = PG_FICTITIOUS | PG_MARKER;
224327952Sdim	marker->oflags = VPO_BUSY;
225327952Sdim	marker->queue = queue;
226341825Sdim	marker->wire_count = 1;
227341825Sdim}
228341825Sdim
229341825Sdim/*
230341825Sdim * vm_pageout_fallback_object_lock:
231341825Sdim *
232341825Sdim * Lock vm object currently associated with `m'. VM_OBJECT_TRYLOCK is
233341825Sdim * known to have failed and page queue must be either PQ_ACTIVE or
234341825Sdim * PQ_INACTIVE.  To avoid lock order violation, unlock the page queues
235341825Sdim * while locking the vm object.  Use marker page to detect page queue
236341825Sdim * changes and maintain notion of next page on page queue.  Return
237341825Sdim * TRUE if no changes were detected, FALSE otherwise.  vm object is
238341825Sdim * locked on return.
239341825Sdim *
240341825Sdim * This function depends on both the lock portion of struct vm_object
241341825Sdim * and normal struct vm_page being type stable.
242341825Sdim */
243341825Sdimboolean_t
244341825Sdimvm_pageout_fallback_object_lock(vm_page_t m, vm_page_t *next)
245341825Sdim{
246341825Sdim	struct vm_page marker;
247341825Sdim	boolean_t unchanged;
248341825Sdim	u_short queue;
249341825Sdim	vm_object_t object;
250341825Sdim
251341825Sdim	queue = m->queue;
252341825Sdim	vm_pageout_init_marker(&marker, queue);
253341825Sdim	object = m->object;
254284677Sdim
255284677Sdim	TAILQ_INSERT_AFTER(&vm_page_queues[queue].pl,
256284677Sdim			   m, &marker, pageq);
257284677Sdim	vm_page_unlock_queues();
258284677Sdim	vm_page_unlock(m);
259284677Sdim	VM_OBJECT_LOCK(object);
260284677Sdim	vm_page_lock(m);
261284677Sdim	vm_page_lock_queues();
262284677Sdim
263284677Sdim	/* Page queue might have changed. */
264321369Sdim	*next = TAILQ_NEXT(&marker, pageq);
265321369Sdim	unchanged = (m->queue == queue &&
266321369Sdim		     m->object == object &&
267321369Sdim		     &marker == TAILQ_NEXT(m, pageq));
268321369Sdim	TAILQ_REMOVE(&vm_page_queues[queue].pl,
269321369Sdim		     &marker, pageq);
270321369Sdim	return (unchanged);
271321369Sdim}
272321369Sdim
273284677Sdim/*
274309124Sdim * Lock the page while holding the page queue lock.  Use marker page
275309124Sdim * to detect page queue changes and maintain notion of next page on
276309124Sdim * page queue.  Return TRUE if no changes were detected, FALSE
277309124Sdim * otherwise.  The page is locked on return. The page queue lock might
278309124Sdim * be dropped and reacquired.
279309124Sdim *
280314564Sdim * This function depends on normal struct vm_page being type stable.
281284677Sdim */
282284677Sdimboolean_t
283296417Sdimvm_pageout_page_lock(vm_page_t m, vm_page_t *next)
284314564Sdim{
285314564Sdim	struct vm_page marker;
286341825Sdim	boolean_t unchanged;
287296417Sdim	u_short queue;
288296417Sdim
289309124Sdim	vm_page_lock_assert(m, MA_NOTOWNED);
290309124Sdim	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
291309124Sdim
292314564Sdim	if (vm_page_trylock(m))
293284677Sdim		return (TRUE);
294309124Sdim
295341825Sdim	queue = m->queue;
296284677Sdim	vm_pageout_init_marker(&marker, queue);
297309124Sdim
298284677Sdim	TAILQ_INSERT_AFTER(&vm_page_queues[queue].pl, m, &marker, pageq);
299284677Sdim	vm_page_unlock_queues();
300284677Sdim	vm_page_lock(m);
301309124Sdim	vm_page_lock_queues();
302341825Sdim
303284677Sdim	/* Page queue might have changed. */
304284677Sdim	*next = TAILQ_NEXT(&marker, pageq);
305284677Sdim	unchanged = (m->queue == queue && &marker == TAILQ_NEXT(m, pageq));
306284677Sdim	TAILQ_REMOVE(&vm_page_queues[queue].pl, &marker, pageq);
307309124Sdim	return (unchanged);
308341825Sdim}
309284677Sdim
310284677Sdim/*
311284677Sdim * vm_pageout_clean:
312341825Sdim *
313309124Sdim * Clean the page and remove it from the laundry.
314321369Sdim *
315284677Sdim * We set the busy bit to cause potential page faults on this page to
316284677Sdim * block.  Note the careful timing, however, the busy bit isn't set till
317284677Sdim * late and we cannot do anything that will mess with the page.
318284677Sdim */
319284677Sdimstatic int
320284677Sdimvm_pageout_clean(vm_page_t m)
321341825Sdim{
322309124Sdim	vm_object_t object;
323341825Sdim	vm_page_t mc[2*vm_pageout_page_count], pb, ps;
324309124Sdim	int pageout_count;
325309124Sdim	int ib, is, page_base;
326284677Sdim	vm_pindex_t pindex = m->pindex;
327284677Sdim
328284677Sdim	vm_page_lock_assert(m, MA_OWNED);
329341825Sdim	object = m->object;
330309124Sdim	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
331341825Sdim
332309124Sdim	/*
333309124Sdim	 * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP
334284677Sdim	 * with the new swapper, but we could have serious problems paging
335284677Sdim	 * out other object types if there is insufficient memory.
336284677Sdim	 *
337341825Sdim	 * Unfortunately, checking free memory here is far too late, so the
338309124Sdim	 * check has been moved up a procedural level.
339284677Sdim	 */
340284677Sdim
341309124Sdim	/*
342284677Sdim	 * Can't clean the page if it's busy or held.
343284677Sdim	 */
344284677Sdim	KASSERT(m->busy == 0 && (m->oflags & VPO_BUSY) == 0,
345341825Sdim	    ("vm_pageout_clean: page %p is busy", m));
346309124Sdim	KASSERT(m->hold_count == 0, ("vm_pageout_clean: page %p is held", m));
347309124Sdim	vm_page_unlock(m);
348309124Sdim
349309124Sdim	mc[vm_pageout_page_count] = pb = ps = m;
350309124Sdim	pageout_count = 1;
351284677Sdim	page_base = vm_pageout_page_count;
352314564Sdim	ib = 1;
353341825Sdim	is = 1;
354309124Sdim
355309124Sdim	/*
356284677Sdim	 * Scan object for clusterable pages.
357314564Sdim	 *
358341825Sdim	 * We can cluster ONLY if: ->> the page is NOT
359309124Sdim	 * clean, wired, busy, held, or mapped into a
360309124Sdim	 * buffer, and one of the following:
361341825Sdim	 * 1) The page is inactive, or a seldom used
362341825Sdim	 *    active page.
363309124Sdim	 * -or-
364309124Sdim	 * 2) we force the issue.
365309124Sdim	 *
366309124Sdim	 * During heavy mmap/modification loads the pageout
367314564Sdim	 * daemon can really fragment the underlying file
368341825Sdim	 * due to flushing pages out of order and not trying
369309124Sdim	 * align the clusters (which leave sporatic out-of-order
370341825Sdim	 * holes).  To solve this problem we do the reverse scan
371341825Sdim	 * first and attempt to align our cluster, then do a
372309124Sdim	 * forward scan if room remains.
373284677Sdim	 */
374284677Sdimmore:
375284677Sdim	while (ib && pageout_count < vm_pageout_page_count) {
376341825Sdim		vm_page_t p;
377341825Sdim
378341825Sdim		if (ib > pindex) {
379309124Sdim			ib = 0;
380321369Sdim			break;
381321369Sdim		}
382309124Sdim
383284677Sdim		if ((p = vm_page_prev(pb)) == NULL ||
384314564Sdim		    (p->oflags & VPO_BUSY) != 0 || p->busy != 0) {
385341825Sdim			ib = 0;
386309124Sdim			break;
387321369Sdim		}
388321369Sdim		vm_page_lock(p);
389321369Sdim		vm_page_test_dirty(p);
390309124Sdim		if (p->dirty == 0 ||
391296417Sdim		    p->queue != PQ_INACTIVE ||
392284677Sdim		    p->hold_count != 0) {	/* may be undergoing I/O */
393341825Sdim			vm_page_unlock(p);
394341825Sdim			ib = 0;
395321369Sdim			break;
396309124Sdim		}
397284677Sdim		vm_page_unlock(p);
398341825Sdim		mc[--page_base] = pb = p;
399284677Sdim		++pageout_count;
400341825Sdim		++ib;
401341825Sdim		/*
402321369Sdim		 * alignment boundry, stop here and switch directions.  Do
403341825Sdim		 * not clear ib.
404309124Sdim		 */
405309124Sdim		if ((pindex - (ib - 1)) % vm_pageout_page_count == 0)
406341825Sdim			break;
407321369Sdim	}
408341825Sdim
409284677Sdim	while (pageout_count < vm_pageout_page_count &&
410284677Sdim	    pindex + is < object->size) {
411284677Sdim		vm_page_t p;
412341825Sdim
413284677Sdim		if ((p = vm_page_next(ps)) == NULL ||
414341825Sdim		    (p->oflags & VPO_BUSY) != 0 || p->busy != 0)
415341825Sdim			break;
416321369Sdim		vm_page_lock(p);
417341825Sdim		vm_page_test_dirty(p);
418284677Sdim		if (p->dirty == 0 ||
419309124Sdim		    p->queue != PQ_INACTIVE ||
420341825Sdim		    p->hold_count != 0) {	/* may be undergoing I/O */
421321369Sdim			vm_page_unlock(p);
422341825Sdim			break;
423284677Sdim		}
424284677Sdim		vm_page_unlock(p);
425284677Sdim		mc[page_base + pageout_count] = ps = p;
426341825Sdim		++pageout_count;
427341825Sdim		++is;
428284677Sdim	}
429284677Sdim
430309124Sdim	/*
431284677Sdim	 * If we exhausted our forward scan, continue with the reverse scan
432284677Sdim	 * when possible, even past a page boundry.  This catches boundry
433284677Sdim	 * conditions.
434341825Sdim	 */
435341825Sdim	if (ib && pageout_count < vm_pageout_page_count)
436284677Sdim		goto more;
437284677Sdim
438284677Sdim	/*
439284677Sdim	 * we allow reads during pageouts...
440284677Sdim	 */
441284677Sdim	return (vm_pageout_flush(&mc[page_base], pageout_count, 0, 0, NULL));
442284677Sdim}
443284677Sdim
444296417Sdim/*
445284677Sdim * vm_pageout_flush() - launder the given pages
446284677Sdim *
447341825Sdim *	The given pages are laundered.  Note that we setup for the start of
448309124Sdim *	I/O ( i.e. busy the page ), mark it read-only, and bump the object
449321369Sdim *	reference count all in here rather then in the parent.  If we want
450321369Sdim *	the parent to do more sophisticated things we may have to change
451321369Sdim *	the ordering.
452321369Sdim *
453321369Sdim *	Returned runlen is the count of pages between mreq and first
454321369Sdim *	page after mreq with status VM_PAGER_AGAIN.
455321369Sdim */
456309124Sdimint
457309124Sdimvm_pageout_flush(vm_page_t *mc, int count, int flags, int mreq, int *prunlen)
458284677Sdim{
459284677Sdim	vm_object_t object = mc[0]->object;
460341825Sdim	int pageout_status[count];
461284677Sdim	int numpagedout = 0;
462284677Sdim	int i, runlen;
463284677Sdim
464284677Sdim	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
465309124Sdim	mtx_assert(&vm_page_queue_mtx, MA_NOTOWNED);
466284677Sdim
467284677Sdim	/*
468284677Sdim	 * Initiate I/O.  Bump the vm_page_t->busy counter and
469284677Sdim	 * mark the pages read-only.
470284677Sdim	 *
471284677Sdim	 * We do not have to fixup the clean/dirty bits here... we can
472284677Sdim	 * allow the pager to do it after the I/O completes.
473284677Sdim	 *
474284677Sdim	 * NOTE! mc[i]->dirty may be partial or fragmented due to an
475284677Sdim	 * edge case with file fragments.
476284677Sdim	 */
477284677Sdim	for (i = 0; i < count; i++) {
478284677Sdim		KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL,
479284677Sdim		    ("vm_pageout_flush: partially invalid page %p index %d/%d",
480284677Sdim			mc[i], i, count));
481284677Sdim		vm_page_io_start(mc[i]);
482284677Sdim		pmap_remove_write(mc[i]);
483284677Sdim	}
484284677Sdim	vm_object_pip_add(object, count);
485284677Sdim
486284677Sdim	vm_pager_put_pages(object, mc, count, flags, pageout_status);
487284677Sdim
488284677Sdim	runlen = count - mreq;
489284677Sdim	for (i = 0; i < count; i++) {
490284677Sdim		vm_page_t mt = mc[i];
491284677Sdim
492284677Sdim		KASSERT(pageout_status[i] == VM_PAGER_PEND ||
493284677Sdim		    (mt->flags & PG_WRITEABLE) == 0,
494284677Sdim		    ("vm_pageout_flush: page %p is not write protected", mt));
495284677Sdim		switch (pageout_status[i]) {
496284677Sdim		case VM_PAGER_OK:
497284677Sdim		case VM_PAGER_PEND:
498284677Sdim			numpagedout++;
499309124Sdim			break;
500284677Sdim		case VM_PAGER_BAD:
501284677Sdim			/*
502284677Sdim			 * Page outside of range of object. Right now we
503284677Sdim			 * essentially lose the changes by pretending it
504284677Sdim			 * worked.
505341825Sdim			 */
506284677Sdim			vm_page_undirty(mt);
507284677Sdim			break;
508284677Sdim		case VM_PAGER_ERROR:
509284677Sdim		case VM_PAGER_FAIL:
510284677Sdim			/*
511284677Sdim			 * If page couldn't be paged out, then reactivate the
512284677Sdim			 * page so it doesn't clog the inactive list.  (We
513284677Sdim			 * will try paging out it again later).
514284677Sdim			 */
515284677Sdim			vm_page_lock(mt);
516284677Sdim			vm_page_activate(mt);
517314564Sdim			vm_page_unlock(mt);
518284677Sdim			break;
519284677Sdim		case VM_PAGER_AGAIN:
520284677Sdim			if (i >= mreq && i - mreq < runlen)
521284677Sdim				runlen = i - mreq;
522284677Sdim			break;
523284677Sdim		}
524284677Sdim
525284677Sdim		/*
526284677Sdim		 * If the operation is still going, leave the page busy to
527284677Sdim		 * block all other accesses. Also, leave the paging in
528284677Sdim		 * progress indicator set so that we don't attempt an object
529284677Sdim		 * collapse.
530284677Sdim		 */
531321369Sdim		if (pageout_status[i] != VM_PAGER_PEND) {
532341825Sdim			vm_object_pip_wakeup(object);
533341825Sdim			vm_page_io_finish(mt);
534284677Sdim			if (vm_page_count_severe()) {
535284677Sdim				vm_page_lock(mt);
536341825Sdim				vm_page_try_to_cache(mt);
537284677Sdim				vm_page_unlock(mt);
538284677Sdim			}
539341825Sdim		}
540284677Sdim	}
541284677Sdim	if (prunlen != NULL)
542284677Sdim		*prunlen = runlen;
543314564Sdim	return (numpagedout);
544284677Sdim}
545284677Sdim
546284677Sdim#if !defined(NO_SWAPPING)
547284677Sdim/*
548284677Sdim *	vm_pageout_object_deactivate_pages
549284677Sdim *
550284677Sdim *	Deactivate enough pages to satisfy the inactive target
551284677Sdim *	requirements.
552284677Sdim *
553284677Sdim *	The object and map must be locked.
554284677Sdim */
555284677Sdimstatic void
556284677Sdimvm_pageout_object_deactivate_pages(pmap_t pmap, vm_object_t first_object,
557284677Sdim    long desired)
558284677Sdim{
559284677Sdim	vm_object_t backing_object, object;
560284677Sdim	vm_page_t p;
561284677Sdim	int actcount, remove_mode;
562284677Sdim
563284677Sdim	VM_OBJECT_LOCK_ASSERT(first_object, MA_OWNED);
564284677Sdim	if (first_object->type == OBJT_DEVICE ||
565284677Sdim	    first_object->type == OBJT_SG)
566284677Sdim		return;
567284677Sdim	for (object = first_object;; object = backing_object) {
568284677Sdim		if (pmap_resident_count(pmap) <= desired)
569341825Sdim			goto unlock_return;
570284677Sdim		VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
571284677Sdim		if (object->type == OBJT_PHYS || object->paging_in_progress)
572284677Sdim			goto unlock_return;
573284677Sdim
574284677Sdim		remove_mode = 0;
575284677Sdim		if (object->shadow_count > 1)
576284677Sdim			remove_mode = 1;
577284677Sdim		/*
578284677Sdim		 * Scan the object's entire memory queue.
579284677Sdim		 */
580284677Sdim		TAILQ_FOREACH(p, &object->memq, listq) {
581284677Sdim			if (pmap_resident_count(pmap) <= desired)
582284677Sdim				goto unlock_return;
583284677Sdim			if ((p->oflags & VPO_BUSY) != 0 || p->busy != 0)
584284677Sdim				continue;
585284677Sdim			PCPU_INC(cnt.v_pdpages);
586284677Sdim			vm_page_lock(p);
587284677Sdim			if (p->wire_count != 0 || p->hold_count != 0 ||
588284677Sdim			    !pmap_page_exists_quick(pmap, p)) {
589284677Sdim				vm_page_unlock(p);
590284677Sdim				continue;
591309124Sdim			}
592321369Sdim			actcount = pmap_ts_referenced(p);
593341825Sdim			if ((p->flags & PG_REFERENCED) != 0) {
594309124Sdim				if (actcount == 0)
595309124Sdim					actcount = 1;
596284677Sdim				vm_page_lock_queues();
597284677Sdim				vm_page_flag_clear(p, PG_REFERENCED);
598284677Sdim				vm_page_unlock_queues();
599284677Sdim			}
600284677Sdim			if (p->queue != PQ_ACTIVE && actcount != 0) {
601284677Sdim				vm_page_activate(p);
602284677Sdim				p->act_count += actcount;
603284677Sdim			} else if (p->queue == PQ_ACTIVE) {
604284677Sdim				if (actcount == 0) {
605284677Sdim					p->act_count -= min(p->act_count,
606284677Sdim					    ACT_DECLINE);
607284677Sdim					if (!remove_mode &&
608284677Sdim					    (vm_pageout_algorithm ||
609284677Sdim					    p->act_count == 0)) {
610284677Sdim						pmap_remove_all(p);
611284677Sdim						vm_page_deactivate(p);
612284677Sdim					} else {
613284677Sdim						vm_page_lock_queues();
614284677Sdim						vm_page_requeue(p);
615284677Sdim						vm_page_unlock_queues();
616341825Sdim					}
617341825Sdim				} else {
618284677Sdim					vm_page_activate(p);
619341825Sdim					if (p->act_count < ACT_MAX -
620341825Sdim					    ACT_ADVANCE)
621284677Sdim						p->act_count += ACT_ADVANCE;
622341825Sdim					vm_page_lock_queues();
623341825Sdim					vm_page_requeue(p);
624284677Sdim					vm_page_unlock_queues();
625341825Sdim				}
626341825Sdim			} else if (p->queue == PQ_INACTIVE)
627284677Sdim				pmap_remove_all(p);
628341825Sdim			vm_page_unlock(p);
629341825Sdim		}
630284677Sdim		if ((backing_object = object->backing_object) == NULL)
631341825Sdim			goto unlock_return;
632341825Sdim		VM_OBJECT_LOCK(backing_object);
633284677Sdim		if (object != first_object)
634309124Sdim			VM_OBJECT_UNLOCK(object);
635309124Sdim	}
636309124Sdimunlock_return:
637309124Sdim	if (object != first_object)
638309124Sdim		VM_OBJECT_UNLOCK(object);
639321369Sdim}
640321369Sdim
641284677Sdim/*
642309124Sdim * deactivate some number of pages in a map, try to do it fairly, but
643284677Sdim * that is really hard to do.
644284677Sdim */
645284677Sdimstatic void
646284677Sdimvm_pageout_map_deactivate_pages(map, desired)
647284677Sdim	vm_map_t map;
648284677Sdim	long desired;
649284677Sdim{
650284677Sdim	vm_map_entry_t tmpe;
651284677Sdim	vm_object_t obj, bigobj;
652284677Sdim	int nothingwired;
653284677Sdim
654284677Sdim	if (!vm_map_trylock(map))
655284677Sdim		return;
656284677Sdim
657284677Sdim	bigobj = NULL;
658284677Sdim	nothingwired = TRUE;
659309124Sdim
660284677Sdim	/*
661284677Sdim	 * first, search out the biggest object, and try to free pages from
662314564Sdim	 * that.
663314564Sdim	 */
664314564Sdim	tmpe = map->header.next;
665314564Sdim	while (tmpe != &map->header) {
666284677Sdim		if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
667309124Sdim			obj = tmpe->object.vm_object;
668309124Sdim			if (obj != NULL && VM_OBJECT_TRYLOCK(obj)) {
669309124Sdim				if (obj->shadow_count <= 1 &&
670309124Sdim				    (bigobj == NULL ||
671309124Sdim				     bigobj->resident_page_count < obj->resident_page_count)) {
672284677Sdim					if (bigobj != NULL)
673284677Sdim						VM_OBJECT_UNLOCK(bigobj);
674284677Sdim					bigobj = obj;
675284677Sdim				} else
676284677Sdim					VM_OBJECT_UNLOCK(obj);
677284677Sdim			}
678284677Sdim		}
679284677Sdim		if (tmpe->wired_count > 0)
680284677Sdim			nothingwired = FALSE;
681284677Sdim		tmpe = tmpe->next;
682284677Sdim	}
683284677Sdim
684284677Sdim	if (bigobj != NULL) {
685284677Sdim		vm_pageout_object_deactivate_pages(map->pmap, bigobj, desired);
686284677Sdim		VM_OBJECT_UNLOCK(bigobj);
687284677Sdim	}
688284677Sdim	/*
689284677Sdim	 * Next, hunt around for other pages to deactivate.  We actually
690284677Sdim	 * do this search sort of wrong -- .text first is not the best idea.
691284677Sdim	 */
692284677Sdim	tmpe = map->header.next;
693284677Sdim	while (tmpe != &map->header) {
694284677Sdim		if (pmap_resident_count(vm_map_pmap(map)) <= desired)
695284677Sdim			break;
696284677Sdim		if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
697284677Sdim			obj = tmpe->object.vm_object;
698284677Sdim			if (obj != NULL) {
699314564Sdim				VM_OBJECT_LOCK(obj);
700286684Sdim				vm_pageout_object_deactivate_pages(map->pmap, obj, desired);
701286684Sdim				VM_OBJECT_UNLOCK(obj);
702286684Sdim			}
703284677Sdim		}
704284677Sdim		tmpe = tmpe->next;
705284677Sdim	}
706284677Sdim
707284677Sdim	/*
708284677Sdim	 * Remove all mappings if a process is swapped out, this will free page
709284677Sdim	 * table pages.
710284677Sdim	 */
711284677Sdim	if (desired == 0 && nothingwired) {
712284677Sdim		tmpe = map->header.next;
713284677Sdim		while (tmpe != &map->header) {
714284677Sdim			pmap_remove(vm_map_pmap(map), tmpe->start, tmpe->end);
715284677Sdim			tmpe = tmpe->next;
716284677Sdim		}
717284677Sdim	}
718284677Sdim	vm_map_unlock(map);
719284677Sdim}
720284677Sdim#endif		/* !defined(NO_SWAPPING) */
721284677Sdim
722284677Sdim/*
723284677Sdim *	vm_pageout_scan does the dirty work for the pageout daemon.
724284677Sdim */
725284677Sdimstatic void
726284677Sdimvm_pageout_scan(int pass)
727284677Sdim{
728284677Sdim	vm_page_t m, next;
729284677Sdim	struct vm_page marker;
730284677Sdim	int page_shortage, maxscan, pcount;
731284677Sdim	int addl_page_shortage, addl_page_shortage_init;
732284677Sdim	vm_object_t object;
733284677Sdim	int actcount;
734284677Sdim	int vnodes_skipped = 0;
735284677Sdim	int maxlaunder;
736284677Sdim
737284677Sdim	/*
738284677Sdim	 * Decrease registered cache sizes.
739284677Sdim	 */
740309124Sdim	EVENTHANDLER_INVOKE(vm_lowmem, 0);
741309124Sdim	/*
742309124Sdim	 * We do this explicitly after the caches have been drained above.
743309124Sdim	 */
744321369Sdim	uma_reclaim();
745309124Sdim
746309124Sdim	addl_page_shortage_init = atomic_readandclear_int(&vm_pageout_deficit);
747309124Sdim
748309124Sdim	/*
749321369Sdim	 * Calculate the number of pages we want to either free or move
750309124Sdim	 * to the cache.
751309124Sdim	 */
752309124Sdim	page_shortage = vm_paging_target() + addl_page_shortage_init;
753309124Sdim
754309124Sdim	vm_pageout_init_marker(&marker, PQ_INACTIVE);
755284677Sdim
756284677Sdim	/*
757284677Sdim	 * Start scanning the inactive queue for pages we can move to the
758284677Sdim	 * cache or free.  The scan will stop when the target is reached or
759284677Sdim	 * we have scanned the entire inactive queue.  Note that m->act_count
760284677Sdim	 * is not used to form decisions for the inactive queue, only for the
761296417Sdim	 * active queue.
762296417Sdim	 *
763284677Sdim	 * maxlaunder limits the number of dirty pages we flush per scan.
764284677Sdim	 * For most systems a smaller value (16 or 32) is more robust under
765284677Sdim	 * extreme memory and disk pressure because any unnecessary writes
766284677Sdim	 * to disk can result in extreme performance degredation.  However,
767284677Sdim	 * systems with excessive dirty pages (especially when MAP_NOSYNC is
768284677Sdim	 * used) will die horribly with limited laundering.  If the pageout
769284677Sdim	 * daemon cannot clean enough pages in the first pass, we let it go
770284677Sdim	 * all out in succeeding passes.
771284677Sdim	 */
772284677Sdim	if ((maxlaunder = vm_max_launder) <= 1)
773284677Sdim		maxlaunder = 1;
774284677Sdim	if (pass)
775284677Sdim		maxlaunder = 10000;
776284677Sdim	vm_page_lock_queues();
777284677Sdimrescan0:
778284677Sdim	addl_page_shortage = addl_page_shortage_init;
779284677Sdim	maxscan = cnt.v_inactive_count;
780284677Sdim
781284677Sdim	for (m = TAILQ_FIRST(&vm_page_queues[PQ_INACTIVE].pl);
782341825Sdim	     m != NULL && maxscan-- > 0 && page_shortage > 0;
783284677Sdim	     m = next) {
784284677Sdim
785284677Sdim		cnt.v_pdpages++;
786284677Sdim
787284677Sdim		if (m->queue != PQ_INACTIVE)
788284677Sdim			goto rescan0;
789284677Sdim
790284677Sdim		next = TAILQ_NEXT(m, pageq);
791284677Sdim
792284677Sdim		/*
793284677Sdim		 * skip marker pages
794284677Sdim		 */
795284677Sdim		if (m->flags & PG_MARKER)
796284677Sdim			continue;
797284677Sdim
798284677Sdim		/*
799284677Sdim		 * Lock the page.
800284677Sdim		 */
801284677Sdim		if (!vm_pageout_page_lock(m, &next)) {
802284677Sdim			vm_page_unlock(m);
803284677Sdim			addl_page_shortage++;
804284677Sdim			continue;
805284677Sdim		}
806284677Sdim
807284677Sdim		/*
808284677Sdim		 * A held page may be undergoing I/O, so skip it.
809284677Sdim		 */
810284677Sdim		if (m->hold_count) {
811284677Sdim			vm_page_unlock(m);
812284677Sdim			vm_page_requeue(m);
813284677Sdim			addl_page_shortage++;
814284677Sdim			continue;
815284677Sdim		}
816284677Sdim
817284677Sdim		/*
818284677Sdim		 * Don't mess with busy pages, keep in the front of the
819284677Sdim		 * queue, most likely are being paged out.
820284677Sdim		 */
821284677Sdim		object = m->object;
822284677Sdim		if (!VM_OBJECT_TRYLOCK(object) &&
823284677Sdim		    (!vm_pageout_fallback_object_lock(m, &next) ||
824284677Sdim			m->hold_count != 0)) {
825284677Sdim			VM_OBJECT_UNLOCK(object);
826284677Sdim			vm_page_unlock(m);
827284677Sdim			addl_page_shortage++;
828284677Sdim			continue;
829284677Sdim		}
830284677Sdim		if (m->busy || (m->oflags & VPO_BUSY)) {
831284677Sdim			vm_page_unlock(m);
832284677Sdim			VM_OBJECT_UNLOCK(object);
833284677Sdim			addl_page_shortage++;
834284677Sdim			continue;
835284677Sdim		}
836284677Sdim
837284677Sdim		/*
838284677Sdim		 * If the object is not being used, we ignore previous
839284677Sdim		 * references.
840284677Sdim		 */
841284677Sdim		if (object->ref_count == 0) {
842284677Sdim			vm_page_flag_clear(m, PG_REFERENCED);
843284677Sdim			KASSERT(!pmap_page_is_mapped(m),
844284677Sdim			    ("vm_pageout_scan: page %p is mapped", m));
845284677Sdim
846284677Sdim		/*
847284677Sdim		 * Otherwise, if the page has been referenced while in the
848284677Sdim		 * inactive queue, we bump the "activation count" upwards,
849284677Sdim		 * making it less likely that the page will be added back to
850284677Sdim		 * the inactive queue prematurely again.  Here we check the
851284677Sdim		 * page tables (or emulated bits, if any), given the upper
852284677Sdim		 * level VM system not knowing anything about existing
853284677Sdim		 * references.
854284677Sdim		 */
855284677Sdim		} else if (((m->flags & PG_REFERENCED) == 0) &&
856284677Sdim			(actcount = pmap_ts_referenced(m))) {
857284677Sdim			vm_page_activate(m);
858284677Sdim			VM_OBJECT_UNLOCK(object);
859284677Sdim			m->act_count += (actcount + ACT_ADVANCE);
860284677Sdim			vm_page_unlock(m);
861284677Sdim			continue;
862284677Sdim		}
863284677Sdim
864284677Sdim		/*
865284677Sdim		 * If the upper level VM system knows about any page
866284677Sdim		 * references, we activate the page.  We also set the
867284677Sdim		 * "activation count" higher than normal so that we will less
868284677Sdim		 * likely place pages back onto the inactive queue again.
869284677Sdim		 */
870284677Sdim		if ((m->flags & PG_REFERENCED) != 0) {
871284677Sdim			vm_page_flag_clear(m, PG_REFERENCED);
872284677Sdim			actcount = pmap_ts_referenced(m);
873284677Sdim			vm_page_activate(m);
874284677Sdim			VM_OBJECT_UNLOCK(object);
875284677Sdim			m->act_count += (actcount + ACT_ADVANCE + 1);
876284677Sdim			vm_page_unlock(m);
877284677Sdim			continue;
878284677Sdim		}
879284677Sdim
880284677Sdim		/*
881309124Sdim		 * If the upper level VM system does not believe that the page
882284677Sdim		 * is fully dirty, but it is mapped for write access, then we
883284677Sdim		 * consult the pmap to see if the page's dirty status should
884284677Sdim		 * be updated.
885284677Sdim		 */
886284677Sdim		if (m->dirty != VM_PAGE_BITS_ALL &&
887309124Sdim		    (m->flags & PG_WRITEABLE) != 0) {
888309124Sdim			/*
889284677Sdim			 * Avoid a race condition: Unless write access is
890284677Sdim			 * removed from the page, another processor could
891309124Sdim			 * modify it before all access is removed by the call
892309124Sdim			 * to vm_page_cache() below.  If vm_page_cache() finds
893309124Sdim			 * that the page has been modified when it removes all
894309124Sdim			 * access, it panics because it cannot cache dirty
895309124Sdim			 * pages.  In principle, we could eliminate just write
896309124Sdim			 * access here rather than all access.  In the expected
897309124Sdim			 * case, when there are no last instant modifications
898309124Sdim			 * to the page, removing all access will be cheaper
899309124Sdim			 * overall.
900309124Sdim			 */
901284677Sdim			if (pmap_is_modified(m))
902309124Sdim				vm_page_dirty(m);
903284677Sdim			else if (m->dirty == 0)
904284677Sdim				pmap_remove_all(m);
905284677Sdim		}
906341825Sdim
907284677Sdim		if (m->valid == 0) {
908284677Sdim			/*
909284677Sdim			 * Invalid pages can be easily freed
910284677Sdim			 */
911284677Sdim			vm_page_free(m);
912284677Sdim			cnt.v_dfree++;
913309124Sdim			--page_shortage;
914284677Sdim		} else if (m->dirty == 0) {
915284677Sdim			/*
916284677Sdim			 * Clean pages can be placed onto the cache queue.
917284677Sdim			 * This effectively frees them.
918284677Sdim			 */
919284677Sdim			vm_page_cache(m);
920284677Sdim			--page_shortage;
921284677Sdim		} else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) {
922284677Sdim			/*
923284677Sdim			 * Dirty pages need to be paged out, but flushing
924284677Sdim			 * a page is extremely expensive verses freeing
925284677Sdim			 * a clean page.  Rather then artificially limiting
926309124Sdim			 * the number of pages we can flush, we instead give
927309124Sdim			 * dirty pages extra priority on the inactive queue
928309124Sdim			 * by forcing them to be cycled through the queue
929309124Sdim			 * twice before being flushed, after which the
930309124Sdim			 * (now clean) page will cycle through once more
931309124Sdim			 * before being freed.  This significantly extends
932309124Sdim			 * the thrash point for a heavily loaded machine.
933309124Sdim			 */
934309124Sdim			vm_page_flag_set(m, PG_WINATCFLS);
935309124Sdim			vm_page_requeue(m);
936309124Sdim		} else if (maxlaunder > 0) {
937309124Sdim			/*
938309124Sdim			 * We always want to try to flush some dirty pages if
939309124Sdim			 * we encounter them, to keep the system stable.
940284677Sdim			 * Normally this number is small, but under extreme
941284677Sdim			 * pressure where there are insufficient clean pages
942284677Sdim			 * on the inactive queue, we may have to go all out.
943284677Sdim			 */
944284677Sdim			int swap_pageouts_ok, vfslocked = 0;
945284677Sdim			struct vnode *vp = NULL;
946284677Sdim			struct mount *mp = NULL;
947284677Sdim
948284677Sdim			if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) {
949284677Sdim				swap_pageouts_ok = 1;
950284677Sdim			} else {
951284677Sdim				swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts);
952284677Sdim				swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts &&
953321369Sdim				vm_page_count_min());
954284677Sdim
955284677Sdim			}
956284677Sdim
957284677Sdim			/*
958284677Sdim			 * We don't bother paging objects that are "dead".
959284677Sdim			 * Those objects are in a "rundown" state.
960284677Sdim			 */
961284677Sdim			if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) {
962284677Sdim				vm_page_unlock(m);
963284677Sdim				VM_OBJECT_UNLOCK(object);
964284677Sdim				vm_page_requeue(m);
965284677Sdim				continue;
966284677Sdim			}
967284677Sdim
968284677Sdim			/*
969284677Sdim			 * Following operations may unlock
970284677Sdim			 * vm_page_queue_mtx, invalidating the 'next'
971284677Sdim			 * pointer.  To prevent an inordinate number
972284677Sdim			 * of restarts we use our marker to remember
973284677Sdim			 * our place.
974284677Sdim			 *
975284677Sdim			 */
976284677Sdim			TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE].pl,
977284677Sdim					   m, &marker, pageq);
978284677Sdim			/*
979284677Sdim			 * The object is already known NOT to be dead.   It
980284677Sdim			 * is possible for the vget() to block the whole
981284677Sdim			 * pageout daemon, but the new low-memory handling
982284677Sdim			 * code should prevent it.
983284677Sdim			 *
984284677Sdim			 * The previous code skipped locked vnodes and, worse,
985284677Sdim			 * reordered pages in the queue.  This results in
986284677Sdim			 * completely non-deterministic operation and, on a
987284677Sdim			 * busy system, can lead to extremely non-optimal
988284677Sdim			 * pageouts.  For example, it can cause clean pages
989284677Sdim			 * to be freed and dirty pages to be moved to the end
990284677Sdim			 * of the queue.  Since dirty pages are also moved to
991284677Sdim			 * the end of the queue once-cleaned, this gives
992284677Sdim			 * way too large a weighting to defering the freeing
993284677Sdim			 * of dirty pages.
994284677Sdim			 *
995284677Sdim			 * We can't wait forever for the vnode lock, we might
996284677Sdim			 * deadlock due to a vn_read() getting stuck in
997284677Sdim			 * vm_wait while holding this vnode.  We skip the
998284677Sdim			 * vnode if we can't get it in a reasonable amount
999284677Sdim			 * of time.
1000284677Sdim			 */
1001284677Sdim			if (object->type == OBJT_VNODE) {
1002284677Sdim				vm_page_unlock_queues();
1003284677Sdim				vm_page_unlock(m);
1004284677Sdim				vp = object->handle;
1005284677Sdim				if (vp->v_type == VREG &&
1006284677Sdim				    vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1007284677Sdim					mp = NULL;
1008284677Sdim					++pageout_lock_miss;
1009284677Sdim					if (object->flags & OBJ_MIGHTBEDIRTY)
1010284677Sdim						vnodes_skipped++;
1011284677Sdim					vm_page_lock_queues();
1012284677Sdim					goto unlock_and_continue;
1013284677Sdim				}
1014284677Sdim				KASSERT(mp != NULL,
1015284677Sdim				    ("vp %p with NULL v_mount", vp));
1016284677Sdim				vm_object_reference_locked(object);
1017284677Sdim				VM_OBJECT_UNLOCK(object);
1018284677Sdim				vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1019284677Sdim				if (vget(vp, LK_EXCLUSIVE | LK_TIMELOCK,
1020284677Sdim				    curthread)) {
1021284677Sdim					VM_OBJECT_LOCK(object);
1022284677Sdim					vm_page_lock_queues();
1023284677Sdim					++pageout_lock_miss;
1024284677Sdim					if (object->flags & OBJ_MIGHTBEDIRTY)
1025284677Sdim						vnodes_skipped++;
1026284677Sdim					vp = NULL;
1027284677Sdim					goto unlock_and_continue;
1028284677Sdim				}
1029284677Sdim				VM_OBJECT_LOCK(object);
1030284677Sdim				vm_page_lock(m);
1031284677Sdim				vm_page_lock_queues();
1032284677Sdim				/*
1033284677Sdim				 * The page might have been moved to another
1034284677Sdim				 * queue during potential blocking in vget()
1035284677Sdim				 * above.  The page might have been freed and
1036284677Sdim				 * reused for another vnode.
1037284677Sdim				 */
1038284677Sdim				if (m->queue != PQ_INACTIVE ||
1039284677Sdim				    m->object != object ||
1040284677Sdim				    TAILQ_NEXT(m, pageq) != &marker) {
1041284677Sdim					vm_page_unlock(m);
1042284677Sdim					if (object->flags & OBJ_MIGHTBEDIRTY)
1043284677Sdim						vnodes_skipped++;
1044284677Sdim					goto unlock_and_continue;
1045284677Sdim				}
1046284677Sdim
1047284677Sdim				/*
1048284677Sdim				 * The page may have been busied during the
1049284677Sdim				 * blocking in vget().  We don't move the
1050284677Sdim				 * page back onto the end of the queue so that
1051284677Sdim				 * statistics are more correct if we don't.
1052284677Sdim				 */
1053284677Sdim				if (m->busy || (m->oflags & VPO_BUSY)) {
1054284677Sdim					vm_page_unlock(m);
1055284677Sdim					goto unlock_and_continue;
1056284677Sdim				}
1057284677Sdim
1058284677Sdim				/*
1059284677Sdim				 * If the page has become held it might
1060284677Sdim				 * be undergoing I/O, so skip it
1061284677Sdim				 */
1062284677Sdim				if (m->hold_count) {
1063284677Sdim					vm_page_unlock(m);
1064284677Sdim					vm_page_requeue(m);
1065284677Sdim					if (object->flags & OBJ_MIGHTBEDIRTY)
1066284677Sdim						vnodes_skipped++;
1067284677Sdim					goto unlock_and_continue;
1068284677Sdim				}
1069284677Sdim			}
1070284677Sdim
1071284677Sdim			/*
1072284677Sdim			 * If a page is dirty, then it is either being washed
1073284677Sdim			 * (but not yet cleaned) or it is still in the
1074284677Sdim			 * laundry.  If it is still in the laundry, then we
1075284677Sdim			 * start the cleaning operation.
1076284677Sdim			 *
1077284677Sdim			 * decrement page_shortage on success to account for
1078284677Sdim			 * the (future) cleaned page.  Otherwise we could wind
1079284677Sdim			 * up laundering or cleaning too many pages.
1080284677Sdim			 */
1081284677Sdim			vm_page_unlock_queues();
1082284677Sdim			if (vm_pageout_clean(m) != 0) {
1083284677Sdim				--page_shortage;
1084284677Sdim				--maxlaunder;
1085284677Sdim			}
1086284677Sdim			vm_page_lock_queues();
1087284677Sdimunlock_and_continue:
1088284677Sdim			vm_page_lock_assert(m, MA_NOTOWNED);
1089284677Sdim			VM_OBJECT_UNLOCK(object);
1090284677Sdim			if (mp != NULL) {
1091284677Sdim				vm_page_unlock_queues();
1092284677Sdim				if (vp != NULL)
1093284677Sdim					vput(vp);
1094284677Sdim				VFS_UNLOCK_GIANT(vfslocked);
1095284677Sdim				vm_object_deallocate(object);
1096284677Sdim				vn_finished_write(mp);
1097284677Sdim				vm_page_lock_queues();
1098284677Sdim			}
1099284677Sdim			next = TAILQ_NEXT(&marker, pageq);
1100284677Sdim			TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl,
1101284677Sdim				     &marker, pageq);
1102284677Sdim			vm_page_lock_assert(m, MA_NOTOWNED);
1103284677Sdim			continue;
1104284677Sdim		}
1105284677Sdim		vm_page_unlock(m);
1106284677Sdim		VM_OBJECT_UNLOCK(object);
1107284677Sdim	}
1108284677Sdim
1109284677Sdim	/*
1110284677Sdim	 * Compute the number of pages we want to try to move from the
1111284677Sdim	 * active queue to the inactive queue.
1112284677Sdim	 */
1113284677Sdim	page_shortage = vm_paging_target() +
1114284677Sdim		cnt.v_inactive_target - cnt.v_inactive_count;
1115284677Sdim	page_shortage += addl_page_shortage;
1116284677Sdim
1117284677Sdim	/*
1118284677Sdim	 * Scan the active queue for things we can deactivate. We nominally
1119284677Sdim	 * track the per-page activity counter and use it to locate
1120284677Sdim	 * deactivation candidates.
1121284677Sdim	 */
1122284677Sdim	pcount = cnt.v_active_count;
1123284677Sdim	m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
1124284677Sdim	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1125284677Sdim
1126284677Sdim	while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) {
1127284677Sdim
1128284677Sdim		KASSERT(m->queue == PQ_ACTIVE,
1129284677Sdim		    ("vm_pageout_scan: page %p isn't active", m));
1130284677Sdim
1131284677Sdim		next = TAILQ_NEXT(m, pageq);
1132284677Sdim		if ((m->flags & PG_MARKER) != 0) {
1133284677Sdim			m = next;
1134284677Sdim			continue;
1135284677Sdim		}
1136284677Sdim		if (!vm_pageout_page_lock(m, &next)) {
1137284677Sdim			vm_page_unlock(m);
1138309124Sdim			m = next;
1139309124Sdim			continue;
1140309124Sdim		}
1141314564Sdim		object = m->object;
1142314564Sdim		if (!VM_OBJECT_TRYLOCK(object) &&
1143314564Sdim		    !vm_pageout_fallback_object_lock(m, &next)) {
1144321369Sdim			VM_OBJECT_UNLOCK(object);
1145309124Sdim			vm_page_unlock(m);
1146314564Sdim			m = next;
1147309124Sdim			continue;
1148314564Sdim		}
1149314564Sdim
1150309124Sdim		/*
1151314564Sdim		 * Don't deactivate pages that are busy.
1152321369Sdim		 */
1153314564Sdim		if ((m->busy != 0) ||
1154314564Sdim		    (m->oflags & VPO_BUSY) ||
1155309124Sdim		    (m->hold_count != 0)) {
1156309124Sdim			vm_page_unlock(m);
1157314564Sdim			VM_OBJECT_UNLOCK(object);
1158314564Sdim			vm_page_requeue(m);
1159314564Sdim			m = next;
1160314564Sdim			continue;
1161309124Sdim		}
1162314564Sdim
1163309124Sdim		/*
1164309124Sdim		 * The count for pagedaemon pages is done after checking the
1165314564Sdim		 * page for eligibility...
1166314564Sdim		 */
1167314564Sdim		cnt.v_pdpages++;
1168314564Sdim
1169309124Sdim		/*
1170314564Sdim		 * Check to see "how much" the page has been used.
1171314564Sdim		 */
1172314564Sdim		actcount = 0;
1173314564Sdim		if (object->ref_count != 0) {
1174314564Sdim			if (m->flags & PG_REFERENCED) {
1175314564Sdim				actcount += 1;
1176314564Sdim			}
1177327952Sdim			actcount += pmap_ts_referenced(m);
1178327952Sdim			if (actcount) {
1179327952Sdim				m->act_count += ACT_ADVANCE + actcount;
1180314564Sdim				if (m->act_count > ACT_MAX)
1181314564Sdim					m->act_count = ACT_MAX;
1182314564Sdim			}
1183314564Sdim		}
1184314564Sdim
1185309124Sdim		/*
1186309124Sdim		 * Since we have "tested" this bit, we need to clear it now.
1187314564Sdim		 */
1188309124Sdim		vm_page_flag_clear(m, PG_REFERENCED);
1189309124Sdim
1190309124Sdim		/*
1191314564Sdim		 * Only if an object is currently being used, do we use the
1192314564Sdim		 * page activation count stats.
1193309124Sdim		 */
1194309124Sdim		if (actcount && (object->ref_count != 0)) {
1195309124Sdim			vm_page_requeue(m);
1196314564Sdim		} else {
1197309124Sdim			m->act_count -= min(m->act_count, ACT_DECLINE);
1198309124Sdim			if (vm_pageout_algorithm ||
1199314564Sdim			    object->ref_count == 0 ||
1200309124Sdim			    m->act_count == 0) {
1201309124Sdim				page_shortage--;
1202309124Sdim				if (object->ref_count == 0) {
1203314564Sdim					KASSERT(!pmap_page_is_mapped(m),
1204314564Sdim				    ("vm_pageout_scan: page %p is mapped", m));
1205314564Sdim					if (m->dirty == 0)
1206314564Sdim						vm_page_cache(m);
1207314564Sdim					else
1208314564Sdim						vm_page_deactivate(m);
1209314564Sdim				} else {
1210314564Sdim					vm_page_deactivate(m);
1211309124Sdim				}
1212309124Sdim			} else {
1213314564Sdim				vm_page_requeue(m);
1214309124Sdim			}
1215314564Sdim		}
1216314564Sdim		vm_page_unlock(m);
1217314564Sdim		VM_OBJECT_UNLOCK(object);
1218327952Sdim		m = next;
1219314564Sdim	}
1220314564Sdim	vm_page_unlock_queues();
1221314564Sdim#if !defined(NO_SWAPPING)
1222314564Sdim	/*
1223314564Sdim	 * Idle process swapout -- run once per second.
1224314564Sdim	 */
1225314564Sdim	if (vm_swap_idle_enabled) {
1226314564Sdim		static long lsec;
1227309124Sdim		if (time_second != lsec) {
1228309124Sdim			vm_req_vmdaemon(VM_SWAP_IDLE);
1229284677Sdim			lsec = time_second;
1230284677Sdim		}
1231309124Sdim	}
1232314564Sdim#endif
1233314564Sdim
1234314564Sdim	/*
1235309124Sdim	 * If we didn't get enough free pages, and we have skipped a vnode
1236284677Sdim	 * in a writeable object, wakeup the sync daemon.  And kick swapout
1237314564Sdim	 * if we did not get enough free pages.
1238314564Sdim	 */
1239314564Sdim	if (vm_paging_target() > 0) {
1240314564Sdim		if (vnodes_skipped && vm_page_count_min())
1241314564Sdim			(void) speedup_syncer();
1242314564Sdim#if !defined(NO_SWAPPING)
1243314564Sdim		if (vm_swap_enabled && vm_page_count_target())
1244321369Sdim			vm_req_vmdaemon(VM_SWAP_NORMAL);
1245314564Sdim#endif
1246321369Sdim	}
1247321369Sdim
1248314564Sdim	/*
1249314564Sdim	 * If we are critically low on one of RAM or swap and low on
1250314564Sdim	 * the other, kill the largest process.  However, we avoid
1251314564Sdim	 * doing this on the first pass in order to give ourselves a
1252314564Sdim	 * chance to flush out dirty vnode-backed pages and to allow
1253314564Sdim	 * active pages to be moved to the inactive queue and reclaimed.
1254314564Sdim	 */
1255314564Sdim	if (pass != 0 &&
1256314564Sdim	    ((swap_pager_avail < 64 && vm_page_count_min()) ||
1257314564Sdim	     (swap_pager_full && vm_paging_target() > 0)))
1258314564Sdim		vm_pageout_oom(VM_OOM_MEM);
1259284677Sdim}
1260284677Sdim
1261314564Sdim
1262314564Sdimvoid
1263314564Sdimvm_pageout_oom(int shortage)
1264314564Sdim{
1265314564Sdim	struct proc *p, *bigproc;
1266309124Sdim	vm_offset_t size, bigsize;
1267314564Sdim	struct thread *td;
1268314564Sdim	struct vmspace *vm;
1269314564Sdim
1270321369Sdim	/*
1271314564Sdim	 * We keep the process bigproc locked once we find it to keep anyone
1272314564Sdim	 * from messing with it; however, there is a possibility of
1273284677Sdim	 * deadlock if process B is bigproc and one of it's child processes
1274284677Sdim	 * attempts to propagate a signal to B while we are waiting for A's
1275284677Sdim	 * lock while walking this list.  To avoid this, we don't block on
1276284677Sdim	 * the process lock but just skip a process if it is already locked.
1277284677Sdim	 */
1278284677Sdim	bigproc = NULL;
1279284677Sdim	bigsize = 0;
1280314564Sdim	sx_slock(&allproc_lock);
1281284677Sdim	FOREACH_PROC_IN_SYSTEM(p) {
1282284677Sdim		int breakout;
1283314564Sdim
1284314564Sdim		if (PROC_TRYLOCK(p) == 0)
1285314564Sdim			continue;
1286314564Sdim		/*
1287314564Sdim		 * If this is a system, protected or killed process, skip it.
1288314564Sdim		 */
1289314564Sdim		if ((p->p_flag & (P_INEXEC | P_PROTECTED | P_SYSTEM)) ||
1290314564Sdim		    (p->p_pid == 1) || P_KILLED(p) ||
1291314564Sdim		    ((p->p_pid < 48) && (swap_pager_avail != 0))) {
1292314564Sdim			PROC_UNLOCK(p);
1293284677Sdim			continue;
1294314564Sdim		}
1295314564Sdim		/*
1296284677Sdim		 * If the process is in a non-running type state,
1297284677Sdim		 * don't touch it.  Check all the threads individually.
1298284677Sdim		 */
1299284677Sdim		breakout = 0;
1300284677Sdim		FOREACH_THREAD_IN_PROC(p, td) {
1301284677Sdim			thread_lock(td);
1302284677Sdim			if (!TD_ON_RUNQ(td) &&
1303284677Sdim			    !TD_IS_RUNNING(td) &&
1304309124Sdim			    !TD_IS_SLEEPING(td)) {
1305284677Sdim				thread_unlock(td);
1306284677Sdim				breakout = 1;
1307284677Sdim				break;
1308284677Sdim			}
1309314564Sdim			thread_unlock(td);
1310284677Sdim		}
1311314564Sdim		if (breakout) {
1312284677Sdim			PROC_UNLOCK(p);
1313284677Sdim			continue;
1314284677Sdim		}
1315284677Sdim		/*
1316284677Sdim		 * get the process size
1317284677Sdim		 */
1318284677Sdim		vm = vmspace_acquire_ref(p);
1319284677Sdim		if (vm == NULL) {
1320284677Sdim			PROC_UNLOCK(p);
1321284677Sdim			continue;
1322314564Sdim		}
1323321369Sdim		if (!vm_map_trylock_read(&vm->vm_map)) {
1324284677Sdim			vmspace_free(vm);
1325284677Sdim			PROC_UNLOCK(p);
1326309124Sdim			continue;
1327309124Sdim		}
1328309124Sdim		size = vmspace_swap_count(vm);
1329314564Sdim		vm_map_unlock_read(&vm->vm_map);
1330314564Sdim		if (shortage == VM_OOM_MEM)
1331314564Sdim			size += vmspace_resident_count(vm);
1332314564Sdim		vmspace_free(vm);
1333314564Sdim		/*
1334284677Sdim		 * if the this process is bigger than the biggest one
1335284677Sdim		 * remember it.
1336314564Sdim		 */
1337314564Sdim		if (size > bigsize) {
1338284677Sdim			if (bigproc != NULL)
1339284677Sdim				PROC_UNLOCK(bigproc);
1340284677Sdim			bigproc = p;
1341284677Sdim			bigsize = size;
1342327952Sdim		} else
1343284677Sdim			PROC_UNLOCK(p);
1344327952Sdim	}
1345284677Sdim	sx_sunlock(&allproc_lock);
1346327952Sdim	if (bigproc != NULL) {
1347284677Sdim		killproc(bigproc, "out of swap space");
1348327952Sdim		sched_nice(bigproc, PRIO_MIN);
1349284677Sdim		PROC_UNLOCK(bigproc);
1350327952Sdim		wakeup(&cnt.v_free_count);
1351284677Sdim	}
1352327952Sdim}
1353284677Sdim
1354327952Sdim/*
1355284677Sdim * This routine tries to maintain the pseudo LRU active queue,
1356327952Sdim * so that during long periods of time where there is no paging,
1357284677Sdim * that some statistic accumulation still occurs.  This code
1358327952Sdim * helps the situation where paging just starts to occur.
1359284677Sdim */
1360327952Sdimstatic void
1361284677Sdimvm_pageout_page_stats()
1362327952Sdim{
1363284677Sdim	vm_object_t object;
1364327952Sdim	vm_page_t m,next;
1365284677Sdim	int pcount,tpcount;		/* Number of pages to check */
1366327952Sdim	static int fullintervalcount = 0;
1367284677Sdim	int page_shortage;
1368327952Sdim
1369284677Sdim	page_shortage =
1370327952Sdim	    (cnt.v_inactive_target + cnt.v_cache_max + cnt.v_free_min) -
1371284677Sdim	    (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count);
1372327952Sdim
1373284677Sdim	if (page_shortage <= 0)
1374327952Sdim		return;
1375284677Sdim
1376284677Sdim	vm_page_lock_queues();
1377284677Sdim	pcount = cnt.v_active_count;
1378284677Sdim	fullintervalcount += vm_pageout_stats_interval;
1379284677Sdim	if (fullintervalcount < vm_pageout_full_stats_interval) {
1380284677Sdim		tpcount = (int64_t)vm_pageout_stats_max * cnt.v_active_count /
1381309124Sdim		    cnt.v_page_count;
1382309124Sdim		if (pcount > tpcount)
1383284677Sdim			pcount = tpcount;
1384309124Sdim	} else {
1385309124Sdim		fullintervalcount = 0;
1386309124Sdim	}
1387314564Sdim
1388284677Sdim	m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
1389314564Sdim	while ((m != NULL) && (pcount-- > 0)) {
1390314564Sdim		int actcount;
1391314564Sdim
1392284677Sdim		KASSERT(m->queue == PQ_ACTIVE,
1393314564Sdim		    ("vm_pageout_page_stats: page %p isn't active", m));
1394314564Sdim
1395314564Sdim		next = TAILQ_NEXT(m, pageq);
1396314564Sdim		if ((m->flags & PG_MARKER) != 0) {
1397284677Sdim			m = next;
1398314564Sdim			continue;
1399314564Sdim		}
1400314564Sdim		vm_page_lock_assert(m, MA_NOTOWNED);
1401314564Sdim		if (!vm_pageout_page_lock(m, &next)) {
1402314564Sdim			vm_page_unlock(m);
1403314564Sdim			m = next;
1404314564Sdim			continue;
1405327952Sdim		}
1406327952Sdim		object = m->object;
1407327952Sdim		if (!VM_OBJECT_TRYLOCK(object) &&
1408314564Sdim		    !vm_pageout_fallback_object_lock(m, &next)) {
1409309124Sdim			VM_OBJECT_UNLOCK(object);
1410309124Sdim			vm_page_unlock(m);
1411314564Sdim			m = next;
1412309124Sdim			continue;
1413309124Sdim		}
1414309124Sdim
1415309124Sdim		/*
1416309124Sdim		 * Don't deactivate pages that are busy.
1417309124Sdim		 */
1418314564Sdim		if ((m->busy != 0) ||
1419309124Sdim		    (m->oflags & VPO_BUSY) ||
1420309124Sdim		    (m->hold_count != 0)) {
1421309124Sdim			vm_page_unlock(m);
1422309124Sdim			VM_OBJECT_UNLOCK(object);
1423314564Sdim			vm_page_requeue(m);
1424309124Sdim			m = next;
1425314564Sdim			continue;
1426314564Sdim		}
1427314564Sdim
1428284677Sdim		actcount = 0;
1429284677Sdim		if (m->flags & PG_REFERENCED) {
1430309124Sdim			vm_page_flag_clear(m, PG_REFERENCED);
1431314564Sdim			actcount += 1;
1432314564Sdim		}
1433309124Sdim
1434309124Sdim		actcount += pmap_ts_referenced(m);
1435309124Sdim		if (actcount) {
1436309124Sdim			m->act_count += ACT_ADVANCE + actcount;
1437309124Sdim			if (m->act_count > ACT_MAX)
1438309124Sdim				m->act_count = ACT_MAX;
1439309124Sdim			vm_page_requeue(m);
1440309124Sdim		} else {
1441309124Sdim			if (m->act_count == 0) {
1442309124Sdim				/*
1443309124Sdim				 * We turn off page access, so that we have
1444321369Sdim				 * more accurate RSS stats.  We don't do this
1445309124Sdim				 * in the normal page deactivation when the
1446309124Sdim				 * system is loaded VM wise, because the
1447309124Sdim				 * cost of the large number of page protect
1448309124Sdim				 * operations would be higher than the value
1449309124Sdim				 * of doing the operation.
1450309124Sdim				 */
1451309124Sdim				pmap_remove_all(m);
1452309124Sdim				vm_page_deactivate(m);
1453309124Sdim			} else {
1454321369Sdim				m->act_count -= min(m->act_count, ACT_DECLINE);
1455321369Sdim				vm_page_requeue(m);
1456314564Sdim			}
1457314564Sdim		}
1458284677Sdim		vm_page_unlock(m);
1459284677Sdim		VM_OBJECT_UNLOCK(object);
1460341825Sdim		m = next;
1461327952Sdim	}
1462284677Sdim	vm_page_unlock_queues();
1463284677Sdim}
1464284677Sdim
1465284677Sdim/*
1466341825Sdim *	vm_pageout is the high level pageout daemon.
1467284677Sdim */
1468341825Sdimstatic void
1469284677Sdimvm_pageout()
1470341825Sdim{
1471284677Sdim	int error, pass;
1472284677Sdim
1473284677Sdim	/*
1474284677Sdim	 * Initialize some paging parameters.
1475284677Sdim	 */
1476321369Sdim	cnt.v_interrupt_free_min = 2;
1477284677Sdim	if (cnt.v_page_count < 2000)
1478284677Sdim		vm_pageout_page_count = 8;
1479284677Sdim
1480284677Sdim	/*
1481284677Sdim	 * v_free_reserved needs to include enough for the largest
1482284677Sdim	 * swap pager structures plus enough for any pv_entry structs
1483284677Sdim	 * when paging.
1484284677Sdim	 */
1485284677Sdim	if (cnt.v_page_count > 1024)
1486284677Sdim		cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200;
1487284677Sdim	else
1488284677Sdim		cnt.v_free_min = 4;
1489284677Sdim	cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE +
1490284677Sdim	    cnt.v_interrupt_free_min;
1491284677Sdim	cnt.v_free_reserved = vm_pageout_page_count +
1492284677Sdim	    cnt.v_pageout_free_min + (cnt.v_page_count / 768);
1493284677Sdim	cnt.v_free_severe = cnt.v_free_min / 2;
1494284677Sdim	cnt.v_free_min += cnt.v_free_reserved;
1495284677Sdim	cnt.v_free_severe += cnt.v_free_reserved;
1496284677Sdim
1497284677Sdim	/*
1498284677Sdim	 * v_free_target and v_cache_min control pageout hysteresis.  Note
1499284677Sdim	 * that these are more a measure of the VM cache queue hysteresis
1500284677Sdim	 * then the VM free queue.  Specifically, v_free_target is the
1501284677Sdim	 * high water mark (free+cache pages).
1502309124Sdim	 *
1503309124Sdim	 * v_free_reserved + v_cache_min (mostly means v_cache_min) is the
1504309124Sdim	 * low water mark, while v_free_min is the stop.  v_cache_min must
1505284677Sdim	 * be big enough to handle memory needs while the pageout daemon
1506284677Sdim	 * is signalled and run to free more pages.
1507284677Sdim	 */
1508284677Sdim	if (cnt.v_free_count > 6144)
1509284677Sdim		cnt.v_free_target = 4 * cnt.v_free_min + cnt.v_free_reserved;
1510284677Sdim	else
1511284677Sdim		cnt.v_free_target = 2 * cnt.v_free_min + cnt.v_free_reserved;
1512321369Sdim
1513284677Sdim	if (cnt.v_free_count > 2048) {
1514284677Sdim		cnt.v_cache_min = cnt.v_free_target;
1515284677Sdim		cnt.v_cache_max = 2 * cnt.v_cache_min;
1516314564Sdim		cnt.v_inactive_target = (3 * cnt.v_free_target) / 2;
1517314564Sdim	} else {
1518314564Sdim		cnt.v_cache_min = 0;
1519314564Sdim		cnt.v_cache_max = 0;
1520314564Sdim		cnt.v_inactive_target = cnt.v_free_count / 4;
1521314564Sdim	}
1522284677Sdim	if (cnt.v_inactive_target > cnt.v_free_count / 3)
1523314564Sdim		cnt.v_inactive_target = cnt.v_free_count / 3;
1524284677Sdim
1525284677Sdim	/* XXX does not really belong here */
1526284677Sdim	if (vm_page_max_wired == 0)
1527284677Sdim		vm_page_max_wired = cnt.v_free_count / 3;
1528284677Sdim
1529284677Sdim	if (vm_pageout_stats_max == 0)
1530284677Sdim		vm_pageout_stats_max = cnt.v_free_target;
1531284677Sdim
1532284677Sdim	/*
1533284677Sdim	 * Set interval in seconds for stats scan.
1534284677Sdim	 */
1535309124Sdim	if (vm_pageout_stats_interval == 0)
1536309124Sdim		vm_pageout_stats_interval = 5;
1537309124Sdim	if (vm_pageout_full_stats_interval == 0)
1538341825Sdim		vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4;
1539309124Sdim
1540309124Sdim	swap_pager_swap_init();
1541309124Sdim	pass = 0;
1542309124Sdim	/*
1543309124Sdim	 * The pageout daemon is never done, so loop forever.
1544309124Sdim	 */
1545309124Sdim	while (TRUE) {
1546309124Sdim		/*
1547309124Sdim		 * If we have enough free memory, wakeup waiters.  Do
1548309124Sdim		 * not clear vm_pages_needed until we reach our target,
1549309124Sdim		 * otherwise we may be woken up over and over again and
1550341825Sdim		 * waste a lot of cpu.
1551341825Sdim		 */
1552341825Sdim		mtx_lock(&vm_page_queue_free_mtx);
1553341825Sdim		if (vm_pages_needed && !vm_page_count_min()) {
1554341825Sdim			if (!vm_paging_needed())
1555341825Sdim				vm_pages_needed = 0;
1556341825Sdim			wakeup(&cnt.v_free_count);
1557341825Sdim		}
1558341825Sdim		if (vm_pages_needed) {
1559341825Sdim			/*
1560341825Sdim			 * Still not done, take a second pass without waiting
1561341825Sdim			 * (unlimited dirty cleaning), otherwise sleep a bit
1562341825Sdim			 * and try again.
1563341825Sdim			 */
1564341825Sdim			++pass;
1565341825Sdim			if (pass > 1)
1566341825Sdim				msleep(&vm_pages_needed,
1567341825Sdim				    &vm_page_queue_free_mtx, PVM, "psleep",
1568341825Sdim				    hz / 2);
1569341825Sdim		} else {
1570341825Sdim			/*
1571341825Sdim			 * Good enough, sleep & handle stats.  Prime the pass
1572284677Sdim			 * for the next run.
1573284677Sdim			 */
1574284677Sdim			if (pass > 1)
1575284677Sdim				pass = 1;
1576309124Sdim			else
1577309124Sdim				pass = 0;
1578309124Sdim			error = msleep(&vm_pages_needed,
1579284677Sdim			    &vm_page_queue_free_mtx, PVM, "psleep",
1580284677Sdim			    vm_pageout_stats_interval * hz);
1581284677Sdim			if (error && !vm_pages_needed) {
1582284677Sdim				mtx_unlock(&vm_page_queue_free_mtx);
1583284677Sdim				pass = 0;
1584284677Sdim				vm_pageout_page_stats();
1585314564Sdim				continue;
1586321369Sdim			}
1587314564Sdim		}
1588314564Sdim		if (vm_pages_needed)
1589314564Sdim			cnt.v_pdwakeups++;
1590284677Sdim		mtx_unlock(&vm_page_queue_free_mtx);
1591284677Sdim		vm_pageout_scan(pass);
1592284677Sdim	}
1593284677Sdim}
1594284677Sdim
1595284677Sdim/*
1596284677Sdim * Unless the free page queue lock is held by the caller, this function
1597284677Sdim * should be regarded as advisory.  Specifically, the caller should
1598284677Sdim * not msleep() on &cnt.v_free_count following this function unless
1599284677Sdim * the free page queue lock is held until the msleep() is performed.
1600284677Sdim */
1601309124Sdimvoid
1602341825Sdimpagedaemon_wakeup()
1603284677Sdim{
1604284677Sdim
1605284677Sdim	if (!vm_pages_needed && curthread->td_proc != pageproc) {
1606284677Sdim		vm_pages_needed = 1;
1607284677Sdim		wakeup(&vm_pages_needed);
1608284677Sdim	}
1609341825Sdim}
1610284677Sdim
1611284677Sdim#if !defined(NO_SWAPPING)
1612284677Sdimstatic void
1613284677Sdimvm_req_vmdaemon(int req)
1614284677Sdim{
1615284677Sdim	static int lastrun = 0;
1616284677Sdim
1617284677Sdim	mtx_lock(&vm_daemon_mtx);
1618284677Sdim	vm_pageout_req_swapout |= req;
1619284677Sdim	if ((ticks > (lastrun + hz)) || (ticks < lastrun)) {
1620284677Sdim		wakeup(&vm_daemon_needed);
1621284677Sdim		lastrun = ticks;
1622284677Sdim	}
1623284677Sdim	mtx_unlock(&vm_daemon_mtx);
1624284677Sdim}
1625284677Sdim
1626284677Sdimstatic void
1627284677Sdimvm_daemon()
1628284677Sdim{
1629284677Sdim	struct rlimit rsslim;
1630284677Sdim	struct proc *p;
1631284677Sdim	struct thread *td;
1632284677Sdim	struct vmspace *vm;
1633341825Sdim	int breakout, swapout_flags;
1634284677Sdim
1635284677Sdim	while (TRUE) {
1636309124Sdim		mtx_lock(&vm_daemon_mtx);
1637309124Sdim		msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", 0);
1638341825Sdim		swapout_flags = vm_pageout_req_swapout;
1639341825Sdim		vm_pageout_req_swapout = 0;
1640341825Sdim		mtx_unlock(&vm_daemon_mtx);
1641314564Sdim		if (swapout_flags)
1642314564Sdim			swapout_procs(swapout_flags);
1643284677Sdim
1644284677Sdim		/*
1645284677Sdim		 * scan the processes for exceeding their rlimits or if
1646284677Sdim		 * process is swapped out -- deactivate pages
1647284677Sdim		 */
1648284677Sdim		sx_slock(&allproc_lock);
1649286684Sdim		FOREACH_PROC_IN_SYSTEM(p) {
1650286684Sdim			vm_pindex_t limit, size;
1651284677Sdim
1652284677Sdim			/*
1653284677Sdim			 * if this is a system process or if we have already
1654284677Sdim			 * looked at this process, skip it.
1655284677Sdim			 */
1656321369Sdim			PROC_LOCK(p);
1657321369Sdim			if (p->p_flag & (P_INEXEC | P_SYSTEM | P_WEXIT)) {
1658321369Sdim				PROC_UNLOCK(p);
1659321369Sdim				continue;
1660321369Sdim			}
1661321369Sdim			/*
1662321369Sdim			 * if the process is in a non-running type state,
1663321369Sdim			 * don't touch it.
1664321369Sdim			 */
1665309124Sdim			breakout = 0;
1666309124Sdim			FOREACH_THREAD_IN_PROC(p, td) {
1667309124Sdim				thread_lock(td);
1668309124Sdim				if (!TD_ON_RUNQ(td) &&
1669309124Sdim				    !TD_IS_RUNNING(td) &&
1670309124Sdim				    !TD_IS_SLEEPING(td)) {
1671309124Sdim					thread_unlock(td);
1672309124Sdim					breakout = 1;
1673309124Sdim					break;
1674309124Sdim				}
1675309124Sdim				thread_unlock(td);
1676309124Sdim			}
1677309124Sdim			if (breakout) {
1678309124Sdim				PROC_UNLOCK(p);
1679309124Sdim				continue;
1680309124Sdim			}
1681309124Sdim			/*
1682309124Sdim			 * get a limit
1683309124Sdim			 */
1684309124Sdim			lim_rlimit(p, RLIMIT_RSS, &rsslim);
1685284677Sdim			limit = OFF_TO_IDX(
1686284677Sdim			    qmin(rsslim.rlim_cur, rsslim.rlim_max));
1687284677Sdim
1688284677Sdim			/*
1689284677Sdim			 * let processes that are swapped out really be
1690284677Sdim			 * swapped out set the limit to nothing (will force a
1691284677Sdim			 * swap-out.)
1692284677Sdim			 */
1693284677Sdim			if ((p->p_flag & P_INMEM) == 0)
1694284677Sdim				limit = 0;	/* XXX */
1695284677Sdim			vm = vmspace_acquire_ref(p);
1696284677Sdim			PROC_UNLOCK(p);
1697284677Sdim			if (vm == NULL)
1698309124Sdim				continue;
1699284677Sdim
1700284677Sdim			size = vmspace_resident_count(vm);
1701284677Sdim			if (limit >= 0 && size >= limit) {
1702284677Sdim				vm_pageout_map_deactivate_pages(
1703284677Sdim				    &vm->vm_map, limit);
1704284677Sdim			}
1705284677Sdim			vmspace_free(vm);
1706284677Sdim		}
1707284677Sdim		sx_sunlock(&allproc_lock);
1708284677Sdim	}
1709284677Sdim}
1710284677Sdim#endif			/* !defined(NO_SWAPPING) */
1711284677Sdim