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 */ 48978f1deefSAlan Cox static daddr_t swp_pager_meta_build(vm_object_t, vm_pindex_t, daddr_t); 490645510e6SKonstantin Belousov static void swp_pager_meta_free(vm_object_t, vm_pindex_t, vm_pindex_t, 491baa1ccceSKonstantin Belousov vm_size_t *); 492467057fcSDoug Moore static void swp_pager_meta_transfer(vm_object_t src, vm_object_t dst, 493baa1ccceSKonstantin Belousov vm_pindex_t pindex, vm_pindex_t count, vm_size_t *freed); 49411caded3SAlfred Perlstein static void swp_pager_meta_free_all(vm_object_t); 4958ecbf14bSDoug Moore static daddr_t swp_pager_meta_lookup(vm_object_t, vm_pindex_t); 4961c7c3c6aSMatthew Dillon 49778f1deefSAlan Cox static void 498a880104aSDoug Moore swp_pager_init_freerange(struct page_range *range) 49978f1deefSAlan Cox { 500a880104aSDoug Moore range->start = SWAPBLK_NONE; 501a880104aSDoug Moore range->num = 0; 50278f1deefSAlan Cox } 50378f1deefSAlan Cox 50478f1deefSAlan Cox static void 505a880104aSDoug Moore swp_pager_update_freerange(struct page_range *range, daddr_t addr) 50678f1deefSAlan Cox { 507a880104aSDoug Moore if (range->start + range->num == addr) { 508a880104aSDoug Moore range->num++; 50978f1deefSAlan Cox } else { 510a880104aSDoug Moore swp_pager_freeswapspace(range); 511a880104aSDoug Moore range->start = addr; 512a880104aSDoug Moore range->num = 1; 51378f1deefSAlan Cox } 51478f1deefSAlan Cox } 51578f1deefSAlan Cox 516f425ab8eSKonstantin Belousov static void * 517f425ab8eSKonstantin Belousov swblk_trie_alloc(struct pctrie *ptree) 518f425ab8eSKonstantin Belousov { 519f425ab8eSKonstantin Belousov 520f425ab8eSKonstantin Belousov return (uma_zalloc(swpctrie_zone, M_NOWAIT | (curproc == pageproc ? 521f425ab8eSKonstantin Belousov M_USE_RESERVE : 0))); 522f425ab8eSKonstantin Belousov } 523f425ab8eSKonstantin Belousov 524f425ab8eSKonstantin Belousov static void 525f425ab8eSKonstantin Belousov swblk_trie_free(struct pctrie *ptree, void *node) 526f425ab8eSKonstantin Belousov { 527f425ab8eSKonstantin Belousov 528f425ab8eSKonstantin Belousov uma_zfree(swpctrie_zone, node); 529f425ab8eSKonstantin Belousov } 530f425ab8eSKonstantin Belousov 531f425ab8eSKonstantin Belousov PCTRIE_DEFINE(SWAP, swblk, p, swblk_trie_alloc, swblk_trie_free); 532f425ab8eSKonstantin Belousov 5331c7c3c6aSMatthew Dillon /* 5341c7c3c6aSMatthew Dillon * SWP_SIZECHECK() - update swap_pager_full indication 5351c7c3c6aSMatthew Dillon * 53620d3034fSMatthew Dillon * update the swap_pager_almost_full indication and warn when we are 53720d3034fSMatthew Dillon * about to run out of swap space, using lowat/hiwat hysteresis. 53820d3034fSMatthew Dillon * 53920d3034fSMatthew Dillon * Clear swap_pager_full ( task killing ) indication when lowat is met. 5401c7c3c6aSMatthew Dillon * 5411c7c3c6aSMatthew Dillon * No restrictions on call 5421c7c3c6aSMatthew Dillon * This routine may not block. 5431c7c3c6aSMatthew Dillon */ 544a5edd34aSPoul-Henning Kamp static void 5452f249180SPoul-Henning Kamp swp_sizecheck(void) 5460d94caffSDavid Greenman { 54723955314SAlfred Perlstein 5488f60c087SPoul-Henning Kamp if (swap_pager_avail < nswap_lowat) { 54920d3034fSMatthew Dillon if (swap_pager_almost_full == 0) { 5501af87c92SDavid Greenman printf("swap_pager: out of swap space\n"); 55120d3034fSMatthew Dillon swap_pager_almost_full = 1; 5522b0d37a4SMatthew Dillon } 55320d3034fSMatthew Dillon } else { 55426f9a767SRodney W. Grimes swap_pager_full = 0; 5558f60c087SPoul-Henning Kamp if (swap_pager_avail > nswap_hiwat) 55620d3034fSMatthew Dillon swap_pager_almost_full = 0; 55726f9a767SRodney W. Grimes } 5581c7c3c6aSMatthew Dillon } 5591c7c3c6aSMatthew Dillon 5601c7c3c6aSMatthew Dillon /* 5611c7c3c6aSMatthew Dillon * SWAP_PAGER_INIT() - initialize the swap pager! 5621c7c3c6aSMatthew Dillon * 5631c7c3c6aSMatthew Dillon * Expected to be started from system init. NOTE: This code is run 5641c7c3c6aSMatthew Dillon * before much else so be careful what you depend on. Most of the VM 5651c7c3c6aSMatthew Dillon * system has yet to be initialized at this point. 5661c7c3c6aSMatthew Dillon */ 567f5a12711SPoul-Henning Kamp static void 5682f249180SPoul-Henning Kamp swap_pager_init(void) 569df8bae1dSRodney W. Grimes { 5701c7c3c6aSMatthew Dillon /* 5711c7c3c6aSMatthew Dillon * Initialize object lists 5721c7c3c6aSMatthew Dillon */ 5731c7c3c6aSMatthew Dillon int i; 5741c7c3c6aSMatthew Dillon 5751c7c3c6aSMatthew Dillon for (i = 0; i < NOBJLISTS; ++i) 5761c7c3c6aSMatthew Dillon TAILQ_INIT(&swap_pager_object_list[i]); 57720da9c2eSPoul-Henning Kamp mtx_init(&sw_dev_mtx, "swapdev", NULL, MTX_DEF); 57815719273SKonstantin Belousov sx_init(&sw_alloc_sx, "swspsx"); 57904533e1eSKonstantin Belousov sx_init(&swdev_syscall_lock, "swsysc"); 580183f8e1eSGleb Smirnoff 581183f8e1eSGleb Smirnoff /* 582183f8e1eSGleb Smirnoff * The nsw_cluster_max is constrained by the bp->b_pages[] 583183f8e1eSGleb Smirnoff * array, which has maxphys / PAGE_SIZE entries, and our locally 584183f8e1eSGleb Smirnoff * defined MAX_PAGEOUT_CLUSTER. Also be aware that swap ops are 585183f8e1eSGleb Smirnoff * constrained by the swap device interleave stripe size. 586183f8e1eSGleb Smirnoff * 587183f8e1eSGleb Smirnoff * Initialized early so that GEOM_ELI can see it. 588183f8e1eSGleb Smirnoff */ 589183f8e1eSGleb Smirnoff nsw_cluster_max = min(maxphys / PAGE_SIZE, MAX_PAGEOUT_CLUSTER); 5901c7c3c6aSMatthew Dillon } 59126f9a767SRodney W. Grimes 592df8bae1dSRodney W. Grimes /* 5931c7c3c6aSMatthew Dillon * SWAP_PAGER_SWAP_INIT() - swap pager initialization from pageout process 5941c7c3c6aSMatthew Dillon * 5951c7c3c6aSMatthew Dillon * Expected to be started from pageout process once, prior to entering 5961c7c3c6aSMatthew Dillon * its main loop. 597df8bae1dSRodney W. Grimes */ 59824a1cce3SDavid Greenman void 5992f249180SPoul-Henning Kamp swap_pager_swap_init(void) 600df8bae1dSRodney W. Grimes { 60161203277SDag-Erling Smørgrav unsigned long n, n2; 6020d94caffSDavid Greenman 60326f9a767SRodney W. Grimes /* 6041c7c3c6aSMatthew Dillon * Number of in-transit swap bp operations. Don't 6051c7c3c6aSMatthew Dillon * exhaust the pbufs completely. Make sure we 6061c7c3c6aSMatthew Dillon * initialize workable values (0 will work for hysteresis 6071c7c3c6aSMatthew Dillon * but it isn't very efficient). 6081c7c3c6aSMatthew Dillon * 609327f4e83SMatthew Dillon * Currently we hardwire nsw_wcount_async to 4. This limit is 610327f4e83SMatthew Dillon * designed to prevent other I/O from having high latencies due to 611327f4e83SMatthew Dillon * our pageout I/O. The value 4 works well for one or two active swap 612327f4e83SMatthew Dillon * devices but is probably a little low if you have more. Even so, 613327f4e83SMatthew Dillon * a higher value would probably generate only a limited improvement 614327f4e83SMatthew Dillon * with three or four active swap devices since the system does not 615327f4e83SMatthew Dillon * typically have to pageout at extreme bandwidths. We will want 616327f4e83SMatthew Dillon * at least 2 per swap devices, and 4 is a pretty good value if you 617327f4e83SMatthew Dillon * have one NFS swap device due to the command/ack latency over NFS. 618327f4e83SMatthew Dillon * So it all works out pretty well. 619183f8e1eSGleb Smirnoff * 620183f8e1eSGleb Smirnoff * nsw_cluster_max is initialized in swap_pager_init(). 62126f9a767SRodney W. Grimes */ 622327f4e83SMatthew Dillon 623327f4e83SMatthew Dillon nsw_wcount_async = 4; 624327f4e83SMatthew Dillon nsw_wcount_async_max = nsw_wcount_async; 625756a5412SGleb Smirnoff mtx_init(&swbuf_mtx, "async swbuf mutex", NULL, MTX_DEF); 626756a5412SGleb Smirnoff 627756a5412SGleb Smirnoff swwbuf_zone = pbuf_zsecond_create("swwbuf", nswbuf / 4); 628756a5412SGleb Smirnoff swrbuf_zone = pbuf_zsecond_create("swrbuf", nswbuf / 2); 62924a1cce3SDavid Greenman 6301c7c3c6aSMatthew Dillon /* 631a8233027SKonstantin Belousov * Initialize our zone, taking the user's requested size or 632a8233027SKonstantin Belousov * estimating the number we need based on the number of pages 633a8233027SKonstantin Belousov * in the system. 6341c7c3c6aSMatthew Dillon */ 635a8233027SKonstantin Belousov n = maxswzone != 0 ? maxswzone / sizeof(struct swblk) : 636a8233027SKonstantin Belousov vm_cnt.v_page_count / 2; 637f425ab8eSKonstantin Belousov swpctrie_zone = uma_zcreate("swpctrie", pctrie_node_size(), NULL, NULL, 6386c5f36ffSJeff Roberson pctrie_zone_init, NULL, UMA_ALIGN_PTR, 0); 639f425ab8eSKonstantin Belousov swblk_zone = uma_zcreate("swblk", sizeof(struct swblk), NULL, NULL, 6406c5f36ffSJeff Roberson NULL, NULL, _Alignof(struct swblk) - 1, 0); 64122a5e6b9SDag-Erling Smørgrav n2 = n; 6428355f576SJeff Roberson do { 643f425ab8eSKonstantin Belousov if (uma_zone_reserve_kva(swblk_zone, n)) 64461ce6eeeSAlfred Perlstein break; 64561ce6eeeSAlfred Perlstein /* 64661ce6eeeSAlfred Perlstein * if the allocation failed, try a zone two thirds the 64761ce6eeeSAlfred Perlstein * size of the previous attempt. 64861ce6eeeSAlfred Perlstein */ 64961ce6eeeSAlfred Perlstein n -= ((n + 2) / 3); 65061ce6eeeSAlfred Perlstein } while (n > 0); 65153faf5a7SKonstantin Belousov 65253faf5a7SKonstantin Belousov /* 65353faf5a7SKonstantin Belousov * Often uma_zone_reserve_kva() cannot reserve exactly the 65453faf5a7SKonstantin Belousov * requested size. Account for the difference when 65553faf5a7SKonstantin Belousov * calculating swap_maxpages. 65653faf5a7SKonstantin Belousov */ 65753faf5a7SKonstantin Belousov n = uma_zone_get_max(swblk_zone); 65853faf5a7SKonstantin Belousov 6591fffcd75SKonstantin Belousov if (n < n2) 660a8233027SKonstantin Belousov printf("Swap blk zone entries changed from %lu to %lu.\n", 661f425ab8eSKonstantin Belousov n2, n); 662303fa05aSKonstantin Belousov /* absolute maximum we can handle assuming 100% efficiency */ 66361203277SDag-Erling Smørgrav swap_maxpages = n * SWAP_META_PAGES; 664f425ab8eSKonstantin Belousov swzone = n * sizeof(struct swblk); 665f425ab8eSKonstantin Belousov if (!uma_zone_reserve_kva(swpctrie_zone, n)) 666f425ab8eSKonstantin Belousov printf("Cannot reserve swap pctrie zone, " 667f425ab8eSKonstantin Belousov "reduce kern.maxswzone.\n"); 66824a1cce3SDavid Greenman } 66924a1cce3SDavid Greenman 67028bc23abSKonstantin Belousov bool 67128bc23abSKonstantin Belousov swap_pager_init_object(vm_object_t object, void *handle, struct ucred *cred, 67228bc23abSKonstantin Belousov vm_ooffset_t size, vm_ooffset_t offset) 67328bc23abSKonstantin Belousov { 67428bc23abSKonstantin Belousov if (cred != NULL) { 67528bc23abSKonstantin Belousov if (!swap_reserve_by_cred(size, cred)) 67628bc23abSKonstantin Belousov return (false); 67728bc23abSKonstantin Belousov crhold(cred); 67828bc23abSKonstantin Belousov } 67928bc23abSKonstantin Belousov 68028bc23abSKonstantin Belousov object->un_pager.swp.writemappings = 0; 68128bc23abSKonstantin Belousov object->handle = handle; 68228bc23abSKonstantin Belousov if (cred != NULL) { 68328bc23abSKonstantin Belousov object->cred = cred; 68428bc23abSKonstantin Belousov object->charge = size; 68528bc23abSKonstantin Belousov } 68628bc23abSKonstantin Belousov return (true); 68728bc23abSKonstantin Belousov } 68828bc23abSKonstantin Belousov 689eb4d6a1bSKonstantin Belousov static vm_object_t 6904b8365d7SKonstantin Belousov swap_pager_alloc_init(objtype_t otype, void *handle, struct ucred *cred, 6914b8365d7SKonstantin Belousov vm_ooffset_t size, vm_ooffset_t offset) 692eb4d6a1bSKonstantin Belousov { 693eb4d6a1bSKonstantin Belousov vm_object_t object; 694eb4d6a1bSKonstantin Belousov 695f425ab8eSKonstantin Belousov /* 696f425ab8eSKonstantin Belousov * The un_pager.swp.swp_blks trie is initialized by 697f425ab8eSKonstantin Belousov * vm_object_allocate() to ensure the correct order of 698f425ab8eSKonstantin Belousov * visibility to other threads. 699f425ab8eSKonstantin Belousov */ 7004b8365d7SKonstantin Belousov object = vm_object_allocate(otype, OFF_TO_IDX(offset + 701eb4d6a1bSKonstantin Belousov PAGE_MASK + size)); 702f425ab8eSKonstantin Belousov 70328bc23abSKonstantin Belousov if (!swap_pager_init_object(object, handle, cred, size, offset)) { 70428bc23abSKonstantin Belousov vm_object_deallocate(object); 70528bc23abSKonstantin Belousov return (NULL); 706eb4d6a1bSKonstantin Belousov } 707eb4d6a1bSKonstantin Belousov return (object); 708eb4d6a1bSKonstantin Belousov } 709eb4d6a1bSKonstantin Belousov 71024a1cce3SDavid Greenman /* 7111c7c3c6aSMatthew Dillon * SWAP_PAGER_ALLOC() - allocate a new OBJT_SWAP VM object and instantiate 7121c7c3c6aSMatthew Dillon * its metadata structures. 7131c7c3c6aSMatthew Dillon * 7141c7c3c6aSMatthew Dillon * This routine is called from the mmap and fork code to create a new 715eb4d6a1bSKonstantin Belousov * OBJT_SWAP object. 7161c7c3c6aSMatthew Dillon * 717eb4d6a1bSKonstantin Belousov * This routine must ensure that no live duplicate is created for 718eb4d6a1bSKonstantin Belousov * the named object request, which is protected against by 719eb4d6a1bSKonstantin Belousov * holding the sw_alloc_sx lock in case handle != NULL. 72024a1cce3SDavid Greenman */ 721f5a12711SPoul-Henning Kamp static vm_object_t 7226cde7a16SDavid Greenman swap_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 7233364c323SKonstantin Belousov vm_ooffset_t offset, struct ucred *cred) 72424a1cce3SDavid Greenman { 72524a1cce3SDavid Greenman vm_object_t object; 7262f7af3dbSAlan Cox 727eb4d6a1bSKonstantin Belousov if (handle != NULL) { 7281c7c3c6aSMatthew Dillon /* 7291c7c3c6aSMatthew Dillon * Reference existing named region or allocate new one. There 7301c7c3c6aSMatthew Dillon * should not be a race here against swp_pager_meta_build() 7311c7c3c6aSMatthew Dillon * as called from vm_page_remove() in regards to the lookup 7321c7c3c6aSMatthew Dillon * of the handle. 7331c7c3c6aSMatthew Dillon */ 7340cddd8f0SMatthew Dillon sx_xlock(&sw_alloc_sx); 7351c7c3c6aSMatthew Dillon object = vm_pager_object_lookup(NOBJLIST(handle), handle); 736b5e8f167SAlan Cox if (object == NULL) { 7374b8365d7SKonstantin Belousov object = swap_pager_alloc_init(OBJT_SWAP, handle, cred, 7384b8365d7SKonstantin Belousov size, offset); 739eb4d6a1bSKonstantin Belousov if (object != NULL) { 740eb4d6a1bSKonstantin Belousov TAILQ_INSERT_TAIL(NOBJLIST(object->handle), 741eb4d6a1bSKonstantin Belousov object, pager_object_list); 7423364c323SKonstantin Belousov } 74324a1cce3SDavid Greenman } 7440cddd8f0SMatthew Dillon sx_xunlock(&sw_alloc_sx); 74524a1cce3SDavid Greenman } else { 7464b8365d7SKonstantin Belousov object = swap_pager_alloc_init(OBJT_SWAP, handle, cred, 7474b8365d7SKonstantin Belousov size, offset); 74824a1cce3SDavid Greenman } 74924a1cce3SDavid Greenman return (object); 750df8bae1dSRodney W. Grimes } 751df8bae1dSRodney W. Grimes 75226f9a767SRodney W. Grimes /* 7531c7c3c6aSMatthew Dillon * SWAP_PAGER_DEALLOC() - remove swap metadata from object 7541c7c3c6aSMatthew Dillon * 7551c7c3c6aSMatthew Dillon * The swap backing for the object is destroyed. The code is 7561c7c3c6aSMatthew Dillon * designed such that we can reinstantiate it later, but this 7571c7c3c6aSMatthew Dillon * routine is typically called only when the entire object is 7581c7c3c6aSMatthew Dillon * about to be destroyed. 7591c7c3c6aSMatthew Dillon * 76015523cf7SKonstantin Belousov * The object must be locked. 76126f9a767SRodney W. Grimes */ 762df8bae1dSRodney W. Grimes static void 7632f249180SPoul-Henning Kamp swap_pager_dealloc(vm_object_t object) 76426f9a767SRodney W. Grimes { 7654dcc5c2dSMatthew Dillon 766eb4d6a1bSKonstantin Belousov VM_OBJECT_ASSERT_WLOCKED(object); 767eb4d6a1bSKonstantin Belousov KASSERT((object->flags & OBJ_DEAD) != 0, ("dealloc of reachable obj")); 768eb4d6a1bSKonstantin Belousov 76926f9a767SRodney W. Grimes /* 7701c7c3c6aSMatthew Dillon * Remove from list right away so lookups will fail if we block for 7711c7c3c6aSMatthew Dillon * pageout completion. 77226f9a767SRodney W. Grimes */ 77367388836SKonstantin Belousov if ((object->flags & OBJ_ANON) == 0 && object->handle != NULL) { 774eb4d6a1bSKonstantin Belousov VM_OBJECT_WUNLOCK(object); 775eb4d6a1bSKonstantin Belousov sx_xlock(&sw_alloc_sx); 776eb4d6a1bSKonstantin Belousov TAILQ_REMOVE(NOBJLIST(object->handle), object, 777eb4d6a1bSKonstantin Belousov pager_object_list); 778eb4d6a1bSKonstantin Belousov sx_xunlock(&sw_alloc_sx); 779eb4d6a1bSKonstantin Belousov VM_OBJECT_WLOCK(object); 780bd228075SAlan Cox } 7811c7c3c6aSMatthew Dillon 7821c7c3c6aSMatthew Dillon vm_object_pip_wait(object, "swpdea"); 7831c7c3c6aSMatthew Dillon 7841c7c3c6aSMatthew Dillon /* 7851c7c3c6aSMatthew Dillon * Free all remaining metadata. We only bother to free it from 7861c7c3c6aSMatthew Dillon * the swap meta data. We do not attempt to free swapblk's still 7871c7c3c6aSMatthew Dillon * associated with vm_page_t's for this object. We do not care 7881c7c3c6aSMatthew Dillon * if paging is still in progress on some objects. 7891c7c3c6aSMatthew Dillon */ 7901c7c3c6aSMatthew Dillon swp_pager_meta_free_all(object); 791e735691bSJohn Baldwin object->handle = NULL; 792e735691bSJohn Baldwin object->type = OBJT_DEAD; 793fffc1c59SMark Johnston 794fffc1c59SMark Johnston /* 795fffc1c59SMark Johnston * Release the allocation charge. 796fffc1c59SMark Johnston */ 797fffc1c59SMark Johnston if (object->cred != NULL) { 798fffc1c59SMark Johnston swap_release_by_cred(object->charge, object->cred); 799fffc1c59SMark Johnston object->charge = 0; 800fffc1c59SMark Johnston crfree(object->cred); 801fffc1c59SMark Johnston object->cred = NULL; 802fffc1c59SMark Johnston } 803fffc1c59SMark Johnston 804fffc1c59SMark Johnston /* 805fffc1c59SMark Johnston * Hide the object from swap_pager_swapoff(). 806fffc1c59SMark Johnston */ 8074b8365d7SKonstantin Belousov vm_object_clear_flag(object, OBJ_SWAP); 8081c7c3c6aSMatthew Dillon } 8091c7c3c6aSMatthew Dillon 8101c7c3c6aSMatthew Dillon /************************************************************************ 8111c7c3c6aSMatthew Dillon * SWAP PAGER BITMAP ROUTINES * 8121c7c3c6aSMatthew Dillon ************************************************************************/ 8131c7c3c6aSMatthew Dillon 8141c7c3c6aSMatthew Dillon /* 8151c7c3c6aSMatthew Dillon * SWP_PAGER_GETSWAPSPACE() - allocate raw swap space 8161c7c3c6aSMatthew Dillon * 81736b01270SDoug Moore * Allocate swap for up to the requested number of pages. The 81836b01270SDoug Moore * starting swap block number (a page index) is returned or 81936b01270SDoug Moore * SWAPBLK_NONE if the allocation failed. 8201c7c3c6aSMatthew Dillon * 8211c7c3c6aSMatthew Dillon * Also has the side effect of advising that somebody made a mistake 8221c7c3c6aSMatthew Dillon * when they configured swap and didn't configure enough. 8231c7c3c6aSMatthew Dillon * 82415523cf7SKonstantin Belousov * This routine may not sleep. 8258f60c087SPoul-Henning Kamp * 8268f60c087SPoul-Henning Kamp * We allocate in round-robin fashion from the configured devices. 8271c7c3c6aSMatthew Dillon */ 828a5edd34aSPoul-Henning Kamp static daddr_t 82936b01270SDoug Moore swp_pager_getswapspace(int *io_npages) 8301c7c3c6aSMatthew Dillon { 8311c7c3c6aSMatthew Dillon daddr_t blk; 8328f60c087SPoul-Henning Kamp struct swdevt *sp; 83387ae0686SDoug Moore int mpages, npages; 8341c7c3c6aSMatthew Dillon 83536b01270SDoug Moore KASSERT(*io_npages >= 1, 83636b01270SDoug Moore ("%s: npages not positive", __func__)); 8378f60c087SPoul-Henning Kamp blk = SWAPBLK_NONE; 83831c82722SDoug Moore mpages = *io_npages; 83931c82722SDoug Moore npages = imin(BLIST_MAX_ALLOC, mpages); 84020da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 8418f60c087SPoul-Henning Kamp sp = swdevhd; 84248e98a2aSDoug Moore while (!TAILQ_EMPTY(&swtailq)) { 8438f60c087SPoul-Henning Kamp if (sp == NULL) 8448f60c087SPoul-Henning Kamp sp = TAILQ_FIRST(&swtailq); 84548e98a2aSDoug Moore if ((sp->sw_flags & SW_CLOSING) == 0) 84687ae0686SDoug Moore blk = blist_alloc(sp->sw_blist, &npages, mpages); 84748e98a2aSDoug Moore if (blk != SWAPBLK_NONE) 84848e98a2aSDoug Moore break; 84948e98a2aSDoug Moore sp = TAILQ_NEXT(sp, sw_list); 85048e98a2aSDoug Moore if (swdevhd == sp) { 85136b01270SDoug Moore if (npages == 1) 85248e98a2aSDoug Moore break; 85387ae0686SDoug Moore mpages = npages - 1; 85448e98a2aSDoug Moore npages >>= 1; 85548e98a2aSDoug Moore } 85648e98a2aSDoug Moore } 8578f60c087SPoul-Henning Kamp if (blk != SWAPBLK_NONE) { 85848e98a2aSDoug Moore *io_npages = npages; 8598f60c087SPoul-Henning Kamp blk += sp->sw_first; 8608f60c087SPoul-Henning Kamp sp->sw_used += npages; 861d05bc129SAlan Cox swap_pager_avail -= npages; 8628f60c087SPoul-Henning Kamp swp_sizecheck(); 8638f60c087SPoul-Henning Kamp swdevhd = TAILQ_NEXT(sp, sw_list); 86448e98a2aSDoug Moore } else { 8652b0d37a4SMatthew Dillon if (swap_pager_full != 2) { 86648e98a2aSDoug Moore printf("swp_pager_getswapspace(%d): failed\n", 86748e98a2aSDoug Moore *io_npages); 8682b0d37a4SMatthew Dillon swap_pager_full = 2; 86920d3034fSMatthew Dillon swap_pager_almost_full = 1; 8702b0d37a4SMatthew Dillon } 8718f60c087SPoul-Henning Kamp swdevhd = NULL; 87248e98a2aSDoug Moore } 873d05bc129SAlan Cox mtx_unlock(&sw_dev_mtx); 8741c7c3c6aSMatthew Dillon return (blk); 87526f9a767SRodney W. Grimes } 87626f9a767SRodney W. Grimes 877541a1175SAlan Cox static bool 878b3fed13eSDavid Schultz swp_pager_isondev(daddr_t blk, struct swdevt *sp) 8798f60c087SPoul-Henning Kamp { 8808f60c087SPoul-Henning Kamp 881b3fed13eSDavid Schultz return (blk >= sp->sw_first && blk < sp->sw_end); 8828f60c087SPoul-Henning Kamp } 8838f60c087SPoul-Henning Kamp 8844b03903aSPoul-Henning Kamp static void 8854b03903aSPoul-Henning Kamp swp_pager_strategy(struct buf *bp) 8864b03903aSPoul-Henning Kamp { 8874b03903aSPoul-Henning Kamp struct swdevt *sp; 8884b03903aSPoul-Henning Kamp 88920da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 8904b03903aSPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 891541a1175SAlan Cox if (swp_pager_isondev(bp->b_blkno, sp)) { 89220da9c2eSPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 8932cc718a1SKonstantin Belousov if ((sp->sw_flags & SW_UNMAPPED) != 0 && 8942cc718a1SKonstantin Belousov unmapped_buf_allowed) { 8952cc718a1SKonstantin Belousov bp->b_data = unmapped_buf; 8962cc718a1SKonstantin Belousov bp->b_offset = 0; 8972cc718a1SKonstantin Belousov } else { 8982cc718a1SKonstantin Belousov pmap_qenter((vm_offset_t)bp->b_data, 8992cc718a1SKonstantin Belousov &bp->b_pages[0], bp->b_bcount / PAGE_SIZE); 9002cc718a1SKonstantin Belousov } 9014b03903aSPoul-Henning Kamp sp->sw_strategy(bp, sp); 9024b03903aSPoul-Henning Kamp return; 9034b03903aSPoul-Henning Kamp } 9044b03903aSPoul-Henning Kamp } 90520da9c2eSPoul-Henning Kamp panic("Swapdev not found"); 9064b03903aSPoul-Henning Kamp } 9074b03903aSPoul-Henning Kamp 90826f9a767SRodney W. Grimes /* 9091c7c3c6aSMatthew Dillon * SWP_PAGER_FREESWAPSPACE() - free raw swap space 9101c7c3c6aSMatthew Dillon * 9111c7c3c6aSMatthew Dillon * This routine returns the specified swap blocks back to the bitmap. 9121c7c3c6aSMatthew Dillon * 91315523cf7SKonstantin Belousov * This routine may not sleep. 91426f9a767SRodney W. Grimes */ 915a5edd34aSPoul-Henning Kamp static void 916a880104aSDoug Moore swp_pager_freeswapspace(const struct page_range *range) 9170d94caffSDavid Greenman { 918a880104aSDoug Moore daddr_t blk, npages; 9198f60c087SPoul-Henning Kamp struct swdevt *sp; 92092da00bbSMatthew Dillon 921a880104aSDoug Moore blk = range->start; 922a880104aSDoug Moore npages = range->num; 923230869e0SAlan Cox if (npages == 0) 924230869e0SAlan Cox return; 9257645e885SAlan Cox mtx_lock(&sw_dev_mtx); 9267645e885SAlan Cox TAILQ_FOREACH(sp, &swtailq, sw_list) { 927541a1175SAlan Cox if (swp_pager_isondev(blk, sp)) { 92892da00bbSMatthew Dillon sp->sw_used -= npages; 92992da00bbSMatthew Dillon /* 9307645e885SAlan Cox * If we are attempting to stop swapping on 9317645e885SAlan Cox * this device, we don't want to mark any 9327645e885SAlan Cox * blocks free lest they be reused. 93392da00bbSMatthew Dillon */ 9347645e885SAlan Cox if ((sp->sw_flags & SW_CLOSING) == 0) { 9357645e885SAlan Cox blist_free(sp->sw_blist, blk - sp->sw_first, 9367645e885SAlan Cox npages); 9378f60c087SPoul-Henning Kamp swap_pager_avail += npages; 9381c7c3c6aSMatthew Dillon swp_sizecheck(); 93926f9a767SRodney W. Grimes } 9407645e885SAlan Cox mtx_unlock(&sw_dev_mtx); 9417645e885SAlan Cox return; 9427645e885SAlan Cox } 9437645e885SAlan Cox } 9447645e885SAlan Cox panic("Swapdev not found"); 9457645e885SAlan Cox } 9461c7c3c6aSMatthew Dillon 94726f9a767SRodney W. Grimes /* 948d027ed2eSAlan Cox * SYSCTL_SWAP_FRAGMENTATION() - produce raw swap space stats 949d027ed2eSAlan Cox */ 950d027ed2eSAlan Cox static int 951d027ed2eSAlan Cox sysctl_swap_fragmentation(SYSCTL_HANDLER_ARGS) 952d027ed2eSAlan Cox { 953d027ed2eSAlan Cox struct sbuf sbuf; 954d027ed2eSAlan Cox struct swdevt *sp; 955d027ed2eSAlan Cox const char *devname; 956d027ed2eSAlan Cox int error; 957d027ed2eSAlan Cox 958d027ed2eSAlan Cox error = sysctl_wire_old_buffer(req, 0); 959d027ed2eSAlan Cox if (error != 0) 960d027ed2eSAlan Cox return (error); 961d027ed2eSAlan Cox sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 962d027ed2eSAlan Cox mtx_lock(&sw_dev_mtx); 963d027ed2eSAlan Cox TAILQ_FOREACH(sp, &swtailq, sw_list) { 9647ad2a82dSMateusz Guzik if (vn_isdisk(sp->sw_vp)) 965d027ed2eSAlan Cox devname = devtoname(sp->sw_vp->v_rdev); 966d027ed2eSAlan Cox else 967d027ed2eSAlan Cox devname = "[file]"; 968d027ed2eSAlan Cox sbuf_printf(&sbuf, "\nFree space on device %s:\n", devname); 969d027ed2eSAlan Cox blist_stats(sp->sw_blist, &sbuf); 970d027ed2eSAlan Cox } 971d027ed2eSAlan Cox mtx_unlock(&sw_dev_mtx); 972d027ed2eSAlan Cox error = sbuf_finish(&sbuf); 973d027ed2eSAlan Cox sbuf_delete(&sbuf); 974d027ed2eSAlan Cox return (error); 975d027ed2eSAlan Cox } 976d027ed2eSAlan Cox 977d027ed2eSAlan Cox /* 9781c7c3c6aSMatthew Dillon * SWAP_PAGER_FREESPACE() - frees swap blocks associated with a page 9791c7c3c6aSMatthew Dillon * range within an object. 9801c7c3c6aSMatthew Dillon * 9811c7c3c6aSMatthew Dillon * This routine removes swapblk assignments from swap metadata. 9821c7c3c6aSMatthew Dillon * 9831c7c3c6aSMatthew Dillon * The external callers of this routine typically have already destroyed 9841c7c3c6aSMatthew Dillon * or renamed vm_page_t's associated with this range in the object so 9851c7c3c6aSMatthew Dillon * we should be ok. 986c25673ffSAttilio Rao * 987c25673ffSAttilio Rao * The object must be locked. 98826f9a767SRodney W. Grimes */ 989baa1ccceSKonstantin Belousov void 990baa1ccceSKonstantin Belousov swap_pager_freespace(vm_object_t object, vm_pindex_t start, vm_size_t size, 991baa1ccceSKonstantin Belousov vm_size_t *freed) 99226f9a767SRodney W. Grimes { 993baa1ccceSKonstantin Belousov MPASS((object->flags & OBJ_SWAP) != 0); 99423955314SAlfred Perlstein 995baa1ccceSKonstantin Belousov swp_pager_meta_free(object, start, size, freed); 996baa1ccceSKonstantin Belousov } 997baa1ccceSKonstantin Belousov 998baa1ccceSKonstantin Belousov static void 999baa1ccceSKonstantin Belousov swap_pager_freespace_pgo(vm_object_t object, vm_pindex_t start, vm_size_t size) 1000baa1ccceSKonstantin Belousov { 1001baa1ccceSKonstantin Belousov MPASS((object->flags & OBJ_SWAP) != 0); 1002baa1ccceSKonstantin Belousov 1003baa1ccceSKonstantin Belousov swp_pager_meta_free(object, start, size, NULL); 10044dcc5c2dSMatthew Dillon } 10054dcc5c2dSMatthew Dillon 10064dcc5c2dSMatthew Dillon /* 10074dcc5c2dSMatthew Dillon * SWAP_PAGER_RESERVE() - reserve swap blocks in object 10084dcc5c2dSMatthew Dillon * 10094dcc5c2dSMatthew Dillon * Assigns swap blocks to the specified range within the object. The 101056ce850bSKonstantin Belousov * swap blocks are not zeroed. Any previous swap assignment is destroyed. 10114dcc5c2dSMatthew Dillon * 10124dcc5c2dSMatthew Dillon * Returns 0 on success, -1 on failure. 10134dcc5c2dSMatthew Dillon */ 10144dcc5c2dSMatthew Dillon int 1015686aa928SMark Johnston swap_pager_reserve(vm_object_t object, vm_pindex_t start, vm_pindex_t size) 10164dcc5c2dSMatthew Dillon { 1017a880104aSDoug Moore struct page_range range; 1018a880104aSDoug Moore daddr_t addr, blk; 1019686aa928SMark Johnston vm_pindex_t i, j; 1020686aa928SMark Johnston int n; 10214dcc5c2dSMatthew Dillon 1022a880104aSDoug Moore swp_pager_init_freerange(&range); 102389f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 102448e98a2aSDoug Moore for (i = 0; i < size; i += n) { 1025686aa928SMark Johnston n = MIN(size - i, INT_MAX); 102636b01270SDoug Moore blk = swp_pager_getswapspace(&n); 102748e98a2aSDoug Moore if (blk == SWAPBLK_NONE) { 1028baa1ccceSKonstantin Belousov swp_pager_meta_free(object, start, i, NULL); 102989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 10304dcc5c2dSMatthew Dillon return (-1); 10314dcc5c2dSMatthew Dillon } 103248e98a2aSDoug Moore for (j = 0; j < n; ++j) { 103348e98a2aSDoug Moore addr = swp_pager_meta_build(object, 103448e98a2aSDoug Moore start + i + j, blk + j); 103578f1deefSAlan Cox if (addr != SWAPBLK_NONE) 1036a880104aSDoug Moore swp_pager_update_freerange(&range, addr); 103748e98a2aSDoug Moore } 10384dcc5c2dSMatthew Dillon } 1039a880104aSDoug Moore swp_pager_freeswapspace(&range); 104089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 10414dcc5c2dSMatthew Dillon return (0); 104226f9a767SRodney W. Grimes } 104326f9a767SRodney W. Grimes 1044467057fcSDoug Moore static bool 1045467057fcSDoug Moore swp_pager_xfer_source(vm_object_t srcobject, vm_object_t dstobject, 1046467057fcSDoug Moore vm_pindex_t pindex, daddr_t addr) 1047467057fcSDoug Moore { 1048b8ebd99aSJohn Baldwin daddr_t dstaddr __diagused; 1049467057fcSDoug Moore 10504b8365d7SKonstantin Belousov KASSERT((srcobject->flags & OBJ_SWAP) != 0, 1051cb6757c0SMark Johnston ("%s: srcobject not swappable", __func__)); 1052cb6757c0SMark Johnston KASSERT((dstobject->flags & OBJ_SWAP) != 0, 1053cb6757c0SMark Johnston ("%s: dstobject not swappable", __func__)); 1054cb6757c0SMark Johnston 1055cb6757c0SMark Johnston if (swp_pager_meta_lookup(dstobject, pindex) != SWAPBLK_NONE) { 1056467057fcSDoug Moore /* Caller should destroy the source block. */ 1057467057fcSDoug Moore return (false); 1058467057fcSDoug Moore } 1059467057fcSDoug Moore 1060467057fcSDoug Moore /* 1061467057fcSDoug Moore * Destination has no swapblk and is not resident, transfer source. 1062467057fcSDoug Moore * swp_pager_meta_build() can sleep. 1063467057fcSDoug Moore */ 1064467057fcSDoug Moore VM_OBJECT_WUNLOCK(srcobject); 1065467057fcSDoug Moore dstaddr = swp_pager_meta_build(dstobject, pindex, addr); 1066467057fcSDoug Moore KASSERT(dstaddr == SWAPBLK_NONE, 1067467057fcSDoug Moore ("Unexpected destination swapblk")); 1068467057fcSDoug Moore VM_OBJECT_WLOCK(srcobject); 106998087a06SJeff Roberson 1070467057fcSDoug Moore return (true); 1071467057fcSDoug Moore } 1072467057fcSDoug Moore 10730a47b48bSJohn Dyson /* 10741c7c3c6aSMatthew Dillon * SWAP_PAGER_COPY() - copy blocks from source pager to destination pager 10751c7c3c6aSMatthew Dillon * and destroy the source. 10761c7c3c6aSMatthew Dillon * 10771c7c3c6aSMatthew Dillon * Copy any valid swapblks from the source to the destination. In 10781c7c3c6aSMatthew Dillon * cases where both the source and destination have a valid swapblk, 10791c7c3c6aSMatthew Dillon * we keep the destination's. 10801c7c3c6aSMatthew Dillon * 108115523cf7SKonstantin Belousov * This routine is allowed to sleep. It may sleep allocating metadata 108298087a06SJeff Roberson * indirectly through swp_pager_meta_build(). 10831c7c3c6aSMatthew Dillon * 10841c7c3c6aSMatthew Dillon * The source object contains no vm_page_t's (which is just as well) 10851c7c3c6aSMatthew Dillon * 108615523cf7SKonstantin Belousov * The source and destination objects must be locked. 108715523cf7SKonstantin Belousov * Both object locks may temporarily be released. 108826f9a767SRodney W. Grimes */ 108926f9a767SRodney W. Grimes void 10902f249180SPoul-Henning Kamp swap_pager_copy(vm_object_t srcobject, vm_object_t dstobject, 10912f249180SPoul-Henning Kamp vm_pindex_t offset, int destroysource) 109226f9a767SRodney W. Grimes { 109389f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(srcobject); 109489f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(dstobject); 10950cddd8f0SMatthew Dillon 109626f9a767SRodney W. Grimes /* 10971c7c3c6aSMatthew Dillon * If destroysource is set, we remove the source object from the 10981c7c3c6aSMatthew Dillon * swap_pager internal queue now. 109926f9a767SRodney W. Grimes */ 110067388836SKonstantin Belousov if (destroysource && (srcobject->flags & OBJ_ANON) == 0 && 110167388836SKonstantin Belousov srcobject->handle != NULL) { 1102eb4d6a1bSKonstantin Belousov VM_OBJECT_WUNLOCK(srcobject); 1103eb4d6a1bSKonstantin Belousov VM_OBJECT_WUNLOCK(dstobject); 1104eb4d6a1bSKonstantin Belousov sx_xlock(&sw_alloc_sx); 1105eb4d6a1bSKonstantin Belousov TAILQ_REMOVE(NOBJLIST(srcobject->handle), srcobject, 1106eb4d6a1bSKonstantin Belousov pager_object_list); 1107eb4d6a1bSKonstantin Belousov sx_xunlock(&sw_alloc_sx); 1108eb4d6a1bSKonstantin Belousov VM_OBJECT_WLOCK(dstobject); 1109eb4d6a1bSKonstantin Belousov VM_OBJECT_WLOCK(srcobject); 1110bd228075SAlan Cox } 111126f9a767SRodney W. Grimes 11121c7c3c6aSMatthew Dillon /* 11134abca9bbSAlan Cox * Transfer source to destination. 11141c7c3c6aSMatthew Dillon */ 1115baa1ccceSKonstantin Belousov swp_pager_meta_transfer(srcobject, dstobject, offset, dstobject->size, 1116baa1ccceSKonstantin Belousov NULL); 111726f9a767SRodney W. Grimes 111826f9a767SRodney W. Grimes /* 11191c7c3c6aSMatthew Dillon * Free left over swap blocks in source. 112026f9a767SRodney W. Grimes */ 1121cb6757c0SMark Johnston if (destroysource) 11221c7c3c6aSMatthew Dillon swp_pager_meta_free_all(srcobject); 112326f9a767SRodney W. Grimes } 112426f9a767SRodney W. Grimes 1125df8bae1dSRodney W. Grimes /* 11261c7c3c6aSMatthew Dillon * SWAP_PAGER_HASPAGE() - determine if we have good backing store for 11271c7c3c6aSMatthew Dillon * the requested page. 11281c7c3c6aSMatthew Dillon * 11291c7c3c6aSMatthew Dillon * We determine whether good backing store exists for the requested 11301c7c3c6aSMatthew Dillon * page and return TRUE if it does, FALSE if it doesn't. 11311c7c3c6aSMatthew Dillon * 11321c7c3c6aSMatthew Dillon * If TRUE, we also try to determine how much valid, contiguous backing 1133915d1b71SMark Johnston * store exists before and after the requested page. 1134df8bae1dSRodney W. Grimes */ 11355ea4972cSAlan Cox static boolean_t 1136915d1b71SMark Johnston swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, 1137915d1b71SMark Johnston int *after) 113826f9a767SRodney W. Grimes { 1139915d1b71SMark Johnston daddr_t blk, blk0; 1140915d1b71SMark Johnston int i; 114126f9a767SRodney W. Grimes 1142c25673ffSAttilio Rao VM_OBJECT_ASSERT_LOCKED(object); 11434b8365d7SKonstantin Belousov KASSERT((object->flags & OBJ_SWAP) != 0, 11448ecbf14bSDoug Moore ("%s: object not swappable", __func__)); 1145915d1b71SMark Johnston 11461c7c3c6aSMatthew Dillon /* 11471c7c3c6aSMatthew Dillon * do we have good backing store at the requested index ? 11481c7c3c6aSMatthew Dillon */ 11498ecbf14bSDoug Moore blk0 = swp_pager_meta_lookup(object, pindex); 11504dcc5c2dSMatthew Dillon if (blk0 == SWAPBLK_NONE) { 11511c7c3c6aSMatthew Dillon if (before) 115224a1cce3SDavid Greenman *before = 0; 11531c7c3c6aSMatthew Dillon if (after) 115424a1cce3SDavid Greenman *after = 0; 115526f9a767SRodney W. Grimes return (FALSE); 115626f9a767SRodney W. Grimes } 115726f9a767SRodney W. Grimes 115826f9a767SRodney W. Grimes /* 11591c7c3c6aSMatthew Dillon * find backwards-looking contiguous good backing store 1160e47ed70bSJohn Dyson */ 11611c7c3c6aSMatthew Dillon if (before != NULL) { 1162915d1b71SMark Johnston for (i = 1; i < SWB_NPAGES; i++) { 11631c7c3c6aSMatthew Dillon if (i > pindex) 11641c7c3c6aSMatthew Dillon break; 11658ecbf14bSDoug Moore blk = swp_pager_meta_lookup(object, pindex - i); 11661c7c3c6aSMatthew Dillon if (blk != blk0 - i) 11671c7c3c6aSMatthew Dillon break; 1168ffc82b0aSJohn Dyson } 1169915d1b71SMark Johnston *before = i - 1; 117026f9a767SRodney W. Grimes } 117126f9a767SRodney W. Grimes 117226f9a767SRodney W. Grimes /* 11731c7c3c6aSMatthew Dillon * find forward-looking contiguous good backing store 117426f9a767SRodney W. Grimes */ 11751c7c3c6aSMatthew Dillon if (after != NULL) { 1176915d1b71SMark Johnston for (i = 1; i < SWB_NPAGES; i++) { 11778ecbf14bSDoug Moore blk = swp_pager_meta_lookup(object, pindex + i); 11781c7c3c6aSMatthew Dillon if (blk != blk0 + i) 11791c7c3c6aSMatthew Dillon break; 118026f9a767SRodney W. Grimes } 1181915d1b71SMark Johnston *after = i - 1; 11821c7c3c6aSMatthew Dillon } 11831c7c3c6aSMatthew Dillon return (TRUE); 11841c7c3c6aSMatthew Dillon } 11851c7c3c6aSMatthew Dillon 1186*995730a6SDoug Moore static void 1187*995730a6SDoug Moore swap_pager_unswapped_acct(vm_page_t m) 1188*995730a6SDoug Moore { 1189*995730a6SDoug Moore KASSERT((m->object->flags & OBJ_SWAP) != 0, 1190*995730a6SDoug Moore ("Free object not swappable")); 1191*995730a6SDoug Moore if ((m->a.flags & PGA_SWAP_FREE) != 0) 1192*995730a6SDoug Moore counter_u64_add(swap_free_completed, 1); 1193*995730a6SDoug Moore vm_page_aflag_clear(m, PGA_SWAP_FREE | PGA_SWAP_SPACE); 1194*995730a6SDoug Moore 1195*995730a6SDoug Moore /* 1196*995730a6SDoug Moore * The meta data only exists if the object is OBJT_SWAP 1197*995730a6SDoug Moore * and even then might not be allocated yet. 1198*995730a6SDoug Moore */ 1199*995730a6SDoug Moore } 1200*995730a6SDoug Moore 12011c7c3c6aSMatthew Dillon /* 12021c7c3c6aSMatthew Dillon * SWAP_PAGER_PAGE_UNSWAPPED() - remove swap backing store related to page 12031c7c3c6aSMatthew Dillon * 12041c7c3c6aSMatthew Dillon * This removes any associated swap backing store, whether valid or 12051c7c3c6aSMatthew Dillon * not, from the page. 12061c7c3c6aSMatthew Dillon * 12071c7c3c6aSMatthew Dillon * This routine is typically called when a page is made dirty, at 12081c7c3c6aSMatthew Dillon * which point any associated swap can be freed. MADV_FREE also 12091c7c3c6aSMatthew Dillon * calls us in a special-case situation 12101c7c3c6aSMatthew Dillon * 12111c7c3c6aSMatthew Dillon * NOTE!!! If the page is clean and the swap was valid, the caller 12121c7c3c6aSMatthew Dillon * should make the page dirty before calling this routine. This routine 12131c7c3c6aSMatthew Dillon * does NOT change the m->dirty status of the page. Also: MADV_FREE 12141c7c3c6aSMatthew Dillon * depends on it. 12151c7c3c6aSMatthew Dillon * 121615523cf7SKonstantin Belousov * This routine may not sleep. 1217c25673ffSAttilio Rao * 1218a8081778SJeff Roberson * The object containing the page may be locked. 12191c7c3c6aSMatthew Dillon */ 12201c7c3c6aSMatthew Dillon static void 12212f249180SPoul-Henning Kamp swap_pager_unswapped(vm_page_t m) 12221c7c3c6aSMatthew Dillon { 1223a880104aSDoug Moore struct page_range range; 12248ecbf14bSDoug Moore struct swblk *sb; 1225a8081778SJeff Roberson vm_object_t obj; 12262f249180SPoul-Henning Kamp 1227a8081778SJeff Roberson /* 1228a8081778SJeff Roberson * Handle enqueing deferred frees first. If we do not have the 1229a8081778SJeff Roberson * object lock we wait for the page daemon to clear the space. 1230a8081778SJeff Roberson */ 1231a8081778SJeff Roberson obj = m->object; 1232a8081778SJeff Roberson if (!VM_OBJECT_WOWNED(obj)) { 1233a8081778SJeff Roberson VM_PAGE_OBJECT_BUSY_ASSERT(m); 1234a8081778SJeff Roberson /* 1235a8081778SJeff Roberson * The caller is responsible for synchronization but we 1236a8081778SJeff Roberson * will harmlessly handle races. This is typically provided 1237a8081778SJeff Roberson * by only calling unswapped() when a page transitions from 1238a8081778SJeff Roberson * clean to dirty. 1239a8081778SJeff Roberson */ 1240a8081778SJeff Roberson if ((m->a.flags & (PGA_SWAP_SPACE | PGA_SWAP_FREE)) == 1241a8081778SJeff Roberson PGA_SWAP_SPACE) { 1242a8081778SJeff Roberson vm_page_aflag_set(m, PGA_SWAP_FREE); 1243a8081778SJeff Roberson counter_u64_add(swap_free_deferred, 1); 1244a8081778SJeff Roberson } 1245a8081778SJeff Roberson return; 1246a8081778SJeff Roberson } 1247*995730a6SDoug Moore swap_pager_unswapped_acct(m); 12488ecbf14bSDoug Moore 12498ecbf14bSDoug Moore sb = SWAP_PCTRIE_LOOKUP(&m->object->un_pager.swp.swp_blks, 12508ecbf14bSDoug Moore rounddown(m->pindex, SWAP_META_PAGES)); 12518ecbf14bSDoug Moore if (sb == NULL) 12528ecbf14bSDoug Moore return; 1253a880104aSDoug Moore range.start = sb->d[m->pindex % SWAP_META_PAGES]; 1254a880104aSDoug Moore if (range.start == SWAPBLK_NONE) 12558ecbf14bSDoug Moore return; 1256a880104aSDoug Moore range.num = 1; 1257a880104aSDoug Moore swp_pager_freeswapspace(&range); 12588ecbf14bSDoug Moore sb->d[m->pindex % SWAP_META_PAGES] = SWAPBLK_NONE; 12598ecbf14bSDoug Moore swp_pager_free_empty_swblk(m->object, sb); 12601c7c3c6aSMatthew Dillon } 12611c7c3c6aSMatthew Dillon 12621c7c3c6aSMatthew Dillon /* 1263915d1b71SMark Johnston * swap_pager_getpages() - bring pages in from swap 12641c7c3c6aSMatthew Dillon * 12653f060b60SMark Johnston * Attempt to page in the pages in array "ma" of length "count". The 12663f060b60SMark Johnston * caller may optionally specify that additional pages preceding and 12673f060b60SMark Johnston * succeeding the specified range be paged in. The number of such pages 12683f060b60SMark Johnston * is returned in the "rbehind" and "rahead" parameters, and they will 12693f060b60SMark Johnston * be in the inactive queue upon return. 12701c7c3c6aSMatthew Dillon * 12713f060b60SMark Johnston * The pages in "ma" must be busied and will remain busied upon return. 12721c7c3c6aSMatthew Dillon */ 1273f708ef1bSPoul-Henning Kamp static int 1274c90d075bSMark Johnston swap_pager_getpages_locked(vm_object_t object, vm_page_t *ma, int count, 1275c90d075bSMark Johnston int *rbehind, int *rahead) 1276df8bae1dSRodney W. Grimes { 12771c7c3c6aSMatthew Dillon struct buf *bp; 1278b7b8a096SKonstantin Belousov vm_page_t bm, mpred, msucc, p; 1279915d1b71SMark Johnston vm_pindex_t pindex; 12801c7c3c6aSMatthew Dillon daddr_t blk; 1281b7b8a096SKonstantin Belousov int i, maxahead, maxbehind, reqcount; 12820d94caffSDavid Greenman 1283c90d075bSMark Johnston VM_OBJECT_ASSERT_WLOCKED(object); 1284915d1b71SMark Johnston reqcount = count; 12851c7c3c6aSMatthew Dillon 12864b8365d7SKonstantin Belousov KASSERT((object->flags & OBJ_SWAP) != 0, 12878ecbf14bSDoug Moore ("%s: object not swappable", __func__)); 1288d6e13f3bSJeff Roberson if (!swap_pager_haspage(object, ma[0]->pindex, &maxbehind, &maxahead)) { 1289d6e13f3bSJeff Roberson VM_OBJECT_WUNLOCK(object); 1290915d1b71SMark Johnston return (VM_PAGER_FAIL); 1291d6e13f3bSJeff Roberson } 1292915d1b71SMark Johnston 129398087a06SJeff Roberson KASSERT(reqcount - 1 <= maxahead, 129498087a06SJeff Roberson ("page count %d extends beyond swap block", reqcount)); 129598087a06SJeff Roberson 129698087a06SJeff Roberson /* 129798087a06SJeff Roberson * Do not transfer any pages other than those that are xbusied 129898087a06SJeff Roberson * when running during a split or collapse operation. This 129998087a06SJeff Roberson * prevents clustering from re-creating pages which are being 130098087a06SJeff Roberson * moved into another object. 130198087a06SJeff Roberson */ 130298087a06SJeff Roberson if ((object->flags & (OBJ_SPLIT | OBJ_DEAD)) != 0) { 130398087a06SJeff Roberson maxahead = reqcount - 1; 130498087a06SJeff Roberson maxbehind = 0; 130598087a06SJeff Roberson } 130698087a06SJeff Roberson 1307915d1b71SMark Johnston /* 1308915d1b71SMark Johnston * Clip the readahead and readbehind ranges to exclude resident pages. 1309915d1b71SMark Johnston */ 1310915d1b71SMark Johnston if (rahead != NULL) { 1311dd9cb6daSMark Johnston *rahead = imin(*rahead, maxahead - (reqcount - 1)); 13123f060b60SMark Johnston pindex = ma[reqcount - 1]->pindex; 13133f060b60SMark Johnston msucc = TAILQ_NEXT(ma[reqcount - 1], listq); 1314915d1b71SMark Johnston if (msucc != NULL && msucc->pindex - pindex - 1 < *rahead) 1315915d1b71SMark Johnston *rahead = msucc->pindex - pindex - 1; 1316915d1b71SMark Johnston } 1317915d1b71SMark Johnston if (rbehind != NULL) { 1318dd9cb6daSMark Johnston *rbehind = imin(*rbehind, maxbehind); 13193f060b60SMark Johnston pindex = ma[0]->pindex; 13203f060b60SMark Johnston mpred = TAILQ_PREV(ma[0], pglist, listq); 1321915d1b71SMark Johnston if (mpred != NULL && pindex - mpred->pindex - 1 < *rbehind) 1322915d1b71SMark Johnston *rbehind = pindex - mpred->pindex - 1; 1323915d1b71SMark Johnston } 1324915d1b71SMark Johnston 1325b7b8a096SKonstantin Belousov bm = ma[0]; 1326b7b8a096SKonstantin Belousov for (i = 0; i < count; i++) 1327b7b8a096SKonstantin Belousov ma[i]->oflags |= VPO_SWAPINPROG; 1328b7b8a096SKonstantin Belousov 1329915d1b71SMark Johnston /* 1330915d1b71SMark Johnston * Allocate readahead and readbehind pages. 1331915d1b71SMark Johnston */ 1332b7b8a096SKonstantin Belousov if (rbehind != NULL) { 1333b7b8a096SKonstantin Belousov for (i = 1; i <= *rbehind; i++) { 13343f060b60SMark Johnston p = vm_page_alloc(object, ma[0]->pindex - i, 13357667839aSAlan Cox VM_ALLOC_NORMAL); 1336b7b8a096SKonstantin Belousov if (p == NULL) 1337915d1b71SMark Johnston break; 1338b7b8a096SKonstantin Belousov p->oflags |= VPO_SWAPINPROG; 1339b7b8a096SKonstantin Belousov bm = p; 1340915d1b71SMark Johnston } 1341b7b8a096SKonstantin Belousov *rbehind = i - 1; 1342915d1b71SMark Johnston } 1343915d1b71SMark Johnston if (rahead != NULL) { 1344915d1b71SMark Johnston for (i = 0; i < *rahead; i++) { 1345915d1b71SMark Johnston p = vm_page_alloc(object, 13463f060b60SMark Johnston ma[reqcount - 1]->pindex + i + 1, VM_ALLOC_NORMAL); 1347915d1b71SMark Johnston if (p == NULL) 1348915d1b71SMark Johnston break; 1349b7b8a096SKonstantin Belousov p->oflags |= VPO_SWAPINPROG; 1350915d1b71SMark Johnston } 1351915d1b71SMark Johnston *rahead = i; 1352915d1b71SMark Johnston } 1353915d1b71SMark Johnston if (rbehind != NULL) 1354915d1b71SMark Johnston count += *rbehind; 1355915d1b71SMark Johnston if (rahead != NULL) 1356915d1b71SMark Johnston count += *rahead; 1357915d1b71SMark Johnston 1358915d1b71SMark Johnston vm_object_pip_add(object, count); 1359915d1b71SMark Johnston 1360b7b8a096SKonstantin Belousov pindex = bm->pindex; 13618ecbf14bSDoug Moore blk = swp_pager_meta_lookup(object, pindex); 1362915d1b71SMark Johnston KASSERT(blk != SWAPBLK_NONE, 1363915d1b71SMark Johnston ("no swap blocking containing %p(%jx)", object, (uintmax_t)pindex)); 1364915d1b71SMark Johnston 1365915d1b71SMark Johnston VM_OBJECT_WUNLOCK(object); 1366756a5412SGleb Smirnoff bp = uma_zalloc(swrbuf_zone, M_WAITOK); 1367cd853791SKonstantin Belousov MPASS((bp->b_flags & B_MAXPHYS) != 0); 1368b7b8a096SKonstantin Belousov /* Pages cannot leave the object while busy. */ 1369b7b8a096SKonstantin Belousov for (i = 0, p = bm; i < count; i++, p = TAILQ_NEXT(p, listq)) { 1370b7b8a096SKonstantin Belousov MPASS(p->pindex == bm->pindex + i); 1371b7b8a096SKonstantin Belousov bp->b_pages[i] = p; 1372b7b8a096SKonstantin Belousov } 1373915d1b71SMark Johnston 1374915d1b71SMark Johnston bp->b_flags |= B_PAGING; 137521144e3bSPoul-Henning Kamp bp->b_iocmd = BIO_READ; 13761c7c3c6aSMatthew Dillon bp->b_iodone = swp_pager_async_iodone; 1377fdcc1cc0SJohn Baldwin bp->b_rcred = crhold(thread0.td_ucred); 1378fdcc1cc0SJohn Baldwin bp->b_wcred = crhold(thread0.td_ucred); 1379b0cd2017SGleb Smirnoff bp->b_blkno = blk; 1380b0cd2017SGleb Smirnoff bp->b_bcount = PAGE_SIZE * count; 1381b0cd2017SGleb Smirnoff bp->b_bufsize = PAGE_SIZE * count; 1382b0cd2017SGleb Smirnoff bp->b_npages = count; 1383915d1b71SMark Johnston bp->b_pgbefore = rbehind != NULL ? *rbehind : 0; 1384915d1b71SMark Johnston bp->b_pgafter = rahead != NULL ? *rahead : 0; 138526f9a767SRodney W. Grimes 138683c9dea1SGleb Smirnoff VM_CNT_INC(v_swapin); 138783c9dea1SGleb Smirnoff VM_CNT_ADD(v_swappgsin, count); 13881c7c3c6aSMatthew Dillon 13891c7c3c6aSMatthew Dillon /* 13901c7c3c6aSMatthew Dillon * perform the I/O. NOTE!!! bp cannot be considered valid after 13911c7c3c6aSMatthew Dillon * this point because we automatically release it on completion. 13921c7c3c6aSMatthew Dillon * Instead, we look at the one page we are interested in which we 13931c7c3c6aSMatthew Dillon * still hold a lock on even through the I/O completion. 13941c7c3c6aSMatthew Dillon * 13953f060b60SMark Johnston * The other pages in our ma[] array are also released on completion, 13961c7c3c6aSMatthew Dillon * so we cannot assume they are valid anymore either. 13971c7c3c6aSMatthew Dillon * 1398c37a77eeSPoul-Henning Kamp * NOTE: b_blkno is destroyed by the call to swapdev_strategy 13991c7c3c6aSMatthew Dillon */ 1400b890cb2cSPeter Wemm BUF_KERNPROC(bp); 14014b03903aSPoul-Henning Kamp swp_pager_strategy(bp); 140226f9a767SRodney W. Grimes 140326f9a767SRodney W. Grimes /* 1404915d1b71SMark Johnston * Wait for the pages we want to complete. VPO_SWAPINPROG is always 14051c7c3c6aSMatthew Dillon * cleared on completion. If an I/O error occurs, SWAPBLK_NONE 1406915d1b71SMark Johnston * is set in the metadata for each page in the request. 140726f9a767SRodney W. Grimes */ 140889f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1409d6e13f3bSJeff Roberson /* This could be implemented more efficiently with aflags */ 14103f060b60SMark Johnston while ((ma[0]->oflags & VPO_SWAPINPROG) != 0) { 14113f060b60SMark Johnston ma[0]->oflags |= VPO_SWAPSLEEP; 141283c9dea1SGleb Smirnoff VM_CNT_INC(v_intrans); 1413cf27e0d1SJeff Roberson if (VM_OBJECT_SLEEP(object, &object->handle, PSWP, 1414c7aebda8SAttilio Rao "swread", hz * 20)) { 14159bd86a98SBruce M Simpson printf( 1416c5690651SPoul-Henning Kamp "swap_pager: indefinite wait buffer: bufobj: %p, blkno: %jd, size: %ld\n", 1417c5690651SPoul-Henning Kamp bp->b_bufobj, (intmax_t)bp->b_blkno, bp->b_bcount); 14181c7c3c6aSMatthew Dillon } 14191b119d9dSDavid Greenman } 1420d6e13f3bSJeff Roberson VM_OBJECT_WUNLOCK(object); 142126f9a767SRodney W. Grimes 142226f9a767SRodney W. Grimes /* 1423b0cd2017SGleb Smirnoff * If we had an unrecoverable read error pages will not be valid. 142426f9a767SRodney W. Grimes */ 1425915d1b71SMark Johnston for (i = 0; i < reqcount; i++) 14263f060b60SMark Johnston if (ma[i]->valid != VM_PAGE_BITS_ALL) 14271c7c3c6aSMatthew Dillon return (VM_PAGER_ERROR); 1428b0cd2017SGleb Smirnoff 14291c7c3c6aSMatthew Dillon return (VM_PAGER_OK); 14301c7c3c6aSMatthew Dillon 14311c7c3c6aSMatthew Dillon /* 14321c7c3c6aSMatthew Dillon * A final note: in a low swap situation, we cannot deallocate swap 14331c7c3c6aSMatthew Dillon * and mark a page dirty here because the caller is likely to mark 14341c7c3c6aSMatthew Dillon * the page clean when we return, causing the page to possibly revert 14351c7c3c6aSMatthew Dillon * to all-zero's later. 14361c7c3c6aSMatthew Dillon */ 1437df8bae1dSRodney W. Grimes } 1438df8bae1dSRodney W. Grimes 1439c90d075bSMark Johnston static int 1440c90d075bSMark Johnston swap_pager_getpages(vm_object_t object, vm_page_t *ma, int count, 1441c90d075bSMark Johnston int *rbehind, int *rahead) 1442c90d075bSMark Johnston { 1443c90d075bSMark Johnston 1444c90d075bSMark Johnston VM_OBJECT_WLOCK(object); 1445c90d075bSMark Johnston return (swap_pager_getpages_locked(object, ma, count, rbehind, rahead)); 1446c90d075bSMark Johnston } 1447c90d075bSMark Johnston 14481c7c3c6aSMatthew Dillon /* 144990effb23SGleb Smirnoff * swap_pager_getpages_async(): 145090effb23SGleb Smirnoff * 145190effb23SGleb Smirnoff * Right now this is emulation of asynchronous operation on top of 145290effb23SGleb Smirnoff * swap_pager_getpages(). 145390effb23SGleb Smirnoff */ 145490effb23SGleb Smirnoff static int 14553f060b60SMark Johnston swap_pager_getpages_async(vm_object_t object, vm_page_t *ma, int count, 1456b0cd2017SGleb Smirnoff int *rbehind, int *rahead, pgo_getpages_iodone_t iodone, void *arg) 145790effb23SGleb Smirnoff { 145890effb23SGleb Smirnoff int r, error; 145990effb23SGleb Smirnoff 14603f060b60SMark Johnston r = swap_pager_getpages(object, ma, count, rbehind, rahead); 146190effb23SGleb Smirnoff switch (r) { 146290effb23SGleb Smirnoff case VM_PAGER_OK: 146390effb23SGleb Smirnoff error = 0; 146490effb23SGleb Smirnoff break; 146590effb23SGleb Smirnoff case VM_PAGER_ERROR: 146690effb23SGleb Smirnoff error = EIO; 146790effb23SGleb Smirnoff break; 146890effb23SGleb Smirnoff case VM_PAGER_FAIL: 146990effb23SGleb Smirnoff error = EINVAL; 147090effb23SGleb Smirnoff break; 147190effb23SGleb Smirnoff default: 1472d9328101SGleb Smirnoff panic("unhandled swap_pager_getpages() error %d", r); 147390effb23SGleb Smirnoff } 14743f060b60SMark Johnston (iodone)(arg, ma, count, error); 147590effb23SGleb Smirnoff 147690effb23SGleb Smirnoff return (r); 147790effb23SGleb Smirnoff } 147890effb23SGleb Smirnoff 147990effb23SGleb Smirnoff /* 14801c7c3c6aSMatthew Dillon * swap_pager_putpages: 14811c7c3c6aSMatthew Dillon * 14821c7c3c6aSMatthew Dillon * Assign swap (if necessary) and initiate I/O on the specified pages. 14831c7c3c6aSMatthew Dillon * 1484ea3aecf5SPeter Wemm * In a low memory situation we may block in VOP_STRATEGY(), but the new 14851c7c3c6aSMatthew Dillon * vm_page reservation system coupled with properly written VFS devices 14861c7c3c6aSMatthew Dillon * should ensure that no low-memory deadlock occurs. This is an area 14871c7c3c6aSMatthew Dillon * which needs work. 14881c7c3c6aSMatthew Dillon * 14891c7c3c6aSMatthew Dillon * The parent has N vm_object_pip_add() references prior to 14901c7c3c6aSMatthew Dillon * calling us and will remove references for rtvals[] that are 14911c7c3c6aSMatthew Dillon * not set to VM_PAGER_PEND. We need to remove the rest on I/O 14921c7c3c6aSMatthew Dillon * completion. 14931c7c3c6aSMatthew Dillon * 14941c7c3c6aSMatthew Dillon * The parent has soft-busy'd the pages it passes us and will unbusy 149523612f0dSDoug Moore * those whose rtvals[] entry is not set to VM_PAGER_PEND on return. 14961c7c3c6aSMatthew Dillon * We need to unbusy the rest on I/O completion. 14971c7c3c6aSMatthew Dillon */ 1498d635a37fSWarner Losh static void 14993f060b60SMark Johnston swap_pager_putpages(vm_object_t object, vm_page_t *ma, int count, 1500e065e87cSKonstantin Belousov int flags, int *rtvals) 1501df8bae1dSRodney W. Grimes { 1502a880104aSDoug Moore struct page_range range; 150323612f0dSDoug Moore struct buf *bp; 1504a880104aSDoug Moore daddr_t addr, blk; 150523612f0dSDoug Moore vm_page_t mreq; 150623612f0dSDoug Moore int i, j, n; 150723612f0dSDoug Moore bool async; 1508df8bae1dSRodney W. Grimes 150923612f0dSDoug Moore KASSERT(count == 0 || ma[0]->object == object, 151023612f0dSDoug Moore ("%s: object mismatch %p/%p", 151123612f0dSDoug Moore __func__, object, ma[0]->object)); 1512ee3dc7d7SAlan Cox 151389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 151423612f0dSDoug Moore async = curproc == pageproc && (flags & VM_PAGER_PUT_SYNC) == 0; 1515a880104aSDoug Moore swp_pager_init_freerange(&range); 151626f9a767SRodney W. Grimes 15171c7c3c6aSMatthew Dillon /* 15181c7c3c6aSMatthew Dillon * Assign swap blocks and issue I/O. We reallocate swap on the fly. 15191c7c3c6aSMatthew Dillon * The page is left dirty until the pageout operation completes 15201c7c3c6aSMatthew Dillon * successfully. 15211c7c3c6aSMatthew Dillon */ 15221c7c3c6aSMatthew Dillon for (i = 0; i < count; i += n) { 152331c82722SDoug Moore /* Maximum I/O size is limited by maximum swap block size. */ 152431c82722SDoug Moore n = min(count - i, nsw_cluster_max); 15251c7c3c6aSMatthew Dillon 152623612f0dSDoug Moore if (async) { 1527756a5412SGleb Smirnoff mtx_lock(&swbuf_mtx); 1528756a5412SGleb Smirnoff while (nsw_wcount_async == 0) 1529756a5412SGleb Smirnoff msleep(&nsw_wcount_async, &swbuf_mtx, PVM, 1530756a5412SGleb Smirnoff "swbufa", 0); 1531756a5412SGleb Smirnoff nsw_wcount_async--; 1532756a5412SGleb Smirnoff mtx_unlock(&swbuf_mtx); 1533327f4e83SMatthew Dillon } 1534725b4ff0SMark Johnston 1535725b4ff0SMark Johnston /* Get a block of swap of size up to size n. */ 153636b01270SDoug Moore blk = swp_pager_getswapspace(&n); 1537725b4ff0SMark Johnston if (blk == SWAPBLK_NONE) { 1538725b4ff0SMark Johnston mtx_lock(&swbuf_mtx); 1539725b4ff0SMark Johnston if (++nsw_wcount_async == 1) 1540725b4ff0SMark Johnston wakeup(&nsw_wcount_async); 1541725b4ff0SMark Johnston mtx_unlock(&swbuf_mtx); 1542725b4ff0SMark Johnston for (j = 0; j < n; ++j) 1543725b4ff0SMark Johnston rtvals[i + j] = VM_PAGER_FAIL; 1544725b4ff0SMark Johnston continue; 1545725b4ff0SMark Johnston } 154654291f7dSAlan Cox VM_OBJECT_WLOCK(object); 1547725b4ff0SMark Johnston for (j = 0; j < n; ++j) { 1548725b4ff0SMark Johnston mreq = ma[i + j]; 1549725b4ff0SMark Johnston vm_page_aflag_clear(mreq, PGA_SWAP_FREE); 1550725b4ff0SMark Johnston addr = swp_pager_meta_build(mreq->object, mreq->pindex, 1551725b4ff0SMark Johnston blk + j); 1552725b4ff0SMark Johnston if (addr != SWAPBLK_NONE) 1553a880104aSDoug Moore swp_pager_update_freerange(&range, addr); 1554725b4ff0SMark Johnston MPASS(mreq->dirty == VM_PAGE_BITS_ALL); 1555725b4ff0SMark Johnston mreq->oflags |= VPO_SWAPINPROG; 1556725b4ff0SMark Johnston } 1557725b4ff0SMark Johnston VM_OBJECT_WUNLOCK(object); 1558725b4ff0SMark Johnston 1559756a5412SGleb Smirnoff bp = uma_zalloc(swwbuf_zone, M_WAITOK); 1560cd853791SKonstantin Belousov MPASS((bp->b_flags & B_MAXPHYS) != 0); 156123612f0dSDoug Moore if (async) 1562cd853791SKonstantin Belousov bp->b_flags |= B_ASYNC; 15635e04322aSPoul-Henning Kamp bp->b_flags |= B_PAGING; 1564912e4ae9SPoul-Henning Kamp bp->b_iocmd = BIO_WRITE; 156526f9a767SRodney W. Grimes 1566fdcc1cc0SJohn Baldwin bp->b_rcred = crhold(thread0.td_ucred); 1567fdcc1cc0SJohn Baldwin bp->b_wcred = crhold(thread0.td_ucred); 15681c7c3c6aSMatthew Dillon bp->b_bcount = PAGE_SIZE * n; 15691c7c3c6aSMatthew Dillon bp->b_bufsize = PAGE_SIZE * n; 15701c7c3c6aSMatthew Dillon bp->b_blkno = blk; 1571725b4ff0SMark Johnston for (j = 0; j < n; j++) 1572725b4ff0SMark Johnston bp->b_pages[j] = ma[i + j]; 15731c7c3c6aSMatthew Dillon bp->b_npages = n; 1574725b4ff0SMark Johnston 1575a5296b05SJulian Elischer /* 1576a5296b05SJulian Elischer * Must set dirty range for NFS to work. 1577a5296b05SJulian Elischer */ 1578a5296b05SJulian Elischer bp->b_dirtyoff = 0; 1579a5296b05SJulian Elischer bp->b_dirtyend = bp->b_bcount; 15801c7c3c6aSMatthew Dillon 158183c9dea1SGleb Smirnoff VM_CNT_INC(v_swapout); 158283c9dea1SGleb Smirnoff VM_CNT_ADD(v_swappgsout, bp->b_npages); 158326f9a767SRodney W. Grimes 158426f9a767SRodney W. Grimes /* 158577923df2SAlan Cox * We unconditionally set rtvals[] to VM_PAGER_PEND so that we 158677923df2SAlan Cox * can call the async completion routine at the end of a 158777923df2SAlan Cox * synchronous I/O operation. Otherwise, our caller would 158877923df2SAlan Cox * perform duplicate unbusy and wakeup operations on the page 158977923df2SAlan Cox * and object, respectively. 159077923df2SAlan Cox */ 159177923df2SAlan Cox for (j = 0; j < n; j++) 159277923df2SAlan Cox rtvals[i + j] = VM_PAGER_PEND; 159377923df2SAlan Cox 159477923df2SAlan Cox /* 15951c7c3c6aSMatthew Dillon * asynchronous 15961c7c3c6aSMatthew Dillon * 159723612f0dSDoug Moore * NOTE: b_blkno is destroyed by the call to swapdev_strategy. 159826f9a767SRodney W. Grimes */ 159923612f0dSDoug Moore if (async) { 16001c7c3c6aSMatthew Dillon bp->b_iodone = swp_pager_async_iodone; 160167812eacSKirk McKusick BUF_KERNPROC(bp); 16024b03903aSPoul-Henning Kamp swp_pager_strategy(bp); 16031c7c3c6aSMatthew Dillon continue; 160426f9a767SRodney W. Grimes } 1605e47ed70bSJohn Dyson 160626f9a767SRodney W. Grimes /* 16071c7c3c6aSMatthew Dillon * synchronous 16081c7c3c6aSMatthew Dillon * 160923612f0dSDoug Moore * NOTE: b_blkno is destroyed by the call to swapdev_strategy. 16101c7c3c6aSMatthew Dillon */ 16112c840b1fSAlan Cox bp->b_iodone = bdone; 16124b03903aSPoul-Henning Kamp swp_pager_strategy(bp); 16131c7c3c6aSMatthew Dillon 16141c7c3c6aSMatthew Dillon /* 161577923df2SAlan Cox * Wait for the sync I/O to complete. 161626f9a767SRodney W. Grimes */ 16172c840b1fSAlan Cox bwait(bp, PVM, "swwrt"); 161877923df2SAlan Cox 16191c7c3c6aSMatthew Dillon /* 16201c7c3c6aSMatthew Dillon * Now that we are through with the bp, we can call the 16211c7c3c6aSMatthew Dillon * normal async completion, which frees everything up. 16221c7c3c6aSMatthew Dillon */ 16231c7c3c6aSMatthew Dillon swp_pager_async_iodone(bp); 16241c7c3c6aSMatthew Dillon } 1625a880104aSDoug Moore swp_pager_freeswapspace(&range); 162623612f0dSDoug Moore VM_OBJECT_WLOCK(object); 16271c7c3c6aSMatthew Dillon } 16281c7c3c6aSMatthew Dillon 16291c7c3c6aSMatthew Dillon /* 16301c7c3c6aSMatthew Dillon * swp_pager_async_iodone: 16311c7c3c6aSMatthew Dillon * 16321c7c3c6aSMatthew Dillon * Completion routine for asynchronous reads and writes from/to swap. 16331c7c3c6aSMatthew Dillon * Also called manually by synchronous code to finish up a bp. 16341c7c3c6aSMatthew Dillon * 163515523cf7SKonstantin Belousov * This routine may not sleep. 16361c7c3c6aSMatthew Dillon */ 16371c7c3c6aSMatthew Dillon static void 16382f249180SPoul-Henning Kamp swp_pager_async_iodone(struct buf *bp) 16391c7c3c6aSMatthew Dillon { 16401c7c3c6aSMatthew Dillon int i; 16411c7c3c6aSMatthew Dillon vm_object_t object = NULL; 16421c7c3c6aSMatthew Dillon 16431c7c3c6aSMatthew Dillon /* 16440b208315SEdward Tomasz Napierala * Report error - unless we ran out of memory, in which case 16450b208315SEdward Tomasz Napierala * we've already logged it in swapgeom_strategy(). 16461c7c3c6aSMatthew Dillon */ 16470b208315SEdward Tomasz Napierala if (bp->b_ioflags & BIO_ERROR && bp->b_error != ENOMEM) { 16481c7c3c6aSMatthew Dillon printf( 16491c7c3c6aSMatthew Dillon "swap_pager: I/O error - %s failed; blkno %ld," 16501c7c3c6aSMatthew Dillon "size %ld, error %d\n", 165121144e3bSPoul-Henning Kamp ((bp->b_iocmd == BIO_READ) ? "pagein" : "pageout"), 16521c7c3c6aSMatthew Dillon (long)bp->b_blkno, 16531c7c3c6aSMatthew Dillon (long)bp->b_bcount, 16541c7c3c6aSMatthew Dillon bp->b_error 16551c7c3c6aSMatthew Dillon ); 16561c7c3c6aSMatthew Dillon } 16571c7c3c6aSMatthew Dillon 16581c7c3c6aSMatthew Dillon /* 165926f9a767SRodney W. Grimes * remove the mapping for kernel virtual 166026f9a767SRodney W. Grimes */ 1661fade8dd7SJeff Roberson if (buf_mapped(bp)) 16621c7c3c6aSMatthew Dillon pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages); 1663fade8dd7SJeff Roberson else 1664fade8dd7SJeff Roberson bp->b_data = bp->b_kvabase; 166526f9a767SRodney W. Grimes 166633a609ecSAlan Cox if (bp->b_npages) { 166733a609ecSAlan Cox object = bp->b_pages[0]->object; 166889f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 166933a609ecSAlan Cox } 16702965a453SKip Macy 167126f9a767SRodney W. Grimes /* 16721c7c3c6aSMatthew Dillon * cleanup pages. If an error occurs writing to swap, we are in 16731c7c3c6aSMatthew Dillon * very serious trouble. If it happens to be a disk error, though, 16741c7c3c6aSMatthew Dillon * we may be able to recover by reassigning the swap later on. So 16751c7c3c6aSMatthew Dillon * in this case we remove the m->swapblk assignment for the page 16761c7c3c6aSMatthew Dillon * but do not free it in the rlist. The errornous block(s) are thus 16771c7c3c6aSMatthew Dillon * never reallocated as swap. Redirty the page and continue. 167826f9a767SRodney W. Grimes */ 16791c7c3c6aSMatthew Dillon for (i = 0; i < bp->b_npages; ++i) { 16801c7c3c6aSMatthew Dillon vm_page_t m = bp->b_pages[i]; 1681e47ed70bSJohn Dyson 16825786be7cSAlan Cox m->oflags &= ~VPO_SWAPINPROG; 1683c7aebda8SAttilio Rao if (m->oflags & VPO_SWAPSLEEP) { 1684c7aebda8SAttilio Rao m->oflags &= ~VPO_SWAPSLEEP; 1685cf27e0d1SJeff Roberson wakeup(&object->handle); 1686c7aebda8SAttilio Rao } 1687e47ed70bSJohn Dyson 1688a8081778SJeff Roberson /* We always have space after I/O, successful or not. */ 1689a8081778SJeff Roberson vm_page_aflag_set(m, PGA_SWAP_SPACE); 1690a8081778SJeff Roberson 1691c244d2deSPoul-Henning Kamp if (bp->b_ioflags & BIO_ERROR) { 1692ffc82b0aSJohn Dyson /* 16931c7c3c6aSMatthew Dillon * If an error occurs I'd love to throw the swapblk 16941c7c3c6aSMatthew Dillon * away without freeing it back to swapspace, so it 16951c7c3c6aSMatthew Dillon * can never be used again. But I can't from an 16961c7c3c6aSMatthew Dillon * interrupt. 1697ffc82b0aSJohn Dyson */ 169821144e3bSPoul-Henning Kamp if (bp->b_iocmd == BIO_READ) { 16991c7c3c6aSMatthew Dillon /* 17001c7c3c6aSMatthew Dillon * NOTE: for reads, m->dirty will probably 1701956f3135SPhilippe Charnier * be overridden by the original caller of 17021c7c3c6aSMatthew Dillon * getpages so don't play cute tricks here. 17031c7c3c6aSMatthew Dillon */ 17040012f373SJeff Roberson vm_page_invalid(m); 170546966507SMark Johnston if (i < bp->b_pgbefore || 170646966507SMark Johnston i >= bp->b_npages - bp->b_pgafter) 170746966507SMark Johnston vm_page_free_invalid(m); 17081c7c3c6aSMatthew Dillon } else { 17091c7c3c6aSMatthew Dillon /* 17101c7c3c6aSMatthew Dillon * If a write error occurs, reactivate page 17111c7c3c6aSMatthew Dillon * so it doesn't clog the inactive list, 17121c7c3c6aSMatthew Dillon * then finish the I/O. 17131c7c3c6aSMatthew Dillon */ 171441e5a226SAlan Cox MPASS(m->dirty == VM_PAGE_BITS_ALL); 17159f5632e6SMark Johnston 1716a8081778SJeff Roberson /* PQ_UNSWAPPABLE? */ 17171c7c3c6aSMatthew Dillon vm_page_activate(m); 1718c7aebda8SAttilio Rao vm_page_sunbusy(m); 17191c7c3c6aSMatthew Dillon } 172021144e3bSPoul-Henning Kamp } else if (bp->b_iocmd == BIO_READ) { 17211c7c3c6aSMatthew Dillon /* 17221c7c3c6aSMatthew Dillon * NOTE: for reads, m->dirty will probably be 1723956f3135SPhilippe Charnier * overridden by the original caller of getpages so 17241c7c3c6aSMatthew Dillon * we cannot set them in order to free the underlying 17251c7c3c6aSMatthew Dillon * swap in a low-swap situation. I don't think we'd 17261c7c3c6aSMatthew Dillon * want to do that anyway, but it was an optimization 17271c7c3c6aSMatthew Dillon * that existed in the old swapper for a time before 17281c7c3c6aSMatthew Dillon * it got ripped out due to precisely this problem. 17291c7c3c6aSMatthew Dillon */ 1730016a3c93SAlan Cox KASSERT(!pmap_page_is_mapped(m), 1731016a3c93SAlan Cox ("swp_pager_async_iodone: page %p is mapped", m)); 1732016a3c93SAlan Cox KASSERT(m->dirty == 0, 1733016a3c93SAlan Cox ("swp_pager_async_iodone: page %p is dirty", m)); 1734915d1b71SMark Johnston 17350012f373SJeff Roberson vm_page_valid(m); 1736915d1b71SMark Johnston if (i < bp->b_pgbefore || 1737915d1b71SMark Johnston i >= bp->b_npages - bp->b_pgafter) 1738915d1b71SMark Johnston vm_page_readahead_finish(m); 17391c7c3c6aSMatthew Dillon } else { 17401c7c3c6aSMatthew Dillon /* 1741016a3c93SAlan Cox * For write success, clear the dirty 17421c7c3c6aSMatthew Dillon * status, then finish the I/O ( which decrements the 17431c7c3c6aSMatthew Dillon * busy count and possibly wakes waiter's up ). 1744ebcddc72SAlan Cox * A page is only written to swap after a period of 1745ebcddc72SAlan Cox * inactivity. Therefore, we do not expect it to be 1746ebcddc72SAlan Cox * reused. 17471c7c3c6aSMatthew Dillon */ 17486031c68dSAlan Cox KASSERT(!pmap_page_is_write_mapped(m), 1749016a3c93SAlan Cox ("swp_pager_async_iodone: page %p is not write" 1750016a3c93SAlan Cox " protected", m)); 1751c52e7044SAlan Cox vm_page_undirty(m); 1752ebcddc72SAlan Cox vm_page_deactivate_noreuse(m); 1753ebcddc72SAlan Cox vm_page_sunbusy(m); 17543c4a2440SAlan Cox } 17553c4a2440SAlan Cox } 175626f9a767SRodney W. Grimes 17571c7c3c6aSMatthew Dillon /* 17581c7c3c6aSMatthew Dillon * adjust pip. NOTE: the original parent may still have its own 17591c7c3c6aSMatthew Dillon * pip refs on the object. 17601c7c3c6aSMatthew Dillon */ 17610d420ad3SAlan Cox if (object != NULL) { 17621c7c3c6aSMatthew Dillon vm_object_pip_wakeupn(object, bp->b_npages); 176389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 17640d420ad3SAlan Cox } 176526f9a767SRodney W. Grimes 17661c7c3c6aSMatthew Dillon /* 1767100650deSOlivier Houchard * swapdev_strategy() manually sets b_vp and b_bufobj before calling 1768100650deSOlivier Houchard * bstrategy(). Set them back to NULL now we're done with it, or we'll 1769100650deSOlivier Houchard * trigger a KASSERT in relpbuf(). 1770100650deSOlivier Houchard */ 1771100650deSOlivier Houchard if (bp->b_vp) { 1772100650deSOlivier Houchard bp->b_vp = NULL; 1773100650deSOlivier Houchard bp->b_bufobj = NULL; 1774100650deSOlivier Houchard } 1775100650deSOlivier Houchard /* 17761c7c3c6aSMatthew Dillon * release the physical I/O buffer 17771c7c3c6aSMatthew Dillon */ 1778756a5412SGleb Smirnoff if (bp->b_flags & B_ASYNC) { 1779756a5412SGleb Smirnoff mtx_lock(&swbuf_mtx); 1780756a5412SGleb Smirnoff if (++nsw_wcount_async == 1) 1781756a5412SGleb Smirnoff wakeup(&nsw_wcount_async); 1782756a5412SGleb Smirnoff mtx_unlock(&swbuf_mtx); 1783756a5412SGleb Smirnoff } 1784756a5412SGleb Smirnoff uma_zfree((bp->b_iocmd == BIO_READ) ? swrbuf_zone : swwbuf_zone, bp); 178526f9a767SRodney W. Grimes } 17861c7c3c6aSMatthew Dillon 1787b1fd102eSMark Johnston int 1788b1fd102eSMark Johnston swap_pager_nswapdev(void) 1789b1fd102eSMark Johnston { 1790b1fd102eSMark Johnston 1791b1fd102eSMark Johnston return (nswapdev); 1792b1fd102eSMark Johnston } 1793b1fd102eSMark Johnston 17947c022327SDoug Moore static void 1795*995730a6SDoug Moore swp_pager_force_dirty(struct page_range *range, vm_page_t m, daddr_t *blk) 17967c022327SDoug Moore { 17977c022327SDoug Moore vm_page_dirty(m); 1798*995730a6SDoug Moore swap_pager_unswapped_acct(m); 1799*995730a6SDoug Moore swp_pager_update_freerange(range, *blk); 1800*995730a6SDoug Moore *blk = SWAPBLK_NONE; 18017c022327SDoug Moore vm_page_launder(m); 180292da00bbSMatthew Dillon } 180392da00bbSMatthew Dillon 1804ecfbddf0SKonstantin Belousov u_long 1805ecfbddf0SKonstantin Belousov swap_pager_swapped_pages(vm_object_t object) 1806ecfbddf0SKonstantin Belousov { 1807ecfbddf0SKonstantin Belousov struct swblk *sb; 1808ecfbddf0SKonstantin Belousov vm_pindex_t pi; 1809ecfbddf0SKonstantin Belousov u_long res; 1810ecfbddf0SKonstantin Belousov int i; 1811ecfbddf0SKonstantin Belousov 1812ecfbddf0SKonstantin Belousov VM_OBJECT_ASSERT_LOCKED(object); 1813cb6757c0SMark Johnston 1814cb6757c0SMark Johnston if (pctrie_is_empty(&object->un_pager.swp.swp_blks)) 1815ecfbddf0SKonstantin Belousov return (0); 1816ecfbddf0SKonstantin Belousov 1817ecfbddf0SKonstantin Belousov for (res = 0, pi = 0; (sb = SWAP_PCTRIE_LOOKUP_GE( 1818ecfbddf0SKonstantin Belousov &object->un_pager.swp.swp_blks, pi)) != NULL; 1819ecfbddf0SKonstantin Belousov pi = sb->p + SWAP_META_PAGES) { 1820ecfbddf0SKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) { 1821ecfbddf0SKonstantin Belousov if (sb->d[i] != SWAPBLK_NONE) 1822ecfbddf0SKonstantin Belousov res++; 1823ecfbddf0SKonstantin Belousov } 1824ecfbddf0SKonstantin Belousov } 1825ecfbddf0SKonstantin Belousov return (res); 1826ecfbddf0SKonstantin Belousov } 1827ecfbddf0SKonstantin Belousov 182892da00bbSMatthew Dillon /* 18297c022327SDoug Moore * swap_pager_swapoff_object: 18307c022327SDoug Moore * 18317c022327SDoug Moore * Page in all of the pages that have been paged out for an object 183256948d17SDoug Moore * to a swap device. 18337c022327SDoug Moore */ 18347c022327SDoug Moore static void 18357c022327SDoug Moore swap_pager_swapoff_object(struct swdevt *sp, vm_object_t object) 18367c022327SDoug Moore { 1837*995730a6SDoug Moore struct page_range range; 18387c022327SDoug Moore struct swblk *sb; 1839c90d075bSMark Johnston vm_page_t m; 1840c90d075bSMark Johnston vm_pindex_t pi; 1841c90d075bSMark Johnston daddr_t blk; 1842*995730a6SDoug Moore int i, rahead, rv; 1843*995730a6SDoug Moore bool sb_empty; 18447c022327SDoug Moore 1845*995730a6SDoug Moore VM_OBJECT_ASSERT_WLOCKED(object); 18464b8365d7SKonstantin Belousov KASSERT((object->flags & OBJ_SWAP) != 0, 18478ecbf14bSDoug Moore ("%s: Object not swappable", __func__)); 1848c90d075bSMark Johnston 1849*995730a6SDoug Moore pi = 0; 1850*995730a6SDoug Moore i = 0; 1851*995730a6SDoug Moore swp_pager_init_freerange(&range); 1852*995730a6SDoug Moore for (;;) { 1853*995730a6SDoug Moore if (i == 0 && (object->flags & OBJ_DEAD) != 0) { 1854c90d075bSMark Johnston /* 1855c90d075bSMark Johnston * Make sure that pending writes finish before 1856c90d075bSMark Johnston * returning. 1857c90d075bSMark Johnston */ 1858c90d075bSMark Johnston vm_object_pip_wait(object, "swpoff"); 1859c90d075bSMark Johnston swp_pager_meta_free_all(object); 1860c90d075bSMark Johnston break; 1861c90d075bSMark Johnston } 1862*995730a6SDoug Moore 1863*995730a6SDoug Moore if (i == SWAP_META_PAGES) { 1864*995730a6SDoug Moore pi = sb->p + SWAP_META_PAGES; 1865*995730a6SDoug Moore if (sb_empty) { 1866*995730a6SDoug Moore SWAP_PCTRIE_REMOVE( 1867*995730a6SDoug Moore &object->un_pager.swp.swp_blks, sb->p); 1868*995730a6SDoug Moore uma_zfree(swblk_zone, sb); 186956948d17SDoug Moore } 1870*995730a6SDoug Moore i = 0; 1871*995730a6SDoug Moore } 1872*995730a6SDoug Moore 1873*995730a6SDoug Moore if (i == 0) { 1874*995730a6SDoug Moore sb = SWAP_PCTRIE_LOOKUP_GE( 1875*995730a6SDoug Moore &object->un_pager.swp.swp_blks, pi); 1876*995730a6SDoug Moore if (sb == NULL) 1877*995730a6SDoug Moore break; 1878*995730a6SDoug Moore sb_empty = true; 1879*995730a6SDoug Moore m = NULL; 1880*995730a6SDoug Moore } 1881*995730a6SDoug Moore 1882*995730a6SDoug Moore /* Skip an invalid block. */ 1883*995730a6SDoug Moore blk = sb->d[i]; 1884*995730a6SDoug Moore if (blk == SWAPBLK_NONE || !swp_pager_isondev(blk, sp)) { 1885*995730a6SDoug Moore if (blk != SWAPBLK_NONE) 1886*995730a6SDoug Moore sb_empty = false; 1887*995730a6SDoug Moore m = NULL; 1888*995730a6SDoug Moore i++; 18897c022327SDoug Moore continue; 1890*995730a6SDoug Moore } 189156948d17SDoug Moore 189256948d17SDoug Moore /* 1893*995730a6SDoug Moore * Look for a page corresponding to this block, If the found 1894*995730a6SDoug Moore * page has pending operations, sleep and restart the scan. 189556948d17SDoug Moore */ 1896*995730a6SDoug Moore m = m != NULL ? vm_page_next(m) : 1897*995730a6SDoug Moore vm_page_lookup(object, sb->p + i); 1898*995730a6SDoug Moore if (m != NULL && (m->oflags & VPO_SWAPINPROG) != 0) { 1899*995730a6SDoug Moore m->oflags |= VPO_SWAPSLEEP; 1900*995730a6SDoug Moore VM_OBJECT_SLEEP(object, &object->handle, PSWP, "swpoff", 1901*995730a6SDoug Moore 0); 1902*995730a6SDoug Moore i = 0; /* Restart scan after object lock dropped. */ 1903*995730a6SDoug Moore continue; 1904*995730a6SDoug Moore } 1905*995730a6SDoug Moore 1906*995730a6SDoug Moore /* 1907*995730a6SDoug Moore * If the found page is valid, mark it dirty and free the swap 1908*995730a6SDoug Moore * block. 1909*995730a6SDoug Moore */ 1910*995730a6SDoug Moore if (m != NULL && vm_page_all_valid(m)) { 1911*995730a6SDoug Moore swp_pager_force_dirty(&range, m, &sb->d[i]); 1912*995730a6SDoug Moore i++; 1913*995730a6SDoug Moore continue; 1914*995730a6SDoug Moore } 1915*995730a6SDoug Moore 1916*995730a6SDoug Moore /* Is there a page we can acquire or allocate? */ 1917c90d075bSMark Johnston if (m == NULL) { 1918c90d075bSMark Johnston m = vm_page_alloc(object, sb->p + i, 1919c90d075bSMark Johnston VM_ALLOC_NORMAL | VM_ALLOC_WAITFAIL); 1920*995730a6SDoug Moore } else if (!vm_page_busy_acquire(m, VM_ALLOC_WAITFAIL)) 1921*995730a6SDoug Moore m = NULL; 192256948d17SDoug Moore 1923*995730a6SDoug Moore /* If no page available, repeat this iteration. */ 1924*995730a6SDoug Moore if (m == NULL) 1925*995730a6SDoug Moore continue; 1926*995730a6SDoug Moore 1927*995730a6SDoug Moore /* Get the page from swap, mark it dirty, restart the scan. */ 1928c90d075bSMark Johnston vm_object_pip_add(object, 1); 1929c90d075bSMark Johnston rahead = SWAP_META_PAGES; 1930*995730a6SDoug Moore rv = swap_pager_getpages_locked(object, &m, 1, NULL, &rahead); 1931c90d075bSMark Johnston if (rv != VM_PAGER_OK) 1932*995730a6SDoug Moore panic("%s: read from swap failed: %d", __func__, rv); 1933c90d075bSMark Johnston VM_OBJECT_WLOCK(object); 1934e61568aeSMark Johnston vm_object_pip_wakeupn(object, 1); 1935*995730a6SDoug Moore KASSERT(vm_page_all_valid(m), 1936*995730a6SDoug Moore ("%s: Page %p not all valid", __func__, m)); 1937*995730a6SDoug Moore swp_pager_force_dirty(&range, m, &sb->d[i]); 1938c90d075bSMark Johnston vm_page_xunbusy(m); 1939*995730a6SDoug Moore i = 0; /* Restart scan after object lock dropped. */ 194056948d17SDoug Moore } 1941*995730a6SDoug Moore swp_pager_freeswapspace(&range); 19427c022327SDoug Moore } 19437c022327SDoug Moore 19447c022327SDoug Moore /* 194592da00bbSMatthew Dillon * swap_pager_swapoff: 194692da00bbSMatthew Dillon * 194792da00bbSMatthew Dillon * Page in all of the pages that have been paged out to the 194892da00bbSMatthew Dillon * given device. The corresponding blocks in the bitmap must be 194992da00bbSMatthew Dillon * marked as allocated and the device must be flagged SW_CLOSING. 195092da00bbSMatthew Dillon * There may be no processes swapped out to the device. 195192da00bbSMatthew Dillon * 195292da00bbSMatthew Dillon * This routine may block. 195392da00bbSMatthew Dillon */ 1954e9c0cc15SPoul-Henning Kamp static void 1955b3fed13eSDavid Schultz swap_pager_swapoff(struct swdevt *sp) 195692da00bbSMatthew Dillon { 1957f425ab8eSKonstantin Belousov vm_object_t object; 19587c022327SDoug Moore int retries; 195992da00bbSMatthew Dillon 196004533e1eSKonstantin Belousov sx_assert(&swdev_syscall_lock, SA_XLOCKED); 196192da00bbSMatthew Dillon 19628bc61209SDavid Schultz retries = 0; 196392da00bbSMatthew Dillon full_rescan: 1964f425ab8eSKonstantin Belousov mtx_lock(&vm_object_list_mtx); 1965f425ab8eSKonstantin Belousov TAILQ_FOREACH(object, &vm_object_list, object_list) { 19664b8365d7SKonstantin Belousov if ((object->flags & OBJ_SWAP) == 0) 196798150664SKonstantin Belousov continue; 1968f425ab8eSKonstantin Belousov mtx_unlock(&vm_object_list_mtx); 196998150664SKonstantin Belousov /* Depends on type-stability. */ 197098150664SKonstantin Belousov VM_OBJECT_WLOCK(object); 1971f425ab8eSKonstantin Belousov 1972f425ab8eSKonstantin Belousov /* 1973f425ab8eSKonstantin Belousov * Dead objects are eventually terminated on their own. 1974f425ab8eSKonstantin Belousov */ 1975f425ab8eSKonstantin Belousov if ((object->flags & OBJ_DEAD) != 0) 1976f425ab8eSKonstantin Belousov goto next_obj; 1977f425ab8eSKonstantin Belousov 1978f425ab8eSKonstantin Belousov /* 1979f425ab8eSKonstantin Belousov * Sync with fences placed after pctrie 1980f425ab8eSKonstantin Belousov * initialization. We must not access pctrie below 1981f425ab8eSKonstantin Belousov * unless we checked that our object is swap and not 1982f425ab8eSKonstantin Belousov * dead. 1983f425ab8eSKonstantin Belousov */ 1984f425ab8eSKonstantin Belousov atomic_thread_fence_acq(); 19854b8365d7SKonstantin Belousov if ((object->flags & OBJ_SWAP) == 0) 1986f425ab8eSKonstantin Belousov goto next_obj; 1987f425ab8eSKonstantin Belousov 19887c022327SDoug Moore swap_pager_swapoff_object(sp, object); 1989f425ab8eSKonstantin Belousov next_obj: 1990f425ab8eSKonstantin Belousov VM_OBJECT_WUNLOCK(object); 1991f425ab8eSKonstantin Belousov mtx_lock(&vm_object_list_mtx); 199292da00bbSMatthew Dillon } 1993f425ab8eSKonstantin Belousov mtx_unlock(&vm_object_list_mtx); 1994f425ab8eSKonstantin Belousov 19958bc61209SDavid Schultz if (sp->sw_used) { 199692da00bbSMatthew Dillon /* 19978bc61209SDavid Schultz * Objects may be locked or paging to the device being 19988bc61209SDavid Schultz * removed, so we will miss their pages and need to 19998bc61209SDavid Schultz * make another pass. We have marked this device as 20008bc61209SDavid Schultz * SW_CLOSING, so the activity should finish soon. 200192da00bbSMatthew Dillon */ 20028bc61209SDavid Schultz retries++; 20038bc61209SDavid Schultz if (retries > 100) { 20048bc61209SDavid Schultz panic("swapoff: failed to locate %d swap blocks", 20058bc61209SDavid Schultz sp->sw_used); 20068bc61209SDavid Schultz } 20074d70511aSJohn Baldwin pause("swpoff", hz / 20); 200892da00bbSMatthew Dillon goto full_rescan; 200992da00bbSMatthew Dillon } 2010b1fd102eSMark Johnston EVENTHANDLER_INVOKE(swapoff, sp); 201192da00bbSMatthew Dillon } 201292da00bbSMatthew Dillon 20131c7c3c6aSMatthew Dillon /************************************************************************ 20141c7c3c6aSMatthew Dillon * SWAP META DATA * 20151c7c3c6aSMatthew Dillon ************************************************************************ 20161c7c3c6aSMatthew Dillon * 20171c7c3c6aSMatthew Dillon * These routines manipulate the swap metadata stored in the 2018cec9f109SDavid E. O'Brien * OBJT_SWAP object. 20191c7c3c6aSMatthew Dillon * 20204dcc5c2dSMatthew Dillon * Swap metadata is implemented with a global hash and not directly 20214dcc5c2dSMatthew Dillon * linked into the object. Instead the object simply contains 20224dcc5c2dSMatthew Dillon * appropriate tracking counters. 20231c7c3c6aSMatthew Dillon */ 20241c7c3c6aSMatthew Dillon 20251c7c3c6aSMatthew Dillon /* 2026230869e0SAlan Cox * SWP_PAGER_SWBLK_EMPTY() - is a range of blocks free? 2027230869e0SAlan Cox */ 2028230869e0SAlan Cox static bool 2029230869e0SAlan Cox swp_pager_swblk_empty(struct swblk *sb, int start, int limit) 2030230869e0SAlan Cox { 2031230869e0SAlan Cox int i; 2032230869e0SAlan Cox 2033230869e0SAlan Cox MPASS(0 <= start && start <= limit && limit <= SWAP_META_PAGES); 2034230869e0SAlan Cox for (i = start; i < limit; i++) { 2035230869e0SAlan Cox if (sb->d[i] != SWAPBLK_NONE) 2036230869e0SAlan Cox return (false); 2037230869e0SAlan Cox } 2038230869e0SAlan Cox return (true); 2039230869e0SAlan Cox } 2040230869e0SAlan Cox 2041230869e0SAlan Cox /* 2042abdab7b6SDoug Moore * SWP_PAGER_FREE_EMPTY_SWBLK() - frees if a block is free 2043abdab7b6SDoug Moore * 2044abdab7b6SDoug Moore * Nothing is done if the block is still in use. 2045abdab7b6SDoug Moore */ 2046abdab7b6SDoug Moore static void 2047abdab7b6SDoug Moore swp_pager_free_empty_swblk(vm_object_t object, struct swblk *sb) 2048abdab7b6SDoug Moore { 2049abdab7b6SDoug Moore 2050abdab7b6SDoug Moore if (swp_pager_swblk_empty(sb, 0, SWAP_META_PAGES)) { 2051abdab7b6SDoug Moore SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, sb->p); 2052abdab7b6SDoug Moore uma_zfree(swblk_zone, sb); 2053abdab7b6SDoug Moore } 2054abdab7b6SDoug Moore } 2055abdab7b6SDoug Moore 2056abdab7b6SDoug Moore /* 20571c7c3c6aSMatthew Dillon * SWP_PAGER_META_BUILD() - add swap block to swap meta data for object 20581c7c3c6aSMatthew Dillon * 20591c7c3c6aSMatthew Dillon * The specified swapblk is added to the object's swap metadata. If 20601c7c3c6aSMatthew Dillon * the swapblk is not valid, it is freed instead. Any previously 206178f1deefSAlan Cox * assigned swapblk is returned. 20621c7c3c6aSMatthew Dillon */ 206378f1deefSAlan Cox static daddr_t 20642f249180SPoul-Henning Kamp swp_pager_meta_build(vm_object_t object, vm_pindex_t pindex, daddr_t swapblk) 20652f249180SPoul-Henning Kamp { 2066f425ab8eSKonstantin Belousov static volatile int swblk_zone_exhausted, swpctrie_zone_exhausted; 2067eed99cb8SKonstantin Belousov struct swblk *sb, *sb1; 2068f425ab8eSKonstantin Belousov vm_pindex_t modpi, rdpi; 206978f1deefSAlan Cox daddr_t prev_swapblk; 2070f425ab8eSKonstantin Belousov int error, i; 20711c7c3c6aSMatthew Dillon 207289f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 2073f425ab8eSKonstantin Belousov 2074f425ab8eSKonstantin Belousov rdpi = rounddown(pindex, SWAP_META_PAGES); 2075f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, rdpi); 2076f425ab8eSKonstantin Belousov if (sb == NULL) { 20771c7c3c6aSMatthew Dillon if (swapblk == SWAPBLK_NONE) 207878f1deefSAlan Cox return (SWAPBLK_NONE); 2079f425ab8eSKonstantin Belousov for (;;) { 2080f425ab8eSKonstantin Belousov sb = uma_zalloc(swblk_zone, M_NOWAIT | (curproc == 2081f425ab8eSKonstantin Belousov pageproc ? M_USE_RESERVE : 0)); 2082f425ab8eSKonstantin Belousov if (sb != NULL) { 2083f425ab8eSKonstantin Belousov sb->p = rdpi; 2084f425ab8eSKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) 2085f425ab8eSKonstantin Belousov sb->d[i] = SWAPBLK_NONE; 2086f425ab8eSKonstantin Belousov if (atomic_cmpset_int(&swblk_zone_exhausted, 2087f425ab8eSKonstantin Belousov 1, 0)) 2088f425ab8eSKonstantin Belousov printf("swblk zone ok\n"); 2089f425ab8eSKonstantin Belousov break; 2090f425ab8eSKonstantin Belousov } 209189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 2092f425ab8eSKonstantin Belousov if (uma_zone_exhausted(swblk_zone)) { 2093f425ab8eSKonstantin Belousov if (atomic_cmpset_int(&swblk_zone_exhausted, 2094f425ab8eSKonstantin Belousov 0, 1)) 2095f425ab8eSKonstantin Belousov printf("swap blk zone exhausted, " 20963ff863f1SDag-Erling Smørgrav "increase kern.maxswzone\n"); 20972025d69bSKonstantin Belousov vm_pageout_oom(VM_OOM_SWAPZ); 2098f425ab8eSKonstantin Belousov pause("swzonxb", 10); 20992025d69bSKonstantin Belousov } else 21008d6fbbb8SJeff Roberson uma_zwait(swblk_zone); 210189f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 2102eed99cb8SKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, 2103eed99cb8SKonstantin Belousov rdpi); 2104eed99cb8SKonstantin Belousov if (sb != NULL) 2105eed99cb8SKonstantin Belousov /* 2106eed99cb8SKonstantin Belousov * Somebody swapped out a nearby page, 2107eed99cb8SKonstantin Belousov * allocating swblk at the rdpi index, 2108eed99cb8SKonstantin Belousov * while we dropped the object lock. 2109eed99cb8SKonstantin Belousov */ 2110eed99cb8SKonstantin Belousov goto allocated; 21114dcc5c2dSMatthew Dillon } 2112f425ab8eSKonstantin Belousov for (;;) { 2113f425ab8eSKonstantin Belousov error = SWAP_PCTRIE_INSERT( 2114f425ab8eSKonstantin Belousov &object->un_pager.swp.swp_blks, sb); 2115f425ab8eSKonstantin Belousov if (error == 0) { 2116f425ab8eSKonstantin Belousov if (atomic_cmpset_int(&swpctrie_zone_exhausted, 2117f425ab8eSKonstantin Belousov 1, 0)) 2118f425ab8eSKonstantin Belousov printf("swpctrie zone ok\n"); 2119f425ab8eSKonstantin Belousov break; 21201c7c3c6aSMatthew Dillon } 2121f425ab8eSKonstantin Belousov VM_OBJECT_WUNLOCK(object); 2122f425ab8eSKonstantin Belousov if (uma_zone_exhausted(swpctrie_zone)) { 2123f425ab8eSKonstantin Belousov if (atomic_cmpset_int(&swpctrie_zone_exhausted, 2124f425ab8eSKonstantin Belousov 0, 1)) 2125f425ab8eSKonstantin Belousov printf("swap pctrie zone exhausted, " 2126f425ab8eSKonstantin Belousov "increase kern.maxswzone\n"); 2127f425ab8eSKonstantin Belousov vm_pageout_oom(VM_OOM_SWAPZ); 2128f425ab8eSKonstantin Belousov pause("swzonxp", 10); 2129f425ab8eSKonstantin Belousov } else 21308d6fbbb8SJeff Roberson uma_zwait(swpctrie_zone); 2131f425ab8eSKonstantin Belousov VM_OBJECT_WLOCK(object); 2132eed99cb8SKonstantin Belousov sb1 = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, 2133eed99cb8SKonstantin Belousov rdpi); 2134eed99cb8SKonstantin Belousov if (sb1 != NULL) { 2135eed99cb8SKonstantin Belousov uma_zfree(swblk_zone, sb); 2136eed99cb8SKonstantin Belousov sb = sb1; 2137eed99cb8SKonstantin Belousov goto allocated; 21381c7c3c6aSMatthew Dillon } 2139f425ab8eSKonstantin Belousov } 2140eed99cb8SKonstantin Belousov } 2141eed99cb8SKonstantin Belousov allocated: 2142f425ab8eSKonstantin Belousov MPASS(sb->p == rdpi); 21431c7c3c6aSMatthew Dillon 2144f425ab8eSKonstantin Belousov modpi = pindex % SWAP_META_PAGES; 214578f1deefSAlan Cox /* Return prior contents of metadata. */ 214678f1deefSAlan Cox prev_swapblk = sb->d[modpi]; 2147f425ab8eSKonstantin Belousov /* Enter block into metadata. */ 2148f425ab8eSKonstantin Belousov sb->d[modpi] = swapblk; 214985d88d87SKonstantin Belousov 215085d88d87SKonstantin Belousov /* 215185d88d87SKonstantin Belousov * Free the swblk if we end up with the empty page run. 215285d88d87SKonstantin Belousov */ 2153abdab7b6SDoug Moore if (swapblk == SWAPBLK_NONE) 2154abdab7b6SDoug Moore swp_pager_free_empty_swblk(object, sb); 215578f1deefSAlan Cox return (prev_swapblk); 215685d88d87SKonstantin Belousov } 21571c7c3c6aSMatthew Dillon 21581c7c3c6aSMatthew Dillon /* 2159467057fcSDoug Moore * SWP_PAGER_META_TRANSFER() - free a range of blocks in the srcobject's swap 2160467057fcSDoug Moore * metadata, or transfer it into dstobject. 2161467057fcSDoug Moore * 2162467057fcSDoug Moore * This routine will free swap metadata structures as they are cleaned 2163467057fcSDoug Moore * out. 2164467057fcSDoug Moore */ 2165467057fcSDoug Moore static void 2166467057fcSDoug Moore swp_pager_meta_transfer(vm_object_t srcobject, vm_object_t dstobject, 2167baa1ccceSKonstantin Belousov vm_pindex_t pindex, vm_pindex_t count, vm_size_t *moved) 2168467057fcSDoug Moore { 2169a880104aSDoug Moore struct page_range range; 2170467057fcSDoug Moore struct swblk *sb; 2171baa1ccceSKonstantin Belousov vm_page_t m; 2172467057fcSDoug Moore vm_pindex_t offset, last; 2173baa1ccceSKonstantin Belousov vm_size_t mc; 2174467057fcSDoug Moore int i, limit, start; 2175467057fcSDoug Moore 2176467057fcSDoug Moore VM_OBJECT_ASSERT_WLOCKED(srcobject); 2177baa1ccceSKonstantin Belousov MPASS(moved == NULL || dstobject == NULL); 2178baa1ccceSKonstantin Belousov 2179baa1ccceSKonstantin Belousov mc = 0; 2180baa1ccceSKonstantin Belousov m = NULL; 2181cb6757c0SMark Johnston if (count == 0 || pctrie_is_empty(&srcobject->un_pager.swp.swp_blks)) 2182baa1ccceSKonstantin Belousov goto out; 2183467057fcSDoug Moore 2184a880104aSDoug Moore swp_pager_init_freerange(&range); 2185467057fcSDoug Moore offset = pindex; 2186467057fcSDoug Moore last = pindex + count; 2187467057fcSDoug Moore for (;;) { 2188467057fcSDoug Moore sb = SWAP_PCTRIE_LOOKUP_GE(&srcobject->un_pager.swp.swp_blks, 2189467057fcSDoug Moore rounddown(pindex, SWAP_META_PAGES)); 2190467057fcSDoug Moore if (sb == NULL || sb->p >= last) 2191467057fcSDoug Moore break; 2192467057fcSDoug Moore start = pindex > sb->p ? pindex - sb->p : 0; 2193467057fcSDoug Moore limit = last - sb->p < SWAP_META_PAGES ? last - sb->p : 2194467057fcSDoug Moore SWAP_META_PAGES; 2195467057fcSDoug Moore for (i = start; i < limit; i++) { 2196467057fcSDoug Moore if (sb->d[i] == SWAPBLK_NONE) 2197467057fcSDoug Moore continue; 2198467057fcSDoug Moore if (dstobject == NULL || 2199467057fcSDoug Moore !swp_pager_xfer_source(srcobject, dstobject, 2200467057fcSDoug Moore sb->p + i - offset, sb->d[i])) { 2201a880104aSDoug Moore swp_pager_update_freerange(&range, sb->d[i]); 2202467057fcSDoug Moore } 2203baa1ccceSKonstantin Belousov if (moved != NULL) { 2204baa1ccceSKonstantin Belousov if (m != NULL && m->pindex != pindex + i - 1) 2205baa1ccceSKonstantin Belousov m = NULL; 2206baa1ccceSKonstantin Belousov m = m != NULL ? vm_page_next(m) : 2207baa1ccceSKonstantin Belousov vm_page_lookup(srcobject, pindex + i); 2208baa1ccceSKonstantin Belousov if (m == NULL || vm_page_none_valid(m)) 2209baa1ccceSKonstantin Belousov mc++; 2210baa1ccceSKonstantin Belousov } 2211467057fcSDoug Moore sb->d[i] = SWAPBLK_NONE; 2212467057fcSDoug Moore } 2213467057fcSDoug Moore pindex = sb->p + SWAP_META_PAGES; 2214467057fcSDoug Moore if (swp_pager_swblk_empty(sb, 0, start) && 2215467057fcSDoug Moore swp_pager_swblk_empty(sb, limit, SWAP_META_PAGES)) { 2216467057fcSDoug Moore SWAP_PCTRIE_REMOVE(&srcobject->un_pager.swp.swp_blks, 2217467057fcSDoug Moore sb->p); 2218467057fcSDoug Moore uma_zfree(swblk_zone, sb); 2219467057fcSDoug Moore } 2220467057fcSDoug Moore } 2221a880104aSDoug Moore swp_pager_freeswapspace(&range); 2222baa1ccceSKonstantin Belousov out: 2223baa1ccceSKonstantin Belousov if (moved != NULL) 2224baa1ccceSKonstantin Belousov *moved = mc; 2225467057fcSDoug Moore } 2226467057fcSDoug Moore 2227467057fcSDoug Moore /* 22281c7c3c6aSMatthew Dillon * SWP_PAGER_META_FREE() - free a range of blocks in the object's swap metadata 22291c7c3c6aSMatthew Dillon * 22301c7c3c6aSMatthew Dillon * The requested range of blocks is freed, with any associated swap 22311c7c3c6aSMatthew Dillon * returned to the swap bitmap. 22321c7c3c6aSMatthew Dillon * 22331c7c3c6aSMatthew Dillon * This routine will free swap metadata structures as they are cleaned 22341c7c3c6aSMatthew Dillon * out. This routine does *NOT* operate on swap metadata associated 22351c7c3c6aSMatthew Dillon * with resident pages. 22361c7c3c6aSMatthew Dillon */ 22371c7c3c6aSMatthew Dillon static void 2238baa1ccceSKonstantin Belousov swp_pager_meta_free(vm_object_t object, vm_pindex_t pindex, vm_pindex_t count, 2239baa1ccceSKonstantin Belousov vm_size_t *freed) 22401c7c3c6aSMatthew Dillon { 2241baa1ccceSKonstantin Belousov swp_pager_meta_transfer(object, NULL, pindex, count, freed); 22421c7c3c6aSMatthew Dillon } 22431c7c3c6aSMatthew Dillon 2244a880104aSDoug Moore static void 22452a21cfe6SDoug Moore swp_pager_meta_free_block(struct swblk *sb, void *rangev) 2246a880104aSDoug Moore { 2247d2acf0a4SDoug Moore struct page_range *range = rangev; 2248d2acf0a4SDoug Moore 2249a880104aSDoug Moore for (int i = 0; i < SWAP_META_PAGES; i++) { 2250a880104aSDoug Moore if (sb->d[i] != SWAPBLK_NONE) 2251a880104aSDoug Moore swp_pager_update_freerange(range, sb->d[i]); 2252a880104aSDoug Moore } 2253a880104aSDoug Moore uma_zfree(swblk_zone, sb); 2254a880104aSDoug Moore } 2255a880104aSDoug Moore 22561c7c3c6aSMatthew Dillon /* 22571c7c3c6aSMatthew Dillon * SWP_PAGER_META_FREE_ALL() - destroy all swap metadata associated with object 22581c7c3c6aSMatthew Dillon * 22591c7c3c6aSMatthew Dillon * This routine locates and destroys all swap metadata associated with 22601c7c3c6aSMatthew Dillon * an object. 22611c7c3c6aSMatthew Dillon */ 22621c7c3c6aSMatthew Dillon static void 22631c7c3c6aSMatthew Dillon swp_pager_meta_free_all(vm_object_t object) 22641c7c3c6aSMatthew Dillon { 2265a880104aSDoug Moore struct page_range range; 22661c7c3c6aSMatthew Dillon 226789f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 2268cb6757c0SMark Johnston 2269a880104aSDoug Moore swp_pager_init_freerange(&range); 2270d2acf0a4SDoug Moore SWAP_PCTRIE_RECLAIM_CALLBACK(&object->un_pager.swp.swp_blks, 2271d2acf0a4SDoug Moore swp_pager_meta_free_block, &range); 2272a880104aSDoug Moore swp_pager_freeswapspace(&range); 22731c7c3c6aSMatthew Dillon } 22741c7c3c6aSMatthew Dillon 22751c7c3c6aSMatthew Dillon /* 22764abca9bbSAlan Cox * SWP_PAGER_METACTL() - misc control of swap meta data. 22771c7c3c6aSMatthew Dillon * 22784abca9bbSAlan Cox * This routine is capable of looking up, or removing swapblk 22794abca9bbSAlan Cox * assignments in the swap meta data. It returns the swapblk being 22804abca9bbSAlan Cox * looked-up, popped, or SWAPBLK_NONE if the block was invalid. 22811c7c3c6aSMatthew Dillon * 22821c7c3c6aSMatthew Dillon * When acting on a busy resident page and paging is in progress, we 22831c7c3c6aSMatthew Dillon * have to wait until paging is complete but otherwise can act on the 22841c7c3c6aSMatthew Dillon * busy page. 22851c7c3c6aSMatthew Dillon */ 22861c7c3c6aSMatthew Dillon static daddr_t 22878ecbf14bSDoug Moore swp_pager_meta_lookup(vm_object_t object, vm_pindex_t pindex) 22882f249180SPoul-Henning Kamp { 2289f425ab8eSKonstantin Belousov struct swblk *sb; 22904dcc5c2dSMatthew Dillon 2291c25673ffSAttilio Rao VM_OBJECT_ASSERT_LOCKED(object); 2292ee620ea4SAlan Cox 22931c7c3c6aSMatthew Dillon /* 2294ee620ea4SAlan Cox * The meta data only exists if the object is OBJT_SWAP 22951c7c3c6aSMatthew Dillon * and even then might not be allocated yet. 22961c7c3c6aSMatthew Dillon */ 22974b8365d7SKonstantin Belousov KASSERT((object->flags & OBJ_SWAP) != 0, 22988ecbf14bSDoug Moore ("Lookup object not swappable")); 22991c7c3c6aSMatthew Dillon 2300f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, 2301f425ab8eSKonstantin Belousov rounddown(pindex, SWAP_META_PAGES)); 2302f425ab8eSKonstantin Belousov if (sb == NULL) 2303f425ab8eSKonstantin Belousov return (SWAPBLK_NONE); 23048ecbf14bSDoug Moore return (sb->d[pindex % SWAP_META_PAGES]); 23051c7c3c6aSMatthew Dillon } 23061c7c3c6aSMatthew Dillon 2307e9c0cc15SPoul-Henning Kamp /* 230877d6fd97SKonstantin Belousov * Returns the least page index which is greater than or equal to the 230977d6fd97SKonstantin Belousov * parameter pindex and for which there is a swap block allocated. 231077d6fd97SKonstantin Belousov * Returns object's size if the object's type is not swap or if there 231177d6fd97SKonstantin Belousov * are no allocated swap blocks for the object after the requested 231277d6fd97SKonstantin Belousov * pindex. 231377d6fd97SKonstantin Belousov */ 231477d6fd97SKonstantin Belousov vm_pindex_t 231577d6fd97SKonstantin Belousov swap_pager_find_least(vm_object_t object, vm_pindex_t pindex) 231677d6fd97SKonstantin Belousov { 2317f425ab8eSKonstantin Belousov struct swblk *sb; 2318f425ab8eSKonstantin Belousov int i; 231977d6fd97SKonstantin Belousov 232077d6fd97SKonstantin Belousov VM_OBJECT_ASSERT_LOCKED(object); 23215bd45b2bSKonstantin Belousov MPASS((object->flags & OBJ_SWAP) != 0); 232277d6fd97SKonstantin Belousov 232328af3eb6SDoug Moore if (pctrie_is_empty(&object->un_pager.swp.swp_blks)) 232428af3eb6SDoug Moore return (object->size); 2325f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks, 2326f425ab8eSKonstantin Belousov rounddown(pindex, SWAP_META_PAGES)); 2327f425ab8eSKonstantin Belousov if (sb == NULL) 2328f425ab8eSKonstantin Belousov return (object->size); 232928af3eb6SDoug Moore if (sb->p < pindex) { 233028af3eb6SDoug Moore for (i = pindex % SWAP_META_PAGES; i < SWAP_META_PAGES; i++) { 2331f425ab8eSKonstantin Belousov if (sb->d[i] != SWAPBLK_NONE) 2332f425ab8eSKonstantin Belousov return (sb->p + i); 233377d6fd97SKonstantin Belousov } 233428af3eb6SDoug Moore sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks, 233528af3eb6SDoug Moore roundup(pindex, SWAP_META_PAGES)); 2336f425ab8eSKonstantin Belousov if (sb == NULL) 2337f425ab8eSKonstantin Belousov return (object->size); 233828af3eb6SDoug Moore } 2339f425ab8eSKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) { 2340f425ab8eSKonstantin Belousov if (sb->d[i] != SWAPBLK_NONE) 2341f425ab8eSKonstantin Belousov return (sb->p + i); 234277d6fd97SKonstantin Belousov } 2343f425ab8eSKonstantin Belousov 2344f425ab8eSKonstantin Belousov /* 2345f425ab8eSKonstantin Belousov * We get here if a swblk is present in the trie but it 2346f425ab8eSKonstantin Belousov * doesn't map any blocks. 2347f425ab8eSKonstantin Belousov */ 234828af3eb6SDoug Moore MPASS(0); 2349f425ab8eSKonstantin Belousov return (object->size); 235077d6fd97SKonstantin Belousov } 235177d6fd97SKonstantin Belousov 235277d6fd97SKonstantin Belousov /* 2353e9c0cc15SPoul-Henning Kamp * System call swapon(name) enables swapping on device name, 2354e9c0cc15SPoul-Henning Kamp * which must be in the swdevsw. Return EBUSY 2355e9c0cc15SPoul-Henning Kamp * if already swapping on this device. 2356e9c0cc15SPoul-Henning Kamp */ 2357e9c0cc15SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_ 2358e9c0cc15SPoul-Henning Kamp struct swapon_args { 2359e9c0cc15SPoul-Henning Kamp char *name; 2360e9c0cc15SPoul-Henning Kamp }; 2361e9c0cc15SPoul-Henning Kamp #endif 2362e9c0cc15SPoul-Henning Kamp 2363e9c0cc15SPoul-Henning Kamp int 23648451d0ddSKip Macy sys_swapon(struct thread *td, struct swapon_args *uap) 2365e9c0cc15SPoul-Henning Kamp { 2366e9c0cc15SPoul-Henning Kamp struct vattr attr; 2367e9c0cc15SPoul-Henning Kamp struct vnode *vp; 2368e9c0cc15SPoul-Henning Kamp struct nameidata nd; 2369e9c0cc15SPoul-Henning Kamp int error; 2370e9c0cc15SPoul-Henning Kamp 2371acd3428bSRobert Watson error = priv_check(td, PRIV_SWAPON); 2372e9c0cc15SPoul-Henning Kamp if (error) 2373acd3428bSRobert Watson return (error); 2374e9c0cc15SPoul-Henning Kamp 237504533e1eSKonstantin Belousov sx_xlock(&swdev_syscall_lock); 2376e9c0cc15SPoul-Henning Kamp 2377e9c0cc15SPoul-Henning Kamp /* 2378e9c0cc15SPoul-Henning Kamp * Swap metadata may not fit in the KVM if we have physical 2379e9c0cc15SPoul-Henning Kamp * memory of >1GB. 2380e9c0cc15SPoul-Henning Kamp */ 2381f425ab8eSKonstantin Belousov if (swblk_zone == NULL) { 2382e9c0cc15SPoul-Henning Kamp error = ENOMEM; 2383e9c0cc15SPoul-Henning Kamp goto done; 2384e9c0cc15SPoul-Henning Kamp } 2385e9c0cc15SPoul-Henning Kamp 23866ddf41faSKonstantin Belousov NDINIT(&nd, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | AUDITVNODE1, 23877e1d3eefSMateusz Guzik UIO_USERSPACE, uap->name); 2388e9c0cc15SPoul-Henning Kamp error = namei(&nd); 2389e9c0cc15SPoul-Henning Kamp if (error) 2390e9c0cc15SPoul-Henning Kamp goto done; 2391e9c0cc15SPoul-Henning Kamp 2392bb92cd7bSMateusz Guzik NDFREE_PNBUF(&nd); 2393e9c0cc15SPoul-Henning Kamp vp = nd.ni_vp; 2394e9c0cc15SPoul-Henning Kamp 23957ad2a82dSMateusz Guzik if (vn_isdisk_error(vp, &error)) { 239688ad2d7bSKonstantin Belousov error = swapongeom(vp); 239720da9c2eSPoul-Henning Kamp } else if (vp->v_type == VREG && 2398e9c0cc15SPoul-Henning Kamp (vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 && 23990359a12eSAttilio Rao (error = VOP_GETATTR(vp, &attr, td->td_ucred)) == 0) { 2400e9c0cc15SPoul-Henning Kamp /* 2401e9c0cc15SPoul-Henning Kamp * Allow direct swapping to NFS regular files in the same 2402e9c0cc15SPoul-Henning Kamp * way that nfs_mountroot() sets up diskless swapping. 2403e9c0cc15SPoul-Henning Kamp */ 240459efee01SPoul-Henning Kamp error = swaponvp(td, vp, attr.va_size / DEV_BSIZE); 2405e9c0cc15SPoul-Henning Kamp } 2406e9c0cc15SPoul-Henning Kamp 24076ddf41faSKonstantin Belousov if (error != 0) 24086ddf41faSKonstantin Belousov vput(vp); 24096ddf41faSKonstantin Belousov else 24106ddf41faSKonstantin Belousov VOP_UNLOCK(vp); 2411e9c0cc15SPoul-Henning Kamp done: 241204533e1eSKonstantin Belousov sx_xunlock(&swdev_syscall_lock); 2413e9c0cc15SPoul-Henning Kamp return (error); 2414e9c0cc15SPoul-Henning Kamp } 2415e9c0cc15SPoul-Henning Kamp 24163ff863f1SDag-Erling Smørgrav /* 24173ff863f1SDag-Erling Smørgrav * Check that the total amount of swap currently configured does not 24183ff863f1SDag-Erling Smørgrav * exceed half the theoretical maximum. If it does, print a warning 241935872e79SKonstantin Belousov * message. 24203ff863f1SDag-Erling Smørgrav */ 242135872e79SKonstantin Belousov static void 242235872e79SKonstantin Belousov swapon_check_swzone(void) 24233ff863f1SDag-Erling Smørgrav { 24243ff863f1SDag-Erling Smørgrav 24253ff863f1SDag-Erling Smørgrav /* recommend using no more than half that amount */ 2426303fa05aSKonstantin Belousov if (swap_total > swap_maxpages / 2) { 24273ff863f1SDag-Erling Smørgrav printf("warning: total configured swap (%lu pages) " 24283ff863f1SDag-Erling Smørgrav "exceeds maximum recommended amount (%lu pages).\n", 2429303fa05aSKonstantin Belousov swap_total, swap_maxpages / 2); 24303ff863f1SDag-Erling Smørgrav printf("warning: increase kern.maxswzone " 24313ff863f1SDag-Erling Smørgrav "or reduce amount of swap.\n"); 24323ff863f1SDag-Erling Smørgrav } 24333ff863f1SDag-Erling Smørgrav } 24343ff863f1SDag-Erling Smørgrav 243559efee01SPoul-Henning Kamp static void 24362cc718a1SKonstantin Belousov swaponsomething(struct vnode *vp, void *id, u_long nblks, 24372cc718a1SKonstantin Belousov sw_strategy_t *strategy, sw_close_t *close, dev_t dev, int flags) 2438e9c0cc15SPoul-Henning Kamp { 24392d9974c1SAlan Cox struct swdevt *sp, *tsp; 244034e2051fSMark Johnston daddr_t dvbase; 2441e9c0cc15SPoul-Henning Kamp 2442e9c0cc15SPoul-Henning Kamp /* 2443e9c0cc15SPoul-Henning Kamp * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks. 2444e9c0cc15SPoul-Henning Kamp * First chop nblks off to page-align it, then convert. 2445e9c0cc15SPoul-Henning Kamp * 2446e9c0cc15SPoul-Henning Kamp * sw->sw_nblks is in page-sized chunks now too. 2447e9c0cc15SPoul-Henning Kamp */ 2448e9c0cc15SPoul-Henning Kamp nblks &= ~(ctodb(1) - 1); 2449e9c0cc15SPoul-Henning Kamp nblks = dbtoc(nblks); 2450e9c0cc15SPoul-Henning Kamp 24518f60c087SPoul-Henning Kamp sp = malloc(sizeof *sp, M_VMPGDATA, M_WAITOK | M_ZERO); 245200fd73d2SDoug Moore sp->sw_blist = blist_create(nblks, M_WAITOK); 2453dee34ca4SPoul-Henning Kamp sp->sw_vp = vp; 2454dee34ca4SPoul-Henning Kamp sp->sw_id = id; 2455f3732fd1SPoul-Henning Kamp sp->sw_dev = dev; 2456e9c0cc15SPoul-Henning Kamp sp->sw_nblks = nblks; 2457e9c0cc15SPoul-Henning Kamp sp->sw_used = 0; 245859efee01SPoul-Henning Kamp sp->sw_strategy = strategy; 2459dee34ca4SPoul-Henning Kamp sp->sw_close = close; 24602cc718a1SKonstantin Belousov sp->sw_flags = flags; 2461e9c0cc15SPoul-Henning Kamp 2462e9c0cc15SPoul-Henning Kamp /* 2463504f5e29SDoug Moore * Do not free the first blocks in order to avoid overwriting 24648f60c087SPoul-Henning Kamp * any bsd label at the front of the partition 2465e9c0cc15SPoul-Henning Kamp */ 2466504f5e29SDoug Moore blist_free(sp->sw_blist, howmany(BBSIZE, PAGE_SIZE), 2467504f5e29SDoug Moore nblks - howmany(BBSIZE, PAGE_SIZE)); 2468e9c0cc15SPoul-Henning Kamp 24692d9974c1SAlan Cox dvbase = 0; 247020da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 24712d9974c1SAlan Cox TAILQ_FOREACH(tsp, &swtailq, sw_list) { 24722d9974c1SAlan Cox if (tsp->sw_end >= dvbase) { 24732d9974c1SAlan Cox /* 24742d9974c1SAlan Cox * We put one uncovered page between the devices 24752d9974c1SAlan Cox * in order to definitively prevent any cross-device 24762d9974c1SAlan Cox * I/O requests 24772d9974c1SAlan Cox */ 24782d9974c1SAlan Cox dvbase = tsp->sw_end + 1; 24792d9974c1SAlan Cox } 24802d9974c1SAlan Cox } 24812d9974c1SAlan Cox sp->sw_first = dvbase; 24822d9974c1SAlan Cox sp->sw_end = dvbase + nblks; 24838f60c087SPoul-Henning Kamp TAILQ_INSERT_TAIL(&swtailq, sp, sw_list); 24848f60c087SPoul-Henning Kamp nswapdev++; 2485504f5e29SDoug Moore swap_pager_avail += nblks - howmany(BBSIZE, PAGE_SIZE); 2486e8bb589dSMatt Macy swap_total += nblks; 248735872e79SKonstantin Belousov swapon_check_swzone(); 2488d05bc129SAlan Cox swp_sizecheck(); 2489d05bc129SAlan Cox mtx_unlock(&sw_dev_mtx); 2490b1fd102eSMark Johnston EVENTHANDLER_INVOKE(swapon, sp); 249159efee01SPoul-Henning Kamp } 2492e9c0cc15SPoul-Henning Kamp 2493e9c0cc15SPoul-Henning Kamp /* 2494e9c0cc15SPoul-Henning Kamp * SYSCALL: swapoff(devname) 2495e9c0cc15SPoul-Henning Kamp * 2496e9c0cc15SPoul-Henning Kamp * Disable swapping on the given device. 2497dee34ca4SPoul-Henning Kamp * 2498dee34ca4SPoul-Henning Kamp * XXX: Badly designed system call: it should use a device index 2499dee34ca4SPoul-Henning Kamp * rather than filename as specification. We keep sw_vp around 2500dee34ca4SPoul-Henning Kamp * only to make this work. 2501e9c0cc15SPoul-Henning Kamp */ 250253465702SKonstantin Belousov static int 250353465702SKonstantin Belousov kern_swapoff(struct thread *td, const char *name, enum uio_seg name_seg, 250453465702SKonstantin Belousov u_int flags) 2505e9c0cc15SPoul-Henning Kamp { 2506e9c0cc15SPoul-Henning Kamp struct vnode *vp; 2507e9c0cc15SPoul-Henning Kamp struct nameidata nd; 2508e9c0cc15SPoul-Henning Kamp struct swdevt *sp; 250953465702SKonstantin Belousov int error; 2510e9c0cc15SPoul-Henning Kamp 2511acd3428bSRobert Watson error = priv_check(td, PRIV_SWAPOFF); 2512a4e4132fSKonstantin Belousov if (error != 0) 2513a4e4132fSKonstantin Belousov return (error); 251453465702SKonstantin Belousov if ((flags & ~(SWAPOFF_FORCE)) != 0) 2515a4e4132fSKonstantin Belousov return (EINVAL); 2516a4e4132fSKonstantin Belousov 251704533e1eSKonstantin Belousov sx_xlock(&swdev_syscall_lock); 2518e9c0cc15SPoul-Henning Kamp 251953465702SKonstantin Belousov NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, name_seg, name); 2520e9c0cc15SPoul-Henning Kamp error = namei(&nd); 2521e9c0cc15SPoul-Henning Kamp if (error) 2522e9c0cc15SPoul-Henning Kamp goto done; 2523bb92cd7bSMateusz Guzik NDFREE_PNBUF(&nd); 2524e9c0cc15SPoul-Henning Kamp vp = nd.ni_vp; 2525e9c0cc15SPoul-Henning Kamp 252620da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 25278f60c087SPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 2528dee34ca4SPoul-Henning Kamp if (sp->sw_vp == vp) 25290909f38aSPawel Jakub Dawidek break; 2530e9c0cc15SPoul-Henning Kamp } 253120da9c2eSPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 25320909f38aSPawel Jakub Dawidek if (sp == NULL) { 2533e9c0cc15SPoul-Henning Kamp error = EINVAL; 2534e9c0cc15SPoul-Henning Kamp goto done; 25350909f38aSPawel Jakub Dawidek } 253653465702SKonstantin Belousov error = swapoff_one(sp, td->td_ucred, flags); 25370909f38aSPawel Jakub Dawidek done: 253804533e1eSKonstantin Belousov sx_xunlock(&swdev_syscall_lock); 25390909f38aSPawel Jakub Dawidek return (error); 25400909f38aSPawel Jakub Dawidek } 25410909f38aSPawel Jakub Dawidek 254253465702SKonstantin Belousov 254353465702SKonstantin Belousov #ifdef COMPAT_FREEBSD13 254453465702SKonstantin Belousov int 254553465702SKonstantin Belousov freebsd13_swapoff(struct thread *td, struct freebsd13_swapoff_args *uap) 254653465702SKonstantin Belousov { 254753465702SKonstantin Belousov return (kern_swapoff(td, uap->name, UIO_USERSPACE, 0)); 254853465702SKonstantin Belousov } 254953465702SKonstantin Belousov #endif 255053465702SKonstantin Belousov 255153465702SKonstantin Belousov int 255253465702SKonstantin Belousov sys_swapoff(struct thread *td, struct swapoff_args *uap) 255353465702SKonstantin Belousov { 255453465702SKonstantin Belousov return (kern_swapoff(td, uap->name, UIO_USERSPACE, uap->flags)); 255553465702SKonstantin Belousov } 255653465702SKonstantin Belousov 25570909f38aSPawel Jakub Dawidek static int 2558e8dc2ba2SKonstantin Belousov swapoff_one(struct swdevt *sp, struct ucred *cred, u_int flags) 25590909f38aSPawel Jakub Dawidek { 256003bdd65fSAlan Cox u_long nblks; 2561e9c0cc15SPoul-Henning Kamp #ifdef MAC 25620909f38aSPawel Jakub Dawidek int error; 2563e9c0cc15SPoul-Henning Kamp #endif 2564e9c0cc15SPoul-Henning Kamp 256504533e1eSKonstantin Belousov sx_assert(&swdev_syscall_lock, SA_XLOCKED); 25660909f38aSPawel Jakub Dawidek #ifdef MAC 2567cb05b60aSAttilio Rao (void) vn_lock(sp->sw_vp, LK_EXCLUSIVE | LK_RETRY); 256835918c55SChristian S.J. Peron error = mac_system_check_swapoff(cred, sp->sw_vp); 2569b249ce48SMateusz Guzik (void) VOP_UNLOCK(sp->sw_vp); 25700909f38aSPawel Jakub Dawidek if (error != 0) 25710909f38aSPawel Jakub Dawidek return (error); 25720909f38aSPawel Jakub Dawidek #endif 2573e9c0cc15SPoul-Henning Kamp nblks = sp->sw_nblks; 2574e9c0cc15SPoul-Henning Kamp 2575e9c0cc15SPoul-Henning Kamp /* 2576e9c0cc15SPoul-Henning Kamp * We can turn off this swap device safely only if the 2577e9c0cc15SPoul-Henning Kamp * available virtual memory in the system will fit the amount 2578e9c0cc15SPoul-Henning Kamp * of data we will have to page back in, plus an epsilon so 2579e9c0cc15SPoul-Henning Kamp * the system doesn't become critically low on swap space. 25800190c38bSKonstantin Belousov * The vm_free_count() part does not account e.g. for clean 25810190c38bSKonstantin Belousov * pages that can be immediately reclaimed without paging, so 25820190c38bSKonstantin Belousov * this is a very rough estimation. 25830190c38bSKonstantin Belousov * 25840190c38bSKonstantin Belousov * On the other hand, not turning swap off on swapoff_all() 25850190c38bSKonstantin Belousov * means that we can lose swap data when filesystems go away, 25860190c38bSKonstantin Belousov * which is arguably worse. 2587e9c0cc15SPoul-Henning Kamp */ 2588e8dc2ba2SKonstantin Belousov if ((flags & SWAPOFF_FORCE) == 0 && 25890190c38bSKonstantin Belousov vm_free_count() + swap_pager_avail < nblks + nswap_lowat) 25900909f38aSPawel Jakub Dawidek return (ENOMEM); 2591e9c0cc15SPoul-Henning Kamp 2592e9c0cc15SPoul-Henning Kamp /* 2593e9c0cc15SPoul-Henning Kamp * Prevent further allocations on this device. 2594e9c0cc15SPoul-Henning Kamp */ 25952928cef7SAlan Cox mtx_lock(&sw_dev_mtx); 2596e9c0cc15SPoul-Henning Kamp sp->sw_flags |= SW_CLOSING; 259703bdd65fSAlan Cox swap_pager_avail -= blist_fill(sp->sw_blist, 0, nblks); 2598e8bb589dSMatt Macy swap_total -= nblks; 25992928cef7SAlan Cox mtx_unlock(&sw_dev_mtx); 2600e9c0cc15SPoul-Henning Kamp 2601e9c0cc15SPoul-Henning Kamp /* 2602e9c0cc15SPoul-Henning Kamp * Page in the contents of the device and close it. 2603e9c0cc15SPoul-Henning Kamp */ 2604b3fed13eSDavid Schultz swap_pager_swapoff(sp); 2605e9c0cc15SPoul-Henning Kamp 260635918c55SChristian S.J. Peron sp->sw_close(curthread, sp); 260720da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 26089e3e3fe5SWarner Losh sp->sw_id = NULL; 26098f60c087SPoul-Henning Kamp TAILQ_REMOVE(&swtailq, sp, sw_list); 26100676a140SAlan Cox nswapdev--; 26117dea2c2eSAlan Cox if (nswapdev == 0) { 26127dea2c2eSAlan Cox swap_pager_full = 2; 26137dea2c2eSAlan Cox swap_pager_almost_full = 1; 26147dea2c2eSAlan Cox } 26158f60c087SPoul-Henning Kamp if (swdevhd == sp) 26168f60c087SPoul-Henning Kamp swdevhd = NULL; 2617d05bc129SAlan Cox mtx_unlock(&sw_dev_mtx); 26188f60c087SPoul-Henning Kamp blist_destroy(sp->sw_blist); 26198f60c087SPoul-Henning Kamp free(sp, M_VMPGDATA); 26200909f38aSPawel Jakub Dawidek return (0); 26210909f38aSPawel Jakub Dawidek } 2622e9c0cc15SPoul-Henning Kamp 26230909f38aSPawel Jakub Dawidek void 26240909f38aSPawel Jakub Dawidek swapoff_all(void) 26250909f38aSPawel Jakub Dawidek { 26260909f38aSPawel Jakub Dawidek struct swdevt *sp, *spt; 26270909f38aSPawel Jakub Dawidek const char *devname; 26280909f38aSPawel Jakub Dawidek int error; 26290909f38aSPawel Jakub Dawidek 263004533e1eSKonstantin Belousov sx_xlock(&swdev_syscall_lock); 26310909f38aSPawel Jakub Dawidek 26320909f38aSPawel Jakub Dawidek mtx_lock(&sw_dev_mtx); 26330909f38aSPawel Jakub Dawidek TAILQ_FOREACH_SAFE(sp, &swtailq, sw_list, spt) { 26340909f38aSPawel Jakub Dawidek mtx_unlock(&sw_dev_mtx); 26357ad2a82dSMateusz Guzik if (vn_isdisk(sp->sw_vp)) 26367870adb6SEd Schouten devname = devtoname(sp->sw_vp->v_rdev); 26370909f38aSPawel Jakub Dawidek else 26380909f38aSPawel Jakub Dawidek devname = "[file]"; 2639e8dc2ba2SKonstantin Belousov error = swapoff_one(sp, thread0.td_ucred, SWAPOFF_FORCE); 26400909f38aSPawel Jakub Dawidek if (error != 0) { 26410909f38aSPawel Jakub Dawidek printf("Cannot remove swap device %s (error=%d), " 26420909f38aSPawel Jakub Dawidek "skipping.\n", devname, error); 26430909f38aSPawel Jakub Dawidek } else if (bootverbose) { 26440909f38aSPawel Jakub Dawidek printf("Swap device %s removed.\n", devname); 26450909f38aSPawel Jakub Dawidek } 26460909f38aSPawel Jakub Dawidek mtx_lock(&sw_dev_mtx); 26470909f38aSPawel Jakub Dawidek } 26480909f38aSPawel Jakub Dawidek mtx_unlock(&sw_dev_mtx); 26490909f38aSPawel Jakub Dawidek 265004533e1eSKonstantin Belousov sx_xunlock(&swdev_syscall_lock); 2651e9c0cc15SPoul-Henning Kamp } 2652e9c0cc15SPoul-Henning Kamp 2653567104a1SPoul-Henning Kamp void 2654567104a1SPoul-Henning Kamp swap_pager_status(int *total, int *used) 2655567104a1SPoul-Henning Kamp { 2656567104a1SPoul-Henning Kamp 26577ce3a312SMateusz Guzik *total = swap_total; 26587ce3a312SMateusz Guzik *used = swap_total - swap_pager_avail - 26597ce3a312SMateusz Guzik nswapdev * howmany(BBSIZE, PAGE_SIZE); 2660567104a1SPoul-Henning Kamp } 2661567104a1SPoul-Henning Kamp 2662dda4f960SKonstantin Belousov int 2663dda4f960SKonstantin Belousov swap_dev_info(int name, struct xswdev *xs, char *devname, size_t len) 2664dda4f960SKonstantin Belousov { 2665dda4f960SKonstantin Belousov struct swdevt *sp; 26667870adb6SEd Schouten const char *tmp_devname; 2667dda4f960SKonstantin Belousov int error, n; 2668dda4f960SKonstantin Belousov 2669dda4f960SKonstantin Belousov n = 0; 2670dda4f960SKonstantin Belousov error = ENOENT; 2671dda4f960SKonstantin Belousov mtx_lock(&sw_dev_mtx); 2672dda4f960SKonstantin Belousov TAILQ_FOREACH(sp, &swtailq, sw_list) { 2673dda4f960SKonstantin Belousov if (n != name) { 2674dda4f960SKonstantin Belousov n++; 2675dda4f960SKonstantin Belousov continue; 2676dda4f960SKonstantin Belousov } 2677dda4f960SKonstantin Belousov xs->xsw_version = XSWDEV_VERSION; 2678dda4f960SKonstantin Belousov xs->xsw_dev = sp->sw_dev; 2679dda4f960SKonstantin Belousov xs->xsw_flags = sp->sw_flags; 2680dda4f960SKonstantin Belousov xs->xsw_nblks = sp->sw_nblks; 2681dda4f960SKonstantin Belousov xs->xsw_used = sp->sw_used; 2682dda4f960SKonstantin Belousov if (devname != NULL) { 26837ad2a82dSMateusz Guzik if (vn_isdisk(sp->sw_vp)) 26847870adb6SEd Schouten tmp_devname = devtoname(sp->sw_vp->v_rdev); 2685dda4f960SKonstantin Belousov else 2686dda4f960SKonstantin Belousov tmp_devname = "[file]"; 2687dda4f960SKonstantin Belousov strncpy(devname, tmp_devname, len); 2688dda4f960SKonstantin Belousov } 2689dda4f960SKonstantin Belousov error = 0; 2690dda4f960SKonstantin Belousov break; 2691dda4f960SKonstantin Belousov } 2692dda4f960SKonstantin Belousov mtx_unlock(&sw_dev_mtx); 2693dda4f960SKonstantin Belousov return (error); 2694dda4f960SKonstantin Belousov } 2695dda4f960SKonstantin Belousov 269669921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11) 269769921123SKonstantin Belousov #define XSWDEV_VERSION_11 1 269869921123SKonstantin Belousov struct xswdev11 { 269969921123SKonstantin Belousov u_int xsw_version; 270069921123SKonstantin Belousov uint32_t xsw_dev; 270169921123SKonstantin Belousov int xsw_flags; 270269921123SKonstantin Belousov int xsw_nblks; 270369921123SKonstantin Belousov int xsw_used; 270469921123SKonstantin Belousov }; 270569921123SKonstantin Belousov #endif 270669921123SKonstantin Belousov 2707f6d281e8SKonstantin Belousov #if defined(__amd64__) && defined(COMPAT_FREEBSD32) 2708f6d281e8SKonstantin Belousov struct xswdev32 { 2709f6d281e8SKonstantin Belousov u_int xsw_version; 2710f6d281e8SKonstantin Belousov u_int xsw_dev1, xsw_dev2; 2711f6d281e8SKonstantin Belousov int xsw_flags; 2712f6d281e8SKonstantin Belousov int xsw_nblks; 2713f6d281e8SKonstantin Belousov int xsw_used; 2714f6d281e8SKonstantin Belousov }; 2715f6d281e8SKonstantin Belousov #endif 2716f6d281e8SKonstantin Belousov 2717e9c0cc15SPoul-Henning Kamp static int 2718e9c0cc15SPoul-Henning Kamp sysctl_vm_swap_info(SYSCTL_HANDLER_ARGS) 2719e9c0cc15SPoul-Henning Kamp { 2720e9c0cc15SPoul-Henning Kamp struct xswdev xs; 2721f6d281e8SKonstantin Belousov #if defined(__amd64__) && defined(COMPAT_FREEBSD32) 2722f6d281e8SKonstantin Belousov struct xswdev32 xs32; 2723f6d281e8SKonstantin Belousov #endif 272469921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11) 272569921123SKonstantin Belousov struct xswdev11 xs11; 272669921123SKonstantin Belousov #endif 2727dda4f960SKonstantin Belousov int error; 2728e9c0cc15SPoul-Henning Kamp 2729e9c0cc15SPoul-Henning Kamp if (arg2 != 1) /* name length */ 2730e9c0cc15SPoul-Henning Kamp return (EINVAL); 273106d1fd9fSMark Johnston 273206d1fd9fSMark Johnston memset(&xs, 0, sizeof(xs)); 2733dda4f960SKonstantin Belousov error = swap_dev_info(*(int *)arg1, &xs, NULL, 0); 2734dda4f960SKonstantin Belousov if (error != 0) 2735dda4f960SKonstantin Belousov return (error); 2736f6d281e8SKonstantin Belousov #if defined(__amd64__) && defined(COMPAT_FREEBSD32) 2737f6d281e8SKonstantin Belousov if (req->oldlen == sizeof(xs32)) { 273806d1fd9fSMark Johnston memset(&xs32, 0, sizeof(xs32)); 2739f6d281e8SKonstantin Belousov xs32.xsw_version = XSWDEV_VERSION; 2740f6d281e8SKonstantin Belousov xs32.xsw_dev1 = xs.xsw_dev; 2741f6d281e8SKonstantin Belousov xs32.xsw_dev2 = xs.xsw_dev >> 32; 2742f6d281e8SKonstantin Belousov xs32.xsw_flags = xs.xsw_flags; 2743f6d281e8SKonstantin Belousov xs32.xsw_nblks = xs.xsw_nblks; 2744f6d281e8SKonstantin Belousov xs32.xsw_used = xs.xsw_used; 2745f6d281e8SKonstantin Belousov error = SYSCTL_OUT(req, &xs32, sizeof(xs32)); 2746f6d281e8SKonstantin Belousov return (error); 2747f6d281e8SKonstantin Belousov } 2748f6d281e8SKonstantin Belousov #endif 274969921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11) 275069921123SKonstantin Belousov if (req->oldlen == sizeof(xs11)) { 275106d1fd9fSMark Johnston memset(&xs11, 0, sizeof(xs11)); 275269921123SKonstantin Belousov xs11.xsw_version = XSWDEV_VERSION_11; 275369921123SKonstantin Belousov xs11.xsw_dev = xs.xsw_dev; /* truncation */ 275469921123SKonstantin Belousov xs11.xsw_flags = xs.xsw_flags; 275569921123SKonstantin Belousov xs11.xsw_nblks = xs.xsw_nblks; 275669921123SKonstantin Belousov xs11.xsw_used = xs.xsw_used; 275769921123SKonstantin Belousov error = SYSCTL_OUT(req, &xs11, sizeof(xs11)); 2758f6d281e8SKonstantin Belousov return (error); 2759f6d281e8SKonstantin Belousov } 276069921123SKonstantin Belousov #endif 2761e9c0cc15SPoul-Henning Kamp error = SYSCTL_OUT(req, &xs, sizeof(xs)); 2762e9c0cc15SPoul-Henning Kamp return (error); 2763e9c0cc15SPoul-Henning Kamp } 2764e9c0cc15SPoul-Henning Kamp 27658f60c087SPoul-Henning Kamp SYSCTL_INT(_vm, OID_AUTO, nswapdev, CTLFLAG_RD, &nswapdev, 0, 2766e9c0cc15SPoul-Henning Kamp "Number of swap devices"); 27674c36e917SKonstantin Belousov SYSCTL_NODE(_vm, OID_AUTO, swap_info, CTLFLAG_RD | CTLFLAG_MPSAFE, 27684c36e917SKonstantin Belousov sysctl_vm_swap_info, 2769e9c0cc15SPoul-Henning Kamp "Swap statistics by device"); 2770ec38b344SPoul-Henning Kamp 2771ec38b344SPoul-Henning Kamp /* 2772f425ab8eSKonstantin Belousov * Count the approximate swap usage in pages for a vmspace. The 2773f425ab8eSKonstantin Belousov * shadowed or not yet copied on write swap blocks are not accounted. 2774ec38b344SPoul-Henning Kamp * The map must be locked. 2775ec38b344SPoul-Henning Kamp */ 27762860553aSRebecca Cran long 2777ec38b344SPoul-Henning Kamp vmspace_swap_count(struct vmspace *vmspace) 2778ec38b344SPoul-Henning Kamp { 277965d8409cSRebecca Cran vm_map_t map; 2780ec38b344SPoul-Henning Kamp vm_map_entry_t cur; 278165d8409cSRebecca Cran vm_object_t object; 2782f425ab8eSKonstantin Belousov struct swblk *sb; 2783f425ab8eSKonstantin Belousov vm_pindex_t e, pi; 2784f425ab8eSKonstantin Belousov long count; 2785f425ab8eSKonstantin Belousov int i; 278665d8409cSRebecca Cran 278765d8409cSRebecca Cran map = &vmspace->vm_map; 278865d8409cSRebecca Cran count = 0; 2789ec38b344SPoul-Henning Kamp 27902288078cSDoug Moore VM_MAP_ENTRY_FOREACH(cur, map) { 2791f425ab8eSKonstantin Belousov if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) != 0) 2792f425ab8eSKonstantin Belousov continue; 2793f425ab8eSKonstantin Belousov object = cur->object.vm_object; 27944b8365d7SKonstantin Belousov if (object == NULL || (object->flags & OBJ_SWAP) == 0) 2795f425ab8eSKonstantin Belousov continue; 2796f425ab8eSKonstantin Belousov VM_OBJECT_RLOCK(object); 27974b8365d7SKonstantin Belousov if ((object->flags & OBJ_SWAP) == 0) 2798f425ab8eSKonstantin Belousov goto unlock; 2799f425ab8eSKonstantin Belousov pi = OFF_TO_IDX(cur->offset); 2800f425ab8eSKonstantin Belousov e = pi + OFF_TO_IDX(cur->end - cur->start); 2801f425ab8eSKonstantin Belousov for (;; pi = sb->p + SWAP_META_PAGES) { 2802f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP_GE( 2803f425ab8eSKonstantin Belousov &object->un_pager.swp.swp_blks, pi); 2804f425ab8eSKonstantin Belousov if (sb == NULL || sb->p >= e) 2805f425ab8eSKonstantin Belousov break; 2806f425ab8eSKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) { 2807f425ab8eSKonstantin Belousov if (sb->p + i < e && 2808f425ab8eSKonstantin Belousov sb->d[i] != SWAPBLK_NONE) 2809f425ab8eSKonstantin Belousov count++; 2810ec38b344SPoul-Henning Kamp } 2811ec38b344SPoul-Henning Kamp } 2812f425ab8eSKonstantin Belousov unlock: 2813f425ab8eSKonstantin Belousov VM_OBJECT_RUNLOCK(object); 2814ec38b344SPoul-Henning Kamp } 2815ec38b344SPoul-Henning Kamp return (count); 2816ec38b344SPoul-Henning Kamp } 2817dee34ca4SPoul-Henning Kamp 2818dee34ca4SPoul-Henning Kamp /* 2819dee34ca4SPoul-Henning Kamp * GEOM backend 2820dee34ca4SPoul-Henning Kamp * 2821dee34ca4SPoul-Henning Kamp * Swapping onto disk devices. 2822dee34ca4SPoul-Henning Kamp * 2823dee34ca4SPoul-Henning Kamp */ 2824dee34ca4SPoul-Henning Kamp 28255721c9c7SPoul-Henning Kamp static g_orphan_t swapgeom_orphan; 28265721c9c7SPoul-Henning Kamp 2827dee34ca4SPoul-Henning Kamp static struct g_class g_swap_class = { 2828dee34ca4SPoul-Henning Kamp .name = "SWAP", 28295721c9c7SPoul-Henning Kamp .version = G_VERSION, 28305721c9c7SPoul-Henning Kamp .orphan = swapgeom_orphan, 2831dee34ca4SPoul-Henning Kamp }; 2832dee34ca4SPoul-Henning Kamp 2833dee34ca4SPoul-Henning Kamp DECLARE_GEOM_CLASS(g_swap_class, g_class); 2834dee34ca4SPoul-Henning Kamp 2835dee34ca4SPoul-Henning Kamp static void 28363398491bSAlexander Motin swapgeom_close_ev(void *arg, int flags) 28373398491bSAlexander Motin { 28383398491bSAlexander Motin struct g_consumer *cp; 28393398491bSAlexander Motin 28403398491bSAlexander Motin cp = arg; 28413398491bSAlexander Motin g_access(cp, -1, -1, 0); 28423398491bSAlexander Motin g_detach(cp); 28433398491bSAlexander Motin g_destroy_consumer(cp); 28443398491bSAlexander Motin } 28453398491bSAlexander Motin 28469e3e3fe5SWarner Losh /* 28479e3e3fe5SWarner Losh * Add a reference to the g_consumer for an inflight transaction. 28489e3e3fe5SWarner Losh */ 28499e3e3fe5SWarner Losh static void 28509e3e3fe5SWarner Losh swapgeom_acquire(struct g_consumer *cp) 28519e3e3fe5SWarner Losh { 28529e3e3fe5SWarner Losh 28539e3e3fe5SWarner Losh mtx_assert(&sw_dev_mtx, MA_OWNED); 28549e3e3fe5SWarner Losh cp->index++; 28559e3e3fe5SWarner Losh } 28569e3e3fe5SWarner Losh 28579e3e3fe5SWarner Losh /* 28580c657d22SKonstantin Belousov * Remove a reference from the g_consumer. Post a close event if all 28590c657d22SKonstantin Belousov * references go away, since the function might be called from the 28600c657d22SKonstantin Belousov * biodone context. 28619e3e3fe5SWarner Losh */ 28629e3e3fe5SWarner Losh static void 28639e3e3fe5SWarner Losh swapgeom_release(struct g_consumer *cp, struct swdevt *sp) 28649e3e3fe5SWarner Losh { 28659e3e3fe5SWarner Losh 28669e3e3fe5SWarner Losh mtx_assert(&sw_dev_mtx, MA_OWNED); 28679e3e3fe5SWarner Losh cp->index--; 28689e3e3fe5SWarner Losh if (cp->index == 0) { 28699e3e3fe5SWarner Losh if (g_post_event(swapgeom_close_ev, cp, M_NOWAIT, NULL) == 0) 28709e3e3fe5SWarner Losh sp->sw_id = NULL; 28719e3e3fe5SWarner Losh } 28729e3e3fe5SWarner Losh } 28739e3e3fe5SWarner Losh 28743398491bSAlexander Motin static void 2875dee34ca4SPoul-Henning Kamp swapgeom_done(struct bio *bp2) 2876dee34ca4SPoul-Henning Kamp { 28773398491bSAlexander Motin struct swdevt *sp; 2878dee34ca4SPoul-Henning Kamp struct buf *bp; 28793398491bSAlexander Motin struct g_consumer *cp; 2880dee34ca4SPoul-Henning Kamp 2881dee34ca4SPoul-Henning Kamp bp = bp2->bio_caller2; 28823398491bSAlexander Motin cp = bp2->bio_from; 2883c5d3d25eSPoul-Henning Kamp bp->b_ioflags = bp2->bio_flags; 2884dee34ca4SPoul-Henning Kamp if (bp2->bio_error) 2885dee34ca4SPoul-Henning Kamp bp->b_ioflags |= BIO_ERROR; 2886c5d3d25eSPoul-Henning Kamp bp->b_resid = bp->b_bcount - bp2->bio_completed; 2887c5d3d25eSPoul-Henning Kamp bp->b_error = bp2->bio_error; 2888756a5412SGleb Smirnoff bp->b_caller1 = NULL; 2889dee34ca4SPoul-Henning Kamp bufdone(bp); 28903398491bSAlexander Motin sp = bp2->bio_caller1; 28919e3e3fe5SWarner Losh mtx_lock(&sw_dev_mtx); 28929e3e3fe5SWarner Losh swapgeom_release(cp, sp); 28933398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 2894dee34ca4SPoul-Henning Kamp g_destroy_bio(bp2); 2895dee34ca4SPoul-Henning Kamp } 2896dee34ca4SPoul-Henning Kamp 2897dee34ca4SPoul-Henning Kamp static void 2898dee34ca4SPoul-Henning Kamp swapgeom_strategy(struct buf *bp, struct swdevt *sp) 2899dee34ca4SPoul-Henning Kamp { 2900dee34ca4SPoul-Henning Kamp struct bio *bio; 2901dee34ca4SPoul-Henning Kamp struct g_consumer *cp; 2902dee34ca4SPoul-Henning Kamp 29033398491bSAlexander Motin mtx_lock(&sw_dev_mtx); 2904dee34ca4SPoul-Henning Kamp cp = sp->sw_id; 2905dee34ca4SPoul-Henning Kamp if (cp == NULL) { 29063398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 2907dee34ca4SPoul-Henning Kamp bp->b_error = ENXIO; 2908dee34ca4SPoul-Henning Kamp bp->b_ioflags |= BIO_ERROR; 2909dee34ca4SPoul-Henning Kamp bufdone(bp); 2910dee34ca4SPoul-Henning Kamp return; 2911dee34ca4SPoul-Henning Kamp } 29129e3e3fe5SWarner Losh swapgeom_acquire(cp); 29133398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 291411041003SKonstantin Belousov if (bp->b_iocmd == BIO_WRITE) 291511041003SKonstantin Belousov bio = g_new_bio(); 291611041003SKonstantin Belousov else 29174f8205e5SPoul-Henning Kamp bio = g_alloc_bio(); 29184f8205e5SPoul-Henning Kamp if (bio == NULL) { 29199e3e3fe5SWarner Losh mtx_lock(&sw_dev_mtx); 29209e3e3fe5SWarner Losh swapgeom_release(cp, sp); 29219e3e3fe5SWarner Losh mtx_unlock(&sw_dev_mtx); 29223e5b6861SPoul-Henning Kamp bp->b_error = ENOMEM; 29233e5b6861SPoul-Henning Kamp bp->b_ioflags |= BIO_ERROR; 29240b208315SEdward Tomasz Napierala printf("swap_pager: cannot allocate bio\n"); 29253e5b6861SPoul-Henning Kamp bufdone(bp); 29263e5b6861SPoul-Henning Kamp return; 29273e5b6861SPoul-Henning Kamp } 292811041003SKonstantin Belousov 2929756a5412SGleb Smirnoff bp->b_caller1 = bio; 29303398491bSAlexander Motin bio->bio_caller1 = sp; 2931dee34ca4SPoul-Henning Kamp bio->bio_caller2 = bp; 2932c5d3d25eSPoul-Henning Kamp bio->bio_cmd = bp->b_iocmd; 2933dee34ca4SPoul-Henning Kamp bio->bio_offset = (bp->b_blkno - sp->sw_first) * PAGE_SIZE; 2934dee34ca4SPoul-Henning Kamp bio->bio_length = bp->b_bcount; 2935dee34ca4SPoul-Henning Kamp bio->bio_done = swapgeom_done; 2936c6213befSGleb Smirnoff bio->bio_flags |= BIO_SWAP; 2937fade8dd7SJeff Roberson if (!buf_mapped(bp)) { 29382cc718a1SKonstantin Belousov bio->bio_ma = bp->b_pages; 29392cc718a1SKonstantin Belousov bio->bio_data = unmapped_buf; 29402cc718a1SKonstantin Belousov bio->bio_ma_offset = (vm_offset_t)bp->b_offset & PAGE_MASK; 29412cc718a1SKonstantin Belousov bio->bio_ma_n = bp->b_npages; 29422cc718a1SKonstantin Belousov bio->bio_flags |= BIO_UNMAPPED; 29432cc718a1SKonstantin Belousov } else { 29442cc718a1SKonstantin Belousov bio->bio_data = bp->b_data; 29452cc718a1SKonstantin Belousov bio->bio_ma = NULL; 29462cc718a1SKonstantin Belousov } 2947dee34ca4SPoul-Henning Kamp g_io_request(bio, cp); 2948dee34ca4SPoul-Henning Kamp return; 2949dee34ca4SPoul-Henning Kamp } 2950dee34ca4SPoul-Henning Kamp 2951dee34ca4SPoul-Henning Kamp static void 2952dee34ca4SPoul-Henning Kamp swapgeom_orphan(struct g_consumer *cp) 2953dee34ca4SPoul-Henning Kamp { 2954dee34ca4SPoul-Henning Kamp struct swdevt *sp; 29553398491bSAlexander Motin int destroy; 2956dee34ca4SPoul-Henning Kamp 2957dee34ca4SPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 29583398491bSAlexander Motin TAILQ_FOREACH(sp, &swtailq, sw_list) { 29593398491bSAlexander Motin if (sp->sw_id == cp) { 29608f12d83aSAlexander Motin sp->sw_flags |= SW_CLOSING; 29613398491bSAlexander Motin break; 2962dee34ca4SPoul-Henning Kamp } 29633398491bSAlexander Motin } 29649e3e3fe5SWarner Losh /* 29659e3e3fe5SWarner Losh * Drop reference we were created with. Do directly since we're in a 29669e3e3fe5SWarner Losh * special context where we don't have to queue the call to 29679e3e3fe5SWarner Losh * swapgeom_close_ev(). 29689e3e3fe5SWarner Losh */ 29699e3e3fe5SWarner Losh cp->index--; 29703398491bSAlexander Motin destroy = ((sp != NULL) && (cp->index == 0)); 29713398491bSAlexander Motin if (destroy) 29723398491bSAlexander Motin sp->sw_id = NULL; 29733398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 29743398491bSAlexander Motin if (destroy) 29753398491bSAlexander Motin swapgeom_close_ev(cp, 0); 2976dee34ca4SPoul-Henning Kamp } 2977dee34ca4SPoul-Henning Kamp 2978dee34ca4SPoul-Henning Kamp static void 2979dee34ca4SPoul-Henning Kamp swapgeom_close(struct thread *td, struct swdevt *sw) 2980dee34ca4SPoul-Henning Kamp { 29813398491bSAlexander Motin struct g_consumer *cp; 2982dee34ca4SPoul-Henning Kamp 29833398491bSAlexander Motin mtx_lock(&sw_dev_mtx); 29843398491bSAlexander Motin cp = sw->sw_id; 29853398491bSAlexander Motin sw->sw_id = NULL; 29863398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 29870c657d22SKonstantin Belousov 29880c657d22SKonstantin Belousov /* 29890c657d22SKonstantin Belousov * swapgeom_close() may be called from the biodone context, 29900c657d22SKonstantin Belousov * where we cannot perform topology changes. Delegate the 29910c657d22SKonstantin Belousov * work to the events thread. 29920c657d22SKonstantin Belousov */ 29933398491bSAlexander Motin if (cp != NULL) 29943398491bSAlexander Motin g_waitfor_event(swapgeom_close_ev, cp, M_WAITOK, NULL); 2995dee34ca4SPoul-Henning Kamp } 2996dee34ca4SPoul-Henning Kamp 299788ad2d7bSKonstantin Belousov static int 299888ad2d7bSKonstantin Belousov swapongeom_locked(struct cdev *dev, struct vnode *vp) 2999dee34ca4SPoul-Henning Kamp { 3000dee34ca4SPoul-Henning Kamp struct g_provider *pp; 3001dee34ca4SPoul-Henning Kamp struct g_consumer *cp; 3002dee34ca4SPoul-Henning Kamp static struct g_geom *gp; 3003dee34ca4SPoul-Henning Kamp struct swdevt *sp; 3004dee34ca4SPoul-Henning Kamp u_long nblks; 3005dee34ca4SPoul-Henning Kamp int error; 3006dee34ca4SPoul-Henning Kamp 300788ad2d7bSKonstantin Belousov pp = g_dev_getprovider(dev); 300888ad2d7bSKonstantin Belousov if (pp == NULL) 300988ad2d7bSKonstantin Belousov return (ENODEV); 3010dee34ca4SPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 3011dee34ca4SPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 3012dee34ca4SPoul-Henning Kamp cp = sp->sw_id; 3013dee34ca4SPoul-Henning Kamp if (cp != NULL && cp->provider == pp) { 3014dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 301588ad2d7bSKonstantin Belousov return (EBUSY); 3016dee34ca4SPoul-Henning Kamp } 3017dee34ca4SPoul-Henning Kamp } 3018dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 30195721c9c7SPoul-Henning Kamp if (gp == NULL) 302002c62349SJaakko Heinonen gp = g_new_geomf(&g_swap_class, "swap"); 3021dee34ca4SPoul-Henning Kamp cp = g_new_consumer(gp); 30229e3e3fe5SWarner Losh cp->index = 1; /* Number of active I/Os, plus one for being active. */ 30239e3e3fe5SWarner Losh cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 3024dee34ca4SPoul-Henning Kamp g_attach(cp, pp); 3025afeb65e6SPoul-Henning Kamp /* 3026afeb65e6SPoul-Henning Kamp * XXX: Every time you think you can improve the margin for 3027afeb65e6SPoul-Henning Kamp * footshooting, somebody depends on the ability to do so: 3028afeb65e6SPoul-Henning Kamp * savecore(8) wants to write to our swapdev so we cannot 3029afeb65e6SPoul-Henning Kamp * set an exclusive count :-( 3030afeb65e6SPoul-Henning Kamp */ 3031d2bae332SPoul-Henning Kamp error = g_access(cp, 1, 1, 0); 303288ad2d7bSKonstantin Belousov if (error != 0) { 3033dee34ca4SPoul-Henning Kamp g_detach(cp); 3034dee34ca4SPoul-Henning Kamp g_destroy_consumer(cp); 303588ad2d7bSKonstantin Belousov return (error); 3036dee34ca4SPoul-Henning Kamp } 3037dee34ca4SPoul-Henning Kamp nblks = pp->mediasize / DEV_BSIZE; 303888ad2d7bSKonstantin Belousov swaponsomething(vp, cp, nblks, swapgeom_strategy, 303988ad2d7bSKonstantin Belousov swapgeom_close, dev2udev(dev), 30402cc718a1SKonstantin Belousov (pp->flags & G_PF_ACCEPT_UNMAPPED) != 0 ? SW_UNMAPPED : 0); 304188ad2d7bSKonstantin Belousov return (0); 3042dee34ca4SPoul-Henning Kamp } 3043dee34ca4SPoul-Henning Kamp 3044dee34ca4SPoul-Henning Kamp static int 304588ad2d7bSKonstantin Belousov swapongeom(struct vnode *vp) 3046dee34ca4SPoul-Henning Kamp { 3047dee34ca4SPoul-Henning Kamp int error; 3048dee34ca4SPoul-Henning Kamp 30496ddf41faSKonstantin Belousov ASSERT_VOP_ELOCKED(vp, "swapongeom"); 3050abd80ddbSMateusz Guzik if (vp->v_type != VCHR || VN_IS_DOOMED(vp)) { 305188ad2d7bSKonstantin Belousov error = ENOENT; 305288ad2d7bSKonstantin Belousov } else { 305388ad2d7bSKonstantin Belousov g_topology_lock(); 305488ad2d7bSKonstantin Belousov error = swapongeom_locked(vp->v_rdev, vp); 305588ad2d7bSKonstantin Belousov g_topology_unlock(); 305688ad2d7bSKonstantin Belousov } 3057dee34ca4SPoul-Henning Kamp return (error); 3058dee34ca4SPoul-Henning Kamp } 3059dee34ca4SPoul-Henning Kamp 3060dee34ca4SPoul-Henning Kamp /* 3061dee34ca4SPoul-Henning Kamp * VNODE backend 3062dee34ca4SPoul-Henning Kamp * 3063dee34ca4SPoul-Henning Kamp * This is used mainly for network filesystem (read: probably only tested 3064dee34ca4SPoul-Henning Kamp * with NFS) swapfiles. 3065dee34ca4SPoul-Henning Kamp * 3066dee34ca4SPoul-Henning Kamp */ 3067dee34ca4SPoul-Henning Kamp 3068dee34ca4SPoul-Henning Kamp static void 3069dee34ca4SPoul-Henning Kamp swapdev_strategy(struct buf *bp, struct swdevt *sp) 3070dee34ca4SPoul-Henning Kamp { 3071494eb176SPoul-Henning Kamp struct vnode *vp2; 3072dee34ca4SPoul-Henning Kamp 3073dee34ca4SPoul-Henning Kamp bp->b_blkno = ctodb(bp->b_blkno - sp->sw_first); 3074dee34ca4SPoul-Henning Kamp 3075dee34ca4SPoul-Henning Kamp vp2 = sp->sw_id; 3076dee34ca4SPoul-Henning Kamp vhold(vp2); 3077dee34ca4SPoul-Henning Kamp if (bp->b_iocmd == BIO_WRITE) { 3078b19740f4SKonstantin Belousov vn_lock(vp2, LK_EXCLUSIVE | LK_RETRY); 30793cfc7651SOlivier Houchard if (bp->b_bufobj) 3080494eb176SPoul-Henning Kamp bufobj_wdrop(bp->b_bufobj); 3081a76d8f4eSPoul-Henning Kamp bufobj_wref(&vp2->v_bufobj); 3082b19740f4SKonstantin Belousov } else { 3083b19740f4SKonstantin Belousov vn_lock(vp2, LK_SHARED | LK_RETRY); 3084dee34ca4SPoul-Henning Kamp } 30853cfc7651SOlivier Houchard if (bp->b_bufobj != &vp2->v_bufobj) 30863cfc7651SOlivier Houchard bp->b_bufobj = &vp2->v_bufobj; 3087dee34ca4SPoul-Henning Kamp bp->b_vp = vp2; 30882c18019fSPoul-Henning Kamp bp->b_iooffset = dbtob(bp->b_blkno); 3089b792bebeSPoul-Henning Kamp bstrategy(bp); 3090b19740f4SKonstantin Belousov VOP_UNLOCK(vp2); 3091dee34ca4SPoul-Henning Kamp } 3092dee34ca4SPoul-Henning Kamp 3093dee34ca4SPoul-Henning Kamp static void 3094dee34ca4SPoul-Henning Kamp swapdev_close(struct thread *td, struct swdevt *sp) 3095dee34ca4SPoul-Henning Kamp { 3096a6d04f34SKonstantin Belousov struct vnode *vp; 3097dee34ca4SPoul-Henning Kamp 3098a6d04f34SKonstantin Belousov vp = sp->sw_vp; 3099a6d04f34SKonstantin Belousov vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 3100a6d04f34SKonstantin Belousov VOP_CLOSE(vp, FREAD | FWRITE, td->td_ucred, td); 3101a6d04f34SKonstantin Belousov vput(vp); 3102dee34ca4SPoul-Henning Kamp } 3103dee34ca4SPoul-Henning Kamp 3104dee34ca4SPoul-Henning Kamp static int 3105dee34ca4SPoul-Henning Kamp swaponvp(struct thread *td, struct vnode *vp, u_long nblks) 3106dee34ca4SPoul-Henning Kamp { 3107dee34ca4SPoul-Henning Kamp struct swdevt *sp; 3108dee34ca4SPoul-Henning Kamp int error; 3109dee34ca4SPoul-Henning Kamp 31106ddf41faSKonstantin Belousov ASSERT_VOP_ELOCKED(vp, "swaponvp"); 3111dee34ca4SPoul-Henning Kamp if (nblks == 0) 3112dee34ca4SPoul-Henning Kamp return (ENXIO); 3113dee34ca4SPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 3114dee34ca4SPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 3115dee34ca4SPoul-Henning Kamp if (sp->sw_id == vp) { 3116dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 3117dee34ca4SPoul-Henning Kamp return (EBUSY); 3118dee34ca4SPoul-Henning Kamp } 3119dee34ca4SPoul-Henning Kamp } 3120dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 3121dee34ca4SPoul-Henning Kamp 3122dee34ca4SPoul-Henning Kamp #ifdef MAC 312330d239bcSRobert Watson error = mac_system_check_swapon(td->td_ucred, vp); 3124dee34ca4SPoul-Henning Kamp if (error == 0) 3125dee34ca4SPoul-Henning Kamp #endif 31269e223287SKonstantin Belousov error = VOP_OPEN(vp, FREAD | FWRITE, td->td_ucred, td, NULL); 31276ddf41faSKonstantin Belousov if (error != 0) 3128dee34ca4SPoul-Henning Kamp return (error); 3129dee34ca4SPoul-Henning Kamp 3130dee34ca4SPoul-Henning Kamp swaponsomething(vp, vp, nblks, swapdev_strategy, swapdev_close, 31312cc718a1SKonstantin Belousov NODEV, 0); 3132dee34ca4SPoul-Henning Kamp return (0); 3133dee34ca4SPoul-Henning Kamp } 313489c241d1SGleb Smirnoff 313589c241d1SGleb Smirnoff static int 313689c241d1SGleb Smirnoff sysctl_swap_async_max(SYSCTL_HANDLER_ARGS) 313789c241d1SGleb Smirnoff { 313889c241d1SGleb Smirnoff int error, new, n; 313989c241d1SGleb Smirnoff 314089c241d1SGleb Smirnoff new = nsw_wcount_async_max; 314189c241d1SGleb Smirnoff error = sysctl_handle_int(oidp, &new, 0, req); 314289c241d1SGleb Smirnoff if (error != 0 || req->newptr == NULL) 314389c241d1SGleb Smirnoff return (error); 314489c241d1SGleb Smirnoff 314589c241d1SGleb Smirnoff if (new > nswbuf / 2 || new < 1) 314689c241d1SGleb Smirnoff return (EINVAL); 314789c241d1SGleb Smirnoff 3148756a5412SGleb Smirnoff mtx_lock(&swbuf_mtx); 314989c241d1SGleb Smirnoff while (nsw_wcount_async_max != new) { 315089c241d1SGleb Smirnoff /* 315189c241d1SGleb Smirnoff * Adjust difference. If the current async count is too low, 315289c241d1SGleb Smirnoff * we will need to sqeeze our update slowly in. Sleep with a 315389c241d1SGleb Smirnoff * higher priority than getpbuf() to finish faster. 315489c241d1SGleb Smirnoff */ 315589c241d1SGleb Smirnoff n = new - nsw_wcount_async_max; 315689c241d1SGleb Smirnoff if (nsw_wcount_async + n >= 0) { 315789c241d1SGleb Smirnoff nsw_wcount_async += n; 315889c241d1SGleb Smirnoff nsw_wcount_async_max += n; 315989c241d1SGleb Smirnoff wakeup(&nsw_wcount_async); 316089c241d1SGleb Smirnoff } else { 316189c241d1SGleb Smirnoff nsw_wcount_async_max -= nsw_wcount_async; 316289c241d1SGleb Smirnoff nsw_wcount_async = 0; 3163756a5412SGleb Smirnoff msleep(&nsw_wcount_async, &swbuf_mtx, PSWP, 316489c241d1SGleb Smirnoff "swpsysctl", 0); 316589c241d1SGleb Smirnoff } 316689c241d1SGleb Smirnoff } 3167756a5412SGleb Smirnoff mtx_unlock(&swbuf_mtx); 316889c241d1SGleb Smirnoff 316989c241d1SGleb Smirnoff return (0); 317089c241d1SGleb Smirnoff } 3171fe7bcbafSKyle Evans 3172fe7bcbafSKyle Evans static void 3173fe7bcbafSKyle Evans swap_pager_update_writecount(vm_object_t object, vm_offset_t start, 3174fe7bcbafSKyle Evans vm_offset_t end) 3175fe7bcbafSKyle Evans { 3176fe7bcbafSKyle Evans 3177fe7bcbafSKyle Evans VM_OBJECT_WLOCK(object); 317863967687SJeff Roberson KASSERT((object->flags & OBJ_ANON) == 0, 3179fe7bcbafSKyle Evans ("Splittable object with writecount")); 3180fe7bcbafSKyle Evans object->un_pager.swp.writemappings += (vm_ooffset_t)end - start; 3181fe7bcbafSKyle Evans VM_OBJECT_WUNLOCK(object); 3182fe7bcbafSKyle Evans } 3183fe7bcbafSKyle Evans 3184fe7bcbafSKyle Evans static void 3185fe7bcbafSKyle Evans swap_pager_release_writecount(vm_object_t object, vm_offset_t start, 3186fe7bcbafSKyle Evans vm_offset_t end) 3187fe7bcbafSKyle Evans { 3188fe7bcbafSKyle Evans 3189fe7bcbafSKyle Evans VM_OBJECT_WLOCK(object); 319063967687SJeff Roberson KASSERT((object->flags & OBJ_ANON) == 0, 3191fe7bcbafSKyle Evans ("Splittable object with writecount")); 31926ada4e8aSKonstantin Belousov KASSERT(object->un_pager.swp.writemappings >= (vm_ooffset_t)end - start, 31936ada4e8aSKonstantin Belousov ("swap obj %p writecount %jx dec %jx", object, 31946ada4e8aSKonstantin Belousov (uintmax_t)object->un_pager.swp.writemappings, 31956ada4e8aSKonstantin Belousov (uintmax_t)((vm_ooffset_t)end - start))); 3196fe7bcbafSKyle Evans object->un_pager.swp.writemappings -= (vm_ooffset_t)end - start; 3197fe7bcbafSKyle Evans VM_OBJECT_WUNLOCK(object); 3198fe7bcbafSKyle Evans } 3199