160727d8bSWarner Losh /*- 226f9a767SRodney W. Grimes * Copyright (c) 1991 Regents of the University of California. 326f9a767SRodney W. Grimes * All rights reserved. 426f9a767SRodney W. Grimes * Copyright (c) 1994 John S. Dyson 526f9a767SRodney W. Grimes * All rights reserved. 626f9a767SRodney W. Grimes * Copyright (c) 1994 David Greenman 726f9a767SRodney W. Grimes * All rights reserved. 88dbca793STor Egge * Copyright (c) 2005 Yahoo! Technologies Norway AS 98dbca793STor Egge * All rights reserved. 10df8bae1dSRodney W. Grimes * 11df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 12df8bae1dSRodney W. Grimes * The Mach Operating System project at Carnegie-Mellon University. 13df8bae1dSRodney W. Grimes * 14df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 15df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 16df8bae1dSRodney W. Grimes * are met: 17df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 18df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 19df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 20df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 21df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 22df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 235929bcfaSPhilippe Charnier * must display the following acknowledgement: 24df8bae1dSRodney W. Grimes * This product includes software developed by the University of 25df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 26df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 27df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 28df8bae1dSRodney W. Grimes * without specific prior written permission. 29df8bae1dSRodney W. Grimes * 30df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 31df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 34df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 35df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 36df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 37df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 38df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 39df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 40df8bae1dSRodney W. Grimes * SUCH DAMAGE. 41df8bae1dSRodney W. Grimes * 423c4dd356SDavid Greenman * from: @(#)vm_pageout.c 7.4 (Berkeley) 5/7/91 43df8bae1dSRodney W. Grimes * 44df8bae1dSRodney W. Grimes * 45df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1990 Carnegie-Mellon University. 46df8bae1dSRodney W. Grimes * All rights reserved. 47df8bae1dSRodney W. Grimes * 48df8bae1dSRodney W. Grimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young 49df8bae1dSRodney W. Grimes * 50df8bae1dSRodney W. Grimes * Permission to use, copy, modify and distribute this software and 51df8bae1dSRodney W. Grimes * its documentation is hereby granted, provided that both the copyright 52df8bae1dSRodney W. Grimes * notice and this permission notice appear in all copies of the 53df8bae1dSRodney W. Grimes * software, derivative works or modified versions, and any portions 54df8bae1dSRodney W. Grimes * thereof, and that both notices appear in supporting documentation. 55df8bae1dSRodney W. Grimes * 56df8bae1dSRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 57df8bae1dSRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 58df8bae1dSRodney W. Grimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 59df8bae1dSRodney W. Grimes * 60df8bae1dSRodney W. Grimes * Carnegie Mellon requests users of this software to return to 61df8bae1dSRodney W. Grimes * 62df8bae1dSRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 63df8bae1dSRodney W. Grimes * School of Computer Science 64df8bae1dSRodney W. Grimes * Carnegie Mellon University 65df8bae1dSRodney W. Grimes * Pittsburgh PA 15213-3890 66df8bae1dSRodney W. Grimes * 67df8bae1dSRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the 68df8bae1dSRodney W. Grimes * rights to redistribute these changes. 69df8bae1dSRodney W. Grimes */ 70df8bae1dSRodney W. Grimes 71df8bae1dSRodney W. Grimes /* 72df8bae1dSRodney W. Grimes * The proverbial page-out daemon. 73df8bae1dSRodney W. Grimes */ 74df8bae1dSRodney W. Grimes 75874651b1SDavid E. O'Brien #include <sys/cdefs.h> 76874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 77874651b1SDavid E. O'Brien 78faa5f8d8SAndrzej Bialecki #include "opt_vm.h" 7914a0d74eSSteven Hartland #include "opt_kdtrace.h" 80df8bae1dSRodney W. Grimes #include <sys/param.h> 8126f9a767SRodney W. Grimes #include <sys/systm.h> 82b5e8ce9fSBruce Evans #include <sys/kernel.h> 83855a310fSJeff Roberson #include <sys/eventhandler.h> 84fb919e4dSMark Murray #include <sys/lock.h> 85fb919e4dSMark Murray #include <sys/mutex.h> 8626f9a767SRodney W. Grimes #include <sys/proc.h> 879c8b8baaSPeter Wemm #include <sys/kthread.h> 880384fff8SJason Evans #include <sys/ktr.h> 8997824da3SAlan Cox #include <sys/mount.h> 90099e7e95SEdward Tomasz Napierala #include <sys/racct.h> 9126f9a767SRodney W. Grimes #include <sys/resourcevar.h> 92b43179fbSJeff Roberson #include <sys/sched.h> 9314a0d74eSSteven Hartland #include <sys/sdt.h> 94d2fc5315SPoul-Henning Kamp #include <sys/signalvar.h> 95449c2e92SKonstantin Belousov #include <sys/smp.h> 96a6bf3a9eSRyan Stone #include <sys/time.h> 97f6b04d2bSDavid Greenman #include <sys/vnode.h> 98efeaf95aSDavid Greenman #include <sys/vmmeter.h> 9989f6b863SAttilio Rao #include <sys/rwlock.h> 1001005a129SJohn Baldwin #include <sys/sx.h> 10138efa82bSJohn Dyson #include <sys/sysctl.h> 102df8bae1dSRodney W. Grimes 103df8bae1dSRodney W. Grimes #include <vm/vm.h> 104efeaf95aSDavid Greenman #include <vm/vm_param.h> 105efeaf95aSDavid Greenman #include <vm/vm_object.h> 106df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 107efeaf95aSDavid Greenman #include <vm/vm_map.h> 108df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h> 10924a1cce3SDavid Greenman #include <vm/vm_pager.h> 110449c2e92SKonstantin Belousov #include <vm/vm_phys.h> 11105f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h> 112efeaf95aSDavid Greenman #include <vm/vm_extern.h> 113670d17b5SJeff Roberson #include <vm/uma.h> 114df8bae1dSRodney W. Grimes 1152b14f991SJulian Elischer /* 1162b14f991SJulian Elischer * System initialization 1172b14f991SJulian Elischer */ 1182b14f991SJulian Elischer 1192b14f991SJulian Elischer /* the kernel process "vm_pageout"*/ 12011caded3SAlfred Perlstein static void vm_pageout(void); 1214d19f4adSSteven Hartland static void vm_pageout_init(void); 12234d8b7eaSJeff Roberson static int vm_pageout_clean(vm_page_t m); 12334d8b7eaSJeff Roberson static int vm_pageout_cluster(vm_page_t m); 124449c2e92SKonstantin Belousov static void vm_pageout_scan(struct vm_domain *vmd, int pass); 125449c2e92SKonstantin Belousov static void vm_pageout_mightbe_oom(struct vm_domain *vmd, int pass); 12645ae1d91SAlan Cox 1274d19f4adSSteven Hartland SYSINIT(pagedaemon_init, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, vm_pageout_init, 1284d19f4adSSteven Hartland NULL); 1294d19f4adSSteven Hartland 1302b14f991SJulian Elischer struct proc *pageproc; 1312b14f991SJulian Elischer 1322b14f991SJulian Elischer static struct kproc_desc page_kp = { 1332b14f991SJulian Elischer "pagedaemon", 1342b14f991SJulian Elischer vm_pageout, 1352b14f991SJulian Elischer &pageproc 1362b14f991SJulian Elischer }; 1374d19f4adSSteven Hartland SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_SECOND, kproc_start, 138237fdd78SRobert Watson &page_kp); 1392b14f991SJulian Elischer 14014a0d74eSSteven Hartland SDT_PROVIDER_DEFINE(vm); 14114a0d74eSSteven Hartland SDT_PROBE_DEFINE(vm, , , vm__lowmem_cache); 14214a0d74eSSteven Hartland SDT_PROBE_DEFINE(vm, , , vm__lowmem_scan); 14314a0d74eSSteven Hartland 14438efa82bSJohn Dyson #if !defined(NO_SWAPPING) 1452b14f991SJulian Elischer /* the kernel process "vm_daemon"*/ 14611caded3SAlfred Perlstein static void vm_daemon(void); 147f708ef1bSPoul-Henning Kamp static struct proc *vmproc; 1482b14f991SJulian Elischer 1492b14f991SJulian Elischer static struct kproc_desc vm_kp = { 1502b14f991SJulian Elischer "vmdaemon", 1512b14f991SJulian Elischer vm_daemon, 1522b14f991SJulian Elischer &vmproc 1532b14f991SJulian Elischer }; 154237fdd78SRobert Watson SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp); 15538efa82bSJohn Dyson #endif 1562b14f991SJulian Elischer 1572b14f991SJulian Elischer 1588b245767SAlan Cox int vm_pages_needed; /* Event on which pageout daemon sleeps */ 1598b245767SAlan Cox int vm_pageout_deficit; /* Estimated number of pages deficit */ 1608b245767SAlan Cox int vm_pageout_pages_needed; /* flag saying that the pageout daemon needs pages */ 161d9e23210SJeff Roberson int vm_pageout_wakeup_thresh; 16226f9a767SRodney W. Grimes 16338efa82bSJohn Dyson #if !defined(NO_SWAPPING) 164f708ef1bSPoul-Henning Kamp static int vm_pageout_req_swapout; /* XXX */ 165f708ef1bSPoul-Henning Kamp static int vm_daemon_needed; 16697824da3SAlan Cox static struct mtx vm_daemon_mtx; 16797824da3SAlan Cox /* Allow for use by vm_pageout before vm_daemon is initialized. */ 16897824da3SAlan Cox MTX_SYSINIT(vm_daemon, &vm_daemon_mtx, "vm daemon", MTX_DEF); 16938efa82bSJohn Dyson #endif 1702b6b0df7SMatthew Dillon static int vm_max_launder = 32; 171d9e23210SJeff Roberson static int vm_pageout_update_period; 1724a365329SAndrey Zonov static int defer_swap_pageouts; 1734a365329SAndrey Zonov static int disable_swap_pageouts; 174c9612b2dSJeff Roberson static int lowmem_period = 10; 175a6bf3a9eSRyan Stone static time_t lowmem_uptime; 17670111b90SJohn Dyson 17738efa82bSJohn Dyson #if defined(NO_SWAPPING) 178303b270bSEivind Eklund static int vm_swap_enabled = 0; 179303b270bSEivind Eklund static int vm_swap_idle_enabled = 0; 18038efa82bSJohn Dyson #else 181303b270bSEivind Eklund static int vm_swap_enabled = 1; 182303b270bSEivind Eklund static int vm_swap_idle_enabled = 0; 18338efa82bSJohn Dyson #endif 18438efa82bSJohn Dyson 1858311a2b8SWill Andrews static int vm_panic_on_oom = 0; 1868311a2b8SWill Andrews 1878311a2b8SWill Andrews SYSCTL_INT(_vm, OID_AUTO, panic_on_oom, 1888311a2b8SWill Andrews CTLFLAG_RWTUN, &vm_panic_on_oom, 0, 1898311a2b8SWill Andrews "panic on out of memory instead of killing the largest process"); 1908311a2b8SWill Andrews 191d9e23210SJeff Roberson SYSCTL_INT(_vm, OID_AUTO, pageout_wakeup_thresh, 192d9e23210SJeff Roberson CTLFLAG_RW, &vm_pageout_wakeup_thresh, 0, 193d9e23210SJeff Roberson "free page threshold for waking up the pageout daemon"); 194d9e23210SJeff Roberson 1952b6b0df7SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, max_launder, 1962b6b0df7SMatthew Dillon CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout"); 19738efa82bSJohn Dyson 198d9e23210SJeff Roberson SYSCTL_INT(_vm, OID_AUTO, pageout_update_period, 199d9e23210SJeff Roberson CTLFLAG_RW, &vm_pageout_update_period, 0, 200d9e23210SJeff Roberson "Maximum active LRU update period"); 20153636869SAndrey Zonov 202c9612b2dSJeff Roberson SYSCTL_INT(_vm, OID_AUTO, lowmem_period, CTLFLAG_RW, &lowmem_period, 0, 203c9612b2dSJeff Roberson "Low memory callback period"); 204c9612b2dSJeff Roberson 20538efa82bSJohn Dyson #if defined(NO_SWAPPING) 206ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 2076bd9cb1cSTom Rhodes CTLFLAG_RD, &vm_swap_enabled, 0, "Enable entire process swapout"); 208ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 2096bd9cb1cSTom Rhodes CTLFLAG_RD, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria"); 21038efa82bSJohn Dyson #else 211ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 212b0359e2cSPeter Wemm CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout"); 213ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 214b0359e2cSPeter Wemm CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria"); 21538efa82bSJohn Dyson #endif 21626f9a767SRodney W. Grimes 217ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts, 218b0359e2cSPeter Wemm CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem"); 21912ac6a1dSJohn Dyson 220ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts, 221b0359e2cSPeter Wemm CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages"); 22212ac6a1dSJohn Dyson 22323b59018SMatthew Dillon static int pageout_lock_miss; 22423b59018SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss, 22523b59018SMatthew Dillon CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout"); 22623b59018SMatthew Dillon 227ffc82b0aSJohn Dyson #define VM_PAGEOUT_PAGE_COUNT 16 228bbc0ec52SDavid Greenman int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT; 229df8bae1dSRodney W. Grimes 230c3cb3e12SDavid Greenman int vm_page_max_wired; /* XXX max # of wired pages system-wide */ 2315dfc2870SAlan Cox SYSCTL_INT(_vm, OID_AUTO, max_wired, 2325dfc2870SAlan Cox CTLFLAG_RW, &vm_page_max_wired, 0, "System-wide limit to wired page count"); 233df8bae1dSRodney W. Grimes 23485eeca35SAlan Cox static boolean_t vm_pageout_fallback_object_lock(vm_page_t, vm_page_t *); 235449c2e92SKonstantin Belousov static boolean_t vm_pageout_launder(struct vm_pagequeue *pq, int, vm_paddr_t, 236449c2e92SKonstantin Belousov vm_paddr_t); 23738efa82bSJohn Dyson #if !defined(NO_SWAPPING) 238ecf6279fSAlan Cox static void vm_pageout_map_deactivate_pages(vm_map_t, long); 239ecf6279fSAlan Cox static void vm_pageout_object_deactivate_pages(pmap_t, vm_object_t, long); 24097824da3SAlan Cox static void vm_req_vmdaemon(int req); 24138efa82bSJohn Dyson #endif 24285eeca35SAlan Cox static boolean_t vm_pageout_page_lock(vm_page_t, vm_page_t *); 243cd41fc12SDavid Greenman 244a8229fa3SAlan Cox /* 245a8229fa3SAlan Cox * Initialize a dummy page for marking the caller's place in the specified 246a8229fa3SAlan Cox * paging queue. In principle, this function only needs to set the flag 247c7aebda8SAttilio Rao * PG_MARKER. Nonetheless, it wirte busies and initializes the hold count 248c7aebda8SAttilio Rao * to one as safety precautions. 249a8229fa3SAlan Cox */ 2508c616246SKonstantin Belousov static void 2518c616246SKonstantin Belousov vm_pageout_init_marker(vm_page_t marker, u_short queue) 2528c616246SKonstantin Belousov { 2538c616246SKonstantin Belousov 2548c616246SKonstantin Belousov bzero(marker, sizeof(*marker)); 255a8229fa3SAlan Cox marker->flags = PG_MARKER; 256c7aebda8SAttilio Rao marker->busy_lock = VPB_SINGLE_EXCLUSIVER; 2578c616246SKonstantin Belousov marker->queue = queue; 258a8229fa3SAlan Cox marker->hold_count = 1; 2598c616246SKonstantin Belousov } 2608c616246SKonstantin Belousov 26126f9a767SRodney W. Grimes /* 2628dbca793STor Egge * vm_pageout_fallback_object_lock: 2638dbca793STor Egge * 26489f6b863SAttilio Rao * Lock vm object currently associated with `m'. VM_OBJECT_TRYWLOCK is 2658dbca793STor Egge * known to have failed and page queue must be either PQ_ACTIVE or 2668dbca793STor Egge * PQ_INACTIVE. To avoid lock order violation, unlock the page queues 2678dbca793STor Egge * while locking the vm object. Use marker page to detect page queue 2688dbca793STor Egge * changes and maintain notion of next page on page queue. Return 2698dbca793STor Egge * TRUE if no changes were detected, FALSE otherwise. vm object is 2708dbca793STor Egge * locked on return. 2718dbca793STor Egge * 2728dbca793STor Egge * This function depends on both the lock portion of struct vm_object 2738dbca793STor Egge * and normal struct vm_page being type stable. 2748dbca793STor Egge */ 27585eeca35SAlan Cox static boolean_t 2768dbca793STor Egge vm_pageout_fallback_object_lock(vm_page_t m, vm_page_t *next) 2778dbca793STor Egge { 2788dbca793STor Egge struct vm_page marker; 2798d220203SAlan Cox struct vm_pagequeue *pq; 2808dbca793STor Egge boolean_t unchanged; 2818dbca793STor Egge u_short queue; 2828dbca793STor Egge vm_object_t object; 2838dbca793STor Egge 2848dbca793STor Egge queue = m->queue; 2858c616246SKonstantin Belousov vm_pageout_init_marker(&marker, queue); 286449c2e92SKonstantin Belousov pq = vm_page_pagequeue(m); 2878dbca793STor Egge object = m->object; 2888dbca793STor Egge 289c325e866SKonstantin Belousov TAILQ_INSERT_AFTER(&pq->pq_pl, m, &marker, plinks.q); 2908d220203SAlan Cox vm_pagequeue_unlock(pq); 2912965a453SKip Macy vm_page_unlock(m); 29289f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 2932965a453SKip Macy vm_page_lock(m); 2948d220203SAlan Cox vm_pagequeue_lock(pq); 2958dbca793STor Egge 2968dbca793STor Egge /* Page queue might have changed. */ 297c325e866SKonstantin Belousov *next = TAILQ_NEXT(&marker, plinks.q); 2988dbca793STor Egge unchanged = (m->queue == queue && 2998dbca793STor Egge m->object == object && 300c325e866SKonstantin Belousov &marker == TAILQ_NEXT(m, plinks.q)); 301c325e866SKonstantin Belousov TAILQ_REMOVE(&pq->pq_pl, &marker, plinks.q); 3028dbca793STor Egge return (unchanged); 3038dbca793STor Egge } 3048dbca793STor Egge 3058dbca793STor Egge /* 3068c616246SKonstantin Belousov * Lock the page while holding the page queue lock. Use marker page 3078c616246SKonstantin Belousov * to detect page queue changes and maintain notion of next page on 3088c616246SKonstantin Belousov * page queue. Return TRUE if no changes were detected, FALSE 3098c616246SKonstantin Belousov * otherwise. The page is locked on return. The page queue lock might 3108c616246SKonstantin Belousov * be dropped and reacquired. 3118c616246SKonstantin Belousov * 3128c616246SKonstantin Belousov * This function depends on normal struct vm_page being type stable. 3138c616246SKonstantin Belousov */ 31485eeca35SAlan Cox static boolean_t 3158c616246SKonstantin Belousov vm_pageout_page_lock(vm_page_t m, vm_page_t *next) 3168c616246SKonstantin Belousov { 3178c616246SKonstantin Belousov struct vm_page marker; 3188d220203SAlan Cox struct vm_pagequeue *pq; 3198c616246SKonstantin Belousov boolean_t unchanged; 3208c616246SKonstantin Belousov u_short queue; 3218c616246SKonstantin Belousov 3228c616246SKonstantin Belousov vm_page_lock_assert(m, MA_NOTOWNED); 3238c616246SKonstantin Belousov if (vm_page_trylock(m)) 3248c616246SKonstantin Belousov return (TRUE); 3258c616246SKonstantin Belousov 3268c616246SKonstantin Belousov queue = m->queue; 3278c616246SKonstantin Belousov vm_pageout_init_marker(&marker, queue); 328449c2e92SKonstantin Belousov pq = vm_page_pagequeue(m); 3298c616246SKonstantin Belousov 330c325e866SKonstantin Belousov TAILQ_INSERT_AFTER(&pq->pq_pl, m, &marker, plinks.q); 3318d220203SAlan Cox vm_pagequeue_unlock(pq); 3328c616246SKonstantin Belousov vm_page_lock(m); 3338d220203SAlan Cox vm_pagequeue_lock(pq); 3348c616246SKonstantin Belousov 3358c616246SKonstantin Belousov /* Page queue might have changed. */ 336c325e866SKonstantin Belousov *next = TAILQ_NEXT(&marker, plinks.q); 337c325e866SKonstantin Belousov unchanged = (m->queue == queue && &marker == TAILQ_NEXT(m, plinks.q)); 338c325e866SKonstantin Belousov TAILQ_REMOVE(&pq->pq_pl, &marker, plinks.q); 3398c616246SKonstantin Belousov return (unchanged); 3408c616246SKonstantin Belousov } 3418c616246SKonstantin Belousov 3428c616246SKonstantin Belousov /* 34326f9a767SRodney W. Grimes * vm_pageout_clean: 34424a1cce3SDavid Greenman * 3450d94caffSDavid Greenman * Clean the page and remove it from the laundry. 34626f9a767SRodney W. Grimes * 3470d94caffSDavid Greenman * We set the busy bit to cause potential page faults on this page to 3481c7c3c6aSMatthew Dillon * block. Note the careful timing, however, the busy bit isn't set till 3491c7c3c6aSMatthew Dillon * late and we cannot do anything that will mess with the page. 35026f9a767SRodney W. Grimes */ 3513af76890SPoul-Henning Kamp static int 35234d8b7eaSJeff Roberson vm_pageout_cluster(vm_page_t m) 35324a1cce3SDavid Greenman { 35454d92145SMatthew Dillon vm_object_t object; 35591b4f427SAlan Cox vm_page_t mc[2*vm_pageout_page_count], pb, ps; 3563562af12SAlan Cox int pageout_count; 35790ecac61SMatthew Dillon int ib, is, page_base; 358a316d390SJohn Dyson vm_pindex_t pindex = m->pindex; 35926f9a767SRodney W. Grimes 36095976f3fSAlan Cox vm_page_lock_assert(m, MA_OWNED); 36117f6a17bSAlan Cox object = m->object; 36289f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 3630cddd8f0SMatthew Dillon 36426f9a767SRodney W. Grimes /* 3651c7c3c6aSMatthew Dillon * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP 3661c7c3c6aSMatthew Dillon * with the new swapper, but we could have serious problems paging 3671c7c3c6aSMatthew Dillon * out other object types if there is insufficient memory. 3681c7c3c6aSMatthew Dillon * 3691c7c3c6aSMatthew Dillon * Unfortunately, checking free memory here is far too late, so the 3701c7c3c6aSMatthew Dillon * check has been moved up a procedural level. 3711c7c3c6aSMatthew Dillon */ 3721c7c3c6aSMatthew Dillon 37324a1cce3SDavid Greenman /* 3749e897b1bSAlan Cox * Can't clean the page if it's busy or held. 37524a1cce3SDavid Greenman */ 376c7aebda8SAttilio Rao vm_page_assert_unbusied(m); 37795976f3fSAlan Cox KASSERT(m->hold_count == 0, ("vm_pageout_clean: page %p is held", m)); 37817f6a17bSAlan Cox vm_page_unlock(m); 3790d94caffSDavid Greenman 38091b4f427SAlan Cox mc[vm_pageout_page_count] = pb = ps = m; 38126f9a767SRodney W. Grimes pageout_count = 1; 382f35329acSJohn Dyson page_base = vm_pageout_page_count; 38390ecac61SMatthew Dillon ib = 1; 38490ecac61SMatthew Dillon is = 1; 38590ecac61SMatthew Dillon 38624a1cce3SDavid Greenman /* 38724a1cce3SDavid Greenman * Scan object for clusterable pages. 38824a1cce3SDavid Greenman * 38924a1cce3SDavid Greenman * We can cluster ONLY if: ->> the page is NOT 39024a1cce3SDavid Greenman * clean, wired, busy, held, or mapped into a 39124a1cce3SDavid Greenman * buffer, and one of the following: 39224a1cce3SDavid Greenman * 1) The page is inactive, or a seldom used 39324a1cce3SDavid Greenman * active page. 39424a1cce3SDavid Greenman * -or- 39524a1cce3SDavid Greenman * 2) we force the issue. 39690ecac61SMatthew Dillon * 39790ecac61SMatthew Dillon * During heavy mmap/modification loads the pageout 39890ecac61SMatthew Dillon * daemon can really fragment the underlying file 39990ecac61SMatthew Dillon * due to flushing pages out of order and not trying 40090ecac61SMatthew Dillon * align the clusters (which leave sporatic out-of-order 40190ecac61SMatthew Dillon * holes). To solve this problem we do the reverse scan 40290ecac61SMatthew Dillon * first and attempt to align our cluster, then do a 40390ecac61SMatthew Dillon * forward scan if room remains. 40424a1cce3SDavid Greenman */ 40590ecac61SMatthew Dillon more: 40690ecac61SMatthew Dillon while (ib && pageout_count < vm_pageout_page_count) { 40724a1cce3SDavid Greenman vm_page_t p; 408f6b04d2bSDavid Greenman 40990ecac61SMatthew Dillon if (ib > pindex) { 41090ecac61SMatthew Dillon ib = 0; 41190ecac61SMatthew Dillon break; 412f6b04d2bSDavid Greenman } 41390ecac61SMatthew Dillon 414c7aebda8SAttilio Rao if ((p = vm_page_prev(pb)) == NULL || vm_page_busied(p)) { 41590ecac61SMatthew Dillon ib = 0; 41690ecac61SMatthew Dillon break; 417f6b04d2bSDavid Greenman } 41824a1cce3SDavid Greenman vm_page_test_dirty(p); 419eb5d3969SAlan Cox if (p->dirty == 0) { 420eb5d3969SAlan Cox ib = 0; 421eb5d3969SAlan Cox break; 422eb5d3969SAlan Cox } 423eb5d3969SAlan Cox vm_page_lock(p); 424eb5d3969SAlan Cox if (p->queue != PQ_INACTIVE || 42557601bcbSMatthew Dillon p->hold_count != 0) { /* may be undergoing I/O */ 4262965a453SKip Macy vm_page_unlock(p); 42790ecac61SMatthew Dillon ib = 0; 42824a1cce3SDavid Greenman break; 429f6b04d2bSDavid Greenman } 4302965a453SKip Macy vm_page_unlock(p); 43191b4f427SAlan Cox mc[--page_base] = pb = p; 43290ecac61SMatthew Dillon ++pageout_count; 43390ecac61SMatthew Dillon ++ib; 43424a1cce3SDavid Greenman /* 43590ecac61SMatthew Dillon * alignment boundry, stop here and switch directions. Do 43690ecac61SMatthew Dillon * not clear ib. 43724a1cce3SDavid Greenman */ 43890ecac61SMatthew Dillon if ((pindex - (ib - 1)) % vm_pageout_page_count == 0) 43990ecac61SMatthew Dillon break; 44024a1cce3SDavid Greenman } 44190ecac61SMatthew Dillon 44290ecac61SMatthew Dillon while (pageout_count < vm_pageout_page_count && 44390ecac61SMatthew Dillon pindex + is < object->size) { 44490ecac61SMatthew Dillon vm_page_t p; 44590ecac61SMatthew Dillon 446c7aebda8SAttilio Rao if ((p = vm_page_next(ps)) == NULL || vm_page_busied(p)) 44790ecac61SMatthew Dillon break; 44824a1cce3SDavid Greenman vm_page_test_dirty(p); 449eb5d3969SAlan Cox if (p->dirty == 0) 450eb5d3969SAlan Cox break; 451eb5d3969SAlan Cox vm_page_lock(p); 452eb5d3969SAlan Cox if (p->queue != PQ_INACTIVE || 45357601bcbSMatthew Dillon p->hold_count != 0) { /* may be undergoing I/O */ 4542965a453SKip Macy vm_page_unlock(p); 45524a1cce3SDavid Greenman break; 45624a1cce3SDavid Greenman } 4572965a453SKip Macy vm_page_unlock(p); 45891b4f427SAlan Cox mc[page_base + pageout_count] = ps = p; 45990ecac61SMatthew Dillon ++pageout_count; 46090ecac61SMatthew Dillon ++is; 46124a1cce3SDavid Greenman } 46290ecac61SMatthew Dillon 46390ecac61SMatthew Dillon /* 46490ecac61SMatthew Dillon * If we exhausted our forward scan, continue with the reverse scan 46590ecac61SMatthew Dillon * when possible, even past a page boundry. This catches boundry 46690ecac61SMatthew Dillon * conditions. 46790ecac61SMatthew Dillon */ 46890ecac61SMatthew Dillon if (ib && pageout_count < vm_pageout_page_count) 46990ecac61SMatthew Dillon goto more; 470f6b04d2bSDavid Greenman 47167bf6868SJohn Dyson /* 47267bf6868SJohn Dyson * we allow reads during pageouts... 47367bf6868SJohn Dyson */ 474126d6082SKonstantin Belousov return (vm_pageout_flush(&mc[page_base], pageout_count, 0, 0, NULL, 475126d6082SKonstantin Belousov NULL)); 476aef922f5SJohn Dyson } 477aef922f5SJohn Dyson 4781c7c3c6aSMatthew Dillon /* 4791c7c3c6aSMatthew Dillon * vm_pageout_flush() - launder the given pages 4801c7c3c6aSMatthew Dillon * 4811c7c3c6aSMatthew Dillon * The given pages are laundered. Note that we setup for the start of 4821c7c3c6aSMatthew Dillon * I/O ( i.e. busy the page ), mark it read-only, and bump the object 4831c7c3c6aSMatthew Dillon * reference count all in here rather then in the parent. If we want 4841c7c3c6aSMatthew Dillon * the parent to do more sophisticated things we may have to change 4851c7c3c6aSMatthew Dillon * the ordering. 4861e8a675cSKonstantin Belousov * 4871e8a675cSKonstantin Belousov * Returned runlen is the count of pages between mreq and first 4881e8a675cSKonstantin Belousov * page after mreq with status VM_PAGER_AGAIN. 489126d6082SKonstantin Belousov * *eio is set to TRUE if pager returned VM_PAGER_ERROR or VM_PAGER_FAIL 490126d6082SKonstantin Belousov * for any page in runlen set. 4911c7c3c6aSMatthew Dillon */ 492aef922f5SJohn Dyson int 493126d6082SKonstantin Belousov vm_pageout_flush(vm_page_t *mc, int count, int flags, int mreq, int *prunlen, 494126d6082SKonstantin Belousov boolean_t *eio) 495aef922f5SJohn Dyson { 4962e3b314dSAlan Cox vm_object_t object = mc[0]->object; 497aef922f5SJohn Dyson int pageout_status[count]; 49895461b45SJohn Dyson int numpagedout = 0; 4991e8a675cSKonstantin Belousov int i, runlen; 500aef922f5SJohn Dyson 50189f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 5027bec141bSKip Macy 5031c7c3c6aSMatthew Dillon /* 5041c7c3c6aSMatthew Dillon * Initiate I/O. Bump the vm_page_t->busy counter and 5051c7c3c6aSMatthew Dillon * mark the pages read-only. 5061c7c3c6aSMatthew Dillon * 5071c7c3c6aSMatthew Dillon * We do not have to fixup the clean/dirty bits here... we can 5081c7c3c6aSMatthew Dillon * allow the pager to do it after the I/O completes. 50902fa91d3SMatthew Dillon * 51002fa91d3SMatthew Dillon * NOTE! mc[i]->dirty may be partial or fragmented due to an 51102fa91d3SMatthew Dillon * edge case with file fragments. 5121c7c3c6aSMatthew Dillon */ 5138f9110f6SJohn Dyson for (i = 0; i < count; i++) { 5147a935082SAlan Cox KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL, 5157a935082SAlan Cox ("vm_pageout_flush: partially invalid page %p index %d/%d", 5167a935082SAlan Cox mc[i], i, count)); 517c7aebda8SAttilio Rao vm_page_sbusy(mc[i]); 51878985e42SAlan Cox pmap_remove_write(mc[i]); 5192965a453SKip Macy } 520d474eaaaSDoug Rabson vm_object_pip_add(object, count); 521aef922f5SJohn Dyson 522d076fbeaSAlan Cox vm_pager_put_pages(object, mc, count, flags, pageout_status); 52326f9a767SRodney W. Grimes 5241e8a675cSKonstantin Belousov runlen = count - mreq; 525126d6082SKonstantin Belousov if (eio != NULL) 526126d6082SKonstantin Belousov *eio = FALSE; 527aef922f5SJohn Dyson for (i = 0; i < count; i++) { 528aef922f5SJohn Dyson vm_page_t mt = mc[i]; 52924a1cce3SDavid Greenman 5304cd45723SAlan Cox KASSERT(pageout_status[i] == VM_PAGER_PEND || 5316031c68dSAlan Cox !pmap_page_is_write_mapped(mt), 5329ea8d1a6SAlan Cox ("vm_pageout_flush: page %p is not write protected", mt)); 53326f9a767SRodney W. Grimes switch (pageout_status[i]) { 53426f9a767SRodney W. Grimes case VM_PAGER_OK: 53526f9a767SRodney W. Grimes case VM_PAGER_PEND: 53695461b45SJohn Dyson numpagedout++; 53726f9a767SRodney W. Grimes break; 53826f9a767SRodney W. Grimes case VM_PAGER_BAD: 53926f9a767SRodney W. Grimes /* 5400d94caffSDavid Greenman * Page outside of range of object. Right now we 5410d94caffSDavid Greenman * essentially lose the changes by pretending it 5420d94caffSDavid Greenman * worked. 54326f9a767SRodney W. Grimes */ 54490ecac61SMatthew Dillon vm_page_undirty(mt); 54526f9a767SRodney W. Grimes break; 54626f9a767SRodney W. Grimes case VM_PAGER_ERROR: 54726f9a767SRodney W. Grimes case VM_PAGER_FAIL: 54826f9a767SRodney W. Grimes /* 5490d94caffSDavid Greenman * If page couldn't be paged out, then reactivate the 5500d94caffSDavid Greenman * page so it doesn't clog the inactive list. (We 5510d94caffSDavid Greenman * will try paging out it again later). 55226f9a767SRodney W. Grimes */ 5533c4a2440SAlan Cox vm_page_lock(mt); 55424a1cce3SDavid Greenman vm_page_activate(mt); 5553c4a2440SAlan Cox vm_page_unlock(mt); 556126d6082SKonstantin Belousov if (eio != NULL && i >= mreq && i - mreq < runlen) 557126d6082SKonstantin Belousov *eio = TRUE; 55826f9a767SRodney W. Grimes break; 55926f9a767SRodney W. Grimes case VM_PAGER_AGAIN: 5601e8a675cSKonstantin Belousov if (i >= mreq && i - mreq < runlen) 5611e8a675cSKonstantin Belousov runlen = i - mreq; 56226f9a767SRodney W. Grimes break; 56326f9a767SRodney W. Grimes } 56426f9a767SRodney W. Grimes 56526f9a767SRodney W. Grimes /* 5660d94caffSDavid Greenman * If the operation is still going, leave the page busy to 5670d94caffSDavid Greenman * block all other accesses. Also, leave the paging in 5680d94caffSDavid Greenman * progress indicator set so that we don't attempt an object 5690d94caffSDavid Greenman * collapse. 57026f9a767SRodney W. Grimes */ 57126f9a767SRodney W. Grimes if (pageout_status[i] != VM_PAGER_PEND) { 572f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 573c7aebda8SAttilio Rao vm_page_sunbusy(mt); 5743c4a2440SAlan Cox } 5753c4a2440SAlan Cox } 5761e8a675cSKonstantin Belousov if (prunlen != NULL) 5771e8a675cSKonstantin Belousov *prunlen = runlen; 5783c4a2440SAlan Cox return (numpagedout); 57926f9a767SRodney W. Grimes } 58026f9a767SRodney W. Grimes 58185eeca35SAlan Cox static boolean_t 582449c2e92SKonstantin Belousov vm_pageout_launder(struct vm_pagequeue *pq, int tries, vm_paddr_t low, 583449c2e92SKonstantin Belousov vm_paddr_t high) 58485eeca35SAlan Cox { 58585eeca35SAlan Cox struct mount *mp; 58685eeca35SAlan Cox struct vnode *vp; 58785eeca35SAlan Cox vm_object_t object; 58885eeca35SAlan Cox vm_paddr_t pa; 58985eeca35SAlan Cox vm_page_t m, m_tmp, next; 5901bd7d0b7SKonstantin Belousov int lockmode; 59185eeca35SAlan Cox 5928d220203SAlan Cox vm_pagequeue_lock(pq); 593c325e866SKonstantin Belousov TAILQ_FOREACH_SAFE(m, &pq->pq_pl, plinks.q, next) { 59485eeca35SAlan Cox if ((m->flags & PG_MARKER) != 0) 59585eeca35SAlan Cox continue; 59685eeca35SAlan Cox pa = VM_PAGE_TO_PHYS(m); 59785eeca35SAlan Cox if (pa < low || pa + PAGE_SIZE > high) 59885eeca35SAlan Cox continue; 59985eeca35SAlan Cox if (!vm_pageout_page_lock(m, &next) || m->hold_count != 0) { 60085eeca35SAlan Cox vm_page_unlock(m); 60185eeca35SAlan Cox continue; 60285eeca35SAlan Cox } 60385eeca35SAlan Cox object = m->object; 60489f6b863SAttilio Rao if ((!VM_OBJECT_TRYWLOCK(object) && 60585eeca35SAlan Cox (!vm_pageout_fallback_object_lock(m, &next) || 606c7aebda8SAttilio Rao m->hold_count != 0)) || vm_page_busied(m)) { 60785eeca35SAlan Cox vm_page_unlock(m); 60889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 60985eeca35SAlan Cox continue; 61085eeca35SAlan Cox } 61185eeca35SAlan Cox vm_page_test_dirty(m); 6129fc4739dSAlan Cox if (m->dirty == 0 && object->ref_count != 0) 61385eeca35SAlan Cox pmap_remove_all(m); 61485eeca35SAlan Cox if (m->dirty != 0) { 61585eeca35SAlan Cox vm_page_unlock(m); 61685eeca35SAlan Cox if (tries == 0 || (object->flags & OBJ_DEAD) != 0) { 61789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 61885eeca35SAlan Cox continue; 61985eeca35SAlan Cox } 62085eeca35SAlan Cox if (object->type == OBJT_VNODE) { 6218d220203SAlan Cox vm_pagequeue_unlock(pq); 62285eeca35SAlan Cox vp = object->handle; 62385eeca35SAlan Cox vm_object_reference_locked(object); 62489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 62585eeca35SAlan Cox (void)vn_start_write(vp, &mp, V_WAIT); 6261bd7d0b7SKonstantin Belousov lockmode = MNT_SHARED_WRITES(vp->v_mount) ? 6271bd7d0b7SKonstantin Belousov LK_SHARED : LK_EXCLUSIVE; 6281bd7d0b7SKonstantin Belousov vn_lock(vp, lockmode | LK_RETRY); 62989f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 63085eeca35SAlan Cox vm_object_page_clean(object, 0, 0, OBJPC_SYNC); 63189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 63285eeca35SAlan Cox VOP_UNLOCK(vp, 0); 63385eeca35SAlan Cox vm_object_deallocate(object); 63485eeca35SAlan Cox vn_finished_write(mp); 63585eeca35SAlan Cox return (TRUE); 63685eeca35SAlan Cox } else if (object->type == OBJT_SWAP || 63785eeca35SAlan Cox object->type == OBJT_DEFAULT) { 6388d220203SAlan Cox vm_pagequeue_unlock(pq); 63985eeca35SAlan Cox m_tmp = m; 64085eeca35SAlan Cox vm_pageout_flush(&m_tmp, 1, VM_PAGER_PUT_SYNC, 64185eeca35SAlan Cox 0, NULL, NULL); 64289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 64385eeca35SAlan Cox return (TRUE); 64485eeca35SAlan Cox } 64585eeca35SAlan Cox } else { 6468d220203SAlan Cox /* 6478d220203SAlan Cox * Dequeue here to prevent lock recursion in 6488d220203SAlan Cox * vm_page_cache(). 6498d220203SAlan Cox */ 6508d220203SAlan Cox vm_page_dequeue_locked(m); 65185eeca35SAlan Cox vm_page_cache(m); 65285eeca35SAlan Cox vm_page_unlock(m); 65385eeca35SAlan Cox } 65489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 65585eeca35SAlan Cox } 6568d220203SAlan Cox vm_pagequeue_unlock(pq); 65785eeca35SAlan Cox return (FALSE); 65885eeca35SAlan Cox } 65985eeca35SAlan Cox 66085eeca35SAlan Cox /* 66185eeca35SAlan Cox * Increase the number of cached pages. The specified value, "tries", 66285eeca35SAlan Cox * determines which categories of pages are cached: 66385eeca35SAlan Cox * 66485eeca35SAlan Cox * 0: All clean, inactive pages within the specified physical address range 66585eeca35SAlan Cox * are cached. Will not sleep. 66685eeca35SAlan Cox * 1: The vm_lowmem handlers are called. All inactive pages within 66785eeca35SAlan Cox * the specified physical address range are cached. May sleep. 66885eeca35SAlan Cox * 2: The vm_lowmem handlers are called. All inactive and active pages 66985eeca35SAlan Cox * within the specified physical address range are cached. May sleep. 67085eeca35SAlan Cox */ 67185eeca35SAlan Cox void 67285eeca35SAlan Cox vm_pageout_grow_cache(int tries, vm_paddr_t low, vm_paddr_t high) 67385eeca35SAlan Cox { 674449c2e92SKonstantin Belousov int actl, actmax, inactl, inactmax, dom, initial_dom; 675449c2e92SKonstantin Belousov static int start_dom = 0; 67685eeca35SAlan Cox 67785eeca35SAlan Cox if (tries > 0) { 67885eeca35SAlan Cox /* 67985eeca35SAlan Cox * Decrease registered cache sizes. The vm_lowmem handlers 68085eeca35SAlan Cox * may acquire locks and/or sleep, so they can only be invoked 68185eeca35SAlan Cox * when "tries" is greater than zero. 68285eeca35SAlan Cox */ 68314a0d74eSSteven Hartland SDT_PROBE0(vm, , , vm__lowmem_cache); 68485eeca35SAlan Cox EVENTHANDLER_INVOKE(vm_lowmem, 0); 68585eeca35SAlan Cox 68685eeca35SAlan Cox /* 68785eeca35SAlan Cox * We do this explicitly after the caches have been drained 68885eeca35SAlan Cox * above. 68985eeca35SAlan Cox */ 69085eeca35SAlan Cox uma_reclaim(); 69185eeca35SAlan Cox } 692449c2e92SKonstantin Belousov 693449c2e92SKonstantin Belousov /* 694449c2e92SKonstantin Belousov * Make the next scan start on the next domain. 695449c2e92SKonstantin Belousov */ 696449c2e92SKonstantin Belousov initial_dom = atomic_fetchadd_int(&start_dom, 1) % vm_ndomains; 697449c2e92SKonstantin Belousov 69885eeca35SAlan Cox inactl = 0; 69944f1c916SBryan Drewery inactmax = vm_cnt.v_inactive_count; 70085eeca35SAlan Cox actl = 0; 70144f1c916SBryan Drewery actmax = tries < 2 ? 0 : vm_cnt.v_active_count; 702449c2e92SKonstantin Belousov dom = initial_dom; 703449c2e92SKonstantin Belousov 704449c2e92SKonstantin Belousov /* 705449c2e92SKonstantin Belousov * Scan domains in round-robin order, first inactive queues, 706449c2e92SKonstantin Belousov * then active. Since domain usually owns large physically 707449c2e92SKonstantin Belousov * contiguous chunk of memory, it makes sense to completely 708449c2e92SKonstantin Belousov * exhaust one domain before switching to next, while growing 709449c2e92SKonstantin Belousov * the pool of contiguous physical pages. 710449c2e92SKonstantin Belousov * 711449c2e92SKonstantin Belousov * Do not even start launder a domain which cannot contain 712449c2e92SKonstantin Belousov * the specified address range, as indicated by segments 713449c2e92SKonstantin Belousov * constituting the domain. 714449c2e92SKonstantin Belousov */ 71585eeca35SAlan Cox again: 716449c2e92SKonstantin Belousov if (inactl < inactmax) { 717449c2e92SKonstantin Belousov if (vm_phys_domain_intersects(vm_dom[dom].vmd_segs, 718449c2e92SKonstantin Belousov low, high) && 719449c2e92SKonstantin Belousov vm_pageout_launder(&vm_dom[dom].vmd_pagequeues[PQ_INACTIVE], 720449c2e92SKonstantin Belousov tries, low, high)) { 72185eeca35SAlan Cox inactl++; 72285eeca35SAlan Cox goto again; 72385eeca35SAlan Cox } 724449c2e92SKonstantin Belousov if (++dom == vm_ndomains) 725449c2e92SKonstantin Belousov dom = 0; 726449c2e92SKonstantin Belousov if (dom != initial_dom) 727449c2e92SKonstantin Belousov goto again; 728449c2e92SKonstantin Belousov } 729449c2e92SKonstantin Belousov if (actl < actmax) { 730449c2e92SKonstantin Belousov if (vm_phys_domain_intersects(vm_dom[dom].vmd_segs, 731449c2e92SKonstantin Belousov low, high) && 732449c2e92SKonstantin Belousov vm_pageout_launder(&vm_dom[dom].vmd_pagequeues[PQ_ACTIVE], 733449c2e92SKonstantin Belousov tries, low, high)) { 73485eeca35SAlan Cox actl++; 73585eeca35SAlan Cox goto again; 73685eeca35SAlan Cox } 737449c2e92SKonstantin Belousov if (++dom == vm_ndomains) 738449c2e92SKonstantin Belousov dom = 0; 739449c2e92SKonstantin Belousov if (dom != initial_dom) 740449c2e92SKonstantin Belousov goto again; 741449c2e92SKonstantin Belousov } 74285eeca35SAlan Cox } 74385eeca35SAlan Cox 74438efa82bSJohn Dyson #if !defined(NO_SWAPPING) 74526f9a767SRodney W. Grimes /* 74626f9a767SRodney W. Grimes * vm_pageout_object_deactivate_pages 74726f9a767SRodney W. Grimes * 748ce186587SAlan Cox * Deactivate enough pages to satisfy the inactive target 749ce186587SAlan Cox * requirements. 75026f9a767SRodney W. Grimes * 75126f9a767SRodney W. Grimes * The object and map must be locked. 75226f9a767SRodney W. Grimes */ 75338efa82bSJohn Dyson static void 754ce186587SAlan Cox vm_pageout_object_deactivate_pages(pmap_t pmap, vm_object_t first_object, 755ce186587SAlan Cox long desired) 75626f9a767SRodney W. Grimes { 757ecf6279fSAlan Cox vm_object_t backing_object, object; 758ce186587SAlan Cox vm_page_t p; 759bb7858eaSJeff Roberson int act_delta, remove_mode; 76026f9a767SRodney W. Grimes 761e23b0a19SAlan Cox VM_OBJECT_ASSERT_LOCKED(first_object); 76228634820SAlan Cox if ((first_object->flags & OBJ_FICTITIOUS) != 0) 76338efa82bSJohn Dyson return; 764ecf6279fSAlan Cox for (object = first_object;; object = backing_object) { 765ecf6279fSAlan Cox if (pmap_resident_count(pmap) <= desired) 766ecf6279fSAlan Cox goto unlock_return; 767e23b0a19SAlan Cox VM_OBJECT_ASSERT_LOCKED(object); 76828634820SAlan Cox if ((object->flags & OBJ_UNMANAGED) != 0 || 76928634820SAlan Cox object->paging_in_progress != 0) 770ecf6279fSAlan Cox goto unlock_return; 77126f9a767SRodney W. Grimes 77285b1dc89SAlan Cox remove_mode = 0; 77338efa82bSJohn Dyson if (object->shadow_count > 1) 77438efa82bSJohn Dyson remove_mode = 1; 77526f9a767SRodney W. Grimes /* 776ce186587SAlan Cox * Scan the object's entire memory queue. 77726f9a767SRodney W. Grimes */ 778ce186587SAlan Cox TAILQ_FOREACH(p, &object->memq, listq) { 779447fe2a4SAlan Cox if (pmap_resident_count(pmap) <= desired) 780447fe2a4SAlan Cox goto unlock_return; 781c7aebda8SAttilio Rao if (vm_page_busied(p)) 782447fe2a4SAlan Cox continue; 783ce186587SAlan Cox PCPU_INC(cnt.v_pdpages); 7842965a453SKip Macy vm_page_lock(p); 785ce186587SAlan Cox if (p->wire_count != 0 || p->hold_count != 0 || 786ecf6279fSAlan Cox !pmap_page_exists_quick(pmap, p)) { 7872965a453SKip Macy vm_page_unlock(p); 7880d94caffSDavid Greenman continue; 7890d94caffSDavid Greenman } 790bb7858eaSJeff Roberson act_delta = pmap_ts_referenced(p); 7913407fefeSKonstantin Belousov if ((p->aflags & PGA_REFERENCED) != 0) { 792bb7858eaSJeff Roberson if (act_delta == 0) 793bb7858eaSJeff Roberson act_delta = 1; 7943407fefeSKonstantin Belousov vm_page_aflag_clear(p, PGA_REFERENCED); 795ef743ce6SJohn Dyson } 796bb7858eaSJeff Roberson if (p->queue != PQ_ACTIVE && act_delta != 0) { 797ef743ce6SJohn Dyson vm_page_activate(p); 798bb7858eaSJeff Roberson p->act_count += act_delta; 799c8c4b40cSJohn Dyson } else if (p->queue == PQ_ACTIVE) { 800bb7858eaSJeff Roberson if (act_delta == 0) { 801ce186587SAlan Cox p->act_count -= min(p->act_count, 802ce186587SAlan Cox ACT_DECLINE); 80390776bd7SJeff Roberson if (!remove_mode && p->act_count == 0) { 8044fec79beSAlan Cox pmap_remove_all(p); 80526f9a767SRodney W. Grimes vm_page_deactivate(p); 8068d220203SAlan Cox } else 8078d220203SAlan Cox vm_page_requeue(p); 808c8c4b40cSJohn Dyson } else { 809eaf13dd7SJohn Dyson vm_page_activate(p); 810ce186587SAlan Cox if (p->act_count < ACT_MAX - 811ce186587SAlan Cox ACT_ADVANCE) 81238efa82bSJohn Dyson p->act_count += ACT_ADVANCE; 8138d220203SAlan Cox vm_page_requeue(p); 814ce186587SAlan Cox } 815ce186587SAlan Cox } else if (p->queue == PQ_INACTIVE) 816ce186587SAlan Cox pmap_remove_all(p); 8172965a453SKip Macy vm_page_unlock(p); 81826f9a767SRodney W. Grimes } 819ecf6279fSAlan Cox if ((backing_object = object->backing_object) == NULL) 820ecf6279fSAlan Cox goto unlock_return; 821e23b0a19SAlan Cox VM_OBJECT_RLOCK(backing_object); 822ecf6279fSAlan Cox if (object != first_object) 823e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(object); 82438efa82bSJohn Dyson } 825ecf6279fSAlan Cox unlock_return: 826ecf6279fSAlan Cox if (object != first_object) 827e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(object); 82826f9a767SRodney W. Grimes } 82926f9a767SRodney W. Grimes 83026f9a767SRodney W. Grimes /* 83126f9a767SRodney W. Grimes * deactivate some number of pages in a map, try to do it fairly, but 83226f9a767SRodney W. Grimes * that is really hard to do. 83326f9a767SRodney W. Grimes */ 834cd41fc12SDavid Greenman static void 83538efa82bSJohn Dyson vm_pageout_map_deactivate_pages(map, desired) 83626f9a767SRodney W. Grimes vm_map_t map; 837ecf6279fSAlan Cox long desired; 83826f9a767SRodney W. Grimes { 83926f9a767SRodney W. Grimes vm_map_entry_t tmpe; 84038efa82bSJohn Dyson vm_object_t obj, bigobj; 84130105b9eSTor Egge int nothingwired; 8420d94caffSDavid Greenman 843d974f03cSAlan Cox if (!vm_map_trylock(map)) 84426f9a767SRodney W. Grimes return; 84538efa82bSJohn Dyson 84638efa82bSJohn Dyson bigobj = NULL; 84730105b9eSTor Egge nothingwired = TRUE; 84838efa82bSJohn Dyson 84938efa82bSJohn Dyson /* 85038efa82bSJohn Dyson * first, search out the biggest object, and try to free pages from 85138efa82bSJohn Dyson * that. 85238efa82bSJohn Dyson */ 85326f9a767SRodney W. Grimes tmpe = map->header.next; 85438efa82bSJohn Dyson while (tmpe != &map->header) { 8559fdfe602SMatthew Dillon if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 85638efa82bSJohn Dyson obj = tmpe->object.vm_object; 857e23b0a19SAlan Cox if (obj != NULL && VM_OBJECT_TRYRLOCK(obj)) { 8580774dfb3SAlan Cox if (obj->shadow_count <= 1 && 8590774dfb3SAlan Cox (bigobj == NULL || 8600774dfb3SAlan Cox bigobj->resident_page_count < obj->resident_page_count)) { 8610774dfb3SAlan Cox if (bigobj != NULL) 862e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(bigobj); 86338efa82bSJohn Dyson bigobj = obj; 8640774dfb3SAlan Cox } else 865e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(obj); 86638efa82bSJohn Dyson } 86738efa82bSJohn Dyson } 86830105b9eSTor Egge if (tmpe->wired_count > 0) 86930105b9eSTor Egge nothingwired = FALSE; 87038efa82bSJohn Dyson tmpe = tmpe->next; 87138efa82bSJohn Dyson } 87238efa82bSJohn Dyson 8730774dfb3SAlan Cox if (bigobj != NULL) { 874ecf6279fSAlan Cox vm_pageout_object_deactivate_pages(map->pmap, bigobj, desired); 875e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(bigobj); 8760774dfb3SAlan Cox } 87738efa82bSJohn Dyson /* 87838efa82bSJohn Dyson * Next, hunt around for other pages to deactivate. We actually 87938efa82bSJohn Dyson * do this search sort of wrong -- .text first is not the best idea. 88038efa82bSJohn Dyson */ 88138efa82bSJohn Dyson tmpe = map->header.next; 88238efa82bSJohn Dyson while (tmpe != &map->header) { 883b1028ad1SLuoqi Chen if (pmap_resident_count(vm_map_pmap(map)) <= desired) 88438efa82bSJohn Dyson break; 8859fdfe602SMatthew Dillon if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 88638efa82bSJohn Dyson obj = tmpe->object.vm_object; 8870774dfb3SAlan Cox if (obj != NULL) { 888e23b0a19SAlan Cox VM_OBJECT_RLOCK(obj); 889ecf6279fSAlan Cox vm_pageout_object_deactivate_pages(map->pmap, obj, desired); 890e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(obj); 8910774dfb3SAlan Cox } 89238efa82bSJohn Dyson } 89326f9a767SRodney W. Grimes tmpe = tmpe->next; 89438857e7fSAlan Cox } 89538efa82bSJohn Dyson 89638efa82bSJohn Dyson /* 89738efa82bSJohn Dyson * Remove all mappings if a process is swapped out, this will free page 89838efa82bSJohn Dyson * table pages. 89938efa82bSJohn Dyson */ 90038857e7fSAlan Cox if (desired == 0 && nothingwired) { 9018d01a3b2SNathan Whitehorn pmap_remove(vm_map_pmap(map), vm_map_min(map), 9028d01a3b2SNathan Whitehorn vm_map_max(map)); 90338857e7fSAlan Cox } 904938b0f5bSMarcel Moolenaar 90538efa82bSJohn Dyson vm_map_unlock(map); 90626f9a767SRodney W. Grimes } 907a1287949SEivind Eklund #endif /* !defined(NO_SWAPPING) */ 908df8bae1dSRodney W. Grimes 9091c7c3c6aSMatthew Dillon /* 91034d8b7eaSJeff Roberson * Attempt to acquire all of the necessary locks to launder a page and 91134d8b7eaSJeff Roberson * then call through the clustering layer to PUTPAGES. Wait a short 91234d8b7eaSJeff Roberson * time for a vnode lock. 91334d8b7eaSJeff Roberson * 91434d8b7eaSJeff Roberson * Requires the page and object lock on entry, releases both before return. 91534d8b7eaSJeff Roberson * Returns 0 on success and an errno otherwise. 91634d8b7eaSJeff Roberson */ 91734d8b7eaSJeff Roberson static int 91834d8b7eaSJeff Roberson vm_pageout_clean(vm_page_t m) 91934d8b7eaSJeff Roberson { 92034d8b7eaSJeff Roberson struct vnode *vp; 92134d8b7eaSJeff Roberson struct mount *mp; 92234d8b7eaSJeff Roberson vm_object_t object; 92334d8b7eaSJeff Roberson vm_pindex_t pindex; 92434d8b7eaSJeff Roberson int error, lockmode; 92534d8b7eaSJeff Roberson 92634d8b7eaSJeff Roberson vm_page_assert_locked(m); 92734d8b7eaSJeff Roberson object = m->object; 92834d8b7eaSJeff Roberson VM_OBJECT_ASSERT_WLOCKED(object); 92934d8b7eaSJeff Roberson error = 0; 93034d8b7eaSJeff Roberson vp = NULL; 93134d8b7eaSJeff Roberson mp = NULL; 93234d8b7eaSJeff Roberson 93334d8b7eaSJeff Roberson /* 93434d8b7eaSJeff Roberson * The object is already known NOT to be dead. It 93534d8b7eaSJeff Roberson * is possible for the vget() to block the whole 93634d8b7eaSJeff Roberson * pageout daemon, but the new low-memory handling 93734d8b7eaSJeff Roberson * code should prevent it. 93834d8b7eaSJeff Roberson * 93934d8b7eaSJeff Roberson * We can't wait forever for the vnode lock, we might 94034d8b7eaSJeff Roberson * deadlock due to a vn_read() getting stuck in 94134d8b7eaSJeff Roberson * vm_wait while holding this vnode. We skip the 94234d8b7eaSJeff Roberson * vnode if we can't get it in a reasonable amount 94334d8b7eaSJeff Roberson * of time. 94434d8b7eaSJeff Roberson */ 94534d8b7eaSJeff Roberson if (object->type == OBJT_VNODE) { 94634d8b7eaSJeff Roberson vm_page_unlock(m); 94734d8b7eaSJeff Roberson vp = object->handle; 94834d8b7eaSJeff Roberson if (vp->v_type == VREG && 94934d8b7eaSJeff Roberson vn_start_write(vp, &mp, V_NOWAIT) != 0) { 95034d8b7eaSJeff Roberson mp = NULL; 95134d8b7eaSJeff Roberson error = EDEADLK; 95234d8b7eaSJeff Roberson goto unlock_all; 95334d8b7eaSJeff Roberson } 95434d8b7eaSJeff Roberson KASSERT(mp != NULL, 95534d8b7eaSJeff Roberson ("vp %p with NULL v_mount", vp)); 95634d8b7eaSJeff Roberson vm_object_reference_locked(object); 95734d8b7eaSJeff Roberson pindex = m->pindex; 95834d8b7eaSJeff Roberson VM_OBJECT_WUNLOCK(object); 95934d8b7eaSJeff Roberson lockmode = MNT_SHARED_WRITES(vp->v_mount) ? 96034d8b7eaSJeff Roberson LK_SHARED : LK_EXCLUSIVE; 96134d8b7eaSJeff Roberson if (vget(vp, lockmode | LK_TIMELOCK, curthread)) { 96234d8b7eaSJeff Roberson vp = NULL; 96334d8b7eaSJeff Roberson error = EDEADLK; 96434d8b7eaSJeff Roberson goto unlock_mp; 96534d8b7eaSJeff Roberson } 96634d8b7eaSJeff Roberson VM_OBJECT_WLOCK(object); 96734d8b7eaSJeff Roberson vm_page_lock(m); 96834d8b7eaSJeff Roberson /* 96934d8b7eaSJeff Roberson * While the object and page were unlocked, the page 97034d8b7eaSJeff Roberson * may have been: 97134d8b7eaSJeff Roberson * (1) moved to a different queue, 97234d8b7eaSJeff Roberson * (2) reallocated to a different object, 97334d8b7eaSJeff Roberson * (3) reallocated to a different offset, or 97434d8b7eaSJeff Roberson * (4) cleaned. 97534d8b7eaSJeff Roberson */ 97634d8b7eaSJeff Roberson if (m->queue != PQ_INACTIVE || m->object != object || 97734d8b7eaSJeff Roberson m->pindex != pindex || m->dirty == 0) { 97834d8b7eaSJeff Roberson vm_page_unlock(m); 97934d8b7eaSJeff Roberson error = ENXIO; 98034d8b7eaSJeff Roberson goto unlock_all; 98134d8b7eaSJeff Roberson } 98234d8b7eaSJeff Roberson 98334d8b7eaSJeff Roberson /* 98434d8b7eaSJeff Roberson * The page may have been busied or held while the object 98534d8b7eaSJeff Roberson * and page locks were released. 98634d8b7eaSJeff Roberson */ 98734d8b7eaSJeff Roberson if (vm_page_busied(m) || m->hold_count != 0) { 98834d8b7eaSJeff Roberson vm_page_unlock(m); 98934d8b7eaSJeff Roberson error = EBUSY; 99034d8b7eaSJeff Roberson goto unlock_all; 99134d8b7eaSJeff Roberson } 99234d8b7eaSJeff Roberson } 99334d8b7eaSJeff Roberson 99434d8b7eaSJeff Roberson /* 99534d8b7eaSJeff Roberson * If a page is dirty, then it is either being washed 99634d8b7eaSJeff Roberson * (but not yet cleaned) or it is still in the 99734d8b7eaSJeff Roberson * laundry. If it is still in the laundry, then we 99834d8b7eaSJeff Roberson * start the cleaning operation. 99934d8b7eaSJeff Roberson */ 100034d8b7eaSJeff Roberson if (vm_pageout_cluster(m) == 0) 100134d8b7eaSJeff Roberson error = EIO; 100234d8b7eaSJeff Roberson 100334d8b7eaSJeff Roberson unlock_all: 100434d8b7eaSJeff Roberson VM_OBJECT_WUNLOCK(object); 100534d8b7eaSJeff Roberson 100634d8b7eaSJeff Roberson unlock_mp: 100734d8b7eaSJeff Roberson vm_page_lock_assert(m, MA_NOTOWNED); 100834d8b7eaSJeff Roberson if (mp != NULL) { 100934d8b7eaSJeff Roberson if (vp != NULL) 101034d8b7eaSJeff Roberson vput(vp); 101134d8b7eaSJeff Roberson vm_object_deallocate(object); 101234d8b7eaSJeff Roberson vn_finished_write(mp); 101334d8b7eaSJeff Roberson } 101434d8b7eaSJeff Roberson 101534d8b7eaSJeff Roberson return (error); 101634d8b7eaSJeff Roberson } 101734d8b7eaSJeff Roberson 101834d8b7eaSJeff Roberson /* 1019df8bae1dSRodney W. Grimes * vm_pageout_scan does the dirty work for the pageout daemon. 1020d9e23210SJeff Roberson * 1021d9e23210SJeff Roberson * pass 0 - Update active LRU/deactivate pages 1022d9e23210SJeff Roberson * pass 1 - Move inactive to cache or free 1023d9e23210SJeff Roberson * pass 2 - Launder dirty pages 1024df8bae1dSRodney W. Grimes */ 10252b6b0df7SMatthew Dillon static void 1026449c2e92SKonstantin Belousov vm_pageout_scan(struct vm_domain *vmd, int pass) 1027df8bae1dSRodney W. Grimes { 1028502ba6e4SJohn Dyson vm_page_t m, next; 10298d220203SAlan Cox struct vm_pagequeue *pq; 1030df8bae1dSRodney W. Grimes vm_object_t object; 103122cf98d1SAlan Cox long min_scan; 103240aa80a7SAlan Cox int act_delta, addl_page_shortage, deficit, error, maxlaunder, maxscan; 103340aa80a7SAlan Cox int page_shortage, scan_tick, scanned, vnodes_skipped; 103440aa80a7SAlan Cox boolean_t pageout_ok, queues_locked; 10350d94caffSDavid Greenman 1036df8bae1dSRodney W. Grimes /* 1037d9e23210SJeff Roberson * If we need to reclaim memory ask kernel caches to return 1038c9612b2dSJeff Roberson * some. We rate limit to avoid thrashing. 1039d9e23210SJeff Roberson */ 1040c9612b2dSJeff Roberson if (vmd == &vm_dom[0] && pass > 0 && 1041a6bf3a9eSRyan Stone (time_uptime - lowmem_uptime) >= lowmem_period) { 1042d9e23210SJeff Roberson /* 1043855a310fSJeff Roberson * Decrease registered cache sizes. 1044855a310fSJeff Roberson */ 104514a0d74eSSteven Hartland SDT_PROBE0(vm, , , vm__lowmem_scan); 1046855a310fSJeff Roberson EVENTHANDLER_INVOKE(vm_lowmem, 0); 1047855a310fSJeff Roberson /* 1048d9e23210SJeff Roberson * We do this explicitly after the caches have been 1049d9e23210SJeff Roberson * drained above. 1050855a310fSJeff Roberson */ 1051855a310fSJeff Roberson uma_reclaim(); 1052a6bf3a9eSRyan Stone lowmem_uptime = time_uptime; 1053d9e23210SJeff Roberson } 10545985940eSJohn Dyson 1055311e34e2SKonstantin Belousov /* 105696240c89SEitan Adler * The addl_page_shortage is the number of temporarily 1057311e34e2SKonstantin Belousov * stuck pages in the inactive queue. In other words, the 1058449c2e92SKonstantin Belousov * number of pages from the inactive count that should be 1059311e34e2SKonstantin Belousov * discounted in setting the target for the active queue scan. 1060311e34e2SKonstantin Belousov */ 10619099545aSAlan Cox addl_page_shortage = 0; 10629099545aSAlan Cox 10631c7c3c6aSMatthew Dillon /* 10641c7c3c6aSMatthew Dillon * Calculate the number of pages we want to either free or move 10652b6b0df7SMatthew Dillon * to the cache. 10661c7c3c6aSMatthew Dillon */ 106760196cdaSAlan Cox if (pass > 0) { 106860196cdaSAlan Cox deficit = atomic_readandclear_int(&vm_pageout_deficit); 10699099545aSAlan Cox page_shortage = vm_paging_target() + deficit; 107060196cdaSAlan Cox } else 107160196cdaSAlan Cox page_shortage = deficit = 0; 10721c7c3c6aSMatthew Dillon 1073936524aaSMatthew Dillon /* 10742b6b0df7SMatthew Dillon * maxlaunder limits the number of dirty pages we flush per scan. 10752b6b0df7SMatthew Dillon * For most systems a smaller value (16 or 32) is more robust under 10762b6b0df7SMatthew Dillon * extreme memory and disk pressure because any unnecessary writes 10772b6b0df7SMatthew Dillon * to disk can result in extreme performance degredation. However, 10782b6b0df7SMatthew Dillon * systems with excessive dirty pages (especially when MAP_NOSYNC is 10792b6b0df7SMatthew Dillon * used) will die horribly with limited laundering. If the pageout 10802b6b0df7SMatthew Dillon * daemon cannot clean enough pages in the first pass, we let it go 10812b6b0df7SMatthew Dillon * all out in succeeding passes. 10821c7c3c6aSMatthew Dillon */ 10832b6b0df7SMatthew Dillon if ((maxlaunder = vm_max_launder) <= 1) 10842b6b0df7SMatthew Dillon maxlaunder = 1; 1085d9e23210SJeff Roberson if (pass > 1) 10862b6b0df7SMatthew Dillon maxlaunder = 10000; 10878d220203SAlan Cox 108840aa80a7SAlan Cox vnodes_skipped = 0; 108940aa80a7SAlan Cox 10908d220203SAlan Cox /* 10918d220203SAlan Cox * Start scanning the inactive queue for pages we can move to the 10928d220203SAlan Cox * cache or free. The scan will stop when the target is reached or 10938d220203SAlan Cox * we have scanned the entire inactive queue. Note that m->act_count 10948d220203SAlan Cox * is not used to form decisions for the inactive queue, only for the 10958d220203SAlan Cox * active queue. 10968d220203SAlan Cox */ 1097449c2e92SKonstantin Belousov pq = &vmd->vmd_pagequeues[PQ_INACTIVE]; 1098449c2e92SKonstantin Belousov maxscan = pq->pq_cnt; 10998d220203SAlan Cox vm_pagequeue_lock(pq); 11008d220203SAlan Cox queues_locked = TRUE; 11018d220203SAlan Cox for (m = TAILQ_FIRST(&pq->pq_pl); 11021c7c3c6aSMatthew Dillon m != NULL && maxscan-- > 0 && page_shortage > 0; 1103e929c00dSKirk McKusick m = next) { 11048d220203SAlan Cox vm_pagequeue_assert_locked(pq); 110548cc2fc7SKonstantin Belousov KASSERT(queues_locked, ("unlocked queues")); 1106d4961bcbSKonstantin Belousov KASSERT(m->queue == PQ_INACTIVE, ("Inactive queue %p", m)); 1107df8bae1dSRodney W. Grimes 11088d220203SAlan Cox PCPU_INC(cnt.v_pdpages); 1109c325e866SKonstantin Belousov next = TAILQ_NEXT(m, plinks.q); 1110df8bae1dSRodney W. Grimes 1111936524aaSMatthew Dillon /* 1112936524aaSMatthew Dillon * skip marker pages 1113936524aaSMatthew Dillon */ 1114936524aaSMatthew Dillon if (m->flags & PG_MARKER) 1115936524aaSMatthew Dillon continue; 1116936524aaSMatthew Dillon 11177900f95dSKonstantin Belousov KASSERT((m->flags & PG_FICTITIOUS) == 0, 11187900f95dSKonstantin Belousov ("Fictitious page %p cannot be in inactive queue", m)); 11197900f95dSKonstantin Belousov KASSERT((m->oflags & VPO_UNMANAGED) == 0, 11207900f95dSKonstantin Belousov ("Unmanaged page %p cannot be in inactive queue", m)); 11217900f95dSKonstantin Belousov 11228c616246SKonstantin Belousov /* 1123311e34e2SKonstantin Belousov * The page or object lock acquisitions fail if the 1124311e34e2SKonstantin Belousov * page was removed from the queue or moved to a 1125311e34e2SKonstantin Belousov * different position within the queue. In either 1126311e34e2SKonstantin Belousov * case, addl_page_shortage should not be incremented. 11278c616246SKonstantin Belousov */ 1128*a3aeedabSAlan Cox if (!vm_pageout_page_lock(m, &next)) 1129*a3aeedabSAlan Cox goto unlock_page; 1130*a3aeedabSAlan Cox else if (m->hold_count != 0) { 1131*a3aeedabSAlan Cox /* 1132*a3aeedabSAlan Cox * Held pages are essentially stuck in the 1133*a3aeedabSAlan Cox * queue. So, they ought to be discounted 1134*a3aeedabSAlan Cox * from the inactive count. See the 1135*a3aeedabSAlan Cox * calculation of the page_shortage for the 1136*a3aeedabSAlan Cox * loop over the active queue below. 1137*a3aeedabSAlan Cox */ 1138*a3aeedabSAlan Cox addl_page_shortage++; 1139*a3aeedabSAlan Cox goto unlock_page; 1140df8bae1dSRodney W. Grimes } 11419ee2165fSAlan Cox object = m->object; 1142*a3aeedabSAlan Cox if (!VM_OBJECT_TRYWLOCK(object)) { 1143*a3aeedabSAlan Cox if (!vm_pageout_fallback_object_lock(m, &next)) 1144*a3aeedabSAlan Cox goto unlock_object; 1145*a3aeedabSAlan Cox else if (m->hold_count != 0) { 1146b182ec9eSJohn Dyson addl_page_shortage++; 1147*a3aeedabSAlan Cox goto unlock_object; 1148*a3aeedabSAlan Cox } 1149*a3aeedabSAlan Cox } 1150*a3aeedabSAlan Cox if (vm_page_busied(m)) { 1151*a3aeedabSAlan Cox /* 1152*a3aeedabSAlan Cox * Don't mess with busy pages. Leave them at 1153*a3aeedabSAlan Cox * the front of the queue. Most likely, they 1154*a3aeedabSAlan Cox * are being paged out and will leave the 1155*a3aeedabSAlan Cox * queue shortly after the scan finishes. So, 1156*a3aeedabSAlan Cox * they ought to be discounted from the 1157*a3aeedabSAlan Cox * inactive count. 1158*a3aeedabSAlan Cox */ 1159*a3aeedabSAlan Cox addl_page_shortage++; 1160*a3aeedabSAlan Cox unlock_object: 1161*a3aeedabSAlan Cox VM_OBJECT_WUNLOCK(object); 1162*a3aeedabSAlan Cox unlock_page: 1163*a3aeedabSAlan Cox vm_page_unlock(m); 116426f9a767SRodney W. Grimes continue; 116526f9a767SRodney W. Grimes } 1166*a3aeedabSAlan Cox KASSERT(m->hold_count == 0, ("Held page %p", m)); 1167bd7e5f99SJohn Dyson 11687e006499SJohn Dyson /* 11698d220203SAlan Cox * We unlock the inactive page queue, invalidating the 117048cc2fc7SKonstantin Belousov * 'next' pointer. Use our marker to remember our 117148cc2fc7SKonstantin Belousov * place. 117248cc2fc7SKonstantin Belousov */ 1173c325e866SKonstantin Belousov TAILQ_INSERT_AFTER(&pq->pq_pl, m, &vmd->vmd_marker, plinks.q); 11748d220203SAlan Cox vm_pagequeue_unlock(pq); 117548cc2fc7SKonstantin Belousov queues_locked = FALSE; 117648cc2fc7SKonstantin Belousov 117748cc2fc7SKonstantin Belousov /* 1178776f729cSKonstantin Belousov * Invalid pages can be easily freed. They cannot be 1179776f729cSKonstantin Belousov * mapped, vm_page_free() asserts this. 1180776f729cSKonstantin Belousov */ 1181*a3aeedabSAlan Cox if (m->valid == 0) { 1182776f729cSKonstantin Belousov vm_page_free(m); 1183776f729cSKonstantin Belousov PCPU_INC(cnt.v_dfree); 1184776f729cSKonstantin Belousov --page_shortage; 1185776f729cSKonstantin Belousov goto drop_page; 1186776f729cSKonstantin Belousov } 1187776f729cSKonstantin Belousov 1188776f729cSKonstantin Belousov /* 1189bb7858eaSJeff Roberson * We bump the activation count if the page has been 1190bb7858eaSJeff Roberson * referenced while in the inactive queue. This makes 1191bb7858eaSJeff Roberson * it less likely that the page will be added back to the 1192bb7858eaSJeff Roberson * inactive queue prematurely again. Here we check the 11931c7c3c6aSMatthew Dillon * page tables (or emulated bits, if any), given the upper 11941c7c3c6aSMatthew Dillon * level VM system not knowing anything about existing 11951c7c3c6aSMatthew Dillon * references. 11967e006499SJohn Dyson */ 1197bb7858eaSJeff Roberson if ((m->aflags & PGA_REFERENCED) != 0) { 1198bb7858eaSJeff Roberson vm_page_aflag_clear(m, PGA_REFERENCED); 1199bb7858eaSJeff Roberson act_delta = 1; 120086fa2471SAlan Cox } else 120186fa2471SAlan Cox act_delta = 0; 1202bb7858eaSJeff Roberson if (object->ref_count != 0) { 1203bb7858eaSJeff Roberson act_delta += pmap_ts_referenced(m); 1204bb7858eaSJeff Roberson } else { 1205bb7858eaSJeff Roberson KASSERT(!pmap_page_is_mapped(m), 1206bb7858eaSJeff Roberson ("vm_pageout_scan: page %p is mapped", m)); 12072fe6e4d7SDavid Greenman } 1208ef743ce6SJohn Dyson 12097e006499SJohn Dyson /* 12101c7c3c6aSMatthew Dillon * If the upper level VM system knows about any page 1211bb7858eaSJeff Roberson * references, we reactivate the page or requeue it. 12127e006499SJohn Dyson */ 1213bb7858eaSJeff Roberson if (act_delta != 0) { 121486fa2471SAlan Cox if (object->ref_count != 0) { 121526f9a767SRodney W. Grimes vm_page_activate(m); 1216bb7858eaSJeff Roberson m->act_count += act_delta + ACT_ADVANCE; 1217bb7858eaSJeff Roberson } else { 1218bb7858eaSJeff Roberson vm_pagequeue_lock(pq); 1219bb7858eaSJeff Roberson queues_locked = TRUE; 1220bb7858eaSJeff Roberson vm_page_requeue_locked(m); 1221bb7858eaSJeff Roberson } 1222776f729cSKonstantin Belousov goto drop_page; 12230d94caffSDavid Greenman } 122467bf6868SJohn Dyson 12257e006499SJohn Dyson /* 12269fc4739dSAlan Cox * If the page appears to be clean at the machine-independent 12279fc4739dSAlan Cox * layer, then remove all of its mappings from the pmap in 12289fc4739dSAlan Cox * anticipation of placing it onto the cache queue. If, 12299fc4739dSAlan Cox * however, any of the page's mappings allow write access, 12309fc4739dSAlan Cox * then the page may still be modified until the last of those 12319fc4739dSAlan Cox * mappings are removed. 12327e006499SJohn Dyson */ 1233aa044135SAlan Cox if (object->ref_count != 0) { 12349fc4739dSAlan Cox vm_page_test_dirty(m); 1235aa044135SAlan Cox if (m->dirty == 0) 1236b78ddb0bSAlan Cox pmap_remove_all(m); 1237aa044135SAlan Cox } 1238dcbcd518SBruce Evans 1239776f729cSKonstantin Belousov if (m->dirty == 0) { 12406989c456SAlan Cox /* 124178afdce6SAlan Cox * Clean pages can be freed. 12426989c456SAlan Cox */ 124378afdce6SAlan Cox vm_page_free(m); 124478afdce6SAlan Cox PCPU_INC(cnt.v_dfree); 12451c7c3c6aSMatthew Dillon --page_shortage; 1246d9e23210SJeff Roberson } else if ((m->flags & PG_WINATCFLS) == 0 && pass < 2) { 12477e006499SJohn Dyson /* 12482b6b0df7SMatthew Dillon * Dirty pages need to be paged out, but flushing 1249ab46f63eSJohn Baldwin * a page is extremely expensive versus freeing 12502b6b0df7SMatthew Dillon * a clean page. Rather then artificially limiting 12512b6b0df7SMatthew Dillon * the number of pages we can flush, we instead give 12522b6b0df7SMatthew Dillon * dirty pages extra priority on the inactive queue 12532b6b0df7SMatthew Dillon * by forcing them to be cycled through the queue 12542b6b0df7SMatthew Dillon * twice before being flushed, after which the 12552b6b0df7SMatthew Dillon * (now clean) page will cycle through once more 12562b6b0df7SMatthew Dillon * before being freed. This significantly extends 12572b6b0df7SMatthew Dillon * the thrash point for a heavily loaded machine. 12587e006499SJohn Dyson */ 12593407fefeSKonstantin Belousov m->flags |= PG_WINATCFLS; 12608d220203SAlan Cox vm_pagequeue_lock(pq); 126148cc2fc7SKonstantin Belousov queues_locked = TRUE; 12628d220203SAlan Cox vm_page_requeue_locked(m); 12630d94caffSDavid Greenman } else if (maxlaunder > 0) { 12642b6b0df7SMatthew Dillon /* 12652b6b0df7SMatthew Dillon * We always want to try to flush some dirty pages if 12662b6b0df7SMatthew Dillon * we encounter them, to keep the system stable. 12672b6b0df7SMatthew Dillon * Normally this number is small, but under extreme 12682b6b0df7SMatthew Dillon * pressure where there are insufficient clean pages 12692b6b0df7SMatthew Dillon * on the inactive queue, we may have to go all out. 12702b6b0df7SMatthew Dillon */ 12710d94caffSDavid Greenman 127240aa80a7SAlan Cox if (object->type != OBJT_SWAP && 127340aa80a7SAlan Cox object->type != OBJT_DEFAULT) 127440aa80a7SAlan Cox pageout_ok = TRUE; 127540aa80a7SAlan Cox else if (disable_swap_pageouts) 127640aa80a7SAlan Cox pageout_ok = FALSE; 127740aa80a7SAlan Cox else if (defer_swap_pageouts) 127840aa80a7SAlan Cox pageout_ok = vm_page_count_min(); 127940aa80a7SAlan Cox else 128040aa80a7SAlan Cox pageout_ok = TRUE; 128170111b90SJohn Dyson 128270111b90SJohn Dyson /* 12831c7c3c6aSMatthew Dillon * We don't bother paging objects that are "dead". 12841c7c3c6aSMatthew Dillon * Those objects are in a "rundown" state. 128570111b90SJohn Dyson */ 128640aa80a7SAlan Cox if (!pageout_ok || (object->flags & OBJ_DEAD) != 0) { 12878d220203SAlan Cox vm_pagequeue_lock(pq); 12882965a453SKip Macy vm_page_unlock(m); 128989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 129048cc2fc7SKonstantin Belousov queues_locked = TRUE; 12918d220203SAlan Cox vm_page_requeue_locked(m); 129248cc2fc7SKonstantin Belousov goto relock_queues; 129312ac6a1dSJohn Dyson } 129434d8b7eaSJeff Roberson error = vm_pageout_clean(m); 12951c7c3c6aSMatthew Dillon /* 129634d8b7eaSJeff Roberson * Decrement page_shortage on success to account for 12972b6b0df7SMatthew Dillon * the (future) cleaned page. Otherwise we could wind 12982b6b0df7SMatthew Dillon * up laundering or cleaning too many pages. 12990d94caffSDavid Greenman */ 130034d8b7eaSJeff Roberson if (error == 0) { 130134d8b7eaSJeff Roberson page_shortage--; 130234d8b7eaSJeff Roberson maxlaunder--; 130334d8b7eaSJeff Roberson } else if (error == EDEADLK) { 130434d8b7eaSJeff Roberson pageout_lock_miss++; 130534d8b7eaSJeff Roberson vnodes_skipped++; 130634d8b7eaSJeff Roberson } else if (error == EBUSY) { 130734d8b7eaSJeff Roberson addl_page_shortage++; 130848cc2fc7SKonstantin Belousov } 130948cc2fc7SKonstantin Belousov vm_page_lock_assert(m, MA_NOTOWNED); 131048cc2fc7SKonstantin Belousov goto relock_queues; 131148cc2fc7SKonstantin Belousov } 1312776f729cSKonstantin Belousov drop_page: 131348cc2fc7SKonstantin Belousov vm_page_unlock(m); 131489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 131548cc2fc7SKonstantin Belousov relock_queues: 131648cc2fc7SKonstantin Belousov if (!queues_locked) { 13178d220203SAlan Cox vm_pagequeue_lock(pq); 131848cc2fc7SKonstantin Belousov queues_locked = TRUE; 13196989c456SAlan Cox } 1320c325e866SKonstantin Belousov next = TAILQ_NEXT(&vmd->vmd_marker, plinks.q); 1321c325e866SKonstantin Belousov TAILQ_REMOVE(&pq->pq_pl, &vmd->vmd_marker, plinks.q); 13220d94caffSDavid Greenman } 13238d220203SAlan Cox vm_pagequeue_unlock(pq); 132426f9a767SRodney W. Grimes 13259452b5edSAlan Cox #if !defined(NO_SWAPPING) 13269452b5edSAlan Cox /* 13279452b5edSAlan Cox * Wakeup the swapout daemon if we didn't cache or free the targeted 13289452b5edSAlan Cox * number of pages. 13299452b5edSAlan Cox */ 13309452b5edSAlan Cox if (vm_swap_enabled && page_shortage > 0) 13319452b5edSAlan Cox vm_req_vmdaemon(VM_SWAP_NORMAL); 13329452b5edSAlan Cox #endif 13339452b5edSAlan Cox 13349452b5edSAlan Cox /* 13359452b5edSAlan Cox * Wakeup the sync daemon if we skipped a vnode in a writeable object 13369452b5edSAlan Cox * and we didn't cache or free enough pages. 13379452b5edSAlan Cox */ 13389452b5edSAlan Cox if (vnodes_skipped > 0 && page_shortage > vm_cnt.v_free_target - 13399452b5edSAlan Cox vm_cnt.v_free_min) 13409452b5edSAlan Cox (void)speedup_syncer(); 13419452b5edSAlan Cox 1342df8bae1dSRodney W. Grimes /* 1343936524aaSMatthew Dillon * Compute the number of pages we want to try to move from the 1344936524aaSMatthew Dillon * active queue to the inactive queue. 13451c7c3c6aSMatthew Dillon */ 134644f1c916SBryan Drewery page_shortage = vm_cnt.v_inactive_target - vm_cnt.v_inactive_count + 13479099545aSAlan Cox vm_paging_target() + deficit + addl_page_shortage; 13489099545aSAlan Cox 1349114f62c6SJeff Roberson pq = &vmd->vmd_pagequeues[PQ_ACTIVE]; 1350114f62c6SJeff Roberson vm_pagequeue_lock(pq); 13519099545aSAlan Cox maxscan = pq->pq_cnt; 13529099545aSAlan Cox 1353d9e23210SJeff Roberson /* 1354d9e23210SJeff Roberson * If we're just idle polling attempt to visit every 1355d9e23210SJeff Roberson * active page within 'update_period' seconds. 1356d9e23210SJeff Roberson */ 135722cf98d1SAlan Cox scan_tick = ticks; 135822cf98d1SAlan Cox if (vm_pageout_update_period != 0) { 135922cf98d1SAlan Cox min_scan = pq->pq_cnt; 136022cf98d1SAlan Cox min_scan *= scan_tick - vmd->vmd_last_active_scan; 136122cf98d1SAlan Cox min_scan /= hz * vm_pageout_update_period; 136222cf98d1SAlan Cox } else 136322cf98d1SAlan Cox min_scan = 0; 136422cf98d1SAlan Cox if (min_scan > 0 || (page_shortage > 0 && maxscan > 0)) 136522cf98d1SAlan Cox vmd->vmd_last_active_scan = scan_tick; 13661c7c3c6aSMatthew Dillon 13671c7c3c6aSMatthew Dillon /* 136822cf98d1SAlan Cox * Scan the active queue for pages that can be deactivated. Update 136922cf98d1SAlan Cox * the per-page activity counter and use it to identify deactivation 137022cf98d1SAlan Cox * candidates. 13711c7c3c6aSMatthew Dillon */ 137222cf98d1SAlan Cox for (m = TAILQ_FIRST(&pq->pq_pl), scanned = 0; m != NULL && (scanned < 137322cf98d1SAlan Cox min_scan || (page_shortage > 0 && scanned < maxscan)); m = next, 137422cf98d1SAlan Cox scanned++) { 1375f35329acSJohn Dyson 13769cf51988SAlan Cox KASSERT(m->queue == PQ_ACTIVE, 1377d3c09dd7SAlan Cox ("vm_pageout_scan: page %p isn't active", m)); 1378f35329acSJohn Dyson 1379c325e866SKonstantin Belousov next = TAILQ_NEXT(m, plinks.q); 138022cf98d1SAlan Cox if ((m->flags & PG_MARKER) != 0) 13818dbca793STor Egge continue; 13827900f95dSKonstantin Belousov KASSERT((m->flags & PG_FICTITIOUS) == 0, 13837900f95dSKonstantin Belousov ("Fictitious page %p cannot be in active queue", m)); 13847900f95dSKonstantin Belousov KASSERT((m->oflags & VPO_UNMANAGED) == 0, 13857900f95dSKonstantin Belousov ("Unmanaged page %p cannot be in active queue", m)); 13869ee2165fSAlan Cox if (!vm_pageout_page_lock(m, &next)) { 13878c616246SKonstantin Belousov vm_page_unlock(m); 13882965a453SKip Macy continue; 13892965a453SKip Macy } 1390b18bfc3dSJohn Dyson 1391b18bfc3dSJohn Dyson /* 1392b18bfc3dSJohn Dyson * The count for pagedaemon pages is done after checking the 1393956f3135SPhilippe Charnier * page for eligibility... 1394b18bfc3dSJohn Dyson */ 13958d220203SAlan Cox PCPU_INC(cnt.v_pdpages); 1396ef743ce6SJohn Dyson 13977e006499SJohn Dyson /* 13987e006499SJohn Dyson * Check to see "how much" the page has been used. 13997e006499SJohn Dyson */ 140086fa2471SAlan Cox if ((m->aflags & PGA_REFERENCED) != 0) { 1401bb7858eaSJeff Roberson vm_page_aflag_clear(m, PGA_REFERENCED); 140286fa2471SAlan Cox act_delta = 1; 140386fa2471SAlan Cox } else 140486fa2471SAlan Cox act_delta = 0; 140586fa2471SAlan Cox 1406274132acSJeff Roberson /* 1407274132acSJeff Roberson * Unlocked object ref count check. Two races are possible. 1408274132acSJeff Roberson * 1) The ref was transitioning to zero and we saw non-zero, 1409274132acSJeff Roberson * the pmap bits will be checked unnecessarily. 1410274132acSJeff Roberson * 2) The ref was transitioning to one and we saw zero. 1411274132acSJeff Roberson * The page lock prevents a new reference to this page so 1412274132acSJeff Roberson * we need not check the reference bits. 1413274132acSJeff Roberson */ 1414274132acSJeff Roberson if (m->object->ref_count != 0) 1415bb7858eaSJeff Roberson act_delta += pmap_ts_referenced(m); 1416bb7858eaSJeff Roberson 1417bb7858eaSJeff Roberson /* 1418bb7858eaSJeff Roberson * Advance or decay the act_count based on recent usage. 1419bb7858eaSJeff Roberson */ 142086fa2471SAlan Cox if (act_delta != 0) { 1421bb7858eaSJeff Roberson m->act_count += ACT_ADVANCE + act_delta; 142238efa82bSJohn Dyson if (m->act_count > ACT_MAX) 142338efa82bSJohn Dyson m->act_count = ACT_MAX; 142486fa2471SAlan Cox } else 142538efa82bSJohn Dyson m->act_count -= min(m->act_count, ACT_DECLINE); 1426bb7858eaSJeff Roberson 1427bb7858eaSJeff Roberson /* 1428bb7858eaSJeff Roberson * Move this page to the tail of the active or inactive 1429bb7858eaSJeff Roberson * queue depending on usage. 1430bb7858eaSJeff Roberson */ 143186fa2471SAlan Cox if (m->act_count == 0) { 14328d220203SAlan Cox /* Dequeue to avoid later lock recursion. */ 14338d220203SAlan Cox vm_page_dequeue_locked(m); 1434d4a272dbSJohn Dyson vm_page_deactivate(m); 1435bb7858eaSJeff Roberson page_shortage--; 14368d220203SAlan Cox } else 14378d220203SAlan Cox vm_page_requeue_locked(m); 14382965a453SKip Macy vm_page_unlock(m); 143926f9a767SRodney W. Grimes } 14408d220203SAlan Cox vm_pagequeue_unlock(pq); 1441ceb0cf87SJohn Dyson #if !defined(NO_SWAPPING) 1442ceb0cf87SJohn Dyson /* 1443ceb0cf87SJohn Dyson * Idle process swapout -- run once per second. 1444ceb0cf87SJohn Dyson */ 1445ceb0cf87SJohn Dyson if (vm_swap_idle_enabled) { 1446ceb0cf87SJohn Dyson static long lsec; 1447227ee8a1SPoul-Henning Kamp if (time_second != lsec) { 144897824da3SAlan Cox vm_req_vmdaemon(VM_SWAP_IDLE); 1449227ee8a1SPoul-Henning Kamp lsec = time_second; 1450ceb0cf87SJohn Dyson } 1451ceb0cf87SJohn Dyson } 1452ceb0cf87SJohn Dyson #endif 1453ceb0cf87SJohn Dyson 14545663e6deSDavid Greenman /* 1455e92686d0SDavid Schultz * If we are critically low on one of RAM or swap and low on 1456e92686d0SDavid Schultz * the other, kill the largest process. However, we avoid 1457e92686d0SDavid Schultz * doing this on the first pass in order to give ourselves a 1458e92686d0SDavid Schultz * chance to flush out dirty vnode-backed pages and to allow 1459e92686d0SDavid Schultz * active pages to be moved to the inactive queue and reclaimed. 14602025d69bSKonstantin Belousov */ 1461449c2e92SKonstantin Belousov vm_pageout_mightbe_oom(vmd, pass); 14622025d69bSKonstantin Belousov } 14632025d69bSKonstantin Belousov 1464449c2e92SKonstantin Belousov static int vm_pageout_oom_vote; 1465449c2e92SKonstantin Belousov 1466449c2e92SKonstantin Belousov /* 1467449c2e92SKonstantin Belousov * The pagedaemon threads randlomly select one to perform the 1468449c2e92SKonstantin Belousov * OOM. Trying to kill processes before all pagedaemons 1469449c2e92SKonstantin Belousov * failed to reach free target is premature. 1470449c2e92SKonstantin Belousov */ 1471449c2e92SKonstantin Belousov static void 1472449c2e92SKonstantin Belousov vm_pageout_mightbe_oom(struct vm_domain *vmd, int pass) 1473449c2e92SKonstantin Belousov { 1474449c2e92SKonstantin Belousov int old_vote; 1475449c2e92SKonstantin Belousov 1476d9e23210SJeff Roberson if (pass <= 1 || !((swap_pager_avail < 64 && vm_page_count_min()) || 1477449c2e92SKonstantin Belousov (swap_pager_full && vm_paging_target() > 0))) { 1478449c2e92SKonstantin Belousov if (vmd->vmd_oom) { 1479449c2e92SKonstantin Belousov vmd->vmd_oom = FALSE; 1480449c2e92SKonstantin Belousov atomic_subtract_int(&vm_pageout_oom_vote, 1); 1481449c2e92SKonstantin Belousov } 1482449c2e92SKonstantin Belousov return; 1483449c2e92SKonstantin Belousov } 1484449c2e92SKonstantin Belousov 1485449c2e92SKonstantin Belousov if (vmd->vmd_oom) 1486449c2e92SKonstantin Belousov return; 1487449c2e92SKonstantin Belousov 1488449c2e92SKonstantin Belousov vmd->vmd_oom = TRUE; 1489449c2e92SKonstantin Belousov old_vote = atomic_fetchadd_int(&vm_pageout_oom_vote, 1); 1490449c2e92SKonstantin Belousov if (old_vote != vm_ndomains - 1) 1491449c2e92SKonstantin Belousov return; 1492449c2e92SKonstantin Belousov 1493449c2e92SKonstantin Belousov /* 1494449c2e92SKonstantin Belousov * The current pagedaemon thread is the last in the quorum to 1495449c2e92SKonstantin Belousov * start OOM. Initiate the selection and signaling of the 1496449c2e92SKonstantin Belousov * victim. 1497449c2e92SKonstantin Belousov */ 1498449c2e92SKonstantin Belousov vm_pageout_oom(VM_OOM_MEM); 1499449c2e92SKonstantin Belousov 1500449c2e92SKonstantin Belousov /* 1501449c2e92SKonstantin Belousov * After one round of OOM terror, recall our vote. On the 1502449c2e92SKonstantin Belousov * next pass, current pagedaemon would vote again if the low 1503449c2e92SKonstantin Belousov * memory condition is still there, due to vmd_oom being 1504449c2e92SKonstantin Belousov * false. 1505449c2e92SKonstantin Belousov */ 1506449c2e92SKonstantin Belousov vmd->vmd_oom = FALSE; 1507449c2e92SKonstantin Belousov atomic_subtract_int(&vm_pageout_oom_vote, 1); 1508449c2e92SKonstantin Belousov } 15092025d69bSKonstantin Belousov 15102025d69bSKonstantin Belousov void 15112025d69bSKonstantin Belousov vm_pageout_oom(int shortage) 15122025d69bSKonstantin Belousov { 15132025d69bSKonstantin Belousov struct proc *p, *bigproc; 15142025d69bSKonstantin Belousov vm_offset_t size, bigsize; 15152025d69bSKonstantin Belousov struct thread *td; 15166bed074cSKonstantin Belousov struct vmspace *vm; 15172025d69bSKonstantin Belousov 15182025d69bSKonstantin Belousov /* 15191c58e4e5SJohn Baldwin * We keep the process bigproc locked once we find it to keep anyone 15201c58e4e5SJohn Baldwin * from messing with it; however, there is a possibility of 15211c58e4e5SJohn Baldwin * deadlock if process B is bigproc and one of it's child processes 15221c58e4e5SJohn Baldwin * attempts to propagate a signal to B while we are waiting for A's 15231c58e4e5SJohn Baldwin * lock while walking this list. To avoid this, we don't block on 15241c58e4e5SJohn Baldwin * the process lock but just skip a process if it is already locked. 15255663e6deSDavid Greenman */ 15265663e6deSDavid Greenman bigproc = NULL; 15275663e6deSDavid Greenman bigsize = 0; 15281005a129SJohn Baldwin sx_slock(&allproc_lock); 1529e602ba25SJulian Elischer FOREACH_PROC_IN_SYSTEM(p) { 1530e602ba25SJulian Elischer int breakout; 1531dcbcd518SBruce Evans 153271943c3dSKonstantin Belousov PROC_LOCK(p); 153371943c3dSKonstantin Belousov 15341c58e4e5SJohn Baldwin /* 15353f1c4c4fSKonstantin Belousov * If this is a system, protected or killed process, skip it. 15365663e6deSDavid Greenman */ 153771943c3dSKonstantin Belousov if (p->p_state != PRS_NORMAL || (p->p_flag & (P_INEXEC | 153871943c3dSKonstantin Belousov P_PROTECTED | P_SYSTEM | P_WEXIT)) != 0 || 153971943c3dSKonstantin Belousov p->p_pid == 1 || P_KILLED(p) || 154071943c3dSKonstantin Belousov (p->p_pid < 48 && swap_pager_avail != 0)) { 15418606d880SJohn Baldwin PROC_UNLOCK(p); 15425663e6deSDavid Greenman continue; 15435663e6deSDavid Greenman } 15445663e6deSDavid Greenman /* 1545dcbcd518SBruce Evans * If the process is in a non-running type state, 1546e602ba25SJulian Elischer * don't touch it. Check all the threads individually. 15475663e6deSDavid Greenman */ 1548e602ba25SJulian Elischer breakout = 0; 1549e602ba25SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 1550982d11f8SJeff Roberson thread_lock(td); 155171fad9fdSJulian Elischer if (!TD_ON_RUNQ(td) && 155271fad9fdSJulian Elischer !TD_IS_RUNNING(td) && 1553f497cda2SEdward Tomasz Napierala !TD_IS_SLEEPING(td) && 1554f497cda2SEdward Tomasz Napierala !TD_IS_SUSPENDED(td)) { 1555982d11f8SJeff Roberson thread_unlock(td); 1556e602ba25SJulian Elischer breakout = 1; 1557e602ba25SJulian Elischer break; 1558e602ba25SJulian Elischer } 1559982d11f8SJeff Roberson thread_unlock(td); 1560e602ba25SJulian Elischer } 1561e602ba25SJulian Elischer if (breakout) { 15621c58e4e5SJohn Baldwin PROC_UNLOCK(p); 15635663e6deSDavid Greenman continue; 15645663e6deSDavid Greenman } 15655663e6deSDavid Greenman /* 15665663e6deSDavid Greenman * get the process size 15675663e6deSDavid Greenman */ 15686bed074cSKonstantin Belousov vm = vmspace_acquire_ref(p); 15696bed074cSKonstantin Belousov if (vm == NULL) { 15706bed074cSKonstantin Belousov PROC_UNLOCK(p); 15716bed074cSKonstantin Belousov continue; 15726bed074cSKonstantin Belousov } 157371943c3dSKonstantin Belousov _PHOLD(p); 15746bed074cSKonstantin Belousov if (!vm_map_trylock_read(&vm->vm_map)) { 157571943c3dSKonstantin Belousov _PRELE(p); 157672d97679SDavid Schultz PROC_UNLOCK(p); 157771943c3dSKonstantin Belousov vmspace_free(vm); 157872d97679SDavid Schultz continue; 157972d97679SDavid Schultz } 158071943c3dSKonstantin Belousov PROC_UNLOCK(p); 15817981aa24SKonstantin Belousov size = vmspace_swap_count(vm); 15826bed074cSKonstantin Belousov vm_map_unlock_read(&vm->vm_map); 15832025d69bSKonstantin Belousov if (shortage == VM_OOM_MEM) 15846bed074cSKonstantin Belousov size += vmspace_resident_count(vm); 15856bed074cSKonstantin Belousov vmspace_free(vm); 15865663e6deSDavid Greenman /* 15875663e6deSDavid Greenman * if the this process is bigger than the biggest one 15885663e6deSDavid Greenman * remember it. 15895663e6deSDavid Greenman */ 15905663e6deSDavid Greenman if (size > bigsize) { 15911c58e4e5SJohn Baldwin if (bigproc != NULL) 159271943c3dSKonstantin Belousov PRELE(bigproc); 15935663e6deSDavid Greenman bigproc = p; 15945663e6deSDavid Greenman bigsize = size; 159571943c3dSKonstantin Belousov } else { 159671943c3dSKonstantin Belousov PRELE(p); 159771943c3dSKonstantin Belousov } 15985663e6deSDavid Greenman } 15991005a129SJohn Baldwin sx_sunlock(&allproc_lock); 16005663e6deSDavid Greenman if (bigproc != NULL) { 16018311a2b8SWill Andrews if (vm_panic_on_oom != 0) 16028311a2b8SWill Andrews panic("out of swap space"); 160371943c3dSKonstantin Belousov PROC_LOCK(bigproc); 1604729b1e51SDavid Greenman killproc(bigproc, "out of swap space"); 1605fa885116SJulian Elischer sched_nice(bigproc, PRIO_MIN); 160671943c3dSKonstantin Belousov _PRELE(bigproc); 16071c58e4e5SJohn Baldwin PROC_UNLOCK(bigproc); 160844f1c916SBryan Drewery wakeup(&vm_cnt.v_free_count); 16095663e6deSDavid Greenman } 16105663e6deSDavid Greenman } 161126f9a767SRodney W. Grimes 1612449c2e92SKonstantin Belousov static void 1613449c2e92SKonstantin Belousov vm_pageout_worker(void *arg) 1614449c2e92SKonstantin Belousov { 1615449c2e92SKonstantin Belousov struct vm_domain *domain; 1616949c9186SKonstantin Belousov int domidx; 1617449c2e92SKonstantin Belousov 1618449c2e92SKonstantin Belousov domidx = (uintptr_t)arg; 1619449c2e92SKonstantin Belousov domain = &vm_dom[domidx]; 1620449c2e92SKonstantin Belousov 1621449c2e92SKonstantin Belousov /* 1622949c9186SKonstantin Belousov * XXXKIB It could be useful to bind pageout daemon threads to 1623949c9186SKonstantin Belousov * the cores belonging to the domain, from which vm_page_array 1624949c9186SKonstantin Belousov * is allocated. 1625449c2e92SKonstantin Belousov */ 1626449c2e92SKonstantin Belousov 1627449c2e92SKonstantin Belousov KASSERT(domain->vmd_segs != 0, ("domain without segments")); 162822cf98d1SAlan Cox domain->vmd_last_active_scan = ticks; 1629449c2e92SKonstantin Belousov vm_pageout_init_marker(&domain->vmd_marker, PQ_INACTIVE); 1630449c2e92SKonstantin Belousov 1631449c2e92SKonstantin Belousov /* 1632449c2e92SKonstantin Belousov * The pageout daemon worker is never done, so loop forever. 1633449c2e92SKonstantin Belousov */ 1634449c2e92SKonstantin Belousov while (TRUE) { 1635449c2e92SKonstantin Belousov /* 1636449c2e92SKonstantin Belousov * If we have enough free memory, wakeup waiters. Do 1637449c2e92SKonstantin Belousov * not clear vm_pages_needed until we reach our target, 1638449c2e92SKonstantin Belousov * otherwise we may be woken up over and over again and 1639449c2e92SKonstantin Belousov * waste a lot of cpu. 1640449c2e92SKonstantin Belousov */ 1641449c2e92SKonstantin Belousov mtx_lock(&vm_page_queue_free_mtx); 1642449c2e92SKonstantin Belousov if (vm_pages_needed && !vm_page_count_min()) { 1643449c2e92SKonstantin Belousov if (!vm_paging_needed()) 1644449c2e92SKonstantin Belousov vm_pages_needed = 0; 164544f1c916SBryan Drewery wakeup(&vm_cnt.v_free_count); 1646449c2e92SKonstantin Belousov } 1647449c2e92SKonstantin Belousov if (vm_pages_needed) { 1648449c2e92SKonstantin Belousov /* 1649449c2e92SKonstantin Belousov * Still not done, take a second pass without waiting 1650449c2e92SKonstantin Belousov * (unlimited dirty cleaning), otherwise sleep a bit 1651449c2e92SKonstantin Belousov * and try again. 1652449c2e92SKonstantin Belousov */ 1653449c2e92SKonstantin Belousov if (domain->vmd_pass > 1) 1654449c2e92SKonstantin Belousov msleep(&vm_pages_needed, 1655449c2e92SKonstantin Belousov &vm_page_queue_free_mtx, PVM, "psleep", 1656449c2e92SKonstantin Belousov hz / 2); 1657449c2e92SKonstantin Belousov } else { 1658449c2e92SKonstantin Belousov /* 1659d9e23210SJeff Roberson * Good enough, sleep until required to refresh 1660d9e23210SJeff Roberson * stats. 1661449c2e92SKonstantin Belousov */ 1662449c2e92SKonstantin Belousov domain->vmd_pass = 0; 1663d9e23210SJeff Roberson msleep(&vm_pages_needed, &vm_page_queue_free_mtx, 1664d9e23210SJeff Roberson PVM, "psleep", hz); 1665d9e23210SJeff Roberson 1666449c2e92SKonstantin Belousov } 1667d9e23210SJeff Roberson if (vm_pages_needed) { 166844f1c916SBryan Drewery vm_cnt.v_pdwakeups++; 1669d9e23210SJeff Roberson domain->vmd_pass++; 1670d9e23210SJeff Roberson } 1671449c2e92SKonstantin Belousov mtx_unlock(&vm_page_queue_free_mtx); 1672449c2e92SKonstantin Belousov vm_pageout_scan(domain, domain->vmd_pass); 1673449c2e92SKonstantin Belousov } 1674449c2e92SKonstantin Belousov } 1675449c2e92SKonstantin Belousov 1676df8bae1dSRodney W. Grimes /* 16774d19f4adSSteven Hartland * vm_pageout_init initialises basic pageout daemon settings. 1678df8bae1dSRodney W. Grimes */ 16792b14f991SJulian Elischer static void 16804d19f4adSSteven Hartland vm_pageout_init(void) 1681df8bae1dSRodney W. Grimes { 1682df8bae1dSRodney W. Grimes /* 1683df8bae1dSRodney W. Grimes * Initialize some paging parameters. 1684df8bae1dSRodney W. Grimes */ 168544f1c916SBryan Drewery vm_cnt.v_interrupt_free_min = 2; 168644f1c916SBryan Drewery if (vm_cnt.v_page_count < 2000) 1687f35329acSJohn Dyson vm_pageout_page_count = 8; 1688f6b04d2bSDavid Greenman 168945ae1d91SAlan Cox /* 169045ae1d91SAlan Cox * v_free_reserved needs to include enough for the largest 169145ae1d91SAlan Cox * swap pager structures plus enough for any pv_entry structs 169245ae1d91SAlan Cox * when paging. 169345ae1d91SAlan Cox */ 169444f1c916SBryan Drewery if (vm_cnt.v_page_count > 1024) 169544f1c916SBryan Drewery vm_cnt.v_free_min = 4 + (vm_cnt.v_page_count - 1024) / 200; 16962feb50bfSAttilio Rao else 169744f1c916SBryan Drewery vm_cnt.v_free_min = 4; 169844f1c916SBryan Drewery vm_cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE + 169944f1c916SBryan Drewery vm_cnt.v_interrupt_free_min; 170044f1c916SBryan Drewery vm_cnt.v_free_reserved = vm_pageout_page_count + 170144f1c916SBryan Drewery vm_cnt.v_pageout_free_min + (vm_cnt.v_page_count / 768); 170244f1c916SBryan Drewery vm_cnt.v_free_severe = vm_cnt.v_free_min / 2; 170344f1c916SBryan Drewery vm_cnt.v_free_target = 4 * vm_cnt.v_free_min + vm_cnt.v_free_reserved; 170444f1c916SBryan Drewery vm_cnt.v_free_min += vm_cnt.v_free_reserved; 170544f1c916SBryan Drewery vm_cnt.v_free_severe += vm_cnt.v_free_reserved; 170644f1c916SBryan Drewery vm_cnt.v_inactive_target = (3 * vm_cnt.v_free_target) / 2; 170744f1c916SBryan Drewery if (vm_cnt.v_inactive_target > vm_cnt.v_free_count / 3) 170844f1c916SBryan Drewery vm_cnt.v_inactive_target = vm_cnt.v_free_count / 3; 1709df8bae1dSRodney W. Grimes 1710d9e23210SJeff Roberson /* 1711d9e23210SJeff Roberson * Set the default wakeup threshold to be 10% above the minimum 1712d9e23210SJeff Roberson * page limit. This keeps the steady state out of shortfall. 1713d9e23210SJeff Roberson */ 171444f1c916SBryan Drewery vm_pageout_wakeup_thresh = (vm_cnt.v_free_min / 10) * 11; 1715d9e23210SJeff Roberson 1716d9e23210SJeff Roberson /* 1717d9e23210SJeff Roberson * Set interval in seconds for active scan. We want to visit each 1718c9612b2dSJeff Roberson * page at least once every ten minutes. This is to prevent worst 1719c9612b2dSJeff Roberson * case paging behaviors with stale active LRU. 1720d9e23210SJeff Roberson */ 1721d9e23210SJeff Roberson if (vm_pageout_update_period == 0) 1722c9612b2dSJeff Roberson vm_pageout_update_period = 600; 1723d9e23210SJeff Roberson 1724df8bae1dSRodney W. Grimes /* XXX does not really belong here */ 1725df8bae1dSRodney W. Grimes if (vm_page_max_wired == 0) 172644f1c916SBryan Drewery vm_page_max_wired = vm_cnt.v_free_count / 3; 17274d19f4adSSteven Hartland } 17284d19f4adSSteven Hartland 17294d19f4adSSteven Hartland /* 17304d19f4adSSteven Hartland * vm_pageout is the high level pageout daemon. 17314d19f4adSSteven Hartland */ 17324d19f4adSSteven Hartland static void 17334d19f4adSSteven Hartland vm_pageout(void) 17344d19f4adSSteven Hartland { 173544ec2b63SKonstantin Belousov int error; 17364d19f4adSSteven Hartland #if MAXMEMDOM > 1 173744ec2b63SKonstantin Belousov int i; 17384d19f4adSSteven Hartland #endif 1739df8bae1dSRodney W. Grimes 174024a1cce3SDavid Greenman swap_pager_swap_init(); 1741449c2e92SKonstantin Belousov #if MAXMEMDOM > 1 1742449c2e92SKonstantin Belousov for (i = 1; i < vm_ndomains; i++) { 1743449c2e92SKonstantin Belousov error = kthread_add(vm_pageout_worker, (void *)(uintptr_t)i, 1744449c2e92SKonstantin Belousov curproc, NULL, 0, 0, "dom%d", i); 1745449c2e92SKonstantin Belousov if (error != 0) { 1746449c2e92SKonstantin Belousov panic("starting pageout for domain %d, error %d\n", 1747449c2e92SKonstantin Belousov i, error); 1748dc2efb27SJohn Dyson } 1749f919ebdeSDavid Greenman } 1750449c2e92SKonstantin Belousov #endif 175144ec2b63SKonstantin Belousov error = kthread_add(uma_reclaim_worker, NULL, curproc, NULL, 175244ec2b63SKonstantin Belousov 0, 0, "uma"); 175344ec2b63SKonstantin Belousov if (error != 0) 175444ec2b63SKonstantin Belousov panic("starting uma_reclaim helper, error %d\n", error); 1755d395270dSDimitry Andric vm_pageout_worker((void *)(uintptr_t)0); 1756df8bae1dSRodney W. Grimes } 175726f9a767SRodney W. Grimes 17586b4b77adSAlan Cox /* 1759e9f995d8SAlan Cox * Unless the free page queue lock is held by the caller, this function 17606b4b77adSAlan Cox * should be regarded as advisory. Specifically, the caller should 176144f1c916SBryan Drewery * not msleep() on &vm_cnt.v_free_count following this function unless 1762e9f995d8SAlan Cox * the free page queue lock is held until the msleep() is performed. 17636b4b77adSAlan Cox */ 1764e0c5a895SJohn Dyson void 17654a365329SAndrey Zonov pagedaemon_wakeup(void) 1766e0c5a895SJohn Dyson { 1767a1c0a785SAlan Cox 1768b40ce416SJulian Elischer if (!vm_pages_needed && curthread->td_proc != pageproc) { 1769a1c0a785SAlan Cox vm_pages_needed = 1; 1770e0c5a895SJohn Dyson wakeup(&vm_pages_needed); 1771e0c5a895SJohn Dyson } 1772e0c5a895SJohn Dyson } 1773e0c5a895SJohn Dyson 177438efa82bSJohn Dyson #if !defined(NO_SWAPPING) 17755afce282SDavid Greenman static void 177697824da3SAlan Cox vm_req_vmdaemon(int req) 17775afce282SDavid Greenman { 17785afce282SDavid Greenman static int lastrun = 0; 17795afce282SDavid Greenman 178097824da3SAlan Cox mtx_lock(&vm_daemon_mtx); 178197824da3SAlan Cox vm_pageout_req_swapout |= req; 1782b18bfc3dSJohn Dyson if ((ticks > (lastrun + hz)) || (ticks < lastrun)) { 17835afce282SDavid Greenman wakeup(&vm_daemon_needed); 17845afce282SDavid Greenman lastrun = ticks; 17855afce282SDavid Greenman } 178697824da3SAlan Cox mtx_unlock(&vm_daemon_mtx); 17875afce282SDavid Greenman } 17885afce282SDavid Greenman 17892b14f991SJulian Elischer static void 17904a365329SAndrey Zonov vm_daemon(void) 17910d94caffSDavid Greenman { 179291d5354aSJohn Baldwin struct rlimit rsslim; 1793dcbcd518SBruce Evans struct proc *p; 1794dcbcd518SBruce Evans struct thread *td; 17956bed074cSKonstantin Belousov struct vmspace *vm; 1796099e7e95SEdward Tomasz Napierala int breakout, swapout_flags, tryagain, attempts; 1797afcc55f3SEdward Tomasz Napierala #ifdef RACCT 1798099e7e95SEdward Tomasz Napierala uint64_t rsize, ravailable; 1799afcc55f3SEdward Tomasz Napierala #endif 18000d94caffSDavid Greenman 18012fe6e4d7SDavid Greenman while (TRUE) { 180297824da3SAlan Cox mtx_lock(&vm_daemon_mtx); 18034b5c9cf6SEdward Tomasz Napierala msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", 1804099e7e95SEdward Tomasz Napierala #ifdef RACCT 18054b5c9cf6SEdward Tomasz Napierala racct_enable ? hz : 0 1806099e7e95SEdward Tomasz Napierala #else 18074b5c9cf6SEdward Tomasz Napierala 0 1808099e7e95SEdward Tomasz Napierala #endif 18094b5c9cf6SEdward Tomasz Napierala ); 181097824da3SAlan Cox swapout_flags = vm_pageout_req_swapout; 18114c1f8ee9SDavid Greenman vm_pageout_req_swapout = 0; 181297824da3SAlan Cox mtx_unlock(&vm_daemon_mtx); 181397824da3SAlan Cox if (swapout_flags) 181497824da3SAlan Cox swapout_procs(swapout_flags); 181597824da3SAlan Cox 18162fe6e4d7SDavid Greenman /* 18170d94caffSDavid Greenman * scan the processes for exceeding their rlimits or if 18180d94caffSDavid Greenman * process is swapped out -- deactivate pages 18192fe6e4d7SDavid Greenman */ 1820099e7e95SEdward Tomasz Napierala tryagain = 0; 1821099e7e95SEdward Tomasz Napierala attempts = 0; 1822099e7e95SEdward Tomasz Napierala again: 1823099e7e95SEdward Tomasz Napierala attempts++; 18241005a129SJohn Baldwin sx_slock(&allproc_lock); 1825f67af5c9SXin LI FOREACH_PROC_IN_SYSTEM(p) { 1826fe2144fdSLuoqi Chen vm_pindex_t limit, size; 18272fe6e4d7SDavid Greenman 18282fe6e4d7SDavid Greenman /* 18292fe6e4d7SDavid Greenman * if this is a system process or if we have already 18302fe6e4d7SDavid Greenman * looked at this process, skip it. 18312fe6e4d7SDavid Greenman */ 1832897ecacdSJohn Baldwin PROC_LOCK(p); 18338e6fa660SJohn Baldwin if (p->p_state != PRS_NORMAL || 18348e6fa660SJohn Baldwin p->p_flag & (P_INEXEC | P_SYSTEM | P_WEXIT)) { 1835897ecacdSJohn Baldwin PROC_UNLOCK(p); 18362fe6e4d7SDavid Greenman continue; 18372fe6e4d7SDavid Greenman } 18382fe6e4d7SDavid Greenman /* 18392fe6e4d7SDavid Greenman * if the process is in a non-running type state, 18402fe6e4d7SDavid Greenman * don't touch it. 18412fe6e4d7SDavid Greenman */ 1842e602ba25SJulian Elischer breakout = 0; 1843e602ba25SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 1844982d11f8SJeff Roberson thread_lock(td); 184571fad9fdSJulian Elischer if (!TD_ON_RUNQ(td) && 184671fad9fdSJulian Elischer !TD_IS_RUNNING(td) && 1847f497cda2SEdward Tomasz Napierala !TD_IS_SLEEPING(td) && 1848f497cda2SEdward Tomasz Napierala !TD_IS_SUSPENDED(td)) { 1849982d11f8SJeff Roberson thread_unlock(td); 1850e602ba25SJulian Elischer breakout = 1; 1851e602ba25SJulian Elischer break; 1852e602ba25SJulian Elischer } 1853982d11f8SJeff Roberson thread_unlock(td); 1854e602ba25SJulian Elischer } 1855897ecacdSJohn Baldwin if (breakout) { 1856897ecacdSJohn Baldwin PROC_UNLOCK(p); 18572fe6e4d7SDavid Greenman continue; 18582fe6e4d7SDavid Greenman } 18592fe6e4d7SDavid Greenman /* 18602fe6e4d7SDavid Greenman * get a limit 18612fe6e4d7SDavid Greenman */ 1862f6f6d240SMateusz Guzik lim_rlimit_proc(p, RLIMIT_RSS, &rsslim); 1863fe2144fdSLuoqi Chen limit = OFF_TO_IDX( 186491d5354aSJohn Baldwin qmin(rsslim.rlim_cur, rsslim.rlim_max)); 18652fe6e4d7SDavid Greenman 18662fe6e4d7SDavid Greenman /* 18670d94caffSDavid Greenman * let processes that are swapped out really be 18680d94caffSDavid Greenman * swapped out set the limit to nothing (will force a 18690d94caffSDavid Greenman * swap-out.) 18702fe6e4d7SDavid Greenman */ 1871b61ce5b0SJeff Roberson if ((p->p_flag & P_INMEM) == 0) 18720d94caffSDavid Greenman limit = 0; /* XXX */ 18736bed074cSKonstantin Belousov vm = vmspace_acquire_ref(p); 1874897ecacdSJohn Baldwin PROC_UNLOCK(p); 18756bed074cSKonstantin Belousov if (vm == NULL) 18766bed074cSKonstantin Belousov continue; 18772fe6e4d7SDavid Greenman 18786bed074cSKonstantin Belousov size = vmspace_resident_count(vm); 1879a406d8c3SEdward Tomasz Napierala if (size >= limit) { 1880fe2144fdSLuoqi Chen vm_pageout_map_deactivate_pages( 18816bed074cSKonstantin Belousov &vm->vm_map, limit); 18822fe6e4d7SDavid Greenman } 1883afcc55f3SEdward Tomasz Napierala #ifdef RACCT 18844b5c9cf6SEdward Tomasz Napierala if (racct_enable) { 1885099e7e95SEdward Tomasz Napierala rsize = IDX_TO_OFF(size); 1886099e7e95SEdward Tomasz Napierala PROC_LOCK(p); 1887099e7e95SEdward Tomasz Napierala racct_set(p, RACCT_RSS, rsize); 1888099e7e95SEdward Tomasz Napierala ravailable = racct_get_available(p, RACCT_RSS); 1889099e7e95SEdward Tomasz Napierala PROC_UNLOCK(p); 1890099e7e95SEdward Tomasz Napierala if (rsize > ravailable) { 1891099e7e95SEdward Tomasz Napierala /* 18924b5c9cf6SEdward Tomasz Napierala * Don't be overly aggressive; this 18934b5c9cf6SEdward Tomasz Napierala * might be an innocent process, 18944b5c9cf6SEdward Tomasz Napierala * and the limit could've been exceeded 18954b5c9cf6SEdward Tomasz Napierala * by some memory hog. Don't try 18964b5c9cf6SEdward Tomasz Napierala * to deactivate more than 1/4th 18974b5c9cf6SEdward Tomasz Napierala * of process' resident set size. 1898099e7e95SEdward Tomasz Napierala */ 1899099e7e95SEdward Tomasz Napierala if (attempts <= 8) { 19004b5c9cf6SEdward Tomasz Napierala if (ravailable < rsize - 19014b5c9cf6SEdward Tomasz Napierala (rsize / 4)) { 19024b5c9cf6SEdward Tomasz Napierala ravailable = rsize - 19034b5c9cf6SEdward Tomasz Napierala (rsize / 4); 19044b5c9cf6SEdward Tomasz Napierala } 1905099e7e95SEdward Tomasz Napierala } 1906099e7e95SEdward Tomasz Napierala vm_pageout_map_deactivate_pages( 19074b5c9cf6SEdward Tomasz Napierala &vm->vm_map, 19084b5c9cf6SEdward Tomasz Napierala OFF_TO_IDX(ravailable)); 1909099e7e95SEdward Tomasz Napierala /* Update RSS usage after paging out. */ 1910099e7e95SEdward Tomasz Napierala size = vmspace_resident_count(vm); 1911099e7e95SEdward Tomasz Napierala rsize = IDX_TO_OFF(size); 1912099e7e95SEdward Tomasz Napierala PROC_LOCK(p); 1913099e7e95SEdward Tomasz Napierala racct_set(p, RACCT_RSS, rsize); 1914099e7e95SEdward Tomasz Napierala PROC_UNLOCK(p); 1915099e7e95SEdward Tomasz Napierala if (rsize > ravailable) 1916099e7e95SEdward Tomasz Napierala tryagain = 1; 1917099e7e95SEdward Tomasz Napierala } 19184b5c9cf6SEdward Tomasz Napierala } 1919afcc55f3SEdward Tomasz Napierala #endif 19206bed074cSKonstantin Belousov vmspace_free(vm); 19212fe6e4d7SDavid Greenman } 19221005a129SJohn Baldwin sx_sunlock(&allproc_lock); 1923099e7e95SEdward Tomasz Napierala if (tryagain != 0 && attempts <= 10) 1924099e7e95SEdward Tomasz Napierala goto again; 192524a1cce3SDavid Greenman } 19262fe6e4d7SDavid Greenman } 1927a1287949SEivind Eklund #endif /* !defined(NO_SWAPPING) */ 1928