xref: /freebsd/sys/vm/vm_pageout.c (revision 5cd29d0f3cdad565df4c35e3f45825009db26003)
160727d8bSWarner Losh /*-
2796df753SPedro F. Giffuni  * SPDX-License-Identifier: (BSD-4-Clause AND MIT-CMU)
3df57947fSPedro F. Giffuni  *
426f9a767SRodney W. Grimes  * Copyright (c) 1991 Regents of the University of California.
526f9a767SRodney W. Grimes  * All rights reserved.
626f9a767SRodney W. Grimes  * Copyright (c) 1994 John S. Dyson
726f9a767SRodney W. Grimes  * All rights reserved.
826f9a767SRodney W. Grimes  * Copyright (c) 1994 David Greenman
926f9a767SRodney W. Grimes  * All rights reserved.
108dbca793STor Egge  * Copyright (c) 2005 Yahoo! Technologies Norway AS
118dbca793STor Egge  * All rights reserved.
12df8bae1dSRodney W. Grimes  *
13df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
14df8bae1dSRodney W. Grimes  * The Mach Operating System project at Carnegie-Mellon University.
15df8bae1dSRodney W. Grimes  *
16df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
17df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
18df8bae1dSRodney W. Grimes  * are met:
19df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
20df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
21df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
22df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
23df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
24df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
255929bcfaSPhilippe Charnier  *    must display the following acknowledgement:
26df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
27df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
28df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
29df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
30df8bae1dSRodney W. Grimes  *    without specific prior written permission.
31df8bae1dSRodney W. Grimes  *
32df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
43df8bae1dSRodney W. Grimes  *
443c4dd356SDavid Greenman  *	from: @(#)vm_pageout.c	7.4 (Berkeley) 5/7/91
45df8bae1dSRodney W. Grimes  *
46df8bae1dSRodney W. Grimes  *
47df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
48df8bae1dSRodney W. Grimes  * All rights reserved.
49df8bae1dSRodney W. Grimes  *
50df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
51df8bae1dSRodney W. Grimes  *
52df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
53df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
54df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
55df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
56df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
57df8bae1dSRodney W. Grimes  *
58df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
59df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
60df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
61df8bae1dSRodney W. Grimes  *
62df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
63df8bae1dSRodney W. Grimes  *
64df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
65df8bae1dSRodney W. Grimes  *  School of Computer Science
66df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
67df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
68df8bae1dSRodney W. Grimes  *
69df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
70df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
71df8bae1dSRodney W. Grimes  */
72df8bae1dSRodney W. Grimes 
73df8bae1dSRodney W. Grimes /*
74df8bae1dSRodney W. Grimes  *	The proverbial page-out daemon.
75df8bae1dSRodney W. Grimes  */
76df8bae1dSRodney W. Grimes 
77874651b1SDavid E. O'Brien #include <sys/cdefs.h>
78874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
79874651b1SDavid E. O'Brien 
80faa5f8d8SAndrzej Bialecki #include "opt_vm.h"
817672ca05SMark Johnston 
82df8bae1dSRodney W. Grimes #include <sys/param.h>
8326f9a767SRodney W. Grimes #include <sys/systm.h>
84b5e8ce9fSBruce Evans #include <sys/kernel.h>
85855a310fSJeff Roberson #include <sys/eventhandler.h>
86fb919e4dSMark Murray #include <sys/lock.h>
87fb919e4dSMark Murray #include <sys/mutex.h>
8826f9a767SRodney W. Grimes #include <sys/proc.h>
899c8b8baaSPeter Wemm #include <sys/kthread.h>
900384fff8SJason Evans #include <sys/ktr.h>
9197824da3SAlan Cox #include <sys/mount.h>
92099e7e95SEdward Tomasz Napierala #include <sys/racct.h>
9326f9a767SRodney W. Grimes #include <sys/resourcevar.h>
94b43179fbSJeff Roberson #include <sys/sched.h>
9514a0d74eSSteven Hartland #include <sys/sdt.h>
96d2fc5315SPoul-Henning Kamp #include <sys/signalvar.h>
97449c2e92SKonstantin Belousov #include <sys/smp.h>
98a6bf3a9eSRyan Stone #include <sys/time.h>
99f6b04d2bSDavid Greenman #include <sys/vnode.h>
100efeaf95aSDavid Greenman #include <sys/vmmeter.h>
10189f6b863SAttilio Rao #include <sys/rwlock.h>
1021005a129SJohn Baldwin #include <sys/sx.h>
10338efa82bSJohn Dyson #include <sys/sysctl.h>
104df8bae1dSRodney W. Grimes 
105df8bae1dSRodney W. Grimes #include <vm/vm.h>
106efeaf95aSDavid Greenman #include <vm/vm_param.h>
107efeaf95aSDavid Greenman #include <vm/vm_object.h>
108df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
109efeaf95aSDavid Greenman #include <vm/vm_map.h>
110df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
11124a1cce3SDavid Greenman #include <vm/vm_pager.h>
112449c2e92SKonstantin Belousov #include <vm/vm_phys.h>
113e2068d0bSJeff Roberson #include <vm/vm_pagequeue.h>
11405f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h>
115efeaf95aSDavid Greenman #include <vm/vm_extern.h>
116670d17b5SJeff Roberson #include <vm/uma.h>
117df8bae1dSRodney W. Grimes 
1182b14f991SJulian Elischer /*
1192b14f991SJulian Elischer  * System initialization
1202b14f991SJulian Elischer  */
1212b14f991SJulian Elischer 
1222b14f991SJulian Elischer /* the kernel process "vm_pageout"*/
12311caded3SAlfred Perlstein static void vm_pageout(void);
1244d19f4adSSteven Hartland static void vm_pageout_init(void);
125ebcddc72SAlan Cox static int vm_pageout_clean(vm_page_t m, int *numpagedout);
12634d8b7eaSJeff Roberson static int vm_pageout_cluster(vm_page_t m);
1275f8cd1c0SJeff Roberson static bool vm_pageout_scan(struct vm_domain *vmd, int pass, int shortage);
12876386c7eSKonstantin Belousov static void vm_pageout_mightbe_oom(struct vm_domain *vmd, int page_shortage,
12976386c7eSKonstantin Belousov     int starting_page_shortage);
13045ae1d91SAlan Cox 
1314d19f4adSSteven Hartland SYSINIT(pagedaemon_init, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, vm_pageout_init,
1324d19f4adSSteven Hartland     NULL);
1334d19f4adSSteven Hartland 
1342b14f991SJulian Elischer struct proc *pageproc;
1352b14f991SJulian Elischer 
1362b14f991SJulian Elischer static struct kproc_desc page_kp = {
1372b14f991SJulian Elischer 	"pagedaemon",
1382b14f991SJulian Elischer 	vm_pageout,
1392b14f991SJulian Elischer 	&pageproc
1402b14f991SJulian Elischer };
1414d19f4adSSteven Hartland SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_SECOND, kproc_start,
142237fdd78SRobert Watson     &page_kp);
1432b14f991SJulian Elischer 
14414a0d74eSSteven Hartland SDT_PROVIDER_DEFINE(vm);
14514a0d74eSSteven Hartland SDT_PROBE_DEFINE(vm, , , vm__lowmem_scan);
14614a0d74eSSteven Hartland 
147ebcddc72SAlan Cox /* Pagedaemon activity rates, in subdivisions of one second. */
148ebcddc72SAlan Cox #define	VM_LAUNDER_RATE		10
1495f8cd1c0SJeff Roberson #define	VM_INACT_SCAN_RATE	10
1502b14f991SJulian Elischer 
15176386c7eSKonstantin Belousov static int vm_pageout_oom_seq = 12;
152ebcddc72SAlan Cox 
153d9e23210SJeff Roberson static int vm_pageout_update_period;
1544a365329SAndrey Zonov static int disable_swap_pageouts;
155c9612b2dSJeff Roberson static int lowmem_period = 10;
156a6bf3a9eSRyan Stone static time_t lowmem_uptime;
157b1fd102eSMark Johnston static int swapdev_enabled;
15870111b90SJohn Dyson 
1598311a2b8SWill Andrews static int vm_panic_on_oom = 0;
1608311a2b8SWill Andrews 
1618311a2b8SWill Andrews SYSCTL_INT(_vm, OID_AUTO, panic_on_oom,
1628311a2b8SWill Andrews 	CTLFLAG_RWTUN, &vm_panic_on_oom, 0,
1638311a2b8SWill Andrews 	"panic on out of memory instead of killing the largest process");
1648311a2b8SWill Andrews 
165d9e23210SJeff Roberson SYSCTL_INT(_vm, OID_AUTO, pageout_update_period,
166e0b2fc3aSMark Johnston 	CTLFLAG_RWTUN, &vm_pageout_update_period, 0,
167d9e23210SJeff Roberson 	"Maximum active LRU update period");
16853636869SAndrey Zonov 
169e0b2fc3aSMark Johnston SYSCTL_INT(_vm, OID_AUTO, lowmem_period, CTLFLAG_RWTUN, &lowmem_period, 0,
170c9612b2dSJeff Roberson 	"Low memory callback period");
171c9612b2dSJeff Roberson 
172ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts,
173e0b2fc3aSMark Johnston 	CTLFLAG_RWTUN, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages");
17412ac6a1dSJohn Dyson 
17523b59018SMatthew Dillon static int pageout_lock_miss;
17623b59018SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss,
17723b59018SMatthew Dillon 	CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout");
17823b59018SMatthew Dillon 
17976386c7eSKonstantin Belousov SYSCTL_INT(_vm, OID_AUTO, pageout_oom_seq,
180e0b2fc3aSMark Johnston 	CTLFLAG_RWTUN, &vm_pageout_oom_seq, 0,
18176386c7eSKonstantin Belousov 	"back-to-back calls to oom detector to start OOM");
18276386c7eSKonstantin Belousov 
183ebcddc72SAlan Cox static int act_scan_laundry_weight = 3;
184e0b2fc3aSMark Johnston SYSCTL_INT(_vm, OID_AUTO, act_scan_laundry_weight, CTLFLAG_RWTUN,
185ebcddc72SAlan Cox     &act_scan_laundry_weight, 0,
186ebcddc72SAlan Cox     "weight given to clean vs. dirty pages in active queue scans");
187ebcddc72SAlan Cox 
188ebcddc72SAlan Cox static u_int vm_background_launder_rate = 4096;
189e0b2fc3aSMark Johnston SYSCTL_UINT(_vm, OID_AUTO, background_launder_rate, CTLFLAG_RWTUN,
190ebcddc72SAlan Cox     &vm_background_launder_rate, 0,
191ebcddc72SAlan Cox     "background laundering rate, in kilobytes per second");
192ebcddc72SAlan Cox 
193ebcddc72SAlan Cox static u_int vm_background_launder_max = 20 * 1024;
194e0b2fc3aSMark Johnston SYSCTL_UINT(_vm, OID_AUTO, background_launder_max, CTLFLAG_RWTUN,
195ebcddc72SAlan Cox     &vm_background_launder_max, 0, "background laundering cap, in kilobytes");
196ebcddc72SAlan Cox 
197e2241590SAlan Cox int vm_pageout_page_count = 32;
198df8bae1dSRodney W. Grimes 
199c3cb3e12SDavid Greenman int vm_page_max_wired;		/* XXX max # of wired pages system-wide */
2005dfc2870SAlan Cox SYSCTL_INT(_vm, OID_AUTO, max_wired,
2015dfc2870SAlan Cox 	CTLFLAG_RW, &vm_page_max_wired, 0, "System-wide limit to wired page count");
202df8bae1dSRodney W. Grimes 
203ebcddc72SAlan Cox static u_int isqrt(u_int num);
204ebcddc72SAlan Cox static int vm_pageout_launder(struct vm_domain *vmd, int launder,
205ebcddc72SAlan Cox     bool in_shortfall);
206ebcddc72SAlan Cox static void vm_pageout_laundry_worker(void *arg);
207cd41fc12SDavid Greenman 
208*5cd29d0fSMark Johnston struct scan_state {
209*5cd29d0fSMark Johnston 	struct vm_batchqueue bq;
2108d220203SAlan Cox 	struct vm_pagequeue *pq;
211*5cd29d0fSMark Johnston 	vm_page_t	marker;
212*5cd29d0fSMark Johnston 	int		maxscan;
213*5cd29d0fSMark Johnston 	int		scanned;
214*5cd29d0fSMark Johnston };
2158dbca793STor Egge 
216*5cd29d0fSMark Johnston static void
217*5cd29d0fSMark Johnston vm_pageout_init_scan(struct scan_state *ss, struct vm_pagequeue *pq,
218*5cd29d0fSMark Johnston     vm_page_t marker, vm_page_t after, int maxscan)
219*5cd29d0fSMark Johnston {
2208dbca793STor Egge 
221*5cd29d0fSMark Johnston 	vm_pagequeue_assert_locked(pq);
222*5cd29d0fSMark Johnston 	KASSERT((marker->aflags & PGA_ENQUEUED) == 0,
223*5cd29d0fSMark Johnston 	    ("marker %p already enqueued", marker));
224*5cd29d0fSMark Johnston 
225*5cd29d0fSMark Johnston 	if (after == NULL)
226*5cd29d0fSMark Johnston 		TAILQ_INSERT_HEAD(&pq->pq_pl, marker, plinks.q);
227*5cd29d0fSMark Johnston 	else
228*5cd29d0fSMark Johnston 		TAILQ_INSERT_AFTER(&pq->pq_pl, after, marker, plinks.q);
229*5cd29d0fSMark Johnston 	vm_page_aflag_set(marker, PGA_ENQUEUED);
230*5cd29d0fSMark Johnston 
231*5cd29d0fSMark Johnston 	vm_batchqueue_init(&ss->bq);
232*5cd29d0fSMark Johnston 	ss->pq = pq;
233*5cd29d0fSMark Johnston 	ss->marker = marker;
234*5cd29d0fSMark Johnston 	ss->maxscan = maxscan;
235*5cd29d0fSMark Johnston 	ss->scanned = 0;
2368d220203SAlan Cox 	vm_pagequeue_unlock(pq);
237*5cd29d0fSMark Johnston }
2388dbca793STor Egge 
239*5cd29d0fSMark Johnston static void
240*5cd29d0fSMark Johnston vm_pageout_end_scan(struct scan_state *ss)
241*5cd29d0fSMark Johnston {
242*5cd29d0fSMark Johnston 	struct vm_pagequeue *pq;
243*5cd29d0fSMark Johnston 
244*5cd29d0fSMark Johnston 	pq = ss->pq;
245*5cd29d0fSMark Johnston 	vm_pagequeue_assert_locked(pq);
246*5cd29d0fSMark Johnston 	KASSERT((ss->marker->aflags & PGA_ENQUEUED) != 0,
247*5cd29d0fSMark Johnston 	    ("marker %p not enqueued", ss->marker));
248*5cd29d0fSMark Johnston 
249*5cd29d0fSMark Johnston 	TAILQ_REMOVE(&pq->pq_pl, ss->marker, plinks.q);
250*5cd29d0fSMark Johnston 	vm_page_aflag_clear(ss->marker, PGA_ENQUEUED);
251*5cd29d0fSMark Johnston 	VM_CNT_ADD(v_pdpages, ss->scanned);
2528dbca793STor Egge }
2538dbca793STor Egge 
2548dbca793STor Egge /*
255*5cd29d0fSMark Johnston  * Ensure that the page has not been dequeued after a pageout batch was
256*5cd29d0fSMark Johnston  * collected.  See vm_page_dequeue_complete().
2578c616246SKonstantin Belousov  */
258*5cd29d0fSMark Johnston static inline bool
259*5cd29d0fSMark Johnston vm_pageout_page_queued(vm_page_t m, int queue)
2608c616246SKonstantin Belousov {
261*5cd29d0fSMark Johnston 
262*5cd29d0fSMark Johnston 	vm_page_assert_locked(m);
263*5cd29d0fSMark Johnston 
264*5cd29d0fSMark Johnston 	if ((m->aflags & PGA_DEQUEUE) != 0)
265*5cd29d0fSMark Johnston 		return (false);
266*5cd29d0fSMark Johnston 	atomic_thread_fence_acq();
267*5cd29d0fSMark Johnston 	return (m->queue == queue);
268*5cd29d0fSMark Johnston }
269*5cd29d0fSMark Johnston 
270*5cd29d0fSMark Johnston /*
271*5cd29d0fSMark Johnston  * Add a small number of queued pages to a batch queue for later processing
272*5cd29d0fSMark Johnston  * without the corresponding queue lock held.  The caller must have enqueued a
273*5cd29d0fSMark Johnston  * marker page at the desired start point for the scan.  Pages will be
274*5cd29d0fSMark Johnston  * physically dequeued if the caller so requests.  Otherwise, the returned
275*5cd29d0fSMark Johnston  * batch may contain marker pages, and it is up to the caller to handle them.
276*5cd29d0fSMark Johnston  *
277*5cd29d0fSMark Johnston  * When processing the batch queue, vm_pageout_page_queued() must be used to
278*5cd29d0fSMark Johnston  * determine whether the page was logically dequeued by another thread.  Once
279*5cd29d0fSMark Johnston  * this check is performed, the page lock guarantees that the page will not be
280*5cd29d0fSMark Johnston  * disassociated from the queue.
281*5cd29d0fSMark Johnston  */
282*5cd29d0fSMark Johnston static __always_inline void
283*5cd29d0fSMark Johnston vm_pageout_collect_batch(struct scan_state *ss, const bool dequeue)
284*5cd29d0fSMark Johnston {
2858d220203SAlan Cox 	struct vm_pagequeue *pq;
286*5cd29d0fSMark Johnston 	vm_page_t m, marker;
2878c616246SKonstantin Belousov 
288*5cd29d0fSMark Johnston 	marker = ss->marker;
289*5cd29d0fSMark Johnston 	pq = ss->pq;
2908c616246SKonstantin Belousov 
291*5cd29d0fSMark Johnston 	KASSERT((marker->aflags & PGA_ENQUEUED) != 0,
292*5cd29d0fSMark Johnston 	    ("marker %p not enqueued", ss->marker));
2938c616246SKonstantin Belousov 
2948d220203SAlan Cox 	vm_pagequeue_lock(pq);
295*5cd29d0fSMark Johnston 	for (m = TAILQ_NEXT(marker, plinks.q); m != NULL &&
296*5cd29d0fSMark Johnston 	    ss->scanned < ss->maxscan && ss->bq.bq_cnt < VM_BATCHQUEUE_SIZE;
297*5cd29d0fSMark Johnston 	    m = TAILQ_NEXT(m, plinks.q), ss->scanned++) {
298*5cd29d0fSMark Johnston 		if ((m->flags & PG_MARKER) == 0) {
299*5cd29d0fSMark Johnston 			KASSERT((m->aflags & PGA_ENQUEUED) != 0,
300*5cd29d0fSMark Johnston 			    ("page %p not enqueued", m));
301*5cd29d0fSMark Johnston 			KASSERT((m->flags & PG_FICTITIOUS) == 0,
302*5cd29d0fSMark Johnston 			    ("Fictitious page %p cannot be in page queue", m));
303*5cd29d0fSMark Johnston 			KASSERT((m->oflags & VPO_UNMANAGED) == 0,
304*5cd29d0fSMark Johnston 			    ("Unmanaged page %p cannot be in page queue", m));
305*5cd29d0fSMark Johnston 		} else if (dequeue)
306*5cd29d0fSMark Johnston 			continue;
3078c616246SKonstantin Belousov 
308*5cd29d0fSMark Johnston 		(void)vm_batchqueue_insert(&ss->bq, m);
309*5cd29d0fSMark Johnston 		if (dequeue) {
310*5cd29d0fSMark Johnston 			TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
311*5cd29d0fSMark Johnston 			vm_page_aflag_clear(m, PGA_ENQUEUED);
312*5cd29d0fSMark Johnston 		}
313*5cd29d0fSMark Johnston 	}
314*5cd29d0fSMark Johnston 	TAILQ_REMOVE(&pq->pq_pl, marker, plinks.q);
315*5cd29d0fSMark Johnston 	if (__predict_true(m != NULL))
316*5cd29d0fSMark Johnston 		TAILQ_INSERT_BEFORE(m, marker, plinks.q);
317*5cd29d0fSMark Johnston 	else
318*5cd29d0fSMark Johnston 		TAILQ_INSERT_TAIL(&pq->pq_pl, marker, plinks.q);
319*5cd29d0fSMark Johnston 	if (dequeue)
320*5cd29d0fSMark Johnston 		vm_pagequeue_cnt_add(pq, -ss->bq.bq_cnt);
321*5cd29d0fSMark Johnston 	vm_pagequeue_unlock(pq);
322*5cd29d0fSMark Johnston }
323*5cd29d0fSMark Johnston 
324*5cd29d0fSMark Johnston /* Return the next page to be scanned, or NULL if the scan is complete. */
325*5cd29d0fSMark Johnston static __always_inline vm_page_t
326*5cd29d0fSMark Johnston vm_pageout_next(struct scan_state *ss, const bool dequeue)
327*5cd29d0fSMark Johnston {
328*5cd29d0fSMark Johnston 
329*5cd29d0fSMark Johnston 	if (ss->bq.bq_cnt == 0)
330*5cd29d0fSMark Johnston 		vm_pageout_collect_batch(ss, dequeue);
331*5cd29d0fSMark Johnston 	return (vm_batchqueue_pop(&ss->bq));
3328c616246SKonstantin Belousov }
3338c616246SKonstantin Belousov 
3348c616246SKonstantin Belousov /*
335248fe642SAlan Cox  * Scan for pages at adjacent offsets within the given page's object that are
336248fe642SAlan Cox  * eligible for laundering, form a cluster of these pages and the given page,
337248fe642SAlan Cox  * and launder that cluster.
33826f9a767SRodney W. Grimes  */
3393af76890SPoul-Henning Kamp static int
34034d8b7eaSJeff Roberson vm_pageout_cluster(vm_page_t m)
34124a1cce3SDavid Greenman {
34254d92145SMatthew Dillon 	vm_object_t object;
343248fe642SAlan Cox 	vm_page_t mc[2 * vm_pageout_page_count], p, pb, ps;
344248fe642SAlan Cox 	vm_pindex_t pindex;
345248fe642SAlan Cox 	int ib, is, page_base, pageout_count;
34626f9a767SRodney W. Grimes 
347248fe642SAlan Cox 	vm_page_assert_locked(m);
34817f6a17bSAlan Cox 	object = m->object;
34989f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
350248fe642SAlan Cox 	pindex = m->pindex;
3510cddd8f0SMatthew Dillon 
352c7aebda8SAttilio Rao 	vm_page_assert_unbusied(m);
3531d3a1bcfSMark Johnston 	KASSERT(!vm_page_held(m), ("page %p is held", m));
354aed9aaaaSMark Johnston 
355aed9aaaaSMark Johnston 	pmap_remove_write(m);
35617f6a17bSAlan Cox 	vm_page_unlock(m);
3570d94caffSDavid Greenman 
35891b4f427SAlan Cox 	mc[vm_pageout_page_count] = pb = ps = m;
35926f9a767SRodney W. Grimes 	pageout_count = 1;
360f35329acSJohn Dyson 	page_base = vm_pageout_page_count;
36190ecac61SMatthew Dillon 	ib = 1;
36290ecac61SMatthew Dillon 	is = 1;
36390ecac61SMatthew Dillon 
36424a1cce3SDavid Greenman 	/*
365248fe642SAlan Cox 	 * We can cluster only if the page is not clean, busy, or held, and
366ebcddc72SAlan Cox 	 * the page is in the laundry queue.
36790ecac61SMatthew Dillon 	 *
36890ecac61SMatthew Dillon 	 * During heavy mmap/modification loads the pageout
36990ecac61SMatthew Dillon 	 * daemon can really fragment the underlying file
370248fe642SAlan Cox 	 * due to flushing pages out of order and not trying to
371248fe642SAlan Cox 	 * align the clusters (which leaves sporadic out-of-order
37290ecac61SMatthew Dillon 	 * holes).  To solve this problem we do the reverse scan
37390ecac61SMatthew Dillon 	 * first and attempt to align our cluster, then do a
37490ecac61SMatthew Dillon 	 * forward scan if room remains.
37524a1cce3SDavid Greenman 	 */
37690ecac61SMatthew Dillon more:
377248fe642SAlan Cox 	while (ib != 0 && pageout_count < vm_pageout_page_count) {
37890ecac61SMatthew Dillon 		if (ib > pindex) {
37990ecac61SMatthew Dillon 			ib = 0;
38090ecac61SMatthew Dillon 			break;
381f6b04d2bSDavid Greenman 		}
382c7aebda8SAttilio Rao 		if ((p = vm_page_prev(pb)) == NULL || vm_page_busied(p)) {
38390ecac61SMatthew Dillon 			ib = 0;
38490ecac61SMatthew Dillon 			break;
385f6b04d2bSDavid Greenman 		}
38624a1cce3SDavid Greenman 		vm_page_test_dirty(p);
387*5cd29d0fSMark Johnston 		if (p->dirty == 0 || !vm_page_in_laundry(p)) {
388eb5d3969SAlan Cox 			ib = 0;
389eb5d3969SAlan Cox 			break;
390eb5d3969SAlan Cox 		}
391eb5d3969SAlan Cox 		vm_page_lock(p);
392*5cd29d0fSMark Johnston 		if (vm_page_held(p)) {
3932965a453SKip Macy 			vm_page_unlock(p);
39490ecac61SMatthew Dillon 			ib = 0;
39524a1cce3SDavid Greenman 			break;
396f6b04d2bSDavid Greenman 		}
397aed9aaaaSMark Johnston 		pmap_remove_write(p);
3982965a453SKip Macy 		vm_page_unlock(p);
39991b4f427SAlan Cox 		mc[--page_base] = pb = p;
40090ecac61SMatthew Dillon 		++pageout_count;
40190ecac61SMatthew Dillon 		++ib;
402248fe642SAlan Cox 
40324a1cce3SDavid Greenman 		/*
404248fe642SAlan Cox 		 * We are at an alignment boundary.  Stop here, and switch
405248fe642SAlan Cox 		 * directions.  Do not clear ib.
40624a1cce3SDavid Greenman 		 */
40790ecac61SMatthew Dillon 		if ((pindex - (ib - 1)) % vm_pageout_page_count == 0)
40890ecac61SMatthew Dillon 			break;
40924a1cce3SDavid Greenman 	}
41090ecac61SMatthew Dillon 	while (pageout_count < vm_pageout_page_count &&
41190ecac61SMatthew Dillon 	    pindex + is < object->size) {
412c7aebda8SAttilio Rao 		if ((p = vm_page_next(ps)) == NULL || vm_page_busied(p))
41390ecac61SMatthew Dillon 			break;
41424a1cce3SDavid Greenman 		vm_page_test_dirty(p);
415*5cd29d0fSMark Johnston 		if (p->dirty == 0 || !vm_page_in_laundry(p))
416eb5d3969SAlan Cox 			break;
417eb5d3969SAlan Cox 		vm_page_lock(p);
418*5cd29d0fSMark Johnston 		if (vm_page_held(p)) {
4192965a453SKip Macy 			vm_page_unlock(p);
42024a1cce3SDavid Greenman 			break;
42124a1cce3SDavid Greenman 		}
422aed9aaaaSMark Johnston 		pmap_remove_write(p);
4232965a453SKip Macy 		vm_page_unlock(p);
42491b4f427SAlan Cox 		mc[page_base + pageout_count] = ps = p;
42590ecac61SMatthew Dillon 		++pageout_count;
42690ecac61SMatthew Dillon 		++is;
42724a1cce3SDavid Greenman 	}
42890ecac61SMatthew Dillon 
42990ecac61SMatthew Dillon 	/*
43090ecac61SMatthew Dillon 	 * If we exhausted our forward scan, continue with the reverse scan
431248fe642SAlan Cox 	 * when possible, even past an alignment boundary.  This catches
432248fe642SAlan Cox 	 * boundary conditions.
43390ecac61SMatthew Dillon 	 */
434248fe642SAlan Cox 	if (ib != 0 && pageout_count < vm_pageout_page_count)
43590ecac61SMatthew Dillon 		goto more;
436f6b04d2bSDavid Greenman 
43799e6e193SMark Johnston 	return (vm_pageout_flush(&mc[page_base], pageout_count,
43899e6e193SMark Johnston 	    VM_PAGER_PUT_NOREUSE, 0, NULL, NULL));
439aef922f5SJohn Dyson }
440aef922f5SJohn Dyson 
4411c7c3c6aSMatthew Dillon /*
4421c7c3c6aSMatthew Dillon  * vm_pageout_flush() - launder the given pages
4431c7c3c6aSMatthew Dillon  *
4441c7c3c6aSMatthew Dillon  *	The given pages are laundered.  Note that we setup for the start of
4451c7c3c6aSMatthew Dillon  *	I/O ( i.e. busy the page ), mark it read-only, and bump the object
4461c7c3c6aSMatthew Dillon  *	reference count all in here rather then in the parent.  If we want
4471c7c3c6aSMatthew Dillon  *	the parent to do more sophisticated things we may have to change
4481c7c3c6aSMatthew Dillon  *	the ordering.
4491e8a675cSKonstantin Belousov  *
4501e8a675cSKonstantin Belousov  *	Returned runlen is the count of pages between mreq and first
4511e8a675cSKonstantin Belousov  *	page after mreq with status VM_PAGER_AGAIN.
452126d6082SKonstantin Belousov  *	*eio is set to TRUE if pager returned VM_PAGER_ERROR or VM_PAGER_FAIL
453126d6082SKonstantin Belousov  *	for any page in runlen set.
4541c7c3c6aSMatthew Dillon  */
455aef922f5SJohn Dyson int
456126d6082SKonstantin Belousov vm_pageout_flush(vm_page_t *mc, int count, int flags, int mreq, int *prunlen,
457126d6082SKonstantin Belousov     boolean_t *eio)
458aef922f5SJohn Dyson {
4592e3b314dSAlan Cox 	vm_object_t object = mc[0]->object;
460aef922f5SJohn Dyson 	int pageout_status[count];
46195461b45SJohn Dyson 	int numpagedout = 0;
4621e8a675cSKonstantin Belousov 	int i, runlen;
463aef922f5SJohn Dyson 
46489f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
4657bec141bSKip Macy 
4661c7c3c6aSMatthew Dillon 	/*
467aed9aaaaSMark Johnston 	 * Initiate I/O.  Mark the pages busy and verify that they're valid
468aed9aaaaSMark Johnston 	 * and read-only.
4691c7c3c6aSMatthew Dillon 	 *
4701c7c3c6aSMatthew Dillon 	 * We do not have to fixup the clean/dirty bits here... we can
4711c7c3c6aSMatthew Dillon 	 * allow the pager to do it after the I/O completes.
47202fa91d3SMatthew Dillon 	 *
47302fa91d3SMatthew Dillon 	 * NOTE! mc[i]->dirty may be partial or fragmented due to an
47402fa91d3SMatthew Dillon 	 * edge case with file fragments.
4751c7c3c6aSMatthew Dillon 	 */
4768f9110f6SJohn Dyson 	for (i = 0; i < count; i++) {
4777a935082SAlan Cox 		KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL,
4787a935082SAlan Cox 		    ("vm_pageout_flush: partially invalid page %p index %d/%d",
4797a935082SAlan Cox 			mc[i], i, count));
480aed9aaaaSMark Johnston 		KASSERT((mc[i]->aflags & PGA_WRITEABLE) == 0,
481aed9aaaaSMark Johnston 		    ("vm_pageout_flush: writeable page %p", mc[i]));
482c7aebda8SAttilio Rao 		vm_page_sbusy(mc[i]);
4832965a453SKip Macy 	}
484d474eaaaSDoug Rabson 	vm_object_pip_add(object, count);
485aef922f5SJohn Dyson 
486d076fbeaSAlan Cox 	vm_pager_put_pages(object, mc, count, flags, pageout_status);
48726f9a767SRodney W. Grimes 
4881e8a675cSKonstantin Belousov 	runlen = count - mreq;
489126d6082SKonstantin Belousov 	if (eio != NULL)
490126d6082SKonstantin Belousov 		*eio = FALSE;
491aef922f5SJohn Dyson 	for (i = 0; i < count; i++) {
492aef922f5SJohn Dyson 		vm_page_t mt = mc[i];
49324a1cce3SDavid Greenman 
4944cd45723SAlan Cox 		KASSERT(pageout_status[i] == VM_PAGER_PEND ||
4956031c68dSAlan Cox 		    !pmap_page_is_write_mapped(mt),
4969ea8d1a6SAlan Cox 		    ("vm_pageout_flush: page %p is not write protected", mt));
49726f9a767SRodney W. Grimes 		switch (pageout_status[i]) {
49826f9a767SRodney W. Grimes 		case VM_PAGER_OK:
499ebcddc72SAlan Cox 			vm_page_lock(mt);
500ebcddc72SAlan Cox 			if (vm_page_in_laundry(mt))
501ebcddc72SAlan Cox 				vm_page_deactivate_noreuse(mt);
502ebcddc72SAlan Cox 			vm_page_unlock(mt);
503ebcddc72SAlan Cox 			/* FALLTHROUGH */
50426f9a767SRodney W. Grimes 		case VM_PAGER_PEND:
50595461b45SJohn Dyson 			numpagedout++;
50626f9a767SRodney W. Grimes 			break;
50726f9a767SRodney W. Grimes 		case VM_PAGER_BAD:
50826f9a767SRodney W. Grimes 			/*
509ebcddc72SAlan Cox 			 * The page is outside the object's range.  We pretend
510ebcddc72SAlan Cox 			 * that the page out worked and clean the page, so the
511ebcddc72SAlan Cox 			 * changes will be lost if the page is reclaimed by
512ebcddc72SAlan Cox 			 * the page daemon.
51326f9a767SRodney W. Grimes 			 */
51490ecac61SMatthew Dillon 			vm_page_undirty(mt);
515ebcddc72SAlan Cox 			vm_page_lock(mt);
516ebcddc72SAlan Cox 			if (vm_page_in_laundry(mt))
517ebcddc72SAlan Cox 				vm_page_deactivate_noreuse(mt);
518ebcddc72SAlan Cox 			vm_page_unlock(mt);
51926f9a767SRodney W. Grimes 			break;
52026f9a767SRodney W. Grimes 		case VM_PAGER_ERROR:
52126f9a767SRodney W. Grimes 		case VM_PAGER_FAIL:
52226f9a767SRodney W. Grimes 			/*
523b1fd102eSMark Johnston 			 * If the page couldn't be paged out to swap because the
524b1fd102eSMark Johnston 			 * pager wasn't able to find space, place the page in
525b1fd102eSMark Johnston 			 * the PQ_UNSWAPPABLE holding queue.  This is an
526b1fd102eSMark Johnston 			 * optimization that prevents the page daemon from
527b1fd102eSMark Johnston 			 * wasting CPU cycles on pages that cannot be reclaimed
528b1fd102eSMark Johnston 			 * becase no swap device is configured.
529b1fd102eSMark Johnston 			 *
530b1fd102eSMark Johnston 			 * Otherwise, reactivate the page so that it doesn't
531b1fd102eSMark Johnston 			 * clog the laundry and inactive queues.  (We will try
532b1fd102eSMark Johnston 			 * paging it out again later.)
53326f9a767SRodney W. Grimes 			 */
5343c4a2440SAlan Cox 			vm_page_lock(mt);
535b1fd102eSMark Johnston 			if (object->type == OBJT_SWAP &&
536b1fd102eSMark Johnston 			    pageout_status[i] == VM_PAGER_FAIL) {
537b1fd102eSMark Johnston 				vm_page_unswappable(mt);
538b1fd102eSMark Johnston 				numpagedout++;
539b1fd102eSMark Johnston 			} else
54024a1cce3SDavid Greenman 				vm_page_activate(mt);
5413c4a2440SAlan Cox 			vm_page_unlock(mt);
542126d6082SKonstantin Belousov 			if (eio != NULL && i >= mreq && i - mreq < runlen)
543126d6082SKonstantin Belousov 				*eio = TRUE;
54426f9a767SRodney W. Grimes 			break;
54526f9a767SRodney W. Grimes 		case VM_PAGER_AGAIN:
5461e8a675cSKonstantin Belousov 			if (i >= mreq && i - mreq < runlen)
5471e8a675cSKonstantin Belousov 				runlen = i - mreq;
54826f9a767SRodney W. Grimes 			break;
54926f9a767SRodney W. Grimes 		}
55026f9a767SRodney W. Grimes 
55126f9a767SRodney W. Grimes 		/*
5520d94caffSDavid Greenman 		 * If the operation is still going, leave the page busy to
5530d94caffSDavid Greenman 		 * block all other accesses. Also, leave the paging in
5540d94caffSDavid Greenman 		 * progress indicator set so that we don't attempt an object
5550d94caffSDavid Greenman 		 * collapse.
55626f9a767SRodney W. Grimes 		 */
55726f9a767SRodney W. Grimes 		if (pageout_status[i] != VM_PAGER_PEND) {
558f919ebdeSDavid Greenman 			vm_object_pip_wakeup(object);
559c7aebda8SAttilio Rao 			vm_page_sunbusy(mt);
5603c4a2440SAlan Cox 		}
5613c4a2440SAlan Cox 	}
5621e8a675cSKonstantin Belousov 	if (prunlen != NULL)
5631e8a675cSKonstantin Belousov 		*prunlen = runlen;
5643c4a2440SAlan Cox 	return (numpagedout);
56526f9a767SRodney W. Grimes }
56626f9a767SRodney W. Grimes 
567b1fd102eSMark Johnston static void
568b1fd102eSMark Johnston vm_pageout_swapon(void *arg __unused, struct swdevt *sp __unused)
569b1fd102eSMark Johnston {
570b1fd102eSMark Johnston 
571b1fd102eSMark Johnston 	atomic_store_rel_int(&swapdev_enabled, 1);
572b1fd102eSMark Johnston }
573b1fd102eSMark Johnston 
574b1fd102eSMark Johnston static void
575b1fd102eSMark Johnston vm_pageout_swapoff(void *arg __unused, struct swdevt *sp __unused)
576b1fd102eSMark Johnston {
577b1fd102eSMark Johnston 
578b1fd102eSMark Johnston 	if (swap_pager_nswapdev() == 1)
579b1fd102eSMark Johnston 		atomic_store_rel_int(&swapdev_enabled, 0);
580b1fd102eSMark Johnston }
581b1fd102eSMark Johnston 
5821c7c3c6aSMatthew Dillon /*
58334d8b7eaSJeff Roberson  * Attempt to acquire all of the necessary locks to launder a page and
58434d8b7eaSJeff Roberson  * then call through the clustering layer to PUTPAGES.  Wait a short
58534d8b7eaSJeff Roberson  * time for a vnode lock.
58634d8b7eaSJeff Roberson  *
58734d8b7eaSJeff Roberson  * Requires the page and object lock on entry, releases both before return.
58834d8b7eaSJeff Roberson  * Returns 0 on success and an errno otherwise.
58934d8b7eaSJeff Roberson  */
59034d8b7eaSJeff Roberson static int
591ebcddc72SAlan Cox vm_pageout_clean(vm_page_t m, int *numpagedout)
59234d8b7eaSJeff Roberson {
59334d8b7eaSJeff Roberson 	struct vnode *vp;
59434d8b7eaSJeff Roberson 	struct mount *mp;
59534d8b7eaSJeff Roberson 	vm_object_t object;
59634d8b7eaSJeff Roberson 	vm_pindex_t pindex;
59734d8b7eaSJeff Roberson 	int error, lockmode;
59834d8b7eaSJeff Roberson 
59934d8b7eaSJeff Roberson 	vm_page_assert_locked(m);
60034d8b7eaSJeff Roberson 	object = m->object;
60134d8b7eaSJeff Roberson 	VM_OBJECT_ASSERT_WLOCKED(object);
60234d8b7eaSJeff Roberson 	error = 0;
60334d8b7eaSJeff Roberson 	vp = NULL;
60434d8b7eaSJeff Roberson 	mp = NULL;
60534d8b7eaSJeff Roberson 
60634d8b7eaSJeff Roberson 	/*
60734d8b7eaSJeff Roberson 	 * The object is already known NOT to be dead.   It
60834d8b7eaSJeff Roberson 	 * is possible for the vget() to block the whole
60934d8b7eaSJeff Roberson 	 * pageout daemon, but the new low-memory handling
61034d8b7eaSJeff Roberson 	 * code should prevent it.
61134d8b7eaSJeff Roberson 	 *
61234d8b7eaSJeff Roberson 	 * We can't wait forever for the vnode lock, we might
61334d8b7eaSJeff Roberson 	 * deadlock due to a vn_read() getting stuck in
61434d8b7eaSJeff Roberson 	 * vm_wait while holding this vnode.  We skip the
61534d8b7eaSJeff Roberson 	 * vnode if we can't get it in a reasonable amount
61634d8b7eaSJeff Roberson 	 * of time.
61734d8b7eaSJeff Roberson 	 */
61834d8b7eaSJeff Roberson 	if (object->type == OBJT_VNODE) {
61934d8b7eaSJeff Roberson 		vm_page_unlock(m);
62034d8b7eaSJeff Roberson 		vp = object->handle;
62134d8b7eaSJeff Roberson 		if (vp->v_type == VREG &&
62234d8b7eaSJeff Roberson 		    vn_start_write(vp, &mp, V_NOWAIT) != 0) {
62334d8b7eaSJeff Roberson 			mp = NULL;
62434d8b7eaSJeff Roberson 			error = EDEADLK;
62534d8b7eaSJeff Roberson 			goto unlock_all;
62634d8b7eaSJeff Roberson 		}
62734d8b7eaSJeff Roberson 		KASSERT(mp != NULL,
62834d8b7eaSJeff Roberson 		    ("vp %p with NULL v_mount", vp));
62934d8b7eaSJeff Roberson 		vm_object_reference_locked(object);
63034d8b7eaSJeff Roberson 		pindex = m->pindex;
63134d8b7eaSJeff Roberson 		VM_OBJECT_WUNLOCK(object);
63234d8b7eaSJeff Roberson 		lockmode = MNT_SHARED_WRITES(vp->v_mount) ?
63334d8b7eaSJeff Roberson 		    LK_SHARED : LK_EXCLUSIVE;
63434d8b7eaSJeff Roberson 		if (vget(vp, lockmode | LK_TIMELOCK, curthread)) {
63534d8b7eaSJeff Roberson 			vp = NULL;
63634d8b7eaSJeff Roberson 			error = EDEADLK;
63734d8b7eaSJeff Roberson 			goto unlock_mp;
63834d8b7eaSJeff Roberson 		}
63934d8b7eaSJeff Roberson 		VM_OBJECT_WLOCK(object);
64057cd81a3SMark Johnston 
64157cd81a3SMark Johnston 		/*
64257cd81a3SMark Johnston 		 * Ensure that the object and vnode were not disassociated
64357cd81a3SMark Johnston 		 * while locks were dropped.
64457cd81a3SMark Johnston 		 */
64557cd81a3SMark Johnston 		if (vp->v_object != object) {
64657cd81a3SMark Johnston 			error = ENOENT;
64757cd81a3SMark Johnston 			goto unlock_all;
64857cd81a3SMark Johnston 		}
64934d8b7eaSJeff Roberson 		vm_page_lock(m);
65057cd81a3SMark Johnston 
65134d8b7eaSJeff Roberson 		/*
65234d8b7eaSJeff Roberson 		 * While the object and page were unlocked, the page
65334d8b7eaSJeff Roberson 		 * may have been:
65434d8b7eaSJeff Roberson 		 * (1) moved to a different queue,
65534d8b7eaSJeff Roberson 		 * (2) reallocated to a different object,
65634d8b7eaSJeff Roberson 		 * (3) reallocated to a different offset, or
65734d8b7eaSJeff Roberson 		 * (4) cleaned.
65834d8b7eaSJeff Roberson 		 */
659ebcddc72SAlan Cox 		if (!vm_page_in_laundry(m) || m->object != object ||
66034d8b7eaSJeff Roberson 		    m->pindex != pindex || m->dirty == 0) {
66134d8b7eaSJeff Roberson 			vm_page_unlock(m);
66234d8b7eaSJeff Roberson 			error = ENXIO;
66334d8b7eaSJeff Roberson 			goto unlock_all;
66434d8b7eaSJeff Roberson 		}
66534d8b7eaSJeff Roberson 
66634d8b7eaSJeff Roberson 		/*
6671d3a1bcfSMark Johnston 		 * The page may have been busied or referenced while the object
66834d8b7eaSJeff Roberson 		 * and page locks were released.
66934d8b7eaSJeff Roberson 		 */
6701d3a1bcfSMark Johnston 		if (vm_page_busied(m) || vm_page_held(m)) {
67134d8b7eaSJeff Roberson 			vm_page_unlock(m);
67234d8b7eaSJeff Roberson 			error = EBUSY;
67334d8b7eaSJeff Roberson 			goto unlock_all;
67434d8b7eaSJeff Roberson 		}
67534d8b7eaSJeff Roberson 	}
67634d8b7eaSJeff Roberson 
67734d8b7eaSJeff Roberson 	/*
67834d8b7eaSJeff Roberson 	 * If a page is dirty, then it is either being washed
67934d8b7eaSJeff Roberson 	 * (but not yet cleaned) or it is still in the
68034d8b7eaSJeff Roberson 	 * laundry.  If it is still in the laundry, then we
68134d8b7eaSJeff Roberson 	 * start the cleaning operation.
68234d8b7eaSJeff Roberson 	 */
683ebcddc72SAlan Cox 	if ((*numpagedout = vm_pageout_cluster(m)) == 0)
68434d8b7eaSJeff Roberson 		error = EIO;
68534d8b7eaSJeff Roberson 
68634d8b7eaSJeff Roberson unlock_all:
68734d8b7eaSJeff Roberson 	VM_OBJECT_WUNLOCK(object);
68834d8b7eaSJeff Roberson 
68934d8b7eaSJeff Roberson unlock_mp:
69034d8b7eaSJeff Roberson 	vm_page_lock_assert(m, MA_NOTOWNED);
69134d8b7eaSJeff Roberson 	if (mp != NULL) {
69234d8b7eaSJeff Roberson 		if (vp != NULL)
69334d8b7eaSJeff Roberson 			vput(vp);
69434d8b7eaSJeff Roberson 		vm_object_deallocate(object);
69534d8b7eaSJeff Roberson 		vn_finished_write(mp);
69634d8b7eaSJeff Roberson 	}
69734d8b7eaSJeff Roberson 
69834d8b7eaSJeff Roberson 	return (error);
69934d8b7eaSJeff Roberson }
70034d8b7eaSJeff Roberson 
70134d8b7eaSJeff Roberson /*
702ebcddc72SAlan Cox  * Attempt to launder the specified number of pages.
703ebcddc72SAlan Cox  *
704ebcddc72SAlan Cox  * Returns the number of pages successfully laundered.
705ebcddc72SAlan Cox  */
706ebcddc72SAlan Cox static int
707ebcddc72SAlan Cox vm_pageout_launder(struct vm_domain *vmd, int launder, bool in_shortfall)
708ebcddc72SAlan Cox {
709*5cd29d0fSMark Johnston 	struct scan_state ss;
710ebcddc72SAlan Cox 	struct vm_pagequeue *pq;
711*5cd29d0fSMark Johnston 	struct mtx *mtx;
712ebcddc72SAlan Cox 	vm_object_t object;
713*5cd29d0fSMark Johnston 	vm_page_t m, marker;
714*5cd29d0fSMark Johnston 	int act_delta, error, numpagedout, queue, starting_target;
715ebcddc72SAlan Cox 	int vnodes_skipped;
716*5cd29d0fSMark Johnston 	bool obj_locked, pageout_ok;
717ebcddc72SAlan Cox 
718*5cd29d0fSMark Johnston 	mtx = NULL;
719*5cd29d0fSMark Johnston 	obj_locked = false;
720*5cd29d0fSMark Johnston 	object = NULL;
721ebcddc72SAlan Cox 	starting_target = launder;
722ebcddc72SAlan Cox 	vnodes_skipped = 0;
723ebcddc72SAlan Cox 
724ebcddc72SAlan Cox 	/*
725b1fd102eSMark Johnston 	 * Scan the laundry queues for pages eligible to be laundered.  We stop
726ebcddc72SAlan Cox 	 * once the target number of dirty pages have been laundered, or once
727ebcddc72SAlan Cox 	 * we've reached the end of the queue.  A single iteration of this loop
728ebcddc72SAlan Cox 	 * may cause more than one page to be laundered because of clustering.
729ebcddc72SAlan Cox 	 *
730b1fd102eSMark Johnston 	 * As an optimization, we avoid laundering from PQ_UNSWAPPABLE when no
731b1fd102eSMark Johnston 	 * swap devices are configured.
732ebcddc72SAlan Cox 	 */
733b1fd102eSMark Johnston 	if (atomic_load_acq_int(&swapdev_enabled))
73464b38930SMark Johnston 		queue = PQ_UNSWAPPABLE;
735b1fd102eSMark Johnston 	else
73664b38930SMark Johnston 		queue = PQ_LAUNDRY;
737ebcddc72SAlan Cox 
738b1fd102eSMark Johnston scan:
73964b38930SMark Johnston 	marker = &vmd->vmd_markers[queue];
740*5cd29d0fSMark Johnston 	pq = &vmd->vmd_pagequeues[queue];
741ebcddc72SAlan Cox 	vm_pagequeue_lock(pq);
742*5cd29d0fSMark Johnston 	vm_pageout_init_scan(&ss, pq, marker, NULL, pq->pq_cnt);
743*5cd29d0fSMark Johnston 	while (launder > 0 && (m = vm_pageout_next(&ss, false)) != NULL) {
744*5cd29d0fSMark Johnston 		if (__predict_false((m->flags & PG_MARKER) != 0))
745ebcddc72SAlan Cox 			continue;
746*5cd29d0fSMark Johnston 
747*5cd29d0fSMark Johnston 		vm_page_change_lock(m, &mtx);
748*5cd29d0fSMark Johnston 
749*5cd29d0fSMark Johnston recheck:
750*5cd29d0fSMark Johnston 		/*
751*5cd29d0fSMark Johnston 		 * The page may have been disassociated from the queue
752*5cd29d0fSMark Johnston 		 * while locks were dropped.
753*5cd29d0fSMark Johnston 		 */
754*5cd29d0fSMark Johnston 		if (!vm_pageout_page_queued(m, queue))
755ebcddc72SAlan Cox 			continue;
756*5cd29d0fSMark Johnston 
757*5cd29d0fSMark Johnston 		/*
758*5cd29d0fSMark Johnston 		 * A requeue was requested, so this page gets a second
759*5cd29d0fSMark Johnston 		 * chance.
760*5cd29d0fSMark Johnston 		 */
761*5cd29d0fSMark Johnston 		if ((m->aflags & PGA_REQUEUE) != 0) {
762*5cd29d0fSMark Johnston 			vm_page_requeue(m);
763ebcddc72SAlan Cox 			continue;
764ebcddc72SAlan Cox 		}
765ebcddc72SAlan Cox 
766ebcddc72SAlan Cox 		/*
767*5cd29d0fSMark Johnston 		 * Held pages are essentially stuck in the queue.
768*5cd29d0fSMark Johnston 		 *
769*5cd29d0fSMark Johnston 		 * Wired pages may not be freed.  Complete their removal
770*5cd29d0fSMark Johnston 		 * from the queue now to avoid needless revisits during
771*5cd29d0fSMark Johnston 		 * future scans.
772ebcddc72SAlan Cox 		 */
773*5cd29d0fSMark Johnston 		if (m->hold_count != 0)
774*5cd29d0fSMark Johnston 			continue;
775*5cd29d0fSMark Johnston 		if (m->wire_count != 0) {
776*5cd29d0fSMark Johnston 			vm_page_dequeue_deferred(m);
777*5cd29d0fSMark Johnston 			continue;
778*5cd29d0fSMark Johnston 		}
779*5cd29d0fSMark Johnston 
780*5cd29d0fSMark Johnston 		if (object != m->object) {
781*5cd29d0fSMark Johnston 			if (obj_locked) {
782*5cd29d0fSMark Johnston 				VM_OBJECT_WUNLOCK(object);
783*5cd29d0fSMark Johnston 				obj_locked = false;
784*5cd29d0fSMark Johnston 			}
785*5cd29d0fSMark Johnston 			object = m->object;
786*5cd29d0fSMark Johnston 		}
787*5cd29d0fSMark Johnston 		if (!obj_locked) {
788*5cd29d0fSMark Johnston 			if (!VM_OBJECT_TRYWLOCK(object)) {
789*5cd29d0fSMark Johnston 				mtx_unlock(mtx);
790*5cd29d0fSMark Johnston 				/* Depends on type-stability. */
791*5cd29d0fSMark Johnston 				VM_OBJECT_WLOCK(object);
792*5cd29d0fSMark Johnston 				obj_locked = true;
793*5cd29d0fSMark Johnston 				mtx_lock(mtx);
794*5cd29d0fSMark Johnston 				goto recheck;
795*5cd29d0fSMark Johnston 			} else
796*5cd29d0fSMark Johnston 				obj_locked = true;
797*5cd29d0fSMark Johnston 		}
798*5cd29d0fSMark Johnston 
799*5cd29d0fSMark Johnston 		if (vm_page_busied(m))
800*5cd29d0fSMark Johnston 			continue;
801ebcddc72SAlan Cox 
802ebcddc72SAlan Cox 		/*
803ebcddc72SAlan Cox 		 * Invalid pages can be easily freed.  They cannot be
804ebcddc72SAlan Cox 		 * mapped; vm_page_free() asserts this.
805ebcddc72SAlan Cox 		 */
806ebcddc72SAlan Cox 		if (m->valid == 0)
807ebcddc72SAlan Cox 			goto free_page;
808ebcddc72SAlan Cox 
809ebcddc72SAlan Cox 		/*
810ebcddc72SAlan Cox 		 * If the page has been referenced and the object is not dead,
811ebcddc72SAlan Cox 		 * reactivate or requeue the page depending on whether the
812ebcddc72SAlan Cox 		 * object is mapped.
813ebcddc72SAlan Cox 		 */
814ebcddc72SAlan Cox 		if ((m->aflags & PGA_REFERENCED) != 0) {
815ebcddc72SAlan Cox 			vm_page_aflag_clear(m, PGA_REFERENCED);
816ebcddc72SAlan Cox 			act_delta = 1;
817ebcddc72SAlan Cox 		} else
818ebcddc72SAlan Cox 			act_delta = 0;
819ebcddc72SAlan Cox 		if (object->ref_count != 0)
820ebcddc72SAlan Cox 			act_delta += pmap_ts_referenced(m);
821ebcddc72SAlan Cox 		else {
822ebcddc72SAlan Cox 			KASSERT(!pmap_page_is_mapped(m),
823ebcddc72SAlan Cox 			    ("page %p is mapped", m));
824ebcddc72SAlan Cox 		}
825ebcddc72SAlan Cox 		if (act_delta != 0) {
826ebcddc72SAlan Cox 			if (object->ref_count != 0) {
82783c9dea1SGleb Smirnoff 				VM_CNT_INC(v_reactivated);
828ebcddc72SAlan Cox 				vm_page_activate(m);
829ebcddc72SAlan Cox 
830ebcddc72SAlan Cox 				/*
831ebcddc72SAlan Cox 				 * Increase the activation count if the page
832ebcddc72SAlan Cox 				 * was referenced while in the laundry queue.
833ebcddc72SAlan Cox 				 * This makes it less likely that the page will
834ebcddc72SAlan Cox 				 * be returned prematurely to the inactive
835ebcddc72SAlan Cox 				 * queue.
836ebcddc72SAlan Cox  				 */
837ebcddc72SAlan Cox 				m->act_count += act_delta + ACT_ADVANCE;
838ebcddc72SAlan Cox 
839ebcddc72SAlan Cox 				/*
840ebcddc72SAlan Cox 				 * If this was a background laundering, count
841ebcddc72SAlan Cox 				 * activated pages towards our target.  The
842ebcddc72SAlan Cox 				 * purpose of background laundering is to ensure
843ebcddc72SAlan Cox 				 * that pages are eventually cycled through the
844ebcddc72SAlan Cox 				 * laundry queue, and an activation is a valid
845ebcddc72SAlan Cox 				 * way out.
846ebcddc72SAlan Cox 				 */
847ebcddc72SAlan Cox 				if (!in_shortfall)
848ebcddc72SAlan Cox 					launder--;
849*5cd29d0fSMark Johnston 				continue;
850*5cd29d0fSMark Johnston 			} else if ((object->flags & OBJ_DEAD) == 0) {
851*5cd29d0fSMark Johnston 				vm_page_requeue(m);
852*5cd29d0fSMark Johnston 				continue;
853*5cd29d0fSMark Johnston 			}
854ebcddc72SAlan Cox 		}
855ebcddc72SAlan Cox 
856ebcddc72SAlan Cox 		/*
857ebcddc72SAlan Cox 		 * If the page appears to be clean at the machine-independent
858ebcddc72SAlan Cox 		 * layer, then remove all of its mappings from the pmap in
859ebcddc72SAlan Cox 		 * anticipation of freeing it.  If, however, any of the page's
860ebcddc72SAlan Cox 		 * mappings allow write access, then the page may still be
861ebcddc72SAlan Cox 		 * modified until the last of those mappings are removed.
862ebcddc72SAlan Cox 		 */
863ebcddc72SAlan Cox 		if (object->ref_count != 0) {
864ebcddc72SAlan Cox 			vm_page_test_dirty(m);
865ebcddc72SAlan Cox 			if (m->dirty == 0)
866ebcddc72SAlan Cox 				pmap_remove_all(m);
867ebcddc72SAlan Cox 		}
868ebcddc72SAlan Cox 
869ebcddc72SAlan Cox 		/*
870ebcddc72SAlan Cox 		 * Clean pages are freed, and dirty pages are paged out unless
871ebcddc72SAlan Cox 		 * they belong to a dead object.  Requeueing dirty pages from
872ebcddc72SAlan Cox 		 * dead objects is pointless, as they are being paged out and
873ebcddc72SAlan Cox 		 * freed by the thread that destroyed the object.
874ebcddc72SAlan Cox 		 */
875ebcddc72SAlan Cox 		if (m->dirty == 0) {
876ebcddc72SAlan Cox free_page:
877ebcddc72SAlan Cox 			vm_page_free(m);
87883c9dea1SGleb Smirnoff 			VM_CNT_INC(v_dfree);
879ebcddc72SAlan Cox 		} else if ((object->flags & OBJ_DEAD) == 0) {
880ebcddc72SAlan Cox 			if (object->type != OBJT_SWAP &&
881ebcddc72SAlan Cox 			    object->type != OBJT_DEFAULT)
882ebcddc72SAlan Cox 				pageout_ok = true;
883ebcddc72SAlan Cox 			else if (disable_swap_pageouts)
884ebcddc72SAlan Cox 				pageout_ok = false;
885ebcddc72SAlan Cox 			else
886ebcddc72SAlan Cox 				pageout_ok = true;
887ebcddc72SAlan Cox 			if (!pageout_ok) {
888*5cd29d0fSMark Johnston 				vm_page_requeue(m);
889*5cd29d0fSMark Johnston 				continue;
890ebcddc72SAlan Cox 			}
891ebcddc72SAlan Cox 
892ebcddc72SAlan Cox 			/*
893ebcddc72SAlan Cox 			 * Form a cluster with adjacent, dirty pages from the
894ebcddc72SAlan Cox 			 * same object, and page out that entire cluster.
895ebcddc72SAlan Cox 			 *
896ebcddc72SAlan Cox 			 * The adjacent, dirty pages must also be in the
897ebcddc72SAlan Cox 			 * laundry.  However, their mappings are not checked
898ebcddc72SAlan Cox 			 * for new references.  Consequently, a recently
899ebcddc72SAlan Cox 			 * referenced page may be paged out.  However, that
900ebcddc72SAlan Cox 			 * page will not be prematurely reclaimed.  After page
901ebcddc72SAlan Cox 			 * out, the page will be placed in the inactive queue,
902ebcddc72SAlan Cox 			 * where any new references will be detected and the
903ebcddc72SAlan Cox 			 * page reactivated.
904ebcddc72SAlan Cox 			 */
905ebcddc72SAlan Cox 			error = vm_pageout_clean(m, &numpagedout);
906ebcddc72SAlan Cox 			if (error == 0) {
907ebcddc72SAlan Cox 				launder -= numpagedout;
908*5cd29d0fSMark Johnston 				ss.scanned += numpagedout;
909ebcddc72SAlan Cox 			} else if (error == EDEADLK) {
910ebcddc72SAlan Cox 				pageout_lock_miss++;
911ebcddc72SAlan Cox 				vnodes_skipped++;
912ebcddc72SAlan Cox 			}
913*5cd29d0fSMark Johnston 			mtx = NULL;
914*5cd29d0fSMark Johnston 			obj_locked = false;
915ebcddc72SAlan Cox 		}
916*5cd29d0fSMark Johnston 	}
917*5cd29d0fSMark Johnston 	if (mtx != NULL) {
918*5cd29d0fSMark Johnston 		mtx_unlock(mtx);
919*5cd29d0fSMark Johnston 		mtx = NULL;
920*5cd29d0fSMark Johnston 	}
921*5cd29d0fSMark Johnston 	if (obj_locked) {
922ebcddc72SAlan Cox 		VM_OBJECT_WUNLOCK(object);
923*5cd29d0fSMark Johnston 		obj_locked = false;
924*5cd29d0fSMark Johnston 	}
925ebcddc72SAlan Cox 	vm_pagequeue_lock(pq);
926*5cd29d0fSMark Johnston 	vm_pageout_end_scan(&ss);
927ebcddc72SAlan Cox 	vm_pagequeue_unlock(pq);
928ebcddc72SAlan Cox 
92964b38930SMark Johnston 	if (launder > 0 && queue == PQ_UNSWAPPABLE) {
93064b38930SMark Johnston 		queue = PQ_LAUNDRY;
931b1fd102eSMark Johnston 		goto scan;
932b1fd102eSMark Johnston 	}
933b1fd102eSMark Johnston 
934ebcddc72SAlan Cox 	/*
935ebcddc72SAlan Cox 	 * Wakeup the sync daemon if we skipped a vnode in a writeable object
936ebcddc72SAlan Cox 	 * and we didn't launder enough pages.
937ebcddc72SAlan Cox 	 */
938ebcddc72SAlan Cox 	if (vnodes_skipped > 0 && launder > 0)
939ebcddc72SAlan Cox 		(void)speedup_syncer();
940ebcddc72SAlan Cox 
941ebcddc72SAlan Cox 	return (starting_target - launder);
942ebcddc72SAlan Cox }
943ebcddc72SAlan Cox 
944ebcddc72SAlan Cox /*
945ebcddc72SAlan Cox  * Compute the integer square root.
946ebcddc72SAlan Cox  */
947ebcddc72SAlan Cox static u_int
948ebcddc72SAlan Cox isqrt(u_int num)
949ebcddc72SAlan Cox {
950ebcddc72SAlan Cox 	u_int bit, root, tmp;
951ebcddc72SAlan Cox 
952ebcddc72SAlan Cox 	bit = 1u << ((NBBY * sizeof(u_int)) - 2);
953ebcddc72SAlan Cox 	while (bit > num)
954ebcddc72SAlan Cox 		bit >>= 2;
955ebcddc72SAlan Cox 	root = 0;
956ebcddc72SAlan Cox 	while (bit != 0) {
957ebcddc72SAlan Cox 		tmp = root + bit;
958ebcddc72SAlan Cox 		root >>= 1;
959ebcddc72SAlan Cox 		if (num >= tmp) {
960ebcddc72SAlan Cox 			num -= tmp;
961ebcddc72SAlan Cox 			root += bit;
962ebcddc72SAlan Cox 		}
963ebcddc72SAlan Cox 		bit >>= 2;
964ebcddc72SAlan Cox 	}
965ebcddc72SAlan Cox 	return (root);
966ebcddc72SAlan Cox }
967ebcddc72SAlan Cox 
968ebcddc72SAlan Cox /*
969ebcddc72SAlan Cox  * Perform the work of the laundry thread: periodically wake up and determine
970ebcddc72SAlan Cox  * whether any pages need to be laundered.  If so, determine the number of pages
971ebcddc72SAlan Cox  * that need to be laundered, and launder them.
972ebcddc72SAlan Cox  */
973ebcddc72SAlan Cox static void
974ebcddc72SAlan Cox vm_pageout_laundry_worker(void *arg)
975ebcddc72SAlan Cox {
976e2068d0bSJeff Roberson 	struct vm_domain *vmd;
977ebcddc72SAlan Cox 	struct vm_pagequeue *pq;
97860684862SMark Johnston 	uint64_t nclean, ndirty, nfreed;
979e2068d0bSJeff Roberson 	int domain, last_target, launder, shortfall, shortfall_cycle, target;
980ebcddc72SAlan Cox 	bool in_shortfall;
981ebcddc72SAlan Cox 
982e2068d0bSJeff Roberson 	domain = (uintptr_t)arg;
983e2068d0bSJeff Roberson 	vmd = VM_DOMAIN(domain);
984e2068d0bSJeff Roberson 	pq = &vmd->vmd_pagequeues[PQ_LAUNDRY];
985e2068d0bSJeff Roberson 	KASSERT(vmd->vmd_segs != 0, ("domain without segments"));
986ebcddc72SAlan Cox 
987ebcddc72SAlan Cox 	shortfall = 0;
988ebcddc72SAlan Cox 	in_shortfall = false;
989ebcddc72SAlan Cox 	shortfall_cycle = 0;
990ebcddc72SAlan Cox 	target = 0;
99160684862SMark Johnston 	nfreed = 0;
992ebcddc72SAlan Cox 
993ebcddc72SAlan Cox 	/*
994b1fd102eSMark Johnston 	 * Calls to these handlers are serialized by the swap syscall lock.
995b1fd102eSMark Johnston 	 */
996e2068d0bSJeff Roberson 	(void)EVENTHANDLER_REGISTER(swapon, vm_pageout_swapon, vmd,
997b1fd102eSMark Johnston 	    EVENTHANDLER_PRI_ANY);
998e2068d0bSJeff Roberson 	(void)EVENTHANDLER_REGISTER(swapoff, vm_pageout_swapoff, vmd,
999b1fd102eSMark Johnston 	    EVENTHANDLER_PRI_ANY);
1000b1fd102eSMark Johnston 
1001b1fd102eSMark Johnston 	/*
1002ebcddc72SAlan Cox 	 * The pageout laundry worker is never done, so loop forever.
1003ebcddc72SAlan Cox 	 */
1004ebcddc72SAlan Cox 	for (;;) {
1005ebcddc72SAlan Cox 		KASSERT(target >= 0, ("negative target %d", target));
1006ebcddc72SAlan Cox 		KASSERT(shortfall_cycle >= 0,
1007ebcddc72SAlan Cox 		    ("negative cycle %d", shortfall_cycle));
1008ebcddc72SAlan Cox 		launder = 0;
1009ebcddc72SAlan Cox 
1010ebcddc72SAlan Cox 		/*
1011ebcddc72SAlan Cox 		 * First determine whether we need to launder pages to meet a
1012ebcddc72SAlan Cox 		 * shortage of free pages.
1013ebcddc72SAlan Cox 		 */
1014ebcddc72SAlan Cox 		if (shortfall > 0) {
1015ebcddc72SAlan Cox 			in_shortfall = true;
1016ebcddc72SAlan Cox 			shortfall_cycle = VM_LAUNDER_RATE / VM_INACT_SCAN_RATE;
1017ebcddc72SAlan Cox 			target = shortfall;
1018ebcddc72SAlan Cox 		} else if (!in_shortfall)
1019ebcddc72SAlan Cox 			goto trybackground;
1020e2068d0bSJeff Roberson 		else if (shortfall_cycle == 0 || vm_laundry_target(vmd) <= 0) {
1021ebcddc72SAlan Cox 			/*
1022ebcddc72SAlan Cox 			 * We recently entered shortfall and began laundering
1023ebcddc72SAlan Cox 			 * pages.  If we have completed that laundering run
1024ebcddc72SAlan Cox 			 * (and we are no longer in shortfall) or we have met
1025ebcddc72SAlan Cox 			 * our laundry target through other activity, then we
1026ebcddc72SAlan Cox 			 * can stop laundering pages.
1027ebcddc72SAlan Cox 			 */
1028ebcddc72SAlan Cox 			in_shortfall = false;
1029ebcddc72SAlan Cox 			target = 0;
1030ebcddc72SAlan Cox 			goto trybackground;
1031ebcddc72SAlan Cox 		}
1032ebcddc72SAlan Cox 		launder = target / shortfall_cycle--;
1033ebcddc72SAlan Cox 		goto dolaundry;
1034ebcddc72SAlan Cox 
1035ebcddc72SAlan Cox 		/*
1036ebcddc72SAlan Cox 		 * There's no immediate need to launder any pages; see if we
1037ebcddc72SAlan Cox 		 * meet the conditions to perform background laundering:
1038ebcddc72SAlan Cox 		 *
1039ebcddc72SAlan Cox 		 * 1. The ratio of dirty to clean inactive pages exceeds the
104060684862SMark Johnston 		 *    background laundering threshold, or
1041ebcddc72SAlan Cox 		 * 2. we haven't yet reached the target of the current
1042ebcddc72SAlan Cox 		 *    background laundering run.
1043ebcddc72SAlan Cox 		 *
1044ebcddc72SAlan Cox 		 * The background laundering threshold is not a constant.
1045ebcddc72SAlan Cox 		 * Instead, it is a slowly growing function of the number of
104660684862SMark Johnston 		 * clean pages freed by the page daemon since the last
104760684862SMark Johnston 		 * background laundering.  Thus, as the ratio of dirty to
104860684862SMark Johnston 		 * clean inactive pages grows, the amount of memory pressure
1049c098768eSMark Johnston 		 * required to trigger laundering decreases.  We ensure
1050c098768eSMark Johnston 		 * that the threshold is non-zero after an inactive queue
1051c098768eSMark Johnston 		 * scan, even if that scan failed to free a single clean page.
1052ebcddc72SAlan Cox 		 */
1053ebcddc72SAlan Cox trybackground:
1054e2068d0bSJeff Roberson 		nclean = vmd->vmd_free_count +
1055e2068d0bSJeff Roberson 		    vmd->vmd_pagequeues[PQ_INACTIVE].pq_cnt;
1056e2068d0bSJeff Roberson 		ndirty = vmd->vmd_pagequeues[PQ_LAUNDRY].pq_cnt;
1057c098768eSMark Johnston 		if (target == 0 && ndirty * isqrt(howmany(nfreed + 1,
1058c098768eSMark Johnston 		    vmd->vmd_free_target - vmd->vmd_free_min)) >= nclean) {
1059e2068d0bSJeff Roberson 			target = vmd->vmd_background_launder_target;
1060ebcddc72SAlan Cox 		}
1061ebcddc72SAlan Cox 
1062ebcddc72SAlan Cox 		/*
1063ebcddc72SAlan Cox 		 * We have a non-zero background laundering target.  If we've
1064ebcddc72SAlan Cox 		 * laundered up to our maximum without observing a page daemon
1065cb35676eSMark Johnston 		 * request, just stop.  This is a safety belt that ensures we
1066ebcddc72SAlan Cox 		 * don't launder an excessive amount if memory pressure is low
1067ebcddc72SAlan Cox 		 * and the ratio of dirty to clean pages is large.  Otherwise,
1068ebcddc72SAlan Cox 		 * proceed at the background laundering rate.
1069ebcddc72SAlan Cox 		 */
1070ebcddc72SAlan Cox 		if (target > 0) {
107160684862SMark Johnston 			if (nfreed > 0) {
107260684862SMark Johnston 				nfreed = 0;
1073ebcddc72SAlan Cox 				last_target = target;
1074ebcddc72SAlan Cox 			} else if (last_target - target >=
1075ebcddc72SAlan Cox 			    vm_background_launder_max * PAGE_SIZE / 1024) {
1076ebcddc72SAlan Cox 				target = 0;
1077ebcddc72SAlan Cox 			}
1078ebcddc72SAlan Cox 			launder = vm_background_launder_rate * PAGE_SIZE / 1024;
1079ebcddc72SAlan Cox 			launder /= VM_LAUNDER_RATE;
1080ebcddc72SAlan Cox 			if (launder > target)
1081ebcddc72SAlan Cox 				launder = target;
1082ebcddc72SAlan Cox 		}
1083ebcddc72SAlan Cox 
1084ebcddc72SAlan Cox dolaundry:
1085ebcddc72SAlan Cox 		if (launder > 0) {
1086ebcddc72SAlan Cox 			/*
1087ebcddc72SAlan Cox 			 * Because of I/O clustering, the number of laundered
1088ebcddc72SAlan Cox 			 * pages could exceed "target" by the maximum size of
1089ebcddc72SAlan Cox 			 * a cluster minus one.
1090ebcddc72SAlan Cox 			 */
1091e2068d0bSJeff Roberson 			target -= min(vm_pageout_launder(vmd, launder,
1092ebcddc72SAlan Cox 			    in_shortfall), target);
1093ebcddc72SAlan Cox 			pause("laundp", hz / VM_LAUNDER_RATE);
1094ebcddc72SAlan Cox 		}
1095ebcddc72SAlan Cox 
1096ebcddc72SAlan Cox 		/*
1097ebcddc72SAlan Cox 		 * If we're not currently laundering pages and the page daemon
1098ebcddc72SAlan Cox 		 * hasn't posted a new request, sleep until the page daemon
1099ebcddc72SAlan Cox 		 * kicks us.
1100ebcddc72SAlan Cox 		 */
1101ebcddc72SAlan Cox 		vm_pagequeue_lock(pq);
1102e2068d0bSJeff Roberson 		if (target == 0 && vmd->vmd_laundry_request == VM_LAUNDRY_IDLE)
1103e2068d0bSJeff Roberson 			(void)mtx_sleep(&vmd->vmd_laundry_request,
1104ebcddc72SAlan Cox 			    vm_pagequeue_lockptr(pq), PVM, "launds", 0);
1105ebcddc72SAlan Cox 
1106ebcddc72SAlan Cox 		/*
1107ebcddc72SAlan Cox 		 * If the pagedaemon has indicated that it's in shortfall, start
1108ebcddc72SAlan Cox 		 * a shortfall laundering unless we're already in the middle of
1109ebcddc72SAlan Cox 		 * one.  This may preempt a background laundering.
1110ebcddc72SAlan Cox 		 */
1111e2068d0bSJeff Roberson 		if (vmd->vmd_laundry_request == VM_LAUNDRY_SHORTFALL &&
1112ebcddc72SAlan Cox 		    (!in_shortfall || shortfall_cycle == 0)) {
1113e2068d0bSJeff Roberson 			shortfall = vm_laundry_target(vmd) +
1114e2068d0bSJeff Roberson 			    vmd->vmd_pageout_deficit;
1115ebcddc72SAlan Cox 			target = 0;
1116ebcddc72SAlan Cox 		} else
1117ebcddc72SAlan Cox 			shortfall = 0;
1118ebcddc72SAlan Cox 
1119ebcddc72SAlan Cox 		if (target == 0)
1120e2068d0bSJeff Roberson 			vmd->vmd_laundry_request = VM_LAUNDRY_IDLE;
112160684862SMark Johnston 		nfreed += vmd->vmd_clean_pages_freed;
112260684862SMark Johnston 		vmd->vmd_clean_pages_freed = 0;
1123ebcddc72SAlan Cox 		vm_pagequeue_unlock(pq);
1124ebcddc72SAlan Cox 	}
1125ebcddc72SAlan Cox }
1126ebcddc72SAlan Cox 
1127*5cd29d0fSMark Johnston static int
1128*5cd29d0fSMark Johnston vm_pageout_reinsert_inactive_page(struct scan_state *ss, vm_page_t m)
1129*5cd29d0fSMark Johnston {
1130*5cd29d0fSMark Johnston 	struct vm_domain *vmd;
1131*5cd29d0fSMark Johnston 
1132*5cd29d0fSMark Johnston 	if (!vm_page_inactive(m) || (m->aflags & PGA_ENQUEUED) != 0)
1133*5cd29d0fSMark Johnston 		return (0);
1134*5cd29d0fSMark Johnston 	vm_page_aflag_set(m, PGA_ENQUEUED);
1135*5cd29d0fSMark Johnston 	if ((m->aflags & PGA_REQUEUE_HEAD) != 0) {
1136*5cd29d0fSMark Johnston 		vmd = vm_pagequeue_domain(m);
1137*5cd29d0fSMark Johnston 		TAILQ_INSERT_BEFORE(&vmd->vmd_inacthead, m, plinks.q);
1138*5cd29d0fSMark Johnston 		vm_page_aflag_clear(m, PGA_REQUEUE | PGA_REQUEUE_HEAD);
1139*5cd29d0fSMark Johnston 	} else if ((m->aflags & PGA_REQUEUE) != 0) {
1140*5cd29d0fSMark Johnston 		TAILQ_INSERT_TAIL(&ss->pq->pq_pl, m, plinks.q);
1141*5cd29d0fSMark Johnston 		vm_page_aflag_clear(m, PGA_REQUEUE | PGA_REQUEUE_HEAD);
1142*5cd29d0fSMark Johnston 	} else
1143*5cd29d0fSMark Johnston 		TAILQ_INSERT_BEFORE(ss->marker, m, plinks.q);
1144*5cd29d0fSMark Johnston 	return (1);
1145*5cd29d0fSMark Johnston }
1146*5cd29d0fSMark Johnston 
1147*5cd29d0fSMark Johnston /*
1148*5cd29d0fSMark Johnston  * Re-add stuck pages to the inactive queue.  We will examine them again
1149*5cd29d0fSMark Johnston  * during the next scan.  If the queue state of a page has changed since
1150*5cd29d0fSMark Johnston  * it was physically removed from the page queue in
1151*5cd29d0fSMark Johnston  * vm_pageout_collect_batch(), don't do anything with that page.
1152*5cd29d0fSMark Johnston  */
1153*5cd29d0fSMark Johnston static void
1154*5cd29d0fSMark Johnston vm_pageout_reinsert_inactive(struct scan_state *ss, struct vm_batchqueue *bq,
1155*5cd29d0fSMark Johnston     vm_page_t m)
1156*5cd29d0fSMark Johnston {
1157*5cd29d0fSMark Johnston 	struct vm_pagequeue *pq;
1158*5cd29d0fSMark Johnston 	int delta;
1159*5cd29d0fSMark Johnston 
1160*5cd29d0fSMark Johnston 	delta = 0;
1161*5cd29d0fSMark Johnston 	pq = ss->pq;
1162*5cd29d0fSMark Johnston 
1163*5cd29d0fSMark Johnston 	if (m != NULL) {
1164*5cd29d0fSMark Johnston 		if (vm_batchqueue_insert(bq, m))
1165*5cd29d0fSMark Johnston 			return;
1166*5cd29d0fSMark Johnston 		vm_pagequeue_lock(pq);
1167*5cd29d0fSMark Johnston 		delta += vm_pageout_reinsert_inactive_page(ss, m);
1168*5cd29d0fSMark Johnston 	} else
1169*5cd29d0fSMark Johnston 		vm_pagequeue_lock(pq);
1170*5cd29d0fSMark Johnston 	while ((m = vm_batchqueue_pop(bq)) != NULL)
1171*5cd29d0fSMark Johnston 		delta += vm_pageout_reinsert_inactive_page(ss, m);
1172*5cd29d0fSMark Johnston 	vm_pagequeue_cnt_add(pq, delta);
1173*5cd29d0fSMark Johnston 	vm_pagequeue_unlock(pq);
1174*5cd29d0fSMark Johnston 	vm_batchqueue_init(bq);
1175*5cd29d0fSMark Johnston }
1176*5cd29d0fSMark Johnston 
1177ebcddc72SAlan Cox /*
1178df8bae1dSRodney W. Grimes  *	vm_pageout_scan does the dirty work for the pageout daemon.
1179d9e23210SJeff Roberson  *
1180ebcddc72SAlan Cox  *	pass == 0: Update active LRU/deactivate pages
1181ebcddc72SAlan Cox  *	pass >= 1: Free inactive pages
1182e57dd910SAlan Cox  *
1183e57dd910SAlan Cox  * Returns true if pass was zero or enough pages were freed by the inactive
1184e57dd910SAlan Cox  * queue scan to meet the target.
1185df8bae1dSRodney W. Grimes  */
1186e57dd910SAlan Cox static bool
11875f8cd1c0SJeff Roberson vm_pageout_scan(struct vm_domain *vmd, int pass, int shortage)
1188df8bae1dSRodney W. Grimes {
1189*5cd29d0fSMark Johnston 	struct scan_state ss;
1190*5cd29d0fSMark Johnston 	struct vm_batchqueue rq;
1191*5cd29d0fSMark Johnston 	struct mtx *mtx;
1192*5cd29d0fSMark Johnston 	vm_page_t m, marker;
11938d220203SAlan Cox 	struct vm_pagequeue *pq;
1194df8bae1dSRodney W. Grimes 	vm_object_t object;
119522cf98d1SAlan Cox 	long min_scan;
1196*5cd29d0fSMark Johnston 	int act_delta, addl_page_shortage, deficit, inactq_shortage, max_scan;
1197*5cd29d0fSMark Johnston 	int page_shortage, scan_tick, starting_page_shortage;
1198*5cd29d0fSMark Johnston 	bool obj_locked;
11990d94caffSDavid Greenman 
1200df8bae1dSRodney W. Grimes 	/*
1201d9e23210SJeff Roberson 	 * If we need to reclaim memory ask kernel caches to return
1202c9612b2dSJeff Roberson 	 * some.  We rate limit to avoid thrashing.
1203d9e23210SJeff Roberson 	 */
1204e2068d0bSJeff Roberson 	if (vmd == VM_DOMAIN(0) && pass > 0 &&
1205a6bf3a9eSRyan Stone 	    (time_uptime - lowmem_uptime) >= lowmem_period) {
1206d9e23210SJeff Roberson 		/*
1207855a310fSJeff Roberson 		 * Decrease registered cache sizes.
1208855a310fSJeff Roberson 		 */
120914a0d74eSSteven Hartland 		SDT_PROBE0(vm, , , vm__lowmem_scan);
12109b43bc27SAndriy Gapon 		EVENTHANDLER_INVOKE(vm_lowmem, VM_LOW_PAGES);
1211855a310fSJeff Roberson 		/*
1212d9e23210SJeff Roberson 		 * We do this explicitly after the caches have been
1213d9e23210SJeff Roberson 		 * drained above.
1214855a310fSJeff Roberson 		 */
1215855a310fSJeff Roberson 		uma_reclaim();
1216a6bf3a9eSRyan Stone 		lowmem_uptime = time_uptime;
1217d9e23210SJeff Roberson 	}
12185985940eSJohn Dyson 
1219311e34e2SKonstantin Belousov 	/*
122096240c89SEitan Adler 	 * The addl_page_shortage is the number of temporarily
1221311e34e2SKonstantin Belousov 	 * stuck pages in the inactive queue.  In other words, the
1222449c2e92SKonstantin Belousov 	 * number of pages from the inactive count that should be
1223311e34e2SKonstantin Belousov 	 * discounted in setting the target for the active queue scan.
1224311e34e2SKonstantin Belousov 	 */
12259099545aSAlan Cox 	addl_page_shortage = 0;
12269099545aSAlan Cox 
12271c7c3c6aSMatthew Dillon 	/*
1228e57dd910SAlan Cox 	 * Calculate the number of pages that we want to free.  This number
1229e57dd910SAlan Cox 	 * can be negative if many pages are freed between the wakeup call to
1230e57dd910SAlan Cox 	 * the page daemon and this calculation.
12311c7c3c6aSMatthew Dillon 	 */
123260196cdaSAlan Cox 	if (pass > 0) {
1233e2068d0bSJeff Roberson 		deficit = atomic_readandclear_int(&vmd->vmd_pageout_deficit);
12345f8cd1c0SJeff Roberson 		page_shortage = shortage + deficit;
123560196cdaSAlan Cox 	} else
123660196cdaSAlan Cox 		page_shortage = deficit = 0;
123776386c7eSKonstantin Belousov 	starting_page_shortage = page_shortage;
12381c7c3c6aSMatthew Dillon 
1239*5cd29d0fSMark Johnston 	mtx = NULL;
1240*5cd29d0fSMark Johnston 	obj_locked = false;
1241*5cd29d0fSMark Johnston 	object = NULL;
1242*5cd29d0fSMark Johnston 	vm_batchqueue_init(&rq);
1243*5cd29d0fSMark Johnston 
1244936524aaSMatthew Dillon 	/*
1245f095d1bbSAlan Cox 	 * Start scanning the inactive queue for pages that we can free.  The
1246f095d1bbSAlan Cox 	 * scan will stop when we reach the target or we have scanned the
1247f095d1bbSAlan Cox 	 * entire queue.  (Note that m->act_count is not used to make
1248f095d1bbSAlan Cox 	 * decisions for the inactive queue, only for the active queue.)
12498d220203SAlan Cox 	 */
125064b38930SMark Johnston 	marker = &vmd->vmd_markers[PQ_INACTIVE];
1251*5cd29d0fSMark Johnston 	pq = &vmd->vmd_pagequeues[PQ_INACTIVE];
12528d220203SAlan Cox 	vm_pagequeue_lock(pq);
1253*5cd29d0fSMark Johnston 	vm_pageout_init_scan(&ss, pq, marker, NULL, pq->pq_cnt);
1254*5cd29d0fSMark Johnston 	while (page_shortage > 0 && (m = vm_pageout_next(&ss, true)) != NULL) {
1255*5cd29d0fSMark Johnston 		KASSERT((m->flags & PG_MARKER) == 0,
1256*5cd29d0fSMark Johnston 		    ("marker page %p was dequeued", m));
1257df8bae1dSRodney W. Grimes 
1258*5cd29d0fSMark Johnston 		vm_page_change_lock(m, &mtx);
1259df8bae1dSRodney W. Grimes 
1260*5cd29d0fSMark Johnston recheck:
1261936524aaSMatthew Dillon 		/*
1262*5cd29d0fSMark Johnston 		 * The page may have been disassociated from the queue
1263*5cd29d0fSMark Johnston 		 * while locks were dropped.
1264936524aaSMatthew Dillon 		 */
1265*5cd29d0fSMark Johnston 		if (!vm_pageout_page_queued(m, PQ_INACTIVE)) {
1266*5cd29d0fSMark Johnston 			addl_page_shortage++;
1267936524aaSMatthew Dillon 			continue;
1268*5cd29d0fSMark Johnston 		}
12697900f95dSKonstantin Belousov 
12708c616246SKonstantin Belousov 		/*
1271*5cd29d0fSMark Johnston 		 * The page was re-enqueued after the page queue lock was
1272*5cd29d0fSMark Johnston 		 * dropped, or a requeue was requested.  This page gets a second
1273*5cd29d0fSMark Johnston 		 * chance.
12748c616246SKonstantin Belousov 		 */
1275*5cd29d0fSMark Johnston 		if ((m->aflags & (PGA_ENQUEUED | PGA_REQUEUE |
1276*5cd29d0fSMark Johnston 		    PGA_REQUEUE_HEAD)) != 0)
1277*5cd29d0fSMark Johnston 			goto reinsert;
1278*5cd29d0fSMark Johnston 
12791d3a1bcfSMark Johnston 		/*
1280*5cd29d0fSMark Johnston 		 * Held pages are essentially stuck in the queue.  So,
1281*5cd29d0fSMark Johnston 		 * they ought to be discounted from the inactive count.
1282*5cd29d0fSMark Johnston 		 * See the calculation of inactq_shortage before the
1283a3aeedabSAlan Cox 		 * loop over the active queue below.
1284*5cd29d0fSMark Johnston 		 *
1285*5cd29d0fSMark Johnston 		 * Wired pages may not be freed.  Complete their removal
1286*5cd29d0fSMark Johnston 		 * from the queue now to avoid needless revisits during
1287*5cd29d0fSMark Johnston 		 * future scans.
1288a3aeedabSAlan Cox 		 */
1289*5cd29d0fSMark Johnston 		if (m->hold_count != 0) {
1290a3aeedabSAlan Cox 			addl_page_shortage++;
1291*5cd29d0fSMark Johnston 			goto reinsert;
1292*5cd29d0fSMark Johnston 		}
1293*5cd29d0fSMark Johnston 		if (m->wire_count != 0) {
1294*5cd29d0fSMark Johnston 			addl_page_shortage++;
1295*5cd29d0fSMark Johnston 			vm_page_dequeue_deferred(m);
1296*5cd29d0fSMark Johnston 			continue;
1297*5cd29d0fSMark Johnston 		}
1298*5cd29d0fSMark Johnston 
1299*5cd29d0fSMark Johnston 		if (object != m->object) {
1300*5cd29d0fSMark Johnston 			if (obj_locked) {
1301*5cd29d0fSMark Johnston 				VM_OBJECT_WUNLOCK(object);
1302*5cd29d0fSMark Johnston 				obj_locked = false;
1303df8bae1dSRodney W. Grimes 			}
13049ee2165fSAlan Cox 			object = m->object;
1305*5cd29d0fSMark Johnston 		}
1306*5cd29d0fSMark Johnston 		if (!obj_locked) {
1307a3aeedabSAlan Cox 			if (!VM_OBJECT_TRYWLOCK(object)) {
1308*5cd29d0fSMark Johnston 				mtx_unlock(mtx);
1309*5cd29d0fSMark Johnston 				/* Depends on type-stability. */
1310*5cd29d0fSMark Johnston 				VM_OBJECT_WLOCK(object);
1311*5cd29d0fSMark Johnston 				obj_locked = true;
1312*5cd29d0fSMark Johnston 				mtx_lock(mtx);
1313*5cd29d0fSMark Johnston 				goto recheck;
1314*5cd29d0fSMark Johnston 			} else
1315*5cd29d0fSMark Johnston 				obj_locked = true;
1316a3aeedabSAlan Cox 		}
1317*5cd29d0fSMark Johnston 
1318a3aeedabSAlan Cox 		if (vm_page_busied(m)) {
1319a3aeedabSAlan Cox 			/*
1320a3aeedabSAlan Cox 			 * Don't mess with busy pages.  Leave them at
1321a3aeedabSAlan Cox 			 * the front of the queue.  Most likely, they
1322a3aeedabSAlan Cox 			 * are being paged out and will leave the
1323a3aeedabSAlan Cox 			 * queue shortly after the scan finishes.  So,
1324a3aeedabSAlan Cox 			 * they ought to be discounted from the
1325a3aeedabSAlan Cox 			 * inactive count.
1326a3aeedabSAlan Cox 			 */
1327a3aeedabSAlan Cox 			addl_page_shortage++;
1328*5cd29d0fSMark Johnston 			goto reinsert;
132926f9a767SRodney W. Grimes 		}
133048cc2fc7SKonstantin Belousov 
133148cc2fc7SKonstantin Belousov 		/*
13328748f58cSKonstantin Belousov 		 * Invalid pages can be easily freed. They cannot be
13338748f58cSKonstantin Belousov 		 * mapped, vm_page_free() asserts this.
1334776f729cSKonstantin Belousov 		 */
13358748f58cSKonstantin Belousov 		if (m->valid == 0)
13368748f58cSKonstantin Belousov 			goto free_page;
1337776f729cSKonstantin Belousov 
1338776f729cSKonstantin Belousov 		/*
1339960810ccSAlan Cox 		 * If the page has been referenced and the object is not dead,
1340960810ccSAlan Cox 		 * reactivate or requeue the page depending on whether the
1341960810ccSAlan Cox 		 * object is mapped.
13427e006499SJohn Dyson 		 */
1343bb7858eaSJeff Roberson 		if ((m->aflags & PGA_REFERENCED) != 0) {
1344bb7858eaSJeff Roberson 			vm_page_aflag_clear(m, PGA_REFERENCED);
1345bb7858eaSJeff Roberson 			act_delta = 1;
134686fa2471SAlan Cox 		} else
134786fa2471SAlan Cox 			act_delta = 0;
1348bb7858eaSJeff Roberson 		if (object->ref_count != 0) {
1349bb7858eaSJeff Roberson 			act_delta += pmap_ts_referenced(m);
1350bb7858eaSJeff Roberson 		} else {
1351bb7858eaSJeff Roberson 			KASSERT(!pmap_page_is_mapped(m),
1352bb7858eaSJeff Roberson 			    ("vm_pageout_scan: page %p is mapped", m));
13532fe6e4d7SDavid Greenman 		}
1354bb7858eaSJeff Roberson 		if (act_delta != 0) {
135586fa2471SAlan Cox 			if (object->ref_count != 0) {
135683c9dea1SGleb Smirnoff 				VM_CNT_INC(v_reactivated);
135726f9a767SRodney W. Grimes 				vm_page_activate(m);
1358960810ccSAlan Cox 
1359960810ccSAlan Cox 				/*
1360960810ccSAlan Cox 				 * Increase the activation count if the page
1361960810ccSAlan Cox 				 * was referenced while in the inactive queue.
1362960810ccSAlan Cox 				 * This makes it less likely that the page will
1363960810ccSAlan Cox 				 * be returned prematurely to the inactive
1364960810ccSAlan Cox 				 * queue.
1365960810ccSAlan Cox  				 */
1366bb7858eaSJeff Roberson 				m->act_count += act_delta + ACT_ADVANCE;
1367*5cd29d0fSMark Johnston 				continue;
1368ebcddc72SAlan Cox 			} else if ((object->flags & OBJ_DEAD) == 0) {
1369*5cd29d0fSMark Johnston 				vm_page_aflag_set(m, PGA_REQUEUE);
1370*5cd29d0fSMark Johnston 				goto reinsert;
1371ebcddc72SAlan Cox 			}
1372960810ccSAlan Cox 		}
137367bf6868SJohn Dyson 
13747e006499SJohn Dyson 		/*
13759fc4739dSAlan Cox 		 * If the page appears to be clean at the machine-independent
13769fc4739dSAlan Cox 		 * layer, then remove all of its mappings from the pmap in
1377a766ffd0SAlan Cox 		 * anticipation of freeing it.  If, however, any of the page's
1378a766ffd0SAlan Cox 		 * mappings allow write access, then the page may still be
1379a766ffd0SAlan Cox 		 * modified until the last of those mappings are removed.
13807e006499SJohn Dyson 		 */
1381aa044135SAlan Cox 		if (object->ref_count != 0) {
13829fc4739dSAlan Cox 			vm_page_test_dirty(m);
1383aa044135SAlan Cox 			if (m->dirty == 0)
1384b78ddb0bSAlan Cox 				pmap_remove_all(m);
1385aa044135SAlan Cox 		}
1386dcbcd518SBruce Evans 
13876989c456SAlan Cox 		/*
1388ebcddc72SAlan Cox 		 * Clean pages can be freed, but dirty pages must be sent back
1389ebcddc72SAlan Cox 		 * to the laundry, unless they belong to a dead object.
1390ebcddc72SAlan Cox 		 * Requeueing dirty pages from dead objects is pointless, as
1391ebcddc72SAlan Cox 		 * they are being paged out and freed by the thread that
1392ebcddc72SAlan Cox 		 * destroyed the object.
13936989c456SAlan Cox 		 */
1394ebcddc72SAlan Cox 		if (m->dirty == 0) {
13958748f58cSKonstantin Belousov free_page:
1396*5cd29d0fSMark Johnston 			/*
1397*5cd29d0fSMark Johnston 			 * Because we dequeued the page and have already
1398*5cd29d0fSMark Johnston 			 * checked for concurrent dequeue and enqueue
1399*5cd29d0fSMark Johnston 			 * requests, we can safely disassociate the page
1400*5cd29d0fSMark Johnston 			 * from the inactive queue.
1401*5cd29d0fSMark Johnston 			 */
1402*5cd29d0fSMark Johnston 			KASSERT((m->aflags & PGA_QUEUE_STATE_MASK) == 0,
1403*5cd29d0fSMark Johnston 			    ("page %p has queue state", m));
1404*5cd29d0fSMark Johnston 			m->queue = PQ_NONE;
140578afdce6SAlan Cox 			vm_page_free(m);
1406*5cd29d0fSMark Johnston 			page_shortage--;
1407ebcddc72SAlan Cox 		} else if ((object->flags & OBJ_DEAD) == 0)
1408ebcddc72SAlan Cox 			vm_page_launder(m);
1409*5cd29d0fSMark Johnston 		continue;
1410*5cd29d0fSMark Johnston reinsert:
1411*5cd29d0fSMark Johnston 		vm_pageout_reinsert_inactive(&ss, &rq, m);
1412*5cd29d0fSMark Johnston 	}
1413*5cd29d0fSMark Johnston 	if (mtx != NULL) {
1414*5cd29d0fSMark Johnston 		mtx_unlock(mtx);
1415*5cd29d0fSMark Johnston 		mtx = NULL;
1416*5cd29d0fSMark Johnston 	}
1417*5cd29d0fSMark Johnston 	if (obj_locked) {
141889f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
1419*5cd29d0fSMark Johnston 		obj_locked = false;
1420*5cd29d0fSMark Johnston 	}
1421*5cd29d0fSMark Johnston 	vm_pageout_reinsert_inactive(&ss, &rq, NULL);
1422*5cd29d0fSMark Johnston 	vm_pageout_reinsert_inactive(&ss, &ss.bq, NULL);
14238d220203SAlan Cox 	vm_pagequeue_lock(pq);
1424*5cd29d0fSMark Johnston 	vm_pageout_end_scan(&ss);
14258d220203SAlan Cox 	vm_pagequeue_unlock(pq);
142626f9a767SRodney W. Grimes 
1427*5cd29d0fSMark Johnston 	VM_CNT_ADD(v_dfree, starting_page_shortage - page_shortage);
1428*5cd29d0fSMark Johnston 
1429ebcddc72SAlan Cox 	/*
1430ebcddc72SAlan Cox 	 * Wake up the laundry thread so that it can perform any needed
1431ebcddc72SAlan Cox 	 * laundering.  If we didn't meet our target, we're in shortfall and
1432b1fd102eSMark Johnston 	 * need to launder more aggressively.  If PQ_LAUNDRY is empty and no
1433b1fd102eSMark Johnston 	 * swap devices are configured, the laundry thread has no work to do, so
1434b1fd102eSMark Johnston 	 * don't bother waking it up.
1435cb35676eSMark Johnston 	 *
1436cb35676eSMark Johnston 	 * The laundry thread uses the number of inactive queue scans elapsed
1437cb35676eSMark Johnston 	 * since the last laundering to determine whether to launder again, so
1438cb35676eSMark Johnston 	 * keep count.
1439ebcddc72SAlan Cox 	 */
1440cb35676eSMark Johnston 	if (starting_page_shortage > 0) {
1441e2068d0bSJeff Roberson 		pq = &vmd->vmd_pagequeues[PQ_LAUNDRY];
1442ebcddc72SAlan Cox 		vm_pagequeue_lock(pq);
1443e2068d0bSJeff Roberson 		if (vmd->vmd_laundry_request == VM_LAUNDRY_IDLE &&
1444cb35676eSMark Johnston 		    (pq->pq_cnt > 0 || atomic_load_acq_int(&swapdev_enabled))) {
1445ebcddc72SAlan Cox 			if (page_shortage > 0) {
1446e2068d0bSJeff Roberson 				vmd->vmd_laundry_request = VM_LAUNDRY_SHORTFALL;
144783c9dea1SGleb Smirnoff 				VM_CNT_INC(v_pdshortfalls);
1448e2068d0bSJeff Roberson 			} else if (vmd->vmd_laundry_request !=
1449e2068d0bSJeff Roberson 			    VM_LAUNDRY_SHORTFALL)
1450e2068d0bSJeff Roberson 				vmd->vmd_laundry_request =
1451e2068d0bSJeff Roberson 				    VM_LAUNDRY_BACKGROUND;
1452e2068d0bSJeff Roberson 			wakeup(&vmd->vmd_laundry_request);
1453b1fd102eSMark Johnston 		}
145460684862SMark Johnston 		vmd->vmd_clean_pages_freed +=
145560684862SMark Johnston 		    starting_page_shortage - page_shortage;
1456ebcddc72SAlan Cox 		vm_pagequeue_unlock(pq);
1457ebcddc72SAlan Cox 	}
1458ebcddc72SAlan Cox 
14599452b5edSAlan Cox 	/*
1460f095d1bbSAlan Cox 	 * Wakeup the swapout daemon if we didn't free the targeted number of
1461f095d1bbSAlan Cox 	 * pages.
14629452b5edSAlan Cox 	 */
1463ac04195bSKonstantin Belousov 	if (page_shortage > 0)
1464ac04195bSKonstantin Belousov 		vm_swapout_run();
14659452b5edSAlan Cox 
14669452b5edSAlan Cox 	/*
146776386c7eSKonstantin Belousov 	 * If the inactive queue scan fails repeatedly to meet its
146876386c7eSKonstantin Belousov 	 * target, kill the largest process.
146976386c7eSKonstantin Belousov 	 */
147076386c7eSKonstantin Belousov 	vm_pageout_mightbe_oom(vmd, page_shortage, starting_page_shortage);
147176386c7eSKonstantin Belousov 
147276386c7eSKonstantin Belousov 	/*
1473936524aaSMatthew Dillon 	 * Compute the number of pages we want to try to move from the
1474ebcddc72SAlan Cox 	 * active queue to either the inactive or laundry queue.
1475ebcddc72SAlan Cox 	 *
1476ebcddc72SAlan Cox 	 * When scanning active pages, we make clean pages count more heavily
1477ebcddc72SAlan Cox 	 * towards the page shortage than dirty pages.  This is because dirty
1478ebcddc72SAlan Cox 	 * pages must be laundered before they can be reused and thus have less
1479ebcddc72SAlan Cox 	 * utility when attempting to quickly alleviate a shortage.  However,
1480ebcddc72SAlan Cox 	 * this weighting also causes the scan to deactivate dirty pages more
1481ebcddc72SAlan Cox 	 * more aggressively, improving the effectiveness of clustering and
1482ebcddc72SAlan Cox 	 * ensuring that they can eventually be reused.
14831c7c3c6aSMatthew Dillon 	 */
1484e2068d0bSJeff Roberson 	inactq_shortage = vmd->vmd_inactive_target - (pq->pq_cnt +
1485e2068d0bSJeff Roberson 	    vmd->vmd_pagequeues[PQ_LAUNDRY].pq_cnt / act_scan_laundry_weight) +
148659d3150bSMark Johnston 	    vm_paging_target(vmd) + deficit + addl_page_shortage;
148782e2d06aSMark Johnston 	inactq_shortage *= act_scan_laundry_weight;
14889099545aSAlan Cox 
1489*5cd29d0fSMark Johnston 	marker = &vmd->vmd_markers[PQ_ACTIVE];
1490114f62c6SJeff Roberson 	pq = &vmd->vmd_pagequeues[PQ_ACTIVE];
1491114f62c6SJeff Roberson 	vm_pagequeue_lock(pq);
14929099545aSAlan Cox 
1493d9e23210SJeff Roberson 	/*
1494d9e23210SJeff Roberson 	 * If we're just idle polling attempt to visit every
1495d9e23210SJeff Roberson 	 * active page within 'update_period' seconds.
1496d9e23210SJeff Roberson 	 */
149722cf98d1SAlan Cox 	scan_tick = ticks;
149822cf98d1SAlan Cox 	if (vm_pageout_update_period != 0) {
149922cf98d1SAlan Cox 		min_scan = pq->pq_cnt;
150022cf98d1SAlan Cox 		min_scan *= scan_tick - vmd->vmd_last_active_scan;
150122cf98d1SAlan Cox 		min_scan /= hz * vm_pageout_update_period;
150222cf98d1SAlan Cox 	} else
150322cf98d1SAlan Cox 		min_scan = 0;
1504*5cd29d0fSMark Johnston 	if (min_scan > 0 || (inactq_shortage > 0 && pq->pq_cnt > 0))
150522cf98d1SAlan Cox 		vmd->vmd_last_active_scan = scan_tick;
15061c7c3c6aSMatthew Dillon 
15071c7c3c6aSMatthew Dillon 	/*
150822cf98d1SAlan Cox 	 * Scan the active queue for pages that can be deactivated.  Update
150922cf98d1SAlan Cox 	 * the per-page activity counter and use it to identify deactivation
151079144408SAlan Cox 	 * candidates.  Held pages may be deactivated.
1511*5cd29d0fSMark Johnston 	 *
1512*5cd29d0fSMark Johnston 	 * To avoid requeuing each page that remains in the active queue, we
1513*5cd29d0fSMark Johnston 	 * implement the CLOCK algorithm.  To maintain consistency in the
1514*5cd29d0fSMark Johnston 	 * generic page queue code, pages are inserted at the tail of the
1515*5cd29d0fSMark Johnston 	 * active queue.  We thus use two hands, represented by marker pages:
1516*5cd29d0fSMark Johnston 	 * scans begin at the first hand, which precedes the second hand in
1517*5cd29d0fSMark Johnston 	 * the queue.  When the two hands meet, they are moved back to the
1518*5cd29d0fSMark Johnston 	 * head and tail of the queue, respectively, and scanning resumes.
15191c7c3c6aSMatthew Dillon 	 */
1520*5cd29d0fSMark Johnston 	max_scan = inactq_shortage > 0 ? pq->pq_cnt : min_scan;
1521*5cd29d0fSMark Johnston act_scan:
1522*5cd29d0fSMark Johnston 	vm_pageout_init_scan(&ss, pq, marker, &vmd->vmd_clock[0], max_scan);
1523*5cd29d0fSMark Johnston 	while ((m = vm_pageout_next(&ss, false)) != NULL) {
1524*5cd29d0fSMark Johnston 		if (__predict_false(m == &vmd->vmd_clock[1])) {
1525*5cd29d0fSMark Johnston 			vm_pagequeue_lock(pq);
1526*5cd29d0fSMark Johnston 			TAILQ_REMOVE(&pq->pq_pl, &vmd->vmd_clock[0], plinks.q);
1527*5cd29d0fSMark Johnston 			TAILQ_REMOVE(&pq->pq_pl, &vmd->vmd_clock[1], plinks.q);
1528*5cd29d0fSMark Johnston 			TAILQ_INSERT_HEAD(&pq->pq_pl, &vmd->vmd_clock[0],
1529*5cd29d0fSMark Johnston 			    plinks.q);
1530*5cd29d0fSMark Johnston 			TAILQ_INSERT_TAIL(&pq->pq_pl, &vmd->vmd_clock[1],
1531*5cd29d0fSMark Johnston 			    plinks.q);
1532*5cd29d0fSMark Johnston 			max_scan -= ss.scanned;
1533*5cd29d0fSMark Johnston 			vm_pageout_end_scan(&ss);
1534*5cd29d0fSMark Johnston 			goto act_scan;
15352965a453SKip Macy 		}
1536*5cd29d0fSMark Johnston 		if (__predict_false((m->flags & PG_MARKER) != 0))
1537*5cd29d0fSMark Johnston 			continue;
1538*5cd29d0fSMark Johnston 
1539*5cd29d0fSMark Johnston 		vm_page_change_lock(m, &mtx);
1540b18bfc3dSJohn Dyson 
1541b18bfc3dSJohn Dyson 		/*
1542*5cd29d0fSMark Johnston 		 * The page may have been disassociated from the queue
1543*5cd29d0fSMark Johnston 		 * while locks were dropped.
1544b18bfc3dSJohn Dyson 		 */
1545*5cd29d0fSMark Johnston 		if (!vm_pageout_page_queued(m, PQ_ACTIVE))
1546*5cd29d0fSMark Johnston 			continue;
1547ef743ce6SJohn Dyson 
15487e006499SJohn Dyson 		/*
15491d3a1bcfSMark Johnston 		 * Wired pages are dequeued lazily.
15501d3a1bcfSMark Johnston 		 */
15511d3a1bcfSMark Johnston 		if (m->wire_count != 0) {
1552*5cd29d0fSMark Johnston 			vm_page_dequeue_deferred(m);
15531d3a1bcfSMark Johnston 			continue;
15541d3a1bcfSMark Johnston 		}
15551d3a1bcfSMark Johnston 
15561d3a1bcfSMark Johnston 		/*
15577e006499SJohn Dyson 		 * Check to see "how much" the page has been used.
15587e006499SJohn Dyson 		 */
155986fa2471SAlan Cox 		if ((m->aflags & PGA_REFERENCED) != 0) {
1560bb7858eaSJeff Roberson 			vm_page_aflag_clear(m, PGA_REFERENCED);
156186fa2471SAlan Cox 			act_delta = 1;
156286fa2471SAlan Cox 		} else
156386fa2471SAlan Cox 			act_delta = 0;
156486fa2471SAlan Cox 
1565274132acSJeff Roberson 		/*
156679144408SAlan Cox 		 * Perform an unsynchronized object ref count check.  While
156779144408SAlan Cox 		 * the page lock ensures that the page is not reallocated to
156879144408SAlan Cox 		 * another object, in particular, one with unmanaged mappings
156979144408SAlan Cox 		 * that cannot support pmap_ts_referenced(), two races are,
157079144408SAlan Cox 		 * nonetheless, possible:
157179144408SAlan Cox 		 * 1) The count was transitioning to zero, but we saw a non-
157279144408SAlan Cox 		 *    zero value.  pmap_ts_referenced() will return zero
157379144408SAlan Cox 		 *    because the page is not mapped.
157479144408SAlan Cox 		 * 2) The count was transitioning to one, but we saw zero.
157579144408SAlan Cox 		 *    This race delays the detection of a new reference.  At
157679144408SAlan Cox 		 *    worst, we will deactivate and reactivate the page.
1577274132acSJeff Roberson 		 */
1578274132acSJeff Roberson 		if (m->object->ref_count != 0)
1579bb7858eaSJeff Roberson 			act_delta += pmap_ts_referenced(m);
1580bb7858eaSJeff Roberson 
1581bb7858eaSJeff Roberson 		/*
1582bb7858eaSJeff Roberson 		 * Advance or decay the act_count based on recent usage.
1583bb7858eaSJeff Roberson 		 */
158486fa2471SAlan Cox 		if (act_delta != 0) {
1585bb7858eaSJeff Roberson 			m->act_count += ACT_ADVANCE + act_delta;
158638efa82bSJohn Dyson 			if (m->act_count > ACT_MAX)
158738efa82bSJohn Dyson 				m->act_count = ACT_MAX;
158886fa2471SAlan Cox 		} else
158938efa82bSJohn Dyson 			m->act_count -= min(m->act_count, ACT_DECLINE);
1590bb7858eaSJeff Roberson 
159186fa2471SAlan Cox 		if (m->act_count == 0) {
1592ebcddc72SAlan Cox 			/*
1593ebcddc72SAlan Cox 			 * When not short for inactive pages, let dirty pages go
1594ebcddc72SAlan Cox 			 * through the inactive queue before moving to the
1595ebcddc72SAlan Cox 			 * laundry queues.  This gives them some extra time to
1596ebcddc72SAlan Cox 			 * be reactivated, potentially avoiding an expensive
1597ebcddc72SAlan Cox 			 * pageout.  During a page shortage, the inactive queue
1598ebcddc72SAlan Cox 			 * is necessarily small, so we may move dirty pages
1599ebcddc72SAlan Cox 			 * directly to the laundry queue.
1600ebcddc72SAlan Cox 			 */
1601ebcddc72SAlan Cox 			if (inactq_shortage <= 0)
1602d4a272dbSJohn Dyson 				vm_page_deactivate(m);
1603ebcddc72SAlan Cox 			else {
1604ebcddc72SAlan Cox 				/*
1605ebcddc72SAlan Cox 				 * Calling vm_page_test_dirty() here would
1606ebcddc72SAlan Cox 				 * require acquisition of the object's write
1607ebcddc72SAlan Cox 				 * lock.  However, during a page shortage,
1608ebcddc72SAlan Cox 				 * directing dirty pages into the laundry
1609ebcddc72SAlan Cox 				 * queue is only an optimization and not a
1610ebcddc72SAlan Cox 				 * requirement.  Therefore, we simply rely on
1611ebcddc72SAlan Cox 				 * the opportunistic updates to the page's
1612ebcddc72SAlan Cox 				 * dirty field by the pmap.
1613ebcddc72SAlan Cox 				 */
1614ebcddc72SAlan Cox 				if (m->dirty == 0) {
1615ebcddc72SAlan Cox 					vm_page_deactivate(m);
1616ebcddc72SAlan Cox 					inactq_shortage -=
1617ebcddc72SAlan Cox 					    act_scan_laundry_weight;
1618ebcddc72SAlan Cox 				} else {
1619ebcddc72SAlan Cox 					vm_page_launder(m);
1620e57dd910SAlan Cox 					inactq_shortage--;
1621ebcddc72SAlan Cox 				}
1622ebcddc72SAlan Cox 			}
162326f9a767SRodney W. Grimes 		}
1624*5cd29d0fSMark Johnston 	}
1625*5cd29d0fSMark Johnston 	if (mtx != NULL) {
1626*5cd29d0fSMark Johnston 		mtx_unlock(mtx);
1627*5cd29d0fSMark Johnston 		mtx = NULL;
1628*5cd29d0fSMark Johnston 	}
1629*5cd29d0fSMark Johnston 	vm_pagequeue_lock(pq);
1630*5cd29d0fSMark Johnston 	TAILQ_REMOVE(&pq->pq_pl, &vmd->vmd_clock[0], plinks.q);
1631*5cd29d0fSMark Johnston 	TAILQ_INSERT_AFTER(&pq->pq_pl, marker, &vmd->vmd_clock[0], plinks.q);
1632*5cd29d0fSMark Johnston 	vm_pageout_end_scan(&ss);
16338d220203SAlan Cox 	vm_pagequeue_unlock(pq);
1634*5cd29d0fSMark Johnston 
1635ac04195bSKonstantin Belousov 	if (pass > 0)
1636ac04195bSKonstantin Belousov 		vm_swapout_run_idle();
1637e57dd910SAlan Cox 	return (page_shortage <= 0);
16382025d69bSKonstantin Belousov }
16392025d69bSKonstantin Belousov 
1640449c2e92SKonstantin Belousov static int vm_pageout_oom_vote;
1641449c2e92SKonstantin Belousov 
1642449c2e92SKonstantin Belousov /*
1643449c2e92SKonstantin Belousov  * The pagedaemon threads randlomly select one to perform the
1644449c2e92SKonstantin Belousov  * OOM.  Trying to kill processes before all pagedaemons
1645449c2e92SKonstantin Belousov  * failed to reach free target is premature.
1646449c2e92SKonstantin Belousov  */
1647449c2e92SKonstantin Belousov static void
164876386c7eSKonstantin Belousov vm_pageout_mightbe_oom(struct vm_domain *vmd, int page_shortage,
164976386c7eSKonstantin Belousov     int starting_page_shortage)
1650449c2e92SKonstantin Belousov {
1651449c2e92SKonstantin Belousov 	int old_vote;
1652449c2e92SKonstantin Belousov 
165376386c7eSKonstantin Belousov 	if (starting_page_shortage <= 0 || starting_page_shortage !=
165476386c7eSKonstantin Belousov 	    page_shortage)
165576386c7eSKonstantin Belousov 		vmd->vmd_oom_seq = 0;
165676386c7eSKonstantin Belousov 	else
165776386c7eSKonstantin Belousov 		vmd->vmd_oom_seq++;
165876386c7eSKonstantin Belousov 	if (vmd->vmd_oom_seq < vm_pageout_oom_seq) {
1659449c2e92SKonstantin Belousov 		if (vmd->vmd_oom) {
1660449c2e92SKonstantin Belousov 			vmd->vmd_oom = FALSE;
1661449c2e92SKonstantin Belousov 			atomic_subtract_int(&vm_pageout_oom_vote, 1);
1662449c2e92SKonstantin Belousov 		}
1663449c2e92SKonstantin Belousov 		return;
1664449c2e92SKonstantin Belousov 	}
1665449c2e92SKonstantin Belousov 
166676386c7eSKonstantin Belousov 	/*
166776386c7eSKonstantin Belousov 	 * Do not follow the call sequence until OOM condition is
166876386c7eSKonstantin Belousov 	 * cleared.
166976386c7eSKonstantin Belousov 	 */
167076386c7eSKonstantin Belousov 	vmd->vmd_oom_seq = 0;
167176386c7eSKonstantin Belousov 
1672449c2e92SKonstantin Belousov 	if (vmd->vmd_oom)
1673449c2e92SKonstantin Belousov 		return;
1674449c2e92SKonstantin Belousov 
1675449c2e92SKonstantin Belousov 	vmd->vmd_oom = TRUE;
1676449c2e92SKonstantin Belousov 	old_vote = atomic_fetchadd_int(&vm_pageout_oom_vote, 1);
1677449c2e92SKonstantin Belousov 	if (old_vote != vm_ndomains - 1)
1678449c2e92SKonstantin Belousov 		return;
1679449c2e92SKonstantin Belousov 
1680449c2e92SKonstantin Belousov 	/*
1681449c2e92SKonstantin Belousov 	 * The current pagedaemon thread is the last in the quorum to
1682449c2e92SKonstantin Belousov 	 * start OOM.  Initiate the selection and signaling of the
1683449c2e92SKonstantin Belousov 	 * victim.
1684449c2e92SKonstantin Belousov 	 */
1685449c2e92SKonstantin Belousov 	vm_pageout_oom(VM_OOM_MEM);
1686449c2e92SKonstantin Belousov 
1687449c2e92SKonstantin Belousov 	/*
1688449c2e92SKonstantin Belousov 	 * After one round of OOM terror, recall our vote.  On the
1689449c2e92SKonstantin Belousov 	 * next pass, current pagedaemon would vote again if the low
1690449c2e92SKonstantin Belousov 	 * memory condition is still there, due to vmd_oom being
1691449c2e92SKonstantin Belousov 	 * false.
1692449c2e92SKonstantin Belousov 	 */
1693449c2e92SKonstantin Belousov 	vmd->vmd_oom = FALSE;
1694449c2e92SKonstantin Belousov 	atomic_subtract_int(&vm_pageout_oom_vote, 1);
1695449c2e92SKonstantin Belousov }
16962025d69bSKonstantin Belousov 
16973949873fSKonstantin Belousov /*
16983949873fSKonstantin Belousov  * The OOM killer is the page daemon's action of last resort when
16993949873fSKonstantin Belousov  * memory allocation requests have been stalled for a prolonged period
17003949873fSKonstantin Belousov  * of time because it cannot reclaim memory.  This function computes
17013949873fSKonstantin Belousov  * the approximate number of physical pages that could be reclaimed if
17023949873fSKonstantin Belousov  * the specified address space is destroyed.
17033949873fSKonstantin Belousov  *
17043949873fSKonstantin Belousov  * Private, anonymous memory owned by the address space is the
17053949873fSKonstantin Belousov  * principal resource that we expect to recover after an OOM kill.
17063949873fSKonstantin Belousov  * Since the physical pages mapped by the address space's COW entries
17073949873fSKonstantin Belousov  * are typically shared pages, they are unlikely to be released and so
17083949873fSKonstantin Belousov  * they are not counted.
17093949873fSKonstantin Belousov  *
17103949873fSKonstantin Belousov  * To get to the point where the page daemon runs the OOM killer, its
17113949873fSKonstantin Belousov  * efforts to write-back vnode-backed pages may have stalled.  This
17123949873fSKonstantin Belousov  * could be caused by a memory allocation deadlock in the write path
17133949873fSKonstantin Belousov  * that might be resolved by an OOM kill.  Therefore, physical pages
17143949873fSKonstantin Belousov  * belonging to vnode-backed objects are counted, because they might
17153949873fSKonstantin Belousov  * be freed without being written out first if the address space holds
17163949873fSKonstantin Belousov  * the last reference to an unlinked vnode.
17173949873fSKonstantin Belousov  *
17183949873fSKonstantin Belousov  * Similarly, physical pages belonging to OBJT_PHYS objects are
17193949873fSKonstantin Belousov  * counted because the address space might hold the last reference to
17203949873fSKonstantin Belousov  * the object.
17213949873fSKonstantin Belousov  */
17223949873fSKonstantin Belousov static long
17233949873fSKonstantin Belousov vm_pageout_oom_pagecount(struct vmspace *vmspace)
17243949873fSKonstantin Belousov {
17253949873fSKonstantin Belousov 	vm_map_t map;
17263949873fSKonstantin Belousov 	vm_map_entry_t entry;
17273949873fSKonstantin Belousov 	vm_object_t obj;
17283949873fSKonstantin Belousov 	long res;
17293949873fSKonstantin Belousov 
17303949873fSKonstantin Belousov 	map = &vmspace->vm_map;
17313949873fSKonstantin Belousov 	KASSERT(!map->system_map, ("system map"));
17323949873fSKonstantin Belousov 	sx_assert(&map->lock, SA_LOCKED);
17333949873fSKonstantin Belousov 	res = 0;
17343949873fSKonstantin Belousov 	for (entry = map->header.next; entry != &map->header;
17353949873fSKonstantin Belousov 	    entry = entry->next) {
17363949873fSKonstantin Belousov 		if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0)
17373949873fSKonstantin Belousov 			continue;
17383949873fSKonstantin Belousov 		obj = entry->object.vm_object;
17393949873fSKonstantin Belousov 		if (obj == NULL)
17403949873fSKonstantin Belousov 			continue;
17413949873fSKonstantin Belousov 		if ((entry->eflags & MAP_ENTRY_NEEDS_COPY) != 0 &&
17423949873fSKonstantin Belousov 		    obj->ref_count != 1)
17433949873fSKonstantin Belousov 			continue;
17443949873fSKonstantin Belousov 		switch (obj->type) {
17453949873fSKonstantin Belousov 		case OBJT_DEFAULT:
17463949873fSKonstantin Belousov 		case OBJT_SWAP:
17473949873fSKonstantin Belousov 		case OBJT_PHYS:
17483949873fSKonstantin Belousov 		case OBJT_VNODE:
17493949873fSKonstantin Belousov 			res += obj->resident_page_count;
17503949873fSKonstantin Belousov 			break;
17513949873fSKonstantin Belousov 		}
17523949873fSKonstantin Belousov 	}
17533949873fSKonstantin Belousov 	return (res);
17543949873fSKonstantin Belousov }
17553949873fSKonstantin Belousov 
17562025d69bSKonstantin Belousov void
17572025d69bSKonstantin Belousov vm_pageout_oom(int shortage)
17582025d69bSKonstantin Belousov {
17592025d69bSKonstantin Belousov 	struct proc *p, *bigproc;
17602025d69bSKonstantin Belousov 	vm_offset_t size, bigsize;
17612025d69bSKonstantin Belousov 	struct thread *td;
17626bed074cSKonstantin Belousov 	struct vmspace *vm;
17633e78e983SAlan Cox 	bool breakout;
17642025d69bSKonstantin Belousov 
17652025d69bSKonstantin Belousov 	/*
17661c58e4e5SJohn Baldwin 	 * We keep the process bigproc locked once we find it to keep anyone
17671c58e4e5SJohn Baldwin 	 * from messing with it; however, there is a possibility of
176828323addSBryan Drewery 	 * deadlock if process B is bigproc and one of its child processes
17691c58e4e5SJohn Baldwin 	 * attempts to propagate a signal to B while we are waiting for A's
17701c58e4e5SJohn Baldwin 	 * lock while walking this list.  To avoid this, we don't block on
17711c58e4e5SJohn Baldwin 	 * the process lock but just skip a process if it is already locked.
17725663e6deSDavid Greenman 	 */
17735663e6deSDavid Greenman 	bigproc = NULL;
17745663e6deSDavid Greenman 	bigsize = 0;
17751005a129SJohn Baldwin 	sx_slock(&allproc_lock);
1776e602ba25SJulian Elischer 	FOREACH_PROC_IN_SYSTEM(p) {
177771943c3dSKonstantin Belousov 		PROC_LOCK(p);
177871943c3dSKonstantin Belousov 
17791c58e4e5SJohn Baldwin 		/*
17803f1c4c4fSKonstantin Belousov 		 * If this is a system, protected or killed process, skip it.
17815663e6deSDavid Greenman 		 */
178271943c3dSKonstantin Belousov 		if (p->p_state != PRS_NORMAL || (p->p_flag & (P_INEXEC |
178371943c3dSKonstantin Belousov 		    P_PROTECTED | P_SYSTEM | P_WEXIT)) != 0 ||
178471943c3dSKonstantin Belousov 		    p->p_pid == 1 || P_KILLED(p) ||
178571943c3dSKonstantin Belousov 		    (p->p_pid < 48 && swap_pager_avail != 0)) {
17868606d880SJohn Baldwin 			PROC_UNLOCK(p);
17875663e6deSDavid Greenman 			continue;
17885663e6deSDavid Greenman 		}
17895663e6deSDavid Greenman 		/*
1790dcbcd518SBruce Evans 		 * If the process is in a non-running type state,
1791e602ba25SJulian Elischer 		 * don't touch it.  Check all the threads individually.
17925663e6deSDavid Greenman 		 */
17933e78e983SAlan Cox 		breakout = false;
1794e602ba25SJulian Elischer 		FOREACH_THREAD_IN_PROC(p, td) {
1795982d11f8SJeff Roberson 			thread_lock(td);
179671fad9fdSJulian Elischer 			if (!TD_ON_RUNQ(td) &&
179771fad9fdSJulian Elischer 			    !TD_IS_RUNNING(td) &&
1798f497cda2SEdward Tomasz Napierala 			    !TD_IS_SLEEPING(td) &&
1799b98acc0aSKonstantin Belousov 			    !TD_IS_SUSPENDED(td) &&
1800b98acc0aSKonstantin Belousov 			    !TD_IS_SWAPPED(td)) {
1801982d11f8SJeff Roberson 				thread_unlock(td);
18023e78e983SAlan Cox 				breakout = true;
1803e602ba25SJulian Elischer 				break;
1804e602ba25SJulian Elischer 			}
1805982d11f8SJeff Roberson 			thread_unlock(td);
1806e602ba25SJulian Elischer 		}
1807e602ba25SJulian Elischer 		if (breakout) {
18081c58e4e5SJohn Baldwin 			PROC_UNLOCK(p);
18095663e6deSDavid Greenman 			continue;
18105663e6deSDavid Greenman 		}
18115663e6deSDavid Greenman 		/*
18125663e6deSDavid Greenman 		 * get the process size
18135663e6deSDavid Greenman 		 */
18146bed074cSKonstantin Belousov 		vm = vmspace_acquire_ref(p);
18156bed074cSKonstantin Belousov 		if (vm == NULL) {
18166bed074cSKonstantin Belousov 			PROC_UNLOCK(p);
18176bed074cSKonstantin Belousov 			continue;
18186bed074cSKonstantin Belousov 		}
181995e2409aSKonstantin Belousov 		_PHOLD_LITE(p);
182072d97679SDavid Schultz 		PROC_UNLOCK(p);
182195e2409aSKonstantin Belousov 		sx_sunlock(&allproc_lock);
182295e2409aSKonstantin Belousov 		if (!vm_map_trylock_read(&vm->vm_map)) {
182371943c3dSKonstantin Belousov 			vmspace_free(vm);
182495e2409aSKonstantin Belousov 			sx_slock(&allproc_lock);
182595e2409aSKonstantin Belousov 			PRELE(p);
182672d97679SDavid Schultz 			continue;
182772d97679SDavid Schultz 		}
18287981aa24SKonstantin Belousov 		size = vmspace_swap_count(vm);
18292025d69bSKonstantin Belousov 		if (shortage == VM_OOM_MEM)
18303949873fSKonstantin Belousov 			size += vm_pageout_oom_pagecount(vm);
18313949873fSKonstantin Belousov 		vm_map_unlock_read(&vm->vm_map);
18326bed074cSKonstantin Belousov 		vmspace_free(vm);
183395e2409aSKonstantin Belousov 		sx_slock(&allproc_lock);
18343949873fSKonstantin Belousov 
18355663e6deSDavid Greenman 		/*
18363949873fSKonstantin Belousov 		 * If this process is bigger than the biggest one,
18375663e6deSDavid Greenman 		 * remember it.
18385663e6deSDavid Greenman 		 */
18395663e6deSDavid Greenman 		if (size > bigsize) {
18401c58e4e5SJohn Baldwin 			if (bigproc != NULL)
184171943c3dSKonstantin Belousov 				PRELE(bigproc);
18425663e6deSDavid Greenman 			bigproc = p;
18435663e6deSDavid Greenman 			bigsize = size;
184471943c3dSKonstantin Belousov 		} else {
184571943c3dSKonstantin Belousov 			PRELE(p);
184671943c3dSKonstantin Belousov 		}
18475663e6deSDavid Greenman 	}
18481005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
18495663e6deSDavid Greenman 	if (bigproc != NULL) {
18508311a2b8SWill Andrews 		if (vm_panic_on_oom != 0)
18518311a2b8SWill Andrews 			panic("out of swap space");
185271943c3dSKonstantin Belousov 		PROC_LOCK(bigproc);
1853729b1e51SDavid Greenman 		killproc(bigproc, "out of swap space");
1854fa885116SJulian Elischer 		sched_nice(bigproc, PRIO_MIN);
185571943c3dSKonstantin Belousov 		_PRELE(bigproc);
18561c58e4e5SJohn Baldwin 		PROC_UNLOCK(bigproc);
18575663e6deSDavid Greenman 	}
18585663e6deSDavid Greenman }
185926f9a767SRodney W. Grimes 
1860449c2e92SKonstantin Belousov static void
1861449c2e92SKonstantin Belousov vm_pageout_worker(void *arg)
1862449c2e92SKonstantin Belousov {
1863e2068d0bSJeff Roberson 	struct vm_domain *vmd;
18645f8cd1c0SJeff Roberson 	int domain, pass, shortage;
1865e57dd910SAlan Cox 	bool target_met;
1866449c2e92SKonstantin Belousov 
1867e2068d0bSJeff Roberson 	domain = (uintptr_t)arg;
1868e2068d0bSJeff Roberson 	vmd = VM_DOMAIN(domain);
186970cf3cedSAlan Cox 	pass = 0;
18705f8cd1c0SJeff Roberson 	shortage = 0;
1871e57dd910SAlan Cox 	target_met = true;
1872449c2e92SKonstantin Belousov 
1873449c2e92SKonstantin Belousov 	/*
1874949c9186SKonstantin Belousov 	 * XXXKIB It could be useful to bind pageout daemon threads to
1875949c9186SKonstantin Belousov 	 * the cores belonging to the domain, from which vm_page_array
1876949c9186SKonstantin Belousov 	 * is allocated.
1877449c2e92SKonstantin Belousov 	 */
1878449c2e92SKonstantin Belousov 
1879e2068d0bSJeff Roberson 	KASSERT(vmd->vmd_segs != 0, ("domain without segments"));
1880e2068d0bSJeff Roberson 	vmd->vmd_last_active_scan = ticks;
1881449c2e92SKonstantin Belousov 
1882449c2e92SKonstantin Belousov 	/*
1883449c2e92SKonstantin Belousov 	 * The pageout daemon worker is never done, so loop forever.
1884449c2e92SKonstantin Belousov 	 */
1885449c2e92SKonstantin Belousov 	while (TRUE) {
188630fbfddaSJeff Roberson 		vm_domain_pageout_lock(vmd);
188730fbfddaSJeff Roberson 		/*
188830fbfddaSJeff Roberson 		 * We need to clear wanted before we check the limits.  This
188930fbfddaSJeff Roberson 		 * prevents races with wakers who will check wanted after they
189030fbfddaSJeff Roberson 		 * reach the limit.
189130fbfddaSJeff Roberson 		 */
189230fbfddaSJeff Roberson 		atomic_store_int(&vmd->vmd_pageout_wanted, 0);
189356ce0690SAlan Cox 
189456ce0690SAlan Cox 		/*
18955f8cd1c0SJeff Roberson 		 * Might the page daemon need to run again?
1896449c2e92SKonstantin Belousov 		 */
18975f8cd1c0SJeff Roberson 		if (vm_paging_needed(vmd, vmd->vmd_free_count)) {
189856ce0690SAlan Cox 			/*
18995f8cd1c0SJeff Roberson 			 * Yes, the scan failed to free enough pages.  If
19005f8cd1c0SJeff Roberson 			 * we have performed a level >= 1 (page reclamation)
19015f8cd1c0SJeff Roberson 			 * scan, then sleep a bit and try again.
190256ce0690SAlan Cox 			 */
190330fbfddaSJeff Roberson 			vm_domain_pageout_unlock(vmd);
19045f8cd1c0SJeff Roberson 			if (pass > 1)
19056eebec83SMark Johnston 				pause("pwait", hz / VM_INACT_SCAN_RATE);
1906449c2e92SKonstantin Belousov 		} else {
1907449c2e92SKonstantin Belousov 			/*
19085f8cd1c0SJeff Roberson 			 * No, sleep until the next wakeup or until pages
19095f8cd1c0SJeff Roberson 			 * need to have their reference stats updated.
1910449c2e92SKonstantin Belousov 			 */
19112c0f13aaSKonstantin Belousov 			if (mtx_sleep(&vmd->vmd_pageout_wanted,
191230fbfddaSJeff Roberson 			    vm_domain_pageout_lockptr(vmd), PDROP | PVM,
19135f8cd1c0SJeff Roberson 			    "psleep", hz / VM_INACT_SCAN_RATE) == 0)
191483c9dea1SGleb Smirnoff 				VM_CNT_INC(v_pdwakeups);
191556ce0690SAlan Cox 		}
191630fbfddaSJeff Roberson 		/* Prevent spurious wakeups by ensuring that wanted is set. */
191730fbfddaSJeff Roberson 		atomic_store_int(&vmd->vmd_pageout_wanted, 1);
191830fbfddaSJeff Roberson 
191930fbfddaSJeff Roberson 		/*
192030fbfddaSJeff Roberson 		 * Use the controller to calculate how many pages to free in
192130fbfddaSJeff Roberson 		 * this interval.
192230fbfddaSJeff Roberson 		 */
19235f8cd1c0SJeff Roberson 		shortage = pidctrl_daemon(&vmd->vmd_pid, vmd->vmd_free_count);
19245f8cd1c0SJeff Roberson 		if (shortage && pass == 0)
19255f8cd1c0SJeff Roberson 			pass = 1;
192656ce0690SAlan Cox 
19275f8cd1c0SJeff Roberson 		target_met = vm_pageout_scan(vmd, pass, shortage);
19285f8cd1c0SJeff Roberson 		/*
19295f8cd1c0SJeff Roberson 		 * If the target was not met we must increase the pass to
19305f8cd1c0SJeff Roberson 		 * more aggressively reclaim.
19315f8cd1c0SJeff Roberson 		 */
19325f8cd1c0SJeff Roberson 		if (!target_met)
19335f8cd1c0SJeff Roberson 			pass++;
1934449c2e92SKonstantin Belousov 	}
1935449c2e92SKonstantin Belousov }
1936449c2e92SKonstantin Belousov 
1937df8bae1dSRodney W. Grimes /*
19384d19f4adSSteven Hartland  *	vm_pageout_init initialises basic pageout daemon settings.
1939df8bae1dSRodney W. Grimes  */
19402b14f991SJulian Elischer static void
1941e2068d0bSJeff Roberson vm_pageout_init_domain(int domain)
1942df8bae1dSRodney W. Grimes {
1943e2068d0bSJeff Roberson 	struct vm_domain *vmd;
19445f8cd1c0SJeff Roberson 	struct sysctl_oid *oid;
1945e2068d0bSJeff Roberson 
1946e2068d0bSJeff Roberson 	vmd = VM_DOMAIN(domain);
1947e2068d0bSJeff Roberson 	vmd->vmd_interrupt_free_min = 2;
1948f6b04d2bSDavid Greenman 
194945ae1d91SAlan Cox 	/*
195045ae1d91SAlan Cox 	 * v_free_reserved needs to include enough for the largest
195145ae1d91SAlan Cox 	 * swap pager structures plus enough for any pv_entry structs
195245ae1d91SAlan Cox 	 * when paging.
195345ae1d91SAlan Cox 	 */
1954e2068d0bSJeff Roberson 	if (vmd->vmd_page_count > 1024)
1955e2068d0bSJeff Roberson 		vmd->vmd_free_min = 4 + (vmd->vmd_page_count - 1024) / 200;
19562feb50bfSAttilio Rao 	else
1957e2068d0bSJeff Roberson 		vmd->vmd_free_min = 4;
1958e2068d0bSJeff Roberson 	vmd->vmd_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE +
1959e2068d0bSJeff Roberson 	    vmd->vmd_interrupt_free_min;
1960e2068d0bSJeff Roberson 	vmd->vmd_free_reserved = vm_pageout_page_count +
1961e2068d0bSJeff Roberson 	    vmd->vmd_pageout_free_min + (vmd->vmd_page_count / 768);
1962e2068d0bSJeff Roberson 	vmd->vmd_free_severe = vmd->vmd_free_min / 2;
1963e2068d0bSJeff Roberson 	vmd->vmd_free_target = 4 * vmd->vmd_free_min + vmd->vmd_free_reserved;
1964e2068d0bSJeff Roberson 	vmd->vmd_free_min += vmd->vmd_free_reserved;
1965e2068d0bSJeff Roberson 	vmd->vmd_free_severe += vmd->vmd_free_reserved;
1966e2068d0bSJeff Roberson 	vmd->vmd_inactive_target = (3 * vmd->vmd_free_target) / 2;
1967e2068d0bSJeff Roberson 	if (vmd->vmd_inactive_target > vmd->vmd_free_count / 3)
1968e2068d0bSJeff Roberson 		vmd->vmd_inactive_target = vmd->vmd_free_count / 3;
1969df8bae1dSRodney W. Grimes 
1970d9e23210SJeff Roberson 	/*
19715f8cd1c0SJeff Roberson 	 * Set the default wakeup threshold to be 10% below the paging
19725f8cd1c0SJeff Roberson 	 * target.  This keeps the steady state out of shortfall.
1973d9e23210SJeff Roberson 	 */
19745f8cd1c0SJeff Roberson 	vmd->vmd_pageout_wakeup_thresh = (vmd->vmd_free_target / 10) * 9;
1975e2068d0bSJeff Roberson 
1976e2068d0bSJeff Roberson 	/*
1977e2068d0bSJeff Roberson 	 * Target amount of memory to move out of the laundry queue during a
1978e2068d0bSJeff Roberson 	 * background laundering.  This is proportional to the amount of system
1979e2068d0bSJeff Roberson 	 * memory.
1980e2068d0bSJeff Roberson 	 */
1981e2068d0bSJeff Roberson 	vmd->vmd_background_launder_target = (vmd->vmd_free_target -
1982e2068d0bSJeff Roberson 	    vmd->vmd_free_min) / 10;
19835f8cd1c0SJeff Roberson 
19845f8cd1c0SJeff Roberson 	/* Initialize the pageout daemon pid controller. */
19855f8cd1c0SJeff Roberson 	pidctrl_init(&vmd->vmd_pid, hz / VM_INACT_SCAN_RATE,
19865f8cd1c0SJeff Roberson 	    vmd->vmd_free_target, PIDCTRL_BOUND,
19875f8cd1c0SJeff Roberson 	    PIDCTRL_KPD, PIDCTRL_KID, PIDCTRL_KDD);
19885f8cd1c0SJeff Roberson 	oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(vmd->vmd_oid), OID_AUTO,
19895f8cd1c0SJeff Roberson 	    "pidctrl", CTLFLAG_RD, NULL, "");
19905f8cd1c0SJeff Roberson 	pidctrl_init_sysctl(&vmd->vmd_pid, SYSCTL_CHILDREN(oid));
1991e2068d0bSJeff Roberson }
1992e2068d0bSJeff Roberson 
1993e2068d0bSJeff Roberson static void
1994e2068d0bSJeff Roberson vm_pageout_init(void)
1995e2068d0bSJeff Roberson {
1996e2068d0bSJeff Roberson 	u_int freecount;
1997e2068d0bSJeff Roberson 	int i;
1998e2068d0bSJeff Roberson 
1999e2068d0bSJeff Roberson 	/*
2000e2068d0bSJeff Roberson 	 * Initialize some paging parameters.
2001e2068d0bSJeff Roberson 	 */
2002e2068d0bSJeff Roberson 	if (vm_cnt.v_page_count < 2000)
2003e2068d0bSJeff Roberson 		vm_pageout_page_count = 8;
2004e2068d0bSJeff Roberson 
2005e2068d0bSJeff Roberson 	freecount = 0;
2006e2068d0bSJeff Roberson 	for (i = 0; i < vm_ndomains; i++) {
2007e2068d0bSJeff Roberson 		struct vm_domain *vmd;
2008e2068d0bSJeff Roberson 
2009e2068d0bSJeff Roberson 		vm_pageout_init_domain(i);
2010e2068d0bSJeff Roberson 		vmd = VM_DOMAIN(i);
2011e2068d0bSJeff Roberson 		vm_cnt.v_free_reserved += vmd->vmd_free_reserved;
2012e2068d0bSJeff Roberson 		vm_cnt.v_free_target += vmd->vmd_free_target;
2013e2068d0bSJeff Roberson 		vm_cnt.v_free_min += vmd->vmd_free_min;
2014e2068d0bSJeff Roberson 		vm_cnt.v_inactive_target += vmd->vmd_inactive_target;
2015e2068d0bSJeff Roberson 		vm_cnt.v_pageout_free_min += vmd->vmd_pageout_free_min;
2016e2068d0bSJeff Roberson 		vm_cnt.v_interrupt_free_min += vmd->vmd_interrupt_free_min;
2017e2068d0bSJeff Roberson 		vm_cnt.v_free_severe += vmd->vmd_free_severe;
2018e2068d0bSJeff Roberson 		freecount += vmd->vmd_free_count;
2019e2068d0bSJeff Roberson 	}
2020d9e23210SJeff Roberson 
2021d9e23210SJeff Roberson 	/*
2022d9e23210SJeff Roberson 	 * Set interval in seconds for active scan.  We want to visit each
2023c9612b2dSJeff Roberson 	 * page at least once every ten minutes.  This is to prevent worst
2024c9612b2dSJeff Roberson 	 * case paging behaviors with stale active LRU.
2025d9e23210SJeff Roberson 	 */
2026d9e23210SJeff Roberson 	if (vm_pageout_update_period == 0)
2027c9612b2dSJeff Roberson 		vm_pageout_update_period = 600;
2028d9e23210SJeff Roberson 
2029df8bae1dSRodney W. Grimes 	if (vm_page_max_wired == 0)
2030e2068d0bSJeff Roberson 		vm_page_max_wired = freecount / 3;
20314d19f4adSSteven Hartland }
20324d19f4adSSteven Hartland 
20334d19f4adSSteven Hartland /*
20344d19f4adSSteven Hartland  *     vm_pageout is the high level pageout daemon.
20354d19f4adSSteven Hartland  */
20364d19f4adSSteven Hartland static void
20374d19f4adSSteven Hartland vm_pageout(void)
20384d19f4adSSteven Hartland {
203944ec2b63SKonstantin Belousov 	int error;
204044ec2b63SKonstantin Belousov 	int i;
2041df8bae1dSRodney W. Grimes 
204224a1cce3SDavid Greenman 	swap_pager_swap_init();
20433b8cf4acSMark Johnston 	snprintf(curthread->td_name, sizeof(curthread->td_name), "dom0");
2044ebcddc72SAlan Cox 	error = kthread_add(vm_pageout_laundry_worker, NULL, curproc, NULL,
2045ebcddc72SAlan Cox 	    0, 0, "laundry: dom0");
2046ebcddc72SAlan Cox 	if (error != 0)
2047ebcddc72SAlan Cox 		panic("starting laundry for domain 0, error %d", error);
2048449c2e92SKonstantin Belousov 	for (i = 1; i < vm_ndomains; i++) {
2049449c2e92SKonstantin Belousov 		error = kthread_add(vm_pageout_worker, (void *)(uintptr_t)i,
2050449c2e92SKonstantin Belousov 		    curproc, NULL, 0, 0, "dom%d", i);
2051449c2e92SKonstantin Belousov 		if (error != 0) {
2052449c2e92SKonstantin Belousov 			panic("starting pageout for domain %d, error %d\n",
2053449c2e92SKonstantin Belousov 			    i, error);
2054dc2efb27SJohn Dyson 		}
2055e2068d0bSJeff Roberson 		error = kthread_add(vm_pageout_laundry_worker,
2056e2068d0bSJeff Roberson 		    (void *)(uintptr_t)i, curproc, NULL, 0, 0,
2057e2068d0bSJeff Roberson 		    "laundry: dom%d", i);
2058e2068d0bSJeff Roberson 		if (error != 0)
2059e2068d0bSJeff Roberson 			panic("starting laundry for domain %d, error %d",
2060e2068d0bSJeff Roberson 			    i, error);
2061f919ebdeSDavid Greenman 	}
206244ec2b63SKonstantin Belousov 	error = kthread_add(uma_reclaim_worker, NULL, curproc, NULL,
206344ec2b63SKonstantin Belousov 	    0, 0, "uma");
206444ec2b63SKonstantin Belousov 	if (error != 0)
206544ec2b63SKonstantin Belousov 		panic("starting uma_reclaim helper, error %d\n", error);
2066d395270dSDimitry Andric 	vm_pageout_worker((void *)(uintptr_t)0);
2067df8bae1dSRodney W. Grimes }
206826f9a767SRodney W. Grimes 
20696b4b77adSAlan Cox /*
2070280d15cdSMark Johnston  * Perform an advisory wakeup of the page daemon.
20716b4b77adSAlan Cox  */
2072e0c5a895SJohn Dyson void
2073e2068d0bSJeff Roberson pagedaemon_wakeup(int domain)
2074e0c5a895SJohn Dyson {
2075e2068d0bSJeff Roberson 	struct vm_domain *vmd;
2076a1c0a785SAlan Cox 
2077e2068d0bSJeff Roberson 	vmd = VM_DOMAIN(domain);
207830fbfddaSJeff Roberson 	vm_domain_pageout_assert_unlocked(vmd);
207930fbfddaSJeff Roberson 	if (curproc == pageproc)
208030fbfddaSJeff Roberson 		return;
2081280d15cdSMark Johnston 
208230fbfddaSJeff Roberson 	if (atomic_fetchadd_int(&vmd->vmd_pageout_wanted, 1) == 0) {
208330fbfddaSJeff Roberson 		vm_domain_pageout_lock(vmd);
208430fbfddaSJeff Roberson 		atomic_store_int(&vmd->vmd_pageout_wanted, 1);
2085e2068d0bSJeff Roberson 		wakeup(&vmd->vmd_pageout_wanted);
208630fbfddaSJeff Roberson 		vm_domain_pageout_unlock(vmd);
2087e0c5a895SJohn Dyson 	}
2088e0c5a895SJohn Dyson }
2089