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