160727d8bSWarner Losh /*- 21c7c3c6aSMatthew Dillon * Copyright (c) 1998 Matthew Dillon, 326f9a767SRodney W. Grimes * Copyright (c) 1994 John S. Dyson 4df8bae1dSRodney W. Grimes * Copyright (c) 1990 University of Utah. 5e9c0cc15SPoul-Henning Kamp * Copyright (c) 1982, 1986, 1989, 1993 6df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 7df8bae1dSRodney W. Grimes * 8df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 9df8bae1dSRodney W. Grimes * the Systems Programming Group of the University of Utah Computer 10df8bae1dSRodney W. Grimes * Science Department. 11df8bae1dSRodney W. Grimes * 12df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 13df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 14df8bae1dSRodney W. Grimes * are met: 15df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 17df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 18df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 19df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 20df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 215929bcfaSPhilippe Charnier * must display the following acknowledgement: 22df8bae1dSRodney W. Grimes * This product includes software developed by the University of 23df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 24df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 25df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 26df8bae1dSRodney W. Grimes * without specific prior written permission. 27df8bae1dSRodney W. Grimes * 28df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38df8bae1dSRodney W. Grimes * SUCH DAMAGE. 39df8bae1dSRodney W. Grimes * 401c7c3c6aSMatthew Dillon * New Swap System 411c7c3c6aSMatthew Dillon * Matthew Dillon 421c7c3c6aSMatthew Dillon * 431c7c3c6aSMatthew Dillon * Radix Bitmap 'blists'. 441c7c3c6aSMatthew Dillon * 451c7c3c6aSMatthew Dillon * - The new swapper uses the new radix bitmap code. This should scale 461c7c3c6aSMatthew Dillon * to arbitrarily small or arbitrarily large swap spaces and an almost 471c7c3c6aSMatthew Dillon * arbitrary degree of fragmentation. 481c7c3c6aSMatthew Dillon * 491c7c3c6aSMatthew Dillon * Features: 501c7c3c6aSMatthew Dillon * 511c7c3c6aSMatthew Dillon * - on the fly reallocation of swap during putpages. The new system 521c7c3c6aSMatthew Dillon * does not try to keep previously allocated swap blocks for dirty 531c7c3c6aSMatthew Dillon * pages. 541c7c3c6aSMatthew Dillon * 551c7c3c6aSMatthew Dillon * - on the fly deallocation of swap 561c7c3c6aSMatthew Dillon * 571c7c3c6aSMatthew Dillon * - No more garbage collection required. Unnecessarily allocated swap 581c7c3c6aSMatthew Dillon * blocks only exist for dirty vm_page_t's now and these are already 591c7c3c6aSMatthew Dillon * cycled (in a high-load system) by the pager. We also do on-the-fly 601c7c3c6aSMatthew Dillon * removal of invalidated swap blocks when a page is destroyed 611c7c3c6aSMatthew Dillon * or renamed. 621c7c3c6aSMatthew Dillon * 63df8bae1dSRodney W. Grimes * from: Utah $Hdr: swap_pager.c 1.4 91/04/30$ 64df8bae1dSRodney W. Grimes * 65df8bae1dSRodney W. Grimes * @(#)swap_pager.c 8.9 (Berkeley) 3/21/94 66e9c0cc15SPoul-Henning Kamp * @(#)vm_swap.c 8.5 (Berkeley) 2/17/94 67df8bae1dSRodney W. Grimes */ 68df8bae1dSRodney W. Grimes 69874651b1SDavid E. O'Brien #include <sys/cdefs.h> 70874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 71874651b1SDavid E. O'Brien 72e9c0cc15SPoul-Henning Kamp #include "opt_swap.h" 73e9c0cc15SPoul-Henning Kamp #include "opt_vm.h" 74e9c0cc15SPoul-Henning Kamp 75df8bae1dSRodney W. Grimes #include <sys/param.h> 76df8bae1dSRodney W. Grimes #include <sys/systm.h> 77af647ddeSBruce Evans #include <sys/conf.h> 7864abb5a5SDavid Greenman #include <sys/kernel.h> 79acd3428bSRobert Watson #include <sys/priv.h> 80df8bae1dSRodney W. Grimes #include <sys/proc.h> 819626b608SPoul-Henning Kamp #include <sys/bio.h> 82df8bae1dSRodney W. Grimes #include <sys/buf.h> 83e9c0cc15SPoul-Henning Kamp #include <sys/disk.h> 84e9c0cc15SPoul-Henning Kamp #include <sys/fcntl.h> 85e9c0cc15SPoul-Henning Kamp #include <sys/mount.h> 86e9c0cc15SPoul-Henning Kamp #include <sys/namei.h> 87df8bae1dSRodney W. Grimes #include <sys/vnode.h> 88df8bae1dSRodney W. Grimes #include <sys/malloc.h> 891ba5ad42SEdward Tomasz Napierala #include <sys/racct.h> 903364c323SKonstantin Belousov #include <sys/resource.h> 913364c323SKonstantin Belousov #include <sys/resourcevar.h> 9289f6b863SAttilio Rao #include <sys/rwlock.h> 93327f4e83SMatthew Dillon #include <sys/sysctl.h> 94e9c0cc15SPoul-Henning Kamp #include <sys/sysproto.h> 951c7c3c6aSMatthew Dillon #include <sys/blist.h> 961c7c3c6aSMatthew Dillon #include <sys/lock.h> 970cddd8f0SMatthew Dillon #include <sys/sx.h> 98936524aaSMatthew Dillon #include <sys/vmmeter.h> 99df8bae1dSRodney W. Grimes 100aed55708SRobert Watson #include <security/mac/mac_framework.h> 101aed55708SRobert Watson 102df8bae1dSRodney W. Grimes #include <vm/vm.h> 10321cd6e62SSeigo Tanimura #include <vm/pmap.h> 10421cd6e62SSeigo Tanimura #include <vm/vm_map.h> 10521cd6e62SSeigo Tanimura #include <vm/vm_kern.h> 106efeaf95aSDavid Greenman #include <vm/vm_object.h> 107df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 108efeaf95aSDavid Greenman #include <vm/vm_pager.h> 109df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h> 110e9c0cc15SPoul-Henning Kamp #include <vm/vm_param.h> 111df8bae1dSRodney W. Grimes #include <vm/swap_pager.h> 112efeaf95aSDavid Greenman #include <vm/vm_extern.h> 113670d17b5SJeff Roberson #include <vm/uma.h> 114df8bae1dSRodney W. Grimes 115dee34ca4SPoul-Henning Kamp #include <geom/geom.h> 116dee34ca4SPoul-Henning Kamp 117ec38b344SPoul-Henning Kamp /* 11815523cf7SKonstantin Belousov * SWB_NPAGES must be a power of 2. It may be set to 1, 2, 4, 8, 16 11915523cf7SKonstantin Belousov * or 32 pages per allocation. 12015523cf7SKonstantin Belousov * The 32-page limit is due to the radix code (kern/subr_blist.c). 121ec38b344SPoul-Henning Kamp */ 122ec38b344SPoul-Henning Kamp #ifndef MAX_PAGEOUT_CLUSTER 123ec38b344SPoul-Henning Kamp #define MAX_PAGEOUT_CLUSTER 16 124ec38b344SPoul-Henning Kamp #endif 125ec38b344SPoul-Henning Kamp 126ec38b344SPoul-Henning Kamp #if !defined(SWB_NPAGES) 127ec38b344SPoul-Henning Kamp #define SWB_NPAGES MAX_PAGEOUT_CLUSTER 128ec38b344SPoul-Henning Kamp #endif 129ec38b344SPoul-Henning Kamp 130ec38b344SPoul-Henning Kamp /* 13115523cf7SKonstantin Belousov * The swblock structure maps an object and a small, fixed-size range 13215523cf7SKonstantin Belousov * of page indices to disk addresses within a swap area. 13315523cf7SKonstantin Belousov * The collection of these mappings is implemented as a hash table. 13415523cf7SKonstantin Belousov * Unused disk addresses within a swap area are allocated and managed 13515523cf7SKonstantin Belousov * using a blist. 136ec38b344SPoul-Henning Kamp */ 137ec38b344SPoul-Henning Kamp #define SWCORRECT(n) (sizeof(void *) * (n) / sizeof(daddr_t)) 138ec38b344SPoul-Henning Kamp #define SWAP_META_PAGES (SWB_NPAGES * 2) 139ec38b344SPoul-Henning Kamp #define SWAP_META_MASK (SWAP_META_PAGES - 1) 140ec38b344SPoul-Henning Kamp 141e9c0cc15SPoul-Henning Kamp struct swblock { 142e9c0cc15SPoul-Henning Kamp struct swblock *swb_hnext; 143e9c0cc15SPoul-Henning Kamp vm_object_t swb_object; 144e9c0cc15SPoul-Henning Kamp vm_pindex_t swb_index; 145e9c0cc15SPoul-Henning Kamp int swb_count; 146e9c0cc15SPoul-Henning Kamp daddr_t swb_pages[SWAP_META_PAGES]; 147e9c0cc15SPoul-Henning Kamp }; 148e9c0cc15SPoul-Henning Kamp 1492c4992dbSAlan Cox static MALLOC_DEFINE(M_VMPGDATA, "vm_pgdata", "swap pager private data"); 15020da9c2eSPoul-Henning Kamp static struct mtx sw_dev_mtx; 1518f60c087SPoul-Henning Kamp static TAILQ_HEAD(, swdevt) swtailq = TAILQ_HEAD_INITIALIZER(swtailq); 1528f60c087SPoul-Henning Kamp static struct swdevt *swdevhd; /* Allocate from here next */ 1538f60c087SPoul-Henning Kamp static int nswapdev; /* Number of swap devices */ 1548f60c087SPoul-Henning Kamp int swap_pager_avail; 15504533e1eSKonstantin Belousov static struct sx swdev_syscall_lock; /* serialize swap(on|off) */ 156e9c0cc15SPoul-Henning Kamp 1573364c323SKonstantin Belousov static vm_ooffset_t swap_total; 1588a9c731fSIvan Voras SYSCTL_QUAD(_vm, OID_AUTO, swap_total, CTLFLAG_RD, &swap_total, 0, 1598a9c731fSIvan Voras "Total amount of available swap storage."); 1603364c323SKonstantin Belousov static vm_ooffset_t swap_reserved; 1618a9c731fSIvan Voras SYSCTL_QUAD(_vm, OID_AUTO, swap_reserved, CTLFLAG_RD, &swap_reserved, 0, 1628a9c731fSIvan Voras "Amount of swap storage needed to back all allocated anonymous memory."); 1633364c323SKonstantin Belousov static int overcommit = 0; 1648a9c731fSIvan Voras SYSCTL_INT(_vm, OID_AUTO, overcommit, CTLFLAG_RW, &overcommit, 0, 1658a9c731fSIvan Voras "Configure virtual memory overcommit behavior. See tuning(7) " 1668a9c731fSIvan Voras "for details."); 16761203277SDag-Erling Smørgrav static unsigned long swzone; 16861203277SDag-Erling Smørgrav SYSCTL_ULONG(_vm, OID_AUTO, swzone, CTLFLAG_RD, &swzone, 0, 16961203277SDag-Erling Smørgrav "Actual size of swap metadata zone"); 17061203277SDag-Erling Smørgrav static unsigned long swap_maxpages; 17161203277SDag-Erling Smørgrav SYSCTL_ULONG(_vm, OID_AUTO, swap_maxpages, CTLFLAG_RD, &swap_maxpages, 0, 17261203277SDag-Erling Smørgrav "Maximum amount of swap supported"); 1733364c323SKonstantin Belousov 1743364c323SKonstantin Belousov /* bits from overcommit */ 1753364c323SKonstantin Belousov #define SWAP_RESERVE_FORCE_ON (1 << 0) 1763364c323SKonstantin Belousov #define SWAP_RESERVE_RLIMIT_ON (1 << 1) 1773364c323SKonstantin Belousov #define SWAP_RESERVE_ALLOW_NONWIRED (1 << 2) 1783364c323SKonstantin Belousov 1793364c323SKonstantin Belousov int 1803364c323SKonstantin Belousov swap_reserve(vm_ooffset_t incr) 1813364c323SKonstantin Belousov { 1823364c323SKonstantin Belousov 183ef694c1aSEdward Tomasz Napierala return (swap_reserve_by_cred(incr, curthread->td_ucred)); 1843364c323SKonstantin Belousov } 1853364c323SKonstantin Belousov 1863364c323SKonstantin Belousov int 187ef694c1aSEdward Tomasz Napierala swap_reserve_by_cred(vm_ooffset_t incr, struct ucred *cred) 1883364c323SKonstantin Belousov { 1895c0e1c11SKonstantin Belousov vm_ooffset_t r, s; 1903364c323SKonstantin Belousov int res, error; 1913364c323SKonstantin Belousov static int curfail; 1923364c323SKonstantin Belousov static struct timeval lastfail; 193ef694c1aSEdward Tomasz Napierala struct uidinfo *uip; 194ef694c1aSEdward Tomasz Napierala 195ef694c1aSEdward Tomasz Napierala uip = cred->cr_ruidinfo; 1963364c323SKonstantin Belousov 1973364c323SKonstantin Belousov if (incr & PAGE_MASK) 1983364c323SKonstantin Belousov panic("swap_reserve: & PAGE_MASK"); 1993364c323SKonstantin Belousov 200afcc55f3SEdward Tomasz Napierala #ifdef RACCT 2014b5c9cf6SEdward Tomasz Napierala if (racct_enable) { 2021ba5ad42SEdward Tomasz Napierala PROC_LOCK(curproc); 2031ba5ad42SEdward Tomasz Napierala error = racct_add(curproc, RACCT_SWAP, incr); 2041ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(curproc); 2051ba5ad42SEdward Tomasz Napierala if (error != 0) 2061ba5ad42SEdward Tomasz Napierala return (0); 2074b5c9cf6SEdward Tomasz Napierala } 208afcc55f3SEdward Tomasz Napierala #endif 2091ba5ad42SEdward Tomasz Napierala 2103364c323SKonstantin Belousov res = 0; 2113364c323SKonstantin Belousov mtx_lock(&sw_dev_mtx); 2123364c323SKonstantin Belousov r = swap_reserved + incr; 2133364c323SKonstantin Belousov if (overcommit & SWAP_RESERVE_ALLOW_NONWIRED) { 21444f1c916SBryan Drewery s = vm_cnt.v_page_count - vm_cnt.v_free_reserved - vm_cnt.v_wire_count; 2153364c323SKonstantin Belousov s *= PAGE_SIZE; 2163364c323SKonstantin Belousov } else 2173364c323SKonstantin Belousov s = 0; 2183364c323SKonstantin Belousov s += swap_total; 2193364c323SKonstantin Belousov if ((overcommit & SWAP_RESERVE_FORCE_ON) == 0 || r <= s || 2203364c323SKonstantin Belousov (error = priv_check(curthread, PRIV_VM_SWAP_NOQUOTA)) == 0) { 2213364c323SKonstantin Belousov res = 1; 2223364c323SKonstantin Belousov swap_reserved = r; 2233364c323SKonstantin Belousov } 2243364c323SKonstantin Belousov mtx_unlock(&sw_dev_mtx); 2253364c323SKonstantin Belousov 2263364c323SKonstantin Belousov if (res) { 2273364c323SKonstantin Belousov UIDINFO_VMSIZE_LOCK(uip); 2285c0e1c11SKonstantin Belousov if ((overcommit & SWAP_RESERVE_RLIMIT_ON) != 0 && 229f6f6d240SMateusz Guzik uip->ui_vmsize + incr > lim_cur(curthread, RLIMIT_SWAP) && 2305c0e1c11SKonstantin Belousov priv_check(curthread, PRIV_VM_SWAP_NORLIMIT)) 2313364c323SKonstantin Belousov res = 0; 2323364c323SKonstantin Belousov else 2333364c323SKonstantin Belousov uip->ui_vmsize += incr; 2343364c323SKonstantin Belousov UIDINFO_VMSIZE_UNLOCK(uip); 2353364c323SKonstantin Belousov if (!res) { 2363364c323SKonstantin Belousov mtx_lock(&sw_dev_mtx); 2373364c323SKonstantin Belousov swap_reserved -= incr; 2383364c323SKonstantin Belousov mtx_unlock(&sw_dev_mtx); 2393364c323SKonstantin Belousov } 2403364c323SKonstantin Belousov } 2413364c323SKonstantin Belousov if (!res && ppsratecheck(&lastfail, &curfail, 1)) { 2423364c323SKonstantin Belousov printf("uid %d, pid %d: swap reservation for %jd bytes failed\n", 243134465d7SKonstantin Belousov uip->ui_uid, curproc->p_pid, incr); 2443364c323SKonstantin Belousov } 2453364c323SKonstantin Belousov 246afcc55f3SEdward Tomasz Napierala #ifdef RACCT 2471ba5ad42SEdward Tomasz Napierala if (!res) { 2481ba5ad42SEdward Tomasz Napierala PROC_LOCK(curproc); 2491ba5ad42SEdward Tomasz Napierala racct_sub(curproc, RACCT_SWAP, incr); 2501ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(curproc); 2511ba5ad42SEdward Tomasz Napierala } 252afcc55f3SEdward Tomasz Napierala #endif 2531ba5ad42SEdward Tomasz Napierala 2543364c323SKonstantin Belousov return (res); 2553364c323SKonstantin Belousov } 2563364c323SKonstantin Belousov 2573364c323SKonstantin Belousov void 2583364c323SKonstantin Belousov swap_reserve_force(vm_ooffset_t incr) 2593364c323SKonstantin Belousov { 2603364c323SKonstantin Belousov struct uidinfo *uip; 2613364c323SKonstantin Belousov 2623364c323SKonstantin Belousov mtx_lock(&sw_dev_mtx); 2633364c323SKonstantin Belousov swap_reserved += incr; 2643364c323SKonstantin Belousov mtx_unlock(&sw_dev_mtx); 2653364c323SKonstantin Belousov 266afcc55f3SEdward Tomasz Napierala #ifdef RACCT 2671ba5ad42SEdward Tomasz Napierala PROC_LOCK(curproc); 2681ba5ad42SEdward Tomasz Napierala racct_add_force(curproc, RACCT_SWAP, incr); 2691ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(curproc); 270afcc55f3SEdward Tomasz Napierala #endif 2711ba5ad42SEdward Tomasz Napierala 2723364c323SKonstantin Belousov uip = curthread->td_ucred->cr_ruidinfo; 2733364c323SKonstantin Belousov PROC_LOCK(curproc); 2743364c323SKonstantin Belousov UIDINFO_VMSIZE_LOCK(uip); 2753364c323SKonstantin Belousov uip->ui_vmsize += incr; 2763364c323SKonstantin Belousov UIDINFO_VMSIZE_UNLOCK(uip); 2773364c323SKonstantin Belousov PROC_UNLOCK(curproc); 2783364c323SKonstantin Belousov } 2793364c323SKonstantin Belousov 2803364c323SKonstantin Belousov void 2813364c323SKonstantin Belousov swap_release(vm_ooffset_t decr) 2823364c323SKonstantin Belousov { 283ef694c1aSEdward Tomasz Napierala struct ucred *cred; 2843364c323SKonstantin Belousov 2853364c323SKonstantin Belousov PROC_LOCK(curproc); 286ef694c1aSEdward Tomasz Napierala cred = curthread->td_ucred; 287ef694c1aSEdward Tomasz Napierala swap_release_by_cred(decr, cred); 2883364c323SKonstantin Belousov PROC_UNLOCK(curproc); 2893364c323SKonstantin Belousov } 2903364c323SKonstantin Belousov 2913364c323SKonstantin Belousov void 292ef694c1aSEdward Tomasz Napierala swap_release_by_cred(vm_ooffset_t decr, struct ucred *cred) 2933364c323SKonstantin Belousov { 294ef694c1aSEdward Tomasz Napierala struct uidinfo *uip; 295ef694c1aSEdward Tomasz Napierala 296ef694c1aSEdward Tomasz Napierala uip = cred->cr_ruidinfo; 2973364c323SKonstantin Belousov 2983364c323SKonstantin Belousov if (decr & PAGE_MASK) 2993364c323SKonstantin Belousov panic("swap_release: & PAGE_MASK"); 3003364c323SKonstantin Belousov 3013364c323SKonstantin Belousov mtx_lock(&sw_dev_mtx); 3023364c323SKonstantin Belousov if (swap_reserved < decr) 3033364c323SKonstantin Belousov panic("swap_reserved < decr"); 3043364c323SKonstantin Belousov swap_reserved -= decr; 3053364c323SKonstantin Belousov mtx_unlock(&sw_dev_mtx); 3063364c323SKonstantin Belousov 3073364c323SKonstantin Belousov UIDINFO_VMSIZE_LOCK(uip); 3083364c323SKonstantin Belousov if (uip->ui_vmsize < decr) 3093364c323SKonstantin Belousov printf("negative vmsize for uid = %d\n", uip->ui_uid); 3103364c323SKonstantin Belousov uip->ui_vmsize -= decr; 3113364c323SKonstantin Belousov UIDINFO_VMSIZE_UNLOCK(uip); 3121ba5ad42SEdward Tomasz Napierala 3131ba5ad42SEdward Tomasz Napierala racct_sub_cred(cred, RACCT_SWAP, decr); 3143364c323SKonstantin Belousov } 3153364c323SKonstantin Belousov 3161c7c3c6aSMatthew Dillon #define SWM_FREE 0x02 /* free, period */ 3171c7c3c6aSMatthew Dillon #define SWM_POP 0x04 /* pop out */ 31826f9a767SRodney W. Grimes 3197dea2c2eSAlan Cox int swap_pager_full = 2; /* swap space exhaustion (task killing) */ 3207dea2c2eSAlan Cox static int swap_pager_almost_full = 1; /* swap space exhaustion (w/hysteresis)*/ 3211c7c3c6aSMatthew Dillon static int nsw_rcount; /* free read buffers */ 322327f4e83SMatthew Dillon static int nsw_wcount_sync; /* limit write buffers / synchronous */ 323327f4e83SMatthew Dillon static int nsw_wcount_async; /* limit write buffers / asynchronous */ 324327f4e83SMatthew Dillon static int nsw_wcount_async_max;/* assigned maximum */ 325327f4e83SMatthew Dillon static int nsw_cluster_max; /* maximum VOP I/O allowed */ 3261c7c3c6aSMatthew Dillon 32789c241d1SGleb Smirnoff static int sysctl_swap_async_max(SYSCTL_HANDLER_ARGS); 3284c36e917SKonstantin Belousov SYSCTL_PROC(_vm, OID_AUTO, swap_async_max, CTLTYPE_INT | CTLFLAG_RW | 3294c36e917SKonstantin Belousov CTLFLAG_MPSAFE, NULL, 0, sysctl_swap_async_max, "I", 3304c36e917SKonstantin Belousov "Maximum running async swap ops"); 33189c241d1SGleb Smirnoff 3321c7c3c6aSMatthew Dillon static struct swblock **swhash; 3331c7c3c6aSMatthew Dillon static int swhash_mask; 3347827d9b0SAlan Cox static struct mtx swhash_mtx; 3357827d9b0SAlan Cox 3360cddd8f0SMatthew Dillon static struct sx sw_alloc_sx; 337327f4e83SMatthew Dillon 3381c7c3c6aSMatthew Dillon /* 3391c7c3c6aSMatthew Dillon * "named" and "unnamed" anon region objects. Try to reduce the overhead 3401c7c3c6aSMatthew Dillon * of searching a named list by hashing it just a little. 3411c7c3c6aSMatthew Dillon */ 3421c7c3c6aSMatthew Dillon 3431c7c3c6aSMatthew Dillon #define NOBJLISTS 8 3441c7c3c6aSMatthew Dillon 3451c7c3c6aSMatthew Dillon #define NOBJLIST(handle) \ 346af647ddeSBruce Evans (&swap_pager_object_list[((int)(intptr_t)handle >> 4) & (NOBJLISTS-1)]) 3471c7c3c6aSMatthew Dillon 3481c7c3c6aSMatthew Dillon static struct pagerlst swap_pager_object_list[NOBJLISTS]; 349e9c0cc15SPoul-Henning Kamp static uma_zone_t swap_zone; 3501c7c3c6aSMatthew Dillon 3511c7c3c6aSMatthew Dillon /* 3521c7c3c6aSMatthew Dillon * pagerops for OBJT_SWAP - "swap pager". Some ops are also global procedure 3531c7c3c6aSMatthew Dillon * calls hooked from other parts of the VM system and do not appear here. 3541c7c3c6aSMatthew Dillon * (see vm/swap_pager.h). 3551c7c3c6aSMatthew Dillon */ 356ff98689dSBruce Evans static vm_object_t 35711caded3SAlfred Perlstein swap_pager_alloc(void *handle, vm_ooffset_t size, 3583364c323SKonstantin Belousov vm_prot_t prot, vm_ooffset_t offset, struct ucred *); 35911caded3SAlfred Perlstein static void swap_pager_dealloc(vm_object_t object); 360b0cd2017SGleb Smirnoff static int swap_pager_getpages(vm_object_t, vm_page_t *, int, int *, 361b0cd2017SGleb Smirnoff int *); 362b0cd2017SGleb Smirnoff static int swap_pager_getpages_async(vm_object_t, vm_page_t *, int, int *, 363b0cd2017SGleb Smirnoff int *, pgo_getpages_iodone_t, void *); 364751221fdSPoul-Henning Kamp static void swap_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *); 3655ea4972cSAlan Cox static boolean_t 3665ea4972cSAlan Cox swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, int *after); 36711caded3SAlfred Perlstein static void swap_pager_init(void); 36811caded3SAlfred Perlstein static void swap_pager_unswapped(vm_page_t); 369b3fed13eSDavid Schultz static void swap_pager_swapoff(struct swdevt *sp); 370f708ef1bSPoul-Henning Kamp 371df8bae1dSRodney W. Grimes struct pagerops swappagerops = { 372e04e4bacSPoul-Henning Kamp .pgo_init = swap_pager_init, /* early system initialization of pager */ 373e04e4bacSPoul-Henning Kamp .pgo_alloc = swap_pager_alloc, /* allocate an OBJT_SWAP object */ 374e04e4bacSPoul-Henning Kamp .pgo_dealloc = swap_pager_dealloc, /* deallocate an OBJT_SWAP object */ 375e04e4bacSPoul-Henning Kamp .pgo_getpages = swap_pager_getpages, /* pagein */ 37690effb23SGleb Smirnoff .pgo_getpages_async = swap_pager_getpages_async, /* pagein (async) */ 377e04e4bacSPoul-Henning Kamp .pgo_putpages = swap_pager_putpages, /* pageout */ 378e04e4bacSPoul-Henning Kamp .pgo_haspage = swap_pager_haspage, /* get backing store status for page */ 379e04e4bacSPoul-Henning Kamp .pgo_pageunswapped = swap_pager_unswapped, /* remove swap related to page */ 380df8bae1dSRodney W. Grimes }; 381df8bae1dSRodney W. Grimes 3821c7c3c6aSMatthew Dillon /* 3831c7c3c6aSMatthew Dillon * dmmax is in page-sized chunks with the new swap system. It was 38464bcb9c8SMatthew Dillon * dev-bsized chunks in the old. dmmax is always a power of 2. 3851c7c3c6aSMatthew Dillon * 3861c7c3c6aSMatthew Dillon * swap_*() routines are externally accessible. swp_*() routines are 3871c7c3c6aSMatthew Dillon * internal. 3881c7c3c6aSMatthew Dillon */ 389751221fdSPoul-Henning Kamp static int dmmax; 390e9c0cc15SPoul-Henning Kamp static int nswap_lowat = 128; /* in pages, swap_pager_almost_full warn */ 391e9c0cc15SPoul-Henning Kamp static int nswap_hiwat = 512; /* in pages, swap_pager_almost_full warn */ 39226f9a767SRodney W. Grimes 3932174a0c6SKonstantin Belousov SYSCTL_INT(_vm, OID_AUTO, dmmax, CTLFLAG_RD, &dmmax, 0, 3942174a0c6SKonstantin Belousov "Maximum size of a swap block"); 395cee313c4SRobert Watson 396a5edd34aSPoul-Henning Kamp static void swp_sizecheck(void); 39711caded3SAlfred Perlstein static void swp_pager_async_iodone(struct buf *bp); 39888ad2d7bSKonstantin Belousov static int swapongeom(struct vnode *); 39959efee01SPoul-Henning Kamp static int swaponvp(struct thread *, struct vnode *, u_long); 40035918c55SChristian S.J. Peron static int swapoff_one(struct swdevt *sp, struct ucred *cred); 40124a1cce3SDavid Greenman 4021c7c3c6aSMatthew Dillon /* 4031c7c3c6aSMatthew Dillon * Swap bitmap functions 4041c7c3c6aSMatthew Dillon */ 405a5edd34aSPoul-Henning Kamp static void swp_pager_freeswapspace(daddr_t blk, int npages); 406a5edd34aSPoul-Henning Kamp static daddr_t swp_pager_getswapspace(int npages); 4071c7c3c6aSMatthew Dillon 4081c7c3c6aSMatthew Dillon /* 4091c7c3c6aSMatthew Dillon * Metadata functions 4101c7c3c6aSMatthew Dillon */ 411a5edd34aSPoul-Henning Kamp static struct swblock **swp_pager_hash(vm_object_t object, vm_pindex_t index); 41211caded3SAlfred Perlstein static void swp_pager_meta_build(vm_object_t, vm_pindex_t, daddr_t); 41311caded3SAlfred Perlstein static void swp_pager_meta_free(vm_object_t, vm_pindex_t, daddr_t); 41411caded3SAlfred Perlstein static void swp_pager_meta_free_all(vm_object_t); 41511caded3SAlfred Perlstein static daddr_t swp_pager_meta_ctl(vm_object_t, vm_pindex_t, int); 4161c7c3c6aSMatthew Dillon 4171c7c3c6aSMatthew Dillon /* 4181c7c3c6aSMatthew Dillon * SWP_SIZECHECK() - update swap_pager_full indication 4191c7c3c6aSMatthew Dillon * 42020d3034fSMatthew Dillon * update the swap_pager_almost_full indication and warn when we are 42120d3034fSMatthew Dillon * about to run out of swap space, using lowat/hiwat hysteresis. 42220d3034fSMatthew Dillon * 42320d3034fSMatthew Dillon * Clear swap_pager_full ( task killing ) indication when lowat is met. 4241c7c3c6aSMatthew Dillon * 4251c7c3c6aSMatthew Dillon * No restrictions on call 4261c7c3c6aSMatthew Dillon * This routine may not block. 4271c7c3c6aSMatthew Dillon */ 428a5edd34aSPoul-Henning Kamp static void 4292f249180SPoul-Henning Kamp swp_sizecheck(void) 4300d94caffSDavid Greenman { 43123955314SAlfred Perlstein 4328f60c087SPoul-Henning Kamp if (swap_pager_avail < nswap_lowat) { 43320d3034fSMatthew Dillon if (swap_pager_almost_full == 0) { 4341af87c92SDavid Greenman printf("swap_pager: out of swap space\n"); 43520d3034fSMatthew Dillon swap_pager_almost_full = 1; 4362b0d37a4SMatthew Dillon } 43720d3034fSMatthew Dillon } else { 43826f9a767SRodney W. Grimes swap_pager_full = 0; 4398f60c087SPoul-Henning Kamp if (swap_pager_avail > nswap_hiwat) 44020d3034fSMatthew Dillon swap_pager_almost_full = 0; 44126f9a767SRodney W. Grimes } 4421c7c3c6aSMatthew Dillon } 4431c7c3c6aSMatthew Dillon 4441c7c3c6aSMatthew Dillon /* 445da5fd145SPeter Wemm * SWP_PAGER_HASH() - hash swap meta data 446da5fd145SPeter Wemm * 447a5edd34aSPoul-Henning Kamp * This is an helper function which hashes the swapblk given 448da5fd145SPeter Wemm * the object and page index. It returns a pointer to a pointer 449da5fd145SPeter Wemm * to the object, or a pointer to a NULL pointer if it could not 450da5fd145SPeter Wemm * find a swapblk. 451da5fd145SPeter Wemm */ 452a5edd34aSPoul-Henning Kamp static struct swblock ** 453da5fd145SPeter Wemm swp_pager_hash(vm_object_t object, vm_pindex_t index) 454da5fd145SPeter Wemm { 455da5fd145SPeter Wemm struct swblock **pswap; 456da5fd145SPeter Wemm struct swblock *swap; 457da5fd145SPeter Wemm 458da5fd145SPeter Wemm index &= ~(vm_pindex_t)SWAP_META_MASK; 459da5fd145SPeter Wemm pswap = &swhash[(index ^ (int)(intptr_t)object) & swhash_mask]; 460da5fd145SPeter Wemm while ((swap = *pswap) != NULL) { 461da5fd145SPeter Wemm if (swap->swb_object == object && 462da5fd145SPeter Wemm swap->swb_index == index 463da5fd145SPeter Wemm ) { 464da5fd145SPeter Wemm break; 465da5fd145SPeter Wemm } 466da5fd145SPeter Wemm pswap = &swap->swb_hnext; 467da5fd145SPeter Wemm } 468da5fd145SPeter Wemm return (pswap); 469da5fd145SPeter Wemm } 470da5fd145SPeter Wemm 471da5fd145SPeter Wemm /* 4721c7c3c6aSMatthew Dillon * SWAP_PAGER_INIT() - initialize the swap pager! 4731c7c3c6aSMatthew Dillon * 4741c7c3c6aSMatthew Dillon * Expected to be started from system init. NOTE: This code is run 4751c7c3c6aSMatthew Dillon * before much else so be careful what you depend on. Most of the VM 4761c7c3c6aSMatthew Dillon * system has yet to be initialized at this point. 4771c7c3c6aSMatthew Dillon */ 478f5a12711SPoul-Henning Kamp static void 4792f249180SPoul-Henning Kamp swap_pager_init(void) 480df8bae1dSRodney W. Grimes { 4811c7c3c6aSMatthew Dillon /* 4821c7c3c6aSMatthew Dillon * Initialize object lists 4831c7c3c6aSMatthew Dillon */ 4841c7c3c6aSMatthew Dillon int i; 4851c7c3c6aSMatthew Dillon 4861c7c3c6aSMatthew Dillon for (i = 0; i < NOBJLISTS; ++i) 4871c7c3c6aSMatthew Dillon TAILQ_INIT(&swap_pager_object_list[i]); 48820da9c2eSPoul-Henning Kamp mtx_init(&sw_dev_mtx, "swapdev", NULL, MTX_DEF); 48915719273SKonstantin Belousov sx_init(&sw_alloc_sx, "swspsx"); 49004533e1eSKonstantin Belousov sx_init(&swdev_syscall_lock, "swsysc"); 491df8bae1dSRodney W. Grimes 492df8bae1dSRodney W. Grimes /* 4931c7c3c6aSMatthew Dillon * Device Stripe, in PAGE_SIZE'd blocks 494df8bae1dSRodney W. Grimes */ 4951c7c3c6aSMatthew Dillon dmmax = SWB_NPAGES * 2; 4961c7c3c6aSMatthew Dillon } 49726f9a767SRodney W. Grimes 498df8bae1dSRodney W. Grimes /* 4991c7c3c6aSMatthew Dillon * SWAP_PAGER_SWAP_INIT() - swap pager initialization from pageout process 5001c7c3c6aSMatthew Dillon * 5011c7c3c6aSMatthew Dillon * Expected to be started from pageout process once, prior to entering 5021c7c3c6aSMatthew Dillon * its main loop. 503df8bae1dSRodney W. Grimes */ 50424a1cce3SDavid Greenman void 5052f249180SPoul-Henning Kamp swap_pager_swap_init(void) 506df8bae1dSRodney W. Grimes { 50761203277SDag-Erling Smørgrav unsigned long n, n2; 5080d94caffSDavid Greenman 50926f9a767SRodney W. Grimes /* 5101c7c3c6aSMatthew Dillon * Number of in-transit swap bp operations. Don't 5111c7c3c6aSMatthew Dillon * exhaust the pbufs completely. Make sure we 5121c7c3c6aSMatthew Dillon * initialize workable values (0 will work for hysteresis 5131c7c3c6aSMatthew Dillon * but it isn't very efficient). 5141c7c3c6aSMatthew Dillon * 515327f4e83SMatthew Dillon * The nsw_cluster_max is constrained by the bp->b_pages[] 5161c7c3c6aSMatthew Dillon * array (MAXPHYS/PAGE_SIZE) and our locally defined 5171c7c3c6aSMatthew Dillon * MAX_PAGEOUT_CLUSTER. Also be aware that swap ops are 5181c7c3c6aSMatthew Dillon * constrained by the swap device interleave stripe size. 519327f4e83SMatthew Dillon * 520327f4e83SMatthew Dillon * Currently we hardwire nsw_wcount_async to 4. This limit is 521327f4e83SMatthew Dillon * designed to prevent other I/O from having high latencies due to 522327f4e83SMatthew Dillon * our pageout I/O. The value 4 works well for one or two active swap 523327f4e83SMatthew Dillon * devices but is probably a little low if you have more. Even so, 524327f4e83SMatthew Dillon * a higher value would probably generate only a limited improvement 525327f4e83SMatthew Dillon * with three or four active swap devices since the system does not 526327f4e83SMatthew Dillon * typically have to pageout at extreme bandwidths. We will want 527327f4e83SMatthew Dillon * at least 2 per swap devices, and 4 is a pretty good value if you 528327f4e83SMatthew Dillon * have one NFS swap device due to the command/ack latency over NFS. 529327f4e83SMatthew Dillon * So it all works out pretty well. 53026f9a767SRodney W. Grimes */ 531ad3cce20SMatthew Dillon nsw_cluster_max = min((MAXPHYS/PAGE_SIZE), MAX_PAGEOUT_CLUSTER); 532327f4e83SMatthew Dillon 5336d541bf1SJohn Baldwin mtx_lock(&pbuf_mtx); 5341c7c3c6aSMatthew Dillon nsw_rcount = (nswbuf + 1) / 2; 535327f4e83SMatthew Dillon nsw_wcount_sync = (nswbuf + 3) / 4; 536327f4e83SMatthew Dillon nsw_wcount_async = 4; 537327f4e83SMatthew Dillon nsw_wcount_async_max = nsw_wcount_async; 5386d541bf1SJohn Baldwin mtx_unlock(&pbuf_mtx); 53924a1cce3SDavid Greenman 5401c7c3c6aSMatthew Dillon /* 5411c7c3c6aSMatthew Dillon * Initialize our zone. Right now I'm just guessing on the number 5421c7c3c6aSMatthew Dillon * we need based on the number of pages in the system. Each swblock 54361203277SDag-Erling Smørgrav * can hold 32 pages, so this is probably overkill. This reservation 544ec61f55dSMatthew Dillon * is typically limited to around 32MB by default. 5451c7c3c6aSMatthew Dillon */ 54644f1c916SBryan Drewery n = vm_cnt.v_page_count / 2; 5472f9e4e80SMatthew Dillon if (maxswzone && n > maxswzone / sizeof(struct swblock)) 5482f9e4e80SMatthew Dillon n = maxswzone / sizeof(struct swblock); 54922a5e6b9SDag-Erling Smørgrav n2 = n; 550670d17b5SJeff Roberson swap_zone = uma_zcreate("SWAPMETA", sizeof(struct swblock), NULL, NULL, 5517827d9b0SAlan Cox NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE | UMA_ZONE_VM); 552010b1ca1SDavid Schultz if (swap_zone == NULL) 553010b1ca1SDavid Schultz panic("failed to create swap_zone."); 5548355f576SJeff Roberson do { 555a4915c21SAttilio Rao if (uma_zone_reserve_kva(swap_zone, n)) 55661ce6eeeSAlfred Perlstein break; 55761ce6eeeSAlfred Perlstein /* 55861ce6eeeSAlfred Perlstein * if the allocation failed, try a zone two thirds the 55961ce6eeeSAlfred Perlstein * size of the previous attempt. 56061ce6eeeSAlfred Perlstein */ 56161ce6eeeSAlfred Perlstein n -= ((n + 2) / 3); 56261ce6eeeSAlfred Perlstein } while (n > 0); 56321cd6e62SSeigo Tanimura if (n2 != n) 56461203277SDag-Erling Smørgrav printf("Swap zone entries reduced from %lu to %lu.\n", n2, n); 56561203277SDag-Erling Smørgrav swap_maxpages = n * SWAP_META_PAGES; 56661203277SDag-Erling Smørgrav swzone = n * sizeof(struct swblock); 56722a5e6b9SDag-Erling Smørgrav n2 = n; 56824a1cce3SDavid Greenman 5691c7c3c6aSMatthew Dillon /* 5701c7c3c6aSMatthew Dillon * Initialize our meta-data hash table. The swapper does not need to 5711c7c3c6aSMatthew Dillon * be quite as efficient as the VM system, so we do not use an 5721c7c3c6aSMatthew Dillon * oversized hash table. 5731c7c3c6aSMatthew Dillon * 5741c7c3c6aSMatthew Dillon * n: size of hash table, must be power of 2 5751c7c3c6aSMatthew Dillon * swhash_mask: hash table index mask 5761c7c3c6aSMatthew Dillon */ 57761ce6eeeSAlfred Perlstein for (n = 1; n < n2 / 8; n *= 2) 5781c7c3c6aSMatthew Dillon ; 579a163d034SWarner Losh swhash = malloc(sizeof(struct swblock *) * n, M_VMPGDATA, M_WAITOK | M_ZERO); 5801c7c3c6aSMatthew Dillon swhash_mask = n - 1; 5817827d9b0SAlan Cox mtx_init(&swhash_mtx, "swap_pager swhash", NULL, MTX_DEF); 58224a1cce3SDavid Greenman } 58324a1cce3SDavid Greenman 584eb4d6a1bSKonstantin Belousov static vm_object_t 585eb4d6a1bSKonstantin Belousov swap_pager_alloc_init(void *handle, struct ucred *cred, vm_ooffset_t size, 586eb4d6a1bSKonstantin Belousov vm_ooffset_t offset) 587eb4d6a1bSKonstantin Belousov { 588eb4d6a1bSKonstantin Belousov vm_object_t object; 589eb4d6a1bSKonstantin Belousov 590eb4d6a1bSKonstantin Belousov if (cred != NULL) { 591eb4d6a1bSKonstantin Belousov if (!swap_reserve_by_cred(size, cred)) 592eb4d6a1bSKonstantin Belousov return (NULL); 593eb4d6a1bSKonstantin Belousov crhold(cred); 594eb4d6a1bSKonstantin Belousov } 595eb4d6a1bSKonstantin Belousov object = vm_object_allocate(OBJT_SWAP, OFF_TO_IDX(offset + 596eb4d6a1bSKonstantin Belousov PAGE_MASK + size)); 597eb4d6a1bSKonstantin Belousov object->handle = handle; 598eb4d6a1bSKonstantin Belousov if (cred != NULL) { 599eb4d6a1bSKonstantin Belousov object->cred = cred; 600eb4d6a1bSKonstantin Belousov object->charge = size; 601eb4d6a1bSKonstantin Belousov } 602eb4d6a1bSKonstantin Belousov object->un_pager.swp.swp_bcount = 0; 603eb4d6a1bSKonstantin Belousov return (object); 604eb4d6a1bSKonstantin Belousov } 605eb4d6a1bSKonstantin Belousov 60624a1cce3SDavid Greenman /* 6071c7c3c6aSMatthew Dillon * SWAP_PAGER_ALLOC() - allocate a new OBJT_SWAP VM object and instantiate 6081c7c3c6aSMatthew Dillon * its metadata structures. 6091c7c3c6aSMatthew Dillon * 6101c7c3c6aSMatthew Dillon * This routine is called from the mmap and fork code to create a new 611eb4d6a1bSKonstantin Belousov * OBJT_SWAP object. 6121c7c3c6aSMatthew Dillon * 613eb4d6a1bSKonstantin Belousov * This routine must ensure that no live duplicate is created for 614eb4d6a1bSKonstantin Belousov * the named object request, which is protected against by 615eb4d6a1bSKonstantin Belousov * holding the sw_alloc_sx lock in case handle != NULL. 61624a1cce3SDavid Greenman */ 617f5a12711SPoul-Henning Kamp static vm_object_t 6186cde7a16SDavid Greenman swap_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 6193364c323SKonstantin Belousov vm_ooffset_t offset, struct ucred *cred) 62024a1cce3SDavid Greenman { 62124a1cce3SDavid Greenman vm_object_t object; 6222f7af3dbSAlan Cox 623eb4d6a1bSKonstantin Belousov if (handle != NULL) { 6241c7c3c6aSMatthew Dillon /* 6251c7c3c6aSMatthew Dillon * Reference existing named region or allocate new one. There 6261c7c3c6aSMatthew Dillon * should not be a race here against swp_pager_meta_build() 6271c7c3c6aSMatthew Dillon * as called from vm_page_remove() in regards to the lookup 6281c7c3c6aSMatthew Dillon * of the handle. 6291c7c3c6aSMatthew Dillon */ 6300cddd8f0SMatthew Dillon sx_xlock(&sw_alloc_sx); 6311c7c3c6aSMatthew Dillon object = vm_pager_object_lookup(NOBJLIST(handle), handle); 632b5e8f167SAlan Cox if (object == NULL) { 633eb4d6a1bSKonstantin Belousov object = swap_pager_alloc_init(handle, cred, size, 634eb4d6a1bSKonstantin Belousov offset); 635eb4d6a1bSKonstantin Belousov if (object != NULL) { 636eb4d6a1bSKonstantin Belousov TAILQ_INSERT_TAIL(NOBJLIST(object->handle), 637eb4d6a1bSKonstantin Belousov object, pager_object_list); 6383364c323SKonstantin Belousov } 63924a1cce3SDavid Greenman } 6400cddd8f0SMatthew Dillon sx_xunlock(&sw_alloc_sx); 64124a1cce3SDavid Greenman } else { 642eb4d6a1bSKonstantin Belousov object = swap_pager_alloc_init(handle, cred, size, offset); 64324a1cce3SDavid Greenman } 64424a1cce3SDavid Greenman return (object); 645df8bae1dSRodney W. Grimes } 646df8bae1dSRodney W. Grimes 64726f9a767SRodney W. Grimes /* 6481c7c3c6aSMatthew Dillon * SWAP_PAGER_DEALLOC() - remove swap metadata from object 6491c7c3c6aSMatthew Dillon * 6501c7c3c6aSMatthew Dillon * The swap backing for the object is destroyed. The code is 6511c7c3c6aSMatthew Dillon * designed such that we can reinstantiate it later, but this 6521c7c3c6aSMatthew Dillon * routine is typically called only when the entire object is 6531c7c3c6aSMatthew Dillon * about to be destroyed. 6541c7c3c6aSMatthew Dillon * 65515523cf7SKonstantin Belousov * The object must be locked. 65626f9a767SRodney W. Grimes */ 657df8bae1dSRodney W. Grimes static void 6582f249180SPoul-Henning Kamp swap_pager_dealloc(vm_object_t object) 65926f9a767SRodney W. Grimes { 6604dcc5c2dSMatthew Dillon 661eb4d6a1bSKonstantin Belousov VM_OBJECT_ASSERT_WLOCKED(object); 662eb4d6a1bSKonstantin Belousov KASSERT((object->flags & OBJ_DEAD) != 0, ("dealloc of reachable obj")); 663eb4d6a1bSKonstantin Belousov 66426f9a767SRodney W. Grimes /* 6651c7c3c6aSMatthew Dillon * Remove from list right away so lookups will fail if we block for 6661c7c3c6aSMatthew Dillon * pageout completion. 66726f9a767SRodney W. Grimes */ 668bd228075SAlan Cox if (object->handle != NULL) { 669eb4d6a1bSKonstantin Belousov VM_OBJECT_WUNLOCK(object); 670eb4d6a1bSKonstantin Belousov sx_xlock(&sw_alloc_sx); 671eb4d6a1bSKonstantin Belousov TAILQ_REMOVE(NOBJLIST(object->handle), object, 672eb4d6a1bSKonstantin Belousov pager_object_list); 673eb4d6a1bSKonstantin Belousov sx_xunlock(&sw_alloc_sx); 674eb4d6a1bSKonstantin Belousov VM_OBJECT_WLOCK(object); 675bd228075SAlan Cox } 6761c7c3c6aSMatthew Dillon 6771c7c3c6aSMatthew Dillon vm_object_pip_wait(object, "swpdea"); 6781c7c3c6aSMatthew Dillon 6791c7c3c6aSMatthew Dillon /* 6801c7c3c6aSMatthew Dillon * Free all remaining metadata. We only bother to free it from 6811c7c3c6aSMatthew Dillon * the swap meta data. We do not attempt to free swapblk's still 6821c7c3c6aSMatthew Dillon * associated with vm_page_t's for this object. We do not care 6831c7c3c6aSMatthew Dillon * if paging is still in progress on some objects. 6841c7c3c6aSMatthew Dillon */ 6851c7c3c6aSMatthew Dillon swp_pager_meta_free_all(object); 686e735691bSJohn Baldwin object->handle = NULL; 687e735691bSJohn Baldwin object->type = OBJT_DEAD; 6881c7c3c6aSMatthew Dillon } 6891c7c3c6aSMatthew Dillon 6901c7c3c6aSMatthew Dillon /************************************************************************ 6911c7c3c6aSMatthew Dillon * SWAP PAGER BITMAP ROUTINES * 6921c7c3c6aSMatthew Dillon ************************************************************************/ 6931c7c3c6aSMatthew Dillon 6941c7c3c6aSMatthew Dillon /* 6951c7c3c6aSMatthew Dillon * SWP_PAGER_GETSWAPSPACE() - allocate raw swap space 6961c7c3c6aSMatthew Dillon * 6971c7c3c6aSMatthew Dillon * Allocate swap for the requested number of pages. The starting 6981c7c3c6aSMatthew Dillon * swap block number (a page index) is returned or SWAPBLK_NONE 6991c7c3c6aSMatthew Dillon * if the allocation failed. 7001c7c3c6aSMatthew Dillon * 7011c7c3c6aSMatthew Dillon * Also has the side effect of advising that somebody made a mistake 7021c7c3c6aSMatthew Dillon * when they configured swap and didn't configure enough. 7031c7c3c6aSMatthew Dillon * 70415523cf7SKonstantin Belousov * This routine may not sleep. 7058f60c087SPoul-Henning Kamp * 7068f60c087SPoul-Henning Kamp * We allocate in round-robin fashion from the configured devices. 7071c7c3c6aSMatthew Dillon */ 708a5edd34aSPoul-Henning Kamp static daddr_t 7092f249180SPoul-Henning Kamp swp_pager_getswapspace(int npages) 7101c7c3c6aSMatthew Dillon { 7111c7c3c6aSMatthew Dillon daddr_t blk; 7128f60c087SPoul-Henning Kamp struct swdevt *sp; 7138f60c087SPoul-Henning Kamp int i; 7141c7c3c6aSMatthew Dillon 7158f60c087SPoul-Henning Kamp blk = SWAPBLK_NONE; 71620da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 7178f60c087SPoul-Henning Kamp sp = swdevhd; 7188f60c087SPoul-Henning Kamp for (i = 0; i < nswapdev; i++) { 7198f60c087SPoul-Henning Kamp if (sp == NULL) 7208f60c087SPoul-Henning Kamp sp = TAILQ_FIRST(&swtailq); 7218f60c087SPoul-Henning Kamp if (!(sp->sw_flags & SW_CLOSING)) { 7228f60c087SPoul-Henning Kamp blk = blist_alloc(sp->sw_blist, npages); 7238f60c087SPoul-Henning Kamp if (blk != SWAPBLK_NONE) { 7248f60c087SPoul-Henning Kamp blk += sp->sw_first; 7258f60c087SPoul-Henning Kamp sp->sw_used += npages; 726d05bc129SAlan Cox swap_pager_avail -= npages; 7278f60c087SPoul-Henning Kamp swp_sizecheck(); 7288f60c087SPoul-Henning Kamp swdevhd = TAILQ_NEXT(sp, sw_list); 729d05bc129SAlan Cox goto done; 7308f60c087SPoul-Henning Kamp } 7318f60c087SPoul-Henning Kamp } 7328f60c087SPoul-Henning Kamp sp = TAILQ_NEXT(sp, sw_list); 7338f60c087SPoul-Henning Kamp } 7342b0d37a4SMatthew Dillon if (swap_pager_full != 2) { 73520da9c2eSPoul-Henning Kamp printf("swap_pager_getswapspace(%d): failed\n", npages); 7362b0d37a4SMatthew Dillon swap_pager_full = 2; 73720d3034fSMatthew Dillon swap_pager_almost_full = 1; 7382b0d37a4SMatthew Dillon } 7398f60c087SPoul-Henning Kamp swdevhd = NULL; 740d05bc129SAlan Cox done: 741d05bc129SAlan Cox mtx_unlock(&sw_dev_mtx); 7421c7c3c6aSMatthew Dillon return (blk); 74326f9a767SRodney W. Grimes } 74426f9a767SRodney W. Grimes 745b3fed13eSDavid Schultz static int 746b3fed13eSDavid Schultz swp_pager_isondev(daddr_t blk, struct swdevt *sp) 7478f60c087SPoul-Henning Kamp { 7488f60c087SPoul-Henning Kamp 749b3fed13eSDavid Schultz return (blk >= sp->sw_first && blk < sp->sw_end); 7508f60c087SPoul-Henning Kamp } 7518f60c087SPoul-Henning Kamp 7524b03903aSPoul-Henning Kamp static void 7534b03903aSPoul-Henning Kamp swp_pager_strategy(struct buf *bp) 7544b03903aSPoul-Henning Kamp { 7554b03903aSPoul-Henning Kamp struct swdevt *sp; 7564b03903aSPoul-Henning Kamp 75720da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 7584b03903aSPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 7594b03903aSPoul-Henning Kamp if (bp->b_blkno >= sp->sw_first && bp->b_blkno < sp->sw_end) { 76020da9c2eSPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 7612cc718a1SKonstantin Belousov if ((sp->sw_flags & SW_UNMAPPED) != 0 && 7622cc718a1SKonstantin Belousov unmapped_buf_allowed) { 7632cc718a1SKonstantin Belousov bp->b_data = unmapped_buf; 7642cc718a1SKonstantin Belousov bp->b_offset = 0; 7652cc718a1SKonstantin Belousov } else { 7662cc718a1SKonstantin Belousov pmap_qenter((vm_offset_t)bp->b_data, 7672cc718a1SKonstantin Belousov &bp->b_pages[0], bp->b_bcount / PAGE_SIZE); 7682cc718a1SKonstantin Belousov } 7694b03903aSPoul-Henning Kamp sp->sw_strategy(bp, sp); 7704b03903aSPoul-Henning Kamp return; 7714b03903aSPoul-Henning Kamp } 7724b03903aSPoul-Henning Kamp } 77320da9c2eSPoul-Henning Kamp panic("Swapdev not found"); 7744b03903aSPoul-Henning Kamp } 7754b03903aSPoul-Henning Kamp 7768f60c087SPoul-Henning Kamp 77726f9a767SRodney W. Grimes /* 7781c7c3c6aSMatthew Dillon * SWP_PAGER_FREESWAPSPACE() - free raw swap space 7791c7c3c6aSMatthew Dillon * 7801c7c3c6aSMatthew Dillon * This routine returns the specified swap blocks back to the bitmap. 7811c7c3c6aSMatthew Dillon * 78215523cf7SKonstantin Belousov * This routine may not sleep. 78326f9a767SRodney W. Grimes */ 784a5edd34aSPoul-Henning Kamp static void 7858f60c087SPoul-Henning Kamp swp_pager_freeswapspace(daddr_t blk, int npages) 7860d94caffSDavid Greenman { 7878f60c087SPoul-Henning Kamp struct swdevt *sp; 78892da00bbSMatthew Dillon 7897645e885SAlan Cox mtx_lock(&sw_dev_mtx); 7907645e885SAlan Cox TAILQ_FOREACH(sp, &swtailq, sw_list) { 7917645e885SAlan Cox if (blk >= sp->sw_first && blk < sp->sw_end) { 79292da00bbSMatthew Dillon sp->sw_used -= npages; 79392da00bbSMatthew Dillon /* 7947645e885SAlan Cox * If we are attempting to stop swapping on 7957645e885SAlan Cox * this device, we don't want to mark any 7967645e885SAlan Cox * blocks free lest they be reused. 79792da00bbSMatthew Dillon */ 7987645e885SAlan Cox if ((sp->sw_flags & SW_CLOSING) == 0) { 7997645e885SAlan Cox blist_free(sp->sw_blist, blk - sp->sw_first, 8007645e885SAlan Cox npages); 8018f60c087SPoul-Henning Kamp swap_pager_avail += npages; 8021c7c3c6aSMatthew Dillon swp_sizecheck(); 80326f9a767SRodney W. Grimes } 8047645e885SAlan Cox mtx_unlock(&sw_dev_mtx); 8057645e885SAlan Cox return; 8067645e885SAlan Cox } 8077645e885SAlan Cox } 8087645e885SAlan Cox panic("Swapdev not found"); 8097645e885SAlan Cox } 8101c7c3c6aSMatthew Dillon 81126f9a767SRodney W. Grimes /* 8121c7c3c6aSMatthew Dillon * SWAP_PAGER_FREESPACE() - frees swap blocks associated with a page 8131c7c3c6aSMatthew Dillon * range within an object. 8141c7c3c6aSMatthew Dillon * 8151c7c3c6aSMatthew Dillon * This is a globally accessible routine. 8161c7c3c6aSMatthew Dillon * 8171c7c3c6aSMatthew Dillon * This routine removes swapblk assignments from swap metadata. 8181c7c3c6aSMatthew Dillon * 8191c7c3c6aSMatthew Dillon * The external callers of this routine typically have already destroyed 8201c7c3c6aSMatthew Dillon * or renamed vm_page_t's associated with this range in the object so 8211c7c3c6aSMatthew Dillon * we should be ok. 822c25673ffSAttilio Rao * 823c25673ffSAttilio Rao * The object must be locked. 82426f9a767SRodney W. Grimes */ 82526f9a767SRodney W. Grimes void 8262f249180SPoul-Henning Kamp swap_pager_freespace(vm_object_t object, vm_pindex_t start, vm_size_t size) 82726f9a767SRodney W. Grimes { 82823955314SAlfred Perlstein 8291c7c3c6aSMatthew Dillon swp_pager_meta_free(object, start, size); 8304dcc5c2dSMatthew Dillon } 8314dcc5c2dSMatthew Dillon 8324dcc5c2dSMatthew Dillon /* 8334dcc5c2dSMatthew Dillon * SWAP_PAGER_RESERVE() - reserve swap blocks in object 8344dcc5c2dSMatthew Dillon * 8354dcc5c2dSMatthew Dillon * Assigns swap blocks to the specified range within the object. The 83656ce850bSKonstantin Belousov * swap blocks are not zeroed. Any previous swap assignment is destroyed. 8374dcc5c2dSMatthew Dillon * 8384dcc5c2dSMatthew Dillon * Returns 0 on success, -1 on failure. 8394dcc5c2dSMatthew Dillon */ 8404dcc5c2dSMatthew Dillon int 8414dcc5c2dSMatthew Dillon swap_pager_reserve(vm_object_t object, vm_pindex_t start, vm_size_t size) 8424dcc5c2dSMatthew Dillon { 8434dcc5c2dSMatthew Dillon int n = 0; 8444dcc5c2dSMatthew Dillon daddr_t blk = SWAPBLK_NONE; 8454dcc5c2dSMatthew Dillon vm_pindex_t beg = start; /* save start index */ 8464dcc5c2dSMatthew Dillon 84789f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 8484dcc5c2dSMatthew Dillon while (size) { 8494dcc5c2dSMatthew Dillon if (n == 0) { 8504dcc5c2dSMatthew Dillon n = BLIST_MAX_ALLOC; 8514dcc5c2dSMatthew Dillon while ((blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE) { 8524dcc5c2dSMatthew Dillon n >>= 1; 8534dcc5c2dSMatthew Dillon if (n == 0) { 8544dcc5c2dSMatthew Dillon swp_pager_meta_free(object, beg, start - beg); 85589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 8564dcc5c2dSMatthew Dillon return (-1); 8574dcc5c2dSMatthew Dillon } 8584dcc5c2dSMatthew Dillon } 8594dcc5c2dSMatthew Dillon } 8604dcc5c2dSMatthew Dillon swp_pager_meta_build(object, start, blk); 8614dcc5c2dSMatthew Dillon --size; 8624dcc5c2dSMatthew Dillon ++start; 8634dcc5c2dSMatthew Dillon ++blk; 8644dcc5c2dSMatthew Dillon --n; 8654dcc5c2dSMatthew Dillon } 8664dcc5c2dSMatthew Dillon swp_pager_meta_free(object, start, n); 86789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 8684dcc5c2dSMatthew Dillon return (0); 86926f9a767SRodney W. Grimes } 87026f9a767SRodney W. Grimes 8710a47b48bSJohn Dyson /* 8721c7c3c6aSMatthew Dillon * SWAP_PAGER_COPY() - copy blocks from source pager to destination pager 8731c7c3c6aSMatthew Dillon * and destroy the source. 8741c7c3c6aSMatthew Dillon * 8751c7c3c6aSMatthew Dillon * Copy any valid swapblks from the source to the destination. In 8761c7c3c6aSMatthew Dillon * cases where both the source and destination have a valid swapblk, 8771c7c3c6aSMatthew Dillon * we keep the destination's. 8781c7c3c6aSMatthew Dillon * 87915523cf7SKonstantin Belousov * This routine is allowed to sleep. It may sleep allocating metadata 8801c7c3c6aSMatthew Dillon * indirectly through swp_pager_meta_build() or if paging is still in 8811c7c3c6aSMatthew Dillon * progress on the source. 8821c7c3c6aSMatthew Dillon * 8831c7c3c6aSMatthew Dillon * The source object contains no vm_page_t's (which is just as well) 8841c7c3c6aSMatthew Dillon * 8851c7c3c6aSMatthew Dillon * The source object is of type OBJT_SWAP. 8861c7c3c6aSMatthew Dillon * 88715523cf7SKonstantin Belousov * The source and destination objects must be locked. 88815523cf7SKonstantin Belousov * Both object locks may temporarily be released. 88926f9a767SRodney W. Grimes */ 89026f9a767SRodney W. Grimes void 8912f249180SPoul-Henning Kamp swap_pager_copy(vm_object_t srcobject, vm_object_t dstobject, 8922f249180SPoul-Henning Kamp vm_pindex_t offset, int destroysource) 89326f9a767SRodney W. Grimes { 894a316d390SJohn Dyson vm_pindex_t i; 8954dcc5c2dSMatthew Dillon 89689f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(srcobject); 89789f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(dstobject); 8980cddd8f0SMatthew Dillon 89926f9a767SRodney W. Grimes /* 9001c7c3c6aSMatthew Dillon * If destroysource is set, we remove the source object from the 9011c7c3c6aSMatthew Dillon * swap_pager internal queue now. 90226f9a767SRodney W. Grimes */ 903eb4d6a1bSKonstantin Belousov if (destroysource && srcobject->handle != NULL) { 904eb4d6a1bSKonstantin Belousov vm_object_pip_add(srcobject, 1); 905eb4d6a1bSKonstantin Belousov VM_OBJECT_WUNLOCK(srcobject); 906eb4d6a1bSKonstantin Belousov vm_object_pip_add(dstobject, 1); 907eb4d6a1bSKonstantin Belousov VM_OBJECT_WUNLOCK(dstobject); 908eb4d6a1bSKonstantin Belousov sx_xlock(&sw_alloc_sx); 909eb4d6a1bSKonstantin Belousov TAILQ_REMOVE(NOBJLIST(srcobject->handle), srcobject, 910eb4d6a1bSKonstantin Belousov pager_object_list); 911eb4d6a1bSKonstantin Belousov sx_xunlock(&sw_alloc_sx); 912eb4d6a1bSKonstantin Belousov VM_OBJECT_WLOCK(dstobject); 913eb4d6a1bSKonstantin Belousov vm_object_pip_wakeup(dstobject); 914eb4d6a1bSKonstantin Belousov VM_OBJECT_WLOCK(srcobject); 915eb4d6a1bSKonstantin Belousov vm_object_pip_wakeup(srcobject); 916bd228075SAlan Cox } 91726f9a767SRodney W. Grimes 9181c7c3c6aSMatthew Dillon /* 9191c7c3c6aSMatthew Dillon * transfer source to destination. 9201c7c3c6aSMatthew Dillon */ 9211c7c3c6aSMatthew Dillon for (i = 0; i < dstobject->size; ++i) { 9221c7c3c6aSMatthew Dillon daddr_t dstaddr; 9231c7c3c6aSMatthew Dillon 9241c7c3c6aSMatthew Dillon /* 9251c7c3c6aSMatthew Dillon * Locate (without changing) the swapblk on the destination, 9261c7c3c6aSMatthew Dillon * unless it is invalid in which case free it silently, or 9271c7c3c6aSMatthew Dillon * if the destination is a resident page, in which case the 9281c7c3c6aSMatthew Dillon * source is thrown away. 9291c7c3c6aSMatthew Dillon */ 9301c7c3c6aSMatthew Dillon dstaddr = swp_pager_meta_ctl(dstobject, i, 0); 9311c7c3c6aSMatthew Dillon 9321c7c3c6aSMatthew Dillon if (dstaddr == SWAPBLK_NONE) { 9331c7c3c6aSMatthew Dillon /* 9341c7c3c6aSMatthew Dillon * Destination has no swapblk and is not resident, 9351c7c3c6aSMatthew Dillon * copy source. 9361c7c3c6aSMatthew Dillon */ 9371c7c3c6aSMatthew Dillon daddr_t srcaddr; 9381c7c3c6aSMatthew Dillon 9391c7c3c6aSMatthew Dillon srcaddr = swp_pager_meta_ctl( 9401c7c3c6aSMatthew Dillon srcobject, 9411c7c3c6aSMatthew Dillon i + offset, 9421c7c3c6aSMatthew Dillon SWM_POP 9431c7c3c6aSMatthew Dillon ); 9441c7c3c6aSMatthew Dillon 945ee3dc7d7SAlan Cox if (srcaddr != SWAPBLK_NONE) { 946c7c8dd7eSAlan Cox /* 947c7c8dd7eSAlan Cox * swp_pager_meta_build() can sleep. 948c7c8dd7eSAlan Cox */ 949c7c8dd7eSAlan Cox vm_object_pip_add(srcobject, 1); 95089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(srcobject); 951c7c8dd7eSAlan Cox vm_object_pip_add(dstobject, 1); 9524dcc5c2dSMatthew Dillon swp_pager_meta_build(dstobject, i, srcaddr); 953c7c8dd7eSAlan Cox vm_object_pip_wakeup(dstobject); 95489f6b863SAttilio Rao VM_OBJECT_WLOCK(srcobject); 955c7c8dd7eSAlan Cox vm_object_pip_wakeup(srcobject); 956ee3dc7d7SAlan Cox } 9571c7c3c6aSMatthew Dillon } else { 9581c7c3c6aSMatthew Dillon /* 9591c7c3c6aSMatthew Dillon * Destination has valid swapblk or it is represented 9601c7c3c6aSMatthew Dillon * by a resident page. We destroy the sourceblock. 9611c7c3c6aSMatthew Dillon */ 9621c7c3c6aSMatthew Dillon 9631c7c3c6aSMatthew Dillon swp_pager_meta_ctl(srcobject, i + offset, SWM_FREE); 9641c7c3c6aSMatthew Dillon } 96526f9a767SRodney W. Grimes } 96626f9a767SRodney W. Grimes 96726f9a767SRodney W. Grimes /* 9681c7c3c6aSMatthew Dillon * Free left over swap blocks in source. 9691c7c3c6aSMatthew Dillon * 970763df3ecSPedro F. Giffuni * We have to revert the type to OBJT_DEFAULT so we do not accidentally 9711c7c3c6aSMatthew Dillon * double-remove the object from the swap queues. 97226f9a767SRodney W. Grimes */ 973c0877f10SJohn Dyson if (destroysource) { 9741c7c3c6aSMatthew Dillon swp_pager_meta_free_all(srcobject); 9751c7c3c6aSMatthew Dillon /* 9761c7c3c6aSMatthew Dillon * Reverting the type is not necessary, the caller is going 9771c7c3c6aSMatthew Dillon * to destroy srcobject directly, but I'm doing it here 978956f3135SPhilippe Charnier * for consistency since we've removed the object from its 9791c7c3c6aSMatthew Dillon * queues. 9801c7c3c6aSMatthew Dillon */ 9811c7c3c6aSMatthew Dillon srcobject->type = OBJT_DEFAULT; 982c0877f10SJohn Dyson } 98326f9a767SRodney W. Grimes } 98426f9a767SRodney W. Grimes 985df8bae1dSRodney W. Grimes /* 9861c7c3c6aSMatthew Dillon * SWAP_PAGER_HASPAGE() - determine if we have good backing store for 9871c7c3c6aSMatthew Dillon * the requested page. 9881c7c3c6aSMatthew Dillon * 9891c7c3c6aSMatthew Dillon * We determine whether good backing store exists for the requested 9901c7c3c6aSMatthew Dillon * page and return TRUE if it does, FALSE if it doesn't. 9911c7c3c6aSMatthew Dillon * 9921c7c3c6aSMatthew Dillon * If TRUE, we also try to determine how much valid, contiguous backing 993*915d1b71SMark Johnston * store exists before and after the requested page. 994df8bae1dSRodney W. Grimes */ 9955ea4972cSAlan Cox static boolean_t 996*915d1b71SMark Johnston swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, 997*915d1b71SMark Johnston int *after) 99826f9a767SRodney W. Grimes { 999*915d1b71SMark Johnston daddr_t blk, blk0; 1000*915d1b71SMark Johnston int i; 100126f9a767SRodney W. Grimes 1002c25673ffSAttilio Rao VM_OBJECT_ASSERT_LOCKED(object); 1003*915d1b71SMark Johnston 10041c7c3c6aSMatthew Dillon /* 10051c7c3c6aSMatthew Dillon * do we have good backing store at the requested index ? 10061c7c3c6aSMatthew Dillon */ 10071c7c3c6aSMatthew Dillon blk0 = swp_pager_meta_ctl(object, pindex, 0); 10084dcc5c2dSMatthew Dillon if (blk0 == SWAPBLK_NONE) { 10091c7c3c6aSMatthew Dillon if (before) 101024a1cce3SDavid Greenman *before = 0; 10111c7c3c6aSMatthew Dillon if (after) 101224a1cce3SDavid Greenman *after = 0; 101326f9a767SRodney W. Grimes return (FALSE); 101426f9a767SRodney W. Grimes } 101526f9a767SRodney W. Grimes 101626f9a767SRodney W. Grimes /* 10171c7c3c6aSMatthew Dillon * find backwards-looking contiguous good backing store 1018e47ed70bSJohn Dyson */ 10191c7c3c6aSMatthew Dillon if (before != NULL) { 1020*915d1b71SMark Johnston for (i = 1; i < SWB_NPAGES; i++) { 10211c7c3c6aSMatthew Dillon if (i > pindex) 10221c7c3c6aSMatthew Dillon break; 10231c7c3c6aSMatthew Dillon blk = swp_pager_meta_ctl(object, pindex - i, 0); 10241c7c3c6aSMatthew Dillon if (blk != blk0 - i) 10251c7c3c6aSMatthew Dillon break; 1026ffc82b0aSJohn Dyson } 1027*915d1b71SMark Johnston *before = i - 1; 102826f9a767SRodney W. Grimes } 102926f9a767SRodney W. Grimes 103026f9a767SRodney W. Grimes /* 10311c7c3c6aSMatthew Dillon * find forward-looking contiguous good backing store 103226f9a767SRodney W. Grimes */ 10331c7c3c6aSMatthew Dillon if (after != NULL) { 1034*915d1b71SMark Johnston for (i = 1; i < SWB_NPAGES; i++) { 10351c7c3c6aSMatthew Dillon blk = swp_pager_meta_ctl(object, pindex + i, 0); 10361c7c3c6aSMatthew Dillon if (blk != blk0 + i) 10371c7c3c6aSMatthew Dillon break; 103826f9a767SRodney W. Grimes } 1039*915d1b71SMark Johnston *after = i - 1; 10401c7c3c6aSMatthew Dillon } 10411c7c3c6aSMatthew Dillon return (TRUE); 10421c7c3c6aSMatthew Dillon } 10431c7c3c6aSMatthew Dillon 10441c7c3c6aSMatthew Dillon /* 10451c7c3c6aSMatthew Dillon * SWAP_PAGER_PAGE_UNSWAPPED() - remove swap backing store related to page 10461c7c3c6aSMatthew Dillon * 10471c7c3c6aSMatthew Dillon * This removes any associated swap backing store, whether valid or 10481c7c3c6aSMatthew Dillon * not, from the page. 10491c7c3c6aSMatthew Dillon * 10501c7c3c6aSMatthew Dillon * This routine is typically called when a page is made dirty, at 10511c7c3c6aSMatthew Dillon * which point any associated swap can be freed. MADV_FREE also 10521c7c3c6aSMatthew Dillon * calls us in a special-case situation 10531c7c3c6aSMatthew Dillon * 10541c7c3c6aSMatthew Dillon * NOTE!!! If the page is clean and the swap was valid, the caller 10551c7c3c6aSMatthew Dillon * should make the page dirty before calling this routine. This routine 10561c7c3c6aSMatthew Dillon * does NOT change the m->dirty status of the page. Also: MADV_FREE 10571c7c3c6aSMatthew Dillon * depends on it. 10581c7c3c6aSMatthew Dillon * 105915523cf7SKonstantin Belousov * This routine may not sleep. 1060c25673ffSAttilio Rao * 1061c25673ffSAttilio Rao * The object containing the page must be locked. 10621c7c3c6aSMatthew Dillon */ 10631c7c3c6aSMatthew Dillon static void 10642f249180SPoul-Henning Kamp swap_pager_unswapped(vm_page_t m) 10651c7c3c6aSMatthew Dillon { 10662f249180SPoul-Henning Kamp 10671c7c3c6aSMatthew Dillon swp_pager_meta_ctl(m->object, m->pindex, SWM_FREE); 10681c7c3c6aSMatthew Dillon } 10691c7c3c6aSMatthew Dillon 10701c7c3c6aSMatthew Dillon /* 1071*915d1b71SMark Johnston * swap_pager_getpages() - bring pages in from swap 10721c7c3c6aSMatthew Dillon * 1073*915d1b71SMark Johnston * Attempt to page in the pages in array "m" of length "count". The caller 1074*915d1b71SMark Johnston * may optionally specify that additional pages preceding and succeeding 1075*915d1b71SMark Johnston * the specified range be paged in. The number of such pages is returned 1076*915d1b71SMark Johnston * in the "rbehind" and "rahead" parameters, and they will be in the 1077*915d1b71SMark Johnston * inactive queue upon return. 10781c7c3c6aSMatthew Dillon * 1079*915d1b71SMark Johnston * The pages in "m" must be busied and will remain busied upon return. 10801c7c3c6aSMatthew Dillon */ 1081f708ef1bSPoul-Henning Kamp static int 1082b0cd2017SGleb Smirnoff swap_pager_getpages(vm_object_t object, vm_page_t *m, int count, int *rbehind, 1083b0cd2017SGleb Smirnoff int *rahead) 1084df8bae1dSRodney W. Grimes { 10851c7c3c6aSMatthew Dillon struct buf *bp; 1086*915d1b71SMark Johnston vm_page_t mpred, msucc, p; 1087*915d1b71SMark Johnston vm_pindex_t pindex; 10881c7c3c6aSMatthew Dillon daddr_t blk; 1089*915d1b71SMark Johnston int i, j, reqcount, shift; 10900d94caffSDavid Greenman 1091*915d1b71SMark Johnston reqcount = count; 10921c7c3c6aSMatthew Dillon 109389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 10941c7c3c6aSMatthew Dillon bp = getpbuf(&nsw_rcount); 1095*915d1b71SMark Johnston VM_OBJECT_WLOCK(object); 109626f9a767SRodney W. Grimes 1097*915d1b71SMark Johnston if (!swap_pager_haspage(object, m[0]->pindex, rbehind, rahead)) { 1098*915d1b71SMark Johnston relpbuf(bp, &nsw_rcount); 1099*915d1b71SMark Johnston return (VM_PAGER_FAIL); 1100*915d1b71SMark Johnston } 1101*915d1b71SMark Johnston 1102*915d1b71SMark Johnston /* 1103*915d1b71SMark Johnston * Clip the readahead and readbehind ranges to exclude resident pages. 1104*915d1b71SMark Johnston */ 1105*915d1b71SMark Johnston if (rahead != NULL) { 1106*915d1b71SMark Johnston KASSERT(reqcount - 1 <= *rahead, 1107*915d1b71SMark Johnston ("page count %d extends beyond swap block", reqcount)); 1108*915d1b71SMark Johnston *rahead -= reqcount - 1; 1109*915d1b71SMark Johnston pindex = m[reqcount - 1]->pindex; 1110*915d1b71SMark Johnston msucc = TAILQ_NEXT(m[reqcount - 1], listq); 1111*915d1b71SMark Johnston if (msucc != NULL && msucc->pindex - pindex - 1 < *rahead) 1112*915d1b71SMark Johnston *rahead = msucc->pindex - pindex - 1; 1113*915d1b71SMark Johnston } 1114*915d1b71SMark Johnston if (rbehind != NULL) { 1115*915d1b71SMark Johnston pindex = m[0]->pindex; 1116*915d1b71SMark Johnston mpred = TAILQ_PREV(m[0], pglist, listq); 1117*915d1b71SMark Johnston if (mpred != NULL && pindex - mpred->pindex - 1 < *rbehind) 1118*915d1b71SMark Johnston *rbehind = pindex - mpred->pindex - 1; 1119*915d1b71SMark Johnston } 1120*915d1b71SMark Johnston 1121*915d1b71SMark Johnston /* 1122*915d1b71SMark Johnston * Allocate readahead and readbehind pages. 1123*915d1b71SMark Johnston */ 1124*915d1b71SMark Johnston shift = rbehind != NULL ? *rbehind : 0; 1125*915d1b71SMark Johnston if (shift != 0) { 1126*915d1b71SMark Johnston for (i = 1; i <= shift; i++) { 1127*915d1b71SMark Johnston p = vm_page_alloc(object, m[0]->pindex - i, 1128*915d1b71SMark Johnston VM_ALLOC_NORMAL | VM_ALLOC_IFNOTCACHED); 1129*915d1b71SMark Johnston if (p == NULL) { 1130*915d1b71SMark Johnston /* Shift allocated pages to the left. */ 1131*915d1b71SMark Johnston for (j = 0; j < i - 1; j++) 1132*915d1b71SMark Johnston bp->b_pages[j] = 1133*915d1b71SMark Johnston bp->b_pages[j + shift - i + 1]; 1134*915d1b71SMark Johnston break; 1135*915d1b71SMark Johnston } 1136*915d1b71SMark Johnston bp->b_pages[shift - i] = p; 1137*915d1b71SMark Johnston } 1138*915d1b71SMark Johnston shift = i - 1; 1139*915d1b71SMark Johnston *rbehind = shift; 1140*915d1b71SMark Johnston } 1141*915d1b71SMark Johnston for (i = 0; i < reqcount; i++) 1142*915d1b71SMark Johnston bp->b_pages[i + shift] = m[i]; 1143*915d1b71SMark Johnston if (rahead != NULL) { 1144*915d1b71SMark Johnston for (i = 0; i < *rahead; i++) { 1145*915d1b71SMark Johnston p = vm_page_alloc(object, 1146*915d1b71SMark Johnston m[reqcount - 1]->pindex + i + 1, 1147*915d1b71SMark Johnston VM_ALLOC_NORMAL | VM_ALLOC_IFNOTCACHED); 1148*915d1b71SMark Johnston if (p == NULL) 1149*915d1b71SMark Johnston break; 1150*915d1b71SMark Johnston bp->b_pages[shift + reqcount + i] = p; 1151*915d1b71SMark Johnston } 1152*915d1b71SMark Johnston *rahead = i; 1153*915d1b71SMark Johnston } 1154*915d1b71SMark Johnston if (rbehind != NULL) 1155*915d1b71SMark Johnston count += *rbehind; 1156*915d1b71SMark Johnston if (rahead != NULL) 1157*915d1b71SMark Johnston count += *rahead; 1158*915d1b71SMark Johnston 1159*915d1b71SMark Johnston vm_object_pip_add(object, count); 1160*915d1b71SMark Johnston 1161*915d1b71SMark Johnston for (i = 0; i < count; i++) 1162*915d1b71SMark Johnston bp->b_pages[i]->oflags |= VPO_SWAPINPROG; 1163*915d1b71SMark Johnston 1164*915d1b71SMark Johnston pindex = bp->b_pages[0]->pindex; 1165*915d1b71SMark Johnston blk = swp_pager_meta_ctl(object, pindex, 0); 1166*915d1b71SMark Johnston KASSERT(blk != SWAPBLK_NONE, 1167*915d1b71SMark Johnston ("no swap blocking containing %p(%jx)", object, (uintmax_t)pindex)); 1168*915d1b71SMark Johnston 1169*915d1b71SMark Johnston VM_OBJECT_WUNLOCK(object); 1170*915d1b71SMark Johnston 1171*915d1b71SMark Johnston bp->b_flags |= B_PAGING; 117221144e3bSPoul-Henning Kamp bp->b_iocmd = BIO_READ; 11731c7c3c6aSMatthew Dillon bp->b_iodone = swp_pager_async_iodone; 1174fdcc1cc0SJohn Baldwin bp->b_rcred = crhold(thread0.td_ucred); 1175fdcc1cc0SJohn Baldwin bp->b_wcred = crhold(thread0.td_ucred); 1176b0cd2017SGleb Smirnoff bp->b_blkno = blk; 1177b0cd2017SGleb Smirnoff bp->b_bcount = PAGE_SIZE * count; 1178b0cd2017SGleb Smirnoff bp->b_bufsize = PAGE_SIZE * count; 1179b0cd2017SGleb Smirnoff bp->b_npages = count; 1180*915d1b71SMark Johnston bp->b_pgbefore = rbehind != NULL ? *rbehind : 0; 1181*915d1b71SMark Johnston bp->b_pgafter = rahead != NULL ? *rahead : 0; 118226f9a767SRodney W. Grimes 1183b4b70819SAttilio Rao PCPU_INC(cnt.v_swapin); 1184*915d1b71SMark Johnston PCPU_ADD(cnt.v_swappgsin, count); 11851c7c3c6aSMatthew Dillon 11861c7c3c6aSMatthew Dillon /* 11871c7c3c6aSMatthew Dillon * perform the I/O. NOTE!!! bp cannot be considered valid after 11881c7c3c6aSMatthew Dillon * this point because we automatically release it on completion. 11891c7c3c6aSMatthew Dillon * Instead, we look at the one page we are interested in which we 11901c7c3c6aSMatthew Dillon * still hold a lock on even through the I/O completion. 11911c7c3c6aSMatthew Dillon * 11921c7c3c6aSMatthew Dillon * The other pages in our m[] array are also released on completion, 11931c7c3c6aSMatthew Dillon * so we cannot assume they are valid anymore either. 11941c7c3c6aSMatthew Dillon * 1195c37a77eeSPoul-Henning Kamp * NOTE: b_blkno is destroyed by the call to swapdev_strategy 11961c7c3c6aSMatthew Dillon */ 1197b890cb2cSPeter Wemm BUF_KERNPROC(bp); 11984b03903aSPoul-Henning Kamp swp_pager_strategy(bp); 119926f9a767SRodney W. Grimes 120026f9a767SRodney W. Grimes /* 1201*915d1b71SMark Johnston * Wait for the pages we want to complete. VPO_SWAPINPROG is always 12021c7c3c6aSMatthew Dillon * cleared on completion. If an I/O error occurs, SWAPBLK_NONE 1203*915d1b71SMark Johnston * is set in the metadata for each page in the request. 120426f9a767SRodney W. Grimes */ 120589f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1206b0cd2017SGleb Smirnoff while ((m[0]->oflags & VPO_SWAPINPROG) != 0) { 1207b0cd2017SGleb Smirnoff m[0]->oflags |= VPO_SWAPSLEEP; 1208b4b70819SAttilio Rao PCPU_INC(cnt.v_intrans); 1209c7aebda8SAttilio Rao if (VM_OBJECT_SLEEP(object, &object->paging_in_progress, PSWP, 1210c7aebda8SAttilio Rao "swread", hz * 20)) { 12119bd86a98SBruce M Simpson printf( 1212c5690651SPoul-Henning Kamp "swap_pager: indefinite wait buffer: bufobj: %p, blkno: %jd, size: %ld\n", 1213c5690651SPoul-Henning Kamp bp->b_bufobj, (intmax_t)bp->b_blkno, bp->b_bcount); 12141c7c3c6aSMatthew Dillon } 12151b119d9dSDavid Greenman } 121626f9a767SRodney W. Grimes 121726f9a767SRodney W. Grimes /* 1218b0cd2017SGleb Smirnoff * If we had an unrecoverable read error pages will not be valid. 121926f9a767SRodney W. Grimes */ 1220*915d1b71SMark Johnston for (i = 0; i < reqcount; i++) 1221b0cd2017SGleb Smirnoff if (m[i]->valid != VM_PAGE_BITS_ALL) 12221c7c3c6aSMatthew Dillon return (VM_PAGER_ERROR); 1223b0cd2017SGleb Smirnoff 12241c7c3c6aSMatthew Dillon return (VM_PAGER_OK); 12251c7c3c6aSMatthew Dillon 12261c7c3c6aSMatthew Dillon /* 12271c7c3c6aSMatthew Dillon * A final note: in a low swap situation, we cannot deallocate swap 12281c7c3c6aSMatthew Dillon * and mark a page dirty here because the caller is likely to mark 12291c7c3c6aSMatthew Dillon * the page clean when we return, causing the page to possibly revert 12301c7c3c6aSMatthew Dillon * to all-zero's later. 12311c7c3c6aSMatthew Dillon */ 1232df8bae1dSRodney W. Grimes } 1233df8bae1dSRodney W. Grimes 12341c7c3c6aSMatthew Dillon /* 123590effb23SGleb Smirnoff * swap_pager_getpages_async(): 123690effb23SGleb Smirnoff * 123790effb23SGleb Smirnoff * Right now this is emulation of asynchronous operation on top of 123890effb23SGleb Smirnoff * swap_pager_getpages(). 123990effb23SGleb Smirnoff */ 124090effb23SGleb Smirnoff static int 124190effb23SGleb Smirnoff swap_pager_getpages_async(vm_object_t object, vm_page_t *m, int count, 1242b0cd2017SGleb Smirnoff int *rbehind, int *rahead, pgo_getpages_iodone_t iodone, void *arg) 124390effb23SGleb Smirnoff { 124490effb23SGleb Smirnoff int r, error; 124590effb23SGleb Smirnoff 1246b0cd2017SGleb Smirnoff r = swap_pager_getpages(object, m, count, rbehind, rahead); 124790effb23SGleb Smirnoff VM_OBJECT_WUNLOCK(object); 124890effb23SGleb Smirnoff switch (r) { 124990effb23SGleb Smirnoff case VM_PAGER_OK: 125090effb23SGleb Smirnoff error = 0; 125190effb23SGleb Smirnoff break; 125290effb23SGleb Smirnoff case VM_PAGER_ERROR: 125390effb23SGleb Smirnoff error = EIO; 125490effb23SGleb Smirnoff break; 125590effb23SGleb Smirnoff case VM_PAGER_FAIL: 125690effb23SGleb Smirnoff error = EINVAL; 125790effb23SGleb Smirnoff break; 125890effb23SGleb Smirnoff default: 1259d9328101SGleb Smirnoff panic("unhandled swap_pager_getpages() error %d", r); 126090effb23SGleb Smirnoff } 126190effb23SGleb Smirnoff (iodone)(arg, m, count, error); 126290effb23SGleb Smirnoff VM_OBJECT_WLOCK(object); 126390effb23SGleb Smirnoff 126490effb23SGleb Smirnoff return (r); 126590effb23SGleb Smirnoff } 126690effb23SGleb Smirnoff 126790effb23SGleb Smirnoff /* 12681c7c3c6aSMatthew Dillon * swap_pager_putpages: 12691c7c3c6aSMatthew Dillon * 12701c7c3c6aSMatthew Dillon * Assign swap (if necessary) and initiate I/O on the specified pages. 12711c7c3c6aSMatthew Dillon * 12721c7c3c6aSMatthew Dillon * We support both OBJT_DEFAULT and OBJT_SWAP objects. DEFAULT objects 12731c7c3c6aSMatthew Dillon * are automatically converted to SWAP objects. 12741c7c3c6aSMatthew Dillon * 1275ea3aecf5SPeter Wemm * In a low memory situation we may block in VOP_STRATEGY(), but the new 12761c7c3c6aSMatthew Dillon * vm_page reservation system coupled with properly written VFS devices 12771c7c3c6aSMatthew Dillon * should ensure that no low-memory deadlock occurs. This is an area 12781c7c3c6aSMatthew Dillon * which needs work. 12791c7c3c6aSMatthew Dillon * 12801c7c3c6aSMatthew Dillon * The parent has N vm_object_pip_add() references prior to 12811c7c3c6aSMatthew Dillon * calling us and will remove references for rtvals[] that are 12821c7c3c6aSMatthew Dillon * not set to VM_PAGER_PEND. We need to remove the rest on I/O 12831c7c3c6aSMatthew Dillon * completion. 12841c7c3c6aSMatthew Dillon * 12851c7c3c6aSMatthew Dillon * The parent has soft-busy'd the pages it passes us and will unbusy 12861c7c3c6aSMatthew Dillon * those whos rtvals[] entry is not set to VM_PAGER_PEND on return. 12871c7c3c6aSMatthew Dillon * We need to unbusy the rest on I/O completion. 12881c7c3c6aSMatthew Dillon */ 1289d635a37fSWarner Losh static void 12902f249180SPoul-Henning Kamp swap_pager_putpages(vm_object_t object, vm_page_t *m, int count, 1291e065e87cSKonstantin Belousov int flags, int *rtvals) 1292df8bae1dSRodney W. Grimes { 1293e065e87cSKonstantin Belousov int i, n; 1294e065e87cSKonstantin Belousov boolean_t sync; 1295df8bae1dSRodney W. Grimes 12961c7c3c6aSMatthew Dillon if (count && m[0]->object != object) { 12977036145bSMaxim Konovalov panic("swap_pager_putpages: object mismatch %p/%p", 12981c7c3c6aSMatthew Dillon object, 12991c7c3c6aSMatthew Dillon m[0]->object 13001c7c3c6aSMatthew Dillon ); 13011c7c3c6aSMatthew Dillon } 1302ee3dc7d7SAlan Cox 13031c7c3c6aSMatthew Dillon /* 13041c7c3c6aSMatthew Dillon * Step 1 13051c7c3c6aSMatthew Dillon * 13061c7c3c6aSMatthew Dillon * Turn object into OBJT_SWAP 13071c7c3c6aSMatthew Dillon * check for bogus sysops 13081c7c3c6aSMatthew Dillon * force sync if not pageout process 13091c7c3c6aSMatthew Dillon */ 13104dcc5c2dSMatthew Dillon if (object->type != OBJT_SWAP) 13114dcc5c2dSMatthew Dillon swp_pager_meta_build(object, 0, SWAPBLK_NONE); 131289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1313e47ed70bSJohn Dyson 1314e065e87cSKonstantin Belousov n = 0; 1315e47ed70bSJohn Dyson if (curproc != pageproc) 1316e47ed70bSJohn Dyson sync = TRUE; 1317e065e87cSKonstantin Belousov else 1318e065e87cSKonstantin Belousov sync = (flags & VM_PAGER_PUT_SYNC) != 0; 131926f9a767SRodney W. Grimes 13201c7c3c6aSMatthew Dillon /* 13211c7c3c6aSMatthew Dillon * Step 2 13221c7c3c6aSMatthew Dillon * 13231c7c3c6aSMatthew Dillon * Assign swap blocks and issue I/O. We reallocate swap on the fly. 13241c7c3c6aSMatthew Dillon * The page is left dirty until the pageout operation completes 13251c7c3c6aSMatthew Dillon * successfully. 13261c7c3c6aSMatthew Dillon */ 13271c7c3c6aSMatthew Dillon for (i = 0; i < count; i += n) { 13281c7c3c6aSMatthew Dillon int j; 13291c7c3c6aSMatthew Dillon struct buf *bp; 1330a316d390SJohn Dyson daddr_t blk; 133126f9a767SRodney W. Grimes 1332df8bae1dSRodney W. Grimes /* 13331c7c3c6aSMatthew Dillon * Maximum I/O size is limited by a number of factors. 1334df8bae1dSRodney W. Grimes */ 13351c7c3c6aSMatthew Dillon n = min(BLIST_MAX_ALLOC, count - i); 1336327f4e83SMatthew Dillon n = min(n, nsw_cluster_max); 13371c7c3c6aSMatthew Dillon 133826f9a767SRodney W. Grimes /* 13391c7c3c6aSMatthew Dillon * Get biggest block of swap we can. If we fail, fall 13401c7c3c6aSMatthew Dillon * back and try to allocate a smaller block. Don't go 13411c7c3c6aSMatthew Dillon * overboard trying to allocate space if it would overly 13421c7c3c6aSMatthew Dillon * fragment swap. 134326f9a767SRodney W. Grimes */ 13441c7c3c6aSMatthew Dillon while ( 13451c7c3c6aSMatthew Dillon (blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE && 13461c7c3c6aSMatthew Dillon n > 4 13471c7c3c6aSMatthew Dillon ) { 13481c7c3c6aSMatthew Dillon n >>= 1; 134926f9a767SRodney W. Grimes } 13501c7c3c6aSMatthew Dillon if (blk == SWAPBLK_NONE) { 13514dcc5c2dSMatthew Dillon for (j = 0; j < n; ++j) 13521c7c3c6aSMatthew Dillon rtvals[i+j] = VM_PAGER_FAIL; 13531c7c3c6aSMatthew Dillon continue; 135426f9a767SRodney W. Grimes } 135526f9a767SRodney W. Grimes 135626f9a767SRodney W. Grimes /* 13571c7c3c6aSMatthew Dillon * All I/O parameters have been satisfied, build the I/O 13581c7c3c6aSMatthew Dillon * request and assign the swap space. 135926f9a767SRodney W. Grimes */ 1360327f4e83SMatthew Dillon if (sync == TRUE) { 1361327f4e83SMatthew Dillon bp = getpbuf(&nsw_wcount_sync); 1362327f4e83SMatthew Dillon } else { 1363327f4e83SMatthew Dillon bp = getpbuf(&nsw_wcount_async); 136421144e3bSPoul-Henning Kamp bp->b_flags = B_ASYNC; 1365327f4e83SMatthew Dillon } 13665e04322aSPoul-Henning Kamp bp->b_flags |= B_PAGING; 1367912e4ae9SPoul-Henning Kamp bp->b_iocmd = BIO_WRITE; 136826f9a767SRodney W. Grimes 1369fdcc1cc0SJohn Baldwin bp->b_rcred = crhold(thread0.td_ucred); 1370fdcc1cc0SJohn Baldwin bp->b_wcred = crhold(thread0.td_ucred); 13711c7c3c6aSMatthew Dillon bp->b_bcount = PAGE_SIZE * n; 13721c7c3c6aSMatthew Dillon bp->b_bufsize = PAGE_SIZE * n; 13731c7c3c6aSMatthew Dillon bp->b_blkno = blk; 1374e47ed70bSJohn Dyson 137589f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 13761c7c3c6aSMatthew Dillon for (j = 0; j < n; ++j) { 13771c7c3c6aSMatthew Dillon vm_page_t mreq = m[i+j]; 13781c7c3c6aSMatthew Dillon 13791c7c3c6aSMatthew Dillon swp_pager_meta_build( 13801c7c3c6aSMatthew Dillon mreq->object, 13811c7c3c6aSMatthew Dillon mreq->pindex, 13824dcc5c2dSMatthew Dillon blk + j 13831c7c3c6aSMatthew Dillon ); 13847dbf82dcSMatthew Dillon vm_page_dirty(mreq); 13855786be7cSAlan Cox mreq->oflags |= VPO_SWAPINPROG; 13861c7c3c6aSMatthew Dillon bp->b_pages[j] = mreq; 13871c7c3c6aSMatthew Dillon } 138889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 13891c7c3c6aSMatthew Dillon bp->b_npages = n; 1390a5296b05SJulian Elischer /* 1391a5296b05SJulian Elischer * Must set dirty range for NFS to work. 1392a5296b05SJulian Elischer */ 1393a5296b05SJulian Elischer bp->b_dirtyoff = 0; 1394a5296b05SJulian Elischer bp->b_dirtyend = bp->b_bcount; 13951c7c3c6aSMatthew Dillon 1396b4b70819SAttilio Rao PCPU_INC(cnt.v_swapout); 1397b4b70819SAttilio Rao PCPU_ADD(cnt.v_swappgsout, bp->b_npages); 139826f9a767SRodney W. Grimes 139926f9a767SRodney W. Grimes /* 140077923df2SAlan Cox * We unconditionally set rtvals[] to VM_PAGER_PEND so that we 140177923df2SAlan Cox * can call the async completion routine at the end of a 140277923df2SAlan Cox * synchronous I/O operation. Otherwise, our caller would 140377923df2SAlan Cox * perform duplicate unbusy and wakeup operations on the page 140477923df2SAlan Cox * and object, respectively. 140577923df2SAlan Cox */ 140677923df2SAlan Cox for (j = 0; j < n; j++) 140777923df2SAlan Cox rtvals[i + j] = VM_PAGER_PEND; 140877923df2SAlan Cox 140977923df2SAlan Cox /* 14101c7c3c6aSMatthew Dillon * asynchronous 14111c7c3c6aSMatthew Dillon * 1412c37a77eeSPoul-Henning Kamp * NOTE: b_blkno is destroyed by the call to swapdev_strategy 141326f9a767SRodney W. Grimes */ 14141c7c3c6aSMatthew Dillon if (sync == FALSE) { 14151c7c3c6aSMatthew Dillon bp->b_iodone = swp_pager_async_iodone; 141667812eacSKirk McKusick BUF_KERNPROC(bp); 14174b03903aSPoul-Henning Kamp swp_pager_strategy(bp); 14181c7c3c6aSMatthew Dillon continue; 141926f9a767SRodney W. Grimes } 1420e47ed70bSJohn Dyson 142126f9a767SRodney W. Grimes /* 14221c7c3c6aSMatthew Dillon * synchronous 14231c7c3c6aSMatthew Dillon * 1424c37a77eeSPoul-Henning Kamp * NOTE: b_blkno is destroyed by the call to swapdev_strategy 14251c7c3c6aSMatthew Dillon */ 14262c840b1fSAlan Cox bp->b_iodone = bdone; 14274b03903aSPoul-Henning Kamp swp_pager_strategy(bp); 14281c7c3c6aSMatthew Dillon 14291c7c3c6aSMatthew Dillon /* 143077923df2SAlan Cox * Wait for the sync I/O to complete. 143126f9a767SRodney W. Grimes */ 14322c840b1fSAlan Cox bwait(bp, PVM, "swwrt"); 143377923df2SAlan Cox 14341c7c3c6aSMatthew Dillon /* 14351c7c3c6aSMatthew Dillon * Now that we are through with the bp, we can call the 14361c7c3c6aSMatthew Dillon * normal async completion, which frees everything up. 14371c7c3c6aSMatthew Dillon */ 14381c7c3c6aSMatthew Dillon swp_pager_async_iodone(bp); 14391c7c3c6aSMatthew Dillon } 144089f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 14411c7c3c6aSMatthew Dillon } 14421c7c3c6aSMatthew Dillon 14431c7c3c6aSMatthew Dillon /* 14441c7c3c6aSMatthew Dillon * swp_pager_async_iodone: 14451c7c3c6aSMatthew Dillon * 14461c7c3c6aSMatthew Dillon * Completion routine for asynchronous reads and writes from/to swap. 14471c7c3c6aSMatthew Dillon * Also called manually by synchronous code to finish up a bp. 14481c7c3c6aSMatthew Dillon * 144915523cf7SKonstantin Belousov * This routine may not sleep. 14501c7c3c6aSMatthew Dillon */ 14511c7c3c6aSMatthew Dillon static void 14522f249180SPoul-Henning Kamp swp_pager_async_iodone(struct buf *bp) 14531c7c3c6aSMatthew Dillon { 14541c7c3c6aSMatthew Dillon int i; 14551c7c3c6aSMatthew Dillon vm_object_t object = NULL; 14561c7c3c6aSMatthew Dillon 14571c7c3c6aSMatthew Dillon /* 14581c7c3c6aSMatthew Dillon * report error 14591c7c3c6aSMatthew Dillon */ 1460c244d2deSPoul-Henning Kamp if (bp->b_ioflags & BIO_ERROR) { 14611c7c3c6aSMatthew Dillon printf( 14621c7c3c6aSMatthew Dillon "swap_pager: I/O error - %s failed; blkno %ld," 14631c7c3c6aSMatthew Dillon "size %ld, error %d\n", 146421144e3bSPoul-Henning Kamp ((bp->b_iocmd == BIO_READ) ? "pagein" : "pageout"), 14651c7c3c6aSMatthew Dillon (long)bp->b_blkno, 14661c7c3c6aSMatthew Dillon (long)bp->b_bcount, 14671c7c3c6aSMatthew Dillon bp->b_error 14681c7c3c6aSMatthew Dillon ); 14691c7c3c6aSMatthew Dillon } 14701c7c3c6aSMatthew Dillon 14711c7c3c6aSMatthew Dillon /* 147226f9a767SRodney W. Grimes * remove the mapping for kernel virtual 147326f9a767SRodney W. Grimes */ 1474fade8dd7SJeff Roberson if (buf_mapped(bp)) 14751c7c3c6aSMatthew Dillon pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages); 1476fade8dd7SJeff Roberson else 1477fade8dd7SJeff Roberson bp->b_data = bp->b_kvabase; 147826f9a767SRodney W. Grimes 147933a609ecSAlan Cox if (bp->b_npages) { 148033a609ecSAlan Cox object = bp->b_pages[0]->object; 148189f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 148233a609ecSAlan Cox } 14832965a453SKip Macy 148426f9a767SRodney W. Grimes /* 14851c7c3c6aSMatthew Dillon * cleanup pages. If an error occurs writing to swap, we are in 14861c7c3c6aSMatthew Dillon * very serious trouble. If it happens to be a disk error, though, 14871c7c3c6aSMatthew Dillon * we may be able to recover by reassigning the swap later on. So 14881c7c3c6aSMatthew Dillon * in this case we remove the m->swapblk assignment for the page 14891c7c3c6aSMatthew Dillon * but do not free it in the rlist. The errornous block(s) are thus 14901c7c3c6aSMatthew Dillon * never reallocated as swap. Redirty the page and continue. 149126f9a767SRodney W. Grimes */ 14921c7c3c6aSMatthew Dillon for (i = 0; i < bp->b_npages; ++i) { 14931c7c3c6aSMatthew Dillon vm_page_t m = bp->b_pages[i]; 1494e47ed70bSJohn Dyson 14955786be7cSAlan Cox m->oflags &= ~VPO_SWAPINPROG; 1496c7aebda8SAttilio Rao if (m->oflags & VPO_SWAPSLEEP) { 1497c7aebda8SAttilio Rao m->oflags &= ~VPO_SWAPSLEEP; 1498c7aebda8SAttilio Rao wakeup(&object->paging_in_progress); 1499c7aebda8SAttilio Rao } 1500e47ed70bSJohn Dyson 1501c244d2deSPoul-Henning Kamp if (bp->b_ioflags & BIO_ERROR) { 1502ffc82b0aSJohn Dyson /* 15031c7c3c6aSMatthew Dillon * If an error occurs I'd love to throw the swapblk 15041c7c3c6aSMatthew Dillon * away without freeing it back to swapspace, so it 15051c7c3c6aSMatthew Dillon * can never be used again. But I can't from an 15061c7c3c6aSMatthew Dillon * interrupt. 1507ffc82b0aSJohn Dyson */ 150821144e3bSPoul-Henning Kamp if (bp->b_iocmd == BIO_READ) { 15091c7c3c6aSMatthew Dillon /* 15101c7c3c6aSMatthew Dillon * NOTE: for reads, m->dirty will probably 1511956f3135SPhilippe Charnier * be overridden by the original caller of 15121c7c3c6aSMatthew Dillon * getpages so don't play cute tricks here. 15131c7c3c6aSMatthew Dillon */ 15141c7c3c6aSMatthew Dillon m->valid = 0; 15151c7c3c6aSMatthew Dillon } else { 15161c7c3c6aSMatthew Dillon /* 15171c7c3c6aSMatthew Dillon * If a write error occurs, reactivate page 15181c7c3c6aSMatthew Dillon * so it doesn't clog the inactive list, 15191c7c3c6aSMatthew Dillon * then finish the I/O. 15201c7c3c6aSMatthew Dillon */ 15217dbf82dcSMatthew Dillon vm_page_dirty(m); 15223c4a2440SAlan Cox vm_page_lock(m); 15231c7c3c6aSMatthew Dillon vm_page_activate(m); 15243c4a2440SAlan Cox vm_page_unlock(m); 1525c7aebda8SAttilio Rao vm_page_sunbusy(m); 15261c7c3c6aSMatthew Dillon } 152721144e3bSPoul-Henning Kamp } else if (bp->b_iocmd == BIO_READ) { 15281c7c3c6aSMatthew Dillon /* 15291c7c3c6aSMatthew Dillon * NOTE: for reads, m->dirty will probably be 1530956f3135SPhilippe Charnier * overridden by the original caller of getpages so 15311c7c3c6aSMatthew Dillon * we cannot set them in order to free the underlying 15321c7c3c6aSMatthew Dillon * swap in a low-swap situation. I don't think we'd 15331c7c3c6aSMatthew Dillon * want to do that anyway, but it was an optimization 15341c7c3c6aSMatthew Dillon * that existed in the old swapper for a time before 15351c7c3c6aSMatthew Dillon * it got ripped out due to precisely this problem. 15361c7c3c6aSMatthew Dillon */ 1537016a3c93SAlan Cox KASSERT(!pmap_page_is_mapped(m), 1538016a3c93SAlan Cox ("swp_pager_async_iodone: page %p is mapped", m)); 1539016a3c93SAlan Cox KASSERT(m->dirty == 0, 1540016a3c93SAlan Cox ("swp_pager_async_iodone: page %p is dirty", m)); 1541*915d1b71SMark Johnston 1542b0cd2017SGleb Smirnoff m->valid = VM_PAGE_BITS_ALL; 1543*915d1b71SMark Johnston if (i < bp->b_pgbefore || 1544*915d1b71SMark Johnston i >= bp->b_npages - bp->b_pgafter) 1545*915d1b71SMark Johnston vm_page_readahead_finish(m); 15461c7c3c6aSMatthew Dillon } else { 15471c7c3c6aSMatthew Dillon /* 1548016a3c93SAlan Cox * For write success, clear the dirty 15491c7c3c6aSMatthew Dillon * status, then finish the I/O ( which decrements the 15501c7c3c6aSMatthew Dillon * busy count and possibly wakes waiter's up ). 15511c7c3c6aSMatthew Dillon */ 15526031c68dSAlan Cox KASSERT(!pmap_page_is_write_mapped(m), 1553016a3c93SAlan Cox ("swp_pager_async_iodone: page %p is not write" 1554016a3c93SAlan Cox " protected", m)); 1555c52e7044SAlan Cox vm_page_undirty(m); 1556c7aebda8SAttilio Rao vm_page_sunbusy(m); 15573c4a2440SAlan Cox if (vm_page_count_severe()) { 15583c4a2440SAlan Cox vm_page_lock(m); 15592c840b1fSAlan Cox vm_page_try_to_cache(m); 15602965a453SKip Macy vm_page_unlock(m); 15612965a453SKip Macy } 15623c4a2440SAlan Cox } 15633c4a2440SAlan Cox } 156426f9a767SRodney W. Grimes 15651c7c3c6aSMatthew Dillon /* 15661c7c3c6aSMatthew Dillon * adjust pip. NOTE: the original parent may still have its own 15671c7c3c6aSMatthew Dillon * pip refs on the object. 15681c7c3c6aSMatthew Dillon */ 15690d420ad3SAlan Cox if (object != NULL) { 15701c7c3c6aSMatthew Dillon vm_object_pip_wakeupn(object, bp->b_npages); 157189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 15720d420ad3SAlan Cox } 157326f9a767SRodney W. Grimes 15741c7c3c6aSMatthew Dillon /* 1575100650deSOlivier Houchard * swapdev_strategy() manually sets b_vp and b_bufobj before calling 1576100650deSOlivier Houchard * bstrategy(). Set them back to NULL now we're done with it, or we'll 1577100650deSOlivier Houchard * trigger a KASSERT in relpbuf(). 1578100650deSOlivier Houchard */ 1579100650deSOlivier Houchard if (bp->b_vp) { 1580100650deSOlivier Houchard bp->b_vp = NULL; 1581100650deSOlivier Houchard bp->b_bufobj = NULL; 1582100650deSOlivier Houchard } 1583100650deSOlivier Houchard /* 15841c7c3c6aSMatthew Dillon * release the physical I/O buffer 15851c7c3c6aSMatthew Dillon */ 1586327f4e83SMatthew Dillon relpbuf( 1587327f4e83SMatthew Dillon bp, 158821144e3bSPoul-Henning Kamp ((bp->b_iocmd == BIO_READ) ? &nsw_rcount : 1589327f4e83SMatthew Dillon ((bp->b_flags & B_ASYNC) ? 1590327f4e83SMatthew Dillon &nsw_wcount_async : 1591327f4e83SMatthew Dillon &nsw_wcount_sync 1592327f4e83SMatthew Dillon ) 1593327f4e83SMatthew Dillon ) 1594327f4e83SMatthew Dillon ); 159526f9a767SRodney W. Grimes } 15961c7c3c6aSMatthew Dillon 159792da00bbSMatthew Dillon /* 159892da00bbSMatthew Dillon * swap_pager_isswapped: 159992da00bbSMatthew Dillon * 160092da00bbSMatthew Dillon * Return 1 if at least one page in the given object is paged 160192da00bbSMatthew Dillon * out to the given swap device. 160292da00bbSMatthew Dillon * 160315523cf7SKonstantin Belousov * This routine may not sleep. 160492da00bbSMatthew Dillon */ 16058f60c087SPoul-Henning Kamp int 16068f60c087SPoul-Henning Kamp swap_pager_isswapped(vm_object_t object, struct swdevt *sp) 16078f60c087SPoul-Henning Kamp { 160892da00bbSMatthew Dillon daddr_t index = 0; 160992da00bbSMatthew Dillon int bcount; 161092da00bbSMatthew Dillon int i; 161192da00bbSMatthew Dillon 161289f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 1613f6bcadc4SDavid Schultz if (object->type != OBJT_SWAP) 1614f6bcadc4SDavid Schultz return (0); 1615f6bcadc4SDavid Schultz 1616b3fed13eSDavid Schultz mtx_lock(&swhash_mtx); 161792da00bbSMatthew Dillon for (bcount = 0; bcount < object->un_pager.swp.swp_bcount; bcount++) { 161892da00bbSMatthew Dillon struct swblock *swap; 161992da00bbSMatthew Dillon 162092da00bbSMatthew Dillon if ((swap = *swp_pager_hash(object, index)) != NULL) { 162192da00bbSMatthew Dillon for (i = 0; i < SWAP_META_PAGES; ++i) { 1622b3fed13eSDavid Schultz if (swp_pager_isondev(swap->swb_pages[i], sp)) { 16237827d9b0SAlan Cox mtx_unlock(&swhash_mtx); 1624b3fed13eSDavid Schultz return (1); 162592da00bbSMatthew Dillon } 162692da00bbSMatthew Dillon } 16277827d9b0SAlan Cox } 162892da00bbSMatthew Dillon index += SWAP_META_PAGES; 162992da00bbSMatthew Dillon } 1630b3fed13eSDavid Schultz mtx_unlock(&swhash_mtx); 1631b3fed13eSDavid Schultz return (0); 163292da00bbSMatthew Dillon } 163392da00bbSMatthew Dillon 163492da00bbSMatthew Dillon /* 163592da00bbSMatthew Dillon * SWP_PAGER_FORCE_PAGEIN() - force a swap block to be paged in 163692da00bbSMatthew Dillon * 163792da00bbSMatthew Dillon * This routine dissociates the page at the given index within a 163892da00bbSMatthew Dillon * swap block from its backing store, paging it in if necessary. 163992da00bbSMatthew Dillon * If the page is paged in, it is placed in the inactive queue, 164092da00bbSMatthew Dillon * since it had its backing store ripped out from under it. 164192da00bbSMatthew Dillon * We also attempt to swap in all other pages in the swap block, 164292da00bbSMatthew Dillon * we only guarantee that the one at the specified index is 164392da00bbSMatthew Dillon * paged in. 164492da00bbSMatthew Dillon * 164592da00bbSMatthew Dillon * XXX - The code to page the whole block in doesn't work, so we 164692da00bbSMatthew Dillon * revert to the one-by-one behavior for now. Sigh. 164792da00bbSMatthew Dillon */ 164862a59e8fSWarner Losh static inline void 1649b3fed13eSDavid Schultz swp_pager_force_pagein(vm_object_t object, vm_pindex_t pindex) 165092da00bbSMatthew Dillon { 165192da00bbSMatthew Dillon vm_page_t m; 165292da00bbSMatthew Dillon 165392da00bbSMatthew Dillon vm_object_pip_add(object, 1); 16545944de8eSKonstantin Belousov m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL); 165592da00bbSMatthew Dillon if (m->valid == VM_PAGE_BITS_ALL) { 16560d8243ccSAttilio Rao vm_object_pip_wakeup(object); 165792da00bbSMatthew Dillon vm_page_dirty(m); 1658d061cdd5SAlan Cox vm_page_lock(m); 1659d061cdd5SAlan Cox vm_page_activate(m); 16602965a453SKip Macy vm_page_unlock(m); 1661c7aebda8SAttilio Rao vm_page_xunbusy(m); 166292da00bbSMatthew Dillon vm_pager_page_unswapped(m); 166392da00bbSMatthew Dillon return; 166492da00bbSMatthew Dillon } 166592da00bbSMatthew Dillon 1666b0cd2017SGleb Smirnoff if (swap_pager_getpages(object, &m, 1, NULL, NULL) != VM_PAGER_OK) 166792da00bbSMatthew Dillon panic("swap_pager_force_pagein: read from swap failed");/*XXX*/ 16680d8243ccSAttilio Rao vm_object_pip_wakeup(object); 166992da00bbSMatthew Dillon vm_page_dirty(m); 1670db1f085eSAlan Cox vm_page_lock(m); 1671db1f085eSAlan Cox vm_page_deactivate(m); 16722965a453SKip Macy vm_page_unlock(m); 1673c7aebda8SAttilio Rao vm_page_xunbusy(m); 167492da00bbSMatthew Dillon vm_pager_page_unswapped(m); 167592da00bbSMatthew Dillon } 167692da00bbSMatthew Dillon 167792da00bbSMatthew Dillon /* 167892da00bbSMatthew Dillon * swap_pager_swapoff: 167992da00bbSMatthew Dillon * 168092da00bbSMatthew Dillon * Page in all of the pages that have been paged out to the 168192da00bbSMatthew Dillon * given device. The corresponding blocks in the bitmap must be 168292da00bbSMatthew Dillon * marked as allocated and the device must be flagged SW_CLOSING. 168392da00bbSMatthew Dillon * There may be no processes swapped out to the device. 168492da00bbSMatthew Dillon * 168592da00bbSMatthew Dillon * This routine may block. 168692da00bbSMatthew Dillon */ 1687e9c0cc15SPoul-Henning Kamp static void 1688b3fed13eSDavid Schultz swap_pager_swapoff(struct swdevt *sp) 168992da00bbSMatthew Dillon { 169092da00bbSMatthew Dillon struct swblock *swap; 16918bc61209SDavid Schultz int i, j, retries; 169292da00bbSMatthew Dillon 169304533e1eSKonstantin Belousov sx_assert(&swdev_syscall_lock, SA_XLOCKED); 169492da00bbSMatthew Dillon 16958bc61209SDavid Schultz retries = 0; 169692da00bbSMatthew Dillon full_rescan: 1697b3fed13eSDavid Schultz mtx_lock(&swhash_mtx); 169892da00bbSMatthew Dillon for (i = 0; i <= swhash_mask; i++) { /* '<=' is correct here */ 169992da00bbSMatthew Dillon restart: 1700b3fed13eSDavid Schultz for (swap = swhash[i]; swap != NULL; swap = swap->swb_hnext) { 1701b3fed13eSDavid Schultz vm_object_t object = swap->swb_object; 1702b3fed13eSDavid Schultz vm_pindex_t pindex = swap->swb_index; 170392da00bbSMatthew Dillon for (j = 0; j < SWAP_META_PAGES; ++j) { 1704b3fed13eSDavid Schultz if (swp_pager_isondev(swap->swb_pages[j], sp)) { 1705b3fed13eSDavid Schultz /* avoid deadlock */ 170689f6b863SAttilio Rao if (!VM_OBJECT_TRYWLOCK(object)) { 170792da00bbSMatthew Dillon break; 1708b3fed13eSDavid Schultz } else { 1709b3fed13eSDavid Schultz mtx_unlock(&swhash_mtx); 1710b3fed13eSDavid Schultz swp_pager_force_pagein(object, 1711b3fed13eSDavid Schultz pindex + j); 171289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1713b3fed13eSDavid Schultz mtx_lock(&swhash_mtx); 171492da00bbSMatthew Dillon goto restart; 171592da00bbSMatthew Dillon } 1716b3fed13eSDavid Schultz } 1717b3fed13eSDavid Schultz } 1718b3fed13eSDavid Schultz } 171992da00bbSMatthew Dillon } 1720d536c58fSAlan Cox mtx_unlock(&swhash_mtx); 17218bc61209SDavid Schultz if (sp->sw_used) { 172292da00bbSMatthew Dillon /* 17238bc61209SDavid Schultz * Objects may be locked or paging to the device being 17248bc61209SDavid Schultz * removed, so we will miss their pages and need to 17258bc61209SDavid Schultz * make another pass. We have marked this device as 17268bc61209SDavid Schultz * SW_CLOSING, so the activity should finish soon. 172792da00bbSMatthew Dillon */ 17288bc61209SDavid Schultz retries++; 17298bc61209SDavid Schultz if (retries > 100) { 17308bc61209SDavid Schultz panic("swapoff: failed to locate %d swap blocks", 17318bc61209SDavid Schultz sp->sw_used); 17328bc61209SDavid Schultz } 17334d70511aSJohn Baldwin pause("swpoff", hz / 20); 173492da00bbSMatthew Dillon goto full_rescan; 173592da00bbSMatthew Dillon } 173692da00bbSMatthew Dillon } 173792da00bbSMatthew Dillon 17381c7c3c6aSMatthew Dillon /************************************************************************ 17391c7c3c6aSMatthew Dillon * SWAP META DATA * 17401c7c3c6aSMatthew Dillon ************************************************************************ 17411c7c3c6aSMatthew Dillon * 17421c7c3c6aSMatthew Dillon * These routines manipulate the swap metadata stored in the 1743cec9f109SDavid E. O'Brien * OBJT_SWAP object. 17441c7c3c6aSMatthew Dillon * 17454dcc5c2dSMatthew Dillon * Swap metadata is implemented with a global hash and not directly 17464dcc5c2dSMatthew Dillon * linked into the object. Instead the object simply contains 17474dcc5c2dSMatthew Dillon * appropriate tracking counters. 17481c7c3c6aSMatthew Dillon */ 17491c7c3c6aSMatthew Dillon 17501c7c3c6aSMatthew Dillon /* 17511c7c3c6aSMatthew Dillon * SWP_PAGER_META_BUILD() - add swap block to swap meta data for object 17521c7c3c6aSMatthew Dillon * 17531c7c3c6aSMatthew Dillon * We first convert the object to a swap object if it is a default 17541c7c3c6aSMatthew Dillon * object. 17551c7c3c6aSMatthew Dillon * 17561c7c3c6aSMatthew Dillon * The specified swapblk is added to the object's swap metadata. If 17571c7c3c6aSMatthew Dillon * the swapblk is not valid, it is freed instead. Any previously 17581c7c3c6aSMatthew Dillon * assigned swapblk is freed. 17591c7c3c6aSMatthew Dillon */ 17601c7c3c6aSMatthew Dillon static void 17612f249180SPoul-Henning Kamp swp_pager_meta_build(vm_object_t object, vm_pindex_t pindex, daddr_t swapblk) 17622f249180SPoul-Henning Kamp { 17633ff863f1SDag-Erling Smørgrav static volatile int exhausted; 17641c7c3c6aSMatthew Dillon struct swblock *swap; 17651c7c3c6aSMatthew Dillon struct swblock **pswap; 176623f09d50SIan Dowse int idx; 17671c7c3c6aSMatthew Dillon 176889f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 17691c7c3c6aSMatthew Dillon /* 17701c7c3c6aSMatthew Dillon * Convert default object to swap object if necessary 17711c7c3c6aSMatthew Dillon */ 17721c7c3c6aSMatthew Dillon if (object->type != OBJT_SWAP) { 17731c7c3c6aSMatthew Dillon object->type = OBJT_SWAP; 17741c7c3c6aSMatthew Dillon object->un_pager.swp.swp_bcount = 0; 1775eb4d6a1bSKonstantin Belousov KASSERT(object->handle == NULL, ("default pager with handle")); 1776bd228075SAlan Cox } 17771c7c3c6aSMatthew Dillon 17781c7c3c6aSMatthew Dillon /* 17791c7c3c6aSMatthew Dillon * Locate hash entry. If not found create, but if we aren't adding 17804dcc5c2dSMatthew Dillon * anything just return. If we run out of space in the map we wait 17814dcc5c2dSMatthew Dillon * and, since the hash table may have changed, retry. 17821c7c3c6aSMatthew Dillon */ 17834dcc5c2dSMatthew Dillon retry: 17847827d9b0SAlan Cox mtx_lock(&swhash_mtx); 178523f09d50SIan Dowse pswap = swp_pager_hash(object, pindex); 17861c7c3c6aSMatthew Dillon 17871c7c3c6aSMatthew Dillon if ((swap = *pswap) == NULL) { 17881c7c3c6aSMatthew Dillon int i; 17891c7c3c6aSMatthew Dillon 17901c7c3c6aSMatthew Dillon if (swapblk == SWAPBLK_NONE) 17917827d9b0SAlan Cox goto done; 17921c7c3c6aSMatthew Dillon 17935a3c920fSKonstantin Belousov swap = *pswap = uma_zalloc(swap_zone, M_NOWAIT | 17945a3c920fSKonstantin Belousov (curproc == pageproc ? M_USE_RESERVE : 0)); 17954dcc5c2dSMatthew Dillon if (swap == NULL) { 17967827d9b0SAlan Cox mtx_unlock(&swhash_mtx); 179789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 17982025d69bSKonstantin Belousov if (uma_zone_exhausted(swap_zone)) { 1799dc1b35b5SDag-Erling Smørgrav if (atomic_cmpset_int(&exhausted, 0, 1)) 18003ff863f1SDag-Erling Smørgrav printf("swap zone exhausted, " 18013ff863f1SDag-Erling Smørgrav "increase kern.maxswzone\n"); 18022025d69bSKonstantin Belousov vm_pageout_oom(VM_OOM_SWAPZ); 18032025d69bSKonstantin Belousov pause("swzonex", 10); 18042025d69bSKonstantin Belousov } else 18054dcc5c2dSMatthew Dillon VM_WAIT; 180689f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 18074dcc5c2dSMatthew Dillon goto retry; 18084dcc5c2dSMatthew Dillon } 1809670d17b5SJeff Roberson 1810dc1b35b5SDag-Erling Smørgrav if (atomic_cmpset_int(&exhausted, 1, 0)) 18113ff863f1SDag-Erling Smørgrav printf("swap zone ok\n"); 18123ff863f1SDag-Erling Smørgrav 18131c7c3c6aSMatthew Dillon swap->swb_hnext = NULL; 18141c7c3c6aSMatthew Dillon swap->swb_object = object; 181523f09d50SIan Dowse swap->swb_index = pindex & ~(vm_pindex_t)SWAP_META_MASK; 18161c7c3c6aSMatthew Dillon swap->swb_count = 0; 18171c7c3c6aSMatthew Dillon 18181c7c3c6aSMatthew Dillon ++object->un_pager.swp.swp_bcount; 18191c7c3c6aSMatthew Dillon 18201c7c3c6aSMatthew Dillon for (i = 0; i < SWAP_META_PAGES; ++i) 18211c7c3c6aSMatthew Dillon swap->swb_pages[i] = SWAPBLK_NONE; 18221c7c3c6aSMatthew Dillon } 18231c7c3c6aSMatthew Dillon 18241c7c3c6aSMatthew Dillon /* 18251c7c3c6aSMatthew Dillon * Delete prior contents of metadata 18261c7c3c6aSMatthew Dillon */ 182723f09d50SIan Dowse idx = pindex & SWAP_META_MASK; 18281c7c3c6aSMatthew Dillon 182923f09d50SIan Dowse if (swap->swb_pages[idx] != SWAPBLK_NONE) { 183023f09d50SIan Dowse swp_pager_freeswapspace(swap->swb_pages[idx], 1); 18311c7c3c6aSMatthew Dillon --swap->swb_count; 18321c7c3c6aSMatthew Dillon } 18331c7c3c6aSMatthew Dillon 18341c7c3c6aSMatthew Dillon /* 18351c7c3c6aSMatthew Dillon * Enter block into metadata 18361c7c3c6aSMatthew Dillon */ 183723f09d50SIan Dowse swap->swb_pages[idx] = swapblk; 18384dcc5c2dSMatthew Dillon if (swapblk != SWAPBLK_NONE) 18391c7c3c6aSMatthew Dillon ++swap->swb_count; 18407827d9b0SAlan Cox done: 18417827d9b0SAlan Cox mtx_unlock(&swhash_mtx); 18421c7c3c6aSMatthew Dillon } 18431c7c3c6aSMatthew Dillon 18441c7c3c6aSMatthew Dillon /* 18451c7c3c6aSMatthew Dillon * SWP_PAGER_META_FREE() - free a range of blocks in the object's swap metadata 18461c7c3c6aSMatthew Dillon * 18471c7c3c6aSMatthew Dillon * The requested range of blocks is freed, with any associated swap 18481c7c3c6aSMatthew Dillon * returned to the swap bitmap. 18491c7c3c6aSMatthew Dillon * 18501c7c3c6aSMatthew Dillon * This routine will free swap metadata structures as they are cleaned 18511c7c3c6aSMatthew Dillon * out. This routine does *NOT* operate on swap metadata associated 18521c7c3c6aSMatthew Dillon * with resident pages. 18531c7c3c6aSMatthew Dillon */ 18541c7c3c6aSMatthew Dillon static void 18554dcc5c2dSMatthew Dillon swp_pager_meta_free(vm_object_t object, vm_pindex_t index, daddr_t count) 18561c7c3c6aSMatthew Dillon { 18572928cef7SAlan Cox 1858c25673ffSAttilio Rao VM_OBJECT_ASSERT_LOCKED(object); 18591c7c3c6aSMatthew Dillon if (object->type != OBJT_SWAP) 18601c7c3c6aSMatthew Dillon return; 18611c7c3c6aSMatthew Dillon 18621c7c3c6aSMatthew Dillon while (count > 0) { 18631c7c3c6aSMatthew Dillon struct swblock **pswap; 18641c7c3c6aSMatthew Dillon struct swblock *swap; 18651c7c3c6aSMatthew Dillon 18667827d9b0SAlan Cox mtx_lock(&swhash_mtx); 18671c7c3c6aSMatthew Dillon pswap = swp_pager_hash(object, index); 18681c7c3c6aSMatthew Dillon 18691c7c3c6aSMatthew Dillon if ((swap = *pswap) != NULL) { 18701c7c3c6aSMatthew Dillon daddr_t v = swap->swb_pages[index & SWAP_META_MASK]; 18711c7c3c6aSMatthew Dillon 18721c7c3c6aSMatthew Dillon if (v != SWAPBLK_NONE) { 18731c7c3c6aSMatthew Dillon swp_pager_freeswapspace(v, 1); 18741c7c3c6aSMatthew Dillon swap->swb_pages[index & SWAP_META_MASK] = 18751c7c3c6aSMatthew Dillon SWAPBLK_NONE; 18761c7c3c6aSMatthew Dillon if (--swap->swb_count == 0) { 18771c7c3c6aSMatthew Dillon *pswap = swap->swb_hnext; 1878670d17b5SJeff Roberson uma_zfree(swap_zone, swap); 18791c7c3c6aSMatthew Dillon --object->un_pager.swp.swp_bcount; 18801c7c3c6aSMatthew Dillon } 18811c7c3c6aSMatthew Dillon } 18821c7c3c6aSMatthew Dillon --count; 18831c7c3c6aSMatthew Dillon ++index; 18841c7c3c6aSMatthew Dillon } else { 18854dcc5c2dSMatthew Dillon int n = SWAP_META_PAGES - (index & SWAP_META_MASK); 18861c7c3c6aSMatthew Dillon count -= n; 18871c7c3c6aSMatthew Dillon index += n; 18881c7c3c6aSMatthew Dillon } 18897827d9b0SAlan Cox mtx_unlock(&swhash_mtx); 18901c7c3c6aSMatthew Dillon } 18911c7c3c6aSMatthew Dillon } 18921c7c3c6aSMatthew Dillon 18931c7c3c6aSMatthew Dillon /* 18941c7c3c6aSMatthew Dillon * SWP_PAGER_META_FREE_ALL() - destroy all swap metadata associated with object 18951c7c3c6aSMatthew Dillon * 18961c7c3c6aSMatthew Dillon * This routine locates and destroys all swap metadata associated with 18971c7c3c6aSMatthew Dillon * an object. 18981c7c3c6aSMatthew Dillon */ 18991c7c3c6aSMatthew Dillon static void 19001c7c3c6aSMatthew Dillon swp_pager_meta_free_all(vm_object_t object) 19011c7c3c6aSMatthew Dillon { 19021c7c3c6aSMatthew Dillon daddr_t index = 0; 19031c7c3c6aSMatthew Dillon 190489f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 19051c7c3c6aSMatthew Dillon if (object->type != OBJT_SWAP) 19061c7c3c6aSMatthew Dillon return; 19071c7c3c6aSMatthew Dillon 19081c7c3c6aSMatthew Dillon while (object->un_pager.swp.swp_bcount) { 19091c7c3c6aSMatthew Dillon struct swblock **pswap; 19101c7c3c6aSMatthew Dillon struct swblock *swap; 19111c7c3c6aSMatthew Dillon 19127827d9b0SAlan Cox mtx_lock(&swhash_mtx); 19131c7c3c6aSMatthew Dillon pswap = swp_pager_hash(object, index); 19141c7c3c6aSMatthew Dillon if ((swap = *pswap) != NULL) { 19151c7c3c6aSMatthew Dillon int i; 19161c7c3c6aSMatthew Dillon 19171c7c3c6aSMatthew Dillon for (i = 0; i < SWAP_META_PAGES; ++i) { 19181c7c3c6aSMatthew Dillon daddr_t v = swap->swb_pages[i]; 19191c7c3c6aSMatthew Dillon if (v != SWAPBLK_NONE) { 19201c7c3c6aSMatthew Dillon --swap->swb_count; 19214dcc5c2dSMatthew Dillon swp_pager_freeswapspace(v, 1); 19221c7c3c6aSMatthew Dillon } 19231c7c3c6aSMatthew Dillon } 19241c7c3c6aSMatthew Dillon if (swap->swb_count != 0) 19251c7c3c6aSMatthew Dillon panic("swap_pager_meta_free_all: swb_count != 0"); 19261c7c3c6aSMatthew Dillon *pswap = swap->swb_hnext; 1927670d17b5SJeff Roberson uma_zfree(swap_zone, swap); 19281c7c3c6aSMatthew Dillon --object->un_pager.swp.swp_bcount; 19291c7c3c6aSMatthew Dillon } 19307827d9b0SAlan Cox mtx_unlock(&swhash_mtx); 19311c7c3c6aSMatthew Dillon index += SWAP_META_PAGES; 19321c7c3c6aSMatthew Dillon } 19331c7c3c6aSMatthew Dillon } 19341c7c3c6aSMatthew Dillon 19351c7c3c6aSMatthew Dillon /* 19361c7c3c6aSMatthew Dillon * SWP_PAGER_METACTL() - misc control of swap and vm_page_t meta data. 19371c7c3c6aSMatthew Dillon * 19381c7c3c6aSMatthew Dillon * This routine is capable of looking up, popping, or freeing 19391c7c3c6aSMatthew Dillon * swapblk assignments in the swap meta data or in the vm_page_t. 19401c7c3c6aSMatthew Dillon * The routine typically returns the swapblk being looked-up, or popped, 19411c7c3c6aSMatthew Dillon * or SWAPBLK_NONE if the block was freed, or SWAPBLK_NONE if the block 19421c7c3c6aSMatthew Dillon * was invalid. This routine will automatically free any invalid 19431c7c3c6aSMatthew Dillon * meta-data swapblks. 19441c7c3c6aSMatthew Dillon * 19451c7c3c6aSMatthew Dillon * It is not possible to store invalid swapblks in the swap meta data 19461c7c3c6aSMatthew Dillon * (other then a literal 'SWAPBLK_NONE'), so we don't bother checking. 19471c7c3c6aSMatthew Dillon * 19481c7c3c6aSMatthew Dillon * When acting on a busy resident page and paging is in progress, we 19491c7c3c6aSMatthew Dillon * have to wait until paging is complete but otherwise can act on the 19501c7c3c6aSMatthew Dillon * busy page. 19511c7c3c6aSMatthew Dillon * 19524dcc5c2dSMatthew Dillon * SWM_FREE remove and free swap block from metadata 19531c7c3c6aSMatthew Dillon * SWM_POP remove from meta data but do not free.. pop it out 19541c7c3c6aSMatthew Dillon */ 19551c7c3c6aSMatthew Dillon static daddr_t 19562f249180SPoul-Henning Kamp swp_pager_meta_ctl(vm_object_t object, vm_pindex_t pindex, int flags) 19572f249180SPoul-Henning Kamp { 19584dcc5c2dSMatthew Dillon struct swblock **pswap; 19594dcc5c2dSMatthew Dillon struct swblock *swap; 19604dcc5c2dSMatthew Dillon daddr_t r1; 196123f09d50SIan Dowse int idx; 19624dcc5c2dSMatthew Dillon 1963c25673ffSAttilio Rao VM_OBJECT_ASSERT_LOCKED(object); 19641c7c3c6aSMatthew Dillon /* 19651c7c3c6aSMatthew Dillon * The meta data only exists of the object is OBJT_SWAP 19661c7c3c6aSMatthew Dillon * and even then might not be allocated yet. 19671c7c3c6aSMatthew Dillon */ 19684dcc5c2dSMatthew Dillon if (object->type != OBJT_SWAP) 19691c7c3c6aSMatthew Dillon return (SWAPBLK_NONE); 19701c7c3c6aSMatthew Dillon 19714dcc5c2dSMatthew Dillon r1 = SWAPBLK_NONE; 19727827d9b0SAlan Cox mtx_lock(&swhash_mtx); 197323f09d50SIan Dowse pswap = swp_pager_hash(object, pindex); 19741c7c3c6aSMatthew Dillon 19751c7c3c6aSMatthew Dillon if ((swap = *pswap) != NULL) { 197623f09d50SIan Dowse idx = pindex & SWAP_META_MASK; 197723f09d50SIan Dowse r1 = swap->swb_pages[idx]; 19781c7c3c6aSMatthew Dillon 19791c7c3c6aSMatthew Dillon if (r1 != SWAPBLK_NONE) { 19801c7c3c6aSMatthew Dillon if (flags & SWM_FREE) { 19814dcc5c2dSMatthew Dillon swp_pager_freeswapspace(r1, 1); 19821c7c3c6aSMatthew Dillon r1 = SWAPBLK_NONE; 19831c7c3c6aSMatthew Dillon } 19841c7c3c6aSMatthew Dillon if (flags & (SWM_FREE|SWM_POP)) { 198523f09d50SIan Dowse swap->swb_pages[idx] = SWAPBLK_NONE; 19861c7c3c6aSMatthew Dillon if (--swap->swb_count == 0) { 19871c7c3c6aSMatthew Dillon *pswap = swap->swb_hnext; 1988670d17b5SJeff Roberson uma_zfree(swap_zone, swap); 19891c7c3c6aSMatthew Dillon --object->un_pager.swp.swp_bcount; 19901c7c3c6aSMatthew Dillon } 19911c7c3c6aSMatthew Dillon } 19921c7c3c6aSMatthew Dillon } 19931c7c3c6aSMatthew Dillon } 19947827d9b0SAlan Cox mtx_unlock(&swhash_mtx); 19951c7c3c6aSMatthew Dillon return (r1); 19961c7c3c6aSMatthew Dillon } 19971c7c3c6aSMatthew Dillon 1998e9c0cc15SPoul-Henning Kamp /* 1999e9c0cc15SPoul-Henning Kamp * System call swapon(name) enables swapping on device name, 2000e9c0cc15SPoul-Henning Kamp * which must be in the swdevsw. Return EBUSY 2001e9c0cc15SPoul-Henning Kamp * if already swapping on this device. 2002e9c0cc15SPoul-Henning Kamp */ 2003e9c0cc15SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_ 2004e9c0cc15SPoul-Henning Kamp struct swapon_args { 2005e9c0cc15SPoul-Henning Kamp char *name; 2006e9c0cc15SPoul-Henning Kamp }; 2007e9c0cc15SPoul-Henning Kamp #endif 2008e9c0cc15SPoul-Henning Kamp 2009e9c0cc15SPoul-Henning Kamp /* 2010e9c0cc15SPoul-Henning Kamp * MPSAFE 2011e9c0cc15SPoul-Henning Kamp */ 2012e9c0cc15SPoul-Henning Kamp /* ARGSUSED */ 2013e9c0cc15SPoul-Henning Kamp int 20148451d0ddSKip Macy sys_swapon(struct thread *td, struct swapon_args *uap) 2015e9c0cc15SPoul-Henning Kamp { 2016e9c0cc15SPoul-Henning Kamp struct vattr attr; 2017e9c0cc15SPoul-Henning Kamp struct vnode *vp; 2018e9c0cc15SPoul-Henning Kamp struct nameidata nd; 2019e9c0cc15SPoul-Henning Kamp int error; 2020e9c0cc15SPoul-Henning Kamp 2021acd3428bSRobert Watson error = priv_check(td, PRIV_SWAPON); 2022e9c0cc15SPoul-Henning Kamp if (error) 2023acd3428bSRobert Watson return (error); 2024e9c0cc15SPoul-Henning Kamp 202504533e1eSKonstantin Belousov sx_xlock(&swdev_syscall_lock); 2026e9c0cc15SPoul-Henning Kamp 2027e9c0cc15SPoul-Henning Kamp /* 2028e9c0cc15SPoul-Henning Kamp * Swap metadata may not fit in the KVM if we have physical 2029e9c0cc15SPoul-Henning Kamp * memory of >1GB. 2030e9c0cc15SPoul-Henning Kamp */ 2031e9c0cc15SPoul-Henning Kamp if (swap_zone == NULL) { 2032e9c0cc15SPoul-Henning Kamp error = ENOMEM; 2033e9c0cc15SPoul-Henning Kamp goto done; 2034e9c0cc15SPoul-Henning Kamp } 2035e9c0cc15SPoul-Henning Kamp 2036d9135e72SRobert Watson NDINIT(&nd, LOOKUP, ISOPEN | FOLLOW | AUDITVNODE1, UIO_USERSPACE, 2037d9135e72SRobert Watson uap->name, td); 2038e9c0cc15SPoul-Henning Kamp error = namei(&nd); 2039e9c0cc15SPoul-Henning Kamp if (error) 2040e9c0cc15SPoul-Henning Kamp goto done; 2041e9c0cc15SPoul-Henning Kamp 2042e9c0cc15SPoul-Henning Kamp NDFREE(&nd, NDF_ONLY_PNBUF); 2043e9c0cc15SPoul-Henning Kamp vp = nd.ni_vp; 2044e9c0cc15SPoul-Henning Kamp 204520da9c2eSPoul-Henning Kamp if (vn_isdisk(vp, &error)) { 204688ad2d7bSKonstantin Belousov error = swapongeom(vp); 204720da9c2eSPoul-Henning Kamp } else if (vp->v_type == VREG && 2048e9c0cc15SPoul-Henning Kamp (vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 && 20490359a12eSAttilio Rao (error = VOP_GETATTR(vp, &attr, td->td_ucred)) == 0) { 2050e9c0cc15SPoul-Henning Kamp /* 2051e9c0cc15SPoul-Henning Kamp * Allow direct swapping to NFS regular files in the same 2052e9c0cc15SPoul-Henning Kamp * way that nfs_mountroot() sets up diskless swapping. 2053e9c0cc15SPoul-Henning Kamp */ 205459efee01SPoul-Henning Kamp error = swaponvp(td, vp, attr.va_size / DEV_BSIZE); 2055e9c0cc15SPoul-Henning Kamp } 2056e9c0cc15SPoul-Henning Kamp 2057e9c0cc15SPoul-Henning Kamp if (error) 2058e9c0cc15SPoul-Henning Kamp vrele(vp); 2059e9c0cc15SPoul-Henning Kamp done: 206004533e1eSKonstantin Belousov sx_xunlock(&swdev_syscall_lock); 2061e9c0cc15SPoul-Henning Kamp return (error); 2062e9c0cc15SPoul-Henning Kamp } 2063e9c0cc15SPoul-Henning Kamp 20643ff863f1SDag-Erling Smørgrav /* 20653ff863f1SDag-Erling Smørgrav * Check that the total amount of swap currently configured does not 20663ff863f1SDag-Erling Smørgrav * exceed half the theoretical maximum. If it does, print a warning 20673ff863f1SDag-Erling Smørgrav * message and return -1; otherwise, return 0. 20683ff863f1SDag-Erling Smørgrav */ 20693ff863f1SDag-Erling Smørgrav static int 20703ff863f1SDag-Erling Smørgrav swapon_check_swzone(unsigned long npages) 20713ff863f1SDag-Erling Smørgrav { 20723ff863f1SDag-Erling Smørgrav unsigned long maxpages; 20733ff863f1SDag-Erling Smørgrav 20743ff863f1SDag-Erling Smørgrav /* absolute maximum we can handle assuming 100% efficiency */ 20753ff863f1SDag-Erling Smørgrav maxpages = uma_zone_get_max(swap_zone) * SWAP_META_PAGES; 20763ff863f1SDag-Erling Smørgrav 20773ff863f1SDag-Erling Smørgrav /* recommend using no more than half that amount */ 20783ff863f1SDag-Erling Smørgrav if (npages > maxpages / 2) { 20793ff863f1SDag-Erling Smørgrav printf("warning: total configured swap (%lu pages) " 20803ff863f1SDag-Erling Smørgrav "exceeds maximum recommended amount (%lu pages).\n", 20819462305cSSergey Kandaurov npages, maxpages / 2); 20823ff863f1SDag-Erling Smørgrav printf("warning: increase kern.maxswzone " 20833ff863f1SDag-Erling Smørgrav "or reduce amount of swap.\n"); 20843ff863f1SDag-Erling Smørgrav return (-1); 20853ff863f1SDag-Erling Smørgrav } 20863ff863f1SDag-Erling Smørgrav return (0); 20873ff863f1SDag-Erling Smørgrav } 20883ff863f1SDag-Erling Smørgrav 208959efee01SPoul-Henning Kamp static void 20902cc718a1SKonstantin Belousov swaponsomething(struct vnode *vp, void *id, u_long nblks, 20912cc718a1SKonstantin Belousov sw_strategy_t *strategy, sw_close_t *close, dev_t dev, int flags) 2092e9c0cc15SPoul-Henning Kamp { 20932d9974c1SAlan Cox struct swdevt *sp, *tsp; 2094e9c0cc15SPoul-Henning Kamp swblk_t dvbase; 20958f60c087SPoul-Henning Kamp u_long mblocks; 2096e9c0cc15SPoul-Henning Kamp 2097e9c0cc15SPoul-Henning Kamp /* 2098e9c0cc15SPoul-Henning Kamp * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks. 2099e9c0cc15SPoul-Henning Kamp * First chop nblks off to page-align it, then convert. 2100e9c0cc15SPoul-Henning Kamp * 2101e9c0cc15SPoul-Henning Kamp * sw->sw_nblks is in page-sized chunks now too. 2102e9c0cc15SPoul-Henning Kamp */ 2103e9c0cc15SPoul-Henning Kamp nblks &= ~(ctodb(1) - 1); 2104e9c0cc15SPoul-Henning Kamp nblks = dbtoc(nblks); 2105e9c0cc15SPoul-Henning Kamp 21066e903bd0SKonstantin Belousov /* 21076e903bd0SKonstantin Belousov * If we go beyond this, we get overflows in the radix 21086e903bd0SKonstantin Belousov * tree bitmap code. 21096e903bd0SKonstantin Belousov */ 21106e903bd0SKonstantin Belousov mblocks = 0x40000000 / BLIST_META_RADIX; 21116e903bd0SKonstantin Belousov if (nblks > mblocks) { 21126e903bd0SKonstantin Belousov printf( 21136e903bd0SKonstantin Belousov "WARNING: reducing swap size to maximum of %luMB per unit\n", 21146e903bd0SKonstantin Belousov mblocks / 1024 / 1024 * PAGE_SIZE); 21156e903bd0SKonstantin Belousov nblks = mblocks; 21166e903bd0SKonstantin Belousov } 21176e903bd0SKonstantin Belousov 21188f60c087SPoul-Henning Kamp sp = malloc(sizeof *sp, M_VMPGDATA, M_WAITOK | M_ZERO); 2119dee34ca4SPoul-Henning Kamp sp->sw_vp = vp; 2120dee34ca4SPoul-Henning Kamp sp->sw_id = id; 2121f3732fd1SPoul-Henning Kamp sp->sw_dev = dev; 21228d677ef9SPoul-Henning Kamp sp->sw_flags = 0; 2123e9c0cc15SPoul-Henning Kamp sp->sw_nblks = nblks; 2124e9c0cc15SPoul-Henning Kamp sp->sw_used = 0; 212559efee01SPoul-Henning Kamp sp->sw_strategy = strategy; 2126dee34ca4SPoul-Henning Kamp sp->sw_close = close; 21272cc718a1SKonstantin Belousov sp->sw_flags = flags; 2128e9c0cc15SPoul-Henning Kamp 2129c8c7ad92SKip Macy sp->sw_blist = blist_create(nblks, M_WAITOK); 2130e9c0cc15SPoul-Henning Kamp /* 2131ef3c5abdSPoul-Henning Kamp * Do not free the first two block in order to avoid overwriting 21328f60c087SPoul-Henning Kamp * any bsd label at the front of the partition 2133e9c0cc15SPoul-Henning Kamp */ 2134ef3c5abdSPoul-Henning Kamp blist_free(sp->sw_blist, 2, nblks - 2); 2135e9c0cc15SPoul-Henning Kamp 21362d9974c1SAlan Cox dvbase = 0; 213720da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 21382d9974c1SAlan Cox TAILQ_FOREACH(tsp, &swtailq, sw_list) { 21392d9974c1SAlan Cox if (tsp->sw_end >= dvbase) { 21402d9974c1SAlan Cox /* 21412d9974c1SAlan Cox * We put one uncovered page between the devices 21422d9974c1SAlan Cox * in order to definitively prevent any cross-device 21432d9974c1SAlan Cox * I/O requests 21442d9974c1SAlan Cox */ 21452d9974c1SAlan Cox dvbase = tsp->sw_end + 1; 21462d9974c1SAlan Cox } 21472d9974c1SAlan Cox } 21482d9974c1SAlan Cox sp->sw_first = dvbase; 21492d9974c1SAlan Cox sp->sw_end = dvbase + nblks; 21508f60c087SPoul-Henning Kamp TAILQ_INSERT_TAIL(&swtailq, sp, sw_list); 21518f60c087SPoul-Henning Kamp nswapdev++; 21528f60c087SPoul-Henning Kamp swap_pager_avail += nblks; 21533364c323SKonstantin Belousov swap_total += (vm_ooffset_t)nblks * PAGE_SIZE; 21543ff863f1SDag-Erling Smørgrav swapon_check_swzone(swap_total / PAGE_SIZE); 2155d05bc129SAlan Cox swp_sizecheck(); 2156d05bc129SAlan Cox mtx_unlock(&sw_dev_mtx); 215759efee01SPoul-Henning Kamp } 2158e9c0cc15SPoul-Henning Kamp 2159e9c0cc15SPoul-Henning Kamp /* 2160e9c0cc15SPoul-Henning Kamp * SYSCALL: swapoff(devname) 2161e9c0cc15SPoul-Henning Kamp * 2162e9c0cc15SPoul-Henning Kamp * Disable swapping on the given device. 2163dee34ca4SPoul-Henning Kamp * 2164dee34ca4SPoul-Henning Kamp * XXX: Badly designed system call: it should use a device index 2165dee34ca4SPoul-Henning Kamp * rather than filename as specification. We keep sw_vp around 2166dee34ca4SPoul-Henning Kamp * only to make this work. 2167e9c0cc15SPoul-Henning Kamp */ 2168e9c0cc15SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_ 2169e9c0cc15SPoul-Henning Kamp struct swapoff_args { 2170e9c0cc15SPoul-Henning Kamp char *name; 2171e9c0cc15SPoul-Henning Kamp }; 2172e9c0cc15SPoul-Henning Kamp #endif 2173e9c0cc15SPoul-Henning Kamp 2174e9c0cc15SPoul-Henning Kamp /* 2175e9c0cc15SPoul-Henning Kamp * MPSAFE 2176e9c0cc15SPoul-Henning Kamp */ 2177e9c0cc15SPoul-Henning Kamp /* ARGSUSED */ 2178e9c0cc15SPoul-Henning Kamp int 21798451d0ddSKip Macy sys_swapoff(struct thread *td, struct swapoff_args *uap) 2180e9c0cc15SPoul-Henning Kamp { 2181e9c0cc15SPoul-Henning Kamp struct vnode *vp; 2182e9c0cc15SPoul-Henning Kamp struct nameidata nd; 2183e9c0cc15SPoul-Henning Kamp struct swdevt *sp; 21848f60c087SPoul-Henning Kamp int error; 2185e9c0cc15SPoul-Henning Kamp 2186acd3428bSRobert Watson error = priv_check(td, PRIV_SWAPOFF); 2187e9c0cc15SPoul-Henning Kamp if (error) 21880909f38aSPawel Jakub Dawidek return (error); 2189e9c0cc15SPoul-Henning Kamp 219004533e1eSKonstantin Belousov sx_xlock(&swdev_syscall_lock); 2191e9c0cc15SPoul-Henning Kamp 2192d9135e72SRobert Watson NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, UIO_USERSPACE, uap->name, 2193d9135e72SRobert Watson td); 2194e9c0cc15SPoul-Henning Kamp error = namei(&nd); 2195e9c0cc15SPoul-Henning Kamp if (error) 2196e9c0cc15SPoul-Henning Kamp goto done; 2197e9c0cc15SPoul-Henning Kamp NDFREE(&nd, NDF_ONLY_PNBUF); 2198e9c0cc15SPoul-Henning Kamp vp = nd.ni_vp; 2199e9c0cc15SPoul-Henning Kamp 220020da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 22018f60c087SPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 2202dee34ca4SPoul-Henning Kamp if (sp->sw_vp == vp) 22030909f38aSPawel Jakub Dawidek break; 2204e9c0cc15SPoul-Henning Kamp } 220520da9c2eSPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 22060909f38aSPawel Jakub Dawidek if (sp == NULL) { 2207e9c0cc15SPoul-Henning Kamp error = EINVAL; 2208e9c0cc15SPoul-Henning Kamp goto done; 22090909f38aSPawel Jakub Dawidek } 221035918c55SChristian S.J. Peron error = swapoff_one(sp, td->td_ucred); 22110909f38aSPawel Jakub Dawidek done: 221204533e1eSKonstantin Belousov sx_xunlock(&swdev_syscall_lock); 22130909f38aSPawel Jakub Dawidek return (error); 22140909f38aSPawel Jakub Dawidek } 22150909f38aSPawel Jakub Dawidek 22160909f38aSPawel Jakub Dawidek static int 221735918c55SChristian S.J. Peron swapoff_one(struct swdevt *sp, struct ucred *cred) 22180909f38aSPawel Jakub Dawidek { 22190909f38aSPawel Jakub Dawidek u_long nblks, dvbase; 2220e9c0cc15SPoul-Henning Kamp #ifdef MAC 22210909f38aSPawel Jakub Dawidek int error; 2222e9c0cc15SPoul-Henning Kamp #endif 2223e9c0cc15SPoul-Henning Kamp 222404533e1eSKonstantin Belousov sx_assert(&swdev_syscall_lock, SA_XLOCKED); 22250909f38aSPawel Jakub Dawidek #ifdef MAC 2226cb05b60aSAttilio Rao (void) vn_lock(sp->sw_vp, LK_EXCLUSIVE | LK_RETRY); 222735918c55SChristian S.J. Peron error = mac_system_check_swapoff(cred, sp->sw_vp); 222822db15c0SAttilio Rao (void) VOP_UNLOCK(sp->sw_vp, 0); 22290909f38aSPawel Jakub Dawidek if (error != 0) 22300909f38aSPawel Jakub Dawidek return (error); 22310909f38aSPawel Jakub Dawidek #endif 2232e9c0cc15SPoul-Henning Kamp nblks = sp->sw_nblks; 2233e9c0cc15SPoul-Henning Kamp 2234e9c0cc15SPoul-Henning Kamp /* 2235e9c0cc15SPoul-Henning Kamp * We can turn off this swap device safely only if the 2236e9c0cc15SPoul-Henning Kamp * available virtual memory in the system will fit the amount 2237e9c0cc15SPoul-Henning Kamp * of data we will have to page back in, plus an epsilon so 2238e9c0cc15SPoul-Henning Kamp * the system doesn't become critically low on swap space. 2239e9c0cc15SPoul-Henning Kamp */ 224044f1c916SBryan Drewery if (vm_cnt.v_free_count + vm_cnt.v_cache_count + swap_pager_avail < 22412feb50bfSAttilio Rao nblks + nswap_lowat) { 22420909f38aSPawel Jakub Dawidek return (ENOMEM); 2243e9c0cc15SPoul-Henning Kamp } 2244e9c0cc15SPoul-Henning Kamp 2245e9c0cc15SPoul-Henning Kamp /* 2246e9c0cc15SPoul-Henning Kamp * Prevent further allocations on this device. 2247e9c0cc15SPoul-Henning Kamp */ 22482928cef7SAlan Cox mtx_lock(&sw_dev_mtx); 2249e9c0cc15SPoul-Henning Kamp sp->sw_flags |= SW_CLOSING; 22508f60c087SPoul-Henning Kamp for (dvbase = 0; dvbase < sp->sw_end; dvbase += dmmax) { 22518f60c087SPoul-Henning Kamp swap_pager_avail -= blist_fill(sp->sw_blist, 22528f60c087SPoul-Henning Kamp dvbase, dmmax); 2253e9c0cc15SPoul-Henning Kamp } 22543364c323SKonstantin Belousov swap_total -= (vm_ooffset_t)nblks * PAGE_SIZE; 22552928cef7SAlan Cox mtx_unlock(&sw_dev_mtx); 2256e9c0cc15SPoul-Henning Kamp 2257e9c0cc15SPoul-Henning Kamp /* 2258e9c0cc15SPoul-Henning Kamp * Page in the contents of the device and close it. 2259e9c0cc15SPoul-Henning Kamp */ 2260b3fed13eSDavid Schultz swap_pager_swapoff(sp); 2261e9c0cc15SPoul-Henning Kamp 226235918c55SChristian S.J. Peron sp->sw_close(curthread, sp); 226320da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 22649e3e3fe5SWarner Losh sp->sw_id = NULL; 22658f60c087SPoul-Henning Kamp TAILQ_REMOVE(&swtailq, sp, sw_list); 22660676a140SAlan Cox nswapdev--; 22677dea2c2eSAlan Cox if (nswapdev == 0) { 22687dea2c2eSAlan Cox swap_pager_full = 2; 22697dea2c2eSAlan Cox swap_pager_almost_full = 1; 22707dea2c2eSAlan Cox } 22718f60c087SPoul-Henning Kamp if (swdevhd == sp) 22728f60c087SPoul-Henning Kamp swdevhd = NULL; 2273d05bc129SAlan Cox mtx_unlock(&sw_dev_mtx); 22748f60c087SPoul-Henning Kamp blist_destroy(sp->sw_blist); 22758f60c087SPoul-Henning Kamp free(sp, M_VMPGDATA); 22760909f38aSPawel Jakub Dawidek return (0); 22770909f38aSPawel Jakub Dawidek } 2278e9c0cc15SPoul-Henning Kamp 22790909f38aSPawel Jakub Dawidek void 22800909f38aSPawel Jakub Dawidek swapoff_all(void) 22810909f38aSPawel Jakub Dawidek { 22820909f38aSPawel Jakub Dawidek struct swdevt *sp, *spt; 22830909f38aSPawel Jakub Dawidek const char *devname; 22840909f38aSPawel Jakub Dawidek int error; 22850909f38aSPawel Jakub Dawidek 228604533e1eSKonstantin Belousov sx_xlock(&swdev_syscall_lock); 22870909f38aSPawel Jakub Dawidek 22880909f38aSPawel Jakub Dawidek mtx_lock(&sw_dev_mtx); 22890909f38aSPawel Jakub Dawidek TAILQ_FOREACH_SAFE(sp, &swtailq, sw_list, spt) { 22900909f38aSPawel Jakub Dawidek mtx_unlock(&sw_dev_mtx); 22910909f38aSPawel Jakub Dawidek if (vn_isdisk(sp->sw_vp, NULL)) 22927870adb6SEd Schouten devname = devtoname(sp->sw_vp->v_rdev); 22930909f38aSPawel Jakub Dawidek else 22940909f38aSPawel Jakub Dawidek devname = "[file]"; 229535918c55SChristian S.J. Peron error = swapoff_one(sp, thread0.td_ucred); 22960909f38aSPawel Jakub Dawidek if (error != 0) { 22970909f38aSPawel Jakub Dawidek printf("Cannot remove swap device %s (error=%d), " 22980909f38aSPawel Jakub Dawidek "skipping.\n", devname, error); 22990909f38aSPawel Jakub Dawidek } else if (bootverbose) { 23000909f38aSPawel Jakub Dawidek printf("Swap device %s removed.\n", devname); 23010909f38aSPawel Jakub Dawidek } 23020909f38aSPawel Jakub Dawidek mtx_lock(&sw_dev_mtx); 23030909f38aSPawel Jakub Dawidek } 23040909f38aSPawel Jakub Dawidek mtx_unlock(&sw_dev_mtx); 23050909f38aSPawel Jakub Dawidek 230604533e1eSKonstantin Belousov sx_xunlock(&swdev_syscall_lock); 2307e9c0cc15SPoul-Henning Kamp } 2308e9c0cc15SPoul-Henning Kamp 2309567104a1SPoul-Henning Kamp void 2310567104a1SPoul-Henning Kamp swap_pager_status(int *total, int *used) 2311567104a1SPoul-Henning Kamp { 2312567104a1SPoul-Henning Kamp struct swdevt *sp; 2313567104a1SPoul-Henning Kamp 2314567104a1SPoul-Henning Kamp *total = 0; 2315567104a1SPoul-Henning Kamp *used = 0; 231620da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 23178f60c087SPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 2318567104a1SPoul-Henning Kamp *total += sp->sw_nblks; 2319567104a1SPoul-Henning Kamp *used += sp->sw_used; 2320567104a1SPoul-Henning Kamp } 232120da9c2eSPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 2322567104a1SPoul-Henning Kamp } 2323567104a1SPoul-Henning Kamp 2324dda4f960SKonstantin Belousov int 2325dda4f960SKonstantin Belousov swap_dev_info(int name, struct xswdev *xs, char *devname, size_t len) 2326dda4f960SKonstantin Belousov { 2327dda4f960SKonstantin Belousov struct swdevt *sp; 23287870adb6SEd Schouten const char *tmp_devname; 2329dda4f960SKonstantin Belousov int error, n; 2330dda4f960SKonstantin Belousov 2331dda4f960SKonstantin Belousov n = 0; 2332dda4f960SKonstantin Belousov error = ENOENT; 2333dda4f960SKonstantin Belousov mtx_lock(&sw_dev_mtx); 2334dda4f960SKonstantin Belousov TAILQ_FOREACH(sp, &swtailq, sw_list) { 2335dda4f960SKonstantin Belousov if (n != name) { 2336dda4f960SKonstantin Belousov n++; 2337dda4f960SKonstantin Belousov continue; 2338dda4f960SKonstantin Belousov } 2339dda4f960SKonstantin Belousov xs->xsw_version = XSWDEV_VERSION; 2340dda4f960SKonstantin Belousov xs->xsw_dev = sp->sw_dev; 2341dda4f960SKonstantin Belousov xs->xsw_flags = sp->sw_flags; 2342dda4f960SKonstantin Belousov xs->xsw_nblks = sp->sw_nblks; 2343dda4f960SKonstantin Belousov xs->xsw_used = sp->sw_used; 2344dda4f960SKonstantin Belousov if (devname != NULL) { 2345dda4f960SKonstantin Belousov if (vn_isdisk(sp->sw_vp, NULL)) 23467870adb6SEd Schouten tmp_devname = devtoname(sp->sw_vp->v_rdev); 2347dda4f960SKonstantin Belousov else 2348dda4f960SKonstantin Belousov tmp_devname = "[file]"; 2349dda4f960SKonstantin Belousov strncpy(devname, tmp_devname, len); 2350dda4f960SKonstantin Belousov } 2351dda4f960SKonstantin Belousov error = 0; 2352dda4f960SKonstantin Belousov break; 2353dda4f960SKonstantin Belousov } 2354dda4f960SKonstantin Belousov mtx_unlock(&sw_dev_mtx); 2355dda4f960SKonstantin Belousov return (error); 2356dda4f960SKonstantin Belousov } 2357dda4f960SKonstantin Belousov 2358e9c0cc15SPoul-Henning Kamp static int 2359e9c0cc15SPoul-Henning Kamp sysctl_vm_swap_info(SYSCTL_HANDLER_ARGS) 2360e9c0cc15SPoul-Henning Kamp { 2361e9c0cc15SPoul-Henning Kamp struct xswdev xs; 2362dda4f960SKonstantin Belousov int error; 2363e9c0cc15SPoul-Henning Kamp 2364e9c0cc15SPoul-Henning Kamp if (arg2 != 1) /* name length */ 2365e9c0cc15SPoul-Henning Kamp return (EINVAL); 2366dda4f960SKonstantin Belousov error = swap_dev_info(*(int *)arg1, &xs, NULL, 0); 2367dda4f960SKonstantin Belousov if (error != 0) 2368dda4f960SKonstantin Belousov return (error); 2369e9c0cc15SPoul-Henning Kamp error = SYSCTL_OUT(req, &xs, sizeof(xs)); 2370e9c0cc15SPoul-Henning Kamp return (error); 2371e9c0cc15SPoul-Henning Kamp } 2372e9c0cc15SPoul-Henning Kamp 23738f60c087SPoul-Henning Kamp SYSCTL_INT(_vm, OID_AUTO, nswapdev, CTLFLAG_RD, &nswapdev, 0, 2374e9c0cc15SPoul-Henning Kamp "Number of swap devices"); 23754c36e917SKonstantin Belousov SYSCTL_NODE(_vm, OID_AUTO, swap_info, CTLFLAG_RD | CTLFLAG_MPSAFE, 23764c36e917SKonstantin Belousov sysctl_vm_swap_info, 2377e9c0cc15SPoul-Henning Kamp "Swap statistics by device"); 2378ec38b344SPoul-Henning Kamp 2379ec38b344SPoul-Henning Kamp /* 23808eb5a1cdSKonstantin Belousov * vmspace_swap_count() - count the approximate swap usage in pages for a 2381ec38b344SPoul-Henning Kamp * vmspace. 2382ec38b344SPoul-Henning Kamp * 2383ec38b344SPoul-Henning Kamp * The map must be locked. 2384ec38b344SPoul-Henning Kamp * 23858eb5a1cdSKonstantin Belousov * Swap usage is determined by taking the proportional swap used by 2386ec38b344SPoul-Henning Kamp * VM objects backing the VM map. To make up for fractional losses, 2387ec38b344SPoul-Henning Kamp * if the VM object has any swap use at all the associated map entries 2388ec38b344SPoul-Henning Kamp * count for at least 1 swap page. 2389ec38b344SPoul-Henning Kamp */ 23902860553aSRebecca Cran long 2391ec38b344SPoul-Henning Kamp vmspace_swap_count(struct vmspace *vmspace) 2392ec38b344SPoul-Henning Kamp { 239365d8409cSRebecca Cran vm_map_t map; 2394ec38b344SPoul-Henning Kamp vm_map_entry_t cur; 239565d8409cSRebecca Cran vm_object_t object; 23962860553aSRebecca Cran long count, n; 239765d8409cSRebecca Cran 239865d8409cSRebecca Cran map = &vmspace->vm_map; 239965d8409cSRebecca Cran count = 0; 2400ec38b344SPoul-Henning Kamp 2401ec38b344SPoul-Henning Kamp for (cur = map->header.next; cur != &map->header; cur = cur->next) { 2402ec38b344SPoul-Henning Kamp if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 && 2403ec38b344SPoul-Henning Kamp (object = cur->object.vm_object) != NULL) { 240489f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 2405ec38b344SPoul-Henning Kamp if (object->type == OBJT_SWAP && 2406ec38b344SPoul-Henning Kamp object->un_pager.swp.swp_bcount != 0) { 240765d8409cSRebecca Cran n = (cur->end - cur->start) / PAGE_SIZE; 2408ec38b344SPoul-Henning Kamp count += object->un_pager.swp.swp_bcount * 2409ec38b344SPoul-Henning Kamp SWAP_META_PAGES * n / object->size + 1; 2410ec38b344SPoul-Henning Kamp } 241189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 2412ec38b344SPoul-Henning Kamp } 2413ec38b344SPoul-Henning Kamp } 2414ec38b344SPoul-Henning Kamp return (count); 2415ec38b344SPoul-Henning Kamp } 2416dee34ca4SPoul-Henning Kamp 2417dee34ca4SPoul-Henning Kamp /* 2418dee34ca4SPoul-Henning Kamp * GEOM backend 2419dee34ca4SPoul-Henning Kamp * 2420dee34ca4SPoul-Henning Kamp * Swapping onto disk devices. 2421dee34ca4SPoul-Henning Kamp * 2422dee34ca4SPoul-Henning Kamp */ 2423dee34ca4SPoul-Henning Kamp 24245721c9c7SPoul-Henning Kamp static g_orphan_t swapgeom_orphan; 24255721c9c7SPoul-Henning Kamp 2426dee34ca4SPoul-Henning Kamp static struct g_class g_swap_class = { 2427dee34ca4SPoul-Henning Kamp .name = "SWAP", 24285721c9c7SPoul-Henning Kamp .version = G_VERSION, 24295721c9c7SPoul-Henning Kamp .orphan = swapgeom_orphan, 2430dee34ca4SPoul-Henning Kamp }; 2431dee34ca4SPoul-Henning Kamp 2432dee34ca4SPoul-Henning Kamp DECLARE_GEOM_CLASS(g_swap_class, g_class); 2433dee34ca4SPoul-Henning Kamp 2434dee34ca4SPoul-Henning Kamp 2435dee34ca4SPoul-Henning Kamp static void 24363398491bSAlexander Motin swapgeom_close_ev(void *arg, int flags) 24373398491bSAlexander Motin { 24383398491bSAlexander Motin struct g_consumer *cp; 24393398491bSAlexander Motin 24403398491bSAlexander Motin cp = arg; 24413398491bSAlexander Motin g_access(cp, -1, -1, 0); 24423398491bSAlexander Motin g_detach(cp); 24433398491bSAlexander Motin g_destroy_consumer(cp); 24443398491bSAlexander Motin } 24453398491bSAlexander Motin 24469e3e3fe5SWarner Losh /* 24479e3e3fe5SWarner Losh * Add a reference to the g_consumer for an inflight transaction. 24489e3e3fe5SWarner Losh */ 24499e3e3fe5SWarner Losh static void 24509e3e3fe5SWarner Losh swapgeom_acquire(struct g_consumer *cp) 24519e3e3fe5SWarner Losh { 24529e3e3fe5SWarner Losh 24539e3e3fe5SWarner Losh mtx_assert(&sw_dev_mtx, MA_OWNED); 24549e3e3fe5SWarner Losh cp->index++; 24559e3e3fe5SWarner Losh } 24569e3e3fe5SWarner Losh 24579e3e3fe5SWarner Losh /* 24580c657d22SKonstantin Belousov * Remove a reference from the g_consumer. Post a close event if all 24590c657d22SKonstantin Belousov * references go away, since the function might be called from the 24600c657d22SKonstantin Belousov * biodone context. 24619e3e3fe5SWarner Losh */ 24629e3e3fe5SWarner Losh static void 24639e3e3fe5SWarner Losh swapgeom_release(struct g_consumer *cp, struct swdevt *sp) 24649e3e3fe5SWarner Losh { 24659e3e3fe5SWarner Losh 24669e3e3fe5SWarner Losh mtx_assert(&sw_dev_mtx, MA_OWNED); 24679e3e3fe5SWarner Losh cp->index--; 24689e3e3fe5SWarner Losh if (cp->index == 0) { 24699e3e3fe5SWarner Losh if (g_post_event(swapgeom_close_ev, cp, M_NOWAIT, NULL) == 0) 24709e3e3fe5SWarner Losh sp->sw_id = NULL; 24719e3e3fe5SWarner Losh } 24729e3e3fe5SWarner Losh } 24739e3e3fe5SWarner Losh 24743398491bSAlexander Motin static void 2475dee34ca4SPoul-Henning Kamp swapgeom_done(struct bio *bp2) 2476dee34ca4SPoul-Henning Kamp { 24773398491bSAlexander Motin struct swdevt *sp; 2478dee34ca4SPoul-Henning Kamp struct buf *bp; 24793398491bSAlexander Motin struct g_consumer *cp; 2480dee34ca4SPoul-Henning Kamp 2481dee34ca4SPoul-Henning Kamp bp = bp2->bio_caller2; 24823398491bSAlexander Motin cp = bp2->bio_from; 2483c5d3d25eSPoul-Henning Kamp bp->b_ioflags = bp2->bio_flags; 2484dee34ca4SPoul-Henning Kamp if (bp2->bio_error) 2485dee34ca4SPoul-Henning Kamp bp->b_ioflags |= BIO_ERROR; 2486c5d3d25eSPoul-Henning Kamp bp->b_resid = bp->b_bcount - bp2->bio_completed; 2487c5d3d25eSPoul-Henning Kamp bp->b_error = bp2->bio_error; 2488dee34ca4SPoul-Henning Kamp bufdone(bp); 24893398491bSAlexander Motin sp = bp2->bio_caller1; 24909e3e3fe5SWarner Losh mtx_lock(&sw_dev_mtx); 24919e3e3fe5SWarner Losh swapgeom_release(cp, sp); 24923398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 2493dee34ca4SPoul-Henning Kamp g_destroy_bio(bp2); 2494dee34ca4SPoul-Henning Kamp } 2495dee34ca4SPoul-Henning Kamp 2496dee34ca4SPoul-Henning Kamp static void 2497dee34ca4SPoul-Henning Kamp swapgeom_strategy(struct buf *bp, struct swdevt *sp) 2498dee34ca4SPoul-Henning Kamp { 2499dee34ca4SPoul-Henning Kamp struct bio *bio; 2500dee34ca4SPoul-Henning Kamp struct g_consumer *cp; 2501dee34ca4SPoul-Henning Kamp 25023398491bSAlexander Motin mtx_lock(&sw_dev_mtx); 2503dee34ca4SPoul-Henning Kamp cp = sp->sw_id; 2504dee34ca4SPoul-Henning Kamp if (cp == NULL) { 25053398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 2506dee34ca4SPoul-Henning Kamp bp->b_error = ENXIO; 2507dee34ca4SPoul-Henning Kamp bp->b_ioflags |= BIO_ERROR; 2508dee34ca4SPoul-Henning Kamp bufdone(bp); 2509dee34ca4SPoul-Henning Kamp return; 2510dee34ca4SPoul-Henning Kamp } 25119e3e3fe5SWarner Losh swapgeom_acquire(cp); 25123398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 251311041003SKonstantin Belousov if (bp->b_iocmd == BIO_WRITE) 251411041003SKonstantin Belousov bio = g_new_bio(); 251511041003SKonstantin Belousov else 25164f8205e5SPoul-Henning Kamp bio = g_alloc_bio(); 25174f8205e5SPoul-Henning Kamp if (bio == NULL) { 25189e3e3fe5SWarner Losh mtx_lock(&sw_dev_mtx); 25199e3e3fe5SWarner Losh swapgeom_release(cp, sp); 25209e3e3fe5SWarner Losh mtx_unlock(&sw_dev_mtx); 25213e5b6861SPoul-Henning Kamp bp->b_error = ENOMEM; 25223e5b6861SPoul-Henning Kamp bp->b_ioflags |= BIO_ERROR; 25233e5b6861SPoul-Henning Kamp bufdone(bp); 25243e5b6861SPoul-Henning Kamp return; 25253e5b6861SPoul-Henning Kamp } 252611041003SKonstantin Belousov 25273398491bSAlexander Motin bio->bio_caller1 = sp; 2528dee34ca4SPoul-Henning Kamp bio->bio_caller2 = bp; 2529c5d3d25eSPoul-Henning Kamp bio->bio_cmd = bp->b_iocmd; 2530dee34ca4SPoul-Henning Kamp bio->bio_offset = (bp->b_blkno - sp->sw_first) * PAGE_SIZE; 2531dee34ca4SPoul-Henning Kamp bio->bio_length = bp->b_bcount; 2532dee34ca4SPoul-Henning Kamp bio->bio_done = swapgeom_done; 2533fade8dd7SJeff Roberson if (!buf_mapped(bp)) { 25342cc718a1SKonstantin Belousov bio->bio_ma = bp->b_pages; 25352cc718a1SKonstantin Belousov bio->bio_data = unmapped_buf; 25362cc718a1SKonstantin Belousov bio->bio_ma_offset = (vm_offset_t)bp->b_offset & PAGE_MASK; 25372cc718a1SKonstantin Belousov bio->bio_ma_n = bp->b_npages; 25382cc718a1SKonstantin Belousov bio->bio_flags |= BIO_UNMAPPED; 25392cc718a1SKonstantin Belousov } else { 25402cc718a1SKonstantin Belousov bio->bio_data = bp->b_data; 25412cc718a1SKonstantin Belousov bio->bio_ma = NULL; 25422cc718a1SKonstantin Belousov } 2543dee34ca4SPoul-Henning Kamp g_io_request(bio, cp); 2544dee34ca4SPoul-Henning Kamp return; 2545dee34ca4SPoul-Henning Kamp } 2546dee34ca4SPoul-Henning Kamp 2547dee34ca4SPoul-Henning Kamp static void 2548dee34ca4SPoul-Henning Kamp swapgeom_orphan(struct g_consumer *cp) 2549dee34ca4SPoul-Henning Kamp { 2550dee34ca4SPoul-Henning Kamp struct swdevt *sp; 25513398491bSAlexander Motin int destroy; 2552dee34ca4SPoul-Henning Kamp 2553dee34ca4SPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 25543398491bSAlexander Motin TAILQ_FOREACH(sp, &swtailq, sw_list) { 25553398491bSAlexander Motin if (sp->sw_id == cp) { 25568f12d83aSAlexander Motin sp->sw_flags |= SW_CLOSING; 25573398491bSAlexander Motin break; 2558dee34ca4SPoul-Henning Kamp } 25593398491bSAlexander Motin } 25609e3e3fe5SWarner Losh /* 25619e3e3fe5SWarner Losh * Drop reference we were created with. Do directly since we're in a 25629e3e3fe5SWarner Losh * special context where we don't have to queue the call to 25639e3e3fe5SWarner Losh * swapgeom_close_ev(). 25649e3e3fe5SWarner Losh */ 25659e3e3fe5SWarner Losh cp->index--; 25663398491bSAlexander Motin destroy = ((sp != NULL) && (cp->index == 0)); 25673398491bSAlexander Motin if (destroy) 25683398491bSAlexander Motin sp->sw_id = NULL; 25693398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 25703398491bSAlexander Motin if (destroy) 25713398491bSAlexander Motin swapgeom_close_ev(cp, 0); 2572dee34ca4SPoul-Henning Kamp } 2573dee34ca4SPoul-Henning Kamp 2574dee34ca4SPoul-Henning Kamp static void 2575dee34ca4SPoul-Henning Kamp swapgeom_close(struct thread *td, struct swdevt *sw) 2576dee34ca4SPoul-Henning Kamp { 25773398491bSAlexander Motin struct g_consumer *cp; 2578dee34ca4SPoul-Henning Kamp 25793398491bSAlexander Motin mtx_lock(&sw_dev_mtx); 25803398491bSAlexander Motin cp = sw->sw_id; 25813398491bSAlexander Motin sw->sw_id = NULL; 25823398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 25830c657d22SKonstantin Belousov 25840c657d22SKonstantin Belousov /* 25850c657d22SKonstantin Belousov * swapgeom_close() may be called from the biodone context, 25860c657d22SKonstantin Belousov * where we cannot perform topology changes. Delegate the 25870c657d22SKonstantin Belousov * work to the events thread. 25880c657d22SKonstantin Belousov */ 25893398491bSAlexander Motin if (cp != NULL) 25903398491bSAlexander Motin g_waitfor_event(swapgeom_close_ev, cp, M_WAITOK, NULL); 2591dee34ca4SPoul-Henning Kamp } 2592dee34ca4SPoul-Henning Kamp 259388ad2d7bSKonstantin Belousov static int 259488ad2d7bSKonstantin Belousov swapongeom_locked(struct cdev *dev, struct vnode *vp) 2595dee34ca4SPoul-Henning Kamp { 2596dee34ca4SPoul-Henning Kamp struct g_provider *pp; 2597dee34ca4SPoul-Henning Kamp struct g_consumer *cp; 2598dee34ca4SPoul-Henning Kamp static struct g_geom *gp; 2599dee34ca4SPoul-Henning Kamp struct swdevt *sp; 2600dee34ca4SPoul-Henning Kamp u_long nblks; 2601dee34ca4SPoul-Henning Kamp int error; 2602dee34ca4SPoul-Henning Kamp 260388ad2d7bSKonstantin Belousov pp = g_dev_getprovider(dev); 260488ad2d7bSKonstantin Belousov if (pp == NULL) 260588ad2d7bSKonstantin Belousov return (ENODEV); 2606dee34ca4SPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 2607dee34ca4SPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 2608dee34ca4SPoul-Henning Kamp cp = sp->sw_id; 2609dee34ca4SPoul-Henning Kamp if (cp != NULL && cp->provider == pp) { 2610dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 261188ad2d7bSKonstantin Belousov return (EBUSY); 2612dee34ca4SPoul-Henning Kamp } 2613dee34ca4SPoul-Henning Kamp } 2614dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 26155721c9c7SPoul-Henning Kamp if (gp == NULL) 261602c62349SJaakko Heinonen gp = g_new_geomf(&g_swap_class, "swap"); 2617dee34ca4SPoul-Henning Kamp cp = g_new_consumer(gp); 26189e3e3fe5SWarner Losh cp->index = 1; /* Number of active I/Os, plus one for being active. */ 26199e3e3fe5SWarner Losh cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 2620dee34ca4SPoul-Henning Kamp g_attach(cp, pp); 2621afeb65e6SPoul-Henning Kamp /* 2622afeb65e6SPoul-Henning Kamp * XXX: Every time you think you can improve the margin for 2623afeb65e6SPoul-Henning Kamp * footshooting, somebody depends on the ability to do so: 2624afeb65e6SPoul-Henning Kamp * savecore(8) wants to write to our swapdev so we cannot 2625afeb65e6SPoul-Henning Kamp * set an exclusive count :-( 2626afeb65e6SPoul-Henning Kamp */ 2627d2bae332SPoul-Henning Kamp error = g_access(cp, 1, 1, 0); 262888ad2d7bSKonstantin Belousov if (error != 0) { 2629dee34ca4SPoul-Henning Kamp g_detach(cp); 2630dee34ca4SPoul-Henning Kamp g_destroy_consumer(cp); 263188ad2d7bSKonstantin Belousov return (error); 2632dee34ca4SPoul-Henning Kamp } 2633dee34ca4SPoul-Henning Kamp nblks = pp->mediasize / DEV_BSIZE; 263488ad2d7bSKonstantin Belousov swaponsomething(vp, cp, nblks, swapgeom_strategy, 263588ad2d7bSKonstantin Belousov swapgeom_close, dev2udev(dev), 26362cc718a1SKonstantin Belousov (pp->flags & G_PF_ACCEPT_UNMAPPED) != 0 ? SW_UNMAPPED : 0); 263788ad2d7bSKonstantin Belousov return (0); 2638dee34ca4SPoul-Henning Kamp } 2639dee34ca4SPoul-Henning Kamp 2640dee34ca4SPoul-Henning Kamp static int 264188ad2d7bSKonstantin Belousov swapongeom(struct vnode *vp) 2642dee34ca4SPoul-Henning Kamp { 2643dee34ca4SPoul-Henning Kamp int error; 2644dee34ca4SPoul-Henning Kamp 2645cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 264688ad2d7bSKonstantin Belousov if (vp->v_type != VCHR || (vp->v_iflag & VI_DOOMED) != 0) { 264788ad2d7bSKonstantin Belousov error = ENOENT; 264888ad2d7bSKonstantin Belousov } else { 264988ad2d7bSKonstantin Belousov g_topology_lock(); 265088ad2d7bSKonstantin Belousov error = swapongeom_locked(vp->v_rdev, vp); 265188ad2d7bSKonstantin Belousov g_topology_unlock(); 265288ad2d7bSKonstantin Belousov } 265322db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 2654dee34ca4SPoul-Henning Kamp return (error); 2655dee34ca4SPoul-Henning Kamp } 2656dee34ca4SPoul-Henning Kamp 2657dee34ca4SPoul-Henning Kamp /* 2658dee34ca4SPoul-Henning Kamp * VNODE backend 2659dee34ca4SPoul-Henning Kamp * 2660dee34ca4SPoul-Henning Kamp * This is used mainly for network filesystem (read: probably only tested 2661dee34ca4SPoul-Henning Kamp * with NFS) swapfiles. 2662dee34ca4SPoul-Henning Kamp * 2663dee34ca4SPoul-Henning Kamp */ 2664dee34ca4SPoul-Henning Kamp 2665dee34ca4SPoul-Henning Kamp static void 2666dee34ca4SPoul-Henning Kamp swapdev_strategy(struct buf *bp, struct swdevt *sp) 2667dee34ca4SPoul-Henning Kamp { 2668494eb176SPoul-Henning Kamp struct vnode *vp2; 2669dee34ca4SPoul-Henning Kamp 2670dee34ca4SPoul-Henning Kamp bp->b_blkno = ctodb(bp->b_blkno - sp->sw_first); 2671dee34ca4SPoul-Henning Kamp 2672dee34ca4SPoul-Henning Kamp vp2 = sp->sw_id; 2673dee34ca4SPoul-Henning Kamp vhold(vp2); 2674dee34ca4SPoul-Henning Kamp if (bp->b_iocmd == BIO_WRITE) { 26753cfc7651SOlivier Houchard if (bp->b_bufobj) 2676494eb176SPoul-Henning Kamp bufobj_wdrop(bp->b_bufobj); 2677a76d8f4eSPoul-Henning Kamp bufobj_wref(&vp2->v_bufobj); 2678dee34ca4SPoul-Henning Kamp } 26793cfc7651SOlivier Houchard if (bp->b_bufobj != &vp2->v_bufobj) 26803cfc7651SOlivier Houchard bp->b_bufobj = &vp2->v_bufobj; 2681dee34ca4SPoul-Henning Kamp bp->b_vp = vp2; 26822c18019fSPoul-Henning Kamp bp->b_iooffset = dbtob(bp->b_blkno); 2683b792bebeSPoul-Henning Kamp bstrategy(bp); 2684dee34ca4SPoul-Henning Kamp return; 2685dee34ca4SPoul-Henning Kamp } 2686dee34ca4SPoul-Henning Kamp 2687dee34ca4SPoul-Henning Kamp static void 2688dee34ca4SPoul-Henning Kamp swapdev_close(struct thread *td, struct swdevt *sp) 2689dee34ca4SPoul-Henning Kamp { 2690dee34ca4SPoul-Henning Kamp 2691dee34ca4SPoul-Henning Kamp VOP_CLOSE(sp->sw_vp, FREAD | FWRITE, td->td_ucred, td); 2692dee34ca4SPoul-Henning Kamp vrele(sp->sw_vp); 2693dee34ca4SPoul-Henning Kamp } 2694dee34ca4SPoul-Henning Kamp 2695dee34ca4SPoul-Henning Kamp 2696dee34ca4SPoul-Henning Kamp static int 2697dee34ca4SPoul-Henning Kamp swaponvp(struct thread *td, struct vnode *vp, u_long nblks) 2698dee34ca4SPoul-Henning Kamp { 2699dee34ca4SPoul-Henning Kamp struct swdevt *sp; 2700dee34ca4SPoul-Henning Kamp int error; 2701dee34ca4SPoul-Henning Kamp 2702dee34ca4SPoul-Henning Kamp if (nblks == 0) 2703dee34ca4SPoul-Henning Kamp return (ENXIO); 2704dee34ca4SPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 2705dee34ca4SPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 2706dee34ca4SPoul-Henning Kamp if (sp->sw_id == vp) { 2707dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 2708dee34ca4SPoul-Henning Kamp return (EBUSY); 2709dee34ca4SPoul-Henning Kamp } 2710dee34ca4SPoul-Henning Kamp } 2711dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 2712dee34ca4SPoul-Henning Kamp 2713cb05b60aSAttilio Rao (void) vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2714dee34ca4SPoul-Henning Kamp #ifdef MAC 271530d239bcSRobert Watson error = mac_system_check_swapon(td->td_ucred, vp); 2716dee34ca4SPoul-Henning Kamp if (error == 0) 2717dee34ca4SPoul-Henning Kamp #endif 27189e223287SKonstantin Belousov error = VOP_OPEN(vp, FREAD | FWRITE, td->td_ucred, td, NULL); 271922db15c0SAttilio Rao (void) VOP_UNLOCK(vp, 0); 2720dee34ca4SPoul-Henning Kamp if (error) 2721dee34ca4SPoul-Henning Kamp return (error); 2722dee34ca4SPoul-Henning Kamp 2723dee34ca4SPoul-Henning Kamp swaponsomething(vp, vp, nblks, swapdev_strategy, swapdev_close, 27242cc718a1SKonstantin Belousov NODEV, 0); 2725dee34ca4SPoul-Henning Kamp return (0); 2726dee34ca4SPoul-Henning Kamp } 272789c241d1SGleb Smirnoff 272889c241d1SGleb Smirnoff static int 272989c241d1SGleb Smirnoff sysctl_swap_async_max(SYSCTL_HANDLER_ARGS) 273089c241d1SGleb Smirnoff { 273189c241d1SGleb Smirnoff int error, new, n; 273289c241d1SGleb Smirnoff 273389c241d1SGleb Smirnoff new = nsw_wcount_async_max; 273489c241d1SGleb Smirnoff error = sysctl_handle_int(oidp, &new, 0, req); 273589c241d1SGleb Smirnoff if (error != 0 || req->newptr == NULL) 273689c241d1SGleb Smirnoff return (error); 273789c241d1SGleb Smirnoff 273889c241d1SGleb Smirnoff if (new > nswbuf / 2 || new < 1) 273989c241d1SGleb Smirnoff return (EINVAL); 274089c241d1SGleb Smirnoff 274189c241d1SGleb Smirnoff mtx_lock(&pbuf_mtx); 274289c241d1SGleb Smirnoff while (nsw_wcount_async_max != new) { 274389c241d1SGleb Smirnoff /* 274489c241d1SGleb Smirnoff * Adjust difference. If the current async count is too low, 274589c241d1SGleb Smirnoff * we will need to sqeeze our update slowly in. Sleep with a 274689c241d1SGleb Smirnoff * higher priority than getpbuf() to finish faster. 274789c241d1SGleb Smirnoff */ 274889c241d1SGleb Smirnoff n = new - nsw_wcount_async_max; 274989c241d1SGleb Smirnoff if (nsw_wcount_async + n >= 0) { 275089c241d1SGleb Smirnoff nsw_wcount_async += n; 275189c241d1SGleb Smirnoff nsw_wcount_async_max += n; 275289c241d1SGleb Smirnoff wakeup(&nsw_wcount_async); 275389c241d1SGleb Smirnoff } else { 275489c241d1SGleb Smirnoff nsw_wcount_async_max -= nsw_wcount_async; 275589c241d1SGleb Smirnoff nsw_wcount_async = 0; 275689c241d1SGleb Smirnoff msleep(&nsw_wcount_async, &pbuf_mtx, PSWP, 275789c241d1SGleb Smirnoff "swpsysctl", 0); 275889c241d1SGleb Smirnoff } 275989c241d1SGleb Smirnoff } 276089c241d1SGleb Smirnoff mtx_unlock(&pbuf_mtx); 276189c241d1SGleb Smirnoff 276289c241d1SGleb Smirnoff return (0); 276389c241d1SGleb Smirnoff } 2764