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; 162c9612b2dSJeff Roberson static int lowmem_period = 10; 163c9612b2dSJeff 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 184c9612b2dSJeff Roberson SYSCTL_INT(_vm, OID_AUTO, lowmem_period, CTLFLAG_RW, &lowmem_period, 0, 185c9612b2dSJeff Roberson "Low memory callback period"); 186c9612b2dSJeff 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; 5731bd7d0b7SKonstantin Belousov int lockmode; 57485eeca35SAlan Cox 5758d220203SAlan Cox vm_pagequeue_lock(pq); 576c325e866SKonstantin Belousov TAILQ_FOREACH_SAFE(m, &pq->pq_pl, plinks.q, next) { 57785eeca35SAlan Cox if ((m->flags & PG_MARKER) != 0) 57885eeca35SAlan Cox continue; 57985eeca35SAlan Cox pa = VM_PAGE_TO_PHYS(m); 58085eeca35SAlan Cox if (pa < low || pa + PAGE_SIZE > high) 58185eeca35SAlan Cox continue; 58285eeca35SAlan Cox if (!vm_pageout_page_lock(m, &next) || m->hold_count != 0) { 58385eeca35SAlan Cox vm_page_unlock(m); 58485eeca35SAlan Cox continue; 58585eeca35SAlan Cox } 58685eeca35SAlan Cox object = m->object; 58789f6b863SAttilio Rao if ((!VM_OBJECT_TRYWLOCK(object) && 58885eeca35SAlan Cox (!vm_pageout_fallback_object_lock(m, &next) || 589c7aebda8SAttilio Rao m->hold_count != 0)) || vm_page_busied(m)) { 59085eeca35SAlan Cox vm_page_unlock(m); 59189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 59285eeca35SAlan Cox continue; 59385eeca35SAlan Cox } 59485eeca35SAlan Cox vm_page_test_dirty(m); 5959fc4739dSAlan Cox if (m->dirty == 0 && object->ref_count != 0) 59685eeca35SAlan Cox pmap_remove_all(m); 59785eeca35SAlan Cox if (m->dirty != 0) { 59885eeca35SAlan Cox vm_page_unlock(m); 59985eeca35SAlan Cox if (tries == 0 || (object->flags & OBJ_DEAD) != 0) { 60089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 60185eeca35SAlan Cox continue; 60285eeca35SAlan Cox } 60385eeca35SAlan Cox if (object->type == OBJT_VNODE) { 6048d220203SAlan Cox vm_pagequeue_unlock(pq); 60585eeca35SAlan Cox vp = object->handle; 60685eeca35SAlan Cox vm_object_reference_locked(object); 60789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 60885eeca35SAlan Cox (void)vn_start_write(vp, &mp, V_WAIT); 6091bd7d0b7SKonstantin Belousov lockmode = MNT_SHARED_WRITES(vp->v_mount) ? 6101bd7d0b7SKonstantin Belousov LK_SHARED : LK_EXCLUSIVE; 6111bd7d0b7SKonstantin Belousov vn_lock(vp, lockmode | LK_RETRY); 61289f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 61385eeca35SAlan Cox vm_object_page_clean(object, 0, 0, OBJPC_SYNC); 61489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 61585eeca35SAlan Cox VOP_UNLOCK(vp, 0); 61685eeca35SAlan Cox vm_object_deallocate(object); 61785eeca35SAlan Cox vn_finished_write(mp); 61885eeca35SAlan Cox return (TRUE); 61985eeca35SAlan Cox } else if (object->type == OBJT_SWAP || 62085eeca35SAlan Cox object->type == OBJT_DEFAULT) { 6218d220203SAlan Cox vm_pagequeue_unlock(pq); 62285eeca35SAlan Cox m_tmp = m; 62385eeca35SAlan Cox vm_pageout_flush(&m_tmp, 1, VM_PAGER_PUT_SYNC, 62485eeca35SAlan Cox 0, NULL, NULL); 62589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 62685eeca35SAlan Cox return (TRUE); 62785eeca35SAlan Cox } 62885eeca35SAlan Cox } else { 6298d220203SAlan Cox /* 6308d220203SAlan Cox * Dequeue here to prevent lock recursion in 6318d220203SAlan Cox * vm_page_cache(). 6328d220203SAlan Cox */ 6338d220203SAlan Cox vm_page_dequeue_locked(m); 63485eeca35SAlan Cox vm_page_cache(m); 63585eeca35SAlan Cox vm_page_unlock(m); 63685eeca35SAlan Cox } 63789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 63885eeca35SAlan Cox } 6398d220203SAlan Cox vm_pagequeue_unlock(pq); 64085eeca35SAlan Cox return (FALSE); 64185eeca35SAlan Cox } 64285eeca35SAlan Cox 64385eeca35SAlan Cox /* 64485eeca35SAlan Cox * Increase the number of cached pages. The specified value, "tries", 64585eeca35SAlan Cox * determines which categories of pages are cached: 64685eeca35SAlan Cox * 64785eeca35SAlan Cox * 0: All clean, inactive pages within the specified physical address range 64885eeca35SAlan Cox * are cached. Will not sleep. 64985eeca35SAlan Cox * 1: The vm_lowmem handlers are called. All inactive pages within 65085eeca35SAlan Cox * the specified physical address range are cached. May sleep. 65185eeca35SAlan Cox * 2: The vm_lowmem handlers are called. All inactive and active pages 65285eeca35SAlan Cox * within the specified physical address range are cached. May sleep. 65385eeca35SAlan Cox */ 65485eeca35SAlan Cox void 65585eeca35SAlan Cox vm_pageout_grow_cache(int tries, vm_paddr_t low, vm_paddr_t high) 65685eeca35SAlan Cox { 657449c2e92SKonstantin Belousov int actl, actmax, inactl, inactmax, dom, initial_dom; 658449c2e92SKonstantin Belousov static int start_dom = 0; 65985eeca35SAlan Cox 66085eeca35SAlan Cox if (tries > 0) { 66185eeca35SAlan Cox /* 66285eeca35SAlan Cox * Decrease registered cache sizes. The vm_lowmem handlers 66385eeca35SAlan Cox * may acquire locks and/or sleep, so they can only be invoked 66485eeca35SAlan Cox * when "tries" is greater than zero. 66585eeca35SAlan Cox */ 66685eeca35SAlan Cox EVENTHANDLER_INVOKE(vm_lowmem, 0); 66785eeca35SAlan Cox 66885eeca35SAlan Cox /* 66985eeca35SAlan Cox * We do this explicitly after the caches have been drained 67085eeca35SAlan Cox * above. 67185eeca35SAlan Cox */ 67285eeca35SAlan Cox uma_reclaim(); 67385eeca35SAlan Cox } 674449c2e92SKonstantin Belousov 675449c2e92SKonstantin Belousov /* 676449c2e92SKonstantin Belousov * Make the next scan start on the next domain. 677449c2e92SKonstantin Belousov */ 678449c2e92SKonstantin Belousov initial_dom = atomic_fetchadd_int(&start_dom, 1) % vm_ndomains; 679449c2e92SKonstantin Belousov 68085eeca35SAlan Cox inactl = 0; 68185eeca35SAlan Cox inactmax = cnt.v_inactive_count; 68285eeca35SAlan Cox actl = 0; 68385eeca35SAlan Cox actmax = tries < 2 ? 0 : cnt.v_active_count; 684449c2e92SKonstantin Belousov dom = initial_dom; 685449c2e92SKonstantin Belousov 686449c2e92SKonstantin Belousov /* 687449c2e92SKonstantin Belousov * Scan domains in round-robin order, first inactive queues, 688449c2e92SKonstantin Belousov * then active. Since domain usually owns large physically 689449c2e92SKonstantin Belousov * contiguous chunk of memory, it makes sense to completely 690449c2e92SKonstantin Belousov * exhaust one domain before switching to next, while growing 691449c2e92SKonstantin Belousov * the pool of contiguous physical pages. 692449c2e92SKonstantin Belousov * 693449c2e92SKonstantin Belousov * Do not even start launder a domain which cannot contain 694449c2e92SKonstantin Belousov * the specified address range, as indicated by segments 695449c2e92SKonstantin Belousov * constituting the domain. 696449c2e92SKonstantin Belousov */ 69785eeca35SAlan Cox again: 698449c2e92SKonstantin Belousov if (inactl < inactmax) { 699449c2e92SKonstantin Belousov if (vm_phys_domain_intersects(vm_dom[dom].vmd_segs, 700449c2e92SKonstantin Belousov low, high) && 701449c2e92SKonstantin Belousov vm_pageout_launder(&vm_dom[dom].vmd_pagequeues[PQ_INACTIVE], 702449c2e92SKonstantin Belousov tries, low, high)) { 70385eeca35SAlan Cox inactl++; 70485eeca35SAlan Cox goto again; 70585eeca35SAlan Cox } 706449c2e92SKonstantin Belousov if (++dom == vm_ndomains) 707449c2e92SKonstantin Belousov dom = 0; 708449c2e92SKonstantin Belousov if (dom != initial_dom) 709449c2e92SKonstantin Belousov goto again; 710449c2e92SKonstantin Belousov } 711449c2e92SKonstantin Belousov if (actl < actmax) { 712449c2e92SKonstantin Belousov if (vm_phys_domain_intersects(vm_dom[dom].vmd_segs, 713449c2e92SKonstantin Belousov low, high) && 714449c2e92SKonstantin Belousov vm_pageout_launder(&vm_dom[dom].vmd_pagequeues[PQ_ACTIVE], 715449c2e92SKonstantin Belousov tries, low, high)) { 71685eeca35SAlan Cox actl++; 71785eeca35SAlan Cox goto again; 71885eeca35SAlan Cox } 719449c2e92SKonstantin Belousov if (++dom == vm_ndomains) 720449c2e92SKonstantin Belousov dom = 0; 721449c2e92SKonstantin Belousov if (dom != initial_dom) 722449c2e92SKonstantin Belousov goto again; 723449c2e92SKonstantin Belousov } 72485eeca35SAlan Cox } 72585eeca35SAlan Cox 72638efa82bSJohn Dyson #if !defined(NO_SWAPPING) 72726f9a767SRodney W. Grimes /* 72826f9a767SRodney W. Grimes * vm_pageout_object_deactivate_pages 72926f9a767SRodney W. Grimes * 730ce186587SAlan Cox * Deactivate enough pages to satisfy the inactive target 731ce186587SAlan Cox * requirements. 73226f9a767SRodney W. Grimes * 73326f9a767SRodney W. Grimes * The object and map must be locked. 73426f9a767SRodney W. Grimes */ 73538efa82bSJohn Dyson static void 736ce186587SAlan Cox vm_pageout_object_deactivate_pages(pmap_t pmap, vm_object_t first_object, 737ce186587SAlan Cox long desired) 73826f9a767SRodney W. Grimes { 739ecf6279fSAlan Cox vm_object_t backing_object, object; 740ce186587SAlan Cox vm_page_t p; 741bb7858eaSJeff Roberson int act_delta, remove_mode; 74226f9a767SRodney W. Grimes 743e23b0a19SAlan Cox VM_OBJECT_ASSERT_LOCKED(first_object); 74428634820SAlan Cox if ((first_object->flags & OBJ_FICTITIOUS) != 0) 74538efa82bSJohn Dyson return; 746ecf6279fSAlan Cox for (object = first_object;; object = backing_object) { 747ecf6279fSAlan Cox if (pmap_resident_count(pmap) <= desired) 748ecf6279fSAlan Cox goto unlock_return; 749e23b0a19SAlan Cox VM_OBJECT_ASSERT_LOCKED(object); 75028634820SAlan Cox if ((object->flags & OBJ_UNMANAGED) != 0 || 75128634820SAlan Cox object->paging_in_progress != 0) 752ecf6279fSAlan Cox goto unlock_return; 75326f9a767SRodney W. Grimes 75485b1dc89SAlan Cox remove_mode = 0; 75538efa82bSJohn Dyson if (object->shadow_count > 1) 75638efa82bSJohn Dyson remove_mode = 1; 75726f9a767SRodney W. Grimes /* 758ce186587SAlan Cox * Scan the object's entire memory queue. 75926f9a767SRodney W. Grimes */ 760ce186587SAlan Cox TAILQ_FOREACH(p, &object->memq, listq) { 761447fe2a4SAlan Cox if (pmap_resident_count(pmap) <= desired) 762447fe2a4SAlan Cox goto unlock_return; 763c7aebda8SAttilio Rao if (vm_page_busied(p)) 764447fe2a4SAlan Cox continue; 765ce186587SAlan Cox PCPU_INC(cnt.v_pdpages); 7662965a453SKip Macy vm_page_lock(p); 767ce186587SAlan Cox if (p->wire_count != 0 || p->hold_count != 0 || 768ecf6279fSAlan Cox !pmap_page_exists_quick(pmap, p)) { 7692965a453SKip Macy vm_page_unlock(p); 7700d94caffSDavid Greenman continue; 7710d94caffSDavid Greenman } 772bb7858eaSJeff Roberson act_delta = pmap_ts_referenced(p); 7733407fefeSKonstantin Belousov if ((p->aflags & PGA_REFERENCED) != 0) { 774bb7858eaSJeff Roberson if (act_delta == 0) 775bb7858eaSJeff Roberson act_delta = 1; 7763407fefeSKonstantin Belousov vm_page_aflag_clear(p, PGA_REFERENCED); 777ef743ce6SJohn Dyson } 778bb7858eaSJeff Roberson if (p->queue != PQ_ACTIVE && act_delta != 0) { 779ef743ce6SJohn Dyson vm_page_activate(p); 780bb7858eaSJeff Roberson p->act_count += act_delta; 781c8c4b40cSJohn Dyson } else if (p->queue == PQ_ACTIVE) { 782bb7858eaSJeff Roberson if (act_delta == 0) { 783ce186587SAlan Cox p->act_count -= min(p->act_count, 784ce186587SAlan Cox ACT_DECLINE); 78590776bd7SJeff Roberson if (!remove_mode && p->act_count == 0) { 7864fec79beSAlan Cox pmap_remove_all(p); 78726f9a767SRodney W. Grimes vm_page_deactivate(p); 7888d220203SAlan Cox } else 7898d220203SAlan Cox vm_page_requeue(p); 790c8c4b40cSJohn Dyson } else { 791eaf13dd7SJohn Dyson vm_page_activate(p); 792ce186587SAlan Cox if (p->act_count < ACT_MAX - 793ce186587SAlan Cox ACT_ADVANCE) 79438efa82bSJohn Dyson p->act_count += ACT_ADVANCE; 7958d220203SAlan Cox vm_page_requeue(p); 796ce186587SAlan Cox } 797ce186587SAlan Cox } else if (p->queue == PQ_INACTIVE) 798ce186587SAlan Cox pmap_remove_all(p); 7992965a453SKip Macy vm_page_unlock(p); 80026f9a767SRodney W. Grimes } 801ecf6279fSAlan Cox if ((backing_object = object->backing_object) == NULL) 802ecf6279fSAlan Cox goto unlock_return; 803e23b0a19SAlan Cox VM_OBJECT_RLOCK(backing_object); 804ecf6279fSAlan Cox if (object != first_object) 805e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(object); 80638efa82bSJohn Dyson } 807ecf6279fSAlan Cox unlock_return: 808ecf6279fSAlan Cox if (object != first_object) 809e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(object); 81026f9a767SRodney W. Grimes } 81126f9a767SRodney W. Grimes 81226f9a767SRodney W. Grimes /* 81326f9a767SRodney W. Grimes * deactivate some number of pages in a map, try to do it fairly, but 81426f9a767SRodney W. Grimes * that is really hard to do. 81526f9a767SRodney W. Grimes */ 816cd41fc12SDavid Greenman static void 81738efa82bSJohn Dyson vm_pageout_map_deactivate_pages(map, desired) 81826f9a767SRodney W. Grimes vm_map_t map; 819ecf6279fSAlan Cox long desired; 82026f9a767SRodney W. Grimes { 82126f9a767SRodney W. Grimes vm_map_entry_t tmpe; 82238efa82bSJohn Dyson vm_object_t obj, bigobj; 82330105b9eSTor Egge int nothingwired; 8240d94caffSDavid Greenman 825d974f03cSAlan Cox if (!vm_map_trylock(map)) 82626f9a767SRodney W. Grimes return; 82738efa82bSJohn Dyson 82838efa82bSJohn Dyson bigobj = NULL; 82930105b9eSTor Egge nothingwired = TRUE; 83038efa82bSJohn Dyson 83138efa82bSJohn Dyson /* 83238efa82bSJohn Dyson * first, search out the biggest object, and try to free pages from 83338efa82bSJohn Dyson * that. 83438efa82bSJohn Dyson */ 83526f9a767SRodney W. Grimes tmpe = map->header.next; 83638efa82bSJohn Dyson while (tmpe != &map->header) { 8379fdfe602SMatthew Dillon if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 83838efa82bSJohn Dyson obj = tmpe->object.vm_object; 839e23b0a19SAlan Cox if (obj != NULL && VM_OBJECT_TRYRLOCK(obj)) { 8400774dfb3SAlan Cox if (obj->shadow_count <= 1 && 8410774dfb3SAlan Cox (bigobj == NULL || 8420774dfb3SAlan Cox bigobj->resident_page_count < obj->resident_page_count)) { 8430774dfb3SAlan Cox if (bigobj != NULL) 844e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(bigobj); 84538efa82bSJohn Dyson bigobj = obj; 8460774dfb3SAlan Cox } else 847e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(obj); 84838efa82bSJohn Dyson } 84938efa82bSJohn Dyson } 85030105b9eSTor Egge if (tmpe->wired_count > 0) 85130105b9eSTor Egge nothingwired = FALSE; 85238efa82bSJohn Dyson tmpe = tmpe->next; 85338efa82bSJohn Dyson } 85438efa82bSJohn Dyson 8550774dfb3SAlan Cox if (bigobj != NULL) { 856ecf6279fSAlan Cox vm_pageout_object_deactivate_pages(map->pmap, bigobj, desired); 857e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(bigobj); 8580774dfb3SAlan Cox } 85938efa82bSJohn Dyson /* 86038efa82bSJohn Dyson * Next, hunt around for other pages to deactivate. We actually 86138efa82bSJohn Dyson * do this search sort of wrong -- .text first is not the best idea. 86238efa82bSJohn Dyson */ 86338efa82bSJohn Dyson tmpe = map->header.next; 86438efa82bSJohn Dyson while (tmpe != &map->header) { 865b1028ad1SLuoqi Chen if (pmap_resident_count(vm_map_pmap(map)) <= desired) 86638efa82bSJohn Dyson break; 8679fdfe602SMatthew Dillon if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 86838efa82bSJohn Dyson obj = tmpe->object.vm_object; 8690774dfb3SAlan Cox if (obj != NULL) { 870e23b0a19SAlan Cox VM_OBJECT_RLOCK(obj); 871ecf6279fSAlan Cox vm_pageout_object_deactivate_pages(map->pmap, obj, desired); 872e23b0a19SAlan Cox VM_OBJECT_RUNLOCK(obj); 8730774dfb3SAlan Cox } 87438efa82bSJohn Dyson } 87526f9a767SRodney W. Grimes tmpe = tmpe->next; 87638857e7fSAlan Cox } 87738efa82bSJohn Dyson 878938b0f5bSMarcel Moolenaar #ifdef __ia64__ 879938b0f5bSMarcel Moolenaar /* 880938b0f5bSMarcel Moolenaar * Remove all non-wired, managed mappings if a process is swapped out. 881938b0f5bSMarcel Moolenaar * This will free page table pages. 882938b0f5bSMarcel Moolenaar */ 883938b0f5bSMarcel Moolenaar if (desired == 0) 884938b0f5bSMarcel Moolenaar pmap_remove_pages(map->pmap); 885938b0f5bSMarcel Moolenaar #else 88638efa82bSJohn Dyson /* 88738efa82bSJohn Dyson * Remove all mappings if a process is swapped out, this will free page 88838efa82bSJohn Dyson * table pages. 88938efa82bSJohn Dyson */ 89038857e7fSAlan Cox if (desired == 0 && nothingwired) { 8918d01a3b2SNathan Whitehorn pmap_remove(vm_map_pmap(map), vm_map_min(map), 8928d01a3b2SNathan Whitehorn vm_map_max(map)); 89338857e7fSAlan Cox } 894938b0f5bSMarcel Moolenaar #endif 895938b0f5bSMarcel Moolenaar 89638efa82bSJohn Dyson vm_map_unlock(map); 89726f9a767SRodney W. Grimes } 898a1287949SEivind Eklund #endif /* !defined(NO_SWAPPING) */ 899df8bae1dSRodney W. Grimes 9001c7c3c6aSMatthew Dillon /* 901df8bae1dSRodney W. Grimes * vm_pageout_scan does the dirty work for the pageout daemon. 902d9e23210SJeff Roberson * 903d9e23210SJeff Roberson * pass 0 - Update active LRU/deactivate pages 904d9e23210SJeff Roberson * pass 1 - Move inactive to cache or free 905d9e23210SJeff Roberson * pass 2 - Launder dirty pages 906df8bae1dSRodney W. Grimes */ 9072b6b0df7SMatthew Dillon static void 908449c2e92SKonstantin Belousov vm_pageout_scan(struct vm_domain *vmd, int pass) 909df8bae1dSRodney W. Grimes { 910502ba6e4SJohn Dyson vm_page_t m, next; 9118d220203SAlan Cox struct vm_pagequeue *pq; 912df8bae1dSRodney W. Grimes vm_object_t object; 913*9099545aSAlan Cox int act_delta, addl_page_shortage, deficit, maxscan, page_shortage; 914f6b04d2bSDavid Greenman int vnodes_skipped = 0; 9152b6b0df7SMatthew Dillon int maxlaunder; 9161bd7d0b7SKonstantin Belousov int lockmode; 91748cc2fc7SKonstantin Belousov boolean_t queues_locked; 9180d94caffSDavid Greenman 919df8bae1dSRodney W. Grimes /* 920d9e23210SJeff Roberson * If we need to reclaim memory ask kernel caches to return 921c9612b2dSJeff Roberson * some. We rate limit to avoid thrashing. 922d9e23210SJeff Roberson */ 923c9612b2dSJeff Roberson if (vmd == &vm_dom[0] && pass > 0 && 924c9612b2dSJeff Roberson lowmem_ticks + (lowmem_period * hz) < ticks) { 925d9e23210SJeff Roberson /* 926855a310fSJeff Roberson * Decrease registered cache sizes. 927855a310fSJeff Roberson */ 928855a310fSJeff Roberson EVENTHANDLER_INVOKE(vm_lowmem, 0); 929855a310fSJeff Roberson /* 930d9e23210SJeff Roberson * We do this explicitly after the caches have been 931d9e23210SJeff Roberson * drained above. 932855a310fSJeff Roberson */ 933855a310fSJeff Roberson uma_reclaim(); 934c9612b2dSJeff Roberson lowmem_ticks = ticks; 935d9e23210SJeff Roberson } 9365985940eSJohn Dyson 937311e34e2SKonstantin Belousov /* 93896240c89SEitan Adler * The addl_page_shortage is the number of temporarily 939311e34e2SKonstantin Belousov * stuck pages in the inactive queue. In other words, the 940449c2e92SKonstantin Belousov * number of pages from the inactive count that should be 941311e34e2SKonstantin Belousov * discounted in setting the target for the active queue scan. 942311e34e2SKonstantin Belousov */ 943*9099545aSAlan Cox addl_page_shortage = 0; 944*9099545aSAlan Cox 945*9099545aSAlan Cox deficit = atomic_readandclear_int(&vm_pageout_deficit); 946b182ec9eSJohn Dyson 9471c7c3c6aSMatthew Dillon /* 9481c7c3c6aSMatthew Dillon * Calculate the number of pages we want to either free or move 9492b6b0df7SMatthew Dillon * to the cache. 9501c7c3c6aSMatthew Dillon */ 951*9099545aSAlan Cox page_shortage = vm_paging_target() + deficit; 9521c7c3c6aSMatthew Dillon 953936524aaSMatthew Dillon /* 9542b6b0df7SMatthew Dillon * maxlaunder limits the number of dirty pages we flush per scan. 9552b6b0df7SMatthew Dillon * For most systems a smaller value (16 or 32) is more robust under 9562b6b0df7SMatthew Dillon * extreme memory and disk pressure because any unnecessary writes 9572b6b0df7SMatthew Dillon * to disk can result in extreme performance degredation. However, 9582b6b0df7SMatthew Dillon * systems with excessive dirty pages (especially when MAP_NOSYNC is 9592b6b0df7SMatthew Dillon * used) will die horribly with limited laundering. If the pageout 9602b6b0df7SMatthew Dillon * daemon cannot clean enough pages in the first pass, we let it go 9612b6b0df7SMatthew Dillon * all out in succeeding passes. 9621c7c3c6aSMatthew Dillon */ 9632b6b0df7SMatthew Dillon if ((maxlaunder = vm_max_launder) <= 1) 9642b6b0df7SMatthew Dillon maxlaunder = 1; 965d9e23210SJeff Roberson if (pass > 1) 9662b6b0df7SMatthew Dillon maxlaunder = 10000; 9678d220203SAlan Cox 9688d220203SAlan Cox /* 9698d220203SAlan Cox * Start scanning the inactive queue for pages we can move to the 9708d220203SAlan Cox * cache or free. The scan will stop when the target is reached or 9718d220203SAlan Cox * we have scanned the entire inactive queue. Note that m->act_count 9728d220203SAlan Cox * is not used to form decisions for the inactive queue, only for the 9738d220203SAlan Cox * active queue. 9748d220203SAlan Cox */ 975449c2e92SKonstantin Belousov pq = &vmd->vmd_pagequeues[PQ_INACTIVE]; 976449c2e92SKonstantin Belousov maxscan = pq->pq_cnt; 9778d220203SAlan Cox vm_pagequeue_lock(pq); 9788d220203SAlan Cox queues_locked = TRUE; 9798d220203SAlan Cox for (m = TAILQ_FIRST(&pq->pq_pl); 9801c7c3c6aSMatthew Dillon m != NULL && maxscan-- > 0 && page_shortage > 0; 981e929c00dSKirk McKusick m = next) { 9828d220203SAlan Cox vm_pagequeue_assert_locked(pq); 98348cc2fc7SKonstantin Belousov KASSERT(queues_locked, ("unlocked queues")); 984d4961bcbSKonstantin Belousov KASSERT(m->queue == PQ_INACTIVE, ("Inactive queue %p", m)); 985df8bae1dSRodney W. Grimes 9868d220203SAlan Cox PCPU_INC(cnt.v_pdpages); 987c325e866SKonstantin Belousov next = TAILQ_NEXT(m, plinks.q); 988df8bae1dSRodney W. Grimes 989936524aaSMatthew Dillon /* 990936524aaSMatthew Dillon * skip marker pages 991936524aaSMatthew Dillon */ 992936524aaSMatthew Dillon if (m->flags & PG_MARKER) 993936524aaSMatthew Dillon continue; 994936524aaSMatthew Dillon 9957900f95dSKonstantin Belousov KASSERT((m->flags & PG_FICTITIOUS) == 0, 9967900f95dSKonstantin Belousov ("Fictitious page %p cannot be in inactive queue", m)); 9977900f95dSKonstantin Belousov KASSERT((m->oflags & VPO_UNMANAGED) == 0, 9987900f95dSKonstantin Belousov ("Unmanaged page %p cannot be in inactive queue", m)); 9997900f95dSKonstantin Belousov 10008c616246SKonstantin Belousov /* 1001311e34e2SKonstantin Belousov * The page or object lock acquisitions fail if the 1002311e34e2SKonstantin Belousov * page was removed from the queue or moved to a 1003311e34e2SKonstantin Belousov * different position within the queue. In either 1004311e34e2SKonstantin Belousov * case, addl_page_shortage should not be incremented. 10058c616246SKonstantin Belousov */ 10068c616246SKonstantin Belousov if (!vm_pageout_page_lock(m, &next)) { 10078c616246SKonstantin Belousov vm_page_unlock(m); 1008b182ec9eSJohn Dyson continue; 1009df8bae1dSRodney W. Grimes } 10109ee2165fSAlan Cox object = m->object; 101189f6b863SAttilio Rao if (!VM_OBJECT_TRYWLOCK(object) && 1012311e34e2SKonstantin Belousov !vm_pageout_fallback_object_lock(m, &next)) { 10132965a453SKip Macy vm_page_unlock(m); 101489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 101534d9e6fdSAlan Cox continue; 101634d9e6fdSAlan Cox } 1017311e34e2SKonstantin Belousov 1018311e34e2SKonstantin Belousov /* 1019311e34e2SKonstantin Belousov * Don't mess with busy pages, keep them at at the 1020311e34e2SKonstantin Belousov * front of the queue, most likely they are being 1021311e34e2SKonstantin Belousov * paged out. Increment addl_page_shortage for busy 1022311e34e2SKonstantin Belousov * pages, because they may leave the inactive queue 1023311e34e2SKonstantin Belousov * shortly after page scan is finished. 1024311e34e2SKonstantin Belousov */ 1025c7aebda8SAttilio Rao if (vm_page_busied(m)) { 10262965a453SKip Macy vm_page_unlock(m); 102789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1028b182ec9eSJohn Dyson addl_page_shortage++; 102926f9a767SRodney W. Grimes continue; 103026f9a767SRodney W. Grimes } 1031bd7e5f99SJohn Dyson 10327e006499SJohn Dyson /* 10338d220203SAlan Cox * We unlock the inactive page queue, invalidating the 103448cc2fc7SKonstantin Belousov * 'next' pointer. Use our marker to remember our 103548cc2fc7SKonstantin Belousov * place. 103648cc2fc7SKonstantin Belousov */ 1037c325e866SKonstantin Belousov TAILQ_INSERT_AFTER(&pq->pq_pl, m, &vmd->vmd_marker, plinks.q); 10388d220203SAlan Cox vm_pagequeue_unlock(pq); 103948cc2fc7SKonstantin Belousov queues_locked = FALSE; 104048cc2fc7SKonstantin Belousov 104148cc2fc7SKonstantin Belousov /* 1042bb7858eaSJeff Roberson * We bump the activation count if the page has been 1043bb7858eaSJeff Roberson * referenced while in the inactive queue. This makes 1044bb7858eaSJeff Roberson * it less likely that the page will be added back to the 1045bb7858eaSJeff Roberson * inactive queue prematurely again. Here we check the 10461c7c3c6aSMatthew Dillon * page tables (or emulated bits, if any), given the upper 10471c7c3c6aSMatthew Dillon * level VM system not knowing anything about existing 10481c7c3c6aSMatthew Dillon * references. 10497e006499SJohn Dyson */ 1050bb7858eaSJeff Roberson act_delta = 0; 1051bb7858eaSJeff Roberson if ((m->aflags & PGA_REFERENCED) != 0) { 1052bb7858eaSJeff Roberson vm_page_aflag_clear(m, PGA_REFERENCED); 1053bb7858eaSJeff Roberson act_delta = 1; 1054bb7858eaSJeff Roberson } 1055bb7858eaSJeff Roberson if (object->ref_count != 0) { 1056bb7858eaSJeff Roberson act_delta += pmap_ts_referenced(m); 1057bb7858eaSJeff Roberson } else { 1058bb7858eaSJeff Roberson KASSERT(!pmap_page_is_mapped(m), 1059bb7858eaSJeff Roberson ("vm_pageout_scan: page %p is mapped", m)); 10602fe6e4d7SDavid Greenman } 1061ef743ce6SJohn Dyson 10627e006499SJohn Dyson /* 10631c7c3c6aSMatthew Dillon * If the upper level VM system knows about any page 1064bb7858eaSJeff Roberson * references, we reactivate the page or requeue it. 10657e006499SJohn Dyson */ 1066bb7858eaSJeff Roberson if (act_delta != 0) { 1067bb7858eaSJeff Roberson if (object->ref_count) { 106826f9a767SRodney W. Grimes vm_page_activate(m); 1069bb7858eaSJeff Roberson m->act_count += act_delta + ACT_ADVANCE; 1070bb7858eaSJeff Roberson } else { 1071bb7858eaSJeff Roberson vm_pagequeue_lock(pq); 1072bb7858eaSJeff Roberson queues_locked = TRUE; 1073bb7858eaSJeff Roberson vm_page_requeue_locked(m); 1074bb7858eaSJeff Roberson } 107589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1076a15f7df5SAttilio Rao vm_page_unlock(m); 107748cc2fc7SKonstantin Belousov goto relock_queues; 10780d94caffSDavid Greenman } 107967bf6868SJohn Dyson 1080311e34e2SKonstantin Belousov if (m->hold_count != 0) { 1081311e34e2SKonstantin Belousov vm_page_unlock(m); 108289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1083311e34e2SKonstantin Belousov 1084311e34e2SKonstantin Belousov /* 1085311e34e2SKonstantin Belousov * Held pages are essentially stuck in the 1086311e34e2SKonstantin Belousov * queue. So, they ought to be discounted 1087449c2e92SKonstantin Belousov * from the inactive count. See the 1088311e34e2SKonstantin Belousov * calculation of the page_shortage for the 1089311e34e2SKonstantin Belousov * loop over the active queue below. 1090311e34e2SKonstantin Belousov */ 1091311e34e2SKonstantin Belousov addl_page_shortage++; 1092311e34e2SKonstantin Belousov goto relock_queues; 1093311e34e2SKonstantin Belousov } 1094311e34e2SKonstantin Belousov 10957e006499SJohn Dyson /* 10969fc4739dSAlan Cox * If the page appears to be clean at the machine-independent 10979fc4739dSAlan Cox * layer, then remove all of its mappings from the pmap in 10989fc4739dSAlan Cox * anticipation of placing it onto the cache queue. If, 10999fc4739dSAlan Cox * however, any of the page's mappings allow write access, 11009fc4739dSAlan Cox * then the page may still be modified until the last of those 11019fc4739dSAlan Cox * mappings are removed. 11027e006499SJohn Dyson */ 11039fc4739dSAlan Cox vm_page_test_dirty(m); 11049fc4739dSAlan Cox if (m->dirty == 0 && object->ref_count != 0) 1105b78ddb0bSAlan Cox pmap_remove_all(m); 1106dcbcd518SBruce Evans 11076989c456SAlan Cox if (m->valid == 0) { 11087e006499SJohn Dyson /* 11097e006499SJohn Dyson * Invalid pages can be easily freed 11107e006499SJohn Dyson */ 11116989c456SAlan Cox vm_page_free(m); 111248cc2fc7SKonstantin Belousov PCPU_INC(cnt.v_dfree); 11131c7c3c6aSMatthew Dillon --page_shortage; 1114bd7e5f99SJohn Dyson } else if (m->dirty == 0) { 11156989c456SAlan Cox /* 11166989c456SAlan Cox * Clean pages can be placed onto the cache queue. 11176989c456SAlan Cox * This effectively frees them. 11186989c456SAlan Cox */ 1119bd7e5f99SJohn Dyson vm_page_cache(m); 11201c7c3c6aSMatthew Dillon --page_shortage; 1121d9e23210SJeff Roberson } else if ((m->flags & PG_WINATCFLS) == 0 && pass < 2) { 11227e006499SJohn Dyson /* 11232b6b0df7SMatthew Dillon * Dirty pages need to be paged out, but flushing 11242b6b0df7SMatthew Dillon * a page is extremely expensive verses freeing 11252b6b0df7SMatthew Dillon * a clean page. Rather then artificially limiting 11262b6b0df7SMatthew Dillon * the number of pages we can flush, we instead give 11272b6b0df7SMatthew Dillon * dirty pages extra priority on the inactive queue 11282b6b0df7SMatthew Dillon * by forcing them to be cycled through the queue 11292b6b0df7SMatthew Dillon * twice before being flushed, after which the 11302b6b0df7SMatthew Dillon * (now clean) page will cycle through once more 11312b6b0df7SMatthew Dillon * before being freed. This significantly extends 11322b6b0df7SMatthew Dillon * the thrash point for a heavily loaded machine. 11337e006499SJohn Dyson */ 11343407fefeSKonstantin Belousov m->flags |= PG_WINATCFLS; 11358d220203SAlan Cox vm_pagequeue_lock(pq); 113648cc2fc7SKonstantin Belousov queues_locked = TRUE; 11378d220203SAlan Cox vm_page_requeue_locked(m); 11380d94caffSDavid Greenman } else if (maxlaunder > 0) { 11392b6b0df7SMatthew Dillon /* 11402b6b0df7SMatthew Dillon * We always want to try to flush some dirty pages if 11412b6b0df7SMatthew Dillon * we encounter them, to keep the system stable. 11422b6b0df7SMatthew Dillon * Normally this number is small, but under extreme 11432b6b0df7SMatthew Dillon * pressure where there are insufficient clean pages 11442b6b0df7SMatthew Dillon * on the inactive queue, we may have to go all out. 11452b6b0df7SMatthew Dillon */ 11465050aa86SKonstantin Belousov int swap_pageouts_ok; 1147f6b04d2bSDavid Greenman struct vnode *vp = NULL; 114814137dc0SAlan Cox struct mount *mp = NULL; 11490d94caffSDavid Greenman 115012ac6a1dSJohn Dyson if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) { 115112ac6a1dSJohn Dyson swap_pageouts_ok = 1; 115212ac6a1dSJohn Dyson } else { 115312ac6a1dSJohn Dyson swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts); 115412ac6a1dSJohn Dyson swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts && 115590ecac61SMatthew Dillon vm_page_count_min()); 115612ac6a1dSJohn Dyson 115712ac6a1dSJohn Dyson } 115870111b90SJohn Dyson 115970111b90SJohn Dyson /* 11601c7c3c6aSMatthew Dillon * We don't bother paging objects that are "dead". 11611c7c3c6aSMatthew Dillon * Those objects are in a "rundown" state. 116270111b90SJohn Dyson */ 116370111b90SJohn Dyson if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) { 11648d220203SAlan Cox vm_pagequeue_lock(pq); 11652965a453SKip Macy vm_page_unlock(m); 116689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 116748cc2fc7SKonstantin Belousov queues_locked = TRUE; 11688d220203SAlan Cox vm_page_requeue_locked(m); 116948cc2fc7SKonstantin Belousov goto relock_queues; 117012ac6a1dSJohn Dyson } 117112ac6a1dSJohn Dyson 11721c7c3c6aSMatthew Dillon /* 11732b6b0df7SMatthew Dillon * The object is already known NOT to be dead. It 11742b6b0df7SMatthew Dillon * is possible for the vget() to block the whole 11752b6b0df7SMatthew Dillon * pageout daemon, but the new low-memory handling 11762b6b0df7SMatthew Dillon * code should prevent it. 11771c7c3c6aSMatthew Dillon * 11782b6b0df7SMatthew Dillon * The previous code skipped locked vnodes and, worse, 11792b6b0df7SMatthew Dillon * reordered pages in the queue. This results in 11802b6b0df7SMatthew Dillon * completely non-deterministic operation and, on a 11812b6b0df7SMatthew Dillon * busy system, can lead to extremely non-optimal 11822b6b0df7SMatthew Dillon * pageouts. For example, it can cause clean pages 11832b6b0df7SMatthew Dillon * to be freed and dirty pages to be moved to the end 11842b6b0df7SMatthew Dillon * of the queue. Since dirty pages are also moved to 11852b6b0df7SMatthew Dillon * the end of the queue once-cleaned, this gives 11862b6b0df7SMatthew Dillon * way too large a weighting to defering the freeing 11872b6b0df7SMatthew Dillon * of dirty pages. 11881c7c3c6aSMatthew Dillon * 118923b59018SMatthew Dillon * We can't wait forever for the vnode lock, we might 119023b59018SMatthew Dillon * deadlock due to a vn_read() getting stuck in 119123b59018SMatthew Dillon * vm_wait while holding this vnode. We skip the 119223b59018SMatthew Dillon * vnode if we can't get it in a reasonable amount 119323b59018SMatthew Dillon * of time. 11941c7c3c6aSMatthew Dillon */ 11951c7c3c6aSMatthew Dillon if (object->type == OBJT_VNODE) { 11962965a453SKip Macy vm_page_unlock(m); 119724a1cce3SDavid Greenman vp = object->handle; 1198db27dcc0STor Egge if (vp->v_type == VREG && 1199db27dcc0STor Egge vn_start_write(vp, &mp, V_NOWAIT) != 0) { 12006129343dSKonstantin Belousov mp = NULL; 1201db27dcc0STor Egge ++pageout_lock_miss; 1202db27dcc0STor Egge if (object->flags & OBJ_MIGHTBEDIRTY) 1203db27dcc0STor Egge vnodes_skipped++; 1204625e6c0aSTor Egge goto unlock_and_continue; 1205db27dcc0STor Egge } 1206b9f180d1SKonstantin Belousov KASSERT(mp != NULL, 1207b9f180d1SKonstantin Belousov ("vp %p with NULL v_mount", vp)); 120814137dc0SAlan Cox vm_object_reference_locked(object); 120989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 12101bd7d0b7SKonstantin Belousov lockmode = MNT_SHARED_WRITES(vp->v_mount) ? 12111bd7d0b7SKonstantin Belousov LK_SHARED : LK_EXCLUSIVE; 12121bd7d0b7SKonstantin Belousov if (vget(vp, lockmode | LK_TIMELOCK, 121397824da3SAlan Cox curthread)) { 121489f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 121523b59018SMatthew Dillon ++pageout_lock_miss; 1216aef922f5SJohn Dyson if (object->flags & OBJ_MIGHTBEDIRTY) 1217925a3a41SJohn Dyson vnodes_skipped++; 1218625e6c0aSTor Egge vp = NULL; 1219625e6c0aSTor Egge goto unlock_and_continue; 122085a376ebSJohn Dyson } 122189f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 12222965a453SKip Macy vm_page_lock(m); 12238d220203SAlan Cox vm_pagequeue_lock(pq); 122448cc2fc7SKonstantin Belousov queues_locked = TRUE; 1225f35329acSJohn Dyson /* 1226936524aaSMatthew Dillon * The page might have been moved to another 1227936524aaSMatthew Dillon * queue during potential blocking in vget() 1228936524aaSMatthew Dillon * above. The page might have been freed and 122914137dc0SAlan Cox * reused for another vnode. 1230f35329acSJohn Dyson */ 12319cf51988SAlan Cox if (m->queue != PQ_INACTIVE || 1232936524aaSMatthew Dillon m->object != object || 1233c325e866SKonstantin Belousov TAILQ_NEXT(m, plinks.q) != &vmd->vmd_marker) { 12342965a453SKip Macy vm_page_unlock(m); 1235b182ec9eSJohn Dyson if (object->flags & OBJ_MIGHTBEDIRTY) 1236925a3a41SJohn Dyson vnodes_skipped++; 12373562af12SAlan Cox goto unlock_and_continue; 1238b182ec9eSJohn Dyson } 1239b182ec9eSJohn Dyson 1240f35329acSJohn Dyson /* 1241936524aaSMatthew Dillon * The page may have been busied during the 124214137dc0SAlan Cox * blocking in vget(). We don't move the 1243936524aaSMatthew Dillon * page back onto the end of the queue so that 1244936524aaSMatthew Dillon * statistics are more correct if we don't. 1245f35329acSJohn Dyson */ 1246c7aebda8SAttilio Rao if (vm_page_busied(m)) { 12472965a453SKip Macy vm_page_unlock(m); 1248*9099545aSAlan Cox addl_page_shortage++; 12493562af12SAlan Cox goto unlock_and_continue; 1250b182ec9eSJohn Dyson } 1251b182ec9eSJohn Dyson 1252f35329acSJohn Dyson /* 125357601bcbSMatthew Dillon * If the page has become held it might 125457601bcbSMatthew Dillon * be undergoing I/O, so skip it 1255f35329acSJohn Dyson */ 1256*9099545aSAlan Cox if (m->hold_count != 0) { 12572965a453SKip Macy vm_page_unlock(m); 1258*9099545aSAlan Cox addl_page_shortage++; 1259b182ec9eSJohn Dyson if (object->flags & OBJ_MIGHTBEDIRTY) 1260925a3a41SJohn Dyson vnodes_skipped++; 12613562af12SAlan Cox goto unlock_and_continue; 1262f6b04d2bSDavid Greenman } 12638d220203SAlan Cox vm_pagequeue_unlock(pq); 1264a4156419SKonstantin Belousov queues_locked = FALSE; 1265f6b04d2bSDavid Greenman } 1266f6b04d2bSDavid Greenman 12670d94caffSDavid Greenman /* 12680d94caffSDavid Greenman * If a page is dirty, then it is either being washed 12690d94caffSDavid Greenman * (but not yet cleaned) or it is still in the 12700d94caffSDavid Greenman * laundry. If it is still in the laundry, then we 12712b6b0df7SMatthew Dillon * start the cleaning operation. 1272936524aaSMatthew Dillon * 12732b6b0df7SMatthew Dillon * decrement page_shortage on success to account for 12742b6b0df7SMatthew Dillon * the (future) cleaned page. Otherwise we could wind 12752b6b0df7SMatthew Dillon * up laundering or cleaning too many pages. 12760d94caffSDavid Greenman */ 12772b6b0df7SMatthew Dillon if (vm_pageout_clean(m) != 0) { 12782b6b0df7SMatthew Dillon --page_shortage; 1279936524aaSMatthew Dillon --maxlaunder; 12802b6b0df7SMatthew Dillon } 12813562af12SAlan Cox unlock_and_continue: 12822965a453SKip Macy vm_page_lock_assert(m, MA_NOTOWNED); 128389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 128414137dc0SAlan Cox if (mp != NULL) { 128548cc2fc7SKonstantin Belousov if (queues_locked) { 12868d220203SAlan Cox vm_pagequeue_unlock(pq); 128748cc2fc7SKonstantin Belousov queues_locked = FALSE; 128848cc2fc7SKonstantin Belousov } 128914137dc0SAlan Cox if (vp != NULL) 1290f6b04d2bSDavid Greenman vput(vp); 129114137dc0SAlan Cox vm_object_deallocate(object); 1292f2a2857bSKirk McKusick vn_finished_write(mp); 129348cc2fc7SKonstantin Belousov } 129448cc2fc7SKonstantin Belousov vm_page_lock_assert(m, MA_NOTOWNED); 129548cc2fc7SKonstantin Belousov goto relock_queues; 129648cc2fc7SKonstantin Belousov } 129748cc2fc7SKonstantin Belousov vm_page_unlock(m); 129889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 129948cc2fc7SKonstantin Belousov relock_queues: 130048cc2fc7SKonstantin Belousov if (!queues_locked) { 13018d220203SAlan Cox vm_pagequeue_lock(pq); 130248cc2fc7SKonstantin Belousov queues_locked = TRUE; 13036989c456SAlan Cox } 1304c325e866SKonstantin Belousov next = TAILQ_NEXT(&vmd->vmd_marker, plinks.q); 1305c325e866SKonstantin Belousov TAILQ_REMOVE(&pq->pq_pl, &vmd->vmd_marker, plinks.q); 13060d94caffSDavid Greenman } 13078d220203SAlan Cox vm_pagequeue_unlock(pq); 130826f9a767SRodney W. Grimes 1309df8bae1dSRodney W. Grimes /* 1310936524aaSMatthew Dillon * Compute the number of pages we want to try to move from the 1311936524aaSMatthew Dillon * active queue to the inactive queue. 13121c7c3c6aSMatthew Dillon */ 1313*9099545aSAlan Cox page_shortage = cnt.v_inactive_target - cnt.v_inactive_count + 1314*9099545aSAlan Cox vm_paging_target() + deficit + addl_page_shortage; 1315*9099545aSAlan Cox 1316114f62c6SJeff Roberson pq = &vmd->vmd_pagequeues[PQ_ACTIVE]; 1317114f62c6SJeff Roberson vm_pagequeue_lock(pq); 1318*9099545aSAlan Cox maxscan = pq->pq_cnt; 1319*9099545aSAlan Cox 1320d9e23210SJeff Roberson /* 1321d9e23210SJeff Roberson * If we're just idle polling attempt to visit every 1322d9e23210SJeff Roberson * active page within 'update_period' seconds. 1323d9e23210SJeff Roberson */ 1324d9e23210SJeff Roberson if (pass == 0 && vm_pageout_update_period != 0) { 1325*9099545aSAlan Cox maxscan /= vm_pageout_update_period; 1326*9099545aSAlan Cox page_shortage = maxscan; 1327d9e23210SJeff Roberson } 13281c7c3c6aSMatthew Dillon 13291c7c3c6aSMatthew Dillon /* 1330936524aaSMatthew Dillon * Scan the active queue for things we can deactivate. We nominally 1331936524aaSMatthew Dillon * track the per-page activity counter and use it to locate 1332936524aaSMatthew Dillon * deactivation candidates. 13331c7c3c6aSMatthew Dillon */ 13348d220203SAlan Cox m = TAILQ_FIRST(&pq->pq_pl); 1335*9099545aSAlan Cox while (m != NULL && maxscan-- > 0 && page_shortage > 0) { 1336f35329acSJohn Dyson 13379cf51988SAlan Cox KASSERT(m->queue == PQ_ACTIVE, 1338d3c09dd7SAlan Cox ("vm_pageout_scan: page %p isn't active", m)); 1339f35329acSJohn Dyson 1340c325e866SKonstantin Belousov next = TAILQ_NEXT(m, plinks.q); 13418dbca793STor Egge if ((m->flags & PG_MARKER) != 0) { 13428dbca793STor Egge m = next; 13438dbca793STor Egge continue; 13448dbca793STor Egge } 13457900f95dSKonstantin Belousov KASSERT((m->flags & PG_FICTITIOUS) == 0, 13467900f95dSKonstantin Belousov ("Fictitious page %p cannot be in active queue", m)); 13477900f95dSKonstantin Belousov KASSERT((m->oflags & VPO_UNMANAGED) == 0, 13487900f95dSKonstantin Belousov ("Unmanaged page %p cannot be in active queue", m)); 13499ee2165fSAlan Cox if (!vm_pageout_page_lock(m, &next)) { 13508c616246SKonstantin Belousov vm_page_unlock(m); 13512965a453SKip Macy m = next; 13522965a453SKip Macy continue; 13532965a453SKip Macy } 1354b18bfc3dSJohn Dyson 1355b18bfc3dSJohn Dyson /* 1356b18bfc3dSJohn Dyson * The count for pagedaemon pages is done after checking the 1357956f3135SPhilippe Charnier * page for eligibility... 1358b18bfc3dSJohn Dyson */ 13598d220203SAlan Cox PCPU_INC(cnt.v_pdpages); 1360ef743ce6SJohn Dyson 13617e006499SJohn Dyson /* 13627e006499SJohn Dyson * Check to see "how much" the page has been used. 13637e006499SJohn Dyson */ 1364bb7858eaSJeff Roberson act_delta = 0; 13653407fefeSKonstantin Belousov if (m->aflags & PGA_REFERENCED) { 1366bb7858eaSJeff Roberson vm_page_aflag_clear(m, PGA_REFERENCED); 1367bb7858eaSJeff Roberson act_delta += 1; 13680d94caffSDavid Greenman } 1369274132acSJeff Roberson /* 1370274132acSJeff Roberson * Unlocked object ref count check. Two races are possible. 1371274132acSJeff Roberson * 1) The ref was transitioning to zero and we saw non-zero, 1372274132acSJeff Roberson * the pmap bits will be checked unnecessarily. 1373274132acSJeff Roberson * 2) The ref was transitioning to one and we saw zero. 1374274132acSJeff Roberson * The page lock prevents a new reference to this page so 1375274132acSJeff Roberson * we need not check the reference bits. 1376274132acSJeff Roberson */ 1377274132acSJeff Roberson if (m->object->ref_count != 0) 1378bb7858eaSJeff Roberson act_delta += pmap_ts_referenced(m); 1379bb7858eaSJeff Roberson 1380bb7858eaSJeff Roberson /* 1381bb7858eaSJeff Roberson * Advance or decay the act_count based on recent usage. 1382bb7858eaSJeff Roberson */ 1383bb7858eaSJeff Roberson if (act_delta) { 1384bb7858eaSJeff Roberson m->act_count += ACT_ADVANCE + act_delta; 138538efa82bSJohn Dyson if (m->act_count > ACT_MAX) 138638efa82bSJohn Dyson m->act_count = ACT_MAX; 1387bb7858eaSJeff Roberson } else { 138838efa82bSJohn Dyson m->act_count -= min(m->act_count, ACT_DECLINE); 1389bb7858eaSJeff Roberson act_delta = m->act_count; 1390bb7858eaSJeff Roberson } 1391bb7858eaSJeff Roberson 1392bb7858eaSJeff Roberson /* 1393bb7858eaSJeff Roberson * Move this page to the tail of the active or inactive 1394bb7858eaSJeff Roberson * queue depending on usage. 1395bb7858eaSJeff Roberson */ 1396bb7858eaSJeff Roberson if (act_delta == 0) { 13978d220203SAlan Cox /* Dequeue to avoid later lock recursion. */ 13988d220203SAlan Cox vm_page_dequeue_locked(m); 1399d4a272dbSJohn Dyson vm_page_deactivate(m); 1400bb7858eaSJeff Roberson page_shortage--; 14018d220203SAlan Cox } else 14028d220203SAlan Cox vm_page_requeue_locked(m); 14032965a453SKip Macy vm_page_unlock(m); 140426f9a767SRodney W. Grimes m = next; 140526f9a767SRodney W. Grimes } 14068d220203SAlan Cox vm_pagequeue_unlock(pq); 1407ceb0cf87SJohn Dyson #if !defined(NO_SWAPPING) 1408ceb0cf87SJohn Dyson /* 1409ceb0cf87SJohn Dyson * Idle process swapout -- run once per second. 1410ceb0cf87SJohn Dyson */ 1411ceb0cf87SJohn Dyson if (vm_swap_idle_enabled) { 1412ceb0cf87SJohn Dyson static long lsec; 1413227ee8a1SPoul-Henning Kamp if (time_second != lsec) { 141497824da3SAlan Cox vm_req_vmdaemon(VM_SWAP_IDLE); 1415227ee8a1SPoul-Henning Kamp lsec = time_second; 1416ceb0cf87SJohn Dyson } 1417ceb0cf87SJohn Dyson } 1418ceb0cf87SJohn Dyson #endif 1419ceb0cf87SJohn Dyson 14205663e6deSDavid Greenman /* 1421f6b04d2bSDavid Greenman * If we didn't get enough free pages, and we have skipped a vnode 14224c1f8ee9SDavid Greenman * in a writeable object, wakeup the sync daemon. And kick swapout 14234c1f8ee9SDavid Greenman * if we did not get enough free pages. 1424f6b04d2bSDavid Greenman */ 142590ecac61SMatthew Dillon if (vm_paging_target() > 0) { 142690ecac61SMatthew Dillon if (vnodes_skipped && vm_page_count_min()) 1427d50c1994SPeter Wemm (void) speedup_syncer(); 142838efa82bSJohn Dyson #if !defined(NO_SWAPPING) 142997824da3SAlan Cox if (vm_swap_enabled && vm_page_count_target()) 143097824da3SAlan Cox vm_req_vmdaemon(VM_SWAP_NORMAL); 14315afce282SDavid Greenman #endif 14324c1f8ee9SDavid Greenman } 14334c1f8ee9SDavid Greenman 1434f6b04d2bSDavid Greenman /* 1435e92686d0SDavid Schultz * If we are critically low on one of RAM or swap and low on 1436e92686d0SDavid Schultz * the other, kill the largest process. However, we avoid 1437e92686d0SDavid Schultz * doing this on the first pass in order to give ourselves a 1438e92686d0SDavid Schultz * chance to flush out dirty vnode-backed pages and to allow 1439e92686d0SDavid Schultz * active pages to be moved to the inactive queue and reclaimed. 14402025d69bSKonstantin Belousov */ 1441449c2e92SKonstantin Belousov vm_pageout_mightbe_oom(vmd, pass); 14422025d69bSKonstantin Belousov } 14432025d69bSKonstantin Belousov 1444449c2e92SKonstantin Belousov static int vm_pageout_oom_vote; 1445449c2e92SKonstantin Belousov 1446449c2e92SKonstantin Belousov /* 1447449c2e92SKonstantin Belousov * The pagedaemon threads randlomly select one to perform the 1448449c2e92SKonstantin Belousov * OOM. Trying to kill processes before all pagedaemons 1449449c2e92SKonstantin Belousov * failed to reach free target is premature. 1450449c2e92SKonstantin Belousov */ 1451449c2e92SKonstantin Belousov static void 1452449c2e92SKonstantin Belousov vm_pageout_mightbe_oom(struct vm_domain *vmd, int pass) 1453449c2e92SKonstantin Belousov { 1454449c2e92SKonstantin Belousov int old_vote; 1455449c2e92SKonstantin Belousov 1456d9e23210SJeff Roberson if (pass <= 1 || !((swap_pager_avail < 64 && vm_page_count_min()) || 1457449c2e92SKonstantin Belousov (swap_pager_full && vm_paging_target() > 0))) { 1458449c2e92SKonstantin Belousov if (vmd->vmd_oom) { 1459449c2e92SKonstantin Belousov vmd->vmd_oom = FALSE; 1460449c2e92SKonstantin Belousov atomic_subtract_int(&vm_pageout_oom_vote, 1); 1461449c2e92SKonstantin Belousov } 1462449c2e92SKonstantin Belousov return; 1463449c2e92SKonstantin Belousov } 1464449c2e92SKonstantin Belousov 1465449c2e92SKonstantin Belousov if (vmd->vmd_oom) 1466449c2e92SKonstantin Belousov return; 1467449c2e92SKonstantin Belousov 1468449c2e92SKonstantin Belousov vmd->vmd_oom = TRUE; 1469449c2e92SKonstantin Belousov old_vote = atomic_fetchadd_int(&vm_pageout_oom_vote, 1); 1470449c2e92SKonstantin Belousov if (old_vote != vm_ndomains - 1) 1471449c2e92SKonstantin Belousov return; 1472449c2e92SKonstantin Belousov 1473449c2e92SKonstantin Belousov /* 1474449c2e92SKonstantin Belousov * The current pagedaemon thread is the last in the quorum to 1475449c2e92SKonstantin Belousov * start OOM. Initiate the selection and signaling of the 1476449c2e92SKonstantin Belousov * victim. 1477449c2e92SKonstantin Belousov */ 1478449c2e92SKonstantin Belousov vm_pageout_oom(VM_OOM_MEM); 1479449c2e92SKonstantin Belousov 1480449c2e92SKonstantin Belousov /* 1481449c2e92SKonstantin Belousov * After one round of OOM terror, recall our vote. On the 1482449c2e92SKonstantin Belousov * next pass, current pagedaemon would vote again if the low 1483449c2e92SKonstantin Belousov * memory condition is still there, due to vmd_oom being 1484449c2e92SKonstantin Belousov * false. 1485449c2e92SKonstantin Belousov */ 1486449c2e92SKonstantin Belousov vmd->vmd_oom = FALSE; 1487449c2e92SKonstantin Belousov atomic_subtract_int(&vm_pageout_oom_vote, 1); 1488449c2e92SKonstantin Belousov } 14892025d69bSKonstantin Belousov 14902025d69bSKonstantin Belousov void 14912025d69bSKonstantin Belousov vm_pageout_oom(int shortage) 14922025d69bSKonstantin Belousov { 14932025d69bSKonstantin Belousov struct proc *p, *bigproc; 14942025d69bSKonstantin Belousov vm_offset_t size, bigsize; 14952025d69bSKonstantin Belousov struct thread *td; 14966bed074cSKonstantin Belousov struct vmspace *vm; 14972025d69bSKonstantin Belousov 14982025d69bSKonstantin Belousov /* 14991c58e4e5SJohn Baldwin * We keep the process bigproc locked once we find it to keep anyone 15001c58e4e5SJohn Baldwin * from messing with it; however, there is a possibility of 15011c58e4e5SJohn Baldwin * deadlock if process B is bigproc and one of it's child processes 15021c58e4e5SJohn Baldwin * attempts to propagate a signal to B while we are waiting for A's 15031c58e4e5SJohn Baldwin * lock while walking this list. To avoid this, we don't block on 15041c58e4e5SJohn Baldwin * the process lock but just skip a process if it is already locked. 15055663e6deSDavid Greenman */ 15065663e6deSDavid Greenman bigproc = NULL; 15075663e6deSDavid Greenman bigsize = 0; 15081005a129SJohn Baldwin sx_slock(&allproc_lock); 1509e602ba25SJulian Elischer FOREACH_PROC_IN_SYSTEM(p) { 1510e602ba25SJulian Elischer int breakout; 1511dcbcd518SBruce Evans 15121c58e4e5SJohn Baldwin if (PROC_TRYLOCK(p) == 0) 15131c58e4e5SJohn Baldwin continue; 15141c58e4e5SJohn Baldwin /* 15153f1c4c4fSKonstantin Belousov * If this is a system, protected or killed process, skip it. 15165663e6deSDavid Greenman */ 15178e6fa660SJohn Baldwin if (p->p_state != PRS_NORMAL || 15188e6fa660SJohn Baldwin (p->p_flag & (P_INEXEC | P_PROTECTED | P_SYSTEM)) || 15193f1c4c4fSKonstantin Belousov (p->p_pid == 1) || P_KILLED(p) || 15208f60c087SPoul-Henning Kamp ((p->p_pid < 48) && (swap_pager_avail != 0))) { 15218606d880SJohn Baldwin PROC_UNLOCK(p); 15225663e6deSDavid Greenman continue; 15235663e6deSDavid Greenman } 15245663e6deSDavid Greenman /* 1525dcbcd518SBruce Evans * If the process is in a non-running type state, 1526e602ba25SJulian Elischer * don't touch it. Check all the threads individually. 15275663e6deSDavid Greenman */ 1528e602ba25SJulian Elischer breakout = 0; 1529e602ba25SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 1530982d11f8SJeff Roberson thread_lock(td); 153171fad9fdSJulian Elischer if (!TD_ON_RUNQ(td) && 153271fad9fdSJulian Elischer !TD_IS_RUNNING(td) && 1533f497cda2SEdward Tomasz Napierala !TD_IS_SLEEPING(td) && 1534f497cda2SEdward Tomasz Napierala !TD_IS_SUSPENDED(td)) { 1535982d11f8SJeff Roberson thread_unlock(td); 1536e602ba25SJulian Elischer breakout = 1; 1537e602ba25SJulian Elischer break; 1538e602ba25SJulian Elischer } 1539982d11f8SJeff Roberson thread_unlock(td); 1540e602ba25SJulian Elischer } 1541e602ba25SJulian Elischer if (breakout) { 15421c58e4e5SJohn Baldwin PROC_UNLOCK(p); 15435663e6deSDavid Greenman continue; 15445663e6deSDavid Greenman } 15455663e6deSDavid Greenman /* 15465663e6deSDavid Greenman * get the process size 15475663e6deSDavid Greenman */ 15486bed074cSKonstantin Belousov vm = vmspace_acquire_ref(p); 15496bed074cSKonstantin Belousov if (vm == NULL) { 15506bed074cSKonstantin Belousov PROC_UNLOCK(p); 15516bed074cSKonstantin Belousov continue; 15526bed074cSKonstantin Belousov } 15536bed074cSKonstantin Belousov if (!vm_map_trylock_read(&vm->vm_map)) { 15546bed074cSKonstantin Belousov vmspace_free(vm); 155572d97679SDavid Schultz PROC_UNLOCK(p); 155672d97679SDavid Schultz continue; 155772d97679SDavid Schultz } 15587981aa24SKonstantin Belousov size = vmspace_swap_count(vm); 15596bed074cSKonstantin Belousov vm_map_unlock_read(&vm->vm_map); 15602025d69bSKonstantin Belousov if (shortage == VM_OOM_MEM) 15616bed074cSKonstantin Belousov size += vmspace_resident_count(vm); 15626bed074cSKonstantin Belousov vmspace_free(vm); 15635663e6deSDavid Greenman /* 15645663e6deSDavid Greenman * if the this process is bigger than the biggest one 15655663e6deSDavid Greenman * remember it. 15665663e6deSDavid Greenman */ 15675663e6deSDavid Greenman if (size > bigsize) { 15681c58e4e5SJohn Baldwin if (bigproc != NULL) 15691c58e4e5SJohn Baldwin PROC_UNLOCK(bigproc); 15705663e6deSDavid Greenman bigproc = p; 15715663e6deSDavid Greenman bigsize = size; 15721c58e4e5SJohn Baldwin } else 15731c58e4e5SJohn Baldwin PROC_UNLOCK(p); 15745663e6deSDavid Greenman } 15751005a129SJohn Baldwin sx_sunlock(&allproc_lock); 15765663e6deSDavid Greenman if (bigproc != NULL) { 1577729b1e51SDavid Greenman killproc(bigproc, "out of swap space"); 1578fa885116SJulian Elischer sched_nice(bigproc, PRIO_MIN); 15791c58e4e5SJohn Baldwin PROC_UNLOCK(bigproc); 15802feb50bfSAttilio Rao wakeup(&cnt.v_free_count); 15815663e6deSDavid Greenman } 15825663e6deSDavid Greenman } 158326f9a767SRodney W. Grimes 1584449c2e92SKonstantin Belousov static void 1585449c2e92SKonstantin Belousov vm_pageout_worker(void *arg) 1586449c2e92SKonstantin Belousov { 1587449c2e92SKonstantin Belousov struct vm_domain *domain; 1588949c9186SKonstantin Belousov int domidx; 1589449c2e92SKonstantin Belousov 1590449c2e92SKonstantin Belousov domidx = (uintptr_t)arg; 1591449c2e92SKonstantin Belousov domain = &vm_dom[domidx]; 1592449c2e92SKonstantin Belousov 1593449c2e92SKonstantin Belousov /* 1594949c9186SKonstantin Belousov * XXXKIB It could be useful to bind pageout daemon threads to 1595949c9186SKonstantin Belousov * the cores belonging to the domain, from which vm_page_array 1596949c9186SKonstantin Belousov * is allocated. 1597449c2e92SKonstantin Belousov */ 1598449c2e92SKonstantin Belousov 1599449c2e92SKonstantin Belousov KASSERT(domain->vmd_segs != 0, ("domain without segments")); 1600449c2e92SKonstantin Belousov vm_pageout_init_marker(&domain->vmd_marker, PQ_INACTIVE); 1601449c2e92SKonstantin Belousov 1602449c2e92SKonstantin Belousov /* 1603449c2e92SKonstantin Belousov * The pageout daemon worker is never done, so loop forever. 1604449c2e92SKonstantin Belousov */ 1605449c2e92SKonstantin Belousov while (TRUE) { 1606449c2e92SKonstantin Belousov /* 1607449c2e92SKonstantin Belousov * If we have enough free memory, wakeup waiters. Do 1608449c2e92SKonstantin Belousov * not clear vm_pages_needed until we reach our target, 1609449c2e92SKonstantin Belousov * otherwise we may be woken up over and over again and 1610449c2e92SKonstantin Belousov * waste a lot of cpu. 1611449c2e92SKonstantin Belousov */ 1612449c2e92SKonstantin Belousov mtx_lock(&vm_page_queue_free_mtx); 1613449c2e92SKonstantin Belousov if (vm_pages_needed && !vm_page_count_min()) { 1614449c2e92SKonstantin Belousov if (!vm_paging_needed()) 1615449c2e92SKonstantin Belousov vm_pages_needed = 0; 1616449c2e92SKonstantin Belousov wakeup(&cnt.v_free_count); 1617449c2e92SKonstantin Belousov } 1618449c2e92SKonstantin Belousov if (vm_pages_needed) { 1619449c2e92SKonstantin Belousov /* 1620449c2e92SKonstantin Belousov * Still not done, take a second pass without waiting 1621449c2e92SKonstantin Belousov * (unlimited dirty cleaning), otherwise sleep a bit 1622449c2e92SKonstantin Belousov * and try again. 1623449c2e92SKonstantin Belousov */ 1624449c2e92SKonstantin Belousov if (domain->vmd_pass > 1) 1625449c2e92SKonstantin Belousov msleep(&vm_pages_needed, 1626449c2e92SKonstantin Belousov &vm_page_queue_free_mtx, PVM, "psleep", 1627449c2e92SKonstantin Belousov hz / 2); 1628449c2e92SKonstantin Belousov } else { 1629449c2e92SKonstantin Belousov /* 1630d9e23210SJeff Roberson * Good enough, sleep until required to refresh 1631d9e23210SJeff Roberson * stats. 1632449c2e92SKonstantin Belousov */ 1633449c2e92SKonstantin Belousov domain->vmd_pass = 0; 1634d9e23210SJeff Roberson msleep(&vm_pages_needed, &vm_page_queue_free_mtx, 1635d9e23210SJeff Roberson PVM, "psleep", hz); 1636d9e23210SJeff Roberson 1637449c2e92SKonstantin Belousov } 1638d9e23210SJeff Roberson if (vm_pages_needed) { 1639449c2e92SKonstantin Belousov cnt.v_pdwakeups++; 1640d9e23210SJeff Roberson domain->vmd_pass++; 1641d9e23210SJeff Roberson } 1642449c2e92SKonstantin Belousov mtx_unlock(&vm_page_queue_free_mtx); 1643449c2e92SKonstantin Belousov vm_pageout_scan(domain, domain->vmd_pass); 1644449c2e92SKonstantin Belousov } 1645449c2e92SKonstantin Belousov } 1646449c2e92SKonstantin Belousov 1647df8bae1dSRodney W. Grimes /* 1648df8bae1dSRodney W. Grimes * vm_pageout is the high level pageout daemon. 1649df8bae1dSRodney W. Grimes */ 16502b14f991SJulian Elischer static void 16514a365329SAndrey Zonov vm_pageout(void) 1652df8bae1dSRodney W. Grimes { 1653449c2e92SKonstantin Belousov #if MAXMEMDOM > 1 1654449c2e92SKonstantin Belousov int error, i; 1655449c2e92SKonstantin Belousov #endif 16560384fff8SJason Evans 1657df8bae1dSRodney W. Grimes /* 1658df8bae1dSRodney W. Grimes * Initialize some paging parameters. 1659df8bae1dSRodney W. Grimes */ 16602feb50bfSAttilio Rao cnt.v_interrupt_free_min = 2; 16612feb50bfSAttilio Rao if (cnt.v_page_count < 2000) 1662f35329acSJohn Dyson vm_pageout_page_count = 8; 1663f6b04d2bSDavid Greenman 166445ae1d91SAlan Cox /* 166545ae1d91SAlan Cox * v_free_reserved needs to include enough for the largest 166645ae1d91SAlan Cox * swap pager structures plus enough for any pv_entry structs 166745ae1d91SAlan Cox * when paging. 166845ae1d91SAlan Cox */ 16692feb50bfSAttilio Rao if (cnt.v_page_count > 1024) 16702feb50bfSAttilio Rao cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200; 16712feb50bfSAttilio Rao else 16722feb50bfSAttilio Rao cnt.v_free_min = 4; 16732feb50bfSAttilio Rao cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE + 16742feb50bfSAttilio Rao cnt.v_interrupt_free_min; 16752feb50bfSAttilio Rao cnt.v_free_reserved = vm_pageout_page_count + 16762446e4f0SAlan Cox cnt.v_pageout_free_min + (cnt.v_page_count / 768); 16772feb50bfSAttilio Rao cnt.v_free_severe = cnt.v_free_min / 2; 1678d9e23210SJeff Roberson cnt.v_free_target = 4 * cnt.v_free_min + cnt.v_free_reserved; 16792feb50bfSAttilio Rao cnt.v_free_min += cnt.v_free_reserved; 16802feb50bfSAttilio Rao cnt.v_free_severe += cnt.v_free_reserved; 16812feb50bfSAttilio Rao cnt.v_inactive_target = (3 * cnt.v_free_target) / 2; 16822feb50bfSAttilio Rao if (cnt.v_inactive_target > cnt.v_free_count / 3) 16832feb50bfSAttilio Rao cnt.v_inactive_target = cnt.v_free_count / 3; 1684df8bae1dSRodney W. Grimes 1685d9e23210SJeff Roberson /* 1686d9e23210SJeff Roberson * Set the default wakeup threshold to be 10% above the minimum 1687d9e23210SJeff Roberson * page limit. This keeps the steady state out of shortfall. 1688d9e23210SJeff Roberson */ 1689d9e23210SJeff Roberson vm_pageout_wakeup_thresh = (cnt.v_free_min / 10) * 11; 1690d9e23210SJeff Roberson 1691d9e23210SJeff Roberson /* 1692d9e23210SJeff Roberson * Set interval in seconds for active scan. We want to visit each 1693c9612b2dSJeff Roberson * page at least once every ten minutes. This is to prevent worst 1694c9612b2dSJeff Roberson * case paging behaviors with stale active LRU. 1695d9e23210SJeff Roberson */ 1696d9e23210SJeff Roberson if (vm_pageout_update_period == 0) 1697c9612b2dSJeff Roberson vm_pageout_update_period = 600; 1698d9e23210SJeff Roberson 1699df8bae1dSRodney W. Grimes /* XXX does not really belong here */ 1700df8bae1dSRodney W. Grimes if (vm_page_max_wired == 0) 17012feb50bfSAttilio Rao vm_page_max_wired = cnt.v_free_count / 3; 1702df8bae1dSRodney W. Grimes 170324a1cce3SDavid Greenman swap_pager_swap_init(); 1704449c2e92SKonstantin Belousov #if MAXMEMDOM > 1 1705449c2e92SKonstantin Belousov for (i = 1; i < vm_ndomains; i++) { 1706449c2e92SKonstantin Belousov error = kthread_add(vm_pageout_worker, (void *)(uintptr_t)i, 1707449c2e92SKonstantin Belousov curproc, NULL, 0, 0, "dom%d", i); 1708449c2e92SKonstantin Belousov if (error != 0) { 1709449c2e92SKonstantin Belousov panic("starting pageout for domain %d, error %d\n", 1710449c2e92SKonstantin Belousov i, error); 1711dc2efb27SJohn Dyson } 1712f919ebdeSDavid Greenman } 1713449c2e92SKonstantin Belousov #endif 1714d395270dSDimitry Andric vm_pageout_worker((void *)(uintptr_t)0); 1715df8bae1dSRodney W. Grimes } 171626f9a767SRodney W. Grimes 17176b4b77adSAlan Cox /* 1718e9f995d8SAlan Cox * Unless the free page queue lock is held by the caller, this function 17196b4b77adSAlan Cox * should be regarded as advisory. Specifically, the caller should 17206b4b77adSAlan Cox * not msleep() on &cnt.v_free_count following this function unless 1721e9f995d8SAlan Cox * the free page queue lock is held until the msleep() is performed. 17226b4b77adSAlan Cox */ 1723e0c5a895SJohn Dyson void 17244a365329SAndrey Zonov pagedaemon_wakeup(void) 1725e0c5a895SJohn Dyson { 1726a1c0a785SAlan Cox 1727b40ce416SJulian Elischer if (!vm_pages_needed && curthread->td_proc != pageproc) { 1728a1c0a785SAlan Cox vm_pages_needed = 1; 1729e0c5a895SJohn Dyson wakeup(&vm_pages_needed); 1730e0c5a895SJohn Dyson } 1731e0c5a895SJohn Dyson } 1732e0c5a895SJohn Dyson 173338efa82bSJohn Dyson #if !defined(NO_SWAPPING) 17345afce282SDavid Greenman static void 173597824da3SAlan Cox vm_req_vmdaemon(int req) 17365afce282SDavid Greenman { 17375afce282SDavid Greenman static int lastrun = 0; 17385afce282SDavid Greenman 173997824da3SAlan Cox mtx_lock(&vm_daemon_mtx); 174097824da3SAlan Cox vm_pageout_req_swapout |= req; 1741b18bfc3dSJohn Dyson if ((ticks > (lastrun + hz)) || (ticks < lastrun)) { 17425afce282SDavid Greenman wakeup(&vm_daemon_needed); 17435afce282SDavid Greenman lastrun = ticks; 17445afce282SDavid Greenman } 174597824da3SAlan Cox mtx_unlock(&vm_daemon_mtx); 17465afce282SDavid Greenman } 17475afce282SDavid Greenman 17482b14f991SJulian Elischer static void 17494a365329SAndrey Zonov vm_daemon(void) 17500d94caffSDavid Greenman { 175191d5354aSJohn Baldwin struct rlimit rsslim; 1752dcbcd518SBruce Evans struct proc *p; 1753dcbcd518SBruce Evans struct thread *td; 17546bed074cSKonstantin Belousov struct vmspace *vm; 1755099e7e95SEdward Tomasz Napierala int breakout, swapout_flags, tryagain, attempts; 1756afcc55f3SEdward Tomasz Napierala #ifdef RACCT 1757099e7e95SEdward Tomasz Napierala uint64_t rsize, ravailable; 1758afcc55f3SEdward Tomasz Napierala #endif 17590d94caffSDavid Greenman 17602fe6e4d7SDavid Greenman while (TRUE) { 176197824da3SAlan Cox mtx_lock(&vm_daemon_mtx); 1762099e7e95SEdward Tomasz Napierala #ifdef RACCT 1763099e7e95SEdward Tomasz Napierala msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", hz); 1764099e7e95SEdward Tomasz Napierala #else 176597824da3SAlan Cox msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", 0); 1766099e7e95SEdward Tomasz Napierala #endif 176797824da3SAlan Cox swapout_flags = vm_pageout_req_swapout; 17684c1f8ee9SDavid Greenman vm_pageout_req_swapout = 0; 176997824da3SAlan Cox mtx_unlock(&vm_daemon_mtx); 177097824da3SAlan Cox if (swapout_flags) 177197824da3SAlan Cox swapout_procs(swapout_flags); 177297824da3SAlan Cox 17732fe6e4d7SDavid Greenman /* 17740d94caffSDavid Greenman * scan the processes for exceeding their rlimits or if 17750d94caffSDavid Greenman * process is swapped out -- deactivate pages 17762fe6e4d7SDavid Greenman */ 1777099e7e95SEdward Tomasz Napierala tryagain = 0; 1778099e7e95SEdward Tomasz Napierala attempts = 0; 1779099e7e95SEdward Tomasz Napierala again: 1780099e7e95SEdward Tomasz Napierala attempts++; 17811005a129SJohn Baldwin sx_slock(&allproc_lock); 1782f67af5c9SXin LI FOREACH_PROC_IN_SYSTEM(p) { 1783fe2144fdSLuoqi Chen vm_pindex_t limit, size; 17842fe6e4d7SDavid Greenman 17852fe6e4d7SDavid Greenman /* 17862fe6e4d7SDavid Greenman * if this is a system process or if we have already 17872fe6e4d7SDavid Greenman * looked at this process, skip it. 17882fe6e4d7SDavid Greenman */ 1789897ecacdSJohn Baldwin PROC_LOCK(p); 17908e6fa660SJohn Baldwin if (p->p_state != PRS_NORMAL || 17918e6fa660SJohn Baldwin p->p_flag & (P_INEXEC | P_SYSTEM | P_WEXIT)) { 1792897ecacdSJohn Baldwin PROC_UNLOCK(p); 17932fe6e4d7SDavid Greenman continue; 17942fe6e4d7SDavid Greenman } 17952fe6e4d7SDavid Greenman /* 17962fe6e4d7SDavid Greenman * if the process is in a non-running type state, 17972fe6e4d7SDavid Greenman * don't touch it. 17982fe6e4d7SDavid Greenman */ 1799e602ba25SJulian Elischer breakout = 0; 1800e602ba25SJulian Elischer FOREACH_THREAD_IN_PROC(p, td) { 1801982d11f8SJeff Roberson thread_lock(td); 180271fad9fdSJulian Elischer if (!TD_ON_RUNQ(td) && 180371fad9fdSJulian Elischer !TD_IS_RUNNING(td) && 1804f497cda2SEdward Tomasz Napierala !TD_IS_SLEEPING(td) && 1805f497cda2SEdward Tomasz Napierala !TD_IS_SUSPENDED(td)) { 1806982d11f8SJeff Roberson thread_unlock(td); 1807e602ba25SJulian Elischer breakout = 1; 1808e602ba25SJulian Elischer break; 1809e602ba25SJulian Elischer } 1810982d11f8SJeff Roberson thread_unlock(td); 1811e602ba25SJulian Elischer } 1812897ecacdSJohn Baldwin if (breakout) { 1813897ecacdSJohn Baldwin PROC_UNLOCK(p); 18142fe6e4d7SDavid Greenman continue; 18152fe6e4d7SDavid Greenman } 18162fe6e4d7SDavid Greenman /* 18172fe6e4d7SDavid Greenman * get a limit 18182fe6e4d7SDavid Greenman */ 1819dcbcd518SBruce Evans lim_rlimit(p, RLIMIT_RSS, &rsslim); 1820fe2144fdSLuoqi Chen limit = OFF_TO_IDX( 182191d5354aSJohn Baldwin qmin(rsslim.rlim_cur, rsslim.rlim_max)); 18222fe6e4d7SDavid Greenman 18232fe6e4d7SDavid Greenman /* 18240d94caffSDavid Greenman * let processes that are swapped out really be 18250d94caffSDavid Greenman * swapped out set the limit to nothing (will force a 18260d94caffSDavid Greenman * swap-out.) 18272fe6e4d7SDavid Greenman */ 1828b61ce5b0SJeff Roberson if ((p->p_flag & P_INMEM) == 0) 18290d94caffSDavid Greenman limit = 0; /* XXX */ 18306bed074cSKonstantin Belousov vm = vmspace_acquire_ref(p); 1831897ecacdSJohn Baldwin PROC_UNLOCK(p); 18326bed074cSKonstantin Belousov if (vm == NULL) 18336bed074cSKonstantin Belousov continue; 18342fe6e4d7SDavid Greenman 18356bed074cSKonstantin Belousov size = vmspace_resident_count(vm); 1836a406d8c3SEdward Tomasz Napierala if (size >= limit) { 1837fe2144fdSLuoqi Chen vm_pageout_map_deactivate_pages( 18386bed074cSKonstantin Belousov &vm->vm_map, limit); 18392fe6e4d7SDavid Greenman } 1840afcc55f3SEdward Tomasz Napierala #ifdef RACCT 1841099e7e95SEdward Tomasz Napierala rsize = IDX_TO_OFF(size); 1842099e7e95SEdward Tomasz Napierala PROC_LOCK(p); 1843099e7e95SEdward Tomasz Napierala racct_set(p, RACCT_RSS, rsize); 1844099e7e95SEdward Tomasz Napierala ravailable = racct_get_available(p, RACCT_RSS); 1845099e7e95SEdward Tomasz Napierala PROC_UNLOCK(p); 1846099e7e95SEdward Tomasz Napierala if (rsize > ravailable) { 1847099e7e95SEdward Tomasz Napierala /* 1848099e7e95SEdward Tomasz Napierala * Don't be overly aggressive; this might be 1849099e7e95SEdward Tomasz Napierala * an innocent process, and the limit could've 1850099e7e95SEdward Tomasz Napierala * been exceeded by some memory hog. Don't 1851099e7e95SEdward Tomasz Napierala * try to deactivate more than 1/4th of process' 1852099e7e95SEdward Tomasz Napierala * resident set size. 1853099e7e95SEdward Tomasz Napierala */ 1854099e7e95SEdward Tomasz Napierala if (attempts <= 8) { 1855099e7e95SEdward Tomasz Napierala if (ravailable < rsize - (rsize / 4)) 1856099e7e95SEdward Tomasz Napierala ravailable = rsize - (rsize / 4); 1857099e7e95SEdward Tomasz Napierala } 1858099e7e95SEdward Tomasz Napierala vm_pageout_map_deactivate_pages( 1859099e7e95SEdward Tomasz Napierala &vm->vm_map, OFF_TO_IDX(ravailable)); 1860099e7e95SEdward Tomasz Napierala /* Update RSS usage after paging out. */ 1861099e7e95SEdward Tomasz Napierala size = vmspace_resident_count(vm); 1862099e7e95SEdward Tomasz Napierala rsize = IDX_TO_OFF(size); 1863099e7e95SEdward Tomasz Napierala PROC_LOCK(p); 1864099e7e95SEdward Tomasz Napierala racct_set(p, RACCT_RSS, rsize); 1865099e7e95SEdward Tomasz Napierala PROC_UNLOCK(p); 1866099e7e95SEdward Tomasz Napierala if (rsize > ravailable) 1867099e7e95SEdward Tomasz Napierala tryagain = 1; 1868099e7e95SEdward Tomasz Napierala } 1869afcc55f3SEdward Tomasz Napierala #endif 18706bed074cSKonstantin Belousov vmspace_free(vm); 18712fe6e4d7SDavid Greenman } 18721005a129SJohn Baldwin sx_sunlock(&allproc_lock); 1873099e7e95SEdward Tomasz Napierala if (tryagain != 0 && attempts <= 10) 1874099e7e95SEdward Tomasz Napierala goto again; 187524a1cce3SDavid Greenman } 18762fe6e4d7SDavid Greenman } 1877a1287949SEivind Eklund #endif /* !defined(NO_SWAPPING) */ 1878