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)*/ 326*756a5412SGleb Smirnoff static struct mtx swbuf_mtx; /* to sync nsw_wcount_async */ 327*756a5412SGleb 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]; 353*756a5412SGleb Smirnoff static uma_zone_t swwbuf_zone; 354*756a5412SGleb 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); 410a5edd34aSPoul-Henning Kamp static daddr_t swp_pager_getswapspace(int npages); 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; 544*756a5412SGleb Smirnoff mtx_init(&swbuf_mtx, "async swbuf mutex", NULL, MTX_DEF); 545*756a5412SGleb Smirnoff 546*756a5412SGleb Smirnoff swwbuf_zone = pbuf_zsecond_create("swwbuf", nswbuf / 4); 547*756a5412SGleb 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 * 7111c7c3c6aSMatthew Dillon * Allocate swap for the requested number of pages. The starting 7121c7c3c6aSMatthew Dillon * swap block number (a page index) is returned or SWAPBLK_NONE 7131c7c3c6aSMatthew Dillon * if the allocation failed. 7141c7c3c6aSMatthew Dillon * 7151c7c3c6aSMatthew Dillon * Also has the side effect of advising that somebody made a mistake 7161c7c3c6aSMatthew Dillon * when they configured swap and didn't configure enough. 7171c7c3c6aSMatthew Dillon * 71815523cf7SKonstantin Belousov * This routine may not sleep. 7198f60c087SPoul-Henning Kamp * 7208f60c087SPoul-Henning Kamp * We allocate in round-robin fashion from the configured devices. 7211c7c3c6aSMatthew Dillon */ 722a5edd34aSPoul-Henning Kamp static daddr_t 7232f249180SPoul-Henning Kamp swp_pager_getswapspace(int npages) 7241c7c3c6aSMatthew Dillon { 7251c7c3c6aSMatthew Dillon daddr_t blk; 7268f60c087SPoul-Henning Kamp struct swdevt *sp; 7278f60c087SPoul-Henning Kamp int i; 7281c7c3c6aSMatthew Dillon 7298f60c087SPoul-Henning Kamp blk = SWAPBLK_NONE; 73020da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 7318f60c087SPoul-Henning Kamp sp = swdevhd; 7328f60c087SPoul-Henning Kamp for (i = 0; i < nswapdev; i++) { 7338f60c087SPoul-Henning Kamp if (sp == NULL) 7348f60c087SPoul-Henning Kamp sp = TAILQ_FIRST(&swtailq); 7358f60c087SPoul-Henning Kamp if (!(sp->sw_flags & SW_CLOSING)) { 7368f60c087SPoul-Henning Kamp blk = blist_alloc(sp->sw_blist, npages); 7378f60c087SPoul-Henning Kamp if (blk != SWAPBLK_NONE) { 7388f60c087SPoul-Henning Kamp blk += sp->sw_first; 7398f60c087SPoul-Henning Kamp sp->sw_used += npages; 740d05bc129SAlan Cox swap_pager_avail -= npages; 7418f60c087SPoul-Henning Kamp swp_sizecheck(); 7428f60c087SPoul-Henning Kamp swdevhd = TAILQ_NEXT(sp, sw_list); 743d05bc129SAlan Cox goto done; 7448f60c087SPoul-Henning Kamp } 7458f60c087SPoul-Henning Kamp } 7468f60c087SPoul-Henning Kamp sp = TAILQ_NEXT(sp, sw_list); 7478f60c087SPoul-Henning Kamp } 7482b0d37a4SMatthew Dillon if (swap_pager_full != 2) { 74920da9c2eSPoul-Henning Kamp printf("swap_pager_getswapspace(%d): failed\n", npages); 7502b0d37a4SMatthew Dillon swap_pager_full = 2; 75120d3034fSMatthew Dillon swap_pager_almost_full = 1; 7522b0d37a4SMatthew Dillon } 7538f60c087SPoul-Henning Kamp swdevhd = NULL; 754d05bc129SAlan Cox done: 755d05bc129SAlan Cox mtx_unlock(&sw_dev_mtx); 7561c7c3c6aSMatthew Dillon return (blk); 75726f9a767SRodney W. Grimes } 75826f9a767SRodney W. Grimes 759541a1175SAlan Cox static bool 760b3fed13eSDavid Schultz swp_pager_isondev(daddr_t blk, struct swdevt *sp) 7618f60c087SPoul-Henning Kamp { 7628f60c087SPoul-Henning Kamp 763b3fed13eSDavid Schultz return (blk >= sp->sw_first && blk < sp->sw_end); 7648f60c087SPoul-Henning Kamp } 7658f60c087SPoul-Henning Kamp 7664b03903aSPoul-Henning Kamp static void 7674b03903aSPoul-Henning Kamp swp_pager_strategy(struct buf *bp) 7684b03903aSPoul-Henning Kamp { 7694b03903aSPoul-Henning Kamp struct swdevt *sp; 7704b03903aSPoul-Henning Kamp 77120da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 7724b03903aSPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 773541a1175SAlan Cox if (swp_pager_isondev(bp->b_blkno, sp)) { 77420da9c2eSPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 7752cc718a1SKonstantin Belousov if ((sp->sw_flags & SW_UNMAPPED) != 0 && 7762cc718a1SKonstantin Belousov unmapped_buf_allowed) { 7772cc718a1SKonstantin Belousov bp->b_data = unmapped_buf; 7782cc718a1SKonstantin Belousov bp->b_offset = 0; 7792cc718a1SKonstantin Belousov } else { 7802cc718a1SKonstantin Belousov pmap_qenter((vm_offset_t)bp->b_data, 7812cc718a1SKonstantin Belousov &bp->b_pages[0], bp->b_bcount / PAGE_SIZE); 7822cc718a1SKonstantin Belousov } 7834b03903aSPoul-Henning Kamp sp->sw_strategy(bp, sp); 7844b03903aSPoul-Henning Kamp return; 7854b03903aSPoul-Henning Kamp } 7864b03903aSPoul-Henning Kamp } 78720da9c2eSPoul-Henning Kamp panic("Swapdev not found"); 7884b03903aSPoul-Henning Kamp } 7894b03903aSPoul-Henning Kamp 7908f60c087SPoul-Henning Kamp 79126f9a767SRodney W. Grimes /* 7921c7c3c6aSMatthew Dillon * SWP_PAGER_FREESWAPSPACE() - free raw swap space 7931c7c3c6aSMatthew Dillon * 7941c7c3c6aSMatthew Dillon * This routine returns the specified swap blocks back to the bitmap. 7951c7c3c6aSMatthew Dillon * 79615523cf7SKonstantin Belousov * This routine may not sleep. 79726f9a767SRodney W. Grimes */ 798a5edd34aSPoul-Henning Kamp static void 799230869e0SAlan Cox swp_pager_freeswapspace(daddr_t blk, daddr_t npages) 8000d94caffSDavid Greenman { 8018f60c087SPoul-Henning Kamp struct swdevt *sp; 80292da00bbSMatthew Dillon 803230869e0SAlan Cox if (npages == 0) 804230869e0SAlan Cox return; 8057645e885SAlan Cox mtx_lock(&sw_dev_mtx); 8067645e885SAlan Cox TAILQ_FOREACH(sp, &swtailq, sw_list) { 807541a1175SAlan Cox if (swp_pager_isondev(blk, sp)) { 80892da00bbSMatthew Dillon sp->sw_used -= npages; 80992da00bbSMatthew Dillon /* 8107645e885SAlan Cox * If we are attempting to stop swapping on 8117645e885SAlan Cox * this device, we don't want to mark any 8127645e885SAlan Cox * blocks free lest they be reused. 81392da00bbSMatthew Dillon */ 8147645e885SAlan Cox if ((sp->sw_flags & SW_CLOSING) == 0) { 8157645e885SAlan Cox blist_free(sp->sw_blist, blk - sp->sw_first, 8167645e885SAlan Cox npages); 8178f60c087SPoul-Henning Kamp swap_pager_avail += npages; 8181c7c3c6aSMatthew Dillon swp_sizecheck(); 81926f9a767SRodney W. Grimes } 8207645e885SAlan Cox mtx_unlock(&sw_dev_mtx); 8217645e885SAlan Cox return; 8227645e885SAlan Cox } 8237645e885SAlan Cox } 8247645e885SAlan Cox panic("Swapdev not found"); 8257645e885SAlan Cox } 8261c7c3c6aSMatthew Dillon 82726f9a767SRodney W. Grimes /* 828d027ed2eSAlan Cox * SYSCTL_SWAP_FRAGMENTATION() - produce raw swap space stats 829d027ed2eSAlan Cox */ 830d027ed2eSAlan Cox static int 831d027ed2eSAlan Cox sysctl_swap_fragmentation(SYSCTL_HANDLER_ARGS) 832d027ed2eSAlan Cox { 833d027ed2eSAlan Cox struct sbuf sbuf; 834d027ed2eSAlan Cox struct swdevt *sp; 835d027ed2eSAlan Cox const char *devname; 836d027ed2eSAlan Cox int error; 837d027ed2eSAlan Cox 838d027ed2eSAlan Cox error = sysctl_wire_old_buffer(req, 0); 839d027ed2eSAlan Cox if (error != 0) 840d027ed2eSAlan Cox return (error); 841d027ed2eSAlan Cox sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 842d027ed2eSAlan Cox mtx_lock(&sw_dev_mtx); 843d027ed2eSAlan Cox TAILQ_FOREACH(sp, &swtailq, sw_list) { 844d027ed2eSAlan Cox if (vn_isdisk(sp->sw_vp, NULL)) 845d027ed2eSAlan Cox devname = devtoname(sp->sw_vp->v_rdev); 846d027ed2eSAlan Cox else 847d027ed2eSAlan Cox devname = "[file]"; 848d027ed2eSAlan Cox sbuf_printf(&sbuf, "\nFree space on device %s:\n", devname); 849d027ed2eSAlan Cox blist_stats(sp->sw_blist, &sbuf); 850d027ed2eSAlan Cox } 851d027ed2eSAlan Cox mtx_unlock(&sw_dev_mtx); 852d027ed2eSAlan Cox error = sbuf_finish(&sbuf); 853d027ed2eSAlan Cox sbuf_delete(&sbuf); 854d027ed2eSAlan Cox return (error); 855d027ed2eSAlan Cox } 856d027ed2eSAlan Cox 857d027ed2eSAlan Cox /* 8581c7c3c6aSMatthew Dillon * SWAP_PAGER_FREESPACE() - frees swap blocks associated with a page 8591c7c3c6aSMatthew Dillon * range within an object. 8601c7c3c6aSMatthew Dillon * 8611c7c3c6aSMatthew Dillon * This is a globally accessible routine. 8621c7c3c6aSMatthew Dillon * 8631c7c3c6aSMatthew Dillon * This routine removes swapblk assignments from swap metadata. 8641c7c3c6aSMatthew Dillon * 8651c7c3c6aSMatthew Dillon * The external callers of this routine typically have already destroyed 8661c7c3c6aSMatthew Dillon * or renamed vm_page_t's associated with this range in the object so 8671c7c3c6aSMatthew Dillon * we should be ok. 868c25673ffSAttilio Rao * 869c25673ffSAttilio Rao * The object must be locked. 87026f9a767SRodney W. Grimes */ 87126f9a767SRodney W. Grimes void 8722f249180SPoul-Henning Kamp swap_pager_freespace(vm_object_t object, vm_pindex_t start, vm_size_t size) 87326f9a767SRodney W. Grimes { 87423955314SAlfred Perlstein 8751c7c3c6aSMatthew Dillon swp_pager_meta_free(object, start, size); 8764dcc5c2dSMatthew Dillon } 8774dcc5c2dSMatthew Dillon 8784dcc5c2dSMatthew Dillon /* 8794dcc5c2dSMatthew Dillon * SWAP_PAGER_RESERVE() - reserve swap blocks in object 8804dcc5c2dSMatthew Dillon * 8814dcc5c2dSMatthew Dillon * Assigns swap blocks to the specified range within the object. The 88256ce850bSKonstantin Belousov * swap blocks are not zeroed. Any previous swap assignment is destroyed. 8834dcc5c2dSMatthew Dillon * 8844dcc5c2dSMatthew Dillon * Returns 0 on success, -1 on failure. 8854dcc5c2dSMatthew Dillon */ 8864dcc5c2dSMatthew Dillon int 8874dcc5c2dSMatthew Dillon swap_pager_reserve(vm_object_t object, vm_pindex_t start, vm_size_t size) 8884dcc5c2dSMatthew Dillon { 8894dcc5c2dSMatthew Dillon int n = 0; 8904dcc5c2dSMatthew Dillon daddr_t blk = SWAPBLK_NONE; 8914dcc5c2dSMatthew Dillon vm_pindex_t beg = start; /* save start index */ 89278f1deefSAlan Cox daddr_t addr, n_free, s_free; 8934dcc5c2dSMatthew Dillon 89478f1deefSAlan Cox swp_pager_init_freerange(&s_free, &n_free); 89589f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 8964dcc5c2dSMatthew Dillon while (size) { 8974dcc5c2dSMatthew Dillon if (n == 0) { 8984dcc5c2dSMatthew Dillon n = BLIST_MAX_ALLOC; 8994dcc5c2dSMatthew Dillon while ((blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE) { 9004dcc5c2dSMatthew Dillon n >>= 1; 9014dcc5c2dSMatthew Dillon if (n == 0) { 9024dcc5c2dSMatthew Dillon swp_pager_meta_free(object, beg, start - beg); 90389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 9044dcc5c2dSMatthew Dillon return (-1); 9054dcc5c2dSMatthew Dillon } 9064dcc5c2dSMatthew Dillon } 9074dcc5c2dSMatthew Dillon } 90878f1deefSAlan Cox addr = swp_pager_meta_build(object, start, blk); 90978f1deefSAlan Cox if (addr != SWAPBLK_NONE) 91078f1deefSAlan Cox swp_pager_update_freerange(&s_free, &n_free, addr); 9114dcc5c2dSMatthew Dillon --size; 9124dcc5c2dSMatthew Dillon ++start; 9134dcc5c2dSMatthew Dillon ++blk; 9144dcc5c2dSMatthew Dillon --n; 9154dcc5c2dSMatthew Dillon } 91678f1deefSAlan Cox swp_pager_freeswapspace(s_free, n_free); 9174dcc5c2dSMatthew Dillon swp_pager_meta_free(object, start, n); 91889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 9194dcc5c2dSMatthew Dillon return (0); 92026f9a767SRodney W. Grimes } 92126f9a767SRodney W. Grimes 9220a47b48bSJohn Dyson /* 9231c7c3c6aSMatthew Dillon * SWAP_PAGER_COPY() - copy blocks from source pager to destination pager 9241c7c3c6aSMatthew Dillon * and destroy the source. 9251c7c3c6aSMatthew Dillon * 9261c7c3c6aSMatthew Dillon * Copy any valid swapblks from the source to the destination. In 9271c7c3c6aSMatthew Dillon * cases where both the source and destination have a valid swapblk, 9281c7c3c6aSMatthew Dillon * we keep the destination's. 9291c7c3c6aSMatthew Dillon * 93015523cf7SKonstantin Belousov * This routine is allowed to sleep. It may sleep allocating metadata 9311c7c3c6aSMatthew Dillon * indirectly through swp_pager_meta_build() or if paging is still in 9321c7c3c6aSMatthew Dillon * progress on the source. 9331c7c3c6aSMatthew Dillon * 9341c7c3c6aSMatthew Dillon * The source object contains no vm_page_t's (which is just as well) 9351c7c3c6aSMatthew Dillon * 9361c7c3c6aSMatthew Dillon * The source object is of type OBJT_SWAP. 9371c7c3c6aSMatthew Dillon * 93815523cf7SKonstantin Belousov * The source and destination objects must be locked. 93915523cf7SKonstantin Belousov * Both object locks may temporarily be released. 94026f9a767SRodney W. Grimes */ 94126f9a767SRodney W. Grimes void 9422f249180SPoul-Henning Kamp swap_pager_copy(vm_object_t srcobject, vm_object_t dstobject, 9432f249180SPoul-Henning Kamp vm_pindex_t offset, int destroysource) 94426f9a767SRodney W. Grimes { 945a316d390SJohn Dyson vm_pindex_t i; 94678f1deefSAlan Cox daddr_t dstaddr, n_free, s_free, srcaddr; 9474dcc5c2dSMatthew Dillon 94889f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(srcobject); 94989f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(dstobject); 9500cddd8f0SMatthew Dillon 95126f9a767SRodney W. Grimes /* 9521c7c3c6aSMatthew Dillon * If destroysource is set, we remove the source object from the 9531c7c3c6aSMatthew Dillon * swap_pager internal queue now. 95426f9a767SRodney W. Grimes */ 955eb4d6a1bSKonstantin Belousov if (destroysource && srcobject->handle != NULL) { 956eb4d6a1bSKonstantin Belousov vm_object_pip_add(srcobject, 1); 957eb4d6a1bSKonstantin Belousov VM_OBJECT_WUNLOCK(srcobject); 958eb4d6a1bSKonstantin Belousov vm_object_pip_add(dstobject, 1); 959eb4d6a1bSKonstantin Belousov VM_OBJECT_WUNLOCK(dstobject); 960eb4d6a1bSKonstantin Belousov sx_xlock(&sw_alloc_sx); 961eb4d6a1bSKonstantin Belousov TAILQ_REMOVE(NOBJLIST(srcobject->handle), srcobject, 962eb4d6a1bSKonstantin Belousov pager_object_list); 963eb4d6a1bSKonstantin Belousov sx_xunlock(&sw_alloc_sx); 964eb4d6a1bSKonstantin Belousov VM_OBJECT_WLOCK(dstobject); 965eb4d6a1bSKonstantin Belousov vm_object_pip_wakeup(dstobject); 966eb4d6a1bSKonstantin Belousov VM_OBJECT_WLOCK(srcobject); 967eb4d6a1bSKonstantin Belousov vm_object_pip_wakeup(srcobject); 968bd228075SAlan Cox } 96926f9a767SRodney W. Grimes 9701c7c3c6aSMatthew Dillon /* 9714abca9bbSAlan Cox * Transfer source to destination. 9721c7c3c6aSMatthew Dillon */ 97378f1deefSAlan Cox swp_pager_init_freerange(&s_free, &n_free); 9741c7c3c6aSMatthew Dillon for (i = 0; i < dstobject->size; ++i) { 9754abca9bbSAlan Cox srcaddr = swp_pager_meta_ctl(srcobject, i + offset, SWM_POP); 9764abca9bbSAlan Cox if (srcaddr == SWAPBLK_NONE) 9774abca9bbSAlan Cox continue; 9781c7c3c6aSMatthew Dillon dstaddr = swp_pager_meta_ctl(dstobject, i, 0); 97978f1deefSAlan Cox if (dstaddr != SWAPBLK_NONE) { 98078f1deefSAlan Cox /* 98178f1deefSAlan Cox * Destination has valid swapblk or it is represented 98278f1deefSAlan Cox * by a resident page. We destroy the source block. 98378f1deefSAlan Cox */ 98478f1deefSAlan Cox swp_pager_update_freerange(&s_free, &n_free, srcaddr); 98578f1deefSAlan Cox continue; 98678f1deefSAlan Cox } 98778f1deefSAlan Cox 9881c7c3c6aSMatthew Dillon /* 9891c7c3c6aSMatthew Dillon * Destination has no swapblk and is not resident, 9901c7c3c6aSMatthew Dillon * copy source. 9914abca9bbSAlan Cox * 992c7c8dd7eSAlan Cox * swp_pager_meta_build() can sleep. 993c7c8dd7eSAlan Cox */ 994c7c8dd7eSAlan Cox vm_object_pip_add(srcobject, 1); 99589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(srcobject); 996c7c8dd7eSAlan Cox vm_object_pip_add(dstobject, 1); 99778f1deefSAlan Cox dstaddr = swp_pager_meta_build(dstobject, i, srcaddr); 99878f1deefSAlan Cox KASSERT(dstaddr == SWAPBLK_NONE, 99978f1deefSAlan Cox ("Unexpected destination swapblk")); 1000c7c8dd7eSAlan Cox vm_object_pip_wakeup(dstobject); 100189f6b863SAttilio Rao VM_OBJECT_WLOCK(srcobject); 1002c7c8dd7eSAlan Cox vm_object_pip_wakeup(srcobject); 10031c7c3c6aSMatthew Dillon } 100478f1deefSAlan Cox swp_pager_freeswapspace(s_free, n_free); 100526f9a767SRodney W. Grimes 100626f9a767SRodney W. Grimes /* 10071c7c3c6aSMatthew Dillon * Free left over swap blocks in source. 10081c7c3c6aSMatthew Dillon * 1009763df3ecSPedro F. Giffuni * We have to revert the type to OBJT_DEFAULT so we do not accidentally 10101c7c3c6aSMatthew Dillon * double-remove the object from the swap queues. 101126f9a767SRodney W. Grimes */ 1012c0877f10SJohn Dyson if (destroysource) { 10131c7c3c6aSMatthew Dillon swp_pager_meta_free_all(srcobject); 10141c7c3c6aSMatthew Dillon /* 10151c7c3c6aSMatthew Dillon * Reverting the type is not necessary, the caller is going 10161c7c3c6aSMatthew Dillon * to destroy srcobject directly, but I'm doing it here 1017956f3135SPhilippe Charnier * for consistency since we've removed the object from its 10181c7c3c6aSMatthew Dillon * queues. 10191c7c3c6aSMatthew Dillon */ 10201c7c3c6aSMatthew Dillon srcobject->type = OBJT_DEFAULT; 1021c0877f10SJohn Dyson } 102226f9a767SRodney W. Grimes } 102326f9a767SRodney W. Grimes 1024df8bae1dSRodney W. Grimes /* 10251c7c3c6aSMatthew Dillon * SWAP_PAGER_HASPAGE() - determine if we have good backing store for 10261c7c3c6aSMatthew Dillon * the requested page. 10271c7c3c6aSMatthew Dillon * 10281c7c3c6aSMatthew Dillon * We determine whether good backing store exists for the requested 10291c7c3c6aSMatthew Dillon * page and return TRUE if it does, FALSE if it doesn't. 10301c7c3c6aSMatthew Dillon * 10311c7c3c6aSMatthew Dillon * If TRUE, we also try to determine how much valid, contiguous backing 1032915d1b71SMark Johnston * store exists before and after the requested page. 1033df8bae1dSRodney W. Grimes */ 10345ea4972cSAlan Cox static boolean_t 1035915d1b71SMark Johnston swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, 1036915d1b71SMark Johnston int *after) 103726f9a767SRodney W. Grimes { 1038915d1b71SMark Johnston daddr_t blk, blk0; 1039915d1b71SMark Johnston int i; 104026f9a767SRodney W. Grimes 1041c25673ffSAttilio Rao VM_OBJECT_ASSERT_LOCKED(object); 1042915d1b71SMark Johnston 10431c7c3c6aSMatthew Dillon /* 10441c7c3c6aSMatthew Dillon * do we have good backing store at the requested index ? 10451c7c3c6aSMatthew Dillon */ 10461c7c3c6aSMatthew Dillon blk0 = swp_pager_meta_ctl(object, pindex, 0); 10474dcc5c2dSMatthew Dillon if (blk0 == SWAPBLK_NONE) { 10481c7c3c6aSMatthew Dillon if (before) 104924a1cce3SDavid Greenman *before = 0; 10501c7c3c6aSMatthew Dillon if (after) 105124a1cce3SDavid Greenman *after = 0; 105226f9a767SRodney W. Grimes return (FALSE); 105326f9a767SRodney W. Grimes } 105426f9a767SRodney W. Grimes 105526f9a767SRodney W. Grimes /* 10561c7c3c6aSMatthew Dillon * find backwards-looking contiguous good backing store 1057e47ed70bSJohn Dyson */ 10581c7c3c6aSMatthew Dillon if (before != NULL) { 1059915d1b71SMark Johnston for (i = 1; i < SWB_NPAGES; i++) { 10601c7c3c6aSMatthew Dillon if (i > pindex) 10611c7c3c6aSMatthew Dillon break; 10621c7c3c6aSMatthew Dillon blk = swp_pager_meta_ctl(object, pindex - i, 0); 10631c7c3c6aSMatthew Dillon if (blk != blk0 - i) 10641c7c3c6aSMatthew Dillon break; 1065ffc82b0aSJohn Dyson } 1066915d1b71SMark Johnston *before = i - 1; 106726f9a767SRodney W. Grimes } 106826f9a767SRodney W. Grimes 106926f9a767SRodney W. Grimes /* 10701c7c3c6aSMatthew Dillon * find forward-looking contiguous good backing store 107126f9a767SRodney W. Grimes */ 10721c7c3c6aSMatthew Dillon if (after != NULL) { 1073915d1b71SMark Johnston for (i = 1; i < SWB_NPAGES; i++) { 10741c7c3c6aSMatthew Dillon blk = swp_pager_meta_ctl(object, pindex + i, 0); 10751c7c3c6aSMatthew Dillon if (blk != blk0 + i) 10761c7c3c6aSMatthew Dillon break; 107726f9a767SRodney W. Grimes } 1078915d1b71SMark Johnston *after = i - 1; 10791c7c3c6aSMatthew Dillon } 10801c7c3c6aSMatthew Dillon return (TRUE); 10811c7c3c6aSMatthew Dillon } 10821c7c3c6aSMatthew Dillon 10831c7c3c6aSMatthew Dillon /* 10841c7c3c6aSMatthew Dillon * SWAP_PAGER_PAGE_UNSWAPPED() - remove swap backing store related to page 10851c7c3c6aSMatthew Dillon * 10861c7c3c6aSMatthew Dillon * This removes any associated swap backing store, whether valid or 10871c7c3c6aSMatthew Dillon * not, from the page. 10881c7c3c6aSMatthew Dillon * 10891c7c3c6aSMatthew Dillon * This routine is typically called when a page is made dirty, at 10901c7c3c6aSMatthew Dillon * which point any associated swap can be freed. MADV_FREE also 10911c7c3c6aSMatthew Dillon * calls us in a special-case situation 10921c7c3c6aSMatthew Dillon * 10931c7c3c6aSMatthew Dillon * NOTE!!! If the page is clean and the swap was valid, the caller 10941c7c3c6aSMatthew Dillon * should make the page dirty before calling this routine. This routine 10951c7c3c6aSMatthew Dillon * does NOT change the m->dirty status of the page. Also: MADV_FREE 10961c7c3c6aSMatthew Dillon * depends on it. 10971c7c3c6aSMatthew Dillon * 109815523cf7SKonstantin Belousov * This routine may not sleep. 1099c25673ffSAttilio Rao * 1100c25673ffSAttilio Rao * The object containing the page must be locked. 11011c7c3c6aSMatthew Dillon */ 11021c7c3c6aSMatthew Dillon static void 11032f249180SPoul-Henning Kamp swap_pager_unswapped(vm_page_t m) 11041c7c3c6aSMatthew Dillon { 11054abca9bbSAlan Cox daddr_t srcaddr; 11062f249180SPoul-Henning Kamp 11074abca9bbSAlan Cox srcaddr = swp_pager_meta_ctl(m->object, m->pindex, SWM_POP); 11084abca9bbSAlan Cox if (srcaddr != SWAPBLK_NONE) 11094abca9bbSAlan Cox swp_pager_freeswapspace(srcaddr, 1); 11101c7c3c6aSMatthew Dillon } 11111c7c3c6aSMatthew Dillon 11121c7c3c6aSMatthew Dillon /* 1113915d1b71SMark Johnston * swap_pager_getpages() - bring pages in from swap 11141c7c3c6aSMatthew Dillon * 11153f060b60SMark Johnston * Attempt to page in the pages in array "ma" of length "count". The 11163f060b60SMark Johnston * caller may optionally specify that additional pages preceding and 11173f060b60SMark Johnston * succeeding the specified range be paged in. The number of such pages 11183f060b60SMark Johnston * is returned in the "rbehind" and "rahead" parameters, and they will 11193f060b60SMark Johnston * be in the inactive queue upon return. 11201c7c3c6aSMatthew Dillon * 11213f060b60SMark Johnston * The pages in "ma" must be busied and will remain busied upon return. 11221c7c3c6aSMatthew Dillon */ 1123f708ef1bSPoul-Henning Kamp static int 11243f060b60SMark Johnston swap_pager_getpages(vm_object_t object, vm_page_t *ma, int count, int *rbehind, 1125b0cd2017SGleb Smirnoff int *rahead) 1126df8bae1dSRodney W. Grimes { 11271c7c3c6aSMatthew Dillon struct buf *bp; 1128b7b8a096SKonstantin Belousov vm_page_t bm, mpred, msucc, p; 1129915d1b71SMark Johnston vm_pindex_t pindex; 11301c7c3c6aSMatthew Dillon daddr_t blk; 1131b7b8a096SKonstantin Belousov int i, maxahead, maxbehind, reqcount; 11320d94caffSDavid Greenman 1133915d1b71SMark Johnston reqcount = count; 11341c7c3c6aSMatthew Dillon 1135b7b8a096SKonstantin Belousov /* 1136b7b8a096SKonstantin Belousov * Determine the final number of read-behind pages and 1137b7b8a096SKonstantin Belousov * allocate them BEFORE releasing the object lock. Otherwise, 1138b7b8a096SKonstantin Belousov * there can be a problematic race with vm_object_split(). 1139b7b8a096SKonstantin Belousov * Specifically, vm_object_split() might first transfer pages 1140b7b8a096SKonstantin Belousov * that precede ma[0] in the current object to a new object, 1141b7b8a096SKonstantin Belousov * and then this function incorrectly recreates those pages as 1142b7b8a096SKonstantin Belousov * read-behind pages in the current object. 1143b7b8a096SKonstantin Belousov */ 1144b7b8a096SKonstantin Belousov if (!swap_pager_haspage(object, ma[0]->pindex, &maxbehind, &maxahead)) 1145915d1b71SMark Johnston return (VM_PAGER_FAIL); 1146915d1b71SMark Johnston 1147915d1b71SMark Johnston /* 1148915d1b71SMark Johnston * Clip the readahead and readbehind ranges to exclude resident pages. 1149915d1b71SMark Johnston */ 1150915d1b71SMark Johnston if (rahead != NULL) { 1151dd9cb6daSMark Johnston KASSERT(reqcount - 1 <= maxahead, 1152915d1b71SMark Johnston ("page count %d extends beyond swap block", reqcount)); 1153dd9cb6daSMark Johnston *rahead = imin(*rahead, maxahead - (reqcount - 1)); 11543f060b60SMark Johnston pindex = ma[reqcount - 1]->pindex; 11553f060b60SMark Johnston msucc = TAILQ_NEXT(ma[reqcount - 1], listq); 1156915d1b71SMark Johnston if (msucc != NULL && msucc->pindex - pindex - 1 < *rahead) 1157915d1b71SMark Johnston *rahead = msucc->pindex - pindex - 1; 1158915d1b71SMark Johnston } 1159915d1b71SMark Johnston if (rbehind != NULL) { 1160dd9cb6daSMark Johnston *rbehind = imin(*rbehind, maxbehind); 11613f060b60SMark Johnston pindex = ma[0]->pindex; 11623f060b60SMark Johnston mpred = TAILQ_PREV(ma[0], pglist, listq); 1163915d1b71SMark Johnston if (mpred != NULL && pindex - mpred->pindex - 1 < *rbehind) 1164915d1b71SMark Johnston *rbehind = pindex - mpred->pindex - 1; 1165915d1b71SMark Johnston } 1166915d1b71SMark Johnston 1167b7b8a096SKonstantin Belousov bm = ma[0]; 1168b7b8a096SKonstantin Belousov for (i = 0; i < count; i++) 1169b7b8a096SKonstantin Belousov ma[i]->oflags |= VPO_SWAPINPROG; 1170b7b8a096SKonstantin Belousov 1171915d1b71SMark Johnston /* 1172915d1b71SMark Johnston * Allocate readahead and readbehind pages. 1173915d1b71SMark Johnston */ 1174b7b8a096SKonstantin Belousov if (rbehind != NULL) { 1175b7b8a096SKonstantin Belousov for (i = 1; i <= *rbehind; i++) { 11763f060b60SMark Johnston p = vm_page_alloc(object, ma[0]->pindex - i, 11777667839aSAlan Cox VM_ALLOC_NORMAL); 1178b7b8a096SKonstantin Belousov if (p == NULL) 1179915d1b71SMark Johnston break; 1180b7b8a096SKonstantin Belousov p->oflags |= VPO_SWAPINPROG; 1181b7b8a096SKonstantin Belousov bm = p; 1182915d1b71SMark Johnston } 1183b7b8a096SKonstantin Belousov *rbehind = i - 1; 1184915d1b71SMark Johnston } 1185915d1b71SMark Johnston if (rahead != NULL) { 1186915d1b71SMark Johnston for (i = 0; i < *rahead; i++) { 1187915d1b71SMark Johnston p = vm_page_alloc(object, 11883f060b60SMark Johnston ma[reqcount - 1]->pindex + i + 1, VM_ALLOC_NORMAL); 1189915d1b71SMark Johnston if (p == NULL) 1190915d1b71SMark Johnston break; 1191b7b8a096SKonstantin Belousov p->oflags |= VPO_SWAPINPROG; 1192915d1b71SMark Johnston } 1193915d1b71SMark Johnston *rahead = i; 1194915d1b71SMark Johnston } 1195915d1b71SMark Johnston if (rbehind != NULL) 1196915d1b71SMark Johnston count += *rbehind; 1197915d1b71SMark Johnston if (rahead != NULL) 1198915d1b71SMark Johnston count += *rahead; 1199915d1b71SMark Johnston 1200915d1b71SMark Johnston vm_object_pip_add(object, count); 1201915d1b71SMark Johnston 1202b7b8a096SKonstantin Belousov pindex = bm->pindex; 1203915d1b71SMark Johnston blk = swp_pager_meta_ctl(object, pindex, 0); 1204915d1b71SMark Johnston KASSERT(blk != SWAPBLK_NONE, 1205915d1b71SMark Johnston ("no swap blocking containing %p(%jx)", object, (uintmax_t)pindex)); 1206915d1b71SMark Johnston 1207915d1b71SMark Johnston VM_OBJECT_WUNLOCK(object); 1208*756a5412SGleb Smirnoff bp = uma_zalloc(swrbuf_zone, M_WAITOK); 1209b7b8a096SKonstantin Belousov /* Pages cannot leave the object while busy. */ 1210b7b8a096SKonstantin Belousov for (i = 0, p = bm; i < count; i++, p = TAILQ_NEXT(p, listq)) { 1211b7b8a096SKonstantin Belousov MPASS(p->pindex == bm->pindex + i); 1212b7b8a096SKonstantin Belousov bp->b_pages[i] = p; 1213b7b8a096SKonstantin Belousov } 1214915d1b71SMark Johnston 1215915d1b71SMark Johnston bp->b_flags |= B_PAGING; 121621144e3bSPoul-Henning Kamp bp->b_iocmd = BIO_READ; 12171c7c3c6aSMatthew Dillon bp->b_iodone = swp_pager_async_iodone; 1218fdcc1cc0SJohn Baldwin bp->b_rcred = crhold(thread0.td_ucred); 1219fdcc1cc0SJohn Baldwin bp->b_wcred = crhold(thread0.td_ucred); 1220b0cd2017SGleb Smirnoff bp->b_blkno = blk; 1221b0cd2017SGleb Smirnoff bp->b_bcount = PAGE_SIZE * count; 1222b0cd2017SGleb Smirnoff bp->b_bufsize = PAGE_SIZE * count; 1223b0cd2017SGleb Smirnoff bp->b_npages = count; 1224915d1b71SMark Johnston bp->b_pgbefore = rbehind != NULL ? *rbehind : 0; 1225915d1b71SMark Johnston bp->b_pgafter = rahead != NULL ? *rahead : 0; 122626f9a767SRodney W. Grimes 122783c9dea1SGleb Smirnoff VM_CNT_INC(v_swapin); 122883c9dea1SGleb Smirnoff VM_CNT_ADD(v_swappgsin, count); 12291c7c3c6aSMatthew Dillon 12301c7c3c6aSMatthew Dillon /* 12311c7c3c6aSMatthew Dillon * perform the I/O. NOTE!!! bp cannot be considered valid after 12321c7c3c6aSMatthew Dillon * this point because we automatically release it on completion. 12331c7c3c6aSMatthew Dillon * Instead, we look at the one page we are interested in which we 12341c7c3c6aSMatthew Dillon * still hold a lock on even through the I/O completion. 12351c7c3c6aSMatthew Dillon * 12363f060b60SMark Johnston * The other pages in our ma[] array are also released on completion, 12371c7c3c6aSMatthew Dillon * so we cannot assume they are valid anymore either. 12381c7c3c6aSMatthew Dillon * 1239c37a77eeSPoul-Henning Kamp * NOTE: b_blkno is destroyed by the call to swapdev_strategy 12401c7c3c6aSMatthew Dillon */ 1241b890cb2cSPeter Wemm BUF_KERNPROC(bp); 12424b03903aSPoul-Henning Kamp swp_pager_strategy(bp); 124326f9a767SRodney W. Grimes 124426f9a767SRodney W. Grimes /* 1245915d1b71SMark Johnston * Wait for the pages we want to complete. VPO_SWAPINPROG is always 12461c7c3c6aSMatthew Dillon * cleared on completion. If an I/O error occurs, SWAPBLK_NONE 1247915d1b71SMark Johnston * is set in the metadata for each page in the request. 124826f9a767SRodney W. Grimes */ 124989f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 12503f060b60SMark Johnston while ((ma[0]->oflags & VPO_SWAPINPROG) != 0) { 12513f060b60SMark Johnston ma[0]->oflags |= VPO_SWAPSLEEP; 125283c9dea1SGleb Smirnoff VM_CNT_INC(v_intrans); 1253c7aebda8SAttilio Rao if (VM_OBJECT_SLEEP(object, &object->paging_in_progress, PSWP, 1254c7aebda8SAttilio Rao "swread", hz * 20)) { 12559bd86a98SBruce M Simpson printf( 1256c5690651SPoul-Henning Kamp "swap_pager: indefinite wait buffer: bufobj: %p, blkno: %jd, size: %ld\n", 1257c5690651SPoul-Henning Kamp bp->b_bufobj, (intmax_t)bp->b_blkno, bp->b_bcount); 12581c7c3c6aSMatthew Dillon } 12591b119d9dSDavid Greenman } 126026f9a767SRodney W. Grimes 126126f9a767SRodney W. Grimes /* 1262b0cd2017SGleb Smirnoff * If we had an unrecoverable read error pages will not be valid. 126326f9a767SRodney W. Grimes */ 1264915d1b71SMark Johnston for (i = 0; i < reqcount; i++) 12653f060b60SMark Johnston if (ma[i]->valid != VM_PAGE_BITS_ALL) 12661c7c3c6aSMatthew Dillon return (VM_PAGER_ERROR); 1267b0cd2017SGleb Smirnoff 12681c7c3c6aSMatthew Dillon return (VM_PAGER_OK); 12691c7c3c6aSMatthew Dillon 12701c7c3c6aSMatthew Dillon /* 12711c7c3c6aSMatthew Dillon * A final note: in a low swap situation, we cannot deallocate swap 12721c7c3c6aSMatthew Dillon * and mark a page dirty here because the caller is likely to mark 12731c7c3c6aSMatthew Dillon * the page clean when we return, causing the page to possibly revert 12741c7c3c6aSMatthew Dillon * to all-zero's later. 12751c7c3c6aSMatthew Dillon */ 1276df8bae1dSRodney W. Grimes } 1277df8bae1dSRodney W. Grimes 12781c7c3c6aSMatthew Dillon /* 127990effb23SGleb Smirnoff * swap_pager_getpages_async(): 128090effb23SGleb Smirnoff * 128190effb23SGleb Smirnoff * Right now this is emulation of asynchronous operation on top of 128290effb23SGleb Smirnoff * swap_pager_getpages(). 128390effb23SGleb Smirnoff */ 128490effb23SGleb Smirnoff static int 12853f060b60SMark Johnston swap_pager_getpages_async(vm_object_t object, vm_page_t *ma, int count, 1286b0cd2017SGleb Smirnoff int *rbehind, int *rahead, pgo_getpages_iodone_t iodone, void *arg) 128790effb23SGleb Smirnoff { 128890effb23SGleb Smirnoff int r, error; 128990effb23SGleb Smirnoff 12903f060b60SMark Johnston r = swap_pager_getpages(object, ma, count, rbehind, rahead); 129190effb23SGleb Smirnoff VM_OBJECT_WUNLOCK(object); 129290effb23SGleb Smirnoff switch (r) { 129390effb23SGleb Smirnoff case VM_PAGER_OK: 129490effb23SGleb Smirnoff error = 0; 129590effb23SGleb Smirnoff break; 129690effb23SGleb Smirnoff case VM_PAGER_ERROR: 129790effb23SGleb Smirnoff error = EIO; 129890effb23SGleb Smirnoff break; 129990effb23SGleb Smirnoff case VM_PAGER_FAIL: 130090effb23SGleb Smirnoff error = EINVAL; 130190effb23SGleb Smirnoff break; 130290effb23SGleb Smirnoff default: 1303d9328101SGleb Smirnoff panic("unhandled swap_pager_getpages() error %d", r); 130490effb23SGleb Smirnoff } 13053f060b60SMark Johnston (iodone)(arg, ma, count, error); 130690effb23SGleb Smirnoff VM_OBJECT_WLOCK(object); 130790effb23SGleb Smirnoff 130890effb23SGleb Smirnoff return (r); 130990effb23SGleb Smirnoff } 131090effb23SGleb Smirnoff 131190effb23SGleb Smirnoff /* 13121c7c3c6aSMatthew Dillon * swap_pager_putpages: 13131c7c3c6aSMatthew Dillon * 13141c7c3c6aSMatthew Dillon * Assign swap (if necessary) and initiate I/O on the specified pages. 13151c7c3c6aSMatthew Dillon * 13161c7c3c6aSMatthew Dillon * We support both OBJT_DEFAULT and OBJT_SWAP objects. DEFAULT objects 13171c7c3c6aSMatthew Dillon * are automatically converted to SWAP objects. 13181c7c3c6aSMatthew Dillon * 1319ea3aecf5SPeter Wemm * In a low memory situation we may block in VOP_STRATEGY(), but the new 13201c7c3c6aSMatthew Dillon * vm_page reservation system coupled with properly written VFS devices 13211c7c3c6aSMatthew Dillon * should ensure that no low-memory deadlock occurs. This is an area 13221c7c3c6aSMatthew Dillon * which needs work. 13231c7c3c6aSMatthew Dillon * 13241c7c3c6aSMatthew Dillon * The parent has N vm_object_pip_add() references prior to 13251c7c3c6aSMatthew Dillon * calling us and will remove references for rtvals[] that are 13261c7c3c6aSMatthew Dillon * not set to VM_PAGER_PEND. We need to remove the rest on I/O 13271c7c3c6aSMatthew Dillon * completion. 13281c7c3c6aSMatthew Dillon * 13291c7c3c6aSMatthew Dillon * The parent has soft-busy'd the pages it passes us and will unbusy 13301c7c3c6aSMatthew Dillon * those whos rtvals[] entry is not set to VM_PAGER_PEND on return. 13311c7c3c6aSMatthew Dillon * We need to unbusy the rest on I/O completion. 13321c7c3c6aSMatthew Dillon */ 1333d635a37fSWarner Losh static void 13343f060b60SMark Johnston swap_pager_putpages(vm_object_t object, vm_page_t *ma, int count, 1335e065e87cSKonstantin Belousov int flags, int *rtvals) 1336df8bae1dSRodney W. Grimes { 1337e065e87cSKonstantin Belousov int i, n; 1338e065e87cSKonstantin Belousov boolean_t sync; 133978f1deefSAlan Cox daddr_t addr, n_free, s_free; 1340df8bae1dSRodney W. Grimes 134178f1deefSAlan Cox swp_pager_init_freerange(&s_free, &n_free); 13423f060b60SMark Johnston if (count && ma[0]->object != object) { 13437036145bSMaxim Konovalov panic("swap_pager_putpages: object mismatch %p/%p", 13441c7c3c6aSMatthew Dillon object, 13453f060b60SMark Johnston ma[0]->object 13461c7c3c6aSMatthew Dillon ); 13471c7c3c6aSMatthew Dillon } 1348ee3dc7d7SAlan Cox 13491c7c3c6aSMatthew Dillon /* 13501c7c3c6aSMatthew Dillon * Step 1 13511c7c3c6aSMatthew Dillon * 13521c7c3c6aSMatthew Dillon * Turn object into OBJT_SWAP 13531c7c3c6aSMatthew Dillon * check for bogus sysops 13541c7c3c6aSMatthew Dillon * force sync if not pageout process 13551c7c3c6aSMatthew Dillon */ 135678f1deefSAlan Cox if (object->type != OBJT_SWAP) { 135778f1deefSAlan Cox addr = swp_pager_meta_build(object, 0, SWAPBLK_NONE); 135878f1deefSAlan Cox KASSERT(addr == SWAPBLK_NONE, 135978f1deefSAlan Cox ("unexpected object swap block")); 136078f1deefSAlan Cox } 136189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1362e47ed70bSJohn Dyson 1363e065e87cSKonstantin Belousov n = 0; 1364e47ed70bSJohn Dyson if (curproc != pageproc) 1365e47ed70bSJohn Dyson sync = TRUE; 1366e065e87cSKonstantin Belousov else 1367e065e87cSKonstantin Belousov sync = (flags & VM_PAGER_PUT_SYNC) != 0; 136826f9a767SRodney W. Grimes 13691c7c3c6aSMatthew Dillon /* 13701c7c3c6aSMatthew Dillon * Step 2 13711c7c3c6aSMatthew Dillon * 13721c7c3c6aSMatthew Dillon * Assign swap blocks and issue I/O. We reallocate swap on the fly. 13731c7c3c6aSMatthew Dillon * The page is left dirty until the pageout operation completes 13741c7c3c6aSMatthew Dillon * successfully. 13751c7c3c6aSMatthew Dillon */ 13761c7c3c6aSMatthew Dillon for (i = 0; i < count; i += n) { 13771c7c3c6aSMatthew Dillon int j; 13781c7c3c6aSMatthew Dillon struct buf *bp; 1379a316d390SJohn Dyson daddr_t blk; 138026f9a767SRodney W. Grimes 1381df8bae1dSRodney W. Grimes /* 13821c7c3c6aSMatthew Dillon * Maximum I/O size is limited by a number of factors. 1383df8bae1dSRodney W. Grimes */ 13841c7c3c6aSMatthew Dillon n = min(BLIST_MAX_ALLOC, count - i); 1385327f4e83SMatthew Dillon n = min(n, nsw_cluster_max); 13861c7c3c6aSMatthew Dillon 138726f9a767SRodney W. Grimes /* 13881c7c3c6aSMatthew Dillon * Get biggest block of swap we can. If we fail, fall 13891c7c3c6aSMatthew Dillon * back and try to allocate a smaller block. Don't go 13901c7c3c6aSMatthew Dillon * overboard trying to allocate space if it would overly 13911c7c3c6aSMatthew Dillon * fragment swap. 139226f9a767SRodney W. Grimes */ 13931c7c3c6aSMatthew Dillon while ( 13941c7c3c6aSMatthew Dillon (blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE && 13951c7c3c6aSMatthew Dillon n > 4 13961c7c3c6aSMatthew Dillon ) { 13971c7c3c6aSMatthew Dillon n >>= 1; 139826f9a767SRodney W. Grimes } 13991c7c3c6aSMatthew Dillon if (blk == SWAPBLK_NONE) { 14004dcc5c2dSMatthew Dillon for (j = 0; j < n; ++j) 14011c7c3c6aSMatthew Dillon rtvals[i+j] = VM_PAGER_FAIL; 14021c7c3c6aSMatthew Dillon continue; 140326f9a767SRodney W. Grimes } 140426f9a767SRodney W. Grimes 140526f9a767SRodney W. Grimes /* 14061c7c3c6aSMatthew Dillon * All I/O parameters have been satisfied, build the I/O 14071c7c3c6aSMatthew Dillon * request and assign the swap space. 140826f9a767SRodney W. Grimes */ 1409*756a5412SGleb Smirnoff if (sync != TRUE) { 1410*756a5412SGleb Smirnoff mtx_lock(&swbuf_mtx); 1411*756a5412SGleb Smirnoff while (nsw_wcount_async == 0) 1412*756a5412SGleb Smirnoff msleep(&nsw_wcount_async, &swbuf_mtx, PVM, 1413*756a5412SGleb Smirnoff "swbufa", 0); 1414*756a5412SGleb Smirnoff nsw_wcount_async--; 1415*756a5412SGleb Smirnoff mtx_unlock(&swbuf_mtx); 1416327f4e83SMatthew Dillon } 1417*756a5412SGleb Smirnoff bp = uma_zalloc(swwbuf_zone, M_WAITOK); 1418*756a5412SGleb Smirnoff if (sync != TRUE) 1419*756a5412SGleb Smirnoff bp->b_flags = B_ASYNC; 14205e04322aSPoul-Henning Kamp bp->b_flags |= B_PAGING; 1421912e4ae9SPoul-Henning Kamp bp->b_iocmd = BIO_WRITE; 142226f9a767SRodney W. Grimes 1423fdcc1cc0SJohn Baldwin bp->b_rcred = crhold(thread0.td_ucred); 1424fdcc1cc0SJohn Baldwin bp->b_wcred = crhold(thread0.td_ucred); 14251c7c3c6aSMatthew Dillon bp->b_bcount = PAGE_SIZE * n; 14261c7c3c6aSMatthew Dillon bp->b_bufsize = PAGE_SIZE * n; 14271c7c3c6aSMatthew Dillon bp->b_blkno = blk; 1428e47ed70bSJohn Dyson 142989f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 14301c7c3c6aSMatthew Dillon for (j = 0; j < n; ++j) { 14313f060b60SMark Johnston vm_page_t mreq = ma[i+j]; 14321c7c3c6aSMatthew Dillon 143378f1deefSAlan Cox addr = swp_pager_meta_build(mreq->object, mreq->pindex, 143478f1deefSAlan Cox blk + j); 143578f1deefSAlan Cox if (addr != SWAPBLK_NONE) 143678f1deefSAlan Cox swp_pager_update_freerange(&s_free, &n_free, 143778f1deefSAlan Cox addr); 143887b0ab69SAlan Cox MPASS(mreq->dirty == VM_PAGE_BITS_ALL); 14395786be7cSAlan Cox mreq->oflags |= VPO_SWAPINPROG; 14401c7c3c6aSMatthew Dillon bp->b_pages[j] = mreq; 14411c7c3c6aSMatthew Dillon } 144289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 14431c7c3c6aSMatthew Dillon bp->b_npages = n; 1444a5296b05SJulian Elischer /* 1445a5296b05SJulian Elischer * Must set dirty range for NFS to work. 1446a5296b05SJulian Elischer */ 1447a5296b05SJulian Elischer bp->b_dirtyoff = 0; 1448a5296b05SJulian Elischer bp->b_dirtyend = bp->b_bcount; 14491c7c3c6aSMatthew Dillon 145083c9dea1SGleb Smirnoff VM_CNT_INC(v_swapout); 145183c9dea1SGleb Smirnoff VM_CNT_ADD(v_swappgsout, bp->b_npages); 145226f9a767SRodney W. Grimes 145326f9a767SRodney W. Grimes /* 145477923df2SAlan Cox * We unconditionally set rtvals[] to VM_PAGER_PEND so that we 145577923df2SAlan Cox * can call the async completion routine at the end of a 145677923df2SAlan Cox * synchronous I/O operation. Otherwise, our caller would 145777923df2SAlan Cox * perform duplicate unbusy and wakeup operations on the page 145877923df2SAlan Cox * and object, respectively. 145977923df2SAlan Cox */ 146077923df2SAlan Cox for (j = 0; j < n; j++) 146177923df2SAlan Cox rtvals[i + j] = VM_PAGER_PEND; 146277923df2SAlan Cox 146377923df2SAlan Cox /* 14641c7c3c6aSMatthew Dillon * asynchronous 14651c7c3c6aSMatthew Dillon * 1466c37a77eeSPoul-Henning Kamp * NOTE: b_blkno is destroyed by the call to swapdev_strategy 146726f9a767SRodney W. Grimes */ 14681c7c3c6aSMatthew Dillon if (sync == FALSE) { 14691c7c3c6aSMatthew Dillon bp->b_iodone = swp_pager_async_iodone; 147067812eacSKirk McKusick BUF_KERNPROC(bp); 14714b03903aSPoul-Henning Kamp swp_pager_strategy(bp); 14721c7c3c6aSMatthew Dillon continue; 147326f9a767SRodney W. Grimes } 1474e47ed70bSJohn Dyson 147526f9a767SRodney W. Grimes /* 14761c7c3c6aSMatthew Dillon * synchronous 14771c7c3c6aSMatthew Dillon * 1478c37a77eeSPoul-Henning Kamp * NOTE: b_blkno is destroyed by the call to swapdev_strategy 14791c7c3c6aSMatthew Dillon */ 14802c840b1fSAlan Cox bp->b_iodone = bdone; 14814b03903aSPoul-Henning Kamp swp_pager_strategy(bp); 14821c7c3c6aSMatthew Dillon 14831c7c3c6aSMatthew Dillon /* 148477923df2SAlan Cox * Wait for the sync I/O to complete. 148526f9a767SRodney W. Grimes */ 14862c840b1fSAlan Cox bwait(bp, PVM, "swwrt"); 148777923df2SAlan Cox 14881c7c3c6aSMatthew Dillon /* 14891c7c3c6aSMatthew Dillon * Now that we are through with the bp, we can call the 14901c7c3c6aSMatthew Dillon * normal async completion, which frees everything up. 14911c7c3c6aSMatthew Dillon */ 14921c7c3c6aSMatthew Dillon swp_pager_async_iodone(bp); 14931c7c3c6aSMatthew Dillon } 149489f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 149578f1deefSAlan Cox swp_pager_freeswapspace(s_free, n_free); 14961c7c3c6aSMatthew Dillon } 14971c7c3c6aSMatthew Dillon 14981c7c3c6aSMatthew Dillon /* 14991c7c3c6aSMatthew Dillon * swp_pager_async_iodone: 15001c7c3c6aSMatthew Dillon * 15011c7c3c6aSMatthew Dillon * Completion routine for asynchronous reads and writes from/to swap. 15021c7c3c6aSMatthew Dillon * Also called manually by synchronous code to finish up a bp. 15031c7c3c6aSMatthew Dillon * 150415523cf7SKonstantin Belousov * This routine may not sleep. 15051c7c3c6aSMatthew Dillon */ 15061c7c3c6aSMatthew Dillon static void 15072f249180SPoul-Henning Kamp swp_pager_async_iodone(struct buf *bp) 15081c7c3c6aSMatthew Dillon { 15091c7c3c6aSMatthew Dillon int i; 15101c7c3c6aSMatthew Dillon vm_object_t object = NULL; 15111c7c3c6aSMatthew Dillon 15121c7c3c6aSMatthew Dillon /* 15131c7c3c6aSMatthew Dillon * report error 15141c7c3c6aSMatthew Dillon */ 1515c244d2deSPoul-Henning Kamp if (bp->b_ioflags & BIO_ERROR) { 15161c7c3c6aSMatthew Dillon printf( 15171c7c3c6aSMatthew Dillon "swap_pager: I/O error - %s failed; blkno %ld," 15181c7c3c6aSMatthew Dillon "size %ld, error %d\n", 151921144e3bSPoul-Henning Kamp ((bp->b_iocmd == BIO_READ) ? "pagein" : "pageout"), 15201c7c3c6aSMatthew Dillon (long)bp->b_blkno, 15211c7c3c6aSMatthew Dillon (long)bp->b_bcount, 15221c7c3c6aSMatthew Dillon bp->b_error 15231c7c3c6aSMatthew Dillon ); 15241c7c3c6aSMatthew Dillon } 15251c7c3c6aSMatthew Dillon 15261c7c3c6aSMatthew Dillon /* 152726f9a767SRodney W. Grimes * remove the mapping for kernel virtual 152826f9a767SRodney W. Grimes */ 1529fade8dd7SJeff Roberson if (buf_mapped(bp)) 15301c7c3c6aSMatthew Dillon pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages); 1531fade8dd7SJeff Roberson else 1532fade8dd7SJeff Roberson bp->b_data = bp->b_kvabase; 153326f9a767SRodney W. Grimes 153433a609ecSAlan Cox if (bp->b_npages) { 153533a609ecSAlan Cox object = bp->b_pages[0]->object; 153689f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 153733a609ecSAlan Cox } 15382965a453SKip Macy 153926f9a767SRodney W. Grimes /* 15401c7c3c6aSMatthew Dillon * cleanup pages. If an error occurs writing to swap, we are in 15411c7c3c6aSMatthew Dillon * very serious trouble. If it happens to be a disk error, though, 15421c7c3c6aSMatthew Dillon * we may be able to recover by reassigning the swap later on. So 15431c7c3c6aSMatthew Dillon * in this case we remove the m->swapblk assignment for the page 15441c7c3c6aSMatthew Dillon * but do not free it in the rlist. The errornous block(s) are thus 15451c7c3c6aSMatthew Dillon * never reallocated as swap. Redirty the page and continue. 154626f9a767SRodney W. Grimes */ 15471c7c3c6aSMatthew Dillon for (i = 0; i < bp->b_npages; ++i) { 15481c7c3c6aSMatthew Dillon vm_page_t m = bp->b_pages[i]; 1549e47ed70bSJohn Dyson 15505786be7cSAlan Cox m->oflags &= ~VPO_SWAPINPROG; 1551c7aebda8SAttilio Rao if (m->oflags & VPO_SWAPSLEEP) { 1552c7aebda8SAttilio Rao m->oflags &= ~VPO_SWAPSLEEP; 1553c7aebda8SAttilio Rao wakeup(&object->paging_in_progress); 1554c7aebda8SAttilio Rao } 1555e47ed70bSJohn Dyson 1556c244d2deSPoul-Henning Kamp if (bp->b_ioflags & BIO_ERROR) { 1557ffc82b0aSJohn Dyson /* 15581c7c3c6aSMatthew Dillon * If an error occurs I'd love to throw the swapblk 15591c7c3c6aSMatthew Dillon * away without freeing it back to swapspace, so it 15601c7c3c6aSMatthew Dillon * can never be used again. But I can't from an 15611c7c3c6aSMatthew Dillon * interrupt. 1562ffc82b0aSJohn Dyson */ 156321144e3bSPoul-Henning Kamp if (bp->b_iocmd == BIO_READ) { 15641c7c3c6aSMatthew Dillon /* 15651c7c3c6aSMatthew Dillon * NOTE: for reads, m->dirty will probably 1566956f3135SPhilippe Charnier * be overridden by the original caller of 15671c7c3c6aSMatthew Dillon * getpages so don't play cute tricks here. 15681c7c3c6aSMatthew Dillon */ 15691c7c3c6aSMatthew Dillon m->valid = 0; 15701c7c3c6aSMatthew Dillon } else { 15711c7c3c6aSMatthew Dillon /* 15721c7c3c6aSMatthew Dillon * If a write error occurs, reactivate page 15731c7c3c6aSMatthew Dillon * so it doesn't clog the inactive list, 15741c7c3c6aSMatthew Dillon * then finish the I/O. 15751c7c3c6aSMatthew Dillon */ 157641e5a226SAlan Cox MPASS(m->dirty == VM_PAGE_BITS_ALL); 15773c4a2440SAlan Cox vm_page_lock(m); 15781c7c3c6aSMatthew Dillon vm_page_activate(m); 15793c4a2440SAlan Cox vm_page_unlock(m); 1580c7aebda8SAttilio Rao vm_page_sunbusy(m); 15811c7c3c6aSMatthew Dillon } 158221144e3bSPoul-Henning Kamp } else if (bp->b_iocmd == BIO_READ) { 15831c7c3c6aSMatthew Dillon /* 15841c7c3c6aSMatthew Dillon * NOTE: for reads, m->dirty will probably be 1585956f3135SPhilippe Charnier * overridden by the original caller of getpages so 15861c7c3c6aSMatthew Dillon * we cannot set them in order to free the underlying 15871c7c3c6aSMatthew Dillon * swap in a low-swap situation. I don't think we'd 15881c7c3c6aSMatthew Dillon * want to do that anyway, but it was an optimization 15891c7c3c6aSMatthew Dillon * that existed in the old swapper for a time before 15901c7c3c6aSMatthew Dillon * it got ripped out due to precisely this problem. 15911c7c3c6aSMatthew Dillon */ 1592016a3c93SAlan Cox KASSERT(!pmap_page_is_mapped(m), 1593016a3c93SAlan Cox ("swp_pager_async_iodone: page %p is mapped", m)); 1594016a3c93SAlan Cox KASSERT(m->dirty == 0, 1595016a3c93SAlan Cox ("swp_pager_async_iodone: page %p is dirty", m)); 1596915d1b71SMark Johnston 1597b0cd2017SGleb Smirnoff m->valid = VM_PAGE_BITS_ALL; 1598915d1b71SMark Johnston if (i < bp->b_pgbefore || 1599915d1b71SMark Johnston i >= bp->b_npages - bp->b_pgafter) 1600915d1b71SMark Johnston vm_page_readahead_finish(m); 16011c7c3c6aSMatthew Dillon } else { 16021c7c3c6aSMatthew Dillon /* 1603016a3c93SAlan Cox * For write success, clear the dirty 16041c7c3c6aSMatthew Dillon * status, then finish the I/O ( which decrements the 16051c7c3c6aSMatthew Dillon * busy count and possibly wakes waiter's up ). 1606ebcddc72SAlan Cox * A page is only written to swap after a period of 1607ebcddc72SAlan Cox * inactivity. Therefore, we do not expect it to be 1608ebcddc72SAlan Cox * reused. 16091c7c3c6aSMatthew Dillon */ 16106031c68dSAlan Cox KASSERT(!pmap_page_is_write_mapped(m), 1611016a3c93SAlan Cox ("swp_pager_async_iodone: page %p is not write" 1612016a3c93SAlan Cox " protected", m)); 1613c52e7044SAlan Cox vm_page_undirty(m); 16143c4a2440SAlan Cox vm_page_lock(m); 1615ebcddc72SAlan Cox vm_page_deactivate_noreuse(m); 16162965a453SKip Macy vm_page_unlock(m); 1617ebcddc72SAlan Cox vm_page_sunbusy(m); 16183c4a2440SAlan Cox } 16193c4a2440SAlan Cox } 162026f9a767SRodney W. Grimes 16211c7c3c6aSMatthew Dillon /* 16221c7c3c6aSMatthew Dillon * adjust pip. NOTE: the original parent may still have its own 16231c7c3c6aSMatthew Dillon * pip refs on the object. 16241c7c3c6aSMatthew Dillon */ 16250d420ad3SAlan Cox if (object != NULL) { 16261c7c3c6aSMatthew Dillon vm_object_pip_wakeupn(object, bp->b_npages); 162789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 16280d420ad3SAlan Cox } 162926f9a767SRodney W. Grimes 16301c7c3c6aSMatthew Dillon /* 1631100650deSOlivier Houchard * swapdev_strategy() manually sets b_vp and b_bufobj before calling 1632100650deSOlivier Houchard * bstrategy(). Set them back to NULL now we're done with it, or we'll 1633100650deSOlivier Houchard * trigger a KASSERT in relpbuf(). 1634100650deSOlivier Houchard */ 1635100650deSOlivier Houchard if (bp->b_vp) { 1636100650deSOlivier Houchard bp->b_vp = NULL; 1637100650deSOlivier Houchard bp->b_bufobj = NULL; 1638100650deSOlivier Houchard } 1639100650deSOlivier Houchard /* 16401c7c3c6aSMatthew Dillon * release the physical I/O buffer 16411c7c3c6aSMatthew Dillon */ 1642*756a5412SGleb Smirnoff if (bp->b_flags & B_ASYNC) { 1643*756a5412SGleb Smirnoff mtx_lock(&swbuf_mtx); 1644*756a5412SGleb Smirnoff if (++nsw_wcount_async == 1) 1645*756a5412SGleb Smirnoff wakeup(&nsw_wcount_async); 1646*756a5412SGleb Smirnoff mtx_unlock(&swbuf_mtx); 1647*756a5412SGleb Smirnoff } 1648*756a5412SGleb Smirnoff uma_zfree((bp->b_iocmd == BIO_READ) ? swrbuf_zone : swwbuf_zone, bp); 164926f9a767SRodney W. Grimes } 16501c7c3c6aSMatthew Dillon 1651b1fd102eSMark Johnston int 1652b1fd102eSMark Johnston swap_pager_nswapdev(void) 1653b1fd102eSMark Johnston { 1654b1fd102eSMark Johnston 1655b1fd102eSMark Johnston return (nswapdev); 1656b1fd102eSMark Johnston } 1657b1fd102eSMark Johnston 165892da00bbSMatthew Dillon /* 165992da00bbSMatthew Dillon * SWP_PAGER_FORCE_PAGEIN() - force a swap block to be paged in 166092da00bbSMatthew Dillon * 1661ebcddc72SAlan Cox * This routine dissociates the page at the given index within an object 1662ebcddc72SAlan Cox * from its backing store, paging it in if it does not reside in memory. 1663ebcddc72SAlan Cox * If the page is paged in, it is marked dirty and placed in the laundry 1664ebcddc72SAlan Cox * queue. The page is marked dirty because it no longer has backing 1665ebcddc72SAlan Cox * store. It is placed in the laundry queue because it has not been 1666ebcddc72SAlan Cox * accessed recently. Otherwise, it would already reside in memory. 1667ebcddc72SAlan Cox * 1668ebcddc72SAlan Cox * We also attempt to swap in all other pages in the swap block. 1669ebcddc72SAlan Cox * However, we only guarantee that the one at the specified index is 167092da00bbSMatthew Dillon * paged in. 167192da00bbSMatthew Dillon * 167292da00bbSMatthew Dillon * XXX - The code to page the whole block in doesn't work, so we 167392da00bbSMatthew Dillon * revert to the one-by-one behavior for now. Sigh. 167492da00bbSMatthew Dillon */ 167562a59e8fSWarner Losh static inline void 1676b3fed13eSDavid Schultz swp_pager_force_pagein(vm_object_t object, vm_pindex_t pindex) 167792da00bbSMatthew Dillon { 167892da00bbSMatthew Dillon vm_page_t m; 167992da00bbSMatthew Dillon 168092da00bbSMatthew Dillon vm_object_pip_add(object, 1); 16815944de8eSKonstantin Belousov m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL); 168292da00bbSMatthew Dillon if (m->valid == VM_PAGE_BITS_ALL) { 16830d8243ccSAttilio Rao vm_object_pip_wakeup(object); 168492da00bbSMatthew Dillon vm_page_dirty(m); 168537244a84SAlan Cox #ifdef INVARIANTS 1686d061cdd5SAlan Cox vm_page_lock(m); 168737244a84SAlan Cox if (m->wire_count == 0 && m->queue == PQ_NONE) 168837244a84SAlan Cox panic("page %p is neither wired nor queued", m); 16892965a453SKip Macy vm_page_unlock(m); 169037244a84SAlan Cox #endif 1691c7aebda8SAttilio Rao vm_page_xunbusy(m); 169292da00bbSMatthew Dillon vm_pager_page_unswapped(m); 169392da00bbSMatthew Dillon return; 169492da00bbSMatthew Dillon } 169592da00bbSMatthew Dillon 1696b0cd2017SGleb Smirnoff if (swap_pager_getpages(object, &m, 1, NULL, NULL) != VM_PAGER_OK) 169792da00bbSMatthew Dillon panic("swap_pager_force_pagein: read from swap failed");/*XXX*/ 16980d8243ccSAttilio Rao vm_object_pip_wakeup(object); 169992da00bbSMatthew Dillon vm_page_dirty(m); 1700db1f085eSAlan Cox vm_page_lock(m); 1701ebcddc72SAlan Cox vm_page_launder(m); 17022965a453SKip Macy vm_page_unlock(m); 1703c7aebda8SAttilio Rao vm_page_xunbusy(m); 170492da00bbSMatthew Dillon vm_pager_page_unswapped(m); 170592da00bbSMatthew Dillon } 170692da00bbSMatthew Dillon 170792da00bbSMatthew Dillon /* 170892da00bbSMatthew Dillon * swap_pager_swapoff: 170992da00bbSMatthew Dillon * 171092da00bbSMatthew Dillon * Page in all of the pages that have been paged out to the 171192da00bbSMatthew Dillon * given device. The corresponding blocks in the bitmap must be 171292da00bbSMatthew Dillon * marked as allocated and the device must be flagged SW_CLOSING. 171392da00bbSMatthew Dillon * There may be no processes swapped out to the device. 171492da00bbSMatthew Dillon * 171592da00bbSMatthew Dillon * This routine may block. 171692da00bbSMatthew Dillon */ 1717e9c0cc15SPoul-Henning Kamp static void 1718b3fed13eSDavid Schultz swap_pager_swapoff(struct swdevt *sp) 171992da00bbSMatthew Dillon { 1720f425ab8eSKonstantin Belousov struct swblk *sb; 1721f425ab8eSKonstantin Belousov vm_object_t object; 1722f425ab8eSKonstantin Belousov vm_pindex_t pi; 1723f425ab8eSKonstantin Belousov int i, retries; 172492da00bbSMatthew Dillon 172504533e1eSKonstantin Belousov sx_assert(&swdev_syscall_lock, SA_XLOCKED); 172692da00bbSMatthew Dillon 17278bc61209SDavid Schultz retries = 0; 172892da00bbSMatthew Dillon full_rescan: 1729f425ab8eSKonstantin Belousov mtx_lock(&vm_object_list_mtx); 1730f425ab8eSKonstantin Belousov TAILQ_FOREACH(object, &vm_object_list, object_list) { 1731f425ab8eSKonstantin Belousov if (object->type != OBJT_SWAP) 173298150664SKonstantin Belousov continue; 1733f425ab8eSKonstantin Belousov mtx_unlock(&vm_object_list_mtx); 173498150664SKonstantin Belousov /* Depends on type-stability. */ 173598150664SKonstantin Belousov VM_OBJECT_WLOCK(object); 1736f425ab8eSKonstantin Belousov 1737f425ab8eSKonstantin Belousov /* 1738f425ab8eSKonstantin Belousov * Dead objects are eventually terminated on their own. 1739f425ab8eSKonstantin Belousov */ 1740f425ab8eSKonstantin Belousov if ((object->flags & OBJ_DEAD) != 0) 1741f425ab8eSKonstantin Belousov goto next_obj; 1742f425ab8eSKonstantin Belousov 1743f425ab8eSKonstantin Belousov /* 1744f425ab8eSKonstantin Belousov * Sync with fences placed after pctrie 1745f425ab8eSKonstantin Belousov * initialization. We must not access pctrie below 1746f425ab8eSKonstantin Belousov * unless we checked that our object is swap and not 1747f425ab8eSKonstantin Belousov * dead. 1748f425ab8eSKonstantin Belousov */ 1749f425ab8eSKonstantin Belousov atomic_thread_fence_acq(); 1750f425ab8eSKonstantin Belousov if (object->type != OBJT_SWAP) 1751f425ab8eSKonstantin Belousov goto next_obj; 1752f425ab8eSKonstantin Belousov 1753f425ab8eSKonstantin Belousov for (pi = 0; (sb = SWAP_PCTRIE_LOOKUP_GE( 1754f425ab8eSKonstantin Belousov &object->un_pager.swp.swp_blks, pi)) != NULL; ) { 1755f425ab8eSKonstantin Belousov pi = sb->p + SWAP_META_PAGES; 1756f425ab8eSKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) { 1757f425ab8eSKonstantin Belousov if (sb->d[i] == SWAPBLK_NONE) 1758f425ab8eSKonstantin Belousov continue; 1759f425ab8eSKonstantin Belousov if (swp_pager_isondev(sb->d[i], sp)) 1760f425ab8eSKonstantin Belousov swp_pager_force_pagein(object, 1761f425ab8eSKonstantin Belousov sb->p + i); 176298150664SKonstantin Belousov } 176398150664SKonstantin Belousov } 1764f425ab8eSKonstantin Belousov next_obj: 1765f425ab8eSKonstantin Belousov VM_OBJECT_WUNLOCK(object); 1766f425ab8eSKonstantin Belousov mtx_lock(&vm_object_list_mtx); 176792da00bbSMatthew Dillon } 1768f425ab8eSKonstantin Belousov mtx_unlock(&vm_object_list_mtx); 1769f425ab8eSKonstantin Belousov 17708bc61209SDavid Schultz if (sp->sw_used) { 177192da00bbSMatthew Dillon /* 17728bc61209SDavid Schultz * Objects may be locked or paging to the device being 17738bc61209SDavid Schultz * removed, so we will miss their pages and need to 17748bc61209SDavid Schultz * make another pass. We have marked this device as 17758bc61209SDavid Schultz * SW_CLOSING, so the activity should finish soon. 177692da00bbSMatthew Dillon */ 17778bc61209SDavid Schultz retries++; 17788bc61209SDavid Schultz if (retries > 100) { 17798bc61209SDavid Schultz panic("swapoff: failed to locate %d swap blocks", 17808bc61209SDavid Schultz sp->sw_used); 17818bc61209SDavid Schultz } 17824d70511aSJohn Baldwin pause("swpoff", hz / 20); 178392da00bbSMatthew Dillon goto full_rescan; 178492da00bbSMatthew Dillon } 1785b1fd102eSMark Johnston EVENTHANDLER_INVOKE(swapoff, sp); 178692da00bbSMatthew Dillon } 178792da00bbSMatthew Dillon 17881c7c3c6aSMatthew Dillon /************************************************************************ 17891c7c3c6aSMatthew Dillon * SWAP META DATA * 17901c7c3c6aSMatthew Dillon ************************************************************************ 17911c7c3c6aSMatthew Dillon * 17921c7c3c6aSMatthew Dillon * These routines manipulate the swap metadata stored in the 1793cec9f109SDavid E. O'Brien * OBJT_SWAP object. 17941c7c3c6aSMatthew Dillon * 17954dcc5c2dSMatthew Dillon * Swap metadata is implemented with a global hash and not directly 17964dcc5c2dSMatthew Dillon * linked into the object. Instead the object simply contains 17974dcc5c2dSMatthew Dillon * appropriate tracking counters. 17981c7c3c6aSMatthew Dillon */ 17991c7c3c6aSMatthew Dillon 18001c7c3c6aSMatthew Dillon /* 1801230869e0SAlan Cox * SWP_PAGER_SWBLK_EMPTY() - is a range of blocks free? 1802230869e0SAlan Cox */ 1803230869e0SAlan Cox static bool 1804230869e0SAlan Cox swp_pager_swblk_empty(struct swblk *sb, int start, int limit) 1805230869e0SAlan Cox { 1806230869e0SAlan Cox int i; 1807230869e0SAlan Cox 1808230869e0SAlan Cox MPASS(0 <= start && start <= limit && limit <= SWAP_META_PAGES); 1809230869e0SAlan Cox for (i = start; i < limit; i++) { 1810230869e0SAlan Cox if (sb->d[i] != SWAPBLK_NONE) 1811230869e0SAlan Cox return (false); 1812230869e0SAlan Cox } 1813230869e0SAlan Cox return (true); 1814230869e0SAlan Cox } 1815230869e0SAlan Cox 1816230869e0SAlan Cox /* 18171c7c3c6aSMatthew Dillon * SWP_PAGER_META_BUILD() - add swap block to swap meta data for object 18181c7c3c6aSMatthew Dillon * 18191c7c3c6aSMatthew Dillon * We first convert the object to a swap object if it is a default 18201c7c3c6aSMatthew Dillon * object. 18211c7c3c6aSMatthew Dillon * 18221c7c3c6aSMatthew Dillon * The specified swapblk is added to the object's swap metadata. If 18231c7c3c6aSMatthew Dillon * the swapblk is not valid, it is freed instead. Any previously 182478f1deefSAlan Cox * assigned swapblk is returned. 18251c7c3c6aSMatthew Dillon */ 182678f1deefSAlan Cox static daddr_t 18272f249180SPoul-Henning Kamp swp_pager_meta_build(vm_object_t object, vm_pindex_t pindex, daddr_t swapblk) 18282f249180SPoul-Henning Kamp { 1829f425ab8eSKonstantin Belousov static volatile int swblk_zone_exhausted, swpctrie_zone_exhausted; 1830eed99cb8SKonstantin Belousov struct swblk *sb, *sb1; 1831f425ab8eSKonstantin Belousov vm_pindex_t modpi, rdpi; 183278f1deefSAlan Cox daddr_t prev_swapblk; 1833f425ab8eSKonstantin Belousov int error, i; 18341c7c3c6aSMatthew Dillon 183589f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 1836f425ab8eSKonstantin Belousov 18371c7c3c6aSMatthew Dillon /* 18381c7c3c6aSMatthew Dillon * Convert default object to swap object if necessary 18391c7c3c6aSMatthew Dillon */ 18401c7c3c6aSMatthew Dillon if (object->type != OBJT_SWAP) { 1841f425ab8eSKonstantin Belousov pctrie_init(&object->un_pager.swp.swp_blks); 1842f425ab8eSKonstantin Belousov 1843f425ab8eSKonstantin Belousov /* 1844f425ab8eSKonstantin Belousov * Ensure that swap_pager_swapoff()'s iteration over 1845f425ab8eSKonstantin Belousov * object_list does not see a garbage pctrie. 1846f425ab8eSKonstantin Belousov */ 1847f425ab8eSKonstantin Belousov atomic_thread_fence_rel(); 1848f425ab8eSKonstantin Belousov 18491c7c3c6aSMatthew Dillon object->type = OBJT_SWAP; 1850eb4d6a1bSKonstantin Belousov KASSERT(object->handle == NULL, ("default pager with handle")); 1851bd228075SAlan Cox } 18521c7c3c6aSMatthew Dillon 1853f425ab8eSKonstantin Belousov rdpi = rounddown(pindex, SWAP_META_PAGES); 1854f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, rdpi); 1855f425ab8eSKonstantin Belousov if (sb == NULL) { 18561c7c3c6aSMatthew Dillon if (swapblk == SWAPBLK_NONE) 185778f1deefSAlan Cox return (SWAPBLK_NONE); 1858f425ab8eSKonstantin Belousov for (;;) { 1859f425ab8eSKonstantin Belousov sb = uma_zalloc(swblk_zone, M_NOWAIT | (curproc == 1860f425ab8eSKonstantin Belousov pageproc ? M_USE_RESERVE : 0)); 1861f425ab8eSKonstantin Belousov if (sb != NULL) { 1862f425ab8eSKonstantin Belousov sb->p = rdpi; 1863f425ab8eSKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) 1864f425ab8eSKonstantin Belousov sb->d[i] = SWAPBLK_NONE; 1865f425ab8eSKonstantin Belousov if (atomic_cmpset_int(&swblk_zone_exhausted, 1866f425ab8eSKonstantin Belousov 1, 0)) 1867f425ab8eSKonstantin Belousov printf("swblk zone ok\n"); 1868f425ab8eSKonstantin Belousov break; 1869f425ab8eSKonstantin Belousov } 187089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1871f425ab8eSKonstantin Belousov if (uma_zone_exhausted(swblk_zone)) { 1872f425ab8eSKonstantin Belousov if (atomic_cmpset_int(&swblk_zone_exhausted, 1873f425ab8eSKonstantin Belousov 0, 1)) 1874f425ab8eSKonstantin Belousov printf("swap blk zone exhausted, " 18753ff863f1SDag-Erling Smørgrav "increase kern.maxswzone\n"); 18762025d69bSKonstantin Belousov vm_pageout_oom(VM_OOM_SWAPZ); 1877f425ab8eSKonstantin Belousov pause("swzonxb", 10); 18782025d69bSKonstantin Belousov } else 18798d6fbbb8SJeff Roberson uma_zwait(swblk_zone); 188089f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1881eed99cb8SKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, 1882eed99cb8SKonstantin Belousov rdpi); 1883eed99cb8SKonstantin Belousov if (sb != NULL) 1884eed99cb8SKonstantin Belousov /* 1885eed99cb8SKonstantin Belousov * Somebody swapped out a nearby page, 1886eed99cb8SKonstantin Belousov * allocating swblk at the rdpi index, 1887eed99cb8SKonstantin Belousov * while we dropped the object lock. 1888eed99cb8SKonstantin Belousov */ 1889eed99cb8SKonstantin Belousov goto allocated; 18904dcc5c2dSMatthew Dillon } 1891f425ab8eSKonstantin Belousov for (;;) { 1892f425ab8eSKonstantin Belousov error = SWAP_PCTRIE_INSERT( 1893f425ab8eSKonstantin Belousov &object->un_pager.swp.swp_blks, sb); 1894f425ab8eSKonstantin Belousov if (error == 0) { 1895f425ab8eSKonstantin Belousov if (atomic_cmpset_int(&swpctrie_zone_exhausted, 1896f425ab8eSKonstantin Belousov 1, 0)) 1897f425ab8eSKonstantin Belousov printf("swpctrie zone ok\n"); 1898f425ab8eSKonstantin Belousov break; 18991c7c3c6aSMatthew Dillon } 1900f425ab8eSKonstantin Belousov VM_OBJECT_WUNLOCK(object); 1901f425ab8eSKonstantin Belousov if (uma_zone_exhausted(swpctrie_zone)) { 1902f425ab8eSKonstantin Belousov if (atomic_cmpset_int(&swpctrie_zone_exhausted, 1903f425ab8eSKonstantin Belousov 0, 1)) 1904f425ab8eSKonstantin Belousov printf("swap pctrie zone exhausted, " 1905f425ab8eSKonstantin Belousov "increase kern.maxswzone\n"); 1906f425ab8eSKonstantin Belousov vm_pageout_oom(VM_OOM_SWAPZ); 1907f425ab8eSKonstantin Belousov pause("swzonxp", 10); 1908f425ab8eSKonstantin Belousov } else 19098d6fbbb8SJeff Roberson uma_zwait(swpctrie_zone); 1910f425ab8eSKonstantin Belousov VM_OBJECT_WLOCK(object); 1911eed99cb8SKonstantin Belousov sb1 = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, 1912eed99cb8SKonstantin Belousov rdpi); 1913eed99cb8SKonstantin Belousov if (sb1 != NULL) { 1914eed99cb8SKonstantin Belousov uma_zfree(swblk_zone, sb); 1915eed99cb8SKonstantin Belousov sb = sb1; 1916eed99cb8SKonstantin Belousov goto allocated; 19171c7c3c6aSMatthew Dillon } 1918f425ab8eSKonstantin Belousov } 1919eed99cb8SKonstantin Belousov } 1920eed99cb8SKonstantin Belousov allocated: 1921f425ab8eSKonstantin Belousov MPASS(sb->p == rdpi); 19221c7c3c6aSMatthew Dillon 1923f425ab8eSKonstantin Belousov modpi = pindex % SWAP_META_PAGES; 192478f1deefSAlan Cox /* Return prior contents of metadata. */ 192578f1deefSAlan Cox prev_swapblk = sb->d[modpi]; 1926f425ab8eSKonstantin Belousov /* Enter block into metadata. */ 1927f425ab8eSKonstantin Belousov sb->d[modpi] = swapblk; 192885d88d87SKonstantin Belousov 192985d88d87SKonstantin Belousov /* 193085d88d87SKonstantin Belousov * Free the swblk if we end up with the empty page run. 193185d88d87SKonstantin Belousov */ 1932230869e0SAlan Cox if (swapblk == SWAPBLK_NONE && 1933230869e0SAlan Cox swp_pager_swblk_empty(sb, 0, SWAP_META_PAGES)) { 1934230869e0SAlan Cox SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, rdpi); 193585d88d87SKonstantin Belousov uma_zfree(swblk_zone, sb); 193685d88d87SKonstantin Belousov } 193778f1deefSAlan Cox return (prev_swapblk); 193885d88d87SKonstantin Belousov } 19391c7c3c6aSMatthew Dillon 19401c7c3c6aSMatthew Dillon /* 19411c7c3c6aSMatthew Dillon * SWP_PAGER_META_FREE() - free a range of blocks in the object's swap metadata 19421c7c3c6aSMatthew Dillon * 19431c7c3c6aSMatthew Dillon * The requested range of blocks is freed, with any associated swap 19441c7c3c6aSMatthew Dillon * returned to the swap bitmap. 19451c7c3c6aSMatthew Dillon * 19461c7c3c6aSMatthew Dillon * This routine will free swap metadata structures as they are cleaned 19471c7c3c6aSMatthew Dillon * out. This routine does *NOT* operate on swap metadata associated 19481c7c3c6aSMatthew Dillon * with resident pages. 19491c7c3c6aSMatthew Dillon */ 19501c7c3c6aSMatthew Dillon static void 1951f425ab8eSKonstantin Belousov swp_pager_meta_free(vm_object_t object, vm_pindex_t pindex, vm_pindex_t count) 19521c7c3c6aSMatthew Dillon { 1953f425ab8eSKonstantin Belousov struct swblk *sb; 195478f1deefSAlan Cox daddr_t n_free, s_free; 1955f425ab8eSKonstantin Belousov vm_pindex_t last; 1956230869e0SAlan Cox int i, limit, start; 19572928cef7SAlan Cox 1958ee620ea4SAlan Cox VM_OBJECT_ASSERT_WLOCKED(object); 19592e56b64fSKonstantin Belousov if (object->type != OBJT_SWAP || count == 0) 19601c7c3c6aSMatthew Dillon return; 19611c7c3c6aSMatthew Dillon 196278f1deefSAlan Cox swp_pager_init_freerange(&s_free, &n_free); 1963230869e0SAlan Cox last = pindex + count; 1964f425ab8eSKonstantin Belousov for (;;) { 1965f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks, 1966f425ab8eSKonstantin Belousov rounddown(pindex, SWAP_META_PAGES)); 1967230869e0SAlan Cox if (sb == NULL || sb->p >= last) 19682e56b64fSKonstantin Belousov break; 1969230869e0SAlan Cox start = pindex > sb->p ? pindex - sb->p : 0; 1970230869e0SAlan Cox limit = last - sb->p < SWAP_META_PAGES ? last - sb->p : 1971230869e0SAlan Cox SWAP_META_PAGES; 1972230869e0SAlan Cox for (i = start; i < limit; i++) { 1973f425ab8eSKonstantin Belousov if (sb->d[i] == SWAPBLK_NONE) 1974f425ab8eSKonstantin Belousov continue; 197578f1deefSAlan Cox swp_pager_update_freerange(&s_free, &n_free, sb->d[i]); 1976230869e0SAlan Cox sb->d[i] = SWAPBLK_NONE; 1977230869e0SAlan Cox } 1978150d384eSMark Johnston pindex = sb->p + SWAP_META_PAGES; 1979230869e0SAlan Cox if (swp_pager_swblk_empty(sb, 0, start) && 1980230869e0SAlan Cox swp_pager_swblk_empty(sb, limit, SWAP_META_PAGES)) { 1981f425ab8eSKonstantin Belousov SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, 1982f425ab8eSKonstantin Belousov sb->p); 1983f425ab8eSKonstantin Belousov uma_zfree(swblk_zone, sb); 19841c7c3c6aSMatthew Dillon } 19851c7c3c6aSMatthew Dillon } 198678f1deefSAlan Cox swp_pager_freeswapspace(s_free, n_free); 19871c7c3c6aSMatthew Dillon } 19881c7c3c6aSMatthew Dillon 19891c7c3c6aSMatthew Dillon /* 19901c7c3c6aSMatthew Dillon * SWP_PAGER_META_FREE_ALL() - destroy all swap metadata associated with object 19911c7c3c6aSMatthew Dillon * 19921c7c3c6aSMatthew Dillon * This routine locates and destroys all swap metadata associated with 19931c7c3c6aSMatthew Dillon * an object. 19941c7c3c6aSMatthew Dillon */ 19951c7c3c6aSMatthew Dillon static void 19961c7c3c6aSMatthew Dillon swp_pager_meta_free_all(vm_object_t object) 19971c7c3c6aSMatthew Dillon { 1998f425ab8eSKonstantin Belousov struct swblk *sb; 199978f1deefSAlan Cox daddr_t n_free, s_free; 2000f425ab8eSKonstantin Belousov vm_pindex_t pindex; 200171057cd2SKonstantin Belousov int i; 20021c7c3c6aSMatthew Dillon 200389f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 20041c7c3c6aSMatthew Dillon if (object->type != OBJT_SWAP) 20051c7c3c6aSMatthew Dillon return; 20061c7c3c6aSMatthew Dillon 200778f1deefSAlan Cox swp_pager_init_freerange(&s_free, &n_free); 2008f425ab8eSKonstantin Belousov for (pindex = 0; (sb = SWAP_PCTRIE_LOOKUP_GE( 2009f425ab8eSKonstantin Belousov &object->un_pager.swp.swp_blks, pindex)) != NULL;) { 2010f425ab8eSKonstantin Belousov pindex = sb->p + SWAP_META_PAGES; 2011f425ab8eSKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) { 2012230869e0SAlan Cox if (sb->d[i] == SWAPBLK_NONE) 2013230869e0SAlan Cox continue; 201478f1deefSAlan Cox swp_pager_update_freerange(&s_free, &n_free, sb->d[i]); 20151c7c3c6aSMatthew Dillon } 2016f425ab8eSKonstantin Belousov SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, sb->p); 2017f425ab8eSKonstantin Belousov uma_zfree(swblk_zone, sb); 20181c7c3c6aSMatthew Dillon } 201978f1deefSAlan Cox swp_pager_freeswapspace(s_free, n_free); 20201c7c3c6aSMatthew Dillon } 20211c7c3c6aSMatthew Dillon 20221c7c3c6aSMatthew Dillon /* 20234abca9bbSAlan Cox * SWP_PAGER_METACTL() - misc control of swap meta data. 20241c7c3c6aSMatthew Dillon * 20254abca9bbSAlan Cox * This routine is capable of looking up, or removing swapblk 20264abca9bbSAlan Cox * assignments in the swap meta data. It returns the swapblk being 20274abca9bbSAlan Cox * looked-up, popped, or SWAPBLK_NONE if the block was invalid. 20281c7c3c6aSMatthew Dillon * 20291c7c3c6aSMatthew Dillon * When acting on a busy resident page and paging is in progress, we 20301c7c3c6aSMatthew Dillon * have to wait until paging is complete but otherwise can act on the 20311c7c3c6aSMatthew Dillon * busy page. 20321c7c3c6aSMatthew Dillon * 20334abca9bbSAlan Cox * SWM_POP remove from meta data but do not free it 20341c7c3c6aSMatthew Dillon */ 20351c7c3c6aSMatthew Dillon static daddr_t 20362f249180SPoul-Henning Kamp swp_pager_meta_ctl(vm_object_t object, vm_pindex_t pindex, int flags) 20372f249180SPoul-Henning Kamp { 2038f425ab8eSKonstantin Belousov struct swblk *sb; 20394dcc5c2dSMatthew Dillon daddr_t r1; 20404dcc5c2dSMatthew Dillon 20414abca9bbSAlan Cox if ((flags & SWM_POP) != 0) 2042ee620ea4SAlan Cox VM_OBJECT_ASSERT_WLOCKED(object); 2043ee620ea4SAlan Cox else 2044c25673ffSAttilio Rao VM_OBJECT_ASSERT_LOCKED(object); 2045ee620ea4SAlan Cox 20461c7c3c6aSMatthew Dillon /* 2047ee620ea4SAlan Cox * The meta data only exists if the object is OBJT_SWAP 20481c7c3c6aSMatthew Dillon * and even then might not be allocated yet. 20491c7c3c6aSMatthew Dillon */ 20504dcc5c2dSMatthew Dillon if (object->type != OBJT_SWAP) 20511c7c3c6aSMatthew Dillon return (SWAPBLK_NONE); 20521c7c3c6aSMatthew Dillon 2053f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, 2054f425ab8eSKonstantin Belousov rounddown(pindex, SWAP_META_PAGES)); 2055f425ab8eSKonstantin Belousov if (sb == NULL) 2056f425ab8eSKonstantin Belousov return (SWAPBLK_NONE); 2057f425ab8eSKonstantin Belousov r1 = sb->d[pindex % SWAP_META_PAGES]; 2058f425ab8eSKonstantin Belousov if (r1 == SWAPBLK_NONE) 2059f425ab8eSKonstantin Belousov return (SWAPBLK_NONE); 20604abca9bbSAlan Cox if ((flags & SWM_POP) != 0) { 2061f425ab8eSKonstantin Belousov sb->d[pindex % SWAP_META_PAGES] = SWAPBLK_NONE; 2062230869e0SAlan Cox if (swp_pager_swblk_empty(sb, 0, SWAP_META_PAGES)) { 2063f425ab8eSKonstantin Belousov SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, 2064f425ab8eSKonstantin Belousov rounddown(pindex, SWAP_META_PAGES)); 2065f425ab8eSKonstantin Belousov uma_zfree(swblk_zone, sb); 2066f425ab8eSKonstantin Belousov } 2067f425ab8eSKonstantin Belousov } 20681c7c3c6aSMatthew Dillon return (r1); 20691c7c3c6aSMatthew Dillon } 20701c7c3c6aSMatthew Dillon 2071e9c0cc15SPoul-Henning Kamp /* 207277d6fd97SKonstantin Belousov * Returns the least page index which is greater than or equal to the 207377d6fd97SKonstantin Belousov * parameter pindex and for which there is a swap block allocated. 207477d6fd97SKonstantin Belousov * Returns object's size if the object's type is not swap or if there 207577d6fd97SKonstantin Belousov * are no allocated swap blocks for the object after the requested 207677d6fd97SKonstantin Belousov * pindex. 207777d6fd97SKonstantin Belousov */ 207877d6fd97SKonstantin Belousov vm_pindex_t 207977d6fd97SKonstantin Belousov swap_pager_find_least(vm_object_t object, vm_pindex_t pindex) 208077d6fd97SKonstantin Belousov { 2081f425ab8eSKonstantin Belousov struct swblk *sb; 2082f425ab8eSKonstantin Belousov int i; 208377d6fd97SKonstantin Belousov 208477d6fd97SKonstantin Belousov VM_OBJECT_ASSERT_LOCKED(object); 2085f425ab8eSKonstantin Belousov if (object->type != OBJT_SWAP) 208677d6fd97SKonstantin Belousov return (object->size); 208777d6fd97SKonstantin Belousov 2088f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks, 2089f425ab8eSKonstantin Belousov rounddown(pindex, SWAP_META_PAGES)); 2090f425ab8eSKonstantin Belousov if (sb == NULL) 2091f425ab8eSKonstantin Belousov return (object->size); 2092f425ab8eSKonstantin Belousov if (sb->p < pindex) { 2093f425ab8eSKonstantin Belousov for (i = pindex % SWAP_META_PAGES; i < SWAP_META_PAGES; i++) { 2094f425ab8eSKonstantin Belousov if (sb->d[i] != SWAPBLK_NONE) 2095f425ab8eSKonstantin Belousov return (sb->p + i); 209677d6fd97SKonstantin Belousov } 2097f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks, 2098f425ab8eSKonstantin Belousov roundup(pindex, SWAP_META_PAGES)); 2099f425ab8eSKonstantin Belousov if (sb == NULL) 2100f425ab8eSKonstantin Belousov return (object->size); 210177d6fd97SKonstantin Belousov } 2102f425ab8eSKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) { 2103f425ab8eSKonstantin Belousov if (sb->d[i] != SWAPBLK_NONE) 2104f425ab8eSKonstantin Belousov return (sb->p + i); 210577d6fd97SKonstantin Belousov } 2106f425ab8eSKonstantin Belousov 2107f425ab8eSKonstantin Belousov /* 2108f425ab8eSKonstantin Belousov * We get here if a swblk is present in the trie but it 2109f425ab8eSKonstantin Belousov * doesn't map any blocks. 2110f425ab8eSKonstantin Belousov */ 2111f425ab8eSKonstantin Belousov MPASS(0); 2112f425ab8eSKonstantin Belousov return (object->size); 211377d6fd97SKonstantin Belousov } 211477d6fd97SKonstantin Belousov 211577d6fd97SKonstantin Belousov /* 2116e9c0cc15SPoul-Henning Kamp * System call swapon(name) enables swapping on device name, 2117e9c0cc15SPoul-Henning Kamp * which must be in the swdevsw. Return EBUSY 2118e9c0cc15SPoul-Henning Kamp * if already swapping on this device. 2119e9c0cc15SPoul-Henning Kamp */ 2120e9c0cc15SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_ 2121e9c0cc15SPoul-Henning Kamp struct swapon_args { 2122e9c0cc15SPoul-Henning Kamp char *name; 2123e9c0cc15SPoul-Henning Kamp }; 2124e9c0cc15SPoul-Henning Kamp #endif 2125e9c0cc15SPoul-Henning Kamp 2126e9c0cc15SPoul-Henning Kamp /* 2127e9c0cc15SPoul-Henning Kamp * MPSAFE 2128e9c0cc15SPoul-Henning Kamp */ 2129e9c0cc15SPoul-Henning Kamp /* ARGSUSED */ 2130e9c0cc15SPoul-Henning Kamp int 21318451d0ddSKip Macy sys_swapon(struct thread *td, struct swapon_args *uap) 2132e9c0cc15SPoul-Henning Kamp { 2133e9c0cc15SPoul-Henning Kamp struct vattr attr; 2134e9c0cc15SPoul-Henning Kamp struct vnode *vp; 2135e9c0cc15SPoul-Henning Kamp struct nameidata nd; 2136e9c0cc15SPoul-Henning Kamp int error; 2137e9c0cc15SPoul-Henning Kamp 2138acd3428bSRobert Watson error = priv_check(td, PRIV_SWAPON); 2139e9c0cc15SPoul-Henning Kamp if (error) 2140acd3428bSRobert Watson return (error); 2141e9c0cc15SPoul-Henning Kamp 214204533e1eSKonstantin Belousov sx_xlock(&swdev_syscall_lock); 2143e9c0cc15SPoul-Henning Kamp 2144e9c0cc15SPoul-Henning Kamp /* 2145e9c0cc15SPoul-Henning Kamp * Swap metadata may not fit in the KVM if we have physical 2146e9c0cc15SPoul-Henning Kamp * memory of >1GB. 2147e9c0cc15SPoul-Henning Kamp */ 2148f425ab8eSKonstantin Belousov if (swblk_zone == NULL) { 2149e9c0cc15SPoul-Henning Kamp error = ENOMEM; 2150e9c0cc15SPoul-Henning Kamp goto done; 2151e9c0cc15SPoul-Henning Kamp } 2152e9c0cc15SPoul-Henning Kamp 2153d9135e72SRobert Watson NDINIT(&nd, LOOKUP, ISOPEN | FOLLOW | AUDITVNODE1, UIO_USERSPACE, 2154d9135e72SRobert Watson uap->name, td); 2155e9c0cc15SPoul-Henning Kamp error = namei(&nd); 2156e9c0cc15SPoul-Henning Kamp if (error) 2157e9c0cc15SPoul-Henning Kamp goto done; 2158e9c0cc15SPoul-Henning Kamp 2159e9c0cc15SPoul-Henning Kamp NDFREE(&nd, NDF_ONLY_PNBUF); 2160e9c0cc15SPoul-Henning Kamp vp = nd.ni_vp; 2161e9c0cc15SPoul-Henning Kamp 216220da9c2eSPoul-Henning Kamp if (vn_isdisk(vp, &error)) { 216388ad2d7bSKonstantin Belousov error = swapongeom(vp); 216420da9c2eSPoul-Henning Kamp } else if (vp->v_type == VREG && 2165e9c0cc15SPoul-Henning Kamp (vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 && 21660359a12eSAttilio Rao (error = VOP_GETATTR(vp, &attr, td->td_ucred)) == 0) { 2167e9c0cc15SPoul-Henning Kamp /* 2168e9c0cc15SPoul-Henning Kamp * Allow direct swapping to NFS regular files in the same 2169e9c0cc15SPoul-Henning Kamp * way that nfs_mountroot() sets up diskless swapping. 2170e9c0cc15SPoul-Henning Kamp */ 217159efee01SPoul-Henning Kamp error = swaponvp(td, vp, attr.va_size / DEV_BSIZE); 2172e9c0cc15SPoul-Henning Kamp } 2173e9c0cc15SPoul-Henning Kamp 2174e9c0cc15SPoul-Henning Kamp if (error) 2175e9c0cc15SPoul-Henning Kamp vrele(vp); 2176e9c0cc15SPoul-Henning Kamp done: 217704533e1eSKonstantin Belousov sx_xunlock(&swdev_syscall_lock); 2178e9c0cc15SPoul-Henning Kamp return (error); 2179e9c0cc15SPoul-Henning Kamp } 2180e9c0cc15SPoul-Henning Kamp 21813ff863f1SDag-Erling Smørgrav /* 21823ff863f1SDag-Erling Smørgrav * Check that the total amount of swap currently configured does not 21833ff863f1SDag-Erling Smørgrav * exceed half the theoretical maximum. If it does, print a warning 218435872e79SKonstantin Belousov * message. 21853ff863f1SDag-Erling Smørgrav */ 218635872e79SKonstantin Belousov static void 218735872e79SKonstantin Belousov swapon_check_swzone(void) 21883ff863f1SDag-Erling Smørgrav { 218935872e79SKonstantin Belousov unsigned long maxpages, npages; 21903ff863f1SDag-Erling Smørgrav 2191e8bb589dSMatt Macy npages = swap_total; 21923ff863f1SDag-Erling Smørgrav /* absolute maximum we can handle assuming 100% efficiency */ 2193f425ab8eSKonstantin Belousov maxpages = uma_zone_get_max(swblk_zone) * SWAP_META_PAGES; 21943ff863f1SDag-Erling Smørgrav 21953ff863f1SDag-Erling Smørgrav /* recommend using no more than half that amount */ 21963ff863f1SDag-Erling Smørgrav if (npages > maxpages / 2) { 21973ff863f1SDag-Erling Smørgrav printf("warning: total configured swap (%lu pages) " 21983ff863f1SDag-Erling Smørgrav "exceeds maximum recommended amount (%lu pages).\n", 21999462305cSSergey Kandaurov npages, maxpages / 2); 22003ff863f1SDag-Erling Smørgrav printf("warning: increase kern.maxswzone " 22013ff863f1SDag-Erling Smørgrav "or reduce amount of swap.\n"); 22023ff863f1SDag-Erling Smørgrav } 22033ff863f1SDag-Erling Smørgrav } 22043ff863f1SDag-Erling Smørgrav 220559efee01SPoul-Henning Kamp static void 22062cc718a1SKonstantin Belousov swaponsomething(struct vnode *vp, void *id, u_long nblks, 22072cc718a1SKonstantin Belousov sw_strategy_t *strategy, sw_close_t *close, dev_t dev, int flags) 2208e9c0cc15SPoul-Henning Kamp { 22092d9974c1SAlan Cox struct swdevt *sp, *tsp; 2210e9c0cc15SPoul-Henning Kamp swblk_t dvbase; 22118f60c087SPoul-Henning Kamp u_long mblocks; 2212e9c0cc15SPoul-Henning Kamp 2213e9c0cc15SPoul-Henning Kamp /* 2214e9c0cc15SPoul-Henning Kamp * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks. 2215e9c0cc15SPoul-Henning Kamp * First chop nblks off to page-align it, then convert. 2216e9c0cc15SPoul-Henning Kamp * 2217e9c0cc15SPoul-Henning Kamp * sw->sw_nblks is in page-sized chunks now too. 2218e9c0cc15SPoul-Henning Kamp */ 2219e9c0cc15SPoul-Henning Kamp nblks &= ~(ctodb(1) - 1); 2220e9c0cc15SPoul-Henning Kamp nblks = dbtoc(nblks); 2221e9c0cc15SPoul-Henning Kamp 22226e903bd0SKonstantin Belousov /* 22236e903bd0SKonstantin Belousov * If we go beyond this, we get overflows in the radix 22246e903bd0SKonstantin Belousov * tree bitmap code. 22256e903bd0SKonstantin Belousov */ 22266e903bd0SKonstantin Belousov mblocks = 0x40000000 / BLIST_META_RADIX; 22276e903bd0SKonstantin Belousov if (nblks > mblocks) { 22286e903bd0SKonstantin Belousov printf( 22296e903bd0SKonstantin Belousov "WARNING: reducing swap size to maximum of %luMB per unit\n", 22306e903bd0SKonstantin Belousov mblocks / 1024 / 1024 * PAGE_SIZE); 22316e903bd0SKonstantin Belousov nblks = mblocks; 22326e903bd0SKonstantin Belousov } 22336e903bd0SKonstantin Belousov 22348f60c087SPoul-Henning Kamp sp = malloc(sizeof *sp, M_VMPGDATA, M_WAITOK | M_ZERO); 2235dee34ca4SPoul-Henning Kamp sp->sw_vp = vp; 2236dee34ca4SPoul-Henning Kamp sp->sw_id = id; 2237f3732fd1SPoul-Henning Kamp sp->sw_dev = dev; 22388d677ef9SPoul-Henning Kamp sp->sw_flags = 0; 2239e9c0cc15SPoul-Henning Kamp sp->sw_nblks = nblks; 2240e9c0cc15SPoul-Henning Kamp sp->sw_used = 0; 224159efee01SPoul-Henning Kamp sp->sw_strategy = strategy; 2242dee34ca4SPoul-Henning Kamp sp->sw_close = close; 22432cc718a1SKonstantin Belousov sp->sw_flags = flags; 2244e9c0cc15SPoul-Henning Kamp 2245c8c7ad92SKip Macy sp->sw_blist = blist_create(nblks, M_WAITOK); 2246e9c0cc15SPoul-Henning Kamp /* 2247ef3c5abdSPoul-Henning Kamp * Do not free the first two block in order to avoid overwriting 22488f60c087SPoul-Henning Kamp * any bsd label at the front of the partition 2249e9c0cc15SPoul-Henning Kamp */ 2250ef3c5abdSPoul-Henning Kamp blist_free(sp->sw_blist, 2, nblks - 2); 2251e9c0cc15SPoul-Henning Kamp 22522d9974c1SAlan Cox dvbase = 0; 225320da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 22542d9974c1SAlan Cox TAILQ_FOREACH(tsp, &swtailq, sw_list) { 22552d9974c1SAlan Cox if (tsp->sw_end >= dvbase) { 22562d9974c1SAlan Cox /* 22572d9974c1SAlan Cox * We put one uncovered page between the devices 22582d9974c1SAlan Cox * in order to definitively prevent any cross-device 22592d9974c1SAlan Cox * I/O requests 22602d9974c1SAlan Cox */ 22612d9974c1SAlan Cox dvbase = tsp->sw_end + 1; 22622d9974c1SAlan Cox } 22632d9974c1SAlan Cox } 22642d9974c1SAlan Cox sp->sw_first = dvbase; 22652d9974c1SAlan Cox sp->sw_end = dvbase + nblks; 22668f60c087SPoul-Henning Kamp TAILQ_INSERT_TAIL(&swtailq, sp, sw_list); 22678f60c087SPoul-Henning Kamp nswapdev++; 2268761097c8SAlan Cox swap_pager_avail += nblks - 2; 2269e8bb589dSMatt Macy swap_total += nblks; 227035872e79SKonstantin Belousov swapon_check_swzone(); 2271d05bc129SAlan Cox swp_sizecheck(); 2272d05bc129SAlan Cox mtx_unlock(&sw_dev_mtx); 2273b1fd102eSMark Johnston EVENTHANDLER_INVOKE(swapon, sp); 227459efee01SPoul-Henning Kamp } 2275e9c0cc15SPoul-Henning Kamp 2276e9c0cc15SPoul-Henning Kamp /* 2277e9c0cc15SPoul-Henning Kamp * SYSCALL: swapoff(devname) 2278e9c0cc15SPoul-Henning Kamp * 2279e9c0cc15SPoul-Henning Kamp * Disable swapping on the given device. 2280dee34ca4SPoul-Henning Kamp * 2281dee34ca4SPoul-Henning Kamp * XXX: Badly designed system call: it should use a device index 2282dee34ca4SPoul-Henning Kamp * rather than filename as specification. We keep sw_vp around 2283dee34ca4SPoul-Henning Kamp * only to make this work. 2284e9c0cc15SPoul-Henning Kamp */ 2285e9c0cc15SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_ 2286e9c0cc15SPoul-Henning Kamp struct swapoff_args { 2287e9c0cc15SPoul-Henning Kamp char *name; 2288e9c0cc15SPoul-Henning Kamp }; 2289e9c0cc15SPoul-Henning Kamp #endif 2290e9c0cc15SPoul-Henning Kamp 2291e9c0cc15SPoul-Henning Kamp /* 2292e9c0cc15SPoul-Henning Kamp * MPSAFE 2293e9c0cc15SPoul-Henning Kamp */ 2294e9c0cc15SPoul-Henning Kamp /* ARGSUSED */ 2295e9c0cc15SPoul-Henning Kamp int 22968451d0ddSKip Macy sys_swapoff(struct thread *td, struct swapoff_args *uap) 2297e9c0cc15SPoul-Henning Kamp { 2298e9c0cc15SPoul-Henning Kamp struct vnode *vp; 2299e9c0cc15SPoul-Henning Kamp struct nameidata nd; 2300e9c0cc15SPoul-Henning Kamp struct swdevt *sp; 23018f60c087SPoul-Henning Kamp int error; 2302e9c0cc15SPoul-Henning Kamp 2303acd3428bSRobert Watson error = priv_check(td, PRIV_SWAPOFF); 2304e9c0cc15SPoul-Henning Kamp if (error) 23050909f38aSPawel Jakub Dawidek return (error); 2306e9c0cc15SPoul-Henning Kamp 230704533e1eSKonstantin Belousov sx_xlock(&swdev_syscall_lock); 2308e9c0cc15SPoul-Henning Kamp 2309d9135e72SRobert Watson NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, UIO_USERSPACE, uap->name, 2310d9135e72SRobert Watson td); 2311e9c0cc15SPoul-Henning Kamp error = namei(&nd); 2312e9c0cc15SPoul-Henning Kamp if (error) 2313e9c0cc15SPoul-Henning Kamp goto done; 2314e9c0cc15SPoul-Henning Kamp NDFREE(&nd, NDF_ONLY_PNBUF); 2315e9c0cc15SPoul-Henning Kamp vp = nd.ni_vp; 2316e9c0cc15SPoul-Henning Kamp 231720da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 23188f60c087SPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 2319dee34ca4SPoul-Henning Kamp if (sp->sw_vp == vp) 23200909f38aSPawel Jakub Dawidek break; 2321e9c0cc15SPoul-Henning Kamp } 232220da9c2eSPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 23230909f38aSPawel Jakub Dawidek if (sp == NULL) { 2324e9c0cc15SPoul-Henning Kamp error = EINVAL; 2325e9c0cc15SPoul-Henning Kamp goto done; 23260909f38aSPawel Jakub Dawidek } 232735918c55SChristian S.J. Peron error = swapoff_one(sp, td->td_ucred); 23280909f38aSPawel Jakub Dawidek done: 232904533e1eSKonstantin Belousov sx_xunlock(&swdev_syscall_lock); 23300909f38aSPawel Jakub Dawidek return (error); 23310909f38aSPawel Jakub Dawidek } 23320909f38aSPawel Jakub Dawidek 23330909f38aSPawel Jakub Dawidek static int 233435918c55SChristian S.J. Peron swapoff_one(struct swdevt *sp, struct ucred *cred) 23350909f38aSPawel Jakub Dawidek { 233603bdd65fSAlan Cox u_long nblks; 2337e9c0cc15SPoul-Henning Kamp #ifdef MAC 23380909f38aSPawel Jakub Dawidek int error; 2339e9c0cc15SPoul-Henning Kamp #endif 2340e9c0cc15SPoul-Henning Kamp 234104533e1eSKonstantin Belousov sx_assert(&swdev_syscall_lock, SA_XLOCKED); 23420909f38aSPawel Jakub Dawidek #ifdef MAC 2343cb05b60aSAttilio Rao (void) vn_lock(sp->sw_vp, LK_EXCLUSIVE | LK_RETRY); 234435918c55SChristian S.J. Peron error = mac_system_check_swapoff(cred, sp->sw_vp); 234522db15c0SAttilio Rao (void) VOP_UNLOCK(sp->sw_vp, 0); 23460909f38aSPawel Jakub Dawidek if (error != 0) 23470909f38aSPawel Jakub Dawidek return (error); 23480909f38aSPawel Jakub Dawidek #endif 2349e9c0cc15SPoul-Henning Kamp nblks = sp->sw_nblks; 2350e9c0cc15SPoul-Henning Kamp 2351e9c0cc15SPoul-Henning Kamp /* 2352e9c0cc15SPoul-Henning Kamp * We can turn off this swap device safely only if the 2353e9c0cc15SPoul-Henning Kamp * available virtual memory in the system will fit the amount 2354e9c0cc15SPoul-Henning Kamp * of data we will have to page back in, plus an epsilon so 2355e9c0cc15SPoul-Henning Kamp * the system doesn't become critically low on swap space. 2356e9c0cc15SPoul-Henning Kamp */ 2357e2068d0bSJeff Roberson if (vm_free_count() + swap_pager_avail < nblks + nswap_lowat) 23580909f38aSPawel Jakub Dawidek return (ENOMEM); 2359e9c0cc15SPoul-Henning Kamp 2360e9c0cc15SPoul-Henning Kamp /* 2361e9c0cc15SPoul-Henning Kamp * Prevent further allocations on this device. 2362e9c0cc15SPoul-Henning Kamp */ 23632928cef7SAlan Cox mtx_lock(&sw_dev_mtx); 2364e9c0cc15SPoul-Henning Kamp sp->sw_flags |= SW_CLOSING; 236503bdd65fSAlan Cox swap_pager_avail -= blist_fill(sp->sw_blist, 0, nblks); 2366e8bb589dSMatt Macy swap_total -= nblks; 23672928cef7SAlan Cox mtx_unlock(&sw_dev_mtx); 2368e9c0cc15SPoul-Henning Kamp 2369e9c0cc15SPoul-Henning Kamp /* 2370e9c0cc15SPoul-Henning Kamp * Page in the contents of the device and close it. 2371e9c0cc15SPoul-Henning Kamp */ 2372b3fed13eSDavid Schultz swap_pager_swapoff(sp); 2373e9c0cc15SPoul-Henning Kamp 237435918c55SChristian S.J. Peron sp->sw_close(curthread, sp); 237520da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 23769e3e3fe5SWarner Losh sp->sw_id = NULL; 23778f60c087SPoul-Henning Kamp TAILQ_REMOVE(&swtailq, sp, sw_list); 23780676a140SAlan Cox nswapdev--; 23797dea2c2eSAlan Cox if (nswapdev == 0) { 23807dea2c2eSAlan Cox swap_pager_full = 2; 23817dea2c2eSAlan Cox swap_pager_almost_full = 1; 23827dea2c2eSAlan Cox } 23838f60c087SPoul-Henning Kamp if (swdevhd == sp) 23848f60c087SPoul-Henning Kamp swdevhd = NULL; 2385d05bc129SAlan Cox mtx_unlock(&sw_dev_mtx); 23868f60c087SPoul-Henning Kamp blist_destroy(sp->sw_blist); 23878f60c087SPoul-Henning Kamp free(sp, M_VMPGDATA); 23880909f38aSPawel Jakub Dawidek return (0); 23890909f38aSPawel Jakub Dawidek } 2390e9c0cc15SPoul-Henning Kamp 23910909f38aSPawel Jakub Dawidek void 23920909f38aSPawel Jakub Dawidek swapoff_all(void) 23930909f38aSPawel Jakub Dawidek { 23940909f38aSPawel Jakub Dawidek struct swdevt *sp, *spt; 23950909f38aSPawel Jakub Dawidek const char *devname; 23960909f38aSPawel Jakub Dawidek int error; 23970909f38aSPawel Jakub Dawidek 239804533e1eSKonstantin Belousov sx_xlock(&swdev_syscall_lock); 23990909f38aSPawel Jakub Dawidek 24000909f38aSPawel Jakub Dawidek mtx_lock(&sw_dev_mtx); 24010909f38aSPawel Jakub Dawidek TAILQ_FOREACH_SAFE(sp, &swtailq, sw_list, spt) { 24020909f38aSPawel Jakub Dawidek mtx_unlock(&sw_dev_mtx); 24030909f38aSPawel Jakub Dawidek if (vn_isdisk(sp->sw_vp, NULL)) 24047870adb6SEd Schouten devname = devtoname(sp->sw_vp->v_rdev); 24050909f38aSPawel Jakub Dawidek else 24060909f38aSPawel Jakub Dawidek devname = "[file]"; 240735918c55SChristian S.J. Peron error = swapoff_one(sp, thread0.td_ucred); 24080909f38aSPawel Jakub Dawidek if (error != 0) { 24090909f38aSPawel Jakub Dawidek printf("Cannot remove swap device %s (error=%d), " 24100909f38aSPawel Jakub Dawidek "skipping.\n", devname, error); 24110909f38aSPawel Jakub Dawidek } else if (bootverbose) { 24120909f38aSPawel Jakub Dawidek printf("Swap device %s removed.\n", devname); 24130909f38aSPawel Jakub Dawidek } 24140909f38aSPawel Jakub Dawidek mtx_lock(&sw_dev_mtx); 24150909f38aSPawel Jakub Dawidek } 24160909f38aSPawel Jakub Dawidek mtx_unlock(&sw_dev_mtx); 24170909f38aSPawel Jakub Dawidek 241804533e1eSKonstantin Belousov sx_xunlock(&swdev_syscall_lock); 2419e9c0cc15SPoul-Henning Kamp } 2420e9c0cc15SPoul-Henning Kamp 2421567104a1SPoul-Henning Kamp void 2422567104a1SPoul-Henning Kamp swap_pager_status(int *total, int *used) 2423567104a1SPoul-Henning Kamp { 2424567104a1SPoul-Henning Kamp struct swdevt *sp; 2425567104a1SPoul-Henning Kamp 2426567104a1SPoul-Henning Kamp *total = 0; 2427567104a1SPoul-Henning Kamp *used = 0; 242820da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 24298f60c087SPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 2430567104a1SPoul-Henning Kamp *total += sp->sw_nblks; 2431567104a1SPoul-Henning Kamp *used += sp->sw_used; 2432567104a1SPoul-Henning Kamp } 243320da9c2eSPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 2434567104a1SPoul-Henning Kamp } 2435567104a1SPoul-Henning Kamp 2436dda4f960SKonstantin Belousov int 2437dda4f960SKonstantin Belousov swap_dev_info(int name, struct xswdev *xs, char *devname, size_t len) 2438dda4f960SKonstantin Belousov { 2439dda4f960SKonstantin Belousov struct swdevt *sp; 24407870adb6SEd Schouten const char *tmp_devname; 2441dda4f960SKonstantin Belousov int error, n; 2442dda4f960SKonstantin Belousov 2443dda4f960SKonstantin Belousov n = 0; 2444dda4f960SKonstantin Belousov error = ENOENT; 2445dda4f960SKonstantin Belousov mtx_lock(&sw_dev_mtx); 2446dda4f960SKonstantin Belousov TAILQ_FOREACH(sp, &swtailq, sw_list) { 2447dda4f960SKonstantin Belousov if (n != name) { 2448dda4f960SKonstantin Belousov n++; 2449dda4f960SKonstantin Belousov continue; 2450dda4f960SKonstantin Belousov } 2451dda4f960SKonstantin Belousov xs->xsw_version = XSWDEV_VERSION; 2452dda4f960SKonstantin Belousov xs->xsw_dev = sp->sw_dev; 2453dda4f960SKonstantin Belousov xs->xsw_flags = sp->sw_flags; 2454dda4f960SKonstantin Belousov xs->xsw_nblks = sp->sw_nblks; 2455dda4f960SKonstantin Belousov xs->xsw_used = sp->sw_used; 2456dda4f960SKonstantin Belousov if (devname != NULL) { 2457dda4f960SKonstantin Belousov if (vn_isdisk(sp->sw_vp, NULL)) 24587870adb6SEd Schouten tmp_devname = devtoname(sp->sw_vp->v_rdev); 2459dda4f960SKonstantin Belousov else 2460dda4f960SKonstantin Belousov tmp_devname = "[file]"; 2461dda4f960SKonstantin Belousov strncpy(devname, tmp_devname, len); 2462dda4f960SKonstantin Belousov } 2463dda4f960SKonstantin Belousov error = 0; 2464dda4f960SKonstantin Belousov break; 2465dda4f960SKonstantin Belousov } 2466dda4f960SKonstantin Belousov mtx_unlock(&sw_dev_mtx); 2467dda4f960SKonstantin Belousov return (error); 2468dda4f960SKonstantin Belousov } 2469dda4f960SKonstantin Belousov 247069921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11) 247169921123SKonstantin Belousov #define XSWDEV_VERSION_11 1 247269921123SKonstantin Belousov struct xswdev11 { 247369921123SKonstantin Belousov u_int xsw_version; 247469921123SKonstantin Belousov uint32_t xsw_dev; 247569921123SKonstantin Belousov int xsw_flags; 247669921123SKonstantin Belousov int xsw_nblks; 247769921123SKonstantin Belousov int xsw_used; 247869921123SKonstantin Belousov }; 247969921123SKonstantin Belousov #endif 248069921123SKonstantin Belousov 2481e9c0cc15SPoul-Henning Kamp static int 2482e9c0cc15SPoul-Henning Kamp sysctl_vm_swap_info(SYSCTL_HANDLER_ARGS) 2483e9c0cc15SPoul-Henning Kamp { 2484e9c0cc15SPoul-Henning Kamp struct xswdev xs; 248569921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11) 248669921123SKonstantin Belousov struct xswdev11 xs11; 248769921123SKonstantin Belousov #endif 2488dda4f960SKonstantin Belousov int error; 2489e9c0cc15SPoul-Henning Kamp 2490e9c0cc15SPoul-Henning Kamp if (arg2 != 1) /* name length */ 2491e9c0cc15SPoul-Henning Kamp return (EINVAL); 2492dda4f960SKonstantin Belousov error = swap_dev_info(*(int *)arg1, &xs, NULL, 0); 2493dda4f960SKonstantin Belousov if (error != 0) 2494dda4f960SKonstantin Belousov return (error); 249569921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11) 249669921123SKonstantin Belousov if (req->oldlen == sizeof(xs11)) { 249769921123SKonstantin Belousov xs11.xsw_version = XSWDEV_VERSION_11; 249869921123SKonstantin Belousov xs11.xsw_dev = xs.xsw_dev; /* truncation */ 249969921123SKonstantin Belousov xs11.xsw_flags = xs.xsw_flags; 250069921123SKonstantin Belousov xs11.xsw_nblks = xs.xsw_nblks; 250169921123SKonstantin Belousov xs11.xsw_used = xs.xsw_used; 250269921123SKonstantin Belousov error = SYSCTL_OUT(req, &xs11, sizeof(xs11)); 250369921123SKonstantin Belousov } else 250469921123SKonstantin Belousov #endif 2505e9c0cc15SPoul-Henning Kamp error = SYSCTL_OUT(req, &xs, sizeof(xs)); 2506e9c0cc15SPoul-Henning Kamp return (error); 2507e9c0cc15SPoul-Henning Kamp } 2508e9c0cc15SPoul-Henning Kamp 25098f60c087SPoul-Henning Kamp SYSCTL_INT(_vm, OID_AUTO, nswapdev, CTLFLAG_RD, &nswapdev, 0, 2510e9c0cc15SPoul-Henning Kamp "Number of swap devices"); 25114c36e917SKonstantin Belousov SYSCTL_NODE(_vm, OID_AUTO, swap_info, CTLFLAG_RD | CTLFLAG_MPSAFE, 25124c36e917SKonstantin Belousov sysctl_vm_swap_info, 2513e9c0cc15SPoul-Henning Kamp "Swap statistics by device"); 2514ec38b344SPoul-Henning Kamp 2515ec38b344SPoul-Henning Kamp /* 2516f425ab8eSKonstantin Belousov * Count the approximate swap usage in pages for a vmspace. The 2517f425ab8eSKonstantin Belousov * shadowed or not yet copied on write swap blocks are not accounted. 2518ec38b344SPoul-Henning Kamp * The map must be locked. 2519ec38b344SPoul-Henning Kamp */ 25202860553aSRebecca Cran long 2521ec38b344SPoul-Henning Kamp vmspace_swap_count(struct vmspace *vmspace) 2522ec38b344SPoul-Henning Kamp { 252365d8409cSRebecca Cran vm_map_t map; 2524ec38b344SPoul-Henning Kamp vm_map_entry_t cur; 252565d8409cSRebecca Cran vm_object_t object; 2526f425ab8eSKonstantin Belousov struct swblk *sb; 2527f425ab8eSKonstantin Belousov vm_pindex_t e, pi; 2528f425ab8eSKonstantin Belousov long count; 2529f425ab8eSKonstantin Belousov int i; 253065d8409cSRebecca Cran 253165d8409cSRebecca Cran map = &vmspace->vm_map; 253265d8409cSRebecca Cran count = 0; 2533ec38b344SPoul-Henning Kamp 2534ec38b344SPoul-Henning Kamp for (cur = map->header.next; cur != &map->header; cur = cur->next) { 2535f425ab8eSKonstantin Belousov if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) != 0) 2536f425ab8eSKonstantin Belousov continue; 2537f425ab8eSKonstantin Belousov object = cur->object.vm_object; 2538f425ab8eSKonstantin Belousov if (object == NULL || object->type != OBJT_SWAP) 2539f425ab8eSKonstantin Belousov continue; 2540f425ab8eSKonstantin Belousov VM_OBJECT_RLOCK(object); 2541f425ab8eSKonstantin Belousov if (object->type != OBJT_SWAP) 2542f425ab8eSKonstantin Belousov goto unlock; 2543f425ab8eSKonstantin Belousov pi = OFF_TO_IDX(cur->offset); 2544f425ab8eSKonstantin Belousov e = pi + OFF_TO_IDX(cur->end - cur->start); 2545f425ab8eSKonstantin Belousov for (;; pi = sb->p + SWAP_META_PAGES) { 2546f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP_GE( 2547f425ab8eSKonstantin Belousov &object->un_pager.swp.swp_blks, pi); 2548f425ab8eSKonstantin Belousov if (sb == NULL || sb->p >= e) 2549f425ab8eSKonstantin Belousov break; 2550f425ab8eSKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) { 2551f425ab8eSKonstantin Belousov if (sb->p + i < e && 2552f425ab8eSKonstantin Belousov sb->d[i] != SWAPBLK_NONE) 2553f425ab8eSKonstantin Belousov count++; 2554ec38b344SPoul-Henning Kamp } 2555ec38b344SPoul-Henning Kamp } 2556f425ab8eSKonstantin Belousov unlock: 2557f425ab8eSKonstantin Belousov VM_OBJECT_RUNLOCK(object); 2558ec38b344SPoul-Henning Kamp } 2559ec38b344SPoul-Henning Kamp return (count); 2560ec38b344SPoul-Henning Kamp } 2561dee34ca4SPoul-Henning Kamp 2562dee34ca4SPoul-Henning Kamp /* 2563dee34ca4SPoul-Henning Kamp * GEOM backend 2564dee34ca4SPoul-Henning Kamp * 2565dee34ca4SPoul-Henning Kamp * Swapping onto disk devices. 2566dee34ca4SPoul-Henning Kamp * 2567dee34ca4SPoul-Henning Kamp */ 2568dee34ca4SPoul-Henning Kamp 25695721c9c7SPoul-Henning Kamp static g_orphan_t swapgeom_orphan; 25705721c9c7SPoul-Henning Kamp 2571dee34ca4SPoul-Henning Kamp static struct g_class g_swap_class = { 2572dee34ca4SPoul-Henning Kamp .name = "SWAP", 25735721c9c7SPoul-Henning Kamp .version = G_VERSION, 25745721c9c7SPoul-Henning Kamp .orphan = swapgeom_orphan, 2575dee34ca4SPoul-Henning Kamp }; 2576dee34ca4SPoul-Henning Kamp 2577dee34ca4SPoul-Henning Kamp DECLARE_GEOM_CLASS(g_swap_class, g_class); 2578dee34ca4SPoul-Henning Kamp 2579dee34ca4SPoul-Henning Kamp 2580dee34ca4SPoul-Henning Kamp static void 25813398491bSAlexander Motin swapgeom_close_ev(void *arg, int flags) 25823398491bSAlexander Motin { 25833398491bSAlexander Motin struct g_consumer *cp; 25843398491bSAlexander Motin 25853398491bSAlexander Motin cp = arg; 25863398491bSAlexander Motin g_access(cp, -1, -1, 0); 25873398491bSAlexander Motin g_detach(cp); 25883398491bSAlexander Motin g_destroy_consumer(cp); 25893398491bSAlexander Motin } 25903398491bSAlexander Motin 25919e3e3fe5SWarner Losh /* 25929e3e3fe5SWarner Losh * Add a reference to the g_consumer for an inflight transaction. 25939e3e3fe5SWarner Losh */ 25949e3e3fe5SWarner Losh static void 25959e3e3fe5SWarner Losh swapgeom_acquire(struct g_consumer *cp) 25969e3e3fe5SWarner Losh { 25979e3e3fe5SWarner Losh 25989e3e3fe5SWarner Losh mtx_assert(&sw_dev_mtx, MA_OWNED); 25999e3e3fe5SWarner Losh cp->index++; 26009e3e3fe5SWarner Losh } 26019e3e3fe5SWarner Losh 26029e3e3fe5SWarner Losh /* 26030c657d22SKonstantin Belousov * Remove a reference from the g_consumer. Post a close event if all 26040c657d22SKonstantin Belousov * references go away, since the function might be called from the 26050c657d22SKonstantin Belousov * biodone context. 26069e3e3fe5SWarner Losh */ 26079e3e3fe5SWarner Losh static void 26089e3e3fe5SWarner Losh swapgeom_release(struct g_consumer *cp, struct swdevt *sp) 26099e3e3fe5SWarner Losh { 26109e3e3fe5SWarner Losh 26119e3e3fe5SWarner Losh mtx_assert(&sw_dev_mtx, MA_OWNED); 26129e3e3fe5SWarner Losh cp->index--; 26139e3e3fe5SWarner Losh if (cp->index == 0) { 26149e3e3fe5SWarner Losh if (g_post_event(swapgeom_close_ev, cp, M_NOWAIT, NULL) == 0) 26159e3e3fe5SWarner Losh sp->sw_id = NULL; 26169e3e3fe5SWarner Losh } 26179e3e3fe5SWarner Losh } 26189e3e3fe5SWarner Losh 26193398491bSAlexander Motin static void 2620dee34ca4SPoul-Henning Kamp swapgeom_done(struct bio *bp2) 2621dee34ca4SPoul-Henning Kamp { 26223398491bSAlexander Motin struct swdevt *sp; 2623dee34ca4SPoul-Henning Kamp struct buf *bp; 26243398491bSAlexander Motin struct g_consumer *cp; 2625dee34ca4SPoul-Henning Kamp 2626dee34ca4SPoul-Henning Kamp bp = bp2->bio_caller2; 26273398491bSAlexander Motin cp = bp2->bio_from; 2628c5d3d25eSPoul-Henning Kamp bp->b_ioflags = bp2->bio_flags; 2629dee34ca4SPoul-Henning Kamp if (bp2->bio_error) 2630dee34ca4SPoul-Henning Kamp bp->b_ioflags |= BIO_ERROR; 2631c5d3d25eSPoul-Henning Kamp bp->b_resid = bp->b_bcount - bp2->bio_completed; 2632c5d3d25eSPoul-Henning Kamp bp->b_error = bp2->bio_error; 2633*756a5412SGleb Smirnoff bp->b_caller1 = NULL; 2634dee34ca4SPoul-Henning Kamp bufdone(bp); 26353398491bSAlexander Motin sp = bp2->bio_caller1; 26369e3e3fe5SWarner Losh mtx_lock(&sw_dev_mtx); 26379e3e3fe5SWarner Losh swapgeom_release(cp, sp); 26383398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 2639dee34ca4SPoul-Henning Kamp g_destroy_bio(bp2); 2640dee34ca4SPoul-Henning Kamp } 2641dee34ca4SPoul-Henning Kamp 2642dee34ca4SPoul-Henning Kamp static void 2643dee34ca4SPoul-Henning Kamp swapgeom_strategy(struct buf *bp, struct swdevt *sp) 2644dee34ca4SPoul-Henning Kamp { 2645dee34ca4SPoul-Henning Kamp struct bio *bio; 2646dee34ca4SPoul-Henning Kamp struct g_consumer *cp; 2647dee34ca4SPoul-Henning Kamp 26483398491bSAlexander Motin mtx_lock(&sw_dev_mtx); 2649dee34ca4SPoul-Henning Kamp cp = sp->sw_id; 2650dee34ca4SPoul-Henning Kamp if (cp == NULL) { 26513398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 2652dee34ca4SPoul-Henning Kamp bp->b_error = ENXIO; 2653dee34ca4SPoul-Henning Kamp bp->b_ioflags |= BIO_ERROR; 2654dee34ca4SPoul-Henning Kamp bufdone(bp); 2655dee34ca4SPoul-Henning Kamp return; 2656dee34ca4SPoul-Henning Kamp } 26579e3e3fe5SWarner Losh swapgeom_acquire(cp); 26583398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 265911041003SKonstantin Belousov if (bp->b_iocmd == BIO_WRITE) 266011041003SKonstantin Belousov bio = g_new_bio(); 266111041003SKonstantin Belousov else 26624f8205e5SPoul-Henning Kamp bio = g_alloc_bio(); 26634f8205e5SPoul-Henning Kamp if (bio == NULL) { 26649e3e3fe5SWarner Losh mtx_lock(&sw_dev_mtx); 26659e3e3fe5SWarner Losh swapgeom_release(cp, sp); 26669e3e3fe5SWarner Losh mtx_unlock(&sw_dev_mtx); 26673e5b6861SPoul-Henning Kamp bp->b_error = ENOMEM; 26683e5b6861SPoul-Henning Kamp bp->b_ioflags |= BIO_ERROR; 26693e5b6861SPoul-Henning Kamp bufdone(bp); 26703e5b6861SPoul-Henning Kamp return; 26713e5b6861SPoul-Henning Kamp } 267211041003SKonstantin Belousov 2673*756a5412SGleb Smirnoff bp->b_caller1 = bio; 26743398491bSAlexander Motin bio->bio_caller1 = sp; 2675dee34ca4SPoul-Henning Kamp bio->bio_caller2 = bp; 2676c5d3d25eSPoul-Henning Kamp bio->bio_cmd = bp->b_iocmd; 2677dee34ca4SPoul-Henning Kamp bio->bio_offset = (bp->b_blkno - sp->sw_first) * PAGE_SIZE; 2678dee34ca4SPoul-Henning Kamp bio->bio_length = bp->b_bcount; 2679dee34ca4SPoul-Henning Kamp bio->bio_done = swapgeom_done; 2680fade8dd7SJeff Roberson if (!buf_mapped(bp)) { 26812cc718a1SKonstantin Belousov bio->bio_ma = bp->b_pages; 26822cc718a1SKonstantin Belousov bio->bio_data = unmapped_buf; 26832cc718a1SKonstantin Belousov bio->bio_ma_offset = (vm_offset_t)bp->b_offset & PAGE_MASK; 26842cc718a1SKonstantin Belousov bio->bio_ma_n = bp->b_npages; 26852cc718a1SKonstantin Belousov bio->bio_flags |= BIO_UNMAPPED; 26862cc718a1SKonstantin Belousov } else { 26872cc718a1SKonstantin Belousov bio->bio_data = bp->b_data; 26882cc718a1SKonstantin Belousov bio->bio_ma = NULL; 26892cc718a1SKonstantin Belousov } 2690dee34ca4SPoul-Henning Kamp g_io_request(bio, cp); 2691dee34ca4SPoul-Henning Kamp return; 2692dee34ca4SPoul-Henning Kamp } 2693dee34ca4SPoul-Henning Kamp 2694dee34ca4SPoul-Henning Kamp static void 2695dee34ca4SPoul-Henning Kamp swapgeom_orphan(struct g_consumer *cp) 2696dee34ca4SPoul-Henning Kamp { 2697dee34ca4SPoul-Henning Kamp struct swdevt *sp; 26983398491bSAlexander Motin int destroy; 2699dee34ca4SPoul-Henning Kamp 2700dee34ca4SPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 27013398491bSAlexander Motin TAILQ_FOREACH(sp, &swtailq, sw_list) { 27023398491bSAlexander Motin if (sp->sw_id == cp) { 27038f12d83aSAlexander Motin sp->sw_flags |= SW_CLOSING; 27043398491bSAlexander Motin break; 2705dee34ca4SPoul-Henning Kamp } 27063398491bSAlexander Motin } 27079e3e3fe5SWarner Losh /* 27089e3e3fe5SWarner Losh * Drop reference we were created with. Do directly since we're in a 27099e3e3fe5SWarner Losh * special context where we don't have to queue the call to 27109e3e3fe5SWarner Losh * swapgeom_close_ev(). 27119e3e3fe5SWarner Losh */ 27129e3e3fe5SWarner Losh cp->index--; 27133398491bSAlexander Motin destroy = ((sp != NULL) && (cp->index == 0)); 27143398491bSAlexander Motin if (destroy) 27153398491bSAlexander Motin sp->sw_id = NULL; 27163398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 27173398491bSAlexander Motin if (destroy) 27183398491bSAlexander Motin swapgeom_close_ev(cp, 0); 2719dee34ca4SPoul-Henning Kamp } 2720dee34ca4SPoul-Henning Kamp 2721dee34ca4SPoul-Henning Kamp static void 2722dee34ca4SPoul-Henning Kamp swapgeom_close(struct thread *td, struct swdevt *sw) 2723dee34ca4SPoul-Henning Kamp { 27243398491bSAlexander Motin struct g_consumer *cp; 2725dee34ca4SPoul-Henning Kamp 27263398491bSAlexander Motin mtx_lock(&sw_dev_mtx); 27273398491bSAlexander Motin cp = sw->sw_id; 27283398491bSAlexander Motin sw->sw_id = NULL; 27293398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 27300c657d22SKonstantin Belousov 27310c657d22SKonstantin Belousov /* 27320c657d22SKonstantin Belousov * swapgeom_close() may be called from the biodone context, 27330c657d22SKonstantin Belousov * where we cannot perform topology changes. Delegate the 27340c657d22SKonstantin Belousov * work to the events thread. 27350c657d22SKonstantin Belousov */ 27363398491bSAlexander Motin if (cp != NULL) 27373398491bSAlexander Motin g_waitfor_event(swapgeom_close_ev, cp, M_WAITOK, NULL); 2738dee34ca4SPoul-Henning Kamp } 2739dee34ca4SPoul-Henning Kamp 274088ad2d7bSKonstantin Belousov static int 274188ad2d7bSKonstantin Belousov swapongeom_locked(struct cdev *dev, struct vnode *vp) 2742dee34ca4SPoul-Henning Kamp { 2743dee34ca4SPoul-Henning Kamp struct g_provider *pp; 2744dee34ca4SPoul-Henning Kamp struct g_consumer *cp; 2745dee34ca4SPoul-Henning Kamp static struct g_geom *gp; 2746dee34ca4SPoul-Henning Kamp struct swdevt *sp; 2747dee34ca4SPoul-Henning Kamp u_long nblks; 2748dee34ca4SPoul-Henning Kamp int error; 2749dee34ca4SPoul-Henning Kamp 275088ad2d7bSKonstantin Belousov pp = g_dev_getprovider(dev); 275188ad2d7bSKonstantin Belousov if (pp == NULL) 275288ad2d7bSKonstantin Belousov return (ENODEV); 2753dee34ca4SPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 2754dee34ca4SPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 2755dee34ca4SPoul-Henning Kamp cp = sp->sw_id; 2756dee34ca4SPoul-Henning Kamp if (cp != NULL && cp->provider == pp) { 2757dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 275888ad2d7bSKonstantin Belousov return (EBUSY); 2759dee34ca4SPoul-Henning Kamp } 2760dee34ca4SPoul-Henning Kamp } 2761dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 27625721c9c7SPoul-Henning Kamp if (gp == NULL) 276302c62349SJaakko Heinonen gp = g_new_geomf(&g_swap_class, "swap"); 2764dee34ca4SPoul-Henning Kamp cp = g_new_consumer(gp); 27659e3e3fe5SWarner Losh cp->index = 1; /* Number of active I/Os, plus one for being active. */ 27669e3e3fe5SWarner Losh cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 2767dee34ca4SPoul-Henning Kamp g_attach(cp, pp); 2768afeb65e6SPoul-Henning Kamp /* 2769afeb65e6SPoul-Henning Kamp * XXX: Every time you think you can improve the margin for 2770afeb65e6SPoul-Henning Kamp * footshooting, somebody depends on the ability to do so: 2771afeb65e6SPoul-Henning Kamp * savecore(8) wants to write to our swapdev so we cannot 2772afeb65e6SPoul-Henning Kamp * set an exclusive count :-( 2773afeb65e6SPoul-Henning Kamp */ 2774d2bae332SPoul-Henning Kamp error = g_access(cp, 1, 1, 0); 277588ad2d7bSKonstantin Belousov if (error != 0) { 2776dee34ca4SPoul-Henning Kamp g_detach(cp); 2777dee34ca4SPoul-Henning Kamp g_destroy_consumer(cp); 277888ad2d7bSKonstantin Belousov return (error); 2779dee34ca4SPoul-Henning Kamp } 2780dee34ca4SPoul-Henning Kamp nblks = pp->mediasize / DEV_BSIZE; 278188ad2d7bSKonstantin Belousov swaponsomething(vp, cp, nblks, swapgeom_strategy, 278288ad2d7bSKonstantin Belousov swapgeom_close, dev2udev(dev), 27832cc718a1SKonstantin Belousov (pp->flags & G_PF_ACCEPT_UNMAPPED) != 0 ? SW_UNMAPPED : 0); 278488ad2d7bSKonstantin Belousov return (0); 2785dee34ca4SPoul-Henning Kamp } 2786dee34ca4SPoul-Henning Kamp 2787dee34ca4SPoul-Henning Kamp static int 278888ad2d7bSKonstantin Belousov swapongeom(struct vnode *vp) 2789dee34ca4SPoul-Henning Kamp { 2790dee34ca4SPoul-Henning Kamp int error; 2791dee34ca4SPoul-Henning Kamp 2792cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 279388ad2d7bSKonstantin Belousov if (vp->v_type != VCHR || (vp->v_iflag & VI_DOOMED) != 0) { 279488ad2d7bSKonstantin Belousov error = ENOENT; 279588ad2d7bSKonstantin Belousov } else { 279688ad2d7bSKonstantin Belousov g_topology_lock(); 279788ad2d7bSKonstantin Belousov error = swapongeom_locked(vp->v_rdev, vp); 279888ad2d7bSKonstantin Belousov g_topology_unlock(); 279988ad2d7bSKonstantin Belousov } 280022db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 2801dee34ca4SPoul-Henning Kamp return (error); 2802dee34ca4SPoul-Henning Kamp } 2803dee34ca4SPoul-Henning Kamp 2804dee34ca4SPoul-Henning Kamp /* 2805dee34ca4SPoul-Henning Kamp * VNODE backend 2806dee34ca4SPoul-Henning Kamp * 2807dee34ca4SPoul-Henning Kamp * This is used mainly for network filesystem (read: probably only tested 2808dee34ca4SPoul-Henning Kamp * with NFS) swapfiles. 2809dee34ca4SPoul-Henning Kamp * 2810dee34ca4SPoul-Henning Kamp */ 2811dee34ca4SPoul-Henning Kamp 2812dee34ca4SPoul-Henning Kamp static void 2813dee34ca4SPoul-Henning Kamp swapdev_strategy(struct buf *bp, struct swdevt *sp) 2814dee34ca4SPoul-Henning Kamp { 2815494eb176SPoul-Henning Kamp struct vnode *vp2; 2816dee34ca4SPoul-Henning Kamp 2817dee34ca4SPoul-Henning Kamp bp->b_blkno = ctodb(bp->b_blkno - sp->sw_first); 2818dee34ca4SPoul-Henning Kamp 2819dee34ca4SPoul-Henning Kamp vp2 = sp->sw_id; 2820dee34ca4SPoul-Henning Kamp vhold(vp2); 2821dee34ca4SPoul-Henning Kamp if (bp->b_iocmd == BIO_WRITE) { 28223cfc7651SOlivier Houchard if (bp->b_bufobj) 2823494eb176SPoul-Henning Kamp bufobj_wdrop(bp->b_bufobj); 2824a76d8f4eSPoul-Henning Kamp bufobj_wref(&vp2->v_bufobj); 2825dee34ca4SPoul-Henning Kamp } 28263cfc7651SOlivier Houchard if (bp->b_bufobj != &vp2->v_bufobj) 28273cfc7651SOlivier Houchard bp->b_bufobj = &vp2->v_bufobj; 2828dee34ca4SPoul-Henning Kamp bp->b_vp = vp2; 28292c18019fSPoul-Henning Kamp bp->b_iooffset = dbtob(bp->b_blkno); 2830b792bebeSPoul-Henning Kamp bstrategy(bp); 2831dee34ca4SPoul-Henning Kamp return; 2832dee34ca4SPoul-Henning Kamp } 2833dee34ca4SPoul-Henning Kamp 2834dee34ca4SPoul-Henning Kamp static void 2835dee34ca4SPoul-Henning Kamp swapdev_close(struct thread *td, struct swdevt *sp) 2836dee34ca4SPoul-Henning Kamp { 2837dee34ca4SPoul-Henning Kamp 2838dee34ca4SPoul-Henning Kamp VOP_CLOSE(sp->sw_vp, FREAD | FWRITE, td->td_ucred, td); 2839dee34ca4SPoul-Henning Kamp vrele(sp->sw_vp); 2840dee34ca4SPoul-Henning Kamp } 2841dee34ca4SPoul-Henning Kamp 2842dee34ca4SPoul-Henning Kamp 2843dee34ca4SPoul-Henning Kamp static int 2844dee34ca4SPoul-Henning Kamp swaponvp(struct thread *td, struct vnode *vp, u_long nblks) 2845dee34ca4SPoul-Henning Kamp { 2846dee34ca4SPoul-Henning Kamp struct swdevt *sp; 2847dee34ca4SPoul-Henning Kamp int error; 2848dee34ca4SPoul-Henning Kamp 2849dee34ca4SPoul-Henning Kamp if (nblks == 0) 2850dee34ca4SPoul-Henning Kamp return (ENXIO); 2851dee34ca4SPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 2852dee34ca4SPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 2853dee34ca4SPoul-Henning Kamp if (sp->sw_id == vp) { 2854dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 2855dee34ca4SPoul-Henning Kamp return (EBUSY); 2856dee34ca4SPoul-Henning Kamp } 2857dee34ca4SPoul-Henning Kamp } 2858dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 2859dee34ca4SPoul-Henning Kamp 2860cb05b60aSAttilio Rao (void) vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2861dee34ca4SPoul-Henning Kamp #ifdef MAC 286230d239bcSRobert Watson error = mac_system_check_swapon(td->td_ucred, vp); 2863dee34ca4SPoul-Henning Kamp if (error == 0) 2864dee34ca4SPoul-Henning Kamp #endif 28659e223287SKonstantin Belousov error = VOP_OPEN(vp, FREAD | FWRITE, td->td_ucred, td, NULL); 286622db15c0SAttilio Rao (void) VOP_UNLOCK(vp, 0); 2867dee34ca4SPoul-Henning Kamp if (error) 2868dee34ca4SPoul-Henning Kamp return (error); 2869dee34ca4SPoul-Henning Kamp 2870dee34ca4SPoul-Henning Kamp swaponsomething(vp, vp, nblks, swapdev_strategy, swapdev_close, 28712cc718a1SKonstantin Belousov NODEV, 0); 2872dee34ca4SPoul-Henning Kamp return (0); 2873dee34ca4SPoul-Henning Kamp } 287489c241d1SGleb Smirnoff 287589c241d1SGleb Smirnoff static int 287689c241d1SGleb Smirnoff sysctl_swap_async_max(SYSCTL_HANDLER_ARGS) 287789c241d1SGleb Smirnoff { 287889c241d1SGleb Smirnoff int error, new, n; 287989c241d1SGleb Smirnoff 288089c241d1SGleb Smirnoff new = nsw_wcount_async_max; 288189c241d1SGleb Smirnoff error = sysctl_handle_int(oidp, &new, 0, req); 288289c241d1SGleb Smirnoff if (error != 0 || req->newptr == NULL) 288389c241d1SGleb Smirnoff return (error); 288489c241d1SGleb Smirnoff 288589c241d1SGleb Smirnoff if (new > nswbuf / 2 || new < 1) 288689c241d1SGleb Smirnoff return (EINVAL); 288789c241d1SGleb Smirnoff 2888*756a5412SGleb Smirnoff mtx_lock(&swbuf_mtx); 288989c241d1SGleb Smirnoff while (nsw_wcount_async_max != new) { 289089c241d1SGleb Smirnoff /* 289189c241d1SGleb Smirnoff * Adjust difference. If the current async count is too low, 289289c241d1SGleb Smirnoff * we will need to sqeeze our update slowly in. Sleep with a 289389c241d1SGleb Smirnoff * higher priority than getpbuf() to finish faster. 289489c241d1SGleb Smirnoff */ 289589c241d1SGleb Smirnoff n = new - nsw_wcount_async_max; 289689c241d1SGleb Smirnoff if (nsw_wcount_async + n >= 0) { 289789c241d1SGleb Smirnoff nsw_wcount_async += n; 289889c241d1SGleb Smirnoff nsw_wcount_async_max += n; 289989c241d1SGleb Smirnoff wakeup(&nsw_wcount_async); 290089c241d1SGleb Smirnoff } else { 290189c241d1SGleb Smirnoff nsw_wcount_async_max -= nsw_wcount_async; 290289c241d1SGleb Smirnoff nsw_wcount_async = 0; 2903*756a5412SGleb Smirnoff msleep(&nsw_wcount_async, &swbuf_mtx, PSWP, 290489c241d1SGleb Smirnoff "swpsysctl", 0); 290589c241d1SGleb Smirnoff } 290689c241d1SGleb Smirnoff } 2907*756a5412SGleb Smirnoff mtx_unlock(&swbuf_mtx); 290889c241d1SGleb Smirnoff 290989c241d1SGleb Smirnoff return (0); 291089c241d1SGleb Smirnoff } 2911