vm_pageout.c revision 51337
119370Spst/* 2130803Smarcel * Copyright (c) 1991 Regents of the University of California. 398944Sobrien * All rights reserved. 419370Spst * Copyright (c) 1994 John S. Dyson 519370Spst * All rights reserved. 619370Spst * Copyright (c) 1994 David Greenman 798944Sobrien * All rights reserved. 819370Spst * 998944Sobrien * This code is derived from software contributed to Berkeley by 1098944Sobrien * The Mach Operating System project at Carnegie-Mellon University. 1198944Sobrien * 1298944Sobrien * Redistribution and use in source and binary forms, with or without 1319370Spst * modification, are permitted provided that the following conditions 1498944Sobrien * are met: 1598944Sobrien * 1. Redistributions of source code must retain the above copyright 1698944Sobrien * notice, this list of conditions and the following disclaimer. 1798944Sobrien * 2. Redistributions in binary form must reproduce the above copyright 1819370Spst * notice, this list of conditions and the following disclaimer in the 1998944Sobrien * documentation and/or other materials provided with the distribution. 2098944Sobrien * 3. All advertising materials mentioning features or use of this software 2198944Sobrien * must display the following acknowledgement: 2298944Sobrien * This product includes software developed by the University of 2319370Spst * California, Berkeley and its contributors. 2419370Spst * 4. Neither the name of the University nor the names of its contributors 2519370Spst * may be used to endorse or promote products derived from this software 2619370Spst * without specific prior written permission. 2719370Spst * 2819370Spst * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2919370Spst * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 3019370Spst * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 3119370Spst * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 3298944Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33130803Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3419370Spst * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3519370Spst * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3619370Spst * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3719370Spst * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3819370Spst * SUCH DAMAGE. 3919370Spst * 4019370Spst * from: @(#)vm_pageout.c 7.4 (Berkeley) 5/7/91 4119370Spst * 4298944Sobrien * 4319370Spst * Copyright (c) 1987, 1990 Carnegie-Mellon University. 4419370Spst * All rights reserved. 4519370Spst * 4619370Spst * Authors: Avadis Tevanian, Jr., Michael Wayne Young 4719370Spst * 4819370Spst * Permission to use, copy, modify and distribute this software and 4919370Spst * its documentation is hereby granted, provided that both the copyright 5019370Spst * notice and this permission notice appear in all copies of the 5119370Spst * software, derivative works or modified versions, and any portions 5246283Sdfr * thereof, and that both notices appear in supporting documentation. 5346283Sdfr * 5446283Sdfr * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 5598944Sobrien * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 5698944Sobrien * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 5798944Sobrien * 5898944Sobrien * Carnegie Mellon requests users of this software to return to 5946283Sdfr * 6046283Sdfr * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 6198944Sobrien * School of Computer Science 6298944Sobrien * Carnegie Mellon University 6398944Sobrien * Pittsburgh PA 15213-3890 6498944Sobrien * 6598944Sobrien * any improvements or extensions that they make and grant Carnegie the 6698944Sobrien * rights to redistribute these changes. 6746283Sdfr * 6898944Sobrien * $FreeBSD: head/sys/vm/vm_pageout.c 51337 1999-09-17 04:56:40Z dillon $ 6998944Sobrien */ 7046283Sdfr 7146283Sdfr/* 7246283Sdfr * The proverbial page-out daemon. 7398944Sobrien */ 7446283Sdfr 7598944Sobrien#include "opt_vm.h" 7698944Sobrien#include <sys/param.h> 7798944Sobrien#include <sys/systm.h> 7898944Sobrien#include <sys/kernel.h> 7998944Sobrien#include <sys/proc.h> 8098944Sobrien#include <sys/kthread.h> 8198944Sobrien#include <sys/resourcevar.h> 8298944Sobrien#include <sys/signalvar.h> 8398944Sobrien#include <sys/vnode.h> 8498944Sobrien#include <sys/vmmeter.h> 8598944Sobrien#include <sys/sysctl.h> 8698944Sobrien 8746283Sdfr#include <vm/vm.h> 8846283Sdfr#include <vm/vm_param.h> 8998944Sobrien#include <vm/vm_prot.h> 9098944Sobrien#include <sys/lock.h> 9198944Sobrien#include <vm/vm_object.h> 9298944Sobrien#include <vm/vm_page.h> 9398944Sobrien#include <vm/vm_map.h> 9498944Sobrien#include <vm/vm_pageout.h> 9546283Sdfr#include <vm/vm_pager.h> 9619370Spst#include <vm/swap_pager.h> 9719370Spst#include <vm/vm_extern.h> 9819370Spst 9919370Spst/* 10019370Spst * System initialization 10119370Spst */ 10219370Spst 103130803Smarcel/* the kernel process "vm_pageout"*/ 10419370Spststatic void vm_pageout __P((void)); 10519370Spststatic int vm_pageout_clean __P((vm_page_t)); 10698944Sobrienstatic int vm_pageout_scan __P((void)); 10719370Spststatic int vm_pageout_free_page_calc __P((vm_size_t count)); 10819370Spststruct proc *pageproc; 10919370Spst 11019370Spststatic struct kproc_desc page_kp = { 11119370Spst "pagedaemon", 11219370Spst vm_pageout, 11319370Spst &pageproc 11419370Spst}; 11519370SpstSYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, &page_kp) 11619370Spst 11719370Spst#if !defined(NO_SWAPPING) 11819370Spst/* the kernel process "vm_daemon"*/ 11919370Spststatic void vm_daemon __P((void)); 12019370Spststatic struct proc *vmproc; 12119370Spst 12219370Spststatic struct kproc_desc vm_kp = { 12319370Spst "vmdaemon", 12419370Spst vm_daemon, 12519370Spst &vmproc 12619370Spst}; 12719370SpstSYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp) 12819370Spst#endif 12919370Spst 13019370Spst 13119370Spstint vm_pages_needed=0; /* Event on which pageout daemon sleeps */ 13219370Spstint vm_pageout_deficit=0; /* Estimated number of pages deficit */ 13319370Spstint vm_pageout_pages_needed=0; /* flag saying that the pageout daemon needs pages */ 13419370Spst 13519370Spst#if !defined(NO_SWAPPING) 13619370Spststatic int vm_pageout_req_swapout; /* XXX */ 13719370Spststatic int vm_daemon_needed; 13819370Spst#endif 13919370Spstextern int vm_swap_size; 14019370Spststatic int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0; 14119370Spststatic int vm_pageout_full_stats_interval = 0; 14219370Spststatic int vm_pageout_stats_free_max=0, vm_pageout_algorithm_lru=0; 14319370Spststatic int defer_swap_pageouts=0; 14419370Spststatic int disable_swap_pageouts=0; 14519370Spst 14619370Spststatic int max_page_launder=100; 14719370Spst#if defined(NO_SWAPPING) 14819370Spststatic int vm_swap_enabled=0; 14998944Sobrienstatic int vm_swap_idle_enabled=0; 15019370Spst#else 15119370Spststatic int vm_swap_enabled=1; 15246283Sdfrstatic int vm_swap_idle_enabled=0; 15319370Spst#endif 15419370Spst 15519370SpstSYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm, 15619370Spst CTLFLAG_RW, &vm_pageout_algorithm_lru, 0, "LRU page mgmt"); 15719370Spst 15819370SpstSYSCTL_INT(_vm, OID_AUTO, pageout_stats_max, 15919370Spst CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length"); 16019370Spst 16119370SpstSYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval, 16219370Spst CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan"); 16319370Spst 16498944SobrienSYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval, 16598944Sobrien CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan"); 16619370Spst 167130803SmarcelSYSCTL_INT(_vm, OID_AUTO, pageout_stats_free_max, 16819370Spst CTLFLAG_RW, &vm_pageout_stats_free_max, 0, "Not implemented"); 16919370Spst 17019370Spst#if defined(NO_SWAPPING) 17198944SobrienSYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 17219370Spst CTLFLAG_RD, &vm_swap_enabled, 0, ""); 17319370SpstSYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 17446283Sdfr CTLFLAG_RD, &vm_swap_idle_enabled, 0, ""); 17519370Spst#else 17619370SpstSYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 17798944Sobrien CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout"); 17819370SpstSYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 17919370Spst CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria"); 18019370Spst#endif 18198944Sobrien 18219370SpstSYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts, 18319370Spst CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem"); 18419370Spst 18598944SobrienSYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts, 18619370Spst CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages"); 18798944Sobrien 18819370SpstSYSCTL_INT(_vm, OID_AUTO, max_page_launder, 18919370Spst CTLFLAG_RW, &max_page_launder, 0, "Maximum number of pages to clean per pass"); 19019370Spst 19119370Spst 19219370Spst#define VM_PAGEOUT_PAGE_COUNT 16 19398944Sobrienint vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT; 19419370Spst 19519370Spstint vm_page_max_wired; /* XXX max # of wired pages system-wide */ 19619370Spst 19719370Spst#if !defined(NO_SWAPPING) 19819370Spsttypedef void freeer_fcn_t __P((vm_map_t, vm_object_t, vm_pindex_t, int)); 19919370Spststatic void vm_pageout_map_deactivate_pages __P((vm_map_t, vm_pindex_t)); 20019370Spststatic freeer_fcn_t vm_pageout_object_deactivate_pages; 20198944Sobrienstatic void vm_req_vmdaemon __P((void)); 20219370Spst#endif 20319370Spststatic void vm_pageout_page_stats(void); 20419370Spst 20519370Spst/* 20619370Spst * vm_pageout_clean: 20719370Spst * 20819370Spst * Clean the page and remove it from the laundry. 20919370Spst * 21019370Spst * We set the busy bit to cause potential page faults on this page to 21119370Spst * block. Note the careful timing, however, the busy bit isn't set till 21219370Spst * late and we cannot do anything that will mess with the page. 21319370Spst */ 21419370Spst 21519370Spststatic int 21619370Spstvm_pageout_clean(m) 21719370Spst vm_page_t m; 21819370Spst{ 21919370Spst register vm_object_t object; 22019370Spst vm_page_t mc[2*vm_pageout_page_count]; 22119370Spst int pageout_count; 22219370Spst int ib, is, page_base; 22319370Spst vm_pindex_t pindex = m->pindex; 22419370Spst 22519370Spst object = m->object; 22619370Spst 22719370Spst /* 22846283Sdfr * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP 22919370Spst * with the new swapper, but we could have serious problems paging 23019370Spst * out other object types if there is insufficient memory. 23119370Spst * 23298944Sobrien * Unfortunately, checking free memory here is far too late, so the 23319370Spst * check has been moved up a procedural level. 23419370Spst */ 23519370Spst 23619370Spst /* 23719370Spst * Don't mess with the page if it's busy. 23819370Spst */ 23919370Spst if ((m->hold_count != 0) || 24019370Spst ((m->busy != 0) || (m->flags & PG_BUSY))) 24198944Sobrien return 0; 24219370Spst 24319370Spst mc[vm_pageout_page_count] = m; 24419370Spst pageout_count = 1; 24519370Spst page_base = vm_pageout_page_count; 24619370Spst ib = 1; 24719370Spst is = 1; 24819370Spst 24919370Spst /* 25019370Spst * Scan object for clusterable pages. 25198944Sobrien * 25219370Spst * We can cluster ONLY if: ->> the page is NOT 253130803Smarcel * clean, wired, busy, held, or mapped into a 25498944Sobrien * buffer, and one of the following: 25519370Spst * 1) The page is inactive, or a seldom used 25619370Spst * active page. 25719370Spst * -or- 25819370Spst * 2) we force the issue. 25919370Spst * 26019370Spst * During heavy mmap/modification loads the pageout 26119370Spst * daemon can really fragment the underlying file 26219370Spst * due to flushing pages out of order and not trying 26319370Spst * align the clusters (which leave sporatic out-of-order 26419370Spst * holes). To solve this problem we do the reverse scan 26519370Spst * first and attempt to align our cluster, then do a 26619370Spst * forward scan if room remains. 26719370Spst */ 26819370Spst 26919370Spstmore: 27019370Spst while (ib && pageout_count < vm_pageout_page_count) { 27119370Spst vm_page_t p; 27219370Spst 27319370Spst if (ib > pindex) { 27419370Spst ib = 0; 27519370Spst break; 27619370Spst } 27719370Spst 27819370Spst if ((p = vm_page_lookup(object, pindex - ib)) == NULL) { 27919370Spst ib = 0; 28019370Spst break; 28119370Spst } 28219370Spst if (((p->queue - p->pc) == PQ_CACHE) || 28319370Spst (p->flags & PG_BUSY) || p->busy) { 28419370Spst ib = 0; 28519370Spst break; 28619370Spst } 28719370Spst vm_page_test_dirty(p); 28819370Spst if ((p->dirty & p->valid) == 0 || 28919370Spst p->queue != PQ_INACTIVE || 29019370Spst p->wire_count != 0 || 29119370Spst p->hold_count != 0) { 29219370Spst ib = 0; 29319370Spst break; 29419370Spst } 29519370Spst mc[--page_base] = p; 29619370Spst ++pageout_count; 29719370Spst ++ib; 29819370Spst /* 29919370Spst * alignment boundry, stop here and switch directions. Do 30019370Spst * not clear ib. 30119370Spst */ 30219370Spst if ((pindex - (ib - 1)) % vm_pageout_page_count == 0) 30319370Spst break; 30419370Spst } 30519370Spst 30619370Spst while (pageout_count < vm_pageout_page_count && 30719370Spst pindex + is < object->size) { 30819370Spst vm_page_t p; 30919370Spst 31098944Sobrien if ((p = vm_page_lookup(object, pindex + is)) == NULL) 31119370Spst break; 31219370Spst if (((p->queue - p->pc) == PQ_CACHE) || 31398944Sobrien (p->flags & PG_BUSY) || p->busy) { 31419370Spst break; 31519370Spst } 31619370Spst vm_page_test_dirty(p); 31719370Spst if ((p->dirty & p->valid) == 0 || 31819370Spst p->queue != PQ_INACTIVE || 31919370Spst p->wire_count != 0 || 32019370Spst p->hold_count != 0) { 32119370Spst break; 32219370Spst } 32319370Spst mc[page_base + pageout_count] = p; 32419370Spst ++pageout_count; 32519370Spst ++is; 32619370Spst } 32719370Spst 32819370Spst /* 32919370Spst * If we exhausted our forward scan, continue with the reverse scan 33098944Sobrien * when possible, even past a page boundry. This catches boundry 33119370Spst * conditions. 33219370Spst */ 33319370Spst if (ib && pageout_count < vm_pageout_page_count) 33419370Spst goto more; 33519370Spst 33619370Spst /* 33719370Spst * we allow reads during pageouts... 33819370Spst */ 33919370Spst return vm_pageout_flush(&mc[page_base], pageout_count, 0); 34019370Spst} 34119370Spst 34219370Spst/* 34319370Spst * vm_pageout_flush() - launder the given pages 34419370Spst * 34519370Spst * The given pages are laundered. Note that we setup for the start of 34619370Spst * I/O ( i.e. busy the page ), mark it read-only, and bump the object 34719370Spst * reference count all in here rather then in the parent. If we want 34819370Spst * the parent to do more sophisticated things we may have to change 34919370Spst * the ordering. 35019370Spst */ 35119370Spst 35219370Spstint 35319370Spstvm_pageout_flush(mc, count, flags) 35419370Spst vm_page_t *mc; 35519370Spst int count; 35619370Spst int flags; 35719370Spst{ 35819370Spst register vm_object_t object; 35919370Spst int pageout_status[count]; 36019370Spst int numpagedout = 0; 36119370Spst int i; 36219370Spst 36319370Spst /* 36419370Spst * Initiate I/O. Bump the vm_page_t->busy counter and 36519370Spst * mark the pages read-only. 36619370Spst * 36719370Spst * We do not have to fixup the clean/dirty bits here... we can 36819370Spst * allow the pager to do it after the I/O completes. 36919370Spst */ 37019370Spst 37119370Spst for (i = 0; i < count; i++) { 37219370Spst vm_page_io_start(mc[i]); 37319370Spst vm_page_protect(mc[i], VM_PROT_READ); 37419370Spst } 37519370Spst 37619370Spst object = mc[0]->object; 37719370Spst vm_object_pip_add(object, count); 37819370Spst 37919370Spst vm_pager_put_pages(object, mc, count, 38019370Spst (flags | ((object == kernel_object) ? OBJPC_SYNC : 0)), 38119370Spst pageout_status); 38219370Spst 38319370Spst for (i = 0; i < count; i++) { 38419370Spst vm_page_t mt = mc[i]; 38519370Spst 38619370Spst switch (pageout_status[i]) { 38719370Spst case VM_PAGER_OK: 38819370Spst numpagedout++; 38919370Spst break; 39019370Spst case VM_PAGER_PEND: 39119370Spst numpagedout++; 39298944Sobrien break; 39398944Sobrien case VM_PAGER_BAD: 39498944Sobrien /* 39519370Spst * Page outside of range of object. Right now we 39619370Spst * essentially lose the changes by pretending it 39719370Spst * worked. 39819370Spst */ 39919370Spst pmap_clear_modify(VM_PAGE_TO_PHYS(mt)); 40019370Spst vm_page_undirty(mt); 40119370Spst break; 40219370Spst case VM_PAGER_ERROR: 40398944Sobrien case VM_PAGER_FAIL: 40419370Spst /* 40519370Spst * If page couldn't be paged out, then reactivate the 40619370Spst * page so it doesn't clog the inactive list. (We 40798944Sobrien * will try paging out it again later). 40898944Sobrien */ 40998944Sobrien vm_page_activate(mt); 41098944Sobrien break; 41198944Sobrien case VM_PAGER_AGAIN: 41298944Sobrien break; 41398944Sobrien } 41498944Sobrien 41598944Sobrien /* 41698944Sobrien * If the operation is still going, leave the page busy to 41798944Sobrien * block all other accesses. Also, leave the paging in 41898944Sobrien * progress indicator set so that we don't attempt an object 41998944Sobrien * collapse. 42098944Sobrien */ 42198944Sobrien if (pageout_status[i] != VM_PAGER_PEND) { 42298944Sobrien vm_object_pip_wakeup(object); 42398944Sobrien vm_page_io_finish(mt); 42498944Sobrien } 42598944Sobrien } 42698944Sobrien return numpagedout; 42798944Sobrien} 42898944Sobrien 42998944Sobrien#if !defined(NO_SWAPPING) 43019370Spst/* 43119370Spst * vm_pageout_object_deactivate_pages 43298944Sobrien * 43319370Spst * deactivate enough pages to satisfy the inactive target 43419370Spst * requirements or if vm_page_proc_limit is set, then 43598944Sobrien * deactivate all of the pages in the object and its 43698944Sobrien * backing_objects. 43798944Sobrien * 43898944Sobrien * The object and map must be locked. 43998944Sobrien */ 44098944Sobrienstatic void 44198944Sobrienvm_pageout_object_deactivate_pages(map, object, desired, map_remove_only) 44298944Sobrien vm_map_t map; 44398944Sobrien vm_object_t object; 44498944Sobrien vm_pindex_t desired; 44519370Spst int map_remove_only; 44698944Sobrien{ 44719370Spst register vm_page_t p, next; 44898944Sobrien int rcount; 44998944Sobrien int remove_mode; 45019370Spst int s; 45119370Spst 45246283Sdfr if (object->type == OBJT_DEVICE) 45346283Sdfr return; 45498944Sobrien 45598944Sobrien while (object) { 45619370Spst if (pmap_resident_count(vm_map_pmap(map)) <= desired) 45798944Sobrien return; 45898944Sobrien if (object->paging_in_progress) 45919370Spst return; 46019370Spst 46119370Spst remove_mode = map_remove_only; 46219370Spst if (object->shadow_count > 1) 46319370Spst remove_mode = 1; 46498944Sobrien /* 465130803Smarcel * scan the objects entire memory queue 46619370Spst */ 46719370Spst rcount = object->resident_page_count; 46819370Spst p = TAILQ_FIRST(&object->memq); 46919370Spst while (p && (rcount-- > 0)) { 47046283Sdfr int actcount; 47119370Spst if (pmap_resident_count(vm_map_pmap(map)) <= desired) 47298944Sobrien return; 47319370Spst next = TAILQ_NEXT(p, listq); 47498944Sobrien cnt.v_pdpages++; 475130803Smarcel if (p->wire_count != 0 || 476130803Smarcel p->hold_count != 0 || 477130803Smarcel p->busy != 0 || 478130803Smarcel (p->flags & PG_BUSY) || 479130803Smarcel !pmap_page_exists(vm_map_pmap(map), VM_PAGE_TO_PHYS(p))) { 48098944Sobrien p = next; 48198944Sobrien continue; 48298944Sobrien } 48398944Sobrien 48419370Spst actcount = pmap_ts_referenced(VM_PAGE_TO_PHYS(p)); 48519370Spst if (actcount) { 48619370Spst vm_page_flag_set(p, PG_REFERENCED); 48798944Sobrien } else if (p->flags & PG_REFERENCED) { 488130803Smarcel actcount = 1; 48919370Spst } 49098944Sobrien 49119370Spst if ((p->queue != PQ_ACTIVE) && 492130803Smarcel (p->flags & PG_REFERENCED)) { 493130803Smarcel vm_page_activate(p); 49419370Spst p->act_count += actcount; 49519370Spst vm_page_flag_clear(p, PG_REFERENCED); 49619370Spst } else if (p->queue == PQ_ACTIVE) { 49719370Spst if ((p->flags & PG_REFERENCED) == 0) { 49819370Spst p->act_count -= min(p->act_count, ACT_DECLINE); 49998944Sobrien if (!remove_mode && (vm_pageout_algorithm_lru || (p->act_count == 0))) { 50019370Spst vm_page_protect(p, VM_PROT_NONE); 50119370Spst vm_page_deactivate(p); 50219370Spst } else { 50319370Spst s = splvm(); 50498944Sobrien TAILQ_REMOVE(&vm_page_queue_active, p, pageq); 50519370Spst TAILQ_INSERT_TAIL(&vm_page_queue_active, p, pageq); 50619370Spst splx(s); 50719370Spst } 50819370Spst } else { 50998944Sobrien vm_page_activate(p); 51019370Spst vm_page_flag_clear(p, PG_REFERENCED); 51119370Spst if (p->act_count < (ACT_MAX - ACT_ADVANCE)) 51219370Spst p->act_count += ACT_ADVANCE; 51319370Spst s = splvm(); 51498944Sobrien TAILQ_REMOVE(&vm_page_queue_active, p, pageq); 51519370Spst TAILQ_INSERT_TAIL(&vm_page_queue_active, p, pageq); 51619370Spst splx(s); 51719370Spst } 51819370Spst } else if (p->queue == PQ_INACTIVE) { 51998944Sobrien vm_page_protect(p, VM_PROT_NONE); 52019370Spst } 52119370Spst p = next; 52219370Spst } 52319370Spst object = object->backing_object; 52498944Sobrien } 52519370Spst return; 52619370Spst} 52719370Spst 52819370Spst/* 52998944Sobrien * deactivate some number of pages in a map, try to do it fairly, but 53019370Spst * that is really hard to do. 53119370Spst */ 53219370Spststatic void 53319370Spstvm_pageout_map_deactivate_pages(map, desired) 53498944Sobrien vm_map_t map; 53519370Spst vm_pindex_t desired; 53619370Spst{ 53719370Spst vm_map_entry_t tmpe; 53819370Spst vm_object_t obj, bigobj; 53998944Sobrien 54019370Spst if (lockmgr(&map->lock, LK_EXCLUSIVE | LK_NOWAIT, (void *)0, curproc)) { 54119370Spst return; 54219370Spst } 54319370Spst 54498944Sobrien bigobj = NULL; 54519370Spst 54619370Spst /* 54719370Spst * first, search out the biggest object, and try to free pages from 54819370Spst * that. 54919370Spst */ 55098944Sobrien tmpe = map->header.next; 55119370Spst while (tmpe != &map->header) { 55219370Spst if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 55319370Spst obj = tmpe->object.vm_object; 55419370Spst if ((obj != NULL) && (obj->shadow_count <= 1) && 55519370Spst ((bigobj == NULL) || 55698944Sobrien (bigobj->resident_page_count < obj->resident_page_count))) { 55719370Spst bigobj = obj; 55819370Spst } 55998944Sobrien } 56098944Sobrien tmpe = tmpe->next; 56119370Spst } 56219370Spst 56319370Spst if (bigobj) 56419370Spst vm_pageout_object_deactivate_pages(map, bigobj, desired, 0); 565130803Smarcel 56619370Spst /* 567130803Smarcel * Next, hunt around for other pages to deactivate. We actually 568130803Smarcel * do this search sort of wrong -- .text first is not the best idea. 569130803Smarcel */ 570130803Smarcel tmpe = map->header.next; 571130803Smarcel while (tmpe != &map->header) { 572130803Smarcel if (pmap_resident_count(vm_map_pmap(map)) <= desired) 573130803Smarcel break; 574130803Smarcel if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 575130803Smarcel obj = tmpe->object.vm_object; 576130803Smarcel if (obj) 577130803Smarcel vm_pageout_object_deactivate_pages(map, obj, desired, 0); 578130803Smarcel } 579130803Smarcel tmpe = tmpe->next; 580130803Smarcel }; 581130803Smarcel 582130803Smarcel /* 583130803Smarcel * Remove all mappings if a process is swapped out, this will free page 584130803Smarcel * table pages. 585130803Smarcel */ 586130803Smarcel if (desired == 0) 587130803Smarcel pmap_remove(vm_map_pmap(map), 58819370Spst VM_MIN_ADDRESS, VM_MAXUSER_ADDRESS); 58919370Spst vm_map_unlock(map); 59019370Spst return; 59198944Sobrien} 59298944Sobrien#endif 59319370Spst 59419370Spst/* 59519370Spst * Don't try to be fancy - being fancy can lead to VOP_LOCK's and therefore 59646283Sdfr * to vnode deadlocks. We only do it for OBJT_DEFAULT and OBJT_SWAP objects 59746283Sdfr * which we know can be trivially freed. 59898944Sobrien */ 59919370Spst 60019370Spstvoid 60198944Sobrienvm_pageout_page_free(vm_page_t m) { 60219370Spst vm_object_t object = m->object; 60398944Sobrien int type = object->type; 60419370Spst 60519370Spst if (type == OBJT_SWAP || type == OBJT_DEFAULT) 60646283Sdfr vm_object_reference(object); 60798944Sobrien vm_page_busy(m); 60819370Spst vm_page_protect(m, VM_PROT_NONE); 60919370Spst vm_page_free(m); 61098944Sobrien if (type == OBJT_SWAP || type == OBJT_DEFAULT) 61119370Spst vm_object_deallocate(object); 61298944Sobrien} 61319370Spst 61419370Spst/* 61598944Sobrien * vm_pageout_scan does the dirty work for the pageout daemon. 61698944Sobrien */ 61719370Spststatic int 61819370Spstvm_pageout_scan() 61998944Sobrien{ 62019370Spst vm_page_t m, next; 62198944Sobrien int page_shortage, maxscan, pcount; 62219370Spst int addl_page_shortage, addl_page_shortage_init; 62319370Spst int maxlaunder; 62498944Sobrien int launder_loop = 0; 62598944Sobrien struct proc *p, *bigproc; 62619370Spst vm_offset_t size, bigsize; 62719370Spst vm_object_t object; 62898944Sobrien int force_wakeup = 0; 62919370Spst int actcount; 63098944Sobrien int vnodes_skipped = 0; 63119370Spst int s; 63246283Sdfr 63319370Spst /* 63498944Sobrien * Do whatever cleanup that the pmap code can. 63598944Sobrien */ 63698944Sobrien pmap_collect(); 63719370Spst 63846283Sdfr addl_page_shortage_init = vm_pageout_deficit; 63998944Sobrien vm_pageout_deficit = 0; 64098944Sobrien 64198944Sobrien if (max_page_launder == 0) 64298944Sobrien max_page_launder = 1; 64398944Sobrien 64419370Spst /* 64598944Sobrien * Calculate the number of pages we want to either free or move 64698944Sobrien * to the cache. 64719370Spst */ 64819370Spst 64919370Spst page_shortage = vm_paging_target() + addl_page_shortage_init; 65019370Spst 65146283Sdfr /* 65298944Sobrien * Figure out what to do with dirty pages when they are encountered. 65319370Spst * Assume that 1/3 of the pages on the inactive list are clean. If 65419370Spst * we think we can reach our target, disable laundering (do not 65598944Sobrien * clean any dirty pages). If we miss the target we will loop back 65698944Sobrien * up and do a laundering run. 65719370Spst */ 65819370Spst 65919370Spst if (cnt.v_inactive_count / 3 > page_shortage) { 66098944Sobrien maxlaunder = 0; 66198944Sobrien launder_loop = 0; 66298944Sobrien } else { 663130803Smarcel maxlaunder = 664130803Smarcel (cnt.v_inactive_target > max_page_launder) ? 66519370Spst max_page_launder : cnt.v_inactive_target; 66698944Sobrien launder_loop = 1; 66798944Sobrien } 66898944Sobrien 66998944Sobrien /* 67019370Spst * Start scanning the inactive queue for pages we can move to the 67198944Sobrien * cache or free. The scan will stop when the target is reached or 67298944Sobrien * we have scanned the entire inactive queue. 67398944Sobrien */ 67498944Sobrien 67598944Sobrienrescan0: 67698944Sobrien addl_page_shortage = addl_page_shortage_init; 67798944Sobrien maxscan = cnt.v_inactive_count; 67898944Sobrien for (m = TAILQ_FIRST(&vm_page_queue_inactive); 67919370Spst m != NULL && maxscan-- > 0 && page_shortage > 0; 68098944Sobrien m = next) { 68198944Sobrien 68298944Sobrien cnt.v_pdpages++; 68398944Sobrien 68498944Sobrien if (m->queue != PQ_INACTIVE) { 68598944Sobrien goto rescan0; 68619370Spst } 68719370Spst 68819370Spst next = TAILQ_NEXT(m, pageq); 68998944Sobrien 69098944Sobrien if (m->hold_count) { 69198944Sobrien s = splvm(); 69298944Sobrien TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq); 69398944Sobrien TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq); 69498944Sobrien splx(s); 69598944Sobrien addl_page_shortage++; 69698944Sobrien continue; 69798944Sobrien } 69819370Spst /* 69919370Spst * Dont mess with busy pages, keep in the front of the 70098944Sobrien * queue, most likely are being paged out. 70198944Sobrien */ 70219370Spst if (m->busy || (m->flags & PG_BUSY)) { 70398944Sobrien addl_page_shortage++; 70419370Spst continue; 70519370Spst } 70619370Spst 70719370Spst /* 70819370Spst * If the object is not being used, we ignore previous 70919370Spst * references. 71098944Sobrien */ 71119370Spst if (m->object->ref_count == 0) { 71219370Spst vm_page_flag_clear(m, PG_REFERENCED); 71319370Spst pmap_clear_reference(VM_PAGE_TO_PHYS(m)); 71446283Sdfr 71519370Spst /* 71619370Spst * Otherwise, if the page has been referenced while in the 71798944Sobrien * inactive queue, we bump the "activation count" upwards, 71819370Spst * making it less likely that the page will be added back to 71946283Sdfr * the inactive queue prematurely again. Here we check the 72046283Sdfr * page tables (or emulated bits, if any), given the upper 72198944Sobrien * level VM system not knowing anything about existing 72219370Spst * references. 72319370Spst */ 72498944Sobrien } else if (((m->flags & PG_REFERENCED) == 0) && 72598944Sobrien (actcount = pmap_ts_referenced(VM_PAGE_TO_PHYS(m)))) { 72698944Sobrien vm_page_activate(m); 72719370Spst m->act_count += (actcount + ACT_ADVANCE); 72819370Spst continue; 72998944Sobrien } 73098944Sobrien 73198944Sobrien /* 73298944Sobrien * If the upper level VM system knows about any page 73319370Spst * references, we activate the page. We also set the 73419370Spst * "activation count" higher than normal so that we will less 73598944Sobrien * likely place pages back onto the inactive queue again. 73619370Spst */ 73798944Sobrien if ((m->flags & PG_REFERENCED) != 0) { 73898944Sobrien vm_page_flag_clear(m, PG_REFERENCED); 73919370Spst actcount = pmap_ts_referenced(VM_PAGE_TO_PHYS(m)); 74019370Spst vm_page_activate(m); 74119370Spst m->act_count += (actcount + ACT_ADVANCE + 1); 74219370Spst continue; 74398944Sobrien } 74419370Spst 74519370Spst /* 74619370Spst * If the upper level VM system doesn't know anything about 74798944Sobrien * the page being dirty, we have to check for it again. As 74898944Sobrien * far as the VM code knows, any partially dirty pages are 74919370Spst * fully dirty. 75019370Spst */ 75119370Spst if (m->dirty == 0) { 75246283Sdfr vm_page_test_dirty(m); 75319370Spst } else { 75498944Sobrien vm_page_dirty(m); 75519370Spst } 75646283Sdfr 75746283Sdfr /* 75898944Sobrien * Invalid pages can be easily freed 75919370Spst */ 76098944Sobrien if (m->valid == 0) { 76119370Spst vm_pageout_page_free(m); 76298944Sobrien cnt.v_dfree++; 76319370Spst --page_shortage; 76498944Sobrien 76519370Spst /* 76619370Spst * Clean pages can be placed onto the cache queue. 767130803Smarcel */ 76898944Sobrien } else if (m->dirty == 0) { 76919370Spst vm_page_cache(m); 77019370Spst --page_shortage; 77119370Spst 77298944Sobrien /* 77319370Spst * Dirty pages need to be paged out. Note that we clean 77446283Sdfr * only a limited number of pages per pagedaemon pass. 77519370Spst */ 77619370Spst } else if (maxlaunder > 0) { 77798944Sobrien int written; 77819370Spst int swap_pageouts_ok; 77998944Sobrien struct vnode *vp = NULL; 78098944Sobrien 78119370Spst object = m->object; 78298944Sobrien 78319370Spst if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) { 78498944Sobrien swap_pageouts_ok = 1; 78519370Spst } else { 78698944Sobrien swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts); 78719370Spst swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts && 78819370Spst vm_page_count_min()); 789130803Smarcel 790130803Smarcel } 79198944Sobrien 79219370Spst /* 79319370Spst * We don't bother paging objects that are "dead". 79419370Spst * Those objects are in a "rundown" state. 79598944Sobrien */ 79619370Spst if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) { 79719370Spst s = splvm(); 79819370Spst TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq); 79946283Sdfr TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq); 80019370Spst splx(s); 80119370Spst continue; 80219370Spst } 80398944Sobrien 80419370Spst /* 80546283Sdfr * For now we protect against potential memory 80698944Sobrien * deadlocks by requiring significant memory to be 80719370Spst * free if the object is not OBJT_DEFAULT or OBJT_SWAP. 80819370Spst * We do not 'trust' any other object type to operate 80998944Sobrien * with low memory, not even OBJT_DEVICE. The VM 81098944Sobrien * allocator will special case allocations done by 81198944Sobrien * the pageout daemon so the check below actually 81219370Spst * does have some hysteresis in it. It isn't the best 81398944Sobrien * solution, though. 81419370Spst */ 81519370Spst 81698944Sobrien if (object->type != OBJT_DEFAULT && 81719370Spst object->type != OBJT_SWAP && 81898944Sobrien cnt.v_free_count < cnt.v_free_reserved) { 81919370Spst s = splvm(); 82019370Spst TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq); 82198944Sobrien TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, 82219370Spst pageq); 82319370Spst splx(s); 82419370Spst continue; 82519370Spst } 82619370Spst 82719370Spst /* 82819370Spst * Presumably we have sufficient free memory to do 82919370Spst * the more sophisticated checks and locking required 83046283Sdfr * for vnodes. 83198944Sobrien * 83219370Spst * The object is already known NOT to be dead. The 83398944Sobrien * vget() may still block, though, because 83419370Spst * VOP_ISLOCKED() doesn't check to see if an inode 83598944Sobrien * (v_data) is associated with the vnode. If it isn't, 83619370Spst * vget() will load in it from disk. Worse, vget() 83719370Spst * may actually get stuck waiting on "inode" if another 83898944Sobrien * process is in the process of bringing the inode in. 839130803Smarcel * This is bad news for us either way. 840130803Smarcel * 84119370Spst * So for the moment we check v_data == NULL as a 84298944Sobrien * workaround. This means that vnodes which do not 84398944Sobrien * use v_data in the way we expect probably will not 84498944Sobrien * wind up being paged out by the pager and it will be 84519370Spst * up to the syncer to get them. That's better then 84698944Sobrien * us blocking here. 84719370Spst * 84898944Sobrien * This whole code section is bogus - we need to fix 84919370Spst * the vnode pager to handle vm_page_t's without us 85019370Spst * having to do any sophisticated VOP tests. 85198944Sobrien */ 852130803Smarcel 85398944Sobrien if (object->type == OBJT_VNODE) { 85498944Sobrien vp = object->handle; 85519370Spst 85698944Sobrien if (VOP_ISLOCKED(vp) || 85719370Spst vp->v_data == NULL || 85846283Sdfr vget(vp, LK_EXCLUSIVE|LK_NOOBJ, curproc)) { 85919370Spst if ((m->queue == PQ_INACTIVE) && 86019370Spst (m->hold_count == 0) && 86119370Spst (m->busy == 0) && 86219370Spst (m->flags & PG_BUSY) == 0) { 86319370Spst s = splvm(); 86419370Spst TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq); 86519370Spst TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq); 86698944Sobrien splx(s); 86719370Spst } 86819370Spst if (object->flags & OBJ_MIGHTBEDIRTY) 86919370Spst vnodes_skipped++; 87019370Spst continue; 87119370Spst } 87219370Spst 87319370Spst /* 87419370Spst * The page might have been moved to another queue 87519370Spst * during potential blocking in vget() above. 87619370Spst */ 87719370Spst if (m->queue != PQ_INACTIVE) { 87819370Spst if (object->flags & OBJ_MIGHTBEDIRTY) 87919370Spst vnodes_skipped++; 88019370Spst vput(vp); 88119370Spst continue; 88219370Spst } 88319370Spst 88419370Spst /* 88519370Spst * The page may have been busied during the blocking in 88619370Spst * vput(); We don't move the page back onto the end of 88719370Spst * the queue so that statistics are more correct if we don't. 88819370Spst */ 88919370Spst if (m->busy || (m->flags & PG_BUSY)) { 89019370Spst vput(vp); 89119370Spst continue; 89298944Sobrien } 89319370Spst 89419370Spst /* 89598944Sobrien * If the page has become held, then skip it 89619370Spst */ 89746283Sdfr if (m->hold_count) { 89898944Sobrien s = splvm(); 89998944Sobrien TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq); 90019370Spst TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq); 90198944Sobrien splx(s); 90219370Spst if (object->flags & OBJ_MIGHTBEDIRTY) 90398944Sobrien vnodes_skipped++; 90498944Sobrien vput(vp); 90519370Spst continue; 90619370Spst } 90719370Spst } 90898944Sobrien 90998944Sobrien /* 91019370Spst * If a page is dirty, then it is either being washed 91119370Spst * (but not yet cleaned) or it is still in the 91219370Spst * laundry. If it is still in the laundry, then we 91346283Sdfr * start the cleaning operation. 91419370Spst */ 91519370Spst written = vm_pageout_clean(m); 91619370Spst if (vp) 91746283Sdfr vput(vp); 91846283Sdfr 91946283Sdfr maxlaunder -= written; 92098944Sobrien } 92119370Spst } 92219370Spst 92319370Spst /* 92498944Sobrien * If we still have a page shortage and we didn't launder anything, 92519370Spst * run the inactive scan again and launder something this time. 92619370Spst */ 92798944Sobrien 92819370Spst if (launder_loop == 0 && page_shortage > 0) { 92998944Sobrien launder_loop = 1; 93098944Sobrien maxlaunder = 93198944Sobrien (cnt.v_inactive_target > max_page_launder) ? 93298944Sobrien max_page_launder : cnt.v_inactive_target; 93319370Spst goto rescan0; 93419370Spst } 93598944Sobrien 936130803Smarcel /* 93719370Spst * Compute the page shortage from the point of view of having to 93898944Sobrien * move pages from the active queue to the inactive queue. 93919370Spst */ 94098944Sobrien 94119370Spst page_shortage = (cnt.v_inactive_target + cnt.v_cache_min) - 94298944Sobrien (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count); 94319370Spst page_shortage += addl_page_shortage; 94419370Spst 94519370Spst /* 94698944Sobrien * Scan the active queue for things we can deactivate 94798944Sobrien */ 948130803Smarcel 94998944Sobrien pcount = cnt.v_active_count; 95098944Sobrien m = TAILQ_FIRST(&vm_page_queue_active); 95119370Spst 95219370Spst while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) { 95319370Spst 95498944Sobrien /* 95519370Spst * This is a consistancy check, and should likely be a panic 95698944Sobrien * or warning. 95719370Spst */ 958130803Smarcel if (m->queue != PQ_ACTIVE) { 95919370Spst break; 96098944Sobrien } 96198944Sobrien 96298944Sobrien next = TAILQ_NEXT(m, pageq); 96319370Spst /* 96498944Sobrien * Don't deactivate pages that are busy. 96598944Sobrien */ 96619370Spst if ((m->busy != 0) || 96719370Spst (m->flags & PG_BUSY) || 96898944Sobrien (m->hold_count != 0)) { 96998944Sobrien s = splvm(); 97019370Spst TAILQ_REMOVE(&vm_page_queue_active, m, pageq); 97146283Sdfr TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq); 97298944Sobrien splx(s); 97319370Spst m = next; 97419370Spst continue; 97598944Sobrien } 97698944Sobrien 97719370Spst /* 97819370Spst * The count for pagedaemon pages is done after checking the 97919370Spst * page for eligbility... 98098944Sobrien */ 98119370Spst cnt.v_pdpages++; 98219370Spst 98398944Sobrien /* 98419370Spst * Check to see "how much" the page has been used. 98519370Spst */ 98646283Sdfr actcount = 0; 987 if (m->object->ref_count != 0) { 988 if (m->flags & PG_REFERENCED) { 989 actcount += 1; 990 } 991 actcount += pmap_ts_referenced(VM_PAGE_TO_PHYS(m)); 992 if (actcount) { 993 m->act_count += ACT_ADVANCE + actcount; 994 if (m->act_count > ACT_MAX) 995 m->act_count = ACT_MAX; 996 } 997 } 998 999 /* 1000 * Since we have "tested" this bit, we need to clear it now. 1001 */ 1002 vm_page_flag_clear(m, PG_REFERENCED); 1003 1004 /* 1005 * Only if an object is currently being used, do we use the 1006 * page activation count stats. 1007 */ 1008 if (actcount && (m->object->ref_count != 0)) { 1009 s = splvm(); 1010 TAILQ_REMOVE(&vm_page_queue_active, m, pageq); 1011 TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq); 1012 splx(s); 1013 } else { 1014 m->act_count -= min(m->act_count, ACT_DECLINE); 1015 if (vm_pageout_algorithm_lru || 1016 (m->object->ref_count == 0) || (m->act_count == 0)) { 1017 page_shortage--; 1018 if (m->object->ref_count == 0) { 1019 vm_page_protect(m, VM_PROT_NONE); 1020 if (m->dirty == 0) 1021 vm_page_cache(m); 1022 else 1023 vm_page_deactivate(m); 1024 } else { 1025 vm_page_deactivate(m); 1026 } 1027 } else { 1028 s = splvm(); 1029 TAILQ_REMOVE(&vm_page_queue_active, m, pageq); 1030 TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq); 1031 splx(s); 1032 } 1033 } 1034 m = next; 1035 } 1036 1037 s = splvm(); 1038 1039 /* 1040 * We try to maintain some *really* free pages, this allows interrupt 1041 * code to be guaranteed space. Since both cache and free queues 1042 * are considered basically 'free', moving pages from cache to free 1043 * does not effect other calculations. 1044 */ 1045 1046 while (cnt.v_free_count < cnt.v_free_reserved) { 1047 static int cache_rover = 0; 1048 m = vm_page_list_find(PQ_CACHE, cache_rover, FALSE); 1049 if (!m) 1050 break; 1051 if ((m->flags & PG_BUSY) || m->busy || m->hold_count || m->wire_count) { 1052#ifdef INVARIANTS 1053 printf("Warning: busy page %p found in cache\n", m); 1054#endif 1055 vm_page_deactivate(m); 1056 continue; 1057 } 1058 cache_rover = (cache_rover + PQ_PRIME2) & PQ_L2_MASK; 1059 vm_pageout_page_free(m); 1060 cnt.v_dfree++; 1061 } 1062 splx(s); 1063 1064#if !defined(NO_SWAPPING) 1065 /* 1066 * Idle process swapout -- run once per second. 1067 */ 1068 if (vm_swap_idle_enabled) { 1069 static long lsec; 1070 if (time_second != lsec) { 1071 vm_pageout_req_swapout |= VM_SWAP_IDLE; 1072 vm_req_vmdaemon(); 1073 lsec = time_second; 1074 } 1075 } 1076#endif 1077 1078 /* 1079 * If we didn't get enough free pages, and we have skipped a vnode 1080 * in a writeable object, wakeup the sync daemon. And kick swapout 1081 * if we did not get enough free pages. 1082 */ 1083 if (vm_paging_target() > 0) { 1084 if (vnodes_skipped && vm_page_count_min()) 1085 (void) speedup_syncer(); 1086#if !defined(NO_SWAPPING) 1087 if (vm_swap_enabled && vm_page_count_target()) { 1088 vm_req_vmdaemon(); 1089 vm_pageout_req_swapout |= VM_SWAP_NORMAL; 1090 } 1091#endif 1092 } 1093 1094 /* 1095 * make sure that we have swap space -- if we are low on memory and 1096 * swap -- then kill the biggest process. 1097 */ 1098 if ((vm_swap_size == 0 || swap_pager_full) && vm_page_count_min()) { 1099 bigproc = NULL; 1100 bigsize = 0; 1101 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 1102 /* 1103 * if this is a system process, skip it 1104 */ 1105 if ((p->p_flag & P_SYSTEM) || (p->p_lock > 0) || 1106 (p->p_pid == 1) || 1107 ((p->p_pid < 48) && (vm_swap_size != 0))) { 1108 continue; 1109 } 1110 /* 1111 * if the process is in a non-running type state, 1112 * don't touch it. 1113 */ 1114 if (p->p_stat != SRUN && p->p_stat != SSLEEP) { 1115 continue; 1116 } 1117 /* 1118 * get the process size 1119 */ 1120 size = vmspace_resident_count(p->p_vmspace); 1121 /* 1122 * if the this process is bigger than the biggest one 1123 * remember it. 1124 */ 1125 if (size > bigsize) { 1126 bigproc = p; 1127 bigsize = size; 1128 } 1129 } 1130 if (bigproc != NULL) { 1131 killproc(bigproc, "out of swap space"); 1132 bigproc->p_estcpu = 0; 1133 bigproc->p_nice = PRIO_MIN; 1134 resetpriority(bigproc); 1135 wakeup(&cnt.v_free_count); 1136 } 1137 } 1138 return force_wakeup; 1139} 1140 1141/* 1142 * This routine tries to maintain the pseudo LRU active queue, 1143 * so that during long periods of time where there is no paging, 1144 * that some statistic accumlation still occurs. This code 1145 * helps the situation where paging just starts to occur. 1146 */ 1147static void 1148vm_pageout_page_stats() 1149{ 1150 int s; 1151 vm_page_t m,next; 1152 int pcount,tpcount; /* Number of pages to check */ 1153 static int fullintervalcount = 0; 1154 int page_shortage; 1155 1156 page_shortage = 1157 (cnt.v_inactive_target + cnt.v_cache_max + cnt.v_free_min) - 1158 (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count); 1159 1160 if (page_shortage <= 0) 1161 return; 1162 1163 pcount = cnt.v_active_count; 1164 fullintervalcount += vm_pageout_stats_interval; 1165 if (fullintervalcount < vm_pageout_full_stats_interval) { 1166 tpcount = (vm_pageout_stats_max * cnt.v_active_count) / cnt.v_page_count; 1167 if (pcount > tpcount) 1168 pcount = tpcount; 1169 } 1170 1171 m = TAILQ_FIRST(&vm_page_queue_active); 1172 while ((m != NULL) && (pcount-- > 0)) { 1173 int actcount; 1174 1175 if (m->queue != PQ_ACTIVE) { 1176 break; 1177 } 1178 1179 next = TAILQ_NEXT(m, pageq); 1180 /* 1181 * Don't deactivate pages that are busy. 1182 */ 1183 if ((m->busy != 0) || 1184 (m->flags & PG_BUSY) || 1185 (m->hold_count != 0)) { 1186 s = splvm(); 1187 TAILQ_REMOVE(&vm_page_queue_active, m, pageq); 1188 TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq); 1189 splx(s); 1190 m = next; 1191 continue; 1192 } 1193 1194 actcount = 0; 1195 if (m->flags & PG_REFERENCED) { 1196 vm_page_flag_clear(m, PG_REFERENCED); 1197 actcount += 1; 1198 } 1199 1200 actcount += pmap_ts_referenced(VM_PAGE_TO_PHYS(m)); 1201 if (actcount) { 1202 m->act_count += ACT_ADVANCE + actcount; 1203 if (m->act_count > ACT_MAX) 1204 m->act_count = ACT_MAX; 1205 s = splvm(); 1206 TAILQ_REMOVE(&vm_page_queue_active, m, pageq); 1207 TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq); 1208 splx(s); 1209 } else { 1210 if (m->act_count == 0) { 1211 /* 1212 * We turn off page access, so that we have more accurate 1213 * RSS stats. We don't do this in the normal page deactivation 1214 * when the system is loaded VM wise, because the cost of 1215 * the large number of page protect operations would be higher 1216 * than the value of doing the operation. 1217 */ 1218 vm_page_protect(m, VM_PROT_NONE); 1219 vm_page_deactivate(m); 1220 } else { 1221 m->act_count -= min(m->act_count, ACT_DECLINE); 1222 s = splvm(); 1223 TAILQ_REMOVE(&vm_page_queue_active, m, pageq); 1224 TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq); 1225 splx(s); 1226 } 1227 } 1228 1229 m = next; 1230 } 1231} 1232 1233static int 1234vm_pageout_free_page_calc(count) 1235vm_size_t count; 1236{ 1237 if (count < cnt.v_page_count) 1238 return 0; 1239 /* 1240 * free_reserved needs to include enough for the largest swap pager 1241 * structures plus enough for any pv_entry structs when paging. 1242 */ 1243 if (cnt.v_page_count > 1024) 1244 cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200; 1245 else 1246 cnt.v_free_min = 4; 1247 cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE + 1248 cnt.v_interrupt_free_min; 1249 cnt.v_free_reserved = vm_pageout_page_count + 1250 cnt.v_pageout_free_min + (count / 768) + PQ_L2_SIZE; 1251 cnt.v_free_severe = cnt.v_free_min / 2; 1252 cnt.v_free_min += cnt.v_free_reserved; 1253 cnt.v_free_severe += cnt.v_free_reserved; 1254 return 1; 1255} 1256 1257 1258/* 1259 * vm_pageout is the high level pageout daemon. 1260 */ 1261static void 1262vm_pageout() 1263{ 1264 /* 1265 * Initialize some paging parameters. 1266 */ 1267 1268 cnt.v_interrupt_free_min = 2; 1269 if (cnt.v_page_count < 2000) 1270 vm_pageout_page_count = 8; 1271 1272 vm_pageout_free_page_calc(cnt.v_page_count); 1273 /* 1274 * free_reserved needs to include enough for the largest swap pager 1275 * structures plus enough for any pv_entry structs when paging. 1276 */ 1277 if (cnt.v_free_count > 6144) 1278 cnt.v_free_target = 3 * cnt.v_free_min + cnt.v_free_reserved; 1279 else 1280 cnt.v_free_target = 2 * cnt.v_free_min + cnt.v_free_reserved; 1281 1282 if (cnt.v_free_count > 2048) { 1283 cnt.v_cache_min = cnt.v_free_target; 1284 cnt.v_cache_max = 2 * cnt.v_cache_min; 1285 cnt.v_inactive_target = (3 * cnt.v_free_target) / 2; 1286 } else { 1287 cnt.v_cache_min = 0; 1288 cnt.v_cache_max = 0; 1289 cnt.v_inactive_target = cnt.v_free_count / 4; 1290 } 1291 if (cnt.v_inactive_target > cnt.v_free_count / 3) 1292 cnt.v_inactive_target = cnt.v_free_count / 3; 1293 1294 /* XXX does not really belong here */ 1295 if (vm_page_max_wired == 0) 1296 vm_page_max_wired = cnt.v_free_count / 3; 1297 1298 if (vm_pageout_stats_max == 0) 1299 vm_pageout_stats_max = cnt.v_free_target; 1300 1301 /* 1302 * Set interval in seconds for stats scan. 1303 */ 1304 if (vm_pageout_stats_interval == 0) 1305 vm_pageout_stats_interval = 5; 1306 if (vm_pageout_full_stats_interval == 0) 1307 vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4; 1308 1309 1310 /* 1311 * Set maximum free per pass 1312 */ 1313 if (vm_pageout_stats_free_max == 0) 1314 vm_pageout_stats_free_max = 5; 1315 1316 max_page_launder = (cnt.v_page_count > 1800 ? 32 : 16); 1317 1318 curproc->p_flag |= P_BUFEXHAUST; 1319 swap_pager_swap_init(); 1320 /* 1321 * The pageout daemon is never done, so loop forever. 1322 */ 1323 while (TRUE) { 1324 int error; 1325 int s = splvm(); 1326 1327 if (vm_pages_needed && vm_page_count_min()) { 1328 /* 1329 * Still not done, sleep a bit and go again 1330 */ 1331 vm_pages_needed = 0; 1332 tsleep(&vm_pages_needed, PVM, "psleep", hz/2); 1333 } else { 1334 /* 1335 * Good enough, sleep & handle stats 1336 */ 1337 vm_pages_needed = 0; 1338 error = tsleep(&vm_pages_needed, 1339 PVM, "psleep", vm_pageout_stats_interval * hz); 1340 if (error && !vm_pages_needed) { 1341 splx(s); 1342 vm_pageout_page_stats(); 1343 continue; 1344 } 1345 } 1346 1347 if (vm_pages_needed) 1348 cnt.v_pdwakeups++; 1349 vm_pages_needed = 0; 1350 splx(s); 1351 vm_pageout_scan(); 1352 vm_pageout_deficit = 0; 1353 wakeup(&cnt.v_free_count); 1354 } 1355} 1356 1357void 1358pagedaemon_wakeup() 1359{ 1360 if (!vm_pages_needed && curproc != pageproc) { 1361 vm_pages_needed++; 1362 wakeup(&vm_pages_needed); 1363 } 1364} 1365 1366#if !defined(NO_SWAPPING) 1367static void 1368vm_req_vmdaemon() 1369{ 1370 static int lastrun = 0; 1371 1372 if ((ticks > (lastrun + hz)) || (ticks < lastrun)) { 1373 wakeup(&vm_daemon_needed); 1374 lastrun = ticks; 1375 } 1376} 1377 1378static void 1379vm_daemon() 1380{ 1381 struct proc *p; 1382 1383 while (TRUE) { 1384 tsleep(&vm_daemon_needed, PPAUSE, "psleep", 0); 1385 if (vm_pageout_req_swapout) { 1386 swapout_procs(vm_pageout_req_swapout); 1387 vm_pageout_req_swapout = 0; 1388 } 1389 /* 1390 * scan the processes for exceeding their rlimits or if 1391 * process is swapped out -- deactivate pages 1392 */ 1393 1394 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 1395 vm_pindex_t limit, size; 1396 1397 /* 1398 * if this is a system process or if we have already 1399 * looked at this process, skip it. 1400 */ 1401 if (p->p_flag & (P_SYSTEM | P_WEXIT)) { 1402 continue; 1403 } 1404 /* 1405 * if the process is in a non-running type state, 1406 * don't touch it. 1407 */ 1408 if (p->p_stat != SRUN && p->p_stat != SSLEEP) { 1409 continue; 1410 } 1411 /* 1412 * get a limit 1413 */ 1414 limit = OFF_TO_IDX( 1415 qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur, 1416 p->p_rlimit[RLIMIT_RSS].rlim_max)); 1417 1418 /* 1419 * let processes that are swapped out really be 1420 * swapped out set the limit to nothing (will force a 1421 * swap-out.) 1422 */ 1423 if ((p->p_flag & P_INMEM) == 0) 1424 limit = 0; /* XXX */ 1425 1426 size = vmspace_resident_count(p->p_vmspace); 1427 if (limit >= 0 && size >= limit) { 1428 vm_pageout_map_deactivate_pages( 1429 &p->p_vmspace->vm_map, limit); 1430 } 1431 } 1432 } 1433} 1434#endif 1435