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 * 6895461b45SJohn Dyson * $Id: vm_pageout.c,v 1.111 1998/02/04 22:33:56 eivind Exp $ 69df8bae1dSRodney W. Grimes */ 70df8bae1dSRodney W. Grimes 71df8bae1dSRodney W. Grimes /* 72df8bae1dSRodney W. Grimes * The proverbial page-out daemon. 73df8bae1dSRodney W. Grimes */ 74df8bae1dSRodney W. Grimes 7547cfdb16SEivind Eklund #include "opt_diagnostic.h" 7647cfdb16SEivind Eklund 77df8bae1dSRodney W. Grimes #include <sys/param.h> 7826f9a767SRodney W. Grimes #include <sys/systm.h> 79b5e8ce9fSBruce Evans #include <sys/kernel.h> 8026f9a767SRodney W. Grimes #include <sys/proc.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)); 1053af76890SPoul-Henning Kamp static int vm_pageout_clean __P((vm_page_t, int)); 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 }; 1154590fd3aSDavid Greenman SYSINIT_KT(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 }; 1274590fd3aSDavid Greenman SYSINIT_KT(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 13526f9a767SRodney W. Grimes extern int npendingio; 13638efa82bSJohn Dyson #if !defined(NO_SWAPPING) 137f708ef1bSPoul-Henning Kamp static int vm_pageout_req_swapout; /* XXX */ 138f708ef1bSPoul-Henning Kamp static int vm_daemon_needed; 13938efa82bSJohn Dyson #endif 14026f9a767SRodney W. Grimes extern int nswiodone; 1415663e6deSDavid Greenman extern int vm_swap_size; 142f6b04d2bSDavid Greenman extern int vfs_update_wakeup; 143dc2efb27SJohn Dyson int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0; 144dc2efb27SJohn Dyson int vm_pageout_full_stats_interval = 0; 145dc2efb27SJohn Dyson int vm_pageout_stats_free_max=0, vm_pageout_algorithm_lru=0; 14612ac6a1dSJohn Dyson int defer_swap_pageouts=0; 14712ac6a1dSJohn Dyson int disable_swap_pageouts=0; 14870111b90SJohn Dyson 149ceb0cf87SJohn Dyson int max_page_launder=100; 15038efa82bSJohn Dyson #if defined(NO_SWAPPING) 151ceb0cf87SJohn Dyson int vm_swap_enabled=0; 152ceb0cf87SJohn Dyson int vm_swap_idle_enabled=0; 15338efa82bSJohn Dyson #else 154ceb0cf87SJohn Dyson int vm_swap_enabled=1; 155ceb0cf87SJohn Dyson int vm_swap_idle_enabled=0; 15638efa82bSJohn Dyson #endif 15738efa82bSJohn Dyson 15838efa82bSJohn Dyson SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm, 15938efa82bSJohn Dyson CTLFLAG_RW, &vm_pageout_algorithm_lru, 0, ""); 16038efa82bSJohn Dyson 161dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max, 162dc2efb27SJohn Dyson CTLFLAG_RW, &vm_pageout_stats_max, 0, ""); 163dc2efb27SJohn Dyson 164dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval, 165dc2efb27SJohn Dyson CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, ""); 166dc2efb27SJohn Dyson 167dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval, 168dc2efb27SJohn Dyson CTLFLAG_RW, &vm_pageout_stats_interval, 0, ""); 169dc2efb27SJohn Dyson 170dc2efb27SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, pageout_stats_free_max, 171dc2efb27SJohn Dyson CTLFLAG_RW, &vm_pageout_stats_free_max, 0, ""); 172dc2efb27SJohn Dyson 17338efa82bSJohn Dyson #if defined(NO_SWAPPING) 174ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 175ceb0cf87SJohn Dyson CTLFLAG_RD, &vm_swap_enabled, 0, ""); 176ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 177ceb0cf87SJohn Dyson CTLFLAG_RD, &vm_swap_idle_enabled, 0, ""); 17838efa82bSJohn Dyson #else 179ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 180ceb0cf87SJohn Dyson CTLFLAG_RW, &vm_swap_enabled, 0, ""); 181ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 182ceb0cf87SJohn Dyson CTLFLAG_RW, &vm_swap_idle_enabled, 0, ""); 18338efa82bSJohn Dyson #endif 18426f9a767SRodney W. Grimes 185ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts, 18612ac6a1dSJohn Dyson CTLFLAG_RW, &defer_swap_pageouts, 0, ""); 18712ac6a1dSJohn Dyson 188ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts, 18912ac6a1dSJohn Dyson CTLFLAG_RW, &disable_swap_pageouts, 0, ""); 19012ac6a1dSJohn Dyson 191ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, max_page_launder, 192ceb0cf87SJohn Dyson CTLFLAG_RW, &max_page_launder, 0, ""); 19370111b90SJohn Dyson 19426f9a767SRodney W. Grimes 195b44e4b7aSJohn Dyson #define VM_PAGEOUT_PAGE_COUNT 8 196bbc0ec52SDavid Greenman int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT; 197df8bae1dSRodney W. Grimes 198c3cb3e12SDavid Greenman int vm_page_max_wired; /* XXX max # of wired pages system-wide */ 199df8bae1dSRodney W. Grimes 20038efa82bSJohn Dyson #if !defined(NO_SWAPPING) 20138efa82bSJohn Dyson typedef void freeer_fcn_t __P((vm_map_t, vm_object_t, vm_pindex_t, int)); 20238efa82bSJohn Dyson static void vm_pageout_map_deactivate_pages __P((vm_map_t, vm_pindex_t)); 203cd41fc12SDavid Greenman static freeer_fcn_t vm_pageout_object_deactivate_pages; 204cd41fc12SDavid Greenman static void vm_req_vmdaemon __P((void)); 20538efa82bSJohn Dyson #endif 206dc2efb27SJohn Dyson static void vm_pageout_page_stats(void); 2075985940eSJohn Dyson void pmap_collect(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 21526f9a767SRodney W. Grimes * block. 21626f9a767SRodney W. Grimes * 2170d94caffSDavid Greenman * And we set pageout-in-progress to keep the object from disappearing 2180d94caffSDavid Greenman * during pageout. This guarantees that the page won't move from the 2190d94caffSDavid Greenman * inactive queue. (However, any other page on the inactive queue may 2200d94caffSDavid Greenman * move!) 22126f9a767SRodney W. Grimes */ 2223af76890SPoul-Henning Kamp static int 22324a1cce3SDavid Greenman vm_pageout_clean(m, sync) 22424a1cce3SDavid Greenman vm_page_t m; 22524a1cce3SDavid Greenman int sync; 22624a1cce3SDavid Greenman { 22726f9a767SRodney W. Grimes register vm_object_t object; 228f35329acSJohn Dyson vm_page_t mc[2*vm_pageout_page_count]; 22924a1cce3SDavid Greenman int pageout_count; 23024a1cce3SDavid Greenman int i, forward_okay, backward_okay, page_base; 231a316d390SJohn Dyson vm_pindex_t pindex = m->pindex; 23226f9a767SRodney W. Grimes 23326f9a767SRodney W. Grimes object = m->object; 23424a1cce3SDavid Greenman 23526f9a767SRodney W. Grimes /* 23624a1cce3SDavid Greenman * If not OBJT_SWAP, additional memory may be needed to do the pageout. 23724a1cce3SDavid Greenman * Try to avoid the deadlock. 23826f9a767SRodney W. Grimes */ 23924a1cce3SDavid Greenman if ((sync != VM_PAGEOUT_FORCE) && 240f35329acSJohn Dyson (object->type == OBJT_DEFAULT) && 24124a1cce3SDavid Greenman ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min)) 24226f9a767SRodney W. Grimes return 0; 24326f9a767SRodney W. Grimes 24424a1cce3SDavid Greenman /* 24524a1cce3SDavid Greenman * Don't mess with the page if it's busy. 24624a1cce3SDavid Greenman */ 247f6b04d2bSDavid Greenman if ((!sync && m->hold_count != 0) || 2480d94caffSDavid Greenman ((m->busy != 0) || (m->flags & PG_BUSY))) 2490d94caffSDavid Greenman return 0; 2500d94caffSDavid Greenman 25124a1cce3SDavid Greenman /* 25224a1cce3SDavid Greenman * Try collapsing before it's too late. 25324a1cce3SDavid Greenman */ 25424a1cce3SDavid Greenman if (!sync && object->backing_object) { 25526f9a767SRodney W. Grimes vm_object_collapse(object); 25626f9a767SRodney W. Grimes } 2573c018e72SJohn Dyson 258f35329acSJohn Dyson mc[vm_pageout_page_count] = m; 25926f9a767SRodney W. Grimes pageout_count = 1; 260f35329acSJohn Dyson page_base = vm_pageout_page_count; 26124a1cce3SDavid Greenman forward_okay = TRUE; 262a316d390SJohn Dyson if (pindex != 0) 26324a1cce3SDavid Greenman backward_okay = TRUE; 26426f9a767SRodney W. Grimes else 26524a1cce3SDavid Greenman backward_okay = FALSE; 26624a1cce3SDavid Greenman /* 26724a1cce3SDavid Greenman * Scan object for clusterable pages. 26824a1cce3SDavid Greenman * 26924a1cce3SDavid Greenman * We can cluster ONLY if: ->> the page is NOT 27024a1cce3SDavid Greenman * clean, wired, busy, held, or mapped into a 27124a1cce3SDavid Greenman * buffer, and one of the following: 27224a1cce3SDavid Greenman * 1) The page is inactive, or a seldom used 27324a1cce3SDavid Greenman * active page. 27424a1cce3SDavid Greenman * -or- 27524a1cce3SDavid Greenman * 2) we force the issue. 27624a1cce3SDavid Greenman */ 27724a1cce3SDavid Greenman for (i = 1; (i < vm_pageout_page_count) && (forward_okay || backward_okay); i++) { 27824a1cce3SDavid Greenman vm_page_t p; 279f6b04d2bSDavid Greenman 28024a1cce3SDavid Greenman /* 28124a1cce3SDavid Greenman * See if forward page is clusterable. 28224a1cce3SDavid Greenman */ 28324a1cce3SDavid Greenman if (forward_okay) { 28424a1cce3SDavid Greenman /* 28524a1cce3SDavid Greenman * Stop forward scan at end of object. 28624a1cce3SDavid Greenman */ 287a316d390SJohn Dyson if ((pindex + i) > object->size) { 28824a1cce3SDavid Greenman forward_okay = FALSE; 28924a1cce3SDavid Greenman goto do_backward; 290f6b04d2bSDavid Greenman } 291a316d390SJohn Dyson p = vm_page_lookup(object, pindex + i); 29224a1cce3SDavid Greenman if (p) { 2935070c7f8SJohn Dyson if (((p->queue - p->pc) == PQ_CACHE) || 2945070c7f8SJohn Dyson (p->flags & PG_BUSY) || p->busy) { 29524a1cce3SDavid Greenman forward_okay = FALSE; 29624a1cce3SDavid Greenman goto do_backward; 297f6b04d2bSDavid Greenman } 29824a1cce3SDavid Greenman vm_page_test_dirty(p); 29924a1cce3SDavid Greenman if ((p->dirty & p->valid) != 0 && 300bd7e5f99SJohn Dyson ((p->queue == PQ_INACTIVE) || 30124a1cce3SDavid Greenman (sync == VM_PAGEOUT_FORCE)) && 30224a1cce3SDavid Greenman (p->wire_count == 0) && 30324a1cce3SDavid Greenman (p->hold_count == 0)) { 304f35329acSJohn Dyson mc[vm_pageout_page_count + i] = p; 30524a1cce3SDavid Greenman pageout_count++; 30624a1cce3SDavid Greenman if (pageout_count == vm_pageout_page_count) 30724a1cce3SDavid Greenman break; 30824a1cce3SDavid Greenman } else { 30924a1cce3SDavid Greenman forward_okay = FALSE; 310f6b04d2bSDavid Greenman } 31124a1cce3SDavid Greenman } else { 31224a1cce3SDavid Greenman forward_okay = FALSE; 31324a1cce3SDavid Greenman } 31424a1cce3SDavid Greenman } 31524a1cce3SDavid Greenman do_backward: 31624a1cce3SDavid Greenman /* 31724a1cce3SDavid Greenman * See if backward page is clusterable. 31824a1cce3SDavid Greenman */ 31924a1cce3SDavid Greenman if (backward_okay) { 32024a1cce3SDavid Greenman /* 32124a1cce3SDavid Greenman * Stop backward scan at beginning of object. 32224a1cce3SDavid Greenman */ 323a316d390SJohn Dyson if ((pindex - i) == 0) { 32424a1cce3SDavid Greenman backward_okay = FALSE; 32524a1cce3SDavid Greenman } 326a316d390SJohn Dyson p = vm_page_lookup(object, pindex - i); 32724a1cce3SDavid Greenman if (p) { 3285070c7f8SJohn Dyson if (((p->queue - p->pc) == PQ_CACHE) || 3295070c7f8SJohn Dyson (p->flags & PG_BUSY) || p->busy) { 33024a1cce3SDavid Greenman backward_okay = FALSE; 33124a1cce3SDavid Greenman continue; 33224a1cce3SDavid Greenman } 33324a1cce3SDavid Greenman vm_page_test_dirty(p); 33424a1cce3SDavid Greenman if ((p->dirty & p->valid) != 0 && 335bd7e5f99SJohn Dyson ((p->queue == PQ_INACTIVE) || 33624a1cce3SDavid Greenman (sync == VM_PAGEOUT_FORCE)) && 33724a1cce3SDavid Greenman (p->wire_count == 0) && 33824a1cce3SDavid Greenman (p->hold_count == 0)) { 339f35329acSJohn Dyson mc[vm_pageout_page_count - i] = p; 34024a1cce3SDavid Greenman pageout_count++; 34124a1cce3SDavid Greenman page_base--; 34224a1cce3SDavid Greenman if (pageout_count == vm_pageout_page_count) 34324a1cce3SDavid Greenman break; 34424a1cce3SDavid Greenman } else { 34524a1cce3SDavid Greenman backward_okay = FALSE; 34624a1cce3SDavid Greenman } 34724a1cce3SDavid Greenman } else { 34824a1cce3SDavid Greenman backward_okay = FALSE; 34924a1cce3SDavid Greenman } 350f6b04d2bSDavid Greenman } 351f6b04d2bSDavid Greenman } 352f6b04d2bSDavid Greenman 35367bf6868SJohn Dyson /* 35467bf6868SJohn Dyson * we allow reads during pageouts... 35567bf6868SJohn Dyson */ 35624a1cce3SDavid Greenman for (i = page_base; i < (page_base + pageout_count); i++) { 35724a1cce3SDavid Greenman mc[i]->flags |= PG_BUSY; 35867bf6868SJohn Dyson vm_page_protect(mc[i], VM_PROT_READ); 35926f9a767SRodney W. Grimes } 36026f9a767SRodney W. Grimes 361aef922f5SJohn Dyson return vm_pageout_flush(&mc[page_base], pageout_count, sync); 362aef922f5SJohn Dyson } 363aef922f5SJohn Dyson 364aef922f5SJohn Dyson int 365aef922f5SJohn Dyson vm_pageout_flush(mc, count, sync) 366aef922f5SJohn Dyson vm_page_t *mc; 367aef922f5SJohn Dyson int count; 368aef922f5SJohn Dyson int sync; 369aef922f5SJohn Dyson { 370aef922f5SJohn Dyson register vm_object_t object; 371aef922f5SJohn Dyson int pageout_status[count]; 37295461b45SJohn Dyson int numpagedout = 0; 373aef922f5SJohn Dyson int i; 374aef922f5SJohn Dyson 375aef922f5SJohn Dyson object = mc[0]->object; 376aef922f5SJohn Dyson object->paging_in_progress += count; 377aef922f5SJohn Dyson 378aef922f5SJohn Dyson vm_pager_put_pages(object, mc, count, 37926f9a767SRodney W. Grimes ((sync || (object == kernel_object)) ? TRUE : FALSE), 38026f9a767SRodney W. Grimes pageout_status); 38126f9a767SRodney W. Grimes 382aef922f5SJohn Dyson for (i = 0; i < count; i++) { 383aef922f5SJohn Dyson vm_page_t mt = mc[i]; 38424a1cce3SDavid Greenman 38526f9a767SRodney W. Grimes switch (pageout_status[i]) { 38626f9a767SRodney W. Grimes case VM_PAGER_OK: 38795461b45SJohn Dyson numpagedout++; 38826f9a767SRodney W. Grimes break; 38926f9a767SRodney W. Grimes case VM_PAGER_PEND: 39095461b45SJohn Dyson numpagedout++; 39126f9a767SRodney W. Grimes break; 39226f9a767SRodney W. Grimes case VM_PAGER_BAD: 39326f9a767SRodney W. Grimes /* 3940d94caffSDavid Greenman * Page outside of range of object. Right now we 3950d94caffSDavid Greenman * essentially lose the changes by pretending it 3960d94caffSDavid Greenman * worked. 39726f9a767SRodney W. Grimes */ 39867bf6868SJohn Dyson pmap_clear_modify(VM_PAGE_TO_PHYS(mt)); 39924a1cce3SDavid Greenman mt->dirty = 0; 40026f9a767SRodney W. Grimes break; 40126f9a767SRodney W. Grimes case VM_PAGER_ERROR: 40226f9a767SRodney W. Grimes case VM_PAGER_FAIL: 40326f9a767SRodney W. Grimes /* 4040d94caffSDavid Greenman * If page couldn't be paged out, then reactivate the 4050d94caffSDavid Greenman * page so it doesn't clog the inactive list. (We 4060d94caffSDavid Greenman * will try paging out it again later). 40726f9a767SRodney W. Grimes */ 40824a1cce3SDavid Greenman vm_page_activate(mt); 40926f9a767SRodney W. Grimes break; 41026f9a767SRodney W. Grimes case VM_PAGER_AGAIN: 41126f9a767SRodney W. Grimes break; 41226f9a767SRodney W. Grimes } 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); 42324a1cce3SDavid Greenman PAGE_WAKEUP(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) { 45638efa82bSJohn Dyson if (vm_map_pmap(map)->pm_stats.resident_count <= 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; 47138efa82bSJohn Dyson if (vm_map_pmap(map)->pm_stats.resident_count <= 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) { 486ef743ce6SJohn Dyson p->flags |= 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; 495c8c4b40cSJohn Dyson p->flags &= ~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); 510a647a309SDavid Greenman p->flags &= ~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) { 552afa07f7eSJohn Dyson if ((tmpe->eflags & (MAP_ENTRY_IS_A_MAP|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) { 57238efa82bSJohn Dyson if (vm_map_pmap(map)->pm_stats.resident_count <= desired) 57338efa82bSJohn Dyson break; 574afa07f7eSJohn Dyson if ((tmpe->eflags & (MAP_ENTRY_IS_A_MAP|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 594925a3a41SJohn Dyson void 595925a3a41SJohn Dyson vm_pageout_page_free(vm_page_t m) { 59647221757SJohn Dyson struct vnode *vp; 59747221757SJohn Dyson vm_object_t object; 598925a3a41SJohn Dyson 59947221757SJohn Dyson object = m->object; 60047221757SJohn Dyson object->ref_count++; 601eaf13dd7SJohn Dyson 60247221757SJohn Dyson if (object->type == OBJT_VNODE) { 60347221757SJohn Dyson vp = object->handle; 60447221757SJohn Dyson vp->v_usecount++; 60547221757SJohn Dyson if (VSHOULDBUSY(vp)) 60647221757SJohn Dyson vbusy(vp); 607925a3a41SJohn Dyson } 608eaf13dd7SJohn Dyson 60947221757SJohn Dyson m->flags |= PG_BUSY; 610925a3a41SJohn Dyson vm_page_protect(m, VM_PROT_NONE); 611925a3a41SJohn Dyson vm_page_free(m); 61247221757SJohn Dyson vm_object_deallocate(object); 613925a3a41SJohn Dyson } 614925a3a41SJohn Dyson 615df8bae1dSRodney W. Grimes /* 616df8bae1dSRodney W. Grimes * vm_pageout_scan does the dirty work for the pageout daemon. 617df8bae1dSRodney W. Grimes */ 6183af76890SPoul-Henning Kamp static int 619df8bae1dSRodney W. Grimes vm_pageout_scan() 620df8bae1dSRodney W. Grimes { 621502ba6e4SJohn Dyson vm_page_t m, next; 62270111b90SJohn Dyson int page_shortage, addl_page_shortage, maxscan, pcount; 62370111b90SJohn Dyson int maxlaunder; 6244e39a515SPoul-Henning Kamp int pages_freed; 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 6385985940eSJohn Dyson /* 6390d94caffSDavid Greenman * Start scanning the inactive queue for pages we can free. We keep 6400d94caffSDavid Greenman * scanning until we have enough free pages or we have scanned through 6410d94caffSDavid Greenman * the entire queue. If we encounter dirty pages, we start cleaning 6420d94caffSDavid Greenman * them. 643df8bae1dSRodney W. Grimes */ 644df8bae1dSRodney W. Grimes 645b182ec9eSJohn Dyson pages_freed = 0; 6462d8acc0fSJohn Dyson addl_page_shortage = vm_pageout_deficit; 64795461b45SJohn Dyson vm_pageout_deficit = 0; 648b182ec9eSJohn Dyson 649ceb0cf87SJohn Dyson if (max_page_launder == 0) 650ceb0cf87SJohn Dyson max_page_launder = 1; 651ceb0cf87SJohn Dyson maxlaunder = (cnt.v_inactive_target > max_page_launder) ? 652ceb0cf87SJohn Dyson max_page_launder : cnt.v_inactive_target; 65370111b90SJohn Dyson 65467bf6868SJohn Dyson rescan0: 655f6b04d2bSDavid Greenman maxscan = cnt.v_inactive_count; 656b182ec9eSJohn Dyson for( m = TAILQ_FIRST(&vm_page_queue_inactive); 657b182ec9eSJohn Dyson 658b182ec9eSJohn Dyson (m != NULL) && (maxscan-- > 0) && 659b18bfc3dSJohn Dyson ((cnt.v_cache_count + cnt.v_free_count) < 660b182ec9eSJohn Dyson (cnt.v_cache_min + cnt.v_free_target)); 661b182ec9eSJohn Dyson 662b182ec9eSJohn Dyson m = next) { 663df8bae1dSRodney W. Grimes 664a58d1fa1SDavid Greenman cnt.v_pdpages++; 665b182ec9eSJohn Dyson 666f35329acSJohn Dyson if (m->queue != PQ_INACTIVE) { 66767bf6868SJohn Dyson goto rescan0; 668f35329acSJohn Dyson } 669b182ec9eSJohn Dyson 670b18bfc3dSJohn Dyson next = TAILQ_NEXT(m, pageq); 671df8bae1dSRodney W. Grimes 672b182ec9eSJohn Dyson if (m->hold_count) { 673f35329acSJohn Dyson s = splvm(); 674b182ec9eSJohn Dyson TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq); 675b182ec9eSJohn Dyson TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq); 676f35329acSJohn Dyson splx(s); 677b182ec9eSJohn Dyson addl_page_shortage++; 678b182ec9eSJohn Dyson continue; 679df8bae1dSRodney W. Grimes } 68026f9a767SRodney W. Grimes /* 681b18bfc3dSJohn Dyson * Dont mess with busy pages, keep in the front of the 682b18bfc3dSJohn Dyson * queue, most likely are being paged out. 68326f9a767SRodney W. Grimes */ 684bd7e5f99SJohn Dyson if (m->busy || (m->flags & PG_BUSY)) { 685b182ec9eSJohn Dyson addl_page_shortage++; 68626f9a767SRodney W. Grimes continue; 68726f9a767SRodney W. Grimes } 688bd7e5f99SJohn Dyson 6897e006499SJohn Dyson /* 6907e006499SJohn Dyson * If the object is not being used, we ignore previous references. 6917e006499SJohn Dyson */ 6920d94caffSDavid Greenman if (m->object->ref_count == 0) { 6930d94caffSDavid Greenman m->flags &= ~PG_REFERENCED; 69467bf6868SJohn Dyson pmap_clear_reference(VM_PAGE_TO_PHYS(m)); 6957e006499SJohn Dyson 6967e006499SJohn Dyson /* 6977e006499SJohn Dyson * Otherwise, if the page has been referenced while in the inactive 6987e006499SJohn Dyson * queue, we bump the "activation count" upwards, making it less 6997e006499SJohn Dyson * likely that the page will be added back to the inactive queue 7007e006499SJohn Dyson * prematurely again. Here we check the page tables (or emulated 7017e006499SJohn Dyson * bits, if any), given the upper level VM system not knowing anything 7027e006499SJohn Dyson * about existing references. 7037e006499SJohn Dyson */ 704ef743ce6SJohn Dyson } else if (((m->flags & PG_REFERENCED) == 0) && 7057e006499SJohn Dyson (actcount = pmap_ts_referenced(VM_PAGE_TO_PHYS(m)))) { 706ef743ce6SJohn Dyson vm_page_activate(m); 7077e006499SJohn Dyson m->act_count += (actcount + ACT_ADVANCE); 708ef743ce6SJohn Dyson continue; 7092fe6e4d7SDavid Greenman } 710ef743ce6SJohn Dyson 7117e006499SJohn Dyson /* 7127e006499SJohn Dyson * If the upper level VM system knows about any page references, 7137e006499SJohn Dyson * we activate the page. We also set the "activation count" higher 7147e006499SJohn Dyson * than normal so that we will less likely place pages back onto the 7157e006499SJohn Dyson * inactive queue again. 7167e006499SJohn Dyson */ 717bd7e5f99SJohn Dyson if ((m->flags & PG_REFERENCED) != 0) { 718a647a309SDavid Greenman m->flags &= ~PG_REFERENCED; 7197e006499SJohn Dyson actcount = pmap_ts_referenced(VM_PAGE_TO_PHYS(m)); 72026f9a767SRodney W. Grimes vm_page_activate(m); 7217e006499SJohn Dyson m->act_count += (actcount + ACT_ADVANCE + 1); 7220d94caffSDavid Greenman continue; 7230d94caffSDavid Greenman } 72467bf6868SJohn Dyson 7257e006499SJohn Dyson /* 7267e006499SJohn Dyson * If the upper level VM system doesn't know anything about the 7277e006499SJohn Dyson * page being dirty, we have to check for it again. As far as the 7287e006499SJohn Dyson * VM code knows, any partially dirty pages are fully dirty. 7297e006499SJohn Dyson */ 730f6b04d2bSDavid Greenman if (m->dirty == 0) { 731bd7e5f99SJohn Dyson vm_page_test_dirty(m); 73230dcfc09SJohn Dyson } else if (m->dirty != 0) { 733bd7e5f99SJohn Dyson m->dirty = VM_PAGE_BITS_ALL; 73430dcfc09SJohn Dyson } 735ef743ce6SJohn Dyson 7367e006499SJohn Dyson /* 7377e006499SJohn Dyson * Invalid pages can be easily freed 7387e006499SJohn Dyson */ 7396d40c3d3SDavid Greenman if (m->valid == 0) { 740925a3a41SJohn Dyson vm_pageout_page_free(m); 74167bf6868SJohn Dyson cnt.v_dfree++; 742925a3a41SJohn Dyson pages_freed++; 7437e006499SJohn Dyson 7447e006499SJohn Dyson /* 7457e006499SJohn Dyson * Clean pages can be placed onto the cache queue. 7467e006499SJohn Dyson */ 747bd7e5f99SJohn Dyson } else if (m->dirty == 0) { 748bd7e5f99SJohn Dyson vm_page_cache(m); 749925a3a41SJohn Dyson pages_freed++; 7507e006499SJohn Dyson 7517e006499SJohn Dyson /* 7527e006499SJohn Dyson * Dirty pages need to be paged out. Note that we clean 7537e006499SJohn Dyson * only a limited number of pages per pagedaemon pass. 7547e006499SJohn Dyson */ 7550d94caffSDavid Greenman } else if (maxlaunder > 0) { 7560d94caffSDavid Greenman int written; 75712ac6a1dSJohn Dyson int swap_pageouts_ok; 758f6b04d2bSDavid Greenman struct vnode *vp = NULL; 7590d94caffSDavid Greenman 7600d94caffSDavid Greenman object = m->object; 7617e006499SJohn Dyson 76212ac6a1dSJohn Dyson if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) { 76312ac6a1dSJohn Dyson swap_pageouts_ok = 1; 76412ac6a1dSJohn Dyson } else { 76512ac6a1dSJohn Dyson swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts); 76612ac6a1dSJohn Dyson swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts && 76712ac6a1dSJohn Dyson (cnt.v_free_count + cnt.v_cache_count) < cnt.v_free_min); 76812ac6a1dSJohn Dyson 76912ac6a1dSJohn Dyson } 77070111b90SJohn Dyson 77170111b90SJohn Dyson /* 77270111b90SJohn Dyson * We don't bother paging objects that are "dead". Those 77370111b90SJohn Dyson * objects are in a "rundown" state. 77470111b90SJohn Dyson */ 77570111b90SJohn Dyson if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) { 77612ac6a1dSJohn Dyson s = splvm(); 77712ac6a1dSJohn Dyson TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq); 77812ac6a1dSJohn Dyson TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq); 77912ac6a1dSJohn Dyson splx(s); 78012ac6a1dSJohn Dyson continue; 78112ac6a1dSJohn Dyson } 78212ac6a1dSJohn Dyson 783eaf13dd7SJohn Dyson if ((object->type == OBJT_VNODE) && 784eaf13dd7SJohn Dyson (object->flags & OBJ_DEAD) == 0) { 78524a1cce3SDavid Greenman vp = object->handle; 786996c772fSJohn Dyson if (VOP_ISLOCKED(vp) || 78747221757SJohn Dyson vget(vp, LK_EXCLUSIVE|LK_NOOBJ, curproc)) { 788b182ec9eSJohn Dyson if ((m->queue == PQ_INACTIVE) && 789b182ec9eSJohn Dyson (m->hold_count == 0) && 790b182ec9eSJohn Dyson (m->busy == 0) && 791b182ec9eSJohn Dyson (m->flags & PG_BUSY) == 0) { 792f35329acSJohn Dyson s = splvm(); 79385a376ebSJohn Dyson TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq); 79485a376ebSJohn Dyson TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq); 795f35329acSJohn Dyson splx(s); 79685a376ebSJohn Dyson } 797aef922f5SJohn Dyson if (object->flags & OBJ_MIGHTBEDIRTY) 798925a3a41SJohn Dyson vnodes_skipped++; 799b182ec9eSJohn Dyson continue; 80085a376ebSJohn Dyson } 801b182ec9eSJohn Dyson 802f35329acSJohn Dyson /* 803f35329acSJohn Dyson * The page might have been moved to another queue 804f35329acSJohn Dyson * during potential blocking in vget() above. 805f35329acSJohn Dyson */ 806b182ec9eSJohn Dyson if (m->queue != PQ_INACTIVE) { 807b182ec9eSJohn Dyson if (object->flags & OBJ_MIGHTBEDIRTY) 808925a3a41SJohn Dyson vnodes_skipped++; 809b182ec9eSJohn Dyson vput(vp); 810b182ec9eSJohn Dyson continue; 811b182ec9eSJohn Dyson } 812b182ec9eSJohn Dyson 813f35329acSJohn Dyson /* 814f35329acSJohn Dyson * The page may have been busied during the blocking in 815f35329acSJohn Dyson * vput(); We don't move the page back onto the end of 816f35329acSJohn Dyson * the queue so that statistics are more correct if we don't. 817f35329acSJohn Dyson */ 818b182ec9eSJohn Dyson if (m->busy || (m->flags & PG_BUSY)) { 819b182ec9eSJohn Dyson vput(vp); 820b182ec9eSJohn Dyson continue; 821b182ec9eSJohn Dyson } 822b182ec9eSJohn Dyson 823f35329acSJohn Dyson /* 824f35329acSJohn Dyson * If the page has become held, then skip it 825f35329acSJohn Dyson */ 826b182ec9eSJohn Dyson if (m->hold_count) { 827f35329acSJohn Dyson s = splvm(); 828b182ec9eSJohn Dyson TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq); 829b182ec9eSJohn Dyson TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq); 830f35329acSJohn Dyson splx(s); 831b182ec9eSJohn Dyson if (object->flags & OBJ_MIGHTBEDIRTY) 832925a3a41SJohn Dyson vnodes_skipped++; 833b182ec9eSJohn Dyson vput(vp); 834f6b04d2bSDavid Greenman continue; 835f6b04d2bSDavid Greenman } 836f6b04d2bSDavid Greenman } 837f6b04d2bSDavid Greenman 8380d94caffSDavid Greenman /* 8390d94caffSDavid Greenman * If a page is dirty, then it is either being washed 8400d94caffSDavid Greenman * (but not yet cleaned) or it is still in the 8410d94caffSDavid Greenman * laundry. If it is still in the laundry, then we 8420d94caffSDavid Greenman * start the cleaning operation. 8430d94caffSDavid Greenman */ 8440d94caffSDavid Greenman written = vm_pageout_clean(m, 0); 845f6b04d2bSDavid Greenman if (vp) 846f6b04d2bSDavid Greenman vput(vp); 847f6b04d2bSDavid Greenman 8480d94caffSDavid Greenman maxlaunder -= written; 8490d94caffSDavid Greenman } 850df8bae1dSRodney W. Grimes } 85126f9a767SRodney W. Grimes 852df8bae1dSRodney W. Grimes /* 8530d94caffSDavid Greenman * Compute the page shortage. If we are still very low on memory be 8540d94caffSDavid Greenman * sure that we will move a minimal amount of pages from active to 8550d94caffSDavid Greenman * inactive. 856df8bae1dSRodney W. Grimes */ 857b182ec9eSJohn Dyson page_shortage = (cnt.v_inactive_target + cnt.v_cache_min) - 8580d94caffSDavid Greenman (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count); 85926f9a767SRodney W. Grimes if (page_shortage <= 0) { 86026f9a767SRodney W. Grimes if (pages_freed == 0) { 86117c4c408SDavid Greenman page_shortage = cnt.v_free_min - cnt.v_free_count; 862f6b04d2bSDavid Greenman } else { 863f6b04d2bSDavid Greenman page_shortage = 1; 86426f9a767SRodney W. Grimes } 865df8bae1dSRodney W. Grimes } 8667e006499SJohn Dyson 8677e006499SJohn Dyson /* 8687e006499SJohn Dyson * If the "inactive" loop finds that there is a shortage over and 8697e006499SJohn Dyson * above the page statistics variables, then we need to accomodate 8707e006499SJohn Dyson * that. This avoids potential deadlocks due to pages being temporarily 8717e006499SJohn Dyson * busy for I/O or other types of temporary wiring. 8727e006499SJohn Dyson */ 873b182ec9eSJohn Dyson if (addl_page_shortage) { 874b182ec9eSJohn Dyson if (page_shortage < 0) 875b182ec9eSJohn Dyson page_shortage = 0; 876b182ec9eSJohn Dyson page_shortage += addl_page_shortage; 877b182ec9eSJohn Dyson } 87826f9a767SRodney W. Grimes 879b18bfc3dSJohn Dyson pcount = cnt.v_active_count; 880b18bfc3dSJohn Dyson m = TAILQ_FIRST(&vm_page_queue_active); 881b18bfc3dSJohn Dyson while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) { 882f35329acSJohn Dyson 8837e006499SJohn Dyson /* 8847e006499SJohn Dyson * This is a consistancy check, and should likely be a panic 8857e006499SJohn Dyson * or warning. 8867e006499SJohn Dyson */ 887f35329acSJohn Dyson if (m->queue != PQ_ACTIVE) { 88838efa82bSJohn Dyson break; 889f35329acSJohn Dyson } 890f35329acSJohn Dyson 891b18bfc3dSJohn Dyson next = TAILQ_NEXT(m, pageq); 892df8bae1dSRodney W. Grimes /* 89326f9a767SRodney W. Grimes * Don't deactivate pages that are busy. 894df8bae1dSRodney W. Grimes */ 895a647a309SDavid Greenman if ((m->busy != 0) || 8960d94caffSDavid Greenman (m->flags & PG_BUSY) || 897f6b04d2bSDavid Greenman (m->hold_count != 0)) { 898f35329acSJohn Dyson s = splvm(); 8996d40c3d3SDavid Greenman TAILQ_REMOVE(&vm_page_queue_active, m, pageq); 9006d40c3d3SDavid Greenman TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq); 901f35329acSJohn Dyson splx(s); 90226f9a767SRodney W. Grimes m = next; 90326f9a767SRodney W. Grimes continue; 904df8bae1dSRodney W. Grimes } 905b18bfc3dSJohn Dyson 906b18bfc3dSJohn Dyson /* 907b18bfc3dSJohn Dyson * The count for pagedaemon pages is done after checking the 908b18bfc3dSJohn Dyson * page for eligbility... 909b18bfc3dSJohn Dyson */ 910b18bfc3dSJohn Dyson cnt.v_pdpages++; 911ef743ce6SJohn Dyson 9127e006499SJohn Dyson /* 9137e006499SJohn Dyson * Check to see "how much" the page has been used. 9147e006499SJohn Dyson */ 9157e006499SJohn Dyson actcount = 0; 916ef743ce6SJohn Dyson if (m->object->ref_count != 0) { 917ef743ce6SJohn Dyson if (m->flags & PG_REFERENCED) { 9187e006499SJohn Dyson actcount += 1; 9190d94caffSDavid Greenman } 9207e006499SJohn Dyson actcount += pmap_ts_referenced(VM_PAGE_TO_PHYS(m)); 9217e006499SJohn Dyson if (actcount) { 9227e006499SJohn Dyson m->act_count += ACT_ADVANCE + actcount; 92338efa82bSJohn Dyson if (m->act_count > ACT_MAX) 92438efa82bSJohn Dyson m->act_count = ACT_MAX; 92538efa82bSJohn Dyson } 926b18bfc3dSJohn Dyson } 927ef743ce6SJohn Dyson 9287e006499SJohn Dyson /* 9297e006499SJohn Dyson * Since we have "tested" this bit, we need to clear it now. 9307e006499SJohn Dyson */ 931b18bfc3dSJohn Dyson m->flags &= ~PG_REFERENCED; 932ef743ce6SJohn Dyson 9337e006499SJohn Dyson /* 9347e006499SJohn Dyson * Only if an object is currently being used, do we use the 9357e006499SJohn Dyson * page activation count stats. 9367e006499SJohn Dyson */ 9377e006499SJohn Dyson if (actcount && (m->object->ref_count != 0)) { 938f35329acSJohn Dyson s = splvm(); 93926f9a767SRodney W. Grimes TAILQ_REMOVE(&vm_page_queue_active, m, pageq); 94026f9a767SRodney W. Grimes TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq); 941f35329acSJohn Dyson splx(s); 94226f9a767SRodney W. Grimes } else { 94338efa82bSJohn Dyson m->act_count -= min(m->act_count, ACT_DECLINE); 94438efa82bSJohn Dyson if (vm_pageout_algorithm_lru || 94538efa82bSJohn Dyson (m->object->ref_count == 0) || (m->act_count == 0)) { 946925a3a41SJohn Dyson page_shortage--; 947d4a272dbSJohn Dyson if (m->object->ref_count == 0) { 948ef743ce6SJohn Dyson vm_page_protect(m, VM_PROT_NONE); 949d4a272dbSJohn Dyson if (m->dirty == 0) 9500d94caffSDavid Greenman vm_page_cache(m); 951d4a272dbSJohn Dyson else 952d4a272dbSJohn Dyson vm_page_deactivate(m); 9530d94caffSDavid Greenman } else { 95426f9a767SRodney W. Grimes vm_page_deactivate(m); 955df8bae1dSRodney W. Grimes } 95638efa82bSJohn Dyson } else { 95738efa82bSJohn Dyson s = splvm(); 95838efa82bSJohn Dyson TAILQ_REMOVE(&vm_page_queue_active, m, pageq); 95938efa82bSJohn Dyson TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq); 96038efa82bSJohn Dyson splx(s); 96138efa82bSJohn Dyson } 962df8bae1dSRodney W. Grimes } 96326f9a767SRodney W. Grimes m = next; 96426f9a767SRodney W. Grimes } 965df8bae1dSRodney W. Grimes 966f35329acSJohn Dyson s = splvm(); 967df8bae1dSRodney W. Grimes /* 9680d94caffSDavid Greenman * We try to maintain some *really* free pages, this allows interrupt 9690d94caffSDavid Greenman * code to be guaranteed space. 970df8bae1dSRodney W. Grimes */ 971a1f6d91cSDavid Greenman while (cnt.v_free_count < cnt.v_free_reserved) { 9725070c7f8SJohn Dyson static int cache_rover = 0; 9735070c7f8SJohn Dyson m = vm_page_list_find(PQ_CACHE, cache_rover); 9740d94caffSDavid Greenman if (!m) 9750d94caffSDavid Greenman break; 9765070c7f8SJohn Dyson cache_rover = (cache_rover + PQ_PRIME2) & PQ_L2_MASK; 977925a3a41SJohn Dyson vm_pageout_page_free(m); 9780bb3a0d2SDavid Greenman cnt.v_dfree++; 97926f9a767SRodney W. Grimes } 980f35329acSJohn Dyson splx(s); 9815663e6deSDavid Greenman 982ceb0cf87SJohn Dyson #if !defined(NO_SWAPPING) 983ceb0cf87SJohn Dyson /* 984ceb0cf87SJohn Dyson * Idle process swapout -- run once per second. 985ceb0cf87SJohn Dyson */ 986ceb0cf87SJohn Dyson if (vm_swap_idle_enabled) { 987ceb0cf87SJohn Dyson static long lsec; 988ceb0cf87SJohn Dyson if (time.tv_sec != lsec) { 989ceb0cf87SJohn Dyson vm_pageout_req_swapout |= VM_SWAP_IDLE; 990ceb0cf87SJohn Dyson vm_req_vmdaemon(); 991ceb0cf87SJohn Dyson lsec = time.tv_sec; 992ceb0cf87SJohn Dyson } 993ceb0cf87SJohn Dyson } 994ceb0cf87SJohn Dyson #endif 995ceb0cf87SJohn Dyson 9965663e6deSDavid Greenman /* 997f6b04d2bSDavid Greenman * If we didn't get enough free pages, and we have skipped a vnode 9984c1f8ee9SDavid Greenman * in a writeable object, wakeup the sync daemon. And kick swapout 9994c1f8ee9SDavid Greenman * if we did not get enough free pages. 1000f6b04d2bSDavid Greenman */ 1001bd7e5f99SJohn Dyson if ((cnt.v_cache_count + cnt.v_free_count) < 1002bd7e5f99SJohn Dyson (cnt.v_free_target + cnt.v_cache_min) ) { 1003f6b04d2bSDavid Greenman if (vnodes_skipped && 1004f6b04d2bSDavid Greenman (cnt.v_cache_count + cnt.v_free_count) < cnt.v_free_min) { 1005f6b04d2bSDavid Greenman if (!vfs_update_wakeup) { 1006f6b04d2bSDavid Greenman vfs_update_wakeup = 1; 100724a1cce3SDavid Greenman wakeup(&vfs_update_wakeup); 1008f6b04d2bSDavid Greenman } 1009f6b04d2bSDavid Greenman } 101038efa82bSJohn Dyson #if !defined(NO_SWAPPING) 1011ceb0cf87SJohn Dyson if (vm_swap_enabled && 101238efa82bSJohn Dyson (cnt.v_free_count + cnt.v_cache_count < cnt.v_free_target)) { 10134c1f8ee9SDavid Greenman vm_req_vmdaemon(); 1014ceb0cf87SJohn Dyson vm_pageout_req_swapout |= VM_SWAP_NORMAL; 10154c1f8ee9SDavid Greenman } 10165afce282SDavid Greenman #endif 10174c1f8ee9SDavid Greenman } 10184c1f8ee9SDavid Greenman 1019f6b04d2bSDavid Greenman 1020f6b04d2bSDavid Greenman /* 10210d94caffSDavid Greenman * make sure that we have swap space -- if we are low on memory and 10220d94caffSDavid Greenman * swap -- then kill the biggest process. 10235663e6deSDavid Greenman */ 10245663e6deSDavid Greenman if ((vm_swap_size == 0 || swap_pager_full) && 10250d94caffSDavid Greenman ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_free_min)) { 10265663e6deSDavid Greenman bigproc = NULL; 10275663e6deSDavid Greenman bigsize = 0; 10281b67ec6dSJeffrey Hsu for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 10295663e6deSDavid Greenman /* 10305663e6deSDavid Greenman * if this is a system process, skip it 10315663e6deSDavid Greenman */ 103279221631SDavid Greenman if ((p->p_flag & P_SYSTEM) || (p->p_pid == 1) || 103379221631SDavid Greenman ((p->p_pid < 48) && (vm_swap_size != 0))) { 10345663e6deSDavid Greenman continue; 10355663e6deSDavid Greenman } 10365663e6deSDavid Greenman /* 10375663e6deSDavid Greenman * if the process is in a non-running type state, 10385663e6deSDavid Greenman * don't touch it. 10395663e6deSDavid Greenman */ 10405663e6deSDavid Greenman if (p->p_stat != SRUN && p->p_stat != SSLEEP) { 10415663e6deSDavid Greenman continue; 10425663e6deSDavid Greenman } 10435663e6deSDavid Greenman /* 10445663e6deSDavid Greenman * get the process size 10455663e6deSDavid Greenman */ 10465663e6deSDavid Greenman size = p->p_vmspace->vm_pmap.pm_stats.resident_count; 10475663e6deSDavid Greenman /* 10485663e6deSDavid Greenman * if the this process is bigger than the biggest one 10495663e6deSDavid Greenman * remember it. 10505663e6deSDavid Greenman */ 10515663e6deSDavid Greenman if (size > bigsize) { 10525663e6deSDavid Greenman bigproc = p; 10535663e6deSDavid Greenman bigsize = size; 10545663e6deSDavid Greenman } 10555663e6deSDavid Greenman } 10565663e6deSDavid Greenman if (bigproc != NULL) { 1057729b1e51SDavid Greenman killproc(bigproc, "out of swap space"); 10585663e6deSDavid Greenman bigproc->p_estcpu = 0; 10595663e6deSDavid Greenman bigproc->p_nice = PRIO_MIN; 10605663e6deSDavid Greenman resetpriority(bigproc); 106124a1cce3SDavid Greenman wakeup(&cnt.v_free_count); 10625663e6deSDavid Greenman } 10635663e6deSDavid Greenman } 106426f9a767SRodney W. Grimes return force_wakeup; 106526f9a767SRodney W. Grimes } 106626f9a767SRodney W. Grimes 1067dc2efb27SJohn Dyson /* 1068dc2efb27SJohn Dyson * This routine tries to maintain the pseudo LRU active queue, 1069dc2efb27SJohn Dyson * so that during long periods of time where there is no paging, 1070dc2efb27SJohn Dyson * that some statistic accumlation still occurs. This code 1071dc2efb27SJohn Dyson * helps the situation where paging just starts to occur. 1072dc2efb27SJohn Dyson */ 1073dc2efb27SJohn Dyson static void 1074dc2efb27SJohn Dyson vm_pageout_page_stats() 1075dc2efb27SJohn Dyson { 1076dc2efb27SJohn Dyson int s; 1077dc2efb27SJohn Dyson vm_page_t m,next; 1078dc2efb27SJohn Dyson int pcount,tpcount; /* Number of pages to check */ 1079dc2efb27SJohn Dyson static int fullintervalcount = 0; 1080dc2efb27SJohn Dyson 1081dc2efb27SJohn Dyson pcount = cnt.v_active_count; 1082dc2efb27SJohn Dyson fullintervalcount += vm_pageout_stats_interval; 1083dc2efb27SJohn Dyson if (fullintervalcount < vm_pageout_full_stats_interval) { 1084dc2efb27SJohn Dyson tpcount = (vm_pageout_stats_max * cnt.v_active_count) / cnt.v_page_count; 1085dc2efb27SJohn Dyson if (pcount > tpcount) 1086dc2efb27SJohn Dyson pcount = tpcount; 1087dc2efb27SJohn Dyson } 1088dc2efb27SJohn Dyson 1089dc2efb27SJohn Dyson m = TAILQ_FIRST(&vm_page_queue_active); 1090dc2efb27SJohn Dyson while ((m != NULL) && (pcount-- > 0)) { 10917e006499SJohn Dyson int actcount; 1092dc2efb27SJohn Dyson 1093dc2efb27SJohn Dyson if (m->queue != PQ_ACTIVE) { 1094dc2efb27SJohn Dyson break; 1095dc2efb27SJohn Dyson } 1096dc2efb27SJohn Dyson 1097dc2efb27SJohn Dyson next = TAILQ_NEXT(m, pageq); 1098dc2efb27SJohn Dyson /* 1099dc2efb27SJohn Dyson * Don't deactivate pages that are busy. 1100dc2efb27SJohn Dyson */ 1101dc2efb27SJohn Dyson if ((m->busy != 0) || 1102dc2efb27SJohn Dyson (m->flags & PG_BUSY) || 1103dc2efb27SJohn Dyson (m->hold_count != 0)) { 1104dc2efb27SJohn Dyson s = splvm(); 1105dc2efb27SJohn Dyson TAILQ_REMOVE(&vm_page_queue_active, m, pageq); 1106dc2efb27SJohn Dyson TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq); 1107dc2efb27SJohn Dyson splx(s); 1108dc2efb27SJohn Dyson m = next; 1109dc2efb27SJohn Dyson continue; 1110dc2efb27SJohn Dyson } 1111dc2efb27SJohn Dyson 11127e006499SJohn Dyson actcount = 0; 1113dc2efb27SJohn Dyson if (m->flags & PG_REFERENCED) { 1114dc2efb27SJohn Dyson m->flags &= ~PG_REFERENCED; 11157e006499SJohn Dyson actcount += 1; 1116dc2efb27SJohn Dyson } 1117dc2efb27SJohn Dyson 11187e006499SJohn Dyson actcount += pmap_ts_referenced(VM_PAGE_TO_PHYS(m)); 11197e006499SJohn Dyson if (actcount) { 11207e006499SJohn Dyson m->act_count += ACT_ADVANCE + actcount; 1121dc2efb27SJohn Dyson if (m->act_count > ACT_MAX) 1122dc2efb27SJohn Dyson m->act_count = ACT_MAX; 1123dc2efb27SJohn Dyson s = splvm(); 1124dc2efb27SJohn Dyson TAILQ_REMOVE(&vm_page_queue_active, m, pageq); 1125dc2efb27SJohn Dyson TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq); 1126dc2efb27SJohn Dyson splx(s); 1127dc2efb27SJohn Dyson } else { 1128dc2efb27SJohn Dyson if (m->act_count == 0) { 11297e006499SJohn Dyson /* 11307e006499SJohn Dyson * We turn off page access, so that we have more accurate 11317e006499SJohn Dyson * RSS stats. We don't do this in the normal page deactivation 11327e006499SJohn Dyson * when the system is loaded VM wise, because the cost of 11337e006499SJohn Dyson * the large number of page protect operations would be higher 11347e006499SJohn Dyson * than the value of doing the operation. 11357e006499SJohn Dyson */ 1136dc2efb27SJohn Dyson vm_page_protect(m, VM_PROT_NONE); 1137dc2efb27SJohn Dyson vm_page_deactivate(m); 1138dc2efb27SJohn Dyson } else { 1139dc2efb27SJohn Dyson m->act_count -= min(m->act_count, ACT_DECLINE); 1140dc2efb27SJohn Dyson s = splvm(); 1141dc2efb27SJohn Dyson TAILQ_REMOVE(&vm_page_queue_active, m, pageq); 1142dc2efb27SJohn Dyson TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq); 1143dc2efb27SJohn Dyson splx(s); 1144dc2efb27SJohn Dyson } 1145dc2efb27SJohn Dyson } 1146dc2efb27SJohn Dyson 1147dc2efb27SJohn Dyson m = next; 1148dc2efb27SJohn Dyson } 1149dc2efb27SJohn Dyson } 1150dc2efb27SJohn Dyson 1151dc2efb27SJohn Dyson 1152b182ec9eSJohn Dyson static int 1153b182ec9eSJohn Dyson vm_pageout_free_page_calc(count) 1154b182ec9eSJohn Dyson vm_size_t count; 1155b182ec9eSJohn Dyson { 1156b182ec9eSJohn Dyson if (count < cnt.v_page_count) 1157b182ec9eSJohn Dyson return 0; 1158b182ec9eSJohn Dyson /* 1159b182ec9eSJohn Dyson * free_reserved needs to include enough for the largest swap pager 1160b182ec9eSJohn Dyson * structures plus enough for any pv_entry structs when paging. 1161b182ec9eSJohn Dyson */ 1162b182ec9eSJohn Dyson if (cnt.v_page_count > 1024) 1163b182ec9eSJohn Dyson cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200; 1164b182ec9eSJohn Dyson else 1165b182ec9eSJohn Dyson cnt.v_free_min = 4; 1166f35329acSJohn Dyson cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE + 1167f35329acSJohn Dyson cnt.v_interrupt_free_min; 1168f35329acSJohn Dyson cnt.v_free_reserved = vm_pageout_page_count + 11692d8acc0fSJohn Dyson cnt.v_pageout_free_min + (count / 2048) + PQ_L2_SIZE; 1170a2f4a846SJohn Dyson cnt.v_free_min += cnt.v_free_reserved; 1171b182ec9eSJohn Dyson return 1; 1172b182ec9eSJohn Dyson } 1173b182ec9eSJohn Dyson 1174b182ec9eSJohn Dyson 1175df8bae1dSRodney W. Grimes /* 1176df8bae1dSRodney W. Grimes * vm_pageout is the high level pageout daemon. 1177df8bae1dSRodney W. Grimes */ 11782b14f991SJulian Elischer static void 117926f9a767SRodney W. Grimes vm_pageout() 1180df8bae1dSRodney W. Grimes { 1181df8bae1dSRodney W. Grimes /* 1182df8bae1dSRodney W. Grimes * Initialize some paging parameters. 1183df8bae1dSRodney W. Grimes */ 1184df8bae1dSRodney W. Grimes 1185f6b04d2bSDavid Greenman cnt.v_interrupt_free_min = 2; 1186f35329acSJohn Dyson if (cnt.v_page_count < 2000) 1187f35329acSJohn Dyson vm_pageout_page_count = 8; 1188f6b04d2bSDavid Greenman 1189b182ec9eSJohn Dyson vm_pageout_free_page_calc(cnt.v_page_count); 1190ed74321bSDavid Greenman /* 11910d94caffSDavid Greenman * free_reserved needs to include enough for the largest swap pager 11920d94caffSDavid Greenman * structures plus enough for any pv_entry structs when paging. 1193ed74321bSDavid Greenman */ 11940d94caffSDavid Greenman cnt.v_free_target = 3 * cnt.v_free_min + cnt.v_free_reserved; 11956f2b142eSDavid Greenman 11966ac5bfdbSJohn Dyson if (cnt.v_free_count > 1024) { 11970d94caffSDavid Greenman cnt.v_cache_max = (cnt.v_free_count - 1024) / 2; 11986f2b142eSDavid Greenman cnt.v_cache_min = (cnt.v_free_count - 1024) / 8; 11996f2b142eSDavid Greenman cnt.v_inactive_target = 2*cnt.v_cache_min + 192; 12000d94caffSDavid Greenman } else { 12010d94caffSDavid Greenman cnt.v_cache_min = 0; 12020d94caffSDavid Greenman cnt.v_cache_max = 0; 12036f2b142eSDavid Greenman cnt.v_inactive_target = cnt.v_free_count / 4; 12040d94caffSDavid Greenman } 1205df8bae1dSRodney W. Grimes 1206df8bae1dSRodney W. Grimes /* XXX does not really belong here */ 1207df8bae1dSRodney W. Grimes if (vm_page_max_wired == 0) 1208df8bae1dSRodney W. Grimes vm_page_max_wired = cnt.v_free_count / 3; 1209df8bae1dSRodney W. Grimes 1210dc2efb27SJohn Dyson if (vm_pageout_stats_max == 0) 1211dc2efb27SJohn Dyson vm_pageout_stats_max = cnt.v_free_target; 1212dc2efb27SJohn Dyson 1213dc2efb27SJohn Dyson /* 1214dc2efb27SJohn Dyson * Set interval in seconds for stats scan. 1215dc2efb27SJohn Dyson */ 1216dc2efb27SJohn Dyson if (vm_pageout_stats_interval == 0) 1217dc2efb27SJohn Dyson vm_pageout_stats_interval = 4; 1218dc2efb27SJohn Dyson if (vm_pageout_full_stats_interval == 0) 1219dc2efb27SJohn Dyson vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4; 1220dc2efb27SJohn Dyson 1221dc2efb27SJohn Dyson 1222dc2efb27SJohn Dyson /* 1223dc2efb27SJohn Dyson * Set maximum free per pass 1224dc2efb27SJohn Dyson */ 1225dc2efb27SJohn Dyson if (vm_pageout_stats_free_max == 0) 1226dc2efb27SJohn Dyson vm_pageout_stats_free_max = 25; 1227dc2efb27SJohn Dyson 1228ceb0cf87SJohn Dyson max_page_launder = (cnt.v_page_count > 1800 ? 32 : 16); 122926f9a767SRodney W. Grimes 123024a1cce3SDavid Greenman swap_pager_swap_init(); 1231df8bae1dSRodney W. Grimes /* 12320d94caffSDavid Greenman * The pageout daemon is never done, so loop forever. 1233df8bae1dSRodney W. Grimes */ 1234df8bae1dSRodney W. Grimes while (TRUE) { 123585a376ebSJohn Dyson int inactive_target; 1236dc2efb27SJohn Dyson int error; 1237b18bfc3dSJohn Dyson int s = splvm(); 1238f919ebdeSDavid Greenman if (!vm_pages_needed || 1239545901f7SJohn Dyson ((cnt.v_free_count + cnt.v_cache_count) > cnt.v_free_min)) { 1240f919ebdeSDavid Greenman vm_pages_needed = 0; 1241dc2efb27SJohn Dyson error = tsleep(&vm_pages_needed, 1242dc2efb27SJohn Dyson PVM, "psleep", vm_pageout_stats_interval * hz); 1243dc2efb27SJohn Dyson if (error && !vm_pages_needed) { 1244dc2efb27SJohn Dyson splx(s); 1245dc2efb27SJohn Dyson vm_pageout_page_stats(); 1246dc2efb27SJohn Dyson continue; 1247dc2efb27SJohn Dyson } 1248dc2efb27SJohn Dyson } else if (vm_pages_needed) { 124938efa82bSJohn Dyson tsleep(&vm_pages_needed, PVM, "psleep", hz/10); 1250f919ebdeSDavid Greenman } 1251b182ec9eSJohn Dyson inactive_target = 1252b182ec9eSJohn Dyson (cnt.v_page_count - cnt.v_wire_count) / 4; 1253b182ec9eSJohn Dyson if (inactive_target < 2*cnt.v_free_min) 1254b182ec9eSJohn Dyson inactive_target = 2*cnt.v_free_min; 1255b182ec9eSJohn Dyson cnt.v_inactive_target = inactive_target; 1256b18bfc3dSJohn Dyson if (vm_pages_needed) 1257b18bfc3dSJohn Dyson cnt.v_pdwakeups++; 1258f919ebdeSDavid Greenman vm_pages_needed = 0; 1259f919ebdeSDavid Greenman splx(s); 1260df8bae1dSRodney W. Grimes vm_pager_sync(); 12610d94caffSDavid Greenman vm_pageout_scan(); 12622d8acc0fSJohn Dyson vm_pageout_deficit = 0; 126326f9a767SRodney W. Grimes vm_pager_sync(); 126424a1cce3SDavid Greenman wakeup(&cnt.v_free_count); 1265df8bae1dSRodney W. Grimes } 1266df8bae1dSRodney W. Grimes } 126726f9a767SRodney W. Grimes 1268e0c5a895SJohn Dyson void 1269e0c5a895SJohn Dyson pagedaemon_wakeup() 1270e0c5a895SJohn Dyson { 1271e0c5a895SJohn Dyson if (!vm_pages_needed && curproc != pageproc) { 1272e0c5a895SJohn Dyson vm_pages_needed++; 1273e0c5a895SJohn Dyson wakeup(&vm_pages_needed); 1274e0c5a895SJohn Dyson } 1275e0c5a895SJohn Dyson } 1276e0c5a895SJohn Dyson 127738efa82bSJohn Dyson #if !defined(NO_SWAPPING) 12785afce282SDavid Greenman static void 12795afce282SDavid Greenman vm_req_vmdaemon() 12805afce282SDavid Greenman { 12815afce282SDavid Greenman static int lastrun = 0; 12825afce282SDavid Greenman 1283b18bfc3dSJohn Dyson if ((ticks > (lastrun + hz)) || (ticks < lastrun)) { 12845afce282SDavid Greenman wakeup(&vm_daemon_needed); 12855afce282SDavid Greenman lastrun = ticks; 12865afce282SDavid Greenman } 12875afce282SDavid Greenman } 12885afce282SDavid Greenman 12892b14f991SJulian Elischer static void 12904f9fb771SBruce Evans vm_daemon() 12910d94caffSDavid Greenman { 12922fe6e4d7SDavid Greenman vm_object_t object; 12932fe6e4d7SDavid Greenman struct proc *p; 12940d94caffSDavid Greenman 12952fe6e4d7SDavid Greenman while (TRUE) { 129624a1cce3SDavid Greenman tsleep(&vm_daemon_needed, PUSER, "psleep", 0); 12974c1f8ee9SDavid Greenman if (vm_pageout_req_swapout) { 1298ceb0cf87SJohn Dyson swapout_procs(vm_pageout_req_swapout); 12994c1f8ee9SDavid Greenman vm_pageout_req_swapout = 0; 13004c1f8ee9SDavid Greenman } 13012fe6e4d7SDavid Greenman /* 13020d94caffSDavid Greenman * scan the processes for exceeding their rlimits or if 13030d94caffSDavid Greenman * process is swapped out -- deactivate pages 13042fe6e4d7SDavid Greenman */ 13052fe6e4d7SDavid Greenman 13061b67ec6dSJeffrey Hsu for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 13072fe6e4d7SDavid Greenman quad_t limit; 13082fe6e4d7SDavid Greenman vm_offset_t size; 13092fe6e4d7SDavid Greenman 13102fe6e4d7SDavid Greenman /* 13112fe6e4d7SDavid Greenman * if this is a system process or if we have already 13122fe6e4d7SDavid Greenman * looked at this process, skip it. 13132fe6e4d7SDavid Greenman */ 13142fe6e4d7SDavid Greenman if (p->p_flag & (P_SYSTEM | P_WEXIT)) { 13152fe6e4d7SDavid Greenman continue; 13162fe6e4d7SDavid Greenman } 13172fe6e4d7SDavid Greenman /* 13182fe6e4d7SDavid Greenman * if the process is in a non-running type state, 13192fe6e4d7SDavid Greenman * don't touch it. 13202fe6e4d7SDavid Greenman */ 13212fe6e4d7SDavid Greenman if (p->p_stat != SRUN && p->p_stat != SSLEEP) { 13222fe6e4d7SDavid Greenman continue; 13232fe6e4d7SDavid Greenman } 13242fe6e4d7SDavid Greenman /* 13252fe6e4d7SDavid Greenman * get a limit 13262fe6e4d7SDavid Greenman */ 13272fe6e4d7SDavid Greenman limit = qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur, 13282fe6e4d7SDavid Greenman p->p_rlimit[RLIMIT_RSS].rlim_max); 13292fe6e4d7SDavid Greenman 13302fe6e4d7SDavid Greenman /* 13310d94caffSDavid Greenman * let processes that are swapped out really be 13320d94caffSDavid Greenman * swapped out set the limit to nothing (will force a 13330d94caffSDavid Greenman * swap-out.) 13342fe6e4d7SDavid Greenman */ 13352fe6e4d7SDavid Greenman if ((p->p_flag & P_INMEM) == 0) 13360d94caffSDavid Greenman limit = 0; /* XXX */ 13372fe6e4d7SDavid Greenman 1338a91c5a7eSJohn Dyson size = p->p_vmspace->vm_pmap.pm_stats.resident_count * PAGE_SIZE; 13392fe6e4d7SDavid Greenman if (limit >= 0 && size >= limit) { 13402fe6e4d7SDavid Greenman vm_pageout_map_deactivate_pages(&p->p_vmspace->vm_map, 134138efa82bSJohn Dyson (vm_pindex_t)(limit >> PAGE_SHIFT) ); 13422fe6e4d7SDavid Greenman } 13432fe6e4d7SDavid Greenman } 134424a1cce3SDavid Greenman } 13452fe6e4d7SDavid Greenman } 134638efa82bSJohn Dyson #endif 1347