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 1452c4992dbSAlan Cox static MALLOC_DEFINE(M_VMPGDATA, "vm_pgdata", "swap pager private data"); 14620da9c2eSPoul-Henning Kamp static struct mtx sw_dev_mtx; 1478f60c087SPoul-Henning Kamp static TAILQ_HEAD(, swdevt) swtailq = TAILQ_HEAD_INITIALIZER(swtailq); 1488f60c087SPoul-Henning Kamp static struct swdevt *swdevhd; /* Allocate from here next */ 1498f60c087SPoul-Henning Kamp static int nswapdev; /* Number of swap devices */ 1508f60c087SPoul-Henning Kamp int swap_pager_avail; 15104533e1eSKonstantin Belousov static struct sx swdev_syscall_lock; /* serialize swap(on|off) */ 152e9c0cc15SPoul-Henning Kamp 153126a2470SMateusz Guzik static __exclusive_cache_line u_long swap_reserved; 154e8bb589dSMatt Macy static u_long swap_total; 155e8bb589dSMatt Macy static int sysctl_page_shift(SYSCTL_HANDLER_ARGS); 156a8081778SJeff Roberson 1577029da5cSPawel Biernacki static SYSCTL_NODE(_vm_stats, OID_AUTO, swap, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 1587029da5cSPawel Biernacki "VM swap stats"); 159a8081778SJeff Roberson 160e8bb589dSMatt Macy SYSCTL_PROC(_vm, OID_AUTO, swap_reserved, CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE, 161567378ccSEnji Cooper &swap_reserved, 0, sysctl_page_shift, "QU", 1628a9c731fSIvan Voras "Amount of swap storage needed to back all allocated anonymous memory."); 163e8bb589dSMatt Macy SYSCTL_PROC(_vm, OID_AUTO, swap_total, CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE, 164567378ccSEnji Cooper &swap_total, 0, sysctl_page_shift, "QU", 165e8bb589dSMatt Macy "Total amount of available swap storage."); 166e8bb589dSMatt Macy 167ccdaa1abSKonstantin Belousov int vm_overcommit __read_mostly = 0; 168a6cc4c6eSKonstantin Belousov SYSCTL_INT(_vm, VM_OVERCOMMIT, overcommit, CTLFLAG_RW, &vm_overcommit, 0, 1698a9c731fSIvan Voras "Configure virtual memory overcommit behavior. See tuning(7) " 1708a9c731fSIvan Voras "for details."); 17161203277SDag-Erling Smørgrav static unsigned long swzone; 17261203277SDag-Erling Smørgrav SYSCTL_ULONG(_vm, OID_AUTO, swzone, CTLFLAG_RD, &swzone, 0, 17361203277SDag-Erling Smørgrav "Actual size of swap metadata zone"); 17461203277SDag-Erling Smørgrav static unsigned long swap_maxpages; 17561203277SDag-Erling Smørgrav SYSCTL_ULONG(_vm, OID_AUTO, swap_maxpages, CTLFLAG_RD, &swap_maxpages, 0, 17661203277SDag-Erling Smørgrav "Maximum amount of swap supported"); 1773364c323SKonstantin Belousov 178d869a17eSMark Johnston static COUNTER_U64_DEFINE_EARLY(swap_free_deferred); 179a8081778SJeff Roberson SYSCTL_COUNTER_U64(_vm_stats_swap, OID_AUTO, free_deferred, 180a8081778SJeff Roberson CTLFLAG_RD, &swap_free_deferred, 181a8081778SJeff Roberson "Number of pages that deferred freeing swap space"); 182a8081778SJeff Roberson 183d869a17eSMark Johnston static COUNTER_U64_DEFINE_EARLY(swap_free_completed); 184a8081778SJeff Roberson SYSCTL_COUNTER_U64(_vm_stats_swap, OID_AUTO, free_completed, 185a8081778SJeff Roberson CTLFLAG_RD, &swap_free_completed, 186a8081778SJeff Roberson "Number of deferred frees completed"); 187a8081778SJeff Roberson 188e8bb589dSMatt Macy static int 189e8bb589dSMatt Macy sysctl_page_shift(SYSCTL_HANDLER_ARGS) 190e8bb589dSMatt Macy { 191e8bb589dSMatt Macy uint64_t newval; 192e8bb589dSMatt Macy u_long value = *(u_long *)arg1; 193e8bb589dSMatt Macy 194e8bb589dSMatt Macy newval = ((uint64_t)value) << PAGE_SHIFT; 195e8bb589dSMatt Macy return (sysctl_handle_64(oidp, &newval, 0, req)); 196e8bb589dSMatt Macy } 197e8bb589dSMatt Macy 198ee744122SMateusz Guzik static bool 199ee744122SMateusz Guzik swap_reserve_by_cred_rlimit(u_long pincr, struct ucred *cred, int oc) 200ee744122SMateusz Guzik { 201ee744122SMateusz Guzik struct uidinfo *uip; 202ee744122SMateusz Guzik u_long prev; 203ee744122SMateusz Guzik 204ee744122SMateusz Guzik uip = cred->cr_ruidinfo; 205ee744122SMateusz Guzik 206ee744122SMateusz Guzik prev = atomic_fetchadd_long(&uip->ui_vmsize, pincr); 207ee744122SMateusz Guzik if ((oc & SWAP_RESERVE_RLIMIT_ON) != 0 && 208ee744122SMateusz Guzik prev + pincr > lim_cur(curthread, RLIMIT_SWAP) && 209ee744122SMateusz Guzik priv_check(curthread, PRIV_VM_SWAP_NORLIMIT) != 0) { 210ee744122SMateusz Guzik prev = atomic_fetchadd_long(&uip->ui_vmsize, -pincr); 21126eed2aaSKonstantin Belousov KASSERT(prev >= pincr, 21226eed2aaSKonstantin Belousov ("negative vmsize for uid %d\n", uip->ui_uid)); 213ee744122SMateusz Guzik return (false); 214ee744122SMateusz Guzik } 215ee744122SMateusz Guzik return (true); 216ee744122SMateusz Guzik } 217ee744122SMateusz Guzik 218ee744122SMateusz Guzik static void 219ee744122SMateusz Guzik swap_release_by_cred_rlimit(u_long pdecr, struct ucred *cred) 220ee744122SMateusz Guzik { 221ee744122SMateusz Guzik struct uidinfo *uip; 222ee744122SMateusz Guzik #ifdef INVARIANTS 223ee744122SMateusz Guzik u_long prev; 224ee744122SMateusz Guzik #endif 225ee744122SMateusz Guzik 226ee744122SMateusz Guzik uip = cred->cr_ruidinfo; 227ee744122SMateusz Guzik 228ee744122SMateusz Guzik #ifdef INVARIANTS 229ee744122SMateusz Guzik prev = atomic_fetchadd_long(&uip->ui_vmsize, -pdecr); 23026eed2aaSKonstantin Belousov KASSERT(prev >= pdecr, 23126eed2aaSKonstantin Belousov ("negative vmsize for uid %d\n", uip->ui_uid)); 232ee744122SMateusz Guzik #else 233ee744122SMateusz Guzik atomic_subtract_long(&uip->ui_vmsize, pdecr); 234ee744122SMateusz Guzik #endif 235ee744122SMateusz Guzik } 236ee744122SMateusz Guzik 237ee744122SMateusz Guzik static void 238ee744122SMateusz Guzik swap_reserve_force_rlimit(u_long pincr, struct ucred *cred) 239ee744122SMateusz Guzik { 240ee744122SMateusz Guzik struct uidinfo *uip; 241ee744122SMateusz Guzik 242ee744122SMateusz Guzik uip = cred->cr_ruidinfo; 243ee744122SMateusz Guzik atomic_add_long(&uip->ui_vmsize, pincr); 244ee744122SMateusz Guzik } 245ee744122SMateusz Guzik 246ee744122SMateusz Guzik bool 2473364c323SKonstantin Belousov swap_reserve(vm_ooffset_t incr) 2483364c323SKonstantin Belousov { 2493364c323SKonstantin Belousov 250ef694c1aSEdward Tomasz Napierala return (swap_reserve_by_cred(incr, curthread->td_ucred)); 2513364c323SKonstantin Belousov } 2523364c323SKonstantin Belousov 253ee744122SMateusz Guzik bool 254ef694c1aSEdward Tomasz Napierala swap_reserve_by_cred(vm_ooffset_t incr, struct ucred *cred) 2553364c323SKonstantin Belousov { 256e8bb589dSMatt Macy u_long r, s, prev, pincr; 257ee744122SMateusz Guzik #ifdef RACCT 258ee744122SMateusz Guzik int error; 259ee744122SMateusz Guzik #endif 260ee744122SMateusz Guzik int oc; 2613364c323SKonstantin Belousov static int curfail; 2623364c323SKonstantin Belousov static struct timeval lastfail; 2633364c323SKonstantin Belousov 26426eed2aaSKonstantin Belousov KASSERT((incr & PAGE_MASK) == 0, ("%s: incr: %ju & PAGE_MASK", 26526eed2aaSKonstantin Belousov __func__, (uintmax_t)incr)); 2663364c323SKonstantin Belousov 267afcc55f3SEdward Tomasz Napierala #ifdef RACCT 268ee744122SMateusz Guzik if (RACCT_ENABLED()) { 2691ba5ad42SEdward Tomasz Napierala PROC_LOCK(curproc); 2701ba5ad42SEdward Tomasz Napierala error = racct_add(curproc, RACCT_SWAP, incr); 2711ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(curproc); 2721ba5ad42SEdward Tomasz Napierala if (error != 0) 273ee744122SMateusz Guzik return (false); 2744b5c9cf6SEdward Tomasz Napierala } 275afcc55f3SEdward Tomasz Napierala #endif 2761ba5ad42SEdward Tomasz Napierala 277e8bb589dSMatt Macy pincr = atop(incr); 278e8bb589dSMatt Macy prev = atomic_fetchadd_long(&swap_reserved, pincr); 279e8bb589dSMatt Macy r = prev + pincr; 280ee744122SMateusz Guzik s = swap_total; 281a6cc4c6eSKonstantin Belousov oc = atomic_load_int(&vm_overcommit); 282ee744122SMateusz Guzik if (r > s && (oc & SWAP_RESERVE_ALLOW_NONWIRED) != 0) { 283ee744122SMateusz Guzik s += vm_cnt.v_page_count - vm_cnt.v_free_reserved - 284e958ad4cSJeff Roberson vm_wire_count(); 285ee744122SMateusz Guzik } 286ee744122SMateusz Guzik if ((oc & SWAP_RESERVE_FORCE_ON) != 0 && r > s && 287ee744122SMateusz Guzik priv_check(curthread, PRIV_VM_SWAP_NOQUOTA) != 0) { 288e8bb589dSMatt Macy prev = atomic_fetchadd_long(&swap_reserved, -pincr); 28926eed2aaSKonstantin Belousov KASSERT(prev >= pincr, 29026eed2aaSKonstantin Belousov ("swap_reserved < incr on overcommit fail")); 291ee744122SMateusz Guzik goto out_error; 2923364c323SKonstantin Belousov } 2933364c323SKonstantin Belousov 294ee744122SMateusz Guzik if (!swap_reserve_by_cred_rlimit(pincr, cred, oc)) { 295ee744122SMateusz Guzik prev = atomic_fetchadd_long(&swap_reserved, -pincr); 29626eed2aaSKonstantin Belousov KASSERT(prev >= pincr, 29726eed2aaSKonstantin Belousov ("swap_reserved < incr on overcommit fail")); 298ee744122SMateusz Guzik goto out_error; 299ee744122SMateusz Guzik } 300ee744122SMateusz Guzik 301ee744122SMateusz Guzik return (true); 302ee744122SMateusz Guzik 303ee744122SMateusz Guzik out_error: 304ee744122SMateusz Guzik if (ppsratecheck(&lastfail, &curfail, 1)) { 30526eed2aaSKonstantin Belousov printf("uid %d, pid %d: swap reservation " 30626eed2aaSKonstantin Belousov "for %jd bytes failed\n", 307ee744122SMateusz Guzik cred->cr_ruidinfo->ui_uid, curproc->p_pid, incr); 308ee744122SMateusz Guzik } 309afcc55f3SEdward Tomasz Napierala #ifdef RACCT 310ee744122SMateusz Guzik if (RACCT_ENABLED()) { 3111ba5ad42SEdward Tomasz Napierala PROC_LOCK(curproc); 3121ba5ad42SEdward Tomasz Napierala racct_sub(curproc, RACCT_SWAP, incr); 3131ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(curproc); 3141ba5ad42SEdward Tomasz Napierala } 315afcc55f3SEdward Tomasz Napierala #endif 3161ba5ad42SEdward Tomasz Napierala 317ee744122SMateusz Guzik return (false); 3183364c323SKonstantin Belousov } 3193364c323SKonstantin Belousov 3203364c323SKonstantin Belousov void 3213364c323SKonstantin Belousov swap_reserve_force(vm_ooffset_t incr) 3223364c323SKonstantin Belousov { 323e8bb589dSMatt Macy u_long pincr; 3243364c323SKonstantin Belousov 32526eed2aaSKonstantin Belousov KASSERT((incr & PAGE_MASK) == 0, ("%s: incr: %ju & PAGE_MASK", 32626eed2aaSKonstantin Belousov __func__, (uintmax_t)incr)); 3273364c323SKonstantin Belousov 328afcc55f3SEdward Tomasz Napierala #ifdef RACCT 329ee744122SMateusz Guzik if (RACCT_ENABLED()) { 330ee744122SMateusz Guzik PROC_LOCK(curproc); 3311ba5ad42SEdward Tomasz Napierala racct_add_force(curproc, RACCT_SWAP, incr); 332ee744122SMateusz Guzik PROC_UNLOCK(curproc); 333ee744122SMateusz Guzik } 334afcc55f3SEdward Tomasz Napierala #endif 335e8bb589dSMatt Macy pincr = atop(incr); 336e8bb589dSMatt Macy atomic_add_long(&swap_reserved, pincr); 337ee744122SMateusz Guzik swap_reserve_force_rlimit(pincr, curthread->td_ucred); 3383364c323SKonstantin Belousov } 3393364c323SKonstantin Belousov 3403364c323SKonstantin Belousov void 3413364c323SKonstantin Belousov swap_release(vm_ooffset_t decr) 3423364c323SKonstantin Belousov { 343ef694c1aSEdward Tomasz Napierala struct ucred *cred; 3443364c323SKonstantin Belousov 3453364c323SKonstantin Belousov PROC_LOCK(curproc); 346e8bb589dSMatt Macy cred = curproc->p_ucred; 347ef694c1aSEdward Tomasz Napierala swap_release_by_cred(decr, cred); 3483364c323SKonstantin Belousov PROC_UNLOCK(curproc); 3493364c323SKonstantin Belousov } 3503364c323SKonstantin Belousov 3513364c323SKonstantin Belousov void 352ef694c1aSEdward Tomasz Napierala swap_release_by_cred(vm_ooffset_t decr, struct ucred *cred) 3533364c323SKonstantin Belousov { 354ee744122SMateusz Guzik u_long pdecr; 355ee744122SMateusz Guzik #ifdef INVARIANTS 356ee744122SMateusz Guzik u_long prev; 357ee744122SMateusz Guzik #endif 3583364c323SKonstantin Belousov 35926eed2aaSKonstantin Belousov KASSERT((decr & PAGE_MASK) == 0, ("%s: decr: %ju & PAGE_MASK", 36026eed2aaSKonstantin Belousov __func__, (uintmax_t)decr)); 3613364c323SKonstantin Belousov 362e8bb589dSMatt Macy pdecr = atop(decr); 363ee744122SMateusz Guzik #ifdef INVARIANTS 364e8bb589dSMatt Macy prev = atomic_fetchadd_long(&swap_reserved, -pdecr); 365ee744122SMateusz Guzik KASSERT(prev >= pdecr, ("swap_reserved < decr")); 366ee744122SMateusz Guzik #else 367ee744122SMateusz Guzik atomic_subtract_long(&swap_reserved, pdecr); 368ee744122SMateusz Guzik #endif 3693364c323SKonstantin Belousov 370ee744122SMateusz Guzik swap_release_by_cred_rlimit(pdecr, cred); 371e8bb589dSMatt Macy #ifdef RACCT 372e8bb589dSMatt Macy if (racct_enable) 3731ba5ad42SEdward Tomasz Napierala racct_sub_cred(cred, RACCT_SWAP, decr); 374e8bb589dSMatt Macy #endif 3753364c323SKonstantin Belousov } 3763364c323SKonstantin Belousov 377f08b3099SKonstantin Belousov static int swap_pager_full = 2; /* swap space exhaustion (task killing) */ 3787dea2c2eSAlan Cox static int swap_pager_almost_full = 1; /* swap space exhaustion (w/hysteresis)*/ 379756a5412SGleb Smirnoff static struct mtx swbuf_mtx; /* to sync nsw_wcount_async */ 380756a5412SGleb Smirnoff static int nsw_wcount_async; /* limit async write buffers */ 381327f4e83SMatthew Dillon static int nsw_wcount_async_max;/* assigned maximum */ 382183f8e1eSGleb Smirnoff int nsw_cluster_max; /* maximum VOP I/O allowed */ 3831c7c3c6aSMatthew Dillon 38489c241d1SGleb Smirnoff static int sysctl_swap_async_max(SYSCTL_HANDLER_ARGS); 3854c36e917SKonstantin Belousov SYSCTL_PROC(_vm, OID_AUTO, swap_async_max, CTLTYPE_INT | CTLFLAG_RW | 3864c36e917SKonstantin Belousov CTLFLAG_MPSAFE, NULL, 0, sysctl_swap_async_max, "I", 3874c36e917SKonstantin Belousov "Maximum running async swap ops"); 388d027ed2eSAlan Cox static int sysctl_swap_fragmentation(SYSCTL_HANDLER_ARGS); 389d027ed2eSAlan Cox SYSCTL_PROC(_vm, OID_AUTO, swap_fragmentation, CTLTYPE_STRING | CTLFLAG_RD | 390d027ed2eSAlan Cox CTLFLAG_MPSAFE, NULL, 0, sysctl_swap_fragmentation, "A", 391d027ed2eSAlan Cox "Swap Fragmentation Info"); 39289c241d1SGleb Smirnoff 3930cddd8f0SMatthew Dillon static struct sx sw_alloc_sx; 394327f4e83SMatthew Dillon 3951c7c3c6aSMatthew Dillon /* 3961c7c3c6aSMatthew Dillon * "named" and "unnamed" anon region objects. Try to reduce the overhead 3971c7c3c6aSMatthew Dillon * of searching a named list by hashing it just a little. 3981c7c3c6aSMatthew Dillon */ 3991c7c3c6aSMatthew Dillon 4001c7c3c6aSMatthew Dillon #define NOBJLISTS 8 4011c7c3c6aSMatthew Dillon 4021c7c3c6aSMatthew Dillon #define NOBJLIST(handle) \ 403af647ddeSBruce Evans (&swap_pager_object_list[((int)(intptr_t)handle >> 4) & (NOBJLISTS-1)]) 4041c7c3c6aSMatthew Dillon 4051c7c3c6aSMatthew Dillon static struct pagerlst swap_pager_object_list[NOBJLISTS]; 406756a5412SGleb Smirnoff static uma_zone_t swwbuf_zone; 407756a5412SGleb Smirnoff static uma_zone_t swrbuf_zone; 408f425ab8eSKonstantin Belousov static uma_zone_t swblk_zone; 409f425ab8eSKonstantin Belousov static uma_zone_t swpctrie_zone; 4101c7c3c6aSMatthew Dillon 4111c7c3c6aSMatthew Dillon /* 4121c7c3c6aSMatthew Dillon * pagerops for OBJT_SWAP - "swap pager". Some ops are also global procedure 4131c7c3c6aSMatthew Dillon * calls hooked from other parts of the VM system and do not appear here. 4141c7c3c6aSMatthew Dillon * (see vm/swap_pager.h). 4151c7c3c6aSMatthew Dillon */ 416ff98689dSBruce Evans static vm_object_t 41711caded3SAlfred Perlstein swap_pager_alloc(void *handle, vm_ooffset_t size, 4183364c323SKonstantin Belousov vm_prot_t prot, vm_ooffset_t offset, struct ucred *); 41911caded3SAlfred Perlstein static void swap_pager_dealloc(vm_object_t object); 420b0cd2017SGleb Smirnoff static int swap_pager_getpages(vm_object_t, vm_page_t *, int, int *, 421b0cd2017SGleb Smirnoff int *); 422b0cd2017SGleb Smirnoff static int swap_pager_getpages_async(vm_object_t, vm_page_t *, int, int *, 423b0cd2017SGleb Smirnoff int *, pgo_getpages_iodone_t, void *); 424f74be55eSDimitry Andric static void swap_pager_putpages(vm_object_t, vm_page_t *, int, int, int *); 4255ea4972cSAlan Cox static boolean_t 4265ea4972cSAlan Cox swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, int *after); 42711caded3SAlfred Perlstein static void swap_pager_init(void); 42811caded3SAlfred Perlstein static void swap_pager_unswapped(vm_page_t); 429b3fed13eSDavid Schultz static void swap_pager_swapoff(struct swdevt *sp); 430fe7bcbafSKyle Evans static void swap_pager_update_writecount(vm_object_t object, 431fe7bcbafSKyle Evans vm_offset_t start, vm_offset_t end); 432fe7bcbafSKyle Evans static void swap_pager_release_writecount(vm_object_t object, 433fe7bcbafSKyle Evans vm_offset_t start, vm_offset_t end); 434baa1ccceSKonstantin Belousov static void swap_pager_freespace_pgo(vm_object_t object, vm_pindex_t start, 4351390a5cbSKonstantin Belousov vm_size_t size); 436f708ef1bSPoul-Henning Kamp 437d474440aSKonstantin Belousov const struct pagerops swappagerops = { 43800a3fe96SKonstantin Belousov .pgo_kvme_type = KVME_TYPE_SWAP, 439e04e4bacSPoul-Henning Kamp .pgo_init = swap_pager_init, /* early system initialization of pager */ 440e04e4bacSPoul-Henning Kamp .pgo_alloc = swap_pager_alloc, /* allocate an OBJT_SWAP object */ 441e04e4bacSPoul-Henning Kamp .pgo_dealloc = swap_pager_dealloc, /* deallocate an OBJT_SWAP object */ 442e04e4bacSPoul-Henning Kamp .pgo_getpages = swap_pager_getpages, /* pagein */ 44390effb23SGleb Smirnoff .pgo_getpages_async = swap_pager_getpages_async, /* pagein (async) */ 444e04e4bacSPoul-Henning Kamp .pgo_putpages = swap_pager_putpages, /* pageout */ 445e04e4bacSPoul-Henning Kamp .pgo_haspage = swap_pager_haspage, /* get backing store status for page */ 446e04e4bacSPoul-Henning Kamp .pgo_pageunswapped = swap_pager_unswapped, /* remove swap related to page */ 447fe7bcbafSKyle Evans .pgo_update_writecount = swap_pager_update_writecount, 448fe7bcbafSKyle Evans .pgo_release_writecount = swap_pager_release_writecount, 449baa1ccceSKonstantin Belousov .pgo_freespace = swap_pager_freespace_pgo, 4504b8365d7SKonstantin Belousov }; 4514b8365d7SKonstantin Belousov 4521c7c3c6aSMatthew Dillon /* 4531c7c3c6aSMatthew Dillon * swap_*() routines are externally accessible. swp_*() routines are 4541c7c3c6aSMatthew Dillon * internal. 4551c7c3c6aSMatthew Dillon */ 456e9c0cc15SPoul-Henning Kamp static int nswap_lowat = 128; /* in pages, swap_pager_almost_full warn */ 457e9c0cc15SPoul-Henning Kamp static int nswap_hiwat = 512; /* in pages, swap_pager_almost_full warn */ 45826f9a767SRodney W. Grimes 45907c348eaSAlan Cox SYSCTL_INT(_vm, OID_AUTO, dmmax, CTLFLAG_RD, &nsw_cluster_max, 0, 46007c348eaSAlan Cox "Maximum size of a swap block in pages"); 461cee313c4SRobert Watson 462a5edd34aSPoul-Henning Kamp static void swp_sizecheck(void); 46311caded3SAlfred Perlstein static void swp_pager_async_iodone(struct buf *bp); 464230869e0SAlan Cox static bool swp_pager_swblk_empty(struct swblk *sb, int start, int limit); 465abdab7b6SDoug Moore static void swp_pager_free_empty_swblk(vm_object_t, struct swblk *sb); 46688ad2d7bSKonstantin Belousov static int swapongeom(struct vnode *); 46759efee01SPoul-Henning Kamp static int swaponvp(struct thread *, struct vnode *, u_long); 4680190c38bSKonstantin Belousov static int swapoff_one(struct swdevt *sp, struct ucred *cred, 469e8dc2ba2SKonstantin Belousov u_int flags); 47024a1cce3SDavid Greenman 4711c7c3c6aSMatthew Dillon /* 4721c7c3c6aSMatthew Dillon * Swap bitmap functions 4731c7c3c6aSMatthew Dillon */ 474230869e0SAlan Cox static void swp_pager_freeswapspace(daddr_t blk, daddr_t npages); 47536b01270SDoug Moore static daddr_t swp_pager_getswapspace(int *npages); 4761c7c3c6aSMatthew Dillon 4771c7c3c6aSMatthew Dillon /* 4781c7c3c6aSMatthew Dillon * Metadata functions 4791c7c3c6aSMatthew Dillon */ 48078f1deefSAlan Cox static daddr_t swp_pager_meta_build(vm_object_t, vm_pindex_t, daddr_t); 481645510e6SKonstantin Belousov static void swp_pager_meta_free(vm_object_t, vm_pindex_t, vm_pindex_t, 482baa1ccceSKonstantin Belousov vm_size_t *); 483467057fcSDoug Moore static void swp_pager_meta_transfer(vm_object_t src, vm_object_t dst, 484baa1ccceSKonstantin Belousov vm_pindex_t pindex, vm_pindex_t count, vm_size_t *freed); 48511caded3SAlfred Perlstein static void swp_pager_meta_free_all(vm_object_t); 4868ecbf14bSDoug Moore static daddr_t swp_pager_meta_lookup(vm_object_t, vm_pindex_t); 4871c7c3c6aSMatthew Dillon 48878f1deefSAlan Cox static void 48978f1deefSAlan Cox swp_pager_init_freerange(daddr_t *start, daddr_t *num) 49078f1deefSAlan Cox { 49178f1deefSAlan Cox 49278f1deefSAlan Cox *start = SWAPBLK_NONE; 49378f1deefSAlan Cox *num = 0; 49478f1deefSAlan Cox } 49578f1deefSAlan Cox 49678f1deefSAlan Cox static void 49778f1deefSAlan Cox swp_pager_update_freerange(daddr_t *start, daddr_t *num, daddr_t addr) 49878f1deefSAlan Cox { 49978f1deefSAlan Cox 50078f1deefSAlan Cox if (*start + *num == addr) { 50178f1deefSAlan Cox (*num)++; 50278f1deefSAlan Cox } else { 50378f1deefSAlan Cox swp_pager_freeswapspace(*start, *num); 50478f1deefSAlan Cox *start = addr; 50578f1deefSAlan Cox *num = 1; 50678f1deefSAlan Cox } 50778f1deefSAlan Cox } 50878f1deefSAlan Cox 509f425ab8eSKonstantin Belousov static void * 510f425ab8eSKonstantin Belousov swblk_trie_alloc(struct pctrie *ptree) 511f425ab8eSKonstantin Belousov { 512f425ab8eSKonstantin Belousov 513f425ab8eSKonstantin Belousov return (uma_zalloc(swpctrie_zone, M_NOWAIT | (curproc == pageproc ? 514f425ab8eSKonstantin Belousov M_USE_RESERVE : 0))); 515f425ab8eSKonstantin Belousov } 516f425ab8eSKonstantin Belousov 517f425ab8eSKonstantin Belousov static void 518f425ab8eSKonstantin Belousov swblk_trie_free(struct pctrie *ptree, void *node) 519f425ab8eSKonstantin Belousov { 520f425ab8eSKonstantin Belousov 521f425ab8eSKonstantin Belousov uma_zfree(swpctrie_zone, node); 522f425ab8eSKonstantin Belousov } 523f425ab8eSKonstantin Belousov 524f425ab8eSKonstantin Belousov PCTRIE_DEFINE(SWAP, swblk, p, swblk_trie_alloc, swblk_trie_free); 525f425ab8eSKonstantin Belousov 5261c7c3c6aSMatthew Dillon /* 5271c7c3c6aSMatthew Dillon * SWP_SIZECHECK() - update swap_pager_full indication 5281c7c3c6aSMatthew Dillon * 52920d3034fSMatthew Dillon * update the swap_pager_almost_full indication and warn when we are 53020d3034fSMatthew Dillon * about to run out of swap space, using lowat/hiwat hysteresis. 53120d3034fSMatthew Dillon * 53220d3034fSMatthew Dillon * Clear swap_pager_full ( task killing ) indication when lowat is met. 5331c7c3c6aSMatthew Dillon * 5341c7c3c6aSMatthew Dillon * No restrictions on call 5351c7c3c6aSMatthew Dillon * This routine may not block. 5361c7c3c6aSMatthew Dillon */ 537a5edd34aSPoul-Henning Kamp static void 5382f249180SPoul-Henning Kamp swp_sizecheck(void) 5390d94caffSDavid Greenman { 54023955314SAlfred Perlstein 5418f60c087SPoul-Henning Kamp if (swap_pager_avail < nswap_lowat) { 54220d3034fSMatthew Dillon if (swap_pager_almost_full == 0) { 5431af87c92SDavid Greenman printf("swap_pager: out of swap space\n"); 54420d3034fSMatthew Dillon swap_pager_almost_full = 1; 5452b0d37a4SMatthew Dillon } 54620d3034fSMatthew Dillon } else { 54726f9a767SRodney W. Grimes swap_pager_full = 0; 5488f60c087SPoul-Henning Kamp if (swap_pager_avail > nswap_hiwat) 54920d3034fSMatthew Dillon swap_pager_almost_full = 0; 55026f9a767SRodney W. Grimes } 5511c7c3c6aSMatthew Dillon } 5521c7c3c6aSMatthew Dillon 5531c7c3c6aSMatthew Dillon /* 5541c7c3c6aSMatthew Dillon * SWAP_PAGER_INIT() - initialize the swap pager! 5551c7c3c6aSMatthew Dillon * 5561c7c3c6aSMatthew Dillon * Expected to be started from system init. NOTE: This code is run 5571c7c3c6aSMatthew Dillon * before much else so be careful what you depend on. Most of the VM 5581c7c3c6aSMatthew Dillon * system has yet to be initialized at this point. 5591c7c3c6aSMatthew Dillon */ 560f5a12711SPoul-Henning Kamp static void 5612f249180SPoul-Henning Kamp swap_pager_init(void) 562df8bae1dSRodney W. Grimes { 5631c7c3c6aSMatthew Dillon /* 5641c7c3c6aSMatthew Dillon * Initialize object lists 5651c7c3c6aSMatthew Dillon */ 5661c7c3c6aSMatthew Dillon int i; 5671c7c3c6aSMatthew Dillon 5681c7c3c6aSMatthew Dillon for (i = 0; i < NOBJLISTS; ++i) 5691c7c3c6aSMatthew Dillon TAILQ_INIT(&swap_pager_object_list[i]); 57020da9c2eSPoul-Henning Kamp mtx_init(&sw_dev_mtx, "swapdev", NULL, MTX_DEF); 57115719273SKonstantin Belousov sx_init(&sw_alloc_sx, "swspsx"); 57204533e1eSKonstantin Belousov sx_init(&swdev_syscall_lock, "swsysc"); 573183f8e1eSGleb Smirnoff 574183f8e1eSGleb Smirnoff /* 575183f8e1eSGleb Smirnoff * The nsw_cluster_max is constrained by the bp->b_pages[] 576183f8e1eSGleb Smirnoff * array, which has maxphys / PAGE_SIZE entries, and our locally 577183f8e1eSGleb Smirnoff * defined MAX_PAGEOUT_CLUSTER. Also be aware that swap ops are 578183f8e1eSGleb Smirnoff * constrained by the swap device interleave stripe size. 579183f8e1eSGleb Smirnoff * 580183f8e1eSGleb Smirnoff * Initialized early so that GEOM_ELI can see it. 581183f8e1eSGleb Smirnoff */ 582183f8e1eSGleb Smirnoff nsw_cluster_max = min(maxphys / PAGE_SIZE, MAX_PAGEOUT_CLUSTER); 5831c7c3c6aSMatthew Dillon } 58426f9a767SRodney W. Grimes 585df8bae1dSRodney W. Grimes /* 5861c7c3c6aSMatthew Dillon * SWAP_PAGER_SWAP_INIT() - swap pager initialization from pageout process 5871c7c3c6aSMatthew Dillon * 5881c7c3c6aSMatthew Dillon * Expected to be started from pageout process once, prior to entering 5891c7c3c6aSMatthew Dillon * its main loop. 590df8bae1dSRodney W. Grimes */ 59124a1cce3SDavid Greenman void 5922f249180SPoul-Henning Kamp swap_pager_swap_init(void) 593df8bae1dSRodney W. Grimes { 59461203277SDag-Erling Smørgrav unsigned long n, n2; 5950d94caffSDavid Greenman 59626f9a767SRodney W. Grimes /* 5971c7c3c6aSMatthew Dillon * Number of in-transit swap bp operations. Don't 5981c7c3c6aSMatthew Dillon * exhaust the pbufs completely. Make sure we 5991c7c3c6aSMatthew Dillon * initialize workable values (0 will work for hysteresis 6001c7c3c6aSMatthew Dillon * but it isn't very efficient). 6011c7c3c6aSMatthew Dillon * 602327f4e83SMatthew Dillon * Currently we hardwire nsw_wcount_async to 4. This limit is 603327f4e83SMatthew Dillon * designed to prevent other I/O from having high latencies due to 604327f4e83SMatthew Dillon * our pageout I/O. The value 4 works well for one or two active swap 605327f4e83SMatthew Dillon * devices but is probably a little low if you have more. Even so, 606327f4e83SMatthew Dillon * a higher value would probably generate only a limited improvement 607327f4e83SMatthew Dillon * with three or four active swap devices since the system does not 608327f4e83SMatthew Dillon * typically have to pageout at extreme bandwidths. We will want 609327f4e83SMatthew Dillon * at least 2 per swap devices, and 4 is a pretty good value if you 610327f4e83SMatthew Dillon * have one NFS swap device due to the command/ack latency over NFS. 611327f4e83SMatthew Dillon * So it all works out pretty well. 612183f8e1eSGleb Smirnoff * 613183f8e1eSGleb Smirnoff * nsw_cluster_max is initialized in swap_pager_init(). 61426f9a767SRodney W. Grimes */ 615327f4e83SMatthew Dillon 616327f4e83SMatthew Dillon nsw_wcount_async = 4; 617327f4e83SMatthew Dillon nsw_wcount_async_max = nsw_wcount_async; 618756a5412SGleb Smirnoff mtx_init(&swbuf_mtx, "async swbuf mutex", NULL, MTX_DEF); 619756a5412SGleb Smirnoff 620756a5412SGleb Smirnoff swwbuf_zone = pbuf_zsecond_create("swwbuf", nswbuf / 4); 621756a5412SGleb Smirnoff swrbuf_zone = pbuf_zsecond_create("swrbuf", nswbuf / 2); 62224a1cce3SDavid Greenman 6231c7c3c6aSMatthew Dillon /* 624a8233027SKonstantin Belousov * Initialize our zone, taking the user's requested size or 625a8233027SKonstantin Belousov * estimating the number we need based on the number of pages 626a8233027SKonstantin Belousov * in the system. 6271c7c3c6aSMatthew Dillon */ 628a8233027SKonstantin Belousov n = maxswzone != 0 ? maxswzone / sizeof(struct swblk) : 629a8233027SKonstantin Belousov vm_cnt.v_page_count / 2; 630f425ab8eSKonstantin Belousov swpctrie_zone = uma_zcreate("swpctrie", pctrie_node_size(), NULL, NULL, 6316c5f36ffSJeff Roberson pctrie_zone_init, NULL, UMA_ALIGN_PTR, 0); 632f425ab8eSKonstantin Belousov swblk_zone = uma_zcreate("swblk", sizeof(struct swblk), NULL, NULL, 6336c5f36ffSJeff Roberson NULL, NULL, _Alignof(struct swblk) - 1, 0); 63422a5e6b9SDag-Erling Smørgrav n2 = n; 6358355f576SJeff Roberson do { 636f425ab8eSKonstantin Belousov if (uma_zone_reserve_kva(swblk_zone, n)) 63761ce6eeeSAlfred Perlstein break; 63861ce6eeeSAlfred Perlstein /* 63961ce6eeeSAlfred Perlstein * if the allocation failed, try a zone two thirds the 64061ce6eeeSAlfred Perlstein * size of the previous attempt. 64161ce6eeeSAlfred Perlstein */ 64261ce6eeeSAlfred Perlstein n -= ((n + 2) / 3); 64361ce6eeeSAlfred Perlstein } while (n > 0); 64453faf5a7SKonstantin Belousov 64553faf5a7SKonstantin Belousov /* 64653faf5a7SKonstantin Belousov * Often uma_zone_reserve_kva() cannot reserve exactly the 64753faf5a7SKonstantin Belousov * requested size. Account for the difference when 64853faf5a7SKonstantin Belousov * calculating swap_maxpages. 64953faf5a7SKonstantin Belousov */ 65053faf5a7SKonstantin Belousov n = uma_zone_get_max(swblk_zone); 65153faf5a7SKonstantin Belousov 6521fffcd75SKonstantin Belousov if (n < n2) 653a8233027SKonstantin Belousov printf("Swap blk zone entries changed from %lu to %lu.\n", 654f425ab8eSKonstantin Belousov n2, n); 655303fa05aSKonstantin Belousov /* absolute maximum we can handle assuming 100% efficiency */ 65661203277SDag-Erling Smørgrav swap_maxpages = n * SWAP_META_PAGES; 657f425ab8eSKonstantin Belousov swzone = n * sizeof(struct swblk); 658f425ab8eSKonstantin Belousov if (!uma_zone_reserve_kva(swpctrie_zone, n)) 659f425ab8eSKonstantin Belousov printf("Cannot reserve swap pctrie zone, " 660f425ab8eSKonstantin Belousov "reduce kern.maxswzone.\n"); 66124a1cce3SDavid Greenman } 66224a1cce3SDavid Greenman 66328bc23abSKonstantin Belousov bool 66428bc23abSKonstantin Belousov swap_pager_init_object(vm_object_t object, void *handle, struct ucred *cred, 66528bc23abSKonstantin Belousov vm_ooffset_t size, vm_ooffset_t offset) 66628bc23abSKonstantin Belousov { 66728bc23abSKonstantin Belousov if (cred != NULL) { 66828bc23abSKonstantin Belousov if (!swap_reserve_by_cred(size, cred)) 66928bc23abSKonstantin Belousov return (false); 67028bc23abSKonstantin Belousov crhold(cred); 67128bc23abSKonstantin Belousov } 67228bc23abSKonstantin Belousov 67328bc23abSKonstantin Belousov object->un_pager.swp.writemappings = 0; 67428bc23abSKonstantin Belousov object->handle = handle; 67528bc23abSKonstantin Belousov if (cred != NULL) { 67628bc23abSKonstantin Belousov object->cred = cred; 67728bc23abSKonstantin Belousov object->charge = size; 67828bc23abSKonstantin Belousov } 67928bc23abSKonstantin Belousov return (true); 68028bc23abSKonstantin Belousov } 68128bc23abSKonstantin Belousov 682eb4d6a1bSKonstantin Belousov static vm_object_t 6834b8365d7SKonstantin Belousov swap_pager_alloc_init(objtype_t otype, void *handle, struct ucred *cred, 6844b8365d7SKonstantin Belousov vm_ooffset_t size, vm_ooffset_t offset) 685eb4d6a1bSKonstantin Belousov { 686eb4d6a1bSKonstantin Belousov vm_object_t object; 687eb4d6a1bSKonstantin Belousov 688f425ab8eSKonstantin Belousov /* 689f425ab8eSKonstantin Belousov * The un_pager.swp.swp_blks trie is initialized by 690f425ab8eSKonstantin Belousov * vm_object_allocate() to ensure the correct order of 691f425ab8eSKonstantin Belousov * visibility to other threads. 692f425ab8eSKonstantin Belousov */ 6934b8365d7SKonstantin Belousov object = vm_object_allocate(otype, OFF_TO_IDX(offset + 694eb4d6a1bSKonstantin Belousov PAGE_MASK + size)); 695f425ab8eSKonstantin Belousov 69628bc23abSKonstantin Belousov if (!swap_pager_init_object(object, handle, cred, size, offset)) { 69728bc23abSKonstantin Belousov vm_object_deallocate(object); 69828bc23abSKonstantin Belousov return (NULL); 699eb4d6a1bSKonstantin Belousov } 700eb4d6a1bSKonstantin Belousov return (object); 701eb4d6a1bSKonstantin Belousov } 702eb4d6a1bSKonstantin Belousov 70324a1cce3SDavid Greenman /* 7041c7c3c6aSMatthew Dillon * SWAP_PAGER_ALLOC() - allocate a new OBJT_SWAP VM object and instantiate 7051c7c3c6aSMatthew Dillon * its metadata structures. 7061c7c3c6aSMatthew Dillon * 7071c7c3c6aSMatthew Dillon * This routine is called from the mmap and fork code to create a new 708eb4d6a1bSKonstantin Belousov * OBJT_SWAP object. 7091c7c3c6aSMatthew Dillon * 710eb4d6a1bSKonstantin Belousov * This routine must ensure that no live duplicate is created for 711eb4d6a1bSKonstantin Belousov * the named object request, which is protected against by 712eb4d6a1bSKonstantin Belousov * holding the sw_alloc_sx lock in case handle != NULL. 71324a1cce3SDavid Greenman */ 714f5a12711SPoul-Henning Kamp static vm_object_t 7156cde7a16SDavid Greenman swap_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 7163364c323SKonstantin Belousov vm_ooffset_t offset, struct ucred *cred) 71724a1cce3SDavid Greenman { 71824a1cce3SDavid Greenman vm_object_t object; 7192f7af3dbSAlan Cox 720eb4d6a1bSKonstantin Belousov if (handle != NULL) { 7211c7c3c6aSMatthew Dillon /* 7221c7c3c6aSMatthew Dillon * Reference existing named region or allocate new one. There 7231c7c3c6aSMatthew Dillon * should not be a race here against swp_pager_meta_build() 7241c7c3c6aSMatthew Dillon * as called from vm_page_remove() in regards to the lookup 7251c7c3c6aSMatthew Dillon * of the handle. 7261c7c3c6aSMatthew Dillon */ 7270cddd8f0SMatthew Dillon sx_xlock(&sw_alloc_sx); 7281c7c3c6aSMatthew Dillon object = vm_pager_object_lookup(NOBJLIST(handle), handle); 729b5e8f167SAlan Cox if (object == NULL) { 7304b8365d7SKonstantin Belousov object = swap_pager_alloc_init(OBJT_SWAP, handle, cred, 7314b8365d7SKonstantin Belousov size, offset); 732eb4d6a1bSKonstantin Belousov if (object != NULL) { 733eb4d6a1bSKonstantin Belousov TAILQ_INSERT_TAIL(NOBJLIST(object->handle), 734eb4d6a1bSKonstantin Belousov object, pager_object_list); 7353364c323SKonstantin Belousov } 73624a1cce3SDavid Greenman } 7370cddd8f0SMatthew Dillon sx_xunlock(&sw_alloc_sx); 73824a1cce3SDavid Greenman } else { 7394b8365d7SKonstantin Belousov object = swap_pager_alloc_init(OBJT_SWAP, handle, cred, 7404b8365d7SKonstantin Belousov size, offset); 74124a1cce3SDavid Greenman } 74224a1cce3SDavid Greenman return (object); 743df8bae1dSRodney W. Grimes } 744df8bae1dSRodney W. Grimes 74526f9a767SRodney W. Grimes /* 7461c7c3c6aSMatthew Dillon * SWAP_PAGER_DEALLOC() - remove swap metadata from object 7471c7c3c6aSMatthew Dillon * 7481c7c3c6aSMatthew Dillon * The swap backing for the object is destroyed. The code is 7491c7c3c6aSMatthew Dillon * designed such that we can reinstantiate it later, but this 7501c7c3c6aSMatthew Dillon * routine is typically called only when the entire object is 7511c7c3c6aSMatthew Dillon * about to be destroyed. 7521c7c3c6aSMatthew Dillon * 75315523cf7SKonstantin Belousov * The object must be locked. 75426f9a767SRodney W. Grimes */ 755df8bae1dSRodney W. Grimes static void 7562f249180SPoul-Henning Kamp swap_pager_dealloc(vm_object_t object) 75726f9a767SRodney W. Grimes { 7584dcc5c2dSMatthew Dillon 759eb4d6a1bSKonstantin Belousov VM_OBJECT_ASSERT_WLOCKED(object); 760eb4d6a1bSKonstantin Belousov KASSERT((object->flags & OBJ_DEAD) != 0, ("dealloc of reachable obj")); 761eb4d6a1bSKonstantin Belousov 76226f9a767SRodney W. Grimes /* 7631c7c3c6aSMatthew Dillon * Remove from list right away so lookups will fail if we block for 7641c7c3c6aSMatthew Dillon * pageout completion. 76526f9a767SRodney W. Grimes */ 76667388836SKonstantin Belousov if ((object->flags & OBJ_ANON) == 0 && object->handle != NULL) { 767eb4d6a1bSKonstantin Belousov VM_OBJECT_WUNLOCK(object); 768eb4d6a1bSKonstantin Belousov sx_xlock(&sw_alloc_sx); 769eb4d6a1bSKonstantin Belousov TAILQ_REMOVE(NOBJLIST(object->handle), object, 770eb4d6a1bSKonstantin Belousov pager_object_list); 771eb4d6a1bSKonstantin Belousov sx_xunlock(&sw_alloc_sx); 772eb4d6a1bSKonstantin Belousov VM_OBJECT_WLOCK(object); 773bd228075SAlan Cox } 7741c7c3c6aSMatthew Dillon 7751c7c3c6aSMatthew Dillon vm_object_pip_wait(object, "swpdea"); 7761c7c3c6aSMatthew Dillon 7771c7c3c6aSMatthew Dillon /* 7781c7c3c6aSMatthew Dillon * Free all remaining metadata. We only bother to free it from 7791c7c3c6aSMatthew Dillon * the swap meta data. We do not attempt to free swapblk's still 7801c7c3c6aSMatthew Dillon * associated with vm_page_t's for this object. We do not care 7811c7c3c6aSMatthew Dillon * if paging is still in progress on some objects. 7821c7c3c6aSMatthew Dillon */ 7831c7c3c6aSMatthew Dillon swp_pager_meta_free_all(object); 784e735691bSJohn Baldwin object->handle = NULL; 785e735691bSJohn Baldwin object->type = OBJT_DEAD; 786fffc1c59SMark Johnston 787fffc1c59SMark Johnston /* 788fffc1c59SMark Johnston * Release the allocation charge. 789fffc1c59SMark Johnston */ 790fffc1c59SMark Johnston if (object->cred != NULL) { 791fffc1c59SMark Johnston swap_release_by_cred(object->charge, object->cred); 792fffc1c59SMark Johnston object->charge = 0; 793fffc1c59SMark Johnston crfree(object->cred); 794fffc1c59SMark Johnston object->cred = NULL; 795fffc1c59SMark Johnston } 796fffc1c59SMark Johnston 797fffc1c59SMark Johnston /* 798fffc1c59SMark Johnston * Hide the object from swap_pager_swapoff(). 799fffc1c59SMark Johnston */ 8004b8365d7SKonstantin Belousov vm_object_clear_flag(object, OBJ_SWAP); 8011c7c3c6aSMatthew Dillon } 8021c7c3c6aSMatthew Dillon 8031c7c3c6aSMatthew Dillon /************************************************************************ 8041c7c3c6aSMatthew Dillon * SWAP PAGER BITMAP ROUTINES * 8051c7c3c6aSMatthew Dillon ************************************************************************/ 8061c7c3c6aSMatthew Dillon 8071c7c3c6aSMatthew Dillon /* 8081c7c3c6aSMatthew Dillon * SWP_PAGER_GETSWAPSPACE() - allocate raw swap space 8091c7c3c6aSMatthew Dillon * 81036b01270SDoug Moore * Allocate swap for up to the requested number of pages. The 81136b01270SDoug Moore * starting swap block number (a page index) is returned or 81236b01270SDoug Moore * SWAPBLK_NONE if the allocation failed. 8131c7c3c6aSMatthew Dillon * 8141c7c3c6aSMatthew Dillon * Also has the side effect of advising that somebody made a mistake 8151c7c3c6aSMatthew Dillon * when they configured swap and didn't configure enough. 8161c7c3c6aSMatthew Dillon * 81715523cf7SKonstantin Belousov * This routine may not sleep. 8188f60c087SPoul-Henning Kamp * 8198f60c087SPoul-Henning Kamp * We allocate in round-robin fashion from the configured devices. 8201c7c3c6aSMatthew Dillon */ 821a5edd34aSPoul-Henning Kamp static daddr_t 82236b01270SDoug Moore swp_pager_getswapspace(int *io_npages) 8231c7c3c6aSMatthew Dillon { 8241c7c3c6aSMatthew Dillon daddr_t blk; 8258f60c087SPoul-Henning Kamp struct swdevt *sp; 82687ae0686SDoug Moore int mpages, npages; 8271c7c3c6aSMatthew Dillon 82836b01270SDoug Moore KASSERT(*io_npages >= 1, 82936b01270SDoug Moore ("%s: npages not positive", __func__)); 8308f60c087SPoul-Henning Kamp blk = SWAPBLK_NONE; 83131c82722SDoug Moore mpages = *io_npages; 83231c82722SDoug Moore npages = imin(BLIST_MAX_ALLOC, mpages); 83320da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 8348f60c087SPoul-Henning Kamp sp = swdevhd; 83548e98a2aSDoug Moore while (!TAILQ_EMPTY(&swtailq)) { 8368f60c087SPoul-Henning Kamp if (sp == NULL) 8378f60c087SPoul-Henning Kamp sp = TAILQ_FIRST(&swtailq); 83848e98a2aSDoug Moore if ((sp->sw_flags & SW_CLOSING) == 0) 83987ae0686SDoug Moore blk = blist_alloc(sp->sw_blist, &npages, mpages); 84048e98a2aSDoug Moore if (blk != SWAPBLK_NONE) 84148e98a2aSDoug Moore break; 84248e98a2aSDoug Moore sp = TAILQ_NEXT(sp, sw_list); 84348e98a2aSDoug Moore if (swdevhd == sp) { 84436b01270SDoug Moore if (npages == 1) 84548e98a2aSDoug Moore break; 84687ae0686SDoug Moore mpages = npages - 1; 84748e98a2aSDoug Moore npages >>= 1; 84848e98a2aSDoug Moore } 84948e98a2aSDoug Moore } 8508f60c087SPoul-Henning Kamp if (blk != SWAPBLK_NONE) { 85148e98a2aSDoug Moore *io_npages = npages; 8528f60c087SPoul-Henning Kamp blk += sp->sw_first; 8538f60c087SPoul-Henning Kamp sp->sw_used += npages; 854d05bc129SAlan Cox swap_pager_avail -= npages; 8558f60c087SPoul-Henning Kamp swp_sizecheck(); 8568f60c087SPoul-Henning Kamp swdevhd = TAILQ_NEXT(sp, sw_list); 85748e98a2aSDoug Moore } else { 8582b0d37a4SMatthew Dillon if (swap_pager_full != 2) { 85948e98a2aSDoug Moore printf("swp_pager_getswapspace(%d): failed\n", 86048e98a2aSDoug Moore *io_npages); 8612b0d37a4SMatthew Dillon swap_pager_full = 2; 86220d3034fSMatthew Dillon swap_pager_almost_full = 1; 8632b0d37a4SMatthew Dillon } 8648f60c087SPoul-Henning Kamp swdevhd = NULL; 86548e98a2aSDoug Moore } 866d05bc129SAlan Cox mtx_unlock(&sw_dev_mtx); 8671c7c3c6aSMatthew Dillon return (blk); 86826f9a767SRodney W. Grimes } 86926f9a767SRodney W. Grimes 870541a1175SAlan Cox static bool 871b3fed13eSDavid Schultz swp_pager_isondev(daddr_t blk, struct swdevt *sp) 8728f60c087SPoul-Henning Kamp { 8738f60c087SPoul-Henning Kamp 874b3fed13eSDavid Schultz return (blk >= sp->sw_first && blk < sp->sw_end); 8758f60c087SPoul-Henning Kamp } 8768f60c087SPoul-Henning Kamp 8774b03903aSPoul-Henning Kamp static void 8784b03903aSPoul-Henning Kamp swp_pager_strategy(struct buf *bp) 8794b03903aSPoul-Henning Kamp { 8804b03903aSPoul-Henning Kamp struct swdevt *sp; 8814b03903aSPoul-Henning Kamp 88220da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 8834b03903aSPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 884541a1175SAlan Cox if (swp_pager_isondev(bp->b_blkno, sp)) { 88520da9c2eSPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 8862cc718a1SKonstantin Belousov if ((sp->sw_flags & SW_UNMAPPED) != 0 && 8872cc718a1SKonstantin Belousov unmapped_buf_allowed) { 8882cc718a1SKonstantin Belousov bp->b_data = unmapped_buf; 8892cc718a1SKonstantin Belousov bp->b_offset = 0; 8902cc718a1SKonstantin Belousov } else { 8912cc718a1SKonstantin Belousov pmap_qenter((vm_offset_t)bp->b_data, 8922cc718a1SKonstantin Belousov &bp->b_pages[0], bp->b_bcount / PAGE_SIZE); 8932cc718a1SKonstantin Belousov } 8944b03903aSPoul-Henning Kamp sp->sw_strategy(bp, sp); 8954b03903aSPoul-Henning Kamp return; 8964b03903aSPoul-Henning Kamp } 8974b03903aSPoul-Henning Kamp } 89820da9c2eSPoul-Henning Kamp panic("Swapdev not found"); 8994b03903aSPoul-Henning Kamp } 9004b03903aSPoul-Henning Kamp 90126f9a767SRodney W. Grimes /* 9021c7c3c6aSMatthew Dillon * SWP_PAGER_FREESWAPSPACE() - free raw swap space 9031c7c3c6aSMatthew Dillon * 9041c7c3c6aSMatthew Dillon * This routine returns the specified swap blocks back to the bitmap. 9051c7c3c6aSMatthew Dillon * 90615523cf7SKonstantin Belousov * This routine may not sleep. 90726f9a767SRodney W. Grimes */ 908a5edd34aSPoul-Henning Kamp static void 909230869e0SAlan Cox swp_pager_freeswapspace(daddr_t blk, daddr_t npages) 9100d94caffSDavid Greenman { 9118f60c087SPoul-Henning Kamp struct swdevt *sp; 91292da00bbSMatthew Dillon 913230869e0SAlan Cox if (npages == 0) 914230869e0SAlan Cox return; 9157645e885SAlan Cox mtx_lock(&sw_dev_mtx); 9167645e885SAlan Cox TAILQ_FOREACH(sp, &swtailq, sw_list) { 917541a1175SAlan Cox if (swp_pager_isondev(blk, sp)) { 91892da00bbSMatthew Dillon sp->sw_used -= npages; 91992da00bbSMatthew Dillon /* 9207645e885SAlan Cox * If we are attempting to stop swapping on 9217645e885SAlan Cox * this device, we don't want to mark any 9227645e885SAlan Cox * blocks free lest they be reused. 92392da00bbSMatthew Dillon */ 9247645e885SAlan Cox if ((sp->sw_flags & SW_CLOSING) == 0) { 9257645e885SAlan Cox blist_free(sp->sw_blist, blk - sp->sw_first, 9267645e885SAlan Cox npages); 9278f60c087SPoul-Henning Kamp swap_pager_avail += npages; 9281c7c3c6aSMatthew Dillon swp_sizecheck(); 92926f9a767SRodney W. Grimes } 9307645e885SAlan Cox mtx_unlock(&sw_dev_mtx); 9317645e885SAlan Cox return; 9327645e885SAlan Cox } 9337645e885SAlan Cox } 9347645e885SAlan Cox panic("Swapdev not found"); 9357645e885SAlan Cox } 9361c7c3c6aSMatthew Dillon 93726f9a767SRodney W. Grimes /* 938d027ed2eSAlan Cox * SYSCTL_SWAP_FRAGMENTATION() - produce raw swap space stats 939d027ed2eSAlan Cox */ 940d027ed2eSAlan Cox static int 941d027ed2eSAlan Cox sysctl_swap_fragmentation(SYSCTL_HANDLER_ARGS) 942d027ed2eSAlan Cox { 943d027ed2eSAlan Cox struct sbuf sbuf; 944d027ed2eSAlan Cox struct swdevt *sp; 945d027ed2eSAlan Cox const char *devname; 946d027ed2eSAlan Cox int error; 947d027ed2eSAlan Cox 948d027ed2eSAlan Cox error = sysctl_wire_old_buffer(req, 0); 949d027ed2eSAlan Cox if (error != 0) 950d027ed2eSAlan Cox return (error); 951d027ed2eSAlan Cox sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 952d027ed2eSAlan Cox mtx_lock(&sw_dev_mtx); 953d027ed2eSAlan Cox TAILQ_FOREACH(sp, &swtailq, sw_list) { 9547ad2a82dSMateusz Guzik if (vn_isdisk(sp->sw_vp)) 955d027ed2eSAlan Cox devname = devtoname(sp->sw_vp->v_rdev); 956d027ed2eSAlan Cox else 957d027ed2eSAlan Cox devname = "[file]"; 958d027ed2eSAlan Cox sbuf_printf(&sbuf, "\nFree space on device %s:\n", devname); 959d027ed2eSAlan Cox blist_stats(sp->sw_blist, &sbuf); 960d027ed2eSAlan Cox } 961d027ed2eSAlan Cox mtx_unlock(&sw_dev_mtx); 962d027ed2eSAlan Cox error = sbuf_finish(&sbuf); 963d027ed2eSAlan Cox sbuf_delete(&sbuf); 964d027ed2eSAlan Cox return (error); 965d027ed2eSAlan Cox } 966d027ed2eSAlan Cox 967d027ed2eSAlan Cox /* 9681c7c3c6aSMatthew Dillon * SWAP_PAGER_FREESPACE() - frees swap blocks associated with a page 9691c7c3c6aSMatthew Dillon * range within an object. 9701c7c3c6aSMatthew Dillon * 9711c7c3c6aSMatthew Dillon * This routine removes swapblk assignments from swap metadata. 9721c7c3c6aSMatthew Dillon * 9731c7c3c6aSMatthew Dillon * The external callers of this routine typically have already destroyed 9741c7c3c6aSMatthew Dillon * or renamed vm_page_t's associated with this range in the object so 9751c7c3c6aSMatthew Dillon * we should be ok. 976c25673ffSAttilio Rao * 977c25673ffSAttilio Rao * The object must be locked. 97826f9a767SRodney W. Grimes */ 979baa1ccceSKonstantin Belousov void 980baa1ccceSKonstantin Belousov swap_pager_freespace(vm_object_t object, vm_pindex_t start, vm_size_t size, 981baa1ccceSKonstantin Belousov vm_size_t *freed) 98226f9a767SRodney W. Grimes { 983baa1ccceSKonstantin Belousov MPASS((object->flags & OBJ_SWAP) != 0); 98423955314SAlfred Perlstein 985baa1ccceSKonstantin Belousov swp_pager_meta_free(object, start, size, freed); 986baa1ccceSKonstantin Belousov } 987baa1ccceSKonstantin Belousov 988baa1ccceSKonstantin Belousov static void 989baa1ccceSKonstantin Belousov swap_pager_freespace_pgo(vm_object_t object, vm_pindex_t start, vm_size_t size) 990baa1ccceSKonstantin Belousov { 991baa1ccceSKonstantin Belousov MPASS((object->flags & OBJ_SWAP) != 0); 992baa1ccceSKonstantin Belousov 993baa1ccceSKonstantin Belousov swp_pager_meta_free(object, start, size, NULL); 9944dcc5c2dSMatthew Dillon } 9954dcc5c2dSMatthew Dillon 9964dcc5c2dSMatthew Dillon /* 9974dcc5c2dSMatthew Dillon * SWAP_PAGER_RESERVE() - reserve swap blocks in object 9984dcc5c2dSMatthew Dillon * 9994dcc5c2dSMatthew Dillon * Assigns swap blocks to the specified range within the object. The 100056ce850bSKonstantin Belousov * swap blocks are not zeroed. Any previous swap assignment is destroyed. 10014dcc5c2dSMatthew Dillon * 10024dcc5c2dSMatthew Dillon * Returns 0 on success, -1 on failure. 10034dcc5c2dSMatthew Dillon */ 10044dcc5c2dSMatthew Dillon int 1005686aa928SMark Johnston swap_pager_reserve(vm_object_t object, vm_pindex_t start, vm_pindex_t size) 10064dcc5c2dSMatthew Dillon { 100748e98a2aSDoug Moore daddr_t addr, blk, n_free, s_free; 1008686aa928SMark Johnston vm_pindex_t i, j; 1009686aa928SMark Johnston int n; 10104dcc5c2dSMatthew Dillon 101178f1deefSAlan Cox swp_pager_init_freerange(&s_free, &n_free); 101289f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 101348e98a2aSDoug Moore for (i = 0; i < size; i += n) { 1014686aa928SMark Johnston n = MIN(size - i, INT_MAX); 101536b01270SDoug Moore blk = swp_pager_getswapspace(&n); 101648e98a2aSDoug Moore if (blk == SWAPBLK_NONE) { 1017baa1ccceSKonstantin Belousov swp_pager_meta_free(object, start, i, NULL); 101889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 10194dcc5c2dSMatthew Dillon return (-1); 10204dcc5c2dSMatthew Dillon } 102148e98a2aSDoug Moore for (j = 0; j < n; ++j) { 102248e98a2aSDoug Moore addr = swp_pager_meta_build(object, 102348e98a2aSDoug Moore start + i + j, blk + j); 102478f1deefSAlan Cox if (addr != SWAPBLK_NONE) 102548e98a2aSDoug Moore swp_pager_update_freerange(&s_free, &n_free, 102648e98a2aSDoug Moore addr); 102748e98a2aSDoug Moore } 10284dcc5c2dSMatthew Dillon } 102978f1deefSAlan Cox swp_pager_freeswapspace(s_free, n_free); 103089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 10314dcc5c2dSMatthew Dillon return (0); 103226f9a767SRodney W. Grimes } 103326f9a767SRodney W. Grimes 1034467057fcSDoug Moore static bool 1035467057fcSDoug Moore swp_pager_xfer_source(vm_object_t srcobject, vm_object_t dstobject, 1036467057fcSDoug Moore vm_pindex_t pindex, daddr_t addr) 1037467057fcSDoug Moore { 1038b8ebd99aSJohn Baldwin daddr_t dstaddr __diagused; 1039467057fcSDoug Moore 10404b8365d7SKonstantin Belousov KASSERT((srcobject->flags & OBJ_SWAP) != 0, 1041cb6757c0SMark Johnston ("%s: srcobject not swappable", __func__)); 1042cb6757c0SMark Johnston KASSERT((dstobject->flags & OBJ_SWAP) != 0, 1043cb6757c0SMark Johnston ("%s: dstobject not swappable", __func__)); 1044cb6757c0SMark Johnston 1045cb6757c0SMark Johnston if (swp_pager_meta_lookup(dstobject, pindex) != SWAPBLK_NONE) { 1046467057fcSDoug Moore /* Caller should destroy the source block. */ 1047467057fcSDoug Moore return (false); 1048467057fcSDoug Moore } 1049467057fcSDoug Moore 1050467057fcSDoug Moore /* 1051467057fcSDoug Moore * Destination has no swapblk and is not resident, transfer source. 1052467057fcSDoug Moore * swp_pager_meta_build() can sleep. 1053467057fcSDoug Moore */ 1054467057fcSDoug Moore VM_OBJECT_WUNLOCK(srcobject); 1055467057fcSDoug Moore dstaddr = swp_pager_meta_build(dstobject, pindex, addr); 1056467057fcSDoug Moore KASSERT(dstaddr == SWAPBLK_NONE, 1057467057fcSDoug Moore ("Unexpected destination swapblk")); 1058467057fcSDoug Moore VM_OBJECT_WLOCK(srcobject); 105998087a06SJeff Roberson 1060467057fcSDoug Moore return (true); 1061467057fcSDoug Moore } 1062467057fcSDoug Moore 10630a47b48bSJohn Dyson /* 10641c7c3c6aSMatthew Dillon * SWAP_PAGER_COPY() - copy blocks from source pager to destination pager 10651c7c3c6aSMatthew Dillon * and destroy the source. 10661c7c3c6aSMatthew Dillon * 10671c7c3c6aSMatthew Dillon * Copy any valid swapblks from the source to the destination. In 10681c7c3c6aSMatthew Dillon * cases where both the source and destination have a valid swapblk, 10691c7c3c6aSMatthew Dillon * we keep the destination's. 10701c7c3c6aSMatthew Dillon * 107115523cf7SKonstantin Belousov * This routine is allowed to sleep. It may sleep allocating metadata 107298087a06SJeff Roberson * indirectly through swp_pager_meta_build(). 10731c7c3c6aSMatthew Dillon * 10741c7c3c6aSMatthew Dillon * The source object contains no vm_page_t's (which is just as well) 10751c7c3c6aSMatthew Dillon * 107615523cf7SKonstantin Belousov * The source and destination objects must be locked. 107715523cf7SKonstantin Belousov * Both object locks may temporarily be released. 107826f9a767SRodney W. Grimes */ 107926f9a767SRodney W. Grimes void 10802f249180SPoul-Henning Kamp swap_pager_copy(vm_object_t srcobject, vm_object_t dstobject, 10812f249180SPoul-Henning Kamp vm_pindex_t offset, int destroysource) 108226f9a767SRodney W. Grimes { 108389f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(srcobject); 108489f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(dstobject); 10850cddd8f0SMatthew Dillon 108626f9a767SRodney W. Grimes /* 10871c7c3c6aSMatthew Dillon * If destroysource is set, we remove the source object from the 10881c7c3c6aSMatthew Dillon * swap_pager internal queue now. 108926f9a767SRodney W. Grimes */ 109067388836SKonstantin Belousov if (destroysource && (srcobject->flags & OBJ_ANON) == 0 && 109167388836SKonstantin Belousov srcobject->handle != NULL) { 1092eb4d6a1bSKonstantin Belousov VM_OBJECT_WUNLOCK(srcobject); 1093eb4d6a1bSKonstantin Belousov VM_OBJECT_WUNLOCK(dstobject); 1094eb4d6a1bSKonstantin Belousov sx_xlock(&sw_alloc_sx); 1095eb4d6a1bSKonstantin Belousov TAILQ_REMOVE(NOBJLIST(srcobject->handle), srcobject, 1096eb4d6a1bSKonstantin Belousov pager_object_list); 1097eb4d6a1bSKonstantin Belousov sx_xunlock(&sw_alloc_sx); 1098eb4d6a1bSKonstantin Belousov VM_OBJECT_WLOCK(dstobject); 1099eb4d6a1bSKonstantin Belousov VM_OBJECT_WLOCK(srcobject); 1100bd228075SAlan Cox } 110126f9a767SRodney W. Grimes 11021c7c3c6aSMatthew Dillon /* 11034abca9bbSAlan Cox * Transfer source to destination. 11041c7c3c6aSMatthew Dillon */ 1105baa1ccceSKonstantin Belousov swp_pager_meta_transfer(srcobject, dstobject, offset, dstobject->size, 1106baa1ccceSKonstantin Belousov NULL); 110726f9a767SRodney W. Grimes 110826f9a767SRodney W. Grimes /* 11091c7c3c6aSMatthew Dillon * Free left over swap blocks in source. 111026f9a767SRodney W. Grimes */ 1111cb6757c0SMark Johnston if (destroysource) 11121c7c3c6aSMatthew Dillon swp_pager_meta_free_all(srcobject); 111326f9a767SRodney W. Grimes } 111426f9a767SRodney W. Grimes 1115df8bae1dSRodney W. Grimes /* 11161c7c3c6aSMatthew Dillon * SWAP_PAGER_HASPAGE() - determine if we have good backing store for 11171c7c3c6aSMatthew Dillon * the requested page. 11181c7c3c6aSMatthew Dillon * 11191c7c3c6aSMatthew Dillon * We determine whether good backing store exists for the requested 11201c7c3c6aSMatthew Dillon * page and return TRUE if it does, FALSE if it doesn't. 11211c7c3c6aSMatthew Dillon * 11221c7c3c6aSMatthew Dillon * If TRUE, we also try to determine how much valid, contiguous backing 1123915d1b71SMark Johnston * store exists before and after the requested page. 1124df8bae1dSRodney W. Grimes */ 11255ea4972cSAlan Cox static boolean_t 1126915d1b71SMark Johnston swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, 1127915d1b71SMark Johnston int *after) 112826f9a767SRodney W. Grimes { 1129915d1b71SMark Johnston daddr_t blk, blk0; 1130915d1b71SMark Johnston int i; 113126f9a767SRodney W. Grimes 1132c25673ffSAttilio Rao VM_OBJECT_ASSERT_LOCKED(object); 11334b8365d7SKonstantin Belousov KASSERT((object->flags & OBJ_SWAP) != 0, 11348ecbf14bSDoug Moore ("%s: object not swappable", __func__)); 1135915d1b71SMark Johnston 11361c7c3c6aSMatthew Dillon /* 11371c7c3c6aSMatthew Dillon * do we have good backing store at the requested index ? 11381c7c3c6aSMatthew Dillon */ 11398ecbf14bSDoug Moore blk0 = swp_pager_meta_lookup(object, pindex); 11404dcc5c2dSMatthew Dillon if (blk0 == SWAPBLK_NONE) { 11411c7c3c6aSMatthew Dillon if (before) 114224a1cce3SDavid Greenman *before = 0; 11431c7c3c6aSMatthew Dillon if (after) 114424a1cce3SDavid Greenman *after = 0; 114526f9a767SRodney W. Grimes return (FALSE); 114626f9a767SRodney W. Grimes } 114726f9a767SRodney W. Grimes 114826f9a767SRodney W. Grimes /* 11491c7c3c6aSMatthew Dillon * find backwards-looking contiguous good backing store 1150e47ed70bSJohn Dyson */ 11511c7c3c6aSMatthew Dillon if (before != NULL) { 1152915d1b71SMark Johnston for (i = 1; i < SWB_NPAGES; i++) { 11531c7c3c6aSMatthew Dillon if (i > pindex) 11541c7c3c6aSMatthew Dillon break; 11558ecbf14bSDoug Moore blk = swp_pager_meta_lookup(object, pindex - i); 11561c7c3c6aSMatthew Dillon if (blk != blk0 - i) 11571c7c3c6aSMatthew Dillon break; 1158ffc82b0aSJohn Dyson } 1159915d1b71SMark Johnston *before = i - 1; 116026f9a767SRodney W. Grimes } 116126f9a767SRodney W. Grimes 116226f9a767SRodney W. Grimes /* 11631c7c3c6aSMatthew Dillon * find forward-looking contiguous good backing store 116426f9a767SRodney W. Grimes */ 11651c7c3c6aSMatthew Dillon if (after != NULL) { 1166915d1b71SMark Johnston for (i = 1; i < SWB_NPAGES; i++) { 11678ecbf14bSDoug Moore blk = swp_pager_meta_lookup(object, pindex + i); 11681c7c3c6aSMatthew Dillon if (blk != blk0 + i) 11691c7c3c6aSMatthew Dillon break; 117026f9a767SRodney W. Grimes } 1171915d1b71SMark Johnston *after = i - 1; 11721c7c3c6aSMatthew Dillon } 11731c7c3c6aSMatthew Dillon return (TRUE); 11741c7c3c6aSMatthew Dillon } 11751c7c3c6aSMatthew Dillon 11761c7c3c6aSMatthew Dillon /* 11771c7c3c6aSMatthew Dillon * SWAP_PAGER_PAGE_UNSWAPPED() - remove swap backing store related to page 11781c7c3c6aSMatthew Dillon * 11791c7c3c6aSMatthew Dillon * This removes any associated swap backing store, whether valid or 11801c7c3c6aSMatthew Dillon * not, from the page. 11811c7c3c6aSMatthew Dillon * 11821c7c3c6aSMatthew Dillon * This routine is typically called when a page is made dirty, at 11831c7c3c6aSMatthew Dillon * which point any associated swap can be freed. MADV_FREE also 11841c7c3c6aSMatthew Dillon * calls us in a special-case situation 11851c7c3c6aSMatthew Dillon * 11861c7c3c6aSMatthew Dillon * NOTE!!! If the page is clean and the swap was valid, the caller 11871c7c3c6aSMatthew Dillon * should make the page dirty before calling this routine. This routine 11881c7c3c6aSMatthew Dillon * does NOT change the m->dirty status of the page. Also: MADV_FREE 11891c7c3c6aSMatthew Dillon * depends on it. 11901c7c3c6aSMatthew Dillon * 119115523cf7SKonstantin Belousov * This routine may not sleep. 1192c25673ffSAttilio Rao * 1193a8081778SJeff Roberson * The object containing the page may be locked. 11941c7c3c6aSMatthew Dillon */ 11951c7c3c6aSMatthew Dillon static void 11962f249180SPoul-Henning Kamp swap_pager_unswapped(vm_page_t m) 11971c7c3c6aSMatthew Dillon { 11988ecbf14bSDoug Moore struct swblk *sb; 1199a8081778SJeff Roberson vm_object_t obj; 12002f249180SPoul-Henning Kamp 1201a8081778SJeff Roberson /* 1202a8081778SJeff Roberson * Handle enqueing deferred frees first. If we do not have the 1203a8081778SJeff Roberson * object lock we wait for the page daemon to clear the space. 1204a8081778SJeff Roberson */ 1205a8081778SJeff Roberson obj = m->object; 1206a8081778SJeff Roberson if (!VM_OBJECT_WOWNED(obj)) { 1207a8081778SJeff Roberson VM_PAGE_OBJECT_BUSY_ASSERT(m); 1208a8081778SJeff Roberson /* 1209a8081778SJeff Roberson * The caller is responsible for synchronization but we 1210a8081778SJeff Roberson * will harmlessly handle races. This is typically provided 1211a8081778SJeff Roberson * by only calling unswapped() when a page transitions from 1212a8081778SJeff Roberson * clean to dirty. 1213a8081778SJeff Roberson */ 1214a8081778SJeff Roberson if ((m->a.flags & (PGA_SWAP_SPACE | PGA_SWAP_FREE)) == 1215a8081778SJeff Roberson PGA_SWAP_SPACE) { 1216a8081778SJeff Roberson vm_page_aflag_set(m, PGA_SWAP_FREE); 1217a8081778SJeff Roberson counter_u64_add(swap_free_deferred, 1); 1218a8081778SJeff Roberson } 1219a8081778SJeff Roberson return; 1220a8081778SJeff Roberson } 1221a8081778SJeff Roberson if ((m->a.flags & PGA_SWAP_FREE) != 0) 1222a8081778SJeff Roberson counter_u64_add(swap_free_completed, 1); 1223a8081778SJeff Roberson vm_page_aflag_clear(m, PGA_SWAP_FREE | PGA_SWAP_SPACE); 12248ecbf14bSDoug Moore 12258ecbf14bSDoug Moore /* 12268ecbf14bSDoug Moore * The meta data only exists if the object is OBJT_SWAP 12278ecbf14bSDoug Moore * and even then might not be allocated yet. 12288ecbf14bSDoug Moore */ 12294b8365d7SKonstantin Belousov KASSERT((m->object->flags & OBJ_SWAP) != 0, 12308ecbf14bSDoug Moore ("Free object not swappable")); 12318ecbf14bSDoug Moore 12328ecbf14bSDoug Moore sb = SWAP_PCTRIE_LOOKUP(&m->object->un_pager.swp.swp_blks, 12338ecbf14bSDoug Moore rounddown(m->pindex, SWAP_META_PAGES)); 12348ecbf14bSDoug Moore if (sb == NULL) 12358ecbf14bSDoug Moore return; 12368ecbf14bSDoug Moore if (sb->d[m->pindex % SWAP_META_PAGES] == SWAPBLK_NONE) 12378ecbf14bSDoug Moore return; 12388ecbf14bSDoug Moore swp_pager_freeswapspace(sb->d[m->pindex % SWAP_META_PAGES], 1); 12398ecbf14bSDoug Moore sb->d[m->pindex % SWAP_META_PAGES] = SWAPBLK_NONE; 12408ecbf14bSDoug Moore swp_pager_free_empty_swblk(m->object, sb); 12411c7c3c6aSMatthew Dillon } 12421c7c3c6aSMatthew Dillon 12431c7c3c6aSMatthew Dillon /* 1244915d1b71SMark Johnston * swap_pager_getpages() - bring pages in from swap 12451c7c3c6aSMatthew Dillon * 12463f060b60SMark Johnston * Attempt to page in the pages in array "ma" of length "count". The 12473f060b60SMark Johnston * caller may optionally specify that additional pages preceding and 12483f060b60SMark Johnston * succeeding the specified range be paged in. The number of such pages 12493f060b60SMark Johnston * is returned in the "rbehind" and "rahead" parameters, and they will 12503f060b60SMark Johnston * be in the inactive queue upon return. 12511c7c3c6aSMatthew Dillon * 12523f060b60SMark Johnston * The pages in "ma" must be busied and will remain busied upon return. 12531c7c3c6aSMatthew Dillon */ 1254f708ef1bSPoul-Henning Kamp static int 1255c90d075bSMark Johnston swap_pager_getpages_locked(vm_object_t object, vm_page_t *ma, int count, 1256c90d075bSMark Johnston int *rbehind, int *rahead) 1257df8bae1dSRodney W. Grimes { 12581c7c3c6aSMatthew Dillon struct buf *bp; 1259b7b8a096SKonstantin Belousov vm_page_t bm, mpred, msucc, p; 1260915d1b71SMark Johnston vm_pindex_t pindex; 12611c7c3c6aSMatthew Dillon daddr_t blk; 1262b7b8a096SKonstantin Belousov int i, maxahead, maxbehind, reqcount; 12630d94caffSDavid Greenman 1264c90d075bSMark Johnston VM_OBJECT_ASSERT_WLOCKED(object); 1265915d1b71SMark Johnston reqcount = count; 12661c7c3c6aSMatthew Dillon 12674b8365d7SKonstantin Belousov KASSERT((object->flags & OBJ_SWAP) != 0, 12688ecbf14bSDoug Moore ("%s: object not swappable", __func__)); 1269d6e13f3bSJeff Roberson if (!swap_pager_haspage(object, ma[0]->pindex, &maxbehind, &maxahead)) { 1270d6e13f3bSJeff Roberson VM_OBJECT_WUNLOCK(object); 1271915d1b71SMark Johnston return (VM_PAGER_FAIL); 1272d6e13f3bSJeff Roberson } 1273915d1b71SMark Johnston 127498087a06SJeff Roberson KASSERT(reqcount - 1 <= maxahead, 127598087a06SJeff Roberson ("page count %d extends beyond swap block", reqcount)); 127698087a06SJeff Roberson 127798087a06SJeff Roberson /* 127898087a06SJeff Roberson * Do not transfer any pages other than those that are xbusied 127998087a06SJeff Roberson * when running during a split or collapse operation. This 128098087a06SJeff Roberson * prevents clustering from re-creating pages which are being 128198087a06SJeff Roberson * moved into another object. 128298087a06SJeff Roberson */ 128398087a06SJeff Roberson if ((object->flags & (OBJ_SPLIT | OBJ_DEAD)) != 0) { 128498087a06SJeff Roberson maxahead = reqcount - 1; 128598087a06SJeff Roberson maxbehind = 0; 128698087a06SJeff Roberson } 128798087a06SJeff Roberson 1288915d1b71SMark Johnston /* 1289915d1b71SMark Johnston * Clip the readahead and readbehind ranges to exclude resident pages. 1290915d1b71SMark Johnston */ 1291915d1b71SMark Johnston if (rahead != NULL) { 1292dd9cb6daSMark Johnston *rahead = imin(*rahead, maxahead - (reqcount - 1)); 12933f060b60SMark Johnston pindex = ma[reqcount - 1]->pindex; 12943f060b60SMark Johnston msucc = TAILQ_NEXT(ma[reqcount - 1], listq); 1295915d1b71SMark Johnston if (msucc != NULL && msucc->pindex - pindex - 1 < *rahead) 1296915d1b71SMark Johnston *rahead = msucc->pindex - pindex - 1; 1297915d1b71SMark Johnston } 1298915d1b71SMark Johnston if (rbehind != NULL) { 1299dd9cb6daSMark Johnston *rbehind = imin(*rbehind, maxbehind); 13003f060b60SMark Johnston pindex = ma[0]->pindex; 13013f060b60SMark Johnston mpred = TAILQ_PREV(ma[0], pglist, listq); 1302915d1b71SMark Johnston if (mpred != NULL && pindex - mpred->pindex - 1 < *rbehind) 1303915d1b71SMark Johnston *rbehind = pindex - mpred->pindex - 1; 1304915d1b71SMark Johnston } 1305915d1b71SMark Johnston 1306b7b8a096SKonstantin Belousov bm = ma[0]; 1307b7b8a096SKonstantin Belousov for (i = 0; i < count; i++) 1308b7b8a096SKonstantin Belousov ma[i]->oflags |= VPO_SWAPINPROG; 1309b7b8a096SKonstantin Belousov 1310915d1b71SMark Johnston /* 1311915d1b71SMark Johnston * Allocate readahead and readbehind pages. 1312915d1b71SMark Johnston */ 1313b7b8a096SKonstantin Belousov if (rbehind != NULL) { 1314b7b8a096SKonstantin Belousov for (i = 1; i <= *rbehind; i++) { 13153f060b60SMark Johnston p = vm_page_alloc(object, ma[0]->pindex - i, 13167667839aSAlan Cox VM_ALLOC_NORMAL); 1317b7b8a096SKonstantin Belousov if (p == NULL) 1318915d1b71SMark Johnston break; 1319b7b8a096SKonstantin Belousov p->oflags |= VPO_SWAPINPROG; 1320b7b8a096SKonstantin Belousov bm = p; 1321915d1b71SMark Johnston } 1322b7b8a096SKonstantin Belousov *rbehind = i - 1; 1323915d1b71SMark Johnston } 1324915d1b71SMark Johnston if (rahead != NULL) { 1325915d1b71SMark Johnston for (i = 0; i < *rahead; i++) { 1326915d1b71SMark Johnston p = vm_page_alloc(object, 13273f060b60SMark Johnston ma[reqcount - 1]->pindex + i + 1, VM_ALLOC_NORMAL); 1328915d1b71SMark Johnston if (p == NULL) 1329915d1b71SMark Johnston break; 1330b7b8a096SKonstantin Belousov p->oflags |= VPO_SWAPINPROG; 1331915d1b71SMark Johnston } 1332915d1b71SMark Johnston *rahead = i; 1333915d1b71SMark Johnston } 1334915d1b71SMark Johnston if (rbehind != NULL) 1335915d1b71SMark Johnston count += *rbehind; 1336915d1b71SMark Johnston if (rahead != NULL) 1337915d1b71SMark Johnston count += *rahead; 1338915d1b71SMark Johnston 1339915d1b71SMark Johnston vm_object_pip_add(object, count); 1340915d1b71SMark Johnston 1341b7b8a096SKonstantin Belousov pindex = bm->pindex; 13428ecbf14bSDoug Moore blk = swp_pager_meta_lookup(object, pindex); 1343915d1b71SMark Johnston KASSERT(blk != SWAPBLK_NONE, 1344915d1b71SMark Johnston ("no swap blocking containing %p(%jx)", object, (uintmax_t)pindex)); 1345915d1b71SMark Johnston 1346915d1b71SMark Johnston VM_OBJECT_WUNLOCK(object); 1347756a5412SGleb Smirnoff bp = uma_zalloc(swrbuf_zone, M_WAITOK); 1348cd853791SKonstantin Belousov MPASS((bp->b_flags & B_MAXPHYS) != 0); 1349b7b8a096SKonstantin Belousov /* Pages cannot leave the object while busy. */ 1350b7b8a096SKonstantin Belousov for (i = 0, p = bm; i < count; i++, p = TAILQ_NEXT(p, listq)) { 1351b7b8a096SKonstantin Belousov MPASS(p->pindex == bm->pindex + i); 1352b7b8a096SKonstantin Belousov bp->b_pages[i] = p; 1353b7b8a096SKonstantin Belousov } 1354915d1b71SMark Johnston 1355915d1b71SMark Johnston bp->b_flags |= B_PAGING; 135621144e3bSPoul-Henning Kamp bp->b_iocmd = BIO_READ; 13571c7c3c6aSMatthew Dillon bp->b_iodone = swp_pager_async_iodone; 1358fdcc1cc0SJohn Baldwin bp->b_rcred = crhold(thread0.td_ucred); 1359fdcc1cc0SJohn Baldwin bp->b_wcred = crhold(thread0.td_ucred); 1360b0cd2017SGleb Smirnoff bp->b_blkno = blk; 1361b0cd2017SGleb Smirnoff bp->b_bcount = PAGE_SIZE * count; 1362b0cd2017SGleb Smirnoff bp->b_bufsize = PAGE_SIZE * count; 1363b0cd2017SGleb Smirnoff bp->b_npages = count; 1364915d1b71SMark Johnston bp->b_pgbefore = rbehind != NULL ? *rbehind : 0; 1365915d1b71SMark Johnston bp->b_pgafter = rahead != NULL ? *rahead : 0; 136626f9a767SRodney W. Grimes 136783c9dea1SGleb Smirnoff VM_CNT_INC(v_swapin); 136883c9dea1SGleb Smirnoff VM_CNT_ADD(v_swappgsin, count); 13691c7c3c6aSMatthew Dillon 13701c7c3c6aSMatthew Dillon /* 13711c7c3c6aSMatthew Dillon * perform the I/O. NOTE!!! bp cannot be considered valid after 13721c7c3c6aSMatthew Dillon * this point because we automatically release it on completion. 13731c7c3c6aSMatthew Dillon * Instead, we look at the one page we are interested in which we 13741c7c3c6aSMatthew Dillon * still hold a lock on even through the I/O completion. 13751c7c3c6aSMatthew Dillon * 13763f060b60SMark Johnston * The other pages in our ma[] array are also released on completion, 13771c7c3c6aSMatthew Dillon * so we cannot assume they are valid anymore either. 13781c7c3c6aSMatthew Dillon * 1379c37a77eeSPoul-Henning Kamp * NOTE: b_blkno is destroyed by the call to swapdev_strategy 13801c7c3c6aSMatthew Dillon */ 1381b890cb2cSPeter Wemm BUF_KERNPROC(bp); 13824b03903aSPoul-Henning Kamp swp_pager_strategy(bp); 138326f9a767SRodney W. Grimes 138426f9a767SRodney W. Grimes /* 1385915d1b71SMark Johnston * Wait for the pages we want to complete. VPO_SWAPINPROG is always 13861c7c3c6aSMatthew Dillon * cleared on completion. If an I/O error occurs, SWAPBLK_NONE 1387915d1b71SMark Johnston * is set in the metadata for each page in the request. 138826f9a767SRodney W. Grimes */ 138989f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1390d6e13f3bSJeff Roberson /* This could be implemented more efficiently with aflags */ 13913f060b60SMark Johnston while ((ma[0]->oflags & VPO_SWAPINPROG) != 0) { 13923f060b60SMark Johnston ma[0]->oflags |= VPO_SWAPSLEEP; 139383c9dea1SGleb Smirnoff VM_CNT_INC(v_intrans); 1394cf27e0d1SJeff Roberson if (VM_OBJECT_SLEEP(object, &object->handle, PSWP, 1395c7aebda8SAttilio Rao "swread", hz * 20)) { 13969bd86a98SBruce M Simpson printf( 1397c5690651SPoul-Henning Kamp "swap_pager: indefinite wait buffer: bufobj: %p, blkno: %jd, size: %ld\n", 1398c5690651SPoul-Henning Kamp bp->b_bufobj, (intmax_t)bp->b_blkno, bp->b_bcount); 13991c7c3c6aSMatthew Dillon } 14001b119d9dSDavid Greenman } 1401d6e13f3bSJeff Roberson VM_OBJECT_WUNLOCK(object); 140226f9a767SRodney W. Grimes 140326f9a767SRodney W. Grimes /* 1404b0cd2017SGleb Smirnoff * If we had an unrecoverable read error pages will not be valid. 140526f9a767SRodney W. Grimes */ 1406915d1b71SMark Johnston for (i = 0; i < reqcount; i++) 14073f060b60SMark Johnston if (ma[i]->valid != VM_PAGE_BITS_ALL) 14081c7c3c6aSMatthew Dillon return (VM_PAGER_ERROR); 1409b0cd2017SGleb Smirnoff 14101c7c3c6aSMatthew Dillon return (VM_PAGER_OK); 14111c7c3c6aSMatthew Dillon 14121c7c3c6aSMatthew Dillon /* 14131c7c3c6aSMatthew Dillon * A final note: in a low swap situation, we cannot deallocate swap 14141c7c3c6aSMatthew Dillon * and mark a page dirty here because the caller is likely to mark 14151c7c3c6aSMatthew Dillon * the page clean when we return, causing the page to possibly revert 14161c7c3c6aSMatthew Dillon * to all-zero's later. 14171c7c3c6aSMatthew Dillon */ 1418df8bae1dSRodney W. Grimes } 1419df8bae1dSRodney W. Grimes 1420c90d075bSMark Johnston static int 1421c90d075bSMark Johnston swap_pager_getpages(vm_object_t object, vm_page_t *ma, int count, 1422c90d075bSMark Johnston int *rbehind, int *rahead) 1423c90d075bSMark Johnston { 1424c90d075bSMark Johnston 1425c90d075bSMark Johnston VM_OBJECT_WLOCK(object); 1426c90d075bSMark Johnston return (swap_pager_getpages_locked(object, ma, count, rbehind, rahead)); 1427c90d075bSMark Johnston } 1428c90d075bSMark Johnston 14291c7c3c6aSMatthew Dillon /* 143090effb23SGleb Smirnoff * swap_pager_getpages_async(): 143190effb23SGleb Smirnoff * 143290effb23SGleb Smirnoff * Right now this is emulation of asynchronous operation on top of 143390effb23SGleb Smirnoff * swap_pager_getpages(). 143490effb23SGleb Smirnoff */ 143590effb23SGleb Smirnoff static int 14363f060b60SMark Johnston swap_pager_getpages_async(vm_object_t object, vm_page_t *ma, int count, 1437b0cd2017SGleb Smirnoff int *rbehind, int *rahead, pgo_getpages_iodone_t iodone, void *arg) 143890effb23SGleb Smirnoff { 143990effb23SGleb Smirnoff int r, error; 144090effb23SGleb Smirnoff 14413f060b60SMark Johnston r = swap_pager_getpages(object, ma, count, rbehind, rahead); 144290effb23SGleb Smirnoff switch (r) { 144390effb23SGleb Smirnoff case VM_PAGER_OK: 144490effb23SGleb Smirnoff error = 0; 144590effb23SGleb Smirnoff break; 144690effb23SGleb Smirnoff case VM_PAGER_ERROR: 144790effb23SGleb Smirnoff error = EIO; 144890effb23SGleb Smirnoff break; 144990effb23SGleb Smirnoff case VM_PAGER_FAIL: 145090effb23SGleb Smirnoff error = EINVAL; 145190effb23SGleb Smirnoff break; 145290effb23SGleb Smirnoff default: 1453d9328101SGleb Smirnoff panic("unhandled swap_pager_getpages() error %d", r); 145490effb23SGleb Smirnoff } 14553f060b60SMark Johnston (iodone)(arg, ma, count, error); 145690effb23SGleb Smirnoff 145790effb23SGleb Smirnoff return (r); 145890effb23SGleb Smirnoff } 145990effb23SGleb Smirnoff 146090effb23SGleb Smirnoff /* 14611c7c3c6aSMatthew Dillon * swap_pager_putpages: 14621c7c3c6aSMatthew Dillon * 14631c7c3c6aSMatthew Dillon * Assign swap (if necessary) and initiate I/O on the specified pages. 14641c7c3c6aSMatthew Dillon * 1465ea3aecf5SPeter Wemm * In a low memory situation we may block in VOP_STRATEGY(), but the new 14661c7c3c6aSMatthew Dillon * vm_page reservation system coupled with properly written VFS devices 14671c7c3c6aSMatthew Dillon * should ensure that no low-memory deadlock occurs. This is an area 14681c7c3c6aSMatthew Dillon * which needs work. 14691c7c3c6aSMatthew Dillon * 14701c7c3c6aSMatthew Dillon * The parent has N vm_object_pip_add() references prior to 14711c7c3c6aSMatthew Dillon * calling us and will remove references for rtvals[] that are 14721c7c3c6aSMatthew Dillon * not set to VM_PAGER_PEND. We need to remove the rest on I/O 14731c7c3c6aSMatthew Dillon * completion. 14741c7c3c6aSMatthew Dillon * 14751c7c3c6aSMatthew Dillon * The parent has soft-busy'd the pages it passes us and will unbusy 147623612f0dSDoug Moore * those whose rtvals[] entry is not set to VM_PAGER_PEND on return. 14771c7c3c6aSMatthew Dillon * We need to unbusy the rest on I/O completion. 14781c7c3c6aSMatthew Dillon */ 1479d635a37fSWarner Losh static void 14803f060b60SMark Johnston swap_pager_putpages(vm_object_t object, vm_page_t *ma, int count, 1481e065e87cSKonstantin Belousov int flags, int *rtvals) 1482df8bae1dSRodney W. Grimes { 148323612f0dSDoug Moore struct buf *bp; 148423612f0dSDoug Moore daddr_t addr, blk, n_free, s_free; 148523612f0dSDoug Moore vm_page_t mreq; 148623612f0dSDoug Moore int i, j, n; 148723612f0dSDoug Moore bool async; 1488df8bae1dSRodney W. Grimes 148923612f0dSDoug Moore KASSERT(count == 0 || ma[0]->object == object, 149023612f0dSDoug Moore ("%s: object mismatch %p/%p", 149123612f0dSDoug Moore __func__, object, ma[0]->object)); 1492ee3dc7d7SAlan Cox 149389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 149423612f0dSDoug Moore async = curproc == pageproc && (flags & VM_PAGER_PUT_SYNC) == 0; 149523612f0dSDoug Moore swp_pager_init_freerange(&s_free, &n_free); 149626f9a767SRodney W. Grimes 14971c7c3c6aSMatthew Dillon /* 14981c7c3c6aSMatthew Dillon * Assign swap blocks and issue I/O. We reallocate swap on the fly. 14991c7c3c6aSMatthew Dillon * The page is left dirty until the pageout operation completes 15001c7c3c6aSMatthew Dillon * successfully. 15011c7c3c6aSMatthew Dillon */ 15021c7c3c6aSMatthew Dillon for (i = 0; i < count; i += n) { 150331c82722SDoug Moore /* Maximum I/O size is limited by maximum swap block size. */ 150431c82722SDoug Moore n = min(count - i, nsw_cluster_max); 15051c7c3c6aSMatthew Dillon 150623612f0dSDoug Moore if (async) { 1507756a5412SGleb Smirnoff mtx_lock(&swbuf_mtx); 1508756a5412SGleb Smirnoff while (nsw_wcount_async == 0) 1509756a5412SGleb Smirnoff msleep(&nsw_wcount_async, &swbuf_mtx, PVM, 1510756a5412SGleb Smirnoff "swbufa", 0); 1511756a5412SGleb Smirnoff nsw_wcount_async--; 1512756a5412SGleb Smirnoff mtx_unlock(&swbuf_mtx); 1513327f4e83SMatthew Dillon } 1514725b4ff0SMark Johnston 1515725b4ff0SMark Johnston /* Get a block of swap of size up to size n. */ 151636b01270SDoug Moore blk = swp_pager_getswapspace(&n); 1517725b4ff0SMark Johnston if (blk == SWAPBLK_NONE) { 1518725b4ff0SMark Johnston mtx_lock(&swbuf_mtx); 1519725b4ff0SMark Johnston if (++nsw_wcount_async == 1) 1520725b4ff0SMark Johnston wakeup(&nsw_wcount_async); 1521725b4ff0SMark Johnston mtx_unlock(&swbuf_mtx); 1522725b4ff0SMark Johnston for (j = 0; j < n; ++j) 1523725b4ff0SMark Johnston rtvals[i + j] = VM_PAGER_FAIL; 1524725b4ff0SMark Johnston continue; 1525725b4ff0SMark Johnston } 152654291f7dSAlan Cox VM_OBJECT_WLOCK(object); 1527725b4ff0SMark Johnston for (j = 0; j < n; ++j) { 1528725b4ff0SMark Johnston mreq = ma[i + j]; 1529725b4ff0SMark Johnston vm_page_aflag_clear(mreq, PGA_SWAP_FREE); 1530725b4ff0SMark Johnston addr = swp_pager_meta_build(mreq->object, mreq->pindex, 1531725b4ff0SMark Johnston blk + j); 1532725b4ff0SMark Johnston if (addr != SWAPBLK_NONE) 1533725b4ff0SMark Johnston swp_pager_update_freerange(&s_free, &n_free, 1534725b4ff0SMark Johnston addr); 1535725b4ff0SMark Johnston MPASS(mreq->dirty == VM_PAGE_BITS_ALL); 1536725b4ff0SMark Johnston mreq->oflags |= VPO_SWAPINPROG; 1537725b4ff0SMark Johnston } 1538725b4ff0SMark Johnston VM_OBJECT_WUNLOCK(object); 1539725b4ff0SMark Johnston 1540756a5412SGleb Smirnoff bp = uma_zalloc(swwbuf_zone, M_WAITOK); 1541cd853791SKonstantin Belousov MPASS((bp->b_flags & B_MAXPHYS) != 0); 154223612f0dSDoug Moore if (async) 1543cd853791SKonstantin Belousov bp->b_flags |= B_ASYNC; 15445e04322aSPoul-Henning Kamp bp->b_flags |= B_PAGING; 1545912e4ae9SPoul-Henning Kamp bp->b_iocmd = BIO_WRITE; 154626f9a767SRodney W. Grimes 1547fdcc1cc0SJohn Baldwin bp->b_rcred = crhold(thread0.td_ucred); 1548fdcc1cc0SJohn Baldwin bp->b_wcred = crhold(thread0.td_ucred); 15491c7c3c6aSMatthew Dillon bp->b_bcount = PAGE_SIZE * n; 15501c7c3c6aSMatthew Dillon bp->b_bufsize = PAGE_SIZE * n; 15511c7c3c6aSMatthew Dillon bp->b_blkno = blk; 1552725b4ff0SMark Johnston for (j = 0; j < n; j++) 1553725b4ff0SMark Johnston bp->b_pages[j] = ma[i + j]; 15541c7c3c6aSMatthew Dillon bp->b_npages = n; 1555725b4ff0SMark Johnston 1556a5296b05SJulian Elischer /* 1557a5296b05SJulian Elischer * Must set dirty range for NFS to work. 1558a5296b05SJulian Elischer */ 1559a5296b05SJulian Elischer bp->b_dirtyoff = 0; 1560a5296b05SJulian Elischer bp->b_dirtyend = bp->b_bcount; 15611c7c3c6aSMatthew Dillon 156283c9dea1SGleb Smirnoff VM_CNT_INC(v_swapout); 156383c9dea1SGleb Smirnoff VM_CNT_ADD(v_swappgsout, bp->b_npages); 156426f9a767SRodney W. Grimes 156526f9a767SRodney W. Grimes /* 156677923df2SAlan Cox * We unconditionally set rtvals[] to VM_PAGER_PEND so that we 156777923df2SAlan Cox * can call the async completion routine at the end of a 156877923df2SAlan Cox * synchronous I/O operation. Otherwise, our caller would 156977923df2SAlan Cox * perform duplicate unbusy and wakeup operations on the page 157077923df2SAlan Cox * and object, respectively. 157177923df2SAlan Cox */ 157277923df2SAlan Cox for (j = 0; j < n; j++) 157377923df2SAlan Cox rtvals[i + j] = VM_PAGER_PEND; 157477923df2SAlan Cox 157577923df2SAlan Cox /* 15761c7c3c6aSMatthew Dillon * asynchronous 15771c7c3c6aSMatthew Dillon * 157823612f0dSDoug Moore * NOTE: b_blkno is destroyed by the call to swapdev_strategy. 157926f9a767SRodney W. Grimes */ 158023612f0dSDoug Moore if (async) { 15811c7c3c6aSMatthew Dillon bp->b_iodone = swp_pager_async_iodone; 158267812eacSKirk McKusick BUF_KERNPROC(bp); 15834b03903aSPoul-Henning Kamp swp_pager_strategy(bp); 15841c7c3c6aSMatthew Dillon continue; 158526f9a767SRodney W. Grimes } 1586e47ed70bSJohn Dyson 158726f9a767SRodney W. Grimes /* 15881c7c3c6aSMatthew Dillon * synchronous 15891c7c3c6aSMatthew Dillon * 159023612f0dSDoug Moore * NOTE: b_blkno is destroyed by the call to swapdev_strategy. 15911c7c3c6aSMatthew Dillon */ 15922c840b1fSAlan Cox bp->b_iodone = bdone; 15934b03903aSPoul-Henning Kamp swp_pager_strategy(bp); 15941c7c3c6aSMatthew Dillon 15951c7c3c6aSMatthew Dillon /* 159677923df2SAlan Cox * Wait for the sync I/O to complete. 159726f9a767SRodney W. Grimes */ 15982c840b1fSAlan Cox bwait(bp, PVM, "swwrt"); 159977923df2SAlan Cox 16001c7c3c6aSMatthew Dillon /* 16011c7c3c6aSMatthew Dillon * Now that we are through with the bp, we can call the 16021c7c3c6aSMatthew Dillon * normal async completion, which frees everything up. 16031c7c3c6aSMatthew Dillon */ 16041c7c3c6aSMatthew Dillon swp_pager_async_iodone(bp); 16051c7c3c6aSMatthew Dillon } 160678f1deefSAlan Cox swp_pager_freeswapspace(s_free, n_free); 160723612f0dSDoug Moore VM_OBJECT_WLOCK(object); 16081c7c3c6aSMatthew Dillon } 16091c7c3c6aSMatthew Dillon 16101c7c3c6aSMatthew Dillon /* 16111c7c3c6aSMatthew Dillon * swp_pager_async_iodone: 16121c7c3c6aSMatthew Dillon * 16131c7c3c6aSMatthew Dillon * Completion routine for asynchronous reads and writes from/to swap. 16141c7c3c6aSMatthew Dillon * Also called manually by synchronous code to finish up a bp. 16151c7c3c6aSMatthew Dillon * 161615523cf7SKonstantin Belousov * This routine may not sleep. 16171c7c3c6aSMatthew Dillon */ 16181c7c3c6aSMatthew Dillon static void 16192f249180SPoul-Henning Kamp swp_pager_async_iodone(struct buf *bp) 16201c7c3c6aSMatthew Dillon { 16211c7c3c6aSMatthew Dillon int i; 16221c7c3c6aSMatthew Dillon vm_object_t object = NULL; 16231c7c3c6aSMatthew Dillon 16241c7c3c6aSMatthew Dillon /* 16250b208315SEdward Tomasz Napierala * Report error - unless we ran out of memory, in which case 16260b208315SEdward Tomasz Napierala * we've already logged it in swapgeom_strategy(). 16271c7c3c6aSMatthew Dillon */ 16280b208315SEdward Tomasz Napierala if (bp->b_ioflags & BIO_ERROR && bp->b_error != ENOMEM) { 16291c7c3c6aSMatthew Dillon printf( 16301c7c3c6aSMatthew Dillon "swap_pager: I/O error - %s failed; blkno %ld," 16311c7c3c6aSMatthew Dillon "size %ld, error %d\n", 163221144e3bSPoul-Henning Kamp ((bp->b_iocmd == BIO_READ) ? "pagein" : "pageout"), 16331c7c3c6aSMatthew Dillon (long)bp->b_blkno, 16341c7c3c6aSMatthew Dillon (long)bp->b_bcount, 16351c7c3c6aSMatthew Dillon bp->b_error 16361c7c3c6aSMatthew Dillon ); 16371c7c3c6aSMatthew Dillon } 16381c7c3c6aSMatthew Dillon 16391c7c3c6aSMatthew Dillon /* 164026f9a767SRodney W. Grimes * remove the mapping for kernel virtual 164126f9a767SRodney W. Grimes */ 1642fade8dd7SJeff Roberson if (buf_mapped(bp)) 16431c7c3c6aSMatthew Dillon pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages); 1644fade8dd7SJeff Roberson else 1645fade8dd7SJeff Roberson bp->b_data = bp->b_kvabase; 164626f9a767SRodney W. Grimes 164733a609ecSAlan Cox if (bp->b_npages) { 164833a609ecSAlan Cox object = bp->b_pages[0]->object; 164989f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 165033a609ecSAlan Cox } 16512965a453SKip Macy 165226f9a767SRodney W. Grimes /* 16531c7c3c6aSMatthew Dillon * cleanup pages. If an error occurs writing to swap, we are in 16541c7c3c6aSMatthew Dillon * very serious trouble. If it happens to be a disk error, though, 16551c7c3c6aSMatthew Dillon * we may be able to recover by reassigning the swap later on. So 16561c7c3c6aSMatthew Dillon * in this case we remove the m->swapblk assignment for the page 16571c7c3c6aSMatthew Dillon * but do not free it in the rlist. The errornous block(s) are thus 16581c7c3c6aSMatthew Dillon * never reallocated as swap. Redirty the page and continue. 165926f9a767SRodney W. Grimes */ 16601c7c3c6aSMatthew Dillon for (i = 0; i < bp->b_npages; ++i) { 16611c7c3c6aSMatthew Dillon vm_page_t m = bp->b_pages[i]; 1662e47ed70bSJohn Dyson 16635786be7cSAlan Cox m->oflags &= ~VPO_SWAPINPROG; 1664c7aebda8SAttilio Rao if (m->oflags & VPO_SWAPSLEEP) { 1665c7aebda8SAttilio Rao m->oflags &= ~VPO_SWAPSLEEP; 1666cf27e0d1SJeff Roberson wakeup(&object->handle); 1667c7aebda8SAttilio Rao } 1668e47ed70bSJohn Dyson 1669a8081778SJeff Roberson /* We always have space after I/O, successful or not. */ 1670a8081778SJeff Roberson vm_page_aflag_set(m, PGA_SWAP_SPACE); 1671a8081778SJeff Roberson 1672c244d2deSPoul-Henning Kamp if (bp->b_ioflags & BIO_ERROR) { 1673ffc82b0aSJohn Dyson /* 16741c7c3c6aSMatthew Dillon * If an error occurs I'd love to throw the swapblk 16751c7c3c6aSMatthew Dillon * away without freeing it back to swapspace, so it 16761c7c3c6aSMatthew Dillon * can never be used again. But I can't from an 16771c7c3c6aSMatthew Dillon * interrupt. 1678ffc82b0aSJohn Dyson */ 167921144e3bSPoul-Henning Kamp if (bp->b_iocmd == BIO_READ) { 16801c7c3c6aSMatthew Dillon /* 16811c7c3c6aSMatthew Dillon * NOTE: for reads, m->dirty will probably 1682956f3135SPhilippe Charnier * be overridden by the original caller of 16831c7c3c6aSMatthew Dillon * getpages so don't play cute tricks here. 16841c7c3c6aSMatthew Dillon */ 16850012f373SJeff Roberson vm_page_invalid(m); 168646966507SMark Johnston if (i < bp->b_pgbefore || 168746966507SMark Johnston i >= bp->b_npages - bp->b_pgafter) 168846966507SMark Johnston vm_page_free_invalid(m); 16891c7c3c6aSMatthew Dillon } else { 16901c7c3c6aSMatthew Dillon /* 16911c7c3c6aSMatthew Dillon * If a write error occurs, reactivate page 16921c7c3c6aSMatthew Dillon * so it doesn't clog the inactive list, 16931c7c3c6aSMatthew Dillon * then finish the I/O. 16941c7c3c6aSMatthew Dillon */ 169541e5a226SAlan Cox MPASS(m->dirty == VM_PAGE_BITS_ALL); 16969f5632e6SMark Johnston 1697a8081778SJeff Roberson /* PQ_UNSWAPPABLE? */ 16981c7c3c6aSMatthew Dillon vm_page_activate(m); 1699c7aebda8SAttilio Rao vm_page_sunbusy(m); 17001c7c3c6aSMatthew Dillon } 170121144e3bSPoul-Henning Kamp } else if (bp->b_iocmd == BIO_READ) { 17021c7c3c6aSMatthew Dillon /* 17031c7c3c6aSMatthew Dillon * NOTE: for reads, m->dirty will probably be 1704956f3135SPhilippe Charnier * overridden by the original caller of getpages so 17051c7c3c6aSMatthew Dillon * we cannot set them in order to free the underlying 17061c7c3c6aSMatthew Dillon * swap in a low-swap situation. I don't think we'd 17071c7c3c6aSMatthew Dillon * want to do that anyway, but it was an optimization 17081c7c3c6aSMatthew Dillon * that existed in the old swapper for a time before 17091c7c3c6aSMatthew Dillon * it got ripped out due to precisely this problem. 17101c7c3c6aSMatthew Dillon */ 1711016a3c93SAlan Cox KASSERT(!pmap_page_is_mapped(m), 1712016a3c93SAlan Cox ("swp_pager_async_iodone: page %p is mapped", m)); 1713016a3c93SAlan Cox KASSERT(m->dirty == 0, 1714016a3c93SAlan Cox ("swp_pager_async_iodone: page %p is dirty", m)); 1715915d1b71SMark Johnston 17160012f373SJeff Roberson vm_page_valid(m); 1717915d1b71SMark Johnston if (i < bp->b_pgbefore || 1718915d1b71SMark Johnston i >= bp->b_npages - bp->b_pgafter) 1719915d1b71SMark Johnston vm_page_readahead_finish(m); 17201c7c3c6aSMatthew Dillon } else { 17211c7c3c6aSMatthew Dillon /* 1722016a3c93SAlan Cox * For write success, clear the dirty 17231c7c3c6aSMatthew Dillon * status, then finish the I/O ( which decrements the 17241c7c3c6aSMatthew Dillon * busy count and possibly wakes waiter's up ). 1725ebcddc72SAlan Cox * A page is only written to swap after a period of 1726ebcddc72SAlan Cox * inactivity. Therefore, we do not expect it to be 1727ebcddc72SAlan Cox * reused. 17281c7c3c6aSMatthew Dillon */ 17296031c68dSAlan Cox KASSERT(!pmap_page_is_write_mapped(m), 1730016a3c93SAlan Cox ("swp_pager_async_iodone: page %p is not write" 1731016a3c93SAlan Cox " protected", m)); 1732c52e7044SAlan Cox vm_page_undirty(m); 1733ebcddc72SAlan Cox vm_page_deactivate_noreuse(m); 1734ebcddc72SAlan Cox vm_page_sunbusy(m); 17353c4a2440SAlan Cox } 17363c4a2440SAlan Cox } 173726f9a767SRodney W. Grimes 17381c7c3c6aSMatthew Dillon /* 17391c7c3c6aSMatthew Dillon * adjust pip. NOTE: the original parent may still have its own 17401c7c3c6aSMatthew Dillon * pip refs on the object. 17411c7c3c6aSMatthew Dillon */ 17420d420ad3SAlan Cox if (object != NULL) { 17431c7c3c6aSMatthew Dillon vm_object_pip_wakeupn(object, bp->b_npages); 174489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 17450d420ad3SAlan Cox } 174626f9a767SRodney W. Grimes 17471c7c3c6aSMatthew Dillon /* 1748100650deSOlivier Houchard * swapdev_strategy() manually sets b_vp and b_bufobj before calling 1749100650deSOlivier Houchard * bstrategy(). Set them back to NULL now we're done with it, or we'll 1750100650deSOlivier Houchard * trigger a KASSERT in relpbuf(). 1751100650deSOlivier Houchard */ 1752100650deSOlivier Houchard if (bp->b_vp) { 1753100650deSOlivier Houchard bp->b_vp = NULL; 1754100650deSOlivier Houchard bp->b_bufobj = NULL; 1755100650deSOlivier Houchard } 1756100650deSOlivier Houchard /* 17571c7c3c6aSMatthew Dillon * release the physical I/O buffer 17581c7c3c6aSMatthew Dillon */ 1759756a5412SGleb Smirnoff if (bp->b_flags & B_ASYNC) { 1760756a5412SGleb Smirnoff mtx_lock(&swbuf_mtx); 1761756a5412SGleb Smirnoff if (++nsw_wcount_async == 1) 1762756a5412SGleb Smirnoff wakeup(&nsw_wcount_async); 1763756a5412SGleb Smirnoff mtx_unlock(&swbuf_mtx); 1764756a5412SGleb Smirnoff } 1765756a5412SGleb Smirnoff uma_zfree((bp->b_iocmd == BIO_READ) ? swrbuf_zone : swwbuf_zone, bp); 176626f9a767SRodney W. Grimes } 17671c7c3c6aSMatthew Dillon 1768b1fd102eSMark Johnston int 1769b1fd102eSMark Johnston swap_pager_nswapdev(void) 1770b1fd102eSMark Johnston { 1771b1fd102eSMark Johnston 1772b1fd102eSMark Johnston return (nswapdev); 1773b1fd102eSMark Johnston } 1774b1fd102eSMark Johnston 17757c022327SDoug Moore static void 17767c022327SDoug Moore swp_pager_force_dirty(vm_page_t m) 17777c022327SDoug Moore { 17787c022327SDoug Moore 17797c022327SDoug Moore vm_page_dirty(m); 178056948d17SDoug Moore swap_pager_unswapped(m); 17817c022327SDoug Moore vm_page_launder(m); 178292da00bbSMatthew Dillon } 178392da00bbSMatthew Dillon 1784ecfbddf0SKonstantin Belousov u_long 1785ecfbddf0SKonstantin Belousov swap_pager_swapped_pages(vm_object_t object) 1786ecfbddf0SKonstantin Belousov { 1787ecfbddf0SKonstantin Belousov struct swblk *sb; 1788ecfbddf0SKonstantin Belousov vm_pindex_t pi; 1789ecfbddf0SKonstantin Belousov u_long res; 1790ecfbddf0SKonstantin Belousov int i; 1791ecfbddf0SKonstantin Belousov 1792ecfbddf0SKonstantin Belousov VM_OBJECT_ASSERT_LOCKED(object); 1793cb6757c0SMark Johnston 1794cb6757c0SMark Johnston if (pctrie_is_empty(&object->un_pager.swp.swp_blks)) 1795ecfbddf0SKonstantin Belousov return (0); 1796ecfbddf0SKonstantin Belousov 1797ecfbddf0SKonstantin Belousov for (res = 0, pi = 0; (sb = SWAP_PCTRIE_LOOKUP_GE( 1798ecfbddf0SKonstantin Belousov &object->un_pager.swp.swp_blks, pi)) != NULL; 1799ecfbddf0SKonstantin Belousov pi = sb->p + SWAP_META_PAGES) { 1800ecfbddf0SKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) { 1801ecfbddf0SKonstantin Belousov if (sb->d[i] != SWAPBLK_NONE) 1802ecfbddf0SKonstantin Belousov res++; 1803ecfbddf0SKonstantin Belousov } 1804ecfbddf0SKonstantin Belousov } 1805ecfbddf0SKonstantin Belousov return (res); 1806ecfbddf0SKonstantin Belousov } 1807ecfbddf0SKonstantin Belousov 180892da00bbSMatthew Dillon /* 18097c022327SDoug Moore * swap_pager_swapoff_object: 18107c022327SDoug Moore * 18117c022327SDoug Moore * Page in all of the pages that have been paged out for an object 181256948d17SDoug Moore * to a swap device. 18137c022327SDoug Moore */ 18147c022327SDoug Moore static void 18157c022327SDoug Moore swap_pager_swapoff_object(struct swdevt *sp, vm_object_t object) 18167c022327SDoug Moore { 18177c022327SDoug Moore struct swblk *sb; 1818c90d075bSMark Johnston vm_page_t m; 1819c90d075bSMark Johnston vm_pindex_t pi; 1820c90d075bSMark Johnston daddr_t blk; 1821c90d075bSMark Johnston int i, nv, rahead, rv; 18227c022327SDoug Moore 18234b8365d7SKonstantin Belousov KASSERT((object->flags & OBJ_SWAP) != 0, 18248ecbf14bSDoug Moore ("%s: Object not swappable", __func__)); 1825c90d075bSMark Johnston 18267c022327SDoug Moore for (pi = 0; (sb = SWAP_PCTRIE_LOOKUP_GE( 18277c022327SDoug Moore &object->un_pager.swp.swp_blks, pi)) != NULL; ) { 1828c90d075bSMark Johnston if ((object->flags & OBJ_DEAD) != 0) { 1829c90d075bSMark Johnston /* 1830c90d075bSMark Johnston * Make sure that pending writes finish before 1831c90d075bSMark Johnston * returning. 1832c90d075bSMark Johnston */ 1833c90d075bSMark Johnston vm_object_pip_wait(object, "swpoff"); 1834c90d075bSMark Johnston swp_pager_meta_free_all(object); 1835c90d075bSMark Johnston break; 1836c90d075bSMark Johnston } 18377c022327SDoug Moore for (i = 0; i < SWAP_META_PAGES; i++) { 183856948d17SDoug Moore /* 1839c90d075bSMark Johnston * Count the number of contiguous valid blocks. 184056948d17SDoug Moore */ 1841c90d075bSMark Johnston for (nv = 0; nv < SWAP_META_PAGES - i; nv++) { 1842c90d075bSMark Johnston blk = sb->d[i + nv]; 1843c90d075bSMark Johnston if (!swp_pager_isondev(blk, sp) || 1844c90d075bSMark Johnston blk == SWAPBLK_NONE) 1845c90d075bSMark Johnston break; 184656948d17SDoug Moore } 1847c90d075bSMark Johnston if (nv == 0) 18487c022327SDoug Moore continue; 184956948d17SDoug Moore 185056948d17SDoug Moore /* 1851c90d075bSMark Johnston * Look for a page corresponding to the first 1852c90d075bSMark Johnston * valid block and ensure that any pending paging 1853c90d075bSMark Johnston * operations on it are complete. If the page is valid, 1854c90d075bSMark Johnston * mark it dirty and free the swap block. Try to batch 1855c90d075bSMark Johnston * this operation since it may cause sp to be freed, 1856c90d075bSMark Johnston * meaning that we must restart the scan. Avoid busying 1857c90d075bSMark Johnston * valid pages since we may block forever on kernel 1858c90d075bSMark Johnston * stack pages. 185956948d17SDoug Moore */ 1860c90d075bSMark Johnston m = vm_page_lookup(object, sb->p + i); 1861c90d075bSMark Johnston if (m == NULL) { 1862c90d075bSMark Johnston m = vm_page_alloc(object, sb->p + i, 1863c90d075bSMark Johnston VM_ALLOC_NORMAL | VM_ALLOC_WAITFAIL); 1864c90d075bSMark Johnston if (m == NULL) 1865c90d075bSMark Johnston break; 1866c90d075bSMark Johnston } else { 1867c90d075bSMark Johnston if ((m->oflags & VPO_SWAPINPROG) != 0) { 1868c90d075bSMark Johnston m->oflags |= VPO_SWAPSLEEP; 1869c90d075bSMark Johnston VM_OBJECT_SLEEP(object, &object->handle, 1870c90d075bSMark Johnston PSWP, "swpoff", 0); 1871c90d075bSMark Johnston break; 1872c90d075bSMark Johnston } 1873c90d075bSMark Johnston if (vm_page_all_valid(m)) { 1874c90d075bSMark Johnston do { 1875c90d075bSMark Johnston swp_pager_force_dirty(m); 1876c90d075bSMark Johnston } while (--nv > 0 && 1877c90d075bSMark Johnston (m = vm_page_next(m)) != NULL && 1878c90d075bSMark Johnston vm_page_all_valid(m) && 1879c90d075bSMark Johnston (m->oflags & VPO_SWAPINPROG) == 0); 1880c90d075bSMark Johnston break; 1881c90d075bSMark Johnston } 1882c90d075bSMark Johnston if (!vm_page_busy_acquire(m, VM_ALLOC_WAITFAIL)) 1883c90d075bSMark Johnston break; 18847c022327SDoug Moore } 188556948d17SDoug Moore 1886c90d075bSMark Johnston vm_object_pip_add(object, 1); 1887c90d075bSMark Johnston rahead = SWAP_META_PAGES; 1888c90d075bSMark Johnston rv = swap_pager_getpages_locked(object, &m, 1, NULL, 1889c90d075bSMark Johnston &rahead); 1890c90d075bSMark Johnston if (rv != VM_PAGER_OK) 1891c90d075bSMark Johnston panic("%s: read from swap failed: %d", 1892c90d075bSMark Johnston __func__, rv); 1893c90d075bSMark Johnston VM_OBJECT_WLOCK(object); 1894e61568aeSMark Johnston vm_object_pip_wakeupn(object, 1); 1895c90d075bSMark Johnston vm_page_xunbusy(m); 1896c90d075bSMark Johnston 189756948d17SDoug Moore /* 1898c90d075bSMark Johnston * The object lock was dropped so we must restart the 1899c90d075bSMark Johnston * scan of this swap block. Pages paged in during this 1900c90d075bSMark Johnston * iteration will be marked dirty in a future iteration. 190156948d17SDoug Moore */ 190256948d17SDoug Moore break; 190356948d17SDoug Moore } 190456948d17SDoug Moore if (i == SWAP_META_PAGES) 190556948d17SDoug Moore pi = sb->p + SWAP_META_PAGES; 190656948d17SDoug Moore } 19077c022327SDoug Moore } 19087c022327SDoug Moore 19097c022327SDoug Moore /* 191092da00bbSMatthew Dillon * swap_pager_swapoff: 191192da00bbSMatthew Dillon * 191292da00bbSMatthew Dillon * Page in all of the pages that have been paged out to the 191392da00bbSMatthew Dillon * given device. The corresponding blocks in the bitmap must be 191492da00bbSMatthew Dillon * marked as allocated and the device must be flagged SW_CLOSING. 191592da00bbSMatthew Dillon * There may be no processes swapped out to the device. 191692da00bbSMatthew Dillon * 191792da00bbSMatthew Dillon * This routine may block. 191892da00bbSMatthew Dillon */ 1919e9c0cc15SPoul-Henning Kamp static void 1920b3fed13eSDavid Schultz swap_pager_swapoff(struct swdevt *sp) 192192da00bbSMatthew Dillon { 1922f425ab8eSKonstantin Belousov vm_object_t object; 19237c022327SDoug Moore int retries; 192492da00bbSMatthew Dillon 192504533e1eSKonstantin Belousov sx_assert(&swdev_syscall_lock, SA_XLOCKED); 192692da00bbSMatthew Dillon 19278bc61209SDavid Schultz retries = 0; 192892da00bbSMatthew Dillon full_rescan: 1929f425ab8eSKonstantin Belousov mtx_lock(&vm_object_list_mtx); 1930f425ab8eSKonstantin Belousov TAILQ_FOREACH(object, &vm_object_list, object_list) { 19314b8365d7SKonstantin Belousov if ((object->flags & OBJ_SWAP) == 0) 193298150664SKonstantin Belousov continue; 1933f425ab8eSKonstantin Belousov mtx_unlock(&vm_object_list_mtx); 193498150664SKonstantin Belousov /* Depends on type-stability. */ 193598150664SKonstantin Belousov VM_OBJECT_WLOCK(object); 1936f425ab8eSKonstantin Belousov 1937f425ab8eSKonstantin Belousov /* 1938f425ab8eSKonstantin Belousov * Dead objects are eventually terminated on their own. 1939f425ab8eSKonstantin Belousov */ 1940f425ab8eSKonstantin Belousov if ((object->flags & OBJ_DEAD) != 0) 1941f425ab8eSKonstantin Belousov goto next_obj; 1942f425ab8eSKonstantin Belousov 1943f425ab8eSKonstantin Belousov /* 1944f425ab8eSKonstantin Belousov * Sync with fences placed after pctrie 1945f425ab8eSKonstantin Belousov * initialization. We must not access pctrie below 1946f425ab8eSKonstantin Belousov * unless we checked that our object is swap and not 1947f425ab8eSKonstantin Belousov * dead. 1948f425ab8eSKonstantin Belousov */ 1949f425ab8eSKonstantin Belousov atomic_thread_fence_acq(); 19504b8365d7SKonstantin Belousov if ((object->flags & OBJ_SWAP) == 0) 1951f425ab8eSKonstantin Belousov goto next_obj; 1952f425ab8eSKonstantin Belousov 19537c022327SDoug Moore swap_pager_swapoff_object(sp, object); 1954f425ab8eSKonstantin Belousov next_obj: 1955f425ab8eSKonstantin Belousov VM_OBJECT_WUNLOCK(object); 1956f425ab8eSKonstantin Belousov mtx_lock(&vm_object_list_mtx); 195792da00bbSMatthew Dillon } 1958f425ab8eSKonstantin Belousov mtx_unlock(&vm_object_list_mtx); 1959f425ab8eSKonstantin Belousov 19608bc61209SDavid Schultz if (sp->sw_used) { 196192da00bbSMatthew Dillon /* 19628bc61209SDavid Schultz * Objects may be locked or paging to the device being 19638bc61209SDavid Schultz * removed, so we will miss their pages and need to 19648bc61209SDavid Schultz * make another pass. We have marked this device as 19658bc61209SDavid Schultz * SW_CLOSING, so the activity should finish soon. 196692da00bbSMatthew Dillon */ 19678bc61209SDavid Schultz retries++; 19688bc61209SDavid Schultz if (retries > 100) { 19698bc61209SDavid Schultz panic("swapoff: failed to locate %d swap blocks", 19708bc61209SDavid Schultz sp->sw_used); 19718bc61209SDavid Schultz } 19724d70511aSJohn Baldwin pause("swpoff", hz / 20); 197392da00bbSMatthew Dillon goto full_rescan; 197492da00bbSMatthew Dillon } 1975b1fd102eSMark Johnston EVENTHANDLER_INVOKE(swapoff, sp); 197692da00bbSMatthew Dillon } 197792da00bbSMatthew Dillon 19781c7c3c6aSMatthew Dillon /************************************************************************ 19791c7c3c6aSMatthew Dillon * SWAP META DATA * 19801c7c3c6aSMatthew Dillon ************************************************************************ 19811c7c3c6aSMatthew Dillon * 19821c7c3c6aSMatthew Dillon * These routines manipulate the swap metadata stored in the 1983cec9f109SDavid E. O'Brien * OBJT_SWAP object. 19841c7c3c6aSMatthew Dillon * 19854dcc5c2dSMatthew Dillon * Swap metadata is implemented with a global hash and not directly 19864dcc5c2dSMatthew Dillon * linked into the object. Instead the object simply contains 19874dcc5c2dSMatthew Dillon * appropriate tracking counters. 19881c7c3c6aSMatthew Dillon */ 19891c7c3c6aSMatthew Dillon 19901c7c3c6aSMatthew Dillon /* 1991230869e0SAlan Cox * SWP_PAGER_SWBLK_EMPTY() - is a range of blocks free? 1992230869e0SAlan Cox */ 1993230869e0SAlan Cox static bool 1994230869e0SAlan Cox swp_pager_swblk_empty(struct swblk *sb, int start, int limit) 1995230869e0SAlan Cox { 1996230869e0SAlan Cox int i; 1997230869e0SAlan Cox 1998230869e0SAlan Cox MPASS(0 <= start && start <= limit && limit <= SWAP_META_PAGES); 1999230869e0SAlan Cox for (i = start; i < limit; i++) { 2000230869e0SAlan Cox if (sb->d[i] != SWAPBLK_NONE) 2001230869e0SAlan Cox return (false); 2002230869e0SAlan Cox } 2003230869e0SAlan Cox return (true); 2004230869e0SAlan Cox } 2005230869e0SAlan Cox 2006230869e0SAlan Cox /* 2007abdab7b6SDoug Moore * SWP_PAGER_FREE_EMPTY_SWBLK() - frees if a block is free 2008abdab7b6SDoug Moore * 2009abdab7b6SDoug Moore * Nothing is done if the block is still in use. 2010abdab7b6SDoug Moore */ 2011abdab7b6SDoug Moore static void 2012abdab7b6SDoug Moore swp_pager_free_empty_swblk(vm_object_t object, struct swblk *sb) 2013abdab7b6SDoug Moore { 2014abdab7b6SDoug Moore 2015abdab7b6SDoug Moore if (swp_pager_swblk_empty(sb, 0, SWAP_META_PAGES)) { 2016abdab7b6SDoug Moore SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, sb->p); 2017abdab7b6SDoug Moore uma_zfree(swblk_zone, sb); 2018abdab7b6SDoug Moore } 2019abdab7b6SDoug Moore } 2020abdab7b6SDoug Moore 2021abdab7b6SDoug Moore /* 20221c7c3c6aSMatthew Dillon * SWP_PAGER_META_BUILD() - add swap block to swap meta data for object 20231c7c3c6aSMatthew Dillon * 20241c7c3c6aSMatthew Dillon * The specified swapblk is added to the object's swap metadata. If 20251c7c3c6aSMatthew Dillon * the swapblk is not valid, it is freed instead. Any previously 202678f1deefSAlan Cox * assigned swapblk is returned. 20271c7c3c6aSMatthew Dillon */ 202878f1deefSAlan Cox static daddr_t 20292f249180SPoul-Henning Kamp swp_pager_meta_build(vm_object_t object, vm_pindex_t pindex, daddr_t swapblk) 20302f249180SPoul-Henning Kamp { 2031f425ab8eSKonstantin Belousov static volatile int swblk_zone_exhausted, swpctrie_zone_exhausted; 2032eed99cb8SKonstantin Belousov struct swblk *sb, *sb1; 2033f425ab8eSKonstantin Belousov vm_pindex_t modpi, rdpi; 203478f1deefSAlan Cox daddr_t prev_swapblk; 2035f425ab8eSKonstantin Belousov int error, i; 20361c7c3c6aSMatthew Dillon 203789f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 2038f425ab8eSKonstantin Belousov 2039f425ab8eSKonstantin Belousov rdpi = rounddown(pindex, SWAP_META_PAGES); 2040f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, rdpi); 2041f425ab8eSKonstantin Belousov if (sb == NULL) { 20421c7c3c6aSMatthew Dillon if (swapblk == SWAPBLK_NONE) 204378f1deefSAlan Cox return (SWAPBLK_NONE); 2044f425ab8eSKonstantin Belousov for (;;) { 2045f425ab8eSKonstantin Belousov sb = uma_zalloc(swblk_zone, M_NOWAIT | (curproc == 2046f425ab8eSKonstantin Belousov pageproc ? M_USE_RESERVE : 0)); 2047f425ab8eSKonstantin Belousov if (sb != NULL) { 2048f425ab8eSKonstantin Belousov sb->p = rdpi; 2049f425ab8eSKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) 2050f425ab8eSKonstantin Belousov sb->d[i] = SWAPBLK_NONE; 2051f425ab8eSKonstantin Belousov if (atomic_cmpset_int(&swblk_zone_exhausted, 2052f425ab8eSKonstantin Belousov 1, 0)) 2053f425ab8eSKonstantin Belousov printf("swblk zone ok\n"); 2054f425ab8eSKonstantin Belousov break; 2055f425ab8eSKonstantin Belousov } 205689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 2057f425ab8eSKonstantin Belousov if (uma_zone_exhausted(swblk_zone)) { 2058f425ab8eSKonstantin Belousov if (atomic_cmpset_int(&swblk_zone_exhausted, 2059f425ab8eSKonstantin Belousov 0, 1)) 2060f425ab8eSKonstantin Belousov printf("swap blk zone exhausted, " 20613ff863f1SDag-Erling Smørgrav "increase kern.maxswzone\n"); 20622025d69bSKonstantin Belousov vm_pageout_oom(VM_OOM_SWAPZ); 2063f425ab8eSKonstantin Belousov pause("swzonxb", 10); 20642025d69bSKonstantin Belousov } else 20658d6fbbb8SJeff Roberson uma_zwait(swblk_zone); 206689f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 2067eed99cb8SKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, 2068eed99cb8SKonstantin Belousov rdpi); 2069eed99cb8SKonstantin Belousov if (sb != NULL) 2070eed99cb8SKonstantin Belousov /* 2071eed99cb8SKonstantin Belousov * Somebody swapped out a nearby page, 2072eed99cb8SKonstantin Belousov * allocating swblk at the rdpi index, 2073eed99cb8SKonstantin Belousov * while we dropped the object lock. 2074eed99cb8SKonstantin Belousov */ 2075eed99cb8SKonstantin Belousov goto allocated; 20764dcc5c2dSMatthew Dillon } 2077f425ab8eSKonstantin Belousov for (;;) { 2078f425ab8eSKonstantin Belousov error = SWAP_PCTRIE_INSERT( 2079f425ab8eSKonstantin Belousov &object->un_pager.swp.swp_blks, sb); 2080f425ab8eSKonstantin Belousov if (error == 0) { 2081f425ab8eSKonstantin Belousov if (atomic_cmpset_int(&swpctrie_zone_exhausted, 2082f425ab8eSKonstantin Belousov 1, 0)) 2083f425ab8eSKonstantin Belousov printf("swpctrie zone ok\n"); 2084f425ab8eSKonstantin Belousov break; 20851c7c3c6aSMatthew Dillon } 2086f425ab8eSKonstantin Belousov VM_OBJECT_WUNLOCK(object); 2087f425ab8eSKonstantin Belousov if (uma_zone_exhausted(swpctrie_zone)) { 2088f425ab8eSKonstantin Belousov if (atomic_cmpset_int(&swpctrie_zone_exhausted, 2089f425ab8eSKonstantin Belousov 0, 1)) 2090f425ab8eSKonstantin Belousov printf("swap pctrie zone exhausted, " 2091f425ab8eSKonstantin Belousov "increase kern.maxswzone\n"); 2092f425ab8eSKonstantin Belousov vm_pageout_oom(VM_OOM_SWAPZ); 2093f425ab8eSKonstantin Belousov pause("swzonxp", 10); 2094f425ab8eSKonstantin Belousov } else 20958d6fbbb8SJeff Roberson uma_zwait(swpctrie_zone); 2096f425ab8eSKonstantin Belousov VM_OBJECT_WLOCK(object); 2097eed99cb8SKonstantin Belousov sb1 = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, 2098eed99cb8SKonstantin Belousov rdpi); 2099eed99cb8SKonstantin Belousov if (sb1 != NULL) { 2100eed99cb8SKonstantin Belousov uma_zfree(swblk_zone, sb); 2101eed99cb8SKonstantin Belousov sb = sb1; 2102eed99cb8SKonstantin Belousov goto allocated; 21031c7c3c6aSMatthew Dillon } 2104f425ab8eSKonstantin Belousov } 2105eed99cb8SKonstantin Belousov } 2106eed99cb8SKonstantin Belousov allocated: 2107f425ab8eSKonstantin Belousov MPASS(sb->p == rdpi); 21081c7c3c6aSMatthew Dillon 2109f425ab8eSKonstantin Belousov modpi = pindex % SWAP_META_PAGES; 211078f1deefSAlan Cox /* Return prior contents of metadata. */ 211178f1deefSAlan Cox prev_swapblk = sb->d[modpi]; 2112f425ab8eSKonstantin Belousov /* Enter block into metadata. */ 2113f425ab8eSKonstantin Belousov sb->d[modpi] = swapblk; 211485d88d87SKonstantin Belousov 211585d88d87SKonstantin Belousov /* 211685d88d87SKonstantin Belousov * Free the swblk if we end up with the empty page run. 211785d88d87SKonstantin Belousov */ 2118abdab7b6SDoug Moore if (swapblk == SWAPBLK_NONE) 2119abdab7b6SDoug Moore swp_pager_free_empty_swblk(object, sb); 212078f1deefSAlan Cox return (prev_swapblk); 212185d88d87SKonstantin Belousov } 21221c7c3c6aSMatthew Dillon 21231c7c3c6aSMatthew Dillon /* 2124467057fcSDoug Moore * SWP_PAGER_META_TRANSFER() - free a range of blocks in the srcobject's swap 2125467057fcSDoug Moore * metadata, or transfer it into dstobject. 2126467057fcSDoug Moore * 2127467057fcSDoug Moore * This routine will free swap metadata structures as they are cleaned 2128467057fcSDoug Moore * out. 2129467057fcSDoug Moore */ 2130467057fcSDoug Moore static void 2131467057fcSDoug Moore swp_pager_meta_transfer(vm_object_t srcobject, vm_object_t dstobject, 2132baa1ccceSKonstantin Belousov vm_pindex_t pindex, vm_pindex_t count, vm_size_t *moved) 2133467057fcSDoug Moore { 2134467057fcSDoug Moore struct swblk *sb; 2135baa1ccceSKonstantin Belousov vm_page_t m; 2136467057fcSDoug Moore daddr_t n_free, s_free; 2137467057fcSDoug Moore vm_pindex_t offset, last; 2138baa1ccceSKonstantin Belousov vm_size_t mc; 2139467057fcSDoug Moore int i, limit, start; 2140467057fcSDoug Moore 2141467057fcSDoug Moore VM_OBJECT_ASSERT_WLOCKED(srcobject); 2142baa1ccceSKonstantin Belousov MPASS(moved == NULL || dstobject == NULL); 2143baa1ccceSKonstantin Belousov 2144baa1ccceSKonstantin Belousov mc = 0; 2145baa1ccceSKonstantin Belousov m = NULL; 2146cb6757c0SMark Johnston if (count == 0 || pctrie_is_empty(&srcobject->un_pager.swp.swp_blks)) 2147baa1ccceSKonstantin Belousov goto out; 2148467057fcSDoug Moore 2149467057fcSDoug Moore swp_pager_init_freerange(&s_free, &n_free); 2150467057fcSDoug Moore offset = pindex; 2151467057fcSDoug Moore last = pindex + count; 2152467057fcSDoug Moore for (;;) { 2153467057fcSDoug Moore sb = SWAP_PCTRIE_LOOKUP_GE(&srcobject->un_pager.swp.swp_blks, 2154467057fcSDoug Moore rounddown(pindex, SWAP_META_PAGES)); 2155467057fcSDoug Moore if (sb == NULL || sb->p >= last) 2156467057fcSDoug Moore break; 2157467057fcSDoug Moore start = pindex > sb->p ? pindex - sb->p : 0; 2158467057fcSDoug Moore limit = last - sb->p < SWAP_META_PAGES ? last - sb->p : 2159467057fcSDoug Moore SWAP_META_PAGES; 2160467057fcSDoug Moore for (i = start; i < limit; i++) { 2161467057fcSDoug Moore if (sb->d[i] == SWAPBLK_NONE) 2162467057fcSDoug Moore continue; 2163467057fcSDoug Moore if (dstobject == NULL || 2164467057fcSDoug Moore !swp_pager_xfer_source(srcobject, dstobject, 2165467057fcSDoug Moore sb->p + i - offset, sb->d[i])) { 2166467057fcSDoug Moore swp_pager_update_freerange(&s_free, &n_free, 2167467057fcSDoug Moore sb->d[i]); 2168467057fcSDoug Moore } 2169baa1ccceSKonstantin Belousov if (moved != NULL) { 2170baa1ccceSKonstantin Belousov if (m != NULL && m->pindex != pindex + i - 1) 2171baa1ccceSKonstantin Belousov m = NULL; 2172baa1ccceSKonstantin Belousov m = m != NULL ? vm_page_next(m) : 2173baa1ccceSKonstantin Belousov vm_page_lookup(srcobject, pindex + i); 2174baa1ccceSKonstantin Belousov if (m == NULL || vm_page_none_valid(m)) 2175baa1ccceSKonstantin Belousov mc++; 2176baa1ccceSKonstantin Belousov } 2177467057fcSDoug Moore sb->d[i] = SWAPBLK_NONE; 2178467057fcSDoug Moore } 2179467057fcSDoug Moore pindex = sb->p + SWAP_META_PAGES; 2180467057fcSDoug Moore if (swp_pager_swblk_empty(sb, 0, start) && 2181467057fcSDoug Moore swp_pager_swblk_empty(sb, limit, SWAP_META_PAGES)) { 2182467057fcSDoug Moore SWAP_PCTRIE_REMOVE(&srcobject->un_pager.swp.swp_blks, 2183467057fcSDoug Moore sb->p); 2184467057fcSDoug Moore uma_zfree(swblk_zone, sb); 2185467057fcSDoug Moore } 2186467057fcSDoug Moore } 2187467057fcSDoug Moore swp_pager_freeswapspace(s_free, n_free); 2188baa1ccceSKonstantin Belousov out: 2189baa1ccceSKonstantin Belousov if (moved != NULL) 2190baa1ccceSKonstantin Belousov *moved = mc; 2191467057fcSDoug Moore } 2192467057fcSDoug Moore 2193467057fcSDoug Moore /* 21941c7c3c6aSMatthew Dillon * SWP_PAGER_META_FREE() - free a range of blocks in the object's swap metadata 21951c7c3c6aSMatthew Dillon * 21961c7c3c6aSMatthew Dillon * The requested range of blocks is freed, with any associated swap 21971c7c3c6aSMatthew Dillon * returned to the swap bitmap. 21981c7c3c6aSMatthew Dillon * 21991c7c3c6aSMatthew Dillon * This routine will free swap metadata structures as they are cleaned 22001c7c3c6aSMatthew Dillon * out. This routine does *NOT* operate on swap metadata associated 22011c7c3c6aSMatthew Dillon * with resident pages. 22021c7c3c6aSMatthew Dillon */ 22031c7c3c6aSMatthew Dillon static void 2204baa1ccceSKonstantin Belousov swp_pager_meta_free(vm_object_t object, vm_pindex_t pindex, vm_pindex_t count, 2205baa1ccceSKonstantin Belousov vm_size_t *freed) 22061c7c3c6aSMatthew Dillon { 2207baa1ccceSKonstantin Belousov swp_pager_meta_transfer(object, NULL, pindex, count, freed); 22081c7c3c6aSMatthew Dillon } 22091c7c3c6aSMatthew Dillon 22101c7c3c6aSMatthew Dillon /* 22111c7c3c6aSMatthew Dillon * SWP_PAGER_META_FREE_ALL() - destroy all swap metadata associated with object 22121c7c3c6aSMatthew Dillon * 22131c7c3c6aSMatthew Dillon * This routine locates and destroys all swap metadata associated with 22141c7c3c6aSMatthew Dillon * an object. 22151c7c3c6aSMatthew Dillon */ 22161c7c3c6aSMatthew Dillon static void 22171c7c3c6aSMatthew Dillon swp_pager_meta_free_all(vm_object_t object) 22181c7c3c6aSMatthew Dillon { 2219f425ab8eSKonstantin Belousov struct swblk *sb; 222078f1deefSAlan Cox daddr_t n_free, s_free; 2221f425ab8eSKonstantin Belousov vm_pindex_t pindex; 222271057cd2SKonstantin Belousov int i; 22231c7c3c6aSMatthew Dillon 222489f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 2225cb6757c0SMark Johnston 2226cb6757c0SMark Johnston if (pctrie_is_empty(&object->un_pager.swp.swp_blks)) 22271c7c3c6aSMatthew Dillon return; 22281c7c3c6aSMatthew Dillon 222978f1deefSAlan Cox swp_pager_init_freerange(&s_free, &n_free); 2230f425ab8eSKonstantin Belousov for (pindex = 0; (sb = SWAP_PCTRIE_LOOKUP_GE( 2231f425ab8eSKonstantin Belousov &object->un_pager.swp.swp_blks, pindex)) != NULL;) { 2232f425ab8eSKonstantin Belousov pindex = sb->p + SWAP_META_PAGES; 2233f425ab8eSKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) { 2234230869e0SAlan Cox if (sb->d[i] == SWAPBLK_NONE) 2235230869e0SAlan Cox continue; 223678f1deefSAlan Cox swp_pager_update_freerange(&s_free, &n_free, sb->d[i]); 22371c7c3c6aSMatthew Dillon } 2238f425ab8eSKonstantin Belousov SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, sb->p); 2239f425ab8eSKonstantin Belousov uma_zfree(swblk_zone, sb); 22401c7c3c6aSMatthew Dillon } 224178f1deefSAlan Cox swp_pager_freeswapspace(s_free, n_free); 22421c7c3c6aSMatthew Dillon } 22431c7c3c6aSMatthew Dillon 22441c7c3c6aSMatthew Dillon /* 22454abca9bbSAlan Cox * SWP_PAGER_METACTL() - misc control of swap meta data. 22461c7c3c6aSMatthew Dillon * 22474abca9bbSAlan Cox * This routine is capable of looking up, or removing swapblk 22484abca9bbSAlan Cox * assignments in the swap meta data. It returns the swapblk being 22494abca9bbSAlan Cox * looked-up, popped, or SWAPBLK_NONE if the block was invalid. 22501c7c3c6aSMatthew Dillon * 22511c7c3c6aSMatthew Dillon * When acting on a busy resident page and paging is in progress, we 22521c7c3c6aSMatthew Dillon * have to wait until paging is complete but otherwise can act on the 22531c7c3c6aSMatthew Dillon * busy page. 22541c7c3c6aSMatthew Dillon */ 22551c7c3c6aSMatthew Dillon static daddr_t 22568ecbf14bSDoug Moore swp_pager_meta_lookup(vm_object_t object, vm_pindex_t pindex) 22572f249180SPoul-Henning Kamp { 2258f425ab8eSKonstantin Belousov struct swblk *sb; 22594dcc5c2dSMatthew Dillon 2260c25673ffSAttilio Rao VM_OBJECT_ASSERT_LOCKED(object); 2261ee620ea4SAlan Cox 22621c7c3c6aSMatthew Dillon /* 2263ee620ea4SAlan Cox * The meta data only exists if the object is OBJT_SWAP 22641c7c3c6aSMatthew Dillon * and even then might not be allocated yet. 22651c7c3c6aSMatthew Dillon */ 22664b8365d7SKonstantin Belousov KASSERT((object->flags & OBJ_SWAP) != 0, 22678ecbf14bSDoug Moore ("Lookup object not swappable")); 22681c7c3c6aSMatthew Dillon 2269f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, 2270f425ab8eSKonstantin Belousov rounddown(pindex, SWAP_META_PAGES)); 2271f425ab8eSKonstantin Belousov if (sb == NULL) 2272f425ab8eSKonstantin Belousov return (SWAPBLK_NONE); 22738ecbf14bSDoug Moore return (sb->d[pindex % SWAP_META_PAGES]); 22741c7c3c6aSMatthew Dillon } 22751c7c3c6aSMatthew Dillon 2276e9c0cc15SPoul-Henning Kamp /* 227777d6fd97SKonstantin Belousov * Returns the least page index which is greater than or equal to the 227877d6fd97SKonstantin Belousov * parameter pindex and for which there is a swap block allocated. 227977d6fd97SKonstantin Belousov * Returns object's size if the object's type is not swap or if there 228077d6fd97SKonstantin Belousov * are no allocated swap blocks for the object after the requested 228177d6fd97SKonstantin Belousov * pindex. 228277d6fd97SKonstantin Belousov */ 228377d6fd97SKonstantin Belousov vm_pindex_t 228477d6fd97SKonstantin Belousov swap_pager_find_least(vm_object_t object, vm_pindex_t pindex) 228577d6fd97SKonstantin Belousov { 2286f425ab8eSKonstantin Belousov struct swblk *sb; 2287f425ab8eSKonstantin Belousov int i; 228877d6fd97SKonstantin Belousov 228977d6fd97SKonstantin Belousov VM_OBJECT_ASSERT_LOCKED(object); 22905bd45b2bSKonstantin Belousov MPASS((object->flags & OBJ_SWAP) != 0); 229177d6fd97SKonstantin Belousov 2292cb6757c0SMark Johnston if (pctrie_is_empty(&object->un_pager.swp.swp_blks)) 2293cb6757c0SMark Johnston return (object->size); 2294f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks, 2295f425ab8eSKonstantin Belousov rounddown(pindex, SWAP_META_PAGES)); 2296f425ab8eSKonstantin Belousov if (sb == NULL) 2297f425ab8eSKonstantin Belousov return (object->size); 2298f425ab8eSKonstantin Belousov if (sb->p < pindex) { 2299f425ab8eSKonstantin Belousov for (i = pindex % SWAP_META_PAGES; i < SWAP_META_PAGES; i++) { 2300f425ab8eSKonstantin Belousov if (sb->d[i] != SWAPBLK_NONE) 2301f425ab8eSKonstantin Belousov return (sb->p + i); 230277d6fd97SKonstantin Belousov } 2303f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks, 2304f425ab8eSKonstantin Belousov roundup(pindex, SWAP_META_PAGES)); 2305f425ab8eSKonstantin Belousov if (sb == NULL) 2306f425ab8eSKonstantin Belousov return (object->size); 230777d6fd97SKonstantin Belousov } 2308f425ab8eSKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) { 2309f425ab8eSKonstantin Belousov if (sb->d[i] != SWAPBLK_NONE) 2310f425ab8eSKonstantin Belousov return (sb->p + i); 231177d6fd97SKonstantin Belousov } 2312f425ab8eSKonstantin Belousov 2313f425ab8eSKonstantin Belousov /* 2314f425ab8eSKonstantin Belousov * We get here if a swblk is present in the trie but it 2315f425ab8eSKonstantin Belousov * doesn't map any blocks. 2316f425ab8eSKonstantin Belousov */ 2317f425ab8eSKonstantin Belousov MPASS(0); 2318f425ab8eSKonstantin Belousov return (object->size); 231977d6fd97SKonstantin Belousov } 232077d6fd97SKonstantin Belousov 232177d6fd97SKonstantin Belousov /* 2322e9c0cc15SPoul-Henning Kamp * System call swapon(name) enables swapping on device name, 2323e9c0cc15SPoul-Henning Kamp * which must be in the swdevsw. Return EBUSY 2324e9c0cc15SPoul-Henning Kamp * if already swapping on this device. 2325e9c0cc15SPoul-Henning Kamp */ 2326e9c0cc15SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_ 2327e9c0cc15SPoul-Henning Kamp struct swapon_args { 2328e9c0cc15SPoul-Henning Kamp char *name; 2329e9c0cc15SPoul-Henning Kamp }; 2330e9c0cc15SPoul-Henning Kamp #endif 2331e9c0cc15SPoul-Henning Kamp 2332e9c0cc15SPoul-Henning Kamp int 23338451d0ddSKip Macy sys_swapon(struct thread *td, struct swapon_args *uap) 2334e9c0cc15SPoul-Henning Kamp { 2335e9c0cc15SPoul-Henning Kamp struct vattr attr; 2336e9c0cc15SPoul-Henning Kamp struct vnode *vp; 2337e9c0cc15SPoul-Henning Kamp struct nameidata nd; 2338e9c0cc15SPoul-Henning Kamp int error; 2339e9c0cc15SPoul-Henning Kamp 2340acd3428bSRobert Watson error = priv_check(td, PRIV_SWAPON); 2341e9c0cc15SPoul-Henning Kamp if (error) 2342acd3428bSRobert Watson return (error); 2343e9c0cc15SPoul-Henning Kamp 234404533e1eSKonstantin Belousov sx_xlock(&swdev_syscall_lock); 2345e9c0cc15SPoul-Henning Kamp 2346e9c0cc15SPoul-Henning Kamp /* 2347e9c0cc15SPoul-Henning Kamp * Swap metadata may not fit in the KVM if we have physical 2348e9c0cc15SPoul-Henning Kamp * memory of >1GB. 2349e9c0cc15SPoul-Henning Kamp */ 2350f425ab8eSKonstantin Belousov if (swblk_zone == NULL) { 2351e9c0cc15SPoul-Henning Kamp error = ENOMEM; 2352e9c0cc15SPoul-Henning Kamp goto done; 2353e9c0cc15SPoul-Henning Kamp } 2354e9c0cc15SPoul-Henning Kamp 23556ddf41faSKonstantin Belousov NDINIT(&nd, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | AUDITVNODE1, 23567e1d3eefSMateusz Guzik UIO_USERSPACE, uap->name); 2357e9c0cc15SPoul-Henning Kamp error = namei(&nd); 2358e9c0cc15SPoul-Henning Kamp if (error) 2359e9c0cc15SPoul-Henning Kamp goto done; 2360e9c0cc15SPoul-Henning Kamp 2361bb92cd7bSMateusz Guzik NDFREE_PNBUF(&nd); 2362e9c0cc15SPoul-Henning Kamp vp = nd.ni_vp; 2363e9c0cc15SPoul-Henning Kamp 23647ad2a82dSMateusz Guzik if (vn_isdisk_error(vp, &error)) { 236588ad2d7bSKonstantin Belousov error = swapongeom(vp); 236620da9c2eSPoul-Henning Kamp } else if (vp->v_type == VREG && 2367e9c0cc15SPoul-Henning Kamp (vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 && 23680359a12eSAttilio Rao (error = VOP_GETATTR(vp, &attr, td->td_ucred)) == 0) { 2369e9c0cc15SPoul-Henning Kamp /* 2370e9c0cc15SPoul-Henning Kamp * Allow direct swapping to NFS regular files in the same 2371e9c0cc15SPoul-Henning Kamp * way that nfs_mountroot() sets up diskless swapping. 2372e9c0cc15SPoul-Henning Kamp */ 237359efee01SPoul-Henning Kamp error = swaponvp(td, vp, attr.va_size / DEV_BSIZE); 2374e9c0cc15SPoul-Henning Kamp } 2375e9c0cc15SPoul-Henning Kamp 23766ddf41faSKonstantin Belousov if (error != 0) 23776ddf41faSKonstantin Belousov vput(vp); 23786ddf41faSKonstantin Belousov else 23796ddf41faSKonstantin Belousov VOP_UNLOCK(vp); 2380e9c0cc15SPoul-Henning Kamp done: 238104533e1eSKonstantin Belousov sx_xunlock(&swdev_syscall_lock); 2382e9c0cc15SPoul-Henning Kamp return (error); 2383e9c0cc15SPoul-Henning Kamp } 2384e9c0cc15SPoul-Henning Kamp 23853ff863f1SDag-Erling Smørgrav /* 23863ff863f1SDag-Erling Smørgrav * Check that the total amount of swap currently configured does not 23873ff863f1SDag-Erling Smørgrav * exceed half the theoretical maximum. If it does, print a warning 238835872e79SKonstantin Belousov * message. 23893ff863f1SDag-Erling Smørgrav */ 239035872e79SKonstantin Belousov static void 239135872e79SKonstantin Belousov swapon_check_swzone(void) 23923ff863f1SDag-Erling Smørgrav { 23933ff863f1SDag-Erling Smørgrav 23943ff863f1SDag-Erling Smørgrav /* recommend using no more than half that amount */ 2395303fa05aSKonstantin Belousov if (swap_total > swap_maxpages / 2) { 23963ff863f1SDag-Erling Smørgrav printf("warning: total configured swap (%lu pages) " 23973ff863f1SDag-Erling Smørgrav "exceeds maximum recommended amount (%lu pages).\n", 2398303fa05aSKonstantin Belousov swap_total, swap_maxpages / 2); 23993ff863f1SDag-Erling Smørgrav printf("warning: increase kern.maxswzone " 24003ff863f1SDag-Erling Smørgrav "or reduce amount of swap.\n"); 24013ff863f1SDag-Erling Smørgrav } 24023ff863f1SDag-Erling Smørgrav } 24033ff863f1SDag-Erling Smørgrav 240459efee01SPoul-Henning Kamp static void 24052cc718a1SKonstantin Belousov swaponsomething(struct vnode *vp, void *id, u_long nblks, 24062cc718a1SKonstantin Belousov sw_strategy_t *strategy, sw_close_t *close, dev_t dev, int flags) 2407e9c0cc15SPoul-Henning Kamp { 24082d9974c1SAlan Cox struct swdevt *sp, *tsp; 240934e2051fSMark Johnston daddr_t dvbase; 2410e9c0cc15SPoul-Henning Kamp 2411e9c0cc15SPoul-Henning Kamp /* 2412e9c0cc15SPoul-Henning Kamp * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks. 2413e9c0cc15SPoul-Henning Kamp * First chop nblks off to page-align it, then convert. 2414e9c0cc15SPoul-Henning Kamp * 2415e9c0cc15SPoul-Henning Kamp * sw->sw_nblks is in page-sized chunks now too. 2416e9c0cc15SPoul-Henning Kamp */ 2417e9c0cc15SPoul-Henning Kamp nblks &= ~(ctodb(1) - 1); 2418e9c0cc15SPoul-Henning Kamp nblks = dbtoc(nblks); 2419e9c0cc15SPoul-Henning Kamp 24208f60c087SPoul-Henning Kamp sp = malloc(sizeof *sp, M_VMPGDATA, M_WAITOK | M_ZERO); 242100fd73d2SDoug Moore sp->sw_blist = blist_create(nblks, M_WAITOK); 2422dee34ca4SPoul-Henning Kamp sp->sw_vp = vp; 2423dee34ca4SPoul-Henning Kamp sp->sw_id = id; 2424f3732fd1SPoul-Henning Kamp sp->sw_dev = dev; 2425e9c0cc15SPoul-Henning Kamp sp->sw_nblks = nblks; 2426e9c0cc15SPoul-Henning Kamp sp->sw_used = 0; 242759efee01SPoul-Henning Kamp sp->sw_strategy = strategy; 2428dee34ca4SPoul-Henning Kamp sp->sw_close = close; 24292cc718a1SKonstantin Belousov sp->sw_flags = flags; 2430e9c0cc15SPoul-Henning Kamp 2431e9c0cc15SPoul-Henning Kamp /* 2432504f5e29SDoug Moore * Do not free the first blocks in order to avoid overwriting 24338f60c087SPoul-Henning Kamp * any bsd label at the front of the partition 2434e9c0cc15SPoul-Henning Kamp */ 2435504f5e29SDoug Moore blist_free(sp->sw_blist, howmany(BBSIZE, PAGE_SIZE), 2436504f5e29SDoug Moore nblks - howmany(BBSIZE, PAGE_SIZE)); 2437e9c0cc15SPoul-Henning Kamp 24382d9974c1SAlan Cox dvbase = 0; 243920da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 24402d9974c1SAlan Cox TAILQ_FOREACH(tsp, &swtailq, sw_list) { 24412d9974c1SAlan Cox if (tsp->sw_end >= dvbase) { 24422d9974c1SAlan Cox /* 24432d9974c1SAlan Cox * We put one uncovered page between the devices 24442d9974c1SAlan Cox * in order to definitively prevent any cross-device 24452d9974c1SAlan Cox * I/O requests 24462d9974c1SAlan Cox */ 24472d9974c1SAlan Cox dvbase = tsp->sw_end + 1; 24482d9974c1SAlan Cox } 24492d9974c1SAlan Cox } 24502d9974c1SAlan Cox sp->sw_first = dvbase; 24512d9974c1SAlan Cox sp->sw_end = dvbase + nblks; 24528f60c087SPoul-Henning Kamp TAILQ_INSERT_TAIL(&swtailq, sp, sw_list); 24538f60c087SPoul-Henning Kamp nswapdev++; 2454504f5e29SDoug Moore swap_pager_avail += nblks - howmany(BBSIZE, PAGE_SIZE); 2455e8bb589dSMatt Macy swap_total += nblks; 245635872e79SKonstantin Belousov swapon_check_swzone(); 2457d05bc129SAlan Cox swp_sizecheck(); 2458d05bc129SAlan Cox mtx_unlock(&sw_dev_mtx); 2459b1fd102eSMark Johnston EVENTHANDLER_INVOKE(swapon, sp); 246059efee01SPoul-Henning Kamp } 2461e9c0cc15SPoul-Henning Kamp 2462e9c0cc15SPoul-Henning Kamp /* 2463e9c0cc15SPoul-Henning Kamp * SYSCALL: swapoff(devname) 2464e9c0cc15SPoul-Henning Kamp * 2465e9c0cc15SPoul-Henning Kamp * Disable swapping on the given device. 2466dee34ca4SPoul-Henning Kamp * 2467dee34ca4SPoul-Henning Kamp * XXX: Badly designed system call: it should use a device index 2468dee34ca4SPoul-Henning Kamp * rather than filename as specification. We keep sw_vp around 2469dee34ca4SPoul-Henning Kamp * only to make this work. 2470e9c0cc15SPoul-Henning Kamp */ 247153465702SKonstantin Belousov static int 247253465702SKonstantin Belousov kern_swapoff(struct thread *td, const char *name, enum uio_seg name_seg, 247353465702SKonstantin Belousov u_int flags) 2474e9c0cc15SPoul-Henning Kamp { 2475e9c0cc15SPoul-Henning Kamp struct vnode *vp; 2476e9c0cc15SPoul-Henning Kamp struct nameidata nd; 2477e9c0cc15SPoul-Henning Kamp struct swdevt *sp; 247853465702SKonstantin Belousov int error; 2479e9c0cc15SPoul-Henning Kamp 2480acd3428bSRobert Watson error = priv_check(td, PRIV_SWAPOFF); 2481a4e4132fSKonstantin Belousov if (error != 0) 2482a4e4132fSKonstantin Belousov return (error); 248353465702SKonstantin Belousov if ((flags & ~(SWAPOFF_FORCE)) != 0) 2484a4e4132fSKonstantin Belousov return (EINVAL); 2485a4e4132fSKonstantin Belousov 248604533e1eSKonstantin Belousov sx_xlock(&swdev_syscall_lock); 2487e9c0cc15SPoul-Henning Kamp 248853465702SKonstantin Belousov NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, name_seg, name); 2489e9c0cc15SPoul-Henning Kamp error = namei(&nd); 2490e9c0cc15SPoul-Henning Kamp if (error) 2491e9c0cc15SPoul-Henning Kamp goto done; 2492bb92cd7bSMateusz Guzik NDFREE_PNBUF(&nd); 2493e9c0cc15SPoul-Henning Kamp vp = nd.ni_vp; 2494e9c0cc15SPoul-Henning Kamp 249520da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 24968f60c087SPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 2497dee34ca4SPoul-Henning Kamp if (sp->sw_vp == vp) 24980909f38aSPawel Jakub Dawidek break; 2499e9c0cc15SPoul-Henning Kamp } 250020da9c2eSPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 25010909f38aSPawel Jakub Dawidek if (sp == NULL) { 2502e9c0cc15SPoul-Henning Kamp error = EINVAL; 2503e9c0cc15SPoul-Henning Kamp goto done; 25040909f38aSPawel Jakub Dawidek } 250553465702SKonstantin Belousov error = swapoff_one(sp, td->td_ucred, flags); 25060909f38aSPawel Jakub Dawidek done: 250704533e1eSKonstantin Belousov sx_xunlock(&swdev_syscall_lock); 25080909f38aSPawel Jakub Dawidek return (error); 25090909f38aSPawel Jakub Dawidek } 25100909f38aSPawel Jakub Dawidek 251153465702SKonstantin Belousov 251253465702SKonstantin Belousov #ifdef COMPAT_FREEBSD13 251353465702SKonstantin Belousov int 251453465702SKonstantin Belousov freebsd13_swapoff(struct thread *td, struct freebsd13_swapoff_args *uap) 251553465702SKonstantin Belousov { 251653465702SKonstantin Belousov return (kern_swapoff(td, uap->name, UIO_USERSPACE, 0)); 251753465702SKonstantin Belousov } 251853465702SKonstantin Belousov #endif 251953465702SKonstantin Belousov 252053465702SKonstantin Belousov int 252153465702SKonstantin Belousov sys_swapoff(struct thread *td, struct swapoff_args *uap) 252253465702SKonstantin Belousov { 252353465702SKonstantin Belousov return (kern_swapoff(td, uap->name, UIO_USERSPACE, uap->flags)); 252453465702SKonstantin Belousov } 252553465702SKonstantin Belousov 25260909f38aSPawel Jakub Dawidek static int 2527e8dc2ba2SKonstantin Belousov swapoff_one(struct swdevt *sp, struct ucred *cred, u_int flags) 25280909f38aSPawel Jakub Dawidek { 252903bdd65fSAlan Cox u_long nblks; 2530e9c0cc15SPoul-Henning Kamp #ifdef MAC 25310909f38aSPawel Jakub Dawidek int error; 2532e9c0cc15SPoul-Henning Kamp #endif 2533e9c0cc15SPoul-Henning Kamp 253404533e1eSKonstantin Belousov sx_assert(&swdev_syscall_lock, SA_XLOCKED); 25350909f38aSPawel Jakub Dawidek #ifdef MAC 2536cb05b60aSAttilio Rao (void) vn_lock(sp->sw_vp, LK_EXCLUSIVE | LK_RETRY); 253735918c55SChristian S.J. Peron error = mac_system_check_swapoff(cred, sp->sw_vp); 2538b249ce48SMateusz Guzik (void) VOP_UNLOCK(sp->sw_vp); 25390909f38aSPawel Jakub Dawidek if (error != 0) 25400909f38aSPawel Jakub Dawidek return (error); 25410909f38aSPawel Jakub Dawidek #endif 2542e9c0cc15SPoul-Henning Kamp nblks = sp->sw_nblks; 2543e9c0cc15SPoul-Henning Kamp 2544e9c0cc15SPoul-Henning Kamp /* 2545e9c0cc15SPoul-Henning Kamp * We can turn off this swap device safely only if the 2546e9c0cc15SPoul-Henning Kamp * available virtual memory in the system will fit the amount 2547e9c0cc15SPoul-Henning Kamp * of data we will have to page back in, plus an epsilon so 2548e9c0cc15SPoul-Henning Kamp * the system doesn't become critically low on swap space. 25490190c38bSKonstantin Belousov * The vm_free_count() part does not account e.g. for clean 25500190c38bSKonstantin Belousov * pages that can be immediately reclaimed without paging, so 25510190c38bSKonstantin Belousov * this is a very rough estimation. 25520190c38bSKonstantin Belousov * 25530190c38bSKonstantin Belousov * On the other hand, not turning swap off on swapoff_all() 25540190c38bSKonstantin Belousov * means that we can lose swap data when filesystems go away, 25550190c38bSKonstantin Belousov * which is arguably worse. 2556e9c0cc15SPoul-Henning Kamp */ 2557e8dc2ba2SKonstantin Belousov if ((flags & SWAPOFF_FORCE) == 0 && 25580190c38bSKonstantin Belousov vm_free_count() + swap_pager_avail < nblks + nswap_lowat) 25590909f38aSPawel Jakub Dawidek return (ENOMEM); 2560e9c0cc15SPoul-Henning Kamp 2561e9c0cc15SPoul-Henning Kamp /* 2562e9c0cc15SPoul-Henning Kamp * Prevent further allocations on this device. 2563e9c0cc15SPoul-Henning Kamp */ 25642928cef7SAlan Cox mtx_lock(&sw_dev_mtx); 2565e9c0cc15SPoul-Henning Kamp sp->sw_flags |= SW_CLOSING; 256603bdd65fSAlan Cox swap_pager_avail -= blist_fill(sp->sw_blist, 0, nblks); 2567e8bb589dSMatt Macy swap_total -= nblks; 25682928cef7SAlan Cox mtx_unlock(&sw_dev_mtx); 2569e9c0cc15SPoul-Henning Kamp 2570e9c0cc15SPoul-Henning Kamp /* 2571e9c0cc15SPoul-Henning Kamp * Page in the contents of the device and close it. 2572e9c0cc15SPoul-Henning Kamp */ 2573b3fed13eSDavid Schultz swap_pager_swapoff(sp); 2574e9c0cc15SPoul-Henning Kamp 257535918c55SChristian S.J. Peron sp->sw_close(curthread, sp); 257620da9c2eSPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 25779e3e3fe5SWarner Losh sp->sw_id = NULL; 25788f60c087SPoul-Henning Kamp TAILQ_REMOVE(&swtailq, sp, sw_list); 25790676a140SAlan Cox nswapdev--; 25807dea2c2eSAlan Cox if (nswapdev == 0) { 25817dea2c2eSAlan Cox swap_pager_full = 2; 25827dea2c2eSAlan Cox swap_pager_almost_full = 1; 25837dea2c2eSAlan Cox } 25848f60c087SPoul-Henning Kamp if (swdevhd == sp) 25858f60c087SPoul-Henning Kamp swdevhd = NULL; 2586d05bc129SAlan Cox mtx_unlock(&sw_dev_mtx); 25878f60c087SPoul-Henning Kamp blist_destroy(sp->sw_blist); 25888f60c087SPoul-Henning Kamp free(sp, M_VMPGDATA); 25890909f38aSPawel Jakub Dawidek return (0); 25900909f38aSPawel Jakub Dawidek } 2591e9c0cc15SPoul-Henning Kamp 25920909f38aSPawel Jakub Dawidek void 25930909f38aSPawel Jakub Dawidek swapoff_all(void) 25940909f38aSPawel Jakub Dawidek { 25950909f38aSPawel Jakub Dawidek struct swdevt *sp, *spt; 25960909f38aSPawel Jakub Dawidek const char *devname; 25970909f38aSPawel Jakub Dawidek int error; 25980909f38aSPawel Jakub Dawidek 259904533e1eSKonstantin Belousov sx_xlock(&swdev_syscall_lock); 26000909f38aSPawel Jakub Dawidek 26010909f38aSPawel Jakub Dawidek mtx_lock(&sw_dev_mtx); 26020909f38aSPawel Jakub Dawidek TAILQ_FOREACH_SAFE(sp, &swtailq, sw_list, spt) { 26030909f38aSPawel Jakub Dawidek mtx_unlock(&sw_dev_mtx); 26047ad2a82dSMateusz Guzik if (vn_isdisk(sp->sw_vp)) 26057870adb6SEd Schouten devname = devtoname(sp->sw_vp->v_rdev); 26060909f38aSPawel Jakub Dawidek else 26070909f38aSPawel Jakub Dawidek devname = "[file]"; 2608e8dc2ba2SKonstantin Belousov error = swapoff_one(sp, thread0.td_ucred, SWAPOFF_FORCE); 26090909f38aSPawel Jakub Dawidek if (error != 0) { 26100909f38aSPawel Jakub Dawidek printf("Cannot remove swap device %s (error=%d), " 26110909f38aSPawel Jakub Dawidek "skipping.\n", devname, error); 26120909f38aSPawel Jakub Dawidek } else if (bootverbose) { 26130909f38aSPawel Jakub Dawidek printf("Swap device %s removed.\n", devname); 26140909f38aSPawel Jakub Dawidek } 26150909f38aSPawel Jakub Dawidek mtx_lock(&sw_dev_mtx); 26160909f38aSPawel Jakub Dawidek } 26170909f38aSPawel Jakub Dawidek mtx_unlock(&sw_dev_mtx); 26180909f38aSPawel Jakub Dawidek 261904533e1eSKonstantin Belousov sx_xunlock(&swdev_syscall_lock); 2620e9c0cc15SPoul-Henning Kamp } 2621e9c0cc15SPoul-Henning Kamp 2622567104a1SPoul-Henning Kamp void 2623567104a1SPoul-Henning Kamp swap_pager_status(int *total, int *used) 2624567104a1SPoul-Henning Kamp { 2625567104a1SPoul-Henning Kamp 26267ce3a312SMateusz Guzik *total = swap_total; 26277ce3a312SMateusz Guzik *used = swap_total - swap_pager_avail - 26287ce3a312SMateusz Guzik nswapdev * howmany(BBSIZE, PAGE_SIZE); 2629567104a1SPoul-Henning Kamp } 2630567104a1SPoul-Henning Kamp 2631dda4f960SKonstantin Belousov int 2632dda4f960SKonstantin Belousov swap_dev_info(int name, struct xswdev *xs, char *devname, size_t len) 2633dda4f960SKonstantin Belousov { 2634dda4f960SKonstantin Belousov struct swdevt *sp; 26357870adb6SEd Schouten const char *tmp_devname; 2636dda4f960SKonstantin Belousov int error, n; 2637dda4f960SKonstantin Belousov 2638dda4f960SKonstantin Belousov n = 0; 2639dda4f960SKonstantin Belousov error = ENOENT; 2640dda4f960SKonstantin Belousov mtx_lock(&sw_dev_mtx); 2641dda4f960SKonstantin Belousov TAILQ_FOREACH(sp, &swtailq, sw_list) { 2642dda4f960SKonstantin Belousov if (n != name) { 2643dda4f960SKonstantin Belousov n++; 2644dda4f960SKonstantin Belousov continue; 2645dda4f960SKonstantin Belousov } 2646dda4f960SKonstantin Belousov xs->xsw_version = XSWDEV_VERSION; 2647dda4f960SKonstantin Belousov xs->xsw_dev = sp->sw_dev; 2648dda4f960SKonstantin Belousov xs->xsw_flags = sp->sw_flags; 2649dda4f960SKonstantin Belousov xs->xsw_nblks = sp->sw_nblks; 2650dda4f960SKonstantin Belousov xs->xsw_used = sp->sw_used; 2651dda4f960SKonstantin Belousov if (devname != NULL) { 26527ad2a82dSMateusz Guzik if (vn_isdisk(sp->sw_vp)) 26537870adb6SEd Schouten tmp_devname = devtoname(sp->sw_vp->v_rdev); 2654dda4f960SKonstantin Belousov else 2655dda4f960SKonstantin Belousov tmp_devname = "[file]"; 2656dda4f960SKonstantin Belousov strncpy(devname, tmp_devname, len); 2657dda4f960SKonstantin Belousov } 2658dda4f960SKonstantin Belousov error = 0; 2659dda4f960SKonstantin Belousov break; 2660dda4f960SKonstantin Belousov } 2661dda4f960SKonstantin Belousov mtx_unlock(&sw_dev_mtx); 2662dda4f960SKonstantin Belousov return (error); 2663dda4f960SKonstantin Belousov } 2664dda4f960SKonstantin Belousov 266569921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11) 266669921123SKonstantin Belousov #define XSWDEV_VERSION_11 1 266769921123SKonstantin Belousov struct xswdev11 { 266869921123SKonstantin Belousov u_int xsw_version; 266969921123SKonstantin Belousov uint32_t xsw_dev; 267069921123SKonstantin Belousov int xsw_flags; 267169921123SKonstantin Belousov int xsw_nblks; 267269921123SKonstantin Belousov int xsw_used; 267369921123SKonstantin Belousov }; 267469921123SKonstantin Belousov #endif 267569921123SKonstantin Belousov 2676f6d281e8SKonstantin Belousov #if defined(__amd64__) && defined(COMPAT_FREEBSD32) 2677f6d281e8SKonstantin Belousov struct xswdev32 { 2678f6d281e8SKonstantin Belousov u_int xsw_version; 2679f6d281e8SKonstantin Belousov u_int xsw_dev1, xsw_dev2; 2680f6d281e8SKonstantin Belousov int xsw_flags; 2681f6d281e8SKonstantin Belousov int xsw_nblks; 2682f6d281e8SKonstantin Belousov int xsw_used; 2683f6d281e8SKonstantin Belousov }; 2684f6d281e8SKonstantin Belousov #endif 2685f6d281e8SKonstantin Belousov 2686e9c0cc15SPoul-Henning Kamp static int 2687e9c0cc15SPoul-Henning Kamp sysctl_vm_swap_info(SYSCTL_HANDLER_ARGS) 2688e9c0cc15SPoul-Henning Kamp { 2689e9c0cc15SPoul-Henning Kamp struct xswdev xs; 2690f6d281e8SKonstantin Belousov #if defined(__amd64__) && defined(COMPAT_FREEBSD32) 2691f6d281e8SKonstantin Belousov struct xswdev32 xs32; 2692f6d281e8SKonstantin Belousov #endif 269369921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11) 269469921123SKonstantin Belousov struct xswdev11 xs11; 269569921123SKonstantin Belousov #endif 2696dda4f960SKonstantin Belousov int error; 2697e9c0cc15SPoul-Henning Kamp 2698e9c0cc15SPoul-Henning Kamp if (arg2 != 1) /* name length */ 2699e9c0cc15SPoul-Henning Kamp return (EINVAL); 270006d1fd9fSMark Johnston 270106d1fd9fSMark Johnston memset(&xs, 0, sizeof(xs)); 2702dda4f960SKonstantin Belousov error = swap_dev_info(*(int *)arg1, &xs, NULL, 0); 2703dda4f960SKonstantin Belousov if (error != 0) 2704dda4f960SKonstantin Belousov return (error); 2705f6d281e8SKonstantin Belousov #if defined(__amd64__) && defined(COMPAT_FREEBSD32) 2706f6d281e8SKonstantin Belousov if (req->oldlen == sizeof(xs32)) { 270706d1fd9fSMark Johnston memset(&xs32, 0, sizeof(xs32)); 2708f6d281e8SKonstantin Belousov xs32.xsw_version = XSWDEV_VERSION; 2709f6d281e8SKonstantin Belousov xs32.xsw_dev1 = xs.xsw_dev; 2710f6d281e8SKonstantin Belousov xs32.xsw_dev2 = xs.xsw_dev >> 32; 2711f6d281e8SKonstantin Belousov xs32.xsw_flags = xs.xsw_flags; 2712f6d281e8SKonstantin Belousov xs32.xsw_nblks = xs.xsw_nblks; 2713f6d281e8SKonstantin Belousov xs32.xsw_used = xs.xsw_used; 2714f6d281e8SKonstantin Belousov error = SYSCTL_OUT(req, &xs32, sizeof(xs32)); 2715f6d281e8SKonstantin Belousov return (error); 2716f6d281e8SKonstantin Belousov } 2717f6d281e8SKonstantin Belousov #endif 271869921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11) 271969921123SKonstantin Belousov if (req->oldlen == sizeof(xs11)) { 272006d1fd9fSMark Johnston memset(&xs11, 0, sizeof(xs11)); 272169921123SKonstantin Belousov xs11.xsw_version = XSWDEV_VERSION_11; 272269921123SKonstantin Belousov xs11.xsw_dev = xs.xsw_dev; /* truncation */ 272369921123SKonstantin Belousov xs11.xsw_flags = xs.xsw_flags; 272469921123SKonstantin Belousov xs11.xsw_nblks = xs.xsw_nblks; 272569921123SKonstantin Belousov xs11.xsw_used = xs.xsw_used; 272669921123SKonstantin Belousov error = SYSCTL_OUT(req, &xs11, sizeof(xs11)); 2727f6d281e8SKonstantin Belousov return (error); 2728f6d281e8SKonstantin Belousov } 272969921123SKonstantin Belousov #endif 2730e9c0cc15SPoul-Henning Kamp error = SYSCTL_OUT(req, &xs, sizeof(xs)); 2731e9c0cc15SPoul-Henning Kamp return (error); 2732e9c0cc15SPoul-Henning Kamp } 2733e9c0cc15SPoul-Henning Kamp 27348f60c087SPoul-Henning Kamp SYSCTL_INT(_vm, OID_AUTO, nswapdev, CTLFLAG_RD, &nswapdev, 0, 2735e9c0cc15SPoul-Henning Kamp "Number of swap devices"); 27364c36e917SKonstantin Belousov SYSCTL_NODE(_vm, OID_AUTO, swap_info, CTLFLAG_RD | CTLFLAG_MPSAFE, 27374c36e917SKonstantin Belousov sysctl_vm_swap_info, 2738e9c0cc15SPoul-Henning Kamp "Swap statistics by device"); 2739ec38b344SPoul-Henning Kamp 2740ec38b344SPoul-Henning Kamp /* 2741f425ab8eSKonstantin Belousov * Count the approximate swap usage in pages for a vmspace. The 2742f425ab8eSKonstantin Belousov * shadowed or not yet copied on write swap blocks are not accounted. 2743ec38b344SPoul-Henning Kamp * The map must be locked. 2744ec38b344SPoul-Henning Kamp */ 27452860553aSRebecca Cran long 2746ec38b344SPoul-Henning Kamp vmspace_swap_count(struct vmspace *vmspace) 2747ec38b344SPoul-Henning Kamp { 274865d8409cSRebecca Cran vm_map_t map; 2749ec38b344SPoul-Henning Kamp vm_map_entry_t cur; 275065d8409cSRebecca Cran vm_object_t object; 2751f425ab8eSKonstantin Belousov struct swblk *sb; 2752f425ab8eSKonstantin Belousov vm_pindex_t e, pi; 2753f425ab8eSKonstantin Belousov long count; 2754f425ab8eSKonstantin Belousov int i; 275565d8409cSRebecca Cran 275665d8409cSRebecca Cran map = &vmspace->vm_map; 275765d8409cSRebecca Cran count = 0; 2758ec38b344SPoul-Henning Kamp 27592288078cSDoug Moore VM_MAP_ENTRY_FOREACH(cur, map) { 2760f425ab8eSKonstantin Belousov if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) != 0) 2761f425ab8eSKonstantin Belousov continue; 2762f425ab8eSKonstantin Belousov object = cur->object.vm_object; 27634b8365d7SKonstantin Belousov if (object == NULL || (object->flags & OBJ_SWAP) == 0) 2764f425ab8eSKonstantin Belousov continue; 2765f425ab8eSKonstantin Belousov VM_OBJECT_RLOCK(object); 27664b8365d7SKonstantin Belousov if ((object->flags & OBJ_SWAP) == 0) 2767f425ab8eSKonstantin Belousov goto unlock; 2768f425ab8eSKonstantin Belousov pi = OFF_TO_IDX(cur->offset); 2769f425ab8eSKonstantin Belousov e = pi + OFF_TO_IDX(cur->end - cur->start); 2770f425ab8eSKonstantin Belousov for (;; pi = sb->p + SWAP_META_PAGES) { 2771f425ab8eSKonstantin Belousov sb = SWAP_PCTRIE_LOOKUP_GE( 2772f425ab8eSKonstantin Belousov &object->un_pager.swp.swp_blks, pi); 2773f425ab8eSKonstantin Belousov if (sb == NULL || sb->p >= e) 2774f425ab8eSKonstantin Belousov break; 2775f425ab8eSKonstantin Belousov for (i = 0; i < SWAP_META_PAGES; i++) { 2776f425ab8eSKonstantin Belousov if (sb->p + i < e && 2777f425ab8eSKonstantin Belousov sb->d[i] != SWAPBLK_NONE) 2778f425ab8eSKonstantin Belousov count++; 2779ec38b344SPoul-Henning Kamp } 2780ec38b344SPoul-Henning Kamp } 2781f425ab8eSKonstantin Belousov unlock: 2782f425ab8eSKonstantin Belousov VM_OBJECT_RUNLOCK(object); 2783ec38b344SPoul-Henning Kamp } 2784ec38b344SPoul-Henning Kamp return (count); 2785ec38b344SPoul-Henning Kamp } 2786dee34ca4SPoul-Henning Kamp 2787dee34ca4SPoul-Henning Kamp /* 2788dee34ca4SPoul-Henning Kamp * GEOM backend 2789dee34ca4SPoul-Henning Kamp * 2790dee34ca4SPoul-Henning Kamp * Swapping onto disk devices. 2791dee34ca4SPoul-Henning Kamp * 2792dee34ca4SPoul-Henning Kamp */ 2793dee34ca4SPoul-Henning Kamp 27945721c9c7SPoul-Henning Kamp static g_orphan_t swapgeom_orphan; 27955721c9c7SPoul-Henning Kamp 2796dee34ca4SPoul-Henning Kamp static struct g_class g_swap_class = { 2797dee34ca4SPoul-Henning Kamp .name = "SWAP", 27985721c9c7SPoul-Henning Kamp .version = G_VERSION, 27995721c9c7SPoul-Henning Kamp .orphan = swapgeom_orphan, 2800dee34ca4SPoul-Henning Kamp }; 2801dee34ca4SPoul-Henning Kamp 2802dee34ca4SPoul-Henning Kamp DECLARE_GEOM_CLASS(g_swap_class, g_class); 2803dee34ca4SPoul-Henning Kamp 2804dee34ca4SPoul-Henning Kamp static void 28053398491bSAlexander Motin swapgeom_close_ev(void *arg, int flags) 28063398491bSAlexander Motin { 28073398491bSAlexander Motin struct g_consumer *cp; 28083398491bSAlexander Motin 28093398491bSAlexander Motin cp = arg; 28103398491bSAlexander Motin g_access(cp, -1, -1, 0); 28113398491bSAlexander Motin g_detach(cp); 28123398491bSAlexander Motin g_destroy_consumer(cp); 28133398491bSAlexander Motin } 28143398491bSAlexander Motin 28159e3e3fe5SWarner Losh /* 28169e3e3fe5SWarner Losh * Add a reference to the g_consumer for an inflight transaction. 28179e3e3fe5SWarner Losh */ 28189e3e3fe5SWarner Losh static void 28199e3e3fe5SWarner Losh swapgeom_acquire(struct g_consumer *cp) 28209e3e3fe5SWarner Losh { 28219e3e3fe5SWarner Losh 28229e3e3fe5SWarner Losh mtx_assert(&sw_dev_mtx, MA_OWNED); 28239e3e3fe5SWarner Losh cp->index++; 28249e3e3fe5SWarner Losh } 28259e3e3fe5SWarner Losh 28269e3e3fe5SWarner Losh /* 28270c657d22SKonstantin Belousov * Remove a reference from the g_consumer. Post a close event if all 28280c657d22SKonstantin Belousov * references go away, since the function might be called from the 28290c657d22SKonstantin Belousov * biodone context. 28309e3e3fe5SWarner Losh */ 28319e3e3fe5SWarner Losh static void 28329e3e3fe5SWarner Losh swapgeom_release(struct g_consumer *cp, struct swdevt *sp) 28339e3e3fe5SWarner Losh { 28349e3e3fe5SWarner Losh 28359e3e3fe5SWarner Losh mtx_assert(&sw_dev_mtx, MA_OWNED); 28369e3e3fe5SWarner Losh cp->index--; 28379e3e3fe5SWarner Losh if (cp->index == 0) { 28389e3e3fe5SWarner Losh if (g_post_event(swapgeom_close_ev, cp, M_NOWAIT, NULL) == 0) 28399e3e3fe5SWarner Losh sp->sw_id = NULL; 28409e3e3fe5SWarner Losh } 28419e3e3fe5SWarner Losh } 28429e3e3fe5SWarner Losh 28433398491bSAlexander Motin static void 2844dee34ca4SPoul-Henning Kamp swapgeom_done(struct bio *bp2) 2845dee34ca4SPoul-Henning Kamp { 28463398491bSAlexander Motin struct swdevt *sp; 2847dee34ca4SPoul-Henning Kamp struct buf *bp; 28483398491bSAlexander Motin struct g_consumer *cp; 2849dee34ca4SPoul-Henning Kamp 2850dee34ca4SPoul-Henning Kamp bp = bp2->bio_caller2; 28513398491bSAlexander Motin cp = bp2->bio_from; 2852c5d3d25eSPoul-Henning Kamp bp->b_ioflags = bp2->bio_flags; 2853dee34ca4SPoul-Henning Kamp if (bp2->bio_error) 2854dee34ca4SPoul-Henning Kamp bp->b_ioflags |= BIO_ERROR; 2855c5d3d25eSPoul-Henning Kamp bp->b_resid = bp->b_bcount - bp2->bio_completed; 2856c5d3d25eSPoul-Henning Kamp bp->b_error = bp2->bio_error; 2857756a5412SGleb Smirnoff bp->b_caller1 = NULL; 2858dee34ca4SPoul-Henning Kamp bufdone(bp); 28593398491bSAlexander Motin sp = bp2->bio_caller1; 28609e3e3fe5SWarner Losh mtx_lock(&sw_dev_mtx); 28619e3e3fe5SWarner Losh swapgeom_release(cp, sp); 28623398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 2863dee34ca4SPoul-Henning Kamp g_destroy_bio(bp2); 2864dee34ca4SPoul-Henning Kamp } 2865dee34ca4SPoul-Henning Kamp 2866dee34ca4SPoul-Henning Kamp static void 2867dee34ca4SPoul-Henning Kamp swapgeom_strategy(struct buf *bp, struct swdevt *sp) 2868dee34ca4SPoul-Henning Kamp { 2869dee34ca4SPoul-Henning Kamp struct bio *bio; 2870dee34ca4SPoul-Henning Kamp struct g_consumer *cp; 2871dee34ca4SPoul-Henning Kamp 28723398491bSAlexander Motin mtx_lock(&sw_dev_mtx); 2873dee34ca4SPoul-Henning Kamp cp = sp->sw_id; 2874dee34ca4SPoul-Henning Kamp if (cp == NULL) { 28753398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 2876dee34ca4SPoul-Henning Kamp bp->b_error = ENXIO; 2877dee34ca4SPoul-Henning Kamp bp->b_ioflags |= BIO_ERROR; 2878dee34ca4SPoul-Henning Kamp bufdone(bp); 2879dee34ca4SPoul-Henning Kamp return; 2880dee34ca4SPoul-Henning Kamp } 28819e3e3fe5SWarner Losh swapgeom_acquire(cp); 28823398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 288311041003SKonstantin Belousov if (bp->b_iocmd == BIO_WRITE) 288411041003SKonstantin Belousov bio = g_new_bio(); 288511041003SKonstantin Belousov else 28864f8205e5SPoul-Henning Kamp bio = g_alloc_bio(); 28874f8205e5SPoul-Henning Kamp if (bio == NULL) { 28889e3e3fe5SWarner Losh mtx_lock(&sw_dev_mtx); 28899e3e3fe5SWarner Losh swapgeom_release(cp, sp); 28909e3e3fe5SWarner Losh mtx_unlock(&sw_dev_mtx); 28913e5b6861SPoul-Henning Kamp bp->b_error = ENOMEM; 28923e5b6861SPoul-Henning Kamp bp->b_ioflags |= BIO_ERROR; 28930b208315SEdward Tomasz Napierala printf("swap_pager: cannot allocate bio\n"); 28943e5b6861SPoul-Henning Kamp bufdone(bp); 28953e5b6861SPoul-Henning Kamp return; 28963e5b6861SPoul-Henning Kamp } 289711041003SKonstantin Belousov 2898756a5412SGleb Smirnoff bp->b_caller1 = bio; 28993398491bSAlexander Motin bio->bio_caller1 = sp; 2900dee34ca4SPoul-Henning Kamp bio->bio_caller2 = bp; 2901c5d3d25eSPoul-Henning Kamp bio->bio_cmd = bp->b_iocmd; 2902dee34ca4SPoul-Henning Kamp bio->bio_offset = (bp->b_blkno - sp->sw_first) * PAGE_SIZE; 2903dee34ca4SPoul-Henning Kamp bio->bio_length = bp->b_bcount; 2904dee34ca4SPoul-Henning Kamp bio->bio_done = swapgeom_done; 2905c6213befSGleb Smirnoff bio->bio_flags |= BIO_SWAP; 2906fade8dd7SJeff Roberson if (!buf_mapped(bp)) { 29072cc718a1SKonstantin Belousov bio->bio_ma = bp->b_pages; 29082cc718a1SKonstantin Belousov bio->bio_data = unmapped_buf; 29092cc718a1SKonstantin Belousov bio->bio_ma_offset = (vm_offset_t)bp->b_offset & PAGE_MASK; 29102cc718a1SKonstantin Belousov bio->bio_ma_n = bp->b_npages; 29112cc718a1SKonstantin Belousov bio->bio_flags |= BIO_UNMAPPED; 29122cc718a1SKonstantin Belousov } else { 29132cc718a1SKonstantin Belousov bio->bio_data = bp->b_data; 29142cc718a1SKonstantin Belousov bio->bio_ma = NULL; 29152cc718a1SKonstantin Belousov } 2916dee34ca4SPoul-Henning Kamp g_io_request(bio, cp); 2917dee34ca4SPoul-Henning Kamp return; 2918dee34ca4SPoul-Henning Kamp } 2919dee34ca4SPoul-Henning Kamp 2920dee34ca4SPoul-Henning Kamp static void 2921dee34ca4SPoul-Henning Kamp swapgeom_orphan(struct g_consumer *cp) 2922dee34ca4SPoul-Henning Kamp { 2923dee34ca4SPoul-Henning Kamp struct swdevt *sp; 29243398491bSAlexander Motin int destroy; 2925dee34ca4SPoul-Henning Kamp 2926dee34ca4SPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 29273398491bSAlexander Motin TAILQ_FOREACH(sp, &swtailq, sw_list) { 29283398491bSAlexander Motin if (sp->sw_id == cp) { 29298f12d83aSAlexander Motin sp->sw_flags |= SW_CLOSING; 29303398491bSAlexander Motin break; 2931dee34ca4SPoul-Henning Kamp } 29323398491bSAlexander Motin } 29339e3e3fe5SWarner Losh /* 29349e3e3fe5SWarner Losh * Drop reference we were created with. Do directly since we're in a 29359e3e3fe5SWarner Losh * special context where we don't have to queue the call to 29369e3e3fe5SWarner Losh * swapgeom_close_ev(). 29379e3e3fe5SWarner Losh */ 29389e3e3fe5SWarner Losh cp->index--; 29393398491bSAlexander Motin destroy = ((sp != NULL) && (cp->index == 0)); 29403398491bSAlexander Motin if (destroy) 29413398491bSAlexander Motin sp->sw_id = NULL; 29423398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 29433398491bSAlexander Motin if (destroy) 29443398491bSAlexander Motin swapgeom_close_ev(cp, 0); 2945dee34ca4SPoul-Henning Kamp } 2946dee34ca4SPoul-Henning Kamp 2947dee34ca4SPoul-Henning Kamp static void 2948dee34ca4SPoul-Henning Kamp swapgeom_close(struct thread *td, struct swdevt *sw) 2949dee34ca4SPoul-Henning Kamp { 29503398491bSAlexander Motin struct g_consumer *cp; 2951dee34ca4SPoul-Henning Kamp 29523398491bSAlexander Motin mtx_lock(&sw_dev_mtx); 29533398491bSAlexander Motin cp = sw->sw_id; 29543398491bSAlexander Motin sw->sw_id = NULL; 29553398491bSAlexander Motin mtx_unlock(&sw_dev_mtx); 29560c657d22SKonstantin Belousov 29570c657d22SKonstantin Belousov /* 29580c657d22SKonstantin Belousov * swapgeom_close() may be called from the biodone context, 29590c657d22SKonstantin Belousov * where we cannot perform topology changes. Delegate the 29600c657d22SKonstantin Belousov * work to the events thread. 29610c657d22SKonstantin Belousov */ 29623398491bSAlexander Motin if (cp != NULL) 29633398491bSAlexander Motin g_waitfor_event(swapgeom_close_ev, cp, M_WAITOK, NULL); 2964dee34ca4SPoul-Henning Kamp } 2965dee34ca4SPoul-Henning Kamp 296688ad2d7bSKonstantin Belousov static int 296788ad2d7bSKonstantin Belousov swapongeom_locked(struct cdev *dev, struct vnode *vp) 2968dee34ca4SPoul-Henning Kamp { 2969dee34ca4SPoul-Henning Kamp struct g_provider *pp; 2970dee34ca4SPoul-Henning Kamp struct g_consumer *cp; 2971dee34ca4SPoul-Henning Kamp static struct g_geom *gp; 2972dee34ca4SPoul-Henning Kamp struct swdevt *sp; 2973dee34ca4SPoul-Henning Kamp u_long nblks; 2974dee34ca4SPoul-Henning Kamp int error; 2975dee34ca4SPoul-Henning Kamp 297688ad2d7bSKonstantin Belousov pp = g_dev_getprovider(dev); 297788ad2d7bSKonstantin Belousov if (pp == NULL) 297888ad2d7bSKonstantin Belousov return (ENODEV); 2979dee34ca4SPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 2980dee34ca4SPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 2981dee34ca4SPoul-Henning Kamp cp = sp->sw_id; 2982dee34ca4SPoul-Henning Kamp if (cp != NULL && cp->provider == pp) { 2983dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 298488ad2d7bSKonstantin Belousov return (EBUSY); 2985dee34ca4SPoul-Henning Kamp } 2986dee34ca4SPoul-Henning Kamp } 2987dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 29885721c9c7SPoul-Henning Kamp if (gp == NULL) 298902c62349SJaakko Heinonen gp = g_new_geomf(&g_swap_class, "swap"); 2990dee34ca4SPoul-Henning Kamp cp = g_new_consumer(gp); 29919e3e3fe5SWarner Losh cp->index = 1; /* Number of active I/Os, plus one for being active. */ 29929e3e3fe5SWarner Losh cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 2993dee34ca4SPoul-Henning Kamp g_attach(cp, pp); 2994afeb65e6SPoul-Henning Kamp /* 2995afeb65e6SPoul-Henning Kamp * XXX: Every time you think you can improve the margin for 2996afeb65e6SPoul-Henning Kamp * footshooting, somebody depends on the ability to do so: 2997afeb65e6SPoul-Henning Kamp * savecore(8) wants to write to our swapdev so we cannot 2998afeb65e6SPoul-Henning Kamp * set an exclusive count :-( 2999afeb65e6SPoul-Henning Kamp */ 3000d2bae332SPoul-Henning Kamp error = g_access(cp, 1, 1, 0); 300188ad2d7bSKonstantin Belousov if (error != 0) { 3002dee34ca4SPoul-Henning Kamp g_detach(cp); 3003dee34ca4SPoul-Henning Kamp g_destroy_consumer(cp); 300488ad2d7bSKonstantin Belousov return (error); 3005dee34ca4SPoul-Henning Kamp } 3006dee34ca4SPoul-Henning Kamp nblks = pp->mediasize / DEV_BSIZE; 300788ad2d7bSKonstantin Belousov swaponsomething(vp, cp, nblks, swapgeom_strategy, 300888ad2d7bSKonstantin Belousov swapgeom_close, dev2udev(dev), 30092cc718a1SKonstantin Belousov (pp->flags & G_PF_ACCEPT_UNMAPPED) != 0 ? SW_UNMAPPED : 0); 301088ad2d7bSKonstantin Belousov return (0); 3011dee34ca4SPoul-Henning Kamp } 3012dee34ca4SPoul-Henning Kamp 3013dee34ca4SPoul-Henning Kamp static int 301488ad2d7bSKonstantin Belousov swapongeom(struct vnode *vp) 3015dee34ca4SPoul-Henning Kamp { 3016dee34ca4SPoul-Henning Kamp int error; 3017dee34ca4SPoul-Henning Kamp 30186ddf41faSKonstantin Belousov ASSERT_VOP_ELOCKED(vp, "swapongeom"); 3019abd80ddbSMateusz Guzik if (vp->v_type != VCHR || VN_IS_DOOMED(vp)) { 302088ad2d7bSKonstantin Belousov error = ENOENT; 302188ad2d7bSKonstantin Belousov } else { 302288ad2d7bSKonstantin Belousov g_topology_lock(); 302388ad2d7bSKonstantin Belousov error = swapongeom_locked(vp->v_rdev, vp); 302488ad2d7bSKonstantin Belousov g_topology_unlock(); 302588ad2d7bSKonstantin Belousov } 3026dee34ca4SPoul-Henning Kamp return (error); 3027dee34ca4SPoul-Henning Kamp } 3028dee34ca4SPoul-Henning Kamp 3029dee34ca4SPoul-Henning Kamp /* 3030dee34ca4SPoul-Henning Kamp * VNODE backend 3031dee34ca4SPoul-Henning Kamp * 3032dee34ca4SPoul-Henning Kamp * This is used mainly for network filesystem (read: probably only tested 3033dee34ca4SPoul-Henning Kamp * with NFS) swapfiles. 3034dee34ca4SPoul-Henning Kamp * 3035dee34ca4SPoul-Henning Kamp */ 3036dee34ca4SPoul-Henning Kamp 3037dee34ca4SPoul-Henning Kamp static void 3038dee34ca4SPoul-Henning Kamp swapdev_strategy(struct buf *bp, struct swdevt *sp) 3039dee34ca4SPoul-Henning Kamp { 3040494eb176SPoul-Henning Kamp struct vnode *vp2; 3041dee34ca4SPoul-Henning Kamp 3042dee34ca4SPoul-Henning Kamp bp->b_blkno = ctodb(bp->b_blkno - sp->sw_first); 3043dee34ca4SPoul-Henning Kamp 3044dee34ca4SPoul-Henning Kamp vp2 = sp->sw_id; 3045dee34ca4SPoul-Henning Kamp vhold(vp2); 3046dee34ca4SPoul-Henning Kamp if (bp->b_iocmd == BIO_WRITE) { 3047b19740f4SKonstantin Belousov vn_lock(vp2, LK_EXCLUSIVE | LK_RETRY); 30483cfc7651SOlivier Houchard if (bp->b_bufobj) 3049494eb176SPoul-Henning Kamp bufobj_wdrop(bp->b_bufobj); 3050a76d8f4eSPoul-Henning Kamp bufobj_wref(&vp2->v_bufobj); 3051b19740f4SKonstantin Belousov } else { 3052b19740f4SKonstantin Belousov vn_lock(vp2, LK_SHARED | LK_RETRY); 3053dee34ca4SPoul-Henning Kamp } 30543cfc7651SOlivier Houchard if (bp->b_bufobj != &vp2->v_bufobj) 30553cfc7651SOlivier Houchard bp->b_bufobj = &vp2->v_bufobj; 3056dee34ca4SPoul-Henning Kamp bp->b_vp = vp2; 30572c18019fSPoul-Henning Kamp bp->b_iooffset = dbtob(bp->b_blkno); 3058b792bebeSPoul-Henning Kamp bstrategy(bp); 3059b19740f4SKonstantin Belousov VOP_UNLOCK(vp2); 3060dee34ca4SPoul-Henning Kamp } 3061dee34ca4SPoul-Henning Kamp 3062dee34ca4SPoul-Henning Kamp static void 3063dee34ca4SPoul-Henning Kamp swapdev_close(struct thread *td, struct swdevt *sp) 3064dee34ca4SPoul-Henning Kamp { 3065a6d04f34SKonstantin Belousov struct vnode *vp; 3066dee34ca4SPoul-Henning Kamp 3067a6d04f34SKonstantin Belousov vp = sp->sw_vp; 3068a6d04f34SKonstantin Belousov vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 3069a6d04f34SKonstantin Belousov VOP_CLOSE(vp, FREAD | FWRITE, td->td_ucred, td); 3070a6d04f34SKonstantin Belousov vput(vp); 3071dee34ca4SPoul-Henning Kamp } 3072dee34ca4SPoul-Henning Kamp 3073dee34ca4SPoul-Henning Kamp static int 3074dee34ca4SPoul-Henning Kamp swaponvp(struct thread *td, struct vnode *vp, u_long nblks) 3075dee34ca4SPoul-Henning Kamp { 3076dee34ca4SPoul-Henning Kamp struct swdevt *sp; 3077dee34ca4SPoul-Henning Kamp int error; 3078dee34ca4SPoul-Henning Kamp 30796ddf41faSKonstantin Belousov ASSERT_VOP_ELOCKED(vp, "swaponvp"); 3080dee34ca4SPoul-Henning Kamp if (nblks == 0) 3081dee34ca4SPoul-Henning Kamp return (ENXIO); 3082dee34ca4SPoul-Henning Kamp mtx_lock(&sw_dev_mtx); 3083dee34ca4SPoul-Henning Kamp TAILQ_FOREACH(sp, &swtailq, sw_list) { 3084dee34ca4SPoul-Henning Kamp if (sp->sw_id == vp) { 3085dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 3086dee34ca4SPoul-Henning Kamp return (EBUSY); 3087dee34ca4SPoul-Henning Kamp } 3088dee34ca4SPoul-Henning Kamp } 3089dee34ca4SPoul-Henning Kamp mtx_unlock(&sw_dev_mtx); 3090dee34ca4SPoul-Henning Kamp 3091dee34ca4SPoul-Henning Kamp #ifdef MAC 309230d239bcSRobert Watson error = mac_system_check_swapon(td->td_ucred, vp); 3093dee34ca4SPoul-Henning Kamp if (error == 0) 3094dee34ca4SPoul-Henning Kamp #endif 30959e223287SKonstantin Belousov error = VOP_OPEN(vp, FREAD | FWRITE, td->td_ucred, td, NULL); 30966ddf41faSKonstantin Belousov if (error != 0) 3097dee34ca4SPoul-Henning Kamp return (error); 3098dee34ca4SPoul-Henning Kamp 3099dee34ca4SPoul-Henning Kamp swaponsomething(vp, vp, nblks, swapdev_strategy, swapdev_close, 31002cc718a1SKonstantin Belousov NODEV, 0); 3101dee34ca4SPoul-Henning Kamp return (0); 3102dee34ca4SPoul-Henning Kamp } 310389c241d1SGleb Smirnoff 310489c241d1SGleb Smirnoff static int 310589c241d1SGleb Smirnoff sysctl_swap_async_max(SYSCTL_HANDLER_ARGS) 310689c241d1SGleb Smirnoff { 310789c241d1SGleb Smirnoff int error, new, n; 310889c241d1SGleb Smirnoff 310989c241d1SGleb Smirnoff new = nsw_wcount_async_max; 311089c241d1SGleb Smirnoff error = sysctl_handle_int(oidp, &new, 0, req); 311189c241d1SGleb Smirnoff if (error != 0 || req->newptr == NULL) 311289c241d1SGleb Smirnoff return (error); 311389c241d1SGleb Smirnoff 311489c241d1SGleb Smirnoff if (new > nswbuf / 2 || new < 1) 311589c241d1SGleb Smirnoff return (EINVAL); 311689c241d1SGleb Smirnoff 3117756a5412SGleb Smirnoff mtx_lock(&swbuf_mtx); 311889c241d1SGleb Smirnoff while (nsw_wcount_async_max != new) { 311989c241d1SGleb Smirnoff /* 312089c241d1SGleb Smirnoff * Adjust difference. If the current async count is too low, 312189c241d1SGleb Smirnoff * we will need to sqeeze our update slowly in. Sleep with a 312289c241d1SGleb Smirnoff * higher priority than getpbuf() to finish faster. 312389c241d1SGleb Smirnoff */ 312489c241d1SGleb Smirnoff n = new - nsw_wcount_async_max; 312589c241d1SGleb Smirnoff if (nsw_wcount_async + n >= 0) { 312689c241d1SGleb Smirnoff nsw_wcount_async += n; 312789c241d1SGleb Smirnoff nsw_wcount_async_max += n; 312889c241d1SGleb Smirnoff wakeup(&nsw_wcount_async); 312989c241d1SGleb Smirnoff } else { 313089c241d1SGleb Smirnoff nsw_wcount_async_max -= nsw_wcount_async; 313189c241d1SGleb Smirnoff nsw_wcount_async = 0; 3132756a5412SGleb Smirnoff msleep(&nsw_wcount_async, &swbuf_mtx, PSWP, 313389c241d1SGleb Smirnoff "swpsysctl", 0); 313489c241d1SGleb Smirnoff } 313589c241d1SGleb Smirnoff } 3136756a5412SGleb Smirnoff mtx_unlock(&swbuf_mtx); 313789c241d1SGleb Smirnoff 313889c241d1SGleb Smirnoff return (0); 313989c241d1SGleb Smirnoff } 3140fe7bcbafSKyle Evans 3141fe7bcbafSKyle Evans static void 3142fe7bcbafSKyle Evans swap_pager_update_writecount(vm_object_t object, vm_offset_t start, 3143fe7bcbafSKyle Evans vm_offset_t end) 3144fe7bcbafSKyle Evans { 3145fe7bcbafSKyle Evans 3146fe7bcbafSKyle Evans VM_OBJECT_WLOCK(object); 314763967687SJeff Roberson KASSERT((object->flags & OBJ_ANON) == 0, 3148fe7bcbafSKyle Evans ("Splittable object with writecount")); 3149fe7bcbafSKyle Evans object->un_pager.swp.writemappings += (vm_ooffset_t)end - start; 3150fe7bcbafSKyle Evans VM_OBJECT_WUNLOCK(object); 3151fe7bcbafSKyle Evans } 3152fe7bcbafSKyle Evans 3153fe7bcbafSKyle Evans static void 3154fe7bcbafSKyle Evans swap_pager_release_writecount(vm_object_t object, vm_offset_t start, 3155fe7bcbafSKyle Evans vm_offset_t end) 3156fe7bcbafSKyle Evans { 3157fe7bcbafSKyle Evans 3158fe7bcbafSKyle Evans VM_OBJECT_WLOCK(object); 315963967687SJeff Roberson KASSERT((object->flags & OBJ_ANON) == 0, 3160fe7bcbafSKyle Evans ("Splittable object with writecount")); 3161*6ada4e8aSKonstantin Belousov KASSERT(object->un_pager.swp.writemappings >= (vm_ooffset_t)end - start, 3162*6ada4e8aSKonstantin Belousov ("swap obj %p writecount %jx dec %jx", object, 3163*6ada4e8aSKonstantin Belousov (uintmax_t)object->un_pager.swp.writemappings, 3164*6ada4e8aSKonstantin Belousov (uintmax_t)((vm_ooffset_t)end - start))); 3165fe7bcbafSKyle Evans object->un_pager.swp.writemappings -= (vm_ooffset_t)end - start; 3166fe7bcbafSKyle Evans VM_OBJECT_WUNLOCK(object); 3167fe7bcbafSKyle Evans } 3168