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 7469921123SKonstantin Belousov #include "opt_compat.h" 75e9c0cc15SPoul-Henning Kamp #include "opt_swap.h" 76e9c0cc15SPoul-Henning Kamp #include "opt_vm.h" 77e9c0cc15SPoul-Henning Kamp 78df8bae1dSRodney W. Grimes #include <sys/param.h> 79df8bae1dSRodney W. Grimes #include <sys/systm.h> 80af647ddeSBruce Evans #include <sys/conf.h> 8164abb5a5SDavid Greenman #include <sys/kernel.h> 82acd3428bSRobert Watson #include <sys/priv.h> 83df8bae1dSRodney W. Grimes #include <sys/proc.h> 849626b608SPoul-Henning Kamp #include <sys/bio.h> 85df8bae1dSRodney W. Grimes #include <sys/buf.h> 86e9c0cc15SPoul-Henning Kamp #include <sys/disk.h> 87e9c0cc15SPoul-Henning Kamp #include <sys/fcntl.h> 88e9c0cc15SPoul-Henning Kamp #include <sys/mount.h> 89e9c0cc15SPoul-Henning Kamp #include <sys/namei.h> 90df8bae1dSRodney W. Grimes #include <sys/vnode.h> 91df8bae1dSRodney W. Grimes #include <sys/malloc.h> 92f425ab8eSKonstantin Belousov #include <sys/pctrie.h> 931ba5ad42SEdward Tomasz Napierala #include <sys/racct.h> 943364c323SKonstantin Belousov #include <sys/resource.h> 953364c323SKonstantin Belousov #include <sys/resourcevar.h> 9689f6b863SAttilio Rao #include <sys/rwlock.h> 97d027ed2eSAlan Cox #include <sys/sbuf.h> 98327f4e83SMatthew Dillon #include <sys/sysctl.h> 99e9c0cc15SPoul-Henning Kamp #include <sys/sysproto.h> 1001c7c3c6aSMatthew Dillon #include <sys/blist.h> 1011c7c3c6aSMatthew Dillon #include <sys/lock.h> 1020cddd8f0SMatthew Dillon #include <sys/sx.h> 103936524aaSMatthew Dillon #include <sys/vmmeter.h> 104df8bae1dSRodney W. Grimes 105aed55708SRobert Watson #include <security/mac/mac_framework.h> 106aed55708SRobert Watson 107df8bae1dSRodney W. Grimes #include <vm/vm.h> 10821cd6e62SSeigo Tanimura #include <vm/pmap.h> 10921cd6e62SSeigo Tanimura #include <vm/vm_map.h> 11021cd6e62SSeigo Tanimura #include <vm/vm_kern.h> 111efeaf95aSDavid Greenman #include <vm/vm_object.h> 112df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 113efeaf95aSDavid Greenman #include <vm/vm_pager.h> 114df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h> 115e9c0cc15SPoul-Henning Kamp #include <vm/vm_param.h> 116df8bae1dSRodney W. Grimes #include <vm/swap_pager.h> 117efeaf95aSDavid Greenman #include <vm/vm_extern.h> 118670d17b5SJeff Roberson #include <vm/uma.h> 119df8bae1dSRodney W. Grimes 120dee34ca4SPoul-Henning Kamp #include <geom/geom.h> 121dee34ca4SPoul-Henning Kamp 122ec38b344SPoul-Henning Kamp /* 123064650c1SAlan Cox * MAX_PAGEOUT_CLUSTER must be a power of 2 between 1 and 64. 124064650c1SAlan Cox * The 64-page limit is due to the radix code (kern/subr_blist.c). 125ec38b344SPoul-Henning Kamp */ 126ec38b344SPoul-Henning Kamp #ifndef MAX_PAGEOUT_CLUSTER 127e2241590SAlan Cox #define MAX_PAGEOUT_CLUSTER 32 128ec38b344SPoul-Henning Kamp #endif 129ec38b344SPoul-Henning Kamp 130ec38b344SPoul-Henning Kamp #if !defined(SWB_NPAGES) 131ec38b344SPoul-Henning Kamp #define SWB_NPAGES MAX_PAGEOUT_CLUSTER 132ec38b344SPoul-Henning Kamp #endif 133ec38b344SPoul-Henning Kamp 134f425ab8eSKonstantin Belousov #define SWAP_META_PAGES PCTRIE_COUNT 135ec38b344SPoul-Henning Kamp 136f425ab8eSKonstantin Belousov /* 137f425ab8eSKonstantin Belousov * A swblk structure maps each page index within a 138f425ab8eSKonstantin Belousov * SWAP_META_PAGES-aligned and sized range to the address of an 139f425ab8eSKonstantin Belousov * on-disk swap block (or SWAPBLK_NONE). The collection of these 140f425ab8eSKonstantin Belousov * mappings for an entire vm object is implemented as a pc-trie. 141f425ab8eSKonstantin Belousov */ 142f425ab8eSKonstantin Belousov struct swblk { 143f425ab8eSKonstantin Belousov vm_pindex_t p; 144f425ab8eSKonstantin Belousov daddr_t d[SWAP_META_PAGES]; 145e9c0cc15SPoul-Henning Kamp }; 146e9c0cc15SPoul-Henning Kamp 1472c4992dbSAlan Cox static MALLOC_DEFINE(M_VMPGDATA, "vm_pgdata", "swap pager private data"); 14820da9c2eSPoul-Henning Kamp static struct mtx sw_dev_mtx; 1498f60c087SPoul-Henning Kamp static TAILQ_HEAD(, swdevt) swtailq = TAILQ_HEAD_INITIALIZER(swtailq); 1508f60c087SPoul-Henning Kamp static struct swdevt *swdevhd; /* Allocate from here next */ 1518f60c087SPoul-Henning Kamp static int nswapdev; /* Number of swap devices */ 1528f60c087SPoul-Henning Kamp int swap_pager_avail; 15304533e1eSKonstantin Belousov static struct sx swdev_syscall_lock; /* serialize swap(on|off) */ 154e9c0cc15SPoul-Henning Kamp 1553364c323SKonstantin Belousov static vm_ooffset_t swap_total; 1568a9c731fSIvan Voras SYSCTL_QUAD(_vm, OID_AUTO, swap_total, CTLFLAG_RD, &swap_total, 0, 1578a9c731fSIvan Voras "Total amount of available swap storage."); 1583364c323SKonstantin Belousov static vm_ooffset_t swap_reserved; 1598a9c731fSIvan Voras SYSCTL_QUAD(_vm, OID_AUTO, swap_reserved, CTLFLAG_RD, &swap_reserved, 0, 1608a9c731fSIvan Voras "Amount of swap storage needed to back all allocated anonymous memory."); 1613364c323SKonstantin Belousov static int overcommit = 0; 162be7d4ac5SEdward Tomasz Napierala SYSCTL_INT(_vm, VM_OVERCOMMIT, overcommit, CTLFLAG_RW, &overcommit, 0, 1638a9c731fSIvan Voras "Configure virtual memory overcommit behavior. See tuning(7) " 1648a9c731fSIvan Voras "for details."); 16561203277SDag-Erling Smørgrav static unsigned long swzone; 16661203277SDag-Erling Smørgrav SYSCTL_ULONG(_vm, OID_AUTO, swzone, CTLFLAG_RD, &swzone, 0, 16761203277SDag-Erling Smørgrav "Actual size of swap metadata zone"); 16861203277SDag-Erling Smørgrav static unsigned long swap_maxpages; 16961203277SDag-Erling Smørgrav SYSCTL_ULONG(_vm, OID_AUTO, swap_maxpages, CTLFLAG_RD, &swap_maxpages, 0, 17061203277SDag-Erling Smørgrav "Maximum amount of swap supported"); 1713364c323SKonstantin Belousov 1723364c323SKonstantin Belousov /* bits from overcommit */ 1733364c323SKonstantin Belousov #define SWAP_RESERVE_FORCE_ON (1 << 0) 1743364c323SKonstantin Belousov #define SWAP_RESERVE_RLIMIT_ON (1 << 1) 1753364c323SKonstantin Belousov #define SWAP_RESERVE_ALLOW_NONWIRED (1 << 2) 1763364c323SKonstantin Belousov 1773364c323SKonstantin Belousov int 1783364c323SKonstantin Belousov swap_reserve(vm_ooffset_t incr) 1793364c323SKonstantin Belousov { 1803364c323SKonstantin Belousov 181ef694c1aSEdward Tomasz Napierala return (swap_reserve_by_cred(incr, curthread->td_ucred)); 1823364c323SKonstantin Belousov } 1833364c323SKonstantin Belousov 1843364c323SKonstantin Belousov int 185ef694c1aSEdward Tomasz Napierala swap_reserve_by_cred(vm_ooffset_t incr, struct ucred *cred) 1863364c323SKonstantin Belousov { 1875c0e1c11SKonstantin Belousov vm_ooffset_t r, s; 1883364c323SKonstantin Belousov int res, error; 1893364c323SKonstantin Belousov static int curfail; 1903364c323SKonstantin Belousov static struct timeval lastfail; 191ef694c1aSEdward Tomasz Napierala struct uidinfo *uip; 192ef694c1aSEdward Tomasz Napierala 193ef694c1aSEdward Tomasz Napierala uip = cred->cr_ruidinfo; 1943364c323SKonstantin Belousov 1953364c323SKonstantin Belousov if (incr & PAGE_MASK) 1963364c323SKonstantin Belousov panic("swap_reserve: & PAGE_MASK"); 1973364c323SKonstantin Belousov 198afcc55f3SEdward Tomasz Napierala #ifdef RACCT 1994b5c9cf6SEdward Tomasz Napierala if (racct_enable) { 2001ba5ad42SEdward Tomasz Napierala PROC_LOCK(curproc); 2011ba5ad42SEdward Tomasz Napierala error = racct_add(curproc, RACCT_SWAP, incr); 2021ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(curproc); 2031ba5ad42SEdward Tomasz Napierala if (error != 0) 2041ba5ad42SEdward Tomasz Napierala return (0); 2054b5c9cf6SEdward Tomasz Napierala } 206afcc55f3SEdward Tomasz Napierala #endif 2071ba5ad42SEdward Tomasz Napierala 2083364c323SKonstantin Belousov res = 0; 2093364c323SKonstantin Belousov mtx_lock(&sw_dev_mtx); 2103364c323SKonstantin Belousov r = swap_reserved + incr; 2113364c323SKonstantin Belousov if (overcommit & SWAP_RESERVE_ALLOW_NONWIRED) { 212e958ad4cSJeff Roberson s = vm_cnt.v_page_count - vm_cnt.v_free_reserved - 213e958ad4cSJeff Roberson vm_wire_count(); 2143364c323SKonstantin Belousov s *= PAGE_SIZE; 2153364c323SKonstantin Belousov } else 2163364c323SKonstantin Belousov s = 0; 2173364c323SKonstantin Belousov s += swap_total; 2183364c323SKonstantin Belousov if ((overcommit & SWAP_RESERVE_FORCE_ON) == 0 || r <= s || 2193364c323SKonstantin Belousov (error = priv_check(curthread, PRIV_VM_SWAP_NOQUOTA)) == 0) { 2203364c323SKonstantin Belousov res = 1; 2213364c323SKonstantin Belousov swap_reserved = r; 2223364c323SKonstantin Belousov } 2233364c323SKonstantin Belousov mtx_unlock(&sw_dev_mtx); 2243364c323SKonstantin Belousov 2253364c323SKonstantin Belousov if (res) { 2263364c323SKonstantin Belousov UIDINFO_VMSIZE_LOCK(uip); 2275c0e1c11SKonstantin Belousov if ((overcommit & SWAP_RESERVE_RLIMIT_ON) != 0 && 228f6f6d240SMateusz Guzik uip->ui_vmsize + incr > lim_cur(curthread, RLIMIT_SWAP) && 2295c0e1c11SKonstantin Belousov priv_check(curthread, PRIV_VM_SWAP_NORLIMIT)) 2303364c323SKonstantin Belousov res = 0; 2313364c323SKonstantin Belousov else 2323364c323SKonstantin Belousov uip->ui_vmsize += incr; 2333364c323SKonstantin Belousov UIDINFO_VMSIZE_UNLOCK(uip); 2343364c323SKonstantin Belousov if (!res) { 2353364c323SKonstantin Belousov mtx_lock(&sw_dev_mtx); 2363364c323SKonstantin Belousov swap_reserved -= incr; 2373364c323SKonstantin Belousov mtx_unlock(&sw_dev_mtx); 2383364c323SKonstantin Belousov } 2393364c323SKonstantin Belousov } 2403364c323SKonstantin Belousov if (!res && ppsratecheck(&lastfail, &curfail, 1)) { 2413364c323SKonstantin Belousov printf("uid %d, pid %d: swap reservation for %jd bytes failed\n", 242134465d7SKonstantin Belousov uip->ui_uid, curproc->p_pid, incr); 2433364c323SKonstantin Belousov } 2443364c323SKonstantin Belousov 245afcc55f3SEdward Tomasz Napierala #ifdef RACCT 2461ba5ad42SEdward Tomasz Napierala if (!res) { 2471ba5ad42SEdward Tomasz Napierala PROC_LOCK(curproc); 2481ba5ad42SEdward Tomasz Napierala racct_sub(curproc, RACCT_SWAP, incr); 2491ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(curproc); 2501ba5ad42SEdward Tomasz Napierala } 251afcc55f3SEdward Tomasz Napierala #endif 2521ba5ad42SEdward Tomasz Napierala 2533364c323SKonstantin Belousov return (res); 2543364c323SKonstantin Belousov } 2553364c323SKonstantin Belousov 2563364c323SKonstantin Belousov void 2573364c323SKonstantin Belousov swap_reserve_force(vm_ooffset_t incr) 2583364c323SKonstantin Belousov { 2593364c323SKonstantin Belousov struct uidinfo *uip; 2603364c323SKonstantin Belousov 2613364c323SKonstantin Belousov mtx_lock(&sw_dev_mtx); 2623364c323SKonstantin Belousov swap_reserved += incr; 2633364c323SKonstantin Belousov mtx_unlock(&sw_dev_mtx); 2643364c323SKonstantin Belousov 265afcc55f3SEdward Tomasz Napierala #ifdef RACCT 2661ba5ad42SEdward Tomasz Napierala PROC_LOCK(curproc); 2671ba5ad42SEdward Tomasz Napierala racct_add_force(curproc, RACCT_SWAP, incr); 2681ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(curproc); 269afcc55f3SEdward Tomasz Napierala #endif 2701ba5ad42SEdward Tomasz Napierala 2713364c323SKonstantin Belousov uip = curthread->td_ucred->cr_ruidinfo; 2723364c323SKonstantin Belousov PROC_LOCK(curproc); 2733364c323SKonstantin Belousov UIDINFO_VMSIZE_LOCK(uip); 2743364c323SKonstantin Belousov uip->ui_vmsize += incr; 2753364c323SKonstantin Belousov UIDINFO_VMSIZE_UNLOCK(uip); 2763364c323SKonstantin Belousov PROC_UNLOCK(curproc); 2773364c323SKonstantin Belousov } 2783364c323SKonstantin Belousov 2793364c323SKonstantin Belousov void 2803364c323SKonstantin Belousov swap_release(vm_ooffset_t decr) 2813364c323SKonstantin Belousov { 282ef694c1aSEdward Tomasz Napierala struct ucred *cred; 2833364c323SKonstantin Belousov 2843364c323SKonstantin Belousov PROC_LOCK(curproc); 285ef694c1aSEdward Tomasz Napierala cred = curthread->td_ucred; 286ef694c1aSEdward Tomasz Napierala swap_release_by_cred(decr, cred); 2873364c323SKonstantin Belousov PROC_UNLOCK(curproc); 2883364c323SKonstantin Belousov } 2893364c323SKonstantin Belousov 2903364c323SKonstantin Belousov void 291ef694c1aSEdward Tomasz Napierala swap_release_by_cred(vm_ooffset_t decr, struct ucred *cred) 2923364c323SKonstantin Belousov { 293ef694c1aSEdward Tomasz Napierala struct uidinfo *uip; 294ef694c1aSEdward Tomasz Napierala 295ef694c1aSEdward Tomasz Napierala uip = cred->cr_ruidinfo; 2963364c323SKonstantin Belousov 2973364c323SKonstantin Belousov if (decr & PAGE_MASK) 2983364c323SKonstantin Belousov panic("swap_release: & PAGE_MASK"); 2993364c323SKonstantin Belousov 3003364c323SKonstantin Belousov mtx_lock(&sw_dev_mtx); 3013364c323SKonstantin Belousov if (swap_reserved < decr) 3023364c323SKonstantin Belousov panic("swap_reserved < decr"); 3033364c323SKonstantin Belousov swap_reserved -= decr; 3043364c323SKonstantin Belousov mtx_unlock(&sw_dev_mtx); 3053364c323SKonstantin Belousov 3063364c323SKonstantin Belousov UIDINFO_VMSIZE_LOCK(uip); 3073364c323SKonstantin Belousov if (uip->ui_vmsize < decr) 3083364c323SKonstantin Belousov printf("negative vmsize for uid = %d\n", uip->ui_uid); 3093364c323SKonstantin Belousov uip->ui_vmsize -= decr; 3103364c323SKonstantin Belousov UIDINFO_VMSIZE_UNLOCK(uip); 3111ba5ad42SEdward Tomasz Napierala 3121ba5ad42SEdward Tomasz Napierala racct_sub_cred(cred, RACCT_SWAP, decr); 3133364c323SKonstantin Belousov } 3143364c323SKonstantin Belousov 3154abca9bbSAlan Cox #define SWM_POP 0x01 /* pop out */ 31626f9a767SRodney W. Grimes 317f08b3099SKonstantin Belousov static int swap_pager_full = 2; /* swap space exhaustion (task killing) */ 3187dea2c2eSAlan Cox static int swap_pager_almost_full = 1; /* swap space exhaustion (w/hysteresis)*/ 3191c7c3c6aSMatthew Dillon static int nsw_rcount; /* free read buffers */ 320327f4e83SMatthew Dillon static int nsw_wcount_sync; /* limit write buffers / synchronous */ 321327f4e83SMatthew Dillon static int nsw_wcount_async; /* limit write buffers / asynchronous */ 322327f4e83SMatthew Dillon static int nsw_wcount_async_max;/* assigned maximum */ 323327f4e83SMatthew Dillon static int nsw_cluster_max; /* maximum VOP I/O allowed */ 3241c7c3c6aSMatthew Dillon 32589c241d1SGleb Smirnoff static int sysctl_swap_async_max(SYSCTL_HANDLER_ARGS); 3264c36e917SKonstantin Belousov SYSCTL_PROC(_vm, OID_AUTO, swap_async_max, CTLTYPE_INT | CTLFLAG_RW | 3274c36e917SKonstantin Belousov CTLFLAG_MPSAFE, NULL, 0, sysctl_swap_async_max, "I", 3284c36e917SKonstantin Belousov "Maximum running async swap ops"); 329d027ed2eSAlan Cox static int sysctl_swap_fragmentation(SYSCTL_HANDLER_ARGS); 330d027ed2eSAlan Cox SYSCTL_PROC(_vm, OID_AUTO, swap_fragmentation, CTLTYPE_STRING | CTLFLAG_RD | 331d027ed2eSAlan Cox CTLFLAG_MPSAFE, NULL, 0, sysctl_swap_fragmentation, "A", 332d027ed2eSAlan Cox "Swap Fragmentation Info"); 33389c241d1SGleb Smirnoff 3340cddd8f0SMatthew Dillon static struct sx sw_alloc_sx; 335327f4e83SMatthew Dillon 3361c7c3c6aSMatthew Dillon /* 3371c7c3c6aSMatthew Dillon * "named" and "unnamed" anon region objects. Try to reduce the overhead 3381c7c3c6aSMatthew Dillon * of searching a named list by hashing it just a little. 3391c7c3c6aSMatthew Dillon */ 3401c7c3c6aSMatthew Dillon 3411c7c3c6aSMatthew Dillon #define NOBJLISTS 8 3421c7c3c6aSMatthew Dillon 3431c7c3c6aSMatthew Dillon #define NOBJLIST(handle) \ 344af647ddeSBruce Evans (&swap_pager_object_list[((int)(intptr_t)handle >> 4) & (NOBJLISTS-1)]) 3451c7c3c6aSMatthew Dillon 3461c7c3c6aSMatthew Dillon static struct pagerlst swap_pager_object_list[NOBJLISTS]; 347f425ab8eSKonstantin Belousov static uma_zone_t swblk_zone; 348f425ab8eSKonstantin Belousov static uma_zone_t swpctrie_zone; 3491c7c3c6aSMatthew Dillon 3501c7c3c6aSMatthew Dillon /* 3511c7c3c6aSMatthew Dillon * pagerops for OBJT_SWAP - "swap pager". Some ops are also global procedure 3521c7c3c6aSMatthew Dillon * calls hooked from other parts of the VM system and do not appear here. 3531c7c3c6aSMatthew Dillon * (see vm/swap_pager.h). 3541c7c3c6aSMatthew Dillon */ 355ff98689dSBruce Evans static vm_object_t 35611caded3SAlfred Perlstein swap_pager_alloc(void *handle, vm_ooffset_t size, 3573364c323SKonstantin Belousov vm_prot_t prot, vm_ooffset_t offset, struct ucred *); 35811caded3SAlfred Perlstein static void swap_pager_dealloc(vm_object_t object); 359b0cd2017SGleb Smirnoff static int swap_pager_getpages(vm_object_t, vm_page_t *, int, int *, 360b0cd2017SGleb Smirnoff int *); 361b0cd2017SGleb Smirnoff static int swap_pager_getpages_async(vm_object_t, vm_page_t *, int, int *, 362b0cd2017SGleb Smirnoff int *, pgo_getpages_iodone_t, void *); 363751221fdSPoul-Henning Kamp static void swap_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *); 3645ea4972cSAlan Cox static boolean_t 3655ea4972cSAlan Cox swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, int *after); 36611caded3SAlfred Perlstein static void swap_pager_init(void); 36711caded3SAlfred Perlstein static void swap_pager_unswapped(vm_page_t); 368b3fed13eSDavid Schultz static void swap_pager_swapoff(struct swdevt *sp); 369f708ef1bSPoul-Henning Kamp 370df8bae1dSRodney W. Grimes struct pagerops swappagerops = { 371e04e4bacSPoul-Henning Kamp .pgo_init = swap_pager_init, /* early system initialization of pager */ 372e04e4bacSPoul-Henning Kamp .pgo_alloc = swap_pager_alloc, /* allocate an OBJT_SWAP object */ 373e04e4bacSPoul-Henning Kamp .pgo_dealloc = swap_pager_dealloc, /* deallocate an OBJT_SWAP object */ 374e04e4bacSPoul-Henning Kamp .pgo_getpages = swap_pager_getpages, /* pagein */ 37590effb23SGleb Smirnoff .pgo_getpages_async = swap_pager_getpages_async, /* pagein (async) */ 376e04e4bacSPoul-Henning Kamp .pgo_putpages = swap_pager_putpages, /* pageout */ 377e04e4bacSPoul-Henning Kamp .pgo_haspage = swap_pager_haspage, /* get backing store status for page */ 378e04e4bacSPoul-Henning Kamp .pgo_pageunswapped = swap_pager_unswapped, /* remove swap related to page */ 379df8bae1dSRodney W. Grimes }; 380df8bae1dSRodney W. Grimes 3811c7c3c6aSMatthew Dillon /* 3821c7c3c6aSMatthew Dillon * swap_*() routines are externally accessible. swp_*() routines are 3831c7c3c6aSMatthew Dillon * internal. 3841c7c3c6aSMatthew Dillon */ 385e9c0cc15SPoul-Henning Kamp static int nswap_lowat = 128; /* in pages, swap_pager_almost_full warn */ 386e9c0cc15SPoul-Henning Kamp static int nswap_hiwat = 512; /* in pages, swap_pager_almost_full warn */ 38726f9a767SRodney W. Grimes 38807c348eaSAlan Cox SYSCTL_INT(_vm, OID_AUTO, dmmax, CTLFLAG_RD, &nsw_cluster_max, 0, 38907c348eaSAlan Cox "Maximum size of a swap block in pages"); 390cee313c4SRobert Watson 391a5edd34aSPoul-Henning Kamp static void swp_sizecheck(void); 39211caded3SAlfred Perlstein static void swp_pager_async_iodone(struct buf *bp); 393230869e0SAlan Cox static bool swp_pager_swblk_empty(struct swblk *sb, int start, int limit); 39488ad2d7bSKonstantin Belousov static int swapongeom(struct vnode *); 39559efee01SPoul-Henning Kamp static int swaponvp(struct thread *, struct vnode *, u_long); 39635918c55SChristian S.J. Peron static int swapoff_one(struct swdevt *sp, struct ucred *cred); 39724a1cce3SDavid Greenman 3981c7c3c6aSMatthew Dillon /* 3991c7c3c6aSMatthew Dillon * Swap bitmap functions 4001c7c3c6aSMatthew Dillon */ 401230869e0SAlan Cox static void swp_pager_freeswapspace(daddr_t blk, daddr_t npages); 402a5edd34aSPoul-Henning Kamp static daddr_t swp_pager_getswapspace(int npages); 4031c7c3c6aSMatthew Dillon 4041c7c3c6aSMatthew Dillon /* 4051c7c3c6aSMatthew Dillon * Metadata functions 4061c7c3c6aSMatthew Dillon */ 40711caded3SAlfred Perlstein static void swp_pager_meta_build(vm_object_t, vm_pindex_t, daddr_t); 4082e56b64fSKonstantin Belousov static void swp_pager_meta_free(vm_object_t, vm_pindex_t, vm_pindex_t); 40911caded3SAlfred Perlstein static void swp_pager_meta_free_all(vm_object_t); 41011caded3SAlfred Perlstein static daddr_t swp_pager_meta_ctl(vm_object_t, vm_pindex_t, int); 4111c7c3c6aSMatthew Dillon 412f425ab8eSKonstantin Belousov static void * 413f425ab8eSKonstantin Belousov swblk_trie_alloc(struct pctrie *ptree) 414f425ab8eSKonstantin Belousov { 415f425ab8eSKonstantin Belousov 416f425ab8eSKonstantin Belousov return (uma_zalloc(swpctrie_zone, M_NOWAIT | (curproc == pageproc ? 417f425ab8eSKonstantin Belousov M_USE_RESERVE : 0))); 418f425ab8eSKonstantin Belousov } 419f425ab8eSKonstantin Belousov 420f425ab8eSKonstantin Belousov static void 421f425ab8eSKonstantin Belousov swblk_trie_free(struct pctrie *ptree, void *node) 422f425ab8eSKonstantin Belousov { 423f425ab8eSKonstantin Belousov 424f425ab8eSKonstantin Belousov uma_zfree(swpctrie_zone, node); 425f425ab8eSKonstantin Belousov } 426f425ab8eSKonstantin Belousov 427f425ab8eSKonstantin Belousov PCTRIE_DEFINE(SWAP, swblk, p, swblk_trie_alloc, swblk_trie_free); 428f425ab8eSKonstantin Belousov 4291c7c3c6aSMatthew Dillon /* 4301c7c3c6aSMatthew Dillon * SWP_SIZECHECK() - update swap_pager_full indication 4311c7c3c6aSMatthew Dillon * 43220d3034fSMatthew Dillon * update the swap_pager_almost_full indication and warn when we are 43320d3034fSMatthew Dillon * about to run out of swap space, using lowat/hiwat hysteresis. 43420d3034fSMatthew Dillon * 43520d3034fSMatthew Dillon * Clear swap_pager_full ( task killing ) indication when lowat is met. 4361c7c3c6aSMatthew Dillon * 4371c7c3c6aSMatthew Dillon * No restrictions on call 4381c7c3c6aSMatthew Dillon * This routine may not block. 4391c7c3c6aSMatthew Dillon */ 440a5edd34aSPoul-Henning Kamp static void 4412f249180SPoul-Henning Kamp swp_sizecheck(void) 4420d94caffSDavid Greenman { 44323955314SAlfred Perlstein 4448f60c087SPoul-Henning Kamp if (swap_pager_avail < nswap_lowat) { 44520d3034fSMatthew Dillon if (swap_pager_almost_full == 0) { 4461af87c92SDavid Greenman printf("swap_pager: out of swap space\n"); 44720d3034fSMatthew Dillon swap_pager_almost_full = 1; 4482b0d37a4SMatthew Dillon } 44920d3034fSMatthew Dillon } else { 45026f9a767SRodney W. Grimes swap_pager_full = 0; 4518f60c087SPoul-Henning Kamp if (swap_pager_avail > nswap_hiwat) 45220d3034fSMatthew Dillon swap_pager_almost_full = 0; 45326f9a767SRodney W. Grimes } 4541c7c3c6aSMatthew Dillon } 4551c7c3c6aSMatthew Dillon 4561c7c3c6aSMatthew Dillon /* 4571c7c3c6aSMatthew Dillon * SWAP_PAGER_INIT() - initialize the swap pager! 4581c7c3c6aSMatthew Dillon * 4591c7c3c6aSMatthew Dillon * Expected to be started from system init. NOTE: This code is run 4601c7c3c6aSMatthew Dillon * before much else so be careful what you depend on. Most of the VM 4611c7c3c6aSMatthew Dillon * system has yet to be initialized at this point. 4621c7c3c6aSMatthew Dillon */ 463f5a12711SPoul-Henning Kamp static void 4642f249180SPoul-Henning Kamp swap_pager_init(void) 465df8bae1dSRodney W. Grimes { 4661c7c3c6aSMatthew Dillon /* 4671c7c3c6aSMatthew Dillon * Initialize object lists 4681c7c3c6aSMatthew Dillon */ 4691c7c3c6aSMatthew Dillon int i; 4701c7c3c6aSMatthew Dillon 4711c7c3c6aSMatthew Dillon for (i = 0; i < NOBJLISTS; ++i) 4721c7c3c6aSMatthew Dillon TAILQ_INIT(&swap_pager_object_list[i]); 47320da9c2eSPoul-Henning Kamp mtx_init(&sw_dev_mtx, "swapdev", NULL, MTX_DEF); 47415719273SKonstantin Belousov sx_init(&sw_alloc_sx, "swspsx"); 47504533e1eSKonstantin Belousov sx_init(&swdev_syscall_lock, "swsysc"); 4761c7c3c6aSMatthew Dillon } 47726f9a767SRodney W. Grimes 478df8bae1dSRodney W. Grimes /* 4791c7c3c6aSMatthew Dillon * SWAP_PAGER_SWAP_INIT() - swap pager initialization from pageout process 4801c7c3c6aSMatthew Dillon * 4811c7c3c6aSMatthew Dillon * Expected to be started from pageout process once, prior to entering 4821c7c3c6aSMatthew Dillon * its main loop. 483df8bae1dSRodney W. Grimes */ 48424a1cce3SDavid Greenman void 4852f249180SPoul-Henning Kamp swap_pager_swap_init(void) 486df8bae1dSRodney W. Grimes { 48761203277SDag-Erling Smørgrav unsigned long n, n2; 4880d94caffSDavid Greenman 48926f9a767SRodney W. Grimes /* 4901c7c3c6aSMatthew Dillon * Number of in-transit swap bp operations. Don't 4911c7c3c6aSMatthew Dillon * exhaust the pbufs completely. Make sure we 4921c7c3c6aSMatthew Dillon * initialize workable values (0 will work for hysteresis 4931c7c3c6aSMatthew Dillon * but it isn't very efficient). 4941c7c3c6aSMatthew Dillon * 495327f4e83SMatthew Dillon * The nsw_cluster_max is constrained by the bp->b_pages[] 4961c7c3c6aSMatthew Dillon * array (MAXPHYS/PAGE_SIZE) and our locally defined 4971c7c3c6aSMatthew Dillon * MAX_PAGEOUT_CLUSTER. Also be aware that swap ops are 4981c7c3c6aSMatthew Dillon * constrained by the swap device interleave stripe size. 499327f4e83SMatthew Dillon * 500327f4e83SMatthew Dillon * Currently we hardwire nsw_wcount_async to 4. This limit is 501327f4e83SMatthew Dillon * designed to prevent other I/O from having high latencies due to 502327f4e83SMatthew Dillon * our pageout I/O. The value 4 works well for one or two active swap 503327f4e83SMatthew Dillon * devices but is probably a little low if you have more. Even so, 504327f4e83SMatthew Dillon * a higher value would probably generate only a limited improvement 505327f4e83SMatthew Dillon * with three or four active swap devices since the system does not 506327f4e83SMatthew Dillon * typically have to pageout at extreme bandwidths. We will want 507327f4e83SMatthew Dillon * at least 2 per swap devices, and 4 is a pretty good value if you 508327f4e83SMatthew Dillon * have one NFS swap device due to the command/ack latency over NFS. 509327f4e83SMatthew Dillon * So it all works out pretty well. 51026f9a767SRodney W. Grimes */ 511ad3cce20SMatthew Dillon nsw_cluster_max = min((MAXPHYS/PAGE_SIZE), MAX_PAGEOUT_CLUSTER); 512327f4e83SMatthew Dillon 5136d541bf1SJohn Baldwin mtx_lock(&pbuf_mtx); 5141c7c3c6aSMatthew Dillon nsw_rcount = (nswbuf + 1) / 2; 515327f4e83SMatthew Dillon nsw_wcount_sync = (nswbuf + 3) / 4; 516327f4e83SMatthew Dillon nsw_wcount_async = 4; 517327f4e83SMatthew Dillon nsw_wcount_async_max = nsw_wcount_async; 5186d541bf1SJohn Baldwin mtx_unlock(&pbuf_mtx); 51924a1cce3SDavid Greenman 5201c7c3c6aSMatthew Dillon /* 521f425ab8eSKonstantin Belousov * Initialize our zone, guessing on the number we need based 522f425ab8eSKonstantin Belousov * on the number of pages in the system. 5231c7c3c6aSMatthew Dillon */ 52444f1c916SBryan Drewery n = vm_cnt.v_page_count / 2; 525f425ab8eSKonstantin Belousov if (maxswzone && n > maxswzone / sizeof(struct swblk)) 526f425ab8eSKonstantin Belousov n = maxswzone / sizeof(struct swblk); 527f425ab8eSKonstantin Belousov swpctrie_zone = uma_zcreate("swpctrie", pctrie_node_size(), NULL, NULL, 528f425ab8eSKonstantin Belousov pctrie_zone_init, NULL, UMA_ALIGN_PTR, 529f425ab8eSKonstantin Belousov UMA_ZONE_NOFREE | UMA_ZONE_VM); 530f425ab8eSKonstantin Belousov if (swpctrie_zone == NULL) 531f425ab8eSKonstantin Belousov panic("failed to create swap pctrie zone."); 532f425ab8eSKonstantin Belousov swblk_zone = uma_zcreate("swblk", sizeof(struct swblk), NULL, NULL, 533f425ab8eSKonstantin Belousov NULL, NULL, _Alignof(struct swblk) - 1, 534f425ab8eSKonstantin Belousov UMA_ZONE_NOFREE | UMA_ZONE_VM); 535f425ab8eSKonstantin Belousov if (swblk_zone == NULL) 536f425ab8eSKonstantin Belousov panic("failed to create swap blk zone."); 53722a5e6b9SDag-Erling Smørgrav n2 = n; 5388355f576SJeff Roberson do { 539f425ab8eSKonstantin Belousov if (uma_zone_reserve_kva(swblk_zone, n)) 54061ce6eeeSAlfred Perlstein break; 54161ce6eeeSAlfred Perlstein /* 54261ce6eeeSAlfred Perlstein * if the allocation failed, try a zone two thirds the 54361ce6eeeSAlfred Perlstein * size of the previous attempt. 54461ce6eeeSAlfred Perlstein */ 54561ce6eeeSAlfred Perlstein n -= ((n + 2) / 3); 54661ce6eeeSAlfred Perlstein } while (n > 0); 54753faf5a7SKonstantin Belousov 54853faf5a7SKonstantin Belousov /* 54953faf5a7SKonstantin Belousov * Often uma_zone_reserve_kva() cannot reserve exactly the 55053faf5a7SKonstantin Belousov * requested size. Account for the difference when 55153faf5a7SKonstantin Belousov * calculating swap_maxpages. 55253faf5a7SKonstantin Belousov */ 55353faf5a7SKonstantin Belousov n = uma_zone_get_max(swblk_zone); 55453faf5a7SKonstantin Belousov 5551fffcd75SKonstantin Belousov if (n < n2) 556f425ab8eSKonstantin Belousov printf("Swap blk zone entries reduced from %lu to %lu.\n", 557f425ab8eSKonstantin Belousov n2, n); 55861203277SDag-Erling Smørgrav swap_maxpages = n * SWAP_META_PAGES; 559f425ab8eSKonstantin Belousov swzone = n * sizeof(struct swblk); 560f425ab8eSKonstantin Belousov if (!uma_zone_reserve_kva(swpctrie_zone, n)) 561f425ab8eSKonstantin Belousov printf("Cannot reserve swap pctrie zone, " 562f425ab8eSKonstantin Belousov "reduce kern.maxswzone.\n"); 56324a1cce3SDavid Greenman } 56424a1cce3SDavid Greenman 565eb4d6a1bSKonstantin Belousov static vm_object_t 566eb4d6a1bSKonstantin Belousov swap_pager_alloc_init(void *handle, struct ucred *cred, vm_ooffset_t size, 567eb4d6a1bSKonstantin Belousov vm_ooffset_t offset) 568eb4d6a1bSKonstantin Belousov { 569eb4d6a1bSKonstantin Belousov vm_object_t object; 570eb4d6a1bSKonstantin Belousov 571eb4d6a1bSKonstantin Belousov if (cred != NULL) { 572eb4d6a1bSKonstantin Belousov if (!swap_reserve_by_cred(size, cred)) 573eb4d6a1bSKonstantin Belousov return (NULL); 574eb4d6a1bSKonstantin Belousov crhold(cred); 575eb4d6a1bSKonstantin Belousov } 576f425ab8eSKonstantin Belousov 577f425ab8eSKonstantin Belousov /* 578f425ab8eSKonstantin Belousov * The un_pager.swp.swp_blks trie is initialized by 579f425ab8eSKonstantin Belousov * vm_object_allocate() to ensure the correct order of 580f425ab8eSKonstantin Belousov * visibility to other threads. 581f425ab8eSKonstantin Belousov */ 582eb4d6a1bSKonstantin Belousov object = vm_object_allocate(OBJT_SWAP, OFF_TO_IDX(offset + 583eb4d6a1bSKonstantin Belousov PAGE_MASK + size)); 584f425ab8eSKonstantin Belousov 585eb4d6a1bSKonstantin Belousov object->handle = handle; 586eb4d6a1bSKonstantin Belousov if (cred != NULL) { 587eb4d6a1bSKonstantin Belousov object->cred = cred; 588eb4d6a1bSKonstantin Belousov object->charge = size; 589eb4d6a1bSKonstantin Belousov } 590eb4d6a1bSKonstantin Belousov return (object); 591eb4d6a1bSKonstantin Belousov } 592eb4d6a1bSKonstantin Belousov 59324a1cce3SDavid Greenman /* 5941c7c3c6aSMatthew Dillon * SWAP_PAGER_ALLOC() - allocate a new OBJT_SWAP VM object and instantiate 5951c7c3c6aSMatthew Dillon * its metadata structures. 5961c7c3c6aSMatthew Dillon * 5971c7c3c6aSMatthew Dillon * This routine is called from the mmap and fork code to create a new 598eb4d6a1bSKonstantin Belousov * OBJT_SWAP object. 5991c7c3c6aSMatthew Dillon * 600eb4d6a1bSKonstantin Belousov * This routine must ensure that no live duplicate is created for 601eb4d6a1bSKonstantin Belousov * the named object request, which is protected against by 602eb4d6a1bSKonstantin Belousov * holding the sw_alloc_sx lock in case handle != NULL. 60324a1cce3SDavid Greenman */ 604f5a12711SPoul-Henning Kamp static vm_object_t 6056cde7a16SDavid Greenman swap_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 6063364c323SKonstantin Belousov vm_ooffset_t offset, struct ucred *cred) 60724a1cce3SDavid Greenman { 60824a1cce3SDavid Greenman vm_object_t object; 6092f7af3dbSAlan Cox 610eb4d6a1bSKonstantin Belousov if (handle != NULL) { 6111c7c3c6aSMatthew Dillon /* 6121c7c3c6aSMatthew Dillon * Reference existing named region or allocate new one. There 6131c7c3c6aSMatthew Dillon * should not be a race here against swp_pager_meta_build() 6141c7c3c6aSMatthew Dillon * as called from vm_page_remove() in regards to the lookup 6151c7c3c6aSMatthew Dillon * of the handle. 6161c7c3c6aSMatthew Dillon */ 6170cddd8f0SMatthew Dillon sx_xlock(&sw_alloc_sx); 6181c7c3c6aSMatthew Dillon object = vm_pager_object_lookup(NOBJLIST(handle), handle); 619b5e8f167SAlan Cox if (object == NULL) { 620eb4d6a1bSKonstantin Belousov object = swap_pager_alloc_init(handle, cred, size, 621eb4d6a1bSKonstantin Belousov offset); 622eb4d6a1bSKonstantin Belousov if (object != NULL) { 623eb4d6a1bSKonstantin Belousov TAILQ_INSERT_TAIL(NOBJLIST(object->handle), 624eb4d6a1bSKonstantin Belousov object, pager_object_list); 6253364c323SKonstantin Belousov } 62624a1cce3SDavid Greenman } 6270cddd8f0SMatthew Dillon sx_xunlock(&sw_alloc_sx); 62824a1cce3SDavid Greenman } else { 629eb4d6a1bSKonstantin Belousov object = swap_pager_alloc_init(handle, cred, size, offset); 63024a1cce3SDavid Greenman } 63124a1cce3SDavid Greenman return (object); 632df8bae1dSRodney W. Grimes } 633df8bae1dSRodney W. Grimes 63426f9a767SRodney W. Grimes /* 6351c7c3c6aSMatthew Dillon * SWAP_PAGER_DEALLOC() - remove swap metadata from object 6361c7c3c6aSMatthew Dillon * 6371c7c3c6aSMatthew Dillon * The swap backing for the object is destroyed. The code is 6381c7c3c6aSMatthew Dillon * designed such that we can reinstantiate it later, but this 6391c7c3c6aSMatthew Dillon * routine is typically called only when the entire object is 6401c7c3c6aSMatthew Dillon * about to be destroyed. 6411c7c3c6aSMatthew Dillon * 64215523cf7SKonstantin Belousov * The object must be locked. 64326f9a767SRodney W. Grimes */ 644df8bae1dSRodney W. Grimes static void 6452f249180SPoul-Henning Kamp swap_pager_dealloc(vm_object_t object) 64626f9a767SRodney W. Grimes { 6474dcc5c2dSMatthew Dillon 648eb4d6a1bSKonstantin Belousov VM_OBJECT_ASSERT_WLOCKED(object); 649eb4d6a1bSKonstantin Belousov KASSERT((object->flags & OBJ_DEAD) != 0, ("dealloc of reachable obj")); 650eb4d6a1bSKonstantin Belousov 65126f9a767SRodney W. Grimes /* 6521c7c3c6aSMatthew Dillon * Remove from list right away so lookups will fail if we block for 6531c7c3c6aSMatthew Dillon * pageout completion. 65426f9a767SRodney W. Grimes */ 655bd228075SAlan Cox if (object->handle != NULL) { 656eb4d6a1bSKonstantin Belousov VM_OBJECT_WUNLOCK(object); 657eb4d6a1bSKonstantin Belousov sx_xlock(&sw_alloc_sx); 658eb4d6a1bSKonstantin Belousov TAILQ_REMOVE(NOBJLIST(object->handle), object, 659eb4d6a1bSKonstantin Belousov pager_object_list); 660eb4d6a1bSKonstantin Belousov sx_xunlock(&sw_alloc_sx); 661eb4d6a1bSKonstantin Belousov VM_OBJECT_WLOCK(object); 662bd228075SAlan Cox } 6631c7c3c6aSMatthew Dillon 6641c7c3c6aSMatthew Dillon vm_object_pip_wait(object, "swpdea"); 6651c7c3c6aSMatthew Dillon 6661c7c3c6aSMatthew Dillon /* 6671c7c3c6aSMatthew Dillon * Free all remaining metadata. We only bother to free it from 6681c7c3c6aSMatthew Dillon * the swap meta data. We do not attempt to free swapblk's still 6691c7c3c6aSMatthew Dillon * associated with vm_page_t's for this object. We do not care 6701c7c3c6aSMatthew Dillon * if paging is still in progress on some objects. 6711c7c3c6aSMatthew Dillon */ 6721c7c3c6aSMatthew Dillon swp_pager_meta_free_all(object); 673e735691bSJohn Baldwin object->handle = NULL; 674e735691bSJohn Baldwin object->type = OBJT_DEAD; 6751c7c3c6aSMatthew Dillon } 6761c7c3c6aSMatthew Dillon 6771c7c3c6aSMatthew Dillon /************************************************************************ 6781c7c3c6aSMatthew Dillon * SWAP PAGER BITMAP ROUTINES * 6791c7c3c6aSMatthew Dillon ************************************************************************/ 6801c7c3c6aSMatthew Dillon 6811c7c3c6aSMatthew Dillon /* 6821c7c3c6aSMatthew Dillon * SWP_PAGER_GETSWAPSPACE() - allocate raw swap space 6831c7c3c6aSMatthew Dillon * 6841c7c3c6aSMatthew Dillon * Allocate swap for the requested number of pages. The starting 6851c7c3c6aSMatthew Dillon * swap block number (a page index) is returned or SWAPBLK_NONE 6861c7c3c6aSMatthew Dillon * if the allocation failed. 6871c7c3c6aSMatthew Dillon * 6881c7c3c6aSMatthew Dillon * Also has the side effect of advising that somebody made a mistake 6891c7c3c6aSMatthew Dillon * when they configured swap and didn't configure enough. 6901c7c3c6aSMatthew Dillon * 69115523cf7SKonstantin Belousov * This routine may not sleep. 6928f60c087SPoul-Henning Kamp * 6938f60c087SPoul-Henning Kamp * We allocate in round-robin fashion from the configured devices. 6941c7c3c6aSMatthew Dillon */ 695a5edd34aSPoul-Henning Kamp static daddr_t 6962f249180SPoul-Henning Kamp swp_pager_getswapspace(int npages) 6971c7c3c6aSMatthew Dillon { 6981c7c3c6aSMatthew Dillon daddr_t blk; 6998f60c087SPoul-Henning Kamp struct swdevt *sp; 7008f60c087SPoul-Henning Kamp int i; 7011c7c3c6aSMatthew Dillon 7028f60c087SPoul-Henning Kamp blk = SWAPBLK_NONE; 70320da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 7048f60c087SPoul-Henning Kamp sp = swdevhd; 7058f60c087SPoul-Henning Kamp for (i = 0; i < nswapdev; i++) { 7068f60c087SPoul-Henning Kamp if (sp == NULL) 7078f60c087SPoul-Henning Kamp sp = TAILQ_FIRST(&swtailq); 7088f60c087SPoul-Henning Kamp if (!(sp->sw_flags & SW_CLOSING)) { 7098f60c087SPoul-Henning Kamp blk = blist_alloc(sp->sw_blist, npages); 7108f60c087SPoul-Henning Kamp if (blk != SWAPBLK_NONE) { 7118f60c087SPoul-Henning Kamp blk += sp->sw_first; 7128f60c087SPoul-Henning Kamp sp->sw_used += npages; 713d05bc129SAlan Cox swap_pager_avail -= npages; 7148f60c087SPoul-Henning Kamp swp_sizecheck(); 7158f60c087SPoul-Henning Kamp swdevhd = TAILQ_NEXT(sp, sw_list); 716d05bc129SAlan Cox goto done; 7178f60c087SPoul-Henning Kamp } 7188f60c087SPoul-Henning Kamp } 7198f60c087SPoul-Henning Kamp sp = TAILQ_NEXT(sp, sw_list); 7208f60c087SPoul-Henning Kamp } 7212b0d37a4SMatthew Dillon if (swap_pager_full != 2) { 72220da9c2eSPoul-Henning Kamp printf("swap_pager_getswapspace(%d): failed\n", npages); 7232b0d37a4SMatthew Dillon swap_pager_full = 2; 72420d3034fSMatthew Dillon swap_pager_almost_full = 1; 7252b0d37a4SMatthew Dillon } 7268f60c087SPoul-Henning Kamp swdevhd = NULL; 727d05bc129SAlan Cox done: 728d05bc129SAlan Cox mtx_unlock(&sw_dev_mtx); 7291c7c3c6aSMatthew Dillon return (blk); 73026f9a767SRodney W. Grimes } 73126f9a767SRodney W. Grimes 732b3fed13eSDavid Schultz static int 733b3fed13eSDavid Schultz swp_pager_isondev(daddr_t blk, struct swdevt *sp) 7348f60c087SPoul-Henning Kamp { 7358f60c087SPoul-Henning Kamp 736b3fed13eSDavid Schultz return (blk >= sp->sw_first && blk < sp->sw_end); 7378f60c087SPoul-Henning Kamp } 7388f60c087SPoul-Henning Kamp 7394b03903aSPoul-Henning Kamp static void 7404b03903aSPoul-Henning Kamp swp_pager_strategy(struct buf *bp) 7414b03903aSPoul-Henning Kamp { 7424b03903aSPoul-Henning Kamp struct swdevt *sp; 7434b03903aSPoul-Henning Kamp 74420da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 7454b03903aSPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 7464b03903aSPoul-Henning Kamp if (bp->b_blkno >= sp->sw_first && bp->b_blkno < sp->sw_end) { 74720da9c2eSPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 7482cc718a1SKonstantin Belousov if ((sp->sw_flags & SW_UNMAPPED) != 0 && 7492cc718a1SKonstantin Belousov unmapped_buf_allowed) { 7502cc718a1SKonstantin Belousov bp->b_data = unmapped_buf; 7512cc718a1SKonstantin Belousov bp->b_offset = 0; 7522cc718a1SKonstantin Belousov } else { 7532cc718a1SKonstantin Belousov pmap_qenter((vm_offset_t)bp->b_data, 7542cc718a1SKonstantin Belousov &bp->b_pages[0], bp->b_bcount / PAGE_SIZE); 7552cc718a1SKonstantin Belousov } 7564b03903aSPoul-Henning Kamp sp->sw_strategy(bp, sp); 7574b03903aSPoul-Henning Kamp return; 7584b03903aSPoul-Henning Kamp } 7594b03903aSPoul-Henning Kamp } 76020da9c2eSPoul-Henning Kamp panic("Swapdev not found"); 7614b03903aSPoul-Henning Kamp } 7624b03903aSPoul-Henning Kamp 7638f60c087SPoul-Henning Kamp 76426f9a767SRodney W. Grimes /* 7651c7c3c6aSMatthew Dillon * SWP_PAGER_FREESWAPSPACE() - free raw swap space 7661c7c3c6aSMatthew Dillon * 7671c7c3c6aSMatthew Dillon * This routine returns the specified swap blocks back to the bitmap. 7681c7c3c6aSMatthew Dillon * 76915523cf7SKonstantin Belousov * This routine may not sleep. 77026f9a767SRodney W. Grimes */ 771a5edd34aSPoul-Henning Kamp static void 772230869e0SAlan Cox swp_pager_freeswapspace(daddr_t blk, daddr_t npages) 7730d94caffSDavid Greenman { 7748f60c087SPoul-Henning Kamp struct swdevt *sp; 77592da00bbSMatthew Dillon 776230869e0SAlan Cox if (npages == 0) 777230869e0SAlan Cox return; 7787645e885SAlan Cox mtx_lock(&sw_dev_mtx); 7797645e885SAlan Cox TAILQ_FOREACH(sp, &swtailq, sw_list) { 7807645e885SAlan Cox if (blk >= sp->sw_first && blk < sp->sw_end) { 78192da00bbSMatthew Dillon sp->sw_used -= npages; 78292da00bbSMatthew Dillon /* 7837645e885SAlan Cox * If we are attempting to stop swapping on 7847645e885SAlan Cox * this device, we don't want to mark any 7857645e885SAlan Cox * blocks free lest they be reused. 78692da00bbSMatthew Dillon */ 7877645e885SAlan Cox if ((sp->sw_flags & SW_CLOSING) == 0) { 7887645e885SAlan Cox blist_free(sp->sw_blist, blk - sp->sw_first, 7897645e885SAlan Cox npages); 7908f60c087SPoul-Henning Kamp swap_pager_avail += npages; 7911c7c3c6aSMatthew Dillon swp_sizecheck(); 79226f9a767SRodney W. Grimes } 7937645e885SAlan Cox mtx_unlock(&sw_dev_mtx); 7947645e885SAlan Cox return; 7957645e885SAlan Cox } 7967645e885SAlan Cox } 7977645e885SAlan Cox panic("Swapdev not found"); 7987645e885SAlan Cox } 7991c7c3c6aSMatthew Dillon 80026f9a767SRodney W. Grimes /* 801d027ed2eSAlan Cox * SYSCTL_SWAP_FRAGMENTATION() - produce raw swap space stats 802d027ed2eSAlan Cox */ 803d027ed2eSAlan Cox static int 804d027ed2eSAlan Cox sysctl_swap_fragmentation(SYSCTL_HANDLER_ARGS) 805d027ed2eSAlan Cox { 806d027ed2eSAlan Cox struct sbuf sbuf; 807d027ed2eSAlan Cox struct swdevt *sp; 808d027ed2eSAlan Cox const char *devname; 809d027ed2eSAlan Cox int error; 810d027ed2eSAlan Cox 811d027ed2eSAlan Cox error = sysctl_wire_old_buffer(req, 0); 812d027ed2eSAlan Cox if (error != 0) 813d027ed2eSAlan Cox return (error); 814d027ed2eSAlan Cox sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 815d027ed2eSAlan Cox mtx_lock(&sw_dev_mtx); 816d027ed2eSAlan Cox TAILQ_FOREACH(sp, &swtailq, sw_list) { 817d027ed2eSAlan Cox if (vn_isdisk(sp->sw_vp, NULL)) 818d027ed2eSAlan Cox devname = devtoname(sp->sw_vp->v_rdev); 819d027ed2eSAlan Cox else 820d027ed2eSAlan Cox devname = "[file]"; 821d027ed2eSAlan Cox sbuf_printf(&sbuf, "\nFree space on device %s:\n", devname); 822d027ed2eSAlan Cox blist_stats(sp->sw_blist, &sbuf); 823d027ed2eSAlan Cox } 824d027ed2eSAlan Cox mtx_unlock(&sw_dev_mtx); 825d027ed2eSAlan Cox error = sbuf_finish(&sbuf); 826d027ed2eSAlan Cox sbuf_delete(&sbuf); 827d027ed2eSAlan Cox return (error); 828d027ed2eSAlan Cox } 829d027ed2eSAlan Cox 830d027ed2eSAlan Cox /* 8311c7c3c6aSMatthew Dillon * SWAP_PAGER_FREESPACE() - frees swap blocks associated with a page 8321c7c3c6aSMatthew Dillon * range within an object. 8331c7c3c6aSMatthew Dillon * 8341c7c3c6aSMatthew Dillon * This is a globally accessible routine. 8351c7c3c6aSMatthew Dillon * 8361c7c3c6aSMatthew Dillon * This routine removes swapblk assignments from swap metadata. 8371c7c3c6aSMatthew Dillon * 8381c7c3c6aSMatthew Dillon * The external callers of this routine typically have already destroyed 8391c7c3c6aSMatthew Dillon * or renamed vm_page_t's associated with this range in the object so 8401c7c3c6aSMatthew Dillon * we should be ok. 841c25673ffSAttilio Rao * 842c25673ffSAttilio Rao * The object must be locked. 84326f9a767SRodney W. Grimes */ 84426f9a767SRodney W. Grimes void 8452f249180SPoul-Henning Kamp swap_pager_freespace(vm_object_t object, vm_pindex_t start, vm_size_t size) 84626f9a767SRodney W. Grimes { 84723955314SAlfred Perlstein 8481c7c3c6aSMatthew Dillon swp_pager_meta_free(object, start, size); 8494dcc5c2dSMatthew Dillon } 8504dcc5c2dSMatthew Dillon 8514dcc5c2dSMatthew Dillon /* 8524dcc5c2dSMatthew Dillon * SWAP_PAGER_RESERVE() - reserve swap blocks in object 8534dcc5c2dSMatthew Dillon * 8544dcc5c2dSMatthew Dillon * Assigns swap blocks to the specified range within the object. The 85556ce850bSKonstantin Belousov * swap blocks are not zeroed. Any previous swap assignment is destroyed. 8564dcc5c2dSMatthew Dillon * 8574dcc5c2dSMatthew Dillon * Returns 0 on success, -1 on failure. 8584dcc5c2dSMatthew Dillon */ 8594dcc5c2dSMatthew Dillon int 8604dcc5c2dSMatthew Dillon swap_pager_reserve(vm_object_t object, vm_pindex_t start, vm_size_t size) 8614dcc5c2dSMatthew Dillon { 8624dcc5c2dSMatthew Dillon int n = 0; 8634dcc5c2dSMatthew Dillon daddr_t blk = SWAPBLK_NONE; 8644dcc5c2dSMatthew Dillon vm_pindex_t beg = start; /* save start index */ 8654dcc5c2dSMatthew Dillon 86689f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 8674dcc5c2dSMatthew Dillon while (size) { 8684dcc5c2dSMatthew Dillon if (n == 0) { 8694dcc5c2dSMatthew Dillon n = BLIST_MAX_ALLOC; 8704dcc5c2dSMatthew Dillon while ((blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE) { 8714dcc5c2dSMatthew Dillon n >>= 1; 8724dcc5c2dSMatthew Dillon if (n == 0) { 8734dcc5c2dSMatthew Dillon swp_pager_meta_free(object, beg, start - beg); 87489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 8754dcc5c2dSMatthew Dillon return (-1); 8764dcc5c2dSMatthew Dillon } 8774dcc5c2dSMatthew Dillon } 8784dcc5c2dSMatthew Dillon } 8794dcc5c2dSMatthew Dillon swp_pager_meta_build(object, start, blk); 8804dcc5c2dSMatthew Dillon --size; 8814dcc5c2dSMatthew Dillon ++start; 8824dcc5c2dSMatthew Dillon ++blk; 8834dcc5c2dSMatthew Dillon --n; 8844dcc5c2dSMatthew Dillon } 8854dcc5c2dSMatthew Dillon swp_pager_meta_free(object, start, n); 88689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 8874dcc5c2dSMatthew Dillon return (0); 88826f9a767SRodney W. Grimes } 88926f9a767SRodney W. Grimes 8900a47b48bSJohn Dyson /* 8911c7c3c6aSMatthew Dillon * SWAP_PAGER_COPY() - copy blocks from source pager to destination pager 8921c7c3c6aSMatthew Dillon * and destroy the source. 8931c7c3c6aSMatthew Dillon * 8941c7c3c6aSMatthew Dillon * Copy any valid swapblks from the source to the destination. In 8951c7c3c6aSMatthew Dillon * cases where both the source and destination have a valid swapblk, 8961c7c3c6aSMatthew Dillon * we keep the destination's. 8971c7c3c6aSMatthew Dillon * 89815523cf7SKonstantin Belousov * This routine is allowed to sleep. It may sleep allocating metadata 8991c7c3c6aSMatthew Dillon * indirectly through swp_pager_meta_build() or if paging is still in 9001c7c3c6aSMatthew Dillon * progress on the source. 9011c7c3c6aSMatthew Dillon * 9021c7c3c6aSMatthew Dillon * The source object contains no vm_page_t's (which is just as well) 9031c7c3c6aSMatthew Dillon * 9041c7c3c6aSMatthew Dillon * The source object is of type OBJT_SWAP. 9051c7c3c6aSMatthew Dillon * 90615523cf7SKonstantin Belousov * The source and destination objects must be locked. 90715523cf7SKonstantin Belousov * Both object locks may temporarily be released. 90826f9a767SRodney W. Grimes */ 90926f9a767SRodney W. Grimes void 9102f249180SPoul-Henning Kamp swap_pager_copy(vm_object_t srcobject, vm_object_t dstobject, 9112f249180SPoul-Henning Kamp vm_pindex_t offset, int destroysource) 91226f9a767SRodney W. Grimes { 913a316d390SJohn Dyson vm_pindex_t i; 9144abca9bbSAlan Cox daddr_t dstaddr, first_free, num_free, srcaddr; 9154dcc5c2dSMatthew Dillon 91689f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(srcobject); 91789f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(dstobject); 9180cddd8f0SMatthew Dillon 91926f9a767SRodney W. Grimes /* 9201c7c3c6aSMatthew Dillon * If destroysource is set, we remove the source object from the 9211c7c3c6aSMatthew Dillon * swap_pager internal queue now. 92226f9a767SRodney W. Grimes */ 923eb4d6a1bSKonstantin Belousov if (destroysource && srcobject->handle != NULL) { 924eb4d6a1bSKonstantin Belousov vm_object_pip_add(srcobject, 1); 925eb4d6a1bSKonstantin Belousov VM_OBJECT_WUNLOCK(srcobject); 926eb4d6a1bSKonstantin Belousov vm_object_pip_add(dstobject, 1); 927eb4d6a1bSKonstantin Belousov VM_OBJECT_WUNLOCK(dstobject); 928eb4d6a1bSKonstantin Belousov sx_xlock(&sw_alloc_sx); 929eb4d6a1bSKonstantin Belousov TAILQ_REMOVE(NOBJLIST(srcobject->handle), srcobject, 930eb4d6a1bSKonstantin Belousov pager_object_list); 931eb4d6a1bSKonstantin Belousov sx_xunlock(&sw_alloc_sx); 932eb4d6a1bSKonstantin Belousov VM_OBJECT_WLOCK(dstobject); 933eb4d6a1bSKonstantin Belousov vm_object_pip_wakeup(dstobject); 934eb4d6a1bSKonstantin Belousov VM_OBJECT_WLOCK(srcobject); 935eb4d6a1bSKonstantin Belousov vm_object_pip_wakeup(srcobject); 936bd228075SAlan Cox } 93726f9a767SRodney W. Grimes 9381c7c3c6aSMatthew Dillon /* 9394abca9bbSAlan Cox * Transfer source to destination. 9401c7c3c6aSMatthew Dillon */ 9414abca9bbSAlan Cox first_free = SWAPBLK_NONE; 9424abca9bbSAlan Cox num_free = 0; 9431c7c3c6aSMatthew Dillon for (i = 0; i < dstobject->size; ++i) { 9444abca9bbSAlan Cox srcaddr = swp_pager_meta_ctl(srcobject, i + offset, SWM_POP); 9454abca9bbSAlan Cox if (srcaddr == SWAPBLK_NONE) 9464abca9bbSAlan Cox continue; 9471c7c3c6aSMatthew Dillon dstaddr = swp_pager_meta_ctl(dstobject, i, 0); 9481c7c3c6aSMatthew Dillon if (dstaddr == SWAPBLK_NONE) { 9491c7c3c6aSMatthew Dillon /* 9501c7c3c6aSMatthew Dillon * Destination has no swapblk and is not resident, 9511c7c3c6aSMatthew Dillon * copy source. 9524abca9bbSAlan Cox * 953c7c8dd7eSAlan Cox * swp_pager_meta_build() can sleep. 954c7c8dd7eSAlan Cox */ 955c7c8dd7eSAlan Cox vm_object_pip_add(srcobject, 1); 95689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(srcobject); 957c7c8dd7eSAlan Cox vm_object_pip_add(dstobject, 1); 9584dcc5c2dSMatthew Dillon swp_pager_meta_build(dstobject, i, srcaddr); 959c7c8dd7eSAlan Cox vm_object_pip_wakeup(dstobject); 96089f6b863SAttilio Rao VM_OBJECT_WLOCK(srcobject); 961c7c8dd7eSAlan Cox vm_object_pip_wakeup(srcobject); 9621c7c3c6aSMatthew Dillon } else { 9631c7c3c6aSMatthew Dillon /* 9641c7c3c6aSMatthew Dillon * Destination has valid swapblk or it is represented 9651c7c3c6aSMatthew Dillon * by a resident page. We destroy the sourceblock. 9661c7c3c6aSMatthew Dillon */ 9674abca9bbSAlan Cox if (first_free + num_free == srcaddr) 9684abca9bbSAlan Cox num_free++; 9694abca9bbSAlan Cox else { 9704abca9bbSAlan Cox swp_pager_freeswapspace(first_free, num_free); 9714abca9bbSAlan Cox first_free = srcaddr; 9724abca9bbSAlan Cox num_free = 1; 9731c7c3c6aSMatthew Dillon } 97426f9a767SRodney W. Grimes } 9754abca9bbSAlan Cox } 9764abca9bbSAlan Cox swp_pager_freeswapspace(first_free, num_free); 97726f9a767SRodney W. Grimes 97826f9a767SRodney W. Grimes /* 9791c7c3c6aSMatthew Dillon * Free left over swap blocks in source. 9801c7c3c6aSMatthew Dillon * 981763df3ecSPedro F. Giffuni * We have to revert the type to OBJT_DEFAULT so we do not accidentally 9821c7c3c6aSMatthew Dillon * double-remove the object from the swap queues. 98326f9a767SRodney W. Grimes */ 984c0877f10SJohn Dyson if (destroysource) { 9851c7c3c6aSMatthew Dillon swp_pager_meta_free_all(srcobject); 9861c7c3c6aSMatthew Dillon /* 9871c7c3c6aSMatthew Dillon * Reverting the type is not necessary, the caller is going 9881c7c3c6aSMatthew Dillon * to destroy srcobject directly, but I'm doing it here 989956f3135SPhilippe Charnier * for consistency since we've removed the object from its 9901c7c3c6aSMatthew Dillon * queues. 9911c7c3c6aSMatthew Dillon */ 9921c7c3c6aSMatthew Dillon srcobject->type = OBJT_DEFAULT; 993c0877f10SJohn Dyson } 99426f9a767SRodney W. Grimes } 99526f9a767SRodney W. Grimes 996df8bae1dSRodney W. Grimes /* 9971c7c3c6aSMatthew Dillon * SWAP_PAGER_HASPAGE() - determine if we have good backing store for 9981c7c3c6aSMatthew Dillon * the requested page. 9991c7c3c6aSMatthew Dillon * 10001c7c3c6aSMatthew Dillon * We determine whether good backing store exists for the requested 10011c7c3c6aSMatthew Dillon * page and return TRUE if it does, FALSE if it doesn't. 10021c7c3c6aSMatthew Dillon * 10031c7c3c6aSMatthew Dillon * If TRUE, we also try to determine how much valid, contiguous backing 1004915d1b71SMark Johnston * store exists before and after the requested page. 1005df8bae1dSRodney W. Grimes */ 10065ea4972cSAlan Cox static boolean_t 1007915d1b71SMark Johnston swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, 1008915d1b71SMark Johnston int *after) 100926f9a767SRodney W. Grimes { 1010915d1b71SMark Johnston daddr_t blk, blk0; 1011915d1b71SMark Johnston int i; 101226f9a767SRodney W. Grimes 1013c25673ffSAttilio Rao VM_OBJECT_ASSERT_LOCKED(object); 1014915d1b71SMark Johnston 10151c7c3c6aSMatthew Dillon /* 10161c7c3c6aSMatthew Dillon * do we have good backing store at the requested index ? 10171c7c3c6aSMatthew Dillon */ 10181c7c3c6aSMatthew Dillon blk0 = swp_pager_meta_ctl(object, pindex, 0); 10194dcc5c2dSMatthew Dillon if (blk0 == SWAPBLK_NONE) { 10201c7c3c6aSMatthew Dillon if (before) 102124a1cce3SDavid Greenman *before = 0; 10221c7c3c6aSMatthew Dillon if (after) 102324a1cce3SDavid Greenman *after = 0; 102426f9a767SRodney W. Grimes return (FALSE); 102526f9a767SRodney W. Grimes } 102626f9a767SRodney W. Grimes 102726f9a767SRodney W. Grimes /* 10281c7c3c6aSMatthew Dillon * find backwards-looking contiguous good backing store 1029e47ed70bSJohn Dyson */ 10301c7c3c6aSMatthew Dillon if (before != NULL) { 1031915d1b71SMark Johnston for (i = 1; i < SWB_NPAGES; i++) { 10321c7c3c6aSMatthew Dillon if (i > pindex) 10331c7c3c6aSMatthew Dillon break; 10341c7c3c6aSMatthew Dillon blk = swp_pager_meta_ctl(object, pindex - i, 0); 10351c7c3c6aSMatthew Dillon if (blk != blk0 - i) 10361c7c3c6aSMatthew Dillon break; 1037ffc82b0aSJohn Dyson } 1038915d1b71SMark Johnston *before = i - 1; 103926f9a767SRodney W. Grimes } 104026f9a767SRodney W. Grimes 104126f9a767SRodney W. Grimes /* 10421c7c3c6aSMatthew Dillon * find forward-looking contiguous good backing store 104326f9a767SRodney W. Grimes */ 10441c7c3c6aSMatthew Dillon if (after != NULL) { 1045915d1b71SMark Johnston for (i = 1; i < SWB_NPAGES; i++) { 10461c7c3c6aSMatthew Dillon blk = swp_pager_meta_ctl(object, pindex + i, 0); 10471c7c3c6aSMatthew Dillon if (blk != blk0 + i) 10481c7c3c6aSMatthew Dillon break; 104926f9a767SRodney W. Grimes } 1050915d1b71SMark Johnston *after = i - 1; 10511c7c3c6aSMatthew Dillon } 10521c7c3c6aSMatthew Dillon return (TRUE); 10531c7c3c6aSMatthew Dillon } 10541c7c3c6aSMatthew Dillon 10551c7c3c6aSMatthew Dillon /* 10561c7c3c6aSMatthew Dillon * SWAP_PAGER_PAGE_UNSWAPPED() - remove swap backing store related to page 10571c7c3c6aSMatthew Dillon * 10581c7c3c6aSMatthew Dillon * This removes any associated swap backing store, whether valid or 10591c7c3c6aSMatthew Dillon * not, from the page. 10601c7c3c6aSMatthew Dillon * 10611c7c3c6aSMatthew Dillon * This routine is typically called when a page is made dirty, at 10621c7c3c6aSMatthew Dillon * which point any associated swap can be freed. MADV_FREE also 10631c7c3c6aSMatthew Dillon * calls us in a special-case situation 10641c7c3c6aSMatthew Dillon * 10651c7c3c6aSMatthew Dillon * NOTE!!! If the page is clean and the swap was valid, the caller 10661c7c3c6aSMatthew Dillon * should make the page dirty before calling this routine. This routine 10671c7c3c6aSMatthew Dillon * does NOT change the m->dirty status of the page. Also: MADV_FREE 10681c7c3c6aSMatthew Dillon * depends on it. 10691c7c3c6aSMatthew Dillon * 107015523cf7SKonstantin Belousov * This routine may not sleep. 1071c25673ffSAttilio Rao * 1072c25673ffSAttilio Rao * The object containing the page must be locked. 10731c7c3c6aSMatthew Dillon */ 10741c7c3c6aSMatthew Dillon static void 10752f249180SPoul-Henning Kamp swap_pager_unswapped(vm_page_t m) 10761c7c3c6aSMatthew Dillon { 10774abca9bbSAlan Cox daddr_t srcaddr; 10782f249180SPoul-Henning Kamp 10794abca9bbSAlan Cox srcaddr = swp_pager_meta_ctl(m->object, m->pindex, SWM_POP); 10804abca9bbSAlan Cox if (srcaddr != SWAPBLK_NONE) 10814abca9bbSAlan Cox swp_pager_freeswapspace(srcaddr, 1); 10821c7c3c6aSMatthew Dillon } 10831c7c3c6aSMatthew Dillon 10841c7c3c6aSMatthew Dillon /* 1085915d1b71SMark Johnston * swap_pager_getpages() - bring pages in from swap 10861c7c3c6aSMatthew Dillon * 1087*3f060b60SMark Johnston * Attempt to page in the pages in array "ma" of length "count". The 1088*3f060b60SMark Johnston * caller may optionally specify that additional pages preceding and 1089*3f060b60SMark Johnston * succeeding the specified range be paged in. The number of such pages 1090*3f060b60SMark Johnston * is returned in the "rbehind" and "rahead" parameters, and they will 1091*3f060b60SMark Johnston * be in the inactive queue upon return. 10921c7c3c6aSMatthew Dillon * 1093*3f060b60SMark Johnston * The pages in "ma" must be busied and will remain busied upon return. 10941c7c3c6aSMatthew Dillon */ 1095f708ef1bSPoul-Henning Kamp static int 1096*3f060b60SMark Johnston swap_pager_getpages(vm_object_t object, vm_page_t *ma, int count, int *rbehind, 1097b0cd2017SGleb Smirnoff int *rahead) 1098df8bae1dSRodney W. Grimes { 10991c7c3c6aSMatthew Dillon struct buf *bp; 1100915d1b71SMark Johnston vm_page_t mpred, msucc, p; 1101915d1b71SMark Johnston vm_pindex_t pindex; 11021c7c3c6aSMatthew Dillon daddr_t blk; 1103dd9cb6daSMark Johnston int i, j, maxahead, maxbehind, reqcount, shift; 11040d94caffSDavid Greenman 1105915d1b71SMark Johnston reqcount = count; 11061c7c3c6aSMatthew Dillon 110789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 11081c7c3c6aSMatthew Dillon bp = getpbuf(&nsw_rcount); 1109915d1b71SMark Johnston VM_OBJECT_WLOCK(object); 111026f9a767SRodney W. Grimes 1111*3f060b60SMark Johnston if (!swap_pager_haspage(object, ma[0]->pindex, &maxbehind, &maxahead)) { 1112915d1b71SMark Johnston relpbuf(bp, &nsw_rcount); 1113915d1b71SMark Johnston return (VM_PAGER_FAIL); 1114915d1b71SMark Johnston } 1115915d1b71SMark Johnston 1116915d1b71SMark Johnston /* 1117915d1b71SMark Johnston * Clip the readahead and readbehind ranges to exclude resident pages. 1118915d1b71SMark Johnston */ 1119915d1b71SMark Johnston if (rahead != NULL) { 1120dd9cb6daSMark Johnston KASSERT(reqcount - 1 <= maxahead, 1121915d1b71SMark Johnston ("page count %d extends beyond swap block", reqcount)); 1122dd9cb6daSMark Johnston *rahead = imin(*rahead, maxahead - (reqcount - 1)); 1123*3f060b60SMark Johnston pindex = ma[reqcount - 1]->pindex; 1124*3f060b60SMark Johnston msucc = TAILQ_NEXT(ma[reqcount - 1], listq); 1125915d1b71SMark Johnston if (msucc != NULL && msucc->pindex - pindex - 1 < *rahead) 1126915d1b71SMark Johnston *rahead = msucc->pindex - pindex - 1; 1127915d1b71SMark Johnston } 1128915d1b71SMark Johnston if (rbehind != NULL) { 1129dd9cb6daSMark Johnston *rbehind = imin(*rbehind, maxbehind); 1130*3f060b60SMark Johnston pindex = ma[0]->pindex; 1131*3f060b60SMark Johnston mpred = TAILQ_PREV(ma[0], pglist, listq); 1132915d1b71SMark Johnston if (mpred != NULL && pindex - mpred->pindex - 1 < *rbehind) 1133915d1b71SMark Johnston *rbehind = pindex - mpred->pindex - 1; 1134915d1b71SMark Johnston } 1135915d1b71SMark Johnston 1136915d1b71SMark Johnston /* 1137915d1b71SMark Johnston * Allocate readahead and readbehind pages. 1138915d1b71SMark Johnston */ 1139915d1b71SMark Johnston shift = rbehind != NULL ? *rbehind : 0; 1140915d1b71SMark Johnston if (shift != 0) { 1141915d1b71SMark Johnston for (i = 1; i <= shift; i++) { 1142*3f060b60SMark Johnston p = vm_page_alloc(object, ma[0]->pindex - i, 11437667839aSAlan Cox VM_ALLOC_NORMAL); 1144915d1b71SMark Johnston if (p == NULL) { 1145915d1b71SMark Johnston /* Shift allocated pages to the left. */ 1146915d1b71SMark Johnston for (j = 0; j < i - 1; j++) 1147915d1b71SMark Johnston bp->b_pages[j] = 1148915d1b71SMark Johnston bp->b_pages[j + shift - i + 1]; 1149915d1b71SMark Johnston break; 1150915d1b71SMark Johnston } 1151915d1b71SMark Johnston bp->b_pages[shift - i] = p; 1152915d1b71SMark Johnston } 1153915d1b71SMark Johnston shift = i - 1; 1154915d1b71SMark Johnston *rbehind = shift; 1155915d1b71SMark Johnston } 1156915d1b71SMark Johnston for (i = 0; i < reqcount; i++) 1157*3f060b60SMark Johnston bp->b_pages[i + shift] = ma[i]; 1158915d1b71SMark Johnston if (rahead != NULL) { 1159915d1b71SMark Johnston for (i = 0; i < *rahead; i++) { 1160915d1b71SMark Johnston p = vm_page_alloc(object, 1161*3f060b60SMark Johnston ma[reqcount - 1]->pindex + i + 1, VM_ALLOC_NORMAL); 1162915d1b71SMark Johnston if (p == NULL) 1163915d1b71SMark Johnston break; 1164915d1b71SMark Johnston bp->b_pages[shift + reqcount + i] = p; 1165915d1b71SMark Johnston } 1166915d1b71SMark Johnston *rahead = i; 1167915d1b71SMark Johnston } 1168915d1b71SMark Johnston if (rbehind != NULL) 1169915d1b71SMark Johnston count += *rbehind; 1170915d1b71SMark Johnston if (rahead != NULL) 1171915d1b71SMark Johnston count += *rahead; 1172915d1b71SMark Johnston 1173915d1b71SMark Johnston vm_object_pip_add(object, count); 1174915d1b71SMark Johnston 1175915d1b71SMark Johnston for (i = 0; i < count; i++) 1176915d1b71SMark Johnston bp->b_pages[i]->oflags |= VPO_SWAPINPROG; 1177915d1b71SMark Johnston 1178915d1b71SMark Johnston pindex = bp->b_pages[0]->pindex; 1179915d1b71SMark Johnston blk = swp_pager_meta_ctl(object, pindex, 0); 1180915d1b71SMark Johnston KASSERT(blk != SWAPBLK_NONE, 1181915d1b71SMark Johnston ("no swap blocking containing %p(%jx)", object, (uintmax_t)pindex)); 1182915d1b71SMark Johnston 1183915d1b71SMark Johnston VM_OBJECT_WUNLOCK(object); 1184915d1b71SMark Johnston 1185915d1b71SMark Johnston bp->b_flags |= B_PAGING; 118621144e3bSPoul-Henning Kamp bp->b_iocmd = BIO_READ; 11871c7c3c6aSMatthew Dillon bp->b_iodone = swp_pager_async_iodone; 1188fdcc1cc0SJohn Baldwin bp->b_rcred = crhold(thread0.td_ucred); 1189fdcc1cc0SJohn Baldwin bp->b_wcred = crhold(thread0.td_ucred); 1190b0cd2017SGleb Smirnoff bp->b_blkno = blk; 1191b0cd2017SGleb Smirnoff bp->b_bcount = PAGE_SIZE * count; 1192b0cd2017SGleb Smirnoff bp->b_bufsize = PAGE_SIZE * count; 1193b0cd2017SGleb Smirnoff bp->b_npages = count; 1194915d1b71SMark Johnston bp->b_pgbefore = rbehind != NULL ? *rbehind : 0; 1195915d1b71SMark Johnston bp->b_pgafter = rahead != NULL ? *rahead : 0; 119626f9a767SRodney W. Grimes 119783c9dea1SGleb Smirnoff VM_CNT_INC(v_swapin); 119883c9dea1SGleb Smirnoff VM_CNT_ADD(v_swappgsin, count); 11991c7c3c6aSMatthew Dillon 12001c7c3c6aSMatthew Dillon /* 12011c7c3c6aSMatthew Dillon * perform the I/O. NOTE!!! bp cannot be considered valid after 12021c7c3c6aSMatthew Dillon * this point because we automatically release it on completion. 12031c7c3c6aSMatthew Dillon * Instead, we look at the one page we are interested in which we 12041c7c3c6aSMatthew Dillon * still hold a lock on even through the I/O completion. 12051c7c3c6aSMatthew Dillon * 1206*3f060b60SMark Johnston * The other pages in our ma[] array are also released on completion, 12071c7c3c6aSMatthew Dillon * so we cannot assume they are valid anymore either. 12081c7c3c6aSMatthew Dillon * 1209c37a77eeSPoul-Henning Kamp * NOTE: b_blkno is destroyed by the call to swapdev_strategy 12101c7c3c6aSMatthew Dillon */ 1211b890cb2cSPeter Wemm BUF_KERNPROC(bp); 12124b03903aSPoul-Henning Kamp swp_pager_strategy(bp); 121326f9a767SRodney W. Grimes 121426f9a767SRodney W. Grimes /* 1215915d1b71SMark Johnston * Wait for the pages we want to complete. VPO_SWAPINPROG is always 12161c7c3c6aSMatthew Dillon * cleared on completion. If an I/O error occurs, SWAPBLK_NONE 1217915d1b71SMark Johnston * is set in the metadata for each page in the request. 121826f9a767SRodney W. Grimes */ 121989f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1220*3f060b60SMark Johnston while ((ma[0]->oflags & VPO_SWAPINPROG) != 0) { 1221*3f060b60SMark Johnston ma[0]->oflags |= VPO_SWAPSLEEP; 122283c9dea1SGleb Smirnoff VM_CNT_INC(v_intrans); 1223c7aebda8SAttilio Rao if (VM_OBJECT_SLEEP(object, &object->paging_in_progress, PSWP, 1224c7aebda8SAttilio Rao "swread", hz * 20)) { 12259bd86a98SBruce M Simpson printf( 1226c5690651SPoul-Henning Kamp "swap_pager: indefinite wait buffer: bufobj: %p, blkno: %jd, size: %ld\n", 1227c5690651SPoul-Henning Kamp bp->b_bufobj, (intmax_t)bp->b_blkno, bp->b_bcount); 12281c7c3c6aSMatthew Dillon } 12291b119d9dSDavid Greenman } 123026f9a767SRodney W. Grimes 123126f9a767SRodney W. Grimes /* 1232b0cd2017SGleb Smirnoff * If we had an unrecoverable read error pages will not be valid. 123326f9a767SRodney W. Grimes */ 1234915d1b71SMark Johnston for (i = 0; i < reqcount; i++) 1235*3f060b60SMark Johnston if (ma[i]->valid != VM_PAGE_BITS_ALL) 12361c7c3c6aSMatthew Dillon return (VM_PAGER_ERROR); 1237b0cd2017SGleb Smirnoff 12381c7c3c6aSMatthew Dillon return (VM_PAGER_OK); 12391c7c3c6aSMatthew Dillon 12401c7c3c6aSMatthew Dillon /* 12411c7c3c6aSMatthew Dillon * A final note: in a low swap situation, we cannot deallocate swap 12421c7c3c6aSMatthew Dillon * and mark a page dirty here because the caller is likely to mark 12431c7c3c6aSMatthew Dillon * the page clean when we return, causing the page to possibly revert 12441c7c3c6aSMatthew Dillon * to all-zero's later. 12451c7c3c6aSMatthew Dillon */ 1246df8bae1dSRodney W. Grimes } 1247df8bae1dSRodney W. Grimes 12481c7c3c6aSMatthew Dillon /* 124990effb23SGleb Smirnoff * swap_pager_getpages_async(): 125090effb23SGleb Smirnoff * 125190effb23SGleb Smirnoff * Right now this is emulation of asynchronous operation on top of 125290effb23SGleb Smirnoff * swap_pager_getpages(). 125390effb23SGleb Smirnoff */ 125490effb23SGleb Smirnoff static int 1255*3f060b60SMark Johnston swap_pager_getpages_async(vm_object_t object, vm_page_t *ma, int count, 1256b0cd2017SGleb Smirnoff int *rbehind, int *rahead, pgo_getpages_iodone_t iodone, void *arg) 125790effb23SGleb Smirnoff { 125890effb23SGleb Smirnoff int r, error; 125990effb23SGleb Smirnoff 1260*3f060b60SMark Johnston r = swap_pager_getpages(object, ma, count, rbehind, rahead); 126190effb23SGleb Smirnoff VM_OBJECT_WUNLOCK(object); 126290effb23SGleb Smirnoff switch (r) { 126390effb23SGleb Smirnoff case VM_PAGER_OK: 126490effb23SGleb Smirnoff error = 0; 126590effb23SGleb Smirnoff break; 126690effb23SGleb Smirnoff case VM_PAGER_ERROR: 126790effb23SGleb Smirnoff error = EIO; 126890effb23SGleb Smirnoff break; 126990effb23SGleb Smirnoff case VM_PAGER_FAIL: 127090effb23SGleb Smirnoff error = EINVAL; 127190effb23SGleb Smirnoff break; 127290effb23SGleb Smirnoff default: 1273d9328101SGleb Smirnoff panic("unhandled swap_pager_getpages() error %d", r); 127490effb23SGleb Smirnoff } 1275*3f060b60SMark Johnston (iodone)(arg, ma, count, error); 127690effb23SGleb Smirnoff VM_OBJECT_WLOCK(object); 127790effb23SGleb Smirnoff 127890effb23SGleb Smirnoff return (r); 127990effb23SGleb Smirnoff } 128090effb23SGleb Smirnoff 128190effb23SGleb Smirnoff /* 12821c7c3c6aSMatthew Dillon * swap_pager_putpages: 12831c7c3c6aSMatthew Dillon * 12841c7c3c6aSMatthew Dillon * Assign swap (if necessary) and initiate I/O on the specified pages. 12851c7c3c6aSMatthew Dillon * 12861c7c3c6aSMatthew Dillon * We support both OBJT_DEFAULT and OBJT_SWAP objects. DEFAULT objects 12871c7c3c6aSMatthew Dillon * are automatically converted to SWAP objects. 12881c7c3c6aSMatthew Dillon * 1289ea3aecf5SPeter Wemm * In a low memory situation we may block in VOP_STRATEGY(), but the new 12901c7c3c6aSMatthew Dillon * vm_page reservation system coupled with properly written VFS devices 12911c7c3c6aSMatthew Dillon * should ensure that no low-memory deadlock occurs. This is an area 12921c7c3c6aSMatthew Dillon * which needs work. 12931c7c3c6aSMatthew Dillon * 12941c7c3c6aSMatthew Dillon * The parent has N vm_object_pip_add() references prior to 12951c7c3c6aSMatthew Dillon * calling us and will remove references for rtvals[] that are 12961c7c3c6aSMatthew Dillon * not set to VM_PAGER_PEND. We need to remove the rest on I/O 12971c7c3c6aSMatthew Dillon * completion. 12981c7c3c6aSMatthew Dillon * 12991c7c3c6aSMatthew Dillon * The parent has soft-busy'd the pages it passes us and will unbusy 13001c7c3c6aSMatthew Dillon * those whos rtvals[] entry is not set to VM_PAGER_PEND on return. 13011c7c3c6aSMatthew Dillon * We need to unbusy the rest on I/O completion. 13021c7c3c6aSMatthew Dillon */ 1303d635a37fSWarner Losh static void 1304*3f060b60SMark Johnston swap_pager_putpages(vm_object_t object, vm_page_t *ma, int count, 1305e065e87cSKonstantin Belousov int flags, int *rtvals) 1306df8bae1dSRodney W. Grimes { 1307e065e87cSKonstantin Belousov int i, n; 1308e065e87cSKonstantin Belousov boolean_t sync; 1309df8bae1dSRodney W. Grimes 1310*3f060b60SMark Johnston if (count && ma[0]->object != object) { 13117036145bSMaxim Konovalov panic("swap_pager_putpages: object mismatch %p/%p", 13121c7c3c6aSMatthew Dillon object, 1313*3f060b60SMark Johnston ma[0]->object 13141c7c3c6aSMatthew Dillon ); 13151c7c3c6aSMatthew Dillon } 1316ee3dc7d7SAlan Cox 13171c7c3c6aSMatthew Dillon /* 13181c7c3c6aSMatthew Dillon * Step 1 13191c7c3c6aSMatthew Dillon * 13201c7c3c6aSMatthew Dillon * Turn object into OBJT_SWAP 13211c7c3c6aSMatthew Dillon * check for bogus sysops 13221c7c3c6aSMatthew Dillon * force sync if not pageout process 13231c7c3c6aSMatthew Dillon */ 13244dcc5c2dSMatthew Dillon if (object->type != OBJT_SWAP) 13254dcc5c2dSMatthew Dillon swp_pager_meta_build(object, 0, SWAPBLK_NONE); 132689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1327e47ed70bSJohn Dyson 1328e065e87cSKonstantin Belousov n = 0; 1329e47ed70bSJohn Dyson if (curproc != pageproc) 1330e47ed70bSJohn Dyson sync = TRUE; 1331e065e87cSKonstantin Belousov else 1332e065e87cSKonstantin Belousov sync = (flags & VM_PAGER_PUT_SYNC) != 0; 133326f9a767SRodney W. Grimes 13341c7c3c6aSMatthew Dillon /* 13351c7c3c6aSMatthew Dillon * Step 2 13361c7c3c6aSMatthew Dillon * 13371c7c3c6aSMatthew Dillon * Assign swap blocks and issue I/O. We reallocate swap on the fly. 13381c7c3c6aSMatthew Dillon * The page is left dirty until the pageout operation completes 13391c7c3c6aSMatthew Dillon * successfully. 13401c7c3c6aSMatthew Dillon */ 13411c7c3c6aSMatthew Dillon for (i = 0; i < count; i += n) { 13421c7c3c6aSMatthew Dillon int j; 13431c7c3c6aSMatthew Dillon struct buf *bp; 1344a316d390SJohn Dyson daddr_t blk; 134526f9a767SRodney W. Grimes 1346df8bae1dSRodney W. Grimes /* 13471c7c3c6aSMatthew Dillon * Maximum I/O size is limited by a number of factors. 1348df8bae1dSRodney W. Grimes */ 13491c7c3c6aSMatthew Dillon n = min(BLIST_MAX_ALLOC, count - i); 1350327f4e83SMatthew Dillon n = min(n, nsw_cluster_max); 13511c7c3c6aSMatthew Dillon 135226f9a767SRodney W. Grimes /* 13531c7c3c6aSMatthew Dillon * Get biggest block of swap we can. If we fail, fall 13541c7c3c6aSMatthew Dillon * back and try to allocate a smaller block. Don't go 13551c7c3c6aSMatthew Dillon * overboard trying to allocate space if it would overly 13561c7c3c6aSMatthew Dillon * fragment swap. 135726f9a767SRodney W. Grimes */ 13581c7c3c6aSMatthew Dillon while ( 13591c7c3c6aSMatthew Dillon (blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE && 13601c7c3c6aSMatthew Dillon n > 4 13611c7c3c6aSMatthew Dillon ) { 13621c7c3c6aSMatthew Dillon n >>= 1; 136326f9a767SRodney W. Grimes } 13641c7c3c6aSMatthew Dillon if (blk == SWAPBLK_NONE) { 13654dcc5c2dSMatthew Dillon for (j = 0; j < n; ++j) 13661c7c3c6aSMatthew Dillon rtvals[i+j] = VM_PAGER_FAIL; 13671c7c3c6aSMatthew Dillon continue; 136826f9a767SRodney W. Grimes } 136926f9a767SRodney W. Grimes 137026f9a767SRodney W. Grimes /* 13711c7c3c6aSMatthew Dillon * All I/O parameters have been satisfied, build the I/O 13721c7c3c6aSMatthew Dillon * request and assign the swap space. 137326f9a767SRodney W. Grimes */ 1374327f4e83SMatthew Dillon if (sync == TRUE) { 1375327f4e83SMatthew Dillon bp = getpbuf(&nsw_wcount_sync); 1376327f4e83SMatthew Dillon } else { 1377327f4e83SMatthew Dillon bp = getpbuf(&nsw_wcount_async); 137821144e3bSPoul-Henning Kamp bp->b_flags = B_ASYNC; 1379327f4e83SMatthew Dillon } 13805e04322aSPoul-Henning Kamp bp->b_flags |= B_PAGING; 1381912e4ae9SPoul-Henning Kamp bp->b_iocmd = BIO_WRITE; 138226f9a767SRodney W. Grimes 1383fdcc1cc0SJohn Baldwin bp->b_rcred = crhold(thread0.td_ucred); 1384fdcc1cc0SJohn Baldwin bp->b_wcred = crhold(thread0.td_ucred); 13851c7c3c6aSMatthew Dillon bp->b_bcount = PAGE_SIZE * n; 13861c7c3c6aSMatthew Dillon bp->b_bufsize = PAGE_SIZE * n; 13871c7c3c6aSMatthew Dillon bp->b_blkno = blk; 1388e47ed70bSJohn Dyson 138989f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 13901c7c3c6aSMatthew Dillon for (j = 0; j < n; ++j) { 1391*3f060b60SMark Johnston vm_page_t mreq = ma[i+j]; 13921c7c3c6aSMatthew Dillon 13931c7c3c6aSMatthew Dillon swp_pager_meta_build( 13941c7c3c6aSMatthew Dillon mreq->object, 13951c7c3c6aSMatthew Dillon mreq->pindex, 13964dcc5c2dSMatthew Dillon blk + j 13971c7c3c6aSMatthew Dillon ); 139887b0ab69SAlan Cox MPASS(mreq->dirty == VM_PAGE_BITS_ALL); 13995786be7cSAlan Cox mreq->oflags |= VPO_SWAPINPROG; 14001c7c3c6aSMatthew Dillon bp->b_pages[j] = mreq; 14011c7c3c6aSMatthew Dillon } 140289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 14031c7c3c6aSMatthew Dillon bp->b_npages = n; 1404a5296b05SJulian Elischer /* 1405a5296b05SJulian Elischer * Must set dirty range for NFS to work. 1406a5296b05SJulian Elischer */ 1407a5296b05SJulian Elischer bp->b_dirtyoff = 0; 1408a5296b05SJulian Elischer bp->b_dirtyend = bp->b_bcount; 14091c7c3c6aSMatthew Dillon 141083c9dea1SGleb Smirnoff VM_CNT_INC(v_swapout); 141183c9dea1SGleb Smirnoff VM_CNT_ADD(v_swappgsout, bp->b_npages); 141226f9a767SRodney W. Grimes 141326f9a767SRodney W. Grimes /* 141477923df2SAlan Cox * We unconditionally set rtvals[] to VM_PAGER_PEND so that we 141577923df2SAlan Cox * can call the async completion routine at the end of a 141677923df2SAlan Cox * synchronous I/O operation. Otherwise, our caller would 141777923df2SAlan Cox * perform duplicate unbusy and wakeup operations on the page 141877923df2SAlan Cox * and object, respectively. 141977923df2SAlan Cox */ 142077923df2SAlan Cox for (j = 0; j < n; j++) 142177923df2SAlan Cox rtvals[i + j] = VM_PAGER_PEND; 142277923df2SAlan Cox 142377923df2SAlan Cox /* 14241c7c3c6aSMatthew Dillon * asynchronous 14251c7c3c6aSMatthew Dillon * 1426c37a77eeSPoul-Henning Kamp * NOTE: b_blkno is destroyed by the call to swapdev_strategy 142726f9a767SRodney W. Grimes */ 14281c7c3c6aSMatthew Dillon if (sync == FALSE) { 14291c7c3c6aSMatthew Dillon bp->b_iodone = swp_pager_async_iodone; 143067812eacSKirk McKusick BUF_KERNPROC(bp); 14314b03903aSPoul-Henning Kamp swp_pager_strategy(bp); 14321c7c3c6aSMatthew Dillon continue; 143326f9a767SRodney W. Grimes } 1434e47ed70bSJohn Dyson 143526f9a767SRodney W. Grimes /* 14361c7c3c6aSMatthew Dillon * synchronous 14371c7c3c6aSMatthew Dillon * 1438c37a77eeSPoul-Henning Kamp * NOTE: b_blkno is destroyed by the call to swapdev_strategy 14391c7c3c6aSMatthew Dillon */ 14402c840b1fSAlan Cox bp->b_iodone = bdone; 14414b03903aSPoul-Henning Kamp swp_pager_strategy(bp); 14421c7c3c6aSMatthew Dillon 14431c7c3c6aSMatthew Dillon /* 144477923df2SAlan Cox * Wait for the sync I/O to complete. 144526f9a767SRodney W. Grimes */ 14462c840b1fSAlan Cox bwait(bp, PVM, "swwrt"); 144777923df2SAlan Cox 14481c7c3c6aSMatthew Dillon /* 14491c7c3c6aSMatthew Dillon * Now that we are through with the bp, we can call the 14501c7c3c6aSMatthew Dillon * normal async completion, which frees everything up. 14511c7c3c6aSMatthew Dillon */ 14521c7c3c6aSMatthew Dillon swp_pager_async_iodone(bp); 14531c7c3c6aSMatthew Dillon } 145489f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 14551c7c3c6aSMatthew Dillon } 14561c7c3c6aSMatthew Dillon 14571c7c3c6aSMatthew Dillon /* 14581c7c3c6aSMatthew Dillon * swp_pager_async_iodone: 14591c7c3c6aSMatthew Dillon * 14601c7c3c6aSMatthew Dillon * Completion routine for asynchronous reads and writes from/to swap. 14611c7c3c6aSMatthew Dillon * Also called manually by synchronous code to finish up a bp. 14621c7c3c6aSMatthew Dillon * 146315523cf7SKonstantin Belousov * This routine may not sleep. 14641c7c3c6aSMatthew Dillon */ 14651c7c3c6aSMatthew Dillon static void 14662f249180SPoul-Henning Kamp swp_pager_async_iodone(struct buf *bp) 14671c7c3c6aSMatthew Dillon { 14681c7c3c6aSMatthew Dillon int i; 14691c7c3c6aSMatthew Dillon vm_object_t object = NULL; 14701c7c3c6aSMatthew Dillon 14711c7c3c6aSMatthew Dillon /* 14721c7c3c6aSMatthew Dillon * report error 14731c7c3c6aSMatthew Dillon */ 1474c244d2deSPoul-Henning Kamp if (bp->b_ioflags & BIO_ERROR) { 14751c7c3c6aSMatthew Dillon printf( 14761c7c3c6aSMatthew Dillon "swap_pager: I/O error - %s failed; blkno %ld," 14771c7c3c6aSMatthew Dillon "size %ld, error %d\n", 147821144e3bSPoul-Henning Kamp ((bp->b_iocmd == BIO_READ) ? "pagein" : "pageout"), 14791c7c3c6aSMatthew Dillon (long)bp->b_blkno, 14801c7c3c6aSMatthew Dillon (long)bp->b_bcount, 14811c7c3c6aSMatthew Dillon bp->b_error 14821c7c3c6aSMatthew Dillon ); 14831c7c3c6aSMatthew Dillon } 14841c7c3c6aSMatthew Dillon 14851c7c3c6aSMatthew Dillon /* 148626f9a767SRodney W. Grimes * remove the mapping for kernel virtual 148726f9a767SRodney W. Grimes */ 1488fade8dd7SJeff Roberson if (buf_mapped(bp)) 14891c7c3c6aSMatthew Dillon pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages); 1490fade8dd7SJeff Roberson else 1491fade8dd7SJeff Roberson bp->b_data = bp->b_kvabase; 149226f9a767SRodney W. Grimes 149333a609ecSAlan Cox if (bp->b_npages) { 149433a609ecSAlan Cox object = bp->b_pages[0]->object; 149589f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 149633a609ecSAlan Cox } 14972965a453SKip Macy 149826f9a767SRodney W. Grimes /* 14991c7c3c6aSMatthew Dillon * cleanup pages. If an error occurs writing to swap, we are in 15001c7c3c6aSMatthew Dillon * very serious trouble. If it happens to be a disk error, though, 15011c7c3c6aSMatthew Dillon * we may be able to recover by reassigning the swap later on. So 15021c7c3c6aSMatthew Dillon * in this case we remove the m->swapblk assignment for the page 15031c7c3c6aSMatthew Dillon * but do not free it in the rlist. The errornous block(s) are thus 15041c7c3c6aSMatthew Dillon * never reallocated as swap. Redirty the page and continue. 150526f9a767SRodney W. Grimes */ 15061c7c3c6aSMatthew Dillon for (i = 0; i < bp->b_npages; ++i) { 15071c7c3c6aSMatthew Dillon vm_page_t m = bp->b_pages[i]; 1508e47ed70bSJohn Dyson 15095786be7cSAlan Cox m->oflags &= ~VPO_SWAPINPROG; 1510c7aebda8SAttilio Rao if (m->oflags & VPO_SWAPSLEEP) { 1511c7aebda8SAttilio Rao m->oflags &= ~VPO_SWAPSLEEP; 1512c7aebda8SAttilio Rao wakeup(&object->paging_in_progress); 1513c7aebda8SAttilio Rao } 1514e47ed70bSJohn Dyson 1515c244d2deSPoul-Henning Kamp if (bp->b_ioflags & BIO_ERROR) { 1516ffc82b0aSJohn Dyson /* 15171c7c3c6aSMatthew Dillon * If an error occurs I'd love to throw the swapblk 15181c7c3c6aSMatthew Dillon * away without freeing it back to swapspace, so it 15191c7c3c6aSMatthew Dillon * can never be used again. But I can't from an 15201c7c3c6aSMatthew Dillon * interrupt. 1521ffc82b0aSJohn Dyson */ 152221144e3bSPoul-Henning Kamp if (bp->b_iocmd == BIO_READ) { 15231c7c3c6aSMatthew Dillon /* 15241c7c3c6aSMatthew Dillon * NOTE: for reads, m->dirty will probably 1525956f3135SPhilippe Charnier * be overridden by the original caller of 15261c7c3c6aSMatthew Dillon * getpages so don't play cute tricks here. 15271c7c3c6aSMatthew Dillon */ 15281c7c3c6aSMatthew Dillon m->valid = 0; 15291c7c3c6aSMatthew Dillon } else { 15301c7c3c6aSMatthew Dillon /* 15311c7c3c6aSMatthew Dillon * If a write error occurs, reactivate page 15321c7c3c6aSMatthew Dillon * so it doesn't clog the inactive list, 15331c7c3c6aSMatthew Dillon * then finish the I/O. 15341c7c3c6aSMatthew Dillon */ 153541e5a226SAlan Cox MPASS(m->dirty == VM_PAGE_BITS_ALL); 15363c4a2440SAlan Cox vm_page_lock(m); 15371c7c3c6aSMatthew Dillon vm_page_activate(m); 15383c4a2440SAlan Cox vm_page_unlock(m); 1539c7aebda8SAttilio Rao vm_page_sunbusy(m); 15401c7c3c6aSMatthew Dillon } 154121144e3bSPoul-Henning Kamp } else if (bp->b_iocmd == BIO_READ) { 15421c7c3c6aSMatthew Dillon /* 15431c7c3c6aSMatthew Dillon * NOTE: for reads, m->dirty will probably be 1544956f3135SPhilippe Charnier * overridden by the original caller of getpages so 15451c7c3c6aSMatthew Dillon * we cannot set them in order to free the underlying 15461c7c3c6aSMatthew Dillon * swap in a low-swap situation. I don't think we'd 15471c7c3c6aSMatthew Dillon * want to do that anyway, but it was an optimization 15481c7c3c6aSMatthew Dillon * that existed in the old swapper for a time before 15491c7c3c6aSMatthew Dillon * it got ripped out due to precisely this problem. 15501c7c3c6aSMatthew Dillon */ 1551016a3c93SAlan Cox KASSERT(!pmap_page_is_mapped(m), 1552016a3c93SAlan Cox ("swp_pager_async_iodone: page %p is mapped", m)); 1553016a3c93SAlan Cox KASSERT(m->dirty == 0, 1554016a3c93SAlan Cox ("swp_pager_async_iodone: page %p is dirty", m)); 1555915d1b71SMark Johnston 1556b0cd2017SGleb Smirnoff m->valid = VM_PAGE_BITS_ALL; 1557915d1b71SMark Johnston if (i < bp->b_pgbefore || 1558915d1b71SMark Johnston i >= bp->b_npages - bp->b_pgafter) 1559915d1b71SMark Johnston vm_page_readahead_finish(m); 15601c7c3c6aSMatthew Dillon } else { 15611c7c3c6aSMatthew Dillon /* 1562016a3c93SAlan Cox * For write success, clear the dirty 15631c7c3c6aSMatthew Dillon * status, then finish the I/O ( which decrements the 15641c7c3c6aSMatthew Dillon * busy count and possibly wakes waiter's up ). 1565ebcddc72SAlan Cox * A page is only written to swap after a period of 1566ebcddc72SAlan Cox * inactivity. Therefore, we do not expect it to be 1567ebcddc72SAlan Cox * reused. 15681c7c3c6aSMatthew Dillon */ 15696031c68dSAlan Cox KASSERT(!pmap_page_is_write_mapped(m), 1570016a3c93SAlan Cox ("swp_pager_async_iodone: page %p is not write" 1571016a3c93SAlan Cox " protected", m)); 1572c52e7044SAlan Cox vm_page_undirty(m); 15733c4a2440SAlan Cox vm_page_lock(m); 1574ebcddc72SAlan Cox vm_page_deactivate_noreuse(m); 15752965a453SKip Macy vm_page_unlock(m); 1576ebcddc72SAlan Cox vm_page_sunbusy(m); 15773c4a2440SAlan Cox } 15783c4a2440SAlan Cox } 157926f9a767SRodney W. Grimes 15801c7c3c6aSMatthew Dillon /* 15811c7c3c6aSMatthew Dillon * adjust pip. NOTE: the original parent may still have its own 15821c7c3c6aSMatthew Dillon * pip refs on the object. 15831c7c3c6aSMatthew Dillon */ 15840d420ad3SAlan Cox if (object != NULL) { 15851c7c3c6aSMatthew Dillon vm_object_pip_wakeupn(object, bp->b_npages); 158689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 15870d420ad3SAlan Cox } 158826f9a767SRodney W. Grimes 15891c7c3c6aSMatthew Dillon /* 1590100650deSOlivier Houchard * swapdev_strategy() manually sets b_vp and b_bufobj before calling 1591100650deSOlivier Houchard * bstrategy(). Set them back to NULL now we're done with it, or we'll 1592100650deSOlivier Houchard * trigger a KASSERT in relpbuf(). 1593100650deSOlivier Houchard */ 1594100650deSOlivier Houchard if (bp->b_vp) { 1595100650deSOlivier Houchard bp->b_vp = NULL; 1596100650deSOlivier Houchard bp->b_bufobj = NULL; 1597100650deSOlivier Houchard } 1598100650deSOlivier Houchard /* 15991c7c3c6aSMatthew Dillon * release the physical I/O buffer 16001c7c3c6aSMatthew Dillon */ 1601327f4e83SMatthew Dillon relpbuf( 1602327f4e83SMatthew Dillon bp, 160321144e3bSPoul-Henning Kamp ((bp->b_iocmd == BIO_READ) ? &nsw_rcount : 1604327f4e83SMatthew Dillon ((bp->b_flags & B_ASYNC) ? 1605327f4e83SMatthew Dillon &nsw_wcount_async : 1606327f4e83SMatthew Dillon &nsw_wcount_sync 1607327f4e83SMatthew Dillon ) 1608327f4e83SMatthew Dillon ) 1609327f4e83SMatthew Dillon ); 161026f9a767SRodney W. Grimes } 16111c7c3c6aSMatthew Dillon 1612b1fd102eSMark Johnston int 1613b1fd102eSMark Johnston swap_pager_nswapdev(void) 1614b1fd102eSMark Johnston { 1615b1fd102eSMark Johnston 1616b1fd102eSMark Johnston return (nswapdev); 1617b1fd102eSMark Johnston } 1618b1fd102eSMark Johnston 161992da00bbSMatthew Dillon /* 162092da00bbSMatthew Dillon * SWP_PAGER_FORCE_PAGEIN() - force a swap block to be paged in 162192da00bbSMatthew Dillon * 1622ebcddc72SAlan Cox * This routine dissociates the page at the given index within an object 1623ebcddc72SAlan Cox * from its backing store, paging it in if it does not reside in memory. 1624ebcddc72SAlan Cox * If the page is paged in, it is marked dirty and placed in the laundry 1625ebcddc72SAlan Cox * queue. The page is marked dirty because it no longer has backing 1626ebcddc72SAlan Cox * store. It is placed in the laundry queue because it has not been 1627ebcddc72SAlan Cox * accessed recently. Otherwise, it would already reside in memory. 1628ebcddc72SAlan Cox * 1629ebcddc72SAlan Cox * We also attempt to swap in all other pages in the swap block. 1630ebcddc72SAlan Cox * However, we only guarantee that the one at the specified index is 163192da00bbSMatthew Dillon * paged in. 163292da00bbSMatthew Dillon * 163392da00bbSMatthew Dillon * XXX - The code to page the whole block in doesn't work, so we 163492da00bbSMatthew Dillon * revert to the one-by-one behavior for now. Sigh. 163592da00bbSMatthew Dillon */ 163662a59e8fSWarner Losh static inline void 1637b3fed13eSDavid Schultz swp_pager_force_pagein(vm_object_t object, vm_pindex_t pindex) 163892da00bbSMatthew Dillon { 163992da00bbSMatthew Dillon vm_page_t m; 164092da00bbSMatthew Dillon 164192da00bbSMatthew Dillon vm_object_pip_add(object, 1); 16425944de8eSKonstantin Belousov m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL); 164392da00bbSMatthew Dillon if (m->valid == VM_PAGE_BITS_ALL) { 16440d8243ccSAttilio Rao vm_object_pip_wakeup(object); 164592da00bbSMatthew Dillon vm_page_dirty(m); 164637244a84SAlan Cox #ifdef INVARIANTS 1647d061cdd5SAlan Cox vm_page_lock(m); 164837244a84SAlan Cox if (m->wire_count == 0 && m->queue == PQ_NONE) 164937244a84SAlan Cox panic("page %p is neither wired nor queued", m); 16502965a453SKip Macy vm_page_unlock(m); 165137244a84SAlan Cox #endif 1652c7aebda8SAttilio Rao vm_page_xunbusy(m); 165392da00bbSMatthew Dillon vm_pager_page_unswapped(m); 165492da00bbSMatthew Dillon return; 165592da00bbSMatthew Dillon } 165692da00bbSMatthew Dillon 1657b0cd2017SGleb Smirnoff if (swap_pager_getpages(object, &m, 1, NULL, NULL) != VM_PAGER_OK) 165892da00bbSMatthew Dillon panic("swap_pager_force_pagein: read from swap failed");/*XXX*/ 16590d8243ccSAttilio Rao vm_object_pip_wakeup(object); 166092da00bbSMatthew Dillon vm_page_dirty(m); 1661db1f085eSAlan Cox vm_page_lock(m); 1662ebcddc72SAlan Cox vm_page_launder(m); 16632965a453SKip Macy vm_page_unlock(m); 1664c7aebda8SAttilio Rao vm_page_xunbusy(m); 166592da00bbSMatthew Dillon vm_pager_page_unswapped(m); 166692da00bbSMatthew Dillon } 166792da00bbSMatthew Dillon 166892da00bbSMatthew Dillon /* 166992da00bbSMatthew Dillon * swap_pager_swapoff: 167092da00bbSMatthew Dillon * 167192da00bbSMatthew Dillon * Page in all of the pages that have been paged out to the 167292da00bbSMatthew Dillon * given device. The corresponding blocks in the bitmap must be 167392da00bbSMatthew Dillon * marked as allocated and the device must be flagged SW_CLOSING. 167492da00bbSMatthew Dillon * There may be no processes swapped out to the device. 167592da00bbSMatthew Dillon * 167692da00bbSMatthew Dillon * This routine may block. 167792da00bbSMatthew Dillon */ 1678e9c0cc15SPoul-Henning Kamp static void 1679b3fed13eSDavid Schultz swap_pager_swapoff(struct swdevt *sp) 168092da00bbSMatthew Dillon { 1681f425ab8eSKonstantin Belousov struct swblk *sb; 1682f425ab8eSKonstantin Belousov vm_object_t object; 1683f425ab8eSKonstantin Belousov vm_pindex_t pi; 1684f425ab8eSKonstantin Belousov int i, retries; 168592da00bbSMatthew Dillon 168604533e1eSKonstantin Belousov sx_assert(&swdev_syscall_lock, SA_XLOCKED); 168792da00bbSMatthew Dillon 16888bc61209SDavid Schultz retries = 0; 168992da00bbSMatthew Dillon full_rescan: 1690f425ab8eSKonstantin Belousov mtx_lock(&vm_object_list_mtx); 1691f425ab8eSKonstantin Belousov TAILQ_FOREACH(object, &vm_object_list, object_list) { 1692f425ab8eSKonstantin Belousov if (object->type != OBJT_SWAP) 169398150664SKonstantin Belousov continue; 1694f425ab8eSKonstantin Belousov mtx_unlock(&vm_object_list_mtx); 169598150664SKonstantin Belousov /* Depends on type-stability. */ 169698150664SKonstantin Belousov VM_OBJECT_WLOCK(object); 1697f425ab8eSKonstantin Belousov 1698f425ab8eSKonstantin Belousov /* 1699f425ab8eSKonstantin Belousov * Dead objects are eventually terminated on their own. 1700f425ab8eSKonstantin Belousov */ 1701f425ab8eSKonstantin Belousov if ((object->flags & OBJ_DEAD) != 0) 1702f425ab8eSKonstantin Belousov goto next_obj; 1703f425ab8eSKonstantin Belousov 1704f425ab8eSKonstantin Belousov /* 1705f425ab8eSKonstantin Belousov * Sync with fences placed after pctrie 1706f425ab8eSKonstantin Belousov * initialization. We must not access pctrie below 1707f425ab8eSKonstantin Belousov * unless we checked that our object is swap and not 1708f425ab8eSKonstantin Belousov * dead. 1709f425ab8eSKonstantin Belousov */ 1710f425ab8eSKonstantin Belousov atomic_thread_fence_acq(); 1711f425ab8eSKonstantin Belousov if (object->type != OBJT_SWAP) 1712f425ab8eSKonstantin Belousov goto next_obj; 1713f425ab8eSKonstantin Belousov 1714f425ab8eSKonstantin Belousov for (pi = 0; (sb = SWAP_PCTRIE_LOOKUP_GE( 1715f425ab8eSKonstantin Belousov &object->un_pager.swp.swp_blks, pi)) != NULL; ) { 1716f425ab8eSKonstantin Belousov pi = sb->p + SWAP_META_PAGES; 1717f425ab8eSKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) { 1718f425ab8eSKonstantin Belousov if (sb->d[i] == SWAPBLK_NONE) 1719f425ab8eSKonstantin Belousov continue; 1720f425ab8eSKonstantin Belousov if (swp_pager_isondev(sb->d[i], sp)) 1721f425ab8eSKonstantin Belousov swp_pager_force_pagein(object, 1722f425ab8eSKonstantin Belousov sb->p + i); 172398150664SKonstantin Belousov } 172498150664SKonstantin Belousov } 1725f425ab8eSKonstantin Belousov next_obj: 1726f425ab8eSKonstantin Belousov VM_OBJECT_WUNLOCK(object); 1727f425ab8eSKonstantin Belousov mtx_lock(&vm_object_list_mtx); 172892da00bbSMatthew Dillon } 1729f425ab8eSKonstantin Belousov mtx_unlock(&vm_object_list_mtx); 1730f425ab8eSKonstantin Belousov 17318bc61209SDavid Schultz if (sp->sw_used) { 173292da00bbSMatthew Dillon /* 17338bc61209SDavid Schultz * Objects may be locked or paging to the device being 17348bc61209SDavid Schultz * removed, so we will miss their pages and need to 17358bc61209SDavid Schultz * make another pass. We have marked this device as 17368bc61209SDavid Schultz * SW_CLOSING, so the activity should finish soon. 173792da00bbSMatthew Dillon */ 17388bc61209SDavid Schultz retries++; 17398bc61209SDavid Schultz if (retries > 100) { 17408bc61209SDavid Schultz panic("swapoff: failed to locate %d swap blocks", 17418bc61209SDavid Schultz sp->sw_used); 17428bc61209SDavid Schultz } 17434d70511aSJohn Baldwin pause("swpoff", hz / 20); 174492da00bbSMatthew Dillon goto full_rescan; 174592da00bbSMatthew Dillon } 1746b1fd102eSMark Johnston EVENTHANDLER_INVOKE(swapoff, sp); 174792da00bbSMatthew Dillon } 174892da00bbSMatthew Dillon 17491c7c3c6aSMatthew Dillon /************************************************************************ 17501c7c3c6aSMatthew Dillon * SWAP META DATA * 17511c7c3c6aSMatthew Dillon ************************************************************************ 17521c7c3c6aSMatthew Dillon * 17531c7c3c6aSMatthew Dillon * These routines manipulate the swap metadata stored in the 1754cec9f109SDavid E. O'Brien * OBJT_SWAP object. 17551c7c3c6aSMatthew Dillon * 17564dcc5c2dSMatthew Dillon * Swap metadata is implemented with a global hash and not directly 17574dcc5c2dSMatthew Dillon * linked into the object. Instead the object simply contains 17584dcc5c2dSMatthew Dillon * appropriate tracking counters. 17591c7c3c6aSMatthew Dillon */ 17601c7c3c6aSMatthew Dillon 17611c7c3c6aSMatthew Dillon /* 1762230869e0SAlan Cox * SWP_PAGER_SWBLK_EMPTY() - is a range of blocks free? 1763230869e0SAlan Cox */ 1764230869e0SAlan Cox static bool 1765230869e0SAlan Cox swp_pager_swblk_empty(struct swblk *sb, int start, int limit) 1766230869e0SAlan Cox { 1767230869e0SAlan Cox int i; 1768230869e0SAlan Cox 1769230869e0SAlan Cox MPASS(0 <= start && start <= limit && limit <= SWAP_META_PAGES); 1770230869e0SAlan Cox for (i = start; i < limit; i++) { 1771230869e0SAlan Cox if (sb->d[i] != SWAPBLK_NONE) 1772230869e0SAlan Cox return (false); 1773230869e0SAlan Cox } 1774230869e0SAlan Cox return (true); 1775230869e0SAlan Cox } 1776230869e0SAlan Cox 1777230869e0SAlan Cox /* 17781c7c3c6aSMatthew Dillon * SWP_PAGER_META_BUILD() - add swap block to swap meta data for object 17791c7c3c6aSMatthew Dillon * 17801c7c3c6aSMatthew Dillon * We first convert the object to a swap object if it is a default 17811c7c3c6aSMatthew Dillon * object. 17821c7c3c6aSMatthew Dillon * 17831c7c3c6aSMatthew Dillon * The specified swapblk is added to the object's swap metadata. If 17841c7c3c6aSMatthew Dillon * the swapblk is not valid, it is freed instead. Any previously 17851c7c3c6aSMatthew Dillon * assigned swapblk is freed. 17861c7c3c6aSMatthew Dillon */ 17871c7c3c6aSMatthew Dillon static void 17882f249180SPoul-Henning Kamp swp_pager_meta_build(vm_object_t object, vm_pindex_t pindex, daddr_t swapblk) 17892f249180SPoul-Henning Kamp { 1790f425ab8eSKonstantin Belousov static volatile int swblk_zone_exhausted, swpctrie_zone_exhausted; 1791eed99cb8SKonstantin Belousov struct swblk *sb, *sb1; 1792f425ab8eSKonstantin Belousov vm_pindex_t modpi, rdpi; 1793f425ab8eSKonstantin Belousov int error, i; 17941c7c3c6aSMatthew Dillon 179589f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 1796f425ab8eSKonstantin Belousov 17971c7c3c6aSMatthew Dillon /* 17981c7c3c6aSMatthew Dillon * Convert default object to swap object if necessary 17991c7c3c6aSMatthew Dillon */ 18001c7c3c6aSMatthew Dillon if (object->type != OBJT_SWAP) { 1801f425ab8eSKonstantin Belousov pctrie_init(&object->un_pager.swp.swp_blks); 1802f425ab8eSKonstantin Belousov 1803f425ab8eSKonstantin Belousov /* 1804f425ab8eSKonstantin Belousov * Ensure that swap_pager_swapoff()'s iteration over 1805f425ab8eSKonstantin Belousov * object_list does not see a garbage pctrie. 1806f425ab8eSKonstantin Belousov */ 1807f425ab8eSKonstantin Belousov atomic_thread_fence_rel(); 1808f425ab8eSKonstantin Belousov 18091c7c3c6aSMatthew Dillon object->type = OBJT_SWAP; 1810eb4d6a1bSKonstantin Belousov KASSERT(object->handle == NULL, ("default pager with handle")); 1811bd228075SAlan Cox } 18121c7c3c6aSMatthew Dillon 1813f425ab8eSKonstantin Belousov rdpi = rounddown(pindex, SWAP_META_PAGES); 1814f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, rdpi); 1815f425ab8eSKonstantin Belousov if (sb == NULL) { 18161c7c3c6aSMatthew Dillon if (swapblk == SWAPBLK_NONE) 1817f425ab8eSKonstantin Belousov return; 1818f425ab8eSKonstantin Belousov for (;;) { 1819f425ab8eSKonstantin Belousov sb = uma_zalloc(swblk_zone, M_NOWAIT | (curproc == 1820f425ab8eSKonstantin Belousov pageproc ? M_USE_RESERVE : 0)); 1821f425ab8eSKonstantin Belousov if (sb != NULL) { 1822f425ab8eSKonstantin Belousov sb->p = rdpi; 1823f425ab8eSKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) 1824f425ab8eSKonstantin Belousov sb->d[i] = SWAPBLK_NONE; 1825f425ab8eSKonstantin Belousov if (atomic_cmpset_int(&swblk_zone_exhausted, 1826f425ab8eSKonstantin Belousov 1, 0)) 1827f425ab8eSKonstantin Belousov printf("swblk zone ok\n"); 1828f425ab8eSKonstantin Belousov break; 1829f425ab8eSKonstantin Belousov } 183089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1831f425ab8eSKonstantin Belousov if (uma_zone_exhausted(swblk_zone)) { 1832f425ab8eSKonstantin Belousov if (atomic_cmpset_int(&swblk_zone_exhausted, 1833f425ab8eSKonstantin Belousov 0, 1)) 1834f425ab8eSKonstantin Belousov printf("swap blk zone exhausted, " 18353ff863f1SDag-Erling Smørgrav "increase kern.maxswzone\n"); 18362025d69bSKonstantin Belousov vm_pageout_oom(VM_OOM_SWAPZ); 1837f425ab8eSKonstantin Belousov pause("swzonxb", 10); 18382025d69bSKonstantin Belousov } else 18398d6fbbb8SJeff Roberson uma_zwait(swblk_zone); 184089f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1841eed99cb8SKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, 1842eed99cb8SKonstantin Belousov rdpi); 1843eed99cb8SKonstantin Belousov if (sb != NULL) 1844eed99cb8SKonstantin Belousov /* 1845eed99cb8SKonstantin Belousov * Somebody swapped out a nearby page, 1846eed99cb8SKonstantin Belousov * allocating swblk at the rdpi index, 1847eed99cb8SKonstantin Belousov * while we dropped the object lock. 1848eed99cb8SKonstantin Belousov */ 1849eed99cb8SKonstantin Belousov goto allocated; 18504dcc5c2dSMatthew Dillon } 1851f425ab8eSKonstantin Belousov for (;;) { 1852f425ab8eSKonstantin Belousov error = SWAP_PCTRIE_INSERT( 1853f425ab8eSKonstantin Belousov &object->un_pager.swp.swp_blks, sb); 1854f425ab8eSKonstantin Belousov if (error == 0) { 1855f425ab8eSKonstantin Belousov if (atomic_cmpset_int(&swpctrie_zone_exhausted, 1856f425ab8eSKonstantin Belousov 1, 0)) 1857f425ab8eSKonstantin Belousov printf("swpctrie zone ok\n"); 1858f425ab8eSKonstantin Belousov break; 18591c7c3c6aSMatthew Dillon } 1860f425ab8eSKonstantin Belousov VM_OBJECT_WUNLOCK(object); 1861f425ab8eSKonstantin Belousov if (uma_zone_exhausted(swpctrie_zone)) { 1862f425ab8eSKonstantin Belousov if (atomic_cmpset_int(&swpctrie_zone_exhausted, 1863f425ab8eSKonstantin Belousov 0, 1)) 1864f425ab8eSKonstantin Belousov printf("swap pctrie zone exhausted, " 1865f425ab8eSKonstantin Belousov "increase kern.maxswzone\n"); 1866f425ab8eSKonstantin Belousov vm_pageout_oom(VM_OOM_SWAPZ); 1867f425ab8eSKonstantin Belousov pause("swzonxp", 10); 1868f425ab8eSKonstantin Belousov } else 18698d6fbbb8SJeff Roberson uma_zwait(swpctrie_zone); 1870f425ab8eSKonstantin Belousov VM_OBJECT_WLOCK(object); 1871eed99cb8SKonstantin Belousov sb1 = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, 1872eed99cb8SKonstantin Belousov rdpi); 1873eed99cb8SKonstantin Belousov if (sb1 != NULL) { 1874eed99cb8SKonstantin Belousov uma_zfree(swblk_zone, sb); 1875eed99cb8SKonstantin Belousov sb = sb1; 1876eed99cb8SKonstantin Belousov goto allocated; 18771c7c3c6aSMatthew Dillon } 1878f425ab8eSKonstantin Belousov } 1879eed99cb8SKonstantin Belousov } 1880eed99cb8SKonstantin Belousov allocated: 1881f425ab8eSKonstantin Belousov MPASS(sb->p == rdpi); 18821c7c3c6aSMatthew Dillon 1883f425ab8eSKonstantin Belousov modpi = pindex % SWAP_META_PAGES; 1884f425ab8eSKonstantin Belousov /* Delete prior contents of metadata. */ 1885f425ab8eSKonstantin Belousov if (sb->d[modpi] != SWAPBLK_NONE) 1886f425ab8eSKonstantin Belousov swp_pager_freeswapspace(sb->d[modpi], 1); 1887f425ab8eSKonstantin Belousov /* Enter block into metadata. */ 1888f425ab8eSKonstantin Belousov sb->d[modpi] = swapblk; 188985d88d87SKonstantin Belousov 189085d88d87SKonstantin Belousov /* 189185d88d87SKonstantin Belousov * Free the swblk if we end up with the empty page run. 189285d88d87SKonstantin Belousov */ 1893230869e0SAlan Cox if (swapblk == SWAPBLK_NONE && 1894230869e0SAlan Cox swp_pager_swblk_empty(sb, 0, SWAP_META_PAGES)) { 1895230869e0SAlan Cox SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, rdpi); 189685d88d87SKonstantin Belousov uma_zfree(swblk_zone, sb); 189785d88d87SKonstantin Belousov } 189885d88d87SKonstantin Belousov } 18991c7c3c6aSMatthew Dillon 19001c7c3c6aSMatthew Dillon /* 19011c7c3c6aSMatthew Dillon * SWP_PAGER_META_FREE() - free a range of blocks in the object's swap metadata 19021c7c3c6aSMatthew Dillon * 19031c7c3c6aSMatthew Dillon * The requested range of blocks is freed, with any associated swap 19041c7c3c6aSMatthew Dillon * returned to the swap bitmap. 19051c7c3c6aSMatthew Dillon * 19061c7c3c6aSMatthew Dillon * This routine will free swap metadata structures as they are cleaned 19071c7c3c6aSMatthew Dillon * out. This routine does *NOT* operate on swap metadata associated 19081c7c3c6aSMatthew Dillon * with resident pages. 19091c7c3c6aSMatthew Dillon */ 19101c7c3c6aSMatthew Dillon static void 1911f425ab8eSKonstantin Belousov swp_pager_meta_free(vm_object_t object, vm_pindex_t pindex, vm_pindex_t count) 19121c7c3c6aSMatthew Dillon { 1913f425ab8eSKonstantin Belousov struct swblk *sb; 1914230869e0SAlan Cox daddr_t first_free, num_free; 1915f425ab8eSKonstantin Belousov vm_pindex_t last; 1916230869e0SAlan Cox int i, limit, start; 19172928cef7SAlan Cox 1918ee620ea4SAlan Cox VM_OBJECT_ASSERT_WLOCKED(object); 19192e56b64fSKonstantin Belousov if (object->type != OBJT_SWAP || count == 0) 19201c7c3c6aSMatthew Dillon return; 19211c7c3c6aSMatthew Dillon 1922230869e0SAlan Cox first_free = SWAPBLK_NONE; 1923230869e0SAlan Cox num_free = 0; 1924230869e0SAlan Cox last = pindex + count; 1925f425ab8eSKonstantin Belousov for (;;) { 1926f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks, 1927f425ab8eSKonstantin Belousov rounddown(pindex, SWAP_META_PAGES)); 1928230869e0SAlan Cox if (sb == NULL || sb->p >= last) 19292e56b64fSKonstantin Belousov break; 1930230869e0SAlan Cox start = pindex > sb->p ? pindex - sb->p : 0; 1931230869e0SAlan Cox limit = last - sb->p < SWAP_META_PAGES ? last - sb->p : 1932230869e0SAlan Cox SWAP_META_PAGES; 1933230869e0SAlan Cox for (i = start; i < limit; i++) { 1934f425ab8eSKonstantin Belousov if (sb->d[i] == SWAPBLK_NONE) 1935f425ab8eSKonstantin Belousov continue; 1936230869e0SAlan Cox if (first_free + num_free == sb->d[i]) 1937230869e0SAlan Cox num_free++; 1938230869e0SAlan Cox else { 1939230869e0SAlan Cox swp_pager_freeswapspace(first_free, num_free); 1940230869e0SAlan Cox first_free = sb->d[i]; 1941230869e0SAlan Cox num_free = 1; 1942f425ab8eSKonstantin Belousov } 1943230869e0SAlan Cox sb->d[i] = SWAPBLK_NONE; 1944230869e0SAlan Cox } 1945230869e0SAlan Cox if (swp_pager_swblk_empty(sb, 0, start) && 1946230869e0SAlan Cox swp_pager_swblk_empty(sb, limit, SWAP_META_PAGES)) { 1947f425ab8eSKonstantin Belousov SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, 1948f425ab8eSKonstantin Belousov sb->p); 1949f425ab8eSKonstantin Belousov uma_zfree(swblk_zone, sb); 19501c7c3c6aSMatthew Dillon } 1951230869e0SAlan Cox pindex = sb->p + SWAP_META_PAGES; 19521c7c3c6aSMatthew Dillon } 1953230869e0SAlan Cox swp_pager_freeswapspace(first_free, num_free); 19541c7c3c6aSMatthew Dillon } 19551c7c3c6aSMatthew Dillon 19561c7c3c6aSMatthew Dillon /* 19571c7c3c6aSMatthew Dillon * SWP_PAGER_META_FREE_ALL() - destroy all swap metadata associated with object 19581c7c3c6aSMatthew Dillon * 19591c7c3c6aSMatthew Dillon * This routine locates and destroys all swap metadata associated with 19601c7c3c6aSMatthew Dillon * an object. 19611c7c3c6aSMatthew Dillon */ 19621c7c3c6aSMatthew Dillon static void 19631c7c3c6aSMatthew Dillon swp_pager_meta_free_all(vm_object_t object) 19641c7c3c6aSMatthew Dillon { 1965f425ab8eSKonstantin Belousov struct swblk *sb; 1966230869e0SAlan Cox daddr_t first_free, num_free; 1967f425ab8eSKonstantin Belousov vm_pindex_t pindex; 196871057cd2SKonstantin Belousov int i; 19691c7c3c6aSMatthew Dillon 197089f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 19711c7c3c6aSMatthew Dillon if (object->type != OBJT_SWAP) 19721c7c3c6aSMatthew Dillon return; 19731c7c3c6aSMatthew Dillon 1974230869e0SAlan Cox first_free = SWAPBLK_NONE; 1975230869e0SAlan Cox num_free = 0; 1976f425ab8eSKonstantin Belousov for (pindex = 0; (sb = SWAP_PCTRIE_LOOKUP_GE( 1977f425ab8eSKonstantin Belousov &object->un_pager.swp.swp_blks, pindex)) != NULL;) { 1978f425ab8eSKonstantin Belousov pindex = sb->p + SWAP_META_PAGES; 1979f425ab8eSKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) { 1980230869e0SAlan Cox if (sb->d[i] == SWAPBLK_NONE) 1981230869e0SAlan Cox continue; 1982230869e0SAlan Cox if (first_free + num_free == sb->d[i]) 1983230869e0SAlan Cox num_free++; 1984230869e0SAlan Cox else { 1985230869e0SAlan Cox swp_pager_freeswapspace(first_free, num_free); 1986230869e0SAlan Cox first_free = sb->d[i]; 1987230869e0SAlan Cox num_free = 1; 1988230869e0SAlan Cox } 19891c7c3c6aSMatthew Dillon } 1990f425ab8eSKonstantin Belousov SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, sb->p); 1991f425ab8eSKonstantin Belousov uma_zfree(swblk_zone, sb); 19921c7c3c6aSMatthew Dillon } 1993230869e0SAlan Cox swp_pager_freeswapspace(first_free, num_free); 19941c7c3c6aSMatthew Dillon } 19951c7c3c6aSMatthew Dillon 19961c7c3c6aSMatthew Dillon /* 19974abca9bbSAlan Cox * SWP_PAGER_METACTL() - misc control of swap meta data. 19981c7c3c6aSMatthew Dillon * 19994abca9bbSAlan Cox * This routine is capable of looking up, or removing swapblk 20004abca9bbSAlan Cox * assignments in the swap meta data. It returns the swapblk being 20014abca9bbSAlan Cox * looked-up, popped, or SWAPBLK_NONE if the block was invalid. 20021c7c3c6aSMatthew Dillon * 20031c7c3c6aSMatthew Dillon * When acting on a busy resident page and paging is in progress, we 20041c7c3c6aSMatthew Dillon * have to wait until paging is complete but otherwise can act on the 20051c7c3c6aSMatthew Dillon * busy page. 20061c7c3c6aSMatthew Dillon * 20074abca9bbSAlan Cox * SWM_POP remove from meta data but do not free it 20081c7c3c6aSMatthew Dillon */ 20091c7c3c6aSMatthew Dillon static daddr_t 20102f249180SPoul-Henning Kamp swp_pager_meta_ctl(vm_object_t object, vm_pindex_t pindex, int flags) 20112f249180SPoul-Henning Kamp { 2012f425ab8eSKonstantin Belousov struct swblk *sb; 20134dcc5c2dSMatthew Dillon daddr_t r1; 20144dcc5c2dSMatthew Dillon 20154abca9bbSAlan Cox if ((flags & SWM_POP) != 0) 2016ee620ea4SAlan Cox VM_OBJECT_ASSERT_WLOCKED(object); 2017ee620ea4SAlan Cox else 2018c25673ffSAttilio Rao VM_OBJECT_ASSERT_LOCKED(object); 2019ee620ea4SAlan Cox 20201c7c3c6aSMatthew Dillon /* 2021ee620ea4SAlan Cox * The meta data only exists if the object is OBJT_SWAP 20221c7c3c6aSMatthew Dillon * and even then might not be allocated yet. 20231c7c3c6aSMatthew Dillon */ 20244dcc5c2dSMatthew Dillon if (object->type != OBJT_SWAP) 20251c7c3c6aSMatthew Dillon return (SWAPBLK_NONE); 20261c7c3c6aSMatthew Dillon 2027f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, 2028f425ab8eSKonstantin Belousov rounddown(pindex, SWAP_META_PAGES)); 2029f425ab8eSKonstantin Belousov if (sb == NULL) 2030f425ab8eSKonstantin Belousov return (SWAPBLK_NONE); 2031f425ab8eSKonstantin Belousov r1 = sb->d[pindex % SWAP_META_PAGES]; 2032f425ab8eSKonstantin Belousov if (r1 == SWAPBLK_NONE) 2033f425ab8eSKonstantin Belousov return (SWAPBLK_NONE); 20344abca9bbSAlan Cox if ((flags & SWM_POP) != 0) { 2035f425ab8eSKonstantin Belousov sb->d[pindex % SWAP_META_PAGES] = SWAPBLK_NONE; 2036230869e0SAlan Cox if (swp_pager_swblk_empty(sb, 0, SWAP_META_PAGES)) { 2037f425ab8eSKonstantin Belousov SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, 2038f425ab8eSKonstantin Belousov rounddown(pindex, SWAP_META_PAGES)); 2039f425ab8eSKonstantin Belousov uma_zfree(swblk_zone, sb); 2040f425ab8eSKonstantin Belousov } 2041f425ab8eSKonstantin Belousov } 20421c7c3c6aSMatthew Dillon return (r1); 20431c7c3c6aSMatthew Dillon } 20441c7c3c6aSMatthew Dillon 2045e9c0cc15SPoul-Henning Kamp /* 204677d6fd97SKonstantin Belousov * Returns the least page index which is greater than or equal to the 204777d6fd97SKonstantin Belousov * parameter pindex and for which there is a swap block allocated. 204877d6fd97SKonstantin Belousov * Returns object's size if the object's type is not swap or if there 204977d6fd97SKonstantin Belousov * are no allocated swap blocks for the object after the requested 205077d6fd97SKonstantin Belousov * pindex. 205177d6fd97SKonstantin Belousov */ 205277d6fd97SKonstantin Belousov vm_pindex_t 205377d6fd97SKonstantin Belousov swap_pager_find_least(vm_object_t object, vm_pindex_t pindex) 205477d6fd97SKonstantin Belousov { 2055f425ab8eSKonstantin Belousov struct swblk *sb; 2056f425ab8eSKonstantin Belousov int i; 205777d6fd97SKonstantin Belousov 205877d6fd97SKonstantin Belousov VM_OBJECT_ASSERT_LOCKED(object); 2059f425ab8eSKonstantin Belousov if (object->type != OBJT_SWAP) 206077d6fd97SKonstantin Belousov return (object->size); 206177d6fd97SKonstantin Belousov 2062f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks, 2063f425ab8eSKonstantin Belousov rounddown(pindex, SWAP_META_PAGES)); 2064f425ab8eSKonstantin Belousov if (sb == NULL) 2065f425ab8eSKonstantin Belousov return (object->size); 2066f425ab8eSKonstantin Belousov if (sb->p < pindex) { 2067f425ab8eSKonstantin Belousov for (i = pindex % SWAP_META_PAGES; i < SWAP_META_PAGES; i++) { 2068f425ab8eSKonstantin Belousov if (sb->d[i] != SWAPBLK_NONE) 2069f425ab8eSKonstantin Belousov return (sb->p + i); 207077d6fd97SKonstantin Belousov } 2071f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks, 2072f425ab8eSKonstantin Belousov roundup(pindex, SWAP_META_PAGES)); 2073f425ab8eSKonstantin Belousov if (sb == NULL) 2074f425ab8eSKonstantin Belousov return (object->size); 207577d6fd97SKonstantin Belousov } 2076f425ab8eSKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) { 2077f425ab8eSKonstantin Belousov if (sb->d[i] != SWAPBLK_NONE) 2078f425ab8eSKonstantin Belousov return (sb->p + i); 207977d6fd97SKonstantin Belousov } 2080f425ab8eSKonstantin Belousov 2081f425ab8eSKonstantin Belousov /* 2082f425ab8eSKonstantin Belousov * We get here if a swblk is present in the trie but it 2083f425ab8eSKonstantin Belousov * doesn't map any blocks. 2084f425ab8eSKonstantin Belousov */ 2085f425ab8eSKonstantin Belousov MPASS(0); 2086f425ab8eSKonstantin Belousov return (object->size); 208777d6fd97SKonstantin Belousov } 208877d6fd97SKonstantin Belousov 208977d6fd97SKonstantin Belousov /* 2090e9c0cc15SPoul-Henning Kamp * System call swapon(name) enables swapping on device name, 2091e9c0cc15SPoul-Henning Kamp * which must be in the swdevsw. Return EBUSY 2092e9c0cc15SPoul-Henning Kamp * if already swapping on this device. 2093e9c0cc15SPoul-Henning Kamp */ 2094e9c0cc15SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_ 2095e9c0cc15SPoul-Henning Kamp struct swapon_args { 2096e9c0cc15SPoul-Henning Kamp char *name; 2097e9c0cc15SPoul-Henning Kamp }; 2098e9c0cc15SPoul-Henning Kamp #endif 2099e9c0cc15SPoul-Henning Kamp 2100e9c0cc15SPoul-Henning Kamp /* 2101e9c0cc15SPoul-Henning Kamp * MPSAFE 2102e9c0cc15SPoul-Henning Kamp */ 2103e9c0cc15SPoul-Henning Kamp /* ARGSUSED */ 2104e9c0cc15SPoul-Henning Kamp int 21058451d0ddSKip Macy sys_swapon(struct thread *td, struct swapon_args *uap) 2106e9c0cc15SPoul-Henning Kamp { 2107e9c0cc15SPoul-Henning Kamp struct vattr attr; 2108e9c0cc15SPoul-Henning Kamp struct vnode *vp; 2109e9c0cc15SPoul-Henning Kamp struct nameidata nd; 2110e9c0cc15SPoul-Henning Kamp int error; 2111e9c0cc15SPoul-Henning Kamp 2112acd3428bSRobert Watson error = priv_check(td, PRIV_SWAPON); 2113e9c0cc15SPoul-Henning Kamp if (error) 2114acd3428bSRobert Watson return (error); 2115e9c0cc15SPoul-Henning Kamp 211604533e1eSKonstantin Belousov sx_xlock(&swdev_syscall_lock); 2117e9c0cc15SPoul-Henning Kamp 2118e9c0cc15SPoul-Henning Kamp /* 2119e9c0cc15SPoul-Henning Kamp * Swap metadata may not fit in the KVM if we have physical 2120e9c0cc15SPoul-Henning Kamp * memory of >1GB. 2121e9c0cc15SPoul-Henning Kamp */ 2122f425ab8eSKonstantin Belousov if (swblk_zone == NULL) { 2123e9c0cc15SPoul-Henning Kamp error = ENOMEM; 2124e9c0cc15SPoul-Henning Kamp goto done; 2125e9c0cc15SPoul-Henning Kamp } 2126e9c0cc15SPoul-Henning Kamp 2127d9135e72SRobert Watson NDINIT(&nd, LOOKUP, ISOPEN | FOLLOW | AUDITVNODE1, UIO_USERSPACE, 2128d9135e72SRobert Watson uap->name, td); 2129e9c0cc15SPoul-Henning Kamp error = namei(&nd); 2130e9c0cc15SPoul-Henning Kamp if (error) 2131e9c0cc15SPoul-Henning Kamp goto done; 2132e9c0cc15SPoul-Henning Kamp 2133e9c0cc15SPoul-Henning Kamp NDFREE(&nd, NDF_ONLY_PNBUF); 2134e9c0cc15SPoul-Henning Kamp vp = nd.ni_vp; 2135e9c0cc15SPoul-Henning Kamp 213620da9c2eSPoul-Henning Kamp if (vn_isdisk(vp, &error)) { 213788ad2d7bSKonstantin Belousov error = swapongeom(vp); 213820da9c2eSPoul-Henning Kamp } else if (vp->v_type == VREG && 2139e9c0cc15SPoul-Henning Kamp (vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 && 21400359a12eSAttilio Rao (error = VOP_GETATTR(vp, &attr, td->td_ucred)) == 0) { 2141e9c0cc15SPoul-Henning Kamp /* 2142e9c0cc15SPoul-Henning Kamp * Allow direct swapping to NFS regular files in the same 2143e9c0cc15SPoul-Henning Kamp * way that nfs_mountroot() sets up diskless swapping. 2144e9c0cc15SPoul-Henning Kamp */ 214559efee01SPoul-Henning Kamp error = swaponvp(td, vp, attr.va_size / DEV_BSIZE); 2146e9c0cc15SPoul-Henning Kamp } 2147e9c0cc15SPoul-Henning Kamp 2148e9c0cc15SPoul-Henning Kamp if (error) 2149e9c0cc15SPoul-Henning Kamp vrele(vp); 2150e9c0cc15SPoul-Henning Kamp done: 215104533e1eSKonstantin Belousov sx_xunlock(&swdev_syscall_lock); 2152e9c0cc15SPoul-Henning Kamp return (error); 2153e9c0cc15SPoul-Henning Kamp } 2154e9c0cc15SPoul-Henning Kamp 21553ff863f1SDag-Erling Smørgrav /* 21563ff863f1SDag-Erling Smørgrav * Check that the total amount of swap currently configured does not 21573ff863f1SDag-Erling Smørgrav * exceed half the theoretical maximum. If it does, print a warning 215835872e79SKonstantin Belousov * message. 21593ff863f1SDag-Erling Smørgrav */ 216035872e79SKonstantin Belousov static void 216135872e79SKonstantin Belousov swapon_check_swzone(void) 21623ff863f1SDag-Erling Smørgrav { 216335872e79SKonstantin Belousov unsigned long maxpages, npages; 21643ff863f1SDag-Erling Smørgrav 216535872e79SKonstantin Belousov npages = swap_total / PAGE_SIZE; 21663ff863f1SDag-Erling Smørgrav /* absolute maximum we can handle assuming 100% efficiency */ 2167f425ab8eSKonstantin Belousov maxpages = uma_zone_get_max(swblk_zone) * SWAP_META_PAGES; 21683ff863f1SDag-Erling Smørgrav 21693ff863f1SDag-Erling Smørgrav /* recommend using no more than half that amount */ 21703ff863f1SDag-Erling Smørgrav if (npages > maxpages / 2) { 21713ff863f1SDag-Erling Smørgrav printf("warning: total configured swap (%lu pages) " 21723ff863f1SDag-Erling Smørgrav "exceeds maximum recommended amount (%lu pages).\n", 21739462305cSSergey Kandaurov npages, maxpages / 2); 21743ff863f1SDag-Erling Smørgrav printf("warning: increase kern.maxswzone " 21753ff863f1SDag-Erling Smørgrav "or reduce amount of swap.\n"); 21763ff863f1SDag-Erling Smørgrav } 21773ff863f1SDag-Erling Smørgrav } 21783ff863f1SDag-Erling Smørgrav 217959efee01SPoul-Henning Kamp static void 21802cc718a1SKonstantin Belousov swaponsomething(struct vnode *vp, void *id, u_long nblks, 21812cc718a1SKonstantin Belousov sw_strategy_t *strategy, sw_close_t *close, dev_t dev, int flags) 2182e9c0cc15SPoul-Henning Kamp { 21832d9974c1SAlan Cox struct swdevt *sp, *tsp; 2184e9c0cc15SPoul-Henning Kamp swblk_t dvbase; 21858f60c087SPoul-Henning Kamp u_long mblocks; 2186e9c0cc15SPoul-Henning Kamp 2187e9c0cc15SPoul-Henning Kamp /* 2188e9c0cc15SPoul-Henning Kamp * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks. 2189e9c0cc15SPoul-Henning Kamp * First chop nblks off to page-align it, then convert. 2190e9c0cc15SPoul-Henning Kamp * 2191e9c0cc15SPoul-Henning Kamp * sw->sw_nblks is in page-sized chunks now too. 2192e9c0cc15SPoul-Henning Kamp */ 2193e9c0cc15SPoul-Henning Kamp nblks &= ~(ctodb(1) - 1); 2194e9c0cc15SPoul-Henning Kamp nblks = dbtoc(nblks); 2195e9c0cc15SPoul-Henning Kamp 21966e903bd0SKonstantin Belousov /* 21976e903bd0SKonstantin Belousov * If we go beyond this, we get overflows in the radix 21986e903bd0SKonstantin Belousov * tree bitmap code. 21996e903bd0SKonstantin Belousov */ 22006e903bd0SKonstantin Belousov mblocks = 0x40000000 / BLIST_META_RADIX; 22016e903bd0SKonstantin Belousov if (nblks > mblocks) { 22026e903bd0SKonstantin Belousov printf( 22036e903bd0SKonstantin Belousov "WARNING: reducing swap size to maximum of %luMB per unit\n", 22046e903bd0SKonstantin Belousov mblocks / 1024 / 1024 * PAGE_SIZE); 22056e903bd0SKonstantin Belousov nblks = mblocks; 22066e903bd0SKonstantin Belousov } 22076e903bd0SKonstantin Belousov 22088f60c087SPoul-Henning Kamp sp = malloc(sizeof *sp, M_VMPGDATA, M_WAITOK | M_ZERO); 2209dee34ca4SPoul-Henning Kamp sp->sw_vp = vp; 2210dee34ca4SPoul-Henning Kamp sp->sw_id = id; 2211f3732fd1SPoul-Henning Kamp sp->sw_dev = dev; 22128d677ef9SPoul-Henning Kamp sp->sw_flags = 0; 2213e9c0cc15SPoul-Henning Kamp sp->sw_nblks = nblks; 2214e9c0cc15SPoul-Henning Kamp sp->sw_used = 0; 221559efee01SPoul-Henning Kamp sp->sw_strategy = strategy; 2216dee34ca4SPoul-Henning Kamp sp->sw_close = close; 22172cc718a1SKonstantin Belousov sp->sw_flags = flags; 2218e9c0cc15SPoul-Henning Kamp 2219c8c7ad92SKip Macy sp->sw_blist = blist_create(nblks, M_WAITOK); 2220e9c0cc15SPoul-Henning Kamp /* 2221ef3c5abdSPoul-Henning Kamp * Do not free the first two block in order to avoid overwriting 22228f60c087SPoul-Henning Kamp * any bsd label at the front of the partition 2223e9c0cc15SPoul-Henning Kamp */ 2224ef3c5abdSPoul-Henning Kamp blist_free(sp->sw_blist, 2, nblks - 2); 2225e9c0cc15SPoul-Henning Kamp 22262d9974c1SAlan Cox dvbase = 0; 222720da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 22282d9974c1SAlan Cox TAILQ_FOREACH(tsp, &swtailq, sw_list) { 22292d9974c1SAlan Cox if (tsp->sw_end >= dvbase) { 22302d9974c1SAlan Cox /* 22312d9974c1SAlan Cox * We put one uncovered page between the devices 22322d9974c1SAlan Cox * in order to definitively prevent any cross-device 22332d9974c1SAlan Cox * I/O requests 22342d9974c1SAlan Cox */ 22352d9974c1SAlan Cox dvbase = tsp->sw_end + 1; 22362d9974c1SAlan Cox } 22372d9974c1SAlan Cox } 22382d9974c1SAlan Cox sp->sw_first = dvbase; 22392d9974c1SAlan Cox sp->sw_end = dvbase + nblks; 22408f60c087SPoul-Henning Kamp TAILQ_INSERT_TAIL(&swtailq, sp, sw_list); 22418f60c087SPoul-Henning Kamp nswapdev++; 2242761097c8SAlan Cox swap_pager_avail += nblks - 2; 22433364c323SKonstantin Belousov swap_total += (vm_ooffset_t)nblks * PAGE_SIZE; 224435872e79SKonstantin Belousov swapon_check_swzone(); 2245d05bc129SAlan Cox swp_sizecheck(); 2246d05bc129SAlan Cox mtx_unlock(&sw_dev_mtx); 2247b1fd102eSMark Johnston EVENTHANDLER_INVOKE(swapon, sp); 224859efee01SPoul-Henning Kamp } 2249e9c0cc15SPoul-Henning Kamp 2250e9c0cc15SPoul-Henning Kamp /* 2251e9c0cc15SPoul-Henning Kamp * SYSCALL: swapoff(devname) 2252e9c0cc15SPoul-Henning Kamp * 2253e9c0cc15SPoul-Henning Kamp * Disable swapping on the given device. 2254dee34ca4SPoul-Henning Kamp * 2255dee34ca4SPoul-Henning Kamp * XXX: Badly designed system call: it should use a device index 2256dee34ca4SPoul-Henning Kamp * rather than filename as specification. We keep sw_vp around 2257dee34ca4SPoul-Henning Kamp * only to make this work. 2258e9c0cc15SPoul-Henning Kamp */ 2259e9c0cc15SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_ 2260e9c0cc15SPoul-Henning Kamp struct swapoff_args { 2261e9c0cc15SPoul-Henning Kamp char *name; 2262e9c0cc15SPoul-Henning Kamp }; 2263e9c0cc15SPoul-Henning Kamp #endif 2264e9c0cc15SPoul-Henning Kamp 2265e9c0cc15SPoul-Henning Kamp /* 2266e9c0cc15SPoul-Henning Kamp * MPSAFE 2267e9c0cc15SPoul-Henning Kamp */ 2268e9c0cc15SPoul-Henning Kamp /* ARGSUSED */ 2269e9c0cc15SPoul-Henning Kamp int 22708451d0ddSKip Macy sys_swapoff(struct thread *td, struct swapoff_args *uap) 2271e9c0cc15SPoul-Henning Kamp { 2272e9c0cc15SPoul-Henning Kamp struct vnode *vp; 2273e9c0cc15SPoul-Henning Kamp struct nameidata nd; 2274e9c0cc15SPoul-Henning Kamp struct swdevt *sp; 22758f60c087SPoul-Henning Kamp int error; 2276e9c0cc15SPoul-Henning Kamp 2277acd3428bSRobert Watson error = priv_check(td, PRIV_SWAPOFF); 2278e9c0cc15SPoul-Henning Kamp if (error) 22790909f38aSPawel Jakub Dawidek return (error); 2280e9c0cc15SPoul-Henning Kamp 228104533e1eSKonstantin Belousov sx_xlock(&swdev_syscall_lock); 2282e9c0cc15SPoul-Henning Kamp 2283d9135e72SRobert Watson NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, UIO_USERSPACE, uap->name, 2284d9135e72SRobert Watson td); 2285e9c0cc15SPoul-Henning Kamp error = namei(&nd); 2286e9c0cc15SPoul-Henning Kamp if (error) 2287e9c0cc15SPoul-Henning Kamp goto done; 2288e9c0cc15SPoul-Henning Kamp NDFREE(&nd, NDF_ONLY_PNBUF); 2289e9c0cc15SPoul-Henning Kamp vp = nd.ni_vp; 2290e9c0cc15SPoul-Henning Kamp 229120da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 22928f60c087SPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 2293dee34ca4SPoul-Henning Kamp if (sp->sw_vp == vp) 22940909f38aSPawel Jakub Dawidek break; 2295e9c0cc15SPoul-Henning Kamp } 229620da9c2eSPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 22970909f38aSPawel Jakub Dawidek if (sp == NULL) { 2298e9c0cc15SPoul-Henning Kamp error = EINVAL; 2299e9c0cc15SPoul-Henning Kamp goto done; 23000909f38aSPawel Jakub Dawidek } 230135918c55SChristian S.J. Peron error = swapoff_one(sp, td->td_ucred); 23020909f38aSPawel Jakub Dawidek done: 230304533e1eSKonstantin Belousov sx_xunlock(&swdev_syscall_lock); 23040909f38aSPawel Jakub Dawidek return (error); 23050909f38aSPawel Jakub Dawidek } 23060909f38aSPawel Jakub Dawidek 23070909f38aSPawel Jakub Dawidek static int 230835918c55SChristian S.J. Peron swapoff_one(struct swdevt *sp, struct ucred *cred) 23090909f38aSPawel Jakub Dawidek { 231003bdd65fSAlan Cox u_long nblks; 2311e9c0cc15SPoul-Henning Kamp #ifdef MAC 23120909f38aSPawel Jakub Dawidek int error; 2313e9c0cc15SPoul-Henning Kamp #endif 2314e9c0cc15SPoul-Henning Kamp 231504533e1eSKonstantin Belousov sx_assert(&swdev_syscall_lock, SA_XLOCKED); 23160909f38aSPawel Jakub Dawidek #ifdef MAC 2317cb05b60aSAttilio Rao (void) vn_lock(sp->sw_vp, LK_EXCLUSIVE | LK_RETRY); 231835918c55SChristian S.J. Peron error = mac_system_check_swapoff(cred, sp->sw_vp); 231922db15c0SAttilio Rao (void) VOP_UNLOCK(sp->sw_vp, 0); 23200909f38aSPawel Jakub Dawidek if (error != 0) 23210909f38aSPawel Jakub Dawidek return (error); 23220909f38aSPawel Jakub Dawidek #endif 2323e9c0cc15SPoul-Henning Kamp nblks = sp->sw_nblks; 2324e9c0cc15SPoul-Henning Kamp 2325e9c0cc15SPoul-Henning Kamp /* 2326e9c0cc15SPoul-Henning Kamp * We can turn off this swap device safely only if the 2327e9c0cc15SPoul-Henning Kamp * available virtual memory in the system will fit the amount 2328e9c0cc15SPoul-Henning Kamp * of data we will have to page back in, plus an epsilon so 2329e9c0cc15SPoul-Henning Kamp * the system doesn't become critically low on swap space. 2330e9c0cc15SPoul-Henning Kamp */ 2331e2068d0bSJeff Roberson if (vm_free_count() + swap_pager_avail < nblks + nswap_lowat) 23320909f38aSPawel Jakub Dawidek return (ENOMEM); 2333e9c0cc15SPoul-Henning Kamp 2334e9c0cc15SPoul-Henning Kamp /* 2335e9c0cc15SPoul-Henning Kamp * Prevent further allocations on this device. 2336e9c0cc15SPoul-Henning Kamp */ 23372928cef7SAlan Cox mtx_lock(&sw_dev_mtx); 2338e9c0cc15SPoul-Henning Kamp sp->sw_flags |= SW_CLOSING; 233903bdd65fSAlan Cox swap_pager_avail -= blist_fill(sp->sw_blist, 0, nblks); 23403364c323SKonstantin Belousov swap_total -= (vm_ooffset_t)nblks * PAGE_SIZE; 23412928cef7SAlan Cox mtx_unlock(&sw_dev_mtx); 2342e9c0cc15SPoul-Henning Kamp 2343e9c0cc15SPoul-Henning Kamp /* 2344e9c0cc15SPoul-Henning Kamp * Page in the contents of the device and close it. 2345e9c0cc15SPoul-Henning Kamp */ 2346b3fed13eSDavid Schultz swap_pager_swapoff(sp); 2347e9c0cc15SPoul-Henning Kamp 234835918c55SChristian S.J. Peron sp->sw_close(curthread, sp); 234920da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 23509e3e3fe5SWarner Losh sp->sw_id = NULL; 23518f60c087SPoul-Henning Kamp TAILQ_REMOVE(&swtailq, sp, sw_list); 23520676a140SAlan Cox nswapdev--; 23537dea2c2eSAlan Cox if (nswapdev == 0) { 23547dea2c2eSAlan Cox swap_pager_full = 2; 23557dea2c2eSAlan Cox swap_pager_almost_full = 1; 23567dea2c2eSAlan Cox } 23578f60c087SPoul-Henning Kamp if (swdevhd == sp) 23588f60c087SPoul-Henning Kamp swdevhd = NULL; 2359d05bc129SAlan Cox mtx_unlock(&sw_dev_mtx); 23608f60c087SPoul-Henning Kamp blist_destroy(sp->sw_blist); 23618f60c087SPoul-Henning Kamp free(sp, M_VMPGDATA); 23620909f38aSPawel Jakub Dawidek return (0); 23630909f38aSPawel Jakub Dawidek } 2364e9c0cc15SPoul-Henning Kamp 23650909f38aSPawel Jakub Dawidek void 23660909f38aSPawel Jakub Dawidek swapoff_all(void) 23670909f38aSPawel Jakub Dawidek { 23680909f38aSPawel Jakub Dawidek struct swdevt *sp, *spt; 23690909f38aSPawel Jakub Dawidek const char *devname; 23700909f38aSPawel Jakub Dawidek int error; 23710909f38aSPawel Jakub Dawidek 237204533e1eSKonstantin Belousov sx_xlock(&swdev_syscall_lock); 23730909f38aSPawel Jakub Dawidek 23740909f38aSPawel Jakub Dawidek mtx_lock(&sw_dev_mtx); 23750909f38aSPawel Jakub Dawidek TAILQ_FOREACH_SAFE(sp, &swtailq, sw_list, spt) { 23760909f38aSPawel Jakub Dawidek mtx_unlock(&sw_dev_mtx); 23770909f38aSPawel Jakub Dawidek if (vn_isdisk(sp->sw_vp, NULL)) 23787870adb6SEd Schouten devname = devtoname(sp->sw_vp->v_rdev); 23790909f38aSPawel Jakub Dawidek else 23800909f38aSPawel Jakub Dawidek devname = "[file]"; 238135918c55SChristian S.J. Peron error = swapoff_one(sp, thread0.td_ucred); 23820909f38aSPawel Jakub Dawidek if (error != 0) { 23830909f38aSPawel Jakub Dawidek printf("Cannot remove swap device %s (error=%d), " 23840909f38aSPawel Jakub Dawidek "skipping.\n", devname, error); 23850909f38aSPawel Jakub Dawidek } else if (bootverbose) { 23860909f38aSPawel Jakub Dawidek printf("Swap device %s removed.\n", devname); 23870909f38aSPawel Jakub Dawidek } 23880909f38aSPawel Jakub Dawidek mtx_lock(&sw_dev_mtx); 23890909f38aSPawel Jakub Dawidek } 23900909f38aSPawel Jakub Dawidek mtx_unlock(&sw_dev_mtx); 23910909f38aSPawel Jakub Dawidek 239204533e1eSKonstantin Belousov sx_xunlock(&swdev_syscall_lock); 2393e9c0cc15SPoul-Henning Kamp } 2394e9c0cc15SPoul-Henning Kamp 2395567104a1SPoul-Henning Kamp void 2396567104a1SPoul-Henning Kamp swap_pager_status(int *total, int *used) 2397567104a1SPoul-Henning Kamp { 2398567104a1SPoul-Henning Kamp struct swdevt *sp; 2399567104a1SPoul-Henning Kamp 2400567104a1SPoul-Henning Kamp *total = 0; 2401567104a1SPoul-Henning Kamp *used = 0; 240220da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 24038f60c087SPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 2404567104a1SPoul-Henning Kamp *total += sp->sw_nblks; 2405567104a1SPoul-Henning Kamp *used += sp->sw_used; 2406567104a1SPoul-Henning Kamp } 240720da9c2eSPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 2408567104a1SPoul-Henning Kamp } 2409567104a1SPoul-Henning Kamp 2410dda4f960SKonstantin Belousov int 2411dda4f960SKonstantin Belousov swap_dev_info(int name, struct xswdev *xs, char *devname, size_t len) 2412dda4f960SKonstantin Belousov { 2413dda4f960SKonstantin Belousov struct swdevt *sp; 24147870adb6SEd Schouten const char *tmp_devname; 2415dda4f960SKonstantin Belousov int error, n; 2416dda4f960SKonstantin Belousov 2417dda4f960SKonstantin Belousov n = 0; 2418dda4f960SKonstantin Belousov error = ENOENT; 2419dda4f960SKonstantin Belousov mtx_lock(&sw_dev_mtx); 2420dda4f960SKonstantin Belousov TAILQ_FOREACH(sp, &swtailq, sw_list) { 2421dda4f960SKonstantin Belousov if (n != name) { 2422dda4f960SKonstantin Belousov n++; 2423dda4f960SKonstantin Belousov continue; 2424dda4f960SKonstantin Belousov } 2425dda4f960SKonstantin Belousov xs->xsw_version = XSWDEV_VERSION; 2426dda4f960SKonstantin Belousov xs->xsw_dev = sp->sw_dev; 2427dda4f960SKonstantin Belousov xs->xsw_flags = sp->sw_flags; 2428dda4f960SKonstantin Belousov xs->xsw_nblks = sp->sw_nblks; 2429dda4f960SKonstantin Belousov xs->xsw_used = sp->sw_used; 2430dda4f960SKonstantin Belousov if (devname != NULL) { 2431dda4f960SKonstantin Belousov if (vn_isdisk(sp->sw_vp, NULL)) 24327870adb6SEd Schouten tmp_devname = devtoname(sp->sw_vp->v_rdev); 2433dda4f960SKonstantin Belousov else 2434dda4f960SKonstantin Belousov tmp_devname = "[file]"; 2435dda4f960SKonstantin Belousov strncpy(devname, tmp_devname, len); 2436dda4f960SKonstantin Belousov } 2437dda4f960SKonstantin Belousov error = 0; 2438dda4f960SKonstantin Belousov break; 2439dda4f960SKonstantin Belousov } 2440dda4f960SKonstantin Belousov mtx_unlock(&sw_dev_mtx); 2441dda4f960SKonstantin Belousov return (error); 2442dda4f960SKonstantin Belousov } 2443dda4f960SKonstantin Belousov 244469921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11) 244569921123SKonstantin Belousov #define XSWDEV_VERSION_11 1 244669921123SKonstantin Belousov struct xswdev11 { 244769921123SKonstantin Belousov u_int xsw_version; 244869921123SKonstantin Belousov uint32_t xsw_dev; 244969921123SKonstantin Belousov int xsw_flags; 245069921123SKonstantin Belousov int xsw_nblks; 245169921123SKonstantin Belousov int xsw_used; 245269921123SKonstantin Belousov }; 245369921123SKonstantin Belousov #endif 245469921123SKonstantin Belousov 2455e9c0cc15SPoul-Henning Kamp static int 2456e9c0cc15SPoul-Henning Kamp sysctl_vm_swap_info(SYSCTL_HANDLER_ARGS) 2457e9c0cc15SPoul-Henning Kamp { 2458e9c0cc15SPoul-Henning Kamp struct xswdev xs; 245969921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11) 246069921123SKonstantin Belousov struct xswdev11 xs11; 246169921123SKonstantin Belousov #endif 2462dda4f960SKonstantin Belousov int error; 2463e9c0cc15SPoul-Henning Kamp 2464e9c0cc15SPoul-Henning Kamp if (arg2 != 1) /* name length */ 2465e9c0cc15SPoul-Henning Kamp return (EINVAL); 2466dda4f960SKonstantin Belousov error = swap_dev_info(*(int *)arg1, &xs, NULL, 0); 2467dda4f960SKonstantin Belousov if (error != 0) 2468dda4f960SKonstantin Belousov return (error); 246969921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11) 247069921123SKonstantin Belousov if (req->oldlen == sizeof(xs11)) { 247169921123SKonstantin Belousov xs11.xsw_version = XSWDEV_VERSION_11; 247269921123SKonstantin Belousov xs11.xsw_dev = xs.xsw_dev; /* truncation */ 247369921123SKonstantin Belousov xs11.xsw_flags = xs.xsw_flags; 247469921123SKonstantin Belousov xs11.xsw_nblks = xs.xsw_nblks; 247569921123SKonstantin Belousov xs11.xsw_used = xs.xsw_used; 247669921123SKonstantin Belousov error = SYSCTL_OUT(req, &xs11, sizeof(xs11)); 247769921123SKonstantin Belousov } else 247869921123SKonstantin Belousov #endif 2479e9c0cc15SPoul-Henning Kamp error = SYSCTL_OUT(req, &xs, sizeof(xs)); 2480e9c0cc15SPoul-Henning Kamp return (error); 2481e9c0cc15SPoul-Henning Kamp } 2482e9c0cc15SPoul-Henning Kamp 24838f60c087SPoul-Henning Kamp SYSCTL_INT(_vm, OID_AUTO, nswapdev, CTLFLAG_RD, &nswapdev, 0, 2484e9c0cc15SPoul-Henning Kamp "Number of swap devices"); 24854c36e917SKonstantin Belousov SYSCTL_NODE(_vm, OID_AUTO, swap_info, CTLFLAG_RD | CTLFLAG_MPSAFE, 24864c36e917SKonstantin Belousov sysctl_vm_swap_info, 2487e9c0cc15SPoul-Henning Kamp "Swap statistics by device"); 2488ec38b344SPoul-Henning Kamp 2489ec38b344SPoul-Henning Kamp /* 2490f425ab8eSKonstantin Belousov * Count the approximate swap usage in pages for a vmspace. The 2491f425ab8eSKonstantin Belousov * shadowed or not yet copied on write swap blocks are not accounted. 2492ec38b344SPoul-Henning Kamp * The map must be locked. 2493ec38b344SPoul-Henning Kamp */ 24942860553aSRebecca Cran long 2495ec38b344SPoul-Henning Kamp vmspace_swap_count(struct vmspace *vmspace) 2496ec38b344SPoul-Henning Kamp { 249765d8409cSRebecca Cran vm_map_t map; 2498ec38b344SPoul-Henning Kamp vm_map_entry_t cur; 249965d8409cSRebecca Cran vm_object_t object; 2500f425ab8eSKonstantin Belousov struct swblk *sb; 2501f425ab8eSKonstantin Belousov vm_pindex_t e, pi; 2502f425ab8eSKonstantin Belousov long count; 2503f425ab8eSKonstantin Belousov int i; 250465d8409cSRebecca Cran 250565d8409cSRebecca Cran map = &vmspace->vm_map; 250665d8409cSRebecca Cran count = 0; 2507ec38b344SPoul-Henning Kamp 2508ec38b344SPoul-Henning Kamp for (cur = map->header.next; cur != &map->header; cur = cur->next) { 2509f425ab8eSKonstantin Belousov if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) != 0) 2510f425ab8eSKonstantin Belousov continue; 2511f425ab8eSKonstantin Belousov object = cur->object.vm_object; 2512f425ab8eSKonstantin Belousov if (object == NULL || object->type != OBJT_SWAP) 2513f425ab8eSKonstantin Belousov continue; 2514f425ab8eSKonstantin Belousov VM_OBJECT_RLOCK(object); 2515f425ab8eSKonstantin Belousov if (object->type != OBJT_SWAP) 2516f425ab8eSKonstantin Belousov goto unlock; 2517f425ab8eSKonstantin Belousov pi = OFF_TO_IDX(cur->offset); 2518f425ab8eSKonstantin Belousov e = pi + OFF_TO_IDX(cur->end - cur->start); 2519f425ab8eSKonstantin Belousov for (;; pi = sb->p + SWAP_META_PAGES) { 2520f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP_GE( 2521f425ab8eSKonstantin Belousov &object->un_pager.swp.swp_blks, pi); 2522f425ab8eSKonstantin Belousov if (sb == NULL || sb->p >= e) 2523f425ab8eSKonstantin Belousov break; 2524f425ab8eSKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) { 2525f425ab8eSKonstantin Belousov if (sb->p + i < e && 2526f425ab8eSKonstantin Belousov sb->d[i] != SWAPBLK_NONE) 2527f425ab8eSKonstantin Belousov count++; 2528ec38b344SPoul-Henning Kamp } 2529ec38b344SPoul-Henning Kamp } 2530f425ab8eSKonstantin Belousov unlock: 2531f425ab8eSKonstantin Belousov VM_OBJECT_RUNLOCK(object); 2532ec38b344SPoul-Henning Kamp } 2533ec38b344SPoul-Henning Kamp return (count); 2534ec38b344SPoul-Henning Kamp } 2535dee34ca4SPoul-Henning Kamp 2536dee34ca4SPoul-Henning Kamp /* 2537dee34ca4SPoul-Henning Kamp * GEOM backend 2538dee34ca4SPoul-Henning Kamp * 2539dee34ca4SPoul-Henning Kamp * Swapping onto disk devices. 2540dee34ca4SPoul-Henning Kamp * 2541dee34ca4SPoul-Henning Kamp */ 2542dee34ca4SPoul-Henning Kamp 25435721c9c7SPoul-Henning Kamp static g_orphan_t swapgeom_orphan; 25445721c9c7SPoul-Henning Kamp 2545dee34ca4SPoul-Henning Kamp static struct g_class g_swap_class = { 2546dee34ca4SPoul-Henning Kamp .name = "SWAP", 25475721c9c7SPoul-Henning Kamp .version = G_VERSION, 25485721c9c7SPoul-Henning Kamp .orphan = swapgeom_orphan, 2549dee34ca4SPoul-Henning Kamp }; 2550dee34ca4SPoul-Henning Kamp 2551dee34ca4SPoul-Henning Kamp DECLARE_GEOM_CLASS(g_swap_class, g_class); 2552dee34ca4SPoul-Henning Kamp 2553dee34ca4SPoul-Henning Kamp 2554dee34ca4SPoul-Henning Kamp static void 25553398491bSAlexander Motin swapgeom_close_ev(void *arg, int flags) 25563398491bSAlexander Motin { 25573398491bSAlexander Motin struct g_consumer *cp; 25583398491bSAlexander Motin 25593398491bSAlexander Motin cp = arg; 25603398491bSAlexander Motin g_access(cp, -1, -1, 0); 25613398491bSAlexander Motin g_detach(cp); 25623398491bSAlexander Motin g_destroy_consumer(cp); 25633398491bSAlexander Motin } 25643398491bSAlexander Motin 25659e3e3fe5SWarner Losh /* 25669e3e3fe5SWarner Losh * Add a reference to the g_consumer for an inflight transaction. 25679e3e3fe5SWarner Losh */ 25689e3e3fe5SWarner Losh static void 25699e3e3fe5SWarner Losh swapgeom_acquire(struct g_consumer *cp) 25709e3e3fe5SWarner Losh { 25719e3e3fe5SWarner Losh 25729e3e3fe5SWarner Losh mtx_assert(&sw_dev_mtx, MA_OWNED); 25739e3e3fe5SWarner Losh cp->index++; 25749e3e3fe5SWarner Losh } 25759e3e3fe5SWarner Losh 25769e3e3fe5SWarner Losh /* 25770c657d22SKonstantin Belousov * Remove a reference from the g_consumer. Post a close event if all 25780c657d22SKonstantin Belousov * references go away, since the function might be called from the 25790c657d22SKonstantin Belousov * biodone context. 25809e3e3fe5SWarner Losh */ 25819e3e3fe5SWarner Losh static void 25829e3e3fe5SWarner Losh swapgeom_release(struct g_consumer *cp, struct swdevt *sp) 25839e3e3fe5SWarner Losh { 25849e3e3fe5SWarner Losh 25859e3e3fe5SWarner Losh mtx_assert(&sw_dev_mtx, MA_OWNED); 25869e3e3fe5SWarner Losh cp->index--; 25879e3e3fe5SWarner Losh if (cp->index == 0) { 25889e3e3fe5SWarner Losh if (g_post_event(swapgeom_close_ev, cp, M_NOWAIT, NULL) == 0) 25899e3e3fe5SWarner Losh sp->sw_id = NULL; 25909e3e3fe5SWarner Losh } 25919e3e3fe5SWarner Losh } 25929e3e3fe5SWarner Losh 25933398491bSAlexander Motin static void 2594dee34ca4SPoul-Henning Kamp swapgeom_done(struct bio *bp2) 2595dee34ca4SPoul-Henning Kamp { 25963398491bSAlexander Motin struct swdevt *sp; 2597dee34ca4SPoul-Henning Kamp struct buf *bp; 25983398491bSAlexander Motin struct g_consumer *cp; 2599dee34ca4SPoul-Henning Kamp 2600dee34ca4SPoul-Henning Kamp bp = bp2->bio_caller2; 26013398491bSAlexander Motin cp = bp2->bio_from; 2602c5d3d25eSPoul-Henning Kamp bp->b_ioflags = bp2->bio_flags; 2603dee34ca4SPoul-Henning Kamp if (bp2->bio_error) 2604dee34ca4SPoul-Henning Kamp bp->b_ioflags |= BIO_ERROR; 2605c5d3d25eSPoul-Henning Kamp bp->b_resid = bp->b_bcount - bp2->bio_completed; 2606c5d3d25eSPoul-Henning Kamp bp->b_error = bp2->bio_error; 2607dee34ca4SPoul-Henning Kamp bufdone(bp); 26083398491bSAlexander Motin sp = bp2->bio_caller1; 26099e3e3fe5SWarner Losh mtx_lock(&sw_dev_mtx); 26109e3e3fe5SWarner Losh swapgeom_release(cp, sp); 26113398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 2612dee34ca4SPoul-Henning Kamp g_destroy_bio(bp2); 2613dee34ca4SPoul-Henning Kamp } 2614dee34ca4SPoul-Henning Kamp 2615dee34ca4SPoul-Henning Kamp static void 2616dee34ca4SPoul-Henning Kamp swapgeom_strategy(struct buf *bp, struct swdevt *sp) 2617dee34ca4SPoul-Henning Kamp { 2618dee34ca4SPoul-Henning Kamp struct bio *bio; 2619dee34ca4SPoul-Henning Kamp struct g_consumer *cp; 2620dee34ca4SPoul-Henning Kamp 26213398491bSAlexander Motin mtx_lock(&sw_dev_mtx); 2622dee34ca4SPoul-Henning Kamp cp = sp->sw_id; 2623dee34ca4SPoul-Henning Kamp if (cp == NULL) { 26243398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 2625dee34ca4SPoul-Henning Kamp bp->b_error = ENXIO; 2626dee34ca4SPoul-Henning Kamp bp->b_ioflags |= BIO_ERROR; 2627dee34ca4SPoul-Henning Kamp bufdone(bp); 2628dee34ca4SPoul-Henning Kamp return; 2629dee34ca4SPoul-Henning Kamp } 26309e3e3fe5SWarner Losh swapgeom_acquire(cp); 26313398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 263211041003SKonstantin Belousov if (bp->b_iocmd == BIO_WRITE) 263311041003SKonstantin Belousov bio = g_new_bio(); 263411041003SKonstantin Belousov else 26354f8205e5SPoul-Henning Kamp bio = g_alloc_bio(); 26364f8205e5SPoul-Henning Kamp if (bio == NULL) { 26379e3e3fe5SWarner Losh mtx_lock(&sw_dev_mtx); 26389e3e3fe5SWarner Losh swapgeom_release(cp, sp); 26399e3e3fe5SWarner Losh mtx_unlock(&sw_dev_mtx); 26403e5b6861SPoul-Henning Kamp bp->b_error = ENOMEM; 26413e5b6861SPoul-Henning Kamp bp->b_ioflags |= BIO_ERROR; 26423e5b6861SPoul-Henning Kamp bufdone(bp); 26433e5b6861SPoul-Henning Kamp return; 26443e5b6861SPoul-Henning Kamp } 264511041003SKonstantin Belousov 26463398491bSAlexander Motin bio->bio_caller1 = sp; 2647dee34ca4SPoul-Henning Kamp bio->bio_caller2 = bp; 2648c5d3d25eSPoul-Henning Kamp bio->bio_cmd = bp->b_iocmd; 2649dee34ca4SPoul-Henning Kamp bio->bio_offset = (bp->b_blkno - sp->sw_first) * PAGE_SIZE; 2650dee34ca4SPoul-Henning Kamp bio->bio_length = bp->b_bcount; 2651dee34ca4SPoul-Henning Kamp bio->bio_done = swapgeom_done; 2652fade8dd7SJeff Roberson if (!buf_mapped(bp)) { 26532cc718a1SKonstantin Belousov bio->bio_ma = bp->b_pages; 26542cc718a1SKonstantin Belousov bio->bio_data = unmapped_buf; 26552cc718a1SKonstantin Belousov bio->bio_ma_offset = (vm_offset_t)bp->b_offset & PAGE_MASK; 26562cc718a1SKonstantin Belousov bio->bio_ma_n = bp->b_npages; 26572cc718a1SKonstantin Belousov bio->bio_flags |= BIO_UNMAPPED; 26582cc718a1SKonstantin Belousov } else { 26592cc718a1SKonstantin Belousov bio->bio_data = bp->b_data; 26602cc718a1SKonstantin Belousov bio->bio_ma = NULL; 26612cc718a1SKonstantin Belousov } 2662dee34ca4SPoul-Henning Kamp g_io_request(bio, cp); 2663dee34ca4SPoul-Henning Kamp return; 2664dee34ca4SPoul-Henning Kamp } 2665dee34ca4SPoul-Henning Kamp 2666dee34ca4SPoul-Henning Kamp static void 2667dee34ca4SPoul-Henning Kamp swapgeom_orphan(struct g_consumer *cp) 2668dee34ca4SPoul-Henning Kamp { 2669dee34ca4SPoul-Henning Kamp struct swdevt *sp; 26703398491bSAlexander Motin int destroy; 2671dee34ca4SPoul-Henning Kamp 2672dee34ca4SPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 26733398491bSAlexander Motin TAILQ_FOREACH(sp, &swtailq, sw_list) { 26743398491bSAlexander Motin if (sp->sw_id == cp) { 26758f12d83aSAlexander Motin sp->sw_flags |= SW_CLOSING; 26763398491bSAlexander Motin break; 2677dee34ca4SPoul-Henning Kamp } 26783398491bSAlexander Motin } 26799e3e3fe5SWarner Losh /* 26809e3e3fe5SWarner Losh * Drop reference we were created with. Do directly since we're in a 26819e3e3fe5SWarner Losh * special context where we don't have to queue the call to 26829e3e3fe5SWarner Losh * swapgeom_close_ev(). 26839e3e3fe5SWarner Losh */ 26849e3e3fe5SWarner Losh cp->index--; 26853398491bSAlexander Motin destroy = ((sp != NULL) && (cp->index == 0)); 26863398491bSAlexander Motin if (destroy) 26873398491bSAlexander Motin sp->sw_id = NULL; 26883398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 26893398491bSAlexander Motin if (destroy) 26903398491bSAlexander Motin swapgeom_close_ev(cp, 0); 2691dee34ca4SPoul-Henning Kamp } 2692dee34ca4SPoul-Henning Kamp 2693dee34ca4SPoul-Henning Kamp static void 2694dee34ca4SPoul-Henning Kamp swapgeom_close(struct thread *td, struct swdevt *sw) 2695dee34ca4SPoul-Henning Kamp { 26963398491bSAlexander Motin struct g_consumer *cp; 2697dee34ca4SPoul-Henning Kamp 26983398491bSAlexander Motin mtx_lock(&sw_dev_mtx); 26993398491bSAlexander Motin cp = sw->sw_id; 27003398491bSAlexander Motin sw->sw_id = NULL; 27013398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 27020c657d22SKonstantin Belousov 27030c657d22SKonstantin Belousov /* 27040c657d22SKonstantin Belousov * swapgeom_close() may be called from the biodone context, 27050c657d22SKonstantin Belousov * where we cannot perform topology changes. Delegate the 27060c657d22SKonstantin Belousov * work to the events thread. 27070c657d22SKonstantin Belousov */ 27083398491bSAlexander Motin if (cp != NULL) 27093398491bSAlexander Motin g_waitfor_event(swapgeom_close_ev, cp, M_WAITOK, NULL); 2710dee34ca4SPoul-Henning Kamp } 2711dee34ca4SPoul-Henning Kamp 271288ad2d7bSKonstantin Belousov static int 271388ad2d7bSKonstantin Belousov swapongeom_locked(struct cdev *dev, struct vnode *vp) 2714dee34ca4SPoul-Henning Kamp { 2715dee34ca4SPoul-Henning Kamp struct g_provider *pp; 2716dee34ca4SPoul-Henning Kamp struct g_consumer *cp; 2717dee34ca4SPoul-Henning Kamp static struct g_geom *gp; 2718dee34ca4SPoul-Henning Kamp struct swdevt *sp; 2719dee34ca4SPoul-Henning Kamp u_long nblks; 2720dee34ca4SPoul-Henning Kamp int error; 2721dee34ca4SPoul-Henning Kamp 272288ad2d7bSKonstantin Belousov pp = g_dev_getprovider(dev); 272388ad2d7bSKonstantin Belousov if (pp == NULL) 272488ad2d7bSKonstantin Belousov return (ENODEV); 2725dee34ca4SPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 2726dee34ca4SPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 2727dee34ca4SPoul-Henning Kamp cp = sp->sw_id; 2728dee34ca4SPoul-Henning Kamp if (cp != NULL && cp->provider == pp) { 2729dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 273088ad2d7bSKonstantin Belousov return (EBUSY); 2731dee34ca4SPoul-Henning Kamp } 2732dee34ca4SPoul-Henning Kamp } 2733dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 27345721c9c7SPoul-Henning Kamp if (gp == NULL) 273502c62349SJaakko Heinonen gp = g_new_geomf(&g_swap_class, "swap"); 2736dee34ca4SPoul-Henning Kamp cp = g_new_consumer(gp); 27379e3e3fe5SWarner Losh cp->index = 1; /* Number of active I/Os, plus one for being active. */ 27389e3e3fe5SWarner Losh cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 2739dee34ca4SPoul-Henning Kamp g_attach(cp, pp); 2740afeb65e6SPoul-Henning Kamp /* 2741afeb65e6SPoul-Henning Kamp * XXX: Every time you think you can improve the margin for 2742afeb65e6SPoul-Henning Kamp * footshooting, somebody depends on the ability to do so: 2743afeb65e6SPoul-Henning Kamp * savecore(8) wants to write to our swapdev so we cannot 2744afeb65e6SPoul-Henning Kamp * set an exclusive count :-( 2745afeb65e6SPoul-Henning Kamp */ 2746d2bae332SPoul-Henning Kamp error = g_access(cp, 1, 1, 0); 274788ad2d7bSKonstantin Belousov if (error != 0) { 2748dee34ca4SPoul-Henning Kamp g_detach(cp); 2749dee34ca4SPoul-Henning Kamp g_destroy_consumer(cp); 275088ad2d7bSKonstantin Belousov return (error); 2751dee34ca4SPoul-Henning Kamp } 2752dee34ca4SPoul-Henning Kamp nblks = pp->mediasize / DEV_BSIZE; 275388ad2d7bSKonstantin Belousov swaponsomething(vp, cp, nblks, swapgeom_strategy, 275488ad2d7bSKonstantin Belousov swapgeom_close, dev2udev(dev), 27552cc718a1SKonstantin Belousov (pp->flags & G_PF_ACCEPT_UNMAPPED) != 0 ? SW_UNMAPPED : 0); 275688ad2d7bSKonstantin Belousov return (0); 2757dee34ca4SPoul-Henning Kamp } 2758dee34ca4SPoul-Henning Kamp 2759dee34ca4SPoul-Henning Kamp static int 276088ad2d7bSKonstantin Belousov swapongeom(struct vnode *vp) 2761dee34ca4SPoul-Henning Kamp { 2762dee34ca4SPoul-Henning Kamp int error; 2763dee34ca4SPoul-Henning Kamp 2764cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 276588ad2d7bSKonstantin Belousov if (vp->v_type != VCHR || (vp->v_iflag & VI_DOOMED) != 0) { 276688ad2d7bSKonstantin Belousov error = ENOENT; 276788ad2d7bSKonstantin Belousov } else { 276888ad2d7bSKonstantin Belousov g_topology_lock(); 276988ad2d7bSKonstantin Belousov error = swapongeom_locked(vp->v_rdev, vp); 277088ad2d7bSKonstantin Belousov g_topology_unlock(); 277188ad2d7bSKonstantin Belousov } 277222db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 2773dee34ca4SPoul-Henning Kamp return (error); 2774dee34ca4SPoul-Henning Kamp } 2775dee34ca4SPoul-Henning Kamp 2776dee34ca4SPoul-Henning Kamp /* 2777dee34ca4SPoul-Henning Kamp * VNODE backend 2778dee34ca4SPoul-Henning Kamp * 2779dee34ca4SPoul-Henning Kamp * This is used mainly for network filesystem (read: probably only tested 2780dee34ca4SPoul-Henning Kamp * with NFS) swapfiles. 2781dee34ca4SPoul-Henning Kamp * 2782dee34ca4SPoul-Henning Kamp */ 2783dee34ca4SPoul-Henning Kamp 2784dee34ca4SPoul-Henning Kamp static void 2785dee34ca4SPoul-Henning Kamp swapdev_strategy(struct buf *bp, struct swdevt *sp) 2786dee34ca4SPoul-Henning Kamp { 2787494eb176SPoul-Henning Kamp struct vnode *vp2; 2788dee34ca4SPoul-Henning Kamp 2789dee34ca4SPoul-Henning Kamp bp->b_blkno = ctodb(bp->b_blkno - sp->sw_first); 2790dee34ca4SPoul-Henning Kamp 2791dee34ca4SPoul-Henning Kamp vp2 = sp->sw_id; 2792dee34ca4SPoul-Henning Kamp vhold(vp2); 2793dee34ca4SPoul-Henning Kamp if (bp->b_iocmd == BIO_WRITE) { 27943cfc7651SOlivier Houchard if (bp->b_bufobj) 2795494eb176SPoul-Henning Kamp bufobj_wdrop(bp->b_bufobj); 2796a76d8f4eSPoul-Henning Kamp bufobj_wref(&vp2->v_bufobj); 2797dee34ca4SPoul-Henning Kamp } 27983cfc7651SOlivier Houchard if (bp->b_bufobj != &vp2->v_bufobj) 27993cfc7651SOlivier Houchard bp->b_bufobj = &vp2->v_bufobj; 2800dee34ca4SPoul-Henning Kamp bp->b_vp = vp2; 28012c18019fSPoul-Henning Kamp bp->b_iooffset = dbtob(bp->b_blkno); 2802b792bebeSPoul-Henning Kamp bstrategy(bp); 2803dee34ca4SPoul-Henning Kamp return; 2804dee34ca4SPoul-Henning Kamp } 2805dee34ca4SPoul-Henning Kamp 2806dee34ca4SPoul-Henning Kamp static void 2807dee34ca4SPoul-Henning Kamp swapdev_close(struct thread *td, struct swdevt *sp) 2808dee34ca4SPoul-Henning Kamp { 2809dee34ca4SPoul-Henning Kamp 2810dee34ca4SPoul-Henning Kamp VOP_CLOSE(sp->sw_vp, FREAD | FWRITE, td->td_ucred, td); 2811dee34ca4SPoul-Henning Kamp vrele(sp->sw_vp); 2812dee34ca4SPoul-Henning Kamp } 2813dee34ca4SPoul-Henning Kamp 2814dee34ca4SPoul-Henning Kamp 2815dee34ca4SPoul-Henning Kamp static int 2816dee34ca4SPoul-Henning Kamp swaponvp(struct thread *td, struct vnode *vp, u_long nblks) 2817dee34ca4SPoul-Henning Kamp { 2818dee34ca4SPoul-Henning Kamp struct swdevt *sp; 2819dee34ca4SPoul-Henning Kamp int error; 2820dee34ca4SPoul-Henning Kamp 2821dee34ca4SPoul-Henning Kamp if (nblks == 0) 2822dee34ca4SPoul-Henning Kamp return (ENXIO); 2823dee34ca4SPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 2824dee34ca4SPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 2825dee34ca4SPoul-Henning Kamp if (sp->sw_id == vp) { 2826dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 2827dee34ca4SPoul-Henning Kamp return (EBUSY); 2828dee34ca4SPoul-Henning Kamp } 2829dee34ca4SPoul-Henning Kamp } 2830dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 2831dee34ca4SPoul-Henning Kamp 2832cb05b60aSAttilio Rao (void) vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2833dee34ca4SPoul-Henning Kamp #ifdef MAC 283430d239bcSRobert Watson error = mac_system_check_swapon(td->td_ucred, vp); 2835dee34ca4SPoul-Henning Kamp if (error == 0) 2836dee34ca4SPoul-Henning Kamp #endif 28379e223287SKonstantin Belousov error = VOP_OPEN(vp, FREAD | FWRITE, td->td_ucred, td, NULL); 283822db15c0SAttilio Rao (void) VOP_UNLOCK(vp, 0); 2839dee34ca4SPoul-Henning Kamp if (error) 2840dee34ca4SPoul-Henning Kamp return (error); 2841dee34ca4SPoul-Henning Kamp 2842dee34ca4SPoul-Henning Kamp swaponsomething(vp, vp, nblks, swapdev_strategy, swapdev_close, 28432cc718a1SKonstantin Belousov NODEV, 0); 2844dee34ca4SPoul-Henning Kamp return (0); 2845dee34ca4SPoul-Henning Kamp } 284689c241d1SGleb Smirnoff 284789c241d1SGleb Smirnoff static int 284889c241d1SGleb Smirnoff sysctl_swap_async_max(SYSCTL_HANDLER_ARGS) 284989c241d1SGleb Smirnoff { 285089c241d1SGleb Smirnoff int error, new, n; 285189c241d1SGleb Smirnoff 285289c241d1SGleb Smirnoff new = nsw_wcount_async_max; 285389c241d1SGleb Smirnoff error = sysctl_handle_int(oidp, &new, 0, req); 285489c241d1SGleb Smirnoff if (error != 0 || req->newptr == NULL) 285589c241d1SGleb Smirnoff return (error); 285689c241d1SGleb Smirnoff 285789c241d1SGleb Smirnoff if (new > nswbuf / 2 || new < 1) 285889c241d1SGleb Smirnoff return (EINVAL); 285989c241d1SGleb Smirnoff 286089c241d1SGleb Smirnoff mtx_lock(&pbuf_mtx); 286189c241d1SGleb Smirnoff while (nsw_wcount_async_max != new) { 286289c241d1SGleb Smirnoff /* 286389c241d1SGleb Smirnoff * Adjust difference. If the current async count is too low, 286489c241d1SGleb Smirnoff * we will need to sqeeze our update slowly in. Sleep with a 286589c241d1SGleb Smirnoff * higher priority than getpbuf() to finish faster. 286689c241d1SGleb Smirnoff */ 286789c241d1SGleb Smirnoff n = new - nsw_wcount_async_max; 286889c241d1SGleb Smirnoff if (nsw_wcount_async + n >= 0) { 286989c241d1SGleb Smirnoff nsw_wcount_async += n; 287089c241d1SGleb Smirnoff nsw_wcount_async_max += n; 287189c241d1SGleb Smirnoff wakeup(&nsw_wcount_async); 287289c241d1SGleb Smirnoff } else { 287389c241d1SGleb Smirnoff nsw_wcount_async_max -= nsw_wcount_async; 287489c241d1SGleb Smirnoff nsw_wcount_async = 0; 287589c241d1SGleb Smirnoff msleep(&nsw_wcount_async, &pbuf_mtx, PSWP, 287689c241d1SGleb Smirnoff "swpsysctl", 0); 287789c241d1SGleb Smirnoff } 287889c241d1SGleb Smirnoff } 287989c241d1SGleb Smirnoff mtx_unlock(&pbuf_mtx); 288089c241d1SGleb Smirnoff 288189c241d1SGleb Smirnoff return (0); 288289c241d1SGleb Smirnoff } 2883