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 * 683af76890SPoul-Henning Kamp * $Id: vm_pageout.c,v 1.59 1995/11/05 20:46:02 dyson Exp $ 69df8bae1dSRodney W. Grimes */ 70df8bae1dSRodney W. Grimes 71df8bae1dSRodney W. Grimes /* 72df8bae1dSRodney W. Grimes * The proverbial page-out daemon. 73df8bae1dSRodney W. Grimes */ 74df8bae1dSRodney W. Grimes 75df8bae1dSRodney W. Grimes #include <sys/param.h> 7626f9a767SRodney W. Grimes #include <sys/systm.h> 77b5e8ce9fSBruce Evans #include <sys/kernel.h> 7826f9a767SRodney W. Grimes #include <sys/proc.h> 7926f9a767SRodney W. Grimes #include <sys/resourcevar.h> 8026f9a767SRodney W. Grimes #include <sys/malloc.h> 810d94caffSDavid Greenman #include <sys/kernel.h> 82d2fc5315SPoul-Henning Kamp #include <sys/signalvar.h> 83f6b04d2bSDavid Greenman #include <sys/vnode.h> 84df8bae1dSRodney W. Grimes 85df8bae1dSRodney W. Grimes #include <vm/vm.h> 86df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 87df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h> 887c0414d0SDavid Greenman #include <vm/vm_kern.h> 8924a1cce3SDavid Greenman #include <vm/vm_pager.h> 9005f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h> 91df8bae1dSRodney W. Grimes 922b14f991SJulian Elischer /* 932b14f991SJulian Elischer * System initialization 942b14f991SJulian Elischer */ 952b14f991SJulian Elischer 962b14f991SJulian Elischer /* the kernel process "vm_pageout"*/ 972b14f991SJulian Elischer static void vm_pageout __P((void)); 983af76890SPoul-Henning Kamp static int vm_pageout_clean __P((vm_page_t, int)); 993af76890SPoul-Henning Kamp static int vm_pageout_scan __P((void)); 1002b14f991SJulian Elischer struct proc *pageproc; 1012b14f991SJulian Elischer 1022b14f991SJulian Elischer static struct kproc_desc page_kp = { 1032b14f991SJulian Elischer "pagedaemon", 1042b14f991SJulian Elischer vm_pageout, 1052b14f991SJulian Elischer &pageproc 1062b14f991SJulian Elischer }; 1074590fd3aSDavid Greenman SYSINIT_KT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, &page_kp) 1082b14f991SJulian Elischer 1092b14f991SJulian Elischer /* the kernel process "vm_daemon"*/ 1102b14f991SJulian Elischer static void vm_daemon __P((void)); 1112b14f991SJulian Elischer struct proc *vmproc; 1122b14f991SJulian Elischer 1132b14f991SJulian Elischer static struct kproc_desc vm_kp = { 1142b14f991SJulian Elischer "vmdaemon", 1152b14f991SJulian Elischer vm_daemon, 1162b14f991SJulian Elischer &vmproc 1172b14f991SJulian Elischer }; 1184590fd3aSDavid Greenman SYSINIT_KT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp) 1192b14f991SJulian Elischer 1202b14f991SJulian Elischer 121df8bae1dSRodney W. Grimes int vm_pages_needed; /* Event on which pageout daemon sleeps */ 12226f9a767SRodney W. Grimes 123c3cb3e12SDavid Greenman int vm_pageout_pages_needed; /* flag saying that the pageout daemon needs pages */ 12426f9a767SRodney W. Grimes 12526f9a767SRodney W. Grimes extern int npendingio; 1267c0414d0SDavid Greenman int vm_pageout_req_swapout; /* XXX */ 1272fe6e4d7SDavid Greenman int vm_daemon_needed; 12826f9a767SRodney W. Grimes extern int nswiodone; 1295663e6deSDavid Greenman extern int vm_swap_size; 130f6b04d2bSDavid Greenman extern int vfs_update_wakeup; 13126f9a767SRodney W. Grimes 1321ed81ef2SDavid Greenman #define MAXSCAN 1024 /* maximum number of pages to scan in queues */ 13326f9a767SRodney W. Grimes 1340d94caffSDavid Greenman #define MAXLAUNDER (cnt.v_page_count > 1800 ? 32 : 16) 13526f9a767SRodney W. Grimes 13626f9a767SRodney W. Grimes #define VM_PAGEOUT_PAGE_COUNT 8 137bbc0ec52SDavid Greenman int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT; 138df8bae1dSRodney W. Grimes 139c3cb3e12SDavid Greenman int vm_page_max_wired; /* XXX max # of wired pages system-wide */ 140df8bae1dSRodney W. Grimes 141cd41fc12SDavid Greenman typedef int freeer_fcn_t __P((vm_map_t, vm_object_t, int, int)); 142cd41fc12SDavid Greenman static void vm_pageout_map_deactivate_pages __P((vm_map_t, vm_map_entry_t, 143cd41fc12SDavid Greenman int *, freeer_fcn_t *)); 144cd41fc12SDavid Greenman static freeer_fcn_t vm_pageout_object_deactivate_pages; 145cd41fc12SDavid Greenman static void vm_req_vmdaemon __P((void)); 146cd41fc12SDavid Greenman 14726f9a767SRodney W. Grimes /* 14826f9a767SRodney W. Grimes * vm_pageout_clean: 14924a1cce3SDavid Greenman * 1500d94caffSDavid Greenman * Clean the page and remove it from the laundry. 15126f9a767SRodney W. Grimes * 1520d94caffSDavid Greenman * We set the busy bit to cause potential page faults on this page to 15326f9a767SRodney W. Grimes * block. 15426f9a767SRodney W. Grimes * 1550d94caffSDavid Greenman * And we set pageout-in-progress to keep the object from disappearing 1560d94caffSDavid Greenman * during pageout. This guarantees that the page won't move from the 1570d94caffSDavid Greenman * inactive queue. (However, any other page on the inactive queue may 1580d94caffSDavid Greenman * move!) 15926f9a767SRodney W. Grimes */ 1603af76890SPoul-Henning Kamp static int 16124a1cce3SDavid Greenman vm_pageout_clean(m, sync) 16224a1cce3SDavid Greenman vm_page_t m; 16324a1cce3SDavid Greenman int sync; 16424a1cce3SDavid Greenman { 16526f9a767SRodney W. Grimes register vm_object_t object; 16624a1cce3SDavid Greenman vm_page_t mc[2*VM_PAGEOUT_PAGE_COUNT]; 16724a1cce3SDavid Greenman int pageout_count; 16824a1cce3SDavid Greenman int i, forward_okay, backward_okay, page_base; 16926f9a767SRodney W. Grimes vm_offset_t offset = m->offset; 17026f9a767SRodney W. Grimes 17126f9a767SRodney W. Grimes object = m->object; 17224a1cce3SDavid Greenman 17326f9a767SRodney W. Grimes /* 17424a1cce3SDavid Greenman * If not OBJT_SWAP, additional memory may be needed to do the pageout. 17524a1cce3SDavid Greenman * Try to avoid the deadlock. 17626f9a767SRodney W. Grimes */ 17724a1cce3SDavid Greenman if ((sync != VM_PAGEOUT_FORCE) && 17824a1cce3SDavid Greenman (object->type != OBJT_SWAP) && 17924a1cce3SDavid Greenman ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min)) 18026f9a767SRodney W. Grimes return 0; 18126f9a767SRodney W. Grimes 18224a1cce3SDavid Greenman /* 18324a1cce3SDavid Greenman * Don't mess with the page if it's busy. 18424a1cce3SDavid Greenman */ 185f6b04d2bSDavid Greenman if ((!sync && m->hold_count != 0) || 1860d94caffSDavid Greenman ((m->busy != 0) || (m->flags & PG_BUSY))) 1870d94caffSDavid Greenman return 0; 1880d94caffSDavid Greenman 18924a1cce3SDavid Greenman /* 19024a1cce3SDavid Greenman * Try collapsing before it's too late. 19124a1cce3SDavid Greenman */ 19224a1cce3SDavid Greenman if (!sync && object->backing_object) { 19326f9a767SRodney W. Grimes vm_object_collapse(object); 19426f9a767SRodney W. Grimes } 19524a1cce3SDavid Greenman mc[VM_PAGEOUT_PAGE_COUNT] = m; 19626f9a767SRodney W. Grimes pageout_count = 1; 19724a1cce3SDavid Greenman page_base = VM_PAGEOUT_PAGE_COUNT; 19824a1cce3SDavid Greenman forward_okay = TRUE; 19924a1cce3SDavid Greenman if (offset != 0) 20024a1cce3SDavid Greenman backward_okay = TRUE; 20126f9a767SRodney W. Grimes else 20224a1cce3SDavid Greenman backward_okay = FALSE; 20324a1cce3SDavid Greenman /* 20424a1cce3SDavid Greenman * Scan object for clusterable pages. 20524a1cce3SDavid Greenman * 20624a1cce3SDavid Greenman * We can cluster ONLY if: ->> the page is NOT 20724a1cce3SDavid Greenman * clean, wired, busy, held, or mapped into a 20824a1cce3SDavid Greenman * buffer, and one of the following: 20924a1cce3SDavid Greenman * 1) The page is inactive, or a seldom used 21024a1cce3SDavid Greenman * active page. 21124a1cce3SDavid Greenman * -or- 21224a1cce3SDavid Greenman * 2) we force the issue. 21324a1cce3SDavid Greenman */ 21424a1cce3SDavid Greenman for (i = 1; (i < vm_pageout_page_count) && (forward_okay || backward_okay); i++) { 21524a1cce3SDavid Greenman vm_page_t p; 216f6b04d2bSDavid Greenman 21724a1cce3SDavid Greenman /* 21824a1cce3SDavid Greenman * See if forward page is clusterable. 21924a1cce3SDavid Greenman */ 22024a1cce3SDavid Greenman if (forward_okay) { 22124a1cce3SDavid Greenman /* 22224a1cce3SDavid Greenman * Stop forward scan at end of object. 22324a1cce3SDavid Greenman */ 22424a1cce3SDavid Greenman if ((offset + i * PAGE_SIZE) > object->size) { 22524a1cce3SDavid Greenman forward_okay = FALSE; 22624a1cce3SDavid Greenman goto do_backward; 227f6b04d2bSDavid Greenman } 22824a1cce3SDavid Greenman p = vm_page_lookup(object, offset + i * PAGE_SIZE); 22924a1cce3SDavid Greenman if (p) { 23024a1cce3SDavid Greenman if ((p->flags & (PG_BUSY|PG_CACHE)) || p->busy) { 23124a1cce3SDavid Greenman forward_okay = FALSE; 23224a1cce3SDavid Greenman goto do_backward; 233f6b04d2bSDavid Greenman } 23424a1cce3SDavid Greenman vm_page_test_dirty(p); 23524a1cce3SDavid Greenman if ((p->dirty & p->valid) != 0 && 23624a1cce3SDavid Greenman ((p->flags & PG_INACTIVE) || 23724a1cce3SDavid Greenman (sync == VM_PAGEOUT_FORCE)) && 23824a1cce3SDavid Greenman (p->wire_count == 0) && 23924a1cce3SDavid Greenman (p->hold_count == 0)) { 24024a1cce3SDavid Greenman mc[VM_PAGEOUT_PAGE_COUNT + i] = p; 24124a1cce3SDavid Greenman pageout_count++; 24224a1cce3SDavid Greenman if (pageout_count == vm_pageout_page_count) 24324a1cce3SDavid Greenman break; 24424a1cce3SDavid Greenman } else { 24524a1cce3SDavid Greenman forward_okay = FALSE; 246f6b04d2bSDavid Greenman } 24724a1cce3SDavid Greenman } else { 24824a1cce3SDavid Greenman forward_okay = FALSE; 24924a1cce3SDavid Greenman } 25024a1cce3SDavid Greenman } 25124a1cce3SDavid Greenman do_backward: 25224a1cce3SDavid Greenman /* 25324a1cce3SDavid Greenman * See if backward page is clusterable. 25424a1cce3SDavid Greenman */ 25524a1cce3SDavid Greenman if (backward_okay) { 25624a1cce3SDavid Greenman /* 25724a1cce3SDavid Greenman * Stop backward scan at beginning of object. 25824a1cce3SDavid Greenman */ 25924a1cce3SDavid Greenman if ((offset - i * PAGE_SIZE) == 0) { 26024a1cce3SDavid Greenman backward_okay = FALSE; 26124a1cce3SDavid Greenman } 26224a1cce3SDavid Greenman p = vm_page_lookup(object, offset - i * PAGE_SIZE); 26324a1cce3SDavid Greenman if (p) { 26424a1cce3SDavid Greenman if ((p->flags & (PG_BUSY|PG_CACHE)) || p->busy) { 26524a1cce3SDavid Greenman backward_okay = FALSE; 26624a1cce3SDavid Greenman continue; 26724a1cce3SDavid Greenman } 26824a1cce3SDavid Greenman vm_page_test_dirty(p); 26924a1cce3SDavid Greenman if ((p->dirty & p->valid) != 0 && 27024a1cce3SDavid Greenman ((p->flags & PG_INACTIVE) || 27124a1cce3SDavid Greenman (sync == VM_PAGEOUT_FORCE)) && 27224a1cce3SDavid Greenman (p->wire_count == 0) && 27324a1cce3SDavid Greenman (p->hold_count == 0)) { 27424a1cce3SDavid Greenman mc[VM_PAGEOUT_PAGE_COUNT - i] = p; 27524a1cce3SDavid Greenman pageout_count++; 27624a1cce3SDavid Greenman page_base--; 27724a1cce3SDavid Greenman if (pageout_count == vm_pageout_page_count) 27824a1cce3SDavid Greenman break; 27924a1cce3SDavid Greenman } else { 28024a1cce3SDavid Greenman backward_okay = FALSE; 28124a1cce3SDavid Greenman } 28224a1cce3SDavid Greenman } else { 28324a1cce3SDavid Greenman backward_okay = FALSE; 28424a1cce3SDavid Greenman } 285f6b04d2bSDavid Greenman } 286f6b04d2bSDavid Greenman } 287f6b04d2bSDavid Greenman 2880d94caffSDavid Greenman /* 2890d94caffSDavid Greenman * we allow reads during pageouts... 2900d94caffSDavid Greenman */ 29124a1cce3SDavid Greenman for (i = page_base; i < (page_base + pageout_count); i++) { 29224a1cce3SDavid Greenman mc[i]->flags |= PG_BUSY; 29324a1cce3SDavid Greenman vm_page_protect(mc[i], VM_PROT_READ); 29426f9a767SRodney W. Grimes } 29526f9a767SRodney W. Grimes 296aef922f5SJohn Dyson return vm_pageout_flush(&mc[page_base], pageout_count, sync); 297aef922f5SJohn Dyson } 298aef922f5SJohn Dyson 299aef922f5SJohn Dyson int 300aef922f5SJohn Dyson vm_pageout_flush(mc, count, sync) 301aef922f5SJohn Dyson vm_page_t *mc; 302aef922f5SJohn Dyson int count; 303aef922f5SJohn Dyson int sync; 304aef922f5SJohn Dyson { 305aef922f5SJohn Dyson register vm_object_t object; 306aef922f5SJohn Dyson int pageout_status[count]; 307aef922f5SJohn Dyson int anyok = 0; 308aef922f5SJohn Dyson int i; 309aef922f5SJohn Dyson 310aef922f5SJohn Dyson object = mc[0]->object; 311aef922f5SJohn Dyson object->paging_in_progress += count; 312aef922f5SJohn Dyson 313aef922f5SJohn Dyson vm_pager_put_pages(object, mc, count, 31426f9a767SRodney W. Grimes ((sync || (object == kernel_object)) ? TRUE : FALSE), 31526f9a767SRodney W. Grimes pageout_status); 31626f9a767SRodney W. Grimes 317aef922f5SJohn Dyson 318aef922f5SJohn Dyson for (i = 0; i < count; i++) { 319aef922f5SJohn Dyson vm_page_t mt = mc[i]; 32024a1cce3SDavid Greenman 32126f9a767SRodney W. Grimes switch (pageout_status[i]) { 32226f9a767SRodney W. Grimes case VM_PAGER_OK: 32326f9a767SRodney W. Grimes ++anyok; 32426f9a767SRodney W. Grimes break; 32526f9a767SRodney W. Grimes case VM_PAGER_PEND: 32626f9a767SRodney W. Grimes ++anyok; 32726f9a767SRodney W. Grimes break; 32826f9a767SRodney W. Grimes case VM_PAGER_BAD: 32926f9a767SRodney W. Grimes /* 3300d94caffSDavid Greenman * Page outside of range of object. Right now we 3310d94caffSDavid Greenman * essentially lose the changes by pretending it 3320d94caffSDavid Greenman * worked. 33326f9a767SRodney W. Grimes */ 33424a1cce3SDavid Greenman pmap_clear_modify(VM_PAGE_TO_PHYS(mt)); 33524a1cce3SDavid Greenman mt->dirty = 0; 33626f9a767SRodney W. Grimes break; 33726f9a767SRodney W. Grimes case VM_PAGER_ERROR: 33826f9a767SRodney W. Grimes case VM_PAGER_FAIL: 33926f9a767SRodney W. Grimes /* 3400d94caffSDavid Greenman * If page couldn't be paged out, then reactivate the 3410d94caffSDavid Greenman * page so it doesn't clog the inactive list. (We 3420d94caffSDavid Greenman * will try paging out it again later). 34326f9a767SRodney W. Grimes */ 34424a1cce3SDavid Greenman if (mt->flags & PG_INACTIVE) 34524a1cce3SDavid Greenman vm_page_activate(mt); 34626f9a767SRodney W. Grimes break; 34726f9a767SRodney W. Grimes case VM_PAGER_AGAIN: 34826f9a767SRodney W. Grimes break; 34926f9a767SRodney W. Grimes } 35026f9a767SRodney W. Grimes 35126f9a767SRodney W. Grimes 35226f9a767SRodney W. Grimes /* 3530d94caffSDavid Greenman * If the operation is still going, leave the page busy to 3540d94caffSDavid Greenman * block all other accesses. Also, leave the paging in 3550d94caffSDavid Greenman * progress indicator set so that we don't attempt an object 3560d94caffSDavid Greenman * collapse. 35726f9a767SRodney W. Grimes */ 35826f9a767SRodney W. Grimes if (pageout_status[i] != VM_PAGER_PEND) { 359f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 36024a1cce3SDavid Greenman if ((mt->flags & (PG_REFERENCED|PG_WANTED)) || 36124a1cce3SDavid Greenman pmap_is_referenced(VM_PAGE_TO_PHYS(mt))) { 36224a1cce3SDavid Greenman pmap_clear_reference(VM_PAGE_TO_PHYS(mt)); 36324a1cce3SDavid Greenman mt->flags &= ~PG_REFERENCED; 36424a1cce3SDavid Greenman if (mt->flags & PG_INACTIVE) 36524a1cce3SDavid Greenman vm_page_activate(mt); 36626f9a767SRodney W. Grimes } 36724a1cce3SDavid Greenman PAGE_WAKEUP(mt); 36826f9a767SRodney W. Grimes } 36926f9a767SRodney W. Grimes } 37026f9a767SRodney W. Grimes return anyok; 37126f9a767SRodney W. Grimes } 37226f9a767SRodney W. Grimes 37326f9a767SRodney W. Grimes /* 37426f9a767SRodney W. Grimes * vm_pageout_object_deactivate_pages 37526f9a767SRodney W. Grimes * 37626f9a767SRodney W. Grimes * deactivate enough pages to satisfy the inactive target 37726f9a767SRodney W. Grimes * requirements or if vm_page_proc_limit is set, then 37826f9a767SRodney W. Grimes * deactivate all of the pages in the object and its 37924a1cce3SDavid Greenman * backing_objects. 38026f9a767SRodney W. Grimes * 38126f9a767SRodney W. Grimes * The object and map must be locked. 38226f9a767SRodney W. Grimes */ 383cd41fc12SDavid Greenman static int 3840d94caffSDavid Greenman vm_pageout_object_deactivate_pages(map, object, count, map_remove_only) 38526f9a767SRodney W. Grimes vm_map_t map; 38626f9a767SRodney W. Grimes vm_object_t object; 38726f9a767SRodney W. Grimes int count; 3880d94caffSDavid Greenman int map_remove_only; 38926f9a767SRodney W. Grimes { 39026f9a767SRodney W. Grimes register vm_page_t p, next; 39126f9a767SRodney W. Grimes int rcount; 39226f9a767SRodney W. Grimes int dcount; 39326f9a767SRodney W. Grimes 39426f9a767SRodney W. Grimes dcount = 0; 39526f9a767SRodney W. Grimes if (count == 0) 39626f9a767SRodney W. Grimes count = 1; 39726f9a767SRodney W. Grimes 39824a1cce3SDavid Greenman if (object->type == OBJT_DEVICE) 3998f895206SDavid Greenman return 0; 4008f895206SDavid Greenman 40124a1cce3SDavid Greenman if (object->backing_object) { 40224a1cce3SDavid Greenman if (object->backing_object->ref_count == 1) 40324a1cce3SDavid Greenman dcount += vm_pageout_object_deactivate_pages(map, 40424a1cce3SDavid Greenman object->backing_object, count / 2 + 1, map_remove_only); 4050d94caffSDavid Greenman else 40624a1cce3SDavid Greenman vm_pageout_object_deactivate_pages(map, 40724a1cce3SDavid Greenman object->backing_object, count, 1); 4082fe6e4d7SDavid Greenman } 40924a1cce3SDavid Greenman if (object->paging_in_progress) 41026f9a767SRodney W. Grimes return dcount; 41126f9a767SRodney W. Grimes 41226f9a767SRodney W. Grimes /* 41326f9a767SRodney W. Grimes * scan the objects entire memory queue 41426f9a767SRodney W. Grimes */ 41526f9a767SRodney W. Grimes rcount = object->resident_page_count; 41626f9a767SRodney W. Grimes p = object->memq.tqh_first; 41726f9a767SRodney W. Grimes while (p && (rcount-- > 0)) { 41826f9a767SRodney W. Grimes next = p->listq.tqe_next; 419a58d1fa1SDavid Greenman cnt.v_pdpages++; 4200d94caffSDavid Greenman if (p->wire_count != 0 || 4210d94caffSDavid Greenman p->hold_count != 0 || 4220d94caffSDavid Greenman p->busy != 0 || 4230d94caffSDavid Greenman !pmap_page_exists(vm_map_pmap(map), VM_PAGE_TO_PHYS(p))) { 4240d94caffSDavid Greenman p = next; 4250d94caffSDavid Greenman continue; 4260d94caffSDavid Greenman } 42726f9a767SRodney W. Grimes /* 4280d94caffSDavid Greenman * if a page is active, not wired and is in the processes 4290d94caffSDavid Greenman * pmap, then deactivate the page. 43026f9a767SRodney W. Grimes */ 4310d94caffSDavid Greenman if ((p->flags & (PG_ACTIVE | PG_BUSY)) == PG_ACTIVE) { 432a647a309SDavid Greenman if (!pmap_is_referenced(VM_PAGE_TO_PHYS(p)) && 4331ed81ef2SDavid Greenman (p->flags & (PG_REFERENCED|PG_WANTED)) == 0) { 43426f9a767SRodney W. Grimes p->act_count -= min(p->act_count, ACT_DECLINE); 43526f9a767SRodney W. Grimes /* 4360d94caffSDavid Greenman * if the page act_count is zero -- then we 4370d94caffSDavid Greenman * deactivate 43826f9a767SRodney W. Grimes */ 43926f9a767SRodney W. Grimes if (!p->act_count) { 4400d94caffSDavid Greenman if (!map_remove_only) 44126f9a767SRodney W. Grimes vm_page_deactivate(p); 442f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 44326f9a767SRodney W. Grimes /* 4440d94caffSDavid Greenman * else if on the next go-around we 4450d94caffSDavid Greenman * will deactivate the page we need to 4460d94caffSDavid Greenman * place the page on the end of the 4470d94caffSDavid Greenman * queue to age the other pages in 4480d94caffSDavid Greenman * memory. 44926f9a767SRodney W. Grimes */ 45026f9a767SRodney W. Grimes } else { 45126f9a767SRodney W. Grimes TAILQ_REMOVE(&vm_page_queue_active, p, pageq); 45226f9a767SRodney W. Grimes TAILQ_INSERT_TAIL(&vm_page_queue_active, p, pageq); 45326f9a767SRodney W. Grimes } 45426f9a767SRodney W. Grimes /* 45526f9a767SRodney W. Grimes * see if we are done yet 45626f9a767SRodney W. Grimes */ 45726f9a767SRodney W. Grimes if (p->flags & PG_INACTIVE) { 45826f9a767SRodney W. Grimes --count; 45926f9a767SRodney W. Grimes ++dcount; 46026f9a767SRodney W. Grimes if (count <= 0 && 46126f9a767SRodney W. Grimes cnt.v_inactive_count > cnt.v_inactive_target) { 46226f9a767SRodney W. Grimes return dcount; 46326f9a767SRodney W. Grimes } 46426f9a767SRodney W. Grimes } 46526f9a767SRodney W. Grimes } else { 46626f9a767SRodney W. Grimes /* 46726f9a767SRodney W. Grimes * Move the page to the bottom of the queue. 46826f9a767SRodney W. Grimes */ 46926f9a767SRodney W. Grimes pmap_clear_reference(VM_PAGE_TO_PHYS(p)); 470a647a309SDavid Greenman p->flags &= ~PG_REFERENCED; 47126f9a767SRodney W. Grimes if (p->act_count < ACT_MAX) 47226f9a767SRodney W. Grimes p->act_count += ACT_ADVANCE; 47326f9a767SRodney W. Grimes 47426f9a767SRodney W. Grimes TAILQ_REMOVE(&vm_page_queue_active, p, pageq); 47526f9a767SRodney W. Grimes TAILQ_INSERT_TAIL(&vm_page_queue_active, p, pageq); 47626f9a767SRodney W. Grimes } 4770d94caffSDavid Greenman } else if ((p->flags & (PG_INACTIVE | PG_BUSY)) == PG_INACTIVE) { 478f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 47926f9a767SRodney W. Grimes } 48026f9a767SRodney W. Grimes p = next; 48126f9a767SRodney W. Grimes } 48226f9a767SRodney W. Grimes return dcount; 48326f9a767SRodney W. Grimes } 48426f9a767SRodney W. Grimes 48526f9a767SRodney W. Grimes 48626f9a767SRodney W. Grimes /* 48726f9a767SRodney W. Grimes * deactivate some number of pages in a map, try to do it fairly, but 48826f9a767SRodney W. Grimes * that is really hard to do. 48926f9a767SRodney W. Grimes */ 49026f9a767SRodney W. Grimes 491cd41fc12SDavid Greenman static void 49226f9a767SRodney W. Grimes vm_pageout_map_deactivate_pages(map, entry, count, freeer) 49326f9a767SRodney W. Grimes vm_map_t map; 49426f9a767SRodney W. Grimes vm_map_entry_t entry; 49526f9a767SRodney W. Grimes int *count; 496cd41fc12SDavid Greenman freeer_fcn_t *freeer; 49726f9a767SRodney W. Grimes { 49826f9a767SRodney W. Grimes vm_map_t tmpm; 49926f9a767SRodney W. Grimes vm_map_entry_t tmpe; 50026f9a767SRodney W. Grimes vm_object_t obj; 5010d94caffSDavid Greenman 50226f9a767SRodney W. Grimes if (*count <= 0) 50326f9a767SRodney W. Grimes return; 50426f9a767SRodney W. Grimes vm_map_reference(map); 50526f9a767SRodney W. Grimes if (!lock_try_read(&map->lock)) { 50626f9a767SRodney W. Grimes vm_map_deallocate(map); 50726f9a767SRodney W. Grimes return; 50826f9a767SRodney W. Grimes } 50926f9a767SRodney W. Grimes if (entry == 0) { 51026f9a767SRodney W. Grimes tmpe = map->header.next; 51126f9a767SRodney W. Grimes while (tmpe != &map->header && *count > 0) { 512cd41fc12SDavid Greenman vm_pageout_map_deactivate_pages(map, tmpe, count, freeer); 51326f9a767SRodney W. Grimes tmpe = tmpe->next; 51426f9a767SRodney W. Grimes }; 51526f9a767SRodney W. Grimes } else if (entry->is_sub_map || entry->is_a_map) { 51626f9a767SRodney W. Grimes tmpm = entry->object.share_map; 51726f9a767SRodney W. Grimes tmpe = tmpm->header.next; 51826f9a767SRodney W. Grimes while (tmpe != &tmpm->header && *count > 0) { 519cd41fc12SDavid Greenman vm_pageout_map_deactivate_pages(tmpm, tmpe, count, freeer); 52026f9a767SRodney W. Grimes tmpe = tmpe->next; 52126f9a767SRodney W. Grimes }; 5224e39a515SPoul-Henning Kamp } else if ((obj = entry->object.vm_object) != 0) { 523cd41fc12SDavid Greenman *count -= (*freeer) (map, obj, *count, TRUE); 52426f9a767SRodney W. Grimes } 52526f9a767SRodney W. Grimes lock_read_done(&map->lock); 52626f9a767SRodney W. Grimes vm_map_deallocate(map); 52726f9a767SRodney W. Grimes return; 52826f9a767SRodney W. Grimes } 529df8bae1dSRodney W. Grimes 530cd41fc12SDavid Greenman static void 5310d94caffSDavid Greenman vm_req_vmdaemon() 5320d94caffSDavid Greenman { 5330d94caffSDavid Greenman static int lastrun = 0; 5340d94caffSDavid Greenman 5352fe6e4d7SDavid Greenman if ((ticks > (lastrun + hz / 10)) || (ticks < lastrun)) { 53624a1cce3SDavid Greenman wakeup(&vm_daemon_needed); 5372fe6e4d7SDavid Greenman lastrun = ticks; 5382fe6e4d7SDavid Greenman } 5392fe6e4d7SDavid Greenman } 5402fe6e4d7SDavid Greenman 541df8bae1dSRodney W. Grimes /* 542df8bae1dSRodney W. Grimes * vm_pageout_scan does the dirty work for the pageout daemon. 543df8bae1dSRodney W. Grimes */ 5443af76890SPoul-Henning Kamp static int 545df8bae1dSRodney W. Grimes vm_pageout_scan() 546df8bae1dSRodney W. Grimes { 54726f9a767SRodney W. Grimes vm_page_t m; 548f6b04d2bSDavid Greenman int page_shortage, maxscan, maxlaunder, pcount; 5494e39a515SPoul-Henning Kamp int pages_freed; 55026f9a767SRodney W. Grimes vm_page_t next; 5515663e6deSDavid Greenman struct proc *p, *bigproc; 5525663e6deSDavid Greenman vm_offset_t size, bigsize; 553df8bae1dSRodney W. Grimes vm_object_t object; 55426f9a767SRodney W. Grimes int force_wakeup = 0; 555f6b04d2bSDavid Greenman int vnodes_skipped = 0; 5560d94caffSDavid Greenman 55726f9a767SRodney W. Grimes pages_freed = 0; 558df8bae1dSRodney W. Grimes 559df8bae1dSRodney W. Grimes /* 5600d94caffSDavid Greenman * Start scanning the inactive queue for pages we can free. We keep 5610d94caffSDavid Greenman * scanning until we have enough free pages or we have scanned through 5620d94caffSDavid Greenman * the entire queue. If we encounter dirty pages, we start cleaning 5630d94caffSDavid Greenman * them. 564df8bae1dSRodney W. Grimes */ 565df8bae1dSRodney W. Grimes 5660d94caffSDavid Greenman maxlaunder = (cnt.v_inactive_target > MAXLAUNDER) ? 5670d94caffSDavid Greenman MAXLAUNDER : cnt.v_inactive_target; 5680d94caffSDavid Greenman 56926f9a767SRodney W. Grimes rescan1: 570f6b04d2bSDavid Greenman maxscan = cnt.v_inactive_count; 57126f9a767SRodney W. Grimes m = vm_page_queue_inactive.tqh_first; 572f6b04d2bSDavid Greenman while ((m != NULL) && (maxscan-- > 0) && 573f6b04d2bSDavid Greenman ((cnt.v_cache_count + cnt.v_free_count) < (cnt.v_cache_min + cnt.v_free_target))) { 57426f9a767SRodney W. Grimes vm_page_t next; 575df8bae1dSRodney W. Grimes 576a58d1fa1SDavid Greenman cnt.v_pdpages++; 577df8bae1dSRodney W. Grimes next = m->pageq.tqe_next; 578df8bae1dSRodney W. Grimes 5790d94caffSDavid Greenman #if defined(VM_DIAGNOSE) 58026f9a767SRodney W. Grimes if ((m->flags & PG_INACTIVE) == 0) { 581832f3afdSAndreas Schulz printf("vm_pageout_scan: page not inactive?\n"); 5820d94caffSDavid Greenman break; 583df8bae1dSRodney W. Grimes } 5840d94caffSDavid Greenman #endif 58526f9a767SRodney W. Grimes 58626f9a767SRodney W. Grimes /* 58726f9a767SRodney W. Grimes * dont mess with busy pages 58826f9a767SRodney W. Grimes */ 589f6b04d2bSDavid Greenman if (m->hold_count || m->busy || (m->flags & PG_BUSY)) { 5900d94caffSDavid Greenman TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq); 5910d94caffSDavid Greenman TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq); 59226f9a767SRodney W. Grimes m = next; 59326f9a767SRodney W. Grimes continue; 59426f9a767SRodney W. Grimes } 5950d94caffSDavid Greenman if (((m->flags & PG_REFERENCED) == 0) && 5960d94caffSDavid Greenman pmap_is_referenced(VM_PAGE_TO_PHYS(m))) { 5972fe6e4d7SDavid Greenman m->flags |= PG_REFERENCED; 5980d94caffSDavid Greenman } 5990d94caffSDavid Greenman if (m->object->ref_count == 0) { 6000d94caffSDavid Greenman m->flags &= ~PG_REFERENCED; 6012fe6e4d7SDavid Greenman pmap_clear_reference(VM_PAGE_TO_PHYS(m)); 6022fe6e4d7SDavid Greenman } 6031ed81ef2SDavid Greenman if ((m->flags & (PG_REFERENCED|PG_WANTED)) != 0) { 604a647a309SDavid Greenman m->flags &= ~PG_REFERENCED; 6050d94caffSDavid Greenman pmap_clear_reference(VM_PAGE_TO_PHYS(m)); 60626f9a767SRodney W. Grimes vm_page_activate(m); 6076d40c3d3SDavid Greenman if (m->act_count < ACT_MAX) 6086d40c3d3SDavid Greenman m->act_count += ACT_ADVANCE; 6090d94caffSDavid Greenman m = next; 6100d94caffSDavid Greenman continue; 6110d94caffSDavid Greenman } 6120d94caffSDavid Greenman 613f6b04d2bSDavid Greenman vm_page_test_dirty(m); 614f6b04d2bSDavid Greenman if (m->dirty == 0) { 615f6b04d2bSDavid Greenman if (m->bmapped == 0) { 6166d40c3d3SDavid Greenman if (m->valid == 0) { 6176d40c3d3SDavid Greenman pmap_page_protect(VM_PAGE_TO_PHYS(m), VM_PROT_NONE); 6186d40c3d3SDavid Greenman vm_page_free(m); 6190bb3a0d2SDavid Greenman cnt.v_dfree++; 6206f2b142eSDavid Greenman } else { 6210d94caffSDavid Greenman vm_page_cache(m); 6226d40c3d3SDavid Greenman } 623f6b04d2bSDavid Greenman ++pages_freed; 624f6b04d2bSDavid Greenman } else { 625f6b04d2bSDavid Greenman m = next; 626f6b04d2bSDavid Greenman continue; 627f6b04d2bSDavid Greenman } 6280d94caffSDavid Greenman } else if (maxlaunder > 0) { 6290d94caffSDavid Greenman int written; 630f6b04d2bSDavid Greenman struct vnode *vp = NULL; 6310d94caffSDavid Greenman 6320d94caffSDavid Greenman object = m->object; 63324a1cce3SDavid Greenman if (object->flags & OBJ_DEAD) { 6340d94caffSDavid Greenman m = next; 6350d94caffSDavid Greenman continue; 6360d94caffSDavid Greenman } 637f6b04d2bSDavid Greenman 63824a1cce3SDavid Greenman if (object->type == OBJT_VNODE) { 63924a1cce3SDavid Greenman vp = object->handle; 640f6b04d2bSDavid Greenman if (VOP_ISLOCKED(vp) || vget(vp, 1)) { 641aef922f5SJohn Dyson if (object->flags & OBJ_MIGHTBEDIRTY) 642f6b04d2bSDavid Greenman ++vnodes_skipped; 643f6b04d2bSDavid Greenman m = next; 644f6b04d2bSDavid Greenman continue; 645f6b04d2bSDavid Greenman } 646f6b04d2bSDavid Greenman } 647f6b04d2bSDavid Greenman 6480d94caffSDavid Greenman /* 6490d94caffSDavid Greenman * If a page is dirty, then it is either being washed 6500d94caffSDavid Greenman * (but not yet cleaned) or it is still in the 6510d94caffSDavid Greenman * laundry. If it is still in the laundry, then we 6520d94caffSDavid Greenman * start the cleaning operation. 6530d94caffSDavid Greenman */ 6540d94caffSDavid Greenman written = vm_pageout_clean(m, 0); 655f6b04d2bSDavid Greenman 656f6b04d2bSDavid Greenman if (vp) 657f6b04d2bSDavid Greenman vput(vp); 658f6b04d2bSDavid Greenman 6590d94caffSDavid Greenman if (!next) { 6600d94caffSDavid Greenman break; 6610d94caffSDavid Greenman } 6620d94caffSDavid Greenman maxlaunder -= written; 6630d94caffSDavid Greenman /* 6640d94caffSDavid Greenman * if the next page has been re-activated, start 6650d94caffSDavid Greenman * scanning again 6660d94caffSDavid Greenman */ 6670d94caffSDavid Greenman if ((next->flags & PG_INACTIVE) == 0) { 6680d94caffSDavid Greenman goto rescan1; 6690d94caffSDavid Greenman } 670df8bae1dSRodney W. Grimes } 67126f9a767SRodney W. Grimes m = next; 67226f9a767SRodney W. Grimes } 67326f9a767SRodney W. Grimes 674df8bae1dSRodney W. Grimes /* 6750d94caffSDavid Greenman * Compute the page shortage. If we are still very low on memory be 6760d94caffSDavid Greenman * sure that we will move a minimal amount of pages from active to 6770d94caffSDavid Greenman * inactive. 678df8bae1dSRodney W. Grimes */ 679df8bae1dSRodney W. Grimes 68026f9a767SRodney W. Grimes page_shortage = cnt.v_inactive_target - 6810d94caffSDavid Greenman (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count); 68226f9a767SRodney W. Grimes if (page_shortage <= 0) { 68326f9a767SRodney W. Grimes if (pages_freed == 0) { 68417c4c408SDavid Greenman page_shortage = cnt.v_free_min - cnt.v_free_count; 685f6b04d2bSDavid Greenman } else { 686f6b04d2bSDavid Greenman page_shortage = 1; 68726f9a767SRodney W. Grimes } 688df8bae1dSRodney W. Grimes } 689f6b04d2bSDavid Greenman maxscan = MAXSCAN; 690f6b04d2bSDavid Greenman pcount = cnt.v_active_count; 69126f9a767SRodney W. Grimes m = vm_page_queue_active.tqh_first; 692f6b04d2bSDavid Greenman while ((m != NULL) && (maxscan > 0) && (pcount-- > 0) && (page_shortage > 0)) { 69326f9a767SRodney W. Grimes 694a58d1fa1SDavid Greenman cnt.v_pdpages++; 69526f9a767SRodney W. Grimes next = m->pageq.tqe_next; 696df8bae1dSRodney W. Grimes 697df8bae1dSRodney W. Grimes /* 69826f9a767SRodney W. Grimes * Don't deactivate pages that are busy. 699df8bae1dSRodney W. Grimes */ 700a647a309SDavid Greenman if ((m->busy != 0) || 7010d94caffSDavid Greenman (m->flags & PG_BUSY) || 702f6b04d2bSDavid Greenman (m->hold_count != 0)) { 7036d40c3d3SDavid Greenman TAILQ_REMOVE(&vm_page_queue_active, m, pageq); 7046d40c3d3SDavid Greenman TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq); 70526f9a767SRodney W. Grimes m = next; 70626f9a767SRodney W. Grimes continue; 707df8bae1dSRodney W. Grimes } 7081ed81ef2SDavid Greenman if (m->object->ref_count && ((m->flags & (PG_REFERENCED|PG_WANTED)) || 7090d94caffSDavid Greenman pmap_is_referenced(VM_PAGE_TO_PHYS(m)))) { 710df8bae1dSRodney W. Grimes 711df8bae1dSRodney W. Grimes pmap_clear_reference(VM_PAGE_TO_PHYS(m)); 712a647a309SDavid Greenman m->flags &= ~PG_REFERENCED; 7130d94caffSDavid Greenman if (m->act_count < ACT_MAX) { 71426f9a767SRodney W. Grimes m->act_count += ACT_ADVANCE; 7150d94caffSDavid Greenman } 71626f9a767SRodney W. Grimes TAILQ_REMOVE(&vm_page_queue_active, m, pageq); 71726f9a767SRodney W. Grimes TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq); 71826f9a767SRodney W. Grimes } else { 7196d40c3d3SDavid Greenman m->flags &= ~PG_REFERENCED; 7206d40c3d3SDavid Greenman pmap_clear_reference(VM_PAGE_TO_PHYS(m)); 72126f9a767SRodney W. Grimes m->act_count -= min(m->act_count, ACT_DECLINE); 722df8bae1dSRodney W. Grimes 723df8bae1dSRodney W. Grimes /* 72426f9a767SRodney W. Grimes * if the page act_count is zero -- then we deactivate 725df8bae1dSRodney W. Grimes */ 7260d94caffSDavid Greenman if (!m->act_count && (page_shortage > 0)) { 7270d94caffSDavid Greenman if (m->object->ref_count == 0) { 7280d94caffSDavid Greenman --page_shortage; 729f6b04d2bSDavid Greenman vm_page_test_dirty(m); 730f6b04d2bSDavid Greenman if ((m->bmapped == 0) && (m->dirty == 0) ) { 7310d94caffSDavid Greenman m->act_count = 0; 7320d94caffSDavid Greenman vm_page_cache(m); 7330d94caffSDavid Greenman } else { 7340d94caffSDavid Greenman vm_page_deactivate(m); 7350d94caffSDavid Greenman } 7360d94caffSDavid Greenman } else { 73726f9a767SRodney W. Grimes vm_page_deactivate(m); 73826f9a767SRodney W. Grimes --page_shortage; 7390d94caffSDavid Greenman } 7406d40c3d3SDavid Greenman } else if (m->act_count) { 74126f9a767SRodney W. Grimes TAILQ_REMOVE(&vm_page_queue_active, m, pageq); 74226f9a767SRodney W. Grimes TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq); 743df8bae1dSRodney W. Grimes } 744df8bae1dSRodney W. Grimes } 745f6b04d2bSDavid Greenman maxscan--; 74626f9a767SRodney W. Grimes m = next; 74726f9a767SRodney W. Grimes } 748df8bae1dSRodney W. Grimes 749df8bae1dSRodney W. Grimes /* 7500d94caffSDavid Greenman * We try to maintain some *really* free pages, this allows interrupt 7510d94caffSDavid Greenman * code to be guaranteed space. 752df8bae1dSRodney W. Grimes */ 753a1f6d91cSDavid Greenman while (cnt.v_free_count < cnt.v_free_reserved) { 7540d94caffSDavid Greenman m = vm_page_queue_cache.tqh_first; 7550d94caffSDavid Greenman if (!m) 7560d94caffSDavid Greenman break; 7570d94caffSDavid Greenman vm_page_free(m); 7580bb3a0d2SDavid Greenman cnt.v_dfree++; 75926f9a767SRodney W. Grimes } 7605663e6deSDavid Greenman 7615663e6deSDavid Greenman /* 762f6b04d2bSDavid Greenman * If we didn't get enough free pages, and we have skipped a vnode 7634c1f8ee9SDavid Greenman * in a writeable object, wakeup the sync daemon. And kick swapout 7644c1f8ee9SDavid Greenman * if we did not get enough free pages. 765f6b04d2bSDavid Greenman */ 7664c1f8ee9SDavid Greenman if ((cnt.v_cache_count + cnt.v_free_count) < cnt.v_free_target) { 767f6b04d2bSDavid Greenman if (vnodes_skipped && 768f6b04d2bSDavid Greenman (cnt.v_cache_count + cnt.v_free_count) < cnt.v_free_min) { 769f6b04d2bSDavid Greenman if (!vfs_update_wakeup) { 770f6b04d2bSDavid Greenman vfs_update_wakeup = 1; 77124a1cce3SDavid Greenman wakeup(&vfs_update_wakeup); 772f6b04d2bSDavid Greenman } 773f6b04d2bSDavid Greenman } 7744c1f8ee9SDavid Greenman /* 7754c1f8ee9SDavid Greenman * now swap processes out if we are in low memory conditions 7764c1f8ee9SDavid Greenman */ 7774c1f8ee9SDavid Greenman if (!swap_pager_full && vm_swap_size && 7784c1f8ee9SDavid Greenman vm_pageout_req_swapout == 0) { 7794c1f8ee9SDavid Greenman vm_pageout_req_swapout = 1; 7804c1f8ee9SDavid Greenman vm_req_vmdaemon(); 7814c1f8ee9SDavid Greenman } 7824c1f8ee9SDavid Greenman } 7834c1f8ee9SDavid Greenman 7844c1f8ee9SDavid Greenman if ((cnt.v_inactive_count + cnt.v_free_count + cnt.v_cache_count) < 7854c1f8ee9SDavid Greenman (cnt.v_inactive_target + cnt.v_free_min)) { 7864c1f8ee9SDavid Greenman vm_req_vmdaemon(); 7874c1f8ee9SDavid Greenman } 788f6b04d2bSDavid Greenman 789f6b04d2bSDavid Greenman /* 7900d94caffSDavid Greenman * make sure that we have swap space -- if we are low on memory and 7910d94caffSDavid Greenman * swap -- then kill the biggest process. 7925663e6deSDavid Greenman */ 7935663e6deSDavid Greenman if ((vm_swap_size == 0 || swap_pager_full) && 7940d94caffSDavid Greenman ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_free_min)) { 7955663e6deSDavid Greenman bigproc = NULL; 7965663e6deSDavid Greenman bigsize = 0; 7975663e6deSDavid Greenman for (p = (struct proc *) allproc; p != NULL; p = p->p_next) { 7985663e6deSDavid Greenman /* 7995663e6deSDavid Greenman * if this is a system process, skip it 8005663e6deSDavid Greenman */ 80179221631SDavid Greenman if ((p->p_flag & P_SYSTEM) || (p->p_pid == 1) || 80279221631SDavid Greenman ((p->p_pid < 48) && (vm_swap_size != 0))) { 8035663e6deSDavid Greenman continue; 8045663e6deSDavid Greenman } 8055663e6deSDavid Greenman /* 8065663e6deSDavid Greenman * if the process is in a non-running type state, 8075663e6deSDavid Greenman * don't touch it. 8085663e6deSDavid Greenman */ 8095663e6deSDavid Greenman if (p->p_stat != SRUN && p->p_stat != SSLEEP) { 8105663e6deSDavid Greenman continue; 8115663e6deSDavid Greenman } 8125663e6deSDavid Greenman /* 8135663e6deSDavid Greenman * get the process size 8145663e6deSDavid Greenman */ 8155663e6deSDavid Greenman size = p->p_vmspace->vm_pmap.pm_stats.resident_count; 8165663e6deSDavid Greenman /* 8175663e6deSDavid Greenman * if the this process is bigger than the biggest one 8185663e6deSDavid Greenman * remember it. 8195663e6deSDavid Greenman */ 8205663e6deSDavid Greenman if (size > bigsize) { 8215663e6deSDavid Greenman bigproc = p; 8225663e6deSDavid Greenman bigsize = size; 8235663e6deSDavid Greenman } 8245663e6deSDavid Greenman } 8255663e6deSDavid Greenman if (bigproc != NULL) { 8265663e6deSDavid Greenman printf("Process %lu killed by vm_pageout -- out of swap\n", (u_long) bigproc->p_pid); 8275663e6deSDavid Greenman psignal(bigproc, SIGKILL); 8285663e6deSDavid Greenman bigproc->p_estcpu = 0; 8295663e6deSDavid Greenman bigproc->p_nice = PRIO_MIN; 8305663e6deSDavid Greenman resetpriority(bigproc); 83124a1cce3SDavid Greenman wakeup(&cnt.v_free_count); 8325663e6deSDavid Greenman } 8335663e6deSDavid Greenman } 83426f9a767SRodney W. Grimes return force_wakeup; 83526f9a767SRodney W. Grimes } 83626f9a767SRodney W. Grimes 837df8bae1dSRodney W. Grimes /* 838df8bae1dSRodney W. Grimes * vm_pageout is the high level pageout daemon. 839df8bae1dSRodney W. Grimes */ 8402b14f991SJulian Elischer static void 84126f9a767SRodney W. Grimes vm_pageout() 842df8bae1dSRodney W. Grimes { 843df8bae1dSRodney W. Grimes (void) spl0(); 844df8bae1dSRodney W. Grimes 845df8bae1dSRodney W. Grimes /* 846df8bae1dSRodney W. Grimes * Initialize some paging parameters. 847df8bae1dSRodney W. Grimes */ 848df8bae1dSRodney W. Grimes 849f6b04d2bSDavid Greenman cnt.v_interrupt_free_min = 2; 850f6b04d2bSDavid Greenman 8510d94caffSDavid Greenman if (cnt.v_page_count > 1024) 8520d94caffSDavid Greenman cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200; 8530d94caffSDavid Greenman else 8540d94caffSDavid Greenman cnt.v_free_min = 4; 855ed74321bSDavid Greenman /* 8560d94caffSDavid Greenman * free_reserved needs to include enough for the largest swap pager 8570d94caffSDavid Greenman * structures plus enough for any pv_entry structs when paging. 858ed74321bSDavid Greenman */ 859f6b04d2bSDavid Greenman cnt.v_pageout_free_min = 6 + cnt.v_page_count / 1024 + 860f6b04d2bSDavid Greenman cnt.v_interrupt_free_min; 86161f5d510SDavid Greenman cnt.v_free_reserved = cnt.v_pageout_free_min + 6; 8620d94caffSDavid Greenman cnt.v_free_target = 3 * cnt.v_free_min + cnt.v_free_reserved; 86326f9a767SRodney W. Grimes cnt.v_free_min += cnt.v_free_reserved; 8646f2b142eSDavid Greenman 8650d94caffSDavid Greenman if (cnt.v_page_count > 1024) { 8660d94caffSDavid Greenman cnt.v_cache_max = (cnt.v_free_count - 1024) / 2; 8676f2b142eSDavid Greenman cnt.v_cache_min = (cnt.v_free_count - 1024) / 8; 8686f2b142eSDavid Greenman cnt.v_inactive_target = 2*cnt.v_cache_min + 192; 8690d94caffSDavid Greenman } else { 8700d94caffSDavid Greenman cnt.v_cache_min = 0; 8710d94caffSDavid Greenman cnt.v_cache_max = 0; 8726f2b142eSDavid Greenman cnt.v_inactive_target = cnt.v_free_count / 4; 8730d94caffSDavid Greenman } 874df8bae1dSRodney W. Grimes 875df8bae1dSRodney W. Grimes /* XXX does not really belong here */ 876df8bae1dSRodney W. Grimes if (vm_page_max_wired == 0) 877df8bae1dSRodney W. Grimes vm_page_max_wired = cnt.v_free_count / 3; 878df8bae1dSRodney W. Grimes 87926f9a767SRodney W. Grimes 88024a1cce3SDavid Greenman swap_pager_swap_init(); 881df8bae1dSRodney W. Grimes /* 8820d94caffSDavid Greenman * The pageout daemon is never done, so loop forever. 883df8bae1dSRodney W. Grimes */ 884df8bae1dSRodney W. Grimes while (TRUE) { 885f919ebdeSDavid Greenman int s = splhigh(); 886f919ebdeSDavid Greenman 887f919ebdeSDavid Greenman if (!vm_pages_needed || 888f919ebdeSDavid Greenman ((cnt.v_free_count >= cnt.v_free_reserved) && 889f919ebdeSDavid Greenman (cnt.v_free_count + cnt.v_cache_count >= cnt.v_free_min))) { 890f919ebdeSDavid Greenman vm_pages_needed = 0; 89124a1cce3SDavid Greenman tsleep(&vm_pages_needed, PVM, "psleep", 0); 892f919ebdeSDavid Greenman } 893f919ebdeSDavid Greenman vm_pages_needed = 0; 894f919ebdeSDavid Greenman splx(s); 895a58d1fa1SDavid Greenman cnt.v_pdwakeups++; 896df8bae1dSRodney W. Grimes vm_pager_sync(); 8970d94caffSDavid Greenman vm_pageout_scan(); 89826f9a767SRodney W. Grimes vm_pager_sync(); 89924a1cce3SDavid Greenman wakeup(&cnt.v_free_count); 90024a1cce3SDavid Greenman wakeup(kmem_map); 901df8bae1dSRodney W. Grimes } 902df8bae1dSRodney W. Grimes } 90326f9a767SRodney W. Grimes 9042b14f991SJulian Elischer static void 9054f9fb771SBruce Evans vm_daemon() 9060d94caffSDavid Greenman { 9072fe6e4d7SDavid Greenman vm_object_t object; 9082fe6e4d7SDavid Greenman struct proc *p; 9090d94caffSDavid Greenman 9102fe6e4d7SDavid Greenman while (TRUE) { 91124a1cce3SDavid Greenman tsleep(&vm_daemon_needed, PUSER, "psleep", 0); 9124c1f8ee9SDavid Greenman if (vm_pageout_req_swapout) { 9136306c897SDavid Greenman swapout_procs(); 9144c1f8ee9SDavid Greenman vm_pageout_req_swapout = 0; 9154c1f8ee9SDavid Greenman } 9162fe6e4d7SDavid Greenman /* 9170d94caffSDavid Greenman * scan the processes for exceeding their rlimits or if 9180d94caffSDavid Greenman * process is swapped out -- deactivate pages 9192fe6e4d7SDavid Greenman */ 9202fe6e4d7SDavid Greenman 9212fe6e4d7SDavid Greenman for (p = (struct proc *) allproc; p != NULL; p = p->p_next) { 9222fe6e4d7SDavid Greenman int overage; 9232fe6e4d7SDavid Greenman quad_t limit; 9242fe6e4d7SDavid Greenman vm_offset_t size; 9252fe6e4d7SDavid Greenman 9262fe6e4d7SDavid Greenman /* 9272fe6e4d7SDavid Greenman * if this is a system process or if we have already 9282fe6e4d7SDavid Greenman * looked at this process, skip it. 9292fe6e4d7SDavid Greenman */ 9302fe6e4d7SDavid Greenman if (p->p_flag & (P_SYSTEM | P_WEXIT)) { 9312fe6e4d7SDavid Greenman continue; 9322fe6e4d7SDavid Greenman } 9332fe6e4d7SDavid Greenman /* 9342fe6e4d7SDavid Greenman * if the process is in a non-running type state, 9352fe6e4d7SDavid Greenman * don't touch it. 9362fe6e4d7SDavid Greenman */ 9372fe6e4d7SDavid Greenman if (p->p_stat != SRUN && p->p_stat != SSLEEP) { 9382fe6e4d7SDavid Greenman continue; 9392fe6e4d7SDavid Greenman } 9402fe6e4d7SDavid Greenman /* 9412fe6e4d7SDavid Greenman * get a limit 9422fe6e4d7SDavid Greenman */ 9432fe6e4d7SDavid Greenman limit = qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur, 9442fe6e4d7SDavid Greenman p->p_rlimit[RLIMIT_RSS].rlim_max); 9452fe6e4d7SDavid Greenman 9462fe6e4d7SDavid Greenman /* 9470d94caffSDavid Greenman * let processes that are swapped out really be 9480d94caffSDavid Greenman * swapped out set the limit to nothing (will force a 9490d94caffSDavid Greenman * swap-out.) 9502fe6e4d7SDavid Greenman */ 9512fe6e4d7SDavid Greenman if ((p->p_flag & P_INMEM) == 0) 9520d94caffSDavid Greenman limit = 0; /* XXX */ 9532fe6e4d7SDavid Greenman 954a91c5a7eSJohn Dyson size = p->p_vmspace->vm_pmap.pm_stats.resident_count * PAGE_SIZE; 9552fe6e4d7SDavid Greenman if (limit >= 0 && size >= limit) { 956a5eb0e27SPoul-Henning Kamp overage = (size - limit) >> PAGE_SHIFT; 9572fe6e4d7SDavid Greenman vm_pageout_map_deactivate_pages(&p->p_vmspace->vm_map, 9582fe6e4d7SDavid Greenman (vm_map_entry_t) 0, &overage, vm_pageout_object_deactivate_pages); 9592fe6e4d7SDavid Greenman } 9602fe6e4d7SDavid Greenman } 9612fe6e4d7SDavid Greenman 9620d94caffSDavid Greenman /* 9630d94caffSDavid Greenman * we remove cached objects that have no RSS... 9640d94caffSDavid Greenman */ 9650d94caffSDavid Greenman restart: 9662fe6e4d7SDavid Greenman object = vm_object_cached_list.tqh_first; 9672fe6e4d7SDavid Greenman while (object) { 9682fe6e4d7SDavid Greenman /* 9692fe6e4d7SDavid Greenman * if there are no resident pages -- get rid of the object 9702fe6e4d7SDavid Greenman */ 9712fe6e4d7SDavid Greenman if (object->resident_page_count == 0) { 97224a1cce3SDavid Greenman vm_object_reference(object); 9732fe6e4d7SDavid Greenman pager_cache(object, FALSE); 9742fe6e4d7SDavid Greenman goto restart; 9752fe6e4d7SDavid Greenman } 9762fe6e4d7SDavid Greenman object = object->cached_list.tqe_next; 9772fe6e4d7SDavid Greenman } 97824a1cce3SDavid Greenman } 9792fe6e4d7SDavid Greenman } 980