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. 8df8bae1dSRodney W. Grimes * 9df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 10df8bae1dSRodney W. Grimes * The Mach Operating System project at Carnegie-Mellon University. 11df8bae1dSRodney W. Grimes * 12df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 13df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 14df8bae1dSRodney W. Grimes * are met: 15df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 17df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 18df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 19df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 20df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 215929bcfaSPhilippe Charnier * must display the following acknowledgement: 22df8bae1dSRodney W. Grimes * This product includes software developed by the University of 23df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 24df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 25df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 26df8bae1dSRodney W. Grimes * without specific prior written permission. 27df8bae1dSRodney W. Grimes * 28df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38df8bae1dSRodney W. Grimes * SUCH DAMAGE. 39df8bae1dSRodney W. Grimes * 403c4dd356SDavid Greenman * from: @(#)vm_pageout.c 7.4 (Berkeley) 5/7/91 41df8bae1dSRodney W. Grimes * 42df8bae1dSRodney W. Grimes * 43df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1990 Carnegie-Mellon University. 44df8bae1dSRodney W. Grimes * All rights reserved. 45df8bae1dSRodney W. Grimes * 46df8bae1dSRodney W. Grimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young 47df8bae1dSRodney W. Grimes * 48df8bae1dSRodney W. Grimes * Permission to use, copy, modify and distribute this software and 49df8bae1dSRodney W. Grimes * its documentation is hereby granted, provided that both the copyright 50df8bae1dSRodney W. Grimes * notice and this permission notice appear in all copies of the 51df8bae1dSRodney W. Grimes * software, derivative works or modified versions, and any portions 52df8bae1dSRodney W. Grimes * thereof, and that both notices appear in supporting documentation. 53df8bae1dSRodney W. Grimes * 54df8bae1dSRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 55df8bae1dSRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 56df8bae1dSRodney W. Grimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 57df8bae1dSRodney W. Grimes * 58df8bae1dSRodney W. Grimes * Carnegie Mellon requests users of this software to return to 59df8bae1dSRodney W. Grimes * 60df8bae1dSRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 61df8bae1dSRodney W. Grimes * School of Computer Science 62df8bae1dSRodney W. Grimes * Carnegie Mellon University 63df8bae1dSRodney W. Grimes * Pittsburgh PA 15213-3890 64df8bae1dSRodney W. Grimes * 65df8bae1dSRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the 66df8bae1dSRodney W. Grimes * rights to redistribute these changes. 67df8bae1dSRodney W. Grimes */ 68df8bae1dSRodney W. Grimes 69df8bae1dSRodney W. Grimes /* 70df8bae1dSRodney W. Grimes * The proverbial page-out daemon. 71df8bae1dSRodney W. Grimes */ 72df8bae1dSRodney W. Grimes 73874651b1SDavid E. O'Brien #include <sys/cdefs.h> 74874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 75874651b1SDavid E. O'Brien 76faa5f8d8SAndrzej Bialecki #include "opt_vm.h" 77df8bae1dSRodney W. Grimes #include <sys/param.h> 7826f9a767SRodney W. Grimes #include <sys/systm.h> 79b5e8ce9fSBruce Evans #include <sys/kernel.h> 80855a310fSJeff Roberson #include <sys/eventhandler.h> 81fb919e4dSMark Murray #include <sys/lock.h> 82fb919e4dSMark Murray #include <sys/mutex.h> 8326f9a767SRodney W. Grimes #include <sys/proc.h> 849c8b8baaSPeter Wemm #include <sys/kthread.h> 850384fff8SJason Evans #include <sys/ktr.h> 8626f9a767SRodney W. Grimes #include <sys/resourcevar.h> 87b43179fbSJeff Roberson #include <sys/sched.h> 88d2fc5315SPoul-Henning Kamp #include <sys/signalvar.h> 89f6b04d2bSDavid Greenman #include <sys/vnode.h> 90efeaf95aSDavid Greenman #include <sys/vmmeter.h> 911005a129SJohn Baldwin #include <sys/sx.h> 9238efa82bSJohn Dyson #include <sys/sysctl.h> 93df8bae1dSRodney W. Grimes 94df8bae1dSRodney W. Grimes #include <vm/vm.h> 95efeaf95aSDavid Greenman #include <vm/vm_param.h> 96efeaf95aSDavid Greenman #include <vm/vm_object.h> 97df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 98efeaf95aSDavid Greenman #include <vm/vm_map.h> 99df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h> 10024a1cce3SDavid Greenman #include <vm/vm_pager.h> 10105f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h> 102efeaf95aSDavid Greenman #include <vm/vm_extern.h> 103670d17b5SJeff Roberson #include <vm/uma.h> 104df8bae1dSRodney W. Grimes 1050384fff8SJason Evans #include <machine/mutex.h> 1060384fff8SJason Evans 1072b14f991SJulian Elischer /* 1082b14f991SJulian Elischer * System initialization 1092b14f991SJulian Elischer */ 1102b14f991SJulian Elischer 1112b14f991SJulian Elischer /* the kernel process "vm_pageout"*/ 11211caded3SAlfred Perlstein static void vm_pageout(void); 11311caded3SAlfred Perlstein static int vm_pageout_clean(vm_page_t); 114eea85e9bSAlan Cox static void vm_pageout_pmap_collect(void); 11511caded3SAlfred Perlstein static void vm_pageout_scan(int pass); 11645ae1d91SAlan Cox 1172b14f991SJulian Elischer struct proc *pageproc; 1182b14f991SJulian Elischer 1192b14f991SJulian Elischer static struct kproc_desc page_kp = { 1202b14f991SJulian Elischer "pagedaemon", 1212b14f991SJulian Elischer vm_pageout, 1222b14f991SJulian Elischer &pageproc 1232b14f991SJulian Elischer }; 1249c8b8baaSPeter Wemm SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, &page_kp) 1252b14f991SJulian Elischer 12638efa82bSJohn Dyson #if !defined(NO_SWAPPING) 1272b14f991SJulian Elischer /* the kernel process "vm_daemon"*/ 12811caded3SAlfred Perlstein static void vm_daemon(void); 129f708ef1bSPoul-Henning Kamp static struct proc *vmproc; 1302b14f991SJulian Elischer 1312b14f991SJulian Elischer static struct kproc_desc vm_kp = { 1322b14f991SJulian Elischer "vmdaemon", 1332b14f991SJulian Elischer vm_daemon, 1342b14f991SJulian Elischer &vmproc 1352b14f991SJulian Elischer }; 1369c8b8baaSPeter Wemm SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp) 13738efa82bSJohn Dyson #endif 1382b14f991SJulian Elischer 1392b14f991SJulian Elischer 1408b245767SAlan Cox int vm_pages_needed; /* Event on which pageout daemon sleeps */ 1418b245767SAlan Cox int vm_pageout_deficit; /* Estimated number of pages deficit */ 1428b245767SAlan Cox int vm_pageout_pages_needed; /* flag saying that the pageout daemon needs pages */ 14326f9a767SRodney W. Grimes 14438efa82bSJohn Dyson #if !defined(NO_SWAPPING) 145f708ef1bSPoul-Henning Kamp static int vm_pageout_req_swapout; /* XXX */ 146f708ef1bSPoul-Henning Kamp static int vm_daemon_needed; 14738efa82bSJohn Dyson #endif 1482b6b0df7SMatthew Dillon static int vm_max_launder = 32; 149303b270bSEivind Eklund static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0; 150303b270bSEivind Eklund static int vm_pageout_full_stats_interval = 0; 15126354d4cSAlan Cox static int vm_pageout_algorithm=0; 152303b270bSEivind Eklund static int defer_swap_pageouts=0; 153303b270bSEivind Eklund static int disable_swap_pageouts=0; 15470111b90SJohn Dyson 15538efa82bSJohn Dyson #if defined(NO_SWAPPING) 156303b270bSEivind Eklund static int vm_swap_enabled=0; 157303b270bSEivind Eklund static int vm_swap_idle_enabled=0; 15838efa82bSJohn Dyson #else 159303b270bSEivind Eklund static int vm_swap_enabled=1; 160303b270bSEivind Eklund static int vm_swap_idle_enabled=0; 16138efa82bSJohn Dyson #endif 16238efa82bSJohn Dyson 16338efa82bSJohn Dyson SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm, 1642b6b0df7SMatthew Dillon CTLFLAG_RW, &vm_pageout_algorithm, 0, "LRU page mgmt"); 1652b6b0df7SMatthew Dillon 1662b6b0df7SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, max_launder, 1672b6b0df7SMatthew Dillon CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout"); 16838efa82bSJohn Dyson 169dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max, 170b0359e2cSPeter Wemm CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length"); 171dc2efb27SJohn Dyson 172dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval, 173b0359e2cSPeter Wemm CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan"); 174dc2efb27SJohn Dyson 175dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval, 176b0359e2cSPeter Wemm CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan"); 177dc2efb27SJohn Dyson 17838efa82bSJohn Dyson #if defined(NO_SWAPPING) 179ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 180ceb0cf87SJohn Dyson CTLFLAG_RD, &vm_swap_enabled, 0, ""); 181ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 182ceb0cf87SJohn Dyson CTLFLAG_RD, &vm_swap_idle_enabled, 0, ""); 18338efa82bSJohn Dyson #else 184ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 185b0359e2cSPeter Wemm CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout"); 186ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 187b0359e2cSPeter Wemm CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria"); 18838efa82bSJohn Dyson #endif 18926f9a767SRodney W. Grimes 190ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts, 191b0359e2cSPeter Wemm CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem"); 19212ac6a1dSJohn Dyson 193ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts, 194b0359e2cSPeter Wemm CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages"); 19512ac6a1dSJohn Dyson 19623b59018SMatthew Dillon static int pageout_lock_miss; 19723b59018SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss, 19823b59018SMatthew Dillon CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout"); 19923b59018SMatthew Dillon 200ffc82b0aSJohn Dyson #define VM_PAGEOUT_PAGE_COUNT 16 201bbc0ec52SDavid Greenman int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT; 202df8bae1dSRodney W. Grimes 203c3cb3e12SDavid Greenman int vm_page_max_wired; /* XXX max # of wired pages system-wide */ 204df8bae1dSRodney W. Grimes 20538efa82bSJohn Dyson #if !defined(NO_SWAPPING) 206ecf6279fSAlan Cox static void vm_pageout_map_deactivate_pages(vm_map_t, long); 207ecf6279fSAlan Cox static void vm_pageout_object_deactivate_pages(pmap_t, vm_object_t, long); 20811caded3SAlfred Perlstein static void vm_req_vmdaemon(void); 20938efa82bSJohn Dyson #endif 210dc2efb27SJohn Dyson static void vm_pageout_page_stats(void); 211cd41fc12SDavid Greenman 21226f9a767SRodney W. Grimes /* 21326f9a767SRodney W. Grimes * vm_pageout_clean: 21424a1cce3SDavid Greenman * 2150d94caffSDavid Greenman * Clean the page and remove it from the laundry. 21626f9a767SRodney W. Grimes * 2170d94caffSDavid Greenman * We set the busy bit to cause potential page faults on this page to 2181c7c3c6aSMatthew Dillon * block. Note the careful timing, however, the busy bit isn't set till 2191c7c3c6aSMatthew Dillon * late and we cannot do anything that will mess with the page. 22026f9a767SRodney W. Grimes */ 2213af76890SPoul-Henning Kamp static int 2228f9110f6SJohn Dyson vm_pageout_clean(m) 22324a1cce3SDavid Greenman vm_page_t m; 22424a1cce3SDavid Greenman { 22554d92145SMatthew Dillon vm_object_t object; 226f35329acSJohn Dyson vm_page_t mc[2*vm_pageout_page_count]; 2273562af12SAlan Cox int pageout_count; 22890ecac61SMatthew Dillon int ib, is, page_base; 229a316d390SJohn Dyson vm_pindex_t pindex = m->pindex; 23026f9a767SRodney W. Grimes 23155df3298SAlan Cox mtx_assert(&vm_page_queue_mtx, MA_OWNED); 2323562af12SAlan Cox VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 2330cddd8f0SMatthew Dillon 23426f9a767SRodney W. Grimes /* 2351c7c3c6aSMatthew Dillon * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP 2361c7c3c6aSMatthew Dillon * with the new swapper, but we could have serious problems paging 2371c7c3c6aSMatthew Dillon * out other object types if there is insufficient memory. 2381c7c3c6aSMatthew Dillon * 2391c7c3c6aSMatthew Dillon * Unfortunately, checking free memory here is far too late, so the 2401c7c3c6aSMatthew Dillon * check has been moved up a procedural level. 2411c7c3c6aSMatthew Dillon */ 2421c7c3c6aSMatthew Dillon 24324a1cce3SDavid Greenman /* 2448b03c8edSMatthew Dillon * Don't mess with the page if it's busy, held, or special 24524a1cce3SDavid Greenman */ 2468f9110f6SJohn Dyson if ((m->hold_count != 0) || 2473562af12SAlan Cox ((m->busy != 0) || (m->flags & (PG_BUSY|PG_UNMANAGED)))) { 2480d94caffSDavid Greenman return 0; 2498b03c8edSMatthew Dillon } 2500d94caffSDavid Greenman 251f35329acSJohn Dyson mc[vm_pageout_page_count] = m; 25226f9a767SRodney W. Grimes pageout_count = 1; 253f35329acSJohn Dyson page_base = vm_pageout_page_count; 25490ecac61SMatthew Dillon ib = 1; 25590ecac61SMatthew Dillon is = 1; 25690ecac61SMatthew Dillon 25724a1cce3SDavid Greenman /* 25824a1cce3SDavid Greenman * Scan object for clusterable pages. 25924a1cce3SDavid Greenman * 26024a1cce3SDavid Greenman * We can cluster ONLY if: ->> the page is NOT 26124a1cce3SDavid Greenman * clean, wired, busy, held, or mapped into a 26224a1cce3SDavid Greenman * buffer, and one of the following: 26324a1cce3SDavid Greenman * 1) The page is inactive, or a seldom used 26424a1cce3SDavid Greenman * active page. 26524a1cce3SDavid Greenman * -or- 26624a1cce3SDavid Greenman * 2) we force the issue. 26790ecac61SMatthew Dillon * 26890ecac61SMatthew Dillon * During heavy mmap/modification loads the pageout 26990ecac61SMatthew Dillon * daemon can really fragment the underlying file 27090ecac61SMatthew Dillon * due to flushing pages out of order and not trying 27190ecac61SMatthew Dillon * align the clusters (which leave sporatic out-of-order 27290ecac61SMatthew Dillon * holes). To solve this problem we do the reverse scan 27390ecac61SMatthew Dillon * first and attempt to align our cluster, then do a 27490ecac61SMatthew Dillon * forward scan if room remains. 27524a1cce3SDavid Greenman */ 2765163584cSAlan Cox object = m->object; 27790ecac61SMatthew Dillon more: 27890ecac61SMatthew Dillon while (ib && pageout_count < vm_pageout_page_count) { 27924a1cce3SDavid Greenman vm_page_t p; 280f6b04d2bSDavid Greenman 28190ecac61SMatthew Dillon if (ib > pindex) { 28290ecac61SMatthew Dillon ib = 0; 28390ecac61SMatthew Dillon break; 284f6b04d2bSDavid Greenman } 28590ecac61SMatthew Dillon 28690ecac61SMatthew Dillon if ((p = vm_page_lookup(object, pindex - ib)) == NULL) { 28790ecac61SMatthew Dillon ib = 0; 28890ecac61SMatthew Dillon break; 28990ecac61SMatthew Dillon } 2905070c7f8SJohn Dyson if (((p->queue - p->pc) == PQ_CACHE) || 2918b03c8edSMatthew Dillon (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) { 29290ecac61SMatthew Dillon ib = 0; 29390ecac61SMatthew Dillon break; 294f6b04d2bSDavid Greenman } 29524a1cce3SDavid Greenman vm_page_test_dirty(p); 29690ecac61SMatthew Dillon if ((p->dirty & p->valid) == 0 || 29790ecac61SMatthew Dillon p->queue != PQ_INACTIVE || 29857601bcbSMatthew Dillon p->wire_count != 0 || /* may be held by buf cache */ 29957601bcbSMatthew Dillon p->hold_count != 0) { /* may be undergoing I/O */ 30090ecac61SMatthew Dillon ib = 0; 30124a1cce3SDavid Greenman break; 302f6b04d2bSDavid Greenman } 30390ecac61SMatthew Dillon mc[--page_base] = p; 30490ecac61SMatthew Dillon ++pageout_count; 30590ecac61SMatthew Dillon ++ib; 30624a1cce3SDavid Greenman /* 30790ecac61SMatthew Dillon * alignment boundry, stop here and switch directions. Do 30890ecac61SMatthew Dillon * not clear ib. 30924a1cce3SDavid Greenman */ 31090ecac61SMatthew Dillon if ((pindex - (ib - 1)) % vm_pageout_page_count == 0) 31190ecac61SMatthew Dillon break; 31224a1cce3SDavid Greenman } 31390ecac61SMatthew Dillon 31490ecac61SMatthew Dillon while (pageout_count < vm_pageout_page_count && 31590ecac61SMatthew Dillon pindex + is < object->size) { 31690ecac61SMatthew Dillon vm_page_t p; 31790ecac61SMatthew Dillon 31890ecac61SMatthew Dillon if ((p = vm_page_lookup(object, pindex + is)) == NULL) 31990ecac61SMatthew Dillon break; 3205070c7f8SJohn Dyson if (((p->queue - p->pc) == PQ_CACHE) || 3218b03c8edSMatthew Dillon (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) { 32290ecac61SMatthew Dillon break; 32324a1cce3SDavid Greenman } 32424a1cce3SDavid Greenman vm_page_test_dirty(p); 32590ecac61SMatthew Dillon if ((p->dirty & p->valid) == 0 || 32690ecac61SMatthew Dillon p->queue != PQ_INACTIVE || 32757601bcbSMatthew Dillon p->wire_count != 0 || /* may be held by buf cache */ 32857601bcbSMatthew Dillon p->hold_count != 0) { /* may be undergoing I/O */ 32924a1cce3SDavid Greenman break; 33024a1cce3SDavid Greenman } 33190ecac61SMatthew Dillon mc[page_base + pageout_count] = p; 33290ecac61SMatthew Dillon ++pageout_count; 33390ecac61SMatthew Dillon ++is; 33424a1cce3SDavid Greenman } 33590ecac61SMatthew Dillon 33690ecac61SMatthew Dillon /* 33790ecac61SMatthew Dillon * If we exhausted our forward scan, continue with the reverse scan 33890ecac61SMatthew Dillon * when possible, even past a page boundry. This catches boundry 33990ecac61SMatthew Dillon * conditions. 34090ecac61SMatthew Dillon */ 34190ecac61SMatthew Dillon if (ib && pageout_count < vm_pageout_page_count) 34290ecac61SMatthew Dillon goto more; 343f6b04d2bSDavid Greenman 34467bf6868SJohn Dyson /* 34567bf6868SJohn Dyson * we allow reads during pageouts... 34667bf6868SJohn Dyson */ 3477a935082SAlan Cox return (vm_pageout_flush(&mc[page_base], pageout_count, 0)); 348aef922f5SJohn Dyson } 349aef922f5SJohn Dyson 3501c7c3c6aSMatthew Dillon /* 3511c7c3c6aSMatthew Dillon * vm_pageout_flush() - launder the given pages 3521c7c3c6aSMatthew Dillon * 3531c7c3c6aSMatthew Dillon * The given pages are laundered. Note that we setup for the start of 3541c7c3c6aSMatthew Dillon * I/O ( i.e. busy the page ), mark it read-only, and bump the object 3551c7c3c6aSMatthew Dillon * reference count all in here rather then in the parent. If we want 3561c7c3c6aSMatthew Dillon * the parent to do more sophisticated things we may have to change 3571c7c3c6aSMatthew Dillon * the ordering. 3581c7c3c6aSMatthew Dillon */ 359aef922f5SJohn Dyson int 3607a935082SAlan Cox vm_pageout_flush(vm_page_t *mc, int count, int flags) 361aef922f5SJohn Dyson { 3622e3b314dSAlan Cox vm_object_t object = mc[0]->object; 363aef922f5SJohn Dyson int pageout_status[count]; 36495461b45SJohn Dyson int numpagedout = 0; 365aef922f5SJohn Dyson int i; 366aef922f5SJohn Dyson 36755df3298SAlan Cox mtx_assert(&vm_page_queue_mtx, MA_OWNED); 3682e3b314dSAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 3691c7c3c6aSMatthew Dillon /* 3701c7c3c6aSMatthew Dillon * Initiate I/O. Bump the vm_page_t->busy counter and 3711c7c3c6aSMatthew Dillon * mark the pages read-only. 3721c7c3c6aSMatthew Dillon * 3731c7c3c6aSMatthew Dillon * We do not have to fixup the clean/dirty bits here... we can 3741c7c3c6aSMatthew Dillon * allow the pager to do it after the I/O completes. 37502fa91d3SMatthew Dillon * 37602fa91d3SMatthew Dillon * NOTE! mc[i]->dirty may be partial or fragmented due to an 37702fa91d3SMatthew Dillon * edge case with file fragments. 3781c7c3c6aSMatthew Dillon */ 3798f9110f6SJohn Dyson for (i = 0; i < count; i++) { 3807a935082SAlan Cox KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL, 3817a935082SAlan Cox ("vm_pageout_flush: partially invalid page %p index %d/%d", 3827a935082SAlan Cox mc[i], i, count)); 383e69763a3SDoug Rabson vm_page_io_start(mc[i]); 384a12cc0e4SAlan Cox pmap_page_protect(mc[i], VM_PROT_READ); 3858f9110f6SJohn Dyson } 38655df3298SAlan Cox vm_page_unlock_queues(); 387d474eaaaSDoug Rabson vm_object_pip_add(object, count); 388aef922f5SJohn Dyson 389aef922f5SJohn Dyson vm_pager_put_pages(object, mc, count, 39040bb4f4bSMatthew Dillon (flags | ((object == kernel_object) ? VM_PAGER_PUT_SYNC : 0)), 39126f9a767SRodney W. Grimes pageout_status); 39226f9a767SRodney W. Grimes 39340eab1e9SAlan Cox vm_page_lock_queues(); 394aef922f5SJohn Dyson for (i = 0; i < count; i++) { 395aef922f5SJohn Dyson vm_page_t mt = mc[i]; 39624a1cce3SDavid Greenman 3979ea8d1a6SAlan Cox KASSERT((mt->flags & PG_WRITEABLE) == 0, 3989ea8d1a6SAlan Cox ("vm_pageout_flush: page %p is not write protected", mt)); 39926f9a767SRodney W. Grimes switch (pageout_status[i]) { 40026f9a767SRodney W. Grimes case VM_PAGER_OK: 40126f9a767SRodney W. Grimes case VM_PAGER_PEND: 40295461b45SJohn Dyson numpagedout++; 40326f9a767SRodney W. Grimes break; 40426f9a767SRodney W. Grimes case VM_PAGER_BAD: 40526f9a767SRodney W. Grimes /* 4060d94caffSDavid Greenman * Page outside of range of object. Right now we 4070d94caffSDavid Greenman * essentially lose the changes by pretending it 4080d94caffSDavid Greenman * worked. 40926f9a767SRodney W. Grimes */ 4100385347cSPeter Wemm pmap_clear_modify(mt); 41190ecac61SMatthew Dillon vm_page_undirty(mt); 41226f9a767SRodney W. Grimes break; 41326f9a767SRodney W. Grimes case VM_PAGER_ERROR: 41426f9a767SRodney W. Grimes case VM_PAGER_FAIL: 41526f9a767SRodney W. Grimes /* 4160d94caffSDavid Greenman * If page couldn't be paged out, then reactivate the 4170d94caffSDavid Greenman * page so it doesn't clog the inactive list. (We 4180d94caffSDavid Greenman * will try paging out it again later). 41926f9a767SRodney W. Grimes */ 42024a1cce3SDavid Greenman vm_page_activate(mt); 42126f9a767SRodney W. Grimes break; 42226f9a767SRodney W. Grimes case VM_PAGER_AGAIN: 42326f9a767SRodney W. Grimes break; 42426f9a767SRodney W. Grimes } 42526f9a767SRodney W. Grimes 42626f9a767SRodney W. Grimes /* 4270d94caffSDavid Greenman * If the operation is still going, leave the page busy to 4280d94caffSDavid Greenman * block all other accesses. Also, leave the paging in 4290d94caffSDavid Greenman * progress indicator set so that we don't attempt an object 4300d94caffSDavid Greenman * collapse. 43126f9a767SRodney W. Grimes */ 43226f9a767SRodney W. Grimes if (pageout_status[i] != VM_PAGER_PEND) { 433f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 434e69763a3SDoug Rabson vm_page_io_finish(mt); 4359ea8d1a6SAlan Cox if (vm_page_count_severe()) 4369ea8d1a6SAlan Cox vm_page_try_to_cache(mt); 43726f9a767SRodney W. Grimes } 43826f9a767SRodney W. Grimes } 43995461b45SJohn Dyson return numpagedout; 44026f9a767SRodney W. Grimes } 44126f9a767SRodney W. Grimes 44238efa82bSJohn Dyson #if !defined(NO_SWAPPING) 44326f9a767SRodney W. Grimes /* 44426f9a767SRodney W. Grimes * vm_pageout_object_deactivate_pages 44526f9a767SRodney W. Grimes * 44626f9a767SRodney W. Grimes * deactivate enough pages to satisfy the inactive target 44726f9a767SRodney W. Grimes * requirements or if vm_page_proc_limit is set, then 44826f9a767SRodney W. Grimes * deactivate all of the pages in the object and its 44924a1cce3SDavid Greenman * backing_objects. 45026f9a767SRodney W. Grimes * 45126f9a767SRodney W. Grimes * The object and map must be locked. 45226f9a767SRodney W. Grimes */ 45338efa82bSJohn Dyson static void 454ecf6279fSAlan Cox vm_pageout_object_deactivate_pages(pmap, first_object, desired) 455ecf6279fSAlan Cox pmap_t pmap; 456ecf6279fSAlan Cox vm_object_t first_object; 457ecf6279fSAlan Cox long desired; 45826f9a767SRodney W. Grimes { 459ecf6279fSAlan Cox vm_object_t backing_object, object; 46054d92145SMatthew Dillon vm_page_t p, next; 461ce18aebdSAlan Cox int actcount, rcount, remove_mode; 46226f9a767SRodney W. Grimes 463ecf6279fSAlan Cox VM_OBJECT_LOCK_ASSERT(first_object, MA_OWNED); 464ecf6279fSAlan Cox if (first_object->type == OBJT_DEVICE || first_object->type == OBJT_PHYS) 46538efa82bSJohn Dyson return; 466ecf6279fSAlan Cox for (object = first_object;; object = backing_object) { 467ecf6279fSAlan Cox if (pmap_resident_count(pmap) <= desired) 468ecf6279fSAlan Cox goto unlock_return; 46924a1cce3SDavid Greenman if (object->paging_in_progress) 470ecf6279fSAlan Cox goto unlock_return; 47126f9a767SRodney W. Grimes 47285b1dc89SAlan Cox remove_mode = 0; 47338efa82bSJohn Dyson if (object->shadow_count > 1) 47438efa82bSJohn Dyson remove_mode = 1; 47526f9a767SRodney W. Grimes /* 47626f9a767SRodney W. Grimes * scan the objects entire memory queue 47726f9a767SRodney W. Grimes */ 47826f9a767SRodney W. Grimes rcount = object->resident_page_count; 479b18bfc3dSJohn Dyson p = TAILQ_FIRST(&object->memq); 480ce18aebdSAlan Cox vm_page_lock_queues(); 48126f9a767SRodney W. Grimes while (p && (rcount-- > 0)) { 482ecf6279fSAlan Cox if (pmap_resident_count(pmap) <= desired) { 483ce18aebdSAlan Cox vm_page_unlock_queues(); 484ecf6279fSAlan Cox goto unlock_return; 485ce18aebdSAlan Cox } 486b18bfc3dSJohn Dyson next = TAILQ_NEXT(p, listq); 487a58d1fa1SDavid Greenman cnt.v_pdpages++; 4880d94caffSDavid Greenman if (p->wire_count != 0 || 4890d94caffSDavid Greenman p->hold_count != 0 || 4900d94caffSDavid Greenman p->busy != 0 || 4918b03c8edSMatthew Dillon (p->flags & (PG_BUSY|PG_UNMANAGED)) || 492ecf6279fSAlan Cox !pmap_page_exists_quick(pmap, p)) { 4930d94caffSDavid Greenman p = next; 4940d94caffSDavid Greenman continue; 4950d94caffSDavid Greenman } 4960385347cSPeter Wemm actcount = pmap_ts_referenced(p); 4977e006499SJohn Dyson if (actcount) { 498e69763a3SDoug Rabson vm_page_flag_set(p, PG_REFERENCED); 499c8c4b40cSJohn Dyson } else if (p->flags & PG_REFERENCED) { 5007e006499SJohn Dyson actcount = 1; 501ef743ce6SJohn Dyson } 50238efa82bSJohn Dyson if ((p->queue != PQ_ACTIVE) && 50338efa82bSJohn Dyson (p->flags & PG_REFERENCED)) { 504ef743ce6SJohn Dyson vm_page_activate(p); 5057e006499SJohn Dyson p->act_count += actcount; 506e69763a3SDoug Rabson vm_page_flag_clear(p, PG_REFERENCED); 507c8c4b40cSJohn Dyson } else if (p->queue == PQ_ACTIVE) { 508ef743ce6SJohn Dyson if ((p->flags & PG_REFERENCED) == 0) { 509c8c4b40cSJohn Dyson p->act_count -= min(p->act_count, ACT_DECLINE); 5102b6b0df7SMatthew Dillon if (!remove_mode && (vm_pageout_algorithm || (p->act_count == 0))) { 5114fec79beSAlan Cox pmap_remove_all(p); 51226f9a767SRodney W. Grimes vm_page_deactivate(p); 51326f9a767SRodney W. Grimes } else { 5146d03d577SMatthew Dillon vm_pageq_requeue(p); 515c8c4b40cSJohn Dyson } 516c8c4b40cSJohn Dyson } else { 517eaf13dd7SJohn Dyson vm_page_activate(p); 518e69763a3SDoug Rabson vm_page_flag_clear(p, PG_REFERENCED); 51938efa82bSJohn Dyson if (p->act_count < (ACT_MAX - ACT_ADVANCE)) 52038efa82bSJohn Dyson p->act_count += ACT_ADVANCE; 5216d03d577SMatthew Dillon vm_pageq_requeue(p); 52226f9a767SRodney W. Grimes } 523bd7e5f99SJohn Dyson } else if (p->queue == PQ_INACTIVE) { 5244fec79beSAlan Cox pmap_remove_all(p); 52526f9a767SRodney W. Grimes } 52626f9a767SRodney W. Grimes p = next; 52726f9a767SRodney W. Grimes } 528ce18aebdSAlan Cox vm_page_unlock_queues(); 529ecf6279fSAlan Cox if ((backing_object = object->backing_object) == NULL) 530ecf6279fSAlan Cox goto unlock_return; 531ecf6279fSAlan Cox VM_OBJECT_LOCK(backing_object); 532ecf6279fSAlan Cox if (object != first_object) 533ecf6279fSAlan Cox VM_OBJECT_UNLOCK(object); 53438efa82bSJohn Dyson } 535ecf6279fSAlan Cox unlock_return: 536ecf6279fSAlan Cox if (object != first_object) 537ecf6279fSAlan Cox VM_OBJECT_UNLOCK(object); 53826f9a767SRodney W. Grimes } 53926f9a767SRodney W. Grimes 54026f9a767SRodney W. Grimes /* 54126f9a767SRodney W. Grimes * deactivate some number of pages in a map, try to do it fairly, but 54226f9a767SRodney W. Grimes * that is really hard to do. 54326f9a767SRodney W. Grimes */ 544cd41fc12SDavid Greenman static void 54538efa82bSJohn Dyson vm_pageout_map_deactivate_pages(map, desired) 54626f9a767SRodney W. Grimes vm_map_t map; 547ecf6279fSAlan Cox long desired; 54826f9a767SRodney W. Grimes { 54926f9a767SRodney W. Grimes vm_map_entry_t tmpe; 55038efa82bSJohn Dyson vm_object_t obj, bigobj; 55130105b9eSTor Egge int nothingwired; 5520d94caffSDavid Greenman 553d974f03cSAlan Cox if (!vm_map_trylock(map)) 55426f9a767SRodney W. Grimes return; 55538efa82bSJohn Dyson 55638efa82bSJohn Dyson bigobj = NULL; 55730105b9eSTor Egge nothingwired = TRUE; 55838efa82bSJohn Dyson 55938efa82bSJohn Dyson /* 56038efa82bSJohn Dyson * first, search out the biggest object, and try to free pages from 56138efa82bSJohn Dyson * that. 56238efa82bSJohn Dyson */ 56326f9a767SRodney W. Grimes tmpe = map->header.next; 56438efa82bSJohn Dyson while (tmpe != &map->header) { 5659fdfe602SMatthew Dillon if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 56638efa82bSJohn Dyson obj = tmpe->object.vm_object; 5670774dfb3SAlan Cox if (obj != NULL && VM_OBJECT_TRYLOCK(obj)) { 5680774dfb3SAlan Cox if (obj->shadow_count <= 1 && 5690774dfb3SAlan Cox (bigobj == NULL || 5700774dfb3SAlan Cox bigobj->resident_page_count < obj->resident_page_count)) { 5710774dfb3SAlan Cox if (bigobj != NULL) 5720774dfb3SAlan Cox VM_OBJECT_UNLOCK(bigobj); 57338efa82bSJohn Dyson bigobj = obj; 5740774dfb3SAlan Cox } else 5750774dfb3SAlan Cox VM_OBJECT_UNLOCK(obj); 57638efa82bSJohn Dyson } 57738efa82bSJohn Dyson } 57830105b9eSTor Egge if (tmpe->wired_count > 0) 57930105b9eSTor Egge nothingwired = FALSE; 58038efa82bSJohn Dyson tmpe = tmpe->next; 58138efa82bSJohn Dyson } 58238efa82bSJohn Dyson 5830774dfb3SAlan Cox if (bigobj != NULL) { 584ecf6279fSAlan Cox vm_pageout_object_deactivate_pages(map->pmap, bigobj, desired); 5850774dfb3SAlan Cox VM_OBJECT_UNLOCK(bigobj); 5860774dfb3SAlan Cox } 58738efa82bSJohn Dyson /* 58838efa82bSJohn Dyson * Next, hunt around for other pages to deactivate. We actually 58938efa82bSJohn Dyson * do this search sort of wrong -- .text first is not the best idea. 59038efa82bSJohn Dyson */ 59138efa82bSJohn Dyson tmpe = map->header.next; 59238efa82bSJohn Dyson while (tmpe != &map->header) { 593b1028ad1SLuoqi Chen if (pmap_resident_count(vm_map_pmap(map)) <= desired) 59438efa82bSJohn Dyson break; 5959fdfe602SMatthew Dillon if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 59638efa82bSJohn Dyson obj = tmpe->object.vm_object; 5970774dfb3SAlan Cox if (obj != NULL) { 5980774dfb3SAlan Cox VM_OBJECT_LOCK(obj); 599ecf6279fSAlan Cox vm_pageout_object_deactivate_pages(map->pmap, obj, desired); 6000774dfb3SAlan Cox VM_OBJECT_UNLOCK(obj); 6010774dfb3SAlan Cox } 60238efa82bSJohn Dyson } 60326f9a767SRodney W. Grimes tmpe = tmpe->next; 60438857e7fSAlan Cox } 60538efa82bSJohn Dyson 60638efa82bSJohn Dyson /* 60738efa82bSJohn Dyson * Remove all mappings if a process is swapped out, this will free page 60838efa82bSJohn Dyson * table pages. 60938efa82bSJohn Dyson */ 61038857e7fSAlan Cox if (desired == 0 && nothingwired) { 61105ba50f5SJake Burkholder pmap_remove(vm_map_pmap(map), vm_map_min(map), 61205ba50f5SJake Burkholder vm_map_max(map)); 61338857e7fSAlan Cox } 61438efa82bSJohn Dyson vm_map_unlock(map); 61526f9a767SRodney W. Grimes } 616a1287949SEivind Eklund #endif /* !defined(NO_SWAPPING) */ 617df8bae1dSRodney W. Grimes 6181c7c3c6aSMatthew Dillon /* 619eea85e9bSAlan Cox * This routine is very drastic, but can save the system 620eea85e9bSAlan Cox * in a pinch. 621eea85e9bSAlan Cox */ 622eea85e9bSAlan Cox static void 623eea85e9bSAlan Cox vm_pageout_pmap_collect(void) 624eea85e9bSAlan Cox { 625eea85e9bSAlan Cox int i; 626eea85e9bSAlan Cox vm_page_t m; 627eea85e9bSAlan Cox static int warningdone; 628eea85e9bSAlan Cox 629eea85e9bSAlan Cox if (pmap_pagedaemon_waken == 0) 630eea85e9bSAlan Cox return; 631eea85e9bSAlan Cox if (warningdone < 5) { 632eea85e9bSAlan Cox printf("collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n"); 633eea85e9bSAlan Cox warningdone++; 634eea85e9bSAlan Cox } 635eea85e9bSAlan Cox vm_page_lock_queues(); 636eea85e9bSAlan Cox for (i = 0; i < vm_page_array_size; i++) { 637eea85e9bSAlan Cox m = &vm_page_array[i]; 638eea85e9bSAlan Cox if (m->wire_count || m->hold_count || m->busy || 639eea85e9bSAlan Cox (m->flags & (PG_BUSY | PG_UNMANAGED))) 640eea85e9bSAlan Cox continue; 641eea85e9bSAlan Cox pmap_remove_all(m); 642eea85e9bSAlan Cox } 643eea85e9bSAlan Cox vm_page_unlock_queues(); 644eea85e9bSAlan Cox pmap_pagedaemon_waken = 0; 645eea85e9bSAlan Cox } 646eea85e9bSAlan Cox 647eea85e9bSAlan Cox /* 648df8bae1dSRodney W. Grimes * vm_pageout_scan does the dirty work for the pageout daemon. 649df8bae1dSRodney W. Grimes */ 6502b6b0df7SMatthew Dillon static void 6512b6b0df7SMatthew Dillon vm_pageout_scan(int pass) 652df8bae1dSRodney W. Grimes { 653502ba6e4SJohn Dyson vm_page_t m, next; 654936524aaSMatthew Dillon struct vm_page marker; 6551c7c3c6aSMatthew Dillon int page_shortage, maxscan, pcount; 6561c7c3c6aSMatthew Dillon int addl_page_shortage, addl_page_shortage_init; 6575663e6deSDavid Greenman struct proc *p, *bigproc; 658dcbcd518SBruce Evans struct thread *td; 6595663e6deSDavid Greenman vm_offset_t size, bigsize; 660df8bae1dSRodney W. Grimes vm_object_t object; 661df2e33bfSAlan Cox int actcount, cache_cur, cache_first_failure; 662df2e33bfSAlan Cox static int cache_last_free; 663f6b04d2bSDavid Greenman int vnodes_skipped = 0; 6642b6b0df7SMatthew Dillon int maxlaunder; 6650d94caffSDavid Greenman 6662e3b314dSAlan Cox mtx_lock(&Giant); 667df8bae1dSRodney W. Grimes /* 668855a310fSJeff Roberson * Decrease registered cache sizes. 669855a310fSJeff Roberson */ 670855a310fSJeff Roberson EVENTHANDLER_INVOKE(vm_lowmem, 0); 671855a310fSJeff Roberson /* 672855a310fSJeff Roberson * We do this explicitly after the caches have been drained above. 673855a310fSJeff Roberson */ 674855a310fSJeff Roberson uma_reclaim(); 675855a310fSJeff Roberson /* 6765985940eSJohn Dyson * Do whatever cleanup that the pmap code can. 6775985940eSJohn Dyson */ 678eea85e9bSAlan Cox vm_pageout_pmap_collect(); 6795985940eSJohn Dyson 680b0ef8c5fSAlan Cox addl_page_shortage_init = atomic_readandclear_int(&vm_pageout_deficit); 681b182ec9eSJohn Dyson 6821c7c3c6aSMatthew Dillon /* 6831c7c3c6aSMatthew Dillon * Calculate the number of pages we want to either free or move 6842b6b0df7SMatthew Dillon * to the cache. 6851c7c3c6aSMatthew Dillon */ 6862b6b0df7SMatthew Dillon page_shortage = vm_paging_target() + addl_page_shortage_init; 6871c7c3c6aSMatthew Dillon 6881c7c3c6aSMatthew Dillon /* 689936524aaSMatthew Dillon * Initialize our marker 690936524aaSMatthew Dillon */ 691936524aaSMatthew Dillon bzero(&marker, sizeof(marker)); 692936524aaSMatthew Dillon marker.flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER; 693936524aaSMatthew Dillon marker.queue = PQ_INACTIVE; 694936524aaSMatthew Dillon marker.wire_count = 1; 695936524aaSMatthew Dillon 696936524aaSMatthew Dillon /* 6971c7c3c6aSMatthew Dillon * Start scanning the inactive queue for pages we can move to the 6981c7c3c6aSMatthew Dillon * cache or free. The scan will stop when the target is reached or 699936524aaSMatthew Dillon * we have scanned the entire inactive queue. Note that m->act_count 700936524aaSMatthew Dillon * is not used to form decisions for the inactive queue, only for the 701936524aaSMatthew Dillon * active queue. 7022b6b0df7SMatthew Dillon * 7032b6b0df7SMatthew Dillon * maxlaunder limits the number of dirty pages we flush per scan. 7042b6b0df7SMatthew Dillon * For most systems a smaller value (16 or 32) is more robust under 7052b6b0df7SMatthew Dillon * extreme memory and disk pressure because any unnecessary writes 7062b6b0df7SMatthew Dillon * to disk can result in extreme performance degredation. However, 7072b6b0df7SMatthew Dillon * systems with excessive dirty pages (especially when MAP_NOSYNC is 7082b6b0df7SMatthew Dillon * used) will die horribly with limited laundering. If the pageout 7092b6b0df7SMatthew Dillon * daemon cannot clean enough pages in the first pass, we let it go 7102b6b0df7SMatthew Dillon * all out in succeeding passes. 7111c7c3c6aSMatthew Dillon */ 7122b6b0df7SMatthew Dillon if ((maxlaunder = vm_max_launder) <= 1) 7132b6b0df7SMatthew Dillon maxlaunder = 1; 7142b6b0df7SMatthew Dillon if (pass) 7152b6b0df7SMatthew Dillon maxlaunder = 10000; 7163e1b578aSAlan Cox vm_page_lock_queues(); 71767bf6868SJohn Dyson rescan0: 7181c7c3c6aSMatthew Dillon addl_page_shortage = addl_page_shortage_init; 719f6b04d2bSDavid Greenman maxscan = cnt.v_inactive_count; 7206d03d577SMatthew Dillon 721be72f788SAlan Cox for (m = TAILQ_FIRST(&vm_page_queues[PQ_INACTIVE].pl); 7221c7c3c6aSMatthew Dillon m != NULL && maxscan-- > 0 && page_shortage > 0; 723e929c00dSKirk McKusick m = next) { 724df8bae1dSRodney W. Grimes 725a58d1fa1SDavid Greenman cnt.v_pdpages++; 726b182ec9eSJohn Dyson 727f35329acSJohn Dyson if (m->queue != PQ_INACTIVE) { 72867bf6868SJohn Dyson goto rescan0; 729f35329acSJohn Dyson } 730b182ec9eSJohn Dyson 731b18bfc3dSJohn Dyson next = TAILQ_NEXT(m, pageq); 73234d9e6fdSAlan Cox object = m->object; 733df8bae1dSRodney W. Grimes 734936524aaSMatthew Dillon /* 735936524aaSMatthew Dillon * skip marker pages 736936524aaSMatthew Dillon */ 737936524aaSMatthew Dillon if (m->flags & PG_MARKER) 738936524aaSMatthew Dillon continue; 739936524aaSMatthew Dillon 74057601bcbSMatthew Dillon /* 74157601bcbSMatthew Dillon * A held page may be undergoing I/O, so skip it. 74257601bcbSMatthew Dillon */ 743b182ec9eSJohn Dyson if (m->hold_count) { 7446d03d577SMatthew Dillon vm_pageq_requeue(m); 745b182ec9eSJohn Dyson addl_page_shortage++; 746b182ec9eSJohn Dyson continue; 747df8bae1dSRodney W. Grimes } 74826f9a767SRodney W. Grimes /* 749a1287949SEivind Eklund * Don't mess with busy pages, keep in the front of the 750b18bfc3dSJohn Dyson * queue, most likely are being paged out. 75126f9a767SRodney W. Grimes */ 75234d9e6fdSAlan Cox if (!VM_OBJECT_TRYLOCK(object)) { 75334d9e6fdSAlan Cox addl_page_shortage++; 75434d9e6fdSAlan Cox continue; 75534d9e6fdSAlan Cox } 756bd7e5f99SJohn Dyson if (m->busy || (m->flags & PG_BUSY)) { 75734d9e6fdSAlan Cox VM_OBJECT_UNLOCK(object); 758b182ec9eSJohn Dyson addl_page_shortage++; 75926f9a767SRodney W. Grimes continue; 76026f9a767SRodney W. Grimes } 761bd7e5f99SJohn Dyson 7627e006499SJohn Dyson /* 7631c7c3c6aSMatthew Dillon * If the object is not being used, we ignore previous 7641c7c3c6aSMatthew Dillon * references. 7657e006499SJohn Dyson */ 76634d9e6fdSAlan Cox if (object->ref_count == 0) { 767e69763a3SDoug Rabson vm_page_flag_clear(m, PG_REFERENCED); 7680385347cSPeter Wemm pmap_clear_reference(m); 7697e006499SJohn Dyson 7707e006499SJohn Dyson /* 7711c7c3c6aSMatthew Dillon * Otherwise, if the page has been referenced while in the 7721c7c3c6aSMatthew Dillon * inactive queue, we bump the "activation count" upwards, 7731c7c3c6aSMatthew Dillon * making it less likely that the page will be added back to 7741c7c3c6aSMatthew Dillon * the inactive queue prematurely again. Here we check the 7751c7c3c6aSMatthew Dillon * page tables (or emulated bits, if any), given the upper 7761c7c3c6aSMatthew Dillon * level VM system not knowing anything about existing 7771c7c3c6aSMatthew Dillon * references. 7787e006499SJohn Dyson */ 779ef743ce6SJohn Dyson } else if (((m->flags & PG_REFERENCED) == 0) && 7800385347cSPeter Wemm (actcount = pmap_ts_referenced(m))) { 781ef743ce6SJohn Dyson vm_page_activate(m); 78234d9e6fdSAlan Cox VM_OBJECT_UNLOCK(object); 7837e006499SJohn Dyson m->act_count += (actcount + ACT_ADVANCE); 784ef743ce6SJohn Dyson continue; 7852fe6e4d7SDavid Greenman } 786ef743ce6SJohn Dyson 7877e006499SJohn Dyson /* 7881c7c3c6aSMatthew Dillon * If the upper level VM system knows about any page 7891c7c3c6aSMatthew Dillon * references, we activate the page. We also set the 7901c7c3c6aSMatthew Dillon * "activation count" higher than normal so that we will less 7911c7c3c6aSMatthew Dillon * likely place pages back onto the inactive queue again. 7927e006499SJohn Dyson */ 793bd7e5f99SJohn Dyson if ((m->flags & PG_REFERENCED) != 0) { 794e69763a3SDoug Rabson vm_page_flag_clear(m, PG_REFERENCED); 7950385347cSPeter Wemm actcount = pmap_ts_referenced(m); 79626f9a767SRodney W. Grimes vm_page_activate(m); 79734d9e6fdSAlan Cox VM_OBJECT_UNLOCK(object); 7987e006499SJohn Dyson m->act_count += (actcount + ACT_ADVANCE + 1); 7990d94caffSDavid Greenman continue; 8000d94caffSDavid Greenman } 80167bf6868SJohn Dyson 8027e006499SJohn Dyson /* 8031c7c3c6aSMatthew Dillon * If the upper level VM system doesn't know anything about 8041c7c3c6aSMatthew Dillon * the page being dirty, we have to check for it again. As 8051c7c3c6aSMatthew Dillon * far as the VM code knows, any partially dirty pages are 8061c7c3c6aSMatthew Dillon * fully dirty. 8077e006499SJohn Dyson */ 80884d98bf6SAlan Cox if (m->dirty == 0 && !pmap_is_modified(m)) { 809a3dfacb5SAlan Cox /* 810a3dfacb5SAlan Cox * Avoid a race condition: Unless write access is 811a3dfacb5SAlan Cox * removed from the page, another processor could 812a3dfacb5SAlan Cox * modify it before all access is removed by the call 813a3dfacb5SAlan Cox * to vm_page_cache() below. If vm_page_cache() finds 814a3dfacb5SAlan Cox * that the page has been modified when it removes all 815a3dfacb5SAlan Cox * access, it panics because it cannot cache dirty 816a3dfacb5SAlan Cox * pages. In principle, we could eliminate just write 817a3dfacb5SAlan Cox * access here rather than all access. In the expected 818a3dfacb5SAlan Cox * case, when there are no last instant modifications 819a3dfacb5SAlan Cox * to the page, removing all access will be cheaper 820a3dfacb5SAlan Cox * overall. 821a3dfacb5SAlan Cox */ 82284d98bf6SAlan Cox if ((m->flags & PG_WRITEABLE) != 0) 823a3dfacb5SAlan Cox pmap_remove_all(m); 824427e99a0SAlexander Langer } else { 8257dbf82dcSMatthew Dillon vm_page_dirty(m); 82630dcfc09SJohn Dyson } 827dcbcd518SBruce Evans 8286989c456SAlan Cox if (m->valid == 0) { 8297e006499SJohn Dyson /* 8307e006499SJohn Dyson * Invalid pages can be easily freed 8317e006499SJohn Dyson */ 8326989c456SAlan Cox pmap_remove_all(m); 8336989c456SAlan Cox vm_page_free(m); 8346989c456SAlan Cox cnt.v_dfree++; 8351c7c3c6aSMatthew Dillon --page_shortage; 836bd7e5f99SJohn Dyson } else if (m->dirty == 0) { 8376989c456SAlan Cox /* 8386989c456SAlan Cox * Clean pages can be placed onto the cache queue. 8396989c456SAlan Cox * This effectively frees them. 8406989c456SAlan Cox */ 841bd7e5f99SJohn Dyson vm_page_cache(m); 8421c7c3c6aSMatthew Dillon --page_shortage; 8432b6b0df7SMatthew Dillon } else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) { 8447e006499SJohn Dyson /* 8452b6b0df7SMatthew Dillon * Dirty pages need to be paged out, but flushing 8462b6b0df7SMatthew Dillon * a page is extremely expensive verses freeing 8472b6b0df7SMatthew Dillon * a clean page. Rather then artificially limiting 8482b6b0df7SMatthew Dillon * the number of pages we can flush, we instead give 8492b6b0df7SMatthew Dillon * dirty pages extra priority on the inactive queue 8502b6b0df7SMatthew Dillon * by forcing them to be cycled through the queue 8512b6b0df7SMatthew Dillon * twice before being flushed, after which the 8522b6b0df7SMatthew Dillon * (now clean) page will cycle through once more 8532b6b0df7SMatthew Dillon * before being freed. This significantly extends 8542b6b0df7SMatthew Dillon * the thrash point for a heavily loaded machine. 8557e006499SJohn Dyson */ 8562b6b0df7SMatthew Dillon vm_page_flag_set(m, PG_WINATCFLS); 8576d03d577SMatthew Dillon vm_pageq_requeue(m); 8580d94caffSDavid Greenman } else if (maxlaunder > 0) { 8592b6b0df7SMatthew Dillon /* 8602b6b0df7SMatthew Dillon * We always want to try to flush some dirty pages if 8612b6b0df7SMatthew Dillon * we encounter them, to keep the system stable. 8622b6b0df7SMatthew Dillon * Normally this number is small, but under extreme 8632b6b0df7SMatthew Dillon * pressure where there are insufficient clean pages 8642b6b0df7SMatthew Dillon * on the inactive queue, we may have to go all out. 8652b6b0df7SMatthew Dillon */ 86612ac6a1dSJohn Dyson int swap_pageouts_ok; 867f6b04d2bSDavid Greenman struct vnode *vp = NULL; 868f2a2857bSKirk McKusick struct mount *mp; 8690d94caffSDavid Greenman 87012ac6a1dSJohn Dyson if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) { 87112ac6a1dSJohn Dyson swap_pageouts_ok = 1; 87212ac6a1dSJohn Dyson } else { 87312ac6a1dSJohn Dyson swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts); 87412ac6a1dSJohn Dyson swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts && 87590ecac61SMatthew Dillon vm_page_count_min()); 87612ac6a1dSJohn Dyson 87712ac6a1dSJohn Dyson } 87870111b90SJohn Dyson 87970111b90SJohn Dyson /* 8801c7c3c6aSMatthew Dillon * We don't bother paging objects that are "dead". 8811c7c3c6aSMatthew Dillon * Those objects are in a "rundown" state. 88270111b90SJohn Dyson */ 88370111b90SJohn Dyson if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) { 8843562af12SAlan Cox VM_OBJECT_UNLOCK(object); 8856d03d577SMatthew Dillon vm_pageq_requeue(m); 88612ac6a1dSJohn Dyson continue; 88712ac6a1dSJohn Dyson } 88812ac6a1dSJohn Dyson 8891c7c3c6aSMatthew Dillon /* 8902b6b0df7SMatthew Dillon * The object is already known NOT to be dead. It 8912b6b0df7SMatthew Dillon * is possible for the vget() to block the whole 8922b6b0df7SMatthew Dillon * pageout daemon, but the new low-memory handling 8932b6b0df7SMatthew Dillon * code should prevent it. 8941c7c3c6aSMatthew Dillon * 8952b6b0df7SMatthew Dillon * The previous code skipped locked vnodes and, worse, 8962b6b0df7SMatthew Dillon * reordered pages in the queue. This results in 8972b6b0df7SMatthew Dillon * completely non-deterministic operation and, on a 8982b6b0df7SMatthew Dillon * busy system, can lead to extremely non-optimal 8992b6b0df7SMatthew Dillon * pageouts. For example, it can cause clean pages 9002b6b0df7SMatthew Dillon * to be freed and dirty pages to be moved to the end 9012b6b0df7SMatthew Dillon * of the queue. Since dirty pages are also moved to 9022b6b0df7SMatthew Dillon * the end of the queue once-cleaned, this gives 9032b6b0df7SMatthew Dillon * way too large a weighting to defering the freeing 9042b6b0df7SMatthew Dillon * of dirty pages. 9051c7c3c6aSMatthew Dillon * 90623b59018SMatthew Dillon * We can't wait forever for the vnode lock, we might 90723b59018SMatthew Dillon * deadlock due to a vn_read() getting stuck in 90823b59018SMatthew Dillon * vm_wait while holding this vnode. We skip the 90923b59018SMatthew Dillon * vnode if we can't get it in a reasonable amount 91023b59018SMatthew Dillon * of time. 9111c7c3c6aSMatthew Dillon */ 9121c7c3c6aSMatthew Dillon if (object->type == OBJT_VNODE) { 91324a1cce3SDavid Greenman vp = object->handle; 914f2a2857bSKirk McKusick mp = NULL; 915f2a2857bSKirk McKusick if (vp->v_type == VREG) 916f2a2857bSKirk McKusick vn_start_write(vp, &mp, V_NOWAIT); 9173e1b578aSAlan Cox vm_page_unlock_queues(); 9184b5f5531SAlan Cox VI_LOCK(vp); 9193562af12SAlan Cox VM_OBJECT_UNLOCK(object); 9204b5f5531SAlan Cox if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK | 9214b5f5531SAlan Cox LK_TIMELOCK, curthread)) { 9223562af12SAlan Cox VM_OBJECT_LOCK(object); 9233e1b578aSAlan Cox vm_page_lock_queues(); 92423b59018SMatthew Dillon ++pageout_lock_miss; 925f2a2857bSKirk McKusick vn_finished_write(mp); 926aef922f5SJohn Dyson if (object->flags & OBJ_MIGHTBEDIRTY) 927925a3a41SJohn Dyson vnodes_skipped++; 9283562af12SAlan Cox VM_OBJECT_UNLOCK(object); 929b182ec9eSJohn Dyson continue; 93085a376ebSJohn Dyson } 9313562af12SAlan Cox VM_OBJECT_LOCK(object); 9323e1b578aSAlan Cox vm_page_lock_queues(); 933f35329acSJohn Dyson /* 934936524aaSMatthew Dillon * The page might have been moved to another 935936524aaSMatthew Dillon * queue during potential blocking in vget() 936936524aaSMatthew Dillon * above. The page might have been freed and 937936524aaSMatthew Dillon * reused for another vnode. The object might 938936524aaSMatthew Dillon * have been reused for another vnode. 939f35329acSJohn Dyson */ 940936524aaSMatthew Dillon if (m->queue != PQ_INACTIVE || 941936524aaSMatthew Dillon m->object != object || 942936524aaSMatthew Dillon object->handle != vp) { 943b182ec9eSJohn Dyson if (object->flags & OBJ_MIGHTBEDIRTY) 944925a3a41SJohn Dyson vnodes_skipped++; 9453562af12SAlan Cox goto unlock_and_continue; 946b182ec9eSJohn Dyson } 947b182ec9eSJohn Dyson 948f35329acSJohn Dyson /* 949936524aaSMatthew Dillon * The page may have been busied during the 950936524aaSMatthew Dillon * blocking in vput(); We don't move the 951936524aaSMatthew Dillon * page back onto the end of the queue so that 952936524aaSMatthew Dillon * statistics are more correct if we don't. 953f35329acSJohn Dyson */ 954b182ec9eSJohn Dyson if (m->busy || (m->flags & PG_BUSY)) { 9553562af12SAlan Cox goto unlock_and_continue; 956b182ec9eSJohn Dyson } 957b182ec9eSJohn Dyson 958f35329acSJohn Dyson /* 95957601bcbSMatthew Dillon * If the page has become held it might 96057601bcbSMatthew Dillon * be undergoing I/O, so skip it 961f35329acSJohn Dyson */ 962b182ec9eSJohn Dyson if (m->hold_count) { 9636d03d577SMatthew Dillon vm_pageq_requeue(m); 964b182ec9eSJohn Dyson if (object->flags & OBJ_MIGHTBEDIRTY) 965925a3a41SJohn Dyson vnodes_skipped++; 9663562af12SAlan Cox goto unlock_and_continue; 967f6b04d2bSDavid Greenman } 968f6b04d2bSDavid Greenman } 969f6b04d2bSDavid Greenman 9700d94caffSDavid Greenman /* 9710d94caffSDavid Greenman * If a page is dirty, then it is either being washed 9720d94caffSDavid Greenman * (but not yet cleaned) or it is still in the 9730d94caffSDavid Greenman * laundry. If it is still in the laundry, then we 9742b6b0df7SMatthew Dillon * start the cleaning operation. 975936524aaSMatthew Dillon * 976936524aaSMatthew Dillon * This operation may cluster, invalidating the 'next' 977936524aaSMatthew Dillon * pointer. To prevent an inordinate number of 978936524aaSMatthew Dillon * restarts we use our marker to remember our place. 9792b6b0df7SMatthew Dillon * 9802b6b0df7SMatthew Dillon * decrement page_shortage on success to account for 9812b6b0df7SMatthew Dillon * the (future) cleaned page. Otherwise we could wind 9822b6b0df7SMatthew Dillon * up laundering or cleaning too many pages. 9830d94caffSDavid Greenman */ 984936524aaSMatthew Dillon TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE].pl, m, &marker, pageq); 9852b6b0df7SMatthew Dillon if (vm_pageout_clean(m) != 0) { 9862b6b0df7SMatthew Dillon --page_shortage; 987936524aaSMatthew Dillon --maxlaunder; 9882b6b0df7SMatthew Dillon } 989936524aaSMatthew Dillon next = TAILQ_NEXT(&marker, pageq); 990936524aaSMatthew Dillon TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, &marker, pageq); 9913562af12SAlan Cox unlock_and_continue: 9926989c456SAlan Cox VM_OBJECT_UNLOCK(object); 993f2a2857bSKirk McKusick if (vp) { 9946989c456SAlan Cox vm_page_unlock_queues(); 995f6b04d2bSDavid Greenman vput(vp); 996f2a2857bSKirk McKusick vn_finished_write(mp); 9976989c456SAlan Cox vm_page_lock_queues(); 9986989c456SAlan Cox } 9996989c456SAlan Cox continue; 1000f2a2857bSKirk McKusick } 10013562af12SAlan Cox VM_OBJECT_UNLOCK(object); 10020d94caffSDavid Greenman } 100326f9a767SRodney W. Grimes 1004df8bae1dSRodney W. Grimes /* 1005936524aaSMatthew Dillon * Compute the number of pages we want to try to move from the 1006936524aaSMatthew Dillon * active queue to the inactive queue. 10071c7c3c6aSMatthew Dillon */ 1008936524aaSMatthew Dillon page_shortage = vm_paging_target() + 1009936524aaSMatthew Dillon cnt.v_inactive_target - cnt.v_inactive_count; 1010b182ec9eSJohn Dyson page_shortage += addl_page_shortage; 10111c7c3c6aSMatthew Dillon 10121c7c3c6aSMatthew Dillon /* 1013936524aaSMatthew Dillon * Scan the active queue for things we can deactivate. We nominally 1014936524aaSMatthew Dillon * track the per-page activity counter and use it to locate 1015936524aaSMatthew Dillon * deactivation candidates. 10161c7c3c6aSMatthew Dillon */ 1017b18bfc3dSJohn Dyson pcount = cnt.v_active_count; 1018be72f788SAlan Cox m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl); 10191c7c3c6aSMatthew Dillon 1020b18bfc3dSJohn Dyson while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) { 1021f35329acSJohn Dyson 1022d3c09dd7SAlan Cox KASSERT(m->queue == PQ_ACTIVE, 1023d3c09dd7SAlan Cox ("vm_pageout_scan: page %p isn't active", m)); 1024f35329acSJohn Dyson 1025b18bfc3dSJohn Dyson next = TAILQ_NEXT(m, pageq); 1026b08abf6cSAlan Cox object = m->object; 1027b08abf6cSAlan Cox if (!VM_OBJECT_TRYLOCK(object)) { 1028b08abf6cSAlan Cox vm_pageq_requeue(m); 10294b8a5c40SAlan Cox m = next; 1030b08abf6cSAlan Cox continue; 1031b08abf6cSAlan Cox } 1032b08abf6cSAlan Cox 1033df8bae1dSRodney W. Grimes /* 103426f9a767SRodney W. Grimes * Don't deactivate pages that are busy. 1035df8bae1dSRodney W. Grimes */ 1036a647a309SDavid Greenman if ((m->busy != 0) || 10370d94caffSDavid Greenman (m->flags & PG_BUSY) || 1038f6b04d2bSDavid Greenman (m->hold_count != 0)) { 1039b08abf6cSAlan Cox VM_OBJECT_UNLOCK(object); 10406d03d577SMatthew Dillon vm_pageq_requeue(m); 104126f9a767SRodney W. Grimes m = next; 104226f9a767SRodney W. Grimes continue; 1043df8bae1dSRodney W. Grimes } 1044b18bfc3dSJohn Dyson 1045b18bfc3dSJohn Dyson /* 1046b18bfc3dSJohn Dyson * The count for pagedaemon pages is done after checking the 1047956f3135SPhilippe Charnier * page for eligibility... 1048b18bfc3dSJohn Dyson */ 1049b18bfc3dSJohn Dyson cnt.v_pdpages++; 1050ef743ce6SJohn Dyson 10517e006499SJohn Dyson /* 10527e006499SJohn Dyson * Check to see "how much" the page has been used. 10537e006499SJohn Dyson */ 10547e006499SJohn Dyson actcount = 0; 1055b08abf6cSAlan Cox if (object->ref_count != 0) { 1056ef743ce6SJohn Dyson if (m->flags & PG_REFERENCED) { 10577e006499SJohn Dyson actcount += 1; 10580d94caffSDavid Greenman } 10590385347cSPeter Wemm actcount += pmap_ts_referenced(m); 10607e006499SJohn Dyson if (actcount) { 10617e006499SJohn Dyson m->act_count += ACT_ADVANCE + actcount; 106238efa82bSJohn Dyson if (m->act_count > ACT_MAX) 106338efa82bSJohn Dyson m->act_count = ACT_MAX; 106438efa82bSJohn Dyson } 1065b18bfc3dSJohn Dyson } 1066ef743ce6SJohn Dyson 10677e006499SJohn Dyson /* 10687e006499SJohn Dyson * Since we have "tested" this bit, we need to clear it now. 10697e006499SJohn Dyson */ 1070e69763a3SDoug Rabson vm_page_flag_clear(m, PG_REFERENCED); 1071ef743ce6SJohn Dyson 10727e006499SJohn Dyson /* 10737e006499SJohn Dyson * Only if an object is currently being used, do we use the 10747e006499SJohn Dyson * page activation count stats. 10757e006499SJohn Dyson */ 1076b08abf6cSAlan Cox if (actcount && (object->ref_count != 0)) { 10776d03d577SMatthew Dillon vm_pageq_requeue(m); 107826f9a767SRodney W. Grimes } else { 107938efa82bSJohn Dyson m->act_count -= min(m->act_count, ACT_DECLINE); 10802b6b0df7SMatthew Dillon if (vm_pageout_algorithm || 1081b08abf6cSAlan Cox object->ref_count == 0 || 10822b6b0df7SMatthew Dillon m->act_count == 0) { 1083925a3a41SJohn Dyson page_shortage--; 1084b08abf6cSAlan Cox if (object->ref_count == 0) { 10854fec79beSAlan Cox pmap_remove_all(m); 1086d4a272dbSJohn Dyson if (m->dirty == 0) 10870d94caffSDavid Greenman vm_page_cache(m); 1088d4a272dbSJohn Dyson else 1089d4a272dbSJohn Dyson vm_page_deactivate(m); 10900d94caffSDavid Greenman } else { 109126f9a767SRodney W. Grimes vm_page_deactivate(m); 1092df8bae1dSRodney W. Grimes } 109338efa82bSJohn Dyson } else { 10946d03d577SMatthew Dillon vm_pageq_requeue(m); 109538efa82bSJohn Dyson } 1096df8bae1dSRodney W. Grimes } 1097b08abf6cSAlan Cox VM_OBJECT_UNLOCK(object); 109826f9a767SRodney W. Grimes m = next; 109926f9a767SRodney W. Grimes } 11001c7c3c6aSMatthew Dillon 1101df8bae1dSRodney W. Grimes /* 11020d94caffSDavid Greenman * We try to maintain some *really* free pages, this allows interrupt 11031c7c3c6aSMatthew Dillon * code to be guaranteed space. Since both cache and free queues 11041c7c3c6aSMatthew Dillon * are considered basically 'free', moving pages from cache to free 11051c7c3c6aSMatthew Dillon * does not effect other calculations. 1106df8bae1dSRodney W. Grimes */ 1107df2e33bfSAlan Cox cache_cur = cache_last_free; 1108df2e33bfSAlan Cox cache_first_failure = -1; 1109df2e33bfSAlan Cox while (cnt.v_free_count < cnt.v_free_reserved && (cache_cur = 1110df2e33bfSAlan Cox (cache_cur + PQ_PRIME2) & PQ_L2_MASK) != cache_first_failure) { 1111df2e33bfSAlan Cox TAILQ_FOREACH(m, &vm_page_queues[PQ_CACHE + cache_cur].pl, 1112df2e33bfSAlan Cox pageq) { 1113df2e33bfSAlan Cox KASSERT(m->dirty == 0, 1114df2e33bfSAlan Cox ("Found dirty cache page %p", m)); 1115df2e33bfSAlan Cox KASSERT(!pmap_page_is_mapped(m), 1116df2e33bfSAlan Cox ("Found mapped cache page %p", m)); 1117df2e33bfSAlan Cox KASSERT((m->flags & PG_UNMANAGED) == 0, 1118df2e33bfSAlan Cox ("Found unmanaged cache page %p", m)); 1119df2e33bfSAlan Cox KASSERT(m->wire_count == 0, 1120df2e33bfSAlan Cox ("Found wired cache page %p", m)); 1121df2e33bfSAlan Cox if (m->hold_count == 0 && VM_OBJECT_TRYLOCK(object = 1122df2e33bfSAlan Cox m->object)) { 1123df2e33bfSAlan Cox KASSERT((m->flags & PG_BUSY) == 0 && 1124df2e33bfSAlan Cox m->busy == 0, ("Found busy cache page %p", 1125df2e33bfSAlan Cox m)); 1126ab42316cSAlan Cox vm_page_free(m); 1127ab42316cSAlan Cox VM_OBJECT_UNLOCK(object); 1128ab42316cSAlan Cox cnt.v_dfree++; 1129df2e33bfSAlan Cox cache_last_free = cache_cur; 1130df2e33bfSAlan Cox cache_first_failure = -1; 1131df2e33bfSAlan Cox break; 1132df2e33bfSAlan Cox } 1133df2e33bfSAlan Cox } 1134df2e33bfSAlan Cox if (m == NULL && cache_first_failure == -1) 1135df2e33bfSAlan Cox cache_first_failure = cache_cur; 113626f9a767SRodney W. Grimes } 11378ffc1519SAlan Cox vm_page_unlock_queues(); 1138ceb0cf87SJohn Dyson #if !defined(NO_SWAPPING) 1139ceb0cf87SJohn Dyson /* 1140ceb0cf87SJohn Dyson * Idle process swapout -- run once per second. 1141ceb0cf87SJohn Dyson */ 1142ceb0cf87SJohn Dyson if (vm_swap_idle_enabled) { 1143ceb0cf87SJohn Dyson static long lsec; 1144227ee8a1SPoul-Henning Kamp if (time_second != lsec) { 1145ceb0cf87SJohn Dyson vm_pageout_req_swapout |= VM_SWAP_IDLE; 1146ceb0cf87SJohn Dyson vm_req_vmdaemon(); 1147227ee8a1SPoul-Henning Kamp lsec = time_second; 1148ceb0cf87SJohn Dyson } 1149ceb0cf87SJohn Dyson } 1150ceb0cf87SJohn Dyson #endif 1151ceb0cf87SJohn Dyson 11525663e6deSDavid Greenman /* 1153f6b04d2bSDavid Greenman * If we didn't get enough free pages, and we have skipped a vnode 11544c1f8ee9SDavid Greenman * in a writeable object, wakeup the sync daemon. And kick swapout 11554c1f8ee9SDavid Greenman * if we did not get enough free pages. 1156f6b04d2bSDavid Greenman */ 115790ecac61SMatthew Dillon if (vm_paging_target() > 0) { 115890ecac61SMatthew Dillon if (vnodes_skipped && vm_page_count_min()) 1159d50c1994SPeter Wemm (void) speedup_syncer(); 116038efa82bSJohn Dyson #if !defined(NO_SWAPPING) 116190ecac61SMatthew Dillon if (vm_swap_enabled && vm_page_count_target()) { 11624c1f8ee9SDavid Greenman vm_req_vmdaemon(); 1163ceb0cf87SJohn Dyson vm_pageout_req_swapout |= VM_SWAP_NORMAL; 11644c1f8ee9SDavid Greenman } 11655afce282SDavid Greenman #endif 11664c1f8ee9SDavid Greenman } 11674c1f8ee9SDavid Greenman 1168f6b04d2bSDavid Greenman /* 1169e92686d0SDavid Schultz * If we are critically low on one of RAM or swap and low on 1170e92686d0SDavid Schultz * the other, kill the largest process. However, we avoid 1171e92686d0SDavid Schultz * doing this on the first pass in order to give ourselves a 1172e92686d0SDavid Schultz * chance to flush out dirty vnode-backed pages and to allow 1173e92686d0SDavid Schultz * active pages to be moved to the inactive queue and reclaimed. 11741c58e4e5SJohn Baldwin * 11751c58e4e5SJohn Baldwin * We keep the process bigproc locked once we find it to keep anyone 11761c58e4e5SJohn Baldwin * from messing with it; however, there is a possibility of 11771c58e4e5SJohn Baldwin * deadlock if process B is bigproc and one of it's child processes 11781c58e4e5SJohn Baldwin * attempts to propagate a signal to B while we are waiting for A's 11791c58e4e5SJohn Baldwin * lock while walking this list. To avoid this, we don't block on 11801c58e4e5SJohn Baldwin * the process lock but just skip a process if it is already locked. 11815663e6deSDavid Greenman */ 1182e92686d0SDavid Schultz if (pass != 0 && 11838f60c087SPoul-Henning Kamp ((swap_pager_avail < 64 && vm_page_count_min()) || 1184e92686d0SDavid Schultz (swap_pager_full && vm_paging_target() > 0))) { 11855663e6deSDavid Greenman bigproc = NULL; 11865663e6deSDavid Greenman bigsize = 0; 11871005a129SJohn Baldwin sx_slock(&allproc_lock); 1188e602ba25SJulian Elischer FOREACH_PROC_IN_SYSTEM(p) { 1189e602ba25SJulian Elischer int breakout; 1190dcbcd518SBruce Evans 11911c58e4e5SJohn Baldwin if (PROC_TRYLOCK(p) == 0) 11921c58e4e5SJohn Baldwin continue; 11931c58e4e5SJohn Baldwin /* 1194f4cf2141SWes Peters * If this is a system or protected process, skip it. 11955663e6deSDavid Greenman */ 1196ef6020d1SMike Silbersack if ((p->p_flag & P_SYSTEM) || (p->p_pid == 1) || 1197f4cf2141SWes Peters (p->p_flag & P_PROTECTED) || 11988f60c087SPoul-Henning Kamp ((p->p_pid < 48) && (swap_pager_avail != 0))) { 11998606d880SJohn Baldwin PROC_UNLOCK(p); 12005663e6deSDavid Greenman continue; 12015663e6deSDavid Greenman } 12025663e6deSDavid Greenman /* 1203dcbcd518SBruce Evans * If the process is in a non-running type state, 1204e602ba25SJulian Elischer * don't touch it. Check all the threads individually. 12055663e6deSDavid Greenman */ 12069ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 1207e602ba25SJulian Elischer breakout = 0; 1208e602ba25SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 120971fad9fdSJulian Elischer if (!TD_ON_RUNQ(td) && 121071fad9fdSJulian Elischer !TD_IS_RUNNING(td) && 121171fad9fdSJulian Elischer !TD_IS_SLEEPING(td)) { 1212e602ba25SJulian Elischer breakout = 1; 1213e602ba25SJulian Elischer break; 1214e602ba25SJulian Elischer } 1215e602ba25SJulian Elischer } 1216e602ba25SJulian Elischer if (breakout) { 12179ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 12181c58e4e5SJohn Baldwin PROC_UNLOCK(p); 12195663e6deSDavid Greenman continue; 12205663e6deSDavid Greenman } 12219ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 12225663e6deSDavid Greenman /* 12235663e6deSDavid Greenman * get the process size 12245663e6deSDavid Greenman */ 122572d97679SDavid Schultz if (!vm_map_trylock_read(&p->p_vmspace->vm_map)) { 122672d97679SDavid Schultz PROC_UNLOCK(p); 122772d97679SDavid Schultz continue; 122872d97679SDavid Schultz } 122972d97679SDavid Schultz size = vmspace_swap_count(p->p_vmspace); 123072d97679SDavid Schultz vm_map_unlock_read(&p->p_vmspace->vm_map); 123172d97679SDavid Schultz size += vmspace_resident_count(p->p_vmspace); 12325663e6deSDavid Greenman /* 12335663e6deSDavid Greenman * if the this process is bigger than the biggest one 12345663e6deSDavid Greenman * remember it. 12355663e6deSDavid Greenman */ 12365663e6deSDavid Greenman if (size > bigsize) { 12371c58e4e5SJohn Baldwin if (bigproc != NULL) 12381c58e4e5SJohn Baldwin PROC_UNLOCK(bigproc); 12395663e6deSDavid Greenman bigproc = p; 12405663e6deSDavid Greenman bigsize = size; 12411c58e4e5SJohn Baldwin } else 12421c58e4e5SJohn Baldwin PROC_UNLOCK(p); 12435663e6deSDavid Greenman } 12441005a129SJohn Baldwin sx_sunlock(&allproc_lock); 12455663e6deSDavid Greenman if (bigproc != NULL) { 1246729b1e51SDavid Greenman killproc(bigproc, "out of swap space"); 12479ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 1248fa885116SJulian Elischer sched_nice(bigproc, PRIO_MIN); 12499ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 12501c58e4e5SJohn Baldwin PROC_UNLOCK(bigproc); 125124a1cce3SDavid Greenman wakeup(&cnt.v_free_count); 12525663e6deSDavid Greenman } 12535663e6deSDavid Greenman } 12542e3b314dSAlan Cox mtx_unlock(&Giant); 125526f9a767SRodney W. Grimes } 125626f9a767SRodney W. Grimes 1257dc2efb27SJohn Dyson /* 1258dc2efb27SJohn Dyson * This routine tries to maintain the pseudo LRU active queue, 1259dc2efb27SJohn Dyson * so that during long periods of time where there is no paging, 1260956f3135SPhilippe Charnier * that some statistic accumulation still occurs. This code 1261dc2efb27SJohn Dyson * helps the situation where paging just starts to occur. 1262dc2efb27SJohn Dyson */ 1263dc2efb27SJohn Dyson static void 1264dc2efb27SJohn Dyson vm_pageout_page_stats() 1265dc2efb27SJohn Dyson { 1266b86e6ec0SAlan Cox vm_object_t object; 1267dc2efb27SJohn Dyson vm_page_t m,next; 1268dc2efb27SJohn Dyson int pcount,tpcount; /* Number of pages to check */ 1269dc2efb27SJohn Dyson static int fullintervalcount = 0; 1270bef608bdSJohn Dyson int page_shortage; 1271bef608bdSJohn Dyson 12725e609009SAlan Cox mtx_assert(&vm_page_queue_mtx, MA_OWNED); 127390ecac61SMatthew Dillon page_shortage = 127490ecac61SMatthew Dillon (cnt.v_inactive_target + cnt.v_cache_max + cnt.v_free_min) - 1275bef608bdSJohn Dyson (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count); 127690ecac61SMatthew Dillon 1277bef608bdSJohn Dyson if (page_shortage <= 0) 1278bef608bdSJohn Dyson return; 1279dc2efb27SJohn Dyson 1280dc2efb27SJohn Dyson pcount = cnt.v_active_count; 1281dc2efb27SJohn Dyson fullintervalcount += vm_pageout_stats_interval; 1282dc2efb27SJohn Dyson if (fullintervalcount < vm_pageout_full_stats_interval) { 1283dc2efb27SJohn Dyson tpcount = (vm_pageout_stats_max * cnt.v_active_count) / cnt.v_page_count; 1284dc2efb27SJohn Dyson if (pcount > tpcount) 1285dc2efb27SJohn Dyson pcount = tpcount; 1286883f3caaSMatthew Dillon } else { 1287883f3caaSMatthew Dillon fullintervalcount = 0; 1288dc2efb27SJohn Dyson } 1289dc2efb27SJohn Dyson 1290be72f788SAlan Cox m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl); 1291dc2efb27SJohn Dyson while ((m != NULL) && (pcount-- > 0)) { 12927e006499SJohn Dyson int actcount; 1293dc2efb27SJohn Dyson 1294ab42316cSAlan Cox KASSERT(m->queue == PQ_ACTIVE, 1295ab42316cSAlan Cox ("vm_pageout_page_stats: page %p isn't active", m)); 1296dc2efb27SJohn Dyson 1297dc2efb27SJohn Dyson next = TAILQ_NEXT(m, pageq); 1298b86e6ec0SAlan Cox object = m->object; 1299b86e6ec0SAlan Cox if (!VM_OBJECT_TRYLOCK(object)) { 1300b86e6ec0SAlan Cox vm_pageq_requeue(m); 1301b86e6ec0SAlan Cox m = next; 1302b86e6ec0SAlan Cox continue; 1303b86e6ec0SAlan Cox } 1304b86e6ec0SAlan Cox 1305dc2efb27SJohn Dyson /* 1306dc2efb27SJohn Dyson * Don't deactivate pages that are busy. 1307dc2efb27SJohn Dyson */ 1308dc2efb27SJohn Dyson if ((m->busy != 0) || 1309dc2efb27SJohn Dyson (m->flags & PG_BUSY) || 1310dc2efb27SJohn Dyson (m->hold_count != 0)) { 1311b86e6ec0SAlan Cox VM_OBJECT_UNLOCK(object); 13126d03d577SMatthew Dillon vm_pageq_requeue(m); 1313dc2efb27SJohn Dyson m = next; 1314dc2efb27SJohn Dyson continue; 1315dc2efb27SJohn Dyson } 1316dc2efb27SJohn Dyson 13177e006499SJohn Dyson actcount = 0; 1318dc2efb27SJohn Dyson if (m->flags & PG_REFERENCED) { 1319e69763a3SDoug Rabson vm_page_flag_clear(m, PG_REFERENCED); 13207e006499SJohn Dyson actcount += 1; 1321dc2efb27SJohn Dyson } 1322dc2efb27SJohn Dyson 13230385347cSPeter Wemm actcount += pmap_ts_referenced(m); 13247e006499SJohn Dyson if (actcount) { 13257e006499SJohn Dyson m->act_count += ACT_ADVANCE + actcount; 1326dc2efb27SJohn Dyson if (m->act_count > ACT_MAX) 1327dc2efb27SJohn Dyson m->act_count = ACT_MAX; 13286d03d577SMatthew Dillon vm_pageq_requeue(m); 1329dc2efb27SJohn Dyson } else { 1330dc2efb27SJohn Dyson if (m->act_count == 0) { 13317e006499SJohn Dyson /* 13322b6b0df7SMatthew Dillon * We turn off page access, so that we have 13332b6b0df7SMatthew Dillon * more accurate RSS stats. We don't do this 13342b6b0df7SMatthew Dillon * in the normal page deactivation when the 13352b6b0df7SMatthew Dillon * system is loaded VM wise, because the 13362b6b0df7SMatthew Dillon * cost of the large number of page protect 13372b6b0df7SMatthew Dillon * operations would be higher than the value 13382b6b0df7SMatthew Dillon * of doing the operation. 13397e006499SJohn Dyson */ 13404fec79beSAlan Cox pmap_remove_all(m); 1341dc2efb27SJohn Dyson vm_page_deactivate(m); 1342dc2efb27SJohn Dyson } else { 1343dc2efb27SJohn Dyson m->act_count -= min(m->act_count, ACT_DECLINE); 13446d03d577SMatthew Dillon vm_pageq_requeue(m); 1345dc2efb27SJohn Dyson } 1346dc2efb27SJohn Dyson } 1347b86e6ec0SAlan Cox VM_OBJECT_UNLOCK(object); 1348dc2efb27SJohn Dyson m = next; 1349dc2efb27SJohn Dyson } 1350dc2efb27SJohn Dyson } 1351dc2efb27SJohn Dyson 1352df8bae1dSRodney W. Grimes /* 1353df8bae1dSRodney W. Grimes * vm_pageout is the high level pageout daemon. 1354df8bae1dSRodney W. Grimes */ 13552b14f991SJulian Elischer static void 135626f9a767SRodney W. Grimes vm_pageout() 1357df8bae1dSRodney W. Grimes { 13581aab16a6SAlan Cox int error, pass; 13590384fff8SJason Evans 1360df8bae1dSRodney W. Grimes /* 1361df8bae1dSRodney W. Grimes * Initialize some paging parameters. 1362df8bae1dSRodney W. Grimes */ 1363f6b04d2bSDavid Greenman cnt.v_interrupt_free_min = 2; 1364f35329acSJohn Dyson if (cnt.v_page_count < 2000) 1365f35329acSJohn Dyson vm_pageout_page_count = 8; 1366f6b04d2bSDavid Greenman 136745ae1d91SAlan Cox /* 136845ae1d91SAlan Cox * v_free_reserved needs to include enough for the largest 136945ae1d91SAlan Cox * swap pager structures plus enough for any pv_entry structs 137045ae1d91SAlan Cox * when paging. 137145ae1d91SAlan Cox */ 137245ae1d91SAlan Cox if (cnt.v_page_count > 1024) 137345ae1d91SAlan Cox cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200; 137445ae1d91SAlan Cox else 137545ae1d91SAlan Cox cnt.v_free_min = 4; 137645ae1d91SAlan Cox cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE + 137745ae1d91SAlan Cox cnt.v_interrupt_free_min; 137845ae1d91SAlan Cox cnt.v_free_reserved = vm_pageout_page_count + 137945ae1d91SAlan Cox cnt.v_pageout_free_min + (cnt.v_page_count / 768) + PQ_L2_SIZE; 138045ae1d91SAlan Cox cnt.v_free_severe = cnt.v_free_min / 2; 138145ae1d91SAlan Cox cnt.v_free_min += cnt.v_free_reserved; 138245ae1d91SAlan Cox cnt.v_free_severe += cnt.v_free_reserved; 138345ae1d91SAlan Cox 1384ed74321bSDavid Greenman /* 13852b6b0df7SMatthew Dillon * v_free_target and v_cache_min control pageout hysteresis. Note 13862b6b0df7SMatthew Dillon * that these are more a measure of the VM cache queue hysteresis 13872b6b0df7SMatthew Dillon * then the VM free queue. Specifically, v_free_target is the 13882b6b0df7SMatthew Dillon * high water mark (free+cache pages). 13892b6b0df7SMatthew Dillon * 13902b6b0df7SMatthew Dillon * v_free_reserved + v_cache_min (mostly means v_cache_min) is the 13912b6b0df7SMatthew Dillon * low water mark, while v_free_min is the stop. v_cache_min must 13922b6b0df7SMatthew Dillon * be big enough to handle memory needs while the pageout daemon 13932b6b0df7SMatthew Dillon * is signalled and run to free more pages. 1394ed74321bSDavid Greenman */ 1395a15403deSJohn Dyson if (cnt.v_free_count > 6144) 13962b6b0df7SMatthew Dillon cnt.v_free_target = 4 * cnt.v_free_min + cnt.v_free_reserved; 1397a15403deSJohn Dyson else 1398a15403deSJohn Dyson cnt.v_free_target = 2 * cnt.v_free_min + cnt.v_free_reserved; 13996f2b142eSDavid Greenman 1400a15403deSJohn Dyson if (cnt.v_free_count > 2048) { 1401a15403deSJohn Dyson cnt.v_cache_min = cnt.v_free_target; 1402a15403deSJohn Dyson cnt.v_cache_max = 2 * cnt.v_cache_min; 1403a15403deSJohn Dyson cnt.v_inactive_target = (3 * cnt.v_free_target) / 2; 14040d94caffSDavid Greenman } else { 14050d94caffSDavid Greenman cnt.v_cache_min = 0; 14060d94caffSDavid Greenman cnt.v_cache_max = 0; 14076f2b142eSDavid Greenman cnt.v_inactive_target = cnt.v_free_count / 4; 14080d94caffSDavid Greenman } 1409e47ed70bSJohn Dyson if (cnt.v_inactive_target > cnt.v_free_count / 3) 1410e47ed70bSJohn Dyson cnt.v_inactive_target = cnt.v_free_count / 3; 1411df8bae1dSRodney W. Grimes 1412df8bae1dSRodney W. Grimes /* XXX does not really belong here */ 1413df8bae1dSRodney W. Grimes if (vm_page_max_wired == 0) 1414df8bae1dSRodney W. Grimes vm_page_max_wired = cnt.v_free_count / 3; 1415df8bae1dSRodney W. Grimes 1416dc2efb27SJohn Dyson if (vm_pageout_stats_max == 0) 1417dc2efb27SJohn Dyson vm_pageout_stats_max = cnt.v_free_target; 1418dc2efb27SJohn Dyson 1419dc2efb27SJohn Dyson /* 1420dc2efb27SJohn Dyson * Set interval in seconds for stats scan. 1421dc2efb27SJohn Dyson */ 1422dc2efb27SJohn Dyson if (vm_pageout_stats_interval == 0) 1423bef608bdSJohn Dyson vm_pageout_stats_interval = 5; 1424dc2efb27SJohn Dyson if (vm_pageout_full_stats_interval == 0) 1425dc2efb27SJohn Dyson vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4; 1426dc2efb27SJohn Dyson 142724a1cce3SDavid Greenman swap_pager_swap_init(); 14282b6b0df7SMatthew Dillon pass = 0; 1429df8bae1dSRodney W. Grimes /* 14300d94caffSDavid Greenman * The pageout daemon is never done, so loop forever. 1431df8bae1dSRodney W. Grimes */ 1432df8bae1dSRodney W. Grimes while (TRUE) { 14338e1d8de5SAlan Cox vm_page_lock_queues(); 1434936524aaSMatthew Dillon /* 1435936524aaSMatthew Dillon * If we have enough free memory, wakeup waiters. Do 1436936524aaSMatthew Dillon * not clear vm_pages_needed until we reach our target, 1437936524aaSMatthew Dillon * otherwise we may be woken up over and over again and 1438936524aaSMatthew Dillon * waste a lot of cpu. 1439936524aaSMatthew Dillon */ 1440936524aaSMatthew Dillon if (vm_pages_needed && !vm_page_count_min()) { 1441a1c0a785SAlan Cox if (!vm_paging_needed()) 1442936524aaSMatthew Dillon vm_pages_needed = 0; 1443936524aaSMatthew Dillon wakeup(&cnt.v_free_count); 1444936524aaSMatthew Dillon } 1445936524aaSMatthew Dillon if (vm_pages_needed) { 144690ecac61SMatthew Dillon /* 14472b6b0df7SMatthew Dillon * Still not done, take a second pass without waiting 14482b6b0df7SMatthew Dillon * (unlimited dirty cleaning), otherwise sleep a bit 14492b6b0df7SMatthew Dillon * and try again. 145090ecac61SMatthew Dillon */ 14512b6b0df7SMatthew Dillon ++pass; 14522b6b0df7SMatthew Dillon if (pass > 1) 14538e1d8de5SAlan Cox msleep(&vm_pages_needed, &vm_page_queue_mtx, PVM, 145423955314SAlfred Perlstein "psleep", hz/2); 145590ecac61SMatthew Dillon } else { 145690ecac61SMatthew Dillon /* 14572b6b0df7SMatthew Dillon * Good enough, sleep & handle stats. Prime the pass 14582b6b0df7SMatthew Dillon * for the next run. 145990ecac61SMatthew Dillon */ 14602b6b0df7SMatthew Dillon if (pass > 1) 14612b6b0df7SMatthew Dillon pass = 1; 14622b6b0df7SMatthew Dillon else 14632b6b0df7SMatthew Dillon pass = 0; 14648e1d8de5SAlan Cox error = msleep(&vm_pages_needed, &vm_page_queue_mtx, PVM, 14650cddd8f0SMatthew Dillon "psleep", vm_pageout_stats_interval * hz); 1466dc2efb27SJohn Dyson if (error && !vm_pages_needed) { 14672b6b0df7SMatthew Dillon pass = 0; 1468dc2efb27SJohn Dyson vm_pageout_page_stats(); 14695e609009SAlan Cox vm_page_unlock_queues(); 1470dc2efb27SJohn Dyson continue; 1471dc2efb27SJohn Dyson } 1472f919ebdeSDavid Greenman } 1473b18bfc3dSJohn Dyson if (vm_pages_needed) 1474b18bfc3dSJohn Dyson cnt.v_pdwakeups++; 14758e1d8de5SAlan Cox vm_page_unlock_queues(); 14762b6b0df7SMatthew Dillon vm_pageout_scan(pass); 1477df8bae1dSRodney W. Grimes } 1478df8bae1dSRodney W. Grimes } 147926f9a767SRodney W. Grimes 14806b4b77adSAlan Cox /* 14816b4b77adSAlan Cox * Unless the page queue lock is held by the caller, this function 14826b4b77adSAlan Cox * should be regarded as advisory. Specifically, the caller should 14836b4b77adSAlan Cox * not msleep() on &cnt.v_free_count following this function unless 14846b4b77adSAlan Cox * the page queue lock is held until the msleep() is performed. 14856b4b77adSAlan Cox */ 1486e0c5a895SJohn Dyson void 1487e0c5a895SJohn Dyson pagedaemon_wakeup() 1488e0c5a895SJohn Dyson { 1489a1c0a785SAlan Cox 1490b40ce416SJulian Elischer if (!vm_pages_needed && curthread->td_proc != pageproc) { 1491a1c0a785SAlan Cox vm_pages_needed = 1; 1492e0c5a895SJohn Dyson wakeup(&vm_pages_needed); 1493e0c5a895SJohn Dyson } 1494e0c5a895SJohn Dyson } 1495e0c5a895SJohn Dyson 149638efa82bSJohn Dyson #if !defined(NO_SWAPPING) 14975afce282SDavid Greenman static void 14985afce282SDavid Greenman vm_req_vmdaemon() 14995afce282SDavid Greenman { 15005afce282SDavid Greenman static int lastrun = 0; 15015afce282SDavid Greenman 1502b18bfc3dSJohn Dyson if ((ticks > (lastrun + hz)) || (ticks < lastrun)) { 15035afce282SDavid Greenman wakeup(&vm_daemon_needed); 15045afce282SDavid Greenman lastrun = ticks; 15055afce282SDavid Greenman } 15065afce282SDavid Greenman } 15075afce282SDavid Greenman 15082b14f991SJulian Elischer static void 15094f9fb771SBruce Evans vm_daemon() 15100d94caffSDavid Greenman { 151191d5354aSJohn Baldwin struct rlimit rsslim; 1512dcbcd518SBruce Evans struct proc *p; 1513dcbcd518SBruce Evans struct thread *td; 1514dcbcd518SBruce Evans int breakout; 15150d94caffSDavid Greenman 15163614c6fcSJohn Baldwin mtx_lock(&Giant); 15172fe6e4d7SDavid Greenman while (TRUE) { 15180cddd8f0SMatthew Dillon tsleep(&vm_daemon_needed, PPAUSE, "psleep", 0); 15194c1f8ee9SDavid Greenman if (vm_pageout_req_swapout) { 1520ceb0cf87SJohn Dyson swapout_procs(vm_pageout_req_swapout); 15214c1f8ee9SDavid Greenman vm_pageout_req_swapout = 0; 15224c1f8ee9SDavid Greenman } 15232fe6e4d7SDavid Greenman /* 15240d94caffSDavid Greenman * scan the processes for exceeding their rlimits or if 15250d94caffSDavid Greenman * process is swapped out -- deactivate pages 15262fe6e4d7SDavid Greenman */ 15271005a129SJohn Baldwin sx_slock(&allproc_lock); 1528fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(p, &allproc, p_list) { 1529fe2144fdSLuoqi Chen vm_pindex_t limit, size; 15302fe6e4d7SDavid Greenman 15312fe6e4d7SDavid Greenman /* 15322fe6e4d7SDavid Greenman * if this is a system process or if we have already 15332fe6e4d7SDavid Greenman * looked at this process, skip it. 15342fe6e4d7SDavid Greenman */ 1535897ecacdSJohn Baldwin PROC_LOCK(p); 15362fe6e4d7SDavid Greenman if (p->p_flag & (P_SYSTEM | P_WEXIT)) { 1537897ecacdSJohn Baldwin PROC_UNLOCK(p); 15382fe6e4d7SDavid Greenman continue; 15392fe6e4d7SDavid Greenman } 15402fe6e4d7SDavid Greenman /* 15412fe6e4d7SDavid Greenman * if the process is in a non-running type state, 15422fe6e4d7SDavid Greenman * don't touch it. 15432fe6e4d7SDavid Greenman */ 15449ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 1545e602ba25SJulian Elischer breakout = 0; 1546e602ba25SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 154771fad9fdSJulian Elischer if (!TD_ON_RUNQ(td) && 154871fad9fdSJulian Elischer !TD_IS_RUNNING(td) && 154971fad9fdSJulian Elischer !TD_IS_SLEEPING(td)) { 1550e602ba25SJulian Elischer breakout = 1; 1551e602ba25SJulian Elischer break; 1552e602ba25SJulian Elischer } 1553e602ba25SJulian Elischer } 15549ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 1555897ecacdSJohn Baldwin if (breakout) { 1556897ecacdSJohn Baldwin PROC_UNLOCK(p); 15572fe6e4d7SDavid Greenman continue; 15582fe6e4d7SDavid Greenman } 15592fe6e4d7SDavid Greenman /* 15602fe6e4d7SDavid Greenman * get a limit 15612fe6e4d7SDavid Greenman */ 1562dcbcd518SBruce Evans lim_rlimit(p, RLIMIT_RSS, &rsslim); 1563fe2144fdSLuoqi Chen limit = OFF_TO_IDX( 156491d5354aSJohn Baldwin qmin(rsslim.rlim_cur, rsslim.rlim_max)); 15652fe6e4d7SDavid Greenman 15662fe6e4d7SDavid Greenman /* 15670d94caffSDavid Greenman * let processes that are swapped out really be 15680d94caffSDavid Greenman * swapped out set the limit to nothing (will force a 15690d94caffSDavid Greenman * swap-out.) 15702fe6e4d7SDavid Greenman */ 15718606d880SJohn Baldwin if ((p->p_sflag & PS_INMEM) == 0) 15720d94caffSDavid Greenman limit = 0; /* XXX */ 1573897ecacdSJohn Baldwin PROC_UNLOCK(p); 15742fe6e4d7SDavid Greenman 1575fe2144fdSLuoqi Chen size = vmspace_resident_count(p->p_vmspace); 15762fe6e4d7SDavid Greenman if (limit >= 0 && size >= limit) { 1577fe2144fdSLuoqi Chen vm_pageout_map_deactivate_pages( 1578fe2144fdSLuoqi Chen &p->p_vmspace->vm_map, limit); 15792fe6e4d7SDavid Greenman } 15802fe6e4d7SDavid Greenman } 15811005a129SJohn Baldwin sx_sunlock(&allproc_lock); 158224a1cce3SDavid Greenman } 15832fe6e4d7SDavid Greenman } 1584a1287949SEivind Eklund #endif /* !defined(NO_SWAPPING) */ 1585