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 21df8bae1dSRodney W. Grimes * 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> 8126f9a767SRodney W. Grimes #include <sys/resourcevar.h> 82d2fc5315SPoul-Henning Kamp #include <sys/signalvar.h> 83f6b04d2bSDavid Greenman #include <sys/vnode.h> 84efeaf95aSDavid Greenman #include <sys/vmmeter.h> 8538efa82bSJohn Dyson #include <sys/sysctl.h> 86df8bae1dSRodney W. Grimes 87df8bae1dSRodney W. Grimes #include <vm/vm.h> 88efeaf95aSDavid Greenman #include <vm/vm_param.h> 89efeaf95aSDavid Greenman #include <vm/vm_prot.h> 90996c772fSJohn Dyson #include <sys/lock.h> 91efeaf95aSDavid Greenman #include <vm/vm_object.h> 92df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 93efeaf95aSDavid Greenman #include <vm/vm_map.h> 94df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h> 9524a1cce3SDavid Greenman #include <vm/vm_pager.h> 9605f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h> 97efeaf95aSDavid Greenman #include <vm/vm_extern.h> 98df8bae1dSRodney W. Grimes 992b14f991SJulian Elischer /* 1002b14f991SJulian Elischer * System initialization 1012b14f991SJulian Elischer */ 1022b14f991SJulian Elischer 1032b14f991SJulian Elischer /* the kernel process "vm_pageout"*/ 1042b14f991SJulian Elischer static void vm_pageout __P((void)); 1058f9110f6SJohn Dyson static int vm_pageout_clean __P((vm_page_t)); 1063af76890SPoul-Henning Kamp static int vm_pageout_scan __P((void)); 107f35329acSJohn Dyson static int vm_pageout_free_page_calc __P((vm_size_t count)); 1082b14f991SJulian Elischer struct proc *pageproc; 1092b14f991SJulian Elischer 1102b14f991SJulian Elischer static struct kproc_desc page_kp = { 1112b14f991SJulian Elischer "pagedaemon", 1122b14f991SJulian Elischer vm_pageout, 1132b14f991SJulian Elischer &pageproc 1142b14f991SJulian Elischer }; 1159c8b8baaSPeter Wemm SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, &page_kp) 1162b14f991SJulian Elischer 11738efa82bSJohn Dyson #if !defined(NO_SWAPPING) 1182b14f991SJulian Elischer /* the kernel process "vm_daemon"*/ 1192b14f991SJulian Elischer static void vm_daemon __P((void)); 120f708ef1bSPoul-Henning Kamp static struct proc *vmproc; 1212b14f991SJulian Elischer 1222b14f991SJulian Elischer static struct kproc_desc vm_kp = { 1232b14f991SJulian Elischer "vmdaemon", 1242b14f991SJulian Elischer vm_daemon, 1252b14f991SJulian Elischer &vmproc 1262b14f991SJulian Elischer }; 1279c8b8baaSPeter Wemm SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp) 12838efa82bSJohn Dyson #endif 1292b14f991SJulian Elischer 1302b14f991SJulian Elischer 1312d8acc0fSJohn Dyson int vm_pages_needed=0; /* Event on which pageout daemon sleeps */ 1322d8acc0fSJohn Dyson int vm_pageout_deficit=0; /* Estimated number of pages deficit */ 1332d8acc0fSJohn Dyson int vm_pageout_pages_needed=0; /* flag saying that the pageout daemon needs pages */ 13426f9a767SRodney W. Grimes 13538efa82bSJohn Dyson #if !defined(NO_SWAPPING) 136f708ef1bSPoul-Henning Kamp static int vm_pageout_req_swapout; /* XXX */ 137f708ef1bSPoul-Henning Kamp static int vm_daemon_needed; 13838efa82bSJohn Dyson #endif 1395663e6deSDavid Greenman extern int vm_swap_size; 140303b270bSEivind Eklund static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0; 141303b270bSEivind Eklund static int vm_pageout_full_stats_interval = 0; 142303b270bSEivind Eklund static int vm_pageout_stats_free_max=0, vm_pageout_algorithm_lru=0; 143303b270bSEivind Eklund static int defer_swap_pageouts=0; 144303b270bSEivind Eklund static int disable_swap_pageouts=0; 14570111b90SJohn Dyson 146303b270bSEivind Eklund static int max_page_launder=100; 14738efa82bSJohn Dyson #if defined(NO_SWAPPING) 148303b270bSEivind Eklund static int vm_swap_enabled=0; 149303b270bSEivind Eklund static int vm_swap_idle_enabled=0; 15038efa82bSJohn Dyson #else 151303b270bSEivind Eklund static int vm_swap_enabled=1; 152303b270bSEivind Eklund static int vm_swap_idle_enabled=0; 15338efa82bSJohn Dyson #endif 15438efa82bSJohn Dyson 15538efa82bSJohn Dyson SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm, 156b0359e2cSPeter Wemm CTLFLAG_RW, &vm_pageout_algorithm_lru, 0, "LRU page mgmt"); 15738efa82bSJohn Dyson 158dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max, 159b0359e2cSPeter Wemm CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length"); 160dc2efb27SJohn Dyson 161dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval, 162b0359e2cSPeter Wemm CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan"); 163dc2efb27SJohn Dyson 164dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval, 165b0359e2cSPeter Wemm CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan"); 166dc2efb27SJohn Dyson 167dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_free_max, 168b0359e2cSPeter Wemm CTLFLAG_RW, &vm_pageout_stats_free_max, 0, "Not implemented"); 169dc2efb27SJohn Dyson 17038efa82bSJohn Dyson #if defined(NO_SWAPPING) 171ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 172ceb0cf87SJohn Dyson CTLFLAG_RD, &vm_swap_enabled, 0, ""); 173ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 174ceb0cf87SJohn Dyson CTLFLAG_RD, &vm_swap_idle_enabled, 0, ""); 17538efa82bSJohn Dyson #else 176ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 177b0359e2cSPeter Wemm CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout"); 178ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 179b0359e2cSPeter Wemm CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria"); 18038efa82bSJohn Dyson #endif 18126f9a767SRodney W. Grimes 182ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts, 183b0359e2cSPeter Wemm CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem"); 18412ac6a1dSJohn Dyson 185ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts, 186b0359e2cSPeter Wemm CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages"); 18712ac6a1dSJohn Dyson 188ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, max_page_launder, 189b0359e2cSPeter Wemm CTLFLAG_RW, &max_page_launder, 0, "Maximum number of pages to clean per pass"); 19070111b90SJohn Dyson 19126f9a767SRodney W. Grimes 192ffc82b0aSJohn Dyson #define VM_PAGEOUT_PAGE_COUNT 16 193bbc0ec52SDavid Greenman int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT; 194df8bae1dSRodney W. Grimes 195c3cb3e12SDavid Greenman int vm_page_max_wired; /* XXX max # of wired pages system-wide */ 196df8bae1dSRodney W. Grimes 19738efa82bSJohn Dyson #if !defined(NO_SWAPPING) 19838efa82bSJohn Dyson typedef void freeer_fcn_t __P((vm_map_t, vm_object_t, vm_pindex_t, int)); 19938efa82bSJohn Dyson static void vm_pageout_map_deactivate_pages __P((vm_map_t, vm_pindex_t)); 200cd41fc12SDavid Greenman static freeer_fcn_t vm_pageout_object_deactivate_pages; 201cd41fc12SDavid Greenman static void vm_req_vmdaemon __P((void)); 20238efa82bSJohn Dyson #endif 203dc2efb27SJohn Dyson static void vm_pageout_page_stats(void); 204cd41fc12SDavid Greenman 20526f9a767SRodney W. Grimes /* 20626f9a767SRodney W. Grimes * vm_pageout_clean: 20724a1cce3SDavid Greenman * 2080d94caffSDavid Greenman * Clean the page and remove it from the laundry. 20926f9a767SRodney W. Grimes * 2100d94caffSDavid Greenman * We set the busy bit to cause potential page faults on this page to 2111c7c3c6aSMatthew Dillon * block. Note the careful timing, however, the busy bit isn't set till 2121c7c3c6aSMatthew Dillon * late and we cannot do anything that will mess with the page. 21326f9a767SRodney W. Grimes */ 2141c7c3c6aSMatthew Dillon 2153af76890SPoul-Henning Kamp static int 2168f9110f6SJohn Dyson vm_pageout_clean(m) 21724a1cce3SDavid Greenman vm_page_t m; 21824a1cce3SDavid Greenman { 21926f9a767SRodney W. Grimes register vm_object_t object; 220f35329acSJohn Dyson vm_page_t mc[2*vm_pageout_page_count]; 22124a1cce3SDavid Greenman int pageout_count; 22290ecac61SMatthew Dillon int ib, is, page_base; 223a316d390SJohn Dyson vm_pindex_t pindex = m->pindex; 22426f9a767SRodney W. Grimes 22526f9a767SRodney W. Grimes object = m->object; 22624a1cce3SDavid Greenman 22726f9a767SRodney W. Grimes /* 2281c7c3c6aSMatthew Dillon * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP 2291c7c3c6aSMatthew Dillon * with the new swapper, but we could have serious problems paging 2301c7c3c6aSMatthew Dillon * out other object types if there is insufficient memory. 2311c7c3c6aSMatthew Dillon * 2321c7c3c6aSMatthew Dillon * Unfortunately, checking free memory here is far too late, so the 2331c7c3c6aSMatthew Dillon * check has been moved up a procedural level. 2341c7c3c6aSMatthew Dillon */ 2351c7c3c6aSMatthew Dillon 23624a1cce3SDavid Greenman /* 23724a1cce3SDavid Greenman * Don't mess with the page if it's busy. 23824a1cce3SDavid Greenman */ 2398f9110f6SJohn Dyson if ((m->hold_count != 0) || 2400d94caffSDavid Greenman ((m->busy != 0) || (m->flags & PG_BUSY))) 2410d94caffSDavid Greenman return 0; 2420d94caffSDavid Greenman 243f35329acSJohn Dyson mc[vm_pageout_page_count] = m; 24426f9a767SRodney W. Grimes pageout_count = 1; 245f35329acSJohn Dyson page_base = vm_pageout_page_count; 24690ecac61SMatthew Dillon ib = 1; 24790ecac61SMatthew Dillon is = 1; 24890ecac61SMatthew Dillon 24924a1cce3SDavid Greenman /* 25024a1cce3SDavid Greenman * Scan object for clusterable pages. 25124a1cce3SDavid Greenman * 25224a1cce3SDavid Greenman * We can cluster ONLY if: ->> the page is NOT 25324a1cce3SDavid Greenman * clean, wired, busy, held, or mapped into a 25424a1cce3SDavid Greenman * buffer, and one of the following: 25524a1cce3SDavid Greenman * 1) The page is inactive, or a seldom used 25624a1cce3SDavid Greenman * active page. 25724a1cce3SDavid Greenman * -or- 25824a1cce3SDavid Greenman * 2) we force the issue. 25990ecac61SMatthew Dillon * 26090ecac61SMatthew Dillon * During heavy mmap/modification loads the pageout 26190ecac61SMatthew Dillon * daemon can really fragment the underlying file 26290ecac61SMatthew Dillon * due to flushing pages out of order and not trying 26390ecac61SMatthew Dillon * align the clusters (which leave sporatic out-of-order 26490ecac61SMatthew Dillon * holes). To solve this problem we do the reverse scan 26590ecac61SMatthew Dillon * first and attempt to align our cluster, then do a 26690ecac61SMatthew Dillon * forward scan if room remains. 26724a1cce3SDavid Greenman */ 26890ecac61SMatthew Dillon 26990ecac61SMatthew Dillon more: 27090ecac61SMatthew Dillon while (ib && pageout_count < vm_pageout_page_count) { 27124a1cce3SDavid Greenman vm_page_t p; 272f6b04d2bSDavid Greenman 27390ecac61SMatthew Dillon if (ib > pindex) { 27490ecac61SMatthew Dillon ib = 0; 27590ecac61SMatthew Dillon break; 276f6b04d2bSDavid Greenman } 27790ecac61SMatthew Dillon 27890ecac61SMatthew Dillon if ((p = vm_page_lookup(object, pindex - ib)) == NULL) { 27990ecac61SMatthew Dillon ib = 0; 28090ecac61SMatthew Dillon break; 28190ecac61SMatthew Dillon } 2825070c7f8SJohn Dyson if (((p->queue - p->pc) == PQ_CACHE) || 2835070c7f8SJohn Dyson (p->flags & PG_BUSY) || p->busy) { 28490ecac61SMatthew Dillon ib = 0; 28590ecac61SMatthew Dillon break; 286f6b04d2bSDavid Greenman } 28724a1cce3SDavid Greenman vm_page_test_dirty(p); 28890ecac61SMatthew Dillon if ((p->dirty & p->valid) == 0 || 28990ecac61SMatthew Dillon p->queue != PQ_INACTIVE || 29090ecac61SMatthew Dillon p->wire_count != 0 || 29190ecac61SMatthew Dillon p->hold_count != 0) { 29290ecac61SMatthew Dillon ib = 0; 29324a1cce3SDavid Greenman break; 294f6b04d2bSDavid Greenman } 29590ecac61SMatthew Dillon mc[--page_base] = p; 29690ecac61SMatthew Dillon ++pageout_count; 29790ecac61SMatthew Dillon ++ib; 29824a1cce3SDavid Greenman /* 29990ecac61SMatthew Dillon * alignment boundry, stop here and switch directions. Do 30090ecac61SMatthew Dillon * not clear ib. 30124a1cce3SDavid Greenman */ 30290ecac61SMatthew Dillon if ((pindex - (ib - 1)) % vm_pageout_page_count == 0) 30390ecac61SMatthew Dillon break; 30424a1cce3SDavid Greenman } 30590ecac61SMatthew Dillon 30690ecac61SMatthew Dillon while (pageout_count < vm_pageout_page_count && 30790ecac61SMatthew Dillon pindex + is < object->size) { 30890ecac61SMatthew Dillon vm_page_t p; 30990ecac61SMatthew Dillon 31090ecac61SMatthew Dillon if ((p = vm_page_lookup(object, pindex + is)) == NULL) 31190ecac61SMatthew Dillon break; 3125070c7f8SJohn Dyson if (((p->queue - p->pc) == PQ_CACHE) || 3135070c7f8SJohn Dyson (p->flags & PG_BUSY) || p->busy) { 31490ecac61SMatthew Dillon break; 31524a1cce3SDavid Greenman } 31624a1cce3SDavid Greenman vm_page_test_dirty(p); 31790ecac61SMatthew Dillon if ((p->dirty & p->valid) == 0 || 31890ecac61SMatthew Dillon p->queue != PQ_INACTIVE || 31990ecac61SMatthew Dillon p->wire_count != 0 || 32090ecac61SMatthew Dillon p->hold_count != 0) { 32124a1cce3SDavid Greenman break; 32224a1cce3SDavid Greenman } 32390ecac61SMatthew Dillon mc[page_base + pageout_count] = p; 32490ecac61SMatthew Dillon ++pageout_count; 32590ecac61SMatthew Dillon ++is; 32624a1cce3SDavid Greenman } 32790ecac61SMatthew Dillon 32890ecac61SMatthew Dillon /* 32990ecac61SMatthew Dillon * If we exhausted our forward scan, continue with the reverse scan 33090ecac61SMatthew Dillon * when possible, even past a page boundry. This catches boundry 33190ecac61SMatthew Dillon * conditions. 33290ecac61SMatthew Dillon */ 33390ecac61SMatthew Dillon if (ib && pageout_count < vm_pageout_page_count) 33490ecac61SMatthew Dillon goto more; 335f6b04d2bSDavid Greenman 33667bf6868SJohn Dyson /* 33767bf6868SJohn Dyson * we allow reads during pageouts... 33867bf6868SJohn Dyson */ 3398f9110f6SJohn Dyson return vm_pageout_flush(&mc[page_base], pageout_count, 0); 340aef922f5SJohn Dyson } 341aef922f5SJohn Dyson 3421c7c3c6aSMatthew Dillon /* 3431c7c3c6aSMatthew Dillon * vm_pageout_flush() - launder the given pages 3441c7c3c6aSMatthew Dillon * 3451c7c3c6aSMatthew Dillon * The given pages are laundered. Note that we setup for the start of 3461c7c3c6aSMatthew Dillon * I/O ( i.e. busy the page ), mark it read-only, and bump the object 3471c7c3c6aSMatthew Dillon * reference count all in here rather then in the parent. If we want 3481c7c3c6aSMatthew Dillon * the parent to do more sophisticated things we may have to change 3491c7c3c6aSMatthew Dillon * the ordering. 3501c7c3c6aSMatthew Dillon */ 3511c7c3c6aSMatthew Dillon 352aef922f5SJohn Dyson int 3538f9110f6SJohn Dyson vm_pageout_flush(mc, count, flags) 354aef922f5SJohn Dyson vm_page_t *mc; 355aef922f5SJohn Dyson int count; 3568f9110f6SJohn Dyson int flags; 357aef922f5SJohn Dyson { 358aef922f5SJohn Dyson register vm_object_t object; 359aef922f5SJohn Dyson int pageout_status[count]; 36095461b45SJohn Dyson int numpagedout = 0; 361aef922f5SJohn Dyson int i; 362aef922f5SJohn Dyson 3631c7c3c6aSMatthew Dillon /* 3641c7c3c6aSMatthew Dillon * Initiate I/O. Bump the vm_page_t->busy counter and 3651c7c3c6aSMatthew Dillon * mark the pages read-only. 3661c7c3c6aSMatthew Dillon * 3671c7c3c6aSMatthew Dillon * We do not have to fixup the clean/dirty bits here... we can 3681c7c3c6aSMatthew Dillon * allow the pager to do it after the I/O completes. 3691c7c3c6aSMatthew Dillon */ 3701c7c3c6aSMatthew Dillon 3718f9110f6SJohn Dyson for (i = 0; i < count; i++) { 372e69763a3SDoug Rabson vm_page_io_start(mc[i]); 3738f9110f6SJohn Dyson vm_page_protect(mc[i], VM_PROT_READ); 3748f9110f6SJohn Dyson } 3758f9110f6SJohn Dyson 376aef922f5SJohn Dyson object = mc[0]->object; 377d474eaaaSDoug Rabson vm_object_pip_add(object, count); 378aef922f5SJohn Dyson 379aef922f5SJohn Dyson vm_pager_put_pages(object, mc, count, 3808f9110f6SJohn Dyson (flags | ((object == kernel_object) ? OBJPC_SYNC : 0)), 38126f9a767SRodney W. Grimes pageout_status); 38226f9a767SRodney W. Grimes 383aef922f5SJohn Dyson for (i = 0; i < count; i++) { 384aef922f5SJohn Dyson vm_page_t mt = mc[i]; 38524a1cce3SDavid Greenman 38626f9a767SRodney W. Grimes switch (pageout_status[i]) { 38726f9a767SRodney W. Grimes case VM_PAGER_OK: 38895461b45SJohn Dyson numpagedout++; 38926f9a767SRodney W. Grimes break; 39026f9a767SRodney W. Grimes case VM_PAGER_PEND: 39195461b45SJohn Dyson numpagedout++; 39226f9a767SRodney W. Grimes break; 39326f9a767SRodney W. Grimes case VM_PAGER_BAD: 39426f9a767SRodney W. Grimes /* 3950d94caffSDavid Greenman * Page outside of range of object. Right now we 3960d94caffSDavid Greenman * essentially lose the changes by pretending it 3970d94caffSDavid Greenman * worked. 39826f9a767SRodney W. Grimes */ 39967bf6868SJohn Dyson pmap_clear_modify(VM_PAGE_TO_PHYS(mt)); 40090ecac61SMatthew Dillon vm_page_undirty(mt); 40126f9a767SRodney W. Grimes break; 40226f9a767SRodney W. Grimes case VM_PAGER_ERROR: 40326f9a767SRodney W. Grimes case VM_PAGER_FAIL: 40426f9a767SRodney W. Grimes /* 4050d94caffSDavid Greenman * If page couldn't be paged out, then reactivate the 4060d94caffSDavid Greenman * page so it doesn't clog the inactive list. (We 4070d94caffSDavid Greenman * will try paging out it again later). 40826f9a767SRodney W. Grimes */ 40924a1cce3SDavid Greenman vm_page_activate(mt); 41026f9a767SRodney W. Grimes break; 41126f9a767SRodney W. Grimes case VM_PAGER_AGAIN: 41226f9a767SRodney W. Grimes break; 41326f9a767SRodney W. Grimes } 41426f9a767SRodney W. Grimes 41526f9a767SRodney W. Grimes /* 4160d94caffSDavid Greenman * If the operation is still going, leave the page busy to 4170d94caffSDavid Greenman * block all other accesses. Also, leave the paging in 4180d94caffSDavid Greenman * progress indicator set so that we don't attempt an object 4190d94caffSDavid Greenman * collapse. 42026f9a767SRodney W. Grimes */ 42126f9a767SRodney W. Grimes if (pageout_status[i] != VM_PAGER_PEND) { 422f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 423e69763a3SDoug Rabson vm_page_io_finish(mt); 42426f9a767SRodney W. Grimes } 42526f9a767SRodney W. Grimes } 42695461b45SJohn Dyson return numpagedout; 42726f9a767SRodney W. Grimes } 42826f9a767SRodney W. Grimes 42938efa82bSJohn Dyson #if !defined(NO_SWAPPING) 43026f9a767SRodney W. Grimes /* 43126f9a767SRodney W. Grimes * vm_pageout_object_deactivate_pages 43226f9a767SRodney W. Grimes * 43326f9a767SRodney W. Grimes * deactivate enough pages to satisfy the inactive target 43426f9a767SRodney W. Grimes * requirements or if vm_page_proc_limit is set, then 43526f9a767SRodney W. Grimes * deactivate all of the pages in the object and its 43624a1cce3SDavid Greenman * backing_objects. 43726f9a767SRodney W. Grimes * 43826f9a767SRodney W. Grimes * The object and map must be locked. 43926f9a767SRodney W. Grimes */ 44038efa82bSJohn Dyson static void 44138efa82bSJohn Dyson vm_pageout_object_deactivate_pages(map, object, desired, map_remove_only) 44226f9a767SRodney W. Grimes vm_map_t map; 44326f9a767SRodney W. Grimes vm_object_t object; 44438efa82bSJohn Dyson vm_pindex_t desired; 4450d94caffSDavid Greenman int map_remove_only; 44626f9a767SRodney W. Grimes { 44726f9a767SRodney W. Grimes register vm_page_t p, next; 44826f9a767SRodney W. Grimes int rcount; 44938efa82bSJohn Dyson int remove_mode; 4501eeaa1e3SJohn Dyson int s; 45126f9a767SRodney W. Grimes 45224a1cce3SDavid Greenman if (object->type == OBJT_DEVICE) 45338efa82bSJohn Dyson return; 4548f895206SDavid Greenman 45538efa82bSJohn Dyson while (object) { 456b1028ad1SLuoqi Chen if (pmap_resident_count(vm_map_pmap(map)) <= desired) 45738efa82bSJohn Dyson return; 45824a1cce3SDavid Greenman if (object->paging_in_progress) 45938efa82bSJohn Dyson return; 46026f9a767SRodney W. Grimes 46138efa82bSJohn Dyson remove_mode = map_remove_only; 46238efa82bSJohn Dyson if (object->shadow_count > 1) 46338efa82bSJohn Dyson remove_mode = 1; 46426f9a767SRodney W. Grimes /* 46526f9a767SRodney W. Grimes * scan the objects entire memory queue 46626f9a767SRodney W. Grimes */ 46726f9a767SRodney W. Grimes rcount = object->resident_page_count; 468b18bfc3dSJohn Dyson p = TAILQ_FIRST(&object->memq); 46926f9a767SRodney W. Grimes while (p && (rcount-- > 0)) { 4707e006499SJohn Dyson int actcount; 471b1028ad1SLuoqi Chen if (pmap_resident_count(vm_map_pmap(map)) <= desired) 47238efa82bSJohn Dyson return; 473b18bfc3dSJohn Dyson next = TAILQ_NEXT(p, listq); 474a58d1fa1SDavid Greenman cnt.v_pdpages++; 4750d94caffSDavid Greenman if (p->wire_count != 0 || 4760d94caffSDavid Greenman p->hold_count != 0 || 4770d94caffSDavid Greenman p->busy != 0 || 478bd7e5f99SJohn Dyson (p->flags & PG_BUSY) || 4790d94caffSDavid Greenman !pmap_page_exists(vm_map_pmap(map), VM_PAGE_TO_PHYS(p))) { 4800d94caffSDavid Greenman p = next; 4810d94caffSDavid Greenman continue; 4820d94caffSDavid Greenman } 483ef743ce6SJohn Dyson 4847e006499SJohn Dyson actcount = pmap_ts_referenced(VM_PAGE_TO_PHYS(p)); 4857e006499SJohn Dyson if (actcount) { 486e69763a3SDoug Rabson vm_page_flag_set(p, PG_REFERENCED); 487c8c4b40cSJohn Dyson } else if (p->flags & PG_REFERENCED) { 4887e006499SJohn Dyson actcount = 1; 489ef743ce6SJohn Dyson } 490ef743ce6SJohn Dyson 49138efa82bSJohn Dyson if ((p->queue != PQ_ACTIVE) && 49238efa82bSJohn Dyson (p->flags & PG_REFERENCED)) { 493ef743ce6SJohn Dyson vm_page_activate(p); 4947e006499SJohn Dyson p->act_count += actcount; 495e69763a3SDoug Rabson vm_page_flag_clear(p, PG_REFERENCED); 496c8c4b40cSJohn Dyson } else if (p->queue == PQ_ACTIVE) { 497ef743ce6SJohn Dyson if ((p->flags & PG_REFERENCED) == 0) { 498c8c4b40cSJohn Dyson p->act_count -= min(p->act_count, ACT_DECLINE); 499c8c4b40cSJohn Dyson if (!remove_mode && (vm_pageout_algorithm_lru || (p->act_count == 0))) { 500b18bfc3dSJohn Dyson vm_page_protect(p, VM_PROT_NONE); 50126f9a767SRodney W. Grimes vm_page_deactivate(p); 50226f9a767SRodney W. Grimes } else { 503c8c4b40cSJohn Dyson s = splvm(); 504c8c4b40cSJohn Dyson TAILQ_REMOVE(&vm_page_queue_active, p, pageq); 505c8c4b40cSJohn Dyson TAILQ_INSERT_TAIL(&vm_page_queue_active, p, pageq); 506c8c4b40cSJohn Dyson splx(s); 507c8c4b40cSJohn Dyson } 508c8c4b40cSJohn Dyson } else { 509eaf13dd7SJohn Dyson vm_page_activate(p); 510e69763a3SDoug Rabson vm_page_flag_clear(p, PG_REFERENCED); 51138efa82bSJohn Dyson if (p->act_count < (ACT_MAX - ACT_ADVANCE)) 51238efa82bSJohn Dyson p->act_count += ACT_ADVANCE; 5131eeaa1e3SJohn Dyson s = splvm(); 51426f9a767SRodney W. Grimes TAILQ_REMOVE(&vm_page_queue_active, p, pageq); 51526f9a767SRodney W. Grimes TAILQ_INSERT_TAIL(&vm_page_queue_active, p, pageq); 5161eeaa1e3SJohn Dyson splx(s); 51726f9a767SRodney W. Grimes } 518bd7e5f99SJohn Dyson } else if (p->queue == PQ_INACTIVE) { 519f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 52026f9a767SRodney W. Grimes } 52126f9a767SRodney W. Grimes p = next; 52226f9a767SRodney W. Grimes } 52338efa82bSJohn Dyson object = object->backing_object; 52438efa82bSJohn Dyson } 52538efa82bSJohn Dyson return; 52626f9a767SRodney W. Grimes } 52726f9a767SRodney W. Grimes 52826f9a767SRodney W. Grimes /* 52926f9a767SRodney W. Grimes * deactivate some number of pages in a map, try to do it fairly, but 53026f9a767SRodney W. Grimes * that is really hard to do. 53126f9a767SRodney W. Grimes */ 532cd41fc12SDavid Greenman static void 53338efa82bSJohn Dyson vm_pageout_map_deactivate_pages(map, desired) 53426f9a767SRodney W. Grimes vm_map_t map; 53538efa82bSJohn Dyson vm_pindex_t desired; 53626f9a767SRodney W. Grimes { 53726f9a767SRodney W. Grimes vm_map_entry_t tmpe; 53838efa82bSJohn Dyson vm_object_t obj, bigobj; 5390d94caffSDavid Greenman 540996c772fSJohn Dyson if (lockmgr(&map->lock, LK_EXCLUSIVE | LK_NOWAIT, (void *)0, curproc)) { 54126f9a767SRodney W. Grimes return; 54226f9a767SRodney W. Grimes } 54338efa82bSJohn Dyson 54438efa82bSJohn Dyson bigobj = NULL; 54538efa82bSJohn Dyson 54638efa82bSJohn Dyson /* 54738efa82bSJohn Dyson * first, search out the biggest object, and try to free pages from 54838efa82bSJohn Dyson * that. 54938efa82bSJohn Dyson */ 55026f9a767SRodney W. Grimes tmpe = map->header.next; 55138efa82bSJohn Dyson while (tmpe != &map->header) { 5529fdfe602SMatthew Dillon if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 55338efa82bSJohn Dyson obj = tmpe->object.vm_object; 55438efa82bSJohn Dyson if ((obj != NULL) && (obj->shadow_count <= 1) && 55538efa82bSJohn Dyson ((bigobj == NULL) || 55638efa82bSJohn Dyson (bigobj->resident_page_count < obj->resident_page_count))) { 55738efa82bSJohn Dyson bigobj = obj; 55838efa82bSJohn Dyson } 55938efa82bSJohn Dyson } 56038efa82bSJohn Dyson tmpe = tmpe->next; 56138efa82bSJohn Dyson } 56238efa82bSJohn Dyson 56338efa82bSJohn Dyson if (bigobj) 56438efa82bSJohn Dyson vm_pageout_object_deactivate_pages(map, bigobj, desired, 0); 56538efa82bSJohn Dyson 56638efa82bSJohn Dyson /* 56738efa82bSJohn Dyson * Next, hunt around for other pages to deactivate. We actually 56838efa82bSJohn Dyson * do this search sort of wrong -- .text first is not the best idea. 56938efa82bSJohn Dyson */ 57038efa82bSJohn Dyson tmpe = map->header.next; 57138efa82bSJohn Dyson while (tmpe != &map->header) { 572b1028ad1SLuoqi Chen if (pmap_resident_count(vm_map_pmap(map)) <= desired) 57338efa82bSJohn Dyson break; 5749fdfe602SMatthew Dillon if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 57538efa82bSJohn Dyson obj = tmpe->object.vm_object; 57601155bd7SDavid Greenman if (obj) 57738efa82bSJohn Dyson vm_pageout_object_deactivate_pages(map, obj, desired, 0); 57838efa82bSJohn Dyson } 57926f9a767SRodney W. Grimes tmpe = tmpe->next; 58026f9a767SRodney W. Grimes }; 58138efa82bSJohn Dyson 58238efa82bSJohn Dyson /* 58338efa82bSJohn Dyson * Remove all mappings if a process is swapped out, this will free page 58438efa82bSJohn Dyson * table pages. 58538efa82bSJohn Dyson */ 58638efa82bSJohn Dyson if (desired == 0) 58738efa82bSJohn Dyson pmap_remove(vm_map_pmap(map), 58838efa82bSJohn Dyson VM_MIN_ADDRESS, VM_MAXUSER_ADDRESS); 58938efa82bSJohn Dyson vm_map_unlock(map); 59026f9a767SRodney W. Grimes return; 59126f9a767SRodney W. Grimes } 59238efa82bSJohn Dyson #endif 593df8bae1dSRodney W. Grimes 5941c7c3c6aSMatthew Dillon /* 5951c7c3c6aSMatthew Dillon * Don't try to be fancy - being fancy can lead to VOP_LOCK's and therefore 5961c7c3c6aSMatthew Dillon * to vnode deadlocks. We only do it for OBJT_DEFAULT and OBJT_SWAP objects 5971c7c3c6aSMatthew Dillon * which we know can be trivially freed. 5981c7c3c6aSMatthew Dillon */ 5991c7c3c6aSMatthew Dillon 600925a3a41SJohn Dyson void 601925a3a41SJohn Dyson vm_pageout_page_free(vm_page_t m) { 6021c7c3c6aSMatthew Dillon vm_object_t object = m->object; 6031c7c3c6aSMatthew Dillon int type = object->type; 604925a3a41SJohn Dyson 6051c7c3c6aSMatthew Dillon if (type == OBJT_SWAP || type == OBJT_DEFAULT) 6061c7c3c6aSMatthew Dillon vm_object_reference(object); 607e69763a3SDoug Rabson vm_page_busy(m); 608925a3a41SJohn Dyson vm_page_protect(m, VM_PROT_NONE); 609925a3a41SJohn Dyson vm_page_free(m); 6101c7c3c6aSMatthew Dillon if (type == OBJT_SWAP || type == OBJT_DEFAULT) 61147221757SJohn Dyson vm_object_deallocate(object); 612925a3a41SJohn Dyson } 613925a3a41SJohn Dyson 614df8bae1dSRodney W. Grimes /* 615df8bae1dSRodney W. Grimes * vm_pageout_scan does the dirty work for the pageout daemon. 616df8bae1dSRodney W. Grimes */ 6173af76890SPoul-Henning Kamp static int 618df8bae1dSRodney W. Grimes vm_pageout_scan() 619df8bae1dSRodney W. Grimes { 620502ba6e4SJohn Dyson vm_page_t m, next; 6211c7c3c6aSMatthew Dillon int page_shortage, maxscan, pcount; 6221c7c3c6aSMatthew Dillon int addl_page_shortage, addl_page_shortage_init; 62370111b90SJohn Dyson int maxlaunder; 6241c7c3c6aSMatthew Dillon int launder_loop = 0; 6255663e6deSDavid Greenman struct proc *p, *bigproc; 6265663e6deSDavid Greenman vm_offset_t size, bigsize; 627df8bae1dSRodney W. Grimes vm_object_t object; 62826f9a767SRodney W. Grimes int force_wakeup = 0; 6297e006499SJohn Dyson int actcount; 630f6b04d2bSDavid Greenman int vnodes_skipped = 0; 6311eeaa1e3SJohn Dyson int s; 6320d94caffSDavid Greenman 633df8bae1dSRodney W. Grimes /* 6345985940eSJohn Dyson * Do whatever cleanup that the pmap code can. 6355985940eSJohn Dyson */ 6365985940eSJohn Dyson pmap_collect(); 6375985940eSJohn Dyson 6381c7c3c6aSMatthew Dillon addl_page_shortage_init = vm_pageout_deficit; 63995461b45SJohn Dyson vm_pageout_deficit = 0; 640b182ec9eSJohn Dyson 641ceb0cf87SJohn Dyson if (max_page_launder == 0) 642ceb0cf87SJohn Dyson max_page_launder = 1; 6431c7c3c6aSMatthew Dillon 6441c7c3c6aSMatthew Dillon /* 6451c7c3c6aSMatthew Dillon * Calculate the number of pages we want to either free or move 6461c7c3c6aSMatthew Dillon * to the cache. 6471c7c3c6aSMatthew Dillon */ 6481c7c3c6aSMatthew Dillon 64990ecac61SMatthew Dillon page_shortage = vm_paging_target() + addl_page_shortage_init; 6501c7c3c6aSMatthew Dillon 6511c7c3c6aSMatthew Dillon /* 6521c7c3c6aSMatthew Dillon * Figure out what to do with dirty pages when they are encountered. 6531c7c3c6aSMatthew Dillon * Assume that 1/3 of the pages on the inactive list are clean. If 6541c7c3c6aSMatthew Dillon * we think we can reach our target, disable laundering (do not 6551c7c3c6aSMatthew Dillon * clean any dirty pages). If we miss the target we will loop back 6561c7c3c6aSMatthew Dillon * up and do a laundering run. 6571c7c3c6aSMatthew Dillon */ 6581c7c3c6aSMatthew Dillon 6591c7c3c6aSMatthew Dillon if (cnt.v_inactive_count / 3 > page_shortage) { 6601c7c3c6aSMatthew Dillon maxlaunder = 0; 6611c7c3c6aSMatthew Dillon launder_loop = 0; 6621c7c3c6aSMatthew Dillon } else { 6631c7c3c6aSMatthew Dillon maxlaunder = 6641c7c3c6aSMatthew Dillon (cnt.v_inactive_target > max_page_launder) ? 665ceb0cf87SJohn Dyson max_page_launder : cnt.v_inactive_target; 6661c7c3c6aSMatthew Dillon launder_loop = 1; 6671c7c3c6aSMatthew Dillon } 6681c7c3c6aSMatthew Dillon 6691c7c3c6aSMatthew 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 6721c7c3c6aSMatthew Dillon * we have scanned the entire inactive queue. 6731c7c3c6aSMatthew Dillon */ 67470111b90SJohn Dyson 67567bf6868SJohn Dyson rescan0: 6761c7c3c6aSMatthew Dillon addl_page_shortage = addl_page_shortage_init; 677f6b04d2bSDavid Greenman maxscan = cnt.v_inactive_count; 678e929c00dSKirk McKusick for (m = TAILQ_FIRST(&vm_page_queue_inactive); 6791c7c3c6aSMatthew Dillon m != NULL && maxscan-- > 0 && page_shortage > 0; 680e929c00dSKirk McKusick m = next) { 681df8bae1dSRodney W. Grimes 682a58d1fa1SDavid Greenman cnt.v_pdpages++; 683b182ec9eSJohn Dyson 684f35329acSJohn Dyson if (m->queue != PQ_INACTIVE) { 68567bf6868SJohn Dyson goto rescan0; 686f35329acSJohn Dyson } 687b182ec9eSJohn Dyson 688b18bfc3dSJohn Dyson next = TAILQ_NEXT(m, pageq); 689df8bae1dSRodney W. Grimes 690b182ec9eSJohn Dyson if (m->hold_count) { 691f35329acSJohn Dyson s = splvm(); 692b182ec9eSJohn Dyson TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq); 693b182ec9eSJohn Dyson TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq); 694f35329acSJohn Dyson splx(s); 695b182ec9eSJohn Dyson addl_page_shortage++; 696b182ec9eSJohn Dyson continue; 697df8bae1dSRodney W. Grimes } 69826f9a767SRodney W. Grimes /* 699b18bfc3dSJohn Dyson * Dont mess with busy pages, keep in the front of the 700b18bfc3dSJohn Dyson * queue, most likely are being paged out. 70126f9a767SRodney W. Grimes */ 702bd7e5f99SJohn Dyson if (m->busy || (m->flags & PG_BUSY)) { 703b182ec9eSJohn Dyson addl_page_shortage++; 70426f9a767SRodney W. Grimes continue; 70526f9a767SRodney W. Grimes } 706bd7e5f99SJohn Dyson 7077e006499SJohn Dyson /* 7081c7c3c6aSMatthew Dillon * If the object is not being used, we ignore previous 7091c7c3c6aSMatthew Dillon * references. 7107e006499SJohn Dyson */ 7110d94caffSDavid Greenman if (m->object->ref_count == 0) { 712e69763a3SDoug Rabson vm_page_flag_clear(m, PG_REFERENCED); 71367bf6868SJohn Dyson pmap_clear_reference(VM_PAGE_TO_PHYS(m)); 7147e006499SJohn Dyson 7157e006499SJohn Dyson /* 7161c7c3c6aSMatthew Dillon * Otherwise, if the page has been referenced while in the 7171c7c3c6aSMatthew Dillon * inactive queue, we bump the "activation count" upwards, 7181c7c3c6aSMatthew Dillon * making it less likely that the page will be added back to 7191c7c3c6aSMatthew Dillon * the inactive queue prematurely again. Here we check the 7201c7c3c6aSMatthew Dillon * page tables (or emulated bits, if any), given the upper 7211c7c3c6aSMatthew Dillon * level VM system not knowing anything about existing 7221c7c3c6aSMatthew Dillon * references. 7237e006499SJohn Dyson */ 724ef743ce6SJohn Dyson } else if (((m->flags & PG_REFERENCED) == 0) && 7257e006499SJohn Dyson (actcount = pmap_ts_referenced(VM_PAGE_TO_PHYS(m)))) { 726ef743ce6SJohn Dyson vm_page_activate(m); 7277e006499SJohn Dyson m->act_count += (actcount + ACT_ADVANCE); 728ef743ce6SJohn Dyson continue; 7292fe6e4d7SDavid Greenman } 730ef743ce6SJohn Dyson 7317e006499SJohn Dyson /* 7321c7c3c6aSMatthew Dillon * If the upper level VM system knows about any page 7331c7c3c6aSMatthew Dillon * references, we activate the page. We also set the 7341c7c3c6aSMatthew Dillon * "activation count" higher than normal so that we will less 7351c7c3c6aSMatthew Dillon * likely place pages back onto the inactive queue again. 7367e006499SJohn Dyson */ 737bd7e5f99SJohn Dyson if ((m->flags & PG_REFERENCED) != 0) { 738e69763a3SDoug Rabson vm_page_flag_clear(m, PG_REFERENCED); 7397e006499SJohn Dyson actcount = pmap_ts_referenced(VM_PAGE_TO_PHYS(m)); 74026f9a767SRodney W. Grimes vm_page_activate(m); 7417e006499SJohn Dyson m->act_count += (actcount + ACT_ADVANCE + 1); 7420d94caffSDavid Greenman continue; 7430d94caffSDavid Greenman } 74467bf6868SJohn Dyson 7457e006499SJohn Dyson /* 7461c7c3c6aSMatthew Dillon * If the upper level VM system doesn't know anything about 7471c7c3c6aSMatthew Dillon * the page being dirty, we have to check for it again. As 7481c7c3c6aSMatthew Dillon * far as the VM code knows, any partially dirty pages are 7491c7c3c6aSMatthew Dillon * fully dirty. 7507e006499SJohn Dyson */ 751f6b04d2bSDavid Greenman if (m->dirty == 0) { 752bd7e5f99SJohn Dyson vm_page_test_dirty(m); 753427e99a0SAlexander Langer } else { 7547dbf82dcSMatthew Dillon vm_page_dirty(m); 75530dcfc09SJohn Dyson } 756ef743ce6SJohn Dyson 7577e006499SJohn Dyson /* 7587e006499SJohn Dyson * Invalid pages can be easily freed 7597e006499SJohn Dyson */ 7606d40c3d3SDavid Greenman if (m->valid == 0) { 761925a3a41SJohn Dyson vm_pageout_page_free(m); 76267bf6868SJohn Dyson cnt.v_dfree++; 7631c7c3c6aSMatthew Dillon --page_shortage; 7647e006499SJohn Dyson 7657e006499SJohn Dyson /* 7667e006499SJohn Dyson * Clean pages can be placed onto the cache queue. 7677e006499SJohn Dyson */ 768bd7e5f99SJohn Dyson } else if (m->dirty == 0) { 769bd7e5f99SJohn Dyson vm_page_cache(m); 7701c7c3c6aSMatthew Dillon --page_shortage; 7717e006499SJohn Dyson 7727e006499SJohn Dyson /* 7737e006499SJohn Dyson * Dirty pages need to be paged out. Note that we clean 7747e006499SJohn Dyson * only a limited number of pages per pagedaemon pass. 7757e006499SJohn Dyson */ 7760d94caffSDavid Greenman } else if (maxlaunder > 0) { 7770d94caffSDavid Greenman int written; 77812ac6a1dSJohn Dyson int swap_pageouts_ok; 779f6b04d2bSDavid Greenman struct vnode *vp = NULL; 7800d94caffSDavid Greenman 7810d94caffSDavid Greenman object = m->object; 7827e006499SJohn Dyson 78312ac6a1dSJohn Dyson if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) { 78412ac6a1dSJohn Dyson swap_pageouts_ok = 1; 78512ac6a1dSJohn Dyson } else { 78612ac6a1dSJohn Dyson swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts); 78712ac6a1dSJohn Dyson swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts && 78890ecac61SMatthew Dillon vm_page_count_min()); 78912ac6a1dSJohn Dyson 79012ac6a1dSJohn Dyson } 79170111b90SJohn Dyson 79270111b90SJohn Dyson /* 7931c7c3c6aSMatthew Dillon * We don't bother paging objects that are "dead". 7941c7c3c6aSMatthew Dillon * Those objects are in a "rundown" state. 79570111b90SJohn Dyson */ 79670111b90SJohn Dyson if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) { 79712ac6a1dSJohn Dyson s = splvm(); 79812ac6a1dSJohn Dyson TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq); 79912ac6a1dSJohn Dyson TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq); 80012ac6a1dSJohn Dyson splx(s); 80112ac6a1dSJohn Dyson continue; 80212ac6a1dSJohn Dyson } 80312ac6a1dSJohn Dyson 8041c7c3c6aSMatthew Dillon /* 8051c7c3c6aSMatthew Dillon * For now we protect against potential memory 8061c7c3c6aSMatthew Dillon * deadlocks by requiring significant memory to be 8071c7c3c6aSMatthew Dillon * free if the object is not OBJT_DEFAULT or OBJT_SWAP. 8081c7c3c6aSMatthew Dillon * We do not 'trust' any other object type to operate 8091c7c3c6aSMatthew Dillon * with low memory, not even OBJT_DEVICE. The VM 8101c7c3c6aSMatthew Dillon * allocator will special case allocations done by 8111c7c3c6aSMatthew Dillon * the pageout daemon so the check below actually 8121c7c3c6aSMatthew Dillon * does have some hysteresis in it. It isn't the best 8131c7c3c6aSMatthew Dillon * solution, though. 8141c7c3c6aSMatthew Dillon */ 8151c7c3c6aSMatthew Dillon 816e929c00dSKirk McKusick if (object->type != OBJT_DEFAULT && 8171c7c3c6aSMatthew Dillon object->type != OBJT_SWAP && 818e929c00dSKirk McKusick cnt.v_free_count < cnt.v_free_reserved) { 8191c7c3c6aSMatthew Dillon s = splvm(); 8201c7c3c6aSMatthew Dillon TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq); 821e929c00dSKirk McKusick TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, 822e929c00dSKirk McKusick pageq); 8231c7c3c6aSMatthew Dillon splx(s); 8241c7c3c6aSMatthew Dillon continue; 8251c7c3c6aSMatthew Dillon } 8261c7c3c6aSMatthew Dillon 8271c7c3c6aSMatthew Dillon /* 8281c7c3c6aSMatthew Dillon * Presumably we have sufficient free memory to do 8291c7c3c6aSMatthew Dillon * the more sophisticated checks and locking required 8301c7c3c6aSMatthew Dillon * for vnodes. 8311c7c3c6aSMatthew Dillon * 8321c7c3c6aSMatthew Dillon * The object is already known NOT to be dead. The 8331c7c3c6aSMatthew Dillon * vget() may still block, though, because 8341c7c3c6aSMatthew Dillon * VOP_ISLOCKED() doesn't check to see if an inode 8351c7c3c6aSMatthew Dillon * (v_data) is associated with the vnode. If it isn't, 8361c7c3c6aSMatthew Dillon * vget() will load in it from disk. Worse, vget() 8371c7c3c6aSMatthew Dillon * may actually get stuck waiting on "inode" if another 8381c7c3c6aSMatthew Dillon * process is in the process of bringing the inode in. 8391c7c3c6aSMatthew Dillon * This is bad news for us either way. 8401c7c3c6aSMatthew Dillon * 8411c7c3c6aSMatthew Dillon * So for the moment we check v_data == NULL as a 8421c7c3c6aSMatthew Dillon * workaround. This means that vnodes which do not 8431c7c3c6aSMatthew Dillon * use v_data in the way we expect probably will not 8441c7c3c6aSMatthew Dillon * wind up being paged out by the pager and it will be 8451c7c3c6aSMatthew Dillon * up to the syncer to get them. That's better then 8461c7c3c6aSMatthew Dillon * us blocking here. 8471c7c3c6aSMatthew Dillon * 8481c7c3c6aSMatthew Dillon * This whole code section is bogus - we need to fix 8491c7c3c6aSMatthew Dillon * the vnode pager to handle vm_page_t's without us 8501c7c3c6aSMatthew Dillon * having to do any sophisticated VOP tests. 8511c7c3c6aSMatthew Dillon */ 8521c7c3c6aSMatthew Dillon 8531c7c3c6aSMatthew Dillon if (object->type == OBJT_VNODE) { 85424a1cce3SDavid Greenman vp = object->handle; 8551c7c3c6aSMatthew Dillon 856996c772fSJohn Dyson if (VOP_ISLOCKED(vp) || 8571c7c3c6aSMatthew Dillon vp->v_data == NULL || 85847221757SJohn Dyson vget(vp, LK_EXCLUSIVE|LK_NOOBJ, curproc)) { 859b182ec9eSJohn Dyson if ((m->queue == PQ_INACTIVE) && 860b182ec9eSJohn Dyson (m->hold_count == 0) && 861b182ec9eSJohn Dyson (m->busy == 0) && 862b182ec9eSJohn Dyson (m->flags & PG_BUSY) == 0) { 863f35329acSJohn Dyson s = splvm(); 86485a376ebSJohn Dyson TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq); 86585a376ebSJohn Dyson TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq); 866f35329acSJohn Dyson splx(s); 86785a376ebSJohn Dyson } 868aef922f5SJohn Dyson if (object->flags & OBJ_MIGHTBEDIRTY) 869925a3a41SJohn Dyson vnodes_skipped++; 870b182ec9eSJohn Dyson continue; 87185a376ebSJohn Dyson } 872b182ec9eSJohn Dyson 873f35329acSJohn Dyson /* 874f35329acSJohn Dyson * The page might have been moved to another queue 875f35329acSJohn Dyson * during potential blocking in vget() above. 876f35329acSJohn Dyson */ 877b182ec9eSJohn Dyson if (m->queue != PQ_INACTIVE) { 878b182ec9eSJohn Dyson if (object->flags & OBJ_MIGHTBEDIRTY) 879925a3a41SJohn Dyson vnodes_skipped++; 880b182ec9eSJohn Dyson vput(vp); 881b182ec9eSJohn Dyson continue; 882b182ec9eSJohn Dyson } 883b182ec9eSJohn Dyson 884f35329acSJohn Dyson /* 885f35329acSJohn Dyson * The page may have been busied during the blocking in 886f35329acSJohn Dyson * vput(); We don't move the page back onto the end of 887f35329acSJohn Dyson * the queue so that statistics are more correct if we don't. 888f35329acSJohn Dyson */ 889b182ec9eSJohn Dyson if (m->busy || (m->flags & PG_BUSY)) { 890b182ec9eSJohn Dyson vput(vp); 891b182ec9eSJohn Dyson continue; 892b182ec9eSJohn Dyson } 893b182ec9eSJohn Dyson 894f35329acSJohn Dyson /* 895f35329acSJohn Dyson * If the page has become held, then skip it 896f35329acSJohn Dyson */ 897b182ec9eSJohn Dyson if (m->hold_count) { 898f35329acSJohn Dyson s = splvm(); 899b182ec9eSJohn Dyson TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq); 900b182ec9eSJohn Dyson TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq); 901f35329acSJohn Dyson splx(s); 902b182ec9eSJohn Dyson if (object->flags & OBJ_MIGHTBEDIRTY) 903925a3a41SJohn Dyson vnodes_skipped++; 904b182ec9eSJohn Dyson vput(vp); 905f6b04d2bSDavid Greenman continue; 906f6b04d2bSDavid Greenman } 907f6b04d2bSDavid Greenman } 908f6b04d2bSDavid Greenman 9090d94caffSDavid Greenman /* 9100d94caffSDavid Greenman * If a page is dirty, then it is either being washed 9110d94caffSDavid Greenman * (but not yet cleaned) or it is still in the 9120d94caffSDavid Greenman * laundry. If it is still in the laundry, then we 9130d94caffSDavid Greenman * start the cleaning operation. 9140d94caffSDavid Greenman */ 9158f9110f6SJohn Dyson written = vm_pageout_clean(m); 916f6b04d2bSDavid Greenman if (vp) 917f6b04d2bSDavid Greenman vput(vp); 918f6b04d2bSDavid Greenman 9190d94caffSDavid Greenman maxlaunder -= written; 9200d94caffSDavid Greenman } 921df8bae1dSRodney W. Grimes } 92226f9a767SRodney W. Grimes 923df8bae1dSRodney W. Grimes /* 9241c7c3c6aSMatthew Dillon * If we still have a page shortage and we didn't launder anything, 9251c7c3c6aSMatthew Dillon * run the inactive scan again and launder something this time. 926df8bae1dSRodney W. Grimes */ 9271c7c3c6aSMatthew Dillon 9281c7c3c6aSMatthew Dillon if (launder_loop == 0 && page_shortage > 0) { 9291c7c3c6aSMatthew Dillon launder_loop = 1; 9301c7c3c6aSMatthew Dillon maxlaunder = 9311c7c3c6aSMatthew Dillon (cnt.v_inactive_target > max_page_launder) ? 9321c7c3c6aSMatthew Dillon max_page_launder : cnt.v_inactive_target; 9331c7c3c6aSMatthew Dillon goto rescan0; 9341c7c3c6aSMatthew Dillon } 9351c7c3c6aSMatthew Dillon 9361c7c3c6aSMatthew Dillon /* 9371c7c3c6aSMatthew Dillon * Compute the page shortage from the point of view of having to 9381c7c3c6aSMatthew Dillon * move pages from the active queue to the inactive queue. 9391c7c3c6aSMatthew Dillon */ 9401c7c3c6aSMatthew Dillon 941b182ec9eSJohn Dyson page_shortage = (cnt.v_inactive_target + cnt.v_cache_min) - 9420d94caffSDavid Greenman (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count); 943b182ec9eSJohn Dyson page_shortage += addl_page_shortage; 9441c7c3c6aSMatthew Dillon 9451c7c3c6aSMatthew Dillon /* 9461c7c3c6aSMatthew Dillon * Scan the active queue for things we can deactivate 9471c7c3c6aSMatthew Dillon */ 94826f9a767SRodney W. Grimes 949b18bfc3dSJohn Dyson pcount = cnt.v_active_count; 950b18bfc3dSJohn Dyson m = TAILQ_FIRST(&vm_page_queue_active); 9511c7c3c6aSMatthew Dillon 952b18bfc3dSJohn Dyson while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) { 953f35329acSJohn Dyson 9547e006499SJohn Dyson /* 9557e006499SJohn Dyson * This is a consistancy check, and should likely be a panic 9567e006499SJohn Dyson * or warning. 9577e006499SJohn Dyson */ 958f35329acSJohn Dyson if (m->queue != PQ_ACTIVE) { 95938efa82bSJohn Dyson break; 960f35329acSJohn Dyson } 961f35329acSJohn Dyson 962b18bfc3dSJohn Dyson next = TAILQ_NEXT(m, pageq); 963df8bae1dSRodney W. Grimes /* 96426f9a767SRodney W. Grimes * Don't deactivate pages that are busy. 965df8bae1dSRodney W. Grimes */ 966a647a309SDavid Greenman if ((m->busy != 0) || 9670d94caffSDavid Greenman (m->flags & PG_BUSY) || 968f6b04d2bSDavid Greenman (m->hold_count != 0)) { 969f35329acSJohn Dyson s = splvm(); 9706d40c3d3SDavid Greenman TAILQ_REMOVE(&vm_page_queue_active, m, pageq); 9716d40c3d3SDavid Greenman TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq); 972f35329acSJohn Dyson splx(s); 97326f9a767SRodney W. Grimes m = next; 97426f9a767SRodney W. Grimes continue; 975df8bae1dSRodney W. Grimes } 976b18bfc3dSJohn Dyson 977b18bfc3dSJohn Dyson /* 978b18bfc3dSJohn Dyson * The count for pagedaemon pages is done after checking the 979b18bfc3dSJohn Dyson * page for eligbility... 980b18bfc3dSJohn Dyson */ 981b18bfc3dSJohn Dyson cnt.v_pdpages++; 982ef743ce6SJohn Dyson 9837e006499SJohn Dyson /* 9847e006499SJohn Dyson * Check to see "how much" the page has been used. 9857e006499SJohn Dyson */ 9867e006499SJohn Dyson actcount = 0; 987ef743ce6SJohn Dyson if (m->object->ref_count != 0) { 988ef743ce6SJohn Dyson if (m->flags & PG_REFERENCED) { 9897e006499SJohn Dyson actcount += 1; 9900d94caffSDavid Greenman } 9917e006499SJohn Dyson actcount += pmap_ts_referenced(VM_PAGE_TO_PHYS(m)); 9927e006499SJohn Dyson if (actcount) { 9937e006499SJohn Dyson m->act_count += ACT_ADVANCE + actcount; 99438efa82bSJohn Dyson if (m->act_count > ACT_MAX) 99538efa82bSJohn Dyson m->act_count = ACT_MAX; 99638efa82bSJohn Dyson } 997b18bfc3dSJohn Dyson } 998ef743ce6SJohn Dyson 9997e006499SJohn Dyson /* 10007e006499SJohn Dyson * Since we have "tested" this bit, we need to clear it now. 10017e006499SJohn Dyson */ 1002e69763a3SDoug Rabson vm_page_flag_clear(m, PG_REFERENCED); 1003ef743ce6SJohn Dyson 10047e006499SJohn Dyson /* 10057e006499SJohn Dyson * Only if an object is currently being used, do we use the 10067e006499SJohn Dyson * page activation count stats. 10077e006499SJohn Dyson */ 10087e006499SJohn Dyson if (actcount && (m->object->ref_count != 0)) { 1009f35329acSJohn Dyson s = splvm(); 101026f9a767SRodney W. Grimes TAILQ_REMOVE(&vm_page_queue_active, m, pageq); 101126f9a767SRodney W. Grimes TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq); 1012f35329acSJohn Dyson splx(s); 101326f9a767SRodney W. Grimes } else { 101438efa82bSJohn Dyson m->act_count -= min(m->act_count, ACT_DECLINE); 101538efa82bSJohn Dyson if (vm_pageout_algorithm_lru || 101638efa82bSJohn Dyson (m->object->ref_count == 0) || (m->act_count == 0)) { 1017925a3a41SJohn Dyson page_shortage--; 1018d4a272dbSJohn Dyson if (m->object->ref_count == 0) { 1019ef743ce6SJohn Dyson vm_page_protect(m, VM_PROT_NONE); 1020d4a272dbSJohn Dyson if (m->dirty == 0) 10210d94caffSDavid Greenman vm_page_cache(m); 1022d4a272dbSJohn Dyson else 1023d4a272dbSJohn Dyson vm_page_deactivate(m); 10240d94caffSDavid Greenman } else { 102526f9a767SRodney W. Grimes vm_page_deactivate(m); 1026df8bae1dSRodney W. Grimes } 102738efa82bSJohn Dyson } else { 102838efa82bSJohn Dyson s = splvm(); 102938efa82bSJohn Dyson TAILQ_REMOVE(&vm_page_queue_active, m, pageq); 103038efa82bSJohn Dyson TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq); 103138efa82bSJohn Dyson splx(s); 103238efa82bSJohn Dyson } 1033df8bae1dSRodney W. Grimes } 103426f9a767SRodney W. Grimes m = next; 103526f9a767SRodney W. Grimes } 1036df8bae1dSRodney W. Grimes 1037f35329acSJohn Dyson s = splvm(); 10381c7c3c6aSMatthew Dillon 1039df8bae1dSRodney W. Grimes /* 10400d94caffSDavid Greenman * We try to maintain some *really* free pages, this allows interrupt 10411c7c3c6aSMatthew Dillon * code to be guaranteed space. Since both cache and free queues 10421c7c3c6aSMatthew Dillon * are considered basically 'free', moving pages from cache to free 10431c7c3c6aSMatthew Dillon * does not effect other calculations. 1044df8bae1dSRodney W. Grimes */ 10451c7c3c6aSMatthew Dillon 1046a1f6d91cSDavid Greenman while (cnt.v_free_count < cnt.v_free_reserved) { 10475070c7f8SJohn Dyson static int cache_rover = 0; 1048faa273d5SMatthew Dillon m = vm_page_list_find(PQ_CACHE, cache_rover, FALSE); 10490d94caffSDavid Greenman if (!m) 10500d94caffSDavid Greenman break; 1051aaba53daSMatthew Dillon if ((m->flags & PG_BUSY) || m->busy || m->hold_count || m->wire_count) { 1052d044d7bfSMatthew Dillon #ifdef INVARIANTS 1053d044d7bfSMatthew Dillon printf("Warning: busy page %p found in cache\n", m); 1054d044d7bfSMatthew Dillon #endif 1055aaba53daSMatthew Dillon vm_page_deactivate(m); 1056aaba53daSMatthew Dillon continue; 1057aaba53daSMatthew Dillon } 10585070c7f8SJohn Dyson cache_rover = (cache_rover + PQ_PRIME2) & PQ_L2_MASK; 1059925a3a41SJohn Dyson vm_pageout_page_free(m); 10600bb3a0d2SDavid Greenman cnt.v_dfree++; 106126f9a767SRodney W. Grimes } 1062f35329acSJohn Dyson splx(s); 10635663e6deSDavid Greenman 1064ceb0cf87SJohn Dyson #if !defined(NO_SWAPPING) 1065ceb0cf87SJohn Dyson /* 1066ceb0cf87SJohn Dyson * Idle process swapout -- run once per second. 1067ceb0cf87SJohn Dyson */ 1068ceb0cf87SJohn Dyson if (vm_swap_idle_enabled) { 1069ceb0cf87SJohn Dyson static long lsec; 1070227ee8a1SPoul-Henning Kamp if (time_second != lsec) { 1071ceb0cf87SJohn Dyson vm_pageout_req_swapout |= VM_SWAP_IDLE; 1072ceb0cf87SJohn Dyson vm_req_vmdaemon(); 1073227ee8a1SPoul-Henning Kamp lsec = time_second; 1074ceb0cf87SJohn Dyson } 1075ceb0cf87SJohn Dyson } 1076ceb0cf87SJohn Dyson #endif 1077ceb0cf87SJohn Dyson 10785663e6deSDavid Greenman /* 1079f6b04d2bSDavid Greenman * If we didn't get enough free pages, and we have skipped a vnode 10804c1f8ee9SDavid Greenman * in a writeable object, wakeup the sync daemon. And kick swapout 10814c1f8ee9SDavid Greenman * if we did not get enough free pages. 1082f6b04d2bSDavid Greenman */ 108390ecac61SMatthew Dillon if (vm_paging_target() > 0) { 108490ecac61SMatthew Dillon if (vnodes_skipped && vm_page_count_min()) 1085d50c1994SPeter Wemm (void) speedup_syncer(); 108638efa82bSJohn Dyson #if !defined(NO_SWAPPING) 108790ecac61SMatthew Dillon if (vm_swap_enabled && vm_page_count_target()) { 10884c1f8ee9SDavid Greenman vm_req_vmdaemon(); 1089ceb0cf87SJohn Dyson vm_pageout_req_swapout |= VM_SWAP_NORMAL; 10904c1f8ee9SDavid Greenman } 10915afce282SDavid Greenman #endif 10924c1f8ee9SDavid Greenman } 10934c1f8ee9SDavid Greenman 1094f6b04d2bSDavid Greenman /* 10950d94caffSDavid Greenman * make sure that we have swap space -- if we are low on memory and 10960d94caffSDavid Greenman * swap -- then kill the biggest process. 10975663e6deSDavid Greenman */ 109890ecac61SMatthew Dillon if ((vm_swap_size == 0 || swap_pager_full) && vm_page_count_min()) { 10995663e6deSDavid Greenman bigproc = NULL; 11005663e6deSDavid Greenman bigsize = 0; 11011b67ec6dSJeffrey Hsu for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 11025663e6deSDavid Greenman /* 11035663e6deSDavid Greenman * if this is a system process, skip it 11045663e6deSDavid Greenman */ 1105c8da68e9SPeter Wemm if ((p->p_flag & P_SYSTEM) || (p->p_lock > 0) || 1106c8da68e9SPeter Wemm (p->p_pid == 1) || 110779221631SDavid Greenman ((p->p_pid < 48) && (vm_swap_size != 0))) { 11085663e6deSDavid Greenman continue; 11095663e6deSDavid Greenman } 11105663e6deSDavid Greenman /* 11115663e6deSDavid Greenman * if the process is in a non-running type state, 11125663e6deSDavid Greenman * don't touch it. 11135663e6deSDavid Greenman */ 11145663e6deSDavid Greenman if (p->p_stat != SRUN && p->p_stat != SSLEEP) { 11155663e6deSDavid Greenman continue; 11165663e6deSDavid Greenman } 11175663e6deSDavid Greenman /* 11185663e6deSDavid Greenman * get the process size 11195663e6deSDavid Greenman */ 1120b1028ad1SLuoqi Chen size = vmspace_resident_count(p->p_vmspace); 11215663e6deSDavid Greenman /* 11225663e6deSDavid Greenman * if the this process is bigger than the biggest one 11235663e6deSDavid Greenman * remember it. 11245663e6deSDavid Greenman */ 11255663e6deSDavid Greenman if (size > bigsize) { 11265663e6deSDavid Greenman bigproc = p; 11275663e6deSDavid Greenman bigsize = size; 11285663e6deSDavid Greenman } 11295663e6deSDavid Greenman } 11305663e6deSDavid Greenman if (bigproc != NULL) { 1131729b1e51SDavid Greenman killproc(bigproc, "out of swap space"); 11325663e6deSDavid Greenman bigproc->p_estcpu = 0; 11335663e6deSDavid Greenman bigproc->p_nice = PRIO_MIN; 11345663e6deSDavid Greenman resetpriority(bigproc); 113524a1cce3SDavid Greenman wakeup(&cnt.v_free_count); 11365663e6deSDavid Greenman } 11375663e6deSDavid Greenman } 113826f9a767SRodney W. Grimes return force_wakeup; 113926f9a767SRodney W. Grimes } 114026f9a767SRodney W. Grimes 1141dc2efb27SJohn Dyson /* 1142dc2efb27SJohn Dyson * This routine tries to maintain the pseudo LRU active queue, 1143dc2efb27SJohn Dyson * so that during long periods of time where there is no paging, 1144dc2efb27SJohn Dyson * that some statistic accumlation still occurs. This code 1145dc2efb27SJohn Dyson * helps the situation where paging just starts to occur. 1146dc2efb27SJohn Dyson */ 1147dc2efb27SJohn Dyson static void 1148dc2efb27SJohn Dyson vm_pageout_page_stats() 1149dc2efb27SJohn Dyson { 1150dc2efb27SJohn Dyson int s; 1151dc2efb27SJohn Dyson vm_page_t m,next; 1152dc2efb27SJohn Dyson int pcount,tpcount; /* Number of pages to check */ 1153dc2efb27SJohn Dyson static int fullintervalcount = 0; 1154bef608bdSJohn Dyson int page_shortage; 1155bef608bdSJohn Dyson 115690ecac61SMatthew Dillon page_shortage = 115790ecac61SMatthew Dillon (cnt.v_inactive_target + cnt.v_cache_max + cnt.v_free_min) - 1158bef608bdSJohn Dyson (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count); 115990ecac61SMatthew Dillon 1160bef608bdSJohn Dyson if (page_shortage <= 0) 1161bef608bdSJohn Dyson return; 1162dc2efb27SJohn Dyson 1163dc2efb27SJohn Dyson pcount = cnt.v_active_count; 1164dc2efb27SJohn Dyson fullintervalcount += vm_pageout_stats_interval; 1165dc2efb27SJohn Dyson if (fullintervalcount < vm_pageout_full_stats_interval) { 1166dc2efb27SJohn Dyson tpcount = (vm_pageout_stats_max * cnt.v_active_count) / cnt.v_page_count; 1167dc2efb27SJohn Dyson if (pcount > tpcount) 1168dc2efb27SJohn Dyson pcount = tpcount; 1169dc2efb27SJohn Dyson } 1170dc2efb27SJohn Dyson 1171dc2efb27SJohn Dyson m = TAILQ_FIRST(&vm_page_queue_active); 1172dc2efb27SJohn Dyson while ((m != NULL) && (pcount-- > 0)) { 11737e006499SJohn Dyson int actcount; 1174dc2efb27SJohn Dyson 1175dc2efb27SJohn Dyson if (m->queue != PQ_ACTIVE) { 1176dc2efb27SJohn Dyson break; 1177dc2efb27SJohn Dyson } 1178dc2efb27SJohn Dyson 1179dc2efb27SJohn Dyson next = TAILQ_NEXT(m, pageq); 1180dc2efb27SJohn Dyson /* 1181dc2efb27SJohn Dyson * Don't deactivate pages that are busy. 1182dc2efb27SJohn Dyson */ 1183dc2efb27SJohn Dyson if ((m->busy != 0) || 1184dc2efb27SJohn Dyson (m->flags & PG_BUSY) || 1185dc2efb27SJohn Dyson (m->hold_count != 0)) { 1186dc2efb27SJohn Dyson s = splvm(); 1187dc2efb27SJohn Dyson TAILQ_REMOVE(&vm_page_queue_active, m, pageq); 1188dc2efb27SJohn Dyson TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq); 1189dc2efb27SJohn Dyson splx(s); 1190dc2efb27SJohn Dyson m = next; 1191dc2efb27SJohn Dyson continue; 1192dc2efb27SJohn Dyson } 1193dc2efb27SJohn Dyson 11947e006499SJohn Dyson actcount = 0; 1195dc2efb27SJohn Dyson if (m->flags & PG_REFERENCED) { 1196e69763a3SDoug Rabson vm_page_flag_clear(m, PG_REFERENCED); 11977e006499SJohn Dyson actcount += 1; 1198dc2efb27SJohn Dyson } 1199dc2efb27SJohn Dyson 12007e006499SJohn Dyson actcount += pmap_ts_referenced(VM_PAGE_TO_PHYS(m)); 12017e006499SJohn Dyson if (actcount) { 12027e006499SJohn Dyson m->act_count += ACT_ADVANCE + actcount; 1203dc2efb27SJohn Dyson if (m->act_count > ACT_MAX) 1204dc2efb27SJohn Dyson m->act_count = ACT_MAX; 1205dc2efb27SJohn Dyson s = splvm(); 1206dc2efb27SJohn Dyson TAILQ_REMOVE(&vm_page_queue_active, m, pageq); 1207dc2efb27SJohn Dyson TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq); 1208dc2efb27SJohn Dyson splx(s); 1209dc2efb27SJohn Dyson } else { 1210dc2efb27SJohn Dyson if (m->act_count == 0) { 12117e006499SJohn Dyson /* 12127e006499SJohn Dyson * We turn off page access, so that we have more accurate 12137e006499SJohn Dyson * RSS stats. We don't do this in the normal page deactivation 12147e006499SJohn Dyson * when the system is loaded VM wise, because the cost of 12157e006499SJohn Dyson * the large number of page protect operations would be higher 12167e006499SJohn Dyson * than the value of doing the operation. 12177e006499SJohn Dyson */ 1218dc2efb27SJohn Dyson vm_page_protect(m, VM_PROT_NONE); 1219dc2efb27SJohn Dyson vm_page_deactivate(m); 1220dc2efb27SJohn Dyson } else { 1221dc2efb27SJohn Dyson m->act_count -= min(m->act_count, ACT_DECLINE); 1222dc2efb27SJohn Dyson s = splvm(); 1223dc2efb27SJohn Dyson TAILQ_REMOVE(&vm_page_queue_active, m, pageq); 1224dc2efb27SJohn Dyson TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq); 1225dc2efb27SJohn Dyson splx(s); 1226dc2efb27SJohn Dyson } 1227dc2efb27SJohn Dyson } 1228dc2efb27SJohn Dyson 1229dc2efb27SJohn Dyson m = next; 1230dc2efb27SJohn Dyson } 1231dc2efb27SJohn Dyson } 1232dc2efb27SJohn Dyson 1233b182ec9eSJohn Dyson static int 1234b182ec9eSJohn Dyson vm_pageout_free_page_calc(count) 1235b182ec9eSJohn Dyson vm_size_t count; 1236b182ec9eSJohn Dyson { 1237b182ec9eSJohn Dyson if (count < cnt.v_page_count) 1238b182ec9eSJohn Dyson return 0; 1239b182ec9eSJohn Dyson /* 1240b182ec9eSJohn Dyson * free_reserved needs to include enough for the largest swap pager 1241b182ec9eSJohn Dyson * structures plus enough for any pv_entry structs when paging. 1242b182ec9eSJohn Dyson */ 1243b182ec9eSJohn Dyson if (cnt.v_page_count > 1024) 1244b182ec9eSJohn Dyson cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200; 1245b182ec9eSJohn Dyson else 1246b182ec9eSJohn Dyson cnt.v_free_min = 4; 1247f35329acSJohn Dyson cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE + 1248f35329acSJohn Dyson cnt.v_interrupt_free_min; 1249f35329acSJohn Dyson cnt.v_free_reserved = vm_pageout_page_count + 1250a15403deSJohn Dyson cnt.v_pageout_free_min + (count / 768) + PQ_L2_SIZE; 125190ecac61SMatthew Dillon cnt.v_free_severe = cnt.v_free_min / 2; 1252a2f4a846SJohn Dyson cnt.v_free_min += cnt.v_free_reserved; 125390ecac61SMatthew Dillon cnt.v_free_severe += cnt.v_free_reserved; 1254b182ec9eSJohn Dyson return 1; 1255b182ec9eSJohn Dyson } 1256b182ec9eSJohn Dyson 1257b182ec9eSJohn Dyson 1258df8bae1dSRodney W. Grimes /* 1259df8bae1dSRodney W. Grimes * vm_pageout is the high level pageout daemon. 1260df8bae1dSRodney W. Grimes */ 12612b14f991SJulian Elischer static void 126226f9a767SRodney W. Grimes vm_pageout() 1263df8bae1dSRodney W. Grimes { 1264df8bae1dSRodney W. Grimes /* 1265df8bae1dSRodney W. Grimes * Initialize some paging parameters. 1266df8bae1dSRodney W. Grimes */ 1267df8bae1dSRodney W. Grimes 1268f6b04d2bSDavid Greenman cnt.v_interrupt_free_min = 2; 1269f35329acSJohn Dyson if (cnt.v_page_count < 2000) 1270f35329acSJohn Dyson vm_pageout_page_count = 8; 1271f6b04d2bSDavid Greenman 1272b182ec9eSJohn Dyson vm_pageout_free_page_calc(cnt.v_page_count); 1273ed74321bSDavid Greenman /* 12740d94caffSDavid Greenman * free_reserved needs to include enough for the largest swap pager 12750d94caffSDavid Greenman * structures plus enough for any pv_entry structs when paging. 1276ed74321bSDavid Greenman */ 1277a15403deSJohn Dyson if (cnt.v_free_count > 6144) 12780d94caffSDavid Greenman cnt.v_free_target = 3 * cnt.v_free_min + cnt.v_free_reserved; 1279a15403deSJohn Dyson else 1280a15403deSJohn Dyson cnt.v_free_target = 2 * cnt.v_free_min + cnt.v_free_reserved; 12816f2b142eSDavid Greenman 1282a15403deSJohn Dyson if (cnt.v_free_count > 2048) { 1283a15403deSJohn Dyson cnt.v_cache_min = cnt.v_free_target; 1284a15403deSJohn Dyson cnt.v_cache_max = 2 * cnt.v_cache_min; 1285a15403deSJohn Dyson cnt.v_inactive_target = (3 * cnt.v_free_target) / 2; 12860d94caffSDavid Greenman } else { 12870d94caffSDavid Greenman cnt.v_cache_min = 0; 12880d94caffSDavid Greenman cnt.v_cache_max = 0; 12896f2b142eSDavid Greenman cnt.v_inactive_target = cnt.v_free_count / 4; 12900d94caffSDavid Greenman } 1291e47ed70bSJohn Dyson if (cnt.v_inactive_target > cnt.v_free_count / 3) 1292e47ed70bSJohn Dyson cnt.v_inactive_target = cnt.v_free_count / 3; 1293df8bae1dSRodney W. Grimes 1294df8bae1dSRodney W. Grimes /* XXX does not really belong here */ 1295df8bae1dSRodney W. Grimes if (vm_page_max_wired == 0) 1296df8bae1dSRodney W. Grimes vm_page_max_wired = cnt.v_free_count / 3; 1297df8bae1dSRodney W. Grimes 1298dc2efb27SJohn Dyson if (vm_pageout_stats_max == 0) 1299dc2efb27SJohn Dyson vm_pageout_stats_max = cnt.v_free_target; 1300dc2efb27SJohn Dyson 1301dc2efb27SJohn Dyson /* 1302dc2efb27SJohn Dyson * Set interval in seconds for stats scan. 1303dc2efb27SJohn Dyson */ 1304dc2efb27SJohn Dyson if (vm_pageout_stats_interval == 0) 1305bef608bdSJohn Dyson vm_pageout_stats_interval = 5; 1306dc2efb27SJohn Dyson if (vm_pageout_full_stats_interval == 0) 1307dc2efb27SJohn Dyson vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4; 1308dc2efb27SJohn Dyson 1309dc2efb27SJohn Dyson 1310dc2efb27SJohn Dyson /* 1311dc2efb27SJohn Dyson * Set maximum free per pass 1312dc2efb27SJohn Dyson */ 1313dc2efb27SJohn Dyson if (vm_pageout_stats_free_max == 0) 1314bef608bdSJohn Dyson vm_pageout_stats_free_max = 5; 1315dc2efb27SJohn Dyson 1316ceb0cf87SJohn Dyson max_page_launder = (cnt.v_page_count > 1800 ? 32 : 16); 131726f9a767SRodney W. Grimes 1318e929c00dSKirk McKusick curproc->p_flag |= P_BUFEXHAUST; 131924a1cce3SDavid Greenman swap_pager_swap_init(); 1320df8bae1dSRodney W. Grimes /* 13210d94caffSDavid Greenman * The pageout daemon is never done, so loop forever. 1322df8bae1dSRodney W. Grimes */ 1323df8bae1dSRodney W. Grimes while (TRUE) { 1324dc2efb27SJohn Dyson int error; 1325b18bfc3dSJohn Dyson int s = splvm(); 132690ecac61SMatthew Dillon 132790ecac61SMatthew Dillon if (vm_pages_needed && vm_page_count_min()) { 132890ecac61SMatthew Dillon /* 132990ecac61SMatthew Dillon * Still not done, sleep a bit and go again 133090ecac61SMatthew Dillon */ 133190ecac61SMatthew Dillon vm_pages_needed = 0; 133290ecac61SMatthew Dillon tsleep(&vm_pages_needed, PVM, "psleep", hz/2); 133390ecac61SMatthew Dillon } else { 133490ecac61SMatthew Dillon /* 133590ecac61SMatthew Dillon * Good enough, sleep & handle stats 133690ecac61SMatthew Dillon */ 1337f919ebdeSDavid Greenman vm_pages_needed = 0; 1338dc2efb27SJohn Dyson error = tsleep(&vm_pages_needed, 1339dc2efb27SJohn Dyson PVM, "psleep", vm_pageout_stats_interval * hz); 1340dc2efb27SJohn Dyson if (error && !vm_pages_needed) { 1341dc2efb27SJohn Dyson splx(s); 1342dc2efb27SJohn Dyson vm_pageout_page_stats(); 1343dc2efb27SJohn Dyson continue; 1344dc2efb27SJohn Dyson } 1345f919ebdeSDavid Greenman } 1346e47ed70bSJohn Dyson 1347b18bfc3dSJohn Dyson if (vm_pages_needed) 1348b18bfc3dSJohn Dyson cnt.v_pdwakeups++; 1349f919ebdeSDavid Greenman vm_pages_needed = 0; 1350f919ebdeSDavid Greenman splx(s); 13510d94caffSDavid Greenman vm_pageout_scan(); 13522d8acc0fSJohn Dyson vm_pageout_deficit = 0; 135324a1cce3SDavid Greenman wakeup(&cnt.v_free_count); 1354df8bae1dSRodney W. Grimes } 1355df8bae1dSRodney W. Grimes } 135626f9a767SRodney W. Grimes 1357e0c5a895SJohn Dyson void 1358e0c5a895SJohn Dyson pagedaemon_wakeup() 1359e0c5a895SJohn Dyson { 1360e0c5a895SJohn Dyson if (!vm_pages_needed && curproc != pageproc) { 1361e0c5a895SJohn Dyson vm_pages_needed++; 1362e0c5a895SJohn Dyson wakeup(&vm_pages_needed); 1363e0c5a895SJohn Dyson } 1364e0c5a895SJohn Dyson } 1365e0c5a895SJohn Dyson 136638efa82bSJohn Dyson #if !defined(NO_SWAPPING) 13675afce282SDavid Greenman static void 13685afce282SDavid Greenman vm_req_vmdaemon() 13695afce282SDavid Greenman { 13705afce282SDavid Greenman static int lastrun = 0; 13715afce282SDavid Greenman 1372b18bfc3dSJohn Dyson if ((ticks > (lastrun + hz)) || (ticks < lastrun)) { 13735afce282SDavid Greenman wakeup(&vm_daemon_needed); 13745afce282SDavid Greenman lastrun = ticks; 13755afce282SDavid Greenman } 13765afce282SDavid Greenman } 13775afce282SDavid Greenman 13782b14f991SJulian Elischer static void 13794f9fb771SBruce Evans vm_daemon() 13800d94caffSDavid Greenman { 13812fe6e4d7SDavid Greenman struct proc *p; 13820d94caffSDavid Greenman 13832fe6e4d7SDavid Greenman while (TRUE) { 1384e8f36785SJohn Dyson tsleep(&vm_daemon_needed, PPAUSE, "psleep", 0); 13854c1f8ee9SDavid Greenman if (vm_pageout_req_swapout) { 1386ceb0cf87SJohn Dyson swapout_procs(vm_pageout_req_swapout); 13874c1f8ee9SDavid Greenman vm_pageout_req_swapout = 0; 13884c1f8ee9SDavid Greenman } 13892fe6e4d7SDavid Greenman /* 13900d94caffSDavid Greenman * scan the processes for exceeding their rlimits or if 13910d94caffSDavid Greenman * process is swapped out -- deactivate pages 13922fe6e4d7SDavid Greenman */ 13932fe6e4d7SDavid Greenman 13941b67ec6dSJeffrey Hsu for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 1395fe2144fdSLuoqi Chen vm_pindex_t limit, size; 13962fe6e4d7SDavid Greenman 13972fe6e4d7SDavid Greenman /* 13982fe6e4d7SDavid Greenman * if this is a system process or if we have already 13992fe6e4d7SDavid Greenman * looked at this process, skip it. 14002fe6e4d7SDavid Greenman */ 14012fe6e4d7SDavid Greenman if (p->p_flag & (P_SYSTEM | P_WEXIT)) { 14022fe6e4d7SDavid Greenman continue; 14032fe6e4d7SDavid Greenman } 14042fe6e4d7SDavid Greenman /* 14052fe6e4d7SDavid Greenman * if the process is in a non-running type state, 14062fe6e4d7SDavid Greenman * don't touch it. 14072fe6e4d7SDavid Greenman */ 14082fe6e4d7SDavid Greenman if (p->p_stat != SRUN && p->p_stat != SSLEEP) { 14092fe6e4d7SDavid Greenman continue; 14102fe6e4d7SDavid Greenman } 14112fe6e4d7SDavid Greenman /* 14122fe6e4d7SDavid Greenman * get a limit 14132fe6e4d7SDavid Greenman */ 1414fe2144fdSLuoqi Chen limit = OFF_TO_IDX( 1415fe2144fdSLuoqi Chen qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur, 1416fe2144fdSLuoqi Chen p->p_rlimit[RLIMIT_RSS].rlim_max)); 14172fe6e4d7SDavid Greenman 14182fe6e4d7SDavid Greenman /* 14190d94caffSDavid Greenman * let processes that are swapped out really be 14200d94caffSDavid Greenman * swapped out set the limit to nothing (will force a 14210d94caffSDavid Greenman * swap-out.) 14222fe6e4d7SDavid Greenman */ 14232fe6e4d7SDavid Greenman if ((p->p_flag & P_INMEM) == 0) 14240d94caffSDavid Greenman limit = 0; /* XXX */ 14252fe6e4d7SDavid Greenman 1426fe2144fdSLuoqi Chen size = vmspace_resident_count(p->p_vmspace); 14272fe6e4d7SDavid Greenman if (limit >= 0 && size >= limit) { 1428fe2144fdSLuoqi Chen vm_pageout_map_deactivate_pages( 1429fe2144fdSLuoqi Chen &p->p_vmspace->vm_map, limit); 14302fe6e4d7SDavid Greenman } 14312fe6e4d7SDavid Greenman } 143224a1cce3SDavid Greenman } 14332fe6e4d7SDavid Greenman } 143438efa82bSJohn Dyson #endif 1435