sys/vm/vm_swapcache.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 | /* * (MPSAFE) * * Copyright (c) 2010,2019 The DragonFly Project. All rights reserved. * * This code is derived from software contributed to The DragonFly Project * by Matthew Dillon <dillon@backplane.com> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. Neither the name of The DragonFly Project nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific, prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* * Implement the swapcache daemon. When enabled swap is assumed to be * configured on a fast storage device such as a SSD. Swap is assigned * to clean vnode-backed pages in the inactive queue, clustered by object * if possible, and written out. The swap assignment sticks around even * after the underlying pages have been recycled. * * The daemon manages write bandwidth based on sysctl settings to control * wear on the SSD. * * The vnode strategy code will check for the swap assignments and divert * reads to the swap device when the data is present in the swapcache. * * This operates on both regular files and the block device vnodes used by * filesystems to manage meta-data. */ #include <sys/param.h> #include <sys/systm.h> #include <sys/kernel.h> #include <sys/proc.h> #include <sys/kthread.h> #include <sys/resourcevar.h> #include <sys/signalvar.h> #include <sys/vnode.h> #include <sys/vmmeter.h> #include <sys/sysctl.h> #include <sys/eventhandler.h> #include <vm/vm.h> #include <vm/vm_param.h> #include <sys/lock.h> #include <vm/vm_object.h> #include <vm/vm_page.h> #include <vm/vm_map.h> #include <vm/vm_pageout.h> #include <vm/vm_pager.h> #include <vm/swap_pager.h> #include <vm/vm_extern.h> #include <sys/spinlock2.h> #include <vm/vm_page2.h> struct swmarker { struct vm_object dummy_obj; struct vm_object *save_obj; vm_ooffset_t save_off; }; typedef struct swmarker swmarker_t; /* the kernel process "vm_pageout"*/ static int vm_swapcached_flush (vm_page_t m, int isblkdev); static int vm_swapcache_test(vm_page_t m); static int vm_swapcache_writing_heuristic(void); static int vm_swapcache_writing(vm_page_t marker, int count, int scount); static void vm_swapcache_cleaning(swmarker_t *marker, struct vm_object_hash **swindexp); static void vm_swapcache_movemarker(swmarker_t *marker, struct vm_object_hash *swindex, vm_object_t object); struct thread *swapcached_thread; SYSCTL_NODE(_vm, OID_AUTO, swapcache, CTLFLAG_RW, NULL, NULL); int vm_swapcache_read_enable; static long vm_swapcache_wtrigger; static int vm_swapcache_sleep; static int vm_swapcache_maxscan = PQ_L2_SIZE * 8; static int vm_swapcache_maxlaunder = PQ_L2_SIZE * 4; static int vm_swapcache_data_enable = 0; static int vm_swapcache_meta_enable = 0; static int vm_swapcache_maxswappct = 75; static int vm_swapcache_hysteresis; static int vm_swapcache_min_hysteresis; int vm_swapcache_use_chflags = 0; /* require chflags cache */ static int64_t vm_swapcache_minburst = 10000000LL; /* 10MB */ static int64_t vm_swapcache_curburst = 4000000000LL; /* 4G after boot */ static int64_t vm_swapcache_maxburst = 2000000000LL; /* 2G nominal max */ static int64_t vm_swapcache_accrate = 100000LL; /* 100K/s */ static int64_t vm_swapcache_write_count; static int64_t vm_swapcache_maxfilesize; static int64_t vm_swapcache_cleanperobj = 16*1024*1024; SYSCTL_INT(_vm_swapcache, OID_AUTO, maxlaunder, CTLFLAG_RW, &vm_swapcache_maxlaunder, 0, ""); SYSCTL_INT(_vm_swapcache, OID_AUTO, maxscan, CTLFLAG_RW, &vm_swapcache_maxscan, 0, ""); SYSCTL_INT(_vm_swapcache, OID_AUTO, data_enable, CTLFLAG_RW, &vm_swapcache_data_enable, 0, ""); SYSCTL_INT(_vm_swapcache, OID_AUTO, meta_enable, CTLFLAG_RW, &vm_swapcache_meta_enable, 0, ""); SYSCTL_INT(_vm_swapcache, OID_AUTO, read_enable, CTLFLAG_RW, &vm_swapcache_read_enable, 0, ""); SYSCTL_INT(_vm_swapcache, OID_AUTO, maxswappct, CTLFLAG_RW, &vm_swapcache_maxswappct, 0, ""); SYSCTL_INT(_vm_swapcache, OID_AUTO, hysteresis, CTLFLAG_RD, &vm_swapcache_hysteresis, 0, ""); SYSCTL_INT(_vm_swapcache, OID_AUTO, min_hysteresis, CTLFLAG_RW, &vm_swapcache_min_hysteresis, 0, ""); SYSCTL_INT(_vm_swapcache, OID_AUTO, use_chflags, CTLFLAG_RW, &vm_swapcache_use_chflags, 0, ""); SYSCTL_QUAD(_vm_swapcache, OID_AUTO, minburst, CTLFLAG_RW, &vm_swapcache_minburst, 0, ""); SYSCTL_QUAD(_vm_swapcache, OID_AUTO, curburst, CTLFLAG_RW, &vm_swapcache_curburst, 0, ""); SYSCTL_QUAD(_vm_swapcache, OID_AUTO, maxburst, CTLFLAG_RW, &vm_swapcache_maxburst, 0, ""); SYSCTL_QUAD(_vm_swapcache, OID_AUTO, maxfilesize, CTLFLAG_RW, &vm_swapcache_maxfilesize, 0, ""); SYSCTL_QUAD(_vm_swapcache, OID_AUTO, accrate, CTLFLAG_RW, &vm_swapcache_accrate, 0, ""); SYSCTL_QUAD(_vm_swapcache, OID_AUTO, write_count, CTLFLAG_RW, &vm_swapcache_write_count, 0, ""); SYSCTL_QUAD(_vm_swapcache, OID_AUTO, cleanperobj, CTLFLAG_RW, &vm_swapcache_cleanperobj, 0, ""); #define SWAPMAX(adj) \ ((int64_t)vm_swap_max * (vm_swapcache_maxswappct + (adj)) / 100) /* * When shutting down the machine we want to stop swapcache operation * immediately so swap is not accessed after devices have been shuttered. */ static void shutdown_swapcache(void *arg __unused) { vm_swapcache_read_enable = 0; vm_swapcache_data_enable = 0; vm_swapcache_meta_enable = 0; wakeup(&vm_swapcache_sleep); /* shortcut 5-second wait */ } /* * vm_swapcached is the high level pageout daemon. * * No requirements. */ static void vm_swapcached_thread(void) { enum { SWAPC_WRITING, SWAPC_CLEANING } state = SWAPC_WRITING; enum { SWAPB_BURSTING, SWAPB_RECOVERING } burst = SWAPB_BURSTING; static struct vm_page page_marker[PQ_L2_SIZE]; static swmarker_t swmarker; static struct vm_object_hash *swindex; int q; /* * Thread setup */ curthread->td_flags |= TDF_SYSTHREAD; EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc, swapcached_thread, SHUTDOWN_PRI_FIRST); EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_swapcache, NULL, SHUTDOWN_PRI_SECOND); /* * Initialize our marker for the inactive scan (SWAPC_WRITING) */ bzero(&page_marker, sizeof(page_marker)); for (q = 0; q < PQ_L2_SIZE; ++q) { page_marker[q].flags = PG_FICTITIOUS | PG_MARKER; page_marker[q].busy_count = PBUSY_LOCKED; page_marker[q].queue = PQ_INACTIVE + q; page_marker[q].pc = q; page_marker[q].wire_count = 1; vm_page_queues_spin_lock(PQ_INACTIVE + q); TAILQ_INSERT_HEAD( &vm_page_queues[PQ_INACTIVE + q].pl, &page_marker[q], pageq); vm_page_queues_spin_unlock(PQ_INACTIVE + q); } vm_swapcache_min_hysteresis = 1024; vm_swapcache_hysteresis = vm_swapcache_min_hysteresis; vm_swapcache_wtrigger = -vm_swapcache_hysteresis; /* * Initialize our marker for the vm_object scan (SWAPC_CLEANING) */ bzero(&swmarker, sizeof(swmarker)); swmarker.dummy_obj.type = OBJT_MARKER; swindex = &vm_object_hash[0]; lwkt_gettoken(&swindex->token); TAILQ_INSERT_HEAD(&swindex->list, &swmarker.dummy_obj, object_entry); lwkt_reltoken(&swindex->token); for (;;) { int reached_end; int scount; int count; /* * Handle shutdown */ kproc_suspend_loop(); /* * Check every 5 seconds when not enabled or if no swap * is present. */ if ((vm_swapcache_data_enable == 0 && vm_swapcache_meta_enable == 0 && vm_swap_cache_use <= SWAPMAX(0)) || vm_swap_max == 0) { tsleep(&vm_swapcache_sleep, 0, "csleep", hz * 5); continue; } /* * Polling rate when enabled is approximately 10 hz. */ tsleep(&vm_swapcache_sleep, 0, "csleep", hz / 10); /* * State hysteresis. Generate write activity up to 75% of * swap, then clean out swap assignments down to 70%, then * repeat. */ if (state == SWAPC_WRITING) { if (vm_swap_cache_use > SWAPMAX(0)) state = SWAPC_CLEANING; } else { if (vm_swap_cache_use < SWAPMAX(-10)) state = SWAPC_WRITING; } /* * We are allowed to continue accumulating burst value * in either state. Allow the user to set curburst > maxburst * for the initial load-in. */ if (vm_swapcache_curburst < vm_swapcache_maxburst) { vm_swapcache_curburst += vm_swapcache_accrate / 10; if (vm_swapcache_curburst > vm_swapcache_maxburst) vm_swapcache_curburst = vm_swapcache_maxburst; } /* * We don't want to nickle-and-dime the scan as that will * create unnecessary fragmentation. The minimum burst * is one-seconds worth of accumulation. */ if (state != SWAPC_WRITING) { vm_swapcache_cleaning(&swmarker, &swindex); continue; } if (vm_swapcache_curburst < vm_swapcache_accrate) continue; reached_end = 0; count = vm_swapcache_maxlaunder / PQ_L2_SIZE + 2; scount = vm_swapcache_maxscan / PQ_L2_SIZE + 2; if (burst == SWAPB_BURSTING) { if (vm_swapcache_writing_heuristic()) { for (q = 0; q < PQ_L2_SIZE; ++q) { reached_end += vm_swapcache_writing( &page_marker[q], count, scount); } } if (vm_swapcache_curburst <= 0) burst = SWAPB_RECOVERING; } else if (vm_swapcache_curburst > vm_swapcache_minburst) { if (vm_swapcache_writing_heuristic()) { for (q = 0; q < PQ_L2_SIZE; ++q) { reached_end += vm_swapcache_writing( &page_marker[q], count, scount); } } burst = SWAPB_BURSTING; } if (reached_end == PQ_L2_SIZE) { vm_swapcache_wtrigger = -vm_swapcache_hysteresis; } } /* * Cleanup (NOT REACHED) */ for (q = 0; q < PQ_L2_SIZE; ++q) { vm_page_queues_spin_lock(PQ_INACTIVE + q); TAILQ_REMOVE( &vm_page_queues[PQ_INACTIVE + q].pl, &page_marker[q], pageq); vm_page_queues_spin_unlock(PQ_INACTIVE + q); } lwkt_gettoken(&swindex->token); TAILQ_REMOVE(&swindex->list, &swmarker.dummy_obj, object_entry); lwkt_reltoken(&swindex->token); } static struct kproc_desc swpc_kp = { "swapcached", vm_swapcached_thread, &swapcached_thread }; SYSINIT(swapcached, SI_SUB_KTHREAD_PAGE, SI_ORDER_SECOND, kproc_start, &swpc_kp); /* * Deal with an overflow of the heuristic counter or if the user * manually changes the hysteresis. * * Try to avoid small incremental pageouts by waiting for enough * pages to buildup in the inactive queue to hopefully get a good * burst in. This heuristic is bumped by the VM system and reset * when our scan hits the end of the queue. * * Return TRUE if we need to take a writing pass. */ static int vm_swapcache_writing_heuristic(void) { int hyst; int q; long adds; hyst = vmstats.v_inactive_count / 4; if (hyst < vm_swapcache_min_hysteresis) hyst = vm_swapcache_min_hysteresis; cpu_ccfence(); vm_swapcache_hysteresis = hyst; adds = 0; for (q = PQ_INACTIVE; q < PQ_INACTIVE + PQ_L2_SIZE; ++q) { adds += atomic_swap_long(&vm_page_queues[q].adds, 0); } vm_swapcache_wtrigger += adds; if (vm_swapcache_wtrigger < -hyst) vm_swapcache_wtrigger = -hyst; return (vm_swapcache_wtrigger >= 0); } /* * Take a writing pass on one of the inactive queues, return non-zero if * we hit the end of the queue. */ static int vm_swapcache_writing(vm_page_t marker, int count, int scount) { vm_object_t object; struct vnode *vp; vm_page_t m; int isblkdev; /* * Scan the inactive queue from our marker to locate * suitable pages to push to the swap cache. * * We are looking for clean vnode-backed pages. */ vm_page_queues_spin_lock(marker->queue); while ((m = TAILQ_NEXT(marker, pageq)) != NULL && count > 0 && scount-- > 0) { KKASSERT(m->queue == marker->queue); /* * Stop using swap if paniced, dumping, or dumped. * Don't try to write if our curburst has been exhausted. */ if (panicstr || dumping) break; if (vm_swapcache_curburst < 0) break; /* * Move marker */ TAILQ_REMOVE( &vm_page_queues[marker->queue].pl, marker, pageq); TAILQ_INSERT_AFTER( &vm_page_queues[marker->queue].pl, m, marker, pageq); /* * Ignore markers and ignore pages that already have a swap * assignment. */ if (m->flags & (PG_MARKER | PG_SWAPPED)) continue; if (vm_page_busy_try(m, TRUE)) continue; vm_page_queues_spin_unlock(marker->queue); if ((object = m->object) == NULL) { vm_page_wakeup(m); vm_page_queues_spin_lock(marker->queue); continue; } vm_object_hold(object); if (m->object != object) { vm_object_drop(object); vm_page_wakeup(m); vm_page_queues_spin_lock(marker->queue); continue; } if (vm_swapcache_test(m)) { vm_object_drop(object); vm_page_wakeup(m); vm_page_queues_spin_lock(marker->queue); continue; } vp = object->handle; if (vp == NULL) { vm_object_drop(object); vm_page_wakeup(m); vm_page_queues_spin_lock(marker->queue); continue; } switch(vp->v_type) { case VREG: /* * PG_NOTMETA generically means 'don't swapcache this', * and HAMMER will set this for regular data buffers * (and leave it unset for meta-data buffers) as * appropriate when double buffering is enabled. */ if (m->flags & PG_NOTMETA) { vm_object_drop(object); vm_page_wakeup(m); vm_page_queues_spin_lock(marker->queue); continue; } /* * If data_enable is 0 do not try to swapcache data. * If use_chflags is set then only swapcache data for * VSWAPCACHE marked vnodes, otherwise any vnode. */ if (vm_swapcache_data_enable == 0 || ((vp->v_flag & VSWAPCACHE) == 0 && vm_swapcache_use_chflags)) { vm_object_drop(object); vm_page_wakeup(m); vm_page_queues_spin_lock(marker->queue); continue; } if (vm_swapcache_maxfilesize && object->size > (vm_swapcache_maxfilesize >> PAGE_SHIFT)) { vm_object_drop(object); vm_page_wakeup(m); vm_page_queues_spin_lock(marker->queue); continue; } isblkdev = 0; break; case VCHR: /* * PG_NOTMETA generically means 'don't swapcache this', * and HAMMER will set this for regular data buffers * (and leave it unset for meta-data buffers) as * appropriate when double buffering is enabled. */ if (m->flags & PG_NOTMETA) { vm_object_drop(object); vm_page_wakeup(m); vm_page_queues_spin_lock(marker->queue); continue; } if (vm_swapcache_meta_enable == 0) { vm_object_drop(object); vm_page_wakeup(m); vm_page_queues_spin_lock(marker->queue); continue; } isblkdev = 1; break; default: vm_object_drop(object); vm_page_wakeup(m); vm_page_queues_spin_lock(marker->queue); continue; } /* * Assign swap and initiate I/O. * * (adjust for the --count which also occurs in the loop) */ count -= vm_swapcached_flush(m, isblkdev); /* * Setup for next loop using marker. */ vm_object_drop(object); vm_page_queues_spin_lock(marker->queue); } /* * The marker could wind up at the end, which is ok. If we hit the * end of the list adjust the heuristic. * * Earlier inactive pages that were dirty and become clean * are typically moved to the end of PQ_INACTIVE by virtue * of vfs_vmio_release() when they become unwired from the * buffer cache. */ vm_page_queues_spin_unlock(marker->queue); /* * m invalid but can be used to test for NULL */ return (m == NULL); } /* * Flush the specified page using the swap_pager. The page * must be busied by the caller and its disposition will become * the responsibility of this function. * * Try to collect surrounding pages, including pages which may * have already been assigned swap. Try to cluster within a * contiguous aligned SMAP_META_PAGES (typ 16 x PAGE_SIZE) block * to match what swap_pager_putpages() can do. * * We also want to try to match against the buffer cache blocksize * but we don't really know what it is here. Since the buffer cache * wires and unwires pages in groups the fact that we skip wired pages * should be sufficient. * * Returns a count of pages we might have flushed (minimum 1) */ static int vm_swapcached_flush(vm_page_t m, int isblkdev) { vm_object_t object; vm_page_t marray[SWAP_META_PAGES]; vm_pindex_t basei; int rtvals[SWAP_META_PAGES]; int x; int i; int j; int count; int error; vm_page_io_start(m); vm_page_protect(m, VM_PROT_READ); object = m->object; vm_object_hold(object); /* * Try to cluster around (m), keeping in mind that the swap pager * can only do SMAP_META_PAGES worth of continguous write. */ x = (int)m->pindex & SWAP_META_MASK; marray[x] = m; basei = m->pindex; vm_page_wakeup(m); for (i = x - 1; i >= 0; --i) { m = vm_page_lookup_busy_try(object, basei - x + i, TRUE, &error); if (error || m == NULL) break; if (vm_swapcache_test(m)) { vm_page_wakeup(m); break; } if (isblkdev && (m->flags & PG_NOTMETA)) { vm_page_wakeup(m); break; } vm_page_io_start(m); vm_page_protect(m, VM_PROT_READ); if (m->queue - m->pc == PQ_CACHE) { vm_page_unqueue_nowakeup(m); vm_page_deactivate(m); } marray[i] = m; vm_page_wakeup(m); } ++i; for (j = x + 1; j < SWAP_META_PAGES; ++j) { m = vm_page_lookup_busy_try(object, basei - x + j, TRUE, &error); if (error || m == NULL) break; if (vm_swapcache_test(m)) { vm_page_wakeup(m); break; } if (isblkdev && (m->flags & PG_NOTMETA)) { vm_page_wakeup(m); break; } vm_page_io_start(m); vm_page_protect(m, VM_PROT_READ); if (m->queue - m->pc == PQ_CACHE) { vm_page_unqueue_nowakeup(m); vm_page_deactivate(m); } marray[j] = m; vm_page_wakeup(m); } count = j - i; vm_object_pip_add(object, count); swap_pager_putpages(object, marray + i, count, FALSE, rtvals + i); vm_swapcache_write_count += count * PAGE_SIZE; vm_swapcache_curburst -= count * PAGE_SIZE; while (i < j) { if (rtvals[i] != VM_PAGER_PEND) { vm_page_busy_wait(marray[i], FALSE, "swppgfd"); vm_page_io_finish(marray[i]); vm_page_wakeup(marray[i]); vm_object_pip_wakeup(object); } ++i; } vm_object_drop(object); return(count); } /* * Test whether a VM page is suitable for writing to the swapcache. * Does not test m->queue, PG_MARKER, or PG_SWAPPED. * * Returns 0 on success, 1 on failure */ static int vm_swapcache_test(vm_page_t m) { vm_object_t object; if (m->flags & (PG_UNQUEUED | PG_FICTITIOUS)) return(1); if (m->hold_count || m->wire_count) return(1); if (m->valid != VM_PAGE_BITS_ALL) return(1); if (m->dirty & m->valid) return(1); if ((object = m->object) == NULL) return(1); if (object->type != OBJT_VNODE || (object->flags & OBJ_DEAD)) { return(1); } vm_page_test_dirty(m); if (m->dirty & m->valid) return(1); return(0); } /* * Cleaning pass. * * We clean whole objects up to 16MB */ static void vm_swapcache_cleaning(swmarker_t *marker, struct vm_object_hash **swindexp) { vm_object_t object; struct vnode *vp; int count; int scount; int n; int didmove; count = vm_swapcache_maxlaunder; scount = vm_swapcache_maxscan; /* * Look for vnode objects */ lwkt_gettoken(&(*swindexp)->token); didmove = 0; outerloop: while ((object = TAILQ_NEXT(&marker->dummy_obj, object_entry)) != NULL) { /* * We have to skip markers. We cannot hold/drop marker * objects! */ if (object->type == OBJT_MARKER) { vm_swapcache_movemarker(marker, *swindexp, object); didmove = 1; continue; } /* * Safety, or in case there are millions of VM objects * without swapcache backing. */ if (--scount <= 0) goto breakout; /* * We must hold the object before potentially yielding. */ vm_object_hold(object); lwkt_yield(); /* * Only operate on live VNODE objects that are either * VREG or VCHR (VCHR for meta-data). */ if ((object->type != OBJT_VNODE) || ((object->flags & OBJ_DEAD) || object->swblock_count == 0) || ((vp = object->handle) == NULL) || (vp->v_type != VREG && vp->v_type != VCHR)) { vm_object_drop(object); /* object may be invalid now */ vm_swapcache_movemarker(marker, *swindexp, object); didmove = 1; continue; } /* * Reset the object pindex stored in the marker if the * working object has changed. */ if (marker->save_obj != object || didmove) { marker->dummy_obj.size = 0; marker->save_off = 0; marker->save_obj = object; didmove = 0; } /* * Look for swblocks starting at our iterator. * * The swap_pager_condfree() function attempts to free * swap space starting at the specified index. The index * will be updated on return. The function will return * a scan factor (NOT the number of blocks freed). * * If it must cut its scan of the object short due to an * excessive number of swblocks, or is able to free the * requested number of blocks, it will return n >= count * and we break and pick it back up on a future attempt. * * Scan the object linearly and try to batch large sets of * blocks that are likely to clean out entire swap radix * tree leafs. */ lwkt_token_swap(); lwkt_reltoken(&(*swindexp)->token); n = swap_pager_condfree(object, &marker->dummy_obj.size, (count + SWAP_META_MASK) & ~SWAP_META_MASK); vm_object_drop(object); /* object may be invalid now */ lwkt_gettoken(&(*swindexp)->token); /* * If we have exhausted the object or deleted our per-pass * page limit then move us to the next object. Note that * the current object may no longer be on the vm_object_entry. */ if (n <= 0 || marker->save_off > vm_swapcache_cleanperobj) { vm_swapcache_movemarker(marker, *swindexp, object); didmove = 1; } /* * If we have exhausted our max-launder stop for now. */ count -= n; marker->save_off += n * PAGE_SIZE; if (count < 0) goto breakout; } /* * Iterate vm_object_hash[] hash table */ TAILQ_REMOVE(&(*swindexp)->list, &marker->dummy_obj, object_entry); lwkt_reltoken(&(*swindexp)->token); if (++*swindexp >= &vm_object_hash[VMOBJ_HSIZE]) *swindexp = &vm_object_hash[0]; lwkt_gettoken(&(*swindexp)->token); TAILQ_INSERT_HEAD(&(*swindexp)->list, &marker->dummy_obj, object_entry); if (*swindexp != &vm_object_hash[0]) goto outerloop; breakout: lwkt_reltoken(&(*swindexp)->token); } /* * Move the marker past the current object. Object can be stale, but we * still need it to determine if the marker has to be moved. If the object * is still the 'current object' (object after the marker), we hop-scotch * the marker past it. */ static void vm_swapcache_movemarker(swmarker_t *marker, struct vm_object_hash *swindex, vm_object_t object) { if (TAILQ_NEXT(&marker->dummy_obj, object_entry) == object) { TAILQ_REMOVE(&swindex->list, &marker->dummy_obj, object_entry); TAILQ_INSERT_AFTER(&swindex->list, object, &marker->dummy_obj, object_entry); } } |