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 * 44df8bae1dSRodney W. Grimes * 45df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1990 Carnegie-Mellon University. 46df8bae1dSRodney W. Grimes * All rights reserved. 47df8bae1dSRodney W. Grimes * 48df8bae1dSRodney W. Grimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young 49df8bae1dSRodney W. Grimes * 50df8bae1dSRodney W. Grimes * Permission to use, copy, modify and distribute this software and 51df8bae1dSRodney W. Grimes * its documentation is hereby granted, provided that both the copyright 52df8bae1dSRodney W. Grimes * notice and this permission notice appear in all copies of the 53df8bae1dSRodney W. Grimes * software, derivative works or modified versions, and any portions 54df8bae1dSRodney W. Grimes * thereof, and that both notices appear in supporting documentation. 55df8bae1dSRodney W. Grimes * 56df8bae1dSRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 57df8bae1dSRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 58df8bae1dSRodney W. Grimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 59df8bae1dSRodney W. Grimes * 60df8bae1dSRodney W. Grimes * Carnegie Mellon requests users of this software to return to 61df8bae1dSRodney W. Grimes * 62df8bae1dSRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 63df8bae1dSRodney W. Grimes * School of Computer Science 64df8bae1dSRodney W. Grimes * Carnegie Mellon University 65df8bae1dSRodney W. Grimes * Pittsburgh PA 15213-3890 66df8bae1dSRodney W. Grimes * 67df8bae1dSRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the 68df8bae1dSRodney W. Grimes * rights to redistribute these changes. 69df8bae1dSRodney W. Grimes */ 70df8bae1dSRodney W. Grimes 71df8bae1dSRodney W. Grimes /* 72df8bae1dSRodney W. Grimes * The proverbial page-out daemon. 73df8bae1dSRodney W. Grimes */ 74df8bae1dSRodney W. Grimes 75874651b1SDavid E. O'Brien #include <sys/cdefs.h> 76faa5f8d8SAndrzej Bialecki #include "opt_vm.h" 777672ca05SMark Johnston 78df8bae1dSRodney W. Grimes #include <sys/param.h> 7926f9a767SRodney W. Grimes #include <sys/systm.h> 80b5e8ce9fSBruce Evans #include <sys/kernel.h> 810292c54bSConrad Meyer #include <sys/blockcount.h> 82855a310fSJeff Roberson #include <sys/eventhandler.h> 83fb919e4dSMark Murray #include <sys/lock.h> 84fb919e4dSMark Murray #include <sys/mutex.h> 8526f9a767SRodney W. Grimes #include <sys/proc.h> 869c8b8baaSPeter Wemm #include <sys/kthread.h> 870384fff8SJason Evans #include <sys/ktr.h> 8897824da3SAlan Cox #include <sys/mount.h> 89099e7e95SEdward Tomasz Napierala #include <sys/racct.h> 9026f9a767SRodney W. Grimes #include <sys/resourcevar.h> 91b43179fbSJeff Roberson #include <sys/sched.h> 9214a0d74eSSteven Hartland #include <sys/sdt.h> 93d2fc5315SPoul-Henning Kamp #include <sys/signalvar.h> 94449c2e92SKonstantin Belousov #include <sys/smp.h> 95a6bf3a9eSRyan Stone #include <sys/time.h> 96f6b04d2bSDavid Greenman #include <sys/vnode.h> 97efeaf95aSDavid Greenman #include <sys/vmmeter.h> 9889f6b863SAttilio Rao #include <sys/rwlock.h> 991005a129SJohn Baldwin #include <sys/sx.h> 10038efa82bSJohn Dyson #include <sys/sysctl.h> 101df8bae1dSRodney W. Grimes 102df8bae1dSRodney W. Grimes #include <vm/vm.h> 103efeaf95aSDavid Greenman #include <vm/vm_param.h> 104efeaf95aSDavid Greenman #include <vm/vm_object.h> 105df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 106efeaf95aSDavid Greenman #include <vm/vm_map.h> 107df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h> 10824a1cce3SDavid Greenman #include <vm/vm_pager.h> 109449c2e92SKonstantin Belousov #include <vm/vm_phys.h> 110e2068d0bSJeff Roberson #include <vm/vm_pagequeue.h> 11105f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h> 112efeaf95aSDavid Greenman #include <vm/vm_extern.h> 113670d17b5SJeff Roberson #include <vm/uma.h> 114df8bae1dSRodney W. Grimes 1152b14f991SJulian Elischer /* 1162b14f991SJulian Elischer * System initialization 1172b14f991SJulian Elischer */ 1182b14f991SJulian Elischer 1192b14f991SJulian Elischer /* the kernel process "vm_pageout"*/ 12011caded3SAlfred Perlstein static void vm_pageout(void); 1214d19f4adSSteven Hartland static void vm_pageout_init(void); 122ebcddc72SAlan Cox static int vm_pageout_clean(vm_page_t m, int *numpagedout); 12334d8b7eaSJeff Roberson static int vm_pageout_cluster(vm_page_t m); 12476386c7eSKonstantin Belousov static void vm_pageout_mightbe_oom(struct vm_domain *vmd, int page_shortage, 12576386c7eSKonstantin Belousov int starting_page_shortage); 12645ae1d91SAlan Cox 1274d19f4adSSteven Hartland SYSINIT(pagedaemon_init, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, vm_pageout_init, 1284d19f4adSSteven Hartland NULL); 1294d19f4adSSteven Hartland 1302b14f991SJulian Elischer struct proc *pageproc; 1312b14f991SJulian Elischer 1322b14f991SJulian Elischer static struct kproc_desc page_kp = { 1332b14f991SJulian Elischer "pagedaemon", 1342b14f991SJulian Elischer vm_pageout, 1352b14f991SJulian Elischer &pageproc 1362b14f991SJulian Elischer }; 1374d19f4adSSteven Hartland SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_SECOND, kproc_start, 138237fdd78SRobert Watson &page_kp); 1392b14f991SJulian Elischer 14014a0d74eSSteven Hartland SDT_PROVIDER_DEFINE(vm); 14114a0d74eSSteven Hartland SDT_PROBE_DEFINE(vm, , , vm__lowmem_scan); 14214a0d74eSSteven Hartland 143ebcddc72SAlan Cox /* Pagedaemon activity rates, in subdivisions of one second. */ 144ebcddc72SAlan Cox #define VM_LAUNDER_RATE 10 1455f8cd1c0SJeff Roberson #define VM_INACT_SCAN_RATE 10 1462b14f991SJulian Elischer 147b1fd102eSMark Johnston static int swapdev_enabled; 148c4a25e07SMark Johnston int vm_pageout_page_count = 32; 14970111b90SJohn Dyson 1508311a2b8SWill Andrews static int vm_panic_on_oom = 0; 1518311a2b8SWill Andrews SYSCTL_INT(_vm, OID_AUTO, panic_on_oom, 1528311a2b8SWill Andrews CTLFLAG_RWTUN, &vm_panic_on_oom, 0, 153c4a25e07SMark Johnston "Panic on the given number of out-of-memory errors instead of " 154c4a25e07SMark Johnston "killing the largest process"); 1558311a2b8SWill Andrews 156c4a25e07SMark Johnston static int vm_pageout_update_period; 157d9e23210SJeff Roberson SYSCTL_INT(_vm, OID_AUTO, pageout_update_period, 158e0b2fc3aSMark Johnston CTLFLAG_RWTUN, &vm_pageout_update_period, 0, 159d9e23210SJeff Roberson "Maximum active LRU update period"); 16053636869SAndrey Zonov 16174f5530dSConrad Meyer static int pageout_cpus_per_thread = 16; 16274f5530dSConrad Meyer SYSCTL_INT(_vm, OID_AUTO, pageout_cpus_per_thread, CTLFLAG_RDTUN, 16374f5530dSConrad Meyer &pageout_cpus_per_thread, 0, 16474f5530dSConrad Meyer "Number of CPUs per pagedaemon worker thread"); 1650292c54bSConrad Meyer 166c4a25e07SMark Johnston static int lowmem_period = 10; 167e0b2fc3aSMark Johnston SYSCTL_INT(_vm, OID_AUTO, lowmem_period, CTLFLAG_RWTUN, &lowmem_period, 0, 168c9612b2dSJeff Roberson "Low memory callback period"); 169c9612b2dSJeff Roberson 170c4a25e07SMark Johnston static int disable_swap_pageouts; 171ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts, 172c4a25e07SMark Johnston CTLFLAG_RWTUN, &disable_swap_pageouts, 0, 173c4a25e07SMark Johnston "Disallow swapout of dirty pages"); 17412ac6a1dSJohn Dyson 17523b59018SMatthew Dillon static int pageout_lock_miss; 17623b59018SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss, 177c4a25e07SMark Johnston CTLFLAG_RD, &pageout_lock_miss, 0, 178c4a25e07SMark Johnston "vget() lock misses during pageout"); 17923b59018SMatthew Dillon 180c4a25e07SMark Johnston static int vm_pageout_oom_seq = 12; 18176386c7eSKonstantin Belousov SYSCTL_INT(_vm, OID_AUTO, pageout_oom_seq, 182e0b2fc3aSMark Johnston CTLFLAG_RWTUN, &vm_pageout_oom_seq, 0, 18376386c7eSKonstantin Belousov "back-to-back calls to oom detector to start OOM"); 18476386c7eSKonstantin Belousov 185ebcddc72SAlan Cox static int act_scan_laundry_weight = 3; 186e0b2fc3aSMark Johnston SYSCTL_INT(_vm, OID_AUTO, act_scan_laundry_weight, CTLFLAG_RWTUN, 187ebcddc72SAlan Cox &act_scan_laundry_weight, 0, 188ebcddc72SAlan Cox "weight given to clean vs. dirty pages in active queue scans"); 189ebcddc72SAlan Cox 190ebcddc72SAlan Cox static u_int vm_background_launder_rate = 4096; 191e0b2fc3aSMark Johnston SYSCTL_UINT(_vm, OID_AUTO, background_launder_rate, CTLFLAG_RWTUN, 192ebcddc72SAlan Cox &vm_background_launder_rate, 0, 193ebcddc72SAlan Cox "background laundering rate, in kilobytes per second"); 194ebcddc72SAlan Cox 195ebcddc72SAlan Cox static u_int vm_background_launder_max = 20 * 1024; 196e0b2fc3aSMark Johnston SYSCTL_UINT(_vm, OID_AUTO, background_launder_max, CTLFLAG_RWTUN, 197c4a25e07SMark Johnston &vm_background_launder_max, 0, 198c4a25e07SMark Johnston "background laundering cap, in kilobytes"); 199df8bae1dSRodney W. Grimes 20054a3a114SMark Johnston u_long vm_page_max_user_wired; 20154a3a114SMark Johnston SYSCTL_ULONG(_vm, OID_AUTO, max_user_wired, CTLFLAG_RW, 20254a3a114SMark Johnston &vm_page_max_user_wired, 0, 20354a3a114SMark Johnston "system-wide limit to user-wired page count"); 204df8bae1dSRodney W. Grimes 205ebcddc72SAlan Cox static u_int isqrt(u_int num); 206ebcddc72SAlan Cox static int vm_pageout_launder(struct vm_domain *vmd, int launder, 207ebcddc72SAlan Cox bool in_shortfall); 208ebcddc72SAlan Cox static void vm_pageout_laundry_worker(void *arg); 209cd41fc12SDavid Greenman 2105cd29d0fSMark Johnston struct scan_state { 2115cd29d0fSMark Johnston struct vm_batchqueue bq; 2128d220203SAlan Cox struct vm_pagequeue *pq; 2135cd29d0fSMark Johnston vm_page_t marker; 2145cd29d0fSMark Johnston int maxscan; 2155cd29d0fSMark Johnston int scanned; 2165cd29d0fSMark Johnston }; 2178dbca793STor Egge 2185cd29d0fSMark Johnston static void 2195cd29d0fSMark Johnston vm_pageout_init_scan(struct scan_state *ss, struct vm_pagequeue *pq, 2205cd29d0fSMark Johnston vm_page_t marker, vm_page_t after, int maxscan) 2215cd29d0fSMark Johnston { 2228dbca793STor Egge 2235cd29d0fSMark Johnston vm_pagequeue_assert_locked(pq); 2245cff1f4dSMark Johnston KASSERT((marker->a.flags & PGA_ENQUEUED) == 0, 2255cd29d0fSMark Johnston ("marker %p already enqueued", marker)); 2265cd29d0fSMark Johnston 2275cd29d0fSMark Johnston if (after == NULL) 2285cd29d0fSMark Johnston TAILQ_INSERT_HEAD(&pq->pq_pl, marker, plinks.q); 2295cd29d0fSMark Johnston else 2305cd29d0fSMark Johnston TAILQ_INSERT_AFTER(&pq->pq_pl, after, marker, plinks.q); 2315cd29d0fSMark Johnston vm_page_aflag_set(marker, PGA_ENQUEUED); 2325cd29d0fSMark Johnston 2335cd29d0fSMark Johnston vm_batchqueue_init(&ss->bq); 2345cd29d0fSMark Johnston ss->pq = pq; 2355cd29d0fSMark Johnston ss->marker = marker; 2365cd29d0fSMark Johnston ss->maxscan = maxscan; 2375cd29d0fSMark Johnston ss->scanned = 0; 2388d220203SAlan Cox vm_pagequeue_unlock(pq); 2395cd29d0fSMark Johnston } 2408dbca793STor Egge 2415cd29d0fSMark Johnston static void 2425cd29d0fSMark Johnston vm_pageout_end_scan(struct scan_state *ss) 2435cd29d0fSMark Johnston { 2445cd29d0fSMark Johnston struct vm_pagequeue *pq; 2455cd29d0fSMark Johnston 2465cd29d0fSMark Johnston pq = ss->pq; 2475cd29d0fSMark Johnston vm_pagequeue_assert_locked(pq); 2485cff1f4dSMark Johnston KASSERT((ss->marker->a.flags & PGA_ENQUEUED) != 0, 2495cd29d0fSMark Johnston ("marker %p not enqueued", ss->marker)); 2505cd29d0fSMark Johnston 2515cd29d0fSMark Johnston TAILQ_REMOVE(&pq->pq_pl, ss->marker, plinks.q); 2525cd29d0fSMark Johnston vm_page_aflag_clear(ss->marker, PGA_ENQUEUED); 253899fe184SMark Johnston pq->pq_pdpages += ss->scanned; 2548dbca793STor Egge } 2558dbca793STor Egge 2568dbca793STor Egge /* 2575cd29d0fSMark Johnston * Add a small number of queued pages to a batch queue for later processing 2585cd29d0fSMark Johnston * without the corresponding queue lock held. The caller must have enqueued a 2595cd29d0fSMark Johnston * marker page at the desired start point for the scan. Pages will be 2605cd29d0fSMark Johnston * physically dequeued if the caller so requests. Otherwise, the returned 2615cd29d0fSMark Johnston * batch may contain marker pages, and it is up to the caller to handle them. 2625cd29d0fSMark Johnston * 263efec381dSMark Johnston * When processing the batch queue, vm_pageout_defer() must be used to 264efec381dSMark Johnston * determine whether the page has been logically dequeued since the batch was 265efec381dSMark Johnston * collected. 2665cd29d0fSMark Johnston */ 2675cd29d0fSMark Johnston static __always_inline void 2685cd29d0fSMark Johnston vm_pageout_collect_batch(struct scan_state *ss, const bool dequeue) 2695cd29d0fSMark Johnston { 2708d220203SAlan Cox struct vm_pagequeue *pq; 271d70f0ab3SMark Johnston vm_page_t m, marker, n; 2728c616246SKonstantin Belousov 2735cd29d0fSMark Johnston marker = ss->marker; 2745cd29d0fSMark Johnston pq = ss->pq; 2758c616246SKonstantin Belousov 2765cff1f4dSMark Johnston KASSERT((marker->a.flags & PGA_ENQUEUED) != 0, 2775cd29d0fSMark Johnston ("marker %p not enqueued", ss->marker)); 2788c616246SKonstantin Belousov 2798d220203SAlan Cox vm_pagequeue_lock(pq); 2805cd29d0fSMark Johnston for (m = TAILQ_NEXT(marker, plinks.q); m != NULL && 2815cd29d0fSMark Johnston ss->scanned < ss->maxscan && ss->bq.bq_cnt < VM_BATCHQUEUE_SIZE; 282d70f0ab3SMark Johnston m = n, ss->scanned++) { 283d70f0ab3SMark Johnston n = TAILQ_NEXT(m, plinks.q); 2845cd29d0fSMark Johnston if ((m->flags & PG_MARKER) == 0) { 2855cff1f4dSMark Johnston KASSERT((m->a.flags & PGA_ENQUEUED) != 0, 2865cd29d0fSMark Johnston ("page %p not enqueued", m)); 2875cd29d0fSMark Johnston KASSERT((m->flags & PG_FICTITIOUS) == 0, 2885cd29d0fSMark Johnston ("Fictitious page %p cannot be in page queue", m)); 2895cd29d0fSMark Johnston KASSERT((m->oflags & VPO_UNMANAGED) == 0, 2905cd29d0fSMark Johnston ("Unmanaged page %p cannot be in page queue", m)); 2915cd29d0fSMark Johnston } else if (dequeue) 2925cd29d0fSMark Johnston continue; 2938c616246SKonstantin Belousov 2945cd29d0fSMark Johnston (void)vm_batchqueue_insert(&ss->bq, m); 2955cd29d0fSMark Johnston if (dequeue) { 2965cd29d0fSMark Johnston TAILQ_REMOVE(&pq->pq_pl, m, plinks.q); 2975cd29d0fSMark Johnston vm_page_aflag_clear(m, PGA_ENQUEUED); 2985cd29d0fSMark Johnston } 2995cd29d0fSMark Johnston } 3005cd29d0fSMark Johnston TAILQ_REMOVE(&pq->pq_pl, marker, plinks.q); 3015cd29d0fSMark Johnston if (__predict_true(m != NULL)) 3025cd29d0fSMark Johnston TAILQ_INSERT_BEFORE(m, marker, plinks.q); 3035cd29d0fSMark Johnston else 3045cd29d0fSMark Johnston TAILQ_INSERT_TAIL(&pq->pq_pl, marker, plinks.q); 3055cd29d0fSMark Johnston if (dequeue) 3065cd29d0fSMark Johnston vm_pagequeue_cnt_add(pq, -ss->bq.bq_cnt); 3075cd29d0fSMark Johnston vm_pagequeue_unlock(pq); 3085cd29d0fSMark Johnston } 3095cd29d0fSMark Johnston 310fee2a2faSMark Johnston /* 311fee2a2faSMark Johnston * Return the next page to be scanned, or NULL if the scan is complete. 312fee2a2faSMark Johnston */ 3135cd29d0fSMark Johnston static __always_inline vm_page_t 3145cd29d0fSMark Johnston vm_pageout_next(struct scan_state *ss, const bool dequeue) 3155cd29d0fSMark Johnston { 3165cd29d0fSMark Johnston 3175cd29d0fSMark Johnston if (ss->bq.bq_cnt == 0) 3185cd29d0fSMark Johnston vm_pageout_collect_batch(ss, dequeue); 3195cd29d0fSMark Johnston return (vm_batchqueue_pop(&ss->bq)); 3208c616246SKonstantin Belousov } 3218c616246SKonstantin Belousov 3228c616246SKonstantin Belousov /* 323b7f30bffSMark Johnston * Determine whether processing of a page should be deferred and ensure that any 324b7f30bffSMark Johnston * outstanding queue operations are processed. 325b7f30bffSMark Johnston */ 326b7f30bffSMark Johnston static __always_inline bool 327b7f30bffSMark Johnston vm_pageout_defer(vm_page_t m, const uint8_t queue, const bool enqueued) 328b7f30bffSMark Johnston { 329b7f30bffSMark Johnston vm_page_astate_t as; 330b7f30bffSMark Johnston 331b7f30bffSMark Johnston as = vm_page_astate_load(m); 332b7f30bffSMark Johnston if (__predict_false(as.queue != queue || 333b7f30bffSMark Johnston ((as.flags & PGA_ENQUEUED) != 0) != enqueued)) 334b7f30bffSMark Johnston return (true); 335b7f30bffSMark Johnston if ((as.flags & PGA_QUEUE_OP_MASK) != 0) { 336b7f30bffSMark Johnston vm_page_pqbatch_submit(m, queue); 337b7f30bffSMark Johnston return (true); 338b7f30bffSMark Johnston } 339b7f30bffSMark Johnston return (false); 340b7f30bffSMark Johnston } 341b7f30bffSMark Johnston 342b7f30bffSMark Johnston /* 343248fe642SAlan Cox * Scan for pages at adjacent offsets within the given page's object that are 344248fe642SAlan Cox * eligible for laundering, form a cluster of these pages and the given page, 345248fe642SAlan Cox * and launder that cluster. 34626f9a767SRodney W. Grimes */ 3473af76890SPoul-Henning Kamp static int 34834d8b7eaSJeff Roberson vm_pageout_cluster(vm_page_t m) 34924a1cce3SDavid Greenman { 35054d92145SMatthew Dillon vm_object_t object; 351248fe642SAlan Cox vm_page_t mc[2 * vm_pageout_page_count], p, pb, ps; 352248fe642SAlan Cox vm_pindex_t pindex; 353248fe642SAlan Cox int ib, is, page_base, pageout_count; 35426f9a767SRodney W. Grimes 35517f6a17bSAlan Cox object = m->object; 35689f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 357248fe642SAlan Cox pindex = m->pindex; 3580cddd8f0SMatthew Dillon 35963e97555SJeff Roberson vm_page_assert_xbusied(m); 3600d94caffSDavid Greenman 36191b4f427SAlan Cox mc[vm_pageout_page_count] = pb = ps = m; 36226f9a767SRodney W. Grimes pageout_count = 1; 363f35329acSJohn Dyson page_base = vm_pageout_page_count; 36490ecac61SMatthew Dillon ib = 1; 36590ecac61SMatthew Dillon is = 1; 36690ecac61SMatthew Dillon 36724a1cce3SDavid Greenman /* 368248fe642SAlan Cox * We can cluster only if the page is not clean, busy, or held, and 369ebcddc72SAlan Cox * the page is in the laundry queue. 37090ecac61SMatthew Dillon * 37190ecac61SMatthew Dillon * During heavy mmap/modification loads the pageout 37290ecac61SMatthew Dillon * daemon can really fragment the underlying file 373248fe642SAlan Cox * due to flushing pages out of order and not trying to 374248fe642SAlan Cox * align the clusters (which leaves sporadic out-of-order 37590ecac61SMatthew Dillon * holes). To solve this problem we do the reverse scan 37690ecac61SMatthew Dillon * first and attempt to align our cluster, then do a 37790ecac61SMatthew Dillon * forward scan if room remains. 37824a1cce3SDavid Greenman */ 37990ecac61SMatthew Dillon more: 380248fe642SAlan Cox while (ib != 0 && pageout_count < vm_pageout_page_count) { 38190ecac61SMatthew Dillon if (ib > pindex) { 38290ecac61SMatthew Dillon ib = 0; 38390ecac61SMatthew Dillon break; 384f6b04d2bSDavid Greenman } 38563e97555SJeff Roberson if ((p = vm_page_prev(pb)) == NULL || 38663e97555SJeff Roberson vm_page_tryxbusy(p) == 0) { 38790ecac61SMatthew Dillon ib = 0; 38890ecac61SMatthew Dillon break; 389f6b04d2bSDavid Greenman } 39063e97555SJeff Roberson if (vm_page_wired(p)) { 39163e97555SJeff Roberson ib = 0; 39263e97555SJeff Roberson vm_page_xunbusy(p); 39363e97555SJeff Roberson break; 39463e97555SJeff Roberson } 39524a1cce3SDavid Greenman vm_page_test_dirty(p); 3961b5c869dSMark Johnston if (p->dirty == 0) { 397eb5d3969SAlan Cox ib = 0; 39863e97555SJeff Roberson vm_page_xunbusy(p); 399eb5d3969SAlan Cox break; 400eb5d3969SAlan Cox } 401fee2a2faSMark Johnston if (!vm_page_in_laundry(p) || !vm_page_try_remove_write(p)) { 40263e97555SJeff Roberson vm_page_xunbusy(p); 40390ecac61SMatthew Dillon ib = 0; 40424a1cce3SDavid Greenman break; 405f6b04d2bSDavid Greenman } 40691b4f427SAlan Cox mc[--page_base] = pb = p; 40790ecac61SMatthew Dillon ++pageout_count; 40890ecac61SMatthew Dillon ++ib; 409248fe642SAlan Cox 41024a1cce3SDavid Greenman /* 411248fe642SAlan Cox * We are at an alignment boundary. Stop here, and switch 412248fe642SAlan Cox * directions. Do not clear ib. 41324a1cce3SDavid Greenman */ 41490ecac61SMatthew Dillon if ((pindex - (ib - 1)) % vm_pageout_page_count == 0) 41590ecac61SMatthew Dillon break; 41624a1cce3SDavid Greenman } 41790ecac61SMatthew Dillon while (pageout_count < vm_pageout_page_count && 41890ecac61SMatthew Dillon pindex + is < object->size) { 41963e97555SJeff Roberson if ((p = vm_page_next(ps)) == NULL || 42063e97555SJeff Roberson vm_page_tryxbusy(p) == 0) 42190ecac61SMatthew Dillon break; 42263e97555SJeff Roberson if (vm_page_wired(p)) { 42363e97555SJeff Roberson vm_page_xunbusy(p); 42463e97555SJeff Roberson break; 42563e97555SJeff Roberson } 42624a1cce3SDavid Greenman vm_page_test_dirty(p); 42763e97555SJeff Roberson if (p->dirty == 0) { 42863e97555SJeff Roberson vm_page_xunbusy(p); 429eb5d3969SAlan Cox break; 43063e97555SJeff Roberson } 431e8bcf696SMark Johnston if (!vm_page_in_laundry(p) || !vm_page_try_remove_write(p)) { 43263e97555SJeff Roberson vm_page_xunbusy(p); 43324a1cce3SDavid Greenman break; 434e8bcf696SMark Johnston } 43591b4f427SAlan Cox mc[page_base + pageout_count] = ps = p; 43690ecac61SMatthew Dillon ++pageout_count; 43790ecac61SMatthew Dillon ++is; 43824a1cce3SDavid Greenman } 43990ecac61SMatthew Dillon 44090ecac61SMatthew Dillon /* 44190ecac61SMatthew Dillon * If we exhausted our forward scan, continue with the reverse scan 442248fe642SAlan Cox * when possible, even past an alignment boundary. This catches 443248fe642SAlan Cox * boundary conditions. 44490ecac61SMatthew Dillon */ 445248fe642SAlan Cox if (ib != 0 && pageout_count < vm_pageout_page_count) 44690ecac61SMatthew Dillon goto more; 447f6b04d2bSDavid Greenman 44899e6e193SMark Johnston return (vm_pageout_flush(&mc[page_base], pageout_count, 44999e6e193SMark Johnston VM_PAGER_PUT_NOREUSE, 0, NULL, NULL)); 450aef922f5SJohn Dyson } 451aef922f5SJohn Dyson 4521c7c3c6aSMatthew Dillon /* 4531c7c3c6aSMatthew Dillon * vm_pageout_flush() - launder the given pages 4541c7c3c6aSMatthew Dillon * 4551c7c3c6aSMatthew Dillon * The given pages are laundered. Note that we setup for the start of 4561c7c3c6aSMatthew Dillon * I/O ( i.e. busy the page ), mark it read-only, and bump the object 4571c7c3c6aSMatthew Dillon * reference count all in here rather then in the parent. If we want 4581c7c3c6aSMatthew Dillon * the parent to do more sophisticated things we may have to change 4591c7c3c6aSMatthew Dillon * the ordering. 4601e8a675cSKonstantin Belousov * 4611e8a675cSKonstantin Belousov * Returned runlen is the count of pages between mreq and first 4621e8a675cSKonstantin Belousov * page after mreq with status VM_PAGER_AGAIN. 463126d6082SKonstantin Belousov * *eio is set to TRUE if pager returned VM_PAGER_ERROR or VM_PAGER_FAIL 464126d6082SKonstantin Belousov * for any page in runlen set. 4651c7c3c6aSMatthew Dillon */ 466aef922f5SJohn Dyson int 467126d6082SKonstantin Belousov vm_pageout_flush(vm_page_t *mc, int count, int flags, int mreq, int *prunlen, 468126d6082SKonstantin Belousov boolean_t *eio) 469aef922f5SJohn Dyson { 4702e3b314dSAlan Cox vm_object_t object = mc[0]->object; 471aef922f5SJohn Dyson int pageout_status[count]; 47295461b45SJohn Dyson int numpagedout = 0; 4731e8a675cSKonstantin Belousov int i, runlen; 474aef922f5SJohn Dyson 47589f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 4767bec141bSKip Macy 4771c7c3c6aSMatthew Dillon /* 47863e97555SJeff Roberson * Initiate I/O. Mark the pages shared busy and verify that they're 47963e97555SJeff Roberson * valid and read-only. 4801c7c3c6aSMatthew Dillon * 4811c7c3c6aSMatthew Dillon * We do not have to fixup the clean/dirty bits here... we can 4821c7c3c6aSMatthew Dillon * allow the pager to do it after the I/O completes. 48302fa91d3SMatthew Dillon * 48402fa91d3SMatthew Dillon * NOTE! mc[i]->dirty may be partial or fragmented due to an 48502fa91d3SMatthew Dillon * edge case with file fragments. 4861c7c3c6aSMatthew Dillon */ 4878f9110f6SJohn Dyson for (i = 0; i < count; i++) { 4880012f373SJeff Roberson KASSERT(vm_page_all_valid(mc[i]), 4897a935082SAlan Cox ("vm_pageout_flush: partially invalid page %p index %d/%d", 4907a935082SAlan Cox mc[i], i, count)); 4915cff1f4dSMark Johnston KASSERT((mc[i]->a.flags & PGA_WRITEABLE) == 0, 492aed9aaaaSMark Johnston ("vm_pageout_flush: writeable page %p", mc[i])); 49363e97555SJeff Roberson vm_page_busy_downgrade(mc[i]); 4942965a453SKip Macy } 495d474eaaaSDoug Rabson vm_object_pip_add(object, count); 496aef922f5SJohn Dyson 497d076fbeaSAlan Cox vm_pager_put_pages(object, mc, count, flags, pageout_status); 49826f9a767SRodney W. Grimes 4991e8a675cSKonstantin Belousov runlen = count - mreq; 500126d6082SKonstantin Belousov if (eio != NULL) 501126d6082SKonstantin Belousov *eio = FALSE; 502aef922f5SJohn Dyson for (i = 0; i < count; i++) { 503aef922f5SJohn Dyson vm_page_t mt = mc[i]; 50424a1cce3SDavid Greenman 5054cd45723SAlan Cox KASSERT(pageout_status[i] == VM_PAGER_PEND || 5066031c68dSAlan Cox !pmap_page_is_write_mapped(mt), 5079ea8d1a6SAlan Cox ("vm_pageout_flush: page %p is not write protected", mt)); 50826f9a767SRodney W. Grimes switch (pageout_status[i]) { 50926f9a767SRodney W. Grimes case VM_PAGER_OK: 5109f5632e6SMark Johnston /* 5119f5632e6SMark Johnston * The page may have moved since laundering started, in 5129f5632e6SMark Johnston * which case it should be left alone. 5139f5632e6SMark Johnston */ 514ebcddc72SAlan Cox if (vm_page_in_laundry(mt)) 515ebcddc72SAlan Cox vm_page_deactivate_noreuse(mt); 516ebcddc72SAlan Cox /* FALLTHROUGH */ 51726f9a767SRodney W. Grimes case VM_PAGER_PEND: 51895461b45SJohn Dyson numpagedout++; 51926f9a767SRodney W. Grimes break; 52026f9a767SRodney W. Grimes case VM_PAGER_BAD: 52126f9a767SRodney W. Grimes /* 522ebcddc72SAlan Cox * The page is outside the object's range. We pretend 523ebcddc72SAlan Cox * that the page out worked and clean the page, so the 524ebcddc72SAlan Cox * changes will be lost if the page is reclaimed by 525ebcddc72SAlan Cox * the page daemon. 52626f9a767SRodney W. Grimes */ 52790ecac61SMatthew Dillon vm_page_undirty(mt); 528ebcddc72SAlan Cox if (vm_page_in_laundry(mt)) 529ebcddc72SAlan Cox vm_page_deactivate_noreuse(mt); 53026f9a767SRodney W. Grimes break; 53126f9a767SRodney W. Grimes case VM_PAGER_ERROR: 53226f9a767SRodney W. Grimes case VM_PAGER_FAIL: 53326f9a767SRodney W. Grimes /* 534b1fd102eSMark Johnston * If the page couldn't be paged out to swap because the 535b1fd102eSMark Johnston * pager wasn't able to find space, place the page in 536b1fd102eSMark Johnston * the PQ_UNSWAPPABLE holding queue. This is an 537b1fd102eSMark Johnston * optimization that prevents the page daemon from 538b1fd102eSMark Johnston * wasting CPU cycles on pages that cannot be reclaimed 539fa7a635fSGordon Bergling * because no swap device is configured. 540b1fd102eSMark Johnston * 541b1fd102eSMark Johnston * Otherwise, reactivate the page so that it doesn't 542b1fd102eSMark Johnston * clog the laundry and inactive queues. (We will try 543b1fd102eSMark Johnston * paging it out again later.) 54426f9a767SRodney W. Grimes */ 5454b8365d7SKonstantin Belousov if ((object->flags & OBJ_SWAP) != 0 && 546b1fd102eSMark Johnston pageout_status[i] == VM_PAGER_FAIL) { 547b1fd102eSMark Johnston vm_page_unswappable(mt); 548b1fd102eSMark Johnston numpagedout++; 549b1fd102eSMark Johnston } else 55024a1cce3SDavid Greenman vm_page_activate(mt); 551126d6082SKonstantin Belousov if (eio != NULL && i >= mreq && i - mreq < runlen) 552126d6082SKonstantin Belousov *eio = TRUE; 55326f9a767SRodney W. Grimes break; 55426f9a767SRodney W. Grimes case VM_PAGER_AGAIN: 5551e8a675cSKonstantin Belousov if (i >= mreq && i - mreq < runlen) 5561e8a675cSKonstantin Belousov runlen = i - mreq; 55726f9a767SRodney W. Grimes break; 55826f9a767SRodney W. Grimes } 55926f9a767SRodney W. Grimes 56026f9a767SRodney W. Grimes /* 5610d94caffSDavid Greenman * If the operation is still going, leave the page busy to 5620d94caffSDavid Greenman * block all other accesses. Also, leave the paging in 5630d94caffSDavid Greenman * progress indicator set so that we don't attempt an object 5640d94caffSDavid Greenman * collapse. 56526f9a767SRodney W. Grimes */ 56626f9a767SRodney W. Grimes if (pageout_status[i] != VM_PAGER_PEND) { 567f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 568c7aebda8SAttilio Rao vm_page_sunbusy(mt); 5693c4a2440SAlan Cox } 5703c4a2440SAlan Cox } 5711e8a675cSKonstantin Belousov if (prunlen != NULL) 5721e8a675cSKonstantin Belousov *prunlen = runlen; 5733c4a2440SAlan Cox return (numpagedout); 57426f9a767SRodney W. Grimes } 57526f9a767SRodney W. Grimes 576b1fd102eSMark Johnston static void 577b1fd102eSMark Johnston vm_pageout_swapon(void *arg __unused, struct swdevt *sp __unused) 578b1fd102eSMark Johnston { 579b1fd102eSMark Johnston 580b1fd102eSMark Johnston atomic_store_rel_int(&swapdev_enabled, 1); 581b1fd102eSMark Johnston } 582b1fd102eSMark Johnston 583b1fd102eSMark Johnston static void 584b1fd102eSMark Johnston vm_pageout_swapoff(void *arg __unused, struct swdevt *sp __unused) 585b1fd102eSMark Johnston { 586b1fd102eSMark Johnston 587b1fd102eSMark Johnston if (swap_pager_nswapdev() == 1) 588b1fd102eSMark Johnston atomic_store_rel_int(&swapdev_enabled, 0); 589b1fd102eSMark Johnston } 590b1fd102eSMark Johnston 5911c7c3c6aSMatthew Dillon /* 59234d8b7eaSJeff Roberson * Attempt to acquire all of the necessary locks to launder a page and 59334d8b7eaSJeff Roberson * then call through the clustering layer to PUTPAGES. Wait a short 59434d8b7eaSJeff Roberson * time for a vnode lock. 59534d8b7eaSJeff Roberson * 59634d8b7eaSJeff Roberson * Requires the page and object lock on entry, releases both before return. 59734d8b7eaSJeff Roberson * Returns 0 on success and an errno otherwise. 59834d8b7eaSJeff Roberson */ 59934d8b7eaSJeff Roberson static int 600ebcddc72SAlan Cox vm_pageout_clean(vm_page_t m, int *numpagedout) 60134d8b7eaSJeff Roberson { 60234d8b7eaSJeff Roberson struct vnode *vp; 60334d8b7eaSJeff Roberson struct mount *mp; 60434d8b7eaSJeff Roberson vm_object_t object; 60534d8b7eaSJeff Roberson vm_pindex_t pindex; 6060ef5eee9SKonstantin Belousov int error; 60734d8b7eaSJeff Roberson 60834d8b7eaSJeff Roberson object = m->object; 60934d8b7eaSJeff Roberson VM_OBJECT_ASSERT_WLOCKED(object); 61034d8b7eaSJeff Roberson error = 0; 61134d8b7eaSJeff Roberson vp = NULL; 61234d8b7eaSJeff Roberson mp = NULL; 61334d8b7eaSJeff Roberson 61434d8b7eaSJeff Roberson /* 61534d8b7eaSJeff Roberson * The object is already known NOT to be dead. It 61634d8b7eaSJeff Roberson * is possible for the vget() to block the whole 61734d8b7eaSJeff Roberson * pageout daemon, but the new low-memory handling 61834d8b7eaSJeff Roberson * code should prevent it. 61934d8b7eaSJeff Roberson * 62034d8b7eaSJeff Roberson * We can't wait forever for the vnode lock, we might 62134d8b7eaSJeff Roberson * deadlock due to a vn_read() getting stuck in 62234d8b7eaSJeff Roberson * vm_wait while holding this vnode. We skip the 62334d8b7eaSJeff Roberson * vnode if we can't get it in a reasonable amount 62434d8b7eaSJeff Roberson * of time. 62534d8b7eaSJeff Roberson */ 62634d8b7eaSJeff Roberson if (object->type == OBJT_VNODE) { 62763e97555SJeff Roberson vm_page_xunbusy(m); 62834d8b7eaSJeff Roberson vp = object->handle; 62934d8b7eaSJeff Roberson if (vp->v_type == VREG && 63034d8b7eaSJeff Roberson vn_start_write(vp, &mp, V_NOWAIT) != 0) { 63134d8b7eaSJeff Roberson mp = NULL; 63234d8b7eaSJeff Roberson error = EDEADLK; 63334d8b7eaSJeff Roberson goto unlock_all; 63434d8b7eaSJeff Roberson } 63534d8b7eaSJeff Roberson KASSERT(mp != NULL, 63634d8b7eaSJeff Roberson ("vp %p with NULL v_mount", vp)); 63734d8b7eaSJeff Roberson vm_object_reference_locked(object); 63834d8b7eaSJeff Roberson pindex = m->pindex; 63934d8b7eaSJeff Roberson VM_OBJECT_WUNLOCK(object); 6400ef5eee9SKonstantin Belousov if (vget(vp, vn_lktype_write(NULL, vp) | LK_TIMELOCK) != 0) { 64134d8b7eaSJeff Roberson vp = NULL; 64234d8b7eaSJeff Roberson error = EDEADLK; 64334d8b7eaSJeff Roberson goto unlock_mp; 64434d8b7eaSJeff Roberson } 64534d8b7eaSJeff Roberson VM_OBJECT_WLOCK(object); 64657cd81a3SMark Johnston 64757cd81a3SMark Johnston /* 64857cd81a3SMark Johnston * Ensure that the object and vnode were not disassociated 64957cd81a3SMark Johnston * while locks were dropped. 65057cd81a3SMark Johnston */ 65157cd81a3SMark Johnston if (vp->v_object != object) { 65257cd81a3SMark Johnston error = ENOENT; 65357cd81a3SMark Johnston goto unlock_all; 65457cd81a3SMark Johnston } 65557cd81a3SMark Johnston 65634d8b7eaSJeff Roberson /* 6579f5632e6SMark Johnston * While the object was unlocked, the page may have been: 65834d8b7eaSJeff Roberson * (1) moved to a different queue, 65934d8b7eaSJeff Roberson * (2) reallocated to a different object, 66034d8b7eaSJeff Roberson * (3) reallocated to a different offset, or 66134d8b7eaSJeff Roberson * (4) cleaned. 66234d8b7eaSJeff Roberson */ 663ebcddc72SAlan Cox if (!vm_page_in_laundry(m) || m->object != object || 66434d8b7eaSJeff Roberson m->pindex != pindex || m->dirty == 0) { 66534d8b7eaSJeff Roberson error = ENXIO; 66634d8b7eaSJeff Roberson goto unlock_all; 66734d8b7eaSJeff Roberson } 66834d8b7eaSJeff Roberson 66934d8b7eaSJeff Roberson /* 6709f5632e6SMark Johnston * The page may have been busied while the object lock was 6719f5632e6SMark Johnston * released. 67234d8b7eaSJeff Roberson */ 67363e97555SJeff Roberson if (vm_page_tryxbusy(m) == 0) { 67434d8b7eaSJeff Roberson error = EBUSY; 67534d8b7eaSJeff Roberson goto unlock_all; 67634d8b7eaSJeff Roberson } 67734d8b7eaSJeff Roberson } 67834d8b7eaSJeff Roberson 67934d8b7eaSJeff Roberson /* 680fee2a2faSMark Johnston * Remove all writeable mappings, failing if the page is wired. 681fee2a2faSMark Johnston */ 682fee2a2faSMark Johnston if (!vm_page_try_remove_write(m)) { 68363e97555SJeff Roberson vm_page_xunbusy(m); 684fee2a2faSMark Johnston error = EBUSY; 685fee2a2faSMark Johnston goto unlock_all; 686fee2a2faSMark Johnston } 687fee2a2faSMark Johnston 688fee2a2faSMark Johnston /* 68934d8b7eaSJeff Roberson * If a page is dirty, then it is either being washed 69034d8b7eaSJeff Roberson * (but not yet cleaned) or it is still in the 69134d8b7eaSJeff Roberson * laundry. If it is still in the laundry, then we 69234d8b7eaSJeff Roberson * start the cleaning operation. 69334d8b7eaSJeff Roberson */ 694ebcddc72SAlan Cox if ((*numpagedout = vm_pageout_cluster(m)) == 0) 69534d8b7eaSJeff Roberson error = EIO; 69634d8b7eaSJeff Roberson 69734d8b7eaSJeff Roberson unlock_all: 69834d8b7eaSJeff Roberson VM_OBJECT_WUNLOCK(object); 69934d8b7eaSJeff Roberson 70034d8b7eaSJeff Roberson unlock_mp: 70134d8b7eaSJeff Roberson if (mp != NULL) { 70234d8b7eaSJeff Roberson if (vp != NULL) 70334d8b7eaSJeff Roberson vput(vp); 70434d8b7eaSJeff Roberson vm_object_deallocate(object); 70534d8b7eaSJeff Roberson vn_finished_write(mp); 70634d8b7eaSJeff Roberson } 70734d8b7eaSJeff Roberson 70834d8b7eaSJeff Roberson return (error); 70934d8b7eaSJeff Roberson } 71034d8b7eaSJeff Roberson 71134d8b7eaSJeff Roberson /* 712ebcddc72SAlan Cox * Attempt to launder the specified number of pages. 713ebcddc72SAlan Cox * 714ebcddc72SAlan Cox * Returns the number of pages successfully laundered. 715ebcddc72SAlan Cox */ 716ebcddc72SAlan Cox static int 717ebcddc72SAlan Cox vm_pageout_launder(struct vm_domain *vmd, int launder, bool in_shortfall) 718ebcddc72SAlan Cox { 7195cd29d0fSMark Johnston struct scan_state ss; 720ebcddc72SAlan Cox struct vm_pagequeue *pq; 721ebcddc72SAlan Cox vm_object_t object; 7225cd29d0fSMark Johnston vm_page_t m, marker; 723f3f38e25SMark Johnston vm_page_astate_t new, old; 724f3f38e25SMark Johnston int act_delta, error, numpagedout, queue, refs, starting_target; 725ebcddc72SAlan Cox int vnodes_skipped; 72660256604SMark Johnston bool pageout_ok; 727ebcddc72SAlan Cox 7285cd29d0fSMark Johnston object = NULL; 729ebcddc72SAlan Cox starting_target = launder; 730ebcddc72SAlan Cox vnodes_skipped = 0; 731ebcddc72SAlan Cox 732ebcddc72SAlan Cox /* 733b1fd102eSMark Johnston * Scan the laundry queues for pages eligible to be laundered. We stop 734ebcddc72SAlan Cox * once the target number of dirty pages have been laundered, or once 735ebcddc72SAlan Cox * we've reached the end of the queue. A single iteration of this loop 736ebcddc72SAlan Cox * may cause more than one page to be laundered because of clustering. 737ebcddc72SAlan Cox * 738b1fd102eSMark Johnston * As an optimization, we avoid laundering from PQ_UNSWAPPABLE when no 739b1fd102eSMark Johnston * swap devices are configured. 740ebcddc72SAlan Cox */ 741b1fd102eSMark Johnston if (atomic_load_acq_int(&swapdev_enabled)) 74264b38930SMark Johnston queue = PQ_UNSWAPPABLE; 743b1fd102eSMark Johnston else 74464b38930SMark Johnston queue = PQ_LAUNDRY; 745ebcddc72SAlan Cox 746b1fd102eSMark Johnston scan: 74764b38930SMark Johnston marker = &vmd->vmd_markers[queue]; 7485cd29d0fSMark Johnston pq = &vmd->vmd_pagequeues[queue]; 749ebcddc72SAlan Cox vm_pagequeue_lock(pq); 7505cd29d0fSMark Johnston vm_pageout_init_scan(&ss, pq, marker, NULL, pq->pq_cnt); 7515cd29d0fSMark Johnston while (launder > 0 && (m = vm_pageout_next(&ss, false)) != NULL) { 7525cd29d0fSMark Johnston if (__predict_false((m->flags & PG_MARKER) != 0)) 753ebcddc72SAlan Cox continue; 7545cd29d0fSMark Johnston 7555cd29d0fSMark Johnston /* 756b7f30bffSMark Johnston * Don't touch a page that was removed from the queue after the 757b7f30bffSMark Johnston * page queue lock was released. Otherwise, ensure that any 758b7f30bffSMark Johnston * pending queue operations, such as dequeues for wired pages, 759b7f30bffSMark Johnston * are handled. 7605cd29d0fSMark Johnston */ 761b7f30bffSMark Johnston if (vm_pageout_defer(m, queue, true)) 762ebcddc72SAlan Cox continue; 763e8bcf696SMark Johnston 7649f5632e6SMark Johnston /* 7659f5632e6SMark Johnston * Lock the page's object. 7669f5632e6SMark Johnston */ 7679f5632e6SMark Johnston if (object == NULL || object != m->object) { 76860256604SMark Johnston if (object != NULL) 7695cd29d0fSMark Johnston VM_OBJECT_WUNLOCK(object); 77023ed568cSMateusz Guzik object = atomic_load_ptr(&m->object); 7719f5632e6SMark Johnston if (__predict_false(object == NULL)) 7729f5632e6SMark Johnston /* The page is being freed by another thread. */ 7739f5632e6SMark Johnston continue; 7749f5632e6SMark Johnston 775e8bcf696SMark Johnston /* Depends on type-stability. */ 77641fd4b94SMark Johnston VM_OBJECT_WLOCK(object); 7779f5632e6SMark Johnston if (__predict_false(m->object != object)) { 7789f5632e6SMark Johnston VM_OBJECT_WUNLOCK(object); 7799f5632e6SMark Johnston object = NULL; 78041fd4b94SMark Johnston continue; 7819f5632e6SMark Johnston } 7829f5632e6SMark Johnston } 7835cd29d0fSMark Johnston 78463e97555SJeff Roberson if (vm_page_tryxbusy(m) == 0) 7855cd29d0fSMark Johnston continue; 786ebcddc72SAlan Cox 787ebcddc72SAlan Cox /* 788b7f30bffSMark Johnston * Check for wirings now that we hold the object lock and have 7899f5632e6SMark Johnston * exclusively busied the page. If the page is mapped, it may 7909f5632e6SMark Johnston * still be wired by pmap lookups. The call to 791fee2a2faSMark Johnston * vm_page_try_remove_all() below atomically checks for such 792fee2a2faSMark Johnston * wirings and removes mappings. If the page is unmapped, the 7939f5632e6SMark Johnston * wire count is guaranteed not to increase after this check. 794fee2a2faSMark Johnston */ 7959f5632e6SMark Johnston if (__predict_false(vm_page_wired(m))) 796f3f38e25SMark Johnston goto skip_page; 797fee2a2faSMark Johnston 798fee2a2faSMark Johnston /* 799ebcddc72SAlan Cox * Invalid pages can be easily freed. They cannot be 800ebcddc72SAlan Cox * mapped; vm_page_free() asserts this. 801ebcddc72SAlan Cox */ 8020012f373SJeff Roberson if (vm_page_none_valid(m)) 803ebcddc72SAlan Cox goto free_page; 804ebcddc72SAlan Cox 805b51927b7SKonstantin Belousov refs = object->ref_count != 0 ? pmap_ts_referenced(m) : 0; 806f3f38e25SMark Johnston 807f3f38e25SMark Johnston for (old = vm_page_astate_load(m);;) { 808ebcddc72SAlan Cox /* 809f3f38e25SMark Johnston * Check to see if the page has been removed from the 810f3f38e25SMark Johnston * queue since the first such check. Leave it alone if 811f3f38e25SMark Johnston * so, discarding any references collected by 812f3f38e25SMark Johnston * pmap_ts_referenced(). 813ebcddc72SAlan Cox */ 814f3f38e25SMark Johnston if (__predict_false(_vm_page_queue(old) == PQ_NONE)) 815f3f38e25SMark Johnston goto skip_page; 816f3f38e25SMark Johnston 817f3f38e25SMark Johnston new = old; 818f3f38e25SMark Johnston act_delta = refs; 819f3f38e25SMark Johnston if ((old.flags & PGA_REFERENCED) != 0) { 820f3f38e25SMark Johnston new.flags &= ~PGA_REFERENCED; 821d7aeb429SAlan Cox act_delta++; 822ebcddc72SAlan Cox } 823f3f38e25SMark Johnston if (act_delta == 0) { 824f3f38e25SMark Johnston ; 825b51927b7SKonstantin Belousov } else if (object->ref_count != 0) { 826ebcddc72SAlan Cox /* 827f3f38e25SMark Johnston * Increase the activation count if the page was 828f3f38e25SMark Johnston * referenced while in the laundry queue. This 829f3f38e25SMark Johnston * makes it less likely that the page will be 830f3f38e25SMark Johnston * returned prematurely to the laundry queue. 831e8bcf696SMark Johnston */ 832f3f38e25SMark Johnston new.act_count += ACT_ADVANCE + 833f3f38e25SMark Johnston act_delta; 834f3f38e25SMark Johnston if (new.act_count > ACT_MAX) 835f3f38e25SMark Johnston new.act_count = ACT_MAX; 836f3f38e25SMark Johnston 837f7607c30SMark Johnston new.flags &= ~PGA_QUEUE_OP_MASK; 838f3f38e25SMark Johnston new.flags |= PGA_REQUEUE; 839f3f38e25SMark Johnston new.queue = PQ_ACTIVE; 840f3f38e25SMark Johnston if (!vm_page_pqstate_commit(m, &old, new)) 841f3f38e25SMark Johnston continue; 842e8bcf696SMark Johnston 843e8bcf696SMark Johnston /* 844e8bcf696SMark Johnston * If this was a background laundering, count 845e8bcf696SMark Johnston * activated pages towards our target. The 846e8bcf696SMark Johnston * purpose of background laundering is to ensure 847e8bcf696SMark Johnston * that pages are eventually cycled through the 848e8bcf696SMark Johnston * laundry queue, and an activation is a valid 849e8bcf696SMark Johnston * way out. 850ebcddc72SAlan Cox */ 851ebcddc72SAlan Cox if (!in_shortfall) 852ebcddc72SAlan Cox launder--; 853f3f38e25SMark Johnston VM_CNT_INC(v_reactivated); 854f3f38e25SMark Johnston goto skip_page; 8555cd29d0fSMark Johnston } else if ((object->flags & OBJ_DEAD) == 0) { 856f3f38e25SMark Johnston new.flags |= PGA_REQUEUE; 857f3f38e25SMark Johnston if (!vm_page_pqstate_commit(m, &old, new)) 858e8bcf696SMark Johnston continue; 859f3f38e25SMark Johnston goto skip_page; 8605cd29d0fSMark Johnston } 861f3f38e25SMark Johnston break; 862ebcddc72SAlan Cox } 863ebcddc72SAlan Cox 864ebcddc72SAlan Cox /* 865ebcddc72SAlan Cox * If the page appears to be clean at the machine-independent 866ebcddc72SAlan Cox * layer, then remove all of its mappings from the pmap in 867ebcddc72SAlan Cox * anticipation of freeing it. If, however, any of the page's 868ebcddc72SAlan Cox * mappings allow write access, then the page may still be 869ebcddc72SAlan Cox * modified until the last of those mappings are removed. 870ebcddc72SAlan Cox */ 871ebcddc72SAlan Cox if (object->ref_count != 0) { 872ebcddc72SAlan Cox vm_page_test_dirty(m); 8739f5632e6SMark Johnston if (m->dirty == 0 && !vm_page_try_remove_all(m)) 874f3f38e25SMark Johnston goto skip_page; 875fee2a2faSMark Johnston } 876ebcddc72SAlan Cox 877ebcddc72SAlan Cox /* 878ebcddc72SAlan Cox * Clean pages are freed, and dirty pages are paged out unless 879ebcddc72SAlan Cox * they belong to a dead object. Requeueing dirty pages from 880ebcddc72SAlan Cox * dead objects is pointless, as they are being paged out and 881ebcddc72SAlan Cox * freed by the thread that destroyed the object. 882ebcddc72SAlan Cox */ 883ebcddc72SAlan Cox if (m->dirty == 0) { 884ebcddc72SAlan Cox free_page: 8859f5632e6SMark Johnston /* 8869f5632e6SMark Johnston * Now we are guaranteed that no other threads are 8879f5632e6SMark Johnston * manipulating the page, check for a last-second 8889f5632e6SMark Johnston * reference. 8899f5632e6SMark Johnston */ 8909f5632e6SMark Johnston if (vm_pageout_defer(m, queue, true)) 8919f5632e6SMark Johnston goto skip_page; 892ebcddc72SAlan Cox vm_page_free(m); 89383c9dea1SGleb Smirnoff VM_CNT_INC(v_dfree); 894ebcddc72SAlan Cox } else if ((object->flags & OBJ_DEAD) == 0) { 8950cb2610eSMark Johnston if ((object->flags & OBJ_SWAP) != 0) 8960cb2610eSMark Johnston pageout_ok = disable_swap_pageouts == 0; 897ebcddc72SAlan Cox else 898ebcddc72SAlan Cox pageout_ok = true; 899ebcddc72SAlan Cox if (!pageout_ok) { 900f3f38e25SMark Johnston vm_page_launder(m); 901f3f38e25SMark Johnston goto skip_page; 902ebcddc72SAlan Cox } 903ebcddc72SAlan Cox 904ebcddc72SAlan Cox /* 905ebcddc72SAlan Cox * Form a cluster with adjacent, dirty pages from the 906ebcddc72SAlan Cox * same object, and page out that entire cluster. 907ebcddc72SAlan Cox * 908ebcddc72SAlan Cox * The adjacent, dirty pages must also be in the 909ebcddc72SAlan Cox * laundry. However, their mappings are not checked 910ebcddc72SAlan Cox * for new references. Consequently, a recently 911ebcddc72SAlan Cox * referenced page may be paged out. However, that 912ebcddc72SAlan Cox * page will not be prematurely reclaimed. After page 913ebcddc72SAlan Cox * out, the page will be placed in the inactive queue, 914ebcddc72SAlan Cox * where any new references will be detected and the 915ebcddc72SAlan Cox * page reactivated. 916ebcddc72SAlan Cox */ 917ebcddc72SAlan Cox error = vm_pageout_clean(m, &numpagedout); 918ebcddc72SAlan Cox if (error == 0) { 919ebcddc72SAlan Cox launder -= numpagedout; 9205cd29d0fSMark Johnston ss.scanned += numpagedout; 921ebcddc72SAlan Cox } else if (error == EDEADLK) { 922ebcddc72SAlan Cox pageout_lock_miss++; 923ebcddc72SAlan Cox vnodes_skipped++; 924ebcddc72SAlan Cox } 92560256604SMark Johnston object = NULL; 926f3f38e25SMark Johnston } else { 927f3f38e25SMark Johnston skip_page: 92863e97555SJeff Roberson vm_page_xunbusy(m); 929e8bcf696SMark Johnston } 930f3f38e25SMark Johnston } 93146e39081SMark Johnston if (object != NULL) { 932ebcddc72SAlan Cox VM_OBJECT_WUNLOCK(object); 93346e39081SMark Johnston object = NULL; 93446e39081SMark Johnston } 935ebcddc72SAlan Cox vm_pagequeue_lock(pq); 9365cd29d0fSMark Johnston vm_pageout_end_scan(&ss); 937ebcddc72SAlan Cox vm_pagequeue_unlock(pq); 938ebcddc72SAlan Cox 93964b38930SMark Johnston if (launder > 0 && queue == PQ_UNSWAPPABLE) { 94064b38930SMark Johnston queue = PQ_LAUNDRY; 941b1fd102eSMark Johnston goto scan; 942b1fd102eSMark Johnston } 943b1fd102eSMark Johnston 944ebcddc72SAlan Cox /* 945ebcddc72SAlan Cox * Wakeup the sync daemon if we skipped a vnode in a writeable object 946ebcddc72SAlan Cox * and we didn't launder enough pages. 947ebcddc72SAlan Cox */ 948ebcddc72SAlan Cox if (vnodes_skipped > 0 && launder > 0) 949ebcddc72SAlan Cox (void)speedup_syncer(); 950ebcddc72SAlan Cox 951ebcddc72SAlan Cox return (starting_target - launder); 952ebcddc72SAlan Cox } 953ebcddc72SAlan Cox 954ebcddc72SAlan Cox /* 955ebcddc72SAlan Cox * Compute the integer square root. 956ebcddc72SAlan Cox */ 957ebcddc72SAlan Cox static u_int 958ebcddc72SAlan Cox isqrt(u_int num) 959ebcddc72SAlan Cox { 960ebcddc72SAlan Cox u_int bit, root, tmp; 961ebcddc72SAlan Cox 96264f8d257SDoug Moore bit = num != 0 ? (1u << ((fls(num) - 1) & ~1)) : 0; 963ebcddc72SAlan Cox root = 0; 964ebcddc72SAlan Cox while (bit != 0) { 965ebcddc72SAlan Cox tmp = root + bit; 966ebcddc72SAlan Cox root >>= 1; 967ebcddc72SAlan Cox if (num >= tmp) { 968ebcddc72SAlan Cox num -= tmp; 969ebcddc72SAlan Cox root += bit; 970ebcddc72SAlan Cox } 971ebcddc72SAlan Cox bit >>= 2; 972ebcddc72SAlan Cox } 973ebcddc72SAlan Cox return (root); 974ebcddc72SAlan Cox } 975ebcddc72SAlan Cox 976ebcddc72SAlan Cox /* 977ebcddc72SAlan Cox * Perform the work of the laundry thread: periodically wake up and determine 978ebcddc72SAlan Cox * whether any pages need to be laundered. If so, determine the number of pages 979ebcddc72SAlan Cox * that need to be laundered, and launder them. 980ebcddc72SAlan Cox */ 981ebcddc72SAlan Cox static void 982ebcddc72SAlan Cox vm_pageout_laundry_worker(void *arg) 983ebcddc72SAlan Cox { 984e2068d0bSJeff Roberson struct vm_domain *vmd; 985ebcddc72SAlan Cox struct vm_pagequeue *pq; 98660684862SMark Johnston uint64_t nclean, ndirty, nfreed; 987e2068d0bSJeff Roberson int domain, last_target, launder, shortfall, shortfall_cycle, target; 988ebcddc72SAlan Cox bool in_shortfall; 989ebcddc72SAlan Cox 990e2068d0bSJeff Roberson domain = (uintptr_t)arg; 991e2068d0bSJeff Roberson vmd = VM_DOMAIN(domain); 992e2068d0bSJeff Roberson pq = &vmd->vmd_pagequeues[PQ_LAUNDRY]; 993e2068d0bSJeff Roberson KASSERT(vmd->vmd_segs != 0, ("domain without segments")); 994ebcddc72SAlan Cox 995ebcddc72SAlan Cox shortfall = 0; 996ebcddc72SAlan Cox in_shortfall = false; 997ebcddc72SAlan Cox shortfall_cycle = 0; 9988002c3a4SMark Johnston last_target = target = 0; 99960684862SMark Johnston nfreed = 0; 1000ebcddc72SAlan Cox 1001ebcddc72SAlan Cox /* 1002b1fd102eSMark Johnston * Calls to these handlers are serialized by the swap syscall lock. 1003b1fd102eSMark Johnston */ 1004e2068d0bSJeff Roberson (void)EVENTHANDLER_REGISTER(swapon, vm_pageout_swapon, vmd, 1005b1fd102eSMark Johnston EVENTHANDLER_PRI_ANY); 1006e2068d0bSJeff Roberson (void)EVENTHANDLER_REGISTER(swapoff, vm_pageout_swapoff, vmd, 1007b1fd102eSMark Johnston EVENTHANDLER_PRI_ANY); 1008b1fd102eSMark Johnston 1009b1fd102eSMark Johnston /* 1010ebcddc72SAlan Cox * The pageout laundry worker is never done, so loop forever. 1011ebcddc72SAlan Cox */ 1012ebcddc72SAlan Cox for (;;) { 1013ebcddc72SAlan Cox KASSERT(target >= 0, ("negative target %d", target)); 1014ebcddc72SAlan Cox KASSERT(shortfall_cycle >= 0, 1015ebcddc72SAlan Cox ("negative cycle %d", shortfall_cycle)); 1016ebcddc72SAlan Cox launder = 0; 1017ebcddc72SAlan Cox 1018ebcddc72SAlan Cox /* 1019ebcddc72SAlan Cox * First determine whether we need to launder pages to meet a 1020ebcddc72SAlan Cox * shortage of free pages. 1021ebcddc72SAlan Cox */ 1022ebcddc72SAlan Cox if (shortfall > 0) { 1023ebcddc72SAlan Cox in_shortfall = true; 1024ebcddc72SAlan Cox shortfall_cycle = VM_LAUNDER_RATE / VM_INACT_SCAN_RATE; 1025ebcddc72SAlan Cox target = shortfall; 1026ebcddc72SAlan Cox } else if (!in_shortfall) 1027ebcddc72SAlan Cox goto trybackground; 1028e2068d0bSJeff Roberson else if (shortfall_cycle == 0 || vm_laundry_target(vmd) <= 0) { 1029ebcddc72SAlan Cox /* 1030ebcddc72SAlan Cox * We recently entered shortfall and began laundering 1031ebcddc72SAlan Cox * pages. If we have completed that laundering run 1032ebcddc72SAlan Cox * (and we are no longer in shortfall) or we have met 1033ebcddc72SAlan Cox * our laundry target through other activity, then we 1034ebcddc72SAlan Cox * can stop laundering pages. 1035ebcddc72SAlan Cox */ 1036ebcddc72SAlan Cox in_shortfall = false; 1037ebcddc72SAlan Cox target = 0; 1038ebcddc72SAlan Cox goto trybackground; 1039ebcddc72SAlan Cox } 1040ebcddc72SAlan Cox launder = target / shortfall_cycle--; 1041ebcddc72SAlan Cox goto dolaundry; 1042ebcddc72SAlan Cox 1043ebcddc72SAlan Cox /* 1044ebcddc72SAlan Cox * There's no immediate need to launder any pages; see if we 1045ebcddc72SAlan Cox * meet the conditions to perform background laundering: 1046ebcddc72SAlan Cox * 1047ebcddc72SAlan Cox * 1. The ratio of dirty to clean inactive pages exceeds the 104860684862SMark Johnston * background laundering threshold, or 1049ebcddc72SAlan Cox * 2. we haven't yet reached the target of the current 1050ebcddc72SAlan Cox * background laundering run. 1051ebcddc72SAlan Cox * 1052ebcddc72SAlan Cox * The background laundering threshold is not a constant. 1053ebcddc72SAlan Cox * Instead, it is a slowly growing function of the number of 105460684862SMark Johnston * clean pages freed by the page daemon since the last 105560684862SMark Johnston * background laundering. Thus, as the ratio of dirty to 105660684862SMark Johnston * clean inactive pages grows, the amount of memory pressure 1057c098768eSMark Johnston * required to trigger laundering decreases. We ensure 1058c098768eSMark Johnston * that the threshold is non-zero after an inactive queue 1059c098768eSMark Johnston * scan, even if that scan failed to free a single clean page. 1060ebcddc72SAlan Cox */ 1061ebcddc72SAlan Cox trybackground: 1062e2068d0bSJeff Roberson nclean = vmd->vmd_free_count + 1063e2068d0bSJeff Roberson vmd->vmd_pagequeues[PQ_INACTIVE].pq_cnt; 1064e2068d0bSJeff Roberson ndirty = vmd->vmd_pagequeues[PQ_LAUNDRY].pq_cnt; 1065c098768eSMark Johnston if (target == 0 && ndirty * isqrt(howmany(nfreed + 1, 1066c098768eSMark Johnston vmd->vmd_free_target - vmd->vmd_free_min)) >= nclean) { 1067e2068d0bSJeff Roberson target = vmd->vmd_background_launder_target; 1068ebcddc72SAlan Cox } 1069ebcddc72SAlan Cox 1070ebcddc72SAlan Cox /* 1071ebcddc72SAlan Cox * We have a non-zero background laundering target. If we've 1072ebcddc72SAlan Cox * laundered up to our maximum without observing a page daemon 1073cb35676eSMark Johnston * request, just stop. This is a safety belt that ensures we 1074ebcddc72SAlan Cox * don't launder an excessive amount if memory pressure is low 1075ebcddc72SAlan Cox * and the ratio of dirty to clean pages is large. Otherwise, 1076ebcddc72SAlan Cox * proceed at the background laundering rate. 1077ebcddc72SAlan Cox */ 1078ebcddc72SAlan Cox if (target > 0) { 107960684862SMark Johnston if (nfreed > 0) { 108060684862SMark Johnston nfreed = 0; 1081ebcddc72SAlan Cox last_target = target; 1082ebcddc72SAlan Cox } else if (last_target - target >= 1083ebcddc72SAlan Cox vm_background_launder_max * PAGE_SIZE / 1024) { 1084ebcddc72SAlan Cox target = 0; 1085ebcddc72SAlan Cox } 1086ebcddc72SAlan Cox launder = vm_background_launder_rate * PAGE_SIZE / 1024; 1087ebcddc72SAlan Cox launder /= VM_LAUNDER_RATE; 1088ebcddc72SAlan Cox if (launder > target) 1089ebcddc72SAlan Cox launder = target; 1090ebcddc72SAlan Cox } 1091ebcddc72SAlan Cox 1092ebcddc72SAlan Cox dolaundry: 1093ebcddc72SAlan Cox if (launder > 0) { 1094ebcddc72SAlan Cox /* 1095ebcddc72SAlan Cox * Because of I/O clustering, the number of laundered 1096ebcddc72SAlan Cox * pages could exceed "target" by the maximum size of 1097ebcddc72SAlan Cox * a cluster minus one. 1098ebcddc72SAlan Cox */ 1099e2068d0bSJeff Roberson target -= min(vm_pageout_launder(vmd, launder, 1100ebcddc72SAlan Cox in_shortfall), target); 1101ebcddc72SAlan Cox pause("laundp", hz / VM_LAUNDER_RATE); 1102ebcddc72SAlan Cox } 1103ebcddc72SAlan Cox 1104ebcddc72SAlan Cox /* 1105ebcddc72SAlan Cox * If we're not currently laundering pages and the page daemon 1106ebcddc72SAlan Cox * hasn't posted a new request, sleep until the page daemon 1107ebcddc72SAlan Cox * kicks us. 1108ebcddc72SAlan Cox */ 1109ebcddc72SAlan Cox vm_pagequeue_lock(pq); 1110e2068d0bSJeff Roberson if (target == 0 && vmd->vmd_laundry_request == VM_LAUNDRY_IDLE) 1111e2068d0bSJeff Roberson (void)mtx_sleep(&vmd->vmd_laundry_request, 1112ebcddc72SAlan Cox vm_pagequeue_lockptr(pq), PVM, "launds", 0); 1113ebcddc72SAlan Cox 1114ebcddc72SAlan Cox /* 1115ebcddc72SAlan Cox * If the pagedaemon has indicated that it's in shortfall, start 1116ebcddc72SAlan Cox * a shortfall laundering unless we're already in the middle of 1117ebcddc72SAlan Cox * one. This may preempt a background laundering. 1118ebcddc72SAlan Cox */ 1119e2068d0bSJeff Roberson if (vmd->vmd_laundry_request == VM_LAUNDRY_SHORTFALL && 1120ebcddc72SAlan Cox (!in_shortfall || shortfall_cycle == 0)) { 1121e2068d0bSJeff Roberson shortfall = vm_laundry_target(vmd) + 1122e2068d0bSJeff Roberson vmd->vmd_pageout_deficit; 1123ebcddc72SAlan Cox target = 0; 1124ebcddc72SAlan Cox } else 1125ebcddc72SAlan Cox shortfall = 0; 1126ebcddc72SAlan Cox 1127ebcddc72SAlan Cox if (target == 0) 1128e2068d0bSJeff Roberson vmd->vmd_laundry_request = VM_LAUNDRY_IDLE; 112960684862SMark Johnston nfreed += vmd->vmd_clean_pages_freed; 113060684862SMark Johnston vmd->vmd_clean_pages_freed = 0; 1131ebcddc72SAlan Cox vm_pagequeue_unlock(pq); 1132ebcddc72SAlan Cox } 1133ebcddc72SAlan Cox } 1134ebcddc72SAlan Cox 1135be37ee79SMark Johnston /* 1136be37ee79SMark Johnston * Compute the number of pages we want to try to move from the 1137be37ee79SMark Johnston * active queue to either the inactive or laundry queue. 1138be37ee79SMark Johnston * 11397bb4634eSMark Johnston * When scanning active pages during a shortage, we make clean pages 11407bb4634eSMark Johnston * count more heavily towards the page shortage than dirty pages. 11417bb4634eSMark Johnston * This is because dirty pages must be laundered before they can be 11427bb4634eSMark Johnston * reused and thus have less utility when attempting to quickly 11437bb4634eSMark Johnston * alleviate a free page shortage. However, this weighting also 11447bb4634eSMark Johnston * causes the scan to deactivate dirty pages more aggressively, 11457bb4634eSMark Johnston * improving the effectiveness of clustering. 1146be37ee79SMark Johnston */ 1147be37ee79SMark Johnston static int 11487bb4634eSMark Johnston vm_pageout_active_target(struct vm_domain *vmd) 1149be37ee79SMark Johnston { 1150be37ee79SMark Johnston int shortage; 1151be37ee79SMark Johnston 1152be37ee79SMark Johnston shortage = vmd->vmd_inactive_target + vm_paging_target(vmd) - 1153be37ee79SMark Johnston (vmd->vmd_pagequeues[PQ_INACTIVE].pq_cnt + 1154be37ee79SMark Johnston vmd->vmd_pagequeues[PQ_LAUNDRY].pq_cnt / act_scan_laundry_weight); 1155be37ee79SMark Johnston shortage *= act_scan_laundry_weight; 1156be37ee79SMark Johnston return (shortage); 1157be37ee79SMark Johnston } 1158be37ee79SMark Johnston 1159be37ee79SMark Johnston /* 1160be37ee79SMark Johnston * Scan the active queue. If there is no shortage of inactive pages, scan a 1161be37ee79SMark Johnston * small portion of the queue in order to maintain quasi-LRU. 1162be37ee79SMark Johnston */ 1163be37ee79SMark Johnston static void 1164be37ee79SMark Johnston vm_pageout_scan_active(struct vm_domain *vmd, int page_shortage) 1165be37ee79SMark Johnston { 1166be37ee79SMark Johnston struct scan_state ss; 1167fee2a2faSMark Johnston vm_object_t object; 1168be37ee79SMark Johnston vm_page_t m, marker; 1169be37ee79SMark Johnston struct vm_pagequeue *pq; 1170f3f38e25SMark Johnston vm_page_astate_t old, new; 1171be37ee79SMark Johnston long min_scan; 1172f3f38e25SMark Johnston int act_delta, max_scan, ps_delta, refs, scan_tick; 1173f3f38e25SMark Johnston uint8_t nqueue; 1174be37ee79SMark Johnston 1175be37ee79SMark Johnston marker = &vmd->vmd_markers[PQ_ACTIVE]; 1176be37ee79SMark Johnston pq = &vmd->vmd_pagequeues[PQ_ACTIVE]; 1177be37ee79SMark Johnston vm_pagequeue_lock(pq); 1178be37ee79SMark Johnston 1179be37ee79SMark Johnston /* 1180be37ee79SMark Johnston * If we're just idle polling attempt to visit every 1181be37ee79SMark Johnston * active page within 'update_period' seconds. 1182be37ee79SMark Johnston */ 1183be37ee79SMark Johnston scan_tick = ticks; 1184be37ee79SMark Johnston if (vm_pageout_update_period != 0) { 1185be37ee79SMark Johnston min_scan = pq->pq_cnt; 1186be37ee79SMark Johnston min_scan *= scan_tick - vmd->vmd_last_active_scan; 1187be37ee79SMark Johnston min_scan /= hz * vm_pageout_update_period; 1188be37ee79SMark Johnston } else 1189be37ee79SMark Johnston min_scan = 0; 1190be37ee79SMark Johnston if (min_scan > 0 || (page_shortage > 0 && pq->pq_cnt > 0)) 1191be37ee79SMark Johnston vmd->vmd_last_active_scan = scan_tick; 1192be37ee79SMark Johnston 1193be37ee79SMark Johnston /* 1194be37ee79SMark Johnston * Scan the active queue for pages that can be deactivated. Update 1195be37ee79SMark Johnston * the per-page activity counter and use it to identify deactivation 1196be37ee79SMark Johnston * candidates. Held pages may be deactivated. 1197be37ee79SMark Johnston * 1198be37ee79SMark Johnston * To avoid requeuing each page that remains in the active queue, we 11997bb4634eSMark Johnston * implement the CLOCK algorithm. To keep the implementation of the 12007bb4634eSMark Johnston * enqueue operation consistent for all page queues, we use two hands, 12017bb4634eSMark Johnston * represented by marker pages. Scans begin at the first hand, which 12027bb4634eSMark Johnston * precedes the second hand in the queue. When the two hands meet, 12037bb4634eSMark Johnston * they are moved back to the head and tail of the queue, respectively, 12047bb4634eSMark Johnston * and scanning resumes. 1205be37ee79SMark Johnston */ 1206be37ee79SMark Johnston max_scan = page_shortage > 0 ? pq->pq_cnt : min_scan; 1207be37ee79SMark Johnston act_scan: 1208be37ee79SMark Johnston vm_pageout_init_scan(&ss, pq, marker, &vmd->vmd_clock[0], max_scan); 1209be37ee79SMark Johnston while ((m = vm_pageout_next(&ss, false)) != NULL) { 1210be37ee79SMark Johnston if (__predict_false(m == &vmd->vmd_clock[1])) { 1211be37ee79SMark Johnston vm_pagequeue_lock(pq); 1212be37ee79SMark Johnston TAILQ_REMOVE(&pq->pq_pl, &vmd->vmd_clock[0], plinks.q); 1213be37ee79SMark Johnston TAILQ_REMOVE(&pq->pq_pl, &vmd->vmd_clock[1], plinks.q); 1214be37ee79SMark Johnston TAILQ_INSERT_HEAD(&pq->pq_pl, &vmd->vmd_clock[0], 1215be37ee79SMark Johnston plinks.q); 1216be37ee79SMark Johnston TAILQ_INSERT_TAIL(&pq->pq_pl, &vmd->vmd_clock[1], 1217be37ee79SMark Johnston plinks.q); 1218be37ee79SMark Johnston max_scan -= ss.scanned; 1219be37ee79SMark Johnston vm_pageout_end_scan(&ss); 1220be37ee79SMark Johnston goto act_scan; 1221be37ee79SMark Johnston } 1222be37ee79SMark Johnston if (__predict_false((m->flags & PG_MARKER) != 0)) 1223be37ee79SMark Johnston continue; 1224be37ee79SMark Johnston 1225e8bcf696SMark Johnston /* 1226b7f30bffSMark Johnston * Don't touch a page that was removed from the queue after the 1227b7f30bffSMark Johnston * page queue lock was released. Otherwise, ensure that any 1228b7f30bffSMark Johnston * pending queue operations, such as dequeues for wired pages, 1229b7f30bffSMark Johnston * are handled. 1230e8bcf696SMark Johnston */ 1231b7f30bffSMark Johnston if (vm_pageout_defer(m, PQ_ACTIVE, true)) 1232e8bcf696SMark Johnston continue; 1233e8bcf696SMark Johnston 1234e8bcf696SMark Johnston /* 1235e8bcf696SMark Johnston * A page's object pointer may be set to NULL before 1236e8bcf696SMark Johnston * the object lock is acquired. 1237e8bcf696SMark Johnston */ 123823ed568cSMateusz Guzik object = atomic_load_ptr(&m->object); 1239fee2a2faSMark Johnston if (__predict_false(object == NULL)) 1240fee2a2faSMark Johnston /* 1241fee2a2faSMark Johnston * The page has been removed from its object. 1242fee2a2faSMark Johnston */ 1243fee2a2faSMark Johnston continue; 1244fee2a2faSMark Johnston 1245f3f38e25SMark Johnston /* Deferred free of swap space. */ 1246f3f38e25SMark Johnston if ((m->a.flags & PGA_SWAP_FREE) != 0 && 1247f3f38e25SMark Johnston VM_OBJECT_TRYWLOCK(object)) { 1248f3f38e25SMark Johnston if (m->object == object) 1249f3f38e25SMark Johnston vm_pager_page_unswapped(m); 1250f3f38e25SMark Johnston VM_OBJECT_WUNLOCK(object); 1251f3f38e25SMark Johnston } 1252f3f38e25SMark Johnston 1253fee2a2faSMark Johnston /* 1254be37ee79SMark Johnston * Check to see "how much" the page has been used. 1255d7aeb429SAlan Cox * 1256d7aeb429SAlan Cox * Test PGA_REFERENCED after calling pmap_ts_referenced() so 1257d7aeb429SAlan Cox * that a reference from a concurrently destroyed mapping is 1258d7aeb429SAlan Cox * observed here and now. 1259b51927b7SKonstantin Belousov * 1260b51927b7SKonstantin Belousov * Perform an unsynchronized object ref count check. While 1261b51927b7SKonstantin Belousov * the page lock ensures that the page is not reallocated to 1262b51927b7SKonstantin Belousov * another object, in particular, one with unmanaged mappings 1263b51927b7SKonstantin Belousov * that cannot support pmap_ts_referenced(), two races are, 1264b51927b7SKonstantin Belousov * nonetheless, possible: 1265b51927b7SKonstantin Belousov * 1) The count was transitioning to zero, but we saw a non- 1266b51927b7SKonstantin Belousov * zero value. pmap_ts_referenced() will return zero 1267b51927b7SKonstantin Belousov * because the page is not mapped. 1268b51927b7SKonstantin Belousov * 2) The count was transitioning to one, but we saw zero. 1269b51927b7SKonstantin Belousov * This race delays the detection of a new reference. At 1270b51927b7SKonstantin Belousov * worst, we will deactivate and reactivate the page. 1271be37ee79SMark Johnston */ 1272b51927b7SKonstantin Belousov refs = object->ref_count != 0 ? pmap_ts_referenced(m) : 0; 1273be37ee79SMark Johnston 1274f3f38e25SMark Johnston old = vm_page_astate_load(m); 1275f3f38e25SMark Johnston do { 1276f3f38e25SMark Johnston /* 1277f3f38e25SMark Johnston * Check to see if the page has been removed from the 1278f3f38e25SMark Johnston * queue since the first such check. Leave it alone if 1279f3f38e25SMark Johnston * so, discarding any references collected by 1280f3f38e25SMark Johnston * pmap_ts_referenced(). 1281f3f38e25SMark Johnston */ 1282609de97eSEric van Gyzen if (__predict_false(_vm_page_queue(old) == PQ_NONE)) { 1283609de97eSEric van Gyzen ps_delta = 0; 1284f3f38e25SMark Johnston break; 1285609de97eSEric van Gyzen } 1286a8081778SJeff Roberson 1287be37ee79SMark Johnston /* 1288be37ee79SMark Johnston * Advance or decay the act_count based on recent usage. 1289be37ee79SMark Johnston */ 1290f3f38e25SMark Johnston new = old; 1291f3f38e25SMark Johnston act_delta = refs; 1292f3f38e25SMark Johnston if ((old.flags & PGA_REFERENCED) != 0) { 1293f3f38e25SMark Johnston new.flags &= ~PGA_REFERENCED; 1294f3f38e25SMark Johnston act_delta++; 1295f3f38e25SMark Johnston } 1296be37ee79SMark Johnston if (act_delta != 0) { 1297f3f38e25SMark Johnston new.act_count += ACT_ADVANCE + act_delta; 1298f3f38e25SMark Johnston if (new.act_count > ACT_MAX) 1299f3f38e25SMark Johnston new.act_count = ACT_MAX; 1300f3f38e25SMark Johnston } else { 1301f3f38e25SMark Johnston new.act_count -= min(new.act_count, 1302f3f38e25SMark Johnston ACT_DECLINE); 1303f3f38e25SMark Johnston } 1304be37ee79SMark Johnston 1305f3f38e25SMark Johnston if (new.act_count > 0) { 1306be37ee79SMark Johnston /* 1307f3f38e25SMark Johnston * Adjust the activation count and keep the page 1308f3f38e25SMark Johnston * in the active queue. The count might be left 1309f3f38e25SMark Johnston * unchanged if it is saturated. The page may 1310f3f38e25SMark Johnston * have been moved to a different queue since we 1311f3f38e25SMark Johnston * started the scan, in which case we move it 1312f3f38e25SMark Johnston * back. 1313be37ee79SMark Johnston */ 1314f3f38e25SMark Johnston ps_delta = 0; 1315f3f38e25SMark Johnston if (old.queue != PQ_ACTIVE) { 1316f7607c30SMark Johnston new.flags &= ~PGA_QUEUE_OP_MASK; 1317f7607c30SMark Johnston new.flags |= PGA_REQUEUE; 1318f7607c30SMark Johnston new.queue = PQ_ACTIVE; 1319f3f38e25SMark Johnston } 13207cdeaf33SMark Johnston } else { 1321be37ee79SMark Johnston /* 1322f3f38e25SMark Johnston * When not short for inactive pages, let dirty 1323f3f38e25SMark Johnston * pages go through the inactive queue before 1324f3f38e25SMark Johnston * moving to the laundry queue. This gives them 1325f3f38e25SMark Johnston * some extra time to be reactivated, 1326f3f38e25SMark Johnston * potentially avoiding an expensive pageout. 1327f3f38e25SMark Johnston * However, during a page shortage, the inactive 1328f3f38e25SMark Johnston * queue is necessarily small, and so dirty 1329f3f38e25SMark Johnston * pages would only spend a trivial amount of 1330f3f38e25SMark Johnston * time in the inactive queue. Therefore, we 1331f3f38e25SMark Johnston * might as well place them directly in the 1332f3f38e25SMark Johnston * laundry queue to reduce queuing overhead. 1333f3f38e25SMark Johnston * 1334be37ee79SMark Johnston * Calling vm_page_test_dirty() here would 1335be37ee79SMark Johnston * require acquisition of the object's write 1336be37ee79SMark Johnston * lock. However, during a page shortage, 1337f3f38e25SMark Johnston * directing dirty pages into the laundry queue 1338f3f38e25SMark Johnston * is only an optimization and not a 1339be37ee79SMark Johnston * requirement. Therefore, we simply rely on 1340f3f38e25SMark Johnston * the opportunistic updates to the page's dirty 1341f3f38e25SMark Johnston * field by the pmap. 1342be37ee79SMark Johnston */ 1343f3f38e25SMark Johnston if (page_shortage <= 0) { 1344f3f38e25SMark Johnston nqueue = PQ_INACTIVE; 1345f3f38e25SMark Johnston ps_delta = 0; 1346f3f38e25SMark Johnston } else if (m->dirty == 0) { 1347f3f38e25SMark Johnston nqueue = PQ_INACTIVE; 1348f3f38e25SMark Johnston ps_delta = act_scan_laundry_weight; 1349be37ee79SMark Johnston } else { 1350f3f38e25SMark Johnston nqueue = PQ_LAUNDRY; 1351f3f38e25SMark Johnston ps_delta = 1; 1352be37ee79SMark Johnston } 1353f3f38e25SMark Johnston 1354f7607c30SMark Johnston new.flags &= ~PGA_QUEUE_OP_MASK; 1355f3f38e25SMark Johnston new.flags |= PGA_REQUEUE; 1356f3f38e25SMark Johnston new.queue = nqueue; 1357be37ee79SMark Johnston } 1358f3f38e25SMark Johnston } while (!vm_page_pqstate_commit(m, &old, new)); 1359f3f38e25SMark Johnston 1360f3f38e25SMark Johnston page_shortage -= ps_delta; 1361be37ee79SMark Johnston } 1362be37ee79SMark Johnston vm_pagequeue_lock(pq); 1363be37ee79SMark Johnston TAILQ_REMOVE(&pq->pq_pl, &vmd->vmd_clock[0], plinks.q); 1364be37ee79SMark Johnston TAILQ_INSERT_AFTER(&pq->pq_pl, marker, &vmd->vmd_clock[0], plinks.q); 1365be37ee79SMark Johnston vm_pageout_end_scan(&ss); 1366be37ee79SMark Johnston vm_pagequeue_unlock(pq); 1367be37ee79SMark Johnston } 1368be37ee79SMark Johnston 13695cd29d0fSMark Johnston static int 1370f3f38e25SMark Johnston vm_pageout_reinsert_inactive_page(struct vm_pagequeue *pq, vm_page_t marker, 1371f3f38e25SMark Johnston vm_page_t m) 13725cd29d0fSMark Johnston { 1373f3f38e25SMark Johnston vm_page_astate_t as; 13745cd29d0fSMark Johnston 1375f3f38e25SMark Johnston vm_pagequeue_assert_locked(pq); 1376f3f38e25SMark Johnston 1377f3f38e25SMark Johnston as = vm_page_astate_load(m); 1378f3f38e25SMark Johnston if (as.queue != PQ_INACTIVE || (as.flags & PGA_ENQUEUED) != 0) 1379e8bcf696SMark Johnston return (0); 1380e8bcf696SMark Johnston vm_page_aflag_set(m, PGA_ENQUEUED); 1381f3f38e25SMark Johnston TAILQ_INSERT_BEFORE(marker, m, plinks.q); 13825cd29d0fSMark Johnston return (1); 13835cd29d0fSMark Johnston } 13845cd29d0fSMark Johnston 13855cd29d0fSMark Johnston /* 13865cd29d0fSMark Johnston * Re-add stuck pages to the inactive queue. We will examine them again 13875cd29d0fSMark Johnston * during the next scan. If the queue state of a page has changed since 13885cd29d0fSMark Johnston * it was physically removed from the page queue in 13895cd29d0fSMark Johnston * vm_pageout_collect_batch(), don't do anything with that page. 13905cd29d0fSMark Johnston */ 13915cd29d0fSMark Johnston static void 13925cd29d0fSMark Johnston vm_pageout_reinsert_inactive(struct scan_state *ss, struct vm_batchqueue *bq, 13935cd29d0fSMark Johnston vm_page_t m) 13945cd29d0fSMark Johnston { 13955cd29d0fSMark Johnston struct vm_pagequeue *pq; 1396f3f38e25SMark Johnston vm_page_t marker; 13975cd29d0fSMark Johnston int delta; 13985cd29d0fSMark Johnston 13995cd29d0fSMark Johnston delta = 0; 1400f3f38e25SMark Johnston marker = ss->marker; 14015cd29d0fSMark Johnston pq = ss->pq; 14025cd29d0fSMark Johnston 14035cd29d0fSMark Johnston if (m != NULL) { 14041cac76c9SAndrew Gallatin if (vm_batchqueue_insert(bq, m) != 0) 14055cd29d0fSMark Johnston return; 14065cd29d0fSMark Johnston vm_pagequeue_lock(pq); 1407f3f38e25SMark Johnston delta += vm_pageout_reinsert_inactive_page(pq, marker, m); 14085cd29d0fSMark Johnston } else 14095cd29d0fSMark Johnston vm_pagequeue_lock(pq); 14105cd29d0fSMark Johnston while ((m = vm_batchqueue_pop(bq)) != NULL) 1411f3f38e25SMark Johnston delta += vm_pageout_reinsert_inactive_page(pq, marker, m); 14125cd29d0fSMark Johnston vm_pagequeue_cnt_add(pq, delta); 14135cd29d0fSMark Johnston vm_pagequeue_unlock(pq); 14145cd29d0fSMark Johnston vm_batchqueue_init(bq); 14155cd29d0fSMark Johnston } 14165cd29d0fSMark Johnston 14170292c54bSConrad Meyer static void 14180292c54bSConrad Meyer vm_pageout_scan_inactive(struct vm_domain *vmd, int page_shortage) 1419df8bae1dSRodney W. Grimes { 14200292c54bSConrad Meyer struct timeval start, end; 14215cd29d0fSMark Johnston struct scan_state ss; 14225cd29d0fSMark Johnston struct vm_batchqueue rq; 14230292c54bSConrad Meyer struct vm_page marker_page; 14245cd29d0fSMark Johnston vm_page_t m, marker; 14258d220203SAlan Cox struct vm_pagequeue *pq; 1426df8bae1dSRodney W. Grimes vm_object_t object; 1427f3f38e25SMark Johnston vm_page_astate_t old, new; 14280292c54bSConrad Meyer int act_delta, addl_page_shortage, starting_page_shortage, refs; 14290292c54bSConrad Meyer 14300292c54bSConrad Meyer object = NULL; 14310292c54bSConrad Meyer vm_batchqueue_init(&rq); 14320292c54bSConrad Meyer getmicrouptime(&start); 14330d94caffSDavid Greenman 1434df8bae1dSRodney W. Grimes /* 143501f04471SMark Johnston * The addl_page_shortage is an estimate of the number of temporarily 1436311e34e2SKonstantin Belousov * stuck pages in the inactive queue. In other words, the 1437449c2e92SKonstantin Belousov * number of pages from the inactive count that should be 1438311e34e2SKonstantin Belousov * discounted in setting the target for the active queue scan. 1439311e34e2SKonstantin Belousov */ 14409099545aSAlan Cox addl_page_shortage = 0; 14419099545aSAlan Cox 14421c7c3c6aSMatthew Dillon /* 1443f095d1bbSAlan Cox * Start scanning the inactive queue for pages that we can free. The 1444f095d1bbSAlan Cox * scan will stop when we reach the target or we have scanned the 14455cff1f4dSMark Johnston * entire queue. (Note that m->a.act_count is not used to make 1446f095d1bbSAlan Cox * decisions for the inactive queue, only for the active queue.) 14478d220203SAlan Cox */ 14480292c54bSConrad Meyer starting_page_shortage = page_shortage; 14490292c54bSConrad Meyer marker = &marker_page; 14500292c54bSConrad Meyer vm_page_init_marker(marker, PQ_INACTIVE, 0); 14515cd29d0fSMark Johnston pq = &vmd->vmd_pagequeues[PQ_INACTIVE]; 14528d220203SAlan Cox vm_pagequeue_lock(pq); 14535cd29d0fSMark Johnston vm_pageout_init_scan(&ss, pq, marker, NULL, pq->pq_cnt); 1454a216e311SRyan Libby while (page_shortage > 0) { 1455a216e311SRyan Libby /* 1456a216e311SRyan Libby * If we need to refill the scan batch queue, release any 1457a216e311SRyan Libby * optimistically held object lock. This gives someone else a 1458a216e311SRyan Libby * chance to grab the lock, and also avoids holding it while we 1459a216e311SRyan Libby * do unrelated work. 1460a216e311SRyan Libby */ 1461a216e311SRyan Libby if (object != NULL && vm_batchqueue_empty(&ss.bq)) { 1462a216e311SRyan Libby VM_OBJECT_WUNLOCK(object); 1463a216e311SRyan Libby object = NULL; 1464a216e311SRyan Libby } 1465a216e311SRyan Libby 1466a216e311SRyan Libby m = vm_pageout_next(&ss, true); 1467a216e311SRyan Libby if (m == NULL) 1468a216e311SRyan Libby break; 14695cd29d0fSMark Johnston KASSERT((m->flags & PG_MARKER) == 0, 14705cd29d0fSMark Johnston ("marker page %p was dequeued", m)); 1471df8bae1dSRodney W. Grimes 1472936524aaSMatthew Dillon /* 1473b7f30bffSMark Johnston * Don't touch a page that was removed from the queue after the 1474b7f30bffSMark Johnston * page queue lock was released. Otherwise, ensure that any 1475b7f30bffSMark Johnston * pending queue operations, such as dequeues for wired pages, 1476b7f30bffSMark Johnston * are handled. 1477936524aaSMatthew Dillon */ 1478b7f30bffSMark Johnston if (vm_pageout_defer(m, PQ_INACTIVE, false)) 1479936524aaSMatthew Dillon continue; 1480e8bcf696SMark Johnston 14819f5632e6SMark Johnston /* 14829f5632e6SMark Johnston * Lock the page's object. 14839f5632e6SMark Johnston */ 14849f5632e6SMark Johnston if (object == NULL || object != m->object) { 148560256604SMark Johnston if (object != NULL) 14865cd29d0fSMark Johnston VM_OBJECT_WUNLOCK(object); 148723ed568cSMateusz Guzik object = atomic_load_ptr(&m->object); 14889f5632e6SMark Johnston if (__predict_false(object == NULL)) 14899f5632e6SMark Johnston /* The page is being freed by another thread. */ 14909f5632e6SMark Johnston continue; 14919f5632e6SMark Johnston 1492e8bcf696SMark Johnston /* Depends on type-stability. */ 149341fd4b94SMark Johnston VM_OBJECT_WLOCK(object); 14949f5632e6SMark Johnston if (__predict_false(m->object != object)) { 14959f5632e6SMark Johnston VM_OBJECT_WUNLOCK(object); 14969f5632e6SMark Johnston object = NULL; 14979f5632e6SMark Johnston goto reinsert; 149841fd4b94SMark Johnston } 149941fd4b94SMark Johnston } 15005cd29d0fSMark Johnston 150163e97555SJeff Roberson if (vm_page_tryxbusy(m) == 0) { 1502a3aeedabSAlan Cox /* 1503a3aeedabSAlan Cox * Don't mess with busy pages. Leave them at 1504a3aeedabSAlan Cox * the front of the queue. Most likely, they 1505a3aeedabSAlan Cox * are being paged out and will leave the 1506a3aeedabSAlan Cox * queue shortly after the scan finishes. So, 1507a3aeedabSAlan Cox * they ought to be discounted from the 1508a3aeedabSAlan Cox * inactive count. 1509a3aeedabSAlan Cox */ 1510a3aeedabSAlan Cox addl_page_shortage++; 15115cd29d0fSMark Johnston goto reinsert; 151226f9a767SRodney W. Grimes } 151348cc2fc7SKonstantin Belousov 1514a8081778SJeff Roberson /* Deferred free of swap space. */ 1515a8081778SJeff Roberson if ((m->a.flags & PGA_SWAP_FREE) != 0) 1516a8081778SJeff Roberson vm_pager_page_unswapped(m); 1517a8081778SJeff Roberson 151848cc2fc7SKonstantin Belousov /* 15199f5632e6SMark Johnston * Check for wirings now that we hold the object lock and have 15209f5632e6SMark Johnston * exclusively busied the page. If the page is mapped, it may 15219f5632e6SMark Johnston * still be wired by pmap lookups. The call to 1522fee2a2faSMark Johnston * vm_page_try_remove_all() below atomically checks for such 1523fee2a2faSMark Johnston * wirings and removes mappings. If the page is unmapped, the 15249f5632e6SMark Johnston * wire count is guaranteed not to increase after this check. 1525fee2a2faSMark Johnston */ 15269f5632e6SMark Johnston if (__predict_false(vm_page_wired(m))) 1527f3f38e25SMark Johnston goto skip_page; 1528fee2a2faSMark Johnston 1529fee2a2faSMark Johnston /* 15308748f58cSKonstantin Belousov * Invalid pages can be easily freed. They cannot be 15318748f58cSKonstantin Belousov * mapped, vm_page_free() asserts this. 1532776f729cSKonstantin Belousov */ 15330012f373SJeff Roberson if (vm_page_none_valid(m)) 15348748f58cSKonstantin Belousov goto free_page; 1535776f729cSKonstantin Belousov 1536b51927b7SKonstantin Belousov refs = object->ref_count != 0 ? pmap_ts_referenced(m) : 0; 1537f3f38e25SMark Johnston 1538f3f38e25SMark Johnston for (old = vm_page_astate_load(m);;) { 1539776f729cSKonstantin Belousov /* 1540f3f38e25SMark Johnston * Check to see if the page has been removed from the 1541f3f38e25SMark Johnston * queue since the first such check. Leave it alone if 1542f3f38e25SMark Johnston * so, discarding any references collected by 1543f3f38e25SMark Johnston * pmap_ts_referenced(). 15447e006499SJohn Dyson */ 1545f3f38e25SMark Johnston if (__predict_false(_vm_page_queue(old) == PQ_NONE)) 1546f3f38e25SMark Johnston goto skip_page; 1547f3f38e25SMark Johnston 1548f3f38e25SMark Johnston new = old; 1549f3f38e25SMark Johnston act_delta = refs; 1550f3f38e25SMark Johnston if ((old.flags & PGA_REFERENCED) != 0) { 1551f3f38e25SMark Johnston new.flags &= ~PGA_REFERENCED; 1552d7aeb429SAlan Cox act_delta++; 15532fe6e4d7SDavid Greenman } 1554f3f38e25SMark Johnston if (act_delta == 0) { 1555f3f38e25SMark Johnston ; 1556b51927b7SKonstantin Belousov } else if (object->ref_count != 0) { 1557e8bcf696SMark Johnston /* 1558f3f38e25SMark Johnston * Increase the activation count if the 1559f3f38e25SMark Johnston * page was referenced while in the 1560f3f38e25SMark Johnston * inactive queue. This makes it less 1561f3f38e25SMark Johnston * likely that the page will be returned 1562f3f38e25SMark Johnston * prematurely to the inactive queue. 1563e8bcf696SMark Johnston */ 1564f3f38e25SMark Johnston new.act_count += ACT_ADVANCE + 1565f3f38e25SMark Johnston act_delta; 1566f3f38e25SMark Johnston if (new.act_count > ACT_MAX) 1567f3f38e25SMark Johnston new.act_count = ACT_MAX; 1568f3f38e25SMark Johnston 1569f7607c30SMark Johnston new.flags &= ~PGA_QUEUE_OP_MASK; 1570f3f38e25SMark Johnston new.flags |= PGA_REQUEUE; 1571f3f38e25SMark Johnston new.queue = PQ_ACTIVE; 1572f3f38e25SMark Johnston if (!vm_page_pqstate_commit(m, &old, new)) 1573e8bcf696SMark Johnston continue; 1574f3f38e25SMark Johnston 1575f3f38e25SMark Johnston VM_CNT_INC(v_reactivated); 1576f3f38e25SMark Johnston goto skip_page; 1577ebcddc72SAlan Cox } else if ((object->flags & OBJ_DEAD) == 0) { 1578f3f38e25SMark Johnston new.queue = PQ_INACTIVE; 1579f3f38e25SMark Johnston new.flags |= PGA_REQUEUE; 1580f3f38e25SMark Johnston if (!vm_page_pqstate_commit(m, &old, new)) 1581f3f38e25SMark Johnston continue; 1582f3f38e25SMark Johnston goto skip_page; 1583ebcddc72SAlan Cox } 1584f3f38e25SMark Johnston break; 1585960810ccSAlan Cox } 158667bf6868SJohn Dyson 15877e006499SJohn Dyson /* 15889fc4739dSAlan Cox * If the page appears to be clean at the machine-independent 15899fc4739dSAlan Cox * layer, then remove all of its mappings from the pmap in 1590a766ffd0SAlan Cox * anticipation of freeing it. If, however, any of the page's 1591a766ffd0SAlan Cox * mappings allow write access, then the page may still be 1592a766ffd0SAlan Cox * modified until the last of those mappings are removed. 15937e006499SJohn Dyson */ 1594b51927b7SKonstantin Belousov if (object->ref_count != 0) { 15959fc4739dSAlan Cox vm_page_test_dirty(m); 15969f5632e6SMark Johnston if (m->dirty == 0 && !vm_page_try_remove_all(m)) 1597f3f38e25SMark Johnston goto skip_page; 1598fee2a2faSMark Johnston } 1599dcbcd518SBruce Evans 16006989c456SAlan Cox /* 1601ebcddc72SAlan Cox * Clean pages can be freed, but dirty pages must be sent back 1602ebcddc72SAlan Cox * to the laundry, unless they belong to a dead object. 1603ebcddc72SAlan Cox * Requeueing dirty pages from dead objects is pointless, as 1604ebcddc72SAlan Cox * they are being paged out and freed by the thread that 1605ebcddc72SAlan Cox * destroyed the object. 16066989c456SAlan Cox */ 1607ebcddc72SAlan Cox if (m->dirty == 0) { 16088748f58cSKonstantin Belousov free_page: 16095cd29d0fSMark Johnston /* 16109f5632e6SMark Johnston * Now we are guaranteed that no other threads are 16119f5632e6SMark Johnston * manipulating the page, check for a last-second 16129f5632e6SMark Johnston * reference that would save it from doom. 16135cd29d0fSMark Johnston */ 16149f5632e6SMark Johnston if (vm_pageout_defer(m, PQ_INACTIVE, false)) 16159f5632e6SMark Johnston goto skip_page; 16169f5632e6SMark Johnston 16179f5632e6SMark Johnston /* 16189f5632e6SMark Johnston * Because we dequeued the page and have already checked 16199f5632e6SMark Johnston * for pending dequeue and enqueue requests, we can 16209f5632e6SMark Johnston * safely disassociate the page from the inactive queue 16219f5632e6SMark Johnston * without holding the queue lock. 16229f5632e6SMark Johnston */ 16235cff1f4dSMark Johnston m->a.queue = PQ_NONE; 162478afdce6SAlan Cox vm_page_free(m); 16255cd29d0fSMark Johnston page_shortage--; 162663e97555SJeff Roberson continue; 162763e97555SJeff Roberson } 162863e97555SJeff Roberson if ((object->flags & OBJ_DEAD) == 0) 1629ebcddc72SAlan Cox vm_page_launder(m); 1630f3f38e25SMark Johnston skip_page: 1631f3f38e25SMark Johnston vm_page_xunbusy(m); 16325cd29d0fSMark Johnston continue; 16335cd29d0fSMark Johnston reinsert: 16345cd29d0fSMark Johnston vm_pageout_reinsert_inactive(&ss, &rq, m); 16355cd29d0fSMark Johnston } 163660256604SMark Johnston if (object != NULL) 163789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 16385cd29d0fSMark Johnston vm_pageout_reinsert_inactive(&ss, &rq, NULL); 16395cd29d0fSMark Johnston vm_pageout_reinsert_inactive(&ss, &ss.bq, NULL); 16408d220203SAlan Cox vm_pagequeue_lock(pq); 16415cd29d0fSMark Johnston vm_pageout_end_scan(&ss); 16428d220203SAlan Cox vm_pagequeue_unlock(pq); 164326f9a767SRodney W. Grimes 16440292c54bSConrad Meyer /* 16450292c54bSConrad Meyer * Record the remaining shortage and the progress and rate it was made. 16460292c54bSConrad Meyer */ 16470292c54bSConrad Meyer atomic_add_int(&vmd->vmd_addl_shortage, addl_page_shortage); 16480292c54bSConrad Meyer getmicrouptime(&end); 16490292c54bSConrad Meyer timevalsub(&end, &start); 16500292c54bSConrad Meyer atomic_add_int(&vmd->vmd_inactive_us, 16510292c54bSConrad Meyer end.tv_sec * 1000000 + end.tv_usec); 16520292c54bSConrad Meyer atomic_add_int(&vmd->vmd_inactive_freed, 16530292c54bSConrad Meyer starting_page_shortage - page_shortage); 16540292c54bSConrad Meyer } 16550292c54bSConrad Meyer 16560292c54bSConrad Meyer /* 16570292c54bSConrad Meyer * Dispatch a number of inactive threads according to load and collect the 16582913cc46SMark Johnston * results to present a coherent view of paging activity on this domain. 16590292c54bSConrad Meyer */ 16600292c54bSConrad Meyer static int 16610292c54bSConrad Meyer vm_pageout_inactive_dispatch(struct vm_domain *vmd, int shortage) 16620292c54bSConrad Meyer { 16632913cc46SMark Johnston u_int freed, pps, slop, threads, us; 16640292c54bSConrad Meyer 16650292c54bSConrad Meyer vmd->vmd_inactive_shortage = shortage; 16662913cc46SMark Johnston slop = 0; 16670292c54bSConrad Meyer 16680292c54bSConrad Meyer /* 16690292c54bSConrad Meyer * If we have more work than we can do in a quarter of our interval, we 16700292c54bSConrad Meyer * fire off multiple threads to process it. 16710292c54bSConrad Meyer */ 16720292c54bSConrad Meyer threads = vmd->vmd_inactive_threads; 16732913cc46SMark Johnston if (threads > 1 && vmd->vmd_inactive_pps != 0 && 16742913cc46SMark Johnston shortage > vmd->vmd_inactive_pps / VM_INACT_SCAN_RATE / 4) { 16750292c54bSConrad Meyer vmd->vmd_inactive_shortage /= threads; 16762913cc46SMark Johnston slop = shortage % threads; 16772913cc46SMark Johnston vm_domain_pageout_lock(vmd); 16780292c54bSConrad Meyer blockcount_acquire(&vmd->vmd_inactive_starting, threads - 1); 16790292c54bSConrad Meyer blockcount_acquire(&vmd->vmd_inactive_running, threads - 1); 16800292c54bSConrad Meyer wakeup(&vmd->vmd_inactive_shortage); 16810292c54bSConrad Meyer vm_domain_pageout_unlock(vmd); 16820292c54bSConrad Meyer } 16830292c54bSConrad Meyer 16840292c54bSConrad Meyer /* Run the local thread scan. */ 16852913cc46SMark Johnston vm_pageout_scan_inactive(vmd, vmd->vmd_inactive_shortage + slop); 16860292c54bSConrad Meyer 16870292c54bSConrad Meyer /* 16880292c54bSConrad Meyer * Block until helper threads report results and then accumulate 16890292c54bSConrad Meyer * totals. 16900292c54bSConrad Meyer */ 16910292c54bSConrad Meyer blockcount_wait(&vmd->vmd_inactive_running, NULL, "vmpoid", PVM); 16920292c54bSConrad Meyer freed = atomic_readandclear_int(&vmd->vmd_inactive_freed); 16930292c54bSConrad Meyer VM_CNT_ADD(v_dfree, freed); 16940292c54bSConrad Meyer 16950292c54bSConrad Meyer /* 16960292c54bSConrad Meyer * Calculate the per-thread paging rate with an exponential decay of 16970292c54bSConrad Meyer * prior results. Careful to avoid integer rounding errors with large 16980292c54bSConrad Meyer * us values. 16990292c54bSConrad Meyer */ 17000292c54bSConrad Meyer us = max(atomic_readandclear_int(&vmd->vmd_inactive_us), 1); 17010292c54bSConrad Meyer if (us > 1000000) 17020292c54bSConrad Meyer /* Keep rounding to tenths */ 17030292c54bSConrad Meyer pps = (freed * 10) / ((us * 10) / 1000000); 17040292c54bSConrad Meyer else 17050292c54bSConrad Meyer pps = (1000000 / us) * freed; 17060292c54bSConrad Meyer vmd->vmd_inactive_pps = (vmd->vmd_inactive_pps / 2) + (pps / 2); 17070292c54bSConrad Meyer 17080292c54bSConrad Meyer return (shortage - freed); 17090292c54bSConrad Meyer } 17100292c54bSConrad Meyer 17110292c54bSConrad Meyer /* 17120292c54bSConrad Meyer * Attempt to reclaim the requested number of pages from the inactive queue. 17130292c54bSConrad Meyer * Returns true if the shortage was addressed. 17140292c54bSConrad Meyer */ 17150292c54bSConrad Meyer static int 17160292c54bSConrad Meyer vm_pageout_inactive(struct vm_domain *vmd, int shortage, int *addl_shortage) 17170292c54bSConrad Meyer { 17180292c54bSConrad Meyer struct vm_pagequeue *pq; 17190292c54bSConrad Meyer u_int addl_page_shortage, deficit, page_shortage; 17200292c54bSConrad Meyer u_int starting_page_shortage; 17210292c54bSConrad Meyer 17220292c54bSConrad Meyer /* 17230292c54bSConrad Meyer * vmd_pageout_deficit counts the number of pages requested in 17240292c54bSConrad Meyer * allocations that failed because of a free page shortage. We assume 17250292c54bSConrad Meyer * that the allocations will be reattempted and thus include the deficit 17260292c54bSConrad Meyer * in our scan target. 17270292c54bSConrad Meyer */ 17280292c54bSConrad Meyer deficit = atomic_readandclear_int(&vmd->vmd_pageout_deficit); 17290292c54bSConrad Meyer starting_page_shortage = shortage + deficit; 17300292c54bSConrad Meyer 17310292c54bSConrad Meyer /* 17320292c54bSConrad Meyer * Run the inactive scan on as many threads as is necessary. 17330292c54bSConrad Meyer */ 17340292c54bSConrad Meyer page_shortage = vm_pageout_inactive_dispatch(vmd, starting_page_shortage); 17350292c54bSConrad Meyer addl_page_shortage = atomic_readandclear_int(&vmd->vmd_addl_shortage); 17365cd29d0fSMark Johnston 1737ebcddc72SAlan Cox /* 1738ebcddc72SAlan Cox * Wake up the laundry thread so that it can perform any needed 1739ebcddc72SAlan Cox * laundering. If we didn't meet our target, we're in shortfall and 1740b1fd102eSMark Johnston * need to launder more aggressively. If PQ_LAUNDRY is empty and no 1741b1fd102eSMark Johnston * swap devices are configured, the laundry thread has no work to do, so 1742b1fd102eSMark Johnston * don't bother waking it up. 1743cb35676eSMark Johnston * 1744cb35676eSMark Johnston * The laundry thread uses the number of inactive queue scans elapsed 1745cb35676eSMark Johnston * since the last laundering to determine whether to launder again, so 1746cb35676eSMark Johnston * keep count. 1747ebcddc72SAlan Cox */ 1748cb35676eSMark Johnston if (starting_page_shortage > 0) { 1749e2068d0bSJeff Roberson pq = &vmd->vmd_pagequeues[PQ_LAUNDRY]; 1750ebcddc72SAlan Cox vm_pagequeue_lock(pq); 1751e2068d0bSJeff Roberson if (vmd->vmd_laundry_request == VM_LAUNDRY_IDLE && 1752cb35676eSMark Johnston (pq->pq_cnt > 0 || atomic_load_acq_int(&swapdev_enabled))) { 1753ebcddc72SAlan Cox if (page_shortage > 0) { 1754e2068d0bSJeff Roberson vmd->vmd_laundry_request = VM_LAUNDRY_SHORTFALL; 175583c9dea1SGleb Smirnoff VM_CNT_INC(v_pdshortfalls); 1756e2068d0bSJeff Roberson } else if (vmd->vmd_laundry_request != 1757e2068d0bSJeff Roberson VM_LAUNDRY_SHORTFALL) 1758e2068d0bSJeff Roberson vmd->vmd_laundry_request = 1759e2068d0bSJeff Roberson VM_LAUNDRY_BACKGROUND; 1760e2068d0bSJeff Roberson wakeup(&vmd->vmd_laundry_request); 1761b1fd102eSMark Johnston } 176260684862SMark Johnston vmd->vmd_clean_pages_freed += 176360684862SMark Johnston starting_page_shortage - page_shortage; 1764ebcddc72SAlan Cox vm_pagequeue_unlock(pq); 1765ebcddc72SAlan Cox } 1766ebcddc72SAlan Cox 17679452b5edSAlan Cox /* 176876386c7eSKonstantin Belousov * If the inactive queue scan fails repeatedly to meet its 176976386c7eSKonstantin Belousov * target, kill the largest process. 177076386c7eSKonstantin Belousov */ 177176386c7eSKonstantin Belousov vm_pageout_mightbe_oom(vmd, page_shortage, starting_page_shortage); 177276386c7eSKonstantin Belousov 177376386c7eSKonstantin Belousov /* 1774be37ee79SMark Johnston * See the description of addl_page_shortage above. 1775be37ee79SMark Johnston */ 1776be37ee79SMark Johnston *addl_shortage = addl_page_shortage + deficit; 1777be37ee79SMark Johnston 1778e57dd910SAlan Cox return (page_shortage <= 0); 17792025d69bSKonstantin Belousov } 17802025d69bSKonstantin Belousov 1781449c2e92SKonstantin Belousov static int vm_pageout_oom_vote; 1782449c2e92SKonstantin Belousov 1783449c2e92SKonstantin Belousov /* 1784449c2e92SKonstantin Belousov * The pagedaemon threads randlomly select one to perform the 1785449c2e92SKonstantin Belousov * OOM. Trying to kill processes before all pagedaemons 1786449c2e92SKonstantin Belousov * failed to reach free target is premature. 1787449c2e92SKonstantin Belousov */ 1788449c2e92SKonstantin Belousov static void 178976386c7eSKonstantin Belousov vm_pageout_mightbe_oom(struct vm_domain *vmd, int page_shortage, 179076386c7eSKonstantin Belousov int starting_page_shortage) 1791449c2e92SKonstantin Belousov { 1792449c2e92SKonstantin Belousov int old_vote; 1793449c2e92SKonstantin Belousov 179476386c7eSKonstantin Belousov if (starting_page_shortage <= 0 || starting_page_shortage != 179576386c7eSKonstantin Belousov page_shortage) 179676386c7eSKonstantin Belousov vmd->vmd_oom_seq = 0; 179776386c7eSKonstantin Belousov else 179876386c7eSKonstantin Belousov vmd->vmd_oom_seq++; 179976386c7eSKonstantin Belousov if (vmd->vmd_oom_seq < vm_pageout_oom_seq) { 1800449c2e92SKonstantin Belousov if (vmd->vmd_oom) { 1801449c2e92SKonstantin Belousov vmd->vmd_oom = FALSE; 1802449c2e92SKonstantin Belousov atomic_subtract_int(&vm_pageout_oom_vote, 1); 1803449c2e92SKonstantin Belousov } 1804449c2e92SKonstantin Belousov return; 1805449c2e92SKonstantin Belousov } 1806449c2e92SKonstantin Belousov 180776386c7eSKonstantin Belousov /* 180876386c7eSKonstantin Belousov * Do not follow the call sequence until OOM condition is 180976386c7eSKonstantin Belousov * cleared. 181076386c7eSKonstantin Belousov */ 181176386c7eSKonstantin Belousov vmd->vmd_oom_seq = 0; 181276386c7eSKonstantin Belousov 1813449c2e92SKonstantin Belousov if (vmd->vmd_oom) 1814449c2e92SKonstantin Belousov return; 1815449c2e92SKonstantin Belousov 1816449c2e92SKonstantin Belousov vmd->vmd_oom = TRUE; 1817449c2e92SKonstantin Belousov old_vote = atomic_fetchadd_int(&vm_pageout_oom_vote, 1); 1818449c2e92SKonstantin Belousov if (old_vote != vm_ndomains - 1) 1819449c2e92SKonstantin Belousov return; 1820449c2e92SKonstantin Belousov 1821449c2e92SKonstantin Belousov /* 1822449c2e92SKonstantin Belousov * The current pagedaemon thread is the last in the quorum to 1823449c2e92SKonstantin Belousov * start OOM. Initiate the selection and signaling of the 1824449c2e92SKonstantin Belousov * victim. 1825449c2e92SKonstantin Belousov */ 1826449c2e92SKonstantin Belousov vm_pageout_oom(VM_OOM_MEM); 1827449c2e92SKonstantin Belousov 1828449c2e92SKonstantin Belousov /* 1829449c2e92SKonstantin Belousov * After one round of OOM terror, recall our vote. On the 1830449c2e92SKonstantin Belousov * next pass, current pagedaemon would vote again if the low 1831449c2e92SKonstantin Belousov * memory condition is still there, due to vmd_oom being 1832449c2e92SKonstantin Belousov * false. 1833449c2e92SKonstantin Belousov */ 1834449c2e92SKonstantin Belousov vmd->vmd_oom = FALSE; 1835449c2e92SKonstantin Belousov atomic_subtract_int(&vm_pageout_oom_vote, 1); 1836449c2e92SKonstantin Belousov } 18372025d69bSKonstantin Belousov 18383949873fSKonstantin Belousov /* 18393949873fSKonstantin Belousov * The OOM killer is the page daemon's action of last resort when 18403949873fSKonstantin Belousov * memory allocation requests have been stalled for a prolonged period 18413949873fSKonstantin Belousov * of time because it cannot reclaim memory. This function computes 18423949873fSKonstantin Belousov * the approximate number of physical pages that could be reclaimed if 18433949873fSKonstantin Belousov * the specified address space is destroyed. 18443949873fSKonstantin Belousov * 18453949873fSKonstantin Belousov * Private, anonymous memory owned by the address space is the 18463949873fSKonstantin Belousov * principal resource that we expect to recover after an OOM kill. 18473949873fSKonstantin Belousov * Since the physical pages mapped by the address space's COW entries 18483949873fSKonstantin Belousov * are typically shared pages, they are unlikely to be released and so 18493949873fSKonstantin Belousov * they are not counted. 18503949873fSKonstantin Belousov * 18513949873fSKonstantin Belousov * To get to the point where the page daemon runs the OOM killer, its 18523949873fSKonstantin Belousov * efforts to write-back vnode-backed pages may have stalled. This 18533949873fSKonstantin Belousov * could be caused by a memory allocation deadlock in the write path 18543949873fSKonstantin Belousov * that might be resolved by an OOM kill. Therefore, physical pages 18553949873fSKonstantin Belousov * belonging to vnode-backed objects are counted, because they might 18563949873fSKonstantin Belousov * be freed without being written out first if the address space holds 18573949873fSKonstantin Belousov * the last reference to an unlinked vnode. 18583949873fSKonstantin Belousov * 18593949873fSKonstantin Belousov * Similarly, physical pages belonging to OBJT_PHYS objects are 18603949873fSKonstantin Belousov * counted because the address space might hold the last reference to 18613949873fSKonstantin Belousov * the object. 18623949873fSKonstantin Belousov */ 18633949873fSKonstantin Belousov static long 18643949873fSKonstantin Belousov vm_pageout_oom_pagecount(struct vmspace *vmspace) 18653949873fSKonstantin Belousov { 18663949873fSKonstantin Belousov vm_map_t map; 18673949873fSKonstantin Belousov vm_map_entry_t entry; 18683949873fSKonstantin Belousov vm_object_t obj; 18693949873fSKonstantin Belousov long res; 18703949873fSKonstantin Belousov 18713949873fSKonstantin Belousov map = &vmspace->vm_map; 18723949873fSKonstantin Belousov KASSERT(!map->system_map, ("system map")); 18733949873fSKonstantin Belousov sx_assert(&map->lock, SA_LOCKED); 18743949873fSKonstantin Belousov res = 0; 18752288078cSDoug Moore VM_MAP_ENTRY_FOREACH(entry, map) { 18763949873fSKonstantin Belousov if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0) 18773949873fSKonstantin Belousov continue; 18783949873fSKonstantin Belousov obj = entry->object.vm_object; 18793949873fSKonstantin Belousov if (obj == NULL) 18803949873fSKonstantin Belousov continue; 18813949873fSKonstantin Belousov if ((entry->eflags & MAP_ENTRY_NEEDS_COPY) != 0 && 18823949873fSKonstantin Belousov obj->ref_count != 1) 18833949873fSKonstantin Belousov continue; 18840cb2610eSMark Johnston if (obj->type == OBJT_PHYS || obj->type == OBJT_VNODE || 1885e123264eSMark Johnston (obj->flags & OBJ_SWAP) != 0) 18863949873fSKonstantin Belousov res += obj->resident_page_count; 18873949873fSKonstantin Belousov } 18883949873fSKonstantin Belousov return (res); 18893949873fSKonstantin Belousov } 18903949873fSKonstantin Belousov 1891245139c6SKonstantin Belousov static int vm_oom_ratelim_last; 1892245139c6SKonstantin Belousov static int vm_oom_pf_secs = 10; 1893245139c6SKonstantin Belousov SYSCTL_INT(_vm, OID_AUTO, oom_pf_secs, CTLFLAG_RWTUN, &vm_oom_pf_secs, 0, 1894245139c6SKonstantin Belousov ""); 1895245139c6SKonstantin Belousov static struct mtx vm_oom_ratelim_mtx; 1896245139c6SKonstantin Belousov 18972025d69bSKonstantin Belousov void 18982025d69bSKonstantin Belousov vm_pageout_oom(int shortage) 18992025d69bSKonstantin Belousov { 19004a864f62SMark Johnston const char *reason; 19012025d69bSKonstantin Belousov struct proc *p, *bigproc; 19022025d69bSKonstantin Belousov vm_offset_t size, bigsize; 19032025d69bSKonstantin Belousov struct thread *td; 19046bed074cSKonstantin Belousov struct vmspace *vm; 1905245139c6SKonstantin Belousov int now; 19063e78e983SAlan Cox bool breakout; 19072025d69bSKonstantin Belousov 19082025d69bSKonstantin Belousov /* 1909245139c6SKonstantin Belousov * For OOM requests originating from vm_fault(), there is a high 1910245139c6SKonstantin Belousov * chance that a single large process faults simultaneously in 1911245139c6SKonstantin Belousov * several threads. Also, on an active system running many 1912245139c6SKonstantin Belousov * processes of middle-size, like buildworld, all of them 1913245139c6SKonstantin Belousov * could fault almost simultaneously as well. 1914245139c6SKonstantin Belousov * 1915245139c6SKonstantin Belousov * To avoid killing too many processes, rate-limit OOMs 1916245139c6SKonstantin Belousov * initiated by vm_fault() time-outs on the waits for free 1917245139c6SKonstantin Belousov * pages. 1918245139c6SKonstantin Belousov */ 1919245139c6SKonstantin Belousov mtx_lock(&vm_oom_ratelim_mtx); 1920245139c6SKonstantin Belousov now = ticks; 1921245139c6SKonstantin Belousov if (shortage == VM_OOM_MEM_PF && 1922245139c6SKonstantin Belousov (u_int)(now - vm_oom_ratelim_last) < hz * vm_oom_pf_secs) { 1923245139c6SKonstantin Belousov mtx_unlock(&vm_oom_ratelim_mtx); 1924245139c6SKonstantin Belousov return; 1925245139c6SKonstantin Belousov } 1926245139c6SKonstantin Belousov vm_oom_ratelim_last = now; 1927245139c6SKonstantin Belousov mtx_unlock(&vm_oom_ratelim_mtx); 1928245139c6SKonstantin Belousov 1929245139c6SKonstantin Belousov /* 19301c58e4e5SJohn Baldwin * We keep the process bigproc locked once we find it to keep anyone 19311c58e4e5SJohn Baldwin * from messing with it; however, there is a possibility of 193228323addSBryan Drewery * deadlock if process B is bigproc and one of its child processes 19331c58e4e5SJohn Baldwin * attempts to propagate a signal to B while we are waiting for A's 19341c58e4e5SJohn Baldwin * lock while walking this list. To avoid this, we don't block on 19351c58e4e5SJohn Baldwin * the process lock but just skip a process if it is already locked. 19365663e6deSDavid Greenman */ 19375663e6deSDavid Greenman bigproc = NULL; 19385663e6deSDavid Greenman bigsize = 0; 19391005a129SJohn Baldwin sx_slock(&allproc_lock); 1940e602ba25SJulian Elischer FOREACH_PROC_IN_SYSTEM(p) { 194171943c3dSKonstantin Belousov PROC_LOCK(p); 194271943c3dSKonstantin Belousov 19431c58e4e5SJohn Baldwin /* 19443f1c4c4fSKonstantin Belousov * If this is a system, protected or killed process, skip it. 19455663e6deSDavid Greenman */ 194671943c3dSKonstantin Belousov if (p->p_state != PRS_NORMAL || (p->p_flag & (P_INEXEC | 194771943c3dSKonstantin Belousov P_PROTECTED | P_SYSTEM | P_WEXIT)) != 0 || 194871943c3dSKonstantin Belousov p->p_pid == 1 || P_KILLED(p) || 194971943c3dSKonstantin Belousov (p->p_pid < 48 && swap_pager_avail != 0)) { 19508606d880SJohn Baldwin PROC_UNLOCK(p); 19515663e6deSDavid Greenman continue; 19525663e6deSDavid Greenman } 19535663e6deSDavid Greenman /* 1954dcbcd518SBruce Evans * If the process is in a non-running type state, 1955e602ba25SJulian Elischer * don't touch it. Check all the threads individually. 19565663e6deSDavid Greenman */ 19573e78e983SAlan Cox breakout = false; 1958e602ba25SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 1959982d11f8SJeff Roberson thread_lock(td); 196071fad9fdSJulian Elischer if (!TD_ON_RUNQ(td) && 196171fad9fdSJulian Elischer !TD_IS_RUNNING(td) && 1962f497cda2SEdward Tomasz Napierala !TD_IS_SLEEPING(td) && 1963*e24a6552SMark Johnston !TD_IS_SUSPENDED(td)) { 1964982d11f8SJeff Roberson thread_unlock(td); 19653e78e983SAlan Cox breakout = true; 1966e602ba25SJulian Elischer break; 1967e602ba25SJulian Elischer } 1968982d11f8SJeff Roberson thread_unlock(td); 1969e602ba25SJulian Elischer } 1970e602ba25SJulian Elischer if (breakout) { 19711c58e4e5SJohn Baldwin PROC_UNLOCK(p); 19725663e6deSDavid Greenman continue; 19735663e6deSDavid Greenman } 19745663e6deSDavid Greenman /* 19755663e6deSDavid Greenman * get the process size 19765663e6deSDavid Greenman */ 19776bed074cSKonstantin Belousov vm = vmspace_acquire_ref(p); 19786bed074cSKonstantin Belousov if (vm == NULL) { 19796bed074cSKonstantin Belousov PROC_UNLOCK(p); 19806bed074cSKonstantin Belousov continue; 19816bed074cSKonstantin Belousov } 19828370e9dfSMark Johnston _PHOLD(p); 198372d97679SDavid Schultz PROC_UNLOCK(p); 198495e2409aSKonstantin Belousov sx_sunlock(&allproc_lock); 198595e2409aSKonstantin Belousov if (!vm_map_trylock_read(&vm->vm_map)) { 198671943c3dSKonstantin Belousov vmspace_free(vm); 198795e2409aSKonstantin Belousov sx_slock(&allproc_lock); 198895e2409aSKonstantin Belousov PRELE(p); 198972d97679SDavid Schultz continue; 199072d97679SDavid Schultz } 19917981aa24SKonstantin Belousov size = vmspace_swap_count(vm); 1992245139c6SKonstantin Belousov if (shortage == VM_OOM_MEM || shortage == VM_OOM_MEM_PF) 19933949873fSKonstantin Belousov size += vm_pageout_oom_pagecount(vm); 19943949873fSKonstantin Belousov vm_map_unlock_read(&vm->vm_map); 19956bed074cSKonstantin Belousov vmspace_free(vm); 199695e2409aSKonstantin Belousov sx_slock(&allproc_lock); 19973949873fSKonstantin Belousov 19985663e6deSDavid Greenman /* 19993949873fSKonstantin Belousov * If this process is bigger than the biggest one, 20005663e6deSDavid Greenman * remember it. 20015663e6deSDavid Greenman */ 20025663e6deSDavid Greenman if (size > bigsize) { 20031c58e4e5SJohn Baldwin if (bigproc != NULL) 200471943c3dSKonstantin Belousov PRELE(bigproc); 20055663e6deSDavid Greenman bigproc = p; 20065663e6deSDavid Greenman bigsize = size; 200771943c3dSKonstantin Belousov } else { 200871943c3dSKonstantin Belousov PRELE(p); 200971943c3dSKonstantin Belousov } 20105663e6deSDavid Greenman } 20111005a129SJohn Baldwin sx_sunlock(&allproc_lock); 20124a864f62SMark Johnston 20135663e6deSDavid Greenman if (bigproc != NULL) { 20144a864f62SMark Johnston switch (shortage) { 20154a864f62SMark Johnston case VM_OOM_MEM: 20164a864f62SMark Johnston reason = "failed to reclaim memory"; 20174a864f62SMark Johnston break; 20184a864f62SMark Johnston case VM_OOM_MEM_PF: 20194a864f62SMark Johnston reason = "a thread waited too long to allocate a page"; 20204a864f62SMark Johnston break; 20214a864f62SMark Johnston case VM_OOM_SWAPZ: 20224a864f62SMark Johnston reason = "out of swap space"; 20234a864f62SMark Johnston break; 20244a864f62SMark Johnston default: 20254a864f62SMark Johnston panic("unknown OOM reason %d", shortage); 20264a864f62SMark Johnston } 20273c200db9SJonathan T. Looney if (vm_panic_on_oom != 0 && --vm_panic_on_oom == 0) 20284a864f62SMark Johnston panic("%s", reason); 202971943c3dSKonstantin Belousov PROC_LOCK(bigproc); 20304a864f62SMark Johnston killproc(bigproc, reason); 2031fa885116SJulian Elischer sched_nice(bigproc, PRIO_MIN); 203271943c3dSKonstantin Belousov _PRELE(bigproc); 20331c58e4e5SJohn Baldwin PROC_UNLOCK(bigproc); 20345663e6deSDavid Greenman } 20355663e6deSDavid Greenman } 203626f9a767SRodney W. Grimes 20378fc25508SMark Johnston /* 20388fc25508SMark Johnston * Signal a free page shortage to subsystems that have registered an event 20398fc25508SMark Johnston * handler. Reclaim memory from UMA in the event of a severe shortage. 20408fc25508SMark Johnston * Return true if the free page count should be re-evaluated. 20418fc25508SMark Johnston */ 2042b50a4ea6SMark Johnston static bool 2043b50a4ea6SMark Johnston vm_pageout_lowmem(void) 204449a3710cSMark Johnston { 2045b50a4ea6SMark Johnston static int lowmem_ticks = 0; 2046b50a4ea6SMark Johnston int last; 20478fc25508SMark Johnston bool ret; 20488fc25508SMark Johnston 20498fc25508SMark Johnston ret = false; 205049a3710cSMark Johnston 2051b50a4ea6SMark Johnston last = atomic_load_int(&lowmem_ticks); 2052b50a4ea6SMark Johnston while ((u_int)(ticks - last) / hz >= lowmem_period) { 2053b50a4ea6SMark Johnston if (atomic_fcmpset_int(&lowmem_ticks, &last, ticks) == 0) 2054b50a4ea6SMark Johnston continue; 2055b50a4ea6SMark Johnston 205649a3710cSMark Johnston /* 205749a3710cSMark Johnston * Decrease registered cache sizes. 205849a3710cSMark Johnston */ 205949a3710cSMark Johnston SDT_PROBE0(vm, , , vm__lowmem_scan); 206049a3710cSMark Johnston EVENTHANDLER_INVOKE(vm_lowmem, VM_LOW_PAGES); 206149a3710cSMark Johnston 206249a3710cSMark Johnston /* 206349a3710cSMark Johnston * We do this explicitly after the caches have been 20648fc25508SMark Johnston * drained above. 206549a3710cSMark Johnston */ 20668fc25508SMark Johnston uma_reclaim(UMA_RECLAIM_TRIM); 20678fc25508SMark Johnston ret = true; 2068ace409ceSAlexander Motin break; 206949a3710cSMark Johnston } 20708fc25508SMark Johnston 20718fc25508SMark Johnston /* 20728fc25508SMark Johnston * Kick off an asynchronous reclaim of cached memory if one of the 20738fc25508SMark Johnston * page daemons is failing to keep up with demand. Use the "severe" 20748fc25508SMark Johnston * threshold instead of "min" to ensure that we do not blow away the 20758fc25508SMark Johnston * caches if a subset of the NUMA domains are depleted by kernel memory 20768fc25508SMark Johnston * allocations; the domainset iterators automatically skip domains 20778fc25508SMark Johnston * below the "min" threshold on the first pass. 20788fc25508SMark Johnston * 20798fc25508SMark Johnston * UMA reclaim worker has its own rate-limiting mechanism, so don't 20808fc25508SMark Johnston * worry about kicking it too often. 20818fc25508SMark Johnston */ 20828fc25508SMark Johnston if (vm_page_count_severe()) 20838fc25508SMark Johnston uma_reclaim_wakeup(); 20848fc25508SMark Johnston 20858fc25508SMark Johnston return (ret); 208649a3710cSMark Johnston } 208749a3710cSMark Johnston 208849a3710cSMark Johnston static void 2089449c2e92SKonstantin Belousov vm_pageout_worker(void *arg) 2090449c2e92SKonstantin Belousov { 2091e2068d0bSJeff Roberson struct vm_domain *vmd; 2092b50a4ea6SMark Johnston u_int ofree; 209349a3710cSMark Johnston int addl_shortage, domain, shortage; 2094e57dd910SAlan Cox bool target_met; 2095449c2e92SKonstantin Belousov 2096e2068d0bSJeff Roberson domain = (uintptr_t)arg; 2097e2068d0bSJeff Roberson vmd = VM_DOMAIN(domain); 20985f8cd1c0SJeff Roberson shortage = 0; 2099e57dd910SAlan Cox target_met = true; 2100449c2e92SKonstantin Belousov 2101449c2e92SKonstantin Belousov /* 2102949c9186SKonstantin Belousov * XXXKIB It could be useful to bind pageout daemon threads to 2103949c9186SKonstantin Belousov * the cores belonging to the domain, from which vm_page_array 2104949c9186SKonstantin Belousov * is allocated. 2105449c2e92SKonstantin Belousov */ 2106449c2e92SKonstantin Belousov 2107e2068d0bSJeff Roberson KASSERT(vmd->vmd_segs != 0, ("domain without segments")); 2108e2068d0bSJeff Roberson vmd->vmd_last_active_scan = ticks; 2109449c2e92SKonstantin Belousov 2110449c2e92SKonstantin Belousov /* 2111449c2e92SKonstantin Belousov * The pageout daemon worker is never done, so loop forever. 2112449c2e92SKonstantin Belousov */ 2113449c2e92SKonstantin Belousov while (TRUE) { 211430fbfddaSJeff Roberson vm_domain_pageout_lock(vmd); 211549a3710cSMark Johnston 211630fbfddaSJeff Roberson /* 211730fbfddaSJeff Roberson * We need to clear wanted before we check the limits. This 211830fbfddaSJeff Roberson * prevents races with wakers who will check wanted after they 211930fbfddaSJeff Roberson * reach the limit. 212030fbfddaSJeff Roberson */ 212130fbfddaSJeff Roberson atomic_store_int(&vmd->vmd_pageout_wanted, 0); 212256ce0690SAlan Cox 212356ce0690SAlan Cox /* 21245f8cd1c0SJeff Roberson * Might the page daemon need to run again? 2125449c2e92SKonstantin Belousov */ 21265f8cd1c0SJeff Roberson if (vm_paging_needed(vmd, vmd->vmd_free_count)) { 212756ce0690SAlan Cox /* 212849a3710cSMark Johnston * Yes. If the scan failed to produce enough free 212949a3710cSMark Johnston * pages, sleep uninterruptibly for some time in the 213049a3710cSMark Johnston * hope that the laundry thread will clean some pages. 213156ce0690SAlan Cox */ 213230fbfddaSJeff Roberson vm_domain_pageout_unlock(vmd); 213349a3710cSMark Johnston if (!target_met) 21346eebec83SMark Johnston pause("pwait", hz / VM_INACT_SCAN_RATE); 2135449c2e92SKonstantin Belousov } else { 2136449c2e92SKonstantin Belousov /* 21375f8cd1c0SJeff Roberson * No, sleep until the next wakeup or until pages 21385f8cd1c0SJeff Roberson * need to have their reference stats updated. 2139449c2e92SKonstantin Belousov */ 21402c0f13aaSKonstantin Belousov if (mtx_sleep(&vmd->vmd_pageout_wanted, 214130fbfddaSJeff Roberson vm_domain_pageout_lockptr(vmd), PDROP | PVM, 21425f8cd1c0SJeff Roberson "psleep", hz / VM_INACT_SCAN_RATE) == 0) 214383c9dea1SGleb Smirnoff VM_CNT_INC(v_pdwakeups); 214456ce0690SAlan Cox } 2145be37ee79SMark Johnston 214630fbfddaSJeff Roberson /* Prevent spurious wakeups by ensuring that wanted is set. */ 214730fbfddaSJeff Roberson atomic_store_int(&vmd->vmd_pageout_wanted, 1); 214830fbfddaSJeff Roberson 214930fbfddaSJeff Roberson /* 215030fbfddaSJeff Roberson * Use the controller to calculate how many pages to free in 2151b50a4ea6SMark Johnston * this interval, and scan the inactive queue. If the lowmem 2152b50a4ea6SMark Johnston * handlers appear to have freed up some pages, subtract the 2153b50a4ea6SMark Johnston * difference from the inactive queue scan target. 215430fbfddaSJeff Roberson */ 21555f8cd1c0SJeff Roberson shortage = pidctrl_daemon(&vmd->vmd_pid, vmd->vmd_free_count); 215649a3710cSMark Johnston if (shortage > 0) { 2157b50a4ea6SMark Johnston ofree = vmd->vmd_free_count; 2158b50a4ea6SMark Johnston if (vm_pageout_lowmem() && vmd->vmd_free_count > ofree) 2159b50a4ea6SMark Johnston shortage -= min(vmd->vmd_free_count - ofree, 2160b50a4ea6SMark Johnston (u_int)shortage); 21610292c54bSConrad Meyer target_met = vm_pageout_inactive(vmd, shortage, 2162be37ee79SMark Johnston &addl_shortage); 216349a3710cSMark Johnston } else 216449a3710cSMark Johnston addl_shortage = 0; 216556ce0690SAlan Cox 2166be37ee79SMark Johnston /* 2167be37ee79SMark Johnston * Scan the active queue. A positive value for shortage 2168be37ee79SMark Johnston * indicates that we must aggressively deactivate pages to avoid 2169be37ee79SMark Johnston * a shortfall. 2170be37ee79SMark Johnston */ 21717bb4634eSMark Johnston shortage = vm_pageout_active_target(vmd) + addl_shortage; 2172be37ee79SMark Johnston vm_pageout_scan_active(vmd, shortage); 2173449c2e92SKonstantin Belousov } 2174449c2e92SKonstantin Belousov } 2175449c2e92SKonstantin Belousov 2176df8bae1dSRodney W. Grimes /* 21770292c54bSConrad Meyer * vm_pageout_helper runs additional pageout daemons in times of high paging 21780292c54bSConrad Meyer * activity. 21790292c54bSConrad Meyer */ 21800292c54bSConrad Meyer static void 21810292c54bSConrad Meyer vm_pageout_helper(void *arg) 21820292c54bSConrad Meyer { 21830292c54bSConrad Meyer struct vm_domain *vmd; 21840292c54bSConrad Meyer int domain; 21850292c54bSConrad Meyer 21860292c54bSConrad Meyer domain = (uintptr_t)arg; 21870292c54bSConrad Meyer vmd = VM_DOMAIN(domain); 21880292c54bSConrad Meyer 21890292c54bSConrad Meyer vm_domain_pageout_lock(vmd); 21900292c54bSConrad Meyer for (;;) { 21910292c54bSConrad Meyer msleep(&vmd->vmd_inactive_shortage, 21920292c54bSConrad Meyer vm_domain_pageout_lockptr(vmd), PVM, "psleep", 0); 21930292c54bSConrad Meyer blockcount_release(&vmd->vmd_inactive_starting, 1); 21940292c54bSConrad Meyer 21950292c54bSConrad Meyer vm_domain_pageout_unlock(vmd); 21960292c54bSConrad Meyer vm_pageout_scan_inactive(vmd, vmd->vmd_inactive_shortage); 21970292c54bSConrad Meyer vm_domain_pageout_lock(vmd); 21980292c54bSConrad Meyer 21990292c54bSConrad Meyer /* 22000292c54bSConrad Meyer * Release the running count while the pageout lock is held to 22010292c54bSConrad Meyer * prevent wakeup races. 22020292c54bSConrad Meyer */ 22030292c54bSConrad Meyer blockcount_release(&vmd->vmd_inactive_running, 1); 22040292c54bSConrad Meyer } 22050292c54bSConrad Meyer } 22060292c54bSConrad Meyer 22070292c54bSConrad Meyer static int 220874f5530dSConrad Meyer get_pageout_threads_per_domain(const struct vm_domain *vmd) 22090292c54bSConrad Meyer { 221074f5530dSConrad Meyer unsigned total_pageout_threads, eligible_cpus, domain_cpus; 22110292c54bSConrad Meyer 221274f5530dSConrad Meyer if (VM_DOMAIN_EMPTY(vmd->vmd_domain)) 221374f5530dSConrad Meyer return (0); 22140292c54bSConrad Meyer 22150292c54bSConrad Meyer /* 22160292c54bSConrad Meyer * Semi-arbitrarily constrain pagedaemon threads to less than half the 221774f5530dSConrad Meyer * total number of CPUs in the system as an upper limit. 22180292c54bSConrad Meyer */ 221974f5530dSConrad Meyer if (pageout_cpus_per_thread < 2) 222074f5530dSConrad Meyer pageout_cpus_per_thread = 2; 222174f5530dSConrad Meyer else if (pageout_cpus_per_thread > mp_ncpus) 222274f5530dSConrad Meyer pageout_cpus_per_thread = mp_ncpus; 22230292c54bSConrad Meyer 222474f5530dSConrad Meyer total_pageout_threads = howmany(mp_ncpus, pageout_cpus_per_thread); 222574f5530dSConrad Meyer domain_cpus = CPU_COUNT(&cpuset_domain[vmd->vmd_domain]); 222674f5530dSConrad Meyer 222774f5530dSConrad Meyer /* Pagedaemons are not run in empty domains. */ 222874f5530dSConrad Meyer eligible_cpus = mp_ncpus; 222974f5530dSConrad Meyer for (unsigned i = 0; i < vm_ndomains; i++) 223074f5530dSConrad Meyer if (VM_DOMAIN_EMPTY(i)) 223174f5530dSConrad Meyer eligible_cpus -= CPU_COUNT(&cpuset_domain[i]); 223274f5530dSConrad Meyer 223374f5530dSConrad Meyer /* 223474f5530dSConrad Meyer * Assign a portion of the total pageout threads to this domain 223574f5530dSConrad Meyer * corresponding to the fraction of pagedaemon-eligible CPUs in the 223674f5530dSConrad Meyer * domain. In asymmetric NUMA systems, domains with more CPUs may be 223774f5530dSConrad Meyer * allocated more threads than domains with fewer CPUs. 223874f5530dSConrad Meyer */ 223974f5530dSConrad Meyer return (howmany(total_pageout_threads * domain_cpus, eligible_cpus)); 22400292c54bSConrad Meyer } 22410292c54bSConrad Meyer 22420292c54bSConrad Meyer /* 22439c770a27SMark Johnston * Initialize basic pageout daemon settings. See the comment above the 22449c770a27SMark Johnston * definition of vm_domain for some explanation of how these thresholds are 22459c770a27SMark Johnston * used. 2246df8bae1dSRodney W. Grimes */ 22472b14f991SJulian Elischer static void 2248e2068d0bSJeff Roberson vm_pageout_init_domain(int domain) 2249df8bae1dSRodney W. Grimes { 2250e2068d0bSJeff Roberson struct vm_domain *vmd; 22515f8cd1c0SJeff Roberson struct sysctl_oid *oid; 2252e2068d0bSJeff Roberson 2253e2068d0bSJeff Roberson vmd = VM_DOMAIN(domain); 2254e2068d0bSJeff Roberson vmd->vmd_interrupt_free_min = 2; 2255f6b04d2bSDavid Greenman 225645ae1d91SAlan Cox /* 225745ae1d91SAlan Cox * v_free_reserved needs to include enough for the largest 225845ae1d91SAlan Cox * swap pager structures plus enough for any pv_entry structs 225945ae1d91SAlan Cox * when paging. 226045ae1d91SAlan Cox */ 22610cab71bcSDoug Moore vmd->vmd_pageout_free_min = 2 * MAXBSIZE / PAGE_SIZE + 2262e2068d0bSJeff Roberson vmd->vmd_interrupt_free_min; 2263e2068d0bSJeff Roberson vmd->vmd_free_reserved = vm_pageout_page_count + 22649c770a27SMark Johnston vmd->vmd_pageout_free_min + vmd->vmd_page_count / 768; 22659c770a27SMark Johnston vmd->vmd_free_min = vmd->vmd_page_count / 200; 2266e2068d0bSJeff Roberson vmd->vmd_free_severe = vmd->vmd_free_min / 2; 2267e2068d0bSJeff Roberson vmd->vmd_free_target = 4 * vmd->vmd_free_min + vmd->vmd_free_reserved; 2268e2068d0bSJeff Roberson vmd->vmd_free_min += vmd->vmd_free_reserved; 2269e2068d0bSJeff Roberson vmd->vmd_free_severe += vmd->vmd_free_reserved; 2270e2068d0bSJeff Roberson vmd->vmd_inactive_target = (3 * vmd->vmd_free_target) / 2; 2271e2068d0bSJeff Roberson if (vmd->vmd_inactive_target > vmd->vmd_free_count / 3) 2272e2068d0bSJeff Roberson vmd->vmd_inactive_target = vmd->vmd_free_count / 3; 2273df8bae1dSRodney W. Grimes 2274d9e23210SJeff Roberson /* 22755f8cd1c0SJeff Roberson * Set the default wakeup threshold to be 10% below the paging 22765f8cd1c0SJeff Roberson * target. This keeps the steady state out of shortfall. 2277d9e23210SJeff Roberson */ 22785f8cd1c0SJeff Roberson vmd->vmd_pageout_wakeup_thresh = (vmd->vmd_free_target / 10) * 9; 2279e2068d0bSJeff Roberson 2280e2068d0bSJeff Roberson /* 2281e2068d0bSJeff Roberson * Target amount of memory to move out of the laundry queue during a 2282e2068d0bSJeff Roberson * background laundering. This is proportional to the amount of system 2283e2068d0bSJeff Roberson * memory. 2284e2068d0bSJeff Roberson */ 2285e2068d0bSJeff Roberson vmd->vmd_background_launder_target = (vmd->vmd_free_target - 2286e2068d0bSJeff Roberson vmd->vmd_free_min) / 10; 22875f8cd1c0SJeff Roberson 22885f8cd1c0SJeff Roberson /* Initialize the pageout daemon pid controller. */ 22895f8cd1c0SJeff Roberson pidctrl_init(&vmd->vmd_pid, hz / VM_INACT_SCAN_RATE, 22905f8cd1c0SJeff Roberson vmd->vmd_free_target, PIDCTRL_BOUND, 22915f8cd1c0SJeff Roberson PIDCTRL_KPD, PIDCTRL_KID, PIDCTRL_KDD); 22925f8cd1c0SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(vmd->vmd_oid), OID_AUTO, 22937029da5cSPawel Biernacki "pidctrl", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 22945f8cd1c0SJeff Roberson pidctrl_init_sysctl(&vmd->vmd_pid, SYSCTL_CHILDREN(oid)); 22950292c54bSConrad Meyer 229674f5530dSConrad Meyer vmd->vmd_inactive_threads = get_pageout_threads_per_domain(vmd); 2297e2068d0bSJeff Roberson } 2298e2068d0bSJeff Roberson 2299e2068d0bSJeff Roberson static void 2300e2068d0bSJeff Roberson vm_pageout_init(void) 2301e2068d0bSJeff Roberson { 230297458520SMark Johnston u_long freecount; 2303e2068d0bSJeff Roberson int i; 2304e2068d0bSJeff Roberson 2305e2068d0bSJeff Roberson /* 2306e2068d0bSJeff Roberson * Initialize some paging parameters. 2307e2068d0bSJeff Roberson */ 2308e2068d0bSJeff Roberson if (vm_cnt.v_page_count < 2000) 2309e2068d0bSJeff Roberson vm_pageout_page_count = 8; 2310e2068d0bSJeff Roberson 2311e2068d0bSJeff Roberson freecount = 0; 2312e2068d0bSJeff Roberson for (i = 0; i < vm_ndomains; i++) { 2313e2068d0bSJeff Roberson struct vm_domain *vmd; 2314e2068d0bSJeff Roberson 2315e2068d0bSJeff Roberson vm_pageout_init_domain(i); 2316e2068d0bSJeff Roberson vmd = VM_DOMAIN(i); 2317e2068d0bSJeff Roberson vm_cnt.v_free_reserved += vmd->vmd_free_reserved; 2318e2068d0bSJeff Roberson vm_cnt.v_free_target += vmd->vmd_free_target; 2319e2068d0bSJeff Roberson vm_cnt.v_free_min += vmd->vmd_free_min; 2320e2068d0bSJeff Roberson vm_cnt.v_inactive_target += vmd->vmd_inactive_target; 2321e2068d0bSJeff Roberson vm_cnt.v_pageout_free_min += vmd->vmd_pageout_free_min; 2322e2068d0bSJeff Roberson vm_cnt.v_interrupt_free_min += vmd->vmd_interrupt_free_min; 2323e2068d0bSJeff Roberson vm_cnt.v_free_severe += vmd->vmd_free_severe; 2324e2068d0bSJeff Roberson freecount += vmd->vmd_free_count; 2325e2068d0bSJeff Roberson } 2326d9e23210SJeff Roberson 2327d9e23210SJeff Roberson /* 2328d9e23210SJeff Roberson * Set interval in seconds for active scan. We want to visit each 2329c9612b2dSJeff Roberson * page at least once every ten minutes. This is to prevent worst 2330c9612b2dSJeff Roberson * case paging behaviors with stale active LRU. 2331d9e23210SJeff Roberson */ 2332d9e23210SJeff Roberson if (vm_pageout_update_period == 0) 2333c9612b2dSJeff Roberson vm_pageout_update_period = 600; 2334d9e23210SJeff Roberson 233597458520SMark Johnston /* 233697458520SMark Johnston * Set the maximum number of user-wired virtual pages. Historically the 233797458520SMark Johnston * main source of such pages was mlock(2) and mlockall(2). Hypervisors 233897458520SMark Johnston * may also request user-wired memory. 233997458520SMark Johnston */ 234054a3a114SMark Johnston if (vm_page_max_user_wired == 0) 234197458520SMark Johnston vm_page_max_user_wired = 4 * freecount / 5; 23424d19f4adSSteven Hartland } 23434d19f4adSSteven Hartland 23444d19f4adSSteven Hartland /* 23454d19f4adSSteven Hartland * vm_pageout is the high level pageout daemon. 23464d19f4adSSteven Hartland */ 23474d19f4adSSteven Hartland static void 23484d19f4adSSteven Hartland vm_pageout(void) 23494d19f4adSSteven Hartland { 2350920239efSMark Johnston struct proc *p; 2351920239efSMark Johnston struct thread *td; 23520292c54bSConrad Meyer int error, first, i, j, pageout_threads; 2353920239efSMark Johnston 2354920239efSMark Johnston p = curproc; 2355920239efSMark Johnston td = curthread; 2356df8bae1dSRodney W. Grimes 2357245139c6SKonstantin Belousov mtx_init(&vm_oom_ratelim_mtx, "vmoomr", NULL, MTX_DEF); 235824a1cce3SDavid Greenman swap_pager_swap_init(); 2359920239efSMark Johnston for (first = -1, i = 0; i < vm_ndomains; i++) { 236030c5525bSAndrew Gallatin if (VM_DOMAIN_EMPTY(i)) { 236130c5525bSAndrew Gallatin if (bootverbose) 236230c5525bSAndrew Gallatin printf("domain %d empty; skipping pageout\n", 236330c5525bSAndrew Gallatin i); 236430c5525bSAndrew Gallatin continue; 236530c5525bSAndrew Gallatin } 2366920239efSMark Johnston if (first == -1) 2367920239efSMark Johnston first = i; 2368920239efSMark Johnston else { 2369920239efSMark Johnston error = kthread_add(vm_pageout_worker, 2370920239efSMark Johnston (void *)(uintptr_t)i, p, NULL, 0, 0, "dom%d", i); 2371920239efSMark Johnston if (error != 0) 2372920239efSMark Johnston panic("starting pageout for domain %d: %d\n", 2373449c2e92SKonstantin Belousov i, error); 2374dc2efb27SJohn Dyson } 237574f5530dSConrad Meyer pageout_threads = VM_DOMAIN(i)->vmd_inactive_threads; 23760292c54bSConrad Meyer for (j = 0; j < pageout_threads - 1; j++) { 23770292c54bSConrad Meyer error = kthread_add(vm_pageout_helper, 23780292c54bSConrad Meyer (void *)(uintptr_t)i, p, NULL, 0, 0, 23790292c54bSConrad Meyer "dom%d helper%d", i, j); 23800292c54bSConrad Meyer if (error != 0) 23810292c54bSConrad Meyer panic("starting pageout helper %d for domain " 23820292c54bSConrad Meyer "%d: %d\n", j, i, error); 23830292c54bSConrad Meyer } 2384e2068d0bSJeff Roberson error = kthread_add(vm_pageout_laundry_worker, 2385920239efSMark Johnston (void *)(uintptr_t)i, p, NULL, 0, 0, "laundry: dom%d", i); 2386e2068d0bSJeff Roberson if (error != 0) 2387920239efSMark Johnston panic("starting laundry for domain %d: %d", i, error); 2388f919ebdeSDavid Greenman } 2389920239efSMark Johnston error = kthread_add(uma_reclaim_worker, NULL, p, NULL, 0, 0, "uma"); 239044ec2b63SKonstantin Belousov if (error != 0) 239144ec2b63SKonstantin Belousov panic("starting uma_reclaim helper, error %d\n", error); 2392920239efSMark Johnston 2393920239efSMark Johnston snprintf(td->td_name, sizeof(td->td_name), "dom%d", first); 2394920239efSMark Johnston vm_pageout_worker((void *)(uintptr_t)first); 2395df8bae1dSRodney W. Grimes } 239626f9a767SRodney W. Grimes 23976b4b77adSAlan Cox /* 2398280d15cdSMark Johnston * Perform an advisory wakeup of the page daemon. 23996b4b77adSAlan Cox */ 2400e0c5a895SJohn Dyson void 2401e2068d0bSJeff Roberson pagedaemon_wakeup(int domain) 2402e0c5a895SJohn Dyson { 2403e2068d0bSJeff Roberson struct vm_domain *vmd; 2404a1c0a785SAlan Cox 2405e2068d0bSJeff Roberson vmd = VM_DOMAIN(domain); 240630fbfddaSJeff Roberson vm_domain_pageout_assert_unlocked(vmd); 240730fbfddaSJeff Roberson if (curproc == pageproc) 240830fbfddaSJeff Roberson return; 2409280d15cdSMark Johnston 241030fbfddaSJeff Roberson if (atomic_fetchadd_int(&vmd->vmd_pageout_wanted, 1) == 0) { 241130fbfddaSJeff Roberson vm_domain_pageout_lock(vmd); 241230fbfddaSJeff Roberson atomic_store_int(&vmd->vmd_pageout_wanted, 1); 2413e2068d0bSJeff Roberson wakeup(&vmd->vmd_pageout_wanted); 241430fbfddaSJeff Roberson vm_domain_pageout_unlock(vmd); 2415e0c5a895SJohn Dyson } 2416e0c5a895SJohn Dyson } 2417