1 /* 2 * Copyright (c) 1998 Matthew Dillon, 3 * Copyright (c) 1994 John S. Dyson 4 * Copyright (c) 1990 University of Utah. 5 * Copyright (c) 1991, 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 * 67 * $FreeBSD$ 68 */ 69 70 #include <sys/param.h> 71 #include <sys/systm.h> 72 #include <sys/conf.h> 73 #include <sys/kernel.h> 74 #include <sys/proc.h> 75 #include <sys/bio.h> 76 #include <sys/buf.h> 77 #include <sys/vnode.h> 78 #include <sys/malloc.h> 79 #include <sys/vmmeter.h> 80 #include <sys/sysctl.h> 81 #include <sys/blist.h> 82 #include <sys/lock.h> 83 #include <sys/sx.h> 84 #include <sys/vmmeter.h> 85 86 #ifndef MAX_PAGEOUT_CLUSTER 87 #define MAX_PAGEOUT_CLUSTER 16 88 #endif 89 90 #define SWB_NPAGES MAX_PAGEOUT_CLUSTER 91 92 #include "opt_swap.h" 93 #include <vm/vm.h> 94 #include <vm/pmap.h> 95 #include <vm/vm_map.h> 96 #include <vm/vm_kern.h> 97 #include <vm/vm_object.h> 98 #include <vm/vm_page.h> 99 #include <vm/vm_pager.h> 100 #include <vm/vm_pageout.h> 101 #include <vm/vm_zone.h> 102 #include <vm/swap_pager.h> 103 #include <vm/vm_extern.h> 104 105 #define SWM_FREE 0x02 /* free, period */ 106 #define SWM_POP 0x04 /* pop out */ 107 108 /* 109 * vm_swap_size is in page-sized chunks now. It was DEV_BSIZE'd chunks 110 * in the old system. 111 */ 112 113 extern int vm_swap_size; /* number of free swap blocks, in pages */ 114 115 int swap_pager_full; /* swap space exhaustion (task killing) */ 116 static int swap_pager_almost_full; /* swap space exhaustion (w/ hysteresis)*/ 117 static int nsw_rcount; /* free read buffers */ 118 static int nsw_wcount_sync; /* limit write buffers / synchronous */ 119 static int nsw_wcount_async; /* limit write buffers / asynchronous */ 120 static int nsw_wcount_async_max;/* assigned maximum */ 121 static int nsw_cluster_max; /* maximum VOP I/O allowed */ 122 123 struct blist *swapblist; 124 static struct swblock **swhash; 125 static int swhash_mask; 126 static int swap_async_max = 4; /* maximum in-progress async I/O's */ 127 128 /* from vm_swap.c */ 129 extern struct vnode *swapdev_vp; 130 extern struct swdevt *swdevt; 131 extern int nswdev; 132 133 SYSCTL_INT(_vm, OID_AUTO, swap_async_max, 134 CTLFLAG_RW, &swap_async_max, 0, "Maximum running async swap ops"); 135 136 #define BLK2DEVIDX(blk) (nswdev > 1 ? blk / dmmax % nswdev : 0) 137 138 /* 139 * "named" and "unnamed" anon region objects. Try to reduce the overhead 140 * of searching a named list by hashing it just a little. 141 */ 142 143 #define NOBJLISTS 8 144 145 #define NOBJLIST(handle) \ 146 (&swap_pager_object_list[((int)(intptr_t)handle >> 4) & (NOBJLISTS-1)]) 147 148 static struct sx sw_alloc_sx; /* prevent concurrant creation */ 149 static struct mtx sw_alloc_mtx; /* protect list manipulation */ 150 static struct pagerlst swap_pager_object_list[NOBJLISTS]; 151 struct pagerlst swap_pager_un_object_list; 152 vm_zone_t swap_zone; 153 154 /* 155 * pagerops for OBJT_SWAP - "swap pager". Some ops are also global procedure 156 * calls hooked from other parts of the VM system and do not appear here. 157 * (see vm/swap_pager.h). 158 */ 159 160 static vm_object_t 161 swap_pager_alloc __P((void *handle, vm_ooffset_t size, 162 vm_prot_t prot, vm_ooffset_t offset)); 163 static void swap_pager_dealloc __P((vm_object_t object)); 164 static int swap_pager_getpages __P((vm_object_t, vm_page_t *, int, int)); 165 static void swap_pager_init __P((void)); 166 static void swap_pager_unswapped __P((vm_page_t)); 167 static void swap_pager_strategy __P((vm_object_t, struct bio *)); 168 169 struct pagerops swappagerops = { 170 swap_pager_init, /* early system initialization of pager */ 171 swap_pager_alloc, /* allocate an OBJT_SWAP object */ 172 swap_pager_dealloc, /* deallocate an OBJT_SWAP object */ 173 swap_pager_getpages, /* pagein */ 174 swap_pager_putpages, /* pageout */ 175 swap_pager_haspage, /* get backing store status for page */ 176 swap_pager_unswapped, /* remove swap related to page */ 177 swap_pager_strategy /* pager strategy call */ 178 }; 179 180 static struct buf *getchainbuf(struct bio *bp, struct vnode *vp, int flags); 181 static void flushchainbuf(struct buf *nbp); 182 static void waitchainbuf(struct bio *bp, int count, int done); 183 184 /* 185 * dmmax is in page-sized chunks with the new swap system. It was 186 * dev-bsized chunks in the old. dmmax is always a power of 2. 187 * 188 * swap_*() routines are externally accessible. swp_*() routines are 189 * internal. 190 */ 191 192 int dmmax; 193 static int dmmax_mask; 194 int nswap_lowat = 128; /* in pages, swap_pager_almost_full warn */ 195 int nswap_hiwat = 512; /* in pages, swap_pager_almost_full warn */ 196 197 SYSCTL_INT(_vm, OID_AUTO, dmmax, 198 CTLFLAG_RD, &dmmax, 0, "Maximum size of a swap block"); 199 200 static __inline void swp_sizecheck __P((void)); 201 static void swp_pager_sync_iodone __P((struct buf *bp)); 202 static void swp_pager_async_iodone __P((struct buf *bp)); 203 204 /* 205 * Swap bitmap functions 206 */ 207 208 static __inline void swp_pager_freeswapspace __P((daddr_t blk, int npages)); 209 static __inline daddr_t swp_pager_getswapspace __P((int npages)); 210 211 /* 212 * Metadata functions 213 */ 214 215 static void swp_pager_meta_build __P((vm_object_t, vm_pindex_t, daddr_t)); 216 static void swp_pager_meta_free __P((vm_object_t, vm_pindex_t, daddr_t)); 217 static void swp_pager_meta_free_all __P((vm_object_t)); 218 static daddr_t swp_pager_meta_ctl __P((vm_object_t, vm_pindex_t, int)); 219 220 /* 221 * SWP_SIZECHECK() - update swap_pager_full indication 222 * 223 * update the swap_pager_almost_full indication and warn when we are 224 * about to run out of swap space, using lowat/hiwat hysteresis. 225 * 226 * Clear swap_pager_full ( task killing ) indication when lowat is met. 227 * 228 * No restrictions on call 229 * This routine may not block. 230 * This routine must be called at splvm() 231 */ 232 233 static __inline void 234 swp_sizecheck() 235 { 236 if (vm_swap_size < nswap_lowat) { 237 if (swap_pager_almost_full == 0) { 238 printf("swap_pager: out of swap space\n"); 239 swap_pager_almost_full = 1; 240 } 241 } else { 242 swap_pager_full = 0; 243 if (vm_swap_size > nswap_hiwat) 244 swap_pager_almost_full = 0; 245 } 246 } 247 248 /* 249 * SWAP_PAGER_INIT() - initialize the swap pager! 250 * 251 * Expected to be started from system init. NOTE: This code is run 252 * before much else so be careful what you depend on. Most of the VM 253 * system has yet to be initialized at this point. 254 */ 255 256 static void 257 swap_pager_init() 258 { 259 /* 260 * Initialize object lists 261 */ 262 int i; 263 264 for (i = 0; i < NOBJLISTS; ++i) 265 TAILQ_INIT(&swap_pager_object_list[i]); 266 TAILQ_INIT(&swap_pager_un_object_list); 267 sx_init(&sw_alloc_sx, "swap_pager create"); 268 mtx_init(&sw_alloc_mtx, "swap_pager list", MTX_DEF); 269 270 /* 271 * Device Stripe, in PAGE_SIZE'd blocks 272 */ 273 274 dmmax = SWB_NPAGES * 2; 275 dmmax_mask = ~(dmmax - 1); 276 } 277 278 /* 279 * SWAP_PAGER_SWAP_INIT() - swap pager initialization from pageout process 280 * 281 * Expected to be started from pageout process once, prior to entering 282 * its main loop. 283 */ 284 285 void 286 swap_pager_swap_init() 287 { 288 int n, n2; 289 290 /* 291 * Number of in-transit swap bp operations. Don't 292 * exhaust the pbufs completely. Make sure we 293 * initialize workable values (0 will work for hysteresis 294 * but it isn't very efficient). 295 * 296 * The nsw_cluster_max is constrained by the bp->b_pages[] 297 * array (MAXPHYS/PAGE_SIZE) and our locally defined 298 * MAX_PAGEOUT_CLUSTER. Also be aware that swap ops are 299 * constrained by the swap device interleave stripe size. 300 * 301 * Currently we hardwire nsw_wcount_async to 4. This limit is 302 * designed to prevent other I/O from having high latencies due to 303 * our pageout I/O. The value 4 works well for one or two active swap 304 * devices but is probably a little low if you have more. Even so, 305 * a higher value would probably generate only a limited improvement 306 * with three or four active swap devices since the system does not 307 * typically have to pageout at extreme bandwidths. We will want 308 * at least 2 per swap devices, and 4 is a pretty good value if you 309 * have one NFS swap device due to the command/ack latency over NFS. 310 * So it all works out pretty well. 311 */ 312 313 nsw_cluster_max = min((MAXPHYS/PAGE_SIZE), MAX_PAGEOUT_CLUSTER); 314 315 nsw_rcount = (nswbuf + 1) / 2; 316 nsw_wcount_sync = (nswbuf + 3) / 4; 317 nsw_wcount_async = 4; 318 nsw_wcount_async_max = nsw_wcount_async; 319 320 /* 321 * Initialize our zone. Right now I'm just guessing on the number 322 * we need based on the number of pages in the system. Each swblock 323 * can hold 16 pages, so this is probably overkill. 324 */ 325 326 n = min(cnt.v_page_count, (kernel_map->max_offset - kernel_map->min_offset) / PAGE_SIZE) * 2; 327 n2 = n; 328 329 while (n > 0 330 && (swap_zone = zinit( 331 "SWAPMETA", 332 sizeof(struct swblock), 333 n, 334 ZONE_INTERRUPT, 335 1 336 )) == NULL) 337 n >>= 1; 338 if (swap_zone == NULL) 339 printf("WARNING: failed to init swap_zone!\n"); 340 if (n2 != n) 341 printf("Swap zone entries reduced to %d.\n", n); 342 n2 = n; 343 344 /* 345 * Initialize our meta-data hash table. The swapper does not need to 346 * be quite as efficient as the VM system, so we do not use an 347 * oversized hash table. 348 * 349 * n: size of hash table, must be power of 2 350 * swhash_mask: hash table index mask 351 */ 352 353 for (n = 1; n < n2 ; n <<= 1) 354 ; 355 356 swhash = malloc(sizeof(struct swblock *) * n, M_VMPGDATA, M_WAITOK | M_ZERO); 357 358 swhash_mask = n - 1; 359 } 360 361 /* 362 * SWAP_PAGER_ALLOC() - allocate a new OBJT_SWAP VM object and instantiate 363 * its metadata structures. 364 * 365 * This routine is called from the mmap and fork code to create a new 366 * OBJT_SWAP object. We do this by creating an OBJT_DEFAULT object 367 * and then converting it with swp_pager_meta_build(). 368 * 369 * This routine may block in vm_object_allocate() and create a named 370 * object lookup race, so we must interlock. We must also run at 371 * splvm() for the object lookup to handle races with interrupts, but 372 * we do not have to maintain splvm() in between the lookup and the 373 * add because (I believe) it is not possible to attempt to create 374 * a new swap object w/handle when a default object with that handle 375 * already exists. 376 */ 377 378 static vm_object_t 379 swap_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 380 vm_ooffset_t offset) 381 { 382 vm_object_t object; 383 384 if (handle) { 385 /* 386 * Reference existing named region or allocate new one. There 387 * should not be a race here against swp_pager_meta_build() 388 * as called from vm_page_remove() in regards to the lookup 389 * of the handle. 390 */ 391 392 sx_xlock(&sw_alloc_sx); 393 394 object = vm_pager_object_lookup(NOBJLIST(handle), handle); 395 396 if (object != NULL) { 397 vm_object_reference(object); 398 } else { 399 object = vm_object_allocate(OBJT_DEFAULT, 400 OFF_TO_IDX(offset + PAGE_MASK + size)); 401 object->handle = handle; 402 403 swp_pager_meta_build(object, 0, SWAPBLK_NONE); 404 } 405 406 sx_xunlock(&sw_alloc_sx); 407 } else { 408 object = vm_object_allocate(OBJT_DEFAULT, 409 OFF_TO_IDX(offset + PAGE_MASK + size)); 410 411 swp_pager_meta_build(object, 0, SWAPBLK_NONE); 412 } 413 414 return (object); 415 } 416 417 /* 418 * SWAP_PAGER_DEALLOC() - remove swap metadata from object 419 * 420 * The swap backing for the object is destroyed. The code is 421 * designed such that we can reinstantiate it later, but this 422 * routine is typically called only when the entire object is 423 * about to be destroyed. 424 * 425 * This routine may block, but no longer does. 426 * 427 * The object must be locked or unreferenceable. 428 */ 429 430 static void 431 swap_pager_dealloc(object) 432 vm_object_t object; 433 { 434 int s; 435 436 /* 437 * Remove from list right away so lookups will fail if we block for 438 * pageout completion. 439 */ 440 441 mtx_lock(&sw_alloc_mtx); 442 if (object->handle == NULL) { 443 TAILQ_REMOVE(&swap_pager_un_object_list, object, pager_object_list); 444 } else { 445 TAILQ_REMOVE(NOBJLIST(object->handle), object, pager_object_list); 446 } 447 mtx_unlock(&sw_alloc_mtx); 448 449 vm_object_pip_wait(object, "swpdea"); 450 451 /* 452 * Free all remaining metadata. We only bother to free it from 453 * the swap meta data. We do not attempt to free swapblk's still 454 * associated with vm_page_t's for this object. We do not care 455 * if paging is still in progress on some objects. 456 */ 457 s = splvm(); 458 swp_pager_meta_free_all(object); 459 splx(s); 460 } 461 462 /************************************************************************ 463 * SWAP PAGER BITMAP ROUTINES * 464 ************************************************************************/ 465 466 /* 467 * SWP_PAGER_GETSWAPSPACE() - allocate raw swap space 468 * 469 * Allocate swap for the requested number of pages. The starting 470 * swap block number (a page index) is returned or SWAPBLK_NONE 471 * if the allocation failed. 472 * 473 * Also has the side effect of advising that somebody made a mistake 474 * when they configured swap and didn't configure enough. 475 * 476 * Must be called at splvm() to avoid races with bitmap frees from 477 * vm_page_remove() aka swap_pager_page_removed(). 478 * 479 * This routine may not block 480 * This routine must be called at splvm(). 481 */ 482 483 static __inline daddr_t 484 swp_pager_getswapspace(npages) 485 int npages; 486 { 487 daddr_t blk; 488 489 if ((blk = blist_alloc(swapblist, npages)) == SWAPBLK_NONE) { 490 if (swap_pager_full != 2) { 491 printf("swap_pager_getswapspace: failed\n"); 492 swap_pager_full = 2; 493 swap_pager_almost_full = 1; 494 } 495 } else { 496 vm_swap_size -= npages; 497 /* per-swap area stats */ 498 swdevt[BLK2DEVIDX(blk)].sw_used += npages; 499 swp_sizecheck(); 500 } 501 return(blk); 502 } 503 504 /* 505 * SWP_PAGER_FREESWAPSPACE() - free raw swap space 506 * 507 * This routine returns the specified swap blocks back to the bitmap. 508 * 509 * Note: This routine may not block (it could in the old swap code), 510 * and through the use of the new blist routines it does not block. 511 * 512 * We must be called at splvm() to avoid races with bitmap frees from 513 * vm_page_remove() aka swap_pager_page_removed(). 514 * 515 * This routine may not block 516 * This routine must be called at splvm(). 517 */ 518 519 static __inline void 520 swp_pager_freeswapspace(blk, npages) 521 daddr_t blk; 522 int npages; 523 { 524 blist_free(swapblist, blk, npages); 525 vm_swap_size += npages; 526 /* per-swap area stats */ 527 swdevt[BLK2DEVIDX(blk)].sw_used -= npages; 528 swp_sizecheck(); 529 } 530 531 /* 532 * SWAP_PAGER_FREESPACE() - frees swap blocks associated with a page 533 * range within an object. 534 * 535 * This is a globally accessible routine. 536 * 537 * This routine removes swapblk assignments from swap metadata. 538 * 539 * The external callers of this routine typically have already destroyed 540 * or renamed vm_page_t's associated with this range in the object so 541 * we should be ok. 542 * 543 * This routine may be called at any spl. We up our spl to splvm temporarily 544 * in order to perform the metadata removal. 545 */ 546 547 void 548 swap_pager_freespace(object, start, size) 549 vm_object_t object; 550 vm_pindex_t start; 551 vm_size_t size; 552 { 553 int s = splvm(); 554 swp_pager_meta_free(object, start, size); 555 splx(s); 556 } 557 558 /* 559 * SWAP_PAGER_RESERVE() - reserve swap blocks in object 560 * 561 * Assigns swap blocks to the specified range within the object. The 562 * swap blocks are not zerod. Any previous swap assignment is destroyed. 563 * 564 * Returns 0 on success, -1 on failure. 565 */ 566 567 int 568 swap_pager_reserve(vm_object_t object, vm_pindex_t start, vm_size_t size) 569 { 570 int s; 571 int n = 0; 572 daddr_t blk = SWAPBLK_NONE; 573 vm_pindex_t beg = start; /* save start index */ 574 575 s = splvm(); 576 while (size) { 577 if (n == 0) { 578 n = BLIST_MAX_ALLOC; 579 while ((blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE) { 580 n >>= 1; 581 if (n == 0) { 582 swp_pager_meta_free(object, beg, start - beg); 583 splx(s); 584 return(-1); 585 } 586 } 587 } 588 swp_pager_meta_build(object, start, blk); 589 --size; 590 ++start; 591 ++blk; 592 --n; 593 } 594 swp_pager_meta_free(object, start, n); 595 splx(s); 596 return(0); 597 } 598 599 /* 600 * SWAP_PAGER_COPY() - copy blocks from source pager to destination pager 601 * and destroy the source. 602 * 603 * Copy any valid swapblks from the source to the destination. In 604 * cases where both the source and destination have a valid swapblk, 605 * we keep the destination's. 606 * 607 * This routine is allowed to block. It may block allocating metadata 608 * indirectly through swp_pager_meta_build() or if paging is still in 609 * progress on the source. 610 * 611 * This routine can be called at any spl 612 * 613 * XXX vm_page_collapse() kinda expects us not to block because we 614 * supposedly do not need to allocate memory, but for the moment we 615 * *may* have to get a little memory from the zone allocator, but 616 * it is taken from the interrupt memory. We should be ok. 617 * 618 * The source object contains no vm_page_t's (which is just as well) 619 * 620 * The source object is of type OBJT_SWAP. 621 * 622 * The source and destination objects must be locked or 623 * inaccessible (XXX are they ?) 624 */ 625 626 void 627 swap_pager_copy(srcobject, dstobject, offset, destroysource) 628 vm_object_t srcobject; 629 vm_object_t dstobject; 630 vm_pindex_t offset; 631 int destroysource; 632 { 633 vm_pindex_t i; 634 int s; 635 636 s = splvm(); 637 638 /* 639 * If destroysource is set, we remove the source object from the 640 * swap_pager internal queue now. 641 */ 642 643 if (destroysource) { 644 mtx_lock(&sw_alloc_mtx); 645 if (srcobject->handle == NULL) { 646 TAILQ_REMOVE( 647 &swap_pager_un_object_list, 648 srcobject, 649 pager_object_list 650 ); 651 } else { 652 TAILQ_REMOVE( 653 NOBJLIST(srcobject->handle), 654 srcobject, 655 pager_object_list 656 ); 657 } 658 mtx_unlock(&sw_alloc_mtx); 659 } 660 661 /* 662 * transfer source to destination. 663 */ 664 665 for (i = 0; i < dstobject->size; ++i) { 666 daddr_t dstaddr; 667 668 /* 669 * Locate (without changing) the swapblk on the destination, 670 * unless it is invalid in which case free it silently, or 671 * if the destination is a resident page, in which case the 672 * source is thrown away. 673 */ 674 675 dstaddr = swp_pager_meta_ctl(dstobject, i, 0); 676 677 if (dstaddr == SWAPBLK_NONE) { 678 /* 679 * Destination has no swapblk and is not resident, 680 * copy source. 681 */ 682 daddr_t srcaddr; 683 684 srcaddr = swp_pager_meta_ctl( 685 srcobject, 686 i + offset, 687 SWM_POP 688 ); 689 690 if (srcaddr != SWAPBLK_NONE) 691 swp_pager_meta_build(dstobject, i, srcaddr); 692 } else { 693 /* 694 * Destination has valid swapblk or it is represented 695 * by a resident page. We destroy the sourceblock. 696 */ 697 698 swp_pager_meta_ctl(srcobject, i + offset, SWM_FREE); 699 } 700 } 701 702 /* 703 * Free left over swap blocks in source. 704 * 705 * We have to revert the type to OBJT_DEFAULT so we do not accidently 706 * double-remove the object from the swap queues. 707 */ 708 709 if (destroysource) { 710 swp_pager_meta_free_all(srcobject); 711 /* 712 * Reverting the type is not necessary, the caller is going 713 * to destroy srcobject directly, but I'm doing it here 714 * for consistency since we've removed the object from its 715 * queues. 716 */ 717 srcobject->type = OBJT_DEFAULT; 718 } 719 splx(s); 720 } 721 722 /* 723 * SWAP_PAGER_HASPAGE() - determine if we have good backing store for 724 * the requested page. 725 * 726 * We determine whether good backing store exists for the requested 727 * page and return TRUE if it does, FALSE if it doesn't. 728 * 729 * If TRUE, we also try to determine how much valid, contiguous backing 730 * store exists before and after the requested page within a reasonable 731 * distance. We do not try to restrict it to the swap device stripe 732 * (that is handled in getpages/putpages). It probably isn't worth 733 * doing here. 734 */ 735 736 boolean_t 737 swap_pager_haspage(object, pindex, before, after) 738 vm_object_t object; 739 vm_pindex_t pindex; 740 int *before; 741 int *after; 742 { 743 daddr_t blk0; 744 int s; 745 746 /* 747 * do we have good backing store at the requested index ? 748 */ 749 750 s = splvm(); 751 blk0 = swp_pager_meta_ctl(object, pindex, 0); 752 753 if (blk0 == SWAPBLK_NONE) { 754 splx(s); 755 if (before) 756 *before = 0; 757 if (after) 758 *after = 0; 759 return (FALSE); 760 } 761 762 /* 763 * find backwards-looking contiguous good backing store 764 */ 765 766 if (before != NULL) { 767 int i; 768 769 for (i = 1; i < (SWB_NPAGES/2); ++i) { 770 daddr_t blk; 771 772 if (i > pindex) 773 break; 774 blk = swp_pager_meta_ctl(object, pindex - i, 0); 775 if (blk != blk0 - i) 776 break; 777 } 778 *before = (i - 1); 779 } 780 781 /* 782 * find forward-looking contiguous good backing store 783 */ 784 785 if (after != NULL) { 786 int i; 787 788 for (i = 1; i < (SWB_NPAGES/2); ++i) { 789 daddr_t blk; 790 791 blk = swp_pager_meta_ctl(object, pindex + i, 0); 792 if (blk != blk0 + i) 793 break; 794 } 795 *after = (i - 1); 796 } 797 splx(s); 798 return (TRUE); 799 } 800 801 /* 802 * SWAP_PAGER_PAGE_UNSWAPPED() - remove swap backing store related to page 803 * 804 * This removes any associated swap backing store, whether valid or 805 * not, from the page. 806 * 807 * This routine is typically called when a page is made dirty, at 808 * which point any associated swap can be freed. MADV_FREE also 809 * calls us in a special-case situation 810 * 811 * NOTE!!! If the page is clean and the swap was valid, the caller 812 * should make the page dirty before calling this routine. This routine 813 * does NOT change the m->dirty status of the page. Also: MADV_FREE 814 * depends on it. 815 * 816 * This routine may not block 817 * This routine must be called at splvm() 818 */ 819 820 static void 821 swap_pager_unswapped(m) 822 vm_page_t m; 823 { 824 swp_pager_meta_ctl(m->object, m->pindex, SWM_FREE); 825 } 826 827 /* 828 * SWAP_PAGER_STRATEGY() - read, write, free blocks 829 * 830 * This implements the vm_pager_strategy() interface to swap and allows 831 * other parts of the system to directly access swap as backing store 832 * through vm_objects of type OBJT_SWAP. This is intended to be a 833 * cacheless interface ( i.e. caching occurs at higher levels ). 834 * Therefore we do not maintain any resident pages. All I/O goes 835 * directly to and from the swap device. 836 * 837 * Note that b_blkno is scaled for PAGE_SIZE 838 * 839 * We currently attempt to run I/O synchronously or asynchronously as 840 * the caller requests. This isn't perfect because we loose error 841 * sequencing when we run multiple ops in parallel to satisfy a request. 842 * But this is swap, so we let it all hang out. 843 */ 844 845 static void 846 swap_pager_strategy(vm_object_t object, struct bio *bp) 847 { 848 vm_pindex_t start; 849 int count; 850 int s; 851 char *data; 852 struct buf *nbp = NULL; 853 854 /* XXX: KASSERT instead ? */ 855 if (bp->bio_bcount & PAGE_MASK) { 856 biofinish(bp, NULL, EINVAL); 857 printf("swap_pager_strategy: bp %p blk %d size %d, not page bounded\n", bp, (int)bp->bio_pblkno, (int)bp->bio_bcount); 858 return; 859 } 860 861 /* 862 * Clear error indication, initialize page index, count, data pointer. 863 */ 864 865 bp->bio_error = 0; 866 bp->bio_flags &= ~BIO_ERROR; 867 bp->bio_resid = bp->bio_bcount; 868 869 start = bp->bio_pblkno; 870 count = howmany(bp->bio_bcount, PAGE_SIZE); 871 data = bp->bio_data; 872 873 s = splvm(); 874 875 /* 876 * Deal with BIO_DELETE 877 */ 878 879 if (bp->bio_cmd == BIO_DELETE) { 880 /* 881 * FREE PAGE(s) - destroy underlying swap that is no longer 882 * needed. 883 */ 884 swp_pager_meta_free(object, start, count); 885 splx(s); 886 bp->bio_resid = 0; 887 biodone(bp); 888 return; 889 } 890 891 /* 892 * Execute read or write 893 */ 894 895 while (count > 0) { 896 daddr_t blk; 897 898 /* 899 * Obtain block. If block not found and writing, allocate a 900 * new block and build it into the object. 901 */ 902 903 blk = swp_pager_meta_ctl(object, start, 0); 904 if ((blk == SWAPBLK_NONE) && (bp->bio_cmd == BIO_WRITE)) { 905 blk = swp_pager_getswapspace(1); 906 if (blk == SWAPBLK_NONE) { 907 bp->bio_error = ENOMEM; 908 bp->bio_flags |= BIO_ERROR; 909 break; 910 } 911 swp_pager_meta_build(object, start, blk); 912 } 913 914 /* 915 * Do we have to flush our current collection? Yes if: 916 * 917 * - no swap block at this index 918 * - swap block is not contiguous 919 * - we cross a physical disk boundry in the 920 * stripe. 921 */ 922 923 if ( 924 nbp && (nbp->b_blkno + btoc(nbp->b_bcount) != blk || 925 ((nbp->b_blkno ^ blk) & dmmax_mask) 926 ) 927 ) { 928 splx(s); 929 if (bp->bio_cmd == BIO_READ) { 930 ++cnt.v_swapin; 931 cnt.v_swappgsin += btoc(nbp->b_bcount); 932 } else { 933 ++cnt.v_swapout; 934 cnt.v_swappgsout += btoc(nbp->b_bcount); 935 nbp->b_dirtyend = nbp->b_bcount; 936 } 937 flushchainbuf(nbp); 938 s = splvm(); 939 nbp = NULL; 940 } 941 942 /* 943 * Add new swapblk to nbp, instantiating nbp if necessary. 944 * Zero-fill reads are able to take a shortcut. 945 */ 946 947 if (blk == SWAPBLK_NONE) { 948 /* 949 * We can only get here if we are reading. Since 950 * we are at splvm() we can safely modify b_resid, 951 * even if chain ops are in progress. 952 */ 953 bzero(data, PAGE_SIZE); 954 bp->bio_resid -= PAGE_SIZE; 955 } else { 956 if (nbp == NULL) { 957 nbp = getchainbuf(bp, swapdev_vp, B_ASYNC); 958 nbp->b_blkno = blk; 959 nbp->b_bcount = 0; 960 nbp->b_data = data; 961 } 962 nbp->b_bcount += PAGE_SIZE; 963 } 964 --count; 965 ++start; 966 data += PAGE_SIZE; 967 } 968 969 /* 970 * Flush out last buffer 971 */ 972 973 splx(s); 974 975 if (nbp) { 976 if (nbp->b_iocmd == BIO_READ) { 977 ++cnt.v_swapin; 978 cnt.v_swappgsin += btoc(nbp->b_bcount); 979 } else { 980 ++cnt.v_swapout; 981 cnt.v_swappgsout += btoc(nbp->b_bcount); 982 nbp->b_dirtyend = nbp->b_bcount; 983 } 984 flushchainbuf(nbp); 985 /* nbp = NULL; */ 986 } 987 988 /* 989 * Wait for completion. 990 */ 991 992 waitchainbuf(bp, 0, 1); 993 } 994 995 /* 996 * SWAP_PAGER_GETPAGES() - bring pages in from swap 997 * 998 * Attempt to retrieve (m, count) pages from backing store, but make 999 * sure we retrieve at least m[reqpage]. We try to load in as large 1000 * a chunk surrounding m[reqpage] as is contiguous in swap and which 1001 * belongs to the same object. 1002 * 1003 * The code is designed for asynchronous operation and 1004 * immediate-notification of 'reqpage' but tends not to be 1005 * used that way. Please do not optimize-out this algorithmic 1006 * feature, I intend to improve on it in the future. 1007 * 1008 * The parent has a single vm_object_pip_add() reference prior to 1009 * calling us and we should return with the same. 1010 * 1011 * The parent has BUSY'd the pages. We should return with 'm' 1012 * left busy, but the others adjusted. 1013 */ 1014 1015 static int 1016 swap_pager_getpages(object, m, count, reqpage) 1017 vm_object_t object; 1018 vm_page_t *m; 1019 int count, reqpage; 1020 { 1021 struct buf *bp; 1022 vm_page_t mreq; 1023 int s; 1024 int i; 1025 int j; 1026 daddr_t blk; 1027 vm_offset_t kva; 1028 vm_pindex_t lastpindex; 1029 1030 mreq = m[reqpage]; 1031 1032 if (mreq->object != object) { 1033 panic("swap_pager_getpages: object mismatch %p/%p", 1034 object, 1035 mreq->object 1036 ); 1037 } 1038 /* 1039 * Calculate range to retrieve. The pages have already been assigned 1040 * their swapblks. We require a *contiguous* range that falls entirely 1041 * within a single device stripe. If we do not supply it, bad things 1042 * happen. Note that blk, iblk & jblk can be SWAPBLK_NONE, but the 1043 * loops are set up such that the case(s) are handled implicitly. 1044 * 1045 * The swp_*() calls must be made at splvm(). vm_page_free() does 1046 * not need to be, but it will go a little faster if it is. 1047 */ 1048 1049 s = splvm(); 1050 blk = swp_pager_meta_ctl(mreq->object, mreq->pindex, 0); 1051 1052 for (i = reqpage - 1; i >= 0; --i) { 1053 daddr_t iblk; 1054 1055 iblk = swp_pager_meta_ctl(m[i]->object, m[i]->pindex, 0); 1056 if (blk != iblk + (reqpage - i)) 1057 break; 1058 if ((blk ^ iblk) & dmmax_mask) 1059 break; 1060 } 1061 ++i; 1062 1063 for (j = reqpage + 1; j < count; ++j) { 1064 daddr_t jblk; 1065 1066 jblk = swp_pager_meta_ctl(m[j]->object, m[j]->pindex, 0); 1067 if (blk != jblk - (j - reqpage)) 1068 break; 1069 if ((blk ^ jblk) & dmmax_mask) 1070 break; 1071 } 1072 1073 /* 1074 * free pages outside our collection range. Note: we never free 1075 * mreq, it must remain busy throughout. 1076 */ 1077 1078 { 1079 int k; 1080 1081 for (k = 0; k < i; ++k) 1082 vm_page_free(m[k]); 1083 for (k = j; k < count; ++k) 1084 vm_page_free(m[k]); 1085 } 1086 splx(s); 1087 1088 1089 /* 1090 * Return VM_PAGER_FAIL if we have nothing to do. Return mreq 1091 * still busy, but the others unbusied. 1092 */ 1093 1094 if (blk == SWAPBLK_NONE) 1095 return(VM_PAGER_FAIL); 1096 1097 /* 1098 * Get a swap buffer header to perform the IO 1099 */ 1100 1101 bp = getpbuf(&nsw_rcount); 1102 kva = (vm_offset_t) bp->b_data; 1103 1104 /* 1105 * map our page(s) into kva for input 1106 * 1107 * NOTE: B_PAGING is set by pbgetvp() 1108 */ 1109 1110 pmap_qenter(kva, m + i, j - i); 1111 1112 bp->b_iocmd = BIO_READ; 1113 bp->b_iodone = swp_pager_async_iodone; 1114 bp->b_rcred = bp->b_wcred = proc0.p_ucred; 1115 bp->b_data = (caddr_t) kva; 1116 crhold(bp->b_rcred); 1117 crhold(bp->b_wcred); 1118 bp->b_blkno = blk - (reqpage - i); 1119 bp->b_bcount = PAGE_SIZE * (j - i); 1120 bp->b_bufsize = PAGE_SIZE * (j - i); 1121 bp->b_pager.pg_reqpage = reqpage - i; 1122 1123 { 1124 int k; 1125 1126 for (k = i; k < j; ++k) { 1127 bp->b_pages[k - i] = m[k]; 1128 vm_page_flag_set(m[k], PG_SWAPINPROG); 1129 } 1130 } 1131 bp->b_npages = j - i; 1132 1133 pbgetvp(swapdev_vp, bp); 1134 1135 cnt.v_swapin++; 1136 cnt.v_swappgsin += bp->b_npages; 1137 1138 /* 1139 * We still hold the lock on mreq, and our automatic completion routine 1140 * does not remove it. 1141 */ 1142 1143 vm_object_pip_add(mreq->object, bp->b_npages); 1144 lastpindex = m[j-1]->pindex; 1145 1146 /* 1147 * perform the I/O. NOTE!!! bp cannot be considered valid after 1148 * this point because we automatically release it on completion. 1149 * Instead, we look at the one page we are interested in which we 1150 * still hold a lock on even through the I/O completion. 1151 * 1152 * The other pages in our m[] array are also released on completion, 1153 * so we cannot assume they are valid anymore either. 1154 * 1155 * NOTE: b_blkno is destroyed by the call to VOP_STRATEGY 1156 */ 1157 1158 BUF_KERNPROC(bp); 1159 BUF_STRATEGY(bp); 1160 1161 /* 1162 * wait for the page we want to complete. PG_SWAPINPROG is always 1163 * cleared on completion. If an I/O error occurs, SWAPBLK_NONE 1164 * is set in the meta-data. 1165 */ 1166 1167 s = splvm(); 1168 1169 while ((mreq->flags & PG_SWAPINPROG) != 0) { 1170 vm_page_flag_set(mreq, PG_WANTED | PG_REFERENCED); 1171 cnt.v_intrans++; 1172 if (tsleep(mreq, PSWP, "swread", hz*20)) { 1173 printf( 1174 "swap_pager: indefinite wait buffer: device:" 1175 " %s, blkno: %ld, size: %ld\n", 1176 devtoname(bp->b_dev), (long)bp->b_blkno, 1177 bp->b_bcount 1178 ); 1179 } 1180 } 1181 1182 splx(s); 1183 1184 /* 1185 * mreq is left bussied after completion, but all the other pages 1186 * are freed. If we had an unrecoverable read error the page will 1187 * not be valid. 1188 */ 1189 1190 if (mreq->valid != VM_PAGE_BITS_ALL) { 1191 return(VM_PAGER_ERROR); 1192 } else { 1193 return(VM_PAGER_OK); 1194 } 1195 1196 /* 1197 * A final note: in a low swap situation, we cannot deallocate swap 1198 * and mark a page dirty here because the caller is likely to mark 1199 * the page clean when we return, causing the page to possibly revert 1200 * to all-zero's later. 1201 */ 1202 } 1203 1204 /* 1205 * swap_pager_putpages: 1206 * 1207 * Assign swap (if necessary) and initiate I/O on the specified pages. 1208 * 1209 * We support both OBJT_DEFAULT and OBJT_SWAP objects. DEFAULT objects 1210 * are automatically converted to SWAP objects. 1211 * 1212 * In a low memory situation we may block in VOP_STRATEGY(), but the new 1213 * vm_page reservation system coupled with properly written VFS devices 1214 * should ensure that no low-memory deadlock occurs. This is an area 1215 * which needs work. 1216 * 1217 * The parent has N vm_object_pip_add() references prior to 1218 * calling us and will remove references for rtvals[] that are 1219 * not set to VM_PAGER_PEND. We need to remove the rest on I/O 1220 * completion. 1221 * 1222 * The parent has soft-busy'd the pages it passes us and will unbusy 1223 * those whos rtvals[] entry is not set to VM_PAGER_PEND on return. 1224 * We need to unbusy the rest on I/O completion. 1225 */ 1226 1227 void 1228 swap_pager_putpages(object, m, count, sync, rtvals) 1229 vm_object_t object; 1230 vm_page_t *m; 1231 int count; 1232 boolean_t sync; 1233 int *rtvals; 1234 { 1235 int i; 1236 int n = 0; 1237 1238 if (count && m[0]->object != object) { 1239 panic("swap_pager_getpages: object mismatch %p/%p", 1240 object, 1241 m[0]->object 1242 ); 1243 } 1244 /* 1245 * Step 1 1246 * 1247 * Turn object into OBJT_SWAP 1248 * check for bogus sysops 1249 * force sync if not pageout process 1250 */ 1251 1252 if (object->type != OBJT_SWAP) 1253 swp_pager_meta_build(object, 0, SWAPBLK_NONE); 1254 1255 if (curproc != pageproc) 1256 sync = TRUE; 1257 1258 /* 1259 * Step 2 1260 * 1261 * Update nsw parameters from swap_async_max sysctl values. 1262 * Do not let the sysop crash the machine with bogus numbers. 1263 */ 1264 1265 if (swap_async_max != nsw_wcount_async_max) { 1266 int n; 1267 int s; 1268 1269 /* 1270 * limit range 1271 */ 1272 if ((n = swap_async_max) > nswbuf / 2) 1273 n = nswbuf / 2; 1274 if (n < 1) 1275 n = 1; 1276 swap_async_max = n; 1277 1278 /* 1279 * Adjust difference ( if possible ). If the current async 1280 * count is too low, we may not be able to make the adjustment 1281 * at this time. 1282 */ 1283 s = splvm(); 1284 mtx_lock(&pbuf_mtx); 1285 n -= nsw_wcount_async_max; 1286 if (nsw_wcount_async + n >= 0) { 1287 nsw_wcount_async += n; 1288 nsw_wcount_async_max += n; 1289 wakeup(&nsw_wcount_async); 1290 } 1291 mtx_unlock(&pbuf_mtx); 1292 splx(s); 1293 } 1294 1295 /* 1296 * Step 3 1297 * 1298 * Assign swap blocks and issue I/O. We reallocate swap on the fly. 1299 * The page is left dirty until the pageout operation completes 1300 * successfully. 1301 */ 1302 1303 for (i = 0; i < count; i += n) { 1304 int s; 1305 int j; 1306 struct buf *bp; 1307 daddr_t blk; 1308 1309 /* 1310 * Maximum I/O size is limited by a number of factors. 1311 */ 1312 1313 n = min(BLIST_MAX_ALLOC, count - i); 1314 n = min(n, nsw_cluster_max); 1315 1316 s = splvm(); 1317 1318 /* 1319 * Get biggest block of swap we can. If we fail, fall 1320 * back and try to allocate a smaller block. Don't go 1321 * overboard trying to allocate space if it would overly 1322 * fragment swap. 1323 */ 1324 while ( 1325 (blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE && 1326 n > 4 1327 ) { 1328 n >>= 1; 1329 } 1330 if (blk == SWAPBLK_NONE) { 1331 for (j = 0; j < n; ++j) 1332 rtvals[i+j] = VM_PAGER_FAIL; 1333 splx(s); 1334 continue; 1335 } 1336 1337 /* 1338 * The I/O we are constructing cannot cross a physical 1339 * disk boundry in the swap stripe. Note: we are still 1340 * at splvm(). 1341 */ 1342 if ((blk ^ (blk + n)) & dmmax_mask) { 1343 j = ((blk + dmmax) & dmmax_mask) - blk; 1344 swp_pager_freeswapspace(blk + j, n - j); 1345 n = j; 1346 } 1347 1348 /* 1349 * All I/O parameters have been satisfied, build the I/O 1350 * request and assign the swap space. 1351 * 1352 * NOTE: B_PAGING is set by pbgetvp() 1353 */ 1354 1355 if (sync == TRUE) { 1356 bp = getpbuf(&nsw_wcount_sync); 1357 } else { 1358 bp = getpbuf(&nsw_wcount_async); 1359 bp->b_flags = B_ASYNC; 1360 } 1361 bp->b_iocmd = BIO_WRITE; 1362 bp->b_spc = NULL; /* not used, but NULL-out anyway */ 1363 1364 pmap_qenter((vm_offset_t)bp->b_data, &m[i], n); 1365 1366 bp->b_rcred = bp->b_wcred = proc0.p_ucred; 1367 bp->b_bcount = PAGE_SIZE * n; 1368 bp->b_bufsize = PAGE_SIZE * n; 1369 bp->b_blkno = blk; 1370 1371 crhold(bp->b_rcred); 1372 crhold(bp->b_wcred); 1373 1374 pbgetvp(swapdev_vp, bp); 1375 1376 for (j = 0; j < n; ++j) { 1377 vm_page_t mreq = m[i+j]; 1378 1379 swp_pager_meta_build( 1380 mreq->object, 1381 mreq->pindex, 1382 blk + j 1383 ); 1384 vm_page_dirty(mreq); 1385 rtvals[i+j] = VM_PAGER_OK; 1386 1387 vm_page_flag_set(mreq, PG_SWAPINPROG); 1388 bp->b_pages[j] = mreq; 1389 } 1390 bp->b_npages = n; 1391 /* 1392 * Must set dirty range for NFS to work. 1393 */ 1394 bp->b_dirtyoff = 0; 1395 bp->b_dirtyend = bp->b_bcount; 1396 1397 cnt.v_swapout++; 1398 cnt.v_swappgsout += bp->b_npages; 1399 swapdev_vp->v_numoutput++; 1400 1401 splx(s); 1402 1403 /* 1404 * asynchronous 1405 * 1406 * NOTE: b_blkno is destroyed by the call to VOP_STRATEGY 1407 */ 1408 1409 if (sync == FALSE) { 1410 bp->b_iodone = swp_pager_async_iodone; 1411 BUF_KERNPROC(bp); 1412 BUF_STRATEGY(bp); 1413 1414 for (j = 0; j < n; ++j) 1415 rtvals[i+j] = VM_PAGER_PEND; 1416 continue; 1417 } 1418 1419 /* 1420 * synchronous 1421 * 1422 * NOTE: b_blkno is destroyed by the call to VOP_STRATEGY 1423 */ 1424 1425 bp->b_iodone = swp_pager_sync_iodone; 1426 BUF_STRATEGY(bp); 1427 1428 /* 1429 * Wait for the sync I/O to complete, then update rtvals. 1430 * We just set the rtvals[] to VM_PAGER_PEND so we can call 1431 * our async completion routine at the end, thus avoiding a 1432 * double-free. 1433 */ 1434 s = splbio(); 1435 1436 while ((bp->b_flags & B_DONE) == 0) { 1437 tsleep(bp, PVM, "swwrt", 0); 1438 } 1439 1440 for (j = 0; j < n; ++j) 1441 rtvals[i+j] = VM_PAGER_PEND; 1442 1443 /* 1444 * Now that we are through with the bp, we can call the 1445 * normal async completion, which frees everything up. 1446 */ 1447 1448 swp_pager_async_iodone(bp); 1449 1450 splx(s); 1451 } 1452 } 1453 1454 /* 1455 * swap_pager_sync_iodone: 1456 * 1457 * Completion routine for synchronous reads and writes from/to swap. 1458 * We just mark the bp is complete and wake up anyone waiting on it. 1459 * 1460 * This routine may not block. This routine is called at splbio() or better. 1461 */ 1462 1463 static void 1464 swp_pager_sync_iodone(bp) 1465 struct buf *bp; 1466 { 1467 bp->b_flags |= B_DONE; 1468 bp->b_flags &= ~B_ASYNC; 1469 wakeup(bp); 1470 } 1471 1472 /* 1473 * swp_pager_async_iodone: 1474 * 1475 * Completion routine for asynchronous reads and writes from/to swap. 1476 * Also called manually by synchronous code to finish up a bp. 1477 * 1478 * For READ operations, the pages are PG_BUSY'd. For WRITE operations, 1479 * the pages are vm_page_t->busy'd. For READ operations, we PG_BUSY 1480 * unbusy all pages except the 'main' request page. For WRITE 1481 * operations, we vm_page_t->busy'd unbusy all pages ( we can do this 1482 * because we marked them all VM_PAGER_PEND on return from putpages ). 1483 * 1484 * This routine may not block. 1485 * This routine is called at splbio() or better 1486 * 1487 * We up ourselves to splvm() as required for various vm_page related 1488 * calls. 1489 */ 1490 1491 static void 1492 swp_pager_async_iodone(bp) 1493 register struct buf *bp; 1494 { 1495 int s; 1496 int i; 1497 vm_object_t object = NULL; 1498 1499 bp->b_flags |= B_DONE; 1500 1501 /* 1502 * report error 1503 */ 1504 1505 if (bp->b_ioflags & BIO_ERROR) { 1506 printf( 1507 "swap_pager: I/O error - %s failed; blkno %ld," 1508 "size %ld, error %d\n", 1509 ((bp->b_iocmd == BIO_READ) ? "pagein" : "pageout"), 1510 (long)bp->b_blkno, 1511 (long)bp->b_bcount, 1512 bp->b_error 1513 ); 1514 } 1515 1516 /* 1517 * set object, raise to splvm(). 1518 */ 1519 1520 if (bp->b_npages) 1521 object = bp->b_pages[0]->object; 1522 s = splvm(); 1523 1524 /* 1525 * remove the mapping for kernel virtual 1526 */ 1527 1528 pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages); 1529 1530 /* 1531 * cleanup pages. If an error occurs writing to swap, we are in 1532 * very serious trouble. If it happens to be a disk error, though, 1533 * we may be able to recover by reassigning the swap later on. So 1534 * in this case we remove the m->swapblk assignment for the page 1535 * but do not free it in the rlist. The errornous block(s) are thus 1536 * never reallocated as swap. Redirty the page and continue. 1537 */ 1538 1539 for (i = 0; i < bp->b_npages; ++i) { 1540 vm_page_t m = bp->b_pages[i]; 1541 1542 vm_page_flag_clear(m, PG_SWAPINPROG); 1543 1544 if (bp->b_ioflags & BIO_ERROR) { 1545 /* 1546 * If an error occurs I'd love to throw the swapblk 1547 * away without freeing it back to swapspace, so it 1548 * can never be used again. But I can't from an 1549 * interrupt. 1550 */ 1551 1552 if (bp->b_iocmd == BIO_READ) { 1553 /* 1554 * When reading, reqpage needs to stay 1555 * locked for the parent, but all other 1556 * pages can be freed. We still want to 1557 * wakeup the parent waiting on the page, 1558 * though. ( also: pg_reqpage can be -1 and 1559 * not match anything ). 1560 * 1561 * We have to wake specifically requested pages 1562 * up too because we cleared PG_SWAPINPROG and 1563 * someone may be waiting for that. 1564 * 1565 * NOTE: for reads, m->dirty will probably 1566 * be overridden by the original caller of 1567 * getpages so don't play cute tricks here. 1568 * 1569 * XXX IT IS NOT LEGAL TO FREE THE PAGE HERE 1570 * AS THIS MESSES WITH object->memq, and it is 1571 * not legal to mess with object->memq from an 1572 * interrupt. 1573 */ 1574 1575 m->valid = 0; 1576 vm_page_flag_clear(m, PG_ZERO); 1577 1578 if (i != bp->b_pager.pg_reqpage) 1579 vm_page_free(m); 1580 else 1581 vm_page_flash(m); 1582 /* 1583 * If i == bp->b_pager.pg_reqpage, do not wake 1584 * the page up. The caller needs to. 1585 */ 1586 } else { 1587 /* 1588 * If a write error occurs, reactivate page 1589 * so it doesn't clog the inactive list, 1590 * then finish the I/O. 1591 */ 1592 vm_page_dirty(m); 1593 vm_page_activate(m); 1594 vm_page_io_finish(m); 1595 } 1596 } else if (bp->b_iocmd == BIO_READ) { 1597 /* 1598 * For read success, clear dirty bits. Nobody should 1599 * have this page mapped but don't take any chances, 1600 * make sure the pmap modify bits are also cleared. 1601 * 1602 * NOTE: for reads, m->dirty will probably be 1603 * overridden by the original caller of getpages so 1604 * we cannot set them in order to free the underlying 1605 * swap in a low-swap situation. I don't think we'd 1606 * want to do that anyway, but it was an optimization 1607 * that existed in the old swapper for a time before 1608 * it got ripped out due to precisely this problem. 1609 * 1610 * clear PG_ZERO in page. 1611 * 1612 * If not the requested page then deactivate it. 1613 * 1614 * Note that the requested page, reqpage, is left 1615 * busied, but we still have to wake it up. The 1616 * other pages are released (unbusied) by 1617 * vm_page_wakeup(). We do not set reqpage's 1618 * valid bits here, it is up to the caller. 1619 */ 1620 1621 pmap_clear_modify(m); 1622 m->valid = VM_PAGE_BITS_ALL; 1623 vm_page_undirty(m); 1624 vm_page_flag_clear(m, PG_ZERO); 1625 1626 /* 1627 * We have to wake specifically requested pages 1628 * up too because we cleared PG_SWAPINPROG and 1629 * could be waiting for it in getpages. However, 1630 * be sure to not unbusy getpages specifically 1631 * requested page - getpages expects it to be 1632 * left busy. 1633 */ 1634 if (i != bp->b_pager.pg_reqpage) { 1635 vm_page_deactivate(m); 1636 vm_page_wakeup(m); 1637 } else { 1638 vm_page_flash(m); 1639 } 1640 } else { 1641 /* 1642 * For write success, clear the modify and dirty 1643 * status, then finish the I/O ( which decrements the 1644 * busy count and possibly wakes waiter's up ). 1645 */ 1646 pmap_clear_modify(m); 1647 vm_page_undirty(m); 1648 vm_page_io_finish(m); 1649 if (!vm_page_count_severe() || !vm_page_try_to_cache(m)) 1650 vm_page_protect(m, VM_PROT_READ); 1651 } 1652 } 1653 1654 /* 1655 * adjust pip. NOTE: the original parent may still have its own 1656 * pip refs on the object. 1657 */ 1658 1659 if (object) 1660 vm_object_pip_wakeupn(object, bp->b_npages); 1661 1662 /* 1663 * release the physical I/O buffer 1664 */ 1665 1666 relpbuf( 1667 bp, 1668 ((bp->b_iocmd == BIO_READ) ? &nsw_rcount : 1669 ((bp->b_flags & B_ASYNC) ? 1670 &nsw_wcount_async : 1671 &nsw_wcount_sync 1672 ) 1673 ) 1674 ); 1675 splx(s); 1676 } 1677 1678 /************************************************************************ 1679 * SWAP META DATA * 1680 ************************************************************************ 1681 * 1682 * These routines manipulate the swap metadata stored in the 1683 * OBJT_SWAP object. All swp_*() routines must be called at 1684 * splvm() because swap can be freed up by the low level vm_page 1685 * code which might be called from interrupts beyond what splbio() covers. 1686 * 1687 * Swap metadata is implemented with a global hash and not directly 1688 * linked into the object. Instead the object simply contains 1689 * appropriate tracking counters. 1690 */ 1691 1692 /* 1693 * SWP_PAGER_HASH() - hash swap meta data 1694 * 1695 * This is an inline helper function which hashes the swapblk given 1696 * the object and page index. It returns a pointer to a pointer 1697 * to the object, or a pointer to a NULL pointer if it could not 1698 * find a swapblk. 1699 * 1700 * This routine must be called at splvm(). 1701 */ 1702 1703 static __inline struct swblock ** 1704 swp_pager_hash(vm_object_t object, vm_pindex_t index) 1705 { 1706 struct swblock **pswap; 1707 struct swblock *swap; 1708 1709 index &= ~SWAP_META_MASK; 1710 pswap = &swhash[(index ^ (int)(intptr_t)object) & swhash_mask]; 1711 1712 while ((swap = *pswap) != NULL) { 1713 if (swap->swb_object == object && 1714 swap->swb_index == index 1715 ) { 1716 break; 1717 } 1718 pswap = &swap->swb_hnext; 1719 } 1720 return(pswap); 1721 } 1722 1723 /* 1724 * SWP_PAGER_META_BUILD() - add swap block to swap meta data for object 1725 * 1726 * We first convert the object to a swap object if it is a default 1727 * object. 1728 * 1729 * The specified swapblk is added to the object's swap metadata. If 1730 * the swapblk is not valid, it is freed instead. Any previously 1731 * assigned swapblk is freed. 1732 * 1733 * This routine must be called at splvm(), except when used to convert 1734 * an OBJT_DEFAULT object into an OBJT_SWAP object. 1735 1736 */ 1737 1738 static void 1739 swp_pager_meta_build( 1740 vm_object_t object, 1741 vm_pindex_t index, 1742 daddr_t swapblk 1743 ) { 1744 struct swblock *swap; 1745 struct swblock **pswap; 1746 1747 /* 1748 * Convert default object to swap object if necessary 1749 */ 1750 1751 if (object->type != OBJT_SWAP) { 1752 object->type = OBJT_SWAP; 1753 object->un_pager.swp.swp_bcount = 0; 1754 1755 mtx_lock(&sw_alloc_mtx); 1756 if (object->handle != NULL) { 1757 TAILQ_INSERT_TAIL( 1758 NOBJLIST(object->handle), 1759 object, 1760 pager_object_list 1761 ); 1762 } else { 1763 TAILQ_INSERT_TAIL( 1764 &swap_pager_un_object_list, 1765 object, 1766 pager_object_list 1767 ); 1768 } 1769 mtx_unlock(&sw_alloc_mtx); 1770 } 1771 1772 /* 1773 * Locate hash entry. If not found create, but if we aren't adding 1774 * anything just return. If we run out of space in the map we wait 1775 * and, since the hash table may have changed, retry. 1776 */ 1777 1778 retry: 1779 pswap = swp_pager_hash(object, index); 1780 1781 if ((swap = *pswap) == NULL) { 1782 int i; 1783 1784 if (swapblk == SWAPBLK_NONE) 1785 return; 1786 1787 swap = *pswap = zalloc(swap_zone); 1788 if (swap == NULL) { 1789 VM_WAIT; 1790 goto retry; 1791 } 1792 swap->swb_hnext = NULL; 1793 swap->swb_object = object; 1794 swap->swb_index = index & ~SWAP_META_MASK; 1795 swap->swb_count = 0; 1796 1797 ++object->un_pager.swp.swp_bcount; 1798 1799 for (i = 0; i < SWAP_META_PAGES; ++i) 1800 swap->swb_pages[i] = SWAPBLK_NONE; 1801 } 1802 1803 /* 1804 * Delete prior contents of metadata 1805 */ 1806 1807 index &= SWAP_META_MASK; 1808 1809 if (swap->swb_pages[index] != SWAPBLK_NONE) { 1810 swp_pager_freeswapspace(swap->swb_pages[index], 1); 1811 --swap->swb_count; 1812 } 1813 1814 /* 1815 * Enter block into metadata 1816 */ 1817 1818 swap->swb_pages[index] = swapblk; 1819 if (swapblk != SWAPBLK_NONE) 1820 ++swap->swb_count; 1821 } 1822 1823 /* 1824 * SWP_PAGER_META_FREE() - free a range of blocks in the object's swap metadata 1825 * 1826 * The requested range of blocks is freed, with any associated swap 1827 * returned to the swap bitmap. 1828 * 1829 * This routine will free swap metadata structures as they are cleaned 1830 * out. This routine does *NOT* operate on swap metadata associated 1831 * with resident pages. 1832 * 1833 * This routine must be called at splvm() 1834 */ 1835 1836 static void 1837 swp_pager_meta_free(vm_object_t object, vm_pindex_t index, daddr_t count) 1838 { 1839 if (object->type != OBJT_SWAP) 1840 return; 1841 1842 while (count > 0) { 1843 struct swblock **pswap; 1844 struct swblock *swap; 1845 1846 pswap = swp_pager_hash(object, index); 1847 1848 if ((swap = *pswap) != NULL) { 1849 daddr_t v = swap->swb_pages[index & SWAP_META_MASK]; 1850 1851 if (v != SWAPBLK_NONE) { 1852 swp_pager_freeswapspace(v, 1); 1853 swap->swb_pages[index & SWAP_META_MASK] = 1854 SWAPBLK_NONE; 1855 if (--swap->swb_count == 0) { 1856 *pswap = swap->swb_hnext; 1857 zfree(swap_zone, swap); 1858 --object->un_pager.swp.swp_bcount; 1859 } 1860 } 1861 --count; 1862 ++index; 1863 } else { 1864 int n = SWAP_META_PAGES - (index & SWAP_META_MASK); 1865 count -= n; 1866 index += n; 1867 } 1868 } 1869 } 1870 1871 /* 1872 * SWP_PAGER_META_FREE_ALL() - destroy all swap metadata associated with object 1873 * 1874 * This routine locates and destroys all swap metadata associated with 1875 * an object. 1876 * 1877 * This routine must be called at splvm() 1878 */ 1879 1880 static void 1881 swp_pager_meta_free_all(vm_object_t object) 1882 { 1883 daddr_t index = 0; 1884 1885 if (object->type != OBJT_SWAP) 1886 return; 1887 1888 while (object->un_pager.swp.swp_bcount) { 1889 struct swblock **pswap; 1890 struct swblock *swap; 1891 1892 pswap = swp_pager_hash(object, index); 1893 if ((swap = *pswap) != NULL) { 1894 int i; 1895 1896 for (i = 0; i < SWAP_META_PAGES; ++i) { 1897 daddr_t v = swap->swb_pages[i]; 1898 if (v != SWAPBLK_NONE) { 1899 --swap->swb_count; 1900 swp_pager_freeswapspace(v, 1); 1901 } 1902 } 1903 if (swap->swb_count != 0) 1904 panic("swap_pager_meta_free_all: swb_count != 0"); 1905 *pswap = swap->swb_hnext; 1906 zfree(swap_zone, swap); 1907 --object->un_pager.swp.swp_bcount; 1908 } 1909 index += SWAP_META_PAGES; 1910 if (index > 0x20000000) 1911 panic("swp_pager_meta_free_all: failed to locate all swap meta blocks"); 1912 } 1913 } 1914 1915 /* 1916 * SWP_PAGER_METACTL() - misc control of swap and vm_page_t meta data. 1917 * 1918 * This routine is capable of looking up, popping, or freeing 1919 * swapblk assignments in the swap meta data or in the vm_page_t. 1920 * The routine typically returns the swapblk being looked-up, or popped, 1921 * or SWAPBLK_NONE if the block was freed, or SWAPBLK_NONE if the block 1922 * was invalid. This routine will automatically free any invalid 1923 * meta-data swapblks. 1924 * 1925 * It is not possible to store invalid swapblks in the swap meta data 1926 * (other then a literal 'SWAPBLK_NONE'), so we don't bother checking. 1927 * 1928 * When acting on a busy resident page and paging is in progress, we 1929 * have to wait until paging is complete but otherwise can act on the 1930 * busy page. 1931 * 1932 * This routine must be called at splvm(). 1933 * 1934 * SWM_FREE remove and free swap block from metadata 1935 * SWM_POP remove from meta data but do not free.. pop it out 1936 */ 1937 1938 static daddr_t 1939 swp_pager_meta_ctl( 1940 vm_object_t object, 1941 vm_pindex_t index, 1942 int flags 1943 ) { 1944 struct swblock **pswap; 1945 struct swblock *swap; 1946 daddr_t r1; 1947 1948 /* 1949 * The meta data only exists of the object is OBJT_SWAP 1950 * and even then might not be allocated yet. 1951 */ 1952 1953 if (object->type != OBJT_SWAP) 1954 return(SWAPBLK_NONE); 1955 1956 r1 = SWAPBLK_NONE; 1957 pswap = swp_pager_hash(object, index); 1958 1959 if ((swap = *pswap) != NULL) { 1960 index &= SWAP_META_MASK; 1961 r1 = swap->swb_pages[index]; 1962 1963 if (r1 != SWAPBLK_NONE) { 1964 if (flags & SWM_FREE) { 1965 swp_pager_freeswapspace(r1, 1); 1966 r1 = SWAPBLK_NONE; 1967 } 1968 if (flags & (SWM_FREE|SWM_POP)) { 1969 swap->swb_pages[index] = SWAPBLK_NONE; 1970 if (--swap->swb_count == 0) { 1971 *pswap = swap->swb_hnext; 1972 zfree(swap_zone, swap); 1973 --object->un_pager.swp.swp_bcount; 1974 } 1975 } 1976 } 1977 } 1978 return(r1); 1979 } 1980 1981 /******************************************************** 1982 * CHAINING FUNCTIONS * 1983 ******************************************************** 1984 * 1985 * These functions support recursion of I/O operations 1986 * on bp's, typically by chaining one or more 'child' bp's 1987 * to the parent. Synchronous, asynchronous, and semi-synchronous 1988 * chaining is possible. 1989 */ 1990 1991 /* 1992 * vm_pager_chain_iodone: 1993 * 1994 * io completion routine for child bp. Currently we fudge a bit 1995 * on dealing with b_resid. Since users of these routines may issue 1996 * multiple children simultaneously, sequencing of the error can be lost. 1997 */ 1998 1999 static void 2000 vm_pager_chain_iodone(struct buf *nbp) 2001 { 2002 struct bio *bp; 2003 u_int *count; 2004 2005 bp = nbp->b_caller1; 2006 count = (u_int *)&(bp->bio_caller1); 2007 if (bp != NULL) { 2008 if (nbp->b_ioflags & BIO_ERROR) { 2009 bp->bio_flags |= BIO_ERROR; 2010 bp->bio_error = nbp->b_error; 2011 } else if (nbp->b_resid != 0) { 2012 bp->bio_flags |= BIO_ERROR; 2013 bp->bio_error = EINVAL; 2014 } else { 2015 bp->bio_resid -= nbp->b_bcount; 2016 } 2017 nbp->b_caller1 = NULL; 2018 --(*count); 2019 if (bp->bio_flags & BIO_FLAG1) { 2020 bp->bio_flags &= ~BIO_FLAG1; 2021 wakeup(bp); 2022 } 2023 } 2024 nbp->b_flags |= B_DONE; 2025 nbp->b_flags &= ~B_ASYNC; 2026 relpbuf(nbp, NULL); 2027 } 2028 2029 /* 2030 * getchainbuf: 2031 * 2032 * Obtain a physical buffer and chain it to its parent buffer. When 2033 * I/O completes, the parent buffer will be B_SIGNAL'd. Errors are 2034 * automatically propagated to the parent 2035 */ 2036 2037 struct buf * 2038 getchainbuf(struct bio *bp, struct vnode *vp, int flags) 2039 { 2040 struct buf *nbp = getpbuf(NULL); 2041 u_int *count = (u_int *)&(bp->bio_caller1); 2042 2043 nbp->b_caller1 = bp; 2044 ++(*count); 2045 2046 if (*count > 4) 2047 waitchainbuf(bp, 4, 0); 2048 2049 nbp->b_iocmd = bp->bio_cmd; 2050 nbp->b_ioflags = bp->bio_flags & BIO_ORDERED; 2051 nbp->b_flags = flags; 2052 nbp->b_rcred = nbp->b_wcred = proc0.p_ucred; 2053 nbp->b_iodone = vm_pager_chain_iodone; 2054 2055 crhold(nbp->b_rcred); 2056 crhold(nbp->b_wcred); 2057 2058 if (vp) 2059 pbgetvp(vp, nbp); 2060 return(nbp); 2061 } 2062 2063 void 2064 flushchainbuf(struct buf *nbp) 2065 { 2066 if (nbp->b_bcount) { 2067 nbp->b_bufsize = nbp->b_bcount; 2068 if (nbp->b_iocmd == BIO_WRITE) 2069 nbp->b_dirtyend = nbp->b_bcount; 2070 BUF_KERNPROC(nbp); 2071 BUF_STRATEGY(nbp); 2072 } else { 2073 bufdone(nbp); 2074 } 2075 } 2076 2077 void 2078 waitchainbuf(struct bio *bp, int limit, int done) 2079 { 2080 int s; 2081 u_int *count = (u_int *)&(bp->bio_caller1); 2082 2083 s = splbio(); 2084 while (*count > limit) { 2085 bp->bio_flags |= BIO_FLAG1; 2086 tsleep(bp, PRIBIO + 4, "bpchain", 0); 2087 } 2088 if (done) { 2089 if (bp->bio_resid != 0 && !(bp->bio_flags & BIO_ERROR)) { 2090 bp->bio_flags |= BIO_ERROR; 2091 bp->bio_error = EINVAL; 2092 } 2093 biodone(bp); 2094 } 2095 splx(s); 2096 } 2097 2098