160727d8bSWarner Losh /*- 226f9a767SRodney W. Grimes * Copyright (c) 1991 Regents of the University of California. 326f9a767SRodney W. Grimes * All rights reserved. 426f9a767SRodney W. Grimes * Copyright (c) 1994 John S. Dyson 526f9a767SRodney W. Grimes * All rights reserved. 626f9a767SRodney W. Grimes * Copyright (c) 1994 David Greenman 726f9a767SRodney W. Grimes * All rights reserved. 88dbca793STor Egge * Copyright (c) 2005 Yahoo! Technologies Norway AS 98dbca793STor Egge * All rights reserved. 10df8bae1dSRodney W. Grimes * 11df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 12df8bae1dSRodney W. Grimes * The Mach Operating System project at Carnegie-Mellon University. 13df8bae1dSRodney W. Grimes * 14df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 15df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 16df8bae1dSRodney W. Grimes * are met: 17df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 18df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 19df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 20df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 21df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 22df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 235929bcfaSPhilippe Charnier * must display the following acknowledgement: 24df8bae1dSRodney W. Grimes * This product includes software developed by the University of 25df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 26df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 27df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 28df8bae1dSRodney W. Grimes * without specific prior written permission. 29df8bae1dSRodney W. Grimes * 30df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 31df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 34df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 35df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 36df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 37df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 38df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 39df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 40df8bae1dSRodney W. Grimes * SUCH DAMAGE. 41df8bae1dSRodney W. Grimes * 423c4dd356SDavid Greenman * from: @(#)vm_pageout.c 7.4 (Berkeley) 5/7/91 43df8bae1dSRodney W. Grimes * 44df8bae1dSRodney W. Grimes * 45df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1990 Carnegie-Mellon University. 46df8bae1dSRodney W. Grimes * All rights reserved. 47df8bae1dSRodney W. Grimes * 48df8bae1dSRodney W. Grimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young 49df8bae1dSRodney W. Grimes * 50df8bae1dSRodney W. Grimes * Permission to use, copy, modify and distribute this software and 51df8bae1dSRodney W. Grimes * its documentation is hereby granted, provided that both the copyright 52df8bae1dSRodney W. Grimes * notice and this permission notice appear in all copies of the 53df8bae1dSRodney W. Grimes * software, derivative works or modified versions, and any portions 54df8bae1dSRodney W. Grimes * thereof, and that both notices appear in supporting documentation. 55df8bae1dSRodney W. Grimes * 56df8bae1dSRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 57df8bae1dSRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 58df8bae1dSRodney W. Grimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 59df8bae1dSRodney W. Grimes * 60df8bae1dSRodney W. Grimes * Carnegie Mellon requests users of this software to return to 61df8bae1dSRodney W. Grimes * 62df8bae1dSRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 63df8bae1dSRodney W. Grimes * School of Computer Science 64df8bae1dSRodney W. Grimes * Carnegie Mellon University 65df8bae1dSRodney W. Grimes * Pittsburgh PA 15213-3890 66df8bae1dSRodney W. Grimes * 67df8bae1dSRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the 68df8bae1dSRodney W. Grimes * rights to redistribute these changes. 69df8bae1dSRodney W. Grimes */ 70df8bae1dSRodney W. Grimes 71df8bae1dSRodney W. Grimes /* 72df8bae1dSRodney W. Grimes * The proverbial page-out daemon. 73df8bae1dSRodney W. Grimes */ 74df8bae1dSRodney W. Grimes 75874651b1SDavid E. O'Brien #include <sys/cdefs.h> 76874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 77874651b1SDavid E. O'Brien 78faa5f8d8SAndrzej Bialecki #include "opt_vm.h" 79df8bae1dSRodney W. Grimes #include <sys/param.h> 8026f9a767SRodney W. Grimes #include <sys/systm.h> 81b5e8ce9fSBruce Evans #include <sys/kernel.h> 82855a310fSJeff Roberson #include <sys/eventhandler.h> 83fb919e4dSMark Murray #include <sys/lock.h> 84fb919e4dSMark Murray #include <sys/mutex.h> 8526f9a767SRodney W. Grimes #include <sys/proc.h> 869c8b8baaSPeter Wemm #include <sys/kthread.h> 870384fff8SJason Evans #include <sys/ktr.h> 8897824da3SAlan Cox #include <sys/mount.h> 89099e7e95SEdward Tomasz Napierala #include <sys/racct.h> 9026f9a767SRodney W. Grimes #include <sys/resourcevar.h> 91b43179fbSJeff Roberson #include <sys/sched.h> 92d2fc5315SPoul-Henning Kamp #include <sys/signalvar.h> 93449c2e92SKonstantin Belousov #include <sys/smp.h> 94f6b04d2bSDavid Greenman #include <sys/vnode.h> 95efeaf95aSDavid Greenman #include <sys/vmmeter.h> 9689f6b863SAttilio Rao #include <sys/rwlock.h> 971005a129SJohn Baldwin #include <sys/sx.h> 9838efa82bSJohn Dyson #include <sys/sysctl.h> 99df8bae1dSRodney W. Grimes 100df8bae1dSRodney W. Grimes #include <vm/vm.h> 101efeaf95aSDavid Greenman #include <vm/vm_param.h> 102efeaf95aSDavid Greenman #include <vm/vm_object.h> 103df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 104efeaf95aSDavid Greenman #include <vm/vm_map.h> 105df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h> 10624a1cce3SDavid Greenman #include <vm/vm_pager.h> 107449c2e92SKonstantin Belousov #include <vm/vm_phys.h> 10805f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h> 109efeaf95aSDavid Greenman #include <vm/vm_extern.h> 110670d17b5SJeff Roberson #include <vm/uma.h> 111df8bae1dSRodney W. Grimes 1122b14f991SJulian Elischer /* 1132b14f991SJulian Elischer * System initialization 1142b14f991SJulian Elischer */ 1152b14f991SJulian Elischer 1162b14f991SJulian Elischer /* the kernel process "vm_pageout"*/ 11711caded3SAlfred Perlstein static void vm_pageout(void); 11811caded3SAlfred Perlstein static int vm_pageout_clean(vm_page_t); 119449c2e92SKonstantin Belousov static void vm_pageout_scan(struct vm_domain *vmd, int pass); 120449c2e92SKonstantin Belousov static void vm_pageout_mightbe_oom(struct vm_domain *vmd, int pass); 12145ae1d91SAlan Cox 1222b14f991SJulian Elischer struct proc *pageproc; 1232b14f991SJulian Elischer 1242b14f991SJulian Elischer static struct kproc_desc page_kp = { 1252b14f991SJulian Elischer "pagedaemon", 1262b14f991SJulian Elischer vm_pageout, 1272b14f991SJulian Elischer &pageproc 1282b14f991SJulian Elischer }; 129237fdd78SRobert Watson SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, 130237fdd78SRobert Watson &page_kp); 1312b14f991SJulian Elischer 13238efa82bSJohn Dyson #if !defined(NO_SWAPPING) 1332b14f991SJulian Elischer /* the kernel process "vm_daemon"*/ 13411caded3SAlfred Perlstein static void vm_daemon(void); 135f708ef1bSPoul-Henning Kamp static struct proc *vmproc; 1362b14f991SJulian Elischer 1372b14f991SJulian Elischer static struct kproc_desc vm_kp = { 1382b14f991SJulian Elischer "vmdaemon", 1392b14f991SJulian Elischer vm_daemon, 1402b14f991SJulian Elischer &vmproc 1412b14f991SJulian Elischer }; 142237fdd78SRobert Watson SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp); 14338efa82bSJohn Dyson #endif 1442b14f991SJulian Elischer 1452b14f991SJulian Elischer 1468b245767SAlan Cox int vm_pages_needed; /* Event on which pageout daemon sleeps */ 1478b245767SAlan Cox int vm_pageout_deficit; /* Estimated number of pages deficit */ 1488b245767SAlan Cox int vm_pageout_pages_needed; /* flag saying that the pageout daemon needs pages */ 149d9e23210SJeff Roberson int vm_pageout_wakeup_thresh; 15026f9a767SRodney W. Grimes 15138efa82bSJohn Dyson #if !defined(NO_SWAPPING) 152f708ef1bSPoul-Henning Kamp static int vm_pageout_req_swapout; /* XXX */ 153f708ef1bSPoul-Henning Kamp static int vm_daemon_needed; 15497824da3SAlan Cox static struct mtx vm_daemon_mtx; 15597824da3SAlan Cox /* Allow for use by vm_pageout before vm_daemon is initialized. */ 15697824da3SAlan Cox MTX_SYSINIT(vm_daemon, &vm_daemon_mtx, "vm daemon", MTX_DEF); 15738efa82bSJohn Dyson #endif 1582b6b0df7SMatthew Dillon static int vm_max_launder = 32; 159d9e23210SJeff Roberson static int vm_pageout_update_period; 1604a365329SAndrey Zonov static int defer_swap_pageouts; 1614a365329SAndrey Zonov static int disable_swap_pageouts; 162*c9612b2dSJeff Roberson static int lowmem_period = 10; 163*c9612b2dSJeff Roberson static int lowmem_ticks; 16470111b90SJohn Dyson 16538efa82bSJohn Dyson #if defined(NO_SWAPPING) 166303b270bSEivind Eklund static int vm_swap_enabled = 0; 167303b270bSEivind Eklund static int vm_swap_idle_enabled = 0; 16838efa82bSJohn Dyson #else 169303b270bSEivind Eklund static int vm_swap_enabled = 1; 170303b270bSEivind Eklund static int vm_swap_idle_enabled = 0; 17138efa82bSJohn Dyson #endif 17238efa82bSJohn Dyson 173d9e23210SJeff Roberson SYSCTL_INT(_vm, OID_AUTO, pageout_wakeup_thresh, 174d9e23210SJeff Roberson CTLFLAG_RW, &vm_pageout_wakeup_thresh, 0, 175d9e23210SJeff Roberson "free page threshold for waking up the pageout daemon"); 176d9e23210SJeff Roberson 1772b6b0df7SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, max_launder, 1782b6b0df7SMatthew Dillon CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout"); 17938efa82bSJohn Dyson 180d9e23210SJeff Roberson SYSCTL_INT(_vm, OID_AUTO, pageout_update_period, 181d9e23210SJeff Roberson CTLFLAG_RW, &vm_pageout_update_period, 0, 182d9e23210SJeff Roberson "Maximum active LRU update period"); 18353636869SAndrey Zonov 184*c9612b2dSJeff Roberson SYSCTL_INT(_vm, OID_AUTO, lowmem_period, CTLFLAG_RW, &lowmem_period, 0, 185*c9612b2dSJeff Roberson "Low memory callback period"); 186*c9612b2dSJeff Roberson 18738efa82bSJohn Dyson #if defined(NO_SWAPPING) 188ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 1896bd9cb1cSTom Rhodes CTLFLAG_RD, &vm_swap_enabled, 0, "Enable entire process swapout"); 190ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 1916bd9cb1cSTom Rhodes CTLFLAG_RD, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria"); 19238efa82bSJohn Dyson #else 193ceb0cf87SJohn Dyson SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 194b0359e2cSPeter Wemm CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout"); 195ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 196b0359e2cSPeter Wemm CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria"); 19738efa82bSJohn Dyson #endif 19826f9a767SRodney W. Grimes 199ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts, 200b0359e2cSPeter Wemm CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem"); 20112ac6a1dSJohn Dyson 202ceb0cf87SJohn Dyson SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts, 203b0359e2cSPeter Wemm CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages"); 20412ac6a1dSJohn Dyson 20523b59018SMatthew Dillon static int pageout_lock_miss; 20623b59018SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss, 20723b59018SMatthew Dillon CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout"); 20823b59018SMatthew Dillon 209ffc82b0aSJohn Dyson #define VM_PAGEOUT_PAGE_COUNT 16 210bbc0ec52SDavid Greenman int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT; 211df8bae1dSRodney W. Grimes 212c3cb3e12SDavid Greenman int vm_page_max_wired; /* XXX max # of wired pages system-wide */ 2135dfc2870SAlan Cox SYSCTL_INT(_vm, OID_AUTO, max_wired, 2145dfc2870SAlan Cox CTLFLAG_RW, &vm_page_max_wired, 0, "System-wide limit to wired page count"); 215df8bae1dSRodney W. Grimes 21685eeca35SAlan Cox static boolean_t vm_pageout_fallback_object_lock(vm_page_t, vm_page_t *); 217449c2e92SKonstantin Belousov static boolean_t vm_pageout_launder(struct vm_pagequeue *pq, int, vm_paddr_t, 218449c2e92SKonstantin Belousov vm_paddr_t); 21938efa82bSJohn Dyson #if !defined(NO_SWAPPING) 220ecf6279fSAlan Cox static void vm_pageout_map_deactivate_pages(vm_map_t, long); 221ecf6279fSAlan Cox static void vm_pageout_object_deactivate_pages(pmap_t, vm_object_t, long); 22297824da3SAlan Cox static void vm_req_vmdaemon(int req); 22338efa82bSJohn Dyson #endif 22485eeca35SAlan Cox static boolean_t vm_pageout_page_lock(vm_page_t, vm_page_t *); 225cd41fc12SDavid Greenman 226a8229fa3SAlan Cox /* 227a8229fa3SAlan Cox * Initialize a dummy page for marking the caller's place in the specified 228a8229fa3SAlan Cox * paging queue. In principle, this function only needs to set the flag 229c7aebda8SAttilio Rao * PG_MARKER. Nonetheless, it wirte busies and initializes the hold count 230c7aebda8SAttilio Rao * to one as safety precautions. 231a8229fa3SAlan Cox */ 2328c616246SKonstantin Belousov static void 2338c616246SKonstantin Belousov vm_pageout_init_marker(vm_page_t marker, u_short queue) 2348c616246SKonstantin Belousov { 2358c616246SKonstantin Belousov 2368c616246SKonstantin Belousov bzero(marker, sizeof(*marker)); 237a8229fa3SAlan Cox marker->flags = PG_MARKER; 238c7aebda8SAttilio Rao marker->busy_lock = VPB_SINGLE_EXCLUSIVER; 2398c616246SKonstantin Belousov marker->queue = queue; 240a8229fa3SAlan Cox marker->hold_count = 1; 2418c616246SKonstantin Belousov } 2428c616246SKonstantin Belousov 24326f9a767SRodney W. Grimes /* 2448dbca793STor Egge * vm_pageout_fallback_object_lock: 2458dbca793STor Egge * 24689f6b863SAttilio Rao * Lock vm object currently associated with `m'. VM_OBJECT_TRYWLOCK is 2478dbca793STor Egge * known to have failed and page queue must be either PQ_ACTIVE or 2488dbca793STor Egge * PQ_INACTIVE. To avoid lock order violation, unlock the page queues 2498dbca793STor Egge * while locking the vm object. Use marker page to detect page queue 2508dbca793STor Egge * changes and maintain notion of next page on page queue. Return 2518dbca793STor Egge * TRUE if no changes were detected, FALSE otherwise. vm object is 2528dbca793STor Egge * locked on return. 2538dbca793STor Egge * 2548dbca793STor Egge * This function depends on both the lock portion of struct vm_object 2558dbca793STor Egge * and normal struct vm_page being type stable. 2568dbca793STor Egge */ 25785eeca35SAlan Cox static boolean_t 2588dbca793STor Egge vm_pageout_fallback_object_lock(vm_page_t m, vm_page_t *next) 2598dbca793STor Egge { 2608dbca793STor Egge struct vm_page marker; 2618d220203SAlan Cox struct vm_pagequeue *pq; 2628dbca793STor Egge boolean_t unchanged; 2638dbca793STor Egge u_short queue; 2648dbca793STor Egge vm_object_t object; 2658dbca793STor Egge 2668dbca793STor Egge queue = m->queue; 2678c616246SKonstantin Belousov vm_pageout_init_marker(&marker, queue); 268449c2e92SKonstantin Belousov pq = vm_page_pagequeue(m); 2698dbca793STor Egge object = m->object; 2708dbca793STor Egge 271c325e866SKonstantin Belousov TAILQ_INSERT_AFTER(&pq->pq_pl, m, &marker, plinks.q); 2728d220203SAlan Cox vm_pagequeue_unlock(pq); 2732965a453SKip Macy vm_page_unlock(m); 27489f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 2752965a453SKip Macy vm_page_lock(m); 2768d220203SAlan Cox vm_pagequeue_lock(pq); 2778dbca793STor Egge 2788dbca793STor Egge /* Page queue might have changed. */ 279c325e866SKonstantin Belousov *next = TAILQ_NEXT(&marker, plinks.q); 2808dbca793STor Egge unchanged = (m->queue == queue && 2818dbca793STor Egge m->object == object && 282c325e866SKonstantin Belousov &marker == TAILQ_NEXT(m, plinks.q)); 283c325e866SKonstantin Belousov TAILQ_REMOVE(&pq->pq_pl, &marker, plinks.q); 2848dbca793STor Egge return (unchanged); 2858dbca793STor Egge } 2868dbca793STor Egge 2878dbca793STor Egge /* 2888c616246SKonstantin Belousov * Lock the page while holding the page queue lock. Use marker page 2898c616246SKonstantin Belousov * to detect page queue changes and maintain notion of next page on 2908c616246SKonstantin Belousov * page queue. Return TRUE if no changes were detected, FALSE 2918c616246SKonstantin Belousov * otherwise. The page is locked on return. The page queue lock might 2928c616246SKonstantin Belousov * be dropped and reacquired. 2938c616246SKonstantin Belousov * 2948c616246SKonstantin Belousov * This function depends on normal struct vm_page being type stable. 2958c616246SKonstantin Belousov */ 29685eeca35SAlan Cox static boolean_t 2978c616246SKonstantin Belousov vm_pageout_page_lock(vm_page_t m, vm_page_t *next) 2988c616246SKonstantin Belousov { 2998c616246SKonstantin Belousov struct vm_page marker; 3008d220203SAlan Cox struct vm_pagequeue *pq; 3018c616246SKonstantin Belousov boolean_t unchanged; 3028c616246SKonstantin Belousov u_short queue; 3038c616246SKonstantin Belousov 3048c616246SKonstantin Belousov vm_page_lock_assert(m, MA_NOTOWNED); 3058c616246SKonstantin Belousov if (vm_page_trylock(m)) 3068c616246SKonstantin Belousov return (TRUE); 3078c616246SKonstantin Belousov 3088c616246SKonstantin Belousov queue = m->queue; 3098c616246SKonstantin Belousov vm_pageout_init_marker(&marker, queue); 310449c2e92SKonstantin Belousov pq = vm_page_pagequeue(m); 3118c616246SKonstantin Belousov 312c325e866SKonstantin Belousov TAILQ_INSERT_AFTER(&pq->pq_pl, m, &marker, plinks.q); 3138d220203SAlan Cox vm_pagequeue_unlock(pq); 3148c616246SKonstantin Belousov vm_page_lock(m); 3158d220203SAlan Cox vm_pagequeue_lock(pq); 3168c616246SKonstantin Belousov 3178c616246SKonstantin Belousov /* Page queue might have changed. */ 318c325e866SKonstantin Belousov *next = TAILQ_NEXT(&marker, plinks.q); 319c325e866SKonstantin Belousov unchanged = (m->queue == queue && &marker == TAILQ_NEXT(m, plinks.q)); 320c325e866SKonstantin Belousov TAILQ_REMOVE(&pq->pq_pl, &marker, plinks.q); 3218c616246SKonstantin Belousov return (unchanged); 3228c616246SKonstantin Belousov } 3238c616246SKonstantin Belousov 3248c616246SKonstantin Belousov /* 32526f9a767SRodney W. Grimes * vm_pageout_clean: 32624a1cce3SDavid Greenman * 3270d94caffSDavid Greenman * Clean the page and remove it from the laundry. 32826f9a767SRodney W. Grimes * 3290d94caffSDavid Greenman * We set the busy bit to cause potential page faults on this page to 3301c7c3c6aSMatthew Dillon * block. Note the careful timing, however, the busy bit isn't set till 3311c7c3c6aSMatthew Dillon * late and we cannot do anything that will mess with the page. 33226f9a767SRodney W. Grimes */ 3333af76890SPoul-Henning Kamp static int 3342965a453SKip Macy vm_pageout_clean(vm_page_t m) 33524a1cce3SDavid Greenman { 33654d92145SMatthew Dillon vm_object_t object; 33791b4f427SAlan Cox vm_page_t mc[2*vm_pageout_page_count], pb, ps; 3383562af12SAlan Cox int pageout_count; 33990ecac61SMatthew Dillon int ib, is, page_base; 340a316d390SJohn Dyson vm_pindex_t pindex = m->pindex; 34126f9a767SRodney W. Grimes 34295976f3fSAlan Cox vm_page_lock_assert(m, MA_OWNED); 34317f6a17bSAlan Cox object = m->object; 34489f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 3450cddd8f0SMatthew Dillon 34626f9a767SRodney W. Grimes /* 3471c7c3c6aSMatthew Dillon * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP 3481c7c3c6aSMatthew Dillon * with the new swapper, but we could have serious problems paging 3491c7c3c6aSMatthew Dillon * out other object types if there is insufficient memory. 3501c7c3c6aSMatthew Dillon * 3511c7c3c6aSMatthew Dillon * Unfortunately, checking free memory here is far too late, so the 3521c7c3c6aSMatthew Dillon * check has been moved up a procedural level. 3531c7c3c6aSMatthew Dillon */ 3541c7c3c6aSMatthew Dillon 35524a1cce3SDavid Greenman /* 3569e897b1bSAlan Cox * Can't clean the page if it's busy or held. 35724a1cce3SDavid Greenman */ 358c7aebda8SAttilio Rao vm_page_assert_unbusied(m); 35995976f3fSAlan Cox KASSERT(m->hold_count == 0, ("vm_pageout_clean: page %p is held", m)); 36017f6a17bSAlan Cox vm_page_unlock(m); 3610d94caffSDavid Greenman 36291b4f427SAlan Cox mc[vm_pageout_page_count] = pb = ps = m; 36326f9a767SRodney W. Grimes pageout_count = 1; 364f35329acSJohn Dyson page_base = vm_pageout_page_count; 36590ecac61SMatthew Dillon ib = 1; 36690ecac61SMatthew Dillon is = 1; 36790ecac61SMatthew Dillon 36824a1cce3SDavid Greenman /* 36924a1cce3SDavid Greenman * Scan object for clusterable pages. 37024a1cce3SDavid Greenman * 37124a1cce3SDavid Greenman * We can cluster ONLY if: ->> the page is NOT 37224a1cce3SDavid Greenman * clean, wired, busy, held, or mapped into a 37324a1cce3SDavid Greenman * buffer, and one of the following: 37424a1cce3SDavid Greenman * 1) The page is inactive, or a seldom used 37524a1cce3SDavid Greenman * active page. 37624a1cce3SDavid Greenman * -or- 37724a1cce3SDavid Greenman * 2) we force the issue. 37890ecac61SMatthew Dillon * 37990ecac61SMatthew Dillon * During heavy mmap/modification loads the pageout 38090ecac61SMatthew Dillon * daemon can really fragment the underlying file 38190ecac61SMatthew Dillon * due to flushing pages out of order and not trying 38290ecac61SMatthew Dillon * align the clusters (which leave sporatic out-of-order 38390ecac61SMatthew Dillon * holes). To solve this problem we do the reverse scan 38490ecac61SMatthew Dillon * first and attempt to align our cluster, then do a 38590ecac61SMatthew Dillon * forward scan if room remains. 38624a1cce3SDavid Greenman */ 38790ecac61SMatthew Dillon more: 38890ecac61SMatthew Dillon while (ib && pageout_count < vm_pageout_page_count) { 38924a1cce3SDavid Greenman vm_page_t p; 390f6b04d2bSDavid Greenman 39190ecac61SMatthew Dillon if (ib > pindex) { 39290ecac61SMatthew Dillon ib = 0; 39390ecac61SMatthew Dillon break; 394f6b04d2bSDavid Greenman } 39590ecac61SMatthew Dillon 396c7aebda8SAttilio Rao if ((p = vm_page_prev(pb)) == NULL || vm_page_busied(p)) { 39790ecac61SMatthew Dillon ib = 0; 39890ecac61SMatthew Dillon break; 399f6b04d2bSDavid Greenman } 4002965a453SKip Macy vm_page_lock(p); 40124a1cce3SDavid Greenman vm_page_test_dirty(p); 40226f4eea5SAlan Cox if (p->dirty == 0 || 40390ecac61SMatthew Dillon p->queue != PQ_INACTIVE || 40457601bcbSMatthew Dillon p->hold_count != 0) { /* may be undergoing I/O */ 4052965a453SKip Macy vm_page_unlock(p); 40690ecac61SMatthew Dillon ib = 0; 40724a1cce3SDavid Greenman break; 408f6b04d2bSDavid Greenman } 4092965a453SKip Macy vm_page_unlock(p); 41091b4f427SAlan Cox mc[--page_base] = pb = p; 41190ecac61SMatthew Dillon ++pageout_count; 41290ecac61SMatthew Dillon ++ib; 41324a1cce3SDavid Greenman /* 41490ecac61SMatthew Dillon * alignment boundry, stop here and switch directions. Do 41590ecac61SMatthew Dillon * not clear ib. 41624a1cce3SDavid Greenman */ 41790ecac61SMatthew Dillon if ((pindex - (ib - 1)) % vm_pageout_page_count == 0) 41890ecac61SMatthew Dillon break; 41924a1cce3SDavid Greenman } 42090ecac61SMatthew Dillon 42190ecac61SMatthew Dillon while (pageout_count < vm_pageout_page_count && 42290ecac61SMatthew Dillon pindex + is < object->size) { 42390ecac61SMatthew Dillon vm_page_t p; 42490ecac61SMatthew Dillon 425c7aebda8SAttilio Rao if ((p = vm_page_next(ps)) == NULL || vm_page_busied(p)) 42690ecac61SMatthew Dillon break; 4272965a453SKip Macy vm_page_lock(p); 42824a1cce3SDavid Greenman vm_page_test_dirty(p); 42926f4eea5SAlan Cox if (p->dirty == 0 || 43090ecac61SMatthew Dillon p->queue != PQ_INACTIVE || 43157601bcbSMatthew Dillon p->hold_count != 0) { /* may be undergoing I/O */ 4322965a453SKip Macy vm_page_unlock(p); 43324a1cce3SDavid Greenman break; 43424a1cce3SDavid Greenman } 4352965a453SKip Macy vm_page_unlock(p); 43691b4f427SAlan Cox mc[page_base + pageout_count] = ps = p; 43790ecac61SMatthew Dillon ++pageout_count; 43890ecac61SMatthew Dillon ++is; 43924a1cce3SDavid Greenman } 44090ecac61SMatthew Dillon 44190ecac61SMatthew Dillon /* 44290ecac61SMatthew Dillon * If we exhausted our forward scan, continue with the reverse scan 44390ecac61SMatthew Dillon * when possible, even past a page boundry. This catches boundry 44490ecac61SMatthew Dillon * conditions. 44590ecac61SMatthew Dillon */ 44690ecac61SMatthew Dillon if (ib && pageout_count < vm_pageout_page_count) 44790ecac61SMatthew Dillon goto more; 448f6b04d2bSDavid Greenman 44967bf6868SJohn Dyson /* 45067bf6868SJohn Dyson * we allow reads during pageouts... 45167bf6868SJohn Dyson */ 452126d6082SKonstantin Belousov return (vm_pageout_flush(&mc[page_base], pageout_count, 0, 0, NULL, 453126d6082SKonstantin Belousov NULL)); 454aef922f5SJohn Dyson } 455aef922f5SJohn Dyson 4561c7c3c6aSMatthew Dillon /* 4571c7c3c6aSMatthew Dillon * vm_pageout_flush() - launder the given pages 4581c7c3c6aSMatthew Dillon * 4591c7c3c6aSMatthew Dillon * The given pages are laundered. Note that we setup for the start of 4601c7c3c6aSMatthew Dillon * I/O ( i.e. busy the page ), mark it read-only, and bump the object 4611c7c3c6aSMatthew Dillon * reference count all in here rather then in the parent. If we want 4621c7c3c6aSMatthew Dillon * the parent to do more sophisticated things we may have to change 4631c7c3c6aSMatthew Dillon * the ordering. 4641e8a675cSKonstantin Belousov * 4651e8a675cSKonstantin Belousov * Returned runlen is the count of pages between mreq and first 4661e8a675cSKonstantin Belousov * page after mreq with status VM_PAGER_AGAIN. 467126d6082SKonstantin Belousov * *eio is set to TRUE if pager returned VM_PAGER_ERROR or VM_PAGER_FAIL 468126d6082SKonstantin Belousov * for any page in runlen set. 4691c7c3c6aSMatthew Dillon */ 470aef922f5SJohn Dyson int 471126d6082SKonstantin Belousov vm_pageout_flush(vm_page_t *mc, int count, int flags, int mreq, int *prunlen, 472126d6082SKonstantin Belousov boolean_t *eio) 473aef922f5SJohn Dyson { 4742e3b314dSAlan Cox vm_object_t object = mc[0]->object; 475aef922f5SJohn Dyson int pageout_status[count]; 47695461b45SJohn Dyson int numpagedout = 0; 4771e8a675cSKonstantin Belousov int i, runlen; 478aef922f5SJohn Dyson 47989f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 4807bec141bSKip Macy 4811c7c3c6aSMatthew Dillon /* 4821c7c3c6aSMatthew Dillon * Initiate I/O. Bump the vm_page_t->busy counter and 4831c7c3c6aSMatthew Dillon * mark the pages read-only. 4841c7c3c6aSMatthew Dillon * 4851c7c3c6aSMatthew Dillon * We do not have to fixup the clean/dirty bits here... we can 4861c7c3c6aSMatthew Dillon * allow the pager to do it after the I/O completes. 48702fa91d3SMatthew Dillon * 48802fa91d3SMatthew Dillon * NOTE! mc[i]->dirty may be partial or fragmented due to an 48902fa91d3SMatthew Dillon * edge case with file fragments. 4901c7c3c6aSMatthew Dillon */ 4918f9110f6SJohn Dyson for (i = 0; i < count; i++) { 4927a935082SAlan Cox KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL, 4937a935082SAlan Cox ("vm_pageout_flush: partially invalid page %p index %d/%d", 4947a935082SAlan Cox mc[i], i, count)); 495c7aebda8SAttilio Rao vm_page_sbusy(mc[i]); 49678985e42SAlan Cox pmap_remove_write(mc[i]); 4972965a453SKip Macy } 498d474eaaaSDoug Rabson vm_object_pip_add(object, count); 499aef922f5SJohn Dyson 500d076fbeaSAlan Cox vm_pager_put_pages(object, mc, count, flags, pageout_status); 50126f9a767SRodney W. Grimes 5021e8a675cSKonstantin Belousov runlen = count - mreq; 503126d6082SKonstantin Belousov if (eio != NULL) 504126d6082SKonstantin Belousov *eio = FALSE; 505aef922f5SJohn Dyson for (i = 0; i < count; i++) { 506aef922f5SJohn Dyson vm_page_t mt = mc[i]; 50724a1cce3SDavid Greenman 5084cd45723SAlan Cox KASSERT(pageout_status[i] == VM_PAGER_PEND || 5096031c68dSAlan Cox !pmap_page_is_write_mapped(mt), 5109ea8d1a6SAlan Cox ("vm_pageout_flush: page %p is not write protected", mt)); 51126f9a767SRodney W. Grimes switch (pageout_status[i]) { 51226f9a767SRodney W. Grimes case VM_PAGER_OK: 51326f9a767SRodney W. Grimes case VM_PAGER_PEND: 51495461b45SJohn Dyson numpagedout++; 51526f9a767SRodney W. Grimes break; 51626f9a767SRodney W. Grimes case VM_PAGER_BAD: 51726f9a767SRodney W. Grimes /* 5180d94caffSDavid Greenman * Page outside of range of object. Right now we 5190d94caffSDavid Greenman * essentially lose the changes by pretending it 5200d94caffSDavid Greenman * worked. 52126f9a767SRodney W. Grimes */ 52290ecac61SMatthew Dillon vm_page_undirty(mt); 52326f9a767SRodney W. Grimes break; 52426f9a767SRodney W. Grimes case VM_PAGER_ERROR: 52526f9a767SRodney W. Grimes case VM_PAGER_FAIL: 52626f9a767SRodney W. Grimes /* 5270d94caffSDavid Greenman * If page couldn't be paged out, then reactivate the 5280d94caffSDavid Greenman * page so it doesn't clog the inactive list. (We 5290d94caffSDavid Greenman * will try paging out it again later). 53026f9a767SRodney W. Grimes */ 5313c4a2440SAlan Cox vm_page_lock(mt); 53224a1cce3SDavid Greenman vm_page_activate(mt); 5333c4a2440SAlan Cox vm_page_unlock(mt); 534126d6082SKonstantin Belousov if (eio != NULL && i >= mreq && i - mreq < runlen) 535126d6082SKonstantin Belousov *eio = TRUE; 53626f9a767SRodney W. Grimes break; 53726f9a767SRodney W. Grimes case VM_PAGER_AGAIN: 5381e8a675cSKonstantin Belousov if (i >= mreq && i - mreq < runlen) 5391e8a675cSKonstantin Belousov runlen = i - mreq; 54026f9a767SRodney W. Grimes break; 54126f9a767SRodney W. Grimes } 54226f9a767SRodney W. Grimes 54326f9a767SRodney W. Grimes /* 5440d94caffSDavid Greenman * If the operation is still going, leave the page busy to 5450d94caffSDavid Greenman * block all other accesses. Also, leave the paging in 5460d94caffSDavid Greenman * progress indicator set so that we don't attempt an object 5470d94caffSDavid Greenman * collapse. 54826f9a767SRodney W. Grimes */ 54926f9a767SRodney W. Grimes if (pageout_status[i] != VM_PAGER_PEND) { 550f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 551c7aebda8SAttilio Rao vm_page_sunbusy(mt); 5523c4a2440SAlan Cox if (vm_page_count_severe()) { 5533c4a2440SAlan Cox vm_page_lock(mt); 5549ea8d1a6SAlan Cox vm_page_try_to_cache(mt); 5552965a453SKip Macy vm_page_unlock(mt); 55626f9a767SRodney W. Grimes } 5573c4a2440SAlan Cox } 5583c4a2440SAlan Cox } 5591e8a675cSKonstantin Belousov if (prunlen != NULL) 5601e8a675cSKonstantin Belousov *prunlen = runlen; 5613c4a2440SAlan Cox return (numpagedout); 56226f9a767SRodney W. Grimes } 56326f9a767SRodney W. Grimes 56485eeca35SAlan Cox static boolean_t 565449c2e92SKonstantin Belousov vm_pageout_launder(struct vm_pagequeue *pq, int tries, vm_paddr_t low, 566449c2e92SKonstantin Belousov vm_paddr_t high) 56785eeca35SAlan Cox { 56885eeca35SAlan Cox struct mount *mp; 56985eeca35SAlan Cox struct vnode *vp; 57085eeca35SAlan Cox vm_object_t object; 57185eeca35SAlan Cox vm_paddr_t pa; 57285eeca35SAlan Cox vm_page_t m, m_tmp, next; 57385eeca35SAlan Cox 5748d220203SAlan Cox vm_pagequeue_lock(pq); 575c325e866SKonstantin Belousov TAILQ_FOREACH_SAFE(m, &pq->pq_pl, plinks.q, next) { 57685eeca35SAlan Cox if ((m->flags & PG_MARKER) != 0) 57785eeca35SAlan Cox continue; 57885eeca35SAlan Cox pa = VM_PAGE_TO_PHYS(m); 57985eeca35SAlan Cox if (pa < low || pa + PAGE_SIZE > high) 58085eeca35SAlan Cox continue; 58185eeca35SAlan Cox if (!vm_pageout_page_lock(m, &next) || m->hold_count != 0) { 58285eeca35SAlan Cox vm_page_unlock(m); 58385eeca35SAlan Cox continue; 58485eeca35SAlan Cox } 58585eeca35SAlan Cox object = m->object; 58689f6b863SAttilio Rao if ((!VM_OBJECT_TRYWLOCK(object) && 58785eeca35SAlan Cox (!vm_pageout_fallback_object_lock(m, &next) || 588c7aebda8SAttilio Rao m->hold_count != 0)) || vm_page_busied(m)) { 58985eeca35SAlan Cox vm_page_unlock(m); 59089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 59185eeca35SAlan Cox continue; 59285eeca35SAlan Cox } 59385eeca35SAlan Cox vm_page_test_dirty(m); 5949fc4739dSAlan Cox if (m->dirty == 0 && object->ref_count != 0) 59585eeca35SAlan Cox pmap_remove_all(m); 59685eeca35SAlan Cox if (m->dirty != 0) { 59785eeca35SAlan Cox vm_page_unlock(m); 59885eeca35SAlan Cox if (tries == 0 || (object->flags & OBJ_DEAD) != 0) { 59989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 60085eeca35SAlan Cox continue; 60185eeca35SAlan Cox } 60285eeca35SAlan Cox if (object->type == OBJT_VNODE) { 6038d220203SAlan Cox vm_pagequeue_unlock(pq); 60485eeca35SAlan Cox vp = object->handle; 60585eeca35SAlan Cox vm_object_reference_locked(object); 60689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 60785eeca35SAlan Cox (void)vn_start_write(vp, &mp, V_WAIT); 60885eeca35SAlan Cox vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 60989f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 61085eeca35SAlan Cox vm_object_page_clean(object, 0, 0, OBJPC_SYNC); 61189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 61285eeca35SAlan Cox VOP_UNLOCK(vp, 0); 61385eeca35SAlan Cox vm_object_deallocate(object); 61485eeca35SAlan Cox vn_finished_write(mp); 61585eeca35SAlan Cox return (TRUE); 61685eeca35SAlan Cox } else if (object->type == OBJT_SWAP || 61785eeca35SAlan Cox object->type == OBJT_DEFAULT) { 6188d220203SAlan Cox vm_pagequeue_unlock(pq); 61985eeca35SAlan Cox m_tmp = m; 62085eeca35SAlan Cox vm_pageout_flush(&m_tmp, 1, VM_PAGER_PUT_SYNC, 62185eeca35SAlan Cox 0, NULL, NULL); 62289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 62385eeca35SAlan Cox return (TRUE); 62485eeca35SAlan Cox } 62585eeca35SAlan Cox } else { 6268d220203SAlan Cox /* 6278d220203SAlan Cox * Dequeue here to prevent lock recursion in 6288d220203SAlan Cox * vm_page_cache(). 6298d220203SAlan Cox */ 6308d220203SAlan Cox vm_page_dequeue_locked(m); 63185eeca35SAlan Cox vm_page_cache(m); 63285eeca35SAlan Cox vm_page_unlock(m); 63385eeca35SAlan Cox } 63489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 63585eeca35SAlan Cox } 6368d220203SAlan Cox vm_pagequeue_unlock(pq); 63785eeca35SAlan Cox return (FALSE); 63885eeca35SAlan Cox } 63985eeca35SAlan Cox 64085eeca35SAlan Cox /* 64185eeca35SAlan Cox * Increase the number of cached pages. The specified value, "tries", 64285eeca35SAlan Cox * determines which categories of pages are cached: 64385eeca35SAlan Cox * 64485eeca35SAlan Cox * 0: All clean, inactive pages within the specified physical address range 64585eeca35SAlan Cox * are cached. Will not sleep. 64685eeca35SAlan Cox * 1: The vm_lowmem handlers are called. All inactive pages within 64785eeca35SAlan Cox * the specified physical address range are cached. May sleep. 64885eeca35SAlan Cox * 2: The vm_lowmem handlers are called. All inactive and active pages 64985eeca35SAlan Cox * within the specified physical address range are cached. May sleep. 65085eeca35SAlan Cox */ 65185eeca35SAlan Cox void 65285eeca35SAlan Cox vm_pageout_grow_cache(int tries, vm_paddr_t low, vm_paddr_t high) 65385eeca35SAlan Cox { 654449c2e92SKonstantin Belousov int actl, actmax, inactl, inactmax, dom, initial_dom; 655449c2e92SKonstantin Belousov static int start_dom = 0; 65685eeca35SAlan Cox 65785eeca35SAlan Cox if (tries > 0) { 65885eeca35SAlan Cox /* 65985eeca35SAlan Cox * Decrease registered cache sizes. The vm_lowmem handlers 66085eeca35SAlan Cox * may acquire locks and/or sleep, so they can only be invoked 66185eeca35SAlan Cox * when "tries" is greater than zero. 66285eeca35SAlan Cox */ 66385eeca35SAlan Cox EVENTHANDLER_INVOKE(vm_lowmem, 0); 66485eeca35SAlan Cox 66585eeca35SAlan Cox /* 66685eeca35SAlan Cox * We do this explicitly after the caches have been drained 66785eeca35SAlan Cox * above. 66885eeca35SAlan Cox */ 66985eeca35SAlan Cox uma_reclaim(); 67085eeca35SAlan Cox } 671449c2e92SKonstantin Belousov 672449c2e92SKonstantin Belousov /* 673449c2e92SKonstantin Belousov * Make the next scan start on the next domain. 674449c2e92SKonstantin Belousov */ 675449c2e92SKonstantin Belousov initial_dom = atomic_fetchadd_int(&start_dom, 1) % vm_ndomains; 676449c2e92SKonstantin Belousov 67785eeca35SAlan Cox inactl = 0; 67885eeca35SAlan Cox inactmax = cnt.v_inactive_count; 67985eeca35SAlan Cox actl = 0; 68085eeca35SAlan Cox actmax = tries < 2 ? 0 : cnt.v_active_count; 681449c2e92SKonstantin Belousov dom = initial_dom; 682449c2e92SKonstantin Belousov 683449c2e92SKonstantin Belousov /* 684449c2e92SKonstantin Belousov * Scan domains in round-robin order, first inactive queues, 685449c2e92SKonstantin Belousov * then active. Since domain usually owns large physically 686449c2e92SKonstantin Belousov * contiguous chunk of memory, it makes sense to completely 687449c2e92SKonstantin Belousov * exhaust one domain before switching to next, while growing 688449c2e92SKonstantin Belousov * the pool of contiguous physical pages. 689449c2e92SKonstantin Belousov * 690449c2e92SKonstantin Belousov * Do not even start launder a domain which cannot contain 691449c2e92SKonstantin Belousov * the specified address range, as indicated by segments 692449c2e92SKonstantin Belousov * constituting the domain. 693449c2e92SKonstantin Belousov */ 69485eeca35SAlan Cox again: 695449c2e92SKonstantin Belousov if (inactl < inactmax) { 696449c2e92SKonstantin Belousov if (vm_phys_domain_intersects(vm_dom[dom].vmd_segs, 697449c2e92SKonstantin Belousov low, high) && 698449c2e92SKonstantin Belousov vm_pageout_launder(&vm_dom[dom].vmd_pagequeues[PQ_INACTIVE], 699449c2e92SKonstantin Belousov tries, low, high)) { 70085eeca35SAlan Cox inactl++; 70185eeca35SAlan Cox goto again; 70285eeca35SAlan Cox } 703449c2e92SKonstantin Belousov if (++dom == vm_ndomains) 704449c2e92SKonstantin Belousov dom = 0; 705449c2e92SKonstantin Belousov if (dom != initial_dom) 706449c2e92SKonstantin Belousov goto again; 707449c2e92SKonstantin Belousov } 708449c2e92SKonstantin Belousov if (actl < actmax) { 709449c2e92SKonstantin Belousov if (vm_phys_domain_intersects(vm_dom[dom].vmd_segs, 710449c2e92SKonstantin Belousov low, high) && 711449c2e92SKonstantin Belousov vm_pageout_launder(&vm_dom[dom].vmd_pagequeues[PQ_ACTIVE], 712449c2e92SKonstantin Belousov tries, low, high)) { 71385eeca35SAlan Cox actl++; 71485eeca35SAlan Cox goto again; 71585eeca35SAlan Cox } 716449c2e92SKonstantin Belousov if (++dom == vm_ndomains) 717449c2e92SKonstantin Belousov dom = 0; 718449c2e92SKonstantin Belousov if (dom != initial_dom) 719449c2e92SKonstantin Belousov goto again; 720449c2e92SKonstantin Belousov } 72185eeca35SAlan Cox } 72285eeca35SAlan Cox 72338efa82bSJohn Dyson #if !defined(NO_SWAPPING) 72426f9a767SRodney W. Grimes /* 72526f9a767SRodney W. Grimes * vm_pageout_object_deactivate_pages 72626f9a767SRodney W. Grimes * 727ce186587SAlan Cox * Deactivate enough pages to satisfy the inactive target 728ce186587SAlan Cox * requirements. 72926f9a767SRodney W. Grimes * 73026f9a767SRodney W. Grimes * The object and map must be locked. 73126f9a767SRodney W. Grimes */ 73238efa82bSJohn Dyson static void 733ce186587SAlan Cox vm_pageout_object_deactivate_pages(pmap_t pmap, vm_object_t first_object, 734ce186587SAlan Cox long desired) 73526f9a767SRodney W. Grimes { 736ecf6279fSAlan Cox vm_object_t backing_object, object; 737ce186587SAlan Cox vm_page_t p; 738bb7858eaSJeff Roberson int act_delta, remove_mode; 73926f9a767SRodney W. Grimes 740e23b0a19SAlan Cox VM_OBJECT_ASSERT_LOCKED(first_object); 74128634820SAlan Cox if ((first_object->flags & OBJ_FICTITIOUS) != 0) 74238efa82bSJohn Dyson return; 743ecf6279fSAlan Cox for (object = first_object;; object = backing_object) { 744ecf6279fSAlan Cox if (pmap_resident_count(pmap) <= desired) 745ecf6279fSAlan Cox goto unlock_return; 746e23b0a19SAlan Cox VM_OBJECT_ASSERT_LOCKED(object); 74728634820SAlan Cox if ((object->flags & OBJ_UNMANAGED) != 0 || 74828634820SAlan Cox object->paging_in_progress != 0) 749ecf6279fSAlan Cox goto unlock_return; 75026f9a767SRodney W. Grimes 75185b1dc89SAlan Cox remove_mode = 0; 75238efa82bSJohn Dyson if (object->shadow_count > 1) 75338efa82bSJohn Dyson remove_mode = 1; 75426f9a767SRodney W. Grimes /* 755ce186587SAlan Cox * Scan the object's entire memory queue. 75626f9a767SRodney W. Grimes */ 757ce186587SAlan Cox TAILQ_FOREACH(p, &object->memq, listq) { 758447fe2a4SAlan Cox if (pmap_resident_count(pmap) <= desired) 759447fe2a4SAlan Cox goto unlock_return; 760c7aebda8SAttilio Rao if (vm_page_busied(p)) 761447fe2a4SAlan Cox continue; 762ce186587SAlan Cox PCPU_INC(cnt.v_pdpages); 7632965a453SKip Macy vm_page_lock(p); 764ce186587SAlan Cox if (p->wire_count != 0 || p->hold_count != 0 || 765ecf6279fSAlan Cox !pmap_page_exists_quick(pmap, p)) { 7662965a453SKip Macy vm_page_unlock(p); 7670d94caffSDavid Greenman continue; 7680d94caffSDavid Greenman } 769bb7858eaSJeff Roberson act_delta = pmap_ts_referenced(p); 7703407fefeSKonstantin Belousov if ((p->aflags & PGA_REFERENCED) != 0) { 771bb7858eaSJeff Roberson if (act_delta == 0) 772bb7858eaSJeff Roberson act_delta = 1; 7733407fefeSKonstantin Belousov vm_page_aflag_clear(p, PGA_REFERENCED); 774ef743ce6SJohn Dyson } 775bb7858eaSJeff Roberson if (p->queue != PQ_ACTIVE && act_delta != 0) { 776ef743ce6SJohn Dyson vm_page_activate(p); 777bb7858eaSJeff Roberson p->act_count += act_delta; 778c8c4b40cSJohn Dyson } else if (p->queue == PQ_ACTIVE) { 779bb7858eaSJeff Roberson if (act_delta == 0) { 780ce186587SAlan Cox p->act_count -= min(p->act_count, 781ce186587SAlan Cox ACT_DECLINE); 78290776bd7SJeff Roberson if (!remove_mode && p->act_count == 0) { 7834fec79beSAlan Cox pmap_remove_all(p); 78426f9a767SRodney W. Grimes vm_page_deactivate(p); 7858d220203SAlan Cox } else 7868d220203SAlan Cox vm_page_requeue(p); 787c8c4b40cSJohn Dyson } else { 788eaf13dd7SJohn Dyson vm_page_activate(p); 789ce186587SAlan Cox if (p->act_count < ACT_MAX - 790ce186587SAlan Cox ACT_ADVANCE) 79138efa82bSJohn Dyson p->act_count += ACT_ADVANCE; 7928d220203SAlan Cox vm_page_requeue(p); 793ce186587SAlan Cox } 794ce186587SAlan Cox } else if (p->queue == PQ_INACTIVE) 795ce186587SAlan Cox pmap_remove_all(p); 7962965a453SKip Macy vm_page_unlock(p); 79726f9a767SRodney W. Grimes } 798ecf6279fSAlan Cox if ((backing_object = object->backing_object) == NULL) 799ecf6279fSAlan Cox goto unlock_return; 800e23b0a19SAlan Cox VM_OBJECT_RLOCK(backing_object); 801ecf6279fSAlan Cox if (object != first_object) 802e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(object); 80338efa82bSJohn Dyson } 804ecf6279fSAlan Cox unlock_return: 805ecf6279fSAlan Cox if (object != first_object) 806e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(object); 80726f9a767SRodney W. Grimes } 80826f9a767SRodney W. Grimes 80926f9a767SRodney W. Grimes /* 81026f9a767SRodney W. Grimes * deactivate some number of pages in a map, try to do it fairly, but 81126f9a767SRodney W. Grimes * that is really hard to do. 81226f9a767SRodney W. Grimes */ 813cd41fc12SDavid Greenman static void 81438efa82bSJohn Dyson vm_pageout_map_deactivate_pages(map, desired) 81526f9a767SRodney W. Grimes vm_map_t map; 816ecf6279fSAlan Cox long desired; 81726f9a767SRodney W. Grimes { 81826f9a767SRodney W. Grimes vm_map_entry_t tmpe; 81938efa82bSJohn Dyson vm_object_t obj, bigobj; 82030105b9eSTor Egge int nothingwired; 8210d94caffSDavid Greenman 822d974f03cSAlan Cox if (!vm_map_trylock(map)) 82326f9a767SRodney W. Grimes return; 82438efa82bSJohn Dyson 82538efa82bSJohn Dyson bigobj = NULL; 82630105b9eSTor Egge nothingwired = TRUE; 82738efa82bSJohn Dyson 82838efa82bSJohn Dyson /* 82938efa82bSJohn Dyson * first, search out the biggest object, and try to free pages from 83038efa82bSJohn Dyson * that. 83138efa82bSJohn Dyson */ 83226f9a767SRodney W. Grimes tmpe = map->header.next; 83338efa82bSJohn Dyson while (tmpe != &map->header) { 8349fdfe602SMatthew Dillon if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 83538efa82bSJohn Dyson obj = tmpe->object.vm_object; 836e23b0a19SAlan Cox if (obj != NULL && VM_OBJECT_TRYRLOCK(obj)) { 8370774dfb3SAlan Cox if (obj->shadow_count <= 1 && 8380774dfb3SAlan Cox (bigobj == NULL || 8390774dfb3SAlan Cox bigobj->resident_page_count < obj->resident_page_count)) { 8400774dfb3SAlan Cox if (bigobj != NULL) 841e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(bigobj); 84238efa82bSJohn Dyson bigobj = obj; 8430774dfb3SAlan Cox } else 844e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(obj); 84538efa82bSJohn Dyson } 84638efa82bSJohn Dyson } 84730105b9eSTor Egge if (tmpe->wired_count > 0) 84830105b9eSTor Egge nothingwired = FALSE; 84938efa82bSJohn Dyson tmpe = tmpe->next; 85038efa82bSJohn Dyson } 85138efa82bSJohn Dyson 8520774dfb3SAlan Cox if (bigobj != NULL) { 853ecf6279fSAlan Cox vm_pageout_object_deactivate_pages(map->pmap, bigobj, desired); 854e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(bigobj); 8550774dfb3SAlan Cox } 85638efa82bSJohn Dyson /* 85738efa82bSJohn Dyson * Next, hunt around for other pages to deactivate. We actually 85838efa82bSJohn Dyson * do this search sort of wrong -- .text first is not the best idea. 85938efa82bSJohn Dyson */ 86038efa82bSJohn Dyson tmpe = map->header.next; 86138efa82bSJohn Dyson while (tmpe != &map->header) { 862b1028ad1SLuoqi Chen if (pmap_resident_count(vm_map_pmap(map)) <= desired) 86338efa82bSJohn Dyson break; 8649fdfe602SMatthew Dillon if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 86538efa82bSJohn Dyson obj = tmpe->object.vm_object; 8660774dfb3SAlan Cox if (obj != NULL) { 867e23b0a19SAlan Cox VM_OBJECT_RLOCK(obj); 868ecf6279fSAlan Cox vm_pageout_object_deactivate_pages(map->pmap, obj, desired); 869e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(obj); 8700774dfb3SAlan Cox } 87138efa82bSJohn Dyson } 87226f9a767SRodney W. Grimes tmpe = tmpe->next; 87338857e7fSAlan Cox } 87438efa82bSJohn Dyson 87538efa82bSJohn Dyson /* 87638efa82bSJohn Dyson * Remove all mappings if a process is swapped out, this will free page 87738efa82bSJohn Dyson * table pages. 87838efa82bSJohn Dyson */ 87938857e7fSAlan Cox if (desired == 0 && nothingwired) { 8808d01a3b2SNathan Whitehorn pmap_remove(vm_map_pmap(map), vm_map_min(map), 8818d01a3b2SNathan Whitehorn vm_map_max(map)); 88238857e7fSAlan Cox } 88338efa82bSJohn Dyson vm_map_unlock(map); 88426f9a767SRodney W. Grimes } 885a1287949SEivind Eklund #endif /* !defined(NO_SWAPPING) */ 886df8bae1dSRodney W. Grimes 8871c7c3c6aSMatthew Dillon /* 888df8bae1dSRodney W. Grimes * vm_pageout_scan does the dirty work for the pageout daemon. 889d9e23210SJeff Roberson * 890d9e23210SJeff Roberson * pass 0 - Update active LRU/deactivate pages 891d9e23210SJeff Roberson * pass 1 - Move inactive to cache or free 892d9e23210SJeff Roberson * pass 2 - Launder dirty pages 893df8bae1dSRodney W. Grimes */ 8942b6b0df7SMatthew Dillon static void 895449c2e92SKonstantin Belousov vm_pageout_scan(struct vm_domain *vmd, int pass) 896df8bae1dSRodney W. Grimes { 897502ba6e4SJohn Dyson vm_page_t m, next; 8988d220203SAlan Cox struct vm_pagequeue *pq; 8991c7c3c6aSMatthew Dillon int page_shortage, maxscan, pcount; 9002cba1ccdSAlan Cox int addl_page_shortage; 901df8bae1dSRodney W. Grimes vm_object_t object; 902bb7858eaSJeff Roberson int act_delta; 903f6b04d2bSDavid Greenman int vnodes_skipped = 0; 9042b6b0df7SMatthew Dillon int maxlaunder; 90548cc2fc7SKonstantin Belousov boolean_t queues_locked; 9060d94caffSDavid Greenman 907df8bae1dSRodney W. Grimes /* 908d9e23210SJeff Roberson * If we need to reclaim memory ask kernel caches to return 909*c9612b2dSJeff Roberson * some. We rate limit to avoid thrashing. 910d9e23210SJeff Roberson */ 911*c9612b2dSJeff Roberson if (vmd == &vm_dom[0] && pass > 0 && 912*c9612b2dSJeff Roberson lowmem_ticks + (lowmem_period * hz) < ticks) { 913d9e23210SJeff Roberson /* 914855a310fSJeff Roberson * Decrease registered cache sizes. 915855a310fSJeff Roberson */ 916855a310fSJeff Roberson EVENTHANDLER_INVOKE(vm_lowmem, 0); 917855a310fSJeff Roberson /* 918d9e23210SJeff Roberson * We do this explicitly after the caches have been 919d9e23210SJeff Roberson * drained above. 920855a310fSJeff Roberson */ 921855a310fSJeff Roberson uma_reclaim(); 922*c9612b2dSJeff Roberson lowmem_ticks = ticks; 923d9e23210SJeff Roberson } 9245985940eSJohn Dyson 925311e34e2SKonstantin Belousov /* 92696240c89SEitan Adler * The addl_page_shortage is the number of temporarily 927311e34e2SKonstantin Belousov * stuck pages in the inactive queue. In other words, the 928449c2e92SKonstantin Belousov * number of pages from the inactive count that should be 929311e34e2SKonstantin Belousov * discounted in setting the target for the active queue scan. 930311e34e2SKonstantin Belousov */ 9312cba1ccdSAlan Cox addl_page_shortage = atomic_readandclear_int(&vm_pageout_deficit); 932b182ec9eSJohn Dyson 9331c7c3c6aSMatthew Dillon /* 9341c7c3c6aSMatthew Dillon * Calculate the number of pages we want to either free or move 9352b6b0df7SMatthew Dillon * to the cache. 9361c7c3c6aSMatthew Dillon */ 9372cba1ccdSAlan Cox page_shortage = vm_paging_target() + addl_page_shortage; 9381c7c3c6aSMatthew Dillon 939936524aaSMatthew Dillon /* 9402b6b0df7SMatthew Dillon * maxlaunder limits the number of dirty pages we flush per scan. 9412b6b0df7SMatthew Dillon * For most systems a smaller value (16 or 32) is more robust under 9422b6b0df7SMatthew Dillon * extreme memory and disk pressure because any unnecessary writes 9432b6b0df7SMatthew Dillon * to disk can result in extreme performance degredation. However, 9442b6b0df7SMatthew Dillon * systems with excessive dirty pages (especially when MAP_NOSYNC is 9452b6b0df7SMatthew Dillon * used) will die horribly with limited laundering. If the pageout 9462b6b0df7SMatthew Dillon * daemon cannot clean enough pages in the first pass, we let it go 9472b6b0df7SMatthew Dillon * all out in succeeding passes. 9481c7c3c6aSMatthew Dillon */ 9492b6b0df7SMatthew Dillon if ((maxlaunder = vm_max_launder) <= 1) 9502b6b0df7SMatthew Dillon maxlaunder = 1; 951d9e23210SJeff Roberson if (pass > 1) 9522b6b0df7SMatthew Dillon maxlaunder = 10000; 9538d220203SAlan Cox 9548d220203SAlan Cox /* 9558d220203SAlan Cox * Start scanning the inactive queue for pages we can move to the 9568d220203SAlan Cox * cache or free. The scan will stop when the target is reached or 9578d220203SAlan Cox * we have scanned the entire inactive queue. Note that m->act_count 9588d220203SAlan Cox * is not used to form decisions for the inactive queue, only for the 9598d220203SAlan Cox * active queue. 9608d220203SAlan Cox */ 961449c2e92SKonstantin Belousov pq = &vmd->vmd_pagequeues[PQ_INACTIVE]; 962449c2e92SKonstantin Belousov maxscan = pq->pq_cnt; 9638d220203SAlan Cox vm_pagequeue_lock(pq); 9648d220203SAlan Cox queues_locked = TRUE; 9658d220203SAlan Cox for (m = TAILQ_FIRST(&pq->pq_pl); 9661c7c3c6aSMatthew Dillon m != NULL && maxscan-- > 0 && page_shortage > 0; 967e929c00dSKirk McKusick m = next) { 9688d220203SAlan Cox vm_pagequeue_assert_locked(pq); 96948cc2fc7SKonstantin Belousov KASSERT(queues_locked, ("unlocked queues")); 970d4961bcbSKonstantin Belousov KASSERT(m->queue == PQ_INACTIVE, ("Inactive queue %p", m)); 971df8bae1dSRodney W. Grimes 9728d220203SAlan Cox PCPU_INC(cnt.v_pdpages); 973c325e866SKonstantin Belousov next = TAILQ_NEXT(m, plinks.q); 974df8bae1dSRodney W. Grimes 975936524aaSMatthew Dillon /* 976936524aaSMatthew Dillon * skip marker pages 977936524aaSMatthew Dillon */ 978936524aaSMatthew Dillon if (m->flags & PG_MARKER) 979936524aaSMatthew Dillon continue; 980936524aaSMatthew Dillon 9817900f95dSKonstantin Belousov KASSERT((m->flags & PG_FICTITIOUS) == 0, 9827900f95dSKonstantin Belousov ("Fictitious page %p cannot be in inactive queue", m)); 9837900f95dSKonstantin Belousov KASSERT((m->oflags & VPO_UNMANAGED) == 0, 9847900f95dSKonstantin Belousov ("Unmanaged page %p cannot be in inactive queue", m)); 9857900f95dSKonstantin Belousov 9868c616246SKonstantin Belousov /* 987311e34e2SKonstantin Belousov * The page or object lock acquisitions fail if the 988311e34e2SKonstantin Belousov * page was removed from the queue or moved to a 989311e34e2SKonstantin Belousov * different position within the queue. In either 990311e34e2SKonstantin Belousov * case, addl_page_shortage should not be incremented. 9918c616246SKonstantin Belousov */ 9928c616246SKonstantin Belousov if (!vm_pageout_page_lock(m, &next)) { 9938c616246SKonstantin Belousov vm_page_unlock(m); 994b182ec9eSJohn Dyson continue; 995df8bae1dSRodney W. Grimes } 9969ee2165fSAlan Cox object = m->object; 99789f6b863SAttilio Rao if (!VM_OBJECT_TRYWLOCK(object) && 998311e34e2SKonstantin Belousov !vm_pageout_fallback_object_lock(m, &next)) { 9992965a453SKip Macy vm_page_unlock(m); 100089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 100134d9e6fdSAlan Cox continue; 100234d9e6fdSAlan Cox } 1003311e34e2SKonstantin Belousov 1004311e34e2SKonstantin Belousov /* 1005311e34e2SKonstantin Belousov * Don't mess with busy pages, keep them at at the 1006311e34e2SKonstantin Belousov * front of the queue, most likely they are being 1007311e34e2SKonstantin Belousov * paged out. Increment addl_page_shortage for busy 1008311e34e2SKonstantin Belousov * pages, because they may leave the inactive queue 1009311e34e2SKonstantin Belousov * shortly after page scan is finished. 1010311e34e2SKonstantin Belousov */ 1011c7aebda8SAttilio Rao if (vm_page_busied(m)) { 10122965a453SKip Macy vm_page_unlock(m); 101389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1014b182ec9eSJohn Dyson addl_page_shortage++; 101526f9a767SRodney W. Grimes continue; 101626f9a767SRodney W. Grimes } 1017bd7e5f99SJohn Dyson 10187e006499SJohn Dyson /* 10198d220203SAlan Cox * We unlock the inactive page queue, invalidating the 102048cc2fc7SKonstantin Belousov * 'next' pointer. Use our marker to remember our 102148cc2fc7SKonstantin Belousov * place. 102248cc2fc7SKonstantin Belousov */ 1023c325e866SKonstantin Belousov TAILQ_INSERT_AFTER(&pq->pq_pl, m, &vmd->vmd_marker, plinks.q); 10248d220203SAlan Cox vm_pagequeue_unlock(pq); 102548cc2fc7SKonstantin Belousov queues_locked = FALSE; 102648cc2fc7SKonstantin Belousov 102748cc2fc7SKonstantin Belousov /* 1028bb7858eaSJeff Roberson * We bump the activation count if the page has been 1029bb7858eaSJeff Roberson * referenced while in the inactive queue. This makes 1030bb7858eaSJeff Roberson * it less likely that the page will be added back to the 1031bb7858eaSJeff Roberson * inactive queue prematurely again. Here we check the 10321c7c3c6aSMatthew Dillon * page tables (or emulated bits, if any), given the upper 10331c7c3c6aSMatthew Dillon * level VM system not knowing anything about existing 10341c7c3c6aSMatthew Dillon * references. 10357e006499SJohn Dyson */ 1036bb7858eaSJeff Roberson act_delta = 0; 1037bb7858eaSJeff Roberson if ((m->aflags & PGA_REFERENCED) != 0) { 1038bb7858eaSJeff Roberson vm_page_aflag_clear(m, PGA_REFERENCED); 1039bb7858eaSJeff Roberson act_delta = 1; 1040bb7858eaSJeff Roberson } 1041bb7858eaSJeff Roberson if (object->ref_count != 0) { 1042bb7858eaSJeff Roberson act_delta += pmap_ts_referenced(m); 1043bb7858eaSJeff Roberson } else { 1044bb7858eaSJeff Roberson KASSERT(!pmap_page_is_mapped(m), 1045bb7858eaSJeff Roberson ("vm_pageout_scan: page %p is mapped", m)); 10462fe6e4d7SDavid Greenman } 1047ef743ce6SJohn Dyson 10487e006499SJohn Dyson /* 10491c7c3c6aSMatthew Dillon * If the upper level VM system knows about any page 1050bb7858eaSJeff Roberson * references, we reactivate the page or requeue it. 10517e006499SJohn Dyson */ 1052bb7858eaSJeff Roberson if (act_delta != 0) { 1053bb7858eaSJeff Roberson if (object->ref_count) { 105426f9a767SRodney W. Grimes vm_page_activate(m); 1055bb7858eaSJeff Roberson m->act_count += act_delta + ACT_ADVANCE; 1056bb7858eaSJeff Roberson } else { 1057bb7858eaSJeff Roberson vm_pagequeue_lock(pq); 1058bb7858eaSJeff Roberson queues_locked = TRUE; 1059bb7858eaSJeff Roberson vm_page_requeue_locked(m); 1060bb7858eaSJeff Roberson } 106189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1062a15f7df5SAttilio Rao vm_page_unlock(m); 106348cc2fc7SKonstantin Belousov goto relock_queues; 10640d94caffSDavid Greenman } 106567bf6868SJohn Dyson 1066311e34e2SKonstantin Belousov if (m->hold_count != 0) { 1067311e34e2SKonstantin Belousov vm_page_unlock(m); 106889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1069311e34e2SKonstantin Belousov 1070311e34e2SKonstantin Belousov /* 1071311e34e2SKonstantin Belousov * Held pages are essentially stuck in the 1072311e34e2SKonstantin Belousov * queue. So, they ought to be discounted 1073449c2e92SKonstantin Belousov * from the inactive count. See the 1074311e34e2SKonstantin Belousov * calculation of the page_shortage for the 1075311e34e2SKonstantin Belousov * loop over the active queue below. 1076311e34e2SKonstantin Belousov */ 1077311e34e2SKonstantin Belousov addl_page_shortage++; 1078311e34e2SKonstantin Belousov goto relock_queues; 1079311e34e2SKonstantin Belousov } 1080311e34e2SKonstantin Belousov 10817e006499SJohn Dyson /* 10829fc4739dSAlan Cox * If the page appears to be clean at the machine-independent 10839fc4739dSAlan Cox * layer, then remove all of its mappings from the pmap in 10849fc4739dSAlan Cox * anticipation of placing it onto the cache queue. If, 10859fc4739dSAlan Cox * however, any of the page's mappings allow write access, 10869fc4739dSAlan Cox * then the page may still be modified until the last of those 10879fc4739dSAlan Cox * mappings are removed. 10887e006499SJohn Dyson */ 10899fc4739dSAlan Cox vm_page_test_dirty(m); 10909fc4739dSAlan Cox if (m->dirty == 0 && object->ref_count != 0) 1091b78ddb0bSAlan Cox pmap_remove_all(m); 1092dcbcd518SBruce Evans 10936989c456SAlan Cox if (m->valid == 0) { 10947e006499SJohn Dyson /* 10957e006499SJohn Dyson * Invalid pages can be easily freed 10967e006499SJohn Dyson */ 10976989c456SAlan Cox vm_page_free(m); 109848cc2fc7SKonstantin Belousov PCPU_INC(cnt.v_dfree); 10991c7c3c6aSMatthew Dillon --page_shortage; 1100bd7e5f99SJohn Dyson } else if (m->dirty == 0) { 11016989c456SAlan Cox /* 11026989c456SAlan Cox * Clean pages can be placed onto the cache queue. 11036989c456SAlan Cox * This effectively frees them. 11046989c456SAlan Cox */ 1105bd7e5f99SJohn Dyson vm_page_cache(m); 11061c7c3c6aSMatthew Dillon --page_shortage; 1107d9e23210SJeff Roberson } else if ((m->flags & PG_WINATCFLS) == 0 && pass < 2) { 11087e006499SJohn Dyson /* 11092b6b0df7SMatthew Dillon * Dirty pages need to be paged out, but flushing 11102b6b0df7SMatthew Dillon * a page is extremely expensive verses freeing 11112b6b0df7SMatthew Dillon * a clean page. Rather then artificially limiting 11122b6b0df7SMatthew Dillon * the number of pages we can flush, we instead give 11132b6b0df7SMatthew Dillon * dirty pages extra priority on the inactive queue 11142b6b0df7SMatthew Dillon * by forcing them to be cycled through the queue 11152b6b0df7SMatthew Dillon * twice before being flushed, after which the 11162b6b0df7SMatthew Dillon * (now clean) page will cycle through once more 11172b6b0df7SMatthew Dillon * before being freed. This significantly extends 11182b6b0df7SMatthew Dillon * the thrash point for a heavily loaded machine. 11197e006499SJohn Dyson */ 11203407fefeSKonstantin Belousov m->flags |= PG_WINATCFLS; 11218d220203SAlan Cox vm_pagequeue_lock(pq); 112248cc2fc7SKonstantin Belousov queues_locked = TRUE; 11238d220203SAlan Cox vm_page_requeue_locked(m); 11240d94caffSDavid Greenman } else if (maxlaunder > 0) { 11252b6b0df7SMatthew Dillon /* 11262b6b0df7SMatthew Dillon * We always want to try to flush some dirty pages if 11272b6b0df7SMatthew Dillon * we encounter them, to keep the system stable. 11282b6b0df7SMatthew Dillon * Normally this number is small, but under extreme 11292b6b0df7SMatthew Dillon * pressure where there are insufficient clean pages 11302b6b0df7SMatthew Dillon * on the inactive queue, we may have to go all out. 11312b6b0df7SMatthew Dillon */ 11325050aa86SKonstantin Belousov int swap_pageouts_ok; 1133f6b04d2bSDavid Greenman struct vnode *vp = NULL; 113414137dc0SAlan Cox struct mount *mp = NULL; 11350d94caffSDavid Greenman 113612ac6a1dSJohn Dyson if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) { 113712ac6a1dSJohn Dyson swap_pageouts_ok = 1; 113812ac6a1dSJohn Dyson } else { 113912ac6a1dSJohn Dyson swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts); 114012ac6a1dSJohn Dyson swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts && 114190ecac61SMatthew Dillon vm_page_count_min()); 114212ac6a1dSJohn Dyson 114312ac6a1dSJohn Dyson } 114470111b90SJohn Dyson 114570111b90SJohn Dyson /* 11461c7c3c6aSMatthew Dillon * We don't bother paging objects that are "dead". 11471c7c3c6aSMatthew Dillon * Those objects are in a "rundown" state. 114870111b90SJohn Dyson */ 114970111b90SJohn Dyson if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) { 11508d220203SAlan Cox vm_pagequeue_lock(pq); 11512965a453SKip Macy vm_page_unlock(m); 115289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 115348cc2fc7SKonstantin Belousov queues_locked = TRUE; 11548d220203SAlan Cox vm_page_requeue_locked(m); 115548cc2fc7SKonstantin Belousov goto relock_queues; 115612ac6a1dSJohn Dyson } 115712ac6a1dSJohn Dyson 11581c7c3c6aSMatthew Dillon /* 11592b6b0df7SMatthew Dillon * The object is already known NOT to be dead. It 11602b6b0df7SMatthew Dillon * is possible for the vget() to block the whole 11612b6b0df7SMatthew Dillon * pageout daemon, but the new low-memory handling 11622b6b0df7SMatthew Dillon * code should prevent it. 11631c7c3c6aSMatthew Dillon * 11642b6b0df7SMatthew Dillon * The previous code skipped locked vnodes and, worse, 11652b6b0df7SMatthew Dillon * reordered pages in the queue. This results in 11662b6b0df7SMatthew Dillon * completely non-deterministic operation and, on a 11672b6b0df7SMatthew Dillon * busy system, can lead to extremely non-optimal 11682b6b0df7SMatthew Dillon * pageouts. For example, it can cause clean pages 11692b6b0df7SMatthew Dillon * to be freed and dirty pages to be moved to the end 11702b6b0df7SMatthew Dillon * of the queue. Since dirty pages are also moved to 11712b6b0df7SMatthew Dillon * the end of the queue once-cleaned, this gives 11722b6b0df7SMatthew Dillon * way too large a weighting to defering the freeing 11732b6b0df7SMatthew Dillon * of dirty pages. 11741c7c3c6aSMatthew Dillon * 117523b59018SMatthew Dillon * We can't wait forever for the vnode lock, we might 117623b59018SMatthew Dillon * deadlock due to a vn_read() getting stuck in 117723b59018SMatthew Dillon * vm_wait while holding this vnode. We skip the 117823b59018SMatthew Dillon * vnode if we can't get it in a reasonable amount 117923b59018SMatthew Dillon * of time. 11801c7c3c6aSMatthew Dillon */ 11811c7c3c6aSMatthew Dillon if (object->type == OBJT_VNODE) { 11822965a453SKip Macy vm_page_unlock(m); 118324a1cce3SDavid Greenman vp = object->handle; 1184db27dcc0STor Egge if (vp->v_type == VREG && 1185db27dcc0STor Egge vn_start_write(vp, &mp, V_NOWAIT) != 0) { 11866129343dSKonstantin Belousov mp = NULL; 1187db27dcc0STor Egge ++pageout_lock_miss; 1188db27dcc0STor Egge if (object->flags & OBJ_MIGHTBEDIRTY) 1189db27dcc0STor Egge vnodes_skipped++; 1190625e6c0aSTor Egge goto unlock_and_continue; 1191db27dcc0STor Egge } 1192b9f180d1SKonstantin Belousov KASSERT(mp != NULL, 1193b9f180d1SKonstantin Belousov ("vp %p with NULL v_mount", vp)); 119414137dc0SAlan Cox vm_object_reference_locked(object); 119589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 119697824da3SAlan Cox if (vget(vp, LK_EXCLUSIVE | LK_TIMELOCK, 119797824da3SAlan Cox curthread)) { 119889f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 119923b59018SMatthew Dillon ++pageout_lock_miss; 1200aef922f5SJohn Dyson if (object->flags & OBJ_MIGHTBEDIRTY) 1201925a3a41SJohn Dyson vnodes_skipped++; 1202625e6c0aSTor Egge vp = NULL; 1203625e6c0aSTor Egge goto unlock_and_continue; 120485a376ebSJohn Dyson } 120589f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 12062965a453SKip Macy vm_page_lock(m); 12078d220203SAlan Cox vm_pagequeue_lock(pq); 120848cc2fc7SKonstantin Belousov queues_locked = TRUE; 1209f35329acSJohn Dyson /* 1210936524aaSMatthew Dillon * The page might have been moved to another 1211936524aaSMatthew Dillon * queue during potential blocking in vget() 1212936524aaSMatthew Dillon * above. The page might have been freed and 121314137dc0SAlan Cox * reused for another vnode. 1214f35329acSJohn Dyson */ 12159cf51988SAlan Cox if (m->queue != PQ_INACTIVE || 1216936524aaSMatthew Dillon m->object != object || 1217c325e866SKonstantin Belousov TAILQ_NEXT(m, plinks.q) != &vmd->vmd_marker) { 12182965a453SKip Macy vm_page_unlock(m); 1219b182ec9eSJohn Dyson if (object->flags & OBJ_MIGHTBEDIRTY) 1220925a3a41SJohn Dyson vnodes_skipped++; 12213562af12SAlan Cox goto unlock_and_continue; 1222b182ec9eSJohn Dyson } 1223b182ec9eSJohn Dyson 1224f35329acSJohn Dyson /* 1225936524aaSMatthew Dillon * The page may have been busied during the 122614137dc0SAlan Cox * blocking in vget(). We don't move the 1227936524aaSMatthew Dillon * page back onto the end of the queue so that 1228936524aaSMatthew Dillon * statistics are more correct if we don't. 1229f35329acSJohn Dyson */ 1230c7aebda8SAttilio Rao if (vm_page_busied(m)) { 12312965a453SKip Macy vm_page_unlock(m); 12323562af12SAlan Cox goto unlock_and_continue; 1233b182ec9eSJohn Dyson } 1234b182ec9eSJohn Dyson 1235f35329acSJohn Dyson /* 123657601bcbSMatthew Dillon * If the page has become held it might 123757601bcbSMatthew Dillon * be undergoing I/O, so skip it 1238f35329acSJohn Dyson */ 1239b182ec9eSJohn Dyson if (m->hold_count) { 12402965a453SKip Macy vm_page_unlock(m); 12418d220203SAlan Cox vm_page_requeue_locked(m); 1242b182ec9eSJohn Dyson if (object->flags & OBJ_MIGHTBEDIRTY) 1243925a3a41SJohn Dyson vnodes_skipped++; 12443562af12SAlan Cox goto unlock_and_continue; 1245f6b04d2bSDavid Greenman } 12468d220203SAlan Cox vm_pagequeue_unlock(pq); 1247a4156419SKonstantin Belousov queues_locked = FALSE; 1248f6b04d2bSDavid Greenman } 1249f6b04d2bSDavid Greenman 12500d94caffSDavid Greenman /* 12510d94caffSDavid Greenman * If a page is dirty, then it is either being washed 12520d94caffSDavid Greenman * (but not yet cleaned) or it is still in the 12530d94caffSDavid Greenman * laundry. If it is still in the laundry, then we 12542b6b0df7SMatthew Dillon * start the cleaning operation. 1255936524aaSMatthew Dillon * 12562b6b0df7SMatthew Dillon * decrement page_shortage on success to account for 12572b6b0df7SMatthew Dillon * the (future) cleaned page. Otherwise we could wind 12582b6b0df7SMatthew Dillon * up laundering or cleaning too many pages. 12590d94caffSDavid Greenman */ 12602b6b0df7SMatthew Dillon if (vm_pageout_clean(m) != 0) { 12612b6b0df7SMatthew Dillon --page_shortage; 1262936524aaSMatthew Dillon --maxlaunder; 12632b6b0df7SMatthew Dillon } 12643562af12SAlan Cox unlock_and_continue: 12652965a453SKip Macy vm_page_lock_assert(m, MA_NOTOWNED); 126689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 126714137dc0SAlan Cox if (mp != NULL) { 126848cc2fc7SKonstantin Belousov if (queues_locked) { 12698d220203SAlan Cox vm_pagequeue_unlock(pq); 127048cc2fc7SKonstantin Belousov queues_locked = FALSE; 127148cc2fc7SKonstantin Belousov } 127214137dc0SAlan Cox if (vp != NULL) 1273f6b04d2bSDavid Greenman vput(vp); 127414137dc0SAlan Cox vm_object_deallocate(object); 1275f2a2857bSKirk McKusick vn_finished_write(mp); 127648cc2fc7SKonstantin Belousov } 127748cc2fc7SKonstantin Belousov vm_page_lock_assert(m, MA_NOTOWNED); 127848cc2fc7SKonstantin Belousov goto relock_queues; 127948cc2fc7SKonstantin Belousov } 128048cc2fc7SKonstantin Belousov vm_page_unlock(m); 128189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 128248cc2fc7SKonstantin Belousov relock_queues: 128348cc2fc7SKonstantin Belousov if (!queues_locked) { 12848d220203SAlan Cox vm_pagequeue_lock(pq); 128548cc2fc7SKonstantin Belousov queues_locked = TRUE; 12866989c456SAlan Cox } 1287c325e866SKonstantin Belousov next = TAILQ_NEXT(&vmd->vmd_marker, plinks.q); 1288c325e866SKonstantin Belousov TAILQ_REMOVE(&pq->pq_pl, &vmd->vmd_marker, plinks.q); 12890d94caffSDavid Greenman } 12908d220203SAlan Cox vm_pagequeue_unlock(pq); 129126f9a767SRodney W. Grimes 1292df8bae1dSRodney W. Grimes /* 1293936524aaSMatthew Dillon * Compute the number of pages we want to try to move from the 1294936524aaSMatthew Dillon * active queue to the inactive queue. 12951c7c3c6aSMatthew Dillon */ 1296114f62c6SJeff Roberson pq = &vmd->vmd_pagequeues[PQ_ACTIVE]; 1297114f62c6SJeff Roberson vm_pagequeue_lock(pq); 1298d9e23210SJeff Roberson pcount = pq->pq_cnt; 12992feb50bfSAttilio Rao page_shortage = vm_paging_target() + 13002feb50bfSAttilio Rao cnt.v_inactive_target - cnt.v_inactive_count; 1301b182ec9eSJohn Dyson page_shortage += addl_page_shortage; 1302d9e23210SJeff Roberson /* 1303d9e23210SJeff Roberson * If we're just idle polling attempt to visit every 1304d9e23210SJeff Roberson * active page within 'update_period' seconds. 1305d9e23210SJeff Roberson */ 1306d9e23210SJeff Roberson if (pass == 0 && vm_pageout_update_period != 0) { 1307d9e23210SJeff Roberson pcount /= vm_pageout_update_period; 1308d9e23210SJeff Roberson page_shortage = pcount; 1309d9e23210SJeff Roberson } 13101c7c3c6aSMatthew Dillon 13111c7c3c6aSMatthew Dillon /* 1312936524aaSMatthew Dillon * Scan the active queue for things we can deactivate. We nominally 1313936524aaSMatthew Dillon * track the per-page activity counter and use it to locate 1314936524aaSMatthew Dillon * deactivation candidates. 13151c7c3c6aSMatthew Dillon */ 13168d220203SAlan Cox m = TAILQ_FIRST(&pq->pq_pl); 1317b18bfc3dSJohn Dyson while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) { 1318f35329acSJohn Dyson 13199cf51988SAlan Cox KASSERT(m->queue == PQ_ACTIVE, 1320d3c09dd7SAlan Cox ("vm_pageout_scan: page %p isn't active", m)); 1321f35329acSJohn Dyson 1322c325e866SKonstantin Belousov next = TAILQ_NEXT(m, plinks.q); 13238dbca793STor Egge if ((m->flags & PG_MARKER) != 0) { 13248dbca793STor Egge m = next; 13258dbca793STor Egge continue; 13268dbca793STor Egge } 13277900f95dSKonstantin Belousov KASSERT((m->flags & PG_FICTITIOUS) == 0, 13287900f95dSKonstantin Belousov ("Fictitious page %p cannot be in active queue", m)); 13297900f95dSKonstantin Belousov KASSERT((m->oflags & VPO_UNMANAGED) == 0, 13307900f95dSKonstantin Belousov ("Unmanaged page %p cannot be in active queue", m)); 13319ee2165fSAlan Cox if (!vm_pageout_page_lock(m, &next)) { 13328c616246SKonstantin Belousov vm_page_unlock(m); 13332965a453SKip Macy m = next; 13342965a453SKip Macy continue; 13352965a453SKip Macy } 13369ee2165fSAlan Cox object = m->object; 133789f6b863SAttilio Rao if (!VM_OBJECT_TRYWLOCK(object) && 13388dbca793STor Egge !vm_pageout_fallback_object_lock(m, &next)) { 133989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 13402965a453SKip Macy vm_page_unlock(m); 13414b8a5c40SAlan Cox m = next; 1342b08abf6cSAlan Cox continue; 1343b08abf6cSAlan Cox } 1344b08abf6cSAlan Cox 1345df8bae1dSRodney W. Grimes /* 134626f9a767SRodney W. Grimes * Don't deactivate pages that are busy. 1347df8bae1dSRodney W. Grimes */ 1348c7aebda8SAttilio Rao if (vm_page_busied(m) || m->hold_count != 0) { 13492965a453SKip Macy vm_page_unlock(m); 135089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 13518d220203SAlan Cox vm_page_requeue_locked(m); 135226f9a767SRodney W. Grimes m = next; 135326f9a767SRodney W. Grimes continue; 1354df8bae1dSRodney W. Grimes } 1355b18bfc3dSJohn Dyson 1356b18bfc3dSJohn Dyson /* 1357b18bfc3dSJohn Dyson * The count for pagedaemon pages is done after checking the 1358956f3135SPhilippe Charnier * page for eligibility... 1359b18bfc3dSJohn Dyson */ 13608d220203SAlan Cox PCPU_INC(cnt.v_pdpages); 1361ef743ce6SJohn Dyson 13627e006499SJohn Dyson /* 13637e006499SJohn Dyson * Check to see "how much" the page has been used. 13647e006499SJohn Dyson */ 1365bb7858eaSJeff Roberson act_delta = 0; 13663407fefeSKonstantin Belousov if (m->aflags & PGA_REFERENCED) { 1367bb7858eaSJeff Roberson vm_page_aflag_clear(m, PGA_REFERENCED); 1368bb7858eaSJeff Roberson act_delta += 1; 13690d94caffSDavid Greenman } 1370bb7858eaSJeff Roberson if (object->ref_count != 0) 1371bb7858eaSJeff Roberson act_delta += pmap_ts_referenced(m); 1372bb7858eaSJeff Roberson 1373bb7858eaSJeff Roberson /* 1374bb7858eaSJeff Roberson * Advance or decay the act_count based on recent usage. 1375bb7858eaSJeff Roberson */ 1376bb7858eaSJeff Roberson if (act_delta) { 1377bb7858eaSJeff Roberson m->act_count += ACT_ADVANCE + act_delta; 137838efa82bSJohn Dyson if (m->act_count > ACT_MAX) 137938efa82bSJohn Dyson m->act_count = ACT_MAX; 1380bb7858eaSJeff Roberson } else { 138138efa82bSJohn Dyson m->act_count -= min(m->act_count, ACT_DECLINE); 1382bb7858eaSJeff Roberson act_delta = m->act_count; 1383bb7858eaSJeff Roberson } 1384bb7858eaSJeff Roberson 1385bb7858eaSJeff Roberson /* 1386bb7858eaSJeff Roberson * Move this page to the tail of the active or inactive 1387bb7858eaSJeff Roberson * queue depending on usage. 1388bb7858eaSJeff Roberson */ 1389bb7858eaSJeff Roberson if (act_delta == 0) { 1390bb7858eaSJeff Roberson KASSERT(object->ref_count != 0 || 1391bb7858eaSJeff Roberson !pmap_page_is_mapped(m), 1392bb7858eaSJeff Roberson ("vm_pageout_scan: page %p is mapped", m)); 13938d220203SAlan Cox /* Dequeue to avoid later lock recursion. */ 13948d220203SAlan Cox vm_page_dequeue_locked(m); 1395d4a272dbSJohn Dyson vm_page_deactivate(m); 1396bb7858eaSJeff Roberson page_shortage--; 13978d220203SAlan Cox } else 13988d220203SAlan Cox vm_page_requeue_locked(m); 13992965a453SKip Macy vm_page_unlock(m); 140089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 140126f9a767SRodney W. Grimes m = next; 140226f9a767SRodney W. Grimes } 14038d220203SAlan Cox vm_pagequeue_unlock(pq); 1404ceb0cf87SJohn Dyson #if !defined(NO_SWAPPING) 1405ceb0cf87SJohn Dyson /* 1406ceb0cf87SJohn Dyson * Idle process swapout -- run once per second. 1407ceb0cf87SJohn Dyson */ 1408ceb0cf87SJohn Dyson if (vm_swap_idle_enabled) { 1409ceb0cf87SJohn Dyson static long lsec; 1410227ee8a1SPoul-Henning Kamp if (time_second != lsec) { 141197824da3SAlan Cox vm_req_vmdaemon(VM_SWAP_IDLE); 1412227ee8a1SPoul-Henning Kamp lsec = time_second; 1413ceb0cf87SJohn Dyson } 1414ceb0cf87SJohn Dyson } 1415ceb0cf87SJohn Dyson #endif 1416ceb0cf87SJohn Dyson 14175663e6deSDavid Greenman /* 1418f6b04d2bSDavid Greenman * If we didn't get enough free pages, and we have skipped a vnode 14194c1f8ee9SDavid Greenman * in a writeable object, wakeup the sync daemon. And kick swapout 14204c1f8ee9SDavid Greenman * if we did not get enough free pages. 1421f6b04d2bSDavid Greenman */ 142290ecac61SMatthew Dillon if (vm_paging_target() > 0) { 142390ecac61SMatthew Dillon if (vnodes_skipped && vm_page_count_min()) 1424d50c1994SPeter Wemm (void) speedup_syncer(); 142538efa82bSJohn Dyson #if !defined(NO_SWAPPING) 142697824da3SAlan Cox if (vm_swap_enabled && vm_page_count_target()) 142797824da3SAlan Cox vm_req_vmdaemon(VM_SWAP_NORMAL); 14285afce282SDavid Greenman #endif 14294c1f8ee9SDavid Greenman } 14304c1f8ee9SDavid Greenman 1431f6b04d2bSDavid Greenman /* 1432e92686d0SDavid Schultz * If we are critically low on one of RAM or swap and low on 1433e92686d0SDavid Schultz * the other, kill the largest process. However, we avoid 1434e92686d0SDavid Schultz * doing this on the first pass in order to give ourselves a 1435e92686d0SDavid Schultz * chance to flush out dirty vnode-backed pages and to allow 1436e92686d0SDavid Schultz * active pages to be moved to the inactive queue and reclaimed. 14372025d69bSKonstantin Belousov */ 1438449c2e92SKonstantin Belousov vm_pageout_mightbe_oom(vmd, pass); 14392025d69bSKonstantin Belousov } 14402025d69bSKonstantin Belousov 1441449c2e92SKonstantin Belousov static int vm_pageout_oom_vote; 1442449c2e92SKonstantin Belousov 1443449c2e92SKonstantin Belousov /* 1444449c2e92SKonstantin Belousov * The pagedaemon threads randlomly select one to perform the 1445449c2e92SKonstantin Belousov * OOM. Trying to kill processes before all pagedaemons 1446449c2e92SKonstantin Belousov * failed to reach free target is premature. 1447449c2e92SKonstantin Belousov */ 1448449c2e92SKonstantin Belousov static void 1449449c2e92SKonstantin Belousov vm_pageout_mightbe_oom(struct vm_domain *vmd, int pass) 1450449c2e92SKonstantin Belousov { 1451449c2e92SKonstantin Belousov int old_vote; 1452449c2e92SKonstantin Belousov 1453d9e23210SJeff Roberson if (pass <= 1 || !((swap_pager_avail < 64 && vm_page_count_min()) || 1454449c2e92SKonstantin Belousov (swap_pager_full && vm_paging_target() > 0))) { 1455449c2e92SKonstantin Belousov if (vmd->vmd_oom) { 1456449c2e92SKonstantin Belousov vmd->vmd_oom = FALSE; 1457449c2e92SKonstantin Belousov atomic_subtract_int(&vm_pageout_oom_vote, 1); 1458449c2e92SKonstantin Belousov } 1459449c2e92SKonstantin Belousov return; 1460449c2e92SKonstantin Belousov } 1461449c2e92SKonstantin Belousov 1462449c2e92SKonstantin Belousov if (vmd->vmd_oom) 1463449c2e92SKonstantin Belousov return; 1464449c2e92SKonstantin Belousov 1465449c2e92SKonstantin Belousov vmd->vmd_oom = TRUE; 1466449c2e92SKonstantin Belousov old_vote = atomic_fetchadd_int(&vm_pageout_oom_vote, 1); 1467449c2e92SKonstantin Belousov if (old_vote != vm_ndomains - 1) 1468449c2e92SKonstantin Belousov return; 1469449c2e92SKonstantin Belousov 1470449c2e92SKonstantin Belousov /* 1471449c2e92SKonstantin Belousov * The current pagedaemon thread is the last in the quorum to 1472449c2e92SKonstantin Belousov * start OOM. Initiate the selection and signaling of the 1473449c2e92SKonstantin Belousov * victim. 1474449c2e92SKonstantin Belousov */ 1475449c2e92SKonstantin Belousov vm_pageout_oom(VM_OOM_MEM); 1476449c2e92SKonstantin Belousov 1477449c2e92SKonstantin Belousov /* 1478449c2e92SKonstantin Belousov * After one round of OOM terror, recall our vote. On the 1479449c2e92SKonstantin Belousov * next pass, current pagedaemon would vote again if the low 1480449c2e92SKonstantin Belousov * memory condition is still there, due to vmd_oom being 1481449c2e92SKonstantin Belousov * false. 1482449c2e92SKonstantin Belousov */ 1483449c2e92SKonstantin Belousov vmd->vmd_oom = FALSE; 1484449c2e92SKonstantin Belousov atomic_subtract_int(&vm_pageout_oom_vote, 1); 1485449c2e92SKonstantin Belousov } 14862025d69bSKonstantin Belousov 14872025d69bSKonstantin Belousov void 14882025d69bSKonstantin Belousov vm_pageout_oom(int shortage) 14892025d69bSKonstantin Belousov { 14902025d69bSKonstantin Belousov struct proc *p, *bigproc; 14912025d69bSKonstantin Belousov vm_offset_t size, bigsize; 14922025d69bSKonstantin Belousov struct thread *td; 14936bed074cSKonstantin Belousov struct vmspace *vm; 14942025d69bSKonstantin Belousov 14952025d69bSKonstantin Belousov /* 14961c58e4e5SJohn Baldwin * We keep the process bigproc locked once we find it to keep anyone 14971c58e4e5SJohn Baldwin * from messing with it; however, there is a possibility of 14981c58e4e5SJohn Baldwin * deadlock if process B is bigproc and one of it's child processes 14991c58e4e5SJohn Baldwin * attempts to propagate a signal to B while we are waiting for A's 15001c58e4e5SJohn Baldwin * lock while walking this list. To avoid this, we don't block on 15011c58e4e5SJohn Baldwin * the process lock but just skip a process if it is already locked. 15025663e6deSDavid Greenman */ 15035663e6deSDavid Greenman bigproc = NULL; 15045663e6deSDavid Greenman bigsize = 0; 15051005a129SJohn Baldwin sx_slock(&allproc_lock); 1506e602ba25SJulian Elischer FOREACH_PROC_IN_SYSTEM(p) { 1507e602ba25SJulian Elischer int breakout; 1508dcbcd518SBruce Evans 15091c58e4e5SJohn Baldwin if (PROC_TRYLOCK(p) == 0) 15101c58e4e5SJohn Baldwin continue; 15111c58e4e5SJohn Baldwin /* 15123f1c4c4fSKonstantin Belousov * If this is a system, protected or killed process, skip it. 15135663e6deSDavid Greenman */ 15148e6fa660SJohn Baldwin if (p->p_state != PRS_NORMAL || 15158e6fa660SJohn Baldwin (p->p_flag & (P_INEXEC | P_PROTECTED | P_SYSTEM)) || 15163f1c4c4fSKonstantin Belousov (p->p_pid == 1) || P_KILLED(p) || 15178f60c087SPoul-Henning Kamp ((p->p_pid < 48) && (swap_pager_avail != 0))) { 15188606d880SJohn Baldwin PROC_UNLOCK(p); 15195663e6deSDavid Greenman continue; 15205663e6deSDavid Greenman } 15215663e6deSDavid Greenman /* 1522dcbcd518SBruce Evans * If the process is in a non-running type state, 1523e602ba25SJulian Elischer * don't touch it. Check all the threads individually. 15245663e6deSDavid Greenman */ 1525e602ba25SJulian Elischer breakout = 0; 1526e602ba25SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 1527982d11f8SJeff Roberson thread_lock(td); 152871fad9fdSJulian Elischer if (!TD_ON_RUNQ(td) && 152971fad9fdSJulian Elischer !TD_IS_RUNNING(td) && 1530f497cda2SEdward Tomasz Napierala !TD_IS_SLEEPING(td) && 1531f497cda2SEdward Tomasz Napierala !TD_IS_SUSPENDED(td)) { 1532982d11f8SJeff Roberson thread_unlock(td); 1533e602ba25SJulian Elischer breakout = 1; 1534e602ba25SJulian Elischer break; 1535e602ba25SJulian Elischer } 1536982d11f8SJeff Roberson thread_unlock(td); 1537e602ba25SJulian Elischer } 1538e602ba25SJulian Elischer if (breakout) { 15391c58e4e5SJohn Baldwin PROC_UNLOCK(p); 15405663e6deSDavid Greenman continue; 15415663e6deSDavid Greenman } 15425663e6deSDavid Greenman /* 15435663e6deSDavid Greenman * get the process size 15445663e6deSDavid Greenman */ 15456bed074cSKonstantin Belousov vm = vmspace_acquire_ref(p); 15466bed074cSKonstantin Belousov if (vm == NULL) { 15476bed074cSKonstantin Belousov PROC_UNLOCK(p); 15486bed074cSKonstantin Belousov continue; 15496bed074cSKonstantin Belousov } 15506bed074cSKonstantin Belousov if (!vm_map_trylock_read(&vm->vm_map)) { 15516bed074cSKonstantin Belousov vmspace_free(vm); 155272d97679SDavid Schultz PROC_UNLOCK(p); 155372d97679SDavid Schultz continue; 155472d97679SDavid Schultz } 15557981aa24SKonstantin Belousov size = vmspace_swap_count(vm); 15566bed074cSKonstantin Belousov vm_map_unlock_read(&vm->vm_map); 15572025d69bSKonstantin Belousov if (shortage == VM_OOM_MEM) 15586bed074cSKonstantin Belousov size += vmspace_resident_count(vm); 15596bed074cSKonstantin Belousov vmspace_free(vm); 15605663e6deSDavid Greenman /* 15615663e6deSDavid Greenman * if the this process is bigger than the biggest one 15625663e6deSDavid Greenman * remember it. 15635663e6deSDavid Greenman */ 15645663e6deSDavid Greenman if (size > bigsize) { 15651c58e4e5SJohn Baldwin if (bigproc != NULL) 15661c58e4e5SJohn Baldwin PROC_UNLOCK(bigproc); 15675663e6deSDavid Greenman bigproc = p; 15685663e6deSDavid Greenman bigsize = size; 15691c58e4e5SJohn Baldwin } else 15701c58e4e5SJohn Baldwin PROC_UNLOCK(p); 15715663e6deSDavid Greenman } 15721005a129SJohn Baldwin sx_sunlock(&allproc_lock); 15735663e6deSDavid Greenman if (bigproc != NULL) { 1574729b1e51SDavid Greenman killproc(bigproc, "out of swap space"); 1575fa885116SJulian Elischer sched_nice(bigproc, PRIO_MIN); 15761c58e4e5SJohn Baldwin PROC_UNLOCK(bigproc); 15772feb50bfSAttilio Rao wakeup(&cnt.v_free_count); 15785663e6deSDavid Greenman } 15795663e6deSDavid Greenman } 158026f9a767SRodney W. Grimes 1581449c2e92SKonstantin Belousov static void 1582449c2e92SKonstantin Belousov vm_pageout_worker(void *arg) 1583449c2e92SKonstantin Belousov { 1584449c2e92SKonstantin Belousov struct vm_domain *domain; 1585949c9186SKonstantin Belousov int domidx; 1586449c2e92SKonstantin Belousov 1587449c2e92SKonstantin Belousov domidx = (uintptr_t)arg; 1588449c2e92SKonstantin Belousov domain = &vm_dom[domidx]; 1589449c2e92SKonstantin Belousov 1590449c2e92SKonstantin Belousov /* 1591949c9186SKonstantin Belousov * XXXKIB It could be useful to bind pageout daemon threads to 1592949c9186SKonstantin Belousov * the cores belonging to the domain, from which vm_page_array 1593949c9186SKonstantin Belousov * is allocated. 1594449c2e92SKonstantin Belousov */ 1595449c2e92SKonstantin Belousov 1596449c2e92SKonstantin Belousov KASSERT(domain->vmd_segs != 0, ("domain without segments")); 1597449c2e92SKonstantin Belousov vm_pageout_init_marker(&domain->vmd_marker, PQ_INACTIVE); 1598449c2e92SKonstantin Belousov 1599449c2e92SKonstantin Belousov /* 1600449c2e92SKonstantin Belousov * The pageout daemon worker is never done, so loop forever. 1601449c2e92SKonstantin Belousov */ 1602449c2e92SKonstantin Belousov while (TRUE) { 1603449c2e92SKonstantin Belousov /* 1604449c2e92SKonstantin Belousov * If we have enough free memory, wakeup waiters. Do 1605449c2e92SKonstantin Belousov * not clear vm_pages_needed until we reach our target, 1606449c2e92SKonstantin Belousov * otherwise we may be woken up over and over again and 1607449c2e92SKonstantin Belousov * waste a lot of cpu. 1608449c2e92SKonstantin Belousov */ 1609449c2e92SKonstantin Belousov mtx_lock(&vm_page_queue_free_mtx); 1610449c2e92SKonstantin Belousov if (vm_pages_needed && !vm_page_count_min()) { 1611449c2e92SKonstantin Belousov if (!vm_paging_needed()) 1612449c2e92SKonstantin Belousov vm_pages_needed = 0; 1613449c2e92SKonstantin Belousov wakeup(&cnt.v_free_count); 1614449c2e92SKonstantin Belousov } 1615449c2e92SKonstantin Belousov if (vm_pages_needed) { 1616449c2e92SKonstantin Belousov /* 1617449c2e92SKonstantin Belousov * Still not done, take a second pass without waiting 1618449c2e92SKonstantin Belousov * (unlimited dirty cleaning), otherwise sleep a bit 1619449c2e92SKonstantin Belousov * and try again. 1620449c2e92SKonstantin Belousov */ 1621449c2e92SKonstantin Belousov if (domain->vmd_pass > 1) 1622449c2e92SKonstantin Belousov msleep(&vm_pages_needed, 1623449c2e92SKonstantin Belousov &vm_page_queue_free_mtx, PVM, "psleep", 1624449c2e92SKonstantin Belousov hz / 2); 1625449c2e92SKonstantin Belousov } else { 1626449c2e92SKonstantin Belousov /* 1627d9e23210SJeff Roberson * Good enough, sleep until required to refresh 1628d9e23210SJeff Roberson * stats. 1629449c2e92SKonstantin Belousov */ 1630449c2e92SKonstantin Belousov domain->vmd_pass = 0; 1631d9e23210SJeff Roberson msleep(&vm_pages_needed, &vm_page_queue_free_mtx, 1632d9e23210SJeff Roberson PVM, "psleep", hz); 1633d9e23210SJeff Roberson 1634449c2e92SKonstantin Belousov } 1635d9e23210SJeff Roberson if (vm_pages_needed) { 1636449c2e92SKonstantin Belousov cnt.v_pdwakeups++; 1637d9e23210SJeff Roberson domain->vmd_pass++; 1638d9e23210SJeff Roberson } 1639449c2e92SKonstantin Belousov mtx_unlock(&vm_page_queue_free_mtx); 1640449c2e92SKonstantin Belousov vm_pageout_scan(domain, domain->vmd_pass); 1641449c2e92SKonstantin Belousov } 1642449c2e92SKonstantin Belousov } 1643449c2e92SKonstantin Belousov 1644df8bae1dSRodney W. Grimes /* 1645df8bae1dSRodney W. Grimes * vm_pageout is the high level pageout daemon. 1646df8bae1dSRodney W. Grimes */ 16472b14f991SJulian Elischer static void 16484a365329SAndrey Zonov vm_pageout(void) 1649df8bae1dSRodney W. Grimes { 1650449c2e92SKonstantin Belousov #if MAXMEMDOM > 1 1651449c2e92SKonstantin Belousov int error, i; 1652449c2e92SKonstantin Belousov #endif 16530384fff8SJason Evans 1654df8bae1dSRodney W. Grimes /* 1655df8bae1dSRodney W. Grimes * Initialize some paging parameters. 1656df8bae1dSRodney W. Grimes */ 16572feb50bfSAttilio Rao cnt.v_interrupt_free_min = 2; 16582feb50bfSAttilio Rao if (cnt.v_page_count < 2000) 1659f35329acSJohn Dyson vm_pageout_page_count = 8; 1660f6b04d2bSDavid Greenman 166145ae1d91SAlan Cox /* 166245ae1d91SAlan Cox * v_free_reserved needs to include enough for the largest 166345ae1d91SAlan Cox * swap pager structures plus enough for any pv_entry structs 166445ae1d91SAlan Cox * when paging. 166545ae1d91SAlan Cox */ 16662feb50bfSAttilio Rao if (cnt.v_page_count > 1024) 16672feb50bfSAttilio Rao cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200; 16682feb50bfSAttilio Rao else 16692feb50bfSAttilio Rao cnt.v_free_min = 4; 16702feb50bfSAttilio Rao cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE + 16712feb50bfSAttilio Rao cnt.v_interrupt_free_min; 16722feb50bfSAttilio Rao cnt.v_free_reserved = vm_pageout_page_count + 16732446e4f0SAlan Cox cnt.v_pageout_free_min + (cnt.v_page_count / 768); 16742feb50bfSAttilio Rao cnt.v_free_severe = cnt.v_free_min / 2; 1675d9e23210SJeff Roberson cnt.v_free_target = 4 * cnt.v_free_min + cnt.v_free_reserved; 16762feb50bfSAttilio Rao cnt.v_free_min += cnt.v_free_reserved; 16772feb50bfSAttilio Rao cnt.v_free_severe += cnt.v_free_reserved; 16782feb50bfSAttilio Rao cnt.v_inactive_target = (3 * cnt.v_free_target) / 2; 16792feb50bfSAttilio Rao if (cnt.v_inactive_target > cnt.v_free_count / 3) 16802feb50bfSAttilio Rao cnt.v_inactive_target = cnt.v_free_count / 3; 1681df8bae1dSRodney W. Grimes 1682d9e23210SJeff Roberson /* 1683d9e23210SJeff Roberson * Set the default wakeup threshold to be 10% above the minimum 1684d9e23210SJeff Roberson * page limit. This keeps the steady state out of shortfall. 1685d9e23210SJeff Roberson */ 1686d9e23210SJeff Roberson vm_pageout_wakeup_thresh = (cnt.v_free_min / 10) * 11; 1687d9e23210SJeff Roberson 1688d9e23210SJeff Roberson /* 1689d9e23210SJeff Roberson * Set interval in seconds for active scan. We want to visit each 1690*c9612b2dSJeff Roberson * page at least once every ten minutes. This is to prevent worst 1691*c9612b2dSJeff Roberson * case paging behaviors with stale active LRU. 1692d9e23210SJeff Roberson */ 1693d9e23210SJeff Roberson if (vm_pageout_update_period == 0) 1694*c9612b2dSJeff Roberson vm_pageout_update_period = 600; 1695d9e23210SJeff Roberson 1696df8bae1dSRodney W. Grimes /* XXX does not really belong here */ 1697df8bae1dSRodney W. Grimes if (vm_page_max_wired == 0) 16982feb50bfSAttilio Rao vm_page_max_wired = cnt.v_free_count / 3; 1699df8bae1dSRodney W. Grimes 170024a1cce3SDavid Greenman swap_pager_swap_init(); 1701449c2e92SKonstantin Belousov #if MAXMEMDOM > 1 1702449c2e92SKonstantin Belousov for (i = 1; i < vm_ndomains; i++) { 1703449c2e92SKonstantin Belousov error = kthread_add(vm_pageout_worker, (void *)(uintptr_t)i, 1704449c2e92SKonstantin Belousov curproc, NULL, 0, 0, "dom%d", i); 1705449c2e92SKonstantin Belousov if (error != 0) { 1706449c2e92SKonstantin Belousov panic("starting pageout for domain %d, error %d\n", 1707449c2e92SKonstantin Belousov i, error); 1708dc2efb27SJohn Dyson } 1709f919ebdeSDavid Greenman } 1710449c2e92SKonstantin Belousov #endif 1711449c2e92SKonstantin Belousov vm_pageout_worker((uintptr_t)0); 1712df8bae1dSRodney W. Grimes } 171326f9a767SRodney W. Grimes 17146b4b77adSAlan Cox /* 1715e9f995d8SAlan Cox * Unless the free page queue lock is held by the caller, this function 17166b4b77adSAlan Cox * should be regarded as advisory. Specifically, the caller should 17176b4b77adSAlan Cox * not msleep() on &cnt.v_free_count following this function unless 1718e9f995d8SAlan Cox * the free page queue lock is held until the msleep() is performed. 17196b4b77adSAlan Cox */ 1720e0c5a895SJohn Dyson void 17214a365329SAndrey Zonov pagedaemon_wakeup(void) 1722e0c5a895SJohn Dyson { 1723a1c0a785SAlan Cox 1724b40ce416SJulian Elischer if (!vm_pages_needed && curthread->td_proc != pageproc) { 1725a1c0a785SAlan Cox vm_pages_needed = 1; 1726e0c5a895SJohn Dyson wakeup(&vm_pages_needed); 1727e0c5a895SJohn Dyson } 1728e0c5a895SJohn Dyson } 1729e0c5a895SJohn Dyson 173038efa82bSJohn Dyson #if !defined(NO_SWAPPING) 17315afce282SDavid Greenman static void 173297824da3SAlan Cox vm_req_vmdaemon(int req) 17335afce282SDavid Greenman { 17345afce282SDavid Greenman static int lastrun = 0; 17355afce282SDavid Greenman 173697824da3SAlan Cox mtx_lock(&vm_daemon_mtx); 173797824da3SAlan Cox vm_pageout_req_swapout |= req; 1738b18bfc3dSJohn Dyson if ((ticks > (lastrun + hz)) || (ticks < lastrun)) { 17395afce282SDavid Greenman wakeup(&vm_daemon_needed); 17405afce282SDavid Greenman lastrun = ticks; 17415afce282SDavid Greenman } 174297824da3SAlan Cox mtx_unlock(&vm_daemon_mtx); 17435afce282SDavid Greenman } 17445afce282SDavid Greenman 17452b14f991SJulian Elischer static void 17464a365329SAndrey Zonov vm_daemon(void) 17470d94caffSDavid Greenman { 174891d5354aSJohn Baldwin struct rlimit rsslim; 1749dcbcd518SBruce Evans struct proc *p; 1750dcbcd518SBruce Evans struct thread *td; 17516bed074cSKonstantin Belousov struct vmspace *vm; 1752099e7e95SEdward Tomasz Napierala int breakout, swapout_flags, tryagain, attempts; 1753afcc55f3SEdward Tomasz Napierala #ifdef RACCT 1754099e7e95SEdward Tomasz Napierala uint64_t rsize, ravailable; 1755afcc55f3SEdward Tomasz Napierala #endif 17560d94caffSDavid Greenman 17572fe6e4d7SDavid Greenman while (TRUE) { 175897824da3SAlan Cox mtx_lock(&vm_daemon_mtx); 1759099e7e95SEdward Tomasz Napierala #ifdef RACCT 1760099e7e95SEdward Tomasz Napierala msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", hz); 1761099e7e95SEdward Tomasz Napierala #else 176297824da3SAlan Cox msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", 0); 1763099e7e95SEdward Tomasz Napierala #endif 176497824da3SAlan Cox swapout_flags = vm_pageout_req_swapout; 17654c1f8ee9SDavid Greenman vm_pageout_req_swapout = 0; 176697824da3SAlan Cox mtx_unlock(&vm_daemon_mtx); 176797824da3SAlan Cox if (swapout_flags) 176897824da3SAlan Cox swapout_procs(swapout_flags); 176997824da3SAlan Cox 17702fe6e4d7SDavid Greenman /* 17710d94caffSDavid Greenman * scan the processes for exceeding their rlimits or if 17720d94caffSDavid Greenman * process is swapped out -- deactivate pages 17732fe6e4d7SDavid Greenman */ 1774099e7e95SEdward Tomasz Napierala tryagain = 0; 1775099e7e95SEdward Tomasz Napierala attempts = 0; 1776099e7e95SEdward Tomasz Napierala again: 1777099e7e95SEdward Tomasz Napierala attempts++; 17781005a129SJohn Baldwin sx_slock(&allproc_lock); 1779f67af5c9SXin LI FOREACH_PROC_IN_SYSTEM(p) { 1780fe2144fdSLuoqi Chen vm_pindex_t limit, size; 17812fe6e4d7SDavid Greenman 17822fe6e4d7SDavid Greenman /* 17832fe6e4d7SDavid Greenman * if this is a system process or if we have already 17842fe6e4d7SDavid Greenman * looked at this process, skip it. 17852fe6e4d7SDavid Greenman */ 1786897ecacdSJohn Baldwin PROC_LOCK(p); 17878e6fa660SJohn Baldwin if (p->p_state != PRS_NORMAL || 17888e6fa660SJohn Baldwin p->p_flag & (P_INEXEC | P_SYSTEM | P_WEXIT)) { 1789897ecacdSJohn Baldwin PROC_UNLOCK(p); 17902fe6e4d7SDavid Greenman continue; 17912fe6e4d7SDavid Greenman } 17922fe6e4d7SDavid Greenman /* 17932fe6e4d7SDavid Greenman * if the process is in a non-running type state, 17942fe6e4d7SDavid Greenman * don't touch it. 17952fe6e4d7SDavid Greenman */ 1796e602ba25SJulian Elischer breakout = 0; 1797e602ba25SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 1798982d11f8SJeff Roberson thread_lock(td); 179971fad9fdSJulian Elischer if (!TD_ON_RUNQ(td) && 180071fad9fdSJulian Elischer !TD_IS_RUNNING(td) && 1801f497cda2SEdward Tomasz Napierala !TD_IS_SLEEPING(td) && 1802f497cda2SEdward Tomasz Napierala !TD_IS_SUSPENDED(td)) { 1803982d11f8SJeff Roberson thread_unlock(td); 1804e602ba25SJulian Elischer breakout = 1; 1805e602ba25SJulian Elischer break; 1806e602ba25SJulian Elischer } 1807982d11f8SJeff Roberson thread_unlock(td); 1808e602ba25SJulian Elischer } 1809897ecacdSJohn Baldwin if (breakout) { 1810897ecacdSJohn Baldwin PROC_UNLOCK(p); 18112fe6e4d7SDavid Greenman continue; 18122fe6e4d7SDavid Greenman } 18132fe6e4d7SDavid Greenman /* 18142fe6e4d7SDavid Greenman * get a limit 18152fe6e4d7SDavid Greenman */ 1816dcbcd518SBruce Evans lim_rlimit(p, RLIMIT_RSS, &rsslim); 1817fe2144fdSLuoqi Chen limit = OFF_TO_IDX( 181891d5354aSJohn Baldwin qmin(rsslim.rlim_cur, rsslim.rlim_max)); 18192fe6e4d7SDavid Greenman 18202fe6e4d7SDavid Greenman /* 18210d94caffSDavid Greenman * let processes that are swapped out really be 18220d94caffSDavid Greenman * swapped out set the limit to nothing (will force a 18230d94caffSDavid Greenman * swap-out.) 18242fe6e4d7SDavid Greenman */ 1825b61ce5b0SJeff Roberson if ((p->p_flag & P_INMEM) == 0) 18260d94caffSDavid Greenman limit = 0; /* XXX */ 18276bed074cSKonstantin Belousov vm = vmspace_acquire_ref(p); 1828897ecacdSJohn Baldwin PROC_UNLOCK(p); 18296bed074cSKonstantin Belousov if (vm == NULL) 18306bed074cSKonstantin Belousov continue; 18312fe6e4d7SDavid Greenman 18326bed074cSKonstantin Belousov size = vmspace_resident_count(vm); 1833a406d8c3SEdward Tomasz Napierala if (size >= limit) { 1834fe2144fdSLuoqi Chen vm_pageout_map_deactivate_pages( 18356bed074cSKonstantin Belousov &vm->vm_map, limit); 18362fe6e4d7SDavid Greenman } 1837afcc55f3SEdward Tomasz Napierala #ifdef RACCT 1838099e7e95SEdward Tomasz Napierala rsize = IDX_TO_OFF(size); 1839099e7e95SEdward Tomasz Napierala PROC_LOCK(p); 1840099e7e95SEdward Tomasz Napierala racct_set(p, RACCT_RSS, rsize); 1841099e7e95SEdward Tomasz Napierala ravailable = racct_get_available(p, RACCT_RSS); 1842099e7e95SEdward Tomasz Napierala PROC_UNLOCK(p); 1843099e7e95SEdward Tomasz Napierala if (rsize > ravailable) { 1844099e7e95SEdward Tomasz Napierala /* 1845099e7e95SEdward Tomasz Napierala * Don't be overly aggressive; this might be 1846099e7e95SEdward Tomasz Napierala * an innocent process, and the limit could've 1847099e7e95SEdward Tomasz Napierala * been exceeded by some memory hog. Don't 1848099e7e95SEdward Tomasz Napierala * try to deactivate more than 1/4th of process' 1849099e7e95SEdward Tomasz Napierala * resident set size. 1850099e7e95SEdward Tomasz Napierala */ 1851099e7e95SEdward Tomasz Napierala if (attempts <= 8) { 1852099e7e95SEdward Tomasz Napierala if (ravailable < rsize - (rsize / 4)) 1853099e7e95SEdward Tomasz Napierala ravailable = rsize - (rsize / 4); 1854099e7e95SEdward Tomasz Napierala } 1855099e7e95SEdward Tomasz Napierala vm_pageout_map_deactivate_pages( 1856099e7e95SEdward Tomasz Napierala &vm->vm_map, OFF_TO_IDX(ravailable)); 1857099e7e95SEdward Tomasz Napierala /* Update RSS usage after paging out. */ 1858099e7e95SEdward Tomasz Napierala size = vmspace_resident_count(vm); 1859099e7e95SEdward Tomasz Napierala rsize = IDX_TO_OFF(size); 1860099e7e95SEdward Tomasz Napierala PROC_LOCK(p); 1861099e7e95SEdward Tomasz Napierala racct_set(p, RACCT_RSS, rsize); 1862099e7e95SEdward Tomasz Napierala PROC_UNLOCK(p); 1863099e7e95SEdward Tomasz Napierala if (rsize > ravailable) 1864099e7e95SEdward Tomasz Napierala tryagain = 1; 1865099e7e95SEdward Tomasz Napierala } 1866afcc55f3SEdward Tomasz Napierala #endif 18676bed074cSKonstantin Belousov vmspace_free(vm); 18682fe6e4d7SDavid Greenman } 18691005a129SJohn Baldwin sx_sunlock(&allproc_lock); 1870099e7e95SEdward Tomasz Napierala if (tryagain != 0 && attempts <= 10) 1871099e7e95SEdward Tomasz Napierala goto again; 187224a1cce3SDavid Greenman } 18732fe6e4d7SDavid Greenman } 1874a1287949SEivind Eklund #endif /* !defined(NO_SWAPPING) */ 1875