160727d8bSWarner Losh /*- 2df57947fSPedro F. Giffuni * SPDX-License-Identifier: BSD-4-Clause 3df57947fSPedro F. Giffuni * 41c7c3c6aSMatthew Dillon * Copyright (c) 1998 Matthew Dillon, 526f9a767SRodney W. Grimes * Copyright (c) 1994 John S. Dyson 6df8bae1dSRodney W. Grimes * Copyright (c) 1990 University of Utah. 7e9c0cc15SPoul-Henning Kamp * Copyright (c) 1982, 1986, 1989, 1993 8df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 11df8bae1dSRodney W. Grimes * the Systems Programming Group of the University of Utah Computer 12df8bae1dSRodney W. Grimes * Science Department. 13df8bae1dSRodney W. Grimes * 14df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 15df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 16df8bae1dSRodney W. Grimes * are met: 17df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 18df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 19df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 20df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 21df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 22df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 235929bcfaSPhilippe Charnier * must display the following acknowledgement: 24df8bae1dSRodney W. Grimes * This product includes software developed by the University of 25df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 26df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 27df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 28df8bae1dSRodney W. Grimes * without specific prior written permission. 29df8bae1dSRodney W. Grimes * 30df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 31df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 34df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 35df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 36df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 37df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 38df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 39df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 40df8bae1dSRodney W. Grimes * SUCH DAMAGE. 41df8bae1dSRodney W. Grimes * 421c7c3c6aSMatthew Dillon * New Swap System 431c7c3c6aSMatthew Dillon * Matthew Dillon 441c7c3c6aSMatthew Dillon * 451c7c3c6aSMatthew Dillon * Radix Bitmap 'blists'. 461c7c3c6aSMatthew Dillon * 471c7c3c6aSMatthew Dillon * - The new swapper uses the new radix bitmap code. This should scale 481c7c3c6aSMatthew Dillon * to arbitrarily small or arbitrarily large swap spaces and an almost 491c7c3c6aSMatthew Dillon * arbitrary degree of fragmentation. 501c7c3c6aSMatthew Dillon * 511c7c3c6aSMatthew Dillon * Features: 521c7c3c6aSMatthew Dillon * 531c7c3c6aSMatthew Dillon * - on the fly reallocation of swap during putpages. The new system 541c7c3c6aSMatthew Dillon * does not try to keep previously allocated swap blocks for dirty 551c7c3c6aSMatthew Dillon * pages. 561c7c3c6aSMatthew Dillon * 571c7c3c6aSMatthew Dillon * - on the fly deallocation of swap 581c7c3c6aSMatthew Dillon * 591c7c3c6aSMatthew Dillon * - No more garbage collection required. Unnecessarily allocated swap 601c7c3c6aSMatthew Dillon * blocks only exist for dirty vm_page_t's now and these are already 611c7c3c6aSMatthew Dillon * cycled (in a high-load system) by the pager. We also do on-the-fly 621c7c3c6aSMatthew Dillon * removal of invalidated swap blocks when a page is destroyed 631c7c3c6aSMatthew Dillon * or renamed. 641c7c3c6aSMatthew Dillon * 65df8bae1dSRodney W. Grimes * from: Utah $Hdr: swap_pager.c 1.4 91/04/30$ 66df8bae1dSRodney W. Grimes * 67df8bae1dSRodney W. Grimes * @(#)swap_pager.c 8.9 (Berkeley) 3/21/94 68e9c0cc15SPoul-Henning Kamp * @(#)vm_swap.c 8.5 (Berkeley) 2/17/94 69df8bae1dSRodney W. Grimes */ 70df8bae1dSRodney W. Grimes 71874651b1SDavid E. O'Brien #include <sys/cdefs.h> 72874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 73874651b1SDavid E. O'Brien 74e9c0cc15SPoul-Henning Kamp #include "opt_vm.h" 75e9c0cc15SPoul-Henning Kamp 76df8bae1dSRodney W. Grimes #include <sys/param.h> 77df8bae1dSRodney W. Grimes #include <sys/systm.h> 78af647ddeSBruce Evans #include <sys/conf.h> 7964abb5a5SDavid Greenman #include <sys/kernel.h> 80acd3428bSRobert Watson #include <sys/priv.h> 81df8bae1dSRodney W. Grimes #include <sys/proc.h> 829626b608SPoul-Henning Kamp #include <sys/bio.h> 83df8bae1dSRodney W. Grimes #include <sys/buf.h> 84e9c0cc15SPoul-Henning Kamp #include <sys/disk.h> 85e9c0cc15SPoul-Henning Kamp #include <sys/fcntl.h> 86e9c0cc15SPoul-Henning Kamp #include <sys/mount.h> 87e9c0cc15SPoul-Henning Kamp #include <sys/namei.h> 88df8bae1dSRodney W. Grimes #include <sys/vnode.h> 89df8bae1dSRodney W. Grimes #include <sys/malloc.h> 90f425ab8eSKonstantin Belousov #include <sys/pctrie.h> 911ba5ad42SEdward Tomasz Napierala #include <sys/racct.h> 923364c323SKonstantin Belousov #include <sys/resource.h> 933364c323SKonstantin Belousov #include <sys/resourcevar.h> 9489f6b863SAttilio Rao #include <sys/rwlock.h> 95d027ed2eSAlan Cox #include <sys/sbuf.h> 96327f4e83SMatthew Dillon #include <sys/sysctl.h> 97e9c0cc15SPoul-Henning Kamp #include <sys/sysproto.h> 981c7c3c6aSMatthew Dillon #include <sys/blist.h> 991c7c3c6aSMatthew Dillon #include <sys/lock.h> 1000cddd8f0SMatthew Dillon #include <sys/sx.h> 101936524aaSMatthew Dillon #include <sys/vmmeter.h> 102df8bae1dSRodney W. Grimes 103aed55708SRobert Watson #include <security/mac/mac_framework.h> 104aed55708SRobert Watson 105df8bae1dSRodney W. Grimes #include <vm/vm.h> 10621cd6e62SSeigo Tanimura #include <vm/pmap.h> 10721cd6e62SSeigo Tanimura #include <vm/vm_map.h> 10821cd6e62SSeigo Tanimura #include <vm/vm_kern.h> 109efeaf95aSDavid Greenman #include <vm/vm_object.h> 110df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 111efeaf95aSDavid Greenman #include <vm/vm_pager.h> 112df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h> 113e9c0cc15SPoul-Henning Kamp #include <vm/vm_param.h> 114df8bae1dSRodney W. Grimes #include <vm/swap_pager.h> 115efeaf95aSDavid Greenman #include <vm/vm_extern.h> 116670d17b5SJeff Roberson #include <vm/uma.h> 117df8bae1dSRodney W. Grimes 118dee34ca4SPoul-Henning Kamp #include <geom/geom.h> 119dee34ca4SPoul-Henning Kamp 120ec38b344SPoul-Henning Kamp /* 121064650c1SAlan Cox * MAX_PAGEOUT_CLUSTER must be a power of 2 between 1 and 64. 122064650c1SAlan Cox * The 64-page limit is due to the radix code (kern/subr_blist.c). 123ec38b344SPoul-Henning Kamp */ 124ec38b344SPoul-Henning Kamp #ifndef MAX_PAGEOUT_CLUSTER 125e2241590SAlan Cox #define MAX_PAGEOUT_CLUSTER 32 126ec38b344SPoul-Henning Kamp #endif 127ec38b344SPoul-Henning Kamp 128ec38b344SPoul-Henning Kamp #if !defined(SWB_NPAGES) 129ec38b344SPoul-Henning Kamp #define SWB_NPAGES MAX_PAGEOUT_CLUSTER 130ec38b344SPoul-Henning Kamp #endif 131ec38b344SPoul-Henning Kamp 132f425ab8eSKonstantin Belousov #define SWAP_META_PAGES PCTRIE_COUNT 133ec38b344SPoul-Henning Kamp 134f425ab8eSKonstantin Belousov /* 135f425ab8eSKonstantin Belousov * A swblk structure maps each page index within a 136f425ab8eSKonstantin Belousov * SWAP_META_PAGES-aligned and sized range to the address of an 137f425ab8eSKonstantin Belousov * on-disk swap block (or SWAPBLK_NONE). The collection of these 138f425ab8eSKonstantin Belousov * mappings for an entire vm object is implemented as a pc-trie. 139f425ab8eSKonstantin Belousov */ 140f425ab8eSKonstantin Belousov struct swblk { 141f425ab8eSKonstantin Belousov vm_pindex_t p; 142f425ab8eSKonstantin Belousov daddr_t d[SWAP_META_PAGES]; 143e9c0cc15SPoul-Henning Kamp }; 144e9c0cc15SPoul-Henning Kamp 1452c4992dbSAlan Cox static MALLOC_DEFINE(M_VMPGDATA, "vm_pgdata", "swap pager private data"); 14620da9c2eSPoul-Henning Kamp static struct mtx sw_dev_mtx; 1478f60c087SPoul-Henning Kamp static TAILQ_HEAD(, swdevt) swtailq = TAILQ_HEAD_INITIALIZER(swtailq); 1488f60c087SPoul-Henning Kamp static struct swdevt *swdevhd; /* Allocate from here next */ 1498f60c087SPoul-Henning Kamp static int nswapdev; /* Number of swap devices */ 1508f60c087SPoul-Henning Kamp int swap_pager_avail; 15104533e1eSKonstantin Belousov static struct sx swdev_syscall_lock; /* serialize swap(on|off) */ 152e9c0cc15SPoul-Henning Kamp 153e8bb589dSMatt Macy static u_long swap_reserved; 154e8bb589dSMatt Macy static u_long swap_total; 155e8bb589dSMatt Macy static int sysctl_page_shift(SYSCTL_HANDLER_ARGS); 156e8bb589dSMatt Macy SYSCTL_PROC(_vm, OID_AUTO, swap_reserved, CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE, 157e8bb589dSMatt Macy &swap_reserved, 0, sysctl_page_shift, "A", 1588a9c731fSIvan Voras "Amount of swap storage needed to back all allocated anonymous memory."); 159e8bb589dSMatt Macy SYSCTL_PROC(_vm, OID_AUTO, swap_total, CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE, 160e8bb589dSMatt Macy &swap_total, 0, sysctl_page_shift, "A", 161e8bb589dSMatt Macy "Total amount of available swap storage."); 162e8bb589dSMatt Macy 1633364c323SKonstantin Belousov static int overcommit = 0; 164be7d4ac5SEdward Tomasz Napierala SYSCTL_INT(_vm, VM_OVERCOMMIT, overcommit, CTLFLAG_RW, &overcommit, 0, 1658a9c731fSIvan Voras "Configure virtual memory overcommit behavior. See tuning(7) " 1668a9c731fSIvan Voras "for details."); 16761203277SDag-Erling Smørgrav static unsigned long swzone; 16861203277SDag-Erling Smørgrav SYSCTL_ULONG(_vm, OID_AUTO, swzone, CTLFLAG_RD, &swzone, 0, 16961203277SDag-Erling Smørgrav "Actual size of swap metadata zone"); 17061203277SDag-Erling Smørgrav static unsigned long swap_maxpages; 17161203277SDag-Erling Smørgrav SYSCTL_ULONG(_vm, OID_AUTO, swap_maxpages, CTLFLAG_RD, &swap_maxpages, 0, 17261203277SDag-Erling Smørgrav "Maximum amount of swap supported"); 1733364c323SKonstantin Belousov 1743364c323SKonstantin Belousov /* bits from overcommit */ 1753364c323SKonstantin Belousov #define SWAP_RESERVE_FORCE_ON (1 << 0) 1763364c323SKonstantin Belousov #define SWAP_RESERVE_RLIMIT_ON (1 << 1) 1773364c323SKonstantin Belousov #define SWAP_RESERVE_ALLOW_NONWIRED (1 << 2) 1783364c323SKonstantin Belousov 179e8bb589dSMatt Macy static int 180e8bb589dSMatt Macy sysctl_page_shift(SYSCTL_HANDLER_ARGS) 181e8bb589dSMatt Macy { 182e8bb589dSMatt Macy uint64_t newval; 183e8bb589dSMatt Macy u_long value = *(u_long *)arg1; 184e8bb589dSMatt Macy 185e8bb589dSMatt Macy newval = ((uint64_t)value) << PAGE_SHIFT; 186e8bb589dSMatt Macy return (sysctl_handle_64(oidp, &newval, 0, req)); 187e8bb589dSMatt Macy } 188e8bb589dSMatt Macy 1893364c323SKonstantin Belousov int 1903364c323SKonstantin Belousov swap_reserve(vm_ooffset_t incr) 1913364c323SKonstantin Belousov { 1923364c323SKonstantin Belousov 193ef694c1aSEdward Tomasz Napierala return (swap_reserve_by_cred(incr, curthread->td_ucred)); 1943364c323SKonstantin Belousov } 1953364c323SKonstantin Belousov 1963364c323SKonstantin Belousov int 197ef694c1aSEdward Tomasz Napierala swap_reserve_by_cred(vm_ooffset_t incr, struct ucred *cred) 1983364c323SKonstantin Belousov { 199e8bb589dSMatt Macy u_long r, s, prev, pincr; 2003364c323SKonstantin Belousov int res, error; 2013364c323SKonstantin Belousov static int curfail; 2023364c323SKonstantin Belousov static struct timeval lastfail; 203ef694c1aSEdward Tomasz Napierala struct uidinfo *uip; 204ef694c1aSEdward Tomasz Napierala 205ef694c1aSEdward Tomasz Napierala uip = cred->cr_ruidinfo; 2063364c323SKonstantin Belousov 207e8bb589dSMatt Macy KASSERT((incr & PAGE_MASK) == 0, ("%s: incr: %ju & PAGE_MASK", __func__, 208e8bb589dSMatt Macy (uintmax_t)incr)); 2093364c323SKonstantin Belousov 210afcc55f3SEdward Tomasz Napierala #ifdef RACCT 2114b5c9cf6SEdward Tomasz Napierala if (racct_enable) { 2121ba5ad42SEdward Tomasz Napierala PROC_LOCK(curproc); 2131ba5ad42SEdward Tomasz Napierala error = racct_add(curproc, RACCT_SWAP, incr); 2141ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(curproc); 2151ba5ad42SEdward Tomasz Napierala if (error != 0) 2161ba5ad42SEdward Tomasz Napierala return (0); 2174b5c9cf6SEdward Tomasz Napierala } 218afcc55f3SEdward Tomasz Napierala #endif 2191ba5ad42SEdward Tomasz Napierala 220e8bb589dSMatt Macy pincr = atop(incr); 2213364c323SKonstantin Belousov res = 0; 222e8bb589dSMatt Macy prev = atomic_fetchadd_long(&swap_reserved, pincr); 223e8bb589dSMatt Macy r = prev + pincr; 2243364c323SKonstantin Belousov if (overcommit & SWAP_RESERVE_ALLOW_NONWIRED) { 225e958ad4cSJeff Roberson s = vm_cnt.v_page_count - vm_cnt.v_free_reserved - 226e958ad4cSJeff Roberson vm_wire_count(); 2273364c323SKonstantin Belousov } else 2283364c323SKonstantin Belousov s = 0; 2293364c323SKonstantin Belousov s += swap_total; 2303364c323SKonstantin Belousov if ((overcommit & SWAP_RESERVE_FORCE_ON) == 0 || r <= s || 2313364c323SKonstantin Belousov (error = priv_check(curthread, PRIV_VM_SWAP_NOQUOTA)) == 0) { 2323364c323SKonstantin Belousov res = 1; 233e8bb589dSMatt Macy } else { 234e8bb589dSMatt Macy prev = atomic_fetchadd_long(&swap_reserved, -pincr); 235e8bb589dSMatt Macy if (prev < pincr) 236e8bb589dSMatt Macy panic("swap_reserved < incr on overcommit fail"); 2373364c323SKonstantin Belousov } 2383364c323SKonstantin Belousov if (res) { 239e8bb589dSMatt Macy prev = atomic_fetchadd_long(&uip->ui_vmsize, pincr); 2405c0e1c11SKonstantin Belousov if ((overcommit & SWAP_RESERVE_RLIMIT_ON) != 0 && 241e8bb589dSMatt Macy prev + pincr > lim_cur(curthread, RLIMIT_SWAP) && 242e8bb589dSMatt Macy priv_check(curthread, PRIV_VM_SWAP_NORLIMIT)) { 2433364c323SKonstantin Belousov res = 0; 244e8bb589dSMatt Macy prev = atomic_fetchadd_long(&uip->ui_vmsize, -pincr); 245e8bb589dSMatt Macy if (prev < pincr) 246e8bb589dSMatt Macy panic("uip->ui_vmsize < incr on overcommit fail"); 2473364c323SKonstantin Belousov } 2483364c323SKonstantin Belousov } 2493364c323SKonstantin Belousov if (!res && ppsratecheck(&lastfail, &curfail, 1)) { 2503364c323SKonstantin Belousov printf("uid %d, pid %d: swap reservation for %jd bytes failed\n", 251134465d7SKonstantin Belousov uip->ui_uid, curproc->p_pid, incr); 2523364c323SKonstantin Belousov } 2533364c323SKonstantin Belousov 254afcc55f3SEdward Tomasz Napierala #ifdef RACCT 255e8bb589dSMatt Macy if (racct_enable && !res) { 2561ba5ad42SEdward Tomasz Napierala PROC_LOCK(curproc); 2571ba5ad42SEdward Tomasz Napierala racct_sub(curproc, RACCT_SWAP, incr); 2581ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(curproc); 2591ba5ad42SEdward Tomasz Napierala } 260afcc55f3SEdward Tomasz Napierala #endif 2611ba5ad42SEdward Tomasz Napierala 2623364c323SKonstantin Belousov return (res); 2633364c323SKonstantin Belousov } 2643364c323SKonstantin Belousov 2653364c323SKonstantin Belousov void 2663364c323SKonstantin Belousov swap_reserve_force(vm_ooffset_t incr) 2673364c323SKonstantin Belousov { 2683364c323SKonstantin Belousov struct uidinfo *uip; 269e8bb589dSMatt Macy u_long pincr; 2703364c323SKonstantin Belousov 271e8bb589dSMatt Macy KASSERT((incr & PAGE_MASK) == 0, ("%s: incr: %ju & PAGE_MASK", __func__, 272e8bb589dSMatt Macy (uintmax_t)incr)); 2733364c323SKonstantin Belousov 274e8bb589dSMatt Macy PROC_LOCK(curproc); 275afcc55f3SEdward Tomasz Napierala #ifdef RACCT 276e8bb589dSMatt Macy if (racct_enable) 2771ba5ad42SEdward Tomasz Napierala racct_add_force(curproc, RACCT_SWAP, incr); 278afcc55f3SEdward Tomasz Napierala #endif 279e8bb589dSMatt Macy pincr = atop(incr); 280e8bb589dSMatt Macy atomic_add_long(&swap_reserved, pincr); 281e8bb589dSMatt Macy uip = curproc->p_ucred->cr_ruidinfo; 282e8bb589dSMatt Macy atomic_add_long(&uip->ui_vmsize, pincr); 2833364c323SKonstantin Belousov PROC_UNLOCK(curproc); 2843364c323SKonstantin Belousov } 2853364c323SKonstantin Belousov 2863364c323SKonstantin Belousov void 2873364c323SKonstantin Belousov swap_release(vm_ooffset_t decr) 2883364c323SKonstantin Belousov { 289ef694c1aSEdward Tomasz Napierala struct ucred *cred; 2903364c323SKonstantin Belousov 2913364c323SKonstantin Belousov PROC_LOCK(curproc); 292e8bb589dSMatt Macy cred = curproc->p_ucred; 293ef694c1aSEdward Tomasz Napierala swap_release_by_cred(decr, cred); 2943364c323SKonstantin Belousov PROC_UNLOCK(curproc); 2953364c323SKonstantin Belousov } 2963364c323SKonstantin Belousov 2973364c323SKonstantin Belousov void 298ef694c1aSEdward Tomasz Napierala swap_release_by_cred(vm_ooffset_t decr, struct ucred *cred) 2993364c323SKonstantin Belousov { 300e8bb589dSMatt Macy u_long prev, pdecr; 301ef694c1aSEdward Tomasz Napierala struct uidinfo *uip; 302ef694c1aSEdward Tomasz Napierala 303ef694c1aSEdward Tomasz Napierala uip = cred->cr_ruidinfo; 3043364c323SKonstantin Belousov 305e8bb589dSMatt Macy KASSERT((decr & PAGE_MASK) == 0, ("%s: decr: %ju & PAGE_MASK", __func__, 306e8bb589dSMatt Macy (uintmax_t)decr)); 3073364c323SKonstantin Belousov 308e8bb589dSMatt Macy pdecr = atop(decr); 309e8bb589dSMatt Macy prev = atomic_fetchadd_long(&swap_reserved, -pdecr); 310e8bb589dSMatt Macy if (prev < pdecr) 3113364c323SKonstantin Belousov panic("swap_reserved < decr"); 3123364c323SKonstantin Belousov 313e8bb589dSMatt Macy prev = atomic_fetchadd_long(&uip->ui_vmsize, -pdecr); 314e8bb589dSMatt Macy if (prev < pdecr) 3153364c323SKonstantin Belousov printf("negative vmsize for uid = %d\n", uip->ui_uid); 316e8bb589dSMatt Macy #ifdef RACCT 317e8bb589dSMatt Macy if (racct_enable) 3181ba5ad42SEdward Tomasz Napierala racct_sub_cred(cred, RACCT_SWAP, decr); 319e8bb589dSMatt Macy #endif 3203364c323SKonstantin Belousov } 3213364c323SKonstantin Belousov 3224abca9bbSAlan Cox #define SWM_POP 0x01 /* pop out */ 32326f9a767SRodney W. Grimes 324f08b3099SKonstantin Belousov static int swap_pager_full = 2; /* swap space exhaustion (task killing) */ 3257dea2c2eSAlan Cox static int swap_pager_almost_full = 1; /* swap space exhaustion (w/hysteresis)*/ 326756a5412SGleb Smirnoff static struct mtx swbuf_mtx; /* to sync nsw_wcount_async */ 327756a5412SGleb Smirnoff static int nsw_wcount_async; /* limit async write buffers */ 328327f4e83SMatthew Dillon static int nsw_wcount_async_max;/* assigned maximum */ 329327f4e83SMatthew Dillon static int nsw_cluster_max; /* maximum VOP I/O allowed */ 3301c7c3c6aSMatthew Dillon 33189c241d1SGleb Smirnoff static int sysctl_swap_async_max(SYSCTL_HANDLER_ARGS); 3324c36e917SKonstantin Belousov SYSCTL_PROC(_vm, OID_AUTO, swap_async_max, CTLTYPE_INT | CTLFLAG_RW | 3334c36e917SKonstantin Belousov CTLFLAG_MPSAFE, NULL, 0, sysctl_swap_async_max, "I", 3344c36e917SKonstantin Belousov "Maximum running async swap ops"); 335d027ed2eSAlan Cox static int sysctl_swap_fragmentation(SYSCTL_HANDLER_ARGS); 336d027ed2eSAlan Cox SYSCTL_PROC(_vm, OID_AUTO, swap_fragmentation, CTLTYPE_STRING | CTLFLAG_RD | 337d027ed2eSAlan Cox CTLFLAG_MPSAFE, NULL, 0, sysctl_swap_fragmentation, "A", 338d027ed2eSAlan Cox "Swap Fragmentation Info"); 33989c241d1SGleb Smirnoff 3400cddd8f0SMatthew Dillon static struct sx sw_alloc_sx; 341327f4e83SMatthew Dillon 3421c7c3c6aSMatthew Dillon /* 3431c7c3c6aSMatthew Dillon * "named" and "unnamed" anon region objects. Try to reduce the overhead 3441c7c3c6aSMatthew Dillon * of searching a named list by hashing it just a little. 3451c7c3c6aSMatthew Dillon */ 3461c7c3c6aSMatthew Dillon 3471c7c3c6aSMatthew Dillon #define NOBJLISTS 8 3481c7c3c6aSMatthew Dillon 3491c7c3c6aSMatthew Dillon #define NOBJLIST(handle) \ 350af647ddeSBruce Evans (&swap_pager_object_list[((int)(intptr_t)handle >> 4) & (NOBJLISTS-1)]) 3511c7c3c6aSMatthew Dillon 3521c7c3c6aSMatthew Dillon static struct pagerlst swap_pager_object_list[NOBJLISTS]; 353756a5412SGleb Smirnoff static uma_zone_t swwbuf_zone; 354756a5412SGleb Smirnoff static uma_zone_t swrbuf_zone; 355f425ab8eSKonstantin Belousov static uma_zone_t swblk_zone; 356f425ab8eSKonstantin Belousov static uma_zone_t swpctrie_zone; 3571c7c3c6aSMatthew Dillon 3581c7c3c6aSMatthew Dillon /* 3591c7c3c6aSMatthew Dillon * pagerops for OBJT_SWAP - "swap pager". Some ops are also global procedure 3601c7c3c6aSMatthew Dillon * calls hooked from other parts of the VM system and do not appear here. 3611c7c3c6aSMatthew Dillon * (see vm/swap_pager.h). 3621c7c3c6aSMatthew Dillon */ 363ff98689dSBruce Evans static vm_object_t 36411caded3SAlfred Perlstein swap_pager_alloc(void *handle, vm_ooffset_t size, 3653364c323SKonstantin Belousov vm_prot_t prot, vm_ooffset_t offset, struct ucred *); 36611caded3SAlfred Perlstein static void swap_pager_dealloc(vm_object_t object); 367b0cd2017SGleb Smirnoff static int swap_pager_getpages(vm_object_t, vm_page_t *, int, int *, 368b0cd2017SGleb Smirnoff int *); 369b0cd2017SGleb Smirnoff static int swap_pager_getpages_async(vm_object_t, vm_page_t *, int, int *, 370b0cd2017SGleb Smirnoff int *, pgo_getpages_iodone_t, void *); 371751221fdSPoul-Henning Kamp static void swap_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *); 3725ea4972cSAlan Cox static boolean_t 3735ea4972cSAlan Cox swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, int *after); 37411caded3SAlfred Perlstein static void swap_pager_init(void); 37511caded3SAlfred Perlstein static void swap_pager_unswapped(vm_page_t); 376b3fed13eSDavid Schultz static void swap_pager_swapoff(struct swdevt *sp); 377f708ef1bSPoul-Henning Kamp 378df8bae1dSRodney W. Grimes struct pagerops swappagerops = { 379e04e4bacSPoul-Henning Kamp .pgo_init = swap_pager_init, /* early system initialization of pager */ 380e04e4bacSPoul-Henning Kamp .pgo_alloc = swap_pager_alloc, /* allocate an OBJT_SWAP object */ 381e04e4bacSPoul-Henning Kamp .pgo_dealloc = swap_pager_dealloc, /* deallocate an OBJT_SWAP object */ 382e04e4bacSPoul-Henning Kamp .pgo_getpages = swap_pager_getpages, /* pagein */ 38390effb23SGleb Smirnoff .pgo_getpages_async = swap_pager_getpages_async, /* pagein (async) */ 384e04e4bacSPoul-Henning Kamp .pgo_putpages = swap_pager_putpages, /* pageout */ 385e04e4bacSPoul-Henning Kamp .pgo_haspage = swap_pager_haspage, /* get backing store status for page */ 386e04e4bacSPoul-Henning Kamp .pgo_pageunswapped = swap_pager_unswapped, /* remove swap related to page */ 387df8bae1dSRodney W. Grimes }; 388df8bae1dSRodney W. Grimes 3891c7c3c6aSMatthew Dillon /* 3901c7c3c6aSMatthew Dillon * swap_*() routines are externally accessible. swp_*() routines are 3911c7c3c6aSMatthew Dillon * internal. 3921c7c3c6aSMatthew Dillon */ 393e9c0cc15SPoul-Henning Kamp static int nswap_lowat = 128; /* in pages, swap_pager_almost_full warn */ 394e9c0cc15SPoul-Henning Kamp static int nswap_hiwat = 512; /* in pages, swap_pager_almost_full warn */ 39526f9a767SRodney W. Grimes 39607c348eaSAlan Cox SYSCTL_INT(_vm, OID_AUTO, dmmax, CTLFLAG_RD, &nsw_cluster_max, 0, 39707c348eaSAlan Cox "Maximum size of a swap block in pages"); 398cee313c4SRobert Watson 399a5edd34aSPoul-Henning Kamp static void swp_sizecheck(void); 40011caded3SAlfred Perlstein static void swp_pager_async_iodone(struct buf *bp); 401230869e0SAlan Cox static bool swp_pager_swblk_empty(struct swblk *sb, int start, int limit); 40288ad2d7bSKonstantin Belousov static int swapongeom(struct vnode *); 40359efee01SPoul-Henning Kamp static int swaponvp(struct thread *, struct vnode *, u_long); 40435918c55SChristian S.J. Peron static int swapoff_one(struct swdevt *sp, struct ucred *cred); 40524a1cce3SDavid Greenman 4061c7c3c6aSMatthew Dillon /* 4071c7c3c6aSMatthew Dillon * Swap bitmap functions 4081c7c3c6aSMatthew Dillon */ 409230869e0SAlan Cox static void swp_pager_freeswapspace(daddr_t blk, daddr_t npages); 410*48e98a2aSDoug Moore static daddr_t swp_pager_getswapspace(int *npages, int limit); 4111c7c3c6aSMatthew Dillon 4121c7c3c6aSMatthew Dillon /* 4131c7c3c6aSMatthew Dillon * Metadata functions 4141c7c3c6aSMatthew Dillon */ 41578f1deefSAlan Cox static daddr_t swp_pager_meta_build(vm_object_t, vm_pindex_t, daddr_t); 4162e56b64fSKonstantin Belousov static void swp_pager_meta_free(vm_object_t, vm_pindex_t, vm_pindex_t); 41711caded3SAlfred Perlstein static void swp_pager_meta_free_all(vm_object_t); 41811caded3SAlfred Perlstein static daddr_t swp_pager_meta_ctl(vm_object_t, vm_pindex_t, int); 4191c7c3c6aSMatthew Dillon 42078f1deefSAlan Cox static void 42178f1deefSAlan Cox swp_pager_init_freerange(daddr_t *start, daddr_t *num) 42278f1deefSAlan Cox { 42378f1deefSAlan Cox 42478f1deefSAlan Cox *start = SWAPBLK_NONE; 42578f1deefSAlan Cox *num = 0; 42678f1deefSAlan Cox } 42778f1deefSAlan Cox 42878f1deefSAlan Cox static void 42978f1deefSAlan Cox swp_pager_update_freerange(daddr_t *start, daddr_t *num, daddr_t addr) 43078f1deefSAlan Cox { 43178f1deefSAlan Cox 43278f1deefSAlan Cox if (*start + *num == addr) { 43378f1deefSAlan Cox (*num)++; 43478f1deefSAlan Cox } else { 43578f1deefSAlan Cox swp_pager_freeswapspace(*start, *num); 43678f1deefSAlan Cox *start = addr; 43778f1deefSAlan Cox *num = 1; 43878f1deefSAlan Cox } 43978f1deefSAlan Cox } 44078f1deefSAlan Cox 441f425ab8eSKonstantin Belousov static void * 442f425ab8eSKonstantin Belousov swblk_trie_alloc(struct pctrie *ptree) 443f425ab8eSKonstantin Belousov { 444f425ab8eSKonstantin Belousov 445f425ab8eSKonstantin Belousov return (uma_zalloc(swpctrie_zone, M_NOWAIT | (curproc == pageproc ? 446f425ab8eSKonstantin Belousov M_USE_RESERVE : 0))); 447f425ab8eSKonstantin Belousov } 448f425ab8eSKonstantin Belousov 449f425ab8eSKonstantin Belousov static void 450f425ab8eSKonstantin Belousov swblk_trie_free(struct pctrie *ptree, void *node) 451f425ab8eSKonstantin Belousov { 452f425ab8eSKonstantin Belousov 453f425ab8eSKonstantin Belousov uma_zfree(swpctrie_zone, node); 454f425ab8eSKonstantin Belousov } 455f425ab8eSKonstantin Belousov 456f425ab8eSKonstantin Belousov PCTRIE_DEFINE(SWAP, swblk, p, swblk_trie_alloc, swblk_trie_free); 457f425ab8eSKonstantin Belousov 4581c7c3c6aSMatthew Dillon /* 4591c7c3c6aSMatthew Dillon * SWP_SIZECHECK() - update swap_pager_full indication 4601c7c3c6aSMatthew Dillon * 46120d3034fSMatthew Dillon * update the swap_pager_almost_full indication and warn when we are 46220d3034fSMatthew Dillon * about to run out of swap space, using lowat/hiwat hysteresis. 46320d3034fSMatthew Dillon * 46420d3034fSMatthew Dillon * Clear swap_pager_full ( task killing ) indication when lowat is met. 4651c7c3c6aSMatthew Dillon * 4661c7c3c6aSMatthew Dillon * No restrictions on call 4671c7c3c6aSMatthew Dillon * This routine may not block. 4681c7c3c6aSMatthew Dillon */ 469a5edd34aSPoul-Henning Kamp static void 4702f249180SPoul-Henning Kamp swp_sizecheck(void) 4710d94caffSDavid Greenman { 47223955314SAlfred Perlstein 4738f60c087SPoul-Henning Kamp if (swap_pager_avail < nswap_lowat) { 47420d3034fSMatthew Dillon if (swap_pager_almost_full == 0) { 4751af87c92SDavid Greenman printf("swap_pager: out of swap space\n"); 47620d3034fSMatthew Dillon swap_pager_almost_full = 1; 4772b0d37a4SMatthew Dillon } 47820d3034fSMatthew Dillon } else { 47926f9a767SRodney W. Grimes swap_pager_full = 0; 4808f60c087SPoul-Henning Kamp if (swap_pager_avail > nswap_hiwat) 48120d3034fSMatthew Dillon swap_pager_almost_full = 0; 48226f9a767SRodney W. Grimes } 4831c7c3c6aSMatthew Dillon } 4841c7c3c6aSMatthew Dillon 4851c7c3c6aSMatthew Dillon /* 4861c7c3c6aSMatthew Dillon * SWAP_PAGER_INIT() - initialize the swap pager! 4871c7c3c6aSMatthew Dillon * 4881c7c3c6aSMatthew Dillon * Expected to be started from system init. NOTE: This code is run 4891c7c3c6aSMatthew Dillon * before much else so be careful what you depend on. Most of the VM 4901c7c3c6aSMatthew Dillon * system has yet to be initialized at this point. 4911c7c3c6aSMatthew Dillon */ 492f5a12711SPoul-Henning Kamp static void 4932f249180SPoul-Henning Kamp swap_pager_init(void) 494df8bae1dSRodney W. Grimes { 4951c7c3c6aSMatthew Dillon /* 4961c7c3c6aSMatthew Dillon * Initialize object lists 4971c7c3c6aSMatthew Dillon */ 4981c7c3c6aSMatthew Dillon int i; 4991c7c3c6aSMatthew Dillon 5001c7c3c6aSMatthew Dillon for (i = 0; i < NOBJLISTS; ++i) 5011c7c3c6aSMatthew Dillon TAILQ_INIT(&swap_pager_object_list[i]); 50220da9c2eSPoul-Henning Kamp mtx_init(&sw_dev_mtx, "swapdev", NULL, MTX_DEF); 50315719273SKonstantin Belousov sx_init(&sw_alloc_sx, "swspsx"); 50404533e1eSKonstantin Belousov sx_init(&swdev_syscall_lock, "swsysc"); 5051c7c3c6aSMatthew Dillon } 50626f9a767SRodney W. Grimes 507df8bae1dSRodney W. Grimes /* 5081c7c3c6aSMatthew Dillon * SWAP_PAGER_SWAP_INIT() - swap pager initialization from pageout process 5091c7c3c6aSMatthew Dillon * 5101c7c3c6aSMatthew Dillon * Expected to be started from pageout process once, prior to entering 5111c7c3c6aSMatthew Dillon * its main loop. 512df8bae1dSRodney W. Grimes */ 51324a1cce3SDavid Greenman void 5142f249180SPoul-Henning Kamp swap_pager_swap_init(void) 515df8bae1dSRodney W. Grimes { 51661203277SDag-Erling Smørgrav unsigned long n, n2; 5170d94caffSDavid Greenman 51826f9a767SRodney W. Grimes /* 5191c7c3c6aSMatthew Dillon * Number of in-transit swap bp operations. Don't 5201c7c3c6aSMatthew Dillon * exhaust the pbufs completely. Make sure we 5211c7c3c6aSMatthew Dillon * initialize workable values (0 will work for hysteresis 5221c7c3c6aSMatthew Dillon * but it isn't very efficient). 5231c7c3c6aSMatthew Dillon * 524327f4e83SMatthew Dillon * The nsw_cluster_max is constrained by the bp->b_pages[] 5251c7c3c6aSMatthew Dillon * array (MAXPHYS/PAGE_SIZE) and our locally defined 5261c7c3c6aSMatthew Dillon * MAX_PAGEOUT_CLUSTER. Also be aware that swap ops are 5271c7c3c6aSMatthew Dillon * constrained by the swap device interleave stripe size. 528327f4e83SMatthew Dillon * 529327f4e83SMatthew Dillon * Currently we hardwire nsw_wcount_async to 4. This limit is 530327f4e83SMatthew Dillon * designed to prevent other I/O from having high latencies due to 531327f4e83SMatthew Dillon * our pageout I/O. The value 4 works well for one or two active swap 532327f4e83SMatthew Dillon * devices but is probably a little low if you have more. Even so, 533327f4e83SMatthew Dillon * a higher value would probably generate only a limited improvement 534327f4e83SMatthew Dillon * with three or four active swap devices since the system does not 535327f4e83SMatthew Dillon * typically have to pageout at extreme bandwidths. We will want 536327f4e83SMatthew Dillon * at least 2 per swap devices, and 4 is a pretty good value if you 537327f4e83SMatthew Dillon * have one NFS swap device due to the command/ack latency over NFS. 538327f4e83SMatthew Dillon * So it all works out pretty well. 53926f9a767SRodney W. Grimes */ 540ad3cce20SMatthew Dillon nsw_cluster_max = min((MAXPHYS/PAGE_SIZE), MAX_PAGEOUT_CLUSTER); 541327f4e83SMatthew Dillon 542327f4e83SMatthew Dillon nsw_wcount_async = 4; 543327f4e83SMatthew Dillon nsw_wcount_async_max = nsw_wcount_async; 544756a5412SGleb Smirnoff mtx_init(&swbuf_mtx, "async swbuf mutex", NULL, MTX_DEF); 545756a5412SGleb Smirnoff 546756a5412SGleb Smirnoff swwbuf_zone = pbuf_zsecond_create("swwbuf", nswbuf / 4); 547756a5412SGleb Smirnoff swrbuf_zone = pbuf_zsecond_create("swrbuf", nswbuf / 2); 54824a1cce3SDavid Greenman 5491c7c3c6aSMatthew Dillon /* 550a8233027SKonstantin Belousov * Initialize our zone, taking the user's requested size or 551a8233027SKonstantin Belousov * estimating the number we need based on the number of pages 552a8233027SKonstantin Belousov * in the system. 5531c7c3c6aSMatthew Dillon */ 554a8233027SKonstantin Belousov n = maxswzone != 0 ? maxswzone / sizeof(struct swblk) : 555a8233027SKonstantin Belousov vm_cnt.v_page_count / 2; 556f425ab8eSKonstantin Belousov swpctrie_zone = uma_zcreate("swpctrie", pctrie_node_size(), NULL, NULL, 557f5fbe90dSAlan Cox pctrie_zone_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM); 558f425ab8eSKonstantin Belousov if (swpctrie_zone == NULL) 559f425ab8eSKonstantin Belousov panic("failed to create swap pctrie zone."); 560f425ab8eSKonstantin Belousov swblk_zone = uma_zcreate("swblk", sizeof(struct swblk), NULL, NULL, 561f5fbe90dSAlan Cox NULL, NULL, _Alignof(struct swblk) - 1, UMA_ZONE_VM); 562f425ab8eSKonstantin Belousov if (swblk_zone == NULL) 563f425ab8eSKonstantin Belousov panic("failed to create swap blk zone."); 56422a5e6b9SDag-Erling Smørgrav n2 = n; 5658355f576SJeff Roberson do { 566f425ab8eSKonstantin Belousov if (uma_zone_reserve_kva(swblk_zone, n)) 56761ce6eeeSAlfred Perlstein break; 56861ce6eeeSAlfred Perlstein /* 56961ce6eeeSAlfred Perlstein * if the allocation failed, try a zone two thirds the 57061ce6eeeSAlfred Perlstein * size of the previous attempt. 57161ce6eeeSAlfred Perlstein */ 57261ce6eeeSAlfred Perlstein n -= ((n + 2) / 3); 57361ce6eeeSAlfred Perlstein } while (n > 0); 57453faf5a7SKonstantin Belousov 57553faf5a7SKonstantin Belousov /* 57653faf5a7SKonstantin Belousov * Often uma_zone_reserve_kva() cannot reserve exactly the 57753faf5a7SKonstantin Belousov * requested size. Account for the difference when 57853faf5a7SKonstantin Belousov * calculating swap_maxpages. 57953faf5a7SKonstantin Belousov */ 58053faf5a7SKonstantin Belousov n = uma_zone_get_max(swblk_zone); 58153faf5a7SKonstantin Belousov 5821fffcd75SKonstantin Belousov if (n < n2) 583a8233027SKonstantin Belousov printf("Swap blk zone entries changed from %lu to %lu.\n", 584f425ab8eSKonstantin Belousov n2, n); 58561203277SDag-Erling Smørgrav swap_maxpages = n * SWAP_META_PAGES; 586f425ab8eSKonstantin Belousov swzone = n * sizeof(struct swblk); 587f425ab8eSKonstantin Belousov if (!uma_zone_reserve_kva(swpctrie_zone, n)) 588f425ab8eSKonstantin Belousov printf("Cannot reserve swap pctrie zone, " 589f425ab8eSKonstantin Belousov "reduce kern.maxswzone.\n"); 59024a1cce3SDavid Greenman } 59124a1cce3SDavid Greenman 592eb4d6a1bSKonstantin Belousov static vm_object_t 593eb4d6a1bSKonstantin Belousov swap_pager_alloc_init(void *handle, struct ucred *cred, vm_ooffset_t size, 594eb4d6a1bSKonstantin Belousov vm_ooffset_t offset) 595eb4d6a1bSKonstantin Belousov { 596eb4d6a1bSKonstantin Belousov vm_object_t object; 597eb4d6a1bSKonstantin Belousov 598eb4d6a1bSKonstantin Belousov if (cred != NULL) { 599eb4d6a1bSKonstantin Belousov if (!swap_reserve_by_cred(size, cred)) 600eb4d6a1bSKonstantin Belousov return (NULL); 601eb4d6a1bSKonstantin Belousov crhold(cred); 602eb4d6a1bSKonstantin Belousov } 603f425ab8eSKonstantin Belousov 604f425ab8eSKonstantin Belousov /* 605f425ab8eSKonstantin Belousov * The un_pager.swp.swp_blks trie is initialized by 606f425ab8eSKonstantin Belousov * vm_object_allocate() to ensure the correct order of 607f425ab8eSKonstantin Belousov * visibility to other threads. 608f425ab8eSKonstantin Belousov */ 609eb4d6a1bSKonstantin Belousov object = vm_object_allocate(OBJT_SWAP, OFF_TO_IDX(offset + 610eb4d6a1bSKonstantin Belousov PAGE_MASK + size)); 611f425ab8eSKonstantin Belousov 612eb4d6a1bSKonstantin Belousov object->handle = handle; 613eb4d6a1bSKonstantin Belousov if (cred != NULL) { 614eb4d6a1bSKonstantin Belousov object->cred = cred; 615eb4d6a1bSKonstantin Belousov object->charge = size; 616eb4d6a1bSKonstantin Belousov } 617eb4d6a1bSKonstantin Belousov return (object); 618eb4d6a1bSKonstantin Belousov } 619eb4d6a1bSKonstantin Belousov 62024a1cce3SDavid Greenman /* 6211c7c3c6aSMatthew Dillon * SWAP_PAGER_ALLOC() - allocate a new OBJT_SWAP VM object and instantiate 6221c7c3c6aSMatthew Dillon * its metadata structures. 6231c7c3c6aSMatthew Dillon * 6241c7c3c6aSMatthew Dillon * This routine is called from the mmap and fork code to create a new 625eb4d6a1bSKonstantin Belousov * OBJT_SWAP object. 6261c7c3c6aSMatthew Dillon * 627eb4d6a1bSKonstantin Belousov * This routine must ensure that no live duplicate is created for 628eb4d6a1bSKonstantin Belousov * the named object request, which is protected against by 629eb4d6a1bSKonstantin Belousov * holding the sw_alloc_sx lock in case handle != NULL. 63024a1cce3SDavid Greenman */ 631f5a12711SPoul-Henning Kamp static vm_object_t 6326cde7a16SDavid Greenman swap_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 6333364c323SKonstantin Belousov vm_ooffset_t offset, struct ucred *cred) 63424a1cce3SDavid Greenman { 63524a1cce3SDavid Greenman vm_object_t object; 6362f7af3dbSAlan Cox 637eb4d6a1bSKonstantin Belousov if (handle != NULL) { 6381c7c3c6aSMatthew Dillon /* 6391c7c3c6aSMatthew Dillon * Reference existing named region or allocate new one. There 6401c7c3c6aSMatthew Dillon * should not be a race here against swp_pager_meta_build() 6411c7c3c6aSMatthew Dillon * as called from vm_page_remove() in regards to the lookup 6421c7c3c6aSMatthew Dillon * of the handle. 6431c7c3c6aSMatthew Dillon */ 6440cddd8f0SMatthew Dillon sx_xlock(&sw_alloc_sx); 6451c7c3c6aSMatthew Dillon object = vm_pager_object_lookup(NOBJLIST(handle), handle); 646b5e8f167SAlan Cox if (object == NULL) { 647eb4d6a1bSKonstantin Belousov object = swap_pager_alloc_init(handle, cred, size, 648eb4d6a1bSKonstantin Belousov offset); 649eb4d6a1bSKonstantin Belousov if (object != NULL) { 650eb4d6a1bSKonstantin Belousov TAILQ_INSERT_TAIL(NOBJLIST(object->handle), 651eb4d6a1bSKonstantin Belousov object, pager_object_list); 6523364c323SKonstantin Belousov } 65324a1cce3SDavid Greenman } 6540cddd8f0SMatthew Dillon sx_xunlock(&sw_alloc_sx); 65524a1cce3SDavid Greenman } else { 656eb4d6a1bSKonstantin Belousov object = swap_pager_alloc_init(handle, cred, size, offset); 65724a1cce3SDavid Greenman } 65824a1cce3SDavid Greenman return (object); 659df8bae1dSRodney W. Grimes } 660df8bae1dSRodney W. Grimes 66126f9a767SRodney W. Grimes /* 6621c7c3c6aSMatthew Dillon * SWAP_PAGER_DEALLOC() - remove swap metadata from object 6631c7c3c6aSMatthew Dillon * 6641c7c3c6aSMatthew Dillon * The swap backing for the object is destroyed. The code is 6651c7c3c6aSMatthew Dillon * designed such that we can reinstantiate it later, but this 6661c7c3c6aSMatthew Dillon * routine is typically called only when the entire object is 6671c7c3c6aSMatthew Dillon * about to be destroyed. 6681c7c3c6aSMatthew Dillon * 66915523cf7SKonstantin Belousov * The object must be locked. 67026f9a767SRodney W. Grimes */ 671df8bae1dSRodney W. Grimes static void 6722f249180SPoul-Henning Kamp swap_pager_dealloc(vm_object_t object) 67326f9a767SRodney W. Grimes { 6744dcc5c2dSMatthew Dillon 675eb4d6a1bSKonstantin Belousov VM_OBJECT_ASSERT_WLOCKED(object); 676eb4d6a1bSKonstantin Belousov KASSERT((object->flags & OBJ_DEAD) != 0, ("dealloc of reachable obj")); 677eb4d6a1bSKonstantin Belousov 67826f9a767SRodney W. Grimes /* 6791c7c3c6aSMatthew Dillon * Remove from list right away so lookups will fail if we block for 6801c7c3c6aSMatthew Dillon * pageout completion. 68126f9a767SRodney W. Grimes */ 682bd228075SAlan Cox if (object->handle != NULL) { 683eb4d6a1bSKonstantin Belousov VM_OBJECT_WUNLOCK(object); 684eb4d6a1bSKonstantin Belousov sx_xlock(&sw_alloc_sx); 685eb4d6a1bSKonstantin Belousov TAILQ_REMOVE(NOBJLIST(object->handle), object, 686eb4d6a1bSKonstantin Belousov pager_object_list); 687eb4d6a1bSKonstantin Belousov sx_xunlock(&sw_alloc_sx); 688eb4d6a1bSKonstantin Belousov VM_OBJECT_WLOCK(object); 689bd228075SAlan Cox } 6901c7c3c6aSMatthew Dillon 6911c7c3c6aSMatthew Dillon vm_object_pip_wait(object, "swpdea"); 6921c7c3c6aSMatthew Dillon 6931c7c3c6aSMatthew Dillon /* 6941c7c3c6aSMatthew Dillon * Free all remaining metadata. We only bother to free it from 6951c7c3c6aSMatthew Dillon * the swap meta data. We do not attempt to free swapblk's still 6961c7c3c6aSMatthew Dillon * associated with vm_page_t's for this object. We do not care 6971c7c3c6aSMatthew Dillon * if paging is still in progress on some objects. 6981c7c3c6aSMatthew Dillon */ 6991c7c3c6aSMatthew Dillon swp_pager_meta_free_all(object); 700e735691bSJohn Baldwin object->handle = NULL; 701e735691bSJohn Baldwin object->type = OBJT_DEAD; 7021c7c3c6aSMatthew Dillon } 7031c7c3c6aSMatthew Dillon 7041c7c3c6aSMatthew Dillon /************************************************************************ 7051c7c3c6aSMatthew Dillon * SWAP PAGER BITMAP ROUTINES * 7061c7c3c6aSMatthew Dillon ************************************************************************/ 7071c7c3c6aSMatthew Dillon 7081c7c3c6aSMatthew Dillon /* 7091c7c3c6aSMatthew Dillon * SWP_PAGER_GETSWAPSPACE() - allocate raw swap space 7101c7c3c6aSMatthew Dillon * 711*48e98a2aSDoug Moore * Allocate swap for up to the requested number of pages, and at 712*48e98a2aSDoug Moore * least a minimum number of pages. The starting swap block number 713*48e98a2aSDoug Moore * (a page index) is returned or SWAPBLK_NONE if the allocation 714*48e98a2aSDoug Moore * failed. 7151c7c3c6aSMatthew Dillon * 7161c7c3c6aSMatthew Dillon * Also has the side effect of advising that somebody made a mistake 7171c7c3c6aSMatthew Dillon * when they configured swap and didn't configure enough. 7181c7c3c6aSMatthew Dillon * 71915523cf7SKonstantin Belousov * This routine may not sleep. 7208f60c087SPoul-Henning Kamp * 7218f60c087SPoul-Henning Kamp * We allocate in round-robin fashion from the configured devices. 7221c7c3c6aSMatthew Dillon */ 723a5edd34aSPoul-Henning Kamp static daddr_t 724*48e98a2aSDoug Moore swp_pager_getswapspace(int *io_npages, int limit) 7251c7c3c6aSMatthew Dillon { 7261c7c3c6aSMatthew Dillon daddr_t blk; 7278f60c087SPoul-Henning Kamp struct swdevt *sp; 728*48e98a2aSDoug Moore int npages; 7291c7c3c6aSMatthew Dillon 7308f60c087SPoul-Henning Kamp blk = SWAPBLK_NONE; 731*48e98a2aSDoug Moore npages = *io_npages; 73220da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 7338f60c087SPoul-Henning Kamp sp = swdevhd; 734*48e98a2aSDoug Moore while (!TAILQ_EMPTY(&swtailq)) { 7358f60c087SPoul-Henning Kamp if (sp == NULL) 7368f60c087SPoul-Henning Kamp sp = TAILQ_FIRST(&swtailq); 737*48e98a2aSDoug Moore if ((sp->sw_flags & SW_CLOSING) == 0) 7388f60c087SPoul-Henning Kamp blk = blist_alloc(sp->sw_blist, npages); 739*48e98a2aSDoug Moore if (blk != SWAPBLK_NONE) 740*48e98a2aSDoug Moore break; 741*48e98a2aSDoug Moore sp = TAILQ_NEXT(sp, sw_list); 742*48e98a2aSDoug Moore if (swdevhd == sp) { 743*48e98a2aSDoug Moore if (npages <= limit) 744*48e98a2aSDoug Moore break; 745*48e98a2aSDoug Moore npages >>= 1; 746*48e98a2aSDoug Moore } 747*48e98a2aSDoug Moore } 7488f60c087SPoul-Henning Kamp if (blk != SWAPBLK_NONE) { 749*48e98a2aSDoug Moore *io_npages = npages; 7508f60c087SPoul-Henning Kamp blk += sp->sw_first; 7518f60c087SPoul-Henning Kamp sp->sw_used += npages; 752d05bc129SAlan Cox swap_pager_avail -= npages; 7538f60c087SPoul-Henning Kamp swp_sizecheck(); 7548f60c087SPoul-Henning Kamp swdevhd = TAILQ_NEXT(sp, sw_list); 755*48e98a2aSDoug Moore } else { 7562b0d37a4SMatthew Dillon if (swap_pager_full != 2) { 757*48e98a2aSDoug Moore printf("swp_pager_getswapspace(%d): failed\n", 758*48e98a2aSDoug Moore *io_npages); 7592b0d37a4SMatthew Dillon swap_pager_full = 2; 76020d3034fSMatthew Dillon swap_pager_almost_full = 1; 7612b0d37a4SMatthew Dillon } 7628f60c087SPoul-Henning Kamp swdevhd = NULL; 763*48e98a2aSDoug Moore } 764d05bc129SAlan Cox mtx_unlock(&sw_dev_mtx); 7651c7c3c6aSMatthew Dillon return (blk); 76626f9a767SRodney W. Grimes } 76726f9a767SRodney W. Grimes 768541a1175SAlan Cox static bool 769b3fed13eSDavid Schultz swp_pager_isondev(daddr_t blk, struct swdevt *sp) 7708f60c087SPoul-Henning Kamp { 7718f60c087SPoul-Henning Kamp 772b3fed13eSDavid Schultz return (blk >= sp->sw_first && blk < sp->sw_end); 7738f60c087SPoul-Henning Kamp } 7748f60c087SPoul-Henning Kamp 7754b03903aSPoul-Henning Kamp static void 7764b03903aSPoul-Henning Kamp swp_pager_strategy(struct buf *bp) 7774b03903aSPoul-Henning Kamp { 7784b03903aSPoul-Henning Kamp struct swdevt *sp; 7794b03903aSPoul-Henning Kamp 78020da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 7814b03903aSPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 782541a1175SAlan Cox if (swp_pager_isondev(bp->b_blkno, sp)) { 78320da9c2eSPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 7842cc718a1SKonstantin Belousov if ((sp->sw_flags & SW_UNMAPPED) != 0 && 7852cc718a1SKonstantin Belousov unmapped_buf_allowed) { 7862cc718a1SKonstantin Belousov bp->b_data = unmapped_buf; 7872cc718a1SKonstantin Belousov bp->b_offset = 0; 7882cc718a1SKonstantin Belousov } else { 7892cc718a1SKonstantin Belousov pmap_qenter((vm_offset_t)bp->b_data, 7902cc718a1SKonstantin Belousov &bp->b_pages[0], bp->b_bcount / PAGE_SIZE); 7912cc718a1SKonstantin Belousov } 7924b03903aSPoul-Henning Kamp sp->sw_strategy(bp, sp); 7934b03903aSPoul-Henning Kamp return; 7944b03903aSPoul-Henning Kamp } 7954b03903aSPoul-Henning Kamp } 79620da9c2eSPoul-Henning Kamp panic("Swapdev not found"); 7974b03903aSPoul-Henning Kamp } 7984b03903aSPoul-Henning Kamp 7998f60c087SPoul-Henning Kamp 80026f9a767SRodney W. Grimes /* 8011c7c3c6aSMatthew Dillon * SWP_PAGER_FREESWAPSPACE() - free raw swap space 8021c7c3c6aSMatthew Dillon * 8031c7c3c6aSMatthew Dillon * This routine returns the specified swap blocks back to the bitmap. 8041c7c3c6aSMatthew Dillon * 80515523cf7SKonstantin Belousov * This routine may not sleep. 80626f9a767SRodney W. Grimes */ 807a5edd34aSPoul-Henning Kamp static void 808230869e0SAlan Cox swp_pager_freeswapspace(daddr_t blk, daddr_t npages) 8090d94caffSDavid Greenman { 8108f60c087SPoul-Henning Kamp struct swdevt *sp; 81192da00bbSMatthew Dillon 812230869e0SAlan Cox if (npages == 0) 813230869e0SAlan Cox return; 8147645e885SAlan Cox mtx_lock(&sw_dev_mtx); 8157645e885SAlan Cox TAILQ_FOREACH(sp, &swtailq, sw_list) { 816541a1175SAlan Cox if (swp_pager_isondev(blk, sp)) { 81792da00bbSMatthew Dillon sp->sw_used -= npages; 81892da00bbSMatthew Dillon /* 8197645e885SAlan Cox * If we are attempting to stop swapping on 8207645e885SAlan Cox * this device, we don't want to mark any 8217645e885SAlan Cox * blocks free lest they be reused. 82292da00bbSMatthew Dillon */ 8237645e885SAlan Cox if ((sp->sw_flags & SW_CLOSING) == 0) { 8247645e885SAlan Cox blist_free(sp->sw_blist, blk - sp->sw_first, 8257645e885SAlan Cox npages); 8268f60c087SPoul-Henning Kamp swap_pager_avail += npages; 8271c7c3c6aSMatthew Dillon swp_sizecheck(); 82826f9a767SRodney W. Grimes } 8297645e885SAlan Cox mtx_unlock(&sw_dev_mtx); 8307645e885SAlan Cox return; 8317645e885SAlan Cox } 8327645e885SAlan Cox } 8337645e885SAlan Cox panic("Swapdev not found"); 8347645e885SAlan Cox } 8351c7c3c6aSMatthew Dillon 83626f9a767SRodney W. Grimes /* 837d027ed2eSAlan Cox * SYSCTL_SWAP_FRAGMENTATION() - produce raw swap space stats 838d027ed2eSAlan Cox */ 839d027ed2eSAlan Cox static int 840d027ed2eSAlan Cox sysctl_swap_fragmentation(SYSCTL_HANDLER_ARGS) 841d027ed2eSAlan Cox { 842d027ed2eSAlan Cox struct sbuf sbuf; 843d027ed2eSAlan Cox struct swdevt *sp; 844d027ed2eSAlan Cox const char *devname; 845d027ed2eSAlan Cox int error; 846d027ed2eSAlan Cox 847d027ed2eSAlan Cox error = sysctl_wire_old_buffer(req, 0); 848d027ed2eSAlan Cox if (error != 0) 849d027ed2eSAlan Cox return (error); 850d027ed2eSAlan Cox sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 851d027ed2eSAlan Cox mtx_lock(&sw_dev_mtx); 852d027ed2eSAlan Cox TAILQ_FOREACH(sp, &swtailq, sw_list) { 853d027ed2eSAlan Cox if (vn_isdisk(sp->sw_vp, NULL)) 854d027ed2eSAlan Cox devname = devtoname(sp->sw_vp->v_rdev); 855d027ed2eSAlan Cox else 856d027ed2eSAlan Cox devname = "[file]"; 857d027ed2eSAlan Cox sbuf_printf(&sbuf, "\nFree space on device %s:\n", devname); 858d027ed2eSAlan Cox blist_stats(sp->sw_blist, &sbuf); 859d027ed2eSAlan Cox } 860d027ed2eSAlan Cox mtx_unlock(&sw_dev_mtx); 861d027ed2eSAlan Cox error = sbuf_finish(&sbuf); 862d027ed2eSAlan Cox sbuf_delete(&sbuf); 863d027ed2eSAlan Cox return (error); 864d027ed2eSAlan Cox } 865d027ed2eSAlan Cox 866d027ed2eSAlan Cox /* 8671c7c3c6aSMatthew Dillon * SWAP_PAGER_FREESPACE() - frees swap blocks associated with a page 8681c7c3c6aSMatthew Dillon * range within an object. 8691c7c3c6aSMatthew Dillon * 8701c7c3c6aSMatthew Dillon * This is a globally accessible routine. 8711c7c3c6aSMatthew Dillon * 8721c7c3c6aSMatthew Dillon * This routine removes swapblk assignments from swap metadata. 8731c7c3c6aSMatthew Dillon * 8741c7c3c6aSMatthew Dillon * The external callers of this routine typically have already destroyed 8751c7c3c6aSMatthew Dillon * or renamed vm_page_t's associated with this range in the object so 8761c7c3c6aSMatthew Dillon * we should be ok. 877c25673ffSAttilio Rao * 878c25673ffSAttilio Rao * The object must be locked. 87926f9a767SRodney W. Grimes */ 88026f9a767SRodney W. Grimes void 8812f249180SPoul-Henning Kamp swap_pager_freespace(vm_object_t object, vm_pindex_t start, vm_size_t size) 88226f9a767SRodney W. Grimes { 88323955314SAlfred Perlstein 8841c7c3c6aSMatthew Dillon swp_pager_meta_free(object, start, size); 8854dcc5c2dSMatthew Dillon } 8864dcc5c2dSMatthew Dillon 8874dcc5c2dSMatthew Dillon /* 8884dcc5c2dSMatthew Dillon * SWAP_PAGER_RESERVE() - reserve swap blocks in object 8894dcc5c2dSMatthew Dillon * 8904dcc5c2dSMatthew Dillon * Assigns swap blocks to the specified range within the object. The 89156ce850bSKonstantin Belousov * swap blocks are not zeroed. Any previous swap assignment is destroyed. 8924dcc5c2dSMatthew Dillon * 8934dcc5c2dSMatthew Dillon * Returns 0 on success, -1 on failure. 8944dcc5c2dSMatthew Dillon */ 8954dcc5c2dSMatthew Dillon int 8964dcc5c2dSMatthew Dillon swap_pager_reserve(vm_object_t object, vm_pindex_t start, vm_size_t size) 8974dcc5c2dSMatthew Dillon { 898*48e98a2aSDoug Moore daddr_t addr, blk, n_free, s_free; 899*48e98a2aSDoug Moore int i, j, n; 9004dcc5c2dSMatthew Dillon 90178f1deefSAlan Cox swp_pager_init_freerange(&s_free, &n_free); 90289f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 903*48e98a2aSDoug Moore for (i = 0; i < size; i += n) { 904*48e98a2aSDoug Moore n = min(BLIST_MAX_ALLOC, size - i); 905*48e98a2aSDoug Moore blk = swp_pager_getswapspace(&n, 1); 906*48e98a2aSDoug Moore if (blk == SWAPBLK_NONE) { 907*48e98a2aSDoug Moore swp_pager_meta_free(object, start, i); 90889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 9094dcc5c2dSMatthew Dillon return (-1); 9104dcc5c2dSMatthew Dillon } 911*48e98a2aSDoug Moore for (j = 0; j < n; ++j) { 912*48e98a2aSDoug Moore addr = swp_pager_meta_build(object, 913*48e98a2aSDoug Moore start + i + j, blk + j); 91478f1deefSAlan Cox if (addr != SWAPBLK_NONE) 915*48e98a2aSDoug Moore swp_pager_update_freerange(&s_free, &n_free, 916*48e98a2aSDoug Moore addr); 917*48e98a2aSDoug Moore } 9184dcc5c2dSMatthew Dillon } 91978f1deefSAlan Cox swp_pager_freeswapspace(s_free, n_free); 92089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 9214dcc5c2dSMatthew Dillon return (0); 92226f9a767SRodney W. Grimes } 92326f9a767SRodney W. Grimes 9240a47b48bSJohn Dyson /* 9251c7c3c6aSMatthew Dillon * SWAP_PAGER_COPY() - copy blocks from source pager to destination pager 9261c7c3c6aSMatthew Dillon * and destroy the source. 9271c7c3c6aSMatthew Dillon * 9281c7c3c6aSMatthew Dillon * Copy any valid swapblks from the source to the destination. In 9291c7c3c6aSMatthew Dillon * cases where both the source and destination have a valid swapblk, 9301c7c3c6aSMatthew Dillon * we keep the destination's. 9311c7c3c6aSMatthew Dillon * 93215523cf7SKonstantin Belousov * This routine is allowed to sleep. It may sleep allocating metadata 9331c7c3c6aSMatthew Dillon * indirectly through swp_pager_meta_build() or if paging is still in 9341c7c3c6aSMatthew Dillon * progress on the source. 9351c7c3c6aSMatthew Dillon * 9361c7c3c6aSMatthew Dillon * The source object contains no vm_page_t's (which is just as well) 9371c7c3c6aSMatthew Dillon * 9381c7c3c6aSMatthew Dillon * The source object is of type OBJT_SWAP. 9391c7c3c6aSMatthew Dillon * 94015523cf7SKonstantin Belousov * The source and destination objects must be locked. 94115523cf7SKonstantin Belousov * Both object locks may temporarily be released. 94226f9a767SRodney W. Grimes */ 94326f9a767SRodney W. Grimes void 9442f249180SPoul-Henning Kamp swap_pager_copy(vm_object_t srcobject, vm_object_t dstobject, 9452f249180SPoul-Henning Kamp vm_pindex_t offset, int destroysource) 94626f9a767SRodney W. Grimes { 947a316d390SJohn Dyson vm_pindex_t i; 94878f1deefSAlan Cox daddr_t dstaddr, n_free, s_free, srcaddr; 9494dcc5c2dSMatthew Dillon 95089f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(srcobject); 95189f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(dstobject); 9520cddd8f0SMatthew Dillon 95326f9a767SRodney W. Grimes /* 9541c7c3c6aSMatthew Dillon * If destroysource is set, we remove the source object from the 9551c7c3c6aSMatthew Dillon * swap_pager internal queue now. 95626f9a767SRodney W. Grimes */ 957eb4d6a1bSKonstantin Belousov if (destroysource && srcobject->handle != NULL) { 958eb4d6a1bSKonstantin Belousov vm_object_pip_add(srcobject, 1); 959eb4d6a1bSKonstantin Belousov VM_OBJECT_WUNLOCK(srcobject); 960eb4d6a1bSKonstantin Belousov vm_object_pip_add(dstobject, 1); 961eb4d6a1bSKonstantin Belousov VM_OBJECT_WUNLOCK(dstobject); 962eb4d6a1bSKonstantin Belousov sx_xlock(&sw_alloc_sx); 963eb4d6a1bSKonstantin Belousov TAILQ_REMOVE(NOBJLIST(srcobject->handle), srcobject, 964eb4d6a1bSKonstantin Belousov pager_object_list); 965eb4d6a1bSKonstantin Belousov sx_xunlock(&sw_alloc_sx); 966eb4d6a1bSKonstantin Belousov VM_OBJECT_WLOCK(dstobject); 967eb4d6a1bSKonstantin Belousov vm_object_pip_wakeup(dstobject); 968eb4d6a1bSKonstantin Belousov VM_OBJECT_WLOCK(srcobject); 969eb4d6a1bSKonstantin Belousov vm_object_pip_wakeup(srcobject); 970bd228075SAlan Cox } 97126f9a767SRodney W. Grimes 9721c7c3c6aSMatthew Dillon /* 9734abca9bbSAlan Cox * Transfer source to destination. 9741c7c3c6aSMatthew Dillon */ 97578f1deefSAlan Cox swp_pager_init_freerange(&s_free, &n_free); 9761c7c3c6aSMatthew Dillon for (i = 0; i < dstobject->size; ++i) { 9774abca9bbSAlan Cox srcaddr = swp_pager_meta_ctl(srcobject, i + offset, SWM_POP); 9784abca9bbSAlan Cox if (srcaddr == SWAPBLK_NONE) 9794abca9bbSAlan Cox continue; 9801c7c3c6aSMatthew Dillon dstaddr = swp_pager_meta_ctl(dstobject, i, 0); 98178f1deefSAlan Cox if (dstaddr != SWAPBLK_NONE) { 98278f1deefSAlan Cox /* 98378f1deefSAlan Cox * Destination has valid swapblk or it is represented 98478f1deefSAlan Cox * by a resident page. We destroy the source block. 98578f1deefSAlan Cox */ 98678f1deefSAlan Cox swp_pager_update_freerange(&s_free, &n_free, srcaddr); 98778f1deefSAlan Cox continue; 98878f1deefSAlan Cox } 98978f1deefSAlan Cox 9901c7c3c6aSMatthew Dillon /* 9911c7c3c6aSMatthew Dillon * Destination has no swapblk and is not resident, 9921c7c3c6aSMatthew Dillon * copy source. 9934abca9bbSAlan Cox * 994c7c8dd7eSAlan Cox * swp_pager_meta_build() can sleep. 995c7c8dd7eSAlan Cox */ 996c7c8dd7eSAlan Cox vm_object_pip_add(srcobject, 1); 99789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(srcobject); 998c7c8dd7eSAlan Cox vm_object_pip_add(dstobject, 1); 99978f1deefSAlan Cox dstaddr = swp_pager_meta_build(dstobject, i, srcaddr); 100078f1deefSAlan Cox KASSERT(dstaddr == SWAPBLK_NONE, 100178f1deefSAlan Cox ("Unexpected destination swapblk")); 1002c7c8dd7eSAlan Cox vm_object_pip_wakeup(dstobject); 100389f6b863SAttilio Rao VM_OBJECT_WLOCK(srcobject); 1004c7c8dd7eSAlan Cox vm_object_pip_wakeup(srcobject); 10051c7c3c6aSMatthew Dillon } 100678f1deefSAlan Cox swp_pager_freeswapspace(s_free, n_free); 100726f9a767SRodney W. Grimes 100826f9a767SRodney W. Grimes /* 10091c7c3c6aSMatthew Dillon * Free left over swap blocks in source. 10101c7c3c6aSMatthew Dillon * 1011763df3ecSPedro F. Giffuni * We have to revert the type to OBJT_DEFAULT so we do not accidentally 10121c7c3c6aSMatthew Dillon * double-remove the object from the swap queues. 101326f9a767SRodney W. Grimes */ 1014c0877f10SJohn Dyson if (destroysource) { 10151c7c3c6aSMatthew Dillon swp_pager_meta_free_all(srcobject); 10161c7c3c6aSMatthew Dillon /* 10171c7c3c6aSMatthew Dillon * Reverting the type is not necessary, the caller is going 10181c7c3c6aSMatthew Dillon * to destroy srcobject directly, but I'm doing it here 1019956f3135SPhilippe Charnier * for consistency since we've removed the object from its 10201c7c3c6aSMatthew Dillon * queues. 10211c7c3c6aSMatthew Dillon */ 10221c7c3c6aSMatthew Dillon srcobject->type = OBJT_DEFAULT; 1023c0877f10SJohn Dyson } 102426f9a767SRodney W. Grimes } 102526f9a767SRodney W. Grimes 1026df8bae1dSRodney W. Grimes /* 10271c7c3c6aSMatthew Dillon * SWAP_PAGER_HASPAGE() - determine if we have good backing store for 10281c7c3c6aSMatthew Dillon * the requested page. 10291c7c3c6aSMatthew Dillon * 10301c7c3c6aSMatthew Dillon * We determine whether good backing store exists for the requested 10311c7c3c6aSMatthew Dillon * page and return TRUE if it does, FALSE if it doesn't. 10321c7c3c6aSMatthew Dillon * 10331c7c3c6aSMatthew Dillon * If TRUE, we also try to determine how much valid, contiguous backing 1034915d1b71SMark Johnston * store exists before and after the requested page. 1035df8bae1dSRodney W. Grimes */ 10365ea4972cSAlan Cox static boolean_t 1037915d1b71SMark Johnston swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, 1038915d1b71SMark Johnston int *after) 103926f9a767SRodney W. Grimes { 1040915d1b71SMark Johnston daddr_t blk, blk0; 1041915d1b71SMark Johnston int i; 104226f9a767SRodney W. Grimes 1043c25673ffSAttilio Rao VM_OBJECT_ASSERT_LOCKED(object); 1044915d1b71SMark Johnston 10451c7c3c6aSMatthew Dillon /* 10461c7c3c6aSMatthew Dillon * do we have good backing store at the requested index ? 10471c7c3c6aSMatthew Dillon */ 10481c7c3c6aSMatthew Dillon blk0 = swp_pager_meta_ctl(object, pindex, 0); 10494dcc5c2dSMatthew Dillon if (blk0 == SWAPBLK_NONE) { 10501c7c3c6aSMatthew Dillon if (before) 105124a1cce3SDavid Greenman *before = 0; 10521c7c3c6aSMatthew Dillon if (after) 105324a1cce3SDavid Greenman *after = 0; 105426f9a767SRodney W. Grimes return (FALSE); 105526f9a767SRodney W. Grimes } 105626f9a767SRodney W. Grimes 105726f9a767SRodney W. Grimes /* 10581c7c3c6aSMatthew Dillon * find backwards-looking contiguous good backing store 1059e47ed70bSJohn Dyson */ 10601c7c3c6aSMatthew Dillon if (before != NULL) { 1061915d1b71SMark Johnston for (i = 1; i < SWB_NPAGES; i++) { 10621c7c3c6aSMatthew Dillon if (i > pindex) 10631c7c3c6aSMatthew Dillon break; 10641c7c3c6aSMatthew Dillon blk = swp_pager_meta_ctl(object, pindex - i, 0); 10651c7c3c6aSMatthew Dillon if (blk != blk0 - i) 10661c7c3c6aSMatthew Dillon break; 1067ffc82b0aSJohn Dyson } 1068915d1b71SMark Johnston *before = i - 1; 106926f9a767SRodney W. Grimes } 107026f9a767SRodney W. Grimes 107126f9a767SRodney W. Grimes /* 10721c7c3c6aSMatthew Dillon * find forward-looking contiguous good backing store 107326f9a767SRodney W. Grimes */ 10741c7c3c6aSMatthew Dillon if (after != NULL) { 1075915d1b71SMark Johnston for (i = 1; i < SWB_NPAGES; i++) { 10761c7c3c6aSMatthew Dillon blk = swp_pager_meta_ctl(object, pindex + i, 0); 10771c7c3c6aSMatthew Dillon if (blk != blk0 + i) 10781c7c3c6aSMatthew Dillon break; 107926f9a767SRodney W. Grimes } 1080915d1b71SMark Johnston *after = i - 1; 10811c7c3c6aSMatthew Dillon } 10821c7c3c6aSMatthew Dillon return (TRUE); 10831c7c3c6aSMatthew Dillon } 10841c7c3c6aSMatthew Dillon 10851c7c3c6aSMatthew Dillon /* 10861c7c3c6aSMatthew Dillon * SWAP_PAGER_PAGE_UNSWAPPED() - remove swap backing store related to page 10871c7c3c6aSMatthew Dillon * 10881c7c3c6aSMatthew Dillon * This removes any associated swap backing store, whether valid or 10891c7c3c6aSMatthew Dillon * not, from the page. 10901c7c3c6aSMatthew Dillon * 10911c7c3c6aSMatthew Dillon * This routine is typically called when a page is made dirty, at 10921c7c3c6aSMatthew Dillon * which point any associated swap can be freed. MADV_FREE also 10931c7c3c6aSMatthew Dillon * calls us in a special-case situation 10941c7c3c6aSMatthew Dillon * 10951c7c3c6aSMatthew Dillon * NOTE!!! If the page is clean and the swap was valid, the caller 10961c7c3c6aSMatthew Dillon * should make the page dirty before calling this routine. This routine 10971c7c3c6aSMatthew Dillon * does NOT change the m->dirty status of the page. Also: MADV_FREE 10981c7c3c6aSMatthew Dillon * depends on it. 10991c7c3c6aSMatthew Dillon * 110015523cf7SKonstantin Belousov * This routine may not sleep. 1101c25673ffSAttilio Rao * 1102c25673ffSAttilio Rao * The object containing the page must be locked. 11031c7c3c6aSMatthew Dillon */ 11041c7c3c6aSMatthew Dillon static void 11052f249180SPoul-Henning Kamp swap_pager_unswapped(vm_page_t m) 11061c7c3c6aSMatthew Dillon { 11074abca9bbSAlan Cox daddr_t srcaddr; 11082f249180SPoul-Henning Kamp 11094abca9bbSAlan Cox srcaddr = swp_pager_meta_ctl(m->object, m->pindex, SWM_POP); 11104abca9bbSAlan Cox if (srcaddr != SWAPBLK_NONE) 11114abca9bbSAlan Cox swp_pager_freeswapspace(srcaddr, 1); 11121c7c3c6aSMatthew Dillon } 11131c7c3c6aSMatthew Dillon 11141c7c3c6aSMatthew Dillon /* 1115915d1b71SMark Johnston * swap_pager_getpages() - bring pages in from swap 11161c7c3c6aSMatthew Dillon * 11173f060b60SMark Johnston * Attempt to page in the pages in array "ma" of length "count". The 11183f060b60SMark Johnston * caller may optionally specify that additional pages preceding and 11193f060b60SMark Johnston * succeeding the specified range be paged in. The number of such pages 11203f060b60SMark Johnston * is returned in the "rbehind" and "rahead" parameters, and they will 11213f060b60SMark Johnston * be in the inactive queue upon return. 11221c7c3c6aSMatthew Dillon * 11233f060b60SMark Johnston * The pages in "ma" must be busied and will remain busied upon return. 11241c7c3c6aSMatthew Dillon */ 1125f708ef1bSPoul-Henning Kamp static int 11263f060b60SMark Johnston swap_pager_getpages(vm_object_t object, vm_page_t *ma, int count, int *rbehind, 1127b0cd2017SGleb Smirnoff int *rahead) 1128df8bae1dSRodney W. Grimes { 11291c7c3c6aSMatthew Dillon struct buf *bp; 1130b7b8a096SKonstantin Belousov vm_page_t bm, mpred, msucc, p; 1131915d1b71SMark Johnston vm_pindex_t pindex; 11321c7c3c6aSMatthew Dillon daddr_t blk; 1133b7b8a096SKonstantin Belousov int i, maxahead, maxbehind, reqcount; 11340d94caffSDavid Greenman 1135915d1b71SMark Johnston reqcount = count; 11361c7c3c6aSMatthew Dillon 1137b7b8a096SKonstantin Belousov /* 1138b7b8a096SKonstantin Belousov * Determine the final number of read-behind pages and 1139b7b8a096SKonstantin Belousov * allocate them BEFORE releasing the object lock. Otherwise, 1140b7b8a096SKonstantin Belousov * there can be a problematic race with vm_object_split(). 1141b7b8a096SKonstantin Belousov * Specifically, vm_object_split() might first transfer pages 1142b7b8a096SKonstantin Belousov * that precede ma[0] in the current object to a new object, 1143b7b8a096SKonstantin Belousov * and then this function incorrectly recreates those pages as 1144b7b8a096SKonstantin Belousov * read-behind pages in the current object. 1145b7b8a096SKonstantin Belousov */ 1146b7b8a096SKonstantin Belousov if (!swap_pager_haspage(object, ma[0]->pindex, &maxbehind, &maxahead)) 1147915d1b71SMark Johnston return (VM_PAGER_FAIL); 1148915d1b71SMark Johnston 1149915d1b71SMark Johnston /* 1150915d1b71SMark Johnston * Clip the readahead and readbehind ranges to exclude resident pages. 1151915d1b71SMark Johnston */ 1152915d1b71SMark Johnston if (rahead != NULL) { 1153dd9cb6daSMark Johnston KASSERT(reqcount - 1 <= maxahead, 1154915d1b71SMark Johnston ("page count %d extends beyond swap block", reqcount)); 1155dd9cb6daSMark Johnston *rahead = imin(*rahead, maxahead - (reqcount - 1)); 11563f060b60SMark Johnston pindex = ma[reqcount - 1]->pindex; 11573f060b60SMark Johnston msucc = TAILQ_NEXT(ma[reqcount - 1], listq); 1158915d1b71SMark Johnston if (msucc != NULL && msucc->pindex - pindex - 1 < *rahead) 1159915d1b71SMark Johnston *rahead = msucc->pindex - pindex - 1; 1160915d1b71SMark Johnston } 1161915d1b71SMark Johnston if (rbehind != NULL) { 1162dd9cb6daSMark Johnston *rbehind = imin(*rbehind, maxbehind); 11633f060b60SMark Johnston pindex = ma[0]->pindex; 11643f060b60SMark Johnston mpred = TAILQ_PREV(ma[0], pglist, listq); 1165915d1b71SMark Johnston if (mpred != NULL && pindex - mpred->pindex - 1 < *rbehind) 1166915d1b71SMark Johnston *rbehind = pindex - mpred->pindex - 1; 1167915d1b71SMark Johnston } 1168915d1b71SMark Johnston 1169b7b8a096SKonstantin Belousov bm = ma[0]; 1170b7b8a096SKonstantin Belousov for (i = 0; i < count; i++) 1171b7b8a096SKonstantin Belousov ma[i]->oflags |= VPO_SWAPINPROG; 1172b7b8a096SKonstantin Belousov 1173915d1b71SMark Johnston /* 1174915d1b71SMark Johnston * Allocate readahead and readbehind pages. 1175915d1b71SMark Johnston */ 1176b7b8a096SKonstantin Belousov if (rbehind != NULL) { 1177b7b8a096SKonstantin Belousov for (i = 1; i <= *rbehind; i++) { 11783f060b60SMark Johnston p = vm_page_alloc(object, ma[0]->pindex - i, 11797667839aSAlan Cox VM_ALLOC_NORMAL); 1180b7b8a096SKonstantin Belousov if (p == NULL) 1181915d1b71SMark Johnston break; 1182b7b8a096SKonstantin Belousov p->oflags |= VPO_SWAPINPROG; 1183b7b8a096SKonstantin Belousov bm = p; 1184915d1b71SMark Johnston } 1185b7b8a096SKonstantin Belousov *rbehind = i - 1; 1186915d1b71SMark Johnston } 1187915d1b71SMark Johnston if (rahead != NULL) { 1188915d1b71SMark Johnston for (i = 0; i < *rahead; i++) { 1189915d1b71SMark Johnston p = vm_page_alloc(object, 11903f060b60SMark Johnston ma[reqcount - 1]->pindex + i + 1, VM_ALLOC_NORMAL); 1191915d1b71SMark Johnston if (p == NULL) 1192915d1b71SMark Johnston break; 1193b7b8a096SKonstantin Belousov p->oflags |= VPO_SWAPINPROG; 1194915d1b71SMark Johnston } 1195915d1b71SMark Johnston *rahead = i; 1196915d1b71SMark Johnston } 1197915d1b71SMark Johnston if (rbehind != NULL) 1198915d1b71SMark Johnston count += *rbehind; 1199915d1b71SMark Johnston if (rahead != NULL) 1200915d1b71SMark Johnston count += *rahead; 1201915d1b71SMark Johnston 1202915d1b71SMark Johnston vm_object_pip_add(object, count); 1203915d1b71SMark Johnston 1204b7b8a096SKonstantin Belousov pindex = bm->pindex; 1205915d1b71SMark Johnston blk = swp_pager_meta_ctl(object, pindex, 0); 1206915d1b71SMark Johnston KASSERT(blk != SWAPBLK_NONE, 1207915d1b71SMark Johnston ("no swap blocking containing %p(%jx)", object, (uintmax_t)pindex)); 1208915d1b71SMark Johnston 1209915d1b71SMark Johnston VM_OBJECT_WUNLOCK(object); 1210756a5412SGleb Smirnoff bp = uma_zalloc(swrbuf_zone, M_WAITOK); 1211b7b8a096SKonstantin Belousov /* Pages cannot leave the object while busy. */ 1212b7b8a096SKonstantin Belousov for (i = 0, p = bm; i < count; i++, p = TAILQ_NEXT(p, listq)) { 1213b7b8a096SKonstantin Belousov MPASS(p->pindex == bm->pindex + i); 1214b7b8a096SKonstantin Belousov bp->b_pages[i] = p; 1215b7b8a096SKonstantin Belousov } 1216915d1b71SMark Johnston 1217915d1b71SMark Johnston bp->b_flags |= B_PAGING; 121821144e3bSPoul-Henning Kamp bp->b_iocmd = BIO_READ; 12191c7c3c6aSMatthew Dillon bp->b_iodone = swp_pager_async_iodone; 1220fdcc1cc0SJohn Baldwin bp->b_rcred = crhold(thread0.td_ucred); 1221fdcc1cc0SJohn Baldwin bp->b_wcred = crhold(thread0.td_ucred); 1222b0cd2017SGleb Smirnoff bp->b_blkno = blk; 1223b0cd2017SGleb Smirnoff bp->b_bcount = PAGE_SIZE * count; 1224b0cd2017SGleb Smirnoff bp->b_bufsize = PAGE_SIZE * count; 1225b0cd2017SGleb Smirnoff bp->b_npages = count; 1226915d1b71SMark Johnston bp->b_pgbefore = rbehind != NULL ? *rbehind : 0; 1227915d1b71SMark Johnston bp->b_pgafter = rahead != NULL ? *rahead : 0; 122826f9a767SRodney W. Grimes 122983c9dea1SGleb Smirnoff VM_CNT_INC(v_swapin); 123083c9dea1SGleb Smirnoff VM_CNT_ADD(v_swappgsin, count); 12311c7c3c6aSMatthew Dillon 12321c7c3c6aSMatthew Dillon /* 12331c7c3c6aSMatthew Dillon * perform the I/O. NOTE!!! bp cannot be considered valid after 12341c7c3c6aSMatthew Dillon * this point because we automatically release it on completion. 12351c7c3c6aSMatthew Dillon * Instead, we look at the one page we are interested in which we 12361c7c3c6aSMatthew Dillon * still hold a lock on even through the I/O completion. 12371c7c3c6aSMatthew Dillon * 12383f060b60SMark Johnston * The other pages in our ma[] array are also released on completion, 12391c7c3c6aSMatthew Dillon * so we cannot assume they are valid anymore either. 12401c7c3c6aSMatthew Dillon * 1241c37a77eeSPoul-Henning Kamp * NOTE: b_blkno is destroyed by the call to swapdev_strategy 12421c7c3c6aSMatthew Dillon */ 1243b890cb2cSPeter Wemm BUF_KERNPROC(bp); 12444b03903aSPoul-Henning Kamp swp_pager_strategy(bp); 124526f9a767SRodney W. Grimes 124626f9a767SRodney W. Grimes /* 1247915d1b71SMark Johnston * Wait for the pages we want to complete. VPO_SWAPINPROG is always 12481c7c3c6aSMatthew Dillon * cleared on completion. If an I/O error occurs, SWAPBLK_NONE 1249915d1b71SMark Johnston * is set in the metadata for each page in the request. 125026f9a767SRodney W. Grimes */ 125189f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 12523f060b60SMark Johnston while ((ma[0]->oflags & VPO_SWAPINPROG) != 0) { 12533f060b60SMark Johnston ma[0]->oflags |= VPO_SWAPSLEEP; 125483c9dea1SGleb Smirnoff VM_CNT_INC(v_intrans); 1255c7aebda8SAttilio Rao if (VM_OBJECT_SLEEP(object, &object->paging_in_progress, PSWP, 1256c7aebda8SAttilio Rao "swread", hz * 20)) { 12579bd86a98SBruce M Simpson printf( 1258c5690651SPoul-Henning Kamp "swap_pager: indefinite wait buffer: bufobj: %p, blkno: %jd, size: %ld\n", 1259c5690651SPoul-Henning Kamp bp->b_bufobj, (intmax_t)bp->b_blkno, bp->b_bcount); 12601c7c3c6aSMatthew Dillon } 12611b119d9dSDavid Greenman } 126226f9a767SRodney W. Grimes 126326f9a767SRodney W. Grimes /* 1264b0cd2017SGleb Smirnoff * If we had an unrecoverable read error pages will not be valid. 126526f9a767SRodney W. Grimes */ 1266915d1b71SMark Johnston for (i = 0; i < reqcount; i++) 12673f060b60SMark Johnston if (ma[i]->valid != VM_PAGE_BITS_ALL) 12681c7c3c6aSMatthew Dillon return (VM_PAGER_ERROR); 1269b0cd2017SGleb Smirnoff 12701c7c3c6aSMatthew Dillon return (VM_PAGER_OK); 12711c7c3c6aSMatthew Dillon 12721c7c3c6aSMatthew Dillon /* 12731c7c3c6aSMatthew Dillon * A final note: in a low swap situation, we cannot deallocate swap 12741c7c3c6aSMatthew Dillon * and mark a page dirty here because the caller is likely to mark 12751c7c3c6aSMatthew Dillon * the page clean when we return, causing the page to possibly revert 12761c7c3c6aSMatthew Dillon * to all-zero's later. 12771c7c3c6aSMatthew Dillon */ 1278df8bae1dSRodney W. Grimes } 1279df8bae1dSRodney W. Grimes 12801c7c3c6aSMatthew Dillon /* 128190effb23SGleb Smirnoff * swap_pager_getpages_async(): 128290effb23SGleb Smirnoff * 128390effb23SGleb Smirnoff * Right now this is emulation of asynchronous operation on top of 128490effb23SGleb Smirnoff * swap_pager_getpages(). 128590effb23SGleb Smirnoff */ 128690effb23SGleb Smirnoff static int 12873f060b60SMark Johnston swap_pager_getpages_async(vm_object_t object, vm_page_t *ma, int count, 1288b0cd2017SGleb Smirnoff int *rbehind, int *rahead, pgo_getpages_iodone_t iodone, void *arg) 128990effb23SGleb Smirnoff { 129090effb23SGleb Smirnoff int r, error; 129190effb23SGleb Smirnoff 12923f060b60SMark Johnston r = swap_pager_getpages(object, ma, count, rbehind, rahead); 129390effb23SGleb Smirnoff VM_OBJECT_WUNLOCK(object); 129490effb23SGleb Smirnoff switch (r) { 129590effb23SGleb Smirnoff case VM_PAGER_OK: 129690effb23SGleb Smirnoff error = 0; 129790effb23SGleb Smirnoff break; 129890effb23SGleb Smirnoff case VM_PAGER_ERROR: 129990effb23SGleb Smirnoff error = EIO; 130090effb23SGleb Smirnoff break; 130190effb23SGleb Smirnoff case VM_PAGER_FAIL: 130290effb23SGleb Smirnoff error = EINVAL; 130390effb23SGleb Smirnoff break; 130490effb23SGleb Smirnoff default: 1305d9328101SGleb Smirnoff panic("unhandled swap_pager_getpages() error %d", r); 130690effb23SGleb Smirnoff } 13073f060b60SMark Johnston (iodone)(arg, ma, count, error); 130890effb23SGleb Smirnoff VM_OBJECT_WLOCK(object); 130990effb23SGleb Smirnoff 131090effb23SGleb Smirnoff return (r); 131190effb23SGleb Smirnoff } 131290effb23SGleb Smirnoff 131390effb23SGleb Smirnoff /* 13141c7c3c6aSMatthew Dillon * swap_pager_putpages: 13151c7c3c6aSMatthew Dillon * 13161c7c3c6aSMatthew Dillon * Assign swap (if necessary) and initiate I/O on the specified pages. 13171c7c3c6aSMatthew Dillon * 13181c7c3c6aSMatthew Dillon * We support both OBJT_DEFAULT and OBJT_SWAP objects. DEFAULT objects 13191c7c3c6aSMatthew Dillon * are automatically converted to SWAP objects. 13201c7c3c6aSMatthew Dillon * 1321ea3aecf5SPeter Wemm * In a low memory situation we may block in VOP_STRATEGY(), but the new 13221c7c3c6aSMatthew Dillon * vm_page reservation system coupled with properly written VFS devices 13231c7c3c6aSMatthew Dillon * should ensure that no low-memory deadlock occurs. This is an area 13241c7c3c6aSMatthew Dillon * which needs work. 13251c7c3c6aSMatthew Dillon * 13261c7c3c6aSMatthew Dillon * The parent has N vm_object_pip_add() references prior to 13271c7c3c6aSMatthew Dillon * calling us and will remove references for rtvals[] that are 13281c7c3c6aSMatthew Dillon * not set to VM_PAGER_PEND. We need to remove the rest on I/O 13291c7c3c6aSMatthew Dillon * completion. 13301c7c3c6aSMatthew Dillon * 13311c7c3c6aSMatthew Dillon * The parent has soft-busy'd the pages it passes us and will unbusy 13321c7c3c6aSMatthew Dillon * those whos rtvals[] entry is not set to VM_PAGER_PEND on return. 13331c7c3c6aSMatthew Dillon * We need to unbusy the rest on I/O completion. 13341c7c3c6aSMatthew Dillon */ 1335d635a37fSWarner Losh static void 13363f060b60SMark Johnston swap_pager_putpages(vm_object_t object, vm_page_t *ma, int count, 1337e065e87cSKonstantin Belousov int flags, int *rtvals) 1338df8bae1dSRodney W. Grimes { 1339e065e87cSKonstantin Belousov int i, n; 1340e065e87cSKonstantin Belousov boolean_t sync; 134178f1deefSAlan Cox daddr_t addr, n_free, s_free; 1342df8bae1dSRodney W. Grimes 134378f1deefSAlan Cox swp_pager_init_freerange(&s_free, &n_free); 13443f060b60SMark Johnston if (count && ma[0]->object != object) { 13457036145bSMaxim Konovalov panic("swap_pager_putpages: object mismatch %p/%p", 13461c7c3c6aSMatthew Dillon object, 13473f060b60SMark Johnston ma[0]->object 13481c7c3c6aSMatthew Dillon ); 13491c7c3c6aSMatthew Dillon } 1350ee3dc7d7SAlan Cox 13511c7c3c6aSMatthew Dillon /* 13521c7c3c6aSMatthew Dillon * Step 1 13531c7c3c6aSMatthew Dillon * 13541c7c3c6aSMatthew Dillon * Turn object into OBJT_SWAP 13551c7c3c6aSMatthew Dillon * check for bogus sysops 13561c7c3c6aSMatthew Dillon * force sync if not pageout process 13571c7c3c6aSMatthew Dillon */ 135878f1deefSAlan Cox if (object->type != OBJT_SWAP) { 135978f1deefSAlan Cox addr = swp_pager_meta_build(object, 0, SWAPBLK_NONE); 136078f1deefSAlan Cox KASSERT(addr == SWAPBLK_NONE, 136178f1deefSAlan Cox ("unexpected object swap block")); 136278f1deefSAlan Cox } 136389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1364e47ed70bSJohn Dyson 1365e065e87cSKonstantin Belousov n = 0; 1366e47ed70bSJohn Dyson if (curproc != pageproc) 1367e47ed70bSJohn Dyson sync = TRUE; 1368e065e87cSKonstantin Belousov else 1369e065e87cSKonstantin Belousov sync = (flags & VM_PAGER_PUT_SYNC) != 0; 137026f9a767SRodney W. Grimes 13711c7c3c6aSMatthew Dillon /* 13721c7c3c6aSMatthew Dillon * Step 2 13731c7c3c6aSMatthew Dillon * 13741c7c3c6aSMatthew Dillon * Assign swap blocks and issue I/O. We reallocate swap on the fly. 13751c7c3c6aSMatthew Dillon * The page is left dirty until the pageout operation completes 13761c7c3c6aSMatthew Dillon * successfully. 13771c7c3c6aSMatthew Dillon */ 13781c7c3c6aSMatthew Dillon for (i = 0; i < count; i += n) { 13791c7c3c6aSMatthew Dillon int j; 13801c7c3c6aSMatthew Dillon struct buf *bp; 1381a316d390SJohn Dyson daddr_t blk; 138226f9a767SRodney W. Grimes 1383df8bae1dSRodney W. Grimes /* 13841c7c3c6aSMatthew Dillon * Maximum I/O size is limited by a number of factors. 1385df8bae1dSRodney W. Grimes */ 13861c7c3c6aSMatthew Dillon n = min(BLIST_MAX_ALLOC, count - i); 1387327f4e83SMatthew Dillon n = min(n, nsw_cluster_max); 13881c7c3c6aSMatthew Dillon 1389*48e98a2aSDoug Moore /* Get a block of swap of size up to size n. */ 1390*48e98a2aSDoug Moore blk = swp_pager_getswapspace(&n, 4); 13911c7c3c6aSMatthew Dillon if (blk == SWAPBLK_NONE) { 13924dcc5c2dSMatthew Dillon for (j = 0; j < n; ++j) 13931c7c3c6aSMatthew Dillon rtvals[i+j] = VM_PAGER_FAIL; 13941c7c3c6aSMatthew Dillon continue; 139526f9a767SRodney W. Grimes } 139626f9a767SRodney W. Grimes 139726f9a767SRodney W. Grimes /* 13981c7c3c6aSMatthew Dillon * All I/O parameters have been satisfied, build the I/O 13991c7c3c6aSMatthew Dillon * request and assign the swap space. 140026f9a767SRodney W. Grimes */ 1401756a5412SGleb Smirnoff if (sync != TRUE) { 1402756a5412SGleb Smirnoff mtx_lock(&swbuf_mtx); 1403756a5412SGleb Smirnoff while (nsw_wcount_async == 0) 1404756a5412SGleb Smirnoff msleep(&nsw_wcount_async, &swbuf_mtx, PVM, 1405756a5412SGleb Smirnoff "swbufa", 0); 1406756a5412SGleb Smirnoff nsw_wcount_async--; 1407756a5412SGleb Smirnoff mtx_unlock(&swbuf_mtx); 1408327f4e83SMatthew Dillon } 1409756a5412SGleb Smirnoff bp = uma_zalloc(swwbuf_zone, M_WAITOK); 1410756a5412SGleb Smirnoff if (sync != TRUE) 1411756a5412SGleb Smirnoff bp->b_flags = B_ASYNC; 14125e04322aSPoul-Henning Kamp bp->b_flags |= B_PAGING; 1413912e4ae9SPoul-Henning Kamp bp->b_iocmd = BIO_WRITE; 141426f9a767SRodney W. Grimes 1415fdcc1cc0SJohn Baldwin bp->b_rcred = crhold(thread0.td_ucred); 1416fdcc1cc0SJohn Baldwin bp->b_wcred = crhold(thread0.td_ucred); 14171c7c3c6aSMatthew Dillon bp->b_bcount = PAGE_SIZE * n; 14181c7c3c6aSMatthew Dillon bp->b_bufsize = PAGE_SIZE * n; 14191c7c3c6aSMatthew Dillon bp->b_blkno = blk; 1420e47ed70bSJohn Dyson 142189f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 14221c7c3c6aSMatthew Dillon for (j = 0; j < n; ++j) { 14233f060b60SMark Johnston vm_page_t mreq = ma[i+j]; 14241c7c3c6aSMatthew Dillon 142578f1deefSAlan Cox addr = swp_pager_meta_build(mreq->object, mreq->pindex, 142678f1deefSAlan Cox blk + j); 142778f1deefSAlan Cox if (addr != SWAPBLK_NONE) 142878f1deefSAlan Cox swp_pager_update_freerange(&s_free, &n_free, 142978f1deefSAlan Cox addr); 143087b0ab69SAlan Cox MPASS(mreq->dirty == VM_PAGE_BITS_ALL); 14315786be7cSAlan Cox mreq->oflags |= VPO_SWAPINPROG; 14321c7c3c6aSMatthew Dillon bp->b_pages[j] = mreq; 14331c7c3c6aSMatthew Dillon } 143489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 14351c7c3c6aSMatthew Dillon bp->b_npages = n; 1436a5296b05SJulian Elischer /* 1437a5296b05SJulian Elischer * Must set dirty range for NFS to work. 1438a5296b05SJulian Elischer */ 1439a5296b05SJulian Elischer bp->b_dirtyoff = 0; 1440a5296b05SJulian Elischer bp->b_dirtyend = bp->b_bcount; 14411c7c3c6aSMatthew Dillon 144283c9dea1SGleb Smirnoff VM_CNT_INC(v_swapout); 144383c9dea1SGleb Smirnoff VM_CNT_ADD(v_swappgsout, bp->b_npages); 144426f9a767SRodney W. Grimes 144526f9a767SRodney W. Grimes /* 144677923df2SAlan Cox * We unconditionally set rtvals[] to VM_PAGER_PEND so that we 144777923df2SAlan Cox * can call the async completion routine at the end of a 144877923df2SAlan Cox * synchronous I/O operation. Otherwise, our caller would 144977923df2SAlan Cox * perform duplicate unbusy and wakeup operations on the page 145077923df2SAlan Cox * and object, respectively. 145177923df2SAlan Cox */ 145277923df2SAlan Cox for (j = 0; j < n; j++) 145377923df2SAlan Cox rtvals[i + j] = VM_PAGER_PEND; 145477923df2SAlan Cox 145577923df2SAlan Cox /* 14561c7c3c6aSMatthew Dillon * asynchronous 14571c7c3c6aSMatthew Dillon * 1458c37a77eeSPoul-Henning Kamp * NOTE: b_blkno is destroyed by the call to swapdev_strategy 145926f9a767SRodney W. Grimes */ 14601c7c3c6aSMatthew Dillon if (sync == FALSE) { 14611c7c3c6aSMatthew Dillon bp->b_iodone = swp_pager_async_iodone; 146267812eacSKirk McKusick BUF_KERNPROC(bp); 14634b03903aSPoul-Henning Kamp swp_pager_strategy(bp); 14641c7c3c6aSMatthew Dillon continue; 146526f9a767SRodney W. Grimes } 1466e47ed70bSJohn Dyson 146726f9a767SRodney W. Grimes /* 14681c7c3c6aSMatthew Dillon * synchronous 14691c7c3c6aSMatthew Dillon * 1470c37a77eeSPoul-Henning Kamp * NOTE: b_blkno is destroyed by the call to swapdev_strategy 14711c7c3c6aSMatthew Dillon */ 14722c840b1fSAlan Cox bp->b_iodone = bdone; 14734b03903aSPoul-Henning Kamp swp_pager_strategy(bp); 14741c7c3c6aSMatthew Dillon 14751c7c3c6aSMatthew Dillon /* 147677923df2SAlan Cox * Wait for the sync I/O to complete. 147726f9a767SRodney W. Grimes */ 14782c840b1fSAlan Cox bwait(bp, PVM, "swwrt"); 147977923df2SAlan Cox 14801c7c3c6aSMatthew Dillon /* 14811c7c3c6aSMatthew Dillon * Now that we are through with the bp, we can call the 14821c7c3c6aSMatthew Dillon * normal async completion, which frees everything up. 14831c7c3c6aSMatthew Dillon */ 14841c7c3c6aSMatthew Dillon swp_pager_async_iodone(bp); 14851c7c3c6aSMatthew Dillon } 148689f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 148778f1deefSAlan Cox swp_pager_freeswapspace(s_free, n_free); 14881c7c3c6aSMatthew Dillon } 14891c7c3c6aSMatthew Dillon 14901c7c3c6aSMatthew Dillon /* 14911c7c3c6aSMatthew Dillon * swp_pager_async_iodone: 14921c7c3c6aSMatthew Dillon * 14931c7c3c6aSMatthew Dillon * Completion routine for asynchronous reads and writes from/to swap. 14941c7c3c6aSMatthew Dillon * Also called manually by synchronous code to finish up a bp. 14951c7c3c6aSMatthew Dillon * 149615523cf7SKonstantin Belousov * This routine may not sleep. 14971c7c3c6aSMatthew Dillon */ 14981c7c3c6aSMatthew Dillon static void 14992f249180SPoul-Henning Kamp swp_pager_async_iodone(struct buf *bp) 15001c7c3c6aSMatthew Dillon { 15011c7c3c6aSMatthew Dillon int i; 15021c7c3c6aSMatthew Dillon vm_object_t object = NULL; 15031c7c3c6aSMatthew Dillon 15041c7c3c6aSMatthew Dillon /* 15050b208315SEdward Tomasz Napierala * Report error - unless we ran out of memory, in which case 15060b208315SEdward Tomasz Napierala * we've already logged it in swapgeom_strategy(). 15071c7c3c6aSMatthew Dillon */ 15080b208315SEdward Tomasz Napierala if (bp->b_ioflags & BIO_ERROR && bp->b_error != ENOMEM) { 15091c7c3c6aSMatthew Dillon printf( 15101c7c3c6aSMatthew Dillon "swap_pager: I/O error - %s failed; blkno %ld," 15111c7c3c6aSMatthew Dillon "size %ld, error %d\n", 151221144e3bSPoul-Henning Kamp ((bp->b_iocmd == BIO_READ) ? "pagein" : "pageout"), 15131c7c3c6aSMatthew Dillon (long)bp->b_blkno, 15141c7c3c6aSMatthew Dillon (long)bp->b_bcount, 15151c7c3c6aSMatthew Dillon bp->b_error 15161c7c3c6aSMatthew Dillon ); 15171c7c3c6aSMatthew Dillon } 15181c7c3c6aSMatthew Dillon 15191c7c3c6aSMatthew Dillon /* 152026f9a767SRodney W. Grimes * remove the mapping for kernel virtual 152126f9a767SRodney W. Grimes */ 1522fade8dd7SJeff Roberson if (buf_mapped(bp)) 15231c7c3c6aSMatthew Dillon pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages); 1524fade8dd7SJeff Roberson else 1525fade8dd7SJeff Roberson bp->b_data = bp->b_kvabase; 152626f9a767SRodney W. Grimes 152733a609ecSAlan Cox if (bp->b_npages) { 152833a609ecSAlan Cox object = bp->b_pages[0]->object; 152989f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 153033a609ecSAlan Cox } 15312965a453SKip Macy 153226f9a767SRodney W. Grimes /* 15331c7c3c6aSMatthew Dillon * cleanup pages. If an error occurs writing to swap, we are in 15341c7c3c6aSMatthew Dillon * very serious trouble. If it happens to be a disk error, though, 15351c7c3c6aSMatthew Dillon * we may be able to recover by reassigning the swap later on. So 15361c7c3c6aSMatthew Dillon * in this case we remove the m->swapblk assignment for the page 15371c7c3c6aSMatthew Dillon * but do not free it in the rlist. The errornous block(s) are thus 15381c7c3c6aSMatthew Dillon * never reallocated as swap. Redirty the page and continue. 153926f9a767SRodney W. Grimes */ 15401c7c3c6aSMatthew Dillon for (i = 0; i < bp->b_npages; ++i) { 15411c7c3c6aSMatthew Dillon vm_page_t m = bp->b_pages[i]; 1542e47ed70bSJohn Dyson 15435786be7cSAlan Cox m->oflags &= ~VPO_SWAPINPROG; 1544c7aebda8SAttilio Rao if (m->oflags & VPO_SWAPSLEEP) { 1545c7aebda8SAttilio Rao m->oflags &= ~VPO_SWAPSLEEP; 1546c7aebda8SAttilio Rao wakeup(&object->paging_in_progress); 1547c7aebda8SAttilio Rao } 1548e47ed70bSJohn Dyson 1549c244d2deSPoul-Henning Kamp if (bp->b_ioflags & BIO_ERROR) { 1550ffc82b0aSJohn Dyson /* 15511c7c3c6aSMatthew Dillon * If an error occurs I'd love to throw the swapblk 15521c7c3c6aSMatthew Dillon * away without freeing it back to swapspace, so it 15531c7c3c6aSMatthew Dillon * can never be used again. But I can't from an 15541c7c3c6aSMatthew Dillon * interrupt. 1555ffc82b0aSJohn Dyson */ 155621144e3bSPoul-Henning Kamp if (bp->b_iocmd == BIO_READ) { 15571c7c3c6aSMatthew Dillon /* 15581c7c3c6aSMatthew Dillon * NOTE: for reads, m->dirty will probably 1559956f3135SPhilippe Charnier * be overridden by the original caller of 15601c7c3c6aSMatthew Dillon * getpages so don't play cute tricks here. 15611c7c3c6aSMatthew Dillon */ 15621c7c3c6aSMatthew Dillon m->valid = 0; 15631c7c3c6aSMatthew Dillon } else { 15641c7c3c6aSMatthew Dillon /* 15651c7c3c6aSMatthew Dillon * If a write error occurs, reactivate page 15661c7c3c6aSMatthew Dillon * so it doesn't clog the inactive list, 15671c7c3c6aSMatthew Dillon * then finish the I/O. 15681c7c3c6aSMatthew Dillon */ 156941e5a226SAlan Cox MPASS(m->dirty == VM_PAGE_BITS_ALL); 15703c4a2440SAlan Cox vm_page_lock(m); 15711c7c3c6aSMatthew Dillon vm_page_activate(m); 15723c4a2440SAlan Cox vm_page_unlock(m); 1573c7aebda8SAttilio Rao vm_page_sunbusy(m); 15741c7c3c6aSMatthew Dillon } 157521144e3bSPoul-Henning Kamp } else if (bp->b_iocmd == BIO_READ) { 15761c7c3c6aSMatthew Dillon /* 15771c7c3c6aSMatthew Dillon * NOTE: for reads, m->dirty will probably be 1578956f3135SPhilippe Charnier * overridden by the original caller of getpages so 15791c7c3c6aSMatthew Dillon * we cannot set them in order to free the underlying 15801c7c3c6aSMatthew Dillon * swap in a low-swap situation. I don't think we'd 15811c7c3c6aSMatthew Dillon * want to do that anyway, but it was an optimization 15821c7c3c6aSMatthew Dillon * that existed in the old swapper for a time before 15831c7c3c6aSMatthew Dillon * it got ripped out due to precisely this problem. 15841c7c3c6aSMatthew Dillon */ 1585016a3c93SAlan Cox KASSERT(!pmap_page_is_mapped(m), 1586016a3c93SAlan Cox ("swp_pager_async_iodone: page %p is mapped", m)); 1587016a3c93SAlan Cox KASSERT(m->dirty == 0, 1588016a3c93SAlan Cox ("swp_pager_async_iodone: page %p is dirty", m)); 1589915d1b71SMark Johnston 1590b0cd2017SGleb Smirnoff m->valid = VM_PAGE_BITS_ALL; 1591915d1b71SMark Johnston if (i < bp->b_pgbefore || 1592915d1b71SMark Johnston i >= bp->b_npages - bp->b_pgafter) 1593915d1b71SMark Johnston vm_page_readahead_finish(m); 15941c7c3c6aSMatthew Dillon } else { 15951c7c3c6aSMatthew Dillon /* 1596016a3c93SAlan Cox * For write success, clear the dirty 15971c7c3c6aSMatthew Dillon * status, then finish the I/O ( which decrements the 15981c7c3c6aSMatthew Dillon * busy count and possibly wakes waiter's up ). 1599ebcddc72SAlan Cox * A page is only written to swap after a period of 1600ebcddc72SAlan Cox * inactivity. Therefore, we do not expect it to be 1601ebcddc72SAlan Cox * reused. 16021c7c3c6aSMatthew Dillon */ 16036031c68dSAlan Cox KASSERT(!pmap_page_is_write_mapped(m), 1604016a3c93SAlan Cox ("swp_pager_async_iodone: page %p is not write" 1605016a3c93SAlan Cox " protected", m)); 1606c52e7044SAlan Cox vm_page_undirty(m); 16073c4a2440SAlan Cox vm_page_lock(m); 1608ebcddc72SAlan Cox vm_page_deactivate_noreuse(m); 16092965a453SKip Macy vm_page_unlock(m); 1610ebcddc72SAlan Cox vm_page_sunbusy(m); 16113c4a2440SAlan Cox } 16123c4a2440SAlan Cox } 161326f9a767SRodney W. Grimes 16141c7c3c6aSMatthew Dillon /* 16151c7c3c6aSMatthew Dillon * adjust pip. NOTE: the original parent may still have its own 16161c7c3c6aSMatthew Dillon * pip refs on the object. 16171c7c3c6aSMatthew Dillon */ 16180d420ad3SAlan Cox if (object != NULL) { 16191c7c3c6aSMatthew Dillon vm_object_pip_wakeupn(object, bp->b_npages); 162089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 16210d420ad3SAlan Cox } 162226f9a767SRodney W. Grimes 16231c7c3c6aSMatthew Dillon /* 1624100650deSOlivier Houchard * swapdev_strategy() manually sets b_vp and b_bufobj before calling 1625100650deSOlivier Houchard * bstrategy(). Set them back to NULL now we're done with it, or we'll 1626100650deSOlivier Houchard * trigger a KASSERT in relpbuf(). 1627100650deSOlivier Houchard */ 1628100650deSOlivier Houchard if (bp->b_vp) { 1629100650deSOlivier Houchard bp->b_vp = NULL; 1630100650deSOlivier Houchard bp->b_bufobj = NULL; 1631100650deSOlivier Houchard } 1632100650deSOlivier Houchard /* 16331c7c3c6aSMatthew Dillon * release the physical I/O buffer 16341c7c3c6aSMatthew Dillon */ 1635756a5412SGleb Smirnoff if (bp->b_flags & B_ASYNC) { 1636756a5412SGleb Smirnoff mtx_lock(&swbuf_mtx); 1637756a5412SGleb Smirnoff if (++nsw_wcount_async == 1) 1638756a5412SGleb Smirnoff wakeup(&nsw_wcount_async); 1639756a5412SGleb Smirnoff mtx_unlock(&swbuf_mtx); 1640756a5412SGleb Smirnoff } 1641756a5412SGleb Smirnoff uma_zfree((bp->b_iocmd == BIO_READ) ? swrbuf_zone : swwbuf_zone, bp); 164226f9a767SRodney W. Grimes } 16431c7c3c6aSMatthew Dillon 1644b1fd102eSMark Johnston int 1645b1fd102eSMark Johnston swap_pager_nswapdev(void) 1646b1fd102eSMark Johnston { 1647b1fd102eSMark Johnston 1648b1fd102eSMark Johnston return (nswapdev); 1649b1fd102eSMark Johnston } 1650b1fd102eSMark Johnston 165192da00bbSMatthew Dillon /* 165292da00bbSMatthew Dillon * SWP_PAGER_FORCE_PAGEIN() - force a swap block to be paged in 165392da00bbSMatthew Dillon * 1654ebcddc72SAlan Cox * This routine dissociates the page at the given index within an object 1655ebcddc72SAlan Cox * from its backing store, paging it in if it does not reside in memory. 1656ebcddc72SAlan Cox * If the page is paged in, it is marked dirty and placed in the laundry 1657ebcddc72SAlan Cox * queue. The page is marked dirty because it no longer has backing 1658ebcddc72SAlan Cox * store. It is placed in the laundry queue because it has not been 1659ebcddc72SAlan Cox * accessed recently. Otherwise, it would already reside in memory. 1660ebcddc72SAlan Cox * 1661ebcddc72SAlan Cox * We also attempt to swap in all other pages in the swap block. 1662ebcddc72SAlan Cox * However, we only guarantee that the one at the specified index is 166392da00bbSMatthew Dillon * paged in. 166492da00bbSMatthew Dillon * 166592da00bbSMatthew Dillon * XXX - The code to page the whole block in doesn't work, so we 166692da00bbSMatthew Dillon * revert to the one-by-one behavior for now. Sigh. 166792da00bbSMatthew Dillon */ 166862a59e8fSWarner Losh static inline void 1669b3fed13eSDavid Schultz swp_pager_force_pagein(vm_object_t object, vm_pindex_t pindex) 167092da00bbSMatthew Dillon { 167192da00bbSMatthew Dillon vm_page_t m; 167292da00bbSMatthew Dillon 167392da00bbSMatthew Dillon vm_object_pip_add(object, 1); 16745944de8eSKonstantin Belousov m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL); 167592da00bbSMatthew Dillon if (m->valid == VM_PAGE_BITS_ALL) { 16760d8243ccSAttilio Rao vm_object_pip_wakeup(object); 167792da00bbSMatthew Dillon vm_page_dirty(m); 167837244a84SAlan Cox #ifdef INVARIANTS 1679d061cdd5SAlan Cox vm_page_lock(m); 168037244a84SAlan Cox if (m->wire_count == 0 && m->queue == PQ_NONE) 168137244a84SAlan Cox panic("page %p is neither wired nor queued", m); 16822965a453SKip Macy vm_page_unlock(m); 168337244a84SAlan Cox #endif 1684c7aebda8SAttilio Rao vm_page_xunbusy(m); 168592da00bbSMatthew Dillon vm_pager_page_unswapped(m); 168692da00bbSMatthew Dillon return; 168792da00bbSMatthew Dillon } 168892da00bbSMatthew Dillon 1689b0cd2017SGleb Smirnoff if (swap_pager_getpages(object, &m, 1, NULL, NULL) != VM_PAGER_OK) 169092da00bbSMatthew Dillon panic("swap_pager_force_pagein: read from swap failed");/*XXX*/ 16910d8243ccSAttilio Rao vm_object_pip_wakeup(object); 169292da00bbSMatthew Dillon vm_page_dirty(m); 1693db1f085eSAlan Cox vm_page_lock(m); 1694ebcddc72SAlan Cox vm_page_launder(m); 16952965a453SKip Macy vm_page_unlock(m); 1696c7aebda8SAttilio Rao vm_page_xunbusy(m); 169792da00bbSMatthew Dillon vm_pager_page_unswapped(m); 169892da00bbSMatthew Dillon } 169992da00bbSMatthew Dillon 170092da00bbSMatthew Dillon /* 170192da00bbSMatthew Dillon * swap_pager_swapoff: 170292da00bbSMatthew Dillon * 170392da00bbSMatthew Dillon * Page in all of the pages that have been paged out to the 170492da00bbSMatthew Dillon * given device. The corresponding blocks in the bitmap must be 170592da00bbSMatthew Dillon * marked as allocated and the device must be flagged SW_CLOSING. 170692da00bbSMatthew Dillon * There may be no processes swapped out to the device. 170792da00bbSMatthew Dillon * 170892da00bbSMatthew Dillon * This routine may block. 170992da00bbSMatthew Dillon */ 1710e9c0cc15SPoul-Henning Kamp static void 1711b3fed13eSDavid Schultz swap_pager_swapoff(struct swdevt *sp) 171292da00bbSMatthew Dillon { 1713f425ab8eSKonstantin Belousov struct swblk *sb; 1714f425ab8eSKonstantin Belousov vm_object_t object; 1715f425ab8eSKonstantin Belousov vm_pindex_t pi; 1716f425ab8eSKonstantin Belousov int i, retries; 171792da00bbSMatthew Dillon 171804533e1eSKonstantin Belousov sx_assert(&swdev_syscall_lock, SA_XLOCKED); 171992da00bbSMatthew Dillon 17208bc61209SDavid Schultz retries = 0; 172192da00bbSMatthew Dillon full_rescan: 1722f425ab8eSKonstantin Belousov mtx_lock(&vm_object_list_mtx); 1723f425ab8eSKonstantin Belousov TAILQ_FOREACH(object, &vm_object_list, object_list) { 1724f425ab8eSKonstantin Belousov if (object->type != OBJT_SWAP) 172598150664SKonstantin Belousov continue; 1726f425ab8eSKonstantin Belousov mtx_unlock(&vm_object_list_mtx); 172798150664SKonstantin Belousov /* Depends on type-stability. */ 172898150664SKonstantin Belousov VM_OBJECT_WLOCK(object); 1729f425ab8eSKonstantin Belousov 1730f425ab8eSKonstantin Belousov /* 1731f425ab8eSKonstantin Belousov * Dead objects are eventually terminated on their own. 1732f425ab8eSKonstantin Belousov */ 1733f425ab8eSKonstantin Belousov if ((object->flags & OBJ_DEAD) != 0) 1734f425ab8eSKonstantin Belousov goto next_obj; 1735f425ab8eSKonstantin Belousov 1736f425ab8eSKonstantin Belousov /* 1737f425ab8eSKonstantin Belousov * Sync with fences placed after pctrie 1738f425ab8eSKonstantin Belousov * initialization. We must not access pctrie below 1739f425ab8eSKonstantin Belousov * unless we checked that our object is swap and not 1740f425ab8eSKonstantin Belousov * dead. 1741f425ab8eSKonstantin Belousov */ 1742f425ab8eSKonstantin Belousov atomic_thread_fence_acq(); 1743f425ab8eSKonstantin Belousov if (object->type != OBJT_SWAP) 1744f425ab8eSKonstantin Belousov goto next_obj; 1745f425ab8eSKonstantin Belousov 1746f425ab8eSKonstantin Belousov for (pi = 0; (sb = SWAP_PCTRIE_LOOKUP_GE( 1747f425ab8eSKonstantin Belousov &object->un_pager.swp.swp_blks, pi)) != NULL; ) { 1748f425ab8eSKonstantin Belousov pi = sb->p + SWAP_META_PAGES; 1749f425ab8eSKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) { 1750f425ab8eSKonstantin Belousov if (sb->d[i] == SWAPBLK_NONE) 1751f425ab8eSKonstantin Belousov continue; 1752f425ab8eSKonstantin Belousov if (swp_pager_isondev(sb->d[i], sp)) 1753f425ab8eSKonstantin Belousov swp_pager_force_pagein(object, 1754f425ab8eSKonstantin Belousov sb->p + i); 175598150664SKonstantin Belousov } 175698150664SKonstantin Belousov } 1757f425ab8eSKonstantin Belousov next_obj: 1758f425ab8eSKonstantin Belousov VM_OBJECT_WUNLOCK(object); 1759f425ab8eSKonstantin Belousov mtx_lock(&vm_object_list_mtx); 176092da00bbSMatthew Dillon } 1761f425ab8eSKonstantin Belousov mtx_unlock(&vm_object_list_mtx); 1762f425ab8eSKonstantin Belousov 17638bc61209SDavid Schultz if (sp->sw_used) { 176492da00bbSMatthew Dillon /* 17658bc61209SDavid Schultz * Objects may be locked or paging to the device being 17668bc61209SDavid Schultz * removed, so we will miss their pages and need to 17678bc61209SDavid Schultz * make another pass. We have marked this device as 17688bc61209SDavid Schultz * SW_CLOSING, so the activity should finish soon. 176992da00bbSMatthew Dillon */ 17708bc61209SDavid Schultz retries++; 17718bc61209SDavid Schultz if (retries > 100) { 17728bc61209SDavid Schultz panic("swapoff: failed to locate %d swap blocks", 17738bc61209SDavid Schultz sp->sw_used); 17748bc61209SDavid Schultz } 17754d70511aSJohn Baldwin pause("swpoff", hz / 20); 177692da00bbSMatthew Dillon goto full_rescan; 177792da00bbSMatthew Dillon } 1778b1fd102eSMark Johnston EVENTHANDLER_INVOKE(swapoff, sp); 177992da00bbSMatthew Dillon } 178092da00bbSMatthew Dillon 17811c7c3c6aSMatthew Dillon /************************************************************************ 17821c7c3c6aSMatthew Dillon * SWAP META DATA * 17831c7c3c6aSMatthew Dillon ************************************************************************ 17841c7c3c6aSMatthew Dillon * 17851c7c3c6aSMatthew Dillon * These routines manipulate the swap metadata stored in the 1786cec9f109SDavid E. O'Brien * OBJT_SWAP object. 17871c7c3c6aSMatthew Dillon * 17884dcc5c2dSMatthew Dillon * Swap metadata is implemented with a global hash and not directly 17894dcc5c2dSMatthew Dillon * linked into the object. Instead the object simply contains 17904dcc5c2dSMatthew Dillon * appropriate tracking counters. 17911c7c3c6aSMatthew Dillon */ 17921c7c3c6aSMatthew Dillon 17931c7c3c6aSMatthew Dillon /* 1794230869e0SAlan Cox * SWP_PAGER_SWBLK_EMPTY() - is a range of blocks free? 1795230869e0SAlan Cox */ 1796230869e0SAlan Cox static bool 1797230869e0SAlan Cox swp_pager_swblk_empty(struct swblk *sb, int start, int limit) 1798230869e0SAlan Cox { 1799230869e0SAlan Cox int i; 1800230869e0SAlan Cox 1801230869e0SAlan Cox MPASS(0 <= start && start <= limit && limit <= SWAP_META_PAGES); 1802230869e0SAlan Cox for (i = start; i < limit; i++) { 1803230869e0SAlan Cox if (sb->d[i] != SWAPBLK_NONE) 1804230869e0SAlan Cox return (false); 1805230869e0SAlan Cox } 1806230869e0SAlan Cox return (true); 1807230869e0SAlan Cox } 1808230869e0SAlan Cox 1809230869e0SAlan Cox /* 18101c7c3c6aSMatthew Dillon * SWP_PAGER_META_BUILD() - add swap block to swap meta data for object 18111c7c3c6aSMatthew Dillon * 18121c7c3c6aSMatthew Dillon * We first convert the object to a swap object if it is a default 18131c7c3c6aSMatthew Dillon * object. 18141c7c3c6aSMatthew Dillon * 18151c7c3c6aSMatthew Dillon * The specified swapblk is added to the object's swap metadata. If 18161c7c3c6aSMatthew Dillon * the swapblk is not valid, it is freed instead. Any previously 181778f1deefSAlan Cox * assigned swapblk is returned. 18181c7c3c6aSMatthew Dillon */ 181978f1deefSAlan Cox static daddr_t 18202f249180SPoul-Henning Kamp swp_pager_meta_build(vm_object_t object, vm_pindex_t pindex, daddr_t swapblk) 18212f249180SPoul-Henning Kamp { 1822f425ab8eSKonstantin Belousov static volatile int swblk_zone_exhausted, swpctrie_zone_exhausted; 1823eed99cb8SKonstantin Belousov struct swblk *sb, *sb1; 1824f425ab8eSKonstantin Belousov vm_pindex_t modpi, rdpi; 182578f1deefSAlan Cox daddr_t prev_swapblk; 1826f425ab8eSKonstantin Belousov int error, i; 18271c7c3c6aSMatthew Dillon 182889f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 1829f425ab8eSKonstantin Belousov 18301c7c3c6aSMatthew Dillon /* 18311c7c3c6aSMatthew Dillon * Convert default object to swap object if necessary 18321c7c3c6aSMatthew Dillon */ 18331c7c3c6aSMatthew Dillon if (object->type != OBJT_SWAP) { 1834f425ab8eSKonstantin Belousov pctrie_init(&object->un_pager.swp.swp_blks); 1835f425ab8eSKonstantin Belousov 1836f425ab8eSKonstantin Belousov /* 1837f425ab8eSKonstantin Belousov * Ensure that swap_pager_swapoff()'s iteration over 1838f425ab8eSKonstantin Belousov * object_list does not see a garbage pctrie. 1839f425ab8eSKonstantin Belousov */ 1840f425ab8eSKonstantin Belousov atomic_thread_fence_rel(); 1841f425ab8eSKonstantin Belousov 18421c7c3c6aSMatthew Dillon object->type = OBJT_SWAP; 1843eb4d6a1bSKonstantin Belousov KASSERT(object->handle == NULL, ("default pager with handle")); 1844bd228075SAlan Cox } 18451c7c3c6aSMatthew Dillon 1846f425ab8eSKonstantin Belousov rdpi = rounddown(pindex, SWAP_META_PAGES); 1847f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, rdpi); 1848f425ab8eSKonstantin Belousov if (sb == NULL) { 18491c7c3c6aSMatthew Dillon if (swapblk == SWAPBLK_NONE) 185078f1deefSAlan Cox return (SWAPBLK_NONE); 1851f425ab8eSKonstantin Belousov for (;;) { 1852f425ab8eSKonstantin Belousov sb = uma_zalloc(swblk_zone, M_NOWAIT | (curproc == 1853f425ab8eSKonstantin Belousov pageproc ? M_USE_RESERVE : 0)); 1854f425ab8eSKonstantin Belousov if (sb != NULL) { 1855f425ab8eSKonstantin Belousov sb->p = rdpi; 1856f425ab8eSKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) 1857f425ab8eSKonstantin Belousov sb->d[i] = SWAPBLK_NONE; 1858f425ab8eSKonstantin Belousov if (atomic_cmpset_int(&swblk_zone_exhausted, 1859f425ab8eSKonstantin Belousov 1, 0)) 1860f425ab8eSKonstantin Belousov printf("swblk zone ok\n"); 1861f425ab8eSKonstantin Belousov break; 1862f425ab8eSKonstantin Belousov } 186389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1864f425ab8eSKonstantin Belousov if (uma_zone_exhausted(swblk_zone)) { 1865f425ab8eSKonstantin Belousov if (atomic_cmpset_int(&swblk_zone_exhausted, 1866f425ab8eSKonstantin Belousov 0, 1)) 1867f425ab8eSKonstantin Belousov printf("swap blk zone exhausted, " 18683ff863f1SDag-Erling Smørgrav "increase kern.maxswzone\n"); 18692025d69bSKonstantin Belousov vm_pageout_oom(VM_OOM_SWAPZ); 1870f425ab8eSKonstantin Belousov pause("swzonxb", 10); 18712025d69bSKonstantin Belousov } else 18728d6fbbb8SJeff Roberson uma_zwait(swblk_zone); 187389f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1874eed99cb8SKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, 1875eed99cb8SKonstantin Belousov rdpi); 1876eed99cb8SKonstantin Belousov if (sb != NULL) 1877eed99cb8SKonstantin Belousov /* 1878eed99cb8SKonstantin Belousov * Somebody swapped out a nearby page, 1879eed99cb8SKonstantin Belousov * allocating swblk at the rdpi index, 1880eed99cb8SKonstantin Belousov * while we dropped the object lock. 1881eed99cb8SKonstantin Belousov */ 1882eed99cb8SKonstantin Belousov goto allocated; 18834dcc5c2dSMatthew Dillon } 1884f425ab8eSKonstantin Belousov for (;;) { 1885f425ab8eSKonstantin Belousov error = SWAP_PCTRIE_INSERT( 1886f425ab8eSKonstantin Belousov &object->un_pager.swp.swp_blks, sb); 1887f425ab8eSKonstantin Belousov if (error == 0) { 1888f425ab8eSKonstantin Belousov if (atomic_cmpset_int(&swpctrie_zone_exhausted, 1889f425ab8eSKonstantin Belousov 1, 0)) 1890f425ab8eSKonstantin Belousov printf("swpctrie zone ok\n"); 1891f425ab8eSKonstantin Belousov break; 18921c7c3c6aSMatthew Dillon } 1893f425ab8eSKonstantin Belousov VM_OBJECT_WUNLOCK(object); 1894f425ab8eSKonstantin Belousov if (uma_zone_exhausted(swpctrie_zone)) { 1895f425ab8eSKonstantin Belousov if (atomic_cmpset_int(&swpctrie_zone_exhausted, 1896f425ab8eSKonstantin Belousov 0, 1)) 1897f425ab8eSKonstantin Belousov printf("swap pctrie zone exhausted, " 1898f425ab8eSKonstantin Belousov "increase kern.maxswzone\n"); 1899f425ab8eSKonstantin Belousov vm_pageout_oom(VM_OOM_SWAPZ); 1900f425ab8eSKonstantin Belousov pause("swzonxp", 10); 1901f425ab8eSKonstantin Belousov } else 19028d6fbbb8SJeff Roberson uma_zwait(swpctrie_zone); 1903f425ab8eSKonstantin Belousov VM_OBJECT_WLOCK(object); 1904eed99cb8SKonstantin Belousov sb1 = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, 1905eed99cb8SKonstantin Belousov rdpi); 1906eed99cb8SKonstantin Belousov if (sb1 != NULL) { 1907eed99cb8SKonstantin Belousov uma_zfree(swblk_zone, sb); 1908eed99cb8SKonstantin Belousov sb = sb1; 1909eed99cb8SKonstantin Belousov goto allocated; 19101c7c3c6aSMatthew Dillon } 1911f425ab8eSKonstantin Belousov } 1912eed99cb8SKonstantin Belousov } 1913eed99cb8SKonstantin Belousov allocated: 1914f425ab8eSKonstantin Belousov MPASS(sb->p == rdpi); 19151c7c3c6aSMatthew Dillon 1916f425ab8eSKonstantin Belousov modpi = pindex % SWAP_META_PAGES; 191778f1deefSAlan Cox /* Return prior contents of metadata. */ 191878f1deefSAlan Cox prev_swapblk = sb->d[modpi]; 1919f425ab8eSKonstantin Belousov /* Enter block into metadata. */ 1920f425ab8eSKonstantin Belousov sb->d[modpi] = swapblk; 192185d88d87SKonstantin Belousov 192285d88d87SKonstantin Belousov /* 192385d88d87SKonstantin Belousov * Free the swblk if we end up with the empty page run. 192485d88d87SKonstantin Belousov */ 1925230869e0SAlan Cox if (swapblk == SWAPBLK_NONE && 1926230869e0SAlan Cox swp_pager_swblk_empty(sb, 0, SWAP_META_PAGES)) { 1927230869e0SAlan Cox SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, rdpi); 192885d88d87SKonstantin Belousov uma_zfree(swblk_zone, sb); 192985d88d87SKonstantin Belousov } 193078f1deefSAlan Cox return (prev_swapblk); 193185d88d87SKonstantin Belousov } 19321c7c3c6aSMatthew Dillon 19331c7c3c6aSMatthew Dillon /* 19341c7c3c6aSMatthew Dillon * SWP_PAGER_META_FREE() - free a range of blocks in the object's swap metadata 19351c7c3c6aSMatthew Dillon * 19361c7c3c6aSMatthew Dillon * The requested range of blocks is freed, with any associated swap 19371c7c3c6aSMatthew Dillon * returned to the swap bitmap. 19381c7c3c6aSMatthew Dillon * 19391c7c3c6aSMatthew Dillon * This routine will free swap metadata structures as they are cleaned 19401c7c3c6aSMatthew Dillon * out. This routine does *NOT* operate on swap metadata associated 19411c7c3c6aSMatthew Dillon * with resident pages. 19421c7c3c6aSMatthew Dillon */ 19431c7c3c6aSMatthew Dillon static void 1944f425ab8eSKonstantin Belousov swp_pager_meta_free(vm_object_t object, vm_pindex_t pindex, vm_pindex_t count) 19451c7c3c6aSMatthew Dillon { 1946f425ab8eSKonstantin Belousov struct swblk *sb; 194778f1deefSAlan Cox daddr_t n_free, s_free; 1948f425ab8eSKonstantin Belousov vm_pindex_t last; 1949230869e0SAlan Cox int i, limit, start; 19502928cef7SAlan Cox 1951ee620ea4SAlan Cox VM_OBJECT_ASSERT_WLOCKED(object); 19522e56b64fSKonstantin Belousov if (object->type != OBJT_SWAP || count == 0) 19531c7c3c6aSMatthew Dillon return; 19541c7c3c6aSMatthew Dillon 195578f1deefSAlan Cox swp_pager_init_freerange(&s_free, &n_free); 1956230869e0SAlan Cox last = pindex + count; 1957f425ab8eSKonstantin Belousov for (;;) { 1958f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks, 1959f425ab8eSKonstantin Belousov rounddown(pindex, SWAP_META_PAGES)); 1960230869e0SAlan Cox if (sb == NULL || sb->p >= last) 19612e56b64fSKonstantin Belousov break; 1962230869e0SAlan Cox start = pindex > sb->p ? pindex - sb->p : 0; 1963230869e0SAlan Cox limit = last - sb->p < SWAP_META_PAGES ? last - sb->p : 1964230869e0SAlan Cox SWAP_META_PAGES; 1965230869e0SAlan Cox for (i = start; i < limit; i++) { 1966f425ab8eSKonstantin Belousov if (sb->d[i] == SWAPBLK_NONE) 1967f425ab8eSKonstantin Belousov continue; 196878f1deefSAlan Cox swp_pager_update_freerange(&s_free, &n_free, sb->d[i]); 1969230869e0SAlan Cox sb->d[i] = SWAPBLK_NONE; 1970230869e0SAlan Cox } 1971150d384eSMark Johnston pindex = sb->p + SWAP_META_PAGES; 1972230869e0SAlan Cox if (swp_pager_swblk_empty(sb, 0, start) && 1973230869e0SAlan Cox swp_pager_swblk_empty(sb, limit, SWAP_META_PAGES)) { 1974f425ab8eSKonstantin Belousov SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, 1975f425ab8eSKonstantin Belousov sb->p); 1976f425ab8eSKonstantin Belousov uma_zfree(swblk_zone, sb); 19771c7c3c6aSMatthew Dillon } 19781c7c3c6aSMatthew Dillon } 197978f1deefSAlan Cox swp_pager_freeswapspace(s_free, n_free); 19801c7c3c6aSMatthew Dillon } 19811c7c3c6aSMatthew Dillon 19821c7c3c6aSMatthew Dillon /* 19831c7c3c6aSMatthew Dillon * SWP_PAGER_META_FREE_ALL() - destroy all swap metadata associated with object 19841c7c3c6aSMatthew Dillon * 19851c7c3c6aSMatthew Dillon * This routine locates and destroys all swap metadata associated with 19861c7c3c6aSMatthew Dillon * an object. 19871c7c3c6aSMatthew Dillon */ 19881c7c3c6aSMatthew Dillon static void 19891c7c3c6aSMatthew Dillon swp_pager_meta_free_all(vm_object_t object) 19901c7c3c6aSMatthew Dillon { 1991f425ab8eSKonstantin Belousov struct swblk *sb; 199278f1deefSAlan Cox daddr_t n_free, s_free; 1993f425ab8eSKonstantin Belousov vm_pindex_t pindex; 199471057cd2SKonstantin Belousov int i; 19951c7c3c6aSMatthew Dillon 199689f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 19971c7c3c6aSMatthew Dillon if (object->type != OBJT_SWAP) 19981c7c3c6aSMatthew Dillon return; 19991c7c3c6aSMatthew Dillon 200078f1deefSAlan Cox swp_pager_init_freerange(&s_free, &n_free); 2001f425ab8eSKonstantin Belousov for (pindex = 0; (sb = SWAP_PCTRIE_LOOKUP_GE( 2002f425ab8eSKonstantin Belousov &object->un_pager.swp.swp_blks, pindex)) != NULL;) { 2003f425ab8eSKonstantin Belousov pindex = sb->p + SWAP_META_PAGES; 2004f425ab8eSKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) { 2005230869e0SAlan Cox if (sb->d[i] == SWAPBLK_NONE) 2006230869e0SAlan Cox continue; 200778f1deefSAlan Cox swp_pager_update_freerange(&s_free, &n_free, sb->d[i]); 20081c7c3c6aSMatthew Dillon } 2009f425ab8eSKonstantin Belousov SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, sb->p); 2010f425ab8eSKonstantin Belousov uma_zfree(swblk_zone, sb); 20111c7c3c6aSMatthew Dillon } 201278f1deefSAlan Cox swp_pager_freeswapspace(s_free, n_free); 20131c7c3c6aSMatthew Dillon } 20141c7c3c6aSMatthew Dillon 20151c7c3c6aSMatthew Dillon /* 20164abca9bbSAlan Cox * SWP_PAGER_METACTL() - misc control of swap meta data. 20171c7c3c6aSMatthew Dillon * 20184abca9bbSAlan Cox * This routine is capable of looking up, or removing swapblk 20194abca9bbSAlan Cox * assignments in the swap meta data. It returns the swapblk being 20204abca9bbSAlan Cox * looked-up, popped, or SWAPBLK_NONE if the block was invalid. 20211c7c3c6aSMatthew Dillon * 20221c7c3c6aSMatthew Dillon * When acting on a busy resident page and paging is in progress, we 20231c7c3c6aSMatthew Dillon * have to wait until paging is complete but otherwise can act on the 20241c7c3c6aSMatthew Dillon * busy page. 20251c7c3c6aSMatthew Dillon * 20264abca9bbSAlan Cox * SWM_POP remove from meta data but do not free it 20271c7c3c6aSMatthew Dillon */ 20281c7c3c6aSMatthew Dillon static daddr_t 20292f249180SPoul-Henning Kamp swp_pager_meta_ctl(vm_object_t object, vm_pindex_t pindex, int flags) 20302f249180SPoul-Henning Kamp { 2031f425ab8eSKonstantin Belousov struct swblk *sb; 20324dcc5c2dSMatthew Dillon daddr_t r1; 20334dcc5c2dSMatthew Dillon 20344abca9bbSAlan Cox if ((flags & SWM_POP) != 0) 2035ee620ea4SAlan Cox VM_OBJECT_ASSERT_WLOCKED(object); 2036ee620ea4SAlan Cox else 2037c25673ffSAttilio Rao VM_OBJECT_ASSERT_LOCKED(object); 2038ee620ea4SAlan Cox 20391c7c3c6aSMatthew Dillon /* 2040ee620ea4SAlan Cox * The meta data only exists if the object is OBJT_SWAP 20411c7c3c6aSMatthew Dillon * and even then might not be allocated yet. 20421c7c3c6aSMatthew Dillon */ 20434dcc5c2dSMatthew Dillon if (object->type != OBJT_SWAP) 20441c7c3c6aSMatthew Dillon return (SWAPBLK_NONE); 20451c7c3c6aSMatthew Dillon 2046f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, 2047f425ab8eSKonstantin Belousov rounddown(pindex, SWAP_META_PAGES)); 2048f425ab8eSKonstantin Belousov if (sb == NULL) 2049f425ab8eSKonstantin Belousov return (SWAPBLK_NONE); 2050f425ab8eSKonstantin Belousov r1 = sb->d[pindex % SWAP_META_PAGES]; 2051f425ab8eSKonstantin Belousov if (r1 == SWAPBLK_NONE) 2052f425ab8eSKonstantin Belousov return (SWAPBLK_NONE); 20534abca9bbSAlan Cox if ((flags & SWM_POP) != 0) { 2054f425ab8eSKonstantin Belousov sb->d[pindex % SWAP_META_PAGES] = SWAPBLK_NONE; 2055230869e0SAlan Cox if (swp_pager_swblk_empty(sb, 0, SWAP_META_PAGES)) { 2056f425ab8eSKonstantin Belousov SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, 2057f425ab8eSKonstantin Belousov rounddown(pindex, SWAP_META_PAGES)); 2058f425ab8eSKonstantin Belousov uma_zfree(swblk_zone, sb); 2059f425ab8eSKonstantin Belousov } 2060f425ab8eSKonstantin Belousov } 20611c7c3c6aSMatthew Dillon return (r1); 20621c7c3c6aSMatthew Dillon } 20631c7c3c6aSMatthew Dillon 2064e9c0cc15SPoul-Henning Kamp /* 206577d6fd97SKonstantin Belousov * Returns the least page index which is greater than or equal to the 206677d6fd97SKonstantin Belousov * parameter pindex and for which there is a swap block allocated. 206777d6fd97SKonstantin Belousov * Returns object's size if the object's type is not swap or if there 206877d6fd97SKonstantin Belousov * are no allocated swap blocks for the object after the requested 206977d6fd97SKonstantin Belousov * pindex. 207077d6fd97SKonstantin Belousov */ 207177d6fd97SKonstantin Belousov vm_pindex_t 207277d6fd97SKonstantin Belousov swap_pager_find_least(vm_object_t object, vm_pindex_t pindex) 207377d6fd97SKonstantin Belousov { 2074f425ab8eSKonstantin Belousov struct swblk *sb; 2075f425ab8eSKonstantin Belousov int i; 207677d6fd97SKonstantin Belousov 207777d6fd97SKonstantin Belousov VM_OBJECT_ASSERT_LOCKED(object); 2078f425ab8eSKonstantin Belousov if (object->type != OBJT_SWAP) 207977d6fd97SKonstantin Belousov return (object->size); 208077d6fd97SKonstantin Belousov 2081f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks, 2082f425ab8eSKonstantin Belousov rounddown(pindex, SWAP_META_PAGES)); 2083f425ab8eSKonstantin Belousov if (sb == NULL) 2084f425ab8eSKonstantin Belousov return (object->size); 2085f425ab8eSKonstantin Belousov if (sb->p < pindex) { 2086f425ab8eSKonstantin Belousov for (i = pindex % SWAP_META_PAGES; i < SWAP_META_PAGES; i++) { 2087f425ab8eSKonstantin Belousov if (sb->d[i] != SWAPBLK_NONE) 2088f425ab8eSKonstantin Belousov return (sb->p + i); 208977d6fd97SKonstantin Belousov } 2090f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks, 2091f425ab8eSKonstantin Belousov roundup(pindex, SWAP_META_PAGES)); 2092f425ab8eSKonstantin Belousov if (sb == NULL) 2093f425ab8eSKonstantin Belousov return (object->size); 209477d6fd97SKonstantin Belousov } 2095f425ab8eSKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) { 2096f425ab8eSKonstantin Belousov if (sb->d[i] != SWAPBLK_NONE) 2097f425ab8eSKonstantin Belousov return (sb->p + i); 209877d6fd97SKonstantin Belousov } 2099f425ab8eSKonstantin Belousov 2100f425ab8eSKonstantin Belousov /* 2101f425ab8eSKonstantin Belousov * We get here if a swblk is present in the trie but it 2102f425ab8eSKonstantin Belousov * doesn't map any blocks. 2103f425ab8eSKonstantin Belousov */ 2104f425ab8eSKonstantin Belousov MPASS(0); 2105f425ab8eSKonstantin Belousov return (object->size); 210677d6fd97SKonstantin Belousov } 210777d6fd97SKonstantin Belousov 210877d6fd97SKonstantin Belousov /* 2109e9c0cc15SPoul-Henning Kamp * System call swapon(name) enables swapping on device name, 2110e9c0cc15SPoul-Henning Kamp * which must be in the swdevsw. Return EBUSY 2111e9c0cc15SPoul-Henning Kamp * if already swapping on this device. 2112e9c0cc15SPoul-Henning Kamp */ 2113e9c0cc15SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_ 2114e9c0cc15SPoul-Henning Kamp struct swapon_args { 2115e9c0cc15SPoul-Henning Kamp char *name; 2116e9c0cc15SPoul-Henning Kamp }; 2117e9c0cc15SPoul-Henning Kamp #endif 2118e9c0cc15SPoul-Henning Kamp 2119e9c0cc15SPoul-Henning Kamp /* 2120e9c0cc15SPoul-Henning Kamp * MPSAFE 2121e9c0cc15SPoul-Henning Kamp */ 2122e9c0cc15SPoul-Henning Kamp /* ARGSUSED */ 2123e9c0cc15SPoul-Henning Kamp int 21248451d0ddSKip Macy sys_swapon(struct thread *td, struct swapon_args *uap) 2125e9c0cc15SPoul-Henning Kamp { 2126e9c0cc15SPoul-Henning Kamp struct vattr attr; 2127e9c0cc15SPoul-Henning Kamp struct vnode *vp; 2128e9c0cc15SPoul-Henning Kamp struct nameidata nd; 2129e9c0cc15SPoul-Henning Kamp int error; 2130e9c0cc15SPoul-Henning Kamp 2131acd3428bSRobert Watson error = priv_check(td, PRIV_SWAPON); 2132e9c0cc15SPoul-Henning Kamp if (error) 2133acd3428bSRobert Watson return (error); 2134e9c0cc15SPoul-Henning Kamp 213504533e1eSKonstantin Belousov sx_xlock(&swdev_syscall_lock); 2136e9c0cc15SPoul-Henning Kamp 2137e9c0cc15SPoul-Henning Kamp /* 2138e9c0cc15SPoul-Henning Kamp * Swap metadata may not fit in the KVM if we have physical 2139e9c0cc15SPoul-Henning Kamp * memory of >1GB. 2140e9c0cc15SPoul-Henning Kamp */ 2141f425ab8eSKonstantin Belousov if (swblk_zone == NULL) { 2142e9c0cc15SPoul-Henning Kamp error = ENOMEM; 2143e9c0cc15SPoul-Henning Kamp goto done; 2144e9c0cc15SPoul-Henning Kamp } 2145e9c0cc15SPoul-Henning Kamp 2146d9135e72SRobert Watson NDINIT(&nd, LOOKUP, ISOPEN | FOLLOW | AUDITVNODE1, UIO_USERSPACE, 2147d9135e72SRobert Watson uap->name, td); 2148e9c0cc15SPoul-Henning Kamp error = namei(&nd); 2149e9c0cc15SPoul-Henning Kamp if (error) 2150e9c0cc15SPoul-Henning Kamp goto done; 2151e9c0cc15SPoul-Henning Kamp 2152e9c0cc15SPoul-Henning Kamp NDFREE(&nd, NDF_ONLY_PNBUF); 2153e9c0cc15SPoul-Henning Kamp vp = nd.ni_vp; 2154e9c0cc15SPoul-Henning Kamp 215520da9c2eSPoul-Henning Kamp if (vn_isdisk(vp, &error)) { 215688ad2d7bSKonstantin Belousov error = swapongeom(vp); 215720da9c2eSPoul-Henning Kamp } else if (vp->v_type == VREG && 2158e9c0cc15SPoul-Henning Kamp (vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 && 21590359a12eSAttilio Rao (error = VOP_GETATTR(vp, &attr, td->td_ucred)) == 0) { 2160e9c0cc15SPoul-Henning Kamp /* 2161e9c0cc15SPoul-Henning Kamp * Allow direct swapping to NFS regular files in the same 2162e9c0cc15SPoul-Henning Kamp * way that nfs_mountroot() sets up diskless swapping. 2163e9c0cc15SPoul-Henning Kamp */ 216459efee01SPoul-Henning Kamp error = swaponvp(td, vp, attr.va_size / DEV_BSIZE); 2165e9c0cc15SPoul-Henning Kamp } 2166e9c0cc15SPoul-Henning Kamp 2167e9c0cc15SPoul-Henning Kamp if (error) 2168e9c0cc15SPoul-Henning Kamp vrele(vp); 2169e9c0cc15SPoul-Henning Kamp done: 217004533e1eSKonstantin Belousov sx_xunlock(&swdev_syscall_lock); 2171e9c0cc15SPoul-Henning Kamp return (error); 2172e9c0cc15SPoul-Henning Kamp } 2173e9c0cc15SPoul-Henning Kamp 21743ff863f1SDag-Erling Smørgrav /* 21753ff863f1SDag-Erling Smørgrav * Check that the total amount of swap currently configured does not 21763ff863f1SDag-Erling Smørgrav * exceed half the theoretical maximum. If it does, print a warning 217735872e79SKonstantin Belousov * message. 21783ff863f1SDag-Erling Smørgrav */ 217935872e79SKonstantin Belousov static void 218035872e79SKonstantin Belousov swapon_check_swzone(void) 21813ff863f1SDag-Erling Smørgrav { 218235872e79SKonstantin Belousov unsigned long maxpages, npages; 21833ff863f1SDag-Erling Smørgrav 2184e8bb589dSMatt Macy npages = swap_total; 21853ff863f1SDag-Erling Smørgrav /* absolute maximum we can handle assuming 100% efficiency */ 2186f425ab8eSKonstantin Belousov maxpages = uma_zone_get_max(swblk_zone) * SWAP_META_PAGES; 21873ff863f1SDag-Erling Smørgrav 21883ff863f1SDag-Erling Smørgrav /* recommend using no more than half that amount */ 21893ff863f1SDag-Erling Smørgrav if (npages > maxpages / 2) { 21903ff863f1SDag-Erling Smørgrav printf("warning: total configured swap (%lu pages) " 21913ff863f1SDag-Erling Smørgrav "exceeds maximum recommended amount (%lu pages).\n", 21929462305cSSergey Kandaurov npages, maxpages / 2); 21933ff863f1SDag-Erling Smørgrav printf("warning: increase kern.maxswzone " 21943ff863f1SDag-Erling Smørgrav "or reduce amount of swap.\n"); 21953ff863f1SDag-Erling Smørgrav } 21963ff863f1SDag-Erling Smørgrav } 21973ff863f1SDag-Erling Smørgrav 219859efee01SPoul-Henning Kamp static void 21992cc718a1SKonstantin Belousov swaponsomething(struct vnode *vp, void *id, u_long nblks, 22002cc718a1SKonstantin Belousov sw_strategy_t *strategy, sw_close_t *close, dev_t dev, int flags) 2201e9c0cc15SPoul-Henning Kamp { 22022d9974c1SAlan Cox struct swdevt *sp, *tsp; 2203e9c0cc15SPoul-Henning Kamp swblk_t dvbase; 22048f60c087SPoul-Henning Kamp u_long mblocks; 2205e9c0cc15SPoul-Henning Kamp 2206e9c0cc15SPoul-Henning Kamp /* 2207e9c0cc15SPoul-Henning Kamp * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks. 2208e9c0cc15SPoul-Henning Kamp * First chop nblks off to page-align it, then convert. 2209e9c0cc15SPoul-Henning Kamp * 2210e9c0cc15SPoul-Henning Kamp * sw->sw_nblks is in page-sized chunks now too. 2211e9c0cc15SPoul-Henning Kamp */ 2212e9c0cc15SPoul-Henning Kamp nblks &= ~(ctodb(1) - 1); 2213e9c0cc15SPoul-Henning Kamp nblks = dbtoc(nblks); 2214e9c0cc15SPoul-Henning Kamp 22156e903bd0SKonstantin Belousov /* 22166e903bd0SKonstantin Belousov * If we go beyond this, we get overflows in the radix 22176e903bd0SKonstantin Belousov * tree bitmap code. 22186e903bd0SKonstantin Belousov */ 22196e903bd0SKonstantin Belousov mblocks = 0x40000000 / BLIST_META_RADIX; 22206e903bd0SKonstantin Belousov if (nblks > mblocks) { 22216e903bd0SKonstantin Belousov printf( 22226e903bd0SKonstantin Belousov "WARNING: reducing swap size to maximum of %luMB per unit\n", 22236e903bd0SKonstantin Belousov mblocks / 1024 / 1024 * PAGE_SIZE); 22246e903bd0SKonstantin Belousov nblks = mblocks; 22256e903bd0SKonstantin Belousov } 22266e903bd0SKonstantin Belousov 22278f60c087SPoul-Henning Kamp sp = malloc(sizeof *sp, M_VMPGDATA, M_WAITOK | M_ZERO); 2228dee34ca4SPoul-Henning Kamp sp->sw_vp = vp; 2229dee34ca4SPoul-Henning Kamp sp->sw_id = id; 2230f3732fd1SPoul-Henning Kamp sp->sw_dev = dev; 22318d677ef9SPoul-Henning Kamp sp->sw_flags = 0; 2232e9c0cc15SPoul-Henning Kamp sp->sw_nblks = nblks; 2233e9c0cc15SPoul-Henning Kamp sp->sw_used = 0; 223459efee01SPoul-Henning Kamp sp->sw_strategy = strategy; 2235dee34ca4SPoul-Henning Kamp sp->sw_close = close; 22362cc718a1SKonstantin Belousov sp->sw_flags = flags; 2237e9c0cc15SPoul-Henning Kamp 2238c8c7ad92SKip Macy sp->sw_blist = blist_create(nblks, M_WAITOK); 2239e9c0cc15SPoul-Henning Kamp /* 2240ef3c5abdSPoul-Henning Kamp * Do not free the first two block in order to avoid overwriting 22418f60c087SPoul-Henning Kamp * any bsd label at the front of the partition 2242e9c0cc15SPoul-Henning Kamp */ 2243ef3c5abdSPoul-Henning Kamp blist_free(sp->sw_blist, 2, nblks - 2); 2244e9c0cc15SPoul-Henning Kamp 22452d9974c1SAlan Cox dvbase = 0; 224620da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 22472d9974c1SAlan Cox TAILQ_FOREACH(tsp, &swtailq, sw_list) { 22482d9974c1SAlan Cox if (tsp->sw_end >= dvbase) { 22492d9974c1SAlan Cox /* 22502d9974c1SAlan Cox * We put one uncovered page between the devices 22512d9974c1SAlan Cox * in order to definitively prevent any cross-device 22522d9974c1SAlan Cox * I/O requests 22532d9974c1SAlan Cox */ 22542d9974c1SAlan Cox dvbase = tsp->sw_end + 1; 22552d9974c1SAlan Cox } 22562d9974c1SAlan Cox } 22572d9974c1SAlan Cox sp->sw_first = dvbase; 22582d9974c1SAlan Cox sp->sw_end = dvbase + nblks; 22598f60c087SPoul-Henning Kamp TAILQ_INSERT_TAIL(&swtailq, sp, sw_list); 22608f60c087SPoul-Henning Kamp nswapdev++; 2261761097c8SAlan Cox swap_pager_avail += nblks - 2; 2262e8bb589dSMatt Macy swap_total += nblks; 226335872e79SKonstantin Belousov swapon_check_swzone(); 2264d05bc129SAlan Cox swp_sizecheck(); 2265d05bc129SAlan Cox mtx_unlock(&sw_dev_mtx); 2266b1fd102eSMark Johnston EVENTHANDLER_INVOKE(swapon, sp); 226759efee01SPoul-Henning Kamp } 2268e9c0cc15SPoul-Henning Kamp 2269e9c0cc15SPoul-Henning Kamp /* 2270e9c0cc15SPoul-Henning Kamp * SYSCALL: swapoff(devname) 2271e9c0cc15SPoul-Henning Kamp * 2272e9c0cc15SPoul-Henning Kamp * Disable swapping on the given device. 2273dee34ca4SPoul-Henning Kamp * 2274dee34ca4SPoul-Henning Kamp * XXX: Badly designed system call: it should use a device index 2275dee34ca4SPoul-Henning Kamp * rather than filename as specification. We keep sw_vp around 2276dee34ca4SPoul-Henning Kamp * only to make this work. 2277e9c0cc15SPoul-Henning Kamp */ 2278e9c0cc15SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_ 2279e9c0cc15SPoul-Henning Kamp struct swapoff_args { 2280e9c0cc15SPoul-Henning Kamp char *name; 2281e9c0cc15SPoul-Henning Kamp }; 2282e9c0cc15SPoul-Henning Kamp #endif 2283e9c0cc15SPoul-Henning Kamp 2284e9c0cc15SPoul-Henning Kamp /* 2285e9c0cc15SPoul-Henning Kamp * MPSAFE 2286e9c0cc15SPoul-Henning Kamp */ 2287e9c0cc15SPoul-Henning Kamp /* ARGSUSED */ 2288e9c0cc15SPoul-Henning Kamp int 22898451d0ddSKip Macy sys_swapoff(struct thread *td, struct swapoff_args *uap) 2290e9c0cc15SPoul-Henning Kamp { 2291e9c0cc15SPoul-Henning Kamp struct vnode *vp; 2292e9c0cc15SPoul-Henning Kamp struct nameidata nd; 2293e9c0cc15SPoul-Henning Kamp struct swdevt *sp; 22948f60c087SPoul-Henning Kamp int error; 2295e9c0cc15SPoul-Henning Kamp 2296acd3428bSRobert Watson error = priv_check(td, PRIV_SWAPOFF); 2297e9c0cc15SPoul-Henning Kamp if (error) 22980909f38aSPawel Jakub Dawidek return (error); 2299e9c0cc15SPoul-Henning Kamp 230004533e1eSKonstantin Belousov sx_xlock(&swdev_syscall_lock); 2301e9c0cc15SPoul-Henning Kamp 2302d9135e72SRobert Watson NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, UIO_USERSPACE, uap->name, 2303d9135e72SRobert Watson td); 2304e9c0cc15SPoul-Henning Kamp error = namei(&nd); 2305e9c0cc15SPoul-Henning Kamp if (error) 2306e9c0cc15SPoul-Henning Kamp goto done; 2307e9c0cc15SPoul-Henning Kamp NDFREE(&nd, NDF_ONLY_PNBUF); 2308e9c0cc15SPoul-Henning Kamp vp = nd.ni_vp; 2309e9c0cc15SPoul-Henning Kamp 231020da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 23118f60c087SPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 2312dee34ca4SPoul-Henning Kamp if (sp->sw_vp == vp) 23130909f38aSPawel Jakub Dawidek break; 2314e9c0cc15SPoul-Henning Kamp } 231520da9c2eSPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 23160909f38aSPawel Jakub Dawidek if (sp == NULL) { 2317e9c0cc15SPoul-Henning Kamp error = EINVAL; 2318e9c0cc15SPoul-Henning Kamp goto done; 23190909f38aSPawel Jakub Dawidek } 232035918c55SChristian S.J. Peron error = swapoff_one(sp, td->td_ucred); 23210909f38aSPawel Jakub Dawidek done: 232204533e1eSKonstantin Belousov sx_xunlock(&swdev_syscall_lock); 23230909f38aSPawel Jakub Dawidek return (error); 23240909f38aSPawel Jakub Dawidek } 23250909f38aSPawel Jakub Dawidek 23260909f38aSPawel Jakub Dawidek static int 232735918c55SChristian S.J. Peron swapoff_one(struct swdevt *sp, struct ucred *cred) 23280909f38aSPawel Jakub Dawidek { 232903bdd65fSAlan Cox u_long nblks; 2330e9c0cc15SPoul-Henning Kamp #ifdef MAC 23310909f38aSPawel Jakub Dawidek int error; 2332e9c0cc15SPoul-Henning Kamp #endif 2333e9c0cc15SPoul-Henning Kamp 233404533e1eSKonstantin Belousov sx_assert(&swdev_syscall_lock, SA_XLOCKED); 23350909f38aSPawel Jakub Dawidek #ifdef MAC 2336cb05b60aSAttilio Rao (void) vn_lock(sp->sw_vp, LK_EXCLUSIVE | LK_RETRY); 233735918c55SChristian S.J. Peron error = mac_system_check_swapoff(cred, sp->sw_vp); 233822db15c0SAttilio Rao (void) VOP_UNLOCK(sp->sw_vp, 0); 23390909f38aSPawel Jakub Dawidek if (error != 0) 23400909f38aSPawel Jakub Dawidek return (error); 23410909f38aSPawel Jakub Dawidek #endif 2342e9c0cc15SPoul-Henning Kamp nblks = sp->sw_nblks; 2343e9c0cc15SPoul-Henning Kamp 2344e9c0cc15SPoul-Henning Kamp /* 2345e9c0cc15SPoul-Henning Kamp * We can turn off this swap device safely only if the 2346e9c0cc15SPoul-Henning Kamp * available virtual memory in the system will fit the amount 2347e9c0cc15SPoul-Henning Kamp * of data we will have to page back in, plus an epsilon so 2348e9c0cc15SPoul-Henning Kamp * the system doesn't become critically low on swap space. 2349e9c0cc15SPoul-Henning Kamp */ 2350e2068d0bSJeff Roberson if (vm_free_count() + swap_pager_avail < nblks + nswap_lowat) 23510909f38aSPawel Jakub Dawidek return (ENOMEM); 2352e9c0cc15SPoul-Henning Kamp 2353e9c0cc15SPoul-Henning Kamp /* 2354e9c0cc15SPoul-Henning Kamp * Prevent further allocations on this device. 2355e9c0cc15SPoul-Henning Kamp */ 23562928cef7SAlan Cox mtx_lock(&sw_dev_mtx); 2357e9c0cc15SPoul-Henning Kamp sp->sw_flags |= SW_CLOSING; 235803bdd65fSAlan Cox swap_pager_avail -= blist_fill(sp->sw_blist, 0, nblks); 2359e8bb589dSMatt Macy swap_total -= nblks; 23602928cef7SAlan Cox mtx_unlock(&sw_dev_mtx); 2361e9c0cc15SPoul-Henning Kamp 2362e9c0cc15SPoul-Henning Kamp /* 2363e9c0cc15SPoul-Henning Kamp * Page in the contents of the device and close it. 2364e9c0cc15SPoul-Henning Kamp */ 2365b3fed13eSDavid Schultz swap_pager_swapoff(sp); 2366e9c0cc15SPoul-Henning Kamp 236735918c55SChristian S.J. Peron sp->sw_close(curthread, sp); 236820da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 23699e3e3fe5SWarner Losh sp->sw_id = NULL; 23708f60c087SPoul-Henning Kamp TAILQ_REMOVE(&swtailq, sp, sw_list); 23710676a140SAlan Cox nswapdev--; 23727dea2c2eSAlan Cox if (nswapdev == 0) { 23737dea2c2eSAlan Cox swap_pager_full = 2; 23747dea2c2eSAlan Cox swap_pager_almost_full = 1; 23757dea2c2eSAlan Cox } 23768f60c087SPoul-Henning Kamp if (swdevhd == sp) 23778f60c087SPoul-Henning Kamp swdevhd = NULL; 2378d05bc129SAlan Cox mtx_unlock(&sw_dev_mtx); 23798f60c087SPoul-Henning Kamp blist_destroy(sp->sw_blist); 23808f60c087SPoul-Henning Kamp free(sp, M_VMPGDATA); 23810909f38aSPawel Jakub Dawidek return (0); 23820909f38aSPawel Jakub Dawidek } 2383e9c0cc15SPoul-Henning Kamp 23840909f38aSPawel Jakub Dawidek void 23850909f38aSPawel Jakub Dawidek swapoff_all(void) 23860909f38aSPawel Jakub Dawidek { 23870909f38aSPawel Jakub Dawidek struct swdevt *sp, *spt; 23880909f38aSPawel Jakub Dawidek const char *devname; 23890909f38aSPawel Jakub Dawidek int error; 23900909f38aSPawel Jakub Dawidek 239104533e1eSKonstantin Belousov sx_xlock(&swdev_syscall_lock); 23920909f38aSPawel Jakub Dawidek 23930909f38aSPawel Jakub Dawidek mtx_lock(&sw_dev_mtx); 23940909f38aSPawel Jakub Dawidek TAILQ_FOREACH_SAFE(sp, &swtailq, sw_list, spt) { 23950909f38aSPawel Jakub Dawidek mtx_unlock(&sw_dev_mtx); 23960909f38aSPawel Jakub Dawidek if (vn_isdisk(sp->sw_vp, NULL)) 23977870adb6SEd Schouten devname = devtoname(sp->sw_vp->v_rdev); 23980909f38aSPawel Jakub Dawidek else 23990909f38aSPawel Jakub Dawidek devname = "[file]"; 240035918c55SChristian S.J. Peron error = swapoff_one(sp, thread0.td_ucred); 24010909f38aSPawel Jakub Dawidek if (error != 0) { 24020909f38aSPawel Jakub Dawidek printf("Cannot remove swap device %s (error=%d), " 24030909f38aSPawel Jakub Dawidek "skipping.\n", devname, error); 24040909f38aSPawel Jakub Dawidek } else if (bootverbose) { 24050909f38aSPawel Jakub Dawidek printf("Swap device %s removed.\n", devname); 24060909f38aSPawel Jakub Dawidek } 24070909f38aSPawel Jakub Dawidek mtx_lock(&sw_dev_mtx); 24080909f38aSPawel Jakub Dawidek } 24090909f38aSPawel Jakub Dawidek mtx_unlock(&sw_dev_mtx); 24100909f38aSPawel Jakub Dawidek 241104533e1eSKonstantin Belousov sx_xunlock(&swdev_syscall_lock); 2412e9c0cc15SPoul-Henning Kamp } 2413e9c0cc15SPoul-Henning Kamp 2414567104a1SPoul-Henning Kamp void 2415567104a1SPoul-Henning Kamp swap_pager_status(int *total, int *used) 2416567104a1SPoul-Henning Kamp { 2417567104a1SPoul-Henning Kamp struct swdevt *sp; 2418567104a1SPoul-Henning Kamp 2419567104a1SPoul-Henning Kamp *total = 0; 2420567104a1SPoul-Henning Kamp *used = 0; 242120da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 24228f60c087SPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 2423567104a1SPoul-Henning Kamp *total += sp->sw_nblks; 2424567104a1SPoul-Henning Kamp *used += sp->sw_used; 2425567104a1SPoul-Henning Kamp } 242620da9c2eSPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 2427567104a1SPoul-Henning Kamp } 2428567104a1SPoul-Henning Kamp 2429dda4f960SKonstantin Belousov int 2430dda4f960SKonstantin Belousov swap_dev_info(int name, struct xswdev *xs, char *devname, size_t len) 2431dda4f960SKonstantin Belousov { 2432dda4f960SKonstantin Belousov struct swdevt *sp; 24337870adb6SEd Schouten const char *tmp_devname; 2434dda4f960SKonstantin Belousov int error, n; 2435dda4f960SKonstantin Belousov 2436dda4f960SKonstantin Belousov n = 0; 2437dda4f960SKonstantin Belousov error = ENOENT; 2438dda4f960SKonstantin Belousov mtx_lock(&sw_dev_mtx); 2439dda4f960SKonstantin Belousov TAILQ_FOREACH(sp, &swtailq, sw_list) { 2440dda4f960SKonstantin Belousov if (n != name) { 2441dda4f960SKonstantin Belousov n++; 2442dda4f960SKonstantin Belousov continue; 2443dda4f960SKonstantin Belousov } 2444dda4f960SKonstantin Belousov xs->xsw_version = XSWDEV_VERSION; 2445dda4f960SKonstantin Belousov xs->xsw_dev = sp->sw_dev; 2446dda4f960SKonstantin Belousov xs->xsw_flags = sp->sw_flags; 2447dda4f960SKonstantin Belousov xs->xsw_nblks = sp->sw_nblks; 2448dda4f960SKonstantin Belousov xs->xsw_used = sp->sw_used; 2449dda4f960SKonstantin Belousov if (devname != NULL) { 2450dda4f960SKonstantin Belousov if (vn_isdisk(sp->sw_vp, NULL)) 24517870adb6SEd Schouten tmp_devname = devtoname(sp->sw_vp->v_rdev); 2452dda4f960SKonstantin Belousov else 2453dda4f960SKonstantin Belousov tmp_devname = "[file]"; 2454dda4f960SKonstantin Belousov strncpy(devname, tmp_devname, len); 2455dda4f960SKonstantin Belousov } 2456dda4f960SKonstantin Belousov error = 0; 2457dda4f960SKonstantin Belousov break; 2458dda4f960SKonstantin Belousov } 2459dda4f960SKonstantin Belousov mtx_unlock(&sw_dev_mtx); 2460dda4f960SKonstantin Belousov return (error); 2461dda4f960SKonstantin Belousov } 2462dda4f960SKonstantin Belousov 246369921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11) 246469921123SKonstantin Belousov #define XSWDEV_VERSION_11 1 246569921123SKonstantin Belousov struct xswdev11 { 246669921123SKonstantin Belousov u_int xsw_version; 246769921123SKonstantin Belousov uint32_t xsw_dev; 246869921123SKonstantin Belousov int xsw_flags; 246969921123SKonstantin Belousov int xsw_nblks; 247069921123SKonstantin Belousov int xsw_used; 247169921123SKonstantin Belousov }; 247269921123SKonstantin Belousov #endif 247369921123SKonstantin Belousov 2474f6d281e8SKonstantin Belousov #if defined(__amd64__) && defined(COMPAT_FREEBSD32) 2475f6d281e8SKonstantin Belousov struct xswdev32 { 2476f6d281e8SKonstantin Belousov u_int xsw_version; 2477f6d281e8SKonstantin Belousov u_int xsw_dev1, xsw_dev2; 2478f6d281e8SKonstantin Belousov int xsw_flags; 2479f6d281e8SKonstantin Belousov int xsw_nblks; 2480f6d281e8SKonstantin Belousov int xsw_used; 2481f6d281e8SKonstantin Belousov }; 2482f6d281e8SKonstantin Belousov #endif 2483f6d281e8SKonstantin Belousov 2484e9c0cc15SPoul-Henning Kamp static int 2485e9c0cc15SPoul-Henning Kamp sysctl_vm_swap_info(SYSCTL_HANDLER_ARGS) 2486e9c0cc15SPoul-Henning Kamp { 2487e9c0cc15SPoul-Henning Kamp struct xswdev xs; 2488f6d281e8SKonstantin Belousov #if defined(__amd64__) && defined(COMPAT_FREEBSD32) 2489f6d281e8SKonstantin Belousov struct xswdev32 xs32; 2490f6d281e8SKonstantin Belousov #endif 249169921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11) 249269921123SKonstantin Belousov struct xswdev11 xs11; 249369921123SKonstantin Belousov #endif 2494dda4f960SKonstantin Belousov int error; 2495e9c0cc15SPoul-Henning Kamp 2496e9c0cc15SPoul-Henning Kamp if (arg2 != 1) /* name length */ 2497e9c0cc15SPoul-Henning Kamp return (EINVAL); 2498dda4f960SKonstantin Belousov error = swap_dev_info(*(int *)arg1, &xs, NULL, 0); 2499dda4f960SKonstantin Belousov if (error != 0) 2500dda4f960SKonstantin Belousov return (error); 2501f6d281e8SKonstantin Belousov #if defined(__amd64__) && defined(COMPAT_FREEBSD32) 2502f6d281e8SKonstantin Belousov if (req->oldlen == sizeof(xs32)) { 2503f6d281e8SKonstantin Belousov xs32.xsw_version = XSWDEV_VERSION; 2504f6d281e8SKonstantin Belousov xs32.xsw_dev1 = xs.xsw_dev; 2505f6d281e8SKonstantin Belousov xs32.xsw_dev2 = xs.xsw_dev >> 32; 2506f6d281e8SKonstantin Belousov xs32.xsw_flags = xs.xsw_flags; 2507f6d281e8SKonstantin Belousov xs32.xsw_nblks = xs.xsw_nblks; 2508f6d281e8SKonstantin Belousov xs32.xsw_used = xs.xsw_used; 2509f6d281e8SKonstantin Belousov error = SYSCTL_OUT(req, &xs32, sizeof(xs32)); 2510f6d281e8SKonstantin Belousov return (error); 2511f6d281e8SKonstantin Belousov } 2512f6d281e8SKonstantin Belousov #endif 251369921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11) 251469921123SKonstantin Belousov if (req->oldlen == sizeof(xs11)) { 251569921123SKonstantin Belousov xs11.xsw_version = XSWDEV_VERSION_11; 251669921123SKonstantin Belousov xs11.xsw_dev = xs.xsw_dev; /* truncation */ 251769921123SKonstantin Belousov xs11.xsw_flags = xs.xsw_flags; 251869921123SKonstantin Belousov xs11.xsw_nblks = xs.xsw_nblks; 251969921123SKonstantin Belousov xs11.xsw_used = xs.xsw_used; 252069921123SKonstantin Belousov error = SYSCTL_OUT(req, &xs11, sizeof(xs11)); 2521f6d281e8SKonstantin Belousov return (error); 2522f6d281e8SKonstantin Belousov } 252369921123SKonstantin Belousov #endif 2524e9c0cc15SPoul-Henning Kamp error = SYSCTL_OUT(req, &xs, sizeof(xs)); 2525e9c0cc15SPoul-Henning Kamp return (error); 2526e9c0cc15SPoul-Henning Kamp } 2527e9c0cc15SPoul-Henning Kamp 25288f60c087SPoul-Henning Kamp SYSCTL_INT(_vm, OID_AUTO, nswapdev, CTLFLAG_RD, &nswapdev, 0, 2529e9c0cc15SPoul-Henning Kamp "Number of swap devices"); 25304c36e917SKonstantin Belousov SYSCTL_NODE(_vm, OID_AUTO, swap_info, CTLFLAG_RD | CTLFLAG_MPSAFE, 25314c36e917SKonstantin Belousov sysctl_vm_swap_info, 2532e9c0cc15SPoul-Henning Kamp "Swap statistics by device"); 2533ec38b344SPoul-Henning Kamp 2534ec38b344SPoul-Henning Kamp /* 2535f425ab8eSKonstantin Belousov * Count the approximate swap usage in pages for a vmspace. The 2536f425ab8eSKonstantin Belousov * shadowed or not yet copied on write swap blocks are not accounted. 2537ec38b344SPoul-Henning Kamp * The map must be locked. 2538ec38b344SPoul-Henning Kamp */ 25392860553aSRebecca Cran long 2540ec38b344SPoul-Henning Kamp vmspace_swap_count(struct vmspace *vmspace) 2541ec38b344SPoul-Henning Kamp { 254265d8409cSRebecca Cran vm_map_t map; 2543ec38b344SPoul-Henning Kamp vm_map_entry_t cur; 254465d8409cSRebecca Cran vm_object_t object; 2545f425ab8eSKonstantin Belousov struct swblk *sb; 2546f425ab8eSKonstantin Belousov vm_pindex_t e, pi; 2547f425ab8eSKonstantin Belousov long count; 2548f425ab8eSKonstantin Belousov int i; 254965d8409cSRebecca Cran 255065d8409cSRebecca Cran map = &vmspace->vm_map; 255165d8409cSRebecca Cran count = 0; 2552ec38b344SPoul-Henning Kamp 2553ec38b344SPoul-Henning Kamp for (cur = map->header.next; cur != &map->header; cur = cur->next) { 2554f425ab8eSKonstantin Belousov if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) != 0) 2555f425ab8eSKonstantin Belousov continue; 2556f425ab8eSKonstantin Belousov object = cur->object.vm_object; 2557f425ab8eSKonstantin Belousov if (object == NULL || object->type != OBJT_SWAP) 2558f425ab8eSKonstantin Belousov continue; 2559f425ab8eSKonstantin Belousov VM_OBJECT_RLOCK(object); 2560f425ab8eSKonstantin Belousov if (object->type != OBJT_SWAP) 2561f425ab8eSKonstantin Belousov goto unlock; 2562f425ab8eSKonstantin Belousov pi = OFF_TO_IDX(cur->offset); 2563f425ab8eSKonstantin Belousov e = pi + OFF_TO_IDX(cur->end - cur->start); 2564f425ab8eSKonstantin Belousov for (;; pi = sb->p + SWAP_META_PAGES) { 2565f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP_GE( 2566f425ab8eSKonstantin Belousov &object->un_pager.swp.swp_blks, pi); 2567f425ab8eSKonstantin Belousov if (sb == NULL || sb->p >= e) 2568f425ab8eSKonstantin Belousov break; 2569f425ab8eSKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) { 2570f425ab8eSKonstantin Belousov if (sb->p + i < e && 2571f425ab8eSKonstantin Belousov sb->d[i] != SWAPBLK_NONE) 2572f425ab8eSKonstantin Belousov count++; 2573ec38b344SPoul-Henning Kamp } 2574ec38b344SPoul-Henning Kamp } 2575f425ab8eSKonstantin Belousov unlock: 2576f425ab8eSKonstantin Belousov VM_OBJECT_RUNLOCK(object); 2577ec38b344SPoul-Henning Kamp } 2578ec38b344SPoul-Henning Kamp return (count); 2579ec38b344SPoul-Henning Kamp } 2580dee34ca4SPoul-Henning Kamp 2581dee34ca4SPoul-Henning Kamp /* 2582dee34ca4SPoul-Henning Kamp * GEOM backend 2583dee34ca4SPoul-Henning Kamp * 2584dee34ca4SPoul-Henning Kamp * Swapping onto disk devices. 2585dee34ca4SPoul-Henning Kamp * 2586dee34ca4SPoul-Henning Kamp */ 2587dee34ca4SPoul-Henning Kamp 25885721c9c7SPoul-Henning Kamp static g_orphan_t swapgeom_orphan; 25895721c9c7SPoul-Henning Kamp 2590dee34ca4SPoul-Henning Kamp static struct g_class g_swap_class = { 2591dee34ca4SPoul-Henning Kamp .name = "SWAP", 25925721c9c7SPoul-Henning Kamp .version = G_VERSION, 25935721c9c7SPoul-Henning Kamp .orphan = swapgeom_orphan, 2594dee34ca4SPoul-Henning Kamp }; 2595dee34ca4SPoul-Henning Kamp 2596dee34ca4SPoul-Henning Kamp DECLARE_GEOM_CLASS(g_swap_class, g_class); 2597dee34ca4SPoul-Henning Kamp 2598dee34ca4SPoul-Henning Kamp 2599dee34ca4SPoul-Henning Kamp static void 26003398491bSAlexander Motin swapgeom_close_ev(void *arg, int flags) 26013398491bSAlexander Motin { 26023398491bSAlexander Motin struct g_consumer *cp; 26033398491bSAlexander Motin 26043398491bSAlexander Motin cp = arg; 26053398491bSAlexander Motin g_access(cp, -1, -1, 0); 26063398491bSAlexander Motin g_detach(cp); 26073398491bSAlexander Motin g_destroy_consumer(cp); 26083398491bSAlexander Motin } 26093398491bSAlexander Motin 26109e3e3fe5SWarner Losh /* 26119e3e3fe5SWarner Losh * Add a reference to the g_consumer for an inflight transaction. 26129e3e3fe5SWarner Losh */ 26139e3e3fe5SWarner Losh static void 26149e3e3fe5SWarner Losh swapgeom_acquire(struct g_consumer *cp) 26159e3e3fe5SWarner Losh { 26169e3e3fe5SWarner Losh 26179e3e3fe5SWarner Losh mtx_assert(&sw_dev_mtx, MA_OWNED); 26189e3e3fe5SWarner Losh cp->index++; 26199e3e3fe5SWarner Losh } 26209e3e3fe5SWarner Losh 26219e3e3fe5SWarner Losh /* 26220c657d22SKonstantin Belousov * Remove a reference from the g_consumer. Post a close event if all 26230c657d22SKonstantin Belousov * references go away, since the function might be called from the 26240c657d22SKonstantin Belousov * biodone context. 26259e3e3fe5SWarner Losh */ 26269e3e3fe5SWarner Losh static void 26279e3e3fe5SWarner Losh swapgeom_release(struct g_consumer *cp, struct swdevt *sp) 26289e3e3fe5SWarner Losh { 26299e3e3fe5SWarner Losh 26309e3e3fe5SWarner Losh mtx_assert(&sw_dev_mtx, MA_OWNED); 26319e3e3fe5SWarner Losh cp->index--; 26329e3e3fe5SWarner Losh if (cp->index == 0) { 26339e3e3fe5SWarner Losh if (g_post_event(swapgeom_close_ev, cp, M_NOWAIT, NULL) == 0) 26349e3e3fe5SWarner Losh sp->sw_id = NULL; 26359e3e3fe5SWarner Losh } 26369e3e3fe5SWarner Losh } 26379e3e3fe5SWarner Losh 26383398491bSAlexander Motin static void 2639dee34ca4SPoul-Henning Kamp swapgeom_done(struct bio *bp2) 2640dee34ca4SPoul-Henning Kamp { 26413398491bSAlexander Motin struct swdevt *sp; 2642dee34ca4SPoul-Henning Kamp struct buf *bp; 26433398491bSAlexander Motin struct g_consumer *cp; 2644dee34ca4SPoul-Henning Kamp 2645dee34ca4SPoul-Henning Kamp bp = bp2->bio_caller2; 26463398491bSAlexander Motin cp = bp2->bio_from; 2647c5d3d25eSPoul-Henning Kamp bp->b_ioflags = bp2->bio_flags; 2648dee34ca4SPoul-Henning Kamp if (bp2->bio_error) 2649dee34ca4SPoul-Henning Kamp bp->b_ioflags |= BIO_ERROR; 2650c5d3d25eSPoul-Henning Kamp bp->b_resid = bp->b_bcount - bp2->bio_completed; 2651c5d3d25eSPoul-Henning Kamp bp->b_error = bp2->bio_error; 2652756a5412SGleb Smirnoff bp->b_caller1 = NULL; 2653dee34ca4SPoul-Henning Kamp bufdone(bp); 26543398491bSAlexander Motin sp = bp2->bio_caller1; 26559e3e3fe5SWarner Losh mtx_lock(&sw_dev_mtx); 26569e3e3fe5SWarner Losh swapgeom_release(cp, sp); 26573398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 2658dee34ca4SPoul-Henning Kamp g_destroy_bio(bp2); 2659dee34ca4SPoul-Henning Kamp } 2660dee34ca4SPoul-Henning Kamp 2661dee34ca4SPoul-Henning Kamp static void 2662dee34ca4SPoul-Henning Kamp swapgeom_strategy(struct buf *bp, struct swdevt *sp) 2663dee34ca4SPoul-Henning Kamp { 2664dee34ca4SPoul-Henning Kamp struct bio *bio; 2665dee34ca4SPoul-Henning Kamp struct g_consumer *cp; 2666dee34ca4SPoul-Henning Kamp 26673398491bSAlexander Motin mtx_lock(&sw_dev_mtx); 2668dee34ca4SPoul-Henning Kamp cp = sp->sw_id; 2669dee34ca4SPoul-Henning Kamp if (cp == NULL) { 26703398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 2671dee34ca4SPoul-Henning Kamp bp->b_error = ENXIO; 2672dee34ca4SPoul-Henning Kamp bp->b_ioflags |= BIO_ERROR; 2673dee34ca4SPoul-Henning Kamp bufdone(bp); 2674dee34ca4SPoul-Henning Kamp return; 2675dee34ca4SPoul-Henning Kamp } 26769e3e3fe5SWarner Losh swapgeom_acquire(cp); 26773398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 267811041003SKonstantin Belousov if (bp->b_iocmd == BIO_WRITE) 267911041003SKonstantin Belousov bio = g_new_bio(); 268011041003SKonstantin Belousov else 26814f8205e5SPoul-Henning Kamp bio = g_alloc_bio(); 26824f8205e5SPoul-Henning Kamp if (bio == NULL) { 26839e3e3fe5SWarner Losh mtx_lock(&sw_dev_mtx); 26849e3e3fe5SWarner Losh swapgeom_release(cp, sp); 26859e3e3fe5SWarner Losh mtx_unlock(&sw_dev_mtx); 26863e5b6861SPoul-Henning Kamp bp->b_error = ENOMEM; 26873e5b6861SPoul-Henning Kamp bp->b_ioflags |= BIO_ERROR; 26880b208315SEdward Tomasz Napierala printf("swap_pager: cannot allocate bio\n"); 26893e5b6861SPoul-Henning Kamp bufdone(bp); 26903e5b6861SPoul-Henning Kamp return; 26913e5b6861SPoul-Henning Kamp } 269211041003SKonstantin Belousov 2693756a5412SGleb Smirnoff bp->b_caller1 = bio; 26943398491bSAlexander Motin bio->bio_caller1 = sp; 2695dee34ca4SPoul-Henning Kamp bio->bio_caller2 = bp; 2696c5d3d25eSPoul-Henning Kamp bio->bio_cmd = bp->b_iocmd; 2697dee34ca4SPoul-Henning Kamp bio->bio_offset = (bp->b_blkno - sp->sw_first) * PAGE_SIZE; 2698dee34ca4SPoul-Henning Kamp bio->bio_length = bp->b_bcount; 2699dee34ca4SPoul-Henning Kamp bio->bio_done = swapgeom_done; 2700fade8dd7SJeff Roberson if (!buf_mapped(bp)) { 27012cc718a1SKonstantin Belousov bio->bio_ma = bp->b_pages; 27022cc718a1SKonstantin Belousov bio->bio_data = unmapped_buf; 27032cc718a1SKonstantin Belousov bio->bio_ma_offset = (vm_offset_t)bp->b_offset & PAGE_MASK; 27042cc718a1SKonstantin Belousov bio->bio_ma_n = bp->b_npages; 27052cc718a1SKonstantin Belousov bio->bio_flags |= BIO_UNMAPPED; 27062cc718a1SKonstantin Belousov } else { 27072cc718a1SKonstantin Belousov bio->bio_data = bp->b_data; 27082cc718a1SKonstantin Belousov bio->bio_ma = NULL; 27092cc718a1SKonstantin Belousov } 2710dee34ca4SPoul-Henning Kamp g_io_request(bio, cp); 2711dee34ca4SPoul-Henning Kamp return; 2712dee34ca4SPoul-Henning Kamp } 2713dee34ca4SPoul-Henning Kamp 2714dee34ca4SPoul-Henning Kamp static void 2715dee34ca4SPoul-Henning Kamp swapgeom_orphan(struct g_consumer *cp) 2716dee34ca4SPoul-Henning Kamp { 2717dee34ca4SPoul-Henning Kamp struct swdevt *sp; 27183398491bSAlexander Motin int destroy; 2719dee34ca4SPoul-Henning Kamp 2720dee34ca4SPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 27213398491bSAlexander Motin TAILQ_FOREACH(sp, &swtailq, sw_list) { 27223398491bSAlexander Motin if (sp->sw_id == cp) { 27238f12d83aSAlexander Motin sp->sw_flags |= SW_CLOSING; 27243398491bSAlexander Motin break; 2725dee34ca4SPoul-Henning Kamp } 27263398491bSAlexander Motin } 27279e3e3fe5SWarner Losh /* 27289e3e3fe5SWarner Losh * Drop reference we were created with. Do directly since we're in a 27299e3e3fe5SWarner Losh * special context where we don't have to queue the call to 27309e3e3fe5SWarner Losh * swapgeom_close_ev(). 27319e3e3fe5SWarner Losh */ 27329e3e3fe5SWarner Losh cp->index--; 27333398491bSAlexander Motin destroy = ((sp != NULL) && (cp->index == 0)); 27343398491bSAlexander Motin if (destroy) 27353398491bSAlexander Motin sp->sw_id = NULL; 27363398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 27373398491bSAlexander Motin if (destroy) 27383398491bSAlexander Motin swapgeom_close_ev(cp, 0); 2739dee34ca4SPoul-Henning Kamp } 2740dee34ca4SPoul-Henning Kamp 2741dee34ca4SPoul-Henning Kamp static void 2742dee34ca4SPoul-Henning Kamp swapgeom_close(struct thread *td, struct swdevt *sw) 2743dee34ca4SPoul-Henning Kamp { 27443398491bSAlexander Motin struct g_consumer *cp; 2745dee34ca4SPoul-Henning Kamp 27463398491bSAlexander Motin mtx_lock(&sw_dev_mtx); 27473398491bSAlexander Motin cp = sw->sw_id; 27483398491bSAlexander Motin sw->sw_id = NULL; 27493398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 27500c657d22SKonstantin Belousov 27510c657d22SKonstantin Belousov /* 27520c657d22SKonstantin Belousov * swapgeom_close() may be called from the biodone context, 27530c657d22SKonstantin Belousov * where we cannot perform topology changes. Delegate the 27540c657d22SKonstantin Belousov * work to the events thread. 27550c657d22SKonstantin Belousov */ 27563398491bSAlexander Motin if (cp != NULL) 27573398491bSAlexander Motin g_waitfor_event(swapgeom_close_ev, cp, M_WAITOK, NULL); 2758dee34ca4SPoul-Henning Kamp } 2759dee34ca4SPoul-Henning Kamp 276088ad2d7bSKonstantin Belousov static int 276188ad2d7bSKonstantin Belousov swapongeom_locked(struct cdev *dev, struct vnode *vp) 2762dee34ca4SPoul-Henning Kamp { 2763dee34ca4SPoul-Henning Kamp struct g_provider *pp; 2764dee34ca4SPoul-Henning Kamp struct g_consumer *cp; 2765dee34ca4SPoul-Henning Kamp static struct g_geom *gp; 2766dee34ca4SPoul-Henning Kamp struct swdevt *sp; 2767dee34ca4SPoul-Henning Kamp u_long nblks; 2768dee34ca4SPoul-Henning Kamp int error; 2769dee34ca4SPoul-Henning Kamp 277088ad2d7bSKonstantin Belousov pp = g_dev_getprovider(dev); 277188ad2d7bSKonstantin Belousov if (pp == NULL) 277288ad2d7bSKonstantin Belousov return (ENODEV); 2773dee34ca4SPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 2774dee34ca4SPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 2775dee34ca4SPoul-Henning Kamp cp = sp->sw_id; 2776dee34ca4SPoul-Henning Kamp if (cp != NULL && cp->provider == pp) { 2777dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 277888ad2d7bSKonstantin Belousov return (EBUSY); 2779dee34ca4SPoul-Henning Kamp } 2780dee34ca4SPoul-Henning Kamp } 2781dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 27825721c9c7SPoul-Henning Kamp if (gp == NULL) 278302c62349SJaakko Heinonen gp = g_new_geomf(&g_swap_class, "swap"); 2784dee34ca4SPoul-Henning Kamp cp = g_new_consumer(gp); 27859e3e3fe5SWarner Losh cp->index = 1; /* Number of active I/Os, plus one for being active. */ 27869e3e3fe5SWarner Losh cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 2787dee34ca4SPoul-Henning Kamp g_attach(cp, pp); 2788afeb65e6SPoul-Henning Kamp /* 2789afeb65e6SPoul-Henning Kamp * XXX: Every time you think you can improve the margin for 2790afeb65e6SPoul-Henning Kamp * footshooting, somebody depends on the ability to do so: 2791afeb65e6SPoul-Henning Kamp * savecore(8) wants to write to our swapdev so we cannot 2792afeb65e6SPoul-Henning Kamp * set an exclusive count :-( 2793afeb65e6SPoul-Henning Kamp */ 2794d2bae332SPoul-Henning Kamp error = g_access(cp, 1, 1, 0); 279588ad2d7bSKonstantin Belousov if (error != 0) { 2796dee34ca4SPoul-Henning Kamp g_detach(cp); 2797dee34ca4SPoul-Henning Kamp g_destroy_consumer(cp); 279888ad2d7bSKonstantin Belousov return (error); 2799dee34ca4SPoul-Henning Kamp } 2800dee34ca4SPoul-Henning Kamp nblks = pp->mediasize / DEV_BSIZE; 280188ad2d7bSKonstantin Belousov swaponsomething(vp, cp, nblks, swapgeom_strategy, 280288ad2d7bSKonstantin Belousov swapgeom_close, dev2udev(dev), 28032cc718a1SKonstantin Belousov (pp->flags & G_PF_ACCEPT_UNMAPPED) != 0 ? SW_UNMAPPED : 0); 280488ad2d7bSKonstantin Belousov return (0); 2805dee34ca4SPoul-Henning Kamp } 2806dee34ca4SPoul-Henning Kamp 2807dee34ca4SPoul-Henning Kamp static int 280888ad2d7bSKonstantin Belousov swapongeom(struct vnode *vp) 2809dee34ca4SPoul-Henning Kamp { 2810dee34ca4SPoul-Henning Kamp int error; 2811dee34ca4SPoul-Henning Kamp 2812cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 281388ad2d7bSKonstantin Belousov if (vp->v_type != VCHR || (vp->v_iflag & VI_DOOMED) != 0) { 281488ad2d7bSKonstantin Belousov error = ENOENT; 281588ad2d7bSKonstantin Belousov } else { 281688ad2d7bSKonstantin Belousov g_topology_lock(); 281788ad2d7bSKonstantin Belousov error = swapongeom_locked(vp->v_rdev, vp); 281888ad2d7bSKonstantin Belousov g_topology_unlock(); 281988ad2d7bSKonstantin Belousov } 282022db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 2821dee34ca4SPoul-Henning Kamp return (error); 2822dee34ca4SPoul-Henning Kamp } 2823dee34ca4SPoul-Henning Kamp 2824dee34ca4SPoul-Henning Kamp /* 2825dee34ca4SPoul-Henning Kamp * VNODE backend 2826dee34ca4SPoul-Henning Kamp * 2827dee34ca4SPoul-Henning Kamp * This is used mainly for network filesystem (read: probably only tested 2828dee34ca4SPoul-Henning Kamp * with NFS) swapfiles. 2829dee34ca4SPoul-Henning Kamp * 2830dee34ca4SPoul-Henning Kamp */ 2831dee34ca4SPoul-Henning Kamp 2832dee34ca4SPoul-Henning Kamp static void 2833dee34ca4SPoul-Henning Kamp swapdev_strategy(struct buf *bp, struct swdevt *sp) 2834dee34ca4SPoul-Henning Kamp { 2835494eb176SPoul-Henning Kamp struct vnode *vp2; 2836dee34ca4SPoul-Henning Kamp 2837dee34ca4SPoul-Henning Kamp bp->b_blkno = ctodb(bp->b_blkno - sp->sw_first); 2838dee34ca4SPoul-Henning Kamp 2839dee34ca4SPoul-Henning Kamp vp2 = sp->sw_id; 2840dee34ca4SPoul-Henning Kamp vhold(vp2); 2841dee34ca4SPoul-Henning Kamp if (bp->b_iocmd == BIO_WRITE) { 28423cfc7651SOlivier Houchard if (bp->b_bufobj) 2843494eb176SPoul-Henning Kamp bufobj_wdrop(bp->b_bufobj); 2844a76d8f4eSPoul-Henning Kamp bufobj_wref(&vp2->v_bufobj); 2845dee34ca4SPoul-Henning Kamp } 28463cfc7651SOlivier Houchard if (bp->b_bufobj != &vp2->v_bufobj) 28473cfc7651SOlivier Houchard bp->b_bufobj = &vp2->v_bufobj; 2848dee34ca4SPoul-Henning Kamp bp->b_vp = vp2; 28492c18019fSPoul-Henning Kamp bp->b_iooffset = dbtob(bp->b_blkno); 2850b792bebeSPoul-Henning Kamp bstrategy(bp); 2851dee34ca4SPoul-Henning Kamp return; 2852dee34ca4SPoul-Henning Kamp } 2853dee34ca4SPoul-Henning Kamp 2854dee34ca4SPoul-Henning Kamp static void 2855dee34ca4SPoul-Henning Kamp swapdev_close(struct thread *td, struct swdevt *sp) 2856dee34ca4SPoul-Henning Kamp { 2857dee34ca4SPoul-Henning Kamp 2858dee34ca4SPoul-Henning Kamp VOP_CLOSE(sp->sw_vp, FREAD | FWRITE, td->td_ucred, td); 2859dee34ca4SPoul-Henning Kamp vrele(sp->sw_vp); 2860dee34ca4SPoul-Henning Kamp } 2861dee34ca4SPoul-Henning Kamp 2862dee34ca4SPoul-Henning Kamp 2863dee34ca4SPoul-Henning Kamp static int 2864dee34ca4SPoul-Henning Kamp swaponvp(struct thread *td, struct vnode *vp, u_long nblks) 2865dee34ca4SPoul-Henning Kamp { 2866dee34ca4SPoul-Henning Kamp struct swdevt *sp; 2867dee34ca4SPoul-Henning Kamp int error; 2868dee34ca4SPoul-Henning Kamp 2869dee34ca4SPoul-Henning Kamp if (nblks == 0) 2870dee34ca4SPoul-Henning Kamp return (ENXIO); 2871dee34ca4SPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 2872dee34ca4SPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 2873dee34ca4SPoul-Henning Kamp if (sp->sw_id == vp) { 2874dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 2875dee34ca4SPoul-Henning Kamp return (EBUSY); 2876dee34ca4SPoul-Henning Kamp } 2877dee34ca4SPoul-Henning Kamp } 2878dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 2879dee34ca4SPoul-Henning Kamp 2880cb05b60aSAttilio Rao (void) vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2881dee34ca4SPoul-Henning Kamp #ifdef MAC 288230d239bcSRobert Watson error = mac_system_check_swapon(td->td_ucred, vp); 2883dee34ca4SPoul-Henning Kamp if (error == 0) 2884dee34ca4SPoul-Henning Kamp #endif 28859e223287SKonstantin Belousov error = VOP_OPEN(vp, FREAD | FWRITE, td->td_ucred, td, NULL); 288622db15c0SAttilio Rao (void) VOP_UNLOCK(vp, 0); 2887dee34ca4SPoul-Henning Kamp if (error) 2888dee34ca4SPoul-Henning Kamp return (error); 2889dee34ca4SPoul-Henning Kamp 2890dee34ca4SPoul-Henning Kamp swaponsomething(vp, vp, nblks, swapdev_strategy, swapdev_close, 28912cc718a1SKonstantin Belousov NODEV, 0); 2892dee34ca4SPoul-Henning Kamp return (0); 2893dee34ca4SPoul-Henning Kamp } 289489c241d1SGleb Smirnoff 289589c241d1SGleb Smirnoff static int 289689c241d1SGleb Smirnoff sysctl_swap_async_max(SYSCTL_HANDLER_ARGS) 289789c241d1SGleb Smirnoff { 289889c241d1SGleb Smirnoff int error, new, n; 289989c241d1SGleb Smirnoff 290089c241d1SGleb Smirnoff new = nsw_wcount_async_max; 290189c241d1SGleb Smirnoff error = sysctl_handle_int(oidp, &new, 0, req); 290289c241d1SGleb Smirnoff if (error != 0 || req->newptr == NULL) 290389c241d1SGleb Smirnoff return (error); 290489c241d1SGleb Smirnoff 290589c241d1SGleb Smirnoff if (new > nswbuf / 2 || new < 1) 290689c241d1SGleb Smirnoff return (EINVAL); 290789c241d1SGleb Smirnoff 2908756a5412SGleb Smirnoff mtx_lock(&swbuf_mtx); 290989c241d1SGleb Smirnoff while (nsw_wcount_async_max != new) { 291089c241d1SGleb Smirnoff /* 291189c241d1SGleb Smirnoff * Adjust difference. If the current async count is too low, 291289c241d1SGleb Smirnoff * we will need to sqeeze our update slowly in. Sleep with a 291389c241d1SGleb Smirnoff * higher priority than getpbuf() to finish faster. 291489c241d1SGleb Smirnoff */ 291589c241d1SGleb Smirnoff n = new - nsw_wcount_async_max; 291689c241d1SGleb Smirnoff if (nsw_wcount_async + n >= 0) { 291789c241d1SGleb Smirnoff nsw_wcount_async += n; 291889c241d1SGleb Smirnoff nsw_wcount_async_max += n; 291989c241d1SGleb Smirnoff wakeup(&nsw_wcount_async); 292089c241d1SGleb Smirnoff } else { 292189c241d1SGleb Smirnoff nsw_wcount_async_max -= nsw_wcount_async; 292289c241d1SGleb Smirnoff nsw_wcount_async = 0; 2923756a5412SGleb Smirnoff msleep(&nsw_wcount_async, &swbuf_mtx, PSWP, 292489c241d1SGleb Smirnoff "swpsysctl", 0); 292589c241d1SGleb Smirnoff } 292689c241d1SGleb Smirnoff } 2927756a5412SGleb Smirnoff mtx_unlock(&swbuf_mtx); 292889c241d1SGleb Smirnoff 292989c241d1SGleb Smirnoff return (0); 293089c241d1SGleb Smirnoff } 2931