vm_pageout.c revision 239897
119370Spst/*- 219370Spst * Copyright (c) 1991 Regents of the University of California. 3130803Smarcel * All rights reserved. 4130803Smarcel * Copyright (c) 1994 John S. Dyson 5130803Smarcel * All rights reserved. 698944Sobrien * Copyright (c) 1994 David Greenman 719370Spst * All rights reserved. 898944Sobrien * Copyright (c) 2005 Yahoo! Technologies Norway AS 998944Sobrien * All rights reserved. 1098944Sobrien * 1198944Sobrien * This code is derived from software contributed to Berkeley by 1219370Spst * The Mach Operating System project at Carnegie-Mellon University. 1398944Sobrien * 1498944Sobrien * Redistribution and use in source and binary forms, with or without 1598944Sobrien * modification, are permitted provided that the following conditions 1698944Sobrien * are met: 1719370Spst * 1. Redistributions of source code must retain the above copyright 1898944Sobrien * notice, this list of conditions and the following disclaimer. 1998944Sobrien * 2. Redistributions in binary form must reproduce the above copyright 2098944Sobrien * notice, this list of conditions and the following disclaimer in the 2198944Sobrien * documentation and/or other materials provided with the distribution. 2219370Spst * 3. All advertising materials mentioning features or use of this software 2319370Spst * must display the following acknowledgement: 24130803Smarcel * This product includes software developed by the University of 2519370Spst * California, Berkeley and its contributors. 2619370Spst * 4. Neither the name of the University nor the names of its contributors 2719370Spst * may be used to endorse or promote products derived from this software 2819370Spst * without specific prior written permission. 2919370Spst * 3019370Spst * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 3119370Spst * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 3298944Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33130803Smarcel * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 3498944Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3519370Spst * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3698944Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3798944Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3898944Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3998944Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 4098944Sobrien * SUCH DAMAGE. 4198944Sobrien * 42130803Smarcel * from: @(#)vm_pageout.c 7.4 (Berkeley) 5/7/91 43130803Smarcel * 44130803Smarcel * 4598944Sobrien * Copyright (c) 1987, 1990 Carnegie-Mellon University. 4698944Sobrien * All rights reserved. 4798944Sobrien * 4898944Sobrien * Authors: Avadis Tevanian, Jr., Michael Wayne Young 4998944Sobrien * 5098944Sobrien * Permission to use, copy, modify and distribute this software and 5198944Sobrien * its documentation is hereby granted, provided that both the copyright 5298944Sobrien * notice and this permission notice appear in all copies of the 5398944Sobrien * software, derivative works or modified versions, and any portions 5498944Sobrien * thereof, and that both notices appear in supporting documentation. 5598944Sobrien * 5698944Sobrien * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 5798944Sobrien * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 5819370Spst * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 5919370Spst * 6019370Spst * Carnegie Mellon requests users of this software to return to 6119370Spst * 6219370Spst * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 6319370Spst * School of Computer Science 6419370Spst * Carnegie Mellon University 6519370Spst * Pittsburgh PA 15213-3890 6619370Spst * 6719370Spst * any improvements or extensions that they make and grant Carnegie the 6819370Spst * rights to redistribute these changes. 6919370Spst */ 7019370Spst 7119370Spst/* 7298944Sobrien * The proverbial page-out daemon. 7398944Sobrien */ 7498944Sobrien 7519370Spst#include <sys/cdefs.h> 76130803Smarcel__FBSDID("$FreeBSD: stable/9/sys/vm/vm_pageout.c 239897 2012-08-30 08:37:06Z kib $"); 7719370Spst 7819370Spst#include "opt_vm.h" 7919370Spst#include <sys/param.h> 8019370Spst#include <sys/systm.h> 8119370Spst#include <sys/kernel.h> 8219370Spst#include <sys/eventhandler.h> 8319370Spst#include <sys/lock.h> 8419370Spst#include <sys/mutex.h> 8519370Spst#include <sys/proc.h> 8619370Spst#include <sys/kthread.h> 8746283Sdfr#include <sys/ktr.h> 8819370Spst#include <sys/mount.h> 8919370Spst#include <sys/racct.h> 9019370Spst#include <sys/resourcevar.h> 9119370Spst#include <sys/sched.h> 9219370Spst#include <sys/signalvar.h> 9319370Spst#include <sys/vnode.h> 9419370Spst#include <sys/vmmeter.h> 9519370Spst#include <sys/sx.h> 9619370Spst#include <sys/sysctl.h> 9719370Spst 9819370Spst#include <vm/vm.h> 9919370Spst#include <vm/vm_param.h> 10019370Spst#include <vm/vm_object.h> 10119370Spst#include <vm/vm_page.h> 10219370Spst#include <vm/vm_map.h> 10319370Spst#include <vm/vm_pageout.h> 10498944Sobrien#include <vm/vm_pager.h> 10519370Spst#include <vm/swap_pager.h> 10619370Spst#include <vm/vm_extern.h> 10798944Sobrien#include <vm/uma.h> 10898944Sobrien 10919370Spst/* 11019370Spst * System initialization 11146283Sdfr */ 11219370Spst 11319370Spst/* the kernel process "vm_pageout"*/ 11419370Spststatic void vm_pageout(void); 11519370Spststatic int vm_pageout_clean(vm_page_t); 11698944Sobrienstatic void vm_pageout_scan(int pass); 11746283Sdfr 11819370Spststruct proc *pageproc; 11919370Spst 12019370Spststatic struct kproc_desc page_kp = { 12119370Spst "pagedaemon", 12219370Spst vm_pageout, 12319370Spst &pageproc 12498944Sobrien}; 12519370SpstSYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, 12619370Spst &page_kp); 12719370Spst 12819370Spst#if !defined(NO_SWAPPING) 12919370Spst/* the kernel process "vm_daemon"*/ 13019370Spststatic void vm_daemon(void); 13119370Spststatic struct proc *vmproc; 13219370Spst 13319370Spststatic struct kproc_desc vm_kp = { 13446283Sdfr "vmdaemon", 13598944Sobrien vm_daemon, 13619370Spst &vmproc 13719370Spst}; 13819370SpstSYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp); 13919370Spst#endif 14019370Spst 14119370Spst 14219370Spstint vm_pages_needed; /* Event on which pageout daemon sleeps */ 14319370Spstint vm_pageout_deficit; /* Estimated number of pages deficit */ 14419370Spstint vm_pageout_pages_needed; /* flag saying that the pageout daemon needs pages */ 14519370Spst 14619370Spst#if !defined(NO_SWAPPING) 14746283Sdfrstatic int vm_pageout_req_swapout; /* XXX */ 14819370Spststatic int vm_daemon_needed; 14919370Spststatic struct mtx vm_daemon_mtx; 15098944Sobrien/* Allow for use by vm_pageout before vm_daemon is initialized. */ 15119370SpstMTX_SYSINIT(vm_daemon, &vm_daemon_mtx, "vm daemon", MTX_DEF); 15298944Sobrien#endif 15319370Spststatic int vm_max_launder = 32; 15419370Spststatic int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0; 15598944Sobrienstatic int vm_pageout_full_stats_interval = 0; 15698944Sobrienstatic int vm_pageout_algorithm=0; 15798944Sobrienstatic int defer_swap_pageouts=0; 15819370Spststatic int disable_swap_pageouts=0; 15919370Spst 16019370Spst#if defined(NO_SWAPPING) 16119370Spststatic int vm_swap_enabled=0; 16219370Spststatic int vm_swap_idle_enabled=0; 16346283Sdfr#else 16419370Spststatic int vm_swap_enabled=1; 16519370Spststatic int vm_swap_idle_enabled=0; 16619370Spst#endif 16746283Sdfr 16819370SpstSYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm, 16919370Spst CTLFLAG_RW, &vm_pageout_algorithm, 0, "LRU page mgmt"); 17019370Spst 17119370SpstSYSCTL_INT(_vm, OID_AUTO, max_launder, 17219370Spst CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout"); 17346283Sdfr 17419370SpstSYSCTL_INT(_vm, OID_AUTO, pageout_stats_max, 17519370Spst CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length"); 17619370Spst 17719370SpstSYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval, 17819370Spst CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan"); 17998944Sobrien 18019370SpstSYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval, 18119370Spst CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan"); 18219370Spst 18319370Spst#if defined(NO_SWAPPING) 18419370SpstSYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 18519370Spst CTLFLAG_RD, &vm_swap_enabled, 0, "Enable entire process swapout"); 18619370SpstSYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 18719370Spst CTLFLAG_RD, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria"); 18819370Spst#else 18919370SpstSYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 19019370Spst CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout"); 19146283SdfrSYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 19246283Sdfr CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria"); 19319370Spst#endif 19419370Spst 19519370SpstSYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts, 19619370Spst CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem"); 19719370Spst 19846283SdfrSYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts, 19919370Spst CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages"); 20098944Sobrien 20198944Sobrienstatic int pageout_lock_miss; 20219370SpstSYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss, 20346283Sdfr CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout"); 20419370Spst 20519370Spst#define VM_PAGEOUT_PAGE_COUNT 16 20698944Sobrienint vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT; 20719370Spst 20819370Spstint vm_page_max_wired; /* XXX max # of wired pages system-wide */ 20919370SpstSYSCTL_INT(_vm, OID_AUTO, max_wired, 21019370Spst CTLFLAG_RW, &vm_page_max_wired, 0, "System-wide limit to wired page count"); 211130803Smarcel 21219370Spst#if !defined(NO_SWAPPING) 21319370Spststatic void vm_pageout_map_deactivate_pages(vm_map_t, long); 21419370Spststatic void vm_pageout_object_deactivate_pages(pmap_t, vm_object_t, long); 21598944Sobrienstatic void vm_req_vmdaemon(int req); 21698944Sobrien#endif 21798944Sobrienstatic void vm_pageout_page_stats(void); 21898944Sobrien 21998944Sobrien/* 22019370Spst * Initialize a dummy page for marking the caller's place in the specified 22119370Spst * paging queue. In principle, this function only needs to set the flag 22219370Spst * PG_MARKER. Nonetheless, it sets the flag VPO_BUSY and initializes the hold 223130803Smarcel * count to one as safety precautions. 224130803Smarcel */ 22598944Sobrienstatic void 22619370Spstvm_pageout_init_marker(vm_page_t marker, u_short queue) 22719370Spst{ 22898944Sobrien 22919370Spst bzero(marker, sizeof(*marker)); 23019370Spst marker->flags = PG_MARKER; 23119370Spst marker->oflags = VPO_BUSY; 23298944Sobrien marker->queue = queue; 23319370Spst marker->hold_count = 1; 23446283Sdfr} 235242936Semaste 236242936Semaste/* 23719370Spst * vm_pageout_fallback_object_lock: 23819370Spst * 23919370Spst * Lock vm object currently associated with `m'. VM_OBJECT_TRYLOCK is 24019370Spst * known to have failed and page queue must be either PQ_ACTIVE or 24119370Spst * PQ_INACTIVE. To avoid lock order violation, unlock the page queues 24298944Sobrien * while locking the vm object. Use marker page to detect page queue 24398944Sobrien * changes and maintain notion of next page on page queue. Return 24419370Spst * TRUE if no changes were detected, FALSE otherwise. vm object is 24519370Spst * locked on return. 24619370Spst * 24719370Spst * This function depends on both the lock portion of struct vm_object 24819370Spst * and normal struct vm_page being type stable. 24919370Spst */ 25019370Spstboolean_t 25119370Spstvm_pageout_fallback_object_lock(vm_page_t m, vm_page_t *next) 25219370Spst{ 25319370Spst struct vm_page marker; 25419370Spst boolean_t unchanged; 25519370Spst u_short queue; 25619370Spst vm_object_t object; 25719370Spst 25819370Spst queue = m->queue; 25998944Sobrien vm_pageout_init_marker(&marker, queue); 26046283Sdfr object = m->object; 26119370Spst 26219370Spst TAILQ_INSERT_AFTER(&vm_page_queues[queue].pl, 26319370Spst m, &marker, pageq); 26419370Spst vm_page_unlock_queues(); 26519370Spst vm_page_unlock(m); 26698944Sobrien VM_OBJECT_LOCK(object); 26798944Sobrien vm_page_lock(m); 26898944Sobrien vm_page_lock_queues(); 26919370Spst 27098944Sobrien /* Page queue might have changed. */ 27119370Spst *next = TAILQ_NEXT(&marker, pageq); 27219370Spst unchanged = (m->queue == queue && 27398944Sobrien m->object == object && 27419370Spst &marker == TAILQ_NEXT(m, pageq)); 27519370Spst TAILQ_REMOVE(&vm_page_queues[queue].pl, 27619370Spst &marker, pageq); 27719370Spst return (unchanged); 27819370Spst} 27998944Sobrien 28098944Sobrien/* 28198944Sobrien * Lock the page while holding the page queue lock. Use marker page 28298944Sobrien * to detect page queue changes and maintain notion of next page on 28398944Sobrien * page queue. Return TRUE if no changes were detected, FALSE 28498944Sobrien * otherwise. The page is locked on return. The page queue lock might 285242936Semaste * be dropped and reacquired. 286242936Semaste * 28719370Spst * This function depends on normal struct vm_page being type stable. 28819370Spst */ 28919370Spstboolean_t 29019370Spstvm_pageout_page_lock(vm_page_t m, vm_page_t *next) 29119370Spst{ 29219370Spst struct vm_page marker; 29319370Spst boolean_t unchanged; 29419370Spst u_short queue; 29519370Spst 29619370Spst vm_page_lock_assert(m, MA_NOTOWNED); 29719370Spst mtx_assert(&vm_page_queue_mtx, MA_OWNED); 29819370Spst 29919370Spst if (vm_page_trylock(m)) 30019370Spst return (TRUE); 30198944Sobrien 30298944Sobrien queue = m->queue; 30319370Spst vm_pageout_init_marker(&marker, queue); 30498944Sobrien 30519370Spst TAILQ_INSERT_AFTER(&vm_page_queues[queue].pl, m, &marker, pageq); 30619370Spst vm_page_unlock_queues(); 30798944Sobrien vm_page_lock(m); 30898944Sobrien vm_page_lock_queues(); 30998944Sobrien 31098944Sobrien /* Page queue might have changed. */ 31198944Sobrien *next = TAILQ_NEXT(&marker, pageq); 31298944Sobrien unchanged = (m->queue == queue && &marker == TAILQ_NEXT(m, pageq)); 31398944Sobrien TAILQ_REMOVE(&vm_page_queues[queue].pl, &marker, pageq); 31419370Spst return (unchanged); 31519370Spst} 31646283Sdfr 31719370Spst/* 31819370Spst * vm_pageout_clean: 31919370Spst * 32019370Spst * Clean the page and remove it from the laundry. 32119370Spst * 32219370Spst * We set the busy bit to cause potential page faults on this page to 32346283Sdfr * block. Note the careful timing, however, the busy bit isn't set till 32419370Spst * late and we cannot do anything that will mess with the page. 32519370Spst */ 32619370Spststatic int 32746283Sdfrvm_pageout_clean(vm_page_t m) 32819370Spst{ 32919370Spst vm_object_t object; 33019370Spst vm_page_t mc[2*vm_pageout_page_count], pb, ps; 33119370Spst int pageout_count; 33219370Spst int ib, is, page_base; 33319370Spst vm_pindex_t pindex = m->pindex; 33419370Spst 33519370Spst vm_page_lock_assert(m, MA_OWNED); 33619370Spst object = m->object; 33719370Spst VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 33819370Spst 33919370Spst /* 34019370Spst * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP 34119370Spst * with the new swapper, but we could have serious problems paging 34219370Spst * out other object types if there is insufficient memory. 34319370Spst * 34419370Spst * Unfortunately, checking free memory here is far too late, so the 34519370Spst * check has been moved up a procedural level. 34619370Spst */ 34719370Spst 34819370Spst /* 34946283Sdfr * Can't clean the page if it's busy or held. 35019370Spst */ 35119370Spst KASSERT(m->busy == 0 && (m->oflags & VPO_BUSY) == 0, 35219370Spst ("vm_pageout_clean: page %p is busy", m)); 35398944Sobrien KASSERT(m->hold_count == 0, ("vm_pageout_clean: page %p is held", m)); 35419370Spst vm_page_unlock(m); 35519370Spst 35619370Spst mc[vm_pageout_page_count] = pb = ps = m; 35719370Spst pageout_count = 1; 35819370Spst page_base = vm_pageout_page_count; 35919370Spst ib = 1; 36019370Spst is = 1; 36119370Spst 36219370Spst /* 36319370Spst * Scan object for clusterable pages. 36446283Sdfr * 36519370Spst * We can cluster ONLY if: ->> the page is NOT 36619370Spst * clean, wired, busy, held, or mapped into a 36746283Sdfr * buffer, and one of the following: 36819370Spst * 1) The page is inactive, or a seldom used 36919370Spst * active page. 37019370Spst * -or- 37119370Spst * 2) we force the issue. 37219370Spst * 37319370Spst * During heavy mmap/modification loads the pageout 37419370Spst * daemon can really fragment the underlying file 37519370Spst * due to flushing pages out of order and not trying 37619370Spst * align the clusters (which leave sporatic out-of-order 37719370Spst * holes). To solve this problem we do the reverse scan 37819370Spst * first and attempt to align our cluster, then do a 37998944Sobrien * forward scan if room remains. 38098944Sobrien */ 38198944Sobrienmore: 38219370Spst while (ib && pageout_count < vm_pageout_page_count) { 38398944Sobrien vm_page_t p; 38498944Sobrien 38519370Spst if (ib > pindex) { 38619370Spst ib = 0; 38719370Spst break; 38819370Spst } 38919370Spst 39019370Spst if ((p = vm_page_prev(pb)) == NULL || 39146283Sdfr (p->oflags & VPO_BUSY) != 0 || p->busy != 0) { 39219370Spst ib = 0; 39319370Spst break; 39419370Spst } 39546283Sdfr vm_page_lock(p); 39619370Spst vm_page_test_dirty(p); 39719370Spst if (p->dirty == 0 || 39819370Spst p->queue != PQ_INACTIVE || 39919370Spst p->hold_count != 0) { /* may be undergoing I/O */ 40019370Spst vm_page_unlock(p); 40119370Spst ib = 0; 40219370Spst break; 40346283Sdfr } 40419370Spst vm_page_unlock(p); 40519370Spst mc[--page_base] = pb = p; 40619370Spst ++pageout_count; 40719370Spst ++ib; 40819370Spst /* 40919370Spst * alignment boundry, stop here and switch directions. Do 41019370Spst * not clear ib. 41119370Spst */ 41219370Spst if ((pindex - (ib - 1)) % vm_pageout_page_count == 0) 41346283Sdfr break; 41419370Spst } 41519370Spst 41619370Spst while (pageout_count < vm_pageout_page_count && 41798944Sobrien pindex + is < object->size) { 41898944Sobrien vm_page_t p; 41998944Sobrien 42098944Sobrien if ((p = vm_page_next(ps)) == NULL || 42198944Sobrien (p->oflags & VPO_BUSY) != 0 || p->busy != 0) 42219370Spst break; 42398944Sobrien vm_page_lock(p); 42419370Spst vm_page_test_dirty(p); 42519370Spst if (p->dirty == 0 || 42619370Spst p->queue != PQ_INACTIVE || 42719370Spst p->hold_count != 0) { /* may be undergoing I/O */ 42819370Spst vm_page_unlock(p); 42919370Spst break; 43046283Sdfr } 43119370Spst vm_page_unlock(p); 43219370Spst mc[page_base + pageout_count] = ps = p; 43319370Spst ++pageout_count; 43446283Sdfr ++is; 43519370Spst } 43619370Spst 43719370Spst /* 43846283Sdfr * If we exhausted our forward scan, continue with the reverse scan 43998944Sobrien * when possible, even past a page boundry. This catches boundry 44098944Sobrien * conditions. 44198944Sobrien */ 44298944Sobrien if (ib && pageout_count < vm_pageout_page_count) 44398944Sobrien goto more; 44498944Sobrien 44546283Sdfr /* 44619370Spst * we allow reads during pageouts... 44719370Spst */ 44819370Spst return (vm_pageout_flush(&mc[page_base], pageout_count, 0, 0, NULL, 44919370Spst NULL)); 45019370Spst} 45119370Spst 45219370Spst/* 45319370Spst * vm_pageout_flush() - launder the given pages 45419370Spst * 45519370Spst * The given pages are laundered. Note that we setup for the start of 45698944Sobrien * I/O ( i.e. busy the page ), mark it read-only, and bump the object 45798944Sobrien * reference count all in here rather then in the parent. If we want 45819370Spst * the parent to do more sophisticated things we may have to change 45919370Spst * the ordering. 46019370Spst * 46198944Sobrien * Returned runlen is the count of pages between mreq and first 46298944Sobrien * page after mreq with status VM_PAGER_AGAIN. 46398944Sobrien * *eio is set to TRUE if pager returned VM_PAGER_ERROR or VM_PAGER_FAIL 46498944Sobrien * for any page in runlen set. 46598944Sobrien */ 46698944Sobrienint 46798944Sobrienvm_pageout_flush(vm_page_t *mc, int count, int flags, int mreq, int *prunlen, 46898944Sobrien boolean_t *eio) 46998944Sobrien{ 47098944Sobrien vm_object_t object = mc[0]->object; 47198944Sobrien int pageout_status[count]; 47298944Sobrien int numpagedout = 0; 47398944Sobrien int i, runlen; 47498944Sobrien 47598944Sobrien VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 47698944Sobrien mtx_assert(&vm_page_queue_mtx, MA_NOTOWNED); 47798944Sobrien 47898944Sobrien /* 47998944Sobrien * Initiate I/O. Bump the vm_page_t->busy counter and 48098944Sobrien * mark the pages read-only. 48198944Sobrien * 48298944Sobrien * We do not have to fixup the clean/dirty bits here... we can 48319370Spst * allow the pager to do it after the I/O completes. 48419370Spst * 48519370Spst * NOTE! mc[i]->dirty may be partial or fragmented due to an 48619370Spst * edge case with file fragments. 48719370Spst */ 48819370Spst for (i = 0; i < count; i++) { 48919370Spst KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL, 49019370Spst ("vm_pageout_flush: partially invalid page %p index %d/%d", 49198944Sobrien mc[i], i, count)); 49298944Sobrien vm_page_io_start(mc[i]); 49319370Spst pmap_remove_write(mc[i]); 49419370Spst } 49598944Sobrien vm_object_pip_add(object, count); 49646283Sdfr 49798944Sobrien vm_pager_put_pages(object, mc, count, flags, pageout_status); 49819370Spst 49919370Spst runlen = count - mreq; 50019370Spst if (eio != NULL) 50119370Spst *eio = FALSE; 50219370Spst for (i = 0; i < count; i++) { 50319370Spst vm_page_t mt = mc[i]; 50419370Spst 50519370Spst KASSERT(pageout_status[i] == VM_PAGER_PEND || 50619370Spst (mt->aflags & PGA_WRITEABLE) == 0, 50719370Spst ("vm_pageout_flush: page %p is not write protected", mt)); 50898944Sobrien switch (pageout_status[i]) { 50919370Spst case VM_PAGER_OK: 51019370Spst case VM_PAGER_PEND: 51119370Spst numpagedout++; 512130803Smarcel break; 51319370Spst case VM_PAGER_BAD: 51419370Spst /* 51519370Spst * Page outside of range of object. Right now we 51646283Sdfr * essentially lose the changes by pretending it 51798944Sobrien * worked. 51898944Sobrien */ 51998944Sobrien vm_page_undirty(mt); 52098944Sobrien break; 52198944Sobrien case VM_PAGER_ERROR: 52298944Sobrien case VM_PAGER_FAIL: 52398944Sobrien /* 52498944Sobrien * If page couldn't be paged out, then reactivate the 52598944Sobrien * page so it doesn't clog the inactive list. (We 52698944Sobrien * will try paging out it again later). 52798944Sobrien */ 52898944Sobrien vm_page_lock(mt); 52998944Sobrien vm_page_activate(mt); 53046283Sdfr vm_page_unlock(mt); 53198944Sobrien if (eio != NULL && i >= mreq && i - mreq < runlen) 53298944Sobrien *eio = TRUE; 53398944Sobrien break; 53498944Sobrien case VM_PAGER_AGAIN: 53598944Sobrien if (i >= mreq && i - mreq < runlen) 53698944Sobrien runlen = i - mreq; 53798944Sobrien break; 53898944Sobrien } 53998944Sobrien 54098944Sobrien /* 54198944Sobrien * If the operation is still going, leave the page busy to 54298944Sobrien * block all other accesses. Also, leave the paging in 54398944Sobrien * progress indicator set so that we don't attempt an object 54498944Sobrien * collapse. 54598944Sobrien */ 54698944Sobrien if (pageout_status[i] != VM_PAGER_PEND) { 54798944Sobrien vm_object_pip_wakeup(object); 54898944Sobrien vm_page_io_finish(mt); 54998944Sobrien if (vm_page_count_severe()) { 55046283Sdfr vm_page_lock(mt); 55198944Sobrien vm_page_try_to_cache(mt); 55246283Sdfr vm_page_unlock(mt); 55398944Sobrien } 55419370Spst } 55519370Spst } 55698944Sobrien if (prunlen != NULL) 55719370Spst *prunlen = runlen; 55819370Spst return (numpagedout); 55919370Spst} 56019370Spst 56119370Spst#if !defined(NO_SWAPPING) 56246283Sdfr/* 56346283Sdfr * vm_pageout_object_deactivate_pages 56446283Sdfr * 56546283Sdfr * Deactivate enough pages to satisfy the inactive target 56698944Sobrien * requirements. 56798944Sobrien * 56898944Sobrien * The object and map must be locked. 56998944Sobrien */ 57098944Sobrienstatic void 57198944Sobrienvm_pageout_object_deactivate_pages(pmap_t pmap, vm_object_t first_object, 57298944Sobrien long desired) 57398944Sobrien{ 57498944Sobrien vm_object_t backing_object, object; 57598944Sobrien vm_page_t p; 57698944Sobrien int actcount, remove_mode; 57798944Sobrien 57898944Sobrien VM_OBJECT_LOCK_ASSERT(first_object, MA_OWNED); 57946283Sdfr if (first_object->type == OBJT_DEVICE || 58098944Sobrien first_object->type == OBJT_SG) 58198944Sobrien return; 58298944Sobrien for (object = first_object;; object = backing_object) { 58398944Sobrien if (pmap_resident_count(pmap) <= desired) 58498944Sobrien goto unlock_return; 58598944Sobrien VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 58698944Sobrien if (object->type == OBJT_PHYS || object->paging_in_progress) 58746283Sdfr goto unlock_return; 58846283Sdfr 58998944Sobrien remove_mode = 0; 590130803Smarcel if (object->shadow_count > 1) 591130803Smarcel remove_mode = 1; 592130803Smarcel /* 59346283Sdfr * Scan the object's entire memory queue. 59419370Spst */ 595 TAILQ_FOREACH(p, &object->memq, listq) { 596 if (pmap_resident_count(pmap) <= desired) 597 goto unlock_return; 598 if ((p->oflags & VPO_BUSY) != 0 || p->busy != 0) 599 continue; 600 PCPU_INC(cnt.v_pdpages); 601 vm_page_lock(p); 602 if (p->wire_count != 0 || p->hold_count != 0 || 603 !pmap_page_exists_quick(pmap, p)) { 604 vm_page_unlock(p); 605 continue; 606 } 607 actcount = pmap_ts_referenced(p); 608 if ((p->aflags & PGA_REFERENCED) != 0) { 609 if (actcount == 0) 610 actcount = 1; 611 vm_page_aflag_clear(p, PGA_REFERENCED); 612 } 613 if (p->queue != PQ_ACTIVE && actcount != 0) { 614 vm_page_activate(p); 615 p->act_count += actcount; 616 } else if (p->queue == PQ_ACTIVE) { 617 if (actcount == 0) { 618 p->act_count -= min(p->act_count, 619 ACT_DECLINE); 620 if (!remove_mode && 621 (vm_pageout_algorithm || 622 p->act_count == 0)) { 623 pmap_remove_all(p); 624 vm_page_deactivate(p); 625 } else { 626 vm_page_lock_queues(); 627 vm_page_requeue(p); 628 vm_page_unlock_queues(); 629 } 630 } else { 631 vm_page_activate(p); 632 if (p->act_count < ACT_MAX - 633 ACT_ADVANCE) 634 p->act_count += ACT_ADVANCE; 635 vm_page_lock_queues(); 636 vm_page_requeue(p); 637 vm_page_unlock_queues(); 638 } 639 } else if (p->queue == PQ_INACTIVE) 640 pmap_remove_all(p); 641 vm_page_unlock(p); 642 } 643 if ((backing_object = object->backing_object) == NULL) 644 goto unlock_return; 645 VM_OBJECT_LOCK(backing_object); 646 if (object != first_object) 647 VM_OBJECT_UNLOCK(object); 648 } 649unlock_return: 650 if (object != first_object) 651 VM_OBJECT_UNLOCK(object); 652} 653 654/* 655 * deactivate some number of pages in a map, try to do it fairly, but 656 * that is really hard to do. 657 */ 658static void 659vm_pageout_map_deactivate_pages(map, desired) 660 vm_map_t map; 661 long desired; 662{ 663 vm_map_entry_t tmpe; 664 vm_object_t obj, bigobj; 665 int nothingwired; 666 667 if (!vm_map_trylock(map)) 668 return; 669 670 bigobj = NULL; 671 nothingwired = TRUE; 672 673 /* 674 * first, search out the biggest object, and try to free pages from 675 * that. 676 */ 677 tmpe = map->header.next; 678 while (tmpe != &map->header) { 679 if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 680 obj = tmpe->object.vm_object; 681 if (obj != NULL && VM_OBJECT_TRYLOCK(obj)) { 682 if (obj->shadow_count <= 1 && 683 (bigobj == NULL || 684 bigobj->resident_page_count < obj->resident_page_count)) { 685 if (bigobj != NULL) 686 VM_OBJECT_UNLOCK(bigobj); 687 bigobj = obj; 688 } else 689 VM_OBJECT_UNLOCK(obj); 690 } 691 } 692 if (tmpe->wired_count > 0) 693 nothingwired = FALSE; 694 tmpe = tmpe->next; 695 } 696 697 if (bigobj != NULL) { 698 vm_pageout_object_deactivate_pages(map->pmap, bigobj, desired); 699 VM_OBJECT_UNLOCK(bigobj); 700 } 701 /* 702 * Next, hunt around for other pages to deactivate. We actually 703 * do this search sort of wrong -- .text first is not the best idea. 704 */ 705 tmpe = map->header.next; 706 while (tmpe != &map->header) { 707 if (pmap_resident_count(vm_map_pmap(map)) <= desired) 708 break; 709 if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 710 obj = tmpe->object.vm_object; 711 if (obj != NULL) { 712 VM_OBJECT_LOCK(obj); 713 vm_pageout_object_deactivate_pages(map->pmap, obj, desired); 714 VM_OBJECT_UNLOCK(obj); 715 } 716 } 717 tmpe = tmpe->next; 718 } 719 720 /* 721 * Remove all mappings if a process is swapped out, this will free page 722 * table pages. 723 */ 724 if (desired == 0 && nothingwired) { 725 pmap_remove(vm_map_pmap(map), vm_map_min(map), 726 vm_map_max(map)); 727 } 728 vm_map_unlock(map); 729} 730#endif /* !defined(NO_SWAPPING) */ 731 732/* 733 * vm_pageout_scan does the dirty work for the pageout daemon. 734 */ 735static void 736vm_pageout_scan(int pass) 737{ 738 vm_page_t m, next; 739 struct vm_page marker; 740 int page_shortage, maxscan, pcount; 741 int addl_page_shortage, addl_page_shortage_init; 742 vm_object_t object; 743 int actcount; 744 int vnodes_skipped = 0; 745 int maxlaunder; 746 747 /* 748 * Decrease registered cache sizes. 749 */ 750 EVENTHANDLER_INVOKE(vm_lowmem, 0); 751 /* 752 * We do this explicitly after the caches have been drained above. 753 */ 754 uma_reclaim(); 755 756 addl_page_shortage_init = atomic_readandclear_int(&vm_pageout_deficit); 757 758 /* 759 * Calculate the number of pages we want to either free or move 760 * to the cache. 761 */ 762 page_shortage = vm_paging_target() + addl_page_shortage_init; 763 764 vm_pageout_init_marker(&marker, PQ_INACTIVE); 765 766 /* 767 * Start scanning the inactive queue for pages we can move to the 768 * cache or free. The scan will stop when the target is reached or 769 * we have scanned the entire inactive queue. Note that m->act_count 770 * is not used to form decisions for the inactive queue, only for the 771 * active queue. 772 * 773 * maxlaunder limits the number of dirty pages we flush per scan. 774 * For most systems a smaller value (16 or 32) is more robust under 775 * extreme memory and disk pressure because any unnecessary writes 776 * to disk can result in extreme performance degredation. However, 777 * systems with excessive dirty pages (especially when MAP_NOSYNC is 778 * used) will die horribly with limited laundering. If the pageout 779 * daemon cannot clean enough pages in the first pass, we let it go 780 * all out in succeeding passes. 781 */ 782 if ((maxlaunder = vm_max_launder) <= 1) 783 maxlaunder = 1; 784 if (pass) 785 maxlaunder = 10000; 786 vm_page_lock_queues(); 787rescan0: 788 addl_page_shortage = addl_page_shortage_init; 789 maxscan = cnt.v_inactive_count; 790 791 for (m = TAILQ_FIRST(&vm_page_queues[PQ_INACTIVE].pl); 792 m != NULL && maxscan-- > 0 && page_shortage > 0; 793 m = next) { 794 795 cnt.v_pdpages++; 796 797 if (m->queue != PQ_INACTIVE) 798 goto rescan0; 799 800 next = TAILQ_NEXT(m, pageq); 801 802 /* 803 * skip marker pages 804 */ 805 if (m->flags & PG_MARKER) 806 continue; 807 808 KASSERT((m->flags & PG_FICTITIOUS) == 0, 809 ("Fictitious page %p cannot be in inactive queue", m)); 810 KASSERT((m->oflags & VPO_UNMANAGED) == 0, 811 ("Unmanaged page %p cannot be in inactive queue", m)); 812 813 /* 814 * Lock the page. 815 */ 816 if (!vm_pageout_page_lock(m, &next)) { 817 vm_page_unlock(m); 818 addl_page_shortage++; 819 continue; 820 } 821 822 /* 823 * A held page may be undergoing I/O, so skip it. 824 */ 825 if (m->hold_count) { 826 vm_page_unlock(m); 827 vm_page_requeue(m); 828 addl_page_shortage++; 829 continue; 830 } 831 832 /* 833 * Don't mess with busy pages, keep in the front of the 834 * queue, most likely are being paged out. 835 */ 836 object = m->object; 837 if (!VM_OBJECT_TRYLOCK(object) && 838 (!vm_pageout_fallback_object_lock(m, &next) || 839 m->hold_count != 0)) { 840 VM_OBJECT_UNLOCK(object); 841 vm_page_unlock(m); 842 addl_page_shortage++; 843 continue; 844 } 845 if (m->busy || (m->oflags & VPO_BUSY)) { 846 vm_page_unlock(m); 847 VM_OBJECT_UNLOCK(object); 848 addl_page_shortage++; 849 continue; 850 } 851 852 /* 853 * If the object is not being used, we ignore previous 854 * references. 855 */ 856 if (object->ref_count == 0) { 857 vm_page_aflag_clear(m, PGA_REFERENCED); 858 KASSERT(!pmap_page_is_mapped(m), 859 ("vm_pageout_scan: page %p is mapped", m)); 860 861 /* 862 * Otherwise, if the page has been referenced while in the 863 * inactive queue, we bump the "activation count" upwards, 864 * making it less likely that the page will be added back to 865 * the inactive queue prematurely again. Here we check the 866 * page tables (or emulated bits, if any), given the upper 867 * level VM system not knowing anything about existing 868 * references. 869 */ 870 } else if ((m->aflags & PGA_REFERENCED) == 0 && 871 (actcount = pmap_ts_referenced(m)) != 0) { 872 vm_page_activate(m); 873 vm_page_unlock(m); 874 m->act_count += actcount + ACT_ADVANCE; 875 VM_OBJECT_UNLOCK(object); 876 continue; 877 } 878 879 /* 880 * If the upper level VM system knows about any page 881 * references, we activate the page. We also set the 882 * "activation count" higher than normal so that we will less 883 * likely place pages back onto the inactive queue again. 884 */ 885 if ((m->aflags & PGA_REFERENCED) != 0) { 886 vm_page_aflag_clear(m, PGA_REFERENCED); 887 actcount = pmap_ts_referenced(m); 888 vm_page_activate(m); 889 vm_page_unlock(m); 890 m->act_count += actcount + ACT_ADVANCE + 1; 891 VM_OBJECT_UNLOCK(object); 892 continue; 893 } 894 895 /* 896 * If the upper level VM system does not believe that the page 897 * is fully dirty, but it is mapped for write access, then we 898 * consult the pmap to see if the page's dirty status should 899 * be updated. 900 */ 901 if (m->dirty != VM_PAGE_BITS_ALL && 902 (m->aflags & PGA_WRITEABLE) != 0) { 903 /* 904 * Avoid a race condition: Unless write access is 905 * removed from the page, another processor could 906 * modify it before all access is removed by the call 907 * to vm_page_cache() below. If vm_page_cache() finds 908 * that the page has been modified when it removes all 909 * access, it panics because it cannot cache dirty 910 * pages. In principle, we could eliminate just write 911 * access here rather than all access. In the expected 912 * case, when there are no last instant modifications 913 * to the page, removing all access will be cheaper 914 * overall. 915 */ 916 if (pmap_is_modified(m)) 917 vm_page_dirty(m); 918 else if (m->dirty == 0) 919 pmap_remove_all(m); 920 } 921 922 if (m->valid == 0) { 923 /* 924 * Invalid pages can be easily freed 925 */ 926 vm_page_free(m); 927 cnt.v_dfree++; 928 --page_shortage; 929 } else if (m->dirty == 0) { 930 /* 931 * Clean pages can be placed onto the cache queue. 932 * This effectively frees them. 933 */ 934 vm_page_cache(m); 935 --page_shortage; 936 } else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) { 937 /* 938 * Dirty pages need to be paged out, but flushing 939 * a page is extremely expensive verses freeing 940 * a clean page. Rather then artificially limiting 941 * the number of pages we can flush, we instead give 942 * dirty pages extra priority on the inactive queue 943 * by forcing them to be cycled through the queue 944 * twice before being flushed, after which the 945 * (now clean) page will cycle through once more 946 * before being freed. This significantly extends 947 * the thrash point for a heavily loaded machine. 948 */ 949 m->flags |= PG_WINATCFLS; 950 vm_page_requeue(m); 951 } else if (maxlaunder > 0) { 952 /* 953 * We always want to try to flush some dirty pages if 954 * we encounter them, to keep the system stable. 955 * Normally this number is small, but under extreme 956 * pressure where there are insufficient clean pages 957 * on the inactive queue, we may have to go all out. 958 */ 959 int swap_pageouts_ok, vfslocked = 0; 960 struct vnode *vp = NULL; 961 struct mount *mp = NULL; 962 963 if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) { 964 swap_pageouts_ok = 1; 965 } else { 966 swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts); 967 swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts && 968 vm_page_count_min()); 969 970 } 971 972 /* 973 * We don't bother paging objects that are "dead". 974 * Those objects are in a "rundown" state. 975 */ 976 if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) { 977 vm_page_unlock(m); 978 VM_OBJECT_UNLOCK(object); 979 vm_page_requeue(m); 980 continue; 981 } 982 983 /* 984 * Following operations may unlock 985 * vm_page_queue_mtx, invalidating the 'next' 986 * pointer. To prevent an inordinate number 987 * of restarts we use our marker to remember 988 * our place. 989 * 990 */ 991 TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE].pl, 992 m, &marker, pageq); 993 /* 994 * The object is already known NOT to be dead. It 995 * is possible for the vget() to block the whole 996 * pageout daemon, but the new low-memory handling 997 * code should prevent it. 998 * 999 * The previous code skipped locked vnodes and, worse, 1000 * reordered pages in the queue. This results in 1001 * completely non-deterministic operation and, on a 1002 * busy system, can lead to extremely non-optimal 1003 * pageouts. For example, it can cause clean pages 1004 * to be freed and dirty pages to be moved to the end 1005 * of the queue. Since dirty pages are also moved to 1006 * the end of the queue once-cleaned, this gives 1007 * way too large a weighting to defering the freeing 1008 * of dirty pages. 1009 * 1010 * We can't wait forever for the vnode lock, we might 1011 * deadlock due to a vn_read() getting stuck in 1012 * vm_wait while holding this vnode. We skip the 1013 * vnode if we can't get it in a reasonable amount 1014 * of time. 1015 */ 1016 if (object->type == OBJT_VNODE) { 1017 vm_page_unlock_queues(); 1018 vm_page_unlock(m); 1019 vp = object->handle; 1020 if (vp->v_type == VREG && 1021 vn_start_write(vp, &mp, V_NOWAIT) != 0) { 1022 mp = NULL; 1023 ++pageout_lock_miss; 1024 if (object->flags & OBJ_MIGHTBEDIRTY) 1025 vnodes_skipped++; 1026 vm_page_lock_queues(); 1027 goto unlock_and_continue; 1028 } 1029 KASSERT(mp != NULL, 1030 ("vp %p with NULL v_mount", vp)); 1031 vm_object_reference_locked(object); 1032 VM_OBJECT_UNLOCK(object); 1033 vfslocked = VFS_LOCK_GIANT(vp->v_mount); 1034 if (vget(vp, LK_EXCLUSIVE | LK_TIMELOCK, 1035 curthread)) { 1036 VM_OBJECT_LOCK(object); 1037 vm_page_lock_queues(); 1038 ++pageout_lock_miss; 1039 if (object->flags & OBJ_MIGHTBEDIRTY) 1040 vnodes_skipped++; 1041 vp = NULL; 1042 goto unlock_and_continue; 1043 } 1044 VM_OBJECT_LOCK(object); 1045 vm_page_lock(m); 1046 vm_page_lock_queues(); 1047 /* 1048 * The page might have been moved to another 1049 * queue during potential blocking in vget() 1050 * above. The page might have been freed and 1051 * reused for another vnode. 1052 */ 1053 if (m->queue != PQ_INACTIVE || 1054 m->object != object || 1055 TAILQ_NEXT(m, pageq) != &marker) { 1056 vm_page_unlock(m); 1057 if (object->flags & OBJ_MIGHTBEDIRTY) 1058 vnodes_skipped++; 1059 goto unlock_and_continue; 1060 } 1061 1062 /* 1063 * The page may have been busied during the 1064 * blocking in vget(). We don't move the 1065 * page back onto the end of the queue so that 1066 * statistics are more correct if we don't. 1067 */ 1068 if (m->busy || (m->oflags & VPO_BUSY)) { 1069 vm_page_unlock(m); 1070 goto unlock_and_continue; 1071 } 1072 1073 /* 1074 * If the page has become held it might 1075 * be undergoing I/O, so skip it 1076 */ 1077 if (m->hold_count) { 1078 vm_page_unlock(m); 1079 vm_page_requeue(m); 1080 if (object->flags & OBJ_MIGHTBEDIRTY) 1081 vnodes_skipped++; 1082 goto unlock_and_continue; 1083 } 1084 } 1085 1086 /* 1087 * If a page is dirty, then it is either being washed 1088 * (but not yet cleaned) or it is still in the 1089 * laundry. If it is still in the laundry, then we 1090 * start the cleaning operation. 1091 * 1092 * decrement page_shortage on success to account for 1093 * the (future) cleaned page. Otherwise we could wind 1094 * up laundering or cleaning too many pages. 1095 */ 1096 vm_page_unlock_queues(); 1097 if (vm_pageout_clean(m) != 0) { 1098 --page_shortage; 1099 --maxlaunder; 1100 } 1101 vm_page_lock_queues(); 1102unlock_and_continue: 1103 vm_page_lock_assert(m, MA_NOTOWNED); 1104 VM_OBJECT_UNLOCK(object); 1105 if (mp != NULL) { 1106 vm_page_unlock_queues(); 1107 if (vp != NULL) 1108 vput(vp); 1109 VFS_UNLOCK_GIANT(vfslocked); 1110 vm_object_deallocate(object); 1111 vn_finished_write(mp); 1112 vm_page_lock_queues(); 1113 } 1114 next = TAILQ_NEXT(&marker, pageq); 1115 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, 1116 &marker, pageq); 1117 vm_page_lock_assert(m, MA_NOTOWNED); 1118 continue; 1119 } 1120 vm_page_unlock(m); 1121 VM_OBJECT_UNLOCK(object); 1122 } 1123 1124 /* 1125 * Compute the number of pages we want to try to move from the 1126 * active queue to the inactive queue. 1127 */ 1128 page_shortage = vm_paging_target() + 1129 cnt.v_inactive_target - cnt.v_inactive_count; 1130 page_shortage += addl_page_shortage; 1131 1132 /* 1133 * Scan the active queue for things we can deactivate. We nominally 1134 * track the per-page activity counter and use it to locate 1135 * deactivation candidates. 1136 */ 1137 pcount = cnt.v_active_count; 1138 m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl); 1139 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1140 1141 while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) { 1142 1143 KASSERT(m->queue == PQ_ACTIVE, 1144 ("vm_pageout_scan: page %p isn't active", m)); 1145 1146 next = TAILQ_NEXT(m, pageq); 1147 if ((m->flags & PG_MARKER) != 0) { 1148 m = next; 1149 continue; 1150 } 1151 KASSERT((m->flags & PG_FICTITIOUS) == 0, 1152 ("Fictitious page %p cannot be in active queue", m)); 1153 KASSERT((m->oflags & VPO_UNMANAGED) == 0, 1154 ("Unmanaged page %p cannot be in active queue", m)); 1155 if (!vm_pageout_page_lock(m, &next)) { 1156 vm_page_unlock(m); 1157 m = next; 1158 continue; 1159 } 1160 object = m->object; 1161 if (!VM_OBJECT_TRYLOCK(object) && 1162 !vm_pageout_fallback_object_lock(m, &next)) { 1163 VM_OBJECT_UNLOCK(object); 1164 vm_page_unlock(m); 1165 m = next; 1166 continue; 1167 } 1168 1169 /* 1170 * Don't deactivate pages that are busy. 1171 */ 1172 if ((m->busy != 0) || 1173 (m->oflags & VPO_BUSY) || 1174 (m->hold_count != 0)) { 1175 vm_page_unlock(m); 1176 VM_OBJECT_UNLOCK(object); 1177 vm_page_requeue(m); 1178 m = next; 1179 continue; 1180 } 1181 1182 /* 1183 * The count for pagedaemon pages is done after checking the 1184 * page for eligibility... 1185 */ 1186 cnt.v_pdpages++; 1187 1188 /* 1189 * Check to see "how much" the page has been used. 1190 */ 1191 actcount = 0; 1192 if (object->ref_count != 0) { 1193 if (m->aflags & PGA_REFERENCED) { 1194 actcount += 1; 1195 } 1196 actcount += pmap_ts_referenced(m); 1197 if (actcount) { 1198 m->act_count += ACT_ADVANCE + actcount; 1199 if (m->act_count > ACT_MAX) 1200 m->act_count = ACT_MAX; 1201 } 1202 } 1203 1204 /* 1205 * Since we have "tested" this bit, we need to clear it now. 1206 */ 1207 vm_page_aflag_clear(m, PGA_REFERENCED); 1208 1209 /* 1210 * Only if an object is currently being used, do we use the 1211 * page activation count stats. 1212 */ 1213 if (actcount && (object->ref_count != 0)) { 1214 vm_page_requeue(m); 1215 } else { 1216 m->act_count -= min(m->act_count, ACT_DECLINE); 1217 if (vm_pageout_algorithm || 1218 object->ref_count == 0 || 1219 m->act_count == 0) { 1220 page_shortage--; 1221 if (object->ref_count == 0) { 1222 KASSERT(!pmap_page_is_mapped(m), 1223 ("vm_pageout_scan: page %p is mapped", m)); 1224 if (m->dirty == 0) 1225 vm_page_cache(m); 1226 else 1227 vm_page_deactivate(m); 1228 } else { 1229 vm_page_deactivate(m); 1230 } 1231 } else { 1232 vm_page_requeue(m); 1233 } 1234 } 1235 vm_page_unlock(m); 1236 VM_OBJECT_UNLOCK(object); 1237 m = next; 1238 } 1239 vm_page_unlock_queues(); 1240#if !defined(NO_SWAPPING) 1241 /* 1242 * Idle process swapout -- run once per second. 1243 */ 1244 if (vm_swap_idle_enabled) { 1245 static long lsec; 1246 if (time_second != lsec) { 1247 vm_req_vmdaemon(VM_SWAP_IDLE); 1248 lsec = time_second; 1249 } 1250 } 1251#endif 1252 1253 /* 1254 * If we didn't get enough free pages, and we have skipped a vnode 1255 * in a writeable object, wakeup the sync daemon. And kick swapout 1256 * if we did not get enough free pages. 1257 */ 1258 if (vm_paging_target() > 0) { 1259 if (vnodes_skipped && vm_page_count_min()) 1260 (void) speedup_syncer(); 1261#if !defined(NO_SWAPPING) 1262 if (vm_swap_enabled && vm_page_count_target()) 1263 vm_req_vmdaemon(VM_SWAP_NORMAL); 1264#endif 1265 } 1266 1267 /* 1268 * If we are critically low on one of RAM or swap and low on 1269 * the other, kill the largest process. However, we avoid 1270 * doing this on the first pass in order to give ourselves a 1271 * chance to flush out dirty vnode-backed pages and to allow 1272 * active pages to be moved to the inactive queue and reclaimed. 1273 */ 1274 if (pass != 0 && 1275 ((swap_pager_avail < 64 && vm_page_count_min()) || 1276 (swap_pager_full && vm_paging_target() > 0))) 1277 vm_pageout_oom(VM_OOM_MEM); 1278} 1279 1280 1281void 1282vm_pageout_oom(int shortage) 1283{ 1284 struct proc *p, *bigproc; 1285 vm_offset_t size, bigsize; 1286 struct thread *td; 1287 struct vmspace *vm; 1288 1289 /* 1290 * We keep the process bigproc locked once we find it to keep anyone 1291 * from messing with it; however, there is a possibility of 1292 * deadlock if process B is bigproc and one of it's child processes 1293 * attempts to propagate a signal to B while we are waiting for A's 1294 * lock while walking this list. To avoid this, we don't block on 1295 * the process lock but just skip a process if it is already locked. 1296 */ 1297 bigproc = NULL; 1298 bigsize = 0; 1299 sx_slock(&allproc_lock); 1300 FOREACH_PROC_IN_SYSTEM(p) { 1301 int breakout; 1302 1303 if (PROC_TRYLOCK(p) == 0) 1304 continue; 1305 /* 1306 * If this is a system, protected or killed process, skip it. 1307 */ 1308 if (p->p_state != PRS_NORMAL || 1309 (p->p_flag & (P_INEXEC | P_PROTECTED | P_SYSTEM)) || 1310 (p->p_pid == 1) || P_KILLED(p) || 1311 ((p->p_pid < 48) && (swap_pager_avail != 0))) { 1312 PROC_UNLOCK(p); 1313 continue; 1314 } 1315 /* 1316 * If the process is in a non-running type state, 1317 * don't touch it. Check all the threads individually. 1318 */ 1319 breakout = 0; 1320 FOREACH_THREAD_IN_PROC(p, td) { 1321 thread_lock(td); 1322 if (!TD_ON_RUNQ(td) && 1323 !TD_IS_RUNNING(td) && 1324 !TD_IS_SLEEPING(td) && 1325 !TD_IS_SUSPENDED(td)) { 1326 thread_unlock(td); 1327 breakout = 1; 1328 break; 1329 } 1330 thread_unlock(td); 1331 } 1332 if (breakout) { 1333 PROC_UNLOCK(p); 1334 continue; 1335 } 1336 /* 1337 * get the process size 1338 */ 1339 vm = vmspace_acquire_ref(p); 1340 if (vm == NULL) { 1341 PROC_UNLOCK(p); 1342 continue; 1343 } 1344 if (!vm_map_trylock_read(&vm->vm_map)) { 1345 vmspace_free(vm); 1346 PROC_UNLOCK(p); 1347 continue; 1348 } 1349 size = vmspace_swap_count(vm); 1350 vm_map_unlock_read(&vm->vm_map); 1351 if (shortage == VM_OOM_MEM) 1352 size += vmspace_resident_count(vm); 1353 vmspace_free(vm); 1354 /* 1355 * if the this process is bigger than the biggest one 1356 * remember it. 1357 */ 1358 if (size > bigsize) { 1359 if (bigproc != NULL) 1360 PROC_UNLOCK(bigproc); 1361 bigproc = p; 1362 bigsize = size; 1363 } else 1364 PROC_UNLOCK(p); 1365 } 1366 sx_sunlock(&allproc_lock); 1367 if (bigproc != NULL) { 1368 killproc(bigproc, "out of swap space"); 1369 sched_nice(bigproc, PRIO_MIN); 1370 PROC_UNLOCK(bigproc); 1371 wakeup(&cnt.v_free_count); 1372 } 1373} 1374 1375/* 1376 * This routine tries to maintain the pseudo LRU active queue, 1377 * so that during long periods of time where there is no paging, 1378 * that some statistic accumulation still occurs. This code 1379 * helps the situation where paging just starts to occur. 1380 */ 1381static void 1382vm_pageout_page_stats() 1383{ 1384 vm_object_t object; 1385 vm_page_t m,next; 1386 int pcount,tpcount; /* Number of pages to check */ 1387 static int fullintervalcount = 0; 1388 int page_shortage; 1389 1390 page_shortage = 1391 (cnt.v_inactive_target + cnt.v_cache_max + cnt.v_free_min) - 1392 (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count); 1393 1394 if (page_shortage <= 0) 1395 return; 1396 1397 vm_page_lock_queues(); 1398 pcount = cnt.v_active_count; 1399 fullintervalcount += vm_pageout_stats_interval; 1400 if (fullintervalcount < vm_pageout_full_stats_interval) { 1401 tpcount = (int64_t)vm_pageout_stats_max * cnt.v_active_count / 1402 cnt.v_page_count; 1403 if (pcount > tpcount) 1404 pcount = tpcount; 1405 } else { 1406 fullintervalcount = 0; 1407 } 1408 1409 m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl); 1410 while ((m != NULL) && (pcount-- > 0)) { 1411 int actcount; 1412 1413 KASSERT(m->queue == PQ_ACTIVE, 1414 ("vm_pageout_page_stats: page %p isn't active", m)); 1415 1416 next = TAILQ_NEXT(m, pageq); 1417 if ((m->flags & PG_MARKER) != 0) { 1418 m = next; 1419 continue; 1420 } 1421 vm_page_lock_assert(m, MA_NOTOWNED); 1422 if (!vm_pageout_page_lock(m, &next)) { 1423 vm_page_unlock(m); 1424 m = next; 1425 continue; 1426 } 1427 object = m->object; 1428 if (!VM_OBJECT_TRYLOCK(object) && 1429 !vm_pageout_fallback_object_lock(m, &next)) { 1430 VM_OBJECT_UNLOCK(object); 1431 vm_page_unlock(m); 1432 m = next; 1433 continue; 1434 } 1435 1436 /* 1437 * Don't deactivate pages that are busy. 1438 */ 1439 if ((m->busy != 0) || 1440 (m->oflags & VPO_BUSY) || 1441 (m->hold_count != 0)) { 1442 vm_page_unlock(m); 1443 VM_OBJECT_UNLOCK(object); 1444 vm_page_requeue(m); 1445 m = next; 1446 continue; 1447 } 1448 1449 actcount = 0; 1450 if (m->aflags & PGA_REFERENCED) { 1451 vm_page_aflag_clear(m, PGA_REFERENCED); 1452 actcount += 1; 1453 } 1454 1455 actcount += pmap_ts_referenced(m); 1456 if (actcount) { 1457 m->act_count += ACT_ADVANCE + actcount; 1458 if (m->act_count > ACT_MAX) 1459 m->act_count = ACT_MAX; 1460 vm_page_requeue(m); 1461 } else { 1462 if (m->act_count == 0) { 1463 /* 1464 * We turn off page access, so that we have 1465 * more accurate RSS stats. We don't do this 1466 * in the normal page deactivation when the 1467 * system is loaded VM wise, because the 1468 * cost of the large number of page protect 1469 * operations would be higher than the value 1470 * of doing the operation. 1471 */ 1472 pmap_remove_all(m); 1473 vm_page_deactivate(m); 1474 } else { 1475 m->act_count -= min(m->act_count, ACT_DECLINE); 1476 vm_page_requeue(m); 1477 } 1478 } 1479 vm_page_unlock(m); 1480 VM_OBJECT_UNLOCK(object); 1481 m = next; 1482 } 1483 vm_page_unlock_queues(); 1484} 1485 1486/* 1487 * vm_pageout is the high level pageout daemon. 1488 */ 1489static void 1490vm_pageout() 1491{ 1492 int error, pass; 1493 1494 /* 1495 * Initialize some paging parameters. 1496 */ 1497 cnt.v_interrupt_free_min = 2; 1498 if (cnt.v_page_count < 2000) 1499 vm_pageout_page_count = 8; 1500 1501 /* 1502 * v_free_reserved needs to include enough for the largest 1503 * swap pager structures plus enough for any pv_entry structs 1504 * when paging. 1505 */ 1506 if (cnt.v_page_count > 1024) 1507 cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200; 1508 else 1509 cnt.v_free_min = 4; 1510 cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE + 1511 cnt.v_interrupt_free_min; 1512 cnt.v_free_reserved = vm_pageout_page_count + 1513 cnt.v_pageout_free_min + (cnt.v_page_count / 768); 1514 cnt.v_free_severe = cnt.v_free_min / 2; 1515 cnt.v_free_min += cnt.v_free_reserved; 1516 cnt.v_free_severe += cnt.v_free_reserved; 1517 1518 /* 1519 * v_free_target and v_cache_min control pageout hysteresis. Note 1520 * that these are more a measure of the VM cache queue hysteresis 1521 * then the VM free queue. Specifically, v_free_target is the 1522 * high water mark (free+cache pages). 1523 * 1524 * v_free_reserved + v_cache_min (mostly means v_cache_min) is the 1525 * low water mark, while v_free_min is the stop. v_cache_min must 1526 * be big enough to handle memory needs while the pageout daemon 1527 * is signalled and run to free more pages. 1528 */ 1529 if (cnt.v_free_count > 6144) 1530 cnt.v_free_target = 4 * cnt.v_free_min + cnt.v_free_reserved; 1531 else 1532 cnt.v_free_target = 2 * cnt.v_free_min + cnt.v_free_reserved; 1533 1534 if (cnt.v_free_count > 2048) { 1535 cnt.v_cache_min = cnt.v_free_target; 1536 cnt.v_cache_max = 2 * cnt.v_cache_min; 1537 cnt.v_inactive_target = (3 * cnt.v_free_target) / 2; 1538 } else { 1539 cnt.v_cache_min = 0; 1540 cnt.v_cache_max = 0; 1541 cnt.v_inactive_target = cnt.v_free_count / 4; 1542 } 1543 if (cnt.v_inactive_target > cnt.v_free_count / 3) 1544 cnt.v_inactive_target = cnt.v_free_count / 3; 1545 1546 /* XXX does not really belong here */ 1547 if (vm_page_max_wired == 0) 1548 vm_page_max_wired = cnt.v_free_count / 3; 1549 1550 if (vm_pageout_stats_max == 0) 1551 vm_pageout_stats_max = cnt.v_free_target; 1552 1553 /* 1554 * Set interval in seconds for stats scan. 1555 */ 1556 if (vm_pageout_stats_interval == 0) 1557 vm_pageout_stats_interval = 5; 1558 if (vm_pageout_full_stats_interval == 0) 1559 vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4; 1560 1561 swap_pager_swap_init(); 1562 pass = 0; 1563 /* 1564 * The pageout daemon is never done, so loop forever. 1565 */ 1566 while (TRUE) { 1567 /* 1568 * If we have enough free memory, wakeup waiters. Do 1569 * not clear vm_pages_needed until we reach our target, 1570 * otherwise we may be woken up over and over again and 1571 * waste a lot of cpu. 1572 */ 1573 mtx_lock(&vm_page_queue_free_mtx); 1574 if (vm_pages_needed && !vm_page_count_min()) { 1575 if (!vm_paging_needed()) 1576 vm_pages_needed = 0; 1577 wakeup(&cnt.v_free_count); 1578 } 1579 if (vm_pages_needed) { 1580 /* 1581 * Still not done, take a second pass without waiting 1582 * (unlimited dirty cleaning), otherwise sleep a bit 1583 * and try again. 1584 */ 1585 ++pass; 1586 if (pass > 1) 1587 msleep(&vm_pages_needed, 1588 &vm_page_queue_free_mtx, PVM, "psleep", 1589 hz / 2); 1590 } else { 1591 /* 1592 * Good enough, sleep & handle stats. Prime the pass 1593 * for the next run. 1594 */ 1595 if (pass > 1) 1596 pass = 1; 1597 else 1598 pass = 0; 1599 error = msleep(&vm_pages_needed, 1600 &vm_page_queue_free_mtx, PVM, "psleep", 1601 vm_pageout_stats_interval * hz); 1602 if (error && !vm_pages_needed) { 1603 mtx_unlock(&vm_page_queue_free_mtx); 1604 pass = 0; 1605 vm_pageout_page_stats(); 1606 continue; 1607 } 1608 } 1609 if (vm_pages_needed) 1610 cnt.v_pdwakeups++; 1611 mtx_unlock(&vm_page_queue_free_mtx); 1612 vm_pageout_scan(pass); 1613 } 1614} 1615 1616/* 1617 * Unless the free page queue lock is held by the caller, this function 1618 * should be regarded as advisory. Specifically, the caller should 1619 * not msleep() on &cnt.v_free_count following this function unless 1620 * the free page queue lock is held until the msleep() is performed. 1621 */ 1622void 1623pagedaemon_wakeup() 1624{ 1625 1626 if (!vm_pages_needed && curthread->td_proc != pageproc) { 1627 vm_pages_needed = 1; 1628 wakeup(&vm_pages_needed); 1629 } 1630} 1631 1632#if !defined(NO_SWAPPING) 1633static void 1634vm_req_vmdaemon(int req) 1635{ 1636 static int lastrun = 0; 1637 1638 mtx_lock(&vm_daemon_mtx); 1639 vm_pageout_req_swapout |= req; 1640 if ((ticks > (lastrun + hz)) || (ticks < lastrun)) { 1641 wakeup(&vm_daemon_needed); 1642 lastrun = ticks; 1643 } 1644 mtx_unlock(&vm_daemon_mtx); 1645} 1646 1647static void 1648vm_daemon() 1649{ 1650 struct rlimit rsslim; 1651 struct proc *p; 1652 struct thread *td; 1653 struct vmspace *vm; 1654 int breakout, swapout_flags, tryagain, attempts; 1655#ifdef RACCT 1656 uint64_t rsize, ravailable; 1657#endif 1658 1659 while (TRUE) { 1660 mtx_lock(&vm_daemon_mtx); 1661#ifdef RACCT 1662 msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", hz); 1663#else 1664 msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", 0); 1665#endif 1666 swapout_flags = vm_pageout_req_swapout; 1667 vm_pageout_req_swapout = 0; 1668 mtx_unlock(&vm_daemon_mtx); 1669 if (swapout_flags) 1670 swapout_procs(swapout_flags); 1671 1672 /* 1673 * scan the processes for exceeding their rlimits or if 1674 * process is swapped out -- deactivate pages 1675 */ 1676 tryagain = 0; 1677 attempts = 0; 1678again: 1679 attempts++; 1680 sx_slock(&allproc_lock); 1681 FOREACH_PROC_IN_SYSTEM(p) { 1682 vm_pindex_t limit, size; 1683 1684 /* 1685 * if this is a system process or if we have already 1686 * looked at this process, skip it. 1687 */ 1688 PROC_LOCK(p); 1689 if (p->p_state != PRS_NORMAL || 1690 p->p_flag & (P_INEXEC | P_SYSTEM | P_WEXIT)) { 1691 PROC_UNLOCK(p); 1692 continue; 1693 } 1694 /* 1695 * if the process is in a non-running type state, 1696 * don't touch it. 1697 */ 1698 breakout = 0; 1699 FOREACH_THREAD_IN_PROC(p, td) { 1700 thread_lock(td); 1701 if (!TD_ON_RUNQ(td) && 1702 !TD_IS_RUNNING(td) && 1703 !TD_IS_SLEEPING(td) && 1704 !TD_IS_SUSPENDED(td)) { 1705 thread_unlock(td); 1706 breakout = 1; 1707 break; 1708 } 1709 thread_unlock(td); 1710 } 1711 if (breakout) { 1712 PROC_UNLOCK(p); 1713 continue; 1714 } 1715 /* 1716 * get a limit 1717 */ 1718 lim_rlimit(p, RLIMIT_RSS, &rsslim); 1719 limit = OFF_TO_IDX( 1720 qmin(rsslim.rlim_cur, rsslim.rlim_max)); 1721 1722 /* 1723 * let processes that are swapped out really be 1724 * swapped out set the limit to nothing (will force a 1725 * swap-out.) 1726 */ 1727 if ((p->p_flag & P_INMEM) == 0) 1728 limit = 0; /* XXX */ 1729 vm = vmspace_acquire_ref(p); 1730 PROC_UNLOCK(p); 1731 if (vm == NULL) 1732 continue; 1733 1734 size = vmspace_resident_count(vm); 1735 if (limit >= 0 && size >= limit) { 1736 vm_pageout_map_deactivate_pages( 1737 &vm->vm_map, limit); 1738 } 1739#ifdef RACCT 1740 rsize = IDX_TO_OFF(size); 1741 PROC_LOCK(p); 1742 racct_set(p, RACCT_RSS, rsize); 1743 ravailable = racct_get_available(p, RACCT_RSS); 1744 PROC_UNLOCK(p); 1745 if (rsize > ravailable) { 1746 /* 1747 * Don't be overly aggressive; this might be 1748 * an innocent process, and the limit could've 1749 * been exceeded by some memory hog. Don't 1750 * try to deactivate more than 1/4th of process' 1751 * resident set size. 1752 */ 1753 if (attempts <= 8) { 1754 if (ravailable < rsize - (rsize / 4)) 1755 ravailable = rsize - (rsize / 4); 1756 } 1757 vm_pageout_map_deactivate_pages( 1758 &vm->vm_map, OFF_TO_IDX(ravailable)); 1759 /* Update RSS usage after paging out. */ 1760 size = vmspace_resident_count(vm); 1761 rsize = IDX_TO_OFF(size); 1762 PROC_LOCK(p); 1763 racct_set(p, RACCT_RSS, rsize); 1764 PROC_UNLOCK(p); 1765 if (rsize > ravailable) 1766 tryagain = 1; 1767 } 1768#endif 1769 vmspace_free(vm); 1770 } 1771 sx_sunlock(&allproc_lock); 1772 if (tryagain != 0 && attempts <= 10) 1773 goto again; 1774 } 1775} 1776#endif /* !defined(NO_SWAPPING) */ 1777