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