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" 79*14a0d74eSSteven Hartland #include "opt_kdtrace.h" 80df8bae1dSRodney W. Grimes #include <sys/param.h> 8126f9a767SRodney W. Grimes #include <sys/systm.h> 82b5e8ce9fSBruce Evans #include <sys/kernel.h> 83855a310fSJeff Roberson #include <sys/eventhandler.h> 84fb919e4dSMark Murray #include <sys/lock.h> 85fb919e4dSMark Murray #include <sys/mutex.h> 8626f9a767SRodney W. Grimes #include <sys/proc.h> 879c8b8baaSPeter Wemm #include <sys/kthread.h> 880384fff8SJason Evans #include <sys/ktr.h> 8997824da3SAlan Cox #include <sys/mount.h> 90099e7e95SEdward Tomasz Napierala #include <sys/racct.h> 9126f9a767SRodney W. Grimes #include <sys/resourcevar.h> 92b43179fbSJeff Roberson #include <sys/sched.h> 93*14a0d74eSSteven Hartland #include <sys/sdt.h> 94d2fc5315SPoul-Henning Kamp #include <sys/signalvar.h> 95449c2e92SKonstantin Belousov #include <sys/smp.h> 96f6b04d2bSDavid Greenman #include <sys/vnode.h> 97efeaf95aSDavid Greenman #include <sys/vmmeter.h> 9889f6b863SAttilio Rao #include <sys/rwlock.h> 991005a129SJohn Baldwin #include <sys/sx.h> 10038efa82bSJohn Dyson #include <sys/sysctl.h> 101df8bae1dSRodney W. Grimes 102df8bae1dSRodney W. Grimes #include <vm/vm.h> 103efeaf95aSDavid Greenman #include <vm/vm_param.h> 104efeaf95aSDavid Greenman #include <vm/vm_object.h> 105df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 106efeaf95aSDavid Greenman #include <vm/vm_map.h> 107df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h> 10824a1cce3SDavid Greenman #include <vm/vm_pager.h> 109449c2e92SKonstantin Belousov #include <vm/vm_phys.h> 11005f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h> 111efeaf95aSDavid Greenman #include <vm/vm_extern.h> 112670d17b5SJeff Roberson #include <vm/uma.h> 113df8bae1dSRodney W. Grimes 1142b14f991SJulian Elischer /* 1152b14f991SJulian Elischer * System initialization 1162b14f991SJulian Elischer */ 1172b14f991SJulian Elischer 1182b14f991SJulian Elischer /* the kernel process "vm_pageout"*/ 11911caded3SAlfred Perlstein static void vm_pageout(void); 1204d19f4adSSteven Hartland static void vm_pageout_init(void); 12111caded3SAlfred Perlstein static int vm_pageout_clean(vm_page_t); 122449c2e92SKonstantin Belousov static void vm_pageout_scan(struct vm_domain *vmd, int pass); 123449c2e92SKonstantin Belousov static void vm_pageout_mightbe_oom(struct vm_domain *vmd, int pass); 12445ae1d91SAlan Cox 1254d19f4adSSteven Hartland SYSINIT(pagedaemon_init, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, vm_pageout_init, 1264d19f4adSSteven Hartland NULL); 1274d19f4adSSteven Hartland 1282b14f991SJulian Elischer struct proc *pageproc; 1292b14f991SJulian Elischer 1302b14f991SJulian Elischer static struct kproc_desc page_kp = { 1312b14f991SJulian Elischer "pagedaemon", 1322b14f991SJulian Elischer vm_pageout, 1332b14f991SJulian Elischer &pageproc 1342b14f991SJulian Elischer }; 1354d19f4adSSteven Hartland SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_SECOND, kproc_start, 136237fdd78SRobert Watson &page_kp); 1372b14f991SJulian Elischer 138*14a0d74eSSteven Hartland SDT_PROVIDER_DEFINE(vm); 139*14a0d74eSSteven Hartland SDT_PROBE_DEFINE(vm, , , vm__lowmem_cache); 140*14a0d74eSSteven Hartland SDT_PROBE_DEFINE(vm, , , vm__lowmem_scan); 141*14a0d74eSSteven Hartland 14238efa82bSJohn Dyson #if !defined(NO_SWAPPING) 1432b14f991SJulian Elischer /* the kernel process "vm_daemon"*/ 14411caded3SAlfred Perlstein static void vm_daemon(void); 145f708ef1bSPoul-Henning Kamp static struct proc *vmproc; 1462b14f991SJulian Elischer 1472b14f991SJulian Elischer static struct kproc_desc vm_kp = { 1482b14f991SJulian Elischer "vmdaemon", 1492b14f991SJulian Elischer vm_daemon, 1502b14f991SJulian Elischer &vmproc 1512b14f991SJulian Elischer }; 152237fdd78SRobert Watson SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp); 15338efa82bSJohn Dyson #endif 1542b14f991SJulian Elischer 1552b14f991SJulian Elischer 1568b245767SAlan Cox int vm_pages_needed; /* Event on which pageout daemon sleeps */ 1578b245767SAlan Cox int vm_pageout_deficit; /* Estimated number of pages deficit */ 1588b245767SAlan Cox int vm_pageout_pages_needed; /* flag saying that the pageout daemon needs pages */ 159d9e23210SJeff Roberson int vm_pageout_wakeup_thresh; 16026f9a767SRodney W. Grimes 16138efa82bSJohn Dyson #if !defined(NO_SWAPPING) 162f708ef1bSPoul-Henning Kamp static int vm_pageout_req_swapout; /* XXX */ 163f708ef1bSPoul-Henning Kamp static int vm_daemon_needed; 16497824da3SAlan Cox static struct mtx vm_daemon_mtx; 16597824da3SAlan Cox /* Allow for use by vm_pageout before vm_daemon is initialized. */ 16697824da3SAlan Cox MTX_SYSINIT(vm_daemon, &vm_daemon_mtx, "vm daemon", MTX_DEF); 16738efa82bSJohn Dyson #endif 1682b6b0df7SMatthew Dillon static int vm_max_launder = 32; 169d9e23210SJeff Roberson static int vm_pageout_update_period; 1704a365329SAndrey Zonov static int defer_swap_pageouts; 1714a365329SAndrey Zonov static int disable_swap_pageouts; 172c9612b2dSJeff Roberson static int lowmem_period = 10; 173c9612b2dSJeff Roberson static int lowmem_ticks; 17470111b90SJohn Dyson 17538efa82bSJohn Dyson #if defined(NO_SWAPPING) 176303b270bSEivind Eklund static int vm_swap_enabled = 0; 177303b270bSEivind Eklund static int vm_swap_idle_enabled = 0; 17838efa82bSJohn Dyson #else 179303b270bSEivind Eklund static int vm_swap_enabled = 1; 180303b270bSEivind Eklund static int vm_swap_idle_enabled = 0; 18138efa82bSJohn Dyson #endif 18238efa82bSJohn Dyson 183d9e23210SJeff Roberson SYSCTL_INT(_vm, OID_AUTO, pageout_wakeup_thresh, 184d9e23210SJeff Roberson CTLFLAG_RW, &vm_pageout_wakeup_thresh, 0, 185d9e23210SJeff Roberson "free page threshold for waking up the pageout daemon"); 186d9e23210SJeff Roberson 1872b6b0df7SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, max_launder, 1882b6b0df7SMatthew Dillon CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout"); 18938efa82bSJohn Dyson 190d9e23210SJeff Roberson SYSCTL_INT(_vm, OID_AUTO, pageout_update_period, 191d9e23210SJeff Roberson CTLFLAG_RW, &vm_pageout_update_period, 0, 192d9e23210SJeff Roberson "Maximum active LRU update period"); 19353636869SAndrey Zonov 194c9612b2dSJeff Roberson SYSCTL_INT(_vm, OID_AUTO, lowmem_period, CTLFLAG_RW, &lowmem_period, 0, 195c9612b2dSJeff Roberson "Low memory callback period"); 196c9612b2dSJeff Roberson 19738efa82bSJohn Dyson #if defined(NO_SWAPPING) 198ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 1996bd9cb1cSTom Rhodes CTLFLAG_RD, &vm_swap_enabled, 0, "Enable entire process swapout"); 200ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 2016bd9cb1cSTom Rhodes CTLFLAG_RD, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria"); 20238efa82bSJohn Dyson #else 203ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 204b0359e2cSPeter Wemm CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout"); 205ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 206b0359e2cSPeter Wemm CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria"); 20738efa82bSJohn Dyson #endif 20826f9a767SRodney W. Grimes 209ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts, 210b0359e2cSPeter Wemm CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem"); 21112ac6a1dSJohn Dyson 212ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts, 213b0359e2cSPeter Wemm CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages"); 21412ac6a1dSJohn Dyson 21523b59018SMatthew Dillon static int pageout_lock_miss; 21623b59018SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss, 21723b59018SMatthew Dillon CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout"); 21823b59018SMatthew Dillon 219ffc82b0aSJohn Dyson #define VM_PAGEOUT_PAGE_COUNT 16 220bbc0ec52SDavid Greenman int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT; 221df8bae1dSRodney W. Grimes 222c3cb3e12SDavid Greenman int vm_page_max_wired; /* XXX max # of wired pages system-wide */ 2235dfc2870SAlan Cox SYSCTL_INT(_vm, OID_AUTO, max_wired, 2245dfc2870SAlan Cox CTLFLAG_RW, &vm_page_max_wired, 0, "System-wide limit to wired page count"); 225df8bae1dSRodney W. Grimes 22685eeca35SAlan Cox static boolean_t vm_pageout_fallback_object_lock(vm_page_t, vm_page_t *); 227449c2e92SKonstantin Belousov static boolean_t vm_pageout_launder(struct vm_pagequeue *pq, int, vm_paddr_t, 228449c2e92SKonstantin Belousov vm_paddr_t); 22938efa82bSJohn Dyson #if !defined(NO_SWAPPING) 230ecf6279fSAlan Cox static void vm_pageout_map_deactivate_pages(vm_map_t, long); 231ecf6279fSAlan Cox static void vm_pageout_object_deactivate_pages(pmap_t, vm_object_t, long); 23297824da3SAlan Cox static void vm_req_vmdaemon(int req); 23338efa82bSJohn Dyson #endif 23485eeca35SAlan Cox static boolean_t vm_pageout_page_lock(vm_page_t, vm_page_t *); 235cd41fc12SDavid Greenman 236a8229fa3SAlan Cox /* 237a8229fa3SAlan Cox * Initialize a dummy page for marking the caller's place in the specified 238a8229fa3SAlan Cox * paging queue. In principle, this function only needs to set the flag 239c7aebda8SAttilio Rao * PG_MARKER. Nonetheless, it wirte busies and initializes the hold count 240c7aebda8SAttilio Rao * to one as safety precautions. 241a8229fa3SAlan Cox */ 2428c616246SKonstantin Belousov static void 2438c616246SKonstantin Belousov vm_pageout_init_marker(vm_page_t marker, u_short queue) 2448c616246SKonstantin Belousov { 2458c616246SKonstantin Belousov 2468c616246SKonstantin Belousov bzero(marker, sizeof(*marker)); 247a8229fa3SAlan Cox marker->flags = PG_MARKER; 248c7aebda8SAttilio Rao marker->busy_lock = VPB_SINGLE_EXCLUSIVER; 2498c616246SKonstantin Belousov marker->queue = queue; 250a8229fa3SAlan Cox marker->hold_count = 1; 2518c616246SKonstantin Belousov } 2528c616246SKonstantin Belousov 25326f9a767SRodney W. Grimes /* 2548dbca793STor Egge * vm_pageout_fallback_object_lock: 2558dbca793STor Egge * 25689f6b863SAttilio Rao * Lock vm object currently associated with `m'. VM_OBJECT_TRYWLOCK is 2578dbca793STor Egge * known to have failed and page queue must be either PQ_ACTIVE or 2588dbca793STor Egge * PQ_INACTIVE. To avoid lock order violation, unlock the page queues 2598dbca793STor Egge * while locking the vm object. Use marker page to detect page queue 2608dbca793STor Egge * changes and maintain notion of next page on page queue. Return 2618dbca793STor Egge * TRUE if no changes were detected, FALSE otherwise. vm object is 2628dbca793STor Egge * locked on return. 2638dbca793STor Egge * 2648dbca793STor Egge * This function depends on both the lock portion of struct vm_object 2658dbca793STor Egge * and normal struct vm_page being type stable. 2668dbca793STor Egge */ 26785eeca35SAlan Cox static boolean_t 2688dbca793STor Egge vm_pageout_fallback_object_lock(vm_page_t m, vm_page_t *next) 2698dbca793STor Egge { 2708dbca793STor Egge struct vm_page marker; 2718d220203SAlan Cox struct vm_pagequeue *pq; 2728dbca793STor Egge boolean_t unchanged; 2738dbca793STor Egge u_short queue; 2748dbca793STor Egge vm_object_t object; 2758dbca793STor Egge 2768dbca793STor Egge queue = m->queue; 2778c616246SKonstantin Belousov vm_pageout_init_marker(&marker, queue); 278449c2e92SKonstantin Belousov pq = vm_page_pagequeue(m); 2798dbca793STor Egge object = m->object; 2808dbca793STor Egge 281c325e866SKonstantin Belousov TAILQ_INSERT_AFTER(&pq->pq_pl, m, &marker, plinks.q); 2828d220203SAlan Cox vm_pagequeue_unlock(pq); 2832965a453SKip Macy vm_page_unlock(m); 28489f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 2852965a453SKip Macy vm_page_lock(m); 2868d220203SAlan Cox vm_pagequeue_lock(pq); 2878dbca793STor Egge 2888dbca793STor Egge /* Page queue might have changed. */ 289c325e866SKonstantin Belousov *next = TAILQ_NEXT(&marker, plinks.q); 2908dbca793STor Egge unchanged = (m->queue == queue && 2918dbca793STor Egge m->object == object && 292c325e866SKonstantin Belousov &marker == TAILQ_NEXT(m, plinks.q)); 293c325e866SKonstantin Belousov TAILQ_REMOVE(&pq->pq_pl, &marker, plinks.q); 2948dbca793STor Egge return (unchanged); 2958dbca793STor Egge } 2968dbca793STor Egge 2978dbca793STor Egge /* 2988c616246SKonstantin Belousov * Lock the page while holding the page queue lock. Use marker page 2998c616246SKonstantin Belousov * to detect page queue changes and maintain notion of next page on 3008c616246SKonstantin Belousov * page queue. Return TRUE if no changes were detected, FALSE 3018c616246SKonstantin Belousov * otherwise. The page is locked on return. The page queue lock might 3028c616246SKonstantin Belousov * be dropped and reacquired. 3038c616246SKonstantin Belousov * 3048c616246SKonstantin Belousov * This function depends on normal struct vm_page being type stable. 3058c616246SKonstantin Belousov */ 30685eeca35SAlan Cox static boolean_t 3078c616246SKonstantin Belousov vm_pageout_page_lock(vm_page_t m, vm_page_t *next) 3088c616246SKonstantin Belousov { 3098c616246SKonstantin Belousov struct vm_page marker; 3108d220203SAlan Cox struct vm_pagequeue *pq; 3118c616246SKonstantin Belousov boolean_t unchanged; 3128c616246SKonstantin Belousov u_short queue; 3138c616246SKonstantin Belousov 3148c616246SKonstantin Belousov vm_page_lock_assert(m, MA_NOTOWNED); 3158c616246SKonstantin Belousov if (vm_page_trylock(m)) 3168c616246SKonstantin Belousov return (TRUE); 3178c616246SKonstantin Belousov 3188c616246SKonstantin Belousov queue = m->queue; 3198c616246SKonstantin Belousov vm_pageout_init_marker(&marker, queue); 320449c2e92SKonstantin Belousov pq = vm_page_pagequeue(m); 3218c616246SKonstantin Belousov 322c325e866SKonstantin Belousov TAILQ_INSERT_AFTER(&pq->pq_pl, m, &marker, plinks.q); 3238d220203SAlan Cox vm_pagequeue_unlock(pq); 3248c616246SKonstantin Belousov vm_page_lock(m); 3258d220203SAlan Cox vm_pagequeue_lock(pq); 3268c616246SKonstantin Belousov 3278c616246SKonstantin Belousov /* Page queue might have changed. */ 328c325e866SKonstantin Belousov *next = TAILQ_NEXT(&marker, plinks.q); 329c325e866SKonstantin Belousov unchanged = (m->queue == queue && &marker == TAILQ_NEXT(m, plinks.q)); 330c325e866SKonstantin Belousov TAILQ_REMOVE(&pq->pq_pl, &marker, plinks.q); 3318c616246SKonstantin Belousov return (unchanged); 3328c616246SKonstantin Belousov } 3338c616246SKonstantin Belousov 3348c616246SKonstantin Belousov /* 33526f9a767SRodney W. Grimes * vm_pageout_clean: 33624a1cce3SDavid Greenman * 3370d94caffSDavid Greenman * Clean the page and remove it from the laundry. 33826f9a767SRodney W. Grimes * 3390d94caffSDavid Greenman * We set the busy bit to cause potential page faults on this page to 3401c7c3c6aSMatthew Dillon * block. Note the careful timing, however, the busy bit isn't set till 3411c7c3c6aSMatthew Dillon * late and we cannot do anything that will mess with the page. 34226f9a767SRodney W. Grimes */ 3433af76890SPoul-Henning Kamp static int 3442965a453SKip Macy vm_pageout_clean(vm_page_t m) 34524a1cce3SDavid Greenman { 34654d92145SMatthew Dillon vm_object_t object; 34791b4f427SAlan Cox vm_page_t mc[2*vm_pageout_page_count], pb, ps; 3483562af12SAlan Cox int pageout_count; 34990ecac61SMatthew Dillon int ib, is, page_base; 350a316d390SJohn Dyson vm_pindex_t pindex = m->pindex; 35126f9a767SRodney W. Grimes 35295976f3fSAlan Cox vm_page_lock_assert(m, MA_OWNED); 35317f6a17bSAlan Cox object = m->object; 35489f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 3550cddd8f0SMatthew Dillon 35626f9a767SRodney W. Grimes /* 3571c7c3c6aSMatthew Dillon * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP 3581c7c3c6aSMatthew Dillon * with the new swapper, but we could have serious problems paging 3591c7c3c6aSMatthew Dillon * out other object types if there is insufficient memory. 3601c7c3c6aSMatthew Dillon * 3611c7c3c6aSMatthew Dillon * Unfortunately, checking free memory here is far too late, so the 3621c7c3c6aSMatthew Dillon * check has been moved up a procedural level. 3631c7c3c6aSMatthew Dillon */ 3641c7c3c6aSMatthew Dillon 36524a1cce3SDavid Greenman /* 3669e897b1bSAlan Cox * Can't clean the page if it's busy or held. 36724a1cce3SDavid Greenman */ 368c7aebda8SAttilio Rao vm_page_assert_unbusied(m); 36995976f3fSAlan Cox KASSERT(m->hold_count == 0, ("vm_pageout_clean: page %p is held", m)); 37017f6a17bSAlan Cox vm_page_unlock(m); 3710d94caffSDavid Greenman 37291b4f427SAlan Cox mc[vm_pageout_page_count] = pb = ps = m; 37326f9a767SRodney W. Grimes pageout_count = 1; 374f35329acSJohn Dyson page_base = vm_pageout_page_count; 37590ecac61SMatthew Dillon ib = 1; 37690ecac61SMatthew Dillon is = 1; 37790ecac61SMatthew Dillon 37824a1cce3SDavid Greenman /* 37924a1cce3SDavid Greenman * Scan object for clusterable pages. 38024a1cce3SDavid Greenman * 38124a1cce3SDavid Greenman * We can cluster ONLY if: ->> the page is NOT 38224a1cce3SDavid Greenman * clean, wired, busy, held, or mapped into a 38324a1cce3SDavid Greenman * buffer, and one of the following: 38424a1cce3SDavid Greenman * 1) The page is inactive, or a seldom used 38524a1cce3SDavid Greenman * active page. 38624a1cce3SDavid Greenman * -or- 38724a1cce3SDavid Greenman * 2) we force the issue. 38890ecac61SMatthew Dillon * 38990ecac61SMatthew Dillon * During heavy mmap/modification loads the pageout 39090ecac61SMatthew Dillon * daemon can really fragment the underlying file 39190ecac61SMatthew Dillon * due to flushing pages out of order and not trying 39290ecac61SMatthew Dillon * align the clusters (which leave sporatic out-of-order 39390ecac61SMatthew Dillon * holes). To solve this problem we do the reverse scan 39490ecac61SMatthew Dillon * first and attempt to align our cluster, then do a 39590ecac61SMatthew Dillon * forward scan if room remains. 39624a1cce3SDavid Greenman */ 39790ecac61SMatthew Dillon more: 39890ecac61SMatthew Dillon while (ib && pageout_count < vm_pageout_page_count) { 39924a1cce3SDavid Greenman vm_page_t p; 400f6b04d2bSDavid Greenman 40190ecac61SMatthew Dillon if (ib > pindex) { 40290ecac61SMatthew Dillon ib = 0; 40390ecac61SMatthew Dillon break; 404f6b04d2bSDavid Greenman } 40590ecac61SMatthew Dillon 406c7aebda8SAttilio Rao if ((p = vm_page_prev(pb)) == NULL || vm_page_busied(p)) { 40790ecac61SMatthew Dillon ib = 0; 40890ecac61SMatthew Dillon break; 409f6b04d2bSDavid Greenman } 4102965a453SKip Macy vm_page_lock(p); 41124a1cce3SDavid Greenman vm_page_test_dirty(p); 41226f4eea5SAlan Cox if (p->dirty == 0 || 41390ecac61SMatthew Dillon p->queue != PQ_INACTIVE || 41457601bcbSMatthew Dillon p->hold_count != 0) { /* may be undergoing I/O */ 4152965a453SKip Macy vm_page_unlock(p); 41690ecac61SMatthew Dillon ib = 0; 41724a1cce3SDavid Greenman break; 418f6b04d2bSDavid Greenman } 4192965a453SKip Macy vm_page_unlock(p); 42091b4f427SAlan Cox mc[--page_base] = pb = p; 42190ecac61SMatthew Dillon ++pageout_count; 42290ecac61SMatthew Dillon ++ib; 42324a1cce3SDavid Greenman /* 42490ecac61SMatthew Dillon * alignment boundry, stop here and switch directions. Do 42590ecac61SMatthew Dillon * not clear ib. 42624a1cce3SDavid Greenman */ 42790ecac61SMatthew Dillon if ((pindex - (ib - 1)) % vm_pageout_page_count == 0) 42890ecac61SMatthew Dillon break; 42924a1cce3SDavid Greenman } 43090ecac61SMatthew Dillon 43190ecac61SMatthew Dillon while (pageout_count < vm_pageout_page_count && 43290ecac61SMatthew Dillon pindex + is < object->size) { 43390ecac61SMatthew Dillon vm_page_t p; 43490ecac61SMatthew Dillon 435c7aebda8SAttilio Rao if ((p = vm_page_next(ps)) == NULL || vm_page_busied(p)) 43690ecac61SMatthew Dillon break; 4372965a453SKip Macy vm_page_lock(p); 43824a1cce3SDavid Greenman vm_page_test_dirty(p); 43926f4eea5SAlan Cox if (p->dirty == 0 || 44090ecac61SMatthew Dillon p->queue != PQ_INACTIVE || 44157601bcbSMatthew Dillon p->hold_count != 0) { /* may be undergoing I/O */ 4422965a453SKip Macy vm_page_unlock(p); 44324a1cce3SDavid Greenman break; 44424a1cce3SDavid Greenman } 4452965a453SKip Macy vm_page_unlock(p); 44691b4f427SAlan Cox mc[page_base + pageout_count] = ps = p; 44790ecac61SMatthew Dillon ++pageout_count; 44890ecac61SMatthew Dillon ++is; 44924a1cce3SDavid Greenman } 45090ecac61SMatthew Dillon 45190ecac61SMatthew Dillon /* 45290ecac61SMatthew Dillon * If we exhausted our forward scan, continue with the reverse scan 45390ecac61SMatthew Dillon * when possible, even past a page boundry. This catches boundry 45490ecac61SMatthew Dillon * conditions. 45590ecac61SMatthew Dillon */ 45690ecac61SMatthew Dillon if (ib && pageout_count < vm_pageout_page_count) 45790ecac61SMatthew Dillon goto more; 458f6b04d2bSDavid Greenman 45967bf6868SJohn Dyson /* 46067bf6868SJohn Dyson * we allow reads during pageouts... 46167bf6868SJohn Dyson */ 462126d6082SKonstantin Belousov return (vm_pageout_flush(&mc[page_base], pageout_count, 0, 0, NULL, 463126d6082SKonstantin Belousov NULL)); 464aef922f5SJohn Dyson } 465aef922f5SJohn Dyson 4661c7c3c6aSMatthew Dillon /* 4671c7c3c6aSMatthew Dillon * vm_pageout_flush() - launder the given pages 4681c7c3c6aSMatthew Dillon * 4691c7c3c6aSMatthew Dillon * The given pages are laundered. Note that we setup for the start of 4701c7c3c6aSMatthew Dillon * I/O ( i.e. busy the page ), mark it read-only, and bump the object 4711c7c3c6aSMatthew Dillon * reference count all in here rather then in the parent. If we want 4721c7c3c6aSMatthew Dillon * the parent to do more sophisticated things we may have to change 4731c7c3c6aSMatthew Dillon * the ordering. 4741e8a675cSKonstantin Belousov * 4751e8a675cSKonstantin Belousov * Returned runlen is the count of pages between mreq and first 4761e8a675cSKonstantin Belousov * page after mreq with status VM_PAGER_AGAIN. 477126d6082SKonstantin Belousov * *eio is set to TRUE if pager returned VM_PAGER_ERROR or VM_PAGER_FAIL 478126d6082SKonstantin Belousov * for any page in runlen set. 4791c7c3c6aSMatthew Dillon */ 480aef922f5SJohn Dyson int 481126d6082SKonstantin Belousov vm_pageout_flush(vm_page_t *mc, int count, int flags, int mreq, int *prunlen, 482126d6082SKonstantin Belousov boolean_t *eio) 483aef922f5SJohn Dyson { 4842e3b314dSAlan Cox vm_object_t object = mc[0]->object; 485aef922f5SJohn Dyson int pageout_status[count]; 48695461b45SJohn Dyson int numpagedout = 0; 4871e8a675cSKonstantin Belousov int i, runlen; 488aef922f5SJohn Dyson 48989f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 4907bec141bSKip Macy 4911c7c3c6aSMatthew Dillon /* 4921c7c3c6aSMatthew Dillon * Initiate I/O. Bump the vm_page_t->busy counter and 4931c7c3c6aSMatthew Dillon * mark the pages read-only. 4941c7c3c6aSMatthew Dillon * 4951c7c3c6aSMatthew Dillon * We do not have to fixup the clean/dirty bits here... we can 4961c7c3c6aSMatthew Dillon * allow the pager to do it after the I/O completes. 49702fa91d3SMatthew Dillon * 49802fa91d3SMatthew Dillon * NOTE! mc[i]->dirty may be partial or fragmented due to an 49902fa91d3SMatthew Dillon * edge case with file fragments. 5001c7c3c6aSMatthew Dillon */ 5018f9110f6SJohn Dyson for (i = 0; i < count; i++) { 5027a935082SAlan Cox KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL, 5037a935082SAlan Cox ("vm_pageout_flush: partially invalid page %p index %d/%d", 5047a935082SAlan Cox mc[i], i, count)); 505c7aebda8SAttilio Rao vm_page_sbusy(mc[i]); 50678985e42SAlan Cox pmap_remove_write(mc[i]); 5072965a453SKip Macy } 508d474eaaaSDoug Rabson vm_object_pip_add(object, count); 509aef922f5SJohn Dyson 510d076fbeaSAlan Cox vm_pager_put_pages(object, mc, count, flags, pageout_status); 51126f9a767SRodney W. Grimes 5121e8a675cSKonstantin Belousov runlen = count - mreq; 513126d6082SKonstantin Belousov if (eio != NULL) 514126d6082SKonstantin Belousov *eio = FALSE; 515aef922f5SJohn Dyson for (i = 0; i < count; i++) { 516aef922f5SJohn Dyson vm_page_t mt = mc[i]; 51724a1cce3SDavid Greenman 5184cd45723SAlan Cox KASSERT(pageout_status[i] == VM_PAGER_PEND || 5196031c68dSAlan Cox !pmap_page_is_write_mapped(mt), 5209ea8d1a6SAlan Cox ("vm_pageout_flush: page %p is not write protected", mt)); 52126f9a767SRodney W. Grimes switch (pageout_status[i]) { 52226f9a767SRodney W. Grimes case VM_PAGER_OK: 52326f9a767SRodney W. Grimes case VM_PAGER_PEND: 52495461b45SJohn Dyson numpagedout++; 52526f9a767SRodney W. Grimes break; 52626f9a767SRodney W. Grimes case VM_PAGER_BAD: 52726f9a767SRodney W. Grimes /* 5280d94caffSDavid Greenman * Page outside of range of object. Right now we 5290d94caffSDavid Greenman * essentially lose the changes by pretending it 5300d94caffSDavid Greenman * worked. 53126f9a767SRodney W. Grimes */ 53290ecac61SMatthew Dillon vm_page_undirty(mt); 53326f9a767SRodney W. Grimes break; 53426f9a767SRodney W. Grimes case VM_PAGER_ERROR: 53526f9a767SRodney W. Grimes case VM_PAGER_FAIL: 53626f9a767SRodney W. Grimes /* 5370d94caffSDavid Greenman * If page couldn't be paged out, then reactivate the 5380d94caffSDavid Greenman * page so it doesn't clog the inactive list. (We 5390d94caffSDavid Greenman * will try paging out it again later). 54026f9a767SRodney W. Grimes */ 5413c4a2440SAlan Cox vm_page_lock(mt); 54224a1cce3SDavid Greenman vm_page_activate(mt); 5433c4a2440SAlan Cox vm_page_unlock(mt); 544126d6082SKonstantin Belousov if (eio != NULL && i >= mreq && i - mreq < runlen) 545126d6082SKonstantin Belousov *eio = TRUE; 54626f9a767SRodney W. Grimes break; 54726f9a767SRodney W. Grimes case VM_PAGER_AGAIN: 5481e8a675cSKonstantin Belousov if (i >= mreq && i - mreq < runlen) 5491e8a675cSKonstantin Belousov runlen = i - mreq; 55026f9a767SRodney W. Grimes break; 55126f9a767SRodney W. Grimes } 55226f9a767SRodney W. Grimes 55326f9a767SRodney W. Grimes /* 5540d94caffSDavid Greenman * If the operation is still going, leave the page busy to 5550d94caffSDavid Greenman * block all other accesses. Also, leave the paging in 5560d94caffSDavid Greenman * progress indicator set so that we don't attempt an object 5570d94caffSDavid Greenman * collapse. 55826f9a767SRodney W. Grimes */ 55926f9a767SRodney W. Grimes if (pageout_status[i] != VM_PAGER_PEND) { 560f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 561c7aebda8SAttilio Rao vm_page_sunbusy(mt); 5623c4a2440SAlan Cox if (vm_page_count_severe()) { 5633c4a2440SAlan Cox vm_page_lock(mt); 5649ea8d1a6SAlan Cox vm_page_try_to_cache(mt); 5652965a453SKip Macy vm_page_unlock(mt); 56626f9a767SRodney W. Grimes } 5673c4a2440SAlan Cox } 5683c4a2440SAlan Cox } 5691e8a675cSKonstantin Belousov if (prunlen != NULL) 5701e8a675cSKonstantin Belousov *prunlen = runlen; 5713c4a2440SAlan Cox return (numpagedout); 57226f9a767SRodney W. Grimes } 57326f9a767SRodney W. Grimes 57485eeca35SAlan Cox static boolean_t 575449c2e92SKonstantin Belousov vm_pageout_launder(struct vm_pagequeue *pq, int tries, vm_paddr_t low, 576449c2e92SKonstantin Belousov vm_paddr_t high) 57785eeca35SAlan Cox { 57885eeca35SAlan Cox struct mount *mp; 57985eeca35SAlan Cox struct vnode *vp; 58085eeca35SAlan Cox vm_object_t object; 58185eeca35SAlan Cox vm_paddr_t pa; 58285eeca35SAlan Cox vm_page_t m, m_tmp, next; 5831bd7d0b7SKonstantin Belousov int lockmode; 58485eeca35SAlan Cox 5858d220203SAlan Cox vm_pagequeue_lock(pq); 586c325e866SKonstantin Belousov TAILQ_FOREACH_SAFE(m, &pq->pq_pl, plinks.q, next) { 58785eeca35SAlan Cox if ((m->flags & PG_MARKER) != 0) 58885eeca35SAlan Cox continue; 58985eeca35SAlan Cox pa = VM_PAGE_TO_PHYS(m); 59085eeca35SAlan Cox if (pa < low || pa + PAGE_SIZE > high) 59185eeca35SAlan Cox continue; 59285eeca35SAlan Cox if (!vm_pageout_page_lock(m, &next) || m->hold_count != 0) { 59385eeca35SAlan Cox vm_page_unlock(m); 59485eeca35SAlan Cox continue; 59585eeca35SAlan Cox } 59685eeca35SAlan Cox object = m->object; 59789f6b863SAttilio Rao if ((!VM_OBJECT_TRYWLOCK(object) && 59885eeca35SAlan Cox (!vm_pageout_fallback_object_lock(m, &next) || 599c7aebda8SAttilio Rao m->hold_count != 0)) || vm_page_busied(m)) { 60085eeca35SAlan Cox vm_page_unlock(m); 60189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 60285eeca35SAlan Cox continue; 60385eeca35SAlan Cox } 60485eeca35SAlan Cox vm_page_test_dirty(m); 6059fc4739dSAlan Cox if (m->dirty == 0 && object->ref_count != 0) 60685eeca35SAlan Cox pmap_remove_all(m); 60785eeca35SAlan Cox if (m->dirty != 0) { 60885eeca35SAlan Cox vm_page_unlock(m); 60985eeca35SAlan Cox if (tries == 0 || (object->flags & OBJ_DEAD) != 0) { 61089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 61185eeca35SAlan Cox continue; 61285eeca35SAlan Cox } 61385eeca35SAlan Cox if (object->type == OBJT_VNODE) { 6148d220203SAlan Cox vm_pagequeue_unlock(pq); 61585eeca35SAlan Cox vp = object->handle; 61685eeca35SAlan Cox vm_object_reference_locked(object); 61789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 61885eeca35SAlan Cox (void)vn_start_write(vp, &mp, V_WAIT); 6191bd7d0b7SKonstantin Belousov lockmode = MNT_SHARED_WRITES(vp->v_mount) ? 6201bd7d0b7SKonstantin Belousov LK_SHARED : LK_EXCLUSIVE; 6211bd7d0b7SKonstantin Belousov vn_lock(vp, lockmode | LK_RETRY); 62289f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 62385eeca35SAlan Cox vm_object_page_clean(object, 0, 0, OBJPC_SYNC); 62489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 62585eeca35SAlan Cox VOP_UNLOCK(vp, 0); 62685eeca35SAlan Cox vm_object_deallocate(object); 62785eeca35SAlan Cox vn_finished_write(mp); 62885eeca35SAlan Cox return (TRUE); 62985eeca35SAlan Cox } else if (object->type == OBJT_SWAP || 63085eeca35SAlan Cox object->type == OBJT_DEFAULT) { 6318d220203SAlan Cox vm_pagequeue_unlock(pq); 63285eeca35SAlan Cox m_tmp = m; 63385eeca35SAlan Cox vm_pageout_flush(&m_tmp, 1, VM_PAGER_PUT_SYNC, 63485eeca35SAlan Cox 0, NULL, NULL); 63589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 63685eeca35SAlan Cox return (TRUE); 63785eeca35SAlan Cox } 63885eeca35SAlan Cox } else { 6398d220203SAlan Cox /* 6408d220203SAlan Cox * Dequeue here to prevent lock recursion in 6418d220203SAlan Cox * vm_page_cache(). 6428d220203SAlan Cox */ 6438d220203SAlan Cox vm_page_dequeue_locked(m); 64485eeca35SAlan Cox vm_page_cache(m); 64585eeca35SAlan Cox vm_page_unlock(m); 64685eeca35SAlan Cox } 64789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 64885eeca35SAlan Cox } 6498d220203SAlan Cox vm_pagequeue_unlock(pq); 65085eeca35SAlan Cox return (FALSE); 65185eeca35SAlan Cox } 65285eeca35SAlan Cox 65385eeca35SAlan Cox /* 65485eeca35SAlan Cox * Increase the number of cached pages. The specified value, "tries", 65585eeca35SAlan Cox * determines which categories of pages are cached: 65685eeca35SAlan Cox * 65785eeca35SAlan Cox * 0: All clean, inactive pages within the specified physical address range 65885eeca35SAlan Cox * are cached. Will not sleep. 65985eeca35SAlan Cox * 1: The vm_lowmem handlers are called. All inactive pages within 66085eeca35SAlan Cox * the specified physical address range are cached. May sleep. 66185eeca35SAlan Cox * 2: The vm_lowmem handlers are called. All inactive and active pages 66285eeca35SAlan Cox * within the specified physical address range are cached. May sleep. 66385eeca35SAlan Cox */ 66485eeca35SAlan Cox void 66585eeca35SAlan Cox vm_pageout_grow_cache(int tries, vm_paddr_t low, vm_paddr_t high) 66685eeca35SAlan Cox { 667449c2e92SKonstantin Belousov int actl, actmax, inactl, inactmax, dom, initial_dom; 668449c2e92SKonstantin Belousov static int start_dom = 0; 66985eeca35SAlan Cox 67085eeca35SAlan Cox if (tries > 0) { 67185eeca35SAlan Cox /* 67285eeca35SAlan Cox * Decrease registered cache sizes. The vm_lowmem handlers 67385eeca35SAlan Cox * may acquire locks and/or sleep, so they can only be invoked 67485eeca35SAlan Cox * when "tries" is greater than zero. 67585eeca35SAlan Cox */ 676*14a0d74eSSteven Hartland SDT_PROBE0(vm, , , vm__lowmem_cache); 67785eeca35SAlan Cox EVENTHANDLER_INVOKE(vm_lowmem, 0); 67885eeca35SAlan Cox 67985eeca35SAlan Cox /* 68085eeca35SAlan Cox * We do this explicitly after the caches have been drained 68185eeca35SAlan Cox * above. 68285eeca35SAlan Cox */ 68385eeca35SAlan Cox uma_reclaim(); 68485eeca35SAlan Cox } 685449c2e92SKonstantin Belousov 686449c2e92SKonstantin Belousov /* 687449c2e92SKonstantin Belousov * Make the next scan start on the next domain. 688449c2e92SKonstantin Belousov */ 689449c2e92SKonstantin Belousov initial_dom = atomic_fetchadd_int(&start_dom, 1) % vm_ndomains; 690449c2e92SKonstantin Belousov 69185eeca35SAlan Cox inactl = 0; 69244f1c916SBryan Drewery inactmax = vm_cnt.v_inactive_count; 69385eeca35SAlan Cox actl = 0; 69444f1c916SBryan Drewery actmax = tries < 2 ? 0 : vm_cnt.v_active_count; 695449c2e92SKonstantin Belousov dom = initial_dom; 696449c2e92SKonstantin Belousov 697449c2e92SKonstantin Belousov /* 698449c2e92SKonstantin Belousov * Scan domains in round-robin order, first inactive queues, 699449c2e92SKonstantin Belousov * then active. Since domain usually owns large physically 700449c2e92SKonstantin Belousov * contiguous chunk of memory, it makes sense to completely 701449c2e92SKonstantin Belousov * exhaust one domain before switching to next, while growing 702449c2e92SKonstantin Belousov * the pool of contiguous physical pages. 703449c2e92SKonstantin Belousov * 704449c2e92SKonstantin Belousov * Do not even start launder a domain which cannot contain 705449c2e92SKonstantin Belousov * the specified address range, as indicated by segments 706449c2e92SKonstantin Belousov * constituting the domain. 707449c2e92SKonstantin Belousov */ 70885eeca35SAlan Cox again: 709449c2e92SKonstantin Belousov if (inactl < inactmax) { 710449c2e92SKonstantin Belousov if (vm_phys_domain_intersects(vm_dom[dom].vmd_segs, 711449c2e92SKonstantin Belousov low, high) && 712449c2e92SKonstantin Belousov vm_pageout_launder(&vm_dom[dom].vmd_pagequeues[PQ_INACTIVE], 713449c2e92SKonstantin Belousov tries, low, high)) { 71485eeca35SAlan Cox inactl++; 71585eeca35SAlan Cox goto again; 71685eeca35SAlan Cox } 717449c2e92SKonstantin Belousov if (++dom == vm_ndomains) 718449c2e92SKonstantin Belousov dom = 0; 719449c2e92SKonstantin Belousov if (dom != initial_dom) 720449c2e92SKonstantin Belousov goto again; 721449c2e92SKonstantin Belousov } 722449c2e92SKonstantin Belousov if (actl < actmax) { 723449c2e92SKonstantin Belousov if (vm_phys_domain_intersects(vm_dom[dom].vmd_segs, 724449c2e92SKonstantin Belousov low, high) && 725449c2e92SKonstantin Belousov vm_pageout_launder(&vm_dom[dom].vmd_pagequeues[PQ_ACTIVE], 726449c2e92SKonstantin Belousov tries, low, high)) { 72785eeca35SAlan Cox actl++; 72885eeca35SAlan Cox goto again; 72985eeca35SAlan Cox } 730449c2e92SKonstantin Belousov if (++dom == vm_ndomains) 731449c2e92SKonstantin Belousov dom = 0; 732449c2e92SKonstantin Belousov if (dom != initial_dom) 733449c2e92SKonstantin Belousov goto again; 734449c2e92SKonstantin Belousov } 73585eeca35SAlan Cox } 73685eeca35SAlan Cox 73738efa82bSJohn Dyson #if !defined(NO_SWAPPING) 73826f9a767SRodney W. Grimes /* 73926f9a767SRodney W. Grimes * vm_pageout_object_deactivate_pages 74026f9a767SRodney W. Grimes * 741ce186587SAlan Cox * Deactivate enough pages to satisfy the inactive target 742ce186587SAlan Cox * requirements. 74326f9a767SRodney W. Grimes * 74426f9a767SRodney W. Grimes * The object and map must be locked. 74526f9a767SRodney W. Grimes */ 74638efa82bSJohn Dyson static void 747ce186587SAlan Cox vm_pageout_object_deactivate_pages(pmap_t pmap, vm_object_t first_object, 748ce186587SAlan Cox long desired) 74926f9a767SRodney W. Grimes { 750ecf6279fSAlan Cox vm_object_t backing_object, object; 751ce186587SAlan Cox vm_page_t p; 752bb7858eaSJeff Roberson int act_delta, remove_mode; 75326f9a767SRodney W. Grimes 754e23b0a19SAlan Cox VM_OBJECT_ASSERT_LOCKED(first_object); 75528634820SAlan Cox if ((first_object->flags & OBJ_FICTITIOUS) != 0) 75638efa82bSJohn Dyson return; 757ecf6279fSAlan Cox for (object = first_object;; object = backing_object) { 758ecf6279fSAlan Cox if (pmap_resident_count(pmap) <= desired) 759ecf6279fSAlan Cox goto unlock_return; 760e23b0a19SAlan Cox VM_OBJECT_ASSERT_LOCKED(object); 76128634820SAlan Cox if ((object->flags & OBJ_UNMANAGED) != 0 || 76228634820SAlan Cox object->paging_in_progress != 0) 763ecf6279fSAlan Cox goto unlock_return; 76426f9a767SRodney W. Grimes 76585b1dc89SAlan Cox remove_mode = 0; 76638efa82bSJohn Dyson if (object->shadow_count > 1) 76738efa82bSJohn Dyson remove_mode = 1; 76826f9a767SRodney W. Grimes /* 769ce186587SAlan Cox * Scan the object's entire memory queue. 77026f9a767SRodney W. Grimes */ 771ce186587SAlan Cox TAILQ_FOREACH(p, &object->memq, listq) { 772447fe2a4SAlan Cox if (pmap_resident_count(pmap) <= desired) 773447fe2a4SAlan Cox goto unlock_return; 774c7aebda8SAttilio Rao if (vm_page_busied(p)) 775447fe2a4SAlan Cox continue; 776ce186587SAlan Cox PCPU_INC(cnt.v_pdpages); 7772965a453SKip Macy vm_page_lock(p); 778ce186587SAlan Cox if (p->wire_count != 0 || p->hold_count != 0 || 779ecf6279fSAlan Cox !pmap_page_exists_quick(pmap, p)) { 7802965a453SKip Macy vm_page_unlock(p); 7810d94caffSDavid Greenman continue; 7820d94caffSDavid Greenman } 783bb7858eaSJeff Roberson act_delta = pmap_ts_referenced(p); 7843407fefeSKonstantin Belousov if ((p->aflags & PGA_REFERENCED) != 0) { 785bb7858eaSJeff Roberson if (act_delta == 0) 786bb7858eaSJeff Roberson act_delta = 1; 7873407fefeSKonstantin Belousov vm_page_aflag_clear(p, PGA_REFERENCED); 788ef743ce6SJohn Dyson } 789bb7858eaSJeff Roberson if (p->queue != PQ_ACTIVE && act_delta != 0) { 790ef743ce6SJohn Dyson vm_page_activate(p); 791bb7858eaSJeff Roberson p->act_count += act_delta; 792c8c4b40cSJohn Dyson } else if (p->queue == PQ_ACTIVE) { 793bb7858eaSJeff Roberson if (act_delta == 0) { 794ce186587SAlan Cox p->act_count -= min(p->act_count, 795ce186587SAlan Cox ACT_DECLINE); 79690776bd7SJeff Roberson if (!remove_mode && p->act_count == 0) { 7974fec79beSAlan Cox pmap_remove_all(p); 79826f9a767SRodney W. Grimes vm_page_deactivate(p); 7998d220203SAlan Cox } else 8008d220203SAlan Cox vm_page_requeue(p); 801c8c4b40cSJohn Dyson } else { 802eaf13dd7SJohn Dyson vm_page_activate(p); 803ce186587SAlan Cox if (p->act_count < ACT_MAX - 804ce186587SAlan Cox ACT_ADVANCE) 80538efa82bSJohn Dyson p->act_count += ACT_ADVANCE; 8068d220203SAlan Cox vm_page_requeue(p); 807ce186587SAlan Cox } 808ce186587SAlan Cox } else if (p->queue == PQ_INACTIVE) 809ce186587SAlan Cox pmap_remove_all(p); 8102965a453SKip Macy vm_page_unlock(p); 81126f9a767SRodney W. Grimes } 812ecf6279fSAlan Cox if ((backing_object = object->backing_object) == NULL) 813ecf6279fSAlan Cox goto unlock_return; 814e23b0a19SAlan Cox VM_OBJECT_RLOCK(backing_object); 815ecf6279fSAlan Cox if (object != first_object) 816e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(object); 81738efa82bSJohn Dyson } 818ecf6279fSAlan Cox unlock_return: 819ecf6279fSAlan Cox if (object != first_object) 820e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(object); 82126f9a767SRodney W. Grimes } 82226f9a767SRodney W. Grimes 82326f9a767SRodney W. Grimes /* 82426f9a767SRodney W. Grimes * deactivate some number of pages in a map, try to do it fairly, but 82526f9a767SRodney W. Grimes * that is really hard to do. 82626f9a767SRodney W. Grimes */ 827cd41fc12SDavid Greenman static void 82838efa82bSJohn Dyson vm_pageout_map_deactivate_pages(map, desired) 82926f9a767SRodney W. Grimes vm_map_t map; 830ecf6279fSAlan Cox long desired; 83126f9a767SRodney W. Grimes { 83226f9a767SRodney W. Grimes vm_map_entry_t tmpe; 83338efa82bSJohn Dyson vm_object_t obj, bigobj; 83430105b9eSTor Egge int nothingwired; 8350d94caffSDavid Greenman 836d974f03cSAlan Cox if (!vm_map_trylock(map)) 83726f9a767SRodney W. Grimes return; 83838efa82bSJohn Dyson 83938efa82bSJohn Dyson bigobj = NULL; 84030105b9eSTor Egge nothingwired = TRUE; 84138efa82bSJohn Dyson 84238efa82bSJohn Dyson /* 84338efa82bSJohn Dyson * first, search out the biggest object, and try to free pages from 84438efa82bSJohn Dyson * that. 84538efa82bSJohn Dyson */ 84626f9a767SRodney W. Grimes tmpe = map->header.next; 84738efa82bSJohn Dyson while (tmpe != &map->header) { 8489fdfe602SMatthew Dillon if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 84938efa82bSJohn Dyson obj = tmpe->object.vm_object; 850e23b0a19SAlan Cox if (obj != NULL && VM_OBJECT_TRYRLOCK(obj)) { 8510774dfb3SAlan Cox if (obj->shadow_count <= 1 && 8520774dfb3SAlan Cox (bigobj == NULL || 8530774dfb3SAlan Cox bigobj->resident_page_count < obj->resident_page_count)) { 8540774dfb3SAlan Cox if (bigobj != NULL) 855e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(bigobj); 85638efa82bSJohn Dyson bigobj = obj; 8570774dfb3SAlan Cox } else 858e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(obj); 85938efa82bSJohn Dyson } 86038efa82bSJohn Dyson } 86130105b9eSTor Egge if (tmpe->wired_count > 0) 86230105b9eSTor Egge nothingwired = FALSE; 86338efa82bSJohn Dyson tmpe = tmpe->next; 86438efa82bSJohn Dyson } 86538efa82bSJohn Dyson 8660774dfb3SAlan Cox if (bigobj != NULL) { 867ecf6279fSAlan Cox vm_pageout_object_deactivate_pages(map->pmap, bigobj, desired); 868e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(bigobj); 8690774dfb3SAlan Cox } 87038efa82bSJohn Dyson /* 87138efa82bSJohn Dyson * Next, hunt around for other pages to deactivate. We actually 87238efa82bSJohn Dyson * do this search sort of wrong -- .text first is not the best idea. 87338efa82bSJohn Dyson */ 87438efa82bSJohn Dyson tmpe = map->header.next; 87538efa82bSJohn Dyson while (tmpe != &map->header) { 876b1028ad1SLuoqi Chen if (pmap_resident_count(vm_map_pmap(map)) <= desired) 87738efa82bSJohn Dyson break; 8789fdfe602SMatthew Dillon if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 87938efa82bSJohn Dyson obj = tmpe->object.vm_object; 8800774dfb3SAlan Cox if (obj != NULL) { 881e23b0a19SAlan Cox VM_OBJECT_RLOCK(obj); 882ecf6279fSAlan Cox vm_pageout_object_deactivate_pages(map->pmap, obj, desired); 883e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(obj); 8840774dfb3SAlan Cox } 88538efa82bSJohn Dyson } 88626f9a767SRodney W. Grimes tmpe = tmpe->next; 88738857e7fSAlan Cox } 88838efa82bSJohn Dyson 88938efa82bSJohn Dyson /* 89038efa82bSJohn Dyson * Remove all mappings if a process is swapped out, this will free page 89138efa82bSJohn Dyson * table pages. 89238efa82bSJohn Dyson */ 89338857e7fSAlan Cox if (desired == 0 && nothingwired) { 8948d01a3b2SNathan Whitehorn pmap_remove(vm_map_pmap(map), vm_map_min(map), 8958d01a3b2SNathan Whitehorn vm_map_max(map)); 89638857e7fSAlan Cox } 897938b0f5bSMarcel Moolenaar 89838efa82bSJohn Dyson vm_map_unlock(map); 89926f9a767SRodney W. Grimes } 900a1287949SEivind Eklund #endif /* !defined(NO_SWAPPING) */ 901df8bae1dSRodney W. Grimes 9021c7c3c6aSMatthew Dillon /* 903df8bae1dSRodney W. Grimes * vm_pageout_scan does the dirty work for the pageout daemon. 904d9e23210SJeff Roberson * 905d9e23210SJeff Roberson * pass 0 - Update active LRU/deactivate pages 906d9e23210SJeff Roberson * pass 1 - Move inactive to cache or free 907d9e23210SJeff Roberson * pass 2 - Launder dirty pages 908df8bae1dSRodney W. Grimes */ 9092b6b0df7SMatthew Dillon static void 910449c2e92SKonstantin Belousov vm_pageout_scan(struct vm_domain *vmd, int pass) 911df8bae1dSRodney W. Grimes { 912502ba6e4SJohn Dyson vm_page_t m, next; 9138d220203SAlan Cox struct vm_pagequeue *pq; 914df8bae1dSRodney W. Grimes vm_object_t object; 9159099545aSAlan Cox int act_delta, addl_page_shortage, deficit, maxscan, page_shortage; 916f6b04d2bSDavid Greenman int vnodes_skipped = 0; 9172b6b0df7SMatthew Dillon int maxlaunder; 9181bd7d0b7SKonstantin Belousov int lockmode; 91948cc2fc7SKonstantin Belousov boolean_t queues_locked; 9200d94caffSDavid Greenman 921df8bae1dSRodney W. Grimes /* 922d9e23210SJeff Roberson * If we need to reclaim memory ask kernel caches to return 923c9612b2dSJeff Roberson * some. We rate limit to avoid thrashing. 924d9e23210SJeff Roberson */ 925c9612b2dSJeff Roberson if (vmd == &vm_dom[0] && pass > 0 && 926f721133eSSteven Hartland (ticks - lowmem_ticks) / hz >= lowmem_period) { 927d9e23210SJeff Roberson /* 928855a310fSJeff Roberson * Decrease registered cache sizes. 929855a310fSJeff Roberson */ 930*14a0d74eSSteven Hartland SDT_PROBE0(vm, , , vm__lowmem_scan); 931855a310fSJeff Roberson EVENTHANDLER_INVOKE(vm_lowmem, 0); 932855a310fSJeff Roberson /* 933d9e23210SJeff Roberson * We do this explicitly after the caches have been 934d9e23210SJeff Roberson * drained above. 935855a310fSJeff Roberson */ 936855a310fSJeff Roberson uma_reclaim(); 937c9612b2dSJeff Roberson lowmem_ticks = ticks; 938d9e23210SJeff Roberson } 9395985940eSJohn Dyson 940311e34e2SKonstantin Belousov /* 94196240c89SEitan Adler * The addl_page_shortage is the number of temporarily 942311e34e2SKonstantin Belousov * stuck pages in the inactive queue. In other words, the 943449c2e92SKonstantin Belousov * number of pages from the inactive count that should be 944311e34e2SKonstantin Belousov * discounted in setting the target for the active queue scan. 945311e34e2SKonstantin Belousov */ 9469099545aSAlan Cox addl_page_shortage = 0; 9479099545aSAlan Cox 9481c7c3c6aSMatthew Dillon /* 9491c7c3c6aSMatthew Dillon * Calculate the number of pages we want to either free or move 9502b6b0df7SMatthew Dillon * to the cache. 9511c7c3c6aSMatthew Dillon */ 95260196cdaSAlan Cox if (pass > 0) { 95360196cdaSAlan Cox deficit = atomic_readandclear_int(&vm_pageout_deficit); 9549099545aSAlan Cox page_shortage = vm_paging_target() + deficit; 95560196cdaSAlan Cox } else 95660196cdaSAlan Cox page_shortage = deficit = 0; 9571c7c3c6aSMatthew Dillon 958936524aaSMatthew Dillon /* 9592b6b0df7SMatthew Dillon * maxlaunder limits the number of dirty pages we flush per scan. 9602b6b0df7SMatthew Dillon * For most systems a smaller value (16 or 32) is more robust under 9612b6b0df7SMatthew Dillon * extreme memory and disk pressure because any unnecessary writes 9622b6b0df7SMatthew Dillon * to disk can result in extreme performance degredation. However, 9632b6b0df7SMatthew Dillon * systems with excessive dirty pages (especially when MAP_NOSYNC is 9642b6b0df7SMatthew Dillon * used) will die horribly with limited laundering. If the pageout 9652b6b0df7SMatthew Dillon * daemon cannot clean enough pages in the first pass, we let it go 9662b6b0df7SMatthew Dillon * all out in succeeding passes. 9671c7c3c6aSMatthew Dillon */ 9682b6b0df7SMatthew Dillon if ((maxlaunder = vm_max_launder) <= 1) 9692b6b0df7SMatthew Dillon maxlaunder = 1; 970d9e23210SJeff Roberson if (pass > 1) 9712b6b0df7SMatthew Dillon maxlaunder = 10000; 9728d220203SAlan Cox 9738d220203SAlan Cox /* 9748d220203SAlan Cox * Start scanning the inactive queue for pages we can move to the 9758d220203SAlan Cox * cache or free. The scan will stop when the target is reached or 9768d220203SAlan Cox * we have scanned the entire inactive queue. Note that m->act_count 9778d220203SAlan Cox * is not used to form decisions for the inactive queue, only for the 9788d220203SAlan Cox * active queue. 9798d220203SAlan Cox */ 980449c2e92SKonstantin Belousov pq = &vmd->vmd_pagequeues[PQ_INACTIVE]; 981449c2e92SKonstantin Belousov maxscan = pq->pq_cnt; 9828d220203SAlan Cox vm_pagequeue_lock(pq); 9838d220203SAlan Cox queues_locked = TRUE; 9848d220203SAlan Cox for (m = TAILQ_FIRST(&pq->pq_pl); 9851c7c3c6aSMatthew Dillon m != NULL && maxscan-- > 0 && page_shortage > 0; 986e929c00dSKirk McKusick m = next) { 9878d220203SAlan Cox vm_pagequeue_assert_locked(pq); 98848cc2fc7SKonstantin Belousov KASSERT(queues_locked, ("unlocked queues")); 989d4961bcbSKonstantin Belousov KASSERT(m->queue == PQ_INACTIVE, ("Inactive queue %p", m)); 990df8bae1dSRodney W. Grimes 9918d220203SAlan Cox PCPU_INC(cnt.v_pdpages); 992c325e866SKonstantin Belousov next = TAILQ_NEXT(m, plinks.q); 993df8bae1dSRodney W. Grimes 994936524aaSMatthew Dillon /* 995936524aaSMatthew Dillon * skip marker pages 996936524aaSMatthew Dillon */ 997936524aaSMatthew Dillon if (m->flags & PG_MARKER) 998936524aaSMatthew Dillon continue; 999936524aaSMatthew Dillon 10007900f95dSKonstantin Belousov KASSERT((m->flags & PG_FICTITIOUS) == 0, 10017900f95dSKonstantin Belousov ("Fictitious page %p cannot be in inactive queue", m)); 10027900f95dSKonstantin Belousov KASSERT((m->oflags & VPO_UNMANAGED) == 0, 10037900f95dSKonstantin Belousov ("Unmanaged page %p cannot be in inactive queue", m)); 10047900f95dSKonstantin Belousov 10058c616246SKonstantin Belousov /* 1006311e34e2SKonstantin Belousov * The page or object lock acquisitions fail if the 1007311e34e2SKonstantin Belousov * page was removed from the queue or moved to a 1008311e34e2SKonstantin Belousov * different position within the queue. In either 1009311e34e2SKonstantin Belousov * case, addl_page_shortage should not be incremented. 10108c616246SKonstantin Belousov */ 10118c616246SKonstantin Belousov if (!vm_pageout_page_lock(m, &next)) { 10128c616246SKonstantin Belousov vm_page_unlock(m); 1013b182ec9eSJohn Dyson continue; 1014df8bae1dSRodney W. Grimes } 10159ee2165fSAlan Cox object = m->object; 101689f6b863SAttilio Rao if (!VM_OBJECT_TRYWLOCK(object) && 1017311e34e2SKonstantin Belousov !vm_pageout_fallback_object_lock(m, &next)) { 10182965a453SKip Macy vm_page_unlock(m); 101989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 102034d9e6fdSAlan Cox continue; 102134d9e6fdSAlan Cox } 1022311e34e2SKonstantin Belousov 1023311e34e2SKonstantin Belousov /* 1024311e34e2SKonstantin Belousov * Don't mess with busy pages, keep them at at the 1025311e34e2SKonstantin Belousov * front of the queue, most likely they are being 1026311e34e2SKonstantin Belousov * paged out. Increment addl_page_shortage for busy 1027311e34e2SKonstantin Belousov * pages, because they may leave the inactive queue 1028311e34e2SKonstantin Belousov * shortly after page scan is finished. 1029311e34e2SKonstantin Belousov */ 1030c7aebda8SAttilio Rao if (vm_page_busied(m)) { 10312965a453SKip Macy vm_page_unlock(m); 103289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1033b182ec9eSJohn Dyson addl_page_shortage++; 103426f9a767SRodney W. Grimes continue; 103526f9a767SRodney W. Grimes } 1036bd7e5f99SJohn Dyson 10377e006499SJohn Dyson /* 10388d220203SAlan Cox * We unlock the inactive page queue, invalidating the 103948cc2fc7SKonstantin Belousov * 'next' pointer. Use our marker to remember our 104048cc2fc7SKonstantin Belousov * place. 104148cc2fc7SKonstantin Belousov */ 1042c325e866SKonstantin Belousov TAILQ_INSERT_AFTER(&pq->pq_pl, m, &vmd->vmd_marker, plinks.q); 10438d220203SAlan Cox vm_pagequeue_unlock(pq); 104448cc2fc7SKonstantin Belousov queues_locked = FALSE; 104548cc2fc7SKonstantin Belousov 104648cc2fc7SKonstantin Belousov /* 1047bb7858eaSJeff Roberson * We bump the activation count if the page has been 1048bb7858eaSJeff Roberson * referenced while in the inactive queue. This makes 1049bb7858eaSJeff Roberson * it less likely that the page will be added back to the 1050bb7858eaSJeff Roberson * inactive queue prematurely again. Here we check the 10511c7c3c6aSMatthew Dillon * page tables (or emulated bits, if any), given the upper 10521c7c3c6aSMatthew Dillon * level VM system not knowing anything about existing 10531c7c3c6aSMatthew Dillon * references. 10547e006499SJohn Dyson */ 1055bb7858eaSJeff Roberson if ((m->aflags & PGA_REFERENCED) != 0) { 1056bb7858eaSJeff Roberson vm_page_aflag_clear(m, PGA_REFERENCED); 1057bb7858eaSJeff Roberson act_delta = 1; 105886fa2471SAlan Cox } else 105986fa2471SAlan Cox act_delta = 0; 1060bb7858eaSJeff Roberson if (object->ref_count != 0) { 1061bb7858eaSJeff Roberson act_delta += pmap_ts_referenced(m); 1062bb7858eaSJeff Roberson } else { 1063bb7858eaSJeff Roberson KASSERT(!pmap_page_is_mapped(m), 1064bb7858eaSJeff Roberson ("vm_pageout_scan: page %p is mapped", m)); 10652fe6e4d7SDavid Greenman } 1066ef743ce6SJohn Dyson 10677e006499SJohn Dyson /* 10681c7c3c6aSMatthew Dillon * If the upper level VM system knows about any page 1069bb7858eaSJeff Roberson * references, we reactivate the page or requeue it. 10707e006499SJohn Dyson */ 1071bb7858eaSJeff Roberson if (act_delta != 0) { 107286fa2471SAlan Cox if (object->ref_count != 0) { 107326f9a767SRodney W. Grimes vm_page_activate(m); 1074bb7858eaSJeff Roberson m->act_count += act_delta + ACT_ADVANCE; 1075bb7858eaSJeff Roberson } else { 1076bb7858eaSJeff Roberson vm_pagequeue_lock(pq); 1077bb7858eaSJeff Roberson queues_locked = TRUE; 1078bb7858eaSJeff Roberson vm_page_requeue_locked(m); 1079bb7858eaSJeff Roberson } 108089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1081a15f7df5SAttilio Rao vm_page_unlock(m); 108248cc2fc7SKonstantin Belousov goto relock_queues; 10830d94caffSDavid Greenman } 108467bf6868SJohn Dyson 1085311e34e2SKonstantin Belousov if (m->hold_count != 0) { 1086311e34e2SKonstantin Belousov vm_page_unlock(m); 108789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1088311e34e2SKonstantin Belousov 1089311e34e2SKonstantin Belousov /* 1090311e34e2SKonstantin Belousov * Held pages are essentially stuck in the 1091311e34e2SKonstantin Belousov * queue. So, they ought to be discounted 1092449c2e92SKonstantin Belousov * from the inactive count. See the 1093311e34e2SKonstantin Belousov * calculation of the page_shortage for the 1094311e34e2SKonstantin Belousov * loop over the active queue below. 1095311e34e2SKonstantin Belousov */ 1096311e34e2SKonstantin Belousov addl_page_shortage++; 1097311e34e2SKonstantin Belousov goto relock_queues; 1098311e34e2SKonstantin Belousov } 1099311e34e2SKonstantin Belousov 11007e006499SJohn Dyson /* 11019fc4739dSAlan Cox * If the page appears to be clean at the machine-independent 11029fc4739dSAlan Cox * layer, then remove all of its mappings from the pmap in 11039fc4739dSAlan Cox * anticipation of placing it onto the cache queue. If, 11049fc4739dSAlan Cox * however, any of the page's mappings allow write access, 11059fc4739dSAlan Cox * then the page may still be modified until the last of those 11069fc4739dSAlan Cox * mappings are removed. 11077e006499SJohn Dyson */ 11089fc4739dSAlan Cox vm_page_test_dirty(m); 11099fc4739dSAlan Cox if (m->dirty == 0 && object->ref_count != 0) 1110b78ddb0bSAlan Cox pmap_remove_all(m); 1111dcbcd518SBruce Evans 11126989c456SAlan Cox if (m->valid == 0) { 11137e006499SJohn Dyson /* 11147e006499SJohn Dyson * Invalid pages can be easily freed 11157e006499SJohn Dyson */ 11166989c456SAlan Cox vm_page_free(m); 111748cc2fc7SKonstantin Belousov PCPU_INC(cnt.v_dfree); 11181c7c3c6aSMatthew Dillon --page_shortage; 1119bd7e5f99SJohn Dyson } else if (m->dirty == 0) { 11206989c456SAlan Cox /* 11216989c456SAlan Cox * Clean pages can be placed onto the cache queue. 11226989c456SAlan Cox * This effectively frees them. 11236989c456SAlan Cox */ 1124bd7e5f99SJohn Dyson vm_page_cache(m); 11251c7c3c6aSMatthew Dillon --page_shortage; 1126d9e23210SJeff Roberson } else if ((m->flags & PG_WINATCFLS) == 0 && pass < 2) { 11277e006499SJohn Dyson /* 11282b6b0df7SMatthew Dillon * Dirty pages need to be paged out, but flushing 1129ab46f63eSJohn Baldwin * a page is extremely expensive versus freeing 11302b6b0df7SMatthew Dillon * a clean page. Rather then artificially limiting 11312b6b0df7SMatthew Dillon * the number of pages we can flush, we instead give 11322b6b0df7SMatthew Dillon * dirty pages extra priority on the inactive queue 11332b6b0df7SMatthew Dillon * by forcing them to be cycled through the queue 11342b6b0df7SMatthew Dillon * twice before being flushed, after which the 11352b6b0df7SMatthew Dillon * (now clean) page will cycle through once more 11362b6b0df7SMatthew Dillon * before being freed. This significantly extends 11372b6b0df7SMatthew Dillon * the thrash point for a heavily loaded machine. 11387e006499SJohn Dyson */ 11393407fefeSKonstantin Belousov m->flags |= PG_WINATCFLS; 11408d220203SAlan Cox vm_pagequeue_lock(pq); 114148cc2fc7SKonstantin Belousov queues_locked = TRUE; 11428d220203SAlan Cox vm_page_requeue_locked(m); 11430d94caffSDavid Greenman } else if (maxlaunder > 0) { 11442b6b0df7SMatthew Dillon /* 11452b6b0df7SMatthew Dillon * We always want to try to flush some dirty pages if 11462b6b0df7SMatthew Dillon * we encounter them, to keep the system stable. 11472b6b0df7SMatthew Dillon * Normally this number is small, but under extreme 11482b6b0df7SMatthew Dillon * pressure where there are insufficient clean pages 11492b6b0df7SMatthew Dillon * on the inactive queue, we may have to go all out. 11502b6b0df7SMatthew Dillon */ 11515050aa86SKonstantin Belousov int swap_pageouts_ok; 1152f6b04d2bSDavid Greenman struct vnode *vp = NULL; 115314137dc0SAlan Cox struct mount *mp = NULL; 11540d94caffSDavid Greenman 115512ac6a1dSJohn Dyson if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) { 115612ac6a1dSJohn Dyson swap_pageouts_ok = 1; 115712ac6a1dSJohn Dyson } else { 115812ac6a1dSJohn Dyson swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts); 115912ac6a1dSJohn Dyson swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts && 116090ecac61SMatthew Dillon vm_page_count_min()); 116112ac6a1dSJohn Dyson 116212ac6a1dSJohn Dyson } 116370111b90SJohn Dyson 116470111b90SJohn Dyson /* 11651c7c3c6aSMatthew Dillon * We don't bother paging objects that are "dead". 11661c7c3c6aSMatthew Dillon * Those objects are in a "rundown" state. 116770111b90SJohn Dyson */ 116870111b90SJohn Dyson if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) { 11698d220203SAlan Cox vm_pagequeue_lock(pq); 11702965a453SKip Macy vm_page_unlock(m); 117189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 117248cc2fc7SKonstantin Belousov queues_locked = TRUE; 11738d220203SAlan Cox vm_page_requeue_locked(m); 117448cc2fc7SKonstantin Belousov goto relock_queues; 117512ac6a1dSJohn Dyson } 117612ac6a1dSJohn Dyson 11771c7c3c6aSMatthew Dillon /* 11782b6b0df7SMatthew Dillon * The object is already known NOT to be dead. It 11792b6b0df7SMatthew Dillon * is possible for the vget() to block the whole 11802b6b0df7SMatthew Dillon * pageout daemon, but the new low-memory handling 11812b6b0df7SMatthew Dillon * code should prevent it. 11821c7c3c6aSMatthew Dillon * 11832b6b0df7SMatthew Dillon * The previous code skipped locked vnodes and, worse, 11842b6b0df7SMatthew Dillon * reordered pages in the queue. This results in 11852b6b0df7SMatthew Dillon * completely non-deterministic operation and, on a 11862b6b0df7SMatthew Dillon * busy system, can lead to extremely non-optimal 11872b6b0df7SMatthew Dillon * pageouts. For example, it can cause clean pages 11882b6b0df7SMatthew Dillon * to be freed and dirty pages to be moved to the end 11892b6b0df7SMatthew Dillon * of the queue. Since dirty pages are also moved to 11902b6b0df7SMatthew Dillon * the end of the queue once-cleaned, this gives 1191ab46f63eSJohn Baldwin * way too large a weighting to deferring the freeing 11922b6b0df7SMatthew Dillon * of dirty pages. 11931c7c3c6aSMatthew Dillon * 119423b59018SMatthew Dillon * We can't wait forever for the vnode lock, we might 119523b59018SMatthew Dillon * deadlock due to a vn_read() getting stuck in 119623b59018SMatthew Dillon * vm_wait while holding this vnode. We skip the 119723b59018SMatthew Dillon * vnode if we can't get it in a reasonable amount 119823b59018SMatthew Dillon * of time. 11991c7c3c6aSMatthew Dillon */ 12001c7c3c6aSMatthew Dillon if (object->type == OBJT_VNODE) { 12012965a453SKip Macy vm_page_unlock(m); 120224a1cce3SDavid Greenman vp = object->handle; 1203db27dcc0STor Egge if (vp->v_type == VREG && 1204db27dcc0STor Egge vn_start_write(vp, &mp, V_NOWAIT) != 0) { 12056129343dSKonstantin Belousov mp = NULL; 1206db27dcc0STor Egge ++pageout_lock_miss; 1207db27dcc0STor Egge if (object->flags & OBJ_MIGHTBEDIRTY) 1208db27dcc0STor Egge vnodes_skipped++; 1209625e6c0aSTor Egge goto unlock_and_continue; 1210db27dcc0STor Egge } 1211b9f180d1SKonstantin Belousov KASSERT(mp != NULL, 1212b9f180d1SKonstantin Belousov ("vp %p with NULL v_mount", vp)); 121314137dc0SAlan Cox vm_object_reference_locked(object); 121489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 12151bd7d0b7SKonstantin Belousov lockmode = MNT_SHARED_WRITES(vp->v_mount) ? 12161bd7d0b7SKonstantin Belousov LK_SHARED : LK_EXCLUSIVE; 12171bd7d0b7SKonstantin Belousov if (vget(vp, lockmode | LK_TIMELOCK, 121897824da3SAlan Cox curthread)) { 121989f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 122023b59018SMatthew Dillon ++pageout_lock_miss; 1221aef922f5SJohn Dyson if (object->flags & OBJ_MIGHTBEDIRTY) 1222925a3a41SJohn Dyson vnodes_skipped++; 1223625e6c0aSTor Egge vp = NULL; 1224625e6c0aSTor Egge goto unlock_and_continue; 122585a376ebSJohn Dyson } 122689f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 12272965a453SKip Macy vm_page_lock(m); 12288d220203SAlan Cox vm_pagequeue_lock(pq); 122948cc2fc7SKonstantin Belousov queues_locked = TRUE; 1230f35329acSJohn Dyson /* 1231936524aaSMatthew Dillon * The page might have been moved to another 1232936524aaSMatthew Dillon * queue during potential blocking in vget() 1233936524aaSMatthew Dillon * above. The page might have been freed and 123414137dc0SAlan Cox * reused for another vnode. 1235f35329acSJohn Dyson */ 12369cf51988SAlan Cox if (m->queue != PQ_INACTIVE || 1237936524aaSMatthew Dillon m->object != object || 1238c325e866SKonstantin Belousov TAILQ_NEXT(m, plinks.q) != &vmd->vmd_marker) { 12392965a453SKip Macy vm_page_unlock(m); 1240b182ec9eSJohn Dyson if (object->flags & OBJ_MIGHTBEDIRTY) 1241925a3a41SJohn Dyson vnodes_skipped++; 12423562af12SAlan Cox goto unlock_and_continue; 1243b182ec9eSJohn Dyson } 1244b182ec9eSJohn Dyson 1245f35329acSJohn Dyson /* 1246936524aaSMatthew Dillon * The page may have been busied during the 124714137dc0SAlan Cox * blocking in vget(). We don't move the 1248936524aaSMatthew Dillon * page back onto the end of the queue so that 1249936524aaSMatthew Dillon * statistics are more correct if we don't. 1250f35329acSJohn Dyson */ 1251c7aebda8SAttilio Rao if (vm_page_busied(m)) { 12522965a453SKip Macy vm_page_unlock(m); 12539099545aSAlan Cox addl_page_shortage++; 12543562af12SAlan Cox goto unlock_and_continue; 1255b182ec9eSJohn Dyson } 1256b182ec9eSJohn Dyson 1257f35329acSJohn Dyson /* 125857601bcbSMatthew Dillon * If the page has become held it might 125957601bcbSMatthew Dillon * be undergoing I/O, so skip it 1260f35329acSJohn Dyson */ 12619099545aSAlan Cox if (m->hold_count != 0) { 12622965a453SKip Macy vm_page_unlock(m); 12639099545aSAlan Cox addl_page_shortage++; 1264b182ec9eSJohn Dyson if (object->flags & OBJ_MIGHTBEDIRTY) 1265925a3a41SJohn Dyson vnodes_skipped++; 12663562af12SAlan Cox goto unlock_and_continue; 1267f6b04d2bSDavid Greenman } 12688d220203SAlan Cox vm_pagequeue_unlock(pq); 1269a4156419SKonstantin Belousov queues_locked = FALSE; 1270f6b04d2bSDavid Greenman } 1271f6b04d2bSDavid Greenman 12720d94caffSDavid Greenman /* 12730d94caffSDavid Greenman * If a page is dirty, then it is either being washed 12740d94caffSDavid Greenman * (but not yet cleaned) or it is still in the 12750d94caffSDavid Greenman * laundry. If it is still in the laundry, then we 12762b6b0df7SMatthew Dillon * start the cleaning operation. 1277936524aaSMatthew Dillon * 12782b6b0df7SMatthew Dillon * decrement page_shortage on success to account for 12792b6b0df7SMatthew Dillon * the (future) cleaned page. Otherwise we could wind 12802b6b0df7SMatthew Dillon * up laundering or cleaning too many pages. 12810d94caffSDavid Greenman */ 12822b6b0df7SMatthew Dillon if (vm_pageout_clean(m) != 0) { 12832b6b0df7SMatthew Dillon --page_shortage; 1284936524aaSMatthew Dillon --maxlaunder; 12852b6b0df7SMatthew Dillon } 12863562af12SAlan Cox unlock_and_continue: 12872965a453SKip Macy vm_page_lock_assert(m, MA_NOTOWNED); 128889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 128914137dc0SAlan Cox if (mp != NULL) { 129048cc2fc7SKonstantin Belousov if (queues_locked) { 12918d220203SAlan Cox vm_pagequeue_unlock(pq); 129248cc2fc7SKonstantin Belousov queues_locked = FALSE; 129348cc2fc7SKonstantin Belousov } 129414137dc0SAlan Cox if (vp != NULL) 1295f6b04d2bSDavid Greenman vput(vp); 129614137dc0SAlan Cox vm_object_deallocate(object); 1297f2a2857bSKirk McKusick vn_finished_write(mp); 129848cc2fc7SKonstantin Belousov } 129948cc2fc7SKonstantin Belousov vm_page_lock_assert(m, MA_NOTOWNED); 130048cc2fc7SKonstantin Belousov goto relock_queues; 130148cc2fc7SKonstantin Belousov } 130248cc2fc7SKonstantin Belousov vm_page_unlock(m); 130389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 130448cc2fc7SKonstantin Belousov relock_queues: 130548cc2fc7SKonstantin Belousov if (!queues_locked) { 13068d220203SAlan Cox vm_pagequeue_lock(pq); 130748cc2fc7SKonstantin Belousov queues_locked = TRUE; 13086989c456SAlan Cox } 1309c325e866SKonstantin Belousov next = TAILQ_NEXT(&vmd->vmd_marker, plinks.q); 1310c325e866SKonstantin Belousov TAILQ_REMOVE(&pq->pq_pl, &vmd->vmd_marker, plinks.q); 13110d94caffSDavid Greenman } 13128d220203SAlan Cox vm_pagequeue_unlock(pq); 131326f9a767SRodney W. Grimes 13149452b5edSAlan Cox #if !defined(NO_SWAPPING) 13159452b5edSAlan Cox /* 13169452b5edSAlan Cox * Wakeup the swapout daemon if we didn't cache or free the targeted 13179452b5edSAlan Cox * number of pages. 13189452b5edSAlan Cox */ 13199452b5edSAlan Cox if (vm_swap_enabled && page_shortage > 0) 13209452b5edSAlan Cox vm_req_vmdaemon(VM_SWAP_NORMAL); 13219452b5edSAlan Cox #endif 13229452b5edSAlan Cox 13239452b5edSAlan Cox /* 13249452b5edSAlan Cox * Wakeup the sync daemon if we skipped a vnode in a writeable object 13259452b5edSAlan Cox * and we didn't cache or free enough pages. 13269452b5edSAlan Cox */ 13279452b5edSAlan Cox if (vnodes_skipped > 0 && page_shortage > vm_cnt.v_free_target - 13289452b5edSAlan Cox vm_cnt.v_free_min) 13299452b5edSAlan Cox (void)speedup_syncer(); 13309452b5edSAlan Cox 1331df8bae1dSRodney W. Grimes /* 1332936524aaSMatthew Dillon * Compute the number of pages we want to try to move from the 1333936524aaSMatthew Dillon * active queue to the inactive queue. 13341c7c3c6aSMatthew Dillon */ 133544f1c916SBryan Drewery page_shortage = vm_cnt.v_inactive_target - vm_cnt.v_inactive_count + 13369099545aSAlan Cox vm_paging_target() + deficit + addl_page_shortage; 13379099545aSAlan Cox 1338114f62c6SJeff Roberson pq = &vmd->vmd_pagequeues[PQ_ACTIVE]; 1339114f62c6SJeff Roberson vm_pagequeue_lock(pq); 13409099545aSAlan Cox maxscan = pq->pq_cnt; 13419099545aSAlan Cox 1342d9e23210SJeff Roberson /* 1343d9e23210SJeff Roberson * If we're just idle polling attempt to visit every 1344d9e23210SJeff Roberson * active page within 'update_period' seconds. 1345d9e23210SJeff Roberson */ 1346d9e23210SJeff Roberson if (pass == 0 && vm_pageout_update_period != 0) { 13479099545aSAlan Cox maxscan /= vm_pageout_update_period; 13489099545aSAlan Cox page_shortage = maxscan; 1349d9e23210SJeff Roberson } 13501c7c3c6aSMatthew Dillon 13511c7c3c6aSMatthew Dillon /* 1352936524aaSMatthew Dillon * Scan the active queue for things we can deactivate. We nominally 1353936524aaSMatthew Dillon * track the per-page activity counter and use it to locate 1354936524aaSMatthew Dillon * deactivation candidates. 13551c7c3c6aSMatthew Dillon */ 13568d220203SAlan Cox m = TAILQ_FIRST(&pq->pq_pl); 13579099545aSAlan Cox while (m != NULL && maxscan-- > 0 && page_shortage > 0) { 1358f35329acSJohn Dyson 13599cf51988SAlan Cox KASSERT(m->queue == PQ_ACTIVE, 1360d3c09dd7SAlan Cox ("vm_pageout_scan: page %p isn't active", m)); 1361f35329acSJohn Dyson 1362c325e866SKonstantin Belousov next = TAILQ_NEXT(m, plinks.q); 13638dbca793STor Egge if ((m->flags & PG_MARKER) != 0) { 13648dbca793STor Egge m = next; 13658dbca793STor Egge continue; 13668dbca793STor Egge } 13677900f95dSKonstantin Belousov KASSERT((m->flags & PG_FICTITIOUS) == 0, 13687900f95dSKonstantin Belousov ("Fictitious page %p cannot be in active queue", m)); 13697900f95dSKonstantin Belousov KASSERT((m->oflags & VPO_UNMANAGED) == 0, 13707900f95dSKonstantin Belousov ("Unmanaged page %p cannot be in active queue", m)); 13719ee2165fSAlan Cox if (!vm_pageout_page_lock(m, &next)) { 13728c616246SKonstantin Belousov vm_page_unlock(m); 13732965a453SKip Macy m = next; 13742965a453SKip Macy continue; 13752965a453SKip Macy } 1376b18bfc3dSJohn Dyson 1377b18bfc3dSJohn Dyson /* 1378b18bfc3dSJohn Dyson * The count for pagedaemon pages is done after checking the 1379956f3135SPhilippe Charnier * page for eligibility... 1380b18bfc3dSJohn Dyson */ 13818d220203SAlan Cox PCPU_INC(cnt.v_pdpages); 1382ef743ce6SJohn Dyson 13837e006499SJohn Dyson /* 13847e006499SJohn Dyson * Check to see "how much" the page has been used. 13857e006499SJohn Dyson */ 138686fa2471SAlan Cox if ((m->aflags & PGA_REFERENCED) != 0) { 1387bb7858eaSJeff Roberson vm_page_aflag_clear(m, PGA_REFERENCED); 138886fa2471SAlan Cox act_delta = 1; 138986fa2471SAlan Cox } else 139086fa2471SAlan Cox act_delta = 0; 139186fa2471SAlan Cox 1392274132acSJeff Roberson /* 1393274132acSJeff Roberson * Unlocked object ref count check. Two races are possible. 1394274132acSJeff Roberson * 1) The ref was transitioning to zero and we saw non-zero, 1395274132acSJeff Roberson * the pmap bits will be checked unnecessarily. 1396274132acSJeff Roberson * 2) The ref was transitioning to one and we saw zero. 1397274132acSJeff Roberson * The page lock prevents a new reference to this page so 1398274132acSJeff Roberson * we need not check the reference bits. 1399274132acSJeff Roberson */ 1400274132acSJeff Roberson if (m->object->ref_count != 0) 1401bb7858eaSJeff Roberson act_delta += pmap_ts_referenced(m); 1402bb7858eaSJeff Roberson 1403bb7858eaSJeff Roberson /* 1404bb7858eaSJeff Roberson * Advance or decay the act_count based on recent usage. 1405bb7858eaSJeff Roberson */ 140686fa2471SAlan Cox if (act_delta != 0) { 1407bb7858eaSJeff Roberson m->act_count += ACT_ADVANCE + act_delta; 140838efa82bSJohn Dyson if (m->act_count > ACT_MAX) 140938efa82bSJohn Dyson m->act_count = ACT_MAX; 141086fa2471SAlan Cox } else 141138efa82bSJohn Dyson m->act_count -= min(m->act_count, ACT_DECLINE); 1412bb7858eaSJeff Roberson 1413bb7858eaSJeff Roberson /* 1414bb7858eaSJeff Roberson * Move this page to the tail of the active or inactive 1415bb7858eaSJeff Roberson * queue depending on usage. 1416bb7858eaSJeff Roberson */ 141786fa2471SAlan Cox if (m->act_count == 0) { 14188d220203SAlan Cox /* Dequeue to avoid later lock recursion. */ 14198d220203SAlan Cox vm_page_dequeue_locked(m); 1420d4a272dbSJohn Dyson vm_page_deactivate(m); 1421bb7858eaSJeff Roberson page_shortage--; 14228d220203SAlan Cox } else 14238d220203SAlan Cox vm_page_requeue_locked(m); 14242965a453SKip Macy vm_page_unlock(m); 142526f9a767SRodney W. Grimes m = next; 142626f9a767SRodney W. Grimes } 14278d220203SAlan Cox vm_pagequeue_unlock(pq); 1428ceb0cf87SJohn Dyson #if !defined(NO_SWAPPING) 1429ceb0cf87SJohn Dyson /* 1430ceb0cf87SJohn Dyson * Idle process swapout -- run once per second. 1431ceb0cf87SJohn Dyson */ 1432ceb0cf87SJohn Dyson if (vm_swap_idle_enabled) { 1433ceb0cf87SJohn Dyson static long lsec; 1434227ee8a1SPoul-Henning Kamp if (time_second != lsec) { 143597824da3SAlan Cox vm_req_vmdaemon(VM_SWAP_IDLE); 1436227ee8a1SPoul-Henning Kamp lsec = time_second; 1437ceb0cf87SJohn Dyson } 1438ceb0cf87SJohn Dyson } 1439ceb0cf87SJohn Dyson #endif 1440ceb0cf87SJohn Dyson 14415663e6deSDavid Greenman /* 1442e92686d0SDavid Schultz * If we are critically low on one of RAM or swap and low on 1443e92686d0SDavid Schultz * the other, kill the largest process. However, we avoid 1444e92686d0SDavid Schultz * doing this on the first pass in order to give ourselves a 1445e92686d0SDavid Schultz * chance to flush out dirty vnode-backed pages and to allow 1446e92686d0SDavid Schultz * active pages to be moved to the inactive queue and reclaimed. 14472025d69bSKonstantin Belousov */ 1448449c2e92SKonstantin Belousov vm_pageout_mightbe_oom(vmd, pass); 14492025d69bSKonstantin Belousov } 14502025d69bSKonstantin Belousov 1451449c2e92SKonstantin Belousov static int vm_pageout_oom_vote; 1452449c2e92SKonstantin Belousov 1453449c2e92SKonstantin Belousov /* 1454449c2e92SKonstantin Belousov * The pagedaemon threads randlomly select one to perform the 1455449c2e92SKonstantin Belousov * OOM. Trying to kill processes before all pagedaemons 1456449c2e92SKonstantin Belousov * failed to reach free target is premature. 1457449c2e92SKonstantin Belousov */ 1458449c2e92SKonstantin Belousov static void 1459449c2e92SKonstantin Belousov vm_pageout_mightbe_oom(struct vm_domain *vmd, int pass) 1460449c2e92SKonstantin Belousov { 1461449c2e92SKonstantin Belousov int old_vote; 1462449c2e92SKonstantin Belousov 1463d9e23210SJeff Roberson if (pass <= 1 || !((swap_pager_avail < 64 && vm_page_count_min()) || 1464449c2e92SKonstantin Belousov (swap_pager_full && vm_paging_target() > 0))) { 1465449c2e92SKonstantin Belousov if (vmd->vmd_oom) { 1466449c2e92SKonstantin Belousov vmd->vmd_oom = FALSE; 1467449c2e92SKonstantin Belousov atomic_subtract_int(&vm_pageout_oom_vote, 1); 1468449c2e92SKonstantin Belousov } 1469449c2e92SKonstantin Belousov return; 1470449c2e92SKonstantin Belousov } 1471449c2e92SKonstantin Belousov 1472449c2e92SKonstantin Belousov if (vmd->vmd_oom) 1473449c2e92SKonstantin Belousov return; 1474449c2e92SKonstantin Belousov 1475449c2e92SKonstantin Belousov vmd->vmd_oom = TRUE; 1476449c2e92SKonstantin Belousov old_vote = atomic_fetchadd_int(&vm_pageout_oom_vote, 1); 1477449c2e92SKonstantin Belousov if (old_vote != vm_ndomains - 1) 1478449c2e92SKonstantin Belousov return; 1479449c2e92SKonstantin Belousov 1480449c2e92SKonstantin Belousov /* 1481449c2e92SKonstantin Belousov * The current pagedaemon thread is the last in the quorum to 1482449c2e92SKonstantin Belousov * start OOM. Initiate the selection and signaling of the 1483449c2e92SKonstantin Belousov * victim. 1484449c2e92SKonstantin Belousov */ 1485449c2e92SKonstantin Belousov vm_pageout_oom(VM_OOM_MEM); 1486449c2e92SKonstantin Belousov 1487449c2e92SKonstantin Belousov /* 1488449c2e92SKonstantin Belousov * After one round of OOM terror, recall our vote. On the 1489449c2e92SKonstantin Belousov * next pass, current pagedaemon would vote again if the low 1490449c2e92SKonstantin Belousov * memory condition is still there, due to vmd_oom being 1491449c2e92SKonstantin Belousov * false. 1492449c2e92SKonstantin Belousov */ 1493449c2e92SKonstantin Belousov vmd->vmd_oom = FALSE; 1494449c2e92SKonstantin Belousov atomic_subtract_int(&vm_pageout_oom_vote, 1); 1495449c2e92SKonstantin Belousov } 14962025d69bSKonstantin Belousov 14972025d69bSKonstantin Belousov void 14982025d69bSKonstantin Belousov vm_pageout_oom(int shortage) 14992025d69bSKonstantin Belousov { 15002025d69bSKonstantin Belousov struct proc *p, *bigproc; 15012025d69bSKonstantin Belousov vm_offset_t size, bigsize; 15022025d69bSKonstantin Belousov struct thread *td; 15036bed074cSKonstantin Belousov struct vmspace *vm; 15042025d69bSKonstantin Belousov 15052025d69bSKonstantin Belousov /* 15061c58e4e5SJohn Baldwin * We keep the process bigproc locked once we find it to keep anyone 15071c58e4e5SJohn Baldwin * from messing with it; however, there is a possibility of 15081c58e4e5SJohn Baldwin * deadlock if process B is bigproc and one of it's child processes 15091c58e4e5SJohn Baldwin * attempts to propagate a signal to B while we are waiting for A's 15101c58e4e5SJohn Baldwin * lock while walking this list. To avoid this, we don't block on 15111c58e4e5SJohn Baldwin * the process lock but just skip a process if it is already locked. 15125663e6deSDavid Greenman */ 15135663e6deSDavid Greenman bigproc = NULL; 15145663e6deSDavid Greenman bigsize = 0; 15151005a129SJohn Baldwin sx_slock(&allproc_lock); 1516e602ba25SJulian Elischer FOREACH_PROC_IN_SYSTEM(p) { 1517e602ba25SJulian Elischer int breakout; 1518dcbcd518SBruce Evans 15191c58e4e5SJohn Baldwin if (PROC_TRYLOCK(p) == 0) 15201c58e4e5SJohn Baldwin continue; 15211c58e4e5SJohn Baldwin /* 15223f1c4c4fSKonstantin Belousov * If this is a system, protected or killed process, skip it. 15235663e6deSDavid Greenman */ 15248e6fa660SJohn Baldwin if (p->p_state != PRS_NORMAL || 15258e6fa660SJohn Baldwin (p->p_flag & (P_INEXEC | P_PROTECTED | P_SYSTEM)) || 15263f1c4c4fSKonstantin Belousov (p->p_pid == 1) || P_KILLED(p) || 15278f60c087SPoul-Henning Kamp ((p->p_pid < 48) && (swap_pager_avail != 0))) { 15288606d880SJohn Baldwin PROC_UNLOCK(p); 15295663e6deSDavid Greenman continue; 15305663e6deSDavid Greenman } 15315663e6deSDavid Greenman /* 1532dcbcd518SBruce Evans * If the process is in a non-running type state, 1533e602ba25SJulian Elischer * don't touch it. Check all the threads individually. 15345663e6deSDavid Greenman */ 1535e602ba25SJulian Elischer breakout = 0; 1536e602ba25SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 1537982d11f8SJeff Roberson thread_lock(td); 153871fad9fdSJulian Elischer if (!TD_ON_RUNQ(td) && 153971fad9fdSJulian Elischer !TD_IS_RUNNING(td) && 1540f497cda2SEdward Tomasz Napierala !TD_IS_SLEEPING(td) && 1541f497cda2SEdward Tomasz Napierala !TD_IS_SUSPENDED(td)) { 1542982d11f8SJeff Roberson thread_unlock(td); 1543e602ba25SJulian Elischer breakout = 1; 1544e602ba25SJulian Elischer break; 1545e602ba25SJulian Elischer } 1546982d11f8SJeff Roberson thread_unlock(td); 1547e602ba25SJulian Elischer } 1548e602ba25SJulian Elischer if (breakout) { 15491c58e4e5SJohn Baldwin PROC_UNLOCK(p); 15505663e6deSDavid Greenman continue; 15515663e6deSDavid Greenman } 15525663e6deSDavid Greenman /* 15535663e6deSDavid Greenman * get the process size 15545663e6deSDavid Greenman */ 15556bed074cSKonstantin Belousov vm = vmspace_acquire_ref(p); 15566bed074cSKonstantin Belousov if (vm == NULL) { 15576bed074cSKonstantin Belousov PROC_UNLOCK(p); 15586bed074cSKonstantin Belousov continue; 15596bed074cSKonstantin Belousov } 15606bed074cSKonstantin Belousov if (!vm_map_trylock_read(&vm->vm_map)) { 15616bed074cSKonstantin Belousov vmspace_free(vm); 156272d97679SDavid Schultz PROC_UNLOCK(p); 156372d97679SDavid Schultz continue; 156472d97679SDavid Schultz } 15657981aa24SKonstantin Belousov size = vmspace_swap_count(vm); 15666bed074cSKonstantin Belousov vm_map_unlock_read(&vm->vm_map); 15672025d69bSKonstantin Belousov if (shortage == VM_OOM_MEM) 15686bed074cSKonstantin Belousov size += vmspace_resident_count(vm); 15696bed074cSKonstantin Belousov vmspace_free(vm); 15705663e6deSDavid Greenman /* 15715663e6deSDavid Greenman * if the this process is bigger than the biggest one 15725663e6deSDavid Greenman * remember it. 15735663e6deSDavid Greenman */ 15745663e6deSDavid Greenman if (size > bigsize) { 15751c58e4e5SJohn Baldwin if (bigproc != NULL) 15761c58e4e5SJohn Baldwin PROC_UNLOCK(bigproc); 15775663e6deSDavid Greenman bigproc = p; 15785663e6deSDavid Greenman bigsize = size; 15791c58e4e5SJohn Baldwin } else 15801c58e4e5SJohn Baldwin PROC_UNLOCK(p); 15815663e6deSDavid Greenman } 15821005a129SJohn Baldwin sx_sunlock(&allproc_lock); 15835663e6deSDavid Greenman if (bigproc != NULL) { 1584729b1e51SDavid Greenman killproc(bigproc, "out of swap space"); 1585fa885116SJulian Elischer sched_nice(bigproc, PRIO_MIN); 15861c58e4e5SJohn Baldwin PROC_UNLOCK(bigproc); 158744f1c916SBryan Drewery wakeup(&vm_cnt.v_free_count); 15885663e6deSDavid Greenman } 15895663e6deSDavid Greenman } 159026f9a767SRodney W. Grimes 1591449c2e92SKonstantin Belousov static void 1592449c2e92SKonstantin Belousov vm_pageout_worker(void *arg) 1593449c2e92SKonstantin Belousov { 1594449c2e92SKonstantin Belousov struct vm_domain *domain; 1595949c9186SKonstantin Belousov int domidx; 1596449c2e92SKonstantin Belousov 1597449c2e92SKonstantin Belousov domidx = (uintptr_t)arg; 1598449c2e92SKonstantin Belousov domain = &vm_dom[domidx]; 1599449c2e92SKonstantin Belousov 1600449c2e92SKonstantin Belousov /* 1601949c9186SKonstantin Belousov * XXXKIB It could be useful to bind pageout daemon threads to 1602949c9186SKonstantin Belousov * the cores belonging to the domain, from which vm_page_array 1603949c9186SKonstantin Belousov * is allocated. 1604449c2e92SKonstantin Belousov */ 1605449c2e92SKonstantin Belousov 1606449c2e92SKonstantin Belousov KASSERT(domain->vmd_segs != 0, ("domain without segments")); 1607449c2e92SKonstantin Belousov vm_pageout_init_marker(&domain->vmd_marker, PQ_INACTIVE); 1608449c2e92SKonstantin Belousov 1609449c2e92SKonstantin Belousov /* 1610449c2e92SKonstantin Belousov * The pageout daemon worker is never done, so loop forever. 1611449c2e92SKonstantin Belousov */ 1612449c2e92SKonstantin Belousov while (TRUE) { 1613449c2e92SKonstantin Belousov /* 1614449c2e92SKonstantin Belousov * If we have enough free memory, wakeup waiters. Do 1615449c2e92SKonstantin Belousov * not clear vm_pages_needed until we reach our target, 1616449c2e92SKonstantin Belousov * otherwise we may be woken up over and over again and 1617449c2e92SKonstantin Belousov * waste a lot of cpu. 1618449c2e92SKonstantin Belousov */ 1619449c2e92SKonstantin Belousov mtx_lock(&vm_page_queue_free_mtx); 1620449c2e92SKonstantin Belousov if (vm_pages_needed && !vm_page_count_min()) { 1621449c2e92SKonstantin Belousov if (!vm_paging_needed()) 1622449c2e92SKonstantin Belousov vm_pages_needed = 0; 162344f1c916SBryan Drewery wakeup(&vm_cnt.v_free_count); 1624449c2e92SKonstantin Belousov } 1625449c2e92SKonstantin Belousov if (vm_pages_needed) { 1626449c2e92SKonstantin Belousov /* 1627449c2e92SKonstantin Belousov * Still not done, take a second pass without waiting 1628449c2e92SKonstantin Belousov * (unlimited dirty cleaning), otherwise sleep a bit 1629449c2e92SKonstantin Belousov * and try again. 1630449c2e92SKonstantin Belousov */ 1631449c2e92SKonstantin Belousov if (domain->vmd_pass > 1) 1632449c2e92SKonstantin Belousov msleep(&vm_pages_needed, 1633449c2e92SKonstantin Belousov &vm_page_queue_free_mtx, PVM, "psleep", 1634449c2e92SKonstantin Belousov hz / 2); 1635449c2e92SKonstantin Belousov } else { 1636449c2e92SKonstantin Belousov /* 1637d9e23210SJeff Roberson * Good enough, sleep until required to refresh 1638d9e23210SJeff Roberson * stats. 1639449c2e92SKonstantin Belousov */ 1640449c2e92SKonstantin Belousov domain->vmd_pass = 0; 1641d9e23210SJeff Roberson msleep(&vm_pages_needed, &vm_page_queue_free_mtx, 1642d9e23210SJeff Roberson PVM, "psleep", hz); 1643d9e23210SJeff Roberson 1644449c2e92SKonstantin Belousov } 1645d9e23210SJeff Roberson if (vm_pages_needed) { 164644f1c916SBryan Drewery vm_cnt.v_pdwakeups++; 1647d9e23210SJeff Roberson domain->vmd_pass++; 1648d9e23210SJeff Roberson } 1649449c2e92SKonstantin Belousov mtx_unlock(&vm_page_queue_free_mtx); 1650449c2e92SKonstantin Belousov vm_pageout_scan(domain, domain->vmd_pass); 1651449c2e92SKonstantin Belousov } 1652449c2e92SKonstantin Belousov } 1653449c2e92SKonstantin Belousov 1654df8bae1dSRodney W. Grimes /* 16554d19f4adSSteven Hartland * vm_pageout_init initialises basic pageout daemon settings. 1656df8bae1dSRodney W. Grimes */ 16572b14f991SJulian Elischer static void 16584d19f4adSSteven Hartland vm_pageout_init(void) 1659df8bae1dSRodney W. Grimes { 1660df8bae1dSRodney W. Grimes /* 1661df8bae1dSRodney W. Grimes * Initialize some paging parameters. 1662df8bae1dSRodney W. Grimes */ 166344f1c916SBryan Drewery vm_cnt.v_interrupt_free_min = 2; 166444f1c916SBryan Drewery if (vm_cnt.v_page_count < 2000) 1665f35329acSJohn Dyson vm_pageout_page_count = 8; 1666f6b04d2bSDavid Greenman 166745ae1d91SAlan Cox /* 166845ae1d91SAlan Cox * v_free_reserved needs to include enough for the largest 166945ae1d91SAlan Cox * swap pager structures plus enough for any pv_entry structs 167045ae1d91SAlan Cox * when paging. 167145ae1d91SAlan Cox */ 167244f1c916SBryan Drewery if (vm_cnt.v_page_count > 1024) 167344f1c916SBryan Drewery vm_cnt.v_free_min = 4 + (vm_cnt.v_page_count - 1024) / 200; 16742feb50bfSAttilio Rao else 167544f1c916SBryan Drewery vm_cnt.v_free_min = 4; 167644f1c916SBryan Drewery vm_cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE + 167744f1c916SBryan Drewery vm_cnt.v_interrupt_free_min; 167844f1c916SBryan Drewery vm_cnt.v_free_reserved = vm_pageout_page_count + 167944f1c916SBryan Drewery vm_cnt.v_pageout_free_min + (vm_cnt.v_page_count / 768); 168044f1c916SBryan Drewery vm_cnt.v_free_severe = vm_cnt.v_free_min / 2; 168144f1c916SBryan Drewery vm_cnt.v_free_target = 4 * vm_cnt.v_free_min + vm_cnt.v_free_reserved; 168244f1c916SBryan Drewery vm_cnt.v_free_min += vm_cnt.v_free_reserved; 168344f1c916SBryan Drewery vm_cnt.v_free_severe += vm_cnt.v_free_reserved; 168444f1c916SBryan Drewery vm_cnt.v_inactive_target = (3 * vm_cnt.v_free_target) / 2; 168544f1c916SBryan Drewery if (vm_cnt.v_inactive_target > vm_cnt.v_free_count / 3) 168644f1c916SBryan Drewery vm_cnt.v_inactive_target = vm_cnt.v_free_count / 3; 1687df8bae1dSRodney W. Grimes 1688d9e23210SJeff Roberson /* 1689d9e23210SJeff Roberson * Set the default wakeup threshold to be 10% above the minimum 1690d9e23210SJeff Roberson * page limit. This keeps the steady state out of shortfall. 1691d9e23210SJeff Roberson */ 169244f1c916SBryan Drewery vm_pageout_wakeup_thresh = (vm_cnt.v_free_min / 10) * 11; 1693d9e23210SJeff Roberson 1694d9e23210SJeff Roberson /* 1695d9e23210SJeff Roberson * Set interval in seconds for active scan. We want to visit each 1696c9612b2dSJeff Roberson * page at least once every ten minutes. This is to prevent worst 1697c9612b2dSJeff Roberson * case paging behaviors with stale active LRU. 1698d9e23210SJeff Roberson */ 1699d9e23210SJeff Roberson if (vm_pageout_update_period == 0) 1700c9612b2dSJeff Roberson vm_pageout_update_period = 600; 1701d9e23210SJeff Roberson 1702df8bae1dSRodney W. Grimes /* XXX does not really belong here */ 1703df8bae1dSRodney W. Grimes if (vm_page_max_wired == 0) 170444f1c916SBryan Drewery vm_page_max_wired = vm_cnt.v_free_count / 3; 17054d19f4adSSteven Hartland } 17064d19f4adSSteven Hartland 17074d19f4adSSteven Hartland /* 17084d19f4adSSteven Hartland * vm_pageout is the high level pageout daemon. 17094d19f4adSSteven Hartland */ 17104d19f4adSSteven Hartland static void 17114d19f4adSSteven Hartland vm_pageout(void) 17124d19f4adSSteven Hartland { 17134d19f4adSSteven Hartland #if MAXMEMDOM > 1 17144d19f4adSSteven Hartland int error, i; 17154d19f4adSSteven Hartland #endif 1716df8bae1dSRodney W. Grimes 171724a1cce3SDavid Greenman swap_pager_swap_init(); 1718449c2e92SKonstantin Belousov #if MAXMEMDOM > 1 1719449c2e92SKonstantin Belousov for (i = 1; i < vm_ndomains; i++) { 1720449c2e92SKonstantin Belousov error = kthread_add(vm_pageout_worker, (void *)(uintptr_t)i, 1721449c2e92SKonstantin Belousov curproc, NULL, 0, 0, "dom%d", i); 1722449c2e92SKonstantin Belousov if (error != 0) { 1723449c2e92SKonstantin Belousov panic("starting pageout for domain %d, error %d\n", 1724449c2e92SKonstantin Belousov i, error); 1725dc2efb27SJohn Dyson } 1726f919ebdeSDavid Greenman } 1727449c2e92SKonstantin Belousov #endif 1728d395270dSDimitry Andric vm_pageout_worker((void *)(uintptr_t)0); 1729df8bae1dSRodney W. Grimes } 173026f9a767SRodney W. Grimes 17316b4b77adSAlan Cox /* 1732e9f995d8SAlan Cox * Unless the free page queue lock is held by the caller, this function 17336b4b77adSAlan Cox * should be regarded as advisory. Specifically, the caller should 173444f1c916SBryan Drewery * not msleep() on &vm_cnt.v_free_count following this function unless 1735e9f995d8SAlan Cox * the free page queue lock is held until the msleep() is performed. 17366b4b77adSAlan Cox */ 1737e0c5a895SJohn Dyson void 17384a365329SAndrey Zonov pagedaemon_wakeup(void) 1739e0c5a895SJohn Dyson { 1740a1c0a785SAlan Cox 1741b40ce416SJulian Elischer if (!vm_pages_needed && curthread->td_proc != pageproc) { 1742a1c0a785SAlan Cox vm_pages_needed = 1; 1743e0c5a895SJohn Dyson wakeup(&vm_pages_needed); 1744e0c5a895SJohn Dyson } 1745e0c5a895SJohn Dyson } 1746e0c5a895SJohn Dyson 174738efa82bSJohn Dyson #if !defined(NO_SWAPPING) 17485afce282SDavid Greenman static void 174997824da3SAlan Cox vm_req_vmdaemon(int req) 17505afce282SDavid Greenman { 17515afce282SDavid Greenman static int lastrun = 0; 17525afce282SDavid Greenman 175397824da3SAlan Cox mtx_lock(&vm_daemon_mtx); 175497824da3SAlan Cox vm_pageout_req_swapout |= req; 1755b18bfc3dSJohn Dyson if ((ticks > (lastrun + hz)) || (ticks < lastrun)) { 17565afce282SDavid Greenman wakeup(&vm_daemon_needed); 17575afce282SDavid Greenman lastrun = ticks; 17585afce282SDavid Greenman } 175997824da3SAlan Cox mtx_unlock(&vm_daemon_mtx); 17605afce282SDavid Greenman } 17615afce282SDavid Greenman 17622b14f991SJulian Elischer static void 17634a365329SAndrey Zonov vm_daemon(void) 17640d94caffSDavid Greenman { 176591d5354aSJohn Baldwin struct rlimit rsslim; 1766dcbcd518SBruce Evans struct proc *p; 1767dcbcd518SBruce Evans struct thread *td; 17686bed074cSKonstantin Belousov struct vmspace *vm; 1769099e7e95SEdward Tomasz Napierala int breakout, swapout_flags, tryagain, attempts; 1770afcc55f3SEdward Tomasz Napierala #ifdef RACCT 1771099e7e95SEdward Tomasz Napierala uint64_t rsize, ravailable; 1772afcc55f3SEdward Tomasz Napierala #endif 17730d94caffSDavid Greenman 17742fe6e4d7SDavid Greenman while (TRUE) { 177597824da3SAlan Cox mtx_lock(&vm_daemon_mtx); 1776099e7e95SEdward Tomasz Napierala #ifdef RACCT 1777099e7e95SEdward Tomasz Napierala msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", hz); 1778099e7e95SEdward Tomasz Napierala #else 177997824da3SAlan Cox msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", 0); 1780099e7e95SEdward Tomasz Napierala #endif 178197824da3SAlan Cox swapout_flags = vm_pageout_req_swapout; 17824c1f8ee9SDavid Greenman vm_pageout_req_swapout = 0; 178397824da3SAlan Cox mtx_unlock(&vm_daemon_mtx); 178497824da3SAlan Cox if (swapout_flags) 178597824da3SAlan Cox swapout_procs(swapout_flags); 178697824da3SAlan Cox 17872fe6e4d7SDavid Greenman /* 17880d94caffSDavid Greenman * scan the processes for exceeding their rlimits or if 17890d94caffSDavid Greenman * process is swapped out -- deactivate pages 17902fe6e4d7SDavid Greenman */ 1791099e7e95SEdward Tomasz Napierala tryagain = 0; 1792099e7e95SEdward Tomasz Napierala attempts = 0; 1793099e7e95SEdward Tomasz Napierala again: 1794099e7e95SEdward Tomasz Napierala attempts++; 17951005a129SJohn Baldwin sx_slock(&allproc_lock); 1796f67af5c9SXin LI FOREACH_PROC_IN_SYSTEM(p) { 1797fe2144fdSLuoqi Chen vm_pindex_t limit, size; 17982fe6e4d7SDavid Greenman 17992fe6e4d7SDavid Greenman /* 18002fe6e4d7SDavid Greenman * if this is a system process or if we have already 18012fe6e4d7SDavid Greenman * looked at this process, skip it. 18022fe6e4d7SDavid Greenman */ 1803897ecacdSJohn Baldwin PROC_LOCK(p); 18048e6fa660SJohn Baldwin if (p->p_state != PRS_NORMAL || 18058e6fa660SJohn Baldwin p->p_flag & (P_INEXEC | P_SYSTEM | P_WEXIT)) { 1806897ecacdSJohn Baldwin PROC_UNLOCK(p); 18072fe6e4d7SDavid Greenman continue; 18082fe6e4d7SDavid Greenman } 18092fe6e4d7SDavid Greenman /* 18102fe6e4d7SDavid Greenman * if the process is in a non-running type state, 18112fe6e4d7SDavid Greenman * don't touch it. 18122fe6e4d7SDavid Greenman */ 1813e602ba25SJulian Elischer breakout = 0; 1814e602ba25SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 1815982d11f8SJeff Roberson thread_lock(td); 181671fad9fdSJulian Elischer if (!TD_ON_RUNQ(td) && 181771fad9fdSJulian Elischer !TD_IS_RUNNING(td) && 1818f497cda2SEdward Tomasz Napierala !TD_IS_SLEEPING(td) && 1819f497cda2SEdward Tomasz Napierala !TD_IS_SUSPENDED(td)) { 1820982d11f8SJeff Roberson thread_unlock(td); 1821e602ba25SJulian Elischer breakout = 1; 1822e602ba25SJulian Elischer break; 1823e602ba25SJulian Elischer } 1824982d11f8SJeff Roberson thread_unlock(td); 1825e602ba25SJulian Elischer } 1826897ecacdSJohn Baldwin if (breakout) { 1827897ecacdSJohn Baldwin PROC_UNLOCK(p); 18282fe6e4d7SDavid Greenman continue; 18292fe6e4d7SDavid Greenman } 18302fe6e4d7SDavid Greenman /* 18312fe6e4d7SDavid Greenman * get a limit 18322fe6e4d7SDavid Greenman */ 1833dcbcd518SBruce Evans lim_rlimit(p, RLIMIT_RSS, &rsslim); 1834fe2144fdSLuoqi Chen limit = OFF_TO_IDX( 183591d5354aSJohn Baldwin qmin(rsslim.rlim_cur, rsslim.rlim_max)); 18362fe6e4d7SDavid Greenman 18372fe6e4d7SDavid Greenman /* 18380d94caffSDavid Greenman * let processes that are swapped out really be 18390d94caffSDavid Greenman * swapped out set the limit to nothing (will force a 18400d94caffSDavid Greenman * swap-out.) 18412fe6e4d7SDavid Greenman */ 1842b61ce5b0SJeff Roberson if ((p->p_flag & P_INMEM) == 0) 18430d94caffSDavid Greenman limit = 0; /* XXX */ 18446bed074cSKonstantin Belousov vm = vmspace_acquire_ref(p); 1845897ecacdSJohn Baldwin PROC_UNLOCK(p); 18466bed074cSKonstantin Belousov if (vm == NULL) 18476bed074cSKonstantin Belousov continue; 18482fe6e4d7SDavid Greenman 18496bed074cSKonstantin Belousov size = vmspace_resident_count(vm); 1850a406d8c3SEdward Tomasz Napierala if (size >= limit) { 1851fe2144fdSLuoqi Chen vm_pageout_map_deactivate_pages( 18526bed074cSKonstantin Belousov &vm->vm_map, limit); 18532fe6e4d7SDavid Greenman } 1854afcc55f3SEdward Tomasz Napierala #ifdef RACCT 1855099e7e95SEdward Tomasz Napierala rsize = IDX_TO_OFF(size); 1856099e7e95SEdward Tomasz Napierala PROC_LOCK(p); 1857099e7e95SEdward Tomasz Napierala racct_set(p, RACCT_RSS, rsize); 1858099e7e95SEdward Tomasz Napierala ravailable = racct_get_available(p, RACCT_RSS); 1859099e7e95SEdward Tomasz Napierala PROC_UNLOCK(p); 1860099e7e95SEdward Tomasz Napierala if (rsize > ravailable) { 1861099e7e95SEdward Tomasz Napierala /* 1862099e7e95SEdward Tomasz Napierala * Don't be overly aggressive; this might be 1863099e7e95SEdward Tomasz Napierala * an innocent process, and the limit could've 1864099e7e95SEdward Tomasz Napierala * been exceeded by some memory hog. Don't 1865099e7e95SEdward Tomasz Napierala * try to deactivate more than 1/4th of process' 1866099e7e95SEdward Tomasz Napierala * resident set size. 1867099e7e95SEdward Tomasz Napierala */ 1868099e7e95SEdward Tomasz Napierala if (attempts <= 8) { 1869099e7e95SEdward Tomasz Napierala if (ravailable < rsize - (rsize / 4)) 1870099e7e95SEdward Tomasz Napierala ravailable = rsize - (rsize / 4); 1871099e7e95SEdward Tomasz Napierala } 1872099e7e95SEdward Tomasz Napierala vm_pageout_map_deactivate_pages( 1873099e7e95SEdward Tomasz Napierala &vm->vm_map, OFF_TO_IDX(ravailable)); 1874099e7e95SEdward Tomasz Napierala /* Update RSS usage after paging out. */ 1875099e7e95SEdward Tomasz Napierala size = vmspace_resident_count(vm); 1876099e7e95SEdward Tomasz Napierala rsize = IDX_TO_OFF(size); 1877099e7e95SEdward Tomasz Napierala PROC_LOCK(p); 1878099e7e95SEdward Tomasz Napierala racct_set(p, RACCT_RSS, rsize); 1879099e7e95SEdward Tomasz Napierala PROC_UNLOCK(p); 1880099e7e95SEdward Tomasz Napierala if (rsize > ravailable) 1881099e7e95SEdward Tomasz Napierala tryagain = 1; 1882099e7e95SEdward Tomasz Napierala } 1883afcc55f3SEdward Tomasz Napierala #endif 18846bed074cSKonstantin Belousov vmspace_free(vm); 18852fe6e4d7SDavid Greenman } 18861005a129SJohn Baldwin sx_sunlock(&allproc_lock); 1887099e7e95SEdward Tomasz Napierala if (tryagain != 0 && attempts <= 10) 1888099e7e95SEdward Tomasz Napierala goto again; 188924a1cce3SDavid Greenman } 18902fe6e4d7SDavid Greenman } 1891a1287949SEivind Eklund #endif /* !defined(NO_SWAPPING) */ 1892