1 /*- 2 * SPDX-License-Identifier: BSD-4-Clause 3 * 4 * Copyright (c) 1998 Matthew Dillon, 5 * Copyright (c) 1994 John S. Dyson 6 * Copyright (c) 1990 University of Utah. 7 * Copyright (c) 1982, 1986, 1989, 1993 8 * The Regents of the University of California. All rights reserved. 9 * 10 * This code is derived from software contributed to Berkeley by 11 * the Systems Programming Group of the University of Utah Computer 12 * Science Department. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 3. All advertising materials mentioning features or use of this software 23 * must display the following acknowledgement: 24 * This product includes software developed by the University of 25 * California, Berkeley and its contributors. 26 * 4. Neither the name of the University nor the names of its contributors 27 * may be used to endorse or promote products derived from this software 28 * without specific prior written permission. 29 * 30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 40 * SUCH DAMAGE. 41 * 42 * New Swap System 43 * Matthew Dillon 44 * 45 * Radix Bitmap 'blists'. 46 * 47 * - The new swapper uses the new radix bitmap code. This should scale 48 * to arbitrarily small or arbitrarily large swap spaces and an almost 49 * arbitrary degree of fragmentation. 50 * 51 * Features: 52 * 53 * - on the fly reallocation of swap during putpages. The new system 54 * does not try to keep previously allocated swap blocks for dirty 55 * pages. 56 * 57 * - on the fly deallocation of swap 58 * 59 * - No more garbage collection required. Unnecessarily allocated swap 60 * blocks only exist for dirty vm_page_t's now and these are already 61 * cycled (in a high-load system) by the pager. We also do on-the-fly 62 * removal of invalidated swap blocks when a page is destroyed 63 * or renamed. 64 * 65 * from: Utah $Hdr: swap_pager.c 1.4 91/04/30$ 66 */ 67 68 #include <sys/cdefs.h> 69 #include "opt_vm.h" 70 71 #include <sys/param.h> 72 #include <sys/bio.h> 73 #include <sys/blist.h> 74 #include <sys/buf.h> 75 #include <sys/conf.h> 76 #include <sys/disk.h> 77 #include <sys/disklabel.h> 78 #include <sys/eventhandler.h> 79 #include <sys/fcntl.h> 80 #include <sys/limits.h> 81 #include <sys/lock.h> 82 #include <sys/kernel.h> 83 #include <sys/mount.h> 84 #include <sys/namei.h> 85 #include <sys/malloc.h> 86 #include <sys/pctrie.h> 87 #include <sys/priv.h> 88 #include <sys/proc.h> 89 #include <sys/racct.h> 90 #include <sys/resource.h> 91 #include <sys/resourcevar.h> 92 #include <sys/rwlock.h> 93 #include <sys/sbuf.h> 94 #include <sys/sysctl.h> 95 #include <sys/sysproto.h> 96 #include <sys/systm.h> 97 #include <sys/sx.h> 98 #include <sys/unistd.h> 99 #include <sys/user.h> 100 #include <sys/vmmeter.h> 101 #include <sys/vnode.h> 102 103 #include <security/mac/mac_framework.h> 104 105 #include <vm/vm.h> 106 #include <vm/pmap.h> 107 #include <vm/vm_map.h> 108 #include <vm/vm_kern.h> 109 #include <vm/vm_object.h> 110 #include <vm/vm_page.h> 111 #include <vm/vm_pager.h> 112 #include <vm/vm_pageout.h> 113 #include <vm/vm_param.h> 114 #include <vm/swap_pager.h> 115 #include <vm/vm_extern.h> 116 #include <vm/uma.h> 117 118 #include <geom/geom.h> 119 120 /* 121 * MAX_PAGEOUT_CLUSTER must be a power of 2 between 1 and 64. 122 * The 64-page limit is due to the radix code (kern/subr_blist.c). 123 */ 124 #ifndef MAX_PAGEOUT_CLUSTER 125 #define MAX_PAGEOUT_CLUSTER 32 126 #endif 127 128 #if !defined(SWB_NPAGES) 129 #define SWB_NPAGES MAX_PAGEOUT_CLUSTER 130 #endif 131 132 #define SWAP_META_PAGES PCTRIE_COUNT 133 134 /* 135 * A swblk structure maps each page index within a 136 * SWAP_META_PAGES-aligned and sized range to the address of an 137 * on-disk swap block (or SWAPBLK_NONE). The collection of these 138 * mappings for an entire vm object is implemented as a pc-trie. 139 */ 140 struct swblk { 141 vm_pindex_t p; 142 daddr_t d[SWAP_META_PAGES]; 143 }; 144 145 /* 146 * A page_range structure records the start address and length of a sequence of 147 * mapped page addresses. 148 */ 149 struct page_range { 150 daddr_t start; 151 daddr_t num; 152 }; 153 154 static MALLOC_DEFINE(M_VMPGDATA, "vm_pgdata", "swap pager private data"); 155 static struct mtx sw_dev_mtx; 156 static TAILQ_HEAD(, swdevt) swtailq = TAILQ_HEAD_INITIALIZER(swtailq); 157 static struct swdevt *swdevhd; /* Allocate from here next */ 158 static int nswapdev; /* Number of swap devices */ 159 int swap_pager_avail; 160 static struct sx swdev_syscall_lock; /* serialize swap(on|off) */ 161 162 static __exclusive_cache_line u_long swap_reserved; 163 static u_long swap_total; 164 static int sysctl_page_shift(SYSCTL_HANDLER_ARGS); 165 166 static SYSCTL_NODE(_vm_stats, OID_AUTO, swap, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 167 "VM swap stats"); 168 169 SYSCTL_PROC(_vm, OID_AUTO, swap_reserved, CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE, 170 &swap_reserved, 0, sysctl_page_shift, "QU", 171 "Amount of swap storage needed to back all allocated anonymous memory."); 172 SYSCTL_PROC(_vm, OID_AUTO, swap_total, CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE, 173 &swap_total, 0, sysctl_page_shift, "QU", 174 "Total amount of available swap storage."); 175 176 int vm_overcommit __read_mostly = 0; 177 SYSCTL_INT(_vm, VM_OVERCOMMIT, overcommit, CTLFLAG_RW, &vm_overcommit, 0, 178 "Configure virtual memory overcommit behavior. See tuning(7) " 179 "for details."); 180 static unsigned long swzone; 181 SYSCTL_ULONG(_vm, OID_AUTO, swzone, CTLFLAG_RD, &swzone, 0, 182 "Actual size of swap metadata zone"); 183 static unsigned long swap_maxpages; 184 SYSCTL_ULONG(_vm, OID_AUTO, swap_maxpages, CTLFLAG_RD, &swap_maxpages, 0, 185 "Maximum amount of swap supported"); 186 187 static COUNTER_U64_DEFINE_EARLY(swap_free_deferred); 188 SYSCTL_COUNTER_U64(_vm_stats_swap, OID_AUTO, free_deferred, 189 CTLFLAG_RD, &swap_free_deferred, 190 "Number of pages that deferred freeing swap space"); 191 192 static COUNTER_U64_DEFINE_EARLY(swap_free_completed); 193 SYSCTL_COUNTER_U64(_vm_stats_swap, OID_AUTO, free_completed, 194 CTLFLAG_RD, &swap_free_completed, 195 "Number of deferred frees completed"); 196 197 static int 198 sysctl_page_shift(SYSCTL_HANDLER_ARGS) 199 { 200 uint64_t newval; 201 u_long value = *(u_long *)arg1; 202 203 newval = ((uint64_t)value) << PAGE_SHIFT; 204 return (sysctl_handle_64(oidp, &newval, 0, req)); 205 } 206 207 static bool 208 swap_reserve_by_cred_rlimit(u_long pincr, struct ucred *cred, int oc) 209 { 210 struct uidinfo *uip; 211 u_long prev; 212 213 uip = cred->cr_ruidinfo; 214 215 prev = atomic_fetchadd_long(&uip->ui_vmsize, pincr); 216 if ((oc & SWAP_RESERVE_RLIMIT_ON) != 0 && 217 prev + pincr > lim_cur(curthread, RLIMIT_SWAP) && 218 priv_check(curthread, PRIV_VM_SWAP_NORLIMIT) != 0) { 219 prev = atomic_fetchadd_long(&uip->ui_vmsize, -pincr); 220 KASSERT(prev >= pincr, 221 ("negative vmsize for uid %d\n", uip->ui_uid)); 222 return (false); 223 } 224 return (true); 225 } 226 227 static void 228 swap_release_by_cred_rlimit(u_long pdecr, struct ucred *cred) 229 { 230 struct uidinfo *uip; 231 #ifdef INVARIANTS 232 u_long prev; 233 #endif 234 235 uip = cred->cr_ruidinfo; 236 237 #ifdef INVARIANTS 238 prev = atomic_fetchadd_long(&uip->ui_vmsize, -pdecr); 239 KASSERT(prev >= pdecr, 240 ("negative vmsize for uid %d\n", uip->ui_uid)); 241 #else 242 atomic_subtract_long(&uip->ui_vmsize, pdecr); 243 #endif 244 } 245 246 static void 247 swap_reserve_force_rlimit(u_long pincr, struct ucred *cred) 248 { 249 struct uidinfo *uip; 250 251 uip = cred->cr_ruidinfo; 252 atomic_add_long(&uip->ui_vmsize, pincr); 253 } 254 255 bool 256 swap_reserve(vm_ooffset_t incr) 257 { 258 259 return (swap_reserve_by_cred(incr, curthread->td_ucred)); 260 } 261 262 bool 263 swap_reserve_by_cred(vm_ooffset_t incr, struct ucred *cred) 264 { 265 u_long r, s, prev, pincr; 266 #ifdef RACCT 267 int error; 268 #endif 269 int oc; 270 static int curfail; 271 static struct timeval lastfail; 272 273 KASSERT((incr & PAGE_MASK) == 0, ("%s: incr: %ju & PAGE_MASK", 274 __func__, (uintmax_t)incr)); 275 276 #ifdef RACCT 277 if (RACCT_ENABLED()) { 278 PROC_LOCK(curproc); 279 error = racct_add(curproc, RACCT_SWAP, incr); 280 PROC_UNLOCK(curproc); 281 if (error != 0) 282 return (false); 283 } 284 #endif 285 286 pincr = atop(incr); 287 prev = atomic_fetchadd_long(&swap_reserved, pincr); 288 r = prev + pincr; 289 s = swap_total; 290 oc = atomic_load_int(&vm_overcommit); 291 if (r > s && (oc & SWAP_RESERVE_ALLOW_NONWIRED) != 0) { 292 s += vm_cnt.v_page_count - vm_cnt.v_free_reserved - 293 vm_wire_count(); 294 } 295 if ((oc & SWAP_RESERVE_FORCE_ON) != 0 && r > s && 296 priv_check(curthread, PRIV_VM_SWAP_NOQUOTA) != 0) { 297 prev = atomic_fetchadd_long(&swap_reserved, -pincr); 298 KASSERT(prev >= pincr, 299 ("swap_reserved < incr on overcommit fail")); 300 goto out_error; 301 } 302 303 if (!swap_reserve_by_cred_rlimit(pincr, cred, oc)) { 304 prev = atomic_fetchadd_long(&swap_reserved, -pincr); 305 KASSERT(prev >= pincr, 306 ("swap_reserved < incr on overcommit fail")); 307 goto out_error; 308 } 309 310 return (true); 311 312 out_error: 313 if (ppsratecheck(&lastfail, &curfail, 1)) { 314 printf("uid %d, pid %d: swap reservation " 315 "for %jd bytes failed\n", 316 cred->cr_ruidinfo->ui_uid, curproc->p_pid, incr); 317 } 318 #ifdef RACCT 319 if (RACCT_ENABLED()) { 320 PROC_LOCK(curproc); 321 racct_sub(curproc, RACCT_SWAP, incr); 322 PROC_UNLOCK(curproc); 323 } 324 #endif 325 326 return (false); 327 } 328 329 void 330 swap_reserve_force(vm_ooffset_t incr) 331 { 332 u_long pincr; 333 334 KASSERT((incr & PAGE_MASK) == 0, ("%s: incr: %ju & PAGE_MASK", 335 __func__, (uintmax_t)incr)); 336 337 #ifdef RACCT 338 if (RACCT_ENABLED()) { 339 PROC_LOCK(curproc); 340 racct_add_force(curproc, RACCT_SWAP, incr); 341 PROC_UNLOCK(curproc); 342 } 343 #endif 344 pincr = atop(incr); 345 atomic_add_long(&swap_reserved, pincr); 346 swap_reserve_force_rlimit(pincr, curthread->td_ucred); 347 } 348 349 void 350 swap_release(vm_ooffset_t decr) 351 { 352 struct ucred *cred; 353 354 PROC_LOCK(curproc); 355 cred = curproc->p_ucred; 356 swap_release_by_cred(decr, cred); 357 PROC_UNLOCK(curproc); 358 } 359 360 void 361 swap_release_by_cred(vm_ooffset_t decr, struct ucred *cred) 362 { 363 u_long pdecr; 364 #ifdef INVARIANTS 365 u_long prev; 366 #endif 367 368 KASSERT((decr & PAGE_MASK) == 0, ("%s: decr: %ju & PAGE_MASK", 369 __func__, (uintmax_t)decr)); 370 371 pdecr = atop(decr); 372 #ifdef INVARIANTS 373 prev = atomic_fetchadd_long(&swap_reserved, -pdecr); 374 KASSERT(prev >= pdecr, ("swap_reserved < decr")); 375 #else 376 atomic_subtract_long(&swap_reserved, pdecr); 377 #endif 378 379 swap_release_by_cred_rlimit(pdecr, cred); 380 #ifdef RACCT 381 if (racct_enable) 382 racct_sub_cred(cred, RACCT_SWAP, decr); 383 #endif 384 } 385 386 static int swap_pager_full = 2; /* swap space exhaustion (task killing) */ 387 static int swap_pager_almost_full = 1; /* swap space exhaustion (w/hysteresis)*/ 388 static struct mtx swbuf_mtx; /* to sync nsw_wcount_async */ 389 static int nsw_wcount_async; /* limit async write buffers */ 390 static int nsw_wcount_async_max;/* assigned maximum */ 391 int nsw_cluster_max; /* maximum VOP I/O allowed */ 392 393 static int sysctl_swap_async_max(SYSCTL_HANDLER_ARGS); 394 SYSCTL_PROC(_vm, OID_AUTO, swap_async_max, CTLTYPE_INT | CTLFLAG_RW | 395 CTLFLAG_MPSAFE, NULL, 0, sysctl_swap_async_max, "I", 396 "Maximum running async swap ops"); 397 static int sysctl_swap_fragmentation(SYSCTL_HANDLER_ARGS); 398 SYSCTL_PROC(_vm, OID_AUTO, swap_fragmentation, CTLTYPE_STRING | CTLFLAG_RD | 399 CTLFLAG_MPSAFE, NULL, 0, sysctl_swap_fragmentation, "A", 400 "Swap Fragmentation Info"); 401 402 static struct sx sw_alloc_sx; 403 404 /* 405 * "named" and "unnamed" anon region objects. Try to reduce the overhead 406 * of searching a named list by hashing it just a little. 407 */ 408 409 #define NOBJLISTS 8 410 411 #define NOBJLIST(handle) \ 412 (&swap_pager_object_list[((int)(intptr_t)handle >> 4) & (NOBJLISTS-1)]) 413 414 static struct pagerlst swap_pager_object_list[NOBJLISTS]; 415 static uma_zone_t swwbuf_zone; 416 static uma_zone_t swrbuf_zone; 417 static uma_zone_t swblk_zone; 418 static uma_zone_t swpctrie_zone; 419 420 /* 421 * pagerops for OBJT_SWAP - "swap pager". Some ops are also global procedure 422 * calls hooked from other parts of the VM system and do not appear here. 423 * (see vm/swap_pager.h). 424 */ 425 static vm_object_t 426 swap_pager_alloc(void *handle, vm_ooffset_t size, 427 vm_prot_t prot, vm_ooffset_t offset, struct ucred *); 428 static void swap_pager_dealloc(vm_object_t object); 429 static int swap_pager_getpages(vm_object_t, vm_page_t *, int, int *, 430 int *); 431 static int swap_pager_getpages_async(vm_object_t, vm_page_t *, int, int *, 432 int *, pgo_getpages_iodone_t, void *); 433 static void swap_pager_putpages(vm_object_t, vm_page_t *, int, int, int *); 434 static boolean_t 435 swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, int *after); 436 static void swap_pager_init(void); 437 static void swap_pager_unswapped(vm_page_t); 438 static void swap_pager_swapoff(struct swdevt *sp); 439 static void swap_pager_update_writecount(vm_object_t object, 440 vm_offset_t start, vm_offset_t end); 441 static void swap_pager_release_writecount(vm_object_t object, 442 vm_offset_t start, vm_offset_t end); 443 static void swap_pager_freespace_pgo(vm_object_t object, vm_pindex_t start, 444 vm_size_t size); 445 446 const struct pagerops swappagerops = { 447 .pgo_kvme_type = KVME_TYPE_SWAP, 448 .pgo_init = swap_pager_init, /* early system initialization of pager */ 449 .pgo_alloc = swap_pager_alloc, /* allocate an OBJT_SWAP object */ 450 .pgo_dealloc = swap_pager_dealloc, /* deallocate an OBJT_SWAP object */ 451 .pgo_getpages = swap_pager_getpages, /* pagein */ 452 .pgo_getpages_async = swap_pager_getpages_async, /* pagein (async) */ 453 .pgo_putpages = swap_pager_putpages, /* pageout */ 454 .pgo_haspage = swap_pager_haspage, /* get backing store status for page */ 455 .pgo_pageunswapped = swap_pager_unswapped, /* remove swap related to page */ 456 .pgo_update_writecount = swap_pager_update_writecount, 457 .pgo_release_writecount = swap_pager_release_writecount, 458 .pgo_freespace = swap_pager_freespace_pgo, 459 }; 460 461 /* 462 * swap_*() routines are externally accessible. swp_*() routines are 463 * internal. 464 */ 465 static int nswap_lowat = 128; /* in pages, swap_pager_almost_full warn */ 466 static int nswap_hiwat = 512; /* in pages, swap_pager_almost_full warn */ 467 468 SYSCTL_INT(_vm, OID_AUTO, dmmax, CTLFLAG_RD, &nsw_cluster_max, 0, 469 "Maximum size of a swap block in pages"); 470 471 static void swp_sizecheck(void); 472 static void swp_pager_async_iodone(struct buf *bp); 473 static bool swp_pager_swblk_empty(struct swblk *sb, int start, int limit); 474 static void swp_pager_free_empty_swblk(vm_object_t, struct swblk *sb); 475 static int swapongeom(struct vnode *); 476 static int swaponvp(struct thread *, struct vnode *, u_long); 477 static int swapoff_one(struct swdevt *sp, struct ucred *cred, 478 u_int flags); 479 480 /* 481 * Swap bitmap functions 482 */ 483 static void swp_pager_freeswapspace(const struct page_range *range); 484 static daddr_t swp_pager_getswapspace(int *npages); 485 486 /* 487 * Metadata functions 488 */ 489 static daddr_t swp_pager_meta_build(vm_object_t, vm_pindex_t, daddr_t, 490 bool); 491 static void swp_pager_meta_free(vm_object_t, vm_pindex_t, vm_pindex_t, 492 vm_size_t *); 493 static void swp_pager_meta_transfer(vm_object_t src, vm_object_t dst, 494 vm_pindex_t pindex, vm_pindex_t count); 495 static void swp_pager_meta_free_all(vm_object_t); 496 static daddr_t swp_pager_meta_lookup(vm_object_t, vm_pindex_t); 497 498 static void 499 swp_pager_init_freerange(struct page_range *range) 500 { 501 range->start = SWAPBLK_NONE; 502 range->num = 0; 503 } 504 505 static void 506 swp_pager_update_freerange(struct page_range *range, daddr_t addr) 507 { 508 if (range->start + range->num == addr) { 509 range->num++; 510 } else { 511 swp_pager_freeswapspace(range); 512 range->start = addr; 513 range->num = 1; 514 } 515 } 516 517 static void * 518 swblk_trie_alloc(struct pctrie *ptree) 519 { 520 521 return (uma_zalloc(swpctrie_zone, M_NOWAIT | (curproc == pageproc ? 522 M_USE_RESERVE : 0))); 523 } 524 525 static void 526 swblk_trie_free(struct pctrie *ptree, void *node) 527 { 528 529 uma_zfree(swpctrie_zone, node); 530 } 531 532 static int 533 swblk_start(struct swblk *sb, vm_pindex_t pindex) 534 { 535 return (sb == NULL || sb->p >= pindex ? 536 0 : pindex % SWAP_META_PAGES); 537 } 538 539 PCTRIE_DEFINE(SWAP, swblk, p, swblk_trie_alloc, swblk_trie_free); 540 541 static struct swblk * 542 swblk_lookup(vm_object_t object, vm_pindex_t pindex) 543 { 544 return (SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, 545 rounddown(pindex, SWAP_META_PAGES))); 546 } 547 548 static void 549 swblk_lookup_remove(vm_object_t object, struct swblk *sb) 550 { 551 SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, sb->p); 552 } 553 554 static int 555 swblk_lookup_insert(vm_object_t object, struct swblk *sb) 556 { 557 return (SWAP_PCTRIE_INSERT(&object->un_pager.swp.swp_blks, sb)); 558 } 559 560 static bool 561 swblk_is_empty(vm_object_t object) 562 { 563 return (pctrie_is_empty(&object->un_pager.swp.swp_blks)); 564 } 565 566 static struct swblk * 567 swblk_iter(struct pctrie_iter *blks, vm_object_t object, 568 vm_pindex_t pindex) 569 { 570 VM_OBJECT_ASSERT_LOCKED(object); 571 MPASS((object->flags & OBJ_SWAP) != 0); 572 pctrie_iter_init(blks, &object->un_pager.swp.swp_blks); 573 return (SWAP_PCTRIE_ITER_LOOKUP_GE(blks, 574 rounddown(pindex, SWAP_META_PAGES))); 575 } 576 577 static struct swblk * 578 swblk_iter_limit(struct pctrie_iter *blks, vm_object_t object, 579 vm_pindex_t pindex, vm_pindex_t limit) 580 { 581 VM_OBJECT_ASSERT_LOCKED(object); 582 MPASS((object->flags & OBJ_SWAP) != 0); 583 pctrie_iter_limit_init(blks, &object->un_pager.swp.swp_blks, limit); 584 return (SWAP_PCTRIE_ITER_LOOKUP_GE(blks, 585 rounddown(pindex, SWAP_META_PAGES))); 586 } 587 588 static struct swblk * 589 swblk_iter_next(struct pctrie_iter *blks) 590 { 591 return (SWAP_PCTRIE_ITER_JUMP_GE(blks, SWAP_META_PAGES)); 592 } 593 594 static void 595 swblk_iter_remove(struct pctrie_iter *blks) 596 { 597 SWAP_PCTRIE_ITER_REMOVE(blks); 598 } 599 600 /* 601 * SWP_SIZECHECK() - update swap_pager_full indication 602 * 603 * update the swap_pager_almost_full indication and warn when we are 604 * about to run out of swap space, using lowat/hiwat hysteresis. 605 * 606 * Clear swap_pager_full ( task killing ) indication when lowat is met. 607 * 608 * No restrictions on call 609 * This routine may not block. 610 */ 611 static void 612 swp_sizecheck(void) 613 { 614 615 if (swap_pager_avail < nswap_lowat) { 616 if (swap_pager_almost_full == 0) { 617 printf("swap_pager: out of swap space\n"); 618 swap_pager_almost_full = 1; 619 } 620 } else { 621 swap_pager_full = 0; 622 if (swap_pager_avail > nswap_hiwat) 623 swap_pager_almost_full = 0; 624 } 625 } 626 627 /* 628 * SWAP_PAGER_INIT() - initialize the swap pager! 629 * 630 * Expected to be started from system init. NOTE: This code is run 631 * before much else so be careful what you depend on. Most of the VM 632 * system has yet to be initialized at this point. 633 */ 634 static void 635 swap_pager_init(void) 636 { 637 /* 638 * Initialize object lists 639 */ 640 int i; 641 642 for (i = 0; i < NOBJLISTS; ++i) 643 TAILQ_INIT(&swap_pager_object_list[i]); 644 mtx_init(&sw_dev_mtx, "swapdev", NULL, MTX_DEF); 645 sx_init(&sw_alloc_sx, "swspsx"); 646 sx_init(&swdev_syscall_lock, "swsysc"); 647 648 /* 649 * The nsw_cluster_max is constrained by the bp->b_pages[] 650 * array, which has maxphys / PAGE_SIZE entries, and our locally 651 * defined MAX_PAGEOUT_CLUSTER. Also be aware that swap ops are 652 * constrained by the swap device interleave stripe size. 653 * 654 * Initialized early so that GEOM_ELI can see it. 655 */ 656 nsw_cluster_max = min(maxphys / PAGE_SIZE, MAX_PAGEOUT_CLUSTER); 657 } 658 659 /* 660 * SWAP_PAGER_SWAP_INIT() - swap pager initialization from pageout process 661 * 662 * Expected to be started from pageout process once, prior to entering 663 * its main loop. 664 */ 665 void 666 swap_pager_swap_init(void) 667 { 668 unsigned long n, n2; 669 670 /* 671 * Number of in-transit swap bp operations. Don't 672 * exhaust the pbufs completely. Make sure we 673 * initialize workable values (0 will work for hysteresis 674 * but it isn't very efficient). 675 * 676 * Currently we hardwire nsw_wcount_async to 4. This limit is 677 * designed to prevent other I/O from having high latencies due to 678 * our pageout I/O. The value 4 works well for one or two active swap 679 * devices but is probably a little low if you have more. Even so, 680 * a higher value would probably generate only a limited improvement 681 * with three or four active swap devices since the system does not 682 * typically have to pageout at extreme bandwidths. We will want 683 * at least 2 per swap devices, and 4 is a pretty good value if you 684 * have one NFS swap device due to the command/ack latency over NFS. 685 * So it all works out pretty well. 686 * 687 * nsw_cluster_max is initialized in swap_pager_init(). 688 */ 689 690 nsw_wcount_async = 4; 691 nsw_wcount_async_max = nsw_wcount_async; 692 mtx_init(&swbuf_mtx, "async swbuf mutex", NULL, MTX_DEF); 693 694 swwbuf_zone = pbuf_zsecond_create("swwbuf", nswbuf / 4); 695 swrbuf_zone = pbuf_zsecond_create("swrbuf", nswbuf / 2); 696 697 /* 698 * Initialize our zone, taking the user's requested size or 699 * estimating the number we need based on the number of pages 700 * in the system. 701 */ 702 n = maxswzone != 0 ? maxswzone / sizeof(struct swblk) : 703 vm_cnt.v_page_count / 2; 704 swpctrie_zone = uma_zcreate("swpctrie", pctrie_node_size(), NULL, NULL, 705 pctrie_zone_init, NULL, UMA_ALIGN_PTR, 0); 706 swblk_zone = uma_zcreate("swblk", sizeof(struct swblk), NULL, NULL, 707 NULL, NULL, _Alignof(struct swblk) - 1, 0); 708 n2 = n; 709 do { 710 if (uma_zone_reserve_kva(swblk_zone, n)) 711 break; 712 /* 713 * if the allocation failed, try a zone two thirds the 714 * size of the previous attempt. 715 */ 716 n -= ((n + 2) / 3); 717 } while (n > 0); 718 719 /* 720 * Often uma_zone_reserve_kva() cannot reserve exactly the 721 * requested size. Account for the difference when 722 * calculating swap_maxpages. 723 */ 724 n = uma_zone_get_max(swblk_zone); 725 726 if (n < n2) 727 printf("Swap blk zone entries changed from %lu to %lu.\n", 728 n2, n); 729 /* absolute maximum we can handle assuming 100% efficiency */ 730 swap_maxpages = n * SWAP_META_PAGES; 731 swzone = n * sizeof(struct swblk); 732 if (!uma_zone_reserve_kva(swpctrie_zone, n)) 733 printf("Cannot reserve swap pctrie zone, " 734 "reduce kern.maxswzone.\n"); 735 } 736 737 bool 738 swap_pager_init_object(vm_object_t object, void *handle, struct ucred *cred, 739 vm_ooffset_t size, vm_ooffset_t offset) 740 { 741 if (cred != NULL) { 742 if (!swap_reserve_by_cred(size, cred)) 743 return (false); 744 crhold(cred); 745 } 746 747 object->un_pager.swp.writemappings = 0; 748 object->handle = handle; 749 if (cred != NULL) { 750 object->cred = cred; 751 object->charge = size; 752 } 753 return (true); 754 } 755 756 static vm_object_t 757 swap_pager_alloc_init(objtype_t otype, void *handle, struct ucred *cred, 758 vm_ooffset_t size, vm_ooffset_t offset) 759 { 760 vm_object_t object; 761 762 /* 763 * The un_pager.swp.swp_blks trie is initialized by 764 * vm_object_allocate() to ensure the correct order of 765 * visibility to other threads. 766 */ 767 object = vm_object_allocate(otype, OFF_TO_IDX(offset + 768 PAGE_MASK + size)); 769 770 if (!swap_pager_init_object(object, handle, cred, size, offset)) { 771 vm_object_deallocate(object); 772 return (NULL); 773 } 774 return (object); 775 } 776 777 /* 778 * SWAP_PAGER_ALLOC() - allocate a new OBJT_SWAP VM object and instantiate 779 * its metadata structures. 780 * 781 * This routine is called from the mmap and fork code to create a new 782 * OBJT_SWAP object. 783 * 784 * This routine must ensure that no live duplicate is created for 785 * the named object request, which is protected against by 786 * holding the sw_alloc_sx lock in case handle != NULL. 787 */ 788 static vm_object_t 789 swap_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 790 vm_ooffset_t offset, struct ucred *cred) 791 { 792 vm_object_t object; 793 794 if (handle != NULL) { 795 /* 796 * Reference existing named region or allocate new one. There 797 * should not be a race here against swp_pager_meta_build() 798 * as called from vm_page_remove() in regards to the lookup 799 * of the handle. 800 */ 801 sx_xlock(&sw_alloc_sx); 802 object = vm_pager_object_lookup(NOBJLIST(handle), handle); 803 if (object == NULL) { 804 object = swap_pager_alloc_init(OBJT_SWAP, handle, cred, 805 size, offset); 806 if (object != NULL) { 807 TAILQ_INSERT_TAIL(NOBJLIST(object->handle), 808 object, pager_object_list); 809 } 810 } 811 sx_xunlock(&sw_alloc_sx); 812 } else { 813 object = swap_pager_alloc_init(OBJT_SWAP, handle, cred, 814 size, offset); 815 } 816 return (object); 817 } 818 819 /* 820 * SWAP_PAGER_DEALLOC() - remove swap metadata from object 821 * 822 * The swap backing for the object is destroyed. The code is 823 * designed such that we can reinstantiate it later, but this 824 * routine is typically called only when the entire object is 825 * about to be destroyed. 826 * 827 * The object must be locked. 828 */ 829 static void 830 swap_pager_dealloc(vm_object_t object) 831 { 832 833 VM_OBJECT_ASSERT_WLOCKED(object); 834 KASSERT((object->flags & OBJ_DEAD) != 0, ("dealloc of reachable obj")); 835 836 /* 837 * Remove from list right away so lookups will fail if we block for 838 * pageout completion. 839 */ 840 if ((object->flags & OBJ_ANON) == 0 && object->handle != NULL) { 841 VM_OBJECT_WUNLOCK(object); 842 sx_xlock(&sw_alloc_sx); 843 TAILQ_REMOVE(NOBJLIST(object->handle), object, 844 pager_object_list); 845 sx_xunlock(&sw_alloc_sx); 846 VM_OBJECT_WLOCK(object); 847 } 848 849 vm_object_pip_wait(object, "swpdea"); 850 851 /* 852 * Free all remaining metadata. We only bother to free it from 853 * the swap meta data. We do not attempt to free swapblk's still 854 * associated with vm_page_t's for this object. We do not care 855 * if paging is still in progress on some objects. 856 */ 857 swp_pager_meta_free_all(object); 858 object->handle = NULL; 859 object->type = OBJT_DEAD; 860 861 /* 862 * Release the allocation charge. 863 */ 864 if (object->cred != NULL) { 865 swap_release_by_cred(object->charge, object->cred); 866 object->charge = 0; 867 crfree(object->cred); 868 object->cred = NULL; 869 } 870 871 /* 872 * Hide the object from swap_pager_swapoff(). 873 */ 874 vm_object_clear_flag(object, OBJ_SWAP); 875 } 876 877 /************************************************************************ 878 * SWAP PAGER BITMAP ROUTINES * 879 ************************************************************************/ 880 881 /* 882 * SWP_PAGER_GETSWAPSPACE() - allocate raw swap space 883 * 884 * Allocate swap for up to the requested number of pages. The 885 * starting swap block number (a page index) is returned or 886 * SWAPBLK_NONE if the allocation failed. 887 * 888 * Also has the side effect of advising that somebody made a mistake 889 * when they configured swap and didn't configure enough. 890 * 891 * This routine may not sleep. 892 * 893 * We allocate in round-robin fashion from the configured devices. 894 */ 895 static daddr_t 896 swp_pager_getswapspace(int *io_npages) 897 { 898 daddr_t blk; 899 struct swdevt *sp; 900 int mpages, npages; 901 902 KASSERT(*io_npages >= 1, 903 ("%s: npages not positive", __func__)); 904 blk = SWAPBLK_NONE; 905 mpages = *io_npages; 906 npages = imin(BLIST_MAX_ALLOC, mpages); 907 mtx_lock(&sw_dev_mtx); 908 sp = swdevhd; 909 while (!TAILQ_EMPTY(&swtailq)) { 910 if (sp == NULL) 911 sp = TAILQ_FIRST(&swtailq); 912 if ((sp->sw_flags & SW_CLOSING) == 0) 913 blk = blist_alloc(sp->sw_blist, &npages, mpages); 914 if (blk != SWAPBLK_NONE) 915 break; 916 sp = TAILQ_NEXT(sp, sw_list); 917 if (swdevhd == sp) { 918 if (npages == 1) 919 break; 920 mpages = npages - 1; 921 npages >>= 1; 922 } 923 } 924 if (blk != SWAPBLK_NONE) { 925 *io_npages = npages; 926 blk += sp->sw_first; 927 sp->sw_used += npages; 928 swap_pager_avail -= npages; 929 swp_sizecheck(); 930 swdevhd = TAILQ_NEXT(sp, sw_list); 931 } else { 932 if (swap_pager_full != 2) { 933 printf("swp_pager_getswapspace(%d): failed\n", 934 *io_npages); 935 swap_pager_full = 2; 936 swap_pager_almost_full = 1; 937 } 938 swdevhd = NULL; 939 } 940 mtx_unlock(&sw_dev_mtx); 941 return (blk); 942 } 943 944 static bool 945 swp_pager_isondev(daddr_t blk, struct swdevt *sp) 946 { 947 948 return (blk >= sp->sw_first && blk < sp->sw_end); 949 } 950 951 static void 952 swp_pager_strategy(struct buf *bp) 953 { 954 struct swdevt *sp; 955 956 mtx_lock(&sw_dev_mtx); 957 TAILQ_FOREACH(sp, &swtailq, sw_list) { 958 if (swp_pager_isondev(bp->b_blkno, sp)) { 959 mtx_unlock(&sw_dev_mtx); 960 if ((sp->sw_flags & SW_UNMAPPED) != 0 && 961 unmapped_buf_allowed) { 962 bp->b_data = unmapped_buf; 963 bp->b_offset = 0; 964 } else { 965 pmap_qenter((vm_offset_t)bp->b_data, 966 &bp->b_pages[0], bp->b_bcount / PAGE_SIZE); 967 } 968 sp->sw_strategy(bp, sp); 969 return; 970 } 971 } 972 panic("Swapdev not found"); 973 } 974 975 /* 976 * SWP_PAGER_FREESWAPSPACE() - free raw swap space 977 * 978 * This routine returns the specified swap blocks back to the bitmap. 979 * 980 * This routine may not sleep. 981 */ 982 static void 983 swp_pager_freeswapspace(const struct page_range *range) 984 { 985 daddr_t blk, npages; 986 struct swdevt *sp; 987 988 blk = range->start; 989 npages = range->num; 990 if (npages == 0) 991 return; 992 mtx_lock(&sw_dev_mtx); 993 TAILQ_FOREACH(sp, &swtailq, sw_list) { 994 if (swp_pager_isondev(blk, sp)) { 995 sp->sw_used -= npages; 996 /* 997 * If we are attempting to stop swapping on 998 * this device, we don't want to mark any 999 * blocks free lest they be reused. 1000 */ 1001 if ((sp->sw_flags & SW_CLOSING) == 0) { 1002 blist_free(sp->sw_blist, blk - sp->sw_first, 1003 npages); 1004 swap_pager_avail += npages; 1005 swp_sizecheck(); 1006 } 1007 mtx_unlock(&sw_dev_mtx); 1008 return; 1009 } 1010 } 1011 panic("Swapdev not found"); 1012 } 1013 1014 /* 1015 * SYSCTL_SWAP_FRAGMENTATION() - produce raw swap space stats 1016 */ 1017 static int 1018 sysctl_swap_fragmentation(SYSCTL_HANDLER_ARGS) 1019 { 1020 struct sbuf sbuf; 1021 struct swdevt *sp; 1022 const char *devname; 1023 int error; 1024 1025 error = sysctl_wire_old_buffer(req, 0); 1026 if (error != 0) 1027 return (error); 1028 sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 1029 mtx_lock(&sw_dev_mtx); 1030 TAILQ_FOREACH(sp, &swtailq, sw_list) { 1031 if (vn_isdisk(sp->sw_vp)) 1032 devname = devtoname(sp->sw_vp->v_rdev); 1033 else 1034 devname = "[file]"; 1035 sbuf_printf(&sbuf, "\nFree space on device %s:\n", devname); 1036 blist_stats(sp->sw_blist, &sbuf); 1037 } 1038 mtx_unlock(&sw_dev_mtx); 1039 error = sbuf_finish(&sbuf); 1040 sbuf_delete(&sbuf); 1041 return (error); 1042 } 1043 1044 /* 1045 * SWAP_PAGER_FREESPACE() - frees swap blocks associated with a page 1046 * range within an object. 1047 * 1048 * This routine removes swapblk assignments from swap metadata. 1049 * 1050 * The external callers of this routine typically have already destroyed 1051 * or renamed vm_page_t's associated with this range in the object so 1052 * we should be ok. 1053 * 1054 * The object must be locked. 1055 */ 1056 void 1057 swap_pager_freespace(vm_object_t object, vm_pindex_t start, vm_size_t size, 1058 vm_size_t *freed) 1059 { 1060 MPASS((object->flags & OBJ_SWAP) != 0); 1061 1062 swp_pager_meta_free(object, start, size, freed); 1063 } 1064 1065 static void 1066 swap_pager_freespace_pgo(vm_object_t object, vm_pindex_t start, vm_size_t size) 1067 { 1068 MPASS((object->flags & OBJ_SWAP) != 0); 1069 1070 swp_pager_meta_free(object, start, size, NULL); 1071 } 1072 1073 /* 1074 * SWAP_PAGER_RESERVE() - reserve swap blocks in object 1075 * 1076 * Assigns swap blocks to the specified range within the object. The 1077 * swap blocks are not zeroed. Any previous swap assignment is destroyed. 1078 * 1079 * Returns 0 on success, -1 on failure. 1080 */ 1081 int 1082 swap_pager_reserve(vm_object_t object, vm_pindex_t start, vm_pindex_t size) 1083 { 1084 struct page_range range; 1085 daddr_t addr, blk; 1086 vm_pindex_t i, j; 1087 int n; 1088 1089 swp_pager_init_freerange(&range); 1090 VM_OBJECT_WLOCK(object); 1091 for (i = 0; i < size; i += n) { 1092 n = MIN(size - i, INT_MAX); 1093 blk = swp_pager_getswapspace(&n); 1094 if (blk == SWAPBLK_NONE) { 1095 swp_pager_meta_free(object, start, i, NULL); 1096 VM_OBJECT_WUNLOCK(object); 1097 return (-1); 1098 } 1099 for (j = 0; j < n; ++j) { 1100 addr = swp_pager_meta_build(object, 1101 start + i + j, blk + j, false); 1102 if (addr != SWAPBLK_NONE) 1103 swp_pager_update_freerange(&range, addr); 1104 } 1105 } 1106 swp_pager_freeswapspace(&range); 1107 VM_OBJECT_WUNLOCK(object); 1108 return (0); 1109 } 1110 1111 /* 1112 * SWAP_PAGER_COPY() - copy blocks from source pager to destination pager 1113 * and destroy the source. 1114 * 1115 * Copy any valid swapblks from the source to the destination. In 1116 * cases where both the source and destination have a valid swapblk, 1117 * we keep the destination's. 1118 * 1119 * This routine is allowed to sleep. It may sleep allocating metadata 1120 * indirectly through swp_pager_meta_build(). 1121 * 1122 * The source object contains no vm_page_t's (which is just as well) 1123 * 1124 * The source and destination objects must be locked. 1125 * Both object locks may temporarily be released. 1126 */ 1127 void 1128 swap_pager_copy(vm_object_t srcobject, vm_object_t dstobject, 1129 vm_pindex_t offset, int destroysource) 1130 { 1131 VM_OBJECT_ASSERT_WLOCKED(srcobject); 1132 VM_OBJECT_ASSERT_WLOCKED(dstobject); 1133 1134 /* 1135 * If destroysource is set, we remove the source object from the 1136 * swap_pager internal queue now. 1137 */ 1138 if (destroysource && (srcobject->flags & OBJ_ANON) == 0 && 1139 srcobject->handle != NULL) { 1140 VM_OBJECT_WUNLOCK(srcobject); 1141 VM_OBJECT_WUNLOCK(dstobject); 1142 sx_xlock(&sw_alloc_sx); 1143 TAILQ_REMOVE(NOBJLIST(srcobject->handle), srcobject, 1144 pager_object_list); 1145 sx_xunlock(&sw_alloc_sx); 1146 VM_OBJECT_WLOCK(dstobject); 1147 VM_OBJECT_WLOCK(srcobject); 1148 } 1149 1150 /* 1151 * Transfer source to destination. 1152 */ 1153 swp_pager_meta_transfer(srcobject, dstobject, offset, dstobject->size); 1154 1155 /* 1156 * Free left over swap blocks in source. 1157 */ 1158 if (destroysource) 1159 swp_pager_meta_free_all(srcobject); 1160 } 1161 1162 /* 1163 * SWAP_PAGER_HASPAGE() - determine if we have good backing store for 1164 * the requested page. 1165 * 1166 * We determine whether good backing store exists for the requested 1167 * page and return TRUE if it does, FALSE if it doesn't. 1168 * 1169 * If TRUE, we also try to determine how much valid, contiguous backing 1170 * store exists before and after the requested page. 1171 */ 1172 static boolean_t 1173 swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, 1174 int *after) 1175 { 1176 daddr_t blk, blk0; 1177 int i; 1178 1179 VM_OBJECT_ASSERT_LOCKED(object); 1180 KASSERT((object->flags & OBJ_SWAP) != 0, 1181 ("%s: object not swappable", __func__)); 1182 1183 /* 1184 * do we have good backing store at the requested index ? 1185 */ 1186 blk0 = swp_pager_meta_lookup(object, pindex); 1187 if (blk0 == SWAPBLK_NONE) { 1188 if (before) 1189 *before = 0; 1190 if (after) 1191 *after = 0; 1192 return (FALSE); 1193 } 1194 1195 /* 1196 * find backwards-looking contiguous good backing store 1197 */ 1198 if (before != NULL) { 1199 for (i = 1; i < SWB_NPAGES; i++) { 1200 if (i > pindex) 1201 break; 1202 blk = swp_pager_meta_lookup(object, pindex - i); 1203 if (blk != blk0 - i) 1204 break; 1205 } 1206 *before = i - 1; 1207 } 1208 1209 /* 1210 * find forward-looking contiguous good backing store 1211 */ 1212 if (after != NULL) { 1213 for (i = 1; i < SWB_NPAGES; i++) { 1214 blk = swp_pager_meta_lookup(object, pindex + i); 1215 if (blk != blk0 + i) 1216 break; 1217 } 1218 *after = i - 1; 1219 } 1220 return (TRUE); 1221 } 1222 1223 static void 1224 swap_pager_unswapped_acct(vm_page_t m) 1225 { 1226 KASSERT((m->object->flags & OBJ_SWAP) != 0, 1227 ("Free object not swappable")); 1228 if ((m->a.flags & PGA_SWAP_FREE) != 0) 1229 counter_u64_add(swap_free_completed, 1); 1230 vm_page_aflag_clear(m, PGA_SWAP_FREE | PGA_SWAP_SPACE); 1231 1232 /* 1233 * The meta data only exists if the object is OBJT_SWAP 1234 * and even then might not be allocated yet. 1235 */ 1236 } 1237 1238 /* 1239 * SWAP_PAGER_PAGE_UNSWAPPED() - remove swap backing store related to page 1240 * 1241 * This removes any associated swap backing store, whether valid or 1242 * not, from the page. 1243 * 1244 * This routine is typically called when a page is made dirty, at 1245 * which point any associated swap can be freed. MADV_FREE also 1246 * calls us in a special-case situation 1247 * 1248 * NOTE!!! If the page is clean and the swap was valid, the caller 1249 * should make the page dirty before calling this routine. This routine 1250 * does NOT change the m->dirty status of the page. Also: MADV_FREE 1251 * depends on it. 1252 * 1253 * This routine may not sleep. 1254 * 1255 * The object containing the page may be locked. 1256 */ 1257 static void 1258 swap_pager_unswapped(vm_page_t m) 1259 { 1260 struct page_range range; 1261 struct swblk *sb; 1262 vm_object_t obj; 1263 1264 /* 1265 * Handle enqueing deferred frees first. If we do not have the 1266 * object lock we wait for the page daemon to clear the space. 1267 */ 1268 obj = m->object; 1269 if (!VM_OBJECT_WOWNED(obj)) { 1270 VM_PAGE_OBJECT_BUSY_ASSERT(m); 1271 /* 1272 * The caller is responsible for synchronization but we 1273 * will harmlessly handle races. This is typically provided 1274 * by only calling unswapped() when a page transitions from 1275 * clean to dirty. 1276 */ 1277 if ((m->a.flags & (PGA_SWAP_SPACE | PGA_SWAP_FREE)) == 1278 PGA_SWAP_SPACE) { 1279 vm_page_aflag_set(m, PGA_SWAP_FREE); 1280 counter_u64_add(swap_free_deferred, 1); 1281 } 1282 return; 1283 } 1284 swap_pager_unswapped_acct(m); 1285 1286 sb = swblk_lookup(m->object, m->pindex); 1287 if (sb == NULL) 1288 return; 1289 range.start = sb->d[m->pindex % SWAP_META_PAGES]; 1290 if (range.start == SWAPBLK_NONE) 1291 return; 1292 range.num = 1; 1293 swp_pager_freeswapspace(&range); 1294 sb->d[m->pindex % SWAP_META_PAGES] = SWAPBLK_NONE; 1295 swp_pager_free_empty_swblk(m->object, sb); 1296 } 1297 1298 /* 1299 * swap_pager_getpages() - bring pages in from swap 1300 * 1301 * Attempt to page in the pages in array "ma" of length "count". The 1302 * caller may optionally specify that additional pages preceding and 1303 * succeeding the specified range be paged in. The number of such pages 1304 * is returned in the "rbehind" and "rahead" parameters, and they will 1305 * be in the inactive queue upon return. 1306 * 1307 * The pages in "ma" must be busied and will remain busied upon return. 1308 */ 1309 static int 1310 swap_pager_getpages_locked(vm_object_t object, vm_page_t *ma, int count, 1311 int *rbehind, int *rahead) 1312 { 1313 struct buf *bp; 1314 vm_page_t bm, mpred, msucc, p; 1315 vm_pindex_t pindex; 1316 daddr_t blk; 1317 int i, maxahead, maxbehind, reqcount; 1318 1319 VM_OBJECT_ASSERT_WLOCKED(object); 1320 reqcount = count; 1321 1322 KASSERT((object->flags & OBJ_SWAP) != 0, 1323 ("%s: object not swappable", __func__)); 1324 if (!swap_pager_haspage(object, ma[0]->pindex, &maxbehind, &maxahead)) { 1325 VM_OBJECT_WUNLOCK(object); 1326 return (VM_PAGER_FAIL); 1327 } 1328 1329 KASSERT(reqcount - 1 <= maxahead, 1330 ("page count %d extends beyond swap block", reqcount)); 1331 1332 /* 1333 * Do not transfer any pages other than those that are xbusied 1334 * when running during a split or collapse operation. This 1335 * prevents clustering from re-creating pages which are being 1336 * moved into another object. 1337 */ 1338 if ((object->flags & (OBJ_SPLIT | OBJ_DEAD)) != 0) { 1339 maxahead = reqcount - 1; 1340 maxbehind = 0; 1341 } 1342 1343 /* 1344 * Clip the readahead and readbehind ranges to exclude resident pages. 1345 */ 1346 if (rahead != NULL) { 1347 *rahead = imin(*rahead, maxahead - (reqcount - 1)); 1348 pindex = ma[reqcount - 1]->pindex; 1349 msucc = TAILQ_NEXT(ma[reqcount - 1], listq); 1350 if (msucc != NULL && msucc->pindex - pindex - 1 < *rahead) 1351 *rahead = msucc->pindex - pindex - 1; 1352 } 1353 if (rbehind != NULL) { 1354 *rbehind = imin(*rbehind, maxbehind); 1355 pindex = ma[0]->pindex; 1356 mpred = TAILQ_PREV(ma[0], pglist, listq); 1357 if (mpred != NULL && pindex - mpred->pindex - 1 < *rbehind) 1358 *rbehind = pindex - mpred->pindex - 1; 1359 } 1360 1361 bm = ma[0]; 1362 for (i = 0; i < count; i++) 1363 ma[i]->oflags |= VPO_SWAPINPROG; 1364 1365 /* 1366 * Allocate readahead and readbehind pages. 1367 */ 1368 if (rbehind != NULL) { 1369 for (i = 1; i <= *rbehind; i++) { 1370 p = vm_page_alloc(object, ma[0]->pindex - i, 1371 VM_ALLOC_NORMAL); 1372 if (p == NULL) 1373 break; 1374 p->oflags |= VPO_SWAPINPROG; 1375 bm = p; 1376 } 1377 *rbehind = i - 1; 1378 } 1379 if (rahead != NULL) { 1380 for (i = 0; i < *rahead; i++) { 1381 p = vm_page_alloc(object, 1382 ma[reqcount - 1]->pindex + i + 1, VM_ALLOC_NORMAL); 1383 if (p == NULL) 1384 break; 1385 p->oflags |= VPO_SWAPINPROG; 1386 } 1387 *rahead = i; 1388 } 1389 if (rbehind != NULL) 1390 count += *rbehind; 1391 if (rahead != NULL) 1392 count += *rahead; 1393 1394 vm_object_pip_add(object, count); 1395 1396 pindex = bm->pindex; 1397 blk = swp_pager_meta_lookup(object, pindex); 1398 KASSERT(blk != SWAPBLK_NONE, 1399 ("no swap blocking containing %p(%jx)", object, (uintmax_t)pindex)); 1400 1401 VM_OBJECT_WUNLOCK(object); 1402 bp = uma_zalloc(swrbuf_zone, M_WAITOK); 1403 MPASS((bp->b_flags & B_MAXPHYS) != 0); 1404 /* Pages cannot leave the object while busy. */ 1405 for (i = 0, p = bm; i < count; i++, p = TAILQ_NEXT(p, listq)) { 1406 MPASS(p->pindex == bm->pindex + i); 1407 bp->b_pages[i] = p; 1408 } 1409 1410 bp->b_flags |= B_PAGING; 1411 bp->b_iocmd = BIO_READ; 1412 bp->b_iodone = swp_pager_async_iodone; 1413 bp->b_rcred = crhold(thread0.td_ucred); 1414 bp->b_wcred = crhold(thread0.td_ucred); 1415 bp->b_blkno = blk; 1416 bp->b_bcount = PAGE_SIZE * count; 1417 bp->b_bufsize = PAGE_SIZE * count; 1418 bp->b_npages = count; 1419 bp->b_pgbefore = rbehind != NULL ? *rbehind : 0; 1420 bp->b_pgafter = rahead != NULL ? *rahead : 0; 1421 1422 VM_CNT_INC(v_swapin); 1423 VM_CNT_ADD(v_swappgsin, count); 1424 1425 /* 1426 * perform the I/O. NOTE!!! bp cannot be considered valid after 1427 * this point because we automatically release it on completion. 1428 * Instead, we look at the one page we are interested in which we 1429 * still hold a lock on even through the I/O completion. 1430 * 1431 * The other pages in our ma[] array are also released on completion, 1432 * so we cannot assume they are valid anymore either. 1433 * 1434 * NOTE: b_blkno is destroyed by the call to swapdev_strategy 1435 */ 1436 BUF_KERNPROC(bp); 1437 swp_pager_strategy(bp); 1438 1439 /* 1440 * Wait for the pages we want to complete. VPO_SWAPINPROG is always 1441 * cleared on completion. If an I/O error occurs, SWAPBLK_NONE 1442 * is set in the metadata for each page in the request. 1443 */ 1444 VM_OBJECT_WLOCK(object); 1445 /* This could be implemented more efficiently with aflags */ 1446 while ((ma[0]->oflags & VPO_SWAPINPROG) != 0) { 1447 ma[0]->oflags |= VPO_SWAPSLEEP; 1448 VM_CNT_INC(v_intrans); 1449 if (VM_OBJECT_SLEEP(object, &object->handle, PSWP, 1450 "swread", hz * 20)) { 1451 printf( 1452 "swap_pager: indefinite wait buffer: bufobj: %p, blkno: %jd, size: %ld\n", 1453 bp->b_bufobj, (intmax_t)bp->b_blkno, bp->b_bcount); 1454 } 1455 } 1456 VM_OBJECT_WUNLOCK(object); 1457 1458 /* 1459 * If we had an unrecoverable read error pages will not be valid. 1460 */ 1461 for (i = 0; i < reqcount; i++) 1462 if (ma[i]->valid != VM_PAGE_BITS_ALL) 1463 return (VM_PAGER_ERROR); 1464 1465 return (VM_PAGER_OK); 1466 1467 /* 1468 * A final note: in a low swap situation, we cannot deallocate swap 1469 * and mark a page dirty here because the caller is likely to mark 1470 * the page clean when we return, causing the page to possibly revert 1471 * to all-zero's later. 1472 */ 1473 } 1474 1475 static int 1476 swap_pager_getpages(vm_object_t object, vm_page_t *ma, int count, 1477 int *rbehind, int *rahead) 1478 { 1479 1480 VM_OBJECT_WLOCK(object); 1481 return (swap_pager_getpages_locked(object, ma, count, rbehind, rahead)); 1482 } 1483 1484 /* 1485 * swap_pager_getpages_async(): 1486 * 1487 * Right now this is emulation of asynchronous operation on top of 1488 * swap_pager_getpages(). 1489 */ 1490 static int 1491 swap_pager_getpages_async(vm_object_t object, vm_page_t *ma, int count, 1492 int *rbehind, int *rahead, pgo_getpages_iodone_t iodone, void *arg) 1493 { 1494 int r, error; 1495 1496 r = swap_pager_getpages(object, ma, count, rbehind, rahead); 1497 switch (r) { 1498 case VM_PAGER_OK: 1499 error = 0; 1500 break; 1501 case VM_PAGER_ERROR: 1502 error = EIO; 1503 break; 1504 case VM_PAGER_FAIL: 1505 error = EINVAL; 1506 break; 1507 default: 1508 panic("unhandled swap_pager_getpages() error %d", r); 1509 } 1510 (iodone)(arg, ma, count, error); 1511 1512 return (r); 1513 } 1514 1515 /* 1516 * swap_pager_putpages: 1517 * 1518 * Assign swap (if necessary) and initiate I/O on the specified pages. 1519 * 1520 * In a low memory situation we may block in VOP_STRATEGY(), but the new 1521 * vm_page reservation system coupled with properly written VFS devices 1522 * should ensure that no low-memory deadlock occurs. This is an area 1523 * which needs work. 1524 * 1525 * The parent has N vm_object_pip_add() references prior to 1526 * calling us and will remove references for rtvals[] that are 1527 * not set to VM_PAGER_PEND. We need to remove the rest on I/O 1528 * completion. 1529 * 1530 * The parent has soft-busy'd the pages it passes us and will unbusy 1531 * those whose rtvals[] entry is not set to VM_PAGER_PEND on return. 1532 * We need to unbusy the rest on I/O completion. 1533 */ 1534 static void 1535 swap_pager_putpages(vm_object_t object, vm_page_t *ma, int count, 1536 int flags, int *rtvals) 1537 { 1538 struct page_range range; 1539 struct buf *bp; 1540 daddr_t addr, blk; 1541 vm_page_t mreq; 1542 int i, j, n; 1543 bool async; 1544 1545 KASSERT(count == 0 || ma[0]->object == object, 1546 ("%s: object mismatch %p/%p", 1547 __func__, object, ma[0]->object)); 1548 1549 VM_OBJECT_WUNLOCK(object); 1550 async = curproc == pageproc && (flags & VM_PAGER_PUT_SYNC) == 0; 1551 swp_pager_init_freerange(&range); 1552 1553 /* 1554 * Assign swap blocks and issue I/O. We reallocate swap on the fly. 1555 * The page is left dirty until the pageout operation completes 1556 * successfully. 1557 */ 1558 for (i = 0; i < count; i += n) { 1559 /* Maximum I/O size is limited by maximum swap block size. */ 1560 n = min(count - i, nsw_cluster_max); 1561 1562 if (async) { 1563 mtx_lock(&swbuf_mtx); 1564 while (nsw_wcount_async == 0) 1565 msleep(&nsw_wcount_async, &swbuf_mtx, PVM, 1566 "swbufa", 0); 1567 nsw_wcount_async--; 1568 mtx_unlock(&swbuf_mtx); 1569 } 1570 1571 /* Get a block of swap of size up to size n. */ 1572 blk = swp_pager_getswapspace(&n); 1573 if (blk == SWAPBLK_NONE) { 1574 mtx_lock(&swbuf_mtx); 1575 if (++nsw_wcount_async == 1) 1576 wakeup(&nsw_wcount_async); 1577 mtx_unlock(&swbuf_mtx); 1578 for (j = 0; j < n; ++j) 1579 rtvals[i + j] = VM_PAGER_FAIL; 1580 continue; 1581 } 1582 VM_OBJECT_WLOCK(object); 1583 for (j = 0; j < n; ++j) { 1584 mreq = ma[i + j]; 1585 vm_page_aflag_clear(mreq, PGA_SWAP_FREE); 1586 addr = swp_pager_meta_build(mreq->object, mreq->pindex, 1587 blk + j, false); 1588 if (addr != SWAPBLK_NONE) 1589 swp_pager_update_freerange(&range, addr); 1590 MPASS(mreq->dirty == VM_PAGE_BITS_ALL); 1591 mreq->oflags |= VPO_SWAPINPROG; 1592 } 1593 VM_OBJECT_WUNLOCK(object); 1594 1595 bp = uma_zalloc(swwbuf_zone, M_WAITOK); 1596 MPASS((bp->b_flags & B_MAXPHYS) != 0); 1597 if (async) 1598 bp->b_flags |= B_ASYNC; 1599 bp->b_flags |= B_PAGING; 1600 bp->b_iocmd = BIO_WRITE; 1601 1602 bp->b_rcred = crhold(thread0.td_ucred); 1603 bp->b_wcred = crhold(thread0.td_ucred); 1604 bp->b_bcount = PAGE_SIZE * n; 1605 bp->b_bufsize = PAGE_SIZE * n; 1606 bp->b_blkno = blk; 1607 for (j = 0; j < n; j++) 1608 bp->b_pages[j] = ma[i + j]; 1609 bp->b_npages = n; 1610 1611 /* 1612 * Must set dirty range for NFS to work. 1613 */ 1614 bp->b_dirtyoff = 0; 1615 bp->b_dirtyend = bp->b_bcount; 1616 1617 VM_CNT_INC(v_swapout); 1618 VM_CNT_ADD(v_swappgsout, bp->b_npages); 1619 1620 /* 1621 * We unconditionally set rtvals[] to VM_PAGER_PEND so that we 1622 * can call the async completion routine at the end of a 1623 * synchronous I/O operation. Otherwise, our caller would 1624 * perform duplicate unbusy and wakeup operations on the page 1625 * and object, respectively. 1626 */ 1627 for (j = 0; j < n; j++) 1628 rtvals[i + j] = VM_PAGER_PEND; 1629 1630 /* 1631 * asynchronous 1632 * 1633 * NOTE: b_blkno is destroyed by the call to swapdev_strategy. 1634 */ 1635 if (async) { 1636 bp->b_iodone = swp_pager_async_iodone; 1637 BUF_KERNPROC(bp); 1638 swp_pager_strategy(bp); 1639 continue; 1640 } 1641 1642 /* 1643 * synchronous 1644 * 1645 * NOTE: b_blkno is destroyed by the call to swapdev_strategy. 1646 */ 1647 bp->b_iodone = bdone; 1648 swp_pager_strategy(bp); 1649 1650 /* 1651 * Wait for the sync I/O to complete. 1652 */ 1653 bwait(bp, PVM, "swwrt"); 1654 1655 /* 1656 * Now that we are through with the bp, we can call the 1657 * normal async completion, which frees everything up. 1658 */ 1659 swp_pager_async_iodone(bp); 1660 } 1661 swp_pager_freeswapspace(&range); 1662 VM_OBJECT_WLOCK(object); 1663 } 1664 1665 /* 1666 * swp_pager_async_iodone: 1667 * 1668 * Completion routine for asynchronous reads and writes from/to swap. 1669 * Also called manually by synchronous code to finish up a bp. 1670 * 1671 * This routine may not sleep. 1672 */ 1673 static void 1674 swp_pager_async_iodone(struct buf *bp) 1675 { 1676 int i; 1677 vm_object_t object = NULL; 1678 1679 /* 1680 * Report error - unless we ran out of memory, in which case 1681 * we've already logged it in swapgeom_strategy(). 1682 */ 1683 if (bp->b_ioflags & BIO_ERROR && bp->b_error != ENOMEM) { 1684 printf( 1685 "swap_pager: I/O error - %s failed; blkno %ld," 1686 "size %ld, error %d\n", 1687 ((bp->b_iocmd == BIO_READ) ? "pagein" : "pageout"), 1688 (long)bp->b_blkno, 1689 (long)bp->b_bcount, 1690 bp->b_error 1691 ); 1692 } 1693 1694 /* 1695 * remove the mapping for kernel virtual 1696 */ 1697 if (buf_mapped(bp)) 1698 pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages); 1699 else 1700 bp->b_data = bp->b_kvabase; 1701 1702 if (bp->b_npages) { 1703 object = bp->b_pages[0]->object; 1704 VM_OBJECT_WLOCK(object); 1705 } 1706 1707 /* 1708 * cleanup pages. If an error occurs writing to swap, we are in 1709 * very serious trouble. If it happens to be a disk error, though, 1710 * we may be able to recover by reassigning the swap later on. So 1711 * in this case we remove the m->swapblk assignment for the page 1712 * but do not free it in the rlist. The errornous block(s) are thus 1713 * never reallocated as swap. Redirty the page and continue. 1714 */ 1715 for (i = 0; i < bp->b_npages; ++i) { 1716 vm_page_t m = bp->b_pages[i]; 1717 1718 m->oflags &= ~VPO_SWAPINPROG; 1719 if (m->oflags & VPO_SWAPSLEEP) { 1720 m->oflags &= ~VPO_SWAPSLEEP; 1721 wakeup(&object->handle); 1722 } 1723 1724 /* We always have space after I/O, successful or not. */ 1725 vm_page_aflag_set(m, PGA_SWAP_SPACE); 1726 1727 if (bp->b_ioflags & BIO_ERROR) { 1728 /* 1729 * If an error occurs I'd love to throw the swapblk 1730 * away without freeing it back to swapspace, so it 1731 * can never be used again. But I can't from an 1732 * interrupt. 1733 */ 1734 if (bp->b_iocmd == BIO_READ) { 1735 /* 1736 * NOTE: for reads, m->dirty will probably 1737 * be overridden by the original caller of 1738 * getpages so don't play cute tricks here. 1739 */ 1740 vm_page_invalid(m); 1741 if (i < bp->b_pgbefore || 1742 i >= bp->b_npages - bp->b_pgafter) 1743 vm_page_free_invalid(m); 1744 } else { 1745 /* 1746 * If a write error occurs, reactivate page 1747 * so it doesn't clog the inactive list, 1748 * then finish the I/O. 1749 */ 1750 MPASS(m->dirty == VM_PAGE_BITS_ALL); 1751 1752 /* PQ_UNSWAPPABLE? */ 1753 vm_page_activate(m); 1754 vm_page_sunbusy(m); 1755 } 1756 } else if (bp->b_iocmd == BIO_READ) { 1757 /* 1758 * NOTE: for reads, m->dirty will probably be 1759 * overridden by the original caller of getpages so 1760 * we cannot set them in order to free the underlying 1761 * swap in a low-swap situation. I don't think we'd 1762 * want to do that anyway, but it was an optimization 1763 * that existed in the old swapper for a time before 1764 * it got ripped out due to precisely this problem. 1765 */ 1766 KASSERT(!pmap_page_is_mapped(m), 1767 ("swp_pager_async_iodone: page %p is mapped", m)); 1768 KASSERT(m->dirty == 0, 1769 ("swp_pager_async_iodone: page %p is dirty", m)); 1770 1771 vm_page_valid(m); 1772 if (i < bp->b_pgbefore || 1773 i >= bp->b_npages - bp->b_pgafter) 1774 vm_page_readahead_finish(m); 1775 } else { 1776 /* 1777 * For write success, clear the dirty 1778 * status, then finish the I/O ( which decrements the 1779 * busy count and possibly wakes waiter's up ). 1780 * A page is only written to swap after a period of 1781 * inactivity. Therefore, we do not expect it to be 1782 * reused. 1783 */ 1784 KASSERT(!pmap_page_is_write_mapped(m), 1785 ("swp_pager_async_iodone: page %p is not write" 1786 " protected", m)); 1787 vm_page_undirty(m); 1788 vm_page_deactivate_noreuse(m); 1789 vm_page_sunbusy(m); 1790 } 1791 } 1792 1793 /* 1794 * adjust pip. NOTE: the original parent may still have its own 1795 * pip refs on the object. 1796 */ 1797 if (object != NULL) { 1798 vm_object_pip_wakeupn(object, bp->b_npages); 1799 VM_OBJECT_WUNLOCK(object); 1800 } 1801 1802 /* 1803 * swapdev_strategy() manually sets b_vp and b_bufobj before calling 1804 * bstrategy(). Set them back to NULL now we're done with it, or we'll 1805 * trigger a KASSERT in relpbuf(). 1806 */ 1807 if (bp->b_vp) { 1808 bp->b_vp = NULL; 1809 bp->b_bufobj = NULL; 1810 } 1811 /* 1812 * release the physical I/O buffer 1813 */ 1814 if (bp->b_flags & B_ASYNC) { 1815 mtx_lock(&swbuf_mtx); 1816 if (++nsw_wcount_async == 1) 1817 wakeup(&nsw_wcount_async); 1818 mtx_unlock(&swbuf_mtx); 1819 } 1820 uma_zfree((bp->b_iocmd == BIO_READ) ? swrbuf_zone : swwbuf_zone, bp); 1821 } 1822 1823 int 1824 swap_pager_nswapdev(void) 1825 { 1826 1827 return (nswapdev); 1828 } 1829 1830 static void 1831 swp_pager_force_dirty(struct page_range *range, vm_page_t m, daddr_t *blk) 1832 { 1833 vm_page_dirty(m); 1834 swap_pager_unswapped_acct(m); 1835 swp_pager_update_freerange(range, *blk); 1836 *blk = SWAPBLK_NONE; 1837 vm_page_launder(m); 1838 } 1839 1840 u_long 1841 swap_pager_swapped_pages(vm_object_t object) 1842 { 1843 struct pctrie_iter blks; 1844 struct swblk *sb; 1845 u_long res; 1846 int i; 1847 1848 VM_OBJECT_ASSERT_LOCKED(object); 1849 1850 if (swblk_is_empty(object)) 1851 return (0); 1852 1853 res = 0; 1854 for (sb = swblk_iter(&blks, object, 0); sb != NULL; 1855 sb = swblk_iter_next(&blks)) { 1856 for (i = 0; i < SWAP_META_PAGES; i++) { 1857 if (sb->d[i] != SWAPBLK_NONE) 1858 res++; 1859 } 1860 } 1861 return (res); 1862 } 1863 1864 /* 1865 * swap_pager_swapoff_object: 1866 * 1867 * Page in all of the pages that have been paged out for an object 1868 * to a swap device. 1869 */ 1870 static void 1871 swap_pager_swapoff_object(struct swdevt *sp, vm_object_t object) 1872 { 1873 struct pctrie_iter blks, pages; 1874 struct page_range range; 1875 struct swblk *sb; 1876 vm_page_t m; 1877 int i, rahead, rv; 1878 bool sb_empty; 1879 1880 VM_OBJECT_ASSERT_WLOCKED(object); 1881 KASSERT((object->flags & OBJ_SWAP) != 0, 1882 ("%s: Object not swappable", __func__)); 1883 KASSERT((sp->sw_flags & SW_CLOSING) != 0, 1884 ("%s: Device not blocking further allocations", __func__)); 1885 1886 vm_page_iter_init(&pages, object); 1887 swp_pager_init_freerange(&range); 1888 sb = swblk_iter(&blks, object, 0); 1889 while (sb != NULL) { 1890 if ((object->flags & OBJ_DEAD) != 0) { 1891 /* 1892 * Make sure that pending writes finish before 1893 * returning. 1894 */ 1895 vm_object_pip_wait(object, "swpoff"); 1896 swp_pager_meta_free_all(object); 1897 break; 1898 } 1899 sb_empty = true; 1900 for (i = 0; i < SWAP_META_PAGES; i++) { 1901 /* Skip an invalid block. */ 1902 if (sb->d[i] == SWAPBLK_NONE) 1903 continue; 1904 /* Skip a block not of this device. */ 1905 if (!swp_pager_isondev(sb->d[i], sp)) { 1906 sb_empty = false; 1907 continue; 1908 } 1909 1910 /* 1911 * Look for a page corresponding to this block. If the 1912 * found page has pending operations, sleep and restart 1913 * the scan. 1914 */ 1915 m = vm_page_iter_lookup(&pages, blks.index + i); 1916 if (m != NULL && (m->oflags & VPO_SWAPINPROG) != 0) { 1917 m->oflags |= VPO_SWAPSLEEP; 1918 VM_OBJECT_SLEEP(object, &object->handle, PSWP, 1919 "swpoff", 0); 1920 break; 1921 } 1922 1923 /* 1924 * If the found page is valid, mark it dirty and free 1925 * the swap block. 1926 */ 1927 if (m != NULL && vm_page_all_valid(m)) { 1928 swp_pager_force_dirty(&range, m, &sb->d[i]); 1929 continue; 1930 } 1931 /* Is there a page we can acquire or allocate? */ 1932 if (m != NULL) { 1933 if (!vm_page_busy_acquire(m, VM_ALLOC_WAITFAIL)) 1934 break; 1935 } else if ((m = vm_page_alloc(object, blks.index + i, 1936 VM_ALLOC_NORMAL | VM_ALLOC_WAITFAIL)) == NULL) 1937 break; 1938 1939 /* Get the page from swap, and restart the scan. */ 1940 vm_object_pip_add(object, 1); 1941 rahead = SWAP_META_PAGES; 1942 rv = swap_pager_getpages_locked(object, &m, 1, 1943 NULL, &rahead); 1944 if (rv != VM_PAGER_OK) 1945 panic("%s: read from swap failed: %d", 1946 __func__, rv); 1947 VM_OBJECT_WLOCK(object); 1948 vm_object_pip_wakeupn(object, 1); 1949 KASSERT(vm_page_all_valid(m), 1950 ("%s: Page %p not all valid", __func__, m)); 1951 vm_page_xunbusy(m); 1952 break; 1953 } 1954 if (i < SWAP_META_PAGES) { 1955 /* 1956 * With the object lock released and regained, the 1957 * swapblk could have been freed, so reset the pages 1958 * iterator and search again for the first swblk at or 1959 * after blks.index. 1960 */ 1961 pctrie_iter_reset(&pages); 1962 sb = swblk_iter(&blks, object, blks.index); 1963 continue; 1964 } 1965 if (sb_empty) { 1966 swblk_iter_remove(&blks); 1967 uma_zfree(swblk_zone, sb); 1968 } 1969 1970 /* 1971 * It is safe to advance to the next block. No allocations 1972 * before blk.index have happened, even with the lock released, 1973 * because allocations on this device are blocked. 1974 */ 1975 sb = swblk_iter_next(&blks); 1976 } 1977 swp_pager_freeswapspace(&range); 1978 } 1979 1980 /* 1981 * swap_pager_swapoff: 1982 * 1983 * Page in all of the pages that have been paged out to the 1984 * given device. The corresponding blocks in the bitmap must be 1985 * marked as allocated and the device must be flagged SW_CLOSING. 1986 * There may be no processes swapped out to the device. 1987 * 1988 * This routine may block. 1989 */ 1990 static void 1991 swap_pager_swapoff(struct swdevt *sp) 1992 { 1993 vm_object_t object; 1994 int retries; 1995 1996 sx_assert(&swdev_syscall_lock, SA_XLOCKED); 1997 1998 retries = 0; 1999 full_rescan: 2000 mtx_lock(&vm_object_list_mtx); 2001 TAILQ_FOREACH(object, &vm_object_list, object_list) { 2002 if ((object->flags & OBJ_SWAP) == 0) 2003 continue; 2004 mtx_unlock(&vm_object_list_mtx); 2005 /* Depends on type-stability. */ 2006 VM_OBJECT_WLOCK(object); 2007 2008 /* 2009 * Dead objects are eventually terminated on their own. 2010 */ 2011 if ((object->flags & OBJ_DEAD) != 0) 2012 goto next_obj; 2013 2014 /* 2015 * Sync with fences placed after pctrie 2016 * initialization. We must not access pctrie below 2017 * unless we checked that our object is swap and not 2018 * dead. 2019 */ 2020 atomic_thread_fence_acq(); 2021 if ((object->flags & OBJ_SWAP) == 0) 2022 goto next_obj; 2023 2024 swap_pager_swapoff_object(sp, object); 2025 next_obj: 2026 VM_OBJECT_WUNLOCK(object); 2027 mtx_lock(&vm_object_list_mtx); 2028 } 2029 mtx_unlock(&vm_object_list_mtx); 2030 2031 if (sp->sw_used) { 2032 /* 2033 * Objects may be locked or paging to the device being 2034 * removed, so we will miss their pages and need to 2035 * make another pass. We have marked this device as 2036 * SW_CLOSING, so the activity should finish soon. 2037 */ 2038 retries++; 2039 if (retries > 100) { 2040 panic("swapoff: failed to locate %d swap blocks", 2041 sp->sw_used); 2042 } 2043 pause("swpoff", hz / 20); 2044 goto full_rescan; 2045 } 2046 EVENTHANDLER_INVOKE(swapoff, sp); 2047 } 2048 2049 /************************************************************************ 2050 * SWAP META DATA * 2051 ************************************************************************ 2052 * 2053 * These routines manipulate the swap metadata stored in the 2054 * OBJT_SWAP object. 2055 * 2056 * Swap metadata is implemented with a global hash and not directly 2057 * linked into the object. Instead the object simply contains 2058 * appropriate tracking counters. 2059 */ 2060 2061 /* 2062 * SWP_PAGER_SWBLK_EMPTY() - is a range of blocks free? 2063 */ 2064 static bool 2065 swp_pager_swblk_empty(struct swblk *sb, int start, int limit) 2066 { 2067 int i; 2068 2069 MPASS(0 <= start && start <= limit && limit <= SWAP_META_PAGES); 2070 for (i = start; i < limit; i++) { 2071 if (sb->d[i] != SWAPBLK_NONE) 2072 return (false); 2073 } 2074 return (true); 2075 } 2076 2077 /* 2078 * SWP_PAGER_FREE_EMPTY_SWBLK() - frees if a block is free 2079 * 2080 * Nothing is done if the block is still in use. 2081 */ 2082 static void 2083 swp_pager_free_empty_swblk(vm_object_t object, struct swblk *sb) 2084 { 2085 2086 if (swp_pager_swblk_empty(sb, 0, SWAP_META_PAGES)) { 2087 swblk_lookup_remove(object, sb); 2088 uma_zfree(swblk_zone, sb); 2089 } 2090 } 2091 2092 /* 2093 * SWP_PAGER_META_BUILD() - add swap block to swap meta data for object 2094 * 2095 * Try to add the specified swapblk to the object's swap metadata. If 2096 * nowait_noreplace is set, add the specified swapblk only if there is no 2097 * previously assigned swapblk at pindex. If the swapblk is invalid, and 2098 * replaces a valid swapblk, empty swap metadata is freed. If memory 2099 * allocation fails, and nowait_noreplace is set, return the specified 2100 * swapblk immediately to indicate failure; otherwise, wait and retry until 2101 * memory allocation succeeds. Return the previously assigned swapblk, if 2102 * any. 2103 */ 2104 static daddr_t 2105 swp_pager_meta_build(vm_object_t object, vm_pindex_t pindex, daddr_t swapblk, 2106 bool nowait_noreplace) 2107 { 2108 static volatile int swblk_zone_exhausted, swpctrie_zone_exhausted; 2109 struct swblk *sb, *sb1; 2110 vm_pindex_t modpi, rdpi; 2111 daddr_t prev_swapblk; 2112 int error, i; 2113 2114 VM_OBJECT_ASSERT_WLOCKED(object); 2115 2116 rdpi = rounddown(pindex, SWAP_META_PAGES); 2117 sb = swblk_lookup(object, rdpi); 2118 if (sb == NULL) { 2119 if (swapblk == SWAPBLK_NONE) 2120 return (SWAPBLK_NONE); 2121 for (;;) { 2122 sb = uma_zalloc(swblk_zone, M_NOWAIT | (curproc == 2123 pageproc ? M_USE_RESERVE : 0)); 2124 if (sb != NULL) { 2125 sb->p = rdpi; 2126 for (i = 0; i < SWAP_META_PAGES; i++) 2127 sb->d[i] = SWAPBLK_NONE; 2128 if (atomic_cmpset_int(&swblk_zone_exhausted, 2129 1, 0)) 2130 printf("swblk zone ok\n"); 2131 break; 2132 } 2133 if (nowait_noreplace) 2134 return (swapblk); 2135 VM_OBJECT_WUNLOCK(object); 2136 if (uma_zone_exhausted(swblk_zone)) { 2137 if (atomic_cmpset_int(&swblk_zone_exhausted, 2138 0, 1)) 2139 printf("swap blk zone exhausted, " 2140 "increase kern.maxswzone\n"); 2141 vm_pageout_oom(VM_OOM_SWAPZ); 2142 pause("swzonxb", 10); 2143 } else 2144 uma_zwait(swblk_zone); 2145 VM_OBJECT_WLOCK(object); 2146 sb = swblk_lookup(object, rdpi); 2147 if (sb != NULL) 2148 /* 2149 * Somebody swapped out a nearby page, 2150 * allocating swblk at the rdpi index, 2151 * while we dropped the object lock. 2152 */ 2153 goto allocated; 2154 } 2155 for (;;) { 2156 error = swblk_lookup_insert(object, sb); 2157 if (error == 0) { 2158 if (atomic_cmpset_int(&swpctrie_zone_exhausted, 2159 1, 0)) 2160 printf("swpctrie zone ok\n"); 2161 break; 2162 } 2163 if (nowait_noreplace) { 2164 uma_zfree(swblk_zone, sb); 2165 return (swapblk); 2166 } 2167 VM_OBJECT_WUNLOCK(object); 2168 if (uma_zone_exhausted(swpctrie_zone)) { 2169 if (atomic_cmpset_int(&swpctrie_zone_exhausted, 2170 0, 1)) 2171 printf("swap pctrie zone exhausted, " 2172 "increase kern.maxswzone\n"); 2173 vm_pageout_oom(VM_OOM_SWAPZ); 2174 pause("swzonxp", 10); 2175 } else 2176 uma_zwait(swpctrie_zone); 2177 VM_OBJECT_WLOCK(object); 2178 sb1 = swblk_lookup(object, rdpi); 2179 if (sb1 != NULL) { 2180 uma_zfree(swblk_zone, sb); 2181 sb = sb1; 2182 goto allocated; 2183 } 2184 } 2185 } 2186 allocated: 2187 MPASS(sb->p == rdpi); 2188 2189 modpi = pindex % SWAP_META_PAGES; 2190 /* Return prior contents of metadata. */ 2191 prev_swapblk = sb->d[modpi]; 2192 if (!nowait_noreplace || prev_swapblk == SWAPBLK_NONE) { 2193 /* Enter block into metadata. */ 2194 sb->d[modpi] = swapblk; 2195 2196 /* 2197 * Free the swblk if we end up with the empty page run. 2198 */ 2199 if (swapblk == SWAPBLK_NONE) 2200 swp_pager_free_empty_swblk(object, sb); 2201 } 2202 return (prev_swapblk); 2203 } 2204 2205 /* 2206 * SWP_PAGER_META_TRANSFER() - transfer a range of blocks in the srcobject's 2207 * swap metadata into dstobject. 2208 * 2209 * Blocks in src that correspond to holes in dst are transferred. Blocks 2210 * in src that correspond to blocks in dst are freed. 2211 */ 2212 static void 2213 swp_pager_meta_transfer(vm_object_t srcobject, vm_object_t dstobject, 2214 vm_pindex_t pindex, vm_pindex_t count) 2215 { 2216 struct pctrie_iter blks; 2217 struct page_range range; 2218 struct swblk *sb; 2219 daddr_t blk, d[SWAP_META_PAGES]; 2220 vm_pindex_t last; 2221 int d_mask, i, limit, start; 2222 _Static_assert(8 * sizeof(d_mask) >= SWAP_META_PAGES, 2223 "d_mask not big enough"); 2224 2225 VM_OBJECT_ASSERT_WLOCKED(srcobject); 2226 VM_OBJECT_ASSERT_WLOCKED(dstobject); 2227 2228 if (count == 0 || swblk_is_empty(srcobject)) 2229 return; 2230 2231 swp_pager_init_freerange(&range); 2232 d_mask = 0; 2233 last = pindex + count; 2234 for (sb = swblk_iter_limit(&blks, srcobject, pindex, last), 2235 start = swblk_start(sb, pindex); 2236 sb != NULL; sb = swblk_iter_next(&blks), start = 0) { 2237 limit = MIN(last - blks.index, SWAP_META_PAGES); 2238 for (i = start; i < limit; i++) { 2239 if (sb->d[i] == SWAPBLK_NONE) 2240 continue; 2241 blk = swp_pager_meta_build(dstobject, 2242 blks.index + i - pindex, sb->d[i], true); 2243 if (blk == sb->d[i]) { 2244 /* 2245 * Failed memory allocation stopped transfer; 2246 * save this block for transfer with lock 2247 * released. 2248 */ 2249 d[i] = blk; 2250 d_mask |= 1 << i; 2251 } else if (blk != SWAPBLK_NONE) { 2252 /* Dst has a block at pindex, so free block. */ 2253 swp_pager_update_freerange(&range, sb->d[i]); 2254 } 2255 sb->d[i] = SWAPBLK_NONE; 2256 } 2257 if (swp_pager_swblk_empty(sb, 0, start) && 2258 swp_pager_swblk_empty(sb, limit, SWAP_META_PAGES)) { 2259 swblk_iter_remove(&blks); 2260 uma_zfree(swblk_zone, sb); 2261 } 2262 if (d_mask != 0) { 2263 /* Finish block transfer, with the lock released. */ 2264 VM_OBJECT_WUNLOCK(srcobject); 2265 do { 2266 i = ffs(d_mask) - 1; 2267 swp_pager_meta_build(dstobject, 2268 blks.index + i - pindex, d[i], false); 2269 d_mask &= ~(1 << i); 2270 } while (d_mask != 0); 2271 VM_OBJECT_WLOCK(srcobject); 2272 2273 /* 2274 * While the lock was not held, the iterator path could 2275 * have become stale, so discard it. 2276 */ 2277 pctrie_iter_reset(&blks); 2278 } 2279 } 2280 swp_pager_freeswapspace(&range); 2281 } 2282 2283 /* 2284 * SWP_PAGER_META_FREE() - free a range of blocks in the object's swap metadata 2285 * 2286 * Return freed swap blocks to the swap bitmap, and free emptied swblk 2287 * metadata. With 'freed' set, provide a count of freed blocks that were 2288 * not associated with valid resident pages. 2289 */ 2290 static void 2291 swp_pager_meta_free(vm_object_t object, vm_pindex_t pindex, vm_pindex_t count, 2292 vm_size_t *freed) 2293 { 2294 struct pctrie_iter blks, pages; 2295 struct page_range range; 2296 struct swblk *sb; 2297 vm_page_t m; 2298 vm_pindex_t last; 2299 vm_size_t fc; 2300 int i, limit, start; 2301 2302 VM_OBJECT_ASSERT_WLOCKED(object); 2303 2304 fc = 0; 2305 if (count == 0 || swblk_is_empty(object)) 2306 goto out; 2307 2308 swp_pager_init_freerange(&range); 2309 vm_page_iter_init(&pages, object); 2310 last = pindex + count; 2311 for (sb = swblk_iter_limit(&blks, object, pindex, last), 2312 start = swblk_start(sb, pindex); 2313 sb != NULL; sb = swblk_iter_next(&blks), start = 0) { 2314 limit = MIN(last - blks.index, SWAP_META_PAGES); 2315 for (i = start; i < limit; i++) { 2316 if (sb->d[i] == SWAPBLK_NONE) 2317 continue; 2318 swp_pager_update_freerange(&range, sb->d[i]); 2319 if (freed != NULL) { 2320 m = vm_page_iter_lookup(&pages, blks.index + i); 2321 if (m == NULL || vm_page_none_valid(m)) 2322 fc++; 2323 } 2324 sb->d[i] = SWAPBLK_NONE; 2325 } 2326 if (swp_pager_swblk_empty(sb, 0, start) && 2327 swp_pager_swblk_empty(sb, limit, SWAP_META_PAGES)) { 2328 swblk_iter_remove(&blks); 2329 uma_zfree(swblk_zone, sb); 2330 } 2331 } 2332 swp_pager_freeswapspace(&range); 2333 out: 2334 if (freed != NULL) 2335 *freed = fc; 2336 } 2337 2338 static void 2339 swp_pager_meta_free_block(struct swblk *sb, void *rangev) 2340 { 2341 struct page_range *range = rangev; 2342 2343 for (int i = 0; i < SWAP_META_PAGES; i++) { 2344 if (sb->d[i] != SWAPBLK_NONE) 2345 swp_pager_update_freerange(range, sb->d[i]); 2346 } 2347 uma_zfree(swblk_zone, sb); 2348 } 2349 2350 /* 2351 * SWP_PAGER_META_FREE_ALL() - destroy all swap metadata associated with object 2352 * 2353 * This routine locates and destroys all swap metadata associated with 2354 * an object. 2355 */ 2356 static void 2357 swp_pager_meta_free_all(vm_object_t object) 2358 { 2359 struct page_range range; 2360 2361 VM_OBJECT_ASSERT_WLOCKED(object); 2362 2363 swp_pager_init_freerange(&range); 2364 SWAP_PCTRIE_RECLAIM_CALLBACK(&object->un_pager.swp.swp_blks, 2365 swp_pager_meta_free_block, &range); 2366 swp_pager_freeswapspace(&range); 2367 } 2368 2369 /* 2370 * SWP_PAGER_METACTL() - misc control of swap meta data. 2371 * 2372 * This routine is capable of looking up, or removing swapblk 2373 * assignments in the swap meta data. It returns the swapblk being 2374 * looked-up, popped, or SWAPBLK_NONE if the block was invalid. 2375 * 2376 * When acting on a busy resident page and paging is in progress, we 2377 * have to wait until paging is complete but otherwise can act on the 2378 * busy page. 2379 */ 2380 static daddr_t 2381 swp_pager_meta_lookup(vm_object_t object, vm_pindex_t pindex) 2382 { 2383 struct swblk *sb; 2384 2385 VM_OBJECT_ASSERT_LOCKED(object); 2386 2387 /* 2388 * The meta data only exists if the object is OBJT_SWAP 2389 * and even then might not be allocated yet. 2390 */ 2391 KASSERT((object->flags & OBJ_SWAP) != 0, 2392 ("Lookup object not swappable")); 2393 2394 sb = swblk_lookup(object, pindex); 2395 if (sb == NULL) 2396 return (SWAPBLK_NONE); 2397 return (sb->d[pindex % SWAP_META_PAGES]); 2398 } 2399 2400 /* 2401 * Returns the least page index which is greater than or equal to the parameter 2402 * pindex and for which there is a swap block allocated. Returns OBJ_MAX_SIZE 2403 * if are no allocated swap blocks for the object after the requested pindex. 2404 */ 2405 vm_pindex_t 2406 swap_pager_find_least(vm_object_t object, vm_pindex_t pindex) 2407 { 2408 struct pctrie_iter blks; 2409 struct swblk *sb; 2410 int i; 2411 2412 if ((sb = swblk_iter(&blks, object, pindex)) == NULL) 2413 return (OBJ_MAX_SIZE); 2414 if (blks.index < pindex) { 2415 for (i = pindex % SWAP_META_PAGES; i < SWAP_META_PAGES; i++) { 2416 if (sb->d[i] != SWAPBLK_NONE) 2417 return (blks.index + i); 2418 } 2419 if ((sb = swblk_iter_next(&blks)) == NULL) 2420 return (OBJ_MAX_SIZE); 2421 } 2422 for (i = 0; i < SWAP_META_PAGES; i++) { 2423 if (sb->d[i] != SWAPBLK_NONE) 2424 return (blks.index + i); 2425 } 2426 2427 /* 2428 * We get here if a swblk is present in the trie but it 2429 * doesn't map any blocks. 2430 */ 2431 MPASS(0); 2432 return (OBJ_MAX_SIZE); 2433 } 2434 2435 /* 2436 * System call swapon(name) enables swapping on device name, 2437 * which must be in the swdevsw. Return EBUSY 2438 * if already swapping on this device. 2439 */ 2440 #ifndef _SYS_SYSPROTO_H_ 2441 struct swapon_args { 2442 char *name; 2443 }; 2444 #endif 2445 2446 int 2447 sys_swapon(struct thread *td, struct swapon_args *uap) 2448 { 2449 struct vattr attr; 2450 struct vnode *vp; 2451 struct nameidata nd; 2452 int error; 2453 2454 error = priv_check(td, PRIV_SWAPON); 2455 if (error) 2456 return (error); 2457 2458 sx_xlock(&swdev_syscall_lock); 2459 2460 /* 2461 * Swap metadata may not fit in the KVM if we have physical 2462 * memory of >1GB. 2463 */ 2464 if (swblk_zone == NULL) { 2465 error = ENOMEM; 2466 goto done; 2467 } 2468 2469 NDINIT(&nd, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | AUDITVNODE1, 2470 UIO_USERSPACE, uap->name); 2471 error = namei(&nd); 2472 if (error) 2473 goto done; 2474 2475 NDFREE_PNBUF(&nd); 2476 vp = nd.ni_vp; 2477 2478 if (vn_isdisk_error(vp, &error)) { 2479 error = swapongeom(vp); 2480 } else if (vp->v_type == VREG && 2481 (vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 && 2482 (error = VOP_GETATTR(vp, &attr, td->td_ucred)) == 0) { 2483 /* 2484 * Allow direct swapping to NFS regular files in the same 2485 * way that nfs_mountroot() sets up diskless swapping. 2486 */ 2487 error = swaponvp(td, vp, attr.va_size / DEV_BSIZE); 2488 } 2489 2490 if (error != 0) 2491 vput(vp); 2492 else 2493 VOP_UNLOCK(vp); 2494 done: 2495 sx_xunlock(&swdev_syscall_lock); 2496 return (error); 2497 } 2498 2499 /* 2500 * Check that the total amount of swap currently configured does not 2501 * exceed half the theoretical maximum. If it does, print a warning 2502 * message. 2503 */ 2504 static void 2505 swapon_check_swzone(void) 2506 { 2507 2508 /* recommend using no more than half that amount */ 2509 if (swap_total > swap_maxpages / 2) { 2510 printf("warning: total configured swap (%lu pages) " 2511 "exceeds maximum recommended amount (%lu pages).\n", 2512 swap_total, swap_maxpages / 2); 2513 printf("warning: increase kern.maxswzone " 2514 "or reduce amount of swap.\n"); 2515 } 2516 } 2517 2518 static void 2519 swaponsomething(struct vnode *vp, void *id, u_long nblks, 2520 sw_strategy_t *strategy, sw_close_t *close, dev_t dev, int flags) 2521 { 2522 struct swdevt *sp, *tsp; 2523 daddr_t dvbase; 2524 2525 /* 2526 * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks. 2527 * First chop nblks off to page-align it, then convert. 2528 * 2529 * sw->sw_nblks is in page-sized chunks now too. 2530 */ 2531 nblks &= ~(ctodb(1) - 1); 2532 nblks = dbtoc(nblks); 2533 2534 sp = malloc(sizeof *sp, M_VMPGDATA, M_WAITOK | M_ZERO); 2535 sp->sw_blist = blist_create(nblks, M_WAITOK); 2536 sp->sw_vp = vp; 2537 sp->sw_id = id; 2538 sp->sw_dev = dev; 2539 sp->sw_nblks = nblks; 2540 sp->sw_used = 0; 2541 sp->sw_strategy = strategy; 2542 sp->sw_close = close; 2543 sp->sw_flags = flags; 2544 2545 /* 2546 * Do not free the first blocks in order to avoid overwriting 2547 * any bsd label at the front of the partition 2548 */ 2549 blist_free(sp->sw_blist, howmany(BBSIZE, PAGE_SIZE), 2550 nblks - howmany(BBSIZE, PAGE_SIZE)); 2551 2552 dvbase = 0; 2553 mtx_lock(&sw_dev_mtx); 2554 TAILQ_FOREACH(tsp, &swtailq, sw_list) { 2555 if (tsp->sw_end >= dvbase) { 2556 /* 2557 * We put one uncovered page between the devices 2558 * in order to definitively prevent any cross-device 2559 * I/O requests 2560 */ 2561 dvbase = tsp->sw_end + 1; 2562 } 2563 } 2564 sp->sw_first = dvbase; 2565 sp->sw_end = dvbase + nblks; 2566 TAILQ_INSERT_TAIL(&swtailq, sp, sw_list); 2567 nswapdev++; 2568 swap_pager_avail += nblks - howmany(BBSIZE, PAGE_SIZE); 2569 swap_total += nblks; 2570 swapon_check_swzone(); 2571 swp_sizecheck(); 2572 mtx_unlock(&sw_dev_mtx); 2573 EVENTHANDLER_INVOKE(swapon, sp); 2574 } 2575 2576 /* 2577 * SYSCALL: swapoff(devname) 2578 * 2579 * Disable swapping on the given device. 2580 * 2581 * XXX: Badly designed system call: it should use a device index 2582 * rather than filename as specification. We keep sw_vp around 2583 * only to make this work. 2584 */ 2585 static int 2586 kern_swapoff(struct thread *td, const char *name, enum uio_seg name_seg, 2587 u_int flags) 2588 { 2589 struct vnode *vp; 2590 struct nameidata nd; 2591 struct swdevt *sp; 2592 int error; 2593 2594 error = priv_check(td, PRIV_SWAPOFF); 2595 if (error != 0) 2596 return (error); 2597 if ((flags & ~(SWAPOFF_FORCE)) != 0) 2598 return (EINVAL); 2599 2600 sx_xlock(&swdev_syscall_lock); 2601 2602 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, name_seg, name); 2603 error = namei(&nd); 2604 if (error) 2605 goto done; 2606 NDFREE_PNBUF(&nd); 2607 vp = nd.ni_vp; 2608 2609 mtx_lock(&sw_dev_mtx); 2610 TAILQ_FOREACH(sp, &swtailq, sw_list) { 2611 if (sp->sw_vp == vp) 2612 break; 2613 } 2614 mtx_unlock(&sw_dev_mtx); 2615 if (sp == NULL) { 2616 error = EINVAL; 2617 goto done; 2618 } 2619 error = swapoff_one(sp, td->td_ucred, flags); 2620 done: 2621 sx_xunlock(&swdev_syscall_lock); 2622 return (error); 2623 } 2624 2625 2626 #ifdef COMPAT_FREEBSD13 2627 int 2628 freebsd13_swapoff(struct thread *td, struct freebsd13_swapoff_args *uap) 2629 { 2630 return (kern_swapoff(td, uap->name, UIO_USERSPACE, 0)); 2631 } 2632 #endif 2633 2634 int 2635 sys_swapoff(struct thread *td, struct swapoff_args *uap) 2636 { 2637 return (kern_swapoff(td, uap->name, UIO_USERSPACE, uap->flags)); 2638 } 2639 2640 static int 2641 swapoff_one(struct swdevt *sp, struct ucred *cred, u_int flags) 2642 { 2643 u_long nblks; 2644 #ifdef MAC 2645 int error; 2646 #endif 2647 2648 sx_assert(&swdev_syscall_lock, SA_XLOCKED); 2649 #ifdef MAC 2650 (void) vn_lock(sp->sw_vp, LK_EXCLUSIVE | LK_RETRY); 2651 error = mac_system_check_swapoff(cred, sp->sw_vp); 2652 (void) VOP_UNLOCK(sp->sw_vp); 2653 if (error != 0) 2654 return (error); 2655 #endif 2656 nblks = sp->sw_nblks; 2657 2658 /* 2659 * We can turn off this swap device safely only if the 2660 * available virtual memory in the system will fit the amount 2661 * of data we will have to page back in, plus an epsilon so 2662 * the system doesn't become critically low on swap space. 2663 * The vm_free_count() part does not account e.g. for clean 2664 * pages that can be immediately reclaimed without paging, so 2665 * this is a very rough estimation. 2666 * 2667 * On the other hand, not turning swap off on swapoff_all() 2668 * means that we can lose swap data when filesystems go away, 2669 * which is arguably worse. 2670 */ 2671 if ((flags & SWAPOFF_FORCE) == 0 && 2672 vm_free_count() + swap_pager_avail < nblks + nswap_lowat) 2673 return (ENOMEM); 2674 2675 /* 2676 * Prevent further allocations on this device. 2677 */ 2678 mtx_lock(&sw_dev_mtx); 2679 sp->sw_flags |= SW_CLOSING; 2680 swap_pager_avail -= blist_fill(sp->sw_blist, 0, nblks); 2681 swap_total -= nblks; 2682 mtx_unlock(&sw_dev_mtx); 2683 2684 /* 2685 * Page in the contents of the device and close it. 2686 */ 2687 swap_pager_swapoff(sp); 2688 2689 sp->sw_close(curthread, sp); 2690 mtx_lock(&sw_dev_mtx); 2691 sp->sw_id = NULL; 2692 TAILQ_REMOVE(&swtailq, sp, sw_list); 2693 nswapdev--; 2694 if (nswapdev == 0) { 2695 swap_pager_full = 2; 2696 swap_pager_almost_full = 1; 2697 } 2698 if (swdevhd == sp) 2699 swdevhd = NULL; 2700 mtx_unlock(&sw_dev_mtx); 2701 blist_destroy(sp->sw_blist); 2702 free(sp, M_VMPGDATA); 2703 return (0); 2704 } 2705 2706 void 2707 swapoff_all(void) 2708 { 2709 struct swdevt *sp, *spt; 2710 const char *devname; 2711 int error; 2712 2713 sx_xlock(&swdev_syscall_lock); 2714 2715 mtx_lock(&sw_dev_mtx); 2716 TAILQ_FOREACH_SAFE(sp, &swtailq, sw_list, spt) { 2717 mtx_unlock(&sw_dev_mtx); 2718 if (vn_isdisk(sp->sw_vp)) 2719 devname = devtoname(sp->sw_vp->v_rdev); 2720 else 2721 devname = "[file]"; 2722 error = swapoff_one(sp, thread0.td_ucred, SWAPOFF_FORCE); 2723 if (error != 0) { 2724 printf("Cannot remove swap device %s (error=%d), " 2725 "skipping.\n", devname, error); 2726 } else if (bootverbose) { 2727 printf("Swap device %s removed.\n", devname); 2728 } 2729 mtx_lock(&sw_dev_mtx); 2730 } 2731 mtx_unlock(&sw_dev_mtx); 2732 2733 sx_xunlock(&swdev_syscall_lock); 2734 } 2735 2736 void 2737 swap_pager_status(int *total, int *used) 2738 { 2739 2740 *total = swap_total; 2741 *used = swap_total - swap_pager_avail - 2742 nswapdev * howmany(BBSIZE, PAGE_SIZE); 2743 } 2744 2745 int 2746 swap_dev_info(int name, struct xswdev *xs, char *devname, size_t len) 2747 { 2748 struct swdevt *sp; 2749 const char *tmp_devname; 2750 int error, n; 2751 2752 n = 0; 2753 error = ENOENT; 2754 mtx_lock(&sw_dev_mtx); 2755 TAILQ_FOREACH(sp, &swtailq, sw_list) { 2756 if (n != name) { 2757 n++; 2758 continue; 2759 } 2760 xs->xsw_version = XSWDEV_VERSION; 2761 xs->xsw_dev = sp->sw_dev; 2762 xs->xsw_flags = sp->sw_flags; 2763 xs->xsw_nblks = sp->sw_nblks; 2764 xs->xsw_used = sp->sw_used; 2765 if (devname != NULL) { 2766 if (vn_isdisk(sp->sw_vp)) 2767 tmp_devname = devtoname(sp->sw_vp->v_rdev); 2768 else 2769 tmp_devname = "[file]"; 2770 strncpy(devname, tmp_devname, len); 2771 } 2772 error = 0; 2773 break; 2774 } 2775 mtx_unlock(&sw_dev_mtx); 2776 return (error); 2777 } 2778 2779 #if defined(COMPAT_FREEBSD11) 2780 #define XSWDEV_VERSION_11 1 2781 struct xswdev11 { 2782 u_int xsw_version; 2783 uint32_t xsw_dev; 2784 int xsw_flags; 2785 int xsw_nblks; 2786 int xsw_used; 2787 }; 2788 #endif 2789 2790 #if defined(__amd64__) && defined(COMPAT_FREEBSD32) 2791 struct xswdev32 { 2792 u_int xsw_version; 2793 u_int xsw_dev1, xsw_dev2; 2794 int xsw_flags; 2795 int xsw_nblks; 2796 int xsw_used; 2797 }; 2798 #endif 2799 2800 static int 2801 sysctl_vm_swap_info(SYSCTL_HANDLER_ARGS) 2802 { 2803 struct xswdev xs; 2804 #if defined(__amd64__) && defined(COMPAT_FREEBSD32) 2805 struct xswdev32 xs32; 2806 #endif 2807 #if defined(COMPAT_FREEBSD11) 2808 struct xswdev11 xs11; 2809 #endif 2810 int error; 2811 2812 if (arg2 != 1) /* name length */ 2813 return (EINVAL); 2814 2815 memset(&xs, 0, sizeof(xs)); 2816 error = swap_dev_info(*(int *)arg1, &xs, NULL, 0); 2817 if (error != 0) 2818 return (error); 2819 #if defined(__amd64__) && defined(COMPAT_FREEBSD32) 2820 if (req->oldlen == sizeof(xs32)) { 2821 memset(&xs32, 0, sizeof(xs32)); 2822 xs32.xsw_version = XSWDEV_VERSION; 2823 xs32.xsw_dev1 = xs.xsw_dev; 2824 xs32.xsw_dev2 = xs.xsw_dev >> 32; 2825 xs32.xsw_flags = xs.xsw_flags; 2826 xs32.xsw_nblks = xs.xsw_nblks; 2827 xs32.xsw_used = xs.xsw_used; 2828 error = SYSCTL_OUT(req, &xs32, sizeof(xs32)); 2829 return (error); 2830 } 2831 #endif 2832 #if defined(COMPAT_FREEBSD11) 2833 if (req->oldlen == sizeof(xs11)) { 2834 memset(&xs11, 0, sizeof(xs11)); 2835 xs11.xsw_version = XSWDEV_VERSION_11; 2836 xs11.xsw_dev = xs.xsw_dev; /* truncation */ 2837 xs11.xsw_flags = xs.xsw_flags; 2838 xs11.xsw_nblks = xs.xsw_nblks; 2839 xs11.xsw_used = xs.xsw_used; 2840 error = SYSCTL_OUT(req, &xs11, sizeof(xs11)); 2841 return (error); 2842 } 2843 #endif 2844 error = SYSCTL_OUT(req, &xs, sizeof(xs)); 2845 return (error); 2846 } 2847 2848 SYSCTL_INT(_vm, OID_AUTO, nswapdev, CTLFLAG_RD, &nswapdev, 0, 2849 "Number of swap devices"); 2850 SYSCTL_NODE(_vm, OID_AUTO, swap_info, CTLFLAG_RD | CTLFLAG_MPSAFE, 2851 sysctl_vm_swap_info, 2852 "Swap statistics by device"); 2853 2854 /* 2855 * Count the approximate swap usage in pages for a vmspace. The 2856 * shadowed or not yet copied on write swap blocks are not accounted. 2857 * The map must be locked. 2858 */ 2859 long 2860 vmspace_swap_count(struct vmspace *vmspace) 2861 { 2862 struct pctrie_iter blks; 2863 vm_map_t map; 2864 vm_map_entry_t cur; 2865 vm_object_t object; 2866 struct swblk *sb; 2867 vm_pindex_t e, pi; 2868 long count; 2869 int i, limit, start; 2870 2871 map = &vmspace->vm_map; 2872 count = 0; 2873 2874 VM_MAP_ENTRY_FOREACH(cur, map) { 2875 if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) != 0) 2876 continue; 2877 object = cur->object.vm_object; 2878 if (object == NULL || (object->flags & OBJ_SWAP) == 0) 2879 continue; 2880 VM_OBJECT_RLOCK(object); 2881 if ((object->flags & OBJ_SWAP) == 0) 2882 goto unlock; 2883 pi = OFF_TO_IDX(cur->offset); 2884 e = pi + OFF_TO_IDX(cur->end - cur->start); 2885 for (sb = swblk_iter_limit(&blks, object, pi, e), 2886 start = swblk_start(sb, pi); 2887 sb != NULL; sb = swblk_iter_next(&blks), start = 0) { 2888 limit = MIN(e - blks.index, SWAP_META_PAGES); 2889 for (i = start; i < limit; i++) { 2890 if (sb->d[i] != SWAPBLK_NONE) 2891 count++; 2892 } 2893 } 2894 unlock: 2895 VM_OBJECT_RUNLOCK(object); 2896 } 2897 return (count); 2898 } 2899 2900 /* 2901 * GEOM backend 2902 * 2903 * Swapping onto disk devices. 2904 * 2905 */ 2906 2907 static g_orphan_t swapgeom_orphan; 2908 2909 static struct g_class g_swap_class = { 2910 .name = "SWAP", 2911 .version = G_VERSION, 2912 .orphan = swapgeom_orphan, 2913 }; 2914 2915 DECLARE_GEOM_CLASS(g_swap_class, g_class); 2916 2917 static void 2918 swapgeom_close_ev(void *arg, int flags) 2919 { 2920 struct g_consumer *cp; 2921 2922 cp = arg; 2923 g_access(cp, -1, -1, 0); 2924 g_detach(cp); 2925 g_destroy_consumer(cp); 2926 } 2927 2928 /* 2929 * Add a reference to the g_consumer for an inflight transaction. 2930 */ 2931 static void 2932 swapgeom_acquire(struct g_consumer *cp) 2933 { 2934 2935 mtx_assert(&sw_dev_mtx, MA_OWNED); 2936 cp->index++; 2937 } 2938 2939 /* 2940 * Remove a reference from the g_consumer. Post a close event if all 2941 * references go away, since the function might be called from the 2942 * biodone context. 2943 */ 2944 static void 2945 swapgeom_release(struct g_consumer *cp, struct swdevt *sp) 2946 { 2947 2948 mtx_assert(&sw_dev_mtx, MA_OWNED); 2949 cp->index--; 2950 if (cp->index == 0) { 2951 if (g_post_event(swapgeom_close_ev, cp, M_NOWAIT, NULL) == 0) 2952 sp->sw_id = NULL; 2953 } 2954 } 2955 2956 static void 2957 swapgeom_done(struct bio *bp2) 2958 { 2959 struct swdevt *sp; 2960 struct buf *bp; 2961 struct g_consumer *cp; 2962 2963 bp = bp2->bio_caller2; 2964 cp = bp2->bio_from; 2965 bp->b_ioflags = bp2->bio_flags; 2966 if (bp2->bio_error) 2967 bp->b_ioflags |= BIO_ERROR; 2968 bp->b_resid = bp->b_bcount - bp2->bio_completed; 2969 bp->b_error = bp2->bio_error; 2970 bp->b_caller1 = NULL; 2971 bufdone(bp); 2972 sp = bp2->bio_caller1; 2973 mtx_lock(&sw_dev_mtx); 2974 swapgeom_release(cp, sp); 2975 mtx_unlock(&sw_dev_mtx); 2976 g_destroy_bio(bp2); 2977 } 2978 2979 static void 2980 swapgeom_strategy(struct buf *bp, struct swdevt *sp) 2981 { 2982 struct bio *bio; 2983 struct g_consumer *cp; 2984 2985 mtx_lock(&sw_dev_mtx); 2986 cp = sp->sw_id; 2987 if (cp == NULL) { 2988 mtx_unlock(&sw_dev_mtx); 2989 bp->b_error = ENXIO; 2990 bp->b_ioflags |= BIO_ERROR; 2991 bufdone(bp); 2992 return; 2993 } 2994 swapgeom_acquire(cp); 2995 mtx_unlock(&sw_dev_mtx); 2996 if (bp->b_iocmd == BIO_WRITE) 2997 bio = g_new_bio(); 2998 else 2999 bio = g_alloc_bio(); 3000 if (bio == NULL) { 3001 mtx_lock(&sw_dev_mtx); 3002 swapgeom_release(cp, sp); 3003 mtx_unlock(&sw_dev_mtx); 3004 bp->b_error = ENOMEM; 3005 bp->b_ioflags |= BIO_ERROR; 3006 printf("swap_pager: cannot allocate bio\n"); 3007 bufdone(bp); 3008 return; 3009 } 3010 3011 bp->b_caller1 = bio; 3012 bio->bio_caller1 = sp; 3013 bio->bio_caller2 = bp; 3014 bio->bio_cmd = bp->b_iocmd; 3015 bio->bio_offset = (bp->b_blkno - sp->sw_first) * PAGE_SIZE; 3016 bio->bio_length = bp->b_bcount; 3017 bio->bio_done = swapgeom_done; 3018 bio->bio_flags |= BIO_SWAP; 3019 if (!buf_mapped(bp)) { 3020 bio->bio_ma = bp->b_pages; 3021 bio->bio_data = unmapped_buf; 3022 bio->bio_ma_offset = (vm_offset_t)bp->b_offset & PAGE_MASK; 3023 bio->bio_ma_n = bp->b_npages; 3024 bio->bio_flags |= BIO_UNMAPPED; 3025 } else { 3026 bio->bio_data = bp->b_data; 3027 bio->bio_ma = NULL; 3028 } 3029 g_io_request(bio, cp); 3030 return; 3031 } 3032 3033 static void 3034 swapgeom_orphan(struct g_consumer *cp) 3035 { 3036 struct swdevt *sp; 3037 int destroy; 3038 3039 mtx_lock(&sw_dev_mtx); 3040 TAILQ_FOREACH(sp, &swtailq, sw_list) { 3041 if (sp->sw_id == cp) { 3042 sp->sw_flags |= SW_CLOSING; 3043 break; 3044 } 3045 } 3046 /* 3047 * Drop reference we were created with. Do directly since we're in a 3048 * special context where we don't have to queue the call to 3049 * swapgeom_close_ev(). 3050 */ 3051 cp->index--; 3052 destroy = ((sp != NULL) && (cp->index == 0)); 3053 if (destroy) 3054 sp->sw_id = NULL; 3055 mtx_unlock(&sw_dev_mtx); 3056 if (destroy) 3057 swapgeom_close_ev(cp, 0); 3058 } 3059 3060 static void 3061 swapgeom_close(struct thread *td, struct swdevt *sw) 3062 { 3063 struct g_consumer *cp; 3064 3065 mtx_lock(&sw_dev_mtx); 3066 cp = sw->sw_id; 3067 sw->sw_id = NULL; 3068 mtx_unlock(&sw_dev_mtx); 3069 3070 /* 3071 * swapgeom_close() may be called from the biodone context, 3072 * where we cannot perform topology changes. Delegate the 3073 * work to the events thread. 3074 */ 3075 if (cp != NULL) 3076 g_waitfor_event(swapgeom_close_ev, cp, M_WAITOK, NULL); 3077 } 3078 3079 static int 3080 swapongeom_locked(struct cdev *dev, struct vnode *vp) 3081 { 3082 struct g_provider *pp; 3083 struct g_consumer *cp; 3084 static struct g_geom *gp; 3085 struct swdevt *sp; 3086 u_long nblks; 3087 int error; 3088 3089 pp = g_dev_getprovider(dev); 3090 if (pp == NULL) 3091 return (ENODEV); 3092 mtx_lock(&sw_dev_mtx); 3093 TAILQ_FOREACH(sp, &swtailq, sw_list) { 3094 cp = sp->sw_id; 3095 if (cp != NULL && cp->provider == pp) { 3096 mtx_unlock(&sw_dev_mtx); 3097 return (EBUSY); 3098 } 3099 } 3100 mtx_unlock(&sw_dev_mtx); 3101 if (gp == NULL) 3102 gp = g_new_geomf(&g_swap_class, "swap"); 3103 cp = g_new_consumer(gp); 3104 cp->index = 1; /* Number of active I/Os, plus one for being active. */ 3105 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 3106 g_attach(cp, pp); 3107 /* 3108 * XXX: Every time you think you can improve the margin for 3109 * footshooting, somebody depends on the ability to do so: 3110 * savecore(8) wants to write to our swapdev so we cannot 3111 * set an exclusive count :-( 3112 */ 3113 error = g_access(cp, 1, 1, 0); 3114 if (error != 0) { 3115 g_detach(cp); 3116 g_destroy_consumer(cp); 3117 return (error); 3118 } 3119 nblks = pp->mediasize / DEV_BSIZE; 3120 swaponsomething(vp, cp, nblks, swapgeom_strategy, 3121 swapgeom_close, dev2udev(dev), 3122 (pp->flags & G_PF_ACCEPT_UNMAPPED) != 0 ? SW_UNMAPPED : 0); 3123 return (0); 3124 } 3125 3126 static int 3127 swapongeom(struct vnode *vp) 3128 { 3129 int error; 3130 3131 ASSERT_VOP_ELOCKED(vp, "swapongeom"); 3132 if (vp->v_type != VCHR || VN_IS_DOOMED(vp)) { 3133 error = ENOENT; 3134 } else { 3135 g_topology_lock(); 3136 error = swapongeom_locked(vp->v_rdev, vp); 3137 g_topology_unlock(); 3138 } 3139 return (error); 3140 } 3141 3142 /* 3143 * VNODE backend 3144 * 3145 * This is used mainly for network filesystem (read: probably only tested 3146 * with NFS) swapfiles. 3147 * 3148 */ 3149 3150 static void 3151 swapdev_strategy(struct buf *bp, struct swdevt *sp) 3152 { 3153 struct vnode *vp2; 3154 3155 bp->b_blkno = ctodb(bp->b_blkno - sp->sw_first); 3156 3157 vp2 = sp->sw_id; 3158 vhold(vp2); 3159 if (bp->b_iocmd == BIO_WRITE) { 3160 vn_lock(vp2, LK_EXCLUSIVE | LK_RETRY); 3161 if (bp->b_bufobj) 3162 bufobj_wdrop(bp->b_bufobj); 3163 bufobj_wref(&vp2->v_bufobj); 3164 } else { 3165 vn_lock(vp2, LK_SHARED | LK_RETRY); 3166 } 3167 if (bp->b_bufobj != &vp2->v_bufobj) 3168 bp->b_bufobj = &vp2->v_bufobj; 3169 bp->b_vp = vp2; 3170 bp->b_iooffset = dbtob(bp->b_blkno); 3171 bstrategy(bp); 3172 VOP_UNLOCK(vp2); 3173 } 3174 3175 static void 3176 swapdev_close(struct thread *td, struct swdevt *sp) 3177 { 3178 struct vnode *vp; 3179 3180 vp = sp->sw_vp; 3181 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 3182 VOP_CLOSE(vp, FREAD | FWRITE, td->td_ucred, td); 3183 vput(vp); 3184 } 3185 3186 static int 3187 swaponvp(struct thread *td, struct vnode *vp, u_long nblks) 3188 { 3189 struct swdevt *sp; 3190 int error; 3191 3192 ASSERT_VOP_ELOCKED(vp, "swaponvp"); 3193 if (nblks == 0) 3194 return (ENXIO); 3195 mtx_lock(&sw_dev_mtx); 3196 TAILQ_FOREACH(sp, &swtailq, sw_list) { 3197 if (sp->sw_id == vp) { 3198 mtx_unlock(&sw_dev_mtx); 3199 return (EBUSY); 3200 } 3201 } 3202 mtx_unlock(&sw_dev_mtx); 3203 3204 #ifdef MAC 3205 error = mac_system_check_swapon(td->td_ucred, vp); 3206 if (error == 0) 3207 #endif 3208 error = VOP_OPEN(vp, FREAD | FWRITE, td->td_ucred, td, NULL); 3209 if (error != 0) 3210 return (error); 3211 3212 swaponsomething(vp, vp, nblks, swapdev_strategy, swapdev_close, 3213 NODEV, 0); 3214 return (0); 3215 } 3216 3217 static int 3218 sysctl_swap_async_max(SYSCTL_HANDLER_ARGS) 3219 { 3220 int error, new, n; 3221 3222 new = nsw_wcount_async_max; 3223 error = sysctl_handle_int(oidp, &new, 0, req); 3224 if (error != 0 || req->newptr == NULL) 3225 return (error); 3226 3227 if (new > nswbuf / 2 || new < 1) 3228 return (EINVAL); 3229 3230 mtx_lock(&swbuf_mtx); 3231 while (nsw_wcount_async_max != new) { 3232 /* 3233 * Adjust difference. If the current async count is too low, 3234 * we will need to sqeeze our update slowly in. Sleep with a 3235 * higher priority than getpbuf() to finish faster. 3236 */ 3237 n = new - nsw_wcount_async_max; 3238 if (nsw_wcount_async + n >= 0) { 3239 nsw_wcount_async += n; 3240 nsw_wcount_async_max += n; 3241 wakeup(&nsw_wcount_async); 3242 } else { 3243 nsw_wcount_async_max -= nsw_wcount_async; 3244 nsw_wcount_async = 0; 3245 msleep(&nsw_wcount_async, &swbuf_mtx, PSWP, 3246 "swpsysctl", 0); 3247 } 3248 } 3249 mtx_unlock(&swbuf_mtx); 3250 3251 return (0); 3252 } 3253 3254 static void 3255 swap_pager_update_writecount(vm_object_t object, vm_offset_t start, 3256 vm_offset_t end) 3257 { 3258 3259 VM_OBJECT_WLOCK(object); 3260 KASSERT((object->flags & OBJ_ANON) == 0, 3261 ("Splittable object with writecount")); 3262 object->un_pager.swp.writemappings += (vm_ooffset_t)end - start; 3263 VM_OBJECT_WUNLOCK(object); 3264 } 3265 3266 static void 3267 swap_pager_release_writecount(vm_object_t object, vm_offset_t start, 3268 vm_offset_t end) 3269 { 3270 3271 VM_OBJECT_WLOCK(object); 3272 KASSERT((object->flags & OBJ_ANON) == 0, 3273 ("Splittable object with writecount")); 3274 KASSERT(object->un_pager.swp.writemappings >= (vm_ooffset_t)end - start, 3275 ("swap obj %p writecount %jx dec %jx", object, 3276 (uintmax_t)object->un_pager.swp.writemappings, 3277 (uintmax_t)((vm_ooffset_t)end - start))); 3278 object->un_pager.swp.writemappings -= (vm_ooffset_t)end - start; 3279 VM_OBJECT_WUNLOCK(object); 3280 } 3281