160727d8bSWarner Losh /*- 2796df753SPedro F. Giffuni * SPDX-License-Identifier: (BSD-4-Clause AND MIT-CMU) 3df57947fSPedro F. Giffuni * 426f9a767SRodney W. Grimes * Copyright (c) 1991 Regents of the University of California. 526f9a767SRodney W. Grimes * All rights reserved. 626f9a767SRodney W. Grimes * Copyright (c) 1994 John S. Dyson 726f9a767SRodney W. Grimes * All rights reserved. 826f9a767SRodney W. Grimes * Copyright (c) 1994 David Greenman 926f9a767SRodney W. Grimes * All rights reserved. 108dbca793STor Egge * Copyright (c) 2005 Yahoo! Technologies Norway AS 118dbca793STor Egge * All rights reserved. 12df8bae1dSRodney W. Grimes * 13df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 14df8bae1dSRodney W. Grimes * The Mach Operating System project at Carnegie-Mellon University. 15df8bae1dSRodney W. Grimes * 16df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 17df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 18df8bae1dSRodney W. Grimes * are met: 19df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 20df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 21df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 22df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 23df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 24df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 255929bcfaSPhilippe Charnier * must display the following acknowledgement: 26df8bae1dSRodney W. Grimes * This product includes software developed by the University of 27df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 28df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 29df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 30df8bae1dSRodney W. Grimes * without specific prior written permission. 31df8bae1dSRodney W. Grimes * 32df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 33df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 34df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 35df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 36df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 37df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 38df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 39df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 40df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 41df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 42df8bae1dSRodney W. Grimes * SUCH DAMAGE. 43df8bae1dSRodney W. Grimes * 443c4dd356SDavid Greenman * from: @(#)vm_pageout.c 7.4 (Berkeley) 5/7/91 45df8bae1dSRodney W. Grimes * 46df8bae1dSRodney W. Grimes * 47df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1990 Carnegie-Mellon University. 48df8bae1dSRodney W. Grimes * All rights reserved. 49df8bae1dSRodney W. Grimes * 50df8bae1dSRodney W. Grimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young 51df8bae1dSRodney W. Grimes * 52df8bae1dSRodney W. Grimes * Permission to use, copy, modify and distribute this software and 53df8bae1dSRodney W. Grimes * its documentation is hereby granted, provided that both the copyright 54df8bae1dSRodney W. Grimes * notice and this permission notice appear in all copies of the 55df8bae1dSRodney W. Grimes * software, derivative works or modified versions, and any portions 56df8bae1dSRodney W. Grimes * thereof, and that both notices appear in supporting documentation. 57df8bae1dSRodney W. Grimes * 58df8bae1dSRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 59df8bae1dSRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 60df8bae1dSRodney W. Grimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 61df8bae1dSRodney W. Grimes * 62df8bae1dSRodney W. Grimes * Carnegie Mellon requests users of this software to return to 63df8bae1dSRodney W. Grimes * 64df8bae1dSRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 65df8bae1dSRodney W. Grimes * School of Computer Science 66df8bae1dSRodney W. Grimes * Carnegie Mellon University 67df8bae1dSRodney W. Grimes * Pittsburgh PA 15213-3890 68df8bae1dSRodney W. Grimes * 69df8bae1dSRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the 70df8bae1dSRodney W. Grimes * rights to redistribute these changes. 71df8bae1dSRodney W. Grimes */ 72df8bae1dSRodney W. Grimes 73df8bae1dSRodney W. Grimes /* 74df8bae1dSRodney W. Grimes * The proverbial page-out daemon. 75df8bae1dSRodney W. Grimes */ 76df8bae1dSRodney W. Grimes 77874651b1SDavid E. O'Brien #include <sys/cdefs.h> 78874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 79874651b1SDavid E. O'Brien 80faa5f8d8SAndrzej Bialecki #include "opt_vm.h" 817672ca05SMark Johnston 82df8bae1dSRodney W. Grimes #include <sys/param.h> 8326f9a767SRodney W. Grimes #include <sys/systm.h> 84b5e8ce9fSBruce Evans #include <sys/kernel.h> 85855a310fSJeff Roberson #include <sys/eventhandler.h> 86fb919e4dSMark Murray #include <sys/lock.h> 87fb919e4dSMark Murray #include <sys/mutex.h> 8826f9a767SRodney W. Grimes #include <sys/proc.h> 899c8b8baaSPeter Wemm #include <sys/kthread.h> 900384fff8SJason Evans #include <sys/ktr.h> 9197824da3SAlan Cox #include <sys/mount.h> 92099e7e95SEdward Tomasz Napierala #include <sys/racct.h> 9326f9a767SRodney W. Grimes #include <sys/resourcevar.h> 94b43179fbSJeff Roberson #include <sys/sched.h> 9514a0d74eSSteven Hartland #include <sys/sdt.h> 96d2fc5315SPoul-Henning Kamp #include <sys/signalvar.h> 97449c2e92SKonstantin Belousov #include <sys/smp.h> 98a6bf3a9eSRyan Stone #include <sys/time.h> 99f6b04d2bSDavid Greenman #include <sys/vnode.h> 100efeaf95aSDavid Greenman #include <sys/vmmeter.h> 10189f6b863SAttilio Rao #include <sys/rwlock.h> 1021005a129SJohn Baldwin #include <sys/sx.h> 10338efa82bSJohn Dyson #include <sys/sysctl.h> 104df8bae1dSRodney W. Grimes 105df8bae1dSRodney W. Grimes #include <vm/vm.h> 106efeaf95aSDavid Greenman #include <vm/vm_param.h> 107efeaf95aSDavid Greenman #include <vm/vm_object.h> 108df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 109efeaf95aSDavid Greenman #include <vm/vm_map.h> 110df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h> 11124a1cce3SDavid Greenman #include <vm/vm_pager.h> 112449c2e92SKonstantin Belousov #include <vm/vm_phys.h> 113e2068d0bSJeff Roberson #include <vm/vm_pagequeue.h> 11405f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h> 115efeaf95aSDavid Greenman #include <vm/vm_extern.h> 116670d17b5SJeff Roberson #include <vm/uma.h> 117df8bae1dSRodney W. Grimes 1182b14f991SJulian Elischer /* 1192b14f991SJulian Elischer * System initialization 1202b14f991SJulian Elischer */ 1212b14f991SJulian Elischer 1222b14f991SJulian Elischer /* the kernel process "vm_pageout"*/ 12311caded3SAlfred Perlstein static void vm_pageout(void); 1244d19f4adSSteven Hartland static void vm_pageout_init(void); 125ebcddc72SAlan Cox static int vm_pageout_clean(vm_page_t m, int *numpagedout); 12634d8b7eaSJeff Roberson static int vm_pageout_cluster(vm_page_t m); 12776386c7eSKonstantin Belousov static void vm_pageout_mightbe_oom(struct vm_domain *vmd, int page_shortage, 12876386c7eSKonstantin Belousov int starting_page_shortage); 12945ae1d91SAlan Cox 1304d19f4adSSteven Hartland SYSINIT(pagedaemon_init, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, vm_pageout_init, 1314d19f4adSSteven Hartland NULL); 1324d19f4adSSteven Hartland 1332b14f991SJulian Elischer struct proc *pageproc; 1342b14f991SJulian Elischer 1352b14f991SJulian Elischer static struct kproc_desc page_kp = { 1362b14f991SJulian Elischer "pagedaemon", 1372b14f991SJulian Elischer vm_pageout, 1382b14f991SJulian Elischer &pageproc 1392b14f991SJulian Elischer }; 1404d19f4adSSteven Hartland SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_SECOND, kproc_start, 141237fdd78SRobert Watson &page_kp); 1422b14f991SJulian Elischer 14314a0d74eSSteven Hartland SDT_PROVIDER_DEFINE(vm); 14414a0d74eSSteven Hartland SDT_PROBE_DEFINE(vm, , , vm__lowmem_scan); 14514a0d74eSSteven Hartland 146ebcddc72SAlan Cox /* Pagedaemon activity rates, in subdivisions of one second. */ 147ebcddc72SAlan Cox #define VM_LAUNDER_RATE 10 1485f8cd1c0SJeff Roberson #define VM_INACT_SCAN_RATE 10 1492b14f991SJulian Elischer 15076386c7eSKonstantin Belousov static int vm_pageout_oom_seq = 12; 151ebcddc72SAlan Cox 152d9e23210SJeff Roberson static int vm_pageout_update_period; 1534a365329SAndrey Zonov static int disable_swap_pageouts; 154c9612b2dSJeff Roberson static int lowmem_period = 10; 155b1fd102eSMark Johnston static int swapdev_enabled; 15670111b90SJohn Dyson 1578311a2b8SWill Andrews static int vm_panic_on_oom = 0; 1588311a2b8SWill Andrews 1598311a2b8SWill Andrews SYSCTL_INT(_vm, OID_AUTO, panic_on_oom, 1608311a2b8SWill Andrews CTLFLAG_RWTUN, &vm_panic_on_oom, 0, 1618311a2b8SWill Andrews "panic on out of memory instead of killing the largest process"); 1628311a2b8SWill Andrews 163d9e23210SJeff Roberson SYSCTL_INT(_vm, OID_AUTO, pageout_update_period, 164e0b2fc3aSMark Johnston CTLFLAG_RWTUN, &vm_pageout_update_period, 0, 165d9e23210SJeff Roberson "Maximum active LRU update period"); 16653636869SAndrey Zonov 167e0b2fc3aSMark Johnston SYSCTL_INT(_vm, OID_AUTO, lowmem_period, CTLFLAG_RWTUN, &lowmem_period, 0, 168c9612b2dSJeff Roberson "Low memory callback period"); 169c9612b2dSJeff Roberson 170ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts, 171e0b2fc3aSMark Johnston CTLFLAG_RWTUN, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages"); 17212ac6a1dSJohn Dyson 17323b59018SMatthew Dillon static int pageout_lock_miss; 17423b59018SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss, 17523b59018SMatthew Dillon CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout"); 17623b59018SMatthew Dillon 17776386c7eSKonstantin Belousov SYSCTL_INT(_vm, OID_AUTO, pageout_oom_seq, 178e0b2fc3aSMark Johnston CTLFLAG_RWTUN, &vm_pageout_oom_seq, 0, 17976386c7eSKonstantin Belousov "back-to-back calls to oom detector to start OOM"); 18076386c7eSKonstantin Belousov 181ebcddc72SAlan Cox static int act_scan_laundry_weight = 3; 182e0b2fc3aSMark Johnston SYSCTL_INT(_vm, OID_AUTO, act_scan_laundry_weight, CTLFLAG_RWTUN, 183ebcddc72SAlan Cox &act_scan_laundry_weight, 0, 184ebcddc72SAlan Cox "weight given to clean vs. dirty pages in active queue scans"); 185ebcddc72SAlan Cox 186ebcddc72SAlan Cox static u_int vm_background_launder_rate = 4096; 187e0b2fc3aSMark Johnston SYSCTL_UINT(_vm, OID_AUTO, background_launder_rate, CTLFLAG_RWTUN, 188ebcddc72SAlan Cox &vm_background_launder_rate, 0, 189ebcddc72SAlan Cox "background laundering rate, in kilobytes per second"); 190ebcddc72SAlan Cox 191ebcddc72SAlan Cox static u_int vm_background_launder_max = 20 * 1024; 192e0b2fc3aSMark Johnston SYSCTL_UINT(_vm, OID_AUTO, background_launder_max, CTLFLAG_RWTUN, 193ebcddc72SAlan Cox &vm_background_launder_max, 0, "background laundering cap, in kilobytes"); 194ebcddc72SAlan Cox 195e2241590SAlan Cox int vm_pageout_page_count = 32; 196df8bae1dSRodney W. Grimes 19754a3a114SMark Johnston u_long vm_page_max_user_wired; 19854a3a114SMark Johnston SYSCTL_ULONG(_vm, OID_AUTO, max_user_wired, CTLFLAG_RW, 19954a3a114SMark Johnston &vm_page_max_user_wired, 0, 20054a3a114SMark Johnston "system-wide limit to user-wired page count"); 201df8bae1dSRodney W. Grimes 202ebcddc72SAlan Cox static u_int isqrt(u_int num); 203ebcddc72SAlan Cox static int vm_pageout_launder(struct vm_domain *vmd, int launder, 204ebcddc72SAlan Cox bool in_shortfall); 205ebcddc72SAlan Cox static void vm_pageout_laundry_worker(void *arg); 206cd41fc12SDavid Greenman 2075cd29d0fSMark Johnston struct scan_state { 2085cd29d0fSMark Johnston struct vm_batchqueue bq; 2098d220203SAlan Cox struct vm_pagequeue *pq; 2105cd29d0fSMark Johnston vm_page_t marker; 2115cd29d0fSMark Johnston int maxscan; 2125cd29d0fSMark Johnston int scanned; 2135cd29d0fSMark Johnston }; 2148dbca793STor Egge 2155cd29d0fSMark Johnston static void 2165cd29d0fSMark Johnston vm_pageout_init_scan(struct scan_state *ss, struct vm_pagequeue *pq, 2175cd29d0fSMark Johnston vm_page_t marker, vm_page_t after, int maxscan) 2185cd29d0fSMark Johnston { 2198dbca793STor Egge 2205cd29d0fSMark Johnston vm_pagequeue_assert_locked(pq); 2215cff1f4dSMark Johnston KASSERT((marker->a.flags & PGA_ENQUEUED) == 0, 2225cd29d0fSMark Johnston ("marker %p already enqueued", marker)); 2235cd29d0fSMark Johnston 2245cd29d0fSMark Johnston if (after == NULL) 2255cd29d0fSMark Johnston TAILQ_INSERT_HEAD(&pq->pq_pl, marker, plinks.q); 2265cd29d0fSMark Johnston else 2275cd29d0fSMark Johnston TAILQ_INSERT_AFTER(&pq->pq_pl, after, marker, plinks.q); 2285cd29d0fSMark Johnston vm_page_aflag_set(marker, PGA_ENQUEUED); 2295cd29d0fSMark Johnston 2305cd29d0fSMark Johnston vm_batchqueue_init(&ss->bq); 2315cd29d0fSMark Johnston ss->pq = pq; 2325cd29d0fSMark Johnston ss->marker = marker; 2335cd29d0fSMark Johnston ss->maxscan = maxscan; 2345cd29d0fSMark Johnston ss->scanned = 0; 2358d220203SAlan Cox vm_pagequeue_unlock(pq); 2365cd29d0fSMark Johnston } 2378dbca793STor Egge 2385cd29d0fSMark Johnston static void 2395cd29d0fSMark Johnston vm_pageout_end_scan(struct scan_state *ss) 2405cd29d0fSMark Johnston { 2415cd29d0fSMark Johnston struct vm_pagequeue *pq; 2425cd29d0fSMark Johnston 2435cd29d0fSMark Johnston pq = ss->pq; 2445cd29d0fSMark Johnston vm_pagequeue_assert_locked(pq); 2455cff1f4dSMark Johnston KASSERT((ss->marker->a.flags & PGA_ENQUEUED) != 0, 2465cd29d0fSMark Johnston ("marker %p not enqueued", ss->marker)); 2475cd29d0fSMark Johnston 2485cd29d0fSMark Johnston TAILQ_REMOVE(&pq->pq_pl, ss->marker, plinks.q); 2495cd29d0fSMark Johnston vm_page_aflag_clear(ss->marker, PGA_ENQUEUED); 250899fe184SMark Johnston pq->pq_pdpages += ss->scanned; 2518dbca793STor Egge } 2528dbca793STor Egge 2538dbca793STor Egge /* 2545cd29d0fSMark Johnston * Add a small number of queued pages to a batch queue for later processing 2555cd29d0fSMark Johnston * without the corresponding queue lock held. The caller must have enqueued a 2565cd29d0fSMark Johnston * marker page at the desired start point for the scan. Pages will be 2575cd29d0fSMark Johnston * physically dequeued if the caller so requests. Otherwise, the returned 2585cd29d0fSMark Johnston * batch may contain marker pages, and it is up to the caller to handle them. 2595cd29d0fSMark Johnston * 26036f8fe9bSMark Johnston * When processing the batch queue, vm_page_queue() must be used to 26136f8fe9bSMark Johnston * determine whether the page has been logically dequeued by another thread. 26236f8fe9bSMark Johnston * Once this check is performed, the page lock guarantees that the page will 26336f8fe9bSMark Johnston * not be disassociated from the queue. 2645cd29d0fSMark Johnston */ 2655cd29d0fSMark Johnston static __always_inline void 2665cd29d0fSMark Johnston vm_pageout_collect_batch(struct scan_state *ss, const bool dequeue) 2675cd29d0fSMark Johnston { 2688d220203SAlan Cox struct vm_pagequeue *pq; 269d70f0ab3SMark Johnston vm_page_t m, marker, n; 2708c616246SKonstantin Belousov 2715cd29d0fSMark Johnston marker = ss->marker; 2725cd29d0fSMark Johnston pq = ss->pq; 2738c616246SKonstantin Belousov 2745cff1f4dSMark Johnston KASSERT((marker->a.flags & PGA_ENQUEUED) != 0, 2755cd29d0fSMark Johnston ("marker %p not enqueued", ss->marker)); 2768c616246SKonstantin Belousov 2778d220203SAlan Cox vm_pagequeue_lock(pq); 2785cd29d0fSMark Johnston for (m = TAILQ_NEXT(marker, plinks.q); m != NULL && 2795cd29d0fSMark Johnston ss->scanned < ss->maxscan && ss->bq.bq_cnt < VM_BATCHQUEUE_SIZE; 280d70f0ab3SMark Johnston m = n, ss->scanned++) { 281d70f0ab3SMark Johnston n = TAILQ_NEXT(m, plinks.q); 2825cd29d0fSMark Johnston if ((m->flags & PG_MARKER) == 0) { 2835cff1f4dSMark Johnston KASSERT((m->a.flags & PGA_ENQUEUED) != 0, 2845cd29d0fSMark Johnston ("page %p not enqueued", m)); 2855cd29d0fSMark Johnston KASSERT((m->flags & PG_FICTITIOUS) == 0, 2865cd29d0fSMark Johnston ("Fictitious page %p cannot be in page queue", m)); 2875cd29d0fSMark Johnston KASSERT((m->oflags & VPO_UNMANAGED) == 0, 2885cd29d0fSMark Johnston ("Unmanaged page %p cannot be in page queue", m)); 2895cd29d0fSMark Johnston } else if (dequeue) 2905cd29d0fSMark Johnston continue; 2918c616246SKonstantin Belousov 2925cd29d0fSMark Johnston (void)vm_batchqueue_insert(&ss->bq, m); 2935cd29d0fSMark Johnston if (dequeue) { 2945cd29d0fSMark Johnston TAILQ_REMOVE(&pq->pq_pl, m, plinks.q); 2955cd29d0fSMark Johnston vm_page_aflag_clear(m, PGA_ENQUEUED); 2965cd29d0fSMark Johnston } 2975cd29d0fSMark Johnston } 2985cd29d0fSMark Johnston TAILQ_REMOVE(&pq->pq_pl, marker, plinks.q); 2995cd29d0fSMark Johnston if (__predict_true(m != NULL)) 3005cd29d0fSMark Johnston TAILQ_INSERT_BEFORE(m, marker, plinks.q); 3015cd29d0fSMark Johnston else 3025cd29d0fSMark Johnston TAILQ_INSERT_TAIL(&pq->pq_pl, marker, plinks.q); 3035cd29d0fSMark Johnston if (dequeue) 3045cd29d0fSMark Johnston vm_pagequeue_cnt_add(pq, -ss->bq.bq_cnt); 3055cd29d0fSMark Johnston vm_pagequeue_unlock(pq); 3065cd29d0fSMark Johnston } 3075cd29d0fSMark Johnston 308fee2a2faSMark Johnston /* 309fee2a2faSMark Johnston * Return the next page to be scanned, or NULL if the scan is complete. 310fee2a2faSMark Johnston */ 3115cd29d0fSMark Johnston static __always_inline vm_page_t 3125cd29d0fSMark Johnston vm_pageout_next(struct scan_state *ss, const bool dequeue) 3135cd29d0fSMark Johnston { 3145cd29d0fSMark Johnston 3155cd29d0fSMark Johnston if (ss->bq.bq_cnt == 0) 3165cd29d0fSMark Johnston vm_pageout_collect_batch(ss, dequeue); 3175cd29d0fSMark Johnston return (vm_batchqueue_pop(&ss->bq)); 3188c616246SKonstantin Belousov } 3198c616246SKonstantin Belousov 3208c616246SKonstantin Belousov /* 321*b7f30bffSMark Johnston * Determine whether processing of a page should be deferred and ensure that any 322*b7f30bffSMark Johnston * outstanding queue operations are processed. 323*b7f30bffSMark Johnston */ 324*b7f30bffSMark Johnston static __always_inline bool 325*b7f30bffSMark Johnston vm_pageout_defer(vm_page_t m, const uint8_t queue, const bool enqueued) 326*b7f30bffSMark Johnston { 327*b7f30bffSMark Johnston vm_page_astate_t as; 328*b7f30bffSMark Johnston 329*b7f30bffSMark Johnston as = vm_page_astate_load(m); 330*b7f30bffSMark Johnston if (__predict_false(as.queue != queue || 331*b7f30bffSMark Johnston ((as.flags & PGA_ENQUEUED) != 0) != enqueued)) 332*b7f30bffSMark Johnston return (true); 333*b7f30bffSMark Johnston if ((as.flags & PGA_QUEUE_OP_MASK) != 0) { 334*b7f30bffSMark Johnston vm_page_pqbatch_submit(m, queue); 335*b7f30bffSMark Johnston return (true); 336*b7f30bffSMark Johnston } 337*b7f30bffSMark Johnston return (false); 338*b7f30bffSMark Johnston } 339*b7f30bffSMark Johnston 340*b7f30bffSMark Johnston /* 341248fe642SAlan Cox * Scan for pages at adjacent offsets within the given page's object that are 342248fe642SAlan Cox * eligible for laundering, form a cluster of these pages and the given page, 343248fe642SAlan Cox * and launder that cluster. 34426f9a767SRodney W. Grimes */ 3453af76890SPoul-Henning Kamp static int 34634d8b7eaSJeff Roberson vm_pageout_cluster(vm_page_t m) 34724a1cce3SDavid Greenman { 34854d92145SMatthew Dillon vm_object_t object; 349248fe642SAlan Cox vm_page_t mc[2 * vm_pageout_page_count], p, pb, ps; 350248fe642SAlan Cox vm_pindex_t pindex; 351248fe642SAlan Cox int ib, is, page_base, pageout_count; 35226f9a767SRodney W. Grimes 35317f6a17bSAlan Cox object = m->object; 35489f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 355248fe642SAlan Cox pindex = m->pindex; 3560cddd8f0SMatthew Dillon 35763e97555SJeff Roberson vm_page_assert_xbusied(m); 3580d94caffSDavid Greenman 35991b4f427SAlan Cox mc[vm_pageout_page_count] = pb = ps = m; 36026f9a767SRodney W. Grimes pageout_count = 1; 361f35329acSJohn Dyson page_base = vm_pageout_page_count; 36290ecac61SMatthew Dillon ib = 1; 36390ecac61SMatthew Dillon is = 1; 36490ecac61SMatthew Dillon 36524a1cce3SDavid Greenman /* 366248fe642SAlan Cox * We can cluster only if the page is not clean, busy, or held, and 367ebcddc72SAlan Cox * the page is in the laundry queue. 36890ecac61SMatthew Dillon * 36990ecac61SMatthew Dillon * During heavy mmap/modification loads the pageout 37090ecac61SMatthew Dillon * daemon can really fragment the underlying file 371248fe642SAlan Cox * due to flushing pages out of order and not trying to 372248fe642SAlan Cox * align the clusters (which leaves sporadic out-of-order 37390ecac61SMatthew Dillon * holes). To solve this problem we do the reverse scan 37490ecac61SMatthew Dillon * first and attempt to align our cluster, then do a 37590ecac61SMatthew Dillon * forward scan if room remains. 37624a1cce3SDavid Greenman */ 37790ecac61SMatthew Dillon more: 378248fe642SAlan Cox while (ib != 0 && pageout_count < vm_pageout_page_count) { 37990ecac61SMatthew Dillon if (ib > pindex) { 38090ecac61SMatthew Dillon ib = 0; 38190ecac61SMatthew Dillon break; 382f6b04d2bSDavid Greenman } 38363e97555SJeff Roberson if ((p = vm_page_prev(pb)) == NULL || 38463e97555SJeff Roberson vm_page_tryxbusy(p) == 0) { 38590ecac61SMatthew Dillon ib = 0; 38690ecac61SMatthew Dillon break; 387f6b04d2bSDavid Greenman } 38863e97555SJeff Roberson if (vm_page_wired(p)) { 38963e97555SJeff Roberson ib = 0; 39063e97555SJeff Roberson vm_page_xunbusy(p); 39163e97555SJeff Roberson break; 39263e97555SJeff Roberson } 39324a1cce3SDavid Greenman vm_page_test_dirty(p); 3941b5c869dSMark Johnston if (p->dirty == 0) { 395eb5d3969SAlan Cox ib = 0; 39663e97555SJeff Roberson vm_page_xunbusy(p); 397eb5d3969SAlan Cox break; 398eb5d3969SAlan Cox } 399e8bcf696SMark Johnston vm_page_lock(p); 400fee2a2faSMark Johnston if (!vm_page_in_laundry(p) || !vm_page_try_remove_write(p)) { 401e8bcf696SMark Johnston vm_page_unlock(p); 40263e97555SJeff Roberson vm_page_xunbusy(p); 40390ecac61SMatthew Dillon ib = 0; 40424a1cce3SDavid Greenman break; 405f6b04d2bSDavid Greenman } 406e8bcf696SMark Johnston vm_page_unlock(p); 40791b4f427SAlan Cox mc[--page_base] = pb = p; 40890ecac61SMatthew Dillon ++pageout_count; 40990ecac61SMatthew Dillon ++ib; 410248fe642SAlan Cox 41124a1cce3SDavid Greenman /* 412248fe642SAlan Cox * We are at an alignment boundary. Stop here, and switch 413248fe642SAlan Cox * directions. Do not clear ib. 41424a1cce3SDavid Greenman */ 41590ecac61SMatthew Dillon if ((pindex - (ib - 1)) % vm_pageout_page_count == 0) 41690ecac61SMatthew Dillon break; 41724a1cce3SDavid Greenman } 41890ecac61SMatthew Dillon while (pageout_count < vm_pageout_page_count && 41990ecac61SMatthew Dillon pindex + is < object->size) { 42063e97555SJeff Roberson if ((p = vm_page_next(ps)) == NULL || 42163e97555SJeff Roberson vm_page_tryxbusy(p) == 0) 42290ecac61SMatthew Dillon break; 42363e97555SJeff Roberson if (vm_page_wired(p)) { 42463e97555SJeff Roberson vm_page_xunbusy(p); 42563e97555SJeff Roberson break; 42663e97555SJeff Roberson } 42724a1cce3SDavid Greenman vm_page_test_dirty(p); 42863e97555SJeff Roberson if (p->dirty == 0) { 42963e97555SJeff Roberson vm_page_xunbusy(p); 430eb5d3969SAlan Cox break; 43163e97555SJeff Roberson } 432e8bcf696SMark Johnston vm_page_lock(p); 433e8bcf696SMark Johnston if (!vm_page_in_laundry(p) || !vm_page_try_remove_write(p)) { 434e8bcf696SMark Johnston vm_page_unlock(p); 43563e97555SJeff Roberson vm_page_xunbusy(p); 43624a1cce3SDavid Greenman break; 437e8bcf696SMark Johnston } 438e8bcf696SMark Johnston vm_page_unlock(p); 43991b4f427SAlan Cox mc[page_base + pageout_count] = ps = p; 44090ecac61SMatthew Dillon ++pageout_count; 44190ecac61SMatthew Dillon ++is; 44224a1cce3SDavid Greenman } 44390ecac61SMatthew Dillon 44490ecac61SMatthew Dillon /* 44590ecac61SMatthew Dillon * If we exhausted our forward scan, continue with the reverse scan 446248fe642SAlan Cox * when possible, even past an alignment boundary. This catches 447248fe642SAlan Cox * boundary conditions. 44890ecac61SMatthew Dillon */ 449248fe642SAlan Cox if (ib != 0 && pageout_count < vm_pageout_page_count) 45090ecac61SMatthew Dillon goto more; 451f6b04d2bSDavid Greenman 45299e6e193SMark Johnston return (vm_pageout_flush(&mc[page_base], pageout_count, 45399e6e193SMark Johnston VM_PAGER_PUT_NOREUSE, 0, NULL, NULL)); 454aef922f5SJohn Dyson } 455aef922f5SJohn Dyson 4561c7c3c6aSMatthew Dillon /* 4571c7c3c6aSMatthew Dillon * vm_pageout_flush() - launder the given pages 4581c7c3c6aSMatthew Dillon * 4591c7c3c6aSMatthew Dillon * The given pages are laundered. Note that we setup for the start of 4601c7c3c6aSMatthew Dillon * I/O ( i.e. busy the page ), mark it read-only, and bump the object 4611c7c3c6aSMatthew Dillon * reference count all in here rather then in the parent. If we want 4621c7c3c6aSMatthew Dillon * the parent to do more sophisticated things we may have to change 4631c7c3c6aSMatthew Dillon * the ordering. 4641e8a675cSKonstantin Belousov * 4651e8a675cSKonstantin Belousov * Returned runlen is the count of pages between mreq and first 4661e8a675cSKonstantin Belousov * page after mreq with status VM_PAGER_AGAIN. 467126d6082SKonstantin Belousov * *eio is set to TRUE if pager returned VM_PAGER_ERROR or VM_PAGER_FAIL 468126d6082SKonstantin Belousov * for any page in runlen set. 4691c7c3c6aSMatthew Dillon */ 470aef922f5SJohn Dyson int 471126d6082SKonstantin Belousov vm_pageout_flush(vm_page_t *mc, int count, int flags, int mreq, int *prunlen, 472126d6082SKonstantin Belousov boolean_t *eio) 473aef922f5SJohn Dyson { 4742e3b314dSAlan Cox vm_object_t object = mc[0]->object; 475aef922f5SJohn Dyson int pageout_status[count]; 47695461b45SJohn Dyson int numpagedout = 0; 4771e8a675cSKonstantin Belousov int i, runlen; 478aef922f5SJohn Dyson 47989f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 4807bec141bSKip Macy 4811c7c3c6aSMatthew Dillon /* 48263e97555SJeff Roberson * Initiate I/O. Mark the pages shared busy and verify that they're 48363e97555SJeff Roberson * valid and read-only. 4841c7c3c6aSMatthew Dillon * 4851c7c3c6aSMatthew Dillon * We do not have to fixup the clean/dirty bits here... we can 4861c7c3c6aSMatthew Dillon * allow the pager to do it after the I/O completes. 48702fa91d3SMatthew Dillon * 48802fa91d3SMatthew Dillon * NOTE! mc[i]->dirty may be partial or fragmented due to an 48902fa91d3SMatthew Dillon * edge case with file fragments. 4901c7c3c6aSMatthew Dillon */ 4918f9110f6SJohn Dyson for (i = 0; i < count; i++) { 4920012f373SJeff Roberson KASSERT(vm_page_all_valid(mc[i]), 4937a935082SAlan Cox ("vm_pageout_flush: partially invalid page %p index %d/%d", 4947a935082SAlan Cox mc[i], i, count)); 4955cff1f4dSMark Johnston KASSERT((mc[i]->a.flags & PGA_WRITEABLE) == 0, 496aed9aaaaSMark Johnston ("vm_pageout_flush: writeable page %p", mc[i])); 49763e97555SJeff Roberson vm_page_busy_downgrade(mc[i]); 4982965a453SKip Macy } 499d474eaaaSDoug Rabson vm_object_pip_add(object, count); 500aef922f5SJohn Dyson 501d076fbeaSAlan Cox vm_pager_put_pages(object, mc, count, flags, pageout_status); 50226f9a767SRodney W. Grimes 5031e8a675cSKonstantin Belousov runlen = count - mreq; 504126d6082SKonstantin Belousov if (eio != NULL) 505126d6082SKonstantin Belousov *eio = FALSE; 506aef922f5SJohn Dyson for (i = 0; i < count; i++) { 507aef922f5SJohn Dyson vm_page_t mt = mc[i]; 50824a1cce3SDavid Greenman 5094cd45723SAlan Cox KASSERT(pageout_status[i] == VM_PAGER_PEND || 5106031c68dSAlan Cox !pmap_page_is_write_mapped(mt), 5119ea8d1a6SAlan Cox ("vm_pageout_flush: page %p is not write protected", mt)); 51226f9a767SRodney W. Grimes switch (pageout_status[i]) { 51326f9a767SRodney W. Grimes case VM_PAGER_OK: 514ebcddc72SAlan Cox vm_page_lock(mt); 515ebcddc72SAlan Cox if (vm_page_in_laundry(mt)) 516ebcddc72SAlan Cox vm_page_deactivate_noreuse(mt); 517ebcddc72SAlan Cox vm_page_unlock(mt); 518ebcddc72SAlan Cox /* FALLTHROUGH */ 51926f9a767SRodney W. Grimes case VM_PAGER_PEND: 52095461b45SJohn Dyson numpagedout++; 52126f9a767SRodney W. Grimes break; 52226f9a767SRodney W. Grimes case VM_PAGER_BAD: 52326f9a767SRodney W. Grimes /* 524ebcddc72SAlan Cox * The page is outside the object's range. We pretend 525ebcddc72SAlan Cox * that the page out worked and clean the page, so the 526ebcddc72SAlan Cox * changes will be lost if the page is reclaimed by 527ebcddc72SAlan Cox * the page daemon. 52826f9a767SRodney W. Grimes */ 52990ecac61SMatthew Dillon vm_page_undirty(mt); 530ebcddc72SAlan Cox vm_page_lock(mt); 531ebcddc72SAlan Cox if (vm_page_in_laundry(mt)) 532ebcddc72SAlan Cox vm_page_deactivate_noreuse(mt); 533ebcddc72SAlan Cox vm_page_unlock(mt); 53426f9a767SRodney W. Grimes break; 53526f9a767SRodney W. Grimes case VM_PAGER_ERROR: 53626f9a767SRodney W. Grimes case VM_PAGER_FAIL: 53726f9a767SRodney W. Grimes /* 538b1fd102eSMark Johnston * If the page couldn't be paged out to swap because the 539b1fd102eSMark Johnston * pager wasn't able to find space, place the page in 540b1fd102eSMark Johnston * the PQ_UNSWAPPABLE holding queue. This is an 541b1fd102eSMark Johnston * optimization that prevents the page daemon from 542b1fd102eSMark Johnston * wasting CPU cycles on pages that cannot be reclaimed 543b1fd102eSMark Johnston * becase no swap device is configured. 544b1fd102eSMark Johnston * 545b1fd102eSMark Johnston * Otherwise, reactivate the page so that it doesn't 546b1fd102eSMark Johnston * clog the laundry and inactive queues. (We will try 547b1fd102eSMark Johnston * paging it out again later.) 54826f9a767SRodney W. Grimes */ 5493c4a2440SAlan Cox vm_page_lock(mt); 550b1fd102eSMark Johnston if (object->type == OBJT_SWAP && 551b1fd102eSMark Johnston pageout_status[i] == VM_PAGER_FAIL) { 552b1fd102eSMark Johnston vm_page_unswappable(mt); 553b1fd102eSMark Johnston numpagedout++; 554b1fd102eSMark Johnston } else 55524a1cce3SDavid Greenman vm_page_activate(mt); 5563c4a2440SAlan Cox vm_page_unlock(mt); 557126d6082SKonstantin Belousov if (eio != NULL && i >= mreq && i - mreq < runlen) 558126d6082SKonstantin Belousov *eio = TRUE; 55926f9a767SRodney W. Grimes break; 56026f9a767SRodney W. Grimes case VM_PAGER_AGAIN: 5611e8a675cSKonstantin Belousov if (i >= mreq && i - mreq < runlen) 5621e8a675cSKonstantin Belousov runlen = i - mreq; 56326f9a767SRodney W. Grimes break; 56426f9a767SRodney W. Grimes } 56526f9a767SRodney W. Grimes 56626f9a767SRodney W. Grimes /* 5670d94caffSDavid Greenman * If the operation is still going, leave the page busy to 5680d94caffSDavid Greenman * block all other accesses. Also, leave the paging in 5690d94caffSDavid Greenman * progress indicator set so that we don't attempt an object 5700d94caffSDavid Greenman * collapse. 57126f9a767SRodney W. Grimes */ 57226f9a767SRodney W. Grimes if (pageout_status[i] != VM_PAGER_PEND) { 573f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 574c7aebda8SAttilio Rao vm_page_sunbusy(mt); 5753c4a2440SAlan Cox } 5763c4a2440SAlan Cox } 5771e8a675cSKonstantin Belousov if (prunlen != NULL) 5781e8a675cSKonstantin Belousov *prunlen = runlen; 5793c4a2440SAlan Cox return (numpagedout); 58026f9a767SRodney W. Grimes } 58126f9a767SRodney W. Grimes 582b1fd102eSMark Johnston static void 583b1fd102eSMark Johnston vm_pageout_swapon(void *arg __unused, struct swdevt *sp __unused) 584b1fd102eSMark Johnston { 585b1fd102eSMark Johnston 586b1fd102eSMark Johnston atomic_store_rel_int(&swapdev_enabled, 1); 587b1fd102eSMark Johnston } 588b1fd102eSMark Johnston 589b1fd102eSMark Johnston static void 590b1fd102eSMark Johnston vm_pageout_swapoff(void *arg __unused, struct swdevt *sp __unused) 591b1fd102eSMark Johnston { 592b1fd102eSMark Johnston 593b1fd102eSMark Johnston if (swap_pager_nswapdev() == 1) 594b1fd102eSMark Johnston atomic_store_rel_int(&swapdev_enabled, 0); 595b1fd102eSMark Johnston } 596b1fd102eSMark Johnston 5971c7c3c6aSMatthew Dillon /* 59834d8b7eaSJeff Roberson * Attempt to acquire all of the necessary locks to launder a page and 59934d8b7eaSJeff Roberson * then call through the clustering layer to PUTPAGES. Wait a short 60034d8b7eaSJeff Roberson * time for a vnode lock. 60134d8b7eaSJeff Roberson * 60234d8b7eaSJeff Roberson * Requires the page and object lock on entry, releases both before return. 60334d8b7eaSJeff Roberson * Returns 0 on success and an errno otherwise. 60434d8b7eaSJeff Roberson */ 60534d8b7eaSJeff Roberson static int 606ebcddc72SAlan Cox vm_pageout_clean(vm_page_t m, int *numpagedout) 60734d8b7eaSJeff Roberson { 60834d8b7eaSJeff Roberson struct vnode *vp; 60934d8b7eaSJeff Roberson struct mount *mp; 61034d8b7eaSJeff Roberson vm_object_t object; 61134d8b7eaSJeff Roberson vm_pindex_t pindex; 61234d8b7eaSJeff Roberson int error, lockmode; 61334d8b7eaSJeff Roberson 614e8bcf696SMark Johnston vm_page_assert_locked(m); 61534d8b7eaSJeff Roberson object = m->object; 61634d8b7eaSJeff Roberson VM_OBJECT_ASSERT_WLOCKED(object); 61734d8b7eaSJeff Roberson error = 0; 61834d8b7eaSJeff Roberson vp = NULL; 61934d8b7eaSJeff Roberson mp = NULL; 62034d8b7eaSJeff Roberson 62134d8b7eaSJeff Roberson /* 62234d8b7eaSJeff Roberson * The object is already known NOT to be dead. It 62334d8b7eaSJeff Roberson * is possible for the vget() to block the whole 62434d8b7eaSJeff Roberson * pageout daemon, but the new low-memory handling 62534d8b7eaSJeff Roberson * code should prevent it. 62634d8b7eaSJeff Roberson * 62734d8b7eaSJeff Roberson * We can't wait forever for the vnode lock, we might 62834d8b7eaSJeff Roberson * deadlock due to a vn_read() getting stuck in 62934d8b7eaSJeff Roberson * vm_wait while holding this vnode. We skip the 63034d8b7eaSJeff Roberson * vnode if we can't get it in a reasonable amount 63134d8b7eaSJeff Roberson * of time. 63234d8b7eaSJeff Roberson */ 63334d8b7eaSJeff Roberson if (object->type == OBJT_VNODE) { 634e8bcf696SMark Johnston vm_page_unlock(m); 63563e97555SJeff Roberson vm_page_xunbusy(m); 63634d8b7eaSJeff Roberson vp = object->handle; 63734d8b7eaSJeff Roberson if (vp->v_type == VREG && 63834d8b7eaSJeff Roberson vn_start_write(vp, &mp, V_NOWAIT) != 0) { 63934d8b7eaSJeff Roberson mp = NULL; 64034d8b7eaSJeff Roberson error = EDEADLK; 64134d8b7eaSJeff Roberson goto unlock_all; 64234d8b7eaSJeff Roberson } 64334d8b7eaSJeff Roberson KASSERT(mp != NULL, 64434d8b7eaSJeff Roberson ("vp %p with NULL v_mount", vp)); 64534d8b7eaSJeff Roberson vm_object_reference_locked(object); 64634d8b7eaSJeff Roberson pindex = m->pindex; 64734d8b7eaSJeff Roberson VM_OBJECT_WUNLOCK(object); 64834d8b7eaSJeff Roberson lockmode = MNT_SHARED_WRITES(vp->v_mount) ? 64934d8b7eaSJeff Roberson LK_SHARED : LK_EXCLUSIVE; 65034d8b7eaSJeff Roberson if (vget(vp, lockmode | LK_TIMELOCK, curthread)) { 65134d8b7eaSJeff Roberson vp = NULL; 65234d8b7eaSJeff Roberson error = EDEADLK; 65334d8b7eaSJeff Roberson goto unlock_mp; 65434d8b7eaSJeff Roberson } 65534d8b7eaSJeff Roberson VM_OBJECT_WLOCK(object); 65657cd81a3SMark Johnston 65757cd81a3SMark Johnston /* 65857cd81a3SMark Johnston * Ensure that the object and vnode were not disassociated 65957cd81a3SMark Johnston * while locks were dropped. 66057cd81a3SMark Johnston */ 66157cd81a3SMark Johnston if (vp->v_object != object) { 66257cd81a3SMark Johnston error = ENOENT; 66357cd81a3SMark Johnston goto unlock_all; 66457cd81a3SMark Johnston } 665e8bcf696SMark Johnston vm_page_lock(m); 66657cd81a3SMark Johnston 66734d8b7eaSJeff Roberson /* 66834d8b7eaSJeff Roberson * While the object and page were unlocked, the page 66934d8b7eaSJeff Roberson * may have been: 67034d8b7eaSJeff Roberson * (1) moved to a different queue, 67134d8b7eaSJeff Roberson * (2) reallocated to a different object, 67234d8b7eaSJeff Roberson * (3) reallocated to a different offset, or 67334d8b7eaSJeff Roberson * (4) cleaned. 67434d8b7eaSJeff Roberson */ 675ebcddc72SAlan Cox if (!vm_page_in_laundry(m) || m->object != object || 67634d8b7eaSJeff Roberson m->pindex != pindex || m->dirty == 0) { 67734d8b7eaSJeff Roberson vm_page_unlock(m); 67834d8b7eaSJeff Roberson error = ENXIO; 67934d8b7eaSJeff Roberson goto unlock_all; 68034d8b7eaSJeff Roberson } 68134d8b7eaSJeff Roberson 68234d8b7eaSJeff Roberson /* 683fee2a2faSMark Johnston * The page may have been busied while the object and page 684fee2a2faSMark Johnston * locks were released. 68534d8b7eaSJeff Roberson */ 68663e97555SJeff Roberson if (vm_page_tryxbusy(m) == 0) { 68734d8b7eaSJeff Roberson vm_page_unlock(m); 68834d8b7eaSJeff Roberson error = EBUSY; 68934d8b7eaSJeff Roberson goto unlock_all; 69034d8b7eaSJeff Roberson } 69134d8b7eaSJeff Roberson } 69234d8b7eaSJeff Roberson 69334d8b7eaSJeff Roberson /* 694fee2a2faSMark Johnston * Remove all writeable mappings, failing if the page is wired. 695fee2a2faSMark Johnston */ 696fee2a2faSMark Johnston if (!vm_page_try_remove_write(m)) { 69763e97555SJeff Roberson vm_page_xunbusy(m); 698fee2a2faSMark Johnston vm_page_unlock(m); 699fee2a2faSMark Johnston error = EBUSY; 700fee2a2faSMark Johnston goto unlock_all; 701fee2a2faSMark Johnston } 702e8bcf696SMark Johnston vm_page_unlock(m); 703fee2a2faSMark Johnston 704fee2a2faSMark Johnston /* 70534d8b7eaSJeff Roberson * If a page is dirty, then it is either being washed 70634d8b7eaSJeff Roberson * (but not yet cleaned) or it is still in the 70734d8b7eaSJeff Roberson * laundry. If it is still in the laundry, then we 70834d8b7eaSJeff Roberson * start the cleaning operation. 70934d8b7eaSJeff Roberson */ 710ebcddc72SAlan Cox if ((*numpagedout = vm_pageout_cluster(m)) == 0) 71134d8b7eaSJeff Roberson error = EIO; 71234d8b7eaSJeff Roberson 71334d8b7eaSJeff Roberson unlock_all: 71434d8b7eaSJeff Roberson VM_OBJECT_WUNLOCK(object); 71534d8b7eaSJeff Roberson 71634d8b7eaSJeff Roberson unlock_mp: 71734d8b7eaSJeff Roberson vm_page_lock_assert(m, MA_NOTOWNED); 71834d8b7eaSJeff Roberson if (mp != NULL) { 71934d8b7eaSJeff Roberson if (vp != NULL) 72034d8b7eaSJeff Roberson vput(vp); 72134d8b7eaSJeff Roberson vm_object_deallocate(object); 72234d8b7eaSJeff Roberson vn_finished_write(mp); 72334d8b7eaSJeff Roberson } 72434d8b7eaSJeff Roberson 72534d8b7eaSJeff Roberson return (error); 72634d8b7eaSJeff Roberson } 72734d8b7eaSJeff Roberson 72834d8b7eaSJeff Roberson /* 729ebcddc72SAlan Cox * Attempt to launder the specified number of pages. 730ebcddc72SAlan Cox * 731ebcddc72SAlan Cox * Returns the number of pages successfully laundered. 732ebcddc72SAlan Cox */ 733ebcddc72SAlan Cox static int 734ebcddc72SAlan Cox vm_pageout_launder(struct vm_domain *vmd, int launder, bool in_shortfall) 735ebcddc72SAlan Cox { 7365cd29d0fSMark Johnston struct scan_state ss; 737ebcddc72SAlan Cox struct vm_pagequeue *pq; 738e8bcf696SMark Johnston struct mtx *mtx; 739ebcddc72SAlan Cox vm_object_t object; 7405cd29d0fSMark Johnston vm_page_t m, marker; 741f3f38e25SMark Johnston vm_page_astate_t new, old; 742f3f38e25SMark Johnston int act_delta, error, numpagedout, queue, refs, starting_target; 743ebcddc72SAlan Cox int vnodes_skipped; 74460256604SMark Johnston bool pageout_ok; 745ebcddc72SAlan Cox 746e8bcf696SMark Johnston mtx = NULL; 7475cd29d0fSMark Johnston object = NULL; 748ebcddc72SAlan Cox starting_target = launder; 749ebcddc72SAlan Cox vnodes_skipped = 0; 750ebcddc72SAlan Cox 751ebcddc72SAlan Cox /* 752b1fd102eSMark Johnston * Scan the laundry queues for pages eligible to be laundered. We stop 753ebcddc72SAlan Cox * once the target number of dirty pages have been laundered, or once 754ebcddc72SAlan Cox * we've reached the end of the queue. A single iteration of this loop 755ebcddc72SAlan Cox * may cause more than one page to be laundered because of clustering. 756ebcddc72SAlan Cox * 757b1fd102eSMark Johnston * As an optimization, we avoid laundering from PQ_UNSWAPPABLE when no 758b1fd102eSMark Johnston * swap devices are configured. 759ebcddc72SAlan Cox */ 760b1fd102eSMark Johnston if (atomic_load_acq_int(&swapdev_enabled)) 76164b38930SMark Johnston queue = PQ_UNSWAPPABLE; 762b1fd102eSMark Johnston else 76364b38930SMark Johnston queue = PQ_LAUNDRY; 764ebcddc72SAlan Cox 765b1fd102eSMark Johnston scan: 76664b38930SMark Johnston marker = &vmd->vmd_markers[queue]; 7675cd29d0fSMark Johnston pq = &vmd->vmd_pagequeues[queue]; 768ebcddc72SAlan Cox vm_pagequeue_lock(pq); 7695cd29d0fSMark Johnston vm_pageout_init_scan(&ss, pq, marker, NULL, pq->pq_cnt); 7705cd29d0fSMark Johnston while (launder > 0 && (m = vm_pageout_next(&ss, false)) != NULL) { 7715cd29d0fSMark Johnston if (__predict_false((m->flags & PG_MARKER) != 0)) 772ebcddc72SAlan Cox continue; 7735cd29d0fSMark Johnston 774e8bcf696SMark Johnston vm_page_change_lock(m, &mtx); 775e8bcf696SMark Johnston 776e8bcf696SMark Johnston recheck: 7775cd29d0fSMark Johnston /* 778*b7f30bffSMark Johnston * Don't touch a page that was removed from the queue after the 779*b7f30bffSMark Johnston * page queue lock was released. Otherwise, ensure that any 780*b7f30bffSMark Johnston * pending queue operations, such as dequeues for wired pages, 781*b7f30bffSMark Johnston * are handled. 7825cd29d0fSMark Johnston */ 783*b7f30bffSMark Johnston if (vm_pageout_defer(m, queue, true)) 784ebcddc72SAlan Cox continue; 785e8bcf696SMark Johnston 7865cd29d0fSMark Johnston if (object != m->object) { 78760256604SMark Johnston if (object != NULL) 7885cd29d0fSMark Johnston VM_OBJECT_WUNLOCK(object); 789e8bcf696SMark Johnston 790e8bcf696SMark Johnston /* 791e8bcf696SMark Johnston * A page's object pointer may be set to NULL before 792e8bcf696SMark Johnston * the object lock is acquired. 793e8bcf696SMark Johnston */ 794fee2a2faSMark Johnston object = (vm_object_t)atomic_load_ptr(&m->object); 795e8bcf696SMark Johnston if (object != NULL && !VM_OBJECT_TRYWLOCK(object)) { 796e8bcf696SMark Johnston mtx_unlock(mtx); 797e8bcf696SMark Johnston /* Depends on type-stability. */ 79841fd4b94SMark Johnston VM_OBJECT_WLOCK(object); 799e8bcf696SMark Johnston mtx_lock(mtx); 800e8bcf696SMark Johnston goto recheck; 801e8bcf696SMark Johnston } 802e8bcf696SMark Johnston } 803e8bcf696SMark Johnston if (__predict_false(m->object == NULL)) 804e8bcf696SMark Johnston /* 805e8bcf696SMark Johnston * The page has been removed from its object. 806e8bcf696SMark Johnston */ 80741fd4b94SMark Johnston continue; 808e8bcf696SMark Johnston KASSERT(m->object == object, ("page %p does not belong to %p", 809e8bcf696SMark Johnston m, object)); 8105cd29d0fSMark Johnston 81163e97555SJeff Roberson if (vm_page_tryxbusy(m) == 0) 8125cd29d0fSMark Johnston continue; 813ebcddc72SAlan Cox 814ebcddc72SAlan Cox /* 815*b7f30bffSMark Johnston * Check for wirings now that we hold the object lock and have 816*b7f30bffSMark Johnston * verified that the page is unbusied. If the page is mapped, 817*b7f30bffSMark Johnston * it may still be wired by pmap lookups. The call to 818fee2a2faSMark Johnston * vm_page_try_remove_all() below atomically checks for such 819fee2a2faSMark Johnston * wirings and removes mappings. If the page is unmapped, the 820fee2a2faSMark Johnston * wire count is guaranteed not to increase. 821fee2a2faSMark Johnston */ 822fee2a2faSMark Johnston if (__predict_false(vm_page_wired(m))) { 823e8bcf696SMark Johnston vm_page_dequeue_deferred(m); 824f3f38e25SMark Johnston goto skip_page; 825fee2a2faSMark Johnston } 826fee2a2faSMark Johnston 827fee2a2faSMark Johnston /* 828ebcddc72SAlan Cox * Invalid pages can be easily freed. They cannot be 829ebcddc72SAlan Cox * mapped; vm_page_free() asserts this. 830ebcddc72SAlan Cox */ 8310012f373SJeff Roberson if (vm_page_none_valid(m)) 832ebcddc72SAlan Cox goto free_page; 833ebcddc72SAlan Cox 834f3f38e25SMark Johnston refs = object->ref_count != 0 ? pmap_ts_referenced(m) : 0; 835f3f38e25SMark Johnston 836f3f38e25SMark Johnston for (old = vm_page_astate_load(m);;) { 837ebcddc72SAlan Cox /* 838f3f38e25SMark Johnston * Check to see if the page has been removed from the 839f3f38e25SMark Johnston * queue since the first such check. Leave it alone if 840f3f38e25SMark Johnston * so, discarding any references collected by 841f3f38e25SMark Johnston * pmap_ts_referenced(). 842ebcddc72SAlan Cox */ 843f3f38e25SMark Johnston if (__predict_false(_vm_page_queue(old) == PQ_NONE)) 844f3f38e25SMark Johnston goto skip_page; 845f3f38e25SMark Johnston 846f3f38e25SMark Johnston new = old; 847f3f38e25SMark Johnston act_delta = refs; 848f3f38e25SMark Johnston if ((old.flags & PGA_REFERENCED) != 0) { 849f3f38e25SMark Johnston new.flags &= ~PGA_REFERENCED; 850d7aeb429SAlan Cox act_delta++; 851ebcddc72SAlan Cox } 852f3f38e25SMark Johnston if (act_delta == 0) { 853f3f38e25SMark Johnston ; 854f3f38e25SMark Johnston } else if (object->ref_count != 0) { 855ebcddc72SAlan Cox /* 856f3f38e25SMark Johnston * Increase the activation count if the page was 857f3f38e25SMark Johnston * referenced while in the laundry queue. This 858f3f38e25SMark Johnston * makes it less likely that the page will be 859f3f38e25SMark Johnston * returned prematurely to the laundry queue. 860e8bcf696SMark Johnston */ 861f3f38e25SMark Johnston new.act_count += ACT_ADVANCE + 862f3f38e25SMark Johnston act_delta; 863f3f38e25SMark Johnston if (new.act_count > ACT_MAX) 864f3f38e25SMark Johnston new.act_count = ACT_MAX; 865f3f38e25SMark Johnston 866f3f38e25SMark Johnston new.flags |= PGA_REQUEUE; 867f3f38e25SMark Johnston new.queue = PQ_ACTIVE; 868f3f38e25SMark Johnston if (!vm_page_pqstate_commit(m, &old, new)) 869f3f38e25SMark Johnston continue; 870e8bcf696SMark Johnston 871e8bcf696SMark Johnston /* 872e8bcf696SMark Johnston * If this was a background laundering, count 873e8bcf696SMark Johnston * activated pages towards our target. The 874e8bcf696SMark Johnston * purpose of background laundering is to ensure 875e8bcf696SMark Johnston * that pages are eventually cycled through the 876e8bcf696SMark Johnston * laundry queue, and an activation is a valid 877e8bcf696SMark Johnston * way out. 878ebcddc72SAlan Cox */ 879ebcddc72SAlan Cox if (!in_shortfall) 880ebcddc72SAlan Cox launder--; 881f3f38e25SMark Johnston VM_CNT_INC(v_reactivated); 882f3f38e25SMark Johnston goto skip_page; 8835cd29d0fSMark Johnston } else if ((object->flags & OBJ_DEAD) == 0) { 884f3f38e25SMark Johnston new.flags |= PGA_REQUEUE; 885f3f38e25SMark Johnston if (!vm_page_pqstate_commit(m, &old, new)) 886e8bcf696SMark Johnston continue; 887f3f38e25SMark Johnston goto skip_page; 8885cd29d0fSMark Johnston } 889f3f38e25SMark Johnston break; 890ebcddc72SAlan Cox } 891ebcddc72SAlan Cox 892ebcddc72SAlan Cox /* 893ebcddc72SAlan Cox * If the page appears to be clean at the machine-independent 894ebcddc72SAlan Cox * layer, then remove all of its mappings from the pmap in 895ebcddc72SAlan Cox * anticipation of freeing it. If, however, any of the page's 896ebcddc72SAlan Cox * mappings allow write access, then the page may still be 897ebcddc72SAlan Cox * modified until the last of those mappings are removed. 898ebcddc72SAlan Cox */ 899ebcddc72SAlan Cox if (object->ref_count != 0) { 900ebcddc72SAlan Cox vm_page_test_dirty(m); 901fee2a2faSMark Johnston if (m->dirty == 0 && !vm_page_try_remove_all(m)) { 902e8bcf696SMark Johnston vm_page_dequeue_deferred(m); 903f3f38e25SMark Johnston goto skip_page; 904fee2a2faSMark Johnston } 905ebcddc72SAlan Cox } 906ebcddc72SAlan Cox 907ebcddc72SAlan Cox /* 908ebcddc72SAlan Cox * Clean pages are freed, and dirty pages are paged out unless 909ebcddc72SAlan Cox * they belong to a dead object. Requeueing dirty pages from 910ebcddc72SAlan Cox * dead objects is pointless, as they are being paged out and 911ebcddc72SAlan Cox * freed by the thread that destroyed the object. 912ebcddc72SAlan Cox */ 913ebcddc72SAlan Cox if (m->dirty == 0) { 914ebcddc72SAlan Cox free_page: 915ebcddc72SAlan Cox vm_page_free(m); 91683c9dea1SGleb Smirnoff VM_CNT_INC(v_dfree); 917ebcddc72SAlan Cox } else if ((object->flags & OBJ_DEAD) == 0) { 918ebcddc72SAlan Cox if (object->type != OBJT_SWAP && 919ebcddc72SAlan Cox object->type != OBJT_DEFAULT) 920ebcddc72SAlan Cox pageout_ok = true; 921ebcddc72SAlan Cox else if (disable_swap_pageouts) 922ebcddc72SAlan Cox pageout_ok = false; 923ebcddc72SAlan Cox else 924ebcddc72SAlan Cox pageout_ok = true; 925ebcddc72SAlan Cox if (!pageout_ok) { 926f3f38e25SMark Johnston vm_page_launder(m); 927f3f38e25SMark Johnston goto skip_page; 928ebcddc72SAlan Cox } 929ebcddc72SAlan Cox 930ebcddc72SAlan Cox /* 931ebcddc72SAlan Cox * Form a cluster with adjacent, dirty pages from the 932ebcddc72SAlan Cox * same object, and page out that entire cluster. 933ebcddc72SAlan Cox * 934ebcddc72SAlan Cox * The adjacent, dirty pages must also be in the 935ebcddc72SAlan Cox * laundry. However, their mappings are not checked 936ebcddc72SAlan Cox * for new references. Consequently, a recently 937ebcddc72SAlan Cox * referenced page may be paged out. However, that 938ebcddc72SAlan Cox * page will not be prematurely reclaimed. After page 939ebcddc72SAlan Cox * out, the page will be placed in the inactive queue, 940ebcddc72SAlan Cox * where any new references will be detected and the 941ebcddc72SAlan Cox * page reactivated. 942ebcddc72SAlan Cox */ 943ebcddc72SAlan Cox error = vm_pageout_clean(m, &numpagedout); 944ebcddc72SAlan Cox if (error == 0) { 945ebcddc72SAlan Cox launder -= numpagedout; 9465cd29d0fSMark Johnston ss.scanned += numpagedout; 947ebcddc72SAlan Cox } else if (error == EDEADLK) { 948ebcddc72SAlan Cox pageout_lock_miss++; 949ebcddc72SAlan Cox vnodes_skipped++; 950ebcddc72SAlan Cox } 951e8bcf696SMark Johnston mtx = NULL; 95260256604SMark Johnston object = NULL; 953f3f38e25SMark Johnston } else { 954f3f38e25SMark Johnston skip_page: 95563e97555SJeff Roberson vm_page_xunbusy(m); 956e8bcf696SMark Johnston } 957f3f38e25SMark Johnston } 958e8bcf696SMark Johnston if (mtx != NULL) { 959e8bcf696SMark Johnston mtx_unlock(mtx); 960e8bcf696SMark Johnston mtx = NULL; 96146e39081SMark Johnston } 96246e39081SMark Johnston if (object != NULL) { 963ebcddc72SAlan Cox VM_OBJECT_WUNLOCK(object); 96446e39081SMark Johnston object = NULL; 96546e39081SMark Johnston } 966ebcddc72SAlan Cox vm_pagequeue_lock(pq); 9675cd29d0fSMark Johnston vm_pageout_end_scan(&ss); 968ebcddc72SAlan Cox vm_pagequeue_unlock(pq); 969ebcddc72SAlan Cox 97064b38930SMark Johnston if (launder > 0 && queue == PQ_UNSWAPPABLE) { 97164b38930SMark Johnston queue = PQ_LAUNDRY; 972b1fd102eSMark Johnston goto scan; 973b1fd102eSMark Johnston } 974b1fd102eSMark Johnston 975ebcddc72SAlan Cox /* 976ebcddc72SAlan Cox * Wakeup the sync daemon if we skipped a vnode in a writeable object 977ebcddc72SAlan Cox * and we didn't launder enough pages. 978ebcddc72SAlan Cox */ 979ebcddc72SAlan Cox if (vnodes_skipped > 0 && launder > 0) 980ebcddc72SAlan Cox (void)speedup_syncer(); 981ebcddc72SAlan Cox 982ebcddc72SAlan Cox return (starting_target - launder); 983ebcddc72SAlan Cox } 984ebcddc72SAlan Cox 985ebcddc72SAlan Cox /* 986ebcddc72SAlan Cox * Compute the integer square root. 987ebcddc72SAlan Cox */ 988ebcddc72SAlan Cox static u_int 989ebcddc72SAlan Cox isqrt(u_int num) 990ebcddc72SAlan Cox { 991ebcddc72SAlan Cox u_int bit, root, tmp; 992ebcddc72SAlan Cox 99364f8d257SDoug Moore bit = num != 0 ? (1u << ((fls(num) - 1) & ~1)) : 0; 994ebcddc72SAlan Cox root = 0; 995ebcddc72SAlan Cox while (bit != 0) { 996ebcddc72SAlan Cox tmp = root + bit; 997ebcddc72SAlan Cox root >>= 1; 998ebcddc72SAlan Cox if (num >= tmp) { 999ebcddc72SAlan Cox num -= tmp; 1000ebcddc72SAlan Cox root += bit; 1001ebcddc72SAlan Cox } 1002ebcddc72SAlan Cox bit >>= 2; 1003ebcddc72SAlan Cox } 1004ebcddc72SAlan Cox return (root); 1005ebcddc72SAlan Cox } 1006ebcddc72SAlan Cox 1007ebcddc72SAlan Cox /* 1008ebcddc72SAlan Cox * Perform the work of the laundry thread: periodically wake up and determine 1009ebcddc72SAlan Cox * whether any pages need to be laundered. If so, determine the number of pages 1010ebcddc72SAlan Cox * that need to be laundered, and launder them. 1011ebcddc72SAlan Cox */ 1012ebcddc72SAlan Cox static void 1013ebcddc72SAlan Cox vm_pageout_laundry_worker(void *arg) 1014ebcddc72SAlan Cox { 1015e2068d0bSJeff Roberson struct vm_domain *vmd; 1016ebcddc72SAlan Cox struct vm_pagequeue *pq; 101760684862SMark Johnston uint64_t nclean, ndirty, nfreed; 1018e2068d0bSJeff Roberson int domain, last_target, launder, shortfall, shortfall_cycle, target; 1019ebcddc72SAlan Cox bool in_shortfall; 1020ebcddc72SAlan Cox 1021e2068d0bSJeff Roberson domain = (uintptr_t)arg; 1022e2068d0bSJeff Roberson vmd = VM_DOMAIN(domain); 1023e2068d0bSJeff Roberson pq = &vmd->vmd_pagequeues[PQ_LAUNDRY]; 1024e2068d0bSJeff Roberson KASSERT(vmd->vmd_segs != 0, ("domain without segments")); 1025ebcddc72SAlan Cox 1026ebcddc72SAlan Cox shortfall = 0; 1027ebcddc72SAlan Cox in_shortfall = false; 1028ebcddc72SAlan Cox shortfall_cycle = 0; 10298002c3a4SMark Johnston last_target = target = 0; 103060684862SMark Johnston nfreed = 0; 1031ebcddc72SAlan Cox 1032ebcddc72SAlan Cox /* 1033b1fd102eSMark Johnston * Calls to these handlers are serialized by the swap syscall lock. 1034b1fd102eSMark Johnston */ 1035e2068d0bSJeff Roberson (void)EVENTHANDLER_REGISTER(swapon, vm_pageout_swapon, vmd, 1036b1fd102eSMark Johnston EVENTHANDLER_PRI_ANY); 1037e2068d0bSJeff Roberson (void)EVENTHANDLER_REGISTER(swapoff, vm_pageout_swapoff, vmd, 1038b1fd102eSMark Johnston EVENTHANDLER_PRI_ANY); 1039b1fd102eSMark Johnston 1040b1fd102eSMark Johnston /* 1041ebcddc72SAlan Cox * The pageout laundry worker is never done, so loop forever. 1042ebcddc72SAlan Cox */ 1043ebcddc72SAlan Cox for (;;) { 1044ebcddc72SAlan Cox KASSERT(target >= 0, ("negative target %d", target)); 1045ebcddc72SAlan Cox KASSERT(shortfall_cycle >= 0, 1046ebcddc72SAlan Cox ("negative cycle %d", shortfall_cycle)); 1047ebcddc72SAlan Cox launder = 0; 1048ebcddc72SAlan Cox 1049ebcddc72SAlan Cox /* 1050ebcddc72SAlan Cox * First determine whether we need to launder pages to meet a 1051ebcddc72SAlan Cox * shortage of free pages. 1052ebcddc72SAlan Cox */ 1053ebcddc72SAlan Cox if (shortfall > 0) { 1054ebcddc72SAlan Cox in_shortfall = true; 1055ebcddc72SAlan Cox shortfall_cycle = VM_LAUNDER_RATE / VM_INACT_SCAN_RATE; 1056ebcddc72SAlan Cox target = shortfall; 1057ebcddc72SAlan Cox } else if (!in_shortfall) 1058ebcddc72SAlan Cox goto trybackground; 1059e2068d0bSJeff Roberson else if (shortfall_cycle == 0 || vm_laundry_target(vmd) <= 0) { 1060ebcddc72SAlan Cox /* 1061ebcddc72SAlan Cox * We recently entered shortfall and began laundering 1062ebcddc72SAlan Cox * pages. If we have completed that laundering run 1063ebcddc72SAlan Cox * (and we are no longer in shortfall) or we have met 1064ebcddc72SAlan Cox * our laundry target through other activity, then we 1065ebcddc72SAlan Cox * can stop laundering pages. 1066ebcddc72SAlan Cox */ 1067ebcddc72SAlan Cox in_shortfall = false; 1068ebcddc72SAlan Cox target = 0; 1069ebcddc72SAlan Cox goto trybackground; 1070ebcddc72SAlan Cox } 1071ebcddc72SAlan Cox launder = target / shortfall_cycle--; 1072ebcddc72SAlan Cox goto dolaundry; 1073ebcddc72SAlan Cox 1074ebcddc72SAlan Cox /* 1075ebcddc72SAlan Cox * There's no immediate need to launder any pages; see if we 1076ebcddc72SAlan Cox * meet the conditions to perform background laundering: 1077ebcddc72SAlan Cox * 1078ebcddc72SAlan Cox * 1. The ratio of dirty to clean inactive pages exceeds the 107960684862SMark Johnston * background laundering threshold, or 1080ebcddc72SAlan Cox * 2. we haven't yet reached the target of the current 1081ebcddc72SAlan Cox * background laundering run. 1082ebcddc72SAlan Cox * 1083ebcddc72SAlan Cox * The background laundering threshold is not a constant. 1084ebcddc72SAlan Cox * Instead, it is a slowly growing function of the number of 108560684862SMark Johnston * clean pages freed by the page daemon since the last 108660684862SMark Johnston * background laundering. Thus, as the ratio of dirty to 108760684862SMark Johnston * clean inactive pages grows, the amount of memory pressure 1088c098768eSMark Johnston * required to trigger laundering decreases. We ensure 1089c098768eSMark Johnston * that the threshold is non-zero after an inactive queue 1090c098768eSMark Johnston * scan, even if that scan failed to free a single clean page. 1091ebcddc72SAlan Cox */ 1092ebcddc72SAlan Cox trybackground: 1093e2068d0bSJeff Roberson nclean = vmd->vmd_free_count + 1094e2068d0bSJeff Roberson vmd->vmd_pagequeues[PQ_INACTIVE].pq_cnt; 1095e2068d0bSJeff Roberson ndirty = vmd->vmd_pagequeues[PQ_LAUNDRY].pq_cnt; 1096c098768eSMark Johnston if (target == 0 && ndirty * isqrt(howmany(nfreed + 1, 1097c098768eSMark Johnston vmd->vmd_free_target - vmd->vmd_free_min)) >= nclean) { 1098e2068d0bSJeff Roberson target = vmd->vmd_background_launder_target; 1099ebcddc72SAlan Cox } 1100ebcddc72SAlan Cox 1101ebcddc72SAlan Cox /* 1102ebcddc72SAlan Cox * We have a non-zero background laundering target. If we've 1103ebcddc72SAlan Cox * laundered up to our maximum without observing a page daemon 1104cb35676eSMark Johnston * request, just stop. This is a safety belt that ensures we 1105ebcddc72SAlan Cox * don't launder an excessive amount if memory pressure is low 1106ebcddc72SAlan Cox * and the ratio of dirty to clean pages is large. Otherwise, 1107ebcddc72SAlan Cox * proceed at the background laundering rate. 1108ebcddc72SAlan Cox */ 1109ebcddc72SAlan Cox if (target > 0) { 111060684862SMark Johnston if (nfreed > 0) { 111160684862SMark Johnston nfreed = 0; 1112ebcddc72SAlan Cox last_target = target; 1113ebcddc72SAlan Cox } else if (last_target - target >= 1114ebcddc72SAlan Cox vm_background_launder_max * PAGE_SIZE / 1024) { 1115ebcddc72SAlan Cox target = 0; 1116ebcddc72SAlan Cox } 1117ebcddc72SAlan Cox launder = vm_background_launder_rate * PAGE_SIZE / 1024; 1118ebcddc72SAlan Cox launder /= VM_LAUNDER_RATE; 1119ebcddc72SAlan Cox if (launder > target) 1120ebcddc72SAlan Cox launder = target; 1121ebcddc72SAlan Cox } 1122ebcddc72SAlan Cox 1123ebcddc72SAlan Cox dolaundry: 1124ebcddc72SAlan Cox if (launder > 0) { 1125ebcddc72SAlan Cox /* 1126ebcddc72SAlan Cox * Because of I/O clustering, the number of laundered 1127ebcddc72SAlan Cox * pages could exceed "target" by the maximum size of 1128ebcddc72SAlan Cox * a cluster minus one. 1129ebcddc72SAlan Cox */ 1130e2068d0bSJeff Roberson target -= min(vm_pageout_launder(vmd, launder, 1131ebcddc72SAlan Cox in_shortfall), target); 1132ebcddc72SAlan Cox pause("laundp", hz / VM_LAUNDER_RATE); 1133ebcddc72SAlan Cox } 1134ebcddc72SAlan Cox 1135ebcddc72SAlan Cox /* 1136ebcddc72SAlan Cox * If we're not currently laundering pages and the page daemon 1137ebcddc72SAlan Cox * hasn't posted a new request, sleep until the page daemon 1138ebcddc72SAlan Cox * kicks us. 1139ebcddc72SAlan Cox */ 1140ebcddc72SAlan Cox vm_pagequeue_lock(pq); 1141e2068d0bSJeff Roberson if (target == 0 && vmd->vmd_laundry_request == VM_LAUNDRY_IDLE) 1142e2068d0bSJeff Roberson (void)mtx_sleep(&vmd->vmd_laundry_request, 1143ebcddc72SAlan Cox vm_pagequeue_lockptr(pq), PVM, "launds", 0); 1144ebcddc72SAlan Cox 1145ebcddc72SAlan Cox /* 1146ebcddc72SAlan Cox * If the pagedaemon has indicated that it's in shortfall, start 1147ebcddc72SAlan Cox * a shortfall laundering unless we're already in the middle of 1148ebcddc72SAlan Cox * one. This may preempt a background laundering. 1149ebcddc72SAlan Cox */ 1150e2068d0bSJeff Roberson if (vmd->vmd_laundry_request == VM_LAUNDRY_SHORTFALL && 1151ebcddc72SAlan Cox (!in_shortfall || shortfall_cycle == 0)) { 1152e2068d0bSJeff Roberson shortfall = vm_laundry_target(vmd) + 1153e2068d0bSJeff Roberson vmd->vmd_pageout_deficit; 1154ebcddc72SAlan Cox target = 0; 1155ebcddc72SAlan Cox } else 1156ebcddc72SAlan Cox shortfall = 0; 1157ebcddc72SAlan Cox 1158ebcddc72SAlan Cox if (target == 0) 1159e2068d0bSJeff Roberson vmd->vmd_laundry_request = VM_LAUNDRY_IDLE; 116060684862SMark Johnston nfreed += vmd->vmd_clean_pages_freed; 116160684862SMark Johnston vmd->vmd_clean_pages_freed = 0; 1162ebcddc72SAlan Cox vm_pagequeue_unlock(pq); 1163ebcddc72SAlan Cox } 1164ebcddc72SAlan Cox } 1165ebcddc72SAlan Cox 1166be37ee79SMark Johnston /* 1167be37ee79SMark Johnston * Compute the number of pages we want to try to move from the 1168be37ee79SMark Johnston * active queue to either the inactive or laundry queue. 1169be37ee79SMark Johnston * 11707bb4634eSMark Johnston * When scanning active pages during a shortage, we make clean pages 11717bb4634eSMark Johnston * count more heavily towards the page shortage than dirty pages. 11727bb4634eSMark Johnston * This is because dirty pages must be laundered before they can be 11737bb4634eSMark Johnston * reused and thus have less utility when attempting to quickly 11747bb4634eSMark Johnston * alleviate a free page shortage. However, this weighting also 11757bb4634eSMark Johnston * causes the scan to deactivate dirty pages more aggressively, 11767bb4634eSMark Johnston * improving the effectiveness of clustering. 1177be37ee79SMark Johnston */ 1178be37ee79SMark Johnston static int 11797bb4634eSMark Johnston vm_pageout_active_target(struct vm_domain *vmd) 1180be37ee79SMark Johnston { 1181be37ee79SMark Johnston int shortage; 1182be37ee79SMark Johnston 1183be37ee79SMark Johnston shortage = vmd->vmd_inactive_target + vm_paging_target(vmd) - 1184be37ee79SMark Johnston (vmd->vmd_pagequeues[PQ_INACTIVE].pq_cnt + 1185be37ee79SMark Johnston vmd->vmd_pagequeues[PQ_LAUNDRY].pq_cnt / act_scan_laundry_weight); 1186be37ee79SMark Johnston shortage *= act_scan_laundry_weight; 1187be37ee79SMark Johnston return (shortage); 1188be37ee79SMark Johnston } 1189be37ee79SMark Johnston 1190be37ee79SMark Johnston /* 1191be37ee79SMark Johnston * Scan the active queue. If there is no shortage of inactive pages, scan a 1192be37ee79SMark Johnston * small portion of the queue in order to maintain quasi-LRU. 1193be37ee79SMark Johnston */ 1194be37ee79SMark Johnston static void 1195be37ee79SMark Johnston vm_pageout_scan_active(struct vm_domain *vmd, int page_shortage) 1196be37ee79SMark Johnston { 1197be37ee79SMark Johnston struct scan_state ss; 1198e8bcf696SMark Johnston struct mtx *mtx; 1199fee2a2faSMark Johnston vm_object_t object; 1200be37ee79SMark Johnston vm_page_t m, marker; 1201be37ee79SMark Johnston struct vm_pagequeue *pq; 1202f3f38e25SMark Johnston vm_page_astate_t old, new; 1203be37ee79SMark Johnston long min_scan; 1204f3f38e25SMark Johnston int act_delta, max_scan, ps_delta, refs, scan_tick; 1205f3f38e25SMark Johnston uint8_t nqueue; 1206be37ee79SMark Johnston 1207be37ee79SMark Johnston marker = &vmd->vmd_markers[PQ_ACTIVE]; 1208be37ee79SMark Johnston pq = &vmd->vmd_pagequeues[PQ_ACTIVE]; 1209be37ee79SMark Johnston vm_pagequeue_lock(pq); 1210be37ee79SMark Johnston 1211be37ee79SMark Johnston /* 1212be37ee79SMark Johnston * If we're just idle polling attempt to visit every 1213be37ee79SMark Johnston * active page within 'update_period' seconds. 1214be37ee79SMark Johnston */ 1215be37ee79SMark Johnston scan_tick = ticks; 1216be37ee79SMark Johnston if (vm_pageout_update_period != 0) { 1217be37ee79SMark Johnston min_scan = pq->pq_cnt; 1218be37ee79SMark Johnston min_scan *= scan_tick - vmd->vmd_last_active_scan; 1219be37ee79SMark Johnston min_scan /= hz * vm_pageout_update_period; 1220be37ee79SMark Johnston } else 1221be37ee79SMark Johnston min_scan = 0; 1222be37ee79SMark Johnston if (min_scan > 0 || (page_shortage > 0 && pq->pq_cnt > 0)) 1223be37ee79SMark Johnston vmd->vmd_last_active_scan = scan_tick; 1224be37ee79SMark Johnston 1225be37ee79SMark Johnston /* 1226be37ee79SMark Johnston * Scan the active queue for pages that can be deactivated. Update 1227be37ee79SMark Johnston * the per-page activity counter and use it to identify deactivation 1228be37ee79SMark Johnston * candidates. Held pages may be deactivated. 1229be37ee79SMark Johnston * 1230be37ee79SMark Johnston * To avoid requeuing each page that remains in the active queue, we 12317bb4634eSMark Johnston * implement the CLOCK algorithm. To keep the implementation of the 12327bb4634eSMark Johnston * enqueue operation consistent for all page queues, we use two hands, 12337bb4634eSMark Johnston * represented by marker pages. Scans begin at the first hand, which 12347bb4634eSMark Johnston * precedes the second hand in the queue. When the two hands meet, 12357bb4634eSMark Johnston * they are moved back to the head and tail of the queue, respectively, 12367bb4634eSMark Johnston * and scanning resumes. 1237be37ee79SMark Johnston */ 1238be37ee79SMark Johnston max_scan = page_shortage > 0 ? pq->pq_cnt : min_scan; 1239e8bcf696SMark Johnston mtx = NULL; 1240be37ee79SMark Johnston act_scan: 1241be37ee79SMark Johnston vm_pageout_init_scan(&ss, pq, marker, &vmd->vmd_clock[0], max_scan); 1242be37ee79SMark Johnston while ((m = vm_pageout_next(&ss, false)) != NULL) { 1243be37ee79SMark Johnston if (__predict_false(m == &vmd->vmd_clock[1])) { 1244be37ee79SMark Johnston vm_pagequeue_lock(pq); 1245be37ee79SMark Johnston TAILQ_REMOVE(&pq->pq_pl, &vmd->vmd_clock[0], plinks.q); 1246be37ee79SMark Johnston TAILQ_REMOVE(&pq->pq_pl, &vmd->vmd_clock[1], plinks.q); 1247be37ee79SMark Johnston TAILQ_INSERT_HEAD(&pq->pq_pl, &vmd->vmd_clock[0], 1248be37ee79SMark Johnston plinks.q); 1249be37ee79SMark Johnston TAILQ_INSERT_TAIL(&pq->pq_pl, &vmd->vmd_clock[1], 1250be37ee79SMark Johnston plinks.q); 1251be37ee79SMark Johnston max_scan -= ss.scanned; 1252be37ee79SMark Johnston vm_pageout_end_scan(&ss); 1253be37ee79SMark Johnston goto act_scan; 1254be37ee79SMark Johnston } 1255be37ee79SMark Johnston if (__predict_false((m->flags & PG_MARKER) != 0)) 1256be37ee79SMark Johnston continue; 1257be37ee79SMark Johnston 1258e8bcf696SMark Johnston vm_page_change_lock(m, &mtx); 1259e8bcf696SMark Johnston 1260e8bcf696SMark Johnston /* 1261*b7f30bffSMark Johnston * Don't touch a page that was removed from the queue after the 1262*b7f30bffSMark Johnston * page queue lock was released. Otherwise, ensure that any 1263*b7f30bffSMark Johnston * pending queue operations, such as dequeues for wired pages, 1264*b7f30bffSMark Johnston * are handled. 1265e8bcf696SMark Johnston */ 1266*b7f30bffSMark Johnston if (vm_pageout_defer(m, PQ_ACTIVE, true)) 1267e8bcf696SMark Johnston continue; 1268e8bcf696SMark Johnston 1269e8bcf696SMark Johnston /* 1270e8bcf696SMark Johnston * A page's object pointer may be set to NULL before 1271e8bcf696SMark Johnston * the object lock is acquired. 1272e8bcf696SMark Johnston */ 1273fee2a2faSMark Johnston object = (vm_object_t)atomic_load_ptr(&m->object); 1274fee2a2faSMark Johnston if (__predict_false(object == NULL)) 1275fee2a2faSMark Johnston /* 1276fee2a2faSMark Johnston * The page has been removed from its object. 1277fee2a2faSMark Johnston */ 1278fee2a2faSMark Johnston continue; 1279fee2a2faSMark Johnston 1280f3f38e25SMark Johnston /* Deferred free of swap space. */ 1281f3f38e25SMark Johnston if ((m->a.flags & PGA_SWAP_FREE) != 0 && 1282f3f38e25SMark Johnston VM_OBJECT_TRYWLOCK(object)) { 1283f3f38e25SMark Johnston if (m->object == object) 1284f3f38e25SMark Johnston vm_pager_page_unswapped(m); 1285f3f38e25SMark Johnston VM_OBJECT_WUNLOCK(object); 1286f3f38e25SMark Johnston } 1287f3f38e25SMark Johnston 1288fee2a2faSMark Johnston /* 1289be37ee79SMark Johnston * Check to see "how much" the page has been used. 1290d7aeb429SAlan Cox * 1291d7aeb429SAlan Cox * Test PGA_REFERENCED after calling pmap_ts_referenced() so 1292d7aeb429SAlan Cox * that a reference from a concurrently destroyed mapping is 1293d7aeb429SAlan Cox * observed here and now. 1294d7aeb429SAlan Cox * 1295e8bcf696SMark Johnston * Perform an unsynchronized object ref count check. While 1296e8bcf696SMark Johnston * the page lock ensures that the page is not reallocated to 1297e8bcf696SMark Johnston * another object, in particular, one with unmanaged mappings 1298e8bcf696SMark Johnston * that cannot support pmap_ts_referenced(), two races are, 1299be37ee79SMark Johnston * nonetheless, possible: 1300be37ee79SMark Johnston * 1) The count was transitioning to zero, but we saw a non- 1301e8bcf696SMark Johnston * zero value. pmap_ts_referenced() will return zero 1302e8bcf696SMark Johnston * because the page is not mapped. 1303e8bcf696SMark Johnston * 2) The count was transitioning to one, but we saw zero. 1304e8bcf696SMark Johnston * This race delays the detection of a new reference. At 1305e8bcf696SMark Johnston * worst, we will deactivate and reactivate the page. 1306be37ee79SMark Johnston */ 1307f3f38e25SMark Johnston refs = object->ref_count != 0 ? pmap_ts_referenced(m) : 0; 1308be37ee79SMark Johnston 1309f3f38e25SMark Johnston old = vm_page_astate_load(m); 1310f3f38e25SMark Johnston do { 1311f3f38e25SMark Johnston /* 1312f3f38e25SMark Johnston * Check to see if the page has been removed from the 1313f3f38e25SMark Johnston * queue since the first such check. Leave it alone if 1314f3f38e25SMark Johnston * so, discarding any references collected by 1315f3f38e25SMark Johnston * pmap_ts_referenced(). 1316f3f38e25SMark Johnston */ 1317f3f38e25SMark Johnston if (__predict_false(_vm_page_queue(old) == PQ_NONE)) 1318f3f38e25SMark Johnston break; 1319a8081778SJeff Roberson 1320be37ee79SMark Johnston /* 1321be37ee79SMark Johnston * Advance or decay the act_count based on recent usage. 1322be37ee79SMark Johnston */ 1323f3f38e25SMark Johnston new = old; 1324f3f38e25SMark Johnston act_delta = refs; 1325f3f38e25SMark Johnston if ((old.flags & PGA_REFERENCED) != 0) { 1326f3f38e25SMark Johnston new.flags &= ~PGA_REFERENCED; 1327f3f38e25SMark Johnston act_delta++; 1328f3f38e25SMark Johnston } 1329be37ee79SMark Johnston if (act_delta != 0) { 1330f3f38e25SMark Johnston new.act_count += ACT_ADVANCE + act_delta; 1331f3f38e25SMark Johnston if (new.act_count > ACT_MAX) 1332f3f38e25SMark Johnston new.act_count = ACT_MAX; 1333f3f38e25SMark Johnston } else { 1334f3f38e25SMark Johnston new.act_count -= min(new.act_count, 1335f3f38e25SMark Johnston ACT_DECLINE); 1336f3f38e25SMark Johnston } 1337be37ee79SMark Johnston 1338f3f38e25SMark Johnston if (new.act_count > 0) { 1339be37ee79SMark Johnston /* 1340f3f38e25SMark Johnston * Adjust the activation count and keep the page 1341f3f38e25SMark Johnston * in the active queue. The count might be left 1342f3f38e25SMark Johnston * unchanged if it is saturated. The page may 1343f3f38e25SMark Johnston * have been moved to a different queue since we 1344f3f38e25SMark Johnston * started the scan, in which case we move it 1345f3f38e25SMark Johnston * back. 1346be37ee79SMark Johnston */ 1347f3f38e25SMark Johnston ps_delta = 0; 1348f3f38e25SMark Johnston if (old.queue != PQ_ACTIVE) { 1349f3f38e25SMark Johnston old.queue = PQ_ACTIVE; 1350f3f38e25SMark Johnston old.flags |= PGA_REQUEUE; 1351f3f38e25SMark Johnston } 13527cdeaf33SMark Johnston } else { 1353be37ee79SMark Johnston /* 1354f3f38e25SMark Johnston * When not short for inactive pages, let dirty 1355f3f38e25SMark Johnston * pages go through the inactive queue before 1356f3f38e25SMark Johnston * moving to the laundry queue. This gives them 1357f3f38e25SMark Johnston * some extra time to be reactivated, 1358f3f38e25SMark Johnston * potentially avoiding an expensive pageout. 1359f3f38e25SMark Johnston * However, during a page shortage, the inactive 1360f3f38e25SMark Johnston * queue is necessarily small, and so dirty 1361f3f38e25SMark Johnston * pages would only spend a trivial amount of 1362f3f38e25SMark Johnston * time in the inactive queue. Therefore, we 1363f3f38e25SMark Johnston * might as well place them directly in the 1364f3f38e25SMark Johnston * laundry queue to reduce queuing overhead. 1365f3f38e25SMark Johnston * 1366be37ee79SMark Johnston * Calling vm_page_test_dirty() here would 1367be37ee79SMark Johnston * require acquisition of the object's write 1368be37ee79SMark Johnston * lock. However, during a page shortage, 1369f3f38e25SMark Johnston * directing dirty pages into the laundry queue 1370f3f38e25SMark Johnston * is only an optimization and not a 1371be37ee79SMark Johnston * requirement. Therefore, we simply rely on 1372f3f38e25SMark Johnston * the opportunistic updates to the page's dirty 1373f3f38e25SMark Johnston * field by the pmap. 1374be37ee79SMark Johnston */ 1375f3f38e25SMark Johnston if (page_shortage <= 0) { 1376f3f38e25SMark Johnston nqueue = PQ_INACTIVE; 1377f3f38e25SMark Johnston ps_delta = 0; 1378f3f38e25SMark Johnston } else if (m->dirty == 0) { 1379f3f38e25SMark Johnston nqueue = PQ_INACTIVE; 1380f3f38e25SMark Johnston ps_delta = act_scan_laundry_weight; 1381be37ee79SMark Johnston } else { 1382f3f38e25SMark Johnston nqueue = PQ_LAUNDRY; 1383f3f38e25SMark Johnston ps_delta = 1; 1384be37ee79SMark Johnston } 1385f3f38e25SMark Johnston 1386f3f38e25SMark Johnston new.flags |= PGA_REQUEUE; 1387f3f38e25SMark Johnston new.queue = nqueue; 1388be37ee79SMark Johnston } 1389f3f38e25SMark Johnston } while (!vm_page_pqstate_commit(m, &old, new)); 1390f3f38e25SMark Johnston 1391f3f38e25SMark Johnston page_shortage -= ps_delta; 1392be37ee79SMark Johnston } 1393e8bcf696SMark Johnston if (mtx != NULL) { 1394e8bcf696SMark Johnston mtx_unlock(mtx); 1395e8bcf696SMark Johnston mtx = NULL; 1396e8bcf696SMark Johnston } 1397be37ee79SMark Johnston vm_pagequeue_lock(pq); 1398be37ee79SMark Johnston TAILQ_REMOVE(&pq->pq_pl, &vmd->vmd_clock[0], plinks.q); 1399be37ee79SMark Johnston TAILQ_INSERT_AFTER(&pq->pq_pl, marker, &vmd->vmd_clock[0], plinks.q); 1400be37ee79SMark Johnston vm_pageout_end_scan(&ss); 1401be37ee79SMark Johnston vm_pagequeue_unlock(pq); 1402be37ee79SMark Johnston } 1403be37ee79SMark Johnston 14045cd29d0fSMark Johnston static int 1405f3f38e25SMark Johnston vm_pageout_reinsert_inactive_page(struct vm_pagequeue *pq, vm_page_t marker, 1406f3f38e25SMark Johnston vm_page_t m) 14075cd29d0fSMark Johnston { 1408f3f38e25SMark Johnston vm_page_astate_t as; 14095cd29d0fSMark Johnston 1410f3f38e25SMark Johnston vm_pagequeue_assert_locked(pq); 1411f3f38e25SMark Johnston 1412f3f38e25SMark Johnston as = vm_page_astate_load(m); 1413f3f38e25SMark Johnston if (as.queue != PQ_INACTIVE || (as.flags & PGA_ENQUEUED) != 0) 1414e8bcf696SMark Johnston return (0); 1415e8bcf696SMark Johnston vm_page_aflag_set(m, PGA_ENQUEUED); 1416f3f38e25SMark Johnston TAILQ_INSERT_BEFORE(marker, m, plinks.q); 14175cd29d0fSMark Johnston return (1); 14185cd29d0fSMark Johnston } 14195cd29d0fSMark Johnston 14205cd29d0fSMark Johnston /* 14215cd29d0fSMark Johnston * Re-add stuck pages to the inactive queue. We will examine them again 14225cd29d0fSMark Johnston * during the next scan. If the queue state of a page has changed since 14235cd29d0fSMark Johnston * it was physically removed from the page queue in 14245cd29d0fSMark Johnston * vm_pageout_collect_batch(), don't do anything with that page. 14255cd29d0fSMark Johnston */ 14265cd29d0fSMark Johnston static void 14275cd29d0fSMark Johnston vm_pageout_reinsert_inactive(struct scan_state *ss, struct vm_batchqueue *bq, 14285cd29d0fSMark Johnston vm_page_t m) 14295cd29d0fSMark Johnston { 14305cd29d0fSMark Johnston struct vm_pagequeue *pq; 1431f3f38e25SMark Johnston vm_page_t marker; 14325cd29d0fSMark Johnston int delta; 14335cd29d0fSMark Johnston 14345cd29d0fSMark Johnston delta = 0; 1435f3f38e25SMark Johnston marker = ss->marker; 14365cd29d0fSMark Johnston pq = ss->pq; 14375cd29d0fSMark Johnston 14385cd29d0fSMark Johnston if (m != NULL) { 14395cd29d0fSMark Johnston if (vm_batchqueue_insert(bq, m)) 14405cd29d0fSMark Johnston return; 14415cd29d0fSMark Johnston vm_pagequeue_lock(pq); 1442f3f38e25SMark Johnston delta += vm_pageout_reinsert_inactive_page(pq, marker, m); 14435cd29d0fSMark Johnston } else 14445cd29d0fSMark Johnston vm_pagequeue_lock(pq); 14455cd29d0fSMark Johnston while ((m = vm_batchqueue_pop(bq)) != NULL) 1446f3f38e25SMark Johnston delta += vm_pageout_reinsert_inactive_page(pq, marker, m); 14475cd29d0fSMark Johnston vm_pagequeue_cnt_add(pq, delta); 14485cd29d0fSMark Johnston vm_pagequeue_unlock(pq); 14495cd29d0fSMark Johnston vm_batchqueue_init(bq); 14505cd29d0fSMark Johnston } 14515cd29d0fSMark Johnston 1452ebcddc72SAlan Cox /* 145327e29d10SMark Johnston * Attempt to reclaim the requested number of pages from the inactive queue. 145427e29d10SMark Johnston * Returns true if the shortage was addressed. 1455df8bae1dSRodney W. Grimes */ 1456be37ee79SMark Johnston static int 145749a3710cSMark Johnston vm_pageout_scan_inactive(struct vm_domain *vmd, int shortage, 1458be37ee79SMark Johnston int *addl_shortage) 1459df8bae1dSRodney W. Grimes { 14605cd29d0fSMark Johnston struct scan_state ss; 14615cd29d0fSMark Johnston struct vm_batchqueue rq; 1462e8bcf696SMark Johnston struct mtx *mtx; 14635cd29d0fSMark Johnston vm_page_t m, marker; 14648d220203SAlan Cox struct vm_pagequeue *pq; 1465df8bae1dSRodney W. Grimes vm_object_t object; 1466f3f38e25SMark Johnston vm_page_astate_t old, new; 1467f3f38e25SMark Johnston int act_delta, addl_page_shortage, deficit, page_shortage, refs; 1468be37ee79SMark Johnston int starting_page_shortage; 14690d94caffSDavid Greenman 1470df8bae1dSRodney W. Grimes /* 147101f04471SMark Johnston * The addl_page_shortage is an estimate of the number of temporarily 1472311e34e2SKonstantin Belousov * stuck pages in the inactive queue. In other words, the 1473449c2e92SKonstantin Belousov * number of pages from the inactive count that should be 1474311e34e2SKonstantin Belousov * discounted in setting the target for the active queue scan. 1475311e34e2SKonstantin Belousov */ 14769099545aSAlan Cox addl_page_shortage = 0; 14779099545aSAlan Cox 14781c7c3c6aSMatthew Dillon /* 147949a3710cSMark Johnston * vmd_pageout_deficit counts the number of pages requested in 148049a3710cSMark Johnston * allocations that failed because of a free page shortage. We assume 148149a3710cSMark Johnston * that the allocations will be reattempted and thus include the deficit 148249a3710cSMark Johnston * in our scan target. 14831c7c3c6aSMatthew Dillon */ 1484e2068d0bSJeff Roberson deficit = atomic_readandclear_int(&vmd->vmd_pageout_deficit); 148549a3710cSMark Johnston starting_page_shortage = page_shortage = shortage + deficit; 14861c7c3c6aSMatthew Dillon 1487e8bcf696SMark Johnston mtx = NULL; 14885cd29d0fSMark Johnston object = NULL; 14895cd29d0fSMark Johnston vm_batchqueue_init(&rq); 14905cd29d0fSMark Johnston 1491936524aaSMatthew Dillon /* 1492f095d1bbSAlan Cox * Start scanning the inactive queue for pages that we can free. The 1493f095d1bbSAlan Cox * scan will stop when we reach the target or we have scanned the 14945cff1f4dSMark Johnston * entire queue. (Note that m->a.act_count is not used to make 1495f095d1bbSAlan Cox * decisions for the inactive queue, only for the active queue.) 14968d220203SAlan Cox */ 149764b38930SMark Johnston marker = &vmd->vmd_markers[PQ_INACTIVE]; 14985cd29d0fSMark Johnston pq = &vmd->vmd_pagequeues[PQ_INACTIVE]; 14998d220203SAlan Cox vm_pagequeue_lock(pq); 15005cd29d0fSMark Johnston vm_pageout_init_scan(&ss, pq, marker, NULL, pq->pq_cnt); 15015cd29d0fSMark Johnston while (page_shortage > 0 && (m = vm_pageout_next(&ss, true)) != NULL) { 15025cd29d0fSMark Johnston KASSERT((m->flags & PG_MARKER) == 0, 15035cd29d0fSMark Johnston ("marker page %p was dequeued", m)); 1504df8bae1dSRodney W. Grimes 1505e8bcf696SMark Johnston vm_page_change_lock(m, &mtx); 1506e8bcf696SMark Johnston 1507e8bcf696SMark Johnston recheck: 1508936524aaSMatthew Dillon /* 1509*b7f30bffSMark Johnston * Don't touch a page that was removed from the queue after the 1510*b7f30bffSMark Johnston * page queue lock was released. Otherwise, ensure that any 1511*b7f30bffSMark Johnston * pending queue operations, such as dequeues for wired pages, 1512*b7f30bffSMark Johnston * are handled. 1513936524aaSMatthew Dillon */ 1514*b7f30bffSMark Johnston if (vm_pageout_defer(m, PQ_INACTIVE, false)) 1515936524aaSMatthew Dillon continue; 1516e8bcf696SMark Johnston 15175cd29d0fSMark Johnston if (object != m->object) { 151860256604SMark Johnston if (object != NULL) 15195cd29d0fSMark Johnston VM_OBJECT_WUNLOCK(object); 1520e8bcf696SMark Johnston 1521e8bcf696SMark Johnston /* 1522e8bcf696SMark Johnston * A page's object pointer may be set to NULL before 1523e8bcf696SMark Johnston * the object lock is acquired. 1524e8bcf696SMark Johnston */ 1525fee2a2faSMark Johnston object = (vm_object_t)atomic_load_ptr(&m->object); 1526e8bcf696SMark Johnston if (object != NULL && !VM_OBJECT_TRYWLOCK(object)) { 1527e8bcf696SMark Johnston mtx_unlock(mtx); 1528e8bcf696SMark Johnston /* Depends on type-stability. */ 152941fd4b94SMark Johnston VM_OBJECT_WLOCK(object); 1530e8bcf696SMark Johnston mtx_lock(mtx); 1531e8bcf696SMark Johnston goto recheck; 153241fd4b94SMark Johnston } 153341fd4b94SMark Johnston } 1534e8bcf696SMark Johnston if (__predict_false(m->object == NULL)) 1535e8bcf696SMark Johnston /* 1536e8bcf696SMark Johnston * The page has been removed from its object. 1537e8bcf696SMark Johnston */ 1538e8bcf696SMark Johnston continue; 1539e8bcf696SMark Johnston KASSERT(m->object == object, ("page %p does not belong to %p", 1540e8bcf696SMark Johnston m, object)); 15415cd29d0fSMark Johnston 154263e97555SJeff Roberson if (vm_page_tryxbusy(m) == 0) { 1543a3aeedabSAlan Cox /* 1544a3aeedabSAlan Cox * Don't mess with busy pages. Leave them at 1545a3aeedabSAlan Cox * the front of the queue. Most likely, they 1546a3aeedabSAlan Cox * are being paged out and will leave the 1547a3aeedabSAlan Cox * queue shortly after the scan finishes. So, 1548a3aeedabSAlan Cox * they ought to be discounted from the 1549a3aeedabSAlan Cox * inactive count. 1550a3aeedabSAlan Cox */ 1551a3aeedabSAlan Cox addl_page_shortage++; 15525cd29d0fSMark Johnston goto reinsert; 155326f9a767SRodney W. Grimes } 155448cc2fc7SKonstantin Belousov 1555a8081778SJeff Roberson /* Deferred free of swap space. */ 1556a8081778SJeff Roberson if ((m->a.flags & PGA_SWAP_FREE) != 0) 1557a8081778SJeff Roberson vm_pager_page_unswapped(m); 1558a8081778SJeff Roberson 155948cc2fc7SKonstantin Belousov /* 1560e8bcf696SMark Johnston * Re-check for wirings now that we hold the object lock and 1561e8bcf696SMark Johnston * have verified that the page is unbusied. If the page is 1562e8bcf696SMark Johnston * mapped, it may still be wired by pmap lookups. The call to 1563fee2a2faSMark Johnston * vm_page_try_remove_all() below atomically checks for such 1564fee2a2faSMark Johnston * wirings and removes mappings. If the page is unmapped, the 1565fee2a2faSMark Johnston * wire count is guaranteed not to increase. 1566fee2a2faSMark Johnston */ 1567fee2a2faSMark Johnston if (__predict_false(vm_page_wired(m))) { 1568e8bcf696SMark Johnston vm_page_dequeue_deferred(m); 1569f3f38e25SMark Johnston goto skip_page; 1570fee2a2faSMark Johnston } 1571fee2a2faSMark Johnston 1572fee2a2faSMark Johnston /* 15738748f58cSKonstantin Belousov * Invalid pages can be easily freed. They cannot be 15748748f58cSKonstantin Belousov * mapped, vm_page_free() asserts this. 1575776f729cSKonstantin Belousov */ 15760012f373SJeff Roberson if (vm_page_none_valid(m)) 15778748f58cSKonstantin Belousov goto free_page; 1578776f729cSKonstantin Belousov 1579f3f38e25SMark Johnston refs = object->ref_count != 0 ? pmap_ts_referenced(m) : 0; 1580f3f38e25SMark Johnston 1581f3f38e25SMark Johnston for (old = vm_page_astate_load(m);;) { 1582776f729cSKonstantin Belousov /* 1583f3f38e25SMark Johnston * Check to see if the page has been removed from the 1584f3f38e25SMark Johnston * queue since the first such check. Leave it alone if 1585f3f38e25SMark Johnston * so, discarding any references collected by 1586f3f38e25SMark Johnston * pmap_ts_referenced(). 15877e006499SJohn Dyson */ 1588f3f38e25SMark Johnston if (__predict_false(_vm_page_queue(old) == PQ_NONE)) 1589f3f38e25SMark Johnston goto skip_page; 1590f3f38e25SMark Johnston 1591f3f38e25SMark Johnston new = old; 1592f3f38e25SMark Johnston act_delta = refs; 1593f3f38e25SMark Johnston if ((old.flags & PGA_REFERENCED) != 0) { 1594f3f38e25SMark Johnston new.flags &= ~PGA_REFERENCED; 1595d7aeb429SAlan Cox act_delta++; 15962fe6e4d7SDavid Greenman } 1597f3f38e25SMark Johnston if (act_delta == 0) { 1598f3f38e25SMark Johnston ; 1599f3f38e25SMark Johnston } else if (object->ref_count != 0) { 1600e8bcf696SMark Johnston /* 1601f3f38e25SMark Johnston * Increase the activation count if the 1602f3f38e25SMark Johnston * page was referenced while in the 1603f3f38e25SMark Johnston * inactive queue. This makes it less 1604f3f38e25SMark Johnston * likely that the page will be returned 1605f3f38e25SMark Johnston * prematurely to the inactive queue. 1606e8bcf696SMark Johnston */ 1607f3f38e25SMark Johnston new.act_count += ACT_ADVANCE + 1608f3f38e25SMark Johnston act_delta; 1609f3f38e25SMark Johnston if (new.act_count > ACT_MAX) 1610f3f38e25SMark Johnston new.act_count = ACT_MAX; 1611f3f38e25SMark Johnston 1612f3f38e25SMark Johnston new.flags |= PGA_REQUEUE; 1613f3f38e25SMark Johnston new.queue = PQ_ACTIVE; 1614f3f38e25SMark Johnston if (!vm_page_pqstate_commit(m, &old, new)) 1615e8bcf696SMark Johnston continue; 1616f3f38e25SMark Johnston 1617f3f38e25SMark Johnston VM_CNT_INC(v_reactivated); 1618f3f38e25SMark Johnston goto skip_page; 1619ebcddc72SAlan Cox } else if ((object->flags & OBJ_DEAD) == 0) { 1620f3f38e25SMark Johnston new.queue = PQ_INACTIVE; 1621f3f38e25SMark Johnston new.flags |= PGA_REQUEUE; 1622f3f38e25SMark Johnston if (!vm_page_pqstate_commit(m, &old, new)) 1623f3f38e25SMark Johnston continue; 1624f3f38e25SMark Johnston goto skip_page; 1625ebcddc72SAlan Cox } 1626f3f38e25SMark Johnston break; 1627960810ccSAlan Cox } 162867bf6868SJohn Dyson 16297e006499SJohn Dyson /* 16309fc4739dSAlan Cox * If the page appears to be clean at the machine-independent 16319fc4739dSAlan Cox * layer, then remove all of its mappings from the pmap in 1632a766ffd0SAlan Cox * anticipation of freeing it. If, however, any of the page's 1633a766ffd0SAlan Cox * mappings allow write access, then the page may still be 1634a766ffd0SAlan Cox * modified until the last of those mappings are removed. 16357e006499SJohn Dyson */ 1636aa044135SAlan Cox if (object->ref_count != 0) { 16379fc4739dSAlan Cox vm_page_test_dirty(m); 1638fee2a2faSMark Johnston if (m->dirty == 0 && !vm_page_try_remove_all(m)) { 1639e8bcf696SMark Johnston vm_page_dequeue_deferred(m); 1640f3f38e25SMark Johnston goto skip_page; 1641fee2a2faSMark Johnston } 1642aa044135SAlan Cox } 1643dcbcd518SBruce Evans 16446989c456SAlan Cox /* 1645ebcddc72SAlan Cox * Clean pages can be freed, but dirty pages must be sent back 1646ebcddc72SAlan Cox * to the laundry, unless they belong to a dead object. 1647ebcddc72SAlan Cox * Requeueing dirty pages from dead objects is pointless, as 1648ebcddc72SAlan Cox * they are being paged out and freed by the thread that 1649ebcddc72SAlan Cox * destroyed the object. 16506989c456SAlan Cox */ 1651ebcddc72SAlan Cox if (m->dirty == 0) { 16528748f58cSKonstantin Belousov free_page: 16535cd29d0fSMark Johnston /* 16545cd29d0fSMark Johnston * Because we dequeued the page and have already 16555cd29d0fSMark Johnston * checked for concurrent dequeue and enqueue 16565cd29d0fSMark Johnston * requests, we can safely disassociate the page 16575cd29d0fSMark Johnston * from the inactive queue. 16585cd29d0fSMark Johnston */ 16595cff1f4dSMark Johnston KASSERT((m->a.flags & PGA_QUEUE_STATE_MASK) == 0, 1660e8bcf696SMark Johnston ("page %p has queue state", m)); 16615cff1f4dSMark Johnston m->a.queue = PQ_NONE; 166278afdce6SAlan Cox vm_page_free(m); 16635cd29d0fSMark Johnston page_shortage--; 166463e97555SJeff Roberson continue; 166563e97555SJeff Roberson } 166663e97555SJeff Roberson if ((object->flags & OBJ_DEAD) == 0) 1667ebcddc72SAlan Cox vm_page_launder(m); 1668f3f38e25SMark Johnston skip_page: 1669f3f38e25SMark Johnston vm_page_xunbusy(m); 16705cd29d0fSMark Johnston continue; 16715cd29d0fSMark Johnston reinsert: 16725cd29d0fSMark Johnston vm_pageout_reinsert_inactive(&ss, &rq, m); 16735cd29d0fSMark Johnston } 1674e8bcf696SMark Johnston if (mtx != NULL) 1675e8bcf696SMark Johnston mtx_unlock(mtx); 167660256604SMark Johnston if (object != NULL) 167789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 16785cd29d0fSMark Johnston vm_pageout_reinsert_inactive(&ss, &rq, NULL); 16795cd29d0fSMark Johnston vm_pageout_reinsert_inactive(&ss, &ss.bq, NULL); 16808d220203SAlan Cox vm_pagequeue_lock(pq); 16815cd29d0fSMark Johnston vm_pageout_end_scan(&ss); 16828d220203SAlan Cox vm_pagequeue_unlock(pq); 168326f9a767SRodney W. Grimes 16845cd29d0fSMark Johnston VM_CNT_ADD(v_dfree, starting_page_shortage - page_shortage); 16855cd29d0fSMark Johnston 1686ebcddc72SAlan Cox /* 1687ebcddc72SAlan Cox * Wake up the laundry thread so that it can perform any needed 1688ebcddc72SAlan Cox * laundering. If we didn't meet our target, we're in shortfall and 1689b1fd102eSMark Johnston * need to launder more aggressively. If PQ_LAUNDRY is empty and no 1690b1fd102eSMark Johnston * swap devices are configured, the laundry thread has no work to do, so 1691b1fd102eSMark Johnston * don't bother waking it up. 1692cb35676eSMark Johnston * 1693cb35676eSMark Johnston * The laundry thread uses the number of inactive queue scans elapsed 1694cb35676eSMark Johnston * since the last laundering to determine whether to launder again, so 1695cb35676eSMark Johnston * keep count. 1696ebcddc72SAlan Cox */ 1697cb35676eSMark Johnston if (starting_page_shortage > 0) { 1698e2068d0bSJeff Roberson pq = &vmd->vmd_pagequeues[PQ_LAUNDRY]; 1699ebcddc72SAlan Cox vm_pagequeue_lock(pq); 1700e2068d0bSJeff Roberson if (vmd->vmd_laundry_request == VM_LAUNDRY_IDLE && 1701cb35676eSMark Johnston (pq->pq_cnt > 0 || atomic_load_acq_int(&swapdev_enabled))) { 1702ebcddc72SAlan Cox if (page_shortage > 0) { 1703e2068d0bSJeff Roberson vmd->vmd_laundry_request = VM_LAUNDRY_SHORTFALL; 170483c9dea1SGleb Smirnoff VM_CNT_INC(v_pdshortfalls); 1705e2068d0bSJeff Roberson } else if (vmd->vmd_laundry_request != 1706e2068d0bSJeff Roberson VM_LAUNDRY_SHORTFALL) 1707e2068d0bSJeff Roberson vmd->vmd_laundry_request = 1708e2068d0bSJeff Roberson VM_LAUNDRY_BACKGROUND; 1709e2068d0bSJeff Roberson wakeup(&vmd->vmd_laundry_request); 1710b1fd102eSMark Johnston } 171160684862SMark Johnston vmd->vmd_clean_pages_freed += 171260684862SMark Johnston starting_page_shortage - page_shortage; 1713ebcddc72SAlan Cox vm_pagequeue_unlock(pq); 1714ebcddc72SAlan Cox } 1715ebcddc72SAlan Cox 17169452b5edSAlan Cox /* 1717f095d1bbSAlan Cox * Wakeup the swapout daemon if we didn't free the targeted number of 1718f095d1bbSAlan Cox * pages. 17199452b5edSAlan Cox */ 1720ac04195bSKonstantin Belousov if (page_shortage > 0) 1721ac04195bSKonstantin Belousov vm_swapout_run(); 17229452b5edSAlan Cox 17239452b5edSAlan Cox /* 172476386c7eSKonstantin Belousov * If the inactive queue scan fails repeatedly to meet its 172576386c7eSKonstantin Belousov * target, kill the largest process. 172676386c7eSKonstantin Belousov */ 172776386c7eSKonstantin Belousov vm_pageout_mightbe_oom(vmd, page_shortage, starting_page_shortage); 172876386c7eSKonstantin Belousov 172976386c7eSKonstantin Belousov /* 1730be37ee79SMark Johnston * Reclaim pages by swapping out idle processes, if configured to do so. 17311c7c3c6aSMatthew Dillon */ 1732ac04195bSKonstantin Belousov vm_swapout_run_idle(); 1733be37ee79SMark Johnston 1734be37ee79SMark Johnston /* 1735be37ee79SMark Johnston * See the description of addl_page_shortage above. 1736be37ee79SMark Johnston */ 1737be37ee79SMark Johnston *addl_shortage = addl_page_shortage + deficit; 1738be37ee79SMark Johnston 1739e57dd910SAlan Cox return (page_shortage <= 0); 17402025d69bSKonstantin Belousov } 17412025d69bSKonstantin Belousov 1742449c2e92SKonstantin Belousov static int vm_pageout_oom_vote; 1743449c2e92SKonstantin Belousov 1744449c2e92SKonstantin Belousov /* 1745449c2e92SKonstantin Belousov * The pagedaemon threads randlomly select one to perform the 1746449c2e92SKonstantin Belousov * OOM. Trying to kill processes before all pagedaemons 1747449c2e92SKonstantin Belousov * failed to reach free target is premature. 1748449c2e92SKonstantin Belousov */ 1749449c2e92SKonstantin Belousov static void 175076386c7eSKonstantin Belousov vm_pageout_mightbe_oom(struct vm_domain *vmd, int page_shortage, 175176386c7eSKonstantin Belousov int starting_page_shortage) 1752449c2e92SKonstantin Belousov { 1753449c2e92SKonstantin Belousov int old_vote; 1754449c2e92SKonstantin Belousov 175576386c7eSKonstantin Belousov if (starting_page_shortage <= 0 || starting_page_shortage != 175676386c7eSKonstantin Belousov page_shortage) 175776386c7eSKonstantin Belousov vmd->vmd_oom_seq = 0; 175876386c7eSKonstantin Belousov else 175976386c7eSKonstantin Belousov vmd->vmd_oom_seq++; 176076386c7eSKonstantin Belousov if (vmd->vmd_oom_seq < vm_pageout_oom_seq) { 1761449c2e92SKonstantin Belousov if (vmd->vmd_oom) { 1762449c2e92SKonstantin Belousov vmd->vmd_oom = FALSE; 1763449c2e92SKonstantin Belousov atomic_subtract_int(&vm_pageout_oom_vote, 1); 1764449c2e92SKonstantin Belousov } 1765449c2e92SKonstantin Belousov return; 1766449c2e92SKonstantin Belousov } 1767449c2e92SKonstantin Belousov 176876386c7eSKonstantin Belousov /* 176976386c7eSKonstantin Belousov * Do not follow the call sequence until OOM condition is 177076386c7eSKonstantin Belousov * cleared. 177176386c7eSKonstantin Belousov */ 177276386c7eSKonstantin Belousov vmd->vmd_oom_seq = 0; 177376386c7eSKonstantin Belousov 1774449c2e92SKonstantin Belousov if (vmd->vmd_oom) 1775449c2e92SKonstantin Belousov return; 1776449c2e92SKonstantin Belousov 1777449c2e92SKonstantin Belousov vmd->vmd_oom = TRUE; 1778449c2e92SKonstantin Belousov old_vote = atomic_fetchadd_int(&vm_pageout_oom_vote, 1); 1779449c2e92SKonstantin Belousov if (old_vote != vm_ndomains - 1) 1780449c2e92SKonstantin Belousov return; 1781449c2e92SKonstantin Belousov 1782449c2e92SKonstantin Belousov /* 1783449c2e92SKonstantin Belousov * The current pagedaemon thread is the last in the quorum to 1784449c2e92SKonstantin Belousov * start OOM. Initiate the selection and signaling of the 1785449c2e92SKonstantin Belousov * victim. 1786449c2e92SKonstantin Belousov */ 1787449c2e92SKonstantin Belousov vm_pageout_oom(VM_OOM_MEM); 1788449c2e92SKonstantin Belousov 1789449c2e92SKonstantin Belousov /* 1790449c2e92SKonstantin Belousov * After one round of OOM terror, recall our vote. On the 1791449c2e92SKonstantin Belousov * next pass, current pagedaemon would vote again if the low 1792449c2e92SKonstantin Belousov * memory condition is still there, due to vmd_oom being 1793449c2e92SKonstantin Belousov * false. 1794449c2e92SKonstantin Belousov */ 1795449c2e92SKonstantin Belousov vmd->vmd_oom = FALSE; 1796449c2e92SKonstantin Belousov atomic_subtract_int(&vm_pageout_oom_vote, 1); 1797449c2e92SKonstantin Belousov } 17982025d69bSKonstantin Belousov 17993949873fSKonstantin Belousov /* 18003949873fSKonstantin Belousov * The OOM killer is the page daemon's action of last resort when 18013949873fSKonstantin Belousov * memory allocation requests have been stalled for a prolonged period 18023949873fSKonstantin Belousov * of time because it cannot reclaim memory. This function computes 18033949873fSKonstantin Belousov * the approximate number of physical pages that could be reclaimed if 18043949873fSKonstantin Belousov * the specified address space is destroyed. 18053949873fSKonstantin Belousov * 18063949873fSKonstantin Belousov * Private, anonymous memory owned by the address space is the 18073949873fSKonstantin Belousov * principal resource that we expect to recover after an OOM kill. 18083949873fSKonstantin Belousov * Since the physical pages mapped by the address space's COW entries 18093949873fSKonstantin Belousov * are typically shared pages, they are unlikely to be released and so 18103949873fSKonstantin Belousov * they are not counted. 18113949873fSKonstantin Belousov * 18123949873fSKonstantin Belousov * To get to the point where the page daemon runs the OOM killer, its 18133949873fSKonstantin Belousov * efforts to write-back vnode-backed pages may have stalled. This 18143949873fSKonstantin Belousov * could be caused by a memory allocation deadlock in the write path 18153949873fSKonstantin Belousov * that might be resolved by an OOM kill. Therefore, physical pages 18163949873fSKonstantin Belousov * belonging to vnode-backed objects are counted, because they might 18173949873fSKonstantin Belousov * be freed without being written out first if the address space holds 18183949873fSKonstantin Belousov * the last reference to an unlinked vnode. 18193949873fSKonstantin Belousov * 18203949873fSKonstantin Belousov * Similarly, physical pages belonging to OBJT_PHYS objects are 18213949873fSKonstantin Belousov * counted because the address space might hold the last reference to 18223949873fSKonstantin Belousov * the object. 18233949873fSKonstantin Belousov */ 18243949873fSKonstantin Belousov static long 18253949873fSKonstantin Belousov vm_pageout_oom_pagecount(struct vmspace *vmspace) 18263949873fSKonstantin Belousov { 18273949873fSKonstantin Belousov vm_map_t map; 18283949873fSKonstantin Belousov vm_map_entry_t entry; 18293949873fSKonstantin Belousov vm_object_t obj; 18303949873fSKonstantin Belousov long res; 18313949873fSKonstantin Belousov 18323949873fSKonstantin Belousov map = &vmspace->vm_map; 18333949873fSKonstantin Belousov KASSERT(!map->system_map, ("system map")); 18343949873fSKonstantin Belousov sx_assert(&map->lock, SA_LOCKED); 18353949873fSKonstantin Belousov res = 0; 18362288078cSDoug Moore VM_MAP_ENTRY_FOREACH(entry, map) { 18373949873fSKonstantin Belousov if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0) 18383949873fSKonstantin Belousov continue; 18393949873fSKonstantin Belousov obj = entry->object.vm_object; 18403949873fSKonstantin Belousov if (obj == NULL) 18413949873fSKonstantin Belousov continue; 18423949873fSKonstantin Belousov if ((entry->eflags & MAP_ENTRY_NEEDS_COPY) != 0 && 18433949873fSKonstantin Belousov obj->ref_count != 1) 18443949873fSKonstantin Belousov continue; 18453949873fSKonstantin Belousov switch (obj->type) { 18463949873fSKonstantin Belousov case OBJT_DEFAULT: 18473949873fSKonstantin Belousov case OBJT_SWAP: 18483949873fSKonstantin Belousov case OBJT_PHYS: 18493949873fSKonstantin Belousov case OBJT_VNODE: 18503949873fSKonstantin Belousov res += obj->resident_page_count; 18513949873fSKonstantin Belousov break; 18523949873fSKonstantin Belousov } 18533949873fSKonstantin Belousov } 18543949873fSKonstantin Belousov return (res); 18553949873fSKonstantin Belousov } 18563949873fSKonstantin Belousov 1857245139c6SKonstantin Belousov static int vm_oom_ratelim_last; 1858245139c6SKonstantin Belousov static int vm_oom_pf_secs = 10; 1859245139c6SKonstantin Belousov SYSCTL_INT(_vm, OID_AUTO, oom_pf_secs, CTLFLAG_RWTUN, &vm_oom_pf_secs, 0, 1860245139c6SKonstantin Belousov ""); 1861245139c6SKonstantin Belousov static struct mtx vm_oom_ratelim_mtx; 1862245139c6SKonstantin Belousov 18632025d69bSKonstantin Belousov void 18642025d69bSKonstantin Belousov vm_pageout_oom(int shortage) 18652025d69bSKonstantin Belousov { 18662025d69bSKonstantin Belousov struct proc *p, *bigproc; 18672025d69bSKonstantin Belousov vm_offset_t size, bigsize; 18682025d69bSKonstantin Belousov struct thread *td; 18696bed074cSKonstantin Belousov struct vmspace *vm; 1870245139c6SKonstantin Belousov int now; 18713e78e983SAlan Cox bool breakout; 18722025d69bSKonstantin Belousov 18732025d69bSKonstantin Belousov /* 1874245139c6SKonstantin Belousov * For OOM requests originating from vm_fault(), there is a high 1875245139c6SKonstantin Belousov * chance that a single large process faults simultaneously in 1876245139c6SKonstantin Belousov * several threads. Also, on an active system running many 1877245139c6SKonstantin Belousov * processes of middle-size, like buildworld, all of them 1878245139c6SKonstantin Belousov * could fault almost simultaneously as well. 1879245139c6SKonstantin Belousov * 1880245139c6SKonstantin Belousov * To avoid killing too many processes, rate-limit OOMs 1881245139c6SKonstantin Belousov * initiated by vm_fault() time-outs on the waits for free 1882245139c6SKonstantin Belousov * pages. 1883245139c6SKonstantin Belousov */ 1884245139c6SKonstantin Belousov mtx_lock(&vm_oom_ratelim_mtx); 1885245139c6SKonstantin Belousov now = ticks; 1886245139c6SKonstantin Belousov if (shortage == VM_OOM_MEM_PF && 1887245139c6SKonstantin Belousov (u_int)(now - vm_oom_ratelim_last) < hz * vm_oom_pf_secs) { 1888245139c6SKonstantin Belousov mtx_unlock(&vm_oom_ratelim_mtx); 1889245139c6SKonstantin Belousov return; 1890245139c6SKonstantin Belousov } 1891245139c6SKonstantin Belousov vm_oom_ratelim_last = now; 1892245139c6SKonstantin Belousov mtx_unlock(&vm_oom_ratelim_mtx); 1893245139c6SKonstantin Belousov 1894245139c6SKonstantin Belousov /* 18951c58e4e5SJohn Baldwin * We keep the process bigproc locked once we find it to keep anyone 18961c58e4e5SJohn Baldwin * from messing with it; however, there is a possibility of 189728323addSBryan Drewery * deadlock if process B is bigproc and one of its child processes 18981c58e4e5SJohn Baldwin * attempts to propagate a signal to B while we are waiting for A's 18991c58e4e5SJohn Baldwin * lock while walking this list. To avoid this, we don't block on 19001c58e4e5SJohn Baldwin * the process lock but just skip a process if it is already locked. 19015663e6deSDavid Greenman */ 19025663e6deSDavid Greenman bigproc = NULL; 19035663e6deSDavid Greenman bigsize = 0; 19041005a129SJohn Baldwin sx_slock(&allproc_lock); 1905e602ba25SJulian Elischer FOREACH_PROC_IN_SYSTEM(p) { 190671943c3dSKonstantin Belousov PROC_LOCK(p); 190771943c3dSKonstantin Belousov 19081c58e4e5SJohn Baldwin /* 19093f1c4c4fSKonstantin Belousov * If this is a system, protected or killed process, skip it. 19105663e6deSDavid Greenman */ 191171943c3dSKonstantin Belousov if (p->p_state != PRS_NORMAL || (p->p_flag & (P_INEXEC | 191271943c3dSKonstantin Belousov P_PROTECTED | P_SYSTEM | P_WEXIT)) != 0 || 191371943c3dSKonstantin Belousov p->p_pid == 1 || P_KILLED(p) || 191471943c3dSKonstantin Belousov (p->p_pid < 48 && swap_pager_avail != 0)) { 19158606d880SJohn Baldwin PROC_UNLOCK(p); 19165663e6deSDavid Greenman continue; 19175663e6deSDavid Greenman } 19185663e6deSDavid Greenman /* 1919dcbcd518SBruce Evans * If the process is in a non-running type state, 1920e602ba25SJulian Elischer * don't touch it. Check all the threads individually. 19215663e6deSDavid Greenman */ 19223e78e983SAlan Cox breakout = false; 1923e602ba25SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 1924982d11f8SJeff Roberson thread_lock(td); 192571fad9fdSJulian Elischer if (!TD_ON_RUNQ(td) && 192671fad9fdSJulian Elischer !TD_IS_RUNNING(td) && 1927f497cda2SEdward Tomasz Napierala !TD_IS_SLEEPING(td) && 1928b98acc0aSKonstantin Belousov !TD_IS_SUSPENDED(td) && 1929b98acc0aSKonstantin Belousov !TD_IS_SWAPPED(td)) { 1930982d11f8SJeff Roberson thread_unlock(td); 19313e78e983SAlan Cox breakout = true; 1932e602ba25SJulian Elischer break; 1933e602ba25SJulian Elischer } 1934982d11f8SJeff Roberson thread_unlock(td); 1935e602ba25SJulian Elischer } 1936e602ba25SJulian Elischer if (breakout) { 19371c58e4e5SJohn Baldwin PROC_UNLOCK(p); 19385663e6deSDavid Greenman continue; 19395663e6deSDavid Greenman } 19405663e6deSDavid Greenman /* 19415663e6deSDavid Greenman * get the process size 19425663e6deSDavid Greenman */ 19436bed074cSKonstantin Belousov vm = vmspace_acquire_ref(p); 19446bed074cSKonstantin Belousov if (vm == NULL) { 19456bed074cSKonstantin Belousov PROC_UNLOCK(p); 19466bed074cSKonstantin Belousov continue; 19476bed074cSKonstantin Belousov } 194895e2409aSKonstantin Belousov _PHOLD_LITE(p); 194972d97679SDavid Schultz PROC_UNLOCK(p); 195095e2409aSKonstantin Belousov sx_sunlock(&allproc_lock); 195195e2409aSKonstantin Belousov if (!vm_map_trylock_read(&vm->vm_map)) { 195271943c3dSKonstantin Belousov vmspace_free(vm); 195395e2409aSKonstantin Belousov sx_slock(&allproc_lock); 195495e2409aSKonstantin Belousov PRELE(p); 195572d97679SDavid Schultz continue; 195672d97679SDavid Schultz } 19577981aa24SKonstantin Belousov size = vmspace_swap_count(vm); 1958245139c6SKonstantin Belousov if (shortage == VM_OOM_MEM || shortage == VM_OOM_MEM_PF) 19593949873fSKonstantin Belousov size += vm_pageout_oom_pagecount(vm); 19603949873fSKonstantin Belousov vm_map_unlock_read(&vm->vm_map); 19616bed074cSKonstantin Belousov vmspace_free(vm); 196295e2409aSKonstantin Belousov sx_slock(&allproc_lock); 19633949873fSKonstantin Belousov 19645663e6deSDavid Greenman /* 19653949873fSKonstantin Belousov * If this process is bigger than the biggest one, 19665663e6deSDavid Greenman * remember it. 19675663e6deSDavid Greenman */ 19685663e6deSDavid Greenman if (size > bigsize) { 19691c58e4e5SJohn Baldwin if (bigproc != NULL) 197071943c3dSKonstantin Belousov PRELE(bigproc); 19715663e6deSDavid Greenman bigproc = p; 19725663e6deSDavid Greenman bigsize = size; 197371943c3dSKonstantin Belousov } else { 197471943c3dSKonstantin Belousov PRELE(p); 197571943c3dSKonstantin Belousov } 19765663e6deSDavid Greenman } 19771005a129SJohn Baldwin sx_sunlock(&allproc_lock); 19785663e6deSDavid Greenman if (bigproc != NULL) { 19798311a2b8SWill Andrews if (vm_panic_on_oom != 0) 19808311a2b8SWill Andrews panic("out of swap space"); 198171943c3dSKonstantin Belousov PROC_LOCK(bigproc); 1982729b1e51SDavid Greenman killproc(bigproc, "out of swap space"); 1983fa885116SJulian Elischer sched_nice(bigproc, PRIO_MIN); 198471943c3dSKonstantin Belousov _PRELE(bigproc); 19851c58e4e5SJohn Baldwin PROC_UNLOCK(bigproc); 19865663e6deSDavid Greenman } 19875663e6deSDavid Greenman } 198826f9a767SRodney W. Grimes 19898fc25508SMark Johnston /* 19908fc25508SMark Johnston * Signal a free page shortage to subsystems that have registered an event 19918fc25508SMark Johnston * handler. Reclaim memory from UMA in the event of a severe shortage. 19928fc25508SMark Johnston * Return true if the free page count should be re-evaluated. 19938fc25508SMark Johnston */ 1994b50a4ea6SMark Johnston static bool 1995b50a4ea6SMark Johnston vm_pageout_lowmem(void) 199649a3710cSMark Johnston { 1997b50a4ea6SMark Johnston static int lowmem_ticks = 0; 1998b50a4ea6SMark Johnston int last; 19998fc25508SMark Johnston bool ret; 20008fc25508SMark Johnston 20018fc25508SMark Johnston ret = false; 200249a3710cSMark Johnston 2003b50a4ea6SMark Johnston last = atomic_load_int(&lowmem_ticks); 2004b50a4ea6SMark Johnston while ((u_int)(ticks - last) / hz >= lowmem_period) { 2005b50a4ea6SMark Johnston if (atomic_fcmpset_int(&lowmem_ticks, &last, ticks) == 0) 2006b50a4ea6SMark Johnston continue; 2007b50a4ea6SMark Johnston 200849a3710cSMark Johnston /* 200949a3710cSMark Johnston * Decrease registered cache sizes. 201049a3710cSMark Johnston */ 201149a3710cSMark Johnston SDT_PROBE0(vm, , , vm__lowmem_scan); 201249a3710cSMark Johnston EVENTHANDLER_INVOKE(vm_lowmem, VM_LOW_PAGES); 201349a3710cSMark Johnston 201449a3710cSMark Johnston /* 201549a3710cSMark Johnston * We do this explicitly after the caches have been 20168fc25508SMark Johnston * drained above. 201749a3710cSMark Johnston */ 20188fc25508SMark Johnston uma_reclaim(UMA_RECLAIM_TRIM); 20198fc25508SMark Johnston ret = true; 202049a3710cSMark Johnston } 20218fc25508SMark Johnston 20228fc25508SMark Johnston /* 20238fc25508SMark Johnston * Kick off an asynchronous reclaim of cached memory if one of the 20248fc25508SMark Johnston * page daemons is failing to keep up with demand. Use the "severe" 20258fc25508SMark Johnston * threshold instead of "min" to ensure that we do not blow away the 20268fc25508SMark Johnston * caches if a subset of the NUMA domains are depleted by kernel memory 20278fc25508SMark Johnston * allocations; the domainset iterators automatically skip domains 20288fc25508SMark Johnston * below the "min" threshold on the first pass. 20298fc25508SMark Johnston * 20308fc25508SMark Johnston * UMA reclaim worker has its own rate-limiting mechanism, so don't 20318fc25508SMark Johnston * worry about kicking it too often. 20328fc25508SMark Johnston */ 20338fc25508SMark Johnston if (vm_page_count_severe()) 20348fc25508SMark Johnston uma_reclaim_wakeup(); 20358fc25508SMark Johnston 20368fc25508SMark Johnston return (ret); 203749a3710cSMark Johnston } 203849a3710cSMark Johnston 203949a3710cSMark Johnston static void 2040449c2e92SKonstantin Belousov vm_pageout_worker(void *arg) 2041449c2e92SKonstantin Belousov { 2042e2068d0bSJeff Roberson struct vm_domain *vmd; 2043b50a4ea6SMark Johnston u_int ofree; 204449a3710cSMark Johnston int addl_shortage, domain, shortage; 2045e57dd910SAlan Cox bool target_met; 2046449c2e92SKonstantin Belousov 2047e2068d0bSJeff Roberson domain = (uintptr_t)arg; 2048e2068d0bSJeff Roberson vmd = VM_DOMAIN(domain); 20495f8cd1c0SJeff Roberson shortage = 0; 2050e57dd910SAlan Cox target_met = true; 2051449c2e92SKonstantin Belousov 2052449c2e92SKonstantin Belousov /* 2053949c9186SKonstantin Belousov * XXXKIB It could be useful to bind pageout daemon threads to 2054949c9186SKonstantin Belousov * the cores belonging to the domain, from which vm_page_array 2055949c9186SKonstantin Belousov * is allocated. 2056449c2e92SKonstantin Belousov */ 2057449c2e92SKonstantin Belousov 2058e2068d0bSJeff Roberson KASSERT(vmd->vmd_segs != 0, ("domain without segments")); 2059e2068d0bSJeff Roberson vmd->vmd_last_active_scan = ticks; 2060449c2e92SKonstantin Belousov 2061449c2e92SKonstantin Belousov /* 2062449c2e92SKonstantin Belousov * The pageout daemon worker is never done, so loop forever. 2063449c2e92SKonstantin Belousov */ 2064449c2e92SKonstantin Belousov while (TRUE) { 206530fbfddaSJeff Roberson vm_domain_pageout_lock(vmd); 206649a3710cSMark Johnston 206730fbfddaSJeff Roberson /* 206830fbfddaSJeff Roberson * We need to clear wanted before we check the limits. This 206930fbfddaSJeff Roberson * prevents races with wakers who will check wanted after they 207030fbfddaSJeff Roberson * reach the limit. 207130fbfddaSJeff Roberson */ 207230fbfddaSJeff Roberson atomic_store_int(&vmd->vmd_pageout_wanted, 0); 207356ce0690SAlan Cox 207456ce0690SAlan Cox /* 20755f8cd1c0SJeff Roberson * Might the page daemon need to run again? 2076449c2e92SKonstantin Belousov */ 20775f8cd1c0SJeff Roberson if (vm_paging_needed(vmd, vmd->vmd_free_count)) { 207856ce0690SAlan Cox /* 207949a3710cSMark Johnston * Yes. If the scan failed to produce enough free 208049a3710cSMark Johnston * pages, sleep uninterruptibly for some time in the 208149a3710cSMark Johnston * hope that the laundry thread will clean some pages. 208256ce0690SAlan Cox */ 208330fbfddaSJeff Roberson vm_domain_pageout_unlock(vmd); 208449a3710cSMark Johnston if (!target_met) 20856eebec83SMark Johnston pause("pwait", hz / VM_INACT_SCAN_RATE); 2086449c2e92SKonstantin Belousov } else { 2087449c2e92SKonstantin Belousov /* 20885f8cd1c0SJeff Roberson * No, sleep until the next wakeup or until pages 20895f8cd1c0SJeff Roberson * need to have their reference stats updated. 2090449c2e92SKonstantin Belousov */ 20912c0f13aaSKonstantin Belousov if (mtx_sleep(&vmd->vmd_pageout_wanted, 209230fbfddaSJeff Roberson vm_domain_pageout_lockptr(vmd), PDROP | PVM, 20935f8cd1c0SJeff Roberson "psleep", hz / VM_INACT_SCAN_RATE) == 0) 209483c9dea1SGleb Smirnoff VM_CNT_INC(v_pdwakeups); 209556ce0690SAlan Cox } 2096be37ee79SMark Johnston 209730fbfddaSJeff Roberson /* Prevent spurious wakeups by ensuring that wanted is set. */ 209830fbfddaSJeff Roberson atomic_store_int(&vmd->vmd_pageout_wanted, 1); 209930fbfddaSJeff Roberson 210030fbfddaSJeff Roberson /* 210130fbfddaSJeff Roberson * Use the controller to calculate how many pages to free in 2102b50a4ea6SMark Johnston * this interval, and scan the inactive queue. If the lowmem 2103b50a4ea6SMark Johnston * handlers appear to have freed up some pages, subtract the 2104b50a4ea6SMark Johnston * difference from the inactive queue scan target. 210530fbfddaSJeff Roberson */ 21065f8cd1c0SJeff Roberson shortage = pidctrl_daemon(&vmd->vmd_pid, vmd->vmd_free_count); 210749a3710cSMark Johnston if (shortage > 0) { 2108b50a4ea6SMark Johnston ofree = vmd->vmd_free_count; 2109b50a4ea6SMark Johnston if (vm_pageout_lowmem() && vmd->vmd_free_count > ofree) 2110b50a4ea6SMark Johnston shortage -= min(vmd->vmd_free_count - ofree, 2111b50a4ea6SMark Johnston (u_int)shortage); 211249a3710cSMark Johnston target_met = vm_pageout_scan_inactive(vmd, shortage, 2113be37ee79SMark Johnston &addl_shortage); 211449a3710cSMark Johnston } else 211549a3710cSMark Johnston addl_shortage = 0; 211656ce0690SAlan Cox 2117be37ee79SMark Johnston /* 2118be37ee79SMark Johnston * Scan the active queue. A positive value for shortage 2119be37ee79SMark Johnston * indicates that we must aggressively deactivate pages to avoid 2120be37ee79SMark Johnston * a shortfall. 2121be37ee79SMark Johnston */ 21227bb4634eSMark Johnston shortage = vm_pageout_active_target(vmd) + addl_shortage; 2123be37ee79SMark Johnston vm_pageout_scan_active(vmd, shortage); 2124449c2e92SKonstantin Belousov } 2125449c2e92SKonstantin Belousov } 2126449c2e92SKonstantin Belousov 2127df8bae1dSRodney W. Grimes /* 21289c770a27SMark Johnston * Initialize basic pageout daemon settings. See the comment above the 21299c770a27SMark Johnston * definition of vm_domain for some explanation of how these thresholds are 21309c770a27SMark Johnston * used. 2131df8bae1dSRodney W. Grimes */ 21322b14f991SJulian Elischer static void 2133e2068d0bSJeff Roberson vm_pageout_init_domain(int domain) 2134df8bae1dSRodney W. Grimes { 2135e2068d0bSJeff Roberson struct vm_domain *vmd; 21365f8cd1c0SJeff Roberson struct sysctl_oid *oid; 2137e2068d0bSJeff Roberson 2138e2068d0bSJeff Roberson vmd = VM_DOMAIN(domain); 2139e2068d0bSJeff Roberson vmd->vmd_interrupt_free_min = 2; 2140f6b04d2bSDavid Greenman 214145ae1d91SAlan Cox /* 214245ae1d91SAlan Cox * v_free_reserved needs to include enough for the largest 214345ae1d91SAlan Cox * swap pager structures plus enough for any pv_entry structs 214445ae1d91SAlan Cox * when paging. 214545ae1d91SAlan Cox */ 21460cab71bcSDoug Moore vmd->vmd_pageout_free_min = 2 * MAXBSIZE / PAGE_SIZE + 2147e2068d0bSJeff Roberson vmd->vmd_interrupt_free_min; 2148e2068d0bSJeff Roberson vmd->vmd_free_reserved = vm_pageout_page_count + 21499c770a27SMark Johnston vmd->vmd_pageout_free_min + vmd->vmd_page_count / 768; 21509c770a27SMark Johnston vmd->vmd_free_min = vmd->vmd_page_count / 200; 2151e2068d0bSJeff Roberson vmd->vmd_free_severe = vmd->vmd_free_min / 2; 2152e2068d0bSJeff Roberson vmd->vmd_free_target = 4 * vmd->vmd_free_min + vmd->vmd_free_reserved; 2153e2068d0bSJeff Roberson vmd->vmd_free_min += vmd->vmd_free_reserved; 2154e2068d0bSJeff Roberson vmd->vmd_free_severe += vmd->vmd_free_reserved; 2155e2068d0bSJeff Roberson vmd->vmd_inactive_target = (3 * vmd->vmd_free_target) / 2; 2156e2068d0bSJeff Roberson if (vmd->vmd_inactive_target > vmd->vmd_free_count / 3) 2157e2068d0bSJeff Roberson vmd->vmd_inactive_target = vmd->vmd_free_count / 3; 2158df8bae1dSRodney W. Grimes 2159d9e23210SJeff Roberson /* 21605f8cd1c0SJeff Roberson * Set the default wakeup threshold to be 10% below the paging 21615f8cd1c0SJeff Roberson * target. This keeps the steady state out of shortfall. 2162d9e23210SJeff Roberson */ 21635f8cd1c0SJeff Roberson vmd->vmd_pageout_wakeup_thresh = (vmd->vmd_free_target / 10) * 9; 2164e2068d0bSJeff Roberson 2165e2068d0bSJeff Roberson /* 2166e2068d0bSJeff Roberson * Target amount of memory to move out of the laundry queue during a 2167e2068d0bSJeff Roberson * background laundering. This is proportional to the amount of system 2168e2068d0bSJeff Roberson * memory. 2169e2068d0bSJeff Roberson */ 2170e2068d0bSJeff Roberson vmd->vmd_background_launder_target = (vmd->vmd_free_target - 2171e2068d0bSJeff Roberson vmd->vmd_free_min) / 10; 21725f8cd1c0SJeff Roberson 21735f8cd1c0SJeff Roberson /* Initialize the pageout daemon pid controller. */ 21745f8cd1c0SJeff Roberson pidctrl_init(&vmd->vmd_pid, hz / VM_INACT_SCAN_RATE, 21755f8cd1c0SJeff Roberson vmd->vmd_free_target, PIDCTRL_BOUND, 21765f8cd1c0SJeff Roberson PIDCTRL_KPD, PIDCTRL_KID, PIDCTRL_KDD); 21775f8cd1c0SJeff Roberson oid = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(vmd->vmd_oid), OID_AUTO, 21785f8cd1c0SJeff Roberson "pidctrl", CTLFLAG_RD, NULL, ""); 21795f8cd1c0SJeff Roberson pidctrl_init_sysctl(&vmd->vmd_pid, SYSCTL_CHILDREN(oid)); 2180e2068d0bSJeff Roberson } 2181e2068d0bSJeff Roberson 2182e2068d0bSJeff Roberson static void 2183e2068d0bSJeff Roberson vm_pageout_init(void) 2184e2068d0bSJeff Roberson { 2185e2068d0bSJeff Roberson u_int freecount; 2186e2068d0bSJeff Roberson int i; 2187e2068d0bSJeff Roberson 2188e2068d0bSJeff Roberson /* 2189e2068d0bSJeff Roberson * Initialize some paging parameters. 2190e2068d0bSJeff Roberson */ 2191e2068d0bSJeff Roberson if (vm_cnt.v_page_count < 2000) 2192e2068d0bSJeff Roberson vm_pageout_page_count = 8; 2193e2068d0bSJeff Roberson 2194e2068d0bSJeff Roberson freecount = 0; 2195e2068d0bSJeff Roberson for (i = 0; i < vm_ndomains; i++) { 2196e2068d0bSJeff Roberson struct vm_domain *vmd; 2197e2068d0bSJeff Roberson 2198e2068d0bSJeff Roberson vm_pageout_init_domain(i); 2199e2068d0bSJeff Roberson vmd = VM_DOMAIN(i); 2200e2068d0bSJeff Roberson vm_cnt.v_free_reserved += vmd->vmd_free_reserved; 2201e2068d0bSJeff Roberson vm_cnt.v_free_target += vmd->vmd_free_target; 2202e2068d0bSJeff Roberson vm_cnt.v_free_min += vmd->vmd_free_min; 2203e2068d0bSJeff Roberson vm_cnt.v_inactive_target += vmd->vmd_inactive_target; 2204e2068d0bSJeff Roberson vm_cnt.v_pageout_free_min += vmd->vmd_pageout_free_min; 2205e2068d0bSJeff Roberson vm_cnt.v_interrupt_free_min += vmd->vmd_interrupt_free_min; 2206e2068d0bSJeff Roberson vm_cnt.v_free_severe += vmd->vmd_free_severe; 2207e2068d0bSJeff Roberson freecount += vmd->vmd_free_count; 2208e2068d0bSJeff Roberson } 2209d9e23210SJeff Roberson 2210d9e23210SJeff Roberson /* 2211d9e23210SJeff Roberson * Set interval in seconds for active scan. We want to visit each 2212c9612b2dSJeff Roberson * page at least once every ten minutes. This is to prevent worst 2213c9612b2dSJeff Roberson * case paging behaviors with stale active LRU. 2214d9e23210SJeff Roberson */ 2215d9e23210SJeff Roberson if (vm_pageout_update_period == 0) 2216c9612b2dSJeff Roberson vm_pageout_update_period = 600; 2217d9e23210SJeff Roberson 221854a3a114SMark Johnston if (vm_page_max_user_wired == 0) 221954a3a114SMark Johnston vm_page_max_user_wired = freecount / 3; 22204d19f4adSSteven Hartland } 22214d19f4adSSteven Hartland 22224d19f4adSSteven Hartland /* 22234d19f4adSSteven Hartland * vm_pageout is the high level pageout daemon. 22244d19f4adSSteven Hartland */ 22254d19f4adSSteven Hartland static void 22264d19f4adSSteven Hartland vm_pageout(void) 22274d19f4adSSteven Hartland { 2228920239efSMark Johnston struct proc *p; 2229920239efSMark Johnston struct thread *td; 2230920239efSMark Johnston int error, first, i; 2231920239efSMark Johnston 2232920239efSMark Johnston p = curproc; 2233920239efSMark Johnston td = curthread; 2234df8bae1dSRodney W. Grimes 2235245139c6SKonstantin Belousov mtx_init(&vm_oom_ratelim_mtx, "vmoomr", NULL, MTX_DEF); 223624a1cce3SDavid Greenman swap_pager_swap_init(); 2237920239efSMark Johnston for (first = -1, i = 0; i < vm_ndomains; i++) { 223830c5525bSAndrew Gallatin if (VM_DOMAIN_EMPTY(i)) { 223930c5525bSAndrew Gallatin if (bootverbose) 224030c5525bSAndrew Gallatin printf("domain %d empty; skipping pageout\n", 224130c5525bSAndrew Gallatin i); 224230c5525bSAndrew Gallatin continue; 224330c5525bSAndrew Gallatin } 2244920239efSMark Johnston if (first == -1) 2245920239efSMark Johnston first = i; 2246920239efSMark Johnston else { 2247920239efSMark Johnston error = kthread_add(vm_pageout_worker, 2248920239efSMark Johnston (void *)(uintptr_t)i, p, NULL, 0, 0, "dom%d", i); 2249920239efSMark Johnston if (error != 0) 2250920239efSMark Johnston panic("starting pageout for domain %d: %d\n", 2251449c2e92SKonstantin Belousov i, error); 2252dc2efb27SJohn Dyson } 2253e2068d0bSJeff Roberson error = kthread_add(vm_pageout_laundry_worker, 2254920239efSMark Johnston (void *)(uintptr_t)i, p, NULL, 0, 0, "laundry: dom%d", i); 2255e2068d0bSJeff Roberson if (error != 0) 2256920239efSMark Johnston panic("starting laundry for domain %d: %d", i, error); 2257f919ebdeSDavid Greenman } 2258920239efSMark Johnston error = kthread_add(uma_reclaim_worker, NULL, p, NULL, 0, 0, "uma"); 225944ec2b63SKonstantin Belousov if (error != 0) 226044ec2b63SKonstantin Belousov panic("starting uma_reclaim helper, error %d\n", error); 2261920239efSMark Johnston 2262920239efSMark Johnston snprintf(td->td_name, sizeof(td->td_name), "dom%d", first); 2263920239efSMark Johnston vm_pageout_worker((void *)(uintptr_t)first); 2264df8bae1dSRodney W. Grimes } 226526f9a767SRodney W. Grimes 22666b4b77adSAlan Cox /* 2267280d15cdSMark Johnston * Perform an advisory wakeup of the page daemon. 22686b4b77adSAlan Cox */ 2269e0c5a895SJohn Dyson void 2270e2068d0bSJeff Roberson pagedaemon_wakeup(int domain) 2271e0c5a895SJohn Dyson { 2272e2068d0bSJeff Roberson struct vm_domain *vmd; 2273a1c0a785SAlan Cox 2274e2068d0bSJeff Roberson vmd = VM_DOMAIN(domain); 227530fbfddaSJeff Roberson vm_domain_pageout_assert_unlocked(vmd); 227630fbfddaSJeff Roberson if (curproc == pageproc) 227730fbfddaSJeff Roberson return; 2278280d15cdSMark Johnston 227930fbfddaSJeff Roberson if (atomic_fetchadd_int(&vmd->vmd_pageout_wanted, 1) == 0) { 228030fbfddaSJeff Roberson vm_domain_pageout_lock(vmd); 228130fbfddaSJeff Roberson atomic_store_int(&vmd->vmd_pageout_wanted, 1); 2282e2068d0bSJeff Roberson wakeup(&vmd->vmd_pageout_wanted); 228330fbfddaSJeff Roberson vm_domain_pageout_unlock(vmd); 2284e0c5a895SJohn Dyson } 2285e0c5a895SJohn Dyson } 2286