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> 96f6b04d2bSDavid Greenman #include <sys/vnode.h> 97efeaf95aSDavid Greenman #include <sys/vmmeter.h> 9889f6b863SAttilio Rao #include <sys/rwlock.h> 991005a129SJohn Baldwin #include <sys/sx.h> 10038efa82bSJohn Dyson #include <sys/sysctl.h> 101df8bae1dSRodney W. Grimes 102df8bae1dSRodney W. Grimes #include <vm/vm.h> 103efeaf95aSDavid Greenman #include <vm/vm_param.h> 104efeaf95aSDavid Greenman #include <vm/vm_object.h> 105df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 106efeaf95aSDavid Greenman #include <vm/vm_map.h> 107df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h> 10824a1cce3SDavid Greenman #include <vm/vm_pager.h> 109449c2e92SKonstantin Belousov #include <vm/vm_phys.h> 11005f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h> 111efeaf95aSDavid Greenman #include <vm/vm_extern.h> 112670d17b5SJeff Roberson #include <vm/uma.h> 113df8bae1dSRodney W. Grimes 1142b14f991SJulian Elischer /* 1152b14f991SJulian Elischer * System initialization 1162b14f991SJulian Elischer */ 1172b14f991SJulian Elischer 1182b14f991SJulian Elischer /* the kernel process "vm_pageout"*/ 11911caded3SAlfred Perlstein static void vm_pageout(void); 1204d19f4adSSteven Hartland static void vm_pageout_init(void); 121*34d8b7eaSJeff Roberson static int vm_pageout_clean(vm_page_t m); 122*34d8b7eaSJeff Roberson static int vm_pageout_cluster(vm_page_t m); 123449c2e92SKonstantin Belousov static void vm_pageout_scan(struct vm_domain *vmd, int pass); 124449c2e92SKonstantin Belousov static void vm_pageout_mightbe_oom(struct vm_domain *vmd, int pass); 12545ae1d91SAlan Cox 1264d19f4adSSteven Hartland SYSINIT(pagedaemon_init, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, vm_pageout_init, 1274d19f4adSSteven Hartland NULL); 1284d19f4adSSteven Hartland 1292b14f991SJulian Elischer struct proc *pageproc; 1302b14f991SJulian Elischer 1312b14f991SJulian Elischer static struct kproc_desc page_kp = { 1322b14f991SJulian Elischer "pagedaemon", 1332b14f991SJulian Elischer vm_pageout, 1342b14f991SJulian Elischer &pageproc 1352b14f991SJulian Elischer }; 1364d19f4adSSteven Hartland SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_SECOND, kproc_start, 137237fdd78SRobert Watson &page_kp); 1382b14f991SJulian Elischer 13914a0d74eSSteven Hartland SDT_PROVIDER_DEFINE(vm); 14014a0d74eSSteven Hartland SDT_PROBE_DEFINE(vm, , , vm__lowmem_cache); 14114a0d74eSSteven Hartland SDT_PROBE_DEFINE(vm, , , vm__lowmem_scan); 14214a0d74eSSteven Hartland 14338efa82bSJohn Dyson #if !defined(NO_SWAPPING) 1442b14f991SJulian Elischer /* the kernel process "vm_daemon"*/ 14511caded3SAlfred Perlstein static void vm_daemon(void); 146f708ef1bSPoul-Henning Kamp static struct proc *vmproc; 1472b14f991SJulian Elischer 1482b14f991SJulian Elischer static struct kproc_desc vm_kp = { 1492b14f991SJulian Elischer "vmdaemon", 1502b14f991SJulian Elischer vm_daemon, 1512b14f991SJulian Elischer &vmproc 1522b14f991SJulian Elischer }; 153237fdd78SRobert Watson SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp); 15438efa82bSJohn Dyson #endif 1552b14f991SJulian Elischer 1562b14f991SJulian Elischer 1578b245767SAlan Cox int vm_pages_needed; /* Event on which pageout daemon sleeps */ 1588b245767SAlan Cox int vm_pageout_deficit; /* Estimated number of pages deficit */ 1598b245767SAlan Cox int vm_pageout_pages_needed; /* flag saying that the pageout daemon needs pages */ 160d9e23210SJeff Roberson int vm_pageout_wakeup_thresh; 16126f9a767SRodney W. Grimes 16238efa82bSJohn Dyson #if !defined(NO_SWAPPING) 163f708ef1bSPoul-Henning Kamp static int vm_pageout_req_swapout; /* XXX */ 164f708ef1bSPoul-Henning Kamp static int vm_daemon_needed; 16597824da3SAlan Cox static struct mtx vm_daemon_mtx; 16697824da3SAlan Cox /* Allow for use by vm_pageout before vm_daemon is initialized. */ 16797824da3SAlan Cox MTX_SYSINIT(vm_daemon, &vm_daemon_mtx, "vm daemon", MTX_DEF); 16838efa82bSJohn Dyson #endif 1692b6b0df7SMatthew Dillon static int vm_max_launder = 32; 170d9e23210SJeff Roberson static int vm_pageout_update_period; 1714a365329SAndrey Zonov static int defer_swap_pageouts; 1724a365329SAndrey Zonov static int disable_swap_pageouts; 173c9612b2dSJeff Roberson static int lowmem_period = 10; 174c9612b2dSJeff Roberson static int lowmem_ticks; 17570111b90SJohn Dyson 17638efa82bSJohn Dyson #if defined(NO_SWAPPING) 177303b270bSEivind Eklund static int vm_swap_enabled = 0; 178303b270bSEivind Eklund static int vm_swap_idle_enabled = 0; 17938efa82bSJohn Dyson #else 180303b270bSEivind Eklund static int vm_swap_enabled = 1; 181303b270bSEivind Eklund static int vm_swap_idle_enabled = 0; 18238efa82bSJohn Dyson #endif 18338efa82bSJohn Dyson 1848311a2b8SWill Andrews static int vm_panic_on_oom = 0; 1858311a2b8SWill Andrews 1868311a2b8SWill Andrews SYSCTL_INT(_vm, OID_AUTO, panic_on_oom, 1878311a2b8SWill Andrews CTLFLAG_RWTUN, &vm_panic_on_oom, 0, 1888311a2b8SWill Andrews "panic on out of memory instead of killing the largest process"); 1898311a2b8SWill Andrews 190d9e23210SJeff Roberson SYSCTL_INT(_vm, OID_AUTO, pageout_wakeup_thresh, 191d9e23210SJeff Roberson CTLFLAG_RW, &vm_pageout_wakeup_thresh, 0, 192d9e23210SJeff Roberson "free page threshold for waking up the pageout daemon"); 193d9e23210SJeff Roberson 1942b6b0df7SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, max_launder, 1952b6b0df7SMatthew Dillon CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout"); 19638efa82bSJohn Dyson 197d9e23210SJeff Roberson SYSCTL_INT(_vm, OID_AUTO, pageout_update_period, 198d9e23210SJeff Roberson CTLFLAG_RW, &vm_pageout_update_period, 0, 199d9e23210SJeff Roberson "Maximum active LRU update period"); 20053636869SAndrey Zonov 201c9612b2dSJeff Roberson SYSCTL_INT(_vm, OID_AUTO, lowmem_period, CTLFLAG_RW, &lowmem_period, 0, 202c9612b2dSJeff Roberson "Low memory callback period"); 203c9612b2dSJeff Roberson 20438efa82bSJohn Dyson #if defined(NO_SWAPPING) 205ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 2066bd9cb1cSTom Rhodes CTLFLAG_RD, &vm_swap_enabled, 0, "Enable entire process swapout"); 207ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 2086bd9cb1cSTom Rhodes CTLFLAG_RD, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria"); 20938efa82bSJohn Dyson #else 210ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 211b0359e2cSPeter Wemm CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout"); 212ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 213b0359e2cSPeter Wemm CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria"); 21438efa82bSJohn Dyson #endif 21526f9a767SRodney W. Grimes 216ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts, 217b0359e2cSPeter Wemm CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem"); 21812ac6a1dSJohn Dyson 219ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts, 220b0359e2cSPeter Wemm CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages"); 22112ac6a1dSJohn Dyson 22223b59018SMatthew Dillon static int pageout_lock_miss; 22323b59018SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss, 22423b59018SMatthew Dillon CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout"); 22523b59018SMatthew Dillon 226ffc82b0aSJohn Dyson #define VM_PAGEOUT_PAGE_COUNT 16 227bbc0ec52SDavid Greenman int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT; 228df8bae1dSRodney W. Grimes 229c3cb3e12SDavid Greenman int vm_page_max_wired; /* XXX max # of wired pages system-wide */ 2305dfc2870SAlan Cox SYSCTL_INT(_vm, OID_AUTO, max_wired, 2315dfc2870SAlan Cox CTLFLAG_RW, &vm_page_max_wired, 0, "System-wide limit to wired page count"); 232df8bae1dSRodney W. Grimes 23385eeca35SAlan Cox static boolean_t vm_pageout_fallback_object_lock(vm_page_t, vm_page_t *); 234449c2e92SKonstantin Belousov static boolean_t vm_pageout_launder(struct vm_pagequeue *pq, int, vm_paddr_t, 235449c2e92SKonstantin Belousov vm_paddr_t); 23638efa82bSJohn Dyson #if !defined(NO_SWAPPING) 237ecf6279fSAlan Cox static void vm_pageout_map_deactivate_pages(vm_map_t, long); 238ecf6279fSAlan Cox static void vm_pageout_object_deactivate_pages(pmap_t, vm_object_t, long); 23997824da3SAlan Cox static void vm_req_vmdaemon(int req); 24038efa82bSJohn Dyson #endif 24185eeca35SAlan Cox static boolean_t vm_pageout_page_lock(vm_page_t, vm_page_t *); 242cd41fc12SDavid Greenman 243a8229fa3SAlan Cox /* 244a8229fa3SAlan Cox * Initialize a dummy page for marking the caller's place in the specified 245a8229fa3SAlan Cox * paging queue. In principle, this function only needs to set the flag 246c7aebda8SAttilio Rao * PG_MARKER. Nonetheless, it wirte busies and initializes the hold count 247c7aebda8SAttilio Rao * to one as safety precautions. 248a8229fa3SAlan Cox */ 2498c616246SKonstantin Belousov static void 2508c616246SKonstantin Belousov vm_pageout_init_marker(vm_page_t marker, u_short queue) 2518c616246SKonstantin Belousov { 2528c616246SKonstantin Belousov 2538c616246SKonstantin Belousov bzero(marker, sizeof(*marker)); 254a8229fa3SAlan Cox marker->flags = PG_MARKER; 255c7aebda8SAttilio Rao marker->busy_lock = VPB_SINGLE_EXCLUSIVER; 2568c616246SKonstantin Belousov marker->queue = queue; 257a8229fa3SAlan Cox marker->hold_count = 1; 2588c616246SKonstantin Belousov } 2598c616246SKonstantin Belousov 26026f9a767SRodney W. Grimes /* 2618dbca793STor Egge * vm_pageout_fallback_object_lock: 2628dbca793STor Egge * 26389f6b863SAttilio Rao * Lock vm object currently associated with `m'. VM_OBJECT_TRYWLOCK is 2648dbca793STor Egge * known to have failed and page queue must be either PQ_ACTIVE or 2658dbca793STor Egge * PQ_INACTIVE. To avoid lock order violation, unlock the page queues 2668dbca793STor Egge * while locking the vm object. Use marker page to detect page queue 2678dbca793STor Egge * changes and maintain notion of next page on page queue. Return 2688dbca793STor Egge * TRUE if no changes were detected, FALSE otherwise. vm object is 2698dbca793STor Egge * locked on return. 2708dbca793STor Egge * 2718dbca793STor Egge * This function depends on both the lock portion of struct vm_object 2728dbca793STor Egge * and normal struct vm_page being type stable. 2738dbca793STor Egge */ 27485eeca35SAlan Cox static boolean_t 2758dbca793STor Egge vm_pageout_fallback_object_lock(vm_page_t m, vm_page_t *next) 2768dbca793STor Egge { 2778dbca793STor Egge struct vm_page marker; 2788d220203SAlan Cox struct vm_pagequeue *pq; 2798dbca793STor Egge boolean_t unchanged; 2808dbca793STor Egge u_short queue; 2818dbca793STor Egge vm_object_t object; 2828dbca793STor Egge 2838dbca793STor Egge queue = m->queue; 2848c616246SKonstantin Belousov vm_pageout_init_marker(&marker, queue); 285449c2e92SKonstantin Belousov pq = vm_page_pagequeue(m); 2868dbca793STor Egge object = m->object; 2878dbca793STor Egge 288c325e866SKonstantin Belousov TAILQ_INSERT_AFTER(&pq->pq_pl, m, &marker, plinks.q); 2898d220203SAlan Cox vm_pagequeue_unlock(pq); 2902965a453SKip Macy vm_page_unlock(m); 29189f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 2922965a453SKip Macy vm_page_lock(m); 2938d220203SAlan Cox vm_pagequeue_lock(pq); 2948dbca793STor Egge 2958dbca793STor Egge /* Page queue might have changed. */ 296c325e866SKonstantin Belousov *next = TAILQ_NEXT(&marker, plinks.q); 2978dbca793STor Egge unchanged = (m->queue == queue && 2988dbca793STor Egge m->object == object && 299c325e866SKonstantin Belousov &marker == TAILQ_NEXT(m, plinks.q)); 300c325e866SKonstantin Belousov TAILQ_REMOVE(&pq->pq_pl, &marker, plinks.q); 3018dbca793STor Egge return (unchanged); 3028dbca793STor Egge } 3038dbca793STor Egge 3048dbca793STor Egge /* 3058c616246SKonstantin Belousov * Lock the page while holding the page queue lock. Use marker page 3068c616246SKonstantin Belousov * to detect page queue changes and maintain notion of next page on 3078c616246SKonstantin Belousov * page queue. Return TRUE if no changes were detected, FALSE 3088c616246SKonstantin Belousov * otherwise. The page is locked on return. The page queue lock might 3098c616246SKonstantin Belousov * be dropped and reacquired. 3108c616246SKonstantin Belousov * 3118c616246SKonstantin Belousov * This function depends on normal struct vm_page being type stable. 3128c616246SKonstantin Belousov */ 31385eeca35SAlan Cox static boolean_t 3148c616246SKonstantin Belousov vm_pageout_page_lock(vm_page_t m, vm_page_t *next) 3158c616246SKonstantin Belousov { 3168c616246SKonstantin Belousov struct vm_page marker; 3178d220203SAlan Cox struct vm_pagequeue *pq; 3188c616246SKonstantin Belousov boolean_t unchanged; 3198c616246SKonstantin Belousov u_short queue; 3208c616246SKonstantin Belousov 3218c616246SKonstantin Belousov vm_page_lock_assert(m, MA_NOTOWNED); 3228c616246SKonstantin Belousov if (vm_page_trylock(m)) 3238c616246SKonstantin Belousov return (TRUE); 3248c616246SKonstantin Belousov 3258c616246SKonstantin Belousov queue = m->queue; 3268c616246SKonstantin Belousov vm_pageout_init_marker(&marker, queue); 327449c2e92SKonstantin Belousov pq = vm_page_pagequeue(m); 3288c616246SKonstantin Belousov 329c325e866SKonstantin Belousov TAILQ_INSERT_AFTER(&pq->pq_pl, m, &marker, plinks.q); 3308d220203SAlan Cox vm_pagequeue_unlock(pq); 3318c616246SKonstantin Belousov vm_page_lock(m); 3328d220203SAlan Cox vm_pagequeue_lock(pq); 3338c616246SKonstantin Belousov 3348c616246SKonstantin Belousov /* Page queue might have changed. */ 335c325e866SKonstantin Belousov *next = TAILQ_NEXT(&marker, plinks.q); 336c325e866SKonstantin Belousov unchanged = (m->queue == queue && &marker == TAILQ_NEXT(m, plinks.q)); 337c325e866SKonstantin Belousov TAILQ_REMOVE(&pq->pq_pl, &marker, plinks.q); 3388c616246SKonstantin Belousov return (unchanged); 3398c616246SKonstantin Belousov } 3408c616246SKonstantin Belousov 3418c616246SKonstantin Belousov /* 34226f9a767SRodney W. Grimes * vm_pageout_clean: 34324a1cce3SDavid Greenman * 3440d94caffSDavid Greenman * Clean the page and remove it from the laundry. 34526f9a767SRodney W. Grimes * 3460d94caffSDavid Greenman * We set the busy bit to cause potential page faults on this page to 3471c7c3c6aSMatthew Dillon * block. Note the careful timing, however, the busy bit isn't set till 3481c7c3c6aSMatthew Dillon * late and we cannot do anything that will mess with the page. 34926f9a767SRodney W. Grimes */ 3503af76890SPoul-Henning Kamp static int 351*34d8b7eaSJeff Roberson vm_pageout_cluster(vm_page_t m) 35224a1cce3SDavid Greenman { 35354d92145SMatthew Dillon vm_object_t object; 35491b4f427SAlan Cox vm_page_t mc[2*vm_pageout_page_count], pb, ps; 3553562af12SAlan Cox int pageout_count; 35690ecac61SMatthew Dillon int ib, is, page_base; 357a316d390SJohn Dyson vm_pindex_t pindex = m->pindex; 35826f9a767SRodney W. Grimes 35995976f3fSAlan Cox vm_page_lock_assert(m, MA_OWNED); 36017f6a17bSAlan Cox object = m->object; 36189f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 3620cddd8f0SMatthew Dillon 36326f9a767SRodney W. Grimes /* 3641c7c3c6aSMatthew Dillon * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP 3651c7c3c6aSMatthew Dillon * with the new swapper, but we could have serious problems paging 3661c7c3c6aSMatthew Dillon * out other object types if there is insufficient memory. 3671c7c3c6aSMatthew Dillon * 3681c7c3c6aSMatthew Dillon * Unfortunately, checking free memory here is far too late, so the 3691c7c3c6aSMatthew Dillon * check has been moved up a procedural level. 3701c7c3c6aSMatthew Dillon */ 3711c7c3c6aSMatthew Dillon 37224a1cce3SDavid Greenman /* 3739e897b1bSAlan Cox * Can't clean the page if it's busy or held. 37424a1cce3SDavid Greenman */ 375c7aebda8SAttilio Rao vm_page_assert_unbusied(m); 37695976f3fSAlan Cox KASSERT(m->hold_count == 0, ("vm_pageout_clean: page %p is held", m)); 37717f6a17bSAlan Cox vm_page_unlock(m); 3780d94caffSDavid Greenman 37991b4f427SAlan Cox mc[vm_pageout_page_count] = pb = ps = m; 38026f9a767SRodney W. Grimes pageout_count = 1; 381f35329acSJohn Dyson page_base = vm_pageout_page_count; 38290ecac61SMatthew Dillon ib = 1; 38390ecac61SMatthew Dillon is = 1; 38490ecac61SMatthew Dillon 38524a1cce3SDavid Greenman /* 38624a1cce3SDavid Greenman * Scan object for clusterable pages. 38724a1cce3SDavid Greenman * 38824a1cce3SDavid Greenman * We can cluster ONLY if: ->> the page is NOT 38924a1cce3SDavid Greenman * clean, wired, busy, held, or mapped into a 39024a1cce3SDavid Greenman * buffer, and one of the following: 39124a1cce3SDavid Greenman * 1) The page is inactive, or a seldom used 39224a1cce3SDavid Greenman * active page. 39324a1cce3SDavid Greenman * -or- 39424a1cce3SDavid Greenman * 2) we force the issue. 39590ecac61SMatthew Dillon * 39690ecac61SMatthew Dillon * During heavy mmap/modification loads the pageout 39790ecac61SMatthew Dillon * daemon can really fragment the underlying file 39890ecac61SMatthew Dillon * due to flushing pages out of order and not trying 39990ecac61SMatthew Dillon * align the clusters (which leave sporatic out-of-order 40090ecac61SMatthew Dillon * holes). To solve this problem we do the reverse scan 40190ecac61SMatthew Dillon * first and attempt to align our cluster, then do a 40290ecac61SMatthew Dillon * forward scan if room remains. 40324a1cce3SDavid Greenman */ 40490ecac61SMatthew Dillon more: 40590ecac61SMatthew Dillon while (ib && pageout_count < vm_pageout_page_count) { 40624a1cce3SDavid Greenman vm_page_t p; 407f6b04d2bSDavid Greenman 40890ecac61SMatthew Dillon if (ib > pindex) { 40990ecac61SMatthew Dillon ib = 0; 41090ecac61SMatthew Dillon break; 411f6b04d2bSDavid Greenman } 41290ecac61SMatthew Dillon 413c7aebda8SAttilio Rao if ((p = vm_page_prev(pb)) == NULL || vm_page_busied(p)) { 41490ecac61SMatthew Dillon ib = 0; 41590ecac61SMatthew Dillon break; 416f6b04d2bSDavid Greenman } 4172965a453SKip Macy vm_page_lock(p); 41824a1cce3SDavid Greenman vm_page_test_dirty(p); 41926f4eea5SAlan Cox if (p->dirty == 0 || 42090ecac61SMatthew Dillon p->queue != PQ_INACTIVE || 42157601bcbSMatthew Dillon p->hold_count != 0) { /* may be undergoing I/O */ 4222965a453SKip Macy vm_page_unlock(p); 42390ecac61SMatthew Dillon ib = 0; 42424a1cce3SDavid Greenman break; 425f6b04d2bSDavid Greenman } 4262965a453SKip Macy vm_page_unlock(p); 42791b4f427SAlan Cox mc[--page_base] = pb = p; 42890ecac61SMatthew Dillon ++pageout_count; 42990ecac61SMatthew Dillon ++ib; 43024a1cce3SDavid Greenman /* 43190ecac61SMatthew Dillon * alignment boundry, stop here and switch directions. Do 43290ecac61SMatthew Dillon * not clear ib. 43324a1cce3SDavid Greenman */ 43490ecac61SMatthew Dillon if ((pindex - (ib - 1)) % vm_pageout_page_count == 0) 43590ecac61SMatthew Dillon break; 43624a1cce3SDavid Greenman } 43790ecac61SMatthew Dillon 43890ecac61SMatthew Dillon while (pageout_count < vm_pageout_page_count && 43990ecac61SMatthew Dillon pindex + is < object->size) { 44090ecac61SMatthew Dillon vm_page_t p; 44190ecac61SMatthew Dillon 442c7aebda8SAttilio Rao if ((p = vm_page_next(ps)) == NULL || vm_page_busied(p)) 44390ecac61SMatthew Dillon break; 4442965a453SKip Macy vm_page_lock(p); 44524a1cce3SDavid Greenman vm_page_test_dirty(p); 44626f4eea5SAlan Cox if (p->dirty == 0 || 44790ecac61SMatthew Dillon p->queue != PQ_INACTIVE || 44857601bcbSMatthew Dillon p->hold_count != 0) { /* may be undergoing I/O */ 4492965a453SKip Macy vm_page_unlock(p); 45024a1cce3SDavid Greenman break; 45124a1cce3SDavid Greenman } 4522965a453SKip Macy vm_page_unlock(p); 45391b4f427SAlan Cox mc[page_base + pageout_count] = ps = p; 45490ecac61SMatthew Dillon ++pageout_count; 45590ecac61SMatthew Dillon ++is; 45624a1cce3SDavid Greenman } 45790ecac61SMatthew Dillon 45890ecac61SMatthew Dillon /* 45990ecac61SMatthew Dillon * If we exhausted our forward scan, continue with the reverse scan 46090ecac61SMatthew Dillon * when possible, even past a page boundry. This catches boundry 46190ecac61SMatthew Dillon * conditions. 46290ecac61SMatthew Dillon */ 46390ecac61SMatthew Dillon if (ib && pageout_count < vm_pageout_page_count) 46490ecac61SMatthew Dillon goto more; 465f6b04d2bSDavid Greenman 46667bf6868SJohn Dyson /* 46767bf6868SJohn Dyson * we allow reads during pageouts... 46867bf6868SJohn Dyson */ 469126d6082SKonstantin Belousov return (vm_pageout_flush(&mc[page_base], pageout_count, 0, 0, NULL, 470126d6082SKonstantin Belousov NULL)); 471aef922f5SJohn Dyson } 472aef922f5SJohn Dyson 4731c7c3c6aSMatthew Dillon /* 4741c7c3c6aSMatthew Dillon * vm_pageout_flush() - launder the given pages 4751c7c3c6aSMatthew Dillon * 4761c7c3c6aSMatthew Dillon * The given pages are laundered. Note that we setup for the start of 4771c7c3c6aSMatthew Dillon * I/O ( i.e. busy the page ), mark it read-only, and bump the object 4781c7c3c6aSMatthew Dillon * reference count all in here rather then in the parent. If we want 4791c7c3c6aSMatthew Dillon * the parent to do more sophisticated things we may have to change 4801c7c3c6aSMatthew Dillon * the ordering. 4811e8a675cSKonstantin Belousov * 4821e8a675cSKonstantin Belousov * Returned runlen is the count of pages between mreq and first 4831e8a675cSKonstantin Belousov * page after mreq with status VM_PAGER_AGAIN. 484126d6082SKonstantin Belousov * *eio is set to TRUE if pager returned VM_PAGER_ERROR or VM_PAGER_FAIL 485126d6082SKonstantin Belousov * for any page in runlen set. 4861c7c3c6aSMatthew Dillon */ 487aef922f5SJohn Dyson int 488126d6082SKonstantin Belousov vm_pageout_flush(vm_page_t *mc, int count, int flags, int mreq, int *prunlen, 489126d6082SKonstantin Belousov boolean_t *eio) 490aef922f5SJohn Dyson { 4912e3b314dSAlan Cox vm_object_t object = mc[0]->object; 492aef922f5SJohn Dyson int pageout_status[count]; 49395461b45SJohn Dyson int numpagedout = 0; 4941e8a675cSKonstantin Belousov int i, runlen; 495aef922f5SJohn Dyson 49689f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 4977bec141bSKip Macy 4981c7c3c6aSMatthew Dillon /* 4991c7c3c6aSMatthew Dillon * Initiate I/O. Bump the vm_page_t->busy counter and 5001c7c3c6aSMatthew Dillon * mark the pages read-only. 5011c7c3c6aSMatthew Dillon * 5021c7c3c6aSMatthew Dillon * We do not have to fixup the clean/dirty bits here... we can 5031c7c3c6aSMatthew Dillon * allow the pager to do it after the I/O completes. 50402fa91d3SMatthew Dillon * 50502fa91d3SMatthew Dillon * NOTE! mc[i]->dirty may be partial or fragmented due to an 50602fa91d3SMatthew Dillon * edge case with file fragments. 5071c7c3c6aSMatthew Dillon */ 5088f9110f6SJohn Dyson for (i = 0; i < count; i++) { 5097a935082SAlan Cox KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL, 5107a935082SAlan Cox ("vm_pageout_flush: partially invalid page %p index %d/%d", 5117a935082SAlan Cox mc[i], i, count)); 512c7aebda8SAttilio Rao vm_page_sbusy(mc[i]); 51378985e42SAlan Cox pmap_remove_write(mc[i]); 5142965a453SKip Macy } 515d474eaaaSDoug Rabson vm_object_pip_add(object, count); 516aef922f5SJohn Dyson 517d076fbeaSAlan Cox vm_pager_put_pages(object, mc, count, flags, pageout_status); 51826f9a767SRodney W. Grimes 5191e8a675cSKonstantin Belousov runlen = count - mreq; 520126d6082SKonstantin Belousov if (eio != NULL) 521126d6082SKonstantin Belousov *eio = FALSE; 522aef922f5SJohn Dyson for (i = 0; i < count; i++) { 523aef922f5SJohn Dyson vm_page_t mt = mc[i]; 52424a1cce3SDavid Greenman 5254cd45723SAlan Cox KASSERT(pageout_status[i] == VM_PAGER_PEND || 5266031c68dSAlan Cox !pmap_page_is_write_mapped(mt), 5279ea8d1a6SAlan Cox ("vm_pageout_flush: page %p is not write protected", mt)); 52826f9a767SRodney W. Grimes switch (pageout_status[i]) { 52926f9a767SRodney W. Grimes case VM_PAGER_OK: 53026f9a767SRodney W. Grimes case VM_PAGER_PEND: 53195461b45SJohn Dyson numpagedout++; 53226f9a767SRodney W. Grimes break; 53326f9a767SRodney W. Grimes case VM_PAGER_BAD: 53426f9a767SRodney W. Grimes /* 5350d94caffSDavid Greenman * Page outside of range of object. Right now we 5360d94caffSDavid Greenman * essentially lose the changes by pretending it 5370d94caffSDavid Greenman * worked. 53826f9a767SRodney W. Grimes */ 53990ecac61SMatthew Dillon vm_page_undirty(mt); 54026f9a767SRodney W. Grimes break; 54126f9a767SRodney W. Grimes case VM_PAGER_ERROR: 54226f9a767SRodney W. Grimes case VM_PAGER_FAIL: 54326f9a767SRodney W. Grimes /* 5440d94caffSDavid Greenman * If page couldn't be paged out, then reactivate the 5450d94caffSDavid Greenman * page so it doesn't clog the inactive list. (We 5460d94caffSDavid Greenman * will try paging out it again later). 54726f9a767SRodney W. Grimes */ 5483c4a2440SAlan Cox vm_page_lock(mt); 54924a1cce3SDavid Greenman vm_page_activate(mt); 5503c4a2440SAlan Cox vm_page_unlock(mt); 551126d6082SKonstantin Belousov if (eio != NULL && i >= mreq && i - mreq < runlen) 552126d6082SKonstantin Belousov *eio = TRUE; 55326f9a767SRodney W. Grimes break; 55426f9a767SRodney W. Grimes case VM_PAGER_AGAIN: 5551e8a675cSKonstantin Belousov if (i >= mreq && i - mreq < runlen) 5561e8a675cSKonstantin Belousov runlen = i - mreq; 55726f9a767SRodney W. Grimes break; 55826f9a767SRodney W. Grimes } 55926f9a767SRodney W. Grimes 56026f9a767SRodney W. Grimes /* 5610d94caffSDavid Greenman * If the operation is still going, leave the page busy to 5620d94caffSDavid Greenman * block all other accesses. Also, leave the paging in 5630d94caffSDavid Greenman * progress indicator set so that we don't attempt an object 5640d94caffSDavid Greenman * collapse. 56526f9a767SRodney W. Grimes */ 56626f9a767SRodney W. Grimes if (pageout_status[i] != VM_PAGER_PEND) { 567f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 568c7aebda8SAttilio Rao vm_page_sunbusy(mt); 5693c4a2440SAlan Cox if (vm_page_count_severe()) { 5703c4a2440SAlan Cox vm_page_lock(mt); 5719ea8d1a6SAlan Cox vm_page_try_to_cache(mt); 5722965a453SKip Macy vm_page_unlock(mt); 57326f9a767SRodney W. Grimes } 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 /* 910*34d8b7eaSJeff Roberson * Attempt to acquire all of the necessary locks to launder a page and 911*34d8b7eaSJeff Roberson * then call through the clustering layer to PUTPAGES. Wait a short 912*34d8b7eaSJeff Roberson * time for a vnode lock. 913*34d8b7eaSJeff Roberson * 914*34d8b7eaSJeff Roberson * Requires the page and object lock on entry, releases both before return. 915*34d8b7eaSJeff Roberson * Returns 0 on success and an errno otherwise. 916*34d8b7eaSJeff Roberson */ 917*34d8b7eaSJeff Roberson static int 918*34d8b7eaSJeff Roberson vm_pageout_clean(vm_page_t m) 919*34d8b7eaSJeff Roberson { 920*34d8b7eaSJeff Roberson struct vnode *vp; 921*34d8b7eaSJeff Roberson struct mount *mp; 922*34d8b7eaSJeff Roberson vm_object_t object; 923*34d8b7eaSJeff Roberson vm_pindex_t pindex; 924*34d8b7eaSJeff Roberson int error, lockmode; 925*34d8b7eaSJeff Roberson 926*34d8b7eaSJeff Roberson vm_page_assert_locked(m); 927*34d8b7eaSJeff Roberson object = m->object; 928*34d8b7eaSJeff Roberson VM_OBJECT_ASSERT_WLOCKED(object); 929*34d8b7eaSJeff Roberson error = 0; 930*34d8b7eaSJeff Roberson vp = NULL; 931*34d8b7eaSJeff Roberson mp = NULL; 932*34d8b7eaSJeff Roberson 933*34d8b7eaSJeff Roberson /* 934*34d8b7eaSJeff Roberson * The object is already known NOT to be dead. It 935*34d8b7eaSJeff Roberson * is possible for the vget() to block the whole 936*34d8b7eaSJeff Roberson * pageout daemon, but the new low-memory handling 937*34d8b7eaSJeff Roberson * code should prevent it. 938*34d8b7eaSJeff Roberson * 939*34d8b7eaSJeff Roberson * We can't wait forever for the vnode lock, we might 940*34d8b7eaSJeff Roberson * deadlock due to a vn_read() getting stuck in 941*34d8b7eaSJeff Roberson * vm_wait while holding this vnode. We skip the 942*34d8b7eaSJeff Roberson * vnode if we can't get it in a reasonable amount 943*34d8b7eaSJeff Roberson * of time. 944*34d8b7eaSJeff Roberson */ 945*34d8b7eaSJeff Roberson if (object->type == OBJT_VNODE) { 946*34d8b7eaSJeff Roberson vm_page_unlock(m); 947*34d8b7eaSJeff Roberson vp = object->handle; 948*34d8b7eaSJeff Roberson if (vp->v_type == VREG && 949*34d8b7eaSJeff Roberson vn_start_write(vp, &mp, V_NOWAIT) != 0) { 950*34d8b7eaSJeff Roberson mp = NULL; 951*34d8b7eaSJeff Roberson error = EDEADLK; 952*34d8b7eaSJeff Roberson goto unlock_all; 953*34d8b7eaSJeff Roberson } 954*34d8b7eaSJeff Roberson KASSERT(mp != NULL, 955*34d8b7eaSJeff Roberson ("vp %p with NULL v_mount", vp)); 956*34d8b7eaSJeff Roberson vm_object_reference_locked(object); 957*34d8b7eaSJeff Roberson pindex = m->pindex; 958*34d8b7eaSJeff Roberson VM_OBJECT_WUNLOCK(object); 959*34d8b7eaSJeff Roberson lockmode = MNT_SHARED_WRITES(vp->v_mount) ? 960*34d8b7eaSJeff Roberson LK_SHARED : LK_EXCLUSIVE; 961*34d8b7eaSJeff Roberson if (vget(vp, lockmode | LK_TIMELOCK, curthread)) { 962*34d8b7eaSJeff Roberson vp = NULL; 963*34d8b7eaSJeff Roberson error = EDEADLK; 964*34d8b7eaSJeff Roberson goto unlock_mp; 965*34d8b7eaSJeff Roberson } 966*34d8b7eaSJeff Roberson VM_OBJECT_WLOCK(object); 967*34d8b7eaSJeff Roberson vm_page_lock(m); 968*34d8b7eaSJeff Roberson /* 969*34d8b7eaSJeff Roberson * While the object and page were unlocked, the page 970*34d8b7eaSJeff Roberson * may have been: 971*34d8b7eaSJeff Roberson * (1) moved to a different queue, 972*34d8b7eaSJeff Roberson * (2) reallocated to a different object, 973*34d8b7eaSJeff Roberson * (3) reallocated to a different offset, or 974*34d8b7eaSJeff Roberson * (4) cleaned. 975*34d8b7eaSJeff Roberson */ 976*34d8b7eaSJeff Roberson if (m->queue != PQ_INACTIVE || m->object != object || 977*34d8b7eaSJeff Roberson m->pindex != pindex || m->dirty == 0) { 978*34d8b7eaSJeff Roberson vm_page_unlock(m); 979*34d8b7eaSJeff Roberson error = ENXIO; 980*34d8b7eaSJeff Roberson goto unlock_all; 981*34d8b7eaSJeff Roberson } 982*34d8b7eaSJeff Roberson 983*34d8b7eaSJeff Roberson /* 984*34d8b7eaSJeff Roberson * The page may have been busied or held while the object 985*34d8b7eaSJeff Roberson * and page locks were released. 986*34d8b7eaSJeff Roberson */ 987*34d8b7eaSJeff Roberson if (vm_page_busied(m) || m->hold_count != 0) { 988*34d8b7eaSJeff Roberson vm_page_unlock(m); 989*34d8b7eaSJeff Roberson error = EBUSY; 990*34d8b7eaSJeff Roberson goto unlock_all; 991*34d8b7eaSJeff Roberson } 992*34d8b7eaSJeff Roberson } 993*34d8b7eaSJeff Roberson 994*34d8b7eaSJeff Roberson /* 995*34d8b7eaSJeff Roberson * If a page is dirty, then it is either being washed 996*34d8b7eaSJeff Roberson * (but not yet cleaned) or it is still in the 997*34d8b7eaSJeff Roberson * laundry. If it is still in the laundry, then we 998*34d8b7eaSJeff Roberson * start the cleaning operation. 999*34d8b7eaSJeff Roberson */ 1000*34d8b7eaSJeff Roberson if (vm_pageout_cluster(m) == 0) 1001*34d8b7eaSJeff Roberson error = EIO; 1002*34d8b7eaSJeff Roberson 1003*34d8b7eaSJeff Roberson unlock_all: 1004*34d8b7eaSJeff Roberson VM_OBJECT_WUNLOCK(object); 1005*34d8b7eaSJeff Roberson 1006*34d8b7eaSJeff Roberson unlock_mp: 1007*34d8b7eaSJeff Roberson vm_page_lock_assert(m, MA_NOTOWNED); 1008*34d8b7eaSJeff Roberson if (mp != NULL) { 1009*34d8b7eaSJeff Roberson if (vp != NULL) 1010*34d8b7eaSJeff Roberson vput(vp); 1011*34d8b7eaSJeff Roberson vm_object_deallocate(object); 1012*34d8b7eaSJeff Roberson vn_finished_write(mp); 1013*34d8b7eaSJeff Roberson } 1014*34d8b7eaSJeff Roberson 1015*34d8b7eaSJeff Roberson return (error); 1016*34d8b7eaSJeff Roberson } 1017*34d8b7eaSJeff Roberson 1018*34d8b7eaSJeff 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; 10319099545aSAlan Cox int act_delta, addl_page_shortage, deficit, maxscan, page_shortage; 1032f6b04d2bSDavid Greenman int vnodes_skipped = 0; 10332b6b0df7SMatthew Dillon int maxlaunder; 103448cc2fc7SKonstantin Belousov boolean_t 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 && 1041f721133eSSteven Hartland (ticks - lowmem_ticks) / hz >= 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(); 1052c9612b2dSJeff Roberson lowmem_ticks = ticks; 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 10888d220203SAlan Cox /* 10898d220203SAlan Cox * Start scanning the inactive queue for pages we can move to the 10908d220203SAlan Cox * cache or free. The scan will stop when the target is reached or 10918d220203SAlan Cox * we have scanned the entire inactive queue. Note that m->act_count 10928d220203SAlan Cox * is not used to form decisions for the inactive queue, only for the 10938d220203SAlan Cox * active queue. 10948d220203SAlan Cox */ 1095449c2e92SKonstantin Belousov pq = &vmd->vmd_pagequeues[PQ_INACTIVE]; 1096449c2e92SKonstantin Belousov maxscan = pq->pq_cnt; 10978d220203SAlan Cox vm_pagequeue_lock(pq); 10988d220203SAlan Cox queues_locked = TRUE; 10998d220203SAlan Cox for (m = TAILQ_FIRST(&pq->pq_pl); 11001c7c3c6aSMatthew Dillon m != NULL && maxscan-- > 0 && page_shortage > 0; 1101e929c00dSKirk McKusick m = next) { 11028d220203SAlan Cox vm_pagequeue_assert_locked(pq); 110348cc2fc7SKonstantin Belousov KASSERT(queues_locked, ("unlocked queues")); 1104d4961bcbSKonstantin Belousov KASSERT(m->queue == PQ_INACTIVE, ("Inactive queue %p", m)); 1105df8bae1dSRodney W. Grimes 11068d220203SAlan Cox PCPU_INC(cnt.v_pdpages); 1107c325e866SKonstantin Belousov next = TAILQ_NEXT(m, plinks.q); 1108df8bae1dSRodney W. Grimes 1109936524aaSMatthew Dillon /* 1110936524aaSMatthew Dillon * skip marker pages 1111936524aaSMatthew Dillon */ 1112936524aaSMatthew Dillon if (m->flags & PG_MARKER) 1113936524aaSMatthew Dillon continue; 1114936524aaSMatthew Dillon 11157900f95dSKonstantin Belousov KASSERT((m->flags & PG_FICTITIOUS) == 0, 11167900f95dSKonstantin Belousov ("Fictitious page %p cannot be in inactive queue", m)); 11177900f95dSKonstantin Belousov KASSERT((m->oflags & VPO_UNMANAGED) == 0, 11187900f95dSKonstantin Belousov ("Unmanaged page %p cannot be in inactive queue", m)); 11197900f95dSKonstantin Belousov 11208c616246SKonstantin Belousov /* 1121311e34e2SKonstantin Belousov * The page or object lock acquisitions fail if the 1122311e34e2SKonstantin Belousov * page was removed from the queue or moved to a 1123311e34e2SKonstantin Belousov * different position within the queue. In either 1124311e34e2SKonstantin Belousov * case, addl_page_shortage should not be incremented. 11258c616246SKonstantin Belousov */ 11268c616246SKonstantin Belousov if (!vm_pageout_page_lock(m, &next)) { 11278c616246SKonstantin Belousov vm_page_unlock(m); 1128b182ec9eSJohn Dyson continue; 1129df8bae1dSRodney W. Grimes } 11309ee2165fSAlan Cox object = m->object; 113189f6b863SAttilio Rao if (!VM_OBJECT_TRYWLOCK(object) && 1132311e34e2SKonstantin Belousov !vm_pageout_fallback_object_lock(m, &next)) { 11332965a453SKip Macy vm_page_unlock(m); 113489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 113534d9e6fdSAlan Cox continue; 113634d9e6fdSAlan Cox } 1137311e34e2SKonstantin Belousov 1138311e34e2SKonstantin Belousov /* 1139311e34e2SKonstantin Belousov * Don't mess with busy pages, keep them at at the 1140311e34e2SKonstantin Belousov * front of the queue, most likely they are being 1141311e34e2SKonstantin Belousov * paged out. Increment addl_page_shortage for busy 1142311e34e2SKonstantin Belousov * pages, because they may leave the inactive queue 1143311e34e2SKonstantin Belousov * shortly after page scan is finished. 1144311e34e2SKonstantin Belousov */ 1145c7aebda8SAttilio Rao if (vm_page_busied(m)) { 11462965a453SKip Macy vm_page_unlock(m); 114789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1148b182ec9eSJohn Dyson addl_page_shortage++; 114926f9a767SRodney W. Grimes continue; 115026f9a767SRodney W. Grimes } 1151bd7e5f99SJohn Dyson 11527e006499SJohn Dyson /* 11538d220203SAlan Cox * We unlock the inactive page queue, invalidating the 115448cc2fc7SKonstantin Belousov * 'next' pointer. Use our marker to remember our 115548cc2fc7SKonstantin Belousov * place. 115648cc2fc7SKonstantin Belousov */ 1157c325e866SKonstantin Belousov TAILQ_INSERT_AFTER(&pq->pq_pl, m, &vmd->vmd_marker, plinks.q); 11588d220203SAlan Cox vm_pagequeue_unlock(pq); 115948cc2fc7SKonstantin Belousov queues_locked = FALSE; 116048cc2fc7SKonstantin Belousov 116148cc2fc7SKonstantin Belousov /* 1162bb7858eaSJeff Roberson * We bump the activation count if the page has been 1163bb7858eaSJeff Roberson * referenced while in the inactive queue. This makes 1164bb7858eaSJeff Roberson * it less likely that the page will be added back to the 1165bb7858eaSJeff Roberson * inactive queue prematurely again. Here we check the 11661c7c3c6aSMatthew Dillon * page tables (or emulated bits, if any), given the upper 11671c7c3c6aSMatthew Dillon * level VM system not knowing anything about existing 11681c7c3c6aSMatthew Dillon * references. 11697e006499SJohn Dyson */ 1170bb7858eaSJeff Roberson if ((m->aflags & PGA_REFERENCED) != 0) { 1171bb7858eaSJeff Roberson vm_page_aflag_clear(m, PGA_REFERENCED); 1172bb7858eaSJeff Roberson act_delta = 1; 117386fa2471SAlan Cox } else 117486fa2471SAlan Cox act_delta = 0; 1175bb7858eaSJeff Roberson if (object->ref_count != 0) { 1176bb7858eaSJeff Roberson act_delta += pmap_ts_referenced(m); 1177bb7858eaSJeff Roberson } else { 1178bb7858eaSJeff Roberson KASSERT(!pmap_page_is_mapped(m), 1179bb7858eaSJeff Roberson ("vm_pageout_scan: page %p is mapped", m)); 11802fe6e4d7SDavid Greenman } 1181ef743ce6SJohn Dyson 11827e006499SJohn Dyson /* 11831c7c3c6aSMatthew Dillon * If the upper level VM system knows about any page 1184bb7858eaSJeff Roberson * references, we reactivate the page or requeue it. 11857e006499SJohn Dyson */ 1186bb7858eaSJeff Roberson if (act_delta != 0) { 118786fa2471SAlan Cox if (object->ref_count != 0) { 118826f9a767SRodney W. Grimes vm_page_activate(m); 1189bb7858eaSJeff Roberson m->act_count += act_delta + ACT_ADVANCE; 1190bb7858eaSJeff Roberson } else { 1191bb7858eaSJeff Roberson vm_pagequeue_lock(pq); 1192bb7858eaSJeff Roberson queues_locked = TRUE; 1193bb7858eaSJeff Roberson vm_page_requeue_locked(m); 1194bb7858eaSJeff Roberson } 119589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1196a15f7df5SAttilio Rao vm_page_unlock(m); 119748cc2fc7SKonstantin Belousov goto relock_queues; 11980d94caffSDavid Greenman } 119967bf6868SJohn Dyson 1200311e34e2SKonstantin Belousov if (m->hold_count != 0) { 1201311e34e2SKonstantin Belousov vm_page_unlock(m); 120289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1203311e34e2SKonstantin Belousov 1204311e34e2SKonstantin Belousov /* 1205311e34e2SKonstantin Belousov * Held pages are essentially stuck in the 1206311e34e2SKonstantin Belousov * queue. So, they ought to be discounted 1207449c2e92SKonstantin Belousov * from the inactive count. See the 1208311e34e2SKonstantin Belousov * calculation of the page_shortage for the 1209311e34e2SKonstantin Belousov * loop over the active queue below. 1210311e34e2SKonstantin Belousov */ 1211311e34e2SKonstantin Belousov addl_page_shortage++; 1212311e34e2SKonstantin Belousov goto relock_queues; 1213311e34e2SKonstantin Belousov } 1214311e34e2SKonstantin Belousov 12157e006499SJohn Dyson /* 12169fc4739dSAlan Cox * If the page appears to be clean at the machine-independent 12179fc4739dSAlan Cox * layer, then remove all of its mappings from the pmap in 12189fc4739dSAlan Cox * anticipation of placing it onto the cache queue. If, 12199fc4739dSAlan Cox * however, any of the page's mappings allow write access, 12209fc4739dSAlan Cox * then the page may still be modified until the last of those 12219fc4739dSAlan Cox * mappings are removed. 12227e006499SJohn Dyson */ 12239fc4739dSAlan Cox vm_page_test_dirty(m); 12249fc4739dSAlan Cox if (m->dirty == 0 && object->ref_count != 0) 1225b78ddb0bSAlan Cox pmap_remove_all(m); 1226dcbcd518SBruce Evans 12276989c456SAlan Cox if (m->valid == 0) { 12287e006499SJohn Dyson /* 12297e006499SJohn Dyson * Invalid pages can be easily freed 12307e006499SJohn Dyson */ 12316989c456SAlan Cox vm_page_free(m); 123248cc2fc7SKonstantin Belousov PCPU_INC(cnt.v_dfree); 12331c7c3c6aSMatthew Dillon --page_shortage; 1234bd7e5f99SJohn Dyson } else if (m->dirty == 0) { 12356989c456SAlan Cox /* 12366989c456SAlan Cox * Clean pages can be placed onto the cache queue. 12376989c456SAlan Cox * This effectively frees them. 12386989c456SAlan Cox */ 1239bd7e5f99SJohn Dyson vm_page_cache(m); 12401c7c3c6aSMatthew Dillon --page_shortage; 1241d9e23210SJeff Roberson } else if ((m->flags & PG_WINATCFLS) == 0 && pass < 2) { 12427e006499SJohn Dyson /* 12432b6b0df7SMatthew Dillon * Dirty pages need to be paged out, but flushing 1244ab46f63eSJohn Baldwin * a page is extremely expensive versus freeing 12452b6b0df7SMatthew Dillon * a clean page. Rather then artificially limiting 12462b6b0df7SMatthew Dillon * the number of pages we can flush, we instead give 12472b6b0df7SMatthew Dillon * dirty pages extra priority on the inactive queue 12482b6b0df7SMatthew Dillon * by forcing them to be cycled through the queue 12492b6b0df7SMatthew Dillon * twice before being flushed, after which the 12502b6b0df7SMatthew Dillon * (now clean) page will cycle through once more 12512b6b0df7SMatthew Dillon * before being freed. This significantly extends 12522b6b0df7SMatthew Dillon * the thrash point for a heavily loaded machine. 12537e006499SJohn Dyson */ 12543407fefeSKonstantin Belousov m->flags |= PG_WINATCFLS; 12558d220203SAlan Cox vm_pagequeue_lock(pq); 125648cc2fc7SKonstantin Belousov queues_locked = TRUE; 12578d220203SAlan Cox vm_page_requeue_locked(m); 12580d94caffSDavid Greenman } else if (maxlaunder > 0) { 12592b6b0df7SMatthew Dillon /* 12602b6b0df7SMatthew Dillon * We always want to try to flush some dirty pages if 12612b6b0df7SMatthew Dillon * we encounter them, to keep the system stable. 12622b6b0df7SMatthew Dillon * Normally this number is small, but under extreme 12632b6b0df7SMatthew Dillon * pressure where there are insufficient clean pages 12642b6b0df7SMatthew Dillon * on the inactive queue, we may have to go all out. 12652b6b0df7SMatthew Dillon */ 12665050aa86SKonstantin Belousov int swap_pageouts_ok; 1267*34d8b7eaSJeff Roberson int error; 12680d94caffSDavid Greenman 126912ac6a1dSJohn Dyson if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) { 127012ac6a1dSJohn Dyson swap_pageouts_ok = 1; 127112ac6a1dSJohn Dyson } else { 127212ac6a1dSJohn Dyson swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts); 127312ac6a1dSJohn Dyson swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts && 127490ecac61SMatthew Dillon vm_page_count_min()); 127512ac6a1dSJohn Dyson 127612ac6a1dSJohn Dyson } 127770111b90SJohn Dyson 127870111b90SJohn Dyson /* 12791c7c3c6aSMatthew Dillon * We don't bother paging objects that are "dead". 12801c7c3c6aSMatthew Dillon * Those objects are in a "rundown" state. 128170111b90SJohn Dyson */ 128270111b90SJohn Dyson if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) { 12838d220203SAlan Cox vm_pagequeue_lock(pq); 12842965a453SKip Macy vm_page_unlock(m); 128589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 128648cc2fc7SKonstantin Belousov queues_locked = TRUE; 12878d220203SAlan Cox vm_page_requeue_locked(m); 128848cc2fc7SKonstantin Belousov goto relock_queues; 128912ac6a1dSJohn Dyson } 1290*34d8b7eaSJeff Roberson error = vm_pageout_clean(m); 12911c7c3c6aSMatthew Dillon /* 1292*34d8b7eaSJeff Roberson * Decrement page_shortage on success to account for 12932b6b0df7SMatthew Dillon * the (future) cleaned page. Otherwise we could wind 12942b6b0df7SMatthew Dillon * up laundering or cleaning too many pages. 12950d94caffSDavid Greenman */ 1296*34d8b7eaSJeff Roberson if (error == 0) { 1297*34d8b7eaSJeff Roberson page_shortage--; 1298*34d8b7eaSJeff Roberson maxlaunder--; 1299*34d8b7eaSJeff Roberson } else if (error == EDEADLK) { 1300*34d8b7eaSJeff Roberson pageout_lock_miss++; 1301*34d8b7eaSJeff Roberson vnodes_skipped++; 1302*34d8b7eaSJeff Roberson } else if (error == EBUSY) { 1303*34d8b7eaSJeff Roberson addl_page_shortage++; 130448cc2fc7SKonstantin Belousov } 130548cc2fc7SKonstantin Belousov vm_page_lock_assert(m, MA_NOTOWNED); 130648cc2fc7SKonstantin Belousov goto relock_queues; 130748cc2fc7SKonstantin Belousov } 130848cc2fc7SKonstantin Belousov vm_page_unlock(m); 130989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 131048cc2fc7SKonstantin Belousov relock_queues: 131148cc2fc7SKonstantin Belousov if (!queues_locked) { 13128d220203SAlan Cox vm_pagequeue_lock(pq); 131348cc2fc7SKonstantin Belousov queues_locked = TRUE; 13146989c456SAlan Cox } 1315c325e866SKonstantin Belousov next = TAILQ_NEXT(&vmd->vmd_marker, plinks.q); 1316c325e866SKonstantin Belousov TAILQ_REMOVE(&pq->pq_pl, &vmd->vmd_marker, plinks.q); 13170d94caffSDavid Greenman } 13188d220203SAlan Cox vm_pagequeue_unlock(pq); 131926f9a767SRodney W. Grimes 13209452b5edSAlan Cox #if !defined(NO_SWAPPING) 13219452b5edSAlan Cox /* 13229452b5edSAlan Cox * Wakeup the swapout daemon if we didn't cache or free the targeted 13239452b5edSAlan Cox * number of pages. 13249452b5edSAlan Cox */ 13259452b5edSAlan Cox if (vm_swap_enabled && page_shortage > 0) 13269452b5edSAlan Cox vm_req_vmdaemon(VM_SWAP_NORMAL); 13279452b5edSAlan Cox #endif 13289452b5edSAlan Cox 13299452b5edSAlan Cox /* 13309452b5edSAlan Cox * Wakeup the sync daemon if we skipped a vnode in a writeable object 13319452b5edSAlan Cox * and we didn't cache or free enough pages. 13329452b5edSAlan Cox */ 13339452b5edSAlan Cox if (vnodes_skipped > 0 && page_shortage > vm_cnt.v_free_target - 13349452b5edSAlan Cox vm_cnt.v_free_min) 13359452b5edSAlan Cox (void)speedup_syncer(); 13369452b5edSAlan Cox 1337df8bae1dSRodney W. Grimes /* 1338936524aaSMatthew Dillon * Compute the number of pages we want to try to move from the 1339936524aaSMatthew Dillon * active queue to the inactive queue. 13401c7c3c6aSMatthew Dillon */ 134144f1c916SBryan Drewery page_shortage = vm_cnt.v_inactive_target - vm_cnt.v_inactive_count + 13429099545aSAlan Cox vm_paging_target() + deficit + addl_page_shortage; 13439099545aSAlan Cox 1344114f62c6SJeff Roberson pq = &vmd->vmd_pagequeues[PQ_ACTIVE]; 1345114f62c6SJeff Roberson vm_pagequeue_lock(pq); 13469099545aSAlan Cox maxscan = pq->pq_cnt; 13479099545aSAlan Cox 1348d9e23210SJeff Roberson /* 1349d9e23210SJeff Roberson * If we're just idle polling attempt to visit every 1350d9e23210SJeff Roberson * active page within 'update_period' seconds. 1351d9e23210SJeff Roberson */ 1352d9e23210SJeff Roberson if (pass == 0 && vm_pageout_update_period != 0) { 13539099545aSAlan Cox maxscan /= vm_pageout_update_period; 13549099545aSAlan Cox page_shortage = maxscan; 1355d9e23210SJeff Roberson } 13561c7c3c6aSMatthew Dillon 13571c7c3c6aSMatthew Dillon /* 1358936524aaSMatthew Dillon * Scan the active queue for things we can deactivate. We nominally 1359936524aaSMatthew Dillon * track the per-page activity counter and use it to locate 1360936524aaSMatthew Dillon * deactivation candidates. 13611c7c3c6aSMatthew Dillon */ 13628d220203SAlan Cox m = TAILQ_FIRST(&pq->pq_pl); 13639099545aSAlan Cox while (m != NULL && maxscan-- > 0 && page_shortage > 0) { 1364f35329acSJohn Dyson 13659cf51988SAlan Cox KASSERT(m->queue == PQ_ACTIVE, 1366d3c09dd7SAlan Cox ("vm_pageout_scan: page %p isn't active", m)); 1367f35329acSJohn Dyson 1368c325e866SKonstantin Belousov next = TAILQ_NEXT(m, plinks.q); 13698dbca793STor Egge if ((m->flags & PG_MARKER) != 0) { 13708dbca793STor Egge m = next; 13718dbca793STor Egge continue; 13728dbca793STor Egge } 13737900f95dSKonstantin Belousov KASSERT((m->flags & PG_FICTITIOUS) == 0, 13747900f95dSKonstantin Belousov ("Fictitious page %p cannot be in active queue", m)); 13757900f95dSKonstantin Belousov KASSERT((m->oflags & VPO_UNMANAGED) == 0, 13767900f95dSKonstantin Belousov ("Unmanaged page %p cannot be in active queue", m)); 13779ee2165fSAlan Cox if (!vm_pageout_page_lock(m, &next)) { 13788c616246SKonstantin Belousov vm_page_unlock(m); 13792965a453SKip Macy m = next; 13802965a453SKip Macy continue; 13812965a453SKip Macy } 1382b18bfc3dSJohn Dyson 1383b18bfc3dSJohn Dyson /* 1384b18bfc3dSJohn Dyson * The count for pagedaemon pages is done after checking the 1385956f3135SPhilippe Charnier * page for eligibility... 1386b18bfc3dSJohn Dyson */ 13878d220203SAlan Cox PCPU_INC(cnt.v_pdpages); 1388ef743ce6SJohn Dyson 13897e006499SJohn Dyson /* 13907e006499SJohn Dyson * Check to see "how much" the page has been used. 13917e006499SJohn Dyson */ 139286fa2471SAlan Cox if ((m->aflags & PGA_REFERENCED) != 0) { 1393bb7858eaSJeff Roberson vm_page_aflag_clear(m, PGA_REFERENCED); 139486fa2471SAlan Cox act_delta = 1; 139586fa2471SAlan Cox } else 139686fa2471SAlan Cox act_delta = 0; 139786fa2471SAlan Cox 1398274132acSJeff Roberson /* 1399274132acSJeff Roberson * Unlocked object ref count check. Two races are possible. 1400274132acSJeff Roberson * 1) The ref was transitioning to zero and we saw non-zero, 1401274132acSJeff Roberson * the pmap bits will be checked unnecessarily. 1402274132acSJeff Roberson * 2) The ref was transitioning to one and we saw zero. 1403274132acSJeff Roberson * The page lock prevents a new reference to this page so 1404274132acSJeff Roberson * we need not check the reference bits. 1405274132acSJeff Roberson */ 1406274132acSJeff Roberson if (m->object->ref_count != 0) 1407bb7858eaSJeff Roberson act_delta += pmap_ts_referenced(m); 1408bb7858eaSJeff Roberson 1409bb7858eaSJeff Roberson /* 1410bb7858eaSJeff Roberson * Advance or decay the act_count based on recent usage. 1411bb7858eaSJeff Roberson */ 141286fa2471SAlan Cox if (act_delta != 0) { 1413bb7858eaSJeff Roberson m->act_count += ACT_ADVANCE + act_delta; 141438efa82bSJohn Dyson if (m->act_count > ACT_MAX) 141538efa82bSJohn Dyson m->act_count = ACT_MAX; 141686fa2471SAlan Cox } else 141738efa82bSJohn Dyson m->act_count -= min(m->act_count, ACT_DECLINE); 1418bb7858eaSJeff Roberson 1419bb7858eaSJeff Roberson /* 1420bb7858eaSJeff Roberson * Move this page to the tail of the active or inactive 1421bb7858eaSJeff Roberson * queue depending on usage. 1422bb7858eaSJeff Roberson */ 142386fa2471SAlan Cox if (m->act_count == 0) { 14248d220203SAlan Cox /* Dequeue to avoid later lock recursion. */ 14258d220203SAlan Cox vm_page_dequeue_locked(m); 1426d4a272dbSJohn Dyson vm_page_deactivate(m); 1427bb7858eaSJeff Roberson page_shortage--; 14288d220203SAlan Cox } else 14298d220203SAlan Cox vm_page_requeue_locked(m); 14302965a453SKip Macy vm_page_unlock(m); 143126f9a767SRodney W. Grimes m = next; 143226f9a767SRodney W. Grimes } 14338d220203SAlan Cox vm_pagequeue_unlock(pq); 1434ceb0cf87SJohn Dyson #if !defined(NO_SWAPPING) 1435ceb0cf87SJohn Dyson /* 1436ceb0cf87SJohn Dyson * Idle process swapout -- run once per second. 1437ceb0cf87SJohn Dyson */ 1438ceb0cf87SJohn Dyson if (vm_swap_idle_enabled) { 1439ceb0cf87SJohn Dyson static long lsec; 1440227ee8a1SPoul-Henning Kamp if (time_second != lsec) { 144197824da3SAlan Cox vm_req_vmdaemon(VM_SWAP_IDLE); 1442227ee8a1SPoul-Henning Kamp lsec = time_second; 1443ceb0cf87SJohn Dyson } 1444ceb0cf87SJohn Dyson } 1445ceb0cf87SJohn Dyson #endif 1446ceb0cf87SJohn Dyson 14475663e6deSDavid Greenman /* 1448e92686d0SDavid Schultz * If we are critically low on one of RAM or swap and low on 1449e92686d0SDavid Schultz * the other, kill the largest process. However, we avoid 1450e92686d0SDavid Schultz * doing this on the first pass in order to give ourselves a 1451e92686d0SDavid Schultz * chance to flush out dirty vnode-backed pages and to allow 1452e92686d0SDavid Schultz * active pages to be moved to the inactive queue and reclaimed. 14532025d69bSKonstantin Belousov */ 1454449c2e92SKonstantin Belousov vm_pageout_mightbe_oom(vmd, pass); 14552025d69bSKonstantin Belousov } 14562025d69bSKonstantin Belousov 1457449c2e92SKonstantin Belousov static int vm_pageout_oom_vote; 1458449c2e92SKonstantin Belousov 1459449c2e92SKonstantin Belousov /* 1460449c2e92SKonstantin Belousov * The pagedaemon threads randlomly select one to perform the 1461449c2e92SKonstantin Belousov * OOM. Trying to kill processes before all pagedaemons 1462449c2e92SKonstantin Belousov * failed to reach free target is premature. 1463449c2e92SKonstantin Belousov */ 1464449c2e92SKonstantin Belousov static void 1465449c2e92SKonstantin Belousov vm_pageout_mightbe_oom(struct vm_domain *vmd, int pass) 1466449c2e92SKonstantin Belousov { 1467449c2e92SKonstantin Belousov int old_vote; 1468449c2e92SKonstantin Belousov 1469d9e23210SJeff Roberson if (pass <= 1 || !((swap_pager_avail < 64 && vm_page_count_min()) || 1470449c2e92SKonstantin Belousov (swap_pager_full && vm_paging_target() > 0))) { 1471449c2e92SKonstantin Belousov if (vmd->vmd_oom) { 1472449c2e92SKonstantin Belousov vmd->vmd_oom = FALSE; 1473449c2e92SKonstantin Belousov atomic_subtract_int(&vm_pageout_oom_vote, 1); 1474449c2e92SKonstantin Belousov } 1475449c2e92SKonstantin Belousov return; 1476449c2e92SKonstantin Belousov } 1477449c2e92SKonstantin Belousov 1478449c2e92SKonstantin Belousov if (vmd->vmd_oom) 1479449c2e92SKonstantin Belousov return; 1480449c2e92SKonstantin Belousov 1481449c2e92SKonstantin Belousov vmd->vmd_oom = TRUE; 1482449c2e92SKonstantin Belousov old_vote = atomic_fetchadd_int(&vm_pageout_oom_vote, 1); 1483449c2e92SKonstantin Belousov if (old_vote != vm_ndomains - 1) 1484449c2e92SKonstantin Belousov return; 1485449c2e92SKonstantin Belousov 1486449c2e92SKonstantin Belousov /* 1487449c2e92SKonstantin Belousov * The current pagedaemon thread is the last in the quorum to 1488449c2e92SKonstantin Belousov * start OOM. Initiate the selection and signaling of the 1489449c2e92SKonstantin Belousov * victim. 1490449c2e92SKonstantin Belousov */ 1491449c2e92SKonstantin Belousov vm_pageout_oom(VM_OOM_MEM); 1492449c2e92SKonstantin Belousov 1493449c2e92SKonstantin Belousov /* 1494449c2e92SKonstantin Belousov * After one round of OOM terror, recall our vote. On the 1495449c2e92SKonstantin Belousov * next pass, current pagedaemon would vote again if the low 1496449c2e92SKonstantin Belousov * memory condition is still there, due to vmd_oom being 1497449c2e92SKonstantin Belousov * false. 1498449c2e92SKonstantin Belousov */ 1499449c2e92SKonstantin Belousov vmd->vmd_oom = FALSE; 1500449c2e92SKonstantin Belousov atomic_subtract_int(&vm_pageout_oom_vote, 1); 1501449c2e92SKonstantin Belousov } 15022025d69bSKonstantin Belousov 15032025d69bSKonstantin Belousov void 15042025d69bSKonstantin Belousov vm_pageout_oom(int shortage) 15052025d69bSKonstantin Belousov { 15062025d69bSKonstantin Belousov struct proc *p, *bigproc; 15072025d69bSKonstantin Belousov vm_offset_t size, bigsize; 15082025d69bSKonstantin Belousov struct thread *td; 15096bed074cSKonstantin Belousov struct vmspace *vm; 15102025d69bSKonstantin Belousov 15112025d69bSKonstantin Belousov /* 15121c58e4e5SJohn Baldwin * We keep the process bigproc locked once we find it to keep anyone 15131c58e4e5SJohn Baldwin * from messing with it; however, there is a possibility of 15141c58e4e5SJohn Baldwin * deadlock if process B is bigproc and one of it's child processes 15151c58e4e5SJohn Baldwin * attempts to propagate a signal to B while we are waiting for A's 15161c58e4e5SJohn Baldwin * lock while walking this list. To avoid this, we don't block on 15171c58e4e5SJohn Baldwin * the process lock but just skip a process if it is already locked. 15185663e6deSDavid Greenman */ 15195663e6deSDavid Greenman bigproc = NULL; 15205663e6deSDavid Greenman bigsize = 0; 15211005a129SJohn Baldwin sx_slock(&allproc_lock); 1522e602ba25SJulian Elischer FOREACH_PROC_IN_SYSTEM(p) { 1523e602ba25SJulian Elischer int breakout; 1524dcbcd518SBruce Evans 152571943c3dSKonstantin Belousov PROC_LOCK(p); 152671943c3dSKonstantin Belousov 15271c58e4e5SJohn Baldwin /* 15283f1c4c4fSKonstantin Belousov * If this is a system, protected or killed process, skip it. 15295663e6deSDavid Greenman */ 153071943c3dSKonstantin Belousov if (p->p_state != PRS_NORMAL || (p->p_flag & (P_INEXEC | 153171943c3dSKonstantin Belousov P_PROTECTED | P_SYSTEM | P_WEXIT)) != 0 || 153271943c3dSKonstantin Belousov p->p_pid == 1 || P_KILLED(p) || 153371943c3dSKonstantin Belousov (p->p_pid < 48 && swap_pager_avail != 0)) { 15348606d880SJohn Baldwin PROC_UNLOCK(p); 15355663e6deSDavid Greenman continue; 15365663e6deSDavid Greenman } 15375663e6deSDavid Greenman /* 1538dcbcd518SBruce Evans * If the process is in a non-running type state, 1539e602ba25SJulian Elischer * don't touch it. Check all the threads individually. 15405663e6deSDavid Greenman */ 1541e602ba25SJulian Elischer breakout = 0; 1542e602ba25SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 1543982d11f8SJeff Roberson thread_lock(td); 154471fad9fdSJulian Elischer if (!TD_ON_RUNQ(td) && 154571fad9fdSJulian Elischer !TD_IS_RUNNING(td) && 1546f497cda2SEdward Tomasz Napierala !TD_IS_SLEEPING(td) && 1547f497cda2SEdward Tomasz Napierala !TD_IS_SUSPENDED(td)) { 1548982d11f8SJeff Roberson thread_unlock(td); 1549e602ba25SJulian Elischer breakout = 1; 1550e602ba25SJulian Elischer break; 1551e602ba25SJulian Elischer } 1552982d11f8SJeff Roberson thread_unlock(td); 1553e602ba25SJulian Elischer } 1554e602ba25SJulian Elischer if (breakout) { 15551c58e4e5SJohn Baldwin PROC_UNLOCK(p); 15565663e6deSDavid Greenman continue; 15575663e6deSDavid Greenman } 15585663e6deSDavid Greenman /* 15595663e6deSDavid Greenman * get the process size 15605663e6deSDavid Greenman */ 15616bed074cSKonstantin Belousov vm = vmspace_acquire_ref(p); 15626bed074cSKonstantin Belousov if (vm == NULL) { 15636bed074cSKonstantin Belousov PROC_UNLOCK(p); 15646bed074cSKonstantin Belousov continue; 15656bed074cSKonstantin Belousov } 156671943c3dSKonstantin Belousov _PHOLD(p); 15676bed074cSKonstantin Belousov if (!vm_map_trylock_read(&vm->vm_map)) { 156871943c3dSKonstantin Belousov _PRELE(p); 156972d97679SDavid Schultz PROC_UNLOCK(p); 157071943c3dSKonstantin Belousov vmspace_free(vm); 157172d97679SDavid Schultz continue; 157272d97679SDavid Schultz } 157371943c3dSKonstantin Belousov PROC_UNLOCK(p); 15747981aa24SKonstantin Belousov size = vmspace_swap_count(vm); 15756bed074cSKonstantin Belousov vm_map_unlock_read(&vm->vm_map); 15762025d69bSKonstantin Belousov if (shortage == VM_OOM_MEM) 15776bed074cSKonstantin Belousov size += vmspace_resident_count(vm); 15786bed074cSKonstantin Belousov vmspace_free(vm); 15795663e6deSDavid Greenman /* 15805663e6deSDavid Greenman * if the this process is bigger than the biggest one 15815663e6deSDavid Greenman * remember it. 15825663e6deSDavid Greenman */ 15835663e6deSDavid Greenman if (size > bigsize) { 15841c58e4e5SJohn Baldwin if (bigproc != NULL) 158571943c3dSKonstantin Belousov PRELE(bigproc); 15865663e6deSDavid Greenman bigproc = p; 15875663e6deSDavid Greenman bigsize = size; 158871943c3dSKonstantin Belousov } else { 158971943c3dSKonstantin Belousov PRELE(p); 159071943c3dSKonstantin Belousov } 15915663e6deSDavid Greenman } 15921005a129SJohn Baldwin sx_sunlock(&allproc_lock); 15935663e6deSDavid Greenman if (bigproc != NULL) { 15948311a2b8SWill Andrews if (vm_panic_on_oom != 0) 15958311a2b8SWill Andrews panic("out of swap space"); 159671943c3dSKonstantin Belousov PROC_LOCK(bigproc); 1597729b1e51SDavid Greenman killproc(bigproc, "out of swap space"); 1598fa885116SJulian Elischer sched_nice(bigproc, PRIO_MIN); 159971943c3dSKonstantin Belousov _PRELE(bigproc); 16001c58e4e5SJohn Baldwin PROC_UNLOCK(bigproc); 160144f1c916SBryan Drewery wakeup(&vm_cnt.v_free_count); 16025663e6deSDavid Greenman } 16035663e6deSDavid Greenman } 160426f9a767SRodney W. Grimes 1605449c2e92SKonstantin Belousov static void 1606449c2e92SKonstantin Belousov vm_pageout_worker(void *arg) 1607449c2e92SKonstantin Belousov { 1608449c2e92SKonstantin Belousov struct vm_domain *domain; 1609949c9186SKonstantin Belousov int domidx; 1610449c2e92SKonstantin Belousov 1611449c2e92SKonstantin Belousov domidx = (uintptr_t)arg; 1612449c2e92SKonstantin Belousov domain = &vm_dom[domidx]; 1613449c2e92SKonstantin Belousov 1614449c2e92SKonstantin Belousov /* 1615949c9186SKonstantin Belousov * XXXKIB It could be useful to bind pageout daemon threads to 1616949c9186SKonstantin Belousov * the cores belonging to the domain, from which vm_page_array 1617949c9186SKonstantin Belousov * is allocated. 1618449c2e92SKonstantin Belousov */ 1619449c2e92SKonstantin Belousov 1620449c2e92SKonstantin Belousov KASSERT(domain->vmd_segs != 0, ("domain without segments")); 1621449c2e92SKonstantin Belousov vm_pageout_init_marker(&domain->vmd_marker, PQ_INACTIVE); 1622449c2e92SKonstantin Belousov 1623449c2e92SKonstantin Belousov /* 1624449c2e92SKonstantin Belousov * The pageout daemon worker is never done, so loop forever. 1625449c2e92SKonstantin Belousov */ 1626449c2e92SKonstantin Belousov while (TRUE) { 1627449c2e92SKonstantin Belousov /* 1628449c2e92SKonstantin Belousov * If we have enough free memory, wakeup waiters. Do 1629449c2e92SKonstantin Belousov * not clear vm_pages_needed until we reach our target, 1630449c2e92SKonstantin Belousov * otherwise we may be woken up over and over again and 1631449c2e92SKonstantin Belousov * waste a lot of cpu. 1632449c2e92SKonstantin Belousov */ 1633449c2e92SKonstantin Belousov mtx_lock(&vm_page_queue_free_mtx); 1634449c2e92SKonstantin Belousov if (vm_pages_needed && !vm_page_count_min()) { 1635449c2e92SKonstantin Belousov if (!vm_paging_needed()) 1636449c2e92SKonstantin Belousov vm_pages_needed = 0; 163744f1c916SBryan Drewery wakeup(&vm_cnt.v_free_count); 1638449c2e92SKonstantin Belousov } 1639449c2e92SKonstantin Belousov if (vm_pages_needed) { 1640449c2e92SKonstantin Belousov /* 1641449c2e92SKonstantin Belousov * Still not done, take a second pass without waiting 1642449c2e92SKonstantin Belousov * (unlimited dirty cleaning), otherwise sleep a bit 1643449c2e92SKonstantin Belousov * and try again. 1644449c2e92SKonstantin Belousov */ 1645449c2e92SKonstantin Belousov if (domain->vmd_pass > 1) 1646449c2e92SKonstantin Belousov msleep(&vm_pages_needed, 1647449c2e92SKonstantin Belousov &vm_page_queue_free_mtx, PVM, "psleep", 1648449c2e92SKonstantin Belousov hz / 2); 1649449c2e92SKonstantin Belousov } else { 1650449c2e92SKonstantin Belousov /* 1651d9e23210SJeff Roberson * Good enough, sleep until required to refresh 1652d9e23210SJeff Roberson * stats. 1653449c2e92SKonstantin Belousov */ 1654449c2e92SKonstantin Belousov domain->vmd_pass = 0; 1655d9e23210SJeff Roberson msleep(&vm_pages_needed, &vm_page_queue_free_mtx, 1656d9e23210SJeff Roberson PVM, "psleep", hz); 1657d9e23210SJeff Roberson 1658449c2e92SKonstantin Belousov } 1659d9e23210SJeff Roberson if (vm_pages_needed) { 166044f1c916SBryan Drewery vm_cnt.v_pdwakeups++; 1661d9e23210SJeff Roberson domain->vmd_pass++; 1662d9e23210SJeff Roberson } 1663449c2e92SKonstantin Belousov mtx_unlock(&vm_page_queue_free_mtx); 1664449c2e92SKonstantin Belousov vm_pageout_scan(domain, domain->vmd_pass); 1665449c2e92SKonstantin Belousov } 1666449c2e92SKonstantin Belousov } 1667449c2e92SKonstantin Belousov 1668df8bae1dSRodney W. Grimes /* 16694d19f4adSSteven Hartland * vm_pageout_init initialises basic pageout daemon settings. 1670df8bae1dSRodney W. Grimes */ 16712b14f991SJulian Elischer static void 16724d19f4adSSteven Hartland vm_pageout_init(void) 1673df8bae1dSRodney W. Grimes { 1674df8bae1dSRodney W. Grimes /* 1675df8bae1dSRodney W. Grimes * Initialize some paging parameters. 1676df8bae1dSRodney W. Grimes */ 167744f1c916SBryan Drewery vm_cnt.v_interrupt_free_min = 2; 167844f1c916SBryan Drewery if (vm_cnt.v_page_count < 2000) 1679f35329acSJohn Dyson vm_pageout_page_count = 8; 1680f6b04d2bSDavid Greenman 168145ae1d91SAlan Cox /* 168245ae1d91SAlan Cox * v_free_reserved needs to include enough for the largest 168345ae1d91SAlan Cox * swap pager structures plus enough for any pv_entry structs 168445ae1d91SAlan Cox * when paging. 168545ae1d91SAlan Cox */ 168644f1c916SBryan Drewery if (vm_cnt.v_page_count > 1024) 168744f1c916SBryan Drewery vm_cnt.v_free_min = 4 + (vm_cnt.v_page_count - 1024) / 200; 16882feb50bfSAttilio Rao else 168944f1c916SBryan Drewery vm_cnt.v_free_min = 4; 169044f1c916SBryan Drewery vm_cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE + 169144f1c916SBryan Drewery vm_cnt.v_interrupt_free_min; 169244f1c916SBryan Drewery vm_cnt.v_free_reserved = vm_pageout_page_count + 169344f1c916SBryan Drewery vm_cnt.v_pageout_free_min + (vm_cnt.v_page_count / 768); 169444f1c916SBryan Drewery vm_cnt.v_free_severe = vm_cnt.v_free_min / 2; 169544f1c916SBryan Drewery vm_cnt.v_free_target = 4 * vm_cnt.v_free_min + vm_cnt.v_free_reserved; 169644f1c916SBryan Drewery vm_cnt.v_free_min += vm_cnt.v_free_reserved; 169744f1c916SBryan Drewery vm_cnt.v_free_severe += vm_cnt.v_free_reserved; 169844f1c916SBryan Drewery vm_cnt.v_inactive_target = (3 * vm_cnt.v_free_target) / 2; 169944f1c916SBryan Drewery if (vm_cnt.v_inactive_target > vm_cnt.v_free_count / 3) 170044f1c916SBryan Drewery vm_cnt.v_inactive_target = vm_cnt.v_free_count / 3; 1701df8bae1dSRodney W. Grimes 1702d9e23210SJeff Roberson /* 1703d9e23210SJeff Roberson * Set the default wakeup threshold to be 10% above the minimum 1704d9e23210SJeff Roberson * page limit. This keeps the steady state out of shortfall. 1705d9e23210SJeff Roberson */ 170644f1c916SBryan Drewery vm_pageout_wakeup_thresh = (vm_cnt.v_free_min / 10) * 11; 1707d9e23210SJeff Roberson 1708d9e23210SJeff Roberson /* 1709d9e23210SJeff Roberson * Set interval in seconds for active scan. We want to visit each 1710c9612b2dSJeff Roberson * page at least once every ten minutes. This is to prevent worst 1711c9612b2dSJeff Roberson * case paging behaviors with stale active LRU. 1712d9e23210SJeff Roberson */ 1713d9e23210SJeff Roberson if (vm_pageout_update_period == 0) 1714c9612b2dSJeff Roberson vm_pageout_update_period = 600; 1715d9e23210SJeff Roberson 1716df8bae1dSRodney W. Grimes /* XXX does not really belong here */ 1717df8bae1dSRodney W. Grimes if (vm_page_max_wired == 0) 171844f1c916SBryan Drewery vm_page_max_wired = vm_cnt.v_free_count / 3; 17194d19f4adSSteven Hartland } 17204d19f4adSSteven Hartland 17214d19f4adSSteven Hartland /* 17224d19f4adSSteven Hartland * vm_pageout is the high level pageout daemon. 17234d19f4adSSteven Hartland */ 17244d19f4adSSteven Hartland static void 17254d19f4adSSteven Hartland vm_pageout(void) 17264d19f4adSSteven Hartland { 17274d19f4adSSteven Hartland #if MAXMEMDOM > 1 17284d19f4adSSteven Hartland int error, i; 17294d19f4adSSteven Hartland #endif 1730df8bae1dSRodney W. Grimes 173124a1cce3SDavid Greenman swap_pager_swap_init(); 1732449c2e92SKonstantin Belousov #if MAXMEMDOM > 1 1733449c2e92SKonstantin Belousov for (i = 1; i < vm_ndomains; i++) { 1734449c2e92SKonstantin Belousov error = kthread_add(vm_pageout_worker, (void *)(uintptr_t)i, 1735449c2e92SKonstantin Belousov curproc, NULL, 0, 0, "dom%d", i); 1736449c2e92SKonstantin Belousov if (error != 0) { 1737449c2e92SKonstantin Belousov panic("starting pageout for domain %d, error %d\n", 1738449c2e92SKonstantin Belousov i, error); 1739dc2efb27SJohn Dyson } 1740f919ebdeSDavid Greenman } 1741449c2e92SKonstantin Belousov #endif 1742d395270dSDimitry Andric vm_pageout_worker((void *)(uintptr_t)0); 1743df8bae1dSRodney W. Grimes } 174426f9a767SRodney W. Grimes 17456b4b77adSAlan Cox /* 1746e9f995d8SAlan Cox * Unless the free page queue lock is held by the caller, this function 17476b4b77adSAlan Cox * should be regarded as advisory. Specifically, the caller should 174844f1c916SBryan Drewery * not msleep() on &vm_cnt.v_free_count following this function unless 1749e9f995d8SAlan Cox * the free page queue lock is held until the msleep() is performed. 17506b4b77adSAlan Cox */ 1751e0c5a895SJohn Dyson void 17524a365329SAndrey Zonov pagedaemon_wakeup(void) 1753e0c5a895SJohn Dyson { 1754a1c0a785SAlan Cox 1755b40ce416SJulian Elischer if (!vm_pages_needed && curthread->td_proc != pageproc) { 1756a1c0a785SAlan Cox vm_pages_needed = 1; 1757e0c5a895SJohn Dyson wakeup(&vm_pages_needed); 1758e0c5a895SJohn Dyson } 1759e0c5a895SJohn Dyson } 1760e0c5a895SJohn Dyson 176138efa82bSJohn Dyson #if !defined(NO_SWAPPING) 17625afce282SDavid Greenman static void 176397824da3SAlan Cox vm_req_vmdaemon(int req) 17645afce282SDavid Greenman { 17655afce282SDavid Greenman static int lastrun = 0; 17665afce282SDavid Greenman 176797824da3SAlan Cox mtx_lock(&vm_daemon_mtx); 176897824da3SAlan Cox vm_pageout_req_swapout |= req; 1769b18bfc3dSJohn Dyson if ((ticks > (lastrun + hz)) || (ticks < lastrun)) { 17705afce282SDavid Greenman wakeup(&vm_daemon_needed); 17715afce282SDavid Greenman lastrun = ticks; 17725afce282SDavid Greenman } 177397824da3SAlan Cox mtx_unlock(&vm_daemon_mtx); 17745afce282SDavid Greenman } 17755afce282SDavid Greenman 17762b14f991SJulian Elischer static void 17774a365329SAndrey Zonov vm_daemon(void) 17780d94caffSDavid Greenman { 177991d5354aSJohn Baldwin struct rlimit rsslim; 1780dcbcd518SBruce Evans struct proc *p; 1781dcbcd518SBruce Evans struct thread *td; 17826bed074cSKonstantin Belousov struct vmspace *vm; 1783099e7e95SEdward Tomasz Napierala int breakout, swapout_flags, tryagain, attempts; 1784afcc55f3SEdward Tomasz Napierala #ifdef RACCT 1785099e7e95SEdward Tomasz Napierala uint64_t rsize, ravailable; 1786afcc55f3SEdward Tomasz Napierala #endif 17870d94caffSDavid Greenman 17882fe6e4d7SDavid Greenman while (TRUE) { 178997824da3SAlan Cox mtx_lock(&vm_daemon_mtx); 1790099e7e95SEdward Tomasz Napierala #ifdef RACCT 1791099e7e95SEdward Tomasz Napierala msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", hz); 1792099e7e95SEdward Tomasz Napierala #else 179397824da3SAlan Cox msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", 0); 1794099e7e95SEdward Tomasz Napierala #endif 179597824da3SAlan Cox swapout_flags = vm_pageout_req_swapout; 17964c1f8ee9SDavid Greenman vm_pageout_req_swapout = 0; 179797824da3SAlan Cox mtx_unlock(&vm_daemon_mtx); 179897824da3SAlan Cox if (swapout_flags) 179997824da3SAlan Cox swapout_procs(swapout_flags); 180097824da3SAlan Cox 18012fe6e4d7SDavid Greenman /* 18020d94caffSDavid Greenman * scan the processes for exceeding their rlimits or if 18030d94caffSDavid Greenman * process is swapped out -- deactivate pages 18042fe6e4d7SDavid Greenman */ 1805099e7e95SEdward Tomasz Napierala tryagain = 0; 1806099e7e95SEdward Tomasz Napierala attempts = 0; 1807099e7e95SEdward Tomasz Napierala again: 1808099e7e95SEdward Tomasz Napierala attempts++; 18091005a129SJohn Baldwin sx_slock(&allproc_lock); 1810f67af5c9SXin LI FOREACH_PROC_IN_SYSTEM(p) { 1811fe2144fdSLuoqi Chen vm_pindex_t limit, size; 18122fe6e4d7SDavid Greenman 18132fe6e4d7SDavid Greenman /* 18142fe6e4d7SDavid Greenman * if this is a system process or if we have already 18152fe6e4d7SDavid Greenman * looked at this process, skip it. 18162fe6e4d7SDavid Greenman */ 1817897ecacdSJohn Baldwin PROC_LOCK(p); 18188e6fa660SJohn Baldwin if (p->p_state != PRS_NORMAL || 18198e6fa660SJohn Baldwin p->p_flag & (P_INEXEC | P_SYSTEM | P_WEXIT)) { 1820897ecacdSJohn Baldwin PROC_UNLOCK(p); 18212fe6e4d7SDavid Greenman continue; 18222fe6e4d7SDavid Greenman } 18232fe6e4d7SDavid Greenman /* 18242fe6e4d7SDavid Greenman * if the process is in a non-running type state, 18252fe6e4d7SDavid Greenman * don't touch it. 18262fe6e4d7SDavid Greenman */ 1827e602ba25SJulian Elischer breakout = 0; 1828e602ba25SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 1829982d11f8SJeff Roberson thread_lock(td); 183071fad9fdSJulian Elischer if (!TD_ON_RUNQ(td) && 183171fad9fdSJulian Elischer !TD_IS_RUNNING(td) && 1832f497cda2SEdward Tomasz Napierala !TD_IS_SLEEPING(td) && 1833f497cda2SEdward Tomasz Napierala !TD_IS_SUSPENDED(td)) { 1834982d11f8SJeff Roberson thread_unlock(td); 1835e602ba25SJulian Elischer breakout = 1; 1836e602ba25SJulian Elischer break; 1837e602ba25SJulian Elischer } 1838982d11f8SJeff Roberson thread_unlock(td); 1839e602ba25SJulian Elischer } 1840897ecacdSJohn Baldwin if (breakout) { 1841897ecacdSJohn Baldwin PROC_UNLOCK(p); 18422fe6e4d7SDavid Greenman continue; 18432fe6e4d7SDavid Greenman } 18442fe6e4d7SDavid Greenman /* 18452fe6e4d7SDavid Greenman * get a limit 18462fe6e4d7SDavid Greenman */ 1847dcbcd518SBruce Evans lim_rlimit(p, RLIMIT_RSS, &rsslim); 1848fe2144fdSLuoqi Chen limit = OFF_TO_IDX( 184991d5354aSJohn Baldwin qmin(rsslim.rlim_cur, rsslim.rlim_max)); 18502fe6e4d7SDavid Greenman 18512fe6e4d7SDavid Greenman /* 18520d94caffSDavid Greenman * let processes that are swapped out really be 18530d94caffSDavid Greenman * swapped out set the limit to nothing (will force a 18540d94caffSDavid Greenman * swap-out.) 18552fe6e4d7SDavid Greenman */ 1856b61ce5b0SJeff Roberson if ((p->p_flag & P_INMEM) == 0) 18570d94caffSDavid Greenman limit = 0; /* XXX */ 18586bed074cSKonstantin Belousov vm = vmspace_acquire_ref(p); 1859897ecacdSJohn Baldwin PROC_UNLOCK(p); 18606bed074cSKonstantin Belousov if (vm == NULL) 18616bed074cSKonstantin Belousov continue; 18622fe6e4d7SDavid Greenman 18636bed074cSKonstantin Belousov size = vmspace_resident_count(vm); 1864a406d8c3SEdward Tomasz Napierala if (size >= limit) { 1865fe2144fdSLuoqi Chen vm_pageout_map_deactivate_pages( 18666bed074cSKonstantin Belousov &vm->vm_map, limit); 18672fe6e4d7SDavid Greenman } 1868afcc55f3SEdward Tomasz Napierala #ifdef RACCT 1869099e7e95SEdward Tomasz Napierala rsize = IDX_TO_OFF(size); 1870099e7e95SEdward Tomasz Napierala PROC_LOCK(p); 1871099e7e95SEdward Tomasz Napierala racct_set(p, RACCT_RSS, rsize); 1872099e7e95SEdward Tomasz Napierala ravailable = racct_get_available(p, RACCT_RSS); 1873099e7e95SEdward Tomasz Napierala PROC_UNLOCK(p); 1874099e7e95SEdward Tomasz Napierala if (rsize > ravailable) { 1875099e7e95SEdward Tomasz Napierala /* 1876099e7e95SEdward Tomasz Napierala * Don't be overly aggressive; this might be 1877099e7e95SEdward Tomasz Napierala * an innocent process, and the limit could've 1878099e7e95SEdward Tomasz Napierala * been exceeded by some memory hog. Don't 1879099e7e95SEdward Tomasz Napierala * try to deactivate more than 1/4th of process' 1880099e7e95SEdward Tomasz Napierala * resident set size. 1881099e7e95SEdward Tomasz Napierala */ 1882099e7e95SEdward Tomasz Napierala if (attempts <= 8) { 1883099e7e95SEdward Tomasz Napierala if (ravailable < rsize - (rsize / 4)) 1884099e7e95SEdward Tomasz Napierala ravailable = rsize - (rsize / 4); 1885099e7e95SEdward Tomasz Napierala } 1886099e7e95SEdward Tomasz Napierala vm_pageout_map_deactivate_pages( 1887099e7e95SEdward Tomasz Napierala &vm->vm_map, OFF_TO_IDX(ravailable)); 1888099e7e95SEdward Tomasz Napierala /* Update RSS usage after paging out. */ 1889099e7e95SEdward Tomasz Napierala size = vmspace_resident_count(vm); 1890099e7e95SEdward Tomasz Napierala rsize = IDX_TO_OFF(size); 1891099e7e95SEdward Tomasz Napierala PROC_LOCK(p); 1892099e7e95SEdward Tomasz Napierala racct_set(p, RACCT_RSS, rsize); 1893099e7e95SEdward Tomasz Napierala PROC_UNLOCK(p); 1894099e7e95SEdward Tomasz Napierala if (rsize > ravailable) 1895099e7e95SEdward Tomasz Napierala tryagain = 1; 1896099e7e95SEdward Tomasz Napierala } 1897afcc55f3SEdward Tomasz Napierala #endif 18986bed074cSKonstantin Belousov vmspace_free(vm); 18992fe6e4d7SDavid Greenman } 19001005a129SJohn Baldwin sx_sunlock(&allproc_lock); 1901099e7e95SEdward Tomasz Napierala if (tryagain != 0 && attempts <= 10) 1902099e7e95SEdward Tomasz Napierala goto again; 190324a1cce3SDavid Greenman } 19042fe6e4d7SDavid Greenman } 1905a1287949SEivind Eklund #endif /* !defined(NO_SWAPPING) */ 1906