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> 79fb919e4dSMark Murray #include <sys/lock.h> 80fb919e4dSMark Murray #include <sys/mutex.h> 8126f9a767SRodney W. Grimes #include <sys/proc.h> 829c8b8baaSPeter Wemm #include <sys/kthread.h> 830384fff8SJason Evans #include <sys/ktr.h> 8426f9a767SRodney W. Grimes #include <sys/resourcevar.h> 85d2fc5315SPoul-Henning Kamp #include <sys/signalvar.h> 86f6b04d2bSDavid Greenman #include <sys/vnode.h> 87efeaf95aSDavid Greenman #include <sys/vmmeter.h> 881005a129SJohn Baldwin #include <sys/sx.h> 8938efa82bSJohn Dyson #include <sys/sysctl.h> 90df8bae1dSRodney W. Grimes 91df8bae1dSRodney W. Grimes #include <vm/vm.h> 92efeaf95aSDavid Greenman #include <vm/vm_param.h> 93efeaf95aSDavid Greenman #include <vm/vm_object.h> 94df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 95efeaf95aSDavid Greenman #include <vm/vm_map.h> 96df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h> 9724a1cce3SDavid Greenman #include <vm/vm_pager.h> 9821cd6e62SSeigo Tanimura #include <vm/vm_zone.h> 9905f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h> 100efeaf95aSDavid Greenman #include <vm/vm_extern.h> 101df8bae1dSRodney W. Grimes 1020384fff8SJason Evans #include <machine/mutex.h> 1030384fff8SJason Evans 1042b14f991SJulian Elischer /* 1052b14f991SJulian Elischer * System initialization 1062b14f991SJulian Elischer */ 1072b14f991SJulian Elischer 1082b14f991SJulian Elischer /* the kernel process "vm_pageout"*/ 1092b14f991SJulian Elischer static void vm_pageout __P((void)); 1108f9110f6SJohn Dyson static int vm_pageout_clean __P((vm_page_t)); 1112b6b0df7SMatthew Dillon static void vm_pageout_scan __P((int pass)); 112f35329acSJohn Dyson static int vm_pageout_free_page_calc __P((vm_size_t count)); 1132b14f991SJulian Elischer struct proc *pageproc; 1142b14f991SJulian Elischer 1152b14f991SJulian Elischer static struct kproc_desc page_kp = { 1162b14f991SJulian Elischer "pagedaemon", 1172b14f991SJulian Elischer vm_pageout, 1182b14f991SJulian Elischer &pageproc 1192b14f991SJulian Elischer }; 1209c8b8baaSPeter Wemm SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, &page_kp) 1212b14f991SJulian Elischer 12238efa82bSJohn Dyson #if !defined(NO_SWAPPING) 1232b14f991SJulian Elischer /* the kernel process "vm_daemon"*/ 1242b14f991SJulian Elischer static void vm_daemon __P((void)); 125f708ef1bSPoul-Henning Kamp static struct proc *vmproc; 1262b14f991SJulian Elischer 1272b14f991SJulian Elischer static struct kproc_desc vm_kp = { 1282b14f991SJulian Elischer "vmdaemon", 1292b14f991SJulian Elischer vm_daemon, 1302b14f991SJulian Elischer &vmproc 1312b14f991SJulian Elischer }; 1329c8b8baaSPeter Wemm SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp) 13338efa82bSJohn Dyson #endif 1342b14f991SJulian Elischer 1352b14f991SJulian Elischer 1362d8acc0fSJohn Dyson int vm_pages_needed=0; /* Event on which pageout daemon sleeps */ 1372d8acc0fSJohn Dyson int vm_pageout_deficit=0; /* Estimated number of pages deficit */ 1382d8acc0fSJohn Dyson int vm_pageout_pages_needed=0; /* flag saying that the pageout daemon needs pages */ 13926f9a767SRodney W. Grimes 14038efa82bSJohn Dyson #if !defined(NO_SWAPPING) 141f708ef1bSPoul-Henning Kamp static int vm_pageout_req_swapout; /* XXX */ 142f708ef1bSPoul-Henning Kamp static int vm_daemon_needed; 14338efa82bSJohn Dyson #endif 1445663e6deSDavid Greenman extern int vm_swap_size; 1452b6b0df7SMatthew Dillon static int vm_max_launder = 32; 146303b270bSEivind Eklund static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0; 147303b270bSEivind Eklund static int vm_pageout_full_stats_interval = 0; 1482b6b0df7SMatthew Dillon static int vm_pageout_stats_free_max=0, vm_pageout_algorithm=0; 149303b270bSEivind Eklund static int defer_swap_pageouts=0; 150303b270bSEivind Eklund static int disable_swap_pageouts=0; 15170111b90SJohn Dyson 15238efa82bSJohn Dyson #if defined(NO_SWAPPING) 153303b270bSEivind Eklund static int vm_swap_enabled=0; 154303b270bSEivind Eklund static int vm_swap_idle_enabled=0; 15538efa82bSJohn Dyson #else 156303b270bSEivind Eklund static int vm_swap_enabled=1; 157303b270bSEivind Eklund static int vm_swap_idle_enabled=0; 15838efa82bSJohn Dyson #endif 15938efa82bSJohn Dyson 16038efa82bSJohn Dyson SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm, 1612b6b0df7SMatthew Dillon CTLFLAG_RW, &vm_pageout_algorithm, 0, "LRU page mgmt"); 1622b6b0df7SMatthew Dillon 1632b6b0df7SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, max_launder, 1642b6b0df7SMatthew Dillon CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout"); 16538efa82bSJohn Dyson 166dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max, 167b0359e2cSPeter Wemm CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length"); 168dc2efb27SJohn Dyson 169dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval, 170b0359e2cSPeter Wemm CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan"); 171dc2efb27SJohn Dyson 172dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval, 173b0359e2cSPeter Wemm CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan"); 174dc2efb27SJohn Dyson 175dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_free_max, 176b0359e2cSPeter Wemm CTLFLAG_RW, &vm_pageout_stats_free_max, 0, "Not implemented"); 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 196ffc82b0aSJohn Dyson #define VM_PAGEOUT_PAGE_COUNT 16 197bbc0ec52SDavid Greenman int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT; 198df8bae1dSRodney W. Grimes 199c3cb3e12SDavid Greenman int vm_page_max_wired; /* XXX max # of wired pages system-wide */ 200df8bae1dSRodney W. Grimes 20138efa82bSJohn Dyson #if !defined(NO_SWAPPING) 20238efa82bSJohn Dyson typedef void freeer_fcn_t __P((vm_map_t, vm_object_t, vm_pindex_t, int)); 20338efa82bSJohn Dyson static void vm_pageout_map_deactivate_pages __P((vm_map_t, vm_pindex_t)); 204cd41fc12SDavid Greenman static freeer_fcn_t vm_pageout_object_deactivate_pages; 205cd41fc12SDavid Greenman static void vm_req_vmdaemon __P((void)); 20638efa82bSJohn Dyson #endif 207dc2efb27SJohn Dyson static void vm_pageout_page_stats(void); 208cd41fc12SDavid Greenman 20926f9a767SRodney W. Grimes /* 21026f9a767SRodney W. Grimes * vm_pageout_clean: 21124a1cce3SDavid Greenman * 2120d94caffSDavid Greenman * Clean the page and remove it from the laundry. 21326f9a767SRodney W. Grimes * 2140d94caffSDavid Greenman * We set the busy bit to cause potential page faults on this page to 2151c7c3c6aSMatthew Dillon * block. Note the careful timing, however, the busy bit isn't set till 2161c7c3c6aSMatthew Dillon * late and we cannot do anything that will mess with the page. 21726f9a767SRodney W. Grimes */ 2181c7c3c6aSMatthew Dillon 2193af76890SPoul-Henning Kamp static int 2208f9110f6SJohn Dyson vm_pageout_clean(m) 22124a1cce3SDavid Greenman vm_page_t m; 22224a1cce3SDavid Greenman { 22326f9a767SRodney W. Grimes register vm_object_t object; 224f35329acSJohn Dyson vm_page_t mc[2*vm_pageout_page_count]; 22524a1cce3SDavid Greenman int pageout_count; 22690ecac61SMatthew Dillon int ib, is, page_base; 227a316d390SJohn Dyson vm_pindex_t pindex = m->pindex; 22826f9a767SRodney W. Grimes 22926f9a767SRodney W. Grimes object = m->object; 23024a1cce3SDavid Greenman 23126f9a767SRodney W. Grimes /* 2321c7c3c6aSMatthew Dillon * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP 2331c7c3c6aSMatthew Dillon * with the new swapper, but we could have serious problems paging 2341c7c3c6aSMatthew Dillon * out other object types if there is insufficient memory. 2351c7c3c6aSMatthew Dillon * 2361c7c3c6aSMatthew Dillon * Unfortunately, checking free memory here is far too late, so the 2371c7c3c6aSMatthew Dillon * check has been moved up a procedural level. 2381c7c3c6aSMatthew Dillon */ 2391c7c3c6aSMatthew Dillon 24024a1cce3SDavid Greenman /* 2418b03c8edSMatthew Dillon * Don't mess with the page if it's busy, held, or special 24224a1cce3SDavid Greenman */ 2438f9110f6SJohn Dyson if ((m->hold_count != 0) || 2448b03c8edSMatthew Dillon ((m->busy != 0) || (m->flags & (PG_BUSY|PG_UNMANAGED)))) { 2450d94caffSDavid Greenman return 0; 2468b03c8edSMatthew Dillon } 2470d94caffSDavid Greenman 248f35329acSJohn Dyson mc[vm_pageout_page_count] = m; 24926f9a767SRodney W. Grimes pageout_count = 1; 250f35329acSJohn Dyson page_base = vm_pageout_page_count; 25190ecac61SMatthew Dillon ib = 1; 25290ecac61SMatthew Dillon is = 1; 25390ecac61SMatthew Dillon 25424a1cce3SDavid Greenman /* 25524a1cce3SDavid Greenman * Scan object for clusterable pages. 25624a1cce3SDavid Greenman * 25724a1cce3SDavid Greenman * We can cluster ONLY if: ->> the page is NOT 25824a1cce3SDavid Greenman * clean, wired, busy, held, or mapped into a 25924a1cce3SDavid Greenman * buffer, and one of the following: 26024a1cce3SDavid Greenman * 1) The page is inactive, or a seldom used 26124a1cce3SDavid Greenman * active page. 26224a1cce3SDavid Greenman * -or- 26324a1cce3SDavid Greenman * 2) we force the issue. 26490ecac61SMatthew Dillon * 26590ecac61SMatthew Dillon * During heavy mmap/modification loads the pageout 26690ecac61SMatthew Dillon * daemon can really fragment the underlying file 26790ecac61SMatthew Dillon * due to flushing pages out of order and not trying 26890ecac61SMatthew Dillon * align the clusters (which leave sporatic out-of-order 26990ecac61SMatthew Dillon * holes). To solve this problem we do the reverse scan 27090ecac61SMatthew Dillon * first and attempt to align our cluster, then do a 27190ecac61SMatthew Dillon * forward scan if room remains. 27224a1cce3SDavid Greenman */ 27390ecac61SMatthew Dillon 27490ecac61SMatthew Dillon more: 27590ecac61SMatthew Dillon while (ib && pageout_count < vm_pageout_page_count) { 27624a1cce3SDavid Greenman vm_page_t p; 277f6b04d2bSDavid Greenman 27890ecac61SMatthew Dillon if (ib > pindex) { 27990ecac61SMatthew Dillon ib = 0; 28090ecac61SMatthew Dillon break; 281f6b04d2bSDavid Greenman } 28290ecac61SMatthew Dillon 28390ecac61SMatthew Dillon if ((p = vm_page_lookup(object, pindex - ib)) == NULL) { 28490ecac61SMatthew Dillon ib = 0; 28590ecac61SMatthew Dillon break; 28690ecac61SMatthew Dillon } 2875070c7f8SJohn Dyson if (((p->queue - p->pc) == PQ_CACHE) || 2888b03c8edSMatthew Dillon (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) { 28990ecac61SMatthew Dillon ib = 0; 29090ecac61SMatthew Dillon break; 291f6b04d2bSDavid Greenman } 29224a1cce3SDavid Greenman vm_page_test_dirty(p); 29390ecac61SMatthew Dillon if ((p->dirty & p->valid) == 0 || 29490ecac61SMatthew Dillon p->queue != PQ_INACTIVE || 29590ecac61SMatthew Dillon p->wire_count != 0 || 29690ecac61SMatthew Dillon p->hold_count != 0) { 29790ecac61SMatthew Dillon ib = 0; 29824a1cce3SDavid Greenman break; 299f6b04d2bSDavid Greenman } 30090ecac61SMatthew Dillon mc[--page_base] = p; 30190ecac61SMatthew Dillon ++pageout_count; 30290ecac61SMatthew Dillon ++ib; 30324a1cce3SDavid Greenman /* 30490ecac61SMatthew Dillon * alignment boundry, stop here and switch directions. Do 30590ecac61SMatthew Dillon * not clear ib. 30624a1cce3SDavid Greenman */ 30790ecac61SMatthew Dillon if ((pindex - (ib - 1)) % vm_pageout_page_count == 0) 30890ecac61SMatthew Dillon break; 30924a1cce3SDavid Greenman } 31090ecac61SMatthew Dillon 31190ecac61SMatthew Dillon while (pageout_count < vm_pageout_page_count && 31290ecac61SMatthew Dillon pindex + is < object->size) { 31390ecac61SMatthew Dillon vm_page_t p; 31490ecac61SMatthew Dillon 31590ecac61SMatthew Dillon if ((p = vm_page_lookup(object, pindex + is)) == NULL) 31690ecac61SMatthew Dillon break; 3175070c7f8SJohn Dyson if (((p->queue - p->pc) == PQ_CACHE) || 3188b03c8edSMatthew Dillon (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) { 31990ecac61SMatthew Dillon break; 32024a1cce3SDavid Greenman } 32124a1cce3SDavid Greenman vm_page_test_dirty(p); 32290ecac61SMatthew Dillon if ((p->dirty & p->valid) == 0 || 32390ecac61SMatthew Dillon p->queue != PQ_INACTIVE || 32490ecac61SMatthew Dillon p->wire_count != 0 || 32590ecac61SMatthew Dillon p->hold_count != 0) { 32624a1cce3SDavid Greenman break; 32724a1cce3SDavid Greenman } 32890ecac61SMatthew Dillon mc[page_base + pageout_count] = p; 32990ecac61SMatthew Dillon ++pageout_count; 33090ecac61SMatthew Dillon ++is; 33124a1cce3SDavid Greenman } 33290ecac61SMatthew Dillon 33390ecac61SMatthew Dillon /* 33490ecac61SMatthew Dillon * If we exhausted our forward scan, continue with the reverse scan 33590ecac61SMatthew Dillon * when possible, even past a page boundry. This catches boundry 33690ecac61SMatthew Dillon * conditions. 33790ecac61SMatthew Dillon */ 33890ecac61SMatthew Dillon if (ib && pageout_count < vm_pageout_page_count) 33990ecac61SMatthew Dillon goto more; 340f6b04d2bSDavid Greenman 34167bf6868SJohn Dyson /* 34267bf6868SJohn Dyson * we allow reads during pageouts... 34367bf6868SJohn Dyson */ 3448f9110f6SJohn Dyson return vm_pageout_flush(&mc[page_base], pageout_count, 0); 345aef922f5SJohn Dyson } 346aef922f5SJohn Dyson 3471c7c3c6aSMatthew Dillon /* 3481c7c3c6aSMatthew Dillon * vm_pageout_flush() - launder the given pages 3491c7c3c6aSMatthew Dillon * 3501c7c3c6aSMatthew Dillon * The given pages are laundered. Note that we setup for the start of 3511c7c3c6aSMatthew Dillon * I/O ( i.e. busy the page ), mark it read-only, and bump the object 3521c7c3c6aSMatthew Dillon * reference count all in here rather then in the parent. If we want 3531c7c3c6aSMatthew Dillon * the parent to do more sophisticated things we may have to change 3541c7c3c6aSMatthew Dillon * the ordering. 3551c7c3c6aSMatthew Dillon */ 3561c7c3c6aSMatthew Dillon 357aef922f5SJohn Dyson int 3588f9110f6SJohn Dyson vm_pageout_flush(mc, count, flags) 359aef922f5SJohn Dyson vm_page_t *mc; 360aef922f5SJohn Dyson int count; 3618f9110f6SJohn Dyson int flags; 362aef922f5SJohn Dyson { 363aef922f5SJohn Dyson register vm_object_t object; 364aef922f5SJohn Dyson int pageout_status[count]; 36595461b45SJohn Dyson int numpagedout = 0; 366aef922f5SJohn Dyson int i; 367aef922f5SJohn Dyson 3681c7c3c6aSMatthew Dillon /* 3691c7c3c6aSMatthew Dillon * Initiate I/O. Bump the vm_page_t->busy counter and 3701c7c3c6aSMatthew Dillon * mark the pages read-only. 3711c7c3c6aSMatthew Dillon * 3721c7c3c6aSMatthew Dillon * We do not have to fixup the clean/dirty bits here... we can 3731c7c3c6aSMatthew Dillon * allow the pager to do it after the I/O completes. 37402fa91d3SMatthew Dillon * 37502fa91d3SMatthew Dillon * NOTE! mc[i]->dirty may be partial or fragmented due to an 37602fa91d3SMatthew Dillon * edge case with file fragments. 3771c7c3c6aSMatthew Dillon */ 3781c7c3c6aSMatthew Dillon 3798f9110f6SJohn Dyson for (i = 0; i < count; i++) { 38002fa91d3SMatthew Dillon KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL, ("vm_pageout_flush page %p index %d/%d: partially invalid page", mc[i], i, count)); 381e69763a3SDoug Rabson vm_page_io_start(mc[i]); 3828f9110f6SJohn Dyson vm_page_protect(mc[i], VM_PROT_READ); 3838f9110f6SJohn Dyson } 3848f9110f6SJohn Dyson 385aef922f5SJohn Dyson object = mc[0]->object; 386d474eaaaSDoug Rabson vm_object_pip_add(object, count); 387aef922f5SJohn Dyson 388aef922f5SJohn Dyson vm_pager_put_pages(object, mc, count, 3898f9110f6SJohn Dyson (flags | ((object == kernel_object) ? OBJPC_SYNC : 0)), 39026f9a767SRodney W. Grimes pageout_status); 39126f9a767SRodney W. Grimes 392aef922f5SJohn Dyson for (i = 0; i < count; i++) { 393aef922f5SJohn Dyson vm_page_t mt = mc[i]; 39424a1cce3SDavid Greenman 39526f9a767SRodney W. Grimes switch (pageout_status[i]) { 39626f9a767SRodney W. Grimes case VM_PAGER_OK: 39795461b45SJohn Dyson numpagedout++; 39826f9a767SRodney W. Grimes break; 39926f9a767SRodney W. Grimes case VM_PAGER_PEND: 40095461b45SJohn Dyson numpagedout++; 40126f9a767SRodney W. Grimes break; 40226f9a767SRodney W. Grimes case VM_PAGER_BAD: 40326f9a767SRodney W. Grimes /* 4040d94caffSDavid Greenman * Page outside of range of object. Right now we 4050d94caffSDavid Greenman * essentially lose the changes by pretending it 4060d94caffSDavid Greenman * worked. 40726f9a767SRodney W. Grimes */ 4080385347cSPeter Wemm pmap_clear_modify(mt); 40990ecac61SMatthew Dillon vm_page_undirty(mt); 41026f9a767SRodney W. Grimes break; 41126f9a767SRodney W. Grimes case VM_PAGER_ERROR: 41226f9a767SRodney W. Grimes case VM_PAGER_FAIL: 41326f9a767SRodney W. Grimes /* 4140d94caffSDavid Greenman * If page couldn't be paged out, then reactivate the 4150d94caffSDavid Greenman * page so it doesn't clog the inactive list. (We 4160d94caffSDavid Greenman * will try paging out it again later). 41726f9a767SRodney W. Grimes */ 41824a1cce3SDavid Greenman vm_page_activate(mt); 41926f9a767SRodney W. Grimes break; 42026f9a767SRodney W. Grimes case VM_PAGER_AGAIN: 42126f9a767SRodney W. Grimes break; 42226f9a767SRodney W. Grimes } 42326f9a767SRodney W. Grimes 42426f9a767SRodney W. Grimes /* 4250d94caffSDavid Greenman * If the operation is still going, leave the page busy to 4260d94caffSDavid Greenman * block all other accesses. Also, leave the paging in 4270d94caffSDavid Greenman * progress indicator set so that we don't attempt an object 4280d94caffSDavid Greenman * collapse. 42926f9a767SRodney W. Grimes */ 43026f9a767SRodney W. Grimes if (pageout_status[i] != VM_PAGER_PEND) { 431f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 432e69763a3SDoug Rabson vm_page_io_finish(mt); 433936524aaSMatthew Dillon if (!vm_page_count_severe() || !vm_page_try_to_cache(mt)) 434936524aaSMatthew Dillon vm_page_protect(mt, VM_PROT_READ); 43526f9a767SRodney W. Grimes } 43626f9a767SRodney W. Grimes } 43795461b45SJohn Dyson return numpagedout; 43826f9a767SRodney W. Grimes } 43926f9a767SRodney W. Grimes 44038efa82bSJohn Dyson #if !defined(NO_SWAPPING) 44126f9a767SRodney W. Grimes /* 44226f9a767SRodney W. Grimes * vm_pageout_object_deactivate_pages 44326f9a767SRodney W. Grimes * 44426f9a767SRodney W. Grimes * deactivate enough pages to satisfy the inactive target 44526f9a767SRodney W. Grimes * requirements or if vm_page_proc_limit is set, then 44626f9a767SRodney W. Grimes * deactivate all of the pages in the object and its 44724a1cce3SDavid Greenman * backing_objects. 44826f9a767SRodney W. Grimes * 44926f9a767SRodney W. Grimes * The object and map must be locked. 45026f9a767SRodney W. Grimes */ 45138efa82bSJohn Dyson static void 45238efa82bSJohn Dyson vm_pageout_object_deactivate_pages(map, object, desired, map_remove_only) 45326f9a767SRodney W. Grimes vm_map_t map; 45426f9a767SRodney W. Grimes vm_object_t object; 45538efa82bSJohn Dyson vm_pindex_t desired; 4560d94caffSDavid Greenman int map_remove_only; 45726f9a767SRodney W. Grimes { 45826f9a767SRodney W. Grimes register vm_page_t p, next; 45926f9a767SRodney W. Grimes int rcount; 46038efa82bSJohn Dyson int remove_mode; 4611eeaa1e3SJohn Dyson int s; 46226f9a767SRodney W. Grimes 46324964514SPeter Wemm if (object->type == OBJT_DEVICE || object->type == OBJT_PHYS) 46438efa82bSJohn Dyson return; 4658f895206SDavid Greenman 46638efa82bSJohn Dyson while (object) { 467b1028ad1SLuoqi Chen if (pmap_resident_count(vm_map_pmap(map)) <= desired) 46838efa82bSJohn Dyson return; 46924a1cce3SDavid Greenman if (object->paging_in_progress) 47038efa82bSJohn Dyson return; 47126f9a767SRodney W. Grimes 47238efa82bSJohn Dyson remove_mode = map_remove_only; 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); 48026f9a767SRodney W. Grimes while (p && (rcount-- > 0)) { 4817e006499SJohn Dyson int actcount; 482b1028ad1SLuoqi Chen if (pmap_resident_count(vm_map_pmap(map)) <= desired) 48338efa82bSJohn Dyson return; 484b18bfc3dSJohn Dyson next = TAILQ_NEXT(p, listq); 485a58d1fa1SDavid Greenman cnt.v_pdpages++; 4860d94caffSDavid Greenman if (p->wire_count != 0 || 4870d94caffSDavid Greenman p->hold_count != 0 || 4880d94caffSDavid Greenman p->busy != 0 || 4898b03c8edSMatthew Dillon (p->flags & (PG_BUSY|PG_UNMANAGED)) || 4900385347cSPeter Wemm !pmap_page_exists(vm_map_pmap(map), p)) { 4910d94caffSDavid Greenman p = next; 4920d94caffSDavid Greenman continue; 4930d94caffSDavid Greenman } 494ef743ce6SJohn Dyson 4950385347cSPeter Wemm actcount = pmap_ts_referenced(p); 4967e006499SJohn Dyson if (actcount) { 497e69763a3SDoug Rabson vm_page_flag_set(p, PG_REFERENCED); 498c8c4b40cSJohn Dyson } else if (p->flags & PG_REFERENCED) { 4997e006499SJohn Dyson actcount = 1; 500ef743ce6SJohn Dyson } 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))) { 511b18bfc3dSJohn Dyson vm_page_protect(p, VM_PROT_NONE); 51226f9a767SRodney W. Grimes vm_page_deactivate(p); 51326f9a767SRodney W. Grimes } else { 514c8c4b40cSJohn Dyson s = splvm(); 515be72f788SAlan Cox TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, p, pageq); 516be72f788SAlan Cox TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, p, pageq); 517c8c4b40cSJohn Dyson splx(s); 518c8c4b40cSJohn Dyson } 519c8c4b40cSJohn Dyson } else { 520eaf13dd7SJohn Dyson vm_page_activate(p); 521e69763a3SDoug Rabson vm_page_flag_clear(p, PG_REFERENCED); 52238efa82bSJohn Dyson if (p->act_count < (ACT_MAX - ACT_ADVANCE)) 52338efa82bSJohn Dyson p->act_count += ACT_ADVANCE; 5241eeaa1e3SJohn Dyson s = splvm(); 525be72f788SAlan Cox TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, p, pageq); 526be72f788SAlan Cox TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, p, pageq); 5271eeaa1e3SJohn Dyson splx(s); 52826f9a767SRodney W. Grimes } 529bd7e5f99SJohn Dyson } else if (p->queue == PQ_INACTIVE) { 530f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 53126f9a767SRodney W. Grimes } 53226f9a767SRodney W. Grimes p = next; 53326f9a767SRodney W. Grimes } 53438efa82bSJohn Dyson object = object->backing_object; 53538efa82bSJohn Dyson } 53638efa82bSJohn Dyson return; 53726f9a767SRodney W. Grimes } 53826f9a767SRodney W. Grimes 53926f9a767SRodney W. Grimes /* 54026f9a767SRodney W. Grimes * deactivate some number of pages in a map, try to do it fairly, but 54126f9a767SRodney W. Grimes * that is really hard to do. 54226f9a767SRodney W. Grimes */ 543cd41fc12SDavid Greenman static void 54438efa82bSJohn Dyson vm_pageout_map_deactivate_pages(map, desired) 54526f9a767SRodney W. Grimes vm_map_t map; 54638efa82bSJohn Dyson vm_pindex_t desired; 54726f9a767SRodney W. Grimes { 54826f9a767SRodney W. Grimes vm_map_entry_t tmpe; 54938efa82bSJohn Dyson vm_object_t obj, bigobj; 5500d94caffSDavid Greenman 551996c772fSJohn Dyson if (lockmgr(&map->lock, LK_EXCLUSIVE | LK_NOWAIT, (void *)0, curproc)) { 55226f9a767SRodney W. Grimes return; 55326f9a767SRodney W. Grimes } 55438efa82bSJohn Dyson 55538efa82bSJohn Dyson bigobj = NULL; 55638efa82bSJohn Dyson 55738efa82bSJohn Dyson /* 55838efa82bSJohn Dyson * first, search out the biggest object, and try to free pages from 55938efa82bSJohn Dyson * that. 56038efa82bSJohn Dyson */ 56126f9a767SRodney W. Grimes tmpe = map->header.next; 56238efa82bSJohn Dyson while (tmpe != &map->header) { 5639fdfe602SMatthew Dillon if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 56438efa82bSJohn Dyson obj = tmpe->object.vm_object; 56538efa82bSJohn Dyson if ((obj != NULL) && (obj->shadow_count <= 1) && 56638efa82bSJohn Dyson ((bigobj == NULL) || 56738efa82bSJohn Dyson (bigobj->resident_page_count < obj->resident_page_count))) { 56838efa82bSJohn Dyson bigobj = obj; 56938efa82bSJohn Dyson } 57038efa82bSJohn Dyson } 57138efa82bSJohn Dyson tmpe = tmpe->next; 57238efa82bSJohn Dyson } 57338efa82bSJohn Dyson 57438efa82bSJohn Dyson if (bigobj) 57538efa82bSJohn Dyson vm_pageout_object_deactivate_pages(map, bigobj, desired, 0); 57638efa82bSJohn Dyson 57738efa82bSJohn Dyson /* 57838efa82bSJohn Dyson * Next, hunt around for other pages to deactivate. We actually 57938efa82bSJohn Dyson * do this search sort of wrong -- .text first is not the best idea. 58038efa82bSJohn Dyson */ 58138efa82bSJohn Dyson tmpe = map->header.next; 58238efa82bSJohn Dyson while (tmpe != &map->header) { 583b1028ad1SLuoqi Chen if (pmap_resident_count(vm_map_pmap(map)) <= desired) 58438efa82bSJohn Dyson break; 5859fdfe602SMatthew Dillon if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 58638efa82bSJohn Dyson obj = tmpe->object.vm_object; 58701155bd7SDavid Greenman if (obj) 58838efa82bSJohn Dyson vm_pageout_object_deactivate_pages(map, obj, desired, 0); 58938efa82bSJohn Dyson } 59026f9a767SRodney W. Grimes tmpe = tmpe->next; 59126f9a767SRodney W. Grimes }; 59238efa82bSJohn Dyson 59338efa82bSJohn Dyson /* 59438efa82bSJohn Dyson * Remove all mappings if a process is swapped out, this will free page 59538efa82bSJohn Dyson * table pages. 59638efa82bSJohn Dyson */ 59738efa82bSJohn Dyson if (desired == 0) 59838efa82bSJohn Dyson pmap_remove(vm_map_pmap(map), 59938efa82bSJohn Dyson VM_MIN_ADDRESS, VM_MAXUSER_ADDRESS); 60038efa82bSJohn Dyson vm_map_unlock(map); 60126f9a767SRodney W. Grimes return; 60226f9a767SRodney W. Grimes } 60338efa82bSJohn Dyson #endif 604df8bae1dSRodney W. Grimes 6051c7c3c6aSMatthew Dillon /* 6061c7c3c6aSMatthew Dillon * Don't try to be fancy - being fancy can lead to VOP_LOCK's and therefore 6071c7c3c6aSMatthew Dillon * to vnode deadlocks. We only do it for OBJT_DEFAULT and OBJT_SWAP objects 6081c7c3c6aSMatthew Dillon * which we know can be trivially freed. 6091c7c3c6aSMatthew Dillon */ 6101c7c3c6aSMatthew Dillon 611925a3a41SJohn Dyson void 612925a3a41SJohn Dyson vm_pageout_page_free(vm_page_t m) { 6131c7c3c6aSMatthew Dillon vm_object_t object = m->object; 6141c7c3c6aSMatthew Dillon int type = object->type; 615925a3a41SJohn Dyson 6161c7c3c6aSMatthew Dillon if (type == OBJT_SWAP || type == OBJT_DEFAULT) 6171c7c3c6aSMatthew Dillon vm_object_reference(object); 618e69763a3SDoug Rabson vm_page_busy(m); 619925a3a41SJohn Dyson vm_page_protect(m, VM_PROT_NONE); 620925a3a41SJohn Dyson vm_page_free(m); 6211c7c3c6aSMatthew Dillon if (type == OBJT_SWAP || type == OBJT_DEFAULT) 62247221757SJohn Dyson vm_object_deallocate(object); 623925a3a41SJohn Dyson } 624925a3a41SJohn Dyson 625df8bae1dSRodney W. Grimes /* 626df8bae1dSRodney W. Grimes * vm_pageout_scan does the dirty work for the pageout daemon. 627df8bae1dSRodney W. Grimes */ 6282b6b0df7SMatthew Dillon static void 6292b6b0df7SMatthew Dillon vm_pageout_scan(int pass) 630df8bae1dSRodney W. Grimes { 631502ba6e4SJohn Dyson vm_page_t m, next; 632936524aaSMatthew Dillon struct vm_page marker; 6332b6b0df7SMatthew Dillon int save_page_shortage; 6342b6b0df7SMatthew Dillon int save_inactive_count; 6351c7c3c6aSMatthew Dillon int page_shortage, maxscan, pcount; 6361c7c3c6aSMatthew Dillon int addl_page_shortage, addl_page_shortage_init; 6375663e6deSDavid Greenman struct proc *p, *bigproc; 6385663e6deSDavid Greenman vm_offset_t size, bigsize; 639df8bae1dSRodney W. Grimes vm_object_t object; 6407e006499SJohn Dyson int actcount; 641f6b04d2bSDavid Greenman int vnodes_skipped = 0; 6422b6b0df7SMatthew Dillon int maxlaunder; 6431eeaa1e3SJohn Dyson int s; 6440d94caffSDavid Greenman 645df8bae1dSRodney W. Grimes /* 6465985940eSJohn Dyson * Do whatever cleanup that the pmap code can. 6475985940eSJohn Dyson */ 6485985940eSJohn Dyson pmap_collect(); 6495985940eSJohn Dyson 6501c7c3c6aSMatthew Dillon addl_page_shortage_init = vm_pageout_deficit; 65195461b45SJohn Dyson vm_pageout_deficit = 0; 652b182ec9eSJohn Dyson 6531c7c3c6aSMatthew Dillon /* 6541c7c3c6aSMatthew Dillon * Calculate the number of pages we want to either free or move 6552b6b0df7SMatthew Dillon * to the cache. 6561c7c3c6aSMatthew Dillon */ 6572b6b0df7SMatthew Dillon page_shortage = vm_paging_target() + addl_page_shortage_init; 6582b6b0df7SMatthew Dillon save_page_shortage = page_shortage; 6592b6b0df7SMatthew Dillon save_inactive_count = cnt.v_inactive_count; 6601c7c3c6aSMatthew Dillon 6611c7c3c6aSMatthew Dillon /* 662936524aaSMatthew Dillon * Initialize our marker 663936524aaSMatthew Dillon */ 664936524aaSMatthew Dillon bzero(&marker, sizeof(marker)); 665936524aaSMatthew Dillon marker.flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER; 666936524aaSMatthew Dillon marker.queue = PQ_INACTIVE; 667936524aaSMatthew Dillon marker.wire_count = 1; 668936524aaSMatthew Dillon 669936524aaSMatthew Dillon /* 6701c7c3c6aSMatthew Dillon * Start scanning the inactive queue for pages we can move to the 6711c7c3c6aSMatthew Dillon * cache or free. The scan will stop when the target is reached or 672936524aaSMatthew Dillon * we have scanned the entire inactive queue. Note that m->act_count 673936524aaSMatthew Dillon * is not used to form decisions for the inactive queue, only for the 674936524aaSMatthew Dillon * active queue. 6752b6b0df7SMatthew Dillon * 6762b6b0df7SMatthew Dillon * maxlaunder limits the number of dirty pages we flush per scan. 6772b6b0df7SMatthew Dillon * For most systems a smaller value (16 or 32) is more robust under 6782b6b0df7SMatthew Dillon * extreme memory and disk pressure because any unnecessary writes 6792b6b0df7SMatthew Dillon * to disk can result in extreme performance degredation. However, 6802b6b0df7SMatthew Dillon * systems with excessive dirty pages (especially when MAP_NOSYNC is 6812b6b0df7SMatthew Dillon * used) will die horribly with limited laundering. If the pageout 6822b6b0df7SMatthew Dillon * daemon cannot clean enough pages in the first pass, we let it go 6832b6b0df7SMatthew Dillon * all out in succeeding passes. 6841c7c3c6aSMatthew Dillon */ 68570111b90SJohn Dyson 6862b6b0df7SMatthew Dillon if ((maxlaunder = vm_max_launder) <= 1) 6872b6b0df7SMatthew Dillon maxlaunder = 1; 6882b6b0df7SMatthew Dillon if (pass) 6892b6b0df7SMatthew Dillon maxlaunder = 10000; 6902b6b0df7SMatthew Dillon 69167bf6868SJohn Dyson rescan0: 6921c7c3c6aSMatthew Dillon addl_page_shortage = addl_page_shortage_init; 693f6b04d2bSDavid Greenman maxscan = cnt.v_inactive_count; 694be72f788SAlan Cox for (m = TAILQ_FIRST(&vm_page_queues[PQ_INACTIVE].pl); 6951c7c3c6aSMatthew Dillon m != NULL && maxscan-- > 0 && page_shortage > 0; 696e929c00dSKirk McKusick m = next) { 697df8bae1dSRodney W. Grimes 698a58d1fa1SDavid Greenman cnt.v_pdpages++; 699b182ec9eSJohn Dyson 700f35329acSJohn Dyson if (m->queue != PQ_INACTIVE) { 70167bf6868SJohn Dyson goto rescan0; 702f35329acSJohn Dyson } 703b182ec9eSJohn Dyson 704b18bfc3dSJohn Dyson next = TAILQ_NEXT(m, pageq); 705df8bae1dSRodney W. Grimes 706936524aaSMatthew Dillon /* 707936524aaSMatthew Dillon * skip marker pages 708936524aaSMatthew Dillon */ 709936524aaSMatthew Dillon if (m->flags & PG_MARKER) 710936524aaSMatthew Dillon continue; 711936524aaSMatthew Dillon 712b182ec9eSJohn Dyson if (m->hold_count) { 713f35329acSJohn Dyson s = splvm(); 714be72f788SAlan Cox TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 715be72f788SAlan Cox TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 716f35329acSJohn Dyson splx(s); 717b182ec9eSJohn Dyson addl_page_shortage++; 718b182ec9eSJohn Dyson continue; 719df8bae1dSRodney W. Grimes } 72026f9a767SRodney W. Grimes /* 721b18bfc3dSJohn Dyson * Dont mess with busy pages, keep in the front of the 722b18bfc3dSJohn Dyson * queue, most likely are being paged out. 72326f9a767SRodney W. Grimes */ 724bd7e5f99SJohn Dyson if (m->busy || (m->flags & PG_BUSY)) { 725b182ec9eSJohn Dyson addl_page_shortage++; 72626f9a767SRodney W. Grimes continue; 72726f9a767SRodney W. Grimes } 728bd7e5f99SJohn Dyson 7297e006499SJohn Dyson /* 7301c7c3c6aSMatthew Dillon * If the object is not being used, we ignore previous 7311c7c3c6aSMatthew Dillon * references. 7327e006499SJohn Dyson */ 7330d94caffSDavid Greenman if (m->object->ref_count == 0) { 734e69763a3SDoug Rabson vm_page_flag_clear(m, PG_REFERENCED); 7350385347cSPeter Wemm pmap_clear_reference(m); 7367e006499SJohn Dyson 7377e006499SJohn Dyson /* 7381c7c3c6aSMatthew Dillon * Otherwise, if the page has been referenced while in the 7391c7c3c6aSMatthew Dillon * inactive queue, we bump the "activation count" upwards, 7401c7c3c6aSMatthew Dillon * making it less likely that the page will be added back to 7411c7c3c6aSMatthew Dillon * the inactive queue prematurely again. Here we check the 7421c7c3c6aSMatthew Dillon * page tables (or emulated bits, if any), given the upper 7431c7c3c6aSMatthew Dillon * level VM system not knowing anything about existing 7441c7c3c6aSMatthew Dillon * references. 7457e006499SJohn Dyson */ 746ef743ce6SJohn Dyson } else if (((m->flags & PG_REFERENCED) == 0) && 7470385347cSPeter Wemm (actcount = pmap_ts_referenced(m))) { 748ef743ce6SJohn Dyson vm_page_activate(m); 7497e006499SJohn Dyson m->act_count += (actcount + ACT_ADVANCE); 750ef743ce6SJohn Dyson continue; 7512fe6e4d7SDavid Greenman } 752ef743ce6SJohn Dyson 7537e006499SJohn Dyson /* 7541c7c3c6aSMatthew Dillon * If the upper level VM system knows about any page 7551c7c3c6aSMatthew Dillon * references, we activate the page. We also set the 7561c7c3c6aSMatthew Dillon * "activation count" higher than normal so that we will less 7571c7c3c6aSMatthew Dillon * likely place pages back onto the inactive queue again. 7587e006499SJohn Dyson */ 759bd7e5f99SJohn Dyson if ((m->flags & PG_REFERENCED) != 0) { 760e69763a3SDoug Rabson vm_page_flag_clear(m, PG_REFERENCED); 7610385347cSPeter Wemm actcount = pmap_ts_referenced(m); 76226f9a767SRodney W. Grimes vm_page_activate(m); 7637e006499SJohn Dyson m->act_count += (actcount + ACT_ADVANCE + 1); 7640d94caffSDavid Greenman continue; 7650d94caffSDavid Greenman } 76667bf6868SJohn Dyson 7677e006499SJohn Dyson /* 7681c7c3c6aSMatthew Dillon * If the upper level VM system doesn't know anything about 7691c7c3c6aSMatthew Dillon * the page being dirty, we have to check for it again. As 7701c7c3c6aSMatthew Dillon * far as the VM code knows, any partially dirty pages are 7711c7c3c6aSMatthew Dillon * fully dirty. 7727e006499SJohn Dyson */ 773f6b04d2bSDavid Greenman if (m->dirty == 0) { 774bd7e5f99SJohn Dyson vm_page_test_dirty(m); 775427e99a0SAlexander Langer } else { 7767dbf82dcSMatthew Dillon vm_page_dirty(m); 77730dcfc09SJohn Dyson } 778ef743ce6SJohn Dyson 7797e006499SJohn Dyson /* 7807e006499SJohn Dyson * Invalid pages can be easily freed 7817e006499SJohn Dyson */ 7826d40c3d3SDavid Greenman if (m->valid == 0) { 783925a3a41SJohn Dyson vm_pageout_page_free(m); 78467bf6868SJohn Dyson cnt.v_dfree++; 7851c7c3c6aSMatthew Dillon --page_shortage; 7867e006499SJohn Dyson 7877e006499SJohn Dyson /* 788936524aaSMatthew Dillon * Clean pages can be placed onto the cache queue. This 789936524aaSMatthew Dillon * effectively frees them. 7907e006499SJohn Dyson */ 791bd7e5f99SJohn Dyson } else if (m->dirty == 0) { 792bd7e5f99SJohn Dyson vm_page_cache(m); 7931c7c3c6aSMatthew Dillon --page_shortage; 7942b6b0df7SMatthew Dillon } else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) { 7957e006499SJohn Dyson /* 7962b6b0df7SMatthew Dillon * Dirty pages need to be paged out, but flushing 7972b6b0df7SMatthew Dillon * a page is extremely expensive verses freeing 7982b6b0df7SMatthew Dillon * a clean page. Rather then artificially limiting 7992b6b0df7SMatthew Dillon * the number of pages we can flush, we instead give 8002b6b0df7SMatthew Dillon * dirty pages extra priority on the inactive queue 8012b6b0df7SMatthew Dillon * by forcing them to be cycled through the queue 8022b6b0df7SMatthew Dillon * twice before being flushed, after which the 8032b6b0df7SMatthew Dillon * (now clean) page will cycle through once more 8042b6b0df7SMatthew Dillon * before being freed. This significantly extends 8052b6b0df7SMatthew Dillon * the thrash point for a heavily loaded machine. 8067e006499SJohn Dyson */ 8072b6b0df7SMatthew Dillon s = splvm(); 8082b6b0df7SMatthew Dillon vm_page_flag_set(m, PG_WINATCFLS); 8092b6b0df7SMatthew Dillon TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 8102b6b0df7SMatthew Dillon TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 8112b6b0df7SMatthew Dillon splx(s); 8120d94caffSDavid Greenman } else if (maxlaunder > 0) { 8132b6b0df7SMatthew Dillon /* 8142b6b0df7SMatthew Dillon * We always want to try to flush some dirty pages if 8152b6b0df7SMatthew Dillon * we encounter them, to keep the system stable. 8162b6b0df7SMatthew Dillon * Normally this number is small, but under extreme 8172b6b0df7SMatthew Dillon * pressure where there are insufficient clean pages 8182b6b0df7SMatthew Dillon * on the inactive queue, we may have to go all out. 8192b6b0df7SMatthew Dillon */ 82012ac6a1dSJohn Dyson int swap_pageouts_ok; 821f6b04d2bSDavid Greenman struct vnode *vp = NULL; 822f2a2857bSKirk McKusick struct mount *mp; 8230d94caffSDavid Greenman 8240d94caffSDavid Greenman object = m->object; 8257e006499SJohn Dyson 82612ac6a1dSJohn Dyson if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) { 82712ac6a1dSJohn Dyson swap_pageouts_ok = 1; 82812ac6a1dSJohn Dyson } else { 82912ac6a1dSJohn Dyson swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts); 83012ac6a1dSJohn Dyson swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts && 83190ecac61SMatthew Dillon vm_page_count_min()); 83212ac6a1dSJohn Dyson 83312ac6a1dSJohn Dyson } 83470111b90SJohn Dyson 83570111b90SJohn Dyson /* 8361c7c3c6aSMatthew Dillon * We don't bother paging objects that are "dead". 8371c7c3c6aSMatthew Dillon * Those objects are in a "rundown" state. 83870111b90SJohn Dyson */ 83970111b90SJohn Dyson if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) { 84012ac6a1dSJohn Dyson s = splvm(); 841be72f788SAlan Cox TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 842be72f788SAlan Cox TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 84312ac6a1dSJohn Dyson splx(s); 84412ac6a1dSJohn Dyson continue; 84512ac6a1dSJohn Dyson } 84612ac6a1dSJohn Dyson 8471c7c3c6aSMatthew Dillon /* 8482b6b0df7SMatthew Dillon * The object is already known NOT to be dead. It 8492b6b0df7SMatthew Dillon * is possible for the vget() to block the whole 8502b6b0df7SMatthew Dillon * pageout daemon, but the new low-memory handling 8512b6b0df7SMatthew Dillon * code should prevent it. 8521c7c3c6aSMatthew Dillon * 8532b6b0df7SMatthew Dillon * The previous code skipped locked vnodes and, worse, 8542b6b0df7SMatthew Dillon * reordered pages in the queue. This results in 8552b6b0df7SMatthew Dillon * completely non-deterministic operation and, on a 8562b6b0df7SMatthew Dillon * busy system, can lead to extremely non-optimal 8572b6b0df7SMatthew Dillon * pageouts. For example, it can cause clean pages 8582b6b0df7SMatthew Dillon * to be freed and dirty pages to be moved to the end 8592b6b0df7SMatthew Dillon * of the queue. Since dirty pages are also moved to 8602b6b0df7SMatthew Dillon * the end of the queue once-cleaned, this gives 8612b6b0df7SMatthew Dillon * way too large a weighting to defering the freeing 8622b6b0df7SMatthew Dillon * of dirty pages. 8631c7c3c6aSMatthew Dillon * 8642b6b0df7SMatthew Dillon * XXX we need to be able to apply a timeout to the 8652b6b0df7SMatthew Dillon * vget() lock attempt. 8661c7c3c6aSMatthew Dillon */ 8671c7c3c6aSMatthew Dillon 8681c7c3c6aSMatthew Dillon if (object->type == OBJT_VNODE) { 86924a1cce3SDavid Greenman vp = object->handle; 8701c7c3c6aSMatthew Dillon 871f2a2857bSKirk McKusick mp = NULL; 872f2a2857bSKirk McKusick if (vp->v_type == VREG) 873f2a2857bSKirk McKusick vn_start_write(vp, &mp, V_NOWAIT); 8742b6b0df7SMatthew Dillon if (vget(vp, LK_EXCLUSIVE|LK_NOOBJ, curproc)) { 875f2a2857bSKirk McKusick vn_finished_write(mp); 876aef922f5SJohn Dyson if (object->flags & OBJ_MIGHTBEDIRTY) 877925a3a41SJohn Dyson vnodes_skipped++; 878b182ec9eSJohn Dyson continue; 87985a376ebSJohn Dyson } 880b182ec9eSJohn Dyson 881f35329acSJohn Dyson /* 882936524aaSMatthew Dillon * The page might have been moved to another 883936524aaSMatthew Dillon * queue during potential blocking in vget() 884936524aaSMatthew Dillon * above. The page might have been freed and 885936524aaSMatthew Dillon * reused for another vnode. The object might 886936524aaSMatthew Dillon * have been reused for another vnode. 887f35329acSJohn Dyson */ 888936524aaSMatthew Dillon if (m->queue != PQ_INACTIVE || 889936524aaSMatthew Dillon m->object != object || 890936524aaSMatthew Dillon object->handle != vp) { 891b182ec9eSJohn Dyson if (object->flags & OBJ_MIGHTBEDIRTY) 892925a3a41SJohn Dyson vnodes_skipped++; 893b182ec9eSJohn Dyson vput(vp); 894f2a2857bSKirk McKusick vn_finished_write(mp); 895b182ec9eSJohn Dyson continue; 896b182ec9eSJohn Dyson } 897b182ec9eSJohn Dyson 898f35329acSJohn Dyson /* 899936524aaSMatthew Dillon * The page may have been busied during the 900936524aaSMatthew Dillon * blocking in vput(); We don't move the 901936524aaSMatthew Dillon * page back onto the end of the queue so that 902936524aaSMatthew Dillon * statistics are more correct if we don't. 903f35329acSJohn Dyson */ 904b182ec9eSJohn Dyson if (m->busy || (m->flags & PG_BUSY)) { 905b182ec9eSJohn Dyson vput(vp); 906f2a2857bSKirk McKusick vn_finished_write(mp); 907b182ec9eSJohn Dyson continue; 908b182ec9eSJohn Dyson } 909b182ec9eSJohn Dyson 910f35329acSJohn Dyson /* 911f35329acSJohn Dyson * If the page has become held, then skip it 912f35329acSJohn Dyson */ 913b182ec9eSJohn Dyson if (m->hold_count) { 914f35329acSJohn Dyson s = splvm(); 915be72f788SAlan Cox TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 916be72f788SAlan Cox TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 917f35329acSJohn Dyson splx(s); 918b182ec9eSJohn Dyson if (object->flags & OBJ_MIGHTBEDIRTY) 919925a3a41SJohn Dyson vnodes_skipped++; 920b182ec9eSJohn Dyson vput(vp); 921f2a2857bSKirk McKusick vn_finished_write(mp); 922f6b04d2bSDavid Greenman continue; 923f6b04d2bSDavid Greenman } 924f6b04d2bSDavid Greenman } 925f6b04d2bSDavid Greenman 9260d94caffSDavid Greenman /* 9270d94caffSDavid Greenman * If a page is dirty, then it is either being washed 9280d94caffSDavid Greenman * (but not yet cleaned) or it is still in the 9290d94caffSDavid Greenman * laundry. If it is still in the laundry, then we 9302b6b0df7SMatthew Dillon * start the cleaning operation. 931936524aaSMatthew Dillon * 932936524aaSMatthew Dillon * This operation may cluster, invalidating the 'next' 933936524aaSMatthew Dillon * pointer. To prevent an inordinate number of 934936524aaSMatthew Dillon * restarts we use our marker to remember our place. 9352b6b0df7SMatthew Dillon * 9362b6b0df7SMatthew Dillon * decrement page_shortage on success to account for 9372b6b0df7SMatthew Dillon * the (future) cleaned page. Otherwise we could wind 9382b6b0df7SMatthew Dillon * up laundering or cleaning too many pages. 9390d94caffSDavid Greenman */ 940936524aaSMatthew Dillon s = splvm(); 941936524aaSMatthew Dillon TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE].pl, m, &marker, pageq); 942936524aaSMatthew Dillon splx(s); 9432b6b0df7SMatthew Dillon if (vm_pageout_clean(m) != 0) { 9442b6b0df7SMatthew Dillon --page_shortage; 945936524aaSMatthew Dillon --maxlaunder; 9462b6b0df7SMatthew Dillon } 947936524aaSMatthew Dillon s = splvm(); 948936524aaSMatthew Dillon next = TAILQ_NEXT(&marker, pageq); 949936524aaSMatthew Dillon TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, &marker, pageq); 950936524aaSMatthew Dillon splx(s); 951f2a2857bSKirk McKusick if (vp) { 952f6b04d2bSDavid Greenman vput(vp); 953f2a2857bSKirk McKusick vn_finished_write(mp); 954f2a2857bSKirk McKusick } 9550d94caffSDavid Greenman } 956df8bae1dSRodney W. Grimes } 95726f9a767SRodney W. Grimes 958df8bae1dSRodney W. Grimes /* 959936524aaSMatthew Dillon * Compute the number of pages we want to try to move from the 960936524aaSMatthew Dillon * active queue to the inactive queue. 9611c7c3c6aSMatthew Dillon */ 962936524aaSMatthew Dillon page_shortage = vm_paging_target() + 963936524aaSMatthew Dillon cnt.v_inactive_target - cnt.v_inactive_count; 964b182ec9eSJohn Dyson page_shortage += addl_page_shortage; 9651c7c3c6aSMatthew Dillon 9661c7c3c6aSMatthew Dillon /* 967936524aaSMatthew Dillon * Scan the active queue for things we can deactivate. We nominally 968936524aaSMatthew Dillon * track the per-page activity counter and use it to locate 969936524aaSMatthew Dillon * deactivation candidates. 9701c7c3c6aSMatthew Dillon */ 97126f9a767SRodney W. Grimes 972b18bfc3dSJohn Dyson pcount = cnt.v_active_count; 973be72f788SAlan Cox m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl); 9741c7c3c6aSMatthew Dillon 975b18bfc3dSJohn Dyson while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) { 976f35329acSJohn Dyson 9777e006499SJohn Dyson /* 978956f3135SPhilippe Charnier * This is a consistency check, and should likely be a panic 9797e006499SJohn Dyson * or warning. 9807e006499SJohn Dyson */ 981f35329acSJohn Dyson if (m->queue != PQ_ACTIVE) { 98238efa82bSJohn Dyson break; 983f35329acSJohn Dyson } 984f35329acSJohn Dyson 985b18bfc3dSJohn Dyson next = TAILQ_NEXT(m, pageq); 986df8bae1dSRodney W. Grimes /* 98726f9a767SRodney W. Grimes * Don't deactivate pages that are busy. 988df8bae1dSRodney W. Grimes */ 989a647a309SDavid Greenman if ((m->busy != 0) || 9900d94caffSDavid Greenman (m->flags & PG_BUSY) || 991f6b04d2bSDavid Greenman (m->hold_count != 0)) { 992f35329acSJohn Dyson s = splvm(); 993be72f788SAlan Cox TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 994be72f788SAlan Cox TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 995f35329acSJohn Dyson splx(s); 99626f9a767SRodney W. Grimes m = next; 99726f9a767SRodney W. Grimes continue; 998df8bae1dSRodney W. Grimes } 999b18bfc3dSJohn Dyson 1000b18bfc3dSJohn Dyson /* 1001b18bfc3dSJohn Dyson * The count for pagedaemon pages is done after checking the 1002956f3135SPhilippe Charnier * page for eligibility... 1003b18bfc3dSJohn Dyson */ 1004b18bfc3dSJohn Dyson cnt.v_pdpages++; 1005ef743ce6SJohn Dyson 10067e006499SJohn Dyson /* 10077e006499SJohn Dyson * Check to see "how much" the page has been used. 10087e006499SJohn Dyson */ 10097e006499SJohn Dyson actcount = 0; 1010ef743ce6SJohn Dyson if (m->object->ref_count != 0) { 1011ef743ce6SJohn Dyson if (m->flags & PG_REFERENCED) { 10127e006499SJohn Dyson actcount += 1; 10130d94caffSDavid Greenman } 10140385347cSPeter Wemm actcount += pmap_ts_referenced(m); 10157e006499SJohn Dyson if (actcount) { 10167e006499SJohn Dyson m->act_count += ACT_ADVANCE + actcount; 101738efa82bSJohn Dyson if (m->act_count > ACT_MAX) 101838efa82bSJohn Dyson m->act_count = ACT_MAX; 101938efa82bSJohn Dyson } 1020b18bfc3dSJohn Dyson } 1021ef743ce6SJohn Dyson 10227e006499SJohn Dyson /* 10237e006499SJohn Dyson * Since we have "tested" this bit, we need to clear it now. 10247e006499SJohn Dyson */ 1025e69763a3SDoug Rabson vm_page_flag_clear(m, PG_REFERENCED); 1026ef743ce6SJohn Dyson 10277e006499SJohn Dyson /* 10287e006499SJohn Dyson * Only if an object is currently being used, do we use the 10297e006499SJohn Dyson * page activation count stats. 10307e006499SJohn Dyson */ 10317e006499SJohn Dyson if (actcount && (m->object->ref_count != 0)) { 1032f35329acSJohn Dyson s = splvm(); 1033be72f788SAlan Cox TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1034be72f788SAlan Cox TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1035f35329acSJohn Dyson splx(s); 103626f9a767SRodney W. Grimes } else { 103738efa82bSJohn Dyson m->act_count -= min(m->act_count, ACT_DECLINE); 10382b6b0df7SMatthew Dillon if (vm_pageout_algorithm || 10392b6b0df7SMatthew Dillon m->object->ref_count == 0 || 10402b6b0df7SMatthew Dillon m->act_count == 0) { 1041925a3a41SJohn Dyson page_shortage--; 1042d4a272dbSJohn Dyson if (m->object->ref_count == 0) { 1043ef743ce6SJohn Dyson vm_page_protect(m, VM_PROT_NONE); 1044d4a272dbSJohn Dyson if (m->dirty == 0) 10450d94caffSDavid Greenman vm_page_cache(m); 1046d4a272dbSJohn Dyson else 1047d4a272dbSJohn Dyson vm_page_deactivate(m); 10480d94caffSDavid Greenman } else { 104926f9a767SRodney W. Grimes vm_page_deactivate(m); 1050df8bae1dSRodney W. Grimes } 105138efa82bSJohn Dyson } else { 105238efa82bSJohn Dyson s = splvm(); 1053be72f788SAlan Cox TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1054be72f788SAlan Cox TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 105538efa82bSJohn Dyson splx(s); 105638efa82bSJohn Dyson } 1057df8bae1dSRodney W. Grimes } 105826f9a767SRodney W. Grimes m = next; 105926f9a767SRodney W. Grimes } 1060df8bae1dSRodney W. Grimes 1061f35329acSJohn Dyson s = splvm(); 10621c7c3c6aSMatthew Dillon 1063df8bae1dSRodney W. Grimes /* 10640d94caffSDavid Greenman * We try to maintain some *really* free pages, this allows interrupt 10651c7c3c6aSMatthew Dillon * code to be guaranteed space. Since both cache and free queues 10661c7c3c6aSMatthew Dillon * are considered basically 'free', moving pages from cache to free 10671c7c3c6aSMatthew Dillon * does not effect other calculations. 1068df8bae1dSRodney W. Grimes */ 10691c7c3c6aSMatthew Dillon 1070a1f6d91cSDavid Greenman while (cnt.v_free_count < cnt.v_free_reserved) { 10715070c7f8SJohn Dyson static int cache_rover = 0; 1072faa273d5SMatthew Dillon m = vm_page_list_find(PQ_CACHE, cache_rover, FALSE); 10730d94caffSDavid Greenman if (!m) 10740d94caffSDavid Greenman break; 10758b03c8edSMatthew Dillon if ((m->flags & (PG_BUSY|PG_UNMANAGED)) || 10768b03c8edSMatthew Dillon m->busy || 10778b03c8edSMatthew Dillon m->hold_count || 10788b03c8edSMatthew Dillon m->wire_count) { 1079d044d7bfSMatthew Dillon #ifdef INVARIANTS 1080d044d7bfSMatthew Dillon printf("Warning: busy page %p found in cache\n", m); 1081d044d7bfSMatthew Dillon #endif 1082aaba53daSMatthew Dillon vm_page_deactivate(m); 1083aaba53daSMatthew Dillon continue; 1084aaba53daSMatthew Dillon } 10855070c7f8SJohn Dyson cache_rover = (cache_rover + PQ_PRIME2) & PQ_L2_MASK; 1086925a3a41SJohn Dyson vm_pageout_page_free(m); 10870bb3a0d2SDavid Greenman cnt.v_dfree++; 108826f9a767SRodney W. Grimes } 1089f35329acSJohn Dyson splx(s); 10905663e6deSDavid Greenman 1091ceb0cf87SJohn Dyson #if !defined(NO_SWAPPING) 1092ceb0cf87SJohn Dyson /* 1093ceb0cf87SJohn Dyson * Idle process swapout -- run once per second. 1094ceb0cf87SJohn Dyson */ 1095ceb0cf87SJohn Dyson if (vm_swap_idle_enabled) { 1096ceb0cf87SJohn Dyson static long lsec; 1097227ee8a1SPoul-Henning Kamp if (time_second != lsec) { 1098ceb0cf87SJohn Dyson vm_pageout_req_swapout |= VM_SWAP_IDLE; 1099ceb0cf87SJohn Dyson vm_req_vmdaemon(); 1100227ee8a1SPoul-Henning Kamp lsec = time_second; 1101ceb0cf87SJohn Dyson } 1102ceb0cf87SJohn Dyson } 1103ceb0cf87SJohn Dyson #endif 1104ceb0cf87SJohn Dyson 11055663e6deSDavid Greenman /* 1106f6b04d2bSDavid Greenman * If we didn't get enough free pages, and we have skipped a vnode 11074c1f8ee9SDavid Greenman * in a writeable object, wakeup the sync daemon. And kick swapout 11084c1f8ee9SDavid Greenman * if we did not get enough free pages. 1109f6b04d2bSDavid Greenman */ 111090ecac61SMatthew Dillon if (vm_paging_target() > 0) { 111190ecac61SMatthew Dillon if (vnodes_skipped && vm_page_count_min()) 1112d50c1994SPeter Wemm (void) speedup_syncer(); 111338efa82bSJohn Dyson #if !defined(NO_SWAPPING) 111490ecac61SMatthew Dillon if (vm_swap_enabled && vm_page_count_target()) { 11154c1f8ee9SDavid Greenman vm_req_vmdaemon(); 1116ceb0cf87SJohn Dyson vm_pageout_req_swapout |= VM_SWAP_NORMAL; 11174c1f8ee9SDavid Greenman } 11185afce282SDavid Greenman #endif 11194c1f8ee9SDavid Greenman } 11204c1f8ee9SDavid Greenman 1121f6b04d2bSDavid Greenman /* 11220d94caffSDavid Greenman * make sure that we have swap space -- if we are low on memory and 11230d94caffSDavid Greenman * swap -- then kill the biggest process. 11245663e6deSDavid Greenman */ 1125936524aaSMatthew Dillon if ((vm_swap_size < 64 || swap_pager_full) && vm_page_count_min()) { 11265663e6deSDavid Greenman bigproc = NULL; 11275663e6deSDavid Greenman bigsize = 0; 11281005a129SJohn Baldwin sx_slock(&allproc_lock); 11298606d880SJohn Baldwin LIST_FOREACH(p, &allproc, p_list) { 11305663e6deSDavid Greenman /* 11315663e6deSDavid Greenman * if this is a system process, skip it 11325663e6deSDavid Greenman */ 11338606d880SJohn Baldwin PROC_LOCK(p); 1134c8da68e9SPeter Wemm if ((p->p_flag & P_SYSTEM) || (p->p_lock > 0) || 1135c8da68e9SPeter Wemm (p->p_pid == 1) || 113679221631SDavid Greenman ((p->p_pid < 48) && (vm_swap_size != 0))) { 11378606d880SJohn Baldwin PROC_UNLOCK(p); 11385663e6deSDavid Greenman continue; 11395663e6deSDavid Greenman } 11408606d880SJohn Baldwin PROC_UNLOCK(p); 11415663e6deSDavid Greenman /* 11425663e6deSDavid Greenman * if the process is in a non-running type state, 11435663e6deSDavid Greenman * don't touch it. 11445663e6deSDavid Greenman */ 11459ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 11465663e6deSDavid Greenman if (p->p_stat != SRUN && p->p_stat != SSLEEP) { 11479ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 11485663e6deSDavid Greenman continue; 11495663e6deSDavid Greenman } 11509ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 11515663e6deSDavid Greenman /* 11525663e6deSDavid Greenman * get the process size 11535663e6deSDavid Greenman */ 1154b1028ad1SLuoqi Chen size = vmspace_resident_count(p->p_vmspace); 11555663e6deSDavid Greenman /* 11565663e6deSDavid Greenman * if the this process is bigger than the biggest one 11575663e6deSDavid Greenman * remember it. 11585663e6deSDavid Greenman */ 11595663e6deSDavid Greenman if (size > bigsize) { 11605663e6deSDavid Greenman bigproc = p; 11615663e6deSDavid Greenman bigsize = size; 11625663e6deSDavid Greenman } 11635663e6deSDavid Greenman } 11641005a129SJohn Baldwin sx_sunlock(&allproc_lock); 11655663e6deSDavid Greenman if (bigproc != NULL) { 1166729b1e51SDavid Greenman killproc(bigproc, "out of swap space"); 11679ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 11685663e6deSDavid Greenman bigproc->p_estcpu = 0; 11695663e6deSDavid Greenman bigproc->p_nice = PRIO_MIN; 11705663e6deSDavid Greenman resetpriority(bigproc); 11719ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 117224a1cce3SDavid Greenman wakeup(&cnt.v_free_count); 11735663e6deSDavid Greenman } 11745663e6deSDavid Greenman } 117526f9a767SRodney W. Grimes } 117626f9a767SRodney W. Grimes 1177dc2efb27SJohn Dyson /* 1178dc2efb27SJohn Dyson * This routine tries to maintain the pseudo LRU active queue, 1179dc2efb27SJohn Dyson * so that during long periods of time where there is no paging, 1180956f3135SPhilippe Charnier * that some statistic accumulation still occurs. This code 1181dc2efb27SJohn Dyson * helps the situation where paging just starts to occur. 1182dc2efb27SJohn Dyson */ 1183dc2efb27SJohn Dyson static void 1184dc2efb27SJohn Dyson vm_pageout_page_stats() 1185dc2efb27SJohn Dyson { 1186dc2efb27SJohn Dyson int s; 1187dc2efb27SJohn Dyson vm_page_t m,next; 1188dc2efb27SJohn Dyson int pcount,tpcount; /* Number of pages to check */ 1189dc2efb27SJohn Dyson static int fullintervalcount = 0; 1190bef608bdSJohn Dyson int page_shortage; 119125db2c54SMatthew Dillon int s0; 1192bef608bdSJohn Dyson 119390ecac61SMatthew Dillon page_shortage = 119490ecac61SMatthew Dillon (cnt.v_inactive_target + cnt.v_cache_max + cnt.v_free_min) - 1195bef608bdSJohn Dyson (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count); 119690ecac61SMatthew Dillon 1197bef608bdSJohn Dyson if (page_shortage <= 0) 1198bef608bdSJohn Dyson return; 1199dc2efb27SJohn Dyson 120025db2c54SMatthew Dillon s0 = splvm(); 120125db2c54SMatthew Dillon 1202dc2efb27SJohn Dyson pcount = cnt.v_active_count; 1203dc2efb27SJohn Dyson fullintervalcount += vm_pageout_stats_interval; 1204dc2efb27SJohn Dyson if (fullintervalcount < vm_pageout_full_stats_interval) { 1205dc2efb27SJohn Dyson tpcount = (vm_pageout_stats_max * cnt.v_active_count) / cnt.v_page_count; 1206dc2efb27SJohn Dyson if (pcount > tpcount) 1207dc2efb27SJohn Dyson pcount = tpcount; 1208883f3caaSMatthew Dillon } else { 1209883f3caaSMatthew Dillon fullintervalcount = 0; 1210dc2efb27SJohn Dyson } 1211dc2efb27SJohn Dyson 1212be72f788SAlan Cox m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl); 1213dc2efb27SJohn Dyson while ((m != NULL) && (pcount-- > 0)) { 12147e006499SJohn Dyson int actcount; 1215dc2efb27SJohn Dyson 1216dc2efb27SJohn Dyson if (m->queue != PQ_ACTIVE) { 1217dc2efb27SJohn Dyson break; 1218dc2efb27SJohn Dyson } 1219dc2efb27SJohn Dyson 1220dc2efb27SJohn Dyson next = TAILQ_NEXT(m, pageq); 1221dc2efb27SJohn Dyson /* 1222dc2efb27SJohn Dyson * Don't deactivate pages that are busy. 1223dc2efb27SJohn Dyson */ 1224dc2efb27SJohn Dyson if ((m->busy != 0) || 1225dc2efb27SJohn Dyson (m->flags & PG_BUSY) || 1226dc2efb27SJohn Dyson (m->hold_count != 0)) { 1227dc2efb27SJohn Dyson s = splvm(); 1228be72f788SAlan Cox TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1229be72f788SAlan Cox TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1230dc2efb27SJohn Dyson splx(s); 1231dc2efb27SJohn Dyson m = next; 1232dc2efb27SJohn Dyson continue; 1233dc2efb27SJohn Dyson } 1234dc2efb27SJohn Dyson 12357e006499SJohn Dyson actcount = 0; 1236dc2efb27SJohn Dyson if (m->flags & PG_REFERENCED) { 1237e69763a3SDoug Rabson vm_page_flag_clear(m, PG_REFERENCED); 12387e006499SJohn Dyson actcount += 1; 1239dc2efb27SJohn Dyson } 1240dc2efb27SJohn Dyson 12410385347cSPeter Wemm actcount += pmap_ts_referenced(m); 12427e006499SJohn Dyson if (actcount) { 12437e006499SJohn Dyson m->act_count += ACT_ADVANCE + actcount; 1244dc2efb27SJohn Dyson if (m->act_count > ACT_MAX) 1245dc2efb27SJohn Dyson m->act_count = ACT_MAX; 1246dc2efb27SJohn Dyson s = splvm(); 1247be72f788SAlan Cox TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1248be72f788SAlan Cox TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1249dc2efb27SJohn Dyson splx(s); 1250dc2efb27SJohn Dyson } else { 1251dc2efb27SJohn Dyson if (m->act_count == 0) { 12527e006499SJohn Dyson /* 12532b6b0df7SMatthew Dillon * We turn off page access, so that we have 12542b6b0df7SMatthew Dillon * more accurate RSS stats. We don't do this 12552b6b0df7SMatthew Dillon * in the normal page deactivation when the 12562b6b0df7SMatthew Dillon * system is loaded VM wise, because the 12572b6b0df7SMatthew Dillon * cost of the large number of page protect 12582b6b0df7SMatthew Dillon * operations would be higher than the value 12592b6b0df7SMatthew Dillon * of doing the operation. 12607e006499SJohn Dyson */ 1261dc2efb27SJohn Dyson vm_page_protect(m, VM_PROT_NONE); 1262dc2efb27SJohn Dyson vm_page_deactivate(m); 1263dc2efb27SJohn Dyson } else { 1264dc2efb27SJohn Dyson m->act_count -= min(m->act_count, ACT_DECLINE); 1265dc2efb27SJohn Dyson s = splvm(); 1266be72f788SAlan Cox TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1267be72f788SAlan Cox TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1268dc2efb27SJohn Dyson splx(s); 1269dc2efb27SJohn Dyson } 1270dc2efb27SJohn Dyson } 1271dc2efb27SJohn Dyson 1272dc2efb27SJohn Dyson m = next; 1273dc2efb27SJohn Dyson } 127425db2c54SMatthew Dillon splx(s0); 1275dc2efb27SJohn Dyson } 1276dc2efb27SJohn Dyson 1277b182ec9eSJohn Dyson static int 1278b182ec9eSJohn Dyson vm_pageout_free_page_calc(count) 1279b182ec9eSJohn Dyson vm_size_t count; 1280b182ec9eSJohn Dyson { 1281b182ec9eSJohn Dyson if (count < cnt.v_page_count) 1282b182ec9eSJohn Dyson return 0; 1283b182ec9eSJohn Dyson /* 1284b182ec9eSJohn Dyson * free_reserved needs to include enough for the largest swap pager 1285b182ec9eSJohn Dyson * structures plus enough for any pv_entry structs when paging. 1286b182ec9eSJohn Dyson */ 1287b182ec9eSJohn Dyson if (cnt.v_page_count > 1024) 1288b182ec9eSJohn Dyson cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200; 1289b182ec9eSJohn Dyson else 1290b182ec9eSJohn Dyson cnt.v_free_min = 4; 1291f35329acSJohn Dyson cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE + 1292f35329acSJohn Dyson cnt.v_interrupt_free_min; 1293f35329acSJohn Dyson cnt.v_free_reserved = vm_pageout_page_count + 1294a15403deSJohn Dyson cnt.v_pageout_free_min + (count / 768) + PQ_L2_SIZE; 129590ecac61SMatthew Dillon cnt.v_free_severe = cnt.v_free_min / 2; 1296a2f4a846SJohn Dyson cnt.v_free_min += cnt.v_free_reserved; 129790ecac61SMatthew Dillon cnt.v_free_severe += cnt.v_free_reserved; 1298b182ec9eSJohn Dyson return 1; 1299b182ec9eSJohn Dyson } 1300b182ec9eSJohn Dyson 1301b182ec9eSJohn Dyson 1302df8bae1dSRodney W. Grimes /* 1303df8bae1dSRodney W. Grimes * vm_pageout is the high level pageout daemon. 1304df8bae1dSRodney W. Grimes */ 13052b14f991SJulian Elischer static void 130626f9a767SRodney W. Grimes vm_pageout() 1307df8bae1dSRodney W. Grimes { 13082b6b0df7SMatthew Dillon int pass; 13090384fff8SJason Evans 13109ed346baSBosko Milekic mtx_lock(&Giant); 13110384fff8SJason Evans 1312df8bae1dSRodney W. Grimes /* 1313df8bae1dSRodney W. Grimes * Initialize some paging parameters. 1314df8bae1dSRodney W. Grimes */ 1315df8bae1dSRodney W. Grimes 1316f6b04d2bSDavid Greenman cnt.v_interrupt_free_min = 2; 1317f35329acSJohn Dyson if (cnt.v_page_count < 2000) 1318f35329acSJohn Dyson vm_pageout_page_count = 8; 1319f6b04d2bSDavid Greenman 1320b182ec9eSJohn Dyson vm_pageout_free_page_calc(cnt.v_page_count); 1321ed74321bSDavid Greenman /* 13222b6b0df7SMatthew Dillon * v_free_target and v_cache_min control pageout hysteresis. Note 13232b6b0df7SMatthew Dillon * that these are more a measure of the VM cache queue hysteresis 13242b6b0df7SMatthew Dillon * then the VM free queue. Specifically, v_free_target is the 13252b6b0df7SMatthew Dillon * high water mark (free+cache pages). 13262b6b0df7SMatthew Dillon * 13272b6b0df7SMatthew Dillon * v_free_reserved + v_cache_min (mostly means v_cache_min) is the 13282b6b0df7SMatthew Dillon * low water mark, while v_free_min is the stop. v_cache_min must 13292b6b0df7SMatthew Dillon * be big enough to handle memory needs while the pageout daemon 13302b6b0df7SMatthew Dillon * is signalled and run to free more pages. 1331ed74321bSDavid Greenman */ 1332a15403deSJohn Dyson if (cnt.v_free_count > 6144) 13332b6b0df7SMatthew Dillon cnt.v_free_target = 4 * cnt.v_free_min + cnt.v_free_reserved; 1334a15403deSJohn Dyson else 1335a15403deSJohn Dyson cnt.v_free_target = 2 * cnt.v_free_min + cnt.v_free_reserved; 13366f2b142eSDavid Greenman 1337a15403deSJohn Dyson if (cnt.v_free_count > 2048) { 1338a15403deSJohn Dyson cnt.v_cache_min = cnt.v_free_target; 1339a15403deSJohn Dyson cnt.v_cache_max = 2 * cnt.v_cache_min; 1340a15403deSJohn Dyson cnt.v_inactive_target = (3 * cnt.v_free_target) / 2; 13410d94caffSDavid Greenman } else { 13420d94caffSDavid Greenman cnt.v_cache_min = 0; 13430d94caffSDavid Greenman cnt.v_cache_max = 0; 13446f2b142eSDavid Greenman cnt.v_inactive_target = cnt.v_free_count / 4; 13450d94caffSDavid Greenman } 1346e47ed70bSJohn Dyson if (cnt.v_inactive_target > cnt.v_free_count / 3) 1347e47ed70bSJohn Dyson cnt.v_inactive_target = cnt.v_free_count / 3; 1348df8bae1dSRodney W. Grimes 1349df8bae1dSRodney W. Grimes /* XXX does not really belong here */ 1350df8bae1dSRodney W. Grimes if (vm_page_max_wired == 0) 1351df8bae1dSRodney W. Grimes vm_page_max_wired = cnt.v_free_count / 3; 1352df8bae1dSRodney W. Grimes 1353dc2efb27SJohn Dyson if (vm_pageout_stats_max == 0) 1354dc2efb27SJohn Dyson vm_pageout_stats_max = cnt.v_free_target; 1355dc2efb27SJohn Dyson 1356dc2efb27SJohn Dyson /* 1357dc2efb27SJohn Dyson * Set interval in seconds for stats scan. 1358dc2efb27SJohn Dyson */ 1359dc2efb27SJohn Dyson if (vm_pageout_stats_interval == 0) 1360bef608bdSJohn Dyson vm_pageout_stats_interval = 5; 1361dc2efb27SJohn Dyson if (vm_pageout_full_stats_interval == 0) 1362dc2efb27SJohn Dyson vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4; 1363dc2efb27SJohn Dyson 1364dc2efb27SJohn Dyson 1365dc2efb27SJohn Dyson /* 1366dc2efb27SJohn Dyson * Set maximum free per pass 1367dc2efb27SJohn Dyson */ 1368dc2efb27SJohn Dyson if (vm_pageout_stats_free_max == 0) 1369bef608bdSJohn Dyson vm_pageout_stats_free_max = 5; 1370dc2efb27SJohn Dyson 1371e929c00dSKirk McKusick curproc->p_flag |= P_BUFEXHAUST; 137224a1cce3SDavid Greenman swap_pager_swap_init(); 13732b6b0df7SMatthew Dillon pass = 0; 1374df8bae1dSRodney W. Grimes /* 13750d94caffSDavid Greenman * The pageout daemon is never done, so loop forever. 1376df8bae1dSRodney W. Grimes */ 1377df8bae1dSRodney W. Grimes while (TRUE) { 1378dc2efb27SJohn Dyson int error; 1379b18bfc3dSJohn Dyson int s = splvm(); 138090ecac61SMatthew Dillon 1381936524aaSMatthew Dillon /* 1382936524aaSMatthew Dillon * If we have enough free memory, wakeup waiters. Do 1383936524aaSMatthew Dillon * not clear vm_pages_needed until we reach our target, 1384936524aaSMatthew Dillon * otherwise we may be woken up over and over again and 1385936524aaSMatthew Dillon * waste a lot of cpu. 1386936524aaSMatthew Dillon */ 1387936524aaSMatthew Dillon if (vm_pages_needed && !vm_page_count_min()) { 1388936524aaSMatthew Dillon if (vm_paging_needed() <= 0) 1389936524aaSMatthew Dillon vm_pages_needed = 0; 1390936524aaSMatthew Dillon wakeup(&cnt.v_free_count); 1391936524aaSMatthew Dillon } 1392936524aaSMatthew Dillon if (vm_pages_needed) { 139390ecac61SMatthew Dillon /* 13942b6b0df7SMatthew Dillon * Still not done, take a second pass without waiting 13952b6b0df7SMatthew Dillon * (unlimited dirty cleaning), otherwise sleep a bit 13962b6b0df7SMatthew Dillon * and try again. 139790ecac61SMatthew Dillon */ 13982b6b0df7SMatthew Dillon ++pass; 13992b6b0df7SMatthew Dillon if (pass > 1) 140090ecac61SMatthew Dillon tsleep(&vm_pages_needed, PVM, "psleep", hz/2); 140190ecac61SMatthew Dillon } else { 140290ecac61SMatthew Dillon /* 14032b6b0df7SMatthew Dillon * Good enough, sleep & handle stats. Prime the pass 14042b6b0df7SMatthew Dillon * for the next run. 140590ecac61SMatthew Dillon */ 14062b6b0df7SMatthew Dillon if (pass > 1) 14072b6b0df7SMatthew Dillon pass = 1; 14082b6b0df7SMatthew Dillon else 14092b6b0df7SMatthew Dillon pass = 0; 1410dc2efb27SJohn Dyson error = tsleep(&vm_pages_needed, 1411dc2efb27SJohn Dyson PVM, "psleep", vm_pageout_stats_interval * hz); 1412dc2efb27SJohn Dyson if (error && !vm_pages_needed) { 1413dc2efb27SJohn Dyson splx(s); 14142b6b0df7SMatthew Dillon pass = 0; 1415dc2efb27SJohn Dyson vm_pageout_page_stats(); 1416dc2efb27SJohn Dyson continue; 1417dc2efb27SJohn Dyson } 1418f919ebdeSDavid Greenman } 1419e47ed70bSJohn Dyson 1420b18bfc3dSJohn Dyson if (vm_pages_needed) 1421b18bfc3dSJohn Dyson cnt.v_pdwakeups++; 1422f919ebdeSDavid Greenman splx(s); 14232b6b0df7SMatthew Dillon vm_pageout_scan(pass); 14242d8acc0fSJohn Dyson vm_pageout_deficit = 0; 1425df8bae1dSRodney W. Grimes } 1426df8bae1dSRodney W. Grimes } 142726f9a767SRodney W. Grimes 1428e0c5a895SJohn Dyson void 1429e0c5a895SJohn Dyson pagedaemon_wakeup() 1430e0c5a895SJohn Dyson { 1431e0c5a895SJohn Dyson if (!vm_pages_needed && curproc != pageproc) { 1432e0c5a895SJohn Dyson vm_pages_needed++; 1433e0c5a895SJohn Dyson wakeup(&vm_pages_needed); 1434e0c5a895SJohn Dyson } 1435e0c5a895SJohn Dyson } 1436e0c5a895SJohn Dyson 143738efa82bSJohn Dyson #if !defined(NO_SWAPPING) 14385afce282SDavid Greenman static void 14395afce282SDavid Greenman vm_req_vmdaemon() 14405afce282SDavid Greenman { 14415afce282SDavid Greenman static int lastrun = 0; 14425afce282SDavid Greenman 1443b18bfc3dSJohn Dyson if ((ticks > (lastrun + hz)) || (ticks < lastrun)) { 14445afce282SDavid Greenman wakeup(&vm_daemon_needed); 14455afce282SDavid Greenman lastrun = ticks; 14465afce282SDavid Greenman } 14475afce282SDavid Greenman } 14485afce282SDavid Greenman 14492b14f991SJulian Elischer static void 14504f9fb771SBruce Evans vm_daemon() 14510d94caffSDavid Greenman { 14522fe6e4d7SDavid Greenman struct proc *p; 14530d94caffSDavid Greenman 14549ed346baSBosko Milekic mtx_lock(&Giant); 14550384fff8SJason Evans 14562fe6e4d7SDavid Greenman while (TRUE) { 1457e8f36785SJohn Dyson tsleep(&vm_daemon_needed, PPAUSE, "psleep", 0); 14584c1f8ee9SDavid Greenman if (vm_pageout_req_swapout) { 1459ceb0cf87SJohn Dyson swapout_procs(vm_pageout_req_swapout); 14604c1f8ee9SDavid Greenman vm_pageout_req_swapout = 0; 14614c1f8ee9SDavid Greenman } 14622fe6e4d7SDavid Greenman /* 14630d94caffSDavid Greenman * scan the processes for exceeding their rlimits or if 14640d94caffSDavid Greenman * process is swapped out -- deactivate pages 14652fe6e4d7SDavid Greenman */ 14662fe6e4d7SDavid Greenman 14671005a129SJohn Baldwin sx_slock(&allproc_lock); 1468fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(p, &allproc, p_list) { 1469fe2144fdSLuoqi Chen vm_pindex_t limit, size; 14702fe6e4d7SDavid Greenman 14712fe6e4d7SDavid Greenman /* 14722fe6e4d7SDavid Greenman * if this is a system process or if we have already 14732fe6e4d7SDavid Greenman * looked at this process, skip it. 14742fe6e4d7SDavid Greenman */ 14752fe6e4d7SDavid Greenman if (p->p_flag & (P_SYSTEM | P_WEXIT)) { 14762fe6e4d7SDavid Greenman continue; 14772fe6e4d7SDavid Greenman } 14782fe6e4d7SDavid Greenman /* 14792fe6e4d7SDavid Greenman * if the process is in a non-running type state, 14802fe6e4d7SDavid Greenman * don't touch it. 14812fe6e4d7SDavid Greenman */ 14829ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 14832fe6e4d7SDavid Greenman if (p->p_stat != SRUN && p->p_stat != SSLEEP) { 14849ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 14852fe6e4d7SDavid Greenman continue; 14862fe6e4d7SDavid Greenman } 14872fe6e4d7SDavid Greenman /* 14882fe6e4d7SDavid Greenman * get a limit 14892fe6e4d7SDavid Greenman */ 1490fe2144fdSLuoqi Chen limit = OFF_TO_IDX( 1491fe2144fdSLuoqi Chen qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur, 1492fe2144fdSLuoqi Chen p->p_rlimit[RLIMIT_RSS].rlim_max)); 14932fe6e4d7SDavid Greenman 14942fe6e4d7SDavid Greenman /* 14950d94caffSDavid Greenman * let processes that are swapped out really be 14960d94caffSDavid Greenman * swapped out set the limit to nothing (will force a 14970d94caffSDavid Greenman * swap-out.) 14982fe6e4d7SDavid Greenman */ 14998606d880SJohn Baldwin if ((p->p_sflag & PS_INMEM) == 0) 15000d94caffSDavid Greenman limit = 0; /* XXX */ 15019ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 15022fe6e4d7SDavid Greenman 1503fe2144fdSLuoqi Chen size = vmspace_resident_count(p->p_vmspace); 15042fe6e4d7SDavid Greenman if (limit >= 0 && size >= limit) { 1505fe2144fdSLuoqi Chen vm_pageout_map_deactivate_pages( 1506fe2144fdSLuoqi Chen &p->p_vmspace->vm_map, limit); 15072fe6e4d7SDavid Greenman } 15082fe6e4d7SDavid Greenman } 15091005a129SJohn Baldwin sx_sunlock(&allproc_lock); 151024a1cce3SDavid Greenman } 15112fe6e4d7SDavid Greenman } 151238efa82bSJohn Dyson #endif 1513