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