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 68874651b1SDavid E. O'Brien #include <sys/cdefs.h> 69e9c0cc15SPoul-Henning Kamp #include "opt_vm.h" 70e9c0cc15SPoul-Henning Kamp 71df8bae1dSRodney W. Grimes #include <sys/param.h> 729626b608SPoul-Henning Kamp #include <sys/bio.h> 73e2e050c8SConrad Meyer #include <sys/blist.h> 74df8bae1dSRodney W. Grimes #include <sys/buf.h> 75e2e050c8SConrad Meyer #include <sys/conf.h> 76e9c0cc15SPoul-Henning Kamp #include <sys/disk.h> 77504f5e29SDoug Moore #include <sys/disklabel.h> 78e2e050c8SConrad Meyer #include <sys/eventhandler.h> 79e9c0cc15SPoul-Henning Kamp #include <sys/fcntl.h> 80686aa928SMark Johnston #include <sys/limits.h> 81e2e050c8SConrad Meyer #include <sys/lock.h> 82e2e050c8SConrad Meyer #include <sys/kernel.h> 83e9c0cc15SPoul-Henning Kamp #include <sys/mount.h> 84e9c0cc15SPoul-Henning Kamp #include <sys/namei.h> 85df8bae1dSRodney W. Grimes #include <sys/malloc.h> 86f425ab8eSKonstantin Belousov #include <sys/pctrie.h> 87e2e050c8SConrad Meyer #include <sys/priv.h> 88e2e050c8SConrad Meyer #include <sys/proc.h> 891ba5ad42SEdward Tomasz Napierala #include <sys/racct.h> 903364c323SKonstantin Belousov #include <sys/resource.h> 913364c323SKonstantin Belousov #include <sys/resourcevar.h> 9289f6b863SAttilio Rao #include <sys/rwlock.h> 93d027ed2eSAlan Cox #include <sys/sbuf.h> 94327f4e83SMatthew Dillon #include <sys/sysctl.h> 95e9c0cc15SPoul-Henning Kamp #include <sys/sysproto.h> 96e2e050c8SConrad Meyer #include <sys/systm.h> 970cddd8f0SMatthew Dillon #include <sys/sx.h> 9853465702SKonstantin Belousov #include <sys/unistd.h> 9900a3fe96SKonstantin Belousov #include <sys/user.h> 100936524aaSMatthew Dillon #include <sys/vmmeter.h> 101e2e050c8SConrad Meyer #include <sys/vnode.h> 102df8bae1dSRodney W. Grimes 103aed55708SRobert Watson #include <security/mac/mac_framework.h> 104aed55708SRobert Watson 105df8bae1dSRodney W. Grimes #include <vm/vm.h> 10621cd6e62SSeigo Tanimura #include <vm/pmap.h> 10721cd6e62SSeigo Tanimura #include <vm/vm_map.h> 10821cd6e62SSeigo Tanimura #include <vm/vm_kern.h> 109efeaf95aSDavid Greenman #include <vm/vm_object.h> 110df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 111efeaf95aSDavid Greenman #include <vm/vm_pager.h> 112df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h> 113e9c0cc15SPoul-Henning Kamp #include <vm/vm_param.h> 114df8bae1dSRodney W. Grimes #include <vm/swap_pager.h> 115efeaf95aSDavid Greenman #include <vm/vm_extern.h> 116670d17b5SJeff Roberson #include <vm/uma.h> 117df8bae1dSRodney W. Grimes 118dee34ca4SPoul-Henning Kamp #include <geom/geom.h> 119dee34ca4SPoul-Henning Kamp 120ec38b344SPoul-Henning Kamp /* 121064650c1SAlan Cox * MAX_PAGEOUT_CLUSTER must be a power of 2 between 1 and 64. 122064650c1SAlan Cox * The 64-page limit is due to the radix code (kern/subr_blist.c). 123ec38b344SPoul-Henning Kamp */ 124ec38b344SPoul-Henning Kamp #ifndef MAX_PAGEOUT_CLUSTER 125e2241590SAlan Cox #define MAX_PAGEOUT_CLUSTER 32 126ec38b344SPoul-Henning Kamp #endif 127ec38b344SPoul-Henning Kamp 128ec38b344SPoul-Henning Kamp #if !defined(SWB_NPAGES) 129ec38b344SPoul-Henning Kamp #define SWB_NPAGES MAX_PAGEOUT_CLUSTER 130ec38b344SPoul-Henning Kamp #endif 131ec38b344SPoul-Henning Kamp 132f425ab8eSKonstantin Belousov #define SWAP_META_PAGES PCTRIE_COUNT 133ec38b344SPoul-Henning Kamp 134f425ab8eSKonstantin Belousov /* 135f425ab8eSKonstantin Belousov * A swblk structure maps each page index within a 136f425ab8eSKonstantin Belousov * SWAP_META_PAGES-aligned and sized range to the address of an 137f425ab8eSKonstantin Belousov * on-disk swap block (or SWAPBLK_NONE). The collection of these 138f425ab8eSKonstantin Belousov * mappings for an entire vm object is implemented as a pc-trie. 139f425ab8eSKonstantin Belousov */ 140f425ab8eSKonstantin Belousov struct swblk { 141f425ab8eSKonstantin Belousov vm_pindex_t p; 142f425ab8eSKonstantin Belousov daddr_t d[SWAP_META_PAGES]; 143e9c0cc15SPoul-Henning Kamp }; 144e9c0cc15SPoul-Henning Kamp 145a880104aSDoug Moore /* 146a880104aSDoug Moore * A page_range structure records the start address and length of a sequence of 147a880104aSDoug Moore * mapped page addresses. 148a880104aSDoug Moore */ 149a880104aSDoug Moore struct page_range { 150a880104aSDoug Moore daddr_t start; 151a880104aSDoug Moore daddr_t num; 152a880104aSDoug Moore }; 153a880104aSDoug Moore 1542c4992dbSAlan Cox static MALLOC_DEFINE(M_VMPGDATA, "vm_pgdata", "swap pager private data"); 15520da9c2eSPoul-Henning Kamp static struct mtx sw_dev_mtx; 1568f60c087SPoul-Henning Kamp static TAILQ_HEAD(, swdevt) swtailq = TAILQ_HEAD_INITIALIZER(swtailq); 1578f60c087SPoul-Henning Kamp static struct swdevt *swdevhd; /* Allocate from here next */ 1588f60c087SPoul-Henning Kamp static int nswapdev; /* Number of swap devices */ 1598f60c087SPoul-Henning Kamp int swap_pager_avail; 16004533e1eSKonstantin Belousov static struct sx swdev_syscall_lock; /* serialize swap(on|off) */ 161e9c0cc15SPoul-Henning Kamp 162126a2470SMateusz Guzik static __exclusive_cache_line u_long swap_reserved; 163e8bb589dSMatt Macy static u_long swap_total; 164e8bb589dSMatt Macy static int sysctl_page_shift(SYSCTL_HANDLER_ARGS); 165a8081778SJeff Roberson 1667029da5cSPawel Biernacki static SYSCTL_NODE(_vm_stats, OID_AUTO, swap, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 1677029da5cSPawel Biernacki "VM swap stats"); 168a8081778SJeff Roberson 169e8bb589dSMatt Macy SYSCTL_PROC(_vm, OID_AUTO, swap_reserved, CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE, 170567378ccSEnji Cooper &swap_reserved, 0, sysctl_page_shift, "QU", 1718a9c731fSIvan Voras "Amount of swap storage needed to back all allocated anonymous memory."); 172e8bb589dSMatt Macy SYSCTL_PROC(_vm, OID_AUTO, swap_total, CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE, 173567378ccSEnji Cooper &swap_total, 0, sysctl_page_shift, "QU", 174e8bb589dSMatt Macy "Total amount of available swap storage."); 175e8bb589dSMatt Macy 176ccdaa1abSKonstantin Belousov int vm_overcommit __read_mostly = 0; 177a6cc4c6eSKonstantin Belousov SYSCTL_INT(_vm, VM_OVERCOMMIT, overcommit, CTLFLAG_RW, &vm_overcommit, 0, 1788a9c731fSIvan Voras "Configure virtual memory overcommit behavior. See tuning(7) " 1798a9c731fSIvan Voras "for details."); 18061203277SDag-Erling Smørgrav static unsigned long swzone; 18161203277SDag-Erling Smørgrav SYSCTL_ULONG(_vm, OID_AUTO, swzone, CTLFLAG_RD, &swzone, 0, 18261203277SDag-Erling Smørgrav "Actual size of swap metadata zone"); 18361203277SDag-Erling Smørgrav static unsigned long swap_maxpages; 18461203277SDag-Erling Smørgrav SYSCTL_ULONG(_vm, OID_AUTO, swap_maxpages, CTLFLAG_RD, &swap_maxpages, 0, 18561203277SDag-Erling Smørgrav "Maximum amount of swap supported"); 1863364c323SKonstantin Belousov 187d869a17eSMark Johnston static COUNTER_U64_DEFINE_EARLY(swap_free_deferred); 188a8081778SJeff Roberson SYSCTL_COUNTER_U64(_vm_stats_swap, OID_AUTO, free_deferred, 189a8081778SJeff Roberson CTLFLAG_RD, &swap_free_deferred, 190a8081778SJeff Roberson "Number of pages that deferred freeing swap space"); 191a8081778SJeff Roberson 192d869a17eSMark Johnston static COUNTER_U64_DEFINE_EARLY(swap_free_completed); 193a8081778SJeff Roberson SYSCTL_COUNTER_U64(_vm_stats_swap, OID_AUTO, free_completed, 194a8081778SJeff Roberson CTLFLAG_RD, &swap_free_completed, 195a8081778SJeff Roberson "Number of deferred frees completed"); 196a8081778SJeff Roberson 197e8bb589dSMatt Macy static int 198e8bb589dSMatt Macy sysctl_page_shift(SYSCTL_HANDLER_ARGS) 199e8bb589dSMatt Macy { 200e8bb589dSMatt Macy uint64_t newval; 201e8bb589dSMatt Macy u_long value = *(u_long *)arg1; 202e8bb589dSMatt Macy 203e8bb589dSMatt Macy newval = ((uint64_t)value) << PAGE_SHIFT; 204e8bb589dSMatt Macy return (sysctl_handle_64(oidp, &newval, 0, req)); 205e8bb589dSMatt Macy } 206e8bb589dSMatt Macy 207ee744122SMateusz Guzik static bool 208ee744122SMateusz Guzik swap_reserve_by_cred_rlimit(u_long pincr, struct ucred *cred, int oc) 209ee744122SMateusz Guzik { 210ee744122SMateusz Guzik struct uidinfo *uip; 211ee744122SMateusz Guzik u_long prev; 212ee744122SMateusz Guzik 213ee744122SMateusz Guzik uip = cred->cr_ruidinfo; 214ee744122SMateusz Guzik 215ee744122SMateusz Guzik prev = atomic_fetchadd_long(&uip->ui_vmsize, pincr); 216ee744122SMateusz Guzik if ((oc & SWAP_RESERVE_RLIMIT_ON) != 0 && 217ee744122SMateusz Guzik prev + pincr > lim_cur(curthread, RLIMIT_SWAP) && 218ee744122SMateusz Guzik priv_check(curthread, PRIV_VM_SWAP_NORLIMIT) != 0) { 219ee744122SMateusz Guzik prev = atomic_fetchadd_long(&uip->ui_vmsize, -pincr); 22026eed2aaSKonstantin Belousov KASSERT(prev >= pincr, 22126eed2aaSKonstantin Belousov ("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); 23926eed2aaSKonstantin Belousov KASSERT(prev >= pdecr, 24026eed2aaSKonstantin Belousov ("negative vmsize for uid %d\n", uip->ui_uid)); 241ee744122SMateusz Guzik #else 242ee744122SMateusz Guzik atomic_subtract_long(&uip->ui_vmsize, pdecr); 243ee744122SMateusz Guzik #endif 244ee744122SMateusz Guzik } 245ee744122SMateusz Guzik 246ee744122SMateusz Guzik static void 247ee744122SMateusz Guzik swap_reserve_force_rlimit(u_long pincr, struct ucred *cred) 248ee744122SMateusz Guzik { 249ee744122SMateusz Guzik struct uidinfo *uip; 250ee744122SMateusz Guzik 251ee744122SMateusz Guzik uip = cred->cr_ruidinfo; 252ee744122SMateusz Guzik atomic_add_long(&uip->ui_vmsize, pincr); 253ee744122SMateusz Guzik } 254ee744122SMateusz Guzik 255ee744122SMateusz Guzik bool 2563364c323SKonstantin Belousov swap_reserve(vm_ooffset_t incr) 2573364c323SKonstantin Belousov { 2583364c323SKonstantin Belousov 259ef694c1aSEdward Tomasz Napierala return (swap_reserve_by_cred(incr, curthread->td_ucred)); 2603364c323SKonstantin Belousov } 2613364c323SKonstantin Belousov 262ee744122SMateusz Guzik bool 263ef694c1aSEdward Tomasz Napierala swap_reserve_by_cred(vm_ooffset_t incr, struct ucred *cred) 2643364c323SKonstantin Belousov { 265e8bb589dSMatt Macy u_long r, s, prev, pincr; 266ee744122SMateusz Guzik #ifdef RACCT 267ee744122SMateusz Guzik int error; 268ee744122SMateusz Guzik #endif 269ee744122SMateusz Guzik int oc; 2703364c323SKonstantin Belousov static int curfail; 2713364c323SKonstantin Belousov static struct timeval lastfail; 2723364c323SKonstantin Belousov 27326eed2aaSKonstantin Belousov KASSERT((incr & PAGE_MASK) == 0, ("%s: incr: %ju & PAGE_MASK", 27426eed2aaSKonstantin Belousov __func__, (uintmax_t)incr)); 2753364c323SKonstantin Belousov 276afcc55f3SEdward Tomasz Napierala #ifdef RACCT 277ee744122SMateusz Guzik if (RACCT_ENABLED()) { 2781ba5ad42SEdward Tomasz Napierala PROC_LOCK(curproc); 2791ba5ad42SEdward Tomasz Napierala error = racct_add(curproc, RACCT_SWAP, incr); 2801ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(curproc); 2811ba5ad42SEdward Tomasz Napierala if (error != 0) 282ee744122SMateusz Guzik return (false); 2834b5c9cf6SEdward Tomasz Napierala } 284afcc55f3SEdward Tomasz Napierala #endif 2851ba5ad42SEdward Tomasz Napierala 286e8bb589dSMatt Macy pincr = atop(incr); 287e8bb589dSMatt Macy prev = atomic_fetchadd_long(&swap_reserved, pincr); 288e8bb589dSMatt Macy r = prev + pincr; 289ee744122SMateusz Guzik s = swap_total; 290a6cc4c6eSKonstantin Belousov oc = atomic_load_int(&vm_overcommit); 291ee744122SMateusz Guzik if (r > s && (oc & SWAP_RESERVE_ALLOW_NONWIRED) != 0) { 292ee744122SMateusz Guzik s += vm_cnt.v_page_count - vm_cnt.v_free_reserved - 293e958ad4cSJeff Roberson vm_wire_count(); 294ee744122SMateusz Guzik } 295ee744122SMateusz Guzik if ((oc & SWAP_RESERVE_FORCE_ON) != 0 && r > s && 296ee744122SMateusz Guzik priv_check(curthread, PRIV_VM_SWAP_NOQUOTA) != 0) { 297e8bb589dSMatt Macy prev = atomic_fetchadd_long(&swap_reserved, -pincr); 29826eed2aaSKonstantin Belousov KASSERT(prev >= pincr, 29926eed2aaSKonstantin Belousov ("swap_reserved < incr on overcommit fail")); 300ee744122SMateusz Guzik goto out_error; 3013364c323SKonstantin Belousov } 3023364c323SKonstantin Belousov 303ee744122SMateusz Guzik if (!swap_reserve_by_cred_rlimit(pincr, cred, oc)) { 304ee744122SMateusz Guzik prev = atomic_fetchadd_long(&swap_reserved, -pincr); 30526eed2aaSKonstantin Belousov KASSERT(prev >= pincr, 30626eed2aaSKonstantin Belousov ("swap_reserved < incr on overcommit fail")); 307ee744122SMateusz Guzik goto out_error; 308ee744122SMateusz Guzik } 309ee744122SMateusz Guzik 310ee744122SMateusz Guzik return (true); 311ee744122SMateusz Guzik 312ee744122SMateusz Guzik out_error: 313ee744122SMateusz Guzik if (ppsratecheck(&lastfail, &curfail, 1)) { 31426eed2aaSKonstantin Belousov printf("uid %d, pid %d: swap reservation " 31526eed2aaSKonstantin Belousov "for %jd bytes failed\n", 316ee744122SMateusz Guzik cred->cr_ruidinfo->ui_uid, curproc->p_pid, incr); 317ee744122SMateusz Guzik } 318afcc55f3SEdward Tomasz Napierala #ifdef RACCT 319ee744122SMateusz Guzik if (RACCT_ENABLED()) { 3201ba5ad42SEdward Tomasz Napierala PROC_LOCK(curproc); 3211ba5ad42SEdward Tomasz Napierala racct_sub(curproc, RACCT_SWAP, incr); 3221ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(curproc); 3231ba5ad42SEdward Tomasz Napierala } 324afcc55f3SEdward Tomasz Napierala #endif 3251ba5ad42SEdward Tomasz Napierala 326ee744122SMateusz Guzik return (false); 3273364c323SKonstantin Belousov } 3283364c323SKonstantin Belousov 3293364c323SKonstantin Belousov void 3303364c323SKonstantin Belousov swap_reserve_force(vm_ooffset_t incr) 3313364c323SKonstantin Belousov { 332e8bb589dSMatt Macy u_long pincr; 3333364c323SKonstantin Belousov 33426eed2aaSKonstantin Belousov KASSERT((incr & PAGE_MASK) == 0, ("%s: incr: %ju & PAGE_MASK", 33526eed2aaSKonstantin Belousov __func__, (uintmax_t)incr)); 3363364c323SKonstantin Belousov 337afcc55f3SEdward Tomasz Napierala #ifdef RACCT 338ee744122SMateusz Guzik if (RACCT_ENABLED()) { 339ee744122SMateusz Guzik PROC_LOCK(curproc); 3401ba5ad42SEdward Tomasz Napierala racct_add_force(curproc, RACCT_SWAP, incr); 341ee744122SMateusz Guzik PROC_UNLOCK(curproc); 342ee744122SMateusz Guzik } 343afcc55f3SEdward Tomasz Napierala #endif 344e8bb589dSMatt Macy pincr = atop(incr); 345e8bb589dSMatt Macy atomic_add_long(&swap_reserved, pincr); 346ee744122SMateusz Guzik swap_reserve_force_rlimit(pincr, curthread->td_ucred); 3473364c323SKonstantin Belousov } 3483364c323SKonstantin Belousov 3493364c323SKonstantin Belousov void 3503364c323SKonstantin Belousov swap_release(vm_ooffset_t decr) 3513364c323SKonstantin Belousov { 352ef694c1aSEdward Tomasz Napierala struct ucred *cred; 3533364c323SKonstantin Belousov 3543364c323SKonstantin Belousov PROC_LOCK(curproc); 355e8bb589dSMatt Macy cred = curproc->p_ucred; 356ef694c1aSEdward Tomasz Napierala swap_release_by_cred(decr, cred); 3573364c323SKonstantin Belousov PROC_UNLOCK(curproc); 3583364c323SKonstantin Belousov } 3593364c323SKonstantin Belousov 3603364c323SKonstantin Belousov void 361ef694c1aSEdward Tomasz Napierala swap_release_by_cred(vm_ooffset_t decr, struct ucred *cred) 3623364c323SKonstantin Belousov { 363ee744122SMateusz Guzik u_long pdecr; 364ee744122SMateusz Guzik #ifdef INVARIANTS 365ee744122SMateusz Guzik u_long prev; 366ee744122SMateusz Guzik #endif 3673364c323SKonstantin Belousov 36826eed2aaSKonstantin Belousov KASSERT((decr & PAGE_MASK) == 0, ("%s: decr: %ju & PAGE_MASK", 36926eed2aaSKonstantin Belousov __func__, (uintmax_t)decr)); 3703364c323SKonstantin Belousov 371e8bb589dSMatt Macy pdecr = atop(decr); 372ee744122SMateusz Guzik #ifdef INVARIANTS 373e8bb589dSMatt Macy prev = atomic_fetchadd_long(&swap_reserved, -pdecr); 374ee744122SMateusz Guzik KASSERT(prev >= pdecr, ("swap_reserved < decr")); 375ee744122SMateusz Guzik #else 376ee744122SMateusz Guzik atomic_subtract_long(&swap_reserved, pdecr); 377ee744122SMateusz Guzik #endif 3783364c323SKonstantin Belousov 379ee744122SMateusz Guzik swap_release_by_cred_rlimit(pdecr, cred); 380e8bb589dSMatt Macy #ifdef RACCT 381e8bb589dSMatt Macy if (racct_enable) 3821ba5ad42SEdward Tomasz Napierala racct_sub_cred(cred, RACCT_SWAP, decr); 383e8bb589dSMatt Macy #endif 3843364c323SKonstantin Belousov } 3853364c323SKonstantin Belousov 386f08b3099SKonstantin Belousov static int swap_pager_full = 2; /* swap space exhaustion (task killing) */ 3877dea2c2eSAlan Cox static int swap_pager_almost_full = 1; /* swap space exhaustion (w/hysteresis)*/ 388756a5412SGleb Smirnoff static struct mtx swbuf_mtx; /* to sync nsw_wcount_async */ 389756a5412SGleb Smirnoff static int nsw_wcount_async; /* limit async write buffers */ 390327f4e83SMatthew Dillon static int nsw_wcount_async_max;/* assigned maximum */ 391183f8e1eSGleb Smirnoff int nsw_cluster_max; /* maximum VOP I/O allowed */ 3921c7c3c6aSMatthew Dillon 39389c241d1SGleb Smirnoff static int sysctl_swap_async_max(SYSCTL_HANDLER_ARGS); 3944c36e917SKonstantin Belousov SYSCTL_PROC(_vm, OID_AUTO, swap_async_max, CTLTYPE_INT | CTLFLAG_RW | 3954c36e917SKonstantin Belousov CTLFLAG_MPSAFE, NULL, 0, sysctl_swap_async_max, "I", 3964c36e917SKonstantin Belousov "Maximum running async swap ops"); 397d027ed2eSAlan Cox static int sysctl_swap_fragmentation(SYSCTL_HANDLER_ARGS); 398d027ed2eSAlan Cox SYSCTL_PROC(_vm, OID_AUTO, swap_fragmentation, CTLTYPE_STRING | CTLFLAG_RD | 399d027ed2eSAlan Cox CTLFLAG_MPSAFE, NULL, 0, sysctl_swap_fragmentation, "A", 400d027ed2eSAlan Cox "Swap Fragmentation Info"); 40189c241d1SGleb Smirnoff 4020cddd8f0SMatthew Dillon static struct sx sw_alloc_sx; 403327f4e83SMatthew Dillon 4041c7c3c6aSMatthew Dillon /* 4051c7c3c6aSMatthew Dillon * "named" and "unnamed" anon region objects. Try to reduce the overhead 4061c7c3c6aSMatthew Dillon * of searching a named list by hashing it just a little. 4071c7c3c6aSMatthew Dillon */ 4081c7c3c6aSMatthew Dillon 4091c7c3c6aSMatthew Dillon #define NOBJLISTS 8 4101c7c3c6aSMatthew Dillon 4111c7c3c6aSMatthew Dillon #define NOBJLIST(handle) \ 412af647ddeSBruce Evans (&swap_pager_object_list[((int)(intptr_t)handle >> 4) & (NOBJLISTS-1)]) 4131c7c3c6aSMatthew Dillon 4141c7c3c6aSMatthew Dillon static struct pagerlst swap_pager_object_list[NOBJLISTS]; 415756a5412SGleb Smirnoff static uma_zone_t swwbuf_zone; 416756a5412SGleb Smirnoff static uma_zone_t swrbuf_zone; 417f425ab8eSKonstantin Belousov static uma_zone_t swblk_zone; 418f425ab8eSKonstantin Belousov static uma_zone_t swpctrie_zone; 4191c7c3c6aSMatthew Dillon 4201c7c3c6aSMatthew Dillon /* 4211c7c3c6aSMatthew Dillon * pagerops for OBJT_SWAP - "swap pager". Some ops are also global procedure 4221c7c3c6aSMatthew Dillon * calls hooked from other parts of the VM system and do not appear here. 4231c7c3c6aSMatthew Dillon * (see vm/swap_pager.h). 4241c7c3c6aSMatthew Dillon */ 425ff98689dSBruce Evans static vm_object_t 42611caded3SAlfred Perlstein swap_pager_alloc(void *handle, vm_ooffset_t size, 4273364c323SKonstantin Belousov vm_prot_t prot, vm_ooffset_t offset, struct ucred *); 42811caded3SAlfred Perlstein static void swap_pager_dealloc(vm_object_t object); 429b0cd2017SGleb Smirnoff static int swap_pager_getpages(vm_object_t, vm_page_t *, int, int *, 430b0cd2017SGleb Smirnoff int *); 431b0cd2017SGleb Smirnoff static int swap_pager_getpages_async(vm_object_t, vm_page_t *, int, int *, 432b0cd2017SGleb Smirnoff int *, pgo_getpages_iodone_t, void *); 433f74be55eSDimitry Andric static void swap_pager_putpages(vm_object_t, vm_page_t *, int, int, int *); 4345ea4972cSAlan Cox static boolean_t 4355ea4972cSAlan Cox swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, int *after); 43611caded3SAlfred Perlstein static void swap_pager_init(void); 43711caded3SAlfred Perlstein static void swap_pager_unswapped(vm_page_t); 438b3fed13eSDavid Schultz static void swap_pager_swapoff(struct swdevt *sp); 439fe7bcbafSKyle Evans static void swap_pager_update_writecount(vm_object_t object, 440fe7bcbafSKyle Evans vm_offset_t start, vm_offset_t end); 441fe7bcbafSKyle Evans static void swap_pager_release_writecount(vm_object_t object, 442fe7bcbafSKyle Evans vm_offset_t start, vm_offset_t end); 443baa1ccceSKonstantin Belousov static void swap_pager_freespace_pgo(vm_object_t object, vm_pindex_t start, 4441390a5cbSKonstantin Belousov vm_size_t size); 445f708ef1bSPoul-Henning Kamp 446d474440aSKonstantin Belousov const struct pagerops swappagerops = { 44700a3fe96SKonstantin Belousov .pgo_kvme_type = KVME_TYPE_SWAP, 448e04e4bacSPoul-Henning Kamp .pgo_init = swap_pager_init, /* early system initialization of pager */ 449e04e4bacSPoul-Henning Kamp .pgo_alloc = swap_pager_alloc, /* allocate an OBJT_SWAP object */ 450e04e4bacSPoul-Henning Kamp .pgo_dealloc = swap_pager_dealloc, /* deallocate an OBJT_SWAP object */ 451e04e4bacSPoul-Henning Kamp .pgo_getpages = swap_pager_getpages, /* pagein */ 45290effb23SGleb Smirnoff .pgo_getpages_async = swap_pager_getpages_async, /* pagein (async) */ 453e04e4bacSPoul-Henning Kamp .pgo_putpages = swap_pager_putpages, /* pageout */ 454e04e4bacSPoul-Henning Kamp .pgo_haspage = swap_pager_haspage, /* get backing store status for page */ 455e04e4bacSPoul-Henning Kamp .pgo_pageunswapped = swap_pager_unswapped, /* remove swap related to page */ 456fe7bcbafSKyle Evans .pgo_update_writecount = swap_pager_update_writecount, 457fe7bcbafSKyle Evans .pgo_release_writecount = swap_pager_release_writecount, 458baa1ccceSKonstantin Belousov .pgo_freespace = swap_pager_freespace_pgo, 4594b8365d7SKonstantin Belousov }; 4604b8365d7SKonstantin Belousov 4611c7c3c6aSMatthew Dillon /* 4621c7c3c6aSMatthew Dillon * swap_*() routines are externally accessible. swp_*() routines are 4631c7c3c6aSMatthew Dillon * internal. 4641c7c3c6aSMatthew Dillon */ 465e9c0cc15SPoul-Henning Kamp static int nswap_lowat = 128; /* in pages, swap_pager_almost_full warn */ 466e9c0cc15SPoul-Henning Kamp static int nswap_hiwat = 512; /* in pages, swap_pager_almost_full warn */ 46726f9a767SRodney W. Grimes 46807c348eaSAlan Cox SYSCTL_INT(_vm, OID_AUTO, dmmax, CTLFLAG_RD, &nsw_cluster_max, 0, 46907c348eaSAlan Cox "Maximum size of a swap block in pages"); 470cee313c4SRobert Watson 471a5edd34aSPoul-Henning Kamp static void swp_sizecheck(void); 47211caded3SAlfred Perlstein static void swp_pager_async_iodone(struct buf *bp); 473230869e0SAlan Cox static bool swp_pager_swblk_empty(struct swblk *sb, int start, int limit); 474abdab7b6SDoug Moore static void swp_pager_free_empty_swblk(vm_object_t, struct swblk *sb); 47588ad2d7bSKonstantin Belousov static int swapongeom(struct vnode *); 47659efee01SPoul-Henning Kamp static int swaponvp(struct thread *, struct vnode *, u_long); 4770190c38bSKonstantin Belousov static int swapoff_one(struct swdevt *sp, struct ucred *cred, 478e8dc2ba2SKonstantin Belousov u_int flags); 47924a1cce3SDavid Greenman 4801c7c3c6aSMatthew Dillon /* 4811c7c3c6aSMatthew Dillon * Swap bitmap functions 4821c7c3c6aSMatthew Dillon */ 483a880104aSDoug Moore static void swp_pager_freeswapspace(const struct page_range *range); 48436b01270SDoug Moore static daddr_t swp_pager_getswapspace(int *npages); 4851c7c3c6aSMatthew Dillon 4861c7c3c6aSMatthew Dillon /* 4871c7c3c6aSMatthew Dillon * Metadata functions 4881c7c3c6aSMatthew Dillon */ 48975694e65SDoug Moore static daddr_t swp_pager_meta_build(vm_object_t, vm_pindex_t, daddr_t, 49075694e65SDoug Moore bool); 491645510e6SKonstantin Belousov static void swp_pager_meta_free(vm_object_t, vm_pindex_t, vm_pindex_t, 492baa1ccceSKonstantin Belousov vm_size_t *); 493467057fcSDoug Moore static void swp_pager_meta_transfer(vm_object_t src, vm_object_t dst, 49494264705SDoug Moore vm_pindex_t pindex, vm_pindex_t count); 49511caded3SAlfred Perlstein static void swp_pager_meta_free_all(vm_object_t); 4968ecbf14bSDoug Moore static daddr_t swp_pager_meta_lookup(vm_object_t, vm_pindex_t); 4971c7c3c6aSMatthew Dillon 49878f1deefSAlan Cox static void 499a880104aSDoug Moore swp_pager_init_freerange(struct page_range *range) 50078f1deefSAlan Cox { 501a880104aSDoug Moore range->start = SWAPBLK_NONE; 502a880104aSDoug Moore range->num = 0; 50378f1deefSAlan Cox } 50478f1deefSAlan Cox 50578f1deefSAlan Cox static void 506a880104aSDoug Moore swp_pager_update_freerange(struct page_range *range, daddr_t addr) 50778f1deefSAlan Cox { 508a880104aSDoug Moore if (range->start + range->num == addr) { 509a880104aSDoug Moore range->num++; 51078f1deefSAlan Cox } else { 511a880104aSDoug Moore swp_pager_freeswapspace(range); 512a880104aSDoug Moore range->start = addr; 513a880104aSDoug Moore range->num = 1; 51478f1deefSAlan Cox } 51578f1deefSAlan Cox } 51678f1deefSAlan Cox 517f425ab8eSKonstantin Belousov static void * 518f425ab8eSKonstantin Belousov swblk_trie_alloc(struct pctrie *ptree) 519f425ab8eSKonstantin Belousov { 520f425ab8eSKonstantin Belousov 521f425ab8eSKonstantin Belousov return (uma_zalloc(swpctrie_zone, M_NOWAIT | (curproc == pageproc ? 522f425ab8eSKonstantin Belousov M_USE_RESERVE : 0))); 523f425ab8eSKonstantin Belousov } 524f425ab8eSKonstantin Belousov 525f425ab8eSKonstantin Belousov static void 526f425ab8eSKonstantin Belousov swblk_trie_free(struct pctrie *ptree, void *node) 527f425ab8eSKonstantin Belousov { 528f425ab8eSKonstantin Belousov 529f425ab8eSKonstantin Belousov uma_zfree(swpctrie_zone, node); 530f425ab8eSKonstantin Belousov } 531f425ab8eSKonstantin Belousov 532f425ab8eSKonstantin Belousov PCTRIE_DEFINE(SWAP, swblk, p, swblk_trie_alloc, swblk_trie_free); 533f425ab8eSKonstantin Belousov 53494264705SDoug Moore static struct swblk * 53594264705SDoug Moore swblk_lookup(vm_object_t object, vm_pindex_t pindex) 53694264705SDoug Moore { 53794264705SDoug Moore return (SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, 53894264705SDoug Moore rounddown(pindex, SWAP_META_PAGES))); 53994264705SDoug Moore } 54094264705SDoug Moore 54194264705SDoug Moore static struct swblk * 54294264705SDoug Moore swblk_start(vm_object_t object, vm_pindex_t pindex) 54394264705SDoug Moore { 54494264705SDoug Moore return (SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks, 54594264705SDoug Moore rounddown(pindex, SWAP_META_PAGES))); 54694264705SDoug Moore } 54794264705SDoug Moore 54894264705SDoug Moore static struct swblk * 54994264705SDoug Moore swblk_next(vm_object_t object, struct swblk *sb) 55094264705SDoug Moore { 55194264705SDoug Moore return (swblk_start(object, sb->p + SWAP_META_PAGES)); 55294264705SDoug Moore } 55394264705SDoug Moore 55494264705SDoug Moore static struct swblk * 55594264705SDoug Moore swblk_start_limit(vm_object_t object, vm_pindex_t pindex, vm_pindex_t limit) 55694264705SDoug Moore { 55794264705SDoug Moore struct swblk *sb = swblk_start(object, pindex); 55894264705SDoug Moore if (sb != NULL && sb->p < limit) 55994264705SDoug Moore return (sb); 56094264705SDoug Moore return (NULL); 56194264705SDoug Moore } 56294264705SDoug Moore 56394264705SDoug Moore static struct swblk * 56494264705SDoug Moore swblk_next_limit(vm_object_t object, struct swblk *sb, vm_pindex_t limit) 56594264705SDoug Moore { 56694264705SDoug Moore return (swblk_start_limit(object, sb->p + SWAP_META_PAGES, limit)); 56794264705SDoug Moore } 56894264705SDoug Moore 56994264705SDoug Moore static void 57094264705SDoug Moore swblk_lookup_remove(vm_object_t object, struct swblk *sb) 57194264705SDoug Moore { 57294264705SDoug Moore SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, sb->p); 57394264705SDoug Moore } 57494264705SDoug Moore 57594264705SDoug Moore static int 57694264705SDoug Moore swblk_lookup_insert(vm_object_t object, struct swblk *sb) 57794264705SDoug Moore { 57894264705SDoug Moore return (SWAP_PCTRIE_INSERT(&object->un_pager.swp.swp_blks, sb)); 57994264705SDoug Moore } 58094264705SDoug Moore 58194264705SDoug Moore static bool 58294264705SDoug Moore swblk_is_empty(vm_object_t object) 58394264705SDoug Moore { 58494264705SDoug Moore return (pctrie_is_empty(&object->un_pager.swp.swp_blks)); 58594264705SDoug Moore } 58694264705SDoug Moore 5871c7c3c6aSMatthew Dillon /* 5881c7c3c6aSMatthew Dillon * SWP_SIZECHECK() - update swap_pager_full indication 5891c7c3c6aSMatthew Dillon * 59020d3034fSMatthew Dillon * update the swap_pager_almost_full indication and warn when we are 59120d3034fSMatthew Dillon * about to run out of swap space, using lowat/hiwat hysteresis. 59220d3034fSMatthew Dillon * 59320d3034fSMatthew Dillon * Clear swap_pager_full ( task killing ) indication when lowat is met. 5941c7c3c6aSMatthew Dillon * 5951c7c3c6aSMatthew Dillon * No restrictions on call 5961c7c3c6aSMatthew Dillon * This routine may not block. 5971c7c3c6aSMatthew Dillon */ 598a5edd34aSPoul-Henning Kamp static void 5992f249180SPoul-Henning Kamp swp_sizecheck(void) 6000d94caffSDavid Greenman { 60123955314SAlfred Perlstein 6028f60c087SPoul-Henning Kamp if (swap_pager_avail < nswap_lowat) { 60320d3034fSMatthew Dillon if (swap_pager_almost_full == 0) { 6041af87c92SDavid Greenman printf("swap_pager: out of swap space\n"); 60520d3034fSMatthew Dillon swap_pager_almost_full = 1; 6062b0d37a4SMatthew Dillon } 60720d3034fSMatthew Dillon } else { 60826f9a767SRodney W. Grimes swap_pager_full = 0; 6098f60c087SPoul-Henning Kamp if (swap_pager_avail > nswap_hiwat) 61020d3034fSMatthew Dillon swap_pager_almost_full = 0; 61126f9a767SRodney W. Grimes } 6121c7c3c6aSMatthew Dillon } 6131c7c3c6aSMatthew Dillon 6141c7c3c6aSMatthew Dillon /* 6151c7c3c6aSMatthew Dillon * SWAP_PAGER_INIT() - initialize the swap pager! 6161c7c3c6aSMatthew Dillon * 6171c7c3c6aSMatthew Dillon * Expected to be started from system init. NOTE: This code is run 6181c7c3c6aSMatthew Dillon * before much else so be careful what you depend on. Most of the VM 6191c7c3c6aSMatthew Dillon * system has yet to be initialized at this point. 6201c7c3c6aSMatthew Dillon */ 621f5a12711SPoul-Henning Kamp static void 6222f249180SPoul-Henning Kamp swap_pager_init(void) 623df8bae1dSRodney W. Grimes { 6241c7c3c6aSMatthew Dillon /* 6251c7c3c6aSMatthew Dillon * Initialize object lists 6261c7c3c6aSMatthew Dillon */ 6271c7c3c6aSMatthew Dillon int i; 6281c7c3c6aSMatthew Dillon 6291c7c3c6aSMatthew Dillon for (i = 0; i < NOBJLISTS; ++i) 6301c7c3c6aSMatthew Dillon TAILQ_INIT(&swap_pager_object_list[i]); 63120da9c2eSPoul-Henning Kamp mtx_init(&sw_dev_mtx, "swapdev", NULL, MTX_DEF); 63215719273SKonstantin Belousov sx_init(&sw_alloc_sx, "swspsx"); 63304533e1eSKonstantin Belousov sx_init(&swdev_syscall_lock, "swsysc"); 634183f8e1eSGleb Smirnoff 635183f8e1eSGleb Smirnoff /* 636183f8e1eSGleb Smirnoff * The nsw_cluster_max is constrained by the bp->b_pages[] 637183f8e1eSGleb Smirnoff * array, which has maxphys / PAGE_SIZE entries, and our locally 638183f8e1eSGleb Smirnoff * defined MAX_PAGEOUT_CLUSTER. Also be aware that swap ops are 639183f8e1eSGleb Smirnoff * constrained by the swap device interleave stripe size. 640183f8e1eSGleb Smirnoff * 641183f8e1eSGleb Smirnoff * Initialized early so that GEOM_ELI can see it. 642183f8e1eSGleb Smirnoff */ 643183f8e1eSGleb Smirnoff nsw_cluster_max = min(maxphys / PAGE_SIZE, MAX_PAGEOUT_CLUSTER); 6441c7c3c6aSMatthew Dillon } 64526f9a767SRodney W. Grimes 646df8bae1dSRodney W. Grimes /* 6471c7c3c6aSMatthew Dillon * SWAP_PAGER_SWAP_INIT() - swap pager initialization from pageout process 6481c7c3c6aSMatthew Dillon * 6491c7c3c6aSMatthew Dillon * Expected to be started from pageout process once, prior to entering 6501c7c3c6aSMatthew Dillon * its main loop. 651df8bae1dSRodney W. Grimes */ 65224a1cce3SDavid Greenman void 6532f249180SPoul-Henning Kamp swap_pager_swap_init(void) 654df8bae1dSRodney W. Grimes { 65561203277SDag-Erling Smørgrav unsigned long n, n2; 6560d94caffSDavid Greenman 65726f9a767SRodney W. Grimes /* 6581c7c3c6aSMatthew Dillon * Number of in-transit swap bp operations. Don't 6591c7c3c6aSMatthew Dillon * exhaust the pbufs completely. Make sure we 6601c7c3c6aSMatthew Dillon * initialize workable values (0 will work for hysteresis 6611c7c3c6aSMatthew Dillon * but it isn't very efficient). 6621c7c3c6aSMatthew Dillon * 663327f4e83SMatthew Dillon * Currently we hardwire nsw_wcount_async to 4. This limit is 664327f4e83SMatthew Dillon * designed to prevent other I/O from having high latencies due to 665327f4e83SMatthew Dillon * our pageout I/O. The value 4 works well for one or two active swap 666327f4e83SMatthew Dillon * devices but is probably a little low if you have more. Even so, 667327f4e83SMatthew Dillon * a higher value would probably generate only a limited improvement 668327f4e83SMatthew Dillon * with three or four active swap devices since the system does not 669327f4e83SMatthew Dillon * typically have to pageout at extreme bandwidths. We will want 670327f4e83SMatthew Dillon * at least 2 per swap devices, and 4 is a pretty good value if you 671327f4e83SMatthew Dillon * have one NFS swap device due to the command/ack latency over NFS. 672327f4e83SMatthew Dillon * So it all works out pretty well. 673183f8e1eSGleb Smirnoff * 674183f8e1eSGleb Smirnoff * nsw_cluster_max is initialized in swap_pager_init(). 67526f9a767SRodney W. Grimes */ 676327f4e83SMatthew Dillon 677327f4e83SMatthew Dillon nsw_wcount_async = 4; 678327f4e83SMatthew Dillon nsw_wcount_async_max = nsw_wcount_async; 679756a5412SGleb Smirnoff mtx_init(&swbuf_mtx, "async swbuf mutex", NULL, MTX_DEF); 680756a5412SGleb Smirnoff 681756a5412SGleb Smirnoff swwbuf_zone = pbuf_zsecond_create("swwbuf", nswbuf / 4); 682756a5412SGleb Smirnoff swrbuf_zone = pbuf_zsecond_create("swrbuf", nswbuf / 2); 68324a1cce3SDavid Greenman 6841c7c3c6aSMatthew Dillon /* 685a8233027SKonstantin Belousov * Initialize our zone, taking the user's requested size or 686a8233027SKonstantin Belousov * estimating the number we need based on the number of pages 687a8233027SKonstantin Belousov * in the system. 6881c7c3c6aSMatthew Dillon */ 689a8233027SKonstantin Belousov n = maxswzone != 0 ? maxswzone / sizeof(struct swblk) : 690a8233027SKonstantin Belousov vm_cnt.v_page_count / 2; 691f425ab8eSKonstantin Belousov swpctrie_zone = uma_zcreate("swpctrie", pctrie_node_size(), NULL, NULL, 6926c5f36ffSJeff Roberson pctrie_zone_init, NULL, UMA_ALIGN_PTR, 0); 693f425ab8eSKonstantin Belousov swblk_zone = uma_zcreate("swblk", sizeof(struct swblk), NULL, NULL, 6946c5f36ffSJeff Roberson NULL, NULL, _Alignof(struct swblk) - 1, 0); 69522a5e6b9SDag-Erling Smørgrav n2 = n; 6968355f576SJeff Roberson do { 697f425ab8eSKonstantin Belousov if (uma_zone_reserve_kva(swblk_zone, n)) 69861ce6eeeSAlfred Perlstein break; 69961ce6eeeSAlfred Perlstein /* 70061ce6eeeSAlfred Perlstein * if the allocation failed, try a zone two thirds the 70161ce6eeeSAlfred Perlstein * size of the previous attempt. 70261ce6eeeSAlfred Perlstein */ 70361ce6eeeSAlfred Perlstein n -= ((n + 2) / 3); 70461ce6eeeSAlfred Perlstein } while (n > 0); 70553faf5a7SKonstantin Belousov 70653faf5a7SKonstantin Belousov /* 70753faf5a7SKonstantin Belousov * Often uma_zone_reserve_kva() cannot reserve exactly the 70853faf5a7SKonstantin Belousov * requested size. Account for the difference when 70953faf5a7SKonstantin Belousov * calculating swap_maxpages. 71053faf5a7SKonstantin Belousov */ 71153faf5a7SKonstantin Belousov n = uma_zone_get_max(swblk_zone); 71253faf5a7SKonstantin Belousov 7131fffcd75SKonstantin Belousov if (n < n2) 714a8233027SKonstantin Belousov printf("Swap blk zone entries changed from %lu to %lu.\n", 715f425ab8eSKonstantin Belousov n2, n); 716303fa05aSKonstantin Belousov /* absolute maximum we can handle assuming 100% efficiency */ 71761203277SDag-Erling Smørgrav swap_maxpages = n * SWAP_META_PAGES; 718f425ab8eSKonstantin Belousov swzone = n * sizeof(struct swblk); 719f425ab8eSKonstantin Belousov if (!uma_zone_reserve_kva(swpctrie_zone, n)) 720f425ab8eSKonstantin Belousov printf("Cannot reserve swap pctrie zone, " 721f425ab8eSKonstantin Belousov "reduce kern.maxswzone.\n"); 72224a1cce3SDavid Greenman } 72324a1cce3SDavid Greenman 72428bc23abSKonstantin Belousov bool 72528bc23abSKonstantin Belousov swap_pager_init_object(vm_object_t object, void *handle, struct ucred *cred, 72628bc23abSKonstantin Belousov vm_ooffset_t size, vm_ooffset_t offset) 72728bc23abSKonstantin Belousov { 72828bc23abSKonstantin Belousov if (cred != NULL) { 72928bc23abSKonstantin Belousov if (!swap_reserve_by_cred(size, cred)) 73028bc23abSKonstantin Belousov return (false); 73128bc23abSKonstantin Belousov crhold(cred); 73228bc23abSKonstantin Belousov } 73328bc23abSKonstantin Belousov 73428bc23abSKonstantin Belousov object->un_pager.swp.writemappings = 0; 73528bc23abSKonstantin Belousov object->handle = handle; 73628bc23abSKonstantin Belousov if (cred != NULL) { 73728bc23abSKonstantin Belousov object->cred = cred; 73828bc23abSKonstantin Belousov object->charge = size; 73928bc23abSKonstantin Belousov } 74028bc23abSKonstantin Belousov return (true); 74128bc23abSKonstantin Belousov } 74228bc23abSKonstantin Belousov 743eb4d6a1bSKonstantin Belousov static vm_object_t 7444b8365d7SKonstantin Belousov swap_pager_alloc_init(objtype_t otype, void *handle, struct ucred *cred, 7454b8365d7SKonstantin Belousov vm_ooffset_t size, vm_ooffset_t offset) 746eb4d6a1bSKonstantin Belousov { 747eb4d6a1bSKonstantin Belousov vm_object_t object; 748eb4d6a1bSKonstantin Belousov 749f425ab8eSKonstantin Belousov /* 750f425ab8eSKonstantin Belousov * The un_pager.swp.swp_blks trie is initialized by 751f425ab8eSKonstantin Belousov * vm_object_allocate() to ensure the correct order of 752f425ab8eSKonstantin Belousov * visibility to other threads. 753f425ab8eSKonstantin Belousov */ 7544b8365d7SKonstantin Belousov object = vm_object_allocate(otype, OFF_TO_IDX(offset + 755eb4d6a1bSKonstantin Belousov PAGE_MASK + size)); 756f425ab8eSKonstantin Belousov 75728bc23abSKonstantin Belousov if (!swap_pager_init_object(object, handle, cred, size, offset)) { 75828bc23abSKonstantin Belousov vm_object_deallocate(object); 75928bc23abSKonstantin Belousov return (NULL); 760eb4d6a1bSKonstantin Belousov } 761eb4d6a1bSKonstantin Belousov return (object); 762eb4d6a1bSKonstantin Belousov } 763eb4d6a1bSKonstantin Belousov 76424a1cce3SDavid Greenman /* 7651c7c3c6aSMatthew Dillon * SWAP_PAGER_ALLOC() - allocate a new OBJT_SWAP VM object and instantiate 7661c7c3c6aSMatthew Dillon * its metadata structures. 7671c7c3c6aSMatthew Dillon * 7681c7c3c6aSMatthew Dillon * This routine is called from the mmap and fork code to create a new 769eb4d6a1bSKonstantin Belousov * OBJT_SWAP object. 7701c7c3c6aSMatthew Dillon * 771eb4d6a1bSKonstantin Belousov * This routine must ensure that no live duplicate is created for 772eb4d6a1bSKonstantin Belousov * the named object request, which is protected against by 773eb4d6a1bSKonstantin Belousov * holding the sw_alloc_sx lock in case handle != NULL. 77424a1cce3SDavid Greenman */ 775f5a12711SPoul-Henning Kamp static vm_object_t 7766cde7a16SDavid Greenman swap_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 7773364c323SKonstantin Belousov vm_ooffset_t offset, struct ucred *cred) 77824a1cce3SDavid Greenman { 77924a1cce3SDavid Greenman vm_object_t object; 7802f7af3dbSAlan Cox 781eb4d6a1bSKonstantin Belousov if (handle != NULL) { 7821c7c3c6aSMatthew Dillon /* 7831c7c3c6aSMatthew Dillon * Reference existing named region or allocate new one. There 7841c7c3c6aSMatthew Dillon * should not be a race here against swp_pager_meta_build() 7851c7c3c6aSMatthew Dillon * as called from vm_page_remove() in regards to the lookup 7861c7c3c6aSMatthew Dillon * of the handle. 7871c7c3c6aSMatthew Dillon */ 7880cddd8f0SMatthew Dillon sx_xlock(&sw_alloc_sx); 7891c7c3c6aSMatthew Dillon object = vm_pager_object_lookup(NOBJLIST(handle), handle); 790b5e8f167SAlan Cox if (object == NULL) { 7914b8365d7SKonstantin Belousov object = swap_pager_alloc_init(OBJT_SWAP, handle, cred, 7924b8365d7SKonstantin Belousov size, offset); 793eb4d6a1bSKonstantin Belousov if (object != NULL) { 794eb4d6a1bSKonstantin Belousov TAILQ_INSERT_TAIL(NOBJLIST(object->handle), 795eb4d6a1bSKonstantin Belousov object, pager_object_list); 7963364c323SKonstantin Belousov } 79724a1cce3SDavid Greenman } 7980cddd8f0SMatthew Dillon sx_xunlock(&sw_alloc_sx); 79924a1cce3SDavid Greenman } else { 8004b8365d7SKonstantin Belousov object = swap_pager_alloc_init(OBJT_SWAP, handle, cred, 8014b8365d7SKonstantin Belousov size, offset); 80224a1cce3SDavid Greenman } 80324a1cce3SDavid Greenman return (object); 804df8bae1dSRodney W. Grimes } 805df8bae1dSRodney W. Grimes 80626f9a767SRodney W. Grimes /* 8071c7c3c6aSMatthew Dillon * SWAP_PAGER_DEALLOC() - remove swap metadata from object 8081c7c3c6aSMatthew Dillon * 8091c7c3c6aSMatthew Dillon * The swap backing for the object is destroyed. The code is 8101c7c3c6aSMatthew Dillon * designed such that we can reinstantiate it later, but this 8111c7c3c6aSMatthew Dillon * routine is typically called only when the entire object is 8121c7c3c6aSMatthew Dillon * about to be destroyed. 8131c7c3c6aSMatthew Dillon * 81415523cf7SKonstantin Belousov * The object must be locked. 81526f9a767SRodney W. Grimes */ 816df8bae1dSRodney W. Grimes static void 8172f249180SPoul-Henning Kamp swap_pager_dealloc(vm_object_t object) 81826f9a767SRodney W. Grimes { 8194dcc5c2dSMatthew Dillon 820eb4d6a1bSKonstantin Belousov VM_OBJECT_ASSERT_WLOCKED(object); 821eb4d6a1bSKonstantin Belousov KASSERT((object->flags & OBJ_DEAD) != 0, ("dealloc of reachable obj")); 822eb4d6a1bSKonstantin Belousov 82326f9a767SRodney W. Grimes /* 8241c7c3c6aSMatthew Dillon * Remove from list right away so lookups will fail if we block for 8251c7c3c6aSMatthew Dillon * pageout completion. 82626f9a767SRodney W. Grimes */ 82767388836SKonstantin Belousov if ((object->flags & OBJ_ANON) == 0 && object->handle != NULL) { 828eb4d6a1bSKonstantin Belousov VM_OBJECT_WUNLOCK(object); 829eb4d6a1bSKonstantin Belousov sx_xlock(&sw_alloc_sx); 830eb4d6a1bSKonstantin Belousov TAILQ_REMOVE(NOBJLIST(object->handle), object, 831eb4d6a1bSKonstantin Belousov pager_object_list); 832eb4d6a1bSKonstantin Belousov sx_xunlock(&sw_alloc_sx); 833eb4d6a1bSKonstantin Belousov VM_OBJECT_WLOCK(object); 834bd228075SAlan Cox } 8351c7c3c6aSMatthew Dillon 8361c7c3c6aSMatthew Dillon vm_object_pip_wait(object, "swpdea"); 8371c7c3c6aSMatthew Dillon 8381c7c3c6aSMatthew Dillon /* 8391c7c3c6aSMatthew Dillon * Free all remaining metadata. We only bother to free it from 8401c7c3c6aSMatthew Dillon * the swap meta data. We do not attempt to free swapblk's still 8411c7c3c6aSMatthew Dillon * associated with vm_page_t's for this object. We do not care 8421c7c3c6aSMatthew Dillon * if paging is still in progress on some objects. 8431c7c3c6aSMatthew Dillon */ 8441c7c3c6aSMatthew Dillon swp_pager_meta_free_all(object); 845e735691bSJohn Baldwin object->handle = NULL; 846e735691bSJohn Baldwin object->type = OBJT_DEAD; 847fffc1c59SMark Johnston 848fffc1c59SMark Johnston /* 849fffc1c59SMark Johnston * Release the allocation charge. 850fffc1c59SMark Johnston */ 851fffc1c59SMark Johnston if (object->cred != NULL) { 852fffc1c59SMark Johnston swap_release_by_cred(object->charge, object->cred); 853fffc1c59SMark Johnston object->charge = 0; 854fffc1c59SMark Johnston crfree(object->cred); 855fffc1c59SMark Johnston object->cred = NULL; 856fffc1c59SMark Johnston } 857fffc1c59SMark Johnston 858fffc1c59SMark Johnston /* 859fffc1c59SMark Johnston * Hide the object from swap_pager_swapoff(). 860fffc1c59SMark Johnston */ 8614b8365d7SKonstantin Belousov vm_object_clear_flag(object, OBJ_SWAP); 8621c7c3c6aSMatthew Dillon } 8631c7c3c6aSMatthew Dillon 8641c7c3c6aSMatthew Dillon /************************************************************************ 8651c7c3c6aSMatthew Dillon * SWAP PAGER BITMAP ROUTINES * 8661c7c3c6aSMatthew Dillon ************************************************************************/ 8671c7c3c6aSMatthew Dillon 8681c7c3c6aSMatthew Dillon /* 8691c7c3c6aSMatthew Dillon * SWP_PAGER_GETSWAPSPACE() - allocate raw swap space 8701c7c3c6aSMatthew Dillon * 87136b01270SDoug Moore * Allocate swap for up to the requested number of pages. The 87236b01270SDoug Moore * starting swap block number (a page index) is returned or 87336b01270SDoug Moore * SWAPBLK_NONE if the allocation failed. 8741c7c3c6aSMatthew Dillon * 8751c7c3c6aSMatthew Dillon * Also has the side effect of advising that somebody made a mistake 8761c7c3c6aSMatthew Dillon * when they configured swap and didn't configure enough. 8771c7c3c6aSMatthew Dillon * 87815523cf7SKonstantin Belousov * This routine may not sleep. 8798f60c087SPoul-Henning Kamp * 8808f60c087SPoul-Henning Kamp * We allocate in round-robin fashion from the configured devices. 8811c7c3c6aSMatthew Dillon */ 882a5edd34aSPoul-Henning Kamp static daddr_t 88336b01270SDoug Moore swp_pager_getswapspace(int *io_npages) 8841c7c3c6aSMatthew Dillon { 8851c7c3c6aSMatthew Dillon daddr_t blk; 8868f60c087SPoul-Henning Kamp struct swdevt *sp; 88787ae0686SDoug Moore int mpages, npages; 8881c7c3c6aSMatthew Dillon 88936b01270SDoug Moore KASSERT(*io_npages >= 1, 89036b01270SDoug Moore ("%s: npages not positive", __func__)); 8918f60c087SPoul-Henning Kamp blk = SWAPBLK_NONE; 89231c82722SDoug Moore mpages = *io_npages; 89331c82722SDoug Moore npages = imin(BLIST_MAX_ALLOC, mpages); 89420da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 8958f60c087SPoul-Henning Kamp sp = swdevhd; 89648e98a2aSDoug Moore while (!TAILQ_EMPTY(&swtailq)) { 8978f60c087SPoul-Henning Kamp if (sp == NULL) 8988f60c087SPoul-Henning Kamp sp = TAILQ_FIRST(&swtailq); 89948e98a2aSDoug Moore if ((sp->sw_flags & SW_CLOSING) == 0) 90087ae0686SDoug Moore blk = blist_alloc(sp->sw_blist, &npages, mpages); 90148e98a2aSDoug Moore if (blk != SWAPBLK_NONE) 90248e98a2aSDoug Moore break; 90348e98a2aSDoug Moore sp = TAILQ_NEXT(sp, sw_list); 90448e98a2aSDoug Moore if (swdevhd == sp) { 90536b01270SDoug Moore if (npages == 1) 90648e98a2aSDoug Moore break; 90787ae0686SDoug Moore mpages = npages - 1; 90848e98a2aSDoug Moore npages >>= 1; 90948e98a2aSDoug Moore } 91048e98a2aSDoug Moore } 9118f60c087SPoul-Henning Kamp if (blk != SWAPBLK_NONE) { 91248e98a2aSDoug Moore *io_npages = npages; 9138f60c087SPoul-Henning Kamp blk += sp->sw_first; 9148f60c087SPoul-Henning Kamp sp->sw_used += npages; 915d05bc129SAlan Cox swap_pager_avail -= npages; 9168f60c087SPoul-Henning Kamp swp_sizecheck(); 9178f60c087SPoul-Henning Kamp swdevhd = TAILQ_NEXT(sp, sw_list); 91848e98a2aSDoug Moore } else { 9192b0d37a4SMatthew Dillon if (swap_pager_full != 2) { 92048e98a2aSDoug Moore printf("swp_pager_getswapspace(%d): failed\n", 92148e98a2aSDoug Moore *io_npages); 9222b0d37a4SMatthew Dillon swap_pager_full = 2; 92320d3034fSMatthew Dillon swap_pager_almost_full = 1; 9242b0d37a4SMatthew Dillon } 9258f60c087SPoul-Henning Kamp swdevhd = NULL; 92648e98a2aSDoug Moore } 927d05bc129SAlan Cox mtx_unlock(&sw_dev_mtx); 9281c7c3c6aSMatthew Dillon return (blk); 92926f9a767SRodney W. Grimes } 93026f9a767SRodney W. Grimes 931541a1175SAlan Cox static bool 932b3fed13eSDavid Schultz swp_pager_isondev(daddr_t blk, struct swdevt *sp) 9338f60c087SPoul-Henning Kamp { 9348f60c087SPoul-Henning Kamp 935b3fed13eSDavid Schultz return (blk >= sp->sw_first && blk < sp->sw_end); 9368f60c087SPoul-Henning Kamp } 9378f60c087SPoul-Henning Kamp 9384b03903aSPoul-Henning Kamp static void 9394b03903aSPoul-Henning Kamp swp_pager_strategy(struct buf *bp) 9404b03903aSPoul-Henning Kamp { 9414b03903aSPoul-Henning Kamp struct swdevt *sp; 9424b03903aSPoul-Henning Kamp 94320da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 9444b03903aSPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 945541a1175SAlan Cox if (swp_pager_isondev(bp->b_blkno, sp)) { 94620da9c2eSPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 9472cc718a1SKonstantin Belousov if ((sp->sw_flags & SW_UNMAPPED) != 0 && 9482cc718a1SKonstantin Belousov unmapped_buf_allowed) { 9492cc718a1SKonstantin Belousov bp->b_data = unmapped_buf; 9502cc718a1SKonstantin Belousov bp->b_offset = 0; 9512cc718a1SKonstantin Belousov } else { 9522cc718a1SKonstantin Belousov pmap_qenter((vm_offset_t)bp->b_data, 9532cc718a1SKonstantin Belousov &bp->b_pages[0], bp->b_bcount / PAGE_SIZE); 9542cc718a1SKonstantin Belousov } 9554b03903aSPoul-Henning Kamp sp->sw_strategy(bp, sp); 9564b03903aSPoul-Henning Kamp return; 9574b03903aSPoul-Henning Kamp } 9584b03903aSPoul-Henning Kamp } 95920da9c2eSPoul-Henning Kamp panic("Swapdev not found"); 9604b03903aSPoul-Henning Kamp } 9614b03903aSPoul-Henning Kamp 96226f9a767SRodney W. Grimes /* 9631c7c3c6aSMatthew Dillon * SWP_PAGER_FREESWAPSPACE() - free raw swap space 9641c7c3c6aSMatthew Dillon * 9651c7c3c6aSMatthew Dillon * This routine returns the specified swap blocks back to the bitmap. 9661c7c3c6aSMatthew Dillon * 96715523cf7SKonstantin Belousov * This routine may not sleep. 96826f9a767SRodney W. Grimes */ 969a5edd34aSPoul-Henning Kamp static void 970a880104aSDoug Moore swp_pager_freeswapspace(const struct page_range *range) 9710d94caffSDavid Greenman { 972a880104aSDoug Moore daddr_t blk, npages; 9738f60c087SPoul-Henning Kamp struct swdevt *sp; 97492da00bbSMatthew Dillon 975a880104aSDoug Moore blk = range->start; 976a880104aSDoug Moore npages = range->num; 977230869e0SAlan Cox if (npages == 0) 978230869e0SAlan Cox return; 9797645e885SAlan Cox mtx_lock(&sw_dev_mtx); 9807645e885SAlan Cox TAILQ_FOREACH(sp, &swtailq, sw_list) { 981541a1175SAlan Cox if (swp_pager_isondev(blk, sp)) { 98292da00bbSMatthew Dillon sp->sw_used -= npages; 98392da00bbSMatthew Dillon /* 9847645e885SAlan Cox * If we are attempting to stop swapping on 9857645e885SAlan Cox * this device, we don't want to mark any 9867645e885SAlan Cox * blocks free lest they be reused. 98792da00bbSMatthew Dillon */ 9887645e885SAlan Cox if ((sp->sw_flags & SW_CLOSING) == 0) { 9897645e885SAlan Cox blist_free(sp->sw_blist, blk - sp->sw_first, 9907645e885SAlan Cox npages); 9918f60c087SPoul-Henning Kamp swap_pager_avail += npages; 9921c7c3c6aSMatthew Dillon swp_sizecheck(); 99326f9a767SRodney W. Grimes } 9947645e885SAlan Cox mtx_unlock(&sw_dev_mtx); 9957645e885SAlan Cox return; 9967645e885SAlan Cox } 9977645e885SAlan Cox } 9987645e885SAlan Cox panic("Swapdev not found"); 9997645e885SAlan Cox } 10001c7c3c6aSMatthew Dillon 100126f9a767SRodney W. Grimes /* 1002d027ed2eSAlan Cox * SYSCTL_SWAP_FRAGMENTATION() - produce raw swap space stats 1003d027ed2eSAlan Cox */ 1004d027ed2eSAlan Cox static int 1005d027ed2eSAlan Cox sysctl_swap_fragmentation(SYSCTL_HANDLER_ARGS) 1006d027ed2eSAlan Cox { 1007d027ed2eSAlan Cox struct sbuf sbuf; 1008d027ed2eSAlan Cox struct swdevt *sp; 1009d027ed2eSAlan Cox const char *devname; 1010d027ed2eSAlan Cox int error; 1011d027ed2eSAlan Cox 1012d027ed2eSAlan Cox error = sysctl_wire_old_buffer(req, 0); 1013d027ed2eSAlan Cox if (error != 0) 1014d027ed2eSAlan Cox return (error); 1015d027ed2eSAlan Cox sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 1016d027ed2eSAlan Cox mtx_lock(&sw_dev_mtx); 1017d027ed2eSAlan Cox TAILQ_FOREACH(sp, &swtailq, sw_list) { 10187ad2a82dSMateusz Guzik if (vn_isdisk(sp->sw_vp)) 1019d027ed2eSAlan Cox devname = devtoname(sp->sw_vp->v_rdev); 1020d027ed2eSAlan Cox else 1021d027ed2eSAlan Cox devname = "[file]"; 1022d027ed2eSAlan Cox sbuf_printf(&sbuf, "\nFree space on device %s:\n", devname); 1023d027ed2eSAlan Cox blist_stats(sp->sw_blist, &sbuf); 1024d027ed2eSAlan Cox } 1025d027ed2eSAlan Cox mtx_unlock(&sw_dev_mtx); 1026d027ed2eSAlan Cox error = sbuf_finish(&sbuf); 1027d027ed2eSAlan Cox sbuf_delete(&sbuf); 1028d027ed2eSAlan Cox return (error); 1029d027ed2eSAlan Cox } 1030d027ed2eSAlan Cox 1031d027ed2eSAlan Cox /* 10321c7c3c6aSMatthew Dillon * SWAP_PAGER_FREESPACE() - frees swap blocks associated with a page 10331c7c3c6aSMatthew Dillon * range within an object. 10341c7c3c6aSMatthew Dillon * 10351c7c3c6aSMatthew Dillon * This routine removes swapblk assignments from swap metadata. 10361c7c3c6aSMatthew Dillon * 10371c7c3c6aSMatthew Dillon * The external callers of this routine typically have already destroyed 10381c7c3c6aSMatthew Dillon * or renamed vm_page_t's associated with this range in the object so 10391c7c3c6aSMatthew Dillon * we should be ok. 1040c25673ffSAttilio Rao * 1041c25673ffSAttilio Rao * The object must be locked. 104226f9a767SRodney W. Grimes */ 1043baa1ccceSKonstantin Belousov void 1044baa1ccceSKonstantin Belousov swap_pager_freespace(vm_object_t object, vm_pindex_t start, vm_size_t size, 1045baa1ccceSKonstantin Belousov vm_size_t *freed) 104626f9a767SRodney W. Grimes { 1047baa1ccceSKonstantin Belousov MPASS((object->flags & OBJ_SWAP) != 0); 104823955314SAlfred Perlstein 1049baa1ccceSKonstantin Belousov swp_pager_meta_free(object, start, size, freed); 1050baa1ccceSKonstantin Belousov } 1051baa1ccceSKonstantin Belousov 1052baa1ccceSKonstantin Belousov static void 1053baa1ccceSKonstantin Belousov swap_pager_freespace_pgo(vm_object_t object, vm_pindex_t start, vm_size_t size) 1054baa1ccceSKonstantin Belousov { 1055baa1ccceSKonstantin Belousov MPASS((object->flags & OBJ_SWAP) != 0); 1056baa1ccceSKonstantin Belousov 1057baa1ccceSKonstantin Belousov swp_pager_meta_free(object, start, size, NULL); 10584dcc5c2dSMatthew Dillon } 10594dcc5c2dSMatthew Dillon 10604dcc5c2dSMatthew Dillon /* 10614dcc5c2dSMatthew Dillon * SWAP_PAGER_RESERVE() - reserve swap blocks in object 10624dcc5c2dSMatthew Dillon * 10634dcc5c2dSMatthew Dillon * Assigns swap blocks to the specified range within the object. The 106456ce850bSKonstantin Belousov * swap blocks are not zeroed. Any previous swap assignment is destroyed. 10654dcc5c2dSMatthew Dillon * 10664dcc5c2dSMatthew Dillon * Returns 0 on success, -1 on failure. 10674dcc5c2dSMatthew Dillon */ 10684dcc5c2dSMatthew Dillon int 1069686aa928SMark Johnston swap_pager_reserve(vm_object_t object, vm_pindex_t start, vm_pindex_t size) 10704dcc5c2dSMatthew Dillon { 1071a880104aSDoug Moore struct page_range range; 1072a880104aSDoug Moore daddr_t addr, blk; 1073686aa928SMark Johnston vm_pindex_t i, j; 1074686aa928SMark Johnston int n; 10754dcc5c2dSMatthew Dillon 1076a880104aSDoug Moore swp_pager_init_freerange(&range); 107789f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 107848e98a2aSDoug Moore for (i = 0; i < size; i += n) { 1079686aa928SMark Johnston n = MIN(size - i, INT_MAX); 108036b01270SDoug Moore blk = swp_pager_getswapspace(&n); 108148e98a2aSDoug Moore if (blk == SWAPBLK_NONE) { 1082baa1ccceSKonstantin Belousov swp_pager_meta_free(object, start, i, NULL); 108389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 10844dcc5c2dSMatthew Dillon return (-1); 10854dcc5c2dSMatthew Dillon } 108648e98a2aSDoug Moore for (j = 0; j < n; ++j) { 108748e98a2aSDoug Moore addr = swp_pager_meta_build(object, 108875694e65SDoug Moore start + i + j, blk + j, false); 108978f1deefSAlan Cox if (addr != SWAPBLK_NONE) 1090a880104aSDoug Moore swp_pager_update_freerange(&range, addr); 109148e98a2aSDoug Moore } 10924dcc5c2dSMatthew Dillon } 1093a880104aSDoug Moore swp_pager_freeswapspace(&range); 109489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 10954dcc5c2dSMatthew Dillon return (0); 109626f9a767SRodney W. Grimes } 109726f9a767SRodney W. Grimes 10980a47b48bSJohn Dyson /* 10991c7c3c6aSMatthew Dillon * SWAP_PAGER_COPY() - copy blocks from source pager to destination pager 11001c7c3c6aSMatthew Dillon * and destroy the source. 11011c7c3c6aSMatthew Dillon * 11021c7c3c6aSMatthew Dillon * Copy any valid swapblks from the source to the destination. In 11031c7c3c6aSMatthew Dillon * cases where both the source and destination have a valid swapblk, 11041c7c3c6aSMatthew Dillon * we keep the destination's. 11051c7c3c6aSMatthew Dillon * 110615523cf7SKonstantin Belousov * This routine is allowed to sleep. It may sleep allocating metadata 110798087a06SJeff Roberson * indirectly through swp_pager_meta_build(). 11081c7c3c6aSMatthew Dillon * 11091c7c3c6aSMatthew Dillon * The source object contains no vm_page_t's (which is just as well) 11101c7c3c6aSMatthew Dillon * 111115523cf7SKonstantin Belousov * The source and destination objects must be locked. 111215523cf7SKonstantin Belousov * Both object locks may temporarily be released. 111326f9a767SRodney W. Grimes */ 111426f9a767SRodney W. Grimes void 11152f249180SPoul-Henning Kamp swap_pager_copy(vm_object_t srcobject, vm_object_t dstobject, 11162f249180SPoul-Henning Kamp vm_pindex_t offset, int destroysource) 111726f9a767SRodney W. Grimes { 111889f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(srcobject); 111989f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(dstobject); 11200cddd8f0SMatthew Dillon 112126f9a767SRodney W. Grimes /* 11221c7c3c6aSMatthew Dillon * If destroysource is set, we remove the source object from the 11231c7c3c6aSMatthew Dillon * swap_pager internal queue now. 112426f9a767SRodney W. Grimes */ 112567388836SKonstantin Belousov if (destroysource && (srcobject->flags & OBJ_ANON) == 0 && 112667388836SKonstantin Belousov srcobject->handle != NULL) { 1127eb4d6a1bSKonstantin Belousov VM_OBJECT_WUNLOCK(srcobject); 1128eb4d6a1bSKonstantin Belousov VM_OBJECT_WUNLOCK(dstobject); 1129eb4d6a1bSKonstantin Belousov sx_xlock(&sw_alloc_sx); 1130eb4d6a1bSKonstantin Belousov TAILQ_REMOVE(NOBJLIST(srcobject->handle), srcobject, 1131eb4d6a1bSKonstantin Belousov pager_object_list); 1132eb4d6a1bSKonstantin Belousov sx_xunlock(&sw_alloc_sx); 1133eb4d6a1bSKonstantin Belousov VM_OBJECT_WLOCK(dstobject); 1134eb4d6a1bSKonstantin Belousov VM_OBJECT_WLOCK(srcobject); 1135bd228075SAlan Cox } 113626f9a767SRodney W. Grimes 11371c7c3c6aSMatthew Dillon /* 11384abca9bbSAlan Cox * Transfer source to destination. 11391c7c3c6aSMatthew Dillon */ 114094264705SDoug Moore swp_pager_meta_transfer(srcobject, dstobject, offset, dstobject->size); 114126f9a767SRodney W. Grimes 114226f9a767SRodney W. Grimes /* 11431c7c3c6aSMatthew Dillon * Free left over swap blocks in source. 114426f9a767SRodney W. Grimes */ 1145cb6757c0SMark Johnston if (destroysource) 11461c7c3c6aSMatthew Dillon swp_pager_meta_free_all(srcobject); 114726f9a767SRodney W. Grimes } 114826f9a767SRodney W. Grimes 1149df8bae1dSRodney W. Grimes /* 11501c7c3c6aSMatthew Dillon * SWAP_PAGER_HASPAGE() - determine if we have good backing store for 11511c7c3c6aSMatthew Dillon * the requested page. 11521c7c3c6aSMatthew Dillon * 11531c7c3c6aSMatthew Dillon * We determine whether good backing store exists for the requested 11541c7c3c6aSMatthew Dillon * page and return TRUE if it does, FALSE if it doesn't. 11551c7c3c6aSMatthew Dillon * 11561c7c3c6aSMatthew Dillon * If TRUE, we also try to determine how much valid, contiguous backing 1157915d1b71SMark Johnston * store exists before and after the requested page. 1158df8bae1dSRodney W. Grimes */ 11595ea4972cSAlan Cox static boolean_t 1160915d1b71SMark Johnston swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, 1161915d1b71SMark Johnston int *after) 116226f9a767SRodney W. Grimes { 1163915d1b71SMark Johnston daddr_t blk, blk0; 1164915d1b71SMark Johnston int i; 116526f9a767SRodney W. Grimes 1166c25673ffSAttilio Rao VM_OBJECT_ASSERT_LOCKED(object); 11674b8365d7SKonstantin Belousov KASSERT((object->flags & OBJ_SWAP) != 0, 11688ecbf14bSDoug Moore ("%s: object not swappable", __func__)); 1169915d1b71SMark Johnston 11701c7c3c6aSMatthew Dillon /* 11711c7c3c6aSMatthew Dillon * do we have good backing store at the requested index ? 11721c7c3c6aSMatthew Dillon */ 11738ecbf14bSDoug Moore blk0 = swp_pager_meta_lookup(object, pindex); 11744dcc5c2dSMatthew Dillon if (blk0 == SWAPBLK_NONE) { 11751c7c3c6aSMatthew Dillon if (before) 117624a1cce3SDavid Greenman *before = 0; 11771c7c3c6aSMatthew Dillon if (after) 117824a1cce3SDavid Greenman *after = 0; 117926f9a767SRodney W. Grimes return (FALSE); 118026f9a767SRodney W. Grimes } 118126f9a767SRodney W. Grimes 118226f9a767SRodney W. Grimes /* 11831c7c3c6aSMatthew Dillon * find backwards-looking contiguous good backing store 1184e47ed70bSJohn Dyson */ 11851c7c3c6aSMatthew Dillon if (before != NULL) { 1186915d1b71SMark Johnston for (i = 1; i < SWB_NPAGES; i++) { 11871c7c3c6aSMatthew Dillon if (i > pindex) 11881c7c3c6aSMatthew Dillon break; 11898ecbf14bSDoug Moore blk = swp_pager_meta_lookup(object, pindex - i); 11901c7c3c6aSMatthew Dillon if (blk != blk0 - i) 11911c7c3c6aSMatthew Dillon break; 1192ffc82b0aSJohn Dyson } 1193915d1b71SMark Johnston *before = i - 1; 119426f9a767SRodney W. Grimes } 119526f9a767SRodney W. Grimes 119626f9a767SRodney W. Grimes /* 11971c7c3c6aSMatthew Dillon * find forward-looking contiguous good backing store 119826f9a767SRodney W. Grimes */ 11991c7c3c6aSMatthew Dillon if (after != NULL) { 1200915d1b71SMark Johnston for (i = 1; i < SWB_NPAGES; i++) { 12018ecbf14bSDoug Moore blk = swp_pager_meta_lookup(object, pindex + i); 12021c7c3c6aSMatthew Dillon if (blk != blk0 + i) 12031c7c3c6aSMatthew Dillon break; 120426f9a767SRodney W. Grimes } 1205915d1b71SMark Johnston *after = i - 1; 12061c7c3c6aSMatthew Dillon } 12071c7c3c6aSMatthew Dillon return (TRUE); 12081c7c3c6aSMatthew Dillon } 12091c7c3c6aSMatthew Dillon 1210995730a6SDoug Moore static void 1211995730a6SDoug Moore swap_pager_unswapped_acct(vm_page_t m) 1212995730a6SDoug Moore { 1213995730a6SDoug Moore KASSERT((m->object->flags & OBJ_SWAP) != 0, 1214995730a6SDoug Moore ("Free object not swappable")); 1215995730a6SDoug Moore if ((m->a.flags & PGA_SWAP_FREE) != 0) 1216995730a6SDoug Moore counter_u64_add(swap_free_completed, 1); 1217995730a6SDoug Moore vm_page_aflag_clear(m, PGA_SWAP_FREE | PGA_SWAP_SPACE); 1218995730a6SDoug Moore 1219995730a6SDoug Moore /* 1220995730a6SDoug Moore * The meta data only exists if the object is OBJT_SWAP 1221995730a6SDoug Moore * and even then might not be allocated yet. 1222995730a6SDoug Moore */ 1223995730a6SDoug Moore } 1224995730a6SDoug Moore 12251c7c3c6aSMatthew Dillon /* 12261c7c3c6aSMatthew Dillon * SWAP_PAGER_PAGE_UNSWAPPED() - remove swap backing store related to page 12271c7c3c6aSMatthew Dillon * 12281c7c3c6aSMatthew Dillon * This removes any associated swap backing store, whether valid or 12291c7c3c6aSMatthew Dillon * not, from the page. 12301c7c3c6aSMatthew Dillon * 12311c7c3c6aSMatthew Dillon * This routine is typically called when a page is made dirty, at 12321c7c3c6aSMatthew Dillon * which point any associated swap can be freed. MADV_FREE also 12331c7c3c6aSMatthew Dillon * calls us in a special-case situation 12341c7c3c6aSMatthew Dillon * 12351c7c3c6aSMatthew Dillon * NOTE!!! If the page is clean and the swap was valid, the caller 12361c7c3c6aSMatthew Dillon * should make the page dirty before calling this routine. This routine 12371c7c3c6aSMatthew Dillon * does NOT change the m->dirty status of the page. Also: MADV_FREE 12381c7c3c6aSMatthew Dillon * depends on it. 12391c7c3c6aSMatthew Dillon * 124015523cf7SKonstantin Belousov * This routine may not sleep. 1241c25673ffSAttilio Rao * 1242a8081778SJeff Roberson * The object containing the page may be locked. 12431c7c3c6aSMatthew Dillon */ 12441c7c3c6aSMatthew Dillon static void 12452f249180SPoul-Henning Kamp swap_pager_unswapped(vm_page_t m) 12461c7c3c6aSMatthew Dillon { 1247a880104aSDoug Moore struct page_range range; 12488ecbf14bSDoug Moore struct swblk *sb; 1249a8081778SJeff Roberson vm_object_t obj; 12502f249180SPoul-Henning Kamp 1251a8081778SJeff Roberson /* 1252a8081778SJeff Roberson * Handle enqueing deferred frees first. If we do not have the 1253a8081778SJeff Roberson * object lock we wait for the page daemon to clear the space. 1254a8081778SJeff Roberson */ 1255a8081778SJeff Roberson obj = m->object; 1256a8081778SJeff Roberson if (!VM_OBJECT_WOWNED(obj)) { 1257a8081778SJeff Roberson VM_PAGE_OBJECT_BUSY_ASSERT(m); 1258a8081778SJeff Roberson /* 1259a8081778SJeff Roberson * The caller is responsible for synchronization but we 1260a8081778SJeff Roberson * will harmlessly handle races. This is typically provided 1261a8081778SJeff Roberson * by only calling unswapped() when a page transitions from 1262a8081778SJeff Roberson * clean to dirty. 1263a8081778SJeff Roberson */ 1264a8081778SJeff Roberson if ((m->a.flags & (PGA_SWAP_SPACE | PGA_SWAP_FREE)) == 1265a8081778SJeff Roberson PGA_SWAP_SPACE) { 1266a8081778SJeff Roberson vm_page_aflag_set(m, PGA_SWAP_FREE); 1267a8081778SJeff Roberson counter_u64_add(swap_free_deferred, 1); 1268a8081778SJeff Roberson } 1269a8081778SJeff Roberson return; 1270a8081778SJeff Roberson } 1271995730a6SDoug Moore swap_pager_unswapped_acct(m); 12728ecbf14bSDoug Moore 127394264705SDoug Moore sb = swblk_lookup(m->object, m->pindex); 12748ecbf14bSDoug Moore if (sb == NULL) 12758ecbf14bSDoug Moore return; 1276a880104aSDoug Moore range.start = sb->d[m->pindex % SWAP_META_PAGES]; 1277a880104aSDoug Moore if (range.start == SWAPBLK_NONE) 12788ecbf14bSDoug Moore return; 1279a880104aSDoug Moore range.num = 1; 1280a880104aSDoug Moore swp_pager_freeswapspace(&range); 12818ecbf14bSDoug Moore sb->d[m->pindex % SWAP_META_PAGES] = SWAPBLK_NONE; 12828ecbf14bSDoug Moore swp_pager_free_empty_swblk(m->object, sb); 12831c7c3c6aSMatthew Dillon } 12841c7c3c6aSMatthew Dillon 12851c7c3c6aSMatthew Dillon /* 1286915d1b71SMark Johnston * swap_pager_getpages() - bring pages in from swap 12871c7c3c6aSMatthew Dillon * 12883f060b60SMark Johnston * Attempt to page in the pages in array "ma" of length "count". The 12893f060b60SMark Johnston * caller may optionally specify that additional pages preceding and 12903f060b60SMark Johnston * succeeding the specified range be paged in. The number of such pages 12913f060b60SMark Johnston * is returned in the "rbehind" and "rahead" parameters, and they will 12923f060b60SMark Johnston * be in the inactive queue upon return. 12931c7c3c6aSMatthew Dillon * 12943f060b60SMark Johnston * The pages in "ma" must be busied and will remain busied upon return. 12951c7c3c6aSMatthew Dillon */ 1296f708ef1bSPoul-Henning Kamp static int 1297c90d075bSMark Johnston swap_pager_getpages_locked(vm_object_t object, vm_page_t *ma, int count, 1298c90d075bSMark Johnston int *rbehind, int *rahead) 1299df8bae1dSRodney W. Grimes { 13001c7c3c6aSMatthew Dillon struct buf *bp; 1301b7b8a096SKonstantin Belousov vm_page_t bm, mpred, msucc, p; 1302915d1b71SMark Johnston vm_pindex_t pindex; 13031c7c3c6aSMatthew Dillon daddr_t blk; 1304b7b8a096SKonstantin Belousov int i, maxahead, maxbehind, reqcount; 13050d94caffSDavid Greenman 1306c90d075bSMark Johnston VM_OBJECT_ASSERT_WLOCKED(object); 1307915d1b71SMark Johnston reqcount = count; 13081c7c3c6aSMatthew Dillon 13094b8365d7SKonstantin Belousov KASSERT((object->flags & OBJ_SWAP) != 0, 13108ecbf14bSDoug Moore ("%s: object not swappable", __func__)); 1311d6e13f3bSJeff Roberson if (!swap_pager_haspage(object, ma[0]->pindex, &maxbehind, &maxahead)) { 1312d6e13f3bSJeff Roberson VM_OBJECT_WUNLOCK(object); 1313915d1b71SMark Johnston return (VM_PAGER_FAIL); 1314d6e13f3bSJeff Roberson } 1315915d1b71SMark Johnston 131698087a06SJeff Roberson KASSERT(reqcount - 1 <= maxahead, 131798087a06SJeff Roberson ("page count %d extends beyond swap block", reqcount)); 131898087a06SJeff Roberson 131998087a06SJeff Roberson /* 132098087a06SJeff Roberson * Do not transfer any pages other than those that are xbusied 132198087a06SJeff Roberson * when running during a split or collapse operation. This 132298087a06SJeff Roberson * prevents clustering from re-creating pages which are being 132398087a06SJeff Roberson * moved into another object. 132498087a06SJeff Roberson */ 132598087a06SJeff Roberson if ((object->flags & (OBJ_SPLIT | OBJ_DEAD)) != 0) { 132698087a06SJeff Roberson maxahead = reqcount - 1; 132798087a06SJeff Roberson maxbehind = 0; 132898087a06SJeff Roberson } 132998087a06SJeff Roberson 1330915d1b71SMark Johnston /* 1331915d1b71SMark Johnston * Clip the readahead and readbehind ranges to exclude resident pages. 1332915d1b71SMark Johnston */ 1333915d1b71SMark Johnston if (rahead != NULL) { 1334dd9cb6daSMark Johnston *rahead = imin(*rahead, maxahead - (reqcount - 1)); 13353f060b60SMark Johnston pindex = ma[reqcount - 1]->pindex; 13363f060b60SMark Johnston msucc = TAILQ_NEXT(ma[reqcount - 1], listq); 1337915d1b71SMark Johnston if (msucc != NULL && msucc->pindex - pindex - 1 < *rahead) 1338915d1b71SMark Johnston *rahead = msucc->pindex - pindex - 1; 1339915d1b71SMark Johnston } 1340915d1b71SMark Johnston if (rbehind != NULL) { 1341dd9cb6daSMark Johnston *rbehind = imin(*rbehind, maxbehind); 13423f060b60SMark Johnston pindex = ma[0]->pindex; 13433f060b60SMark Johnston mpred = TAILQ_PREV(ma[0], pglist, listq); 1344915d1b71SMark Johnston if (mpred != NULL && pindex - mpred->pindex - 1 < *rbehind) 1345915d1b71SMark Johnston *rbehind = pindex - mpred->pindex - 1; 1346915d1b71SMark Johnston } 1347915d1b71SMark Johnston 1348b7b8a096SKonstantin Belousov bm = ma[0]; 1349b7b8a096SKonstantin Belousov for (i = 0; i < count; i++) 1350b7b8a096SKonstantin Belousov ma[i]->oflags |= VPO_SWAPINPROG; 1351b7b8a096SKonstantin Belousov 1352915d1b71SMark Johnston /* 1353915d1b71SMark Johnston * Allocate readahead and readbehind pages. 1354915d1b71SMark Johnston */ 1355b7b8a096SKonstantin Belousov if (rbehind != NULL) { 1356b7b8a096SKonstantin Belousov for (i = 1; i <= *rbehind; i++) { 13573f060b60SMark Johnston p = vm_page_alloc(object, ma[0]->pindex - i, 13587667839aSAlan Cox VM_ALLOC_NORMAL); 1359b7b8a096SKonstantin Belousov if (p == NULL) 1360915d1b71SMark Johnston break; 1361b7b8a096SKonstantin Belousov p->oflags |= VPO_SWAPINPROG; 1362b7b8a096SKonstantin Belousov bm = p; 1363915d1b71SMark Johnston } 1364b7b8a096SKonstantin Belousov *rbehind = i - 1; 1365915d1b71SMark Johnston } 1366915d1b71SMark Johnston if (rahead != NULL) { 1367915d1b71SMark Johnston for (i = 0; i < *rahead; i++) { 1368915d1b71SMark Johnston p = vm_page_alloc(object, 13693f060b60SMark Johnston ma[reqcount - 1]->pindex + i + 1, VM_ALLOC_NORMAL); 1370915d1b71SMark Johnston if (p == NULL) 1371915d1b71SMark Johnston break; 1372b7b8a096SKonstantin Belousov p->oflags |= VPO_SWAPINPROG; 1373915d1b71SMark Johnston } 1374915d1b71SMark Johnston *rahead = i; 1375915d1b71SMark Johnston } 1376915d1b71SMark Johnston if (rbehind != NULL) 1377915d1b71SMark Johnston count += *rbehind; 1378915d1b71SMark Johnston if (rahead != NULL) 1379915d1b71SMark Johnston count += *rahead; 1380915d1b71SMark Johnston 1381915d1b71SMark Johnston vm_object_pip_add(object, count); 1382915d1b71SMark Johnston 1383b7b8a096SKonstantin Belousov pindex = bm->pindex; 13848ecbf14bSDoug Moore blk = swp_pager_meta_lookup(object, pindex); 1385915d1b71SMark Johnston KASSERT(blk != SWAPBLK_NONE, 1386915d1b71SMark Johnston ("no swap blocking containing %p(%jx)", object, (uintmax_t)pindex)); 1387915d1b71SMark Johnston 1388915d1b71SMark Johnston VM_OBJECT_WUNLOCK(object); 1389756a5412SGleb Smirnoff bp = uma_zalloc(swrbuf_zone, M_WAITOK); 1390cd853791SKonstantin Belousov MPASS((bp->b_flags & B_MAXPHYS) != 0); 1391b7b8a096SKonstantin Belousov /* Pages cannot leave the object while busy. */ 1392b7b8a096SKonstantin Belousov for (i = 0, p = bm; i < count; i++, p = TAILQ_NEXT(p, listq)) { 1393b7b8a096SKonstantin Belousov MPASS(p->pindex == bm->pindex + i); 1394b7b8a096SKonstantin Belousov bp->b_pages[i] = p; 1395b7b8a096SKonstantin Belousov } 1396915d1b71SMark Johnston 1397915d1b71SMark Johnston bp->b_flags |= B_PAGING; 139821144e3bSPoul-Henning Kamp bp->b_iocmd = BIO_READ; 13991c7c3c6aSMatthew Dillon bp->b_iodone = swp_pager_async_iodone; 1400fdcc1cc0SJohn Baldwin bp->b_rcred = crhold(thread0.td_ucred); 1401fdcc1cc0SJohn Baldwin bp->b_wcred = crhold(thread0.td_ucred); 1402b0cd2017SGleb Smirnoff bp->b_blkno = blk; 1403b0cd2017SGleb Smirnoff bp->b_bcount = PAGE_SIZE * count; 1404b0cd2017SGleb Smirnoff bp->b_bufsize = PAGE_SIZE * count; 1405b0cd2017SGleb Smirnoff bp->b_npages = count; 1406915d1b71SMark Johnston bp->b_pgbefore = rbehind != NULL ? *rbehind : 0; 1407915d1b71SMark Johnston bp->b_pgafter = rahead != NULL ? *rahead : 0; 140826f9a767SRodney W. Grimes 140983c9dea1SGleb Smirnoff VM_CNT_INC(v_swapin); 141083c9dea1SGleb Smirnoff VM_CNT_ADD(v_swappgsin, count); 14111c7c3c6aSMatthew Dillon 14121c7c3c6aSMatthew Dillon /* 14131c7c3c6aSMatthew Dillon * perform the I/O. NOTE!!! bp cannot be considered valid after 14141c7c3c6aSMatthew Dillon * this point because we automatically release it on completion. 14151c7c3c6aSMatthew Dillon * Instead, we look at the one page we are interested in which we 14161c7c3c6aSMatthew Dillon * still hold a lock on even through the I/O completion. 14171c7c3c6aSMatthew Dillon * 14183f060b60SMark Johnston * The other pages in our ma[] array are also released on completion, 14191c7c3c6aSMatthew Dillon * so we cannot assume they are valid anymore either. 14201c7c3c6aSMatthew Dillon * 1421c37a77eeSPoul-Henning Kamp * NOTE: b_blkno is destroyed by the call to swapdev_strategy 14221c7c3c6aSMatthew Dillon */ 1423b890cb2cSPeter Wemm BUF_KERNPROC(bp); 14244b03903aSPoul-Henning Kamp swp_pager_strategy(bp); 142526f9a767SRodney W. Grimes 142626f9a767SRodney W. Grimes /* 1427915d1b71SMark Johnston * Wait for the pages we want to complete. VPO_SWAPINPROG is always 14281c7c3c6aSMatthew Dillon * cleared on completion. If an I/O error occurs, SWAPBLK_NONE 1429915d1b71SMark Johnston * is set in the metadata for each page in the request. 143026f9a767SRodney W. Grimes */ 143189f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1432d6e13f3bSJeff Roberson /* This could be implemented more efficiently with aflags */ 14333f060b60SMark Johnston while ((ma[0]->oflags & VPO_SWAPINPROG) != 0) { 14343f060b60SMark Johnston ma[0]->oflags |= VPO_SWAPSLEEP; 143583c9dea1SGleb Smirnoff VM_CNT_INC(v_intrans); 1436cf27e0d1SJeff Roberson if (VM_OBJECT_SLEEP(object, &object->handle, PSWP, 1437c7aebda8SAttilio Rao "swread", hz * 20)) { 14389bd86a98SBruce M Simpson printf( 1439c5690651SPoul-Henning Kamp "swap_pager: indefinite wait buffer: bufobj: %p, blkno: %jd, size: %ld\n", 1440c5690651SPoul-Henning Kamp bp->b_bufobj, (intmax_t)bp->b_blkno, bp->b_bcount); 14411c7c3c6aSMatthew Dillon } 14421b119d9dSDavid Greenman } 1443d6e13f3bSJeff Roberson VM_OBJECT_WUNLOCK(object); 144426f9a767SRodney W. Grimes 144526f9a767SRodney W. Grimes /* 1446b0cd2017SGleb Smirnoff * If we had an unrecoverable read error pages will not be valid. 144726f9a767SRodney W. Grimes */ 1448915d1b71SMark Johnston for (i = 0; i < reqcount; i++) 14493f060b60SMark Johnston if (ma[i]->valid != VM_PAGE_BITS_ALL) 14501c7c3c6aSMatthew Dillon return (VM_PAGER_ERROR); 1451b0cd2017SGleb Smirnoff 14521c7c3c6aSMatthew Dillon return (VM_PAGER_OK); 14531c7c3c6aSMatthew Dillon 14541c7c3c6aSMatthew Dillon /* 14551c7c3c6aSMatthew Dillon * A final note: in a low swap situation, we cannot deallocate swap 14561c7c3c6aSMatthew Dillon * and mark a page dirty here because the caller is likely to mark 14571c7c3c6aSMatthew Dillon * the page clean when we return, causing the page to possibly revert 14581c7c3c6aSMatthew Dillon * to all-zero's later. 14591c7c3c6aSMatthew Dillon */ 1460df8bae1dSRodney W. Grimes } 1461df8bae1dSRodney W. Grimes 1462c90d075bSMark Johnston static int 1463c90d075bSMark Johnston swap_pager_getpages(vm_object_t object, vm_page_t *ma, int count, 1464c90d075bSMark Johnston int *rbehind, int *rahead) 1465c90d075bSMark Johnston { 1466c90d075bSMark Johnston 1467c90d075bSMark Johnston VM_OBJECT_WLOCK(object); 1468c90d075bSMark Johnston return (swap_pager_getpages_locked(object, ma, count, rbehind, rahead)); 1469c90d075bSMark Johnston } 1470c90d075bSMark Johnston 14711c7c3c6aSMatthew Dillon /* 147290effb23SGleb Smirnoff * swap_pager_getpages_async(): 147390effb23SGleb Smirnoff * 147490effb23SGleb Smirnoff * Right now this is emulation of asynchronous operation on top of 147590effb23SGleb Smirnoff * swap_pager_getpages(). 147690effb23SGleb Smirnoff */ 147790effb23SGleb Smirnoff static int 14783f060b60SMark Johnston swap_pager_getpages_async(vm_object_t object, vm_page_t *ma, int count, 1479b0cd2017SGleb Smirnoff int *rbehind, int *rahead, pgo_getpages_iodone_t iodone, void *arg) 148090effb23SGleb Smirnoff { 148190effb23SGleb Smirnoff int r, error; 148290effb23SGleb Smirnoff 14833f060b60SMark Johnston r = swap_pager_getpages(object, ma, count, rbehind, rahead); 148490effb23SGleb Smirnoff switch (r) { 148590effb23SGleb Smirnoff case VM_PAGER_OK: 148690effb23SGleb Smirnoff error = 0; 148790effb23SGleb Smirnoff break; 148890effb23SGleb Smirnoff case VM_PAGER_ERROR: 148990effb23SGleb Smirnoff error = EIO; 149090effb23SGleb Smirnoff break; 149190effb23SGleb Smirnoff case VM_PAGER_FAIL: 149290effb23SGleb Smirnoff error = EINVAL; 149390effb23SGleb Smirnoff break; 149490effb23SGleb Smirnoff default: 1495d9328101SGleb Smirnoff panic("unhandled swap_pager_getpages() error %d", r); 149690effb23SGleb Smirnoff } 14973f060b60SMark Johnston (iodone)(arg, ma, count, error); 149890effb23SGleb Smirnoff 149990effb23SGleb Smirnoff return (r); 150090effb23SGleb Smirnoff } 150190effb23SGleb Smirnoff 150290effb23SGleb Smirnoff /* 15031c7c3c6aSMatthew Dillon * swap_pager_putpages: 15041c7c3c6aSMatthew Dillon * 15051c7c3c6aSMatthew Dillon * Assign swap (if necessary) and initiate I/O on the specified pages. 15061c7c3c6aSMatthew Dillon * 1507ea3aecf5SPeter Wemm * In a low memory situation we may block in VOP_STRATEGY(), but the new 15081c7c3c6aSMatthew Dillon * vm_page reservation system coupled with properly written VFS devices 15091c7c3c6aSMatthew Dillon * should ensure that no low-memory deadlock occurs. This is an area 15101c7c3c6aSMatthew Dillon * which needs work. 15111c7c3c6aSMatthew Dillon * 15121c7c3c6aSMatthew Dillon * The parent has N vm_object_pip_add() references prior to 15131c7c3c6aSMatthew Dillon * calling us and will remove references for rtvals[] that are 15141c7c3c6aSMatthew Dillon * not set to VM_PAGER_PEND. We need to remove the rest on I/O 15151c7c3c6aSMatthew Dillon * completion. 15161c7c3c6aSMatthew Dillon * 15171c7c3c6aSMatthew Dillon * The parent has soft-busy'd the pages it passes us and will unbusy 151823612f0dSDoug Moore * those whose rtvals[] entry is not set to VM_PAGER_PEND on return. 15191c7c3c6aSMatthew Dillon * We need to unbusy the rest on I/O completion. 15201c7c3c6aSMatthew Dillon */ 1521d635a37fSWarner Losh static void 15223f060b60SMark Johnston swap_pager_putpages(vm_object_t object, vm_page_t *ma, int count, 1523e065e87cSKonstantin Belousov int flags, int *rtvals) 1524df8bae1dSRodney W. Grimes { 1525a880104aSDoug Moore struct page_range range; 152623612f0dSDoug Moore struct buf *bp; 1527a880104aSDoug Moore daddr_t addr, blk; 152823612f0dSDoug Moore vm_page_t mreq; 152923612f0dSDoug Moore int i, j, n; 153023612f0dSDoug Moore bool async; 1531df8bae1dSRodney W. Grimes 153223612f0dSDoug Moore KASSERT(count == 0 || ma[0]->object == object, 153323612f0dSDoug Moore ("%s: object mismatch %p/%p", 153423612f0dSDoug Moore __func__, object, ma[0]->object)); 1535ee3dc7d7SAlan Cox 153689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 153723612f0dSDoug Moore async = curproc == pageproc && (flags & VM_PAGER_PUT_SYNC) == 0; 1538a880104aSDoug Moore swp_pager_init_freerange(&range); 153926f9a767SRodney W. Grimes 15401c7c3c6aSMatthew Dillon /* 15411c7c3c6aSMatthew Dillon * Assign swap blocks and issue I/O. We reallocate swap on the fly. 15421c7c3c6aSMatthew Dillon * The page is left dirty until the pageout operation completes 15431c7c3c6aSMatthew Dillon * successfully. 15441c7c3c6aSMatthew Dillon */ 15451c7c3c6aSMatthew Dillon for (i = 0; i < count; i += n) { 154631c82722SDoug Moore /* Maximum I/O size is limited by maximum swap block size. */ 154731c82722SDoug Moore n = min(count - i, nsw_cluster_max); 15481c7c3c6aSMatthew Dillon 154923612f0dSDoug Moore if (async) { 1550756a5412SGleb Smirnoff mtx_lock(&swbuf_mtx); 1551756a5412SGleb Smirnoff while (nsw_wcount_async == 0) 1552756a5412SGleb Smirnoff msleep(&nsw_wcount_async, &swbuf_mtx, PVM, 1553756a5412SGleb Smirnoff "swbufa", 0); 1554756a5412SGleb Smirnoff nsw_wcount_async--; 1555756a5412SGleb Smirnoff mtx_unlock(&swbuf_mtx); 1556327f4e83SMatthew Dillon } 1557725b4ff0SMark Johnston 1558725b4ff0SMark Johnston /* Get a block of swap of size up to size n. */ 155936b01270SDoug Moore blk = swp_pager_getswapspace(&n); 1560725b4ff0SMark Johnston if (blk == SWAPBLK_NONE) { 1561725b4ff0SMark Johnston mtx_lock(&swbuf_mtx); 1562725b4ff0SMark Johnston if (++nsw_wcount_async == 1) 1563725b4ff0SMark Johnston wakeup(&nsw_wcount_async); 1564725b4ff0SMark Johnston mtx_unlock(&swbuf_mtx); 1565725b4ff0SMark Johnston for (j = 0; j < n; ++j) 1566725b4ff0SMark Johnston rtvals[i + j] = VM_PAGER_FAIL; 1567725b4ff0SMark Johnston continue; 1568725b4ff0SMark Johnston } 156954291f7dSAlan Cox VM_OBJECT_WLOCK(object); 1570725b4ff0SMark Johnston for (j = 0; j < n; ++j) { 1571725b4ff0SMark Johnston mreq = ma[i + j]; 1572725b4ff0SMark Johnston vm_page_aflag_clear(mreq, PGA_SWAP_FREE); 1573725b4ff0SMark Johnston addr = swp_pager_meta_build(mreq->object, mreq->pindex, 157475694e65SDoug Moore blk + j, false); 1575725b4ff0SMark Johnston if (addr != SWAPBLK_NONE) 1576a880104aSDoug Moore swp_pager_update_freerange(&range, addr); 1577725b4ff0SMark Johnston MPASS(mreq->dirty == VM_PAGE_BITS_ALL); 1578725b4ff0SMark Johnston mreq->oflags |= VPO_SWAPINPROG; 1579725b4ff0SMark Johnston } 1580725b4ff0SMark Johnston VM_OBJECT_WUNLOCK(object); 1581725b4ff0SMark Johnston 1582756a5412SGleb Smirnoff bp = uma_zalloc(swwbuf_zone, M_WAITOK); 1583cd853791SKonstantin Belousov MPASS((bp->b_flags & B_MAXPHYS) != 0); 158423612f0dSDoug Moore if (async) 1585cd853791SKonstantin Belousov bp->b_flags |= B_ASYNC; 15865e04322aSPoul-Henning Kamp bp->b_flags |= B_PAGING; 1587912e4ae9SPoul-Henning Kamp bp->b_iocmd = BIO_WRITE; 158826f9a767SRodney W. Grimes 1589fdcc1cc0SJohn Baldwin bp->b_rcred = crhold(thread0.td_ucred); 1590fdcc1cc0SJohn Baldwin bp->b_wcred = crhold(thread0.td_ucred); 15911c7c3c6aSMatthew Dillon bp->b_bcount = PAGE_SIZE * n; 15921c7c3c6aSMatthew Dillon bp->b_bufsize = PAGE_SIZE * n; 15931c7c3c6aSMatthew Dillon bp->b_blkno = blk; 1594725b4ff0SMark Johnston for (j = 0; j < n; j++) 1595725b4ff0SMark Johnston bp->b_pages[j] = ma[i + j]; 15961c7c3c6aSMatthew Dillon bp->b_npages = n; 1597725b4ff0SMark Johnston 1598a5296b05SJulian Elischer /* 1599a5296b05SJulian Elischer * Must set dirty range for NFS to work. 1600a5296b05SJulian Elischer */ 1601a5296b05SJulian Elischer bp->b_dirtyoff = 0; 1602a5296b05SJulian Elischer bp->b_dirtyend = bp->b_bcount; 16031c7c3c6aSMatthew Dillon 160483c9dea1SGleb Smirnoff VM_CNT_INC(v_swapout); 160583c9dea1SGleb Smirnoff VM_CNT_ADD(v_swappgsout, bp->b_npages); 160626f9a767SRodney W. Grimes 160726f9a767SRodney W. Grimes /* 160877923df2SAlan Cox * We unconditionally set rtvals[] to VM_PAGER_PEND so that we 160977923df2SAlan Cox * can call the async completion routine at the end of a 161077923df2SAlan Cox * synchronous I/O operation. Otherwise, our caller would 161177923df2SAlan Cox * perform duplicate unbusy and wakeup operations on the page 161277923df2SAlan Cox * and object, respectively. 161377923df2SAlan Cox */ 161477923df2SAlan Cox for (j = 0; j < n; j++) 161577923df2SAlan Cox rtvals[i + j] = VM_PAGER_PEND; 161677923df2SAlan Cox 161777923df2SAlan Cox /* 16181c7c3c6aSMatthew Dillon * asynchronous 16191c7c3c6aSMatthew Dillon * 162023612f0dSDoug Moore * NOTE: b_blkno is destroyed by the call to swapdev_strategy. 162126f9a767SRodney W. Grimes */ 162223612f0dSDoug Moore if (async) { 16231c7c3c6aSMatthew Dillon bp->b_iodone = swp_pager_async_iodone; 162467812eacSKirk McKusick BUF_KERNPROC(bp); 16254b03903aSPoul-Henning Kamp swp_pager_strategy(bp); 16261c7c3c6aSMatthew Dillon continue; 162726f9a767SRodney W. Grimes } 1628e47ed70bSJohn Dyson 162926f9a767SRodney W. Grimes /* 16301c7c3c6aSMatthew Dillon * synchronous 16311c7c3c6aSMatthew Dillon * 163223612f0dSDoug Moore * NOTE: b_blkno is destroyed by the call to swapdev_strategy. 16331c7c3c6aSMatthew Dillon */ 16342c840b1fSAlan Cox bp->b_iodone = bdone; 16354b03903aSPoul-Henning Kamp swp_pager_strategy(bp); 16361c7c3c6aSMatthew Dillon 16371c7c3c6aSMatthew Dillon /* 163877923df2SAlan Cox * Wait for the sync I/O to complete. 163926f9a767SRodney W. Grimes */ 16402c840b1fSAlan Cox bwait(bp, PVM, "swwrt"); 164177923df2SAlan Cox 16421c7c3c6aSMatthew Dillon /* 16431c7c3c6aSMatthew Dillon * Now that we are through with the bp, we can call the 16441c7c3c6aSMatthew Dillon * normal async completion, which frees everything up. 16451c7c3c6aSMatthew Dillon */ 16461c7c3c6aSMatthew Dillon swp_pager_async_iodone(bp); 16471c7c3c6aSMatthew Dillon } 1648a880104aSDoug Moore swp_pager_freeswapspace(&range); 164923612f0dSDoug Moore VM_OBJECT_WLOCK(object); 16501c7c3c6aSMatthew Dillon } 16511c7c3c6aSMatthew Dillon 16521c7c3c6aSMatthew Dillon /* 16531c7c3c6aSMatthew Dillon * swp_pager_async_iodone: 16541c7c3c6aSMatthew Dillon * 16551c7c3c6aSMatthew Dillon * Completion routine for asynchronous reads and writes from/to swap. 16561c7c3c6aSMatthew Dillon * Also called manually by synchronous code to finish up a bp. 16571c7c3c6aSMatthew Dillon * 165815523cf7SKonstantin Belousov * This routine may not sleep. 16591c7c3c6aSMatthew Dillon */ 16601c7c3c6aSMatthew Dillon static void 16612f249180SPoul-Henning Kamp swp_pager_async_iodone(struct buf *bp) 16621c7c3c6aSMatthew Dillon { 16631c7c3c6aSMatthew Dillon int i; 16641c7c3c6aSMatthew Dillon vm_object_t object = NULL; 16651c7c3c6aSMatthew Dillon 16661c7c3c6aSMatthew Dillon /* 16670b208315SEdward Tomasz Napierala * Report error - unless we ran out of memory, in which case 16680b208315SEdward Tomasz Napierala * we've already logged it in swapgeom_strategy(). 16691c7c3c6aSMatthew Dillon */ 16700b208315SEdward Tomasz Napierala if (bp->b_ioflags & BIO_ERROR && bp->b_error != ENOMEM) { 16711c7c3c6aSMatthew Dillon printf( 16721c7c3c6aSMatthew Dillon "swap_pager: I/O error - %s failed; blkno %ld," 16731c7c3c6aSMatthew Dillon "size %ld, error %d\n", 167421144e3bSPoul-Henning Kamp ((bp->b_iocmd == BIO_READ) ? "pagein" : "pageout"), 16751c7c3c6aSMatthew Dillon (long)bp->b_blkno, 16761c7c3c6aSMatthew Dillon (long)bp->b_bcount, 16771c7c3c6aSMatthew Dillon bp->b_error 16781c7c3c6aSMatthew Dillon ); 16791c7c3c6aSMatthew Dillon } 16801c7c3c6aSMatthew Dillon 16811c7c3c6aSMatthew Dillon /* 168226f9a767SRodney W. Grimes * remove the mapping for kernel virtual 168326f9a767SRodney W. Grimes */ 1684fade8dd7SJeff Roberson if (buf_mapped(bp)) 16851c7c3c6aSMatthew Dillon pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages); 1686fade8dd7SJeff Roberson else 1687fade8dd7SJeff Roberson bp->b_data = bp->b_kvabase; 168826f9a767SRodney W. Grimes 168933a609ecSAlan Cox if (bp->b_npages) { 169033a609ecSAlan Cox object = bp->b_pages[0]->object; 169189f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 169233a609ecSAlan Cox } 16932965a453SKip Macy 169426f9a767SRodney W. Grimes /* 16951c7c3c6aSMatthew Dillon * cleanup pages. If an error occurs writing to swap, we are in 16961c7c3c6aSMatthew Dillon * very serious trouble. If it happens to be a disk error, though, 16971c7c3c6aSMatthew Dillon * we may be able to recover by reassigning the swap later on. So 16981c7c3c6aSMatthew Dillon * in this case we remove the m->swapblk assignment for the page 16991c7c3c6aSMatthew Dillon * but do not free it in the rlist. The errornous block(s) are thus 17001c7c3c6aSMatthew Dillon * never reallocated as swap. Redirty the page and continue. 170126f9a767SRodney W. Grimes */ 17021c7c3c6aSMatthew Dillon for (i = 0; i < bp->b_npages; ++i) { 17031c7c3c6aSMatthew Dillon vm_page_t m = bp->b_pages[i]; 1704e47ed70bSJohn Dyson 17055786be7cSAlan Cox m->oflags &= ~VPO_SWAPINPROG; 1706c7aebda8SAttilio Rao if (m->oflags & VPO_SWAPSLEEP) { 1707c7aebda8SAttilio Rao m->oflags &= ~VPO_SWAPSLEEP; 1708cf27e0d1SJeff Roberson wakeup(&object->handle); 1709c7aebda8SAttilio Rao } 1710e47ed70bSJohn Dyson 1711a8081778SJeff Roberson /* We always have space after I/O, successful or not. */ 1712a8081778SJeff Roberson vm_page_aflag_set(m, PGA_SWAP_SPACE); 1713a8081778SJeff Roberson 1714c244d2deSPoul-Henning Kamp if (bp->b_ioflags & BIO_ERROR) { 1715ffc82b0aSJohn Dyson /* 17161c7c3c6aSMatthew Dillon * If an error occurs I'd love to throw the swapblk 17171c7c3c6aSMatthew Dillon * away without freeing it back to swapspace, so it 17181c7c3c6aSMatthew Dillon * can never be used again. But I can't from an 17191c7c3c6aSMatthew Dillon * interrupt. 1720ffc82b0aSJohn Dyson */ 172121144e3bSPoul-Henning Kamp if (bp->b_iocmd == BIO_READ) { 17221c7c3c6aSMatthew Dillon /* 17231c7c3c6aSMatthew Dillon * NOTE: for reads, m->dirty will probably 1724956f3135SPhilippe Charnier * be overridden by the original caller of 17251c7c3c6aSMatthew Dillon * getpages so don't play cute tricks here. 17261c7c3c6aSMatthew Dillon */ 17270012f373SJeff Roberson vm_page_invalid(m); 172846966507SMark Johnston if (i < bp->b_pgbefore || 172946966507SMark Johnston i >= bp->b_npages - bp->b_pgafter) 173046966507SMark Johnston vm_page_free_invalid(m); 17311c7c3c6aSMatthew Dillon } else { 17321c7c3c6aSMatthew Dillon /* 17331c7c3c6aSMatthew Dillon * If a write error occurs, reactivate page 17341c7c3c6aSMatthew Dillon * so it doesn't clog the inactive list, 17351c7c3c6aSMatthew Dillon * then finish the I/O. 17361c7c3c6aSMatthew Dillon */ 173741e5a226SAlan Cox MPASS(m->dirty == VM_PAGE_BITS_ALL); 17389f5632e6SMark Johnston 1739a8081778SJeff Roberson /* PQ_UNSWAPPABLE? */ 17401c7c3c6aSMatthew Dillon vm_page_activate(m); 1741c7aebda8SAttilio Rao vm_page_sunbusy(m); 17421c7c3c6aSMatthew Dillon } 174321144e3bSPoul-Henning Kamp } else if (bp->b_iocmd == BIO_READ) { 17441c7c3c6aSMatthew Dillon /* 17451c7c3c6aSMatthew Dillon * NOTE: for reads, m->dirty will probably be 1746956f3135SPhilippe Charnier * overridden by the original caller of getpages so 17471c7c3c6aSMatthew Dillon * we cannot set them in order to free the underlying 17481c7c3c6aSMatthew Dillon * swap in a low-swap situation. I don't think we'd 17491c7c3c6aSMatthew Dillon * want to do that anyway, but it was an optimization 17501c7c3c6aSMatthew Dillon * that existed in the old swapper for a time before 17511c7c3c6aSMatthew Dillon * it got ripped out due to precisely this problem. 17521c7c3c6aSMatthew Dillon */ 1753016a3c93SAlan Cox KASSERT(!pmap_page_is_mapped(m), 1754016a3c93SAlan Cox ("swp_pager_async_iodone: page %p is mapped", m)); 1755016a3c93SAlan Cox KASSERT(m->dirty == 0, 1756016a3c93SAlan Cox ("swp_pager_async_iodone: page %p is dirty", m)); 1757915d1b71SMark Johnston 17580012f373SJeff Roberson vm_page_valid(m); 1759915d1b71SMark Johnston if (i < bp->b_pgbefore || 1760915d1b71SMark Johnston i >= bp->b_npages - bp->b_pgafter) 1761915d1b71SMark Johnston vm_page_readahead_finish(m); 17621c7c3c6aSMatthew Dillon } else { 17631c7c3c6aSMatthew Dillon /* 1764016a3c93SAlan Cox * For write success, clear the dirty 17651c7c3c6aSMatthew Dillon * status, then finish the I/O ( which decrements the 17661c7c3c6aSMatthew Dillon * busy count and possibly wakes waiter's up ). 1767ebcddc72SAlan Cox * A page is only written to swap after a period of 1768ebcddc72SAlan Cox * inactivity. Therefore, we do not expect it to be 1769ebcddc72SAlan Cox * reused. 17701c7c3c6aSMatthew Dillon */ 17716031c68dSAlan Cox KASSERT(!pmap_page_is_write_mapped(m), 1772016a3c93SAlan Cox ("swp_pager_async_iodone: page %p is not write" 1773016a3c93SAlan Cox " protected", m)); 1774c52e7044SAlan Cox vm_page_undirty(m); 1775ebcddc72SAlan Cox vm_page_deactivate_noreuse(m); 1776ebcddc72SAlan Cox vm_page_sunbusy(m); 17773c4a2440SAlan Cox } 17783c4a2440SAlan Cox } 177926f9a767SRodney W. Grimes 17801c7c3c6aSMatthew Dillon /* 17811c7c3c6aSMatthew Dillon * adjust pip. NOTE: the original parent may still have its own 17821c7c3c6aSMatthew Dillon * pip refs on the object. 17831c7c3c6aSMatthew Dillon */ 17840d420ad3SAlan Cox if (object != NULL) { 17851c7c3c6aSMatthew Dillon vm_object_pip_wakeupn(object, bp->b_npages); 178689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 17870d420ad3SAlan Cox } 178826f9a767SRodney W. Grimes 17891c7c3c6aSMatthew Dillon /* 1790100650deSOlivier Houchard * swapdev_strategy() manually sets b_vp and b_bufobj before calling 1791100650deSOlivier Houchard * bstrategy(). Set them back to NULL now we're done with it, or we'll 1792100650deSOlivier Houchard * trigger a KASSERT in relpbuf(). 1793100650deSOlivier Houchard */ 1794100650deSOlivier Houchard if (bp->b_vp) { 1795100650deSOlivier Houchard bp->b_vp = NULL; 1796100650deSOlivier Houchard bp->b_bufobj = NULL; 1797100650deSOlivier Houchard } 1798100650deSOlivier Houchard /* 17991c7c3c6aSMatthew Dillon * release the physical I/O buffer 18001c7c3c6aSMatthew Dillon */ 1801756a5412SGleb Smirnoff if (bp->b_flags & B_ASYNC) { 1802756a5412SGleb Smirnoff mtx_lock(&swbuf_mtx); 1803756a5412SGleb Smirnoff if (++nsw_wcount_async == 1) 1804756a5412SGleb Smirnoff wakeup(&nsw_wcount_async); 1805756a5412SGleb Smirnoff mtx_unlock(&swbuf_mtx); 1806756a5412SGleb Smirnoff } 1807756a5412SGleb Smirnoff uma_zfree((bp->b_iocmd == BIO_READ) ? swrbuf_zone : swwbuf_zone, bp); 180826f9a767SRodney W. Grimes } 18091c7c3c6aSMatthew Dillon 1810b1fd102eSMark Johnston int 1811b1fd102eSMark Johnston swap_pager_nswapdev(void) 1812b1fd102eSMark Johnston { 1813b1fd102eSMark Johnston 1814b1fd102eSMark Johnston return (nswapdev); 1815b1fd102eSMark Johnston } 1816b1fd102eSMark Johnston 18177c022327SDoug Moore static void 1818995730a6SDoug Moore swp_pager_force_dirty(struct page_range *range, vm_page_t m, daddr_t *blk) 18197c022327SDoug Moore { 18207c022327SDoug Moore vm_page_dirty(m); 1821995730a6SDoug Moore swap_pager_unswapped_acct(m); 1822995730a6SDoug Moore swp_pager_update_freerange(range, *blk); 1823995730a6SDoug Moore *blk = SWAPBLK_NONE; 18247c022327SDoug Moore vm_page_launder(m); 182592da00bbSMatthew Dillon } 182692da00bbSMatthew Dillon 1827ecfbddf0SKonstantin Belousov u_long 1828ecfbddf0SKonstantin Belousov swap_pager_swapped_pages(vm_object_t object) 1829ecfbddf0SKonstantin Belousov { 1830ecfbddf0SKonstantin Belousov struct swblk *sb; 1831ecfbddf0SKonstantin Belousov u_long res; 1832ecfbddf0SKonstantin Belousov int i; 1833ecfbddf0SKonstantin Belousov 1834ecfbddf0SKonstantin Belousov VM_OBJECT_ASSERT_LOCKED(object); 1835cb6757c0SMark Johnston 183694264705SDoug Moore if (swblk_is_empty(object)) 1837ecfbddf0SKonstantin Belousov return (0); 1838ecfbddf0SKonstantin Belousov 183994264705SDoug Moore res = 0; 184094264705SDoug Moore for (sb = swblk_start(object, 0); sb != NULL; 184194264705SDoug Moore sb = swblk_next(object, sb)) { 1842ecfbddf0SKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) { 1843ecfbddf0SKonstantin Belousov if (sb->d[i] != SWAPBLK_NONE) 1844ecfbddf0SKonstantin Belousov res++; 1845ecfbddf0SKonstantin Belousov } 1846ecfbddf0SKonstantin Belousov } 1847ecfbddf0SKonstantin Belousov return (res); 1848ecfbddf0SKonstantin Belousov } 1849ecfbddf0SKonstantin Belousov 185092da00bbSMatthew Dillon /* 18517c022327SDoug Moore * swap_pager_swapoff_object: 18527c022327SDoug Moore * 18537c022327SDoug Moore * Page in all of the pages that have been paged out for an object 185456948d17SDoug Moore * to a swap device. 18557c022327SDoug Moore */ 18567c022327SDoug Moore static void 18577c022327SDoug Moore swap_pager_swapoff_object(struct swdevt *sp, vm_object_t object) 18587c022327SDoug Moore { 1859995730a6SDoug Moore struct page_range range; 18607c022327SDoug Moore struct swblk *sb; 1861c90d075bSMark Johnston vm_page_t m; 1862c90d075bSMark Johnston vm_pindex_t pi; 1863c90d075bSMark Johnston daddr_t blk; 1864995730a6SDoug Moore int i, rahead, rv; 1865995730a6SDoug Moore bool sb_empty; 18667c022327SDoug Moore 1867995730a6SDoug Moore VM_OBJECT_ASSERT_WLOCKED(object); 18684b8365d7SKonstantin Belousov KASSERT((object->flags & OBJ_SWAP) != 0, 18698ecbf14bSDoug Moore ("%s: Object not swappable", __func__)); 1870c90d075bSMark Johnston 1871995730a6SDoug Moore pi = 0; 1872995730a6SDoug Moore i = 0; 1873995730a6SDoug Moore swp_pager_init_freerange(&range); 1874995730a6SDoug Moore for (;;) { 1875995730a6SDoug Moore if (i == 0 && (object->flags & OBJ_DEAD) != 0) { 1876c90d075bSMark Johnston /* 1877c90d075bSMark Johnston * Make sure that pending writes finish before 1878c90d075bSMark Johnston * returning. 1879c90d075bSMark Johnston */ 1880c90d075bSMark Johnston vm_object_pip_wait(object, "swpoff"); 1881c90d075bSMark Johnston swp_pager_meta_free_all(object); 1882c90d075bSMark Johnston break; 1883c90d075bSMark Johnston } 1884995730a6SDoug Moore 1885995730a6SDoug Moore if (i == SWAP_META_PAGES) { 1886995730a6SDoug Moore pi = sb->p + SWAP_META_PAGES; 1887995730a6SDoug Moore if (sb_empty) { 188894264705SDoug Moore swblk_lookup_remove(object, sb); 1889995730a6SDoug Moore uma_zfree(swblk_zone, sb); 189056948d17SDoug Moore } 1891995730a6SDoug Moore i = 0; 1892995730a6SDoug Moore } 1893995730a6SDoug Moore 1894995730a6SDoug Moore if (i == 0) { 189594264705SDoug Moore sb = swblk_start(object, pi); 1896995730a6SDoug Moore if (sb == NULL) 1897995730a6SDoug Moore break; 1898995730a6SDoug Moore sb_empty = true; 1899995730a6SDoug Moore m = NULL; 1900995730a6SDoug Moore } 1901995730a6SDoug Moore 1902995730a6SDoug Moore /* Skip an invalid block. */ 1903995730a6SDoug Moore blk = sb->d[i]; 1904995730a6SDoug Moore if (blk == SWAPBLK_NONE || !swp_pager_isondev(blk, sp)) { 1905995730a6SDoug Moore if (blk != SWAPBLK_NONE) 1906995730a6SDoug Moore sb_empty = false; 1907995730a6SDoug Moore m = NULL; 1908995730a6SDoug Moore i++; 19097c022327SDoug Moore continue; 1910995730a6SDoug Moore } 191156948d17SDoug Moore 191256948d17SDoug Moore /* 1913995730a6SDoug Moore * Look for a page corresponding to this block, If the found 1914995730a6SDoug Moore * page has pending operations, sleep and restart the scan. 191556948d17SDoug Moore */ 1916995730a6SDoug Moore m = m != NULL ? vm_page_next(m) : 1917995730a6SDoug Moore vm_page_lookup(object, sb->p + i); 1918995730a6SDoug Moore if (m != NULL && (m->oflags & VPO_SWAPINPROG) != 0) { 1919995730a6SDoug Moore m->oflags |= VPO_SWAPSLEEP; 1920995730a6SDoug Moore VM_OBJECT_SLEEP(object, &object->handle, PSWP, "swpoff", 1921995730a6SDoug Moore 0); 1922995730a6SDoug Moore i = 0; /* Restart scan after object lock dropped. */ 1923995730a6SDoug Moore continue; 1924995730a6SDoug Moore } 1925995730a6SDoug Moore 1926995730a6SDoug Moore /* 1927995730a6SDoug Moore * If the found page is valid, mark it dirty and free the swap 1928995730a6SDoug Moore * block. 1929995730a6SDoug Moore */ 1930995730a6SDoug Moore if (m != NULL && vm_page_all_valid(m)) { 1931995730a6SDoug Moore swp_pager_force_dirty(&range, m, &sb->d[i]); 1932995730a6SDoug Moore i++; 1933995730a6SDoug Moore continue; 1934995730a6SDoug Moore } 1935995730a6SDoug Moore 1936995730a6SDoug Moore /* Is there a page we can acquire or allocate? */ 1937c90d075bSMark Johnston if (m == NULL) { 1938c90d075bSMark Johnston m = vm_page_alloc(object, sb->p + i, 1939c90d075bSMark Johnston VM_ALLOC_NORMAL | VM_ALLOC_WAITFAIL); 1940995730a6SDoug Moore } else if (!vm_page_busy_acquire(m, VM_ALLOC_WAITFAIL)) 1941995730a6SDoug Moore m = NULL; 194256948d17SDoug Moore 1943995730a6SDoug Moore /* If no page available, repeat this iteration. */ 1944995730a6SDoug Moore if (m == NULL) 1945995730a6SDoug Moore continue; 1946995730a6SDoug Moore 1947995730a6SDoug Moore /* Get the page from swap, mark it dirty, restart the scan. */ 1948c90d075bSMark Johnston vm_object_pip_add(object, 1); 1949c90d075bSMark Johnston rahead = SWAP_META_PAGES; 1950995730a6SDoug Moore rv = swap_pager_getpages_locked(object, &m, 1, NULL, &rahead); 1951c90d075bSMark Johnston if (rv != VM_PAGER_OK) 1952995730a6SDoug Moore panic("%s: read from swap failed: %d", __func__, rv); 1953c90d075bSMark Johnston VM_OBJECT_WLOCK(object); 1954e61568aeSMark Johnston vm_object_pip_wakeupn(object, 1); 1955995730a6SDoug Moore KASSERT(vm_page_all_valid(m), 1956995730a6SDoug Moore ("%s: Page %p not all valid", __func__, m)); 1957995730a6SDoug Moore swp_pager_force_dirty(&range, m, &sb->d[i]); 1958c90d075bSMark Johnston vm_page_xunbusy(m); 1959995730a6SDoug Moore i = 0; /* Restart scan after object lock dropped. */ 196056948d17SDoug Moore } 1961995730a6SDoug Moore swp_pager_freeswapspace(&range); 19627c022327SDoug Moore } 19637c022327SDoug Moore 19647c022327SDoug Moore /* 196592da00bbSMatthew Dillon * swap_pager_swapoff: 196692da00bbSMatthew Dillon * 196792da00bbSMatthew Dillon * Page in all of the pages that have been paged out to the 196892da00bbSMatthew Dillon * given device. The corresponding blocks in the bitmap must be 196992da00bbSMatthew Dillon * marked as allocated and the device must be flagged SW_CLOSING. 197092da00bbSMatthew Dillon * There may be no processes swapped out to the device. 197192da00bbSMatthew Dillon * 197292da00bbSMatthew Dillon * This routine may block. 197392da00bbSMatthew Dillon */ 1974e9c0cc15SPoul-Henning Kamp static void 1975b3fed13eSDavid Schultz swap_pager_swapoff(struct swdevt *sp) 197692da00bbSMatthew Dillon { 1977f425ab8eSKonstantin Belousov vm_object_t object; 19787c022327SDoug Moore int retries; 197992da00bbSMatthew Dillon 198004533e1eSKonstantin Belousov sx_assert(&swdev_syscall_lock, SA_XLOCKED); 198192da00bbSMatthew Dillon 19828bc61209SDavid Schultz retries = 0; 198392da00bbSMatthew Dillon full_rescan: 1984f425ab8eSKonstantin Belousov mtx_lock(&vm_object_list_mtx); 1985f425ab8eSKonstantin Belousov TAILQ_FOREACH(object, &vm_object_list, object_list) { 19864b8365d7SKonstantin Belousov if ((object->flags & OBJ_SWAP) == 0) 198798150664SKonstantin Belousov continue; 1988f425ab8eSKonstantin Belousov mtx_unlock(&vm_object_list_mtx); 198998150664SKonstantin Belousov /* Depends on type-stability. */ 199098150664SKonstantin Belousov VM_OBJECT_WLOCK(object); 1991f425ab8eSKonstantin Belousov 1992f425ab8eSKonstantin Belousov /* 1993f425ab8eSKonstantin Belousov * Dead objects are eventually terminated on their own. 1994f425ab8eSKonstantin Belousov */ 1995f425ab8eSKonstantin Belousov if ((object->flags & OBJ_DEAD) != 0) 1996f425ab8eSKonstantin Belousov goto next_obj; 1997f425ab8eSKonstantin Belousov 1998f425ab8eSKonstantin Belousov /* 1999f425ab8eSKonstantin Belousov * Sync with fences placed after pctrie 2000f425ab8eSKonstantin Belousov * initialization. We must not access pctrie below 2001f425ab8eSKonstantin Belousov * unless we checked that our object is swap and not 2002f425ab8eSKonstantin Belousov * dead. 2003f425ab8eSKonstantin Belousov */ 2004f425ab8eSKonstantin Belousov atomic_thread_fence_acq(); 20054b8365d7SKonstantin Belousov if ((object->flags & OBJ_SWAP) == 0) 2006f425ab8eSKonstantin Belousov goto next_obj; 2007f425ab8eSKonstantin Belousov 20087c022327SDoug Moore swap_pager_swapoff_object(sp, object); 2009f425ab8eSKonstantin Belousov next_obj: 2010f425ab8eSKonstantin Belousov VM_OBJECT_WUNLOCK(object); 2011f425ab8eSKonstantin Belousov mtx_lock(&vm_object_list_mtx); 201292da00bbSMatthew Dillon } 2013f425ab8eSKonstantin Belousov mtx_unlock(&vm_object_list_mtx); 2014f425ab8eSKonstantin Belousov 20158bc61209SDavid Schultz if (sp->sw_used) { 201692da00bbSMatthew Dillon /* 20178bc61209SDavid Schultz * Objects may be locked or paging to the device being 20188bc61209SDavid Schultz * removed, so we will miss their pages and need to 20198bc61209SDavid Schultz * make another pass. We have marked this device as 20208bc61209SDavid Schultz * SW_CLOSING, so the activity should finish soon. 202192da00bbSMatthew Dillon */ 20228bc61209SDavid Schultz retries++; 20238bc61209SDavid Schultz if (retries > 100) { 20248bc61209SDavid Schultz panic("swapoff: failed to locate %d swap blocks", 20258bc61209SDavid Schultz sp->sw_used); 20268bc61209SDavid Schultz } 20274d70511aSJohn Baldwin pause("swpoff", hz / 20); 202892da00bbSMatthew Dillon goto full_rescan; 202992da00bbSMatthew Dillon } 2030b1fd102eSMark Johnston EVENTHANDLER_INVOKE(swapoff, sp); 203192da00bbSMatthew Dillon } 203292da00bbSMatthew Dillon 20331c7c3c6aSMatthew Dillon /************************************************************************ 20341c7c3c6aSMatthew Dillon * SWAP META DATA * 20351c7c3c6aSMatthew Dillon ************************************************************************ 20361c7c3c6aSMatthew Dillon * 20371c7c3c6aSMatthew Dillon * These routines manipulate the swap metadata stored in the 2038cec9f109SDavid E. O'Brien * OBJT_SWAP object. 20391c7c3c6aSMatthew Dillon * 20404dcc5c2dSMatthew Dillon * Swap metadata is implemented with a global hash and not directly 20414dcc5c2dSMatthew Dillon * linked into the object. Instead the object simply contains 20424dcc5c2dSMatthew Dillon * appropriate tracking counters. 20431c7c3c6aSMatthew Dillon */ 20441c7c3c6aSMatthew Dillon 20451c7c3c6aSMatthew Dillon /* 2046230869e0SAlan Cox * SWP_PAGER_SWBLK_EMPTY() - is a range of blocks free? 2047230869e0SAlan Cox */ 2048230869e0SAlan Cox static bool 2049230869e0SAlan Cox swp_pager_swblk_empty(struct swblk *sb, int start, int limit) 2050230869e0SAlan Cox { 2051230869e0SAlan Cox int i; 2052230869e0SAlan Cox 2053230869e0SAlan Cox MPASS(0 <= start && start <= limit && limit <= SWAP_META_PAGES); 2054230869e0SAlan Cox for (i = start; i < limit; i++) { 2055230869e0SAlan Cox if (sb->d[i] != SWAPBLK_NONE) 2056230869e0SAlan Cox return (false); 2057230869e0SAlan Cox } 2058230869e0SAlan Cox return (true); 2059230869e0SAlan Cox } 2060230869e0SAlan Cox 2061230869e0SAlan Cox /* 2062abdab7b6SDoug Moore * SWP_PAGER_FREE_EMPTY_SWBLK() - frees if a block is free 2063abdab7b6SDoug Moore * 2064abdab7b6SDoug Moore * Nothing is done if the block is still in use. 2065abdab7b6SDoug Moore */ 2066abdab7b6SDoug Moore static void 2067abdab7b6SDoug Moore swp_pager_free_empty_swblk(vm_object_t object, struct swblk *sb) 2068abdab7b6SDoug Moore { 2069abdab7b6SDoug Moore 2070abdab7b6SDoug Moore if (swp_pager_swblk_empty(sb, 0, SWAP_META_PAGES)) { 207194264705SDoug Moore swblk_lookup_remove(object, sb); 2072abdab7b6SDoug Moore uma_zfree(swblk_zone, sb); 2073abdab7b6SDoug Moore } 2074abdab7b6SDoug Moore } 2075abdab7b6SDoug Moore 2076abdab7b6SDoug Moore /* 20771c7c3c6aSMatthew Dillon * SWP_PAGER_META_BUILD() - add swap block to swap meta data for object 20781c7c3c6aSMatthew Dillon * 207975694e65SDoug Moore * Try to add the specified swapblk to the object's swap metadata. If 208075694e65SDoug Moore * nowait_noreplace is set, add the specified swapblk only if there is no 208175694e65SDoug Moore * previously assigned swapblk at pindex. If the swapblk is invalid, and 208275694e65SDoug Moore * replaces a valid swapblk, empty swap metadata is freed. If memory 208375694e65SDoug Moore * allocation fails, and nowait_noreplace is set, return the specified 208475694e65SDoug Moore * swapblk immediately to indicate failure; otherwise, wait and retry until 208575694e65SDoug Moore * memory allocation succeeds. Return the previously assigned swapblk, if 208675694e65SDoug Moore * any. 20871c7c3c6aSMatthew Dillon */ 208878f1deefSAlan Cox static daddr_t 208975694e65SDoug Moore swp_pager_meta_build(vm_object_t object, vm_pindex_t pindex, daddr_t swapblk, 209075694e65SDoug Moore bool nowait_noreplace) 20912f249180SPoul-Henning Kamp { 2092f425ab8eSKonstantin Belousov static volatile int swblk_zone_exhausted, swpctrie_zone_exhausted; 2093eed99cb8SKonstantin Belousov struct swblk *sb, *sb1; 2094f425ab8eSKonstantin Belousov vm_pindex_t modpi, rdpi; 209578f1deefSAlan Cox daddr_t prev_swapblk; 2096f425ab8eSKonstantin Belousov int error, i; 20971c7c3c6aSMatthew Dillon 209889f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 2099f425ab8eSKonstantin Belousov 2100f425ab8eSKonstantin Belousov rdpi = rounddown(pindex, SWAP_META_PAGES); 210194264705SDoug Moore sb = swblk_lookup(object, rdpi); 2102f425ab8eSKonstantin Belousov if (sb == NULL) { 21031c7c3c6aSMatthew Dillon if (swapblk == SWAPBLK_NONE) 210478f1deefSAlan Cox return (SWAPBLK_NONE); 2105f425ab8eSKonstantin Belousov for (;;) { 2106f425ab8eSKonstantin Belousov sb = uma_zalloc(swblk_zone, M_NOWAIT | (curproc == 2107f425ab8eSKonstantin Belousov pageproc ? M_USE_RESERVE : 0)); 2108f425ab8eSKonstantin Belousov if (sb != NULL) { 2109f425ab8eSKonstantin Belousov sb->p = rdpi; 2110f425ab8eSKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) 2111f425ab8eSKonstantin Belousov sb->d[i] = SWAPBLK_NONE; 2112f425ab8eSKonstantin Belousov if (atomic_cmpset_int(&swblk_zone_exhausted, 2113f425ab8eSKonstantin Belousov 1, 0)) 2114f425ab8eSKonstantin Belousov printf("swblk zone ok\n"); 2115f425ab8eSKonstantin Belousov break; 2116f425ab8eSKonstantin Belousov } 211775694e65SDoug Moore if (nowait_noreplace) 211875694e65SDoug Moore return (swapblk); 211989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 2120f425ab8eSKonstantin Belousov if (uma_zone_exhausted(swblk_zone)) { 2121f425ab8eSKonstantin Belousov if (atomic_cmpset_int(&swblk_zone_exhausted, 2122f425ab8eSKonstantin Belousov 0, 1)) 2123f425ab8eSKonstantin Belousov printf("swap blk zone exhausted, " 21243ff863f1SDag-Erling Smørgrav "increase kern.maxswzone\n"); 21252025d69bSKonstantin Belousov vm_pageout_oom(VM_OOM_SWAPZ); 2126f425ab8eSKonstantin Belousov pause("swzonxb", 10); 21272025d69bSKonstantin Belousov } else 21288d6fbbb8SJeff Roberson uma_zwait(swblk_zone); 212989f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 213094264705SDoug Moore sb = swblk_lookup(object, rdpi); 2131eed99cb8SKonstantin Belousov if (sb != NULL) 2132eed99cb8SKonstantin Belousov /* 2133eed99cb8SKonstantin Belousov * Somebody swapped out a nearby page, 2134eed99cb8SKonstantin Belousov * allocating swblk at the rdpi index, 2135eed99cb8SKonstantin Belousov * while we dropped the object lock. 2136eed99cb8SKonstantin Belousov */ 2137eed99cb8SKonstantin Belousov goto allocated; 21384dcc5c2dSMatthew Dillon } 2139f425ab8eSKonstantin Belousov for (;;) { 214094264705SDoug Moore error = swblk_lookup_insert(object, sb); 2141f425ab8eSKonstantin Belousov if (error == 0) { 2142f425ab8eSKonstantin Belousov if (atomic_cmpset_int(&swpctrie_zone_exhausted, 2143f425ab8eSKonstantin Belousov 1, 0)) 2144f425ab8eSKonstantin Belousov printf("swpctrie zone ok\n"); 2145f425ab8eSKonstantin Belousov break; 21461c7c3c6aSMatthew Dillon } 214775694e65SDoug Moore if (nowait_noreplace) { 214875694e65SDoug Moore uma_zfree(swblk_zone, sb); 214975694e65SDoug Moore return (swapblk); 215075694e65SDoug Moore } 2151f425ab8eSKonstantin Belousov VM_OBJECT_WUNLOCK(object); 2152f425ab8eSKonstantin Belousov if (uma_zone_exhausted(swpctrie_zone)) { 2153f425ab8eSKonstantin Belousov if (atomic_cmpset_int(&swpctrie_zone_exhausted, 2154f425ab8eSKonstantin Belousov 0, 1)) 2155f425ab8eSKonstantin Belousov printf("swap pctrie zone exhausted, " 2156f425ab8eSKonstantin Belousov "increase kern.maxswzone\n"); 2157f425ab8eSKonstantin Belousov vm_pageout_oom(VM_OOM_SWAPZ); 2158f425ab8eSKonstantin Belousov pause("swzonxp", 10); 2159f425ab8eSKonstantin Belousov } else 21608d6fbbb8SJeff Roberson uma_zwait(swpctrie_zone); 2161f425ab8eSKonstantin Belousov VM_OBJECT_WLOCK(object); 216294264705SDoug Moore sb1 = swblk_lookup(object, rdpi); 2163eed99cb8SKonstantin Belousov if (sb1 != NULL) { 2164eed99cb8SKonstantin Belousov uma_zfree(swblk_zone, sb); 2165eed99cb8SKonstantin Belousov sb = sb1; 2166eed99cb8SKonstantin Belousov goto allocated; 21671c7c3c6aSMatthew Dillon } 2168f425ab8eSKonstantin Belousov } 2169eed99cb8SKonstantin Belousov } 2170eed99cb8SKonstantin Belousov allocated: 2171f425ab8eSKonstantin Belousov MPASS(sb->p == rdpi); 21721c7c3c6aSMatthew Dillon 2173f425ab8eSKonstantin Belousov modpi = pindex % SWAP_META_PAGES; 217478f1deefSAlan Cox /* Return prior contents of metadata. */ 217578f1deefSAlan Cox prev_swapblk = sb->d[modpi]; 217675694e65SDoug Moore if (!nowait_noreplace || prev_swapblk == SWAPBLK_NONE) { 2177f425ab8eSKonstantin Belousov /* Enter block into metadata. */ 2178f425ab8eSKonstantin Belousov sb->d[modpi] = swapblk; 217985d88d87SKonstantin Belousov 218085d88d87SKonstantin Belousov /* 218185d88d87SKonstantin Belousov * Free the swblk if we end up with the empty page run. 218285d88d87SKonstantin Belousov */ 2183abdab7b6SDoug Moore if (swapblk == SWAPBLK_NONE) 2184abdab7b6SDoug Moore swp_pager_free_empty_swblk(object, sb); 218575694e65SDoug Moore } 218678f1deefSAlan Cox return (prev_swapblk); 218785d88d87SKonstantin Belousov } 21881c7c3c6aSMatthew Dillon 21891c7c3c6aSMatthew Dillon /* 219094264705SDoug Moore * SWP_PAGER_META_TRANSFER() - transfer a range of blocks in the srcobject's 219194264705SDoug Moore * swap metadata into dstobject. 2192467057fcSDoug Moore * 2193*bae51702SDoug Moore * Blocks in src that correspond to holes in dst are transferred. Blocks 2194*bae51702SDoug Moore * in src that correspond to blocks in dst are freed. 2195467057fcSDoug Moore */ 2196467057fcSDoug Moore static void 2197467057fcSDoug Moore swp_pager_meta_transfer(vm_object_t srcobject, vm_object_t dstobject, 219894264705SDoug Moore vm_pindex_t pindex, vm_pindex_t count) 2199467057fcSDoug Moore { 2200a880104aSDoug Moore struct page_range range; 2201467057fcSDoug Moore struct swblk *sb; 22024ccad545SDoug Moore daddr_t blk, d[SWAP_META_PAGES]; 2203467057fcSDoug Moore vm_pindex_t offset, last; 22044ccad545SDoug Moore int d_mask, i, limit, start; 22054ccad545SDoug Moore _Static_assert(8 * sizeof(d_mask) >= SWAP_META_PAGES, 22064ccad545SDoug Moore "d_mask not big enough"); 2207467057fcSDoug Moore 2208467057fcSDoug Moore VM_OBJECT_ASSERT_WLOCKED(srcobject); 220994264705SDoug Moore VM_OBJECT_ASSERT_WLOCKED(dstobject); 2210baa1ccceSKonstantin Belousov 221194264705SDoug Moore if (count == 0 || swblk_is_empty(srcobject)) 221294264705SDoug Moore return; 2213467057fcSDoug Moore 2214a880104aSDoug Moore swp_pager_init_freerange(&range); 22154ccad545SDoug Moore d_mask = 0; 2216467057fcSDoug Moore offset = pindex; 2217467057fcSDoug Moore last = pindex + count; 221894264705SDoug Moore sb = swblk_start_limit(srcobject, pindex, last); 221994264705SDoug Moore start = (sb != NULL && sb->p < pindex) ? pindex - sb->p : 0; 222094264705SDoug Moore for (; sb != NULL; 222194264705SDoug Moore sb = swblk_start_limit(srcobject, pindex, last), start = 0) { 22224ccad545SDoug Moore pindex = sb->p; 22234ccad545SDoug Moore MPASS(d_mask == 0); 22244ccad545SDoug Moore limit = MIN(last - pindex, SWAP_META_PAGES); 2225467057fcSDoug Moore for (i = start; i < limit; i++) { 222694264705SDoug Moore if (sb->d[i] == SWAPBLK_NONE) 2227467057fcSDoug Moore continue; 222894264705SDoug Moore blk = swp_pager_meta_build(dstobject, 22294ccad545SDoug Moore pindex + i - offset, sb->d[i], true); 223094264705SDoug Moore if (blk == sb->d[i]) { 223175694e65SDoug Moore /* 22324ccad545SDoug Moore * Failed memory allocation stopped transfer; 22334ccad545SDoug Moore * save this block for transfer with lock 22344ccad545SDoug Moore * released. 223575694e65SDoug Moore */ 22364ccad545SDoug Moore d[i] = blk; 22374ccad545SDoug Moore d_mask |= 1 << i; 2238*bae51702SDoug Moore } else if (blk != SWAPBLK_NONE) { 2239*bae51702SDoug Moore /* Dst has a block at pindex, so free block. */ 224094264705SDoug Moore swp_pager_update_freerange(&range, sb->d[i]); 2241*bae51702SDoug Moore } 2242467057fcSDoug Moore sb->d[i] = SWAPBLK_NONE; 2243467057fcSDoug Moore } 2244467057fcSDoug Moore if (swp_pager_swblk_empty(sb, 0, start) && 2245467057fcSDoug Moore swp_pager_swblk_empty(sb, limit, SWAP_META_PAGES)) { 224694264705SDoug Moore swblk_lookup_remove(srcobject, sb); 2247467057fcSDoug Moore uma_zfree(swblk_zone, sb); 2248467057fcSDoug Moore } 22494ccad545SDoug Moore if (d_mask != 0) { 22504ccad545SDoug Moore /* Finish block transfer, with the lock released. */ 22514ccad545SDoug Moore VM_OBJECT_WUNLOCK(srcobject); 22524ccad545SDoug Moore do { 22534ccad545SDoug Moore i = ffs(d_mask) - 1; 22544ccad545SDoug Moore swp_pager_meta_build(dstobject, 22554ccad545SDoug Moore pindex + i - offset, d[i], false); 22564ccad545SDoug Moore d_mask &= ~(1 << i); 22574ccad545SDoug Moore } while (d_mask != 0); 22584ccad545SDoug Moore VM_OBJECT_WLOCK(srcobject); 22594ccad545SDoug Moore } 22604ccad545SDoug Moore pindex += SWAP_META_PAGES; 2261467057fcSDoug Moore } 2262a880104aSDoug Moore swp_pager_freeswapspace(&range); 2263467057fcSDoug Moore } 2264467057fcSDoug Moore 2265467057fcSDoug Moore /* 22661c7c3c6aSMatthew Dillon * SWP_PAGER_META_FREE() - free a range of blocks in the object's swap metadata 22671c7c3c6aSMatthew Dillon * 22681c7c3c6aSMatthew Dillon * The requested range of blocks is freed, with any associated swap 22691c7c3c6aSMatthew Dillon * returned to the swap bitmap. 22701c7c3c6aSMatthew Dillon * 22711c7c3c6aSMatthew Dillon * This routine will free swap metadata structures as they are cleaned 22721c7c3c6aSMatthew Dillon * out. This routine does *NOT* operate on swap metadata associated 22731c7c3c6aSMatthew Dillon * with resident pages. 22741c7c3c6aSMatthew Dillon */ 22751c7c3c6aSMatthew Dillon static void 2276baa1ccceSKonstantin Belousov swp_pager_meta_free(vm_object_t object, vm_pindex_t pindex, vm_pindex_t count, 2277baa1ccceSKonstantin Belousov vm_size_t *freed) 22781c7c3c6aSMatthew Dillon { 227994264705SDoug Moore struct page_range range; 228094264705SDoug Moore struct swblk *sb; 228194264705SDoug Moore vm_page_t m; 228294264705SDoug Moore vm_pindex_t last; 228394264705SDoug Moore vm_size_t fc; 228494264705SDoug Moore int i, limit, start; 228594264705SDoug Moore 228694264705SDoug Moore VM_OBJECT_ASSERT_WLOCKED(object); 228794264705SDoug Moore 228894264705SDoug Moore fc = 0; 228994264705SDoug Moore m = NULL; 229094264705SDoug Moore if (count == 0 || swblk_is_empty(object)) 229194264705SDoug Moore goto out; 229294264705SDoug Moore 229394264705SDoug Moore swp_pager_init_freerange(&range); 229494264705SDoug Moore last = pindex + count; 229594264705SDoug Moore sb = swblk_start_limit(object, pindex, last); 229694264705SDoug Moore start = (sb != NULL && sb->p < pindex) ? pindex - sb->p : 0; 229794264705SDoug Moore for (; sb != NULL; 229894264705SDoug Moore sb = swblk_start_limit(object, pindex, last), start = 0) { 229994264705SDoug Moore limit = MIN(last - sb->p, SWAP_META_PAGES); 230094264705SDoug Moore for (i = start; i < limit; i++) { 230194264705SDoug Moore if (sb->d[i] == SWAPBLK_NONE) 230294264705SDoug Moore continue; 230394264705SDoug Moore swp_pager_update_freerange(&range, sb->d[i]); 230494264705SDoug Moore if (freed != NULL) { 230594264705SDoug Moore m = (m != NULL && m->pindex == sb->p + i - 1) ? 230694264705SDoug Moore vm_page_next(m) : 230794264705SDoug Moore vm_page_lookup(object, sb->p + i); 230894264705SDoug Moore if (m == NULL || vm_page_none_valid(m)) 230994264705SDoug Moore fc++; 231094264705SDoug Moore } 231194264705SDoug Moore sb->d[i] = SWAPBLK_NONE; 231294264705SDoug Moore } 231394264705SDoug Moore pindex = sb->p + SWAP_META_PAGES; 231494264705SDoug Moore if (swp_pager_swblk_empty(sb, 0, start) && 231594264705SDoug Moore swp_pager_swblk_empty(sb, limit, SWAP_META_PAGES)) { 231694264705SDoug Moore swblk_lookup_remove(object, sb); 231794264705SDoug Moore uma_zfree(swblk_zone, sb); 231894264705SDoug Moore } 231994264705SDoug Moore } 232094264705SDoug Moore swp_pager_freeswapspace(&range); 232194264705SDoug Moore out: 232294264705SDoug Moore if (freed != NULL) 232394264705SDoug Moore *freed = fc; 23241c7c3c6aSMatthew Dillon } 23251c7c3c6aSMatthew Dillon 2326a880104aSDoug Moore static void 23272a21cfe6SDoug Moore swp_pager_meta_free_block(struct swblk *sb, void *rangev) 2328a880104aSDoug Moore { 2329d2acf0a4SDoug Moore struct page_range *range = rangev; 2330d2acf0a4SDoug Moore 2331a880104aSDoug Moore for (int i = 0; i < SWAP_META_PAGES; i++) { 2332a880104aSDoug Moore if (sb->d[i] != SWAPBLK_NONE) 2333a880104aSDoug Moore swp_pager_update_freerange(range, sb->d[i]); 2334a880104aSDoug Moore } 2335a880104aSDoug Moore uma_zfree(swblk_zone, sb); 2336a880104aSDoug Moore } 2337a880104aSDoug Moore 23381c7c3c6aSMatthew Dillon /* 23391c7c3c6aSMatthew Dillon * SWP_PAGER_META_FREE_ALL() - destroy all swap metadata associated with object 23401c7c3c6aSMatthew Dillon * 23411c7c3c6aSMatthew Dillon * This routine locates and destroys all swap metadata associated with 23421c7c3c6aSMatthew Dillon * an object. 23431c7c3c6aSMatthew Dillon */ 23441c7c3c6aSMatthew Dillon static void 23451c7c3c6aSMatthew Dillon swp_pager_meta_free_all(vm_object_t object) 23461c7c3c6aSMatthew Dillon { 2347a880104aSDoug Moore struct page_range range; 23481c7c3c6aSMatthew Dillon 234989f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 2350cb6757c0SMark Johnston 2351a880104aSDoug Moore swp_pager_init_freerange(&range); 2352d2acf0a4SDoug Moore SWAP_PCTRIE_RECLAIM_CALLBACK(&object->un_pager.swp.swp_blks, 2353d2acf0a4SDoug Moore swp_pager_meta_free_block, &range); 2354a880104aSDoug Moore swp_pager_freeswapspace(&range); 23551c7c3c6aSMatthew Dillon } 23561c7c3c6aSMatthew Dillon 23571c7c3c6aSMatthew Dillon /* 23584abca9bbSAlan Cox * SWP_PAGER_METACTL() - misc control of swap meta data. 23591c7c3c6aSMatthew Dillon * 23604abca9bbSAlan Cox * This routine is capable of looking up, or removing swapblk 23614abca9bbSAlan Cox * assignments in the swap meta data. It returns the swapblk being 23624abca9bbSAlan Cox * looked-up, popped, or SWAPBLK_NONE if the block was invalid. 23631c7c3c6aSMatthew Dillon * 23641c7c3c6aSMatthew Dillon * When acting on a busy resident page and paging is in progress, we 23651c7c3c6aSMatthew Dillon * have to wait until paging is complete but otherwise can act on the 23661c7c3c6aSMatthew Dillon * busy page. 23671c7c3c6aSMatthew Dillon */ 23681c7c3c6aSMatthew Dillon static daddr_t 23698ecbf14bSDoug Moore swp_pager_meta_lookup(vm_object_t object, vm_pindex_t pindex) 23702f249180SPoul-Henning Kamp { 2371f425ab8eSKonstantin Belousov struct swblk *sb; 23724dcc5c2dSMatthew Dillon 2373c25673ffSAttilio Rao VM_OBJECT_ASSERT_LOCKED(object); 2374ee620ea4SAlan Cox 23751c7c3c6aSMatthew Dillon /* 2376ee620ea4SAlan Cox * The meta data only exists if the object is OBJT_SWAP 23771c7c3c6aSMatthew Dillon * and even then might not be allocated yet. 23781c7c3c6aSMatthew Dillon */ 23794b8365d7SKonstantin Belousov KASSERT((object->flags & OBJ_SWAP) != 0, 23808ecbf14bSDoug Moore ("Lookup object not swappable")); 23811c7c3c6aSMatthew Dillon 238294264705SDoug Moore sb = swblk_lookup(object, pindex); 2383f425ab8eSKonstantin Belousov if (sb == NULL) 2384f425ab8eSKonstantin Belousov return (SWAPBLK_NONE); 23858ecbf14bSDoug Moore return (sb->d[pindex % SWAP_META_PAGES]); 23861c7c3c6aSMatthew Dillon } 23871c7c3c6aSMatthew Dillon 2388e9c0cc15SPoul-Henning Kamp /* 238994264705SDoug Moore * Returns the least page index which is greater than or equal to the parameter 239094264705SDoug Moore * pindex and for which there is a swap block allocated. Returns OBJ_MAX_SIZE 239194264705SDoug Moore * if are no allocated swap blocks for the object after the requested pindex. 239277d6fd97SKonstantin Belousov */ 239377d6fd97SKonstantin Belousov vm_pindex_t 239477d6fd97SKonstantin Belousov swap_pager_find_least(vm_object_t object, vm_pindex_t pindex) 239577d6fd97SKonstantin Belousov { 2396f425ab8eSKonstantin Belousov struct swblk *sb; 2397f425ab8eSKonstantin Belousov int i; 239877d6fd97SKonstantin Belousov 239994264705SDoug Moore if ((sb = swblk_start(object, pindex)) == NULL) 240094264705SDoug Moore return (OBJ_MAX_SIZE); 240128af3eb6SDoug Moore if (sb->p < pindex) { 240228af3eb6SDoug Moore for (i = pindex % SWAP_META_PAGES; i < SWAP_META_PAGES; i++) { 2403f425ab8eSKonstantin Belousov if (sb->d[i] != SWAPBLK_NONE) 2404f425ab8eSKonstantin Belousov return (sb->p + i); 240577d6fd97SKonstantin Belousov } 240694264705SDoug Moore if ((sb = swblk_next(object, sb)) == NULL) 240794264705SDoug Moore return (OBJ_MAX_SIZE); 240828af3eb6SDoug Moore } 2409f425ab8eSKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) { 2410f425ab8eSKonstantin Belousov if (sb->d[i] != SWAPBLK_NONE) 2411f425ab8eSKonstantin Belousov return (sb->p + i); 241277d6fd97SKonstantin Belousov } 2413f425ab8eSKonstantin Belousov 2414f425ab8eSKonstantin Belousov /* 2415f425ab8eSKonstantin Belousov * We get here if a swblk is present in the trie but it 2416f425ab8eSKonstantin Belousov * doesn't map any blocks. 2417f425ab8eSKonstantin Belousov */ 241828af3eb6SDoug Moore MPASS(0); 241994264705SDoug Moore return (OBJ_MAX_SIZE); 242077d6fd97SKonstantin Belousov } 242177d6fd97SKonstantin Belousov 242277d6fd97SKonstantin Belousov /* 2423e9c0cc15SPoul-Henning Kamp * System call swapon(name) enables swapping on device name, 2424e9c0cc15SPoul-Henning Kamp * which must be in the swdevsw. Return EBUSY 2425e9c0cc15SPoul-Henning Kamp * if already swapping on this device. 2426e9c0cc15SPoul-Henning Kamp */ 2427e9c0cc15SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_ 2428e9c0cc15SPoul-Henning Kamp struct swapon_args { 2429e9c0cc15SPoul-Henning Kamp char *name; 2430e9c0cc15SPoul-Henning Kamp }; 2431e9c0cc15SPoul-Henning Kamp #endif 2432e9c0cc15SPoul-Henning Kamp 2433e9c0cc15SPoul-Henning Kamp int 24348451d0ddSKip Macy sys_swapon(struct thread *td, struct swapon_args *uap) 2435e9c0cc15SPoul-Henning Kamp { 2436e9c0cc15SPoul-Henning Kamp struct vattr attr; 2437e9c0cc15SPoul-Henning Kamp struct vnode *vp; 2438e9c0cc15SPoul-Henning Kamp struct nameidata nd; 2439e9c0cc15SPoul-Henning Kamp int error; 2440e9c0cc15SPoul-Henning Kamp 2441acd3428bSRobert Watson error = priv_check(td, PRIV_SWAPON); 2442e9c0cc15SPoul-Henning Kamp if (error) 2443acd3428bSRobert Watson return (error); 2444e9c0cc15SPoul-Henning Kamp 244504533e1eSKonstantin Belousov sx_xlock(&swdev_syscall_lock); 2446e9c0cc15SPoul-Henning Kamp 2447e9c0cc15SPoul-Henning Kamp /* 2448e9c0cc15SPoul-Henning Kamp * Swap metadata may not fit in the KVM if we have physical 2449e9c0cc15SPoul-Henning Kamp * memory of >1GB. 2450e9c0cc15SPoul-Henning Kamp */ 2451f425ab8eSKonstantin Belousov if (swblk_zone == NULL) { 2452e9c0cc15SPoul-Henning Kamp error = ENOMEM; 2453e9c0cc15SPoul-Henning Kamp goto done; 2454e9c0cc15SPoul-Henning Kamp } 2455e9c0cc15SPoul-Henning Kamp 24566ddf41faSKonstantin Belousov NDINIT(&nd, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | AUDITVNODE1, 24577e1d3eefSMateusz Guzik UIO_USERSPACE, uap->name); 2458e9c0cc15SPoul-Henning Kamp error = namei(&nd); 2459e9c0cc15SPoul-Henning Kamp if (error) 2460e9c0cc15SPoul-Henning Kamp goto done; 2461e9c0cc15SPoul-Henning Kamp 2462bb92cd7bSMateusz Guzik NDFREE_PNBUF(&nd); 2463e9c0cc15SPoul-Henning Kamp vp = nd.ni_vp; 2464e9c0cc15SPoul-Henning Kamp 24657ad2a82dSMateusz Guzik if (vn_isdisk_error(vp, &error)) { 246688ad2d7bSKonstantin Belousov error = swapongeom(vp); 246720da9c2eSPoul-Henning Kamp } else if (vp->v_type == VREG && 2468e9c0cc15SPoul-Henning Kamp (vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 && 24690359a12eSAttilio Rao (error = VOP_GETATTR(vp, &attr, td->td_ucred)) == 0) { 2470e9c0cc15SPoul-Henning Kamp /* 2471e9c0cc15SPoul-Henning Kamp * Allow direct swapping to NFS regular files in the same 2472e9c0cc15SPoul-Henning Kamp * way that nfs_mountroot() sets up diskless swapping. 2473e9c0cc15SPoul-Henning Kamp */ 247459efee01SPoul-Henning Kamp error = swaponvp(td, vp, attr.va_size / DEV_BSIZE); 2475e9c0cc15SPoul-Henning Kamp } 2476e9c0cc15SPoul-Henning Kamp 24776ddf41faSKonstantin Belousov if (error != 0) 24786ddf41faSKonstantin Belousov vput(vp); 24796ddf41faSKonstantin Belousov else 24806ddf41faSKonstantin Belousov VOP_UNLOCK(vp); 2481e9c0cc15SPoul-Henning Kamp done: 248204533e1eSKonstantin Belousov sx_xunlock(&swdev_syscall_lock); 2483e9c0cc15SPoul-Henning Kamp return (error); 2484e9c0cc15SPoul-Henning Kamp } 2485e9c0cc15SPoul-Henning Kamp 24863ff863f1SDag-Erling Smørgrav /* 24873ff863f1SDag-Erling Smørgrav * Check that the total amount of swap currently configured does not 24883ff863f1SDag-Erling Smørgrav * exceed half the theoretical maximum. If it does, print a warning 248935872e79SKonstantin Belousov * message. 24903ff863f1SDag-Erling Smørgrav */ 249135872e79SKonstantin Belousov static void 249235872e79SKonstantin Belousov swapon_check_swzone(void) 24933ff863f1SDag-Erling Smørgrav { 24943ff863f1SDag-Erling Smørgrav 24953ff863f1SDag-Erling Smørgrav /* recommend using no more than half that amount */ 2496303fa05aSKonstantin Belousov if (swap_total > swap_maxpages / 2) { 24973ff863f1SDag-Erling Smørgrav printf("warning: total configured swap (%lu pages) " 24983ff863f1SDag-Erling Smørgrav "exceeds maximum recommended amount (%lu pages).\n", 2499303fa05aSKonstantin Belousov swap_total, swap_maxpages / 2); 25003ff863f1SDag-Erling Smørgrav printf("warning: increase kern.maxswzone " 25013ff863f1SDag-Erling Smørgrav "or reduce amount of swap.\n"); 25023ff863f1SDag-Erling Smørgrav } 25033ff863f1SDag-Erling Smørgrav } 25043ff863f1SDag-Erling Smørgrav 250559efee01SPoul-Henning Kamp static void 25062cc718a1SKonstantin Belousov swaponsomething(struct vnode *vp, void *id, u_long nblks, 25072cc718a1SKonstantin Belousov sw_strategy_t *strategy, sw_close_t *close, dev_t dev, int flags) 2508e9c0cc15SPoul-Henning Kamp { 25092d9974c1SAlan Cox struct swdevt *sp, *tsp; 251034e2051fSMark Johnston daddr_t dvbase; 2511e9c0cc15SPoul-Henning Kamp 2512e9c0cc15SPoul-Henning Kamp /* 2513e9c0cc15SPoul-Henning Kamp * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks. 2514e9c0cc15SPoul-Henning Kamp * First chop nblks off to page-align it, then convert. 2515e9c0cc15SPoul-Henning Kamp * 2516e9c0cc15SPoul-Henning Kamp * sw->sw_nblks is in page-sized chunks now too. 2517e9c0cc15SPoul-Henning Kamp */ 2518e9c0cc15SPoul-Henning Kamp nblks &= ~(ctodb(1) - 1); 2519e9c0cc15SPoul-Henning Kamp nblks = dbtoc(nblks); 2520e9c0cc15SPoul-Henning Kamp 25218f60c087SPoul-Henning Kamp sp = malloc(sizeof *sp, M_VMPGDATA, M_WAITOK | M_ZERO); 252200fd73d2SDoug Moore sp->sw_blist = blist_create(nblks, M_WAITOK); 2523dee34ca4SPoul-Henning Kamp sp->sw_vp = vp; 2524dee34ca4SPoul-Henning Kamp sp->sw_id = id; 2525f3732fd1SPoul-Henning Kamp sp->sw_dev = dev; 2526e9c0cc15SPoul-Henning Kamp sp->sw_nblks = nblks; 2527e9c0cc15SPoul-Henning Kamp sp->sw_used = 0; 252859efee01SPoul-Henning Kamp sp->sw_strategy = strategy; 2529dee34ca4SPoul-Henning Kamp sp->sw_close = close; 25302cc718a1SKonstantin Belousov sp->sw_flags = flags; 2531e9c0cc15SPoul-Henning Kamp 2532e9c0cc15SPoul-Henning Kamp /* 2533504f5e29SDoug Moore * Do not free the first blocks in order to avoid overwriting 25348f60c087SPoul-Henning Kamp * any bsd label at the front of the partition 2535e9c0cc15SPoul-Henning Kamp */ 2536504f5e29SDoug Moore blist_free(sp->sw_blist, howmany(BBSIZE, PAGE_SIZE), 2537504f5e29SDoug Moore nblks - howmany(BBSIZE, PAGE_SIZE)); 2538e9c0cc15SPoul-Henning Kamp 25392d9974c1SAlan Cox dvbase = 0; 254020da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 25412d9974c1SAlan Cox TAILQ_FOREACH(tsp, &swtailq, sw_list) { 25422d9974c1SAlan Cox if (tsp->sw_end >= dvbase) { 25432d9974c1SAlan Cox /* 25442d9974c1SAlan Cox * We put one uncovered page between the devices 25452d9974c1SAlan Cox * in order to definitively prevent any cross-device 25462d9974c1SAlan Cox * I/O requests 25472d9974c1SAlan Cox */ 25482d9974c1SAlan Cox dvbase = tsp->sw_end + 1; 25492d9974c1SAlan Cox } 25502d9974c1SAlan Cox } 25512d9974c1SAlan Cox sp->sw_first = dvbase; 25522d9974c1SAlan Cox sp->sw_end = dvbase + nblks; 25538f60c087SPoul-Henning Kamp TAILQ_INSERT_TAIL(&swtailq, sp, sw_list); 25548f60c087SPoul-Henning Kamp nswapdev++; 2555504f5e29SDoug Moore swap_pager_avail += nblks - howmany(BBSIZE, PAGE_SIZE); 2556e8bb589dSMatt Macy swap_total += nblks; 255735872e79SKonstantin Belousov swapon_check_swzone(); 2558d05bc129SAlan Cox swp_sizecheck(); 2559d05bc129SAlan Cox mtx_unlock(&sw_dev_mtx); 2560b1fd102eSMark Johnston EVENTHANDLER_INVOKE(swapon, sp); 256159efee01SPoul-Henning Kamp } 2562e9c0cc15SPoul-Henning Kamp 2563e9c0cc15SPoul-Henning Kamp /* 2564e9c0cc15SPoul-Henning Kamp * SYSCALL: swapoff(devname) 2565e9c0cc15SPoul-Henning Kamp * 2566e9c0cc15SPoul-Henning Kamp * Disable swapping on the given device. 2567dee34ca4SPoul-Henning Kamp * 2568dee34ca4SPoul-Henning Kamp * XXX: Badly designed system call: it should use a device index 2569dee34ca4SPoul-Henning Kamp * rather than filename as specification. We keep sw_vp around 2570dee34ca4SPoul-Henning Kamp * only to make this work. 2571e9c0cc15SPoul-Henning Kamp */ 257253465702SKonstantin Belousov static int 257353465702SKonstantin Belousov kern_swapoff(struct thread *td, const char *name, enum uio_seg name_seg, 257453465702SKonstantin Belousov u_int flags) 2575e9c0cc15SPoul-Henning Kamp { 2576e9c0cc15SPoul-Henning Kamp struct vnode *vp; 2577e9c0cc15SPoul-Henning Kamp struct nameidata nd; 2578e9c0cc15SPoul-Henning Kamp struct swdevt *sp; 257953465702SKonstantin Belousov int error; 2580e9c0cc15SPoul-Henning Kamp 2581acd3428bSRobert Watson error = priv_check(td, PRIV_SWAPOFF); 2582a4e4132fSKonstantin Belousov if (error != 0) 2583a4e4132fSKonstantin Belousov return (error); 258453465702SKonstantin Belousov if ((flags & ~(SWAPOFF_FORCE)) != 0) 2585a4e4132fSKonstantin Belousov return (EINVAL); 2586a4e4132fSKonstantin Belousov 258704533e1eSKonstantin Belousov sx_xlock(&swdev_syscall_lock); 2588e9c0cc15SPoul-Henning Kamp 258953465702SKonstantin Belousov NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, name_seg, name); 2590e9c0cc15SPoul-Henning Kamp error = namei(&nd); 2591e9c0cc15SPoul-Henning Kamp if (error) 2592e9c0cc15SPoul-Henning Kamp goto done; 2593bb92cd7bSMateusz Guzik NDFREE_PNBUF(&nd); 2594e9c0cc15SPoul-Henning Kamp vp = nd.ni_vp; 2595e9c0cc15SPoul-Henning Kamp 259620da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 25978f60c087SPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 2598dee34ca4SPoul-Henning Kamp if (sp->sw_vp == vp) 25990909f38aSPawel Jakub Dawidek break; 2600e9c0cc15SPoul-Henning Kamp } 260120da9c2eSPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 26020909f38aSPawel Jakub Dawidek if (sp == NULL) { 2603e9c0cc15SPoul-Henning Kamp error = EINVAL; 2604e9c0cc15SPoul-Henning Kamp goto done; 26050909f38aSPawel Jakub Dawidek } 260653465702SKonstantin Belousov error = swapoff_one(sp, td->td_ucred, flags); 26070909f38aSPawel Jakub Dawidek done: 260804533e1eSKonstantin Belousov sx_xunlock(&swdev_syscall_lock); 26090909f38aSPawel Jakub Dawidek return (error); 26100909f38aSPawel Jakub Dawidek } 26110909f38aSPawel Jakub Dawidek 261253465702SKonstantin Belousov 261353465702SKonstantin Belousov #ifdef COMPAT_FREEBSD13 261453465702SKonstantin Belousov int 261553465702SKonstantin Belousov freebsd13_swapoff(struct thread *td, struct freebsd13_swapoff_args *uap) 261653465702SKonstantin Belousov { 261753465702SKonstantin Belousov return (kern_swapoff(td, uap->name, UIO_USERSPACE, 0)); 261853465702SKonstantin Belousov } 261953465702SKonstantin Belousov #endif 262053465702SKonstantin Belousov 262153465702SKonstantin Belousov int 262253465702SKonstantin Belousov sys_swapoff(struct thread *td, struct swapoff_args *uap) 262353465702SKonstantin Belousov { 262453465702SKonstantin Belousov return (kern_swapoff(td, uap->name, UIO_USERSPACE, uap->flags)); 262553465702SKonstantin Belousov } 262653465702SKonstantin Belousov 26270909f38aSPawel Jakub Dawidek static int 2628e8dc2ba2SKonstantin Belousov swapoff_one(struct swdevt *sp, struct ucred *cred, u_int flags) 26290909f38aSPawel Jakub Dawidek { 263003bdd65fSAlan Cox u_long nblks; 2631e9c0cc15SPoul-Henning Kamp #ifdef MAC 26320909f38aSPawel Jakub Dawidek int error; 2633e9c0cc15SPoul-Henning Kamp #endif 2634e9c0cc15SPoul-Henning Kamp 263504533e1eSKonstantin Belousov sx_assert(&swdev_syscall_lock, SA_XLOCKED); 26360909f38aSPawel Jakub Dawidek #ifdef MAC 2637cb05b60aSAttilio Rao (void) vn_lock(sp->sw_vp, LK_EXCLUSIVE | LK_RETRY); 263835918c55SChristian S.J. Peron error = mac_system_check_swapoff(cred, sp->sw_vp); 2639b249ce48SMateusz Guzik (void) VOP_UNLOCK(sp->sw_vp); 26400909f38aSPawel Jakub Dawidek if (error != 0) 26410909f38aSPawel Jakub Dawidek return (error); 26420909f38aSPawel Jakub Dawidek #endif 2643e9c0cc15SPoul-Henning Kamp nblks = sp->sw_nblks; 2644e9c0cc15SPoul-Henning Kamp 2645e9c0cc15SPoul-Henning Kamp /* 2646e9c0cc15SPoul-Henning Kamp * We can turn off this swap device safely only if the 2647e9c0cc15SPoul-Henning Kamp * available virtual memory in the system will fit the amount 2648e9c0cc15SPoul-Henning Kamp * of data we will have to page back in, plus an epsilon so 2649e9c0cc15SPoul-Henning Kamp * the system doesn't become critically low on swap space. 26500190c38bSKonstantin Belousov * The vm_free_count() part does not account e.g. for clean 26510190c38bSKonstantin Belousov * pages that can be immediately reclaimed without paging, so 26520190c38bSKonstantin Belousov * this is a very rough estimation. 26530190c38bSKonstantin Belousov * 26540190c38bSKonstantin Belousov * On the other hand, not turning swap off on swapoff_all() 26550190c38bSKonstantin Belousov * means that we can lose swap data when filesystems go away, 26560190c38bSKonstantin Belousov * which is arguably worse. 2657e9c0cc15SPoul-Henning Kamp */ 2658e8dc2ba2SKonstantin Belousov if ((flags & SWAPOFF_FORCE) == 0 && 26590190c38bSKonstantin Belousov vm_free_count() + swap_pager_avail < nblks + nswap_lowat) 26600909f38aSPawel Jakub Dawidek return (ENOMEM); 2661e9c0cc15SPoul-Henning Kamp 2662e9c0cc15SPoul-Henning Kamp /* 2663e9c0cc15SPoul-Henning Kamp * Prevent further allocations on this device. 2664e9c0cc15SPoul-Henning Kamp */ 26652928cef7SAlan Cox mtx_lock(&sw_dev_mtx); 2666e9c0cc15SPoul-Henning Kamp sp->sw_flags |= SW_CLOSING; 266703bdd65fSAlan Cox swap_pager_avail -= blist_fill(sp->sw_blist, 0, nblks); 2668e8bb589dSMatt Macy swap_total -= nblks; 26692928cef7SAlan Cox mtx_unlock(&sw_dev_mtx); 2670e9c0cc15SPoul-Henning Kamp 2671e9c0cc15SPoul-Henning Kamp /* 2672e9c0cc15SPoul-Henning Kamp * Page in the contents of the device and close it. 2673e9c0cc15SPoul-Henning Kamp */ 2674b3fed13eSDavid Schultz swap_pager_swapoff(sp); 2675e9c0cc15SPoul-Henning Kamp 267635918c55SChristian S.J. Peron sp->sw_close(curthread, sp); 267720da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 26789e3e3fe5SWarner Losh sp->sw_id = NULL; 26798f60c087SPoul-Henning Kamp TAILQ_REMOVE(&swtailq, sp, sw_list); 26800676a140SAlan Cox nswapdev--; 26817dea2c2eSAlan Cox if (nswapdev == 0) { 26827dea2c2eSAlan Cox swap_pager_full = 2; 26837dea2c2eSAlan Cox swap_pager_almost_full = 1; 26847dea2c2eSAlan Cox } 26858f60c087SPoul-Henning Kamp if (swdevhd == sp) 26868f60c087SPoul-Henning Kamp swdevhd = NULL; 2687d05bc129SAlan Cox mtx_unlock(&sw_dev_mtx); 26888f60c087SPoul-Henning Kamp blist_destroy(sp->sw_blist); 26898f60c087SPoul-Henning Kamp free(sp, M_VMPGDATA); 26900909f38aSPawel Jakub Dawidek return (0); 26910909f38aSPawel Jakub Dawidek } 2692e9c0cc15SPoul-Henning Kamp 26930909f38aSPawel Jakub Dawidek void 26940909f38aSPawel Jakub Dawidek swapoff_all(void) 26950909f38aSPawel Jakub Dawidek { 26960909f38aSPawel Jakub Dawidek struct swdevt *sp, *spt; 26970909f38aSPawel Jakub Dawidek const char *devname; 26980909f38aSPawel Jakub Dawidek int error; 26990909f38aSPawel Jakub Dawidek 270004533e1eSKonstantin Belousov sx_xlock(&swdev_syscall_lock); 27010909f38aSPawel Jakub Dawidek 27020909f38aSPawel Jakub Dawidek mtx_lock(&sw_dev_mtx); 27030909f38aSPawel Jakub Dawidek TAILQ_FOREACH_SAFE(sp, &swtailq, sw_list, spt) { 27040909f38aSPawel Jakub Dawidek mtx_unlock(&sw_dev_mtx); 27057ad2a82dSMateusz Guzik if (vn_isdisk(sp->sw_vp)) 27067870adb6SEd Schouten devname = devtoname(sp->sw_vp->v_rdev); 27070909f38aSPawel Jakub Dawidek else 27080909f38aSPawel Jakub Dawidek devname = "[file]"; 2709e8dc2ba2SKonstantin Belousov error = swapoff_one(sp, thread0.td_ucred, SWAPOFF_FORCE); 27100909f38aSPawel Jakub Dawidek if (error != 0) { 27110909f38aSPawel Jakub Dawidek printf("Cannot remove swap device %s (error=%d), " 27120909f38aSPawel Jakub Dawidek "skipping.\n", devname, error); 27130909f38aSPawel Jakub Dawidek } else if (bootverbose) { 27140909f38aSPawel Jakub Dawidek printf("Swap device %s removed.\n", devname); 27150909f38aSPawel Jakub Dawidek } 27160909f38aSPawel Jakub Dawidek mtx_lock(&sw_dev_mtx); 27170909f38aSPawel Jakub Dawidek } 27180909f38aSPawel Jakub Dawidek mtx_unlock(&sw_dev_mtx); 27190909f38aSPawel Jakub Dawidek 272004533e1eSKonstantin Belousov sx_xunlock(&swdev_syscall_lock); 2721e9c0cc15SPoul-Henning Kamp } 2722e9c0cc15SPoul-Henning Kamp 2723567104a1SPoul-Henning Kamp void 2724567104a1SPoul-Henning Kamp swap_pager_status(int *total, int *used) 2725567104a1SPoul-Henning Kamp { 2726567104a1SPoul-Henning Kamp 27277ce3a312SMateusz Guzik *total = swap_total; 27287ce3a312SMateusz Guzik *used = swap_total - swap_pager_avail - 27297ce3a312SMateusz Guzik nswapdev * howmany(BBSIZE, PAGE_SIZE); 2730567104a1SPoul-Henning Kamp } 2731567104a1SPoul-Henning Kamp 2732dda4f960SKonstantin Belousov int 2733dda4f960SKonstantin Belousov swap_dev_info(int name, struct xswdev *xs, char *devname, size_t len) 2734dda4f960SKonstantin Belousov { 2735dda4f960SKonstantin Belousov struct swdevt *sp; 27367870adb6SEd Schouten const char *tmp_devname; 2737dda4f960SKonstantin Belousov int error, n; 2738dda4f960SKonstantin Belousov 2739dda4f960SKonstantin Belousov n = 0; 2740dda4f960SKonstantin Belousov error = ENOENT; 2741dda4f960SKonstantin Belousov mtx_lock(&sw_dev_mtx); 2742dda4f960SKonstantin Belousov TAILQ_FOREACH(sp, &swtailq, sw_list) { 2743dda4f960SKonstantin Belousov if (n != name) { 2744dda4f960SKonstantin Belousov n++; 2745dda4f960SKonstantin Belousov continue; 2746dda4f960SKonstantin Belousov } 2747dda4f960SKonstantin Belousov xs->xsw_version = XSWDEV_VERSION; 2748dda4f960SKonstantin Belousov xs->xsw_dev = sp->sw_dev; 2749dda4f960SKonstantin Belousov xs->xsw_flags = sp->sw_flags; 2750dda4f960SKonstantin Belousov xs->xsw_nblks = sp->sw_nblks; 2751dda4f960SKonstantin Belousov xs->xsw_used = sp->sw_used; 2752dda4f960SKonstantin Belousov if (devname != NULL) { 27537ad2a82dSMateusz Guzik if (vn_isdisk(sp->sw_vp)) 27547870adb6SEd Schouten tmp_devname = devtoname(sp->sw_vp->v_rdev); 2755dda4f960SKonstantin Belousov else 2756dda4f960SKonstantin Belousov tmp_devname = "[file]"; 2757dda4f960SKonstantin Belousov strncpy(devname, tmp_devname, len); 2758dda4f960SKonstantin Belousov } 2759dda4f960SKonstantin Belousov error = 0; 2760dda4f960SKonstantin Belousov break; 2761dda4f960SKonstantin Belousov } 2762dda4f960SKonstantin Belousov mtx_unlock(&sw_dev_mtx); 2763dda4f960SKonstantin Belousov return (error); 2764dda4f960SKonstantin Belousov } 2765dda4f960SKonstantin Belousov 276669921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11) 276769921123SKonstantin Belousov #define XSWDEV_VERSION_11 1 276869921123SKonstantin Belousov struct xswdev11 { 276969921123SKonstantin Belousov u_int xsw_version; 277069921123SKonstantin Belousov uint32_t xsw_dev; 277169921123SKonstantin Belousov int xsw_flags; 277269921123SKonstantin Belousov int xsw_nblks; 277369921123SKonstantin Belousov int xsw_used; 277469921123SKonstantin Belousov }; 277569921123SKonstantin Belousov #endif 277669921123SKonstantin Belousov 2777f6d281e8SKonstantin Belousov #if defined(__amd64__) && defined(COMPAT_FREEBSD32) 2778f6d281e8SKonstantin Belousov struct xswdev32 { 2779f6d281e8SKonstantin Belousov u_int xsw_version; 2780f6d281e8SKonstantin Belousov u_int xsw_dev1, xsw_dev2; 2781f6d281e8SKonstantin Belousov int xsw_flags; 2782f6d281e8SKonstantin Belousov int xsw_nblks; 2783f6d281e8SKonstantin Belousov int xsw_used; 2784f6d281e8SKonstantin Belousov }; 2785f6d281e8SKonstantin Belousov #endif 2786f6d281e8SKonstantin Belousov 2787e9c0cc15SPoul-Henning Kamp static int 2788e9c0cc15SPoul-Henning Kamp sysctl_vm_swap_info(SYSCTL_HANDLER_ARGS) 2789e9c0cc15SPoul-Henning Kamp { 2790e9c0cc15SPoul-Henning Kamp struct xswdev xs; 2791f6d281e8SKonstantin Belousov #if defined(__amd64__) && defined(COMPAT_FREEBSD32) 2792f6d281e8SKonstantin Belousov struct xswdev32 xs32; 2793f6d281e8SKonstantin Belousov #endif 279469921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11) 279569921123SKonstantin Belousov struct xswdev11 xs11; 279669921123SKonstantin Belousov #endif 2797dda4f960SKonstantin Belousov int error; 2798e9c0cc15SPoul-Henning Kamp 2799e9c0cc15SPoul-Henning Kamp if (arg2 != 1) /* name length */ 2800e9c0cc15SPoul-Henning Kamp return (EINVAL); 280106d1fd9fSMark Johnston 280206d1fd9fSMark Johnston memset(&xs, 0, sizeof(xs)); 2803dda4f960SKonstantin Belousov error = swap_dev_info(*(int *)arg1, &xs, NULL, 0); 2804dda4f960SKonstantin Belousov if (error != 0) 2805dda4f960SKonstantin Belousov return (error); 2806f6d281e8SKonstantin Belousov #if defined(__amd64__) && defined(COMPAT_FREEBSD32) 2807f6d281e8SKonstantin Belousov if (req->oldlen == sizeof(xs32)) { 280806d1fd9fSMark Johnston memset(&xs32, 0, sizeof(xs32)); 2809f6d281e8SKonstantin Belousov xs32.xsw_version = XSWDEV_VERSION; 2810f6d281e8SKonstantin Belousov xs32.xsw_dev1 = xs.xsw_dev; 2811f6d281e8SKonstantin Belousov xs32.xsw_dev2 = xs.xsw_dev >> 32; 2812f6d281e8SKonstantin Belousov xs32.xsw_flags = xs.xsw_flags; 2813f6d281e8SKonstantin Belousov xs32.xsw_nblks = xs.xsw_nblks; 2814f6d281e8SKonstantin Belousov xs32.xsw_used = xs.xsw_used; 2815f6d281e8SKonstantin Belousov error = SYSCTL_OUT(req, &xs32, sizeof(xs32)); 2816f6d281e8SKonstantin Belousov return (error); 2817f6d281e8SKonstantin Belousov } 2818f6d281e8SKonstantin Belousov #endif 281969921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11) 282069921123SKonstantin Belousov if (req->oldlen == sizeof(xs11)) { 282106d1fd9fSMark Johnston memset(&xs11, 0, sizeof(xs11)); 282269921123SKonstantin Belousov xs11.xsw_version = XSWDEV_VERSION_11; 282369921123SKonstantin Belousov xs11.xsw_dev = xs.xsw_dev; /* truncation */ 282469921123SKonstantin Belousov xs11.xsw_flags = xs.xsw_flags; 282569921123SKonstantin Belousov xs11.xsw_nblks = xs.xsw_nblks; 282669921123SKonstantin Belousov xs11.xsw_used = xs.xsw_used; 282769921123SKonstantin Belousov error = SYSCTL_OUT(req, &xs11, sizeof(xs11)); 2828f6d281e8SKonstantin Belousov return (error); 2829f6d281e8SKonstantin Belousov } 283069921123SKonstantin Belousov #endif 2831e9c0cc15SPoul-Henning Kamp error = SYSCTL_OUT(req, &xs, sizeof(xs)); 2832e9c0cc15SPoul-Henning Kamp return (error); 2833e9c0cc15SPoul-Henning Kamp } 2834e9c0cc15SPoul-Henning Kamp 28358f60c087SPoul-Henning Kamp SYSCTL_INT(_vm, OID_AUTO, nswapdev, CTLFLAG_RD, &nswapdev, 0, 2836e9c0cc15SPoul-Henning Kamp "Number of swap devices"); 28374c36e917SKonstantin Belousov SYSCTL_NODE(_vm, OID_AUTO, swap_info, CTLFLAG_RD | CTLFLAG_MPSAFE, 28384c36e917SKonstantin Belousov sysctl_vm_swap_info, 2839e9c0cc15SPoul-Henning Kamp "Swap statistics by device"); 2840ec38b344SPoul-Henning Kamp 2841ec38b344SPoul-Henning Kamp /* 2842f425ab8eSKonstantin Belousov * Count the approximate swap usage in pages for a vmspace. The 2843f425ab8eSKonstantin Belousov * shadowed or not yet copied on write swap blocks are not accounted. 2844ec38b344SPoul-Henning Kamp * The map must be locked. 2845ec38b344SPoul-Henning Kamp */ 28462860553aSRebecca Cran long 2847ec38b344SPoul-Henning Kamp vmspace_swap_count(struct vmspace *vmspace) 2848ec38b344SPoul-Henning Kamp { 284965d8409cSRebecca Cran vm_map_t map; 2850ec38b344SPoul-Henning Kamp vm_map_entry_t cur; 285165d8409cSRebecca Cran vm_object_t object; 2852f425ab8eSKonstantin Belousov struct swblk *sb; 2853f425ab8eSKonstantin Belousov vm_pindex_t e, pi; 2854f425ab8eSKonstantin Belousov long count; 2855f425ab8eSKonstantin Belousov int i; 285665d8409cSRebecca Cran 285765d8409cSRebecca Cran map = &vmspace->vm_map; 285865d8409cSRebecca Cran count = 0; 2859ec38b344SPoul-Henning Kamp 28602288078cSDoug Moore VM_MAP_ENTRY_FOREACH(cur, map) { 2861f425ab8eSKonstantin Belousov if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) != 0) 2862f425ab8eSKonstantin Belousov continue; 2863f425ab8eSKonstantin Belousov object = cur->object.vm_object; 28644b8365d7SKonstantin Belousov if (object == NULL || (object->flags & OBJ_SWAP) == 0) 2865f425ab8eSKonstantin Belousov continue; 2866f425ab8eSKonstantin Belousov VM_OBJECT_RLOCK(object); 28674b8365d7SKonstantin Belousov if ((object->flags & OBJ_SWAP) == 0) 2868f425ab8eSKonstantin Belousov goto unlock; 2869f425ab8eSKonstantin Belousov pi = OFF_TO_IDX(cur->offset); 2870f425ab8eSKonstantin Belousov e = pi + OFF_TO_IDX(cur->end - cur->start); 287194264705SDoug Moore for (sb = swblk_start_limit(object, pi, e); 287294264705SDoug Moore sb != NULL; sb = swblk_next_limit(object, sb, e)) { 2873f425ab8eSKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) { 2874f425ab8eSKonstantin Belousov if (sb->p + i < e && 2875f425ab8eSKonstantin Belousov sb->d[i] != SWAPBLK_NONE) 2876f425ab8eSKonstantin Belousov count++; 2877ec38b344SPoul-Henning Kamp } 2878ec38b344SPoul-Henning Kamp } 2879f425ab8eSKonstantin Belousov unlock: 2880f425ab8eSKonstantin Belousov VM_OBJECT_RUNLOCK(object); 2881ec38b344SPoul-Henning Kamp } 2882ec38b344SPoul-Henning Kamp return (count); 2883ec38b344SPoul-Henning Kamp } 2884dee34ca4SPoul-Henning Kamp 2885dee34ca4SPoul-Henning Kamp /* 2886dee34ca4SPoul-Henning Kamp * GEOM backend 2887dee34ca4SPoul-Henning Kamp * 2888dee34ca4SPoul-Henning Kamp * Swapping onto disk devices. 2889dee34ca4SPoul-Henning Kamp * 2890dee34ca4SPoul-Henning Kamp */ 2891dee34ca4SPoul-Henning Kamp 28925721c9c7SPoul-Henning Kamp static g_orphan_t swapgeom_orphan; 28935721c9c7SPoul-Henning Kamp 2894dee34ca4SPoul-Henning Kamp static struct g_class g_swap_class = { 2895dee34ca4SPoul-Henning Kamp .name = "SWAP", 28965721c9c7SPoul-Henning Kamp .version = G_VERSION, 28975721c9c7SPoul-Henning Kamp .orphan = swapgeom_orphan, 2898dee34ca4SPoul-Henning Kamp }; 2899dee34ca4SPoul-Henning Kamp 2900dee34ca4SPoul-Henning Kamp DECLARE_GEOM_CLASS(g_swap_class, g_class); 2901dee34ca4SPoul-Henning Kamp 2902dee34ca4SPoul-Henning Kamp static void 29033398491bSAlexander Motin swapgeom_close_ev(void *arg, int flags) 29043398491bSAlexander Motin { 29053398491bSAlexander Motin struct g_consumer *cp; 29063398491bSAlexander Motin 29073398491bSAlexander Motin cp = arg; 29083398491bSAlexander Motin g_access(cp, -1, -1, 0); 29093398491bSAlexander Motin g_detach(cp); 29103398491bSAlexander Motin g_destroy_consumer(cp); 29113398491bSAlexander Motin } 29123398491bSAlexander Motin 29139e3e3fe5SWarner Losh /* 29149e3e3fe5SWarner Losh * Add a reference to the g_consumer for an inflight transaction. 29159e3e3fe5SWarner Losh */ 29169e3e3fe5SWarner Losh static void 29179e3e3fe5SWarner Losh swapgeom_acquire(struct g_consumer *cp) 29189e3e3fe5SWarner Losh { 29199e3e3fe5SWarner Losh 29209e3e3fe5SWarner Losh mtx_assert(&sw_dev_mtx, MA_OWNED); 29219e3e3fe5SWarner Losh cp->index++; 29229e3e3fe5SWarner Losh } 29239e3e3fe5SWarner Losh 29249e3e3fe5SWarner Losh /* 29250c657d22SKonstantin Belousov * Remove a reference from the g_consumer. Post a close event if all 29260c657d22SKonstantin Belousov * references go away, since the function might be called from the 29270c657d22SKonstantin Belousov * biodone context. 29289e3e3fe5SWarner Losh */ 29299e3e3fe5SWarner Losh static void 29309e3e3fe5SWarner Losh swapgeom_release(struct g_consumer *cp, struct swdevt *sp) 29319e3e3fe5SWarner Losh { 29329e3e3fe5SWarner Losh 29339e3e3fe5SWarner Losh mtx_assert(&sw_dev_mtx, MA_OWNED); 29349e3e3fe5SWarner Losh cp->index--; 29359e3e3fe5SWarner Losh if (cp->index == 0) { 29369e3e3fe5SWarner Losh if (g_post_event(swapgeom_close_ev, cp, M_NOWAIT, NULL) == 0) 29379e3e3fe5SWarner Losh sp->sw_id = NULL; 29389e3e3fe5SWarner Losh } 29399e3e3fe5SWarner Losh } 29409e3e3fe5SWarner Losh 29413398491bSAlexander Motin static void 2942dee34ca4SPoul-Henning Kamp swapgeom_done(struct bio *bp2) 2943dee34ca4SPoul-Henning Kamp { 29443398491bSAlexander Motin struct swdevt *sp; 2945dee34ca4SPoul-Henning Kamp struct buf *bp; 29463398491bSAlexander Motin struct g_consumer *cp; 2947dee34ca4SPoul-Henning Kamp 2948dee34ca4SPoul-Henning Kamp bp = bp2->bio_caller2; 29493398491bSAlexander Motin cp = bp2->bio_from; 2950c5d3d25eSPoul-Henning Kamp bp->b_ioflags = bp2->bio_flags; 2951dee34ca4SPoul-Henning Kamp if (bp2->bio_error) 2952dee34ca4SPoul-Henning Kamp bp->b_ioflags |= BIO_ERROR; 2953c5d3d25eSPoul-Henning Kamp bp->b_resid = bp->b_bcount - bp2->bio_completed; 2954c5d3d25eSPoul-Henning Kamp bp->b_error = bp2->bio_error; 2955756a5412SGleb Smirnoff bp->b_caller1 = NULL; 2956dee34ca4SPoul-Henning Kamp bufdone(bp); 29573398491bSAlexander Motin sp = bp2->bio_caller1; 29589e3e3fe5SWarner Losh mtx_lock(&sw_dev_mtx); 29599e3e3fe5SWarner Losh swapgeom_release(cp, sp); 29603398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 2961dee34ca4SPoul-Henning Kamp g_destroy_bio(bp2); 2962dee34ca4SPoul-Henning Kamp } 2963dee34ca4SPoul-Henning Kamp 2964dee34ca4SPoul-Henning Kamp static void 2965dee34ca4SPoul-Henning Kamp swapgeom_strategy(struct buf *bp, struct swdevt *sp) 2966dee34ca4SPoul-Henning Kamp { 2967dee34ca4SPoul-Henning Kamp struct bio *bio; 2968dee34ca4SPoul-Henning Kamp struct g_consumer *cp; 2969dee34ca4SPoul-Henning Kamp 29703398491bSAlexander Motin mtx_lock(&sw_dev_mtx); 2971dee34ca4SPoul-Henning Kamp cp = sp->sw_id; 2972dee34ca4SPoul-Henning Kamp if (cp == NULL) { 29733398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 2974dee34ca4SPoul-Henning Kamp bp->b_error = ENXIO; 2975dee34ca4SPoul-Henning Kamp bp->b_ioflags |= BIO_ERROR; 2976dee34ca4SPoul-Henning Kamp bufdone(bp); 2977dee34ca4SPoul-Henning Kamp return; 2978dee34ca4SPoul-Henning Kamp } 29799e3e3fe5SWarner Losh swapgeom_acquire(cp); 29803398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 298111041003SKonstantin Belousov if (bp->b_iocmd == BIO_WRITE) 298211041003SKonstantin Belousov bio = g_new_bio(); 298311041003SKonstantin Belousov else 29844f8205e5SPoul-Henning Kamp bio = g_alloc_bio(); 29854f8205e5SPoul-Henning Kamp if (bio == NULL) { 29869e3e3fe5SWarner Losh mtx_lock(&sw_dev_mtx); 29879e3e3fe5SWarner Losh swapgeom_release(cp, sp); 29889e3e3fe5SWarner Losh mtx_unlock(&sw_dev_mtx); 29893e5b6861SPoul-Henning Kamp bp->b_error = ENOMEM; 29903e5b6861SPoul-Henning Kamp bp->b_ioflags |= BIO_ERROR; 29910b208315SEdward Tomasz Napierala printf("swap_pager: cannot allocate bio\n"); 29923e5b6861SPoul-Henning Kamp bufdone(bp); 29933e5b6861SPoul-Henning Kamp return; 29943e5b6861SPoul-Henning Kamp } 299511041003SKonstantin Belousov 2996756a5412SGleb Smirnoff bp->b_caller1 = bio; 29973398491bSAlexander Motin bio->bio_caller1 = sp; 2998dee34ca4SPoul-Henning Kamp bio->bio_caller2 = bp; 2999c5d3d25eSPoul-Henning Kamp bio->bio_cmd = bp->b_iocmd; 3000dee34ca4SPoul-Henning Kamp bio->bio_offset = (bp->b_blkno - sp->sw_first) * PAGE_SIZE; 3001dee34ca4SPoul-Henning Kamp bio->bio_length = bp->b_bcount; 3002dee34ca4SPoul-Henning Kamp bio->bio_done = swapgeom_done; 3003c6213befSGleb Smirnoff bio->bio_flags |= BIO_SWAP; 3004fade8dd7SJeff Roberson if (!buf_mapped(bp)) { 30052cc718a1SKonstantin Belousov bio->bio_ma = bp->b_pages; 30062cc718a1SKonstantin Belousov bio->bio_data = unmapped_buf; 30072cc718a1SKonstantin Belousov bio->bio_ma_offset = (vm_offset_t)bp->b_offset & PAGE_MASK; 30082cc718a1SKonstantin Belousov bio->bio_ma_n = bp->b_npages; 30092cc718a1SKonstantin Belousov bio->bio_flags |= BIO_UNMAPPED; 30102cc718a1SKonstantin Belousov } else { 30112cc718a1SKonstantin Belousov bio->bio_data = bp->b_data; 30122cc718a1SKonstantin Belousov bio->bio_ma = NULL; 30132cc718a1SKonstantin Belousov } 3014dee34ca4SPoul-Henning Kamp g_io_request(bio, cp); 3015dee34ca4SPoul-Henning Kamp return; 3016dee34ca4SPoul-Henning Kamp } 3017dee34ca4SPoul-Henning Kamp 3018dee34ca4SPoul-Henning Kamp static void 3019dee34ca4SPoul-Henning Kamp swapgeom_orphan(struct g_consumer *cp) 3020dee34ca4SPoul-Henning Kamp { 3021dee34ca4SPoul-Henning Kamp struct swdevt *sp; 30223398491bSAlexander Motin int destroy; 3023dee34ca4SPoul-Henning Kamp 3024dee34ca4SPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 30253398491bSAlexander Motin TAILQ_FOREACH(sp, &swtailq, sw_list) { 30263398491bSAlexander Motin if (sp->sw_id == cp) { 30278f12d83aSAlexander Motin sp->sw_flags |= SW_CLOSING; 30283398491bSAlexander Motin break; 3029dee34ca4SPoul-Henning Kamp } 30303398491bSAlexander Motin } 30319e3e3fe5SWarner Losh /* 30329e3e3fe5SWarner Losh * Drop reference we were created with. Do directly since we're in a 30339e3e3fe5SWarner Losh * special context where we don't have to queue the call to 30349e3e3fe5SWarner Losh * swapgeom_close_ev(). 30359e3e3fe5SWarner Losh */ 30369e3e3fe5SWarner Losh cp->index--; 30373398491bSAlexander Motin destroy = ((sp != NULL) && (cp->index == 0)); 30383398491bSAlexander Motin if (destroy) 30393398491bSAlexander Motin sp->sw_id = NULL; 30403398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 30413398491bSAlexander Motin if (destroy) 30423398491bSAlexander Motin swapgeom_close_ev(cp, 0); 3043dee34ca4SPoul-Henning Kamp } 3044dee34ca4SPoul-Henning Kamp 3045dee34ca4SPoul-Henning Kamp static void 3046dee34ca4SPoul-Henning Kamp swapgeom_close(struct thread *td, struct swdevt *sw) 3047dee34ca4SPoul-Henning Kamp { 30483398491bSAlexander Motin struct g_consumer *cp; 3049dee34ca4SPoul-Henning Kamp 30503398491bSAlexander Motin mtx_lock(&sw_dev_mtx); 30513398491bSAlexander Motin cp = sw->sw_id; 30523398491bSAlexander Motin sw->sw_id = NULL; 30533398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 30540c657d22SKonstantin Belousov 30550c657d22SKonstantin Belousov /* 30560c657d22SKonstantin Belousov * swapgeom_close() may be called from the biodone context, 30570c657d22SKonstantin Belousov * where we cannot perform topology changes. Delegate the 30580c657d22SKonstantin Belousov * work to the events thread. 30590c657d22SKonstantin Belousov */ 30603398491bSAlexander Motin if (cp != NULL) 30613398491bSAlexander Motin g_waitfor_event(swapgeom_close_ev, cp, M_WAITOK, NULL); 3062dee34ca4SPoul-Henning Kamp } 3063dee34ca4SPoul-Henning Kamp 306488ad2d7bSKonstantin Belousov static int 306588ad2d7bSKonstantin Belousov swapongeom_locked(struct cdev *dev, struct vnode *vp) 3066dee34ca4SPoul-Henning Kamp { 3067dee34ca4SPoul-Henning Kamp struct g_provider *pp; 3068dee34ca4SPoul-Henning Kamp struct g_consumer *cp; 3069dee34ca4SPoul-Henning Kamp static struct g_geom *gp; 3070dee34ca4SPoul-Henning Kamp struct swdevt *sp; 3071dee34ca4SPoul-Henning Kamp u_long nblks; 3072dee34ca4SPoul-Henning Kamp int error; 3073dee34ca4SPoul-Henning Kamp 307488ad2d7bSKonstantin Belousov pp = g_dev_getprovider(dev); 307588ad2d7bSKonstantin Belousov if (pp == NULL) 307688ad2d7bSKonstantin Belousov return (ENODEV); 3077dee34ca4SPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 3078dee34ca4SPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 3079dee34ca4SPoul-Henning Kamp cp = sp->sw_id; 3080dee34ca4SPoul-Henning Kamp if (cp != NULL && cp->provider == pp) { 3081dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 308288ad2d7bSKonstantin Belousov return (EBUSY); 3083dee34ca4SPoul-Henning Kamp } 3084dee34ca4SPoul-Henning Kamp } 3085dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 30865721c9c7SPoul-Henning Kamp if (gp == NULL) 308702c62349SJaakko Heinonen gp = g_new_geomf(&g_swap_class, "swap"); 3088dee34ca4SPoul-Henning Kamp cp = g_new_consumer(gp); 30899e3e3fe5SWarner Losh cp->index = 1; /* Number of active I/Os, plus one for being active. */ 30909e3e3fe5SWarner Losh cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 3091dee34ca4SPoul-Henning Kamp g_attach(cp, pp); 3092afeb65e6SPoul-Henning Kamp /* 3093afeb65e6SPoul-Henning Kamp * XXX: Every time you think you can improve the margin for 3094afeb65e6SPoul-Henning Kamp * footshooting, somebody depends on the ability to do so: 3095afeb65e6SPoul-Henning Kamp * savecore(8) wants to write to our swapdev so we cannot 3096afeb65e6SPoul-Henning Kamp * set an exclusive count :-( 3097afeb65e6SPoul-Henning Kamp */ 3098d2bae332SPoul-Henning Kamp error = g_access(cp, 1, 1, 0); 309988ad2d7bSKonstantin Belousov if (error != 0) { 3100dee34ca4SPoul-Henning Kamp g_detach(cp); 3101dee34ca4SPoul-Henning Kamp g_destroy_consumer(cp); 310288ad2d7bSKonstantin Belousov return (error); 3103dee34ca4SPoul-Henning Kamp } 3104dee34ca4SPoul-Henning Kamp nblks = pp->mediasize / DEV_BSIZE; 310588ad2d7bSKonstantin Belousov swaponsomething(vp, cp, nblks, swapgeom_strategy, 310688ad2d7bSKonstantin Belousov swapgeom_close, dev2udev(dev), 31072cc718a1SKonstantin Belousov (pp->flags & G_PF_ACCEPT_UNMAPPED) != 0 ? SW_UNMAPPED : 0); 310888ad2d7bSKonstantin Belousov return (0); 3109dee34ca4SPoul-Henning Kamp } 3110dee34ca4SPoul-Henning Kamp 3111dee34ca4SPoul-Henning Kamp static int 311288ad2d7bSKonstantin Belousov swapongeom(struct vnode *vp) 3113dee34ca4SPoul-Henning Kamp { 3114dee34ca4SPoul-Henning Kamp int error; 3115dee34ca4SPoul-Henning Kamp 31166ddf41faSKonstantin Belousov ASSERT_VOP_ELOCKED(vp, "swapongeom"); 3117abd80ddbSMateusz Guzik if (vp->v_type != VCHR || VN_IS_DOOMED(vp)) { 311888ad2d7bSKonstantin Belousov error = ENOENT; 311988ad2d7bSKonstantin Belousov } else { 312088ad2d7bSKonstantin Belousov g_topology_lock(); 312188ad2d7bSKonstantin Belousov error = swapongeom_locked(vp->v_rdev, vp); 312288ad2d7bSKonstantin Belousov g_topology_unlock(); 312388ad2d7bSKonstantin Belousov } 3124dee34ca4SPoul-Henning Kamp return (error); 3125dee34ca4SPoul-Henning Kamp } 3126dee34ca4SPoul-Henning Kamp 3127dee34ca4SPoul-Henning Kamp /* 3128dee34ca4SPoul-Henning Kamp * VNODE backend 3129dee34ca4SPoul-Henning Kamp * 3130dee34ca4SPoul-Henning Kamp * This is used mainly for network filesystem (read: probably only tested 3131dee34ca4SPoul-Henning Kamp * with NFS) swapfiles. 3132dee34ca4SPoul-Henning Kamp * 3133dee34ca4SPoul-Henning Kamp */ 3134dee34ca4SPoul-Henning Kamp 3135dee34ca4SPoul-Henning Kamp static void 3136dee34ca4SPoul-Henning Kamp swapdev_strategy(struct buf *bp, struct swdevt *sp) 3137dee34ca4SPoul-Henning Kamp { 3138494eb176SPoul-Henning Kamp struct vnode *vp2; 3139dee34ca4SPoul-Henning Kamp 3140dee34ca4SPoul-Henning Kamp bp->b_blkno = ctodb(bp->b_blkno - sp->sw_first); 3141dee34ca4SPoul-Henning Kamp 3142dee34ca4SPoul-Henning Kamp vp2 = sp->sw_id; 3143dee34ca4SPoul-Henning Kamp vhold(vp2); 3144dee34ca4SPoul-Henning Kamp if (bp->b_iocmd == BIO_WRITE) { 3145b19740f4SKonstantin Belousov vn_lock(vp2, LK_EXCLUSIVE | LK_RETRY); 31463cfc7651SOlivier Houchard if (bp->b_bufobj) 3147494eb176SPoul-Henning Kamp bufobj_wdrop(bp->b_bufobj); 3148a76d8f4eSPoul-Henning Kamp bufobj_wref(&vp2->v_bufobj); 3149b19740f4SKonstantin Belousov } else { 3150b19740f4SKonstantin Belousov vn_lock(vp2, LK_SHARED | LK_RETRY); 3151dee34ca4SPoul-Henning Kamp } 31523cfc7651SOlivier Houchard if (bp->b_bufobj != &vp2->v_bufobj) 31533cfc7651SOlivier Houchard bp->b_bufobj = &vp2->v_bufobj; 3154dee34ca4SPoul-Henning Kamp bp->b_vp = vp2; 31552c18019fSPoul-Henning Kamp bp->b_iooffset = dbtob(bp->b_blkno); 3156b792bebeSPoul-Henning Kamp bstrategy(bp); 3157b19740f4SKonstantin Belousov VOP_UNLOCK(vp2); 3158dee34ca4SPoul-Henning Kamp } 3159dee34ca4SPoul-Henning Kamp 3160dee34ca4SPoul-Henning Kamp static void 3161dee34ca4SPoul-Henning Kamp swapdev_close(struct thread *td, struct swdevt *sp) 3162dee34ca4SPoul-Henning Kamp { 3163a6d04f34SKonstantin Belousov struct vnode *vp; 3164dee34ca4SPoul-Henning Kamp 3165a6d04f34SKonstantin Belousov vp = sp->sw_vp; 3166a6d04f34SKonstantin Belousov vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 3167a6d04f34SKonstantin Belousov VOP_CLOSE(vp, FREAD | FWRITE, td->td_ucred, td); 3168a6d04f34SKonstantin Belousov vput(vp); 3169dee34ca4SPoul-Henning Kamp } 3170dee34ca4SPoul-Henning Kamp 3171dee34ca4SPoul-Henning Kamp static int 3172dee34ca4SPoul-Henning Kamp swaponvp(struct thread *td, struct vnode *vp, u_long nblks) 3173dee34ca4SPoul-Henning Kamp { 3174dee34ca4SPoul-Henning Kamp struct swdevt *sp; 3175dee34ca4SPoul-Henning Kamp int error; 3176dee34ca4SPoul-Henning Kamp 31776ddf41faSKonstantin Belousov ASSERT_VOP_ELOCKED(vp, "swaponvp"); 3178dee34ca4SPoul-Henning Kamp if (nblks == 0) 3179dee34ca4SPoul-Henning Kamp return (ENXIO); 3180dee34ca4SPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 3181dee34ca4SPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 3182dee34ca4SPoul-Henning Kamp if (sp->sw_id == vp) { 3183dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 3184dee34ca4SPoul-Henning Kamp return (EBUSY); 3185dee34ca4SPoul-Henning Kamp } 3186dee34ca4SPoul-Henning Kamp } 3187dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 3188dee34ca4SPoul-Henning Kamp 3189dee34ca4SPoul-Henning Kamp #ifdef MAC 319030d239bcSRobert Watson error = mac_system_check_swapon(td->td_ucred, vp); 3191dee34ca4SPoul-Henning Kamp if (error == 0) 3192dee34ca4SPoul-Henning Kamp #endif 31939e223287SKonstantin Belousov error = VOP_OPEN(vp, FREAD | FWRITE, td->td_ucred, td, NULL); 31946ddf41faSKonstantin Belousov if (error != 0) 3195dee34ca4SPoul-Henning Kamp return (error); 3196dee34ca4SPoul-Henning Kamp 3197dee34ca4SPoul-Henning Kamp swaponsomething(vp, vp, nblks, swapdev_strategy, swapdev_close, 31982cc718a1SKonstantin Belousov NODEV, 0); 3199dee34ca4SPoul-Henning Kamp return (0); 3200dee34ca4SPoul-Henning Kamp } 320189c241d1SGleb Smirnoff 320289c241d1SGleb Smirnoff static int 320389c241d1SGleb Smirnoff sysctl_swap_async_max(SYSCTL_HANDLER_ARGS) 320489c241d1SGleb Smirnoff { 320589c241d1SGleb Smirnoff int error, new, n; 320689c241d1SGleb Smirnoff 320789c241d1SGleb Smirnoff new = nsw_wcount_async_max; 320889c241d1SGleb Smirnoff error = sysctl_handle_int(oidp, &new, 0, req); 320989c241d1SGleb Smirnoff if (error != 0 || req->newptr == NULL) 321089c241d1SGleb Smirnoff return (error); 321189c241d1SGleb Smirnoff 321289c241d1SGleb Smirnoff if (new > nswbuf / 2 || new < 1) 321389c241d1SGleb Smirnoff return (EINVAL); 321489c241d1SGleb Smirnoff 3215756a5412SGleb Smirnoff mtx_lock(&swbuf_mtx); 321689c241d1SGleb Smirnoff while (nsw_wcount_async_max != new) { 321789c241d1SGleb Smirnoff /* 321889c241d1SGleb Smirnoff * Adjust difference. If the current async count is too low, 321989c241d1SGleb Smirnoff * we will need to sqeeze our update slowly in. Sleep with a 322089c241d1SGleb Smirnoff * higher priority than getpbuf() to finish faster. 322189c241d1SGleb Smirnoff */ 322289c241d1SGleb Smirnoff n = new - nsw_wcount_async_max; 322389c241d1SGleb Smirnoff if (nsw_wcount_async + n >= 0) { 322489c241d1SGleb Smirnoff nsw_wcount_async += n; 322589c241d1SGleb Smirnoff nsw_wcount_async_max += n; 322689c241d1SGleb Smirnoff wakeup(&nsw_wcount_async); 322789c241d1SGleb Smirnoff } else { 322889c241d1SGleb Smirnoff nsw_wcount_async_max -= nsw_wcount_async; 322989c241d1SGleb Smirnoff nsw_wcount_async = 0; 3230756a5412SGleb Smirnoff msleep(&nsw_wcount_async, &swbuf_mtx, PSWP, 323189c241d1SGleb Smirnoff "swpsysctl", 0); 323289c241d1SGleb Smirnoff } 323389c241d1SGleb Smirnoff } 3234756a5412SGleb Smirnoff mtx_unlock(&swbuf_mtx); 323589c241d1SGleb Smirnoff 323689c241d1SGleb Smirnoff return (0); 323789c241d1SGleb Smirnoff } 3238fe7bcbafSKyle Evans 3239fe7bcbafSKyle Evans static void 3240fe7bcbafSKyle Evans swap_pager_update_writecount(vm_object_t object, vm_offset_t start, 3241fe7bcbafSKyle Evans vm_offset_t end) 3242fe7bcbafSKyle Evans { 3243fe7bcbafSKyle Evans 3244fe7bcbafSKyle Evans VM_OBJECT_WLOCK(object); 324563967687SJeff Roberson KASSERT((object->flags & OBJ_ANON) == 0, 3246fe7bcbafSKyle Evans ("Splittable object with writecount")); 3247fe7bcbafSKyle Evans object->un_pager.swp.writemappings += (vm_ooffset_t)end - start; 3248fe7bcbafSKyle Evans VM_OBJECT_WUNLOCK(object); 3249fe7bcbafSKyle Evans } 3250fe7bcbafSKyle Evans 3251fe7bcbafSKyle Evans static void 3252fe7bcbafSKyle Evans swap_pager_release_writecount(vm_object_t object, vm_offset_t start, 3253fe7bcbafSKyle Evans vm_offset_t end) 3254fe7bcbafSKyle Evans { 3255fe7bcbafSKyle Evans 3256fe7bcbafSKyle Evans VM_OBJECT_WLOCK(object); 325763967687SJeff Roberson KASSERT((object->flags & OBJ_ANON) == 0, 3258fe7bcbafSKyle Evans ("Splittable object with writecount")); 32596ada4e8aSKonstantin Belousov KASSERT(object->un_pager.swp.writemappings >= (vm_ooffset_t)end - start, 32606ada4e8aSKonstantin Belousov ("swap obj %p writecount %jx dec %jx", object, 32616ada4e8aSKonstantin Belousov (uintmax_t)object->un_pager.swp.writemappings, 32626ada4e8aSKonstantin Belousov (uintmax_t)((vm_ooffset_t)end - start))); 3263fe7bcbafSKyle Evans object->un_pager.swp.writemappings -= (vm_ooffset_t)end - start; 3264fe7bcbafSKyle Evans VM_OBJECT_WUNLOCK(object); 3265fe7bcbafSKyle Evans } 3266