160727d8bSWarner Losh /*- 2df57947fSPedro F. Giffuni * SPDX-License-Identifier: BSD-4-Clause 3df57947fSPedro F. Giffuni * 41c7c3c6aSMatthew Dillon * Copyright (c) 1998 Matthew Dillon, 526f9a767SRodney W. Grimes * Copyright (c) 1994 John S. Dyson 6df8bae1dSRodney W. Grimes * Copyright (c) 1990 University of Utah. 7e9c0cc15SPoul-Henning Kamp * Copyright (c) 1982, 1986, 1989, 1993 8df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 11df8bae1dSRodney W. Grimes * the Systems Programming Group of the University of Utah Computer 12df8bae1dSRodney W. Grimes * Science Department. 13df8bae1dSRodney W. Grimes * 14df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 15df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 16df8bae1dSRodney W. Grimes * are met: 17df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 18df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 19df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 20df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 21df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 22df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 235929bcfaSPhilippe Charnier * must display the following acknowledgement: 24df8bae1dSRodney W. Grimes * This product includes software developed by the University of 25df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 26df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 27df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 28df8bae1dSRodney W. Grimes * without specific prior written permission. 29df8bae1dSRodney W. Grimes * 30df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 31df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 34df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 35df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 36df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 37df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 38df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 39df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 40df8bae1dSRodney W. Grimes * SUCH DAMAGE. 41df8bae1dSRodney W. Grimes * 421c7c3c6aSMatthew Dillon * New Swap System 431c7c3c6aSMatthew Dillon * Matthew Dillon 441c7c3c6aSMatthew Dillon * 451c7c3c6aSMatthew Dillon * Radix Bitmap 'blists'. 461c7c3c6aSMatthew Dillon * 471c7c3c6aSMatthew Dillon * - The new swapper uses the new radix bitmap code. This should scale 481c7c3c6aSMatthew Dillon * to arbitrarily small or arbitrarily large swap spaces and an almost 491c7c3c6aSMatthew Dillon * arbitrary degree of fragmentation. 501c7c3c6aSMatthew Dillon * 511c7c3c6aSMatthew Dillon * Features: 521c7c3c6aSMatthew Dillon * 531c7c3c6aSMatthew Dillon * - on the fly reallocation of swap during putpages. The new system 541c7c3c6aSMatthew Dillon * does not try to keep previously allocated swap blocks for dirty 551c7c3c6aSMatthew Dillon * pages. 561c7c3c6aSMatthew Dillon * 571c7c3c6aSMatthew Dillon * - on the fly deallocation of swap 581c7c3c6aSMatthew Dillon * 591c7c3c6aSMatthew Dillon * - No more garbage collection required. Unnecessarily allocated swap 601c7c3c6aSMatthew Dillon * blocks only exist for dirty vm_page_t's now and these are already 611c7c3c6aSMatthew Dillon * cycled (in a high-load system) by the pager. We also do on-the-fly 621c7c3c6aSMatthew Dillon * removal of invalidated swap blocks when a page is destroyed 631c7c3c6aSMatthew Dillon * or renamed. 641c7c3c6aSMatthew Dillon * 65df8bae1dSRodney W. Grimes * from: Utah $Hdr: swap_pager.c 1.4 91/04/30$ 66df8bae1dSRodney W. Grimes * 67df8bae1dSRodney W. Grimes * @(#)swap_pager.c 8.9 (Berkeley) 3/21/94 68e9c0cc15SPoul-Henning Kamp * @(#)vm_swap.c 8.5 (Berkeley) 2/17/94 69df8bae1dSRodney W. Grimes */ 70df8bae1dSRodney W. Grimes 71874651b1SDavid E. O'Brien #include <sys/cdefs.h> 72874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 73874651b1SDavid E. O'Brien 74e9c0cc15SPoul-Henning Kamp #include "opt_swap.h" 75e9c0cc15SPoul-Henning Kamp #include "opt_vm.h" 76e9c0cc15SPoul-Henning Kamp 77df8bae1dSRodney W. Grimes #include <sys/param.h> 78df8bae1dSRodney W. Grimes #include <sys/systm.h> 79af647ddeSBruce Evans #include <sys/conf.h> 8064abb5a5SDavid Greenman #include <sys/kernel.h> 81acd3428bSRobert Watson #include <sys/priv.h> 82df8bae1dSRodney W. Grimes #include <sys/proc.h> 839626b608SPoul-Henning Kamp #include <sys/bio.h> 84df8bae1dSRodney W. Grimes #include <sys/buf.h> 85e9c0cc15SPoul-Henning Kamp #include <sys/disk.h> 86e9c0cc15SPoul-Henning Kamp #include <sys/fcntl.h> 87e9c0cc15SPoul-Henning Kamp #include <sys/mount.h> 88e9c0cc15SPoul-Henning Kamp #include <sys/namei.h> 89df8bae1dSRodney W. Grimes #include <sys/vnode.h> 90df8bae1dSRodney W. Grimes #include <sys/malloc.h> 91f425ab8eSKonstantin Belousov #include <sys/pctrie.h> 921ba5ad42SEdward Tomasz Napierala #include <sys/racct.h> 933364c323SKonstantin Belousov #include <sys/resource.h> 943364c323SKonstantin Belousov #include <sys/resourcevar.h> 9589f6b863SAttilio Rao #include <sys/rwlock.h> 96d027ed2eSAlan Cox #include <sys/sbuf.h> 97327f4e83SMatthew Dillon #include <sys/sysctl.h> 98e9c0cc15SPoul-Henning Kamp #include <sys/sysproto.h> 991c7c3c6aSMatthew Dillon #include <sys/blist.h> 1001c7c3c6aSMatthew Dillon #include <sys/lock.h> 1010cddd8f0SMatthew Dillon #include <sys/sx.h> 102936524aaSMatthew Dillon #include <sys/vmmeter.h> 103df8bae1dSRodney W. Grimes 104aed55708SRobert Watson #include <security/mac/mac_framework.h> 105aed55708SRobert Watson 106df8bae1dSRodney W. Grimes #include <vm/vm.h> 10721cd6e62SSeigo Tanimura #include <vm/pmap.h> 10821cd6e62SSeigo Tanimura #include <vm/vm_map.h> 10921cd6e62SSeigo Tanimura #include <vm/vm_kern.h> 110efeaf95aSDavid Greenman #include <vm/vm_object.h> 111df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 112efeaf95aSDavid Greenman #include <vm/vm_pager.h> 113df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h> 114e9c0cc15SPoul-Henning Kamp #include <vm/vm_param.h> 115df8bae1dSRodney W. Grimes #include <vm/swap_pager.h> 116efeaf95aSDavid Greenman #include <vm/vm_extern.h> 117670d17b5SJeff Roberson #include <vm/uma.h> 118df8bae1dSRodney W. Grimes 119dee34ca4SPoul-Henning Kamp #include <geom/geom.h> 120dee34ca4SPoul-Henning Kamp 121ec38b344SPoul-Henning Kamp /* 122064650c1SAlan Cox * MAX_PAGEOUT_CLUSTER must be a power of 2 between 1 and 64. 123064650c1SAlan Cox * The 64-page limit is due to the radix code (kern/subr_blist.c). 124ec38b344SPoul-Henning Kamp */ 125ec38b344SPoul-Henning Kamp #ifndef MAX_PAGEOUT_CLUSTER 126e2241590SAlan Cox #define MAX_PAGEOUT_CLUSTER 32 127ec38b344SPoul-Henning Kamp #endif 128ec38b344SPoul-Henning Kamp 129ec38b344SPoul-Henning Kamp #if !defined(SWB_NPAGES) 130ec38b344SPoul-Henning Kamp #define SWB_NPAGES MAX_PAGEOUT_CLUSTER 131ec38b344SPoul-Henning Kamp #endif 132ec38b344SPoul-Henning Kamp 133f425ab8eSKonstantin Belousov #define SWAP_META_PAGES PCTRIE_COUNT 134ec38b344SPoul-Henning Kamp 135f425ab8eSKonstantin Belousov /* 136f425ab8eSKonstantin Belousov * A swblk structure maps each page index within a 137f425ab8eSKonstantin Belousov * SWAP_META_PAGES-aligned and sized range to the address of an 138f425ab8eSKonstantin Belousov * on-disk swap block (or SWAPBLK_NONE). The collection of these 139f425ab8eSKonstantin Belousov * mappings for an entire vm object is implemented as a pc-trie. 140f425ab8eSKonstantin Belousov */ 141f425ab8eSKonstantin Belousov struct swblk { 142f425ab8eSKonstantin Belousov vm_pindex_t p; 143f425ab8eSKonstantin Belousov daddr_t d[SWAP_META_PAGES]; 144e9c0cc15SPoul-Henning Kamp }; 145e9c0cc15SPoul-Henning Kamp 1462c4992dbSAlan Cox static MALLOC_DEFINE(M_VMPGDATA, "vm_pgdata", "swap pager private data"); 14720da9c2eSPoul-Henning Kamp static struct mtx sw_dev_mtx; 1488f60c087SPoul-Henning Kamp static TAILQ_HEAD(, swdevt) swtailq = TAILQ_HEAD_INITIALIZER(swtailq); 1498f60c087SPoul-Henning Kamp static struct swdevt *swdevhd; /* Allocate from here next */ 1508f60c087SPoul-Henning Kamp static int nswapdev; /* Number of swap devices */ 1518f60c087SPoul-Henning Kamp int swap_pager_avail; 15204533e1eSKonstantin Belousov static struct sx swdev_syscall_lock; /* serialize swap(on|off) */ 153e9c0cc15SPoul-Henning Kamp 1543364c323SKonstantin Belousov static vm_ooffset_t swap_total; 1558a9c731fSIvan Voras SYSCTL_QUAD(_vm, OID_AUTO, swap_total, CTLFLAG_RD, &swap_total, 0, 1568a9c731fSIvan Voras "Total amount of available swap storage."); 1573364c323SKonstantin Belousov static vm_ooffset_t swap_reserved; 1588a9c731fSIvan Voras SYSCTL_QUAD(_vm, OID_AUTO, swap_reserved, CTLFLAG_RD, &swap_reserved, 0, 1598a9c731fSIvan Voras "Amount of swap storage needed to back all allocated anonymous memory."); 1603364c323SKonstantin Belousov static int overcommit = 0; 161be7d4ac5SEdward Tomasz Napierala SYSCTL_INT(_vm, VM_OVERCOMMIT, overcommit, CTLFLAG_RW, &overcommit, 0, 1628a9c731fSIvan Voras "Configure virtual memory overcommit behavior. See tuning(7) " 1638a9c731fSIvan Voras "for details."); 16461203277SDag-Erling Smørgrav static unsigned long swzone; 16561203277SDag-Erling Smørgrav SYSCTL_ULONG(_vm, OID_AUTO, swzone, CTLFLAG_RD, &swzone, 0, 16661203277SDag-Erling Smørgrav "Actual size of swap metadata zone"); 16761203277SDag-Erling Smørgrav static unsigned long swap_maxpages; 16861203277SDag-Erling Smørgrav SYSCTL_ULONG(_vm, OID_AUTO, swap_maxpages, CTLFLAG_RD, &swap_maxpages, 0, 16961203277SDag-Erling Smørgrav "Maximum amount of swap supported"); 1703364c323SKonstantin Belousov 1713364c323SKonstantin Belousov /* bits from overcommit */ 1723364c323SKonstantin Belousov #define SWAP_RESERVE_FORCE_ON (1 << 0) 1733364c323SKonstantin Belousov #define SWAP_RESERVE_RLIMIT_ON (1 << 1) 1743364c323SKonstantin Belousov #define SWAP_RESERVE_ALLOW_NONWIRED (1 << 2) 1753364c323SKonstantin Belousov 1763364c323SKonstantin Belousov int 1773364c323SKonstantin Belousov swap_reserve(vm_ooffset_t incr) 1783364c323SKonstantin Belousov { 1793364c323SKonstantin Belousov 180ef694c1aSEdward Tomasz Napierala return (swap_reserve_by_cred(incr, curthread->td_ucred)); 1813364c323SKonstantin Belousov } 1823364c323SKonstantin Belousov 1833364c323SKonstantin Belousov int 184ef694c1aSEdward Tomasz Napierala swap_reserve_by_cred(vm_ooffset_t incr, struct ucred *cred) 1853364c323SKonstantin Belousov { 1865c0e1c11SKonstantin Belousov vm_ooffset_t r, s; 1873364c323SKonstantin Belousov int res, error; 1883364c323SKonstantin Belousov static int curfail; 1893364c323SKonstantin Belousov static struct timeval lastfail; 190ef694c1aSEdward Tomasz Napierala struct uidinfo *uip; 191ef694c1aSEdward Tomasz Napierala 192ef694c1aSEdward Tomasz Napierala uip = cred->cr_ruidinfo; 1933364c323SKonstantin Belousov 1943364c323SKonstantin Belousov if (incr & PAGE_MASK) 1953364c323SKonstantin Belousov panic("swap_reserve: & PAGE_MASK"); 1963364c323SKonstantin Belousov 197afcc55f3SEdward Tomasz Napierala #ifdef RACCT 1984b5c9cf6SEdward Tomasz Napierala if (racct_enable) { 1991ba5ad42SEdward Tomasz Napierala PROC_LOCK(curproc); 2001ba5ad42SEdward Tomasz Napierala error = racct_add(curproc, RACCT_SWAP, incr); 2011ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(curproc); 2021ba5ad42SEdward Tomasz Napierala if (error != 0) 2031ba5ad42SEdward Tomasz Napierala return (0); 2044b5c9cf6SEdward Tomasz Napierala } 205afcc55f3SEdward Tomasz Napierala #endif 2061ba5ad42SEdward Tomasz Napierala 2073364c323SKonstantin Belousov res = 0; 2083364c323SKonstantin Belousov mtx_lock(&sw_dev_mtx); 2093364c323SKonstantin Belousov r = swap_reserved + incr; 2103364c323SKonstantin Belousov if (overcommit & SWAP_RESERVE_ALLOW_NONWIRED) { 211e958ad4cSJeff Roberson s = vm_cnt.v_page_count - vm_cnt.v_free_reserved - 212e958ad4cSJeff Roberson vm_wire_count(); 2133364c323SKonstantin Belousov s *= PAGE_SIZE; 2143364c323SKonstantin Belousov } else 2153364c323SKonstantin Belousov s = 0; 2163364c323SKonstantin Belousov s += swap_total; 2173364c323SKonstantin Belousov if ((overcommit & SWAP_RESERVE_FORCE_ON) == 0 || r <= s || 2183364c323SKonstantin Belousov (error = priv_check(curthread, PRIV_VM_SWAP_NOQUOTA)) == 0) { 2193364c323SKonstantin Belousov res = 1; 2203364c323SKonstantin Belousov swap_reserved = r; 2213364c323SKonstantin Belousov } 2223364c323SKonstantin Belousov mtx_unlock(&sw_dev_mtx); 2233364c323SKonstantin Belousov 2243364c323SKonstantin Belousov if (res) { 2253364c323SKonstantin Belousov UIDINFO_VMSIZE_LOCK(uip); 2265c0e1c11SKonstantin Belousov if ((overcommit & SWAP_RESERVE_RLIMIT_ON) != 0 && 227f6f6d240SMateusz Guzik uip->ui_vmsize + incr > lim_cur(curthread, RLIMIT_SWAP) && 2285c0e1c11SKonstantin Belousov priv_check(curthread, PRIV_VM_SWAP_NORLIMIT)) 2293364c323SKonstantin Belousov res = 0; 2303364c323SKonstantin Belousov else 2313364c323SKonstantin Belousov uip->ui_vmsize += incr; 2323364c323SKonstantin Belousov UIDINFO_VMSIZE_UNLOCK(uip); 2333364c323SKonstantin Belousov if (!res) { 2343364c323SKonstantin Belousov mtx_lock(&sw_dev_mtx); 2353364c323SKonstantin Belousov swap_reserved -= incr; 2363364c323SKonstantin Belousov mtx_unlock(&sw_dev_mtx); 2373364c323SKonstantin Belousov } 2383364c323SKonstantin Belousov } 2393364c323SKonstantin Belousov if (!res && ppsratecheck(&lastfail, &curfail, 1)) { 2403364c323SKonstantin Belousov printf("uid %d, pid %d: swap reservation for %jd bytes failed\n", 241134465d7SKonstantin Belousov uip->ui_uid, curproc->p_pid, incr); 2423364c323SKonstantin Belousov } 2433364c323SKonstantin Belousov 244afcc55f3SEdward Tomasz Napierala #ifdef RACCT 2451ba5ad42SEdward Tomasz Napierala if (!res) { 2461ba5ad42SEdward Tomasz Napierala PROC_LOCK(curproc); 2471ba5ad42SEdward Tomasz Napierala racct_sub(curproc, RACCT_SWAP, incr); 2481ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(curproc); 2491ba5ad42SEdward Tomasz Napierala } 250afcc55f3SEdward Tomasz Napierala #endif 2511ba5ad42SEdward Tomasz Napierala 2523364c323SKonstantin Belousov return (res); 2533364c323SKonstantin Belousov } 2543364c323SKonstantin Belousov 2553364c323SKonstantin Belousov void 2563364c323SKonstantin Belousov swap_reserve_force(vm_ooffset_t incr) 2573364c323SKonstantin Belousov { 2583364c323SKonstantin Belousov struct uidinfo *uip; 2593364c323SKonstantin Belousov 2603364c323SKonstantin Belousov mtx_lock(&sw_dev_mtx); 2613364c323SKonstantin Belousov swap_reserved += incr; 2623364c323SKonstantin Belousov mtx_unlock(&sw_dev_mtx); 2633364c323SKonstantin Belousov 264afcc55f3SEdward Tomasz Napierala #ifdef RACCT 2651ba5ad42SEdward Tomasz Napierala PROC_LOCK(curproc); 2661ba5ad42SEdward Tomasz Napierala racct_add_force(curproc, RACCT_SWAP, incr); 2671ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(curproc); 268afcc55f3SEdward Tomasz Napierala #endif 2691ba5ad42SEdward Tomasz Napierala 2703364c323SKonstantin Belousov uip = curthread->td_ucred->cr_ruidinfo; 2713364c323SKonstantin Belousov PROC_LOCK(curproc); 2723364c323SKonstantin Belousov UIDINFO_VMSIZE_LOCK(uip); 2733364c323SKonstantin Belousov uip->ui_vmsize += incr; 2743364c323SKonstantin Belousov UIDINFO_VMSIZE_UNLOCK(uip); 2753364c323SKonstantin Belousov PROC_UNLOCK(curproc); 2763364c323SKonstantin Belousov } 2773364c323SKonstantin Belousov 2783364c323SKonstantin Belousov void 2793364c323SKonstantin Belousov swap_release(vm_ooffset_t decr) 2803364c323SKonstantin Belousov { 281ef694c1aSEdward Tomasz Napierala struct ucred *cred; 2823364c323SKonstantin Belousov 2833364c323SKonstantin Belousov PROC_LOCK(curproc); 284ef694c1aSEdward Tomasz Napierala cred = curthread->td_ucred; 285ef694c1aSEdward Tomasz Napierala swap_release_by_cred(decr, cred); 2863364c323SKonstantin Belousov PROC_UNLOCK(curproc); 2873364c323SKonstantin Belousov } 2883364c323SKonstantin Belousov 2893364c323SKonstantin Belousov void 290ef694c1aSEdward Tomasz Napierala swap_release_by_cred(vm_ooffset_t decr, struct ucred *cred) 2913364c323SKonstantin Belousov { 292ef694c1aSEdward Tomasz Napierala struct uidinfo *uip; 293ef694c1aSEdward Tomasz Napierala 294ef694c1aSEdward Tomasz Napierala uip = cred->cr_ruidinfo; 2953364c323SKonstantin Belousov 2963364c323SKonstantin Belousov if (decr & PAGE_MASK) 2973364c323SKonstantin Belousov panic("swap_release: & PAGE_MASK"); 2983364c323SKonstantin Belousov 2993364c323SKonstantin Belousov mtx_lock(&sw_dev_mtx); 3003364c323SKonstantin Belousov if (swap_reserved < decr) 3013364c323SKonstantin Belousov panic("swap_reserved < decr"); 3023364c323SKonstantin Belousov swap_reserved -= decr; 3033364c323SKonstantin Belousov mtx_unlock(&sw_dev_mtx); 3043364c323SKonstantin Belousov 3053364c323SKonstantin Belousov UIDINFO_VMSIZE_LOCK(uip); 3063364c323SKonstantin Belousov if (uip->ui_vmsize < decr) 3073364c323SKonstantin Belousov printf("negative vmsize for uid = %d\n", uip->ui_uid); 3083364c323SKonstantin Belousov uip->ui_vmsize -= decr; 3093364c323SKonstantin Belousov UIDINFO_VMSIZE_UNLOCK(uip); 3101ba5ad42SEdward Tomasz Napierala 3111ba5ad42SEdward Tomasz Napierala racct_sub_cred(cred, RACCT_SWAP, decr); 3123364c323SKonstantin Belousov } 3133364c323SKonstantin Belousov 3144abca9bbSAlan Cox #define SWM_POP 0x01 /* pop out */ 31526f9a767SRodney W. Grimes 316f08b3099SKonstantin Belousov static int swap_pager_full = 2; /* swap space exhaustion (task killing) */ 3177dea2c2eSAlan Cox static int swap_pager_almost_full = 1; /* swap space exhaustion (w/hysteresis)*/ 3181c7c3c6aSMatthew Dillon static int nsw_rcount; /* free read buffers */ 319327f4e83SMatthew Dillon static int nsw_wcount_sync; /* limit write buffers / synchronous */ 320327f4e83SMatthew Dillon static int nsw_wcount_async; /* limit write buffers / asynchronous */ 321327f4e83SMatthew Dillon static int nsw_wcount_async_max;/* assigned maximum */ 322327f4e83SMatthew Dillon static int nsw_cluster_max; /* maximum VOP I/O allowed */ 3231c7c3c6aSMatthew Dillon 32489c241d1SGleb Smirnoff static int sysctl_swap_async_max(SYSCTL_HANDLER_ARGS); 3254c36e917SKonstantin Belousov SYSCTL_PROC(_vm, OID_AUTO, swap_async_max, CTLTYPE_INT | CTLFLAG_RW | 3264c36e917SKonstantin Belousov CTLFLAG_MPSAFE, NULL, 0, sysctl_swap_async_max, "I", 3274c36e917SKonstantin Belousov "Maximum running async swap ops"); 328d027ed2eSAlan Cox static int sysctl_swap_fragmentation(SYSCTL_HANDLER_ARGS); 329d027ed2eSAlan Cox SYSCTL_PROC(_vm, OID_AUTO, swap_fragmentation, CTLTYPE_STRING | CTLFLAG_RD | 330d027ed2eSAlan Cox CTLFLAG_MPSAFE, NULL, 0, sysctl_swap_fragmentation, "A", 331d027ed2eSAlan Cox "Swap Fragmentation Info"); 33289c241d1SGleb Smirnoff 3330cddd8f0SMatthew Dillon static struct sx sw_alloc_sx; 334327f4e83SMatthew Dillon 3351c7c3c6aSMatthew Dillon /* 3361c7c3c6aSMatthew Dillon * "named" and "unnamed" anon region objects. Try to reduce the overhead 3371c7c3c6aSMatthew Dillon * of searching a named list by hashing it just a little. 3381c7c3c6aSMatthew Dillon */ 3391c7c3c6aSMatthew Dillon 3401c7c3c6aSMatthew Dillon #define NOBJLISTS 8 3411c7c3c6aSMatthew Dillon 3421c7c3c6aSMatthew Dillon #define NOBJLIST(handle) \ 343af647ddeSBruce Evans (&swap_pager_object_list[((int)(intptr_t)handle >> 4) & (NOBJLISTS-1)]) 3441c7c3c6aSMatthew Dillon 3451c7c3c6aSMatthew Dillon static struct pagerlst swap_pager_object_list[NOBJLISTS]; 346f425ab8eSKonstantin Belousov static uma_zone_t swblk_zone; 347f425ab8eSKonstantin Belousov static uma_zone_t swpctrie_zone; 3481c7c3c6aSMatthew Dillon 3491c7c3c6aSMatthew Dillon /* 3501c7c3c6aSMatthew Dillon * pagerops for OBJT_SWAP - "swap pager". Some ops are also global procedure 3511c7c3c6aSMatthew Dillon * calls hooked from other parts of the VM system and do not appear here. 3521c7c3c6aSMatthew Dillon * (see vm/swap_pager.h). 3531c7c3c6aSMatthew Dillon */ 354ff98689dSBruce Evans static vm_object_t 35511caded3SAlfred Perlstein swap_pager_alloc(void *handle, vm_ooffset_t size, 3563364c323SKonstantin Belousov vm_prot_t prot, vm_ooffset_t offset, struct ucred *); 35711caded3SAlfred Perlstein static void swap_pager_dealloc(vm_object_t object); 358b0cd2017SGleb Smirnoff static int swap_pager_getpages(vm_object_t, vm_page_t *, int, int *, 359b0cd2017SGleb Smirnoff int *); 360b0cd2017SGleb Smirnoff static int swap_pager_getpages_async(vm_object_t, vm_page_t *, int, int *, 361b0cd2017SGleb Smirnoff int *, pgo_getpages_iodone_t, void *); 362751221fdSPoul-Henning Kamp static void swap_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *); 3635ea4972cSAlan Cox static boolean_t 3645ea4972cSAlan Cox swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, int *after); 36511caded3SAlfred Perlstein static void swap_pager_init(void); 36611caded3SAlfred Perlstein static void swap_pager_unswapped(vm_page_t); 367b3fed13eSDavid Schultz static void swap_pager_swapoff(struct swdevt *sp); 368f708ef1bSPoul-Henning Kamp 369df8bae1dSRodney W. Grimes struct pagerops swappagerops = { 370e04e4bacSPoul-Henning Kamp .pgo_init = swap_pager_init, /* early system initialization of pager */ 371e04e4bacSPoul-Henning Kamp .pgo_alloc = swap_pager_alloc, /* allocate an OBJT_SWAP object */ 372e04e4bacSPoul-Henning Kamp .pgo_dealloc = swap_pager_dealloc, /* deallocate an OBJT_SWAP object */ 373e04e4bacSPoul-Henning Kamp .pgo_getpages = swap_pager_getpages, /* pagein */ 37490effb23SGleb Smirnoff .pgo_getpages_async = swap_pager_getpages_async, /* pagein (async) */ 375e04e4bacSPoul-Henning Kamp .pgo_putpages = swap_pager_putpages, /* pageout */ 376e04e4bacSPoul-Henning Kamp .pgo_haspage = swap_pager_haspage, /* get backing store status for page */ 377e04e4bacSPoul-Henning Kamp .pgo_pageunswapped = swap_pager_unswapped, /* remove swap related to page */ 378df8bae1dSRodney W. Grimes }; 379df8bae1dSRodney W. Grimes 3801c7c3c6aSMatthew Dillon /* 3811c7c3c6aSMatthew Dillon * swap_*() routines are externally accessible. swp_*() routines are 3821c7c3c6aSMatthew Dillon * internal. 3831c7c3c6aSMatthew Dillon */ 384e9c0cc15SPoul-Henning Kamp static int nswap_lowat = 128; /* in pages, swap_pager_almost_full warn */ 385e9c0cc15SPoul-Henning Kamp static int nswap_hiwat = 512; /* in pages, swap_pager_almost_full warn */ 38626f9a767SRodney W. Grimes 38707c348eaSAlan Cox SYSCTL_INT(_vm, OID_AUTO, dmmax, CTLFLAG_RD, &nsw_cluster_max, 0, 38807c348eaSAlan Cox "Maximum size of a swap block in pages"); 389cee313c4SRobert Watson 390a5edd34aSPoul-Henning Kamp static void swp_sizecheck(void); 39111caded3SAlfred Perlstein static void swp_pager_async_iodone(struct buf *bp); 392230869e0SAlan Cox static bool swp_pager_swblk_empty(struct swblk *sb, int start, int limit); 39388ad2d7bSKonstantin Belousov static int swapongeom(struct vnode *); 39459efee01SPoul-Henning Kamp static int swaponvp(struct thread *, struct vnode *, u_long); 39535918c55SChristian S.J. Peron static int swapoff_one(struct swdevt *sp, struct ucred *cred); 39624a1cce3SDavid Greenman 3971c7c3c6aSMatthew Dillon /* 3981c7c3c6aSMatthew Dillon * Swap bitmap functions 3991c7c3c6aSMatthew Dillon */ 400230869e0SAlan Cox static void swp_pager_freeswapspace(daddr_t blk, daddr_t npages); 401a5edd34aSPoul-Henning Kamp static daddr_t swp_pager_getswapspace(int npages); 4021c7c3c6aSMatthew Dillon 4031c7c3c6aSMatthew Dillon /* 4041c7c3c6aSMatthew Dillon * Metadata functions 4051c7c3c6aSMatthew Dillon */ 406*78f1deefSAlan Cox static daddr_t swp_pager_meta_build(vm_object_t, vm_pindex_t, daddr_t); 4072e56b64fSKonstantin Belousov static void swp_pager_meta_free(vm_object_t, vm_pindex_t, vm_pindex_t); 40811caded3SAlfred Perlstein static void swp_pager_meta_free_all(vm_object_t); 40911caded3SAlfred Perlstein static daddr_t swp_pager_meta_ctl(vm_object_t, vm_pindex_t, int); 4101c7c3c6aSMatthew Dillon 411*78f1deefSAlan Cox static void 412*78f1deefSAlan Cox swp_pager_init_freerange(daddr_t *start, daddr_t *num) 413*78f1deefSAlan Cox { 414*78f1deefSAlan Cox 415*78f1deefSAlan Cox *start = SWAPBLK_NONE; 416*78f1deefSAlan Cox *num = 0; 417*78f1deefSAlan Cox } 418*78f1deefSAlan Cox 419*78f1deefSAlan Cox static void 420*78f1deefSAlan Cox swp_pager_update_freerange(daddr_t *start, daddr_t *num, daddr_t addr) 421*78f1deefSAlan Cox { 422*78f1deefSAlan Cox 423*78f1deefSAlan Cox if (*start + *num == addr) { 424*78f1deefSAlan Cox (*num)++; 425*78f1deefSAlan Cox } else { 426*78f1deefSAlan Cox swp_pager_freeswapspace(*start, *num); 427*78f1deefSAlan Cox *start = addr; 428*78f1deefSAlan Cox *num = 1; 429*78f1deefSAlan Cox } 430*78f1deefSAlan Cox } 431*78f1deefSAlan Cox 432f425ab8eSKonstantin Belousov static void * 433f425ab8eSKonstantin Belousov swblk_trie_alloc(struct pctrie *ptree) 434f425ab8eSKonstantin Belousov { 435f425ab8eSKonstantin Belousov 436f425ab8eSKonstantin Belousov return (uma_zalloc(swpctrie_zone, M_NOWAIT | (curproc == pageproc ? 437f425ab8eSKonstantin Belousov M_USE_RESERVE : 0))); 438f425ab8eSKonstantin Belousov } 439f425ab8eSKonstantin Belousov 440f425ab8eSKonstantin Belousov static void 441f425ab8eSKonstantin Belousov swblk_trie_free(struct pctrie *ptree, void *node) 442f425ab8eSKonstantin Belousov { 443f425ab8eSKonstantin Belousov 444f425ab8eSKonstantin Belousov uma_zfree(swpctrie_zone, node); 445f425ab8eSKonstantin Belousov } 446f425ab8eSKonstantin Belousov 447f425ab8eSKonstantin Belousov PCTRIE_DEFINE(SWAP, swblk, p, swblk_trie_alloc, swblk_trie_free); 448f425ab8eSKonstantin Belousov 4491c7c3c6aSMatthew Dillon /* 4501c7c3c6aSMatthew Dillon * SWP_SIZECHECK() - update swap_pager_full indication 4511c7c3c6aSMatthew Dillon * 45220d3034fSMatthew Dillon * update the swap_pager_almost_full indication and warn when we are 45320d3034fSMatthew Dillon * about to run out of swap space, using lowat/hiwat hysteresis. 45420d3034fSMatthew Dillon * 45520d3034fSMatthew Dillon * Clear swap_pager_full ( task killing ) indication when lowat is met. 4561c7c3c6aSMatthew Dillon * 4571c7c3c6aSMatthew Dillon * No restrictions on call 4581c7c3c6aSMatthew Dillon * This routine may not block. 4591c7c3c6aSMatthew Dillon */ 460a5edd34aSPoul-Henning Kamp static void 4612f249180SPoul-Henning Kamp swp_sizecheck(void) 4620d94caffSDavid Greenman { 46323955314SAlfred Perlstein 4648f60c087SPoul-Henning Kamp if (swap_pager_avail < nswap_lowat) { 46520d3034fSMatthew Dillon if (swap_pager_almost_full == 0) { 4661af87c92SDavid Greenman printf("swap_pager: out of swap space\n"); 46720d3034fSMatthew Dillon swap_pager_almost_full = 1; 4682b0d37a4SMatthew Dillon } 46920d3034fSMatthew Dillon } else { 47026f9a767SRodney W. Grimes swap_pager_full = 0; 4718f60c087SPoul-Henning Kamp if (swap_pager_avail > nswap_hiwat) 47220d3034fSMatthew Dillon swap_pager_almost_full = 0; 47326f9a767SRodney W. Grimes } 4741c7c3c6aSMatthew Dillon } 4751c7c3c6aSMatthew Dillon 4761c7c3c6aSMatthew Dillon /* 4771c7c3c6aSMatthew Dillon * SWAP_PAGER_INIT() - initialize the swap pager! 4781c7c3c6aSMatthew Dillon * 4791c7c3c6aSMatthew Dillon * Expected to be started from system init. NOTE: This code is run 4801c7c3c6aSMatthew Dillon * before much else so be careful what you depend on. Most of the VM 4811c7c3c6aSMatthew Dillon * system has yet to be initialized at this point. 4821c7c3c6aSMatthew Dillon */ 483f5a12711SPoul-Henning Kamp static void 4842f249180SPoul-Henning Kamp swap_pager_init(void) 485df8bae1dSRodney W. Grimes { 4861c7c3c6aSMatthew Dillon /* 4871c7c3c6aSMatthew Dillon * Initialize object lists 4881c7c3c6aSMatthew Dillon */ 4891c7c3c6aSMatthew Dillon int i; 4901c7c3c6aSMatthew Dillon 4911c7c3c6aSMatthew Dillon for (i = 0; i < NOBJLISTS; ++i) 4921c7c3c6aSMatthew Dillon TAILQ_INIT(&swap_pager_object_list[i]); 49320da9c2eSPoul-Henning Kamp mtx_init(&sw_dev_mtx, "swapdev", NULL, MTX_DEF); 49415719273SKonstantin Belousov sx_init(&sw_alloc_sx, "swspsx"); 49504533e1eSKonstantin Belousov sx_init(&swdev_syscall_lock, "swsysc"); 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 /* 541f425ab8eSKonstantin Belousov * Initialize our zone, guessing on the number we need based 542f425ab8eSKonstantin Belousov * on the number of pages in the system. 5431c7c3c6aSMatthew Dillon */ 54444f1c916SBryan Drewery n = vm_cnt.v_page_count / 2; 545f425ab8eSKonstantin Belousov if (maxswzone && n > maxswzone / sizeof(struct swblk)) 546f425ab8eSKonstantin Belousov n = maxswzone / sizeof(struct swblk); 547f425ab8eSKonstantin Belousov swpctrie_zone = uma_zcreate("swpctrie", pctrie_node_size(), NULL, NULL, 548f425ab8eSKonstantin Belousov pctrie_zone_init, NULL, UMA_ALIGN_PTR, 549f425ab8eSKonstantin Belousov UMA_ZONE_NOFREE | UMA_ZONE_VM); 550f425ab8eSKonstantin Belousov if (swpctrie_zone == NULL) 551f425ab8eSKonstantin Belousov panic("failed to create swap pctrie zone."); 552f425ab8eSKonstantin Belousov swblk_zone = uma_zcreate("swblk", sizeof(struct swblk), NULL, NULL, 553f425ab8eSKonstantin Belousov NULL, NULL, _Alignof(struct swblk) - 1, 554f425ab8eSKonstantin Belousov UMA_ZONE_NOFREE | UMA_ZONE_VM); 555f425ab8eSKonstantin Belousov if (swblk_zone == NULL) 556f425ab8eSKonstantin Belousov panic("failed to create swap blk zone."); 55722a5e6b9SDag-Erling Smørgrav n2 = n; 5588355f576SJeff Roberson do { 559f425ab8eSKonstantin Belousov if (uma_zone_reserve_kva(swblk_zone, n)) 56061ce6eeeSAlfred Perlstein break; 56161ce6eeeSAlfred Perlstein /* 56261ce6eeeSAlfred Perlstein * if the allocation failed, try a zone two thirds the 56361ce6eeeSAlfred Perlstein * size of the previous attempt. 56461ce6eeeSAlfred Perlstein */ 56561ce6eeeSAlfred Perlstein n -= ((n + 2) / 3); 56661ce6eeeSAlfred Perlstein } while (n > 0); 56753faf5a7SKonstantin Belousov 56853faf5a7SKonstantin Belousov /* 56953faf5a7SKonstantin Belousov * Often uma_zone_reserve_kva() cannot reserve exactly the 57053faf5a7SKonstantin Belousov * requested size. Account for the difference when 57153faf5a7SKonstantin Belousov * calculating swap_maxpages. 57253faf5a7SKonstantin Belousov */ 57353faf5a7SKonstantin Belousov n = uma_zone_get_max(swblk_zone); 57453faf5a7SKonstantin Belousov 5751fffcd75SKonstantin Belousov if (n < n2) 576f425ab8eSKonstantin Belousov printf("Swap blk zone entries reduced from %lu to %lu.\n", 577f425ab8eSKonstantin Belousov n2, n); 57861203277SDag-Erling Smørgrav swap_maxpages = n * SWAP_META_PAGES; 579f425ab8eSKonstantin Belousov swzone = n * sizeof(struct swblk); 580f425ab8eSKonstantin Belousov if (!uma_zone_reserve_kva(swpctrie_zone, n)) 581f425ab8eSKonstantin Belousov printf("Cannot reserve swap pctrie zone, " 582f425ab8eSKonstantin Belousov "reduce kern.maxswzone.\n"); 58324a1cce3SDavid Greenman } 58424a1cce3SDavid Greenman 585eb4d6a1bSKonstantin Belousov static vm_object_t 586eb4d6a1bSKonstantin Belousov swap_pager_alloc_init(void *handle, struct ucred *cred, vm_ooffset_t size, 587eb4d6a1bSKonstantin Belousov vm_ooffset_t offset) 588eb4d6a1bSKonstantin Belousov { 589eb4d6a1bSKonstantin Belousov vm_object_t object; 590eb4d6a1bSKonstantin Belousov 591eb4d6a1bSKonstantin Belousov if (cred != NULL) { 592eb4d6a1bSKonstantin Belousov if (!swap_reserve_by_cred(size, cred)) 593eb4d6a1bSKonstantin Belousov return (NULL); 594eb4d6a1bSKonstantin Belousov crhold(cred); 595eb4d6a1bSKonstantin Belousov } 596f425ab8eSKonstantin Belousov 597f425ab8eSKonstantin Belousov /* 598f425ab8eSKonstantin Belousov * The un_pager.swp.swp_blks trie is initialized by 599f425ab8eSKonstantin Belousov * vm_object_allocate() to ensure the correct order of 600f425ab8eSKonstantin Belousov * visibility to other threads. 601f425ab8eSKonstantin Belousov */ 602eb4d6a1bSKonstantin Belousov object = vm_object_allocate(OBJT_SWAP, OFF_TO_IDX(offset + 603eb4d6a1bSKonstantin Belousov PAGE_MASK + size)); 604f425ab8eSKonstantin Belousov 605eb4d6a1bSKonstantin Belousov object->handle = handle; 606eb4d6a1bSKonstantin Belousov if (cred != NULL) { 607eb4d6a1bSKonstantin Belousov object->cred = cred; 608eb4d6a1bSKonstantin Belousov object->charge = size; 609eb4d6a1bSKonstantin Belousov } 610eb4d6a1bSKonstantin Belousov return (object); 611eb4d6a1bSKonstantin Belousov } 612eb4d6a1bSKonstantin Belousov 61324a1cce3SDavid Greenman /* 6141c7c3c6aSMatthew Dillon * SWAP_PAGER_ALLOC() - allocate a new OBJT_SWAP VM object and instantiate 6151c7c3c6aSMatthew Dillon * its metadata structures. 6161c7c3c6aSMatthew Dillon * 6171c7c3c6aSMatthew Dillon * This routine is called from the mmap and fork code to create a new 618eb4d6a1bSKonstantin Belousov * OBJT_SWAP object. 6191c7c3c6aSMatthew Dillon * 620eb4d6a1bSKonstantin Belousov * This routine must ensure that no live duplicate is created for 621eb4d6a1bSKonstantin Belousov * the named object request, which is protected against by 622eb4d6a1bSKonstantin Belousov * holding the sw_alloc_sx lock in case handle != NULL. 62324a1cce3SDavid Greenman */ 624f5a12711SPoul-Henning Kamp static vm_object_t 6256cde7a16SDavid Greenman swap_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 6263364c323SKonstantin Belousov vm_ooffset_t offset, struct ucred *cred) 62724a1cce3SDavid Greenman { 62824a1cce3SDavid Greenman vm_object_t object; 6292f7af3dbSAlan Cox 630eb4d6a1bSKonstantin Belousov if (handle != NULL) { 6311c7c3c6aSMatthew Dillon /* 6321c7c3c6aSMatthew Dillon * Reference existing named region or allocate new one. There 6331c7c3c6aSMatthew Dillon * should not be a race here against swp_pager_meta_build() 6341c7c3c6aSMatthew Dillon * as called from vm_page_remove() in regards to the lookup 6351c7c3c6aSMatthew Dillon * of the handle. 6361c7c3c6aSMatthew Dillon */ 6370cddd8f0SMatthew Dillon sx_xlock(&sw_alloc_sx); 6381c7c3c6aSMatthew Dillon object = vm_pager_object_lookup(NOBJLIST(handle), handle); 639b5e8f167SAlan Cox if (object == NULL) { 640eb4d6a1bSKonstantin Belousov object = swap_pager_alloc_init(handle, cred, size, 641eb4d6a1bSKonstantin Belousov offset); 642eb4d6a1bSKonstantin Belousov if (object != NULL) { 643eb4d6a1bSKonstantin Belousov TAILQ_INSERT_TAIL(NOBJLIST(object->handle), 644eb4d6a1bSKonstantin Belousov object, pager_object_list); 6453364c323SKonstantin Belousov } 64624a1cce3SDavid Greenman } 6470cddd8f0SMatthew Dillon sx_xunlock(&sw_alloc_sx); 64824a1cce3SDavid Greenman } else { 649eb4d6a1bSKonstantin Belousov object = swap_pager_alloc_init(handle, cred, size, offset); 65024a1cce3SDavid Greenman } 65124a1cce3SDavid Greenman return (object); 652df8bae1dSRodney W. Grimes } 653df8bae1dSRodney W. Grimes 65426f9a767SRodney W. Grimes /* 6551c7c3c6aSMatthew Dillon * SWAP_PAGER_DEALLOC() - remove swap metadata from object 6561c7c3c6aSMatthew Dillon * 6571c7c3c6aSMatthew Dillon * The swap backing for the object is destroyed. The code is 6581c7c3c6aSMatthew Dillon * designed such that we can reinstantiate it later, but this 6591c7c3c6aSMatthew Dillon * routine is typically called only when the entire object is 6601c7c3c6aSMatthew Dillon * about to be destroyed. 6611c7c3c6aSMatthew Dillon * 66215523cf7SKonstantin Belousov * The object must be locked. 66326f9a767SRodney W. Grimes */ 664df8bae1dSRodney W. Grimes static void 6652f249180SPoul-Henning Kamp swap_pager_dealloc(vm_object_t object) 66626f9a767SRodney W. Grimes { 6674dcc5c2dSMatthew Dillon 668eb4d6a1bSKonstantin Belousov VM_OBJECT_ASSERT_WLOCKED(object); 669eb4d6a1bSKonstantin Belousov KASSERT((object->flags & OBJ_DEAD) != 0, ("dealloc of reachable obj")); 670eb4d6a1bSKonstantin Belousov 67126f9a767SRodney W. Grimes /* 6721c7c3c6aSMatthew Dillon * Remove from list right away so lookups will fail if we block for 6731c7c3c6aSMatthew Dillon * pageout completion. 67426f9a767SRodney W. Grimes */ 675bd228075SAlan Cox if (object->handle != NULL) { 676eb4d6a1bSKonstantin Belousov VM_OBJECT_WUNLOCK(object); 677eb4d6a1bSKonstantin Belousov sx_xlock(&sw_alloc_sx); 678eb4d6a1bSKonstantin Belousov TAILQ_REMOVE(NOBJLIST(object->handle), object, 679eb4d6a1bSKonstantin Belousov pager_object_list); 680eb4d6a1bSKonstantin Belousov sx_xunlock(&sw_alloc_sx); 681eb4d6a1bSKonstantin Belousov VM_OBJECT_WLOCK(object); 682bd228075SAlan Cox } 6831c7c3c6aSMatthew Dillon 6841c7c3c6aSMatthew Dillon vm_object_pip_wait(object, "swpdea"); 6851c7c3c6aSMatthew Dillon 6861c7c3c6aSMatthew Dillon /* 6871c7c3c6aSMatthew Dillon * Free all remaining metadata. We only bother to free it from 6881c7c3c6aSMatthew Dillon * the swap meta data. We do not attempt to free swapblk's still 6891c7c3c6aSMatthew Dillon * associated with vm_page_t's for this object. We do not care 6901c7c3c6aSMatthew Dillon * if paging is still in progress on some objects. 6911c7c3c6aSMatthew Dillon */ 6921c7c3c6aSMatthew Dillon swp_pager_meta_free_all(object); 693e735691bSJohn Baldwin object->handle = NULL; 694e735691bSJohn Baldwin object->type = OBJT_DEAD; 6951c7c3c6aSMatthew Dillon } 6961c7c3c6aSMatthew Dillon 6971c7c3c6aSMatthew Dillon /************************************************************************ 6981c7c3c6aSMatthew Dillon * SWAP PAGER BITMAP ROUTINES * 6991c7c3c6aSMatthew Dillon ************************************************************************/ 7001c7c3c6aSMatthew Dillon 7011c7c3c6aSMatthew Dillon /* 7021c7c3c6aSMatthew Dillon * SWP_PAGER_GETSWAPSPACE() - allocate raw swap space 7031c7c3c6aSMatthew Dillon * 7041c7c3c6aSMatthew Dillon * Allocate swap for the requested number of pages. The starting 7051c7c3c6aSMatthew Dillon * swap block number (a page index) is returned or SWAPBLK_NONE 7061c7c3c6aSMatthew Dillon * if the allocation failed. 7071c7c3c6aSMatthew Dillon * 7081c7c3c6aSMatthew Dillon * Also has the side effect of advising that somebody made a mistake 7091c7c3c6aSMatthew Dillon * when they configured swap and didn't configure enough. 7101c7c3c6aSMatthew Dillon * 71115523cf7SKonstantin Belousov * This routine may not sleep. 7128f60c087SPoul-Henning Kamp * 7138f60c087SPoul-Henning Kamp * We allocate in round-robin fashion from the configured devices. 7141c7c3c6aSMatthew Dillon */ 715a5edd34aSPoul-Henning Kamp static daddr_t 7162f249180SPoul-Henning Kamp swp_pager_getswapspace(int npages) 7171c7c3c6aSMatthew Dillon { 7181c7c3c6aSMatthew Dillon daddr_t blk; 7198f60c087SPoul-Henning Kamp struct swdevt *sp; 7208f60c087SPoul-Henning Kamp int i; 7211c7c3c6aSMatthew Dillon 7228f60c087SPoul-Henning Kamp blk = SWAPBLK_NONE; 72320da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 7248f60c087SPoul-Henning Kamp sp = swdevhd; 7258f60c087SPoul-Henning Kamp for (i = 0; i < nswapdev; i++) { 7268f60c087SPoul-Henning Kamp if (sp == NULL) 7278f60c087SPoul-Henning Kamp sp = TAILQ_FIRST(&swtailq); 7288f60c087SPoul-Henning Kamp if (!(sp->sw_flags & SW_CLOSING)) { 7298f60c087SPoul-Henning Kamp blk = blist_alloc(sp->sw_blist, npages); 7308f60c087SPoul-Henning Kamp if (blk != SWAPBLK_NONE) { 7318f60c087SPoul-Henning Kamp blk += sp->sw_first; 7328f60c087SPoul-Henning Kamp sp->sw_used += npages; 733d05bc129SAlan Cox swap_pager_avail -= npages; 7348f60c087SPoul-Henning Kamp swp_sizecheck(); 7358f60c087SPoul-Henning Kamp swdevhd = TAILQ_NEXT(sp, sw_list); 736d05bc129SAlan Cox goto done; 7378f60c087SPoul-Henning Kamp } 7388f60c087SPoul-Henning Kamp } 7398f60c087SPoul-Henning Kamp sp = TAILQ_NEXT(sp, sw_list); 7408f60c087SPoul-Henning Kamp } 7412b0d37a4SMatthew Dillon if (swap_pager_full != 2) { 74220da9c2eSPoul-Henning Kamp printf("swap_pager_getswapspace(%d): failed\n", npages); 7432b0d37a4SMatthew Dillon swap_pager_full = 2; 74420d3034fSMatthew Dillon swap_pager_almost_full = 1; 7452b0d37a4SMatthew Dillon } 7468f60c087SPoul-Henning Kamp swdevhd = NULL; 747d05bc129SAlan Cox done: 748d05bc129SAlan Cox mtx_unlock(&sw_dev_mtx); 7491c7c3c6aSMatthew Dillon return (blk); 75026f9a767SRodney W. Grimes } 75126f9a767SRodney W. Grimes 752b3fed13eSDavid Schultz static int 753b3fed13eSDavid Schultz swp_pager_isondev(daddr_t blk, struct swdevt *sp) 7548f60c087SPoul-Henning Kamp { 7558f60c087SPoul-Henning Kamp 756b3fed13eSDavid Schultz return (blk >= sp->sw_first && blk < sp->sw_end); 7578f60c087SPoul-Henning Kamp } 7588f60c087SPoul-Henning Kamp 7594b03903aSPoul-Henning Kamp static void 7604b03903aSPoul-Henning Kamp swp_pager_strategy(struct buf *bp) 7614b03903aSPoul-Henning Kamp { 7624b03903aSPoul-Henning Kamp struct swdevt *sp; 7634b03903aSPoul-Henning Kamp 76420da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 7654b03903aSPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 7664b03903aSPoul-Henning Kamp if (bp->b_blkno >= sp->sw_first && bp->b_blkno < sp->sw_end) { 76720da9c2eSPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 7682cc718a1SKonstantin Belousov if ((sp->sw_flags & SW_UNMAPPED) != 0 && 7692cc718a1SKonstantin Belousov unmapped_buf_allowed) { 7702cc718a1SKonstantin Belousov bp->b_data = unmapped_buf; 7712cc718a1SKonstantin Belousov bp->b_offset = 0; 7722cc718a1SKonstantin Belousov } else { 7732cc718a1SKonstantin Belousov pmap_qenter((vm_offset_t)bp->b_data, 7742cc718a1SKonstantin Belousov &bp->b_pages[0], bp->b_bcount / PAGE_SIZE); 7752cc718a1SKonstantin Belousov } 7764b03903aSPoul-Henning Kamp sp->sw_strategy(bp, sp); 7774b03903aSPoul-Henning Kamp return; 7784b03903aSPoul-Henning Kamp } 7794b03903aSPoul-Henning Kamp } 78020da9c2eSPoul-Henning Kamp panic("Swapdev not found"); 7814b03903aSPoul-Henning Kamp } 7824b03903aSPoul-Henning Kamp 7838f60c087SPoul-Henning Kamp 78426f9a767SRodney W. Grimes /* 7851c7c3c6aSMatthew Dillon * SWP_PAGER_FREESWAPSPACE() - free raw swap space 7861c7c3c6aSMatthew Dillon * 7871c7c3c6aSMatthew Dillon * This routine returns the specified swap blocks back to the bitmap. 7881c7c3c6aSMatthew Dillon * 78915523cf7SKonstantin Belousov * This routine may not sleep. 79026f9a767SRodney W. Grimes */ 791a5edd34aSPoul-Henning Kamp static void 792230869e0SAlan Cox swp_pager_freeswapspace(daddr_t blk, daddr_t npages) 7930d94caffSDavid Greenman { 7948f60c087SPoul-Henning Kamp struct swdevt *sp; 79592da00bbSMatthew Dillon 796230869e0SAlan Cox if (npages == 0) 797230869e0SAlan Cox return; 7987645e885SAlan Cox mtx_lock(&sw_dev_mtx); 7997645e885SAlan Cox TAILQ_FOREACH(sp, &swtailq, sw_list) { 8007645e885SAlan Cox if (blk >= sp->sw_first && blk < sp->sw_end) { 80192da00bbSMatthew Dillon sp->sw_used -= npages; 80292da00bbSMatthew Dillon /* 8037645e885SAlan Cox * If we are attempting to stop swapping on 8047645e885SAlan Cox * this device, we don't want to mark any 8057645e885SAlan Cox * blocks free lest they be reused. 80692da00bbSMatthew Dillon */ 8077645e885SAlan Cox if ((sp->sw_flags & SW_CLOSING) == 0) { 8087645e885SAlan Cox blist_free(sp->sw_blist, blk - sp->sw_first, 8097645e885SAlan Cox npages); 8108f60c087SPoul-Henning Kamp swap_pager_avail += npages; 8111c7c3c6aSMatthew Dillon swp_sizecheck(); 81226f9a767SRodney W. Grimes } 8137645e885SAlan Cox mtx_unlock(&sw_dev_mtx); 8147645e885SAlan Cox return; 8157645e885SAlan Cox } 8167645e885SAlan Cox } 8177645e885SAlan Cox panic("Swapdev not found"); 8187645e885SAlan Cox } 8191c7c3c6aSMatthew Dillon 82026f9a767SRodney W. Grimes /* 821d027ed2eSAlan Cox * SYSCTL_SWAP_FRAGMENTATION() - produce raw swap space stats 822d027ed2eSAlan Cox */ 823d027ed2eSAlan Cox static int 824d027ed2eSAlan Cox sysctl_swap_fragmentation(SYSCTL_HANDLER_ARGS) 825d027ed2eSAlan Cox { 826d027ed2eSAlan Cox struct sbuf sbuf; 827d027ed2eSAlan Cox struct swdevt *sp; 828d027ed2eSAlan Cox const char *devname; 829d027ed2eSAlan Cox int error; 830d027ed2eSAlan Cox 831d027ed2eSAlan Cox error = sysctl_wire_old_buffer(req, 0); 832d027ed2eSAlan Cox if (error != 0) 833d027ed2eSAlan Cox return (error); 834d027ed2eSAlan Cox sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 835d027ed2eSAlan Cox mtx_lock(&sw_dev_mtx); 836d027ed2eSAlan Cox TAILQ_FOREACH(sp, &swtailq, sw_list) { 837d027ed2eSAlan Cox if (vn_isdisk(sp->sw_vp, NULL)) 838d027ed2eSAlan Cox devname = devtoname(sp->sw_vp->v_rdev); 839d027ed2eSAlan Cox else 840d027ed2eSAlan Cox devname = "[file]"; 841d027ed2eSAlan Cox sbuf_printf(&sbuf, "\nFree space on device %s:\n", devname); 842d027ed2eSAlan Cox blist_stats(sp->sw_blist, &sbuf); 843d027ed2eSAlan Cox } 844d027ed2eSAlan Cox mtx_unlock(&sw_dev_mtx); 845d027ed2eSAlan Cox error = sbuf_finish(&sbuf); 846d027ed2eSAlan Cox sbuf_delete(&sbuf); 847d027ed2eSAlan Cox return (error); 848d027ed2eSAlan Cox } 849d027ed2eSAlan Cox 850d027ed2eSAlan Cox /* 8511c7c3c6aSMatthew Dillon * SWAP_PAGER_FREESPACE() - frees swap blocks associated with a page 8521c7c3c6aSMatthew Dillon * range within an object. 8531c7c3c6aSMatthew Dillon * 8541c7c3c6aSMatthew Dillon * This is a globally accessible routine. 8551c7c3c6aSMatthew Dillon * 8561c7c3c6aSMatthew Dillon * This routine removes swapblk assignments from swap metadata. 8571c7c3c6aSMatthew Dillon * 8581c7c3c6aSMatthew Dillon * The external callers of this routine typically have already destroyed 8591c7c3c6aSMatthew Dillon * or renamed vm_page_t's associated with this range in the object so 8601c7c3c6aSMatthew Dillon * we should be ok. 861c25673ffSAttilio Rao * 862c25673ffSAttilio Rao * The object must be locked. 86326f9a767SRodney W. Grimes */ 86426f9a767SRodney W. Grimes void 8652f249180SPoul-Henning Kamp swap_pager_freespace(vm_object_t object, vm_pindex_t start, vm_size_t size) 86626f9a767SRodney W. Grimes { 86723955314SAlfred Perlstein 8681c7c3c6aSMatthew Dillon swp_pager_meta_free(object, start, size); 8694dcc5c2dSMatthew Dillon } 8704dcc5c2dSMatthew Dillon 8714dcc5c2dSMatthew Dillon /* 8724dcc5c2dSMatthew Dillon * SWAP_PAGER_RESERVE() - reserve swap blocks in object 8734dcc5c2dSMatthew Dillon * 8744dcc5c2dSMatthew Dillon * Assigns swap blocks to the specified range within the object. The 87556ce850bSKonstantin Belousov * swap blocks are not zeroed. Any previous swap assignment is destroyed. 8764dcc5c2dSMatthew Dillon * 8774dcc5c2dSMatthew Dillon * Returns 0 on success, -1 on failure. 8784dcc5c2dSMatthew Dillon */ 8794dcc5c2dSMatthew Dillon int 8804dcc5c2dSMatthew Dillon swap_pager_reserve(vm_object_t object, vm_pindex_t start, vm_size_t size) 8814dcc5c2dSMatthew Dillon { 8824dcc5c2dSMatthew Dillon int n = 0; 8834dcc5c2dSMatthew Dillon daddr_t blk = SWAPBLK_NONE; 8844dcc5c2dSMatthew Dillon vm_pindex_t beg = start; /* save start index */ 885*78f1deefSAlan Cox daddr_t addr, n_free, s_free; 8864dcc5c2dSMatthew Dillon 887*78f1deefSAlan Cox swp_pager_init_freerange(&s_free, &n_free); 88889f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 8894dcc5c2dSMatthew Dillon while (size) { 8904dcc5c2dSMatthew Dillon if (n == 0) { 8914dcc5c2dSMatthew Dillon n = BLIST_MAX_ALLOC; 8924dcc5c2dSMatthew Dillon while ((blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE) { 8934dcc5c2dSMatthew Dillon n >>= 1; 8944dcc5c2dSMatthew Dillon if (n == 0) { 8954dcc5c2dSMatthew Dillon swp_pager_meta_free(object, beg, start - beg); 89689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 8974dcc5c2dSMatthew Dillon return (-1); 8984dcc5c2dSMatthew Dillon } 8994dcc5c2dSMatthew Dillon } 9004dcc5c2dSMatthew Dillon } 901*78f1deefSAlan Cox addr = swp_pager_meta_build(object, start, blk); 902*78f1deefSAlan Cox if (addr != SWAPBLK_NONE) 903*78f1deefSAlan Cox swp_pager_update_freerange(&s_free, &n_free, addr); 9044dcc5c2dSMatthew Dillon --size; 9054dcc5c2dSMatthew Dillon ++start; 9064dcc5c2dSMatthew Dillon ++blk; 9074dcc5c2dSMatthew Dillon --n; 9084dcc5c2dSMatthew Dillon } 909*78f1deefSAlan Cox swp_pager_freeswapspace(s_free, n_free); 9104dcc5c2dSMatthew Dillon swp_pager_meta_free(object, start, n); 91189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 9124dcc5c2dSMatthew Dillon return (0); 91326f9a767SRodney W. Grimes } 91426f9a767SRodney W. Grimes 9150a47b48bSJohn Dyson /* 9161c7c3c6aSMatthew Dillon * SWAP_PAGER_COPY() - copy blocks from source pager to destination pager 9171c7c3c6aSMatthew Dillon * and destroy the source. 9181c7c3c6aSMatthew Dillon * 9191c7c3c6aSMatthew Dillon * Copy any valid swapblks from the source to the destination. In 9201c7c3c6aSMatthew Dillon * cases where both the source and destination have a valid swapblk, 9211c7c3c6aSMatthew Dillon * we keep the destination's. 9221c7c3c6aSMatthew Dillon * 92315523cf7SKonstantin Belousov * This routine is allowed to sleep. It may sleep allocating metadata 9241c7c3c6aSMatthew Dillon * indirectly through swp_pager_meta_build() or if paging is still in 9251c7c3c6aSMatthew Dillon * progress on the source. 9261c7c3c6aSMatthew Dillon * 9271c7c3c6aSMatthew Dillon * The source object contains no vm_page_t's (which is just as well) 9281c7c3c6aSMatthew Dillon * 9291c7c3c6aSMatthew Dillon * The source object is of type OBJT_SWAP. 9301c7c3c6aSMatthew Dillon * 93115523cf7SKonstantin Belousov * The source and destination objects must be locked. 93215523cf7SKonstantin Belousov * Both object locks may temporarily be released. 93326f9a767SRodney W. Grimes */ 93426f9a767SRodney W. Grimes void 9352f249180SPoul-Henning Kamp swap_pager_copy(vm_object_t srcobject, vm_object_t dstobject, 9362f249180SPoul-Henning Kamp vm_pindex_t offset, int destroysource) 93726f9a767SRodney W. Grimes { 938a316d390SJohn Dyson vm_pindex_t i; 939*78f1deefSAlan Cox daddr_t dstaddr, n_free, s_free, srcaddr; 9404dcc5c2dSMatthew Dillon 94189f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(srcobject); 94289f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(dstobject); 9430cddd8f0SMatthew Dillon 94426f9a767SRodney W. Grimes /* 9451c7c3c6aSMatthew Dillon * If destroysource is set, we remove the source object from the 9461c7c3c6aSMatthew Dillon * swap_pager internal queue now. 94726f9a767SRodney W. Grimes */ 948eb4d6a1bSKonstantin Belousov if (destroysource && srcobject->handle != NULL) { 949eb4d6a1bSKonstantin Belousov vm_object_pip_add(srcobject, 1); 950eb4d6a1bSKonstantin Belousov VM_OBJECT_WUNLOCK(srcobject); 951eb4d6a1bSKonstantin Belousov vm_object_pip_add(dstobject, 1); 952eb4d6a1bSKonstantin Belousov VM_OBJECT_WUNLOCK(dstobject); 953eb4d6a1bSKonstantin Belousov sx_xlock(&sw_alloc_sx); 954eb4d6a1bSKonstantin Belousov TAILQ_REMOVE(NOBJLIST(srcobject->handle), srcobject, 955eb4d6a1bSKonstantin Belousov pager_object_list); 956eb4d6a1bSKonstantin Belousov sx_xunlock(&sw_alloc_sx); 957eb4d6a1bSKonstantin Belousov VM_OBJECT_WLOCK(dstobject); 958eb4d6a1bSKonstantin Belousov vm_object_pip_wakeup(dstobject); 959eb4d6a1bSKonstantin Belousov VM_OBJECT_WLOCK(srcobject); 960eb4d6a1bSKonstantin Belousov vm_object_pip_wakeup(srcobject); 961bd228075SAlan Cox } 96226f9a767SRodney W. Grimes 9631c7c3c6aSMatthew Dillon /* 9644abca9bbSAlan Cox * Transfer source to destination. 9651c7c3c6aSMatthew Dillon */ 966*78f1deefSAlan Cox swp_pager_init_freerange(&s_free, &n_free); 9671c7c3c6aSMatthew Dillon for (i = 0; i < dstobject->size; ++i) { 9684abca9bbSAlan Cox srcaddr = swp_pager_meta_ctl(srcobject, i + offset, SWM_POP); 9694abca9bbSAlan Cox if (srcaddr == SWAPBLK_NONE) 9704abca9bbSAlan Cox continue; 9711c7c3c6aSMatthew Dillon dstaddr = swp_pager_meta_ctl(dstobject, i, 0); 972*78f1deefSAlan Cox if (dstaddr != SWAPBLK_NONE) { 973*78f1deefSAlan Cox /* 974*78f1deefSAlan Cox * Destination has valid swapblk or it is represented 975*78f1deefSAlan Cox * by a resident page. We destroy the source block. 976*78f1deefSAlan Cox */ 977*78f1deefSAlan Cox swp_pager_update_freerange(&s_free, &n_free, srcaddr); 978*78f1deefSAlan Cox continue; 979*78f1deefSAlan Cox } 980*78f1deefSAlan Cox 9811c7c3c6aSMatthew Dillon /* 9821c7c3c6aSMatthew Dillon * Destination has no swapblk and is not resident, 9831c7c3c6aSMatthew Dillon * copy source. 9844abca9bbSAlan Cox * 985c7c8dd7eSAlan Cox * swp_pager_meta_build() can sleep. 986c7c8dd7eSAlan Cox */ 987c7c8dd7eSAlan Cox vm_object_pip_add(srcobject, 1); 98889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(srcobject); 989c7c8dd7eSAlan Cox vm_object_pip_add(dstobject, 1); 990*78f1deefSAlan Cox dstaddr = swp_pager_meta_build(dstobject, i, srcaddr); 991*78f1deefSAlan Cox KASSERT(dstaddr == SWAPBLK_NONE, 992*78f1deefSAlan Cox ("Unexpected destination swapblk")); 993c7c8dd7eSAlan Cox vm_object_pip_wakeup(dstobject); 99489f6b863SAttilio Rao VM_OBJECT_WLOCK(srcobject); 995c7c8dd7eSAlan Cox vm_object_pip_wakeup(srcobject); 9961c7c3c6aSMatthew Dillon } 997*78f1deefSAlan Cox swp_pager_freeswapspace(s_free, n_free); 99826f9a767SRodney W. Grimes 99926f9a767SRodney W. Grimes /* 10001c7c3c6aSMatthew Dillon * Free left over swap blocks in source. 10011c7c3c6aSMatthew Dillon * 1002763df3ecSPedro F. Giffuni * We have to revert the type to OBJT_DEFAULT so we do not accidentally 10031c7c3c6aSMatthew Dillon * double-remove the object from the swap queues. 100426f9a767SRodney W. Grimes */ 1005c0877f10SJohn Dyson if (destroysource) { 10061c7c3c6aSMatthew Dillon swp_pager_meta_free_all(srcobject); 10071c7c3c6aSMatthew Dillon /* 10081c7c3c6aSMatthew Dillon * Reverting the type is not necessary, the caller is going 10091c7c3c6aSMatthew Dillon * to destroy srcobject directly, but I'm doing it here 1010956f3135SPhilippe Charnier * for consistency since we've removed the object from its 10111c7c3c6aSMatthew Dillon * queues. 10121c7c3c6aSMatthew Dillon */ 10131c7c3c6aSMatthew Dillon srcobject->type = OBJT_DEFAULT; 1014c0877f10SJohn Dyson } 101526f9a767SRodney W. Grimes } 101626f9a767SRodney W. Grimes 1017df8bae1dSRodney W. Grimes /* 10181c7c3c6aSMatthew Dillon * SWAP_PAGER_HASPAGE() - determine if we have good backing store for 10191c7c3c6aSMatthew Dillon * the requested page. 10201c7c3c6aSMatthew Dillon * 10211c7c3c6aSMatthew Dillon * We determine whether good backing store exists for the requested 10221c7c3c6aSMatthew Dillon * page and return TRUE if it does, FALSE if it doesn't. 10231c7c3c6aSMatthew Dillon * 10241c7c3c6aSMatthew Dillon * If TRUE, we also try to determine how much valid, contiguous backing 1025915d1b71SMark Johnston * store exists before and after the requested page. 1026df8bae1dSRodney W. Grimes */ 10275ea4972cSAlan Cox static boolean_t 1028915d1b71SMark Johnston swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, 1029915d1b71SMark Johnston int *after) 103026f9a767SRodney W. Grimes { 1031915d1b71SMark Johnston daddr_t blk, blk0; 1032915d1b71SMark Johnston int i; 103326f9a767SRodney W. Grimes 1034c25673ffSAttilio Rao VM_OBJECT_ASSERT_LOCKED(object); 1035915d1b71SMark Johnston 10361c7c3c6aSMatthew Dillon /* 10371c7c3c6aSMatthew Dillon * do we have good backing store at the requested index ? 10381c7c3c6aSMatthew Dillon */ 10391c7c3c6aSMatthew Dillon blk0 = swp_pager_meta_ctl(object, pindex, 0); 10404dcc5c2dSMatthew Dillon if (blk0 == SWAPBLK_NONE) { 10411c7c3c6aSMatthew Dillon if (before) 104224a1cce3SDavid Greenman *before = 0; 10431c7c3c6aSMatthew Dillon if (after) 104424a1cce3SDavid Greenman *after = 0; 104526f9a767SRodney W. Grimes return (FALSE); 104626f9a767SRodney W. Grimes } 104726f9a767SRodney W. Grimes 104826f9a767SRodney W. Grimes /* 10491c7c3c6aSMatthew Dillon * find backwards-looking contiguous good backing store 1050e47ed70bSJohn Dyson */ 10511c7c3c6aSMatthew Dillon if (before != NULL) { 1052915d1b71SMark Johnston for (i = 1; i < SWB_NPAGES; i++) { 10531c7c3c6aSMatthew Dillon if (i > pindex) 10541c7c3c6aSMatthew Dillon break; 10551c7c3c6aSMatthew Dillon blk = swp_pager_meta_ctl(object, pindex - i, 0); 10561c7c3c6aSMatthew Dillon if (blk != blk0 - i) 10571c7c3c6aSMatthew Dillon break; 1058ffc82b0aSJohn Dyson } 1059915d1b71SMark Johnston *before = i - 1; 106026f9a767SRodney W. Grimes } 106126f9a767SRodney W. Grimes 106226f9a767SRodney W. Grimes /* 10631c7c3c6aSMatthew Dillon * find forward-looking contiguous good backing store 106426f9a767SRodney W. Grimes */ 10651c7c3c6aSMatthew Dillon if (after != NULL) { 1066915d1b71SMark Johnston for (i = 1; i < SWB_NPAGES; i++) { 10671c7c3c6aSMatthew Dillon blk = swp_pager_meta_ctl(object, pindex + i, 0); 10681c7c3c6aSMatthew Dillon if (blk != blk0 + i) 10691c7c3c6aSMatthew Dillon break; 107026f9a767SRodney W. Grimes } 1071915d1b71SMark Johnston *after = i - 1; 10721c7c3c6aSMatthew Dillon } 10731c7c3c6aSMatthew Dillon return (TRUE); 10741c7c3c6aSMatthew Dillon } 10751c7c3c6aSMatthew Dillon 10761c7c3c6aSMatthew Dillon /* 10771c7c3c6aSMatthew Dillon * SWAP_PAGER_PAGE_UNSWAPPED() - remove swap backing store related to page 10781c7c3c6aSMatthew Dillon * 10791c7c3c6aSMatthew Dillon * This removes any associated swap backing store, whether valid or 10801c7c3c6aSMatthew Dillon * not, from the page. 10811c7c3c6aSMatthew Dillon * 10821c7c3c6aSMatthew Dillon * This routine is typically called when a page is made dirty, at 10831c7c3c6aSMatthew Dillon * which point any associated swap can be freed. MADV_FREE also 10841c7c3c6aSMatthew Dillon * calls us in a special-case situation 10851c7c3c6aSMatthew Dillon * 10861c7c3c6aSMatthew Dillon * NOTE!!! If the page is clean and the swap was valid, the caller 10871c7c3c6aSMatthew Dillon * should make the page dirty before calling this routine. This routine 10881c7c3c6aSMatthew Dillon * does NOT change the m->dirty status of the page. Also: MADV_FREE 10891c7c3c6aSMatthew Dillon * depends on it. 10901c7c3c6aSMatthew Dillon * 109115523cf7SKonstantin Belousov * This routine may not sleep. 1092c25673ffSAttilio Rao * 1093c25673ffSAttilio Rao * The object containing the page must be locked. 10941c7c3c6aSMatthew Dillon */ 10951c7c3c6aSMatthew Dillon static void 10962f249180SPoul-Henning Kamp swap_pager_unswapped(vm_page_t m) 10971c7c3c6aSMatthew Dillon { 10984abca9bbSAlan Cox daddr_t srcaddr; 10992f249180SPoul-Henning Kamp 11004abca9bbSAlan Cox srcaddr = swp_pager_meta_ctl(m->object, m->pindex, SWM_POP); 11014abca9bbSAlan Cox if (srcaddr != SWAPBLK_NONE) 11024abca9bbSAlan Cox swp_pager_freeswapspace(srcaddr, 1); 11031c7c3c6aSMatthew Dillon } 11041c7c3c6aSMatthew Dillon 11051c7c3c6aSMatthew Dillon /* 1106915d1b71SMark Johnston * swap_pager_getpages() - bring pages in from swap 11071c7c3c6aSMatthew Dillon * 11083f060b60SMark Johnston * Attempt to page in the pages in array "ma" of length "count". The 11093f060b60SMark Johnston * caller may optionally specify that additional pages preceding and 11103f060b60SMark Johnston * succeeding the specified range be paged in. The number of such pages 11113f060b60SMark Johnston * is returned in the "rbehind" and "rahead" parameters, and they will 11123f060b60SMark Johnston * be in the inactive queue upon return. 11131c7c3c6aSMatthew Dillon * 11143f060b60SMark Johnston * The pages in "ma" must be busied and will remain busied upon return. 11151c7c3c6aSMatthew Dillon */ 1116f708ef1bSPoul-Henning Kamp static int 11173f060b60SMark Johnston swap_pager_getpages(vm_object_t object, vm_page_t *ma, int count, int *rbehind, 1118b0cd2017SGleb Smirnoff int *rahead) 1119df8bae1dSRodney W. Grimes { 11201c7c3c6aSMatthew Dillon struct buf *bp; 1121b7b8a096SKonstantin Belousov vm_page_t bm, mpred, msucc, p; 1122915d1b71SMark Johnston vm_pindex_t pindex; 11231c7c3c6aSMatthew Dillon daddr_t blk; 1124b7b8a096SKonstantin Belousov int i, maxahead, maxbehind, reqcount; 11250d94caffSDavid Greenman 1126915d1b71SMark Johnston reqcount = count; 11271c7c3c6aSMatthew Dillon 1128b7b8a096SKonstantin Belousov /* 1129b7b8a096SKonstantin Belousov * Determine the final number of read-behind pages and 1130b7b8a096SKonstantin Belousov * allocate them BEFORE releasing the object lock. Otherwise, 1131b7b8a096SKonstantin Belousov * there can be a problematic race with vm_object_split(). 1132b7b8a096SKonstantin Belousov * Specifically, vm_object_split() might first transfer pages 1133b7b8a096SKonstantin Belousov * that precede ma[0] in the current object to a new object, 1134b7b8a096SKonstantin Belousov * and then this function incorrectly recreates those pages as 1135b7b8a096SKonstantin Belousov * read-behind pages in the current object. 1136b7b8a096SKonstantin Belousov */ 1137b7b8a096SKonstantin Belousov if (!swap_pager_haspage(object, ma[0]->pindex, &maxbehind, &maxahead)) 1138915d1b71SMark Johnston return (VM_PAGER_FAIL); 1139915d1b71SMark Johnston 1140915d1b71SMark Johnston /* 1141915d1b71SMark Johnston * Clip the readahead and readbehind ranges to exclude resident pages. 1142915d1b71SMark Johnston */ 1143915d1b71SMark Johnston if (rahead != NULL) { 1144dd9cb6daSMark Johnston KASSERT(reqcount - 1 <= maxahead, 1145915d1b71SMark Johnston ("page count %d extends beyond swap block", reqcount)); 1146dd9cb6daSMark Johnston *rahead = imin(*rahead, maxahead - (reqcount - 1)); 11473f060b60SMark Johnston pindex = ma[reqcount - 1]->pindex; 11483f060b60SMark Johnston msucc = TAILQ_NEXT(ma[reqcount - 1], listq); 1149915d1b71SMark Johnston if (msucc != NULL && msucc->pindex - pindex - 1 < *rahead) 1150915d1b71SMark Johnston *rahead = msucc->pindex - pindex - 1; 1151915d1b71SMark Johnston } 1152915d1b71SMark Johnston if (rbehind != NULL) { 1153dd9cb6daSMark Johnston *rbehind = imin(*rbehind, maxbehind); 11543f060b60SMark Johnston pindex = ma[0]->pindex; 11553f060b60SMark Johnston mpred = TAILQ_PREV(ma[0], pglist, listq); 1156915d1b71SMark Johnston if (mpred != NULL && pindex - mpred->pindex - 1 < *rbehind) 1157915d1b71SMark Johnston *rbehind = pindex - mpred->pindex - 1; 1158915d1b71SMark Johnston } 1159915d1b71SMark Johnston 1160b7b8a096SKonstantin Belousov bm = ma[0]; 1161b7b8a096SKonstantin Belousov for (i = 0; i < count; i++) 1162b7b8a096SKonstantin Belousov ma[i]->oflags |= VPO_SWAPINPROG; 1163b7b8a096SKonstantin Belousov 1164915d1b71SMark Johnston /* 1165915d1b71SMark Johnston * Allocate readahead and readbehind pages. 1166915d1b71SMark Johnston */ 1167b7b8a096SKonstantin Belousov if (rbehind != NULL) { 1168b7b8a096SKonstantin Belousov for (i = 1; i <= *rbehind; i++) { 11693f060b60SMark Johnston p = vm_page_alloc(object, ma[0]->pindex - i, 11707667839aSAlan Cox VM_ALLOC_NORMAL); 1171b7b8a096SKonstantin Belousov if (p == NULL) 1172915d1b71SMark Johnston break; 1173b7b8a096SKonstantin Belousov p->oflags |= VPO_SWAPINPROG; 1174b7b8a096SKonstantin Belousov bm = p; 1175915d1b71SMark Johnston } 1176b7b8a096SKonstantin Belousov *rbehind = i - 1; 1177915d1b71SMark Johnston } 1178915d1b71SMark Johnston if (rahead != NULL) { 1179915d1b71SMark Johnston for (i = 0; i < *rahead; i++) { 1180915d1b71SMark Johnston p = vm_page_alloc(object, 11813f060b60SMark Johnston ma[reqcount - 1]->pindex + i + 1, VM_ALLOC_NORMAL); 1182915d1b71SMark Johnston if (p == NULL) 1183915d1b71SMark Johnston break; 1184b7b8a096SKonstantin Belousov p->oflags |= VPO_SWAPINPROG; 1185915d1b71SMark Johnston } 1186915d1b71SMark Johnston *rahead = i; 1187915d1b71SMark Johnston } 1188915d1b71SMark Johnston if (rbehind != NULL) 1189915d1b71SMark Johnston count += *rbehind; 1190915d1b71SMark Johnston if (rahead != NULL) 1191915d1b71SMark Johnston count += *rahead; 1192915d1b71SMark Johnston 1193915d1b71SMark Johnston vm_object_pip_add(object, count); 1194915d1b71SMark Johnston 1195b7b8a096SKonstantin Belousov pindex = bm->pindex; 1196915d1b71SMark Johnston blk = swp_pager_meta_ctl(object, pindex, 0); 1197915d1b71SMark Johnston KASSERT(blk != SWAPBLK_NONE, 1198915d1b71SMark Johnston ("no swap blocking containing %p(%jx)", object, (uintmax_t)pindex)); 1199915d1b71SMark Johnston 1200915d1b71SMark Johnston VM_OBJECT_WUNLOCK(object); 1201b7b8a096SKonstantin Belousov bp = getpbuf(&nsw_rcount); 1202b7b8a096SKonstantin Belousov /* Pages cannot leave the object while busy. */ 1203b7b8a096SKonstantin Belousov for (i = 0, p = bm; i < count; i++, p = TAILQ_NEXT(p, listq)) { 1204b7b8a096SKonstantin Belousov MPASS(p->pindex == bm->pindex + i); 1205b7b8a096SKonstantin Belousov bp->b_pages[i] = p; 1206b7b8a096SKonstantin Belousov } 1207915d1b71SMark Johnston 1208915d1b71SMark Johnston bp->b_flags |= B_PAGING; 120921144e3bSPoul-Henning Kamp bp->b_iocmd = BIO_READ; 12101c7c3c6aSMatthew Dillon bp->b_iodone = swp_pager_async_iodone; 1211fdcc1cc0SJohn Baldwin bp->b_rcred = crhold(thread0.td_ucred); 1212fdcc1cc0SJohn Baldwin bp->b_wcred = crhold(thread0.td_ucred); 1213b0cd2017SGleb Smirnoff bp->b_blkno = blk; 1214b0cd2017SGleb Smirnoff bp->b_bcount = PAGE_SIZE * count; 1215b0cd2017SGleb Smirnoff bp->b_bufsize = PAGE_SIZE * count; 1216b0cd2017SGleb Smirnoff bp->b_npages = count; 1217915d1b71SMark Johnston bp->b_pgbefore = rbehind != NULL ? *rbehind : 0; 1218915d1b71SMark Johnston bp->b_pgafter = rahead != NULL ? *rahead : 0; 121926f9a767SRodney W. Grimes 122083c9dea1SGleb Smirnoff VM_CNT_INC(v_swapin); 122183c9dea1SGleb Smirnoff VM_CNT_ADD(v_swappgsin, count); 12221c7c3c6aSMatthew Dillon 12231c7c3c6aSMatthew Dillon /* 12241c7c3c6aSMatthew Dillon * perform the I/O. NOTE!!! bp cannot be considered valid after 12251c7c3c6aSMatthew Dillon * this point because we automatically release it on completion. 12261c7c3c6aSMatthew Dillon * Instead, we look at the one page we are interested in which we 12271c7c3c6aSMatthew Dillon * still hold a lock on even through the I/O completion. 12281c7c3c6aSMatthew Dillon * 12293f060b60SMark Johnston * The other pages in our ma[] array are also released on completion, 12301c7c3c6aSMatthew Dillon * so we cannot assume they are valid anymore either. 12311c7c3c6aSMatthew Dillon * 1232c37a77eeSPoul-Henning Kamp * NOTE: b_blkno is destroyed by the call to swapdev_strategy 12331c7c3c6aSMatthew Dillon */ 1234b890cb2cSPeter Wemm BUF_KERNPROC(bp); 12354b03903aSPoul-Henning Kamp swp_pager_strategy(bp); 123626f9a767SRodney W. Grimes 123726f9a767SRodney W. Grimes /* 1238915d1b71SMark Johnston * Wait for the pages we want to complete. VPO_SWAPINPROG is always 12391c7c3c6aSMatthew Dillon * cleared on completion. If an I/O error occurs, SWAPBLK_NONE 1240915d1b71SMark Johnston * is set in the metadata for each page in the request. 124126f9a767SRodney W. Grimes */ 124289f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 12433f060b60SMark Johnston while ((ma[0]->oflags & VPO_SWAPINPROG) != 0) { 12443f060b60SMark Johnston ma[0]->oflags |= VPO_SWAPSLEEP; 124583c9dea1SGleb Smirnoff VM_CNT_INC(v_intrans); 1246c7aebda8SAttilio Rao if (VM_OBJECT_SLEEP(object, &object->paging_in_progress, PSWP, 1247c7aebda8SAttilio Rao "swread", hz * 20)) { 12489bd86a98SBruce M Simpson printf( 1249c5690651SPoul-Henning Kamp "swap_pager: indefinite wait buffer: bufobj: %p, blkno: %jd, size: %ld\n", 1250c5690651SPoul-Henning Kamp bp->b_bufobj, (intmax_t)bp->b_blkno, bp->b_bcount); 12511c7c3c6aSMatthew Dillon } 12521b119d9dSDavid Greenman } 125326f9a767SRodney W. Grimes 125426f9a767SRodney W. Grimes /* 1255b0cd2017SGleb Smirnoff * If we had an unrecoverable read error pages will not be valid. 125626f9a767SRodney W. Grimes */ 1257915d1b71SMark Johnston for (i = 0; i < reqcount; i++) 12583f060b60SMark Johnston if (ma[i]->valid != VM_PAGE_BITS_ALL) 12591c7c3c6aSMatthew Dillon return (VM_PAGER_ERROR); 1260b0cd2017SGleb Smirnoff 12611c7c3c6aSMatthew Dillon return (VM_PAGER_OK); 12621c7c3c6aSMatthew Dillon 12631c7c3c6aSMatthew Dillon /* 12641c7c3c6aSMatthew Dillon * A final note: in a low swap situation, we cannot deallocate swap 12651c7c3c6aSMatthew Dillon * and mark a page dirty here because the caller is likely to mark 12661c7c3c6aSMatthew Dillon * the page clean when we return, causing the page to possibly revert 12671c7c3c6aSMatthew Dillon * to all-zero's later. 12681c7c3c6aSMatthew Dillon */ 1269df8bae1dSRodney W. Grimes } 1270df8bae1dSRodney W. Grimes 12711c7c3c6aSMatthew Dillon /* 127290effb23SGleb Smirnoff * swap_pager_getpages_async(): 127390effb23SGleb Smirnoff * 127490effb23SGleb Smirnoff * Right now this is emulation of asynchronous operation on top of 127590effb23SGleb Smirnoff * swap_pager_getpages(). 127690effb23SGleb Smirnoff */ 127790effb23SGleb Smirnoff static int 12783f060b60SMark Johnston swap_pager_getpages_async(vm_object_t object, vm_page_t *ma, int count, 1279b0cd2017SGleb Smirnoff int *rbehind, int *rahead, pgo_getpages_iodone_t iodone, void *arg) 128090effb23SGleb Smirnoff { 128190effb23SGleb Smirnoff int r, error; 128290effb23SGleb Smirnoff 12833f060b60SMark Johnston r = swap_pager_getpages(object, ma, count, rbehind, rahead); 128490effb23SGleb Smirnoff VM_OBJECT_WUNLOCK(object); 128590effb23SGleb Smirnoff switch (r) { 128690effb23SGleb Smirnoff case VM_PAGER_OK: 128790effb23SGleb Smirnoff error = 0; 128890effb23SGleb Smirnoff break; 128990effb23SGleb Smirnoff case VM_PAGER_ERROR: 129090effb23SGleb Smirnoff error = EIO; 129190effb23SGleb Smirnoff break; 129290effb23SGleb Smirnoff case VM_PAGER_FAIL: 129390effb23SGleb Smirnoff error = EINVAL; 129490effb23SGleb Smirnoff break; 129590effb23SGleb Smirnoff default: 1296d9328101SGleb Smirnoff panic("unhandled swap_pager_getpages() error %d", r); 129790effb23SGleb Smirnoff } 12983f060b60SMark Johnston (iodone)(arg, ma, count, error); 129990effb23SGleb Smirnoff VM_OBJECT_WLOCK(object); 130090effb23SGleb Smirnoff 130190effb23SGleb Smirnoff return (r); 130290effb23SGleb Smirnoff } 130390effb23SGleb Smirnoff 130490effb23SGleb Smirnoff /* 13051c7c3c6aSMatthew Dillon * swap_pager_putpages: 13061c7c3c6aSMatthew Dillon * 13071c7c3c6aSMatthew Dillon * Assign swap (if necessary) and initiate I/O on the specified pages. 13081c7c3c6aSMatthew Dillon * 13091c7c3c6aSMatthew Dillon * We support both OBJT_DEFAULT and OBJT_SWAP objects. DEFAULT objects 13101c7c3c6aSMatthew Dillon * are automatically converted to SWAP objects. 13111c7c3c6aSMatthew Dillon * 1312ea3aecf5SPeter Wemm * In a low memory situation we may block in VOP_STRATEGY(), but the new 13131c7c3c6aSMatthew Dillon * vm_page reservation system coupled with properly written VFS devices 13141c7c3c6aSMatthew Dillon * should ensure that no low-memory deadlock occurs. This is an area 13151c7c3c6aSMatthew Dillon * which needs work. 13161c7c3c6aSMatthew Dillon * 13171c7c3c6aSMatthew Dillon * The parent has N vm_object_pip_add() references prior to 13181c7c3c6aSMatthew Dillon * calling us and will remove references for rtvals[] that are 13191c7c3c6aSMatthew Dillon * not set to VM_PAGER_PEND. We need to remove the rest on I/O 13201c7c3c6aSMatthew Dillon * completion. 13211c7c3c6aSMatthew Dillon * 13221c7c3c6aSMatthew Dillon * The parent has soft-busy'd the pages it passes us and will unbusy 13231c7c3c6aSMatthew Dillon * those whos rtvals[] entry is not set to VM_PAGER_PEND on return. 13241c7c3c6aSMatthew Dillon * We need to unbusy the rest on I/O completion. 13251c7c3c6aSMatthew Dillon */ 1326d635a37fSWarner Losh static void 13273f060b60SMark Johnston swap_pager_putpages(vm_object_t object, vm_page_t *ma, int count, 1328e065e87cSKonstantin Belousov int flags, int *rtvals) 1329df8bae1dSRodney W. Grimes { 1330e065e87cSKonstantin Belousov int i, n; 1331e065e87cSKonstantin Belousov boolean_t sync; 1332*78f1deefSAlan Cox daddr_t addr, n_free, s_free; 1333df8bae1dSRodney W. Grimes 1334*78f1deefSAlan Cox swp_pager_init_freerange(&s_free, &n_free); 13353f060b60SMark Johnston if (count && ma[0]->object != object) { 13367036145bSMaxim Konovalov panic("swap_pager_putpages: object mismatch %p/%p", 13371c7c3c6aSMatthew Dillon object, 13383f060b60SMark Johnston ma[0]->object 13391c7c3c6aSMatthew Dillon ); 13401c7c3c6aSMatthew Dillon } 1341ee3dc7d7SAlan Cox 13421c7c3c6aSMatthew Dillon /* 13431c7c3c6aSMatthew Dillon * Step 1 13441c7c3c6aSMatthew Dillon * 13451c7c3c6aSMatthew Dillon * Turn object into OBJT_SWAP 13461c7c3c6aSMatthew Dillon * check for bogus sysops 13471c7c3c6aSMatthew Dillon * force sync if not pageout process 13481c7c3c6aSMatthew Dillon */ 1349*78f1deefSAlan Cox if (object->type != OBJT_SWAP) { 1350*78f1deefSAlan Cox addr = swp_pager_meta_build(object, 0, SWAPBLK_NONE); 1351*78f1deefSAlan Cox KASSERT(addr == SWAPBLK_NONE, 1352*78f1deefSAlan Cox ("unexpected object swap block")); 1353*78f1deefSAlan Cox } 135489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1355e47ed70bSJohn Dyson 1356e065e87cSKonstantin Belousov n = 0; 1357e47ed70bSJohn Dyson if (curproc != pageproc) 1358e47ed70bSJohn Dyson sync = TRUE; 1359e065e87cSKonstantin Belousov else 1360e065e87cSKonstantin Belousov sync = (flags & VM_PAGER_PUT_SYNC) != 0; 136126f9a767SRodney W. Grimes 13621c7c3c6aSMatthew Dillon /* 13631c7c3c6aSMatthew Dillon * Step 2 13641c7c3c6aSMatthew Dillon * 13651c7c3c6aSMatthew Dillon * Assign swap blocks and issue I/O. We reallocate swap on the fly. 13661c7c3c6aSMatthew Dillon * The page is left dirty until the pageout operation completes 13671c7c3c6aSMatthew Dillon * successfully. 13681c7c3c6aSMatthew Dillon */ 13691c7c3c6aSMatthew Dillon for (i = 0; i < count; i += n) { 13701c7c3c6aSMatthew Dillon int j; 13711c7c3c6aSMatthew Dillon struct buf *bp; 1372a316d390SJohn Dyson daddr_t blk; 137326f9a767SRodney W. Grimes 1374df8bae1dSRodney W. Grimes /* 13751c7c3c6aSMatthew Dillon * Maximum I/O size is limited by a number of factors. 1376df8bae1dSRodney W. Grimes */ 13771c7c3c6aSMatthew Dillon n = min(BLIST_MAX_ALLOC, count - i); 1378327f4e83SMatthew Dillon n = min(n, nsw_cluster_max); 13791c7c3c6aSMatthew Dillon 138026f9a767SRodney W. Grimes /* 13811c7c3c6aSMatthew Dillon * Get biggest block of swap we can. If we fail, fall 13821c7c3c6aSMatthew Dillon * back and try to allocate a smaller block. Don't go 13831c7c3c6aSMatthew Dillon * overboard trying to allocate space if it would overly 13841c7c3c6aSMatthew Dillon * fragment swap. 138526f9a767SRodney W. Grimes */ 13861c7c3c6aSMatthew Dillon while ( 13871c7c3c6aSMatthew Dillon (blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE && 13881c7c3c6aSMatthew Dillon n > 4 13891c7c3c6aSMatthew Dillon ) { 13901c7c3c6aSMatthew Dillon n >>= 1; 139126f9a767SRodney W. Grimes } 13921c7c3c6aSMatthew Dillon if (blk == SWAPBLK_NONE) { 13934dcc5c2dSMatthew Dillon for (j = 0; j < n; ++j) 13941c7c3c6aSMatthew Dillon rtvals[i+j] = VM_PAGER_FAIL; 13951c7c3c6aSMatthew Dillon continue; 139626f9a767SRodney W. Grimes } 139726f9a767SRodney W. Grimes 139826f9a767SRodney W. Grimes /* 13991c7c3c6aSMatthew Dillon * All I/O parameters have been satisfied, build the I/O 14001c7c3c6aSMatthew Dillon * request and assign the swap space. 140126f9a767SRodney W. Grimes */ 1402327f4e83SMatthew Dillon if (sync == TRUE) { 1403327f4e83SMatthew Dillon bp = getpbuf(&nsw_wcount_sync); 1404327f4e83SMatthew Dillon } else { 1405327f4e83SMatthew Dillon bp = getpbuf(&nsw_wcount_async); 140621144e3bSPoul-Henning Kamp bp->b_flags = B_ASYNC; 1407327f4e83SMatthew Dillon } 14085e04322aSPoul-Henning Kamp bp->b_flags |= B_PAGING; 1409912e4ae9SPoul-Henning Kamp bp->b_iocmd = BIO_WRITE; 141026f9a767SRodney W. Grimes 1411fdcc1cc0SJohn Baldwin bp->b_rcred = crhold(thread0.td_ucred); 1412fdcc1cc0SJohn Baldwin bp->b_wcred = crhold(thread0.td_ucred); 14131c7c3c6aSMatthew Dillon bp->b_bcount = PAGE_SIZE * n; 14141c7c3c6aSMatthew Dillon bp->b_bufsize = PAGE_SIZE * n; 14151c7c3c6aSMatthew Dillon bp->b_blkno = blk; 1416e47ed70bSJohn Dyson 141789f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 14181c7c3c6aSMatthew Dillon for (j = 0; j < n; ++j) { 14193f060b60SMark Johnston vm_page_t mreq = ma[i+j]; 14201c7c3c6aSMatthew Dillon 1421*78f1deefSAlan Cox addr = swp_pager_meta_build(mreq->object, mreq->pindex, 1422*78f1deefSAlan Cox blk + j); 1423*78f1deefSAlan Cox if (addr != SWAPBLK_NONE) 1424*78f1deefSAlan Cox swp_pager_update_freerange(&s_free, &n_free, 1425*78f1deefSAlan Cox addr); 142687b0ab69SAlan Cox MPASS(mreq->dirty == VM_PAGE_BITS_ALL); 14275786be7cSAlan Cox mreq->oflags |= VPO_SWAPINPROG; 14281c7c3c6aSMatthew Dillon bp->b_pages[j] = mreq; 14291c7c3c6aSMatthew Dillon } 143089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 14311c7c3c6aSMatthew Dillon bp->b_npages = n; 1432a5296b05SJulian Elischer /* 1433a5296b05SJulian Elischer * Must set dirty range for NFS to work. 1434a5296b05SJulian Elischer */ 1435a5296b05SJulian Elischer bp->b_dirtyoff = 0; 1436a5296b05SJulian Elischer bp->b_dirtyend = bp->b_bcount; 14371c7c3c6aSMatthew Dillon 143883c9dea1SGleb Smirnoff VM_CNT_INC(v_swapout); 143983c9dea1SGleb Smirnoff VM_CNT_ADD(v_swappgsout, bp->b_npages); 144026f9a767SRodney W. Grimes 144126f9a767SRodney W. Grimes /* 144277923df2SAlan Cox * We unconditionally set rtvals[] to VM_PAGER_PEND so that we 144377923df2SAlan Cox * can call the async completion routine at the end of a 144477923df2SAlan Cox * synchronous I/O operation. Otherwise, our caller would 144577923df2SAlan Cox * perform duplicate unbusy and wakeup operations on the page 144677923df2SAlan Cox * and object, respectively. 144777923df2SAlan Cox */ 144877923df2SAlan Cox for (j = 0; j < n; j++) 144977923df2SAlan Cox rtvals[i + j] = VM_PAGER_PEND; 145077923df2SAlan Cox 145177923df2SAlan Cox /* 14521c7c3c6aSMatthew Dillon * asynchronous 14531c7c3c6aSMatthew Dillon * 1454c37a77eeSPoul-Henning Kamp * NOTE: b_blkno is destroyed by the call to swapdev_strategy 145526f9a767SRodney W. Grimes */ 14561c7c3c6aSMatthew Dillon if (sync == FALSE) { 14571c7c3c6aSMatthew Dillon bp->b_iodone = swp_pager_async_iodone; 145867812eacSKirk McKusick BUF_KERNPROC(bp); 14594b03903aSPoul-Henning Kamp swp_pager_strategy(bp); 14601c7c3c6aSMatthew Dillon continue; 146126f9a767SRodney W. Grimes } 1462e47ed70bSJohn Dyson 146326f9a767SRodney W. Grimes /* 14641c7c3c6aSMatthew Dillon * synchronous 14651c7c3c6aSMatthew Dillon * 1466c37a77eeSPoul-Henning Kamp * NOTE: b_blkno is destroyed by the call to swapdev_strategy 14671c7c3c6aSMatthew Dillon */ 14682c840b1fSAlan Cox bp->b_iodone = bdone; 14694b03903aSPoul-Henning Kamp swp_pager_strategy(bp); 14701c7c3c6aSMatthew Dillon 14711c7c3c6aSMatthew Dillon /* 147277923df2SAlan Cox * Wait for the sync I/O to complete. 147326f9a767SRodney W. Grimes */ 14742c840b1fSAlan Cox bwait(bp, PVM, "swwrt"); 147577923df2SAlan Cox 14761c7c3c6aSMatthew Dillon /* 14771c7c3c6aSMatthew Dillon * Now that we are through with the bp, we can call the 14781c7c3c6aSMatthew Dillon * normal async completion, which frees everything up. 14791c7c3c6aSMatthew Dillon */ 14801c7c3c6aSMatthew Dillon swp_pager_async_iodone(bp); 14811c7c3c6aSMatthew Dillon } 148289f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1483*78f1deefSAlan Cox swp_pager_freeswapspace(s_free, n_free); 14841c7c3c6aSMatthew Dillon } 14851c7c3c6aSMatthew Dillon 14861c7c3c6aSMatthew Dillon /* 14871c7c3c6aSMatthew Dillon * swp_pager_async_iodone: 14881c7c3c6aSMatthew Dillon * 14891c7c3c6aSMatthew Dillon * Completion routine for asynchronous reads and writes from/to swap. 14901c7c3c6aSMatthew Dillon * Also called manually by synchronous code to finish up a bp. 14911c7c3c6aSMatthew Dillon * 149215523cf7SKonstantin Belousov * This routine may not sleep. 14931c7c3c6aSMatthew Dillon */ 14941c7c3c6aSMatthew Dillon static void 14952f249180SPoul-Henning Kamp swp_pager_async_iodone(struct buf *bp) 14961c7c3c6aSMatthew Dillon { 14971c7c3c6aSMatthew Dillon int i; 14981c7c3c6aSMatthew Dillon vm_object_t object = NULL; 14991c7c3c6aSMatthew Dillon 15001c7c3c6aSMatthew Dillon /* 15011c7c3c6aSMatthew Dillon * report error 15021c7c3c6aSMatthew Dillon */ 1503c244d2deSPoul-Henning Kamp if (bp->b_ioflags & BIO_ERROR) { 15041c7c3c6aSMatthew Dillon printf( 15051c7c3c6aSMatthew Dillon "swap_pager: I/O error - %s failed; blkno %ld," 15061c7c3c6aSMatthew Dillon "size %ld, error %d\n", 150721144e3bSPoul-Henning Kamp ((bp->b_iocmd == BIO_READ) ? "pagein" : "pageout"), 15081c7c3c6aSMatthew Dillon (long)bp->b_blkno, 15091c7c3c6aSMatthew Dillon (long)bp->b_bcount, 15101c7c3c6aSMatthew Dillon bp->b_error 15111c7c3c6aSMatthew Dillon ); 15121c7c3c6aSMatthew Dillon } 15131c7c3c6aSMatthew Dillon 15141c7c3c6aSMatthew Dillon /* 151526f9a767SRodney W. Grimes * remove the mapping for kernel virtual 151626f9a767SRodney W. Grimes */ 1517fade8dd7SJeff Roberson if (buf_mapped(bp)) 15181c7c3c6aSMatthew Dillon pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages); 1519fade8dd7SJeff Roberson else 1520fade8dd7SJeff Roberson bp->b_data = bp->b_kvabase; 152126f9a767SRodney W. Grimes 152233a609ecSAlan Cox if (bp->b_npages) { 152333a609ecSAlan Cox object = bp->b_pages[0]->object; 152489f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 152533a609ecSAlan Cox } 15262965a453SKip Macy 152726f9a767SRodney W. Grimes /* 15281c7c3c6aSMatthew Dillon * cleanup pages. If an error occurs writing to swap, we are in 15291c7c3c6aSMatthew Dillon * very serious trouble. If it happens to be a disk error, though, 15301c7c3c6aSMatthew Dillon * we may be able to recover by reassigning the swap later on. So 15311c7c3c6aSMatthew Dillon * in this case we remove the m->swapblk assignment for the page 15321c7c3c6aSMatthew Dillon * but do not free it in the rlist. The errornous block(s) are thus 15331c7c3c6aSMatthew Dillon * never reallocated as swap. Redirty the page and continue. 153426f9a767SRodney W. Grimes */ 15351c7c3c6aSMatthew Dillon for (i = 0; i < bp->b_npages; ++i) { 15361c7c3c6aSMatthew Dillon vm_page_t m = bp->b_pages[i]; 1537e47ed70bSJohn Dyson 15385786be7cSAlan Cox m->oflags &= ~VPO_SWAPINPROG; 1539c7aebda8SAttilio Rao if (m->oflags & VPO_SWAPSLEEP) { 1540c7aebda8SAttilio Rao m->oflags &= ~VPO_SWAPSLEEP; 1541c7aebda8SAttilio Rao wakeup(&object->paging_in_progress); 1542c7aebda8SAttilio Rao } 1543e47ed70bSJohn Dyson 1544c244d2deSPoul-Henning Kamp if (bp->b_ioflags & BIO_ERROR) { 1545ffc82b0aSJohn Dyson /* 15461c7c3c6aSMatthew Dillon * If an error occurs I'd love to throw the swapblk 15471c7c3c6aSMatthew Dillon * away without freeing it back to swapspace, so it 15481c7c3c6aSMatthew Dillon * can never be used again. But I can't from an 15491c7c3c6aSMatthew Dillon * interrupt. 1550ffc82b0aSJohn Dyson */ 155121144e3bSPoul-Henning Kamp if (bp->b_iocmd == BIO_READ) { 15521c7c3c6aSMatthew Dillon /* 15531c7c3c6aSMatthew Dillon * NOTE: for reads, m->dirty will probably 1554956f3135SPhilippe Charnier * be overridden by the original caller of 15551c7c3c6aSMatthew Dillon * getpages so don't play cute tricks here. 15561c7c3c6aSMatthew Dillon */ 15571c7c3c6aSMatthew Dillon m->valid = 0; 15581c7c3c6aSMatthew Dillon } else { 15591c7c3c6aSMatthew Dillon /* 15601c7c3c6aSMatthew Dillon * If a write error occurs, reactivate page 15611c7c3c6aSMatthew Dillon * so it doesn't clog the inactive list, 15621c7c3c6aSMatthew Dillon * then finish the I/O. 15631c7c3c6aSMatthew Dillon */ 156441e5a226SAlan Cox MPASS(m->dirty == VM_PAGE_BITS_ALL); 15653c4a2440SAlan Cox vm_page_lock(m); 15661c7c3c6aSMatthew Dillon vm_page_activate(m); 15673c4a2440SAlan Cox vm_page_unlock(m); 1568c7aebda8SAttilio Rao vm_page_sunbusy(m); 15691c7c3c6aSMatthew Dillon } 157021144e3bSPoul-Henning Kamp } else if (bp->b_iocmd == BIO_READ) { 15711c7c3c6aSMatthew Dillon /* 15721c7c3c6aSMatthew Dillon * NOTE: for reads, m->dirty will probably be 1573956f3135SPhilippe Charnier * overridden by the original caller of getpages so 15741c7c3c6aSMatthew Dillon * we cannot set them in order to free the underlying 15751c7c3c6aSMatthew Dillon * swap in a low-swap situation. I don't think we'd 15761c7c3c6aSMatthew Dillon * want to do that anyway, but it was an optimization 15771c7c3c6aSMatthew Dillon * that existed in the old swapper for a time before 15781c7c3c6aSMatthew Dillon * it got ripped out due to precisely this problem. 15791c7c3c6aSMatthew Dillon */ 1580016a3c93SAlan Cox KASSERT(!pmap_page_is_mapped(m), 1581016a3c93SAlan Cox ("swp_pager_async_iodone: page %p is mapped", m)); 1582016a3c93SAlan Cox KASSERT(m->dirty == 0, 1583016a3c93SAlan Cox ("swp_pager_async_iodone: page %p is dirty", m)); 1584915d1b71SMark Johnston 1585b0cd2017SGleb Smirnoff m->valid = VM_PAGE_BITS_ALL; 1586915d1b71SMark Johnston if (i < bp->b_pgbefore || 1587915d1b71SMark Johnston i >= bp->b_npages - bp->b_pgafter) 1588915d1b71SMark Johnston vm_page_readahead_finish(m); 15891c7c3c6aSMatthew Dillon } else { 15901c7c3c6aSMatthew Dillon /* 1591016a3c93SAlan Cox * For write success, clear the dirty 15921c7c3c6aSMatthew Dillon * status, then finish the I/O ( which decrements the 15931c7c3c6aSMatthew Dillon * busy count and possibly wakes waiter's up ). 1594ebcddc72SAlan Cox * A page is only written to swap after a period of 1595ebcddc72SAlan Cox * inactivity. Therefore, we do not expect it to be 1596ebcddc72SAlan Cox * reused. 15971c7c3c6aSMatthew Dillon */ 15986031c68dSAlan Cox KASSERT(!pmap_page_is_write_mapped(m), 1599016a3c93SAlan Cox ("swp_pager_async_iodone: page %p is not write" 1600016a3c93SAlan Cox " protected", m)); 1601c52e7044SAlan Cox vm_page_undirty(m); 16023c4a2440SAlan Cox vm_page_lock(m); 1603ebcddc72SAlan Cox vm_page_deactivate_noreuse(m); 16042965a453SKip Macy vm_page_unlock(m); 1605ebcddc72SAlan Cox vm_page_sunbusy(m); 16063c4a2440SAlan Cox } 16073c4a2440SAlan Cox } 160826f9a767SRodney W. Grimes 16091c7c3c6aSMatthew Dillon /* 16101c7c3c6aSMatthew Dillon * adjust pip. NOTE: the original parent may still have its own 16111c7c3c6aSMatthew Dillon * pip refs on the object. 16121c7c3c6aSMatthew Dillon */ 16130d420ad3SAlan Cox if (object != NULL) { 16141c7c3c6aSMatthew Dillon vm_object_pip_wakeupn(object, bp->b_npages); 161589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 16160d420ad3SAlan Cox } 161726f9a767SRodney W. Grimes 16181c7c3c6aSMatthew Dillon /* 1619100650deSOlivier Houchard * swapdev_strategy() manually sets b_vp and b_bufobj before calling 1620100650deSOlivier Houchard * bstrategy(). Set them back to NULL now we're done with it, or we'll 1621100650deSOlivier Houchard * trigger a KASSERT in relpbuf(). 1622100650deSOlivier Houchard */ 1623100650deSOlivier Houchard if (bp->b_vp) { 1624100650deSOlivier Houchard bp->b_vp = NULL; 1625100650deSOlivier Houchard bp->b_bufobj = NULL; 1626100650deSOlivier Houchard } 1627100650deSOlivier Houchard /* 16281c7c3c6aSMatthew Dillon * release the physical I/O buffer 16291c7c3c6aSMatthew Dillon */ 1630327f4e83SMatthew Dillon relpbuf( 1631327f4e83SMatthew Dillon bp, 163221144e3bSPoul-Henning Kamp ((bp->b_iocmd == BIO_READ) ? &nsw_rcount : 1633327f4e83SMatthew Dillon ((bp->b_flags & B_ASYNC) ? 1634327f4e83SMatthew Dillon &nsw_wcount_async : 1635327f4e83SMatthew Dillon &nsw_wcount_sync 1636327f4e83SMatthew Dillon ) 1637327f4e83SMatthew Dillon ) 1638327f4e83SMatthew Dillon ); 163926f9a767SRodney W. Grimes } 16401c7c3c6aSMatthew Dillon 1641b1fd102eSMark Johnston int 1642b1fd102eSMark Johnston swap_pager_nswapdev(void) 1643b1fd102eSMark Johnston { 1644b1fd102eSMark Johnston 1645b1fd102eSMark Johnston return (nswapdev); 1646b1fd102eSMark Johnston } 1647b1fd102eSMark Johnston 164892da00bbSMatthew Dillon /* 164992da00bbSMatthew Dillon * SWP_PAGER_FORCE_PAGEIN() - force a swap block to be paged in 165092da00bbSMatthew Dillon * 1651ebcddc72SAlan Cox * This routine dissociates the page at the given index within an object 1652ebcddc72SAlan Cox * from its backing store, paging it in if it does not reside in memory. 1653ebcddc72SAlan Cox * If the page is paged in, it is marked dirty and placed in the laundry 1654ebcddc72SAlan Cox * queue. The page is marked dirty because it no longer has backing 1655ebcddc72SAlan Cox * store. It is placed in the laundry queue because it has not been 1656ebcddc72SAlan Cox * accessed recently. Otherwise, it would already reside in memory. 1657ebcddc72SAlan Cox * 1658ebcddc72SAlan Cox * We also attempt to swap in all other pages in the swap block. 1659ebcddc72SAlan Cox * However, we only guarantee that the one at the specified index is 166092da00bbSMatthew Dillon * paged in. 166192da00bbSMatthew Dillon * 166292da00bbSMatthew Dillon * XXX - The code to page the whole block in doesn't work, so we 166392da00bbSMatthew Dillon * revert to the one-by-one behavior for now. Sigh. 166492da00bbSMatthew Dillon */ 166562a59e8fSWarner Losh static inline void 1666b3fed13eSDavid Schultz swp_pager_force_pagein(vm_object_t object, vm_pindex_t pindex) 166792da00bbSMatthew Dillon { 166892da00bbSMatthew Dillon vm_page_t m; 166992da00bbSMatthew Dillon 167092da00bbSMatthew Dillon vm_object_pip_add(object, 1); 16715944de8eSKonstantin Belousov m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL); 167292da00bbSMatthew Dillon if (m->valid == VM_PAGE_BITS_ALL) { 16730d8243ccSAttilio Rao vm_object_pip_wakeup(object); 167492da00bbSMatthew Dillon vm_page_dirty(m); 167537244a84SAlan Cox #ifdef INVARIANTS 1676d061cdd5SAlan Cox vm_page_lock(m); 167737244a84SAlan Cox if (m->wire_count == 0 && m->queue == PQ_NONE) 167837244a84SAlan Cox panic("page %p is neither wired nor queued", m); 16792965a453SKip Macy vm_page_unlock(m); 168037244a84SAlan Cox #endif 1681c7aebda8SAttilio Rao vm_page_xunbusy(m); 168292da00bbSMatthew Dillon vm_pager_page_unswapped(m); 168392da00bbSMatthew Dillon return; 168492da00bbSMatthew Dillon } 168592da00bbSMatthew Dillon 1686b0cd2017SGleb Smirnoff if (swap_pager_getpages(object, &m, 1, NULL, NULL) != VM_PAGER_OK) 168792da00bbSMatthew Dillon panic("swap_pager_force_pagein: read from swap failed");/*XXX*/ 16880d8243ccSAttilio Rao vm_object_pip_wakeup(object); 168992da00bbSMatthew Dillon vm_page_dirty(m); 1690db1f085eSAlan Cox vm_page_lock(m); 1691ebcddc72SAlan Cox vm_page_launder(m); 16922965a453SKip Macy vm_page_unlock(m); 1693c7aebda8SAttilio Rao vm_page_xunbusy(m); 169492da00bbSMatthew Dillon vm_pager_page_unswapped(m); 169592da00bbSMatthew Dillon } 169692da00bbSMatthew Dillon 169792da00bbSMatthew Dillon /* 169892da00bbSMatthew Dillon * swap_pager_swapoff: 169992da00bbSMatthew Dillon * 170092da00bbSMatthew Dillon * Page in all of the pages that have been paged out to the 170192da00bbSMatthew Dillon * given device. The corresponding blocks in the bitmap must be 170292da00bbSMatthew Dillon * marked as allocated and the device must be flagged SW_CLOSING. 170392da00bbSMatthew Dillon * There may be no processes swapped out to the device. 170492da00bbSMatthew Dillon * 170592da00bbSMatthew Dillon * This routine may block. 170692da00bbSMatthew Dillon */ 1707e9c0cc15SPoul-Henning Kamp static void 1708b3fed13eSDavid Schultz swap_pager_swapoff(struct swdevt *sp) 170992da00bbSMatthew Dillon { 1710f425ab8eSKonstantin Belousov struct swblk *sb; 1711f425ab8eSKonstantin Belousov vm_object_t object; 1712f425ab8eSKonstantin Belousov vm_pindex_t pi; 1713f425ab8eSKonstantin Belousov int i, retries; 171492da00bbSMatthew Dillon 171504533e1eSKonstantin Belousov sx_assert(&swdev_syscall_lock, SA_XLOCKED); 171692da00bbSMatthew Dillon 17178bc61209SDavid Schultz retries = 0; 171892da00bbSMatthew Dillon full_rescan: 1719f425ab8eSKonstantin Belousov mtx_lock(&vm_object_list_mtx); 1720f425ab8eSKonstantin Belousov TAILQ_FOREACH(object, &vm_object_list, object_list) { 1721f425ab8eSKonstantin Belousov if (object->type != OBJT_SWAP) 172298150664SKonstantin Belousov continue; 1723f425ab8eSKonstantin Belousov mtx_unlock(&vm_object_list_mtx); 172498150664SKonstantin Belousov /* Depends on type-stability. */ 172598150664SKonstantin Belousov VM_OBJECT_WLOCK(object); 1726f425ab8eSKonstantin Belousov 1727f425ab8eSKonstantin Belousov /* 1728f425ab8eSKonstantin Belousov * Dead objects are eventually terminated on their own. 1729f425ab8eSKonstantin Belousov */ 1730f425ab8eSKonstantin Belousov if ((object->flags & OBJ_DEAD) != 0) 1731f425ab8eSKonstantin Belousov goto next_obj; 1732f425ab8eSKonstantin Belousov 1733f425ab8eSKonstantin Belousov /* 1734f425ab8eSKonstantin Belousov * Sync with fences placed after pctrie 1735f425ab8eSKonstantin Belousov * initialization. We must not access pctrie below 1736f425ab8eSKonstantin Belousov * unless we checked that our object is swap and not 1737f425ab8eSKonstantin Belousov * dead. 1738f425ab8eSKonstantin Belousov */ 1739f425ab8eSKonstantin Belousov atomic_thread_fence_acq(); 1740f425ab8eSKonstantin Belousov if (object->type != OBJT_SWAP) 1741f425ab8eSKonstantin Belousov goto next_obj; 1742f425ab8eSKonstantin Belousov 1743f425ab8eSKonstantin Belousov for (pi = 0; (sb = SWAP_PCTRIE_LOOKUP_GE( 1744f425ab8eSKonstantin Belousov &object->un_pager.swp.swp_blks, pi)) != NULL; ) { 1745f425ab8eSKonstantin Belousov pi = sb->p + SWAP_META_PAGES; 1746f425ab8eSKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) { 1747f425ab8eSKonstantin Belousov if (sb->d[i] == SWAPBLK_NONE) 1748f425ab8eSKonstantin Belousov continue; 1749f425ab8eSKonstantin Belousov if (swp_pager_isondev(sb->d[i], sp)) 1750f425ab8eSKonstantin Belousov swp_pager_force_pagein(object, 1751f425ab8eSKonstantin Belousov sb->p + i); 175298150664SKonstantin Belousov } 175398150664SKonstantin Belousov } 1754f425ab8eSKonstantin Belousov next_obj: 1755f425ab8eSKonstantin Belousov VM_OBJECT_WUNLOCK(object); 1756f425ab8eSKonstantin Belousov mtx_lock(&vm_object_list_mtx); 175792da00bbSMatthew Dillon } 1758f425ab8eSKonstantin Belousov mtx_unlock(&vm_object_list_mtx); 1759f425ab8eSKonstantin Belousov 17608bc61209SDavid Schultz if (sp->sw_used) { 176192da00bbSMatthew Dillon /* 17628bc61209SDavid Schultz * Objects may be locked or paging to the device being 17638bc61209SDavid Schultz * removed, so we will miss their pages and need to 17648bc61209SDavid Schultz * make another pass. We have marked this device as 17658bc61209SDavid Schultz * SW_CLOSING, so the activity should finish soon. 176692da00bbSMatthew Dillon */ 17678bc61209SDavid Schultz retries++; 17688bc61209SDavid Schultz if (retries > 100) { 17698bc61209SDavid Schultz panic("swapoff: failed to locate %d swap blocks", 17708bc61209SDavid Schultz sp->sw_used); 17718bc61209SDavid Schultz } 17724d70511aSJohn Baldwin pause("swpoff", hz / 20); 177392da00bbSMatthew Dillon goto full_rescan; 177492da00bbSMatthew Dillon } 1775b1fd102eSMark Johnston EVENTHANDLER_INVOKE(swapoff, sp); 177692da00bbSMatthew Dillon } 177792da00bbSMatthew Dillon 17781c7c3c6aSMatthew Dillon /************************************************************************ 17791c7c3c6aSMatthew Dillon * SWAP META DATA * 17801c7c3c6aSMatthew Dillon ************************************************************************ 17811c7c3c6aSMatthew Dillon * 17821c7c3c6aSMatthew Dillon * These routines manipulate the swap metadata stored in the 1783cec9f109SDavid E. O'Brien * OBJT_SWAP object. 17841c7c3c6aSMatthew Dillon * 17854dcc5c2dSMatthew Dillon * Swap metadata is implemented with a global hash and not directly 17864dcc5c2dSMatthew Dillon * linked into the object. Instead the object simply contains 17874dcc5c2dSMatthew Dillon * appropriate tracking counters. 17881c7c3c6aSMatthew Dillon */ 17891c7c3c6aSMatthew Dillon 17901c7c3c6aSMatthew Dillon /* 1791230869e0SAlan Cox * SWP_PAGER_SWBLK_EMPTY() - is a range of blocks free? 1792230869e0SAlan Cox */ 1793230869e0SAlan Cox static bool 1794230869e0SAlan Cox swp_pager_swblk_empty(struct swblk *sb, int start, int limit) 1795230869e0SAlan Cox { 1796230869e0SAlan Cox int i; 1797230869e0SAlan Cox 1798230869e0SAlan Cox MPASS(0 <= start && start <= limit && limit <= SWAP_META_PAGES); 1799230869e0SAlan Cox for (i = start; i < limit; i++) { 1800230869e0SAlan Cox if (sb->d[i] != SWAPBLK_NONE) 1801230869e0SAlan Cox return (false); 1802230869e0SAlan Cox } 1803230869e0SAlan Cox return (true); 1804230869e0SAlan Cox } 1805230869e0SAlan Cox 1806230869e0SAlan Cox /* 18071c7c3c6aSMatthew Dillon * SWP_PAGER_META_BUILD() - add swap block to swap meta data for object 18081c7c3c6aSMatthew Dillon * 18091c7c3c6aSMatthew Dillon * We first convert the object to a swap object if it is a default 18101c7c3c6aSMatthew Dillon * object. 18111c7c3c6aSMatthew Dillon * 18121c7c3c6aSMatthew Dillon * The specified swapblk is added to the object's swap metadata. If 18131c7c3c6aSMatthew Dillon * the swapblk is not valid, it is freed instead. Any previously 1814*78f1deefSAlan Cox * assigned swapblk is returned. 18151c7c3c6aSMatthew Dillon */ 1816*78f1deefSAlan Cox static daddr_t 18172f249180SPoul-Henning Kamp swp_pager_meta_build(vm_object_t object, vm_pindex_t pindex, daddr_t swapblk) 18182f249180SPoul-Henning Kamp { 1819f425ab8eSKonstantin Belousov static volatile int swblk_zone_exhausted, swpctrie_zone_exhausted; 1820eed99cb8SKonstantin Belousov struct swblk *sb, *sb1; 1821f425ab8eSKonstantin Belousov vm_pindex_t modpi, rdpi; 1822*78f1deefSAlan Cox daddr_t prev_swapblk; 1823f425ab8eSKonstantin Belousov int error, i; 18241c7c3c6aSMatthew Dillon 182589f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 1826f425ab8eSKonstantin Belousov 18271c7c3c6aSMatthew Dillon /* 18281c7c3c6aSMatthew Dillon * Convert default object to swap object if necessary 18291c7c3c6aSMatthew Dillon */ 18301c7c3c6aSMatthew Dillon if (object->type != OBJT_SWAP) { 1831f425ab8eSKonstantin Belousov pctrie_init(&object->un_pager.swp.swp_blks); 1832f425ab8eSKonstantin Belousov 1833f425ab8eSKonstantin Belousov /* 1834f425ab8eSKonstantin Belousov * Ensure that swap_pager_swapoff()'s iteration over 1835f425ab8eSKonstantin Belousov * object_list does not see a garbage pctrie. 1836f425ab8eSKonstantin Belousov */ 1837f425ab8eSKonstantin Belousov atomic_thread_fence_rel(); 1838f425ab8eSKonstantin Belousov 18391c7c3c6aSMatthew Dillon object->type = OBJT_SWAP; 1840eb4d6a1bSKonstantin Belousov KASSERT(object->handle == NULL, ("default pager with handle")); 1841bd228075SAlan Cox } 18421c7c3c6aSMatthew Dillon 1843f425ab8eSKonstantin Belousov rdpi = rounddown(pindex, SWAP_META_PAGES); 1844f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, rdpi); 1845f425ab8eSKonstantin Belousov if (sb == NULL) { 18461c7c3c6aSMatthew Dillon if (swapblk == SWAPBLK_NONE) 1847*78f1deefSAlan Cox return (SWAPBLK_NONE); 1848f425ab8eSKonstantin Belousov for (;;) { 1849f425ab8eSKonstantin Belousov sb = uma_zalloc(swblk_zone, M_NOWAIT | (curproc == 1850f425ab8eSKonstantin Belousov pageproc ? M_USE_RESERVE : 0)); 1851f425ab8eSKonstantin Belousov if (sb != NULL) { 1852f425ab8eSKonstantin Belousov sb->p = rdpi; 1853f425ab8eSKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) 1854f425ab8eSKonstantin Belousov sb->d[i] = SWAPBLK_NONE; 1855f425ab8eSKonstantin Belousov if (atomic_cmpset_int(&swblk_zone_exhausted, 1856f425ab8eSKonstantin Belousov 1, 0)) 1857f425ab8eSKonstantin Belousov printf("swblk zone ok\n"); 1858f425ab8eSKonstantin Belousov break; 1859f425ab8eSKonstantin Belousov } 186089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1861f425ab8eSKonstantin Belousov if (uma_zone_exhausted(swblk_zone)) { 1862f425ab8eSKonstantin Belousov if (atomic_cmpset_int(&swblk_zone_exhausted, 1863f425ab8eSKonstantin Belousov 0, 1)) 1864f425ab8eSKonstantin Belousov printf("swap blk zone exhausted, " 18653ff863f1SDag-Erling Smørgrav "increase kern.maxswzone\n"); 18662025d69bSKonstantin Belousov vm_pageout_oom(VM_OOM_SWAPZ); 1867f425ab8eSKonstantin Belousov pause("swzonxb", 10); 18682025d69bSKonstantin Belousov } else 18698d6fbbb8SJeff Roberson uma_zwait(swblk_zone); 187089f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1871eed99cb8SKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, 1872eed99cb8SKonstantin Belousov rdpi); 1873eed99cb8SKonstantin Belousov if (sb != NULL) 1874eed99cb8SKonstantin Belousov /* 1875eed99cb8SKonstantin Belousov * Somebody swapped out a nearby page, 1876eed99cb8SKonstantin Belousov * allocating swblk at the rdpi index, 1877eed99cb8SKonstantin Belousov * while we dropped the object lock. 1878eed99cb8SKonstantin Belousov */ 1879eed99cb8SKonstantin Belousov goto allocated; 18804dcc5c2dSMatthew Dillon } 1881f425ab8eSKonstantin Belousov for (;;) { 1882f425ab8eSKonstantin Belousov error = SWAP_PCTRIE_INSERT( 1883f425ab8eSKonstantin Belousov &object->un_pager.swp.swp_blks, sb); 1884f425ab8eSKonstantin Belousov if (error == 0) { 1885f425ab8eSKonstantin Belousov if (atomic_cmpset_int(&swpctrie_zone_exhausted, 1886f425ab8eSKonstantin Belousov 1, 0)) 1887f425ab8eSKonstantin Belousov printf("swpctrie zone ok\n"); 1888f425ab8eSKonstantin Belousov break; 18891c7c3c6aSMatthew Dillon } 1890f425ab8eSKonstantin Belousov VM_OBJECT_WUNLOCK(object); 1891f425ab8eSKonstantin Belousov if (uma_zone_exhausted(swpctrie_zone)) { 1892f425ab8eSKonstantin Belousov if (atomic_cmpset_int(&swpctrie_zone_exhausted, 1893f425ab8eSKonstantin Belousov 0, 1)) 1894f425ab8eSKonstantin Belousov printf("swap pctrie zone exhausted, " 1895f425ab8eSKonstantin Belousov "increase kern.maxswzone\n"); 1896f425ab8eSKonstantin Belousov vm_pageout_oom(VM_OOM_SWAPZ); 1897f425ab8eSKonstantin Belousov pause("swzonxp", 10); 1898f425ab8eSKonstantin Belousov } else 18998d6fbbb8SJeff Roberson uma_zwait(swpctrie_zone); 1900f425ab8eSKonstantin Belousov VM_OBJECT_WLOCK(object); 1901eed99cb8SKonstantin Belousov sb1 = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, 1902eed99cb8SKonstantin Belousov rdpi); 1903eed99cb8SKonstantin Belousov if (sb1 != NULL) { 1904eed99cb8SKonstantin Belousov uma_zfree(swblk_zone, sb); 1905eed99cb8SKonstantin Belousov sb = sb1; 1906eed99cb8SKonstantin Belousov goto allocated; 19071c7c3c6aSMatthew Dillon } 1908f425ab8eSKonstantin Belousov } 1909eed99cb8SKonstantin Belousov } 1910eed99cb8SKonstantin Belousov allocated: 1911f425ab8eSKonstantin Belousov MPASS(sb->p == rdpi); 19121c7c3c6aSMatthew Dillon 1913f425ab8eSKonstantin Belousov modpi = pindex % SWAP_META_PAGES; 1914*78f1deefSAlan Cox /* Return prior contents of metadata. */ 1915*78f1deefSAlan Cox prev_swapblk = sb->d[modpi]; 1916f425ab8eSKonstantin Belousov /* Enter block into metadata. */ 1917f425ab8eSKonstantin Belousov sb->d[modpi] = swapblk; 191885d88d87SKonstantin Belousov 191985d88d87SKonstantin Belousov /* 192085d88d87SKonstantin Belousov * Free the swblk if we end up with the empty page run. 192185d88d87SKonstantin Belousov */ 1922230869e0SAlan Cox if (swapblk == SWAPBLK_NONE && 1923230869e0SAlan Cox swp_pager_swblk_empty(sb, 0, SWAP_META_PAGES)) { 1924230869e0SAlan Cox SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, rdpi); 192585d88d87SKonstantin Belousov uma_zfree(swblk_zone, sb); 192685d88d87SKonstantin Belousov } 1927*78f1deefSAlan Cox return (prev_swapblk); 192885d88d87SKonstantin Belousov } 19291c7c3c6aSMatthew Dillon 19301c7c3c6aSMatthew Dillon /* 19311c7c3c6aSMatthew Dillon * SWP_PAGER_META_FREE() - free a range of blocks in the object's swap metadata 19321c7c3c6aSMatthew Dillon * 19331c7c3c6aSMatthew Dillon * The requested range of blocks is freed, with any associated swap 19341c7c3c6aSMatthew Dillon * returned to the swap bitmap. 19351c7c3c6aSMatthew Dillon * 19361c7c3c6aSMatthew Dillon * This routine will free swap metadata structures as they are cleaned 19371c7c3c6aSMatthew Dillon * out. This routine does *NOT* operate on swap metadata associated 19381c7c3c6aSMatthew Dillon * with resident pages. 19391c7c3c6aSMatthew Dillon */ 19401c7c3c6aSMatthew Dillon static void 1941f425ab8eSKonstantin Belousov swp_pager_meta_free(vm_object_t object, vm_pindex_t pindex, vm_pindex_t count) 19421c7c3c6aSMatthew Dillon { 1943f425ab8eSKonstantin Belousov struct swblk *sb; 1944*78f1deefSAlan Cox daddr_t n_free, s_free; 1945f425ab8eSKonstantin Belousov vm_pindex_t last; 1946230869e0SAlan Cox int i, limit, start; 19472928cef7SAlan Cox 1948ee620ea4SAlan Cox VM_OBJECT_ASSERT_WLOCKED(object); 19492e56b64fSKonstantin Belousov if (object->type != OBJT_SWAP || count == 0) 19501c7c3c6aSMatthew Dillon return; 19511c7c3c6aSMatthew Dillon 1952*78f1deefSAlan Cox swp_pager_init_freerange(&s_free, &n_free); 1953230869e0SAlan Cox last = pindex + count; 1954f425ab8eSKonstantin Belousov for (;;) { 1955f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks, 1956f425ab8eSKonstantin Belousov rounddown(pindex, SWAP_META_PAGES)); 1957230869e0SAlan Cox if (sb == NULL || sb->p >= last) 19582e56b64fSKonstantin Belousov break; 1959230869e0SAlan Cox start = pindex > sb->p ? pindex - sb->p : 0; 1960230869e0SAlan Cox limit = last - sb->p < SWAP_META_PAGES ? last - sb->p : 1961230869e0SAlan Cox SWAP_META_PAGES; 1962230869e0SAlan Cox for (i = start; i < limit; i++) { 1963f425ab8eSKonstantin Belousov if (sb->d[i] == SWAPBLK_NONE) 1964f425ab8eSKonstantin Belousov continue; 1965*78f1deefSAlan Cox swp_pager_update_freerange(&s_free, &n_free, sb->d[i]); 1966230869e0SAlan Cox sb->d[i] = SWAPBLK_NONE; 1967230869e0SAlan Cox } 1968230869e0SAlan Cox if (swp_pager_swblk_empty(sb, 0, start) && 1969230869e0SAlan Cox swp_pager_swblk_empty(sb, limit, SWAP_META_PAGES)) { 1970f425ab8eSKonstantin Belousov SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, 1971f425ab8eSKonstantin Belousov sb->p); 1972f425ab8eSKonstantin Belousov uma_zfree(swblk_zone, sb); 19731c7c3c6aSMatthew Dillon } 1974230869e0SAlan Cox pindex = sb->p + SWAP_META_PAGES; 19751c7c3c6aSMatthew Dillon } 1976*78f1deefSAlan Cox swp_pager_freeswapspace(s_free, n_free); 19771c7c3c6aSMatthew Dillon } 19781c7c3c6aSMatthew Dillon 19791c7c3c6aSMatthew Dillon /* 19801c7c3c6aSMatthew Dillon * SWP_PAGER_META_FREE_ALL() - destroy all swap metadata associated with object 19811c7c3c6aSMatthew Dillon * 19821c7c3c6aSMatthew Dillon * This routine locates and destroys all swap metadata associated with 19831c7c3c6aSMatthew Dillon * an object. 19841c7c3c6aSMatthew Dillon */ 19851c7c3c6aSMatthew Dillon static void 19861c7c3c6aSMatthew Dillon swp_pager_meta_free_all(vm_object_t object) 19871c7c3c6aSMatthew Dillon { 1988f425ab8eSKonstantin Belousov struct swblk *sb; 1989*78f1deefSAlan Cox daddr_t n_free, s_free; 1990f425ab8eSKonstantin Belousov vm_pindex_t pindex; 199171057cd2SKonstantin Belousov int i; 19921c7c3c6aSMatthew Dillon 199389f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 19941c7c3c6aSMatthew Dillon if (object->type != OBJT_SWAP) 19951c7c3c6aSMatthew Dillon return; 19961c7c3c6aSMatthew Dillon 1997*78f1deefSAlan Cox swp_pager_init_freerange(&s_free, &n_free); 1998f425ab8eSKonstantin Belousov for (pindex = 0; (sb = SWAP_PCTRIE_LOOKUP_GE( 1999f425ab8eSKonstantin Belousov &object->un_pager.swp.swp_blks, pindex)) != NULL;) { 2000f425ab8eSKonstantin Belousov pindex = sb->p + SWAP_META_PAGES; 2001f425ab8eSKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) { 2002230869e0SAlan Cox if (sb->d[i] == SWAPBLK_NONE) 2003230869e0SAlan Cox continue; 2004*78f1deefSAlan Cox swp_pager_update_freerange(&s_free, &n_free, sb->d[i]); 20051c7c3c6aSMatthew Dillon } 2006f425ab8eSKonstantin Belousov SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, sb->p); 2007f425ab8eSKonstantin Belousov uma_zfree(swblk_zone, sb); 20081c7c3c6aSMatthew Dillon } 2009*78f1deefSAlan Cox swp_pager_freeswapspace(s_free, n_free); 20101c7c3c6aSMatthew Dillon } 20111c7c3c6aSMatthew Dillon 20121c7c3c6aSMatthew Dillon /* 20134abca9bbSAlan Cox * SWP_PAGER_METACTL() - misc control of swap meta data. 20141c7c3c6aSMatthew Dillon * 20154abca9bbSAlan Cox * This routine is capable of looking up, or removing swapblk 20164abca9bbSAlan Cox * assignments in the swap meta data. It returns the swapblk being 20174abca9bbSAlan Cox * looked-up, popped, or SWAPBLK_NONE if the block was invalid. 20181c7c3c6aSMatthew Dillon * 20191c7c3c6aSMatthew Dillon * When acting on a busy resident page and paging is in progress, we 20201c7c3c6aSMatthew Dillon * have to wait until paging is complete but otherwise can act on the 20211c7c3c6aSMatthew Dillon * busy page. 20221c7c3c6aSMatthew Dillon * 20234abca9bbSAlan Cox * SWM_POP remove from meta data but do not free it 20241c7c3c6aSMatthew Dillon */ 20251c7c3c6aSMatthew Dillon static daddr_t 20262f249180SPoul-Henning Kamp swp_pager_meta_ctl(vm_object_t object, vm_pindex_t pindex, int flags) 20272f249180SPoul-Henning Kamp { 2028f425ab8eSKonstantin Belousov struct swblk *sb; 20294dcc5c2dSMatthew Dillon daddr_t r1; 20304dcc5c2dSMatthew Dillon 20314abca9bbSAlan Cox if ((flags & SWM_POP) != 0) 2032ee620ea4SAlan Cox VM_OBJECT_ASSERT_WLOCKED(object); 2033ee620ea4SAlan Cox else 2034c25673ffSAttilio Rao VM_OBJECT_ASSERT_LOCKED(object); 2035ee620ea4SAlan Cox 20361c7c3c6aSMatthew Dillon /* 2037ee620ea4SAlan Cox * The meta data only exists if the object is OBJT_SWAP 20381c7c3c6aSMatthew Dillon * and even then might not be allocated yet. 20391c7c3c6aSMatthew Dillon */ 20404dcc5c2dSMatthew Dillon if (object->type != OBJT_SWAP) 20411c7c3c6aSMatthew Dillon return (SWAPBLK_NONE); 20421c7c3c6aSMatthew Dillon 2043f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, 2044f425ab8eSKonstantin Belousov rounddown(pindex, SWAP_META_PAGES)); 2045f425ab8eSKonstantin Belousov if (sb == NULL) 2046f425ab8eSKonstantin Belousov return (SWAPBLK_NONE); 2047f425ab8eSKonstantin Belousov r1 = sb->d[pindex % SWAP_META_PAGES]; 2048f425ab8eSKonstantin Belousov if (r1 == SWAPBLK_NONE) 2049f425ab8eSKonstantin Belousov return (SWAPBLK_NONE); 20504abca9bbSAlan Cox if ((flags & SWM_POP) != 0) { 2051f425ab8eSKonstantin Belousov sb->d[pindex % SWAP_META_PAGES] = SWAPBLK_NONE; 2052230869e0SAlan Cox if (swp_pager_swblk_empty(sb, 0, SWAP_META_PAGES)) { 2053f425ab8eSKonstantin Belousov SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, 2054f425ab8eSKonstantin Belousov rounddown(pindex, SWAP_META_PAGES)); 2055f425ab8eSKonstantin Belousov uma_zfree(swblk_zone, sb); 2056f425ab8eSKonstantin Belousov } 2057f425ab8eSKonstantin Belousov } 20581c7c3c6aSMatthew Dillon return (r1); 20591c7c3c6aSMatthew Dillon } 20601c7c3c6aSMatthew Dillon 2061e9c0cc15SPoul-Henning Kamp /* 206277d6fd97SKonstantin Belousov * Returns the least page index which is greater than or equal to the 206377d6fd97SKonstantin Belousov * parameter pindex and for which there is a swap block allocated. 206477d6fd97SKonstantin Belousov * Returns object's size if the object's type is not swap or if there 206577d6fd97SKonstantin Belousov * are no allocated swap blocks for the object after the requested 206677d6fd97SKonstantin Belousov * pindex. 206777d6fd97SKonstantin Belousov */ 206877d6fd97SKonstantin Belousov vm_pindex_t 206977d6fd97SKonstantin Belousov swap_pager_find_least(vm_object_t object, vm_pindex_t pindex) 207077d6fd97SKonstantin Belousov { 2071f425ab8eSKonstantin Belousov struct swblk *sb; 2072f425ab8eSKonstantin Belousov int i; 207377d6fd97SKonstantin Belousov 207477d6fd97SKonstantin Belousov VM_OBJECT_ASSERT_LOCKED(object); 2075f425ab8eSKonstantin Belousov if (object->type != OBJT_SWAP) 207677d6fd97SKonstantin Belousov return (object->size); 207777d6fd97SKonstantin Belousov 2078f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks, 2079f425ab8eSKonstantin Belousov rounddown(pindex, SWAP_META_PAGES)); 2080f425ab8eSKonstantin Belousov if (sb == NULL) 2081f425ab8eSKonstantin Belousov return (object->size); 2082f425ab8eSKonstantin Belousov if (sb->p < pindex) { 2083f425ab8eSKonstantin Belousov for (i = pindex % SWAP_META_PAGES; i < SWAP_META_PAGES; i++) { 2084f425ab8eSKonstantin Belousov if (sb->d[i] != SWAPBLK_NONE) 2085f425ab8eSKonstantin Belousov return (sb->p + i); 208677d6fd97SKonstantin Belousov } 2087f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks, 2088f425ab8eSKonstantin Belousov roundup(pindex, SWAP_META_PAGES)); 2089f425ab8eSKonstantin Belousov if (sb == NULL) 2090f425ab8eSKonstantin Belousov return (object->size); 209177d6fd97SKonstantin Belousov } 2092f425ab8eSKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) { 2093f425ab8eSKonstantin Belousov if (sb->d[i] != SWAPBLK_NONE) 2094f425ab8eSKonstantin Belousov return (sb->p + i); 209577d6fd97SKonstantin Belousov } 2096f425ab8eSKonstantin Belousov 2097f425ab8eSKonstantin Belousov /* 2098f425ab8eSKonstantin Belousov * We get here if a swblk is present in the trie but it 2099f425ab8eSKonstantin Belousov * doesn't map any blocks. 2100f425ab8eSKonstantin Belousov */ 2101f425ab8eSKonstantin Belousov MPASS(0); 2102f425ab8eSKonstantin Belousov return (object->size); 210377d6fd97SKonstantin Belousov } 210477d6fd97SKonstantin Belousov 210577d6fd97SKonstantin Belousov /* 2106e9c0cc15SPoul-Henning Kamp * System call swapon(name) enables swapping on device name, 2107e9c0cc15SPoul-Henning Kamp * which must be in the swdevsw. Return EBUSY 2108e9c0cc15SPoul-Henning Kamp * if already swapping on this device. 2109e9c0cc15SPoul-Henning Kamp */ 2110e9c0cc15SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_ 2111e9c0cc15SPoul-Henning Kamp struct swapon_args { 2112e9c0cc15SPoul-Henning Kamp char *name; 2113e9c0cc15SPoul-Henning Kamp }; 2114e9c0cc15SPoul-Henning Kamp #endif 2115e9c0cc15SPoul-Henning Kamp 2116e9c0cc15SPoul-Henning Kamp /* 2117e9c0cc15SPoul-Henning Kamp * MPSAFE 2118e9c0cc15SPoul-Henning Kamp */ 2119e9c0cc15SPoul-Henning Kamp /* ARGSUSED */ 2120e9c0cc15SPoul-Henning Kamp int 21218451d0ddSKip Macy sys_swapon(struct thread *td, struct swapon_args *uap) 2122e9c0cc15SPoul-Henning Kamp { 2123e9c0cc15SPoul-Henning Kamp struct vattr attr; 2124e9c0cc15SPoul-Henning Kamp struct vnode *vp; 2125e9c0cc15SPoul-Henning Kamp struct nameidata nd; 2126e9c0cc15SPoul-Henning Kamp int error; 2127e9c0cc15SPoul-Henning Kamp 2128acd3428bSRobert Watson error = priv_check(td, PRIV_SWAPON); 2129e9c0cc15SPoul-Henning Kamp if (error) 2130acd3428bSRobert Watson return (error); 2131e9c0cc15SPoul-Henning Kamp 213204533e1eSKonstantin Belousov sx_xlock(&swdev_syscall_lock); 2133e9c0cc15SPoul-Henning Kamp 2134e9c0cc15SPoul-Henning Kamp /* 2135e9c0cc15SPoul-Henning Kamp * Swap metadata may not fit in the KVM if we have physical 2136e9c0cc15SPoul-Henning Kamp * memory of >1GB. 2137e9c0cc15SPoul-Henning Kamp */ 2138f425ab8eSKonstantin Belousov if (swblk_zone == NULL) { 2139e9c0cc15SPoul-Henning Kamp error = ENOMEM; 2140e9c0cc15SPoul-Henning Kamp goto done; 2141e9c0cc15SPoul-Henning Kamp } 2142e9c0cc15SPoul-Henning Kamp 2143d9135e72SRobert Watson NDINIT(&nd, LOOKUP, ISOPEN | FOLLOW | AUDITVNODE1, UIO_USERSPACE, 2144d9135e72SRobert Watson uap->name, td); 2145e9c0cc15SPoul-Henning Kamp error = namei(&nd); 2146e9c0cc15SPoul-Henning Kamp if (error) 2147e9c0cc15SPoul-Henning Kamp goto done; 2148e9c0cc15SPoul-Henning Kamp 2149e9c0cc15SPoul-Henning Kamp NDFREE(&nd, NDF_ONLY_PNBUF); 2150e9c0cc15SPoul-Henning Kamp vp = nd.ni_vp; 2151e9c0cc15SPoul-Henning Kamp 215220da9c2eSPoul-Henning Kamp if (vn_isdisk(vp, &error)) { 215388ad2d7bSKonstantin Belousov error = swapongeom(vp); 215420da9c2eSPoul-Henning Kamp } else if (vp->v_type == VREG && 2155e9c0cc15SPoul-Henning Kamp (vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 && 21560359a12eSAttilio Rao (error = VOP_GETATTR(vp, &attr, td->td_ucred)) == 0) { 2157e9c0cc15SPoul-Henning Kamp /* 2158e9c0cc15SPoul-Henning Kamp * Allow direct swapping to NFS regular files in the same 2159e9c0cc15SPoul-Henning Kamp * way that nfs_mountroot() sets up diskless swapping. 2160e9c0cc15SPoul-Henning Kamp */ 216159efee01SPoul-Henning Kamp error = swaponvp(td, vp, attr.va_size / DEV_BSIZE); 2162e9c0cc15SPoul-Henning Kamp } 2163e9c0cc15SPoul-Henning Kamp 2164e9c0cc15SPoul-Henning Kamp if (error) 2165e9c0cc15SPoul-Henning Kamp vrele(vp); 2166e9c0cc15SPoul-Henning Kamp done: 216704533e1eSKonstantin Belousov sx_xunlock(&swdev_syscall_lock); 2168e9c0cc15SPoul-Henning Kamp return (error); 2169e9c0cc15SPoul-Henning Kamp } 2170e9c0cc15SPoul-Henning Kamp 21713ff863f1SDag-Erling Smørgrav /* 21723ff863f1SDag-Erling Smørgrav * Check that the total amount of swap currently configured does not 21733ff863f1SDag-Erling Smørgrav * exceed half the theoretical maximum. If it does, print a warning 217435872e79SKonstantin Belousov * message. 21753ff863f1SDag-Erling Smørgrav */ 217635872e79SKonstantin Belousov static void 217735872e79SKonstantin Belousov swapon_check_swzone(void) 21783ff863f1SDag-Erling Smørgrav { 217935872e79SKonstantin Belousov unsigned long maxpages, npages; 21803ff863f1SDag-Erling Smørgrav 218135872e79SKonstantin Belousov npages = swap_total / PAGE_SIZE; 21823ff863f1SDag-Erling Smørgrav /* absolute maximum we can handle assuming 100% efficiency */ 2183f425ab8eSKonstantin Belousov maxpages = uma_zone_get_max(swblk_zone) * SWAP_META_PAGES; 21843ff863f1SDag-Erling Smørgrav 21853ff863f1SDag-Erling Smørgrav /* recommend using no more than half that amount */ 21863ff863f1SDag-Erling Smørgrav if (npages > maxpages / 2) { 21873ff863f1SDag-Erling Smørgrav printf("warning: total configured swap (%lu pages) " 21883ff863f1SDag-Erling Smørgrav "exceeds maximum recommended amount (%lu pages).\n", 21899462305cSSergey Kandaurov npages, maxpages / 2); 21903ff863f1SDag-Erling Smørgrav printf("warning: increase kern.maxswzone " 21913ff863f1SDag-Erling Smørgrav "or reduce amount of swap.\n"); 21923ff863f1SDag-Erling Smørgrav } 21933ff863f1SDag-Erling Smørgrav } 21943ff863f1SDag-Erling Smørgrav 219559efee01SPoul-Henning Kamp static void 21962cc718a1SKonstantin Belousov swaponsomething(struct vnode *vp, void *id, u_long nblks, 21972cc718a1SKonstantin Belousov sw_strategy_t *strategy, sw_close_t *close, dev_t dev, int flags) 2198e9c0cc15SPoul-Henning Kamp { 21992d9974c1SAlan Cox struct swdevt *sp, *tsp; 2200e9c0cc15SPoul-Henning Kamp swblk_t dvbase; 22018f60c087SPoul-Henning Kamp u_long mblocks; 2202e9c0cc15SPoul-Henning Kamp 2203e9c0cc15SPoul-Henning Kamp /* 2204e9c0cc15SPoul-Henning Kamp * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks. 2205e9c0cc15SPoul-Henning Kamp * First chop nblks off to page-align it, then convert. 2206e9c0cc15SPoul-Henning Kamp * 2207e9c0cc15SPoul-Henning Kamp * sw->sw_nblks is in page-sized chunks now too. 2208e9c0cc15SPoul-Henning Kamp */ 2209e9c0cc15SPoul-Henning Kamp nblks &= ~(ctodb(1) - 1); 2210e9c0cc15SPoul-Henning Kamp nblks = dbtoc(nblks); 2211e9c0cc15SPoul-Henning Kamp 22126e903bd0SKonstantin Belousov /* 22136e903bd0SKonstantin Belousov * If we go beyond this, we get overflows in the radix 22146e903bd0SKonstantin Belousov * tree bitmap code. 22156e903bd0SKonstantin Belousov */ 22166e903bd0SKonstantin Belousov mblocks = 0x40000000 / BLIST_META_RADIX; 22176e903bd0SKonstantin Belousov if (nblks > mblocks) { 22186e903bd0SKonstantin Belousov printf( 22196e903bd0SKonstantin Belousov "WARNING: reducing swap size to maximum of %luMB per unit\n", 22206e903bd0SKonstantin Belousov mblocks / 1024 / 1024 * PAGE_SIZE); 22216e903bd0SKonstantin Belousov nblks = mblocks; 22226e903bd0SKonstantin Belousov } 22236e903bd0SKonstantin Belousov 22248f60c087SPoul-Henning Kamp sp = malloc(sizeof *sp, M_VMPGDATA, M_WAITOK | M_ZERO); 2225dee34ca4SPoul-Henning Kamp sp->sw_vp = vp; 2226dee34ca4SPoul-Henning Kamp sp->sw_id = id; 2227f3732fd1SPoul-Henning Kamp sp->sw_dev = dev; 22288d677ef9SPoul-Henning Kamp sp->sw_flags = 0; 2229e9c0cc15SPoul-Henning Kamp sp->sw_nblks = nblks; 2230e9c0cc15SPoul-Henning Kamp sp->sw_used = 0; 223159efee01SPoul-Henning Kamp sp->sw_strategy = strategy; 2232dee34ca4SPoul-Henning Kamp sp->sw_close = close; 22332cc718a1SKonstantin Belousov sp->sw_flags = flags; 2234e9c0cc15SPoul-Henning Kamp 2235c8c7ad92SKip Macy sp->sw_blist = blist_create(nblks, M_WAITOK); 2236e9c0cc15SPoul-Henning Kamp /* 2237ef3c5abdSPoul-Henning Kamp * Do not free the first two block in order to avoid overwriting 22388f60c087SPoul-Henning Kamp * any bsd label at the front of the partition 2239e9c0cc15SPoul-Henning Kamp */ 2240ef3c5abdSPoul-Henning Kamp blist_free(sp->sw_blist, 2, nblks - 2); 2241e9c0cc15SPoul-Henning Kamp 22422d9974c1SAlan Cox dvbase = 0; 224320da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 22442d9974c1SAlan Cox TAILQ_FOREACH(tsp, &swtailq, sw_list) { 22452d9974c1SAlan Cox if (tsp->sw_end >= dvbase) { 22462d9974c1SAlan Cox /* 22472d9974c1SAlan Cox * We put one uncovered page between the devices 22482d9974c1SAlan Cox * in order to definitively prevent any cross-device 22492d9974c1SAlan Cox * I/O requests 22502d9974c1SAlan Cox */ 22512d9974c1SAlan Cox dvbase = tsp->sw_end + 1; 22522d9974c1SAlan Cox } 22532d9974c1SAlan Cox } 22542d9974c1SAlan Cox sp->sw_first = dvbase; 22552d9974c1SAlan Cox sp->sw_end = dvbase + nblks; 22568f60c087SPoul-Henning Kamp TAILQ_INSERT_TAIL(&swtailq, sp, sw_list); 22578f60c087SPoul-Henning Kamp nswapdev++; 2258761097c8SAlan Cox swap_pager_avail += nblks - 2; 22593364c323SKonstantin Belousov swap_total += (vm_ooffset_t)nblks * PAGE_SIZE; 226035872e79SKonstantin Belousov swapon_check_swzone(); 2261d05bc129SAlan Cox swp_sizecheck(); 2262d05bc129SAlan Cox mtx_unlock(&sw_dev_mtx); 2263b1fd102eSMark Johnston EVENTHANDLER_INVOKE(swapon, sp); 226459efee01SPoul-Henning Kamp } 2265e9c0cc15SPoul-Henning Kamp 2266e9c0cc15SPoul-Henning Kamp /* 2267e9c0cc15SPoul-Henning Kamp * SYSCALL: swapoff(devname) 2268e9c0cc15SPoul-Henning Kamp * 2269e9c0cc15SPoul-Henning Kamp * Disable swapping on the given device. 2270dee34ca4SPoul-Henning Kamp * 2271dee34ca4SPoul-Henning Kamp * XXX: Badly designed system call: it should use a device index 2272dee34ca4SPoul-Henning Kamp * rather than filename as specification. We keep sw_vp around 2273dee34ca4SPoul-Henning Kamp * only to make this work. 2274e9c0cc15SPoul-Henning Kamp */ 2275e9c0cc15SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_ 2276e9c0cc15SPoul-Henning Kamp struct swapoff_args { 2277e9c0cc15SPoul-Henning Kamp char *name; 2278e9c0cc15SPoul-Henning Kamp }; 2279e9c0cc15SPoul-Henning Kamp #endif 2280e9c0cc15SPoul-Henning Kamp 2281e9c0cc15SPoul-Henning Kamp /* 2282e9c0cc15SPoul-Henning Kamp * MPSAFE 2283e9c0cc15SPoul-Henning Kamp */ 2284e9c0cc15SPoul-Henning Kamp /* ARGSUSED */ 2285e9c0cc15SPoul-Henning Kamp int 22868451d0ddSKip Macy sys_swapoff(struct thread *td, struct swapoff_args *uap) 2287e9c0cc15SPoul-Henning Kamp { 2288e9c0cc15SPoul-Henning Kamp struct vnode *vp; 2289e9c0cc15SPoul-Henning Kamp struct nameidata nd; 2290e9c0cc15SPoul-Henning Kamp struct swdevt *sp; 22918f60c087SPoul-Henning Kamp int error; 2292e9c0cc15SPoul-Henning Kamp 2293acd3428bSRobert Watson error = priv_check(td, PRIV_SWAPOFF); 2294e9c0cc15SPoul-Henning Kamp if (error) 22950909f38aSPawel Jakub Dawidek return (error); 2296e9c0cc15SPoul-Henning Kamp 229704533e1eSKonstantin Belousov sx_xlock(&swdev_syscall_lock); 2298e9c0cc15SPoul-Henning Kamp 2299d9135e72SRobert Watson NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, UIO_USERSPACE, uap->name, 2300d9135e72SRobert Watson td); 2301e9c0cc15SPoul-Henning Kamp error = namei(&nd); 2302e9c0cc15SPoul-Henning Kamp if (error) 2303e9c0cc15SPoul-Henning Kamp goto done; 2304e9c0cc15SPoul-Henning Kamp NDFREE(&nd, NDF_ONLY_PNBUF); 2305e9c0cc15SPoul-Henning Kamp vp = nd.ni_vp; 2306e9c0cc15SPoul-Henning Kamp 230720da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 23088f60c087SPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 2309dee34ca4SPoul-Henning Kamp if (sp->sw_vp == vp) 23100909f38aSPawel Jakub Dawidek break; 2311e9c0cc15SPoul-Henning Kamp } 231220da9c2eSPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 23130909f38aSPawel Jakub Dawidek if (sp == NULL) { 2314e9c0cc15SPoul-Henning Kamp error = EINVAL; 2315e9c0cc15SPoul-Henning Kamp goto done; 23160909f38aSPawel Jakub Dawidek } 231735918c55SChristian S.J. Peron error = swapoff_one(sp, td->td_ucred); 23180909f38aSPawel Jakub Dawidek done: 231904533e1eSKonstantin Belousov sx_xunlock(&swdev_syscall_lock); 23200909f38aSPawel Jakub Dawidek return (error); 23210909f38aSPawel Jakub Dawidek } 23220909f38aSPawel Jakub Dawidek 23230909f38aSPawel Jakub Dawidek static int 232435918c55SChristian S.J. Peron swapoff_one(struct swdevt *sp, struct ucred *cred) 23250909f38aSPawel Jakub Dawidek { 232603bdd65fSAlan Cox u_long nblks; 2327e9c0cc15SPoul-Henning Kamp #ifdef MAC 23280909f38aSPawel Jakub Dawidek int error; 2329e9c0cc15SPoul-Henning Kamp #endif 2330e9c0cc15SPoul-Henning Kamp 233104533e1eSKonstantin Belousov sx_assert(&swdev_syscall_lock, SA_XLOCKED); 23320909f38aSPawel Jakub Dawidek #ifdef MAC 2333cb05b60aSAttilio Rao (void) vn_lock(sp->sw_vp, LK_EXCLUSIVE | LK_RETRY); 233435918c55SChristian S.J. Peron error = mac_system_check_swapoff(cred, sp->sw_vp); 233522db15c0SAttilio Rao (void) VOP_UNLOCK(sp->sw_vp, 0); 23360909f38aSPawel Jakub Dawidek if (error != 0) 23370909f38aSPawel Jakub Dawidek return (error); 23380909f38aSPawel Jakub Dawidek #endif 2339e9c0cc15SPoul-Henning Kamp nblks = sp->sw_nblks; 2340e9c0cc15SPoul-Henning Kamp 2341e9c0cc15SPoul-Henning Kamp /* 2342e9c0cc15SPoul-Henning Kamp * We can turn off this swap device safely only if the 2343e9c0cc15SPoul-Henning Kamp * available virtual memory in the system will fit the amount 2344e9c0cc15SPoul-Henning Kamp * of data we will have to page back in, plus an epsilon so 2345e9c0cc15SPoul-Henning Kamp * the system doesn't become critically low on swap space. 2346e9c0cc15SPoul-Henning Kamp */ 2347e2068d0bSJeff Roberson if (vm_free_count() + swap_pager_avail < nblks + nswap_lowat) 23480909f38aSPawel Jakub Dawidek return (ENOMEM); 2349e9c0cc15SPoul-Henning Kamp 2350e9c0cc15SPoul-Henning Kamp /* 2351e9c0cc15SPoul-Henning Kamp * Prevent further allocations on this device. 2352e9c0cc15SPoul-Henning Kamp */ 23532928cef7SAlan Cox mtx_lock(&sw_dev_mtx); 2354e9c0cc15SPoul-Henning Kamp sp->sw_flags |= SW_CLOSING; 235503bdd65fSAlan Cox swap_pager_avail -= blist_fill(sp->sw_blist, 0, nblks); 23563364c323SKonstantin Belousov swap_total -= (vm_ooffset_t)nblks * PAGE_SIZE; 23572928cef7SAlan Cox mtx_unlock(&sw_dev_mtx); 2358e9c0cc15SPoul-Henning Kamp 2359e9c0cc15SPoul-Henning Kamp /* 2360e9c0cc15SPoul-Henning Kamp * Page in the contents of the device and close it. 2361e9c0cc15SPoul-Henning Kamp */ 2362b3fed13eSDavid Schultz swap_pager_swapoff(sp); 2363e9c0cc15SPoul-Henning Kamp 236435918c55SChristian S.J. Peron sp->sw_close(curthread, sp); 236520da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 23669e3e3fe5SWarner Losh sp->sw_id = NULL; 23678f60c087SPoul-Henning Kamp TAILQ_REMOVE(&swtailq, sp, sw_list); 23680676a140SAlan Cox nswapdev--; 23697dea2c2eSAlan Cox if (nswapdev == 0) { 23707dea2c2eSAlan Cox swap_pager_full = 2; 23717dea2c2eSAlan Cox swap_pager_almost_full = 1; 23727dea2c2eSAlan Cox } 23738f60c087SPoul-Henning Kamp if (swdevhd == sp) 23748f60c087SPoul-Henning Kamp swdevhd = NULL; 2375d05bc129SAlan Cox mtx_unlock(&sw_dev_mtx); 23768f60c087SPoul-Henning Kamp blist_destroy(sp->sw_blist); 23778f60c087SPoul-Henning Kamp free(sp, M_VMPGDATA); 23780909f38aSPawel Jakub Dawidek return (0); 23790909f38aSPawel Jakub Dawidek } 2380e9c0cc15SPoul-Henning Kamp 23810909f38aSPawel Jakub Dawidek void 23820909f38aSPawel Jakub Dawidek swapoff_all(void) 23830909f38aSPawel Jakub Dawidek { 23840909f38aSPawel Jakub Dawidek struct swdevt *sp, *spt; 23850909f38aSPawel Jakub Dawidek const char *devname; 23860909f38aSPawel Jakub Dawidek int error; 23870909f38aSPawel Jakub Dawidek 238804533e1eSKonstantin Belousov sx_xlock(&swdev_syscall_lock); 23890909f38aSPawel Jakub Dawidek 23900909f38aSPawel Jakub Dawidek mtx_lock(&sw_dev_mtx); 23910909f38aSPawel Jakub Dawidek TAILQ_FOREACH_SAFE(sp, &swtailq, sw_list, spt) { 23920909f38aSPawel Jakub Dawidek mtx_unlock(&sw_dev_mtx); 23930909f38aSPawel Jakub Dawidek if (vn_isdisk(sp->sw_vp, NULL)) 23947870adb6SEd Schouten devname = devtoname(sp->sw_vp->v_rdev); 23950909f38aSPawel Jakub Dawidek else 23960909f38aSPawel Jakub Dawidek devname = "[file]"; 239735918c55SChristian S.J. Peron error = swapoff_one(sp, thread0.td_ucred); 23980909f38aSPawel Jakub Dawidek if (error != 0) { 23990909f38aSPawel Jakub Dawidek printf("Cannot remove swap device %s (error=%d), " 24000909f38aSPawel Jakub Dawidek "skipping.\n", devname, error); 24010909f38aSPawel Jakub Dawidek } else if (bootverbose) { 24020909f38aSPawel Jakub Dawidek printf("Swap device %s removed.\n", devname); 24030909f38aSPawel Jakub Dawidek } 24040909f38aSPawel Jakub Dawidek mtx_lock(&sw_dev_mtx); 24050909f38aSPawel Jakub Dawidek } 24060909f38aSPawel Jakub Dawidek mtx_unlock(&sw_dev_mtx); 24070909f38aSPawel Jakub Dawidek 240804533e1eSKonstantin Belousov sx_xunlock(&swdev_syscall_lock); 2409e9c0cc15SPoul-Henning Kamp } 2410e9c0cc15SPoul-Henning Kamp 2411567104a1SPoul-Henning Kamp void 2412567104a1SPoul-Henning Kamp swap_pager_status(int *total, int *used) 2413567104a1SPoul-Henning Kamp { 2414567104a1SPoul-Henning Kamp struct swdevt *sp; 2415567104a1SPoul-Henning Kamp 2416567104a1SPoul-Henning Kamp *total = 0; 2417567104a1SPoul-Henning Kamp *used = 0; 241820da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 24198f60c087SPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 2420567104a1SPoul-Henning Kamp *total += sp->sw_nblks; 2421567104a1SPoul-Henning Kamp *used += sp->sw_used; 2422567104a1SPoul-Henning Kamp } 242320da9c2eSPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 2424567104a1SPoul-Henning Kamp } 2425567104a1SPoul-Henning Kamp 2426dda4f960SKonstantin Belousov int 2427dda4f960SKonstantin Belousov swap_dev_info(int name, struct xswdev *xs, char *devname, size_t len) 2428dda4f960SKonstantin Belousov { 2429dda4f960SKonstantin Belousov struct swdevt *sp; 24307870adb6SEd Schouten const char *tmp_devname; 2431dda4f960SKonstantin Belousov int error, n; 2432dda4f960SKonstantin Belousov 2433dda4f960SKonstantin Belousov n = 0; 2434dda4f960SKonstantin Belousov error = ENOENT; 2435dda4f960SKonstantin Belousov mtx_lock(&sw_dev_mtx); 2436dda4f960SKonstantin Belousov TAILQ_FOREACH(sp, &swtailq, sw_list) { 2437dda4f960SKonstantin Belousov if (n != name) { 2438dda4f960SKonstantin Belousov n++; 2439dda4f960SKonstantin Belousov continue; 2440dda4f960SKonstantin Belousov } 2441dda4f960SKonstantin Belousov xs->xsw_version = XSWDEV_VERSION; 2442dda4f960SKonstantin Belousov xs->xsw_dev = sp->sw_dev; 2443dda4f960SKonstantin Belousov xs->xsw_flags = sp->sw_flags; 2444dda4f960SKonstantin Belousov xs->xsw_nblks = sp->sw_nblks; 2445dda4f960SKonstantin Belousov xs->xsw_used = sp->sw_used; 2446dda4f960SKonstantin Belousov if (devname != NULL) { 2447dda4f960SKonstantin Belousov if (vn_isdisk(sp->sw_vp, NULL)) 24487870adb6SEd Schouten tmp_devname = devtoname(sp->sw_vp->v_rdev); 2449dda4f960SKonstantin Belousov else 2450dda4f960SKonstantin Belousov tmp_devname = "[file]"; 2451dda4f960SKonstantin Belousov strncpy(devname, tmp_devname, len); 2452dda4f960SKonstantin Belousov } 2453dda4f960SKonstantin Belousov error = 0; 2454dda4f960SKonstantin Belousov break; 2455dda4f960SKonstantin Belousov } 2456dda4f960SKonstantin Belousov mtx_unlock(&sw_dev_mtx); 2457dda4f960SKonstantin Belousov return (error); 2458dda4f960SKonstantin Belousov } 2459dda4f960SKonstantin Belousov 246069921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11) 246169921123SKonstantin Belousov #define XSWDEV_VERSION_11 1 246269921123SKonstantin Belousov struct xswdev11 { 246369921123SKonstantin Belousov u_int xsw_version; 246469921123SKonstantin Belousov uint32_t xsw_dev; 246569921123SKonstantin Belousov int xsw_flags; 246669921123SKonstantin Belousov int xsw_nblks; 246769921123SKonstantin Belousov int xsw_used; 246869921123SKonstantin Belousov }; 246969921123SKonstantin Belousov #endif 247069921123SKonstantin Belousov 2471e9c0cc15SPoul-Henning Kamp static int 2472e9c0cc15SPoul-Henning Kamp sysctl_vm_swap_info(SYSCTL_HANDLER_ARGS) 2473e9c0cc15SPoul-Henning Kamp { 2474e9c0cc15SPoul-Henning Kamp struct xswdev xs; 247569921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11) 247669921123SKonstantin Belousov struct xswdev11 xs11; 247769921123SKonstantin Belousov #endif 2478dda4f960SKonstantin Belousov int error; 2479e9c0cc15SPoul-Henning Kamp 2480e9c0cc15SPoul-Henning Kamp if (arg2 != 1) /* name length */ 2481e9c0cc15SPoul-Henning Kamp return (EINVAL); 2482dda4f960SKonstantin Belousov error = swap_dev_info(*(int *)arg1, &xs, NULL, 0); 2483dda4f960SKonstantin Belousov if (error != 0) 2484dda4f960SKonstantin Belousov return (error); 248569921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11) 248669921123SKonstantin Belousov if (req->oldlen == sizeof(xs11)) { 248769921123SKonstantin Belousov xs11.xsw_version = XSWDEV_VERSION_11; 248869921123SKonstantin Belousov xs11.xsw_dev = xs.xsw_dev; /* truncation */ 248969921123SKonstantin Belousov xs11.xsw_flags = xs.xsw_flags; 249069921123SKonstantin Belousov xs11.xsw_nblks = xs.xsw_nblks; 249169921123SKonstantin Belousov xs11.xsw_used = xs.xsw_used; 249269921123SKonstantin Belousov error = SYSCTL_OUT(req, &xs11, sizeof(xs11)); 249369921123SKonstantin Belousov } else 249469921123SKonstantin Belousov #endif 2495e9c0cc15SPoul-Henning Kamp error = SYSCTL_OUT(req, &xs, sizeof(xs)); 2496e9c0cc15SPoul-Henning Kamp return (error); 2497e9c0cc15SPoul-Henning Kamp } 2498e9c0cc15SPoul-Henning Kamp 24998f60c087SPoul-Henning Kamp SYSCTL_INT(_vm, OID_AUTO, nswapdev, CTLFLAG_RD, &nswapdev, 0, 2500e9c0cc15SPoul-Henning Kamp "Number of swap devices"); 25014c36e917SKonstantin Belousov SYSCTL_NODE(_vm, OID_AUTO, swap_info, CTLFLAG_RD | CTLFLAG_MPSAFE, 25024c36e917SKonstantin Belousov sysctl_vm_swap_info, 2503e9c0cc15SPoul-Henning Kamp "Swap statistics by device"); 2504ec38b344SPoul-Henning Kamp 2505ec38b344SPoul-Henning Kamp /* 2506f425ab8eSKonstantin Belousov * Count the approximate swap usage in pages for a vmspace. The 2507f425ab8eSKonstantin Belousov * shadowed or not yet copied on write swap blocks are not accounted. 2508ec38b344SPoul-Henning Kamp * The map must be locked. 2509ec38b344SPoul-Henning Kamp */ 25102860553aSRebecca Cran long 2511ec38b344SPoul-Henning Kamp vmspace_swap_count(struct vmspace *vmspace) 2512ec38b344SPoul-Henning Kamp { 251365d8409cSRebecca Cran vm_map_t map; 2514ec38b344SPoul-Henning Kamp vm_map_entry_t cur; 251565d8409cSRebecca Cran vm_object_t object; 2516f425ab8eSKonstantin Belousov struct swblk *sb; 2517f425ab8eSKonstantin Belousov vm_pindex_t e, pi; 2518f425ab8eSKonstantin Belousov long count; 2519f425ab8eSKonstantin Belousov int i; 252065d8409cSRebecca Cran 252165d8409cSRebecca Cran map = &vmspace->vm_map; 252265d8409cSRebecca Cran count = 0; 2523ec38b344SPoul-Henning Kamp 2524ec38b344SPoul-Henning Kamp for (cur = map->header.next; cur != &map->header; cur = cur->next) { 2525f425ab8eSKonstantin Belousov if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) != 0) 2526f425ab8eSKonstantin Belousov continue; 2527f425ab8eSKonstantin Belousov object = cur->object.vm_object; 2528f425ab8eSKonstantin Belousov if (object == NULL || object->type != OBJT_SWAP) 2529f425ab8eSKonstantin Belousov continue; 2530f425ab8eSKonstantin Belousov VM_OBJECT_RLOCK(object); 2531f425ab8eSKonstantin Belousov if (object->type != OBJT_SWAP) 2532f425ab8eSKonstantin Belousov goto unlock; 2533f425ab8eSKonstantin Belousov pi = OFF_TO_IDX(cur->offset); 2534f425ab8eSKonstantin Belousov e = pi + OFF_TO_IDX(cur->end - cur->start); 2535f425ab8eSKonstantin Belousov for (;; pi = sb->p + SWAP_META_PAGES) { 2536f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP_GE( 2537f425ab8eSKonstantin Belousov &object->un_pager.swp.swp_blks, pi); 2538f425ab8eSKonstantin Belousov if (sb == NULL || sb->p >= e) 2539f425ab8eSKonstantin Belousov break; 2540f425ab8eSKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) { 2541f425ab8eSKonstantin Belousov if (sb->p + i < e && 2542f425ab8eSKonstantin Belousov sb->d[i] != SWAPBLK_NONE) 2543f425ab8eSKonstantin Belousov count++; 2544ec38b344SPoul-Henning Kamp } 2545ec38b344SPoul-Henning Kamp } 2546f425ab8eSKonstantin Belousov unlock: 2547f425ab8eSKonstantin Belousov VM_OBJECT_RUNLOCK(object); 2548ec38b344SPoul-Henning Kamp } 2549ec38b344SPoul-Henning Kamp return (count); 2550ec38b344SPoul-Henning Kamp } 2551dee34ca4SPoul-Henning Kamp 2552dee34ca4SPoul-Henning Kamp /* 2553dee34ca4SPoul-Henning Kamp * GEOM backend 2554dee34ca4SPoul-Henning Kamp * 2555dee34ca4SPoul-Henning Kamp * Swapping onto disk devices. 2556dee34ca4SPoul-Henning Kamp * 2557dee34ca4SPoul-Henning Kamp */ 2558dee34ca4SPoul-Henning Kamp 25595721c9c7SPoul-Henning Kamp static g_orphan_t swapgeom_orphan; 25605721c9c7SPoul-Henning Kamp 2561dee34ca4SPoul-Henning Kamp static struct g_class g_swap_class = { 2562dee34ca4SPoul-Henning Kamp .name = "SWAP", 25635721c9c7SPoul-Henning Kamp .version = G_VERSION, 25645721c9c7SPoul-Henning Kamp .orphan = swapgeom_orphan, 2565dee34ca4SPoul-Henning Kamp }; 2566dee34ca4SPoul-Henning Kamp 2567dee34ca4SPoul-Henning Kamp DECLARE_GEOM_CLASS(g_swap_class, g_class); 2568dee34ca4SPoul-Henning Kamp 2569dee34ca4SPoul-Henning Kamp 2570dee34ca4SPoul-Henning Kamp static void 25713398491bSAlexander Motin swapgeom_close_ev(void *arg, int flags) 25723398491bSAlexander Motin { 25733398491bSAlexander Motin struct g_consumer *cp; 25743398491bSAlexander Motin 25753398491bSAlexander Motin cp = arg; 25763398491bSAlexander Motin g_access(cp, -1, -1, 0); 25773398491bSAlexander Motin g_detach(cp); 25783398491bSAlexander Motin g_destroy_consumer(cp); 25793398491bSAlexander Motin } 25803398491bSAlexander Motin 25819e3e3fe5SWarner Losh /* 25829e3e3fe5SWarner Losh * Add a reference to the g_consumer for an inflight transaction. 25839e3e3fe5SWarner Losh */ 25849e3e3fe5SWarner Losh static void 25859e3e3fe5SWarner Losh swapgeom_acquire(struct g_consumer *cp) 25869e3e3fe5SWarner Losh { 25879e3e3fe5SWarner Losh 25889e3e3fe5SWarner Losh mtx_assert(&sw_dev_mtx, MA_OWNED); 25899e3e3fe5SWarner Losh cp->index++; 25909e3e3fe5SWarner Losh } 25919e3e3fe5SWarner Losh 25929e3e3fe5SWarner Losh /* 25930c657d22SKonstantin Belousov * Remove a reference from the g_consumer. Post a close event if all 25940c657d22SKonstantin Belousov * references go away, since the function might be called from the 25950c657d22SKonstantin Belousov * biodone context. 25969e3e3fe5SWarner Losh */ 25979e3e3fe5SWarner Losh static void 25989e3e3fe5SWarner Losh swapgeom_release(struct g_consumer *cp, struct swdevt *sp) 25999e3e3fe5SWarner Losh { 26009e3e3fe5SWarner Losh 26019e3e3fe5SWarner Losh mtx_assert(&sw_dev_mtx, MA_OWNED); 26029e3e3fe5SWarner Losh cp->index--; 26039e3e3fe5SWarner Losh if (cp->index == 0) { 26049e3e3fe5SWarner Losh if (g_post_event(swapgeom_close_ev, cp, M_NOWAIT, NULL) == 0) 26059e3e3fe5SWarner Losh sp->sw_id = NULL; 26069e3e3fe5SWarner Losh } 26079e3e3fe5SWarner Losh } 26089e3e3fe5SWarner Losh 26093398491bSAlexander Motin static void 2610dee34ca4SPoul-Henning Kamp swapgeom_done(struct bio *bp2) 2611dee34ca4SPoul-Henning Kamp { 26123398491bSAlexander Motin struct swdevt *sp; 2613dee34ca4SPoul-Henning Kamp struct buf *bp; 26143398491bSAlexander Motin struct g_consumer *cp; 2615dee34ca4SPoul-Henning Kamp 2616dee34ca4SPoul-Henning Kamp bp = bp2->bio_caller2; 26173398491bSAlexander Motin cp = bp2->bio_from; 2618c5d3d25eSPoul-Henning Kamp bp->b_ioflags = bp2->bio_flags; 2619dee34ca4SPoul-Henning Kamp if (bp2->bio_error) 2620dee34ca4SPoul-Henning Kamp bp->b_ioflags |= BIO_ERROR; 2621c5d3d25eSPoul-Henning Kamp bp->b_resid = bp->b_bcount - bp2->bio_completed; 2622c5d3d25eSPoul-Henning Kamp bp->b_error = bp2->bio_error; 2623dee34ca4SPoul-Henning Kamp bufdone(bp); 26243398491bSAlexander Motin sp = bp2->bio_caller1; 26259e3e3fe5SWarner Losh mtx_lock(&sw_dev_mtx); 26269e3e3fe5SWarner Losh swapgeom_release(cp, sp); 26273398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 2628dee34ca4SPoul-Henning Kamp g_destroy_bio(bp2); 2629dee34ca4SPoul-Henning Kamp } 2630dee34ca4SPoul-Henning Kamp 2631dee34ca4SPoul-Henning Kamp static void 2632dee34ca4SPoul-Henning Kamp swapgeom_strategy(struct buf *bp, struct swdevt *sp) 2633dee34ca4SPoul-Henning Kamp { 2634dee34ca4SPoul-Henning Kamp struct bio *bio; 2635dee34ca4SPoul-Henning Kamp struct g_consumer *cp; 2636dee34ca4SPoul-Henning Kamp 26373398491bSAlexander Motin mtx_lock(&sw_dev_mtx); 2638dee34ca4SPoul-Henning Kamp cp = sp->sw_id; 2639dee34ca4SPoul-Henning Kamp if (cp == NULL) { 26403398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 2641dee34ca4SPoul-Henning Kamp bp->b_error = ENXIO; 2642dee34ca4SPoul-Henning Kamp bp->b_ioflags |= BIO_ERROR; 2643dee34ca4SPoul-Henning Kamp bufdone(bp); 2644dee34ca4SPoul-Henning Kamp return; 2645dee34ca4SPoul-Henning Kamp } 26469e3e3fe5SWarner Losh swapgeom_acquire(cp); 26473398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 264811041003SKonstantin Belousov if (bp->b_iocmd == BIO_WRITE) 264911041003SKonstantin Belousov bio = g_new_bio(); 265011041003SKonstantin Belousov else 26514f8205e5SPoul-Henning Kamp bio = g_alloc_bio(); 26524f8205e5SPoul-Henning Kamp if (bio == NULL) { 26539e3e3fe5SWarner Losh mtx_lock(&sw_dev_mtx); 26549e3e3fe5SWarner Losh swapgeom_release(cp, sp); 26559e3e3fe5SWarner Losh mtx_unlock(&sw_dev_mtx); 26563e5b6861SPoul-Henning Kamp bp->b_error = ENOMEM; 26573e5b6861SPoul-Henning Kamp bp->b_ioflags |= BIO_ERROR; 26583e5b6861SPoul-Henning Kamp bufdone(bp); 26593e5b6861SPoul-Henning Kamp return; 26603e5b6861SPoul-Henning Kamp } 266111041003SKonstantin Belousov 26623398491bSAlexander Motin bio->bio_caller1 = sp; 2663dee34ca4SPoul-Henning Kamp bio->bio_caller2 = bp; 2664c5d3d25eSPoul-Henning Kamp bio->bio_cmd = bp->b_iocmd; 2665dee34ca4SPoul-Henning Kamp bio->bio_offset = (bp->b_blkno - sp->sw_first) * PAGE_SIZE; 2666dee34ca4SPoul-Henning Kamp bio->bio_length = bp->b_bcount; 2667dee34ca4SPoul-Henning Kamp bio->bio_done = swapgeom_done; 2668fade8dd7SJeff Roberson if (!buf_mapped(bp)) { 26692cc718a1SKonstantin Belousov bio->bio_ma = bp->b_pages; 26702cc718a1SKonstantin Belousov bio->bio_data = unmapped_buf; 26712cc718a1SKonstantin Belousov bio->bio_ma_offset = (vm_offset_t)bp->b_offset & PAGE_MASK; 26722cc718a1SKonstantin Belousov bio->bio_ma_n = bp->b_npages; 26732cc718a1SKonstantin Belousov bio->bio_flags |= BIO_UNMAPPED; 26742cc718a1SKonstantin Belousov } else { 26752cc718a1SKonstantin Belousov bio->bio_data = bp->b_data; 26762cc718a1SKonstantin Belousov bio->bio_ma = NULL; 26772cc718a1SKonstantin Belousov } 2678dee34ca4SPoul-Henning Kamp g_io_request(bio, cp); 2679dee34ca4SPoul-Henning Kamp return; 2680dee34ca4SPoul-Henning Kamp } 2681dee34ca4SPoul-Henning Kamp 2682dee34ca4SPoul-Henning Kamp static void 2683dee34ca4SPoul-Henning Kamp swapgeom_orphan(struct g_consumer *cp) 2684dee34ca4SPoul-Henning Kamp { 2685dee34ca4SPoul-Henning Kamp struct swdevt *sp; 26863398491bSAlexander Motin int destroy; 2687dee34ca4SPoul-Henning Kamp 2688dee34ca4SPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 26893398491bSAlexander Motin TAILQ_FOREACH(sp, &swtailq, sw_list) { 26903398491bSAlexander Motin if (sp->sw_id == cp) { 26918f12d83aSAlexander Motin sp->sw_flags |= SW_CLOSING; 26923398491bSAlexander Motin break; 2693dee34ca4SPoul-Henning Kamp } 26943398491bSAlexander Motin } 26959e3e3fe5SWarner Losh /* 26969e3e3fe5SWarner Losh * Drop reference we were created with. Do directly since we're in a 26979e3e3fe5SWarner Losh * special context where we don't have to queue the call to 26989e3e3fe5SWarner Losh * swapgeom_close_ev(). 26999e3e3fe5SWarner Losh */ 27009e3e3fe5SWarner Losh cp->index--; 27013398491bSAlexander Motin destroy = ((sp != NULL) && (cp->index == 0)); 27023398491bSAlexander Motin if (destroy) 27033398491bSAlexander Motin sp->sw_id = NULL; 27043398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 27053398491bSAlexander Motin if (destroy) 27063398491bSAlexander Motin swapgeom_close_ev(cp, 0); 2707dee34ca4SPoul-Henning Kamp } 2708dee34ca4SPoul-Henning Kamp 2709dee34ca4SPoul-Henning Kamp static void 2710dee34ca4SPoul-Henning Kamp swapgeom_close(struct thread *td, struct swdevt *sw) 2711dee34ca4SPoul-Henning Kamp { 27123398491bSAlexander Motin struct g_consumer *cp; 2713dee34ca4SPoul-Henning Kamp 27143398491bSAlexander Motin mtx_lock(&sw_dev_mtx); 27153398491bSAlexander Motin cp = sw->sw_id; 27163398491bSAlexander Motin sw->sw_id = NULL; 27173398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 27180c657d22SKonstantin Belousov 27190c657d22SKonstantin Belousov /* 27200c657d22SKonstantin Belousov * swapgeom_close() may be called from the biodone context, 27210c657d22SKonstantin Belousov * where we cannot perform topology changes. Delegate the 27220c657d22SKonstantin Belousov * work to the events thread. 27230c657d22SKonstantin Belousov */ 27243398491bSAlexander Motin if (cp != NULL) 27253398491bSAlexander Motin g_waitfor_event(swapgeom_close_ev, cp, M_WAITOK, NULL); 2726dee34ca4SPoul-Henning Kamp } 2727dee34ca4SPoul-Henning Kamp 272888ad2d7bSKonstantin Belousov static int 272988ad2d7bSKonstantin Belousov swapongeom_locked(struct cdev *dev, struct vnode *vp) 2730dee34ca4SPoul-Henning Kamp { 2731dee34ca4SPoul-Henning Kamp struct g_provider *pp; 2732dee34ca4SPoul-Henning Kamp struct g_consumer *cp; 2733dee34ca4SPoul-Henning Kamp static struct g_geom *gp; 2734dee34ca4SPoul-Henning Kamp struct swdevt *sp; 2735dee34ca4SPoul-Henning Kamp u_long nblks; 2736dee34ca4SPoul-Henning Kamp int error; 2737dee34ca4SPoul-Henning Kamp 273888ad2d7bSKonstantin Belousov pp = g_dev_getprovider(dev); 273988ad2d7bSKonstantin Belousov if (pp == NULL) 274088ad2d7bSKonstantin Belousov return (ENODEV); 2741dee34ca4SPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 2742dee34ca4SPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 2743dee34ca4SPoul-Henning Kamp cp = sp->sw_id; 2744dee34ca4SPoul-Henning Kamp if (cp != NULL && cp->provider == pp) { 2745dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 274688ad2d7bSKonstantin Belousov return (EBUSY); 2747dee34ca4SPoul-Henning Kamp } 2748dee34ca4SPoul-Henning Kamp } 2749dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 27505721c9c7SPoul-Henning Kamp if (gp == NULL) 275102c62349SJaakko Heinonen gp = g_new_geomf(&g_swap_class, "swap"); 2752dee34ca4SPoul-Henning Kamp cp = g_new_consumer(gp); 27539e3e3fe5SWarner Losh cp->index = 1; /* Number of active I/Os, plus one for being active. */ 27549e3e3fe5SWarner Losh cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 2755dee34ca4SPoul-Henning Kamp g_attach(cp, pp); 2756afeb65e6SPoul-Henning Kamp /* 2757afeb65e6SPoul-Henning Kamp * XXX: Every time you think you can improve the margin for 2758afeb65e6SPoul-Henning Kamp * footshooting, somebody depends on the ability to do so: 2759afeb65e6SPoul-Henning Kamp * savecore(8) wants to write to our swapdev so we cannot 2760afeb65e6SPoul-Henning Kamp * set an exclusive count :-( 2761afeb65e6SPoul-Henning Kamp */ 2762d2bae332SPoul-Henning Kamp error = g_access(cp, 1, 1, 0); 276388ad2d7bSKonstantin Belousov if (error != 0) { 2764dee34ca4SPoul-Henning Kamp g_detach(cp); 2765dee34ca4SPoul-Henning Kamp g_destroy_consumer(cp); 276688ad2d7bSKonstantin Belousov return (error); 2767dee34ca4SPoul-Henning Kamp } 2768dee34ca4SPoul-Henning Kamp nblks = pp->mediasize / DEV_BSIZE; 276988ad2d7bSKonstantin Belousov swaponsomething(vp, cp, nblks, swapgeom_strategy, 277088ad2d7bSKonstantin Belousov swapgeom_close, dev2udev(dev), 27712cc718a1SKonstantin Belousov (pp->flags & G_PF_ACCEPT_UNMAPPED) != 0 ? SW_UNMAPPED : 0); 277288ad2d7bSKonstantin Belousov return (0); 2773dee34ca4SPoul-Henning Kamp } 2774dee34ca4SPoul-Henning Kamp 2775dee34ca4SPoul-Henning Kamp static int 277688ad2d7bSKonstantin Belousov swapongeom(struct vnode *vp) 2777dee34ca4SPoul-Henning Kamp { 2778dee34ca4SPoul-Henning Kamp int error; 2779dee34ca4SPoul-Henning Kamp 2780cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 278188ad2d7bSKonstantin Belousov if (vp->v_type != VCHR || (vp->v_iflag & VI_DOOMED) != 0) { 278288ad2d7bSKonstantin Belousov error = ENOENT; 278388ad2d7bSKonstantin Belousov } else { 278488ad2d7bSKonstantin Belousov g_topology_lock(); 278588ad2d7bSKonstantin Belousov error = swapongeom_locked(vp->v_rdev, vp); 278688ad2d7bSKonstantin Belousov g_topology_unlock(); 278788ad2d7bSKonstantin Belousov } 278822db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 2789dee34ca4SPoul-Henning Kamp return (error); 2790dee34ca4SPoul-Henning Kamp } 2791dee34ca4SPoul-Henning Kamp 2792dee34ca4SPoul-Henning Kamp /* 2793dee34ca4SPoul-Henning Kamp * VNODE backend 2794dee34ca4SPoul-Henning Kamp * 2795dee34ca4SPoul-Henning Kamp * This is used mainly for network filesystem (read: probably only tested 2796dee34ca4SPoul-Henning Kamp * with NFS) swapfiles. 2797dee34ca4SPoul-Henning Kamp * 2798dee34ca4SPoul-Henning Kamp */ 2799dee34ca4SPoul-Henning Kamp 2800dee34ca4SPoul-Henning Kamp static void 2801dee34ca4SPoul-Henning Kamp swapdev_strategy(struct buf *bp, struct swdevt *sp) 2802dee34ca4SPoul-Henning Kamp { 2803494eb176SPoul-Henning Kamp struct vnode *vp2; 2804dee34ca4SPoul-Henning Kamp 2805dee34ca4SPoul-Henning Kamp bp->b_blkno = ctodb(bp->b_blkno - sp->sw_first); 2806dee34ca4SPoul-Henning Kamp 2807dee34ca4SPoul-Henning Kamp vp2 = sp->sw_id; 2808dee34ca4SPoul-Henning Kamp vhold(vp2); 2809dee34ca4SPoul-Henning Kamp if (bp->b_iocmd == BIO_WRITE) { 28103cfc7651SOlivier Houchard if (bp->b_bufobj) 2811494eb176SPoul-Henning Kamp bufobj_wdrop(bp->b_bufobj); 2812a76d8f4eSPoul-Henning Kamp bufobj_wref(&vp2->v_bufobj); 2813dee34ca4SPoul-Henning Kamp } 28143cfc7651SOlivier Houchard if (bp->b_bufobj != &vp2->v_bufobj) 28153cfc7651SOlivier Houchard bp->b_bufobj = &vp2->v_bufobj; 2816dee34ca4SPoul-Henning Kamp bp->b_vp = vp2; 28172c18019fSPoul-Henning Kamp bp->b_iooffset = dbtob(bp->b_blkno); 2818b792bebeSPoul-Henning Kamp bstrategy(bp); 2819dee34ca4SPoul-Henning Kamp return; 2820dee34ca4SPoul-Henning Kamp } 2821dee34ca4SPoul-Henning Kamp 2822dee34ca4SPoul-Henning Kamp static void 2823dee34ca4SPoul-Henning Kamp swapdev_close(struct thread *td, struct swdevt *sp) 2824dee34ca4SPoul-Henning Kamp { 2825dee34ca4SPoul-Henning Kamp 2826dee34ca4SPoul-Henning Kamp VOP_CLOSE(sp->sw_vp, FREAD | FWRITE, td->td_ucred, td); 2827dee34ca4SPoul-Henning Kamp vrele(sp->sw_vp); 2828dee34ca4SPoul-Henning Kamp } 2829dee34ca4SPoul-Henning Kamp 2830dee34ca4SPoul-Henning Kamp 2831dee34ca4SPoul-Henning Kamp static int 2832dee34ca4SPoul-Henning Kamp swaponvp(struct thread *td, struct vnode *vp, u_long nblks) 2833dee34ca4SPoul-Henning Kamp { 2834dee34ca4SPoul-Henning Kamp struct swdevt *sp; 2835dee34ca4SPoul-Henning Kamp int error; 2836dee34ca4SPoul-Henning Kamp 2837dee34ca4SPoul-Henning Kamp if (nblks == 0) 2838dee34ca4SPoul-Henning Kamp return (ENXIO); 2839dee34ca4SPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 2840dee34ca4SPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 2841dee34ca4SPoul-Henning Kamp if (sp->sw_id == vp) { 2842dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 2843dee34ca4SPoul-Henning Kamp return (EBUSY); 2844dee34ca4SPoul-Henning Kamp } 2845dee34ca4SPoul-Henning Kamp } 2846dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 2847dee34ca4SPoul-Henning Kamp 2848cb05b60aSAttilio Rao (void) vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2849dee34ca4SPoul-Henning Kamp #ifdef MAC 285030d239bcSRobert Watson error = mac_system_check_swapon(td->td_ucred, vp); 2851dee34ca4SPoul-Henning Kamp if (error == 0) 2852dee34ca4SPoul-Henning Kamp #endif 28539e223287SKonstantin Belousov error = VOP_OPEN(vp, FREAD | FWRITE, td->td_ucred, td, NULL); 285422db15c0SAttilio Rao (void) VOP_UNLOCK(vp, 0); 2855dee34ca4SPoul-Henning Kamp if (error) 2856dee34ca4SPoul-Henning Kamp return (error); 2857dee34ca4SPoul-Henning Kamp 2858dee34ca4SPoul-Henning Kamp swaponsomething(vp, vp, nblks, swapdev_strategy, swapdev_close, 28592cc718a1SKonstantin Belousov NODEV, 0); 2860dee34ca4SPoul-Henning Kamp return (0); 2861dee34ca4SPoul-Henning Kamp } 286289c241d1SGleb Smirnoff 286389c241d1SGleb Smirnoff static int 286489c241d1SGleb Smirnoff sysctl_swap_async_max(SYSCTL_HANDLER_ARGS) 286589c241d1SGleb Smirnoff { 286689c241d1SGleb Smirnoff int error, new, n; 286789c241d1SGleb Smirnoff 286889c241d1SGleb Smirnoff new = nsw_wcount_async_max; 286989c241d1SGleb Smirnoff error = sysctl_handle_int(oidp, &new, 0, req); 287089c241d1SGleb Smirnoff if (error != 0 || req->newptr == NULL) 287189c241d1SGleb Smirnoff return (error); 287289c241d1SGleb Smirnoff 287389c241d1SGleb Smirnoff if (new > nswbuf / 2 || new < 1) 287489c241d1SGleb Smirnoff return (EINVAL); 287589c241d1SGleb Smirnoff 287689c241d1SGleb Smirnoff mtx_lock(&pbuf_mtx); 287789c241d1SGleb Smirnoff while (nsw_wcount_async_max != new) { 287889c241d1SGleb Smirnoff /* 287989c241d1SGleb Smirnoff * Adjust difference. If the current async count is too low, 288089c241d1SGleb Smirnoff * we will need to sqeeze our update slowly in. Sleep with a 288189c241d1SGleb Smirnoff * higher priority than getpbuf() to finish faster. 288289c241d1SGleb Smirnoff */ 288389c241d1SGleb Smirnoff n = new - nsw_wcount_async_max; 288489c241d1SGleb Smirnoff if (nsw_wcount_async + n >= 0) { 288589c241d1SGleb Smirnoff nsw_wcount_async += n; 288689c241d1SGleb Smirnoff nsw_wcount_async_max += n; 288789c241d1SGleb Smirnoff wakeup(&nsw_wcount_async); 288889c241d1SGleb Smirnoff } else { 288989c241d1SGleb Smirnoff nsw_wcount_async_max -= nsw_wcount_async; 289089c241d1SGleb Smirnoff nsw_wcount_async = 0; 289189c241d1SGleb Smirnoff msleep(&nsw_wcount_async, &pbuf_mtx, PSWP, 289289c241d1SGleb Smirnoff "swpsysctl", 0); 289389c241d1SGleb Smirnoff } 289489c241d1SGleb Smirnoff } 289589c241d1SGleb Smirnoff mtx_unlock(&pbuf_mtx); 289689c241d1SGleb Smirnoff 289789c241d1SGleb Smirnoff return (0); 289889c241d1SGleb Smirnoff } 2899