160727d8bSWarner Losh /*- 226f9a767SRodney W. Grimes * Copyright (c) 1991 Regents of the University of California. 326f9a767SRodney W. Grimes * All rights reserved. 426f9a767SRodney W. Grimes * Copyright (c) 1994 John S. Dyson 526f9a767SRodney W. Grimes * All rights reserved. 626f9a767SRodney W. Grimes * Copyright (c) 1994 David Greenman 726f9a767SRodney W. Grimes * All rights reserved. 88dbca793STor Egge * Copyright (c) 2005 Yahoo! Technologies Norway AS 98dbca793STor Egge * All rights reserved. 10df8bae1dSRodney W. Grimes * 11df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 12df8bae1dSRodney W. Grimes * The Mach Operating System project at Carnegie-Mellon University. 13df8bae1dSRodney W. Grimes * 14df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 15df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 16df8bae1dSRodney W. Grimes * are met: 17df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 18df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 19df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 20df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 21df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 22df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 235929bcfaSPhilippe Charnier * must display the following acknowledgement: 24df8bae1dSRodney W. Grimes * This product includes software developed by the University of 25df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 26df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 27df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 28df8bae1dSRodney W. Grimes * without specific prior written permission. 29df8bae1dSRodney W. Grimes * 30df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 31df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 34df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 35df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 36df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 37df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 38df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 39df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 40df8bae1dSRodney W. Grimes * SUCH DAMAGE. 41df8bae1dSRodney W. Grimes * 423c4dd356SDavid Greenman * from: @(#)vm_pageout.c 7.4 (Berkeley) 5/7/91 43df8bae1dSRodney W. Grimes * 44df8bae1dSRodney W. Grimes * 45df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1990 Carnegie-Mellon University. 46df8bae1dSRodney W. Grimes * All rights reserved. 47df8bae1dSRodney W. Grimes * 48df8bae1dSRodney W. Grimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young 49df8bae1dSRodney W. Grimes * 50df8bae1dSRodney W. Grimes * Permission to use, copy, modify and distribute this software and 51df8bae1dSRodney W. Grimes * its documentation is hereby granted, provided that both the copyright 52df8bae1dSRodney W. Grimes * notice and this permission notice appear in all copies of the 53df8bae1dSRodney W. Grimes * software, derivative works or modified versions, and any portions 54df8bae1dSRodney W. Grimes * thereof, and that both notices appear in supporting documentation. 55df8bae1dSRodney W. Grimes * 56df8bae1dSRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 57df8bae1dSRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 58df8bae1dSRodney W. Grimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 59df8bae1dSRodney W. Grimes * 60df8bae1dSRodney W. Grimes * Carnegie Mellon requests users of this software to return to 61df8bae1dSRodney W. Grimes * 62df8bae1dSRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 63df8bae1dSRodney W. Grimes * School of Computer Science 64df8bae1dSRodney W. Grimes * Carnegie Mellon University 65df8bae1dSRodney W. Grimes * Pittsburgh PA 15213-3890 66df8bae1dSRodney W. Grimes * 67df8bae1dSRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the 68df8bae1dSRodney W. Grimes * rights to redistribute these changes. 69df8bae1dSRodney W. Grimes */ 70df8bae1dSRodney W. Grimes 71df8bae1dSRodney W. Grimes /* 72df8bae1dSRodney W. Grimes * The proverbial page-out daemon. 73df8bae1dSRodney W. Grimes */ 74df8bae1dSRodney W. Grimes 75874651b1SDavid E. O'Brien #include <sys/cdefs.h> 76874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 77874651b1SDavid E. O'Brien 78faa5f8d8SAndrzej Bialecki #include "opt_vm.h" 79df8bae1dSRodney W. Grimes #include <sys/param.h> 8026f9a767SRodney W. Grimes #include <sys/systm.h> 81b5e8ce9fSBruce Evans #include <sys/kernel.h> 82855a310fSJeff Roberson #include <sys/eventhandler.h> 83fb919e4dSMark Murray #include <sys/lock.h> 84fb919e4dSMark Murray #include <sys/mutex.h> 8526f9a767SRodney W. Grimes #include <sys/proc.h> 869c8b8baaSPeter Wemm #include <sys/kthread.h> 870384fff8SJason Evans #include <sys/ktr.h> 8897824da3SAlan Cox #include <sys/mount.h> 89099e7e95SEdward Tomasz Napierala #include <sys/racct.h> 9026f9a767SRodney W. Grimes #include <sys/resourcevar.h> 91b43179fbSJeff Roberson #include <sys/sched.h> 92d2fc5315SPoul-Henning Kamp #include <sys/signalvar.h> 93f6b04d2bSDavid Greenman #include <sys/vnode.h> 94efeaf95aSDavid Greenman #include <sys/vmmeter.h> 951005a129SJohn Baldwin #include <sys/sx.h> 9638efa82bSJohn Dyson #include <sys/sysctl.h> 97df8bae1dSRodney W. Grimes 98df8bae1dSRodney W. Grimes #include <vm/vm.h> 99efeaf95aSDavid Greenman #include <vm/vm_param.h> 100efeaf95aSDavid Greenman #include <vm/vm_object.h> 101df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 102efeaf95aSDavid Greenman #include <vm/vm_map.h> 103df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h> 10424a1cce3SDavid Greenman #include <vm/vm_pager.h> 10505f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h> 106efeaf95aSDavid Greenman #include <vm/vm_extern.h> 107670d17b5SJeff Roberson #include <vm/uma.h> 108df8bae1dSRodney W. Grimes 1092b14f991SJulian Elischer /* 1102b14f991SJulian Elischer * System initialization 1112b14f991SJulian Elischer */ 1122b14f991SJulian Elischer 1132b14f991SJulian Elischer /* the kernel process "vm_pageout"*/ 11411caded3SAlfred Perlstein static void vm_pageout(void); 11511caded3SAlfred Perlstein static int vm_pageout_clean(vm_page_t); 11611caded3SAlfred Perlstein static void vm_pageout_scan(int pass); 11745ae1d91SAlan Cox 1182b14f991SJulian Elischer struct proc *pageproc; 1192b14f991SJulian Elischer 1202b14f991SJulian Elischer static struct kproc_desc page_kp = { 1212b14f991SJulian Elischer "pagedaemon", 1222b14f991SJulian Elischer vm_pageout, 1232b14f991SJulian Elischer &pageproc 1242b14f991SJulian Elischer }; 125237fdd78SRobert Watson SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, 126237fdd78SRobert Watson &page_kp); 1272b14f991SJulian Elischer 12838efa82bSJohn Dyson #if !defined(NO_SWAPPING) 1292b14f991SJulian Elischer /* the kernel process "vm_daemon"*/ 13011caded3SAlfred Perlstein static void vm_daemon(void); 131f708ef1bSPoul-Henning Kamp static struct proc *vmproc; 1322b14f991SJulian Elischer 1332b14f991SJulian Elischer static struct kproc_desc vm_kp = { 1342b14f991SJulian Elischer "vmdaemon", 1352b14f991SJulian Elischer vm_daemon, 1362b14f991SJulian Elischer &vmproc 1372b14f991SJulian Elischer }; 138237fdd78SRobert Watson SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp); 13938efa82bSJohn Dyson #endif 1402b14f991SJulian Elischer 1412b14f991SJulian Elischer 1428b245767SAlan Cox int vm_pages_needed; /* Event on which pageout daemon sleeps */ 1438b245767SAlan Cox int vm_pageout_deficit; /* Estimated number of pages deficit */ 1448b245767SAlan Cox int vm_pageout_pages_needed; /* flag saying that the pageout daemon needs pages */ 14526f9a767SRodney W. Grimes 14638efa82bSJohn Dyson #if !defined(NO_SWAPPING) 147f708ef1bSPoul-Henning Kamp static int vm_pageout_req_swapout; /* XXX */ 148f708ef1bSPoul-Henning Kamp static int vm_daemon_needed; 14997824da3SAlan Cox static struct mtx vm_daemon_mtx; 15097824da3SAlan Cox /* Allow for use by vm_pageout before vm_daemon is initialized. */ 15197824da3SAlan Cox MTX_SYSINIT(vm_daemon, &vm_daemon_mtx, "vm daemon", MTX_DEF); 15238efa82bSJohn Dyson #endif 1532b6b0df7SMatthew Dillon static int vm_max_launder = 32; 154303b270bSEivind Eklund static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0; 155303b270bSEivind Eklund static int vm_pageout_full_stats_interval = 0; 15626354d4cSAlan Cox static int vm_pageout_algorithm=0; 157303b270bSEivind Eklund static int defer_swap_pageouts=0; 158303b270bSEivind Eklund static int disable_swap_pageouts=0; 15970111b90SJohn Dyson 16038efa82bSJohn Dyson #if defined(NO_SWAPPING) 161303b270bSEivind Eklund static int vm_swap_enabled=0; 162303b270bSEivind Eklund static int vm_swap_idle_enabled=0; 16338efa82bSJohn Dyson #else 164303b270bSEivind Eklund static int vm_swap_enabled=1; 165303b270bSEivind Eklund static int vm_swap_idle_enabled=0; 16638efa82bSJohn Dyson #endif 16738efa82bSJohn Dyson 16838efa82bSJohn Dyson SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm, 1692b6b0df7SMatthew Dillon CTLFLAG_RW, &vm_pageout_algorithm, 0, "LRU page mgmt"); 1702b6b0df7SMatthew Dillon 1712b6b0df7SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, max_launder, 1722b6b0df7SMatthew Dillon CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout"); 17338efa82bSJohn Dyson 174dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max, 175b0359e2cSPeter Wemm CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length"); 176dc2efb27SJohn Dyson 177dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval, 178b0359e2cSPeter Wemm CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan"); 179dc2efb27SJohn Dyson 180dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval, 181b0359e2cSPeter Wemm CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan"); 182dc2efb27SJohn Dyson 18338efa82bSJohn Dyson #if defined(NO_SWAPPING) 184ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 1856bd9cb1cSTom Rhodes CTLFLAG_RD, &vm_swap_enabled, 0, "Enable entire process swapout"); 186ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 1876bd9cb1cSTom Rhodes CTLFLAG_RD, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria"); 18838efa82bSJohn Dyson #else 189ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 190b0359e2cSPeter Wemm CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout"); 191ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 192b0359e2cSPeter Wemm CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria"); 19338efa82bSJohn Dyson #endif 19426f9a767SRodney W. Grimes 195ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts, 196b0359e2cSPeter Wemm CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem"); 19712ac6a1dSJohn Dyson 198ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts, 199b0359e2cSPeter Wemm CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages"); 20012ac6a1dSJohn Dyson 20123b59018SMatthew Dillon static int pageout_lock_miss; 20223b59018SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss, 20323b59018SMatthew Dillon CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout"); 20423b59018SMatthew Dillon 205ffc82b0aSJohn Dyson #define VM_PAGEOUT_PAGE_COUNT 16 206bbc0ec52SDavid Greenman int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT; 207df8bae1dSRodney W. Grimes 208c3cb3e12SDavid Greenman int vm_page_max_wired; /* XXX max # of wired pages system-wide */ 2095dfc2870SAlan Cox SYSCTL_INT(_vm, OID_AUTO, max_wired, 2105dfc2870SAlan Cox CTLFLAG_RW, &vm_page_max_wired, 0, "System-wide limit to wired page count"); 211df8bae1dSRodney W. Grimes 21285eeca35SAlan Cox static boolean_t vm_pageout_fallback_object_lock(vm_page_t, vm_page_t *); 21385eeca35SAlan Cox static boolean_t vm_pageout_launder(int, int, vm_paddr_t, vm_paddr_t); 21438efa82bSJohn Dyson #if !defined(NO_SWAPPING) 215ecf6279fSAlan Cox static void vm_pageout_map_deactivate_pages(vm_map_t, long); 216ecf6279fSAlan Cox static void vm_pageout_object_deactivate_pages(pmap_t, vm_object_t, long); 21797824da3SAlan Cox static void vm_req_vmdaemon(int req); 21838efa82bSJohn Dyson #endif 21985eeca35SAlan Cox static boolean_t vm_pageout_page_lock(vm_page_t, vm_page_t *); 220dc2efb27SJohn Dyson static void vm_pageout_page_stats(void); 221cd41fc12SDavid Greenman 222a8229fa3SAlan Cox /* 223a8229fa3SAlan Cox * Initialize a dummy page for marking the caller's place in the specified 224a8229fa3SAlan Cox * paging queue. In principle, this function only needs to set the flag 225a8229fa3SAlan Cox * PG_MARKER. Nonetheless, it sets the flag VPO_BUSY and initializes the hold 226a8229fa3SAlan Cox * count to one as safety precautions. 227a8229fa3SAlan Cox */ 2288c616246SKonstantin Belousov static void 2298c616246SKonstantin Belousov vm_pageout_init_marker(vm_page_t marker, u_short queue) 2308c616246SKonstantin Belousov { 2318c616246SKonstantin Belousov 2328c616246SKonstantin Belousov bzero(marker, sizeof(*marker)); 233a8229fa3SAlan Cox marker->flags = PG_MARKER; 2348c616246SKonstantin Belousov marker->oflags = VPO_BUSY; 2358c616246SKonstantin Belousov marker->queue = queue; 236a8229fa3SAlan Cox marker->hold_count = 1; 2378c616246SKonstantin Belousov } 2388c616246SKonstantin Belousov 23926f9a767SRodney W. Grimes /* 2408dbca793STor Egge * vm_pageout_fallback_object_lock: 2418dbca793STor Egge * 2428dbca793STor Egge * Lock vm object currently associated with `m'. VM_OBJECT_TRYLOCK is 2438dbca793STor Egge * known to have failed and page queue must be either PQ_ACTIVE or 2448dbca793STor Egge * PQ_INACTIVE. To avoid lock order violation, unlock the page queues 2458dbca793STor Egge * while locking the vm object. Use marker page to detect page queue 2468dbca793STor Egge * changes and maintain notion of next page on page queue. Return 2478dbca793STor Egge * TRUE if no changes were detected, FALSE otherwise. vm object is 2488dbca793STor Egge * locked on return. 2498dbca793STor Egge * 2508dbca793STor Egge * This function depends on both the lock portion of struct vm_object 2518dbca793STor Egge * and normal struct vm_page being type stable. 2528dbca793STor Egge */ 25385eeca35SAlan Cox static boolean_t 2548dbca793STor Egge vm_pageout_fallback_object_lock(vm_page_t m, vm_page_t *next) 2558dbca793STor Egge { 2568dbca793STor Egge struct vm_page marker; 257*8d220203SAlan Cox struct vm_pagequeue *pq; 2588dbca793STor Egge boolean_t unchanged; 2598dbca793STor Egge u_short queue; 2608dbca793STor Egge vm_object_t object; 2618dbca793STor Egge 2628dbca793STor Egge queue = m->queue; 2638c616246SKonstantin Belousov vm_pageout_init_marker(&marker, queue); 264*8d220203SAlan Cox pq = &vm_pagequeues[queue]; 2658dbca793STor Egge object = m->object; 2668dbca793STor Egge 267*8d220203SAlan Cox TAILQ_INSERT_AFTER(&pq->pq_pl, m, &marker, pageq); 268*8d220203SAlan Cox vm_pagequeue_unlock(pq); 2692965a453SKip Macy vm_page_unlock(m); 2708dbca793STor Egge VM_OBJECT_LOCK(object); 2712965a453SKip Macy vm_page_lock(m); 272*8d220203SAlan Cox vm_pagequeue_lock(pq); 2738dbca793STor Egge 2748dbca793STor Egge /* Page queue might have changed. */ 2758dbca793STor Egge *next = TAILQ_NEXT(&marker, pageq); 2768dbca793STor Egge unchanged = (m->queue == queue && 2778dbca793STor Egge m->object == object && 2788dbca793STor Egge &marker == TAILQ_NEXT(m, pageq)); 279*8d220203SAlan Cox TAILQ_REMOVE(&pq->pq_pl, &marker, pageq); 2808dbca793STor Egge return (unchanged); 2818dbca793STor Egge } 2828dbca793STor Egge 2838dbca793STor Egge /* 2848c616246SKonstantin Belousov * Lock the page while holding the page queue lock. Use marker page 2858c616246SKonstantin Belousov * to detect page queue changes and maintain notion of next page on 2868c616246SKonstantin Belousov * page queue. Return TRUE if no changes were detected, FALSE 2878c616246SKonstantin Belousov * otherwise. The page is locked on return. The page queue lock might 2888c616246SKonstantin Belousov * be dropped and reacquired. 2898c616246SKonstantin Belousov * 2908c616246SKonstantin Belousov * This function depends on normal struct vm_page being type stable. 2918c616246SKonstantin Belousov */ 29285eeca35SAlan Cox static boolean_t 2938c616246SKonstantin Belousov vm_pageout_page_lock(vm_page_t m, vm_page_t *next) 2948c616246SKonstantin Belousov { 2958c616246SKonstantin Belousov struct vm_page marker; 296*8d220203SAlan Cox struct vm_pagequeue *pq; 2978c616246SKonstantin Belousov boolean_t unchanged; 2988c616246SKonstantin Belousov u_short queue; 2998c616246SKonstantin Belousov 3008c616246SKonstantin Belousov vm_page_lock_assert(m, MA_NOTOWNED); 3018c616246SKonstantin Belousov if (vm_page_trylock(m)) 3028c616246SKonstantin Belousov return (TRUE); 3038c616246SKonstantin Belousov 3048c616246SKonstantin Belousov queue = m->queue; 3058c616246SKonstantin Belousov vm_pageout_init_marker(&marker, queue); 306*8d220203SAlan Cox pq = &vm_pagequeues[queue]; 3078c616246SKonstantin Belousov 308*8d220203SAlan Cox TAILQ_INSERT_AFTER(&pq->pq_pl, m, &marker, pageq); 309*8d220203SAlan Cox vm_pagequeue_unlock(pq); 3108c616246SKonstantin Belousov vm_page_lock(m); 311*8d220203SAlan Cox vm_pagequeue_lock(pq); 3128c616246SKonstantin Belousov 3138c616246SKonstantin Belousov /* Page queue might have changed. */ 3148c616246SKonstantin Belousov *next = TAILQ_NEXT(&marker, pageq); 3158c616246SKonstantin Belousov unchanged = (m->queue == queue && &marker == TAILQ_NEXT(m, pageq)); 316*8d220203SAlan Cox TAILQ_REMOVE(&pq->pq_pl, &marker, pageq); 3178c616246SKonstantin Belousov return (unchanged); 3188c616246SKonstantin Belousov } 3198c616246SKonstantin Belousov 3208c616246SKonstantin Belousov /* 32126f9a767SRodney W. Grimes * vm_pageout_clean: 32224a1cce3SDavid Greenman * 3230d94caffSDavid Greenman * Clean the page and remove it from the laundry. 32426f9a767SRodney W. Grimes * 3250d94caffSDavid Greenman * We set the busy bit to cause potential page faults on this page to 3261c7c3c6aSMatthew Dillon * block. Note the careful timing, however, the busy bit isn't set till 3271c7c3c6aSMatthew Dillon * late and we cannot do anything that will mess with the page. 32826f9a767SRodney W. Grimes */ 3293af76890SPoul-Henning Kamp static int 3302965a453SKip Macy vm_pageout_clean(vm_page_t m) 33124a1cce3SDavid Greenman { 33254d92145SMatthew Dillon vm_object_t object; 33391b4f427SAlan Cox vm_page_t mc[2*vm_pageout_page_count], pb, ps; 3343562af12SAlan Cox int pageout_count; 33590ecac61SMatthew Dillon int ib, is, page_base; 336a316d390SJohn Dyson vm_pindex_t pindex = m->pindex; 33726f9a767SRodney W. Grimes 33895976f3fSAlan Cox vm_page_lock_assert(m, MA_OWNED); 33917f6a17bSAlan Cox object = m->object; 34017f6a17bSAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 3410cddd8f0SMatthew Dillon 34226f9a767SRodney W. Grimes /* 3431c7c3c6aSMatthew Dillon * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP 3441c7c3c6aSMatthew Dillon * with the new swapper, but we could have serious problems paging 3451c7c3c6aSMatthew Dillon * out other object types if there is insufficient memory. 3461c7c3c6aSMatthew Dillon * 3471c7c3c6aSMatthew Dillon * Unfortunately, checking free memory here is far too late, so the 3481c7c3c6aSMatthew Dillon * check has been moved up a procedural level. 3491c7c3c6aSMatthew Dillon */ 3501c7c3c6aSMatthew Dillon 35124a1cce3SDavid Greenman /* 3529e897b1bSAlan Cox * Can't clean the page if it's busy or held. 35324a1cce3SDavid Greenman */ 35495976f3fSAlan Cox KASSERT(m->busy == 0 && (m->oflags & VPO_BUSY) == 0, 35595976f3fSAlan Cox ("vm_pageout_clean: page %p is busy", m)); 35695976f3fSAlan Cox KASSERT(m->hold_count == 0, ("vm_pageout_clean: page %p is held", m)); 35717f6a17bSAlan Cox vm_page_unlock(m); 3580d94caffSDavid Greenman 35991b4f427SAlan Cox mc[vm_pageout_page_count] = pb = ps = m; 36026f9a767SRodney W. Grimes pageout_count = 1; 361f35329acSJohn Dyson page_base = vm_pageout_page_count; 36290ecac61SMatthew Dillon ib = 1; 36390ecac61SMatthew Dillon is = 1; 36490ecac61SMatthew Dillon 36524a1cce3SDavid Greenman /* 36624a1cce3SDavid Greenman * Scan object for clusterable pages. 36724a1cce3SDavid Greenman * 36824a1cce3SDavid Greenman * We can cluster ONLY if: ->> the page is NOT 36924a1cce3SDavid Greenman * clean, wired, busy, held, or mapped into a 37024a1cce3SDavid Greenman * buffer, and one of the following: 37124a1cce3SDavid Greenman * 1) The page is inactive, or a seldom used 37224a1cce3SDavid Greenman * active page. 37324a1cce3SDavid Greenman * -or- 37424a1cce3SDavid Greenman * 2) we force the issue. 37590ecac61SMatthew Dillon * 37690ecac61SMatthew Dillon * During heavy mmap/modification loads the pageout 37790ecac61SMatthew Dillon * daemon can really fragment the underlying file 37890ecac61SMatthew Dillon * due to flushing pages out of order and not trying 37990ecac61SMatthew Dillon * align the clusters (which leave sporatic out-of-order 38090ecac61SMatthew Dillon * holes). To solve this problem we do the reverse scan 38190ecac61SMatthew Dillon * first and attempt to align our cluster, then do a 38290ecac61SMatthew Dillon * forward scan if room remains. 38324a1cce3SDavid Greenman */ 38490ecac61SMatthew Dillon more: 38590ecac61SMatthew Dillon while (ib && pageout_count < vm_pageout_page_count) { 38624a1cce3SDavid Greenman vm_page_t p; 387f6b04d2bSDavid Greenman 38890ecac61SMatthew Dillon if (ib > pindex) { 38990ecac61SMatthew Dillon ib = 0; 39090ecac61SMatthew Dillon break; 391f6b04d2bSDavid Greenman } 39290ecac61SMatthew Dillon 39391b4f427SAlan Cox if ((p = vm_page_prev(pb)) == NULL || 39491b4f427SAlan Cox (p->oflags & VPO_BUSY) != 0 || p->busy != 0) { 39590ecac61SMatthew Dillon ib = 0; 39690ecac61SMatthew Dillon break; 397f6b04d2bSDavid Greenman } 3982965a453SKip Macy vm_page_lock(p); 39924a1cce3SDavid Greenman vm_page_test_dirty(p); 40026f4eea5SAlan Cox if (p->dirty == 0 || 40190ecac61SMatthew Dillon p->queue != PQ_INACTIVE || 40257601bcbSMatthew Dillon p->hold_count != 0) { /* may be undergoing I/O */ 4032965a453SKip Macy vm_page_unlock(p); 40490ecac61SMatthew Dillon ib = 0; 40524a1cce3SDavid Greenman break; 406f6b04d2bSDavid Greenman } 4072965a453SKip Macy vm_page_unlock(p); 40891b4f427SAlan Cox mc[--page_base] = pb = p; 40990ecac61SMatthew Dillon ++pageout_count; 41090ecac61SMatthew Dillon ++ib; 41124a1cce3SDavid Greenman /* 41290ecac61SMatthew Dillon * alignment boundry, stop here and switch directions. Do 41390ecac61SMatthew Dillon * not clear ib. 41424a1cce3SDavid Greenman */ 41590ecac61SMatthew Dillon if ((pindex - (ib - 1)) % vm_pageout_page_count == 0) 41690ecac61SMatthew Dillon break; 41724a1cce3SDavid Greenman } 41890ecac61SMatthew Dillon 41990ecac61SMatthew Dillon while (pageout_count < vm_pageout_page_count && 42090ecac61SMatthew Dillon pindex + is < object->size) { 42190ecac61SMatthew Dillon vm_page_t p; 42290ecac61SMatthew Dillon 42391b4f427SAlan Cox if ((p = vm_page_next(ps)) == NULL || 42491b4f427SAlan Cox (p->oflags & VPO_BUSY) != 0 || p->busy != 0) 42590ecac61SMatthew Dillon break; 4262965a453SKip Macy vm_page_lock(p); 42724a1cce3SDavid Greenman vm_page_test_dirty(p); 42826f4eea5SAlan Cox if (p->dirty == 0 || 42990ecac61SMatthew Dillon p->queue != PQ_INACTIVE || 43057601bcbSMatthew Dillon p->hold_count != 0) { /* may be undergoing I/O */ 4312965a453SKip Macy vm_page_unlock(p); 43224a1cce3SDavid Greenman break; 43324a1cce3SDavid Greenman } 4342965a453SKip Macy vm_page_unlock(p); 43591b4f427SAlan Cox mc[page_base + pageout_count] = ps = p; 43690ecac61SMatthew Dillon ++pageout_count; 43790ecac61SMatthew Dillon ++is; 43824a1cce3SDavid Greenman } 43990ecac61SMatthew Dillon 44090ecac61SMatthew Dillon /* 44190ecac61SMatthew Dillon * If we exhausted our forward scan, continue with the reverse scan 44290ecac61SMatthew Dillon * when possible, even past a page boundry. This catches boundry 44390ecac61SMatthew Dillon * conditions. 44490ecac61SMatthew Dillon */ 44590ecac61SMatthew Dillon if (ib && pageout_count < vm_pageout_page_count) 44690ecac61SMatthew Dillon goto more; 447f6b04d2bSDavid Greenman 44867bf6868SJohn Dyson /* 44967bf6868SJohn Dyson * we allow reads during pageouts... 45067bf6868SJohn Dyson */ 451126d6082SKonstantin Belousov return (vm_pageout_flush(&mc[page_base], pageout_count, 0, 0, NULL, 452126d6082SKonstantin Belousov NULL)); 453aef922f5SJohn Dyson } 454aef922f5SJohn Dyson 4551c7c3c6aSMatthew Dillon /* 4561c7c3c6aSMatthew Dillon * vm_pageout_flush() - launder the given pages 4571c7c3c6aSMatthew Dillon * 4581c7c3c6aSMatthew Dillon * The given pages are laundered. Note that we setup for the start of 4591c7c3c6aSMatthew Dillon * I/O ( i.e. busy the page ), mark it read-only, and bump the object 4601c7c3c6aSMatthew Dillon * reference count all in here rather then in the parent. If we want 4611c7c3c6aSMatthew Dillon * the parent to do more sophisticated things we may have to change 4621c7c3c6aSMatthew Dillon * the ordering. 4631e8a675cSKonstantin Belousov * 4641e8a675cSKonstantin Belousov * Returned runlen is the count of pages between mreq and first 4651e8a675cSKonstantin Belousov * page after mreq with status VM_PAGER_AGAIN. 466126d6082SKonstantin Belousov * *eio is set to TRUE if pager returned VM_PAGER_ERROR or VM_PAGER_FAIL 467126d6082SKonstantin Belousov * for any page in runlen set. 4681c7c3c6aSMatthew Dillon */ 469aef922f5SJohn Dyson int 470126d6082SKonstantin Belousov vm_pageout_flush(vm_page_t *mc, int count, int flags, int mreq, int *prunlen, 471126d6082SKonstantin Belousov boolean_t *eio) 472aef922f5SJohn Dyson { 4732e3b314dSAlan Cox vm_object_t object = mc[0]->object; 474aef922f5SJohn Dyson int pageout_status[count]; 47595461b45SJohn Dyson int numpagedout = 0; 4761e8a675cSKonstantin Belousov int i, runlen; 477aef922f5SJohn Dyson 4782e3b314dSAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 4797bec141bSKip Macy 4801c7c3c6aSMatthew Dillon /* 4811c7c3c6aSMatthew Dillon * Initiate I/O. Bump the vm_page_t->busy counter and 4821c7c3c6aSMatthew Dillon * mark the pages read-only. 4831c7c3c6aSMatthew Dillon * 4841c7c3c6aSMatthew Dillon * We do not have to fixup the clean/dirty bits here... we can 4851c7c3c6aSMatthew Dillon * allow the pager to do it after the I/O completes. 48602fa91d3SMatthew Dillon * 48702fa91d3SMatthew Dillon * NOTE! mc[i]->dirty may be partial or fragmented due to an 48802fa91d3SMatthew Dillon * edge case with file fragments. 4891c7c3c6aSMatthew Dillon */ 4908f9110f6SJohn Dyson for (i = 0; i < count; i++) { 4917a935082SAlan Cox KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL, 4927a935082SAlan Cox ("vm_pageout_flush: partially invalid page %p index %d/%d", 4937a935082SAlan Cox mc[i], i, count)); 494e69763a3SDoug Rabson vm_page_io_start(mc[i]); 49578985e42SAlan Cox pmap_remove_write(mc[i]); 4962965a453SKip Macy } 497d474eaaaSDoug Rabson vm_object_pip_add(object, count); 498aef922f5SJohn Dyson 499d076fbeaSAlan Cox vm_pager_put_pages(object, mc, count, flags, pageout_status); 50026f9a767SRodney W. Grimes 5011e8a675cSKonstantin Belousov runlen = count - mreq; 502126d6082SKonstantin Belousov if (eio != NULL) 503126d6082SKonstantin Belousov *eio = FALSE; 504aef922f5SJohn Dyson for (i = 0; i < count; i++) { 505aef922f5SJohn Dyson vm_page_t mt = mc[i]; 50624a1cce3SDavid Greenman 5074cd45723SAlan Cox KASSERT(pageout_status[i] == VM_PAGER_PEND || 5086031c68dSAlan Cox !pmap_page_is_write_mapped(mt), 5099ea8d1a6SAlan Cox ("vm_pageout_flush: page %p is not write protected", mt)); 51026f9a767SRodney W. Grimes switch (pageout_status[i]) { 51126f9a767SRodney W. Grimes case VM_PAGER_OK: 51226f9a767SRodney W. Grimes case VM_PAGER_PEND: 51395461b45SJohn Dyson numpagedout++; 51426f9a767SRodney W. Grimes break; 51526f9a767SRodney W. Grimes case VM_PAGER_BAD: 51626f9a767SRodney W. Grimes /* 5170d94caffSDavid Greenman * Page outside of range of object. Right now we 5180d94caffSDavid Greenman * essentially lose the changes by pretending it 5190d94caffSDavid Greenman * worked. 52026f9a767SRodney W. Grimes */ 52190ecac61SMatthew Dillon vm_page_undirty(mt); 52226f9a767SRodney W. Grimes break; 52326f9a767SRodney W. Grimes case VM_PAGER_ERROR: 52426f9a767SRodney W. Grimes case VM_PAGER_FAIL: 52526f9a767SRodney W. Grimes /* 5260d94caffSDavid Greenman * If page couldn't be paged out, then reactivate the 5270d94caffSDavid Greenman * page so it doesn't clog the inactive list. (We 5280d94caffSDavid Greenman * will try paging out it again later). 52926f9a767SRodney W. Grimes */ 5303c4a2440SAlan Cox vm_page_lock(mt); 53124a1cce3SDavid Greenman vm_page_activate(mt); 5323c4a2440SAlan Cox vm_page_unlock(mt); 533126d6082SKonstantin Belousov if (eio != NULL && i >= mreq && i - mreq < runlen) 534126d6082SKonstantin Belousov *eio = TRUE; 53526f9a767SRodney W. Grimes break; 53626f9a767SRodney W. Grimes case VM_PAGER_AGAIN: 5371e8a675cSKonstantin Belousov if (i >= mreq && i - mreq < runlen) 5381e8a675cSKonstantin Belousov runlen = i - mreq; 53926f9a767SRodney W. Grimes break; 54026f9a767SRodney W. Grimes } 54126f9a767SRodney W. Grimes 54226f9a767SRodney W. Grimes /* 5430d94caffSDavid Greenman * If the operation is still going, leave the page busy to 5440d94caffSDavid Greenman * block all other accesses. Also, leave the paging in 5450d94caffSDavid Greenman * progress indicator set so that we don't attempt an object 5460d94caffSDavid Greenman * collapse. 54726f9a767SRodney W. Grimes */ 54826f9a767SRodney W. Grimes if (pageout_status[i] != VM_PAGER_PEND) { 549f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 550e69763a3SDoug Rabson vm_page_io_finish(mt); 5513c4a2440SAlan Cox if (vm_page_count_severe()) { 5523c4a2440SAlan Cox vm_page_lock(mt); 5539ea8d1a6SAlan Cox vm_page_try_to_cache(mt); 5542965a453SKip Macy vm_page_unlock(mt); 55526f9a767SRodney W. Grimes } 5563c4a2440SAlan Cox } 5573c4a2440SAlan Cox } 5581e8a675cSKonstantin Belousov if (prunlen != NULL) 5591e8a675cSKonstantin Belousov *prunlen = runlen; 5603c4a2440SAlan Cox return (numpagedout); 56126f9a767SRodney W. Grimes } 56226f9a767SRodney W. Grimes 56385eeca35SAlan Cox static boolean_t 56485eeca35SAlan Cox vm_pageout_launder(int queue, int tries, vm_paddr_t low, vm_paddr_t high) 56585eeca35SAlan Cox { 56685eeca35SAlan Cox struct mount *mp; 567*8d220203SAlan Cox struct vm_pagequeue *pq; 56885eeca35SAlan Cox struct vnode *vp; 56985eeca35SAlan Cox vm_object_t object; 57085eeca35SAlan Cox vm_paddr_t pa; 57185eeca35SAlan Cox vm_page_t m, m_tmp, next; 57285eeca35SAlan Cox 573*8d220203SAlan Cox pq = &vm_pagequeues[queue]; 574*8d220203SAlan Cox vm_pagequeue_lock(pq); 575*8d220203SAlan Cox TAILQ_FOREACH_SAFE(m, &pq->pq_pl, pageq, next) { 57685eeca35SAlan Cox KASSERT(m->queue == queue, 57785eeca35SAlan Cox ("vm_pageout_launder: page %p's queue is not %d", m, 57885eeca35SAlan Cox queue)); 57985eeca35SAlan Cox if ((m->flags & PG_MARKER) != 0) 58085eeca35SAlan Cox continue; 58185eeca35SAlan Cox pa = VM_PAGE_TO_PHYS(m); 58285eeca35SAlan Cox if (pa < low || pa + PAGE_SIZE > high) 58385eeca35SAlan Cox continue; 58485eeca35SAlan Cox if (!vm_pageout_page_lock(m, &next) || m->hold_count != 0) { 58585eeca35SAlan Cox vm_page_unlock(m); 58685eeca35SAlan Cox continue; 58785eeca35SAlan Cox } 58885eeca35SAlan Cox object = m->object; 58918f55b01SAlan Cox if ((!VM_OBJECT_TRYLOCK(object) && 59085eeca35SAlan Cox (!vm_pageout_fallback_object_lock(m, &next) || 59118f55b01SAlan Cox m->hold_count != 0)) || (m->oflags & VPO_BUSY) != 0 || 59218f55b01SAlan Cox m->busy != 0) { 59385eeca35SAlan Cox vm_page_unlock(m); 59485eeca35SAlan Cox VM_OBJECT_UNLOCK(object); 59585eeca35SAlan Cox continue; 59685eeca35SAlan Cox } 59785eeca35SAlan Cox vm_page_test_dirty(m); 5989fc4739dSAlan Cox if (m->dirty == 0 && object->ref_count != 0) 59985eeca35SAlan Cox pmap_remove_all(m); 60085eeca35SAlan Cox if (m->dirty != 0) { 60185eeca35SAlan Cox vm_page_unlock(m); 60285eeca35SAlan Cox if (tries == 0 || (object->flags & OBJ_DEAD) != 0) { 60385eeca35SAlan Cox VM_OBJECT_UNLOCK(object); 60485eeca35SAlan Cox continue; 60585eeca35SAlan Cox } 60685eeca35SAlan Cox if (object->type == OBJT_VNODE) { 607*8d220203SAlan Cox vm_pagequeue_unlock(pq); 60885eeca35SAlan Cox vp = object->handle; 60985eeca35SAlan Cox vm_object_reference_locked(object); 61085eeca35SAlan Cox VM_OBJECT_UNLOCK(object); 61185eeca35SAlan Cox (void)vn_start_write(vp, &mp, V_WAIT); 61285eeca35SAlan Cox vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 61385eeca35SAlan Cox VM_OBJECT_LOCK(object); 61485eeca35SAlan Cox vm_object_page_clean(object, 0, 0, OBJPC_SYNC); 61585eeca35SAlan Cox VM_OBJECT_UNLOCK(object); 61685eeca35SAlan Cox VOP_UNLOCK(vp, 0); 61785eeca35SAlan Cox vm_object_deallocate(object); 61885eeca35SAlan Cox vn_finished_write(mp); 61985eeca35SAlan Cox return (TRUE); 62085eeca35SAlan Cox } else if (object->type == OBJT_SWAP || 62185eeca35SAlan Cox object->type == OBJT_DEFAULT) { 622*8d220203SAlan Cox vm_pagequeue_unlock(pq); 62385eeca35SAlan Cox m_tmp = m; 62485eeca35SAlan Cox vm_pageout_flush(&m_tmp, 1, VM_PAGER_PUT_SYNC, 62585eeca35SAlan Cox 0, NULL, NULL); 62685eeca35SAlan Cox VM_OBJECT_UNLOCK(object); 62785eeca35SAlan Cox return (TRUE); 62885eeca35SAlan Cox } 62985eeca35SAlan Cox } else { 630*8d220203SAlan Cox /* 631*8d220203SAlan Cox * Dequeue here to prevent lock recursion in 632*8d220203SAlan Cox * vm_page_cache(). 633*8d220203SAlan Cox */ 634*8d220203SAlan Cox vm_page_dequeue_locked(m); 63585eeca35SAlan Cox vm_page_cache(m); 63685eeca35SAlan Cox vm_page_unlock(m); 63785eeca35SAlan Cox } 63885eeca35SAlan Cox VM_OBJECT_UNLOCK(object); 63985eeca35SAlan Cox } 640*8d220203SAlan Cox vm_pagequeue_unlock(pq); 64185eeca35SAlan Cox return (FALSE); 64285eeca35SAlan Cox } 64385eeca35SAlan Cox 64485eeca35SAlan Cox /* 64585eeca35SAlan Cox * Increase the number of cached pages. The specified value, "tries", 64685eeca35SAlan Cox * determines which categories of pages are cached: 64785eeca35SAlan Cox * 64885eeca35SAlan Cox * 0: All clean, inactive pages within the specified physical address range 64985eeca35SAlan Cox * are cached. Will not sleep. 65085eeca35SAlan Cox * 1: The vm_lowmem handlers are called. All inactive pages within 65185eeca35SAlan Cox * the specified physical address range are cached. May sleep. 65285eeca35SAlan Cox * 2: The vm_lowmem handlers are called. All inactive and active pages 65385eeca35SAlan Cox * within the specified physical address range are cached. May sleep. 65485eeca35SAlan Cox */ 65585eeca35SAlan Cox void 65685eeca35SAlan Cox vm_pageout_grow_cache(int tries, vm_paddr_t low, vm_paddr_t high) 65785eeca35SAlan Cox { 65885eeca35SAlan Cox int actl, actmax, inactl, inactmax; 65985eeca35SAlan Cox 66085eeca35SAlan Cox if (tries > 0) { 66185eeca35SAlan Cox /* 66285eeca35SAlan Cox * Decrease registered cache sizes. The vm_lowmem handlers 66385eeca35SAlan Cox * may acquire locks and/or sleep, so they can only be invoked 66485eeca35SAlan Cox * when "tries" is greater than zero. 66585eeca35SAlan Cox */ 66685eeca35SAlan Cox EVENTHANDLER_INVOKE(vm_lowmem, 0); 66785eeca35SAlan Cox 66885eeca35SAlan Cox /* 66985eeca35SAlan Cox * We do this explicitly after the caches have been drained 67085eeca35SAlan Cox * above. 67185eeca35SAlan Cox */ 67285eeca35SAlan Cox uma_reclaim(); 67385eeca35SAlan Cox } 67485eeca35SAlan Cox inactl = 0; 67585eeca35SAlan Cox inactmax = cnt.v_inactive_count; 67685eeca35SAlan Cox actl = 0; 67785eeca35SAlan Cox actmax = tries < 2 ? 0 : cnt.v_active_count; 67885eeca35SAlan Cox again: 67985eeca35SAlan Cox if (inactl < inactmax && vm_pageout_launder(PQ_INACTIVE, tries, low, 68085eeca35SAlan Cox high)) { 68185eeca35SAlan Cox inactl++; 68285eeca35SAlan Cox goto again; 68385eeca35SAlan Cox } 68485eeca35SAlan Cox if (actl < actmax && vm_pageout_launder(PQ_ACTIVE, tries, low, high)) { 68585eeca35SAlan Cox actl++; 68685eeca35SAlan Cox goto again; 68785eeca35SAlan Cox } 68885eeca35SAlan Cox } 68985eeca35SAlan Cox 69038efa82bSJohn Dyson #if !defined(NO_SWAPPING) 69126f9a767SRodney W. Grimes /* 69226f9a767SRodney W. Grimes * vm_pageout_object_deactivate_pages 69326f9a767SRodney W. Grimes * 694ce186587SAlan Cox * Deactivate enough pages to satisfy the inactive target 695ce186587SAlan Cox * requirements. 69626f9a767SRodney W. Grimes * 69726f9a767SRodney W. Grimes * The object and map must be locked. 69826f9a767SRodney W. Grimes */ 69938efa82bSJohn Dyson static void 700ce186587SAlan Cox vm_pageout_object_deactivate_pages(pmap_t pmap, vm_object_t first_object, 701ce186587SAlan Cox long desired) 70226f9a767SRodney W. Grimes { 703ecf6279fSAlan Cox vm_object_t backing_object, object; 704ce186587SAlan Cox vm_page_t p; 70582bfb965SAlan Cox int actcount, remove_mode; 70626f9a767SRodney W. Grimes 707ecf6279fSAlan Cox VM_OBJECT_LOCK_ASSERT(first_object, MA_OWNED); 70801381811SJohn Baldwin if (first_object->type == OBJT_DEVICE || 70982bfb965SAlan Cox first_object->type == OBJT_SG) 71038efa82bSJohn Dyson return; 711ecf6279fSAlan Cox for (object = first_object;; object = backing_object) { 712ecf6279fSAlan Cox if (pmap_resident_count(pmap) <= desired) 713ecf6279fSAlan Cox goto unlock_return; 714447fe2a4SAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 71582bfb965SAlan Cox if (object->type == OBJT_PHYS || object->paging_in_progress) 716ecf6279fSAlan Cox goto unlock_return; 71726f9a767SRodney W. Grimes 71885b1dc89SAlan Cox remove_mode = 0; 71938efa82bSJohn Dyson if (object->shadow_count > 1) 72038efa82bSJohn Dyson remove_mode = 1; 72126f9a767SRodney W. Grimes /* 722ce186587SAlan Cox * Scan the object's entire memory queue. 72326f9a767SRodney W. Grimes */ 724ce186587SAlan Cox TAILQ_FOREACH(p, &object->memq, listq) { 725447fe2a4SAlan Cox if (pmap_resident_count(pmap) <= desired) 726447fe2a4SAlan Cox goto unlock_return; 727ce186587SAlan Cox if ((p->oflags & VPO_BUSY) != 0 || p->busy != 0) 728447fe2a4SAlan Cox continue; 729ce186587SAlan Cox PCPU_INC(cnt.v_pdpages); 7302965a453SKip Macy vm_page_lock(p); 731ce186587SAlan Cox if (p->wire_count != 0 || p->hold_count != 0 || 732ecf6279fSAlan Cox !pmap_page_exists_quick(pmap, p)) { 7332965a453SKip Macy vm_page_unlock(p); 7340d94caffSDavid Greenman continue; 7350d94caffSDavid Greenman } 7360385347cSPeter Wemm actcount = pmap_ts_referenced(p); 7373407fefeSKonstantin Belousov if ((p->aflags & PGA_REFERENCED) != 0) { 738ce186587SAlan Cox if (actcount == 0) 7397e006499SJohn Dyson actcount = 1; 7403407fefeSKonstantin Belousov vm_page_aflag_clear(p, PGA_REFERENCED); 741ef743ce6SJohn Dyson } 742ce186587SAlan Cox if (p->queue != PQ_ACTIVE && actcount != 0) { 743ef743ce6SJohn Dyson vm_page_activate(p); 7447e006499SJohn Dyson p->act_count += actcount; 745c8c4b40cSJohn Dyson } else if (p->queue == PQ_ACTIVE) { 746ce186587SAlan Cox if (actcount == 0) { 747ce186587SAlan Cox p->act_count -= min(p->act_count, 748ce186587SAlan Cox ACT_DECLINE); 749ce186587SAlan Cox if (!remove_mode && 750ce186587SAlan Cox (vm_pageout_algorithm || 751ce186587SAlan Cox p->act_count == 0)) { 7524fec79beSAlan Cox pmap_remove_all(p); 75326f9a767SRodney W. Grimes vm_page_deactivate(p); 754*8d220203SAlan Cox } else 755*8d220203SAlan Cox vm_page_requeue(p); 756c8c4b40cSJohn Dyson } else { 757eaf13dd7SJohn Dyson vm_page_activate(p); 758ce186587SAlan Cox if (p->act_count < ACT_MAX - 759ce186587SAlan Cox ACT_ADVANCE) 76038efa82bSJohn Dyson p->act_count += ACT_ADVANCE; 761*8d220203SAlan Cox vm_page_requeue(p); 762ce186587SAlan Cox } 763ce186587SAlan Cox } else if (p->queue == PQ_INACTIVE) 764ce186587SAlan Cox pmap_remove_all(p); 7652965a453SKip Macy vm_page_unlock(p); 76626f9a767SRodney W. Grimes } 767ecf6279fSAlan Cox if ((backing_object = object->backing_object) == NULL) 768ecf6279fSAlan Cox goto unlock_return; 769ecf6279fSAlan Cox VM_OBJECT_LOCK(backing_object); 770ecf6279fSAlan Cox if (object != first_object) 771ecf6279fSAlan Cox VM_OBJECT_UNLOCK(object); 77238efa82bSJohn Dyson } 773ecf6279fSAlan Cox unlock_return: 774ecf6279fSAlan Cox if (object != first_object) 775ecf6279fSAlan Cox VM_OBJECT_UNLOCK(object); 77626f9a767SRodney W. Grimes } 77726f9a767SRodney W. Grimes 77826f9a767SRodney W. Grimes /* 77926f9a767SRodney W. Grimes * deactivate some number of pages in a map, try to do it fairly, but 78026f9a767SRodney W. Grimes * that is really hard to do. 78126f9a767SRodney W. Grimes */ 782cd41fc12SDavid Greenman static void 78338efa82bSJohn Dyson vm_pageout_map_deactivate_pages(map, desired) 78426f9a767SRodney W. Grimes vm_map_t map; 785ecf6279fSAlan Cox long desired; 78626f9a767SRodney W. Grimes { 78726f9a767SRodney W. Grimes vm_map_entry_t tmpe; 78838efa82bSJohn Dyson vm_object_t obj, bigobj; 78930105b9eSTor Egge int nothingwired; 7900d94caffSDavid Greenman 791d974f03cSAlan Cox if (!vm_map_trylock(map)) 79226f9a767SRodney W. Grimes return; 79338efa82bSJohn Dyson 79438efa82bSJohn Dyson bigobj = NULL; 79530105b9eSTor Egge nothingwired = TRUE; 79638efa82bSJohn Dyson 79738efa82bSJohn Dyson /* 79838efa82bSJohn Dyson * first, search out the biggest object, and try to free pages from 79938efa82bSJohn Dyson * that. 80038efa82bSJohn Dyson */ 80126f9a767SRodney W. Grimes tmpe = map->header.next; 80238efa82bSJohn Dyson while (tmpe != &map->header) { 8039fdfe602SMatthew Dillon if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 80438efa82bSJohn Dyson obj = tmpe->object.vm_object; 8050774dfb3SAlan Cox if (obj != NULL && VM_OBJECT_TRYLOCK(obj)) { 8060774dfb3SAlan Cox if (obj->shadow_count <= 1 && 8070774dfb3SAlan Cox (bigobj == NULL || 8080774dfb3SAlan Cox bigobj->resident_page_count < obj->resident_page_count)) { 8090774dfb3SAlan Cox if (bigobj != NULL) 8100774dfb3SAlan Cox VM_OBJECT_UNLOCK(bigobj); 81138efa82bSJohn Dyson bigobj = obj; 8120774dfb3SAlan Cox } else 8130774dfb3SAlan Cox VM_OBJECT_UNLOCK(obj); 81438efa82bSJohn Dyson } 81538efa82bSJohn Dyson } 81630105b9eSTor Egge if (tmpe->wired_count > 0) 81730105b9eSTor Egge nothingwired = FALSE; 81838efa82bSJohn Dyson tmpe = tmpe->next; 81938efa82bSJohn Dyson } 82038efa82bSJohn Dyson 8210774dfb3SAlan Cox if (bigobj != NULL) { 822ecf6279fSAlan Cox vm_pageout_object_deactivate_pages(map->pmap, bigobj, desired); 8230774dfb3SAlan Cox VM_OBJECT_UNLOCK(bigobj); 8240774dfb3SAlan Cox } 82538efa82bSJohn Dyson /* 82638efa82bSJohn Dyson * Next, hunt around for other pages to deactivate. We actually 82738efa82bSJohn Dyson * do this search sort of wrong -- .text first is not the best idea. 82838efa82bSJohn Dyson */ 82938efa82bSJohn Dyson tmpe = map->header.next; 83038efa82bSJohn Dyson while (tmpe != &map->header) { 831b1028ad1SLuoqi Chen if (pmap_resident_count(vm_map_pmap(map)) <= desired) 83238efa82bSJohn Dyson break; 8339fdfe602SMatthew Dillon if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 83438efa82bSJohn Dyson obj = tmpe->object.vm_object; 8350774dfb3SAlan Cox if (obj != NULL) { 8360774dfb3SAlan Cox VM_OBJECT_LOCK(obj); 837ecf6279fSAlan Cox vm_pageout_object_deactivate_pages(map->pmap, obj, desired); 8380774dfb3SAlan Cox VM_OBJECT_UNLOCK(obj); 8390774dfb3SAlan Cox } 84038efa82bSJohn Dyson } 84126f9a767SRodney W. Grimes tmpe = tmpe->next; 84238857e7fSAlan Cox } 84338efa82bSJohn Dyson 84438efa82bSJohn Dyson /* 84538efa82bSJohn Dyson * Remove all mappings if a process is swapped out, this will free page 84638efa82bSJohn Dyson * table pages. 84738efa82bSJohn Dyson */ 84838857e7fSAlan Cox if (desired == 0 && nothingwired) { 8498d01a3b2SNathan Whitehorn pmap_remove(vm_map_pmap(map), vm_map_min(map), 8508d01a3b2SNathan Whitehorn vm_map_max(map)); 85138857e7fSAlan Cox } 85238efa82bSJohn Dyson vm_map_unlock(map); 85326f9a767SRodney W. Grimes } 854a1287949SEivind Eklund #endif /* !defined(NO_SWAPPING) */ 855df8bae1dSRodney W. Grimes 8561c7c3c6aSMatthew Dillon /* 857df8bae1dSRodney W. Grimes * vm_pageout_scan does the dirty work for the pageout daemon. 858df8bae1dSRodney W. Grimes */ 8592b6b0df7SMatthew Dillon static void 8602b6b0df7SMatthew Dillon vm_pageout_scan(int pass) 861df8bae1dSRodney W. Grimes { 862502ba6e4SJohn Dyson vm_page_t m, next; 863936524aaSMatthew Dillon struct vm_page marker; 864*8d220203SAlan Cox struct vm_pagequeue *pq; 8651c7c3c6aSMatthew Dillon int page_shortage, maxscan, pcount; 8662cba1ccdSAlan Cox int addl_page_shortage; 867df8bae1dSRodney W. Grimes vm_object_t object; 8682446e4f0SAlan Cox int actcount; 869f6b04d2bSDavid Greenman int vnodes_skipped = 0; 8702b6b0df7SMatthew Dillon int maxlaunder; 87148cc2fc7SKonstantin Belousov boolean_t queues_locked; 8720d94caffSDavid Greenman 873*8d220203SAlan Cox vm_pageout_init_marker(&marker, PQ_INACTIVE); 874*8d220203SAlan Cox 875df8bae1dSRodney W. Grimes /* 876855a310fSJeff Roberson * Decrease registered cache sizes. 877855a310fSJeff Roberson */ 878855a310fSJeff Roberson EVENTHANDLER_INVOKE(vm_lowmem, 0); 879855a310fSJeff Roberson /* 880855a310fSJeff Roberson * We do this explicitly after the caches have been drained above. 881855a310fSJeff Roberson */ 882855a310fSJeff Roberson uma_reclaim(); 8835985940eSJohn Dyson 884311e34e2SKonstantin Belousov /* 88596240c89SEitan Adler * The addl_page_shortage is the number of temporarily 886311e34e2SKonstantin Belousov * stuck pages in the inactive queue. In other words, the 887311e34e2SKonstantin Belousov * number of pages from cnt.v_inactive_count that should be 888311e34e2SKonstantin Belousov * discounted in setting the target for the active queue scan. 889311e34e2SKonstantin Belousov */ 8902cba1ccdSAlan Cox addl_page_shortage = atomic_readandclear_int(&vm_pageout_deficit); 891b182ec9eSJohn Dyson 8921c7c3c6aSMatthew Dillon /* 8931c7c3c6aSMatthew Dillon * Calculate the number of pages we want to either free or move 8942b6b0df7SMatthew Dillon * to the cache. 8951c7c3c6aSMatthew Dillon */ 8962cba1ccdSAlan Cox page_shortage = vm_paging_target() + addl_page_shortage; 8971c7c3c6aSMatthew Dillon 898936524aaSMatthew Dillon /* 8992b6b0df7SMatthew Dillon * maxlaunder limits the number of dirty pages we flush per scan. 9002b6b0df7SMatthew Dillon * For most systems a smaller value (16 or 32) is more robust under 9012b6b0df7SMatthew Dillon * extreme memory and disk pressure because any unnecessary writes 9022b6b0df7SMatthew Dillon * to disk can result in extreme performance degredation. However, 9032b6b0df7SMatthew Dillon * systems with excessive dirty pages (especially when MAP_NOSYNC is 9042b6b0df7SMatthew Dillon * used) will die horribly with limited laundering. If the pageout 9052b6b0df7SMatthew Dillon * daemon cannot clean enough pages in the first pass, we let it go 9062b6b0df7SMatthew Dillon * all out in succeeding passes. 9071c7c3c6aSMatthew Dillon */ 9082b6b0df7SMatthew Dillon if ((maxlaunder = vm_max_launder) <= 1) 9092b6b0df7SMatthew Dillon maxlaunder = 1; 9102b6b0df7SMatthew Dillon if (pass) 9112b6b0df7SMatthew Dillon maxlaunder = 10000; 912*8d220203SAlan Cox 9132feb50bfSAttilio Rao maxscan = cnt.v_inactive_count; 9146d03d577SMatthew Dillon 915*8d220203SAlan Cox /* 916*8d220203SAlan Cox * Start scanning the inactive queue for pages we can move to the 917*8d220203SAlan Cox * cache or free. The scan will stop when the target is reached or 918*8d220203SAlan Cox * we have scanned the entire inactive queue. Note that m->act_count 919*8d220203SAlan Cox * is not used to form decisions for the inactive queue, only for the 920*8d220203SAlan Cox * active queue. 921*8d220203SAlan Cox */ 922*8d220203SAlan Cox pq = &vm_pagequeues[PQ_INACTIVE]; 923*8d220203SAlan Cox vm_pagequeue_lock(pq); 924*8d220203SAlan Cox queues_locked = TRUE; 925*8d220203SAlan Cox for (m = TAILQ_FIRST(&pq->pq_pl); 9261c7c3c6aSMatthew Dillon m != NULL && maxscan-- > 0 && page_shortage > 0; 927e929c00dSKirk McKusick m = next) { 928*8d220203SAlan Cox vm_pagequeue_assert_locked(pq); 92948cc2fc7SKonstantin Belousov KASSERT(queues_locked, ("unlocked queues")); 930d4961bcbSKonstantin Belousov KASSERT(m->queue == PQ_INACTIVE, ("Inactive queue %p", m)); 931df8bae1dSRodney W. Grimes 932*8d220203SAlan Cox PCPU_INC(cnt.v_pdpages); 933b18bfc3dSJohn Dyson next = TAILQ_NEXT(m, pageq); 934df8bae1dSRodney W. Grimes 935936524aaSMatthew Dillon /* 936936524aaSMatthew Dillon * skip marker pages 937936524aaSMatthew Dillon */ 938936524aaSMatthew Dillon if (m->flags & PG_MARKER) 939936524aaSMatthew Dillon continue; 940936524aaSMatthew Dillon 9417900f95dSKonstantin Belousov KASSERT((m->flags & PG_FICTITIOUS) == 0, 9427900f95dSKonstantin Belousov ("Fictitious page %p cannot be in inactive queue", m)); 9437900f95dSKonstantin Belousov KASSERT((m->oflags & VPO_UNMANAGED) == 0, 9447900f95dSKonstantin Belousov ("Unmanaged page %p cannot be in inactive queue", m)); 9457900f95dSKonstantin Belousov 9468c616246SKonstantin Belousov /* 947311e34e2SKonstantin Belousov * The page or object lock acquisitions fail if the 948311e34e2SKonstantin Belousov * page was removed from the queue or moved to a 949311e34e2SKonstantin Belousov * different position within the queue. In either 950311e34e2SKonstantin Belousov * case, addl_page_shortage should not be incremented. 9518c616246SKonstantin Belousov */ 9528c616246SKonstantin Belousov if (!vm_pageout_page_lock(m, &next)) { 9538c616246SKonstantin Belousov vm_page_unlock(m); 954b182ec9eSJohn Dyson continue; 955df8bae1dSRodney W. Grimes } 9569ee2165fSAlan Cox object = m->object; 9578dbca793STor Egge if (!VM_OBJECT_TRYLOCK(object) && 958311e34e2SKonstantin Belousov !vm_pageout_fallback_object_lock(m, &next)) { 9592965a453SKip Macy vm_page_unlock(m); 960311e34e2SKonstantin Belousov VM_OBJECT_UNLOCK(object); 96134d9e6fdSAlan Cox continue; 96234d9e6fdSAlan Cox } 963311e34e2SKonstantin Belousov 964311e34e2SKonstantin Belousov /* 965311e34e2SKonstantin Belousov * Don't mess with busy pages, keep them at at the 966311e34e2SKonstantin Belousov * front of the queue, most likely they are being 967311e34e2SKonstantin Belousov * paged out. Increment addl_page_shortage for busy 968311e34e2SKonstantin Belousov * pages, because they may leave the inactive queue 969311e34e2SKonstantin Belousov * shortly after page scan is finished. 970311e34e2SKonstantin Belousov */ 971311e34e2SKonstantin Belousov if (m->busy != 0 || (m->oflags & VPO_BUSY) != 0) { 9722965a453SKip Macy vm_page_unlock(m); 97334d9e6fdSAlan Cox VM_OBJECT_UNLOCK(object); 974b182ec9eSJohn Dyson addl_page_shortage++; 97526f9a767SRodney W. Grimes continue; 97626f9a767SRodney W. Grimes } 977bd7e5f99SJohn Dyson 9787e006499SJohn Dyson /* 979*8d220203SAlan Cox * We unlock the inactive page queue, invalidating the 98048cc2fc7SKonstantin Belousov * 'next' pointer. Use our marker to remember our 98148cc2fc7SKonstantin Belousov * place. 98248cc2fc7SKonstantin Belousov */ 983*8d220203SAlan Cox TAILQ_INSERT_AFTER(&pq->pq_pl, m, &marker, pageq); 984*8d220203SAlan Cox vm_pagequeue_unlock(pq); 98548cc2fc7SKonstantin Belousov queues_locked = FALSE; 98648cc2fc7SKonstantin Belousov 98748cc2fc7SKonstantin Belousov /* 9881c7c3c6aSMatthew Dillon * If the object is not being used, we ignore previous 9891c7c3c6aSMatthew Dillon * references. 9907e006499SJohn Dyson */ 99134d9e6fdSAlan Cox if (object->ref_count == 0) { 9923407fefeSKonstantin Belousov vm_page_aflag_clear(m, PGA_REFERENCED); 99347916d0cSAlan Cox KASSERT(!pmap_page_is_mapped(m), 99447916d0cSAlan Cox ("vm_pageout_scan: page %p is mapped", m)); 9957e006499SJohn Dyson 9967e006499SJohn Dyson /* 9971c7c3c6aSMatthew Dillon * Otherwise, if the page has been referenced while in the 9981c7c3c6aSMatthew Dillon * inactive queue, we bump the "activation count" upwards, 9991c7c3c6aSMatthew Dillon * making it less likely that the page will be added back to 10001c7c3c6aSMatthew Dillon * the inactive queue prematurely again. Here we check the 10011c7c3c6aSMatthew Dillon * page tables (or emulated bits, if any), given the upper 10021c7c3c6aSMatthew Dillon * level VM system not knowing anything about existing 10031c7c3c6aSMatthew Dillon * references. 10047e006499SJohn Dyson */ 10055d10ef20SKonstantin Belousov } else if ((m->aflags & PGA_REFERENCED) == 0 && 10065d10ef20SKonstantin Belousov (actcount = pmap_ts_referenced(m)) != 0) { 1007ef743ce6SJohn Dyson vm_page_activate(m); 10082965a453SKip Macy vm_page_unlock(m); 10094c6a2e7aSAlan Cox m->act_count += actcount + ACT_ADVANCE; 10104c6a2e7aSAlan Cox VM_OBJECT_UNLOCK(object); 101148cc2fc7SKonstantin Belousov goto relock_queues; 10122fe6e4d7SDavid Greenman } 1013ef743ce6SJohn Dyson 10147e006499SJohn Dyson /* 10151c7c3c6aSMatthew Dillon * If the upper level VM system knows about any page 10161c7c3c6aSMatthew Dillon * references, we activate the page. We also set the 10171c7c3c6aSMatthew Dillon * "activation count" higher than normal so that we will less 10181c7c3c6aSMatthew Dillon * likely place pages back onto the inactive queue again. 10197e006499SJohn Dyson */ 10203407fefeSKonstantin Belousov if ((m->aflags & PGA_REFERENCED) != 0) { 10213407fefeSKonstantin Belousov vm_page_aflag_clear(m, PGA_REFERENCED); 10220385347cSPeter Wemm actcount = pmap_ts_referenced(m); 102326f9a767SRodney W. Grimes vm_page_activate(m); 10242965a453SKip Macy vm_page_unlock(m); 10254c6a2e7aSAlan Cox m->act_count += actcount + ACT_ADVANCE + 1; 10264c6a2e7aSAlan Cox VM_OBJECT_UNLOCK(object); 102748cc2fc7SKonstantin Belousov goto relock_queues; 10280d94caffSDavid Greenman } 102967bf6868SJohn Dyson 1030311e34e2SKonstantin Belousov if (m->hold_count != 0) { 1031311e34e2SKonstantin Belousov vm_page_unlock(m); 1032311e34e2SKonstantin Belousov VM_OBJECT_UNLOCK(object); 1033311e34e2SKonstantin Belousov 1034311e34e2SKonstantin Belousov /* 1035311e34e2SKonstantin Belousov * Held pages are essentially stuck in the 1036311e34e2SKonstantin Belousov * queue. So, they ought to be discounted 1037311e34e2SKonstantin Belousov * from cnt.v_inactive_count. See the 1038311e34e2SKonstantin Belousov * calculation of the page_shortage for the 1039311e34e2SKonstantin Belousov * loop over the active queue below. 1040311e34e2SKonstantin Belousov */ 1041311e34e2SKonstantin Belousov addl_page_shortage++; 1042311e34e2SKonstantin Belousov goto relock_queues; 1043311e34e2SKonstantin Belousov } 1044311e34e2SKonstantin Belousov 10457e006499SJohn Dyson /* 10469fc4739dSAlan Cox * If the page appears to be clean at the machine-independent 10479fc4739dSAlan Cox * layer, then remove all of its mappings from the pmap in 10489fc4739dSAlan Cox * anticipation of placing it onto the cache queue. If, 10499fc4739dSAlan Cox * however, any of the page's mappings allow write access, 10509fc4739dSAlan Cox * then the page may still be modified until the last of those 10519fc4739dSAlan Cox * mappings are removed. 10527e006499SJohn Dyson */ 10539fc4739dSAlan Cox vm_page_test_dirty(m); 10549fc4739dSAlan Cox if (m->dirty == 0 && object->ref_count != 0) 1055b78ddb0bSAlan Cox pmap_remove_all(m); 1056dcbcd518SBruce Evans 10576989c456SAlan Cox if (m->valid == 0) { 10587e006499SJohn Dyson /* 10597e006499SJohn Dyson * Invalid pages can be easily freed 10607e006499SJohn Dyson */ 10616989c456SAlan Cox vm_page_free(m); 106248cc2fc7SKonstantin Belousov PCPU_INC(cnt.v_dfree); 10631c7c3c6aSMatthew Dillon --page_shortage; 1064bd7e5f99SJohn Dyson } else if (m->dirty == 0) { 10656989c456SAlan Cox /* 10666989c456SAlan Cox * Clean pages can be placed onto the cache queue. 10676989c456SAlan Cox * This effectively frees them. 10686989c456SAlan Cox */ 1069bd7e5f99SJohn Dyson vm_page_cache(m); 10701c7c3c6aSMatthew Dillon --page_shortage; 10712b6b0df7SMatthew Dillon } else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) { 10727e006499SJohn Dyson /* 10732b6b0df7SMatthew Dillon * Dirty pages need to be paged out, but flushing 10742b6b0df7SMatthew Dillon * a page is extremely expensive verses freeing 10752b6b0df7SMatthew Dillon * a clean page. Rather then artificially limiting 10762b6b0df7SMatthew Dillon * the number of pages we can flush, we instead give 10772b6b0df7SMatthew Dillon * dirty pages extra priority on the inactive queue 10782b6b0df7SMatthew Dillon * by forcing them to be cycled through the queue 10792b6b0df7SMatthew Dillon * twice before being flushed, after which the 10802b6b0df7SMatthew Dillon * (now clean) page will cycle through once more 10812b6b0df7SMatthew Dillon * before being freed. This significantly extends 10822b6b0df7SMatthew Dillon * the thrash point for a heavily loaded machine. 10837e006499SJohn Dyson */ 10843407fefeSKonstantin Belousov m->flags |= PG_WINATCFLS; 1085*8d220203SAlan Cox vm_pagequeue_lock(pq); 108648cc2fc7SKonstantin Belousov queues_locked = TRUE; 1087*8d220203SAlan Cox vm_page_requeue_locked(m); 10880d94caffSDavid Greenman } else if (maxlaunder > 0) { 10892b6b0df7SMatthew Dillon /* 10902b6b0df7SMatthew Dillon * We always want to try to flush some dirty pages if 10912b6b0df7SMatthew Dillon * we encounter them, to keep the system stable. 10922b6b0df7SMatthew Dillon * Normally this number is small, but under extreme 10932b6b0df7SMatthew Dillon * pressure where there are insufficient clean pages 10942b6b0df7SMatthew Dillon * on the inactive queue, we may have to go all out. 10952b6b0df7SMatthew Dillon */ 10965050aa86SKonstantin Belousov int swap_pageouts_ok; 1097f6b04d2bSDavid Greenman struct vnode *vp = NULL; 109814137dc0SAlan Cox struct mount *mp = NULL; 10990d94caffSDavid Greenman 110012ac6a1dSJohn Dyson if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) { 110112ac6a1dSJohn Dyson swap_pageouts_ok = 1; 110212ac6a1dSJohn Dyson } else { 110312ac6a1dSJohn Dyson swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts); 110412ac6a1dSJohn Dyson swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts && 110590ecac61SMatthew Dillon vm_page_count_min()); 110612ac6a1dSJohn Dyson 110712ac6a1dSJohn Dyson } 110870111b90SJohn Dyson 110970111b90SJohn Dyson /* 11101c7c3c6aSMatthew Dillon * We don't bother paging objects that are "dead". 11111c7c3c6aSMatthew Dillon * Those objects are in a "rundown" state. 111270111b90SJohn Dyson */ 111370111b90SJohn Dyson if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) { 1114*8d220203SAlan Cox vm_pagequeue_lock(pq); 11152965a453SKip Macy vm_page_unlock(m); 11163562af12SAlan Cox VM_OBJECT_UNLOCK(object); 111748cc2fc7SKonstantin Belousov queues_locked = TRUE; 1118*8d220203SAlan Cox vm_page_requeue_locked(m); 111948cc2fc7SKonstantin Belousov goto relock_queues; 112012ac6a1dSJohn Dyson } 112112ac6a1dSJohn Dyson 11221c7c3c6aSMatthew Dillon /* 11232b6b0df7SMatthew Dillon * The object is already known NOT to be dead. It 11242b6b0df7SMatthew Dillon * is possible for the vget() to block the whole 11252b6b0df7SMatthew Dillon * pageout daemon, but the new low-memory handling 11262b6b0df7SMatthew Dillon * code should prevent it. 11271c7c3c6aSMatthew Dillon * 11282b6b0df7SMatthew Dillon * The previous code skipped locked vnodes and, worse, 11292b6b0df7SMatthew Dillon * reordered pages in the queue. This results in 11302b6b0df7SMatthew Dillon * completely non-deterministic operation and, on a 11312b6b0df7SMatthew Dillon * busy system, can lead to extremely non-optimal 11322b6b0df7SMatthew Dillon * pageouts. For example, it can cause clean pages 11332b6b0df7SMatthew Dillon * to be freed and dirty pages to be moved to the end 11342b6b0df7SMatthew Dillon * of the queue. Since dirty pages are also moved to 11352b6b0df7SMatthew Dillon * the end of the queue once-cleaned, this gives 11362b6b0df7SMatthew Dillon * way too large a weighting to defering the freeing 11372b6b0df7SMatthew Dillon * of dirty pages. 11381c7c3c6aSMatthew Dillon * 113923b59018SMatthew Dillon * We can't wait forever for the vnode lock, we might 114023b59018SMatthew Dillon * deadlock due to a vn_read() getting stuck in 114123b59018SMatthew Dillon * vm_wait while holding this vnode. We skip the 114223b59018SMatthew Dillon * vnode if we can't get it in a reasonable amount 114323b59018SMatthew Dillon * of time. 11441c7c3c6aSMatthew Dillon */ 11451c7c3c6aSMatthew Dillon if (object->type == OBJT_VNODE) { 11462965a453SKip Macy vm_page_unlock(m); 114724a1cce3SDavid Greenman vp = object->handle; 1148db27dcc0STor Egge if (vp->v_type == VREG && 1149db27dcc0STor Egge vn_start_write(vp, &mp, V_NOWAIT) != 0) { 11506129343dSKonstantin Belousov mp = NULL; 1151db27dcc0STor Egge ++pageout_lock_miss; 1152db27dcc0STor Egge if (object->flags & OBJ_MIGHTBEDIRTY) 1153db27dcc0STor Egge vnodes_skipped++; 1154625e6c0aSTor Egge goto unlock_and_continue; 1155db27dcc0STor Egge } 1156b9f180d1SKonstantin Belousov KASSERT(mp != NULL, 1157b9f180d1SKonstantin Belousov ("vp %p with NULL v_mount", vp)); 115814137dc0SAlan Cox vm_object_reference_locked(object); 11593562af12SAlan Cox VM_OBJECT_UNLOCK(object); 116097824da3SAlan Cox if (vget(vp, LK_EXCLUSIVE | LK_TIMELOCK, 116197824da3SAlan Cox curthread)) { 11623562af12SAlan Cox VM_OBJECT_LOCK(object); 116323b59018SMatthew Dillon ++pageout_lock_miss; 1164aef922f5SJohn Dyson if (object->flags & OBJ_MIGHTBEDIRTY) 1165925a3a41SJohn Dyson vnodes_skipped++; 1166625e6c0aSTor Egge vp = NULL; 1167625e6c0aSTor Egge goto unlock_and_continue; 116885a376ebSJohn Dyson } 11693562af12SAlan Cox VM_OBJECT_LOCK(object); 11702965a453SKip Macy vm_page_lock(m); 1171*8d220203SAlan Cox vm_pagequeue_lock(pq); 117248cc2fc7SKonstantin Belousov queues_locked = TRUE; 1173f35329acSJohn Dyson /* 1174936524aaSMatthew Dillon * The page might have been moved to another 1175936524aaSMatthew Dillon * queue during potential blocking in vget() 1176936524aaSMatthew Dillon * above. The page might have been freed and 117714137dc0SAlan Cox * reused for another vnode. 1178f35329acSJohn Dyson */ 11799cf51988SAlan Cox if (m->queue != PQ_INACTIVE || 1180936524aaSMatthew Dillon m->object != object || 1181625e6c0aSTor Egge TAILQ_NEXT(m, pageq) != &marker) { 11822965a453SKip Macy vm_page_unlock(m); 1183b182ec9eSJohn Dyson if (object->flags & OBJ_MIGHTBEDIRTY) 1184925a3a41SJohn Dyson vnodes_skipped++; 11853562af12SAlan Cox goto unlock_and_continue; 1186b182ec9eSJohn Dyson } 1187b182ec9eSJohn Dyson 1188f35329acSJohn Dyson /* 1189936524aaSMatthew Dillon * The page may have been busied during the 119014137dc0SAlan Cox * blocking in vget(). We don't move the 1191936524aaSMatthew Dillon * page back onto the end of the queue so that 1192936524aaSMatthew Dillon * statistics are more correct if we don't. 1193f35329acSJohn Dyson */ 11949af80719SAlan Cox if (m->busy || (m->oflags & VPO_BUSY)) { 11952965a453SKip Macy vm_page_unlock(m); 11963562af12SAlan Cox goto unlock_and_continue; 1197b182ec9eSJohn Dyson } 1198b182ec9eSJohn Dyson 1199f35329acSJohn Dyson /* 120057601bcbSMatthew Dillon * If the page has become held it might 120157601bcbSMatthew Dillon * be undergoing I/O, so skip it 1202f35329acSJohn Dyson */ 1203b182ec9eSJohn Dyson if (m->hold_count) { 12042965a453SKip Macy vm_page_unlock(m); 1205*8d220203SAlan Cox vm_page_requeue_locked(m); 1206b182ec9eSJohn Dyson if (object->flags & OBJ_MIGHTBEDIRTY) 1207925a3a41SJohn Dyson vnodes_skipped++; 12083562af12SAlan Cox goto unlock_and_continue; 1209f6b04d2bSDavid Greenman } 1210*8d220203SAlan Cox vm_pagequeue_unlock(pq); 1211a4156419SKonstantin Belousov queues_locked = FALSE; 1212f6b04d2bSDavid Greenman } 1213f6b04d2bSDavid Greenman 12140d94caffSDavid Greenman /* 12150d94caffSDavid Greenman * If a page is dirty, then it is either being washed 12160d94caffSDavid Greenman * (but not yet cleaned) or it is still in the 12170d94caffSDavid Greenman * laundry. If it is still in the laundry, then we 12182b6b0df7SMatthew Dillon * start the cleaning operation. 1219936524aaSMatthew Dillon * 12202b6b0df7SMatthew Dillon * decrement page_shortage on success to account for 12212b6b0df7SMatthew Dillon * the (future) cleaned page. Otherwise we could wind 12222b6b0df7SMatthew Dillon * up laundering or cleaning too many pages. 12230d94caffSDavid Greenman */ 12242b6b0df7SMatthew Dillon if (vm_pageout_clean(m) != 0) { 12252b6b0df7SMatthew Dillon --page_shortage; 1226936524aaSMatthew Dillon --maxlaunder; 12272b6b0df7SMatthew Dillon } 12283562af12SAlan Cox unlock_and_continue: 12292965a453SKip Macy vm_page_lock_assert(m, MA_NOTOWNED); 12306989c456SAlan Cox VM_OBJECT_UNLOCK(object); 123114137dc0SAlan Cox if (mp != NULL) { 123248cc2fc7SKonstantin Belousov if (queues_locked) { 1233*8d220203SAlan Cox vm_pagequeue_unlock(pq); 123448cc2fc7SKonstantin Belousov queues_locked = FALSE; 123548cc2fc7SKonstantin Belousov } 123614137dc0SAlan Cox if (vp != NULL) 1237f6b04d2bSDavid Greenman vput(vp); 123814137dc0SAlan Cox vm_object_deallocate(object); 1239f2a2857bSKirk McKusick vn_finished_write(mp); 124048cc2fc7SKonstantin Belousov } 124148cc2fc7SKonstantin Belousov vm_page_lock_assert(m, MA_NOTOWNED); 124248cc2fc7SKonstantin Belousov goto relock_queues; 124348cc2fc7SKonstantin Belousov } 124448cc2fc7SKonstantin Belousov vm_page_unlock(m); 124548cc2fc7SKonstantin Belousov VM_OBJECT_UNLOCK(object); 124648cc2fc7SKonstantin Belousov relock_queues: 124748cc2fc7SKonstantin Belousov if (!queues_locked) { 1248*8d220203SAlan Cox vm_pagequeue_lock(pq); 124948cc2fc7SKonstantin Belousov queues_locked = TRUE; 12506989c456SAlan Cox } 1251625e6c0aSTor Egge next = TAILQ_NEXT(&marker, pageq); 1252*8d220203SAlan Cox TAILQ_REMOVE(&pq->pq_pl, &marker, pageq); 12530d94caffSDavid Greenman } 1254*8d220203SAlan Cox vm_pagequeue_unlock(pq); 125526f9a767SRodney W. Grimes 1256df8bae1dSRodney W. Grimes /* 1257936524aaSMatthew Dillon * Compute the number of pages we want to try to move from the 1258936524aaSMatthew Dillon * active queue to the inactive queue. 12591c7c3c6aSMatthew Dillon */ 12602feb50bfSAttilio Rao page_shortage = vm_paging_target() + 12612feb50bfSAttilio Rao cnt.v_inactive_target - cnt.v_inactive_count; 1262b182ec9eSJohn Dyson page_shortage += addl_page_shortage; 12631c7c3c6aSMatthew Dillon 12641c7c3c6aSMatthew Dillon /* 1265936524aaSMatthew Dillon * Scan the active queue for things we can deactivate. We nominally 1266936524aaSMatthew Dillon * track the per-page activity counter and use it to locate 1267936524aaSMatthew Dillon * deactivation candidates. 12681c7c3c6aSMatthew Dillon */ 12692feb50bfSAttilio Rao pcount = cnt.v_active_count; 1270*8d220203SAlan Cox pq = &vm_pagequeues[PQ_ACTIVE]; 1271*8d220203SAlan Cox vm_pagequeue_lock(pq); 1272*8d220203SAlan Cox m = TAILQ_FIRST(&pq->pq_pl); 1273b18bfc3dSJohn Dyson while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) { 1274f35329acSJohn Dyson 12759cf51988SAlan Cox KASSERT(m->queue == PQ_ACTIVE, 1276d3c09dd7SAlan Cox ("vm_pageout_scan: page %p isn't active", m)); 1277f35329acSJohn Dyson 1278b18bfc3dSJohn Dyson next = TAILQ_NEXT(m, pageq); 12798dbca793STor Egge if ((m->flags & PG_MARKER) != 0) { 12808dbca793STor Egge m = next; 12818dbca793STor Egge continue; 12828dbca793STor Egge } 12837900f95dSKonstantin Belousov KASSERT((m->flags & PG_FICTITIOUS) == 0, 12847900f95dSKonstantin Belousov ("Fictitious page %p cannot be in active queue", m)); 12857900f95dSKonstantin Belousov KASSERT((m->oflags & VPO_UNMANAGED) == 0, 12867900f95dSKonstantin Belousov ("Unmanaged page %p cannot be in active queue", m)); 12879ee2165fSAlan Cox if (!vm_pageout_page_lock(m, &next)) { 12888c616246SKonstantin Belousov vm_page_unlock(m); 12892965a453SKip Macy m = next; 12902965a453SKip Macy continue; 12912965a453SKip Macy } 12929ee2165fSAlan Cox object = m->object; 12938dbca793STor Egge if (!VM_OBJECT_TRYLOCK(object) && 12948dbca793STor Egge !vm_pageout_fallback_object_lock(m, &next)) { 12958dbca793STor Egge VM_OBJECT_UNLOCK(object); 12962965a453SKip Macy vm_page_unlock(m); 12974b8a5c40SAlan Cox m = next; 1298b08abf6cSAlan Cox continue; 1299b08abf6cSAlan Cox } 1300b08abf6cSAlan Cox 1301df8bae1dSRodney W. Grimes /* 130226f9a767SRodney W. Grimes * Don't deactivate pages that are busy. 1303df8bae1dSRodney W. Grimes */ 1304a647a309SDavid Greenman if ((m->busy != 0) || 13059af80719SAlan Cox (m->oflags & VPO_BUSY) || 1306f6b04d2bSDavid Greenman (m->hold_count != 0)) { 13072965a453SKip Macy vm_page_unlock(m); 1308b08abf6cSAlan Cox VM_OBJECT_UNLOCK(object); 1309*8d220203SAlan Cox vm_page_requeue_locked(m); 131026f9a767SRodney W. Grimes m = next; 131126f9a767SRodney W. Grimes continue; 1312df8bae1dSRodney W. Grimes } 1313b18bfc3dSJohn Dyson 1314b18bfc3dSJohn Dyson /* 1315b18bfc3dSJohn Dyson * The count for pagedaemon pages is done after checking the 1316956f3135SPhilippe Charnier * page for eligibility... 1317b18bfc3dSJohn Dyson */ 1318*8d220203SAlan Cox PCPU_INC(cnt.v_pdpages); 1319ef743ce6SJohn Dyson 13207e006499SJohn Dyson /* 13217e006499SJohn Dyson * Check to see "how much" the page has been used. 13227e006499SJohn Dyson */ 13237e006499SJohn Dyson actcount = 0; 1324b08abf6cSAlan Cox if (object->ref_count != 0) { 13253407fefeSKonstantin Belousov if (m->aflags & PGA_REFERENCED) { 13267e006499SJohn Dyson actcount += 1; 13270d94caffSDavid Greenman } 13280385347cSPeter Wemm actcount += pmap_ts_referenced(m); 13297e006499SJohn Dyson if (actcount) { 13307e006499SJohn Dyson m->act_count += ACT_ADVANCE + actcount; 133138efa82bSJohn Dyson if (m->act_count > ACT_MAX) 133238efa82bSJohn Dyson m->act_count = ACT_MAX; 133338efa82bSJohn Dyson } 1334b18bfc3dSJohn Dyson } 1335ef743ce6SJohn Dyson 13367e006499SJohn Dyson /* 13377e006499SJohn Dyson * Since we have "tested" this bit, we need to clear it now. 13387e006499SJohn Dyson */ 13393407fefeSKonstantin Belousov vm_page_aflag_clear(m, PGA_REFERENCED); 1340ef743ce6SJohn Dyson 13417e006499SJohn Dyson /* 13427e006499SJohn Dyson * Only if an object is currently being used, do we use the 13437e006499SJohn Dyson * page activation count stats. 13447e006499SJohn Dyson */ 1345*8d220203SAlan Cox if (actcount != 0 && object->ref_count != 0) 1346*8d220203SAlan Cox vm_page_requeue_locked(m); 1347*8d220203SAlan Cox else { 134838efa82bSJohn Dyson m->act_count -= min(m->act_count, ACT_DECLINE); 13492b6b0df7SMatthew Dillon if (vm_pageout_algorithm || 1350b08abf6cSAlan Cox object->ref_count == 0 || 13512b6b0df7SMatthew Dillon m->act_count == 0) { 1352925a3a41SJohn Dyson page_shortage--; 1353*8d220203SAlan Cox /* Dequeue to avoid later lock recursion. */ 1354*8d220203SAlan Cox vm_page_dequeue_locked(m); 1355b08abf6cSAlan Cox if (object->ref_count == 0) { 13565d4a7b79SAlan Cox KASSERT(!pmap_page_is_mapped(m), 13575d4a7b79SAlan Cox ("vm_pageout_scan: page %p is mapped", m)); 1358d4a272dbSJohn Dyson if (m->dirty == 0) 13590d94caffSDavid Greenman vm_page_cache(m); 1360d4a272dbSJohn Dyson else 1361d4a272dbSJohn Dyson vm_page_deactivate(m); 13620d94caffSDavid Greenman } else { 136326f9a767SRodney W. Grimes vm_page_deactivate(m); 1364df8bae1dSRodney W. Grimes } 1365*8d220203SAlan Cox } else 1366*8d220203SAlan Cox vm_page_requeue_locked(m); 1367df8bae1dSRodney W. Grimes } 13682965a453SKip Macy vm_page_unlock(m); 1369b08abf6cSAlan Cox VM_OBJECT_UNLOCK(object); 137026f9a767SRodney W. Grimes m = next; 137126f9a767SRodney W. Grimes } 1372*8d220203SAlan Cox vm_pagequeue_unlock(pq); 1373ceb0cf87SJohn Dyson #if !defined(NO_SWAPPING) 1374ceb0cf87SJohn Dyson /* 1375ceb0cf87SJohn Dyson * Idle process swapout -- run once per second. 1376ceb0cf87SJohn Dyson */ 1377ceb0cf87SJohn Dyson if (vm_swap_idle_enabled) { 1378ceb0cf87SJohn Dyson static long lsec; 1379227ee8a1SPoul-Henning Kamp if (time_second != lsec) { 138097824da3SAlan Cox vm_req_vmdaemon(VM_SWAP_IDLE); 1381227ee8a1SPoul-Henning Kamp lsec = time_second; 1382ceb0cf87SJohn Dyson } 1383ceb0cf87SJohn Dyson } 1384ceb0cf87SJohn Dyson #endif 1385ceb0cf87SJohn Dyson 13865663e6deSDavid Greenman /* 1387f6b04d2bSDavid Greenman * If we didn't get enough free pages, and we have skipped a vnode 13884c1f8ee9SDavid Greenman * in a writeable object, wakeup the sync daemon. And kick swapout 13894c1f8ee9SDavid Greenman * if we did not get enough free pages. 1390f6b04d2bSDavid Greenman */ 139190ecac61SMatthew Dillon if (vm_paging_target() > 0) { 139290ecac61SMatthew Dillon if (vnodes_skipped && vm_page_count_min()) 1393d50c1994SPeter Wemm (void) speedup_syncer(); 139438efa82bSJohn Dyson #if !defined(NO_SWAPPING) 139597824da3SAlan Cox if (vm_swap_enabled && vm_page_count_target()) 139697824da3SAlan Cox vm_req_vmdaemon(VM_SWAP_NORMAL); 13975afce282SDavid Greenman #endif 13984c1f8ee9SDavid Greenman } 13994c1f8ee9SDavid Greenman 1400f6b04d2bSDavid Greenman /* 1401e92686d0SDavid Schultz * If we are critically low on one of RAM or swap and low on 1402e92686d0SDavid Schultz * the other, kill the largest process. However, we avoid 1403e92686d0SDavid Schultz * doing this on the first pass in order to give ourselves a 1404e92686d0SDavid Schultz * chance to flush out dirty vnode-backed pages and to allow 1405e92686d0SDavid Schultz * active pages to be moved to the inactive queue and reclaimed. 14062025d69bSKonstantin Belousov */ 14072025d69bSKonstantin Belousov if (pass != 0 && 14082025d69bSKonstantin Belousov ((swap_pager_avail < 64 && vm_page_count_min()) || 14092025d69bSKonstantin Belousov (swap_pager_full && vm_paging_target() > 0))) 14102025d69bSKonstantin Belousov vm_pageout_oom(VM_OOM_MEM); 14112025d69bSKonstantin Belousov } 14122025d69bSKonstantin Belousov 14132025d69bSKonstantin Belousov 14142025d69bSKonstantin Belousov void 14152025d69bSKonstantin Belousov vm_pageout_oom(int shortage) 14162025d69bSKonstantin Belousov { 14172025d69bSKonstantin Belousov struct proc *p, *bigproc; 14182025d69bSKonstantin Belousov vm_offset_t size, bigsize; 14192025d69bSKonstantin Belousov struct thread *td; 14206bed074cSKonstantin Belousov struct vmspace *vm; 14212025d69bSKonstantin Belousov 14222025d69bSKonstantin Belousov /* 14231c58e4e5SJohn Baldwin * We keep the process bigproc locked once we find it to keep anyone 14241c58e4e5SJohn Baldwin * from messing with it; however, there is a possibility of 14251c58e4e5SJohn Baldwin * deadlock if process B is bigproc and one of it's child processes 14261c58e4e5SJohn Baldwin * attempts to propagate a signal to B while we are waiting for A's 14271c58e4e5SJohn Baldwin * lock while walking this list. To avoid this, we don't block on 14281c58e4e5SJohn Baldwin * the process lock but just skip a process if it is already locked. 14295663e6deSDavid Greenman */ 14305663e6deSDavid Greenman bigproc = NULL; 14315663e6deSDavid Greenman bigsize = 0; 14321005a129SJohn Baldwin sx_slock(&allproc_lock); 1433e602ba25SJulian Elischer FOREACH_PROC_IN_SYSTEM(p) { 1434e602ba25SJulian Elischer int breakout; 1435dcbcd518SBruce Evans 14361c58e4e5SJohn Baldwin if (PROC_TRYLOCK(p) == 0) 14371c58e4e5SJohn Baldwin continue; 14381c58e4e5SJohn Baldwin /* 14393f1c4c4fSKonstantin Belousov * If this is a system, protected or killed process, skip it. 14405663e6deSDavid Greenman */ 14418e6fa660SJohn Baldwin if (p->p_state != PRS_NORMAL || 14428e6fa660SJohn Baldwin (p->p_flag & (P_INEXEC | P_PROTECTED | P_SYSTEM)) || 14433f1c4c4fSKonstantin Belousov (p->p_pid == 1) || P_KILLED(p) || 14448f60c087SPoul-Henning Kamp ((p->p_pid < 48) && (swap_pager_avail != 0))) { 14458606d880SJohn Baldwin PROC_UNLOCK(p); 14465663e6deSDavid Greenman continue; 14475663e6deSDavid Greenman } 14485663e6deSDavid Greenman /* 1449dcbcd518SBruce Evans * If the process is in a non-running type state, 1450e602ba25SJulian Elischer * don't touch it. Check all the threads individually. 14515663e6deSDavid Greenman */ 1452e602ba25SJulian Elischer breakout = 0; 1453e602ba25SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 1454982d11f8SJeff Roberson thread_lock(td); 145571fad9fdSJulian Elischer if (!TD_ON_RUNQ(td) && 145671fad9fdSJulian Elischer !TD_IS_RUNNING(td) && 1457f497cda2SEdward Tomasz Napierala !TD_IS_SLEEPING(td) && 1458f497cda2SEdward Tomasz Napierala !TD_IS_SUSPENDED(td)) { 1459982d11f8SJeff Roberson thread_unlock(td); 1460e602ba25SJulian Elischer breakout = 1; 1461e602ba25SJulian Elischer break; 1462e602ba25SJulian Elischer } 1463982d11f8SJeff Roberson thread_unlock(td); 1464e602ba25SJulian Elischer } 1465e602ba25SJulian Elischer if (breakout) { 14661c58e4e5SJohn Baldwin PROC_UNLOCK(p); 14675663e6deSDavid Greenman continue; 14685663e6deSDavid Greenman } 14695663e6deSDavid Greenman /* 14705663e6deSDavid Greenman * get the process size 14715663e6deSDavid Greenman */ 14726bed074cSKonstantin Belousov vm = vmspace_acquire_ref(p); 14736bed074cSKonstantin Belousov if (vm == NULL) { 14746bed074cSKonstantin Belousov PROC_UNLOCK(p); 14756bed074cSKonstantin Belousov continue; 14766bed074cSKonstantin Belousov } 14776bed074cSKonstantin Belousov if (!vm_map_trylock_read(&vm->vm_map)) { 14786bed074cSKonstantin Belousov vmspace_free(vm); 147972d97679SDavid Schultz PROC_UNLOCK(p); 148072d97679SDavid Schultz continue; 148172d97679SDavid Schultz } 14827981aa24SKonstantin Belousov size = vmspace_swap_count(vm); 14836bed074cSKonstantin Belousov vm_map_unlock_read(&vm->vm_map); 14842025d69bSKonstantin Belousov if (shortage == VM_OOM_MEM) 14856bed074cSKonstantin Belousov size += vmspace_resident_count(vm); 14866bed074cSKonstantin Belousov vmspace_free(vm); 14875663e6deSDavid Greenman /* 14885663e6deSDavid Greenman * if the this process is bigger than the biggest one 14895663e6deSDavid Greenman * remember it. 14905663e6deSDavid Greenman */ 14915663e6deSDavid Greenman if (size > bigsize) { 14921c58e4e5SJohn Baldwin if (bigproc != NULL) 14931c58e4e5SJohn Baldwin PROC_UNLOCK(bigproc); 14945663e6deSDavid Greenman bigproc = p; 14955663e6deSDavid Greenman bigsize = size; 14961c58e4e5SJohn Baldwin } else 14971c58e4e5SJohn Baldwin PROC_UNLOCK(p); 14985663e6deSDavid Greenman } 14991005a129SJohn Baldwin sx_sunlock(&allproc_lock); 15005663e6deSDavid Greenman if (bigproc != NULL) { 1501729b1e51SDavid Greenman killproc(bigproc, "out of swap space"); 1502fa885116SJulian Elischer sched_nice(bigproc, PRIO_MIN); 15031c58e4e5SJohn Baldwin PROC_UNLOCK(bigproc); 15042feb50bfSAttilio Rao wakeup(&cnt.v_free_count); 15055663e6deSDavid Greenman } 15065663e6deSDavid Greenman } 150726f9a767SRodney W. Grimes 1508dc2efb27SJohn Dyson /* 1509dc2efb27SJohn Dyson * This routine tries to maintain the pseudo LRU active queue, 1510dc2efb27SJohn Dyson * so that during long periods of time where there is no paging, 1511956f3135SPhilippe Charnier * that some statistic accumulation still occurs. This code 1512dc2efb27SJohn Dyson * helps the situation where paging just starts to occur. 1513dc2efb27SJohn Dyson */ 1514dc2efb27SJohn Dyson static void 1515dc2efb27SJohn Dyson vm_pageout_page_stats() 1516dc2efb27SJohn Dyson { 1517*8d220203SAlan Cox struct vm_pagequeue *pq; 1518b86e6ec0SAlan Cox vm_object_t object; 1519dc2efb27SJohn Dyson vm_page_t m,next; 1520dc2efb27SJohn Dyson int pcount,tpcount; /* Number of pages to check */ 1521dc2efb27SJohn Dyson static int fullintervalcount = 0; 1522bef608bdSJohn Dyson int page_shortage; 1523bef608bdSJohn Dyson 152490ecac61SMatthew Dillon page_shortage = 15252feb50bfSAttilio Rao (cnt.v_inactive_target + cnt.v_cache_max + cnt.v_free_min) - 15262feb50bfSAttilio Rao (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count); 152790ecac61SMatthew Dillon 1528bef608bdSJohn Dyson if (page_shortage <= 0) 1529bef608bdSJohn Dyson return; 1530dc2efb27SJohn Dyson 15312feb50bfSAttilio Rao pcount = cnt.v_active_count; 1532dc2efb27SJohn Dyson fullintervalcount += vm_pageout_stats_interval; 1533dc2efb27SJohn Dyson if (fullintervalcount < vm_pageout_full_stats_interval) { 15348d28bf04SAlan Cox tpcount = (int64_t)vm_pageout_stats_max * cnt.v_active_count / 15358d28bf04SAlan Cox cnt.v_page_count; 1536dc2efb27SJohn Dyson if (pcount > tpcount) 1537dc2efb27SJohn Dyson pcount = tpcount; 1538883f3caaSMatthew Dillon } else { 1539883f3caaSMatthew Dillon fullintervalcount = 0; 1540dc2efb27SJohn Dyson } 1541dc2efb27SJohn Dyson 1542*8d220203SAlan Cox pq = &vm_pagequeues[PQ_ACTIVE]; 1543*8d220203SAlan Cox vm_pagequeue_lock(pq); 1544*8d220203SAlan Cox m = TAILQ_FIRST(&pq->pq_pl); 1545dc2efb27SJohn Dyson while ((m != NULL) && (pcount-- > 0)) { 15467e006499SJohn Dyson int actcount; 1547dc2efb27SJohn Dyson 15489cf51988SAlan Cox KASSERT(m->queue == PQ_ACTIVE, 1549ab42316cSAlan Cox ("vm_pageout_page_stats: page %p isn't active", m)); 1550dc2efb27SJohn Dyson 1551dc2efb27SJohn Dyson next = TAILQ_NEXT(m, pageq); 15528dbca793STor Egge if ((m->flags & PG_MARKER) != 0) { 15538dbca793STor Egge m = next; 15548dbca793STor Egge continue; 15558dbca793STor Egge } 15562965a453SKip Macy vm_page_lock_assert(m, MA_NOTOWNED); 15579ee2165fSAlan Cox if (!vm_pageout_page_lock(m, &next)) { 15588c616246SKonstantin Belousov vm_page_unlock(m); 15592965a453SKip Macy m = next; 15602965a453SKip Macy continue; 15612965a453SKip Macy } 15629ee2165fSAlan Cox object = m->object; 15638dbca793STor Egge if (!VM_OBJECT_TRYLOCK(object) && 15648dbca793STor Egge !vm_pageout_fallback_object_lock(m, &next)) { 15658dbca793STor Egge VM_OBJECT_UNLOCK(object); 15662965a453SKip Macy vm_page_unlock(m); 1567b86e6ec0SAlan Cox m = next; 1568b86e6ec0SAlan Cox continue; 1569b86e6ec0SAlan Cox } 1570b86e6ec0SAlan Cox 1571dc2efb27SJohn Dyson /* 1572dc2efb27SJohn Dyson * Don't deactivate pages that are busy. 1573dc2efb27SJohn Dyson */ 1574dc2efb27SJohn Dyson if ((m->busy != 0) || 15759af80719SAlan Cox (m->oflags & VPO_BUSY) || 1576dc2efb27SJohn Dyson (m->hold_count != 0)) { 15772965a453SKip Macy vm_page_unlock(m); 1578b86e6ec0SAlan Cox VM_OBJECT_UNLOCK(object); 1579*8d220203SAlan Cox vm_page_requeue_locked(m); 1580dc2efb27SJohn Dyson m = next; 1581dc2efb27SJohn Dyson continue; 1582dc2efb27SJohn Dyson } 1583dc2efb27SJohn Dyson 15847e006499SJohn Dyson actcount = 0; 15853407fefeSKonstantin Belousov if (m->aflags & PGA_REFERENCED) { 15863407fefeSKonstantin Belousov vm_page_aflag_clear(m, PGA_REFERENCED); 15877e006499SJohn Dyson actcount += 1; 1588dc2efb27SJohn Dyson } 1589dc2efb27SJohn Dyson 15900385347cSPeter Wemm actcount += pmap_ts_referenced(m); 15917e006499SJohn Dyson if (actcount) { 15927e006499SJohn Dyson m->act_count += ACT_ADVANCE + actcount; 1593dc2efb27SJohn Dyson if (m->act_count > ACT_MAX) 1594dc2efb27SJohn Dyson m->act_count = ACT_MAX; 1595*8d220203SAlan Cox vm_page_requeue_locked(m); 1596dc2efb27SJohn Dyson } else { 1597dc2efb27SJohn Dyson if (m->act_count == 0) { 15987e006499SJohn Dyson /* 15992b6b0df7SMatthew Dillon * We turn off page access, so that we have 16002b6b0df7SMatthew Dillon * more accurate RSS stats. We don't do this 16012b6b0df7SMatthew Dillon * in the normal page deactivation when the 16022b6b0df7SMatthew Dillon * system is loaded VM wise, because the 16032b6b0df7SMatthew Dillon * cost of the large number of page protect 16042b6b0df7SMatthew Dillon * operations would be higher than the value 16052b6b0df7SMatthew Dillon * of doing the operation. 16067e006499SJohn Dyson */ 16074fec79beSAlan Cox pmap_remove_all(m); 1608*8d220203SAlan Cox /* Dequeue to avoid later lock recursion. */ 1609*8d220203SAlan Cox vm_page_dequeue_locked(m); 1610dc2efb27SJohn Dyson vm_page_deactivate(m); 1611dc2efb27SJohn Dyson } else { 1612dc2efb27SJohn Dyson m->act_count -= min(m->act_count, ACT_DECLINE); 1613*8d220203SAlan Cox vm_page_requeue_locked(m); 1614dc2efb27SJohn Dyson } 1615dc2efb27SJohn Dyson } 16162965a453SKip Macy vm_page_unlock(m); 1617b86e6ec0SAlan Cox VM_OBJECT_UNLOCK(object); 1618dc2efb27SJohn Dyson m = next; 1619dc2efb27SJohn Dyson } 1620*8d220203SAlan Cox vm_pagequeue_unlock(pq); 1621dc2efb27SJohn Dyson } 1622dc2efb27SJohn Dyson 1623df8bae1dSRodney W. Grimes /* 1624df8bae1dSRodney W. Grimes * vm_pageout is the high level pageout daemon. 1625df8bae1dSRodney W. Grimes */ 16262b14f991SJulian Elischer static void 162726f9a767SRodney W. Grimes vm_pageout() 1628df8bae1dSRodney W. Grimes { 16291aab16a6SAlan Cox int error, pass; 16300384fff8SJason Evans 1631df8bae1dSRodney W. Grimes /* 1632df8bae1dSRodney W. Grimes * Initialize some paging parameters. 1633df8bae1dSRodney W. Grimes */ 16342feb50bfSAttilio Rao cnt.v_interrupt_free_min = 2; 16352feb50bfSAttilio Rao if (cnt.v_page_count < 2000) 1636f35329acSJohn Dyson vm_pageout_page_count = 8; 1637f6b04d2bSDavid Greenman 163845ae1d91SAlan Cox /* 163945ae1d91SAlan Cox * v_free_reserved needs to include enough for the largest 164045ae1d91SAlan Cox * swap pager structures plus enough for any pv_entry structs 164145ae1d91SAlan Cox * when paging. 164245ae1d91SAlan Cox */ 16432feb50bfSAttilio Rao if (cnt.v_page_count > 1024) 16442feb50bfSAttilio Rao cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200; 16452feb50bfSAttilio Rao else 16462feb50bfSAttilio Rao cnt.v_free_min = 4; 16472feb50bfSAttilio Rao cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE + 16482feb50bfSAttilio Rao cnt.v_interrupt_free_min; 16492feb50bfSAttilio Rao cnt.v_free_reserved = vm_pageout_page_count + 16502446e4f0SAlan Cox cnt.v_pageout_free_min + (cnt.v_page_count / 768); 16512feb50bfSAttilio Rao cnt.v_free_severe = cnt.v_free_min / 2; 16522feb50bfSAttilio Rao cnt.v_free_min += cnt.v_free_reserved; 16532feb50bfSAttilio Rao cnt.v_free_severe += cnt.v_free_reserved; 165445ae1d91SAlan Cox 1655ed74321bSDavid Greenman /* 16562b6b0df7SMatthew Dillon * v_free_target and v_cache_min control pageout hysteresis. Note 16572b6b0df7SMatthew Dillon * that these are more a measure of the VM cache queue hysteresis 16582b6b0df7SMatthew Dillon * then the VM free queue. Specifically, v_free_target is the 16592b6b0df7SMatthew Dillon * high water mark (free+cache pages). 16602b6b0df7SMatthew Dillon * 16612b6b0df7SMatthew Dillon * v_free_reserved + v_cache_min (mostly means v_cache_min) is the 16622b6b0df7SMatthew Dillon * low water mark, while v_free_min is the stop. v_cache_min must 16632b6b0df7SMatthew Dillon * be big enough to handle memory needs while the pageout daemon 16642b6b0df7SMatthew Dillon * is signalled and run to free more pages. 1665ed74321bSDavid Greenman */ 16662feb50bfSAttilio Rao if (cnt.v_free_count > 6144) 16672feb50bfSAttilio Rao cnt.v_free_target = 4 * cnt.v_free_min + cnt.v_free_reserved; 16682feb50bfSAttilio Rao else 16692feb50bfSAttilio Rao cnt.v_free_target = 2 * cnt.v_free_min + cnt.v_free_reserved; 16706f2b142eSDavid Greenman 16712feb50bfSAttilio Rao if (cnt.v_free_count > 2048) { 16722feb50bfSAttilio Rao cnt.v_cache_min = cnt.v_free_target; 16732feb50bfSAttilio Rao cnt.v_cache_max = 2 * cnt.v_cache_min; 16742feb50bfSAttilio Rao cnt.v_inactive_target = (3 * cnt.v_free_target) / 2; 16750d94caffSDavid Greenman } else { 16762feb50bfSAttilio Rao cnt.v_cache_min = 0; 16772feb50bfSAttilio Rao cnt.v_cache_max = 0; 16782feb50bfSAttilio Rao cnt.v_inactive_target = cnt.v_free_count / 4; 16790d94caffSDavid Greenman } 16802feb50bfSAttilio Rao if (cnt.v_inactive_target > cnt.v_free_count / 3) 16812feb50bfSAttilio Rao cnt.v_inactive_target = cnt.v_free_count / 3; 1682df8bae1dSRodney W. Grimes 1683df8bae1dSRodney W. Grimes /* XXX does not really belong here */ 1684df8bae1dSRodney W. Grimes if (vm_page_max_wired == 0) 16852feb50bfSAttilio Rao vm_page_max_wired = cnt.v_free_count / 3; 1686df8bae1dSRodney W. Grimes 1687dc2efb27SJohn Dyson if (vm_pageout_stats_max == 0) 16882feb50bfSAttilio Rao vm_pageout_stats_max = cnt.v_free_target; 1689dc2efb27SJohn Dyson 1690dc2efb27SJohn Dyson /* 1691dc2efb27SJohn Dyson * Set interval in seconds for stats scan. 1692dc2efb27SJohn Dyson */ 1693dc2efb27SJohn Dyson if (vm_pageout_stats_interval == 0) 1694bef608bdSJohn Dyson vm_pageout_stats_interval = 5; 1695dc2efb27SJohn Dyson if (vm_pageout_full_stats_interval == 0) 1696dc2efb27SJohn Dyson vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4; 1697dc2efb27SJohn Dyson 169824a1cce3SDavid Greenman swap_pager_swap_init(); 16992b6b0df7SMatthew Dillon pass = 0; 1700df8bae1dSRodney W. Grimes /* 17010d94caffSDavid Greenman * The pageout daemon is never done, so loop forever. 1702df8bae1dSRodney W. Grimes */ 1703df8bae1dSRodney W. Grimes while (TRUE) { 1704936524aaSMatthew Dillon /* 1705936524aaSMatthew Dillon * If we have enough free memory, wakeup waiters. Do 1706936524aaSMatthew Dillon * not clear vm_pages_needed until we reach our target, 1707936524aaSMatthew Dillon * otherwise we may be woken up over and over again and 1708936524aaSMatthew Dillon * waste a lot of cpu. 1709936524aaSMatthew Dillon */ 1710e9f995d8SAlan Cox mtx_lock(&vm_page_queue_free_mtx); 1711936524aaSMatthew Dillon if (vm_pages_needed && !vm_page_count_min()) { 1712a1c0a785SAlan Cox if (!vm_paging_needed()) 1713936524aaSMatthew Dillon vm_pages_needed = 0; 17142feb50bfSAttilio Rao wakeup(&cnt.v_free_count); 1715936524aaSMatthew Dillon } 1716936524aaSMatthew Dillon if (vm_pages_needed) { 171790ecac61SMatthew Dillon /* 17182b6b0df7SMatthew Dillon * Still not done, take a second pass without waiting 17192b6b0df7SMatthew Dillon * (unlimited dirty cleaning), otherwise sleep a bit 17202b6b0df7SMatthew Dillon * and try again. 172190ecac61SMatthew Dillon */ 17222b6b0df7SMatthew Dillon ++pass; 17232b6b0df7SMatthew Dillon if (pass > 1) 1724e9f995d8SAlan Cox msleep(&vm_pages_needed, 1725e9f995d8SAlan Cox &vm_page_queue_free_mtx, PVM, "psleep", 1726e9f995d8SAlan Cox hz / 2); 172790ecac61SMatthew Dillon } else { 172890ecac61SMatthew Dillon /* 17292b6b0df7SMatthew Dillon * Good enough, sleep & handle stats. Prime the pass 17302b6b0df7SMatthew Dillon * for the next run. 173190ecac61SMatthew Dillon */ 17322b6b0df7SMatthew Dillon if (pass > 1) 17332b6b0df7SMatthew Dillon pass = 1; 17342b6b0df7SMatthew Dillon else 17352b6b0df7SMatthew Dillon pass = 0; 1736e9f995d8SAlan Cox error = msleep(&vm_pages_needed, 1737e9f995d8SAlan Cox &vm_page_queue_free_mtx, PVM, "psleep", 1738e9f995d8SAlan Cox vm_pageout_stats_interval * hz); 1739dc2efb27SJohn Dyson if (error && !vm_pages_needed) { 1740e9f995d8SAlan Cox mtx_unlock(&vm_page_queue_free_mtx); 17412b6b0df7SMatthew Dillon pass = 0; 1742dc2efb27SJohn Dyson vm_pageout_page_stats(); 1743dc2efb27SJohn Dyson continue; 1744dc2efb27SJohn Dyson } 1745f919ebdeSDavid Greenman } 1746b18bfc3dSJohn Dyson if (vm_pages_needed) 1747393a081dSAttilio Rao cnt.v_pdwakeups++; 1748e9f995d8SAlan Cox mtx_unlock(&vm_page_queue_free_mtx); 17492b6b0df7SMatthew Dillon vm_pageout_scan(pass); 1750df8bae1dSRodney W. Grimes } 1751df8bae1dSRodney W. Grimes } 175226f9a767SRodney W. Grimes 17536b4b77adSAlan Cox /* 1754e9f995d8SAlan Cox * Unless the free page queue lock is held by the caller, this function 17556b4b77adSAlan Cox * should be regarded as advisory. Specifically, the caller should 17566b4b77adSAlan Cox * not msleep() on &cnt.v_free_count following this function unless 1757e9f995d8SAlan Cox * the free page queue lock is held until the msleep() is performed. 17586b4b77adSAlan Cox */ 1759e0c5a895SJohn Dyson void 1760e0c5a895SJohn Dyson pagedaemon_wakeup() 1761e0c5a895SJohn Dyson { 1762a1c0a785SAlan Cox 1763b40ce416SJulian Elischer if (!vm_pages_needed && curthread->td_proc != pageproc) { 1764a1c0a785SAlan Cox vm_pages_needed = 1; 1765e0c5a895SJohn Dyson wakeup(&vm_pages_needed); 1766e0c5a895SJohn Dyson } 1767e0c5a895SJohn Dyson } 1768e0c5a895SJohn Dyson 176938efa82bSJohn Dyson #if !defined(NO_SWAPPING) 17705afce282SDavid Greenman static void 177197824da3SAlan Cox vm_req_vmdaemon(int req) 17725afce282SDavid Greenman { 17735afce282SDavid Greenman static int lastrun = 0; 17745afce282SDavid Greenman 177597824da3SAlan Cox mtx_lock(&vm_daemon_mtx); 177697824da3SAlan Cox vm_pageout_req_swapout |= req; 1777b18bfc3dSJohn Dyson if ((ticks > (lastrun + hz)) || (ticks < lastrun)) { 17785afce282SDavid Greenman wakeup(&vm_daemon_needed); 17795afce282SDavid Greenman lastrun = ticks; 17805afce282SDavid Greenman } 178197824da3SAlan Cox mtx_unlock(&vm_daemon_mtx); 17825afce282SDavid Greenman } 17835afce282SDavid Greenman 17842b14f991SJulian Elischer static void 17854f9fb771SBruce Evans vm_daemon() 17860d94caffSDavid Greenman { 178791d5354aSJohn Baldwin struct rlimit rsslim; 1788dcbcd518SBruce Evans struct proc *p; 1789dcbcd518SBruce Evans struct thread *td; 17906bed074cSKonstantin Belousov struct vmspace *vm; 1791099e7e95SEdward Tomasz Napierala int breakout, swapout_flags, tryagain, attempts; 1792afcc55f3SEdward Tomasz Napierala #ifdef RACCT 1793099e7e95SEdward Tomasz Napierala uint64_t rsize, ravailable; 1794afcc55f3SEdward Tomasz Napierala #endif 17950d94caffSDavid Greenman 17962fe6e4d7SDavid Greenman while (TRUE) { 179797824da3SAlan Cox mtx_lock(&vm_daemon_mtx); 1798099e7e95SEdward Tomasz Napierala #ifdef RACCT 1799099e7e95SEdward Tomasz Napierala msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", hz); 1800099e7e95SEdward Tomasz Napierala #else 180197824da3SAlan Cox msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", 0); 1802099e7e95SEdward Tomasz Napierala #endif 180397824da3SAlan Cox swapout_flags = vm_pageout_req_swapout; 18044c1f8ee9SDavid Greenman vm_pageout_req_swapout = 0; 180597824da3SAlan Cox mtx_unlock(&vm_daemon_mtx); 180697824da3SAlan Cox if (swapout_flags) 180797824da3SAlan Cox swapout_procs(swapout_flags); 180897824da3SAlan Cox 18092fe6e4d7SDavid Greenman /* 18100d94caffSDavid Greenman * scan the processes for exceeding their rlimits or if 18110d94caffSDavid Greenman * process is swapped out -- deactivate pages 18122fe6e4d7SDavid Greenman */ 1813099e7e95SEdward Tomasz Napierala tryagain = 0; 1814099e7e95SEdward Tomasz Napierala attempts = 0; 1815099e7e95SEdward Tomasz Napierala again: 1816099e7e95SEdward Tomasz Napierala attempts++; 18171005a129SJohn Baldwin sx_slock(&allproc_lock); 1818f67af5c9SXin LI FOREACH_PROC_IN_SYSTEM(p) { 1819fe2144fdSLuoqi Chen vm_pindex_t limit, size; 18202fe6e4d7SDavid Greenman 18212fe6e4d7SDavid Greenman /* 18222fe6e4d7SDavid Greenman * if this is a system process or if we have already 18232fe6e4d7SDavid Greenman * looked at this process, skip it. 18242fe6e4d7SDavid Greenman */ 1825897ecacdSJohn Baldwin PROC_LOCK(p); 18268e6fa660SJohn Baldwin if (p->p_state != PRS_NORMAL || 18278e6fa660SJohn Baldwin p->p_flag & (P_INEXEC | P_SYSTEM | P_WEXIT)) { 1828897ecacdSJohn Baldwin PROC_UNLOCK(p); 18292fe6e4d7SDavid Greenman continue; 18302fe6e4d7SDavid Greenman } 18312fe6e4d7SDavid Greenman /* 18322fe6e4d7SDavid Greenman * if the process is in a non-running type state, 18332fe6e4d7SDavid Greenman * don't touch it. 18342fe6e4d7SDavid Greenman */ 1835e602ba25SJulian Elischer breakout = 0; 1836e602ba25SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 1837982d11f8SJeff Roberson thread_lock(td); 183871fad9fdSJulian Elischer if (!TD_ON_RUNQ(td) && 183971fad9fdSJulian Elischer !TD_IS_RUNNING(td) && 1840f497cda2SEdward Tomasz Napierala !TD_IS_SLEEPING(td) && 1841f497cda2SEdward Tomasz Napierala !TD_IS_SUSPENDED(td)) { 1842982d11f8SJeff Roberson thread_unlock(td); 1843e602ba25SJulian Elischer breakout = 1; 1844e602ba25SJulian Elischer break; 1845e602ba25SJulian Elischer } 1846982d11f8SJeff Roberson thread_unlock(td); 1847e602ba25SJulian Elischer } 1848897ecacdSJohn Baldwin if (breakout) { 1849897ecacdSJohn Baldwin PROC_UNLOCK(p); 18502fe6e4d7SDavid Greenman continue; 18512fe6e4d7SDavid Greenman } 18522fe6e4d7SDavid Greenman /* 18532fe6e4d7SDavid Greenman * get a limit 18542fe6e4d7SDavid Greenman */ 1855dcbcd518SBruce Evans lim_rlimit(p, RLIMIT_RSS, &rsslim); 1856fe2144fdSLuoqi Chen limit = OFF_TO_IDX( 185791d5354aSJohn Baldwin qmin(rsslim.rlim_cur, rsslim.rlim_max)); 18582fe6e4d7SDavid Greenman 18592fe6e4d7SDavid Greenman /* 18600d94caffSDavid Greenman * let processes that are swapped out really be 18610d94caffSDavid Greenman * swapped out set the limit to nothing (will force a 18620d94caffSDavid Greenman * swap-out.) 18632fe6e4d7SDavid Greenman */ 1864b61ce5b0SJeff Roberson if ((p->p_flag & P_INMEM) == 0) 18650d94caffSDavid Greenman limit = 0; /* XXX */ 18666bed074cSKonstantin Belousov vm = vmspace_acquire_ref(p); 1867897ecacdSJohn Baldwin PROC_UNLOCK(p); 18686bed074cSKonstantin Belousov if (vm == NULL) 18696bed074cSKonstantin Belousov continue; 18702fe6e4d7SDavid Greenman 18716bed074cSKonstantin Belousov size = vmspace_resident_count(vm); 1872a406d8c3SEdward Tomasz Napierala if (size >= limit) { 1873fe2144fdSLuoqi Chen vm_pageout_map_deactivate_pages( 18746bed074cSKonstantin Belousov &vm->vm_map, limit); 18752fe6e4d7SDavid Greenman } 1876afcc55f3SEdward Tomasz Napierala #ifdef RACCT 1877099e7e95SEdward Tomasz Napierala rsize = IDX_TO_OFF(size); 1878099e7e95SEdward Tomasz Napierala PROC_LOCK(p); 1879099e7e95SEdward Tomasz Napierala racct_set(p, RACCT_RSS, rsize); 1880099e7e95SEdward Tomasz Napierala ravailable = racct_get_available(p, RACCT_RSS); 1881099e7e95SEdward Tomasz Napierala PROC_UNLOCK(p); 1882099e7e95SEdward Tomasz Napierala if (rsize > ravailable) { 1883099e7e95SEdward Tomasz Napierala /* 1884099e7e95SEdward Tomasz Napierala * Don't be overly aggressive; this might be 1885099e7e95SEdward Tomasz Napierala * an innocent process, and the limit could've 1886099e7e95SEdward Tomasz Napierala * been exceeded by some memory hog. Don't 1887099e7e95SEdward Tomasz Napierala * try to deactivate more than 1/4th of process' 1888099e7e95SEdward Tomasz Napierala * resident set size. 1889099e7e95SEdward Tomasz Napierala */ 1890099e7e95SEdward Tomasz Napierala if (attempts <= 8) { 1891099e7e95SEdward Tomasz Napierala if (ravailable < rsize - (rsize / 4)) 1892099e7e95SEdward Tomasz Napierala ravailable = rsize - (rsize / 4); 1893099e7e95SEdward Tomasz Napierala } 1894099e7e95SEdward Tomasz Napierala vm_pageout_map_deactivate_pages( 1895099e7e95SEdward Tomasz Napierala &vm->vm_map, OFF_TO_IDX(ravailable)); 1896099e7e95SEdward Tomasz Napierala /* Update RSS usage after paging out. */ 1897099e7e95SEdward Tomasz Napierala size = vmspace_resident_count(vm); 1898099e7e95SEdward Tomasz Napierala rsize = IDX_TO_OFF(size); 1899099e7e95SEdward Tomasz Napierala PROC_LOCK(p); 1900099e7e95SEdward Tomasz Napierala racct_set(p, RACCT_RSS, rsize); 1901099e7e95SEdward Tomasz Napierala PROC_UNLOCK(p); 1902099e7e95SEdward Tomasz Napierala if (rsize > ravailable) 1903099e7e95SEdward Tomasz Napierala tryagain = 1; 1904099e7e95SEdward Tomasz Napierala } 1905afcc55f3SEdward Tomasz Napierala #endif 19066bed074cSKonstantin Belousov vmspace_free(vm); 19072fe6e4d7SDavid Greenman } 19081005a129SJohn Baldwin sx_sunlock(&allproc_lock); 1909099e7e95SEdward Tomasz Napierala if (tryagain != 0 && attempts <= 10) 1910099e7e95SEdward Tomasz Napierala goto again; 191124a1cce3SDavid Greenman } 19122fe6e4d7SDavid Greenman } 1913a1287949SEivind Eklund #endif /* !defined(NO_SWAPPING) */ 1914