160727d8bSWarner Losh /*- 226f9a767SRodney W. Grimes * Copyright (c) 1991 Regents of the University of California. 326f9a767SRodney W. Grimes * All rights reserved. 426f9a767SRodney W. Grimes * Copyright (c) 1994 John S. Dyson 526f9a767SRodney W. Grimes * All rights reserved. 626f9a767SRodney W. Grimes * Copyright (c) 1994 David Greenman 726f9a767SRodney W. Grimes * All rights reserved. 88dbca793STor Egge * Copyright (c) 2005 Yahoo! Technologies Norway AS 98dbca793STor Egge * All rights reserved. 10df8bae1dSRodney W. Grimes * 11df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 12df8bae1dSRodney W. Grimes * The Mach Operating System project at Carnegie-Mellon University. 13df8bae1dSRodney W. Grimes * 14df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 15df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 16df8bae1dSRodney W. Grimes * are met: 17df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 18df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 19df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 20df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 21df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 22df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 235929bcfaSPhilippe Charnier * must display the following acknowledgement: 24df8bae1dSRodney W. Grimes * This product includes software developed by the University of 25df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 26df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 27df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 28df8bae1dSRodney W. Grimes * without specific prior written permission. 29df8bae1dSRodney W. Grimes * 30df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 31df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 34df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 35df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 36df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 37df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 38df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 39df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 40df8bae1dSRodney W. Grimes * SUCH DAMAGE. 41df8bae1dSRodney W. Grimes * 423c4dd356SDavid Greenman * from: @(#)vm_pageout.c 7.4 (Berkeley) 5/7/91 43df8bae1dSRodney W. Grimes * 44df8bae1dSRodney W. Grimes * 45df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1990 Carnegie-Mellon University. 46df8bae1dSRodney W. Grimes * All rights reserved. 47df8bae1dSRodney W. Grimes * 48df8bae1dSRodney W. Grimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young 49df8bae1dSRodney W. Grimes * 50df8bae1dSRodney W. Grimes * Permission to use, copy, modify and distribute this software and 51df8bae1dSRodney W. Grimes * its documentation is hereby granted, provided that both the copyright 52df8bae1dSRodney W. Grimes * notice and this permission notice appear in all copies of the 53df8bae1dSRodney W. Grimes * software, derivative works or modified versions, and any portions 54df8bae1dSRodney W. Grimes * thereof, and that both notices appear in supporting documentation. 55df8bae1dSRodney W. Grimes * 56df8bae1dSRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 57df8bae1dSRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 58df8bae1dSRodney W. Grimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 59df8bae1dSRodney W. Grimes * 60df8bae1dSRodney W. Grimes * Carnegie Mellon requests users of this software to return to 61df8bae1dSRodney W. Grimes * 62df8bae1dSRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 63df8bae1dSRodney W. Grimes * School of Computer Science 64df8bae1dSRodney W. Grimes * Carnegie Mellon University 65df8bae1dSRodney W. Grimes * Pittsburgh PA 15213-3890 66df8bae1dSRodney W. Grimes * 67df8bae1dSRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the 68df8bae1dSRodney W. Grimes * rights to redistribute these changes. 69df8bae1dSRodney W. Grimes */ 70df8bae1dSRodney W. Grimes 71df8bae1dSRodney W. Grimes /* 72df8bae1dSRodney W. Grimes * The proverbial page-out daemon. 73df8bae1dSRodney W. Grimes */ 74df8bae1dSRodney W. Grimes 75874651b1SDavid E. O'Brien #include <sys/cdefs.h> 76874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 77874651b1SDavid E. O'Brien 78faa5f8d8SAndrzej Bialecki #include "opt_vm.h" 79df8bae1dSRodney W. Grimes #include <sys/param.h> 8026f9a767SRodney W. Grimes #include <sys/systm.h> 81b5e8ce9fSBruce Evans #include <sys/kernel.h> 82855a310fSJeff Roberson #include <sys/eventhandler.h> 83fb919e4dSMark Murray #include <sys/lock.h> 84fb919e4dSMark Murray #include <sys/mutex.h> 8526f9a767SRodney W. Grimes #include <sys/proc.h> 869c8b8baaSPeter Wemm #include <sys/kthread.h> 870384fff8SJason Evans #include <sys/ktr.h> 8897824da3SAlan Cox #include <sys/mount.h> 89099e7e95SEdward Tomasz Napierala #include <sys/racct.h> 9026f9a767SRodney W. Grimes #include <sys/resourcevar.h> 91b43179fbSJeff Roberson #include <sys/sched.h> 92d2fc5315SPoul-Henning Kamp #include <sys/signalvar.h> 93449c2e92SKonstantin Belousov #include <sys/smp.h> 94f6b04d2bSDavid Greenman #include <sys/vnode.h> 95efeaf95aSDavid Greenman #include <sys/vmmeter.h> 9689f6b863SAttilio Rao #include <sys/rwlock.h> 971005a129SJohn Baldwin #include <sys/sx.h> 9838efa82bSJohn Dyson #include <sys/sysctl.h> 99df8bae1dSRodney W. Grimes 100df8bae1dSRodney W. Grimes #include <vm/vm.h> 101efeaf95aSDavid Greenman #include <vm/vm_param.h> 102efeaf95aSDavid Greenman #include <vm/vm_object.h> 103df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 104efeaf95aSDavid Greenman #include <vm/vm_map.h> 105df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h> 10624a1cce3SDavid Greenman #include <vm/vm_pager.h> 107449c2e92SKonstantin Belousov #include <vm/vm_phys.h> 10805f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h> 109efeaf95aSDavid Greenman #include <vm/vm_extern.h> 110670d17b5SJeff Roberson #include <vm/uma.h> 111df8bae1dSRodney W. Grimes 1122b14f991SJulian Elischer /* 1132b14f991SJulian Elischer * System initialization 1142b14f991SJulian Elischer */ 1152b14f991SJulian Elischer 1162b14f991SJulian Elischer /* the kernel process "vm_pageout"*/ 11711caded3SAlfred Perlstein static void vm_pageout(void); 11811caded3SAlfred Perlstein static int vm_pageout_clean(vm_page_t); 119449c2e92SKonstantin Belousov static void vm_pageout_scan(struct vm_domain *vmd, int pass); 120449c2e92SKonstantin Belousov static void vm_pageout_mightbe_oom(struct vm_domain *vmd, int pass); 12145ae1d91SAlan Cox 1222b14f991SJulian Elischer struct proc *pageproc; 1232b14f991SJulian Elischer 1242b14f991SJulian Elischer static struct kproc_desc page_kp = { 1252b14f991SJulian Elischer "pagedaemon", 1262b14f991SJulian Elischer vm_pageout, 1272b14f991SJulian Elischer &pageproc 1282b14f991SJulian Elischer }; 129237fdd78SRobert Watson SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, 130237fdd78SRobert Watson &page_kp); 1312b14f991SJulian Elischer 13238efa82bSJohn Dyson #if !defined(NO_SWAPPING) 1332b14f991SJulian Elischer /* the kernel process "vm_daemon"*/ 13411caded3SAlfred Perlstein static void vm_daemon(void); 135f708ef1bSPoul-Henning Kamp static struct proc *vmproc; 1362b14f991SJulian Elischer 1372b14f991SJulian Elischer static struct kproc_desc vm_kp = { 1382b14f991SJulian Elischer "vmdaemon", 1392b14f991SJulian Elischer vm_daemon, 1402b14f991SJulian Elischer &vmproc 1412b14f991SJulian Elischer }; 142237fdd78SRobert Watson SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp); 14338efa82bSJohn Dyson #endif 1442b14f991SJulian Elischer 1452b14f991SJulian Elischer 1468b245767SAlan Cox int vm_pages_needed; /* Event on which pageout daemon sleeps */ 1478b245767SAlan Cox int vm_pageout_deficit; /* Estimated number of pages deficit */ 1488b245767SAlan Cox int vm_pageout_pages_needed; /* flag saying that the pageout daemon needs pages */ 149*d9e23210SJeff Roberson int vm_pageout_wakeup_thresh; 15026f9a767SRodney W. Grimes 15138efa82bSJohn Dyson #if !defined(NO_SWAPPING) 152f708ef1bSPoul-Henning Kamp static int vm_pageout_req_swapout; /* XXX */ 153f708ef1bSPoul-Henning Kamp static int vm_daemon_needed; 15497824da3SAlan Cox static struct mtx vm_daemon_mtx; 15597824da3SAlan Cox /* Allow for use by vm_pageout before vm_daemon is initialized. */ 15697824da3SAlan Cox MTX_SYSINIT(vm_daemon, &vm_daemon_mtx, "vm daemon", MTX_DEF); 15738efa82bSJohn Dyson #endif 1582b6b0df7SMatthew Dillon static int vm_max_launder = 32; 159*d9e23210SJeff Roberson static int vm_pageout_update_period; 1604a365329SAndrey Zonov static int defer_swap_pageouts; 1614a365329SAndrey Zonov static int disable_swap_pageouts; 16270111b90SJohn Dyson 16338efa82bSJohn Dyson #if defined(NO_SWAPPING) 164303b270bSEivind Eklund static int vm_swap_enabled = 0; 165303b270bSEivind Eklund static int vm_swap_idle_enabled = 0; 16638efa82bSJohn Dyson #else 167303b270bSEivind Eklund static int vm_swap_enabled = 1; 168303b270bSEivind Eklund static int vm_swap_idle_enabled = 0; 16938efa82bSJohn Dyson #endif 17038efa82bSJohn Dyson 171*d9e23210SJeff Roberson SYSCTL_INT(_vm, OID_AUTO, pageout_wakeup_thresh, 172*d9e23210SJeff Roberson CTLFLAG_RW, &vm_pageout_wakeup_thresh, 0, 173*d9e23210SJeff Roberson "free page threshold for waking up the pageout daemon"); 174*d9e23210SJeff Roberson 1752b6b0df7SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, max_launder, 1762b6b0df7SMatthew Dillon CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout"); 17738efa82bSJohn Dyson 178*d9e23210SJeff Roberson SYSCTL_INT(_vm, OID_AUTO, pageout_update_period, 179*d9e23210SJeff Roberson CTLFLAG_RW, &vm_pageout_update_period, 0, 180*d9e23210SJeff Roberson "Maximum active LRU update period"); 18153636869SAndrey Zonov 18238efa82bSJohn Dyson #if defined(NO_SWAPPING) 183ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 1846bd9cb1cSTom Rhodes CTLFLAG_RD, &vm_swap_enabled, 0, "Enable entire process swapout"); 185ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 1866bd9cb1cSTom Rhodes CTLFLAG_RD, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria"); 18738efa82bSJohn Dyson #else 188ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 189b0359e2cSPeter Wemm CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout"); 190ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 191b0359e2cSPeter Wemm CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria"); 19238efa82bSJohn Dyson #endif 19326f9a767SRodney W. Grimes 194ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts, 195b0359e2cSPeter Wemm CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem"); 19612ac6a1dSJohn Dyson 197ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts, 198b0359e2cSPeter Wemm CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages"); 19912ac6a1dSJohn Dyson 20023b59018SMatthew Dillon static int pageout_lock_miss; 20123b59018SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss, 20223b59018SMatthew Dillon CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout"); 20323b59018SMatthew Dillon 204ffc82b0aSJohn Dyson #define VM_PAGEOUT_PAGE_COUNT 16 205bbc0ec52SDavid Greenman int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT; 206df8bae1dSRodney W. Grimes 207c3cb3e12SDavid Greenman int vm_page_max_wired; /* XXX max # of wired pages system-wide */ 2085dfc2870SAlan Cox SYSCTL_INT(_vm, OID_AUTO, max_wired, 2095dfc2870SAlan Cox CTLFLAG_RW, &vm_page_max_wired, 0, "System-wide limit to wired page count"); 210df8bae1dSRodney W. Grimes 21185eeca35SAlan Cox static boolean_t vm_pageout_fallback_object_lock(vm_page_t, vm_page_t *); 212449c2e92SKonstantin Belousov static boolean_t vm_pageout_launder(struct vm_pagequeue *pq, int, vm_paddr_t, 213449c2e92SKonstantin Belousov 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 *); 220cd41fc12SDavid Greenman 221a8229fa3SAlan Cox /* 222a8229fa3SAlan Cox * Initialize a dummy page for marking the caller's place in the specified 223a8229fa3SAlan Cox * paging queue. In principle, this function only needs to set the flag 224c7aebda8SAttilio Rao * PG_MARKER. Nonetheless, it wirte busies and initializes the hold count 225c7aebda8SAttilio Rao * to one as safety precautions. 226a8229fa3SAlan Cox */ 2278c616246SKonstantin Belousov static void 2288c616246SKonstantin Belousov vm_pageout_init_marker(vm_page_t marker, u_short queue) 2298c616246SKonstantin Belousov { 2308c616246SKonstantin Belousov 2318c616246SKonstantin Belousov bzero(marker, sizeof(*marker)); 232a8229fa3SAlan Cox marker->flags = PG_MARKER; 233c7aebda8SAttilio Rao marker->busy_lock = VPB_SINGLE_EXCLUSIVER; 2348c616246SKonstantin Belousov marker->queue = queue; 235a8229fa3SAlan Cox marker->hold_count = 1; 2368c616246SKonstantin Belousov } 2378c616246SKonstantin Belousov 23826f9a767SRodney W. Grimes /* 2398dbca793STor Egge * vm_pageout_fallback_object_lock: 2408dbca793STor Egge * 24189f6b863SAttilio Rao * Lock vm object currently associated with `m'. VM_OBJECT_TRYWLOCK is 2428dbca793STor Egge * known to have failed and page queue must be either PQ_ACTIVE or 2438dbca793STor Egge * PQ_INACTIVE. To avoid lock order violation, unlock the page queues 2448dbca793STor Egge * while locking the vm object. Use marker page to detect page queue 2458dbca793STor Egge * changes and maintain notion of next page on page queue. Return 2468dbca793STor Egge * TRUE if no changes were detected, FALSE otherwise. vm object is 2478dbca793STor Egge * locked on return. 2488dbca793STor Egge * 2498dbca793STor Egge * This function depends on both the lock portion of struct vm_object 2508dbca793STor Egge * and normal struct vm_page being type stable. 2518dbca793STor Egge */ 25285eeca35SAlan Cox static boolean_t 2538dbca793STor Egge vm_pageout_fallback_object_lock(vm_page_t m, vm_page_t *next) 2548dbca793STor Egge { 2558dbca793STor Egge struct vm_page marker; 2568d220203SAlan Cox struct vm_pagequeue *pq; 2578dbca793STor Egge boolean_t unchanged; 2588dbca793STor Egge u_short queue; 2598dbca793STor Egge vm_object_t object; 2608dbca793STor Egge 2618dbca793STor Egge queue = m->queue; 2628c616246SKonstantin Belousov vm_pageout_init_marker(&marker, queue); 263449c2e92SKonstantin Belousov pq = vm_page_pagequeue(m); 2648dbca793STor Egge object = m->object; 2658dbca793STor Egge 266c325e866SKonstantin Belousov TAILQ_INSERT_AFTER(&pq->pq_pl, m, &marker, plinks.q); 2678d220203SAlan Cox vm_pagequeue_unlock(pq); 2682965a453SKip Macy vm_page_unlock(m); 26989f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 2702965a453SKip Macy vm_page_lock(m); 2718d220203SAlan Cox vm_pagequeue_lock(pq); 2728dbca793STor Egge 2738dbca793STor Egge /* Page queue might have changed. */ 274c325e866SKonstantin Belousov *next = TAILQ_NEXT(&marker, plinks.q); 2758dbca793STor Egge unchanged = (m->queue == queue && 2768dbca793STor Egge m->object == object && 277c325e866SKonstantin Belousov &marker == TAILQ_NEXT(m, plinks.q)); 278c325e866SKonstantin Belousov TAILQ_REMOVE(&pq->pq_pl, &marker, plinks.q); 2798dbca793STor Egge return (unchanged); 2808dbca793STor Egge } 2818dbca793STor Egge 2828dbca793STor Egge /* 2838c616246SKonstantin Belousov * Lock the page while holding the page queue lock. Use marker page 2848c616246SKonstantin Belousov * to detect page queue changes and maintain notion of next page on 2858c616246SKonstantin Belousov * page queue. Return TRUE if no changes were detected, FALSE 2868c616246SKonstantin Belousov * otherwise. The page is locked on return. The page queue lock might 2878c616246SKonstantin Belousov * be dropped and reacquired. 2888c616246SKonstantin Belousov * 2898c616246SKonstantin Belousov * This function depends on normal struct vm_page being type stable. 2908c616246SKonstantin Belousov */ 29185eeca35SAlan Cox static boolean_t 2928c616246SKonstantin Belousov vm_pageout_page_lock(vm_page_t m, vm_page_t *next) 2938c616246SKonstantin Belousov { 2948c616246SKonstantin Belousov struct vm_page marker; 2958d220203SAlan Cox struct vm_pagequeue *pq; 2968c616246SKonstantin Belousov boolean_t unchanged; 2978c616246SKonstantin Belousov u_short queue; 2988c616246SKonstantin Belousov 2998c616246SKonstantin Belousov vm_page_lock_assert(m, MA_NOTOWNED); 3008c616246SKonstantin Belousov if (vm_page_trylock(m)) 3018c616246SKonstantin Belousov return (TRUE); 3028c616246SKonstantin Belousov 3038c616246SKonstantin Belousov queue = m->queue; 3048c616246SKonstantin Belousov vm_pageout_init_marker(&marker, queue); 305449c2e92SKonstantin Belousov pq = vm_page_pagequeue(m); 3068c616246SKonstantin Belousov 307c325e866SKonstantin Belousov TAILQ_INSERT_AFTER(&pq->pq_pl, m, &marker, plinks.q); 3088d220203SAlan Cox vm_pagequeue_unlock(pq); 3098c616246SKonstantin Belousov vm_page_lock(m); 3108d220203SAlan Cox vm_pagequeue_lock(pq); 3118c616246SKonstantin Belousov 3128c616246SKonstantin Belousov /* Page queue might have changed. */ 313c325e866SKonstantin Belousov *next = TAILQ_NEXT(&marker, plinks.q); 314c325e866SKonstantin Belousov unchanged = (m->queue == queue && &marker == TAILQ_NEXT(m, plinks.q)); 315c325e866SKonstantin Belousov TAILQ_REMOVE(&pq->pq_pl, &marker, plinks.q); 3168c616246SKonstantin Belousov return (unchanged); 3178c616246SKonstantin Belousov } 3188c616246SKonstantin Belousov 3198c616246SKonstantin Belousov /* 32026f9a767SRodney W. Grimes * vm_pageout_clean: 32124a1cce3SDavid Greenman * 3220d94caffSDavid Greenman * Clean the page and remove it from the laundry. 32326f9a767SRodney W. Grimes * 3240d94caffSDavid Greenman * We set the busy bit to cause potential page faults on this page to 3251c7c3c6aSMatthew Dillon * block. Note the careful timing, however, the busy bit isn't set till 3261c7c3c6aSMatthew Dillon * late and we cannot do anything that will mess with the page. 32726f9a767SRodney W. Grimes */ 3283af76890SPoul-Henning Kamp static int 3292965a453SKip Macy vm_pageout_clean(vm_page_t m) 33024a1cce3SDavid Greenman { 33154d92145SMatthew Dillon vm_object_t object; 33291b4f427SAlan Cox vm_page_t mc[2*vm_pageout_page_count], pb, ps; 3333562af12SAlan Cox int pageout_count; 33490ecac61SMatthew Dillon int ib, is, page_base; 335a316d390SJohn Dyson vm_pindex_t pindex = m->pindex; 33626f9a767SRodney W. Grimes 33795976f3fSAlan Cox vm_page_lock_assert(m, MA_OWNED); 33817f6a17bSAlan Cox object = m->object; 33989f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 3400cddd8f0SMatthew Dillon 34126f9a767SRodney W. Grimes /* 3421c7c3c6aSMatthew Dillon * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP 3431c7c3c6aSMatthew Dillon * with the new swapper, but we could have serious problems paging 3441c7c3c6aSMatthew Dillon * out other object types if there is insufficient memory. 3451c7c3c6aSMatthew Dillon * 3461c7c3c6aSMatthew Dillon * Unfortunately, checking free memory here is far too late, so the 3471c7c3c6aSMatthew Dillon * check has been moved up a procedural level. 3481c7c3c6aSMatthew Dillon */ 3491c7c3c6aSMatthew Dillon 35024a1cce3SDavid Greenman /* 3519e897b1bSAlan Cox * Can't clean the page if it's busy or held. 35224a1cce3SDavid Greenman */ 353c7aebda8SAttilio Rao vm_page_assert_unbusied(m); 35495976f3fSAlan Cox KASSERT(m->hold_count == 0, ("vm_pageout_clean: page %p is held", m)); 35517f6a17bSAlan Cox vm_page_unlock(m); 3560d94caffSDavid Greenman 35791b4f427SAlan Cox mc[vm_pageout_page_count] = pb = ps = m; 35826f9a767SRodney W. Grimes pageout_count = 1; 359f35329acSJohn Dyson page_base = vm_pageout_page_count; 36090ecac61SMatthew Dillon ib = 1; 36190ecac61SMatthew Dillon is = 1; 36290ecac61SMatthew Dillon 36324a1cce3SDavid Greenman /* 36424a1cce3SDavid Greenman * Scan object for clusterable pages. 36524a1cce3SDavid Greenman * 36624a1cce3SDavid Greenman * We can cluster ONLY if: ->> the page is NOT 36724a1cce3SDavid Greenman * clean, wired, busy, held, or mapped into a 36824a1cce3SDavid Greenman * buffer, and one of the following: 36924a1cce3SDavid Greenman * 1) The page is inactive, or a seldom used 37024a1cce3SDavid Greenman * active page. 37124a1cce3SDavid Greenman * -or- 37224a1cce3SDavid Greenman * 2) we force the issue. 37390ecac61SMatthew Dillon * 37490ecac61SMatthew Dillon * During heavy mmap/modification loads the pageout 37590ecac61SMatthew Dillon * daemon can really fragment the underlying file 37690ecac61SMatthew Dillon * due to flushing pages out of order and not trying 37790ecac61SMatthew Dillon * align the clusters (which leave sporatic out-of-order 37890ecac61SMatthew Dillon * holes). To solve this problem we do the reverse scan 37990ecac61SMatthew Dillon * first and attempt to align our cluster, then do a 38090ecac61SMatthew Dillon * forward scan if room remains. 38124a1cce3SDavid Greenman */ 38290ecac61SMatthew Dillon more: 38390ecac61SMatthew Dillon while (ib && pageout_count < vm_pageout_page_count) { 38424a1cce3SDavid Greenman vm_page_t p; 385f6b04d2bSDavid Greenman 38690ecac61SMatthew Dillon if (ib > pindex) { 38790ecac61SMatthew Dillon ib = 0; 38890ecac61SMatthew Dillon break; 389f6b04d2bSDavid Greenman } 39090ecac61SMatthew Dillon 391c7aebda8SAttilio Rao if ((p = vm_page_prev(pb)) == NULL || vm_page_busied(p)) { 39290ecac61SMatthew Dillon ib = 0; 39390ecac61SMatthew Dillon break; 394f6b04d2bSDavid Greenman } 3952965a453SKip Macy vm_page_lock(p); 39624a1cce3SDavid Greenman vm_page_test_dirty(p); 39726f4eea5SAlan Cox if (p->dirty == 0 || 39890ecac61SMatthew Dillon p->queue != PQ_INACTIVE || 39957601bcbSMatthew Dillon p->hold_count != 0) { /* may be undergoing I/O */ 4002965a453SKip Macy vm_page_unlock(p); 40190ecac61SMatthew Dillon ib = 0; 40224a1cce3SDavid Greenman break; 403f6b04d2bSDavid Greenman } 4042965a453SKip Macy vm_page_unlock(p); 40591b4f427SAlan Cox mc[--page_base] = pb = p; 40690ecac61SMatthew Dillon ++pageout_count; 40790ecac61SMatthew Dillon ++ib; 40824a1cce3SDavid Greenman /* 40990ecac61SMatthew Dillon * alignment boundry, stop here and switch directions. Do 41090ecac61SMatthew Dillon * not clear ib. 41124a1cce3SDavid Greenman */ 41290ecac61SMatthew Dillon if ((pindex - (ib - 1)) % vm_pageout_page_count == 0) 41390ecac61SMatthew Dillon break; 41424a1cce3SDavid Greenman } 41590ecac61SMatthew Dillon 41690ecac61SMatthew Dillon while (pageout_count < vm_pageout_page_count && 41790ecac61SMatthew Dillon pindex + is < object->size) { 41890ecac61SMatthew Dillon vm_page_t p; 41990ecac61SMatthew Dillon 420c7aebda8SAttilio Rao if ((p = vm_page_next(ps)) == NULL || vm_page_busied(p)) 42190ecac61SMatthew Dillon break; 4222965a453SKip Macy vm_page_lock(p); 42324a1cce3SDavid Greenman vm_page_test_dirty(p); 42426f4eea5SAlan Cox if (p->dirty == 0 || 42590ecac61SMatthew Dillon p->queue != PQ_INACTIVE || 42657601bcbSMatthew Dillon p->hold_count != 0) { /* may be undergoing I/O */ 4272965a453SKip Macy vm_page_unlock(p); 42824a1cce3SDavid Greenman break; 42924a1cce3SDavid Greenman } 4302965a453SKip Macy vm_page_unlock(p); 43191b4f427SAlan Cox mc[page_base + pageout_count] = ps = p; 43290ecac61SMatthew Dillon ++pageout_count; 43390ecac61SMatthew Dillon ++is; 43424a1cce3SDavid Greenman } 43590ecac61SMatthew Dillon 43690ecac61SMatthew Dillon /* 43790ecac61SMatthew Dillon * If we exhausted our forward scan, continue with the reverse scan 43890ecac61SMatthew Dillon * when possible, even past a page boundry. This catches boundry 43990ecac61SMatthew Dillon * conditions. 44090ecac61SMatthew Dillon */ 44190ecac61SMatthew Dillon if (ib && pageout_count < vm_pageout_page_count) 44290ecac61SMatthew Dillon goto more; 443f6b04d2bSDavid Greenman 44467bf6868SJohn Dyson /* 44567bf6868SJohn Dyson * we allow reads during pageouts... 44667bf6868SJohn Dyson */ 447126d6082SKonstantin Belousov return (vm_pageout_flush(&mc[page_base], pageout_count, 0, 0, NULL, 448126d6082SKonstantin Belousov NULL)); 449aef922f5SJohn Dyson } 450aef922f5SJohn Dyson 4511c7c3c6aSMatthew Dillon /* 4521c7c3c6aSMatthew Dillon * vm_pageout_flush() - launder the given pages 4531c7c3c6aSMatthew Dillon * 4541c7c3c6aSMatthew Dillon * The given pages are laundered. Note that we setup for the start of 4551c7c3c6aSMatthew Dillon * I/O ( i.e. busy the page ), mark it read-only, and bump the object 4561c7c3c6aSMatthew Dillon * reference count all in here rather then in the parent. If we want 4571c7c3c6aSMatthew Dillon * the parent to do more sophisticated things we may have to change 4581c7c3c6aSMatthew Dillon * the ordering. 4591e8a675cSKonstantin Belousov * 4601e8a675cSKonstantin Belousov * Returned runlen is the count of pages between mreq and first 4611e8a675cSKonstantin Belousov * page after mreq with status VM_PAGER_AGAIN. 462126d6082SKonstantin Belousov * *eio is set to TRUE if pager returned VM_PAGER_ERROR or VM_PAGER_FAIL 463126d6082SKonstantin Belousov * for any page in runlen set. 4641c7c3c6aSMatthew Dillon */ 465aef922f5SJohn Dyson int 466126d6082SKonstantin Belousov vm_pageout_flush(vm_page_t *mc, int count, int flags, int mreq, int *prunlen, 467126d6082SKonstantin Belousov boolean_t *eio) 468aef922f5SJohn Dyson { 4692e3b314dSAlan Cox vm_object_t object = mc[0]->object; 470aef922f5SJohn Dyson int pageout_status[count]; 47195461b45SJohn Dyson int numpagedout = 0; 4721e8a675cSKonstantin Belousov int i, runlen; 473aef922f5SJohn Dyson 47489f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 4757bec141bSKip Macy 4761c7c3c6aSMatthew Dillon /* 4771c7c3c6aSMatthew Dillon * Initiate I/O. Bump the vm_page_t->busy counter and 4781c7c3c6aSMatthew Dillon * mark the pages read-only. 4791c7c3c6aSMatthew Dillon * 4801c7c3c6aSMatthew Dillon * We do not have to fixup the clean/dirty bits here... we can 4811c7c3c6aSMatthew Dillon * allow the pager to do it after the I/O completes. 48202fa91d3SMatthew Dillon * 48302fa91d3SMatthew Dillon * NOTE! mc[i]->dirty may be partial or fragmented due to an 48402fa91d3SMatthew Dillon * edge case with file fragments. 4851c7c3c6aSMatthew Dillon */ 4868f9110f6SJohn Dyson for (i = 0; i < count; i++) { 4877a935082SAlan Cox KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL, 4887a935082SAlan Cox ("vm_pageout_flush: partially invalid page %p index %d/%d", 4897a935082SAlan Cox mc[i], i, count)); 490c7aebda8SAttilio Rao vm_page_sbusy(mc[i]); 49178985e42SAlan Cox pmap_remove_write(mc[i]); 4922965a453SKip Macy } 493d474eaaaSDoug Rabson vm_object_pip_add(object, count); 494aef922f5SJohn Dyson 495d076fbeaSAlan Cox vm_pager_put_pages(object, mc, count, flags, pageout_status); 49626f9a767SRodney W. Grimes 4971e8a675cSKonstantin Belousov runlen = count - mreq; 498126d6082SKonstantin Belousov if (eio != NULL) 499126d6082SKonstantin Belousov *eio = FALSE; 500aef922f5SJohn Dyson for (i = 0; i < count; i++) { 501aef922f5SJohn Dyson vm_page_t mt = mc[i]; 50224a1cce3SDavid Greenman 5034cd45723SAlan Cox KASSERT(pageout_status[i] == VM_PAGER_PEND || 5046031c68dSAlan Cox !pmap_page_is_write_mapped(mt), 5059ea8d1a6SAlan Cox ("vm_pageout_flush: page %p is not write protected", mt)); 50626f9a767SRodney W. Grimes switch (pageout_status[i]) { 50726f9a767SRodney W. Grimes case VM_PAGER_OK: 50826f9a767SRodney W. Grimes case VM_PAGER_PEND: 50995461b45SJohn Dyson numpagedout++; 51026f9a767SRodney W. Grimes break; 51126f9a767SRodney W. Grimes case VM_PAGER_BAD: 51226f9a767SRodney W. Grimes /* 5130d94caffSDavid Greenman * Page outside of range of object. Right now we 5140d94caffSDavid Greenman * essentially lose the changes by pretending it 5150d94caffSDavid Greenman * worked. 51626f9a767SRodney W. Grimes */ 51790ecac61SMatthew Dillon vm_page_undirty(mt); 51826f9a767SRodney W. Grimes break; 51926f9a767SRodney W. Grimes case VM_PAGER_ERROR: 52026f9a767SRodney W. Grimes case VM_PAGER_FAIL: 52126f9a767SRodney W. Grimes /* 5220d94caffSDavid Greenman * If page couldn't be paged out, then reactivate the 5230d94caffSDavid Greenman * page so it doesn't clog the inactive list. (We 5240d94caffSDavid Greenman * will try paging out it again later). 52526f9a767SRodney W. Grimes */ 5263c4a2440SAlan Cox vm_page_lock(mt); 52724a1cce3SDavid Greenman vm_page_activate(mt); 5283c4a2440SAlan Cox vm_page_unlock(mt); 529126d6082SKonstantin Belousov if (eio != NULL && i >= mreq && i - mreq < runlen) 530126d6082SKonstantin Belousov *eio = TRUE; 53126f9a767SRodney W. Grimes break; 53226f9a767SRodney W. Grimes case VM_PAGER_AGAIN: 5331e8a675cSKonstantin Belousov if (i >= mreq && i - mreq < runlen) 5341e8a675cSKonstantin Belousov runlen = i - mreq; 53526f9a767SRodney W. Grimes break; 53626f9a767SRodney W. Grimes } 53726f9a767SRodney W. Grimes 53826f9a767SRodney W. Grimes /* 5390d94caffSDavid Greenman * If the operation is still going, leave the page busy to 5400d94caffSDavid Greenman * block all other accesses. Also, leave the paging in 5410d94caffSDavid Greenman * progress indicator set so that we don't attempt an object 5420d94caffSDavid Greenman * collapse. 54326f9a767SRodney W. Grimes */ 54426f9a767SRodney W. Grimes if (pageout_status[i] != VM_PAGER_PEND) { 545f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 546c7aebda8SAttilio Rao vm_page_sunbusy(mt); 5473c4a2440SAlan Cox if (vm_page_count_severe()) { 5483c4a2440SAlan Cox vm_page_lock(mt); 5499ea8d1a6SAlan Cox vm_page_try_to_cache(mt); 5502965a453SKip Macy vm_page_unlock(mt); 55126f9a767SRodney W. Grimes } 5523c4a2440SAlan Cox } 5533c4a2440SAlan Cox } 5541e8a675cSKonstantin Belousov if (prunlen != NULL) 5551e8a675cSKonstantin Belousov *prunlen = runlen; 5563c4a2440SAlan Cox return (numpagedout); 55726f9a767SRodney W. Grimes } 55826f9a767SRodney W. Grimes 55985eeca35SAlan Cox static boolean_t 560449c2e92SKonstantin Belousov vm_pageout_launder(struct vm_pagequeue *pq, int tries, vm_paddr_t low, 561449c2e92SKonstantin Belousov vm_paddr_t high) 56285eeca35SAlan Cox { 56385eeca35SAlan Cox struct mount *mp; 56485eeca35SAlan Cox struct vnode *vp; 56585eeca35SAlan Cox vm_object_t object; 56685eeca35SAlan Cox vm_paddr_t pa; 56785eeca35SAlan Cox vm_page_t m, m_tmp, next; 56885eeca35SAlan Cox 5698d220203SAlan Cox vm_pagequeue_lock(pq); 570c325e866SKonstantin Belousov TAILQ_FOREACH_SAFE(m, &pq->pq_pl, plinks.q, next) { 57185eeca35SAlan Cox if ((m->flags & PG_MARKER) != 0) 57285eeca35SAlan Cox continue; 57385eeca35SAlan Cox pa = VM_PAGE_TO_PHYS(m); 57485eeca35SAlan Cox if (pa < low || pa + PAGE_SIZE > high) 57585eeca35SAlan Cox continue; 57685eeca35SAlan Cox if (!vm_pageout_page_lock(m, &next) || m->hold_count != 0) { 57785eeca35SAlan Cox vm_page_unlock(m); 57885eeca35SAlan Cox continue; 57985eeca35SAlan Cox } 58085eeca35SAlan Cox object = m->object; 58189f6b863SAttilio Rao if ((!VM_OBJECT_TRYWLOCK(object) && 58285eeca35SAlan Cox (!vm_pageout_fallback_object_lock(m, &next) || 583c7aebda8SAttilio Rao m->hold_count != 0)) || vm_page_busied(m)) { 58485eeca35SAlan Cox vm_page_unlock(m); 58589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 58685eeca35SAlan Cox continue; 58785eeca35SAlan Cox } 58885eeca35SAlan Cox vm_page_test_dirty(m); 5899fc4739dSAlan Cox if (m->dirty == 0 && object->ref_count != 0) 59085eeca35SAlan Cox pmap_remove_all(m); 59185eeca35SAlan Cox if (m->dirty != 0) { 59285eeca35SAlan Cox vm_page_unlock(m); 59385eeca35SAlan Cox if (tries == 0 || (object->flags & OBJ_DEAD) != 0) { 59489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 59585eeca35SAlan Cox continue; 59685eeca35SAlan Cox } 59785eeca35SAlan Cox if (object->type == OBJT_VNODE) { 5988d220203SAlan Cox vm_pagequeue_unlock(pq); 59985eeca35SAlan Cox vp = object->handle; 60085eeca35SAlan Cox vm_object_reference_locked(object); 60189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 60285eeca35SAlan Cox (void)vn_start_write(vp, &mp, V_WAIT); 60385eeca35SAlan Cox vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 60489f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 60585eeca35SAlan Cox vm_object_page_clean(object, 0, 0, OBJPC_SYNC); 60689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 60785eeca35SAlan Cox VOP_UNLOCK(vp, 0); 60885eeca35SAlan Cox vm_object_deallocate(object); 60985eeca35SAlan Cox vn_finished_write(mp); 61085eeca35SAlan Cox return (TRUE); 61185eeca35SAlan Cox } else if (object->type == OBJT_SWAP || 61285eeca35SAlan Cox object->type == OBJT_DEFAULT) { 6138d220203SAlan Cox vm_pagequeue_unlock(pq); 61485eeca35SAlan Cox m_tmp = m; 61585eeca35SAlan Cox vm_pageout_flush(&m_tmp, 1, VM_PAGER_PUT_SYNC, 61685eeca35SAlan Cox 0, NULL, NULL); 61789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 61885eeca35SAlan Cox return (TRUE); 61985eeca35SAlan Cox } 62085eeca35SAlan Cox } else { 6218d220203SAlan Cox /* 6228d220203SAlan Cox * Dequeue here to prevent lock recursion in 6238d220203SAlan Cox * vm_page_cache(). 6248d220203SAlan Cox */ 6258d220203SAlan Cox vm_page_dequeue_locked(m); 62685eeca35SAlan Cox vm_page_cache(m); 62785eeca35SAlan Cox vm_page_unlock(m); 62885eeca35SAlan Cox } 62989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 63085eeca35SAlan Cox } 6318d220203SAlan Cox vm_pagequeue_unlock(pq); 63285eeca35SAlan Cox return (FALSE); 63385eeca35SAlan Cox } 63485eeca35SAlan Cox 63585eeca35SAlan Cox /* 63685eeca35SAlan Cox * Increase the number of cached pages. The specified value, "tries", 63785eeca35SAlan Cox * determines which categories of pages are cached: 63885eeca35SAlan Cox * 63985eeca35SAlan Cox * 0: All clean, inactive pages within the specified physical address range 64085eeca35SAlan Cox * are cached. Will not sleep. 64185eeca35SAlan Cox * 1: The vm_lowmem handlers are called. All inactive pages within 64285eeca35SAlan Cox * the specified physical address range are cached. May sleep. 64385eeca35SAlan Cox * 2: The vm_lowmem handlers are called. All inactive and active pages 64485eeca35SAlan Cox * within the specified physical address range are cached. May sleep. 64585eeca35SAlan Cox */ 64685eeca35SAlan Cox void 64785eeca35SAlan Cox vm_pageout_grow_cache(int tries, vm_paddr_t low, vm_paddr_t high) 64885eeca35SAlan Cox { 649449c2e92SKonstantin Belousov int actl, actmax, inactl, inactmax, dom, initial_dom; 650449c2e92SKonstantin Belousov static int start_dom = 0; 65185eeca35SAlan Cox 65285eeca35SAlan Cox if (tries > 0) { 65385eeca35SAlan Cox /* 65485eeca35SAlan Cox * Decrease registered cache sizes. The vm_lowmem handlers 65585eeca35SAlan Cox * may acquire locks and/or sleep, so they can only be invoked 65685eeca35SAlan Cox * when "tries" is greater than zero. 65785eeca35SAlan Cox */ 65885eeca35SAlan Cox EVENTHANDLER_INVOKE(vm_lowmem, 0); 65985eeca35SAlan Cox 66085eeca35SAlan Cox /* 66185eeca35SAlan Cox * We do this explicitly after the caches have been drained 66285eeca35SAlan Cox * above. 66385eeca35SAlan Cox */ 66485eeca35SAlan Cox uma_reclaim(); 66585eeca35SAlan Cox } 666449c2e92SKonstantin Belousov 667449c2e92SKonstantin Belousov /* 668449c2e92SKonstantin Belousov * Make the next scan start on the next domain. 669449c2e92SKonstantin Belousov */ 670449c2e92SKonstantin Belousov initial_dom = atomic_fetchadd_int(&start_dom, 1) % vm_ndomains; 671449c2e92SKonstantin Belousov 67285eeca35SAlan Cox inactl = 0; 67385eeca35SAlan Cox inactmax = cnt.v_inactive_count; 67485eeca35SAlan Cox actl = 0; 67585eeca35SAlan Cox actmax = tries < 2 ? 0 : cnt.v_active_count; 676449c2e92SKonstantin Belousov dom = initial_dom; 677449c2e92SKonstantin Belousov 678449c2e92SKonstantin Belousov /* 679449c2e92SKonstantin Belousov * Scan domains in round-robin order, first inactive queues, 680449c2e92SKonstantin Belousov * then active. Since domain usually owns large physically 681449c2e92SKonstantin Belousov * contiguous chunk of memory, it makes sense to completely 682449c2e92SKonstantin Belousov * exhaust one domain before switching to next, while growing 683449c2e92SKonstantin Belousov * the pool of contiguous physical pages. 684449c2e92SKonstantin Belousov * 685449c2e92SKonstantin Belousov * Do not even start launder a domain which cannot contain 686449c2e92SKonstantin Belousov * the specified address range, as indicated by segments 687449c2e92SKonstantin Belousov * constituting the domain. 688449c2e92SKonstantin Belousov */ 68985eeca35SAlan Cox again: 690449c2e92SKonstantin Belousov if (inactl < inactmax) { 691449c2e92SKonstantin Belousov if (vm_phys_domain_intersects(vm_dom[dom].vmd_segs, 692449c2e92SKonstantin Belousov low, high) && 693449c2e92SKonstantin Belousov vm_pageout_launder(&vm_dom[dom].vmd_pagequeues[PQ_INACTIVE], 694449c2e92SKonstantin Belousov tries, low, high)) { 69585eeca35SAlan Cox inactl++; 69685eeca35SAlan Cox goto again; 69785eeca35SAlan Cox } 698449c2e92SKonstantin Belousov if (++dom == vm_ndomains) 699449c2e92SKonstantin Belousov dom = 0; 700449c2e92SKonstantin Belousov if (dom != initial_dom) 701449c2e92SKonstantin Belousov goto again; 702449c2e92SKonstantin Belousov } 703449c2e92SKonstantin Belousov if (actl < actmax) { 704449c2e92SKonstantin Belousov if (vm_phys_domain_intersects(vm_dom[dom].vmd_segs, 705449c2e92SKonstantin Belousov low, high) && 706449c2e92SKonstantin Belousov vm_pageout_launder(&vm_dom[dom].vmd_pagequeues[PQ_ACTIVE], 707449c2e92SKonstantin Belousov tries, low, high)) { 70885eeca35SAlan Cox actl++; 70985eeca35SAlan Cox goto again; 71085eeca35SAlan Cox } 711449c2e92SKonstantin Belousov if (++dom == vm_ndomains) 712449c2e92SKonstantin Belousov dom = 0; 713449c2e92SKonstantin Belousov if (dom != initial_dom) 714449c2e92SKonstantin Belousov goto again; 715449c2e92SKonstantin Belousov } 71685eeca35SAlan Cox } 71785eeca35SAlan Cox 71838efa82bSJohn Dyson #if !defined(NO_SWAPPING) 71926f9a767SRodney W. Grimes /* 72026f9a767SRodney W. Grimes * vm_pageout_object_deactivate_pages 72126f9a767SRodney W. Grimes * 722ce186587SAlan Cox * Deactivate enough pages to satisfy the inactive target 723ce186587SAlan Cox * requirements. 72426f9a767SRodney W. Grimes * 72526f9a767SRodney W. Grimes * The object and map must be locked. 72626f9a767SRodney W. Grimes */ 72738efa82bSJohn Dyson static void 728ce186587SAlan Cox vm_pageout_object_deactivate_pages(pmap_t pmap, vm_object_t first_object, 729ce186587SAlan Cox long desired) 73026f9a767SRodney W. Grimes { 731ecf6279fSAlan Cox vm_object_t backing_object, object; 732ce186587SAlan Cox vm_page_t p; 733bb7858eaSJeff Roberson int act_delta, remove_mode; 73426f9a767SRodney W. Grimes 735e23b0a19SAlan Cox VM_OBJECT_ASSERT_LOCKED(first_object); 73628634820SAlan Cox if ((first_object->flags & OBJ_FICTITIOUS) != 0) 73738efa82bSJohn Dyson return; 738ecf6279fSAlan Cox for (object = first_object;; object = backing_object) { 739ecf6279fSAlan Cox if (pmap_resident_count(pmap) <= desired) 740ecf6279fSAlan Cox goto unlock_return; 741e23b0a19SAlan Cox VM_OBJECT_ASSERT_LOCKED(object); 74228634820SAlan Cox if ((object->flags & OBJ_UNMANAGED) != 0 || 74328634820SAlan Cox object->paging_in_progress != 0) 744ecf6279fSAlan Cox goto unlock_return; 74526f9a767SRodney W. Grimes 74685b1dc89SAlan Cox remove_mode = 0; 74738efa82bSJohn Dyson if (object->shadow_count > 1) 74838efa82bSJohn Dyson remove_mode = 1; 74926f9a767SRodney W. Grimes /* 750ce186587SAlan Cox * Scan the object's entire memory queue. 75126f9a767SRodney W. Grimes */ 752ce186587SAlan Cox TAILQ_FOREACH(p, &object->memq, listq) { 753447fe2a4SAlan Cox if (pmap_resident_count(pmap) <= desired) 754447fe2a4SAlan Cox goto unlock_return; 755c7aebda8SAttilio Rao if (vm_page_busied(p)) 756447fe2a4SAlan Cox continue; 757ce186587SAlan Cox PCPU_INC(cnt.v_pdpages); 7582965a453SKip Macy vm_page_lock(p); 759ce186587SAlan Cox if (p->wire_count != 0 || p->hold_count != 0 || 760ecf6279fSAlan Cox !pmap_page_exists_quick(pmap, p)) { 7612965a453SKip Macy vm_page_unlock(p); 7620d94caffSDavid Greenman continue; 7630d94caffSDavid Greenman } 764bb7858eaSJeff Roberson act_delta = pmap_ts_referenced(p); 7653407fefeSKonstantin Belousov if ((p->aflags & PGA_REFERENCED) != 0) { 766bb7858eaSJeff Roberson if (act_delta == 0) 767bb7858eaSJeff Roberson act_delta = 1; 7683407fefeSKonstantin Belousov vm_page_aflag_clear(p, PGA_REFERENCED); 769ef743ce6SJohn Dyson } 770bb7858eaSJeff Roberson if (p->queue != PQ_ACTIVE && act_delta != 0) { 771ef743ce6SJohn Dyson vm_page_activate(p); 772bb7858eaSJeff Roberson p->act_count += act_delta; 773c8c4b40cSJohn Dyson } else if (p->queue == PQ_ACTIVE) { 774bb7858eaSJeff Roberson if (act_delta == 0) { 775ce186587SAlan Cox p->act_count -= min(p->act_count, 776ce186587SAlan Cox ACT_DECLINE); 77790776bd7SJeff Roberson if (!remove_mode && p->act_count == 0) { 7784fec79beSAlan Cox pmap_remove_all(p); 77926f9a767SRodney W. Grimes vm_page_deactivate(p); 7808d220203SAlan Cox } else 7818d220203SAlan Cox vm_page_requeue(p); 782c8c4b40cSJohn Dyson } else { 783eaf13dd7SJohn Dyson vm_page_activate(p); 784ce186587SAlan Cox if (p->act_count < ACT_MAX - 785ce186587SAlan Cox ACT_ADVANCE) 78638efa82bSJohn Dyson p->act_count += ACT_ADVANCE; 7878d220203SAlan Cox vm_page_requeue(p); 788ce186587SAlan Cox } 789ce186587SAlan Cox } else if (p->queue == PQ_INACTIVE) 790ce186587SAlan Cox pmap_remove_all(p); 7912965a453SKip Macy vm_page_unlock(p); 79226f9a767SRodney W. Grimes } 793ecf6279fSAlan Cox if ((backing_object = object->backing_object) == NULL) 794ecf6279fSAlan Cox goto unlock_return; 795e23b0a19SAlan Cox VM_OBJECT_RLOCK(backing_object); 796ecf6279fSAlan Cox if (object != first_object) 797e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(object); 79838efa82bSJohn Dyson } 799ecf6279fSAlan Cox unlock_return: 800ecf6279fSAlan Cox if (object != first_object) 801e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(object); 80226f9a767SRodney W. Grimes } 80326f9a767SRodney W. Grimes 80426f9a767SRodney W. Grimes /* 80526f9a767SRodney W. Grimes * deactivate some number of pages in a map, try to do it fairly, but 80626f9a767SRodney W. Grimes * that is really hard to do. 80726f9a767SRodney W. Grimes */ 808cd41fc12SDavid Greenman static void 80938efa82bSJohn Dyson vm_pageout_map_deactivate_pages(map, desired) 81026f9a767SRodney W. Grimes vm_map_t map; 811ecf6279fSAlan Cox long desired; 81226f9a767SRodney W. Grimes { 81326f9a767SRodney W. Grimes vm_map_entry_t tmpe; 81438efa82bSJohn Dyson vm_object_t obj, bigobj; 81530105b9eSTor Egge int nothingwired; 8160d94caffSDavid Greenman 817d974f03cSAlan Cox if (!vm_map_trylock(map)) 81826f9a767SRodney W. Grimes return; 81938efa82bSJohn Dyson 82038efa82bSJohn Dyson bigobj = NULL; 82130105b9eSTor Egge nothingwired = TRUE; 82238efa82bSJohn Dyson 82338efa82bSJohn Dyson /* 82438efa82bSJohn Dyson * first, search out the biggest object, and try to free pages from 82538efa82bSJohn Dyson * that. 82638efa82bSJohn Dyson */ 82726f9a767SRodney W. Grimes tmpe = map->header.next; 82838efa82bSJohn Dyson while (tmpe != &map->header) { 8299fdfe602SMatthew Dillon if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 83038efa82bSJohn Dyson obj = tmpe->object.vm_object; 831e23b0a19SAlan Cox if (obj != NULL && VM_OBJECT_TRYRLOCK(obj)) { 8320774dfb3SAlan Cox if (obj->shadow_count <= 1 && 8330774dfb3SAlan Cox (bigobj == NULL || 8340774dfb3SAlan Cox bigobj->resident_page_count < obj->resident_page_count)) { 8350774dfb3SAlan Cox if (bigobj != NULL) 836e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(bigobj); 83738efa82bSJohn Dyson bigobj = obj; 8380774dfb3SAlan Cox } else 839e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(obj); 84038efa82bSJohn Dyson } 84138efa82bSJohn Dyson } 84230105b9eSTor Egge if (tmpe->wired_count > 0) 84330105b9eSTor Egge nothingwired = FALSE; 84438efa82bSJohn Dyson tmpe = tmpe->next; 84538efa82bSJohn Dyson } 84638efa82bSJohn Dyson 8470774dfb3SAlan Cox if (bigobj != NULL) { 848ecf6279fSAlan Cox vm_pageout_object_deactivate_pages(map->pmap, bigobj, desired); 849e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(bigobj); 8500774dfb3SAlan Cox } 85138efa82bSJohn Dyson /* 85238efa82bSJohn Dyson * Next, hunt around for other pages to deactivate. We actually 85338efa82bSJohn Dyson * do this search sort of wrong -- .text first is not the best idea. 85438efa82bSJohn Dyson */ 85538efa82bSJohn Dyson tmpe = map->header.next; 85638efa82bSJohn Dyson while (tmpe != &map->header) { 857b1028ad1SLuoqi Chen if (pmap_resident_count(vm_map_pmap(map)) <= desired) 85838efa82bSJohn Dyson break; 8599fdfe602SMatthew Dillon if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 86038efa82bSJohn Dyson obj = tmpe->object.vm_object; 8610774dfb3SAlan Cox if (obj != NULL) { 862e23b0a19SAlan Cox VM_OBJECT_RLOCK(obj); 863ecf6279fSAlan Cox vm_pageout_object_deactivate_pages(map->pmap, obj, desired); 864e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(obj); 8650774dfb3SAlan Cox } 86638efa82bSJohn Dyson } 86726f9a767SRodney W. Grimes tmpe = tmpe->next; 86838857e7fSAlan Cox } 86938efa82bSJohn Dyson 87038efa82bSJohn Dyson /* 87138efa82bSJohn Dyson * Remove all mappings if a process is swapped out, this will free page 87238efa82bSJohn Dyson * table pages. 87338efa82bSJohn Dyson */ 87438857e7fSAlan Cox if (desired == 0 && nothingwired) { 8758d01a3b2SNathan Whitehorn pmap_remove(vm_map_pmap(map), vm_map_min(map), 8768d01a3b2SNathan Whitehorn vm_map_max(map)); 87738857e7fSAlan Cox } 87838efa82bSJohn Dyson vm_map_unlock(map); 87926f9a767SRodney W. Grimes } 880a1287949SEivind Eklund #endif /* !defined(NO_SWAPPING) */ 881df8bae1dSRodney W. Grimes 8821c7c3c6aSMatthew Dillon /* 883df8bae1dSRodney W. Grimes * vm_pageout_scan does the dirty work for the pageout daemon. 884*d9e23210SJeff Roberson * 885*d9e23210SJeff Roberson * pass 0 - Update active LRU/deactivate pages 886*d9e23210SJeff Roberson * pass 1 - Move inactive to cache or free 887*d9e23210SJeff Roberson * pass 2 - Launder dirty pages 888df8bae1dSRodney W. Grimes */ 8892b6b0df7SMatthew Dillon static void 890449c2e92SKonstantin Belousov vm_pageout_scan(struct vm_domain *vmd, int pass) 891df8bae1dSRodney W. Grimes { 892502ba6e4SJohn Dyson vm_page_t m, next; 8938d220203SAlan Cox struct vm_pagequeue *pq; 8941c7c3c6aSMatthew Dillon int page_shortage, maxscan, pcount; 8952cba1ccdSAlan Cox int addl_page_shortage; 896df8bae1dSRodney W. Grimes vm_object_t object; 897bb7858eaSJeff Roberson int act_delta; 898f6b04d2bSDavid Greenman int vnodes_skipped = 0; 8992b6b0df7SMatthew Dillon int maxlaunder; 90048cc2fc7SKonstantin Belousov boolean_t queues_locked; 9010d94caffSDavid Greenman 902df8bae1dSRodney W. Grimes /* 903*d9e23210SJeff Roberson * If we need to reclaim memory ask kernel caches to return 904*d9e23210SJeff Roberson * some. 905*d9e23210SJeff Roberson */ 906*d9e23210SJeff Roberson if (pass > 0) { 907*d9e23210SJeff Roberson /* 908855a310fSJeff Roberson * Decrease registered cache sizes. 909855a310fSJeff Roberson */ 910855a310fSJeff Roberson EVENTHANDLER_INVOKE(vm_lowmem, 0); 911855a310fSJeff Roberson /* 912*d9e23210SJeff Roberson * We do this explicitly after the caches have been 913*d9e23210SJeff Roberson * drained above. 914855a310fSJeff Roberson */ 915855a310fSJeff Roberson uma_reclaim(); 916*d9e23210SJeff Roberson } 9175985940eSJohn Dyson 918311e34e2SKonstantin Belousov /* 91996240c89SEitan Adler * The addl_page_shortage is the number of temporarily 920311e34e2SKonstantin Belousov * stuck pages in the inactive queue. In other words, the 921449c2e92SKonstantin Belousov * number of pages from the inactive count that should be 922311e34e2SKonstantin Belousov * discounted in setting the target for the active queue scan. 923311e34e2SKonstantin Belousov */ 9242cba1ccdSAlan Cox addl_page_shortage = atomic_readandclear_int(&vm_pageout_deficit); 925b182ec9eSJohn Dyson 9261c7c3c6aSMatthew Dillon /* 9271c7c3c6aSMatthew Dillon * Calculate the number of pages we want to either free or move 9282b6b0df7SMatthew Dillon * to the cache. 9291c7c3c6aSMatthew Dillon */ 9302cba1ccdSAlan Cox page_shortage = vm_paging_target() + addl_page_shortage; 9311c7c3c6aSMatthew Dillon 932936524aaSMatthew Dillon /* 9332b6b0df7SMatthew Dillon * maxlaunder limits the number of dirty pages we flush per scan. 9342b6b0df7SMatthew Dillon * For most systems a smaller value (16 or 32) is more robust under 9352b6b0df7SMatthew Dillon * extreme memory and disk pressure because any unnecessary writes 9362b6b0df7SMatthew Dillon * to disk can result in extreme performance degredation. However, 9372b6b0df7SMatthew Dillon * systems with excessive dirty pages (especially when MAP_NOSYNC is 9382b6b0df7SMatthew Dillon * used) will die horribly with limited laundering. If the pageout 9392b6b0df7SMatthew Dillon * daemon cannot clean enough pages in the first pass, we let it go 9402b6b0df7SMatthew Dillon * all out in succeeding passes. 9411c7c3c6aSMatthew Dillon */ 9422b6b0df7SMatthew Dillon if ((maxlaunder = vm_max_launder) <= 1) 9432b6b0df7SMatthew Dillon maxlaunder = 1; 944*d9e23210SJeff Roberson if (pass > 1) 9452b6b0df7SMatthew Dillon maxlaunder = 10000; 9468d220203SAlan Cox 9478d220203SAlan Cox /* 9488d220203SAlan Cox * Start scanning the inactive queue for pages we can move to the 9498d220203SAlan Cox * cache or free. The scan will stop when the target is reached or 9508d220203SAlan Cox * we have scanned the entire inactive queue. Note that m->act_count 9518d220203SAlan Cox * is not used to form decisions for the inactive queue, only for the 9528d220203SAlan Cox * active queue. 9538d220203SAlan Cox */ 954449c2e92SKonstantin Belousov pq = &vmd->vmd_pagequeues[PQ_INACTIVE]; 955449c2e92SKonstantin Belousov maxscan = pq->pq_cnt; 9568d220203SAlan Cox vm_pagequeue_lock(pq); 9578d220203SAlan Cox queues_locked = TRUE; 9588d220203SAlan Cox for (m = TAILQ_FIRST(&pq->pq_pl); 9591c7c3c6aSMatthew Dillon m != NULL && maxscan-- > 0 && page_shortage > 0; 960e929c00dSKirk McKusick m = next) { 9618d220203SAlan Cox vm_pagequeue_assert_locked(pq); 96248cc2fc7SKonstantin Belousov KASSERT(queues_locked, ("unlocked queues")); 963d4961bcbSKonstantin Belousov KASSERT(m->queue == PQ_INACTIVE, ("Inactive queue %p", m)); 964df8bae1dSRodney W. Grimes 9658d220203SAlan Cox PCPU_INC(cnt.v_pdpages); 966c325e866SKonstantin Belousov next = TAILQ_NEXT(m, plinks.q); 967df8bae1dSRodney W. Grimes 968936524aaSMatthew Dillon /* 969936524aaSMatthew Dillon * skip marker pages 970936524aaSMatthew Dillon */ 971936524aaSMatthew Dillon if (m->flags & PG_MARKER) 972936524aaSMatthew Dillon continue; 973936524aaSMatthew Dillon 9747900f95dSKonstantin Belousov KASSERT((m->flags & PG_FICTITIOUS) == 0, 9757900f95dSKonstantin Belousov ("Fictitious page %p cannot be in inactive queue", m)); 9767900f95dSKonstantin Belousov KASSERT((m->oflags & VPO_UNMANAGED) == 0, 9777900f95dSKonstantin Belousov ("Unmanaged page %p cannot be in inactive queue", m)); 9787900f95dSKonstantin Belousov 9798c616246SKonstantin Belousov /* 980311e34e2SKonstantin Belousov * The page or object lock acquisitions fail if the 981311e34e2SKonstantin Belousov * page was removed from the queue or moved to a 982311e34e2SKonstantin Belousov * different position within the queue. In either 983311e34e2SKonstantin Belousov * case, addl_page_shortage should not be incremented. 9848c616246SKonstantin Belousov */ 9858c616246SKonstantin Belousov if (!vm_pageout_page_lock(m, &next)) { 9868c616246SKonstantin Belousov vm_page_unlock(m); 987b182ec9eSJohn Dyson continue; 988df8bae1dSRodney W. Grimes } 9899ee2165fSAlan Cox object = m->object; 99089f6b863SAttilio Rao if (!VM_OBJECT_TRYWLOCK(object) && 991311e34e2SKonstantin Belousov !vm_pageout_fallback_object_lock(m, &next)) { 9922965a453SKip Macy vm_page_unlock(m); 99389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 99434d9e6fdSAlan Cox continue; 99534d9e6fdSAlan Cox } 996311e34e2SKonstantin Belousov 997311e34e2SKonstantin Belousov /* 998311e34e2SKonstantin Belousov * Don't mess with busy pages, keep them at at the 999311e34e2SKonstantin Belousov * front of the queue, most likely they are being 1000311e34e2SKonstantin Belousov * paged out. Increment addl_page_shortage for busy 1001311e34e2SKonstantin Belousov * pages, because they may leave the inactive queue 1002311e34e2SKonstantin Belousov * shortly after page scan is finished. 1003311e34e2SKonstantin Belousov */ 1004c7aebda8SAttilio Rao if (vm_page_busied(m)) { 10052965a453SKip Macy vm_page_unlock(m); 100689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1007b182ec9eSJohn Dyson addl_page_shortage++; 100826f9a767SRodney W. Grimes continue; 100926f9a767SRodney W. Grimes } 1010bd7e5f99SJohn Dyson 10117e006499SJohn Dyson /* 10128d220203SAlan Cox * We unlock the inactive page queue, invalidating the 101348cc2fc7SKonstantin Belousov * 'next' pointer. Use our marker to remember our 101448cc2fc7SKonstantin Belousov * place. 101548cc2fc7SKonstantin Belousov */ 1016c325e866SKonstantin Belousov TAILQ_INSERT_AFTER(&pq->pq_pl, m, &vmd->vmd_marker, plinks.q); 10178d220203SAlan Cox vm_pagequeue_unlock(pq); 101848cc2fc7SKonstantin Belousov queues_locked = FALSE; 101948cc2fc7SKonstantin Belousov 102048cc2fc7SKonstantin Belousov /* 1021bb7858eaSJeff Roberson * We bump the activation count if the page has been 1022bb7858eaSJeff Roberson * referenced while in the inactive queue. This makes 1023bb7858eaSJeff Roberson * it less likely that the page will be added back to the 1024bb7858eaSJeff Roberson * inactive queue prematurely again. Here we check the 10251c7c3c6aSMatthew Dillon * page tables (or emulated bits, if any), given the upper 10261c7c3c6aSMatthew Dillon * level VM system not knowing anything about existing 10271c7c3c6aSMatthew Dillon * references. 10287e006499SJohn Dyson */ 1029bb7858eaSJeff Roberson act_delta = 0; 1030bb7858eaSJeff Roberson if ((m->aflags & PGA_REFERENCED) != 0) { 1031bb7858eaSJeff Roberson vm_page_aflag_clear(m, PGA_REFERENCED); 1032bb7858eaSJeff Roberson act_delta = 1; 1033bb7858eaSJeff Roberson } 1034bb7858eaSJeff Roberson if (object->ref_count != 0) { 1035bb7858eaSJeff Roberson act_delta += pmap_ts_referenced(m); 1036bb7858eaSJeff Roberson } else { 1037bb7858eaSJeff Roberson KASSERT(!pmap_page_is_mapped(m), 1038bb7858eaSJeff Roberson ("vm_pageout_scan: page %p is mapped", m)); 10392fe6e4d7SDavid Greenman } 1040ef743ce6SJohn Dyson 10417e006499SJohn Dyson /* 10421c7c3c6aSMatthew Dillon * If the upper level VM system knows about any page 1043bb7858eaSJeff Roberson * references, we reactivate the page or requeue it. 10447e006499SJohn Dyson */ 1045bb7858eaSJeff Roberson if (act_delta != 0) { 1046bb7858eaSJeff Roberson if (object->ref_count) { 104726f9a767SRodney W. Grimes vm_page_activate(m); 1048bb7858eaSJeff Roberson m->act_count += act_delta + ACT_ADVANCE; 1049bb7858eaSJeff Roberson } else { 1050bb7858eaSJeff Roberson vm_pagequeue_lock(pq); 1051bb7858eaSJeff Roberson queues_locked = TRUE; 1052bb7858eaSJeff Roberson vm_page_requeue_locked(m); 1053bb7858eaSJeff Roberson } 105489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1055a15f7df5SAttilio Rao vm_page_unlock(m); 105648cc2fc7SKonstantin Belousov goto relock_queues; 10570d94caffSDavid Greenman } 105867bf6868SJohn Dyson 1059311e34e2SKonstantin Belousov if (m->hold_count != 0) { 1060311e34e2SKonstantin Belousov vm_page_unlock(m); 106189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1062311e34e2SKonstantin Belousov 1063311e34e2SKonstantin Belousov /* 1064311e34e2SKonstantin Belousov * Held pages are essentially stuck in the 1065311e34e2SKonstantin Belousov * queue. So, they ought to be discounted 1066449c2e92SKonstantin Belousov * from the inactive count. See the 1067311e34e2SKonstantin Belousov * calculation of the page_shortage for the 1068311e34e2SKonstantin Belousov * loop over the active queue below. 1069311e34e2SKonstantin Belousov */ 1070311e34e2SKonstantin Belousov addl_page_shortage++; 1071311e34e2SKonstantin Belousov goto relock_queues; 1072311e34e2SKonstantin Belousov } 1073311e34e2SKonstantin Belousov 10747e006499SJohn Dyson /* 10759fc4739dSAlan Cox * If the page appears to be clean at the machine-independent 10769fc4739dSAlan Cox * layer, then remove all of its mappings from the pmap in 10779fc4739dSAlan Cox * anticipation of placing it onto the cache queue. If, 10789fc4739dSAlan Cox * however, any of the page's mappings allow write access, 10799fc4739dSAlan Cox * then the page may still be modified until the last of those 10809fc4739dSAlan Cox * mappings are removed. 10817e006499SJohn Dyson */ 10829fc4739dSAlan Cox vm_page_test_dirty(m); 10839fc4739dSAlan Cox if (m->dirty == 0 && object->ref_count != 0) 1084b78ddb0bSAlan Cox pmap_remove_all(m); 1085dcbcd518SBruce Evans 10866989c456SAlan Cox if (m->valid == 0) { 10877e006499SJohn Dyson /* 10887e006499SJohn Dyson * Invalid pages can be easily freed 10897e006499SJohn Dyson */ 10906989c456SAlan Cox vm_page_free(m); 109148cc2fc7SKonstantin Belousov PCPU_INC(cnt.v_dfree); 10921c7c3c6aSMatthew Dillon --page_shortage; 1093bd7e5f99SJohn Dyson } else if (m->dirty == 0) { 10946989c456SAlan Cox /* 10956989c456SAlan Cox * Clean pages can be placed onto the cache queue. 10966989c456SAlan Cox * This effectively frees them. 10976989c456SAlan Cox */ 1098bd7e5f99SJohn Dyson vm_page_cache(m); 10991c7c3c6aSMatthew Dillon --page_shortage; 1100*d9e23210SJeff Roberson } else if ((m->flags & PG_WINATCFLS) == 0 && pass < 2) { 11017e006499SJohn Dyson /* 11022b6b0df7SMatthew Dillon * Dirty pages need to be paged out, but flushing 11032b6b0df7SMatthew Dillon * a page is extremely expensive verses freeing 11042b6b0df7SMatthew Dillon * a clean page. Rather then artificially limiting 11052b6b0df7SMatthew Dillon * the number of pages we can flush, we instead give 11062b6b0df7SMatthew Dillon * dirty pages extra priority on the inactive queue 11072b6b0df7SMatthew Dillon * by forcing them to be cycled through the queue 11082b6b0df7SMatthew Dillon * twice before being flushed, after which the 11092b6b0df7SMatthew Dillon * (now clean) page will cycle through once more 11102b6b0df7SMatthew Dillon * before being freed. This significantly extends 11112b6b0df7SMatthew Dillon * the thrash point for a heavily loaded machine. 11127e006499SJohn Dyson */ 11133407fefeSKonstantin Belousov m->flags |= PG_WINATCFLS; 11148d220203SAlan Cox vm_pagequeue_lock(pq); 111548cc2fc7SKonstantin Belousov queues_locked = TRUE; 11168d220203SAlan Cox vm_page_requeue_locked(m); 11170d94caffSDavid Greenman } else if (maxlaunder > 0) { 11182b6b0df7SMatthew Dillon /* 11192b6b0df7SMatthew Dillon * We always want to try to flush some dirty pages if 11202b6b0df7SMatthew Dillon * we encounter them, to keep the system stable. 11212b6b0df7SMatthew Dillon * Normally this number is small, but under extreme 11222b6b0df7SMatthew Dillon * pressure where there are insufficient clean pages 11232b6b0df7SMatthew Dillon * on the inactive queue, we may have to go all out. 11242b6b0df7SMatthew Dillon */ 11255050aa86SKonstantin Belousov int swap_pageouts_ok; 1126f6b04d2bSDavid Greenman struct vnode *vp = NULL; 112714137dc0SAlan Cox struct mount *mp = NULL; 11280d94caffSDavid Greenman 112912ac6a1dSJohn Dyson if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) { 113012ac6a1dSJohn Dyson swap_pageouts_ok = 1; 113112ac6a1dSJohn Dyson } else { 113212ac6a1dSJohn Dyson swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts); 113312ac6a1dSJohn Dyson swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts && 113490ecac61SMatthew Dillon vm_page_count_min()); 113512ac6a1dSJohn Dyson 113612ac6a1dSJohn Dyson } 113770111b90SJohn Dyson 113870111b90SJohn Dyson /* 11391c7c3c6aSMatthew Dillon * We don't bother paging objects that are "dead". 11401c7c3c6aSMatthew Dillon * Those objects are in a "rundown" state. 114170111b90SJohn Dyson */ 114270111b90SJohn Dyson if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) { 11438d220203SAlan Cox vm_pagequeue_lock(pq); 11442965a453SKip Macy vm_page_unlock(m); 114589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 114648cc2fc7SKonstantin Belousov queues_locked = TRUE; 11478d220203SAlan Cox vm_page_requeue_locked(m); 114848cc2fc7SKonstantin Belousov goto relock_queues; 114912ac6a1dSJohn Dyson } 115012ac6a1dSJohn Dyson 11511c7c3c6aSMatthew Dillon /* 11522b6b0df7SMatthew Dillon * The object is already known NOT to be dead. It 11532b6b0df7SMatthew Dillon * is possible for the vget() to block the whole 11542b6b0df7SMatthew Dillon * pageout daemon, but the new low-memory handling 11552b6b0df7SMatthew Dillon * code should prevent it. 11561c7c3c6aSMatthew Dillon * 11572b6b0df7SMatthew Dillon * The previous code skipped locked vnodes and, worse, 11582b6b0df7SMatthew Dillon * reordered pages in the queue. This results in 11592b6b0df7SMatthew Dillon * completely non-deterministic operation and, on a 11602b6b0df7SMatthew Dillon * busy system, can lead to extremely non-optimal 11612b6b0df7SMatthew Dillon * pageouts. For example, it can cause clean pages 11622b6b0df7SMatthew Dillon * to be freed and dirty pages to be moved to the end 11632b6b0df7SMatthew Dillon * of the queue. Since dirty pages are also moved to 11642b6b0df7SMatthew Dillon * the end of the queue once-cleaned, this gives 11652b6b0df7SMatthew Dillon * way too large a weighting to defering the freeing 11662b6b0df7SMatthew Dillon * of dirty pages. 11671c7c3c6aSMatthew Dillon * 116823b59018SMatthew Dillon * We can't wait forever for the vnode lock, we might 116923b59018SMatthew Dillon * deadlock due to a vn_read() getting stuck in 117023b59018SMatthew Dillon * vm_wait while holding this vnode. We skip the 117123b59018SMatthew Dillon * vnode if we can't get it in a reasonable amount 117223b59018SMatthew Dillon * of time. 11731c7c3c6aSMatthew Dillon */ 11741c7c3c6aSMatthew Dillon if (object->type == OBJT_VNODE) { 11752965a453SKip Macy vm_page_unlock(m); 117624a1cce3SDavid Greenman vp = object->handle; 1177db27dcc0STor Egge if (vp->v_type == VREG && 1178db27dcc0STor Egge vn_start_write(vp, &mp, V_NOWAIT) != 0) { 11796129343dSKonstantin Belousov mp = NULL; 1180db27dcc0STor Egge ++pageout_lock_miss; 1181db27dcc0STor Egge if (object->flags & OBJ_MIGHTBEDIRTY) 1182db27dcc0STor Egge vnodes_skipped++; 1183625e6c0aSTor Egge goto unlock_and_continue; 1184db27dcc0STor Egge } 1185b9f180d1SKonstantin Belousov KASSERT(mp != NULL, 1186b9f180d1SKonstantin Belousov ("vp %p with NULL v_mount", vp)); 118714137dc0SAlan Cox vm_object_reference_locked(object); 118889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 118997824da3SAlan Cox if (vget(vp, LK_EXCLUSIVE | LK_TIMELOCK, 119097824da3SAlan Cox curthread)) { 119189f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 119223b59018SMatthew Dillon ++pageout_lock_miss; 1193aef922f5SJohn Dyson if (object->flags & OBJ_MIGHTBEDIRTY) 1194925a3a41SJohn Dyson vnodes_skipped++; 1195625e6c0aSTor Egge vp = NULL; 1196625e6c0aSTor Egge goto unlock_and_continue; 119785a376ebSJohn Dyson } 119889f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 11992965a453SKip Macy vm_page_lock(m); 12008d220203SAlan Cox vm_pagequeue_lock(pq); 120148cc2fc7SKonstantin Belousov queues_locked = TRUE; 1202f35329acSJohn Dyson /* 1203936524aaSMatthew Dillon * The page might have been moved to another 1204936524aaSMatthew Dillon * queue during potential blocking in vget() 1205936524aaSMatthew Dillon * above. The page might have been freed and 120614137dc0SAlan Cox * reused for another vnode. 1207f35329acSJohn Dyson */ 12089cf51988SAlan Cox if (m->queue != PQ_INACTIVE || 1209936524aaSMatthew Dillon m->object != object || 1210c325e866SKonstantin Belousov TAILQ_NEXT(m, plinks.q) != &vmd->vmd_marker) { 12112965a453SKip Macy vm_page_unlock(m); 1212b182ec9eSJohn Dyson if (object->flags & OBJ_MIGHTBEDIRTY) 1213925a3a41SJohn Dyson vnodes_skipped++; 12143562af12SAlan Cox goto unlock_and_continue; 1215b182ec9eSJohn Dyson } 1216b182ec9eSJohn Dyson 1217f35329acSJohn Dyson /* 1218936524aaSMatthew Dillon * The page may have been busied during the 121914137dc0SAlan Cox * blocking in vget(). We don't move the 1220936524aaSMatthew Dillon * page back onto the end of the queue so that 1221936524aaSMatthew Dillon * statistics are more correct if we don't. 1222f35329acSJohn Dyson */ 1223c7aebda8SAttilio Rao if (vm_page_busied(m)) { 12242965a453SKip Macy vm_page_unlock(m); 12253562af12SAlan Cox goto unlock_and_continue; 1226b182ec9eSJohn Dyson } 1227b182ec9eSJohn Dyson 1228f35329acSJohn Dyson /* 122957601bcbSMatthew Dillon * If the page has become held it might 123057601bcbSMatthew Dillon * be undergoing I/O, so skip it 1231f35329acSJohn Dyson */ 1232b182ec9eSJohn Dyson if (m->hold_count) { 12332965a453SKip Macy vm_page_unlock(m); 12348d220203SAlan Cox vm_page_requeue_locked(m); 1235b182ec9eSJohn Dyson if (object->flags & OBJ_MIGHTBEDIRTY) 1236925a3a41SJohn Dyson vnodes_skipped++; 12373562af12SAlan Cox goto unlock_and_continue; 1238f6b04d2bSDavid Greenman } 12398d220203SAlan Cox vm_pagequeue_unlock(pq); 1240a4156419SKonstantin Belousov queues_locked = FALSE; 1241f6b04d2bSDavid Greenman } 1242f6b04d2bSDavid Greenman 12430d94caffSDavid Greenman /* 12440d94caffSDavid Greenman * If a page is dirty, then it is either being washed 12450d94caffSDavid Greenman * (but not yet cleaned) or it is still in the 12460d94caffSDavid Greenman * laundry. If it is still in the laundry, then we 12472b6b0df7SMatthew Dillon * start the cleaning operation. 1248936524aaSMatthew Dillon * 12492b6b0df7SMatthew Dillon * decrement page_shortage on success to account for 12502b6b0df7SMatthew Dillon * the (future) cleaned page. Otherwise we could wind 12512b6b0df7SMatthew Dillon * up laundering or cleaning too many pages. 12520d94caffSDavid Greenman */ 12532b6b0df7SMatthew Dillon if (vm_pageout_clean(m) != 0) { 12542b6b0df7SMatthew Dillon --page_shortage; 1255936524aaSMatthew Dillon --maxlaunder; 12562b6b0df7SMatthew Dillon } 12573562af12SAlan Cox unlock_and_continue: 12582965a453SKip Macy vm_page_lock_assert(m, MA_NOTOWNED); 125989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 126014137dc0SAlan Cox if (mp != NULL) { 126148cc2fc7SKonstantin Belousov if (queues_locked) { 12628d220203SAlan Cox vm_pagequeue_unlock(pq); 126348cc2fc7SKonstantin Belousov queues_locked = FALSE; 126448cc2fc7SKonstantin Belousov } 126514137dc0SAlan Cox if (vp != NULL) 1266f6b04d2bSDavid Greenman vput(vp); 126714137dc0SAlan Cox vm_object_deallocate(object); 1268f2a2857bSKirk McKusick vn_finished_write(mp); 126948cc2fc7SKonstantin Belousov } 127048cc2fc7SKonstantin Belousov vm_page_lock_assert(m, MA_NOTOWNED); 127148cc2fc7SKonstantin Belousov goto relock_queues; 127248cc2fc7SKonstantin Belousov } 127348cc2fc7SKonstantin Belousov vm_page_unlock(m); 127489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 127548cc2fc7SKonstantin Belousov relock_queues: 127648cc2fc7SKonstantin Belousov if (!queues_locked) { 12778d220203SAlan Cox vm_pagequeue_lock(pq); 127848cc2fc7SKonstantin Belousov queues_locked = TRUE; 12796989c456SAlan Cox } 1280c325e866SKonstantin Belousov next = TAILQ_NEXT(&vmd->vmd_marker, plinks.q); 1281c325e866SKonstantin Belousov TAILQ_REMOVE(&pq->pq_pl, &vmd->vmd_marker, plinks.q); 12820d94caffSDavid Greenman } 12838d220203SAlan Cox vm_pagequeue_unlock(pq); 128426f9a767SRodney W. Grimes 1285df8bae1dSRodney W. Grimes /* 1286936524aaSMatthew Dillon * Compute the number of pages we want to try to move from the 1287936524aaSMatthew Dillon * active queue to the inactive queue. 12881c7c3c6aSMatthew Dillon */ 1289*d9e23210SJeff Roberson pcount = pq->pq_cnt; 12902feb50bfSAttilio Rao page_shortage = vm_paging_target() + 12912feb50bfSAttilio Rao cnt.v_inactive_target - cnt.v_inactive_count; 1292b182ec9eSJohn Dyson page_shortage += addl_page_shortage; 1293*d9e23210SJeff Roberson /* 1294*d9e23210SJeff Roberson * If we're just idle polling attempt to visit every 1295*d9e23210SJeff Roberson * active page within 'update_period' seconds. 1296*d9e23210SJeff Roberson */ 1297*d9e23210SJeff Roberson if (pass == 0 && vm_pageout_update_period != 0) { 1298*d9e23210SJeff Roberson pcount /= vm_pageout_update_period; 1299*d9e23210SJeff Roberson page_shortage = pcount; 1300*d9e23210SJeff Roberson } 13011c7c3c6aSMatthew Dillon 13021c7c3c6aSMatthew Dillon /* 1303936524aaSMatthew Dillon * Scan the active queue for things we can deactivate. We nominally 1304936524aaSMatthew Dillon * track the per-page activity counter and use it to locate 1305936524aaSMatthew Dillon * deactivation candidates. 13061c7c3c6aSMatthew Dillon */ 1307449c2e92SKonstantin Belousov pq = &vmd->vmd_pagequeues[PQ_ACTIVE]; 13088d220203SAlan Cox vm_pagequeue_lock(pq); 13098d220203SAlan Cox m = TAILQ_FIRST(&pq->pq_pl); 1310b18bfc3dSJohn Dyson while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) { 1311f35329acSJohn Dyson 13129cf51988SAlan Cox KASSERT(m->queue == PQ_ACTIVE, 1313d3c09dd7SAlan Cox ("vm_pageout_scan: page %p isn't active", m)); 1314f35329acSJohn Dyson 1315c325e866SKonstantin Belousov next = TAILQ_NEXT(m, plinks.q); 13168dbca793STor Egge if ((m->flags & PG_MARKER) != 0) { 13178dbca793STor Egge m = next; 13188dbca793STor Egge continue; 13198dbca793STor Egge } 13207900f95dSKonstantin Belousov KASSERT((m->flags & PG_FICTITIOUS) == 0, 13217900f95dSKonstantin Belousov ("Fictitious page %p cannot be in active queue", m)); 13227900f95dSKonstantin Belousov KASSERT((m->oflags & VPO_UNMANAGED) == 0, 13237900f95dSKonstantin Belousov ("Unmanaged page %p cannot be in active queue", m)); 13249ee2165fSAlan Cox if (!vm_pageout_page_lock(m, &next)) { 13258c616246SKonstantin Belousov vm_page_unlock(m); 13262965a453SKip Macy m = next; 13272965a453SKip Macy continue; 13282965a453SKip Macy } 13299ee2165fSAlan Cox object = m->object; 133089f6b863SAttilio Rao if (!VM_OBJECT_TRYWLOCK(object) && 13318dbca793STor Egge !vm_pageout_fallback_object_lock(m, &next)) { 133289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 13332965a453SKip Macy vm_page_unlock(m); 13344b8a5c40SAlan Cox m = next; 1335b08abf6cSAlan Cox continue; 1336b08abf6cSAlan Cox } 1337b08abf6cSAlan Cox 1338df8bae1dSRodney W. Grimes /* 133926f9a767SRodney W. Grimes * Don't deactivate pages that are busy. 1340df8bae1dSRodney W. Grimes */ 1341c7aebda8SAttilio Rao if (vm_page_busied(m) || m->hold_count != 0) { 13422965a453SKip Macy vm_page_unlock(m); 134389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 13448d220203SAlan Cox vm_page_requeue_locked(m); 134526f9a767SRodney W. Grimes m = next; 134626f9a767SRodney W. Grimes continue; 1347df8bae1dSRodney W. Grimes } 1348b18bfc3dSJohn Dyson 1349b18bfc3dSJohn Dyson /* 1350b18bfc3dSJohn Dyson * The count for pagedaemon pages is done after checking the 1351956f3135SPhilippe Charnier * page for eligibility... 1352b18bfc3dSJohn Dyson */ 13538d220203SAlan Cox PCPU_INC(cnt.v_pdpages); 1354ef743ce6SJohn Dyson 13557e006499SJohn Dyson /* 13567e006499SJohn Dyson * Check to see "how much" the page has been used. 13577e006499SJohn Dyson */ 1358bb7858eaSJeff Roberson act_delta = 0; 13593407fefeSKonstantin Belousov if (m->aflags & PGA_REFERENCED) { 1360bb7858eaSJeff Roberson vm_page_aflag_clear(m, PGA_REFERENCED); 1361bb7858eaSJeff Roberson act_delta += 1; 13620d94caffSDavid Greenman } 1363bb7858eaSJeff Roberson if (object->ref_count != 0) 1364bb7858eaSJeff Roberson act_delta += pmap_ts_referenced(m); 1365bb7858eaSJeff Roberson 1366bb7858eaSJeff Roberson /* 1367bb7858eaSJeff Roberson * Advance or decay the act_count based on recent usage. 1368bb7858eaSJeff Roberson */ 1369bb7858eaSJeff Roberson if (act_delta) { 1370bb7858eaSJeff Roberson m->act_count += ACT_ADVANCE + act_delta; 137138efa82bSJohn Dyson if (m->act_count > ACT_MAX) 137238efa82bSJohn Dyson m->act_count = ACT_MAX; 1373bb7858eaSJeff Roberson } else { 137438efa82bSJohn Dyson m->act_count -= min(m->act_count, ACT_DECLINE); 1375bb7858eaSJeff Roberson act_delta = m->act_count; 1376bb7858eaSJeff Roberson } 1377bb7858eaSJeff Roberson 1378bb7858eaSJeff Roberson /* 1379bb7858eaSJeff Roberson * Move this page to the tail of the active or inactive 1380bb7858eaSJeff Roberson * queue depending on usage. 1381bb7858eaSJeff Roberson */ 1382bb7858eaSJeff Roberson if (act_delta == 0) { 1383bb7858eaSJeff Roberson KASSERT(object->ref_count != 0 || 1384bb7858eaSJeff Roberson !pmap_page_is_mapped(m), 1385bb7858eaSJeff Roberson ("vm_pageout_scan: page %p is mapped", m)); 13868d220203SAlan Cox /* Dequeue to avoid later lock recursion. */ 13878d220203SAlan Cox vm_page_dequeue_locked(m); 1388d4a272dbSJohn Dyson vm_page_deactivate(m); 1389bb7858eaSJeff Roberson page_shortage--; 13908d220203SAlan Cox } else 13918d220203SAlan Cox vm_page_requeue_locked(m); 13922965a453SKip Macy vm_page_unlock(m); 139389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 139426f9a767SRodney W. Grimes m = next; 139526f9a767SRodney W. Grimes } 13968d220203SAlan Cox vm_pagequeue_unlock(pq); 1397ceb0cf87SJohn Dyson #if !defined(NO_SWAPPING) 1398ceb0cf87SJohn Dyson /* 1399ceb0cf87SJohn Dyson * Idle process swapout -- run once per second. 1400ceb0cf87SJohn Dyson */ 1401ceb0cf87SJohn Dyson if (vm_swap_idle_enabled) { 1402ceb0cf87SJohn Dyson static long lsec; 1403227ee8a1SPoul-Henning Kamp if (time_second != lsec) { 140497824da3SAlan Cox vm_req_vmdaemon(VM_SWAP_IDLE); 1405227ee8a1SPoul-Henning Kamp lsec = time_second; 1406ceb0cf87SJohn Dyson } 1407ceb0cf87SJohn Dyson } 1408ceb0cf87SJohn Dyson #endif 1409ceb0cf87SJohn Dyson 14105663e6deSDavid Greenman /* 1411f6b04d2bSDavid Greenman * If we didn't get enough free pages, and we have skipped a vnode 14124c1f8ee9SDavid Greenman * in a writeable object, wakeup the sync daemon. And kick swapout 14134c1f8ee9SDavid Greenman * if we did not get enough free pages. 1414f6b04d2bSDavid Greenman */ 141590ecac61SMatthew Dillon if (vm_paging_target() > 0) { 141690ecac61SMatthew Dillon if (vnodes_skipped && vm_page_count_min()) 1417d50c1994SPeter Wemm (void) speedup_syncer(); 141838efa82bSJohn Dyson #if !defined(NO_SWAPPING) 141997824da3SAlan Cox if (vm_swap_enabled && vm_page_count_target()) 142097824da3SAlan Cox vm_req_vmdaemon(VM_SWAP_NORMAL); 14215afce282SDavid Greenman #endif 14224c1f8ee9SDavid Greenman } 14234c1f8ee9SDavid Greenman 1424f6b04d2bSDavid Greenman /* 1425e92686d0SDavid Schultz * If we are critically low on one of RAM or swap and low on 1426e92686d0SDavid Schultz * the other, kill the largest process. However, we avoid 1427e92686d0SDavid Schultz * doing this on the first pass in order to give ourselves a 1428e92686d0SDavid Schultz * chance to flush out dirty vnode-backed pages and to allow 1429e92686d0SDavid Schultz * active pages to be moved to the inactive queue and reclaimed. 14302025d69bSKonstantin Belousov */ 1431449c2e92SKonstantin Belousov vm_pageout_mightbe_oom(vmd, pass); 14322025d69bSKonstantin Belousov } 14332025d69bSKonstantin Belousov 1434449c2e92SKonstantin Belousov static int vm_pageout_oom_vote; 1435449c2e92SKonstantin Belousov 1436449c2e92SKonstantin Belousov /* 1437449c2e92SKonstantin Belousov * The pagedaemon threads randlomly select one to perform the 1438449c2e92SKonstantin Belousov * OOM. Trying to kill processes before all pagedaemons 1439449c2e92SKonstantin Belousov * failed to reach free target is premature. 1440449c2e92SKonstantin Belousov */ 1441449c2e92SKonstantin Belousov static void 1442449c2e92SKonstantin Belousov vm_pageout_mightbe_oom(struct vm_domain *vmd, int pass) 1443449c2e92SKonstantin Belousov { 1444449c2e92SKonstantin Belousov int old_vote; 1445449c2e92SKonstantin Belousov 1446*d9e23210SJeff Roberson if (pass <= 1 || !((swap_pager_avail < 64 && vm_page_count_min()) || 1447449c2e92SKonstantin Belousov (swap_pager_full && vm_paging_target() > 0))) { 1448449c2e92SKonstantin Belousov if (vmd->vmd_oom) { 1449449c2e92SKonstantin Belousov vmd->vmd_oom = FALSE; 1450449c2e92SKonstantin Belousov atomic_subtract_int(&vm_pageout_oom_vote, 1); 1451449c2e92SKonstantin Belousov } 1452449c2e92SKonstantin Belousov return; 1453449c2e92SKonstantin Belousov } 1454449c2e92SKonstantin Belousov 1455449c2e92SKonstantin Belousov if (vmd->vmd_oom) 1456449c2e92SKonstantin Belousov return; 1457449c2e92SKonstantin Belousov 1458449c2e92SKonstantin Belousov vmd->vmd_oom = TRUE; 1459449c2e92SKonstantin Belousov old_vote = atomic_fetchadd_int(&vm_pageout_oom_vote, 1); 1460449c2e92SKonstantin Belousov if (old_vote != vm_ndomains - 1) 1461449c2e92SKonstantin Belousov return; 1462449c2e92SKonstantin Belousov 1463449c2e92SKonstantin Belousov /* 1464449c2e92SKonstantin Belousov * The current pagedaemon thread is the last in the quorum to 1465449c2e92SKonstantin Belousov * start OOM. Initiate the selection and signaling of the 1466449c2e92SKonstantin Belousov * victim. 1467449c2e92SKonstantin Belousov */ 1468449c2e92SKonstantin Belousov vm_pageout_oom(VM_OOM_MEM); 1469449c2e92SKonstantin Belousov 1470449c2e92SKonstantin Belousov /* 1471449c2e92SKonstantin Belousov * After one round of OOM terror, recall our vote. On the 1472449c2e92SKonstantin Belousov * next pass, current pagedaemon would vote again if the low 1473449c2e92SKonstantin Belousov * memory condition is still there, due to vmd_oom being 1474449c2e92SKonstantin Belousov * false. 1475449c2e92SKonstantin Belousov */ 1476449c2e92SKonstantin Belousov vmd->vmd_oom = FALSE; 1477449c2e92SKonstantin Belousov atomic_subtract_int(&vm_pageout_oom_vote, 1); 1478449c2e92SKonstantin Belousov } 14792025d69bSKonstantin Belousov 14802025d69bSKonstantin Belousov void 14812025d69bSKonstantin Belousov vm_pageout_oom(int shortage) 14822025d69bSKonstantin Belousov { 14832025d69bSKonstantin Belousov struct proc *p, *bigproc; 14842025d69bSKonstantin Belousov vm_offset_t size, bigsize; 14852025d69bSKonstantin Belousov struct thread *td; 14866bed074cSKonstantin Belousov struct vmspace *vm; 14872025d69bSKonstantin Belousov 14882025d69bSKonstantin Belousov /* 14891c58e4e5SJohn Baldwin * We keep the process bigproc locked once we find it to keep anyone 14901c58e4e5SJohn Baldwin * from messing with it; however, there is a possibility of 14911c58e4e5SJohn Baldwin * deadlock if process B is bigproc and one of it's child processes 14921c58e4e5SJohn Baldwin * attempts to propagate a signal to B while we are waiting for A's 14931c58e4e5SJohn Baldwin * lock while walking this list. To avoid this, we don't block on 14941c58e4e5SJohn Baldwin * the process lock but just skip a process if it is already locked. 14955663e6deSDavid Greenman */ 14965663e6deSDavid Greenman bigproc = NULL; 14975663e6deSDavid Greenman bigsize = 0; 14981005a129SJohn Baldwin sx_slock(&allproc_lock); 1499e602ba25SJulian Elischer FOREACH_PROC_IN_SYSTEM(p) { 1500e602ba25SJulian Elischer int breakout; 1501dcbcd518SBruce Evans 15021c58e4e5SJohn Baldwin if (PROC_TRYLOCK(p) == 0) 15031c58e4e5SJohn Baldwin continue; 15041c58e4e5SJohn Baldwin /* 15053f1c4c4fSKonstantin Belousov * If this is a system, protected or killed process, skip it. 15065663e6deSDavid Greenman */ 15078e6fa660SJohn Baldwin if (p->p_state != PRS_NORMAL || 15088e6fa660SJohn Baldwin (p->p_flag & (P_INEXEC | P_PROTECTED | P_SYSTEM)) || 15093f1c4c4fSKonstantin Belousov (p->p_pid == 1) || P_KILLED(p) || 15108f60c087SPoul-Henning Kamp ((p->p_pid < 48) && (swap_pager_avail != 0))) { 15118606d880SJohn Baldwin PROC_UNLOCK(p); 15125663e6deSDavid Greenman continue; 15135663e6deSDavid Greenman } 15145663e6deSDavid Greenman /* 1515dcbcd518SBruce Evans * If the process is in a non-running type state, 1516e602ba25SJulian Elischer * don't touch it. Check all the threads individually. 15175663e6deSDavid Greenman */ 1518e602ba25SJulian Elischer breakout = 0; 1519e602ba25SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 1520982d11f8SJeff Roberson thread_lock(td); 152171fad9fdSJulian Elischer if (!TD_ON_RUNQ(td) && 152271fad9fdSJulian Elischer !TD_IS_RUNNING(td) && 1523f497cda2SEdward Tomasz Napierala !TD_IS_SLEEPING(td) && 1524f497cda2SEdward Tomasz Napierala !TD_IS_SUSPENDED(td)) { 1525982d11f8SJeff Roberson thread_unlock(td); 1526e602ba25SJulian Elischer breakout = 1; 1527e602ba25SJulian Elischer break; 1528e602ba25SJulian Elischer } 1529982d11f8SJeff Roberson thread_unlock(td); 1530e602ba25SJulian Elischer } 1531e602ba25SJulian Elischer if (breakout) { 15321c58e4e5SJohn Baldwin PROC_UNLOCK(p); 15335663e6deSDavid Greenman continue; 15345663e6deSDavid Greenman } 15355663e6deSDavid Greenman /* 15365663e6deSDavid Greenman * get the process size 15375663e6deSDavid Greenman */ 15386bed074cSKonstantin Belousov vm = vmspace_acquire_ref(p); 15396bed074cSKonstantin Belousov if (vm == NULL) { 15406bed074cSKonstantin Belousov PROC_UNLOCK(p); 15416bed074cSKonstantin Belousov continue; 15426bed074cSKonstantin Belousov } 15436bed074cSKonstantin Belousov if (!vm_map_trylock_read(&vm->vm_map)) { 15446bed074cSKonstantin Belousov vmspace_free(vm); 154572d97679SDavid Schultz PROC_UNLOCK(p); 154672d97679SDavid Schultz continue; 154772d97679SDavid Schultz } 15487981aa24SKonstantin Belousov size = vmspace_swap_count(vm); 15496bed074cSKonstantin Belousov vm_map_unlock_read(&vm->vm_map); 15502025d69bSKonstantin Belousov if (shortage == VM_OOM_MEM) 15516bed074cSKonstantin Belousov size += vmspace_resident_count(vm); 15526bed074cSKonstantin Belousov vmspace_free(vm); 15535663e6deSDavid Greenman /* 15545663e6deSDavid Greenman * if the this process is bigger than the biggest one 15555663e6deSDavid Greenman * remember it. 15565663e6deSDavid Greenman */ 15575663e6deSDavid Greenman if (size > bigsize) { 15581c58e4e5SJohn Baldwin if (bigproc != NULL) 15591c58e4e5SJohn Baldwin PROC_UNLOCK(bigproc); 15605663e6deSDavid Greenman bigproc = p; 15615663e6deSDavid Greenman bigsize = size; 15621c58e4e5SJohn Baldwin } else 15631c58e4e5SJohn Baldwin PROC_UNLOCK(p); 15645663e6deSDavid Greenman } 15651005a129SJohn Baldwin sx_sunlock(&allproc_lock); 15665663e6deSDavid Greenman if (bigproc != NULL) { 1567729b1e51SDavid Greenman killproc(bigproc, "out of swap space"); 1568fa885116SJulian Elischer sched_nice(bigproc, PRIO_MIN); 15691c58e4e5SJohn Baldwin PROC_UNLOCK(bigproc); 15702feb50bfSAttilio Rao wakeup(&cnt.v_free_count); 15715663e6deSDavid Greenman } 15725663e6deSDavid Greenman } 157326f9a767SRodney W. Grimes 1574449c2e92SKonstantin Belousov static void 1575449c2e92SKonstantin Belousov vm_pageout_worker(void *arg) 1576449c2e92SKonstantin Belousov { 1577449c2e92SKonstantin Belousov struct vm_domain *domain; 1578449c2e92SKonstantin Belousov struct pcpu *pc; 1579*d9e23210SJeff Roberson int cpu, domidx; 1580449c2e92SKonstantin Belousov 1581449c2e92SKonstantin Belousov domidx = (uintptr_t)arg; 1582449c2e92SKonstantin Belousov domain = &vm_dom[domidx]; 1583449c2e92SKonstantin Belousov 1584449c2e92SKonstantin Belousov /* 1585449c2e92SKonstantin Belousov * XXXKIB The bind is rather arbitrary. With some minor 1586449c2e92SKonstantin Belousov * complications, we could assign the cpuset consisting of all 1587449c2e92SKonstantin Belousov * CPUs in the same domain. In fact, it even does not matter 1588449c2e92SKonstantin Belousov * if the CPU we bind to is in the affinity domain of this 1589449c2e92SKonstantin Belousov * page queue, we only need to establish the fair distribution 1590449c2e92SKonstantin Belousov * of pagedaemon threads among CPUs. 1591449c2e92SKonstantin Belousov * 1592449c2e92SKonstantin Belousov * XXXKIB It would be useful to allocate vm_pages for the 1593449c2e92SKonstantin Belousov * domain from the domain, and put pcpu area into the page 1594449c2e92SKonstantin Belousov * owned by the domain. 1595449c2e92SKonstantin Belousov */ 1596449c2e92SKonstantin Belousov if (mem_affinity != NULL) { 1597449c2e92SKonstantin Belousov CPU_FOREACH(cpu) { 1598449c2e92SKonstantin Belousov pc = pcpu_find(cpu); 1599449c2e92SKonstantin Belousov if (pc->pc_domain == domidx) { 1600449c2e92SKonstantin Belousov thread_lock(curthread); 1601449c2e92SKonstantin Belousov sched_bind(curthread, cpu); 1602449c2e92SKonstantin Belousov thread_unlock(curthread); 1603449c2e92SKonstantin Belousov break; 1604449c2e92SKonstantin Belousov } 1605449c2e92SKonstantin Belousov } 1606449c2e92SKonstantin Belousov } 1607449c2e92SKonstantin Belousov 1608449c2e92SKonstantin Belousov KASSERT(domain->vmd_segs != 0, ("domain without segments")); 1609449c2e92SKonstantin Belousov vm_pageout_init_marker(&domain->vmd_marker, PQ_INACTIVE); 1610449c2e92SKonstantin Belousov 1611449c2e92SKonstantin Belousov /* 1612449c2e92SKonstantin Belousov * The pageout daemon worker is never done, so loop forever. 1613449c2e92SKonstantin Belousov */ 1614449c2e92SKonstantin Belousov while (TRUE) { 1615449c2e92SKonstantin Belousov /* 1616449c2e92SKonstantin Belousov * If we have enough free memory, wakeup waiters. Do 1617449c2e92SKonstantin Belousov * not clear vm_pages_needed until we reach our target, 1618449c2e92SKonstantin Belousov * otherwise we may be woken up over and over again and 1619449c2e92SKonstantin Belousov * waste a lot of cpu. 1620449c2e92SKonstantin Belousov */ 1621449c2e92SKonstantin Belousov mtx_lock(&vm_page_queue_free_mtx); 1622449c2e92SKonstantin Belousov if (vm_pages_needed && !vm_page_count_min()) { 1623449c2e92SKonstantin Belousov if (!vm_paging_needed()) 1624449c2e92SKonstantin Belousov vm_pages_needed = 0; 1625449c2e92SKonstantin Belousov wakeup(&cnt.v_free_count); 1626449c2e92SKonstantin Belousov } 1627449c2e92SKonstantin Belousov if (vm_pages_needed) { 1628449c2e92SKonstantin Belousov /* 1629449c2e92SKonstantin Belousov * Still not done, take a second pass without waiting 1630449c2e92SKonstantin Belousov * (unlimited dirty cleaning), otherwise sleep a bit 1631449c2e92SKonstantin Belousov * and try again. 1632449c2e92SKonstantin Belousov */ 1633449c2e92SKonstantin Belousov if (domain->vmd_pass > 1) 1634449c2e92SKonstantin Belousov msleep(&vm_pages_needed, 1635449c2e92SKonstantin Belousov &vm_page_queue_free_mtx, PVM, "psleep", 1636449c2e92SKonstantin Belousov hz / 2); 1637449c2e92SKonstantin Belousov } else { 1638449c2e92SKonstantin Belousov /* 1639*d9e23210SJeff Roberson * Good enough, sleep until required to refresh 1640*d9e23210SJeff Roberson * stats. 1641449c2e92SKonstantin Belousov */ 1642449c2e92SKonstantin Belousov domain->vmd_pass = 0; 1643*d9e23210SJeff Roberson msleep(&vm_pages_needed, &vm_page_queue_free_mtx, 1644*d9e23210SJeff Roberson PVM, "psleep", hz); 1645*d9e23210SJeff Roberson 1646449c2e92SKonstantin Belousov } 1647*d9e23210SJeff Roberson if (vm_pages_needed) { 1648449c2e92SKonstantin Belousov cnt.v_pdwakeups++; 1649*d9e23210SJeff Roberson domain->vmd_pass++; 1650*d9e23210SJeff Roberson } 1651449c2e92SKonstantin Belousov mtx_unlock(&vm_page_queue_free_mtx); 1652449c2e92SKonstantin Belousov vm_pageout_scan(domain, domain->vmd_pass); 1653449c2e92SKonstantin Belousov } 1654449c2e92SKonstantin Belousov } 1655449c2e92SKonstantin Belousov 1656df8bae1dSRodney W. Grimes /* 1657df8bae1dSRodney W. Grimes * vm_pageout is the high level pageout daemon. 1658df8bae1dSRodney W. Grimes */ 16592b14f991SJulian Elischer static void 16604a365329SAndrey Zonov vm_pageout(void) 1661df8bae1dSRodney W. Grimes { 1662449c2e92SKonstantin Belousov #if MAXMEMDOM > 1 1663449c2e92SKonstantin Belousov int error, i; 1664449c2e92SKonstantin Belousov #endif 16650384fff8SJason Evans 1666df8bae1dSRodney W. Grimes /* 1667df8bae1dSRodney W. Grimes * Initialize some paging parameters. 1668df8bae1dSRodney W. Grimes */ 16692feb50bfSAttilio Rao cnt.v_interrupt_free_min = 2; 16702feb50bfSAttilio Rao if (cnt.v_page_count < 2000) 1671f35329acSJohn Dyson vm_pageout_page_count = 8; 1672f6b04d2bSDavid Greenman 167345ae1d91SAlan Cox /* 167445ae1d91SAlan Cox * v_free_reserved needs to include enough for the largest 167545ae1d91SAlan Cox * swap pager structures plus enough for any pv_entry structs 167645ae1d91SAlan Cox * when paging. 167745ae1d91SAlan Cox */ 16782feb50bfSAttilio Rao if (cnt.v_page_count > 1024) 16792feb50bfSAttilio Rao cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200; 16802feb50bfSAttilio Rao else 16812feb50bfSAttilio Rao cnt.v_free_min = 4; 16822feb50bfSAttilio Rao cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE + 16832feb50bfSAttilio Rao cnt.v_interrupt_free_min; 16842feb50bfSAttilio Rao cnt.v_free_reserved = vm_pageout_page_count + 16852446e4f0SAlan Cox cnt.v_pageout_free_min + (cnt.v_page_count / 768); 16862feb50bfSAttilio Rao cnt.v_free_severe = cnt.v_free_min / 2; 1687*d9e23210SJeff Roberson cnt.v_free_target = 4 * cnt.v_free_min + cnt.v_free_reserved; 16882feb50bfSAttilio Rao cnt.v_free_min += cnt.v_free_reserved; 16892feb50bfSAttilio Rao cnt.v_free_severe += cnt.v_free_reserved; 16902feb50bfSAttilio Rao cnt.v_inactive_target = (3 * cnt.v_free_target) / 2; 16912feb50bfSAttilio Rao if (cnt.v_inactive_target > cnt.v_free_count / 3) 16922feb50bfSAttilio Rao cnt.v_inactive_target = cnt.v_free_count / 3; 1693df8bae1dSRodney W. Grimes 1694*d9e23210SJeff Roberson /* 1695*d9e23210SJeff Roberson * Set the default wakeup threshold to be 10% above the minimum 1696*d9e23210SJeff Roberson * page limit. This keeps the steady state out of shortfall. 1697*d9e23210SJeff Roberson */ 1698*d9e23210SJeff Roberson vm_pageout_wakeup_thresh = (cnt.v_free_min / 10) * 11; 1699*d9e23210SJeff Roberson 1700*d9e23210SJeff Roberson /* 1701*d9e23210SJeff Roberson * Set interval in seconds for active scan. We want to visit each 1702*d9e23210SJeff Roberson * page at least once a minute. 1703*d9e23210SJeff Roberson */ 1704*d9e23210SJeff Roberson if (vm_pageout_update_period == 0) 1705*d9e23210SJeff Roberson vm_pageout_update_period = 60; 1706*d9e23210SJeff Roberson 1707df8bae1dSRodney W. Grimes /* XXX does not really belong here */ 1708df8bae1dSRodney W. Grimes if (vm_page_max_wired == 0) 17092feb50bfSAttilio Rao vm_page_max_wired = cnt.v_free_count / 3; 1710df8bae1dSRodney W. Grimes 171124a1cce3SDavid Greenman swap_pager_swap_init(); 1712449c2e92SKonstantin Belousov #if MAXMEMDOM > 1 1713449c2e92SKonstantin Belousov for (i = 1; i < vm_ndomains; i++) { 1714449c2e92SKonstantin Belousov error = kthread_add(vm_pageout_worker, (void *)(uintptr_t)i, 1715449c2e92SKonstantin Belousov curproc, NULL, 0, 0, "dom%d", i); 1716449c2e92SKonstantin Belousov if (error != 0) { 1717449c2e92SKonstantin Belousov panic("starting pageout for domain %d, error %d\n", 1718449c2e92SKonstantin Belousov i, error); 1719dc2efb27SJohn Dyson } 1720f919ebdeSDavid Greenman } 1721449c2e92SKonstantin Belousov #endif 1722449c2e92SKonstantin Belousov vm_pageout_worker((uintptr_t)0); 1723df8bae1dSRodney W. Grimes } 172426f9a767SRodney W. Grimes 17256b4b77adSAlan Cox /* 1726e9f995d8SAlan Cox * Unless the free page queue lock is held by the caller, this function 17276b4b77adSAlan Cox * should be regarded as advisory. Specifically, the caller should 17286b4b77adSAlan Cox * not msleep() on &cnt.v_free_count following this function unless 1729e9f995d8SAlan Cox * the free page queue lock is held until the msleep() is performed. 17306b4b77adSAlan Cox */ 1731e0c5a895SJohn Dyson void 17324a365329SAndrey Zonov pagedaemon_wakeup(void) 1733e0c5a895SJohn Dyson { 1734a1c0a785SAlan Cox 1735b40ce416SJulian Elischer if (!vm_pages_needed && curthread->td_proc != pageproc) { 1736a1c0a785SAlan Cox vm_pages_needed = 1; 1737e0c5a895SJohn Dyson wakeup(&vm_pages_needed); 1738e0c5a895SJohn Dyson } 1739e0c5a895SJohn Dyson } 1740e0c5a895SJohn Dyson 174138efa82bSJohn Dyson #if !defined(NO_SWAPPING) 17425afce282SDavid Greenman static void 174397824da3SAlan Cox vm_req_vmdaemon(int req) 17445afce282SDavid Greenman { 17455afce282SDavid Greenman static int lastrun = 0; 17465afce282SDavid Greenman 174797824da3SAlan Cox mtx_lock(&vm_daemon_mtx); 174897824da3SAlan Cox vm_pageout_req_swapout |= req; 1749b18bfc3dSJohn Dyson if ((ticks > (lastrun + hz)) || (ticks < lastrun)) { 17505afce282SDavid Greenman wakeup(&vm_daemon_needed); 17515afce282SDavid Greenman lastrun = ticks; 17525afce282SDavid Greenman } 175397824da3SAlan Cox mtx_unlock(&vm_daemon_mtx); 17545afce282SDavid Greenman } 17555afce282SDavid Greenman 17562b14f991SJulian Elischer static void 17574a365329SAndrey Zonov vm_daemon(void) 17580d94caffSDavid Greenman { 175991d5354aSJohn Baldwin struct rlimit rsslim; 1760dcbcd518SBruce Evans struct proc *p; 1761dcbcd518SBruce Evans struct thread *td; 17626bed074cSKonstantin Belousov struct vmspace *vm; 1763099e7e95SEdward Tomasz Napierala int breakout, swapout_flags, tryagain, attempts; 1764afcc55f3SEdward Tomasz Napierala #ifdef RACCT 1765099e7e95SEdward Tomasz Napierala uint64_t rsize, ravailable; 1766afcc55f3SEdward Tomasz Napierala #endif 17670d94caffSDavid Greenman 17682fe6e4d7SDavid Greenman while (TRUE) { 176997824da3SAlan Cox mtx_lock(&vm_daemon_mtx); 1770099e7e95SEdward Tomasz Napierala #ifdef RACCT 1771099e7e95SEdward Tomasz Napierala msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", hz); 1772099e7e95SEdward Tomasz Napierala #else 177397824da3SAlan Cox msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", 0); 1774099e7e95SEdward Tomasz Napierala #endif 177597824da3SAlan Cox swapout_flags = vm_pageout_req_swapout; 17764c1f8ee9SDavid Greenman vm_pageout_req_swapout = 0; 177797824da3SAlan Cox mtx_unlock(&vm_daemon_mtx); 177897824da3SAlan Cox if (swapout_flags) 177997824da3SAlan Cox swapout_procs(swapout_flags); 178097824da3SAlan Cox 17812fe6e4d7SDavid Greenman /* 17820d94caffSDavid Greenman * scan the processes for exceeding their rlimits or if 17830d94caffSDavid Greenman * process is swapped out -- deactivate pages 17842fe6e4d7SDavid Greenman */ 1785099e7e95SEdward Tomasz Napierala tryagain = 0; 1786099e7e95SEdward Tomasz Napierala attempts = 0; 1787099e7e95SEdward Tomasz Napierala again: 1788099e7e95SEdward Tomasz Napierala attempts++; 17891005a129SJohn Baldwin sx_slock(&allproc_lock); 1790f67af5c9SXin LI FOREACH_PROC_IN_SYSTEM(p) { 1791fe2144fdSLuoqi Chen vm_pindex_t limit, size; 17922fe6e4d7SDavid Greenman 17932fe6e4d7SDavid Greenman /* 17942fe6e4d7SDavid Greenman * if this is a system process or if we have already 17952fe6e4d7SDavid Greenman * looked at this process, skip it. 17962fe6e4d7SDavid Greenman */ 1797897ecacdSJohn Baldwin PROC_LOCK(p); 17988e6fa660SJohn Baldwin if (p->p_state != PRS_NORMAL || 17998e6fa660SJohn Baldwin p->p_flag & (P_INEXEC | P_SYSTEM | P_WEXIT)) { 1800897ecacdSJohn Baldwin PROC_UNLOCK(p); 18012fe6e4d7SDavid Greenman continue; 18022fe6e4d7SDavid Greenman } 18032fe6e4d7SDavid Greenman /* 18042fe6e4d7SDavid Greenman * if the process is in a non-running type state, 18052fe6e4d7SDavid Greenman * don't touch it. 18062fe6e4d7SDavid Greenman */ 1807e602ba25SJulian Elischer breakout = 0; 1808e602ba25SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 1809982d11f8SJeff Roberson thread_lock(td); 181071fad9fdSJulian Elischer if (!TD_ON_RUNQ(td) && 181171fad9fdSJulian Elischer !TD_IS_RUNNING(td) && 1812f497cda2SEdward Tomasz Napierala !TD_IS_SLEEPING(td) && 1813f497cda2SEdward Tomasz Napierala !TD_IS_SUSPENDED(td)) { 1814982d11f8SJeff Roberson thread_unlock(td); 1815e602ba25SJulian Elischer breakout = 1; 1816e602ba25SJulian Elischer break; 1817e602ba25SJulian Elischer } 1818982d11f8SJeff Roberson thread_unlock(td); 1819e602ba25SJulian Elischer } 1820897ecacdSJohn Baldwin if (breakout) { 1821897ecacdSJohn Baldwin PROC_UNLOCK(p); 18222fe6e4d7SDavid Greenman continue; 18232fe6e4d7SDavid Greenman } 18242fe6e4d7SDavid Greenman /* 18252fe6e4d7SDavid Greenman * get a limit 18262fe6e4d7SDavid Greenman */ 1827dcbcd518SBruce Evans lim_rlimit(p, RLIMIT_RSS, &rsslim); 1828fe2144fdSLuoqi Chen limit = OFF_TO_IDX( 182991d5354aSJohn Baldwin qmin(rsslim.rlim_cur, rsslim.rlim_max)); 18302fe6e4d7SDavid Greenman 18312fe6e4d7SDavid Greenman /* 18320d94caffSDavid Greenman * let processes that are swapped out really be 18330d94caffSDavid Greenman * swapped out set the limit to nothing (will force a 18340d94caffSDavid Greenman * swap-out.) 18352fe6e4d7SDavid Greenman */ 1836b61ce5b0SJeff Roberson if ((p->p_flag & P_INMEM) == 0) 18370d94caffSDavid Greenman limit = 0; /* XXX */ 18386bed074cSKonstantin Belousov vm = vmspace_acquire_ref(p); 1839897ecacdSJohn Baldwin PROC_UNLOCK(p); 18406bed074cSKonstantin Belousov if (vm == NULL) 18416bed074cSKonstantin Belousov continue; 18422fe6e4d7SDavid Greenman 18436bed074cSKonstantin Belousov size = vmspace_resident_count(vm); 1844a406d8c3SEdward Tomasz Napierala if (size >= limit) { 1845fe2144fdSLuoqi Chen vm_pageout_map_deactivate_pages( 18466bed074cSKonstantin Belousov &vm->vm_map, limit); 18472fe6e4d7SDavid Greenman } 1848afcc55f3SEdward Tomasz Napierala #ifdef RACCT 1849099e7e95SEdward Tomasz Napierala rsize = IDX_TO_OFF(size); 1850099e7e95SEdward Tomasz Napierala PROC_LOCK(p); 1851099e7e95SEdward Tomasz Napierala racct_set(p, RACCT_RSS, rsize); 1852099e7e95SEdward Tomasz Napierala ravailable = racct_get_available(p, RACCT_RSS); 1853099e7e95SEdward Tomasz Napierala PROC_UNLOCK(p); 1854099e7e95SEdward Tomasz Napierala if (rsize > ravailable) { 1855099e7e95SEdward Tomasz Napierala /* 1856099e7e95SEdward Tomasz Napierala * Don't be overly aggressive; this might be 1857099e7e95SEdward Tomasz Napierala * an innocent process, and the limit could've 1858099e7e95SEdward Tomasz Napierala * been exceeded by some memory hog. Don't 1859099e7e95SEdward Tomasz Napierala * try to deactivate more than 1/4th of process' 1860099e7e95SEdward Tomasz Napierala * resident set size. 1861099e7e95SEdward Tomasz Napierala */ 1862099e7e95SEdward Tomasz Napierala if (attempts <= 8) { 1863099e7e95SEdward Tomasz Napierala if (ravailable < rsize - (rsize / 4)) 1864099e7e95SEdward Tomasz Napierala ravailable = rsize - (rsize / 4); 1865099e7e95SEdward Tomasz Napierala } 1866099e7e95SEdward Tomasz Napierala vm_pageout_map_deactivate_pages( 1867099e7e95SEdward Tomasz Napierala &vm->vm_map, OFF_TO_IDX(ravailable)); 1868099e7e95SEdward Tomasz Napierala /* Update RSS usage after paging out. */ 1869099e7e95SEdward Tomasz Napierala size = vmspace_resident_count(vm); 1870099e7e95SEdward Tomasz Napierala rsize = IDX_TO_OFF(size); 1871099e7e95SEdward Tomasz Napierala PROC_LOCK(p); 1872099e7e95SEdward Tomasz Napierala racct_set(p, RACCT_RSS, rsize); 1873099e7e95SEdward Tomasz Napierala PROC_UNLOCK(p); 1874099e7e95SEdward Tomasz Napierala if (rsize > ravailable) 1875099e7e95SEdward Tomasz Napierala tryagain = 1; 1876099e7e95SEdward Tomasz Napierala } 1877afcc55f3SEdward Tomasz Napierala #endif 18786bed074cSKonstantin Belousov vmspace_free(vm); 18792fe6e4d7SDavid Greenman } 18801005a129SJohn Baldwin sx_sunlock(&allproc_lock); 1881099e7e95SEdward Tomasz Napierala if (tryagain != 0 && attempts <= 10) 1882099e7e95SEdward Tomasz Napierala goto again; 188324a1cce3SDavid Greenman } 18842fe6e4d7SDavid Greenman } 1885a1287949SEivind Eklund #endif /* !defined(NO_SWAPPING) */ 1886