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> 779626b608SPoul-Henning Kamp #include <sys/bio.h> 78e2e050c8SConrad Meyer #include <sys/blist.h> 79df8bae1dSRodney W. Grimes #include <sys/buf.h> 80e2e050c8SConrad Meyer #include <sys/conf.h> 81e9c0cc15SPoul-Henning Kamp #include <sys/disk.h> 82504f5e29SDoug Moore #include <sys/disklabel.h> 83e2e050c8SConrad Meyer #include <sys/eventhandler.h> 84e9c0cc15SPoul-Henning Kamp #include <sys/fcntl.h> 85686aa928SMark Johnston #include <sys/limits.h> 86e2e050c8SConrad Meyer #include <sys/lock.h> 87e2e050c8SConrad Meyer #include <sys/kernel.h> 88e9c0cc15SPoul-Henning Kamp #include <sys/mount.h> 89e9c0cc15SPoul-Henning Kamp #include <sys/namei.h> 90df8bae1dSRodney W. Grimes #include <sys/malloc.h> 91f425ab8eSKonstantin Belousov #include <sys/pctrie.h> 92e2e050c8SConrad Meyer #include <sys/priv.h> 93e2e050c8SConrad Meyer #include <sys/proc.h> 941ba5ad42SEdward Tomasz Napierala #include <sys/racct.h> 953364c323SKonstantin Belousov #include <sys/resource.h> 963364c323SKonstantin Belousov #include <sys/resourcevar.h> 9789f6b863SAttilio Rao #include <sys/rwlock.h> 98d027ed2eSAlan Cox #include <sys/sbuf.h> 99327f4e83SMatthew Dillon #include <sys/sysctl.h> 100e9c0cc15SPoul-Henning Kamp #include <sys/sysproto.h> 101e2e050c8SConrad Meyer #include <sys/systm.h> 1020cddd8f0SMatthew Dillon #include <sys/sx.h> 10353465702SKonstantin Belousov #include <sys/unistd.h> 10400a3fe96SKonstantin Belousov #include <sys/user.h> 105936524aaSMatthew Dillon #include <sys/vmmeter.h> 106e2e050c8SConrad Meyer #include <sys/vnode.h> 107df8bae1dSRodney W. Grimes 108aed55708SRobert Watson #include <security/mac/mac_framework.h> 109aed55708SRobert Watson 110df8bae1dSRodney W. Grimes #include <vm/vm.h> 11121cd6e62SSeigo Tanimura #include <vm/pmap.h> 11221cd6e62SSeigo Tanimura #include <vm/vm_map.h> 11321cd6e62SSeigo Tanimura #include <vm/vm_kern.h> 114efeaf95aSDavid Greenman #include <vm/vm_object.h> 115df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 116efeaf95aSDavid Greenman #include <vm/vm_pager.h> 117df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h> 118e9c0cc15SPoul-Henning Kamp #include <vm/vm_param.h> 119df8bae1dSRodney W. Grimes #include <vm/swap_pager.h> 120efeaf95aSDavid Greenman #include <vm/vm_extern.h> 121670d17b5SJeff Roberson #include <vm/uma.h> 122df8bae1dSRodney W. Grimes 123dee34ca4SPoul-Henning Kamp #include <geom/geom.h> 124dee34ca4SPoul-Henning Kamp 125ec38b344SPoul-Henning Kamp /* 126064650c1SAlan Cox * MAX_PAGEOUT_CLUSTER must be a power of 2 between 1 and 64. 127064650c1SAlan Cox * The 64-page limit is due to the radix code (kern/subr_blist.c). 128ec38b344SPoul-Henning Kamp */ 129ec38b344SPoul-Henning Kamp #ifndef MAX_PAGEOUT_CLUSTER 130e2241590SAlan Cox #define MAX_PAGEOUT_CLUSTER 32 131ec38b344SPoul-Henning Kamp #endif 132ec38b344SPoul-Henning Kamp 133ec38b344SPoul-Henning Kamp #if !defined(SWB_NPAGES) 134ec38b344SPoul-Henning Kamp #define SWB_NPAGES MAX_PAGEOUT_CLUSTER 135ec38b344SPoul-Henning Kamp #endif 136ec38b344SPoul-Henning Kamp 137f425ab8eSKonstantin Belousov #define SWAP_META_PAGES PCTRIE_COUNT 138ec38b344SPoul-Henning Kamp 139f425ab8eSKonstantin Belousov /* 140f425ab8eSKonstantin Belousov * A swblk structure maps each page index within a 141f425ab8eSKonstantin Belousov * SWAP_META_PAGES-aligned and sized range to the address of an 142f425ab8eSKonstantin Belousov * on-disk swap block (or SWAPBLK_NONE). The collection of these 143f425ab8eSKonstantin Belousov * mappings for an entire vm object is implemented as a pc-trie. 144f425ab8eSKonstantin Belousov */ 145f425ab8eSKonstantin Belousov struct swblk { 146f425ab8eSKonstantin Belousov vm_pindex_t p; 147f425ab8eSKonstantin Belousov daddr_t d[SWAP_META_PAGES]; 148e9c0cc15SPoul-Henning Kamp }; 149e9c0cc15SPoul-Henning Kamp 1502c4992dbSAlan Cox static MALLOC_DEFINE(M_VMPGDATA, "vm_pgdata", "swap pager private data"); 15120da9c2eSPoul-Henning Kamp static struct mtx sw_dev_mtx; 1528f60c087SPoul-Henning Kamp static TAILQ_HEAD(, swdevt) swtailq = TAILQ_HEAD_INITIALIZER(swtailq); 1538f60c087SPoul-Henning Kamp static struct swdevt *swdevhd; /* Allocate from here next */ 1548f60c087SPoul-Henning Kamp static int nswapdev; /* Number of swap devices */ 1558f60c087SPoul-Henning Kamp int swap_pager_avail; 15604533e1eSKonstantin Belousov static struct sx swdev_syscall_lock; /* serialize swap(on|off) */ 157e9c0cc15SPoul-Henning Kamp 158126a2470SMateusz Guzik static __exclusive_cache_line u_long swap_reserved; 159e8bb589dSMatt Macy static u_long swap_total; 160e8bb589dSMatt Macy static int sysctl_page_shift(SYSCTL_HANDLER_ARGS); 161a8081778SJeff Roberson 1627029da5cSPawel Biernacki static SYSCTL_NODE(_vm_stats, OID_AUTO, swap, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 1637029da5cSPawel Biernacki "VM swap stats"); 164a8081778SJeff Roberson 165e8bb589dSMatt Macy SYSCTL_PROC(_vm, OID_AUTO, swap_reserved, CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE, 166*567378ccSEnji Cooper &swap_reserved, 0, sysctl_page_shift, "QU", 1678a9c731fSIvan Voras "Amount of swap storage needed to back all allocated anonymous memory."); 168e8bb589dSMatt Macy SYSCTL_PROC(_vm, OID_AUTO, swap_total, CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE, 169*567378ccSEnji Cooper &swap_total, 0, sysctl_page_shift, "QU", 170e8bb589dSMatt Macy "Total amount of available swap storage."); 171e8bb589dSMatt Macy 1723364c323SKonstantin Belousov static int overcommit = 0; 173be7d4ac5SEdward Tomasz Napierala SYSCTL_INT(_vm, VM_OVERCOMMIT, overcommit, CTLFLAG_RW, &overcommit, 0, 1748a9c731fSIvan Voras "Configure virtual memory overcommit behavior. See tuning(7) " 1758a9c731fSIvan Voras "for details."); 17661203277SDag-Erling Smørgrav static unsigned long swzone; 17761203277SDag-Erling Smørgrav SYSCTL_ULONG(_vm, OID_AUTO, swzone, CTLFLAG_RD, &swzone, 0, 17861203277SDag-Erling Smørgrav "Actual size of swap metadata zone"); 17961203277SDag-Erling Smørgrav static unsigned long swap_maxpages; 18061203277SDag-Erling Smørgrav SYSCTL_ULONG(_vm, OID_AUTO, swap_maxpages, CTLFLAG_RD, &swap_maxpages, 0, 18161203277SDag-Erling Smørgrav "Maximum amount of swap supported"); 1823364c323SKonstantin Belousov 183d869a17eSMark Johnston static COUNTER_U64_DEFINE_EARLY(swap_free_deferred); 184a8081778SJeff Roberson SYSCTL_COUNTER_U64(_vm_stats_swap, OID_AUTO, free_deferred, 185a8081778SJeff Roberson CTLFLAG_RD, &swap_free_deferred, 186a8081778SJeff Roberson "Number of pages that deferred freeing swap space"); 187a8081778SJeff Roberson 188d869a17eSMark Johnston static COUNTER_U64_DEFINE_EARLY(swap_free_completed); 189a8081778SJeff Roberson SYSCTL_COUNTER_U64(_vm_stats_swap, OID_AUTO, free_completed, 190a8081778SJeff Roberson CTLFLAG_RD, &swap_free_completed, 191a8081778SJeff Roberson "Number of deferred frees completed"); 192a8081778SJeff Roberson 1933364c323SKonstantin Belousov /* bits from overcommit */ 1943364c323SKonstantin Belousov #define SWAP_RESERVE_FORCE_ON (1 << 0) 1953364c323SKonstantin Belousov #define SWAP_RESERVE_RLIMIT_ON (1 << 1) 1963364c323SKonstantin Belousov #define SWAP_RESERVE_ALLOW_NONWIRED (1 << 2) 1973364c323SKonstantin Belousov 198e8bb589dSMatt Macy static int 199e8bb589dSMatt Macy sysctl_page_shift(SYSCTL_HANDLER_ARGS) 200e8bb589dSMatt Macy { 201e8bb589dSMatt Macy uint64_t newval; 202e8bb589dSMatt Macy u_long value = *(u_long *)arg1; 203e8bb589dSMatt Macy 204e8bb589dSMatt Macy newval = ((uint64_t)value) << PAGE_SHIFT; 205e8bb589dSMatt Macy return (sysctl_handle_64(oidp, &newval, 0, req)); 206e8bb589dSMatt Macy } 207e8bb589dSMatt Macy 208ee744122SMateusz Guzik static bool 209ee744122SMateusz Guzik swap_reserve_by_cred_rlimit(u_long pincr, struct ucred *cred, int oc) 210ee744122SMateusz Guzik { 211ee744122SMateusz Guzik struct uidinfo *uip; 212ee744122SMateusz Guzik u_long prev; 213ee744122SMateusz Guzik 214ee744122SMateusz Guzik uip = cred->cr_ruidinfo; 215ee744122SMateusz Guzik 216ee744122SMateusz Guzik prev = atomic_fetchadd_long(&uip->ui_vmsize, pincr); 217ee744122SMateusz Guzik if ((oc & SWAP_RESERVE_RLIMIT_ON) != 0 && 218ee744122SMateusz Guzik prev + pincr > lim_cur(curthread, RLIMIT_SWAP) && 219ee744122SMateusz Guzik priv_check(curthread, PRIV_VM_SWAP_NORLIMIT) != 0) { 220ee744122SMateusz Guzik prev = atomic_fetchadd_long(&uip->ui_vmsize, -pincr); 221ee744122SMateusz Guzik KASSERT(prev >= pincr, ("negative vmsize for uid = %d\n", uip->ui_uid)); 222ee744122SMateusz Guzik return (false); 223ee744122SMateusz Guzik } 224ee744122SMateusz Guzik return (true); 225ee744122SMateusz Guzik } 226ee744122SMateusz Guzik 227ee744122SMateusz Guzik static void 228ee744122SMateusz Guzik swap_release_by_cred_rlimit(u_long pdecr, struct ucred *cred) 229ee744122SMateusz Guzik { 230ee744122SMateusz Guzik struct uidinfo *uip; 231ee744122SMateusz Guzik #ifdef INVARIANTS 232ee744122SMateusz Guzik u_long prev; 233ee744122SMateusz Guzik #endif 234ee744122SMateusz Guzik 235ee744122SMateusz Guzik uip = cred->cr_ruidinfo; 236ee744122SMateusz Guzik 237ee744122SMateusz Guzik #ifdef INVARIANTS 238ee744122SMateusz Guzik prev = atomic_fetchadd_long(&uip->ui_vmsize, -pdecr); 239ee744122SMateusz Guzik KASSERT(prev >= pdecr, ("negative vmsize for uid = %d\n", uip->ui_uid)); 240ee744122SMateusz Guzik #else 241ee744122SMateusz Guzik atomic_subtract_long(&uip->ui_vmsize, pdecr); 242ee744122SMateusz Guzik #endif 243ee744122SMateusz Guzik } 244ee744122SMateusz Guzik 245ee744122SMateusz Guzik static void 246ee744122SMateusz Guzik swap_reserve_force_rlimit(u_long pincr, struct ucred *cred) 247ee744122SMateusz Guzik { 248ee744122SMateusz Guzik struct uidinfo *uip; 249ee744122SMateusz Guzik 250ee744122SMateusz Guzik uip = cred->cr_ruidinfo; 251ee744122SMateusz Guzik atomic_add_long(&uip->ui_vmsize, pincr); 252ee744122SMateusz Guzik } 253ee744122SMateusz Guzik 254ee744122SMateusz Guzik bool 2553364c323SKonstantin Belousov swap_reserve(vm_ooffset_t incr) 2563364c323SKonstantin Belousov { 2573364c323SKonstantin Belousov 258ef694c1aSEdward Tomasz Napierala return (swap_reserve_by_cred(incr, curthread->td_ucred)); 2593364c323SKonstantin Belousov } 2603364c323SKonstantin Belousov 261ee744122SMateusz Guzik bool 262ef694c1aSEdward Tomasz Napierala swap_reserve_by_cred(vm_ooffset_t incr, struct ucred *cred) 2633364c323SKonstantin Belousov { 264e8bb589dSMatt Macy u_long r, s, prev, pincr; 265ee744122SMateusz Guzik #ifdef RACCT 266ee744122SMateusz Guzik int error; 267ee744122SMateusz Guzik #endif 268ee744122SMateusz Guzik int oc; 2693364c323SKonstantin Belousov static int curfail; 2703364c323SKonstantin Belousov static struct timeval lastfail; 2713364c323SKonstantin Belousov 272e8bb589dSMatt Macy KASSERT((incr & PAGE_MASK) == 0, ("%s: incr: %ju & PAGE_MASK", __func__, 273e8bb589dSMatt Macy (uintmax_t)incr)); 2743364c323SKonstantin Belousov 275afcc55f3SEdward Tomasz Napierala #ifdef RACCT 276ee744122SMateusz Guzik if (RACCT_ENABLED()) { 2771ba5ad42SEdward Tomasz Napierala PROC_LOCK(curproc); 2781ba5ad42SEdward Tomasz Napierala error = racct_add(curproc, RACCT_SWAP, incr); 2791ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(curproc); 2801ba5ad42SEdward Tomasz Napierala if (error != 0) 281ee744122SMateusz Guzik return (false); 2824b5c9cf6SEdward Tomasz Napierala } 283afcc55f3SEdward Tomasz Napierala #endif 2841ba5ad42SEdward Tomasz Napierala 285e8bb589dSMatt Macy pincr = atop(incr); 286e8bb589dSMatt Macy prev = atomic_fetchadd_long(&swap_reserved, pincr); 287e8bb589dSMatt Macy r = prev + pincr; 288ee744122SMateusz Guzik s = swap_total; 289ee744122SMateusz Guzik oc = atomic_load_int(&overcommit); 290ee744122SMateusz Guzik if (r > s && (oc & SWAP_RESERVE_ALLOW_NONWIRED) != 0) { 291ee744122SMateusz Guzik s += vm_cnt.v_page_count - vm_cnt.v_free_reserved - 292e958ad4cSJeff Roberson vm_wire_count(); 293ee744122SMateusz Guzik } 294ee744122SMateusz Guzik if ((oc & SWAP_RESERVE_FORCE_ON) != 0 && r > s && 295ee744122SMateusz Guzik priv_check(curthread, PRIV_VM_SWAP_NOQUOTA) != 0) { 296e8bb589dSMatt Macy prev = atomic_fetchadd_long(&swap_reserved, -pincr); 297ee744122SMateusz Guzik KASSERT(prev >= pincr, ("swap_reserved < incr on overcommit fail")); 298ee744122SMateusz Guzik goto out_error; 2993364c323SKonstantin Belousov } 3003364c323SKonstantin Belousov 301ee744122SMateusz Guzik if (!swap_reserve_by_cred_rlimit(pincr, cred, oc)) { 302ee744122SMateusz Guzik prev = atomic_fetchadd_long(&swap_reserved, -pincr); 303ee744122SMateusz Guzik KASSERT(prev >= pincr, ("swap_reserved < incr on overcommit fail")); 304ee744122SMateusz Guzik goto out_error; 305ee744122SMateusz Guzik } 306ee744122SMateusz Guzik 307ee744122SMateusz Guzik return (true); 308ee744122SMateusz Guzik 309ee744122SMateusz Guzik out_error: 310ee744122SMateusz Guzik if (ppsratecheck(&lastfail, &curfail, 1)) { 311ee744122SMateusz Guzik printf("uid %d, pid %d: swap reservation for %jd bytes failed\n", 312ee744122SMateusz Guzik cred->cr_ruidinfo->ui_uid, curproc->p_pid, incr); 313ee744122SMateusz Guzik } 314afcc55f3SEdward Tomasz Napierala #ifdef RACCT 315ee744122SMateusz Guzik if (RACCT_ENABLED()) { 3161ba5ad42SEdward Tomasz Napierala PROC_LOCK(curproc); 3171ba5ad42SEdward Tomasz Napierala racct_sub(curproc, RACCT_SWAP, incr); 3181ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(curproc); 3191ba5ad42SEdward Tomasz Napierala } 320afcc55f3SEdward Tomasz Napierala #endif 3211ba5ad42SEdward Tomasz Napierala 322ee744122SMateusz Guzik return (false); 3233364c323SKonstantin Belousov } 3243364c323SKonstantin Belousov 3253364c323SKonstantin Belousov void 3263364c323SKonstantin Belousov swap_reserve_force(vm_ooffset_t incr) 3273364c323SKonstantin Belousov { 328e8bb589dSMatt Macy u_long pincr; 3293364c323SKonstantin Belousov 330e8bb589dSMatt Macy KASSERT((incr & PAGE_MASK) == 0, ("%s: incr: %ju & PAGE_MASK", __func__, 331e8bb589dSMatt Macy (uintmax_t)incr)); 3323364c323SKonstantin Belousov 333afcc55f3SEdward Tomasz Napierala #ifdef RACCT 334ee744122SMateusz Guzik if (RACCT_ENABLED()) { 335ee744122SMateusz Guzik PROC_LOCK(curproc); 3361ba5ad42SEdward Tomasz Napierala racct_add_force(curproc, RACCT_SWAP, incr); 337ee744122SMateusz Guzik PROC_UNLOCK(curproc); 338ee744122SMateusz Guzik } 339afcc55f3SEdward Tomasz Napierala #endif 340e8bb589dSMatt Macy pincr = atop(incr); 341e8bb589dSMatt Macy atomic_add_long(&swap_reserved, pincr); 342ee744122SMateusz Guzik swap_reserve_force_rlimit(pincr, curthread->td_ucred); 3433364c323SKonstantin Belousov } 3443364c323SKonstantin Belousov 3453364c323SKonstantin Belousov void 3463364c323SKonstantin Belousov swap_release(vm_ooffset_t decr) 3473364c323SKonstantin Belousov { 348ef694c1aSEdward Tomasz Napierala struct ucred *cred; 3493364c323SKonstantin Belousov 3503364c323SKonstantin Belousov PROC_LOCK(curproc); 351e8bb589dSMatt Macy cred = curproc->p_ucred; 352ef694c1aSEdward Tomasz Napierala swap_release_by_cred(decr, cred); 3533364c323SKonstantin Belousov PROC_UNLOCK(curproc); 3543364c323SKonstantin Belousov } 3553364c323SKonstantin Belousov 3563364c323SKonstantin Belousov void 357ef694c1aSEdward Tomasz Napierala swap_release_by_cred(vm_ooffset_t decr, struct ucred *cred) 3583364c323SKonstantin Belousov { 359ee744122SMateusz Guzik u_long pdecr; 360ee744122SMateusz Guzik #ifdef INVARIANTS 361ee744122SMateusz Guzik u_long prev; 362ee744122SMateusz Guzik #endif 3633364c323SKonstantin Belousov 364e8bb589dSMatt Macy KASSERT((decr & PAGE_MASK) == 0, ("%s: decr: %ju & PAGE_MASK", __func__, 365e8bb589dSMatt Macy (uintmax_t)decr)); 3663364c323SKonstantin Belousov 367e8bb589dSMatt Macy pdecr = atop(decr); 368ee744122SMateusz Guzik #ifdef INVARIANTS 369e8bb589dSMatt Macy prev = atomic_fetchadd_long(&swap_reserved, -pdecr); 370ee744122SMateusz Guzik KASSERT(prev >= pdecr, ("swap_reserved < decr")); 371ee744122SMateusz Guzik #else 372ee744122SMateusz Guzik atomic_subtract_long(&swap_reserved, pdecr); 373ee744122SMateusz Guzik #endif 3743364c323SKonstantin Belousov 375ee744122SMateusz Guzik swap_release_by_cred_rlimit(pdecr, cred); 376e8bb589dSMatt Macy #ifdef RACCT 377e8bb589dSMatt Macy if (racct_enable) 3781ba5ad42SEdward Tomasz Napierala racct_sub_cred(cred, RACCT_SWAP, decr); 379e8bb589dSMatt Macy #endif 3803364c323SKonstantin Belousov } 3813364c323SKonstantin Belousov 382f08b3099SKonstantin Belousov static int swap_pager_full = 2; /* swap space exhaustion (task killing) */ 3837dea2c2eSAlan Cox static int swap_pager_almost_full = 1; /* swap space exhaustion (w/hysteresis)*/ 384756a5412SGleb Smirnoff static struct mtx swbuf_mtx; /* to sync nsw_wcount_async */ 385756a5412SGleb Smirnoff static int nsw_wcount_async; /* limit async write buffers */ 386327f4e83SMatthew Dillon static int nsw_wcount_async_max;/* assigned maximum */ 387183f8e1eSGleb Smirnoff int nsw_cluster_max; /* maximum VOP I/O allowed */ 3881c7c3c6aSMatthew Dillon 38989c241d1SGleb Smirnoff static int sysctl_swap_async_max(SYSCTL_HANDLER_ARGS); 3904c36e917SKonstantin Belousov SYSCTL_PROC(_vm, OID_AUTO, swap_async_max, CTLTYPE_INT | CTLFLAG_RW | 3914c36e917SKonstantin Belousov CTLFLAG_MPSAFE, NULL, 0, sysctl_swap_async_max, "I", 3924c36e917SKonstantin Belousov "Maximum running async swap ops"); 393d027ed2eSAlan Cox static int sysctl_swap_fragmentation(SYSCTL_HANDLER_ARGS); 394d027ed2eSAlan Cox SYSCTL_PROC(_vm, OID_AUTO, swap_fragmentation, CTLTYPE_STRING | CTLFLAG_RD | 395d027ed2eSAlan Cox CTLFLAG_MPSAFE, NULL, 0, sysctl_swap_fragmentation, "A", 396d027ed2eSAlan Cox "Swap Fragmentation Info"); 39789c241d1SGleb Smirnoff 3980cddd8f0SMatthew Dillon static struct sx sw_alloc_sx; 399327f4e83SMatthew Dillon 4001c7c3c6aSMatthew Dillon /* 4011c7c3c6aSMatthew Dillon * "named" and "unnamed" anon region objects. Try to reduce the overhead 4021c7c3c6aSMatthew Dillon * of searching a named list by hashing it just a little. 4031c7c3c6aSMatthew Dillon */ 4041c7c3c6aSMatthew Dillon 4051c7c3c6aSMatthew Dillon #define NOBJLISTS 8 4061c7c3c6aSMatthew Dillon 4071c7c3c6aSMatthew Dillon #define NOBJLIST(handle) \ 408af647ddeSBruce Evans (&swap_pager_object_list[((int)(intptr_t)handle >> 4) & (NOBJLISTS-1)]) 4091c7c3c6aSMatthew Dillon 4101c7c3c6aSMatthew Dillon static struct pagerlst swap_pager_object_list[NOBJLISTS]; 411756a5412SGleb Smirnoff static uma_zone_t swwbuf_zone; 412756a5412SGleb Smirnoff static uma_zone_t swrbuf_zone; 413f425ab8eSKonstantin Belousov static uma_zone_t swblk_zone; 414f425ab8eSKonstantin Belousov static uma_zone_t swpctrie_zone; 4151c7c3c6aSMatthew Dillon 4161c7c3c6aSMatthew Dillon /* 4171c7c3c6aSMatthew Dillon * pagerops for OBJT_SWAP - "swap pager". Some ops are also global procedure 4181c7c3c6aSMatthew Dillon * calls hooked from other parts of the VM system and do not appear here. 4191c7c3c6aSMatthew Dillon * (see vm/swap_pager.h). 4201c7c3c6aSMatthew Dillon */ 421ff98689dSBruce Evans static vm_object_t 42211caded3SAlfred Perlstein swap_pager_alloc(void *handle, vm_ooffset_t size, 4233364c323SKonstantin Belousov vm_prot_t prot, vm_ooffset_t offset, struct ucred *); 42411caded3SAlfred Perlstein static void swap_pager_dealloc(vm_object_t object); 425b0cd2017SGleb Smirnoff static int swap_pager_getpages(vm_object_t, vm_page_t *, int, int *, 426b0cd2017SGleb Smirnoff int *); 427b0cd2017SGleb Smirnoff static int swap_pager_getpages_async(vm_object_t, vm_page_t *, int, int *, 428b0cd2017SGleb Smirnoff int *, pgo_getpages_iodone_t, void *); 429751221fdSPoul-Henning Kamp static void swap_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *); 4305ea4972cSAlan Cox static boolean_t 4315ea4972cSAlan Cox swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, int *after); 43211caded3SAlfred Perlstein static void swap_pager_init(void); 43311caded3SAlfred Perlstein static void swap_pager_unswapped(vm_page_t); 434b3fed13eSDavid Schultz static void swap_pager_swapoff(struct swdevt *sp); 435fe7bcbafSKyle Evans static void swap_pager_update_writecount(vm_object_t object, 436fe7bcbafSKyle Evans vm_offset_t start, vm_offset_t end); 437fe7bcbafSKyle Evans static void swap_pager_release_writecount(vm_object_t object, 438fe7bcbafSKyle Evans vm_offset_t start, vm_offset_t end); 4391390a5cbSKonstantin Belousov static void swap_pager_freespace(vm_object_t object, vm_pindex_t start, 4401390a5cbSKonstantin Belousov vm_size_t size); 441f708ef1bSPoul-Henning Kamp 442d474440aSKonstantin Belousov const struct pagerops swappagerops = { 44300a3fe96SKonstantin Belousov .pgo_kvme_type = KVME_TYPE_SWAP, 444e04e4bacSPoul-Henning Kamp .pgo_init = swap_pager_init, /* early system initialization of pager */ 445e04e4bacSPoul-Henning Kamp .pgo_alloc = swap_pager_alloc, /* allocate an OBJT_SWAP object */ 446e04e4bacSPoul-Henning Kamp .pgo_dealloc = swap_pager_dealloc, /* deallocate an OBJT_SWAP object */ 447e04e4bacSPoul-Henning Kamp .pgo_getpages = swap_pager_getpages, /* pagein */ 44890effb23SGleb Smirnoff .pgo_getpages_async = swap_pager_getpages_async, /* pagein (async) */ 449e04e4bacSPoul-Henning Kamp .pgo_putpages = swap_pager_putpages, /* pageout */ 450e04e4bacSPoul-Henning Kamp .pgo_haspage = swap_pager_haspage, /* get backing store status for page */ 451e04e4bacSPoul-Henning Kamp .pgo_pageunswapped = swap_pager_unswapped, /* remove swap related to page */ 452fe7bcbafSKyle Evans .pgo_update_writecount = swap_pager_update_writecount, 453fe7bcbafSKyle Evans .pgo_release_writecount = swap_pager_release_writecount, 4544b8365d7SKonstantin Belousov .pgo_freespace = swap_pager_freespace, 4554b8365d7SKonstantin Belousov }; 4564b8365d7SKonstantin Belousov 4571c7c3c6aSMatthew Dillon /* 4581c7c3c6aSMatthew Dillon * swap_*() routines are externally accessible. swp_*() routines are 4591c7c3c6aSMatthew Dillon * internal. 4601c7c3c6aSMatthew Dillon */ 461e9c0cc15SPoul-Henning Kamp static int nswap_lowat = 128; /* in pages, swap_pager_almost_full warn */ 462e9c0cc15SPoul-Henning Kamp static int nswap_hiwat = 512; /* in pages, swap_pager_almost_full warn */ 46326f9a767SRodney W. Grimes 46407c348eaSAlan Cox SYSCTL_INT(_vm, OID_AUTO, dmmax, CTLFLAG_RD, &nsw_cluster_max, 0, 46507c348eaSAlan Cox "Maximum size of a swap block in pages"); 466cee313c4SRobert Watson 467a5edd34aSPoul-Henning Kamp static void swp_sizecheck(void); 46811caded3SAlfred Perlstein static void swp_pager_async_iodone(struct buf *bp); 469230869e0SAlan Cox static bool swp_pager_swblk_empty(struct swblk *sb, int start, int limit); 470abdab7b6SDoug Moore static void swp_pager_free_empty_swblk(vm_object_t, struct swblk *sb); 47188ad2d7bSKonstantin Belousov static int swapongeom(struct vnode *); 47259efee01SPoul-Henning Kamp static int swaponvp(struct thread *, struct vnode *, u_long); 4730190c38bSKonstantin Belousov static int swapoff_one(struct swdevt *sp, struct ucred *cred, 474e8dc2ba2SKonstantin Belousov u_int flags); 47524a1cce3SDavid Greenman 4761c7c3c6aSMatthew Dillon /* 4771c7c3c6aSMatthew Dillon * Swap bitmap functions 4781c7c3c6aSMatthew Dillon */ 479230869e0SAlan Cox static void swp_pager_freeswapspace(daddr_t blk, daddr_t npages); 48036b01270SDoug Moore static daddr_t swp_pager_getswapspace(int *npages); 4811c7c3c6aSMatthew Dillon 4821c7c3c6aSMatthew Dillon /* 4831c7c3c6aSMatthew Dillon * Metadata functions 4841c7c3c6aSMatthew Dillon */ 48578f1deefSAlan Cox static daddr_t swp_pager_meta_build(vm_object_t, vm_pindex_t, daddr_t); 4862e56b64fSKonstantin Belousov static void swp_pager_meta_free(vm_object_t, vm_pindex_t, vm_pindex_t); 487467057fcSDoug Moore static void swp_pager_meta_transfer(vm_object_t src, vm_object_t dst, 488467057fcSDoug Moore vm_pindex_t pindex, vm_pindex_t count); 48911caded3SAlfred Perlstein static void swp_pager_meta_free_all(vm_object_t); 4908ecbf14bSDoug Moore static daddr_t swp_pager_meta_lookup(vm_object_t, vm_pindex_t); 4911c7c3c6aSMatthew Dillon 49278f1deefSAlan Cox static void 49378f1deefSAlan Cox swp_pager_init_freerange(daddr_t *start, daddr_t *num) 49478f1deefSAlan Cox { 49578f1deefSAlan Cox 49678f1deefSAlan Cox *start = SWAPBLK_NONE; 49778f1deefSAlan Cox *num = 0; 49878f1deefSAlan Cox } 49978f1deefSAlan Cox 50078f1deefSAlan Cox static void 50178f1deefSAlan Cox swp_pager_update_freerange(daddr_t *start, daddr_t *num, daddr_t addr) 50278f1deefSAlan Cox { 50378f1deefSAlan Cox 50478f1deefSAlan Cox if (*start + *num == addr) { 50578f1deefSAlan Cox (*num)++; 50678f1deefSAlan Cox } else { 50778f1deefSAlan Cox swp_pager_freeswapspace(*start, *num); 50878f1deefSAlan Cox *start = addr; 50978f1deefSAlan Cox *num = 1; 51078f1deefSAlan Cox } 51178f1deefSAlan Cox } 51278f1deefSAlan Cox 513f425ab8eSKonstantin Belousov static void * 514f425ab8eSKonstantin Belousov swblk_trie_alloc(struct pctrie *ptree) 515f425ab8eSKonstantin Belousov { 516f425ab8eSKonstantin Belousov 517f425ab8eSKonstantin Belousov return (uma_zalloc(swpctrie_zone, M_NOWAIT | (curproc == pageproc ? 518f425ab8eSKonstantin Belousov M_USE_RESERVE : 0))); 519f425ab8eSKonstantin Belousov } 520f425ab8eSKonstantin Belousov 521f425ab8eSKonstantin Belousov static void 522f425ab8eSKonstantin Belousov swblk_trie_free(struct pctrie *ptree, void *node) 523f425ab8eSKonstantin Belousov { 524f425ab8eSKonstantin Belousov 525f425ab8eSKonstantin Belousov uma_zfree(swpctrie_zone, node); 526f425ab8eSKonstantin Belousov } 527f425ab8eSKonstantin Belousov 528f425ab8eSKonstantin Belousov PCTRIE_DEFINE(SWAP, swblk, p, swblk_trie_alloc, swblk_trie_free); 529f425ab8eSKonstantin Belousov 5301c7c3c6aSMatthew Dillon /* 5311c7c3c6aSMatthew Dillon * SWP_SIZECHECK() - update swap_pager_full indication 5321c7c3c6aSMatthew Dillon * 53320d3034fSMatthew Dillon * update the swap_pager_almost_full indication and warn when we are 53420d3034fSMatthew Dillon * about to run out of swap space, using lowat/hiwat hysteresis. 53520d3034fSMatthew Dillon * 53620d3034fSMatthew Dillon * Clear swap_pager_full ( task killing ) indication when lowat is met. 5371c7c3c6aSMatthew Dillon * 5381c7c3c6aSMatthew Dillon * No restrictions on call 5391c7c3c6aSMatthew Dillon * This routine may not block. 5401c7c3c6aSMatthew Dillon */ 541a5edd34aSPoul-Henning Kamp static void 5422f249180SPoul-Henning Kamp swp_sizecheck(void) 5430d94caffSDavid Greenman { 54423955314SAlfred Perlstein 5458f60c087SPoul-Henning Kamp if (swap_pager_avail < nswap_lowat) { 54620d3034fSMatthew Dillon if (swap_pager_almost_full == 0) { 5471af87c92SDavid Greenman printf("swap_pager: out of swap space\n"); 54820d3034fSMatthew Dillon swap_pager_almost_full = 1; 5492b0d37a4SMatthew Dillon } 55020d3034fSMatthew Dillon } else { 55126f9a767SRodney W. Grimes swap_pager_full = 0; 5528f60c087SPoul-Henning Kamp if (swap_pager_avail > nswap_hiwat) 55320d3034fSMatthew Dillon swap_pager_almost_full = 0; 55426f9a767SRodney W. Grimes } 5551c7c3c6aSMatthew Dillon } 5561c7c3c6aSMatthew Dillon 5571c7c3c6aSMatthew Dillon /* 5581c7c3c6aSMatthew Dillon * SWAP_PAGER_INIT() - initialize the swap pager! 5591c7c3c6aSMatthew Dillon * 5601c7c3c6aSMatthew Dillon * Expected to be started from system init. NOTE: This code is run 5611c7c3c6aSMatthew Dillon * before much else so be careful what you depend on. Most of the VM 5621c7c3c6aSMatthew Dillon * system has yet to be initialized at this point. 5631c7c3c6aSMatthew Dillon */ 564f5a12711SPoul-Henning Kamp static void 5652f249180SPoul-Henning Kamp swap_pager_init(void) 566df8bae1dSRodney W. Grimes { 5671c7c3c6aSMatthew Dillon /* 5681c7c3c6aSMatthew Dillon * Initialize object lists 5691c7c3c6aSMatthew Dillon */ 5701c7c3c6aSMatthew Dillon int i; 5711c7c3c6aSMatthew Dillon 5721c7c3c6aSMatthew Dillon for (i = 0; i < NOBJLISTS; ++i) 5731c7c3c6aSMatthew Dillon TAILQ_INIT(&swap_pager_object_list[i]); 57420da9c2eSPoul-Henning Kamp mtx_init(&sw_dev_mtx, "swapdev", NULL, MTX_DEF); 57515719273SKonstantin Belousov sx_init(&sw_alloc_sx, "swspsx"); 57604533e1eSKonstantin Belousov sx_init(&swdev_syscall_lock, "swsysc"); 577183f8e1eSGleb Smirnoff 578183f8e1eSGleb Smirnoff /* 579183f8e1eSGleb Smirnoff * The nsw_cluster_max is constrained by the bp->b_pages[] 580183f8e1eSGleb Smirnoff * array, which has maxphys / PAGE_SIZE entries, and our locally 581183f8e1eSGleb Smirnoff * defined MAX_PAGEOUT_CLUSTER. Also be aware that swap ops are 582183f8e1eSGleb Smirnoff * constrained by the swap device interleave stripe size. 583183f8e1eSGleb Smirnoff * 584183f8e1eSGleb Smirnoff * Initialized early so that GEOM_ELI can see it. 585183f8e1eSGleb Smirnoff */ 586183f8e1eSGleb Smirnoff nsw_cluster_max = min(maxphys / PAGE_SIZE, MAX_PAGEOUT_CLUSTER); 5871c7c3c6aSMatthew Dillon } 58826f9a767SRodney W. Grimes 589df8bae1dSRodney W. Grimes /* 5901c7c3c6aSMatthew Dillon * SWAP_PAGER_SWAP_INIT() - swap pager initialization from pageout process 5911c7c3c6aSMatthew Dillon * 5921c7c3c6aSMatthew Dillon * Expected to be started from pageout process once, prior to entering 5931c7c3c6aSMatthew Dillon * its main loop. 594df8bae1dSRodney W. Grimes */ 59524a1cce3SDavid Greenman void 5962f249180SPoul-Henning Kamp swap_pager_swap_init(void) 597df8bae1dSRodney W. Grimes { 59861203277SDag-Erling Smørgrav unsigned long n, n2; 5990d94caffSDavid Greenman 60026f9a767SRodney W. Grimes /* 6011c7c3c6aSMatthew Dillon * Number of in-transit swap bp operations. Don't 6021c7c3c6aSMatthew Dillon * exhaust the pbufs completely. Make sure we 6031c7c3c6aSMatthew Dillon * initialize workable values (0 will work for hysteresis 6041c7c3c6aSMatthew Dillon * but it isn't very efficient). 6051c7c3c6aSMatthew Dillon * 606327f4e83SMatthew Dillon * Currently we hardwire nsw_wcount_async to 4. This limit is 607327f4e83SMatthew Dillon * designed to prevent other I/O from having high latencies due to 608327f4e83SMatthew Dillon * our pageout I/O. The value 4 works well for one or two active swap 609327f4e83SMatthew Dillon * devices but is probably a little low if you have more. Even so, 610327f4e83SMatthew Dillon * a higher value would probably generate only a limited improvement 611327f4e83SMatthew Dillon * with three or four active swap devices since the system does not 612327f4e83SMatthew Dillon * typically have to pageout at extreme bandwidths. We will want 613327f4e83SMatthew Dillon * at least 2 per swap devices, and 4 is a pretty good value if you 614327f4e83SMatthew Dillon * have one NFS swap device due to the command/ack latency over NFS. 615327f4e83SMatthew Dillon * So it all works out pretty well. 616183f8e1eSGleb Smirnoff * 617183f8e1eSGleb Smirnoff * nsw_cluster_max is initialized in swap_pager_init(). 61826f9a767SRodney W. Grimes */ 619327f4e83SMatthew Dillon 620327f4e83SMatthew Dillon nsw_wcount_async = 4; 621327f4e83SMatthew Dillon nsw_wcount_async_max = nsw_wcount_async; 622756a5412SGleb Smirnoff mtx_init(&swbuf_mtx, "async swbuf mutex", NULL, MTX_DEF); 623756a5412SGleb Smirnoff 624756a5412SGleb Smirnoff swwbuf_zone = pbuf_zsecond_create("swwbuf", nswbuf / 4); 625756a5412SGleb Smirnoff swrbuf_zone = pbuf_zsecond_create("swrbuf", nswbuf / 2); 62624a1cce3SDavid Greenman 6271c7c3c6aSMatthew Dillon /* 628a8233027SKonstantin Belousov * Initialize our zone, taking the user's requested size or 629a8233027SKonstantin Belousov * estimating the number we need based on the number of pages 630a8233027SKonstantin Belousov * in the system. 6311c7c3c6aSMatthew Dillon */ 632a8233027SKonstantin Belousov n = maxswzone != 0 ? maxswzone / sizeof(struct swblk) : 633a8233027SKonstantin Belousov vm_cnt.v_page_count / 2; 634f425ab8eSKonstantin Belousov swpctrie_zone = uma_zcreate("swpctrie", pctrie_node_size(), NULL, NULL, 6356c5f36ffSJeff Roberson pctrie_zone_init, NULL, UMA_ALIGN_PTR, 0); 636f425ab8eSKonstantin Belousov swblk_zone = uma_zcreate("swblk", sizeof(struct swblk), NULL, NULL, 6376c5f36ffSJeff Roberson NULL, NULL, _Alignof(struct swblk) - 1, 0); 63822a5e6b9SDag-Erling Smørgrav n2 = n; 6398355f576SJeff Roberson do { 640f425ab8eSKonstantin Belousov if (uma_zone_reserve_kva(swblk_zone, n)) 64161ce6eeeSAlfred Perlstein break; 64261ce6eeeSAlfred Perlstein /* 64361ce6eeeSAlfred Perlstein * if the allocation failed, try a zone two thirds the 64461ce6eeeSAlfred Perlstein * size of the previous attempt. 64561ce6eeeSAlfred Perlstein */ 64661ce6eeeSAlfred Perlstein n -= ((n + 2) / 3); 64761ce6eeeSAlfred Perlstein } while (n > 0); 64853faf5a7SKonstantin Belousov 64953faf5a7SKonstantin Belousov /* 65053faf5a7SKonstantin Belousov * Often uma_zone_reserve_kva() cannot reserve exactly the 65153faf5a7SKonstantin Belousov * requested size. Account for the difference when 65253faf5a7SKonstantin Belousov * calculating swap_maxpages. 65353faf5a7SKonstantin Belousov */ 65453faf5a7SKonstantin Belousov n = uma_zone_get_max(swblk_zone); 65553faf5a7SKonstantin Belousov 6561fffcd75SKonstantin Belousov if (n < n2) 657a8233027SKonstantin Belousov printf("Swap blk zone entries changed from %lu to %lu.\n", 658f425ab8eSKonstantin Belousov n2, n); 659303fa05aSKonstantin Belousov /* absolute maximum we can handle assuming 100% efficiency */ 66061203277SDag-Erling Smørgrav swap_maxpages = n * SWAP_META_PAGES; 661f425ab8eSKonstantin Belousov swzone = n * sizeof(struct swblk); 662f425ab8eSKonstantin Belousov if (!uma_zone_reserve_kva(swpctrie_zone, n)) 663f425ab8eSKonstantin Belousov printf("Cannot reserve swap pctrie zone, " 664f425ab8eSKonstantin Belousov "reduce kern.maxswzone.\n"); 66524a1cce3SDavid Greenman } 66624a1cce3SDavid Greenman 66728bc23abSKonstantin Belousov bool 66828bc23abSKonstantin Belousov swap_pager_init_object(vm_object_t object, void *handle, struct ucred *cred, 66928bc23abSKonstantin Belousov vm_ooffset_t size, vm_ooffset_t offset) 67028bc23abSKonstantin Belousov { 67128bc23abSKonstantin Belousov if (cred != NULL) { 67228bc23abSKonstantin Belousov if (!swap_reserve_by_cred(size, cred)) 67328bc23abSKonstantin Belousov return (false); 67428bc23abSKonstantin Belousov crhold(cred); 67528bc23abSKonstantin Belousov } 67628bc23abSKonstantin Belousov 67728bc23abSKonstantin Belousov object->un_pager.swp.writemappings = 0; 67828bc23abSKonstantin Belousov object->handle = handle; 67928bc23abSKonstantin Belousov if (cred != NULL) { 68028bc23abSKonstantin Belousov object->cred = cred; 68128bc23abSKonstantin Belousov object->charge = size; 68228bc23abSKonstantin Belousov } 68328bc23abSKonstantin Belousov return (true); 68428bc23abSKonstantin Belousov } 68528bc23abSKonstantin Belousov 686eb4d6a1bSKonstantin Belousov static vm_object_t 6874b8365d7SKonstantin Belousov swap_pager_alloc_init(objtype_t otype, void *handle, struct ucred *cred, 6884b8365d7SKonstantin Belousov vm_ooffset_t size, vm_ooffset_t offset) 689eb4d6a1bSKonstantin Belousov { 690eb4d6a1bSKonstantin Belousov vm_object_t object; 691eb4d6a1bSKonstantin Belousov 692f425ab8eSKonstantin Belousov /* 693f425ab8eSKonstantin Belousov * The un_pager.swp.swp_blks trie is initialized by 694f425ab8eSKonstantin Belousov * vm_object_allocate() to ensure the correct order of 695f425ab8eSKonstantin Belousov * visibility to other threads. 696f425ab8eSKonstantin Belousov */ 6974b8365d7SKonstantin Belousov object = vm_object_allocate(otype, OFF_TO_IDX(offset + 698eb4d6a1bSKonstantin Belousov PAGE_MASK + size)); 699f425ab8eSKonstantin Belousov 70028bc23abSKonstantin Belousov if (!swap_pager_init_object(object, handle, cred, size, offset)) { 70128bc23abSKonstantin Belousov vm_object_deallocate(object); 70228bc23abSKonstantin Belousov return (NULL); 703eb4d6a1bSKonstantin Belousov } 704eb4d6a1bSKonstantin Belousov return (object); 705eb4d6a1bSKonstantin Belousov } 706eb4d6a1bSKonstantin Belousov 70724a1cce3SDavid Greenman /* 7081c7c3c6aSMatthew Dillon * SWAP_PAGER_ALLOC() - allocate a new OBJT_SWAP VM object and instantiate 7091c7c3c6aSMatthew Dillon * its metadata structures. 7101c7c3c6aSMatthew Dillon * 7111c7c3c6aSMatthew Dillon * This routine is called from the mmap and fork code to create a new 712eb4d6a1bSKonstantin Belousov * OBJT_SWAP object. 7131c7c3c6aSMatthew Dillon * 714eb4d6a1bSKonstantin Belousov * This routine must ensure that no live duplicate is created for 715eb4d6a1bSKonstantin Belousov * the named object request, which is protected against by 716eb4d6a1bSKonstantin Belousov * holding the sw_alloc_sx lock in case handle != NULL. 71724a1cce3SDavid Greenman */ 718f5a12711SPoul-Henning Kamp static vm_object_t 7196cde7a16SDavid Greenman swap_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 7203364c323SKonstantin Belousov vm_ooffset_t offset, struct ucred *cred) 72124a1cce3SDavid Greenman { 72224a1cce3SDavid Greenman vm_object_t object; 7232f7af3dbSAlan Cox 724eb4d6a1bSKonstantin Belousov if (handle != NULL) { 7251c7c3c6aSMatthew Dillon /* 7261c7c3c6aSMatthew Dillon * Reference existing named region or allocate new one. There 7271c7c3c6aSMatthew Dillon * should not be a race here against swp_pager_meta_build() 7281c7c3c6aSMatthew Dillon * as called from vm_page_remove() in regards to the lookup 7291c7c3c6aSMatthew Dillon * of the handle. 7301c7c3c6aSMatthew Dillon */ 7310cddd8f0SMatthew Dillon sx_xlock(&sw_alloc_sx); 7321c7c3c6aSMatthew Dillon object = vm_pager_object_lookup(NOBJLIST(handle), handle); 733b5e8f167SAlan Cox if (object == NULL) { 7344b8365d7SKonstantin Belousov object = swap_pager_alloc_init(OBJT_SWAP, handle, cred, 7354b8365d7SKonstantin Belousov size, offset); 736eb4d6a1bSKonstantin Belousov if (object != NULL) { 737eb4d6a1bSKonstantin Belousov TAILQ_INSERT_TAIL(NOBJLIST(object->handle), 738eb4d6a1bSKonstantin Belousov object, pager_object_list); 7393364c323SKonstantin Belousov } 74024a1cce3SDavid Greenman } 7410cddd8f0SMatthew Dillon sx_xunlock(&sw_alloc_sx); 74224a1cce3SDavid Greenman } else { 7434b8365d7SKonstantin Belousov object = swap_pager_alloc_init(OBJT_SWAP, handle, cred, 7444b8365d7SKonstantin Belousov size, offset); 74524a1cce3SDavid Greenman } 74624a1cce3SDavid Greenman return (object); 747df8bae1dSRodney W. Grimes } 748df8bae1dSRodney W. Grimes 74926f9a767SRodney W. Grimes /* 7501c7c3c6aSMatthew Dillon * SWAP_PAGER_DEALLOC() - remove swap metadata from object 7511c7c3c6aSMatthew Dillon * 7521c7c3c6aSMatthew Dillon * The swap backing for the object is destroyed. The code is 7531c7c3c6aSMatthew Dillon * designed such that we can reinstantiate it later, but this 7541c7c3c6aSMatthew Dillon * routine is typically called only when the entire object is 7551c7c3c6aSMatthew Dillon * about to be destroyed. 7561c7c3c6aSMatthew Dillon * 75715523cf7SKonstantin Belousov * The object must be locked. 75826f9a767SRodney W. Grimes */ 759df8bae1dSRodney W. Grimes static void 7602f249180SPoul-Henning Kamp swap_pager_dealloc(vm_object_t object) 76126f9a767SRodney W. Grimes { 7624dcc5c2dSMatthew Dillon 763eb4d6a1bSKonstantin Belousov VM_OBJECT_ASSERT_WLOCKED(object); 764eb4d6a1bSKonstantin Belousov KASSERT((object->flags & OBJ_DEAD) != 0, ("dealloc of reachable obj")); 765eb4d6a1bSKonstantin Belousov 76626f9a767SRodney W. Grimes /* 7671c7c3c6aSMatthew Dillon * Remove from list right away so lookups will fail if we block for 7681c7c3c6aSMatthew Dillon * pageout completion. 76926f9a767SRodney W. Grimes */ 77067388836SKonstantin Belousov if ((object->flags & OBJ_ANON) == 0 && object->handle != NULL) { 771eb4d6a1bSKonstantin Belousov VM_OBJECT_WUNLOCK(object); 772eb4d6a1bSKonstantin Belousov sx_xlock(&sw_alloc_sx); 773eb4d6a1bSKonstantin Belousov TAILQ_REMOVE(NOBJLIST(object->handle), object, 774eb4d6a1bSKonstantin Belousov pager_object_list); 775eb4d6a1bSKonstantin Belousov sx_xunlock(&sw_alloc_sx); 776eb4d6a1bSKonstantin Belousov VM_OBJECT_WLOCK(object); 777bd228075SAlan Cox } 7781c7c3c6aSMatthew Dillon 7791c7c3c6aSMatthew Dillon vm_object_pip_wait(object, "swpdea"); 7801c7c3c6aSMatthew Dillon 7811c7c3c6aSMatthew Dillon /* 7821c7c3c6aSMatthew Dillon * Free all remaining metadata. We only bother to free it from 7831c7c3c6aSMatthew Dillon * the swap meta data. We do not attempt to free swapblk's still 7841c7c3c6aSMatthew Dillon * associated with vm_page_t's for this object. We do not care 7851c7c3c6aSMatthew Dillon * if paging is still in progress on some objects. 7861c7c3c6aSMatthew Dillon */ 7871c7c3c6aSMatthew Dillon swp_pager_meta_free_all(object); 788e735691bSJohn Baldwin object->handle = NULL; 789e735691bSJohn Baldwin object->type = OBJT_DEAD; 7904b8365d7SKonstantin Belousov vm_object_clear_flag(object, OBJ_SWAP); 7911c7c3c6aSMatthew Dillon } 7921c7c3c6aSMatthew Dillon 7931c7c3c6aSMatthew Dillon /************************************************************************ 7941c7c3c6aSMatthew Dillon * SWAP PAGER BITMAP ROUTINES * 7951c7c3c6aSMatthew Dillon ************************************************************************/ 7961c7c3c6aSMatthew Dillon 7971c7c3c6aSMatthew Dillon /* 7981c7c3c6aSMatthew Dillon * SWP_PAGER_GETSWAPSPACE() - allocate raw swap space 7991c7c3c6aSMatthew Dillon * 80036b01270SDoug Moore * Allocate swap for up to the requested number of pages. The 80136b01270SDoug Moore * starting swap block number (a page index) is returned or 80236b01270SDoug Moore * SWAPBLK_NONE if the allocation failed. 8031c7c3c6aSMatthew Dillon * 8041c7c3c6aSMatthew Dillon * Also has the side effect of advising that somebody made a mistake 8051c7c3c6aSMatthew Dillon * when they configured swap and didn't configure enough. 8061c7c3c6aSMatthew Dillon * 80715523cf7SKonstantin Belousov * This routine may not sleep. 8088f60c087SPoul-Henning Kamp * 8098f60c087SPoul-Henning Kamp * We allocate in round-robin fashion from the configured devices. 8101c7c3c6aSMatthew Dillon */ 811a5edd34aSPoul-Henning Kamp static daddr_t 81236b01270SDoug Moore swp_pager_getswapspace(int *io_npages) 8131c7c3c6aSMatthew Dillon { 8141c7c3c6aSMatthew Dillon daddr_t blk; 8158f60c087SPoul-Henning Kamp struct swdevt *sp; 81687ae0686SDoug Moore int mpages, npages; 8171c7c3c6aSMatthew Dillon 81836b01270SDoug Moore KASSERT(*io_npages >= 1, 81936b01270SDoug Moore ("%s: npages not positive", __func__)); 8208f60c087SPoul-Henning Kamp blk = SWAPBLK_NONE; 82131c82722SDoug Moore mpages = *io_npages; 82231c82722SDoug Moore npages = imin(BLIST_MAX_ALLOC, mpages); 82320da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 8248f60c087SPoul-Henning Kamp sp = swdevhd; 82548e98a2aSDoug Moore while (!TAILQ_EMPTY(&swtailq)) { 8268f60c087SPoul-Henning Kamp if (sp == NULL) 8278f60c087SPoul-Henning Kamp sp = TAILQ_FIRST(&swtailq); 82848e98a2aSDoug Moore if ((sp->sw_flags & SW_CLOSING) == 0) 82987ae0686SDoug Moore blk = blist_alloc(sp->sw_blist, &npages, mpages); 83048e98a2aSDoug Moore if (blk != SWAPBLK_NONE) 83148e98a2aSDoug Moore break; 83248e98a2aSDoug Moore sp = TAILQ_NEXT(sp, sw_list); 83348e98a2aSDoug Moore if (swdevhd == sp) { 83436b01270SDoug Moore if (npages == 1) 83548e98a2aSDoug Moore break; 83687ae0686SDoug Moore mpages = npages - 1; 83748e98a2aSDoug Moore npages >>= 1; 83848e98a2aSDoug Moore } 83948e98a2aSDoug Moore } 8408f60c087SPoul-Henning Kamp if (blk != SWAPBLK_NONE) { 84148e98a2aSDoug Moore *io_npages = npages; 8428f60c087SPoul-Henning Kamp blk += sp->sw_first; 8438f60c087SPoul-Henning Kamp sp->sw_used += npages; 844d05bc129SAlan Cox swap_pager_avail -= npages; 8458f60c087SPoul-Henning Kamp swp_sizecheck(); 8468f60c087SPoul-Henning Kamp swdevhd = TAILQ_NEXT(sp, sw_list); 84748e98a2aSDoug Moore } else { 8482b0d37a4SMatthew Dillon if (swap_pager_full != 2) { 84948e98a2aSDoug Moore printf("swp_pager_getswapspace(%d): failed\n", 85048e98a2aSDoug Moore *io_npages); 8512b0d37a4SMatthew Dillon swap_pager_full = 2; 85220d3034fSMatthew Dillon swap_pager_almost_full = 1; 8532b0d37a4SMatthew Dillon } 8548f60c087SPoul-Henning Kamp swdevhd = NULL; 85548e98a2aSDoug Moore } 856d05bc129SAlan Cox mtx_unlock(&sw_dev_mtx); 8571c7c3c6aSMatthew Dillon return (blk); 85826f9a767SRodney W. Grimes } 85926f9a767SRodney W. Grimes 860541a1175SAlan Cox static bool 861b3fed13eSDavid Schultz swp_pager_isondev(daddr_t blk, struct swdevt *sp) 8628f60c087SPoul-Henning Kamp { 8638f60c087SPoul-Henning Kamp 864b3fed13eSDavid Schultz return (blk >= sp->sw_first && blk < sp->sw_end); 8658f60c087SPoul-Henning Kamp } 8668f60c087SPoul-Henning Kamp 8674b03903aSPoul-Henning Kamp static void 8684b03903aSPoul-Henning Kamp swp_pager_strategy(struct buf *bp) 8694b03903aSPoul-Henning Kamp { 8704b03903aSPoul-Henning Kamp struct swdevt *sp; 8714b03903aSPoul-Henning Kamp 87220da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 8734b03903aSPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 874541a1175SAlan Cox if (swp_pager_isondev(bp->b_blkno, sp)) { 87520da9c2eSPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 8762cc718a1SKonstantin Belousov if ((sp->sw_flags & SW_UNMAPPED) != 0 && 8772cc718a1SKonstantin Belousov unmapped_buf_allowed) { 8782cc718a1SKonstantin Belousov bp->b_data = unmapped_buf; 8792cc718a1SKonstantin Belousov bp->b_offset = 0; 8802cc718a1SKonstantin Belousov } else { 8812cc718a1SKonstantin Belousov pmap_qenter((vm_offset_t)bp->b_data, 8822cc718a1SKonstantin Belousov &bp->b_pages[0], bp->b_bcount / PAGE_SIZE); 8832cc718a1SKonstantin Belousov } 8844b03903aSPoul-Henning Kamp sp->sw_strategy(bp, sp); 8854b03903aSPoul-Henning Kamp return; 8864b03903aSPoul-Henning Kamp } 8874b03903aSPoul-Henning Kamp } 88820da9c2eSPoul-Henning Kamp panic("Swapdev not found"); 8894b03903aSPoul-Henning Kamp } 8904b03903aSPoul-Henning Kamp 89126f9a767SRodney W. Grimes /* 8921c7c3c6aSMatthew Dillon * SWP_PAGER_FREESWAPSPACE() - free raw swap space 8931c7c3c6aSMatthew Dillon * 8941c7c3c6aSMatthew Dillon * This routine returns the specified swap blocks back to the bitmap. 8951c7c3c6aSMatthew Dillon * 89615523cf7SKonstantin Belousov * This routine may not sleep. 89726f9a767SRodney W. Grimes */ 898a5edd34aSPoul-Henning Kamp static void 899230869e0SAlan Cox swp_pager_freeswapspace(daddr_t blk, daddr_t npages) 9000d94caffSDavid Greenman { 9018f60c087SPoul-Henning Kamp struct swdevt *sp; 90292da00bbSMatthew Dillon 903230869e0SAlan Cox if (npages == 0) 904230869e0SAlan Cox return; 9057645e885SAlan Cox mtx_lock(&sw_dev_mtx); 9067645e885SAlan Cox TAILQ_FOREACH(sp, &swtailq, sw_list) { 907541a1175SAlan Cox if (swp_pager_isondev(blk, sp)) { 90892da00bbSMatthew Dillon sp->sw_used -= npages; 90992da00bbSMatthew Dillon /* 9107645e885SAlan Cox * If we are attempting to stop swapping on 9117645e885SAlan Cox * this device, we don't want to mark any 9127645e885SAlan Cox * blocks free lest they be reused. 91392da00bbSMatthew Dillon */ 9147645e885SAlan Cox if ((sp->sw_flags & SW_CLOSING) == 0) { 9157645e885SAlan Cox blist_free(sp->sw_blist, blk - sp->sw_first, 9167645e885SAlan Cox npages); 9178f60c087SPoul-Henning Kamp swap_pager_avail += npages; 9181c7c3c6aSMatthew Dillon swp_sizecheck(); 91926f9a767SRodney W. Grimes } 9207645e885SAlan Cox mtx_unlock(&sw_dev_mtx); 9217645e885SAlan Cox return; 9227645e885SAlan Cox } 9237645e885SAlan Cox } 9247645e885SAlan Cox panic("Swapdev not found"); 9257645e885SAlan Cox } 9261c7c3c6aSMatthew Dillon 92726f9a767SRodney W. Grimes /* 928d027ed2eSAlan Cox * SYSCTL_SWAP_FRAGMENTATION() - produce raw swap space stats 929d027ed2eSAlan Cox */ 930d027ed2eSAlan Cox static int 931d027ed2eSAlan Cox sysctl_swap_fragmentation(SYSCTL_HANDLER_ARGS) 932d027ed2eSAlan Cox { 933d027ed2eSAlan Cox struct sbuf sbuf; 934d027ed2eSAlan Cox struct swdevt *sp; 935d027ed2eSAlan Cox const char *devname; 936d027ed2eSAlan Cox int error; 937d027ed2eSAlan Cox 938d027ed2eSAlan Cox error = sysctl_wire_old_buffer(req, 0); 939d027ed2eSAlan Cox if (error != 0) 940d027ed2eSAlan Cox return (error); 941d027ed2eSAlan Cox sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 942d027ed2eSAlan Cox mtx_lock(&sw_dev_mtx); 943d027ed2eSAlan Cox TAILQ_FOREACH(sp, &swtailq, sw_list) { 9447ad2a82dSMateusz Guzik if (vn_isdisk(sp->sw_vp)) 945d027ed2eSAlan Cox devname = devtoname(sp->sw_vp->v_rdev); 946d027ed2eSAlan Cox else 947d027ed2eSAlan Cox devname = "[file]"; 948d027ed2eSAlan Cox sbuf_printf(&sbuf, "\nFree space on device %s:\n", devname); 949d027ed2eSAlan Cox blist_stats(sp->sw_blist, &sbuf); 950d027ed2eSAlan Cox } 951d027ed2eSAlan Cox mtx_unlock(&sw_dev_mtx); 952d027ed2eSAlan Cox error = sbuf_finish(&sbuf); 953d027ed2eSAlan Cox sbuf_delete(&sbuf); 954d027ed2eSAlan Cox return (error); 955d027ed2eSAlan Cox } 956d027ed2eSAlan Cox 957d027ed2eSAlan Cox /* 9581c7c3c6aSMatthew Dillon * SWAP_PAGER_FREESPACE() - frees swap blocks associated with a page 9591c7c3c6aSMatthew Dillon * range within an object. 9601c7c3c6aSMatthew Dillon * 9611c7c3c6aSMatthew Dillon * This routine removes swapblk assignments from swap metadata. 9621c7c3c6aSMatthew Dillon * 9631c7c3c6aSMatthew Dillon * The external callers of this routine typically have already destroyed 9641c7c3c6aSMatthew Dillon * or renamed vm_page_t's associated with this range in the object so 9651c7c3c6aSMatthew Dillon * we should be ok. 966c25673ffSAttilio Rao * 967c25673ffSAttilio Rao * The object must be locked. 96826f9a767SRodney W. Grimes */ 9691390a5cbSKonstantin Belousov static void 9702f249180SPoul-Henning Kamp swap_pager_freespace(vm_object_t object, vm_pindex_t start, vm_size_t size) 97126f9a767SRodney W. Grimes { 97223955314SAlfred Perlstein 9731c7c3c6aSMatthew Dillon swp_pager_meta_free(object, start, size); 9744dcc5c2dSMatthew Dillon } 9754dcc5c2dSMatthew Dillon 9764dcc5c2dSMatthew Dillon /* 9774dcc5c2dSMatthew Dillon * SWAP_PAGER_RESERVE() - reserve swap blocks in object 9784dcc5c2dSMatthew Dillon * 9794dcc5c2dSMatthew Dillon * Assigns swap blocks to the specified range within the object. The 98056ce850bSKonstantin Belousov * swap blocks are not zeroed. Any previous swap assignment is destroyed. 9814dcc5c2dSMatthew Dillon * 9824dcc5c2dSMatthew Dillon * Returns 0 on success, -1 on failure. 9834dcc5c2dSMatthew Dillon */ 9844dcc5c2dSMatthew Dillon int 985686aa928SMark Johnston swap_pager_reserve(vm_object_t object, vm_pindex_t start, vm_pindex_t size) 9864dcc5c2dSMatthew Dillon { 98748e98a2aSDoug Moore daddr_t addr, blk, n_free, s_free; 988686aa928SMark Johnston vm_pindex_t i, j; 989686aa928SMark Johnston int n; 9904dcc5c2dSMatthew Dillon 99178f1deefSAlan Cox swp_pager_init_freerange(&s_free, &n_free); 99289f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 99348e98a2aSDoug Moore for (i = 0; i < size; i += n) { 994686aa928SMark Johnston n = MIN(size - i, INT_MAX); 99536b01270SDoug Moore blk = swp_pager_getswapspace(&n); 99648e98a2aSDoug Moore if (blk == SWAPBLK_NONE) { 99748e98a2aSDoug Moore swp_pager_meta_free(object, start, i); 99889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 9994dcc5c2dSMatthew Dillon return (-1); 10004dcc5c2dSMatthew Dillon } 100148e98a2aSDoug Moore for (j = 0; j < n; ++j) { 100248e98a2aSDoug Moore addr = swp_pager_meta_build(object, 100348e98a2aSDoug Moore start + i + j, blk + j); 100478f1deefSAlan Cox if (addr != SWAPBLK_NONE) 100548e98a2aSDoug Moore swp_pager_update_freerange(&s_free, &n_free, 100648e98a2aSDoug Moore addr); 100748e98a2aSDoug Moore } 10084dcc5c2dSMatthew Dillon } 100978f1deefSAlan Cox swp_pager_freeswapspace(s_free, n_free); 101089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 10114dcc5c2dSMatthew Dillon return (0); 101226f9a767SRodney W. Grimes } 101326f9a767SRodney W. Grimes 1014467057fcSDoug Moore static bool 1015467057fcSDoug Moore swp_pager_xfer_source(vm_object_t srcobject, vm_object_t dstobject, 1016467057fcSDoug Moore vm_pindex_t pindex, daddr_t addr) 1017467057fcSDoug Moore { 1018467057fcSDoug Moore daddr_t dstaddr; 1019467057fcSDoug Moore 10204b8365d7SKonstantin Belousov KASSERT((srcobject->flags & OBJ_SWAP) != 0, 10218ecbf14bSDoug Moore ("%s: Srcobject not swappable", __func__)); 10224b8365d7SKonstantin Belousov if ((dstobject->flags & OBJ_SWAP) != 0 && 10238ecbf14bSDoug Moore swp_pager_meta_lookup(dstobject, pindex) != SWAPBLK_NONE) { 1024467057fcSDoug Moore /* Caller should destroy the source block. */ 1025467057fcSDoug Moore return (false); 1026467057fcSDoug Moore } 1027467057fcSDoug Moore 1028467057fcSDoug Moore /* 1029467057fcSDoug Moore * Destination has no swapblk and is not resident, transfer source. 1030467057fcSDoug Moore * swp_pager_meta_build() can sleep. 1031467057fcSDoug Moore */ 1032467057fcSDoug Moore VM_OBJECT_WUNLOCK(srcobject); 1033467057fcSDoug Moore dstaddr = swp_pager_meta_build(dstobject, pindex, addr); 1034467057fcSDoug Moore KASSERT(dstaddr == SWAPBLK_NONE, 1035467057fcSDoug Moore ("Unexpected destination swapblk")); 1036467057fcSDoug Moore VM_OBJECT_WLOCK(srcobject); 103798087a06SJeff Roberson 1038467057fcSDoug Moore return (true); 1039467057fcSDoug Moore } 1040467057fcSDoug Moore 10410a47b48bSJohn Dyson /* 10421c7c3c6aSMatthew Dillon * SWAP_PAGER_COPY() - copy blocks from source pager to destination pager 10431c7c3c6aSMatthew Dillon * and destroy the source. 10441c7c3c6aSMatthew Dillon * 10451c7c3c6aSMatthew Dillon * Copy any valid swapblks from the source to the destination. In 10461c7c3c6aSMatthew Dillon * cases where both the source and destination have a valid swapblk, 10471c7c3c6aSMatthew Dillon * we keep the destination's. 10481c7c3c6aSMatthew Dillon * 104915523cf7SKonstantin Belousov * This routine is allowed to sleep. It may sleep allocating metadata 105098087a06SJeff Roberson * indirectly through swp_pager_meta_build(). 10511c7c3c6aSMatthew Dillon * 10521c7c3c6aSMatthew Dillon * The source object contains no vm_page_t's (which is just as well) 10531c7c3c6aSMatthew Dillon * 10541c7c3c6aSMatthew Dillon * The source object is of type OBJT_SWAP. 10551c7c3c6aSMatthew Dillon * 105615523cf7SKonstantin Belousov * The source and destination objects must be locked. 105715523cf7SKonstantin Belousov * Both object locks may temporarily be released. 105826f9a767SRodney W. Grimes */ 105926f9a767SRodney W. Grimes void 10602f249180SPoul-Henning Kamp swap_pager_copy(vm_object_t srcobject, vm_object_t dstobject, 10612f249180SPoul-Henning Kamp vm_pindex_t offset, int destroysource) 106226f9a767SRodney W. Grimes { 10634dcc5c2dSMatthew Dillon 106489f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(srcobject); 106589f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(dstobject); 10660cddd8f0SMatthew Dillon 106726f9a767SRodney W. Grimes /* 10681c7c3c6aSMatthew Dillon * If destroysource is set, we remove the source object from the 10691c7c3c6aSMatthew Dillon * swap_pager internal queue now. 107026f9a767SRodney W. Grimes */ 107167388836SKonstantin Belousov if (destroysource && (srcobject->flags & OBJ_ANON) == 0 && 107267388836SKonstantin Belousov srcobject->handle != NULL) { 1073eb4d6a1bSKonstantin Belousov VM_OBJECT_WUNLOCK(srcobject); 1074eb4d6a1bSKonstantin Belousov VM_OBJECT_WUNLOCK(dstobject); 1075eb4d6a1bSKonstantin Belousov sx_xlock(&sw_alloc_sx); 1076eb4d6a1bSKonstantin Belousov TAILQ_REMOVE(NOBJLIST(srcobject->handle), srcobject, 1077eb4d6a1bSKonstantin Belousov pager_object_list); 1078eb4d6a1bSKonstantin Belousov sx_xunlock(&sw_alloc_sx); 1079eb4d6a1bSKonstantin Belousov VM_OBJECT_WLOCK(dstobject); 1080eb4d6a1bSKonstantin Belousov VM_OBJECT_WLOCK(srcobject); 1081bd228075SAlan Cox } 108226f9a767SRodney W. Grimes 10831c7c3c6aSMatthew Dillon /* 10844abca9bbSAlan Cox * Transfer source to destination. 10851c7c3c6aSMatthew Dillon */ 1086467057fcSDoug Moore swp_pager_meta_transfer(srcobject, dstobject, offset, dstobject->size); 108726f9a767SRodney W. Grimes 108826f9a767SRodney W. Grimes /* 10891c7c3c6aSMatthew Dillon * Free left over swap blocks in source. 10901c7c3c6aSMatthew Dillon * 1091763df3ecSPedro F. Giffuni * We have to revert the type to OBJT_DEFAULT so we do not accidentally 10921c7c3c6aSMatthew Dillon * double-remove the object from the swap queues. 109326f9a767SRodney W. Grimes */ 1094c0877f10SJohn Dyson if (destroysource) { 10951c7c3c6aSMatthew Dillon swp_pager_meta_free_all(srcobject); 10961c7c3c6aSMatthew Dillon /* 10971c7c3c6aSMatthew Dillon * Reverting the type is not necessary, the caller is going 10981c7c3c6aSMatthew Dillon * to destroy srcobject directly, but I'm doing it here 1099956f3135SPhilippe Charnier * for consistency since we've removed the object from its 11001c7c3c6aSMatthew Dillon * queues. 11011c7c3c6aSMatthew Dillon */ 11021c7c3c6aSMatthew Dillon srcobject->type = OBJT_DEFAULT; 11034b8365d7SKonstantin Belousov vm_object_clear_flag(srcobject, OBJ_SWAP); 1104c0877f10SJohn Dyson } 110526f9a767SRodney W. Grimes } 110626f9a767SRodney W. Grimes 1107df8bae1dSRodney W. Grimes /* 11081c7c3c6aSMatthew Dillon * SWAP_PAGER_HASPAGE() - determine if we have good backing store for 11091c7c3c6aSMatthew Dillon * the requested page. 11101c7c3c6aSMatthew Dillon * 11111c7c3c6aSMatthew Dillon * We determine whether good backing store exists for the requested 11121c7c3c6aSMatthew Dillon * page and return TRUE if it does, FALSE if it doesn't. 11131c7c3c6aSMatthew Dillon * 11141c7c3c6aSMatthew Dillon * If TRUE, we also try to determine how much valid, contiguous backing 1115915d1b71SMark Johnston * store exists before and after the requested page. 1116df8bae1dSRodney W. Grimes */ 11175ea4972cSAlan Cox static boolean_t 1118915d1b71SMark Johnston swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, 1119915d1b71SMark Johnston int *after) 112026f9a767SRodney W. Grimes { 1121915d1b71SMark Johnston daddr_t blk, blk0; 1122915d1b71SMark Johnston int i; 112326f9a767SRodney W. Grimes 1124c25673ffSAttilio Rao VM_OBJECT_ASSERT_LOCKED(object); 11254b8365d7SKonstantin Belousov KASSERT((object->flags & OBJ_SWAP) != 0, 11268ecbf14bSDoug Moore ("%s: object not swappable", __func__)); 1127915d1b71SMark Johnston 11281c7c3c6aSMatthew Dillon /* 11291c7c3c6aSMatthew Dillon * do we have good backing store at the requested index ? 11301c7c3c6aSMatthew Dillon */ 11318ecbf14bSDoug Moore blk0 = swp_pager_meta_lookup(object, pindex); 11324dcc5c2dSMatthew Dillon if (blk0 == SWAPBLK_NONE) { 11331c7c3c6aSMatthew Dillon if (before) 113424a1cce3SDavid Greenman *before = 0; 11351c7c3c6aSMatthew Dillon if (after) 113624a1cce3SDavid Greenman *after = 0; 113726f9a767SRodney W. Grimes return (FALSE); 113826f9a767SRodney W. Grimes } 113926f9a767SRodney W. Grimes 114026f9a767SRodney W. Grimes /* 11411c7c3c6aSMatthew Dillon * find backwards-looking contiguous good backing store 1142e47ed70bSJohn Dyson */ 11431c7c3c6aSMatthew Dillon if (before != NULL) { 1144915d1b71SMark Johnston for (i = 1; i < SWB_NPAGES; i++) { 11451c7c3c6aSMatthew Dillon if (i > pindex) 11461c7c3c6aSMatthew Dillon break; 11478ecbf14bSDoug Moore blk = swp_pager_meta_lookup(object, pindex - i); 11481c7c3c6aSMatthew Dillon if (blk != blk0 - i) 11491c7c3c6aSMatthew Dillon break; 1150ffc82b0aSJohn Dyson } 1151915d1b71SMark Johnston *before = i - 1; 115226f9a767SRodney W. Grimes } 115326f9a767SRodney W. Grimes 115426f9a767SRodney W. Grimes /* 11551c7c3c6aSMatthew Dillon * find forward-looking contiguous good backing store 115626f9a767SRodney W. Grimes */ 11571c7c3c6aSMatthew Dillon if (after != NULL) { 1158915d1b71SMark Johnston for (i = 1; i < SWB_NPAGES; i++) { 11598ecbf14bSDoug Moore blk = swp_pager_meta_lookup(object, pindex + i); 11601c7c3c6aSMatthew Dillon if (blk != blk0 + i) 11611c7c3c6aSMatthew Dillon break; 116226f9a767SRodney W. Grimes } 1163915d1b71SMark Johnston *after = i - 1; 11641c7c3c6aSMatthew Dillon } 11651c7c3c6aSMatthew Dillon return (TRUE); 11661c7c3c6aSMatthew Dillon } 11671c7c3c6aSMatthew Dillon 11681c7c3c6aSMatthew Dillon /* 11691c7c3c6aSMatthew Dillon * SWAP_PAGER_PAGE_UNSWAPPED() - remove swap backing store related to page 11701c7c3c6aSMatthew Dillon * 11711c7c3c6aSMatthew Dillon * This removes any associated swap backing store, whether valid or 11721c7c3c6aSMatthew Dillon * not, from the page. 11731c7c3c6aSMatthew Dillon * 11741c7c3c6aSMatthew Dillon * This routine is typically called when a page is made dirty, at 11751c7c3c6aSMatthew Dillon * which point any associated swap can be freed. MADV_FREE also 11761c7c3c6aSMatthew Dillon * calls us in a special-case situation 11771c7c3c6aSMatthew Dillon * 11781c7c3c6aSMatthew Dillon * NOTE!!! If the page is clean and the swap was valid, the caller 11791c7c3c6aSMatthew Dillon * should make the page dirty before calling this routine. This routine 11801c7c3c6aSMatthew Dillon * does NOT change the m->dirty status of the page. Also: MADV_FREE 11811c7c3c6aSMatthew Dillon * depends on it. 11821c7c3c6aSMatthew Dillon * 118315523cf7SKonstantin Belousov * This routine may not sleep. 1184c25673ffSAttilio Rao * 1185a8081778SJeff Roberson * The object containing the page may be locked. 11861c7c3c6aSMatthew Dillon */ 11871c7c3c6aSMatthew Dillon static void 11882f249180SPoul-Henning Kamp swap_pager_unswapped(vm_page_t m) 11891c7c3c6aSMatthew Dillon { 11908ecbf14bSDoug Moore struct swblk *sb; 1191a8081778SJeff Roberson vm_object_t obj; 11922f249180SPoul-Henning Kamp 1193a8081778SJeff Roberson /* 1194a8081778SJeff Roberson * Handle enqueing deferred frees first. If we do not have the 1195a8081778SJeff Roberson * object lock we wait for the page daemon to clear the space. 1196a8081778SJeff Roberson */ 1197a8081778SJeff Roberson obj = m->object; 1198a8081778SJeff Roberson if (!VM_OBJECT_WOWNED(obj)) { 1199a8081778SJeff Roberson VM_PAGE_OBJECT_BUSY_ASSERT(m); 1200a8081778SJeff Roberson /* 1201a8081778SJeff Roberson * The caller is responsible for synchronization but we 1202a8081778SJeff Roberson * will harmlessly handle races. This is typically provided 1203a8081778SJeff Roberson * by only calling unswapped() when a page transitions from 1204a8081778SJeff Roberson * clean to dirty. 1205a8081778SJeff Roberson */ 1206a8081778SJeff Roberson if ((m->a.flags & (PGA_SWAP_SPACE | PGA_SWAP_FREE)) == 1207a8081778SJeff Roberson PGA_SWAP_SPACE) { 1208a8081778SJeff Roberson vm_page_aflag_set(m, PGA_SWAP_FREE); 1209a8081778SJeff Roberson counter_u64_add(swap_free_deferred, 1); 1210a8081778SJeff Roberson } 1211a8081778SJeff Roberson return; 1212a8081778SJeff Roberson } 1213a8081778SJeff Roberson if ((m->a.flags & PGA_SWAP_FREE) != 0) 1214a8081778SJeff Roberson counter_u64_add(swap_free_completed, 1); 1215a8081778SJeff Roberson vm_page_aflag_clear(m, PGA_SWAP_FREE | PGA_SWAP_SPACE); 12168ecbf14bSDoug Moore 12178ecbf14bSDoug Moore /* 12188ecbf14bSDoug Moore * The meta data only exists if the object is OBJT_SWAP 12198ecbf14bSDoug Moore * and even then might not be allocated yet. 12208ecbf14bSDoug Moore */ 12214b8365d7SKonstantin Belousov KASSERT((m->object->flags & OBJ_SWAP) != 0, 12228ecbf14bSDoug Moore ("Free object not swappable")); 12238ecbf14bSDoug Moore 12248ecbf14bSDoug Moore sb = SWAP_PCTRIE_LOOKUP(&m->object->un_pager.swp.swp_blks, 12258ecbf14bSDoug Moore rounddown(m->pindex, SWAP_META_PAGES)); 12268ecbf14bSDoug Moore if (sb == NULL) 12278ecbf14bSDoug Moore return; 12288ecbf14bSDoug Moore if (sb->d[m->pindex % SWAP_META_PAGES] == SWAPBLK_NONE) 12298ecbf14bSDoug Moore return; 12308ecbf14bSDoug Moore swp_pager_freeswapspace(sb->d[m->pindex % SWAP_META_PAGES], 1); 12318ecbf14bSDoug Moore sb->d[m->pindex % SWAP_META_PAGES] = SWAPBLK_NONE; 12328ecbf14bSDoug Moore swp_pager_free_empty_swblk(m->object, sb); 12331c7c3c6aSMatthew Dillon } 12341c7c3c6aSMatthew Dillon 12351c7c3c6aSMatthew Dillon /* 1236915d1b71SMark Johnston * swap_pager_getpages() - bring pages in from swap 12371c7c3c6aSMatthew Dillon * 12383f060b60SMark Johnston * Attempt to page in the pages in array "ma" of length "count". The 12393f060b60SMark Johnston * caller may optionally specify that additional pages preceding and 12403f060b60SMark Johnston * succeeding the specified range be paged in. The number of such pages 12413f060b60SMark Johnston * is returned in the "rbehind" and "rahead" parameters, and they will 12423f060b60SMark Johnston * be in the inactive queue upon return. 12431c7c3c6aSMatthew Dillon * 12443f060b60SMark Johnston * The pages in "ma" must be busied and will remain busied upon return. 12451c7c3c6aSMatthew Dillon */ 1246f708ef1bSPoul-Henning Kamp static int 1247c90d075bSMark Johnston swap_pager_getpages_locked(vm_object_t object, vm_page_t *ma, int count, 1248c90d075bSMark Johnston int *rbehind, int *rahead) 1249df8bae1dSRodney W. Grimes { 12501c7c3c6aSMatthew Dillon struct buf *bp; 1251b7b8a096SKonstantin Belousov vm_page_t bm, mpred, msucc, p; 1252915d1b71SMark Johnston vm_pindex_t pindex; 12531c7c3c6aSMatthew Dillon daddr_t blk; 1254b7b8a096SKonstantin Belousov int i, maxahead, maxbehind, reqcount; 12550d94caffSDavid Greenman 1256c90d075bSMark Johnston VM_OBJECT_ASSERT_WLOCKED(object); 1257915d1b71SMark Johnston reqcount = count; 12581c7c3c6aSMatthew Dillon 12594b8365d7SKonstantin Belousov KASSERT((object->flags & OBJ_SWAP) != 0, 12608ecbf14bSDoug Moore ("%s: object not swappable", __func__)); 1261d6e13f3bSJeff Roberson if (!swap_pager_haspage(object, ma[0]->pindex, &maxbehind, &maxahead)) { 1262d6e13f3bSJeff Roberson VM_OBJECT_WUNLOCK(object); 1263915d1b71SMark Johnston return (VM_PAGER_FAIL); 1264d6e13f3bSJeff Roberson } 1265915d1b71SMark Johnston 126698087a06SJeff Roberson KASSERT(reqcount - 1 <= maxahead, 126798087a06SJeff Roberson ("page count %d extends beyond swap block", reqcount)); 126898087a06SJeff Roberson 126998087a06SJeff Roberson /* 127098087a06SJeff Roberson * Do not transfer any pages other than those that are xbusied 127198087a06SJeff Roberson * when running during a split or collapse operation. This 127298087a06SJeff Roberson * prevents clustering from re-creating pages which are being 127398087a06SJeff Roberson * moved into another object. 127498087a06SJeff Roberson */ 127598087a06SJeff Roberson if ((object->flags & (OBJ_SPLIT | OBJ_DEAD)) != 0) { 127698087a06SJeff Roberson maxahead = reqcount - 1; 127798087a06SJeff Roberson maxbehind = 0; 127898087a06SJeff Roberson } 127998087a06SJeff Roberson 1280915d1b71SMark Johnston /* 1281915d1b71SMark Johnston * Clip the readahead and readbehind ranges to exclude resident pages. 1282915d1b71SMark Johnston */ 1283915d1b71SMark Johnston if (rahead != NULL) { 1284dd9cb6daSMark Johnston *rahead = imin(*rahead, maxahead - (reqcount - 1)); 12853f060b60SMark Johnston pindex = ma[reqcount - 1]->pindex; 12863f060b60SMark Johnston msucc = TAILQ_NEXT(ma[reqcount - 1], listq); 1287915d1b71SMark Johnston if (msucc != NULL && msucc->pindex - pindex - 1 < *rahead) 1288915d1b71SMark Johnston *rahead = msucc->pindex - pindex - 1; 1289915d1b71SMark Johnston } 1290915d1b71SMark Johnston if (rbehind != NULL) { 1291dd9cb6daSMark Johnston *rbehind = imin(*rbehind, maxbehind); 12923f060b60SMark Johnston pindex = ma[0]->pindex; 12933f060b60SMark Johnston mpred = TAILQ_PREV(ma[0], pglist, listq); 1294915d1b71SMark Johnston if (mpred != NULL && pindex - mpred->pindex - 1 < *rbehind) 1295915d1b71SMark Johnston *rbehind = pindex - mpred->pindex - 1; 1296915d1b71SMark Johnston } 1297915d1b71SMark Johnston 1298b7b8a096SKonstantin Belousov bm = ma[0]; 1299b7b8a096SKonstantin Belousov for (i = 0; i < count; i++) 1300b7b8a096SKonstantin Belousov ma[i]->oflags |= VPO_SWAPINPROG; 1301b7b8a096SKonstantin Belousov 1302915d1b71SMark Johnston /* 1303915d1b71SMark Johnston * Allocate readahead and readbehind pages. 1304915d1b71SMark Johnston */ 1305b7b8a096SKonstantin Belousov if (rbehind != NULL) { 1306b7b8a096SKonstantin Belousov for (i = 1; i <= *rbehind; i++) { 13073f060b60SMark Johnston p = vm_page_alloc(object, ma[0]->pindex - i, 13087667839aSAlan Cox VM_ALLOC_NORMAL); 1309b7b8a096SKonstantin Belousov if (p == NULL) 1310915d1b71SMark Johnston break; 1311b7b8a096SKonstantin Belousov p->oflags |= VPO_SWAPINPROG; 1312b7b8a096SKonstantin Belousov bm = p; 1313915d1b71SMark Johnston } 1314b7b8a096SKonstantin Belousov *rbehind = i - 1; 1315915d1b71SMark Johnston } 1316915d1b71SMark Johnston if (rahead != NULL) { 1317915d1b71SMark Johnston for (i = 0; i < *rahead; i++) { 1318915d1b71SMark Johnston p = vm_page_alloc(object, 13193f060b60SMark Johnston ma[reqcount - 1]->pindex + i + 1, VM_ALLOC_NORMAL); 1320915d1b71SMark Johnston if (p == NULL) 1321915d1b71SMark Johnston break; 1322b7b8a096SKonstantin Belousov p->oflags |= VPO_SWAPINPROG; 1323915d1b71SMark Johnston } 1324915d1b71SMark Johnston *rahead = i; 1325915d1b71SMark Johnston } 1326915d1b71SMark Johnston if (rbehind != NULL) 1327915d1b71SMark Johnston count += *rbehind; 1328915d1b71SMark Johnston if (rahead != NULL) 1329915d1b71SMark Johnston count += *rahead; 1330915d1b71SMark Johnston 1331915d1b71SMark Johnston vm_object_pip_add(object, count); 1332915d1b71SMark Johnston 1333b7b8a096SKonstantin Belousov pindex = bm->pindex; 13348ecbf14bSDoug Moore blk = swp_pager_meta_lookup(object, pindex); 1335915d1b71SMark Johnston KASSERT(blk != SWAPBLK_NONE, 1336915d1b71SMark Johnston ("no swap blocking containing %p(%jx)", object, (uintmax_t)pindex)); 1337915d1b71SMark Johnston 1338915d1b71SMark Johnston VM_OBJECT_WUNLOCK(object); 1339756a5412SGleb Smirnoff bp = uma_zalloc(swrbuf_zone, M_WAITOK); 1340cd853791SKonstantin Belousov MPASS((bp->b_flags & B_MAXPHYS) != 0); 1341b7b8a096SKonstantin Belousov /* Pages cannot leave the object while busy. */ 1342b7b8a096SKonstantin Belousov for (i = 0, p = bm; i < count; i++, p = TAILQ_NEXT(p, listq)) { 1343b7b8a096SKonstantin Belousov MPASS(p->pindex == bm->pindex + i); 1344b7b8a096SKonstantin Belousov bp->b_pages[i] = p; 1345b7b8a096SKonstantin Belousov } 1346915d1b71SMark Johnston 1347915d1b71SMark Johnston bp->b_flags |= B_PAGING; 134821144e3bSPoul-Henning Kamp bp->b_iocmd = BIO_READ; 13491c7c3c6aSMatthew Dillon bp->b_iodone = swp_pager_async_iodone; 1350fdcc1cc0SJohn Baldwin bp->b_rcred = crhold(thread0.td_ucred); 1351fdcc1cc0SJohn Baldwin bp->b_wcred = crhold(thread0.td_ucred); 1352b0cd2017SGleb Smirnoff bp->b_blkno = blk; 1353b0cd2017SGleb Smirnoff bp->b_bcount = PAGE_SIZE * count; 1354b0cd2017SGleb Smirnoff bp->b_bufsize = PAGE_SIZE * count; 1355b0cd2017SGleb Smirnoff bp->b_npages = count; 1356915d1b71SMark Johnston bp->b_pgbefore = rbehind != NULL ? *rbehind : 0; 1357915d1b71SMark Johnston bp->b_pgafter = rahead != NULL ? *rahead : 0; 135826f9a767SRodney W. Grimes 135983c9dea1SGleb Smirnoff VM_CNT_INC(v_swapin); 136083c9dea1SGleb Smirnoff VM_CNT_ADD(v_swappgsin, count); 13611c7c3c6aSMatthew Dillon 13621c7c3c6aSMatthew Dillon /* 13631c7c3c6aSMatthew Dillon * perform the I/O. NOTE!!! bp cannot be considered valid after 13641c7c3c6aSMatthew Dillon * this point because we automatically release it on completion. 13651c7c3c6aSMatthew Dillon * Instead, we look at the one page we are interested in which we 13661c7c3c6aSMatthew Dillon * still hold a lock on even through the I/O completion. 13671c7c3c6aSMatthew Dillon * 13683f060b60SMark Johnston * The other pages in our ma[] array are also released on completion, 13691c7c3c6aSMatthew Dillon * so we cannot assume they are valid anymore either. 13701c7c3c6aSMatthew Dillon * 1371c37a77eeSPoul-Henning Kamp * NOTE: b_blkno is destroyed by the call to swapdev_strategy 13721c7c3c6aSMatthew Dillon */ 1373b890cb2cSPeter Wemm BUF_KERNPROC(bp); 13744b03903aSPoul-Henning Kamp swp_pager_strategy(bp); 137526f9a767SRodney W. Grimes 137626f9a767SRodney W. Grimes /* 1377915d1b71SMark Johnston * Wait for the pages we want to complete. VPO_SWAPINPROG is always 13781c7c3c6aSMatthew Dillon * cleared on completion. If an I/O error occurs, SWAPBLK_NONE 1379915d1b71SMark Johnston * is set in the metadata for each page in the request. 138026f9a767SRodney W. Grimes */ 138189f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1382d6e13f3bSJeff Roberson /* This could be implemented more efficiently with aflags */ 13833f060b60SMark Johnston while ((ma[0]->oflags & VPO_SWAPINPROG) != 0) { 13843f060b60SMark Johnston ma[0]->oflags |= VPO_SWAPSLEEP; 138583c9dea1SGleb Smirnoff VM_CNT_INC(v_intrans); 1386cf27e0d1SJeff Roberson if (VM_OBJECT_SLEEP(object, &object->handle, PSWP, 1387c7aebda8SAttilio Rao "swread", hz * 20)) { 13889bd86a98SBruce M Simpson printf( 1389c5690651SPoul-Henning Kamp "swap_pager: indefinite wait buffer: bufobj: %p, blkno: %jd, size: %ld\n", 1390c5690651SPoul-Henning Kamp bp->b_bufobj, (intmax_t)bp->b_blkno, bp->b_bcount); 13911c7c3c6aSMatthew Dillon } 13921b119d9dSDavid Greenman } 1393d6e13f3bSJeff Roberson VM_OBJECT_WUNLOCK(object); 139426f9a767SRodney W. Grimes 139526f9a767SRodney W. Grimes /* 1396b0cd2017SGleb Smirnoff * If we had an unrecoverable read error pages will not be valid. 139726f9a767SRodney W. Grimes */ 1398915d1b71SMark Johnston for (i = 0; i < reqcount; i++) 13993f060b60SMark Johnston if (ma[i]->valid != VM_PAGE_BITS_ALL) 14001c7c3c6aSMatthew Dillon return (VM_PAGER_ERROR); 1401b0cd2017SGleb Smirnoff 14021c7c3c6aSMatthew Dillon return (VM_PAGER_OK); 14031c7c3c6aSMatthew Dillon 14041c7c3c6aSMatthew Dillon /* 14051c7c3c6aSMatthew Dillon * A final note: in a low swap situation, we cannot deallocate swap 14061c7c3c6aSMatthew Dillon * and mark a page dirty here because the caller is likely to mark 14071c7c3c6aSMatthew Dillon * the page clean when we return, causing the page to possibly revert 14081c7c3c6aSMatthew Dillon * to all-zero's later. 14091c7c3c6aSMatthew Dillon */ 1410df8bae1dSRodney W. Grimes } 1411df8bae1dSRodney W. Grimes 1412c90d075bSMark Johnston static int 1413c90d075bSMark Johnston swap_pager_getpages(vm_object_t object, vm_page_t *ma, int count, 1414c90d075bSMark Johnston int *rbehind, int *rahead) 1415c90d075bSMark Johnston { 1416c90d075bSMark Johnston 1417c90d075bSMark Johnston VM_OBJECT_WLOCK(object); 1418c90d075bSMark Johnston return (swap_pager_getpages_locked(object, ma, count, rbehind, rahead)); 1419c90d075bSMark Johnston } 1420c90d075bSMark Johnston 14211c7c3c6aSMatthew Dillon /* 142290effb23SGleb Smirnoff * swap_pager_getpages_async(): 142390effb23SGleb Smirnoff * 142490effb23SGleb Smirnoff * Right now this is emulation of asynchronous operation on top of 142590effb23SGleb Smirnoff * swap_pager_getpages(). 142690effb23SGleb Smirnoff */ 142790effb23SGleb Smirnoff static int 14283f060b60SMark Johnston swap_pager_getpages_async(vm_object_t object, vm_page_t *ma, int count, 1429b0cd2017SGleb Smirnoff int *rbehind, int *rahead, pgo_getpages_iodone_t iodone, void *arg) 143090effb23SGleb Smirnoff { 143190effb23SGleb Smirnoff int r, error; 143290effb23SGleb Smirnoff 14333f060b60SMark Johnston r = swap_pager_getpages(object, ma, count, rbehind, rahead); 143490effb23SGleb Smirnoff switch (r) { 143590effb23SGleb Smirnoff case VM_PAGER_OK: 143690effb23SGleb Smirnoff error = 0; 143790effb23SGleb Smirnoff break; 143890effb23SGleb Smirnoff case VM_PAGER_ERROR: 143990effb23SGleb Smirnoff error = EIO; 144090effb23SGleb Smirnoff break; 144190effb23SGleb Smirnoff case VM_PAGER_FAIL: 144290effb23SGleb Smirnoff error = EINVAL; 144390effb23SGleb Smirnoff break; 144490effb23SGleb Smirnoff default: 1445d9328101SGleb Smirnoff panic("unhandled swap_pager_getpages() error %d", r); 144690effb23SGleb Smirnoff } 14473f060b60SMark Johnston (iodone)(arg, ma, count, error); 144890effb23SGleb Smirnoff 144990effb23SGleb Smirnoff return (r); 145090effb23SGleb Smirnoff } 145190effb23SGleb Smirnoff 145290effb23SGleb Smirnoff /* 14531c7c3c6aSMatthew Dillon * swap_pager_putpages: 14541c7c3c6aSMatthew Dillon * 14551c7c3c6aSMatthew Dillon * Assign swap (if necessary) and initiate I/O on the specified pages. 14561c7c3c6aSMatthew Dillon * 14571c7c3c6aSMatthew Dillon * We support both OBJT_DEFAULT and OBJT_SWAP objects. DEFAULT objects 14581c7c3c6aSMatthew Dillon * are automatically converted to SWAP objects. 14591c7c3c6aSMatthew Dillon * 1460ea3aecf5SPeter Wemm * In a low memory situation we may block in VOP_STRATEGY(), but the new 14611c7c3c6aSMatthew Dillon * vm_page reservation system coupled with properly written VFS devices 14621c7c3c6aSMatthew Dillon * should ensure that no low-memory deadlock occurs. This is an area 14631c7c3c6aSMatthew Dillon * which needs work. 14641c7c3c6aSMatthew Dillon * 14651c7c3c6aSMatthew Dillon * The parent has N vm_object_pip_add() references prior to 14661c7c3c6aSMatthew Dillon * calling us and will remove references for rtvals[] that are 14671c7c3c6aSMatthew Dillon * not set to VM_PAGER_PEND. We need to remove the rest on I/O 14681c7c3c6aSMatthew Dillon * completion. 14691c7c3c6aSMatthew Dillon * 14701c7c3c6aSMatthew Dillon * The parent has soft-busy'd the pages it passes us and will unbusy 147123612f0dSDoug Moore * those whose rtvals[] entry is not set to VM_PAGER_PEND on return. 14721c7c3c6aSMatthew Dillon * We need to unbusy the rest on I/O completion. 14731c7c3c6aSMatthew Dillon */ 1474d635a37fSWarner Losh static void 14753f060b60SMark Johnston swap_pager_putpages(vm_object_t object, vm_page_t *ma, int count, 1476e065e87cSKonstantin Belousov int flags, int *rtvals) 1477df8bae1dSRodney W. Grimes { 147823612f0dSDoug Moore struct buf *bp; 147923612f0dSDoug Moore daddr_t addr, blk, n_free, s_free; 148023612f0dSDoug Moore vm_page_t mreq; 148123612f0dSDoug Moore int i, j, n; 148223612f0dSDoug Moore bool async; 1483df8bae1dSRodney W. Grimes 148423612f0dSDoug Moore KASSERT(count == 0 || ma[0]->object == object, 148523612f0dSDoug Moore ("%s: object mismatch %p/%p", 148623612f0dSDoug Moore __func__, object, ma[0]->object)); 1487ee3dc7d7SAlan Cox 14881c7c3c6aSMatthew Dillon /* 14891c7c3c6aSMatthew Dillon * Step 1 14901c7c3c6aSMatthew Dillon * 149123612f0dSDoug Moore * Turn object into OBJT_SWAP. Force sync if not a pageout process. 14921c7c3c6aSMatthew Dillon */ 14934b8365d7SKonstantin Belousov if ((object->flags & OBJ_SWAP) == 0) { 149478f1deefSAlan Cox addr = swp_pager_meta_build(object, 0, SWAPBLK_NONE); 149578f1deefSAlan Cox KASSERT(addr == SWAPBLK_NONE, 149678f1deefSAlan Cox ("unexpected object swap block")); 149778f1deefSAlan Cox } 149889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 149923612f0dSDoug Moore async = curproc == pageproc && (flags & VM_PAGER_PUT_SYNC) == 0; 150023612f0dSDoug Moore swp_pager_init_freerange(&s_free, &n_free); 150126f9a767SRodney W. Grimes 15021c7c3c6aSMatthew Dillon /* 15031c7c3c6aSMatthew Dillon * Step 2 15041c7c3c6aSMatthew Dillon * 15051c7c3c6aSMatthew Dillon * Assign swap blocks and issue I/O. We reallocate swap on the fly. 15061c7c3c6aSMatthew Dillon * The page is left dirty until the pageout operation completes 15071c7c3c6aSMatthew Dillon * successfully. 15081c7c3c6aSMatthew Dillon */ 15091c7c3c6aSMatthew Dillon for (i = 0; i < count; i += n) { 151031c82722SDoug Moore /* Maximum I/O size is limited by maximum swap block size. */ 151131c82722SDoug Moore n = min(count - i, nsw_cluster_max); 15121c7c3c6aSMatthew Dillon 151323612f0dSDoug Moore if (async) { 1514756a5412SGleb Smirnoff mtx_lock(&swbuf_mtx); 1515756a5412SGleb Smirnoff while (nsw_wcount_async == 0) 1516756a5412SGleb Smirnoff msleep(&nsw_wcount_async, &swbuf_mtx, PVM, 1517756a5412SGleb Smirnoff "swbufa", 0); 1518756a5412SGleb Smirnoff nsw_wcount_async--; 1519756a5412SGleb Smirnoff mtx_unlock(&swbuf_mtx); 1520327f4e83SMatthew Dillon } 1521725b4ff0SMark Johnston 1522725b4ff0SMark Johnston /* Get a block of swap of size up to size n. */ 1523725b4ff0SMark Johnston VM_OBJECT_WLOCK(object); 152436b01270SDoug Moore blk = swp_pager_getswapspace(&n); 1525725b4ff0SMark Johnston if (blk == SWAPBLK_NONE) { 1526725b4ff0SMark Johnston VM_OBJECT_WUNLOCK(object); 1527725b4ff0SMark Johnston mtx_lock(&swbuf_mtx); 1528725b4ff0SMark Johnston if (++nsw_wcount_async == 1) 1529725b4ff0SMark Johnston wakeup(&nsw_wcount_async); 1530725b4ff0SMark Johnston mtx_unlock(&swbuf_mtx); 1531725b4ff0SMark Johnston for (j = 0; j < n; ++j) 1532725b4ff0SMark Johnston rtvals[i + j] = VM_PAGER_FAIL; 1533725b4ff0SMark Johnston continue; 1534725b4ff0SMark Johnston } 1535725b4ff0SMark Johnston for (j = 0; j < n; ++j) { 1536725b4ff0SMark Johnston mreq = ma[i + j]; 1537725b4ff0SMark Johnston vm_page_aflag_clear(mreq, PGA_SWAP_FREE); 1538725b4ff0SMark Johnston addr = swp_pager_meta_build(mreq->object, mreq->pindex, 1539725b4ff0SMark Johnston blk + j); 1540725b4ff0SMark Johnston if (addr != SWAPBLK_NONE) 1541725b4ff0SMark Johnston swp_pager_update_freerange(&s_free, &n_free, 1542725b4ff0SMark Johnston addr); 1543725b4ff0SMark Johnston MPASS(mreq->dirty == VM_PAGE_BITS_ALL); 1544725b4ff0SMark Johnston mreq->oflags |= VPO_SWAPINPROG; 1545725b4ff0SMark Johnston } 1546725b4ff0SMark Johnston VM_OBJECT_WUNLOCK(object); 1547725b4ff0SMark Johnston 1548756a5412SGleb Smirnoff bp = uma_zalloc(swwbuf_zone, M_WAITOK); 1549cd853791SKonstantin Belousov MPASS((bp->b_flags & B_MAXPHYS) != 0); 155023612f0dSDoug Moore if (async) 1551cd853791SKonstantin Belousov bp->b_flags |= B_ASYNC; 15525e04322aSPoul-Henning Kamp bp->b_flags |= B_PAGING; 1553912e4ae9SPoul-Henning Kamp bp->b_iocmd = BIO_WRITE; 155426f9a767SRodney W. Grimes 1555fdcc1cc0SJohn Baldwin bp->b_rcred = crhold(thread0.td_ucred); 1556fdcc1cc0SJohn Baldwin bp->b_wcred = crhold(thread0.td_ucred); 15571c7c3c6aSMatthew Dillon bp->b_bcount = PAGE_SIZE * n; 15581c7c3c6aSMatthew Dillon bp->b_bufsize = PAGE_SIZE * n; 15591c7c3c6aSMatthew Dillon bp->b_blkno = blk; 1560725b4ff0SMark Johnston for (j = 0; j < n; j++) 1561725b4ff0SMark Johnston bp->b_pages[j] = ma[i + j]; 15621c7c3c6aSMatthew Dillon bp->b_npages = n; 1563725b4ff0SMark Johnston 1564a5296b05SJulian Elischer /* 1565a5296b05SJulian Elischer * Must set dirty range for NFS to work. 1566a5296b05SJulian Elischer */ 1567a5296b05SJulian Elischer bp->b_dirtyoff = 0; 1568a5296b05SJulian Elischer bp->b_dirtyend = bp->b_bcount; 15691c7c3c6aSMatthew Dillon 157083c9dea1SGleb Smirnoff VM_CNT_INC(v_swapout); 157183c9dea1SGleb Smirnoff VM_CNT_ADD(v_swappgsout, bp->b_npages); 157226f9a767SRodney W. Grimes 157326f9a767SRodney W. Grimes /* 157477923df2SAlan Cox * We unconditionally set rtvals[] to VM_PAGER_PEND so that we 157577923df2SAlan Cox * can call the async completion routine at the end of a 157677923df2SAlan Cox * synchronous I/O operation. Otherwise, our caller would 157777923df2SAlan Cox * perform duplicate unbusy and wakeup operations on the page 157877923df2SAlan Cox * and object, respectively. 157977923df2SAlan Cox */ 158077923df2SAlan Cox for (j = 0; j < n; j++) 158177923df2SAlan Cox rtvals[i + j] = VM_PAGER_PEND; 158277923df2SAlan Cox 158377923df2SAlan Cox /* 15841c7c3c6aSMatthew Dillon * asynchronous 15851c7c3c6aSMatthew Dillon * 158623612f0dSDoug Moore * NOTE: b_blkno is destroyed by the call to swapdev_strategy. 158726f9a767SRodney W. Grimes */ 158823612f0dSDoug Moore if (async) { 15891c7c3c6aSMatthew Dillon bp->b_iodone = swp_pager_async_iodone; 159067812eacSKirk McKusick BUF_KERNPROC(bp); 15914b03903aSPoul-Henning Kamp swp_pager_strategy(bp); 15921c7c3c6aSMatthew Dillon continue; 159326f9a767SRodney W. Grimes } 1594e47ed70bSJohn Dyson 159526f9a767SRodney W. Grimes /* 15961c7c3c6aSMatthew Dillon * synchronous 15971c7c3c6aSMatthew Dillon * 159823612f0dSDoug Moore * NOTE: b_blkno is destroyed by the call to swapdev_strategy. 15991c7c3c6aSMatthew Dillon */ 16002c840b1fSAlan Cox bp->b_iodone = bdone; 16014b03903aSPoul-Henning Kamp swp_pager_strategy(bp); 16021c7c3c6aSMatthew Dillon 16031c7c3c6aSMatthew Dillon /* 160477923df2SAlan Cox * Wait for the sync I/O to complete. 160526f9a767SRodney W. Grimes */ 16062c840b1fSAlan Cox bwait(bp, PVM, "swwrt"); 160777923df2SAlan Cox 16081c7c3c6aSMatthew Dillon /* 16091c7c3c6aSMatthew Dillon * Now that we are through with the bp, we can call the 16101c7c3c6aSMatthew Dillon * normal async completion, which frees everything up. 16111c7c3c6aSMatthew Dillon */ 16121c7c3c6aSMatthew Dillon swp_pager_async_iodone(bp); 16131c7c3c6aSMatthew Dillon } 161478f1deefSAlan Cox swp_pager_freeswapspace(s_free, n_free); 161523612f0dSDoug Moore VM_OBJECT_WLOCK(object); 16161c7c3c6aSMatthew Dillon } 16171c7c3c6aSMatthew Dillon 16181c7c3c6aSMatthew Dillon /* 16191c7c3c6aSMatthew Dillon * swp_pager_async_iodone: 16201c7c3c6aSMatthew Dillon * 16211c7c3c6aSMatthew Dillon * Completion routine for asynchronous reads and writes from/to swap. 16221c7c3c6aSMatthew Dillon * Also called manually by synchronous code to finish up a bp. 16231c7c3c6aSMatthew Dillon * 162415523cf7SKonstantin Belousov * This routine may not sleep. 16251c7c3c6aSMatthew Dillon */ 16261c7c3c6aSMatthew Dillon static void 16272f249180SPoul-Henning Kamp swp_pager_async_iodone(struct buf *bp) 16281c7c3c6aSMatthew Dillon { 16291c7c3c6aSMatthew Dillon int i; 16301c7c3c6aSMatthew Dillon vm_object_t object = NULL; 16311c7c3c6aSMatthew Dillon 16321c7c3c6aSMatthew Dillon /* 16330b208315SEdward Tomasz Napierala * Report error - unless we ran out of memory, in which case 16340b208315SEdward Tomasz Napierala * we've already logged it in swapgeom_strategy(). 16351c7c3c6aSMatthew Dillon */ 16360b208315SEdward Tomasz Napierala if (bp->b_ioflags & BIO_ERROR && bp->b_error != ENOMEM) { 16371c7c3c6aSMatthew Dillon printf( 16381c7c3c6aSMatthew Dillon "swap_pager: I/O error - %s failed; blkno %ld," 16391c7c3c6aSMatthew Dillon "size %ld, error %d\n", 164021144e3bSPoul-Henning Kamp ((bp->b_iocmd == BIO_READ) ? "pagein" : "pageout"), 16411c7c3c6aSMatthew Dillon (long)bp->b_blkno, 16421c7c3c6aSMatthew Dillon (long)bp->b_bcount, 16431c7c3c6aSMatthew Dillon bp->b_error 16441c7c3c6aSMatthew Dillon ); 16451c7c3c6aSMatthew Dillon } 16461c7c3c6aSMatthew Dillon 16471c7c3c6aSMatthew Dillon /* 164826f9a767SRodney W. Grimes * remove the mapping for kernel virtual 164926f9a767SRodney W. Grimes */ 1650fade8dd7SJeff Roberson if (buf_mapped(bp)) 16511c7c3c6aSMatthew Dillon pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages); 1652fade8dd7SJeff Roberson else 1653fade8dd7SJeff Roberson bp->b_data = bp->b_kvabase; 165426f9a767SRodney W. Grimes 165533a609ecSAlan Cox if (bp->b_npages) { 165633a609ecSAlan Cox object = bp->b_pages[0]->object; 165789f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 165833a609ecSAlan Cox } 16592965a453SKip Macy 166026f9a767SRodney W. Grimes /* 16611c7c3c6aSMatthew Dillon * cleanup pages. If an error occurs writing to swap, we are in 16621c7c3c6aSMatthew Dillon * very serious trouble. If it happens to be a disk error, though, 16631c7c3c6aSMatthew Dillon * we may be able to recover by reassigning the swap later on. So 16641c7c3c6aSMatthew Dillon * in this case we remove the m->swapblk assignment for the page 16651c7c3c6aSMatthew Dillon * but do not free it in the rlist. The errornous block(s) are thus 16661c7c3c6aSMatthew Dillon * never reallocated as swap. Redirty the page and continue. 166726f9a767SRodney W. Grimes */ 16681c7c3c6aSMatthew Dillon for (i = 0; i < bp->b_npages; ++i) { 16691c7c3c6aSMatthew Dillon vm_page_t m = bp->b_pages[i]; 1670e47ed70bSJohn Dyson 16715786be7cSAlan Cox m->oflags &= ~VPO_SWAPINPROG; 1672c7aebda8SAttilio Rao if (m->oflags & VPO_SWAPSLEEP) { 1673c7aebda8SAttilio Rao m->oflags &= ~VPO_SWAPSLEEP; 1674cf27e0d1SJeff Roberson wakeup(&object->handle); 1675c7aebda8SAttilio Rao } 1676e47ed70bSJohn Dyson 1677a8081778SJeff Roberson /* We always have space after I/O, successful or not. */ 1678a8081778SJeff Roberson vm_page_aflag_set(m, PGA_SWAP_SPACE); 1679a8081778SJeff Roberson 1680c244d2deSPoul-Henning Kamp if (bp->b_ioflags & BIO_ERROR) { 1681ffc82b0aSJohn Dyson /* 16821c7c3c6aSMatthew Dillon * If an error occurs I'd love to throw the swapblk 16831c7c3c6aSMatthew Dillon * away without freeing it back to swapspace, so it 16841c7c3c6aSMatthew Dillon * can never be used again. But I can't from an 16851c7c3c6aSMatthew Dillon * interrupt. 1686ffc82b0aSJohn Dyson */ 168721144e3bSPoul-Henning Kamp if (bp->b_iocmd == BIO_READ) { 16881c7c3c6aSMatthew Dillon /* 16891c7c3c6aSMatthew Dillon * NOTE: for reads, m->dirty will probably 1690956f3135SPhilippe Charnier * be overridden by the original caller of 16911c7c3c6aSMatthew Dillon * getpages so don't play cute tricks here. 16921c7c3c6aSMatthew Dillon */ 16930012f373SJeff Roberson vm_page_invalid(m); 16941c7c3c6aSMatthew Dillon } else { 16951c7c3c6aSMatthew Dillon /* 16961c7c3c6aSMatthew Dillon * If a write error occurs, reactivate page 16971c7c3c6aSMatthew Dillon * so it doesn't clog the inactive list, 16981c7c3c6aSMatthew Dillon * then finish the I/O. 16991c7c3c6aSMatthew Dillon */ 170041e5a226SAlan Cox MPASS(m->dirty == VM_PAGE_BITS_ALL); 17019f5632e6SMark Johnston 1702a8081778SJeff Roberson /* PQ_UNSWAPPABLE? */ 17031c7c3c6aSMatthew Dillon vm_page_activate(m); 1704c7aebda8SAttilio Rao vm_page_sunbusy(m); 17051c7c3c6aSMatthew Dillon } 170621144e3bSPoul-Henning Kamp } else if (bp->b_iocmd == BIO_READ) { 17071c7c3c6aSMatthew Dillon /* 17081c7c3c6aSMatthew Dillon * NOTE: for reads, m->dirty will probably be 1709956f3135SPhilippe Charnier * overridden by the original caller of getpages so 17101c7c3c6aSMatthew Dillon * we cannot set them in order to free the underlying 17111c7c3c6aSMatthew Dillon * swap in a low-swap situation. I don't think we'd 17121c7c3c6aSMatthew Dillon * want to do that anyway, but it was an optimization 17131c7c3c6aSMatthew Dillon * that existed in the old swapper for a time before 17141c7c3c6aSMatthew Dillon * it got ripped out due to precisely this problem. 17151c7c3c6aSMatthew Dillon */ 1716016a3c93SAlan Cox KASSERT(!pmap_page_is_mapped(m), 1717016a3c93SAlan Cox ("swp_pager_async_iodone: page %p is mapped", m)); 1718016a3c93SAlan Cox KASSERT(m->dirty == 0, 1719016a3c93SAlan Cox ("swp_pager_async_iodone: page %p is dirty", m)); 1720915d1b71SMark Johnston 17210012f373SJeff Roberson vm_page_valid(m); 1722915d1b71SMark Johnston if (i < bp->b_pgbefore || 1723915d1b71SMark Johnston i >= bp->b_npages - bp->b_pgafter) 1724915d1b71SMark Johnston vm_page_readahead_finish(m); 17251c7c3c6aSMatthew Dillon } else { 17261c7c3c6aSMatthew Dillon /* 1727016a3c93SAlan Cox * For write success, clear the dirty 17281c7c3c6aSMatthew Dillon * status, then finish the I/O ( which decrements the 17291c7c3c6aSMatthew Dillon * busy count and possibly wakes waiter's up ). 1730ebcddc72SAlan Cox * A page is only written to swap after a period of 1731ebcddc72SAlan Cox * inactivity. Therefore, we do not expect it to be 1732ebcddc72SAlan Cox * reused. 17331c7c3c6aSMatthew Dillon */ 17346031c68dSAlan Cox KASSERT(!pmap_page_is_write_mapped(m), 1735016a3c93SAlan Cox ("swp_pager_async_iodone: page %p is not write" 1736016a3c93SAlan Cox " protected", m)); 1737c52e7044SAlan Cox vm_page_undirty(m); 1738ebcddc72SAlan Cox vm_page_deactivate_noreuse(m); 1739ebcddc72SAlan Cox vm_page_sunbusy(m); 17403c4a2440SAlan Cox } 17413c4a2440SAlan Cox } 174226f9a767SRodney W. Grimes 17431c7c3c6aSMatthew Dillon /* 17441c7c3c6aSMatthew Dillon * adjust pip. NOTE: the original parent may still have its own 17451c7c3c6aSMatthew Dillon * pip refs on the object. 17461c7c3c6aSMatthew Dillon */ 17470d420ad3SAlan Cox if (object != NULL) { 17481c7c3c6aSMatthew Dillon vm_object_pip_wakeupn(object, bp->b_npages); 174989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 17500d420ad3SAlan Cox } 175126f9a767SRodney W. Grimes 17521c7c3c6aSMatthew Dillon /* 1753100650deSOlivier Houchard * swapdev_strategy() manually sets b_vp and b_bufobj before calling 1754100650deSOlivier Houchard * bstrategy(). Set them back to NULL now we're done with it, or we'll 1755100650deSOlivier Houchard * trigger a KASSERT in relpbuf(). 1756100650deSOlivier Houchard */ 1757100650deSOlivier Houchard if (bp->b_vp) { 1758100650deSOlivier Houchard bp->b_vp = NULL; 1759100650deSOlivier Houchard bp->b_bufobj = NULL; 1760100650deSOlivier Houchard } 1761100650deSOlivier Houchard /* 17621c7c3c6aSMatthew Dillon * release the physical I/O buffer 17631c7c3c6aSMatthew Dillon */ 1764756a5412SGleb Smirnoff if (bp->b_flags & B_ASYNC) { 1765756a5412SGleb Smirnoff mtx_lock(&swbuf_mtx); 1766756a5412SGleb Smirnoff if (++nsw_wcount_async == 1) 1767756a5412SGleb Smirnoff wakeup(&nsw_wcount_async); 1768756a5412SGleb Smirnoff mtx_unlock(&swbuf_mtx); 1769756a5412SGleb Smirnoff } 1770756a5412SGleb Smirnoff uma_zfree((bp->b_iocmd == BIO_READ) ? swrbuf_zone : swwbuf_zone, bp); 177126f9a767SRodney W. Grimes } 17721c7c3c6aSMatthew Dillon 1773b1fd102eSMark Johnston int 1774b1fd102eSMark Johnston swap_pager_nswapdev(void) 1775b1fd102eSMark Johnston { 1776b1fd102eSMark Johnston 1777b1fd102eSMark Johnston return (nswapdev); 1778b1fd102eSMark Johnston } 1779b1fd102eSMark Johnston 17807c022327SDoug Moore static void 17817c022327SDoug Moore swp_pager_force_dirty(vm_page_t m) 17827c022327SDoug Moore { 17837c022327SDoug Moore 17847c022327SDoug Moore vm_page_dirty(m); 178556948d17SDoug Moore swap_pager_unswapped(m); 17867c022327SDoug Moore vm_page_launder(m); 178792da00bbSMatthew Dillon } 178892da00bbSMatthew Dillon 1789ecfbddf0SKonstantin Belousov u_long 1790ecfbddf0SKonstantin Belousov swap_pager_swapped_pages(vm_object_t object) 1791ecfbddf0SKonstantin Belousov { 1792ecfbddf0SKonstantin Belousov struct swblk *sb; 1793ecfbddf0SKonstantin Belousov vm_pindex_t pi; 1794ecfbddf0SKonstantin Belousov u_long res; 1795ecfbddf0SKonstantin Belousov int i; 1796ecfbddf0SKonstantin Belousov 1797ecfbddf0SKonstantin Belousov VM_OBJECT_ASSERT_LOCKED(object); 17984b8365d7SKonstantin Belousov if ((object->flags & OBJ_SWAP) == 0) 1799ecfbddf0SKonstantin Belousov return (0); 1800ecfbddf0SKonstantin Belousov 1801ecfbddf0SKonstantin Belousov for (res = 0, pi = 0; (sb = SWAP_PCTRIE_LOOKUP_GE( 1802ecfbddf0SKonstantin Belousov &object->un_pager.swp.swp_blks, pi)) != NULL; 1803ecfbddf0SKonstantin Belousov pi = sb->p + SWAP_META_PAGES) { 1804ecfbddf0SKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) { 1805ecfbddf0SKonstantin Belousov if (sb->d[i] != SWAPBLK_NONE) 1806ecfbddf0SKonstantin Belousov res++; 1807ecfbddf0SKonstantin Belousov } 1808ecfbddf0SKonstantin Belousov } 1809ecfbddf0SKonstantin Belousov return (res); 1810ecfbddf0SKonstantin Belousov } 1811ecfbddf0SKonstantin Belousov 181292da00bbSMatthew Dillon /* 18137c022327SDoug Moore * swap_pager_swapoff_object: 18147c022327SDoug Moore * 18157c022327SDoug Moore * Page in all of the pages that have been paged out for an object 181656948d17SDoug Moore * to a swap device. 18177c022327SDoug Moore */ 18187c022327SDoug Moore static void 18197c022327SDoug Moore swap_pager_swapoff_object(struct swdevt *sp, vm_object_t object) 18207c022327SDoug Moore { 18217c022327SDoug Moore struct swblk *sb; 1822c90d075bSMark Johnston vm_page_t m; 1823c90d075bSMark Johnston vm_pindex_t pi; 1824c90d075bSMark Johnston daddr_t blk; 1825c90d075bSMark Johnston int i, nv, rahead, rv; 18267c022327SDoug Moore 18274b8365d7SKonstantin Belousov KASSERT((object->flags & OBJ_SWAP) != 0, 18288ecbf14bSDoug Moore ("%s: Object not swappable", __func__)); 1829c90d075bSMark Johnston 18307c022327SDoug Moore for (pi = 0; (sb = SWAP_PCTRIE_LOOKUP_GE( 18317c022327SDoug Moore &object->un_pager.swp.swp_blks, pi)) != NULL; ) { 1832c90d075bSMark Johnston if ((object->flags & OBJ_DEAD) != 0) { 1833c90d075bSMark Johnston /* 1834c90d075bSMark Johnston * Make sure that pending writes finish before 1835c90d075bSMark Johnston * returning. 1836c90d075bSMark Johnston */ 1837c90d075bSMark Johnston vm_object_pip_wait(object, "swpoff"); 1838c90d075bSMark Johnston swp_pager_meta_free_all(object); 1839c90d075bSMark Johnston break; 1840c90d075bSMark Johnston } 18417c022327SDoug Moore for (i = 0; i < SWAP_META_PAGES; i++) { 184256948d17SDoug Moore /* 1843c90d075bSMark Johnston * Count the number of contiguous valid blocks. 184456948d17SDoug Moore */ 1845c90d075bSMark Johnston for (nv = 0; nv < SWAP_META_PAGES - i; nv++) { 1846c90d075bSMark Johnston blk = sb->d[i + nv]; 1847c90d075bSMark Johnston if (!swp_pager_isondev(blk, sp) || 1848c90d075bSMark Johnston blk == SWAPBLK_NONE) 1849c90d075bSMark Johnston break; 185056948d17SDoug Moore } 1851c90d075bSMark Johnston if (nv == 0) 18527c022327SDoug Moore continue; 185356948d17SDoug Moore 185456948d17SDoug Moore /* 1855c90d075bSMark Johnston * Look for a page corresponding to the first 1856c90d075bSMark Johnston * valid block and ensure that any pending paging 1857c90d075bSMark Johnston * operations on it are complete. If the page is valid, 1858c90d075bSMark Johnston * mark it dirty and free the swap block. Try to batch 1859c90d075bSMark Johnston * this operation since it may cause sp to be freed, 1860c90d075bSMark Johnston * meaning that we must restart the scan. Avoid busying 1861c90d075bSMark Johnston * valid pages since we may block forever on kernel 1862c90d075bSMark Johnston * stack pages. 186356948d17SDoug Moore */ 1864c90d075bSMark Johnston m = vm_page_lookup(object, sb->p + i); 1865c90d075bSMark Johnston if (m == NULL) { 1866c90d075bSMark Johnston m = vm_page_alloc(object, sb->p + i, 1867c90d075bSMark Johnston VM_ALLOC_NORMAL | VM_ALLOC_WAITFAIL); 1868c90d075bSMark Johnston if (m == NULL) 1869c90d075bSMark Johnston break; 1870c90d075bSMark Johnston } else { 1871c90d075bSMark Johnston if ((m->oflags & VPO_SWAPINPROG) != 0) { 1872c90d075bSMark Johnston m->oflags |= VPO_SWAPSLEEP; 1873c90d075bSMark Johnston VM_OBJECT_SLEEP(object, &object->handle, 1874c90d075bSMark Johnston PSWP, "swpoff", 0); 1875c90d075bSMark Johnston break; 1876c90d075bSMark Johnston } 1877c90d075bSMark Johnston if (vm_page_all_valid(m)) { 1878c90d075bSMark Johnston do { 1879c90d075bSMark Johnston swp_pager_force_dirty(m); 1880c90d075bSMark Johnston } while (--nv > 0 && 1881c90d075bSMark Johnston (m = vm_page_next(m)) != NULL && 1882c90d075bSMark Johnston vm_page_all_valid(m) && 1883c90d075bSMark Johnston (m->oflags & VPO_SWAPINPROG) == 0); 1884c90d075bSMark Johnston break; 1885c90d075bSMark Johnston } 1886c90d075bSMark Johnston if (!vm_page_busy_acquire(m, VM_ALLOC_WAITFAIL)) 1887c90d075bSMark Johnston break; 18887c022327SDoug Moore } 188956948d17SDoug Moore 1890c90d075bSMark Johnston vm_object_pip_add(object, 1); 1891c90d075bSMark Johnston rahead = SWAP_META_PAGES; 1892c90d075bSMark Johnston rv = swap_pager_getpages_locked(object, &m, 1, NULL, 1893c90d075bSMark Johnston &rahead); 1894c90d075bSMark Johnston if (rv != VM_PAGER_OK) 1895c90d075bSMark Johnston panic("%s: read from swap failed: %d", 1896c90d075bSMark Johnston __func__, rv); 1897c90d075bSMark Johnston vm_object_pip_wakeupn(object, 1); 1898c90d075bSMark Johnston VM_OBJECT_WLOCK(object); 1899c90d075bSMark Johnston vm_page_xunbusy(m); 1900c90d075bSMark Johnston 190156948d17SDoug Moore /* 1902c90d075bSMark Johnston * The object lock was dropped so we must restart the 1903c90d075bSMark Johnston * scan of this swap block. Pages paged in during this 1904c90d075bSMark Johnston * iteration will be marked dirty in a future iteration. 190556948d17SDoug Moore */ 190656948d17SDoug Moore break; 190756948d17SDoug Moore } 190856948d17SDoug Moore if (i == SWAP_META_PAGES) 190956948d17SDoug Moore pi = sb->p + SWAP_META_PAGES; 191056948d17SDoug Moore } 19117c022327SDoug Moore } 19127c022327SDoug Moore 19137c022327SDoug Moore /* 191492da00bbSMatthew Dillon * swap_pager_swapoff: 191592da00bbSMatthew Dillon * 191692da00bbSMatthew Dillon * Page in all of the pages that have been paged out to the 191792da00bbSMatthew Dillon * given device. The corresponding blocks in the bitmap must be 191892da00bbSMatthew Dillon * marked as allocated and the device must be flagged SW_CLOSING. 191992da00bbSMatthew Dillon * There may be no processes swapped out to the device. 192092da00bbSMatthew Dillon * 192192da00bbSMatthew Dillon * This routine may block. 192292da00bbSMatthew Dillon */ 1923e9c0cc15SPoul-Henning Kamp static void 1924b3fed13eSDavid Schultz swap_pager_swapoff(struct swdevt *sp) 192592da00bbSMatthew Dillon { 1926f425ab8eSKonstantin Belousov vm_object_t object; 19277c022327SDoug Moore int retries; 192892da00bbSMatthew Dillon 192904533e1eSKonstantin Belousov sx_assert(&swdev_syscall_lock, SA_XLOCKED); 193092da00bbSMatthew Dillon 19318bc61209SDavid Schultz retries = 0; 193292da00bbSMatthew Dillon full_rescan: 1933f425ab8eSKonstantin Belousov mtx_lock(&vm_object_list_mtx); 1934f425ab8eSKonstantin Belousov TAILQ_FOREACH(object, &vm_object_list, object_list) { 19354b8365d7SKonstantin Belousov if ((object->flags & OBJ_SWAP) == 0) 193698150664SKonstantin Belousov continue; 1937f425ab8eSKonstantin Belousov mtx_unlock(&vm_object_list_mtx); 193898150664SKonstantin Belousov /* Depends on type-stability. */ 193998150664SKonstantin Belousov VM_OBJECT_WLOCK(object); 1940f425ab8eSKonstantin Belousov 1941f425ab8eSKonstantin Belousov /* 1942f425ab8eSKonstantin Belousov * Dead objects are eventually terminated on their own. 1943f425ab8eSKonstantin Belousov */ 1944f425ab8eSKonstantin Belousov if ((object->flags & OBJ_DEAD) != 0) 1945f425ab8eSKonstantin Belousov goto next_obj; 1946f425ab8eSKonstantin Belousov 1947f425ab8eSKonstantin Belousov /* 1948f425ab8eSKonstantin Belousov * Sync with fences placed after pctrie 1949f425ab8eSKonstantin Belousov * initialization. We must not access pctrie below 1950f425ab8eSKonstantin Belousov * unless we checked that our object is swap and not 1951f425ab8eSKonstantin Belousov * dead. 1952f425ab8eSKonstantin Belousov */ 1953f425ab8eSKonstantin Belousov atomic_thread_fence_acq(); 19544b8365d7SKonstantin Belousov if ((object->flags & OBJ_SWAP) == 0) 1955f425ab8eSKonstantin Belousov goto next_obj; 1956f425ab8eSKonstantin Belousov 19577c022327SDoug Moore swap_pager_swapoff_object(sp, object); 1958f425ab8eSKonstantin Belousov next_obj: 1959f425ab8eSKonstantin Belousov VM_OBJECT_WUNLOCK(object); 1960f425ab8eSKonstantin Belousov mtx_lock(&vm_object_list_mtx); 196192da00bbSMatthew Dillon } 1962f425ab8eSKonstantin Belousov mtx_unlock(&vm_object_list_mtx); 1963f425ab8eSKonstantin Belousov 19648bc61209SDavid Schultz if (sp->sw_used) { 196592da00bbSMatthew Dillon /* 19668bc61209SDavid Schultz * Objects may be locked or paging to the device being 19678bc61209SDavid Schultz * removed, so we will miss their pages and need to 19688bc61209SDavid Schultz * make another pass. We have marked this device as 19698bc61209SDavid Schultz * SW_CLOSING, so the activity should finish soon. 197092da00bbSMatthew Dillon */ 19718bc61209SDavid Schultz retries++; 19728bc61209SDavid Schultz if (retries > 100) { 19738bc61209SDavid Schultz panic("swapoff: failed to locate %d swap blocks", 19748bc61209SDavid Schultz sp->sw_used); 19758bc61209SDavid Schultz } 19764d70511aSJohn Baldwin pause("swpoff", hz / 20); 197792da00bbSMatthew Dillon goto full_rescan; 197892da00bbSMatthew Dillon } 1979b1fd102eSMark Johnston EVENTHANDLER_INVOKE(swapoff, sp); 198092da00bbSMatthew Dillon } 198192da00bbSMatthew Dillon 19821c7c3c6aSMatthew Dillon /************************************************************************ 19831c7c3c6aSMatthew Dillon * SWAP META DATA * 19841c7c3c6aSMatthew Dillon ************************************************************************ 19851c7c3c6aSMatthew Dillon * 19861c7c3c6aSMatthew Dillon * These routines manipulate the swap metadata stored in the 1987cec9f109SDavid E. O'Brien * OBJT_SWAP object. 19881c7c3c6aSMatthew Dillon * 19894dcc5c2dSMatthew Dillon * Swap metadata is implemented with a global hash and not directly 19904dcc5c2dSMatthew Dillon * linked into the object. Instead the object simply contains 19914dcc5c2dSMatthew Dillon * appropriate tracking counters. 19921c7c3c6aSMatthew Dillon */ 19931c7c3c6aSMatthew Dillon 19941c7c3c6aSMatthew Dillon /* 1995230869e0SAlan Cox * SWP_PAGER_SWBLK_EMPTY() - is a range of blocks free? 1996230869e0SAlan Cox */ 1997230869e0SAlan Cox static bool 1998230869e0SAlan Cox swp_pager_swblk_empty(struct swblk *sb, int start, int limit) 1999230869e0SAlan Cox { 2000230869e0SAlan Cox int i; 2001230869e0SAlan Cox 2002230869e0SAlan Cox MPASS(0 <= start && start <= limit && limit <= SWAP_META_PAGES); 2003230869e0SAlan Cox for (i = start; i < limit; i++) { 2004230869e0SAlan Cox if (sb->d[i] != SWAPBLK_NONE) 2005230869e0SAlan Cox return (false); 2006230869e0SAlan Cox } 2007230869e0SAlan Cox return (true); 2008230869e0SAlan Cox } 2009230869e0SAlan Cox 2010230869e0SAlan Cox /* 2011abdab7b6SDoug Moore * SWP_PAGER_FREE_EMPTY_SWBLK() - frees if a block is free 2012abdab7b6SDoug Moore * 2013abdab7b6SDoug Moore * Nothing is done if the block is still in use. 2014abdab7b6SDoug Moore */ 2015abdab7b6SDoug Moore static void 2016abdab7b6SDoug Moore swp_pager_free_empty_swblk(vm_object_t object, struct swblk *sb) 2017abdab7b6SDoug Moore { 2018abdab7b6SDoug Moore 2019abdab7b6SDoug Moore if (swp_pager_swblk_empty(sb, 0, SWAP_META_PAGES)) { 2020abdab7b6SDoug Moore SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, sb->p); 2021abdab7b6SDoug Moore uma_zfree(swblk_zone, sb); 2022abdab7b6SDoug Moore } 2023abdab7b6SDoug Moore } 2024abdab7b6SDoug Moore 2025abdab7b6SDoug Moore /* 20261c7c3c6aSMatthew Dillon * SWP_PAGER_META_BUILD() - add swap block to swap meta data for object 20271c7c3c6aSMatthew Dillon * 20281c7c3c6aSMatthew Dillon * We first convert the object to a swap object if it is a default 20291c7c3c6aSMatthew Dillon * object. 20301c7c3c6aSMatthew Dillon * 20311c7c3c6aSMatthew Dillon * The specified swapblk is added to the object's swap metadata. If 20321c7c3c6aSMatthew Dillon * the swapblk is not valid, it is freed instead. Any previously 203378f1deefSAlan Cox * assigned swapblk is returned. 20341c7c3c6aSMatthew Dillon */ 203578f1deefSAlan Cox static daddr_t 20362f249180SPoul-Henning Kamp swp_pager_meta_build(vm_object_t object, vm_pindex_t pindex, daddr_t swapblk) 20372f249180SPoul-Henning Kamp { 2038f425ab8eSKonstantin Belousov static volatile int swblk_zone_exhausted, swpctrie_zone_exhausted; 2039eed99cb8SKonstantin Belousov struct swblk *sb, *sb1; 2040f425ab8eSKonstantin Belousov vm_pindex_t modpi, rdpi; 204178f1deefSAlan Cox daddr_t prev_swapblk; 2042f425ab8eSKonstantin Belousov int error, i; 20431c7c3c6aSMatthew Dillon 204489f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 2045f425ab8eSKonstantin Belousov 20461c7c3c6aSMatthew Dillon /* 20471c7c3c6aSMatthew Dillon * Convert default object to swap object if necessary 20481c7c3c6aSMatthew Dillon */ 20494b8365d7SKonstantin Belousov if ((object->flags & OBJ_SWAP) == 0) { 2050f425ab8eSKonstantin Belousov pctrie_init(&object->un_pager.swp.swp_blks); 2051f425ab8eSKonstantin Belousov 2052f425ab8eSKonstantin Belousov /* 2053f425ab8eSKonstantin Belousov * Ensure that swap_pager_swapoff()'s iteration over 2054f425ab8eSKonstantin Belousov * object_list does not see a garbage pctrie. 2055f425ab8eSKonstantin Belousov */ 2056f425ab8eSKonstantin Belousov atomic_thread_fence_rel(); 2057f425ab8eSKonstantin Belousov 20581c7c3c6aSMatthew Dillon object->type = OBJT_SWAP; 20594b8365d7SKonstantin Belousov vm_object_set_flag(object, OBJ_SWAP); 2060fe7bcbafSKyle Evans object->un_pager.swp.writemappings = 0; 206167388836SKonstantin Belousov KASSERT((object->flags & OBJ_ANON) != 0 || 206267388836SKonstantin Belousov object->handle == NULL, 206367388836SKonstantin Belousov ("default pager %p with handle %p", 206467388836SKonstantin Belousov object, object->handle)); 2065bd228075SAlan Cox } 20661c7c3c6aSMatthew Dillon 2067f425ab8eSKonstantin Belousov rdpi = rounddown(pindex, SWAP_META_PAGES); 2068f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, rdpi); 2069f425ab8eSKonstantin Belousov if (sb == NULL) { 20701c7c3c6aSMatthew Dillon if (swapblk == SWAPBLK_NONE) 207178f1deefSAlan Cox return (SWAPBLK_NONE); 2072f425ab8eSKonstantin Belousov for (;;) { 2073f425ab8eSKonstantin Belousov sb = uma_zalloc(swblk_zone, M_NOWAIT | (curproc == 2074f425ab8eSKonstantin Belousov pageproc ? M_USE_RESERVE : 0)); 2075f425ab8eSKonstantin Belousov if (sb != NULL) { 2076f425ab8eSKonstantin Belousov sb->p = rdpi; 2077f425ab8eSKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) 2078f425ab8eSKonstantin Belousov sb->d[i] = SWAPBLK_NONE; 2079f425ab8eSKonstantin Belousov if (atomic_cmpset_int(&swblk_zone_exhausted, 2080f425ab8eSKonstantin Belousov 1, 0)) 2081f425ab8eSKonstantin Belousov printf("swblk zone ok\n"); 2082f425ab8eSKonstantin Belousov break; 2083f425ab8eSKonstantin Belousov } 208489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 2085f425ab8eSKonstantin Belousov if (uma_zone_exhausted(swblk_zone)) { 2086f425ab8eSKonstantin Belousov if (atomic_cmpset_int(&swblk_zone_exhausted, 2087f425ab8eSKonstantin Belousov 0, 1)) 2088f425ab8eSKonstantin Belousov printf("swap blk zone exhausted, " 20893ff863f1SDag-Erling Smørgrav "increase kern.maxswzone\n"); 20902025d69bSKonstantin Belousov vm_pageout_oom(VM_OOM_SWAPZ); 2091f425ab8eSKonstantin Belousov pause("swzonxb", 10); 20922025d69bSKonstantin Belousov } else 20938d6fbbb8SJeff Roberson uma_zwait(swblk_zone); 209489f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 2095eed99cb8SKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, 2096eed99cb8SKonstantin Belousov rdpi); 2097eed99cb8SKonstantin Belousov if (sb != NULL) 2098eed99cb8SKonstantin Belousov /* 2099eed99cb8SKonstantin Belousov * Somebody swapped out a nearby page, 2100eed99cb8SKonstantin Belousov * allocating swblk at the rdpi index, 2101eed99cb8SKonstantin Belousov * while we dropped the object lock. 2102eed99cb8SKonstantin Belousov */ 2103eed99cb8SKonstantin Belousov goto allocated; 21044dcc5c2dSMatthew Dillon } 2105f425ab8eSKonstantin Belousov for (;;) { 2106f425ab8eSKonstantin Belousov error = SWAP_PCTRIE_INSERT( 2107f425ab8eSKonstantin Belousov &object->un_pager.swp.swp_blks, sb); 2108f425ab8eSKonstantin Belousov if (error == 0) { 2109f425ab8eSKonstantin Belousov if (atomic_cmpset_int(&swpctrie_zone_exhausted, 2110f425ab8eSKonstantin Belousov 1, 0)) 2111f425ab8eSKonstantin Belousov printf("swpctrie zone ok\n"); 2112f425ab8eSKonstantin Belousov break; 21131c7c3c6aSMatthew Dillon } 2114f425ab8eSKonstantin Belousov VM_OBJECT_WUNLOCK(object); 2115f425ab8eSKonstantin Belousov if (uma_zone_exhausted(swpctrie_zone)) { 2116f425ab8eSKonstantin Belousov if (atomic_cmpset_int(&swpctrie_zone_exhausted, 2117f425ab8eSKonstantin Belousov 0, 1)) 2118f425ab8eSKonstantin Belousov printf("swap pctrie zone exhausted, " 2119f425ab8eSKonstantin Belousov "increase kern.maxswzone\n"); 2120f425ab8eSKonstantin Belousov vm_pageout_oom(VM_OOM_SWAPZ); 2121f425ab8eSKonstantin Belousov pause("swzonxp", 10); 2122f425ab8eSKonstantin Belousov } else 21238d6fbbb8SJeff Roberson uma_zwait(swpctrie_zone); 2124f425ab8eSKonstantin Belousov VM_OBJECT_WLOCK(object); 2125eed99cb8SKonstantin Belousov sb1 = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, 2126eed99cb8SKonstantin Belousov rdpi); 2127eed99cb8SKonstantin Belousov if (sb1 != NULL) { 2128eed99cb8SKonstantin Belousov uma_zfree(swblk_zone, sb); 2129eed99cb8SKonstantin Belousov sb = sb1; 2130eed99cb8SKonstantin Belousov goto allocated; 21311c7c3c6aSMatthew Dillon } 2132f425ab8eSKonstantin Belousov } 2133eed99cb8SKonstantin Belousov } 2134eed99cb8SKonstantin Belousov allocated: 2135f425ab8eSKonstantin Belousov MPASS(sb->p == rdpi); 21361c7c3c6aSMatthew Dillon 2137f425ab8eSKonstantin Belousov modpi = pindex % SWAP_META_PAGES; 213878f1deefSAlan Cox /* Return prior contents of metadata. */ 213978f1deefSAlan Cox prev_swapblk = sb->d[modpi]; 2140f425ab8eSKonstantin Belousov /* Enter block into metadata. */ 2141f425ab8eSKonstantin Belousov sb->d[modpi] = swapblk; 214285d88d87SKonstantin Belousov 214385d88d87SKonstantin Belousov /* 214485d88d87SKonstantin Belousov * Free the swblk if we end up with the empty page run. 214585d88d87SKonstantin Belousov */ 2146abdab7b6SDoug Moore if (swapblk == SWAPBLK_NONE) 2147abdab7b6SDoug Moore swp_pager_free_empty_swblk(object, sb); 214878f1deefSAlan Cox return (prev_swapblk); 214985d88d87SKonstantin Belousov } 21501c7c3c6aSMatthew Dillon 21511c7c3c6aSMatthew Dillon /* 2152467057fcSDoug Moore * SWP_PAGER_META_TRANSFER() - free a range of blocks in the srcobject's swap 2153467057fcSDoug Moore * metadata, or transfer it into dstobject. 2154467057fcSDoug Moore * 2155467057fcSDoug Moore * This routine will free swap metadata structures as they are cleaned 2156467057fcSDoug Moore * out. 2157467057fcSDoug Moore */ 2158467057fcSDoug Moore static void 2159467057fcSDoug Moore swp_pager_meta_transfer(vm_object_t srcobject, vm_object_t dstobject, 2160467057fcSDoug Moore vm_pindex_t pindex, vm_pindex_t count) 2161467057fcSDoug Moore { 2162467057fcSDoug Moore struct swblk *sb; 2163467057fcSDoug Moore daddr_t n_free, s_free; 2164467057fcSDoug Moore vm_pindex_t offset, last; 2165467057fcSDoug Moore int i, limit, start; 2166467057fcSDoug Moore 2167467057fcSDoug Moore VM_OBJECT_ASSERT_WLOCKED(srcobject); 21684b8365d7SKonstantin Belousov if ((srcobject->flags & OBJ_SWAP) == 0 || count == 0) 2169467057fcSDoug Moore return; 2170467057fcSDoug Moore 2171467057fcSDoug Moore swp_pager_init_freerange(&s_free, &n_free); 2172467057fcSDoug Moore offset = pindex; 2173467057fcSDoug Moore last = pindex + count; 2174467057fcSDoug Moore for (;;) { 2175467057fcSDoug Moore sb = SWAP_PCTRIE_LOOKUP_GE(&srcobject->un_pager.swp.swp_blks, 2176467057fcSDoug Moore rounddown(pindex, SWAP_META_PAGES)); 2177467057fcSDoug Moore if (sb == NULL || sb->p >= last) 2178467057fcSDoug Moore break; 2179467057fcSDoug Moore start = pindex > sb->p ? pindex - sb->p : 0; 2180467057fcSDoug Moore limit = last - sb->p < SWAP_META_PAGES ? last - sb->p : 2181467057fcSDoug Moore SWAP_META_PAGES; 2182467057fcSDoug Moore for (i = start; i < limit; i++) { 2183467057fcSDoug Moore if (sb->d[i] == SWAPBLK_NONE) 2184467057fcSDoug Moore continue; 2185467057fcSDoug Moore if (dstobject == NULL || 2186467057fcSDoug Moore !swp_pager_xfer_source(srcobject, dstobject, 2187467057fcSDoug Moore sb->p + i - offset, sb->d[i])) { 2188467057fcSDoug Moore swp_pager_update_freerange(&s_free, &n_free, 2189467057fcSDoug Moore sb->d[i]); 2190467057fcSDoug Moore } 2191467057fcSDoug Moore sb->d[i] = SWAPBLK_NONE; 2192467057fcSDoug Moore } 2193467057fcSDoug Moore pindex = sb->p + SWAP_META_PAGES; 2194467057fcSDoug Moore if (swp_pager_swblk_empty(sb, 0, start) && 2195467057fcSDoug Moore swp_pager_swblk_empty(sb, limit, SWAP_META_PAGES)) { 2196467057fcSDoug Moore SWAP_PCTRIE_REMOVE(&srcobject->un_pager.swp.swp_blks, 2197467057fcSDoug Moore sb->p); 2198467057fcSDoug Moore uma_zfree(swblk_zone, sb); 2199467057fcSDoug Moore } 2200467057fcSDoug Moore } 2201467057fcSDoug Moore swp_pager_freeswapspace(s_free, n_free); 2202467057fcSDoug Moore } 2203467057fcSDoug Moore 2204467057fcSDoug Moore /* 22051c7c3c6aSMatthew Dillon * SWP_PAGER_META_FREE() - free a range of blocks in the object's swap metadata 22061c7c3c6aSMatthew Dillon * 22071c7c3c6aSMatthew Dillon * The requested range of blocks is freed, with any associated swap 22081c7c3c6aSMatthew Dillon * returned to the swap bitmap. 22091c7c3c6aSMatthew Dillon * 22101c7c3c6aSMatthew Dillon * This routine will free swap metadata structures as they are cleaned 22111c7c3c6aSMatthew Dillon * out. This routine does *NOT* operate on swap metadata associated 22121c7c3c6aSMatthew Dillon * with resident pages. 22131c7c3c6aSMatthew Dillon */ 22141c7c3c6aSMatthew Dillon static void 2215f425ab8eSKonstantin Belousov swp_pager_meta_free(vm_object_t object, vm_pindex_t pindex, vm_pindex_t count) 22161c7c3c6aSMatthew Dillon { 2217467057fcSDoug Moore swp_pager_meta_transfer(object, NULL, pindex, count); 22181c7c3c6aSMatthew Dillon } 22191c7c3c6aSMatthew Dillon 22201c7c3c6aSMatthew Dillon /* 22211c7c3c6aSMatthew Dillon * SWP_PAGER_META_FREE_ALL() - destroy all swap metadata associated with object 22221c7c3c6aSMatthew Dillon * 22231c7c3c6aSMatthew Dillon * This routine locates and destroys all swap metadata associated with 22241c7c3c6aSMatthew Dillon * an object. 22251c7c3c6aSMatthew Dillon */ 22261c7c3c6aSMatthew Dillon static void 22271c7c3c6aSMatthew Dillon swp_pager_meta_free_all(vm_object_t object) 22281c7c3c6aSMatthew Dillon { 2229f425ab8eSKonstantin Belousov struct swblk *sb; 223078f1deefSAlan Cox daddr_t n_free, s_free; 2231f425ab8eSKonstantin Belousov vm_pindex_t pindex; 223271057cd2SKonstantin Belousov int i; 22331c7c3c6aSMatthew Dillon 223489f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 22354b8365d7SKonstantin Belousov if ((object->flags & OBJ_SWAP) == 0) 22361c7c3c6aSMatthew Dillon return; 22371c7c3c6aSMatthew Dillon 223878f1deefSAlan Cox swp_pager_init_freerange(&s_free, &n_free); 2239f425ab8eSKonstantin Belousov for (pindex = 0; (sb = SWAP_PCTRIE_LOOKUP_GE( 2240f425ab8eSKonstantin Belousov &object->un_pager.swp.swp_blks, pindex)) != NULL;) { 2241f425ab8eSKonstantin Belousov pindex = sb->p + SWAP_META_PAGES; 2242f425ab8eSKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) { 2243230869e0SAlan Cox if (sb->d[i] == SWAPBLK_NONE) 2244230869e0SAlan Cox continue; 224578f1deefSAlan Cox swp_pager_update_freerange(&s_free, &n_free, sb->d[i]); 22461c7c3c6aSMatthew Dillon } 2247f425ab8eSKonstantin Belousov SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, sb->p); 2248f425ab8eSKonstantin Belousov uma_zfree(swblk_zone, sb); 22491c7c3c6aSMatthew Dillon } 225078f1deefSAlan Cox swp_pager_freeswapspace(s_free, n_free); 22511c7c3c6aSMatthew Dillon } 22521c7c3c6aSMatthew Dillon 22531c7c3c6aSMatthew Dillon /* 22544abca9bbSAlan Cox * SWP_PAGER_METACTL() - misc control of swap meta data. 22551c7c3c6aSMatthew Dillon * 22564abca9bbSAlan Cox * This routine is capable of looking up, or removing swapblk 22574abca9bbSAlan Cox * assignments in the swap meta data. It returns the swapblk being 22584abca9bbSAlan Cox * looked-up, popped, or SWAPBLK_NONE if the block was invalid. 22591c7c3c6aSMatthew Dillon * 22601c7c3c6aSMatthew Dillon * When acting on a busy resident page and paging is in progress, we 22611c7c3c6aSMatthew Dillon * have to wait until paging is complete but otherwise can act on the 22621c7c3c6aSMatthew Dillon * busy page. 22631c7c3c6aSMatthew Dillon */ 22641c7c3c6aSMatthew Dillon static daddr_t 22658ecbf14bSDoug Moore swp_pager_meta_lookup(vm_object_t object, vm_pindex_t pindex) 22662f249180SPoul-Henning Kamp { 2267f425ab8eSKonstantin Belousov struct swblk *sb; 22684dcc5c2dSMatthew Dillon 2269c25673ffSAttilio Rao VM_OBJECT_ASSERT_LOCKED(object); 2270ee620ea4SAlan Cox 22711c7c3c6aSMatthew Dillon /* 2272ee620ea4SAlan Cox * The meta data only exists if the object is OBJT_SWAP 22731c7c3c6aSMatthew Dillon * and even then might not be allocated yet. 22741c7c3c6aSMatthew Dillon */ 22754b8365d7SKonstantin Belousov KASSERT((object->flags & OBJ_SWAP) != 0, 22768ecbf14bSDoug Moore ("Lookup object not swappable")); 22771c7c3c6aSMatthew Dillon 2278f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, 2279f425ab8eSKonstantin Belousov rounddown(pindex, SWAP_META_PAGES)); 2280f425ab8eSKonstantin Belousov if (sb == NULL) 2281f425ab8eSKonstantin Belousov return (SWAPBLK_NONE); 22828ecbf14bSDoug Moore return (sb->d[pindex % SWAP_META_PAGES]); 22831c7c3c6aSMatthew Dillon } 22841c7c3c6aSMatthew Dillon 2285e9c0cc15SPoul-Henning Kamp /* 228677d6fd97SKonstantin Belousov * Returns the least page index which is greater than or equal to the 228777d6fd97SKonstantin Belousov * parameter pindex and for which there is a swap block allocated. 228877d6fd97SKonstantin Belousov * Returns object's size if the object's type is not swap or if there 228977d6fd97SKonstantin Belousov * are no allocated swap blocks for the object after the requested 229077d6fd97SKonstantin Belousov * pindex. 229177d6fd97SKonstantin Belousov */ 229277d6fd97SKonstantin Belousov vm_pindex_t 229377d6fd97SKonstantin Belousov swap_pager_find_least(vm_object_t object, vm_pindex_t pindex) 229477d6fd97SKonstantin Belousov { 2295f425ab8eSKonstantin Belousov struct swblk *sb; 2296f425ab8eSKonstantin Belousov int i; 229777d6fd97SKonstantin Belousov 229877d6fd97SKonstantin Belousov VM_OBJECT_ASSERT_LOCKED(object); 22994b8365d7SKonstantin Belousov if ((object->flags & OBJ_SWAP) == 0) 230077d6fd97SKonstantin Belousov return (object->size); 230177d6fd97SKonstantin Belousov 2302f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks, 2303f425ab8eSKonstantin Belousov rounddown(pindex, SWAP_META_PAGES)); 2304f425ab8eSKonstantin Belousov if (sb == NULL) 2305f425ab8eSKonstantin Belousov return (object->size); 2306f425ab8eSKonstantin Belousov if (sb->p < pindex) { 2307f425ab8eSKonstantin Belousov for (i = pindex % SWAP_META_PAGES; i < SWAP_META_PAGES; i++) { 2308f425ab8eSKonstantin Belousov if (sb->d[i] != SWAPBLK_NONE) 2309f425ab8eSKonstantin Belousov return (sb->p + i); 231077d6fd97SKonstantin Belousov } 2311f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks, 2312f425ab8eSKonstantin Belousov roundup(pindex, SWAP_META_PAGES)); 2313f425ab8eSKonstantin Belousov if (sb == NULL) 2314f425ab8eSKonstantin Belousov return (object->size); 231577d6fd97SKonstantin Belousov } 2316f425ab8eSKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) { 2317f425ab8eSKonstantin Belousov if (sb->d[i] != SWAPBLK_NONE) 2318f425ab8eSKonstantin Belousov return (sb->p + i); 231977d6fd97SKonstantin Belousov } 2320f425ab8eSKonstantin Belousov 2321f425ab8eSKonstantin Belousov /* 2322f425ab8eSKonstantin Belousov * We get here if a swblk is present in the trie but it 2323f425ab8eSKonstantin Belousov * doesn't map any blocks. 2324f425ab8eSKonstantin Belousov */ 2325f425ab8eSKonstantin Belousov MPASS(0); 2326f425ab8eSKonstantin Belousov return (object->size); 232777d6fd97SKonstantin Belousov } 232877d6fd97SKonstantin Belousov 232977d6fd97SKonstantin Belousov /* 2330e9c0cc15SPoul-Henning Kamp * System call swapon(name) enables swapping on device name, 2331e9c0cc15SPoul-Henning Kamp * which must be in the swdevsw. Return EBUSY 2332e9c0cc15SPoul-Henning Kamp * if already swapping on this device. 2333e9c0cc15SPoul-Henning Kamp */ 2334e9c0cc15SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_ 2335e9c0cc15SPoul-Henning Kamp struct swapon_args { 2336e9c0cc15SPoul-Henning Kamp char *name; 2337e9c0cc15SPoul-Henning Kamp }; 2338e9c0cc15SPoul-Henning Kamp #endif 2339e9c0cc15SPoul-Henning Kamp 2340e9c0cc15SPoul-Henning Kamp int 23418451d0ddSKip Macy sys_swapon(struct thread *td, struct swapon_args *uap) 2342e9c0cc15SPoul-Henning Kamp { 2343e9c0cc15SPoul-Henning Kamp struct vattr attr; 2344e9c0cc15SPoul-Henning Kamp struct vnode *vp; 2345e9c0cc15SPoul-Henning Kamp struct nameidata nd; 2346e9c0cc15SPoul-Henning Kamp int error; 2347e9c0cc15SPoul-Henning Kamp 2348acd3428bSRobert Watson error = priv_check(td, PRIV_SWAPON); 2349e9c0cc15SPoul-Henning Kamp if (error) 2350acd3428bSRobert Watson return (error); 2351e9c0cc15SPoul-Henning Kamp 235204533e1eSKonstantin Belousov sx_xlock(&swdev_syscall_lock); 2353e9c0cc15SPoul-Henning Kamp 2354e9c0cc15SPoul-Henning Kamp /* 2355e9c0cc15SPoul-Henning Kamp * Swap metadata may not fit in the KVM if we have physical 2356e9c0cc15SPoul-Henning Kamp * memory of >1GB. 2357e9c0cc15SPoul-Henning Kamp */ 2358f425ab8eSKonstantin Belousov if (swblk_zone == NULL) { 2359e9c0cc15SPoul-Henning Kamp error = ENOMEM; 2360e9c0cc15SPoul-Henning Kamp goto done; 2361e9c0cc15SPoul-Henning Kamp } 2362e9c0cc15SPoul-Henning Kamp 23636ddf41faSKonstantin Belousov NDINIT(&nd, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | AUDITVNODE1, 23647e1d3eefSMateusz Guzik UIO_USERSPACE, uap->name); 2365e9c0cc15SPoul-Henning Kamp error = namei(&nd); 2366e9c0cc15SPoul-Henning Kamp if (error) 2367e9c0cc15SPoul-Henning Kamp goto done; 2368e9c0cc15SPoul-Henning Kamp 2369bb92cd7bSMateusz Guzik NDFREE_PNBUF(&nd); 2370e9c0cc15SPoul-Henning Kamp vp = nd.ni_vp; 2371e9c0cc15SPoul-Henning Kamp 23727ad2a82dSMateusz Guzik if (vn_isdisk_error(vp, &error)) { 237388ad2d7bSKonstantin Belousov error = swapongeom(vp); 237420da9c2eSPoul-Henning Kamp } else if (vp->v_type == VREG && 2375e9c0cc15SPoul-Henning Kamp (vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 && 23760359a12eSAttilio Rao (error = VOP_GETATTR(vp, &attr, td->td_ucred)) == 0) { 2377e9c0cc15SPoul-Henning Kamp /* 2378e9c0cc15SPoul-Henning Kamp * Allow direct swapping to NFS regular files in the same 2379e9c0cc15SPoul-Henning Kamp * way that nfs_mountroot() sets up diskless swapping. 2380e9c0cc15SPoul-Henning Kamp */ 238159efee01SPoul-Henning Kamp error = swaponvp(td, vp, attr.va_size / DEV_BSIZE); 2382e9c0cc15SPoul-Henning Kamp } 2383e9c0cc15SPoul-Henning Kamp 23846ddf41faSKonstantin Belousov if (error != 0) 23856ddf41faSKonstantin Belousov vput(vp); 23866ddf41faSKonstantin Belousov else 23876ddf41faSKonstantin Belousov VOP_UNLOCK(vp); 2388e9c0cc15SPoul-Henning Kamp done: 238904533e1eSKonstantin Belousov sx_xunlock(&swdev_syscall_lock); 2390e9c0cc15SPoul-Henning Kamp return (error); 2391e9c0cc15SPoul-Henning Kamp } 2392e9c0cc15SPoul-Henning Kamp 23933ff863f1SDag-Erling Smørgrav /* 23943ff863f1SDag-Erling Smørgrav * Check that the total amount of swap currently configured does not 23953ff863f1SDag-Erling Smørgrav * exceed half the theoretical maximum. If it does, print a warning 239635872e79SKonstantin Belousov * message. 23973ff863f1SDag-Erling Smørgrav */ 239835872e79SKonstantin Belousov static void 239935872e79SKonstantin Belousov swapon_check_swzone(void) 24003ff863f1SDag-Erling Smørgrav { 24013ff863f1SDag-Erling Smørgrav 24023ff863f1SDag-Erling Smørgrav /* recommend using no more than half that amount */ 2403303fa05aSKonstantin Belousov if (swap_total > swap_maxpages / 2) { 24043ff863f1SDag-Erling Smørgrav printf("warning: total configured swap (%lu pages) " 24053ff863f1SDag-Erling Smørgrav "exceeds maximum recommended amount (%lu pages).\n", 2406303fa05aSKonstantin Belousov swap_total, swap_maxpages / 2); 24073ff863f1SDag-Erling Smørgrav printf("warning: increase kern.maxswzone " 24083ff863f1SDag-Erling Smørgrav "or reduce amount of swap.\n"); 24093ff863f1SDag-Erling Smørgrav } 24103ff863f1SDag-Erling Smørgrav } 24113ff863f1SDag-Erling Smørgrav 241259efee01SPoul-Henning Kamp static void 24132cc718a1SKonstantin Belousov swaponsomething(struct vnode *vp, void *id, u_long nblks, 24142cc718a1SKonstantin Belousov sw_strategy_t *strategy, sw_close_t *close, dev_t dev, int flags) 2415e9c0cc15SPoul-Henning Kamp { 24162d9974c1SAlan Cox struct swdevt *sp, *tsp; 241734e2051fSMark Johnston daddr_t dvbase; 2418e9c0cc15SPoul-Henning Kamp 2419e9c0cc15SPoul-Henning Kamp /* 2420e9c0cc15SPoul-Henning Kamp * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks. 2421e9c0cc15SPoul-Henning Kamp * First chop nblks off to page-align it, then convert. 2422e9c0cc15SPoul-Henning Kamp * 2423e9c0cc15SPoul-Henning Kamp * sw->sw_nblks is in page-sized chunks now too. 2424e9c0cc15SPoul-Henning Kamp */ 2425e9c0cc15SPoul-Henning Kamp nblks &= ~(ctodb(1) - 1); 2426e9c0cc15SPoul-Henning Kamp nblks = dbtoc(nblks); 2427e9c0cc15SPoul-Henning Kamp 24288f60c087SPoul-Henning Kamp sp = malloc(sizeof *sp, M_VMPGDATA, M_WAITOK | M_ZERO); 242900fd73d2SDoug Moore sp->sw_blist = blist_create(nblks, M_WAITOK); 2430dee34ca4SPoul-Henning Kamp sp->sw_vp = vp; 2431dee34ca4SPoul-Henning Kamp sp->sw_id = id; 2432f3732fd1SPoul-Henning Kamp sp->sw_dev = dev; 2433e9c0cc15SPoul-Henning Kamp sp->sw_nblks = nblks; 2434e9c0cc15SPoul-Henning Kamp sp->sw_used = 0; 243559efee01SPoul-Henning Kamp sp->sw_strategy = strategy; 2436dee34ca4SPoul-Henning Kamp sp->sw_close = close; 24372cc718a1SKonstantin Belousov sp->sw_flags = flags; 2438e9c0cc15SPoul-Henning Kamp 2439e9c0cc15SPoul-Henning Kamp /* 2440504f5e29SDoug Moore * Do not free the first blocks in order to avoid overwriting 24418f60c087SPoul-Henning Kamp * any bsd label at the front of the partition 2442e9c0cc15SPoul-Henning Kamp */ 2443504f5e29SDoug Moore blist_free(sp->sw_blist, howmany(BBSIZE, PAGE_SIZE), 2444504f5e29SDoug Moore nblks - howmany(BBSIZE, PAGE_SIZE)); 2445e9c0cc15SPoul-Henning Kamp 24462d9974c1SAlan Cox dvbase = 0; 244720da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 24482d9974c1SAlan Cox TAILQ_FOREACH(tsp, &swtailq, sw_list) { 24492d9974c1SAlan Cox if (tsp->sw_end >= dvbase) { 24502d9974c1SAlan Cox /* 24512d9974c1SAlan Cox * We put one uncovered page between the devices 24522d9974c1SAlan Cox * in order to definitively prevent any cross-device 24532d9974c1SAlan Cox * I/O requests 24542d9974c1SAlan Cox */ 24552d9974c1SAlan Cox dvbase = tsp->sw_end + 1; 24562d9974c1SAlan Cox } 24572d9974c1SAlan Cox } 24582d9974c1SAlan Cox sp->sw_first = dvbase; 24592d9974c1SAlan Cox sp->sw_end = dvbase + nblks; 24608f60c087SPoul-Henning Kamp TAILQ_INSERT_TAIL(&swtailq, sp, sw_list); 24618f60c087SPoul-Henning Kamp nswapdev++; 2462504f5e29SDoug Moore swap_pager_avail += nblks - howmany(BBSIZE, PAGE_SIZE); 2463e8bb589dSMatt Macy swap_total += nblks; 246435872e79SKonstantin Belousov swapon_check_swzone(); 2465d05bc129SAlan Cox swp_sizecheck(); 2466d05bc129SAlan Cox mtx_unlock(&sw_dev_mtx); 2467b1fd102eSMark Johnston EVENTHANDLER_INVOKE(swapon, sp); 246859efee01SPoul-Henning Kamp } 2469e9c0cc15SPoul-Henning Kamp 2470e9c0cc15SPoul-Henning Kamp /* 2471e9c0cc15SPoul-Henning Kamp * SYSCALL: swapoff(devname) 2472e9c0cc15SPoul-Henning Kamp * 2473e9c0cc15SPoul-Henning Kamp * Disable swapping on the given device. 2474dee34ca4SPoul-Henning Kamp * 2475dee34ca4SPoul-Henning Kamp * XXX: Badly designed system call: it should use a device index 2476dee34ca4SPoul-Henning Kamp * rather than filename as specification. We keep sw_vp around 2477dee34ca4SPoul-Henning Kamp * only to make this work. 2478e9c0cc15SPoul-Henning Kamp */ 247953465702SKonstantin Belousov static int 248053465702SKonstantin Belousov kern_swapoff(struct thread *td, const char *name, enum uio_seg name_seg, 248153465702SKonstantin Belousov u_int flags) 2482e9c0cc15SPoul-Henning Kamp { 2483e9c0cc15SPoul-Henning Kamp struct vnode *vp; 2484e9c0cc15SPoul-Henning Kamp struct nameidata nd; 2485e9c0cc15SPoul-Henning Kamp struct swdevt *sp; 248653465702SKonstantin Belousov int error; 2487e9c0cc15SPoul-Henning Kamp 2488acd3428bSRobert Watson error = priv_check(td, PRIV_SWAPOFF); 2489a4e4132fSKonstantin Belousov if (error != 0) 2490a4e4132fSKonstantin Belousov return (error); 249153465702SKonstantin Belousov if ((flags & ~(SWAPOFF_FORCE)) != 0) 2492a4e4132fSKonstantin Belousov return (EINVAL); 2493a4e4132fSKonstantin Belousov 249404533e1eSKonstantin Belousov sx_xlock(&swdev_syscall_lock); 2495e9c0cc15SPoul-Henning Kamp 249653465702SKonstantin Belousov NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, name_seg, name); 2497e9c0cc15SPoul-Henning Kamp error = namei(&nd); 2498e9c0cc15SPoul-Henning Kamp if (error) 2499e9c0cc15SPoul-Henning Kamp goto done; 2500bb92cd7bSMateusz Guzik NDFREE_PNBUF(&nd); 2501e9c0cc15SPoul-Henning Kamp vp = nd.ni_vp; 2502e9c0cc15SPoul-Henning Kamp 250320da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 25048f60c087SPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 2505dee34ca4SPoul-Henning Kamp if (sp->sw_vp == vp) 25060909f38aSPawel Jakub Dawidek break; 2507e9c0cc15SPoul-Henning Kamp } 250820da9c2eSPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 25090909f38aSPawel Jakub Dawidek if (sp == NULL) { 2510e9c0cc15SPoul-Henning Kamp error = EINVAL; 2511e9c0cc15SPoul-Henning Kamp goto done; 25120909f38aSPawel Jakub Dawidek } 251353465702SKonstantin Belousov error = swapoff_one(sp, td->td_ucred, flags); 25140909f38aSPawel Jakub Dawidek done: 251504533e1eSKonstantin Belousov sx_xunlock(&swdev_syscall_lock); 25160909f38aSPawel Jakub Dawidek return (error); 25170909f38aSPawel Jakub Dawidek } 25180909f38aSPawel Jakub Dawidek 251953465702SKonstantin Belousov 252053465702SKonstantin Belousov #ifdef COMPAT_FREEBSD13 252153465702SKonstantin Belousov int 252253465702SKonstantin Belousov freebsd13_swapoff(struct thread *td, struct freebsd13_swapoff_args *uap) 252353465702SKonstantin Belousov { 252453465702SKonstantin Belousov return (kern_swapoff(td, uap->name, UIO_USERSPACE, 0)); 252553465702SKonstantin Belousov } 252653465702SKonstantin Belousov #endif 252753465702SKonstantin Belousov 252853465702SKonstantin Belousov int 252953465702SKonstantin Belousov sys_swapoff(struct thread *td, struct swapoff_args *uap) 253053465702SKonstantin Belousov { 253153465702SKonstantin Belousov return (kern_swapoff(td, uap->name, UIO_USERSPACE, uap->flags)); 253253465702SKonstantin Belousov } 253353465702SKonstantin Belousov 25340909f38aSPawel Jakub Dawidek static int 2535e8dc2ba2SKonstantin Belousov swapoff_one(struct swdevt *sp, struct ucred *cred, u_int flags) 25360909f38aSPawel Jakub Dawidek { 253703bdd65fSAlan Cox u_long nblks; 2538e9c0cc15SPoul-Henning Kamp #ifdef MAC 25390909f38aSPawel Jakub Dawidek int error; 2540e9c0cc15SPoul-Henning Kamp #endif 2541e9c0cc15SPoul-Henning Kamp 254204533e1eSKonstantin Belousov sx_assert(&swdev_syscall_lock, SA_XLOCKED); 25430909f38aSPawel Jakub Dawidek #ifdef MAC 2544cb05b60aSAttilio Rao (void) vn_lock(sp->sw_vp, LK_EXCLUSIVE | LK_RETRY); 254535918c55SChristian S.J. Peron error = mac_system_check_swapoff(cred, sp->sw_vp); 2546b249ce48SMateusz Guzik (void) VOP_UNLOCK(sp->sw_vp); 25470909f38aSPawel Jakub Dawidek if (error != 0) 25480909f38aSPawel Jakub Dawidek return (error); 25490909f38aSPawel Jakub Dawidek #endif 2550e9c0cc15SPoul-Henning Kamp nblks = sp->sw_nblks; 2551e9c0cc15SPoul-Henning Kamp 2552e9c0cc15SPoul-Henning Kamp /* 2553e9c0cc15SPoul-Henning Kamp * We can turn off this swap device safely only if the 2554e9c0cc15SPoul-Henning Kamp * available virtual memory in the system will fit the amount 2555e9c0cc15SPoul-Henning Kamp * of data we will have to page back in, plus an epsilon so 2556e9c0cc15SPoul-Henning Kamp * the system doesn't become critically low on swap space. 25570190c38bSKonstantin Belousov * The vm_free_count() part does not account e.g. for clean 25580190c38bSKonstantin Belousov * pages that can be immediately reclaimed without paging, so 25590190c38bSKonstantin Belousov * this is a very rough estimation. 25600190c38bSKonstantin Belousov * 25610190c38bSKonstantin Belousov * On the other hand, not turning swap off on swapoff_all() 25620190c38bSKonstantin Belousov * means that we can lose swap data when filesystems go away, 25630190c38bSKonstantin Belousov * which is arguably worse. 2564e9c0cc15SPoul-Henning Kamp */ 2565e8dc2ba2SKonstantin Belousov if ((flags & SWAPOFF_FORCE) == 0 && 25660190c38bSKonstantin Belousov vm_free_count() + swap_pager_avail < nblks + nswap_lowat) 25670909f38aSPawel Jakub Dawidek return (ENOMEM); 2568e9c0cc15SPoul-Henning Kamp 2569e9c0cc15SPoul-Henning Kamp /* 2570e9c0cc15SPoul-Henning Kamp * Prevent further allocations on this device. 2571e9c0cc15SPoul-Henning Kamp */ 25722928cef7SAlan Cox mtx_lock(&sw_dev_mtx); 2573e9c0cc15SPoul-Henning Kamp sp->sw_flags |= SW_CLOSING; 257403bdd65fSAlan Cox swap_pager_avail -= blist_fill(sp->sw_blist, 0, nblks); 2575e8bb589dSMatt Macy swap_total -= nblks; 25762928cef7SAlan Cox mtx_unlock(&sw_dev_mtx); 2577e9c0cc15SPoul-Henning Kamp 2578e9c0cc15SPoul-Henning Kamp /* 2579e9c0cc15SPoul-Henning Kamp * Page in the contents of the device and close it. 2580e9c0cc15SPoul-Henning Kamp */ 2581b3fed13eSDavid Schultz swap_pager_swapoff(sp); 2582e9c0cc15SPoul-Henning Kamp 258335918c55SChristian S.J. Peron sp->sw_close(curthread, sp); 258420da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 25859e3e3fe5SWarner Losh sp->sw_id = NULL; 25868f60c087SPoul-Henning Kamp TAILQ_REMOVE(&swtailq, sp, sw_list); 25870676a140SAlan Cox nswapdev--; 25887dea2c2eSAlan Cox if (nswapdev == 0) { 25897dea2c2eSAlan Cox swap_pager_full = 2; 25907dea2c2eSAlan Cox swap_pager_almost_full = 1; 25917dea2c2eSAlan Cox } 25928f60c087SPoul-Henning Kamp if (swdevhd == sp) 25938f60c087SPoul-Henning Kamp swdevhd = NULL; 2594d05bc129SAlan Cox mtx_unlock(&sw_dev_mtx); 25958f60c087SPoul-Henning Kamp blist_destroy(sp->sw_blist); 25968f60c087SPoul-Henning Kamp free(sp, M_VMPGDATA); 25970909f38aSPawel Jakub Dawidek return (0); 25980909f38aSPawel Jakub Dawidek } 2599e9c0cc15SPoul-Henning Kamp 26000909f38aSPawel Jakub Dawidek void 26010909f38aSPawel Jakub Dawidek swapoff_all(void) 26020909f38aSPawel Jakub Dawidek { 26030909f38aSPawel Jakub Dawidek struct swdevt *sp, *spt; 26040909f38aSPawel Jakub Dawidek const char *devname; 26050909f38aSPawel Jakub Dawidek int error; 26060909f38aSPawel Jakub Dawidek 260704533e1eSKonstantin Belousov sx_xlock(&swdev_syscall_lock); 26080909f38aSPawel Jakub Dawidek 26090909f38aSPawel Jakub Dawidek mtx_lock(&sw_dev_mtx); 26100909f38aSPawel Jakub Dawidek TAILQ_FOREACH_SAFE(sp, &swtailq, sw_list, spt) { 26110909f38aSPawel Jakub Dawidek mtx_unlock(&sw_dev_mtx); 26127ad2a82dSMateusz Guzik if (vn_isdisk(sp->sw_vp)) 26137870adb6SEd Schouten devname = devtoname(sp->sw_vp->v_rdev); 26140909f38aSPawel Jakub Dawidek else 26150909f38aSPawel Jakub Dawidek devname = "[file]"; 2616e8dc2ba2SKonstantin Belousov error = swapoff_one(sp, thread0.td_ucred, SWAPOFF_FORCE); 26170909f38aSPawel Jakub Dawidek if (error != 0) { 26180909f38aSPawel Jakub Dawidek printf("Cannot remove swap device %s (error=%d), " 26190909f38aSPawel Jakub Dawidek "skipping.\n", devname, error); 26200909f38aSPawel Jakub Dawidek } else if (bootverbose) { 26210909f38aSPawel Jakub Dawidek printf("Swap device %s removed.\n", devname); 26220909f38aSPawel Jakub Dawidek } 26230909f38aSPawel Jakub Dawidek mtx_lock(&sw_dev_mtx); 26240909f38aSPawel Jakub Dawidek } 26250909f38aSPawel Jakub Dawidek mtx_unlock(&sw_dev_mtx); 26260909f38aSPawel Jakub Dawidek 262704533e1eSKonstantin Belousov sx_xunlock(&swdev_syscall_lock); 2628e9c0cc15SPoul-Henning Kamp } 2629e9c0cc15SPoul-Henning Kamp 2630567104a1SPoul-Henning Kamp void 2631567104a1SPoul-Henning Kamp swap_pager_status(int *total, int *used) 2632567104a1SPoul-Henning Kamp { 2633567104a1SPoul-Henning Kamp 26347ce3a312SMateusz Guzik *total = swap_total; 26357ce3a312SMateusz Guzik *used = swap_total - swap_pager_avail - 26367ce3a312SMateusz Guzik nswapdev * howmany(BBSIZE, PAGE_SIZE); 2637567104a1SPoul-Henning Kamp } 2638567104a1SPoul-Henning Kamp 2639dda4f960SKonstantin Belousov int 2640dda4f960SKonstantin Belousov swap_dev_info(int name, struct xswdev *xs, char *devname, size_t len) 2641dda4f960SKonstantin Belousov { 2642dda4f960SKonstantin Belousov struct swdevt *sp; 26437870adb6SEd Schouten const char *tmp_devname; 2644dda4f960SKonstantin Belousov int error, n; 2645dda4f960SKonstantin Belousov 2646dda4f960SKonstantin Belousov n = 0; 2647dda4f960SKonstantin Belousov error = ENOENT; 2648dda4f960SKonstantin Belousov mtx_lock(&sw_dev_mtx); 2649dda4f960SKonstantin Belousov TAILQ_FOREACH(sp, &swtailq, sw_list) { 2650dda4f960SKonstantin Belousov if (n != name) { 2651dda4f960SKonstantin Belousov n++; 2652dda4f960SKonstantin Belousov continue; 2653dda4f960SKonstantin Belousov } 2654dda4f960SKonstantin Belousov xs->xsw_version = XSWDEV_VERSION; 2655dda4f960SKonstantin Belousov xs->xsw_dev = sp->sw_dev; 2656dda4f960SKonstantin Belousov xs->xsw_flags = sp->sw_flags; 2657dda4f960SKonstantin Belousov xs->xsw_nblks = sp->sw_nblks; 2658dda4f960SKonstantin Belousov xs->xsw_used = sp->sw_used; 2659dda4f960SKonstantin Belousov if (devname != NULL) { 26607ad2a82dSMateusz Guzik if (vn_isdisk(sp->sw_vp)) 26617870adb6SEd Schouten tmp_devname = devtoname(sp->sw_vp->v_rdev); 2662dda4f960SKonstantin Belousov else 2663dda4f960SKonstantin Belousov tmp_devname = "[file]"; 2664dda4f960SKonstantin Belousov strncpy(devname, tmp_devname, len); 2665dda4f960SKonstantin Belousov } 2666dda4f960SKonstantin Belousov error = 0; 2667dda4f960SKonstantin Belousov break; 2668dda4f960SKonstantin Belousov } 2669dda4f960SKonstantin Belousov mtx_unlock(&sw_dev_mtx); 2670dda4f960SKonstantin Belousov return (error); 2671dda4f960SKonstantin Belousov } 2672dda4f960SKonstantin Belousov 267369921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11) 267469921123SKonstantin Belousov #define XSWDEV_VERSION_11 1 267569921123SKonstantin Belousov struct xswdev11 { 267669921123SKonstantin Belousov u_int xsw_version; 267769921123SKonstantin Belousov uint32_t xsw_dev; 267869921123SKonstantin Belousov int xsw_flags; 267969921123SKonstantin Belousov int xsw_nblks; 268069921123SKonstantin Belousov int xsw_used; 268169921123SKonstantin Belousov }; 268269921123SKonstantin Belousov #endif 268369921123SKonstantin Belousov 2684f6d281e8SKonstantin Belousov #if defined(__amd64__) && defined(COMPAT_FREEBSD32) 2685f6d281e8SKonstantin Belousov struct xswdev32 { 2686f6d281e8SKonstantin Belousov u_int xsw_version; 2687f6d281e8SKonstantin Belousov u_int xsw_dev1, xsw_dev2; 2688f6d281e8SKonstantin Belousov int xsw_flags; 2689f6d281e8SKonstantin Belousov int xsw_nblks; 2690f6d281e8SKonstantin Belousov int xsw_used; 2691f6d281e8SKonstantin Belousov }; 2692f6d281e8SKonstantin Belousov #endif 2693f6d281e8SKonstantin Belousov 2694e9c0cc15SPoul-Henning Kamp static int 2695e9c0cc15SPoul-Henning Kamp sysctl_vm_swap_info(SYSCTL_HANDLER_ARGS) 2696e9c0cc15SPoul-Henning Kamp { 2697e9c0cc15SPoul-Henning Kamp struct xswdev xs; 2698f6d281e8SKonstantin Belousov #if defined(__amd64__) && defined(COMPAT_FREEBSD32) 2699f6d281e8SKonstantin Belousov struct xswdev32 xs32; 2700f6d281e8SKonstantin Belousov #endif 270169921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11) 270269921123SKonstantin Belousov struct xswdev11 xs11; 270369921123SKonstantin Belousov #endif 2704dda4f960SKonstantin Belousov int error; 2705e9c0cc15SPoul-Henning Kamp 2706e9c0cc15SPoul-Henning Kamp if (arg2 != 1) /* name length */ 2707e9c0cc15SPoul-Henning Kamp return (EINVAL); 270806d1fd9fSMark Johnston 270906d1fd9fSMark Johnston memset(&xs, 0, sizeof(xs)); 2710dda4f960SKonstantin Belousov error = swap_dev_info(*(int *)arg1, &xs, NULL, 0); 2711dda4f960SKonstantin Belousov if (error != 0) 2712dda4f960SKonstantin Belousov return (error); 2713f6d281e8SKonstantin Belousov #if defined(__amd64__) && defined(COMPAT_FREEBSD32) 2714f6d281e8SKonstantin Belousov if (req->oldlen == sizeof(xs32)) { 271506d1fd9fSMark Johnston memset(&xs32, 0, sizeof(xs32)); 2716f6d281e8SKonstantin Belousov xs32.xsw_version = XSWDEV_VERSION; 2717f6d281e8SKonstantin Belousov xs32.xsw_dev1 = xs.xsw_dev; 2718f6d281e8SKonstantin Belousov xs32.xsw_dev2 = xs.xsw_dev >> 32; 2719f6d281e8SKonstantin Belousov xs32.xsw_flags = xs.xsw_flags; 2720f6d281e8SKonstantin Belousov xs32.xsw_nblks = xs.xsw_nblks; 2721f6d281e8SKonstantin Belousov xs32.xsw_used = xs.xsw_used; 2722f6d281e8SKonstantin Belousov error = SYSCTL_OUT(req, &xs32, sizeof(xs32)); 2723f6d281e8SKonstantin Belousov return (error); 2724f6d281e8SKonstantin Belousov } 2725f6d281e8SKonstantin Belousov #endif 272669921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11) 272769921123SKonstantin Belousov if (req->oldlen == sizeof(xs11)) { 272806d1fd9fSMark Johnston memset(&xs11, 0, sizeof(xs11)); 272969921123SKonstantin Belousov xs11.xsw_version = XSWDEV_VERSION_11; 273069921123SKonstantin Belousov xs11.xsw_dev = xs.xsw_dev; /* truncation */ 273169921123SKonstantin Belousov xs11.xsw_flags = xs.xsw_flags; 273269921123SKonstantin Belousov xs11.xsw_nblks = xs.xsw_nblks; 273369921123SKonstantin Belousov xs11.xsw_used = xs.xsw_used; 273469921123SKonstantin Belousov error = SYSCTL_OUT(req, &xs11, sizeof(xs11)); 2735f6d281e8SKonstantin Belousov return (error); 2736f6d281e8SKonstantin Belousov } 273769921123SKonstantin Belousov #endif 2738e9c0cc15SPoul-Henning Kamp error = SYSCTL_OUT(req, &xs, sizeof(xs)); 2739e9c0cc15SPoul-Henning Kamp return (error); 2740e9c0cc15SPoul-Henning Kamp } 2741e9c0cc15SPoul-Henning Kamp 27428f60c087SPoul-Henning Kamp SYSCTL_INT(_vm, OID_AUTO, nswapdev, CTLFLAG_RD, &nswapdev, 0, 2743e9c0cc15SPoul-Henning Kamp "Number of swap devices"); 27444c36e917SKonstantin Belousov SYSCTL_NODE(_vm, OID_AUTO, swap_info, CTLFLAG_RD | CTLFLAG_MPSAFE, 27454c36e917SKonstantin Belousov sysctl_vm_swap_info, 2746e9c0cc15SPoul-Henning Kamp "Swap statistics by device"); 2747ec38b344SPoul-Henning Kamp 2748ec38b344SPoul-Henning Kamp /* 2749f425ab8eSKonstantin Belousov * Count the approximate swap usage in pages for a vmspace. The 2750f425ab8eSKonstantin Belousov * shadowed or not yet copied on write swap blocks are not accounted. 2751ec38b344SPoul-Henning Kamp * The map must be locked. 2752ec38b344SPoul-Henning Kamp */ 27532860553aSRebecca Cran long 2754ec38b344SPoul-Henning Kamp vmspace_swap_count(struct vmspace *vmspace) 2755ec38b344SPoul-Henning Kamp { 275665d8409cSRebecca Cran vm_map_t map; 2757ec38b344SPoul-Henning Kamp vm_map_entry_t cur; 275865d8409cSRebecca Cran vm_object_t object; 2759f425ab8eSKonstantin Belousov struct swblk *sb; 2760f425ab8eSKonstantin Belousov vm_pindex_t e, pi; 2761f425ab8eSKonstantin Belousov long count; 2762f425ab8eSKonstantin Belousov int i; 276365d8409cSRebecca Cran 276465d8409cSRebecca Cran map = &vmspace->vm_map; 276565d8409cSRebecca Cran count = 0; 2766ec38b344SPoul-Henning Kamp 27672288078cSDoug Moore VM_MAP_ENTRY_FOREACH(cur, map) { 2768f425ab8eSKonstantin Belousov if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) != 0) 2769f425ab8eSKonstantin Belousov continue; 2770f425ab8eSKonstantin Belousov object = cur->object.vm_object; 27714b8365d7SKonstantin Belousov if (object == NULL || (object->flags & OBJ_SWAP) == 0) 2772f425ab8eSKonstantin Belousov continue; 2773f425ab8eSKonstantin Belousov VM_OBJECT_RLOCK(object); 27744b8365d7SKonstantin Belousov if ((object->flags & OBJ_SWAP) == 0) 2775f425ab8eSKonstantin Belousov goto unlock; 2776f425ab8eSKonstantin Belousov pi = OFF_TO_IDX(cur->offset); 2777f425ab8eSKonstantin Belousov e = pi + OFF_TO_IDX(cur->end - cur->start); 2778f425ab8eSKonstantin Belousov for (;; pi = sb->p + SWAP_META_PAGES) { 2779f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP_GE( 2780f425ab8eSKonstantin Belousov &object->un_pager.swp.swp_blks, pi); 2781f425ab8eSKonstantin Belousov if (sb == NULL || sb->p >= e) 2782f425ab8eSKonstantin Belousov break; 2783f425ab8eSKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) { 2784f425ab8eSKonstantin Belousov if (sb->p + i < e && 2785f425ab8eSKonstantin Belousov sb->d[i] != SWAPBLK_NONE) 2786f425ab8eSKonstantin Belousov count++; 2787ec38b344SPoul-Henning Kamp } 2788ec38b344SPoul-Henning Kamp } 2789f425ab8eSKonstantin Belousov unlock: 2790f425ab8eSKonstantin Belousov VM_OBJECT_RUNLOCK(object); 2791ec38b344SPoul-Henning Kamp } 2792ec38b344SPoul-Henning Kamp return (count); 2793ec38b344SPoul-Henning Kamp } 2794dee34ca4SPoul-Henning Kamp 2795dee34ca4SPoul-Henning Kamp /* 2796dee34ca4SPoul-Henning Kamp * GEOM backend 2797dee34ca4SPoul-Henning Kamp * 2798dee34ca4SPoul-Henning Kamp * Swapping onto disk devices. 2799dee34ca4SPoul-Henning Kamp * 2800dee34ca4SPoul-Henning Kamp */ 2801dee34ca4SPoul-Henning Kamp 28025721c9c7SPoul-Henning Kamp static g_orphan_t swapgeom_orphan; 28035721c9c7SPoul-Henning Kamp 2804dee34ca4SPoul-Henning Kamp static struct g_class g_swap_class = { 2805dee34ca4SPoul-Henning Kamp .name = "SWAP", 28065721c9c7SPoul-Henning Kamp .version = G_VERSION, 28075721c9c7SPoul-Henning Kamp .orphan = swapgeom_orphan, 2808dee34ca4SPoul-Henning Kamp }; 2809dee34ca4SPoul-Henning Kamp 2810dee34ca4SPoul-Henning Kamp DECLARE_GEOM_CLASS(g_swap_class, g_class); 2811dee34ca4SPoul-Henning Kamp 2812dee34ca4SPoul-Henning Kamp static void 28133398491bSAlexander Motin swapgeom_close_ev(void *arg, int flags) 28143398491bSAlexander Motin { 28153398491bSAlexander Motin struct g_consumer *cp; 28163398491bSAlexander Motin 28173398491bSAlexander Motin cp = arg; 28183398491bSAlexander Motin g_access(cp, -1, -1, 0); 28193398491bSAlexander Motin g_detach(cp); 28203398491bSAlexander Motin g_destroy_consumer(cp); 28213398491bSAlexander Motin } 28223398491bSAlexander Motin 28239e3e3fe5SWarner Losh /* 28249e3e3fe5SWarner Losh * Add a reference to the g_consumer for an inflight transaction. 28259e3e3fe5SWarner Losh */ 28269e3e3fe5SWarner Losh static void 28279e3e3fe5SWarner Losh swapgeom_acquire(struct g_consumer *cp) 28289e3e3fe5SWarner Losh { 28299e3e3fe5SWarner Losh 28309e3e3fe5SWarner Losh mtx_assert(&sw_dev_mtx, MA_OWNED); 28319e3e3fe5SWarner Losh cp->index++; 28329e3e3fe5SWarner Losh } 28339e3e3fe5SWarner Losh 28349e3e3fe5SWarner Losh /* 28350c657d22SKonstantin Belousov * Remove a reference from the g_consumer. Post a close event if all 28360c657d22SKonstantin Belousov * references go away, since the function might be called from the 28370c657d22SKonstantin Belousov * biodone context. 28389e3e3fe5SWarner Losh */ 28399e3e3fe5SWarner Losh static void 28409e3e3fe5SWarner Losh swapgeom_release(struct g_consumer *cp, struct swdevt *sp) 28419e3e3fe5SWarner Losh { 28429e3e3fe5SWarner Losh 28439e3e3fe5SWarner Losh mtx_assert(&sw_dev_mtx, MA_OWNED); 28449e3e3fe5SWarner Losh cp->index--; 28459e3e3fe5SWarner Losh if (cp->index == 0) { 28469e3e3fe5SWarner Losh if (g_post_event(swapgeom_close_ev, cp, M_NOWAIT, NULL) == 0) 28479e3e3fe5SWarner Losh sp->sw_id = NULL; 28489e3e3fe5SWarner Losh } 28499e3e3fe5SWarner Losh } 28509e3e3fe5SWarner Losh 28513398491bSAlexander Motin static void 2852dee34ca4SPoul-Henning Kamp swapgeom_done(struct bio *bp2) 2853dee34ca4SPoul-Henning Kamp { 28543398491bSAlexander Motin struct swdevt *sp; 2855dee34ca4SPoul-Henning Kamp struct buf *bp; 28563398491bSAlexander Motin struct g_consumer *cp; 2857dee34ca4SPoul-Henning Kamp 2858dee34ca4SPoul-Henning Kamp bp = bp2->bio_caller2; 28593398491bSAlexander Motin cp = bp2->bio_from; 2860c5d3d25eSPoul-Henning Kamp bp->b_ioflags = bp2->bio_flags; 2861dee34ca4SPoul-Henning Kamp if (bp2->bio_error) 2862dee34ca4SPoul-Henning Kamp bp->b_ioflags |= BIO_ERROR; 2863c5d3d25eSPoul-Henning Kamp bp->b_resid = bp->b_bcount - bp2->bio_completed; 2864c5d3d25eSPoul-Henning Kamp bp->b_error = bp2->bio_error; 2865756a5412SGleb Smirnoff bp->b_caller1 = NULL; 2866dee34ca4SPoul-Henning Kamp bufdone(bp); 28673398491bSAlexander Motin sp = bp2->bio_caller1; 28689e3e3fe5SWarner Losh mtx_lock(&sw_dev_mtx); 28699e3e3fe5SWarner Losh swapgeom_release(cp, sp); 28703398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 2871dee34ca4SPoul-Henning Kamp g_destroy_bio(bp2); 2872dee34ca4SPoul-Henning Kamp } 2873dee34ca4SPoul-Henning Kamp 2874dee34ca4SPoul-Henning Kamp static void 2875dee34ca4SPoul-Henning Kamp swapgeom_strategy(struct buf *bp, struct swdevt *sp) 2876dee34ca4SPoul-Henning Kamp { 2877dee34ca4SPoul-Henning Kamp struct bio *bio; 2878dee34ca4SPoul-Henning Kamp struct g_consumer *cp; 2879dee34ca4SPoul-Henning Kamp 28803398491bSAlexander Motin mtx_lock(&sw_dev_mtx); 2881dee34ca4SPoul-Henning Kamp cp = sp->sw_id; 2882dee34ca4SPoul-Henning Kamp if (cp == NULL) { 28833398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 2884dee34ca4SPoul-Henning Kamp bp->b_error = ENXIO; 2885dee34ca4SPoul-Henning Kamp bp->b_ioflags |= BIO_ERROR; 2886dee34ca4SPoul-Henning Kamp bufdone(bp); 2887dee34ca4SPoul-Henning Kamp return; 2888dee34ca4SPoul-Henning Kamp } 28899e3e3fe5SWarner Losh swapgeom_acquire(cp); 28903398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 289111041003SKonstantin Belousov if (bp->b_iocmd == BIO_WRITE) 289211041003SKonstantin Belousov bio = g_new_bio(); 289311041003SKonstantin Belousov else 28944f8205e5SPoul-Henning Kamp bio = g_alloc_bio(); 28954f8205e5SPoul-Henning Kamp if (bio == NULL) { 28969e3e3fe5SWarner Losh mtx_lock(&sw_dev_mtx); 28979e3e3fe5SWarner Losh swapgeom_release(cp, sp); 28989e3e3fe5SWarner Losh mtx_unlock(&sw_dev_mtx); 28993e5b6861SPoul-Henning Kamp bp->b_error = ENOMEM; 29003e5b6861SPoul-Henning Kamp bp->b_ioflags |= BIO_ERROR; 29010b208315SEdward Tomasz Napierala printf("swap_pager: cannot allocate bio\n"); 29023e5b6861SPoul-Henning Kamp bufdone(bp); 29033e5b6861SPoul-Henning Kamp return; 29043e5b6861SPoul-Henning Kamp } 290511041003SKonstantin Belousov 2906756a5412SGleb Smirnoff bp->b_caller1 = bio; 29073398491bSAlexander Motin bio->bio_caller1 = sp; 2908dee34ca4SPoul-Henning Kamp bio->bio_caller2 = bp; 2909c5d3d25eSPoul-Henning Kamp bio->bio_cmd = bp->b_iocmd; 2910dee34ca4SPoul-Henning Kamp bio->bio_offset = (bp->b_blkno - sp->sw_first) * PAGE_SIZE; 2911dee34ca4SPoul-Henning Kamp bio->bio_length = bp->b_bcount; 2912dee34ca4SPoul-Henning Kamp bio->bio_done = swapgeom_done; 2913c6213befSGleb Smirnoff bio->bio_flags |= BIO_SWAP; 2914fade8dd7SJeff Roberson if (!buf_mapped(bp)) { 29152cc718a1SKonstantin Belousov bio->bio_ma = bp->b_pages; 29162cc718a1SKonstantin Belousov bio->bio_data = unmapped_buf; 29172cc718a1SKonstantin Belousov bio->bio_ma_offset = (vm_offset_t)bp->b_offset & PAGE_MASK; 29182cc718a1SKonstantin Belousov bio->bio_ma_n = bp->b_npages; 29192cc718a1SKonstantin Belousov bio->bio_flags |= BIO_UNMAPPED; 29202cc718a1SKonstantin Belousov } else { 29212cc718a1SKonstantin Belousov bio->bio_data = bp->b_data; 29222cc718a1SKonstantin Belousov bio->bio_ma = NULL; 29232cc718a1SKonstantin Belousov } 2924dee34ca4SPoul-Henning Kamp g_io_request(bio, cp); 2925dee34ca4SPoul-Henning Kamp return; 2926dee34ca4SPoul-Henning Kamp } 2927dee34ca4SPoul-Henning Kamp 2928dee34ca4SPoul-Henning Kamp static void 2929dee34ca4SPoul-Henning Kamp swapgeom_orphan(struct g_consumer *cp) 2930dee34ca4SPoul-Henning Kamp { 2931dee34ca4SPoul-Henning Kamp struct swdevt *sp; 29323398491bSAlexander Motin int destroy; 2933dee34ca4SPoul-Henning Kamp 2934dee34ca4SPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 29353398491bSAlexander Motin TAILQ_FOREACH(sp, &swtailq, sw_list) { 29363398491bSAlexander Motin if (sp->sw_id == cp) { 29378f12d83aSAlexander Motin sp->sw_flags |= SW_CLOSING; 29383398491bSAlexander Motin break; 2939dee34ca4SPoul-Henning Kamp } 29403398491bSAlexander Motin } 29419e3e3fe5SWarner Losh /* 29429e3e3fe5SWarner Losh * Drop reference we were created with. Do directly since we're in a 29439e3e3fe5SWarner Losh * special context where we don't have to queue the call to 29449e3e3fe5SWarner Losh * swapgeom_close_ev(). 29459e3e3fe5SWarner Losh */ 29469e3e3fe5SWarner Losh cp->index--; 29473398491bSAlexander Motin destroy = ((sp != NULL) && (cp->index == 0)); 29483398491bSAlexander Motin if (destroy) 29493398491bSAlexander Motin sp->sw_id = NULL; 29503398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 29513398491bSAlexander Motin if (destroy) 29523398491bSAlexander Motin swapgeom_close_ev(cp, 0); 2953dee34ca4SPoul-Henning Kamp } 2954dee34ca4SPoul-Henning Kamp 2955dee34ca4SPoul-Henning Kamp static void 2956dee34ca4SPoul-Henning Kamp swapgeom_close(struct thread *td, struct swdevt *sw) 2957dee34ca4SPoul-Henning Kamp { 29583398491bSAlexander Motin struct g_consumer *cp; 2959dee34ca4SPoul-Henning Kamp 29603398491bSAlexander Motin mtx_lock(&sw_dev_mtx); 29613398491bSAlexander Motin cp = sw->sw_id; 29623398491bSAlexander Motin sw->sw_id = NULL; 29633398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 29640c657d22SKonstantin Belousov 29650c657d22SKonstantin Belousov /* 29660c657d22SKonstantin Belousov * swapgeom_close() may be called from the biodone context, 29670c657d22SKonstantin Belousov * where we cannot perform topology changes. Delegate the 29680c657d22SKonstantin Belousov * work to the events thread. 29690c657d22SKonstantin Belousov */ 29703398491bSAlexander Motin if (cp != NULL) 29713398491bSAlexander Motin g_waitfor_event(swapgeom_close_ev, cp, M_WAITOK, NULL); 2972dee34ca4SPoul-Henning Kamp } 2973dee34ca4SPoul-Henning Kamp 297488ad2d7bSKonstantin Belousov static int 297588ad2d7bSKonstantin Belousov swapongeom_locked(struct cdev *dev, struct vnode *vp) 2976dee34ca4SPoul-Henning Kamp { 2977dee34ca4SPoul-Henning Kamp struct g_provider *pp; 2978dee34ca4SPoul-Henning Kamp struct g_consumer *cp; 2979dee34ca4SPoul-Henning Kamp static struct g_geom *gp; 2980dee34ca4SPoul-Henning Kamp struct swdevt *sp; 2981dee34ca4SPoul-Henning Kamp u_long nblks; 2982dee34ca4SPoul-Henning Kamp int error; 2983dee34ca4SPoul-Henning Kamp 298488ad2d7bSKonstantin Belousov pp = g_dev_getprovider(dev); 298588ad2d7bSKonstantin Belousov if (pp == NULL) 298688ad2d7bSKonstantin Belousov return (ENODEV); 2987dee34ca4SPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 2988dee34ca4SPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 2989dee34ca4SPoul-Henning Kamp cp = sp->sw_id; 2990dee34ca4SPoul-Henning Kamp if (cp != NULL && cp->provider == pp) { 2991dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 299288ad2d7bSKonstantin Belousov return (EBUSY); 2993dee34ca4SPoul-Henning Kamp } 2994dee34ca4SPoul-Henning Kamp } 2995dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 29965721c9c7SPoul-Henning Kamp if (gp == NULL) 299702c62349SJaakko Heinonen gp = g_new_geomf(&g_swap_class, "swap"); 2998dee34ca4SPoul-Henning Kamp cp = g_new_consumer(gp); 29999e3e3fe5SWarner Losh cp->index = 1; /* Number of active I/Os, plus one for being active. */ 30009e3e3fe5SWarner Losh cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 3001dee34ca4SPoul-Henning Kamp g_attach(cp, pp); 3002afeb65e6SPoul-Henning Kamp /* 3003afeb65e6SPoul-Henning Kamp * XXX: Every time you think you can improve the margin for 3004afeb65e6SPoul-Henning Kamp * footshooting, somebody depends on the ability to do so: 3005afeb65e6SPoul-Henning Kamp * savecore(8) wants to write to our swapdev so we cannot 3006afeb65e6SPoul-Henning Kamp * set an exclusive count :-( 3007afeb65e6SPoul-Henning Kamp */ 3008d2bae332SPoul-Henning Kamp error = g_access(cp, 1, 1, 0); 300988ad2d7bSKonstantin Belousov if (error != 0) { 3010dee34ca4SPoul-Henning Kamp g_detach(cp); 3011dee34ca4SPoul-Henning Kamp g_destroy_consumer(cp); 301288ad2d7bSKonstantin Belousov return (error); 3013dee34ca4SPoul-Henning Kamp } 3014dee34ca4SPoul-Henning Kamp nblks = pp->mediasize / DEV_BSIZE; 301588ad2d7bSKonstantin Belousov swaponsomething(vp, cp, nblks, swapgeom_strategy, 301688ad2d7bSKonstantin Belousov swapgeom_close, dev2udev(dev), 30172cc718a1SKonstantin Belousov (pp->flags & G_PF_ACCEPT_UNMAPPED) != 0 ? SW_UNMAPPED : 0); 301888ad2d7bSKonstantin Belousov return (0); 3019dee34ca4SPoul-Henning Kamp } 3020dee34ca4SPoul-Henning Kamp 3021dee34ca4SPoul-Henning Kamp static int 302288ad2d7bSKonstantin Belousov swapongeom(struct vnode *vp) 3023dee34ca4SPoul-Henning Kamp { 3024dee34ca4SPoul-Henning Kamp int error; 3025dee34ca4SPoul-Henning Kamp 30266ddf41faSKonstantin Belousov ASSERT_VOP_ELOCKED(vp, "swapongeom"); 3027abd80ddbSMateusz Guzik if (vp->v_type != VCHR || VN_IS_DOOMED(vp)) { 302888ad2d7bSKonstantin Belousov error = ENOENT; 302988ad2d7bSKonstantin Belousov } else { 303088ad2d7bSKonstantin Belousov g_topology_lock(); 303188ad2d7bSKonstantin Belousov error = swapongeom_locked(vp->v_rdev, vp); 303288ad2d7bSKonstantin Belousov g_topology_unlock(); 303388ad2d7bSKonstantin Belousov } 3034dee34ca4SPoul-Henning Kamp return (error); 3035dee34ca4SPoul-Henning Kamp } 3036dee34ca4SPoul-Henning Kamp 3037dee34ca4SPoul-Henning Kamp /* 3038dee34ca4SPoul-Henning Kamp * VNODE backend 3039dee34ca4SPoul-Henning Kamp * 3040dee34ca4SPoul-Henning Kamp * This is used mainly for network filesystem (read: probably only tested 3041dee34ca4SPoul-Henning Kamp * with NFS) swapfiles. 3042dee34ca4SPoul-Henning Kamp * 3043dee34ca4SPoul-Henning Kamp */ 3044dee34ca4SPoul-Henning Kamp 3045dee34ca4SPoul-Henning Kamp static void 3046dee34ca4SPoul-Henning Kamp swapdev_strategy(struct buf *bp, struct swdevt *sp) 3047dee34ca4SPoul-Henning Kamp { 3048494eb176SPoul-Henning Kamp struct vnode *vp2; 3049dee34ca4SPoul-Henning Kamp 3050dee34ca4SPoul-Henning Kamp bp->b_blkno = ctodb(bp->b_blkno - sp->sw_first); 3051dee34ca4SPoul-Henning Kamp 3052dee34ca4SPoul-Henning Kamp vp2 = sp->sw_id; 3053dee34ca4SPoul-Henning Kamp vhold(vp2); 3054dee34ca4SPoul-Henning Kamp if (bp->b_iocmd == BIO_WRITE) { 3055b19740f4SKonstantin Belousov vn_lock(vp2, LK_EXCLUSIVE | LK_RETRY); 30563cfc7651SOlivier Houchard if (bp->b_bufobj) 3057494eb176SPoul-Henning Kamp bufobj_wdrop(bp->b_bufobj); 3058a76d8f4eSPoul-Henning Kamp bufobj_wref(&vp2->v_bufobj); 3059b19740f4SKonstantin Belousov } else { 3060b19740f4SKonstantin Belousov vn_lock(vp2, LK_SHARED | LK_RETRY); 3061dee34ca4SPoul-Henning Kamp } 30623cfc7651SOlivier Houchard if (bp->b_bufobj != &vp2->v_bufobj) 30633cfc7651SOlivier Houchard bp->b_bufobj = &vp2->v_bufobj; 3064dee34ca4SPoul-Henning Kamp bp->b_vp = vp2; 30652c18019fSPoul-Henning Kamp bp->b_iooffset = dbtob(bp->b_blkno); 3066b792bebeSPoul-Henning Kamp bstrategy(bp); 3067b19740f4SKonstantin Belousov VOP_UNLOCK(vp2); 3068dee34ca4SPoul-Henning Kamp } 3069dee34ca4SPoul-Henning Kamp 3070dee34ca4SPoul-Henning Kamp static void 3071dee34ca4SPoul-Henning Kamp swapdev_close(struct thread *td, struct swdevt *sp) 3072dee34ca4SPoul-Henning Kamp { 3073a6d04f34SKonstantin Belousov struct vnode *vp; 3074dee34ca4SPoul-Henning Kamp 3075a6d04f34SKonstantin Belousov vp = sp->sw_vp; 3076a6d04f34SKonstantin Belousov vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 3077a6d04f34SKonstantin Belousov VOP_CLOSE(vp, FREAD | FWRITE, td->td_ucred, td); 3078a6d04f34SKonstantin Belousov vput(vp); 3079dee34ca4SPoul-Henning Kamp } 3080dee34ca4SPoul-Henning Kamp 3081dee34ca4SPoul-Henning Kamp static int 3082dee34ca4SPoul-Henning Kamp swaponvp(struct thread *td, struct vnode *vp, u_long nblks) 3083dee34ca4SPoul-Henning Kamp { 3084dee34ca4SPoul-Henning Kamp struct swdevt *sp; 3085dee34ca4SPoul-Henning Kamp int error; 3086dee34ca4SPoul-Henning Kamp 30876ddf41faSKonstantin Belousov ASSERT_VOP_ELOCKED(vp, "swaponvp"); 3088dee34ca4SPoul-Henning Kamp if (nblks == 0) 3089dee34ca4SPoul-Henning Kamp return (ENXIO); 3090dee34ca4SPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 3091dee34ca4SPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 3092dee34ca4SPoul-Henning Kamp if (sp->sw_id == vp) { 3093dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 3094dee34ca4SPoul-Henning Kamp return (EBUSY); 3095dee34ca4SPoul-Henning Kamp } 3096dee34ca4SPoul-Henning Kamp } 3097dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 3098dee34ca4SPoul-Henning Kamp 3099dee34ca4SPoul-Henning Kamp #ifdef MAC 310030d239bcSRobert Watson error = mac_system_check_swapon(td->td_ucred, vp); 3101dee34ca4SPoul-Henning Kamp if (error == 0) 3102dee34ca4SPoul-Henning Kamp #endif 31039e223287SKonstantin Belousov error = VOP_OPEN(vp, FREAD | FWRITE, td->td_ucred, td, NULL); 31046ddf41faSKonstantin Belousov if (error != 0) 3105dee34ca4SPoul-Henning Kamp return (error); 3106dee34ca4SPoul-Henning Kamp 3107dee34ca4SPoul-Henning Kamp swaponsomething(vp, vp, nblks, swapdev_strategy, swapdev_close, 31082cc718a1SKonstantin Belousov NODEV, 0); 3109dee34ca4SPoul-Henning Kamp return (0); 3110dee34ca4SPoul-Henning Kamp } 311189c241d1SGleb Smirnoff 311289c241d1SGleb Smirnoff static int 311389c241d1SGleb Smirnoff sysctl_swap_async_max(SYSCTL_HANDLER_ARGS) 311489c241d1SGleb Smirnoff { 311589c241d1SGleb Smirnoff int error, new, n; 311689c241d1SGleb Smirnoff 311789c241d1SGleb Smirnoff new = nsw_wcount_async_max; 311889c241d1SGleb Smirnoff error = sysctl_handle_int(oidp, &new, 0, req); 311989c241d1SGleb Smirnoff if (error != 0 || req->newptr == NULL) 312089c241d1SGleb Smirnoff return (error); 312189c241d1SGleb Smirnoff 312289c241d1SGleb Smirnoff if (new > nswbuf / 2 || new < 1) 312389c241d1SGleb Smirnoff return (EINVAL); 312489c241d1SGleb Smirnoff 3125756a5412SGleb Smirnoff mtx_lock(&swbuf_mtx); 312689c241d1SGleb Smirnoff while (nsw_wcount_async_max != new) { 312789c241d1SGleb Smirnoff /* 312889c241d1SGleb Smirnoff * Adjust difference. If the current async count is too low, 312989c241d1SGleb Smirnoff * we will need to sqeeze our update slowly in. Sleep with a 313089c241d1SGleb Smirnoff * higher priority than getpbuf() to finish faster. 313189c241d1SGleb Smirnoff */ 313289c241d1SGleb Smirnoff n = new - nsw_wcount_async_max; 313389c241d1SGleb Smirnoff if (nsw_wcount_async + n >= 0) { 313489c241d1SGleb Smirnoff nsw_wcount_async += n; 313589c241d1SGleb Smirnoff nsw_wcount_async_max += n; 313689c241d1SGleb Smirnoff wakeup(&nsw_wcount_async); 313789c241d1SGleb Smirnoff } else { 313889c241d1SGleb Smirnoff nsw_wcount_async_max -= nsw_wcount_async; 313989c241d1SGleb Smirnoff nsw_wcount_async = 0; 3140756a5412SGleb Smirnoff msleep(&nsw_wcount_async, &swbuf_mtx, PSWP, 314189c241d1SGleb Smirnoff "swpsysctl", 0); 314289c241d1SGleb Smirnoff } 314389c241d1SGleb Smirnoff } 3144756a5412SGleb Smirnoff mtx_unlock(&swbuf_mtx); 314589c241d1SGleb Smirnoff 314689c241d1SGleb Smirnoff return (0); 314789c241d1SGleb Smirnoff } 3148fe7bcbafSKyle Evans 3149fe7bcbafSKyle Evans static void 3150fe7bcbafSKyle Evans swap_pager_update_writecount(vm_object_t object, vm_offset_t start, 3151fe7bcbafSKyle Evans vm_offset_t end) 3152fe7bcbafSKyle Evans { 3153fe7bcbafSKyle Evans 3154fe7bcbafSKyle Evans VM_OBJECT_WLOCK(object); 315563967687SJeff Roberson KASSERT((object->flags & OBJ_ANON) == 0, 3156fe7bcbafSKyle Evans ("Splittable object with writecount")); 3157fe7bcbafSKyle Evans object->un_pager.swp.writemappings += (vm_ooffset_t)end - start; 3158fe7bcbafSKyle Evans VM_OBJECT_WUNLOCK(object); 3159fe7bcbafSKyle Evans } 3160fe7bcbafSKyle Evans 3161fe7bcbafSKyle Evans static void 3162fe7bcbafSKyle Evans swap_pager_release_writecount(vm_object_t object, vm_offset_t start, 3163fe7bcbafSKyle Evans vm_offset_t end) 3164fe7bcbafSKyle Evans { 3165fe7bcbafSKyle Evans 3166fe7bcbafSKyle Evans VM_OBJECT_WLOCK(object); 316763967687SJeff Roberson KASSERT((object->flags & OBJ_ANON) == 0, 3168fe7bcbafSKyle Evans ("Splittable object with writecount")); 3169fe7bcbafSKyle Evans object->un_pager.swp.writemappings -= (vm_ooffset_t)end - start; 3170fe7bcbafSKyle Evans VM_OBJECT_WUNLOCK(object); 3171fe7bcbafSKyle Evans } 3172