1 /* 2 * (MPSAFE) 3 * 4 * Copyright (c) 1998-2010 The DragonFly Project. All rights reserved. 5 * 6 * This code is derived from software contributed to The DragonFly Project 7 * by Matthew Dillon <dillon@backplane.com> 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 3. Neither the name of The DragonFly Project nor the names of its 20 * contributors may be used to endorse or promote products derived 21 * from this software without specific, prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * Copyright (c) 1994 John S. Dyson 37 * Copyright (c) 1990 University of Utah. 38 * Copyright (c) 1991, 1993 39 * The Regents of the University of California. All rights reserved. 40 * 41 * This code is derived from software contributed to Berkeley by 42 * the Systems Programming Group of the University of Utah Computer 43 * Science Department. 44 * 45 * Redistribution and use in source and binary forms, with or without 46 * modification, are permitted provided that the following conditions 47 * are met: 48 * 1. Redistributions of source code must retain the above copyright 49 * notice, this list of conditions and the following disclaimer. 50 * 2. Redistributions in binary form must reproduce the above copyright 51 * notice, this list of conditions and the following disclaimer in the 52 * documentation and/or other materials provided with the distribution. 53 * 3. Neither the name of the University nor the names of its contributors 54 * may be used to endorse or promote products derived from this software 55 * without specific prior written permission. 56 * 57 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 58 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 59 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 60 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 61 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 62 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 63 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 64 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 65 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 66 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 67 * SUCH DAMAGE. 68 * 69 * New Swap System 70 * Matthew Dillon 71 * 72 * Radix Bitmap 'blists'. 73 * 74 * - The new swapper uses the new radix bitmap code. This should scale 75 * to arbitrarily small or arbitrarily large swap spaces and an almost 76 * arbitrary degree of fragmentation. 77 * 78 * Features: 79 * 80 * - on the fly reallocation of swap during putpages. The new system 81 * does not try to keep previously allocated swap blocks for dirty 82 * pages. 83 * 84 * - on the fly deallocation of swap 85 * 86 * - No more garbage collection required. Unnecessarily allocated swap 87 * blocks only exist for dirty vm_page_t's now and these are already 88 * cycled (in a high-load system) by the pager. We also do on-the-fly 89 * removal of invalidated swap blocks when a page is destroyed 90 * or renamed. 91 * 92 * from: Utah $Hdr: swap_pager.c 1.4 91/04/30$ 93 * @(#)swap_pager.c 8.9 (Berkeley) 3/21/94 94 * $FreeBSD: src/sys/vm/swap_pager.c,v 1.130.2.12 2002/08/31 21:15:55 dillon Exp $ 95 */ 96 97 #include <sys/param.h> 98 #include <sys/systm.h> 99 #include <sys/conf.h> 100 #include <sys/kernel.h> 101 #include <sys/proc.h> 102 #include <sys/buf.h> 103 #include <sys/vnode.h> 104 #include <sys/malloc.h> 105 #include <sys/vmmeter.h> 106 #include <sys/sysctl.h> 107 #include <sys/blist.h> 108 #include <sys/lock.h> 109 #include <sys/kcollect.h> 110 111 #include <unistd.h> 112 #include "opt_swap.h" 113 #include <vm/vm.h> 114 #include <vm/vm_object.h> 115 #include <vm/vm_page.h> 116 #include <vm/vm_pager.h> 117 #include <vm/vm_pageout.h> 118 #include <vm/swap_pager.h> 119 #include <vm/vm_extern.h> 120 #include <vm/vm_zone.h> 121 #include <vm/vnode_pager.h> 122 123 #include <sys/buf2.h> 124 #include <vm/vm_page2.h> 125 126 #ifndef MAX_PAGEOUT_CLUSTER 127 #define MAX_PAGEOUT_CLUSTER SWB_NPAGES 128 #endif 129 130 #define SWM_FREE 0x02 /* free, period */ 131 #define SWM_POP 0x04 /* pop out */ 132 133 #define SWBIO_READ 0x01 134 #define SWBIO_WRITE 0x02 135 #define SWBIO_SYNC 0x04 136 #define SWBIO_TTC 0x08 /* for VM_PAGER_TRY_TO_CACHE */ 137 138 struct swfreeinfo { 139 vm_object_t object; 140 vm_pindex_t basei; 141 vm_pindex_t begi; 142 vm_pindex_t endi; /* inclusive */ 143 }; 144 145 struct swswapoffinfo { 146 vm_object_t object; 147 int devidx; 148 int shared; 149 }; 150 151 /* 152 * vm_swap_size is in page-sized chunks now. It was DEV_BSIZE'd chunks 153 * in the old system. 154 */ 155 156 int swap_pager_full; /* swap space exhaustion (task killing) */ 157 int swap_fail_ticks; /* when we became exhausted */ 158 int swap_pager_almost_full; /* swap space exhaustion (w/ hysteresis)*/ 159 swblk_t vm_swap_cache_use; 160 swblk_t vm_swap_anon_use; 161 static int vm_report_swap_allocs; 162 163 static struct krate kswaprate = { 1 }; 164 static int nsw_rcount; /* free read buffers */ 165 static int nsw_wcount_sync; /* limit write buffers / synchronous */ 166 static int nsw_wcount_async; /* limit write buffers / asynchronous */ 167 static int nsw_wcount_async_max;/* assigned maximum */ 168 static int nsw_cluster_max; /* maximum VOP I/O allowed */ 169 170 struct blist *swapblist; 171 static int swap_async_max = 4; /* maximum in-progress async I/O's */ 172 static int swap_burst_read = 0; /* allow burst reading */ 173 static swblk_t swapiterator; /* linearize allocations */ 174 int swap_user_async = 0; /* user swap pager operation can be async */ 175 176 static struct spinlock swapbp_spin = SPINLOCK_INITIALIZER(&swapbp_spin, "swapbp_spin"); 177 178 /* from vm_swap.c */ 179 extern struct vnode *swapdev_vp; 180 extern struct swdevt *swdevt; 181 extern int nswdev; 182 183 #define BLK2DEVIDX(blk) (nswdev > 1 ? blk / SWB_DMMAX % nswdev : 0) 184 185 SYSCTL_INT(_vm, OID_AUTO, swap_async_max, 186 CTLFLAG_RW, &swap_async_max, 0, "Maximum running async swap ops"); 187 SYSCTL_INT(_vm, OID_AUTO, swap_burst_read, 188 CTLFLAG_RW, &swap_burst_read, 0, "Allow burst reads for pageins"); 189 SYSCTL_INT(_vm, OID_AUTO, swap_user_async, 190 CTLFLAG_RW, &swap_user_async, 0, "Allow async uuser swap write I/O"); 191 192 #if SWBLK_BITS == 64 193 SYSCTL_LONG(_vm, OID_AUTO, swap_cache_use, 194 CTLFLAG_RD, &vm_swap_cache_use, 0, ""); 195 SYSCTL_LONG(_vm, OID_AUTO, swap_anon_use, 196 CTLFLAG_RD, &vm_swap_anon_use, 0, ""); 197 SYSCTL_LONG(_vm, OID_AUTO, swap_size, 198 CTLFLAG_RD, &vm_swap_size, 0, ""); 199 #else 200 SYSCTL_INT(_vm, OID_AUTO, swap_cache_use, 201 CTLFLAG_RD, &vm_swap_cache_use, 0, ""); 202 SYSCTL_INT(_vm, OID_AUTO, swap_anon_use, 203 CTLFLAG_RD, &vm_swap_anon_use, 0, ""); 204 SYSCTL_INT(_vm, OID_AUTO, swap_size, 205 CTLFLAG_RD, &vm_swap_size, 0, ""); 206 #endif 207 SYSCTL_INT(_vm, OID_AUTO, report_swap_allocs, 208 CTLFLAG_RW, &vm_report_swap_allocs, 0, ""); 209 210 vm_zone_t swap_zone; 211 212 /* 213 * Red-Black tree for swblock entries 214 * 215 * The caller must hold vm_token 216 */ 217 RB_GENERATE2(swblock_rb_tree, swblock, swb_entry, rb_swblock_compare, 218 vm_pindex_t, swb_index); 219 220 int 221 rb_swblock_compare(struct swblock *swb1, struct swblock *swb2) 222 { 223 if (swb1->swb_index < swb2->swb_index) 224 return(-1); 225 if (swb1->swb_index > swb2->swb_index) 226 return(1); 227 return(0); 228 } 229 230 static 231 int 232 rb_swblock_scancmp(struct swblock *swb, void *data) 233 { 234 struct swfreeinfo *info = data; 235 236 if (swb->swb_index < info->basei) 237 return(-1); 238 if (swb->swb_index > info->endi) 239 return(1); 240 return(0); 241 } 242 243 static 244 int 245 rb_swblock_condcmp(struct swblock *swb, void *data) 246 { 247 struct swfreeinfo *info = data; 248 249 if (swb->swb_index < info->basei) 250 return(-1); 251 return(0); 252 } 253 254 /* 255 * pagerops for OBJT_SWAP - "swap pager". Some ops are also global procedure 256 * calls hooked from other parts of the VM system and do not appear here. 257 * (see vm/swap_pager.h). 258 */ 259 260 static void swap_pager_dealloc (vm_object_t object); 261 static int swap_pager_getpage (vm_object_t, vm_page_t *, int); 262 static void swap_chain_iodone(struct bio *biox); 263 264 struct pagerops swappagerops = { 265 swap_pager_dealloc, /* deallocate an OBJT_SWAP object */ 266 swap_pager_getpage, /* pagein */ 267 swap_pager_putpages, /* pageout */ 268 swap_pager_haspage /* get backing store status for page */ 269 }; 270 271 /* 272 * SWB_DMMAX is in page-sized chunks with the new swap system. It was 273 * dev-bsized chunks in the old. SWB_DMMAX is always a power of 2. 274 * 275 * swap_*() routines are externally accessible. swp_*() routines are 276 * internal. 277 */ 278 279 int nswap_lowat = 128; /* in pages, swap_pager_almost_full warn */ 280 int nswap_hiwat = 512; /* in pages, swap_pager_almost_full warn */ 281 282 static __inline void swp_sizecheck (void); 283 static void swp_pager_async_iodone (struct bio *bio); 284 285 /* 286 * Swap bitmap functions 287 */ 288 289 static __inline void swp_pager_freeswapspace(vm_object_t object, 290 swblk_t blk, int npages); 291 static __inline swblk_t swp_pager_getswapspace(vm_object_t object, int npages); 292 293 /* 294 * Metadata functions 295 */ 296 297 static void swp_pager_meta_convert(vm_object_t); 298 static void swp_pager_meta_build(vm_object_t, vm_pindex_t, swblk_t); 299 static void swp_pager_meta_free(vm_object_t, vm_pindex_t, vm_pindex_t); 300 static void swp_pager_meta_free_all(vm_object_t); 301 static swblk_t swp_pager_meta_ctl(vm_object_t, vm_pindex_t, int); 302 303 /* 304 * SWP_SIZECHECK() - update swap_pager_full indication 305 * 306 * update the swap_pager_almost_full indication and warn when we are 307 * about to run out of swap space, using lowat/hiwat hysteresis. 308 * 309 * Clear swap_pager_full ( task killing ) indication when lowat is met. 310 * 311 * No restrictions on call 312 * This routine may not block. 313 * SMP races are ok. 314 */ 315 static __inline void 316 swp_sizecheck(void) 317 { 318 if (vm_swap_size < nswap_lowat) { 319 if (swap_pager_almost_full == 0) { 320 kprintf("swap_pager: out of swap space\n"); 321 swap_pager_almost_full = 1; 322 swap_fail_ticks = ticks; 323 } 324 } else { 325 swap_pager_full = 0; 326 if (vm_swap_size > nswap_hiwat) 327 swap_pager_almost_full = 0; 328 } 329 } 330 331 /* 332 * Long-term data collection on 10-second interval. Return the value 333 * for KCOLLECT_SWAPPCT and set the values for SWAPANO and SWAPCCAC. 334 * 335 * Return total swap in the scale field. This can change if swap is 336 * regularly added or removed and may cause some historical confusion 337 * in that case, but SWAPPCT will always be historically accurate. 338 */ 339 340 #define PTOB(value) ((uint64_t)(value) << PAGE_SHIFT) 341 342 static uint64_t 343 collect_swap_callback(int n) 344 { 345 uint64_t total = vm_swap_max; 346 uint64_t anon = vm_swap_anon_use; 347 uint64_t cache = vm_swap_cache_use; 348 349 if (total == 0) /* avoid divide by zero */ 350 total = 1; 351 kcollect_setvalue(KCOLLECT_SWAPANO, PTOB(anon)); 352 kcollect_setvalue(KCOLLECT_SWAPCAC, PTOB(cache)); 353 kcollect_setscale(KCOLLECT_SWAPANO, 354 KCOLLECT_SCALE(KCOLLECT_SWAPANO_FORMAT, PTOB(total))); 355 kcollect_setscale(KCOLLECT_SWAPCAC, 356 KCOLLECT_SCALE(KCOLLECT_SWAPCAC_FORMAT, PTOB(total))); 357 return (((anon + cache) * 10000 + (total >> 1)) / total); 358 } 359 360 /* 361 * SWAP_PAGER_INIT() - initialize the swap pager! 362 * 363 * Expected to be started from system init. NOTE: This code is run 364 * before much else so be careful what you depend on. Most of the VM 365 * system has yet to be initialized at this point. 366 * 367 * Called from the low level boot code only. 368 */ 369 static void 370 swap_pager_init(void *arg __unused) 371 { 372 kcollect_register(KCOLLECT_SWAPPCT, "swapuse", collect_swap_callback, 373 KCOLLECT_SCALE(KCOLLECT_SWAPPCT_FORMAT, 0)); 374 kcollect_register(KCOLLECT_SWAPANO, "swapano", NULL, 375 KCOLLECT_SCALE(KCOLLECT_SWAPANO_FORMAT, 0)); 376 kcollect_register(KCOLLECT_SWAPCAC, "swapcac", NULL, 377 KCOLLECT_SCALE(KCOLLECT_SWAPCAC_FORMAT, 0)); 378 } 379 SYSINIT(vm_mem, SI_BOOT1_VM, SI_ORDER_THIRD, swap_pager_init, NULL); 380 381 /* 382 * SWAP_PAGER_SWAP_INIT() - swap pager initialization from pageout process 383 * 384 * Expected to be started from pageout process once, prior to entering 385 * its main loop. 386 * 387 * Called from the low level boot code only. 388 */ 389 void 390 swap_pager_swap_init(void) 391 { 392 int n, n2; 393 394 /* 395 * Number of in-transit swap bp operations. Don't 396 * exhaust the pbufs completely. Make sure we 397 * initialize workable values (0 will work for hysteresis 398 * but it isn't very efficient). 399 * 400 * The nsw_cluster_max is constrained by the number of pages an XIO 401 * holds, i.e., (MAXPHYS/PAGE_SIZE) and our locally defined 402 * MAX_PAGEOUT_CLUSTER. Also be aware that swap ops are 403 * constrained by the swap device interleave stripe size. 404 * 405 * Currently we hardwire nsw_wcount_async to 4. This limit is 406 * designed to prevent other I/O from having high latencies due to 407 * our pageout I/O. The value 4 works well for one or two active swap 408 * devices but is probably a little low if you have more. Even so, 409 * a higher value would probably generate only a limited improvement 410 * with three or four active swap devices since the system does not 411 * typically have to pageout at extreme bandwidths. We will want 412 * at least 2 per swap devices, and 4 is a pretty good value if you 413 * have one NFS swap device due to the command/ack latency over NFS. 414 * So it all works out pretty well. 415 */ 416 417 nsw_cluster_max = min((MAXPHYS/PAGE_SIZE), MAX_PAGEOUT_CLUSTER); 418 419 nsw_rcount = (nswbuf_kva + 1) / 2; 420 nsw_wcount_sync = (nswbuf_kva + 3) / 4; 421 nsw_wcount_async = 4; 422 nsw_wcount_async_max = nsw_wcount_async; 423 424 /* 425 * The zone is dynamically allocated so generally size it to 426 * maxswzone (32MB to 256GB of KVM). Set a minimum size based 427 * on physical memory of around 8x (each swblock can hold 16 pages). 428 * 429 * With the advent of SSDs (vs HDs) the practical (swap:memory) ratio 430 * has increased dramatically. 431 */ 432 n = vmstats.v_page_count / 2; 433 if (maxswzone && n < maxswzone / sizeof(struct swblock)) 434 n = maxswzone / sizeof(struct swblock); 435 n2 = n; 436 437 do { 438 swap_zone = zinit( 439 "SWAPMETA", 440 sizeof(struct swblock), 441 n, 442 ZONE_INTERRUPT); 443 if (swap_zone != NULL) 444 break; 445 /* 446 * if the allocation failed, try a zone two thirds the 447 * size of the previous attempt. 448 */ 449 n -= ((n + 2) / 3); 450 } while (n > 0); 451 452 if (swap_zone == NULL) 453 panic("swap_pager_swap_init: swap_zone == NULL"); 454 if (n2 != n) 455 kprintf("Swap zone entries reduced from %d to %d.\n", n2, n); 456 } 457 458 /* 459 * SWAP_PAGER_ALLOC() - allocate a new OBJT_SWAP VM object and instantiate 460 * its metadata structures. 461 * 462 * This routine is called from the mmap and fork code to create a new 463 * OBJT_SWAP object. We do this by creating an OBJT_DEFAULT object 464 * and then converting it with swp_pager_meta_convert(). 465 * 466 * We only support unnamed objects. 467 * 468 * No restrictions. 469 */ 470 vm_object_t 471 swap_pager_alloc(void *handle, off_t size, vm_prot_t prot, off_t offset) 472 { 473 vm_object_t object; 474 475 KKASSERT(handle == NULL); 476 object = vm_object_allocate_hold(OBJT_DEFAULT, 477 OFF_TO_IDX(offset + PAGE_MASK + size)); 478 swp_pager_meta_convert(object); 479 vm_object_drop(object); 480 481 return (object); 482 } 483 484 /* 485 * SWAP_PAGER_DEALLOC() - remove swap metadata from object 486 * 487 * The swap backing for the object is destroyed. The code is 488 * designed such that we can reinstantiate it later, but this 489 * routine is typically called only when the entire object is 490 * about to be destroyed. 491 * 492 * The object must be locked or unreferenceable. 493 * No other requirements. 494 */ 495 static void 496 swap_pager_dealloc(vm_object_t object) 497 { 498 vm_object_hold(object); 499 vm_object_pip_wait(object, "swpdea"); 500 501 /* 502 * Free all remaining metadata. We only bother to free it from 503 * the swap meta data. We do not attempt to free swapblk's still 504 * associated with vm_page_t's for this object. We do not care 505 * if paging is still in progress on some objects. 506 */ 507 swp_pager_meta_free_all(object); 508 vm_object_drop(object); 509 } 510 511 /************************************************************************ 512 * SWAP PAGER BITMAP ROUTINES * 513 ************************************************************************/ 514 515 /* 516 * SWP_PAGER_GETSWAPSPACE() - allocate raw swap space 517 * 518 * Allocate swap for the requested number of pages. The starting 519 * swap block number (a page index) is returned or SWAPBLK_NONE 520 * if the allocation failed. 521 * 522 * Also has the side effect of advising that somebody made a mistake 523 * when they configured swap and didn't configure enough. 524 * 525 * The caller must hold the object. 526 * This routine may not block. 527 */ 528 static __inline swblk_t 529 swp_pager_getswapspace(vm_object_t object, int npages) 530 { 531 swblk_t blk; 532 533 lwkt_gettoken(&vm_token); 534 blk = blist_allocat(swapblist, npages, swapiterator); 535 if (blk == SWAPBLK_NONE) 536 blk = blist_allocat(swapblist, npages, 0); 537 if (blk == SWAPBLK_NONE) { 538 if (swap_pager_full != 2) { 539 if (vm_swap_max == 0) { 540 krateprintf(&kswaprate, 541 "Warning: The system would like to " 542 "page to swap but no swap space " 543 "is configured!\n"); 544 } else { 545 krateprintf(&kswaprate, 546 "swap_pager_getswapspace: " 547 "swap full allocating %d pages\n", 548 npages); 549 } 550 swap_pager_full = 2; 551 if (swap_pager_almost_full == 0) 552 swap_fail_ticks = ticks; 553 swap_pager_almost_full = 1; 554 } 555 } else { 556 /* swapiterator = blk; disable for now, doesn't work well */ 557 swapacctspace(blk, -npages); 558 if (object->type == OBJT_SWAP) 559 vm_swap_anon_use += npages; 560 else 561 vm_swap_cache_use += npages; 562 swp_sizecheck(); 563 } 564 lwkt_reltoken(&vm_token); 565 return(blk); 566 } 567 568 /* 569 * SWP_PAGER_FREESWAPSPACE() - free raw swap space 570 * 571 * This routine returns the specified swap blocks back to the bitmap. 572 * 573 * Note: This routine may not block (it could in the old swap code), 574 * and through the use of the new blist routines it does not block. 575 * 576 * This routine may not block. 577 */ 578 579 static __inline void 580 swp_pager_freeswapspace(vm_object_t object, swblk_t blk, int npages) 581 { 582 struct swdevt *sp = &swdevt[BLK2DEVIDX(blk)]; 583 584 lwkt_gettoken(&vm_token); 585 sp->sw_nused -= npages; 586 if (object->type == OBJT_SWAP) 587 vm_swap_anon_use -= npages; 588 else 589 vm_swap_cache_use -= npages; 590 591 if (sp->sw_flags & SW_CLOSING) { 592 lwkt_reltoken(&vm_token); 593 return; 594 } 595 596 blist_free(swapblist, blk, npages); 597 vm_swap_size += npages; 598 swp_sizecheck(); 599 lwkt_reltoken(&vm_token); 600 } 601 602 /* 603 * SWAP_PAGER_FREESPACE() - frees swap blocks associated with a page 604 * range within an object. 605 * 606 * This is a globally accessible routine. 607 * 608 * This routine removes swapblk assignments from swap metadata. 609 * 610 * The external callers of this routine typically have already destroyed 611 * or renamed vm_page_t's associated with this range in the object so 612 * we should be ok. 613 * 614 * No requirements. 615 */ 616 void 617 swap_pager_freespace(vm_object_t object, vm_pindex_t start, vm_pindex_t size) 618 { 619 vm_object_hold(object); 620 swp_pager_meta_free(object, start, size); 621 vm_object_drop(object); 622 } 623 624 /* 625 * No requirements. 626 */ 627 void 628 swap_pager_freespace_all(vm_object_t object) 629 { 630 vm_object_hold(object); 631 swp_pager_meta_free_all(object); 632 vm_object_drop(object); 633 } 634 635 /* 636 * This function conditionally frees swap cache swap starting at 637 * (*basei) in the object. (count) swap blocks will be nominally freed. 638 * The actual number of blocks freed can be more or less than the 639 * requested number. 640 * 641 * This function nominally returns the number of blocks freed. However, 642 * the actual number of blocks freed may be less then the returned value. 643 * If the function is unable to exhaust the object or if it is able to 644 * free (approximately) the requested number of blocks it returns 645 * a value n > count. 646 * 647 * If we exhaust the object we will return a value n <= count. 648 * 649 * The caller must hold the object. 650 * 651 * WARNING! If count == 0 then -1 can be returned as a degenerate case, 652 * callers should always pass a count value > 0. 653 */ 654 static int swap_pager_condfree_callback(struct swblock *swap, void *data); 655 656 int 657 swap_pager_condfree(vm_object_t object, vm_pindex_t *basei, int count) 658 { 659 struct swfreeinfo info; 660 int n; 661 int t; 662 663 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object)); 664 665 info.object = object; 666 info.basei = *basei; /* skip up to this page index */ 667 info.begi = count; /* max swap pages to destroy */ 668 info.endi = count * 8; /* max swblocks to scan */ 669 670 swblock_rb_tree_RB_SCAN(&object->swblock_root, rb_swblock_condcmp, 671 swap_pager_condfree_callback, &info); 672 *basei = info.basei; 673 674 /* 675 * Take the higher difference swblocks vs pages 676 */ 677 n = count - (int)info.begi; 678 t = count * 8 - (int)info.endi; 679 if (n < t) 680 n = t; 681 if (n < 1) 682 n = 1; 683 return(n); 684 } 685 686 /* 687 * The idea is to free whole meta-block to avoid fragmenting 688 * the swap space or disk I/O. We only do this if NO VM pages 689 * are present. 690 * 691 * We do not have to deal with clearing PG_SWAPPED in related VM 692 * pages because there are no related VM pages. 693 * 694 * The caller must hold the object. 695 */ 696 static int 697 swap_pager_condfree_callback(struct swblock *swap, void *data) 698 { 699 struct swfreeinfo *info = data; 700 vm_object_t object = info->object; 701 int i; 702 703 for (i = 0; i < SWAP_META_PAGES; ++i) { 704 if (vm_page_lookup(object, swap->swb_index + i)) 705 break; 706 } 707 info->basei = swap->swb_index + SWAP_META_PAGES; 708 if (i == SWAP_META_PAGES) { 709 info->begi -= swap->swb_count; 710 swap_pager_freespace(object, swap->swb_index, SWAP_META_PAGES); 711 } 712 --info->endi; 713 if ((int)info->begi < 0 || (int)info->endi < 0) 714 return(-1); 715 lwkt_yield(); 716 return(0); 717 } 718 719 /* 720 * Called by vm_page_alloc() when a new VM page is inserted 721 * into a VM object. Checks whether swap has been assigned to 722 * the page and sets PG_SWAPPED as necessary. 723 * 724 * (m) must be busied by caller and remains busied on return. 725 */ 726 void 727 swap_pager_page_inserted(vm_page_t m) 728 { 729 if (m->object->swblock_count) { 730 vm_object_hold(m->object); 731 if (swp_pager_meta_ctl(m->object, m->pindex, 0) != SWAPBLK_NONE) 732 vm_page_flag_set(m, PG_SWAPPED); 733 vm_object_drop(m->object); 734 } 735 } 736 737 /* 738 * SWAP_PAGER_RESERVE() - reserve swap blocks in object 739 * 740 * Assigns swap blocks to the specified range within the object. The 741 * swap blocks are not zerod. Any previous swap assignment is destroyed. 742 * 743 * Returns 0 on success, -1 on failure. 744 * 745 * The caller is responsible for avoiding races in the specified range. 746 * No other requirements. 747 */ 748 int 749 swap_pager_reserve(vm_object_t object, vm_pindex_t start, vm_size_t size) 750 { 751 int n = 0; 752 swblk_t blk = SWAPBLK_NONE; 753 vm_pindex_t beg = start; /* save start index */ 754 755 vm_object_hold(object); 756 757 while (size) { 758 if (n == 0) { 759 n = BLIST_MAX_ALLOC; 760 while ((blk = swp_pager_getswapspace(object, n)) == 761 SWAPBLK_NONE) 762 { 763 n >>= 1; 764 if (n == 0) { 765 swp_pager_meta_free(object, beg, 766 start - beg); 767 vm_object_drop(object); 768 return(-1); 769 } 770 } 771 } 772 swp_pager_meta_build(object, start, blk); 773 --size; 774 ++start; 775 ++blk; 776 --n; 777 } 778 swp_pager_meta_free(object, start, n); 779 vm_object_drop(object); 780 return(0); 781 } 782 783 /* 784 * SWAP_PAGER_COPY() - copy blocks from source pager to destination pager 785 * and destroy the source. 786 * 787 * Copy any valid swapblks from the source to the destination. In 788 * cases where both the source and destination have a valid swapblk, 789 * we keep the destination's. 790 * 791 * This routine is allowed to block. It may block allocating metadata 792 * indirectly through swp_pager_meta_build() or if paging is still in 793 * progress on the source. 794 * 795 * XXX vm_page_collapse() kinda expects us not to block because we 796 * supposedly do not need to allocate memory, but for the moment we 797 * *may* have to get a little memory from the zone allocator, but 798 * it is taken from the interrupt memory. We should be ok. 799 * 800 * The source object contains no vm_page_t's (which is just as well) 801 * The source object is of type OBJT_SWAP. 802 * 803 * The source and destination objects must be held by the caller. 804 */ 805 void 806 swap_pager_copy(vm_object_t srcobject, vm_object_t dstobject, 807 vm_pindex_t base_index, int destroysource) 808 { 809 vm_pindex_t i; 810 811 ASSERT_LWKT_TOKEN_HELD(vm_object_token(srcobject)); 812 ASSERT_LWKT_TOKEN_HELD(vm_object_token(dstobject)); 813 814 /* 815 * transfer source to destination. 816 */ 817 for (i = 0; i < dstobject->size; ++i) { 818 swblk_t dstaddr; 819 820 /* 821 * Locate (without changing) the swapblk on the destination, 822 * unless it is invalid in which case free it silently, or 823 * if the destination is a resident page, in which case the 824 * source is thrown away. 825 */ 826 dstaddr = swp_pager_meta_ctl(dstobject, i, 0); 827 828 if (dstaddr == SWAPBLK_NONE) { 829 /* 830 * Destination has no swapblk and is not resident, 831 * copy source. 832 */ 833 swblk_t srcaddr; 834 835 srcaddr = swp_pager_meta_ctl(srcobject, 836 base_index + i, SWM_POP); 837 838 if (srcaddr != SWAPBLK_NONE) 839 swp_pager_meta_build(dstobject, i, srcaddr); 840 } else { 841 /* 842 * Destination has valid swapblk or it is represented 843 * by a resident page. We destroy the sourceblock. 844 */ 845 swp_pager_meta_ctl(srcobject, base_index + i, SWM_FREE); 846 } 847 } 848 849 /* 850 * Free left over swap blocks in source. 851 * 852 * We have to revert the type to OBJT_DEFAULT so we do not accidently 853 * double-remove the object from the swap queues. 854 */ 855 if (destroysource) { 856 /* 857 * Reverting the type is not necessary, the caller is going 858 * to destroy srcobject directly, but I'm doing it here 859 * for consistency since we've removed the object from its 860 * queues. 861 */ 862 swp_pager_meta_free_all(srcobject); 863 if (srcobject->type == OBJT_SWAP) 864 srcobject->type = OBJT_DEFAULT; 865 } 866 } 867 868 /* 869 * SWAP_PAGER_HASPAGE() - determine if we have good backing store for 870 * the requested page. 871 * 872 * We determine whether good backing store exists for the requested 873 * page and return TRUE if it does, FALSE if it doesn't. 874 * 875 * If TRUE, we also try to determine how much valid, contiguous backing 876 * store exists before and after the requested page within a reasonable 877 * distance. We do not try to restrict it to the swap device stripe 878 * (that is handled in getpages/putpages). It probably isn't worth 879 * doing here. 880 * 881 * No requirements. 882 */ 883 boolean_t 884 swap_pager_haspage(vm_object_t object, vm_pindex_t pindex) 885 { 886 swblk_t blk0; 887 888 /* 889 * do we have good backing store at the requested index ? 890 */ 891 vm_object_hold(object); 892 blk0 = swp_pager_meta_ctl(object, pindex, 0); 893 894 if (blk0 == SWAPBLK_NONE) { 895 vm_object_drop(object); 896 return (FALSE); 897 } 898 vm_object_drop(object); 899 return (TRUE); 900 } 901 902 /* 903 * SWAP_PAGER_PAGE_UNSWAPPED() - remove swap backing store related to page 904 * 905 * This removes any associated swap backing store, whether valid or 906 * not, from the page. This operates on any VM object, not just OBJT_SWAP 907 * objects. 908 * 909 * This routine is typically called when a page is made dirty, at 910 * which point any associated swap can be freed. MADV_FREE also 911 * calls us in a special-case situation 912 * 913 * NOTE!!! If the page is clean and the swap was valid, the caller 914 * should make the page dirty before calling this routine. 915 * This routine does NOT change the m->dirty status of the page. 916 * Also: MADV_FREE depends on it. 917 * 918 * The page must be busied. 919 * The caller can hold the object to avoid blocking, else we might block. 920 * No other requirements. 921 */ 922 void 923 swap_pager_unswapped(vm_page_t m) 924 { 925 if (m->flags & PG_SWAPPED) { 926 vm_object_hold(m->object); 927 KKASSERT(m->flags & PG_SWAPPED); 928 swp_pager_meta_ctl(m->object, m->pindex, SWM_FREE); 929 vm_page_flag_clear(m, PG_SWAPPED); 930 vm_object_drop(m->object); 931 } 932 } 933 934 /* 935 * SWAP_PAGER_STRATEGY() - read, write, free blocks 936 * 937 * This implements a VM OBJECT strategy function using swap backing store. 938 * This can operate on any VM OBJECT type, not necessarily just OBJT_SWAP 939 * types. Only BUF_CMD_{READ,WRITE,FREEBLKS} is supported, any other 940 * requests will return EINVAL. 941 * 942 * This is intended to be a cacheless interface (i.e. caching occurs at 943 * higher levels), and is also used as a swap-based SSD cache for vnode 944 * and device objects. 945 * 946 * All I/O goes directly to and from the swap device. 947 * 948 * We currently attempt to run I/O synchronously or asynchronously as 949 * the caller requests. This isn't perfect because we loose error 950 * sequencing when we run multiple ops in parallel to satisfy a request. 951 * But this is swap, so we let it all hang out. 952 * 953 * NOTE: This function supports the KVABIO API wherein bp->b_data might 954 * not be synchronized to the current cpu. 955 * 956 * No requirements. 957 */ 958 void 959 swap_pager_strategy(vm_object_t object, struct bio *bio) 960 { 961 struct buf *bp = bio->bio_buf; 962 struct bio *nbio; 963 vm_pindex_t start; 964 vm_pindex_t biox_blkno = 0; 965 int count; 966 char *data; 967 struct bio *biox; 968 struct buf *bufx; 969 #if 0 970 struct bio_track *track; 971 #endif 972 973 #if 0 974 /* 975 * tracking for swapdev vnode I/Os 976 */ 977 if (bp->b_cmd == BUF_CMD_READ) 978 track = &swapdev_vp->v_track_read; 979 else 980 track = &swapdev_vp->v_track_write; 981 #endif 982 983 /* 984 * Only supported commands 985 */ 986 if (bp->b_cmd != BUF_CMD_FREEBLKS && 987 bp->b_cmd != BUF_CMD_READ && 988 bp->b_cmd != BUF_CMD_WRITE) { 989 bp->b_error = EINVAL; 990 bp->b_flags |= B_ERROR | B_INVAL; 991 biodone(bio); 992 return; 993 } 994 995 /* 996 * bcount must be an integral number of pages. 997 */ 998 if (bp->b_bcount & PAGE_MASK) { 999 bp->b_error = EINVAL; 1000 bp->b_flags |= B_ERROR | B_INVAL; 1001 biodone(bio); 1002 kprintf("swap_pager_strategy: bp %p offset %lld size %d, " 1003 "not page bounded\n", 1004 bp, (long long)bio->bio_offset, (int)bp->b_bcount); 1005 return; 1006 } 1007 1008 /* 1009 * Clear error indication, initialize page index, count, data pointer. 1010 */ 1011 bp->b_error = 0; 1012 bp->b_flags &= ~B_ERROR; 1013 bp->b_resid = bp->b_bcount; 1014 1015 start = (vm_pindex_t)(bio->bio_offset >> PAGE_SHIFT); 1016 count = howmany(bp->b_bcount, PAGE_SIZE); 1017 1018 /* 1019 * WARNING! Do not dereference *data without issuing a bkvasync() 1020 */ 1021 data = bp->b_data; 1022 1023 /* 1024 * Deal with BUF_CMD_FREEBLKS 1025 */ 1026 if (bp->b_cmd == BUF_CMD_FREEBLKS) { 1027 /* 1028 * FREE PAGE(s) - destroy underlying swap that is no longer 1029 * needed. 1030 */ 1031 vm_object_hold(object); 1032 swp_pager_meta_free(object, start, count); 1033 vm_object_drop(object); 1034 bp->b_resid = 0; 1035 biodone(bio); 1036 return; 1037 } 1038 1039 /* 1040 * We need to be able to create a new cluster of I/O's. We cannot 1041 * use the caller fields of the passed bio so push a new one. 1042 * 1043 * Because nbio is just a placeholder for the cluster links, 1044 * we can biodone() the original bio instead of nbio to make 1045 * things a bit more efficient. 1046 */ 1047 nbio = push_bio(bio); 1048 nbio->bio_offset = bio->bio_offset; 1049 nbio->bio_caller_info1.cluster_head = NULL; 1050 nbio->bio_caller_info2.cluster_tail = NULL; 1051 1052 biox = NULL; 1053 bufx = NULL; 1054 1055 /* 1056 * Execute read or write 1057 */ 1058 vm_object_hold(object); 1059 1060 while (count > 0) { 1061 swblk_t blk; 1062 1063 /* 1064 * Obtain block. If block not found and writing, allocate a 1065 * new block and build it into the object. 1066 */ 1067 blk = swp_pager_meta_ctl(object, start, 0); 1068 if ((blk == SWAPBLK_NONE) && bp->b_cmd == BUF_CMD_WRITE) { 1069 blk = swp_pager_getswapspace(object, 1); 1070 if (blk == SWAPBLK_NONE) { 1071 bp->b_error = ENOMEM; 1072 bp->b_flags |= B_ERROR; 1073 break; 1074 } 1075 swp_pager_meta_build(object, start, blk); 1076 } 1077 1078 /* 1079 * Do we have to flush our current collection? Yes if: 1080 * 1081 * - no swap block at this index 1082 * - swap block is not contiguous 1083 * - we cross a physical disk boundry in the 1084 * stripe. 1085 */ 1086 if (biox && 1087 (biox_blkno + btoc(bufx->b_bcount) != blk || 1088 ((biox_blkno ^ blk) & ~SWB_DMMASK))) { 1089 switch(bp->b_cmd) { 1090 case BUF_CMD_READ: 1091 ++mycpu->gd_cnt.v_swapin; 1092 mycpu->gd_cnt.v_swappgsin += 1093 btoc(bufx->b_bcount); 1094 break; 1095 case BUF_CMD_WRITE: 1096 ++mycpu->gd_cnt.v_swapout; 1097 mycpu->gd_cnt.v_swappgsout += 1098 btoc(bufx->b_bcount); 1099 bufx->b_dirtyend = bufx->b_bcount; 1100 break; 1101 default: 1102 /* NOT REACHED */ 1103 break; 1104 } 1105 1106 /* 1107 * Finished with this buf. 1108 */ 1109 KKASSERT(bufx->b_bcount != 0); 1110 if (bufx->b_cmd != BUF_CMD_READ) 1111 bufx->b_dirtyend = bufx->b_bcount; 1112 biox = NULL; 1113 bufx = NULL; 1114 } 1115 1116 /* 1117 * Add new swapblk to biox, instantiating biox if necessary. 1118 * Zero-fill reads are able to take a shortcut. 1119 */ 1120 if (blk == SWAPBLK_NONE) { 1121 /* 1122 * We can only get here if we are reading. 1123 */ 1124 bkvasync(bp); 1125 bzero(data, PAGE_SIZE); 1126 bp->b_resid -= PAGE_SIZE; 1127 } else { 1128 if (biox == NULL) { 1129 /* XXX chain count > 4, wait to <= 4 */ 1130 1131 bufx = getpbuf(NULL); 1132 bufx->b_flags |= B_KVABIO; 1133 biox = &bufx->b_bio1; 1134 cluster_append(nbio, bufx); 1135 bufx->b_cmd = bp->b_cmd; 1136 biox->bio_done = swap_chain_iodone; 1137 biox->bio_offset = (off_t)blk << PAGE_SHIFT; 1138 biox->bio_caller_info1.cluster_parent = nbio; 1139 biox_blkno = blk; 1140 bufx->b_bcount = 0; 1141 bufx->b_data = data; 1142 } 1143 bufx->b_bcount += PAGE_SIZE; 1144 } 1145 --count; 1146 ++start; 1147 data += PAGE_SIZE; 1148 } 1149 1150 vm_object_drop(object); 1151 1152 /* 1153 * Flush out last buffer 1154 */ 1155 if (biox) { 1156 if (bufx->b_cmd == BUF_CMD_READ) { 1157 ++mycpu->gd_cnt.v_swapin; 1158 mycpu->gd_cnt.v_swappgsin += btoc(bufx->b_bcount); 1159 } else { 1160 ++mycpu->gd_cnt.v_swapout; 1161 mycpu->gd_cnt.v_swappgsout += btoc(bufx->b_bcount); 1162 bufx->b_dirtyend = bufx->b_bcount; 1163 } 1164 KKASSERT(bufx->b_bcount); 1165 if (bufx->b_cmd != BUF_CMD_READ) 1166 bufx->b_dirtyend = bufx->b_bcount; 1167 /* biox, bufx = NULL */ 1168 } 1169 1170 /* 1171 * Now initiate all the I/O. Be careful looping on our chain as 1172 * I/O's may complete while we are still initiating them. 1173 * 1174 * If the request is a 100% sparse read no bios will be present 1175 * and we just biodone() the buffer. 1176 */ 1177 nbio->bio_caller_info2.cluster_tail = NULL; 1178 bufx = nbio->bio_caller_info1.cluster_head; 1179 1180 if (bufx) { 1181 while (bufx) { 1182 biox = &bufx->b_bio1; 1183 BUF_KERNPROC(bufx); 1184 bufx = bufx->b_cluster_next; 1185 vn_strategy(swapdev_vp, biox); 1186 } 1187 } else { 1188 biodone(bio); 1189 } 1190 1191 /* 1192 * Completion of the cluster will also call biodone_chain(nbio). 1193 * We never call biodone(nbio) so we don't have to worry about 1194 * setting up a bio_done callback. It's handled in the sub-IO. 1195 */ 1196 /**/ 1197 } 1198 1199 /* 1200 * biodone callback 1201 * 1202 * No requirements. 1203 */ 1204 static void 1205 swap_chain_iodone(struct bio *biox) 1206 { 1207 struct buf **nextp; 1208 struct buf *bufx; /* chained sub-buffer */ 1209 struct bio *nbio; /* parent nbio with chain glue */ 1210 struct buf *bp; /* original bp associated with nbio */ 1211 int chain_empty; 1212 1213 bufx = biox->bio_buf; 1214 nbio = biox->bio_caller_info1.cluster_parent; 1215 bp = nbio->bio_buf; 1216 1217 /* 1218 * Update the original buffer 1219 */ 1220 KKASSERT(bp != NULL); 1221 if (bufx->b_flags & B_ERROR) { 1222 atomic_set_int(&bufx->b_flags, B_ERROR); 1223 bp->b_error = bufx->b_error; /* race ok */ 1224 } else if (bufx->b_resid != 0) { 1225 atomic_set_int(&bufx->b_flags, B_ERROR); 1226 bp->b_error = EINVAL; /* race ok */ 1227 } else { 1228 atomic_subtract_int(&bp->b_resid, bufx->b_bcount); 1229 } 1230 1231 /* 1232 * Remove us from the chain. 1233 */ 1234 spin_lock(&swapbp_spin); 1235 nextp = &nbio->bio_caller_info1.cluster_head; 1236 while (*nextp != bufx) { 1237 KKASSERT(*nextp != NULL); 1238 nextp = &(*nextp)->b_cluster_next; 1239 } 1240 *nextp = bufx->b_cluster_next; 1241 chain_empty = (nbio->bio_caller_info1.cluster_head == NULL); 1242 spin_unlock(&swapbp_spin); 1243 1244 /* 1245 * Clean up bufx. If the chain is now empty we finish out 1246 * the parent. Note that we may be racing other completions 1247 * so we must use the chain_empty status from above. 1248 */ 1249 if (chain_empty) { 1250 if (bp->b_resid != 0 && !(bp->b_flags & B_ERROR)) { 1251 atomic_set_int(&bp->b_flags, B_ERROR); 1252 bp->b_error = EINVAL; 1253 } 1254 biodone_chain(nbio); 1255 } 1256 relpbuf(bufx, NULL); 1257 } 1258 1259 /* 1260 * SWAP_PAGER_GETPAGES() - bring page in from swap 1261 * 1262 * The requested page may have to be brought in from swap. Calculate the 1263 * swap block and bring in additional pages if possible. All pages must 1264 * have contiguous swap block assignments and reside in the same object. 1265 * 1266 * The caller has a single vm_object_pip_add() reference prior to 1267 * calling us and we should return with the same. 1268 * 1269 * The caller has BUSY'd the page. We should return with (*mpp) left busy, 1270 * and any additinal pages unbusied. 1271 * 1272 * If the caller encounters a PG_RAM page it will pass it to us even though 1273 * it may be valid and dirty. We cannot overwrite the page in this case! 1274 * The case is used to allow us to issue pure read-aheads. 1275 * 1276 * NOTE! XXX This code does not entirely pipeline yet due to the fact that 1277 * the PG_RAM page is validated at the same time as mreq. What we 1278 * really need to do is issue a separate read-ahead pbuf. 1279 * 1280 * No requirements. 1281 */ 1282 static int 1283 swap_pager_getpage(vm_object_t object, vm_page_t *mpp, int seqaccess) 1284 { 1285 struct buf *bp; 1286 struct bio *bio; 1287 vm_page_t mreq; 1288 vm_page_t m; 1289 vm_offset_t kva; 1290 swblk_t blk; 1291 int i; 1292 int j; 1293 int raonly; 1294 int error; 1295 u_int32_t busy_count; 1296 vm_page_t marray[XIO_INTERNAL_PAGES]; 1297 1298 mreq = *mpp; 1299 1300 vm_object_hold(object); 1301 if (mreq->object != object) { 1302 panic("swap_pager_getpages: object mismatch %p/%p", 1303 object, 1304 mreq->object 1305 ); 1306 } 1307 1308 /* 1309 * We don't want to overwrite a fully valid page as it might be 1310 * dirty. This case can occur when e.g. vm_fault hits a perfectly 1311 * valid page with PG_RAM set. 1312 * 1313 * In this case we see if the next page is a suitable page-in 1314 * candidate and if it is we issue read-ahead. PG_RAM will be 1315 * set on the last page of the read-ahead to continue the pipeline. 1316 */ 1317 if (mreq->valid == VM_PAGE_BITS_ALL) { 1318 if (swap_burst_read == 0 || mreq->pindex + 1 >= object->size) { 1319 vm_object_drop(object); 1320 return(VM_PAGER_OK); 1321 } 1322 blk = swp_pager_meta_ctl(object, mreq->pindex + 1, 0); 1323 if (blk == SWAPBLK_NONE) { 1324 vm_object_drop(object); 1325 return(VM_PAGER_OK); 1326 } 1327 m = vm_page_lookup_busy_try(object, mreq->pindex + 1, 1328 TRUE, &error); 1329 if (error) { 1330 vm_object_drop(object); 1331 return(VM_PAGER_OK); 1332 } else if (m == NULL) { 1333 /* 1334 * Use VM_ALLOC_QUICK to avoid blocking on cache 1335 * page reuse. 1336 */ 1337 m = vm_page_alloc(object, mreq->pindex + 1, 1338 VM_ALLOC_QUICK); 1339 if (m == NULL) { 1340 vm_object_drop(object); 1341 return(VM_PAGER_OK); 1342 } 1343 } else { 1344 if (m->valid) { 1345 vm_page_wakeup(m); 1346 vm_object_drop(object); 1347 return(VM_PAGER_OK); 1348 } 1349 vm_page_unqueue_nowakeup(m); 1350 } 1351 /* page is busy */ 1352 mreq = m; 1353 raonly = 1; 1354 } else { 1355 raonly = 0; 1356 } 1357 1358 /* 1359 * Try to block-read contiguous pages from swap if sequential, 1360 * otherwise just read one page. Contiguous pages from swap must 1361 * reside within a single device stripe because the I/O cannot be 1362 * broken up across multiple stripes. 1363 * 1364 * Note that blk and iblk can be SWAPBLK_NONE but the loop is 1365 * set up such that the case(s) are handled implicitly. 1366 */ 1367 blk = swp_pager_meta_ctl(mreq->object, mreq->pindex, 0); 1368 marray[0] = mreq; 1369 1370 for (i = 1; i <= swap_burst_read && 1371 i < XIO_INTERNAL_PAGES && 1372 mreq->pindex + i < object->size; ++i) { 1373 swblk_t iblk; 1374 1375 iblk = swp_pager_meta_ctl(object, mreq->pindex + i, 0); 1376 if (iblk != blk + i) 1377 break; 1378 if ((blk ^ iblk) & ~SWB_DMMASK) 1379 break; 1380 m = vm_page_lookup_busy_try(object, mreq->pindex + i, 1381 TRUE, &error); 1382 if (error) { 1383 break; 1384 } else if (m == NULL) { 1385 /* 1386 * Use VM_ALLOC_QUICK to avoid blocking on cache 1387 * page reuse. 1388 */ 1389 m = vm_page_alloc(object, mreq->pindex + i, 1390 VM_ALLOC_QUICK); 1391 if (m == NULL) 1392 break; 1393 } else { 1394 if (m->valid) { 1395 vm_page_wakeup(m); 1396 break; 1397 } 1398 vm_page_unqueue_nowakeup(m); 1399 } 1400 /* page is busy */ 1401 marray[i] = m; 1402 } 1403 if (i > 1) 1404 vm_page_flag_set(marray[i - 1], PG_RAM); 1405 1406 /* 1407 * If mreq is the requested page and we have nothing to do return 1408 * VM_PAGER_FAIL. If raonly is set mreq is just another read-ahead 1409 * page and must be cleaned up. 1410 */ 1411 if (blk == SWAPBLK_NONE) { 1412 KKASSERT(i == 1); 1413 if (raonly) { 1414 vnode_pager_freepage(mreq); 1415 vm_object_drop(object); 1416 return(VM_PAGER_OK); 1417 } else { 1418 vm_object_drop(object); 1419 return(VM_PAGER_FAIL); 1420 } 1421 } 1422 1423 /* 1424 * Map our page(s) into kva for input 1425 * 1426 * Use the KVABIO API to avoid synchronizing the pmap. 1427 */ 1428 bp = getpbuf_kva(&nsw_rcount); 1429 bio = &bp->b_bio1; 1430 kva = (vm_offset_t) bp->b_kvabase; 1431 bcopy(marray, bp->b_xio.xio_pages, i * sizeof(vm_page_t)); 1432 pmap_qenter_noinval(kva, bp->b_xio.xio_pages, i); 1433 1434 bp->b_data = (caddr_t)kva; 1435 bp->b_bcount = PAGE_SIZE * i; 1436 bp->b_xio.xio_npages = i; 1437 bp->b_flags |= B_KVABIO; 1438 bio->bio_done = swp_pager_async_iodone; 1439 bio->bio_offset = (off_t)blk << PAGE_SHIFT; 1440 bio->bio_caller_info1.index = SWBIO_READ; 1441 1442 /* 1443 * Set index. If raonly set the index beyond the array so all 1444 * the pages are treated the same, otherwise the original mreq is 1445 * at index 0. 1446 */ 1447 if (raonly) 1448 bio->bio_driver_info = (void *)(intptr_t)i; 1449 else 1450 bio->bio_driver_info = (void *)(intptr_t)0; 1451 1452 for (j = 0; j < i; ++j) { 1453 atomic_set_int(&bp->b_xio.xio_pages[j]->busy_count, 1454 PBUSY_SWAPINPROG); 1455 } 1456 1457 mycpu->gd_cnt.v_swapin++; 1458 mycpu->gd_cnt.v_swappgsin += bp->b_xio.xio_npages; 1459 1460 /* 1461 * We still hold the lock on mreq, and our automatic completion routine 1462 * does not remove it. 1463 */ 1464 vm_object_pip_add(object, bp->b_xio.xio_npages); 1465 1466 /* 1467 * perform the I/O. NOTE!!! bp cannot be considered valid after 1468 * this point because we automatically release it on completion. 1469 * Instead, we look at the one page we are interested in which we 1470 * still hold a lock on even through the I/O completion. 1471 * 1472 * The other pages in our m[] array are also released on completion, 1473 * so we cannot assume they are valid anymore either. 1474 */ 1475 bp->b_cmd = BUF_CMD_READ; 1476 BUF_KERNPROC(bp); 1477 vn_strategy(swapdev_vp, bio); 1478 1479 /* 1480 * Wait for the page we want to complete. PBUSY_SWAPINPROG is always 1481 * cleared on completion. If an I/O error occurs, SWAPBLK_NONE 1482 * is set in the meta-data. 1483 * 1484 * If this is a read-ahead only we return immediately without 1485 * waiting for I/O. 1486 */ 1487 if (raonly) { 1488 vm_object_drop(object); 1489 return(VM_PAGER_OK); 1490 } 1491 1492 /* 1493 * Read-ahead includes originally requested page case. 1494 */ 1495 for (;;) { 1496 busy_count = mreq->busy_count; 1497 cpu_ccfence(); 1498 if ((busy_count & PBUSY_SWAPINPROG) == 0) 1499 break; 1500 tsleep_interlock(mreq, 0); 1501 if (!atomic_cmpset_int(&mreq->busy_count, busy_count, 1502 busy_count | 1503 PBUSY_SWAPINPROG | PBUSY_WANTED)) { 1504 continue; 1505 } 1506 atomic_set_int(&mreq->flags, PG_REFERENCED); 1507 mycpu->gd_cnt.v_intrans++; 1508 if (tsleep(mreq, PINTERLOCKED, "swread", hz*20)) { 1509 kprintf( 1510 "swap_pager: indefinite wait buffer: " 1511 " bp %p offset: %lld, size: %ld\n", 1512 bp, 1513 (long long)bio->bio_offset, 1514 (long)bp->b_bcount 1515 ); 1516 } 1517 } 1518 1519 /* 1520 * Disallow speculative reads prior to the SWAPINPROG test. 1521 */ 1522 cpu_lfence(); 1523 1524 /* 1525 * mreq is left busied after completion, but all the other pages 1526 * are freed. If we had an unrecoverable read error the page will 1527 * not be valid. 1528 */ 1529 vm_object_drop(object); 1530 if (mreq->valid != VM_PAGE_BITS_ALL) 1531 return(VM_PAGER_ERROR); 1532 else 1533 return(VM_PAGER_OK); 1534 1535 /* 1536 * A final note: in a low swap situation, we cannot deallocate swap 1537 * and mark a page dirty here because the caller is likely to mark 1538 * the page clean when we return, causing the page to possibly revert 1539 * to all-zero's later. 1540 */ 1541 } 1542 1543 /* 1544 * swap_pager_putpages: 1545 * 1546 * Assign swap (if necessary) and initiate I/O on the specified pages. 1547 * 1548 * We support both OBJT_DEFAULT and OBJT_SWAP objects. DEFAULT objects 1549 * are automatically converted to SWAP objects. 1550 * 1551 * In a low memory situation we may block in vn_strategy(), but the new 1552 * vm_page reservation system coupled with properly written VFS devices 1553 * should ensure that no low-memory deadlock occurs. This is an area 1554 * which needs work. 1555 * 1556 * The parent has N vm_object_pip_add() references prior to 1557 * calling us and will remove references for rtvals[] that are 1558 * not set to VM_PAGER_PEND. We need to remove the rest on I/O 1559 * completion. 1560 * 1561 * The parent has soft-busy'd the pages it passes us and will unbusy 1562 * those whos rtvals[] entry is not set to VM_PAGER_PEND on return. 1563 * We need to unbusy the rest on I/O completion. 1564 * 1565 * No requirements. 1566 */ 1567 void 1568 swap_pager_putpages(vm_object_t object, vm_page_t *m, int count, 1569 int flags, int *rtvals) 1570 { 1571 int i; 1572 int n = 0; 1573 1574 vm_object_hold(object); 1575 1576 if (count && m[0]->object != object) { 1577 panic("swap_pager_getpages: object mismatch %p/%p", 1578 object, 1579 m[0]->object 1580 ); 1581 } 1582 1583 /* 1584 * Step 1 1585 * 1586 * Turn object into OBJT_SWAP 1587 * Check for bogus sysops 1588 * 1589 * Force sync if not pageout process, we don't want any single 1590 * non-pageout process to be able to hog the I/O subsystem! This 1591 * can be overridden by setting. 1592 */ 1593 if (object->type == OBJT_DEFAULT) { 1594 if (object->type == OBJT_DEFAULT) 1595 swp_pager_meta_convert(object); 1596 } 1597 1598 /* 1599 * Normally we force synchronous swap I/O if this is not the 1600 * pageout daemon to prevent any single user process limited 1601 * via RLIMIT_RSS from hogging swap write bandwidth. 1602 */ 1603 if (curthread != pagethread && 1604 curthread != emergpager && 1605 swap_user_async == 0) { 1606 flags |= VM_PAGER_PUT_SYNC; 1607 } 1608 1609 /* 1610 * Step 2 1611 * 1612 * Update nsw parameters from swap_async_max sysctl values. 1613 * Do not let the sysop crash the machine with bogus numbers. 1614 */ 1615 if (swap_async_max != nsw_wcount_async_max) { 1616 int n; 1617 1618 /* 1619 * limit range 1620 */ 1621 if ((n = swap_async_max) > nswbuf_kva / 2) 1622 n = nswbuf_kva / 2; 1623 if (n < 1) 1624 n = 1; 1625 swap_async_max = n; 1626 1627 /* 1628 * Adjust difference ( if possible ). If the current async 1629 * count is too low, we may not be able to make the adjustment 1630 * at this time. 1631 * 1632 * vm_token needed for nsw_wcount sleep interlock 1633 */ 1634 lwkt_gettoken(&vm_token); 1635 n -= nsw_wcount_async_max; 1636 if (nsw_wcount_async + n >= 0) { 1637 nsw_wcount_async_max += n; 1638 pbuf_adjcount(&nsw_wcount_async, n); 1639 } 1640 lwkt_reltoken(&vm_token); 1641 } 1642 1643 /* 1644 * Step 3 1645 * 1646 * Assign swap blocks and issue I/O. We reallocate swap on the fly. 1647 * The page is left dirty until the pageout operation completes 1648 * successfully. 1649 */ 1650 1651 for (i = 0; i < count; i += n) { 1652 struct buf *bp; 1653 struct bio *bio; 1654 swblk_t blk; 1655 int j; 1656 1657 /* 1658 * Maximum I/O size is limited by a number of factors. 1659 */ 1660 1661 n = min(BLIST_MAX_ALLOC, count - i); 1662 n = min(n, nsw_cluster_max); 1663 1664 lwkt_gettoken(&vm_token); 1665 1666 /* 1667 * Get biggest block of swap we can. If we fail, fall 1668 * back and try to allocate a smaller block. Don't go 1669 * overboard trying to allocate space if it would overly 1670 * fragment swap. 1671 */ 1672 while ( 1673 (blk = swp_pager_getswapspace(object, n)) == SWAPBLK_NONE && 1674 n > 4 1675 ) { 1676 n >>= 1; 1677 } 1678 if (blk == SWAPBLK_NONE) { 1679 for (j = 0; j < n; ++j) 1680 rtvals[i+j] = VM_PAGER_FAIL; 1681 lwkt_reltoken(&vm_token); 1682 continue; 1683 } 1684 if (vm_report_swap_allocs > 0) { 1685 kprintf("swap_alloc %08jx,%d\n", (intmax_t)blk, n); 1686 --vm_report_swap_allocs; 1687 } 1688 1689 /* 1690 * The I/O we are constructing cannot cross a physical 1691 * disk boundry in the swap stripe. 1692 */ 1693 if ((blk ^ (blk + n)) & ~SWB_DMMASK) { 1694 j = ((blk + SWB_DMMAX) & ~SWB_DMMASK) - blk; 1695 swp_pager_freeswapspace(object, blk + j, n - j); 1696 n = j; 1697 } 1698 1699 /* 1700 * All I/O parameters have been satisfied, build the I/O 1701 * request and assign the swap space. 1702 * 1703 * Use the KVABIO API to avoid synchronizing the pmap. 1704 */ 1705 if ((flags & VM_PAGER_PUT_SYNC)) 1706 bp = getpbuf_kva(&nsw_wcount_sync); 1707 else 1708 bp = getpbuf_kva(&nsw_wcount_async); 1709 bio = &bp->b_bio1; 1710 1711 lwkt_reltoken(&vm_token); 1712 1713 pmap_qenter_noinval((vm_offset_t)bp->b_data, &m[i], n); 1714 1715 bp->b_flags |= B_KVABIO; 1716 bp->b_bcount = PAGE_SIZE * n; 1717 bio->bio_offset = (off_t)blk << PAGE_SHIFT; 1718 1719 for (j = 0; j < n; ++j) { 1720 vm_page_t mreq = m[i+j]; 1721 1722 swp_pager_meta_build(mreq->object, mreq->pindex, 1723 blk + j); 1724 if (object->type == OBJT_SWAP) 1725 vm_page_dirty(mreq); 1726 rtvals[i+j] = VM_PAGER_OK; 1727 1728 atomic_set_int(&mreq->busy_count, PBUSY_SWAPINPROG); 1729 bp->b_xio.xio_pages[j] = mreq; 1730 } 1731 bp->b_xio.xio_npages = n; 1732 1733 mycpu->gd_cnt.v_swapout++; 1734 mycpu->gd_cnt.v_swappgsout += bp->b_xio.xio_npages; 1735 1736 bp->b_dirtyoff = 0; /* req'd for NFS */ 1737 bp->b_dirtyend = bp->b_bcount; /* req'd for NFS */ 1738 bp->b_cmd = BUF_CMD_WRITE; 1739 bio->bio_caller_info1.index = SWBIO_WRITE; 1740 1741 /* 1742 * asynchronous 1743 */ 1744 if ((flags & VM_PAGER_PUT_SYNC) == 0) { 1745 bio->bio_done = swp_pager_async_iodone; 1746 BUF_KERNPROC(bp); 1747 vn_strategy(swapdev_vp, bio); 1748 1749 for (j = 0; j < n; ++j) 1750 rtvals[i+j] = VM_PAGER_PEND; 1751 continue; 1752 } 1753 1754 /* 1755 * Issue synchrnously. 1756 * 1757 * Wait for the sync I/O to complete, then update rtvals. 1758 * We just set the rtvals[] to VM_PAGER_PEND so we can call 1759 * our async completion routine at the end, thus avoiding a 1760 * double-free. 1761 */ 1762 bio->bio_caller_info1.index |= SWBIO_SYNC; 1763 if (flags & VM_PAGER_TRY_TO_CACHE) 1764 bio->bio_caller_info1.index |= SWBIO_TTC; 1765 bio->bio_done = biodone_sync; 1766 bio->bio_flags |= BIO_SYNC; 1767 vn_strategy(swapdev_vp, bio); 1768 biowait(bio, "swwrt"); 1769 1770 for (j = 0; j < n; ++j) 1771 rtvals[i+j] = VM_PAGER_PEND; 1772 1773 /* 1774 * Now that we are through with the bp, we can call the 1775 * normal async completion, which frees everything up. 1776 */ 1777 swp_pager_async_iodone(bio); 1778 } 1779 vm_object_drop(object); 1780 } 1781 1782 /* 1783 * No requirements. 1784 * 1785 * Recalculate the low and high-water marks. 1786 */ 1787 void 1788 swap_pager_newswap(void) 1789 { 1790 /* 1791 * NOTE: vm_swap_max cannot exceed 1 billion blocks, which is the 1792 * limitation imposed by the blist code. Remember that this 1793 * will be divided by NSWAP_MAX (4), so each swap device is 1794 * limited to around a terrabyte. 1795 */ 1796 if (vm_swap_max) { 1797 nswap_lowat = (int64_t)vm_swap_max * 4 / 100; /* 4% left */ 1798 nswap_hiwat = (int64_t)vm_swap_max * 6 / 100; /* 6% left */ 1799 kprintf("swap low/high-water marks set to %d/%d\n", 1800 nswap_lowat, nswap_hiwat); 1801 } else { 1802 nswap_lowat = 128; 1803 nswap_hiwat = 512; 1804 } 1805 swp_sizecheck(); 1806 } 1807 1808 /* 1809 * swp_pager_async_iodone: 1810 * 1811 * Completion routine for asynchronous reads and writes from/to swap. 1812 * Also called manually by synchronous code to finish up a bp. 1813 * 1814 * For READ operations, the pages are BUSY'd. For WRITE operations, 1815 * the pages are vm_page_t->busy'd. For READ operations, we BUSY 1816 * unbusy all pages except the 'main' request page. For WRITE 1817 * operations, we vm_page_t->busy'd unbusy all pages ( we can do this 1818 * because we marked them all VM_PAGER_PEND on return from putpages ). 1819 * 1820 * This routine may not block. 1821 * 1822 * No requirements. 1823 */ 1824 static void 1825 swp_pager_async_iodone(struct bio *bio) 1826 { 1827 struct buf *bp = bio->bio_buf; 1828 vm_object_t object = NULL; 1829 int i; 1830 int *nswptr; 1831 1832 /* 1833 * report error 1834 */ 1835 if (bp->b_flags & B_ERROR) { 1836 kprintf( 1837 "swap_pager: I/O error - %s failed; offset %lld," 1838 "size %ld, error %d\n", 1839 ((bio->bio_caller_info1.index & SWBIO_READ) ? 1840 "pagein" : "pageout"), 1841 (long long)bio->bio_offset, 1842 (long)bp->b_bcount, 1843 bp->b_error 1844 ); 1845 } 1846 1847 /* 1848 * set object. 1849 */ 1850 if (bp->b_xio.xio_npages) 1851 object = bp->b_xio.xio_pages[0]->object; 1852 1853 #if 0 1854 /* PMAP TESTING CODE (useful, keep it in but #if 0'd) */ 1855 if (bio->bio_caller_info1.index & SWBIO_WRITE) { 1856 if (bio->bio_crc != iscsi_crc32(bp->b_data, bp->b_bcount)) { 1857 kprintf("SWAPOUT: BADCRC %08x %08x\n", 1858 bio->bio_crc, 1859 iscsi_crc32(bp->b_data, bp->b_bcount)); 1860 for (i = 0; i < bp->b_xio.xio_npages; ++i) { 1861 vm_page_t m = bp->b_xio.xio_pages[i]; 1862 if (m->flags & PG_WRITEABLE) 1863 kprintf("SWAPOUT: " 1864 "%d/%d %p writable\n", 1865 i, bp->b_xio.xio_npages, m); 1866 } 1867 } 1868 } 1869 #endif 1870 1871 /* 1872 * remove the mapping for kernel virtual 1873 */ 1874 pmap_qremove((vm_offset_t)bp->b_data, bp->b_xio.xio_npages); 1875 1876 /* 1877 * cleanup pages. If an error occurs writing to swap, we are in 1878 * very serious trouble. If it happens to be a disk error, though, 1879 * we may be able to recover by reassigning the swap later on. So 1880 * in this case we remove the m->swapblk assignment for the page 1881 * but do not free it in the rlist. The errornous block(s) are thus 1882 * never reallocated as swap. Redirty the page and continue. 1883 */ 1884 for (i = 0; i < bp->b_xio.xio_npages; ++i) { 1885 vm_page_t m = bp->b_xio.xio_pages[i]; 1886 1887 if (bp->b_flags & B_ERROR) { 1888 /* 1889 * If an error occurs I'd love to throw the swapblk 1890 * away without freeing it back to swapspace, so it 1891 * can never be used again. But I can't from an 1892 * interrupt. 1893 */ 1894 1895 if (bio->bio_caller_info1.index & SWBIO_READ) { 1896 /* 1897 * When reading, reqpage needs to stay 1898 * locked for the parent, but all other 1899 * pages can be freed. We still want to 1900 * wakeup the parent waiting on the page, 1901 * though. ( also: pg_reqpage can be -1 and 1902 * not match anything ). 1903 * 1904 * We have to wake specifically requested pages 1905 * up too because we cleared SWAPINPROG and 1906 * someone may be waiting for that. 1907 * 1908 * NOTE: For reads, m->dirty will probably 1909 * be overridden by the original caller 1910 * of getpages so don't play cute tricks 1911 * here. 1912 * 1913 * NOTE: We can't actually free the page from 1914 * here, because this is an interrupt. 1915 * It is not legal to mess with 1916 * object->memq from an interrupt. 1917 * Deactivate the page instead. 1918 * 1919 * WARNING! The instant SWAPINPROG is 1920 * cleared another cpu may start 1921 * using the mreq page (it will 1922 * check m->valid immediately). 1923 */ 1924 1925 m->valid = 0; 1926 atomic_clear_int(&m->busy_count, 1927 PBUSY_SWAPINPROG); 1928 1929 /* 1930 * bio_driver_info holds the requested page 1931 * index. 1932 */ 1933 if (i != (int)(intptr_t)bio->bio_driver_info) { 1934 vm_page_deactivate(m); 1935 vm_page_wakeup(m); 1936 } else { 1937 vm_page_flash(m); 1938 } 1939 /* 1940 * If i == bp->b_pager.pg_reqpage, do not wake 1941 * the page up. The caller needs to. 1942 */ 1943 } else { 1944 /* 1945 * If a write error occurs remove the swap 1946 * assignment (note that PG_SWAPPED may or 1947 * may not be set depending on prior activity). 1948 * 1949 * Re-dirty OBJT_SWAP pages as there is no 1950 * other backing store, we can't throw the 1951 * page away. 1952 * 1953 * Non-OBJT_SWAP pages (aka swapcache) must 1954 * not be dirtied since they may not have 1955 * been dirty in the first place, and they 1956 * do have backing store (the vnode). 1957 */ 1958 vm_page_busy_wait(m, FALSE, "swadpg"); 1959 vm_object_hold(m->object); 1960 swp_pager_meta_ctl(m->object, m->pindex, 1961 SWM_FREE); 1962 vm_page_flag_clear(m, PG_SWAPPED); 1963 vm_object_drop(m->object); 1964 if (m->object->type == OBJT_SWAP) { 1965 vm_page_dirty(m); 1966 vm_page_activate(m); 1967 } 1968 vm_page_io_finish(m); 1969 atomic_clear_int(&m->busy_count, 1970 PBUSY_SWAPINPROG); 1971 vm_page_wakeup(m); 1972 } 1973 } else if (bio->bio_caller_info1.index & SWBIO_READ) { 1974 /* 1975 * NOTE: for reads, m->dirty will probably be 1976 * overridden by the original caller of getpages so 1977 * we cannot set them in order to free the underlying 1978 * swap in a low-swap situation. I don't think we'd 1979 * want to do that anyway, but it was an optimization 1980 * that existed in the old swapper for a time before 1981 * it got ripped out due to precisely this problem. 1982 * 1983 * If not the requested page then deactivate it. 1984 * 1985 * Note that the requested page, reqpage, is left 1986 * busied, but we still have to wake it up. The 1987 * other pages are released (unbusied) by 1988 * vm_page_wakeup(). We do not set reqpage's 1989 * valid bits here, it is up to the caller. 1990 */ 1991 1992 /* 1993 * NOTE: Can't call pmap_clear_modify(m) from an 1994 * interrupt thread, the pmap code may have to 1995 * map non-kernel pmaps and currently asserts 1996 * the case. 1997 * 1998 * WARNING! The instant SWAPINPROG is 1999 * cleared another cpu may start 2000 * using the mreq page (it will 2001 * check m->valid immediately). 2002 */ 2003 /*pmap_clear_modify(m);*/ 2004 m->valid = VM_PAGE_BITS_ALL; 2005 vm_page_undirty(m); 2006 vm_page_flag_set(m, PG_SWAPPED); 2007 atomic_clear_int(&m->busy_count, PBUSY_SWAPINPROG); 2008 2009 /* 2010 * We have to wake specifically requested pages 2011 * up too because we cleared SWAPINPROG and 2012 * could be waiting for it in getpages. However, 2013 * be sure to not unbusy getpages specifically 2014 * requested page - getpages expects it to be 2015 * left busy. 2016 * 2017 * bio_driver_info holds the requested page 2018 */ 2019 if (i != (int)(intptr_t)bio->bio_driver_info) { 2020 vm_page_deactivate(m); 2021 vm_page_wakeup(m); 2022 } else { 2023 vm_page_flash(m); 2024 } 2025 } else { 2026 /* 2027 * Mark the page clean but do not mess with the 2028 * pmap-layer's modified state. That state should 2029 * also be clear since the caller protected the 2030 * page VM_PROT_READ, but allow the case. 2031 * 2032 * We are in an interrupt, avoid pmap operations. 2033 * 2034 * If we have a severe page deficit, deactivate the 2035 * page. Do not try to cache it (which would also 2036 * involve a pmap op), because the page might still 2037 * be read-heavy. 2038 * 2039 * When using the swap to cache clean vnode pages 2040 * we do not mess with the page dirty bits. 2041 * 2042 * NOTE! Nobody is waiting for the key mreq page 2043 * on write completion. 2044 */ 2045 vm_page_busy_wait(m, FALSE, "swadpg"); 2046 if (m->object->type == OBJT_SWAP) 2047 vm_page_undirty(m); 2048 vm_page_flag_set(m, PG_SWAPPED); 2049 atomic_clear_int(&m->busy_count, PBUSY_SWAPINPROG); 2050 if (vm_page_count_severe()) 2051 vm_page_deactivate(m); 2052 vm_page_io_finish(m); 2053 if (bio->bio_caller_info1.index & SWBIO_TTC) 2054 vm_page_try_to_cache(m); 2055 else 2056 vm_page_wakeup(m); 2057 } 2058 } 2059 2060 /* 2061 * adjust pip. NOTE: the original parent may still have its own 2062 * pip refs on the object. 2063 */ 2064 2065 if (object) 2066 vm_object_pip_wakeup_n(object, bp->b_xio.xio_npages); 2067 2068 /* 2069 * Release the physical I/O buffer. 2070 * 2071 * NOTE: Due to synchronous operations in the write case b_cmd may 2072 * already be set to BUF_CMD_DONE and BIO_SYNC may have already 2073 * been cleared. 2074 * 2075 * Use vm_token to interlock nsw_rcount/wcount wakeup? 2076 */ 2077 lwkt_gettoken(&vm_token); 2078 if (bio->bio_caller_info1.index & SWBIO_READ) 2079 nswptr = &nsw_rcount; 2080 else if (bio->bio_caller_info1.index & SWBIO_SYNC) 2081 nswptr = &nsw_wcount_sync; 2082 else 2083 nswptr = &nsw_wcount_async; 2084 bp->b_cmd = BUF_CMD_DONE; 2085 relpbuf(bp, nswptr); 2086 lwkt_reltoken(&vm_token); 2087 } 2088 2089 /* 2090 * Fault-in a potentially swapped page and remove the swap reference. 2091 * (used by swapoff code) 2092 * 2093 * object must be held. 2094 */ 2095 static __inline void 2096 swp_pager_fault_page(vm_object_t object, int *sharedp, vm_pindex_t pindex) 2097 { 2098 struct vnode *vp; 2099 vm_page_t m; 2100 int error; 2101 2102 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object)); 2103 2104 if (object->type == OBJT_VNODE) { 2105 /* 2106 * Any swap related to a vnode is due to swapcache. We must 2107 * vget() the vnode in case it is not active (otherwise 2108 * vref() will panic). Calling vm_object_page_remove() will 2109 * ensure that any swap ref is removed interlocked with the 2110 * page. clean_only is set to TRUE so we don't throw away 2111 * dirty pages. 2112 */ 2113 vp = object->handle; 2114 error = vget(vp, LK_SHARED | LK_RETRY | LK_CANRECURSE); 2115 if (error == 0) { 2116 vm_object_page_remove(object, pindex, pindex + 1, TRUE); 2117 vput(vp); 2118 } 2119 } else { 2120 /* 2121 * Otherwise it is a normal OBJT_SWAP object and we can 2122 * fault the page in and remove the swap. 2123 */ 2124 m = vm_fault_object_page(object, IDX_TO_OFF(pindex), 2125 VM_PROT_NONE, 2126 VM_FAULT_DIRTY | VM_FAULT_UNSWAP, 2127 sharedp, &error); 2128 if (m) 2129 vm_page_unhold(m); 2130 } 2131 } 2132 2133 /* 2134 * This removes all swap blocks related to a particular device. We have 2135 * to be careful of ripups during the scan. 2136 */ 2137 static int swp_pager_swapoff_callback(struct swblock *swap, void *data); 2138 2139 int 2140 swap_pager_swapoff(int devidx) 2141 { 2142 struct vm_object_hash *hash; 2143 struct swswapoffinfo info; 2144 struct vm_object marker; 2145 vm_object_t object; 2146 int n; 2147 2148 bzero(&marker, sizeof(marker)); 2149 marker.type = OBJT_MARKER; 2150 2151 for (n = 0; n < VMOBJ_HSIZE; ++n) { 2152 hash = &vm_object_hash[n]; 2153 2154 lwkt_gettoken(&hash->token); 2155 TAILQ_INSERT_HEAD(&hash->list, &marker, object_list); 2156 2157 while ((object = TAILQ_NEXT(&marker, object_list)) != NULL) { 2158 if (object->type == OBJT_MARKER) 2159 goto skip; 2160 if (object->type != OBJT_SWAP && 2161 object->type != OBJT_VNODE) 2162 goto skip; 2163 vm_object_hold(object); 2164 if (object->type != OBJT_SWAP && 2165 object->type != OBJT_VNODE) { 2166 vm_object_drop(object); 2167 goto skip; 2168 } 2169 2170 /* 2171 * Object is special in that we can't just pagein 2172 * into vm_page's in it (tmpfs, vn). 2173 */ 2174 if ((object->flags & OBJ_NOPAGEIN) && 2175 RB_ROOT(&object->swblock_root)) { 2176 vm_object_drop(object); 2177 goto skip; 2178 } 2179 2180 info.object = object; 2181 info.shared = 0; 2182 info.devidx = devidx; 2183 swblock_rb_tree_RB_SCAN(&object->swblock_root, 2184 NULL, swp_pager_swapoff_callback, 2185 &info); 2186 vm_object_drop(object); 2187 skip: 2188 if (object == TAILQ_NEXT(&marker, object_list)) { 2189 TAILQ_REMOVE(&hash->list, &marker, object_list); 2190 TAILQ_INSERT_AFTER(&hash->list, object, 2191 &marker, object_list); 2192 } 2193 } 2194 TAILQ_REMOVE(&hash->list, &marker, object_list); 2195 lwkt_reltoken(&hash->token); 2196 } 2197 2198 /* 2199 * If we fail to locate all swblocks we just fail gracefully and 2200 * do not bother to restore paging on the swap device. If the 2201 * user wants to retry the user can retry. 2202 */ 2203 if (swdevt[devidx].sw_nused) 2204 return (1); 2205 else 2206 return (0); 2207 } 2208 2209 static 2210 int 2211 swp_pager_swapoff_callback(struct swblock *swap, void *data) 2212 { 2213 struct swswapoffinfo *info = data; 2214 vm_object_t object = info->object; 2215 vm_pindex_t index; 2216 swblk_t v; 2217 int i; 2218 2219 index = swap->swb_index; 2220 for (i = 0; i < SWAP_META_PAGES; ++i) { 2221 /* 2222 * Make sure we don't race a dying object. This will 2223 * kill the scan of the object's swap blocks entirely. 2224 */ 2225 if (object->flags & OBJ_DEAD) 2226 return(-1); 2227 2228 /* 2229 * Fault the page, which can obviously block. If the swap 2230 * structure disappears break out. 2231 */ 2232 v = swap->swb_pages[i]; 2233 if (v != SWAPBLK_NONE && BLK2DEVIDX(v) == info->devidx) { 2234 swp_pager_fault_page(object, &info->shared, 2235 swap->swb_index + i); 2236 /* swap ptr might go away */ 2237 if (RB_LOOKUP(swblock_rb_tree, 2238 &object->swblock_root, index) != swap) { 2239 break; 2240 } 2241 } 2242 } 2243 return(0); 2244 } 2245 2246 /************************************************************************ 2247 * SWAP META DATA * 2248 ************************************************************************ 2249 * 2250 * These routines manipulate the swap metadata stored in the 2251 * OBJT_SWAP object. 2252 * 2253 * Swap metadata is implemented with a global hash and not directly 2254 * linked into the object. Instead the object simply contains 2255 * appropriate tracking counters. 2256 */ 2257 2258 /* 2259 * Lookup the swblock containing the specified swap block index. 2260 * 2261 * The caller must hold the object. 2262 */ 2263 static __inline 2264 struct swblock * 2265 swp_pager_lookup(vm_object_t object, vm_pindex_t index) 2266 { 2267 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object)); 2268 index &= ~(vm_pindex_t)SWAP_META_MASK; 2269 return (RB_LOOKUP(swblock_rb_tree, &object->swblock_root, index)); 2270 } 2271 2272 /* 2273 * Remove a swblock from the RB tree. 2274 * 2275 * The caller must hold the object. 2276 */ 2277 static __inline 2278 void 2279 swp_pager_remove(vm_object_t object, struct swblock *swap) 2280 { 2281 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object)); 2282 RB_REMOVE(swblock_rb_tree, &object->swblock_root, swap); 2283 } 2284 2285 /* 2286 * Convert default object to swap object if necessary 2287 * 2288 * The caller must hold the object. 2289 */ 2290 static void 2291 swp_pager_meta_convert(vm_object_t object) 2292 { 2293 if (object->type == OBJT_DEFAULT) { 2294 object->type = OBJT_SWAP; 2295 KKASSERT(object->swblock_count == 0); 2296 } 2297 } 2298 2299 /* 2300 * SWP_PAGER_META_BUILD() - add swap block to swap meta data for object 2301 * 2302 * We first convert the object to a swap object if it is a default 2303 * object. Vnode objects do not need to be converted. 2304 * 2305 * The specified swapblk is added to the object's swap metadata. If 2306 * the swapblk is not valid, it is freed instead. Any previously 2307 * assigned swapblk is freed. 2308 * 2309 * The caller must hold the object. 2310 */ 2311 static void 2312 swp_pager_meta_build(vm_object_t object, vm_pindex_t index, swblk_t swapblk) 2313 { 2314 struct swblock *swap; 2315 struct swblock *oswap; 2316 vm_pindex_t v; 2317 2318 KKASSERT(swapblk != SWAPBLK_NONE); 2319 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object)); 2320 2321 /* 2322 * Convert object if necessary 2323 */ 2324 if (object->type == OBJT_DEFAULT) 2325 swp_pager_meta_convert(object); 2326 2327 /* 2328 * Locate swblock. If not found create, but if we aren't adding 2329 * anything just return. If we run out of space in the map we wait 2330 * and, since the hash table may have changed, retry. 2331 */ 2332 retry: 2333 swap = swp_pager_lookup(object, index); 2334 2335 if (swap == NULL) { 2336 int i; 2337 2338 swap = zalloc(swap_zone); 2339 if (swap == NULL) { 2340 vm_wait(0); 2341 goto retry; 2342 } 2343 swap->swb_index = index & ~(vm_pindex_t)SWAP_META_MASK; 2344 swap->swb_count = 0; 2345 2346 ++object->swblock_count; 2347 2348 for (i = 0; i < SWAP_META_PAGES; ++i) 2349 swap->swb_pages[i] = SWAPBLK_NONE; 2350 oswap = RB_INSERT(swblock_rb_tree, &object->swblock_root, swap); 2351 KKASSERT(oswap == NULL); 2352 } 2353 2354 /* 2355 * Delete prior contents of metadata. 2356 * 2357 * NOTE: Decrement swb_count after the freeing operation (which 2358 * might block) to prevent racing destruction of the swblock. 2359 */ 2360 index &= SWAP_META_MASK; 2361 2362 while ((v = swap->swb_pages[index]) != SWAPBLK_NONE) { 2363 swap->swb_pages[index] = SWAPBLK_NONE; 2364 /* can block */ 2365 swp_pager_freeswapspace(object, v, 1); 2366 --swap->swb_count; 2367 --mycpu->gd_vmtotal.t_vm; 2368 } 2369 2370 /* 2371 * Enter block into metadata 2372 */ 2373 swap->swb_pages[index] = swapblk; 2374 if (swapblk != SWAPBLK_NONE) { 2375 ++swap->swb_count; 2376 ++mycpu->gd_vmtotal.t_vm; 2377 } 2378 } 2379 2380 /* 2381 * SWP_PAGER_META_FREE() - free a range of blocks in the object's swap metadata 2382 * 2383 * The requested range of blocks is freed, with any associated swap 2384 * returned to the swap bitmap. 2385 * 2386 * This routine will free swap metadata structures as they are cleaned 2387 * out. This routine does *NOT* operate on swap metadata associated 2388 * with resident pages. 2389 * 2390 * The caller must hold the object. 2391 */ 2392 static int swp_pager_meta_free_callback(struct swblock *swb, void *data); 2393 2394 static void 2395 swp_pager_meta_free(vm_object_t object, vm_pindex_t index, vm_pindex_t count) 2396 { 2397 struct swfreeinfo info; 2398 2399 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object)); 2400 2401 /* 2402 * Nothing to do 2403 */ 2404 if (object->swblock_count == 0) { 2405 KKASSERT(RB_EMPTY(&object->swblock_root)); 2406 return; 2407 } 2408 if (count == 0) 2409 return; 2410 2411 /* 2412 * Setup for RB tree scan. Note that the pindex range can be huge 2413 * due to the 64 bit page index space so we cannot safely iterate. 2414 */ 2415 info.object = object; 2416 info.basei = index & ~(vm_pindex_t)SWAP_META_MASK; 2417 info.begi = index; 2418 info.endi = index + count - 1; 2419 swblock_rb_tree_RB_SCAN(&object->swblock_root, rb_swblock_scancmp, 2420 swp_pager_meta_free_callback, &info); 2421 } 2422 2423 /* 2424 * The caller must hold the object. 2425 */ 2426 static 2427 int 2428 swp_pager_meta_free_callback(struct swblock *swap, void *data) 2429 { 2430 struct swfreeinfo *info = data; 2431 vm_object_t object = info->object; 2432 int index; 2433 int eindex; 2434 2435 /* 2436 * Figure out the range within the swblock. The wider scan may 2437 * return edge-case swap blocks when the start and/or end points 2438 * are in the middle of a block. 2439 */ 2440 if (swap->swb_index < info->begi) 2441 index = (int)info->begi & SWAP_META_MASK; 2442 else 2443 index = 0; 2444 2445 if (swap->swb_index + SWAP_META_PAGES > info->endi) 2446 eindex = (int)info->endi & SWAP_META_MASK; 2447 else 2448 eindex = SWAP_META_MASK; 2449 2450 /* 2451 * Scan and free the blocks. The loop terminates early 2452 * if (swap) runs out of blocks and could be freed. 2453 * 2454 * NOTE: Decrement swb_count after swp_pager_freeswapspace() 2455 * to deal with a zfree race. 2456 */ 2457 while (index <= eindex) { 2458 swblk_t v = swap->swb_pages[index]; 2459 2460 if (v != SWAPBLK_NONE) { 2461 swap->swb_pages[index] = SWAPBLK_NONE; 2462 /* can block */ 2463 swp_pager_freeswapspace(object, v, 1); 2464 --mycpu->gd_vmtotal.t_vm; 2465 if (--swap->swb_count == 0) { 2466 swp_pager_remove(object, swap); 2467 zfree(swap_zone, swap); 2468 --object->swblock_count; 2469 break; 2470 } 2471 } 2472 ++index; 2473 } 2474 2475 /* swap may be invalid here due to zfree above */ 2476 lwkt_yield(); 2477 2478 return(0); 2479 } 2480 2481 /* 2482 * SWP_PAGER_META_FREE_ALL() - destroy all swap metadata associated with object 2483 * 2484 * This routine locates and destroys all swap metadata associated with 2485 * an object. 2486 * 2487 * NOTE: Decrement swb_count after the freeing operation (which 2488 * might block) to prevent racing destruction of the swblock. 2489 * 2490 * The caller must hold the object. 2491 */ 2492 static void 2493 swp_pager_meta_free_all(vm_object_t object) 2494 { 2495 struct swblock *swap; 2496 int i; 2497 2498 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object)); 2499 2500 while ((swap = RB_ROOT(&object->swblock_root)) != NULL) { 2501 swp_pager_remove(object, swap); 2502 for (i = 0; i < SWAP_META_PAGES; ++i) { 2503 swblk_t v = swap->swb_pages[i]; 2504 if (v != SWAPBLK_NONE) { 2505 /* can block */ 2506 swp_pager_freeswapspace(object, v, 1); 2507 --swap->swb_count; 2508 --mycpu->gd_vmtotal.t_vm; 2509 } 2510 } 2511 if (swap->swb_count != 0) 2512 panic("swap_pager_meta_free_all: swb_count != 0"); 2513 zfree(swap_zone, swap); 2514 --object->swblock_count; 2515 lwkt_yield(); 2516 } 2517 KKASSERT(object->swblock_count == 0); 2518 } 2519 2520 /* 2521 * SWP_PAGER_METACTL() - misc control of swap and vm_page_t meta data. 2522 * 2523 * This routine is capable of looking up, popping, or freeing 2524 * swapblk assignments in the swap meta data or in the vm_page_t. 2525 * The routine typically returns the swapblk being looked-up, or popped, 2526 * or SWAPBLK_NONE if the block was freed, or SWAPBLK_NONE if the block 2527 * was invalid. This routine will automatically free any invalid 2528 * meta-data swapblks. 2529 * 2530 * It is not possible to store invalid swapblks in the swap meta data 2531 * (other then a literal 'SWAPBLK_NONE'), so we don't bother checking. 2532 * 2533 * When acting on a busy resident page and paging is in progress, we 2534 * have to wait until paging is complete but otherwise can act on the 2535 * busy page. 2536 * 2537 * SWM_FREE remove and free swap block from metadata 2538 * SWM_POP remove from meta data but do not free.. pop it out 2539 * 2540 * The caller must hold the object. 2541 */ 2542 static swblk_t 2543 swp_pager_meta_ctl(vm_object_t object, vm_pindex_t index, int flags) 2544 { 2545 struct swblock *swap; 2546 swblk_t r1; 2547 2548 if (object->swblock_count == 0) 2549 return(SWAPBLK_NONE); 2550 2551 r1 = SWAPBLK_NONE; 2552 swap = swp_pager_lookup(object, index); 2553 2554 if (swap != NULL) { 2555 index &= SWAP_META_MASK; 2556 r1 = swap->swb_pages[index]; 2557 2558 if (r1 != SWAPBLK_NONE) { 2559 if (flags & (SWM_FREE|SWM_POP)) { 2560 swap->swb_pages[index] = SWAPBLK_NONE; 2561 --mycpu->gd_vmtotal.t_vm; 2562 if (--swap->swb_count == 0) { 2563 swp_pager_remove(object, swap); 2564 zfree(swap_zone, swap); 2565 --object->swblock_count; 2566 } 2567 } 2568 /* swap ptr may be invalid */ 2569 if (flags & SWM_FREE) { 2570 swp_pager_freeswapspace(object, r1, 1); 2571 r1 = SWAPBLK_NONE; 2572 } 2573 } 2574 /* swap ptr may be invalid */ 2575 } 2576 return(r1); 2577 } 2578