1 /*- 2 * Copyright (c) 1998 Matthew Dillon, 3 * Copyright (c) 1994 John S. Dyson 4 * Copyright (c) 1990 University of Utah. 5 * Copyright (c) 1982, 1986, 1989, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * the Systems Programming Group of the University of Utah Computer 10 * Science Department. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the University of 23 * California, Berkeley and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * New Swap System 41 * Matthew Dillon 42 * 43 * Radix Bitmap 'blists'. 44 * 45 * - The new swapper uses the new radix bitmap code. This should scale 46 * to arbitrarily small or arbitrarily large swap spaces and an almost 47 * arbitrary degree of fragmentation. 48 * 49 * Features: 50 * 51 * - on the fly reallocation of swap during putpages. The new system 52 * does not try to keep previously allocated swap blocks for dirty 53 * pages. 54 * 55 * - on the fly deallocation of swap 56 * 57 * - No more garbage collection required. Unnecessarily allocated swap 58 * blocks only exist for dirty vm_page_t's now and these are already 59 * cycled (in a high-load system) by the pager. We also do on-the-fly 60 * removal of invalidated swap blocks when a page is destroyed 61 * or renamed. 62 * 63 * from: Utah $Hdr: swap_pager.c 1.4 91/04/30$ 64 * 65 * @(#)swap_pager.c 8.9 (Berkeley) 3/21/94 66 * @(#)vm_swap.c 8.5 (Berkeley) 2/17/94 67 */ 68 69 #include <sys/cdefs.h> 70 __FBSDID("$FreeBSD$"); 71 72 #include "opt_swap.h" 73 #include "opt_vm.h" 74 75 #include <sys/param.h> 76 #include <sys/systm.h> 77 #include <sys/conf.h> 78 #include <sys/kernel.h> 79 #include <sys/priv.h> 80 #include <sys/proc.h> 81 #include <sys/bio.h> 82 #include <sys/buf.h> 83 #include <sys/disk.h> 84 #include <sys/fcntl.h> 85 #include <sys/mount.h> 86 #include <sys/namei.h> 87 #include <sys/vnode.h> 88 #include <sys/malloc.h> 89 #include <sys/racct.h> 90 #include <sys/resource.h> 91 #include <sys/resourcevar.h> 92 #include <sys/rwlock.h> 93 #include <sys/sysctl.h> 94 #include <sys/sysproto.h> 95 #include <sys/blist.h> 96 #include <sys/lock.h> 97 #include <sys/sx.h> 98 #include <sys/vmmeter.h> 99 100 #include <security/mac/mac_framework.h> 101 102 #include <vm/vm.h> 103 #include <vm/pmap.h> 104 #include <vm/vm_map.h> 105 #include <vm/vm_kern.h> 106 #include <vm/vm_object.h> 107 #include <vm/vm_page.h> 108 #include <vm/vm_pager.h> 109 #include <vm/vm_pageout.h> 110 #include <vm/vm_param.h> 111 #include <vm/swap_pager.h> 112 #include <vm/vm_extern.h> 113 #include <vm/uma.h> 114 115 #include <geom/geom.h> 116 117 /* 118 * SWB_NPAGES must be a power of 2. It may be set to 1, 2, 4, 8, 16 119 * or 32 pages per allocation. 120 * The 32-page limit is due to the radix code (kern/subr_blist.c). 121 */ 122 #ifndef MAX_PAGEOUT_CLUSTER 123 #define MAX_PAGEOUT_CLUSTER 16 124 #endif 125 126 #if !defined(SWB_NPAGES) 127 #define SWB_NPAGES MAX_PAGEOUT_CLUSTER 128 #endif 129 130 /* 131 * The swblock structure maps an object and a small, fixed-size range 132 * of page indices to disk addresses within a swap area. 133 * The collection of these mappings is implemented as a hash table. 134 * Unused disk addresses within a swap area are allocated and managed 135 * using a blist. 136 */ 137 #define SWCORRECT(n) (sizeof(void *) * (n) / sizeof(daddr_t)) 138 #define SWAP_META_PAGES (SWB_NPAGES * 2) 139 #define SWAP_META_MASK (SWAP_META_PAGES - 1) 140 141 struct swblock { 142 struct swblock *swb_hnext; 143 vm_object_t swb_object; 144 vm_pindex_t swb_index; 145 int swb_count; 146 daddr_t swb_pages[SWAP_META_PAGES]; 147 }; 148 149 static MALLOC_DEFINE(M_VMPGDATA, "vm_pgdata", "swap pager private data"); 150 static struct mtx sw_dev_mtx; 151 static TAILQ_HEAD(, swdevt) swtailq = TAILQ_HEAD_INITIALIZER(swtailq); 152 static struct swdevt *swdevhd; /* Allocate from here next */ 153 static int nswapdev; /* Number of swap devices */ 154 int swap_pager_avail; 155 static int swdev_syscall_active = 0; /* serialize swap(on|off) */ 156 157 static vm_ooffset_t swap_total; 158 SYSCTL_QUAD(_vm, OID_AUTO, swap_total, CTLFLAG_RD, &swap_total, 0, 159 "Total amount of available swap storage."); 160 static vm_ooffset_t swap_reserved; 161 SYSCTL_QUAD(_vm, OID_AUTO, swap_reserved, CTLFLAG_RD, &swap_reserved, 0, 162 "Amount of swap storage needed to back all allocated anonymous memory."); 163 static int overcommit = 0; 164 SYSCTL_INT(_vm, OID_AUTO, overcommit, CTLFLAG_RW, &overcommit, 0, 165 "Configure virtual memory overcommit behavior. See tuning(7) " 166 "for details."); 167 static unsigned long swzone; 168 SYSCTL_ULONG(_vm, OID_AUTO, swzone, CTLFLAG_RD, &swzone, 0, 169 "Actual size of swap metadata zone"); 170 static unsigned long swap_maxpages; 171 SYSCTL_ULONG(_vm, OID_AUTO, swap_maxpages, CTLFLAG_RD, &swap_maxpages, 0, 172 "Maximum amount of swap supported"); 173 174 /* bits from overcommit */ 175 #define SWAP_RESERVE_FORCE_ON (1 << 0) 176 #define SWAP_RESERVE_RLIMIT_ON (1 << 1) 177 #define SWAP_RESERVE_ALLOW_NONWIRED (1 << 2) 178 179 int 180 swap_reserve(vm_ooffset_t incr) 181 { 182 183 return (swap_reserve_by_cred(incr, curthread->td_ucred)); 184 } 185 186 int 187 swap_reserve_by_cred(vm_ooffset_t incr, struct ucred *cred) 188 { 189 vm_ooffset_t r, s; 190 int res, error; 191 static int curfail; 192 static struct timeval lastfail; 193 struct uidinfo *uip; 194 195 uip = cred->cr_ruidinfo; 196 197 if (incr & PAGE_MASK) 198 panic("swap_reserve: & PAGE_MASK"); 199 200 #ifdef RACCT 201 if (racct_enable) { 202 PROC_LOCK(curproc); 203 error = racct_add(curproc, RACCT_SWAP, incr); 204 PROC_UNLOCK(curproc); 205 if (error != 0) 206 return (0); 207 } 208 #endif 209 210 res = 0; 211 mtx_lock(&sw_dev_mtx); 212 r = swap_reserved + incr; 213 if (overcommit & SWAP_RESERVE_ALLOW_NONWIRED) { 214 s = vm_cnt.v_page_count - vm_cnt.v_free_reserved - vm_cnt.v_wire_count; 215 s *= PAGE_SIZE; 216 } else 217 s = 0; 218 s += swap_total; 219 if ((overcommit & SWAP_RESERVE_FORCE_ON) == 0 || r <= s || 220 (error = priv_check(curthread, PRIV_VM_SWAP_NOQUOTA)) == 0) { 221 res = 1; 222 swap_reserved = r; 223 } 224 mtx_unlock(&sw_dev_mtx); 225 226 if (res) { 227 PROC_LOCK(curproc); 228 UIDINFO_VMSIZE_LOCK(uip); 229 if ((overcommit & SWAP_RESERVE_RLIMIT_ON) != 0 && 230 uip->ui_vmsize + incr > lim_cur(curproc, RLIMIT_SWAP) && 231 priv_check(curthread, PRIV_VM_SWAP_NORLIMIT)) 232 res = 0; 233 else 234 uip->ui_vmsize += incr; 235 UIDINFO_VMSIZE_UNLOCK(uip); 236 PROC_UNLOCK(curproc); 237 if (!res) { 238 mtx_lock(&sw_dev_mtx); 239 swap_reserved -= incr; 240 mtx_unlock(&sw_dev_mtx); 241 } 242 } 243 if (!res && ppsratecheck(&lastfail, &curfail, 1)) { 244 printf("uid %d, pid %d: swap reservation for %jd bytes failed\n", 245 uip->ui_uid, curproc->p_pid, incr); 246 } 247 248 #ifdef RACCT 249 if (!res) { 250 PROC_LOCK(curproc); 251 racct_sub(curproc, RACCT_SWAP, incr); 252 PROC_UNLOCK(curproc); 253 } 254 #endif 255 256 return (res); 257 } 258 259 void 260 swap_reserve_force(vm_ooffset_t incr) 261 { 262 struct uidinfo *uip; 263 264 mtx_lock(&sw_dev_mtx); 265 swap_reserved += incr; 266 mtx_unlock(&sw_dev_mtx); 267 268 #ifdef RACCT 269 PROC_LOCK(curproc); 270 racct_add_force(curproc, RACCT_SWAP, incr); 271 PROC_UNLOCK(curproc); 272 #endif 273 274 uip = curthread->td_ucred->cr_ruidinfo; 275 PROC_LOCK(curproc); 276 UIDINFO_VMSIZE_LOCK(uip); 277 uip->ui_vmsize += incr; 278 UIDINFO_VMSIZE_UNLOCK(uip); 279 PROC_UNLOCK(curproc); 280 } 281 282 void 283 swap_release(vm_ooffset_t decr) 284 { 285 struct ucred *cred; 286 287 PROC_LOCK(curproc); 288 cred = curthread->td_ucred; 289 swap_release_by_cred(decr, cred); 290 PROC_UNLOCK(curproc); 291 } 292 293 void 294 swap_release_by_cred(vm_ooffset_t decr, struct ucred *cred) 295 { 296 struct uidinfo *uip; 297 298 uip = cred->cr_ruidinfo; 299 300 if (decr & PAGE_MASK) 301 panic("swap_release: & PAGE_MASK"); 302 303 mtx_lock(&sw_dev_mtx); 304 if (swap_reserved < decr) 305 panic("swap_reserved < decr"); 306 swap_reserved -= decr; 307 mtx_unlock(&sw_dev_mtx); 308 309 UIDINFO_VMSIZE_LOCK(uip); 310 if (uip->ui_vmsize < decr) 311 printf("negative vmsize for uid = %d\n", uip->ui_uid); 312 uip->ui_vmsize -= decr; 313 UIDINFO_VMSIZE_UNLOCK(uip); 314 315 racct_sub_cred(cred, RACCT_SWAP, decr); 316 } 317 318 static void swapdev_strategy(struct buf *, struct swdevt *sw); 319 320 #define SWM_FREE 0x02 /* free, period */ 321 #define SWM_POP 0x04 /* pop out */ 322 323 int swap_pager_full = 2; /* swap space exhaustion (task killing) */ 324 static int swap_pager_almost_full = 1; /* swap space exhaustion (w/hysteresis)*/ 325 static int nsw_rcount; /* free read buffers */ 326 static int nsw_wcount_sync; /* limit write buffers / synchronous */ 327 static int nsw_wcount_async; /* limit write buffers / asynchronous */ 328 static int nsw_wcount_async_max;/* assigned maximum */ 329 static int nsw_cluster_max; /* maximum VOP I/O allowed */ 330 331 static int sysctl_swap_async_max(SYSCTL_HANDLER_ARGS); 332 SYSCTL_PROC(_vm, OID_AUTO, swap_async_max, CTLTYPE_INT | CTLFLAG_RW, 333 NULL, 0, sysctl_swap_async_max, "I", "Maximum running async swap ops"); 334 335 static struct swblock **swhash; 336 static int swhash_mask; 337 static struct mtx swhash_mtx; 338 339 static struct sx sw_alloc_sx; 340 341 /* 342 * "named" and "unnamed" anon region objects. Try to reduce the overhead 343 * of searching a named list by hashing it just a little. 344 */ 345 346 #define NOBJLISTS 8 347 348 #define NOBJLIST(handle) \ 349 (&swap_pager_object_list[((int)(intptr_t)handle >> 4) & (NOBJLISTS-1)]) 350 351 static struct mtx sw_alloc_mtx; /* protect list manipulation */ 352 static struct pagerlst swap_pager_object_list[NOBJLISTS]; 353 static uma_zone_t swap_zone; 354 355 /* 356 * pagerops for OBJT_SWAP - "swap pager". Some ops are also global procedure 357 * calls hooked from other parts of the VM system and do not appear here. 358 * (see vm/swap_pager.h). 359 */ 360 static vm_object_t 361 swap_pager_alloc(void *handle, vm_ooffset_t size, 362 vm_prot_t prot, vm_ooffset_t offset, struct ucred *); 363 static void swap_pager_dealloc(vm_object_t object); 364 static int swap_pager_getpages(vm_object_t, vm_page_t *, int, int); 365 static int swap_pager_getpages_async(vm_object_t, vm_page_t *, int, int, 366 pgo_getpages_iodone_t, void *); 367 static void swap_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *); 368 static boolean_t 369 swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, int *after); 370 static void swap_pager_init(void); 371 static void swap_pager_unswapped(vm_page_t); 372 static void swap_pager_swapoff(struct swdevt *sp); 373 374 struct pagerops swappagerops = { 375 .pgo_init = swap_pager_init, /* early system initialization of pager */ 376 .pgo_alloc = swap_pager_alloc, /* allocate an OBJT_SWAP object */ 377 .pgo_dealloc = swap_pager_dealloc, /* deallocate an OBJT_SWAP object */ 378 .pgo_getpages = swap_pager_getpages, /* pagein */ 379 .pgo_getpages_async = swap_pager_getpages_async, /* pagein (async) */ 380 .pgo_putpages = swap_pager_putpages, /* pageout */ 381 .pgo_haspage = swap_pager_haspage, /* get backing store status for page */ 382 .pgo_pageunswapped = swap_pager_unswapped, /* remove swap related to page */ 383 }; 384 385 /* 386 * dmmax is in page-sized chunks with the new swap system. It was 387 * dev-bsized chunks in the old. dmmax is always a power of 2. 388 * 389 * swap_*() routines are externally accessible. swp_*() routines are 390 * internal. 391 */ 392 static int dmmax; 393 static int nswap_lowat = 128; /* in pages, swap_pager_almost_full warn */ 394 static int nswap_hiwat = 512; /* in pages, swap_pager_almost_full warn */ 395 396 SYSCTL_INT(_vm, OID_AUTO, dmmax, 397 CTLFLAG_RD, &dmmax, 0, "Maximum size of a swap block"); 398 399 static void swp_sizecheck(void); 400 static void swp_pager_async_iodone(struct buf *bp); 401 static int swapongeom(struct thread *, struct vnode *); 402 static int swaponvp(struct thread *, struct vnode *, u_long); 403 static int swapoff_one(struct swdevt *sp, struct ucred *cred); 404 405 /* 406 * Swap bitmap functions 407 */ 408 static void swp_pager_freeswapspace(daddr_t blk, int npages); 409 static daddr_t swp_pager_getswapspace(int npages); 410 411 /* 412 * Metadata functions 413 */ 414 static struct swblock **swp_pager_hash(vm_object_t object, vm_pindex_t index); 415 static void swp_pager_meta_build(vm_object_t, vm_pindex_t, daddr_t); 416 static void swp_pager_meta_free(vm_object_t, vm_pindex_t, daddr_t); 417 static void swp_pager_meta_free_all(vm_object_t); 418 static daddr_t swp_pager_meta_ctl(vm_object_t, vm_pindex_t, int); 419 420 static void 421 swp_pager_free_nrpage(vm_page_t m) 422 { 423 424 vm_page_lock(m); 425 if (m->wire_count == 0) 426 vm_page_free(m); 427 vm_page_unlock(m); 428 } 429 430 /* 431 * SWP_SIZECHECK() - update swap_pager_full indication 432 * 433 * update the swap_pager_almost_full indication and warn when we are 434 * about to run out of swap space, using lowat/hiwat hysteresis. 435 * 436 * Clear swap_pager_full ( task killing ) indication when lowat is met. 437 * 438 * No restrictions on call 439 * This routine may not block. 440 */ 441 static void 442 swp_sizecheck(void) 443 { 444 445 if (swap_pager_avail < nswap_lowat) { 446 if (swap_pager_almost_full == 0) { 447 printf("swap_pager: out of swap space\n"); 448 swap_pager_almost_full = 1; 449 } 450 } else { 451 swap_pager_full = 0; 452 if (swap_pager_avail > nswap_hiwat) 453 swap_pager_almost_full = 0; 454 } 455 } 456 457 /* 458 * SWP_PAGER_HASH() - hash swap meta data 459 * 460 * This is an helper function which hashes the swapblk given 461 * the object and page index. It returns a pointer to a pointer 462 * to the object, or a pointer to a NULL pointer if it could not 463 * find a swapblk. 464 */ 465 static struct swblock ** 466 swp_pager_hash(vm_object_t object, vm_pindex_t index) 467 { 468 struct swblock **pswap; 469 struct swblock *swap; 470 471 index &= ~(vm_pindex_t)SWAP_META_MASK; 472 pswap = &swhash[(index ^ (int)(intptr_t)object) & swhash_mask]; 473 while ((swap = *pswap) != NULL) { 474 if (swap->swb_object == object && 475 swap->swb_index == index 476 ) { 477 break; 478 } 479 pswap = &swap->swb_hnext; 480 } 481 return (pswap); 482 } 483 484 /* 485 * SWAP_PAGER_INIT() - initialize the swap pager! 486 * 487 * Expected to be started from system init. NOTE: This code is run 488 * before much else so be careful what you depend on. Most of the VM 489 * system has yet to be initialized at this point. 490 */ 491 static void 492 swap_pager_init(void) 493 { 494 /* 495 * Initialize object lists 496 */ 497 int i; 498 499 for (i = 0; i < NOBJLISTS; ++i) 500 TAILQ_INIT(&swap_pager_object_list[i]); 501 mtx_init(&sw_alloc_mtx, "swap_pager list", NULL, MTX_DEF); 502 mtx_init(&sw_dev_mtx, "swapdev", NULL, MTX_DEF); 503 504 /* 505 * Device Stripe, in PAGE_SIZE'd blocks 506 */ 507 dmmax = SWB_NPAGES * 2; 508 } 509 510 /* 511 * SWAP_PAGER_SWAP_INIT() - swap pager initialization from pageout process 512 * 513 * Expected to be started from pageout process once, prior to entering 514 * its main loop. 515 */ 516 void 517 swap_pager_swap_init(void) 518 { 519 unsigned long n, n2; 520 521 /* 522 * Number of in-transit swap bp operations. Don't 523 * exhaust the pbufs completely. Make sure we 524 * initialize workable values (0 will work for hysteresis 525 * but it isn't very efficient). 526 * 527 * The nsw_cluster_max is constrained by the bp->b_pages[] 528 * array (MAXPHYS/PAGE_SIZE) and our locally defined 529 * MAX_PAGEOUT_CLUSTER. Also be aware that swap ops are 530 * constrained by the swap device interleave stripe size. 531 * 532 * Currently we hardwire nsw_wcount_async to 4. This limit is 533 * designed to prevent other I/O from having high latencies due to 534 * our pageout I/O. The value 4 works well for one or two active swap 535 * devices but is probably a little low if you have more. Even so, 536 * a higher value would probably generate only a limited improvement 537 * with three or four active swap devices since the system does not 538 * typically have to pageout at extreme bandwidths. We will want 539 * at least 2 per swap devices, and 4 is a pretty good value if you 540 * have one NFS swap device due to the command/ack latency over NFS. 541 * So it all works out pretty well. 542 */ 543 nsw_cluster_max = min((MAXPHYS/PAGE_SIZE), MAX_PAGEOUT_CLUSTER); 544 545 mtx_lock(&pbuf_mtx); 546 nsw_rcount = (nswbuf + 1) / 2; 547 nsw_wcount_sync = (nswbuf + 3) / 4; 548 nsw_wcount_async = 4; 549 nsw_wcount_async_max = nsw_wcount_async; 550 mtx_unlock(&pbuf_mtx); 551 552 /* 553 * Initialize our zone. Right now I'm just guessing on the number 554 * we need based on the number of pages in the system. Each swblock 555 * can hold 32 pages, so this is probably overkill. This reservation 556 * is typically limited to around 32MB by default. 557 */ 558 n = vm_cnt.v_page_count / 2; 559 if (maxswzone && n > maxswzone / sizeof(struct swblock)) 560 n = maxswzone / sizeof(struct swblock); 561 n2 = n; 562 swap_zone = uma_zcreate("SWAPMETA", sizeof(struct swblock), NULL, NULL, 563 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE | UMA_ZONE_VM); 564 if (swap_zone == NULL) 565 panic("failed to create swap_zone."); 566 do { 567 if (uma_zone_reserve_kva(swap_zone, n)) 568 break; 569 /* 570 * if the allocation failed, try a zone two thirds the 571 * size of the previous attempt. 572 */ 573 n -= ((n + 2) / 3); 574 } while (n > 0); 575 if (n2 != n) 576 printf("Swap zone entries reduced from %lu to %lu.\n", n2, n); 577 swap_maxpages = n * SWAP_META_PAGES; 578 swzone = n * sizeof(struct swblock); 579 n2 = n; 580 581 /* 582 * Initialize our meta-data hash table. The swapper does not need to 583 * be quite as efficient as the VM system, so we do not use an 584 * oversized hash table. 585 * 586 * n: size of hash table, must be power of 2 587 * swhash_mask: hash table index mask 588 */ 589 for (n = 1; n < n2 / 8; n *= 2) 590 ; 591 swhash = malloc(sizeof(struct swblock *) * n, M_VMPGDATA, M_WAITOK | M_ZERO); 592 swhash_mask = n - 1; 593 mtx_init(&swhash_mtx, "swap_pager swhash", NULL, MTX_DEF); 594 } 595 596 /* 597 * SWAP_PAGER_ALLOC() - allocate a new OBJT_SWAP VM object and instantiate 598 * its metadata structures. 599 * 600 * This routine is called from the mmap and fork code to create a new 601 * OBJT_SWAP object. We do this by creating an OBJT_DEFAULT object 602 * and then converting it with swp_pager_meta_build(). 603 * 604 * This routine may block in vm_object_allocate() and create a named 605 * object lookup race, so we must interlock. 606 * 607 * MPSAFE 608 */ 609 static vm_object_t 610 swap_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 611 vm_ooffset_t offset, struct ucred *cred) 612 { 613 vm_object_t object; 614 vm_pindex_t pindex; 615 616 pindex = OFF_TO_IDX(offset + PAGE_MASK + size); 617 if (handle) { 618 mtx_lock(&Giant); 619 /* 620 * Reference existing named region or allocate new one. There 621 * should not be a race here against swp_pager_meta_build() 622 * as called from vm_page_remove() in regards to the lookup 623 * of the handle. 624 */ 625 sx_xlock(&sw_alloc_sx); 626 object = vm_pager_object_lookup(NOBJLIST(handle), handle); 627 if (object == NULL) { 628 if (cred != NULL) { 629 if (!swap_reserve_by_cred(size, cred)) { 630 sx_xunlock(&sw_alloc_sx); 631 mtx_unlock(&Giant); 632 return (NULL); 633 } 634 crhold(cred); 635 } 636 object = vm_object_allocate(OBJT_DEFAULT, pindex); 637 VM_OBJECT_WLOCK(object); 638 object->handle = handle; 639 if (cred != NULL) { 640 object->cred = cred; 641 object->charge = size; 642 } 643 swp_pager_meta_build(object, 0, SWAPBLK_NONE); 644 VM_OBJECT_WUNLOCK(object); 645 } 646 sx_xunlock(&sw_alloc_sx); 647 mtx_unlock(&Giant); 648 } else { 649 if (cred != NULL) { 650 if (!swap_reserve_by_cred(size, cred)) 651 return (NULL); 652 crhold(cred); 653 } 654 object = vm_object_allocate(OBJT_DEFAULT, pindex); 655 VM_OBJECT_WLOCK(object); 656 if (cred != NULL) { 657 object->cred = cred; 658 object->charge = size; 659 } 660 swp_pager_meta_build(object, 0, SWAPBLK_NONE); 661 VM_OBJECT_WUNLOCK(object); 662 } 663 return (object); 664 } 665 666 /* 667 * SWAP_PAGER_DEALLOC() - remove swap metadata from object 668 * 669 * The swap backing for the object is destroyed. The code is 670 * designed such that we can reinstantiate it later, but this 671 * routine is typically called only when the entire object is 672 * about to be destroyed. 673 * 674 * The object must be locked. 675 */ 676 static void 677 swap_pager_dealloc(vm_object_t object) 678 { 679 680 /* 681 * Remove from list right away so lookups will fail if we block for 682 * pageout completion. 683 */ 684 if (object->handle != NULL) { 685 mtx_lock(&sw_alloc_mtx); 686 TAILQ_REMOVE(NOBJLIST(object->handle), object, pager_object_list); 687 mtx_unlock(&sw_alloc_mtx); 688 } 689 690 VM_OBJECT_ASSERT_WLOCKED(object); 691 vm_object_pip_wait(object, "swpdea"); 692 693 /* 694 * Free all remaining metadata. We only bother to free it from 695 * the swap meta data. We do not attempt to free swapblk's still 696 * associated with vm_page_t's for this object. We do not care 697 * if paging is still in progress on some objects. 698 */ 699 swp_pager_meta_free_all(object); 700 object->handle = NULL; 701 object->type = OBJT_DEAD; 702 } 703 704 /************************************************************************ 705 * SWAP PAGER BITMAP ROUTINES * 706 ************************************************************************/ 707 708 /* 709 * SWP_PAGER_GETSWAPSPACE() - allocate raw swap space 710 * 711 * Allocate swap for the requested number of pages. The starting 712 * swap block number (a page index) is returned or SWAPBLK_NONE 713 * if the allocation failed. 714 * 715 * Also has the side effect of advising that somebody made a mistake 716 * when they configured swap and didn't configure enough. 717 * 718 * This routine may not sleep. 719 * 720 * We allocate in round-robin fashion from the configured devices. 721 */ 722 static daddr_t 723 swp_pager_getswapspace(int npages) 724 { 725 daddr_t blk; 726 struct swdevt *sp; 727 int i; 728 729 blk = SWAPBLK_NONE; 730 mtx_lock(&sw_dev_mtx); 731 sp = swdevhd; 732 for (i = 0; i < nswapdev; i++) { 733 if (sp == NULL) 734 sp = TAILQ_FIRST(&swtailq); 735 if (!(sp->sw_flags & SW_CLOSING)) { 736 blk = blist_alloc(sp->sw_blist, npages); 737 if (blk != SWAPBLK_NONE) { 738 blk += sp->sw_first; 739 sp->sw_used += npages; 740 swap_pager_avail -= npages; 741 swp_sizecheck(); 742 swdevhd = TAILQ_NEXT(sp, sw_list); 743 goto done; 744 } 745 } 746 sp = TAILQ_NEXT(sp, sw_list); 747 } 748 if (swap_pager_full != 2) { 749 printf("swap_pager_getswapspace(%d): failed\n", npages); 750 swap_pager_full = 2; 751 swap_pager_almost_full = 1; 752 } 753 swdevhd = NULL; 754 done: 755 mtx_unlock(&sw_dev_mtx); 756 return (blk); 757 } 758 759 static int 760 swp_pager_isondev(daddr_t blk, struct swdevt *sp) 761 { 762 763 return (blk >= sp->sw_first && blk < sp->sw_end); 764 } 765 766 static void 767 swp_pager_strategy(struct buf *bp) 768 { 769 struct swdevt *sp; 770 771 mtx_lock(&sw_dev_mtx); 772 TAILQ_FOREACH(sp, &swtailq, sw_list) { 773 if (bp->b_blkno >= sp->sw_first && bp->b_blkno < sp->sw_end) { 774 mtx_unlock(&sw_dev_mtx); 775 if ((sp->sw_flags & SW_UNMAPPED) != 0 && 776 unmapped_buf_allowed) { 777 bp->b_kvaalloc = bp->b_data; 778 bp->b_data = unmapped_buf; 779 bp->b_kvabase = unmapped_buf; 780 bp->b_offset = 0; 781 bp->b_flags |= B_UNMAPPED; 782 } else { 783 pmap_qenter((vm_offset_t)bp->b_data, 784 &bp->b_pages[0], bp->b_bcount / PAGE_SIZE); 785 } 786 sp->sw_strategy(bp, sp); 787 return; 788 } 789 } 790 panic("Swapdev not found"); 791 } 792 793 794 /* 795 * SWP_PAGER_FREESWAPSPACE() - free raw swap space 796 * 797 * This routine returns the specified swap blocks back to the bitmap. 798 * 799 * This routine may not sleep. 800 */ 801 static void 802 swp_pager_freeswapspace(daddr_t blk, int npages) 803 { 804 struct swdevt *sp; 805 806 mtx_lock(&sw_dev_mtx); 807 TAILQ_FOREACH(sp, &swtailq, sw_list) { 808 if (blk >= sp->sw_first && blk < sp->sw_end) { 809 sp->sw_used -= npages; 810 /* 811 * If we are attempting to stop swapping on 812 * this device, we don't want to mark any 813 * blocks free lest they be reused. 814 */ 815 if ((sp->sw_flags & SW_CLOSING) == 0) { 816 blist_free(sp->sw_blist, blk - sp->sw_first, 817 npages); 818 swap_pager_avail += npages; 819 swp_sizecheck(); 820 } 821 mtx_unlock(&sw_dev_mtx); 822 return; 823 } 824 } 825 panic("Swapdev not found"); 826 } 827 828 /* 829 * SWAP_PAGER_FREESPACE() - frees swap blocks associated with a page 830 * range within an object. 831 * 832 * This is a globally accessible routine. 833 * 834 * This routine removes swapblk assignments from swap metadata. 835 * 836 * The external callers of this routine typically have already destroyed 837 * or renamed vm_page_t's associated with this range in the object so 838 * we should be ok. 839 * 840 * The object must be locked. 841 */ 842 void 843 swap_pager_freespace(vm_object_t object, vm_pindex_t start, vm_size_t size) 844 { 845 846 swp_pager_meta_free(object, start, size); 847 } 848 849 /* 850 * SWAP_PAGER_RESERVE() - reserve swap blocks in object 851 * 852 * Assigns swap blocks to the specified range within the object. The 853 * swap blocks are not zeroed. Any previous swap assignment is destroyed. 854 * 855 * Returns 0 on success, -1 on failure. 856 */ 857 int 858 swap_pager_reserve(vm_object_t object, vm_pindex_t start, vm_size_t size) 859 { 860 int n = 0; 861 daddr_t blk = SWAPBLK_NONE; 862 vm_pindex_t beg = start; /* save start index */ 863 864 VM_OBJECT_WLOCK(object); 865 while (size) { 866 if (n == 0) { 867 n = BLIST_MAX_ALLOC; 868 while ((blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE) { 869 n >>= 1; 870 if (n == 0) { 871 swp_pager_meta_free(object, beg, start - beg); 872 VM_OBJECT_WUNLOCK(object); 873 return (-1); 874 } 875 } 876 } 877 swp_pager_meta_build(object, start, blk); 878 --size; 879 ++start; 880 ++blk; 881 --n; 882 } 883 swp_pager_meta_free(object, start, n); 884 VM_OBJECT_WUNLOCK(object); 885 return (0); 886 } 887 888 /* 889 * SWAP_PAGER_COPY() - copy blocks from source pager to destination pager 890 * and destroy the source. 891 * 892 * Copy any valid swapblks from the source to the destination. In 893 * cases where both the source and destination have a valid swapblk, 894 * we keep the destination's. 895 * 896 * This routine is allowed to sleep. It may sleep allocating metadata 897 * indirectly through swp_pager_meta_build() or if paging is still in 898 * progress on the source. 899 * 900 * The source object contains no vm_page_t's (which is just as well) 901 * 902 * The source object is of type OBJT_SWAP. 903 * 904 * The source and destination objects must be locked. 905 * Both object locks may temporarily be released. 906 */ 907 void 908 swap_pager_copy(vm_object_t srcobject, vm_object_t dstobject, 909 vm_pindex_t offset, int destroysource) 910 { 911 vm_pindex_t i; 912 913 VM_OBJECT_ASSERT_WLOCKED(srcobject); 914 VM_OBJECT_ASSERT_WLOCKED(dstobject); 915 916 /* 917 * If destroysource is set, we remove the source object from the 918 * swap_pager internal queue now. 919 */ 920 if (destroysource) { 921 if (srcobject->handle != NULL) { 922 mtx_lock(&sw_alloc_mtx); 923 TAILQ_REMOVE( 924 NOBJLIST(srcobject->handle), 925 srcobject, 926 pager_object_list 927 ); 928 mtx_unlock(&sw_alloc_mtx); 929 } 930 } 931 932 /* 933 * transfer source to destination. 934 */ 935 for (i = 0; i < dstobject->size; ++i) { 936 daddr_t dstaddr; 937 938 /* 939 * Locate (without changing) the swapblk on the destination, 940 * unless it is invalid in which case free it silently, or 941 * if the destination is a resident page, in which case the 942 * source is thrown away. 943 */ 944 dstaddr = swp_pager_meta_ctl(dstobject, i, 0); 945 946 if (dstaddr == SWAPBLK_NONE) { 947 /* 948 * Destination has no swapblk and is not resident, 949 * copy source. 950 */ 951 daddr_t srcaddr; 952 953 srcaddr = swp_pager_meta_ctl( 954 srcobject, 955 i + offset, 956 SWM_POP 957 ); 958 959 if (srcaddr != SWAPBLK_NONE) { 960 /* 961 * swp_pager_meta_build() can sleep. 962 */ 963 vm_object_pip_add(srcobject, 1); 964 VM_OBJECT_WUNLOCK(srcobject); 965 vm_object_pip_add(dstobject, 1); 966 swp_pager_meta_build(dstobject, i, srcaddr); 967 vm_object_pip_wakeup(dstobject); 968 VM_OBJECT_WLOCK(srcobject); 969 vm_object_pip_wakeup(srcobject); 970 } 971 } else { 972 /* 973 * Destination has valid swapblk or it is represented 974 * by a resident page. We destroy the sourceblock. 975 */ 976 977 swp_pager_meta_ctl(srcobject, i + offset, SWM_FREE); 978 } 979 } 980 981 /* 982 * Free left over swap blocks in source. 983 * 984 * We have to revert the type to OBJT_DEFAULT so we do not accidently 985 * double-remove the object from the swap queues. 986 */ 987 if (destroysource) { 988 swp_pager_meta_free_all(srcobject); 989 /* 990 * Reverting the type is not necessary, the caller is going 991 * to destroy srcobject directly, but I'm doing it here 992 * for consistency since we've removed the object from its 993 * queues. 994 */ 995 srcobject->type = OBJT_DEFAULT; 996 } 997 } 998 999 /* 1000 * SWAP_PAGER_HASPAGE() - determine if we have good backing store for 1001 * the requested page. 1002 * 1003 * We determine whether good backing store exists for the requested 1004 * page and return TRUE if it does, FALSE if it doesn't. 1005 * 1006 * If TRUE, we also try to determine how much valid, contiguous backing 1007 * store exists before and after the requested page within a reasonable 1008 * distance. We do not try to restrict it to the swap device stripe 1009 * (that is handled in getpages/putpages). It probably isn't worth 1010 * doing here. 1011 */ 1012 static boolean_t 1013 swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, int *after) 1014 { 1015 daddr_t blk0; 1016 1017 VM_OBJECT_ASSERT_LOCKED(object); 1018 /* 1019 * do we have good backing store at the requested index ? 1020 */ 1021 blk0 = swp_pager_meta_ctl(object, pindex, 0); 1022 1023 if (blk0 == SWAPBLK_NONE) { 1024 if (before) 1025 *before = 0; 1026 if (after) 1027 *after = 0; 1028 return (FALSE); 1029 } 1030 1031 /* 1032 * find backwards-looking contiguous good backing store 1033 */ 1034 if (before != NULL) { 1035 int i; 1036 1037 for (i = 1; i < (SWB_NPAGES/2); ++i) { 1038 daddr_t blk; 1039 1040 if (i > pindex) 1041 break; 1042 blk = swp_pager_meta_ctl(object, pindex - i, 0); 1043 if (blk != blk0 - i) 1044 break; 1045 } 1046 *before = (i - 1); 1047 } 1048 1049 /* 1050 * find forward-looking contiguous good backing store 1051 */ 1052 if (after != NULL) { 1053 int i; 1054 1055 for (i = 1; i < (SWB_NPAGES/2); ++i) { 1056 daddr_t blk; 1057 1058 blk = swp_pager_meta_ctl(object, pindex + i, 0); 1059 if (blk != blk0 + i) 1060 break; 1061 } 1062 *after = (i - 1); 1063 } 1064 return (TRUE); 1065 } 1066 1067 /* 1068 * SWAP_PAGER_PAGE_UNSWAPPED() - remove swap backing store related to page 1069 * 1070 * This removes any associated swap backing store, whether valid or 1071 * not, from the page. 1072 * 1073 * This routine is typically called when a page is made dirty, at 1074 * which point any associated swap can be freed. MADV_FREE also 1075 * calls us in a special-case situation 1076 * 1077 * NOTE!!! If the page is clean and the swap was valid, the caller 1078 * should make the page dirty before calling this routine. This routine 1079 * does NOT change the m->dirty status of the page. Also: MADV_FREE 1080 * depends on it. 1081 * 1082 * This routine may not sleep. 1083 * 1084 * The object containing the page must be locked. 1085 */ 1086 static void 1087 swap_pager_unswapped(vm_page_t m) 1088 { 1089 1090 swp_pager_meta_ctl(m->object, m->pindex, SWM_FREE); 1091 } 1092 1093 /* 1094 * SWAP_PAGER_GETPAGES() - bring pages in from swap 1095 * 1096 * Attempt to retrieve (m, count) pages from backing store, but make 1097 * sure we retrieve at least m[reqpage]. We try to load in as large 1098 * a chunk surrounding m[reqpage] as is contiguous in swap and which 1099 * belongs to the same object. 1100 * 1101 * The code is designed for asynchronous operation and 1102 * immediate-notification of 'reqpage' but tends not to be 1103 * used that way. Please do not optimize-out this algorithmic 1104 * feature, I intend to improve on it in the future. 1105 * 1106 * The parent has a single vm_object_pip_add() reference prior to 1107 * calling us and we should return with the same. 1108 * 1109 * The parent has BUSY'd the pages. We should return with 'm' 1110 * left busy, but the others adjusted. 1111 */ 1112 static int 1113 swap_pager_getpages(vm_object_t object, vm_page_t *m, int count, int reqpage) 1114 { 1115 struct buf *bp; 1116 vm_page_t mreq; 1117 int i; 1118 int j; 1119 daddr_t blk; 1120 1121 mreq = m[reqpage]; 1122 1123 KASSERT(mreq->object == object, 1124 ("swap_pager_getpages: object mismatch %p/%p", 1125 object, mreq->object)); 1126 1127 /* 1128 * Calculate range to retrieve. The pages have already been assigned 1129 * their swapblks. We require a *contiguous* range but we know it to 1130 * not span devices. If we do not supply it, bad things 1131 * happen. Note that blk, iblk & jblk can be SWAPBLK_NONE, but the 1132 * loops are set up such that the case(s) are handled implicitly. 1133 * 1134 * The swp_*() calls must be made with the object locked. 1135 */ 1136 blk = swp_pager_meta_ctl(mreq->object, mreq->pindex, 0); 1137 1138 for (i = reqpage - 1; i >= 0; --i) { 1139 daddr_t iblk; 1140 1141 iblk = swp_pager_meta_ctl(m[i]->object, m[i]->pindex, 0); 1142 if (blk != iblk + (reqpage - i)) 1143 break; 1144 } 1145 ++i; 1146 1147 for (j = reqpage + 1; j < count; ++j) { 1148 daddr_t jblk; 1149 1150 jblk = swp_pager_meta_ctl(m[j]->object, m[j]->pindex, 0); 1151 if (blk != jblk - (j - reqpage)) 1152 break; 1153 } 1154 1155 /* 1156 * free pages outside our collection range. Note: we never free 1157 * mreq, it must remain busy throughout. 1158 */ 1159 if (0 < i || j < count) { 1160 int k; 1161 1162 for (k = 0; k < i; ++k) 1163 swp_pager_free_nrpage(m[k]); 1164 for (k = j; k < count; ++k) 1165 swp_pager_free_nrpage(m[k]); 1166 } 1167 1168 /* 1169 * Return VM_PAGER_FAIL if we have nothing to do. Return mreq 1170 * still busy, but the others unbusied. 1171 */ 1172 if (blk == SWAPBLK_NONE) 1173 return (VM_PAGER_FAIL); 1174 1175 /* 1176 * Getpbuf() can sleep. 1177 */ 1178 VM_OBJECT_WUNLOCK(object); 1179 /* 1180 * Get a swap buffer header to perform the IO 1181 */ 1182 bp = getpbuf(&nsw_rcount); 1183 bp->b_flags |= B_PAGING; 1184 1185 bp->b_iocmd = BIO_READ; 1186 bp->b_iodone = swp_pager_async_iodone; 1187 bp->b_rcred = crhold(thread0.td_ucred); 1188 bp->b_wcred = crhold(thread0.td_ucred); 1189 bp->b_blkno = blk - (reqpage - i); 1190 bp->b_bcount = PAGE_SIZE * (j - i); 1191 bp->b_bufsize = PAGE_SIZE * (j - i); 1192 bp->b_pager.pg_reqpage = reqpage - i; 1193 1194 VM_OBJECT_WLOCK(object); 1195 { 1196 int k; 1197 1198 for (k = i; k < j; ++k) { 1199 bp->b_pages[k - i] = m[k]; 1200 m[k]->oflags |= VPO_SWAPINPROG; 1201 } 1202 } 1203 bp->b_npages = j - i; 1204 1205 PCPU_INC(cnt.v_swapin); 1206 PCPU_ADD(cnt.v_swappgsin, bp->b_npages); 1207 1208 /* 1209 * We still hold the lock on mreq, and our automatic completion routine 1210 * does not remove it. 1211 */ 1212 vm_object_pip_add(object, bp->b_npages); 1213 VM_OBJECT_WUNLOCK(object); 1214 1215 /* 1216 * perform the I/O. NOTE!!! bp cannot be considered valid after 1217 * this point because we automatically release it on completion. 1218 * Instead, we look at the one page we are interested in which we 1219 * still hold a lock on even through the I/O completion. 1220 * 1221 * The other pages in our m[] array are also released on completion, 1222 * so we cannot assume they are valid anymore either. 1223 * 1224 * NOTE: b_blkno is destroyed by the call to swapdev_strategy 1225 */ 1226 BUF_KERNPROC(bp); 1227 swp_pager_strategy(bp); 1228 1229 /* 1230 * wait for the page we want to complete. VPO_SWAPINPROG is always 1231 * cleared on completion. If an I/O error occurs, SWAPBLK_NONE 1232 * is set in the meta-data. 1233 */ 1234 VM_OBJECT_WLOCK(object); 1235 while ((mreq->oflags & VPO_SWAPINPROG) != 0) { 1236 mreq->oflags |= VPO_SWAPSLEEP; 1237 PCPU_INC(cnt.v_intrans); 1238 if (VM_OBJECT_SLEEP(object, &object->paging_in_progress, PSWP, 1239 "swread", hz * 20)) { 1240 printf( 1241 "swap_pager: indefinite wait buffer: bufobj: %p, blkno: %jd, size: %ld\n", 1242 bp->b_bufobj, (intmax_t)bp->b_blkno, bp->b_bcount); 1243 } 1244 } 1245 1246 /* 1247 * mreq is left busied after completion, but all the other pages 1248 * are freed. If we had an unrecoverable read error the page will 1249 * not be valid. 1250 */ 1251 if (mreq->valid != VM_PAGE_BITS_ALL) { 1252 return (VM_PAGER_ERROR); 1253 } else { 1254 return (VM_PAGER_OK); 1255 } 1256 1257 /* 1258 * A final note: in a low swap situation, we cannot deallocate swap 1259 * and mark a page dirty here because the caller is likely to mark 1260 * the page clean when we return, causing the page to possibly revert 1261 * to all-zero's later. 1262 */ 1263 } 1264 1265 /* 1266 * swap_pager_getpages_async(): 1267 * 1268 * Right now this is emulation of asynchronous operation on top of 1269 * swap_pager_getpages(). 1270 */ 1271 static int 1272 swap_pager_getpages_async(vm_object_t object, vm_page_t *m, int count, 1273 int reqpage, pgo_getpages_iodone_t iodone, void *arg) 1274 { 1275 int r, error; 1276 1277 r = swap_pager_getpages(object, m, count, reqpage); 1278 VM_OBJECT_WUNLOCK(object); 1279 switch (r) { 1280 case VM_PAGER_OK: 1281 error = 0; 1282 break; 1283 case VM_PAGER_ERROR: 1284 error = EIO; 1285 break; 1286 case VM_PAGER_FAIL: 1287 error = EINVAL; 1288 break; 1289 default: 1290 panic("unhandled swap_pager_getpages() error %d", r); 1291 } 1292 (iodone)(arg, m, count, error); 1293 VM_OBJECT_WLOCK(object); 1294 1295 return (r); 1296 } 1297 1298 /* 1299 * swap_pager_putpages: 1300 * 1301 * Assign swap (if necessary) and initiate I/O on the specified pages. 1302 * 1303 * We support both OBJT_DEFAULT and OBJT_SWAP objects. DEFAULT objects 1304 * are automatically converted to SWAP objects. 1305 * 1306 * In a low memory situation we may block in VOP_STRATEGY(), but the new 1307 * vm_page reservation system coupled with properly written VFS devices 1308 * should ensure that no low-memory deadlock occurs. This is an area 1309 * which needs work. 1310 * 1311 * The parent has N vm_object_pip_add() references prior to 1312 * calling us and will remove references for rtvals[] that are 1313 * not set to VM_PAGER_PEND. We need to remove the rest on I/O 1314 * completion. 1315 * 1316 * The parent has soft-busy'd the pages it passes us and will unbusy 1317 * those whos rtvals[] entry is not set to VM_PAGER_PEND on return. 1318 * We need to unbusy the rest on I/O completion. 1319 */ 1320 void 1321 swap_pager_putpages(vm_object_t object, vm_page_t *m, int count, 1322 int flags, int *rtvals) 1323 { 1324 int i, n; 1325 boolean_t sync; 1326 1327 if (count && m[0]->object != object) { 1328 panic("swap_pager_putpages: object mismatch %p/%p", 1329 object, 1330 m[0]->object 1331 ); 1332 } 1333 1334 /* 1335 * Step 1 1336 * 1337 * Turn object into OBJT_SWAP 1338 * check for bogus sysops 1339 * force sync if not pageout process 1340 */ 1341 if (object->type != OBJT_SWAP) 1342 swp_pager_meta_build(object, 0, SWAPBLK_NONE); 1343 VM_OBJECT_WUNLOCK(object); 1344 1345 n = 0; 1346 if (curproc != pageproc) 1347 sync = TRUE; 1348 else 1349 sync = (flags & VM_PAGER_PUT_SYNC) != 0; 1350 1351 /* 1352 * Step 2 1353 * 1354 * Assign swap blocks and issue I/O. We reallocate swap on the fly. 1355 * The page is left dirty until the pageout operation completes 1356 * successfully. 1357 */ 1358 for (i = 0; i < count; i += n) { 1359 int j; 1360 struct buf *bp; 1361 daddr_t blk; 1362 1363 /* 1364 * Maximum I/O size is limited by a number of factors. 1365 */ 1366 n = min(BLIST_MAX_ALLOC, count - i); 1367 n = min(n, nsw_cluster_max); 1368 1369 /* 1370 * Get biggest block of swap we can. If we fail, fall 1371 * back and try to allocate a smaller block. Don't go 1372 * overboard trying to allocate space if it would overly 1373 * fragment swap. 1374 */ 1375 while ( 1376 (blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE && 1377 n > 4 1378 ) { 1379 n >>= 1; 1380 } 1381 if (blk == SWAPBLK_NONE) { 1382 for (j = 0; j < n; ++j) 1383 rtvals[i+j] = VM_PAGER_FAIL; 1384 continue; 1385 } 1386 1387 /* 1388 * All I/O parameters have been satisfied, build the I/O 1389 * request and assign the swap space. 1390 */ 1391 if (sync == TRUE) { 1392 bp = getpbuf(&nsw_wcount_sync); 1393 } else { 1394 bp = getpbuf(&nsw_wcount_async); 1395 bp->b_flags = B_ASYNC; 1396 } 1397 bp->b_flags |= B_PAGING; 1398 bp->b_iocmd = BIO_WRITE; 1399 1400 bp->b_rcred = crhold(thread0.td_ucred); 1401 bp->b_wcred = crhold(thread0.td_ucred); 1402 bp->b_bcount = PAGE_SIZE * n; 1403 bp->b_bufsize = PAGE_SIZE * n; 1404 bp->b_blkno = blk; 1405 1406 VM_OBJECT_WLOCK(object); 1407 for (j = 0; j < n; ++j) { 1408 vm_page_t mreq = m[i+j]; 1409 1410 swp_pager_meta_build( 1411 mreq->object, 1412 mreq->pindex, 1413 blk + j 1414 ); 1415 vm_page_dirty(mreq); 1416 rtvals[i+j] = VM_PAGER_OK; 1417 1418 mreq->oflags |= VPO_SWAPINPROG; 1419 bp->b_pages[j] = mreq; 1420 } 1421 VM_OBJECT_WUNLOCK(object); 1422 bp->b_npages = n; 1423 /* 1424 * Must set dirty range for NFS to work. 1425 */ 1426 bp->b_dirtyoff = 0; 1427 bp->b_dirtyend = bp->b_bcount; 1428 1429 PCPU_INC(cnt.v_swapout); 1430 PCPU_ADD(cnt.v_swappgsout, bp->b_npages); 1431 1432 /* 1433 * asynchronous 1434 * 1435 * NOTE: b_blkno is destroyed by the call to swapdev_strategy 1436 */ 1437 if (sync == FALSE) { 1438 bp->b_iodone = swp_pager_async_iodone; 1439 BUF_KERNPROC(bp); 1440 swp_pager_strategy(bp); 1441 1442 for (j = 0; j < n; ++j) 1443 rtvals[i+j] = VM_PAGER_PEND; 1444 /* restart outter loop */ 1445 continue; 1446 } 1447 1448 /* 1449 * synchronous 1450 * 1451 * NOTE: b_blkno is destroyed by the call to swapdev_strategy 1452 */ 1453 bp->b_iodone = bdone; 1454 swp_pager_strategy(bp); 1455 1456 /* 1457 * Wait for the sync I/O to complete, then update rtvals. 1458 * We just set the rtvals[] to VM_PAGER_PEND so we can call 1459 * our async completion routine at the end, thus avoiding a 1460 * double-free. 1461 */ 1462 bwait(bp, PVM, "swwrt"); 1463 for (j = 0; j < n; ++j) 1464 rtvals[i+j] = VM_PAGER_PEND; 1465 /* 1466 * Now that we are through with the bp, we can call the 1467 * normal async completion, which frees everything up. 1468 */ 1469 swp_pager_async_iodone(bp); 1470 } 1471 VM_OBJECT_WLOCK(object); 1472 } 1473 1474 /* 1475 * swp_pager_async_iodone: 1476 * 1477 * Completion routine for asynchronous reads and writes from/to swap. 1478 * Also called manually by synchronous code to finish up a bp. 1479 * 1480 * This routine may not sleep. 1481 */ 1482 static void 1483 swp_pager_async_iodone(struct buf *bp) 1484 { 1485 int i; 1486 vm_object_t object = NULL; 1487 1488 /* 1489 * report error 1490 */ 1491 if (bp->b_ioflags & BIO_ERROR) { 1492 printf( 1493 "swap_pager: I/O error - %s failed; blkno %ld," 1494 "size %ld, error %d\n", 1495 ((bp->b_iocmd == BIO_READ) ? "pagein" : "pageout"), 1496 (long)bp->b_blkno, 1497 (long)bp->b_bcount, 1498 bp->b_error 1499 ); 1500 } 1501 1502 /* 1503 * remove the mapping for kernel virtual 1504 */ 1505 if ((bp->b_flags & B_UNMAPPED) != 0) { 1506 bp->b_data = bp->b_kvaalloc; 1507 bp->b_kvabase = bp->b_kvaalloc; 1508 bp->b_flags &= ~B_UNMAPPED; 1509 } else 1510 pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages); 1511 1512 if (bp->b_npages) { 1513 object = bp->b_pages[0]->object; 1514 VM_OBJECT_WLOCK(object); 1515 } 1516 1517 /* 1518 * cleanup pages. If an error occurs writing to swap, we are in 1519 * very serious trouble. If it happens to be a disk error, though, 1520 * we may be able to recover by reassigning the swap later on. So 1521 * in this case we remove the m->swapblk assignment for the page 1522 * but do not free it in the rlist. The errornous block(s) are thus 1523 * never reallocated as swap. Redirty the page and continue. 1524 */ 1525 for (i = 0; i < bp->b_npages; ++i) { 1526 vm_page_t m = bp->b_pages[i]; 1527 1528 m->oflags &= ~VPO_SWAPINPROG; 1529 if (m->oflags & VPO_SWAPSLEEP) { 1530 m->oflags &= ~VPO_SWAPSLEEP; 1531 wakeup(&object->paging_in_progress); 1532 } 1533 1534 if (bp->b_ioflags & BIO_ERROR) { 1535 /* 1536 * If an error occurs I'd love to throw the swapblk 1537 * away without freeing it back to swapspace, so it 1538 * can never be used again. But I can't from an 1539 * interrupt. 1540 */ 1541 if (bp->b_iocmd == BIO_READ) { 1542 /* 1543 * When reading, reqpage needs to stay 1544 * locked for the parent, but all other 1545 * pages can be freed. We still want to 1546 * wakeup the parent waiting on the page, 1547 * though. ( also: pg_reqpage can be -1 and 1548 * not match anything ). 1549 * 1550 * We have to wake specifically requested pages 1551 * up too because we cleared VPO_SWAPINPROG and 1552 * someone may be waiting for that. 1553 * 1554 * NOTE: for reads, m->dirty will probably 1555 * be overridden by the original caller of 1556 * getpages so don't play cute tricks here. 1557 */ 1558 m->valid = 0; 1559 if (i != bp->b_pager.pg_reqpage) 1560 swp_pager_free_nrpage(m); 1561 else { 1562 vm_page_lock(m); 1563 vm_page_flash(m); 1564 vm_page_unlock(m); 1565 } 1566 /* 1567 * If i == bp->b_pager.pg_reqpage, do not wake 1568 * the page up. The caller needs to. 1569 */ 1570 } else { 1571 /* 1572 * If a write error occurs, reactivate page 1573 * so it doesn't clog the inactive list, 1574 * then finish the I/O. 1575 */ 1576 vm_page_dirty(m); 1577 vm_page_lock(m); 1578 vm_page_activate(m); 1579 vm_page_unlock(m); 1580 vm_page_sunbusy(m); 1581 } 1582 } else if (bp->b_iocmd == BIO_READ) { 1583 /* 1584 * NOTE: for reads, m->dirty will probably be 1585 * overridden by the original caller of getpages so 1586 * we cannot set them in order to free the underlying 1587 * swap in a low-swap situation. I don't think we'd 1588 * want to do that anyway, but it was an optimization 1589 * that existed in the old swapper for a time before 1590 * it got ripped out due to precisely this problem. 1591 * 1592 * If not the requested page then deactivate it. 1593 * 1594 * Note that the requested page, reqpage, is left 1595 * busied, but we still have to wake it up. The 1596 * other pages are released (unbusied) by 1597 * vm_page_xunbusy(). 1598 */ 1599 KASSERT(!pmap_page_is_mapped(m), 1600 ("swp_pager_async_iodone: page %p is mapped", m)); 1601 m->valid = VM_PAGE_BITS_ALL; 1602 KASSERT(m->dirty == 0, 1603 ("swp_pager_async_iodone: page %p is dirty", m)); 1604 1605 /* 1606 * We have to wake specifically requested pages 1607 * up too because we cleared VPO_SWAPINPROG and 1608 * could be waiting for it in getpages. However, 1609 * be sure to not unbusy getpages specifically 1610 * requested page - getpages expects it to be 1611 * left busy. 1612 */ 1613 if (i != bp->b_pager.pg_reqpage) { 1614 vm_page_lock(m); 1615 vm_page_deactivate(m); 1616 vm_page_unlock(m); 1617 vm_page_xunbusy(m); 1618 } else { 1619 vm_page_lock(m); 1620 vm_page_flash(m); 1621 vm_page_unlock(m); 1622 } 1623 } else { 1624 /* 1625 * For write success, clear the dirty 1626 * status, then finish the I/O ( which decrements the 1627 * busy count and possibly wakes waiter's up ). 1628 */ 1629 KASSERT(!pmap_page_is_write_mapped(m), 1630 ("swp_pager_async_iodone: page %p is not write" 1631 " protected", m)); 1632 vm_page_undirty(m); 1633 vm_page_sunbusy(m); 1634 if (vm_page_count_severe()) { 1635 vm_page_lock(m); 1636 vm_page_try_to_cache(m); 1637 vm_page_unlock(m); 1638 } 1639 } 1640 } 1641 1642 /* 1643 * adjust pip. NOTE: the original parent may still have its own 1644 * pip refs on the object. 1645 */ 1646 if (object != NULL) { 1647 vm_object_pip_wakeupn(object, bp->b_npages); 1648 VM_OBJECT_WUNLOCK(object); 1649 } 1650 1651 /* 1652 * swapdev_strategy() manually sets b_vp and b_bufobj before calling 1653 * bstrategy(). Set them back to NULL now we're done with it, or we'll 1654 * trigger a KASSERT in relpbuf(). 1655 */ 1656 if (bp->b_vp) { 1657 bp->b_vp = NULL; 1658 bp->b_bufobj = NULL; 1659 } 1660 /* 1661 * release the physical I/O buffer 1662 */ 1663 relpbuf( 1664 bp, 1665 ((bp->b_iocmd == BIO_READ) ? &nsw_rcount : 1666 ((bp->b_flags & B_ASYNC) ? 1667 &nsw_wcount_async : 1668 &nsw_wcount_sync 1669 ) 1670 ) 1671 ); 1672 } 1673 1674 /* 1675 * swap_pager_isswapped: 1676 * 1677 * Return 1 if at least one page in the given object is paged 1678 * out to the given swap device. 1679 * 1680 * This routine may not sleep. 1681 */ 1682 int 1683 swap_pager_isswapped(vm_object_t object, struct swdevt *sp) 1684 { 1685 daddr_t index = 0; 1686 int bcount; 1687 int i; 1688 1689 VM_OBJECT_ASSERT_WLOCKED(object); 1690 if (object->type != OBJT_SWAP) 1691 return (0); 1692 1693 mtx_lock(&swhash_mtx); 1694 for (bcount = 0; bcount < object->un_pager.swp.swp_bcount; bcount++) { 1695 struct swblock *swap; 1696 1697 if ((swap = *swp_pager_hash(object, index)) != NULL) { 1698 for (i = 0; i < SWAP_META_PAGES; ++i) { 1699 if (swp_pager_isondev(swap->swb_pages[i], sp)) { 1700 mtx_unlock(&swhash_mtx); 1701 return (1); 1702 } 1703 } 1704 } 1705 index += SWAP_META_PAGES; 1706 } 1707 mtx_unlock(&swhash_mtx); 1708 return (0); 1709 } 1710 1711 /* 1712 * SWP_PAGER_FORCE_PAGEIN() - force a swap block to be paged in 1713 * 1714 * This routine dissociates the page at the given index within a 1715 * swap block from its backing store, paging it in if necessary. 1716 * If the page is paged in, it is placed in the inactive queue, 1717 * since it had its backing store ripped out from under it. 1718 * We also attempt to swap in all other pages in the swap block, 1719 * we only guarantee that the one at the specified index is 1720 * paged in. 1721 * 1722 * XXX - The code to page the whole block in doesn't work, so we 1723 * revert to the one-by-one behavior for now. Sigh. 1724 */ 1725 static inline void 1726 swp_pager_force_pagein(vm_object_t object, vm_pindex_t pindex) 1727 { 1728 vm_page_t m; 1729 1730 vm_object_pip_add(object, 1); 1731 m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL); 1732 if (m->valid == VM_PAGE_BITS_ALL) { 1733 vm_object_pip_wakeup(object); 1734 vm_page_dirty(m); 1735 vm_page_lock(m); 1736 vm_page_activate(m); 1737 vm_page_unlock(m); 1738 vm_page_xunbusy(m); 1739 vm_pager_page_unswapped(m); 1740 return; 1741 } 1742 1743 if (swap_pager_getpages(object, &m, 1, 0) != VM_PAGER_OK) 1744 panic("swap_pager_force_pagein: read from swap failed");/*XXX*/ 1745 vm_object_pip_wakeup(object); 1746 vm_page_dirty(m); 1747 vm_page_lock(m); 1748 vm_page_deactivate(m); 1749 vm_page_unlock(m); 1750 vm_page_xunbusy(m); 1751 vm_pager_page_unswapped(m); 1752 } 1753 1754 /* 1755 * swap_pager_swapoff: 1756 * 1757 * Page in all of the pages that have been paged out to the 1758 * given device. The corresponding blocks in the bitmap must be 1759 * marked as allocated and the device must be flagged SW_CLOSING. 1760 * There may be no processes swapped out to the device. 1761 * 1762 * This routine may block. 1763 */ 1764 static void 1765 swap_pager_swapoff(struct swdevt *sp) 1766 { 1767 struct swblock *swap; 1768 int i, j, retries; 1769 1770 GIANT_REQUIRED; 1771 1772 retries = 0; 1773 full_rescan: 1774 mtx_lock(&swhash_mtx); 1775 for (i = 0; i <= swhash_mask; i++) { /* '<=' is correct here */ 1776 restart: 1777 for (swap = swhash[i]; swap != NULL; swap = swap->swb_hnext) { 1778 vm_object_t object = swap->swb_object; 1779 vm_pindex_t pindex = swap->swb_index; 1780 for (j = 0; j < SWAP_META_PAGES; ++j) { 1781 if (swp_pager_isondev(swap->swb_pages[j], sp)) { 1782 /* avoid deadlock */ 1783 if (!VM_OBJECT_TRYWLOCK(object)) { 1784 break; 1785 } else { 1786 mtx_unlock(&swhash_mtx); 1787 swp_pager_force_pagein(object, 1788 pindex + j); 1789 VM_OBJECT_WUNLOCK(object); 1790 mtx_lock(&swhash_mtx); 1791 goto restart; 1792 } 1793 } 1794 } 1795 } 1796 } 1797 mtx_unlock(&swhash_mtx); 1798 if (sp->sw_used) { 1799 /* 1800 * Objects may be locked or paging to the device being 1801 * removed, so we will miss their pages and need to 1802 * make another pass. We have marked this device as 1803 * SW_CLOSING, so the activity should finish soon. 1804 */ 1805 retries++; 1806 if (retries > 100) { 1807 panic("swapoff: failed to locate %d swap blocks", 1808 sp->sw_used); 1809 } 1810 pause("swpoff", hz / 20); 1811 goto full_rescan; 1812 } 1813 } 1814 1815 /************************************************************************ 1816 * SWAP META DATA * 1817 ************************************************************************ 1818 * 1819 * These routines manipulate the swap metadata stored in the 1820 * OBJT_SWAP object. 1821 * 1822 * Swap metadata is implemented with a global hash and not directly 1823 * linked into the object. Instead the object simply contains 1824 * appropriate tracking counters. 1825 */ 1826 1827 /* 1828 * SWP_PAGER_META_BUILD() - add swap block to swap meta data for object 1829 * 1830 * We first convert the object to a swap object if it is a default 1831 * object. 1832 * 1833 * The specified swapblk is added to the object's swap metadata. If 1834 * the swapblk is not valid, it is freed instead. Any previously 1835 * assigned swapblk is freed. 1836 */ 1837 static void 1838 swp_pager_meta_build(vm_object_t object, vm_pindex_t pindex, daddr_t swapblk) 1839 { 1840 static volatile int exhausted; 1841 struct swblock *swap; 1842 struct swblock **pswap; 1843 int idx; 1844 1845 VM_OBJECT_ASSERT_WLOCKED(object); 1846 /* 1847 * Convert default object to swap object if necessary 1848 */ 1849 if (object->type != OBJT_SWAP) { 1850 object->type = OBJT_SWAP; 1851 object->un_pager.swp.swp_bcount = 0; 1852 1853 if (object->handle != NULL) { 1854 mtx_lock(&sw_alloc_mtx); 1855 TAILQ_INSERT_TAIL( 1856 NOBJLIST(object->handle), 1857 object, 1858 pager_object_list 1859 ); 1860 mtx_unlock(&sw_alloc_mtx); 1861 } 1862 } 1863 1864 /* 1865 * Locate hash entry. If not found create, but if we aren't adding 1866 * anything just return. If we run out of space in the map we wait 1867 * and, since the hash table may have changed, retry. 1868 */ 1869 retry: 1870 mtx_lock(&swhash_mtx); 1871 pswap = swp_pager_hash(object, pindex); 1872 1873 if ((swap = *pswap) == NULL) { 1874 int i; 1875 1876 if (swapblk == SWAPBLK_NONE) 1877 goto done; 1878 1879 swap = *pswap = uma_zalloc(swap_zone, M_NOWAIT | 1880 (curproc == pageproc ? M_USE_RESERVE : 0)); 1881 if (swap == NULL) { 1882 mtx_unlock(&swhash_mtx); 1883 VM_OBJECT_WUNLOCK(object); 1884 if (uma_zone_exhausted(swap_zone)) { 1885 if (atomic_cmpset_int(&exhausted, 0, 1)) 1886 printf("swap zone exhausted, " 1887 "increase kern.maxswzone\n"); 1888 vm_pageout_oom(VM_OOM_SWAPZ); 1889 pause("swzonex", 10); 1890 } else 1891 VM_WAIT; 1892 VM_OBJECT_WLOCK(object); 1893 goto retry; 1894 } 1895 1896 if (atomic_cmpset_int(&exhausted, 1, 0)) 1897 printf("swap zone ok\n"); 1898 1899 swap->swb_hnext = NULL; 1900 swap->swb_object = object; 1901 swap->swb_index = pindex & ~(vm_pindex_t)SWAP_META_MASK; 1902 swap->swb_count = 0; 1903 1904 ++object->un_pager.swp.swp_bcount; 1905 1906 for (i = 0; i < SWAP_META_PAGES; ++i) 1907 swap->swb_pages[i] = SWAPBLK_NONE; 1908 } 1909 1910 /* 1911 * Delete prior contents of metadata 1912 */ 1913 idx = pindex & SWAP_META_MASK; 1914 1915 if (swap->swb_pages[idx] != SWAPBLK_NONE) { 1916 swp_pager_freeswapspace(swap->swb_pages[idx], 1); 1917 --swap->swb_count; 1918 } 1919 1920 /* 1921 * Enter block into metadata 1922 */ 1923 swap->swb_pages[idx] = swapblk; 1924 if (swapblk != SWAPBLK_NONE) 1925 ++swap->swb_count; 1926 done: 1927 mtx_unlock(&swhash_mtx); 1928 } 1929 1930 /* 1931 * SWP_PAGER_META_FREE() - free a range of blocks in the object's swap metadata 1932 * 1933 * The requested range of blocks is freed, with any associated swap 1934 * returned to the swap bitmap. 1935 * 1936 * This routine will free swap metadata structures as they are cleaned 1937 * out. This routine does *NOT* operate on swap metadata associated 1938 * with resident pages. 1939 */ 1940 static void 1941 swp_pager_meta_free(vm_object_t object, vm_pindex_t index, daddr_t count) 1942 { 1943 1944 VM_OBJECT_ASSERT_LOCKED(object); 1945 if (object->type != OBJT_SWAP) 1946 return; 1947 1948 while (count > 0) { 1949 struct swblock **pswap; 1950 struct swblock *swap; 1951 1952 mtx_lock(&swhash_mtx); 1953 pswap = swp_pager_hash(object, index); 1954 1955 if ((swap = *pswap) != NULL) { 1956 daddr_t v = swap->swb_pages[index & SWAP_META_MASK]; 1957 1958 if (v != SWAPBLK_NONE) { 1959 swp_pager_freeswapspace(v, 1); 1960 swap->swb_pages[index & SWAP_META_MASK] = 1961 SWAPBLK_NONE; 1962 if (--swap->swb_count == 0) { 1963 *pswap = swap->swb_hnext; 1964 uma_zfree(swap_zone, swap); 1965 --object->un_pager.swp.swp_bcount; 1966 } 1967 } 1968 --count; 1969 ++index; 1970 } else { 1971 int n = SWAP_META_PAGES - (index & SWAP_META_MASK); 1972 count -= n; 1973 index += n; 1974 } 1975 mtx_unlock(&swhash_mtx); 1976 } 1977 } 1978 1979 /* 1980 * SWP_PAGER_META_FREE_ALL() - destroy all swap metadata associated with object 1981 * 1982 * This routine locates and destroys all swap metadata associated with 1983 * an object. 1984 */ 1985 static void 1986 swp_pager_meta_free_all(vm_object_t object) 1987 { 1988 daddr_t index = 0; 1989 1990 VM_OBJECT_ASSERT_WLOCKED(object); 1991 if (object->type != OBJT_SWAP) 1992 return; 1993 1994 while (object->un_pager.swp.swp_bcount) { 1995 struct swblock **pswap; 1996 struct swblock *swap; 1997 1998 mtx_lock(&swhash_mtx); 1999 pswap = swp_pager_hash(object, index); 2000 if ((swap = *pswap) != NULL) { 2001 int i; 2002 2003 for (i = 0; i < SWAP_META_PAGES; ++i) { 2004 daddr_t v = swap->swb_pages[i]; 2005 if (v != SWAPBLK_NONE) { 2006 --swap->swb_count; 2007 swp_pager_freeswapspace(v, 1); 2008 } 2009 } 2010 if (swap->swb_count != 0) 2011 panic("swap_pager_meta_free_all: swb_count != 0"); 2012 *pswap = swap->swb_hnext; 2013 uma_zfree(swap_zone, swap); 2014 --object->un_pager.swp.swp_bcount; 2015 } 2016 mtx_unlock(&swhash_mtx); 2017 index += SWAP_META_PAGES; 2018 } 2019 } 2020 2021 /* 2022 * SWP_PAGER_METACTL() - misc control of swap and vm_page_t meta data. 2023 * 2024 * This routine is capable of looking up, popping, or freeing 2025 * swapblk assignments in the swap meta data or in the vm_page_t. 2026 * The routine typically returns the swapblk being looked-up, or popped, 2027 * or SWAPBLK_NONE if the block was freed, or SWAPBLK_NONE if the block 2028 * was invalid. This routine will automatically free any invalid 2029 * meta-data swapblks. 2030 * 2031 * It is not possible to store invalid swapblks in the swap meta data 2032 * (other then a literal 'SWAPBLK_NONE'), so we don't bother checking. 2033 * 2034 * When acting on a busy resident page and paging is in progress, we 2035 * have to wait until paging is complete but otherwise can act on the 2036 * busy page. 2037 * 2038 * SWM_FREE remove and free swap block from metadata 2039 * SWM_POP remove from meta data but do not free.. pop it out 2040 */ 2041 static daddr_t 2042 swp_pager_meta_ctl(vm_object_t object, vm_pindex_t pindex, int flags) 2043 { 2044 struct swblock **pswap; 2045 struct swblock *swap; 2046 daddr_t r1; 2047 int idx; 2048 2049 VM_OBJECT_ASSERT_LOCKED(object); 2050 /* 2051 * The meta data only exists of the object is OBJT_SWAP 2052 * and even then might not be allocated yet. 2053 */ 2054 if (object->type != OBJT_SWAP) 2055 return (SWAPBLK_NONE); 2056 2057 r1 = SWAPBLK_NONE; 2058 mtx_lock(&swhash_mtx); 2059 pswap = swp_pager_hash(object, pindex); 2060 2061 if ((swap = *pswap) != NULL) { 2062 idx = pindex & SWAP_META_MASK; 2063 r1 = swap->swb_pages[idx]; 2064 2065 if (r1 != SWAPBLK_NONE) { 2066 if (flags & SWM_FREE) { 2067 swp_pager_freeswapspace(r1, 1); 2068 r1 = SWAPBLK_NONE; 2069 } 2070 if (flags & (SWM_FREE|SWM_POP)) { 2071 swap->swb_pages[idx] = SWAPBLK_NONE; 2072 if (--swap->swb_count == 0) { 2073 *pswap = swap->swb_hnext; 2074 uma_zfree(swap_zone, swap); 2075 --object->un_pager.swp.swp_bcount; 2076 } 2077 } 2078 } 2079 } 2080 mtx_unlock(&swhash_mtx); 2081 return (r1); 2082 } 2083 2084 /* 2085 * System call swapon(name) enables swapping on device name, 2086 * which must be in the swdevsw. Return EBUSY 2087 * if already swapping on this device. 2088 */ 2089 #ifndef _SYS_SYSPROTO_H_ 2090 struct swapon_args { 2091 char *name; 2092 }; 2093 #endif 2094 2095 /* 2096 * MPSAFE 2097 */ 2098 /* ARGSUSED */ 2099 int 2100 sys_swapon(struct thread *td, struct swapon_args *uap) 2101 { 2102 struct vattr attr; 2103 struct vnode *vp; 2104 struct nameidata nd; 2105 int error; 2106 2107 error = priv_check(td, PRIV_SWAPON); 2108 if (error) 2109 return (error); 2110 2111 mtx_lock(&Giant); 2112 while (swdev_syscall_active) 2113 tsleep(&swdev_syscall_active, PUSER - 1, "swpon", 0); 2114 swdev_syscall_active = 1; 2115 2116 /* 2117 * Swap metadata may not fit in the KVM if we have physical 2118 * memory of >1GB. 2119 */ 2120 if (swap_zone == NULL) { 2121 error = ENOMEM; 2122 goto done; 2123 } 2124 2125 NDINIT(&nd, LOOKUP, ISOPEN | FOLLOW | AUDITVNODE1, UIO_USERSPACE, 2126 uap->name, td); 2127 error = namei(&nd); 2128 if (error) 2129 goto done; 2130 2131 NDFREE(&nd, NDF_ONLY_PNBUF); 2132 vp = nd.ni_vp; 2133 2134 if (vn_isdisk(vp, &error)) { 2135 error = swapongeom(td, vp); 2136 } else if (vp->v_type == VREG && 2137 (vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 && 2138 (error = VOP_GETATTR(vp, &attr, td->td_ucred)) == 0) { 2139 /* 2140 * Allow direct swapping to NFS regular files in the same 2141 * way that nfs_mountroot() sets up diskless swapping. 2142 */ 2143 error = swaponvp(td, vp, attr.va_size / DEV_BSIZE); 2144 } 2145 2146 if (error) 2147 vrele(vp); 2148 done: 2149 swdev_syscall_active = 0; 2150 wakeup_one(&swdev_syscall_active); 2151 mtx_unlock(&Giant); 2152 return (error); 2153 } 2154 2155 /* 2156 * Check that the total amount of swap currently configured does not 2157 * exceed half the theoretical maximum. If it does, print a warning 2158 * message and return -1; otherwise, return 0. 2159 */ 2160 static int 2161 swapon_check_swzone(unsigned long npages) 2162 { 2163 unsigned long maxpages; 2164 2165 /* absolute maximum we can handle assuming 100% efficiency */ 2166 maxpages = uma_zone_get_max(swap_zone) * SWAP_META_PAGES; 2167 2168 /* recommend using no more than half that amount */ 2169 if (npages > maxpages / 2) { 2170 printf("warning: total configured swap (%lu pages) " 2171 "exceeds maximum recommended amount (%lu pages).\n", 2172 npages, maxpages / 2); 2173 printf("warning: increase kern.maxswzone " 2174 "or reduce amount of swap.\n"); 2175 return (-1); 2176 } 2177 return (0); 2178 } 2179 2180 static void 2181 swaponsomething(struct vnode *vp, void *id, u_long nblks, 2182 sw_strategy_t *strategy, sw_close_t *close, dev_t dev, int flags) 2183 { 2184 struct swdevt *sp, *tsp; 2185 swblk_t dvbase; 2186 u_long mblocks; 2187 2188 /* 2189 * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks. 2190 * First chop nblks off to page-align it, then convert. 2191 * 2192 * sw->sw_nblks is in page-sized chunks now too. 2193 */ 2194 nblks &= ~(ctodb(1) - 1); 2195 nblks = dbtoc(nblks); 2196 2197 /* 2198 * If we go beyond this, we get overflows in the radix 2199 * tree bitmap code. 2200 */ 2201 mblocks = 0x40000000 / BLIST_META_RADIX; 2202 if (nblks > mblocks) { 2203 printf( 2204 "WARNING: reducing swap size to maximum of %luMB per unit\n", 2205 mblocks / 1024 / 1024 * PAGE_SIZE); 2206 nblks = mblocks; 2207 } 2208 2209 sp = malloc(sizeof *sp, M_VMPGDATA, M_WAITOK | M_ZERO); 2210 sp->sw_vp = vp; 2211 sp->sw_id = id; 2212 sp->sw_dev = dev; 2213 sp->sw_flags = 0; 2214 sp->sw_nblks = nblks; 2215 sp->sw_used = 0; 2216 sp->sw_strategy = strategy; 2217 sp->sw_close = close; 2218 sp->sw_flags = flags; 2219 2220 sp->sw_blist = blist_create(nblks, M_WAITOK); 2221 /* 2222 * Do not free the first two block in order to avoid overwriting 2223 * any bsd label at the front of the partition 2224 */ 2225 blist_free(sp->sw_blist, 2, nblks - 2); 2226 2227 dvbase = 0; 2228 mtx_lock(&sw_dev_mtx); 2229 TAILQ_FOREACH(tsp, &swtailq, sw_list) { 2230 if (tsp->sw_end >= dvbase) { 2231 /* 2232 * We put one uncovered page between the devices 2233 * in order to definitively prevent any cross-device 2234 * I/O requests 2235 */ 2236 dvbase = tsp->sw_end + 1; 2237 } 2238 } 2239 sp->sw_first = dvbase; 2240 sp->sw_end = dvbase + nblks; 2241 TAILQ_INSERT_TAIL(&swtailq, sp, sw_list); 2242 nswapdev++; 2243 swap_pager_avail += nblks; 2244 swap_total += (vm_ooffset_t)nblks * PAGE_SIZE; 2245 swapon_check_swzone(swap_total / PAGE_SIZE); 2246 swp_sizecheck(); 2247 mtx_unlock(&sw_dev_mtx); 2248 } 2249 2250 /* 2251 * SYSCALL: swapoff(devname) 2252 * 2253 * Disable swapping on the given device. 2254 * 2255 * XXX: Badly designed system call: it should use a device index 2256 * rather than filename as specification. We keep sw_vp around 2257 * only to make this work. 2258 */ 2259 #ifndef _SYS_SYSPROTO_H_ 2260 struct swapoff_args { 2261 char *name; 2262 }; 2263 #endif 2264 2265 /* 2266 * MPSAFE 2267 */ 2268 /* ARGSUSED */ 2269 int 2270 sys_swapoff(struct thread *td, struct swapoff_args *uap) 2271 { 2272 struct vnode *vp; 2273 struct nameidata nd; 2274 struct swdevt *sp; 2275 int error; 2276 2277 error = priv_check(td, PRIV_SWAPOFF); 2278 if (error) 2279 return (error); 2280 2281 mtx_lock(&Giant); 2282 while (swdev_syscall_active) 2283 tsleep(&swdev_syscall_active, PUSER - 1, "swpoff", 0); 2284 swdev_syscall_active = 1; 2285 2286 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, UIO_USERSPACE, uap->name, 2287 td); 2288 error = namei(&nd); 2289 if (error) 2290 goto done; 2291 NDFREE(&nd, NDF_ONLY_PNBUF); 2292 vp = nd.ni_vp; 2293 2294 mtx_lock(&sw_dev_mtx); 2295 TAILQ_FOREACH(sp, &swtailq, sw_list) { 2296 if (sp->sw_vp == vp) 2297 break; 2298 } 2299 mtx_unlock(&sw_dev_mtx); 2300 if (sp == NULL) { 2301 error = EINVAL; 2302 goto done; 2303 } 2304 error = swapoff_one(sp, td->td_ucred); 2305 done: 2306 swdev_syscall_active = 0; 2307 wakeup_one(&swdev_syscall_active); 2308 mtx_unlock(&Giant); 2309 return (error); 2310 } 2311 2312 static int 2313 swapoff_one(struct swdevt *sp, struct ucred *cred) 2314 { 2315 u_long nblks, dvbase; 2316 #ifdef MAC 2317 int error; 2318 #endif 2319 2320 mtx_assert(&Giant, MA_OWNED); 2321 #ifdef MAC 2322 (void) vn_lock(sp->sw_vp, LK_EXCLUSIVE | LK_RETRY); 2323 error = mac_system_check_swapoff(cred, sp->sw_vp); 2324 (void) VOP_UNLOCK(sp->sw_vp, 0); 2325 if (error != 0) 2326 return (error); 2327 #endif 2328 nblks = sp->sw_nblks; 2329 2330 /* 2331 * We can turn off this swap device safely only if the 2332 * available virtual memory in the system will fit the amount 2333 * of data we will have to page back in, plus an epsilon so 2334 * the system doesn't become critically low on swap space. 2335 */ 2336 if (vm_cnt.v_free_count + vm_cnt.v_cache_count + swap_pager_avail < 2337 nblks + nswap_lowat) { 2338 return (ENOMEM); 2339 } 2340 2341 /* 2342 * Prevent further allocations on this device. 2343 */ 2344 mtx_lock(&sw_dev_mtx); 2345 sp->sw_flags |= SW_CLOSING; 2346 for (dvbase = 0; dvbase < sp->sw_end; dvbase += dmmax) { 2347 swap_pager_avail -= blist_fill(sp->sw_blist, 2348 dvbase, dmmax); 2349 } 2350 swap_total -= (vm_ooffset_t)nblks * PAGE_SIZE; 2351 mtx_unlock(&sw_dev_mtx); 2352 2353 /* 2354 * Page in the contents of the device and close it. 2355 */ 2356 swap_pager_swapoff(sp); 2357 2358 sp->sw_close(curthread, sp); 2359 sp->sw_id = NULL; 2360 mtx_lock(&sw_dev_mtx); 2361 TAILQ_REMOVE(&swtailq, sp, sw_list); 2362 nswapdev--; 2363 if (nswapdev == 0) { 2364 swap_pager_full = 2; 2365 swap_pager_almost_full = 1; 2366 } 2367 if (swdevhd == sp) 2368 swdevhd = NULL; 2369 mtx_unlock(&sw_dev_mtx); 2370 blist_destroy(sp->sw_blist); 2371 free(sp, M_VMPGDATA); 2372 return (0); 2373 } 2374 2375 void 2376 swapoff_all(void) 2377 { 2378 struct swdevt *sp, *spt; 2379 const char *devname; 2380 int error; 2381 2382 mtx_lock(&Giant); 2383 while (swdev_syscall_active) 2384 tsleep(&swdev_syscall_active, PUSER - 1, "swpoff", 0); 2385 swdev_syscall_active = 1; 2386 2387 mtx_lock(&sw_dev_mtx); 2388 TAILQ_FOREACH_SAFE(sp, &swtailq, sw_list, spt) { 2389 mtx_unlock(&sw_dev_mtx); 2390 if (vn_isdisk(sp->sw_vp, NULL)) 2391 devname = devtoname(sp->sw_vp->v_rdev); 2392 else 2393 devname = "[file]"; 2394 error = swapoff_one(sp, thread0.td_ucred); 2395 if (error != 0) { 2396 printf("Cannot remove swap device %s (error=%d), " 2397 "skipping.\n", devname, error); 2398 } else if (bootverbose) { 2399 printf("Swap device %s removed.\n", devname); 2400 } 2401 mtx_lock(&sw_dev_mtx); 2402 } 2403 mtx_unlock(&sw_dev_mtx); 2404 2405 swdev_syscall_active = 0; 2406 wakeup_one(&swdev_syscall_active); 2407 mtx_unlock(&Giant); 2408 } 2409 2410 void 2411 swap_pager_status(int *total, int *used) 2412 { 2413 struct swdevt *sp; 2414 2415 *total = 0; 2416 *used = 0; 2417 mtx_lock(&sw_dev_mtx); 2418 TAILQ_FOREACH(sp, &swtailq, sw_list) { 2419 *total += sp->sw_nblks; 2420 *used += sp->sw_used; 2421 } 2422 mtx_unlock(&sw_dev_mtx); 2423 } 2424 2425 int 2426 swap_dev_info(int name, struct xswdev *xs, char *devname, size_t len) 2427 { 2428 struct swdevt *sp; 2429 const char *tmp_devname; 2430 int error, n; 2431 2432 n = 0; 2433 error = ENOENT; 2434 mtx_lock(&sw_dev_mtx); 2435 TAILQ_FOREACH(sp, &swtailq, sw_list) { 2436 if (n != name) { 2437 n++; 2438 continue; 2439 } 2440 xs->xsw_version = XSWDEV_VERSION; 2441 xs->xsw_dev = sp->sw_dev; 2442 xs->xsw_flags = sp->sw_flags; 2443 xs->xsw_nblks = sp->sw_nblks; 2444 xs->xsw_used = sp->sw_used; 2445 if (devname != NULL) { 2446 if (vn_isdisk(sp->sw_vp, NULL)) 2447 tmp_devname = devtoname(sp->sw_vp->v_rdev); 2448 else 2449 tmp_devname = "[file]"; 2450 strncpy(devname, tmp_devname, len); 2451 } 2452 error = 0; 2453 break; 2454 } 2455 mtx_unlock(&sw_dev_mtx); 2456 return (error); 2457 } 2458 2459 static int 2460 sysctl_vm_swap_info(SYSCTL_HANDLER_ARGS) 2461 { 2462 struct xswdev xs; 2463 int error; 2464 2465 if (arg2 != 1) /* name length */ 2466 return (EINVAL); 2467 error = swap_dev_info(*(int *)arg1, &xs, NULL, 0); 2468 if (error != 0) 2469 return (error); 2470 error = SYSCTL_OUT(req, &xs, sizeof(xs)); 2471 return (error); 2472 } 2473 2474 SYSCTL_INT(_vm, OID_AUTO, nswapdev, CTLFLAG_RD, &nswapdev, 0, 2475 "Number of swap devices"); 2476 SYSCTL_NODE(_vm, OID_AUTO, swap_info, CTLFLAG_RD, sysctl_vm_swap_info, 2477 "Swap statistics by device"); 2478 2479 /* 2480 * vmspace_swap_count() - count the approximate swap usage in pages for a 2481 * vmspace. 2482 * 2483 * The map must be locked. 2484 * 2485 * Swap usage is determined by taking the proportional swap used by 2486 * VM objects backing the VM map. To make up for fractional losses, 2487 * if the VM object has any swap use at all the associated map entries 2488 * count for at least 1 swap page. 2489 */ 2490 long 2491 vmspace_swap_count(struct vmspace *vmspace) 2492 { 2493 vm_map_t map; 2494 vm_map_entry_t cur; 2495 vm_object_t object; 2496 long count, n; 2497 2498 map = &vmspace->vm_map; 2499 count = 0; 2500 2501 for (cur = map->header.next; cur != &map->header; cur = cur->next) { 2502 if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 && 2503 (object = cur->object.vm_object) != NULL) { 2504 VM_OBJECT_WLOCK(object); 2505 if (object->type == OBJT_SWAP && 2506 object->un_pager.swp.swp_bcount != 0) { 2507 n = (cur->end - cur->start) / PAGE_SIZE; 2508 count += object->un_pager.swp.swp_bcount * 2509 SWAP_META_PAGES * n / object->size + 1; 2510 } 2511 VM_OBJECT_WUNLOCK(object); 2512 } 2513 } 2514 return (count); 2515 } 2516 2517 /* 2518 * GEOM backend 2519 * 2520 * Swapping onto disk devices. 2521 * 2522 */ 2523 2524 static g_orphan_t swapgeom_orphan; 2525 2526 static struct g_class g_swap_class = { 2527 .name = "SWAP", 2528 .version = G_VERSION, 2529 .orphan = swapgeom_orphan, 2530 }; 2531 2532 DECLARE_GEOM_CLASS(g_swap_class, g_class); 2533 2534 2535 static void 2536 swapgeom_close_ev(void *arg, int flags) 2537 { 2538 struct g_consumer *cp; 2539 2540 cp = arg; 2541 g_access(cp, -1, -1, 0); 2542 g_detach(cp); 2543 g_destroy_consumer(cp); 2544 } 2545 2546 static void 2547 swapgeom_done(struct bio *bp2) 2548 { 2549 struct swdevt *sp; 2550 struct buf *bp; 2551 struct g_consumer *cp; 2552 2553 bp = bp2->bio_caller2; 2554 cp = bp2->bio_from; 2555 bp->b_ioflags = bp2->bio_flags; 2556 if (bp2->bio_error) 2557 bp->b_ioflags |= BIO_ERROR; 2558 bp->b_resid = bp->b_bcount - bp2->bio_completed; 2559 bp->b_error = bp2->bio_error; 2560 bufdone(bp); 2561 mtx_lock(&sw_dev_mtx); 2562 if ((--cp->index) == 0 && cp->private) { 2563 if (g_post_event(swapgeom_close_ev, cp, M_NOWAIT, NULL) == 0) { 2564 sp = bp2->bio_caller1; 2565 sp->sw_id = NULL; 2566 } 2567 } 2568 mtx_unlock(&sw_dev_mtx); 2569 g_destroy_bio(bp2); 2570 } 2571 2572 static void 2573 swapgeom_strategy(struct buf *bp, struct swdevt *sp) 2574 { 2575 struct bio *bio; 2576 struct g_consumer *cp; 2577 2578 mtx_lock(&sw_dev_mtx); 2579 cp = sp->sw_id; 2580 if (cp == NULL) { 2581 mtx_unlock(&sw_dev_mtx); 2582 bp->b_error = ENXIO; 2583 bp->b_ioflags |= BIO_ERROR; 2584 bufdone(bp); 2585 return; 2586 } 2587 cp->index++; 2588 mtx_unlock(&sw_dev_mtx); 2589 if (bp->b_iocmd == BIO_WRITE) 2590 bio = g_new_bio(); 2591 else 2592 bio = g_alloc_bio(); 2593 if (bio == NULL) { 2594 bp->b_error = ENOMEM; 2595 bp->b_ioflags |= BIO_ERROR; 2596 bufdone(bp); 2597 return; 2598 } 2599 2600 bio->bio_caller1 = sp; 2601 bio->bio_caller2 = bp; 2602 bio->bio_cmd = bp->b_iocmd; 2603 bio->bio_offset = (bp->b_blkno - sp->sw_first) * PAGE_SIZE; 2604 bio->bio_length = bp->b_bcount; 2605 bio->bio_done = swapgeom_done; 2606 if ((bp->b_flags & B_UNMAPPED) != 0) { 2607 bio->bio_ma = bp->b_pages; 2608 bio->bio_data = unmapped_buf; 2609 bio->bio_ma_offset = (vm_offset_t)bp->b_offset & PAGE_MASK; 2610 bio->bio_ma_n = bp->b_npages; 2611 bio->bio_flags |= BIO_UNMAPPED; 2612 } else { 2613 bio->bio_data = bp->b_data; 2614 bio->bio_ma = NULL; 2615 } 2616 g_io_request(bio, cp); 2617 return; 2618 } 2619 2620 static void 2621 swapgeom_orphan(struct g_consumer *cp) 2622 { 2623 struct swdevt *sp; 2624 int destroy; 2625 2626 mtx_lock(&sw_dev_mtx); 2627 TAILQ_FOREACH(sp, &swtailq, sw_list) { 2628 if (sp->sw_id == cp) { 2629 sp->sw_flags |= SW_CLOSING; 2630 break; 2631 } 2632 } 2633 cp->private = (void *)(uintptr_t)1; 2634 destroy = ((sp != NULL) && (cp->index == 0)); 2635 if (destroy) 2636 sp->sw_id = NULL; 2637 mtx_unlock(&sw_dev_mtx); 2638 if (destroy) 2639 swapgeom_close_ev(cp, 0); 2640 } 2641 2642 static void 2643 swapgeom_close(struct thread *td, struct swdevt *sw) 2644 { 2645 struct g_consumer *cp; 2646 2647 mtx_lock(&sw_dev_mtx); 2648 cp = sw->sw_id; 2649 sw->sw_id = NULL; 2650 mtx_unlock(&sw_dev_mtx); 2651 /* XXX: direct call when Giant untangled */ 2652 if (cp != NULL) 2653 g_waitfor_event(swapgeom_close_ev, cp, M_WAITOK, NULL); 2654 } 2655 2656 2657 struct swh0h0 { 2658 struct cdev *dev; 2659 struct vnode *vp; 2660 int error; 2661 }; 2662 2663 static void 2664 swapongeom_ev(void *arg, int flags) 2665 { 2666 struct swh0h0 *swh; 2667 struct g_provider *pp; 2668 struct g_consumer *cp; 2669 static struct g_geom *gp; 2670 struct swdevt *sp; 2671 u_long nblks; 2672 int error; 2673 2674 swh = arg; 2675 swh->error = 0; 2676 pp = g_dev_getprovider(swh->dev); 2677 if (pp == NULL) { 2678 swh->error = ENODEV; 2679 return; 2680 } 2681 mtx_lock(&sw_dev_mtx); 2682 TAILQ_FOREACH(sp, &swtailq, sw_list) { 2683 cp = sp->sw_id; 2684 if (cp != NULL && cp->provider == pp) { 2685 mtx_unlock(&sw_dev_mtx); 2686 swh->error = EBUSY; 2687 return; 2688 } 2689 } 2690 mtx_unlock(&sw_dev_mtx); 2691 if (gp == NULL) 2692 gp = g_new_geomf(&g_swap_class, "swap"); 2693 cp = g_new_consumer(gp); 2694 cp->index = 0; /* Number of active I/Os. */ 2695 cp->private = NULL; /* Orphanization flag */ 2696 g_attach(cp, pp); 2697 /* 2698 * XXX: Everytime you think you can improve the margin for 2699 * footshooting, somebody depends on the ability to do so: 2700 * savecore(8) wants to write to our swapdev so we cannot 2701 * set an exclusive count :-( 2702 */ 2703 error = g_access(cp, 1, 1, 0); 2704 if (error) { 2705 g_detach(cp); 2706 g_destroy_consumer(cp); 2707 swh->error = error; 2708 return; 2709 } 2710 nblks = pp->mediasize / DEV_BSIZE; 2711 swaponsomething(swh->vp, cp, nblks, swapgeom_strategy, 2712 swapgeom_close, dev2udev(swh->dev), 2713 (pp->flags & G_PF_ACCEPT_UNMAPPED) != 0 ? SW_UNMAPPED : 0); 2714 swh->error = 0; 2715 } 2716 2717 static int 2718 swapongeom(struct thread *td, struct vnode *vp) 2719 { 2720 int error; 2721 struct swh0h0 swh; 2722 2723 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2724 2725 swh.dev = vp->v_rdev; 2726 swh.vp = vp; 2727 swh.error = 0; 2728 /* XXX: direct call when Giant untangled */ 2729 error = g_waitfor_event(swapongeom_ev, &swh, M_WAITOK, NULL); 2730 if (!error) 2731 error = swh.error; 2732 VOP_UNLOCK(vp, 0); 2733 return (error); 2734 } 2735 2736 /* 2737 * VNODE backend 2738 * 2739 * This is used mainly for network filesystem (read: probably only tested 2740 * with NFS) swapfiles. 2741 * 2742 */ 2743 2744 static void 2745 swapdev_strategy(struct buf *bp, struct swdevt *sp) 2746 { 2747 struct vnode *vp2; 2748 2749 bp->b_blkno = ctodb(bp->b_blkno - sp->sw_first); 2750 2751 vp2 = sp->sw_id; 2752 vhold(vp2); 2753 if (bp->b_iocmd == BIO_WRITE) { 2754 if (bp->b_bufobj) 2755 bufobj_wdrop(bp->b_bufobj); 2756 bufobj_wref(&vp2->v_bufobj); 2757 } 2758 if (bp->b_bufobj != &vp2->v_bufobj) 2759 bp->b_bufobj = &vp2->v_bufobj; 2760 bp->b_vp = vp2; 2761 bp->b_iooffset = dbtob(bp->b_blkno); 2762 bstrategy(bp); 2763 return; 2764 } 2765 2766 static void 2767 swapdev_close(struct thread *td, struct swdevt *sp) 2768 { 2769 2770 VOP_CLOSE(sp->sw_vp, FREAD | FWRITE, td->td_ucred, td); 2771 vrele(sp->sw_vp); 2772 } 2773 2774 2775 static int 2776 swaponvp(struct thread *td, struct vnode *vp, u_long nblks) 2777 { 2778 struct swdevt *sp; 2779 int error; 2780 2781 if (nblks == 0) 2782 return (ENXIO); 2783 mtx_lock(&sw_dev_mtx); 2784 TAILQ_FOREACH(sp, &swtailq, sw_list) { 2785 if (sp->sw_id == vp) { 2786 mtx_unlock(&sw_dev_mtx); 2787 return (EBUSY); 2788 } 2789 } 2790 mtx_unlock(&sw_dev_mtx); 2791 2792 (void) vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2793 #ifdef MAC 2794 error = mac_system_check_swapon(td->td_ucred, vp); 2795 if (error == 0) 2796 #endif 2797 error = VOP_OPEN(vp, FREAD | FWRITE, td->td_ucred, td, NULL); 2798 (void) VOP_UNLOCK(vp, 0); 2799 if (error) 2800 return (error); 2801 2802 swaponsomething(vp, vp, nblks, swapdev_strategy, swapdev_close, 2803 NODEV, 0); 2804 return (0); 2805 } 2806 2807 static int 2808 sysctl_swap_async_max(SYSCTL_HANDLER_ARGS) 2809 { 2810 int error, new, n; 2811 2812 new = nsw_wcount_async_max; 2813 error = sysctl_handle_int(oidp, &new, 0, req); 2814 if (error != 0 || req->newptr == NULL) 2815 return (error); 2816 2817 if (new > nswbuf / 2 || new < 1) 2818 return (EINVAL); 2819 2820 mtx_lock(&pbuf_mtx); 2821 while (nsw_wcount_async_max != new) { 2822 /* 2823 * Adjust difference. If the current async count is too low, 2824 * we will need to sqeeze our update slowly in. Sleep with a 2825 * higher priority than getpbuf() to finish faster. 2826 */ 2827 n = new - nsw_wcount_async_max; 2828 if (nsw_wcount_async + n >= 0) { 2829 nsw_wcount_async += n; 2830 nsw_wcount_async_max += n; 2831 wakeup(&nsw_wcount_async); 2832 } else { 2833 nsw_wcount_async_max -= nsw_wcount_async; 2834 nsw_wcount_async = 0; 2835 msleep(&nsw_wcount_async, &pbuf_mtx, PSWP, 2836 "swpsysctl", 0); 2837 } 2838 } 2839 mtx_unlock(&pbuf_mtx); 2840 2841 return (0); 2842 } 2843