1df8bae1dSRodney W. Grimes /* 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. 6726f9a767SRodney W. Grimes * 68c3aac50fSPeter Wemm * $FreeBSD$ 69df8bae1dSRodney W. Grimes */ 70df8bae1dSRodney W. Grimes 71df8bae1dSRodney W. Grimes /* 72df8bae1dSRodney W. Grimes * The proverbial page-out daemon. 73df8bae1dSRodney W. Grimes */ 74df8bae1dSRodney W. Grimes 75faa5f8d8SAndrzej Bialecki #include "opt_vm.h" 76df8bae1dSRodney W. Grimes #include <sys/param.h> 7726f9a767SRodney W. Grimes #include <sys/systm.h> 78b5e8ce9fSBruce Evans #include <sys/kernel.h> 7926f9a767SRodney W. Grimes #include <sys/proc.h> 809c8b8baaSPeter Wemm #include <sys/kthread.h> 810384fff8SJason Evans #include <sys/ktr.h> 8226f9a767SRodney W. Grimes #include <sys/resourcevar.h> 83d2fc5315SPoul-Henning Kamp #include <sys/signalvar.h> 84f6b04d2bSDavid Greenman #include <sys/vnode.h> 85efeaf95aSDavid Greenman #include <sys/vmmeter.h> 861005a129SJohn Baldwin #include <sys/sx.h> 8738efa82bSJohn Dyson #include <sys/sysctl.h> 88df8bae1dSRodney W. Grimes 89df8bae1dSRodney W. Grimes #include <vm/vm.h> 90efeaf95aSDavid Greenman #include <vm/vm_param.h> 91996c772fSJohn Dyson #include <sys/lock.h> 92efeaf95aSDavid Greenman #include <vm/vm_object.h> 93df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 94efeaf95aSDavid Greenman #include <vm/vm_map.h> 95df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h> 9624a1cce3SDavid Greenman #include <vm/vm_pager.h> 9721cd6e62SSeigo Tanimura #include <vm/vm_zone.h> 9805f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h> 99efeaf95aSDavid Greenman #include <vm/vm_extern.h> 100df8bae1dSRodney W. Grimes 1010384fff8SJason Evans #include <machine/mutex.h> 1020384fff8SJason Evans 1032b14f991SJulian Elischer /* 1042b14f991SJulian Elischer * System initialization 1052b14f991SJulian Elischer */ 1062b14f991SJulian Elischer 1072b14f991SJulian Elischer /* the kernel process "vm_pageout"*/ 1082b14f991SJulian Elischer static void vm_pageout __P((void)); 1098f9110f6SJohn Dyson static int vm_pageout_clean __P((vm_page_t)); 1102b6b0df7SMatthew Dillon static void vm_pageout_scan __P((int pass)); 111f35329acSJohn Dyson static int vm_pageout_free_page_calc __P((vm_size_t count)); 1122b14f991SJulian Elischer struct proc *pageproc; 1132b14f991SJulian Elischer 1142b14f991SJulian Elischer static struct kproc_desc page_kp = { 1152b14f991SJulian Elischer "pagedaemon", 1162b14f991SJulian Elischer vm_pageout, 1172b14f991SJulian Elischer &pageproc 1182b14f991SJulian Elischer }; 1199c8b8baaSPeter Wemm SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, &page_kp) 1202b14f991SJulian Elischer 12138efa82bSJohn Dyson #if !defined(NO_SWAPPING) 1222b14f991SJulian Elischer /* the kernel process "vm_daemon"*/ 1232b14f991SJulian Elischer static void vm_daemon __P((void)); 124f708ef1bSPoul-Henning Kamp static struct proc *vmproc; 1252b14f991SJulian Elischer 1262b14f991SJulian Elischer static struct kproc_desc vm_kp = { 1272b14f991SJulian Elischer "vmdaemon", 1282b14f991SJulian Elischer vm_daemon, 1292b14f991SJulian Elischer &vmproc 1302b14f991SJulian Elischer }; 1319c8b8baaSPeter Wemm SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp) 13238efa82bSJohn Dyson #endif 1332b14f991SJulian Elischer 1342b14f991SJulian Elischer 1352d8acc0fSJohn Dyson int vm_pages_needed=0; /* Event on which pageout daemon sleeps */ 1362d8acc0fSJohn Dyson int vm_pageout_deficit=0; /* Estimated number of pages deficit */ 1372d8acc0fSJohn Dyson int vm_pageout_pages_needed=0; /* flag saying that the pageout daemon needs pages */ 13826f9a767SRodney W. Grimes 13938efa82bSJohn Dyson #if !defined(NO_SWAPPING) 140f708ef1bSPoul-Henning Kamp static int vm_pageout_req_swapout; /* XXX */ 141f708ef1bSPoul-Henning Kamp static int vm_daemon_needed; 14238efa82bSJohn Dyson #endif 1435663e6deSDavid Greenman extern int vm_swap_size; 1442b6b0df7SMatthew Dillon static int vm_max_launder = 32; 145303b270bSEivind Eklund static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0; 146303b270bSEivind Eklund static int vm_pageout_full_stats_interval = 0; 1472b6b0df7SMatthew Dillon static int vm_pageout_stats_free_max=0, vm_pageout_algorithm=0; 148303b270bSEivind Eklund static int defer_swap_pageouts=0; 149303b270bSEivind Eklund static int disable_swap_pageouts=0; 15070111b90SJohn Dyson 15138efa82bSJohn Dyson #if defined(NO_SWAPPING) 152303b270bSEivind Eklund static int vm_swap_enabled=0; 153303b270bSEivind Eklund static int vm_swap_idle_enabled=0; 15438efa82bSJohn Dyson #else 155303b270bSEivind Eklund static int vm_swap_enabled=1; 156303b270bSEivind Eklund static int vm_swap_idle_enabled=0; 15738efa82bSJohn Dyson #endif 15838efa82bSJohn Dyson 15938efa82bSJohn Dyson SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm, 1602b6b0df7SMatthew Dillon CTLFLAG_RW, &vm_pageout_algorithm, 0, "LRU page mgmt"); 1612b6b0df7SMatthew Dillon 1622b6b0df7SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, max_launder, 1632b6b0df7SMatthew Dillon CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout"); 16438efa82bSJohn Dyson 165dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max, 166b0359e2cSPeter Wemm CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length"); 167dc2efb27SJohn Dyson 168dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval, 169b0359e2cSPeter Wemm CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan"); 170dc2efb27SJohn Dyson 171dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval, 172b0359e2cSPeter Wemm CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan"); 173dc2efb27SJohn Dyson 174dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_free_max, 175b0359e2cSPeter Wemm CTLFLAG_RW, &vm_pageout_stats_free_max, 0, "Not implemented"); 176dc2efb27SJohn Dyson 17738efa82bSJohn Dyson #if defined(NO_SWAPPING) 178ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 179ceb0cf87SJohn Dyson CTLFLAG_RD, &vm_swap_enabled, 0, ""); 180ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 181ceb0cf87SJohn Dyson CTLFLAG_RD, &vm_swap_idle_enabled, 0, ""); 18238efa82bSJohn Dyson #else 183ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 184b0359e2cSPeter Wemm CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout"); 185ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 186b0359e2cSPeter Wemm CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria"); 18738efa82bSJohn Dyson #endif 18826f9a767SRodney W. Grimes 189ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts, 190b0359e2cSPeter Wemm CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem"); 19112ac6a1dSJohn Dyson 192ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts, 193b0359e2cSPeter Wemm CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages"); 19412ac6a1dSJohn Dyson 195ffc82b0aSJohn Dyson #define VM_PAGEOUT_PAGE_COUNT 16 196bbc0ec52SDavid Greenman int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT; 197df8bae1dSRodney W. Grimes 198c3cb3e12SDavid Greenman int vm_page_max_wired; /* XXX max # of wired pages system-wide */ 199df8bae1dSRodney W. Grimes 20038efa82bSJohn Dyson #if !defined(NO_SWAPPING) 20138efa82bSJohn Dyson typedef void freeer_fcn_t __P((vm_map_t, vm_object_t, vm_pindex_t, int)); 20238efa82bSJohn Dyson static void vm_pageout_map_deactivate_pages __P((vm_map_t, vm_pindex_t)); 203cd41fc12SDavid Greenman static freeer_fcn_t vm_pageout_object_deactivate_pages; 204cd41fc12SDavid Greenman static void vm_req_vmdaemon __P((void)); 20538efa82bSJohn Dyson #endif 206dc2efb27SJohn Dyson static void vm_pageout_page_stats(void); 207cd41fc12SDavid Greenman 20826f9a767SRodney W. Grimes /* 20926f9a767SRodney W. Grimes * vm_pageout_clean: 21024a1cce3SDavid Greenman * 2110d94caffSDavid Greenman * Clean the page and remove it from the laundry. 21226f9a767SRodney W. Grimes * 2130d94caffSDavid Greenman * We set the busy bit to cause potential page faults on this page to 2141c7c3c6aSMatthew Dillon * block. Note the careful timing, however, the busy bit isn't set till 2151c7c3c6aSMatthew Dillon * late and we cannot do anything that will mess with the page. 21626f9a767SRodney W. Grimes */ 2171c7c3c6aSMatthew Dillon 2183af76890SPoul-Henning Kamp static int 2198f9110f6SJohn Dyson vm_pageout_clean(m) 22024a1cce3SDavid Greenman vm_page_t m; 22124a1cce3SDavid Greenman { 22226f9a767SRodney W. Grimes register vm_object_t object; 223f35329acSJohn Dyson vm_page_t mc[2*vm_pageout_page_count]; 22424a1cce3SDavid Greenman int pageout_count; 22590ecac61SMatthew Dillon int ib, is, page_base; 226a316d390SJohn Dyson vm_pindex_t pindex = m->pindex; 22726f9a767SRodney W. Grimes 22826f9a767SRodney W. Grimes object = m->object; 22924a1cce3SDavid Greenman 23026f9a767SRodney W. Grimes /* 2311c7c3c6aSMatthew Dillon * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP 2321c7c3c6aSMatthew Dillon * with the new swapper, but we could have serious problems paging 2331c7c3c6aSMatthew Dillon * out other object types if there is insufficient memory. 2341c7c3c6aSMatthew Dillon * 2351c7c3c6aSMatthew Dillon * Unfortunately, checking free memory here is far too late, so the 2361c7c3c6aSMatthew Dillon * check has been moved up a procedural level. 2371c7c3c6aSMatthew Dillon */ 2381c7c3c6aSMatthew Dillon 23924a1cce3SDavid Greenman /* 2408b03c8edSMatthew Dillon * Don't mess with the page if it's busy, held, or special 24124a1cce3SDavid Greenman */ 2428f9110f6SJohn Dyson if ((m->hold_count != 0) || 2438b03c8edSMatthew Dillon ((m->busy != 0) || (m->flags & (PG_BUSY|PG_UNMANAGED)))) { 2440d94caffSDavid Greenman return 0; 2458b03c8edSMatthew Dillon } 2460d94caffSDavid Greenman 247f35329acSJohn Dyson mc[vm_pageout_page_count] = m; 24826f9a767SRodney W. Grimes pageout_count = 1; 249f35329acSJohn Dyson page_base = vm_pageout_page_count; 25090ecac61SMatthew Dillon ib = 1; 25190ecac61SMatthew Dillon is = 1; 25290ecac61SMatthew Dillon 25324a1cce3SDavid Greenman /* 25424a1cce3SDavid Greenman * Scan object for clusterable pages. 25524a1cce3SDavid Greenman * 25624a1cce3SDavid Greenman * We can cluster ONLY if: ->> the page is NOT 25724a1cce3SDavid Greenman * clean, wired, busy, held, or mapped into a 25824a1cce3SDavid Greenman * buffer, and one of the following: 25924a1cce3SDavid Greenman * 1) The page is inactive, or a seldom used 26024a1cce3SDavid Greenman * active page. 26124a1cce3SDavid Greenman * -or- 26224a1cce3SDavid Greenman * 2) we force the issue. 26390ecac61SMatthew Dillon * 26490ecac61SMatthew Dillon * During heavy mmap/modification loads the pageout 26590ecac61SMatthew Dillon * daemon can really fragment the underlying file 26690ecac61SMatthew Dillon * due to flushing pages out of order and not trying 26790ecac61SMatthew Dillon * align the clusters (which leave sporatic out-of-order 26890ecac61SMatthew Dillon * holes). To solve this problem we do the reverse scan 26990ecac61SMatthew Dillon * first and attempt to align our cluster, then do a 27090ecac61SMatthew Dillon * forward scan if room remains. 27124a1cce3SDavid Greenman */ 27290ecac61SMatthew Dillon 27390ecac61SMatthew Dillon more: 27490ecac61SMatthew Dillon while (ib && pageout_count < vm_pageout_page_count) { 27524a1cce3SDavid Greenman vm_page_t p; 276f6b04d2bSDavid Greenman 27790ecac61SMatthew Dillon if (ib > pindex) { 27890ecac61SMatthew Dillon ib = 0; 27990ecac61SMatthew Dillon break; 280f6b04d2bSDavid Greenman } 28190ecac61SMatthew Dillon 28290ecac61SMatthew Dillon if ((p = vm_page_lookup(object, pindex - ib)) == NULL) { 28390ecac61SMatthew Dillon ib = 0; 28490ecac61SMatthew Dillon break; 28590ecac61SMatthew Dillon } 2865070c7f8SJohn Dyson if (((p->queue - p->pc) == PQ_CACHE) || 2878b03c8edSMatthew Dillon (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) { 28890ecac61SMatthew Dillon ib = 0; 28990ecac61SMatthew Dillon break; 290f6b04d2bSDavid Greenman } 29124a1cce3SDavid Greenman vm_page_test_dirty(p); 29290ecac61SMatthew Dillon if ((p->dirty & p->valid) == 0 || 29390ecac61SMatthew Dillon p->queue != PQ_INACTIVE || 29490ecac61SMatthew Dillon p->wire_count != 0 || 29590ecac61SMatthew Dillon p->hold_count != 0) { 29690ecac61SMatthew Dillon ib = 0; 29724a1cce3SDavid Greenman break; 298f6b04d2bSDavid Greenman } 29990ecac61SMatthew Dillon mc[--page_base] = p; 30090ecac61SMatthew Dillon ++pageout_count; 30190ecac61SMatthew Dillon ++ib; 30224a1cce3SDavid Greenman /* 30390ecac61SMatthew Dillon * alignment boundry, stop here and switch directions. Do 30490ecac61SMatthew Dillon * not clear ib. 30524a1cce3SDavid Greenman */ 30690ecac61SMatthew Dillon if ((pindex - (ib - 1)) % vm_pageout_page_count == 0) 30790ecac61SMatthew Dillon break; 30824a1cce3SDavid Greenman } 30990ecac61SMatthew Dillon 31090ecac61SMatthew Dillon while (pageout_count < vm_pageout_page_count && 31190ecac61SMatthew Dillon pindex + is < object->size) { 31290ecac61SMatthew Dillon vm_page_t p; 31390ecac61SMatthew Dillon 31490ecac61SMatthew Dillon if ((p = vm_page_lookup(object, pindex + is)) == NULL) 31590ecac61SMatthew Dillon break; 3165070c7f8SJohn Dyson if (((p->queue - p->pc) == PQ_CACHE) || 3178b03c8edSMatthew Dillon (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) { 31890ecac61SMatthew Dillon break; 31924a1cce3SDavid Greenman } 32024a1cce3SDavid Greenman vm_page_test_dirty(p); 32190ecac61SMatthew Dillon if ((p->dirty & p->valid) == 0 || 32290ecac61SMatthew Dillon p->queue != PQ_INACTIVE || 32390ecac61SMatthew Dillon p->wire_count != 0 || 32490ecac61SMatthew Dillon p->hold_count != 0) { 32524a1cce3SDavid Greenman break; 32624a1cce3SDavid Greenman } 32790ecac61SMatthew Dillon mc[page_base + pageout_count] = p; 32890ecac61SMatthew Dillon ++pageout_count; 32990ecac61SMatthew Dillon ++is; 33024a1cce3SDavid Greenman } 33190ecac61SMatthew Dillon 33290ecac61SMatthew Dillon /* 33390ecac61SMatthew Dillon * If we exhausted our forward scan, continue with the reverse scan 33490ecac61SMatthew Dillon * when possible, even past a page boundry. This catches boundry 33590ecac61SMatthew Dillon * conditions. 33690ecac61SMatthew Dillon */ 33790ecac61SMatthew Dillon if (ib && pageout_count < vm_pageout_page_count) 33890ecac61SMatthew Dillon goto more; 339f6b04d2bSDavid Greenman 34067bf6868SJohn Dyson /* 34167bf6868SJohn Dyson * we allow reads during pageouts... 34267bf6868SJohn Dyson */ 3438f9110f6SJohn Dyson return vm_pageout_flush(&mc[page_base], pageout_count, 0); 344aef922f5SJohn Dyson } 345aef922f5SJohn Dyson 3461c7c3c6aSMatthew Dillon /* 3471c7c3c6aSMatthew Dillon * vm_pageout_flush() - launder the given pages 3481c7c3c6aSMatthew Dillon * 3491c7c3c6aSMatthew Dillon * The given pages are laundered. Note that we setup for the start of 3501c7c3c6aSMatthew Dillon * I/O ( i.e. busy the page ), mark it read-only, and bump the object 3511c7c3c6aSMatthew Dillon * reference count all in here rather then in the parent. If we want 3521c7c3c6aSMatthew Dillon * the parent to do more sophisticated things we may have to change 3531c7c3c6aSMatthew Dillon * the ordering. 3541c7c3c6aSMatthew Dillon */ 3551c7c3c6aSMatthew Dillon 356aef922f5SJohn Dyson int 3578f9110f6SJohn Dyson vm_pageout_flush(mc, count, flags) 358aef922f5SJohn Dyson vm_page_t *mc; 359aef922f5SJohn Dyson int count; 3608f9110f6SJohn Dyson int flags; 361aef922f5SJohn Dyson { 362aef922f5SJohn Dyson register vm_object_t object; 363aef922f5SJohn Dyson int pageout_status[count]; 36495461b45SJohn Dyson int numpagedout = 0; 365aef922f5SJohn Dyson int i; 366aef922f5SJohn Dyson 3671c7c3c6aSMatthew Dillon /* 3681c7c3c6aSMatthew Dillon * Initiate I/O. Bump the vm_page_t->busy counter and 3691c7c3c6aSMatthew Dillon * mark the pages read-only. 3701c7c3c6aSMatthew Dillon * 3711c7c3c6aSMatthew Dillon * We do not have to fixup the clean/dirty bits here... we can 3721c7c3c6aSMatthew Dillon * allow the pager to do it after the I/O completes. 37302fa91d3SMatthew Dillon * 37402fa91d3SMatthew Dillon * NOTE! mc[i]->dirty may be partial or fragmented due to an 37502fa91d3SMatthew Dillon * edge case with file fragments. 3761c7c3c6aSMatthew Dillon */ 3771c7c3c6aSMatthew Dillon 3788f9110f6SJohn Dyson for (i = 0; i < count; i++) { 37902fa91d3SMatthew Dillon KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL, ("vm_pageout_flush page %p index %d/%d: partially invalid page", mc[i], i, count)); 380e69763a3SDoug Rabson vm_page_io_start(mc[i]); 3818f9110f6SJohn Dyson vm_page_protect(mc[i], VM_PROT_READ); 3828f9110f6SJohn Dyson } 3838f9110f6SJohn Dyson 384aef922f5SJohn Dyson object = mc[0]->object; 385d474eaaaSDoug Rabson vm_object_pip_add(object, count); 386aef922f5SJohn Dyson 387aef922f5SJohn Dyson vm_pager_put_pages(object, mc, count, 3888f9110f6SJohn Dyson (flags | ((object == kernel_object) ? OBJPC_SYNC : 0)), 38926f9a767SRodney W. Grimes pageout_status); 39026f9a767SRodney W. Grimes 391aef922f5SJohn Dyson for (i = 0; i < count; i++) { 392aef922f5SJohn Dyson vm_page_t mt = mc[i]; 39324a1cce3SDavid Greenman 39426f9a767SRodney W. Grimes switch (pageout_status[i]) { 39526f9a767SRodney W. Grimes case VM_PAGER_OK: 39695461b45SJohn Dyson numpagedout++; 39726f9a767SRodney W. Grimes break; 39826f9a767SRodney W. Grimes case VM_PAGER_PEND: 39995461b45SJohn Dyson numpagedout++; 40026f9a767SRodney W. Grimes break; 40126f9a767SRodney W. Grimes case VM_PAGER_BAD: 40226f9a767SRodney W. Grimes /* 4030d94caffSDavid Greenman * Page outside of range of object. Right now we 4040d94caffSDavid Greenman * essentially lose the changes by pretending it 4050d94caffSDavid Greenman * worked. 40626f9a767SRodney W. Grimes */ 4070385347cSPeter Wemm pmap_clear_modify(mt); 40890ecac61SMatthew Dillon vm_page_undirty(mt); 40926f9a767SRodney W. Grimes break; 41026f9a767SRodney W. Grimes case VM_PAGER_ERROR: 41126f9a767SRodney W. Grimes case VM_PAGER_FAIL: 41226f9a767SRodney W. Grimes /* 4130d94caffSDavid Greenman * If page couldn't be paged out, then reactivate the 4140d94caffSDavid Greenman * page so it doesn't clog the inactive list. (We 4150d94caffSDavid Greenman * will try paging out it again later). 41626f9a767SRodney W. Grimes */ 41724a1cce3SDavid Greenman vm_page_activate(mt); 41826f9a767SRodney W. Grimes break; 41926f9a767SRodney W. Grimes case VM_PAGER_AGAIN: 42026f9a767SRodney W. Grimes break; 42126f9a767SRodney W. Grimes } 42226f9a767SRodney W. Grimes 42326f9a767SRodney W. Grimes /* 4240d94caffSDavid Greenman * If the operation is still going, leave the page busy to 4250d94caffSDavid Greenman * block all other accesses. Also, leave the paging in 4260d94caffSDavid Greenman * progress indicator set so that we don't attempt an object 4270d94caffSDavid Greenman * collapse. 42826f9a767SRodney W. Grimes */ 42926f9a767SRodney W. Grimes if (pageout_status[i] != VM_PAGER_PEND) { 430f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 431e69763a3SDoug Rabson vm_page_io_finish(mt); 432936524aaSMatthew Dillon if (!vm_page_count_severe() || !vm_page_try_to_cache(mt)) 433936524aaSMatthew Dillon vm_page_protect(mt, VM_PROT_READ); 43426f9a767SRodney W. Grimes } 43526f9a767SRodney W. Grimes } 43695461b45SJohn Dyson return numpagedout; 43726f9a767SRodney W. Grimes } 43826f9a767SRodney W. Grimes 43938efa82bSJohn Dyson #if !defined(NO_SWAPPING) 44026f9a767SRodney W. Grimes /* 44126f9a767SRodney W. Grimes * vm_pageout_object_deactivate_pages 44226f9a767SRodney W. Grimes * 44326f9a767SRodney W. Grimes * deactivate enough pages to satisfy the inactive target 44426f9a767SRodney W. Grimes * requirements or if vm_page_proc_limit is set, then 44526f9a767SRodney W. Grimes * deactivate all of the pages in the object and its 44624a1cce3SDavid Greenman * backing_objects. 44726f9a767SRodney W. Grimes * 44826f9a767SRodney W. Grimes * The object and map must be locked. 44926f9a767SRodney W. Grimes */ 45038efa82bSJohn Dyson static void 45138efa82bSJohn Dyson vm_pageout_object_deactivate_pages(map, object, desired, map_remove_only) 45226f9a767SRodney W. Grimes vm_map_t map; 45326f9a767SRodney W. Grimes vm_object_t object; 45438efa82bSJohn Dyson vm_pindex_t desired; 4550d94caffSDavid Greenman int map_remove_only; 45626f9a767SRodney W. Grimes { 45726f9a767SRodney W. Grimes register vm_page_t p, next; 45826f9a767SRodney W. Grimes int rcount; 45938efa82bSJohn Dyson int remove_mode; 4601eeaa1e3SJohn Dyson int s; 46126f9a767SRodney W. Grimes 46224964514SPeter Wemm if (object->type == OBJT_DEVICE || object->type == OBJT_PHYS) 46338efa82bSJohn Dyson return; 4648f895206SDavid Greenman 46538efa82bSJohn Dyson while (object) { 466b1028ad1SLuoqi Chen if (pmap_resident_count(vm_map_pmap(map)) <= desired) 46738efa82bSJohn Dyson return; 46824a1cce3SDavid Greenman if (object->paging_in_progress) 46938efa82bSJohn Dyson return; 47026f9a767SRodney W. Grimes 47138efa82bSJohn Dyson remove_mode = map_remove_only; 47238efa82bSJohn Dyson if (object->shadow_count > 1) 47338efa82bSJohn Dyson remove_mode = 1; 47426f9a767SRodney W. Grimes /* 47526f9a767SRodney W. Grimes * scan the objects entire memory queue 47626f9a767SRodney W. Grimes */ 47726f9a767SRodney W. Grimes rcount = object->resident_page_count; 478b18bfc3dSJohn Dyson p = TAILQ_FIRST(&object->memq); 47926f9a767SRodney W. Grimes while (p && (rcount-- > 0)) { 4807e006499SJohn Dyson int actcount; 481b1028ad1SLuoqi Chen if (pmap_resident_count(vm_map_pmap(map)) <= desired) 48238efa82bSJohn Dyson return; 483b18bfc3dSJohn Dyson next = TAILQ_NEXT(p, listq); 484a58d1fa1SDavid Greenman cnt.v_pdpages++; 4850d94caffSDavid Greenman if (p->wire_count != 0 || 4860d94caffSDavid Greenman p->hold_count != 0 || 4870d94caffSDavid Greenman p->busy != 0 || 4888b03c8edSMatthew Dillon (p->flags & (PG_BUSY|PG_UNMANAGED)) || 4890385347cSPeter Wemm !pmap_page_exists(vm_map_pmap(map), p)) { 4900d94caffSDavid Greenman p = next; 4910d94caffSDavid Greenman continue; 4920d94caffSDavid Greenman } 493ef743ce6SJohn Dyson 4940385347cSPeter Wemm actcount = pmap_ts_referenced(p); 4957e006499SJohn Dyson if (actcount) { 496e69763a3SDoug Rabson vm_page_flag_set(p, PG_REFERENCED); 497c8c4b40cSJohn Dyson } else if (p->flags & PG_REFERENCED) { 4987e006499SJohn Dyson actcount = 1; 499ef743ce6SJohn Dyson } 500ef743ce6SJohn Dyson 50138efa82bSJohn Dyson if ((p->queue != PQ_ACTIVE) && 50238efa82bSJohn Dyson (p->flags & PG_REFERENCED)) { 503ef743ce6SJohn Dyson vm_page_activate(p); 5047e006499SJohn Dyson p->act_count += actcount; 505e69763a3SDoug Rabson vm_page_flag_clear(p, PG_REFERENCED); 506c8c4b40cSJohn Dyson } else if (p->queue == PQ_ACTIVE) { 507ef743ce6SJohn Dyson if ((p->flags & PG_REFERENCED) == 0) { 508c8c4b40cSJohn Dyson p->act_count -= min(p->act_count, ACT_DECLINE); 5092b6b0df7SMatthew Dillon if (!remove_mode && (vm_pageout_algorithm || (p->act_count == 0))) { 510b18bfc3dSJohn Dyson vm_page_protect(p, VM_PROT_NONE); 51126f9a767SRodney W. Grimes vm_page_deactivate(p); 51226f9a767SRodney W. Grimes } else { 513c8c4b40cSJohn Dyson s = splvm(); 514be72f788SAlan Cox TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, p, pageq); 515be72f788SAlan Cox TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, p, pageq); 516c8c4b40cSJohn Dyson splx(s); 517c8c4b40cSJohn Dyson } 518c8c4b40cSJohn Dyson } else { 519eaf13dd7SJohn Dyson vm_page_activate(p); 520e69763a3SDoug Rabson vm_page_flag_clear(p, PG_REFERENCED); 52138efa82bSJohn Dyson if (p->act_count < (ACT_MAX - ACT_ADVANCE)) 52238efa82bSJohn Dyson p->act_count += ACT_ADVANCE; 5231eeaa1e3SJohn Dyson s = splvm(); 524be72f788SAlan Cox TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, p, pageq); 525be72f788SAlan Cox TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, p, pageq); 5261eeaa1e3SJohn Dyson splx(s); 52726f9a767SRodney W. Grimes } 528bd7e5f99SJohn Dyson } else if (p->queue == PQ_INACTIVE) { 529f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 53026f9a767SRodney W. Grimes } 53126f9a767SRodney W. Grimes p = next; 53226f9a767SRodney W. Grimes } 53338efa82bSJohn Dyson object = object->backing_object; 53438efa82bSJohn Dyson } 53538efa82bSJohn Dyson return; 53626f9a767SRodney W. Grimes } 53726f9a767SRodney W. Grimes 53826f9a767SRodney W. Grimes /* 53926f9a767SRodney W. Grimes * deactivate some number of pages in a map, try to do it fairly, but 54026f9a767SRodney W. Grimes * that is really hard to do. 54126f9a767SRodney W. Grimes */ 542cd41fc12SDavid Greenman static void 54338efa82bSJohn Dyson vm_pageout_map_deactivate_pages(map, desired) 54426f9a767SRodney W. Grimes vm_map_t map; 54538efa82bSJohn Dyson vm_pindex_t desired; 54626f9a767SRodney W. Grimes { 54726f9a767SRodney W. Grimes vm_map_entry_t tmpe; 54838efa82bSJohn Dyson vm_object_t obj, bigobj; 5490d94caffSDavid Greenman 550996c772fSJohn Dyson if (lockmgr(&map->lock, LK_EXCLUSIVE | LK_NOWAIT, (void *)0, curproc)) { 55126f9a767SRodney W. Grimes return; 55226f9a767SRodney W. Grimes } 55338efa82bSJohn Dyson 55438efa82bSJohn Dyson bigobj = NULL; 55538efa82bSJohn Dyson 55638efa82bSJohn Dyson /* 55738efa82bSJohn Dyson * first, search out the biggest object, and try to free pages from 55838efa82bSJohn Dyson * that. 55938efa82bSJohn Dyson */ 56026f9a767SRodney W. Grimes tmpe = map->header.next; 56138efa82bSJohn Dyson while (tmpe != &map->header) { 5629fdfe602SMatthew Dillon if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 56338efa82bSJohn Dyson obj = tmpe->object.vm_object; 56438efa82bSJohn Dyson if ((obj != NULL) && (obj->shadow_count <= 1) && 56538efa82bSJohn Dyson ((bigobj == NULL) || 56638efa82bSJohn Dyson (bigobj->resident_page_count < obj->resident_page_count))) { 56738efa82bSJohn Dyson bigobj = obj; 56838efa82bSJohn Dyson } 56938efa82bSJohn Dyson } 57038efa82bSJohn Dyson tmpe = tmpe->next; 57138efa82bSJohn Dyson } 57238efa82bSJohn Dyson 57338efa82bSJohn Dyson if (bigobj) 57438efa82bSJohn Dyson vm_pageout_object_deactivate_pages(map, bigobj, desired, 0); 57538efa82bSJohn Dyson 57638efa82bSJohn Dyson /* 57738efa82bSJohn Dyson * Next, hunt around for other pages to deactivate. We actually 57838efa82bSJohn Dyson * do this search sort of wrong -- .text first is not the best idea. 57938efa82bSJohn Dyson */ 58038efa82bSJohn Dyson tmpe = map->header.next; 58138efa82bSJohn Dyson while (tmpe != &map->header) { 582b1028ad1SLuoqi Chen if (pmap_resident_count(vm_map_pmap(map)) <= desired) 58338efa82bSJohn Dyson break; 5849fdfe602SMatthew Dillon if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 58538efa82bSJohn Dyson obj = tmpe->object.vm_object; 58601155bd7SDavid Greenman if (obj) 58738efa82bSJohn Dyson vm_pageout_object_deactivate_pages(map, obj, desired, 0); 58838efa82bSJohn Dyson } 58926f9a767SRodney W. Grimes tmpe = tmpe->next; 59026f9a767SRodney W. Grimes }; 59138efa82bSJohn Dyson 59238efa82bSJohn Dyson /* 59338efa82bSJohn Dyson * Remove all mappings if a process is swapped out, this will free page 59438efa82bSJohn Dyson * table pages. 59538efa82bSJohn Dyson */ 59638efa82bSJohn Dyson if (desired == 0) 59738efa82bSJohn Dyson pmap_remove(vm_map_pmap(map), 59838efa82bSJohn Dyson VM_MIN_ADDRESS, VM_MAXUSER_ADDRESS); 59938efa82bSJohn Dyson vm_map_unlock(map); 60026f9a767SRodney W. Grimes return; 60126f9a767SRodney W. Grimes } 60238efa82bSJohn Dyson #endif 603df8bae1dSRodney W. Grimes 6041c7c3c6aSMatthew Dillon /* 6051c7c3c6aSMatthew Dillon * Don't try to be fancy - being fancy can lead to VOP_LOCK's and therefore 6061c7c3c6aSMatthew Dillon * to vnode deadlocks. We only do it for OBJT_DEFAULT and OBJT_SWAP objects 6071c7c3c6aSMatthew Dillon * which we know can be trivially freed. 6081c7c3c6aSMatthew Dillon */ 6091c7c3c6aSMatthew Dillon 610925a3a41SJohn Dyson void 611925a3a41SJohn Dyson vm_pageout_page_free(vm_page_t m) { 6121c7c3c6aSMatthew Dillon vm_object_t object = m->object; 6131c7c3c6aSMatthew Dillon int type = object->type; 614925a3a41SJohn Dyson 6151c7c3c6aSMatthew Dillon if (type == OBJT_SWAP || type == OBJT_DEFAULT) 6161c7c3c6aSMatthew Dillon vm_object_reference(object); 617e69763a3SDoug Rabson vm_page_busy(m); 618925a3a41SJohn Dyson vm_page_protect(m, VM_PROT_NONE); 619925a3a41SJohn Dyson vm_page_free(m); 6201c7c3c6aSMatthew Dillon if (type == OBJT_SWAP || type == OBJT_DEFAULT) 62147221757SJohn Dyson vm_object_deallocate(object); 622925a3a41SJohn Dyson } 623925a3a41SJohn Dyson 624df8bae1dSRodney W. Grimes /* 625df8bae1dSRodney W. Grimes * vm_pageout_scan does the dirty work for the pageout daemon. 626df8bae1dSRodney W. Grimes */ 6272b6b0df7SMatthew Dillon static void 6282b6b0df7SMatthew Dillon vm_pageout_scan(int pass) 629df8bae1dSRodney W. Grimes { 630502ba6e4SJohn Dyson vm_page_t m, next; 631936524aaSMatthew Dillon struct vm_page marker; 6322b6b0df7SMatthew Dillon int save_page_shortage; 6332b6b0df7SMatthew Dillon int save_inactive_count; 6341c7c3c6aSMatthew Dillon int page_shortage, maxscan, pcount; 6351c7c3c6aSMatthew Dillon int addl_page_shortage, addl_page_shortage_init; 6365663e6deSDavid Greenman struct proc *p, *bigproc; 6375663e6deSDavid Greenman vm_offset_t size, bigsize; 638df8bae1dSRodney W. Grimes vm_object_t object; 6397e006499SJohn Dyson int actcount; 640f6b04d2bSDavid Greenman int vnodes_skipped = 0; 6412b6b0df7SMatthew Dillon int maxlaunder; 6421eeaa1e3SJohn Dyson int s; 6430d94caffSDavid Greenman 644df8bae1dSRodney W. Grimes /* 6455985940eSJohn Dyson * Do whatever cleanup that the pmap code can. 6465985940eSJohn Dyson */ 6475985940eSJohn Dyson pmap_collect(); 6485985940eSJohn Dyson 6491c7c3c6aSMatthew Dillon addl_page_shortage_init = vm_pageout_deficit; 65095461b45SJohn Dyson vm_pageout_deficit = 0; 651b182ec9eSJohn Dyson 6521c7c3c6aSMatthew Dillon /* 6531c7c3c6aSMatthew Dillon * Calculate the number of pages we want to either free or move 6542b6b0df7SMatthew Dillon * to the cache. 6551c7c3c6aSMatthew Dillon */ 6562b6b0df7SMatthew Dillon page_shortage = vm_paging_target() + addl_page_shortage_init; 6572b6b0df7SMatthew Dillon save_page_shortage = page_shortage; 6582b6b0df7SMatthew Dillon save_inactive_count = cnt.v_inactive_count; 6591c7c3c6aSMatthew Dillon 6601c7c3c6aSMatthew Dillon /* 661936524aaSMatthew Dillon * Initialize our marker 662936524aaSMatthew Dillon */ 663936524aaSMatthew Dillon bzero(&marker, sizeof(marker)); 664936524aaSMatthew Dillon marker.flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER; 665936524aaSMatthew Dillon marker.queue = PQ_INACTIVE; 666936524aaSMatthew Dillon marker.wire_count = 1; 667936524aaSMatthew Dillon 668936524aaSMatthew Dillon /* 6691c7c3c6aSMatthew Dillon * Start scanning the inactive queue for pages we can move to the 6701c7c3c6aSMatthew Dillon * cache or free. The scan will stop when the target is reached or 671936524aaSMatthew Dillon * we have scanned the entire inactive queue. Note that m->act_count 672936524aaSMatthew Dillon * is not used to form decisions for the inactive queue, only for the 673936524aaSMatthew Dillon * active queue. 6742b6b0df7SMatthew Dillon * 6752b6b0df7SMatthew Dillon * maxlaunder limits the number of dirty pages we flush per scan. 6762b6b0df7SMatthew Dillon * For most systems a smaller value (16 or 32) is more robust under 6772b6b0df7SMatthew Dillon * extreme memory and disk pressure because any unnecessary writes 6782b6b0df7SMatthew Dillon * to disk can result in extreme performance degredation. However, 6792b6b0df7SMatthew Dillon * systems with excessive dirty pages (especially when MAP_NOSYNC is 6802b6b0df7SMatthew Dillon * used) will die horribly with limited laundering. If the pageout 6812b6b0df7SMatthew Dillon * daemon cannot clean enough pages in the first pass, we let it go 6822b6b0df7SMatthew Dillon * all out in succeeding passes. 6831c7c3c6aSMatthew Dillon */ 68470111b90SJohn Dyson 6852b6b0df7SMatthew Dillon if ((maxlaunder = vm_max_launder) <= 1) 6862b6b0df7SMatthew Dillon maxlaunder = 1; 6872b6b0df7SMatthew Dillon if (pass) 6882b6b0df7SMatthew Dillon maxlaunder = 10000; 6892b6b0df7SMatthew Dillon 69067bf6868SJohn Dyson rescan0: 6911c7c3c6aSMatthew Dillon addl_page_shortage = addl_page_shortage_init; 692f6b04d2bSDavid Greenman maxscan = cnt.v_inactive_count; 693be72f788SAlan Cox for (m = TAILQ_FIRST(&vm_page_queues[PQ_INACTIVE].pl); 6941c7c3c6aSMatthew Dillon m != NULL && maxscan-- > 0 && page_shortage > 0; 695e929c00dSKirk McKusick m = next) { 696df8bae1dSRodney W. Grimes 697a58d1fa1SDavid Greenman cnt.v_pdpages++; 698b182ec9eSJohn Dyson 699f35329acSJohn Dyson if (m->queue != PQ_INACTIVE) { 70067bf6868SJohn Dyson goto rescan0; 701f35329acSJohn Dyson } 702b182ec9eSJohn Dyson 703b18bfc3dSJohn Dyson next = TAILQ_NEXT(m, pageq); 704df8bae1dSRodney W. Grimes 705936524aaSMatthew Dillon /* 706936524aaSMatthew Dillon * skip marker pages 707936524aaSMatthew Dillon */ 708936524aaSMatthew Dillon if (m->flags & PG_MARKER) 709936524aaSMatthew Dillon continue; 710936524aaSMatthew Dillon 711b182ec9eSJohn Dyson if (m->hold_count) { 712f35329acSJohn Dyson s = splvm(); 713be72f788SAlan Cox TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 714be72f788SAlan Cox TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 715f35329acSJohn Dyson splx(s); 716b182ec9eSJohn Dyson addl_page_shortage++; 717b182ec9eSJohn Dyson continue; 718df8bae1dSRodney W. Grimes } 71926f9a767SRodney W. Grimes /* 720b18bfc3dSJohn Dyson * Dont mess with busy pages, keep in the front of the 721b18bfc3dSJohn Dyson * queue, most likely are being paged out. 72226f9a767SRodney W. Grimes */ 723bd7e5f99SJohn Dyson if (m->busy || (m->flags & PG_BUSY)) { 724b182ec9eSJohn Dyson addl_page_shortage++; 72526f9a767SRodney W. Grimes continue; 72626f9a767SRodney W. Grimes } 727bd7e5f99SJohn Dyson 7287e006499SJohn Dyson /* 7291c7c3c6aSMatthew Dillon * If the object is not being used, we ignore previous 7301c7c3c6aSMatthew Dillon * references. 7317e006499SJohn Dyson */ 7320d94caffSDavid Greenman if (m->object->ref_count == 0) { 733e69763a3SDoug Rabson vm_page_flag_clear(m, PG_REFERENCED); 7340385347cSPeter Wemm pmap_clear_reference(m); 7357e006499SJohn Dyson 7367e006499SJohn Dyson /* 7371c7c3c6aSMatthew Dillon * Otherwise, if the page has been referenced while in the 7381c7c3c6aSMatthew Dillon * inactive queue, we bump the "activation count" upwards, 7391c7c3c6aSMatthew Dillon * making it less likely that the page will be added back to 7401c7c3c6aSMatthew Dillon * the inactive queue prematurely again. Here we check the 7411c7c3c6aSMatthew Dillon * page tables (or emulated bits, if any), given the upper 7421c7c3c6aSMatthew Dillon * level VM system not knowing anything about existing 7431c7c3c6aSMatthew Dillon * references. 7447e006499SJohn Dyson */ 745ef743ce6SJohn Dyson } else if (((m->flags & PG_REFERENCED) == 0) && 7460385347cSPeter Wemm (actcount = pmap_ts_referenced(m))) { 747ef743ce6SJohn Dyson vm_page_activate(m); 7487e006499SJohn Dyson m->act_count += (actcount + ACT_ADVANCE); 749ef743ce6SJohn Dyson continue; 7502fe6e4d7SDavid Greenman } 751ef743ce6SJohn Dyson 7527e006499SJohn Dyson /* 7531c7c3c6aSMatthew Dillon * If the upper level VM system knows about any page 7541c7c3c6aSMatthew Dillon * references, we activate the page. We also set the 7551c7c3c6aSMatthew Dillon * "activation count" higher than normal so that we will less 7561c7c3c6aSMatthew Dillon * likely place pages back onto the inactive queue again. 7577e006499SJohn Dyson */ 758bd7e5f99SJohn Dyson if ((m->flags & PG_REFERENCED) != 0) { 759e69763a3SDoug Rabson vm_page_flag_clear(m, PG_REFERENCED); 7600385347cSPeter Wemm actcount = pmap_ts_referenced(m); 76126f9a767SRodney W. Grimes vm_page_activate(m); 7627e006499SJohn Dyson m->act_count += (actcount + ACT_ADVANCE + 1); 7630d94caffSDavid Greenman continue; 7640d94caffSDavid Greenman } 76567bf6868SJohn Dyson 7667e006499SJohn Dyson /* 7671c7c3c6aSMatthew Dillon * If the upper level VM system doesn't know anything about 7681c7c3c6aSMatthew Dillon * the page being dirty, we have to check for it again. As 7691c7c3c6aSMatthew Dillon * far as the VM code knows, any partially dirty pages are 7701c7c3c6aSMatthew Dillon * fully dirty. 7717e006499SJohn Dyson */ 772f6b04d2bSDavid Greenman if (m->dirty == 0) { 773bd7e5f99SJohn Dyson vm_page_test_dirty(m); 774427e99a0SAlexander Langer } else { 7757dbf82dcSMatthew Dillon vm_page_dirty(m); 77630dcfc09SJohn Dyson } 777ef743ce6SJohn Dyson 7787e006499SJohn Dyson /* 7797e006499SJohn Dyson * Invalid pages can be easily freed 7807e006499SJohn Dyson */ 7816d40c3d3SDavid Greenman if (m->valid == 0) { 782925a3a41SJohn Dyson vm_pageout_page_free(m); 78367bf6868SJohn Dyson cnt.v_dfree++; 7841c7c3c6aSMatthew Dillon --page_shortage; 7857e006499SJohn Dyson 7867e006499SJohn Dyson /* 787936524aaSMatthew Dillon * Clean pages can be placed onto the cache queue. This 788936524aaSMatthew Dillon * effectively frees them. 7897e006499SJohn Dyson */ 790bd7e5f99SJohn Dyson } else if (m->dirty == 0) { 791bd7e5f99SJohn Dyson vm_page_cache(m); 7921c7c3c6aSMatthew Dillon --page_shortage; 7932b6b0df7SMatthew Dillon } else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) { 7947e006499SJohn Dyson /* 7952b6b0df7SMatthew Dillon * Dirty pages need to be paged out, but flushing 7962b6b0df7SMatthew Dillon * a page is extremely expensive verses freeing 7972b6b0df7SMatthew Dillon * a clean page. Rather then artificially limiting 7982b6b0df7SMatthew Dillon * the number of pages we can flush, we instead give 7992b6b0df7SMatthew Dillon * dirty pages extra priority on the inactive queue 8002b6b0df7SMatthew Dillon * by forcing them to be cycled through the queue 8012b6b0df7SMatthew Dillon * twice before being flushed, after which the 8022b6b0df7SMatthew Dillon * (now clean) page will cycle through once more 8032b6b0df7SMatthew Dillon * before being freed. This significantly extends 8042b6b0df7SMatthew Dillon * the thrash point for a heavily loaded machine. 8057e006499SJohn Dyson */ 8062b6b0df7SMatthew Dillon s = splvm(); 8072b6b0df7SMatthew Dillon vm_page_flag_set(m, PG_WINATCFLS); 8082b6b0df7SMatthew Dillon TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 8092b6b0df7SMatthew Dillon TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 8102b6b0df7SMatthew Dillon splx(s); 8110d94caffSDavid Greenman } else if (maxlaunder > 0) { 8122b6b0df7SMatthew Dillon /* 8132b6b0df7SMatthew Dillon * We always want to try to flush some dirty pages if 8142b6b0df7SMatthew Dillon * we encounter them, to keep the system stable. 8152b6b0df7SMatthew Dillon * Normally this number is small, but under extreme 8162b6b0df7SMatthew Dillon * pressure where there are insufficient clean pages 8172b6b0df7SMatthew Dillon * on the inactive queue, we may have to go all out. 8182b6b0df7SMatthew Dillon */ 81912ac6a1dSJohn Dyson int swap_pageouts_ok; 820f6b04d2bSDavid Greenman struct vnode *vp = NULL; 821f2a2857bSKirk McKusick struct mount *mp; 8220d94caffSDavid Greenman 8230d94caffSDavid Greenman object = m->object; 8247e006499SJohn Dyson 82512ac6a1dSJohn Dyson if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) { 82612ac6a1dSJohn Dyson swap_pageouts_ok = 1; 82712ac6a1dSJohn Dyson } else { 82812ac6a1dSJohn Dyson swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts); 82912ac6a1dSJohn Dyson swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts && 83090ecac61SMatthew Dillon vm_page_count_min()); 83112ac6a1dSJohn Dyson 83212ac6a1dSJohn Dyson } 83370111b90SJohn Dyson 83470111b90SJohn Dyson /* 8351c7c3c6aSMatthew Dillon * We don't bother paging objects that are "dead". 8361c7c3c6aSMatthew Dillon * Those objects are in a "rundown" state. 83770111b90SJohn Dyson */ 83870111b90SJohn Dyson if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) { 83912ac6a1dSJohn Dyson s = splvm(); 840be72f788SAlan Cox TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 841be72f788SAlan Cox TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 84212ac6a1dSJohn Dyson splx(s); 84312ac6a1dSJohn Dyson continue; 84412ac6a1dSJohn Dyson } 84512ac6a1dSJohn Dyson 8461c7c3c6aSMatthew Dillon /* 8472b6b0df7SMatthew Dillon * The object is already known NOT to be dead. It 8482b6b0df7SMatthew Dillon * is possible for the vget() to block the whole 8492b6b0df7SMatthew Dillon * pageout daemon, but the new low-memory handling 8502b6b0df7SMatthew Dillon * code should prevent it. 8511c7c3c6aSMatthew Dillon * 8522b6b0df7SMatthew Dillon * The previous code skipped locked vnodes and, worse, 8532b6b0df7SMatthew Dillon * reordered pages in the queue. This results in 8542b6b0df7SMatthew Dillon * completely non-deterministic operation and, on a 8552b6b0df7SMatthew Dillon * busy system, can lead to extremely non-optimal 8562b6b0df7SMatthew Dillon * pageouts. For example, it can cause clean pages 8572b6b0df7SMatthew Dillon * to be freed and dirty pages to be moved to the end 8582b6b0df7SMatthew Dillon * of the queue. Since dirty pages are also moved to 8592b6b0df7SMatthew Dillon * the end of the queue once-cleaned, this gives 8602b6b0df7SMatthew Dillon * way too large a weighting to defering the freeing 8612b6b0df7SMatthew Dillon * of dirty pages. 8621c7c3c6aSMatthew Dillon * 8632b6b0df7SMatthew Dillon * XXX we need to be able to apply a timeout to the 8642b6b0df7SMatthew Dillon * vget() lock attempt. 8651c7c3c6aSMatthew Dillon */ 8661c7c3c6aSMatthew Dillon 8671c7c3c6aSMatthew Dillon if (object->type == OBJT_VNODE) { 86824a1cce3SDavid Greenman vp = object->handle; 8691c7c3c6aSMatthew Dillon 870f2a2857bSKirk McKusick mp = NULL; 871f2a2857bSKirk McKusick if (vp->v_type == VREG) 872f2a2857bSKirk McKusick vn_start_write(vp, &mp, V_NOWAIT); 8732b6b0df7SMatthew Dillon if (vget(vp, LK_EXCLUSIVE|LK_NOOBJ, curproc)) { 874f2a2857bSKirk McKusick vn_finished_write(mp); 875aef922f5SJohn Dyson if (object->flags & OBJ_MIGHTBEDIRTY) 876925a3a41SJohn Dyson vnodes_skipped++; 877b182ec9eSJohn Dyson continue; 87885a376ebSJohn Dyson } 879b182ec9eSJohn Dyson 880f35329acSJohn Dyson /* 881936524aaSMatthew Dillon * The page might have been moved to another 882936524aaSMatthew Dillon * queue during potential blocking in vget() 883936524aaSMatthew Dillon * above. The page might have been freed and 884936524aaSMatthew Dillon * reused for another vnode. The object might 885936524aaSMatthew Dillon * have been reused for another vnode. 886f35329acSJohn Dyson */ 887936524aaSMatthew Dillon if (m->queue != PQ_INACTIVE || 888936524aaSMatthew Dillon m->object != object || 889936524aaSMatthew Dillon object->handle != vp) { 890b182ec9eSJohn Dyson if (object->flags & OBJ_MIGHTBEDIRTY) 891925a3a41SJohn Dyson vnodes_skipped++; 892b182ec9eSJohn Dyson vput(vp); 893f2a2857bSKirk McKusick vn_finished_write(mp); 894b182ec9eSJohn Dyson continue; 895b182ec9eSJohn Dyson } 896b182ec9eSJohn Dyson 897f35329acSJohn Dyson /* 898936524aaSMatthew Dillon * The page may have been busied during the 899936524aaSMatthew Dillon * blocking in vput(); We don't move the 900936524aaSMatthew Dillon * page back onto the end of the queue so that 901936524aaSMatthew Dillon * statistics are more correct if we don't. 902f35329acSJohn Dyson */ 903b182ec9eSJohn Dyson if (m->busy || (m->flags & PG_BUSY)) { 904b182ec9eSJohn Dyson vput(vp); 905f2a2857bSKirk McKusick vn_finished_write(mp); 906b182ec9eSJohn Dyson continue; 907b182ec9eSJohn Dyson } 908b182ec9eSJohn Dyson 909f35329acSJohn Dyson /* 910f35329acSJohn Dyson * If the page has become held, then skip it 911f35329acSJohn Dyson */ 912b182ec9eSJohn Dyson if (m->hold_count) { 913f35329acSJohn Dyson s = splvm(); 914be72f788SAlan Cox TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 915be72f788SAlan Cox TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 916f35329acSJohn Dyson splx(s); 917b182ec9eSJohn Dyson if (object->flags & OBJ_MIGHTBEDIRTY) 918925a3a41SJohn Dyson vnodes_skipped++; 919b182ec9eSJohn Dyson vput(vp); 920f2a2857bSKirk McKusick vn_finished_write(mp); 921f6b04d2bSDavid Greenman continue; 922f6b04d2bSDavid Greenman } 923f6b04d2bSDavid Greenman } 924f6b04d2bSDavid Greenman 9250d94caffSDavid Greenman /* 9260d94caffSDavid Greenman * If a page is dirty, then it is either being washed 9270d94caffSDavid Greenman * (but not yet cleaned) or it is still in the 9280d94caffSDavid Greenman * laundry. If it is still in the laundry, then we 9292b6b0df7SMatthew Dillon * start the cleaning operation. 930936524aaSMatthew Dillon * 931936524aaSMatthew Dillon * This operation may cluster, invalidating the 'next' 932936524aaSMatthew Dillon * pointer. To prevent an inordinate number of 933936524aaSMatthew Dillon * restarts we use our marker to remember our place. 9342b6b0df7SMatthew Dillon * 9352b6b0df7SMatthew Dillon * decrement page_shortage on success to account for 9362b6b0df7SMatthew Dillon * the (future) cleaned page. Otherwise we could wind 9372b6b0df7SMatthew Dillon * up laundering or cleaning too many pages. 9380d94caffSDavid Greenman */ 939936524aaSMatthew Dillon s = splvm(); 940936524aaSMatthew Dillon TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE].pl, m, &marker, pageq); 941936524aaSMatthew Dillon splx(s); 9422b6b0df7SMatthew Dillon if (vm_pageout_clean(m) != 0) { 9432b6b0df7SMatthew Dillon --page_shortage; 944936524aaSMatthew Dillon --maxlaunder; 9452b6b0df7SMatthew Dillon } 946936524aaSMatthew Dillon s = splvm(); 947936524aaSMatthew Dillon next = TAILQ_NEXT(&marker, pageq); 948936524aaSMatthew Dillon TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, &marker, pageq); 949936524aaSMatthew Dillon splx(s); 950f2a2857bSKirk McKusick if (vp) { 951f6b04d2bSDavid Greenman vput(vp); 952f2a2857bSKirk McKusick vn_finished_write(mp); 953f2a2857bSKirk McKusick } 9540d94caffSDavid Greenman } 955df8bae1dSRodney W. Grimes } 95626f9a767SRodney W. Grimes 957df8bae1dSRodney W. Grimes /* 958936524aaSMatthew Dillon * Compute the number of pages we want to try to move from the 959936524aaSMatthew Dillon * active queue to the inactive queue. 9601c7c3c6aSMatthew Dillon */ 961936524aaSMatthew Dillon page_shortage = vm_paging_target() + 962936524aaSMatthew Dillon cnt.v_inactive_target - cnt.v_inactive_count; 963b182ec9eSJohn Dyson page_shortage += addl_page_shortage; 9641c7c3c6aSMatthew Dillon 9651c7c3c6aSMatthew Dillon /* 966936524aaSMatthew Dillon * Scan the active queue for things we can deactivate. We nominally 967936524aaSMatthew Dillon * track the per-page activity counter and use it to locate 968936524aaSMatthew Dillon * deactivation candidates. 9691c7c3c6aSMatthew Dillon */ 97026f9a767SRodney W. Grimes 971b18bfc3dSJohn Dyson pcount = cnt.v_active_count; 972be72f788SAlan Cox m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl); 9731c7c3c6aSMatthew Dillon 974b18bfc3dSJohn Dyson while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) { 975f35329acSJohn Dyson 9767e006499SJohn Dyson /* 977956f3135SPhilippe Charnier * This is a consistency check, and should likely be a panic 9787e006499SJohn Dyson * or warning. 9797e006499SJohn Dyson */ 980f35329acSJohn Dyson if (m->queue != PQ_ACTIVE) { 98138efa82bSJohn Dyson break; 982f35329acSJohn Dyson } 983f35329acSJohn Dyson 984b18bfc3dSJohn Dyson next = TAILQ_NEXT(m, pageq); 985df8bae1dSRodney W. Grimes /* 98626f9a767SRodney W. Grimes * Don't deactivate pages that are busy. 987df8bae1dSRodney W. Grimes */ 988a647a309SDavid Greenman if ((m->busy != 0) || 9890d94caffSDavid Greenman (m->flags & PG_BUSY) || 990f6b04d2bSDavid Greenman (m->hold_count != 0)) { 991f35329acSJohn Dyson s = splvm(); 992be72f788SAlan Cox TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 993be72f788SAlan Cox TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 994f35329acSJohn Dyson splx(s); 99526f9a767SRodney W. Grimes m = next; 99626f9a767SRodney W. Grimes continue; 997df8bae1dSRodney W. Grimes } 998b18bfc3dSJohn Dyson 999b18bfc3dSJohn Dyson /* 1000b18bfc3dSJohn Dyson * The count for pagedaemon pages is done after checking the 1001956f3135SPhilippe Charnier * page for eligibility... 1002b18bfc3dSJohn Dyson */ 1003b18bfc3dSJohn Dyson cnt.v_pdpages++; 1004ef743ce6SJohn Dyson 10057e006499SJohn Dyson /* 10067e006499SJohn Dyson * Check to see "how much" the page has been used. 10077e006499SJohn Dyson */ 10087e006499SJohn Dyson actcount = 0; 1009ef743ce6SJohn Dyson if (m->object->ref_count != 0) { 1010ef743ce6SJohn Dyson if (m->flags & PG_REFERENCED) { 10117e006499SJohn Dyson actcount += 1; 10120d94caffSDavid Greenman } 10130385347cSPeter Wemm actcount += pmap_ts_referenced(m); 10147e006499SJohn Dyson if (actcount) { 10157e006499SJohn Dyson m->act_count += ACT_ADVANCE + actcount; 101638efa82bSJohn Dyson if (m->act_count > ACT_MAX) 101738efa82bSJohn Dyson m->act_count = ACT_MAX; 101838efa82bSJohn Dyson } 1019b18bfc3dSJohn Dyson } 1020ef743ce6SJohn Dyson 10217e006499SJohn Dyson /* 10227e006499SJohn Dyson * Since we have "tested" this bit, we need to clear it now. 10237e006499SJohn Dyson */ 1024e69763a3SDoug Rabson vm_page_flag_clear(m, PG_REFERENCED); 1025ef743ce6SJohn Dyson 10267e006499SJohn Dyson /* 10277e006499SJohn Dyson * Only if an object is currently being used, do we use the 10287e006499SJohn Dyson * page activation count stats. 10297e006499SJohn Dyson */ 10307e006499SJohn Dyson if (actcount && (m->object->ref_count != 0)) { 1031f35329acSJohn Dyson s = splvm(); 1032be72f788SAlan Cox TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1033be72f788SAlan Cox TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1034f35329acSJohn Dyson splx(s); 103526f9a767SRodney W. Grimes } else { 103638efa82bSJohn Dyson m->act_count -= min(m->act_count, ACT_DECLINE); 10372b6b0df7SMatthew Dillon if (vm_pageout_algorithm || 10382b6b0df7SMatthew Dillon m->object->ref_count == 0 || 10392b6b0df7SMatthew Dillon m->act_count == 0) { 1040925a3a41SJohn Dyson page_shortage--; 1041d4a272dbSJohn Dyson if (m->object->ref_count == 0) { 1042ef743ce6SJohn Dyson vm_page_protect(m, VM_PROT_NONE); 1043d4a272dbSJohn Dyson if (m->dirty == 0) 10440d94caffSDavid Greenman vm_page_cache(m); 1045d4a272dbSJohn Dyson else 1046d4a272dbSJohn Dyson vm_page_deactivate(m); 10470d94caffSDavid Greenman } else { 104826f9a767SRodney W. Grimes vm_page_deactivate(m); 1049df8bae1dSRodney W. Grimes } 105038efa82bSJohn Dyson } else { 105138efa82bSJohn Dyson s = splvm(); 1052be72f788SAlan Cox TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1053be72f788SAlan Cox TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 105438efa82bSJohn Dyson splx(s); 105538efa82bSJohn Dyson } 1056df8bae1dSRodney W. Grimes } 105726f9a767SRodney W. Grimes m = next; 105826f9a767SRodney W. Grimes } 1059df8bae1dSRodney W. Grimes 1060f35329acSJohn Dyson s = splvm(); 10611c7c3c6aSMatthew Dillon 1062df8bae1dSRodney W. Grimes /* 10630d94caffSDavid Greenman * We try to maintain some *really* free pages, this allows interrupt 10641c7c3c6aSMatthew Dillon * code to be guaranteed space. Since both cache and free queues 10651c7c3c6aSMatthew Dillon * are considered basically 'free', moving pages from cache to free 10661c7c3c6aSMatthew Dillon * does not effect other calculations. 1067df8bae1dSRodney W. Grimes */ 10681c7c3c6aSMatthew Dillon 1069a1f6d91cSDavid Greenman while (cnt.v_free_count < cnt.v_free_reserved) { 10705070c7f8SJohn Dyson static int cache_rover = 0; 1071faa273d5SMatthew Dillon m = vm_page_list_find(PQ_CACHE, cache_rover, FALSE); 10720d94caffSDavid Greenman if (!m) 10730d94caffSDavid Greenman break; 10748b03c8edSMatthew Dillon if ((m->flags & (PG_BUSY|PG_UNMANAGED)) || 10758b03c8edSMatthew Dillon m->busy || 10768b03c8edSMatthew Dillon m->hold_count || 10778b03c8edSMatthew Dillon m->wire_count) { 1078d044d7bfSMatthew Dillon #ifdef INVARIANTS 1079d044d7bfSMatthew Dillon printf("Warning: busy page %p found in cache\n", m); 1080d044d7bfSMatthew Dillon #endif 1081aaba53daSMatthew Dillon vm_page_deactivate(m); 1082aaba53daSMatthew Dillon continue; 1083aaba53daSMatthew Dillon } 10845070c7f8SJohn Dyson cache_rover = (cache_rover + PQ_PRIME2) & PQ_L2_MASK; 1085925a3a41SJohn Dyson vm_pageout_page_free(m); 10860bb3a0d2SDavid Greenman cnt.v_dfree++; 108726f9a767SRodney W. Grimes } 1088f35329acSJohn Dyson splx(s); 10895663e6deSDavid Greenman 1090ceb0cf87SJohn Dyson #if !defined(NO_SWAPPING) 1091ceb0cf87SJohn Dyson /* 1092ceb0cf87SJohn Dyson * Idle process swapout -- run once per second. 1093ceb0cf87SJohn Dyson */ 1094ceb0cf87SJohn Dyson if (vm_swap_idle_enabled) { 1095ceb0cf87SJohn Dyson static long lsec; 1096227ee8a1SPoul-Henning Kamp if (time_second != lsec) { 1097ceb0cf87SJohn Dyson vm_pageout_req_swapout |= VM_SWAP_IDLE; 1098ceb0cf87SJohn Dyson vm_req_vmdaemon(); 1099227ee8a1SPoul-Henning Kamp lsec = time_second; 1100ceb0cf87SJohn Dyson } 1101ceb0cf87SJohn Dyson } 1102ceb0cf87SJohn Dyson #endif 1103ceb0cf87SJohn Dyson 11045663e6deSDavid Greenman /* 1105f6b04d2bSDavid Greenman * If we didn't get enough free pages, and we have skipped a vnode 11064c1f8ee9SDavid Greenman * in a writeable object, wakeup the sync daemon. And kick swapout 11074c1f8ee9SDavid Greenman * if we did not get enough free pages. 1108f6b04d2bSDavid Greenman */ 110990ecac61SMatthew Dillon if (vm_paging_target() > 0) { 111090ecac61SMatthew Dillon if (vnodes_skipped && vm_page_count_min()) 1111d50c1994SPeter Wemm (void) speedup_syncer(); 111238efa82bSJohn Dyson #if !defined(NO_SWAPPING) 111390ecac61SMatthew Dillon if (vm_swap_enabled && vm_page_count_target()) { 11144c1f8ee9SDavid Greenman vm_req_vmdaemon(); 1115ceb0cf87SJohn Dyson vm_pageout_req_swapout |= VM_SWAP_NORMAL; 11164c1f8ee9SDavid Greenman } 11175afce282SDavid Greenman #endif 11184c1f8ee9SDavid Greenman } 11194c1f8ee9SDavid Greenman 1120f6b04d2bSDavid Greenman /* 11210d94caffSDavid Greenman * make sure that we have swap space -- if we are low on memory and 11220d94caffSDavid Greenman * swap -- then kill the biggest process. 11235663e6deSDavid Greenman */ 1124936524aaSMatthew Dillon if ((vm_swap_size < 64 || swap_pager_full) && vm_page_count_min()) { 11255663e6deSDavid Greenman bigproc = NULL; 11265663e6deSDavid Greenman bigsize = 0; 11271005a129SJohn Baldwin sx_slock(&allproc_lock); 11288606d880SJohn Baldwin LIST_FOREACH(p, &allproc, p_list) { 11295663e6deSDavid Greenman /* 11305663e6deSDavid Greenman * if this is a system process, skip it 11315663e6deSDavid Greenman */ 11328606d880SJohn Baldwin PROC_LOCK(p); 1133c8da68e9SPeter Wemm if ((p->p_flag & P_SYSTEM) || (p->p_lock > 0) || 1134c8da68e9SPeter Wemm (p->p_pid == 1) || 113579221631SDavid Greenman ((p->p_pid < 48) && (vm_swap_size != 0))) { 11368606d880SJohn Baldwin PROC_UNLOCK(p); 11375663e6deSDavid Greenman continue; 11385663e6deSDavid Greenman } 11398606d880SJohn Baldwin PROC_UNLOCK(p); 11405663e6deSDavid Greenman /* 11415663e6deSDavid Greenman * if the process is in a non-running type state, 11425663e6deSDavid Greenman * don't touch it. 11435663e6deSDavid Greenman */ 11449ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 11455663e6deSDavid Greenman if (p->p_stat != SRUN && p->p_stat != SSLEEP) { 11469ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 11475663e6deSDavid Greenman continue; 11485663e6deSDavid Greenman } 11499ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 11505663e6deSDavid Greenman /* 11515663e6deSDavid Greenman * get the process size 11525663e6deSDavid Greenman */ 1153b1028ad1SLuoqi Chen size = vmspace_resident_count(p->p_vmspace); 11545663e6deSDavid Greenman /* 11555663e6deSDavid Greenman * if the this process is bigger than the biggest one 11565663e6deSDavid Greenman * remember it. 11575663e6deSDavid Greenman */ 11585663e6deSDavid Greenman if (size > bigsize) { 11595663e6deSDavid Greenman bigproc = p; 11605663e6deSDavid Greenman bigsize = size; 11615663e6deSDavid Greenman } 11625663e6deSDavid Greenman } 11631005a129SJohn Baldwin sx_sunlock(&allproc_lock); 11645663e6deSDavid Greenman if (bigproc != NULL) { 1165729b1e51SDavid Greenman killproc(bigproc, "out of swap space"); 11669ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 11675663e6deSDavid Greenman bigproc->p_estcpu = 0; 11685663e6deSDavid Greenman bigproc->p_nice = PRIO_MIN; 11695663e6deSDavid Greenman resetpriority(bigproc); 11709ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 117124a1cce3SDavid Greenman wakeup(&cnt.v_free_count); 11725663e6deSDavid Greenman } 11735663e6deSDavid Greenman } 117426f9a767SRodney W. Grimes } 117526f9a767SRodney W. Grimes 1176dc2efb27SJohn Dyson /* 1177dc2efb27SJohn Dyson * This routine tries to maintain the pseudo LRU active queue, 1178dc2efb27SJohn Dyson * so that during long periods of time where there is no paging, 1179956f3135SPhilippe Charnier * that some statistic accumulation still occurs. This code 1180dc2efb27SJohn Dyson * helps the situation where paging just starts to occur. 1181dc2efb27SJohn Dyson */ 1182dc2efb27SJohn Dyson static void 1183dc2efb27SJohn Dyson vm_pageout_page_stats() 1184dc2efb27SJohn Dyson { 1185dc2efb27SJohn Dyson int s; 1186dc2efb27SJohn Dyson vm_page_t m,next; 1187dc2efb27SJohn Dyson int pcount,tpcount; /* Number of pages to check */ 1188dc2efb27SJohn Dyson static int fullintervalcount = 0; 1189bef608bdSJohn Dyson int page_shortage; 119025db2c54SMatthew Dillon int s0; 1191bef608bdSJohn Dyson 119290ecac61SMatthew Dillon page_shortage = 119390ecac61SMatthew Dillon (cnt.v_inactive_target + cnt.v_cache_max + cnt.v_free_min) - 1194bef608bdSJohn Dyson (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count); 119590ecac61SMatthew Dillon 1196bef608bdSJohn Dyson if (page_shortage <= 0) 1197bef608bdSJohn Dyson return; 1198dc2efb27SJohn Dyson 119925db2c54SMatthew Dillon s0 = splvm(); 120025db2c54SMatthew Dillon 1201dc2efb27SJohn Dyson pcount = cnt.v_active_count; 1202dc2efb27SJohn Dyson fullintervalcount += vm_pageout_stats_interval; 1203dc2efb27SJohn Dyson if (fullintervalcount < vm_pageout_full_stats_interval) { 1204dc2efb27SJohn Dyson tpcount = (vm_pageout_stats_max * cnt.v_active_count) / cnt.v_page_count; 1205dc2efb27SJohn Dyson if (pcount > tpcount) 1206dc2efb27SJohn Dyson pcount = tpcount; 1207883f3caaSMatthew Dillon } else { 1208883f3caaSMatthew Dillon fullintervalcount = 0; 1209dc2efb27SJohn Dyson } 1210dc2efb27SJohn Dyson 1211be72f788SAlan Cox m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl); 1212dc2efb27SJohn Dyson while ((m != NULL) && (pcount-- > 0)) { 12137e006499SJohn Dyson int actcount; 1214dc2efb27SJohn Dyson 1215dc2efb27SJohn Dyson if (m->queue != PQ_ACTIVE) { 1216dc2efb27SJohn Dyson break; 1217dc2efb27SJohn Dyson } 1218dc2efb27SJohn Dyson 1219dc2efb27SJohn Dyson next = TAILQ_NEXT(m, pageq); 1220dc2efb27SJohn Dyson /* 1221dc2efb27SJohn Dyson * Don't deactivate pages that are busy. 1222dc2efb27SJohn Dyson */ 1223dc2efb27SJohn Dyson if ((m->busy != 0) || 1224dc2efb27SJohn Dyson (m->flags & PG_BUSY) || 1225dc2efb27SJohn Dyson (m->hold_count != 0)) { 1226dc2efb27SJohn Dyson s = splvm(); 1227be72f788SAlan Cox TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1228be72f788SAlan Cox TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1229dc2efb27SJohn Dyson splx(s); 1230dc2efb27SJohn Dyson m = next; 1231dc2efb27SJohn Dyson continue; 1232dc2efb27SJohn Dyson } 1233dc2efb27SJohn Dyson 12347e006499SJohn Dyson actcount = 0; 1235dc2efb27SJohn Dyson if (m->flags & PG_REFERENCED) { 1236e69763a3SDoug Rabson vm_page_flag_clear(m, PG_REFERENCED); 12377e006499SJohn Dyson actcount += 1; 1238dc2efb27SJohn Dyson } 1239dc2efb27SJohn Dyson 12400385347cSPeter Wemm actcount += pmap_ts_referenced(m); 12417e006499SJohn Dyson if (actcount) { 12427e006499SJohn Dyson m->act_count += ACT_ADVANCE + actcount; 1243dc2efb27SJohn Dyson if (m->act_count > ACT_MAX) 1244dc2efb27SJohn Dyson m->act_count = ACT_MAX; 1245dc2efb27SJohn Dyson s = splvm(); 1246be72f788SAlan Cox TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1247be72f788SAlan Cox TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1248dc2efb27SJohn Dyson splx(s); 1249dc2efb27SJohn Dyson } else { 1250dc2efb27SJohn Dyson if (m->act_count == 0) { 12517e006499SJohn Dyson /* 12522b6b0df7SMatthew Dillon * We turn off page access, so that we have 12532b6b0df7SMatthew Dillon * more accurate RSS stats. We don't do this 12542b6b0df7SMatthew Dillon * in the normal page deactivation when the 12552b6b0df7SMatthew Dillon * system is loaded VM wise, because the 12562b6b0df7SMatthew Dillon * cost of the large number of page protect 12572b6b0df7SMatthew Dillon * operations would be higher than the value 12582b6b0df7SMatthew Dillon * of doing the operation. 12597e006499SJohn Dyson */ 1260dc2efb27SJohn Dyson vm_page_protect(m, VM_PROT_NONE); 1261dc2efb27SJohn Dyson vm_page_deactivate(m); 1262dc2efb27SJohn Dyson } else { 1263dc2efb27SJohn Dyson m->act_count -= min(m->act_count, ACT_DECLINE); 1264dc2efb27SJohn Dyson s = splvm(); 1265be72f788SAlan Cox TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1266be72f788SAlan Cox TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1267dc2efb27SJohn Dyson splx(s); 1268dc2efb27SJohn Dyson } 1269dc2efb27SJohn Dyson } 1270dc2efb27SJohn Dyson 1271dc2efb27SJohn Dyson m = next; 1272dc2efb27SJohn Dyson } 127325db2c54SMatthew Dillon splx(s0); 1274dc2efb27SJohn Dyson } 1275dc2efb27SJohn Dyson 1276b182ec9eSJohn Dyson static int 1277b182ec9eSJohn Dyson vm_pageout_free_page_calc(count) 1278b182ec9eSJohn Dyson vm_size_t count; 1279b182ec9eSJohn Dyson { 1280b182ec9eSJohn Dyson if (count < cnt.v_page_count) 1281b182ec9eSJohn Dyson return 0; 1282b182ec9eSJohn Dyson /* 1283b182ec9eSJohn Dyson * free_reserved needs to include enough for the largest swap pager 1284b182ec9eSJohn Dyson * structures plus enough for any pv_entry structs when paging. 1285b182ec9eSJohn Dyson */ 1286b182ec9eSJohn Dyson if (cnt.v_page_count > 1024) 1287b182ec9eSJohn Dyson cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200; 1288b182ec9eSJohn Dyson else 1289b182ec9eSJohn Dyson cnt.v_free_min = 4; 1290f35329acSJohn Dyson cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE + 1291f35329acSJohn Dyson cnt.v_interrupt_free_min; 1292f35329acSJohn Dyson cnt.v_free_reserved = vm_pageout_page_count + 1293a15403deSJohn Dyson cnt.v_pageout_free_min + (count / 768) + PQ_L2_SIZE; 129490ecac61SMatthew Dillon cnt.v_free_severe = cnt.v_free_min / 2; 1295a2f4a846SJohn Dyson cnt.v_free_min += cnt.v_free_reserved; 129690ecac61SMatthew Dillon cnt.v_free_severe += cnt.v_free_reserved; 1297b182ec9eSJohn Dyson return 1; 1298b182ec9eSJohn Dyson } 1299b182ec9eSJohn Dyson 1300b182ec9eSJohn Dyson 1301df8bae1dSRodney W. Grimes /* 1302df8bae1dSRodney W. Grimes * vm_pageout is the high level pageout daemon. 1303df8bae1dSRodney W. Grimes */ 13042b14f991SJulian Elischer static void 130526f9a767SRodney W. Grimes vm_pageout() 1306df8bae1dSRodney W. Grimes { 13072b6b0df7SMatthew Dillon int pass; 13080384fff8SJason Evans 13099ed346baSBosko Milekic mtx_lock(&Giant); 13100384fff8SJason Evans 1311df8bae1dSRodney W. Grimes /* 1312df8bae1dSRodney W. Grimes * Initialize some paging parameters. 1313df8bae1dSRodney W. Grimes */ 1314df8bae1dSRodney W. Grimes 1315f6b04d2bSDavid Greenman cnt.v_interrupt_free_min = 2; 1316f35329acSJohn Dyson if (cnt.v_page_count < 2000) 1317f35329acSJohn Dyson vm_pageout_page_count = 8; 1318f6b04d2bSDavid Greenman 1319b182ec9eSJohn Dyson vm_pageout_free_page_calc(cnt.v_page_count); 1320ed74321bSDavid Greenman /* 13212b6b0df7SMatthew Dillon * v_free_target and v_cache_min control pageout hysteresis. Note 13222b6b0df7SMatthew Dillon * that these are more a measure of the VM cache queue hysteresis 13232b6b0df7SMatthew Dillon * then the VM free queue. Specifically, v_free_target is the 13242b6b0df7SMatthew Dillon * high water mark (free+cache pages). 13252b6b0df7SMatthew Dillon * 13262b6b0df7SMatthew Dillon * v_free_reserved + v_cache_min (mostly means v_cache_min) is the 13272b6b0df7SMatthew Dillon * low water mark, while v_free_min is the stop. v_cache_min must 13282b6b0df7SMatthew Dillon * be big enough to handle memory needs while the pageout daemon 13292b6b0df7SMatthew Dillon * is signalled and run to free more pages. 1330ed74321bSDavid Greenman */ 1331a15403deSJohn Dyson if (cnt.v_free_count > 6144) 13322b6b0df7SMatthew Dillon cnt.v_free_target = 4 * cnt.v_free_min + cnt.v_free_reserved; 1333a15403deSJohn Dyson else 1334a15403deSJohn Dyson cnt.v_free_target = 2 * cnt.v_free_min + cnt.v_free_reserved; 13356f2b142eSDavid Greenman 1336a15403deSJohn Dyson if (cnt.v_free_count > 2048) { 1337a15403deSJohn Dyson cnt.v_cache_min = cnt.v_free_target; 1338a15403deSJohn Dyson cnt.v_cache_max = 2 * cnt.v_cache_min; 1339a15403deSJohn Dyson cnt.v_inactive_target = (3 * cnt.v_free_target) / 2; 13400d94caffSDavid Greenman } else { 13410d94caffSDavid Greenman cnt.v_cache_min = 0; 13420d94caffSDavid Greenman cnt.v_cache_max = 0; 13436f2b142eSDavid Greenman cnt.v_inactive_target = cnt.v_free_count / 4; 13440d94caffSDavid Greenman } 1345e47ed70bSJohn Dyson if (cnt.v_inactive_target > cnt.v_free_count / 3) 1346e47ed70bSJohn Dyson cnt.v_inactive_target = cnt.v_free_count / 3; 1347df8bae1dSRodney W. Grimes 1348df8bae1dSRodney W. Grimes /* XXX does not really belong here */ 1349df8bae1dSRodney W. Grimes if (vm_page_max_wired == 0) 1350df8bae1dSRodney W. Grimes vm_page_max_wired = cnt.v_free_count / 3; 1351df8bae1dSRodney W. Grimes 1352dc2efb27SJohn Dyson if (vm_pageout_stats_max == 0) 1353dc2efb27SJohn Dyson vm_pageout_stats_max = cnt.v_free_target; 1354dc2efb27SJohn Dyson 1355dc2efb27SJohn Dyson /* 1356dc2efb27SJohn Dyson * Set interval in seconds for stats scan. 1357dc2efb27SJohn Dyson */ 1358dc2efb27SJohn Dyson if (vm_pageout_stats_interval == 0) 1359bef608bdSJohn Dyson vm_pageout_stats_interval = 5; 1360dc2efb27SJohn Dyson if (vm_pageout_full_stats_interval == 0) 1361dc2efb27SJohn Dyson vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4; 1362dc2efb27SJohn Dyson 1363dc2efb27SJohn Dyson 1364dc2efb27SJohn Dyson /* 1365dc2efb27SJohn Dyson * Set maximum free per pass 1366dc2efb27SJohn Dyson */ 1367dc2efb27SJohn Dyson if (vm_pageout_stats_free_max == 0) 1368bef608bdSJohn Dyson vm_pageout_stats_free_max = 5; 1369dc2efb27SJohn Dyson 1370e929c00dSKirk McKusick curproc->p_flag |= P_BUFEXHAUST; 137124a1cce3SDavid Greenman swap_pager_swap_init(); 13722b6b0df7SMatthew Dillon pass = 0; 1373df8bae1dSRodney W. Grimes /* 13740d94caffSDavid Greenman * The pageout daemon is never done, so loop forever. 1375df8bae1dSRodney W. Grimes */ 1376df8bae1dSRodney W. Grimes while (TRUE) { 1377dc2efb27SJohn Dyson int error; 1378b18bfc3dSJohn Dyson int s = splvm(); 137990ecac61SMatthew Dillon 1380936524aaSMatthew Dillon /* 1381936524aaSMatthew Dillon * If we have enough free memory, wakeup waiters. Do 1382936524aaSMatthew Dillon * not clear vm_pages_needed until we reach our target, 1383936524aaSMatthew Dillon * otherwise we may be woken up over and over again and 1384936524aaSMatthew Dillon * waste a lot of cpu. 1385936524aaSMatthew Dillon */ 1386936524aaSMatthew Dillon if (vm_pages_needed && !vm_page_count_min()) { 1387936524aaSMatthew Dillon if (vm_paging_needed() <= 0) 1388936524aaSMatthew Dillon vm_pages_needed = 0; 1389936524aaSMatthew Dillon wakeup(&cnt.v_free_count); 1390936524aaSMatthew Dillon } 1391936524aaSMatthew Dillon if (vm_pages_needed) { 139290ecac61SMatthew Dillon /* 13932b6b0df7SMatthew Dillon * Still not done, take a second pass without waiting 13942b6b0df7SMatthew Dillon * (unlimited dirty cleaning), otherwise sleep a bit 13952b6b0df7SMatthew Dillon * and try again. 139690ecac61SMatthew Dillon */ 13972b6b0df7SMatthew Dillon ++pass; 13982b6b0df7SMatthew Dillon if (pass > 1) 139990ecac61SMatthew Dillon tsleep(&vm_pages_needed, PVM, "psleep", hz/2); 140090ecac61SMatthew Dillon } else { 140190ecac61SMatthew Dillon /* 14022b6b0df7SMatthew Dillon * Good enough, sleep & handle stats. Prime the pass 14032b6b0df7SMatthew Dillon * for the next run. 140490ecac61SMatthew Dillon */ 14052b6b0df7SMatthew Dillon if (pass > 1) 14062b6b0df7SMatthew Dillon pass = 1; 14072b6b0df7SMatthew Dillon else 14082b6b0df7SMatthew Dillon pass = 0; 1409dc2efb27SJohn Dyson error = tsleep(&vm_pages_needed, 1410dc2efb27SJohn Dyson PVM, "psleep", vm_pageout_stats_interval * hz); 1411dc2efb27SJohn Dyson if (error && !vm_pages_needed) { 1412dc2efb27SJohn Dyson splx(s); 14132b6b0df7SMatthew Dillon pass = 0; 1414dc2efb27SJohn Dyson vm_pageout_page_stats(); 1415dc2efb27SJohn Dyson continue; 1416dc2efb27SJohn Dyson } 1417f919ebdeSDavid Greenman } 1418e47ed70bSJohn Dyson 1419b18bfc3dSJohn Dyson if (vm_pages_needed) 1420b18bfc3dSJohn Dyson cnt.v_pdwakeups++; 1421f919ebdeSDavid Greenman splx(s); 14222b6b0df7SMatthew Dillon vm_pageout_scan(pass); 14232d8acc0fSJohn Dyson vm_pageout_deficit = 0; 1424df8bae1dSRodney W. Grimes } 1425df8bae1dSRodney W. Grimes } 142626f9a767SRodney W. Grimes 1427e0c5a895SJohn Dyson void 1428e0c5a895SJohn Dyson pagedaemon_wakeup() 1429e0c5a895SJohn Dyson { 1430e0c5a895SJohn Dyson if (!vm_pages_needed && curproc != pageproc) { 1431e0c5a895SJohn Dyson vm_pages_needed++; 1432e0c5a895SJohn Dyson wakeup(&vm_pages_needed); 1433e0c5a895SJohn Dyson } 1434e0c5a895SJohn Dyson } 1435e0c5a895SJohn Dyson 143638efa82bSJohn Dyson #if !defined(NO_SWAPPING) 14375afce282SDavid Greenman static void 14385afce282SDavid Greenman vm_req_vmdaemon() 14395afce282SDavid Greenman { 14405afce282SDavid Greenman static int lastrun = 0; 14415afce282SDavid Greenman 1442b18bfc3dSJohn Dyson if ((ticks > (lastrun + hz)) || (ticks < lastrun)) { 14435afce282SDavid Greenman wakeup(&vm_daemon_needed); 14445afce282SDavid Greenman lastrun = ticks; 14455afce282SDavid Greenman } 14465afce282SDavid Greenman } 14475afce282SDavid Greenman 14482b14f991SJulian Elischer static void 14494f9fb771SBruce Evans vm_daemon() 14500d94caffSDavid Greenman { 14512fe6e4d7SDavid Greenman struct proc *p; 14520d94caffSDavid Greenman 14539ed346baSBosko Milekic mtx_lock(&Giant); 14540384fff8SJason Evans 14552fe6e4d7SDavid Greenman while (TRUE) { 1456e8f36785SJohn Dyson tsleep(&vm_daemon_needed, PPAUSE, "psleep", 0); 14574c1f8ee9SDavid Greenman if (vm_pageout_req_swapout) { 1458ceb0cf87SJohn Dyson swapout_procs(vm_pageout_req_swapout); 14594c1f8ee9SDavid Greenman vm_pageout_req_swapout = 0; 14604c1f8ee9SDavid Greenman } 14612fe6e4d7SDavid Greenman /* 14620d94caffSDavid Greenman * scan the processes for exceeding their rlimits or if 14630d94caffSDavid Greenman * process is swapped out -- deactivate pages 14642fe6e4d7SDavid Greenman */ 14652fe6e4d7SDavid Greenman 14661005a129SJohn Baldwin sx_slock(&allproc_lock); 1467fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(p, &allproc, p_list) { 1468fe2144fdSLuoqi Chen vm_pindex_t limit, size; 14692fe6e4d7SDavid Greenman 14702fe6e4d7SDavid Greenman /* 14712fe6e4d7SDavid Greenman * if this is a system process or if we have already 14722fe6e4d7SDavid Greenman * looked at this process, skip it. 14732fe6e4d7SDavid Greenman */ 14742fe6e4d7SDavid Greenman if (p->p_flag & (P_SYSTEM | P_WEXIT)) { 14752fe6e4d7SDavid Greenman continue; 14762fe6e4d7SDavid Greenman } 14772fe6e4d7SDavid Greenman /* 14782fe6e4d7SDavid Greenman * if the process is in a non-running type state, 14792fe6e4d7SDavid Greenman * don't touch it. 14802fe6e4d7SDavid Greenman */ 14819ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 14822fe6e4d7SDavid Greenman if (p->p_stat != SRUN && p->p_stat != SSLEEP) { 14839ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 14842fe6e4d7SDavid Greenman continue; 14852fe6e4d7SDavid Greenman } 14862fe6e4d7SDavid Greenman /* 14872fe6e4d7SDavid Greenman * get a limit 14882fe6e4d7SDavid Greenman */ 1489fe2144fdSLuoqi Chen limit = OFF_TO_IDX( 1490fe2144fdSLuoqi Chen qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur, 1491fe2144fdSLuoqi Chen p->p_rlimit[RLIMIT_RSS].rlim_max)); 14922fe6e4d7SDavid Greenman 14932fe6e4d7SDavid Greenman /* 14940d94caffSDavid Greenman * let processes that are swapped out really be 14950d94caffSDavid Greenman * swapped out set the limit to nothing (will force a 14960d94caffSDavid Greenman * swap-out.) 14972fe6e4d7SDavid Greenman */ 14988606d880SJohn Baldwin if ((p->p_sflag & PS_INMEM) == 0) 14990d94caffSDavid Greenman limit = 0; /* XXX */ 15009ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 15012fe6e4d7SDavid Greenman 1502fe2144fdSLuoqi Chen size = vmspace_resident_count(p->p_vmspace); 15032fe6e4d7SDavid Greenman if (limit >= 0 && size >= limit) { 1504fe2144fdSLuoqi Chen vm_pageout_map_deactivate_pages( 1505fe2144fdSLuoqi Chen &p->p_vmspace->vm_map, limit); 15062fe6e4d7SDavid Greenman } 15072fe6e4d7SDavid Greenman } 15081005a129SJohn Baldwin sx_sunlock(&allproc_lock); 150924a1cce3SDavid Greenman } 15102fe6e4d7SDavid Greenman } 151138efa82bSJohn Dyson #endif 1512