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" 79df8bae1dSRodney W. Grimes #include <sys/param.h> 8026f9a767SRodney W. Grimes #include <sys/systm.h> 81b5e8ce9fSBruce Evans #include <sys/kernel.h> 82855a310fSJeff Roberson #include <sys/eventhandler.h> 83fb919e4dSMark Murray #include <sys/lock.h> 84fb919e4dSMark Murray #include <sys/mutex.h> 8526f9a767SRodney W. Grimes #include <sys/proc.h> 869c8b8baaSPeter Wemm #include <sys/kthread.h> 870384fff8SJason Evans #include <sys/ktr.h> 8897824da3SAlan Cox #include <sys/mount.h> 89099e7e95SEdward Tomasz Napierala #include <sys/racct.h> 9026f9a767SRodney W. Grimes #include <sys/resourcevar.h> 91b43179fbSJeff Roberson #include <sys/sched.h> 92d2fc5315SPoul-Henning Kamp #include <sys/signalvar.h> 93449c2e92SKonstantin Belousov #include <sys/smp.h> 94f6b04d2bSDavid Greenman #include <sys/vnode.h> 95efeaf95aSDavid Greenman #include <sys/vmmeter.h> 9689f6b863SAttilio Rao #include <sys/rwlock.h> 971005a129SJohn Baldwin #include <sys/sx.h> 9838efa82bSJohn Dyson #include <sys/sysctl.h> 99df8bae1dSRodney W. Grimes 100df8bae1dSRodney W. Grimes #include <vm/vm.h> 101efeaf95aSDavid Greenman #include <vm/vm_param.h> 102efeaf95aSDavid Greenman #include <vm/vm_object.h> 103df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 104efeaf95aSDavid Greenman #include <vm/vm_map.h> 105df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h> 10624a1cce3SDavid Greenman #include <vm/vm_pager.h> 107449c2e92SKonstantin Belousov #include <vm/vm_phys.h> 10805f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h> 109efeaf95aSDavid Greenman #include <vm/vm_extern.h> 110670d17b5SJeff Roberson #include <vm/uma.h> 111df8bae1dSRodney W. Grimes 1122b14f991SJulian Elischer /* 1132b14f991SJulian Elischer * System initialization 1142b14f991SJulian Elischer */ 1152b14f991SJulian Elischer 1162b14f991SJulian Elischer /* the kernel process "vm_pageout"*/ 11711caded3SAlfred Perlstein static void vm_pageout(void); 118*4d19f4adSSteven Hartland static void vm_pageout_init(void); 11911caded3SAlfred Perlstein static int vm_pageout_clean(vm_page_t); 120449c2e92SKonstantin Belousov static void vm_pageout_scan(struct vm_domain *vmd, int pass); 121449c2e92SKonstantin Belousov static void vm_pageout_mightbe_oom(struct vm_domain *vmd, int pass); 12245ae1d91SAlan Cox 123*4d19f4adSSteven Hartland SYSINIT(pagedaemon_init, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, vm_pageout_init, 124*4d19f4adSSteven Hartland NULL); 125*4d19f4adSSteven Hartland 1262b14f991SJulian Elischer struct proc *pageproc; 1272b14f991SJulian Elischer 1282b14f991SJulian Elischer static struct kproc_desc page_kp = { 1292b14f991SJulian Elischer "pagedaemon", 1302b14f991SJulian Elischer vm_pageout, 1312b14f991SJulian Elischer &pageproc 1322b14f991SJulian Elischer }; 133*4d19f4adSSteven Hartland SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_SECOND, kproc_start, 134237fdd78SRobert Watson &page_kp); 1352b14f991SJulian Elischer 13638efa82bSJohn Dyson #if !defined(NO_SWAPPING) 1372b14f991SJulian Elischer /* the kernel process "vm_daemon"*/ 13811caded3SAlfred Perlstein static void vm_daemon(void); 139f708ef1bSPoul-Henning Kamp static struct proc *vmproc; 1402b14f991SJulian Elischer 1412b14f991SJulian Elischer static struct kproc_desc vm_kp = { 1422b14f991SJulian Elischer "vmdaemon", 1432b14f991SJulian Elischer vm_daemon, 1442b14f991SJulian Elischer &vmproc 1452b14f991SJulian Elischer }; 146237fdd78SRobert Watson SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp); 14738efa82bSJohn Dyson #endif 1482b14f991SJulian Elischer 1492b14f991SJulian Elischer 1508b245767SAlan Cox int vm_pages_needed; /* Event on which pageout daemon sleeps */ 1518b245767SAlan Cox int vm_pageout_deficit; /* Estimated number of pages deficit */ 1528b245767SAlan Cox int vm_pageout_pages_needed; /* flag saying that the pageout daemon needs pages */ 153d9e23210SJeff Roberson int vm_pageout_wakeup_thresh; 15426f9a767SRodney W. Grimes 15538efa82bSJohn Dyson #if !defined(NO_SWAPPING) 156f708ef1bSPoul-Henning Kamp static int vm_pageout_req_swapout; /* XXX */ 157f708ef1bSPoul-Henning Kamp static int vm_daemon_needed; 15897824da3SAlan Cox static struct mtx vm_daemon_mtx; 15997824da3SAlan Cox /* Allow for use by vm_pageout before vm_daemon is initialized. */ 16097824da3SAlan Cox MTX_SYSINIT(vm_daemon, &vm_daemon_mtx, "vm daemon", MTX_DEF); 16138efa82bSJohn Dyson #endif 1622b6b0df7SMatthew Dillon static int vm_max_launder = 32; 163d9e23210SJeff Roberson static int vm_pageout_update_period; 1644a365329SAndrey Zonov static int defer_swap_pageouts; 1654a365329SAndrey Zonov static int disable_swap_pageouts; 166c9612b2dSJeff Roberson static int lowmem_period = 10; 167c9612b2dSJeff Roberson static int lowmem_ticks; 16870111b90SJohn Dyson 16938efa82bSJohn Dyson #if defined(NO_SWAPPING) 170303b270bSEivind Eklund static int vm_swap_enabled = 0; 171303b270bSEivind Eklund static int vm_swap_idle_enabled = 0; 17238efa82bSJohn Dyson #else 173303b270bSEivind Eklund static int vm_swap_enabled = 1; 174303b270bSEivind Eklund static int vm_swap_idle_enabled = 0; 17538efa82bSJohn Dyson #endif 17638efa82bSJohn Dyson 177d9e23210SJeff Roberson SYSCTL_INT(_vm, OID_AUTO, pageout_wakeup_thresh, 178d9e23210SJeff Roberson CTLFLAG_RW, &vm_pageout_wakeup_thresh, 0, 179d9e23210SJeff Roberson "free page threshold for waking up the pageout daemon"); 180d9e23210SJeff Roberson 1812b6b0df7SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, max_launder, 1822b6b0df7SMatthew Dillon CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout"); 18338efa82bSJohn Dyson 184d9e23210SJeff Roberson SYSCTL_INT(_vm, OID_AUTO, pageout_update_period, 185d9e23210SJeff Roberson CTLFLAG_RW, &vm_pageout_update_period, 0, 186d9e23210SJeff Roberson "Maximum active LRU update period"); 18753636869SAndrey Zonov 188c9612b2dSJeff Roberson SYSCTL_INT(_vm, OID_AUTO, lowmem_period, CTLFLAG_RW, &lowmem_period, 0, 189c9612b2dSJeff Roberson "Low memory callback period"); 190c9612b2dSJeff Roberson 19138efa82bSJohn Dyson #if defined(NO_SWAPPING) 192ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 1936bd9cb1cSTom Rhodes CTLFLAG_RD, &vm_swap_enabled, 0, "Enable entire process swapout"); 194ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 1956bd9cb1cSTom Rhodes CTLFLAG_RD, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria"); 19638efa82bSJohn Dyson #else 197ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 198b0359e2cSPeter Wemm CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout"); 199ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 200b0359e2cSPeter Wemm CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria"); 20138efa82bSJohn Dyson #endif 20226f9a767SRodney W. Grimes 203ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts, 204b0359e2cSPeter Wemm CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem"); 20512ac6a1dSJohn Dyson 206ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts, 207b0359e2cSPeter Wemm CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages"); 20812ac6a1dSJohn Dyson 20923b59018SMatthew Dillon static int pageout_lock_miss; 21023b59018SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss, 21123b59018SMatthew Dillon CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout"); 21223b59018SMatthew Dillon 213ffc82b0aSJohn Dyson #define VM_PAGEOUT_PAGE_COUNT 16 214bbc0ec52SDavid Greenman int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT; 215df8bae1dSRodney W. Grimes 216c3cb3e12SDavid Greenman int vm_page_max_wired; /* XXX max # of wired pages system-wide */ 2175dfc2870SAlan Cox SYSCTL_INT(_vm, OID_AUTO, max_wired, 2185dfc2870SAlan Cox CTLFLAG_RW, &vm_page_max_wired, 0, "System-wide limit to wired page count"); 219df8bae1dSRodney W. Grimes 22085eeca35SAlan Cox static boolean_t vm_pageout_fallback_object_lock(vm_page_t, vm_page_t *); 221449c2e92SKonstantin Belousov static boolean_t vm_pageout_launder(struct vm_pagequeue *pq, int, vm_paddr_t, 222449c2e92SKonstantin Belousov vm_paddr_t); 22338efa82bSJohn Dyson #if !defined(NO_SWAPPING) 224ecf6279fSAlan Cox static void vm_pageout_map_deactivate_pages(vm_map_t, long); 225ecf6279fSAlan Cox static void vm_pageout_object_deactivate_pages(pmap_t, vm_object_t, long); 22697824da3SAlan Cox static void vm_req_vmdaemon(int req); 22738efa82bSJohn Dyson #endif 22885eeca35SAlan Cox static boolean_t vm_pageout_page_lock(vm_page_t, vm_page_t *); 229cd41fc12SDavid Greenman 230a8229fa3SAlan Cox /* 231a8229fa3SAlan Cox * Initialize a dummy page for marking the caller's place in the specified 232a8229fa3SAlan Cox * paging queue. In principle, this function only needs to set the flag 233c7aebda8SAttilio Rao * PG_MARKER. Nonetheless, it wirte busies and initializes the hold count 234c7aebda8SAttilio Rao * to one as safety precautions. 235a8229fa3SAlan Cox */ 2368c616246SKonstantin Belousov static void 2378c616246SKonstantin Belousov vm_pageout_init_marker(vm_page_t marker, u_short queue) 2388c616246SKonstantin Belousov { 2398c616246SKonstantin Belousov 2408c616246SKonstantin Belousov bzero(marker, sizeof(*marker)); 241a8229fa3SAlan Cox marker->flags = PG_MARKER; 242c7aebda8SAttilio Rao marker->busy_lock = VPB_SINGLE_EXCLUSIVER; 2438c616246SKonstantin Belousov marker->queue = queue; 244a8229fa3SAlan Cox marker->hold_count = 1; 2458c616246SKonstantin Belousov } 2468c616246SKonstantin Belousov 24726f9a767SRodney W. Grimes /* 2488dbca793STor Egge * vm_pageout_fallback_object_lock: 2498dbca793STor Egge * 25089f6b863SAttilio Rao * Lock vm object currently associated with `m'. VM_OBJECT_TRYWLOCK is 2518dbca793STor Egge * known to have failed and page queue must be either PQ_ACTIVE or 2528dbca793STor Egge * PQ_INACTIVE. To avoid lock order violation, unlock the page queues 2538dbca793STor Egge * while locking the vm object. Use marker page to detect page queue 2548dbca793STor Egge * changes and maintain notion of next page on page queue. Return 2558dbca793STor Egge * TRUE if no changes were detected, FALSE otherwise. vm object is 2568dbca793STor Egge * locked on return. 2578dbca793STor Egge * 2588dbca793STor Egge * This function depends on both the lock portion of struct vm_object 2598dbca793STor Egge * and normal struct vm_page being type stable. 2608dbca793STor Egge */ 26185eeca35SAlan Cox static boolean_t 2628dbca793STor Egge vm_pageout_fallback_object_lock(vm_page_t m, vm_page_t *next) 2638dbca793STor Egge { 2648dbca793STor Egge struct vm_page marker; 2658d220203SAlan Cox struct vm_pagequeue *pq; 2668dbca793STor Egge boolean_t unchanged; 2678dbca793STor Egge u_short queue; 2688dbca793STor Egge vm_object_t object; 2698dbca793STor Egge 2708dbca793STor Egge queue = m->queue; 2718c616246SKonstantin Belousov vm_pageout_init_marker(&marker, queue); 272449c2e92SKonstantin Belousov pq = vm_page_pagequeue(m); 2738dbca793STor Egge object = m->object; 2748dbca793STor Egge 275c325e866SKonstantin Belousov TAILQ_INSERT_AFTER(&pq->pq_pl, m, &marker, plinks.q); 2768d220203SAlan Cox vm_pagequeue_unlock(pq); 2772965a453SKip Macy vm_page_unlock(m); 27889f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 2792965a453SKip Macy vm_page_lock(m); 2808d220203SAlan Cox vm_pagequeue_lock(pq); 2818dbca793STor Egge 2828dbca793STor Egge /* Page queue might have changed. */ 283c325e866SKonstantin Belousov *next = TAILQ_NEXT(&marker, plinks.q); 2848dbca793STor Egge unchanged = (m->queue == queue && 2858dbca793STor Egge m->object == object && 286c325e866SKonstantin Belousov &marker == TAILQ_NEXT(m, plinks.q)); 287c325e866SKonstantin Belousov TAILQ_REMOVE(&pq->pq_pl, &marker, plinks.q); 2888dbca793STor Egge return (unchanged); 2898dbca793STor Egge } 2908dbca793STor Egge 2918dbca793STor Egge /* 2928c616246SKonstantin Belousov * Lock the page while holding the page queue lock. Use marker page 2938c616246SKonstantin Belousov * to detect page queue changes and maintain notion of next page on 2948c616246SKonstantin Belousov * page queue. Return TRUE if no changes were detected, FALSE 2958c616246SKonstantin Belousov * otherwise. The page is locked on return. The page queue lock might 2968c616246SKonstantin Belousov * be dropped and reacquired. 2978c616246SKonstantin Belousov * 2988c616246SKonstantin Belousov * This function depends on normal struct vm_page being type stable. 2998c616246SKonstantin Belousov */ 30085eeca35SAlan Cox static boolean_t 3018c616246SKonstantin Belousov vm_pageout_page_lock(vm_page_t m, vm_page_t *next) 3028c616246SKonstantin Belousov { 3038c616246SKonstantin Belousov struct vm_page marker; 3048d220203SAlan Cox struct vm_pagequeue *pq; 3058c616246SKonstantin Belousov boolean_t unchanged; 3068c616246SKonstantin Belousov u_short queue; 3078c616246SKonstantin Belousov 3088c616246SKonstantin Belousov vm_page_lock_assert(m, MA_NOTOWNED); 3098c616246SKonstantin Belousov if (vm_page_trylock(m)) 3108c616246SKonstantin Belousov return (TRUE); 3118c616246SKonstantin Belousov 3128c616246SKonstantin Belousov queue = m->queue; 3138c616246SKonstantin Belousov vm_pageout_init_marker(&marker, queue); 314449c2e92SKonstantin Belousov pq = vm_page_pagequeue(m); 3158c616246SKonstantin Belousov 316c325e866SKonstantin Belousov TAILQ_INSERT_AFTER(&pq->pq_pl, m, &marker, plinks.q); 3178d220203SAlan Cox vm_pagequeue_unlock(pq); 3188c616246SKonstantin Belousov vm_page_lock(m); 3198d220203SAlan Cox vm_pagequeue_lock(pq); 3208c616246SKonstantin Belousov 3218c616246SKonstantin Belousov /* Page queue might have changed. */ 322c325e866SKonstantin Belousov *next = TAILQ_NEXT(&marker, plinks.q); 323c325e866SKonstantin Belousov unchanged = (m->queue == queue && &marker == TAILQ_NEXT(m, plinks.q)); 324c325e866SKonstantin Belousov TAILQ_REMOVE(&pq->pq_pl, &marker, plinks.q); 3258c616246SKonstantin Belousov return (unchanged); 3268c616246SKonstantin Belousov } 3278c616246SKonstantin Belousov 3288c616246SKonstantin Belousov /* 32926f9a767SRodney W. Grimes * vm_pageout_clean: 33024a1cce3SDavid Greenman * 3310d94caffSDavid Greenman * Clean the page and remove it from the laundry. 33226f9a767SRodney W. Grimes * 3330d94caffSDavid Greenman * We set the busy bit to cause potential page faults on this page to 3341c7c3c6aSMatthew Dillon * block. Note the careful timing, however, the busy bit isn't set till 3351c7c3c6aSMatthew Dillon * late and we cannot do anything that will mess with the page. 33626f9a767SRodney W. Grimes */ 3373af76890SPoul-Henning Kamp static int 3382965a453SKip Macy vm_pageout_clean(vm_page_t m) 33924a1cce3SDavid Greenman { 34054d92145SMatthew Dillon vm_object_t object; 34191b4f427SAlan Cox vm_page_t mc[2*vm_pageout_page_count], pb, ps; 3423562af12SAlan Cox int pageout_count; 34390ecac61SMatthew Dillon int ib, is, page_base; 344a316d390SJohn Dyson vm_pindex_t pindex = m->pindex; 34526f9a767SRodney W. Grimes 34695976f3fSAlan Cox vm_page_lock_assert(m, MA_OWNED); 34717f6a17bSAlan Cox object = m->object; 34889f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 3490cddd8f0SMatthew Dillon 35026f9a767SRodney W. Grimes /* 3511c7c3c6aSMatthew Dillon * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP 3521c7c3c6aSMatthew Dillon * with the new swapper, but we could have serious problems paging 3531c7c3c6aSMatthew Dillon * out other object types if there is insufficient memory. 3541c7c3c6aSMatthew Dillon * 3551c7c3c6aSMatthew Dillon * Unfortunately, checking free memory here is far too late, so the 3561c7c3c6aSMatthew Dillon * check has been moved up a procedural level. 3571c7c3c6aSMatthew Dillon */ 3581c7c3c6aSMatthew Dillon 35924a1cce3SDavid Greenman /* 3609e897b1bSAlan Cox * Can't clean the page if it's busy or held. 36124a1cce3SDavid Greenman */ 362c7aebda8SAttilio Rao vm_page_assert_unbusied(m); 36395976f3fSAlan Cox KASSERT(m->hold_count == 0, ("vm_pageout_clean: page %p is held", m)); 36417f6a17bSAlan Cox vm_page_unlock(m); 3650d94caffSDavid Greenman 36691b4f427SAlan Cox mc[vm_pageout_page_count] = pb = ps = m; 36726f9a767SRodney W. Grimes pageout_count = 1; 368f35329acSJohn Dyson page_base = vm_pageout_page_count; 36990ecac61SMatthew Dillon ib = 1; 37090ecac61SMatthew Dillon is = 1; 37190ecac61SMatthew Dillon 37224a1cce3SDavid Greenman /* 37324a1cce3SDavid Greenman * Scan object for clusterable pages. 37424a1cce3SDavid Greenman * 37524a1cce3SDavid Greenman * We can cluster ONLY if: ->> the page is NOT 37624a1cce3SDavid Greenman * clean, wired, busy, held, or mapped into a 37724a1cce3SDavid Greenman * buffer, and one of the following: 37824a1cce3SDavid Greenman * 1) The page is inactive, or a seldom used 37924a1cce3SDavid Greenman * active page. 38024a1cce3SDavid Greenman * -or- 38124a1cce3SDavid Greenman * 2) we force the issue. 38290ecac61SMatthew Dillon * 38390ecac61SMatthew Dillon * During heavy mmap/modification loads the pageout 38490ecac61SMatthew Dillon * daemon can really fragment the underlying file 38590ecac61SMatthew Dillon * due to flushing pages out of order and not trying 38690ecac61SMatthew Dillon * align the clusters (which leave sporatic out-of-order 38790ecac61SMatthew Dillon * holes). To solve this problem we do the reverse scan 38890ecac61SMatthew Dillon * first and attempt to align our cluster, then do a 38990ecac61SMatthew Dillon * forward scan if room remains. 39024a1cce3SDavid Greenman */ 39190ecac61SMatthew Dillon more: 39290ecac61SMatthew Dillon while (ib && pageout_count < vm_pageout_page_count) { 39324a1cce3SDavid Greenman vm_page_t p; 394f6b04d2bSDavid Greenman 39590ecac61SMatthew Dillon if (ib > pindex) { 39690ecac61SMatthew Dillon ib = 0; 39790ecac61SMatthew Dillon break; 398f6b04d2bSDavid Greenman } 39990ecac61SMatthew Dillon 400c7aebda8SAttilio Rao if ((p = vm_page_prev(pb)) == NULL || vm_page_busied(p)) { 40190ecac61SMatthew Dillon ib = 0; 40290ecac61SMatthew Dillon break; 403f6b04d2bSDavid Greenman } 4042965a453SKip Macy vm_page_lock(p); 40524a1cce3SDavid Greenman vm_page_test_dirty(p); 40626f4eea5SAlan Cox if (p->dirty == 0 || 40790ecac61SMatthew Dillon p->queue != PQ_INACTIVE || 40857601bcbSMatthew Dillon p->hold_count != 0) { /* may be undergoing I/O */ 4092965a453SKip Macy vm_page_unlock(p); 41090ecac61SMatthew Dillon ib = 0; 41124a1cce3SDavid Greenman break; 412f6b04d2bSDavid Greenman } 4132965a453SKip Macy vm_page_unlock(p); 41491b4f427SAlan Cox mc[--page_base] = pb = p; 41590ecac61SMatthew Dillon ++pageout_count; 41690ecac61SMatthew Dillon ++ib; 41724a1cce3SDavid Greenman /* 41890ecac61SMatthew Dillon * alignment boundry, stop here and switch directions. Do 41990ecac61SMatthew Dillon * not clear ib. 42024a1cce3SDavid Greenman */ 42190ecac61SMatthew Dillon if ((pindex - (ib - 1)) % vm_pageout_page_count == 0) 42290ecac61SMatthew Dillon break; 42324a1cce3SDavid Greenman } 42490ecac61SMatthew Dillon 42590ecac61SMatthew Dillon while (pageout_count < vm_pageout_page_count && 42690ecac61SMatthew Dillon pindex + is < object->size) { 42790ecac61SMatthew Dillon vm_page_t p; 42890ecac61SMatthew Dillon 429c7aebda8SAttilio Rao if ((p = vm_page_next(ps)) == NULL || vm_page_busied(p)) 43090ecac61SMatthew Dillon break; 4312965a453SKip Macy vm_page_lock(p); 43224a1cce3SDavid Greenman vm_page_test_dirty(p); 43326f4eea5SAlan Cox if (p->dirty == 0 || 43490ecac61SMatthew Dillon p->queue != PQ_INACTIVE || 43557601bcbSMatthew Dillon p->hold_count != 0) { /* may be undergoing I/O */ 4362965a453SKip Macy vm_page_unlock(p); 43724a1cce3SDavid Greenman break; 43824a1cce3SDavid Greenman } 4392965a453SKip Macy vm_page_unlock(p); 44091b4f427SAlan Cox mc[page_base + pageout_count] = ps = p; 44190ecac61SMatthew Dillon ++pageout_count; 44290ecac61SMatthew Dillon ++is; 44324a1cce3SDavid Greenman } 44490ecac61SMatthew Dillon 44590ecac61SMatthew Dillon /* 44690ecac61SMatthew Dillon * If we exhausted our forward scan, continue with the reverse scan 44790ecac61SMatthew Dillon * when possible, even past a page boundry. This catches boundry 44890ecac61SMatthew Dillon * conditions. 44990ecac61SMatthew Dillon */ 45090ecac61SMatthew Dillon if (ib && pageout_count < vm_pageout_page_count) 45190ecac61SMatthew Dillon goto more; 452f6b04d2bSDavid Greenman 45367bf6868SJohn Dyson /* 45467bf6868SJohn Dyson * we allow reads during pageouts... 45567bf6868SJohn Dyson */ 456126d6082SKonstantin Belousov return (vm_pageout_flush(&mc[page_base], pageout_count, 0, 0, NULL, 457126d6082SKonstantin Belousov NULL)); 458aef922f5SJohn Dyson } 459aef922f5SJohn Dyson 4601c7c3c6aSMatthew Dillon /* 4611c7c3c6aSMatthew Dillon * vm_pageout_flush() - launder the given pages 4621c7c3c6aSMatthew Dillon * 4631c7c3c6aSMatthew Dillon * The given pages are laundered. Note that we setup for the start of 4641c7c3c6aSMatthew Dillon * I/O ( i.e. busy the page ), mark it read-only, and bump the object 4651c7c3c6aSMatthew Dillon * reference count all in here rather then in the parent. If we want 4661c7c3c6aSMatthew Dillon * the parent to do more sophisticated things we may have to change 4671c7c3c6aSMatthew Dillon * the ordering. 4681e8a675cSKonstantin Belousov * 4691e8a675cSKonstantin Belousov * Returned runlen is the count of pages between mreq and first 4701e8a675cSKonstantin Belousov * page after mreq with status VM_PAGER_AGAIN. 471126d6082SKonstantin Belousov * *eio is set to TRUE if pager returned VM_PAGER_ERROR or VM_PAGER_FAIL 472126d6082SKonstantin Belousov * for any page in runlen set. 4731c7c3c6aSMatthew Dillon */ 474aef922f5SJohn Dyson int 475126d6082SKonstantin Belousov vm_pageout_flush(vm_page_t *mc, int count, int flags, int mreq, int *prunlen, 476126d6082SKonstantin Belousov boolean_t *eio) 477aef922f5SJohn Dyson { 4782e3b314dSAlan Cox vm_object_t object = mc[0]->object; 479aef922f5SJohn Dyson int pageout_status[count]; 48095461b45SJohn Dyson int numpagedout = 0; 4811e8a675cSKonstantin Belousov int i, runlen; 482aef922f5SJohn Dyson 48389f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 4847bec141bSKip Macy 4851c7c3c6aSMatthew Dillon /* 4861c7c3c6aSMatthew Dillon * Initiate I/O. Bump the vm_page_t->busy counter and 4871c7c3c6aSMatthew Dillon * mark the pages read-only. 4881c7c3c6aSMatthew Dillon * 4891c7c3c6aSMatthew Dillon * We do not have to fixup the clean/dirty bits here... we can 4901c7c3c6aSMatthew Dillon * allow the pager to do it after the I/O completes. 49102fa91d3SMatthew Dillon * 49202fa91d3SMatthew Dillon * NOTE! mc[i]->dirty may be partial or fragmented due to an 49302fa91d3SMatthew Dillon * edge case with file fragments. 4941c7c3c6aSMatthew Dillon */ 4958f9110f6SJohn Dyson for (i = 0; i < count; i++) { 4967a935082SAlan Cox KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL, 4977a935082SAlan Cox ("vm_pageout_flush: partially invalid page %p index %d/%d", 4987a935082SAlan Cox mc[i], i, count)); 499c7aebda8SAttilio Rao vm_page_sbusy(mc[i]); 50078985e42SAlan Cox pmap_remove_write(mc[i]); 5012965a453SKip Macy } 502d474eaaaSDoug Rabson vm_object_pip_add(object, count); 503aef922f5SJohn Dyson 504d076fbeaSAlan Cox vm_pager_put_pages(object, mc, count, flags, pageout_status); 50526f9a767SRodney W. Grimes 5061e8a675cSKonstantin Belousov runlen = count - mreq; 507126d6082SKonstantin Belousov if (eio != NULL) 508126d6082SKonstantin Belousov *eio = FALSE; 509aef922f5SJohn Dyson for (i = 0; i < count; i++) { 510aef922f5SJohn Dyson vm_page_t mt = mc[i]; 51124a1cce3SDavid Greenman 5124cd45723SAlan Cox KASSERT(pageout_status[i] == VM_PAGER_PEND || 5136031c68dSAlan Cox !pmap_page_is_write_mapped(mt), 5149ea8d1a6SAlan Cox ("vm_pageout_flush: page %p is not write protected", mt)); 51526f9a767SRodney W. Grimes switch (pageout_status[i]) { 51626f9a767SRodney W. Grimes case VM_PAGER_OK: 51726f9a767SRodney W. Grimes case VM_PAGER_PEND: 51895461b45SJohn Dyson numpagedout++; 51926f9a767SRodney W. Grimes break; 52026f9a767SRodney W. Grimes case VM_PAGER_BAD: 52126f9a767SRodney W. Grimes /* 5220d94caffSDavid Greenman * Page outside of range of object. Right now we 5230d94caffSDavid Greenman * essentially lose the changes by pretending it 5240d94caffSDavid Greenman * worked. 52526f9a767SRodney W. Grimes */ 52690ecac61SMatthew Dillon vm_page_undirty(mt); 52726f9a767SRodney W. Grimes break; 52826f9a767SRodney W. Grimes case VM_PAGER_ERROR: 52926f9a767SRodney W. Grimes case VM_PAGER_FAIL: 53026f9a767SRodney W. Grimes /* 5310d94caffSDavid Greenman * If page couldn't be paged out, then reactivate the 5320d94caffSDavid Greenman * page so it doesn't clog the inactive list. (We 5330d94caffSDavid Greenman * will try paging out it again later). 53426f9a767SRodney W. Grimes */ 5353c4a2440SAlan Cox vm_page_lock(mt); 53624a1cce3SDavid Greenman vm_page_activate(mt); 5373c4a2440SAlan Cox vm_page_unlock(mt); 538126d6082SKonstantin Belousov if (eio != NULL && i >= mreq && i - mreq < runlen) 539126d6082SKonstantin Belousov *eio = TRUE; 54026f9a767SRodney W. Grimes break; 54126f9a767SRodney W. Grimes case VM_PAGER_AGAIN: 5421e8a675cSKonstantin Belousov if (i >= mreq && i - mreq < runlen) 5431e8a675cSKonstantin Belousov runlen = i - mreq; 54426f9a767SRodney W. Grimes break; 54526f9a767SRodney W. Grimes } 54626f9a767SRodney W. Grimes 54726f9a767SRodney W. Grimes /* 5480d94caffSDavid Greenman * If the operation is still going, leave the page busy to 5490d94caffSDavid Greenman * block all other accesses. Also, leave the paging in 5500d94caffSDavid Greenman * progress indicator set so that we don't attempt an object 5510d94caffSDavid Greenman * collapse. 55226f9a767SRodney W. Grimes */ 55326f9a767SRodney W. Grimes if (pageout_status[i] != VM_PAGER_PEND) { 554f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 555c7aebda8SAttilio Rao vm_page_sunbusy(mt); 5563c4a2440SAlan Cox if (vm_page_count_severe()) { 5573c4a2440SAlan Cox vm_page_lock(mt); 5589ea8d1a6SAlan Cox vm_page_try_to_cache(mt); 5592965a453SKip Macy vm_page_unlock(mt); 56026f9a767SRodney W. Grimes } 5613c4a2440SAlan Cox } 5623c4a2440SAlan Cox } 5631e8a675cSKonstantin Belousov if (prunlen != NULL) 5641e8a675cSKonstantin Belousov *prunlen = runlen; 5653c4a2440SAlan Cox return (numpagedout); 56626f9a767SRodney W. Grimes } 56726f9a767SRodney W. Grimes 56885eeca35SAlan Cox static boolean_t 569449c2e92SKonstantin Belousov vm_pageout_launder(struct vm_pagequeue *pq, int tries, vm_paddr_t low, 570449c2e92SKonstantin Belousov vm_paddr_t high) 57185eeca35SAlan Cox { 57285eeca35SAlan Cox struct mount *mp; 57385eeca35SAlan Cox struct vnode *vp; 57485eeca35SAlan Cox vm_object_t object; 57585eeca35SAlan Cox vm_paddr_t pa; 57685eeca35SAlan Cox vm_page_t m, m_tmp, next; 5771bd7d0b7SKonstantin Belousov int lockmode; 57885eeca35SAlan Cox 5798d220203SAlan Cox vm_pagequeue_lock(pq); 580c325e866SKonstantin Belousov TAILQ_FOREACH_SAFE(m, &pq->pq_pl, plinks.q, next) { 58185eeca35SAlan Cox if ((m->flags & PG_MARKER) != 0) 58285eeca35SAlan Cox continue; 58385eeca35SAlan Cox pa = VM_PAGE_TO_PHYS(m); 58485eeca35SAlan Cox if (pa < low || pa + PAGE_SIZE > high) 58585eeca35SAlan Cox continue; 58685eeca35SAlan Cox if (!vm_pageout_page_lock(m, &next) || m->hold_count != 0) { 58785eeca35SAlan Cox vm_page_unlock(m); 58885eeca35SAlan Cox continue; 58985eeca35SAlan Cox } 59085eeca35SAlan Cox object = m->object; 59189f6b863SAttilio Rao if ((!VM_OBJECT_TRYWLOCK(object) && 59285eeca35SAlan Cox (!vm_pageout_fallback_object_lock(m, &next) || 593c7aebda8SAttilio Rao m->hold_count != 0)) || vm_page_busied(m)) { 59485eeca35SAlan Cox vm_page_unlock(m); 59589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 59685eeca35SAlan Cox continue; 59785eeca35SAlan Cox } 59885eeca35SAlan Cox vm_page_test_dirty(m); 5999fc4739dSAlan Cox if (m->dirty == 0 && object->ref_count != 0) 60085eeca35SAlan Cox pmap_remove_all(m); 60185eeca35SAlan Cox if (m->dirty != 0) { 60285eeca35SAlan Cox vm_page_unlock(m); 60385eeca35SAlan Cox if (tries == 0 || (object->flags & OBJ_DEAD) != 0) { 60489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 60585eeca35SAlan Cox continue; 60685eeca35SAlan Cox } 60785eeca35SAlan Cox if (object->type == OBJT_VNODE) { 6088d220203SAlan Cox vm_pagequeue_unlock(pq); 60985eeca35SAlan Cox vp = object->handle; 61085eeca35SAlan Cox vm_object_reference_locked(object); 61189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 61285eeca35SAlan Cox (void)vn_start_write(vp, &mp, V_WAIT); 6131bd7d0b7SKonstantin Belousov lockmode = MNT_SHARED_WRITES(vp->v_mount) ? 6141bd7d0b7SKonstantin Belousov LK_SHARED : LK_EXCLUSIVE; 6151bd7d0b7SKonstantin Belousov vn_lock(vp, lockmode | LK_RETRY); 61689f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 61785eeca35SAlan Cox vm_object_page_clean(object, 0, 0, OBJPC_SYNC); 61889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 61985eeca35SAlan Cox VOP_UNLOCK(vp, 0); 62085eeca35SAlan Cox vm_object_deallocate(object); 62185eeca35SAlan Cox vn_finished_write(mp); 62285eeca35SAlan Cox return (TRUE); 62385eeca35SAlan Cox } else if (object->type == OBJT_SWAP || 62485eeca35SAlan Cox object->type == OBJT_DEFAULT) { 6258d220203SAlan Cox vm_pagequeue_unlock(pq); 62685eeca35SAlan Cox m_tmp = m; 62785eeca35SAlan Cox vm_pageout_flush(&m_tmp, 1, VM_PAGER_PUT_SYNC, 62885eeca35SAlan Cox 0, NULL, NULL); 62989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 63085eeca35SAlan Cox return (TRUE); 63185eeca35SAlan Cox } 63285eeca35SAlan Cox } else { 6338d220203SAlan Cox /* 6348d220203SAlan Cox * Dequeue here to prevent lock recursion in 6358d220203SAlan Cox * vm_page_cache(). 6368d220203SAlan Cox */ 6378d220203SAlan Cox vm_page_dequeue_locked(m); 63885eeca35SAlan Cox vm_page_cache(m); 63985eeca35SAlan Cox vm_page_unlock(m); 64085eeca35SAlan Cox } 64189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 64285eeca35SAlan Cox } 6438d220203SAlan Cox vm_pagequeue_unlock(pq); 64485eeca35SAlan Cox return (FALSE); 64585eeca35SAlan Cox } 64685eeca35SAlan Cox 64785eeca35SAlan Cox /* 64885eeca35SAlan Cox * Increase the number of cached pages. The specified value, "tries", 64985eeca35SAlan Cox * determines which categories of pages are cached: 65085eeca35SAlan Cox * 65185eeca35SAlan Cox * 0: All clean, inactive pages within the specified physical address range 65285eeca35SAlan Cox * are cached. Will not sleep. 65385eeca35SAlan Cox * 1: The vm_lowmem handlers are called. All inactive pages within 65485eeca35SAlan Cox * the specified physical address range are cached. May sleep. 65585eeca35SAlan Cox * 2: The vm_lowmem handlers are called. All inactive and active pages 65685eeca35SAlan Cox * within the specified physical address range are cached. May sleep. 65785eeca35SAlan Cox */ 65885eeca35SAlan Cox void 65985eeca35SAlan Cox vm_pageout_grow_cache(int tries, vm_paddr_t low, vm_paddr_t high) 66085eeca35SAlan Cox { 661449c2e92SKonstantin Belousov int actl, actmax, inactl, inactmax, dom, initial_dom; 662449c2e92SKonstantin Belousov static int start_dom = 0; 66385eeca35SAlan Cox 66485eeca35SAlan Cox if (tries > 0) { 66585eeca35SAlan Cox /* 66685eeca35SAlan Cox * Decrease registered cache sizes. The vm_lowmem handlers 66785eeca35SAlan Cox * may acquire locks and/or sleep, so they can only be invoked 66885eeca35SAlan Cox * when "tries" is greater than zero. 66985eeca35SAlan Cox */ 67085eeca35SAlan Cox EVENTHANDLER_INVOKE(vm_lowmem, 0); 67185eeca35SAlan Cox 67285eeca35SAlan Cox /* 67385eeca35SAlan Cox * We do this explicitly after the caches have been drained 67485eeca35SAlan Cox * above. 67585eeca35SAlan Cox */ 67685eeca35SAlan Cox uma_reclaim(); 67785eeca35SAlan Cox } 678449c2e92SKonstantin Belousov 679449c2e92SKonstantin Belousov /* 680449c2e92SKonstantin Belousov * Make the next scan start on the next domain. 681449c2e92SKonstantin Belousov */ 682449c2e92SKonstantin Belousov initial_dom = atomic_fetchadd_int(&start_dom, 1) % vm_ndomains; 683449c2e92SKonstantin Belousov 68485eeca35SAlan Cox inactl = 0; 68544f1c916SBryan Drewery inactmax = vm_cnt.v_inactive_count; 68685eeca35SAlan Cox actl = 0; 68744f1c916SBryan Drewery actmax = tries < 2 ? 0 : vm_cnt.v_active_count; 688449c2e92SKonstantin Belousov dom = initial_dom; 689449c2e92SKonstantin Belousov 690449c2e92SKonstantin Belousov /* 691449c2e92SKonstantin Belousov * Scan domains in round-robin order, first inactive queues, 692449c2e92SKonstantin Belousov * then active. Since domain usually owns large physically 693449c2e92SKonstantin Belousov * contiguous chunk of memory, it makes sense to completely 694449c2e92SKonstantin Belousov * exhaust one domain before switching to next, while growing 695449c2e92SKonstantin Belousov * the pool of contiguous physical pages. 696449c2e92SKonstantin Belousov * 697449c2e92SKonstantin Belousov * Do not even start launder a domain which cannot contain 698449c2e92SKonstantin Belousov * the specified address range, as indicated by segments 699449c2e92SKonstantin Belousov * constituting the domain. 700449c2e92SKonstantin Belousov */ 70185eeca35SAlan Cox again: 702449c2e92SKonstantin Belousov if (inactl < inactmax) { 703449c2e92SKonstantin Belousov if (vm_phys_domain_intersects(vm_dom[dom].vmd_segs, 704449c2e92SKonstantin Belousov low, high) && 705449c2e92SKonstantin Belousov vm_pageout_launder(&vm_dom[dom].vmd_pagequeues[PQ_INACTIVE], 706449c2e92SKonstantin Belousov tries, low, high)) { 70785eeca35SAlan Cox inactl++; 70885eeca35SAlan Cox goto again; 70985eeca35SAlan Cox } 710449c2e92SKonstantin Belousov if (++dom == vm_ndomains) 711449c2e92SKonstantin Belousov dom = 0; 712449c2e92SKonstantin Belousov if (dom != initial_dom) 713449c2e92SKonstantin Belousov goto again; 714449c2e92SKonstantin Belousov } 715449c2e92SKonstantin Belousov if (actl < actmax) { 716449c2e92SKonstantin Belousov if (vm_phys_domain_intersects(vm_dom[dom].vmd_segs, 717449c2e92SKonstantin Belousov low, high) && 718449c2e92SKonstantin Belousov vm_pageout_launder(&vm_dom[dom].vmd_pagequeues[PQ_ACTIVE], 719449c2e92SKonstantin Belousov tries, low, high)) { 72085eeca35SAlan Cox actl++; 72185eeca35SAlan Cox goto again; 72285eeca35SAlan Cox } 723449c2e92SKonstantin Belousov if (++dom == vm_ndomains) 724449c2e92SKonstantin Belousov dom = 0; 725449c2e92SKonstantin Belousov if (dom != initial_dom) 726449c2e92SKonstantin Belousov goto again; 727449c2e92SKonstantin Belousov } 72885eeca35SAlan Cox } 72985eeca35SAlan Cox 73038efa82bSJohn Dyson #if !defined(NO_SWAPPING) 73126f9a767SRodney W. Grimes /* 73226f9a767SRodney W. Grimes * vm_pageout_object_deactivate_pages 73326f9a767SRodney W. Grimes * 734ce186587SAlan Cox * Deactivate enough pages to satisfy the inactive target 735ce186587SAlan Cox * requirements. 73626f9a767SRodney W. Grimes * 73726f9a767SRodney W. Grimes * The object and map must be locked. 73826f9a767SRodney W. Grimes */ 73938efa82bSJohn Dyson static void 740ce186587SAlan Cox vm_pageout_object_deactivate_pages(pmap_t pmap, vm_object_t first_object, 741ce186587SAlan Cox long desired) 74226f9a767SRodney W. Grimes { 743ecf6279fSAlan Cox vm_object_t backing_object, object; 744ce186587SAlan Cox vm_page_t p; 745bb7858eaSJeff Roberson int act_delta, remove_mode; 74626f9a767SRodney W. Grimes 747e23b0a19SAlan Cox VM_OBJECT_ASSERT_LOCKED(first_object); 74828634820SAlan Cox if ((first_object->flags & OBJ_FICTITIOUS) != 0) 74938efa82bSJohn Dyson return; 750ecf6279fSAlan Cox for (object = first_object;; object = backing_object) { 751ecf6279fSAlan Cox if (pmap_resident_count(pmap) <= desired) 752ecf6279fSAlan Cox goto unlock_return; 753e23b0a19SAlan Cox VM_OBJECT_ASSERT_LOCKED(object); 75428634820SAlan Cox if ((object->flags & OBJ_UNMANAGED) != 0 || 75528634820SAlan Cox object->paging_in_progress != 0) 756ecf6279fSAlan Cox goto unlock_return; 75726f9a767SRodney W. Grimes 75885b1dc89SAlan Cox remove_mode = 0; 75938efa82bSJohn Dyson if (object->shadow_count > 1) 76038efa82bSJohn Dyson remove_mode = 1; 76126f9a767SRodney W. Grimes /* 762ce186587SAlan Cox * Scan the object's entire memory queue. 76326f9a767SRodney W. Grimes */ 764ce186587SAlan Cox TAILQ_FOREACH(p, &object->memq, listq) { 765447fe2a4SAlan Cox if (pmap_resident_count(pmap) <= desired) 766447fe2a4SAlan Cox goto unlock_return; 767c7aebda8SAttilio Rao if (vm_page_busied(p)) 768447fe2a4SAlan Cox continue; 769ce186587SAlan Cox PCPU_INC(cnt.v_pdpages); 7702965a453SKip Macy vm_page_lock(p); 771ce186587SAlan Cox if (p->wire_count != 0 || p->hold_count != 0 || 772ecf6279fSAlan Cox !pmap_page_exists_quick(pmap, p)) { 7732965a453SKip Macy vm_page_unlock(p); 7740d94caffSDavid Greenman continue; 7750d94caffSDavid Greenman } 776bb7858eaSJeff Roberson act_delta = pmap_ts_referenced(p); 7773407fefeSKonstantin Belousov if ((p->aflags & PGA_REFERENCED) != 0) { 778bb7858eaSJeff Roberson if (act_delta == 0) 779bb7858eaSJeff Roberson act_delta = 1; 7803407fefeSKonstantin Belousov vm_page_aflag_clear(p, PGA_REFERENCED); 781ef743ce6SJohn Dyson } 782bb7858eaSJeff Roberson if (p->queue != PQ_ACTIVE && act_delta != 0) { 783ef743ce6SJohn Dyson vm_page_activate(p); 784bb7858eaSJeff Roberson p->act_count += act_delta; 785c8c4b40cSJohn Dyson } else if (p->queue == PQ_ACTIVE) { 786bb7858eaSJeff Roberson if (act_delta == 0) { 787ce186587SAlan Cox p->act_count -= min(p->act_count, 788ce186587SAlan Cox ACT_DECLINE); 78990776bd7SJeff Roberson if (!remove_mode && p->act_count == 0) { 7904fec79beSAlan Cox pmap_remove_all(p); 79126f9a767SRodney W. Grimes vm_page_deactivate(p); 7928d220203SAlan Cox } else 7938d220203SAlan Cox vm_page_requeue(p); 794c8c4b40cSJohn Dyson } else { 795eaf13dd7SJohn Dyson vm_page_activate(p); 796ce186587SAlan Cox if (p->act_count < ACT_MAX - 797ce186587SAlan Cox ACT_ADVANCE) 79838efa82bSJohn Dyson p->act_count += ACT_ADVANCE; 7998d220203SAlan Cox vm_page_requeue(p); 800ce186587SAlan Cox } 801ce186587SAlan Cox } else if (p->queue == PQ_INACTIVE) 802ce186587SAlan Cox pmap_remove_all(p); 8032965a453SKip Macy vm_page_unlock(p); 80426f9a767SRodney W. Grimes } 805ecf6279fSAlan Cox if ((backing_object = object->backing_object) == NULL) 806ecf6279fSAlan Cox goto unlock_return; 807e23b0a19SAlan Cox VM_OBJECT_RLOCK(backing_object); 808ecf6279fSAlan Cox if (object != first_object) 809e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(object); 81038efa82bSJohn Dyson } 811ecf6279fSAlan Cox unlock_return: 812ecf6279fSAlan Cox if (object != first_object) 813e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(object); 81426f9a767SRodney W. Grimes } 81526f9a767SRodney W. Grimes 81626f9a767SRodney W. Grimes /* 81726f9a767SRodney W. Grimes * deactivate some number of pages in a map, try to do it fairly, but 81826f9a767SRodney W. Grimes * that is really hard to do. 81926f9a767SRodney W. Grimes */ 820cd41fc12SDavid Greenman static void 82138efa82bSJohn Dyson vm_pageout_map_deactivate_pages(map, desired) 82226f9a767SRodney W. Grimes vm_map_t map; 823ecf6279fSAlan Cox long desired; 82426f9a767SRodney W. Grimes { 82526f9a767SRodney W. Grimes vm_map_entry_t tmpe; 82638efa82bSJohn Dyson vm_object_t obj, bigobj; 82730105b9eSTor Egge int nothingwired; 8280d94caffSDavid Greenman 829d974f03cSAlan Cox if (!vm_map_trylock(map)) 83026f9a767SRodney W. Grimes return; 83138efa82bSJohn Dyson 83238efa82bSJohn Dyson bigobj = NULL; 83330105b9eSTor Egge nothingwired = TRUE; 83438efa82bSJohn Dyson 83538efa82bSJohn Dyson /* 83638efa82bSJohn Dyson * first, search out the biggest object, and try to free pages from 83738efa82bSJohn Dyson * that. 83838efa82bSJohn Dyson */ 83926f9a767SRodney W. Grimes tmpe = map->header.next; 84038efa82bSJohn Dyson while (tmpe != &map->header) { 8419fdfe602SMatthew Dillon if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 84238efa82bSJohn Dyson obj = tmpe->object.vm_object; 843e23b0a19SAlan Cox if (obj != NULL && VM_OBJECT_TRYRLOCK(obj)) { 8440774dfb3SAlan Cox if (obj->shadow_count <= 1 && 8450774dfb3SAlan Cox (bigobj == NULL || 8460774dfb3SAlan Cox bigobj->resident_page_count < obj->resident_page_count)) { 8470774dfb3SAlan Cox if (bigobj != NULL) 848e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(bigobj); 84938efa82bSJohn Dyson bigobj = obj; 8500774dfb3SAlan Cox } else 851e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(obj); 85238efa82bSJohn Dyson } 85338efa82bSJohn Dyson } 85430105b9eSTor Egge if (tmpe->wired_count > 0) 85530105b9eSTor Egge nothingwired = FALSE; 85638efa82bSJohn Dyson tmpe = tmpe->next; 85738efa82bSJohn Dyson } 85838efa82bSJohn Dyson 8590774dfb3SAlan Cox if (bigobj != NULL) { 860ecf6279fSAlan Cox vm_pageout_object_deactivate_pages(map->pmap, bigobj, desired); 861e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(bigobj); 8620774dfb3SAlan Cox } 86338efa82bSJohn Dyson /* 86438efa82bSJohn Dyson * Next, hunt around for other pages to deactivate. We actually 86538efa82bSJohn Dyson * do this search sort of wrong -- .text first is not the best idea. 86638efa82bSJohn Dyson */ 86738efa82bSJohn Dyson tmpe = map->header.next; 86838efa82bSJohn Dyson while (tmpe != &map->header) { 869b1028ad1SLuoqi Chen if (pmap_resident_count(vm_map_pmap(map)) <= desired) 87038efa82bSJohn Dyson break; 8719fdfe602SMatthew Dillon if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 87238efa82bSJohn Dyson obj = tmpe->object.vm_object; 8730774dfb3SAlan Cox if (obj != NULL) { 874e23b0a19SAlan Cox VM_OBJECT_RLOCK(obj); 875ecf6279fSAlan Cox vm_pageout_object_deactivate_pages(map->pmap, obj, desired); 876e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(obj); 8770774dfb3SAlan Cox } 87838efa82bSJohn Dyson } 87926f9a767SRodney W. Grimes tmpe = tmpe->next; 88038857e7fSAlan Cox } 88138efa82bSJohn Dyson 88238efa82bSJohn Dyson /* 88338efa82bSJohn Dyson * Remove all mappings if a process is swapped out, this will free page 88438efa82bSJohn Dyson * table pages. 88538efa82bSJohn Dyson */ 88638857e7fSAlan Cox if (desired == 0 && nothingwired) { 8878d01a3b2SNathan Whitehorn pmap_remove(vm_map_pmap(map), vm_map_min(map), 8888d01a3b2SNathan Whitehorn vm_map_max(map)); 88938857e7fSAlan Cox } 890938b0f5bSMarcel Moolenaar 89138efa82bSJohn Dyson vm_map_unlock(map); 89226f9a767SRodney W. Grimes } 893a1287949SEivind Eklund #endif /* !defined(NO_SWAPPING) */ 894df8bae1dSRodney W. Grimes 8951c7c3c6aSMatthew Dillon /* 896df8bae1dSRodney W. Grimes * vm_pageout_scan does the dirty work for the pageout daemon. 897d9e23210SJeff Roberson * 898d9e23210SJeff Roberson * pass 0 - Update active LRU/deactivate pages 899d9e23210SJeff Roberson * pass 1 - Move inactive to cache or free 900d9e23210SJeff Roberson * pass 2 - Launder dirty pages 901df8bae1dSRodney W. Grimes */ 9022b6b0df7SMatthew Dillon static void 903449c2e92SKonstantin Belousov vm_pageout_scan(struct vm_domain *vmd, int pass) 904df8bae1dSRodney W. Grimes { 905502ba6e4SJohn Dyson vm_page_t m, next; 9068d220203SAlan Cox struct vm_pagequeue *pq; 907df8bae1dSRodney W. Grimes vm_object_t object; 9089099545aSAlan Cox int act_delta, addl_page_shortage, deficit, maxscan, page_shortage; 909f6b04d2bSDavid Greenman int vnodes_skipped = 0; 9102b6b0df7SMatthew Dillon int maxlaunder; 9111bd7d0b7SKonstantin Belousov int lockmode; 91248cc2fc7SKonstantin Belousov boolean_t queues_locked; 9130d94caffSDavid Greenman 914df8bae1dSRodney W. Grimes /* 915d9e23210SJeff Roberson * If we need to reclaim memory ask kernel caches to return 916c9612b2dSJeff Roberson * some. We rate limit to avoid thrashing. 917d9e23210SJeff Roberson */ 918c9612b2dSJeff Roberson if (vmd == &vm_dom[0] && pass > 0 && 919c9612b2dSJeff Roberson lowmem_ticks + (lowmem_period * hz) < ticks) { 920d9e23210SJeff Roberson /* 921855a310fSJeff Roberson * Decrease registered cache sizes. 922855a310fSJeff Roberson */ 923855a310fSJeff Roberson EVENTHANDLER_INVOKE(vm_lowmem, 0); 924855a310fSJeff Roberson /* 925d9e23210SJeff Roberson * We do this explicitly after the caches have been 926d9e23210SJeff Roberson * drained above. 927855a310fSJeff Roberson */ 928855a310fSJeff Roberson uma_reclaim(); 929c9612b2dSJeff Roberson lowmem_ticks = ticks; 930d9e23210SJeff Roberson } 9315985940eSJohn Dyson 932311e34e2SKonstantin Belousov /* 93396240c89SEitan Adler * The addl_page_shortage is the number of temporarily 934311e34e2SKonstantin Belousov * stuck pages in the inactive queue. In other words, the 935449c2e92SKonstantin Belousov * number of pages from the inactive count that should be 936311e34e2SKonstantin Belousov * discounted in setting the target for the active queue scan. 937311e34e2SKonstantin Belousov */ 9389099545aSAlan Cox addl_page_shortage = 0; 9399099545aSAlan Cox 9401c7c3c6aSMatthew Dillon /* 9411c7c3c6aSMatthew Dillon * Calculate the number of pages we want to either free or move 9422b6b0df7SMatthew Dillon * to the cache. 9431c7c3c6aSMatthew Dillon */ 94460196cdaSAlan Cox if (pass > 0) { 94560196cdaSAlan Cox deficit = atomic_readandclear_int(&vm_pageout_deficit); 9469099545aSAlan Cox page_shortage = vm_paging_target() + deficit; 94760196cdaSAlan Cox } else 94860196cdaSAlan Cox page_shortage = deficit = 0; 9491c7c3c6aSMatthew Dillon 950936524aaSMatthew Dillon /* 9512b6b0df7SMatthew Dillon * maxlaunder limits the number of dirty pages we flush per scan. 9522b6b0df7SMatthew Dillon * For most systems a smaller value (16 or 32) is more robust under 9532b6b0df7SMatthew Dillon * extreme memory and disk pressure because any unnecessary writes 9542b6b0df7SMatthew Dillon * to disk can result in extreme performance degredation. However, 9552b6b0df7SMatthew Dillon * systems with excessive dirty pages (especially when MAP_NOSYNC is 9562b6b0df7SMatthew Dillon * used) will die horribly with limited laundering. If the pageout 9572b6b0df7SMatthew Dillon * daemon cannot clean enough pages in the first pass, we let it go 9582b6b0df7SMatthew Dillon * all out in succeeding passes. 9591c7c3c6aSMatthew Dillon */ 9602b6b0df7SMatthew Dillon if ((maxlaunder = vm_max_launder) <= 1) 9612b6b0df7SMatthew Dillon maxlaunder = 1; 962d9e23210SJeff Roberson if (pass > 1) 9632b6b0df7SMatthew Dillon maxlaunder = 10000; 9648d220203SAlan Cox 9658d220203SAlan Cox /* 9668d220203SAlan Cox * Start scanning the inactive queue for pages we can move to the 9678d220203SAlan Cox * cache or free. The scan will stop when the target is reached or 9688d220203SAlan Cox * we have scanned the entire inactive queue. Note that m->act_count 9698d220203SAlan Cox * is not used to form decisions for the inactive queue, only for the 9708d220203SAlan Cox * active queue. 9718d220203SAlan Cox */ 972449c2e92SKonstantin Belousov pq = &vmd->vmd_pagequeues[PQ_INACTIVE]; 973449c2e92SKonstantin Belousov maxscan = pq->pq_cnt; 9748d220203SAlan Cox vm_pagequeue_lock(pq); 9758d220203SAlan Cox queues_locked = TRUE; 9768d220203SAlan Cox for (m = TAILQ_FIRST(&pq->pq_pl); 9771c7c3c6aSMatthew Dillon m != NULL && maxscan-- > 0 && page_shortage > 0; 978e929c00dSKirk McKusick m = next) { 9798d220203SAlan Cox vm_pagequeue_assert_locked(pq); 98048cc2fc7SKonstantin Belousov KASSERT(queues_locked, ("unlocked queues")); 981d4961bcbSKonstantin Belousov KASSERT(m->queue == PQ_INACTIVE, ("Inactive queue %p", m)); 982df8bae1dSRodney W. Grimes 9838d220203SAlan Cox PCPU_INC(cnt.v_pdpages); 984c325e866SKonstantin Belousov next = TAILQ_NEXT(m, plinks.q); 985df8bae1dSRodney W. Grimes 986936524aaSMatthew Dillon /* 987936524aaSMatthew Dillon * skip marker pages 988936524aaSMatthew Dillon */ 989936524aaSMatthew Dillon if (m->flags & PG_MARKER) 990936524aaSMatthew Dillon continue; 991936524aaSMatthew Dillon 9927900f95dSKonstantin Belousov KASSERT((m->flags & PG_FICTITIOUS) == 0, 9937900f95dSKonstantin Belousov ("Fictitious page %p cannot be in inactive queue", m)); 9947900f95dSKonstantin Belousov KASSERT((m->oflags & VPO_UNMANAGED) == 0, 9957900f95dSKonstantin Belousov ("Unmanaged page %p cannot be in inactive queue", m)); 9967900f95dSKonstantin Belousov 9978c616246SKonstantin Belousov /* 998311e34e2SKonstantin Belousov * The page or object lock acquisitions fail if the 999311e34e2SKonstantin Belousov * page was removed from the queue or moved to a 1000311e34e2SKonstantin Belousov * different position within the queue. In either 1001311e34e2SKonstantin Belousov * case, addl_page_shortage should not be incremented. 10028c616246SKonstantin Belousov */ 10038c616246SKonstantin Belousov if (!vm_pageout_page_lock(m, &next)) { 10048c616246SKonstantin Belousov vm_page_unlock(m); 1005b182ec9eSJohn Dyson continue; 1006df8bae1dSRodney W. Grimes } 10079ee2165fSAlan Cox object = m->object; 100889f6b863SAttilio Rao if (!VM_OBJECT_TRYWLOCK(object) && 1009311e34e2SKonstantin Belousov !vm_pageout_fallback_object_lock(m, &next)) { 10102965a453SKip Macy vm_page_unlock(m); 101189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 101234d9e6fdSAlan Cox continue; 101334d9e6fdSAlan Cox } 1014311e34e2SKonstantin Belousov 1015311e34e2SKonstantin Belousov /* 1016311e34e2SKonstantin Belousov * Don't mess with busy pages, keep them at at the 1017311e34e2SKonstantin Belousov * front of the queue, most likely they are being 1018311e34e2SKonstantin Belousov * paged out. Increment addl_page_shortage for busy 1019311e34e2SKonstantin Belousov * pages, because they may leave the inactive queue 1020311e34e2SKonstantin Belousov * shortly after page scan is finished. 1021311e34e2SKonstantin Belousov */ 1022c7aebda8SAttilio Rao if (vm_page_busied(m)) { 10232965a453SKip Macy vm_page_unlock(m); 102489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1025b182ec9eSJohn Dyson addl_page_shortage++; 102626f9a767SRodney W. Grimes continue; 102726f9a767SRodney W. Grimes } 1028bd7e5f99SJohn Dyson 10297e006499SJohn Dyson /* 10308d220203SAlan Cox * We unlock the inactive page queue, invalidating the 103148cc2fc7SKonstantin Belousov * 'next' pointer. Use our marker to remember our 103248cc2fc7SKonstantin Belousov * place. 103348cc2fc7SKonstantin Belousov */ 1034c325e866SKonstantin Belousov TAILQ_INSERT_AFTER(&pq->pq_pl, m, &vmd->vmd_marker, plinks.q); 10358d220203SAlan Cox vm_pagequeue_unlock(pq); 103648cc2fc7SKonstantin Belousov queues_locked = FALSE; 103748cc2fc7SKonstantin Belousov 103848cc2fc7SKonstantin Belousov /* 1039bb7858eaSJeff Roberson * We bump the activation count if the page has been 1040bb7858eaSJeff Roberson * referenced while in the inactive queue. This makes 1041bb7858eaSJeff Roberson * it less likely that the page will be added back to the 1042bb7858eaSJeff Roberson * inactive queue prematurely again. Here we check the 10431c7c3c6aSMatthew Dillon * page tables (or emulated bits, if any), given the upper 10441c7c3c6aSMatthew Dillon * level VM system not knowing anything about existing 10451c7c3c6aSMatthew Dillon * references. 10467e006499SJohn Dyson */ 1047bb7858eaSJeff Roberson if ((m->aflags & PGA_REFERENCED) != 0) { 1048bb7858eaSJeff Roberson vm_page_aflag_clear(m, PGA_REFERENCED); 1049bb7858eaSJeff Roberson act_delta = 1; 105086fa2471SAlan Cox } else 105186fa2471SAlan Cox act_delta = 0; 1052bb7858eaSJeff Roberson if (object->ref_count != 0) { 1053bb7858eaSJeff Roberson act_delta += pmap_ts_referenced(m); 1054bb7858eaSJeff Roberson } else { 1055bb7858eaSJeff Roberson KASSERT(!pmap_page_is_mapped(m), 1056bb7858eaSJeff Roberson ("vm_pageout_scan: page %p is mapped", m)); 10572fe6e4d7SDavid Greenman } 1058ef743ce6SJohn Dyson 10597e006499SJohn Dyson /* 10601c7c3c6aSMatthew Dillon * If the upper level VM system knows about any page 1061bb7858eaSJeff Roberson * references, we reactivate the page or requeue it. 10627e006499SJohn Dyson */ 1063bb7858eaSJeff Roberson if (act_delta != 0) { 106486fa2471SAlan Cox if (object->ref_count != 0) { 106526f9a767SRodney W. Grimes vm_page_activate(m); 1066bb7858eaSJeff Roberson m->act_count += act_delta + ACT_ADVANCE; 1067bb7858eaSJeff Roberson } else { 1068bb7858eaSJeff Roberson vm_pagequeue_lock(pq); 1069bb7858eaSJeff Roberson queues_locked = TRUE; 1070bb7858eaSJeff Roberson vm_page_requeue_locked(m); 1071bb7858eaSJeff Roberson } 107289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1073a15f7df5SAttilio Rao vm_page_unlock(m); 107448cc2fc7SKonstantin Belousov goto relock_queues; 10750d94caffSDavid Greenman } 107667bf6868SJohn Dyson 1077311e34e2SKonstantin Belousov if (m->hold_count != 0) { 1078311e34e2SKonstantin Belousov vm_page_unlock(m); 107989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1080311e34e2SKonstantin Belousov 1081311e34e2SKonstantin Belousov /* 1082311e34e2SKonstantin Belousov * Held pages are essentially stuck in the 1083311e34e2SKonstantin Belousov * queue. So, they ought to be discounted 1084449c2e92SKonstantin Belousov * from the inactive count. See the 1085311e34e2SKonstantin Belousov * calculation of the page_shortage for the 1086311e34e2SKonstantin Belousov * loop over the active queue below. 1087311e34e2SKonstantin Belousov */ 1088311e34e2SKonstantin Belousov addl_page_shortage++; 1089311e34e2SKonstantin Belousov goto relock_queues; 1090311e34e2SKonstantin Belousov } 1091311e34e2SKonstantin Belousov 10927e006499SJohn Dyson /* 10939fc4739dSAlan Cox * If the page appears to be clean at the machine-independent 10949fc4739dSAlan Cox * layer, then remove all of its mappings from the pmap in 10959fc4739dSAlan Cox * anticipation of placing it onto the cache queue. If, 10969fc4739dSAlan Cox * however, any of the page's mappings allow write access, 10979fc4739dSAlan Cox * then the page may still be modified until the last of those 10989fc4739dSAlan Cox * mappings are removed. 10997e006499SJohn Dyson */ 11009fc4739dSAlan Cox vm_page_test_dirty(m); 11019fc4739dSAlan Cox if (m->dirty == 0 && object->ref_count != 0) 1102b78ddb0bSAlan Cox pmap_remove_all(m); 1103dcbcd518SBruce Evans 11046989c456SAlan Cox if (m->valid == 0) { 11057e006499SJohn Dyson /* 11067e006499SJohn Dyson * Invalid pages can be easily freed 11077e006499SJohn Dyson */ 11086989c456SAlan Cox vm_page_free(m); 110948cc2fc7SKonstantin Belousov PCPU_INC(cnt.v_dfree); 11101c7c3c6aSMatthew Dillon --page_shortage; 1111bd7e5f99SJohn Dyson } else if (m->dirty == 0) { 11126989c456SAlan Cox /* 11136989c456SAlan Cox * Clean pages can be placed onto the cache queue. 11146989c456SAlan Cox * This effectively frees them. 11156989c456SAlan Cox */ 1116bd7e5f99SJohn Dyson vm_page_cache(m); 11171c7c3c6aSMatthew Dillon --page_shortage; 1118d9e23210SJeff Roberson } else if ((m->flags & PG_WINATCFLS) == 0 && pass < 2) { 11197e006499SJohn Dyson /* 11202b6b0df7SMatthew Dillon * Dirty pages need to be paged out, but flushing 1121ab46f63eSJohn Baldwin * a page is extremely expensive versus freeing 11222b6b0df7SMatthew Dillon * a clean page. Rather then artificially limiting 11232b6b0df7SMatthew Dillon * the number of pages we can flush, we instead give 11242b6b0df7SMatthew Dillon * dirty pages extra priority on the inactive queue 11252b6b0df7SMatthew Dillon * by forcing them to be cycled through the queue 11262b6b0df7SMatthew Dillon * twice before being flushed, after which the 11272b6b0df7SMatthew Dillon * (now clean) page will cycle through once more 11282b6b0df7SMatthew Dillon * before being freed. This significantly extends 11292b6b0df7SMatthew Dillon * the thrash point for a heavily loaded machine. 11307e006499SJohn Dyson */ 11313407fefeSKonstantin Belousov m->flags |= PG_WINATCFLS; 11328d220203SAlan Cox vm_pagequeue_lock(pq); 113348cc2fc7SKonstantin Belousov queues_locked = TRUE; 11348d220203SAlan Cox vm_page_requeue_locked(m); 11350d94caffSDavid Greenman } else if (maxlaunder > 0) { 11362b6b0df7SMatthew Dillon /* 11372b6b0df7SMatthew Dillon * We always want to try to flush some dirty pages if 11382b6b0df7SMatthew Dillon * we encounter them, to keep the system stable. 11392b6b0df7SMatthew Dillon * Normally this number is small, but under extreme 11402b6b0df7SMatthew Dillon * pressure where there are insufficient clean pages 11412b6b0df7SMatthew Dillon * on the inactive queue, we may have to go all out. 11422b6b0df7SMatthew Dillon */ 11435050aa86SKonstantin Belousov int swap_pageouts_ok; 1144f6b04d2bSDavid Greenman struct vnode *vp = NULL; 114514137dc0SAlan Cox struct mount *mp = NULL; 11460d94caffSDavid Greenman 114712ac6a1dSJohn Dyson if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) { 114812ac6a1dSJohn Dyson swap_pageouts_ok = 1; 114912ac6a1dSJohn Dyson } else { 115012ac6a1dSJohn Dyson swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts); 115112ac6a1dSJohn Dyson swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts && 115290ecac61SMatthew Dillon vm_page_count_min()); 115312ac6a1dSJohn Dyson 115412ac6a1dSJohn Dyson } 115570111b90SJohn Dyson 115670111b90SJohn Dyson /* 11571c7c3c6aSMatthew Dillon * We don't bother paging objects that are "dead". 11581c7c3c6aSMatthew Dillon * Those objects are in a "rundown" state. 115970111b90SJohn Dyson */ 116070111b90SJohn Dyson if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) { 11618d220203SAlan Cox vm_pagequeue_lock(pq); 11622965a453SKip Macy vm_page_unlock(m); 116389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 116448cc2fc7SKonstantin Belousov queues_locked = TRUE; 11658d220203SAlan Cox vm_page_requeue_locked(m); 116648cc2fc7SKonstantin Belousov goto relock_queues; 116712ac6a1dSJohn Dyson } 116812ac6a1dSJohn Dyson 11691c7c3c6aSMatthew Dillon /* 11702b6b0df7SMatthew Dillon * The object is already known NOT to be dead. It 11712b6b0df7SMatthew Dillon * is possible for the vget() to block the whole 11722b6b0df7SMatthew Dillon * pageout daemon, but the new low-memory handling 11732b6b0df7SMatthew Dillon * code should prevent it. 11741c7c3c6aSMatthew Dillon * 11752b6b0df7SMatthew Dillon * The previous code skipped locked vnodes and, worse, 11762b6b0df7SMatthew Dillon * reordered pages in the queue. This results in 11772b6b0df7SMatthew Dillon * completely non-deterministic operation and, on a 11782b6b0df7SMatthew Dillon * busy system, can lead to extremely non-optimal 11792b6b0df7SMatthew Dillon * pageouts. For example, it can cause clean pages 11802b6b0df7SMatthew Dillon * to be freed and dirty pages to be moved to the end 11812b6b0df7SMatthew Dillon * of the queue. Since dirty pages are also moved to 11822b6b0df7SMatthew Dillon * the end of the queue once-cleaned, this gives 1183ab46f63eSJohn Baldwin * way too large a weighting to deferring the freeing 11842b6b0df7SMatthew Dillon * of dirty pages. 11851c7c3c6aSMatthew Dillon * 118623b59018SMatthew Dillon * We can't wait forever for the vnode lock, we might 118723b59018SMatthew Dillon * deadlock due to a vn_read() getting stuck in 118823b59018SMatthew Dillon * vm_wait while holding this vnode. We skip the 118923b59018SMatthew Dillon * vnode if we can't get it in a reasonable amount 119023b59018SMatthew Dillon * of time. 11911c7c3c6aSMatthew Dillon */ 11921c7c3c6aSMatthew Dillon if (object->type == OBJT_VNODE) { 11932965a453SKip Macy vm_page_unlock(m); 119424a1cce3SDavid Greenman vp = object->handle; 1195db27dcc0STor Egge if (vp->v_type == VREG && 1196db27dcc0STor Egge vn_start_write(vp, &mp, V_NOWAIT) != 0) { 11976129343dSKonstantin Belousov mp = NULL; 1198db27dcc0STor Egge ++pageout_lock_miss; 1199db27dcc0STor Egge if (object->flags & OBJ_MIGHTBEDIRTY) 1200db27dcc0STor Egge vnodes_skipped++; 1201625e6c0aSTor Egge goto unlock_and_continue; 1202db27dcc0STor Egge } 1203b9f180d1SKonstantin Belousov KASSERT(mp != NULL, 1204b9f180d1SKonstantin Belousov ("vp %p with NULL v_mount", vp)); 120514137dc0SAlan Cox vm_object_reference_locked(object); 120689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 12071bd7d0b7SKonstantin Belousov lockmode = MNT_SHARED_WRITES(vp->v_mount) ? 12081bd7d0b7SKonstantin Belousov LK_SHARED : LK_EXCLUSIVE; 12091bd7d0b7SKonstantin Belousov if (vget(vp, lockmode | LK_TIMELOCK, 121097824da3SAlan Cox curthread)) { 121189f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 121223b59018SMatthew Dillon ++pageout_lock_miss; 1213aef922f5SJohn Dyson if (object->flags & OBJ_MIGHTBEDIRTY) 1214925a3a41SJohn Dyson vnodes_skipped++; 1215625e6c0aSTor Egge vp = NULL; 1216625e6c0aSTor Egge goto unlock_and_continue; 121785a376ebSJohn Dyson } 121889f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 12192965a453SKip Macy vm_page_lock(m); 12208d220203SAlan Cox vm_pagequeue_lock(pq); 122148cc2fc7SKonstantin Belousov queues_locked = TRUE; 1222f35329acSJohn Dyson /* 1223936524aaSMatthew Dillon * The page might have been moved to another 1224936524aaSMatthew Dillon * queue during potential blocking in vget() 1225936524aaSMatthew Dillon * above. The page might have been freed and 122614137dc0SAlan Cox * reused for another vnode. 1227f35329acSJohn Dyson */ 12289cf51988SAlan Cox if (m->queue != PQ_INACTIVE || 1229936524aaSMatthew Dillon m->object != object || 1230c325e866SKonstantin Belousov TAILQ_NEXT(m, plinks.q) != &vmd->vmd_marker) { 12312965a453SKip Macy vm_page_unlock(m); 1232b182ec9eSJohn Dyson if (object->flags & OBJ_MIGHTBEDIRTY) 1233925a3a41SJohn Dyson vnodes_skipped++; 12343562af12SAlan Cox goto unlock_and_continue; 1235b182ec9eSJohn Dyson } 1236b182ec9eSJohn Dyson 1237f35329acSJohn Dyson /* 1238936524aaSMatthew Dillon * The page may have been busied during the 123914137dc0SAlan Cox * blocking in vget(). We don't move the 1240936524aaSMatthew Dillon * page back onto the end of the queue so that 1241936524aaSMatthew Dillon * statistics are more correct if we don't. 1242f35329acSJohn Dyson */ 1243c7aebda8SAttilio Rao if (vm_page_busied(m)) { 12442965a453SKip Macy vm_page_unlock(m); 12459099545aSAlan Cox addl_page_shortage++; 12463562af12SAlan Cox goto unlock_and_continue; 1247b182ec9eSJohn Dyson } 1248b182ec9eSJohn Dyson 1249f35329acSJohn Dyson /* 125057601bcbSMatthew Dillon * If the page has become held it might 125157601bcbSMatthew Dillon * be undergoing I/O, so skip it 1252f35329acSJohn Dyson */ 12539099545aSAlan Cox if (m->hold_count != 0) { 12542965a453SKip Macy vm_page_unlock(m); 12559099545aSAlan Cox addl_page_shortage++; 1256b182ec9eSJohn Dyson if (object->flags & OBJ_MIGHTBEDIRTY) 1257925a3a41SJohn Dyson vnodes_skipped++; 12583562af12SAlan Cox goto unlock_and_continue; 1259f6b04d2bSDavid Greenman } 12608d220203SAlan Cox vm_pagequeue_unlock(pq); 1261a4156419SKonstantin Belousov queues_locked = FALSE; 1262f6b04d2bSDavid Greenman } 1263f6b04d2bSDavid Greenman 12640d94caffSDavid Greenman /* 12650d94caffSDavid Greenman * If a page is dirty, then it is either being washed 12660d94caffSDavid Greenman * (but not yet cleaned) or it is still in the 12670d94caffSDavid Greenman * laundry. If it is still in the laundry, then we 12682b6b0df7SMatthew Dillon * start the cleaning operation. 1269936524aaSMatthew Dillon * 12702b6b0df7SMatthew Dillon * decrement page_shortage on success to account for 12712b6b0df7SMatthew Dillon * the (future) cleaned page. Otherwise we could wind 12722b6b0df7SMatthew Dillon * up laundering or cleaning too many pages. 12730d94caffSDavid Greenman */ 12742b6b0df7SMatthew Dillon if (vm_pageout_clean(m) != 0) { 12752b6b0df7SMatthew Dillon --page_shortage; 1276936524aaSMatthew Dillon --maxlaunder; 12772b6b0df7SMatthew Dillon } 12783562af12SAlan Cox unlock_and_continue: 12792965a453SKip Macy vm_page_lock_assert(m, MA_NOTOWNED); 128089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 128114137dc0SAlan Cox if (mp != NULL) { 128248cc2fc7SKonstantin Belousov if (queues_locked) { 12838d220203SAlan Cox vm_pagequeue_unlock(pq); 128448cc2fc7SKonstantin Belousov queues_locked = FALSE; 128548cc2fc7SKonstantin Belousov } 128614137dc0SAlan Cox if (vp != NULL) 1287f6b04d2bSDavid Greenman vput(vp); 128814137dc0SAlan Cox vm_object_deallocate(object); 1289f2a2857bSKirk McKusick vn_finished_write(mp); 129048cc2fc7SKonstantin Belousov } 129148cc2fc7SKonstantin Belousov vm_page_lock_assert(m, MA_NOTOWNED); 129248cc2fc7SKonstantin Belousov goto relock_queues; 129348cc2fc7SKonstantin Belousov } 129448cc2fc7SKonstantin Belousov vm_page_unlock(m); 129589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 129648cc2fc7SKonstantin Belousov relock_queues: 129748cc2fc7SKonstantin Belousov if (!queues_locked) { 12988d220203SAlan Cox vm_pagequeue_lock(pq); 129948cc2fc7SKonstantin Belousov queues_locked = TRUE; 13006989c456SAlan Cox } 1301c325e866SKonstantin Belousov next = TAILQ_NEXT(&vmd->vmd_marker, plinks.q); 1302c325e866SKonstantin Belousov TAILQ_REMOVE(&pq->pq_pl, &vmd->vmd_marker, plinks.q); 13030d94caffSDavid Greenman } 13048d220203SAlan Cox vm_pagequeue_unlock(pq); 130526f9a767SRodney W. Grimes 13069452b5edSAlan Cox #if !defined(NO_SWAPPING) 13079452b5edSAlan Cox /* 13089452b5edSAlan Cox * Wakeup the swapout daemon if we didn't cache or free the targeted 13099452b5edSAlan Cox * number of pages. 13109452b5edSAlan Cox */ 13119452b5edSAlan Cox if (vm_swap_enabled && page_shortage > 0) 13129452b5edSAlan Cox vm_req_vmdaemon(VM_SWAP_NORMAL); 13139452b5edSAlan Cox #endif 13149452b5edSAlan Cox 13159452b5edSAlan Cox /* 13169452b5edSAlan Cox * Wakeup the sync daemon if we skipped a vnode in a writeable object 13179452b5edSAlan Cox * and we didn't cache or free enough pages. 13189452b5edSAlan Cox */ 13199452b5edSAlan Cox if (vnodes_skipped > 0 && page_shortage > vm_cnt.v_free_target - 13209452b5edSAlan Cox vm_cnt.v_free_min) 13219452b5edSAlan Cox (void)speedup_syncer(); 13229452b5edSAlan Cox 1323df8bae1dSRodney W. Grimes /* 1324936524aaSMatthew Dillon * Compute the number of pages we want to try to move from the 1325936524aaSMatthew Dillon * active queue to the inactive queue. 13261c7c3c6aSMatthew Dillon */ 132744f1c916SBryan Drewery page_shortage = vm_cnt.v_inactive_target - vm_cnt.v_inactive_count + 13289099545aSAlan Cox vm_paging_target() + deficit + addl_page_shortage; 13299099545aSAlan Cox 1330114f62c6SJeff Roberson pq = &vmd->vmd_pagequeues[PQ_ACTIVE]; 1331114f62c6SJeff Roberson vm_pagequeue_lock(pq); 13329099545aSAlan Cox maxscan = pq->pq_cnt; 13339099545aSAlan Cox 1334d9e23210SJeff Roberson /* 1335d9e23210SJeff Roberson * If we're just idle polling attempt to visit every 1336d9e23210SJeff Roberson * active page within 'update_period' seconds. 1337d9e23210SJeff Roberson */ 1338d9e23210SJeff Roberson if (pass == 0 && vm_pageout_update_period != 0) { 13399099545aSAlan Cox maxscan /= vm_pageout_update_period; 13409099545aSAlan Cox page_shortage = maxscan; 1341d9e23210SJeff Roberson } 13421c7c3c6aSMatthew Dillon 13431c7c3c6aSMatthew Dillon /* 1344936524aaSMatthew Dillon * Scan the active queue for things we can deactivate. We nominally 1345936524aaSMatthew Dillon * track the per-page activity counter and use it to locate 1346936524aaSMatthew Dillon * deactivation candidates. 13471c7c3c6aSMatthew Dillon */ 13488d220203SAlan Cox m = TAILQ_FIRST(&pq->pq_pl); 13499099545aSAlan Cox while (m != NULL && maxscan-- > 0 && page_shortage > 0) { 1350f35329acSJohn Dyson 13519cf51988SAlan Cox KASSERT(m->queue == PQ_ACTIVE, 1352d3c09dd7SAlan Cox ("vm_pageout_scan: page %p isn't active", m)); 1353f35329acSJohn Dyson 1354c325e866SKonstantin Belousov next = TAILQ_NEXT(m, plinks.q); 13558dbca793STor Egge if ((m->flags & PG_MARKER) != 0) { 13568dbca793STor Egge m = next; 13578dbca793STor Egge continue; 13588dbca793STor Egge } 13597900f95dSKonstantin Belousov KASSERT((m->flags & PG_FICTITIOUS) == 0, 13607900f95dSKonstantin Belousov ("Fictitious page %p cannot be in active queue", m)); 13617900f95dSKonstantin Belousov KASSERT((m->oflags & VPO_UNMANAGED) == 0, 13627900f95dSKonstantin Belousov ("Unmanaged page %p cannot be in active queue", m)); 13639ee2165fSAlan Cox if (!vm_pageout_page_lock(m, &next)) { 13648c616246SKonstantin Belousov vm_page_unlock(m); 13652965a453SKip Macy m = next; 13662965a453SKip Macy continue; 13672965a453SKip Macy } 1368b18bfc3dSJohn Dyson 1369b18bfc3dSJohn Dyson /* 1370b18bfc3dSJohn Dyson * The count for pagedaemon pages is done after checking the 1371956f3135SPhilippe Charnier * page for eligibility... 1372b18bfc3dSJohn Dyson */ 13738d220203SAlan Cox PCPU_INC(cnt.v_pdpages); 1374ef743ce6SJohn Dyson 13757e006499SJohn Dyson /* 13767e006499SJohn Dyson * Check to see "how much" the page has been used. 13777e006499SJohn Dyson */ 137886fa2471SAlan Cox if ((m->aflags & PGA_REFERENCED) != 0) { 1379bb7858eaSJeff Roberson vm_page_aflag_clear(m, PGA_REFERENCED); 138086fa2471SAlan Cox act_delta = 1; 138186fa2471SAlan Cox } else 138286fa2471SAlan Cox act_delta = 0; 138386fa2471SAlan Cox 1384274132acSJeff Roberson /* 1385274132acSJeff Roberson * Unlocked object ref count check. Two races are possible. 1386274132acSJeff Roberson * 1) The ref was transitioning to zero and we saw non-zero, 1387274132acSJeff Roberson * the pmap bits will be checked unnecessarily. 1388274132acSJeff Roberson * 2) The ref was transitioning to one and we saw zero. 1389274132acSJeff Roberson * The page lock prevents a new reference to this page so 1390274132acSJeff Roberson * we need not check the reference bits. 1391274132acSJeff Roberson */ 1392274132acSJeff Roberson if (m->object->ref_count != 0) 1393bb7858eaSJeff Roberson act_delta += pmap_ts_referenced(m); 1394bb7858eaSJeff Roberson 1395bb7858eaSJeff Roberson /* 1396bb7858eaSJeff Roberson * Advance or decay the act_count based on recent usage. 1397bb7858eaSJeff Roberson */ 139886fa2471SAlan Cox if (act_delta != 0) { 1399bb7858eaSJeff Roberson m->act_count += ACT_ADVANCE + act_delta; 140038efa82bSJohn Dyson if (m->act_count > ACT_MAX) 140138efa82bSJohn Dyson m->act_count = ACT_MAX; 140286fa2471SAlan Cox } else 140338efa82bSJohn Dyson m->act_count -= min(m->act_count, ACT_DECLINE); 1404bb7858eaSJeff Roberson 1405bb7858eaSJeff Roberson /* 1406bb7858eaSJeff Roberson * Move this page to the tail of the active or inactive 1407bb7858eaSJeff Roberson * queue depending on usage. 1408bb7858eaSJeff Roberson */ 140986fa2471SAlan Cox if (m->act_count == 0) { 14108d220203SAlan Cox /* Dequeue to avoid later lock recursion. */ 14118d220203SAlan Cox vm_page_dequeue_locked(m); 1412d4a272dbSJohn Dyson vm_page_deactivate(m); 1413bb7858eaSJeff Roberson page_shortage--; 14148d220203SAlan Cox } else 14158d220203SAlan Cox vm_page_requeue_locked(m); 14162965a453SKip Macy vm_page_unlock(m); 141726f9a767SRodney W. Grimes m = next; 141826f9a767SRodney W. Grimes } 14198d220203SAlan Cox vm_pagequeue_unlock(pq); 1420ceb0cf87SJohn Dyson #if !defined(NO_SWAPPING) 1421ceb0cf87SJohn Dyson /* 1422ceb0cf87SJohn Dyson * Idle process swapout -- run once per second. 1423ceb0cf87SJohn Dyson */ 1424ceb0cf87SJohn Dyson if (vm_swap_idle_enabled) { 1425ceb0cf87SJohn Dyson static long lsec; 1426227ee8a1SPoul-Henning Kamp if (time_second != lsec) { 142797824da3SAlan Cox vm_req_vmdaemon(VM_SWAP_IDLE); 1428227ee8a1SPoul-Henning Kamp lsec = time_second; 1429ceb0cf87SJohn Dyson } 1430ceb0cf87SJohn Dyson } 1431ceb0cf87SJohn Dyson #endif 1432ceb0cf87SJohn Dyson 14335663e6deSDavid Greenman /* 1434e92686d0SDavid Schultz * If we are critically low on one of RAM or swap and low on 1435e92686d0SDavid Schultz * the other, kill the largest process. However, we avoid 1436e92686d0SDavid Schultz * doing this on the first pass in order to give ourselves a 1437e92686d0SDavid Schultz * chance to flush out dirty vnode-backed pages and to allow 1438e92686d0SDavid Schultz * active pages to be moved to the inactive queue and reclaimed. 14392025d69bSKonstantin Belousov */ 1440449c2e92SKonstantin Belousov vm_pageout_mightbe_oom(vmd, pass); 14412025d69bSKonstantin Belousov } 14422025d69bSKonstantin Belousov 1443449c2e92SKonstantin Belousov static int vm_pageout_oom_vote; 1444449c2e92SKonstantin Belousov 1445449c2e92SKonstantin Belousov /* 1446449c2e92SKonstantin Belousov * The pagedaemon threads randlomly select one to perform the 1447449c2e92SKonstantin Belousov * OOM. Trying to kill processes before all pagedaemons 1448449c2e92SKonstantin Belousov * failed to reach free target is premature. 1449449c2e92SKonstantin Belousov */ 1450449c2e92SKonstantin Belousov static void 1451449c2e92SKonstantin Belousov vm_pageout_mightbe_oom(struct vm_domain *vmd, int pass) 1452449c2e92SKonstantin Belousov { 1453449c2e92SKonstantin Belousov int old_vote; 1454449c2e92SKonstantin Belousov 1455d9e23210SJeff Roberson if (pass <= 1 || !((swap_pager_avail < 64 && vm_page_count_min()) || 1456449c2e92SKonstantin Belousov (swap_pager_full && vm_paging_target() > 0))) { 1457449c2e92SKonstantin Belousov if (vmd->vmd_oom) { 1458449c2e92SKonstantin Belousov vmd->vmd_oom = FALSE; 1459449c2e92SKonstantin Belousov atomic_subtract_int(&vm_pageout_oom_vote, 1); 1460449c2e92SKonstantin Belousov } 1461449c2e92SKonstantin Belousov return; 1462449c2e92SKonstantin Belousov } 1463449c2e92SKonstantin Belousov 1464449c2e92SKonstantin Belousov if (vmd->vmd_oom) 1465449c2e92SKonstantin Belousov return; 1466449c2e92SKonstantin Belousov 1467449c2e92SKonstantin Belousov vmd->vmd_oom = TRUE; 1468449c2e92SKonstantin Belousov old_vote = atomic_fetchadd_int(&vm_pageout_oom_vote, 1); 1469449c2e92SKonstantin Belousov if (old_vote != vm_ndomains - 1) 1470449c2e92SKonstantin Belousov return; 1471449c2e92SKonstantin Belousov 1472449c2e92SKonstantin Belousov /* 1473449c2e92SKonstantin Belousov * The current pagedaemon thread is the last in the quorum to 1474449c2e92SKonstantin Belousov * start OOM. Initiate the selection and signaling of the 1475449c2e92SKonstantin Belousov * victim. 1476449c2e92SKonstantin Belousov */ 1477449c2e92SKonstantin Belousov vm_pageout_oom(VM_OOM_MEM); 1478449c2e92SKonstantin Belousov 1479449c2e92SKonstantin Belousov /* 1480449c2e92SKonstantin Belousov * After one round of OOM terror, recall our vote. On the 1481449c2e92SKonstantin Belousov * next pass, current pagedaemon would vote again if the low 1482449c2e92SKonstantin Belousov * memory condition is still there, due to vmd_oom being 1483449c2e92SKonstantin Belousov * false. 1484449c2e92SKonstantin Belousov */ 1485449c2e92SKonstantin Belousov vmd->vmd_oom = FALSE; 1486449c2e92SKonstantin Belousov atomic_subtract_int(&vm_pageout_oom_vote, 1); 1487449c2e92SKonstantin Belousov } 14882025d69bSKonstantin Belousov 14892025d69bSKonstantin Belousov void 14902025d69bSKonstantin Belousov vm_pageout_oom(int shortage) 14912025d69bSKonstantin Belousov { 14922025d69bSKonstantin Belousov struct proc *p, *bigproc; 14932025d69bSKonstantin Belousov vm_offset_t size, bigsize; 14942025d69bSKonstantin Belousov struct thread *td; 14956bed074cSKonstantin Belousov struct vmspace *vm; 14962025d69bSKonstantin Belousov 14972025d69bSKonstantin Belousov /* 14981c58e4e5SJohn Baldwin * We keep the process bigproc locked once we find it to keep anyone 14991c58e4e5SJohn Baldwin * from messing with it; however, there is a possibility of 15001c58e4e5SJohn Baldwin * deadlock if process B is bigproc and one of it's child processes 15011c58e4e5SJohn Baldwin * attempts to propagate a signal to B while we are waiting for A's 15021c58e4e5SJohn Baldwin * lock while walking this list. To avoid this, we don't block on 15031c58e4e5SJohn Baldwin * the process lock but just skip a process if it is already locked. 15045663e6deSDavid Greenman */ 15055663e6deSDavid Greenman bigproc = NULL; 15065663e6deSDavid Greenman bigsize = 0; 15071005a129SJohn Baldwin sx_slock(&allproc_lock); 1508e602ba25SJulian Elischer FOREACH_PROC_IN_SYSTEM(p) { 1509e602ba25SJulian Elischer int breakout; 1510dcbcd518SBruce Evans 15111c58e4e5SJohn Baldwin if (PROC_TRYLOCK(p) == 0) 15121c58e4e5SJohn Baldwin continue; 15131c58e4e5SJohn Baldwin /* 15143f1c4c4fSKonstantin Belousov * If this is a system, protected or killed process, skip it. 15155663e6deSDavid Greenman */ 15168e6fa660SJohn Baldwin if (p->p_state != PRS_NORMAL || 15178e6fa660SJohn Baldwin (p->p_flag & (P_INEXEC | P_PROTECTED | P_SYSTEM)) || 15183f1c4c4fSKonstantin Belousov (p->p_pid == 1) || P_KILLED(p) || 15198f60c087SPoul-Henning Kamp ((p->p_pid < 48) && (swap_pager_avail != 0))) { 15208606d880SJohn Baldwin PROC_UNLOCK(p); 15215663e6deSDavid Greenman continue; 15225663e6deSDavid Greenman } 15235663e6deSDavid Greenman /* 1524dcbcd518SBruce Evans * If the process is in a non-running type state, 1525e602ba25SJulian Elischer * don't touch it. Check all the threads individually. 15265663e6deSDavid Greenman */ 1527e602ba25SJulian Elischer breakout = 0; 1528e602ba25SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 1529982d11f8SJeff Roberson thread_lock(td); 153071fad9fdSJulian Elischer if (!TD_ON_RUNQ(td) && 153171fad9fdSJulian Elischer !TD_IS_RUNNING(td) && 1532f497cda2SEdward Tomasz Napierala !TD_IS_SLEEPING(td) && 1533f497cda2SEdward Tomasz Napierala !TD_IS_SUSPENDED(td)) { 1534982d11f8SJeff Roberson thread_unlock(td); 1535e602ba25SJulian Elischer breakout = 1; 1536e602ba25SJulian Elischer break; 1537e602ba25SJulian Elischer } 1538982d11f8SJeff Roberson thread_unlock(td); 1539e602ba25SJulian Elischer } 1540e602ba25SJulian Elischer if (breakout) { 15411c58e4e5SJohn Baldwin PROC_UNLOCK(p); 15425663e6deSDavid Greenman continue; 15435663e6deSDavid Greenman } 15445663e6deSDavid Greenman /* 15455663e6deSDavid Greenman * get the process size 15465663e6deSDavid Greenman */ 15476bed074cSKonstantin Belousov vm = vmspace_acquire_ref(p); 15486bed074cSKonstantin Belousov if (vm == NULL) { 15496bed074cSKonstantin Belousov PROC_UNLOCK(p); 15506bed074cSKonstantin Belousov continue; 15516bed074cSKonstantin Belousov } 15526bed074cSKonstantin Belousov if (!vm_map_trylock_read(&vm->vm_map)) { 15536bed074cSKonstantin Belousov vmspace_free(vm); 155472d97679SDavid Schultz PROC_UNLOCK(p); 155572d97679SDavid Schultz continue; 155672d97679SDavid Schultz } 15577981aa24SKonstantin Belousov size = vmspace_swap_count(vm); 15586bed074cSKonstantin Belousov vm_map_unlock_read(&vm->vm_map); 15592025d69bSKonstantin Belousov if (shortage == VM_OOM_MEM) 15606bed074cSKonstantin Belousov size += vmspace_resident_count(vm); 15616bed074cSKonstantin Belousov vmspace_free(vm); 15625663e6deSDavid Greenman /* 15635663e6deSDavid Greenman * if the this process is bigger than the biggest one 15645663e6deSDavid Greenman * remember it. 15655663e6deSDavid Greenman */ 15665663e6deSDavid Greenman if (size > bigsize) { 15671c58e4e5SJohn Baldwin if (bigproc != NULL) 15681c58e4e5SJohn Baldwin PROC_UNLOCK(bigproc); 15695663e6deSDavid Greenman bigproc = p; 15705663e6deSDavid Greenman bigsize = size; 15711c58e4e5SJohn Baldwin } else 15721c58e4e5SJohn Baldwin PROC_UNLOCK(p); 15735663e6deSDavid Greenman } 15741005a129SJohn Baldwin sx_sunlock(&allproc_lock); 15755663e6deSDavid Greenman if (bigproc != NULL) { 1576729b1e51SDavid Greenman killproc(bigproc, "out of swap space"); 1577fa885116SJulian Elischer sched_nice(bigproc, PRIO_MIN); 15781c58e4e5SJohn Baldwin PROC_UNLOCK(bigproc); 157944f1c916SBryan Drewery wakeup(&vm_cnt.v_free_count); 15805663e6deSDavid Greenman } 15815663e6deSDavid Greenman } 158226f9a767SRodney W. Grimes 1583449c2e92SKonstantin Belousov static void 1584449c2e92SKonstantin Belousov vm_pageout_worker(void *arg) 1585449c2e92SKonstantin Belousov { 1586449c2e92SKonstantin Belousov struct vm_domain *domain; 1587949c9186SKonstantin Belousov int domidx; 1588449c2e92SKonstantin Belousov 1589449c2e92SKonstantin Belousov domidx = (uintptr_t)arg; 1590449c2e92SKonstantin Belousov domain = &vm_dom[domidx]; 1591449c2e92SKonstantin Belousov 1592449c2e92SKonstantin Belousov /* 1593949c9186SKonstantin Belousov * XXXKIB It could be useful to bind pageout daemon threads to 1594949c9186SKonstantin Belousov * the cores belonging to the domain, from which vm_page_array 1595949c9186SKonstantin Belousov * is allocated. 1596449c2e92SKonstantin Belousov */ 1597449c2e92SKonstantin Belousov 1598449c2e92SKonstantin Belousov KASSERT(domain->vmd_segs != 0, ("domain without segments")); 1599449c2e92SKonstantin Belousov vm_pageout_init_marker(&domain->vmd_marker, PQ_INACTIVE); 1600449c2e92SKonstantin Belousov 1601449c2e92SKonstantin Belousov /* 1602449c2e92SKonstantin Belousov * The pageout daemon worker is never done, so loop forever. 1603449c2e92SKonstantin Belousov */ 1604449c2e92SKonstantin Belousov while (TRUE) { 1605449c2e92SKonstantin Belousov /* 1606449c2e92SKonstantin Belousov * If we have enough free memory, wakeup waiters. Do 1607449c2e92SKonstantin Belousov * not clear vm_pages_needed until we reach our target, 1608449c2e92SKonstantin Belousov * otherwise we may be woken up over and over again and 1609449c2e92SKonstantin Belousov * waste a lot of cpu. 1610449c2e92SKonstantin Belousov */ 1611449c2e92SKonstantin Belousov mtx_lock(&vm_page_queue_free_mtx); 1612449c2e92SKonstantin Belousov if (vm_pages_needed && !vm_page_count_min()) { 1613449c2e92SKonstantin Belousov if (!vm_paging_needed()) 1614449c2e92SKonstantin Belousov vm_pages_needed = 0; 161544f1c916SBryan Drewery wakeup(&vm_cnt.v_free_count); 1616449c2e92SKonstantin Belousov } 1617449c2e92SKonstantin Belousov if (vm_pages_needed) { 1618449c2e92SKonstantin Belousov /* 1619449c2e92SKonstantin Belousov * Still not done, take a second pass without waiting 1620449c2e92SKonstantin Belousov * (unlimited dirty cleaning), otherwise sleep a bit 1621449c2e92SKonstantin Belousov * and try again. 1622449c2e92SKonstantin Belousov */ 1623449c2e92SKonstantin Belousov if (domain->vmd_pass > 1) 1624449c2e92SKonstantin Belousov msleep(&vm_pages_needed, 1625449c2e92SKonstantin Belousov &vm_page_queue_free_mtx, PVM, "psleep", 1626449c2e92SKonstantin Belousov hz / 2); 1627449c2e92SKonstantin Belousov } else { 1628449c2e92SKonstantin Belousov /* 1629d9e23210SJeff Roberson * Good enough, sleep until required to refresh 1630d9e23210SJeff Roberson * stats. 1631449c2e92SKonstantin Belousov */ 1632449c2e92SKonstantin Belousov domain->vmd_pass = 0; 1633d9e23210SJeff Roberson msleep(&vm_pages_needed, &vm_page_queue_free_mtx, 1634d9e23210SJeff Roberson PVM, "psleep", hz); 1635d9e23210SJeff Roberson 1636449c2e92SKonstantin Belousov } 1637d9e23210SJeff Roberson if (vm_pages_needed) { 163844f1c916SBryan Drewery vm_cnt.v_pdwakeups++; 1639d9e23210SJeff Roberson domain->vmd_pass++; 1640d9e23210SJeff Roberson } 1641449c2e92SKonstantin Belousov mtx_unlock(&vm_page_queue_free_mtx); 1642449c2e92SKonstantin Belousov vm_pageout_scan(domain, domain->vmd_pass); 1643449c2e92SKonstantin Belousov } 1644449c2e92SKonstantin Belousov } 1645449c2e92SKonstantin Belousov 1646df8bae1dSRodney W. Grimes /* 1647*4d19f4adSSteven Hartland * vm_pageout_init initialises basic pageout daemon settings. 1648df8bae1dSRodney W. Grimes */ 16492b14f991SJulian Elischer static void 1650*4d19f4adSSteven Hartland vm_pageout_init(void) 1651df8bae1dSRodney W. Grimes { 1652df8bae1dSRodney W. Grimes /* 1653df8bae1dSRodney W. Grimes * Initialize some paging parameters. 1654df8bae1dSRodney W. Grimes */ 165544f1c916SBryan Drewery vm_cnt.v_interrupt_free_min = 2; 165644f1c916SBryan Drewery if (vm_cnt.v_page_count < 2000) 1657f35329acSJohn Dyson vm_pageout_page_count = 8; 1658f6b04d2bSDavid Greenman 165945ae1d91SAlan Cox /* 166045ae1d91SAlan Cox * v_free_reserved needs to include enough for the largest 166145ae1d91SAlan Cox * swap pager structures plus enough for any pv_entry structs 166245ae1d91SAlan Cox * when paging. 166345ae1d91SAlan Cox */ 166444f1c916SBryan Drewery if (vm_cnt.v_page_count > 1024) 166544f1c916SBryan Drewery vm_cnt.v_free_min = 4 + (vm_cnt.v_page_count - 1024) / 200; 16662feb50bfSAttilio Rao else 166744f1c916SBryan Drewery vm_cnt.v_free_min = 4; 166844f1c916SBryan Drewery vm_cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE + 166944f1c916SBryan Drewery vm_cnt.v_interrupt_free_min; 167044f1c916SBryan Drewery vm_cnt.v_free_reserved = vm_pageout_page_count + 167144f1c916SBryan Drewery vm_cnt.v_pageout_free_min + (vm_cnt.v_page_count / 768); 167244f1c916SBryan Drewery vm_cnt.v_free_severe = vm_cnt.v_free_min / 2; 167344f1c916SBryan Drewery vm_cnt.v_free_target = 4 * vm_cnt.v_free_min + vm_cnt.v_free_reserved; 167444f1c916SBryan Drewery vm_cnt.v_free_min += vm_cnt.v_free_reserved; 167544f1c916SBryan Drewery vm_cnt.v_free_severe += vm_cnt.v_free_reserved; 167644f1c916SBryan Drewery vm_cnt.v_inactive_target = (3 * vm_cnt.v_free_target) / 2; 167744f1c916SBryan Drewery if (vm_cnt.v_inactive_target > vm_cnt.v_free_count / 3) 167844f1c916SBryan Drewery vm_cnt.v_inactive_target = vm_cnt.v_free_count / 3; 1679df8bae1dSRodney W. Grimes 1680d9e23210SJeff Roberson /* 1681d9e23210SJeff Roberson * Set the default wakeup threshold to be 10% above the minimum 1682d9e23210SJeff Roberson * page limit. This keeps the steady state out of shortfall. 1683d9e23210SJeff Roberson */ 168444f1c916SBryan Drewery vm_pageout_wakeup_thresh = (vm_cnt.v_free_min / 10) * 11; 1685d9e23210SJeff Roberson 1686d9e23210SJeff Roberson /* 1687d9e23210SJeff Roberson * Set interval in seconds for active scan. We want to visit each 1688c9612b2dSJeff Roberson * page at least once every ten minutes. This is to prevent worst 1689c9612b2dSJeff Roberson * case paging behaviors with stale active LRU. 1690d9e23210SJeff Roberson */ 1691d9e23210SJeff Roberson if (vm_pageout_update_period == 0) 1692c9612b2dSJeff Roberson vm_pageout_update_period = 600; 1693d9e23210SJeff Roberson 1694df8bae1dSRodney W. Grimes /* XXX does not really belong here */ 1695df8bae1dSRodney W. Grimes if (vm_page_max_wired == 0) 169644f1c916SBryan Drewery vm_page_max_wired = vm_cnt.v_free_count / 3; 1697*4d19f4adSSteven Hartland } 1698*4d19f4adSSteven Hartland 1699*4d19f4adSSteven Hartland /* 1700*4d19f4adSSteven Hartland * vm_pageout is the high level pageout daemon. 1701*4d19f4adSSteven Hartland */ 1702*4d19f4adSSteven Hartland static void 1703*4d19f4adSSteven Hartland vm_pageout(void) 1704*4d19f4adSSteven Hartland { 1705*4d19f4adSSteven Hartland #if MAXMEMDOM > 1 1706*4d19f4adSSteven Hartland int error, i; 1707*4d19f4adSSteven Hartland #endif 1708df8bae1dSRodney W. Grimes 170924a1cce3SDavid Greenman swap_pager_swap_init(); 1710449c2e92SKonstantin Belousov #if MAXMEMDOM > 1 1711449c2e92SKonstantin Belousov for (i = 1; i < vm_ndomains; i++) { 1712449c2e92SKonstantin Belousov error = kthread_add(vm_pageout_worker, (void *)(uintptr_t)i, 1713449c2e92SKonstantin Belousov curproc, NULL, 0, 0, "dom%d", i); 1714449c2e92SKonstantin Belousov if (error != 0) { 1715449c2e92SKonstantin Belousov panic("starting pageout for domain %d, error %d\n", 1716449c2e92SKonstantin Belousov i, error); 1717dc2efb27SJohn Dyson } 1718f919ebdeSDavid Greenman } 1719449c2e92SKonstantin Belousov #endif 1720d395270dSDimitry Andric vm_pageout_worker((void *)(uintptr_t)0); 1721df8bae1dSRodney W. Grimes } 172226f9a767SRodney W. Grimes 17236b4b77adSAlan Cox /* 1724e9f995d8SAlan Cox * Unless the free page queue lock is held by the caller, this function 17256b4b77adSAlan Cox * should be regarded as advisory. Specifically, the caller should 172644f1c916SBryan Drewery * not msleep() on &vm_cnt.v_free_count following this function unless 1727e9f995d8SAlan Cox * the free page queue lock is held until the msleep() is performed. 17286b4b77adSAlan Cox */ 1729e0c5a895SJohn Dyson void 17304a365329SAndrey Zonov pagedaemon_wakeup(void) 1731e0c5a895SJohn Dyson { 1732a1c0a785SAlan Cox 1733b40ce416SJulian Elischer if (!vm_pages_needed && curthread->td_proc != pageproc) { 1734a1c0a785SAlan Cox vm_pages_needed = 1; 1735e0c5a895SJohn Dyson wakeup(&vm_pages_needed); 1736e0c5a895SJohn Dyson } 1737e0c5a895SJohn Dyson } 1738e0c5a895SJohn Dyson 173938efa82bSJohn Dyson #if !defined(NO_SWAPPING) 17405afce282SDavid Greenman static void 174197824da3SAlan Cox vm_req_vmdaemon(int req) 17425afce282SDavid Greenman { 17435afce282SDavid Greenman static int lastrun = 0; 17445afce282SDavid Greenman 174597824da3SAlan Cox mtx_lock(&vm_daemon_mtx); 174697824da3SAlan Cox vm_pageout_req_swapout |= req; 1747b18bfc3dSJohn Dyson if ((ticks > (lastrun + hz)) || (ticks < lastrun)) { 17485afce282SDavid Greenman wakeup(&vm_daemon_needed); 17495afce282SDavid Greenman lastrun = ticks; 17505afce282SDavid Greenman } 175197824da3SAlan Cox mtx_unlock(&vm_daemon_mtx); 17525afce282SDavid Greenman } 17535afce282SDavid Greenman 17542b14f991SJulian Elischer static void 17554a365329SAndrey Zonov vm_daemon(void) 17560d94caffSDavid Greenman { 175791d5354aSJohn Baldwin struct rlimit rsslim; 1758dcbcd518SBruce Evans struct proc *p; 1759dcbcd518SBruce Evans struct thread *td; 17606bed074cSKonstantin Belousov struct vmspace *vm; 1761099e7e95SEdward Tomasz Napierala int breakout, swapout_flags, tryagain, attempts; 1762afcc55f3SEdward Tomasz Napierala #ifdef RACCT 1763099e7e95SEdward Tomasz Napierala uint64_t rsize, ravailable; 1764afcc55f3SEdward Tomasz Napierala #endif 17650d94caffSDavid Greenman 17662fe6e4d7SDavid Greenman while (TRUE) { 176797824da3SAlan Cox mtx_lock(&vm_daemon_mtx); 1768099e7e95SEdward Tomasz Napierala #ifdef RACCT 1769099e7e95SEdward Tomasz Napierala msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", hz); 1770099e7e95SEdward Tomasz Napierala #else 177197824da3SAlan Cox msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", 0); 1772099e7e95SEdward Tomasz Napierala #endif 177397824da3SAlan Cox swapout_flags = vm_pageout_req_swapout; 17744c1f8ee9SDavid Greenman vm_pageout_req_swapout = 0; 177597824da3SAlan Cox mtx_unlock(&vm_daemon_mtx); 177697824da3SAlan Cox if (swapout_flags) 177797824da3SAlan Cox swapout_procs(swapout_flags); 177897824da3SAlan Cox 17792fe6e4d7SDavid Greenman /* 17800d94caffSDavid Greenman * scan the processes for exceeding their rlimits or if 17810d94caffSDavid Greenman * process is swapped out -- deactivate pages 17822fe6e4d7SDavid Greenman */ 1783099e7e95SEdward Tomasz Napierala tryagain = 0; 1784099e7e95SEdward Tomasz Napierala attempts = 0; 1785099e7e95SEdward Tomasz Napierala again: 1786099e7e95SEdward Tomasz Napierala attempts++; 17871005a129SJohn Baldwin sx_slock(&allproc_lock); 1788f67af5c9SXin LI FOREACH_PROC_IN_SYSTEM(p) { 1789fe2144fdSLuoqi Chen vm_pindex_t limit, size; 17902fe6e4d7SDavid Greenman 17912fe6e4d7SDavid Greenman /* 17922fe6e4d7SDavid Greenman * if this is a system process or if we have already 17932fe6e4d7SDavid Greenman * looked at this process, skip it. 17942fe6e4d7SDavid Greenman */ 1795897ecacdSJohn Baldwin PROC_LOCK(p); 17968e6fa660SJohn Baldwin if (p->p_state != PRS_NORMAL || 17978e6fa660SJohn Baldwin p->p_flag & (P_INEXEC | P_SYSTEM | P_WEXIT)) { 1798897ecacdSJohn Baldwin PROC_UNLOCK(p); 17992fe6e4d7SDavid Greenman continue; 18002fe6e4d7SDavid Greenman } 18012fe6e4d7SDavid Greenman /* 18022fe6e4d7SDavid Greenman * if the process is in a non-running type state, 18032fe6e4d7SDavid Greenman * don't touch it. 18042fe6e4d7SDavid Greenman */ 1805e602ba25SJulian Elischer breakout = 0; 1806e602ba25SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 1807982d11f8SJeff Roberson thread_lock(td); 180871fad9fdSJulian Elischer if (!TD_ON_RUNQ(td) && 180971fad9fdSJulian Elischer !TD_IS_RUNNING(td) && 1810f497cda2SEdward Tomasz Napierala !TD_IS_SLEEPING(td) && 1811f497cda2SEdward Tomasz Napierala !TD_IS_SUSPENDED(td)) { 1812982d11f8SJeff Roberson thread_unlock(td); 1813e602ba25SJulian Elischer breakout = 1; 1814e602ba25SJulian Elischer break; 1815e602ba25SJulian Elischer } 1816982d11f8SJeff Roberson thread_unlock(td); 1817e602ba25SJulian Elischer } 1818897ecacdSJohn Baldwin if (breakout) { 1819897ecacdSJohn Baldwin PROC_UNLOCK(p); 18202fe6e4d7SDavid Greenman continue; 18212fe6e4d7SDavid Greenman } 18222fe6e4d7SDavid Greenman /* 18232fe6e4d7SDavid Greenman * get a limit 18242fe6e4d7SDavid Greenman */ 1825dcbcd518SBruce Evans lim_rlimit(p, RLIMIT_RSS, &rsslim); 1826fe2144fdSLuoqi Chen limit = OFF_TO_IDX( 182791d5354aSJohn Baldwin qmin(rsslim.rlim_cur, rsslim.rlim_max)); 18282fe6e4d7SDavid Greenman 18292fe6e4d7SDavid Greenman /* 18300d94caffSDavid Greenman * let processes that are swapped out really be 18310d94caffSDavid Greenman * swapped out set the limit to nothing (will force a 18320d94caffSDavid Greenman * swap-out.) 18332fe6e4d7SDavid Greenman */ 1834b61ce5b0SJeff Roberson if ((p->p_flag & P_INMEM) == 0) 18350d94caffSDavid Greenman limit = 0; /* XXX */ 18366bed074cSKonstantin Belousov vm = vmspace_acquire_ref(p); 1837897ecacdSJohn Baldwin PROC_UNLOCK(p); 18386bed074cSKonstantin Belousov if (vm == NULL) 18396bed074cSKonstantin Belousov continue; 18402fe6e4d7SDavid Greenman 18416bed074cSKonstantin Belousov size = vmspace_resident_count(vm); 1842a406d8c3SEdward Tomasz Napierala if (size >= limit) { 1843fe2144fdSLuoqi Chen vm_pageout_map_deactivate_pages( 18446bed074cSKonstantin Belousov &vm->vm_map, limit); 18452fe6e4d7SDavid Greenman } 1846afcc55f3SEdward Tomasz Napierala #ifdef RACCT 1847099e7e95SEdward Tomasz Napierala rsize = IDX_TO_OFF(size); 1848099e7e95SEdward Tomasz Napierala PROC_LOCK(p); 1849099e7e95SEdward Tomasz Napierala racct_set(p, RACCT_RSS, rsize); 1850099e7e95SEdward Tomasz Napierala ravailable = racct_get_available(p, RACCT_RSS); 1851099e7e95SEdward Tomasz Napierala PROC_UNLOCK(p); 1852099e7e95SEdward Tomasz Napierala if (rsize > ravailable) { 1853099e7e95SEdward Tomasz Napierala /* 1854099e7e95SEdward Tomasz Napierala * Don't be overly aggressive; this might be 1855099e7e95SEdward Tomasz Napierala * an innocent process, and the limit could've 1856099e7e95SEdward Tomasz Napierala * been exceeded by some memory hog. Don't 1857099e7e95SEdward Tomasz Napierala * try to deactivate more than 1/4th of process' 1858099e7e95SEdward Tomasz Napierala * resident set size. 1859099e7e95SEdward Tomasz Napierala */ 1860099e7e95SEdward Tomasz Napierala if (attempts <= 8) { 1861099e7e95SEdward Tomasz Napierala if (ravailable < rsize - (rsize / 4)) 1862099e7e95SEdward Tomasz Napierala ravailable = rsize - (rsize / 4); 1863099e7e95SEdward Tomasz Napierala } 1864099e7e95SEdward Tomasz Napierala vm_pageout_map_deactivate_pages( 1865099e7e95SEdward Tomasz Napierala &vm->vm_map, OFF_TO_IDX(ravailable)); 1866099e7e95SEdward Tomasz Napierala /* Update RSS usage after paging out. */ 1867099e7e95SEdward Tomasz Napierala size = vmspace_resident_count(vm); 1868099e7e95SEdward Tomasz Napierala rsize = IDX_TO_OFF(size); 1869099e7e95SEdward Tomasz Napierala PROC_LOCK(p); 1870099e7e95SEdward Tomasz Napierala racct_set(p, RACCT_RSS, rsize); 1871099e7e95SEdward Tomasz Napierala PROC_UNLOCK(p); 1872099e7e95SEdward Tomasz Napierala if (rsize > ravailable) 1873099e7e95SEdward Tomasz Napierala tryagain = 1; 1874099e7e95SEdward Tomasz Napierala } 1875afcc55f3SEdward Tomasz Napierala #endif 18766bed074cSKonstantin Belousov vmspace_free(vm); 18772fe6e4d7SDavid Greenman } 18781005a129SJohn Baldwin sx_sunlock(&allproc_lock); 1879099e7e95SEdward Tomasz Napierala if (tryagain != 0 && attempts <= 10) 1880099e7e95SEdward Tomasz Napierala goto again; 188124a1cce3SDavid Greenman } 18822fe6e4d7SDavid Greenman } 1883a1287949SEivind Eklund #endif /* !defined(NO_SWAPPING) */ 1884