xref: /freebsd/sys/vm/vm_pageout.c (revision 0ef5eee9d94162ac3cefa0bd8aedf1bfa6f87192)
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>
850292c54bSConrad Meyer #include <sys/blockcount.h>
86855a310fSJeff Roberson #include <sys/eventhandler.h>
87fb919e4dSMark Murray #include <sys/lock.h>
88fb919e4dSMark Murray #include <sys/mutex.h>
8926f9a767SRodney W. Grimes #include <sys/proc.h>
909c8b8baaSPeter Wemm #include <sys/kthread.h>
910384fff8SJason Evans #include <sys/ktr.h>
9297824da3SAlan Cox #include <sys/mount.h>
93099e7e95SEdward Tomasz Napierala #include <sys/racct.h>
9426f9a767SRodney W. Grimes #include <sys/resourcevar.h>
95b43179fbSJeff Roberson #include <sys/sched.h>
9614a0d74eSSteven Hartland #include <sys/sdt.h>
97d2fc5315SPoul-Henning Kamp #include <sys/signalvar.h>
98449c2e92SKonstantin Belousov #include <sys/smp.h>
99a6bf3a9eSRyan Stone #include <sys/time.h>
100f6b04d2bSDavid Greenman #include <sys/vnode.h>
101efeaf95aSDavid Greenman #include <sys/vmmeter.h>
10289f6b863SAttilio Rao #include <sys/rwlock.h>
1031005a129SJohn Baldwin #include <sys/sx.h>
10438efa82bSJohn Dyson #include <sys/sysctl.h>
105df8bae1dSRodney W. Grimes 
106df8bae1dSRodney W. Grimes #include <vm/vm.h>
107efeaf95aSDavid Greenman #include <vm/vm_param.h>
108efeaf95aSDavid Greenman #include <vm/vm_object.h>
109df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
110efeaf95aSDavid Greenman #include <vm/vm_map.h>
111df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
11224a1cce3SDavid Greenman #include <vm/vm_pager.h>
113449c2e92SKonstantin Belousov #include <vm/vm_phys.h>
114e2068d0bSJeff Roberson #include <vm/vm_pagequeue.h>
11505f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h>
116efeaf95aSDavid Greenman #include <vm/vm_extern.h>
117670d17b5SJeff Roberson #include <vm/uma.h>
118df8bae1dSRodney W. Grimes 
1192b14f991SJulian Elischer /*
1202b14f991SJulian Elischer  * System initialization
1212b14f991SJulian Elischer  */
1222b14f991SJulian Elischer 
1232b14f991SJulian Elischer /* the kernel process "vm_pageout"*/
12411caded3SAlfred Perlstein static void vm_pageout(void);
1254d19f4adSSteven Hartland static void vm_pageout_init(void);
126ebcddc72SAlan Cox static int vm_pageout_clean(vm_page_t m, int *numpagedout);
12734d8b7eaSJeff Roberson static int vm_pageout_cluster(vm_page_t m);
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;
156b1fd102eSMark Johnston static int swapdev_enabled;
15770111b90SJohn Dyson 
1588311a2b8SWill Andrews static int vm_panic_on_oom = 0;
1598311a2b8SWill Andrews 
1608311a2b8SWill Andrews SYSCTL_INT(_vm, OID_AUTO, panic_on_oom,
1618311a2b8SWill Andrews 	CTLFLAG_RWTUN, &vm_panic_on_oom, 0,
1623c200db9SJonathan T. Looney 	"Panic on the given number of out-of-memory errors instead of killing the largest process");
1638311a2b8SWill Andrews 
164d9e23210SJeff Roberson SYSCTL_INT(_vm, OID_AUTO, pageout_update_period,
165e0b2fc3aSMark Johnston 	CTLFLAG_RWTUN, &vm_pageout_update_period, 0,
166d9e23210SJeff Roberson 	"Maximum active LRU update period");
16753636869SAndrey Zonov 
16874f5530dSConrad Meyer static int pageout_cpus_per_thread = 16;
16974f5530dSConrad Meyer SYSCTL_INT(_vm, OID_AUTO, pageout_cpus_per_thread, CTLFLAG_RDTUN,
17074f5530dSConrad Meyer     &pageout_cpus_per_thread, 0,
17174f5530dSConrad Meyer     "Number of CPUs per pagedaemon worker thread");
1720292c54bSConrad Meyer 
173e0b2fc3aSMark Johnston SYSCTL_INT(_vm, OID_AUTO, lowmem_period, CTLFLAG_RWTUN, &lowmem_period, 0,
174c9612b2dSJeff Roberson 	"Low memory callback period");
175c9612b2dSJeff Roberson 
176ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts,
177e0b2fc3aSMark Johnston 	CTLFLAG_RWTUN, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages");
17812ac6a1dSJohn Dyson 
17923b59018SMatthew Dillon static int pageout_lock_miss;
18023b59018SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss,
18123b59018SMatthew Dillon 	CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout");
18223b59018SMatthew Dillon 
18376386c7eSKonstantin Belousov SYSCTL_INT(_vm, OID_AUTO, pageout_oom_seq,
184e0b2fc3aSMark Johnston 	CTLFLAG_RWTUN, &vm_pageout_oom_seq, 0,
18576386c7eSKonstantin Belousov 	"back-to-back calls to oom detector to start OOM");
18676386c7eSKonstantin Belousov 
187ebcddc72SAlan Cox static int act_scan_laundry_weight = 3;
188e0b2fc3aSMark Johnston SYSCTL_INT(_vm, OID_AUTO, act_scan_laundry_weight, CTLFLAG_RWTUN,
189ebcddc72SAlan Cox     &act_scan_laundry_weight, 0,
190ebcddc72SAlan Cox     "weight given to clean vs. dirty pages in active queue scans");
191ebcddc72SAlan Cox 
192ebcddc72SAlan Cox static u_int vm_background_launder_rate = 4096;
193e0b2fc3aSMark Johnston SYSCTL_UINT(_vm, OID_AUTO, background_launder_rate, CTLFLAG_RWTUN,
194ebcddc72SAlan Cox     &vm_background_launder_rate, 0,
195ebcddc72SAlan Cox     "background laundering rate, in kilobytes per second");
196ebcddc72SAlan Cox 
197ebcddc72SAlan Cox static u_int vm_background_launder_max = 20 * 1024;
198e0b2fc3aSMark Johnston SYSCTL_UINT(_vm, OID_AUTO, background_launder_max, CTLFLAG_RWTUN,
199ebcddc72SAlan Cox     &vm_background_launder_max, 0, "background laundering cap, in kilobytes");
200ebcddc72SAlan Cox 
201e2241590SAlan Cox int vm_pageout_page_count = 32;
202df8bae1dSRodney W. Grimes 
20354a3a114SMark Johnston u_long vm_page_max_user_wired;
20454a3a114SMark Johnston SYSCTL_ULONG(_vm, OID_AUTO, max_user_wired, CTLFLAG_RW,
20554a3a114SMark Johnston     &vm_page_max_user_wired, 0,
20654a3a114SMark Johnston     "system-wide limit to user-wired page count");
207df8bae1dSRodney W. Grimes 
208ebcddc72SAlan Cox static u_int isqrt(u_int num);
209ebcddc72SAlan Cox static int vm_pageout_launder(struct vm_domain *vmd, int launder,
210ebcddc72SAlan Cox     bool in_shortfall);
211ebcddc72SAlan Cox static void vm_pageout_laundry_worker(void *arg);
212cd41fc12SDavid Greenman 
2135cd29d0fSMark Johnston struct scan_state {
2145cd29d0fSMark Johnston 	struct vm_batchqueue bq;
2158d220203SAlan Cox 	struct vm_pagequeue *pq;
2165cd29d0fSMark Johnston 	vm_page_t	marker;
2175cd29d0fSMark Johnston 	int		maxscan;
2185cd29d0fSMark Johnston 	int		scanned;
2195cd29d0fSMark Johnston };
2208dbca793STor Egge 
2215cd29d0fSMark Johnston static void
2225cd29d0fSMark Johnston vm_pageout_init_scan(struct scan_state *ss, struct vm_pagequeue *pq,
2235cd29d0fSMark Johnston     vm_page_t marker, vm_page_t after, int maxscan)
2245cd29d0fSMark Johnston {
2258dbca793STor Egge 
2265cd29d0fSMark Johnston 	vm_pagequeue_assert_locked(pq);
2275cff1f4dSMark Johnston 	KASSERT((marker->a.flags & PGA_ENQUEUED) == 0,
2285cd29d0fSMark Johnston 	    ("marker %p already enqueued", marker));
2295cd29d0fSMark Johnston 
2305cd29d0fSMark Johnston 	if (after == NULL)
2315cd29d0fSMark Johnston 		TAILQ_INSERT_HEAD(&pq->pq_pl, marker, plinks.q);
2325cd29d0fSMark Johnston 	else
2335cd29d0fSMark Johnston 		TAILQ_INSERT_AFTER(&pq->pq_pl, after, marker, plinks.q);
2345cd29d0fSMark Johnston 	vm_page_aflag_set(marker, PGA_ENQUEUED);
2355cd29d0fSMark Johnston 
2365cd29d0fSMark Johnston 	vm_batchqueue_init(&ss->bq);
2375cd29d0fSMark Johnston 	ss->pq = pq;
2385cd29d0fSMark Johnston 	ss->marker = marker;
2395cd29d0fSMark Johnston 	ss->maxscan = maxscan;
2405cd29d0fSMark Johnston 	ss->scanned = 0;
2418d220203SAlan Cox 	vm_pagequeue_unlock(pq);
2425cd29d0fSMark Johnston }
2438dbca793STor Egge 
2445cd29d0fSMark Johnston static void
2455cd29d0fSMark Johnston vm_pageout_end_scan(struct scan_state *ss)
2465cd29d0fSMark Johnston {
2475cd29d0fSMark Johnston 	struct vm_pagequeue *pq;
2485cd29d0fSMark Johnston 
2495cd29d0fSMark Johnston 	pq = ss->pq;
2505cd29d0fSMark Johnston 	vm_pagequeue_assert_locked(pq);
2515cff1f4dSMark Johnston 	KASSERT((ss->marker->a.flags & PGA_ENQUEUED) != 0,
2525cd29d0fSMark Johnston 	    ("marker %p not enqueued", ss->marker));
2535cd29d0fSMark Johnston 
2545cd29d0fSMark Johnston 	TAILQ_REMOVE(&pq->pq_pl, ss->marker, plinks.q);
2555cd29d0fSMark Johnston 	vm_page_aflag_clear(ss->marker, PGA_ENQUEUED);
256899fe184SMark Johnston 	pq->pq_pdpages += ss->scanned;
2578dbca793STor Egge }
2588dbca793STor Egge 
2598dbca793STor Egge /*
2605cd29d0fSMark Johnston  * Add a small number of queued pages to a batch queue for later processing
2615cd29d0fSMark Johnston  * without the corresponding queue lock held.  The caller must have enqueued a
2625cd29d0fSMark Johnston  * marker page at the desired start point for the scan.  Pages will be
2635cd29d0fSMark Johnston  * physically dequeued if the caller so requests.  Otherwise, the returned
2645cd29d0fSMark Johnston  * batch may contain marker pages, and it is up to the caller to handle them.
2655cd29d0fSMark Johnston  *
266efec381dSMark Johnston  * When processing the batch queue, vm_pageout_defer() must be used to
267efec381dSMark Johnston  * determine whether the page has been logically dequeued since the batch was
268efec381dSMark Johnston  * collected.
2695cd29d0fSMark Johnston  */
2705cd29d0fSMark Johnston static __always_inline void
2715cd29d0fSMark Johnston vm_pageout_collect_batch(struct scan_state *ss, const bool dequeue)
2725cd29d0fSMark Johnston {
2738d220203SAlan Cox 	struct vm_pagequeue *pq;
274d70f0ab3SMark Johnston 	vm_page_t m, marker, n;
2758c616246SKonstantin Belousov 
2765cd29d0fSMark Johnston 	marker = ss->marker;
2775cd29d0fSMark Johnston 	pq = ss->pq;
2788c616246SKonstantin Belousov 
2795cff1f4dSMark Johnston 	KASSERT((marker->a.flags & PGA_ENQUEUED) != 0,
2805cd29d0fSMark Johnston 	    ("marker %p not enqueued", ss->marker));
2818c616246SKonstantin Belousov 
2828d220203SAlan Cox 	vm_pagequeue_lock(pq);
2835cd29d0fSMark Johnston 	for (m = TAILQ_NEXT(marker, plinks.q); m != NULL &&
2845cd29d0fSMark Johnston 	    ss->scanned < ss->maxscan && ss->bq.bq_cnt < VM_BATCHQUEUE_SIZE;
285d70f0ab3SMark Johnston 	    m = n, ss->scanned++) {
286d70f0ab3SMark Johnston 		n = TAILQ_NEXT(m, plinks.q);
2875cd29d0fSMark Johnston 		if ((m->flags & PG_MARKER) == 0) {
2885cff1f4dSMark Johnston 			KASSERT((m->a.flags & PGA_ENQUEUED) != 0,
2895cd29d0fSMark Johnston 			    ("page %p not enqueued", m));
2905cd29d0fSMark Johnston 			KASSERT((m->flags & PG_FICTITIOUS) == 0,
2915cd29d0fSMark Johnston 			    ("Fictitious page %p cannot be in page queue", m));
2925cd29d0fSMark Johnston 			KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2935cd29d0fSMark Johnston 			    ("Unmanaged page %p cannot be in page queue", m));
2945cd29d0fSMark Johnston 		} else if (dequeue)
2955cd29d0fSMark Johnston 			continue;
2968c616246SKonstantin Belousov 
2975cd29d0fSMark Johnston 		(void)vm_batchqueue_insert(&ss->bq, m);
2985cd29d0fSMark Johnston 		if (dequeue) {
2995cd29d0fSMark Johnston 			TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
3005cd29d0fSMark Johnston 			vm_page_aflag_clear(m, PGA_ENQUEUED);
3015cd29d0fSMark Johnston 		}
3025cd29d0fSMark Johnston 	}
3035cd29d0fSMark Johnston 	TAILQ_REMOVE(&pq->pq_pl, marker, plinks.q);
3045cd29d0fSMark Johnston 	if (__predict_true(m != NULL))
3055cd29d0fSMark Johnston 		TAILQ_INSERT_BEFORE(m, marker, plinks.q);
3065cd29d0fSMark Johnston 	else
3075cd29d0fSMark Johnston 		TAILQ_INSERT_TAIL(&pq->pq_pl, marker, plinks.q);
3085cd29d0fSMark Johnston 	if (dequeue)
3095cd29d0fSMark Johnston 		vm_pagequeue_cnt_add(pq, -ss->bq.bq_cnt);
3105cd29d0fSMark Johnston 	vm_pagequeue_unlock(pq);
3115cd29d0fSMark Johnston }
3125cd29d0fSMark Johnston 
313fee2a2faSMark Johnston /*
314fee2a2faSMark Johnston  * Return the next page to be scanned, or NULL if the scan is complete.
315fee2a2faSMark Johnston  */
3165cd29d0fSMark Johnston static __always_inline vm_page_t
3175cd29d0fSMark Johnston vm_pageout_next(struct scan_state *ss, const bool dequeue)
3185cd29d0fSMark Johnston {
3195cd29d0fSMark Johnston 
3205cd29d0fSMark Johnston 	if (ss->bq.bq_cnt == 0)
3215cd29d0fSMark Johnston 		vm_pageout_collect_batch(ss, dequeue);
3225cd29d0fSMark Johnston 	return (vm_batchqueue_pop(&ss->bq));
3238c616246SKonstantin Belousov }
3248c616246SKonstantin Belousov 
3258c616246SKonstantin Belousov /*
326b7f30bffSMark Johnston  * Determine whether processing of a page should be deferred and ensure that any
327b7f30bffSMark Johnston  * outstanding queue operations are processed.
328b7f30bffSMark Johnston  */
329b7f30bffSMark Johnston static __always_inline bool
330b7f30bffSMark Johnston vm_pageout_defer(vm_page_t m, const uint8_t queue, const bool enqueued)
331b7f30bffSMark Johnston {
332b7f30bffSMark Johnston 	vm_page_astate_t as;
333b7f30bffSMark Johnston 
334b7f30bffSMark Johnston 	as = vm_page_astate_load(m);
335b7f30bffSMark Johnston 	if (__predict_false(as.queue != queue ||
336b7f30bffSMark Johnston 	    ((as.flags & PGA_ENQUEUED) != 0) != enqueued))
337b7f30bffSMark Johnston 		return (true);
338b7f30bffSMark Johnston 	if ((as.flags & PGA_QUEUE_OP_MASK) != 0) {
339b7f30bffSMark Johnston 		vm_page_pqbatch_submit(m, queue);
340b7f30bffSMark Johnston 		return (true);
341b7f30bffSMark Johnston 	}
342b7f30bffSMark Johnston 	return (false);
343b7f30bffSMark Johnston }
344b7f30bffSMark Johnston 
345b7f30bffSMark Johnston /*
346248fe642SAlan Cox  * Scan for pages at adjacent offsets within the given page's object that are
347248fe642SAlan Cox  * eligible for laundering, form a cluster of these pages and the given page,
348248fe642SAlan Cox  * and launder that cluster.
34926f9a767SRodney W. Grimes  */
3503af76890SPoul-Henning Kamp static int
35134d8b7eaSJeff Roberson vm_pageout_cluster(vm_page_t m)
35224a1cce3SDavid Greenman {
35354d92145SMatthew Dillon 	vm_object_t object;
354248fe642SAlan Cox 	vm_page_t mc[2 * vm_pageout_page_count], p, pb, ps;
355248fe642SAlan Cox 	vm_pindex_t pindex;
356248fe642SAlan Cox 	int ib, is, page_base, pageout_count;
35726f9a767SRodney W. Grimes 
35817f6a17bSAlan Cox 	object = m->object;
35989f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
360248fe642SAlan Cox 	pindex = m->pindex;
3610cddd8f0SMatthew Dillon 
36263e97555SJeff Roberson 	vm_page_assert_xbusied(m);
3630d94caffSDavid Greenman 
36491b4f427SAlan Cox 	mc[vm_pageout_page_count] = pb = ps = m;
36526f9a767SRodney W. Grimes 	pageout_count = 1;
366f35329acSJohn Dyson 	page_base = vm_pageout_page_count;
36790ecac61SMatthew Dillon 	ib = 1;
36890ecac61SMatthew Dillon 	is = 1;
36990ecac61SMatthew Dillon 
37024a1cce3SDavid Greenman 	/*
371248fe642SAlan Cox 	 * We can cluster only if the page is not clean, busy, or held, and
372ebcddc72SAlan Cox 	 * the page is in the laundry queue.
37390ecac61SMatthew Dillon 	 *
37490ecac61SMatthew Dillon 	 * During heavy mmap/modification loads the pageout
37590ecac61SMatthew Dillon 	 * daemon can really fragment the underlying file
376248fe642SAlan Cox 	 * due to flushing pages out of order and not trying to
377248fe642SAlan Cox 	 * align the clusters (which leaves sporadic out-of-order
37890ecac61SMatthew Dillon 	 * holes).  To solve this problem we do the reverse scan
37990ecac61SMatthew Dillon 	 * first and attempt to align our cluster, then do a
38090ecac61SMatthew Dillon 	 * forward scan if room remains.
38124a1cce3SDavid Greenman 	 */
38290ecac61SMatthew Dillon more:
383248fe642SAlan Cox 	while (ib != 0 && pageout_count < vm_pageout_page_count) {
38490ecac61SMatthew Dillon 		if (ib > pindex) {
38590ecac61SMatthew Dillon 			ib = 0;
38690ecac61SMatthew Dillon 			break;
387f6b04d2bSDavid Greenman 		}
38863e97555SJeff Roberson 		if ((p = vm_page_prev(pb)) == NULL ||
38963e97555SJeff Roberson 		    vm_page_tryxbusy(p) == 0) {
39090ecac61SMatthew Dillon 			ib = 0;
39190ecac61SMatthew Dillon 			break;
392f6b04d2bSDavid Greenman 		}
39363e97555SJeff Roberson 		if (vm_page_wired(p)) {
39463e97555SJeff Roberson 			ib = 0;
39563e97555SJeff Roberson 			vm_page_xunbusy(p);
39663e97555SJeff Roberson 			break;
39763e97555SJeff Roberson 		}
39824a1cce3SDavid Greenman 		vm_page_test_dirty(p);
3991b5c869dSMark Johnston 		if (p->dirty == 0) {
400eb5d3969SAlan Cox 			ib = 0;
40163e97555SJeff Roberson 			vm_page_xunbusy(p);
402eb5d3969SAlan Cox 			break;
403eb5d3969SAlan Cox 		}
404fee2a2faSMark Johnston 		if (!vm_page_in_laundry(p) || !vm_page_try_remove_write(p)) {
40563e97555SJeff Roberson 			vm_page_xunbusy(p);
40690ecac61SMatthew Dillon 			ib = 0;
40724a1cce3SDavid Greenman 			break;
408f6b04d2bSDavid Greenman 		}
40991b4f427SAlan Cox 		mc[--page_base] = pb = p;
41090ecac61SMatthew Dillon 		++pageout_count;
41190ecac61SMatthew Dillon 		++ib;
412248fe642SAlan Cox 
41324a1cce3SDavid Greenman 		/*
414248fe642SAlan Cox 		 * We are at an alignment boundary.  Stop here, and switch
415248fe642SAlan Cox 		 * directions.  Do not clear ib.
41624a1cce3SDavid Greenman 		 */
41790ecac61SMatthew Dillon 		if ((pindex - (ib - 1)) % vm_pageout_page_count == 0)
41890ecac61SMatthew Dillon 			break;
41924a1cce3SDavid Greenman 	}
42090ecac61SMatthew Dillon 	while (pageout_count < vm_pageout_page_count &&
42190ecac61SMatthew Dillon 	    pindex + is < object->size) {
42263e97555SJeff Roberson 		if ((p = vm_page_next(ps)) == NULL ||
42363e97555SJeff Roberson 		    vm_page_tryxbusy(p) == 0)
42490ecac61SMatthew Dillon 			break;
42563e97555SJeff Roberson 		if (vm_page_wired(p)) {
42663e97555SJeff Roberson 			vm_page_xunbusy(p);
42763e97555SJeff Roberson 			break;
42863e97555SJeff Roberson 		}
42924a1cce3SDavid Greenman 		vm_page_test_dirty(p);
43063e97555SJeff Roberson 		if (p->dirty == 0) {
43163e97555SJeff Roberson 			vm_page_xunbusy(p);
432eb5d3969SAlan Cox 			break;
43363e97555SJeff Roberson 		}
434e8bcf696SMark Johnston 		if (!vm_page_in_laundry(p) || !vm_page_try_remove_write(p)) {
43563e97555SJeff Roberson 			vm_page_xunbusy(p);
43624a1cce3SDavid Greenman 			break;
437e8bcf696SMark Johnston 		}
43891b4f427SAlan Cox 		mc[page_base + pageout_count] = ps = p;
43990ecac61SMatthew Dillon 		++pageout_count;
44090ecac61SMatthew Dillon 		++is;
44124a1cce3SDavid Greenman 	}
44290ecac61SMatthew Dillon 
44390ecac61SMatthew Dillon 	/*
44490ecac61SMatthew Dillon 	 * If we exhausted our forward scan, continue with the reverse scan
445248fe642SAlan Cox 	 * when possible, even past an alignment boundary.  This catches
446248fe642SAlan Cox 	 * boundary conditions.
44790ecac61SMatthew Dillon 	 */
448248fe642SAlan Cox 	if (ib != 0 && pageout_count < vm_pageout_page_count)
44990ecac61SMatthew Dillon 		goto more;
450f6b04d2bSDavid Greenman 
45199e6e193SMark Johnston 	return (vm_pageout_flush(&mc[page_base], pageout_count,
45299e6e193SMark Johnston 	    VM_PAGER_PUT_NOREUSE, 0, NULL, NULL));
453aef922f5SJohn Dyson }
454aef922f5SJohn Dyson 
4551c7c3c6aSMatthew Dillon /*
4561c7c3c6aSMatthew Dillon  * vm_pageout_flush() - launder the given pages
4571c7c3c6aSMatthew Dillon  *
4581c7c3c6aSMatthew Dillon  *	The given pages are laundered.  Note that we setup for the start of
4591c7c3c6aSMatthew Dillon  *	I/O ( i.e. busy the page ), mark it read-only, and bump the object
4601c7c3c6aSMatthew Dillon  *	reference count all in here rather then in the parent.  If we want
4611c7c3c6aSMatthew Dillon  *	the parent to do more sophisticated things we may have to change
4621c7c3c6aSMatthew Dillon  *	the ordering.
4631e8a675cSKonstantin Belousov  *
4641e8a675cSKonstantin Belousov  *	Returned runlen is the count of pages between mreq and first
4651e8a675cSKonstantin Belousov  *	page after mreq with status VM_PAGER_AGAIN.
466126d6082SKonstantin Belousov  *	*eio is set to TRUE if pager returned VM_PAGER_ERROR or VM_PAGER_FAIL
467126d6082SKonstantin Belousov  *	for any page in runlen set.
4681c7c3c6aSMatthew Dillon  */
469aef922f5SJohn Dyson int
470126d6082SKonstantin Belousov vm_pageout_flush(vm_page_t *mc, int count, int flags, int mreq, int *prunlen,
471126d6082SKonstantin Belousov     boolean_t *eio)
472aef922f5SJohn Dyson {
4732e3b314dSAlan Cox 	vm_object_t object = mc[0]->object;
474aef922f5SJohn Dyson 	int pageout_status[count];
47595461b45SJohn Dyson 	int numpagedout = 0;
4761e8a675cSKonstantin Belousov 	int i, runlen;
477aef922f5SJohn Dyson 
47889f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
4797bec141bSKip Macy 
4801c7c3c6aSMatthew Dillon 	/*
48163e97555SJeff Roberson 	 * Initiate I/O.  Mark the pages shared busy and verify that they're
48263e97555SJeff Roberson 	 * valid and read-only.
4831c7c3c6aSMatthew Dillon 	 *
4841c7c3c6aSMatthew Dillon 	 * We do not have to fixup the clean/dirty bits here... we can
4851c7c3c6aSMatthew Dillon 	 * allow the pager to do it after the I/O completes.
48602fa91d3SMatthew Dillon 	 *
48702fa91d3SMatthew Dillon 	 * NOTE! mc[i]->dirty may be partial or fragmented due to an
48802fa91d3SMatthew Dillon 	 * edge case with file fragments.
4891c7c3c6aSMatthew Dillon 	 */
4908f9110f6SJohn Dyson 	for (i = 0; i < count; i++) {
4910012f373SJeff Roberson 		KASSERT(vm_page_all_valid(mc[i]),
4927a935082SAlan Cox 		    ("vm_pageout_flush: partially invalid page %p index %d/%d",
4937a935082SAlan Cox 			mc[i], i, count));
4945cff1f4dSMark Johnston 		KASSERT((mc[i]->a.flags & PGA_WRITEABLE) == 0,
495aed9aaaaSMark Johnston 		    ("vm_pageout_flush: writeable page %p", mc[i]));
49663e97555SJeff Roberson 		vm_page_busy_downgrade(mc[i]);
4972965a453SKip Macy 	}
498d474eaaaSDoug Rabson 	vm_object_pip_add(object, count);
499aef922f5SJohn Dyson 
500d076fbeaSAlan Cox 	vm_pager_put_pages(object, mc, count, flags, pageout_status);
50126f9a767SRodney W. Grimes 
5021e8a675cSKonstantin Belousov 	runlen = count - mreq;
503126d6082SKonstantin Belousov 	if (eio != NULL)
504126d6082SKonstantin Belousov 		*eio = FALSE;
505aef922f5SJohn Dyson 	for (i = 0; i < count; i++) {
506aef922f5SJohn Dyson 		vm_page_t mt = mc[i];
50724a1cce3SDavid Greenman 
5084cd45723SAlan Cox 		KASSERT(pageout_status[i] == VM_PAGER_PEND ||
5096031c68dSAlan Cox 		    !pmap_page_is_write_mapped(mt),
5109ea8d1a6SAlan Cox 		    ("vm_pageout_flush: page %p is not write protected", mt));
51126f9a767SRodney W. Grimes 		switch (pageout_status[i]) {
51226f9a767SRodney W. Grimes 		case VM_PAGER_OK:
5139f5632e6SMark Johnston 			/*
5149f5632e6SMark Johnston 			 * The page may have moved since laundering started, in
5159f5632e6SMark Johnston 			 * which case it should be left alone.
5169f5632e6SMark Johnston 			 */
517ebcddc72SAlan Cox 			if (vm_page_in_laundry(mt))
518ebcddc72SAlan Cox 				vm_page_deactivate_noreuse(mt);
519ebcddc72SAlan Cox 			/* FALLTHROUGH */
52026f9a767SRodney W. Grimes 		case VM_PAGER_PEND:
52195461b45SJohn Dyson 			numpagedout++;
52226f9a767SRodney W. Grimes 			break;
52326f9a767SRodney W. Grimes 		case VM_PAGER_BAD:
52426f9a767SRodney W. Grimes 			/*
525ebcddc72SAlan Cox 			 * The page is outside the object's range.  We pretend
526ebcddc72SAlan Cox 			 * that the page out worked and clean the page, so the
527ebcddc72SAlan Cox 			 * changes will be lost if the page is reclaimed by
528ebcddc72SAlan Cox 			 * the page daemon.
52926f9a767SRodney W. Grimes 			 */
53090ecac61SMatthew Dillon 			vm_page_undirty(mt);
531ebcddc72SAlan Cox 			if (vm_page_in_laundry(mt))
532ebcddc72SAlan Cox 				vm_page_deactivate_noreuse(mt);
53326f9a767SRodney W. Grimes 			break;
53426f9a767SRodney W. Grimes 		case VM_PAGER_ERROR:
53526f9a767SRodney W. Grimes 		case VM_PAGER_FAIL:
53626f9a767SRodney W. Grimes 			/*
537b1fd102eSMark Johnston 			 * If the page couldn't be paged out to swap because the
538b1fd102eSMark Johnston 			 * pager wasn't able to find space, place the page in
539b1fd102eSMark Johnston 			 * the PQ_UNSWAPPABLE holding queue.  This is an
540b1fd102eSMark Johnston 			 * optimization that prevents the page daemon from
541b1fd102eSMark Johnston 			 * wasting CPU cycles on pages that cannot be reclaimed
542b1fd102eSMark Johnston 			 * becase no swap device is configured.
543b1fd102eSMark Johnston 			 *
544b1fd102eSMark Johnston 			 * Otherwise, reactivate the page so that it doesn't
545b1fd102eSMark Johnston 			 * clog the laundry and inactive queues.  (We will try
546b1fd102eSMark Johnston 			 * paging it out again later.)
54726f9a767SRodney W. Grimes 			 */
5484b8365d7SKonstantin Belousov 			if ((object->flags & OBJ_SWAP) != 0 &&
549b1fd102eSMark Johnston 			    pageout_status[i] == VM_PAGER_FAIL) {
550b1fd102eSMark Johnston 				vm_page_unswappable(mt);
551b1fd102eSMark Johnston 				numpagedout++;
552b1fd102eSMark Johnston 			} else
55324a1cce3SDavid Greenman 				vm_page_activate(mt);
554126d6082SKonstantin Belousov 			if (eio != NULL && i >= mreq && i - mreq < runlen)
555126d6082SKonstantin Belousov 				*eio = TRUE;
55626f9a767SRodney W. Grimes 			break;
55726f9a767SRodney W. Grimes 		case VM_PAGER_AGAIN:
5581e8a675cSKonstantin Belousov 			if (i >= mreq && i - mreq < runlen)
5591e8a675cSKonstantin Belousov 				runlen = i - mreq;
56026f9a767SRodney W. Grimes 			break;
56126f9a767SRodney W. Grimes 		}
56226f9a767SRodney W. Grimes 
56326f9a767SRodney W. Grimes 		/*
5640d94caffSDavid Greenman 		 * If the operation is still going, leave the page busy to
5650d94caffSDavid Greenman 		 * block all other accesses. Also, leave the paging in
5660d94caffSDavid Greenman 		 * progress indicator set so that we don't attempt an object
5670d94caffSDavid Greenman 		 * collapse.
56826f9a767SRodney W. Grimes 		 */
56926f9a767SRodney W. Grimes 		if (pageout_status[i] != VM_PAGER_PEND) {
570f919ebdeSDavid Greenman 			vm_object_pip_wakeup(object);
571c7aebda8SAttilio Rao 			vm_page_sunbusy(mt);
5723c4a2440SAlan Cox 		}
5733c4a2440SAlan Cox 	}
5741e8a675cSKonstantin Belousov 	if (prunlen != NULL)
5751e8a675cSKonstantin Belousov 		*prunlen = runlen;
5763c4a2440SAlan Cox 	return (numpagedout);
57726f9a767SRodney W. Grimes }
57826f9a767SRodney W. Grimes 
579b1fd102eSMark Johnston static void
580b1fd102eSMark Johnston vm_pageout_swapon(void *arg __unused, struct swdevt *sp __unused)
581b1fd102eSMark Johnston {
582b1fd102eSMark Johnston 
583b1fd102eSMark Johnston 	atomic_store_rel_int(&swapdev_enabled, 1);
584b1fd102eSMark Johnston }
585b1fd102eSMark Johnston 
586b1fd102eSMark Johnston static void
587b1fd102eSMark Johnston vm_pageout_swapoff(void *arg __unused, struct swdevt *sp __unused)
588b1fd102eSMark Johnston {
589b1fd102eSMark Johnston 
590b1fd102eSMark Johnston 	if (swap_pager_nswapdev() == 1)
591b1fd102eSMark Johnston 		atomic_store_rel_int(&swapdev_enabled, 0);
592b1fd102eSMark Johnston }
593b1fd102eSMark Johnston 
5941c7c3c6aSMatthew Dillon /*
59534d8b7eaSJeff Roberson  * Attempt to acquire all of the necessary locks to launder a page and
59634d8b7eaSJeff Roberson  * then call through the clustering layer to PUTPAGES.  Wait a short
59734d8b7eaSJeff Roberson  * time for a vnode lock.
59834d8b7eaSJeff Roberson  *
59934d8b7eaSJeff Roberson  * Requires the page and object lock on entry, releases both before return.
60034d8b7eaSJeff Roberson  * Returns 0 on success and an errno otherwise.
60134d8b7eaSJeff Roberson  */
60234d8b7eaSJeff Roberson static int
603ebcddc72SAlan Cox vm_pageout_clean(vm_page_t m, int *numpagedout)
60434d8b7eaSJeff Roberson {
60534d8b7eaSJeff Roberson 	struct vnode *vp;
60634d8b7eaSJeff Roberson 	struct mount *mp;
60734d8b7eaSJeff Roberson 	vm_object_t object;
60834d8b7eaSJeff Roberson 	vm_pindex_t pindex;
609*0ef5eee9SKonstantin Belousov 	int error;
61034d8b7eaSJeff Roberson 
61134d8b7eaSJeff Roberson 	object = m->object;
61234d8b7eaSJeff Roberson 	VM_OBJECT_ASSERT_WLOCKED(object);
61334d8b7eaSJeff Roberson 	error = 0;
61434d8b7eaSJeff Roberson 	vp = NULL;
61534d8b7eaSJeff Roberson 	mp = NULL;
61634d8b7eaSJeff Roberson 
61734d8b7eaSJeff Roberson 	/*
61834d8b7eaSJeff Roberson 	 * The object is already known NOT to be dead.   It
61934d8b7eaSJeff Roberson 	 * is possible for the vget() to block the whole
62034d8b7eaSJeff Roberson 	 * pageout daemon, but the new low-memory handling
62134d8b7eaSJeff Roberson 	 * code should prevent it.
62234d8b7eaSJeff Roberson 	 *
62334d8b7eaSJeff Roberson 	 * We can't wait forever for the vnode lock, we might
62434d8b7eaSJeff Roberson 	 * deadlock due to a vn_read() getting stuck in
62534d8b7eaSJeff Roberson 	 * vm_wait while holding this vnode.  We skip the
62634d8b7eaSJeff Roberson 	 * vnode if we can't get it in a reasonable amount
62734d8b7eaSJeff Roberson 	 * of time.
62834d8b7eaSJeff Roberson 	 */
62934d8b7eaSJeff Roberson 	if (object->type == OBJT_VNODE) {
63063e97555SJeff Roberson 		vm_page_xunbusy(m);
63134d8b7eaSJeff Roberson 		vp = object->handle;
63234d8b7eaSJeff Roberson 		if (vp->v_type == VREG &&
63334d8b7eaSJeff Roberson 		    vn_start_write(vp, &mp, V_NOWAIT) != 0) {
63434d8b7eaSJeff Roberson 			mp = NULL;
63534d8b7eaSJeff Roberson 			error = EDEADLK;
63634d8b7eaSJeff Roberson 			goto unlock_all;
63734d8b7eaSJeff Roberson 		}
63834d8b7eaSJeff Roberson 		KASSERT(mp != NULL,
63934d8b7eaSJeff Roberson 		    ("vp %p with NULL v_mount", vp));
64034d8b7eaSJeff Roberson 		vm_object_reference_locked(object);
64134d8b7eaSJeff Roberson 		pindex = m->pindex;
64234d8b7eaSJeff Roberson 		VM_OBJECT_WUNLOCK(object);
643*0ef5eee9SKonstantin Belousov 		if (vget(vp, vn_lktype_write(NULL, vp) | LK_TIMELOCK) != 0) {
64434d8b7eaSJeff Roberson 			vp = NULL;
64534d8b7eaSJeff Roberson 			error = EDEADLK;
64634d8b7eaSJeff Roberson 			goto unlock_mp;
64734d8b7eaSJeff Roberson 		}
64834d8b7eaSJeff Roberson 		VM_OBJECT_WLOCK(object);
64957cd81a3SMark Johnston 
65057cd81a3SMark Johnston 		/*
65157cd81a3SMark Johnston 		 * Ensure that the object and vnode were not disassociated
65257cd81a3SMark Johnston 		 * while locks were dropped.
65357cd81a3SMark Johnston 		 */
65457cd81a3SMark Johnston 		if (vp->v_object != object) {
65557cd81a3SMark Johnston 			error = ENOENT;
65657cd81a3SMark Johnston 			goto unlock_all;
65757cd81a3SMark Johnston 		}
65857cd81a3SMark Johnston 
65934d8b7eaSJeff Roberson 		/*
6609f5632e6SMark Johnston 		 * While the object was unlocked, the page may have been:
66134d8b7eaSJeff Roberson 		 * (1) moved to a different queue,
66234d8b7eaSJeff Roberson 		 * (2) reallocated to a different object,
66334d8b7eaSJeff Roberson 		 * (3) reallocated to a different offset, or
66434d8b7eaSJeff Roberson 		 * (4) cleaned.
66534d8b7eaSJeff Roberson 		 */
666ebcddc72SAlan Cox 		if (!vm_page_in_laundry(m) || m->object != object ||
66734d8b7eaSJeff Roberson 		    m->pindex != pindex || m->dirty == 0) {
66834d8b7eaSJeff Roberson 			error = ENXIO;
66934d8b7eaSJeff Roberson 			goto unlock_all;
67034d8b7eaSJeff Roberson 		}
67134d8b7eaSJeff Roberson 
67234d8b7eaSJeff Roberson 		/*
6739f5632e6SMark Johnston 		 * The page may have been busied while the object lock was
6749f5632e6SMark Johnston 		 * released.
67534d8b7eaSJeff Roberson 		 */
67663e97555SJeff Roberson 		if (vm_page_tryxbusy(m) == 0) {
67734d8b7eaSJeff Roberson 			error = EBUSY;
67834d8b7eaSJeff Roberson 			goto unlock_all;
67934d8b7eaSJeff Roberson 		}
68034d8b7eaSJeff Roberson 	}
68134d8b7eaSJeff Roberson 
68234d8b7eaSJeff Roberson 	/*
683fee2a2faSMark Johnston 	 * Remove all writeable mappings, failing if the page is wired.
684fee2a2faSMark Johnston 	 */
685fee2a2faSMark Johnston 	if (!vm_page_try_remove_write(m)) {
68663e97555SJeff Roberson 		vm_page_xunbusy(m);
687fee2a2faSMark Johnston 		error = EBUSY;
688fee2a2faSMark Johnston 		goto unlock_all;
689fee2a2faSMark Johnston 	}
690fee2a2faSMark Johnston 
691fee2a2faSMark Johnston 	/*
69234d8b7eaSJeff Roberson 	 * If a page is dirty, then it is either being washed
69334d8b7eaSJeff Roberson 	 * (but not yet cleaned) or it is still in the
69434d8b7eaSJeff Roberson 	 * laundry.  If it is still in the laundry, then we
69534d8b7eaSJeff Roberson 	 * start the cleaning operation.
69634d8b7eaSJeff Roberson 	 */
697ebcddc72SAlan Cox 	if ((*numpagedout = vm_pageout_cluster(m)) == 0)
69834d8b7eaSJeff Roberson 		error = EIO;
69934d8b7eaSJeff Roberson 
70034d8b7eaSJeff Roberson unlock_all:
70134d8b7eaSJeff Roberson 	VM_OBJECT_WUNLOCK(object);
70234d8b7eaSJeff Roberson 
70334d8b7eaSJeff Roberson unlock_mp:
70434d8b7eaSJeff Roberson 	if (mp != NULL) {
70534d8b7eaSJeff Roberson 		if (vp != NULL)
70634d8b7eaSJeff Roberson 			vput(vp);
70734d8b7eaSJeff Roberson 		vm_object_deallocate(object);
70834d8b7eaSJeff Roberson 		vn_finished_write(mp);
70934d8b7eaSJeff Roberson 	}
71034d8b7eaSJeff Roberson 
71134d8b7eaSJeff Roberson 	return (error);
71234d8b7eaSJeff Roberson }
71334d8b7eaSJeff Roberson 
71434d8b7eaSJeff Roberson /*
715ebcddc72SAlan Cox  * Attempt to launder the specified number of pages.
716ebcddc72SAlan Cox  *
717ebcddc72SAlan Cox  * Returns the number of pages successfully laundered.
718ebcddc72SAlan Cox  */
719ebcddc72SAlan Cox static int
720ebcddc72SAlan Cox vm_pageout_launder(struct vm_domain *vmd, int launder, bool in_shortfall)
721ebcddc72SAlan Cox {
7225cd29d0fSMark Johnston 	struct scan_state ss;
723ebcddc72SAlan Cox 	struct vm_pagequeue *pq;
724ebcddc72SAlan Cox 	vm_object_t object;
7255cd29d0fSMark Johnston 	vm_page_t m, marker;
726f3f38e25SMark Johnston 	vm_page_astate_t new, old;
727f3f38e25SMark Johnston 	int act_delta, error, numpagedout, queue, refs, starting_target;
728ebcddc72SAlan Cox 	int vnodes_skipped;
72960256604SMark Johnston 	bool pageout_ok;
730ebcddc72SAlan Cox 
7315cd29d0fSMark Johnston 	object = NULL;
732ebcddc72SAlan Cox 	starting_target = launder;
733ebcddc72SAlan Cox 	vnodes_skipped = 0;
734ebcddc72SAlan Cox 
735ebcddc72SAlan Cox 	/*
736b1fd102eSMark Johnston 	 * Scan the laundry queues for pages eligible to be laundered.  We stop
737ebcddc72SAlan Cox 	 * once the target number of dirty pages have been laundered, or once
738ebcddc72SAlan Cox 	 * we've reached the end of the queue.  A single iteration of this loop
739ebcddc72SAlan Cox 	 * may cause more than one page to be laundered because of clustering.
740ebcddc72SAlan Cox 	 *
741b1fd102eSMark Johnston 	 * As an optimization, we avoid laundering from PQ_UNSWAPPABLE when no
742b1fd102eSMark Johnston 	 * swap devices are configured.
743ebcddc72SAlan Cox 	 */
744b1fd102eSMark Johnston 	if (atomic_load_acq_int(&swapdev_enabled))
74564b38930SMark Johnston 		queue = PQ_UNSWAPPABLE;
746b1fd102eSMark Johnston 	else
74764b38930SMark Johnston 		queue = PQ_LAUNDRY;
748ebcddc72SAlan Cox 
749b1fd102eSMark Johnston scan:
75064b38930SMark Johnston 	marker = &vmd->vmd_markers[queue];
7515cd29d0fSMark Johnston 	pq = &vmd->vmd_pagequeues[queue];
752ebcddc72SAlan Cox 	vm_pagequeue_lock(pq);
7535cd29d0fSMark Johnston 	vm_pageout_init_scan(&ss, pq, marker, NULL, pq->pq_cnt);
7545cd29d0fSMark Johnston 	while (launder > 0 && (m = vm_pageout_next(&ss, false)) != NULL) {
7555cd29d0fSMark Johnston 		if (__predict_false((m->flags & PG_MARKER) != 0))
756ebcddc72SAlan Cox 			continue;
7575cd29d0fSMark Johnston 
7585cd29d0fSMark Johnston 		/*
759b7f30bffSMark Johnston 		 * Don't touch a page that was removed from the queue after the
760b7f30bffSMark Johnston 		 * page queue lock was released.  Otherwise, ensure that any
761b7f30bffSMark Johnston 		 * pending queue operations, such as dequeues for wired pages,
762b7f30bffSMark Johnston 		 * are handled.
7635cd29d0fSMark Johnston 		 */
764b7f30bffSMark Johnston 		if (vm_pageout_defer(m, queue, true))
765ebcddc72SAlan Cox 			continue;
766e8bcf696SMark Johnston 
7679f5632e6SMark Johnston 		/*
7689f5632e6SMark Johnston 		 * Lock the page's object.
7699f5632e6SMark Johnston 		 */
7709f5632e6SMark Johnston 		if (object == NULL || object != m->object) {
77160256604SMark Johnston 			if (object != NULL)
7725cd29d0fSMark Johnston 				VM_OBJECT_WUNLOCK(object);
77323ed568cSMateusz Guzik 			object = atomic_load_ptr(&m->object);
7749f5632e6SMark Johnston 			if (__predict_false(object == NULL))
7759f5632e6SMark Johnston 				/* The page is being freed by another thread. */
7769f5632e6SMark Johnston 				continue;
7779f5632e6SMark Johnston 
778e8bcf696SMark Johnston 			/* Depends on type-stability. */
77941fd4b94SMark Johnston 			VM_OBJECT_WLOCK(object);
7809f5632e6SMark Johnston 			if (__predict_false(m->object != object)) {
7819f5632e6SMark Johnston 				VM_OBJECT_WUNLOCK(object);
7829f5632e6SMark Johnston 				object = NULL;
78341fd4b94SMark Johnston 				continue;
7849f5632e6SMark Johnston 			}
7859f5632e6SMark Johnston 		}
7865cd29d0fSMark Johnston 
78763e97555SJeff Roberson 		if (vm_page_tryxbusy(m) == 0)
7885cd29d0fSMark Johnston 			continue;
789ebcddc72SAlan Cox 
790ebcddc72SAlan Cox 		/*
791b7f30bffSMark Johnston 		 * Check for wirings now that we hold the object lock and have
7929f5632e6SMark Johnston 		 * exclusively busied the page.  If the page is mapped, it may
7939f5632e6SMark Johnston 		 * still be wired by pmap lookups.  The call to
794fee2a2faSMark Johnston 		 * vm_page_try_remove_all() below atomically checks for such
795fee2a2faSMark Johnston 		 * wirings and removes mappings.  If the page is unmapped, the
7969f5632e6SMark Johnston 		 * wire count is guaranteed not to increase after this check.
797fee2a2faSMark Johnston 		 */
7989f5632e6SMark Johnston 		if (__predict_false(vm_page_wired(m)))
799f3f38e25SMark Johnston 			goto skip_page;
800fee2a2faSMark Johnston 
801fee2a2faSMark Johnston 		/*
802ebcddc72SAlan Cox 		 * Invalid pages can be easily freed.  They cannot be
803ebcddc72SAlan Cox 		 * mapped; vm_page_free() asserts this.
804ebcddc72SAlan Cox 		 */
8050012f373SJeff Roberson 		if (vm_page_none_valid(m))
806ebcddc72SAlan Cox 			goto free_page;
807ebcddc72SAlan Cox 
808f3f38e25SMark Johnston 		refs = object->ref_count != 0 ? pmap_ts_referenced(m) : 0;
809f3f38e25SMark Johnston 
810f3f38e25SMark Johnston 		for (old = vm_page_astate_load(m);;) {
811ebcddc72SAlan Cox 			/*
812f3f38e25SMark Johnston 			 * Check to see if the page has been removed from the
813f3f38e25SMark Johnston 			 * queue since the first such check.  Leave it alone if
814f3f38e25SMark Johnston 			 * so, discarding any references collected by
815f3f38e25SMark Johnston 			 * pmap_ts_referenced().
816ebcddc72SAlan Cox 			 */
817f3f38e25SMark Johnston 			if (__predict_false(_vm_page_queue(old) == PQ_NONE))
818f3f38e25SMark Johnston 				goto skip_page;
819f3f38e25SMark Johnston 
820f3f38e25SMark Johnston 			new = old;
821f3f38e25SMark Johnston 			act_delta = refs;
822f3f38e25SMark Johnston 			if ((old.flags & PGA_REFERENCED) != 0) {
823f3f38e25SMark Johnston 				new.flags &= ~PGA_REFERENCED;
824d7aeb429SAlan Cox 				act_delta++;
825ebcddc72SAlan Cox 			}
826f3f38e25SMark Johnston 			if (act_delta == 0) {
827f3f38e25SMark Johnston 				;
828f3f38e25SMark Johnston 			} else if (object->ref_count != 0) {
829ebcddc72SAlan Cox 				/*
830f3f38e25SMark Johnston 				 * Increase the activation count if the page was
831f3f38e25SMark Johnston 				 * referenced while in the laundry queue.  This
832f3f38e25SMark Johnston 				 * makes it less likely that the page will be
833f3f38e25SMark Johnston 				 * returned prematurely to the laundry queue.
834e8bcf696SMark Johnston 				 */
835f3f38e25SMark Johnston 				new.act_count += ACT_ADVANCE +
836f3f38e25SMark Johnston 				    act_delta;
837f3f38e25SMark Johnston 				if (new.act_count > ACT_MAX)
838f3f38e25SMark Johnston 					new.act_count = ACT_MAX;
839f3f38e25SMark Johnston 
840f7607c30SMark Johnston 				new.flags &= ~PGA_QUEUE_OP_MASK;
841f3f38e25SMark Johnston 				new.flags |= PGA_REQUEUE;
842f3f38e25SMark Johnston 				new.queue = PQ_ACTIVE;
843f3f38e25SMark Johnston 				if (!vm_page_pqstate_commit(m, &old, new))
844f3f38e25SMark Johnston 					continue;
845e8bcf696SMark Johnston 
846e8bcf696SMark Johnston 				/*
847e8bcf696SMark Johnston 				 * If this was a background laundering, count
848e8bcf696SMark Johnston 				 * activated pages towards our target.  The
849e8bcf696SMark Johnston 				 * purpose of background laundering is to ensure
850e8bcf696SMark Johnston 				 * that pages are eventually cycled through the
851e8bcf696SMark Johnston 				 * laundry queue, and an activation is a valid
852e8bcf696SMark Johnston 				 * way out.
853ebcddc72SAlan Cox 				 */
854ebcddc72SAlan Cox 				if (!in_shortfall)
855ebcddc72SAlan Cox 					launder--;
856f3f38e25SMark Johnston 				VM_CNT_INC(v_reactivated);
857f3f38e25SMark Johnston 				goto skip_page;
8585cd29d0fSMark Johnston 			} else if ((object->flags & OBJ_DEAD) == 0) {
859f3f38e25SMark Johnston 				new.flags |= PGA_REQUEUE;
860f3f38e25SMark Johnston 				if (!vm_page_pqstate_commit(m, &old, new))
861e8bcf696SMark Johnston 					continue;
862f3f38e25SMark Johnston 				goto skip_page;
8635cd29d0fSMark Johnston 			}
864f3f38e25SMark Johnston 			break;
865ebcddc72SAlan Cox 		}
866ebcddc72SAlan Cox 
867ebcddc72SAlan Cox 		/*
868ebcddc72SAlan Cox 		 * If the page appears to be clean at the machine-independent
869ebcddc72SAlan Cox 		 * layer, then remove all of its mappings from the pmap in
870ebcddc72SAlan Cox 		 * anticipation of freeing it.  If, however, any of the page's
871ebcddc72SAlan Cox 		 * mappings allow write access, then the page may still be
872ebcddc72SAlan Cox 		 * modified until the last of those mappings are removed.
873ebcddc72SAlan Cox 		 */
874ebcddc72SAlan Cox 		if (object->ref_count != 0) {
875ebcddc72SAlan Cox 			vm_page_test_dirty(m);
8769f5632e6SMark Johnston 			if (m->dirty == 0 && !vm_page_try_remove_all(m))
877f3f38e25SMark Johnston 				goto skip_page;
878fee2a2faSMark Johnston 		}
879ebcddc72SAlan Cox 
880ebcddc72SAlan Cox 		/*
881ebcddc72SAlan Cox 		 * Clean pages are freed, and dirty pages are paged out unless
882ebcddc72SAlan Cox 		 * they belong to a dead object.  Requeueing dirty pages from
883ebcddc72SAlan Cox 		 * dead objects is pointless, as they are being paged out and
884ebcddc72SAlan Cox 		 * freed by the thread that destroyed the object.
885ebcddc72SAlan Cox 		 */
886ebcddc72SAlan Cox 		if (m->dirty == 0) {
887ebcddc72SAlan Cox free_page:
8889f5632e6SMark Johnston 			/*
8899f5632e6SMark Johnston 			 * Now we are guaranteed that no other threads are
8909f5632e6SMark Johnston 			 * manipulating the page, check for a last-second
8919f5632e6SMark Johnston 			 * reference.
8929f5632e6SMark Johnston 			 */
8939f5632e6SMark Johnston 			if (vm_pageout_defer(m, queue, true))
8949f5632e6SMark Johnston 				goto skip_page;
895ebcddc72SAlan Cox 			vm_page_free(m);
89683c9dea1SGleb Smirnoff 			VM_CNT_INC(v_dfree);
897ebcddc72SAlan Cox 		} else if ((object->flags & OBJ_DEAD) == 0) {
8984b8365d7SKonstantin Belousov 			if ((object->flags & OBJ_SWAP) == 0 &&
899ebcddc72SAlan Cox 			    object->type != OBJT_DEFAULT)
900ebcddc72SAlan Cox 				pageout_ok = true;
901ebcddc72SAlan Cox 			else if (disable_swap_pageouts)
902ebcddc72SAlan Cox 				pageout_ok = false;
903ebcddc72SAlan Cox 			else
904ebcddc72SAlan Cox 				pageout_ok = true;
905ebcddc72SAlan Cox 			if (!pageout_ok) {
906f3f38e25SMark Johnston 				vm_page_launder(m);
907f3f38e25SMark Johnston 				goto skip_page;
908ebcddc72SAlan Cox 			}
909ebcddc72SAlan Cox 
910ebcddc72SAlan Cox 			/*
911ebcddc72SAlan Cox 			 * Form a cluster with adjacent, dirty pages from the
912ebcddc72SAlan Cox 			 * same object, and page out that entire cluster.
913ebcddc72SAlan Cox 			 *
914ebcddc72SAlan Cox 			 * The adjacent, dirty pages must also be in the
915ebcddc72SAlan Cox 			 * laundry.  However, their mappings are not checked
916ebcddc72SAlan Cox 			 * for new references.  Consequently, a recently
917ebcddc72SAlan Cox 			 * referenced page may be paged out.  However, that
918ebcddc72SAlan Cox 			 * page will not be prematurely reclaimed.  After page
919ebcddc72SAlan Cox 			 * out, the page will be placed in the inactive queue,
920ebcddc72SAlan Cox 			 * where any new references will be detected and the
921ebcddc72SAlan Cox 			 * page reactivated.
922ebcddc72SAlan Cox 			 */
923ebcddc72SAlan Cox 			error = vm_pageout_clean(m, &numpagedout);
924ebcddc72SAlan Cox 			if (error == 0) {
925ebcddc72SAlan Cox 				launder -= numpagedout;
9265cd29d0fSMark Johnston 				ss.scanned += numpagedout;
927ebcddc72SAlan Cox 			} else if (error == EDEADLK) {
928ebcddc72SAlan Cox 				pageout_lock_miss++;
929ebcddc72SAlan Cox 				vnodes_skipped++;
930ebcddc72SAlan Cox 			}
93160256604SMark Johnston 			object = NULL;
932f3f38e25SMark Johnston 		} else {
933f3f38e25SMark Johnston skip_page:
93463e97555SJeff Roberson 			vm_page_xunbusy(m);
935e8bcf696SMark Johnston 		}
936f3f38e25SMark Johnston 	}
93746e39081SMark Johnston 	if (object != NULL) {
938ebcddc72SAlan Cox 		VM_OBJECT_WUNLOCK(object);
93946e39081SMark Johnston 		object = NULL;
94046e39081SMark Johnston 	}
941ebcddc72SAlan Cox 	vm_pagequeue_lock(pq);
9425cd29d0fSMark Johnston 	vm_pageout_end_scan(&ss);
943ebcddc72SAlan Cox 	vm_pagequeue_unlock(pq);
944ebcddc72SAlan Cox 
94564b38930SMark Johnston 	if (launder > 0 && queue == PQ_UNSWAPPABLE) {
94664b38930SMark Johnston 		queue = PQ_LAUNDRY;
947b1fd102eSMark Johnston 		goto scan;
948b1fd102eSMark Johnston 	}
949b1fd102eSMark Johnston 
950ebcddc72SAlan Cox 	/*
951ebcddc72SAlan Cox 	 * Wakeup the sync daemon if we skipped a vnode in a writeable object
952ebcddc72SAlan Cox 	 * and we didn't launder enough pages.
953ebcddc72SAlan Cox 	 */
954ebcddc72SAlan Cox 	if (vnodes_skipped > 0 && launder > 0)
955ebcddc72SAlan Cox 		(void)speedup_syncer();
956ebcddc72SAlan Cox 
957ebcddc72SAlan Cox 	return (starting_target - launder);
958ebcddc72SAlan Cox }
959ebcddc72SAlan Cox 
960ebcddc72SAlan Cox /*
961ebcddc72SAlan Cox  * Compute the integer square root.
962ebcddc72SAlan Cox  */
963ebcddc72SAlan Cox static u_int
964ebcddc72SAlan Cox isqrt(u_int num)
965ebcddc72SAlan Cox {
966ebcddc72SAlan Cox 	u_int bit, root, tmp;
967ebcddc72SAlan Cox 
96864f8d257SDoug Moore 	bit = num != 0 ? (1u << ((fls(num) - 1) & ~1)) : 0;
969ebcddc72SAlan Cox 	root = 0;
970ebcddc72SAlan Cox 	while (bit != 0) {
971ebcddc72SAlan Cox 		tmp = root + bit;
972ebcddc72SAlan Cox 		root >>= 1;
973ebcddc72SAlan Cox 		if (num >= tmp) {
974ebcddc72SAlan Cox 			num -= tmp;
975ebcddc72SAlan Cox 			root += bit;
976ebcddc72SAlan Cox 		}
977ebcddc72SAlan Cox 		bit >>= 2;
978ebcddc72SAlan Cox 	}
979ebcddc72SAlan Cox 	return (root);
980ebcddc72SAlan Cox }
981ebcddc72SAlan Cox 
982ebcddc72SAlan Cox /*
983ebcddc72SAlan Cox  * Perform the work of the laundry thread: periodically wake up and determine
984ebcddc72SAlan Cox  * whether any pages need to be laundered.  If so, determine the number of pages
985ebcddc72SAlan Cox  * that need to be laundered, and launder them.
986ebcddc72SAlan Cox  */
987ebcddc72SAlan Cox static void
988ebcddc72SAlan Cox vm_pageout_laundry_worker(void *arg)
989ebcddc72SAlan Cox {
990e2068d0bSJeff Roberson 	struct vm_domain *vmd;
991ebcddc72SAlan Cox 	struct vm_pagequeue *pq;
99260684862SMark Johnston 	uint64_t nclean, ndirty, nfreed;
993e2068d0bSJeff Roberson 	int domain, last_target, launder, shortfall, shortfall_cycle, target;
994ebcddc72SAlan Cox 	bool in_shortfall;
995ebcddc72SAlan Cox 
996e2068d0bSJeff Roberson 	domain = (uintptr_t)arg;
997e2068d0bSJeff Roberson 	vmd = VM_DOMAIN(domain);
998e2068d0bSJeff Roberson 	pq = &vmd->vmd_pagequeues[PQ_LAUNDRY];
999e2068d0bSJeff Roberson 	KASSERT(vmd->vmd_segs != 0, ("domain without segments"));
1000ebcddc72SAlan Cox 
1001ebcddc72SAlan Cox 	shortfall = 0;
1002ebcddc72SAlan Cox 	in_shortfall = false;
1003ebcddc72SAlan Cox 	shortfall_cycle = 0;
10048002c3a4SMark Johnston 	last_target = target = 0;
100560684862SMark Johnston 	nfreed = 0;
1006ebcddc72SAlan Cox 
1007ebcddc72SAlan Cox 	/*
1008b1fd102eSMark Johnston 	 * Calls to these handlers are serialized by the swap syscall lock.
1009b1fd102eSMark Johnston 	 */
1010e2068d0bSJeff Roberson 	(void)EVENTHANDLER_REGISTER(swapon, vm_pageout_swapon, vmd,
1011b1fd102eSMark Johnston 	    EVENTHANDLER_PRI_ANY);
1012e2068d0bSJeff Roberson 	(void)EVENTHANDLER_REGISTER(swapoff, vm_pageout_swapoff, vmd,
1013b1fd102eSMark Johnston 	    EVENTHANDLER_PRI_ANY);
1014b1fd102eSMark Johnston 
1015b1fd102eSMark Johnston 	/*
1016ebcddc72SAlan Cox 	 * The pageout laundry worker is never done, so loop forever.
1017ebcddc72SAlan Cox 	 */
1018ebcddc72SAlan Cox 	for (;;) {
1019ebcddc72SAlan Cox 		KASSERT(target >= 0, ("negative target %d", target));
1020ebcddc72SAlan Cox 		KASSERT(shortfall_cycle >= 0,
1021ebcddc72SAlan Cox 		    ("negative cycle %d", shortfall_cycle));
1022ebcddc72SAlan Cox 		launder = 0;
1023ebcddc72SAlan Cox 
1024ebcddc72SAlan Cox 		/*
1025ebcddc72SAlan Cox 		 * First determine whether we need to launder pages to meet a
1026ebcddc72SAlan Cox 		 * shortage of free pages.
1027ebcddc72SAlan Cox 		 */
1028ebcddc72SAlan Cox 		if (shortfall > 0) {
1029ebcddc72SAlan Cox 			in_shortfall = true;
1030ebcddc72SAlan Cox 			shortfall_cycle = VM_LAUNDER_RATE / VM_INACT_SCAN_RATE;
1031ebcddc72SAlan Cox 			target = shortfall;
1032ebcddc72SAlan Cox 		} else if (!in_shortfall)
1033ebcddc72SAlan Cox 			goto trybackground;
1034e2068d0bSJeff Roberson 		else if (shortfall_cycle == 0 || vm_laundry_target(vmd) <= 0) {
1035ebcddc72SAlan Cox 			/*
1036ebcddc72SAlan Cox 			 * We recently entered shortfall and began laundering
1037ebcddc72SAlan Cox 			 * pages.  If we have completed that laundering run
1038ebcddc72SAlan Cox 			 * (and we are no longer in shortfall) or we have met
1039ebcddc72SAlan Cox 			 * our laundry target through other activity, then we
1040ebcddc72SAlan Cox 			 * can stop laundering pages.
1041ebcddc72SAlan Cox 			 */
1042ebcddc72SAlan Cox 			in_shortfall = false;
1043ebcddc72SAlan Cox 			target = 0;
1044ebcddc72SAlan Cox 			goto trybackground;
1045ebcddc72SAlan Cox 		}
1046ebcddc72SAlan Cox 		launder = target / shortfall_cycle--;
1047ebcddc72SAlan Cox 		goto dolaundry;
1048ebcddc72SAlan Cox 
1049ebcddc72SAlan Cox 		/*
1050ebcddc72SAlan Cox 		 * There's no immediate need to launder any pages; see if we
1051ebcddc72SAlan Cox 		 * meet the conditions to perform background laundering:
1052ebcddc72SAlan Cox 		 *
1053ebcddc72SAlan Cox 		 * 1. The ratio of dirty to clean inactive pages exceeds the
105460684862SMark Johnston 		 *    background laundering threshold, or
1055ebcddc72SAlan Cox 		 * 2. we haven't yet reached the target of the current
1056ebcddc72SAlan Cox 		 *    background laundering run.
1057ebcddc72SAlan Cox 		 *
1058ebcddc72SAlan Cox 		 * The background laundering threshold is not a constant.
1059ebcddc72SAlan Cox 		 * Instead, it is a slowly growing function of the number of
106060684862SMark Johnston 		 * clean pages freed by the page daemon since the last
106160684862SMark Johnston 		 * background laundering.  Thus, as the ratio of dirty to
106260684862SMark Johnston 		 * clean inactive pages grows, the amount of memory pressure
1063c098768eSMark Johnston 		 * required to trigger laundering decreases.  We ensure
1064c098768eSMark Johnston 		 * that the threshold is non-zero after an inactive queue
1065c098768eSMark Johnston 		 * scan, even if that scan failed to free a single clean page.
1066ebcddc72SAlan Cox 		 */
1067ebcddc72SAlan Cox trybackground:
1068e2068d0bSJeff Roberson 		nclean = vmd->vmd_free_count +
1069e2068d0bSJeff Roberson 		    vmd->vmd_pagequeues[PQ_INACTIVE].pq_cnt;
1070e2068d0bSJeff Roberson 		ndirty = vmd->vmd_pagequeues[PQ_LAUNDRY].pq_cnt;
1071c098768eSMark Johnston 		if (target == 0 && ndirty * isqrt(howmany(nfreed + 1,
1072c098768eSMark Johnston 		    vmd->vmd_free_target - vmd->vmd_free_min)) >= nclean) {
1073e2068d0bSJeff Roberson 			target = vmd->vmd_background_launder_target;
1074ebcddc72SAlan Cox 		}
1075ebcddc72SAlan Cox 
1076ebcddc72SAlan Cox 		/*
1077ebcddc72SAlan Cox 		 * We have a non-zero background laundering target.  If we've
1078ebcddc72SAlan Cox 		 * laundered up to our maximum without observing a page daemon
1079cb35676eSMark Johnston 		 * request, just stop.  This is a safety belt that ensures we
1080ebcddc72SAlan Cox 		 * don't launder an excessive amount if memory pressure is low
1081ebcddc72SAlan Cox 		 * and the ratio of dirty to clean pages is large.  Otherwise,
1082ebcddc72SAlan Cox 		 * proceed at the background laundering rate.
1083ebcddc72SAlan Cox 		 */
1084ebcddc72SAlan Cox 		if (target > 0) {
108560684862SMark Johnston 			if (nfreed > 0) {
108660684862SMark Johnston 				nfreed = 0;
1087ebcddc72SAlan Cox 				last_target = target;
1088ebcddc72SAlan Cox 			} else if (last_target - target >=
1089ebcddc72SAlan Cox 			    vm_background_launder_max * PAGE_SIZE / 1024) {
1090ebcddc72SAlan Cox 				target = 0;
1091ebcddc72SAlan Cox 			}
1092ebcddc72SAlan Cox 			launder = vm_background_launder_rate * PAGE_SIZE / 1024;
1093ebcddc72SAlan Cox 			launder /= VM_LAUNDER_RATE;
1094ebcddc72SAlan Cox 			if (launder > target)
1095ebcddc72SAlan Cox 				launder = target;
1096ebcddc72SAlan Cox 		}
1097ebcddc72SAlan Cox 
1098ebcddc72SAlan Cox dolaundry:
1099ebcddc72SAlan Cox 		if (launder > 0) {
1100ebcddc72SAlan Cox 			/*
1101ebcddc72SAlan Cox 			 * Because of I/O clustering, the number of laundered
1102ebcddc72SAlan Cox 			 * pages could exceed "target" by the maximum size of
1103ebcddc72SAlan Cox 			 * a cluster minus one.
1104ebcddc72SAlan Cox 			 */
1105e2068d0bSJeff Roberson 			target -= min(vm_pageout_launder(vmd, launder,
1106ebcddc72SAlan Cox 			    in_shortfall), target);
1107ebcddc72SAlan Cox 			pause("laundp", hz / VM_LAUNDER_RATE);
1108ebcddc72SAlan Cox 		}
1109ebcddc72SAlan Cox 
1110ebcddc72SAlan Cox 		/*
1111ebcddc72SAlan Cox 		 * If we're not currently laundering pages and the page daemon
1112ebcddc72SAlan Cox 		 * hasn't posted a new request, sleep until the page daemon
1113ebcddc72SAlan Cox 		 * kicks us.
1114ebcddc72SAlan Cox 		 */
1115ebcddc72SAlan Cox 		vm_pagequeue_lock(pq);
1116e2068d0bSJeff Roberson 		if (target == 0 && vmd->vmd_laundry_request == VM_LAUNDRY_IDLE)
1117e2068d0bSJeff Roberson 			(void)mtx_sleep(&vmd->vmd_laundry_request,
1118ebcddc72SAlan Cox 			    vm_pagequeue_lockptr(pq), PVM, "launds", 0);
1119ebcddc72SAlan Cox 
1120ebcddc72SAlan Cox 		/*
1121ebcddc72SAlan Cox 		 * If the pagedaemon has indicated that it's in shortfall, start
1122ebcddc72SAlan Cox 		 * a shortfall laundering unless we're already in the middle of
1123ebcddc72SAlan Cox 		 * one.  This may preempt a background laundering.
1124ebcddc72SAlan Cox 		 */
1125e2068d0bSJeff Roberson 		if (vmd->vmd_laundry_request == VM_LAUNDRY_SHORTFALL &&
1126ebcddc72SAlan Cox 		    (!in_shortfall || shortfall_cycle == 0)) {
1127e2068d0bSJeff Roberson 			shortfall = vm_laundry_target(vmd) +
1128e2068d0bSJeff Roberson 			    vmd->vmd_pageout_deficit;
1129ebcddc72SAlan Cox 			target = 0;
1130ebcddc72SAlan Cox 		} else
1131ebcddc72SAlan Cox 			shortfall = 0;
1132ebcddc72SAlan Cox 
1133ebcddc72SAlan Cox 		if (target == 0)
1134e2068d0bSJeff Roberson 			vmd->vmd_laundry_request = VM_LAUNDRY_IDLE;
113560684862SMark Johnston 		nfreed += vmd->vmd_clean_pages_freed;
113660684862SMark Johnston 		vmd->vmd_clean_pages_freed = 0;
1137ebcddc72SAlan Cox 		vm_pagequeue_unlock(pq);
1138ebcddc72SAlan Cox 	}
1139ebcddc72SAlan Cox }
1140ebcddc72SAlan Cox 
1141be37ee79SMark Johnston /*
1142be37ee79SMark Johnston  * Compute the number of pages we want to try to move from the
1143be37ee79SMark Johnston  * active queue to either the inactive or laundry queue.
1144be37ee79SMark Johnston  *
11457bb4634eSMark Johnston  * When scanning active pages during a shortage, we make clean pages
11467bb4634eSMark Johnston  * count more heavily towards the page shortage than dirty pages.
11477bb4634eSMark Johnston  * This is because dirty pages must be laundered before they can be
11487bb4634eSMark Johnston  * reused and thus have less utility when attempting to quickly
11497bb4634eSMark Johnston  * alleviate a free page shortage.  However, this weighting also
11507bb4634eSMark Johnston  * causes the scan to deactivate dirty pages more aggressively,
11517bb4634eSMark Johnston  * improving the effectiveness of clustering.
1152be37ee79SMark Johnston  */
1153be37ee79SMark Johnston static int
11547bb4634eSMark Johnston vm_pageout_active_target(struct vm_domain *vmd)
1155be37ee79SMark Johnston {
1156be37ee79SMark Johnston 	int shortage;
1157be37ee79SMark Johnston 
1158be37ee79SMark Johnston 	shortage = vmd->vmd_inactive_target + vm_paging_target(vmd) -
1159be37ee79SMark Johnston 	    (vmd->vmd_pagequeues[PQ_INACTIVE].pq_cnt +
1160be37ee79SMark Johnston 	    vmd->vmd_pagequeues[PQ_LAUNDRY].pq_cnt / act_scan_laundry_weight);
1161be37ee79SMark Johnston 	shortage *= act_scan_laundry_weight;
1162be37ee79SMark Johnston 	return (shortage);
1163be37ee79SMark Johnston }
1164be37ee79SMark Johnston 
1165be37ee79SMark Johnston /*
1166be37ee79SMark Johnston  * Scan the active queue.  If there is no shortage of inactive pages, scan a
1167be37ee79SMark Johnston  * small portion of the queue in order to maintain quasi-LRU.
1168be37ee79SMark Johnston  */
1169be37ee79SMark Johnston static void
1170be37ee79SMark Johnston vm_pageout_scan_active(struct vm_domain *vmd, int page_shortage)
1171be37ee79SMark Johnston {
1172be37ee79SMark Johnston 	struct scan_state ss;
1173fee2a2faSMark Johnston 	vm_object_t object;
1174be37ee79SMark Johnston 	vm_page_t m, marker;
1175be37ee79SMark Johnston 	struct vm_pagequeue *pq;
1176f3f38e25SMark Johnston 	vm_page_astate_t old, new;
1177be37ee79SMark Johnston 	long min_scan;
1178f3f38e25SMark Johnston 	int act_delta, max_scan, ps_delta, refs, scan_tick;
1179f3f38e25SMark Johnston 	uint8_t nqueue;
1180be37ee79SMark Johnston 
1181be37ee79SMark Johnston 	marker = &vmd->vmd_markers[PQ_ACTIVE];
1182be37ee79SMark Johnston 	pq = &vmd->vmd_pagequeues[PQ_ACTIVE];
1183be37ee79SMark Johnston 	vm_pagequeue_lock(pq);
1184be37ee79SMark Johnston 
1185be37ee79SMark Johnston 	/*
1186be37ee79SMark Johnston 	 * If we're just idle polling attempt to visit every
1187be37ee79SMark Johnston 	 * active page within 'update_period' seconds.
1188be37ee79SMark Johnston 	 */
1189be37ee79SMark Johnston 	scan_tick = ticks;
1190be37ee79SMark Johnston 	if (vm_pageout_update_period != 0) {
1191be37ee79SMark Johnston 		min_scan = pq->pq_cnt;
1192be37ee79SMark Johnston 		min_scan *= scan_tick - vmd->vmd_last_active_scan;
1193be37ee79SMark Johnston 		min_scan /= hz * vm_pageout_update_period;
1194be37ee79SMark Johnston 	} else
1195be37ee79SMark Johnston 		min_scan = 0;
1196be37ee79SMark Johnston 	if (min_scan > 0 || (page_shortage > 0 && pq->pq_cnt > 0))
1197be37ee79SMark Johnston 		vmd->vmd_last_active_scan = scan_tick;
1198be37ee79SMark Johnston 
1199be37ee79SMark Johnston 	/*
1200be37ee79SMark Johnston 	 * Scan the active queue for pages that can be deactivated.  Update
1201be37ee79SMark Johnston 	 * the per-page activity counter and use it to identify deactivation
1202be37ee79SMark Johnston 	 * candidates.  Held pages may be deactivated.
1203be37ee79SMark Johnston 	 *
1204be37ee79SMark Johnston 	 * To avoid requeuing each page that remains in the active queue, we
12057bb4634eSMark Johnston 	 * implement the CLOCK algorithm.  To keep the implementation of the
12067bb4634eSMark Johnston 	 * enqueue operation consistent for all page queues, we use two hands,
12077bb4634eSMark Johnston 	 * represented by marker pages. Scans begin at the first hand, which
12087bb4634eSMark Johnston 	 * precedes the second hand in the queue.  When the two hands meet,
12097bb4634eSMark Johnston 	 * they are moved back to the head and tail of the queue, respectively,
12107bb4634eSMark Johnston 	 * and scanning resumes.
1211be37ee79SMark Johnston 	 */
1212be37ee79SMark Johnston 	max_scan = page_shortage > 0 ? pq->pq_cnt : min_scan;
1213be37ee79SMark Johnston act_scan:
1214be37ee79SMark Johnston 	vm_pageout_init_scan(&ss, pq, marker, &vmd->vmd_clock[0], max_scan);
1215be37ee79SMark Johnston 	while ((m = vm_pageout_next(&ss, false)) != NULL) {
1216be37ee79SMark Johnston 		if (__predict_false(m == &vmd->vmd_clock[1])) {
1217be37ee79SMark Johnston 			vm_pagequeue_lock(pq);
1218be37ee79SMark Johnston 			TAILQ_REMOVE(&pq->pq_pl, &vmd->vmd_clock[0], plinks.q);
1219be37ee79SMark Johnston 			TAILQ_REMOVE(&pq->pq_pl, &vmd->vmd_clock[1], plinks.q);
1220be37ee79SMark Johnston 			TAILQ_INSERT_HEAD(&pq->pq_pl, &vmd->vmd_clock[0],
1221be37ee79SMark Johnston 			    plinks.q);
1222be37ee79SMark Johnston 			TAILQ_INSERT_TAIL(&pq->pq_pl, &vmd->vmd_clock[1],
1223be37ee79SMark Johnston 			    plinks.q);
1224be37ee79SMark Johnston 			max_scan -= ss.scanned;
1225be37ee79SMark Johnston 			vm_pageout_end_scan(&ss);
1226be37ee79SMark Johnston 			goto act_scan;
1227be37ee79SMark Johnston 		}
1228be37ee79SMark Johnston 		if (__predict_false((m->flags & PG_MARKER) != 0))
1229be37ee79SMark Johnston 			continue;
1230be37ee79SMark Johnston 
1231e8bcf696SMark Johnston 		/*
1232b7f30bffSMark Johnston 		 * Don't touch a page that was removed from the queue after the
1233b7f30bffSMark Johnston 		 * page queue lock was released.  Otherwise, ensure that any
1234b7f30bffSMark Johnston 		 * pending queue operations, such as dequeues for wired pages,
1235b7f30bffSMark Johnston 		 * are handled.
1236e8bcf696SMark Johnston 		 */
1237b7f30bffSMark Johnston 		if (vm_pageout_defer(m, PQ_ACTIVE, true))
1238e8bcf696SMark Johnston 			continue;
1239e8bcf696SMark Johnston 
1240e8bcf696SMark Johnston 		/*
1241e8bcf696SMark Johnston 		 * A page's object pointer may be set to NULL before
1242e8bcf696SMark Johnston 		 * the object lock is acquired.
1243e8bcf696SMark Johnston 		 */
124423ed568cSMateusz Guzik 		object = atomic_load_ptr(&m->object);
1245fee2a2faSMark Johnston 		if (__predict_false(object == NULL))
1246fee2a2faSMark Johnston 			/*
1247fee2a2faSMark Johnston 			 * The page has been removed from its object.
1248fee2a2faSMark Johnston 			 */
1249fee2a2faSMark Johnston 			continue;
1250fee2a2faSMark Johnston 
1251f3f38e25SMark Johnston 		/* Deferred free of swap space. */
1252f3f38e25SMark Johnston 		if ((m->a.flags & PGA_SWAP_FREE) != 0 &&
1253f3f38e25SMark Johnston 		    VM_OBJECT_TRYWLOCK(object)) {
1254f3f38e25SMark Johnston 			if (m->object == object)
1255f3f38e25SMark Johnston 				vm_pager_page_unswapped(m);
1256f3f38e25SMark Johnston 			VM_OBJECT_WUNLOCK(object);
1257f3f38e25SMark Johnston 		}
1258f3f38e25SMark Johnston 
1259fee2a2faSMark Johnston 		/*
1260be37ee79SMark Johnston 		 * Check to see "how much" the page has been used.
1261d7aeb429SAlan Cox 		 *
1262d7aeb429SAlan Cox 		 * Test PGA_REFERENCED after calling pmap_ts_referenced() so
1263d7aeb429SAlan Cox 		 * that a reference from a concurrently destroyed mapping is
1264d7aeb429SAlan Cox 		 * observed here and now.
1265d7aeb429SAlan Cox 		 *
1266e8bcf696SMark Johnston 		 * Perform an unsynchronized object ref count check.  While
1267e8bcf696SMark Johnston 		 * the page lock ensures that the page is not reallocated to
1268e8bcf696SMark Johnston 		 * another object, in particular, one with unmanaged mappings
1269e8bcf696SMark Johnston 		 * that cannot support pmap_ts_referenced(), two races are,
1270be37ee79SMark Johnston 		 * nonetheless, possible:
1271be37ee79SMark Johnston 		 * 1) The count was transitioning to zero, but we saw a non-
1272e8bcf696SMark Johnston 		 *    zero value.  pmap_ts_referenced() will return zero
1273e8bcf696SMark Johnston 		 *    because the page is not mapped.
1274e8bcf696SMark Johnston 		 * 2) The count was transitioning to one, but we saw zero.
1275e8bcf696SMark Johnston 		 *    This race delays the detection of a new reference.  At
1276e8bcf696SMark Johnston 		 *    worst, we will deactivate and reactivate the page.
1277be37ee79SMark Johnston 		 */
1278f3f38e25SMark Johnston 		refs = object->ref_count != 0 ? pmap_ts_referenced(m) : 0;
1279be37ee79SMark Johnston 
1280f3f38e25SMark Johnston 		old = vm_page_astate_load(m);
1281f3f38e25SMark Johnston 		do {
1282f3f38e25SMark Johnston 			/*
1283f3f38e25SMark Johnston 			 * Check to see if the page has been removed from the
1284f3f38e25SMark Johnston 			 * queue since the first such check.  Leave it alone if
1285f3f38e25SMark Johnston 			 * so, discarding any references collected by
1286f3f38e25SMark Johnston 			 * pmap_ts_referenced().
1287f3f38e25SMark Johnston 			 */
1288609de97eSEric van Gyzen 			if (__predict_false(_vm_page_queue(old) == PQ_NONE)) {
1289609de97eSEric van Gyzen 				ps_delta = 0;
1290f3f38e25SMark Johnston 				break;
1291609de97eSEric van Gyzen 			}
1292a8081778SJeff Roberson 
1293be37ee79SMark Johnston 			/*
1294be37ee79SMark Johnston 			 * Advance or decay the act_count based on recent usage.
1295be37ee79SMark Johnston 			 */
1296f3f38e25SMark Johnston 			new = old;
1297f3f38e25SMark Johnston 			act_delta = refs;
1298f3f38e25SMark Johnston 			if ((old.flags & PGA_REFERENCED) != 0) {
1299f3f38e25SMark Johnston 				new.flags &= ~PGA_REFERENCED;
1300f3f38e25SMark Johnston 				act_delta++;
1301f3f38e25SMark Johnston 			}
1302be37ee79SMark Johnston 			if (act_delta != 0) {
1303f3f38e25SMark Johnston 				new.act_count += ACT_ADVANCE + act_delta;
1304f3f38e25SMark Johnston 				if (new.act_count > ACT_MAX)
1305f3f38e25SMark Johnston 					new.act_count = ACT_MAX;
1306f3f38e25SMark Johnston 			} else {
1307f3f38e25SMark Johnston 				new.act_count -= min(new.act_count,
1308f3f38e25SMark Johnston 				    ACT_DECLINE);
1309f3f38e25SMark Johnston 			}
1310be37ee79SMark Johnston 
1311f3f38e25SMark Johnston 			if (new.act_count > 0) {
1312be37ee79SMark Johnston 				/*
1313f3f38e25SMark Johnston 				 * Adjust the activation count and keep the page
1314f3f38e25SMark Johnston 				 * in the active queue.  The count might be left
1315f3f38e25SMark Johnston 				 * unchanged if it is saturated.  The page may
1316f3f38e25SMark Johnston 				 * have been moved to a different queue since we
1317f3f38e25SMark Johnston 				 * started the scan, in which case we move it
1318f3f38e25SMark Johnston 				 * back.
1319be37ee79SMark Johnston 				 */
1320f3f38e25SMark Johnston 				ps_delta = 0;
1321f3f38e25SMark Johnston 				if (old.queue != PQ_ACTIVE) {
1322f7607c30SMark Johnston 					new.flags &= ~PGA_QUEUE_OP_MASK;
1323f7607c30SMark Johnston 					new.flags |= PGA_REQUEUE;
1324f7607c30SMark Johnston 					new.queue = PQ_ACTIVE;
1325f3f38e25SMark Johnston 				}
13267cdeaf33SMark Johnston 			} else {
1327be37ee79SMark Johnston 				/*
1328f3f38e25SMark Johnston 				 * When not short for inactive pages, let dirty
1329f3f38e25SMark Johnston 				 * pages go through the inactive queue before
1330f3f38e25SMark Johnston 				 * moving to the laundry queue.  This gives them
1331f3f38e25SMark Johnston 				 * some extra time to be reactivated,
1332f3f38e25SMark Johnston 				 * potentially avoiding an expensive pageout.
1333f3f38e25SMark Johnston 				 * However, during a page shortage, the inactive
1334f3f38e25SMark Johnston 				 * queue is necessarily small, and so dirty
1335f3f38e25SMark Johnston 				 * pages would only spend a trivial amount of
1336f3f38e25SMark Johnston 				 * time in the inactive queue.  Therefore, we
1337f3f38e25SMark Johnston 				 * might as well place them directly in the
1338f3f38e25SMark Johnston 				 * laundry queue to reduce queuing overhead.
1339f3f38e25SMark Johnston 				 *
1340be37ee79SMark Johnston 				 * Calling vm_page_test_dirty() here would
1341be37ee79SMark Johnston 				 * require acquisition of the object's write
1342be37ee79SMark Johnston 				 * lock.  However, during a page shortage,
1343f3f38e25SMark Johnston 				 * directing dirty pages into the laundry queue
1344f3f38e25SMark Johnston 				 * is only an optimization and not a
1345be37ee79SMark Johnston 				 * requirement.  Therefore, we simply rely on
1346f3f38e25SMark Johnston 				 * the opportunistic updates to the page's dirty
1347f3f38e25SMark Johnston 				 * field by the pmap.
1348be37ee79SMark Johnston 				 */
1349f3f38e25SMark Johnston 				if (page_shortage <= 0) {
1350f3f38e25SMark Johnston 					nqueue = PQ_INACTIVE;
1351f3f38e25SMark Johnston 					ps_delta = 0;
1352f3f38e25SMark Johnston 				} else if (m->dirty == 0) {
1353f3f38e25SMark Johnston 					nqueue = PQ_INACTIVE;
1354f3f38e25SMark Johnston 					ps_delta = act_scan_laundry_weight;
1355be37ee79SMark Johnston 				} else {
1356f3f38e25SMark Johnston 					nqueue = PQ_LAUNDRY;
1357f3f38e25SMark Johnston 					ps_delta = 1;
1358be37ee79SMark Johnston 				}
1359f3f38e25SMark Johnston 
1360f7607c30SMark Johnston 				new.flags &= ~PGA_QUEUE_OP_MASK;
1361f3f38e25SMark Johnston 				new.flags |= PGA_REQUEUE;
1362f3f38e25SMark Johnston 				new.queue = nqueue;
1363be37ee79SMark Johnston 			}
1364f3f38e25SMark Johnston 		} while (!vm_page_pqstate_commit(m, &old, new));
1365f3f38e25SMark Johnston 
1366f3f38e25SMark Johnston 		page_shortage -= ps_delta;
1367be37ee79SMark Johnston 	}
1368be37ee79SMark Johnston 	vm_pagequeue_lock(pq);
1369be37ee79SMark Johnston 	TAILQ_REMOVE(&pq->pq_pl, &vmd->vmd_clock[0], plinks.q);
1370be37ee79SMark Johnston 	TAILQ_INSERT_AFTER(&pq->pq_pl, marker, &vmd->vmd_clock[0], plinks.q);
1371be37ee79SMark Johnston 	vm_pageout_end_scan(&ss);
1372be37ee79SMark Johnston 	vm_pagequeue_unlock(pq);
1373be37ee79SMark Johnston }
1374be37ee79SMark Johnston 
13755cd29d0fSMark Johnston static int
1376f3f38e25SMark Johnston vm_pageout_reinsert_inactive_page(struct vm_pagequeue *pq, vm_page_t marker,
1377f3f38e25SMark Johnston     vm_page_t m)
13785cd29d0fSMark Johnston {
1379f3f38e25SMark Johnston 	vm_page_astate_t as;
13805cd29d0fSMark Johnston 
1381f3f38e25SMark Johnston 	vm_pagequeue_assert_locked(pq);
1382f3f38e25SMark Johnston 
1383f3f38e25SMark Johnston 	as = vm_page_astate_load(m);
1384f3f38e25SMark Johnston 	if (as.queue != PQ_INACTIVE || (as.flags & PGA_ENQUEUED) != 0)
1385e8bcf696SMark Johnston 		return (0);
1386e8bcf696SMark Johnston 	vm_page_aflag_set(m, PGA_ENQUEUED);
1387f3f38e25SMark Johnston 	TAILQ_INSERT_BEFORE(marker, m, plinks.q);
13885cd29d0fSMark Johnston 	return (1);
13895cd29d0fSMark Johnston }
13905cd29d0fSMark Johnston 
13915cd29d0fSMark Johnston /*
13925cd29d0fSMark Johnston  * Re-add stuck pages to the inactive queue.  We will examine them again
13935cd29d0fSMark Johnston  * during the next scan.  If the queue state of a page has changed since
13945cd29d0fSMark Johnston  * it was physically removed from the page queue in
13955cd29d0fSMark Johnston  * vm_pageout_collect_batch(), don't do anything with that page.
13965cd29d0fSMark Johnston  */
13975cd29d0fSMark Johnston static void
13985cd29d0fSMark Johnston vm_pageout_reinsert_inactive(struct scan_state *ss, struct vm_batchqueue *bq,
13995cd29d0fSMark Johnston     vm_page_t m)
14005cd29d0fSMark Johnston {
14015cd29d0fSMark Johnston 	struct vm_pagequeue *pq;
1402f3f38e25SMark Johnston 	vm_page_t marker;
14035cd29d0fSMark Johnston 	int delta;
14045cd29d0fSMark Johnston 
14055cd29d0fSMark Johnston 	delta = 0;
1406f3f38e25SMark Johnston 	marker = ss->marker;
14075cd29d0fSMark Johnston 	pq = ss->pq;
14085cd29d0fSMark Johnston 
14095cd29d0fSMark Johnston 	if (m != NULL) {
14105cd29d0fSMark Johnston 		if (vm_batchqueue_insert(bq, m))
14115cd29d0fSMark Johnston 			return;
14125cd29d0fSMark Johnston 		vm_pagequeue_lock(pq);
1413f3f38e25SMark Johnston 		delta += vm_pageout_reinsert_inactive_page(pq, marker, m);
14145cd29d0fSMark Johnston 	} else
14155cd29d0fSMark Johnston 		vm_pagequeue_lock(pq);
14165cd29d0fSMark Johnston 	while ((m = vm_batchqueue_pop(bq)) != NULL)
1417f3f38e25SMark Johnston 		delta += vm_pageout_reinsert_inactive_page(pq, marker, m);
14185cd29d0fSMark Johnston 	vm_pagequeue_cnt_add(pq, delta);
14195cd29d0fSMark Johnston 	vm_pagequeue_unlock(pq);
14205cd29d0fSMark Johnston 	vm_batchqueue_init(bq);
14215cd29d0fSMark Johnston }
14225cd29d0fSMark Johnston 
14230292c54bSConrad Meyer static void
14240292c54bSConrad Meyer vm_pageout_scan_inactive(struct vm_domain *vmd, int page_shortage)
1425df8bae1dSRodney W. Grimes {
14260292c54bSConrad Meyer 	struct timeval start, end;
14275cd29d0fSMark Johnston 	struct scan_state ss;
14285cd29d0fSMark Johnston 	struct vm_batchqueue rq;
14290292c54bSConrad Meyer 	struct vm_page marker_page;
14305cd29d0fSMark Johnston 	vm_page_t m, marker;
14318d220203SAlan Cox 	struct vm_pagequeue *pq;
1432df8bae1dSRodney W. Grimes 	vm_object_t object;
1433f3f38e25SMark Johnston 	vm_page_astate_t old, new;
14340292c54bSConrad Meyer 	int act_delta, addl_page_shortage, starting_page_shortage, refs;
14350292c54bSConrad Meyer 
14360292c54bSConrad Meyer 	object = NULL;
14370292c54bSConrad Meyer 	vm_batchqueue_init(&rq);
14380292c54bSConrad Meyer 	getmicrouptime(&start);
14390d94caffSDavid Greenman 
1440df8bae1dSRodney W. Grimes 	/*
144101f04471SMark Johnston 	 * The addl_page_shortage is an estimate of the number of temporarily
1442311e34e2SKonstantin Belousov 	 * stuck pages in the inactive queue.  In other words, the
1443449c2e92SKonstantin Belousov 	 * number of pages from the inactive count that should be
1444311e34e2SKonstantin Belousov 	 * discounted in setting the target for the active queue scan.
1445311e34e2SKonstantin Belousov 	 */
14469099545aSAlan Cox 	addl_page_shortage = 0;
14479099545aSAlan Cox 
14481c7c3c6aSMatthew Dillon 	/*
1449f095d1bbSAlan Cox 	 * Start scanning the inactive queue for pages that we can free.  The
1450f095d1bbSAlan Cox 	 * scan will stop when we reach the target or we have scanned the
14515cff1f4dSMark Johnston 	 * entire queue.  (Note that m->a.act_count is not used to make
1452f095d1bbSAlan Cox 	 * decisions for the inactive queue, only for the active queue.)
14538d220203SAlan Cox 	 */
14540292c54bSConrad Meyer 	starting_page_shortage = page_shortage;
14550292c54bSConrad Meyer 	marker = &marker_page;
14560292c54bSConrad Meyer 	vm_page_init_marker(marker, PQ_INACTIVE, 0);
14575cd29d0fSMark Johnston 	pq = &vmd->vmd_pagequeues[PQ_INACTIVE];
14588d220203SAlan Cox 	vm_pagequeue_lock(pq);
14595cd29d0fSMark Johnston 	vm_pageout_init_scan(&ss, pq, marker, NULL, pq->pq_cnt);
14605cd29d0fSMark Johnston 	while (page_shortage > 0 && (m = vm_pageout_next(&ss, true)) != NULL) {
14615cd29d0fSMark Johnston 		KASSERT((m->flags & PG_MARKER) == 0,
14625cd29d0fSMark Johnston 		    ("marker page %p was dequeued", m));
1463df8bae1dSRodney W. Grimes 
1464936524aaSMatthew Dillon 		/*
1465b7f30bffSMark Johnston 		 * Don't touch a page that was removed from the queue after the
1466b7f30bffSMark Johnston 		 * page queue lock was released.  Otherwise, ensure that any
1467b7f30bffSMark Johnston 		 * pending queue operations, such as dequeues for wired pages,
1468b7f30bffSMark Johnston 		 * are handled.
1469936524aaSMatthew Dillon 		 */
1470b7f30bffSMark Johnston 		if (vm_pageout_defer(m, PQ_INACTIVE, false))
1471936524aaSMatthew Dillon 			continue;
1472e8bcf696SMark Johnston 
14739f5632e6SMark Johnston 		/*
14749f5632e6SMark Johnston 		 * Lock the page's object.
14759f5632e6SMark Johnston 		 */
14769f5632e6SMark Johnston 		if (object == NULL || object != m->object) {
147760256604SMark Johnston 			if (object != NULL)
14785cd29d0fSMark Johnston 				VM_OBJECT_WUNLOCK(object);
147923ed568cSMateusz Guzik 			object = atomic_load_ptr(&m->object);
14809f5632e6SMark Johnston 			if (__predict_false(object == NULL))
14819f5632e6SMark Johnston 				/* The page is being freed by another thread. */
14829f5632e6SMark Johnston 				continue;
14839f5632e6SMark Johnston 
1484e8bcf696SMark Johnston 			/* Depends on type-stability. */
148541fd4b94SMark Johnston 			VM_OBJECT_WLOCK(object);
14869f5632e6SMark Johnston 			if (__predict_false(m->object != object)) {
14879f5632e6SMark Johnston 				VM_OBJECT_WUNLOCK(object);
14889f5632e6SMark Johnston 				object = NULL;
14899f5632e6SMark Johnston 				goto reinsert;
149041fd4b94SMark Johnston 			}
149141fd4b94SMark Johnston 		}
14925cd29d0fSMark Johnston 
149363e97555SJeff Roberson 		if (vm_page_tryxbusy(m) == 0) {
1494a3aeedabSAlan Cox 			/*
1495a3aeedabSAlan Cox 			 * Don't mess with busy pages.  Leave them at
1496a3aeedabSAlan Cox 			 * the front of the queue.  Most likely, they
1497a3aeedabSAlan Cox 			 * are being paged out and will leave the
1498a3aeedabSAlan Cox 			 * queue shortly after the scan finishes.  So,
1499a3aeedabSAlan Cox 			 * they ought to be discounted from the
1500a3aeedabSAlan Cox 			 * inactive count.
1501a3aeedabSAlan Cox 			 */
1502a3aeedabSAlan Cox 			addl_page_shortage++;
15035cd29d0fSMark Johnston 			goto reinsert;
150426f9a767SRodney W. Grimes 		}
150548cc2fc7SKonstantin Belousov 
1506a8081778SJeff Roberson 		/* Deferred free of swap space. */
1507a8081778SJeff Roberson 		if ((m->a.flags & PGA_SWAP_FREE) != 0)
1508a8081778SJeff Roberson 			vm_pager_page_unswapped(m);
1509a8081778SJeff Roberson 
151048cc2fc7SKonstantin Belousov 		/*
15119f5632e6SMark Johnston 		 * Check for wirings now that we hold the object lock and have
15129f5632e6SMark Johnston 		 * exclusively busied the page.  If the page is mapped, it may
15139f5632e6SMark Johnston 		 * still be wired by pmap lookups.  The call to
1514fee2a2faSMark Johnston 		 * vm_page_try_remove_all() below atomically checks for such
1515fee2a2faSMark Johnston 		 * wirings and removes mappings.  If the page is unmapped, the
15169f5632e6SMark Johnston 		 * wire count is guaranteed not to increase after this check.
1517fee2a2faSMark Johnston 		 */
15189f5632e6SMark Johnston 		if (__predict_false(vm_page_wired(m)))
1519f3f38e25SMark Johnston 			goto skip_page;
1520fee2a2faSMark Johnston 
1521fee2a2faSMark Johnston 		/*
15228748f58cSKonstantin Belousov 		 * Invalid pages can be easily freed. They cannot be
15238748f58cSKonstantin Belousov 		 * mapped, vm_page_free() asserts this.
1524776f729cSKonstantin Belousov 		 */
15250012f373SJeff Roberson 		if (vm_page_none_valid(m))
15268748f58cSKonstantin Belousov 			goto free_page;
1527776f729cSKonstantin Belousov 
1528f3f38e25SMark Johnston 		refs = object->ref_count != 0 ? pmap_ts_referenced(m) : 0;
1529f3f38e25SMark Johnston 
1530f3f38e25SMark Johnston 		for (old = vm_page_astate_load(m);;) {
1531776f729cSKonstantin Belousov 			/*
1532f3f38e25SMark Johnston 			 * Check to see if the page has been removed from the
1533f3f38e25SMark Johnston 			 * queue since the first such check.  Leave it alone if
1534f3f38e25SMark Johnston 			 * so, discarding any references collected by
1535f3f38e25SMark Johnston 			 * pmap_ts_referenced().
15367e006499SJohn Dyson 			 */
1537f3f38e25SMark Johnston 			if (__predict_false(_vm_page_queue(old) == PQ_NONE))
1538f3f38e25SMark Johnston 				goto skip_page;
1539f3f38e25SMark Johnston 
1540f3f38e25SMark Johnston 			new = old;
1541f3f38e25SMark Johnston 			act_delta = refs;
1542f3f38e25SMark Johnston 			if ((old.flags & PGA_REFERENCED) != 0) {
1543f3f38e25SMark Johnston 				new.flags &= ~PGA_REFERENCED;
1544d7aeb429SAlan Cox 				act_delta++;
15452fe6e4d7SDavid Greenman 			}
1546f3f38e25SMark Johnston 			if (act_delta == 0) {
1547f3f38e25SMark Johnston 				;
1548f3f38e25SMark Johnston 			} else if (object->ref_count != 0) {
1549e8bcf696SMark Johnston 				/*
1550f3f38e25SMark Johnston 				 * Increase the activation count if the
1551f3f38e25SMark Johnston 				 * page was referenced while in the
1552f3f38e25SMark Johnston 				 * inactive queue.  This makes it less
1553f3f38e25SMark Johnston 				 * likely that the page will be returned
1554f3f38e25SMark Johnston 				 * prematurely to the inactive queue.
1555e8bcf696SMark Johnston 				 */
1556f3f38e25SMark Johnston 				new.act_count += ACT_ADVANCE +
1557f3f38e25SMark Johnston 				    act_delta;
1558f3f38e25SMark Johnston 				if (new.act_count > ACT_MAX)
1559f3f38e25SMark Johnston 					new.act_count = ACT_MAX;
1560f3f38e25SMark Johnston 
1561f7607c30SMark Johnston 				new.flags &= ~PGA_QUEUE_OP_MASK;
1562f3f38e25SMark Johnston 				new.flags |= PGA_REQUEUE;
1563f3f38e25SMark Johnston 				new.queue = PQ_ACTIVE;
1564f3f38e25SMark Johnston 				if (!vm_page_pqstate_commit(m, &old, new))
1565e8bcf696SMark Johnston 					continue;
1566f3f38e25SMark Johnston 
1567f3f38e25SMark Johnston 				VM_CNT_INC(v_reactivated);
1568f3f38e25SMark Johnston 				goto skip_page;
1569ebcddc72SAlan Cox 			} else if ((object->flags & OBJ_DEAD) == 0) {
1570f3f38e25SMark Johnston 				new.queue = PQ_INACTIVE;
1571f3f38e25SMark Johnston 				new.flags |= PGA_REQUEUE;
1572f3f38e25SMark Johnston 				if (!vm_page_pqstate_commit(m, &old, new))
1573f3f38e25SMark Johnston 					continue;
1574f3f38e25SMark Johnston 				goto skip_page;
1575ebcddc72SAlan Cox 			}
1576f3f38e25SMark Johnston 			break;
1577960810ccSAlan Cox 		}
157867bf6868SJohn Dyson 
15797e006499SJohn Dyson 		/*
15809fc4739dSAlan Cox 		 * If the page appears to be clean at the machine-independent
15819fc4739dSAlan Cox 		 * layer, then remove all of its mappings from the pmap in
1582a766ffd0SAlan Cox 		 * anticipation of freeing it.  If, however, any of the page's
1583a766ffd0SAlan Cox 		 * mappings allow write access, then the page may still be
1584a766ffd0SAlan Cox 		 * modified until the last of those mappings are removed.
15857e006499SJohn Dyson 		 */
1586aa044135SAlan Cox 		if (object->ref_count != 0) {
15879fc4739dSAlan Cox 			vm_page_test_dirty(m);
15889f5632e6SMark Johnston 			if (m->dirty == 0 && !vm_page_try_remove_all(m))
1589f3f38e25SMark Johnston 				goto skip_page;
1590fee2a2faSMark Johnston 		}
1591dcbcd518SBruce Evans 
15926989c456SAlan Cox 		/*
1593ebcddc72SAlan Cox 		 * Clean pages can be freed, but dirty pages must be sent back
1594ebcddc72SAlan Cox 		 * to the laundry, unless they belong to a dead object.
1595ebcddc72SAlan Cox 		 * Requeueing dirty pages from dead objects is pointless, as
1596ebcddc72SAlan Cox 		 * they are being paged out and freed by the thread that
1597ebcddc72SAlan Cox 		 * destroyed the object.
15986989c456SAlan Cox 		 */
1599ebcddc72SAlan Cox 		if (m->dirty == 0) {
16008748f58cSKonstantin Belousov free_page:
16015cd29d0fSMark Johnston 			/*
16029f5632e6SMark Johnston 			 * Now we are guaranteed that no other threads are
16039f5632e6SMark Johnston 			 * manipulating the page, check for a last-second
16049f5632e6SMark Johnston 			 * reference that would save it from doom.
16055cd29d0fSMark Johnston 			 */
16069f5632e6SMark Johnston 			if (vm_pageout_defer(m, PQ_INACTIVE, false))
16079f5632e6SMark Johnston 				goto skip_page;
16089f5632e6SMark Johnston 
16099f5632e6SMark Johnston 			/*
16109f5632e6SMark Johnston 			 * Because we dequeued the page and have already checked
16119f5632e6SMark Johnston 			 * for pending dequeue and enqueue requests, we can
16129f5632e6SMark Johnston 			 * safely disassociate the page from the inactive queue
16139f5632e6SMark Johnston 			 * without holding the queue lock.
16149f5632e6SMark Johnston 			 */
16155cff1f4dSMark Johnston 			m->a.queue = PQ_NONE;
161678afdce6SAlan Cox 			vm_page_free(m);
16175cd29d0fSMark Johnston 			page_shortage--;
161863e97555SJeff Roberson 			continue;
161963e97555SJeff Roberson 		}
162063e97555SJeff Roberson 		if ((object->flags & OBJ_DEAD) == 0)
1621ebcddc72SAlan Cox 			vm_page_launder(m);
1622f3f38e25SMark Johnston skip_page:
1623f3f38e25SMark Johnston 		vm_page_xunbusy(m);
16245cd29d0fSMark Johnston 		continue;
16255cd29d0fSMark Johnston reinsert:
16265cd29d0fSMark Johnston 		vm_pageout_reinsert_inactive(&ss, &rq, m);
16275cd29d0fSMark Johnston 	}
162860256604SMark Johnston 	if (object != NULL)
162989f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
16305cd29d0fSMark Johnston 	vm_pageout_reinsert_inactive(&ss, &rq, NULL);
16315cd29d0fSMark Johnston 	vm_pageout_reinsert_inactive(&ss, &ss.bq, NULL);
16328d220203SAlan Cox 	vm_pagequeue_lock(pq);
16335cd29d0fSMark Johnston 	vm_pageout_end_scan(&ss);
16348d220203SAlan Cox 	vm_pagequeue_unlock(pq);
163526f9a767SRodney W. Grimes 
16360292c54bSConrad Meyer 	/*
16370292c54bSConrad Meyer 	 * Record the remaining shortage and the progress and rate it was made.
16380292c54bSConrad Meyer 	 */
16390292c54bSConrad Meyer 	atomic_add_int(&vmd->vmd_addl_shortage, addl_page_shortage);
16400292c54bSConrad Meyer 	getmicrouptime(&end);
16410292c54bSConrad Meyer 	timevalsub(&end, &start);
16420292c54bSConrad Meyer 	atomic_add_int(&vmd->vmd_inactive_us,
16430292c54bSConrad Meyer 	    end.tv_sec * 1000000 + end.tv_usec);
16440292c54bSConrad Meyer 	atomic_add_int(&vmd->vmd_inactive_freed,
16450292c54bSConrad Meyer 	    starting_page_shortage - page_shortage);
16460292c54bSConrad Meyer }
16470292c54bSConrad Meyer 
16480292c54bSConrad Meyer /*
16490292c54bSConrad Meyer  * Dispatch a number of inactive threads according to load and collect the
16502913cc46SMark Johnston  * results to present a coherent view of paging activity on this domain.
16510292c54bSConrad Meyer  */
16520292c54bSConrad Meyer static int
16530292c54bSConrad Meyer vm_pageout_inactive_dispatch(struct vm_domain *vmd, int shortage)
16540292c54bSConrad Meyer {
16552913cc46SMark Johnston 	u_int freed, pps, slop, threads, us;
16560292c54bSConrad Meyer 
16570292c54bSConrad Meyer 	vmd->vmd_inactive_shortage = shortage;
16582913cc46SMark Johnston 	slop = 0;
16590292c54bSConrad Meyer 
16600292c54bSConrad Meyer 	/*
16610292c54bSConrad Meyer 	 * If we have more work than we can do in a quarter of our interval, we
16620292c54bSConrad Meyer 	 * fire off multiple threads to process it.
16630292c54bSConrad Meyer 	 */
16640292c54bSConrad Meyer 	threads = vmd->vmd_inactive_threads;
16652913cc46SMark Johnston 	if (threads > 1 && vmd->vmd_inactive_pps != 0 &&
16662913cc46SMark Johnston 	    shortage > vmd->vmd_inactive_pps / VM_INACT_SCAN_RATE / 4) {
16670292c54bSConrad Meyer 		vmd->vmd_inactive_shortage /= threads;
16682913cc46SMark Johnston 		slop = shortage % threads;
16692913cc46SMark Johnston 		vm_domain_pageout_lock(vmd);
16700292c54bSConrad Meyer 		blockcount_acquire(&vmd->vmd_inactive_starting, threads - 1);
16710292c54bSConrad Meyer 		blockcount_acquire(&vmd->vmd_inactive_running, threads - 1);
16720292c54bSConrad Meyer 		wakeup(&vmd->vmd_inactive_shortage);
16730292c54bSConrad Meyer 		vm_domain_pageout_unlock(vmd);
16740292c54bSConrad Meyer 	}
16750292c54bSConrad Meyer 
16760292c54bSConrad Meyer 	/* Run the local thread scan. */
16772913cc46SMark Johnston 	vm_pageout_scan_inactive(vmd, vmd->vmd_inactive_shortage + slop);
16780292c54bSConrad Meyer 
16790292c54bSConrad Meyer 	/*
16800292c54bSConrad Meyer 	 * Block until helper threads report results and then accumulate
16810292c54bSConrad Meyer 	 * totals.
16820292c54bSConrad Meyer 	 */
16830292c54bSConrad Meyer 	blockcount_wait(&vmd->vmd_inactive_running, NULL, "vmpoid", PVM);
16840292c54bSConrad Meyer 	freed = atomic_readandclear_int(&vmd->vmd_inactive_freed);
16850292c54bSConrad Meyer 	VM_CNT_ADD(v_dfree, freed);
16860292c54bSConrad Meyer 
16870292c54bSConrad Meyer 	/*
16880292c54bSConrad Meyer 	 * Calculate the per-thread paging rate with an exponential decay of
16890292c54bSConrad Meyer 	 * prior results.  Careful to avoid integer rounding errors with large
16900292c54bSConrad Meyer 	 * us values.
16910292c54bSConrad Meyer 	 */
16920292c54bSConrad Meyer 	us = max(atomic_readandclear_int(&vmd->vmd_inactive_us), 1);
16930292c54bSConrad Meyer 	if (us > 1000000)
16940292c54bSConrad Meyer 		/* Keep rounding to tenths */
16950292c54bSConrad Meyer 		pps = (freed * 10) / ((us * 10) / 1000000);
16960292c54bSConrad Meyer 	else
16970292c54bSConrad Meyer 		pps = (1000000 / us) * freed;
16980292c54bSConrad Meyer 	vmd->vmd_inactive_pps = (vmd->vmd_inactive_pps / 2) + (pps / 2);
16990292c54bSConrad Meyer 
17000292c54bSConrad Meyer 	return (shortage - freed);
17010292c54bSConrad Meyer }
17020292c54bSConrad Meyer 
17030292c54bSConrad Meyer /*
17040292c54bSConrad Meyer  * Attempt to reclaim the requested number of pages from the inactive queue.
17050292c54bSConrad Meyer  * Returns true if the shortage was addressed.
17060292c54bSConrad Meyer  */
17070292c54bSConrad Meyer static int
17080292c54bSConrad Meyer vm_pageout_inactive(struct vm_domain *vmd, int shortage, int *addl_shortage)
17090292c54bSConrad Meyer {
17100292c54bSConrad Meyer 	struct vm_pagequeue *pq;
17110292c54bSConrad Meyer 	u_int addl_page_shortage, deficit, page_shortage;
17120292c54bSConrad Meyer 	u_int starting_page_shortage;
17130292c54bSConrad Meyer 
17140292c54bSConrad Meyer 	/*
17150292c54bSConrad Meyer 	 * vmd_pageout_deficit counts the number of pages requested in
17160292c54bSConrad Meyer 	 * allocations that failed because of a free page shortage.  We assume
17170292c54bSConrad Meyer 	 * that the allocations will be reattempted and thus include the deficit
17180292c54bSConrad Meyer 	 * in our scan target.
17190292c54bSConrad Meyer 	 */
17200292c54bSConrad Meyer 	deficit = atomic_readandclear_int(&vmd->vmd_pageout_deficit);
17210292c54bSConrad Meyer 	starting_page_shortage = shortage + deficit;
17220292c54bSConrad Meyer 
17230292c54bSConrad Meyer 	/*
17240292c54bSConrad Meyer 	 * Run the inactive scan on as many threads as is necessary.
17250292c54bSConrad Meyer 	 */
17260292c54bSConrad Meyer 	page_shortage = vm_pageout_inactive_dispatch(vmd, starting_page_shortage);
17270292c54bSConrad Meyer 	addl_page_shortage = atomic_readandclear_int(&vmd->vmd_addl_shortage);
17285cd29d0fSMark Johnston 
1729ebcddc72SAlan Cox 	/*
1730ebcddc72SAlan Cox 	 * Wake up the laundry thread so that it can perform any needed
1731ebcddc72SAlan Cox 	 * laundering.  If we didn't meet our target, we're in shortfall and
1732b1fd102eSMark Johnston 	 * need to launder more aggressively.  If PQ_LAUNDRY is empty and no
1733b1fd102eSMark Johnston 	 * swap devices are configured, the laundry thread has no work to do, so
1734b1fd102eSMark Johnston 	 * don't bother waking it up.
1735cb35676eSMark Johnston 	 *
1736cb35676eSMark Johnston 	 * The laundry thread uses the number of inactive queue scans elapsed
1737cb35676eSMark Johnston 	 * since the last laundering to determine whether to launder again, so
1738cb35676eSMark Johnston 	 * keep count.
1739ebcddc72SAlan Cox 	 */
1740cb35676eSMark Johnston 	if (starting_page_shortage > 0) {
1741e2068d0bSJeff Roberson 		pq = &vmd->vmd_pagequeues[PQ_LAUNDRY];
1742ebcddc72SAlan Cox 		vm_pagequeue_lock(pq);
1743e2068d0bSJeff Roberson 		if (vmd->vmd_laundry_request == VM_LAUNDRY_IDLE &&
1744cb35676eSMark Johnston 		    (pq->pq_cnt > 0 || atomic_load_acq_int(&swapdev_enabled))) {
1745ebcddc72SAlan Cox 			if (page_shortage > 0) {
1746e2068d0bSJeff Roberson 				vmd->vmd_laundry_request = VM_LAUNDRY_SHORTFALL;
174783c9dea1SGleb Smirnoff 				VM_CNT_INC(v_pdshortfalls);
1748e2068d0bSJeff Roberson 			} else if (vmd->vmd_laundry_request !=
1749e2068d0bSJeff Roberson 			    VM_LAUNDRY_SHORTFALL)
1750e2068d0bSJeff Roberson 				vmd->vmd_laundry_request =
1751e2068d0bSJeff Roberson 				    VM_LAUNDRY_BACKGROUND;
1752e2068d0bSJeff Roberson 			wakeup(&vmd->vmd_laundry_request);
1753b1fd102eSMark Johnston 		}
175460684862SMark Johnston 		vmd->vmd_clean_pages_freed +=
175560684862SMark Johnston 		    starting_page_shortage - page_shortage;
1756ebcddc72SAlan Cox 		vm_pagequeue_unlock(pq);
1757ebcddc72SAlan Cox 	}
1758ebcddc72SAlan Cox 
17599452b5edSAlan Cox 	/*
1760f095d1bbSAlan Cox 	 * Wakeup the swapout daemon if we didn't free the targeted number of
1761f095d1bbSAlan Cox 	 * pages.
17629452b5edSAlan Cox 	 */
1763ac04195bSKonstantin Belousov 	if (page_shortage > 0)
1764ac04195bSKonstantin Belousov 		vm_swapout_run();
17659452b5edSAlan Cox 
17669452b5edSAlan Cox 	/*
176776386c7eSKonstantin Belousov 	 * If the inactive queue scan fails repeatedly to meet its
176876386c7eSKonstantin Belousov 	 * target, kill the largest process.
176976386c7eSKonstantin Belousov 	 */
177076386c7eSKonstantin Belousov 	vm_pageout_mightbe_oom(vmd, page_shortage, starting_page_shortage);
177176386c7eSKonstantin Belousov 
177276386c7eSKonstantin Belousov 	/*
1773be37ee79SMark Johnston 	 * Reclaim pages by swapping out idle processes, if configured to do so.
17741c7c3c6aSMatthew Dillon 	 */
1775ac04195bSKonstantin Belousov 	vm_swapout_run_idle();
1776be37ee79SMark Johnston 
1777be37ee79SMark Johnston 	/*
1778be37ee79SMark Johnston 	 * See the description of addl_page_shortage above.
1779be37ee79SMark Johnston 	 */
1780be37ee79SMark Johnston 	*addl_shortage = addl_page_shortage + deficit;
1781be37ee79SMark Johnston 
1782e57dd910SAlan Cox 	return (page_shortage <= 0);
17832025d69bSKonstantin Belousov }
17842025d69bSKonstantin Belousov 
1785449c2e92SKonstantin Belousov static int vm_pageout_oom_vote;
1786449c2e92SKonstantin Belousov 
1787449c2e92SKonstantin Belousov /*
1788449c2e92SKonstantin Belousov  * The pagedaemon threads randlomly select one to perform the
1789449c2e92SKonstantin Belousov  * OOM.  Trying to kill processes before all pagedaemons
1790449c2e92SKonstantin Belousov  * failed to reach free target is premature.
1791449c2e92SKonstantin Belousov  */
1792449c2e92SKonstantin Belousov static void
179376386c7eSKonstantin Belousov vm_pageout_mightbe_oom(struct vm_domain *vmd, int page_shortage,
179476386c7eSKonstantin Belousov     int starting_page_shortage)
1795449c2e92SKonstantin Belousov {
1796449c2e92SKonstantin Belousov 	int old_vote;
1797449c2e92SKonstantin Belousov 
179876386c7eSKonstantin Belousov 	if (starting_page_shortage <= 0 || starting_page_shortage !=
179976386c7eSKonstantin Belousov 	    page_shortage)
180076386c7eSKonstantin Belousov 		vmd->vmd_oom_seq = 0;
180176386c7eSKonstantin Belousov 	else
180276386c7eSKonstantin Belousov 		vmd->vmd_oom_seq++;
180376386c7eSKonstantin Belousov 	if (vmd->vmd_oom_seq < vm_pageout_oom_seq) {
1804449c2e92SKonstantin Belousov 		if (vmd->vmd_oom) {
1805449c2e92SKonstantin Belousov 			vmd->vmd_oom = FALSE;
1806449c2e92SKonstantin Belousov 			atomic_subtract_int(&vm_pageout_oom_vote, 1);
1807449c2e92SKonstantin Belousov 		}
1808449c2e92SKonstantin Belousov 		return;
1809449c2e92SKonstantin Belousov 	}
1810449c2e92SKonstantin Belousov 
181176386c7eSKonstantin Belousov 	/*
181276386c7eSKonstantin Belousov 	 * Do not follow the call sequence until OOM condition is
181376386c7eSKonstantin Belousov 	 * cleared.
181476386c7eSKonstantin Belousov 	 */
181576386c7eSKonstantin Belousov 	vmd->vmd_oom_seq = 0;
181676386c7eSKonstantin Belousov 
1817449c2e92SKonstantin Belousov 	if (vmd->vmd_oom)
1818449c2e92SKonstantin Belousov 		return;
1819449c2e92SKonstantin Belousov 
1820449c2e92SKonstantin Belousov 	vmd->vmd_oom = TRUE;
1821449c2e92SKonstantin Belousov 	old_vote = atomic_fetchadd_int(&vm_pageout_oom_vote, 1);
1822449c2e92SKonstantin Belousov 	if (old_vote != vm_ndomains - 1)
1823449c2e92SKonstantin Belousov 		return;
1824449c2e92SKonstantin Belousov 
1825449c2e92SKonstantin Belousov 	/*
1826449c2e92SKonstantin Belousov 	 * The current pagedaemon thread is the last in the quorum to
1827449c2e92SKonstantin Belousov 	 * start OOM.  Initiate the selection and signaling of the
1828449c2e92SKonstantin Belousov 	 * victim.
1829449c2e92SKonstantin Belousov 	 */
1830449c2e92SKonstantin Belousov 	vm_pageout_oom(VM_OOM_MEM);
1831449c2e92SKonstantin Belousov 
1832449c2e92SKonstantin Belousov 	/*
1833449c2e92SKonstantin Belousov 	 * After one round of OOM terror, recall our vote.  On the
1834449c2e92SKonstantin Belousov 	 * next pass, current pagedaemon would vote again if the low
1835449c2e92SKonstantin Belousov 	 * memory condition is still there, due to vmd_oom being
1836449c2e92SKonstantin Belousov 	 * false.
1837449c2e92SKonstantin Belousov 	 */
1838449c2e92SKonstantin Belousov 	vmd->vmd_oom = FALSE;
1839449c2e92SKonstantin Belousov 	atomic_subtract_int(&vm_pageout_oom_vote, 1);
1840449c2e92SKonstantin Belousov }
18412025d69bSKonstantin Belousov 
18423949873fSKonstantin Belousov /*
18433949873fSKonstantin Belousov  * The OOM killer is the page daemon's action of last resort when
18443949873fSKonstantin Belousov  * memory allocation requests have been stalled for a prolonged period
18453949873fSKonstantin Belousov  * of time because it cannot reclaim memory.  This function computes
18463949873fSKonstantin Belousov  * the approximate number of physical pages that could be reclaimed if
18473949873fSKonstantin Belousov  * the specified address space is destroyed.
18483949873fSKonstantin Belousov  *
18493949873fSKonstantin Belousov  * Private, anonymous memory owned by the address space is the
18503949873fSKonstantin Belousov  * principal resource that we expect to recover after an OOM kill.
18513949873fSKonstantin Belousov  * Since the physical pages mapped by the address space's COW entries
18523949873fSKonstantin Belousov  * are typically shared pages, they are unlikely to be released and so
18533949873fSKonstantin Belousov  * they are not counted.
18543949873fSKonstantin Belousov  *
18553949873fSKonstantin Belousov  * To get to the point where the page daemon runs the OOM killer, its
18563949873fSKonstantin Belousov  * efforts to write-back vnode-backed pages may have stalled.  This
18573949873fSKonstantin Belousov  * could be caused by a memory allocation deadlock in the write path
18583949873fSKonstantin Belousov  * that might be resolved by an OOM kill.  Therefore, physical pages
18593949873fSKonstantin Belousov  * belonging to vnode-backed objects are counted, because they might
18603949873fSKonstantin Belousov  * be freed without being written out first if the address space holds
18613949873fSKonstantin Belousov  * the last reference to an unlinked vnode.
18623949873fSKonstantin Belousov  *
18633949873fSKonstantin Belousov  * Similarly, physical pages belonging to OBJT_PHYS objects are
18643949873fSKonstantin Belousov  * counted because the address space might hold the last reference to
18653949873fSKonstantin Belousov  * the object.
18663949873fSKonstantin Belousov  */
18673949873fSKonstantin Belousov static long
18683949873fSKonstantin Belousov vm_pageout_oom_pagecount(struct vmspace *vmspace)
18693949873fSKonstantin Belousov {
18703949873fSKonstantin Belousov 	vm_map_t map;
18713949873fSKonstantin Belousov 	vm_map_entry_t entry;
18723949873fSKonstantin Belousov 	vm_object_t obj;
18733949873fSKonstantin Belousov 	long res;
18743949873fSKonstantin Belousov 
18753949873fSKonstantin Belousov 	map = &vmspace->vm_map;
18763949873fSKonstantin Belousov 	KASSERT(!map->system_map, ("system map"));
18773949873fSKonstantin Belousov 	sx_assert(&map->lock, SA_LOCKED);
18783949873fSKonstantin Belousov 	res = 0;
18792288078cSDoug Moore 	VM_MAP_ENTRY_FOREACH(entry, map) {
18803949873fSKonstantin Belousov 		if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0)
18813949873fSKonstantin Belousov 			continue;
18823949873fSKonstantin Belousov 		obj = entry->object.vm_object;
18833949873fSKonstantin Belousov 		if (obj == NULL)
18843949873fSKonstantin Belousov 			continue;
18853949873fSKonstantin Belousov 		if ((entry->eflags & MAP_ENTRY_NEEDS_COPY) != 0 &&
18863949873fSKonstantin Belousov 		    obj->ref_count != 1)
18873949873fSKonstantin Belousov 			continue;
18887079449bSKonstantin Belousov 		if (obj->type == OBJT_DEFAULT || obj->type == OBJT_PHYS ||
18897079449bSKonstantin Belousov 		    obj->type == OBJT_VNODE || (obj->flags & OBJ_SWAP) != 0)
18903949873fSKonstantin Belousov 			res += obj->resident_page_count;
18913949873fSKonstantin Belousov 	}
18923949873fSKonstantin Belousov 	return (res);
18933949873fSKonstantin Belousov }
18943949873fSKonstantin Belousov 
1895245139c6SKonstantin Belousov static int vm_oom_ratelim_last;
1896245139c6SKonstantin Belousov static int vm_oom_pf_secs = 10;
1897245139c6SKonstantin Belousov SYSCTL_INT(_vm, OID_AUTO, oom_pf_secs, CTLFLAG_RWTUN, &vm_oom_pf_secs, 0,
1898245139c6SKonstantin Belousov     "");
1899245139c6SKonstantin Belousov static struct mtx vm_oom_ratelim_mtx;
1900245139c6SKonstantin Belousov 
19012025d69bSKonstantin Belousov void
19022025d69bSKonstantin Belousov vm_pageout_oom(int shortage)
19032025d69bSKonstantin Belousov {
19042025d69bSKonstantin Belousov 	struct proc *p, *bigproc;
19052025d69bSKonstantin Belousov 	vm_offset_t size, bigsize;
19062025d69bSKonstantin Belousov 	struct thread *td;
19076bed074cSKonstantin Belousov 	struct vmspace *vm;
1908245139c6SKonstantin Belousov 	int now;
19093e78e983SAlan Cox 	bool breakout;
19102025d69bSKonstantin Belousov 
19112025d69bSKonstantin Belousov 	/*
1912245139c6SKonstantin Belousov 	 * For OOM requests originating from vm_fault(), there is a high
1913245139c6SKonstantin Belousov 	 * chance that a single large process faults simultaneously in
1914245139c6SKonstantin Belousov 	 * several threads.  Also, on an active system running many
1915245139c6SKonstantin Belousov 	 * processes of middle-size, like buildworld, all of them
1916245139c6SKonstantin Belousov 	 * could fault almost simultaneously as well.
1917245139c6SKonstantin Belousov 	 *
1918245139c6SKonstantin Belousov 	 * To avoid killing too many processes, rate-limit OOMs
1919245139c6SKonstantin Belousov 	 * initiated by vm_fault() time-outs on the waits for free
1920245139c6SKonstantin Belousov 	 * pages.
1921245139c6SKonstantin Belousov 	 */
1922245139c6SKonstantin Belousov 	mtx_lock(&vm_oom_ratelim_mtx);
1923245139c6SKonstantin Belousov 	now = ticks;
1924245139c6SKonstantin Belousov 	if (shortage == VM_OOM_MEM_PF &&
1925245139c6SKonstantin Belousov 	    (u_int)(now - vm_oom_ratelim_last) < hz * vm_oom_pf_secs) {
1926245139c6SKonstantin Belousov 		mtx_unlock(&vm_oom_ratelim_mtx);
1927245139c6SKonstantin Belousov 		return;
1928245139c6SKonstantin Belousov 	}
1929245139c6SKonstantin Belousov 	vm_oom_ratelim_last = now;
1930245139c6SKonstantin Belousov 	mtx_unlock(&vm_oom_ratelim_mtx);
1931245139c6SKonstantin Belousov 
1932245139c6SKonstantin Belousov 	/*
19331c58e4e5SJohn Baldwin 	 * We keep the process bigproc locked once we find it to keep anyone
19341c58e4e5SJohn Baldwin 	 * from messing with it; however, there is a possibility of
193528323addSBryan Drewery 	 * deadlock if process B is bigproc and one of its child processes
19361c58e4e5SJohn Baldwin 	 * attempts to propagate a signal to B while we are waiting for A's
19371c58e4e5SJohn Baldwin 	 * lock while walking this list.  To avoid this, we don't block on
19381c58e4e5SJohn Baldwin 	 * the process lock but just skip a process if it is already locked.
19395663e6deSDavid Greenman 	 */
19405663e6deSDavid Greenman 	bigproc = NULL;
19415663e6deSDavid Greenman 	bigsize = 0;
19421005a129SJohn Baldwin 	sx_slock(&allproc_lock);
1943e602ba25SJulian Elischer 	FOREACH_PROC_IN_SYSTEM(p) {
194471943c3dSKonstantin Belousov 		PROC_LOCK(p);
194571943c3dSKonstantin Belousov 
19461c58e4e5SJohn Baldwin 		/*
19473f1c4c4fSKonstantin Belousov 		 * If this is a system, protected or killed process, skip it.
19485663e6deSDavid Greenman 		 */
194971943c3dSKonstantin Belousov 		if (p->p_state != PRS_NORMAL || (p->p_flag & (P_INEXEC |
195071943c3dSKonstantin Belousov 		    P_PROTECTED | P_SYSTEM | P_WEXIT)) != 0 ||
195171943c3dSKonstantin Belousov 		    p->p_pid == 1 || P_KILLED(p) ||
195271943c3dSKonstantin Belousov 		    (p->p_pid < 48 && swap_pager_avail != 0)) {
19538606d880SJohn Baldwin 			PROC_UNLOCK(p);
19545663e6deSDavid Greenman 			continue;
19555663e6deSDavid Greenman 		}
19565663e6deSDavid Greenman 		/*
1957dcbcd518SBruce Evans 		 * If the process is in a non-running type state,
1958e602ba25SJulian Elischer 		 * don't touch it.  Check all the threads individually.
19595663e6deSDavid Greenman 		 */
19603e78e983SAlan Cox 		breakout = false;
1961e602ba25SJulian Elischer 		FOREACH_THREAD_IN_PROC(p, td) {
1962982d11f8SJeff Roberson 			thread_lock(td);
196371fad9fdSJulian Elischer 			if (!TD_ON_RUNQ(td) &&
196471fad9fdSJulian Elischer 			    !TD_IS_RUNNING(td) &&
1965f497cda2SEdward Tomasz Napierala 			    !TD_IS_SLEEPING(td) &&
1966b98acc0aSKonstantin Belousov 			    !TD_IS_SUSPENDED(td) &&
1967b98acc0aSKonstantin Belousov 			    !TD_IS_SWAPPED(td)) {
1968982d11f8SJeff Roberson 				thread_unlock(td);
19693e78e983SAlan Cox 				breakout = true;
1970e602ba25SJulian Elischer 				break;
1971e602ba25SJulian Elischer 			}
1972982d11f8SJeff Roberson 			thread_unlock(td);
1973e602ba25SJulian Elischer 		}
1974e602ba25SJulian Elischer 		if (breakout) {
19751c58e4e5SJohn Baldwin 			PROC_UNLOCK(p);
19765663e6deSDavid Greenman 			continue;
19775663e6deSDavid Greenman 		}
19785663e6deSDavid Greenman 		/*
19795663e6deSDavid Greenman 		 * get the process size
19805663e6deSDavid Greenman 		 */
19816bed074cSKonstantin Belousov 		vm = vmspace_acquire_ref(p);
19826bed074cSKonstantin Belousov 		if (vm == NULL) {
19836bed074cSKonstantin Belousov 			PROC_UNLOCK(p);
19846bed074cSKonstantin Belousov 			continue;
19856bed074cSKonstantin Belousov 		}
198695e2409aSKonstantin Belousov 		_PHOLD_LITE(p);
198772d97679SDavid Schultz 		PROC_UNLOCK(p);
198895e2409aSKonstantin Belousov 		sx_sunlock(&allproc_lock);
198995e2409aSKonstantin Belousov 		if (!vm_map_trylock_read(&vm->vm_map)) {
199071943c3dSKonstantin Belousov 			vmspace_free(vm);
199195e2409aSKonstantin Belousov 			sx_slock(&allproc_lock);
199295e2409aSKonstantin Belousov 			PRELE(p);
199372d97679SDavid Schultz 			continue;
199472d97679SDavid Schultz 		}
19957981aa24SKonstantin Belousov 		size = vmspace_swap_count(vm);
1996245139c6SKonstantin Belousov 		if (shortage == VM_OOM_MEM || shortage == VM_OOM_MEM_PF)
19973949873fSKonstantin Belousov 			size += vm_pageout_oom_pagecount(vm);
19983949873fSKonstantin Belousov 		vm_map_unlock_read(&vm->vm_map);
19996bed074cSKonstantin Belousov 		vmspace_free(vm);
200095e2409aSKonstantin Belousov 		sx_slock(&allproc_lock);
20013949873fSKonstantin Belousov 
20025663e6deSDavid Greenman 		/*
20033949873fSKonstantin Belousov 		 * If this process is bigger than the biggest one,
20045663e6deSDavid Greenman 		 * remember it.
20055663e6deSDavid Greenman 		 */
20065663e6deSDavid Greenman 		if (size > bigsize) {
20071c58e4e5SJohn Baldwin 			if (bigproc != NULL)
200871943c3dSKonstantin Belousov 				PRELE(bigproc);
20095663e6deSDavid Greenman 			bigproc = p;
20105663e6deSDavid Greenman 			bigsize = size;
201171943c3dSKonstantin Belousov 		} else {
201271943c3dSKonstantin Belousov 			PRELE(p);
201371943c3dSKonstantin Belousov 		}
20145663e6deSDavid Greenman 	}
20151005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
20165663e6deSDavid Greenman 	if (bigproc != NULL) {
20173c200db9SJonathan T. Looney 		if (vm_panic_on_oom != 0 && --vm_panic_on_oom == 0)
20188311a2b8SWill Andrews 			panic("out of swap space");
201971943c3dSKonstantin Belousov 		PROC_LOCK(bigproc);
2020729b1e51SDavid Greenman 		killproc(bigproc, "out of swap space");
2021fa885116SJulian Elischer 		sched_nice(bigproc, PRIO_MIN);
202271943c3dSKonstantin Belousov 		_PRELE(bigproc);
20231c58e4e5SJohn Baldwin 		PROC_UNLOCK(bigproc);
20245663e6deSDavid Greenman 	}
20255663e6deSDavid Greenman }
202626f9a767SRodney W. Grimes 
20278fc25508SMark Johnston /*
20288fc25508SMark Johnston  * Signal a free page shortage to subsystems that have registered an event
20298fc25508SMark Johnston  * handler.  Reclaim memory from UMA in the event of a severe shortage.
20308fc25508SMark Johnston  * Return true if the free page count should be re-evaluated.
20318fc25508SMark Johnston  */
2032b50a4ea6SMark Johnston static bool
2033b50a4ea6SMark Johnston vm_pageout_lowmem(void)
203449a3710cSMark Johnston {
2035b50a4ea6SMark Johnston 	static int lowmem_ticks = 0;
2036b50a4ea6SMark Johnston 	int last;
20378fc25508SMark Johnston 	bool ret;
20388fc25508SMark Johnston 
20398fc25508SMark Johnston 	ret = false;
204049a3710cSMark Johnston 
2041b50a4ea6SMark Johnston 	last = atomic_load_int(&lowmem_ticks);
2042b50a4ea6SMark Johnston 	while ((u_int)(ticks - last) / hz >= lowmem_period) {
2043b50a4ea6SMark Johnston 		if (atomic_fcmpset_int(&lowmem_ticks, &last, ticks) == 0)
2044b50a4ea6SMark Johnston 			continue;
2045b50a4ea6SMark Johnston 
204649a3710cSMark Johnston 		/*
204749a3710cSMark Johnston 		 * Decrease registered cache sizes.
204849a3710cSMark Johnston 		 */
204949a3710cSMark Johnston 		SDT_PROBE0(vm, , , vm__lowmem_scan);
205049a3710cSMark Johnston 		EVENTHANDLER_INVOKE(vm_lowmem, VM_LOW_PAGES);
205149a3710cSMark Johnston 
205249a3710cSMark Johnston 		/*
205349a3710cSMark Johnston 		 * We do this explicitly after the caches have been
20548fc25508SMark Johnston 		 * drained above.
205549a3710cSMark Johnston 		 */
20568fc25508SMark Johnston 		uma_reclaim(UMA_RECLAIM_TRIM);
20578fc25508SMark Johnston 		ret = true;
2058ace409ceSAlexander Motin 		break;
205949a3710cSMark Johnston 	}
20608fc25508SMark Johnston 
20618fc25508SMark Johnston 	/*
20628fc25508SMark Johnston 	 * Kick off an asynchronous reclaim of cached memory if one of the
20638fc25508SMark Johnston 	 * page daemons is failing to keep up with demand.  Use the "severe"
20648fc25508SMark Johnston 	 * threshold instead of "min" to ensure that we do not blow away the
20658fc25508SMark Johnston 	 * caches if a subset of the NUMA domains are depleted by kernel memory
20668fc25508SMark Johnston 	 * allocations; the domainset iterators automatically skip domains
20678fc25508SMark Johnston 	 * below the "min" threshold on the first pass.
20688fc25508SMark Johnston 	 *
20698fc25508SMark Johnston 	 * UMA reclaim worker has its own rate-limiting mechanism, so don't
20708fc25508SMark Johnston 	 * worry about kicking it too often.
20718fc25508SMark Johnston 	 */
20728fc25508SMark Johnston 	if (vm_page_count_severe())
20738fc25508SMark Johnston 		uma_reclaim_wakeup();
20748fc25508SMark Johnston 
20758fc25508SMark Johnston 	return (ret);
207649a3710cSMark Johnston }
207749a3710cSMark Johnston 
207849a3710cSMark Johnston static void
2079449c2e92SKonstantin Belousov vm_pageout_worker(void *arg)
2080449c2e92SKonstantin Belousov {
2081e2068d0bSJeff Roberson 	struct vm_domain *vmd;
2082b50a4ea6SMark Johnston 	u_int ofree;
208349a3710cSMark Johnston 	int addl_shortage, domain, shortage;
2084e57dd910SAlan Cox 	bool target_met;
2085449c2e92SKonstantin Belousov 
2086e2068d0bSJeff Roberson 	domain = (uintptr_t)arg;
2087e2068d0bSJeff Roberson 	vmd = VM_DOMAIN(domain);
20885f8cd1c0SJeff Roberson 	shortage = 0;
2089e57dd910SAlan Cox 	target_met = true;
2090449c2e92SKonstantin Belousov 
2091449c2e92SKonstantin Belousov 	/*
2092949c9186SKonstantin Belousov 	 * XXXKIB It could be useful to bind pageout daemon threads to
2093949c9186SKonstantin Belousov 	 * the cores belonging to the domain, from which vm_page_array
2094949c9186SKonstantin Belousov 	 * is allocated.
2095449c2e92SKonstantin Belousov 	 */
2096449c2e92SKonstantin Belousov 
2097e2068d0bSJeff Roberson 	KASSERT(vmd->vmd_segs != 0, ("domain without segments"));
2098e2068d0bSJeff Roberson 	vmd->vmd_last_active_scan = ticks;
2099449c2e92SKonstantin Belousov 
2100449c2e92SKonstantin Belousov 	/*
2101449c2e92SKonstantin Belousov 	 * The pageout daemon worker is never done, so loop forever.
2102449c2e92SKonstantin Belousov 	 */
2103449c2e92SKonstantin Belousov 	while (TRUE) {
210430fbfddaSJeff Roberson 		vm_domain_pageout_lock(vmd);
210549a3710cSMark Johnston 
210630fbfddaSJeff Roberson 		/*
210730fbfddaSJeff Roberson 		 * We need to clear wanted before we check the limits.  This
210830fbfddaSJeff Roberson 		 * prevents races with wakers who will check wanted after they
210930fbfddaSJeff Roberson 		 * reach the limit.
211030fbfddaSJeff Roberson 		 */
211130fbfddaSJeff Roberson 		atomic_store_int(&vmd->vmd_pageout_wanted, 0);
211256ce0690SAlan Cox 
211356ce0690SAlan Cox 		/*
21145f8cd1c0SJeff Roberson 		 * Might the page daemon need to run again?
2115449c2e92SKonstantin Belousov 		 */
21165f8cd1c0SJeff Roberson 		if (vm_paging_needed(vmd, vmd->vmd_free_count)) {
211756ce0690SAlan Cox 			/*
211849a3710cSMark Johnston 			 * Yes.  If the scan failed to produce enough free
211949a3710cSMark Johnston 			 * pages, sleep uninterruptibly for some time in the
212049a3710cSMark Johnston 			 * hope that the laundry thread will clean some pages.
212156ce0690SAlan Cox 			 */
212230fbfddaSJeff Roberson 			vm_domain_pageout_unlock(vmd);
212349a3710cSMark Johnston 			if (!target_met)
21246eebec83SMark Johnston 				pause("pwait", hz / VM_INACT_SCAN_RATE);
2125449c2e92SKonstantin Belousov 		} else {
2126449c2e92SKonstantin Belousov 			/*
21275f8cd1c0SJeff Roberson 			 * No, sleep until the next wakeup or until pages
21285f8cd1c0SJeff Roberson 			 * need to have their reference stats updated.
2129449c2e92SKonstantin Belousov 			 */
21302c0f13aaSKonstantin Belousov 			if (mtx_sleep(&vmd->vmd_pageout_wanted,
213130fbfddaSJeff Roberson 			    vm_domain_pageout_lockptr(vmd), PDROP | PVM,
21325f8cd1c0SJeff Roberson 			    "psleep", hz / VM_INACT_SCAN_RATE) == 0)
213383c9dea1SGleb Smirnoff 				VM_CNT_INC(v_pdwakeups);
213456ce0690SAlan Cox 		}
2135be37ee79SMark Johnston 
213630fbfddaSJeff Roberson 		/* Prevent spurious wakeups by ensuring that wanted is set. */
213730fbfddaSJeff Roberson 		atomic_store_int(&vmd->vmd_pageout_wanted, 1);
213830fbfddaSJeff Roberson 
213930fbfddaSJeff Roberson 		/*
214030fbfddaSJeff Roberson 		 * Use the controller to calculate how many pages to free in
2141b50a4ea6SMark Johnston 		 * this interval, and scan the inactive queue.  If the lowmem
2142b50a4ea6SMark Johnston 		 * handlers appear to have freed up some pages, subtract the
2143b50a4ea6SMark Johnston 		 * difference from the inactive queue scan target.
214430fbfddaSJeff Roberson 		 */
21455f8cd1c0SJeff Roberson 		shortage = pidctrl_daemon(&vmd->vmd_pid, vmd->vmd_free_count);
214649a3710cSMark Johnston 		if (shortage > 0) {
2147b50a4ea6SMark Johnston 			ofree = vmd->vmd_free_count;
2148b50a4ea6SMark Johnston 			if (vm_pageout_lowmem() && vmd->vmd_free_count > ofree)
2149b50a4ea6SMark Johnston 				shortage -= min(vmd->vmd_free_count - ofree,
2150b50a4ea6SMark Johnston 				    (u_int)shortage);
21510292c54bSConrad Meyer 			target_met = vm_pageout_inactive(vmd, shortage,
2152be37ee79SMark Johnston 			    &addl_shortage);
215349a3710cSMark Johnston 		} else
215449a3710cSMark Johnston 			addl_shortage = 0;
215556ce0690SAlan Cox 
2156be37ee79SMark Johnston 		/*
2157be37ee79SMark Johnston 		 * Scan the active queue.  A positive value for shortage
2158be37ee79SMark Johnston 		 * indicates that we must aggressively deactivate pages to avoid
2159be37ee79SMark Johnston 		 * a shortfall.
2160be37ee79SMark Johnston 		 */
21617bb4634eSMark Johnston 		shortage = vm_pageout_active_target(vmd) + addl_shortage;
2162be37ee79SMark Johnston 		vm_pageout_scan_active(vmd, shortage);
2163449c2e92SKonstantin Belousov 	}
2164449c2e92SKonstantin Belousov }
2165449c2e92SKonstantin Belousov 
2166df8bae1dSRodney W. Grimes /*
21670292c54bSConrad Meyer  * vm_pageout_helper runs additional pageout daemons in times of high paging
21680292c54bSConrad Meyer  * activity.
21690292c54bSConrad Meyer  */
21700292c54bSConrad Meyer static void
21710292c54bSConrad Meyer vm_pageout_helper(void *arg)
21720292c54bSConrad Meyer {
21730292c54bSConrad Meyer 	struct vm_domain *vmd;
21740292c54bSConrad Meyer 	int domain;
21750292c54bSConrad Meyer 
21760292c54bSConrad Meyer 	domain = (uintptr_t)arg;
21770292c54bSConrad Meyer 	vmd = VM_DOMAIN(domain);
21780292c54bSConrad Meyer 
21790292c54bSConrad Meyer 	vm_domain_pageout_lock(vmd);
21800292c54bSConrad Meyer 	for (;;) {
21810292c54bSConrad Meyer 		msleep(&vmd->vmd_inactive_shortage,
21820292c54bSConrad Meyer 		    vm_domain_pageout_lockptr(vmd), PVM, "psleep", 0);
21830292c54bSConrad Meyer 		blockcount_release(&vmd->vmd_inactive_starting, 1);
21840292c54bSConrad Meyer 
21850292c54bSConrad Meyer 		vm_domain_pageout_unlock(vmd);
21860292c54bSConrad Meyer 		vm_pageout_scan_inactive(vmd, vmd->vmd_inactive_shortage);
21870292c54bSConrad Meyer 		vm_domain_pageout_lock(vmd);
21880292c54bSConrad Meyer 
21890292c54bSConrad Meyer 		/*
21900292c54bSConrad Meyer 		 * Release the running count while the pageout lock is held to
21910292c54bSConrad Meyer 		 * prevent wakeup races.
21920292c54bSConrad Meyer 		 */
21930292c54bSConrad Meyer 		blockcount_release(&vmd->vmd_inactive_running, 1);
21940292c54bSConrad Meyer 	}
21950292c54bSConrad Meyer }
21960292c54bSConrad Meyer 
21970292c54bSConrad Meyer static int
219874f5530dSConrad Meyer get_pageout_threads_per_domain(const struct vm_domain *vmd)
21990292c54bSConrad Meyer {
220074f5530dSConrad Meyer 	unsigned total_pageout_threads, eligible_cpus, domain_cpus;
22010292c54bSConrad Meyer 
220274f5530dSConrad Meyer 	if (VM_DOMAIN_EMPTY(vmd->vmd_domain))
220374f5530dSConrad Meyer 		return (0);
22040292c54bSConrad Meyer 
22050292c54bSConrad Meyer 	/*
22060292c54bSConrad Meyer 	 * Semi-arbitrarily constrain pagedaemon threads to less than half the
220774f5530dSConrad Meyer 	 * total number of CPUs in the system as an upper limit.
22080292c54bSConrad Meyer 	 */
220974f5530dSConrad Meyer 	if (pageout_cpus_per_thread < 2)
221074f5530dSConrad Meyer 		pageout_cpus_per_thread = 2;
221174f5530dSConrad Meyer 	else if (pageout_cpus_per_thread > mp_ncpus)
221274f5530dSConrad Meyer 		pageout_cpus_per_thread = mp_ncpus;
22130292c54bSConrad Meyer 
221474f5530dSConrad Meyer 	total_pageout_threads = howmany(mp_ncpus, pageout_cpus_per_thread);
221574f5530dSConrad Meyer 	domain_cpus = CPU_COUNT(&cpuset_domain[vmd->vmd_domain]);
221674f5530dSConrad Meyer 
221774f5530dSConrad Meyer 	/* Pagedaemons are not run in empty domains. */
221874f5530dSConrad Meyer 	eligible_cpus = mp_ncpus;
221974f5530dSConrad Meyer 	for (unsigned i = 0; i < vm_ndomains; i++)
222074f5530dSConrad Meyer 		if (VM_DOMAIN_EMPTY(i))
222174f5530dSConrad Meyer 			eligible_cpus -= CPU_COUNT(&cpuset_domain[i]);
222274f5530dSConrad Meyer 
222374f5530dSConrad Meyer 	/*
222474f5530dSConrad Meyer 	 * Assign a portion of the total pageout threads to this domain
222574f5530dSConrad Meyer 	 * corresponding to the fraction of pagedaemon-eligible CPUs in the
222674f5530dSConrad Meyer 	 * domain.  In asymmetric NUMA systems, domains with more CPUs may be
222774f5530dSConrad Meyer 	 * allocated more threads than domains with fewer CPUs.
222874f5530dSConrad Meyer 	 */
222974f5530dSConrad Meyer 	return (howmany(total_pageout_threads * domain_cpus, eligible_cpus));
22300292c54bSConrad Meyer }
22310292c54bSConrad Meyer 
22320292c54bSConrad Meyer /*
22339c770a27SMark Johnston  * Initialize basic pageout daemon settings.  See the comment above the
22349c770a27SMark Johnston  * definition of vm_domain for some explanation of how these thresholds are
22359c770a27SMark Johnston  * used.
2236df8bae1dSRodney W. Grimes  */
22372b14f991SJulian Elischer static void
2238e2068d0bSJeff Roberson vm_pageout_init_domain(int domain)
2239df8bae1dSRodney W. Grimes {
2240e2068d0bSJeff Roberson 	struct vm_domain *vmd;
22415f8cd1c0SJeff Roberson 	struct sysctl_oid *oid;
2242e2068d0bSJeff Roberson 
2243e2068d0bSJeff Roberson 	vmd = VM_DOMAIN(domain);
2244e2068d0bSJeff Roberson 	vmd->vmd_interrupt_free_min = 2;
2245f6b04d2bSDavid Greenman 
224645ae1d91SAlan Cox 	/*
224745ae1d91SAlan Cox 	 * v_free_reserved needs to include enough for the largest
224845ae1d91SAlan Cox 	 * swap pager structures plus enough for any pv_entry structs
224945ae1d91SAlan Cox 	 * when paging.
225045ae1d91SAlan Cox 	 */
22510cab71bcSDoug Moore 	vmd->vmd_pageout_free_min = 2 * MAXBSIZE / PAGE_SIZE +
2252e2068d0bSJeff Roberson 	    vmd->vmd_interrupt_free_min;
2253e2068d0bSJeff Roberson 	vmd->vmd_free_reserved = vm_pageout_page_count +
22549c770a27SMark Johnston 	    vmd->vmd_pageout_free_min + vmd->vmd_page_count / 768;
22559c770a27SMark Johnston 	vmd->vmd_free_min = vmd->vmd_page_count / 200;
2256e2068d0bSJeff Roberson 	vmd->vmd_free_severe = vmd->vmd_free_min / 2;
2257e2068d0bSJeff Roberson 	vmd->vmd_free_target = 4 * vmd->vmd_free_min + vmd->vmd_free_reserved;
2258e2068d0bSJeff Roberson 	vmd->vmd_free_min += vmd->vmd_free_reserved;
2259e2068d0bSJeff Roberson 	vmd->vmd_free_severe += vmd->vmd_free_reserved;
2260e2068d0bSJeff Roberson 	vmd->vmd_inactive_target = (3 * vmd->vmd_free_target) / 2;
2261e2068d0bSJeff Roberson 	if (vmd->vmd_inactive_target > vmd->vmd_free_count / 3)
2262e2068d0bSJeff Roberson 		vmd->vmd_inactive_target = vmd->vmd_free_count / 3;
2263df8bae1dSRodney W. Grimes 
2264d9e23210SJeff Roberson 	/*
22655f8cd1c0SJeff Roberson 	 * Set the default wakeup threshold to be 10% below the paging
22665f8cd1c0SJeff Roberson 	 * target.  This keeps the steady state out of shortfall.
2267d9e23210SJeff Roberson 	 */
22685f8cd1c0SJeff Roberson 	vmd->vmd_pageout_wakeup_thresh = (vmd->vmd_free_target / 10) * 9;
2269e2068d0bSJeff Roberson 
2270e2068d0bSJeff Roberson 	/*
2271e2068d0bSJeff Roberson 	 * Target amount of memory to move out of the laundry queue during a
2272e2068d0bSJeff Roberson 	 * background laundering.  This is proportional to the amount of system
2273e2068d0bSJeff Roberson 	 * memory.
2274e2068d0bSJeff Roberson 	 */
2275e2068d0bSJeff Roberson 	vmd->vmd_background_launder_target = (vmd->vmd_free_target -
2276e2068d0bSJeff Roberson 	    vmd->vmd_free_min) / 10;
22775f8cd1c0SJeff Roberson 
22785f8cd1c0SJeff Roberson 	/* Initialize the pageout daemon pid controller. */
22795f8cd1c0SJeff Roberson 	pidctrl_init(&vmd->vmd_pid, hz / VM_INACT_SCAN_RATE,
22805f8cd1c0SJeff Roberson 	    vmd->vmd_free_target, PIDCTRL_BOUND,
22815f8cd1c0SJeff Roberson 	    PIDCTRL_KPD, PIDCTRL_KID, PIDCTRL_KDD);
22825f8cd1c0SJeff Roberson 	oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(vmd->vmd_oid), OID_AUTO,
22837029da5cSPawel Biernacki 	    "pidctrl", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
22845f8cd1c0SJeff Roberson 	pidctrl_init_sysctl(&vmd->vmd_pid, SYSCTL_CHILDREN(oid));
22850292c54bSConrad Meyer 
228674f5530dSConrad Meyer 	vmd->vmd_inactive_threads = get_pageout_threads_per_domain(vmd);
2287e2068d0bSJeff Roberson }
2288e2068d0bSJeff Roberson 
2289e2068d0bSJeff Roberson static void
2290e2068d0bSJeff Roberson vm_pageout_init(void)
2291e2068d0bSJeff Roberson {
229297458520SMark Johnston 	u_long freecount;
2293e2068d0bSJeff Roberson 	int i;
2294e2068d0bSJeff Roberson 
2295e2068d0bSJeff Roberson 	/*
2296e2068d0bSJeff Roberson 	 * Initialize some paging parameters.
2297e2068d0bSJeff Roberson 	 */
2298e2068d0bSJeff Roberson 	if (vm_cnt.v_page_count < 2000)
2299e2068d0bSJeff Roberson 		vm_pageout_page_count = 8;
2300e2068d0bSJeff Roberson 
2301e2068d0bSJeff Roberson 	freecount = 0;
2302e2068d0bSJeff Roberson 	for (i = 0; i < vm_ndomains; i++) {
2303e2068d0bSJeff Roberson 		struct vm_domain *vmd;
2304e2068d0bSJeff Roberson 
2305e2068d0bSJeff Roberson 		vm_pageout_init_domain(i);
2306e2068d0bSJeff Roberson 		vmd = VM_DOMAIN(i);
2307e2068d0bSJeff Roberson 		vm_cnt.v_free_reserved += vmd->vmd_free_reserved;
2308e2068d0bSJeff Roberson 		vm_cnt.v_free_target += vmd->vmd_free_target;
2309e2068d0bSJeff Roberson 		vm_cnt.v_free_min += vmd->vmd_free_min;
2310e2068d0bSJeff Roberson 		vm_cnt.v_inactive_target += vmd->vmd_inactive_target;
2311e2068d0bSJeff Roberson 		vm_cnt.v_pageout_free_min += vmd->vmd_pageout_free_min;
2312e2068d0bSJeff Roberson 		vm_cnt.v_interrupt_free_min += vmd->vmd_interrupt_free_min;
2313e2068d0bSJeff Roberson 		vm_cnt.v_free_severe += vmd->vmd_free_severe;
2314e2068d0bSJeff Roberson 		freecount += vmd->vmd_free_count;
2315e2068d0bSJeff Roberson 	}
2316d9e23210SJeff Roberson 
2317d9e23210SJeff Roberson 	/*
2318d9e23210SJeff Roberson 	 * Set interval in seconds for active scan.  We want to visit each
2319c9612b2dSJeff Roberson 	 * page at least once every ten minutes.  This is to prevent worst
2320c9612b2dSJeff Roberson 	 * case paging behaviors with stale active LRU.
2321d9e23210SJeff Roberson 	 */
2322d9e23210SJeff Roberson 	if (vm_pageout_update_period == 0)
2323c9612b2dSJeff Roberson 		vm_pageout_update_period = 600;
2324d9e23210SJeff Roberson 
232597458520SMark Johnston 	/*
232697458520SMark Johnston 	 * Set the maximum number of user-wired virtual pages.  Historically the
232797458520SMark Johnston 	 * main source of such pages was mlock(2) and mlockall(2).  Hypervisors
232897458520SMark Johnston 	 * may also request user-wired memory.
232997458520SMark Johnston 	 */
233054a3a114SMark Johnston 	if (vm_page_max_user_wired == 0)
233197458520SMark Johnston 		vm_page_max_user_wired = 4 * freecount / 5;
23324d19f4adSSteven Hartland }
23334d19f4adSSteven Hartland 
23344d19f4adSSteven Hartland /*
23354d19f4adSSteven Hartland  *     vm_pageout is the high level pageout daemon.
23364d19f4adSSteven Hartland  */
23374d19f4adSSteven Hartland static void
23384d19f4adSSteven Hartland vm_pageout(void)
23394d19f4adSSteven Hartland {
2340920239efSMark Johnston 	struct proc *p;
2341920239efSMark Johnston 	struct thread *td;
23420292c54bSConrad Meyer 	int error, first, i, j, pageout_threads;
2343920239efSMark Johnston 
2344920239efSMark Johnston 	p = curproc;
2345920239efSMark Johnston 	td = curthread;
2346df8bae1dSRodney W. Grimes 
2347245139c6SKonstantin Belousov 	mtx_init(&vm_oom_ratelim_mtx, "vmoomr", NULL, MTX_DEF);
234824a1cce3SDavid Greenman 	swap_pager_swap_init();
2349920239efSMark Johnston 	for (first = -1, i = 0; i < vm_ndomains; i++) {
235030c5525bSAndrew Gallatin 		if (VM_DOMAIN_EMPTY(i)) {
235130c5525bSAndrew Gallatin 			if (bootverbose)
235230c5525bSAndrew Gallatin 				printf("domain %d empty; skipping pageout\n",
235330c5525bSAndrew Gallatin 				    i);
235430c5525bSAndrew Gallatin 			continue;
235530c5525bSAndrew Gallatin 		}
2356920239efSMark Johnston 		if (first == -1)
2357920239efSMark Johnston 			first = i;
2358920239efSMark Johnston 		else {
2359920239efSMark Johnston 			error = kthread_add(vm_pageout_worker,
2360920239efSMark Johnston 			    (void *)(uintptr_t)i, p, NULL, 0, 0, "dom%d", i);
2361920239efSMark Johnston 			if (error != 0)
2362920239efSMark Johnston 				panic("starting pageout for domain %d: %d\n",
2363449c2e92SKonstantin Belousov 				    i, error);
2364dc2efb27SJohn Dyson 		}
236574f5530dSConrad Meyer 		pageout_threads = VM_DOMAIN(i)->vmd_inactive_threads;
23660292c54bSConrad Meyer 		for (j = 0; j < pageout_threads - 1; j++) {
23670292c54bSConrad Meyer 			error = kthread_add(vm_pageout_helper,
23680292c54bSConrad Meyer 			    (void *)(uintptr_t)i, p, NULL, 0, 0,
23690292c54bSConrad Meyer 			    "dom%d helper%d", i, j);
23700292c54bSConrad Meyer 			if (error != 0)
23710292c54bSConrad Meyer 				panic("starting pageout helper %d for domain "
23720292c54bSConrad Meyer 				    "%d: %d\n", j, i, error);
23730292c54bSConrad Meyer 		}
2374e2068d0bSJeff Roberson 		error = kthread_add(vm_pageout_laundry_worker,
2375920239efSMark Johnston 		    (void *)(uintptr_t)i, p, NULL, 0, 0, "laundry: dom%d", i);
2376e2068d0bSJeff Roberson 		if (error != 0)
2377920239efSMark Johnston 			panic("starting laundry for domain %d: %d", i, error);
2378f919ebdeSDavid Greenman 	}
2379920239efSMark Johnston 	error = kthread_add(uma_reclaim_worker, NULL, p, NULL, 0, 0, "uma");
238044ec2b63SKonstantin Belousov 	if (error != 0)
238144ec2b63SKonstantin Belousov 		panic("starting uma_reclaim helper, error %d\n", error);
2382920239efSMark Johnston 
2383920239efSMark Johnston 	snprintf(td->td_name, sizeof(td->td_name), "dom%d", first);
2384920239efSMark Johnston 	vm_pageout_worker((void *)(uintptr_t)first);
2385df8bae1dSRodney W. Grimes }
238626f9a767SRodney W. Grimes 
23876b4b77adSAlan Cox /*
2388280d15cdSMark Johnston  * Perform an advisory wakeup of the page daemon.
23896b4b77adSAlan Cox  */
2390e0c5a895SJohn Dyson void
2391e2068d0bSJeff Roberson pagedaemon_wakeup(int domain)
2392e0c5a895SJohn Dyson {
2393e2068d0bSJeff Roberson 	struct vm_domain *vmd;
2394a1c0a785SAlan Cox 
2395e2068d0bSJeff Roberson 	vmd = VM_DOMAIN(domain);
239630fbfddaSJeff Roberson 	vm_domain_pageout_assert_unlocked(vmd);
239730fbfddaSJeff Roberson 	if (curproc == pageproc)
239830fbfddaSJeff Roberson 		return;
2399280d15cdSMark Johnston 
240030fbfddaSJeff Roberson 	if (atomic_fetchadd_int(&vmd->vmd_pageout_wanted, 1) == 0) {
240130fbfddaSJeff Roberson 		vm_domain_pageout_lock(vmd);
240230fbfddaSJeff Roberson 		atomic_store_int(&vmd->vmd_pageout_wanted, 1);
2403e2068d0bSJeff Roberson 		wakeup(&vmd->vmd_pageout_wanted);
240430fbfddaSJeff Roberson 		vm_domain_pageout_unlock(vmd);
2405e0c5a895SJohn Dyson 	}
2406e0c5a895SJohn Dyson }
2407