160727d8bSWarner Losh /*- 2796df753SPedro F. Giffuni * SPDX-License-Identifier: (BSD-4-Clause AND MIT-CMU) 3df57947fSPedro F. Giffuni * 426f9a767SRodney W. Grimes * Copyright (c) 1991 Regents of the University of California. 526f9a767SRodney W. Grimes * All rights reserved. 626f9a767SRodney W. Grimes * Copyright (c) 1994 John S. Dyson 726f9a767SRodney W. Grimes * All rights reserved. 826f9a767SRodney W. Grimes * Copyright (c) 1994 David Greenman 926f9a767SRodney W. Grimes * All rights reserved. 108dbca793STor Egge * Copyright (c) 2005 Yahoo! Technologies Norway AS 118dbca793STor Egge * All rights reserved. 12df8bae1dSRodney W. Grimes * 13df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 14df8bae1dSRodney W. Grimes * The Mach Operating System project at Carnegie-Mellon University. 15df8bae1dSRodney W. Grimes * 16df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 17df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 18df8bae1dSRodney W. Grimes * are met: 19df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 20df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 21df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 22df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 23df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 24df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 255929bcfaSPhilippe Charnier * must display the following acknowledgement: 26df8bae1dSRodney W. Grimes * This product includes software developed by the University of 27df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 28df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 29df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 30df8bae1dSRodney W. Grimes * without specific prior written permission. 31df8bae1dSRodney W. Grimes * 32df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 33df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 34df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 35df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 36df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 37df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 38df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 39df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 40df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 41df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 42df8bae1dSRodney W. Grimes * SUCH DAMAGE. 43df8bae1dSRodney W. Grimes * 443c4dd356SDavid Greenman * from: @(#)vm_pageout.c 7.4 (Berkeley) 5/7/91 45df8bae1dSRodney W. Grimes * 46df8bae1dSRodney W. Grimes * 47df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1990 Carnegie-Mellon University. 48df8bae1dSRodney W. Grimes * All rights reserved. 49df8bae1dSRodney W. Grimes * 50df8bae1dSRodney W. Grimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young 51df8bae1dSRodney W. Grimes * 52df8bae1dSRodney W. Grimes * Permission to use, copy, modify and distribute this software and 53df8bae1dSRodney W. Grimes * its documentation is hereby granted, provided that both the copyright 54df8bae1dSRodney W. Grimes * notice and this permission notice appear in all copies of the 55df8bae1dSRodney W. Grimes * software, derivative works or modified versions, and any portions 56df8bae1dSRodney W. Grimes * thereof, and that both notices appear in supporting documentation. 57df8bae1dSRodney W. Grimes * 58df8bae1dSRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 59df8bae1dSRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 60df8bae1dSRodney W. Grimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 61df8bae1dSRodney W. Grimes * 62df8bae1dSRodney W. Grimes * Carnegie Mellon requests users of this software to return to 63df8bae1dSRodney W. Grimes * 64df8bae1dSRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 65df8bae1dSRodney W. Grimes * School of Computer Science 66df8bae1dSRodney W. Grimes * Carnegie Mellon University 67df8bae1dSRodney W. Grimes * Pittsburgh PA 15213-3890 68df8bae1dSRodney W. Grimes * 69df8bae1dSRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the 70df8bae1dSRodney W. Grimes * rights to redistribute these changes. 71df8bae1dSRodney W. Grimes */ 72df8bae1dSRodney W. Grimes 73df8bae1dSRodney W. Grimes /* 74df8bae1dSRodney W. Grimes * The proverbial page-out daemon. 75df8bae1dSRodney W. Grimes */ 76df8bae1dSRodney W. Grimes 77874651b1SDavid E. O'Brien #include <sys/cdefs.h> 78874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 79874651b1SDavid E. O'Brien 80faa5f8d8SAndrzej Bialecki #include "opt_vm.h" 817672ca05SMark Johnston 82df8bae1dSRodney W. Grimes #include <sys/param.h> 8326f9a767SRodney W. Grimes #include <sys/systm.h> 84b5e8ce9fSBruce Evans #include <sys/kernel.h> 850292c54bSConrad Meyer #include <sys/blockcount.h> 86855a310fSJeff Roberson #include <sys/eventhandler.h> 87fb919e4dSMark Murray #include <sys/lock.h> 88fb919e4dSMark Murray #include <sys/mutex.h> 8926f9a767SRodney W. Grimes #include <sys/proc.h> 909c8b8baaSPeter Wemm #include <sys/kthread.h> 910384fff8SJason Evans #include <sys/ktr.h> 9297824da3SAlan Cox #include <sys/mount.h> 93099e7e95SEdward Tomasz Napierala #include <sys/racct.h> 9426f9a767SRodney W. Grimes #include <sys/resourcevar.h> 95b43179fbSJeff Roberson #include <sys/sched.h> 9614a0d74eSSteven Hartland #include <sys/sdt.h> 97d2fc5315SPoul-Henning Kamp #include <sys/signalvar.h> 98449c2e92SKonstantin Belousov #include <sys/smp.h> 99a6bf3a9eSRyan Stone #include <sys/time.h> 100f6b04d2bSDavid Greenman #include <sys/vnode.h> 101efeaf95aSDavid Greenman #include <sys/vmmeter.h> 10289f6b863SAttilio Rao #include <sys/rwlock.h> 1031005a129SJohn Baldwin #include <sys/sx.h> 10438efa82bSJohn Dyson #include <sys/sysctl.h> 105df8bae1dSRodney W. Grimes 106df8bae1dSRodney W. Grimes #include <vm/vm.h> 107efeaf95aSDavid Greenman #include <vm/vm_param.h> 108efeaf95aSDavid Greenman #include <vm/vm_object.h> 109df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 110efeaf95aSDavid Greenman #include <vm/vm_map.h> 111df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h> 11224a1cce3SDavid Greenman #include <vm/vm_pager.h> 113449c2e92SKonstantin Belousov #include <vm/vm_phys.h> 114e2068d0bSJeff Roberson #include <vm/vm_pagequeue.h> 11505f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h> 116efeaf95aSDavid Greenman #include <vm/vm_extern.h> 117670d17b5SJeff Roberson #include <vm/uma.h> 118df8bae1dSRodney W. Grimes 1192b14f991SJulian Elischer /* 1202b14f991SJulian Elischer * System initialization 1212b14f991SJulian Elischer */ 1222b14f991SJulian Elischer 1232b14f991SJulian Elischer /* the kernel process "vm_pageout"*/ 12411caded3SAlfred Perlstein static void vm_pageout(void); 1254d19f4adSSteven Hartland static void vm_pageout_init(void); 126ebcddc72SAlan Cox static int vm_pageout_clean(vm_page_t m, int *numpagedout); 12734d8b7eaSJeff Roberson static int vm_pageout_cluster(vm_page_t m); 12876386c7eSKonstantin Belousov static void vm_pageout_mightbe_oom(struct vm_domain *vmd, int page_shortage, 12976386c7eSKonstantin Belousov int starting_page_shortage); 13045ae1d91SAlan Cox 1314d19f4adSSteven Hartland SYSINIT(pagedaemon_init, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, vm_pageout_init, 1324d19f4adSSteven Hartland NULL); 1334d19f4adSSteven Hartland 1342b14f991SJulian Elischer struct proc *pageproc; 1352b14f991SJulian Elischer 1362b14f991SJulian Elischer static struct kproc_desc page_kp = { 1372b14f991SJulian Elischer "pagedaemon", 1382b14f991SJulian Elischer vm_pageout, 1392b14f991SJulian Elischer &pageproc 1402b14f991SJulian Elischer }; 1414d19f4adSSteven Hartland SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_SECOND, kproc_start, 142237fdd78SRobert Watson &page_kp); 1432b14f991SJulian Elischer 14414a0d74eSSteven Hartland SDT_PROVIDER_DEFINE(vm); 14514a0d74eSSteven Hartland SDT_PROBE_DEFINE(vm, , , vm__lowmem_scan); 14614a0d74eSSteven Hartland 147ebcddc72SAlan Cox /* Pagedaemon activity rates, in subdivisions of one second. */ 148ebcddc72SAlan Cox #define VM_LAUNDER_RATE 10 1495f8cd1c0SJeff Roberson #define VM_INACT_SCAN_RATE 10 1502b14f991SJulian Elischer 15176386c7eSKonstantin Belousov static int vm_pageout_oom_seq = 12; 152ebcddc72SAlan Cox 153d9e23210SJeff Roberson static int vm_pageout_update_period; 1544a365329SAndrey Zonov static int disable_swap_pageouts; 155c9612b2dSJeff Roberson static int lowmem_period = 10; 156b1fd102eSMark Johnston static int swapdev_enabled; 15770111b90SJohn Dyson 1588311a2b8SWill Andrews static int vm_panic_on_oom = 0; 1598311a2b8SWill Andrews 1608311a2b8SWill Andrews SYSCTL_INT(_vm, OID_AUTO, panic_on_oom, 1618311a2b8SWill Andrews CTLFLAG_RWTUN, &vm_panic_on_oom, 0, 1623c200db9SJonathan T. Looney "Panic on the given number of out-of-memory errors instead of killing the largest process"); 1638311a2b8SWill Andrews 164d9e23210SJeff Roberson SYSCTL_INT(_vm, OID_AUTO, pageout_update_period, 165e0b2fc3aSMark Johnston CTLFLAG_RWTUN, &vm_pageout_update_period, 0, 166d9e23210SJeff Roberson "Maximum active LRU update period"); 16753636869SAndrey Zonov 16874f5530dSConrad Meyer static int pageout_cpus_per_thread = 16; 16974f5530dSConrad Meyer SYSCTL_INT(_vm, OID_AUTO, pageout_cpus_per_thread, CTLFLAG_RDTUN, 17074f5530dSConrad Meyer &pageout_cpus_per_thread, 0, 17174f5530dSConrad Meyer "Number of CPUs per pagedaemon worker thread"); 1720292c54bSConrad Meyer 173e0b2fc3aSMark Johnston SYSCTL_INT(_vm, OID_AUTO, lowmem_period, CTLFLAG_RWTUN, &lowmem_period, 0, 174c9612b2dSJeff Roberson "Low memory callback period"); 175c9612b2dSJeff Roberson 176ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts, 177e0b2fc3aSMark Johnston CTLFLAG_RWTUN, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages"); 17812ac6a1dSJohn Dyson 17923b59018SMatthew Dillon static int pageout_lock_miss; 18023b59018SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss, 18123b59018SMatthew Dillon CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout"); 18223b59018SMatthew Dillon 18376386c7eSKonstantin Belousov SYSCTL_INT(_vm, OID_AUTO, pageout_oom_seq, 184e0b2fc3aSMark Johnston CTLFLAG_RWTUN, &vm_pageout_oom_seq, 0, 18576386c7eSKonstantin Belousov "back-to-back calls to oom detector to start OOM"); 18676386c7eSKonstantin Belousov 187ebcddc72SAlan Cox static int act_scan_laundry_weight = 3; 188e0b2fc3aSMark Johnston SYSCTL_INT(_vm, OID_AUTO, act_scan_laundry_weight, CTLFLAG_RWTUN, 189ebcddc72SAlan Cox &act_scan_laundry_weight, 0, 190ebcddc72SAlan Cox "weight given to clean vs. dirty pages in active queue scans"); 191ebcddc72SAlan Cox 192ebcddc72SAlan Cox static u_int vm_background_launder_rate = 4096; 193e0b2fc3aSMark Johnston SYSCTL_UINT(_vm, OID_AUTO, background_launder_rate, CTLFLAG_RWTUN, 194ebcddc72SAlan Cox &vm_background_launder_rate, 0, 195ebcddc72SAlan Cox "background laundering rate, in kilobytes per second"); 196ebcddc72SAlan Cox 197ebcddc72SAlan Cox static u_int vm_background_launder_max = 20 * 1024; 198e0b2fc3aSMark Johnston SYSCTL_UINT(_vm, OID_AUTO, background_launder_max, CTLFLAG_RWTUN, 199ebcddc72SAlan Cox &vm_background_launder_max, 0, "background laundering cap, in kilobytes"); 200ebcddc72SAlan Cox 201e2241590SAlan Cox int vm_pageout_page_count = 32; 202df8bae1dSRodney W. Grimes 20354a3a114SMark Johnston u_long vm_page_max_user_wired; 20454a3a114SMark Johnston SYSCTL_ULONG(_vm, OID_AUTO, max_user_wired, CTLFLAG_RW, 20554a3a114SMark Johnston &vm_page_max_user_wired, 0, 20654a3a114SMark Johnston "system-wide limit to user-wired page count"); 207df8bae1dSRodney W. Grimes 208ebcddc72SAlan Cox static u_int isqrt(u_int num); 209ebcddc72SAlan Cox static int vm_pageout_launder(struct vm_domain *vmd, int launder, 210ebcddc72SAlan Cox bool in_shortfall); 211ebcddc72SAlan Cox static void vm_pageout_laundry_worker(void *arg); 212cd41fc12SDavid Greenman 2135cd29d0fSMark Johnston struct scan_state { 2145cd29d0fSMark Johnston struct vm_batchqueue bq; 2158d220203SAlan Cox struct vm_pagequeue *pq; 2165cd29d0fSMark Johnston vm_page_t marker; 2175cd29d0fSMark Johnston int maxscan; 2185cd29d0fSMark Johnston int scanned; 2195cd29d0fSMark Johnston }; 2208dbca793STor Egge 2215cd29d0fSMark Johnston static void 2225cd29d0fSMark Johnston vm_pageout_init_scan(struct scan_state *ss, struct vm_pagequeue *pq, 2235cd29d0fSMark Johnston vm_page_t marker, vm_page_t after, int maxscan) 2245cd29d0fSMark Johnston { 2258dbca793STor Egge 2265cd29d0fSMark Johnston vm_pagequeue_assert_locked(pq); 2275cff1f4dSMark Johnston KASSERT((marker->a.flags & PGA_ENQUEUED) == 0, 2285cd29d0fSMark Johnston ("marker %p already enqueued", marker)); 2295cd29d0fSMark Johnston 2305cd29d0fSMark Johnston if (after == NULL) 2315cd29d0fSMark Johnston TAILQ_INSERT_HEAD(&pq->pq_pl, marker, plinks.q); 2325cd29d0fSMark Johnston else 2335cd29d0fSMark Johnston TAILQ_INSERT_AFTER(&pq->pq_pl, after, marker, plinks.q); 2345cd29d0fSMark Johnston vm_page_aflag_set(marker, PGA_ENQUEUED); 2355cd29d0fSMark Johnston 2365cd29d0fSMark Johnston vm_batchqueue_init(&ss->bq); 2375cd29d0fSMark Johnston ss->pq = pq; 2385cd29d0fSMark Johnston ss->marker = marker; 2395cd29d0fSMark Johnston ss->maxscan = maxscan; 2405cd29d0fSMark Johnston ss->scanned = 0; 2418d220203SAlan Cox vm_pagequeue_unlock(pq); 2425cd29d0fSMark Johnston } 2438dbca793STor Egge 2445cd29d0fSMark Johnston static void 2455cd29d0fSMark Johnston vm_pageout_end_scan(struct scan_state *ss) 2465cd29d0fSMark Johnston { 2475cd29d0fSMark Johnston struct vm_pagequeue *pq; 2485cd29d0fSMark Johnston 2495cd29d0fSMark Johnston pq = ss->pq; 2505cd29d0fSMark Johnston vm_pagequeue_assert_locked(pq); 2515cff1f4dSMark Johnston KASSERT((ss->marker->a.flags & PGA_ENQUEUED) != 0, 2525cd29d0fSMark Johnston ("marker %p not enqueued", ss->marker)); 2535cd29d0fSMark Johnston 2545cd29d0fSMark Johnston TAILQ_REMOVE(&pq->pq_pl, ss->marker, plinks.q); 2555cd29d0fSMark Johnston vm_page_aflag_clear(ss->marker, PGA_ENQUEUED); 256899fe184SMark Johnston pq->pq_pdpages += ss->scanned; 2578dbca793STor Egge } 2588dbca793STor Egge 2598dbca793STor Egge /* 2605cd29d0fSMark Johnston * Add a small number of queued pages to a batch queue for later processing 2615cd29d0fSMark Johnston * without the corresponding queue lock held. The caller must have enqueued a 2625cd29d0fSMark Johnston * marker page at the desired start point for the scan. Pages will be 2635cd29d0fSMark Johnston * physically dequeued if the caller so requests. Otherwise, the returned 2645cd29d0fSMark Johnston * batch may contain marker pages, and it is up to the caller to handle them. 2655cd29d0fSMark Johnston * 266efec381dSMark Johnston * When processing the batch queue, vm_pageout_defer() must be used to 267efec381dSMark Johnston * determine whether the page has been logically dequeued since the batch was 268efec381dSMark Johnston * collected. 2695cd29d0fSMark Johnston */ 2705cd29d0fSMark Johnston static __always_inline void 2715cd29d0fSMark Johnston vm_pageout_collect_batch(struct scan_state *ss, const bool dequeue) 2725cd29d0fSMark Johnston { 2738d220203SAlan Cox struct vm_pagequeue *pq; 274d70f0ab3SMark Johnston vm_page_t m, marker, n; 2758c616246SKonstantin Belousov 2765cd29d0fSMark Johnston marker = ss->marker; 2775cd29d0fSMark Johnston pq = ss->pq; 2788c616246SKonstantin Belousov 2795cff1f4dSMark Johnston KASSERT((marker->a.flags & PGA_ENQUEUED) != 0, 2805cd29d0fSMark Johnston ("marker %p not enqueued", ss->marker)); 2818c616246SKonstantin Belousov 2828d220203SAlan Cox vm_pagequeue_lock(pq); 2835cd29d0fSMark Johnston for (m = TAILQ_NEXT(marker, plinks.q); m != NULL && 2845cd29d0fSMark Johnston ss->scanned < ss->maxscan && ss->bq.bq_cnt < VM_BATCHQUEUE_SIZE; 285d70f0ab3SMark Johnston m = n, ss->scanned++) { 286d70f0ab3SMark Johnston n = TAILQ_NEXT(m, plinks.q); 2875cd29d0fSMark Johnston if ((m->flags & PG_MARKER) == 0) { 2885cff1f4dSMark Johnston KASSERT((m->a.flags & PGA_ENQUEUED) != 0, 2895cd29d0fSMark Johnston ("page %p not enqueued", m)); 2905cd29d0fSMark Johnston KASSERT((m->flags & PG_FICTITIOUS) == 0, 2915cd29d0fSMark Johnston ("Fictitious page %p cannot be in page queue", m)); 2925cd29d0fSMark Johnston KASSERT((m->oflags & VPO_UNMANAGED) == 0, 2935cd29d0fSMark Johnston ("Unmanaged page %p cannot be in page queue", m)); 2945cd29d0fSMark Johnston } else if (dequeue) 2955cd29d0fSMark Johnston continue; 2968c616246SKonstantin Belousov 2975cd29d0fSMark Johnston (void)vm_batchqueue_insert(&ss->bq, m); 2985cd29d0fSMark Johnston if (dequeue) { 2995cd29d0fSMark Johnston TAILQ_REMOVE(&pq->pq_pl, m, plinks.q); 3005cd29d0fSMark Johnston vm_page_aflag_clear(m, PGA_ENQUEUED); 3015cd29d0fSMark Johnston } 3025cd29d0fSMark Johnston } 3035cd29d0fSMark Johnston TAILQ_REMOVE(&pq->pq_pl, marker, plinks.q); 3045cd29d0fSMark Johnston if (__predict_true(m != NULL)) 3055cd29d0fSMark Johnston TAILQ_INSERT_BEFORE(m, marker, plinks.q); 3065cd29d0fSMark Johnston else 3075cd29d0fSMark Johnston TAILQ_INSERT_TAIL(&pq->pq_pl, marker, plinks.q); 3085cd29d0fSMark Johnston if (dequeue) 3095cd29d0fSMark Johnston vm_pagequeue_cnt_add(pq, -ss->bq.bq_cnt); 3105cd29d0fSMark Johnston vm_pagequeue_unlock(pq); 3115cd29d0fSMark Johnston } 3125cd29d0fSMark Johnston 313fee2a2faSMark Johnston /* 314fee2a2faSMark Johnston * Return the next page to be scanned, or NULL if the scan is complete. 315fee2a2faSMark Johnston */ 3165cd29d0fSMark Johnston static __always_inline vm_page_t 3175cd29d0fSMark Johnston vm_pageout_next(struct scan_state *ss, const bool dequeue) 3185cd29d0fSMark Johnston { 3195cd29d0fSMark Johnston 3205cd29d0fSMark Johnston if (ss->bq.bq_cnt == 0) 3215cd29d0fSMark Johnston vm_pageout_collect_batch(ss, dequeue); 3225cd29d0fSMark Johnston return (vm_batchqueue_pop(&ss->bq)); 3238c616246SKonstantin Belousov } 3248c616246SKonstantin Belousov 3258c616246SKonstantin Belousov /* 326b7f30bffSMark Johnston * Determine whether processing of a page should be deferred and ensure that any 327b7f30bffSMark Johnston * outstanding queue operations are processed. 328b7f30bffSMark Johnston */ 329b7f30bffSMark Johnston static __always_inline bool 330b7f30bffSMark Johnston vm_pageout_defer(vm_page_t m, const uint8_t queue, const bool enqueued) 331b7f30bffSMark Johnston { 332b7f30bffSMark Johnston vm_page_astate_t as; 333b7f30bffSMark Johnston 334b7f30bffSMark Johnston as = vm_page_astate_load(m); 335b7f30bffSMark Johnston if (__predict_false(as.queue != queue || 336b7f30bffSMark Johnston ((as.flags & PGA_ENQUEUED) != 0) != enqueued)) 337b7f30bffSMark Johnston return (true); 338b7f30bffSMark Johnston if ((as.flags & PGA_QUEUE_OP_MASK) != 0) { 339b7f30bffSMark Johnston vm_page_pqbatch_submit(m, queue); 340b7f30bffSMark Johnston return (true); 341b7f30bffSMark Johnston } 342b7f30bffSMark Johnston return (false); 343b7f30bffSMark Johnston } 344b7f30bffSMark Johnston 345b7f30bffSMark Johnston /* 346248fe642SAlan Cox * Scan for pages at adjacent offsets within the given page's object that are 347248fe642SAlan Cox * eligible for laundering, form a cluster of these pages and the given page, 348248fe642SAlan Cox * and launder that cluster. 34926f9a767SRodney W. Grimes */ 3503af76890SPoul-Henning Kamp static int 35134d8b7eaSJeff Roberson vm_pageout_cluster(vm_page_t m) 35224a1cce3SDavid Greenman { 35354d92145SMatthew Dillon vm_object_t object; 354248fe642SAlan Cox vm_page_t mc[2 * vm_pageout_page_count], p, pb, ps; 355248fe642SAlan Cox vm_pindex_t pindex; 356248fe642SAlan Cox int ib, is, page_base, pageout_count; 35726f9a767SRodney W. Grimes 35817f6a17bSAlan Cox object = m->object; 35989f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 360248fe642SAlan Cox pindex = m->pindex; 3610cddd8f0SMatthew Dillon 36263e97555SJeff Roberson vm_page_assert_xbusied(m); 3630d94caffSDavid Greenman 36491b4f427SAlan Cox mc[vm_pageout_page_count] = pb = ps = m; 36526f9a767SRodney W. Grimes pageout_count = 1; 366f35329acSJohn Dyson page_base = vm_pageout_page_count; 36790ecac61SMatthew Dillon ib = 1; 36890ecac61SMatthew Dillon is = 1; 36990ecac61SMatthew Dillon 37024a1cce3SDavid Greenman /* 371248fe642SAlan Cox * We can cluster only if the page is not clean, busy, or held, and 372ebcddc72SAlan Cox * the page is in the laundry queue. 37390ecac61SMatthew Dillon * 37490ecac61SMatthew Dillon * During heavy mmap/modification loads the pageout 37590ecac61SMatthew Dillon * daemon can really fragment the underlying file 376248fe642SAlan Cox * due to flushing pages out of order and not trying to 377248fe642SAlan Cox * align the clusters (which leaves sporadic out-of-order 37890ecac61SMatthew Dillon * holes). To solve this problem we do the reverse scan 37990ecac61SMatthew Dillon * first and attempt to align our cluster, then do a 38090ecac61SMatthew Dillon * forward scan if room remains. 38124a1cce3SDavid Greenman */ 38290ecac61SMatthew Dillon more: 383248fe642SAlan Cox while (ib != 0 && pageout_count < vm_pageout_page_count) { 38490ecac61SMatthew Dillon if (ib > pindex) { 38590ecac61SMatthew Dillon ib = 0; 38690ecac61SMatthew Dillon break; 387f6b04d2bSDavid Greenman } 38863e97555SJeff Roberson if ((p = vm_page_prev(pb)) == NULL || 38963e97555SJeff Roberson vm_page_tryxbusy(p) == 0) { 39090ecac61SMatthew Dillon ib = 0; 39190ecac61SMatthew Dillon break; 392f6b04d2bSDavid Greenman } 39363e97555SJeff Roberson if (vm_page_wired(p)) { 39463e97555SJeff Roberson ib = 0; 39563e97555SJeff Roberson vm_page_xunbusy(p); 39663e97555SJeff Roberson break; 39763e97555SJeff Roberson } 39824a1cce3SDavid Greenman vm_page_test_dirty(p); 3991b5c869dSMark Johnston if (p->dirty == 0) { 400eb5d3969SAlan Cox ib = 0; 40163e97555SJeff Roberson vm_page_xunbusy(p); 402eb5d3969SAlan Cox break; 403eb5d3969SAlan Cox } 404fee2a2faSMark Johnston if (!vm_page_in_laundry(p) || !vm_page_try_remove_write(p)) { 40563e97555SJeff Roberson vm_page_xunbusy(p); 40690ecac61SMatthew Dillon ib = 0; 40724a1cce3SDavid Greenman break; 408f6b04d2bSDavid Greenman } 40991b4f427SAlan Cox mc[--page_base] = pb = p; 41090ecac61SMatthew Dillon ++pageout_count; 41190ecac61SMatthew Dillon ++ib; 412248fe642SAlan Cox 41324a1cce3SDavid Greenman /* 414248fe642SAlan Cox * We are at an alignment boundary. Stop here, and switch 415248fe642SAlan Cox * directions. Do not clear ib. 41624a1cce3SDavid Greenman */ 41790ecac61SMatthew Dillon if ((pindex - (ib - 1)) % vm_pageout_page_count == 0) 41890ecac61SMatthew Dillon break; 41924a1cce3SDavid Greenman } 42090ecac61SMatthew Dillon while (pageout_count < vm_pageout_page_count && 42190ecac61SMatthew Dillon pindex + is < object->size) { 42263e97555SJeff Roberson if ((p = vm_page_next(ps)) == NULL || 42363e97555SJeff Roberson vm_page_tryxbusy(p) == 0) 42490ecac61SMatthew Dillon break; 42563e97555SJeff Roberson if (vm_page_wired(p)) { 42663e97555SJeff Roberson vm_page_xunbusy(p); 42763e97555SJeff Roberson break; 42863e97555SJeff Roberson } 42924a1cce3SDavid Greenman vm_page_test_dirty(p); 43063e97555SJeff Roberson if (p->dirty == 0) { 43163e97555SJeff Roberson vm_page_xunbusy(p); 432eb5d3969SAlan Cox break; 43363e97555SJeff Roberson } 434e8bcf696SMark Johnston if (!vm_page_in_laundry(p) || !vm_page_try_remove_write(p)) { 43563e97555SJeff Roberson vm_page_xunbusy(p); 43624a1cce3SDavid Greenman break; 437e8bcf696SMark Johnston } 43891b4f427SAlan Cox mc[page_base + pageout_count] = ps = p; 43990ecac61SMatthew Dillon ++pageout_count; 44090ecac61SMatthew Dillon ++is; 44124a1cce3SDavid Greenman } 44290ecac61SMatthew Dillon 44390ecac61SMatthew Dillon /* 44490ecac61SMatthew Dillon * If we exhausted our forward scan, continue with the reverse scan 445248fe642SAlan Cox * when possible, even past an alignment boundary. This catches 446248fe642SAlan Cox * boundary conditions. 44790ecac61SMatthew Dillon */ 448248fe642SAlan Cox if (ib != 0 && pageout_count < vm_pageout_page_count) 44990ecac61SMatthew Dillon goto more; 450f6b04d2bSDavid Greenman 45199e6e193SMark Johnston return (vm_pageout_flush(&mc[page_base], pageout_count, 45299e6e193SMark Johnston VM_PAGER_PUT_NOREUSE, 0, NULL, NULL)); 453aef922f5SJohn Dyson } 454aef922f5SJohn Dyson 4551c7c3c6aSMatthew Dillon /* 4561c7c3c6aSMatthew Dillon * vm_pageout_flush() - launder the given pages 4571c7c3c6aSMatthew Dillon * 4581c7c3c6aSMatthew Dillon * The given pages are laundered. Note that we setup for the start of 4591c7c3c6aSMatthew Dillon * I/O ( i.e. busy the page ), mark it read-only, and bump the object 4601c7c3c6aSMatthew Dillon * reference count all in here rather then in the parent. If we want 4611c7c3c6aSMatthew Dillon * the parent to do more sophisticated things we may have to change 4621c7c3c6aSMatthew Dillon * the ordering. 4631e8a675cSKonstantin Belousov * 4641e8a675cSKonstantin Belousov * Returned runlen is the count of pages between mreq and first 4651e8a675cSKonstantin Belousov * page after mreq with status VM_PAGER_AGAIN. 466126d6082SKonstantin Belousov * *eio is set to TRUE if pager returned VM_PAGER_ERROR or VM_PAGER_FAIL 467126d6082SKonstantin Belousov * for any page in runlen set. 4681c7c3c6aSMatthew Dillon */ 469aef922f5SJohn Dyson int 470126d6082SKonstantin Belousov vm_pageout_flush(vm_page_t *mc, int count, int flags, int mreq, int *prunlen, 471126d6082SKonstantin Belousov boolean_t *eio) 472aef922f5SJohn Dyson { 4732e3b314dSAlan Cox vm_object_t object = mc[0]->object; 474aef922f5SJohn Dyson int pageout_status[count]; 47595461b45SJohn Dyson int numpagedout = 0; 4761e8a675cSKonstantin Belousov int i, runlen; 477aef922f5SJohn Dyson 47889f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 4797bec141bSKip Macy 4801c7c3c6aSMatthew Dillon /* 48163e97555SJeff Roberson * Initiate I/O. Mark the pages shared busy and verify that they're 48263e97555SJeff Roberson * valid and read-only. 4831c7c3c6aSMatthew Dillon * 4841c7c3c6aSMatthew Dillon * We do not have to fixup the clean/dirty bits here... we can 4851c7c3c6aSMatthew Dillon * allow the pager to do it after the I/O completes. 48602fa91d3SMatthew Dillon * 48702fa91d3SMatthew Dillon * NOTE! mc[i]->dirty may be partial or fragmented due to an 48802fa91d3SMatthew Dillon * edge case with file fragments. 4891c7c3c6aSMatthew Dillon */ 4908f9110f6SJohn Dyson for (i = 0; i < count; i++) { 4910012f373SJeff Roberson KASSERT(vm_page_all_valid(mc[i]), 4927a935082SAlan Cox ("vm_pageout_flush: partially invalid page %p index %d/%d", 4937a935082SAlan Cox mc[i], i, count)); 4945cff1f4dSMark Johnston KASSERT((mc[i]->a.flags & PGA_WRITEABLE) == 0, 495aed9aaaaSMark Johnston ("vm_pageout_flush: writeable page %p", mc[i])); 49663e97555SJeff Roberson vm_page_busy_downgrade(mc[i]); 4972965a453SKip Macy } 498d474eaaaSDoug Rabson vm_object_pip_add(object, count); 499aef922f5SJohn Dyson 500d076fbeaSAlan Cox vm_pager_put_pages(object, mc, count, flags, pageout_status); 50126f9a767SRodney W. Grimes 5021e8a675cSKonstantin Belousov runlen = count - mreq; 503126d6082SKonstantin Belousov if (eio != NULL) 504126d6082SKonstantin Belousov *eio = FALSE; 505aef922f5SJohn Dyson for (i = 0; i < count; i++) { 506aef922f5SJohn Dyson vm_page_t mt = mc[i]; 50724a1cce3SDavid Greenman 5084cd45723SAlan Cox KASSERT(pageout_status[i] == VM_PAGER_PEND || 5096031c68dSAlan Cox !pmap_page_is_write_mapped(mt), 5109ea8d1a6SAlan Cox ("vm_pageout_flush: page %p is not write protected", mt)); 51126f9a767SRodney W. Grimes switch (pageout_status[i]) { 51226f9a767SRodney W. Grimes case VM_PAGER_OK: 5139f5632e6SMark Johnston /* 5149f5632e6SMark Johnston * The page may have moved since laundering started, in 5159f5632e6SMark Johnston * which case it should be left alone. 5169f5632e6SMark Johnston */ 517ebcddc72SAlan Cox if (vm_page_in_laundry(mt)) 518ebcddc72SAlan Cox vm_page_deactivate_noreuse(mt); 519ebcddc72SAlan Cox /* FALLTHROUGH */ 52026f9a767SRodney W. Grimes case VM_PAGER_PEND: 52195461b45SJohn Dyson numpagedout++; 52226f9a767SRodney W. Grimes break; 52326f9a767SRodney W. Grimes case VM_PAGER_BAD: 52426f9a767SRodney W. Grimes /* 525ebcddc72SAlan Cox * The page is outside the object's range. We pretend 526ebcddc72SAlan Cox * that the page out worked and clean the page, so the 527ebcddc72SAlan Cox * changes will be lost if the page is reclaimed by 528ebcddc72SAlan Cox * the page daemon. 52926f9a767SRodney W. Grimes */ 53090ecac61SMatthew Dillon vm_page_undirty(mt); 531ebcddc72SAlan Cox if (vm_page_in_laundry(mt)) 532ebcddc72SAlan Cox vm_page_deactivate_noreuse(mt); 53326f9a767SRodney W. Grimes break; 53426f9a767SRodney W. Grimes case VM_PAGER_ERROR: 53526f9a767SRodney W. Grimes case VM_PAGER_FAIL: 53626f9a767SRodney W. Grimes /* 537b1fd102eSMark Johnston * If the page couldn't be paged out to swap because the 538b1fd102eSMark Johnston * pager wasn't able to find space, place the page in 539b1fd102eSMark Johnston * the PQ_UNSWAPPABLE holding queue. This is an 540b1fd102eSMark Johnston * optimization that prevents the page daemon from 541b1fd102eSMark Johnston * wasting CPU cycles on pages that cannot be reclaimed 542b1fd102eSMark Johnston * becase no swap device is configured. 543b1fd102eSMark Johnston * 544b1fd102eSMark Johnston * Otherwise, reactivate the page so that it doesn't 545b1fd102eSMark Johnston * clog the laundry and inactive queues. (We will try 546b1fd102eSMark Johnston * paging it out again later.) 54726f9a767SRodney W. Grimes */ 548b1fd102eSMark Johnston if (object->type == OBJT_SWAP && 549b1fd102eSMark Johnston pageout_status[i] == VM_PAGER_FAIL) { 550b1fd102eSMark Johnston vm_page_unswappable(mt); 551b1fd102eSMark Johnston numpagedout++; 552b1fd102eSMark Johnston } else 55324a1cce3SDavid Greenman vm_page_activate(mt); 554126d6082SKonstantin Belousov if (eio != NULL && i >= mreq && i - mreq < runlen) 555126d6082SKonstantin Belousov *eio = TRUE; 55626f9a767SRodney W. Grimes break; 55726f9a767SRodney W. Grimes case VM_PAGER_AGAIN: 5581e8a675cSKonstantin Belousov if (i >= mreq && i - mreq < runlen) 5591e8a675cSKonstantin Belousov runlen = i - mreq; 56026f9a767SRodney W. Grimes break; 56126f9a767SRodney W. Grimes } 56226f9a767SRodney W. Grimes 56326f9a767SRodney W. Grimes /* 5640d94caffSDavid Greenman * If the operation is still going, leave the page busy to 5650d94caffSDavid Greenman * block all other accesses. Also, leave the paging in 5660d94caffSDavid Greenman * progress indicator set so that we don't attempt an object 5670d94caffSDavid Greenman * collapse. 56826f9a767SRodney W. Grimes */ 56926f9a767SRodney W. Grimes if (pageout_status[i] != VM_PAGER_PEND) { 570f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 571c7aebda8SAttilio Rao vm_page_sunbusy(mt); 5723c4a2440SAlan Cox } 5733c4a2440SAlan Cox } 5741e8a675cSKonstantin Belousov if (prunlen != NULL) 5751e8a675cSKonstantin Belousov *prunlen = runlen; 5763c4a2440SAlan Cox return (numpagedout); 57726f9a767SRodney W. Grimes } 57826f9a767SRodney W. Grimes 579b1fd102eSMark Johnston static void 580b1fd102eSMark Johnston vm_pageout_swapon(void *arg __unused, struct swdevt *sp __unused) 581b1fd102eSMark Johnston { 582b1fd102eSMark Johnston 583b1fd102eSMark Johnston atomic_store_rel_int(&swapdev_enabled, 1); 584b1fd102eSMark Johnston } 585b1fd102eSMark Johnston 586b1fd102eSMark Johnston static void 587b1fd102eSMark Johnston vm_pageout_swapoff(void *arg __unused, struct swdevt *sp __unused) 588b1fd102eSMark Johnston { 589b1fd102eSMark Johnston 590b1fd102eSMark Johnston if (swap_pager_nswapdev() == 1) 591b1fd102eSMark Johnston atomic_store_rel_int(&swapdev_enabled, 0); 592b1fd102eSMark Johnston } 593b1fd102eSMark Johnston 5941c7c3c6aSMatthew Dillon /* 59534d8b7eaSJeff Roberson * Attempt to acquire all of the necessary locks to launder a page and 59634d8b7eaSJeff Roberson * then call through the clustering layer to PUTPAGES. Wait a short 59734d8b7eaSJeff Roberson * time for a vnode lock. 59834d8b7eaSJeff Roberson * 59934d8b7eaSJeff Roberson * Requires the page and object lock on entry, releases both before return. 60034d8b7eaSJeff Roberson * Returns 0 on success and an errno otherwise. 60134d8b7eaSJeff Roberson */ 60234d8b7eaSJeff Roberson static int 603ebcddc72SAlan Cox vm_pageout_clean(vm_page_t m, int *numpagedout) 60434d8b7eaSJeff Roberson { 60534d8b7eaSJeff Roberson struct vnode *vp; 60634d8b7eaSJeff Roberson struct mount *mp; 60734d8b7eaSJeff Roberson vm_object_t object; 60834d8b7eaSJeff Roberson vm_pindex_t pindex; 60934d8b7eaSJeff Roberson int error, lockmode; 61034d8b7eaSJeff Roberson 61134d8b7eaSJeff Roberson object = m->object; 61234d8b7eaSJeff Roberson VM_OBJECT_ASSERT_WLOCKED(object); 61334d8b7eaSJeff Roberson error = 0; 61434d8b7eaSJeff Roberson vp = NULL; 61534d8b7eaSJeff Roberson mp = NULL; 61634d8b7eaSJeff Roberson 61734d8b7eaSJeff Roberson /* 61834d8b7eaSJeff Roberson * The object is already known NOT to be dead. It 61934d8b7eaSJeff Roberson * is possible for the vget() to block the whole 62034d8b7eaSJeff Roberson * pageout daemon, but the new low-memory handling 62134d8b7eaSJeff Roberson * code should prevent it. 62234d8b7eaSJeff Roberson * 62334d8b7eaSJeff Roberson * We can't wait forever for the vnode lock, we might 62434d8b7eaSJeff Roberson * deadlock due to a vn_read() getting stuck in 62534d8b7eaSJeff Roberson * vm_wait while holding this vnode. We skip the 62634d8b7eaSJeff Roberson * vnode if we can't get it in a reasonable amount 62734d8b7eaSJeff Roberson * of time. 62834d8b7eaSJeff Roberson */ 62934d8b7eaSJeff Roberson if (object->type == OBJT_VNODE) { 63063e97555SJeff Roberson vm_page_xunbusy(m); 63134d8b7eaSJeff Roberson vp = object->handle; 63234d8b7eaSJeff Roberson if (vp->v_type == VREG && 63334d8b7eaSJeff Roberson vn_start_write(vp, &mp, V_NOWAIT) != 0) { 63434d8b7eaSJeff Roberson mp = NULL; 63534d8b7eaSJeff Roberson error = EDEADLK; 63634d8b7eaSJeff Roberson goto unlock_all; 63734d8b7eaSJeff Roberson } 63834d8b7eaSJeff Roberson KASSERT(mp != NULL, 63934d8b7eaSJeff Roberson ("vp %p with NULL v_mount", vp)); 64034d8b7eaSJeff Roberson vm_object_reference_locked(object); 64134d8b7eaSJeff Roberson pindex = m->pindex; 64234d8b7eaSJeff Roberson VM_OBJECT_WUNLOCK(object); 64334d8b7eaSJeff Roberson lockmode = MNT_SHARED_WRITES(vp->v_mount) ? 64434d8b7eaSJeff Roberson LK_SHARED : LK_EXCLUSIVE; 645a92a971bSMateusz Guzik if (vget(vp, lockmode | LK_TIMELOCK)) { 64634d8b7eaSJeff Roberson vp = NULL; 64734d8b7eaSJeff Roberson error = EDEADLK; 64834d8b7eaSJeff Roberson goto unlock_mp; 64934d8b7eaSJeff Roberson } 65034d8b7eaSJeff Roberson VM_OBJECT_WLOCK(object); 65157cd81a3SMark Johnston 65257cd81a3SMark Johnston /* 65357cd81a3SMark Johnston * Ensure that the object and vnode were not disassociated 65457cd81a3SMark Johnston * while locks were dropped. 65557cd81a3SMark Johnston */ 65657cd81a3SMark Johnston if (vp->v_object != object) { 65757cd81a3SMark Johnston error = ENOENT; 65857cd81a3SMark Johnston goto unlock_all; 65957cd81a3SMark Johnston } 66057cd81a3SMark Johnston 66134d8b7eaSJeff Roberson /* 6629f5632e6SMark Johnston * While the object was unlocked, the page may have been: 66334d8b7eaSJeff Roberson * (1) moved to a different queue, 66434d8b7eaSJeff Roberson * (2) reallocated to a different object, 66534d8b7eaSJeff Roberson * (3) reallocated to a different offset, or 66634d8b7eaSJeff Roberson * (4) cleaned. 66734d8b7eaSJeff Roberson */ 668ebcddc72SAlan Cox if (!vm_page_in_laundry(m) || m->object != object || 66934d8b7eaSJeff Roberson m->pindex != pindex || m->dirty == 0) { 67034d8b7eaSJeff Roberson error = ENXIO; 67134d8b7eaSJeff Roberson goto unlock_all; 67234d8b7eaSJeff Roberson } 67334d8b7eaSJeff Roberson 67434d8b7eaSJeff Roberson /* 6759f5632e6SMark Johnston * The page may have been busied while the object lock was 6769f5632e6SMark Johnston * released. 67734d8b7eaSJeff Roberson */ 67863e97555SJeff Roberson if (vm_page_tryxbusy(m) == 0) { 67934d8b7eaSJeff Roberson error = EBUSY; 68034d8b7eaSJeff Roberson goto unlock_all; 68134d8b7eaSJeff Roberson } 68234d8b7eaSJeff Roberson } 68334d8b7eaSJeff Roberson 68434d8b7eaSJeff Roberson /* 685fee2a2faSMark Johnston * Remove all writeable mappings, failing if the page is wired. 686fee2a2faSMark Johnston */ 687fee2a2faSMark Johnston if (!vm_page_try_remove_write(m)) { 68863e97555SJeff Roberson vm_page_xunbusy(m); 689fee2a2faSMark Johnston error = EBUSY; 690fee2a2faSMark Johnston goto unlock_all; 691fee2a2faSMark Johnston } 692fee2a2faSMark Johnston 693fee2a2faSMark Johnston /* 69434d8b7eaSJeff Roberson * If a page is dirty, then it is either being washed 69534d8b7eaSJeff Roberson * (but not yet cleaned) or it is still in the 69634d8b7eaSJeff Roberson * laundry. If it is still in the laundry, then we 69734d8b7eaSJeff Roberson * start the cleaning operation. 69834d8b7eaSJeff Roberson */ 699ebcddc72SAlan Cox if ((*numpagedout = vm_pageout_cluster(m)) == 0) 70034d8b7eaSJeff Roberson error = EIO; 70134d8b7eaSJeff Roberson 70234d8b7eaSJeff Roberson unlock_all: 70334d8b7eaSJeff Roberson VM_OBJECT_WUNLOCK(object); 70434d8b7eaSJeff Roberson 70534d8b7eaSJeff Roberson unlock_mp: 70634d8b7eaSJeff Roberson if (mp != NULL) { 70734d8b7eaSJeff Roberson if (vp != NULL) 70834d8b7eaSJeff Roberson vput(vp); 70934d8b7eaSJeff Roberson vm_object_deallocate(object); 71034d8b7eaSJeff Roberson vn_finished_write(mp); 71134d8b7eaSJeff Roberson } 71234d8b7eaSJeff Roberson 71334d8b7eaSJeff Roberson return (error); 71434d8b7eaSJeff Roberson } 71534d8b7eaSJeff Roberson 71634d8b7eaSJeff Roberson /* 717ebcddc72SAlan Cox * Attempt to launder the specified number of pages. 718ebcddc72SAlan Cox * 719ebcddc72SAlan Cox * Returns the number of pages successfully laundered. 720ebcddc72SAlan Cox */ 721ebcddc72SAlan Cox static int 722ebcddc72SAlan Cox vm_pageout_launder(struct vm_domain *vmd, int launder, bool in_shortfall) 723ebcddc72SAlan Cox { 7245cd29d0fSMark Johnston struct scan_state ss; 725ebcddc72SAlan Cox struct vm_pagequeue *pq; 726ebcddc72SAlan Cox vm_object_t object; 7275cd29d0fSMark Johnston vm_page_t m, marker; 728f3f38e25SMark Johnston vm_page_astate_t new, old; 729f3f38e25SMark Johnston int act_delta, error, numpagedout, queue, refs, starting_target; 730ebcddc72SAlan Cox int vnodes_skipped; 73160256604SMark Johnston bool pageout_ok; 732ebcddc72SAlan Cox 7335cd29d0fSMark Johnston object = NULL; 734ebcddc72SAlan Cox starting_target = launder; 735ebcddc72SAlan Cox vnodes_skipped = 0; 736ebcddc72SAlan Cox 737ebcddc72SAlan Cox /* 738b1fd102eSMark Johnston * Scan the laundry queues for pages eligible to be laundered. We stop 739ebcddc72SAlan Cox * once the target number of dirty pages have been laundered, or once 740ebcddc72SAlan Cox * we've reached the end of the queue. A single iteration of this loop 741ebcddc72SAlan Cox * may cause more than one page to be laundered because of clustering. 742ebcddc72SAlan Cox * 743b1fd102eSMark Johnston * As an optimization, we avoid laundering from PQ_UNSWAPPABLE when no 744b1fd102eSMark Johnston * swap devices are configured. 745ebcddc72SAlan Cox */ 746b1fd102eSMark Johnston if (atomic_load_acq_int(&swapdev_enabled)) 74764b38930SMark Johnston queue = PQ_UNSWAPPABLE; 748b1fd102eSMark Johnston else 74964b38930SMark Johnston queue = PQ_LAUNDRY; 750ebcddc72SAlan Cox 751b1fd102eSMark Johnston scan: 75264b38930SMark Johnston marker = &vmd->vmd_markers[queue]; 7535cd29d0fSMark Johnston pq = &vmd->vmd_pagequeues[queue]; 754ebcddc72SAlan Cox vm_pagequeue_lock(pq); 7555cd29d0fSMark Johnston vm_pageout_init_scan(&ss, pq, marker, NULL, pq->pq_cnt); 7565cd29d0fSMark Johnston while (launder > 0 && (m = vm_pageout_next(&ss, false)) != NULL) { 7575cd29d0fSMark Johnston if (__predict_false((m->flags & PG_MARKER) != 0)) 758ebcddc72SAlan Cox continue; 7595cd29d0fSMark Johnston 7605cd29d0fSMark Johnston /* 761b7f30bffSMark Johnston * Don't touch a page that was removed from the queue after the 762b7f30bffSMark Johnston * page queue lock was released. Otherwise, ensure that any 763b7f30bffSMark Johnston * pending queue operations, such as dequeues for wired pages, 764b7f30bffSMark Johnston * are handled. 7655cd29d0fSMark Johnston */ 766b7f30bffSMark Johnston if (vm_pageout_defer(m, queue, true)) 767ebcddc72SAlan Cox continue; 768e8bcf696SMark Johnston 7699f5632e6SMark Johnston /* 7709f5632e6SMark Johnston * Lock the page's object. 7719f5632e6SMark Johnston */ 7729f5632e6SMark Johnston if (object == NULL || object != m->object) { 77360256604SMark Johnston if (object != NULL) 7745cd29d0fSMark Johnston VM_OBJECT_WUNLOCK(object); 77523ed568cSMateusz Guzik object = atomic_load_ptr(&m->object); 7769f5632e6SMark Johnston if (__predict_false(object == NULL)) 7779f5632e6SMark Johnston /* The page is being freed by another thread. */ 7789f5632e6SMark Johnston continue; 7799f5632e6SMark Johnston 780e8bcf696SMark Johnston /* Depends on type-stability. */ 78141fd4b94SMark Johnston VM_OBJECT_WLOCK(object); 7829f5632e6SMark Johnston if (__predict_false(m->object != object)) { 7839f5632e6SMark Johnston VM_OBJECT_WUNLOCK(object); 7849f5632e6SMark Johnston object = NULL; 78541fd4b94SMark Johnston continue; 7869f5632e6SMark Johnston } 7879f5632e6SMark Johnston } 7885cd29d0fSMark Johnston 78963e97555SJeff Roberson if (vm_page_tryxbusy(m) == 0) 7905cd29d0fSMark Johnston continue; 791ebcddc72SAlan Cox 792ebcddc72SAlan Cox /* 793b7f30bffSMark Johnston * Check for wirings now that we hold the object lock and have 7949f5632e6SMark Johnston * exclusively busied the page. If the page is mapped, it may 7959f5632e6SMark Johnston * still be wired by pmap lookups. The call to 796fee2a2faSMark Johnston * vm_page_try_remove_all() below atomically checks for such 797fee2a2faSMark Johnston * wirings and removes mappings. If the page is unmapped, the 7989f5632e6SMark Johnston * wire count is guaranteed not to increase after this check. 799fee2a2faSMark Johnston */ 8009f5632e6SMark Johnston if (__predict_false(vm_page_wired(m))) 801f3f38e25SMark Johnston goto skip_page; 802fee2a2faSMark Johnston 803fee2a2faSMark Johnston /* 804ebcddc72SAlan Cox * Invalid pages can be easily freed. They cannot be 805ebcddc72SAlan Cox * mapped; vm_page_free() asserts this. 806ebcddc72SAlan Cox */ 8070012f373SJeff Roberson if (vm_page_none_valid(m)) 808ebcddc72SAlan Cox goto free_page; 809ebcddc72SAlan Cox 810f3f38e25SMark Johnston refs = object->ref_count != 0 ? pmap_ts_referenced(m) : 0; 811f3f38e25SMark Johnston 812f3f38e25SMark Johnston for (old = vm_page_astate_load(m);;) { 813ebcddc72SAlan Cox /* 814f3f38e25SMark Johnston * Check to see if the page has been removed from the 815f3f38e25SMark Johnston * queue since the first such check. Leave it alone if 816f3f38e25SMark Johnston * so, discarding any references collected by 817f3f38e25SMark Johnston * pmap_ts_referenced(). 818ebcddc72SAlan Cox */ 819f3f38e25SMark Johnston if (__predict_false(_vm_page_queue(old) == PQ_NONE)) 820f3f38e25SMark Johnston goto skip_page; 821f3f38e25SMark Johnston 822f3f38e25SMark Johnston new = old; 823f3f38e25SMark Johnston act_delta = refs; 824f3f38e25SMark Johnston if ((old.flags & PGA_REFERENCED) != 0) { 825f3f38e25SMark Johnston new.flags &= ~PGA_REFERENCED; 826d7aeb429SAlan Cox act_delta++; 827ebcddc72SAlan Cox } 828f3f38e25SMark Johnston if (act_delta == 0) { 829f3f38e25SMark Johnston ; 830f3f38e25SMark Johnston } else if (object->ref_count != 0) { 831ebcddc72SAlan Cox /* 832f3f38e25SMark Johnston * Increase the activation count if the page was 833f3f38e25SMark Johnston * referenced while in the laundry queue. This 834f3f38e25SMark Johnston * makes it less likely that the page will be 835f3f38e25SMark Johnston * returned prematurely to the laundry queue. 836e8bcf696SMark Johnston */ 837f3f38e25SMark Johnston new.act_count += ACT_ADVANCE + 838f3f38e25SMark Johnston act_delta; 839f3f38e25SMark Johnston if (new.act_count > ACT_MAX) 840f3f38e25SMark Johnston new.act_count = ACT_MAX; 841f3f38e25SMark Johnston 842f7607c30SMark Johnston new.flags &= ~PGA_QUEUE_OP_MASK; 843f3f38e25SMark Johnston new.flags |= PGA_REQUEUE; 844f3f38e25SMark Johnston new.queue = PQ_ACTIVE; 845f3f38e25SMark Johnston if (!vm_page_pqstate_commit(m, &old, new)) 846f3f38e25SMark Johnston continue; 847e8bcf696SMark Johnston 848e8bcf696SMark Johnston /* 849e8bcf696SMark Johnston * If this was a background laundering, count 850e8bcf696SMark Johnston * activated pages towards our target. The 851e8bcf696SMark Johnston * purpose of background laundering is to ensure 852e8bcf696SMark Johnston * that pages are eventually cycled through the 853e8bcf696SMark Johnston * laundry queue, and an activation is a valid 854e8bcf696SMark Johnston * way out. 855ebcddc72SAlan Cox */ 856ebcddc72SAlan Cox if (!in_shortfall) 857ebcddc72SAlan Cox launder--; 858f3f38e25SMark Johnston VM_CNT_INC(v_reactivated); 859f3f38e25SMark Johnston goto skip_page; 8605cd29d0fSMark Johnston } else if ((object->flags & OBJ_DEAD) == 0) { 861f3f38e25SMark Johnston new.flags |= PGA_REQUEUE; 862f3f38e25SMark Johnston if (!vm_page_pqstate_commit(m, &old, new)) 863e8bcf696SMark Johnston continue; 864f3f38e25SMark Johnston goto skip_page; 8655cd29d0fSMark Johnston } 866f3f38e25SMark Johnston break; 867ebcddc72SAlan Cox } 868ebcddc72SAlan Cox 869ebcddc72SAlan Cox /* 870ebcddc72SAlan Cox * If the page appears to be clean at the machine-independent 871ebcddc72SAlan Cox * layer, then remove all of its mappings from the pmap in 872ebcddc72SAlan Cox * anticipation of freeing it. If, however, any of the page's 873ebcddc72SAlan Cox * mappings allow write access, then the page may still be 874ebcddc72SAlan Cox * modified until the last of those mappings are removed. 875ebcddc72SAlan Cox */ 876ebcddc72SAlan Cox if (object->ref_count != 0) { 877ebcddc72SAlan Cox vm_page_test_dirty(m); 8789f5632e6SMark Johnston if (m->dirty == 0 && !vm_page_try_remove_all(m)) 879f3f38e25SMark Johnston goto skip_page; 880fee2a2faSMark Johnston } 881ebcddc72SAlan Cox 882ebcddc72SAlan Cox /* 883ebcddc72SAlan Cox * Clean pages are freed, and dirty pages are paged out unless 884ebcddc72SAlan Cox * they belong to a dead object. Requeueing dirty pages from 885ebcddc72SAlan Cox * dead objects is pointless, as they are being paged out and 886ebcddc72SAlan Cox * freed by the thread that destroyed the object. 887ebcddc72SAlan Cox */ 888ebcddc72SAlan Cox if (m->dirty == 0) { 889ebcddc72SAlan Cox free_page: 8909f5632e6SMark Johnston /* 8919f5632e6SMark Johnston * Now we are guaranteed that no other threads are 8929f5632e6SMark Johnston * manipulating the page, check for a last-second 8939f5632e6SMark Johnston * reference. 8949f5632e6SMark Johnston */ 8959f5632e6SMark Johnston if (vm_pageout_defer(m, queue, true)) 8969f5632e6SMark Johnston goto skip_page; 897ebcddc72SAlan Cox vm_page_free(m); 89883c9dea1SGleb Smirnoff VM_CNT_INC(v_dfree); 899ebcddc72SAlan Cox } else if ((object->flags & OBJ_DEAD) == 0) { 900ebcddc72SAlan Cox if (object->type != OBJT_SWAP && 901ebcddc72SAlan Cox object->type != OBJT_DEFAULT) 902ebcddc72SAlan Cox pageout_ok = true; 903ebcddc72SAlan Cox else if (disable_swap_pageouts) 904ebcddc72SAlan Cox pageout_ok = false; 905ebcddc72SAlan Cox else 906ebcddc72SAlan Cox pageout_ok = true; 907ebcddc72SAlan Cox if (!pageout_ok) { 908f3f38e25SMark Johnston vm_page_launder(m); 909f3f38e25SMark Johnston goto skip_page; 910ebcddc72SAlan Cox } 911ebcddc72SAlan Cox 912ebcddc72SAlan Cox /* 913ebcddc72SAlan Cox * Form a cluster with adjacent, dirty pages from the 914ebcddc72SAlan Cox * same object, and page out that entire cluster. 915ebcddc72SAlan Cox * 916ebcddc72SAlan Cox * The adjacent, dirty pages must also be in the 917ebcddc72SAlan Cox * laundry. However, their mappings are not checked 918ebcddc72SAlan Cox * for new references. Consequently, a recently 919ebcddc72SAlan Cox * referenced page may be paged out. However, that 920ebcddc72SAlan Cox * page will not be prematurely reclaimed. After page 921ebcddc72SAlan Cox * out, the page will be placed in the inactive queue, 922ebcddc72SAlan Cox * where any new references will be detected and the 923ebcddc72SAlan Cox * page reactivated. 924ebcddc72SAlan Cox */ 925ebcddc72SAlan Cox error = vm_pageout_clean(m, &numpagedout); 926ebcddc72SAlan Cox if (error == 0) { 927ebcddc72SAlan Cox launder -= numpagedout; 9285cd29d0fSMark Johnston ss.scanned += numpagedout; 929ebcddc72SAlan Cox } else if (error == EDEADLK) { 930ebcddc72SAlan Cox pageout_lock_miss++; 931ebcddc72SAlan Cox vnodes_skipped++; 932ebcddc72SAlan Cox } 93360256604SMark Johnston object = NULL; 934f3f38e25SMark Johnston } else { 935f3f38e25SMark Johnston skip_page: 93663e97555SJeff Roberson vm_page_xunbusy(m); 937e8bcf696SMark Johnston } 938f3f38e25SMark Johnston } 93946e39081SMark Johnston if (object != NULL) { 940ebcddc72SAlan Cox VM_OBJECT_WUNLOCK(object); 94146e39081SMark Johnston object = NULL; 94246e39081SMark Johnston } 943ebcddc72SAlan Cox vm_pagequeue_lock(pq); 9445cd29d0fSMark Johnston vm_pageout_end_scan(&ss); 945ebcddc72SAlan Cox vm_pagequeue_unlock(pq); 946ebcddc72SAlan Cox 94764b38930SMark Johnston if (launder > 0 && queue == PQ_UNSWAPPABLE) { 94864b38930SMark Johnston queue = PQ_LAUNDRY; 949b1fd102eSMark Johnston goto scan; 950b1fd102eSMark Johnston } 951b1fd102eSMark Johnston 952ebcddc72SAlan Cox /* 953ebcddc72SAlan Cox * Wakeup the sync daemon if we skipped a vnode in a writeable object 954ebcddc72SAlan Cox * and we didn't launder enough pages. 955ebcddc72SAlan Cox */ 956ebcddc72SAlan Cox if (vnodes_skipped > 0 && launder > 0) 957ebcddc72SAlan Cox (void)speedup_syncer(); 958ebcddc72SAlan Cox 959ebcddc72SAlan Cox return (starting_target - launder); 960ebcddc72SAlan Cox } 961ebcddc72SAlan Cox 962ebcddc72SAlan Cox /* 963ebcddc72SAlan Cox * Compute the integer square root. 964ebcddc72SAlan Cox */ 965ebcddc72SAlan Cox static u_int 966ebcddc72SAlan Cox isqrt(u_int num) 967ebcddc72SAlan Cox { 968ebcddc72SAlan Cox u_int bit, root, tmp; 969ebcddc72SAlan Cox 97064f8d257SDoug Moore bit = num != 0 ? (1u << ((fls(num) - 1) & ~1)) : 0; 971ebcddc72SAlan Cox root = 0; 972ebcddc72SAlan Cox while (bit != 0) { 973ebcddc72SAlan Cox tmp = root + bit; 974ebcddc72SAlan Cox root >>= 1; 975ebcddc72SAlan Cox if (num >= tmp) { 976ebcddc72SAlan Cox num -= tmp; 977ebcddc72SAlan Cox root += bit; 978ebcddc72SAlan Cox } 979ebcddc72SAlan Cox bit >>= 2; 980ebcddc72SAlan Cox } 981ebcddc72SAlan Cox return (root); 982ebcddc72SAlan Cox } 983ebcddc72SAlan Cox 984ebcddc72SAlan Cox /* 985ebcddc72SAlan Cox * Perform the work of the laundry thread: periodically wake up and determine 986ebcddc72SAlan Cox * whether any pages need to be laundered. If so, determine the number of pages 987ebcddc72SAlan Cox * that need to be laundered, and launder them. 988ebcddc72SAlan Cox */ 989ebcddc72SAlan Cox static void 990ebcddc72SAlan Cox vm_pageout_laundry_worker(void *arg) 991ebcddc72SAlan Cox { 992e2068d0bSJeff Roberson struct vm_domain *vmd; 993ebcddc72SAlan Cox struct vm_pagequeue *pq; 99460684862SMark Johnston uint64_t nclean, ndirty, nfreed; 995e2068d0bSJeff Roberson int domain, last_target, launder, shortfall, shortfall_cycle, target; 996ebcddc72SAlan Cox bool in_shortfall; 997ebcddc72SAlan Cox 998e2068d0bSJeff Roberson domain = (uintptr_t)arg; 999e2068d0bSJeff Roberson vmd = VM_DOMAIN(domain); 1000e2068d0bSJeff Roberson pq = &vmd->vmd_pagequeues[PQ_LAUNDRY]; 1001e2068d0bSJeff Roberson KASSERT(vmd->vmd_segs != 0, ("domain without segments")); 1002ebcddc72SAlan Cox 1003ebcddc72SAlan Cox shortfall = 0; 1004ebcddc72SAlan Cox in_shortfall = false; 1005ebcddc72SAlan Cox shortfall_cycle = 0; 10068002c3a4SMark Johnston last_target = target = 0; 100760684862SMark Johnston nfreed = 0; 1008ebcddc72SAlan Cox 1009ebcddc72SAlan Cox /* 1010b1fd102eSMark Johnston * Calls to these handlers are serialized by the swap syscall lock. 1011b1fd102eSMark Johnston */ 1012e2068d0bSJeff Roberson (void)EVENTHANDLER_REGISTER(swapon, vm_pageout_swapon, vmd, 1013b1fd102eSMark Johnston EVENTHANDLER_PRI_ANY); 1014e2068d0bSJeff Roberson (void)EVENTHANDLER_REGISTER(swapoff, vm_pageout_swapoff, vmd, 1015b1fd102eSMark Johnston EVENTHANDLER_PRI_ANY); 1016b1fd102eSMark Johnston 1017b1fd102eSMark Johnston /* 1018ebcddc72SAlan Cox * The pageout laundry worker is never done, so loop forever. 1019ebcddc72SAlan Cox */ 1020ebcddc72SAlan Cox for (;;) { 1021ebcddc72SAlan Cox KASSERT(target >= 0, ("negative target %d", target)); 1022ebcddc72SAlan Cox KASSERT(shortfall_cycle >= 0, 1023ebcddc72SAlan Cox ("negative cycle %d", shortfall_cycle)); 1024ebcddc72SAlan Cox launder = 0; 1025ebcddc72SAlan Cox 1026ebcddc72SAlan Cox /* 1027ebcddc72SAlan Cox * First determine whether we need to launder pages to meet a 1028ebcddc72SAlan Cox * shortage of free pages. 1029ebcddc72SAlan Cox */ 1030ebcddc72SAlan Cox if (shortfall > 0) { 1031ebcddc72SAlan Cox in_shortfall = true; 1032ebcddc72SAlan Cox shortfall_cycle = VM_LAUNDER_RATE / VM_INACT_SCAN_RATE; 1033ebcddc72SAlan Cox target = shortfall; 1034ebcddc72SAlan Cox } else if (!in_shortfall) 1035ebcddc72SAlan Cox goto trybackground; 1036e2068d0bSJeff Roberson else if (shortfall_cycle == 0 || vm_laundry_target(vmd) <= 0) { 1037ebcddc72SAlan Cox /* 1038ebcddc72SAlan Cox * We recently entered shortfall and began laundering 1039ebcddc72SAlan Cox * pages. If we have completed that laundering run 1040ebcddc72SAlan Cox * (and we are no longer in shortfall) or we have met 1041ebcddc72SAlan Cox * our laundry target through other activity, then we 1042ebcddc72SAlan Cox * can stop laundering pages. 1043ebcddc72SAlan Cox */ 1044ebcddc72SAlan Cox in_shortfall = false; 1045ebcddc72SAlan Cox target = 0; 1046ebcddc72SAlan Cox goto trybackground; 1047ebcddc72SAlan Cox } 1048ebcddc72SAlan Cox launder = target / shortfall_cycle--; 1049ebcddc72SAlan Cox goto dolaundry; 1050ebcddc72SAlan Cox 1051ebcddc72SAlan Cox /* 1052ebcddc72SAlan Cox * There's no immediate need to launder any pages; see if we 1053ebcddc72SAlan Cox * meet the conditions to perform background laundering: 1054ebcddc72SAlan Cox * 1055ebcddc72SAlan Cox * 1. The ratio of dirty to clean inactive pages exceeds the 105660684862SMark Johnston * background laundering threshold, or 1057ebcddc72SAlan Cox * 2. we haven't yet reached the target of the current 1058ebcddc72SAlan Cox * background laundering run. 1059ebcddc72SAlan Cox * 1060ebcddc72SAlan Cox * The background laundering threshold is not a constant. 1061ebcddc72SAlan Cox * Instead, it is a slowly growing function of the number of 106260684862SMark Johnston * clean pages freed by the page daemon since the last 106360684862SMark Johnston * background laundering. Thus, as the ratio of dirty to 106460684862SMark Johnston * clean inactive pages grows, the amount of memory pressure 1065c098768eSMark Johnston * required to trigger laundering decreases. We ensure 1066c098768eSMark Johnston * that the threshold is non-zero after an inactive queue 1067c098768eSMark Johnston * scan, even if that scan failed to free a single clean page. 1068ebcddc72SAlan Cox */ 1069ebcddc72SAlan Cox trybackground: 1070e2068d0bSJeff Roberson nclean = vmd->vmd_free_count + 1071e2068d0bSJeff Roberson vmd->vmd_pagequeues[PQ_INACTIVE].pq_cnt; 1072e2068d0bSJeff Roberson ndirty = vmd->vmd_pagequeues[PQ_LAUNDRY].pq_cnt; 1073c098768eSMark Johnston if (target == 0 && ndirty * isqrt(howmany(nfreed + 1, 1074c098768eSMark Johnston vmd->vmd_free_target - vmd->vmd_free_min)) >= nclean) { 1075e2068d0bSJeff Roberson target = vmd->vmd_background_launder_target; 1076ebcddc72SAlan Cox } 1077ebcddc72SAlan Cox 1078ebcddc72SAlan Cox /* 1079ebcddc72SAlan Cox * We have a non-zero background laundering target. If we've 1080ebcddc72SAlan Cox * laundered up to our maximum without observing a page daemon 1081cb35676eSMark Johnston * request, just stop. This is a safety belt that ensures we 1082ebcddc72SAlan Cox * don't launder an excessive amount if memory pressure is low 1083ebcddc72SAlan Cox * and the ratio of dirty to clean pages is large. Otherwise, 1084ebcddc72SAlan Cox * proceed at the background laundering rate. 1085ebcddc72SAlan Cox */ 1086ebcddc72SAlan Cox if (target > 0) { 108760684862SMark Johnston if (nfreed > 0) { 108860684862SMark Johnston nfreed = 0; 1089ebcddc72SAlan Cox last_target = target; 1090ebcddc72SAlan Cox } else if (last_target - target >= 1091ebcddc72SAlan Cox vm_background_launder_max * PAGE_SIZE / 1024) { 1092ebcddc72SAlan Cox target = 0; 1093ebcddc72SAlan Cox } 1094ebcddc72SAlan Cox launder = vm_background_launder_rate * PAGE_SIZE / 1024; 1095ebcddc72SAlan Cox launder /= VM_LAUNDER_RATE; 1096ebcddc72SAlan Cox if (launder > target) 1097ebcddc72SAlan Cox launder = target; 1098ebcddc72SAlan Cox } 1099ebcddc72SAlan Cox 1100ebcddc72SAlan Cox dolaundry: 1101ebcddc72SAlan Cox if (launder > 0) { 1102ebcddc72SAlan Cox /* 1103ebcddc72SAlan Cox * Because of I/O clustering, the number of laundered 1104ebcddc72SAlan Cox * pages could exceed "target" by the maximum size of 1105ebcddc72SAlan Cox * a cluster minus one. 1106ebcddc72SAlan Cox */ 1107e2068d0bSJeff Roberson target -= min(vm_pageout_launder(vmd, launder, 1108ebcddc72SAlan Cox in_shortfall), target); 1109ebcddc72SAlan Cox pause("laundp", hz / VM_LAUNDER_RATE); 1110ebcddc72SAlan Cox } 1111ebcddc72SAlan Cox 1112ebcddc72SAlan Cox /* 1113ebcddc72SAlan Cox * If we're not currently laundering pages and the page daemon 1114ebcddc72SAlan Cox * hasn't posted a new request, sleep until the page daemon 1115ebcddc72SAlan Cox * kicks us. 1116ebcddc72SAlan Cox */ 1117ebcddc72SAlan Cox vm_pagequeue_lock(pq); 1118e2068d0bSJeff Roberson if (target == 0 && vmd->vmd_laundry_request == VM_LAUNDRY_IDLE) 1119e2068d0bSJeff Roberson (void)mtx_sleep(&vmd->vmd_laundry_request, 1120ebcddc72SAlan Cox vm_pagequeue_lockptr(pq), PVM, "launds", 0); 1121ebcddc72SAlan Cox 1122ebcddc72SAlan Cox /* 1123ebcddc72SAlan Cox * If the pagedaemon has indicated that it's in shortfall, start 1124ebcddc72SAlan Cox * a shortfall laundering unless we're already in the middle of 1125ebcddc72SAlan Cox * one. This may preempt a background laundering. 1126ebcddc72SAlan Cox */ 1127e2068d0bSJeff Roberson if (vmd->vmd_laundry_request == VM_LAUNDRY_SHORTFALL && 1128ebcddc72SAlan Cox (!in_shortfall || shortfall_cycle == 0)) { 1129e2068d0bSJeff Roberson shortfall = vm_laundry_target(vmd) + 1130e2068d0bSJeff Roberson vmd->vmd_pageout_deficit; 1131ebcddc72SAlan Cox target = 0; 1132ebcddc72SAlan Cox } else 1133ebcddc72SAlan Cox shortfall = 0; 1134ebcddc72SAlan Cox 1135ebcddc72SAlan Cox if (target == 0) 1136e2068d0bSJeff Roberson vmd->vmd_laundry_request = VM_LAUNDRY_IDLE; 113760684862SMark Johnston nfreed += vmd->vmd_clean_pages_freed; 113860684862SMark Johnston vmd->vmd_clean_pages_freed = 0; 1139ebcddc72SAlan Cox vm_pagequeue_unlock(pq); 1140ebcddc72SAlan Cox } 1141ebcddc72SAlan Cox } 1142ebcddc72SAlan Cox 1143be37ee79SMark Johnston /* 1144be37ee79SMark Johnston * Compute the number of pages we want to try to move from the 1145be37ee79SMark Johnston * active queue to either the inactive or laundry queue. 1146be37ee79SMark Johnston * 11477bb4634eSMark Johnston * When scanning active pages during a shortage, we make clean pages 11487bb4634eSMark Johnston * count more heavily towards the page shortage than dirty pages. 11497bb4634eSMark Johnston * This is because dirty pages must be laundered before they can be 11507bb4634eSMark Johnston * reused and thus have less utility when attempting to quickly 11517bb4634eSMark Johnston * alleviate a free page shortage. However, this weighting also 11527bb4634eSMark Johnston * causes the scan to deactivate dirty pages more aggressively, 11537bb4634eSMark Johnston * improving the effectiveness of clustering. 1154be37ee79SMark Johnston */ 1155be37ee79SMark Johnston static int 11567bb4634eSMark Johnston vm_pageout_active_target(struct vm_domain *vmd) 1157be37ee79SMark Johnston { 1158be37ee79SMark Johnston int shortage; 1159be37ee79SMark Johnston 1160be37ee79SMark Johnston shortage = vmd->vmd_inactive_target + vm_paging_target(vmd) - 1161be37ee79SMark Johnston (vmd->vmd_pagequeues[PQ_INACTIVE].pq_cnt + 1162be37ee79SMark Johnston vmd->vmd_pagequeues[PQ_LAUNDRY].pq_cnt / act_scan_laundry_weight); 1163be37ee79SMark Johnston shortage *= act_scan_laundry_weight; 1164be37ee79SMark Johnston return (shortage); 1165be37ee79SMark Johnston } 1166be37ee79SMark Johnston 1167be37ee79SMark Johnston /* 1168be37ee79SMark Johnston * Scan the active queue. If there is no shortage of inactive pages, scan a 1169be37ee79SMark Johnston * small portion of the queue in order to maintain quasi-LRU. 1170be37ee79SMark Johnston */ 1171be37ee79SMark Johnston static void 1172be37ee79SMark Johnston vm_pageout_scan_active(struct vm_domain *vmd, int page_shortage) 1173be37ee79SMark Johnston { 1174be37ee79SMark Johnston struct scan_state ss; 1175fee2a2faSMark Johnston vm_object_t object; 1176be37ee79SMark Johnston vm_page_t m, marker; 1177be37ee79SMark Johnston struct vm_pagequeue *pq; 1178f3f38e25SMark Johnston vm_page_astate_t old, new; 1179be37ee79SMark Johnston long min_scan; 1180f3f38e25SMark Johnston int act_delta, max_scan, ps_delta, refs, scan_tick; 1181f3f38e25SMark Johnston uint8_t nqueue; 1182be37ee79SMark Johnston 1183be37ee79SMark Johnston marker = &vmd->vmd_markers[PQ_ACTIVE]; 1184be37ee79SMark Johnston pq = &vmd->vmd_pagequeues[PQ_ACTIVE]; 1185be37ee79SMark Johnston vm_pagequeue_lock(pq); 1186be37ee79SMark Johnston 1187be37ee79SMark Johnston /* 1188be37ee79SMark Johnston * If we're just idle polling attempt to visit every 1189be37ee79SMark Johnston * active page within 'update_period' seconds. 1190be37ee79SMark Johnston */ 1191be37ee79SMark Johnston scan_tick = ticks; 1192be37ee79SMark Johnston if (vm_pageout_update_period != 0) { 1193be37ee79SMark Johnston min_scan = pq->pq_cnt; 1194be37ee79SMark Johnston min_scan *= scan_tick - vmd->vmd_last_active_scan; 1195be37ee79SMark Johnston min_scan /= hz * vm_pageout_update_period; 1196be37ee79SMark Johnston } else 1197be37ee79SMark Johnston min_scan = 0; 1198be37ee79SMark Johnston if (min_scan > 0 || (page_shortage > 0 && pq->pq_cnt > 0)) 1199be37ee79SMark Johnston vmd->vmd_last_active_scan = scan_tick; 1200be37ee79SMark Johnston 1201be37ee79SMark Johnston /* 1202be37ee79SMark Johnston * Scan the active queue for pages that can be deactivated. Update 1203be37ee79SMark Johnston * the per-page activity counter and use it to identify deactivation 1204be37ee79SMark Johnston * candidates. Held pages may be deactivated. 1205be37ee79SMark Johnston * 1206be37ee79SMark Johnston * To avoid requeuing each page that remains in the active queue, we 12077bb4634eSMark Johnston * implement the CLOCK algorithm. To keep the implementation of the 12087bb4634eSMark Johnston * enqueue operation consistent for all page queues, we use two hands, 12097bb4634eSMark Johnston * represented by marker pages. Scans begin at the first hand, which 12107bb4634eSMark Johnston * precedes the second hand in the queue. When the two hands meet, 12117bb4634eSMark Johnston * they are moved back to the head and tail of the queue, respectively, 12127bb4634eSMark Johnston * and scanning resumes. 1213be37ee79SMark Johnston */ 1214be37ee79SMark Johnston max_scan = page_shortage > 0 ? pq->pq_cnt : min_scan; 1215be37ee79SMark Johnston act_scan: 1216be37ee79SMark Johnston vm_pageout_init_scan(&ss, pq, marker, &vmd->vmd_clock[0], max_scan); 1217be37ee79SMark Johnston while ((m = vm_pageout_next(&ss, false)) != NULL) { 1218be37ee79SMark Johnston if (__predict_false(m == &vmd->vmd_clock[1])) { 1219be37ee79SMark Johnston vm_pagequeue_lock(pq); 1220be37ee79SMark Johnston TAILQ_REMOVE(&pq->pq_pl, &vmd->vmd_clock[0], plinks.q); 1221be37ee79SMark Johnston TAILQ_REMOVE(&pq->pq_pl, &vmd->vmd_clock[1], plinks.q); 1222be37ee79SMark Johnston TAILQ_INSERT_HEAD(&pq->pq_pl, &vmd->vmd_clock[0], 1223be37ee79SMark Johnston plinks.q); 1224be37ee79SMark Johnston TAILQ_INSERT_TAIL(&pq->pq_pl, &vmd->vmd_clock[1], 1225be37ee79SMark Johnston plinks.q); 1226be37ee79SMark Johnston max_scan -= ss.scanned; 1227be37ee79SMark Johnston vm_pageout_end_scan(&ss); 1228be37ee79SMark Johnston goto act_scan; 1229be37ee79SMark Johnston } 1230be37ee79SMark Johnston if (__predict_false((m->flags & PG_MARKER) != 0)) 1231be37ee79SMark Johnston continue; 1232be37ee79SMark Johnston 1233e8bcf696SMark Johnston /* 1234b7f30bffSMark Johnston * Don't touch a page that was removed from the queue after the 1235b7f30bffSMark Johnston * page queue lock was released. Otherwise, ensure that any 1236b7f30bffSMark Johnston * pending queue operations, such as dequeues for wired pages, 1237b7f30bffSMark Johnston * are handled. 1238e8bcf696SMark Johnston */ 1239b7f30bffSMark Johnston if (vm_pageout_defer(m, PQ_ACTIVE, true)) 1240e8bcf696SMark Johnston continue; 1241e8bcf696SMark Johnston 1242e8bcf696SMark Johnston /* 1243e8bcf696SMark Johnston * A page's object pointer may be set to NULL before 1244e8bcf696SMark Johnston * the object lock is acquired. 1245e8bcf696SMark Johnston */ 124623ed568cSMateusz Guzik object = atomic_load_ptr(&m->object); 1247fee2a2faSMark Johnston if (__predict_false(object == NULL)) 1248fee2a2faSMark Johnston /* 1249fee2a2faSMark Johnston * The page has been removed from its object. 1250fee2a2faSMark Johnston */ 1251fee2a2faSMark Johnston continue; 1252fee2a2faSMark Johnston 1253f3f38e25SMark Johnston /* Deferred free of swap space. */ 1254f3f38e25SMark Johnston if ((m->a.flags & PGA_SWAP_FREE) != 0 && 1255f3f38e25SMark Johnston VM_OBJECT_TRYWLOCK(object)) { 1256f3f38e25SMark Johnston if (m->object == object) 1257f3f38e25SMark Johnston vm_pager_page_unswapped(m); 1258f3f38e25SMark Johnston VM_OBJECT_WUNLOCK(object); 1259f3f38e25SMark Johnston } 1260f3f38e25SMark Johnston 1261fee2a2faSMark Johnston /* 1262be37ee79SMark Johnston * Check to see "how much" the page has been used. 1263d7aeb429SAlan Cox * 1264d7aeb429SAlan Cox * Test PGA_REFERENCED after calling pmap_ts_referenced() so 1265d7aeb429SAlan Cox * that a reference from a concurrently destroyed mapping is 1266d7aeb429SAlan Cox * observed here and now. 1267d7aeb429SAlan Cox * 1268e8bcf696SMark Johnston * Perform an unsynchronized object ref count check. While 1269e8bcf696SMark Johnston * the page lock ensures that the page is not reallocated to 1270e8bcf696SMark Johnston * another object, in particular, one with unmanaged mappings 1271e8bcf696SMark Johnston * that cannot support pmap_ts_referenced(), two races are, 1272be37ee79SMark Johnston * nonetheless, possible: 1273be37ee79SMark Johnston * 1) The count was transitioning to zero, but we saw a non- 1274e8bcf696SMark Johnston * zero value. pmap_ts_referenced() will return zero 1275e8bcf696SMark Johnston * because the page is not mapped. 1276e8bcf696SMark Johnston * 2) The count was transitioning to one, but we saw zero. 1277e8bcf696SMark Johnston * This race delays the detection of a new reference. At 1278e8bcf696SMark Johnston * worst, we will deactivate and reactivate the page. 1279be37ee79SMark Johnston */ 1280f3f38e25SMark Johnston refs = object->ref_count != 0 ? pmap_ts_referenced(m) : 0; 1281be37ee79SMark Johnston 1282f3f38e25SMark Johnston old = vm_page_astate_load(m); 1283f3f38e25SMark Johnston do { 1284f3f38e25SMark Johnston /* 1285f3f38e25SMark Johnston * Check to see if the page has been removed from the 1286f3f38e25SMark Johnston * queue since the first such check. Leave it alone if 1287f3f38e25SMark Johnston * so, discarding any references collected by 1288f3f38e25SMark Johnston * pmap_ts_referenced(). 1289f3f38e25SMark Johnston */ 1290609de97eSEric van Gyzen if (__predict_false(_vm_page_queue(old) == PQ_NONE)) { 1291609de97eSEric van Gyzen ps_delta = 0; 1292f3f38e25SMark Johnston break; 1293609de97eSEric van Gyzen } 1294a8081778SJeff Roberson 1295be37ee79SMark Johnston /* 1296be37ee79SMark Johnston * Advance or decay the act_count based on recent usage. 1297be37ee79SMark Johnston */ 1298f3f38e25SMark Johnston new = old; 1299f3f38e25SMark Johnston act_delta = refs; 1300f3f38e25SMark Johnston if ((old.flags & PGA_REFERENCED) != 0) { 1301f3f38e25SMark Johnston new.flags &= ~PGA_REFERENCED; 1302f3f38e25SMark Johnston act_delta++; 1303f3f38e25SMark Johnston } 1304be37ee79SMark Johnston if (act_delta != 0) { 1305f3f38e25SMark Johnston new.act_count += ACT_ADVANCE + act_delta; 1306f3f38e25SMark Johnston if (new.act_count > ACT_MAX) 1307f3f38e25SMark Johnston new.act_count = ACT_MAX; 1308f3f38e25SMark Johnston } else { 1309f3f38e25SMark Johnston new.act_count -= min(new.act_count, 1310f3f38e25SMark Johnston ACT_DECLINE); 1311f3f38e25SMark Johnston } 1312be37ee79SMark Johnston 1313f3f38e25SMark Johnston if (new.act_count > 0) { 1314be37ee79SMark Johnston /* 1315f3f38e25SMark Johnston * Adjust the activation count and keep the page 1316f3f38e25SMark Johnston * in the active queue. The count might be left 1317f3f38e25SMark Johnston * unchanged if it is saturated. The page may 1318f3f38e25SMark Johnston * have been moved to a different queue since we 1319f3f38e25SMark Johnston * started the scan, in which case we move it 1320f3f38e25SMark Johnston * back. 1321be37ee79SMark Johnston */ 1322f3f38e25SMark Johnston ps_delta = 0; 1323f3f38e25SMark Johnston if (old.queue != PQ_ACTIVE) { 1324f7607c30SMark Johnston new.flags &= ~PGA_QUEUE_OP_MASK; 1325f7607c30SMark Johnston new.flags |= PGA_REQUEUE; 1326f7607c30SMark Johnston new.queue = PQ_ACTIVE; 1327f3f38e25SMark Johnston } 13287cdeaf33SMark Johnston } else { 1329be37ee79SMark Johnston /* 1330f3f38e25SMark Johnston * When not short for inactive pages, let dirty 1331f3f38e25SMark Johnston * pages go through the inactive queue before 1332f3f38e25SMark Johnston * moving to the laundry queue. This gives them 1333f3f38e25SMark Johnston * some extra time to be reactivated, 1334f3f38e25SMark Johnston * potentially avoiding an expensive pageout. 1335f3f38e25SMark Johnston * However, during a page shortage, the inactive 1336f3f38e25SMark Johnston * queue is necessarily small, and so dirty 1337f3f38e25SMark Johnston * pages would only spend a trivial amount of 1338f3f38e25SMark Johnston * time in the inactive queue. Therefore, we 1339f3f38e25SMark Johnston * might as well place them directly in the 1340f3f38e25SMark Johnston * laundry queue to reduce queuing overhead. 1341f3f38e25SMark Johnston * 1342be37ee79SMark Johnston * Calling vm_page_test_dirty() here would 1343be37ee79SMark Johnston * require acquisition of the object's write 1344be37ee79SMark Johnston * lock. However, during a page shortage, 1345f3f38e25SMark Johnston * directing dirty pages into the laundry queue 1346f3f38e25SMark Johnston * is only an optimization and not a 1347be37ee79SMark Johnston * requirement. Therefore, we simply rely on 1348f3f38e25SMark Johnston * the opportunistic updates to the page's dirty 1349f3f38e25SMark Johnston * field by the pmap. 1350be37ee79SMark Johnston */ 1351f3f38e25SMark Johnston if (page_shortage <= 0) { 1352f3f38e25SMark Johnston nqueue = PQ_INACTIVE; 1353f3f38e25SMark Johnston ps_delta = 0; 1354f3f38e25SMark Johnston } else if (m->dirty == 0) { 1355f3f38e25SMark Johnston nqueue = PQ_INACTIVE; 1356f3f38e25SMark Johnston ps_delta = act_scan_laundry_weight; 1357be37ee79SMark Johnston } else { 1358f3f38e25SMark Johnston nqueue = PQ_LAUNDRY; 1359f3f38e25SMark Johnston ps_delta = 1; 1360be37ee79SMark Johnston } 1361f3f38e25SMark Johnston 1362f7607c30SMark Johnston new.flags &= ~PGA_QUEUE_OP_MASK; 1363f3f38e25SMark Johnston new.flags |= PGA_REQUEUE; 1364f3f38e25SMark Johnston new.queue = nqueue; 1365be37ee79SMark Johnston } 1366f3f38e25SMark Johnston } while (!vm_page_pqstate_commit(m, &old, new)); 1367f3f38e25SMark Johnston 1368f3f38e25SMark Johnston page_shortage -= ps_delta; 1369be37ee79SMark Johnston } 1370be37ee79SMark Johnston vm_pagequeue_lock(pq); 1371be37ee79SMark Johnston TAILQ_REMOVE(&pq->pq_pl, &vmd->vmd_clock[0], plinks.q); 1372be37ee79SMark Johnston TAILQ_INSERT_AFTER(&pq->pq_pl, marker, &vmd->vmd_clock[0], plinks.q); 1373be37ee79SMark Johnston vm_pageout_end_scan(&ss); 1374be37ee79SMark Johnston vm_pagequeue_unlock(pq); 1375be37ee79SMark Johnston } 1376be37ee79SMark Johnston 13775cd29d0fSMark Johnston static int 1378f3f38e25SMark Johnston vm_pageout_reinsert_inactive_page(struct vm_pagequeue *pq, vm_page_t marker, 1379f3f38e25SMark Johnston vm_page_t m) 13805cd29d0fSMark Johnston { 1381f3f38e25SMark Johnston vm_page_astate_t as; 13825cd29d0fSMark Johnston 1383f3f38e25SMark Johnston vm_pagequeue_assert_locked(pq); 1384f3f38e25SMark Johnston 1385f3f38e25SMark Johnston as = vm_page_astate_load(m); 1386f3f38e25SMark Johnston if (as.queue != PQ_INACTIVE || (as.flags & PGA_ENQUEUED) != 0) 1387e8bcf696SMark Johnston return (0); 1388e8bcf696SMark Johnston vm_page_aflag_set(m, PGA_ENQUEUED); 1389f3f38e25SMark Johnston TAILQ_INSERT_BEFORE(marker, m, plinks.q); 13905cd29d0fSMark Johnston return (1); 13915cd29d0fSMark Johnston } 13925cd29d0fSMark Johnston 13935cd29d0fSMark Johnston /* 13945cd29d0fSMark Johnston * Re-add stuck pages to the inactive queue. We will examine them again 13955cd29d0fSMark Johnston * during the next scan. If the queue state of a page has changed since 13965cd29d0fSMark Johnston * it was physically removed from the page queue in 13975cd29d0fSMark Johnston * vm_pageout_collect_batch(), don't do anything with that page. 13985cd29d0fSMark Johnston */ 13995cd29d0fSMark Johnston static void 14005cd29d0fSMark Johnston vm_pageout_reinsert_inactive(struct scan_state *ss, struct vm_batchqueue *bq, 14015cd29d0fSMark Johnston vm_page_t m) 14025cd29d0fSMark Johnston { 14035cd29d0fSMark Johnston struct vm_pagequeue *pq; 1404f3f38e25SMark Johnston vm_page_t marker; 14055cd29d0fSMark Johnston int delta; 14065cd29d0fSMark Johnston 14075cd29d0fSMark Johnston delta = 0; 1408f3f38e25SMark Johnston marker = ss->marker; 14095cd29d0fSMark Johnston pq = ss->pq; 14105cd29d0fSMark Johnston 14115cd29d0fSMark Johnston if (m != NULL) { 14125cd29d0fSMark Johnston if (vm_batchqueue_insert(bq, m)) 14135cd29d0fSMark Johnston return; 14145cd29d0fSMark Johnston vm_pagequeue_lock(pq); 1415f3f38e25SMark Johnston delta += vm_pageout_reinsert_inactive_page(pq, marker, m); 14165cd29d0fSMark Johnston } else 14175cd29d0fSMark Johnston vm_pagequeue_lock(pq); 14185cd29d0fSMark Johnston while ((m = vm_batchqueue_pop(bq)) != NULL) 1419f3f38e25SMark Johnston delta += vm_pageout_reinsert_inactive_page(pq, marker, m); 14205cd29d0fSMark Johnston vm_pagequeue_cnt_add(pq, delta); 14215cd29d0fSMark Johnston vm_pagequeue_unlock(pq); 14225cd29d0fSMark Johnston vm_batchqueue_init(bq); 14235cd29d0fSMark Johnston } 14245cd29d0fSMark Johnston 14250292c54bSConrad Meyer static void 14260292c54bSConrad Meyer vm_pageout_scan_inactive(struct vm_domain *vmd, int page_shortage) 1427df8bae1dSRodney W. Grimes { 14280292c54bSConrad Meyer struct timeval start, end; 14295cd29d0fSMark Johnston struct scan_state ss; 14305cd29d0fSMark Johnston struct vm_batchqueue rq; 14310292c54bSConrad Meyer struct vm_page marker_page; 14325cd29d0fSMark Johnston vm_page_t m, marker; 14338d220203SAlan Cox struct vm_pagequeue *pq; 1434df8bae1dSRodney W. Grimes vm_object_t object; 1435f3f38e25SMark Johnston vm_page_astate_t old, new; 14360292c54bSConrad Meyer int act_delta, addl_page_shortage, starting_page_shortage, refs; 14370292c54bSConrad Meyer 14380292c54bSConrad Meyer object = NULL; 14390292c54bSConrad Meyer vm_batchqueue_init(&rq); 14400292c54bSConrad Meyer getmicrouptime(&start); 14410d94caffSDavid Greenman 1442df8bae1dSRodney W. Grimes /* 144301f04471SMark Johnston * The addl_page_shortage is an estimate of the number of temporarily 1444311e34e2SKonstantin Belousov * stuck pages in the inactive queue. In other words, the 1445449c2e92SKonstantin Belousov * number of pages from the inactive count that should be 1446311e34e2SKonstantin Belousov * discounted in setting the target for the active queue scan. 1447311e34e2SKonstantin Belousov */ 14489099545aSAlan Cox addl_page_shortage = 0; 14499099545aSAlan Cox 14501c7c3c6aSMatthew Dillon /* 1451f095d1bbSAlan Cox * Start scanning the inactive queue for pages that we can free. The 1452f095d1bbSAlan Cox * scan will stop when we reach the target or we have scanned the 14535cff1f4dSMark Johnston * entire queue. (Note that m->a.act_count is not used to make 1454f095d1bbSAlan Cox * decisions for the inactive queue, only for the active queue.) 14558d220203SAlan Cox */ 14560292c54bSConrad Meyer starting_page_shortage = page_shortage; 14570292c54bSConrad Meyer marker = &marker_page; 14580292c54bSConrad Meyer vm_page_init_marker(marker, PQ_INACTIVE, 0); 14595cd29d0fSMark Johnston pq = &vmd->vmd_pagequeues[PQ_INACTIVE]; 14608d220203SAlan Cox vm_pagequeue_lock(pq); 14615cd29d0fSMark Johnston vm_pageout_init_scan(&ss, pq, marker, NULL, pq->pq_cnt); 14625cd29d0fSMark Johnston while (page_shortage > 0 && (m = vm_pageout_next(&ss, true)) != NULL) { 14635cd29d0fSMark Johnston KASSERT((m->flags & PG_MARKER) == 0, 14645cd29d0fSMark Johnston ("marker page %p was dequeued", m)); 1465df8bae1dSRodney W. Grimes 1466936524aaSMatthew Dillon /* 1467b7f30bffSMark Johnston * Don't touch a page that was removed from the queue after the 1468b7f30bffSMark Johnston * page queue lock was released. Otherwise, ensure that any 1469b7f30bffSMark Johnston * pending queue operations, such as dequeues for wired pages, 1470b7f30bffSMark Johnston * are handled. 1471936524aaSMatthew Dillon */ 1472b7f30bffSMark Johnston if (vm_pageout_defer(m, PQ_INACTIVE, false)) 1473936524aaSMatthew Dillon continue; 1474e8bcf696SMark Johnston 14759f5632e6SMark Johnston /* 14769f5632e6SMark Johnston * Lock the page's object. 14779f5632e6SMark Johnston */ 14789f5632e6SMark Johnston if (object == NULL || object != m->object) { 147960256604SMark Johnston if (object != NULL) 14805cd29d0fSMark Johnston VM_OBJECT_WUNLOCK(object); 148123ed568cSMateusz Guzik object = atomic_load_ptr(&m->object); 14829f5632e6SMark Johnston if (__predict_false(object == NULL)) 14839f5632e6SMark Johnston /* The page is being freed by another thread. */ 14849f5632e6SMark Johnston continue; 14859f5632e6SMark Johnston 1486e8bcf696SMark Johnston /* Depends on type-stability. */ 148741fd4b94SMark Johnston VM_OBJECT_WLOCK(object); 14889f5632e6SMark Johnston if (__predict_false(m->object != object)) { 14899f5632e6SMark Johnston VM_OBJECT_WUNLOCK(object); 14909f5632e6SMark Johnston object = NULL; 14919f5632e6SMark Johnston goto reinsert; 149241fd4b94SMark Johnston } 149341fd4b94SMark Johnston } 14945cd29d0fSMark Johnston 149563e97555SJeff Roberson if (vm_page_tryxbusy(m) == 0) { 1496a3aeedabSAlan Cox /* 1497a3aeedabSAlan Cox * Don't mess with busy pages. Leave them at 1498a3aeedabSAlan Cox * the front of the queue. Most likely, they 1499a3aeedabSAlan Cox * are being paged out and will leave the 1500a3aeedabSAlan Cox * queue shortly after the scan finishes. So, 1501a3aeedabSAlan Cox * they ought to be discounted from the 1502a3aeedabSAlan Cox * inactive count. 1503a3aeedabSAlan Cox */ 1504a3aeedabSAlan Cox addl_page_shortage++; 15055cd29d0fSMark Johnston goto reinsert; 150626f9a767SRodney W. Grimes } 150748cc2fc7SKonstantin Belousov 1508a8081778SJeff Roberson /* Deferred free of swap space. */ 1509a8081778SJeff Roberson if ((m->a.flags & PGA_SWAP_FREE) != 0) 1510a8081778SJeff Roberson vm_pager_page_unswapped(m); 1511a8081778SJeff Roberson 151248cc2fc7SKonstantin Belousov /* 15139f5632e6SMark Johnston * Check for wirings now that we hold the object lock and have 15149f5632e6SMark Johnston * exclusively busied the page. If the page is mapped, it may 15159f5632e6SMark Johnston * still be wired by pmap lookups. The call to 1516fee2a2faSMark Johnston * vm_page_try_remove_all() below atomically checks for such 1517fee2a2faSMark Johnston * wirings and removes mappings. If the page is unmapped, the 15189f5632e6SMark Johnston * wire count is guaranteed not to increase after this check. 1519fee2a2faSMark Johnston */ 15209f5632e6SMark Johnston if (__predict_false(vm_page_wired(m))) 1521f3f38e25SMark Johnston goto skip_page; 1522fee2a2faSMark Johnston 1523fee2a2faSMark Johnston /* 15248748f58cSKonstantin Belousov * Invalid pages can be easily freed. They cannot be 15258748f58cSKonstantin Belousov * mapped, vm_page_free() asserts this. 1526776f729cSKonstantin Belousov */ 15270012f373SJeff Roberson if (vm_page_none_valid(m)) 15288748f58cSKonstantin Belousov goto free_page; 1529776f729cSKonstantin Belousov 1530f3f38e25SMark Johnston refs = object->ref_count != 0 ? pmap_ts_referenced(m) : 0; 1531f3f38e25SMark Johnston 1532f3f38e25SMark Johnston for (old = vm_page_astate_load(m);;) { 1533776f729cSKonstantin Belousov /* 1534f3f38e25SMark Johnston * Check to see if the page has been removed from the 1535f3f38e25SMark Johnston * queue since the first such check. Leave it alone if 1536f3f38e25SMark Johnston * so, discarding any references collected by 1537f3f38e25SMark Johnston * pmap_ts_referenced(). 15387e006499SJohn Dyson */ 1539f3f38e25SMark Johnston if (__predict_false(_vm_page_queue(old) == PQ_NONE)) 1540f3f38e25SMark Johnston goto skip_page; 1541f3f38e25SMark Johnston 1542f3f38e25SMark Johnston new = old; 1543f3f38e25SMark Johnston act_delta = refs; 1544f3f38e25SMark Johnston if ((old.flags & PGA_REFERENCED) != 0) { 1545f3f38e25SMark Johnston new.flags &= ~PGA_REFERENCED; 1546d7aeb429SAlan Cox act_delta++; 15472fe6e4d7SDavid Greenman } 1548f3f38e25SMark Johnston if (act_delta == 0) { 1549f3f38e25SMark Johnston ; 1550f3f38e25SMark Johnston } else if (object->ref_count != 0) { 1551e8bcf696SMark Johnston /* 1552f3f38e25SMark Johnston * Increase the activation count if the 1553f3f38e25SMark Johnston * page was referenced while in the 1554f3f38e25SMark Johnston * inactive queue. This makes it less 1555f3f38e25SMark Johnston * likely that the page will be returned 1556f3f38e25SMark Johnston * prematurely to the inactive queue. 1557e8bcf696SMark Johnston */ 1558f3f38e25SMark Johnston new.act_count += ACT_ADVANCE + 1559f3f38e25SMark Johnston act_delta; 1560f3f38e25SMark Johnston if (new.act_count > ACT_MAX) 1561f3f38e25SMark Johnston new.act_count = ACT_MAX; 1562f3f38e25SMark Johnston 1563f7607c30SMark Johnston new.flags &= ~PGA_QUEUE_OP_MASK; 1564f3f38e25SMark Johnston new.flags |= PGA_REQUEUE; 1565f3f38e25SMark Johnston new.queue = PQ_ACTIVE; 1566f3f38e25SMark Johnston if (!vm_page_pqstate_commit(m, &old, new)) 1567e8bcf696SMark Johnston continue; 1568f3f38e25SMark Johnston 1569f3f38e25SMark Johnston VM_CNT_INC(v_reactivated); 1570f3f38e25SMark Johnston goto skip_page; 1571ebcddc72SAlan Cox } else if ((object->flags & OBJ_DEAD) == 0) { 1572f3f38e25SMark Johnston new.queue = PQ_INACTIVE; 1573f3f38e25SMark Johnston new.flags |= PGA_REQUEUE; 1574f3f38e25SMark Johnston if (!vm_page_pqstate_commit(m, &old, new)) 1575f3f38e25SMark Johnston continue; 1576f3f38e25SMark Johnston goto skip_page; 1577ebcddc72SAlan Cox } 1578f3f38e25SMark Johnston break; 1579960810ccSAlan Cox } 158067bf6868SJohn Dyson 15817e006499SJohn Dyson /* 15829fc4739dSAlan Cox * If the page appears to be clean at the machine-independent 15839fc4739dSAlan Cox * layer, then remove all of its mappings from the pmap in 1584a766ffd0SAlan Cox * anticipation of freeing it. If, however, any of the page's 1585a766ffd0SAlan Cox * mappings allow write access, then the page may still be 1586a766ffd0SAlan Cox * modified until the last of those mappings are removed. 15877e006499SJohn Dyson */ 1588aa044135SAlan Cox if (object->ref_count != 0) { 15899fc4739dSAlan Cox vm_page_test_dirty(m); 15909f5632e6SMark Johnston if (m->dirty == 0 && !vm_page_try_remove_all(m)) 1591f3f38e25SMark Johnston goto skip_page; 1592fee2a2faSMark Johnston } 1593dcbcd518SBruce Evans 15946989c456SAlan Cox /* 1595ebcddc72SAlan Cox * Clean pages can be freed, but dirty pages must be sent back 1596ebcddc72SAlan Cox * to the laundry, unless they belong to a dead object. 1597ebcddc72SAlan Cox * Requeueing dirty pages from dead objects is pointless, as 1598ebcddc72SAlan Cox * they are being paged out and freed by the thread that 1599ebcddc72SAlan Cox * destroyed the object. 16006989c456SAlan Cox */ 1601ebcddc72SAlan Cox if (m->dirty == 0) { 16028748f58cSKonstantin Belousov free_page: 16035cd29d0fSMark Johnston /* 16049f5632e6SMark Johnston * Now we are guaranteed that no other threads are 16059f5632e6SMark Johnston * manipulating the page, check for a last-second 16069f5632e6SMark Johnston * reference that would save it from doom. 16075cd29d0fSMark Johnston */ 16089f5632e6SMark Johnston if (vm_pageout_defer(m, PQ_INACTIVE, false)) 16099f5632e6SMark Johnston goto skip_page; 16109f5632e6SMark Johnston 16119f5632e6SMark Johnston /* 16129f5632e6SMark Johnston * Because we dequeued the page and have already checked 16139f5632e6SMark Johnston * for pending dequeue and enqueue requests, we can 16149f5632e6SMark Johnston * safely disassociate the page from the inactive queue 16159f5632e6SMark Johnston * without holding the queue lock. 16169f5632e6SMark Johnston */ 16175cff1f4dSMark Johnston m->a.queue = PQ_NONE; 161878afdce6SAlan Cox vm_page_free(m); 16195cd29d0fSMark Johnston page_shortage--; 162063e97555SJeff Roberson continue; 162163e97555SJeff Roberson } 162263e97555SJeff Roberson if ((object->flags & OBJ_DEAD) == 0) 1623ebcddc72SAlan Cox vm_page_launder(m); 1624f3f38e25SMark Johnston skip_page: 1625f3f38e25SMark Johnston vm_page_xunbusy(m); 16265cd29d0fSMark Johnston continue; 16275cd29d0fSMark Johnston reinsert: 16285cd29d0fSMark Johnston vm_pageout_reinsert_inactive(&ss, &rq, m); 16295cd29d0fSMark Johnston } 163060256604SMark Johnston if (object != NULL) 163189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 16325cd29d0fSMark Johnston vm_pageout_reinsert_inactive(&ss, &rq, NULL); 16335cd29d0fSMark Johnston vm_pageout_reinsert_inactive(&ss, &ss.bq, NULL); 16348d220203SAlan Cox vm_pagequeue_lock(pq); 16355cd29d0fSMark Johnston vm_pageout_end_scan(&ss); 16368d220203SAlan Cox vm_pagequeue_unlock(pq); 163726f9a767SRodney W. Grimes 16380292c54bSConrad Meyer /* 16390292c54bSConrad Meyer * Record the remaining shortage and the progress and rate it was made. 16400292c54bSConrad Meyer */ 16410292c54bSConrad Meyer atomic_add_int(&vmd->vmd_addl_shortage, addl_page_shortage); 16420292c54bSConrad Meyer getmicrouptime(&end); 16430292c54bSConrad Meyer timevalsub(&end, &start); 16440292c54bSConrad Meyer atomic_add_int(&vmd->vmd_inactive_us, 16450292c54bSConrad Meyer end.tv_sec * 1000000 + end.tv_usec); 16460292c54bSConrad Meyer atomic_add_int(&vmd->vmd_inactive_freed, 16470292c54bSConrad Meyer starting_page_shortage - page_shortage); 16480292c54bSConrad Meyer } 16490292c54bSConrad Meyer 16500292c54bSConrad Meyer /* 16510292c54bSConrad Meyer * Dispatch a number of inactive threads according to load and collect the 16520292c54bSConrad Meyer * results to prevent a coherent (CEM: incoherent?) view of paging activity on 16530292c54bSConrad Meyer * this domain. 16540292c54bSConrad Meyer */ 16550292c54bSConrad Meyer static int 16560292c54bSConrad Meyer vm_pageout_inactive_dispatch(struct vm_domain *vmd, int shortage) 16570292c54bSConrad Meyer { 16580292c54bSConrad Meyer u_int freed, pps, threads, us; 16590292c54bSConrad Meyer 16600292c54bSConrad Meyer vmd->vmd_inactive_shortage = shortage; 16610292c54bSConrad Meyer 16620292c54bSConrad Meyer /* 16630292c54bSConrad Meyer * If we have more work than we can do in a quarter of our interval, we 16640292c54bSConrad Meyer * fire off multiple threads to process it. 16650292c54bSConrad Meyer */ 16660292c54bSConrad Meyer if (vmd->vmd_inactive_threads > 1 && vmd->vmd_inactive_pps != 0 && 16670292c54bSConrad Meyer shortage > vmd->vmd_inactive_pps / VM_INACT_SCAN_RATE / 4) { 16680292c54bSConrad Meyer threads = vmd->vmd_inactive_threads; 16690292c54bSConrad Meyer vm_domain_pageout_lock(vmd); 16700292c54bSConrad Meyer vmd->vmd_inactive_shortage /= threads; 16710292c54bSConrad Meyer blockcount_acquire(&vmd->vmd_inactive_starting, threads - 1); 16720292c54bSConrad Meyer blockcount_acquire(&vmd->vmd_inactive_running, threads - 1); 16730292c54bSConrad Meyer wakeup(&vmd->vmd_inactive_shortage); 16740292c54bSConrad Meyer vm_domain_pageout_unlock(vmd); 16750292c54bSConrad Meyer } 16760292c54bSConrad Meyer 16770292c54bSConrad Meyer /* Run the local thread scan. */ 16780292c54bSConrad Meyer vm_pageout_scan_inactive(vmd, vmd->vmd_inactive_shortage); 16790292c54bSConrad Meyer 16800292c54bSConrad Meyer /* 16810292c54bSConrad Meyer * Block until helper threads report results and then accumulate 16820292c54bSConrad Meyer * totals. 16830292c54bSConrad Meyer */ 16840292c54bSConrad Meyer blockcount_wait(&vmd->vmd_inactive_running, NULL, "vmpoid", PVM); 16850292c54bSConrad Meyer freed = atomic_readandclear_int(&vmd->vmd_inactive_freed); 16860292c54bSConrad Meyer VM_CNT_ADD(v_dfree, freed); 16870292c54bSConrad Meyer 16880292c54bSConrad Meyer /* 16890292c54bSConrad Meyer * Calculate the per-thread paging rate with an exponential decay of 16900292c54bSConrad Meyer * prior results. Careful to avoid integer rounding errors with large 16910292c54bSConrad Meyer * us values. 16920292c54bSConrad Meyer */ 16930292c54bSConrad Meyer us = max(atomic_readandclear_int(&vmd->vmd_inactive_us), 1); 16940292c54bSConrad Meyer if (us > 1000000) 16950292c54bSConrad Meyer /* Keep rounding to tenths */ 16960292c54bSConrad Meyer pps = (freed * 10) / ((us * 10) / 1000000); 16970292c54bSConrad Meyer else 16980292c54bSConrad Meyer pps = (1000000 / us) * freed; 16990292c54bSConrad Meyer vmd->vmd_inactive_pps = (vmd->vmd_inactive_pps / 2) + (pps / 2); 17000292c54bSConrad Meyer 17010292c54bSConrad Meyer return (shortage - freed); 17020292c54bSConrad Meyer } 17030292c54bSConrad Meyer 17040292c54bSConrad Meyer /* 17050292c54bSConrad Meyer * Attempt to reclaim the requested number of pages from the inactive queue. 17060292c54bSConrad Meyer * Returns true if the shortage was addressed. 17070292c54bSConrad Meyer */ 17080292c54bSConrad Meyer static int 17090292c54bSConrad Meyer vm_pageout_inactive(struct vm_domain *vmd, int shortage, int *addl_shortage) 17100292c54bSConrad Meyer { 17110292c54bSConrad Meyer struct vm_pagequeue *pq; 17120292c54bSConrad Meyer u_int addl_page_shortage, deficit, page_shortage; 17130292c54bSConrad Meyer u_int starting_page_shortage; 17140292c54bSConrad Meyer 17150292c54bSConrad Meyer /* 17160292c54bSConrad Meyer * vmd_pageout_deficit counts the number of pages requested in 17170292c54bSConrad Meyer * allocations that failed because of a free page shortage. We assume 17180292c54bSConrad Meyer * that the allocations will be reattempted and thus include the deficit 17190292c54bSConrad Meyer * in our scan target. 17200292c54bSConrad Meyer */ 17210292c54bSConrad Meyer deficit = atomic_readandclear_int(&vmd->vmd_pageout_deficit); 17220292c54bSConrad Meyer starting_page_shortage = shortage + deficit; 17230292c54bSConrad Meyer 17240292c54bSConrad Meyer /* 17250292c54bSConrad Meyer * Run the inactive scan on as many threads as is necessary. 17260292c54bSConrad Meyer */ 17270292c54bSConrad Meyer page_shortage = vm_pageout_inactive_dispatch(vmd, starting_page_shortage); 17280292c54bSConrad Meyer addl_page_shortage = atomic_readandclear_int(&vmd->vmd_addl_shortage); 17295cd29d0fSMark Johnston 1730ebcddc72SAlan Cox /* 1731ebcddc72SAlan Cox * Wake up the laundry thread so that it can perform any needed 1732ebcddc72SAlan Cox * laundering. If we didn't meet our target, we're in shortfall and 1733b1fd102eSMark Johnston * need to launder more aggressively. If PQ_LAUNDRY is empty and no 1734b1fd102eSMark Johnston * swap devices are configured, the laundry thread has no work to do, so 1735b1fd102eSMark Johnston * don't bother waking it up. 1736cb35676eSMark Johnston * 1737cb35676eSMark Johnston * The laundry thread uses the number of inactive queue scans elapsed 1738cb35676eSMark Johnston * since the last laundering to determine whether to launder again, so 1739cb35676eSMark Johnston * keep count. 1740ebcddc72SAlan Cox */ 1741cb35676eSMark Johnston if (starting_page_shortage > 0) { 1742e2068d0bSJeff Roberson pq = &vmd->vmd_pagequeues[PQ_LAUNDRY]; 1743ebcddc72SAlan Cox vm_pagequeue_lock(pq); 1744e2068d0bSJeff Roberson if (vmd->vmd_laundry_request == VM_LAUNDRY_IDLE && 1745cb35676eSMark Johnston (pq->pq_cnt > 0 || atomic_load_acq_int(&swapdev_enabled))) { 1746ebcddc72SAlan Cox if (page_shortage > 0) { 1747e2068d0bSJeff Roberson vmd->vmd_laundry_request = VM_LAUNDRY_SHORTFALL; 174883c9dea1SGleb Smirnoff VM_CNT_INC(v_pdshortfalls); 1749e2068d0bSJeff Roberson } else if (vmd->vmd_laundry_request != 1750e2068d0bSJeff Roberson VM_LAUNDRY_SHORTFALL) 1751e2068d0bSJeff Roberson vmd->vmd_laundry_request = 1752e2068d0bSJeff Roberson VM_LAUNDRY_BACKGROUND; 1753e2068d0bSJeff Roberson wakeup(&vmd->vmd_laundry_request); 1754b1fd102eSMark Johnston } 175560684862SMark Johnston vmd->vmd_clean_pages_freed += 175660684862SMark Johnston starting_page_shortage - page_shortage; 1757ebcddc72SAlan Cox vm_pagequeue_unlock(pq); 1758ebcddc72SAlan Cox } 1759ebcddc72SAlan Cox 17609452b5edSAlan Cox /* 1761f095d1bbSAlan Cox * Wakeup the swapout daemon if we didn't free the targeted number of 1762f095d1bbSAlan Cox * pages. 17639452b5edSAlan Cox */ 1764ac04195bSKonstantin Belousov if (page_shortage > 0) 1765ac04195bSKonstantin Belousov vm_swapout_run(); 17669452b5edSAlan 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 * Reclaim pages by swapping out idle processes, if configured to do so. 17751c7c3c6aSMatthew Dillon */ 1776ac04195bSKonstantin Belousov vm_swapout_run_idle(); 1777be37ee79SMark Johnston 1778be37ee79SMark Johnston /* 1779be37ee79SMark Johnston * See the description of addl_page_shortage above. 1780be37ee79SMark Johnston */ 1781be37ee79SMark Johnston *addl_shortage = addl_page_shortage + deficit; 1782be37ee79SMark Johnston 1783e57dd910SAlan Cox return (page_shortage <= 0); 17842025d69bSKonstantin Belousov } 17852025d69bSKonstantin Belousov 1786449c2e92SKonstantin Belousov static int vm_pageout_oom_vote; 1787449c2e92SKonstantin Belousov 1788449c2e92SKonstantin Belousov /* 1789449c2e92SKonstantin Belousov * The pagedaemon threads randlomly select one to perform the 1790449c2e92SKonstantin Belousov * OOM. Trying to kill processes before all pagedaemons 1791449c2e92SKonstantin Belousov * failed to reach free target is premature. 1792449c2e92SKonstantin Belousov */ 1793449c2e92SKonstantin Belousov static void 179476386c7eSKonstantin Belousov vm_pageout_mightbe_oom(struct vm_domain *vmd, int page_shortage, 179576386c7eSKonstantin Belousov int starting_page_shortage) 1796449c2e92SKonstantin Belousov { 1797449c2e92SKonstantin Belousov int old_vote; 1798449c2e92SKonstantin Belousov 179976386c7eSKonstantin Belousov if (starting_page_shortage <= 0 || starting_page_shortage != 180076386c7eSKonstantin Belousov page_shortage) 180176386c7eSKonstantin Belousov vmd->vmd_oom_seq = 0; 180276386c7eSKonstantin Belousov else 180376386c7eSKonstantin Belousov vmd->vmd_oom_seq++; 180476386c7eSKonstantin Belousov if (vmd->vmd_oom_seq < vm_pageout_oom_seq) { 1805449c2e92SKonstantin Belousov if (vmd->vmd_oom) { 1806449c2e92SKonstantin Belousov vmd->vmd_oom = FALSE; 1807449c2e92SKonstantin Belousov atomic_subtract_int(&vm_pageout_oom_vote, 1); 1808449c2e92SKonstantin Belousov } 1809449c2e92SKonstantin Belousov return; 1810449c2e92SKonstantin Belousov } 1811449c2e92SKonstantin Belousov 181276386c7eSKonstantin Belousov /* 181376386c7eSKonstantin Belousov * Do not follow the call sequence until OOM condition is 181476386c7eSKonstantin Belousov * cleared. 181576386c7eSKonstantin Belousov */ 181676386c7eSKonstantin Belousov vmd->vmd_oom_seq = 0; 181776386c7eSKonstantin Belousov 1818449c2e92SKonstantin Belousov if (vmd->vmd_oom) 1819449c2e92SKonstantin Belousov return; 1820449c2e92SKonstantin Belousov 1821449c2e92SKonstantin Belousov vmd->vmd_oom = TRUE; 1822449c2e92SKonstantin Belousov old_vote = atomic_fetchadd_int(&vm_pageout_oom_vote, 1); 1823449c2e92SKonstantin Belousov if (old_vote != vm_ndomains - 1) 1824449c2e92SKonstantin Belousov return; 1825449c2e92SKonstantin Belousov 1826449c2e92SKonstantin Belousov /* 1827449c2e92SKonstantin Belousov * The current pagedaemon thread is the last in the quorum to 1828449c2e92SKonstantin Belousov * start OOM. Initiate the selection and signaling of the 1829449c2e92SKonstantin Belousov * victim. 1830449c2e92SKonstantin Belousov */ 1831449c2e92SKonstantin Belousov vm_pageout_oom(VM_OOM_MEM); 1832449c2e92SKonstantin Belousov 1833449c2e92SKonstantin Belousov /* 1834449c2e92SKonstantin Belousov * After one round of OOM terror, recall our vote. On the 1835449c2e92SKonstantin Belousov * next pass, current pagedaemon would vote again if the low 1836449c2e92SKonstantin Belousov * memory condition is still there, due to vmd_oom being 1837449c2e92SKonstantin Belousov * false. 1838449c2e92SKonstantin Belousov */ 1839449c2e92SKonstantin Belousov vmd->vmd_oom = FALSE; 1840449c2e92SKonstantin Belousov atomic_subtract_int(&vm_pageout_oom_vote, 1); 1841449c2e92SKonstantin Belousov } 18422025d69bSKonstantin Belousov 18433949873fSKonstantin Belousov /* 18443949873fSKonstantin Belousov * The OOM killer is the page daemon's action of last resort when 18453949873fSKonstantin Belousov * memory allocation requests have been stalled for a prolonged period 18463949873fSKonstantin Belousov * of time because it cannot reclaim memory. This function computes 18473949873fSKonstantin Belousov * the approximate number of physical pages that could be reclaimed if 18483949873fSKonstantin Belousov * the specified address space is destroyed. 18493949873fSKonstantin Belousov * 18503949873fSKonstantin Belousov * Private, anonymous memory owned by the address space is the 18513949873fSKonstantin Belousov * principal resource that we expect to recover after an OOM kill. 18523949873fSKonstantin Belousov * Since the physical pages mapped by the address space's COW entries 18533949873fSKonstantin Belousov * are typically shared pages, they are unlikely to be released and so 18543949873fSKonstantin Belousov * they are not counted. 18553949873fSKonstantin Belousov * 18563949873fSKonstantin Belousov * To get to the point where the page daemon runs the OOM killer, its 18573949873fSKonstantin Belousov * efforts to write-back vnode-backed pages may have stalled. This 18583949873fSKonstantin Belousov * could be caused by a memory allocation deadlock in the write path 18593949873fSKonstantin Belousov * that might be resolved by an OOM kill. Therefore, physical pages 18603949873fSKonstantin Belousov * belonging to vnode-backed objects are counted, because they might 18613949873fSKonstantin Belousov * be freed without being written out first if the address space holds 18623949873fSKonstantin Belousov * the last reference to an unlinked vnode. 18633949873fSKonstantin Belousov * 18643949873fSKonstantin Belousov * Similarly, physical pages belonging to OBJT_PHYS objects are 18653949873fSKonstantin Belousov * counted because the address space might hold the last reference to 18663949873fSKonstantin Belousov * the object. 18673949873fSKonstantin Belousov */ 18683949873fSKonstantin Belousov static long 18693949873fSKonstantin Belousov vm_pageout_oom_pagecount(struct vmspace *vmspace) 18703949873fSKonstantin Belousov { 18713949873fSKonstantin Belousov vm_map_t map; 18723949873fSKonstantin Belousov vm_map_entry_t entry; 18733949873fSKonstantin Belousov vm_object_t obj; 18743949873fSKonstantin Belousov long res; 18753949873fSKonstantin Belousov 18763949873fSKonstantin Belousov map = &vmspace->vm_map; 18773949873fSKonstantin Belousov KASSERT(!map->system_map, ("system map")); 18783949873fSKonstantin Belousov sx_assert(&map->lock, SA_LOCKED); 18793949873fSKonstantin Belousov res = 0; 18802288078cSDoug Moore VM_MAP_ENTRY_FOREACH(entry, map) { 18813949873fSKonstantin Belousov if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0) 18823949873fSKonstantin Belousov continue; 18833949873fSKonstantin Belousov obj = entry->object.vm_object; 18843949873fSKonstantin Belousov if (obj == NULL) 18853949873fSKonstantin Belousov continue; 18863949873fSKonstantin Belousov if ((entry->eflags & MAP_ENTRY_NEEDS_COPY) != 0 && 18873949873fSKonstantin Belousov obj->ref_count != 1) 18883949873fSKonstantin Belousov continue; 18893949873fSKonstantin Belousov switch (obj->type) { 18903949873fSKonstantin Belousov case OBJT_DEFAULT: 18913949873fSKonstantin Belousov case OBJT_SWAP: 18923949873fSKonstantin Belousov case OBJT_PHYS: 18933949873fSKonstantin Belousov case OBJT_VNODE: 18943949873fSKonstantin Belousov res += obj->resident_page_count; 18953949873fSKonstantin Belousov break; 18963949873fSKonstantin Belousov } 18973949873fSKonstantin Belousov } 18983949873fSKonstantin Belousov return (res); 18993949873fSKonstantin Belousov } 19003949873fSKonstantin Belousov 1901245139c6SKonstantin Belousov static int vm_oom_ratelim_last; 1902245139c6SKonstantin Belousov static int vm_oom_pf_secs = 10; 1903245139c6SKonstantin Belousov SYSCTL_INT(_vm, OID_AUTO, oom_pf_secs, CTLFLAG_RWTUN, &vm_oom_pf_secs, 0, 1904245139c6SKonstantin Belousov ""); 1905245139c6SKonstantin Belousov static struct mtx vm_oom_ratelim_mtx; 1906245139c6SKonstantin Belousov 19072025d69bSKonstantin Belousov void 19082025d69bSKonstantin Belousov vm_pageout_oom(int shortage) 19092025d69bSKonstantin Belousov { 19102025d69bSKonstantin Belousov struct proc *p, *bigproc; 19112025d69bSKonstantin Belousov vm_offset_t size, bigsize; 19122025d69bSKonstantin Belousov struct thread *td; 19136bed074cSKonstantin Belousov struct vmspace *vm; 1914245139c6SKonstantin Belousov int now; 19153e78e983SAlan Cox bool breakout; 19162025d69bSKonstantin Belousov 19172025d69bSKonstantin Belousov /* 1918245139c6SKonstantin Belousov * For OOM requests originating from vm_fault(), there is a high 1919245139c6SKonstantin Belousov * chance that a single large process faults simultaneously in 1920245139c6SKonstantin Belousov * several threads. Also, on an active system running many 1921245139c6SKonstantin Belousov * processes of middle-size, like buildworld, all of them 1922245139c6SKonstantin Belousov * could fault almost simultaneously as well. 1923245139c6SKonstantin Belousov * 1924245139c6SKonstantin Belousov * To avoid killing too many processes, rate-limit OOMs 1925245139c6SKonstantin Belousov * initiated by vm_fault() time-outs on the waits for free 1926245139c6SKonstantin Belousov * pages. 1927245139c6SKonstantin Belousov */ 1928245139c6SKonstantin Belousov mtx_lock(&vm_oom_ratelim_mtx); 1929245139c6SKonstantin Belousov now = ticks; 1930245139c6SKonstantin Belousov if (shortage == VM_OOM_MEM_PF && 1931245139c6SKonstantin Belousov (u_int)(now - vm_oom_ratelim_last) < hz * vm_oom_pf_secs) { 1932245139c6SKonstantin Belousov mtx_unlock(&vm_oom_ratelim_mtx); 1933245139c6SKonstantin Belousov return; 1934245139c6SKonstantin Belousov } 1935245139c6SKonstantin Belousov vm_oom_ratelim_last = now; 1936245139c6SKonstantin Belousov mtx_unlock(&vm_oom_ratelim_mtx); 1937245139c6SKonstantin Belousov 1938245139c6SKonstantin Belousov /* 19391c58e4e5SJohn Baldwin * We keep the process bigproc locked once we find it to keep anyone 19401c58e4e5SJohn Baldwin * from messing with it; however, there is a possibility of 194128323addSBryan Drewery * deadlock if process B is bigproc and one of its child processes 19421c58e4e5SJohn Baldwin * attempts to propagate a signal to B while we are waiting for A's 19431c58e4e5SJohn Baldwin * lock while walking this list. To avoid this, we don't block on 19441c58e4e5SJohn Baldwin * the process lock but just skip a process if it is already locked. 19455663e6deSDavid Greenman */ 19465663e6deSDavid Greenman bigproc = NULL; 19475663e6deSDavid Greenman bigsize = 0; 19481005a129SJohn Baldwin sx_slock(&allproc_lock); 1949e602ba25SJulian Elischer FOREACH_PROC_IN_SYSTEM(p) { 195071943c3dSKonstantin Belousov PROC_LOCK(p); 195171943c3dSKonstantin Belousov 19521c58e4e5SJohn Baldwin /* 19533f1c4c4fSKonstantin Belousov * If this is a system, protected or killed process, skip it. 19545663e6deSDavid Greenman */ 195571943c3dSKonstantin Belousov if (p->p_state != PRS_NORMAL || (p->p_flag & (P_INEXEC | 195671943c3dSKonstantin Belousov P_PROTECTED | P_SYSTEM | P_WEXIT)) != 0 || 195771943c3dSKonstantin Belousov p->p_pid == 1 || P_KILLED(p) || 195871943c3dSKonstantin Belousov (p->p_pid < 48 && swap_pager_avail != 0)) { 19598606d880SJohn Baldwin PROC_UNLOCK(p); 19605663e6deSDavid Greenman continue; 19615663e6deSDavid Greenman } 19625663e6deSDavid Greenman /* 1963dcbcd518SBruce Evans * If the process is in a non-running type state, 1964e602ba25SJulian Elischer * don't touch it. Check all the threads individually. 19655663e6deSDavid Greenman */ 19663e78e983SAlan Cox breakout = false; 1967e602ba25SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 1968982d11f8SJeff Roberson thread_lock(td); 196971fad9fdSJulian Elischer if (!TD_ON_RUNQ(td) && 197071fad9fdSJulian Elischer !TD_IS_RUNNING(td) && 1971f497cda2SEdward Tomasz Napierala !TD_IS_SLEEPING(td) && 1972b98acc0aSKonstantin Belousov !TD_IS_SUSPENDED(td) && 1973b98acc0aSKonstantin Belousov !TD_IS_SWAPPED(td)) { 1974982d11f8SJeff Roberson thread_unlock(td); 19753e78e983SAlan Cox breakout = true; 1976e602ba25SJulian Elischer break; 1977e602ba25SJulian Elischer } 1978982d11f8SJeff Roberson thread_unlock(td); 1979e602ba25SJulian Elischer } 1980e602ba25SJulian Elischer if (breakout) { 19811c58e4e5SJohn Baldwin PROC_UNLOCK(p); 19825663e6deSDavid Greenman continue; 19835663e6deSDavid Greenman } 19845663e6deSDavid Greenman /* 19855663e6deSDavid Greenman * get the process size 19865663e6deSDavid Greenman */ 19876bed074cSKonstantin Belousov vm = vmspace_acquire_ref(p); 19886bed074cSKonstantin Belousov if (vm == NULL) { 19896bed074cSKonstantin Belousov PROC_UNLOCK(p); 19906bed074cSKonstantin Belousov continue; 19916bed074cSKonstantin Belousov } 199295e2409aSKonstantin Belousov _PHOLD_LITE(p); 199372d97679SDavid Schultz PROC_UNLOCK(p); 199495e2409aSKonstantin Belousov sx_sunlock(&allproc_lock); 199595e2409aSKonstantin Belousov if (!vm_map_trylock_read(&vm->vm_map)) { 199671943c3dSKonstantin Belousov vmspace_free(vm); 199795e2409aSKonstantin Belousov sx_slock(&allproc_lock); 199895e2409aSKonstantin Belousov PRELE(p); 199972d97679SDavid Schultz continue; 200072d97679SDavid Schultz } 20017981aa24SKonstantin Belousov size = vmspace_swap_count(vm); 2002245139c6SKonstantin Belousov if (shortage == VM_OOM_MEM || shortage == VM_OOM_MEM_PF) 20033949873fSKonstantin Belousov size += vm_pageout_oom_pagecount(vm); 20043949873fSKonstantin Belousov vm_map_unlock_read(&vm->vm_map); 20056bed074cSKonstantin Belousov vmspace_free(vm); 200695e2409aSKonstantin Belousov sx_slock(&allproc_lock); 20073949873fSKonstantin Belousov 20085663e6deSDavid Greenman /* 20093949873fSKonstantin Belousov * If this process is bigger than the biggest one, 20105663e6deSDavid Greenman * remember it. 20115663e6deSDavid Greenman */ 20125663e6deSDavid Greenman if (size > bigsize) { 20131c58e4e5SJohn Baldwin if (bigproc != NULL) 201471943c3dSKonstantin Belousov PRELE(bigproc); 20155663e6deSDavid Greenman bigproc = p; 20165663e6deSDavid Greenman bigsize = size; 201771943c3dSKonstantin Belousov } else { 201871943c3dSKonstantin Belousov PRELE(p); 201971943c3dSKonstantin Belousov } 20205663e6deSDavid Greenman } 20211005a129SJohn Baldwin sx_sunlock(&allproc_lock); 20225663e6deSDavid Greenman if (bigproc != NULL) { 20233c200db9SJonathan T. Looney if (vm_panic_on_oom != 0 && --vm_panic_on_oom == 0) 20248311a2b8SWill Andrews panic("out of swap space"); 202571943c3dSKonstantin Belousov PROC_LOCK(bigproc); 2026729b1e51SDavid Greenman killproc(bigproc, "out of swap space"); 2027fa885116SJulian Elischer sched_nice(bigproc, PRIO_MIN); 202871943c3dSKonstantin Belousov _PRELE(bigproc); 20291c58e4e5SJohn Baldwin PROC_UNLOCK(bigproc); 20305663e6deSDavid Greenman } 20315663e6deSDavid Greenman } 203226f9a767SRodney W. Grimes 20338fc25508SMark Johnston /* 20348fc25508SMark Johnston * Signal a free page shortage to subsystems that have registered an event 20358fc25508SMark Johnston * handler. Reclaim memory from UMA in the event of a severe shortage. 20368fc25508SMark Johnston * Return true if the free page count should be re-evaluated. 20378fc25508SMark Johnston */ 2038b50a4ea6SMark Johnston static bool 2039b50a4ea6SMark Johnston vm_pageout_lowmem(void) 204049a3710cSMark Johnston { 2041b50a4ea6SMark Johnston static int lowmem_ticks = 0; 2042b50a4ea6SMark Johnston int last; 20438fc25508SMark Johnston bool ret; 20448fc25508SMark Johnston 20458fc25508SMark Johnston ret = false; 204649a3710cSMark Johnston 2047b50a4ea6SMark Johnston last = atomic_load_int(&lowmem_ticks); 2048b50a4ea6SMark Johnston while ((u_int)(ticks - last) / hz >= lowmem_period) { 2049b50a4ea6SMark Johnston if (atomic_fcmpset_int(&lowmem_ticks, &last, ticks) == 0) 2050b50a4ea6SMark Johnston continue; 2051b50a4ea6SMark Johnston 205249a3710cSMark Johnston /* 205349a3710cSMark Johnston * Decrease registered cache sizes. 205449a3710cSMark Johnston */ 205549a3710cSMark Johnston SDT_PROBE0(vm, , , vm__lowmem_scan); 205649a3710cSMark Johnston EVENTHANDLER_INVOKE(vm_lowmem, VM_LOW_PAGES); 205749a3710cSMark Johnston 205849a3710cSMark Johnston /* 205949a3710cSMark Johnston * We do this explicitly after the caches have been 20608fc25508SMark Johnston * drained above. 206149a3710cSMark Johnston */ 20628fc25508SMark Johnston uma_reclaim(UMA_RECLAIM_TRIM); 20638fc25508SMark Johnston ret = true; 2064ace409ceSAlexander Motin break; 206549a3710cSMark Johnston } 20668fc25508SMark Johnston 20678fc25508SMark Johnston /* 20688fc25508SMark Johnston * Kick off an asynchronous reclaim of cached memory if one of the 20698fc25508SMark Johnston * page daemons is failing to keep up with demand. Use the "severe" 20708fc25508SMark Johnston * threshold instead of "min" to ensure that we do not blow away the 20718fc25508SMark Johnston * caches if a subset of the NUMA domains are depleted by kernel memory 20728fc25508SMark Johnston * allocations; the domainset iterators automatically skip domains 20738fc25508SMark Johnston * below the "min" threshold on the first pass. 20748fc25508SMark Johnston * 20758fc25508SMark Johnston * UMA reclaim worker has its own rate-limiting mechanism, so don't 20768fc25508SMark Johnston * worry about kicking it too often. 20778fc25508SMark Johnston */ 20788fc25508SMark Johnston if (vm_page_count_severe()) 20798fc25508SMark Johnston uma_reclaim_wakeup(); 20808fc25508SMark Johnston 20818fc25508SMark Johnston return (ret); 208249a3710cSMark Johnston } 208349a3710cSMark Johnston 208449a3710cSMark Johnston static void 2085449c2e92SKonstantin Belousov vm_pageout_worker(void *arg) 2086449c2e92SKonstantin Belousov { 2087e2068d0bSJeff Roberson struct vm_domain *vmd; 2088b50a4ea6SMark Johnston u_int ofree; 208949a3710cSMark Johnston int addl_shortage, domain, shortage; 2090e57dd910SAlan Cox bool target_met; 2091449c2e92SKonstantin Belousov 2092e2068d0bSJeff Roberson domain = (uintptr_t)arg; 2093e2068d0bSJeff Roberson vmd = VM_DOMAIN(domain); 20945f8cd1c0SJeff Roberson shortage = 0; 2095e57dd910SAlan Cox target_met = true; 2096449c2e92SKonstantin Belousov 2097449c2e92SKonstantin Belousov /* 2098949c9186SKonstantin Belousov * XXXKIB It could be useful to bind pageout daemon threads to 2099949c9186SKonstantin Belousov * the cores belonging to the domain, from which vm_page_array 2100949c9186SKonstantin Belousov * is allocated. 2101449c2e92SKonstantin Belousov */ 2102449c2e92SKonstantin Belousov 2103e2068d0bSJeff Roberson KASSERT(vmd->vmd_segs != 0, ("domain without segments")); 2104e2068d0bSJeff Roberson vmd->vmd_last_active_scan = ticks; 2105449c2e92SKonstantin Belousov 2106449c2e92SKonstantin Belousov /* 2107449c2e92SKonstantin Belousov * The pageout daemon worker is never done, so loop forever. 2108449c2e92SKonstantin Belousov */ 2109449c2e92SKonstantin Belousov while (TRUE) { 211030fbfddaSJeff Roberson vm_domain_pageout_lock(vmd); 211149a3710cSMark Johnston 211230fbfddaSJeff Roberson /* 211330fbfddaSJeff Roberson * We need to clear wanted before we check the limits. This 211430fbfddaSJeff Roberson * prevents races with wakers who will check wanted after they 211530fbfddaSJeff Roberson * reach the limit. 211630fbfddaSJeff Roberson */ 211730fbfddaSJeff Roberson atomic_store_int(&vmd->vmd_pageout_wanted, 0); 211856ce0690SAlan Cox 211956ce0690SAlan Cox /* 21205f8cd1c0SJeff Roberson * Might the page daemon need to run again? 2121449c2e92SKonstantin Belousov */ 21225f8cd1c0SJeff Roberson if (vm_paging_needed(vmd, vmd->vmd_free_count)) { 212356ce0690SAlan Cox /* 212449a3710cSMark Johnston * Yes. If the scan failed to produce enough free 212549a3710cSMark Johnston * pages, sleep uninterruptibly for some time in the 212649a3710cSMark Johnston * hope that the laundry thread will clean some pages. 212756ce0690SAlan Cox */ 212830fbfddaSJeff Roberson vm_domain_pageout_unlock(vmd); 212949a3710cSMark Johnston if (!target_met) 21306eebec83SMark Johnston pause("pwait", hz / VM_INACT_SCAN_RATE); 2131449c2e92SKonstantin Belousov } else { 2132449c2e92SKonstantin Belousov /* 21335f8cd1c0SJeff Roberson * No, sleep until the next wakeup or until pages 21345f8cd1c0SJeff Roberson * need to have their reference stats updated. 2135449c2e92SKonstantin Belousov */ 21362c0f13aaSKonstantin Belousov if (mtx_sleep(&vmd->vmd_pageout_wanted, 213730fbfddaSJeff Roberson vm_domain_pageout_lockptr(vmd), PDROP | PVM, 21385f8cd1c0SJeff Roberson "psleep", hz / VM_INACT_SCAN_RATE) == 0) 213983c9dea1SGleb Smirnoff VM_CNT_INC(v_pdwakeups); 214056ce0690SAlan Cox } 2141be37ee79SMark Johnston 214230fbfddaSJeff Roberson /* Prevent spurious wakeups by ensuring that wanted is set. */ 214330fbfddaSJeff Roberson atomic_store_int(&vmd->vmd_pageout_wanted, 1); 214430fbfddaSJeff Roberson 214530fbfddaSJeff Roberson /* 214630fbfddaSJeff Roberson * Use the controller to calculate how many pages to free in 2147b50a4ea6SMark Johnston * this interval, and scan the inactive queue. If the lowmem 2148b50a4ea6SMark Johnston * handlers appear to have freed up some pages, subtract the 2149b50a4ea6SMark Johnston * difference from the inactive queue scan target. 215030fbfddaSJeff Roberson */ 21515f8cd1c0SJeff Roberson shortage = pidctrl_daemon(&vmd->vmd_pid, vmd->vmd_free_count); 215249a3710cSMark Johnston if (shortage > 0) { 2153b50a4ea6SMark Johnston ofree = vmd->vmd_free_count; 2154b50a4ea6SMark Johnston if (vm_pageout_lowmem() && vmd->vmd_free_count > ofree) 2155b50a4ea6SMark Johnston shortage -= min(vmd->vmd_free_count - ofree, 2156b50a4ea6SMark Johnston (u_int)shortage); 21570292c54bSConrad Meyer target_met = vm_pageout_inactive(vmd, shortage, 2158be37ee79SMark Johnston &addl_shortage); 215949a3710cSMark Johnston } else 216049a3710cSMark Johnston addl_shortage = 0; 216156ce0690SAlan Cox 2162be37ee79SMark Johnston /* 2163be37ee79SMark Johnston * Scan the active queue. A positive value for shortage 2164be37ee79SMark Johnston * indicates that we must aggressively deactivate pages to avoid 2165be37ee79SMark Johnston * a shortfall. 2166be37ee79SMark Johnston */ 21677bb4634eSMark Johnston shortage = vm_pageout_active_target(vmd) + addl_shortage; 2168be37ee79SMark Johnston vm_pageout_scan_active(vmd, shortage); 2169449c2e92SKonstantin Belousov } 2170449c2e92SKonstantin Belousov } 2171449c2e92SKonstantin Belousov 2172df8bae1dSRodney W. Grimes /* 21730292c54bSConrad Meyer * vm_pageout_helper runs additional pageout daemons in times of high paging 21740292c54bSConrad Meyer * activity. 21750292c54bSConrad Meyer */ 21760292c54bSConrad Meyer static void 21770292c54bSConrad Meyer vm_pageout_helper(void *arg) 21780292c54bSConrad Meyer { 21790292c54bSConrad Meyer struct vm_domain *vmd; 21800292c54bSConrad Meyer int domain; 21810292c54bSConrad Meyer 21820292c54bSConrad Meyer domain = (uintptr_t)arg; 21830292c54bSConrad Meyer vmd = VM_DOMAIN(domain); 21840292c54bSConrad Meyer 21850292c54bSConrad Meyer vm_domain_pageout_lock(vmd); 21860292c54bSConrad Meyer for (;;) { 21870292c54bSConrad Meyer msleep(&vmd->vmd_inactive_shortage, 21880292c54bSConrad Meyer vm_domain_pageout_lockptr(vmd), PVM, "psleep", 0); 21890292c54bSConrad Meyer blockcount_release(&vmd->vmd_inactive_starting, 1); 21900292c54bSConrad Meyer 21910292c54bSConrad Meyer vm_domain_pageout_unlock(vmd); 21920292c54bSConrad Meyer vm_pageout_scan_inactive(vmd, vmd->vmd_inactive_shortage); 21930292c54bSConrad Meyer vm_domain_pageout_lock(vmd); 21940292c54bSConrad Meyer 21950292c54bSConrad Meyer /* 21960292c54bSConrad Meyer * Release the running count while the pageout lock is held to 21970292c54bSConrad Meyer * prevent wakeup races. 21980292c54bSConrad Meyer */ 21990292c54bSConrad Meyer blockcount_release(&vmd->vmd_inactive_running, 1); 22000292c54bSConrad Meyer } 22010292c54bSConrad Meyer } 22020292c54bSConrad Meyer 22030292c54bSConrad Meyer static int 220474f5530dSConrad Meyer get_pageout_threads_per_domain(const struct vm_domain *vmd) 22050292c54bSConrad Meyer { 220674f5530dSConrad Meyer unsigned total_pageout_threads, eligible_cpus, domain_cpus; 22070292c54bSConrad Meyer 220874f5530dSConrad Meyer if (VM_DOMAIN_EMPTY(vmd->vmd_domain)) 220974f5530dSConrad Meyer return (0); 22100292c54bSConrad Meyer 22110292c54bSConrad Meyer /* 22120292c54bSConrad Meyer * Semi-arbitrarily constrain pagedaemon threads to less than half the 221374f5530dSConrad Meyer * total number of CPUs in the system as an upper limit. 22140292c54bSConrad Meyer */ 221574f5530dSConrad Meyer if (pageout_cpus_per_thread < 2) 221674f5530dSConrad Meyer pageout_cpus_per_thread = 2; 221774f5530dSConrad Meyer else if (pageout_cpus_per_thread > mp_ncpus) 221874f5530dSConrad Meyer pageout_cpus_per_thread = mp_ncpus; 22190292c54bSConrad Meyer 222074f5530dSConrad Meyer total_pageout_threads = howmany(mp_ncpus, pageout_cpus_per_thread); 222174f5530dSConrad Meyer domain_cpus = CPU_COUNT(&cpuset_domain[vmd->vmd_domain]); 222274f5530dSConrad Meyer 222374f5530dSConrad Meyer /* Pagedaemons are not run in empty domains. */ 222474f5530dSConrad Meyer eligible_cpus = mp_ncpus; 222574f5530dSConrad Meyer for (unsigned i = 0; i < vm_ndomains; i++) 222674f5530dSConrad Meyer if (VM_DOMAIN_EMPTY(i)) 222774f5530dSConrad Meyer eligible_cpus -= CPU_COUNT(&cpuset_domain[i]); 222874f5530dSConrad Meyer 222974f5530dSConrad Meyer /* 223074f5530dSConrad Meyer * Assign a portion of the total pageout threads to this domain 223174f5530dSConrad Meyer * corresponding to the fraction of pagedaemon-eligible CPUs in the 223274f5530dSConrad Meyer * domain. In asymmetric NUMA systems, domains with more CPUs may be 223374f5530dSConrad Meyer * allocated more threads than domains with fewer CPUs. 223474f5530dSConrad Meyer */ 223574f5530dSConrad Meyer return (howmany(total_pageout_threads * domain_cpus, eligible_cpus)); 22360292c54bSConrad Meyer } 22370292c54bSConrad Meyer 22380292c54bSConrad Meyer /* 22399c770a27SMark Johnston * Initialize basic pageout daemon settings. See the comment above the 22409c770a27SMark Johnston * definition of vm_domain for some explanation of how these thresholds are 22419c770a27SMark Johnston * used. 2242df8bae1dSRodney W. Grimes */ 22432b14f991SJulian Elischer static void 2244e2068d0bSJeff Roberson vm_pageout_init_domain(int domain) 2245df8bae1dSRodney W. Grimes { 2246e2068d0bSJeff Roberson struct vm_domain *vmd; 22475f8cd1c0SJeff Roberson struct sysctl_oid *oid; 2248e2068d0bSJeff Roberson 2249e2068d0bSJeff Roberson vmd = VM_DOMAIN(domain); 2250e2068d0bSJeff Roberson vmd->vmd_interrupt_free_min = 2; 2251f6b04d2bSDavid Greenman 225245ae1d91SAlan Cox /* 225345ae1d91SAlan Cox * v_free_reserved needs to include enough for the largest 225445ae1d91SAlan Cox * swap pager structures plus enough for any pv_entry structs 225545ae1d91SAlan Cox * when paging. 225645ae1d91SAlan Cox */ 22570cab71bcSDoug Moore vmd->vmd_pageout_free_min = 2 * MAXBSIZE / PAGE_SIZE + 2258e2068d0bSJeff Roberson vmd->vmd_interrupt_free_min; 2259e2068d0bSJeff Roberson vmd->vmd_free_reserved = vm_pageout_page_count + 22609c770a27SMark Johnston vmd->vmd_pageout_free_min + vmd->vmd_page_count / 768; 22619c770a27SMark Johnston vmd->vmd_free_min = vmd->vmd_page_count / 200; 2262e2068d0bSJeff Roberson vmd->vmd_free_severe = vmd->vmd_free_min / 2; 2263e2068d0bSJeff Roberson vmd->vmd_free_target = 4 * vmd->vmd_free_min + vmd->vmd_free_reserved; 2264e2068d0bSJeff Roberson vmd->vmd_free_min += vmd->vmd_free_reserved; 2265e2068d0bSJeff Roberson vmd->vmd_free_severe += vmd->vmd_free_reserved; 2266e2068d0bSJeff Roberson vmd->vmd_inactive_target = (3 * vmd->vmd_free_target) / 2; 2267e2068d0bSJeff Roberson if (vmd->vmd_inactive_target > vmd->vmd_free_count / 3) 2268e2068d0bSJeff Roberson vmd->vmd_inactive_target = vmd->vmd_free_count / 3; 2269df8bae1dSRodney W. Grimes 2270d9e23210SJeff Roberson /* 22715f8cd1c0SJeff Roberson * Set the default wakeup threshold to be 10% below the paging 22725f8cd1c0SJeff Roberson * target. This keeps the steady state out of shortfall. 2273d9e23210SJeff Roberson */ 22745f8cd1c0SJeff Roberson vmd->vmd_pageout_wakeup_thresh = (vmd->vmd_free_target / 10) * 9; 2275e2068d0bSJeff Roberson 2276e2068d0bSJeff Roberson /* 2277e2068d0bSJeff Roberson * Target amount of memory to move out of the laundry queue during a 2278e2068d0bSJeff Roberson * background laundering. This is proportional to the amount of system 2279e2068d0bSJeff Roberson * memory. 2280e2068d0bSJeff Roberson */ 2281e2068d0bSJeff Roberson vmd->vmd_background_launder_target = (vmd->vmd_free_target - 2282e2068d0bSJeff Roberson vmd->vmd_free_min) / 10; 22835f8cd1c0SJeff Roberson 22845f8cd1c0SJeff Roberson /* Initialize the pageout daemon pid controller. */ 22855f8cd1c0SJeff Roberson pidctrl_init(&vmd->vmd_pid, hz / VM_INACT_SCAN_RATE, 22865f8cd1c0SJeff Roberson vmd->vmd_free_target, PIDCTRL_BOUND, 22875f8cd1c0SJeff Roberson PIDCTRL_KPD, PIDCTRL_KID, PIDCTRL_KDD); 22885f8cd1c0SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(vmd->vmd_oid), OID_AUTO, 22897029da5cSPawel Biernacki "pidctrl", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 22905f8cd1c0SJeff Roberson pidctrl_init_sysctl(&vmd->vmd_pid, SYSCTL_CHILDREN(oid)); 22910292c54bSConrad Meyer 229274f5530dSConrad Meyer vmd->vmd_inactive_threads = get_pageout_threads_per_domain(vmd); 2293e2068d0bSJeff Roberson } 2294e2068d0bSJeff Roberson 2295e2068d0bSJeff Roberson static void 2296e2068d0bSJeff Roberson vm_pageout_init(void) 2297e2068d0bSJeff Roberson { 2298*97458520SMark Johnston u_long freecount; 2299e2068d0bSJeff Roberson int i; 2300e2068d0bSJeff Roberson 2301e2068d0bSJeff Roberson /* 2302e2068d0bSJeff Roberson * Initialize some paging parameters. 2303e2068d0bSJeff Roberson */ 2304e2068d0bSJeff Roberson if (vm_cnt.v_page_count < 2000) 2305e2068d0bSJeff Roberson vm_pageout_page_count = 8; 2306e2068d0bSJeff Roberson 2307e2068d0bSJeff Roberson freecount = 0; 2308e2068d0bSJeff Roberson for (i = 0; i < vm_ndomains; i++) { 2309e2068d0bSJeff Roberson struct vm_domain *vmd; 2310e2068d0bSJeff Roberson 2311e2068d0bSJeff Roberson vm_pageout_init_domain(i); 2312e2068d0bSJeff Roberson vmd = VM_DOMAIN(i); 2313e2068d0bSJeff Roberson vm_cnt.v_free_reserved += vmd->vmd_free_reserved; 2314e2068d0bSJeff Roberson vm_cnt.v_free_target += vmd->vmd_free_target; 2315e2068d0bSJeff Roberson vm_cnt.v_free_min += vmd->vmd_free_min; 2316e2068d0bSJeff Roberson vm_cnt.v_inactive_target += vmd->vmd_inactive_target; 2317e2068d0bSJeff Roberson vm_cnt.v_pageout_free_min += vmd->vmd_pageout_free_min; 2318e2068d0bSJeff Roberson vm_cnt.v_interrupt_free_min += vmd->vmd_interrupt_free_min; 2319e2068d0bSJeff Roberson vm_cnt.v_free_severe += vmd->vmd_free_severe; 2320e2068d0bSJeff Roberson freecount += vmd->vmd_free_count; 2321e2068d0bSJeff Roberson } 2322d9e23210SJeff Roberson 2323d9e23210SJeff Roberson /* 2324d9e23210SJeff Roberson * Set interval in seconds for active scan. We want to visit each 2325c9612b2dSJeff Roberson * page at least once every ten minutes. This is to prevent worst 2326c9612b2dSJeff Roberson * case paging behaviors with stale active LRU. 2327d9e23210SJeff Roberson */ 2328d9e23210SJeff Roberson if (vm_pageout_update_period == 0) 2329c9612b2dSJeff Roberson vm_pageout_update_period = 600; 2330d9e23210SJeff Roberson 2331*97458520SMark Johnston /* 2332*97458520SMark Johnston * Set the maximum number of user-wired virtual pages. Historically the 2333*97458520SMark Johnston * main source of such pages was mlock(2) and mlockall(2). Hypervisors 2334*97458520SMark Johnston * may also request user-wired memory. 2335*97458520SMark Johnston */ 233654a3a114SMark Johnston if (vm_page_max_user_wired == 0) 2337*97458520SMark Johnston vm_page_max_user_wired = 4 * freecount / 5; 23384d19f4adSSteven Hartland } 23394d19f4adSSteven Hartland 23404d19f4adSSteven Hartland /* 23414d19f4adSSteven Hartland * vm_pageout is the high level pageout daemon. 23424d19f4adSSteven Hartland */ 23434d19f4adSSteven Hartland static void 23444d19f4adSSteven Hartland vm_pageout(void) 23454d19f4adSSteven Hartland { 2346920239efSMark Johnston struct proc *p; 2347920239efSMark Johnston struct thread *td; 23480292c54bSConrad Meyer int error, first, i, j, pageout_threads; 2349920239efSMark Johnston 2350920239efSMark Johnston p = curproc; 2351920239efSMark Johnston td = curthread; 2352df8bae1dSRodney W. Grimes 2353245139c6SKonstantin Belousov mtx_init(&vm_oom_ratelim_mtx, "vmoomr", NULL, MTX_DEF); 235424a1cce3SDavid Greenman swap_pager_swap_init(); 2355920239efSMark Johnston for (first = -1, i = 0; i < vm_ndomains; i++) { 235630c5525bSAndrew Gallatin if (VM_DOMAIN_EMPTY(i)) { 235730c5525bSAndrew Gallatin if (bootverbose) 235830c5525bSAndrew Gallatin printf("domain %d empty; skipping pageout\n", 235930c5525bSAndrew Gallatin i); 236030c5525bSAndrew Gallatin continue; 236130c5525bSAndrew Gallatin } 2362920239efSMark Johnston if (first == -1) 2363920239efSMark Johnston first = i; 2364920239efSMark Johnston else { 2365920239efSMark Johnston error = kthread_add(vm_pageout_worker, 2366920239efSMark Johnston (void *)(uintptr_t)i, p, NULL, 0, 0, "dom%d", i); 2367920239efSMark Johnston if (error != 0) 2368920239efSMark Johnston panic("starting pageout for domain %d: %d\n", 2369449c2e92SKonstantin Belousov i, error); 2370dc2efb27SJohn Dyson } 237174f5530dSConrad Meyer pageout_threads = VM_DOMAIN(i)->vmd_inactive_threads; 23720292c54bSConrad Meyer for (j = 0; j < pageout_threads - 1; j++) { 23730292c54bSConrad Meyer error = kthread_add(vm_pageout_helper, 23740292c54bSConrad Meyer (void *)(uintptr_t)i, p, NULL, 0, 0, 23750292c54bSConrad Meyer "dom%d helper%d", i, j); 23760292c54bSConrad Meyer if (error != 0) 23770292c54bSConrad Meyer panic("starting pageout helper %d for domain " 23780292c54bSConrad Meyer "%d: %d\n", j, i, error); 23790292c54bSConrad Meyer } 2380e2068d0bSJeff Roberson error = kthread_add(vm_pageout_laundry_worker, 2381920239efSMark Johnston (void *)(uintptr_t)i, p, NULL, 0, 0, "laundry: dom%d", i); 2382e2068d0bSJeff Roberson if (error != 0) 2383920239efSMark Johnston panic("starting laundry for domain %d: %d", i, error); 2384f919ebdeSDavid Greenman } 2385920239efSMark Johnston error = kthread_add(uma_reclaim_worker, NULL, p, NULL, 0, 0, "uma"); 238644ec2b63SKonstantin Belousov if (error != 0) 238744ec2b63SKonstantin Belousov panic("starting uma_reclaim helper, error %d\n", error); 2388920239efSMark Johnston 2389920239efSMark Johnston snprintf(td->td_name, sizeof(td->td_name), "dom%d", first); 2390920239efSMark Johnston vm_pageout_worker((void *)(uintptr_t)first); 2391df8bae1dSRodney W. Grimes } 239226f9a767SRodney W. Grimes 23936b4b77adSAlan Cox /* 2394280d15cdSMark Johnston * Perform an advisory wakeup of the page daemon. 23956b4b77adSAlan Cox */ 2396e0c5a895SJohn Dyson void 2397e2068d0bSJeff Roberson pagedaemon_wakeup(int domain) 2398e0c5a895SJohn Dyson { 2399e2068d0bSJeff Roberson struct vm_domain *vmd; 2400a1c0a785SAlan Cox 2401e2068d0bSJeff Roberson vmd = VM_DOMAIN(domain); 240230fbfddaSJeff Roberson vm_domain_pageout_assert_unlocked(vmd); 240330fbfddaSJeff Roberson if (curproc == pageproc) 240430fbfddaSJeff Roberson return; 2405280d15cdSMark Johnston 240630fbfddaSJeff Roberson if (atomic_fetchadd_int(&vmd->vmd_pageout_wanted, 1) == 0) { 240730fbfddaSJeff Roberson vm_domain_pageout_lock(vmd); 240830fbfddaSJeff Roberson atomic_store_int(&vmd->vmd_pageout_wanted, 1); 2409e2068d0bSJeff Roberson wakeup(&vmd->vmd_pageout_wanted); 241030fbfddaSJeff Roberson vm_domain_pageout_unlock(vmd); 2411e0c5a895SJohn Dyson } 2412e0c5a895SJohn Dyson } 2413