1 /* 2 * (MPSAFE) 3 * 4 * Copyright (c) 1991 Regents of the University of California. 5 * All rights reserved. 6 * Copyright (c) 1994 John S. Dyson 7 * All rights reserved. 8 * Copyright (c) 1994 David Greenman 9 * All rights reserved. 10 * 11 * This code is derived from software contributed to Berkeley by 12 * The Mach Operating System project at Carnegie-Mellon University. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * from: @(#)vm_pageout.c 7.4 (Berkeley) 5/7/91 39 * 40 * 41 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 42 * All rights reserved. 43 * 44 * Authors: Avadis Tevanian, Jr., Michael Wayne Young 45 * 46 * Permission to use, copy, modify and distribute this software and 47 * its documentation is hereby granted, provided that both the copyright 48 * notice and this permission notice appear in all copies of the 49 * software, derivative works or modified versions, and any portions 50 * thereof, and that both notices appear in supporting documentation. 51 * 52 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 53 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 54 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 55 * 56 * Carnegie Mellon requests users of this software to return to 57 * 58 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 59 * School of Computer Science 60 * Carnegie Mellon University 61 * Pittsburgh PA 15213-3890 62 * 63 * any improvements or extensions that they make and grant Carnegie the 64 * rights to redistribute these changes. 65 * 66 * $FreeBSD: src/sys/vm/vm_pageout.c,v 1.151.2.15 2002/12/29 18:21:04 dillon Exp $ 67 */ 68 69 /* 70 * The proverbial page-out daemon. 71 */ 72 73 #include "opt_vm.h" 74 #include <sys/param.h> 75 #include <sys/systm.h> 76 #include <sys/kernel.h> 77 #include <sys/proc.h> 78 #include <sys/kthread.h> 79 #include <sys/resourcevar.h> 80 #include <sys/signalvar.h> 81 #include <sys/vnode.h> 82 #include <sys/vmmeter.h> 83 #include <sys/sysctl.h> 84 85 #include <vm/vm.h> 86 #include <vm/vm_param.h> 87 #include <sys/lock.h> 88 #include <vm/vm_object.h> 89 #include <vm/vm_page.h> 90 #include <vm/vm_map.h> 91 #include <vm/vm_pageout.h> 92 #include <vm/vm_pager.h> 93 #include <vm/swap_pager.h> 94 #include <vm/vm_extern.h> 95 96 #include <sys/thread2.h> 97 #include <vm/vm_page2.h> 98 99 /* 100 * System initialization 101 */ 102 103 /* the kernel process "vm_pageout"*/ 104 static int vm_pageout_clean (vm_page_t); 105 static int vm_pageout_scan (int pass); 106 static int vm_pageout_free_page_calc (vm_size_t count); 107 struct thread *pagethread; 108 109 #if !defined(NO_SWAPPING) 110 /* the kernel process "vm_daemon"*/ 111 static void vm_daemon (void); 112 static struct thread *vmthread; 113 114 static struct kproc_desc vm_kp = { 115 "vmdaemon", 116 vm_daemon, 117 &vmthread 118 }; 119 SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp) 120 #endif 121 122 123 int vm_pages_needed=0; /* Event on which pageout daemon sleeps */ 124 int vm_pageout_deficit=0; /* Estimated number of pages deficit */ 125 int vm_pageout_pages_needed=0; /* flag saying that the pageout daemon needs pages */ 126 127 #if !defined(NO_SWAPPING) 128 static int vm_pageout_req_swapout; /* XXX */ 129 static int vm_daemon_needed; 130 #endif 131 static int vm_max_launder = 32; 132 static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0; 133 static int vm_pageout_full_stats_interval = 0; 134 static int vm_pageout_stats_free_max=0, vm_pageout_algorithm=0; 135 static int defer_swap_pageouts=0; 136 static int disable_swap_pageouts=0; 137 138 #if defined(NO_SWAPPING) 139 static int vm_swap_enabled=0; 140 static int vm_swap_idle_enabled=0; 141 #else 142 static int vm_swap_enabled=1; 143 static int vm_swap_idle_enabled=0; 144 #endif 145 146 SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm, 147 CTLFLAG_RW, &vm_pageout_algorithm, 0, "LRU page mgmt"); 148 149 SYSCTL_INT(_vm, OID_AUTO, max_launder, 150 CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout"); 151 152 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max, 153 CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length"); 154 155 SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval, 156 CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan"); 157 158 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval, 159 CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan"); 160 161 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_free_max, 162 CTLFLAG_RW, &vm_pageout_stats_free_max, 0, "Not implemented"); 163 164 #if defined(NO_SWAPPING) 165 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 166 CTLFLAG_RD, &vm_swap_enabled, 0, ""); 167 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 168 CTLFLAG_RD, &vm_swap_idle_enabled, 0, ""); 169 #else 170 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 171 CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout"); 172 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 173 CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria"); 174 #endif 175 176 SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts, 177 CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem"); 178 179 SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts, 180 CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages"); 181 182 static int pageout_lock_miss; 183 SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss, 184 CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout"); 185 186 int vm_load; 187 SYSCTL_INT(_vm, OID_AUTO, vm_load, 188 CTLFLAG_RD, &vm_load, 0, "load on the VM system"); 189 int vm_load_enable = 1; 190 SYSCTL_INT(_vm, OID_AUTO, vm_load_enable, 191 CTLFLAG_RW, &vm_load_enable, 0, "enable vm_load rate limiting"); 192 #ifdef INVARIANTS 193 int vm_load_debug; 194 SYSCTL_INT(_vm, OID_AUTO, vm_load_debug, 195 CTLFLAG_RW, &vm_load_debug, 0, "debug vm_load"); 196 #endif 197 198 #define VM_PAGEOUT_PAGE_COUNT 16 199 int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT; 200 201 int vm_page_max_wired; /* XXX max # of wired pages system-wide */ 202 203 #if !defined(NO_SWAPPING) 204 typedef void freeer_fcn_t (vm_map_t, vm_object_t, vm_pindex_t, int); 205 static void vm_pageout_map_deactivate_pages (vm_map_t, vm_pindex_t); 206 static freeer_fcn_t vm_pageout_object_deactivate_pages; 207 static void vm_req_vmdaemon (void); 208 #endif 209 static void vm_pageout_page_stats(void); 210 211 /* 212 * Update vm_load to slow down faulting processes. 213 * 214 * SMP races ok. 215 * No requirements. 216 */ 217 void 218 vm_fault_ratecheck(void) 219 { 220 if (vm_pages_needed) { 221 if (vm_load < 1000) 222 ++vm_load; 223 } else { 224 if (vm_load > 0) 225 --vm_load; 226 } 227 } 228 229 /* 230 * vm_pageout_clean: 231 * 232 * Clean the page and remove it from the laundry. The page must not be 233 * busy on-call. 234 * 235 * We set the busy bit to cause potential page faults on this page to 236 * block. Note the careful timing, however, the busy bit isn't set till 237 * late and we cannot do anything that will mess with the page. 238 * 239 * The caller must hold vm_token. 240 */ 241 static int 242 vm_pageout_clean(vm_page_t m) 243 { 244 vm_object_t object; 245 vm_page_t mc[2*vm_pageout_page_count]; 246 int pageout_count; 247 int ib, is, page_base; 248 vm_pindex_t pindex = m->pindex; 249 250 object = m->object; 251 252 /* 253 * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP 254 * with the new swapper, but we could have serious problems paging 255 * out other object types if there is insufficient memory. 256 * 257 * Unfortunately, checking free memory here is far too late, so the 258 * check has been moved up a procedural level. 259 */ 260 261 /* 262 * Don't mess with the page if it's busy, held, or special 263 */ 264 if ((m->hold_count != 0) || 265 ((m->busy != 0) || (m->flags & (PG_BUSY|PG_UNMANAGED)))) { 266 return 0; 267 } 268 269 mc[vm_pageout_page_count] = m; 270 pageout_count = 1; 271 page_base = vm_pageout_page_count; 272 ib = 1; 273 is = 1; 274 275 /* 276 * Scan object for clusterable pages. 277 * 278 * We can cluster ONLY if: ->> the page is NOT 279 * clean, wired, busy, held, or mapped into a 280 * buffer, and one of the following: 281 * 1) The page is inactive, or a seldom used 282 * active page. 283 * -or- 284 * 2) we force the issue. 285 * 286 * During heavy mmap/modification loads the pageout 287 * daemon can really fragment the underlying file 288 * due to flushing pages out of order and not trying 289 * align the clusters (which leave sporatic out-of-order 290 * holes). To solve this problem we do the reverse scan 291 * first and attempt to align our cluster, then do a 292 * forward scan if room remains. 293 */ 294 295 vm_object_hold(object); 296 more: 297 while (ib && pageout_count < vm_pageout_page_count) { 298 vm_page_t p; 299 300 if (ib > pindex) { 301 ib = 0; 302 break; 303 } 304 305 if ((p = vm_page_lookup(object, pindex - ib)) == NULL) { 306 ib = 0; 307 break; 308 } 309 if (((p->queue - p->pc) == PQ_CACHE) || 310 (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) { 311 ib = 0; 312 break; 313 } 314 vm_page_test_dirty(p); 315 if ((p->dirty & p->valid) == 0 || 316 p->queue != PQ_INACTIVE || 317 p->wire_count != 0 || /* may be held by buf cache */ 318 p->hold_count != 0) { /* may be undergoing I/O */ 319 ib = 0; 320 break; 321 } 322 mc[--page_base] = p; 323 ++pageout_count; 324 ++ib; 325 /* 326 * alignment boundry, stop here and switch directions. Do 327 * not clear ib. 328 */ 329 if ((pindex - (ib - 1)) % vm_pageout_page_count == 0) 330 break; 331 } 332 333 while (pageout_count < vm_pageout_page_count && 334 pindex + is < object->size) { 335 vm_page_t p; 336 337 if ((p = vm_page_lookup(object, pindex + is)) == NULL) 338 break; 339 if (((p->queue - p->pc) == PQ_CACHE) || 340 (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) { 341 break; 342 } 343 vm_page_test_dirty(p); 344 if ((p->dirty & p->valid) == 0 || 345 p->queue != PQ_INACTIVE || 346 p->wire_count != 0 || /* may be held by buf cache */ 347 p->hold_count != 0) { /* may be undergoing I/O */ 348 break; 349 } 350 mc[page_base + pageout_count] = p; 351 ++pageout_count; 352 ++is; 353 } 354 355 /* 356 * If we exhausted our forward scan, continue with the reverse scan 357 * when possible, even past a page boundry. This catches boundry 358 * conditions. 359 */ 360 if (ib && pageout_count < vm_pageout_page_count) 361 goto more; 362 363 vm_object_drop(object); 364 365 /* 366 * we allow reads during pageouts... 367 */ 368 return vm_pageout_flush(&mc[page_base], pageout_count, 0); 369 } 370 371 /* 372 * vm_pageout_flush() - launder the given pages 373 * 374 * The given pages are laundered. Note that we setup for the start of 375 * I/O ( i.e. busy the page ), mark it read-only, and bump the object 376 * reference count all in here rather then in the parent. If we want 377 * the parent to do more sophisticated things we may have to change 378 * the ordering. 379 * 380 * The caller must hold vm_token. 381 */ 382 int 383 vm_pageout_flush(vm_page_t *mc, int count, int flags) 384 { 385 vm_object_t object; 386 int pageout_status[count]; 387 int numpagedout = 0; 388 int i; 389 390 ASSERT_LWKT_TOKEN_HELD(&vm_token); 391 392 /* 393 * Initiate I/O. Bump the vm_page_t->busy counter. 394 */ 395 for (i = 0; i < count; i++) { 396 KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL, ("vm_pageout_flush page %p index %d/%d: partially invalid page", mc[i], i, count)); 397 vm_page_io_start(mc[i]); 398 } 399 400 /* 401 * We must make the pages read-only. This will also force the 402 * modified bit in the related pmaps to be cleared. The pager 403 * cannot clear the bit for us since the I/O completion code 404 * typically runs from an interrupt. The act of making the page 405 * read-only handles the case for us. 406 */ 407 for (i = 0; i < count; i++) { 408 vm_page_protect(mc[i], VM_PROT_READ); 409 } 410 411 object = mc[0]->object; 412 vm_object_pip_add(object, count); 413 414 vm_pager_put_pages(object, mc, count, 415 (flags | ((object == &kernel_object) ? VM_PAGER_PUT_SYNC : 0)), 416 pageout_status); 417 418 for (i = 0; i < count; i++) { 419 vm_page_t mt = mc[i]; 420 421 switch (pageout_status[i]) { 422 case VM_PAGER_OK: 423 numpagedout++; 424 break; 425 case VM_PAGER_PEND: 426 numpagedout++; 427 break; 428 case VM_PAGER_BAD: 429 /* 430 * Page outside of range of object. Right now we 431 * essentially lose the changes by pretending it 432 * worked. 433 */ 434 pmap_clear_modify(mt); 435 vm_page_undirty(mt); 436 break; 437 case VM_PAGER_ERROR: 438 case VM_PAGER_FAIL: 439 /* 440 * A page typically cannot be paged out when we 441 * have run out of swap. We leave the page 442 * marked inactive and will try to page it out 443 * again later. 444 * 445 * Starvation of the active page list is used to 446 * determine when the system is massively memory 447 * starved. 448 */ 449 break; 450 case VM_PAGER_AGAIN: 451 break; 452 } 453 454 /* 455 * If the operation is still going, leave the page busy to 456 * block all other accesses. Also, leave the paging in 457 * progress indicator set so that we don't attempt an object 458 * collapse. 459 * 460 * For any pages which have completed synchronously, 461 * deactivate the page if we are under a severe deficit. 462 * Do not try to enter them into the cache, though, they 463 * might still be read-heavy. 464 */ 465 if (pageout_status[i] != VM_PAGER_PEND) { 466 if (vm_page_count_severe()) 467 vm_page_deactivate(mt); 468 #if 0 469 if (!vm_page_count_severe() || !vm_page_try_to_cache(mt)) 470 vm_page_protect(mt, VM_PROT_READ); 471 #endif 472 vm_page_io_finish(mt); 473 vm_object_pip_wakeup(object); 474 } 475 } 476 return numpagedout; 477 } 478 479 #if !defined(NO_SWAPPING) 480 /* 481 * vm_pageout_object_deactivate_pages 482 * 483 * deactivate enough pages to satisfy the inactive target 484 * requirements or if vm_page_proc_limit is set, then 485 * deactivate all of the pages in the object and its 486 * backing_objects. 487 * 488 * The map must be locked. 489 * The caller must hold vm_token. 490 * The caller must hold the vm_object. 491 */ 492 static int vm_pageout_object_deactivate_pages_callback(vm_page_t, void *); 493 494 static void 495 vm_pageout_object_deactivate_pages(vm_map_t map, vm_object_t object, 496 vm_pindex_t desired, int map_remove_only) 497 { 498 struct rb_vm_page_scan_info info; 499 vm_object_t tmp; 500 int remove_mode; 501 502 while (object) { 503 if (pmap_resident_count(vm_map_pmap(map)) <= desired) 504 return; 505 506 vm_object_hold(object); 507 508 if (object->type == OBJT_DEVICE || object->type == OBJT_PHYS) { 509 vm_object_drop(object); 510 return; 511 } 512 if (object->paging_in_progress) { 513 vm_object_drop(object); 514 return; 515 } 516 517 remove_mode = map_remove_only; 518 if (object->shadow_count > 1) 519 remove_mode = 1; 520 521 /* 522 * scan the objects entire memory queue. We hold the 523 * object's token so the scan should not race anything. 524 */ 525 info.limit = remove_mode; 526 info.map = map; 527 info.desired = desired; 528 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL, 529 vm_pageout_object_deactivate_pages_callback, 530 &info 531 ); 532 tmp = object->backing_object; 533 vm_object_drop(object); 534 object = tmp; 535 } 536 } 537 538 /* 539 * The caller must hold vm_token. 540 * The caller must hold the vm_object. 541 */ 542 static int 543 vm_pageout_object_deactivate_pages_callback(vm_page_t p, void *data) 544 { 545 struct rb_vm_page_scan_info *info = data; 546 int actcount; 547 548 if (pmap_resident_count(vm_map_pmap(info->map)) <= info->desired) { 549 return(-1); 550 } 551 mycpu->gd_cnt.v_pdpages++; 552 if (p->wire_count != 0 || p->hold_count != 0 || p->busy != 0 || 553 (p->flags & (PG_BUSY|PG_UNMANAGED)) || 554 !pmap_page_exists_quick(vm_map_pmap(info->map), p)) { 555 return(0); 556 } 557 558 actcount = pmap_ts_referenced(p); 559 if (actcount) { 560 vm_page_flag_set(p, PG_REFERENCED); 561 } else if (p->flags & PG_REFERENCED) { 562 actcount = 1; 563 } 564 565 if ((p->queue != PQ_ACTIVE) && 566 (p->flags & PG_REFERENCED)) { 567 vm_page_activate(p); 568 p->act_count += actcount; 569 vm_page_flag_clear(p, PG_REFERENCED); 570 } else if (p->queue == PQ_ACTIVE) { 571 if ((p->flags & PG_REFERENCED) == 0) { 572 p->act_count -= min(p->act_count, ACT_DECLINE); 573 if (!info->limit && (vm_pageout_algorithm || (p->act_count == 0))) { 574 vm_page_busy(p); 575 vm_page_protect(p, VM_PROT_NONE); 576 vm_page_deactivate(p); 577 vm_page_wakeup(p); 578 } else { 579 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, p, pageq); 580 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, p, pageq); 581 } 582 } else { 583 vm_page_activate(p); 584 vm_page_flag_clear(p, PG_REFERENCED); 585 if (p->act_count < (ACT_MAX - ACT_ADVANCE)) 586 p->act_count += ACT_ADVANCE; 587 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, p, pageq); 588 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, p, pageq); 589 } 590 } else if (p->queue == PQ_INACTIVE) { 591 vm_page_busy(p); 592 vm_page_protect(p, VM_PROT_NONE); 593 vm_page_wakeup(p); 594 } 595 return(0); 596 } 597 598 /* 599 * Deactivate some number of pages in a map, try to do it fairly, but 600 * that is really hard to do. 601 * 602 * The caller must hold vm_token. 603 */ 604 static void 605 vm_pageout_map_deactivate_pages(vm_map_t map, vm_pindex_t desired) 606 { 607 vm_map_entry_t tmpe; 608 vm_object_t obj, bigobj; 609 int nothingwired; 610 611 if (lockmgr(&map->lock, LK_EXCLUSIVE | LK_NOWAIT)) { 612 return; 613 } 614 615 bigobj = NULL; 616 nothingwired = TRUE; 617 618 /* 619 * first, search out the biggest object, and try to free pages from 620 * that. 621 */ 622 tmpe = map->header.next; 623 while (tmpe != &map->header) { 624 switch(tmpe->maptype) { 625 case VM_MAPTYPE_NORMAL: 626 case VM_MAPTYPE_VPAGETABLE: 627 obj = tmpe->object.vm_object; 628 if ((obj != NULL) && (obj->shadow_count <= 1) && 629 ((bigobj == NULL) || 630 (bigobj->resident_page_count < obj->resident_page_count))) { 631 bigobj = obj; 632 } 633 break; 634 default: 635 break; 636 } 637 if (tmpe->wired_count > 0) 638 nothingwired = FALSE; 639 tmpe = tmpe->next; 640 } 641 642 if (bigobj) 643 vm_pageout_object_deactivate_pages(map, bigobj, desired, 0); 644 645 /* 646 * Next, hunt around for other pages to deactivate. We actually 647 * do this search sort of wrong -- .text first is not the best idea. 648 */ 649 tmpe = map->header.next; 650 while (tmpe != &map->header) { 651 if (pmap_resident_count(vm_map_pmap(map)) <= desired) 652 break; 653 switch(tmpe->maptype) { 654 case VM_MAPTYPE_NORMAL: 655 case VM_MAPTYPE_VPAGETABLE: 656 obj = tmpe->object.vm_object; 657 if (obj) 658 vm_pageout_object_deactivate_pages(map, obj, desired, 0); 659 break; 660 default: 661 break; 662 } 663 tmpe = tmpe->next; 664 }; 665 666 /* 667 * Remove all mappings if a process is swapped out, this will free page 668 * table pages. 669 */ 670 if (desired == 0 && nothingwired) 671 pmap_remove(vm_map_pmap(map), 672 VM_MIN_USER_ADDRESS, VM_MAX_USER_ADDRESS); 673 vm_map_unlock(map); 674 } 675 #endif 676 677 /* 678 * Called when the pageout scan wants to free a page. We no longer 679 * try to cycle the vm_object here with a reference & dealloc, which can 680 * cause a non-trivial object collapse in a critical path. 681 * 682 * It is unclear why we cycled the ref_count in the past, perhaps to try 683 * to optimize shadow chain collapses but I don't quite see why it would 684 * be necessary. An OBJ_DEAD object should terminate any and all vm_pages 685 * synchronously and not have to be kicked-start. 686 * 687 * The caller must hold vm_token. 688 */ 689 static void 690 vm_pageout_page_free(vm_page_t m) 691 { 692 vm_page_busy(m); 693 vm_page_protect(m, VM_PROT_NONE); 694 vm_page_free(m); 695 } 696 697 /* 698 * vm_pageout_scan does the dirty work for the pageout daemon. 699 */ 700 struct vm_pageout_scan_info { 701 struct proc *bigproc; 702 vm_offset_t bigsize; 703 }; 704 705 static int vm_pageout_scan_callback(struct proc *p, void *data); 706 707 /* 708 * The caller must hold vm_token. 709 */ 710 static int 711 vm_pageout_scan(int pass) 712 { 713 struct vm_pageout_scan_info info; 714 vm_page_t m, next; 715 struct vm_page marker; 716 struct vnode *vpfailed; /* warning, allowed to be stale */ 717 int maxscan, pcount; 718 int recycle_count; 719 int inactive_shortage, active_shortage; 720 int inactive_original_shortage; 721 vm_object_t object; 722 int actcount; 723 int vnodes_skipped = 0; 724 int maxlaunder; 725 726 /* 727 * Do whatever cleanup that the pmap code can. 728 */ 729 pmap_collect(); 730 731 /* 732 * Calculate our target for the number of free+cache pages we 733 * want to get to. This is higher then the number that causes 734 * allocations to stall (severe) in order to provide hysteresis, 735 * and if we don't make it all the way but get to the minimum 736 * we're happy. 737 */ 738 inactive_shortage = vm_paging_target() + vm_pageout_deficit; 739 inactive_original_shortage = inactive_shortage; 740 vm_pageout_deficit = 0; 741 742 /* 743 * Initialize our marker 744 */ 745 bzero(&marker, sizeof(marker)); 746 marker.flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER; 747 marker.queue = PQ_INACTIVE; 748 marker.wire_count = 1; 749 750 /* 751 * Start scanning the inactive queue for pages we can move to the 752 * cache or free. The scan will stop when the target is reached or 753 * we have scanned the entire inactive queue. Note that m->act_count 754 * is not used to form decisions for the inactive queue, only for the 755 * active queue. 756 * 757 * maxlaunder limits the number of dirty pages we flush per scan. 758 * For most systems a smaller value (16 or 32) is more robust under 759 * extreme memory and disk pressure because any unnecessary writes 760 * to disk can result in extreme performance degredation. However, 761 * systems with excessive dirty pages (especially when MAP_NOSYNC is 762 * used) will die horribly with limited laundering. If the pageout 763 * daemon cannot clean enough pages in the first pass, we let it go 764 * all out in succeeding passes. 765 */ 766 if ((maxlaunder = vm_max_launder) <= 1) 767 maxlaunder = 1; 768 if (pass) 769 maxlaunder = 10000; 770 771 /* 772 * We will generally be in a critical section throughout the 773 * scan, but we can release it temporarily when we are sitting on a 774 * non-busy page without fear. this is required to prevent an 775 * interrupt from unbusying or freeing a page prior to our busy 776 * check, leaving us on the wrong queue or checking the wrong 777 * page. 778 */ 779 rescan0: 780 vpfailed = NULL; 781 maxscan = vmstats.v_inactive_count; 782 for (m = TAILQ_FIRST(&vm_page_queues[PQ_INACTIVE].pl); 783 m != NULL && maxscan-- > 0 && inactive_shortage > 0; 784 m = next 785 ) { 786 mycpu->gd_cnt.v_pdpages++; 787 788 /* 789 * It's easier for some of the conditions below to just loop 790 * and catch queue changes here rather then check everywhere 791 * else. 792 */ 793 if (m->queue != PQ_INACTIVE) 794 goto rescan0; 795 next = TAILQ_NEXT(m, pageq); 796 797 /* 798 * skip marker pages 799 */ 800 if (m->flags & PG_MARKER) 801 continue; 802 803 /* 804 * A held page may be undergoing I/O, so skip it. 805 */ 806 if (m->hold_count) { 807 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 808 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 809 ++vm_swapcache_inactive_heuristic; 810 continue; 811 } 812 813 /* 814 * Dont mess with busy pages, keep in the front of the 815 * queue, most likely are being paged out. 816 */ 817 if (m->busy || (m->flags & PG_BUSY)) { 818 continue; 819 } 820 821 if (m->object->ref_count == 0) { 822 /* 823 * If the object is not being used, we ignore previous 824 * references. 825 */ 826 vm_page_flag_clear(m, PG_REFERENCED); 827 pmap_clear_reference(m); 828 829 } else if (((m->flags & PG_REFERENCED) == 0) && 830 (actcount = pmap_ts_referenced(m))) { 831 /* 832 * Otherwise, if the page has been referenced while 833 * in the inactive queue, we bump the "activation 834 * count" upwards, making it less likely that the 835 * page will be added back to the inactive queue 836 * prematurely again. Here we check the page tables 837 * (or emulated bits, if any), given the upper level 838 * VM system not knowing anything about existing 839 * references. 840 */ 841 vm_page_activate(m); 842 m->act_count += (actcount + ACT_ADVANCE); 843 continue; 844 } 845 846 /* 847 * If the upper level VM system knows about any page 848 * references, we activate the page. We also set the 849 * "activation count" higher than normal so that we will less 850 * likely place pages back onto the inactive queue again. 851 */ 852 if ((m->flags & PG_REFERENCED) != 0) { 853 vm_page_flag_clear(m, PG_REFERENCED); 854 actcount = pmap_ts_referenced(m); 855 vm_page_activate(m); 856 m->act_count += (actcount + ACT_ADVANCE + 1); 857 continue; 858 } 859 860 /* 861 * If the upper level VM system doesn't know anything about 862 * the page being dirty, we have to check for it again. As 863 * far as the VM code knows, any partially dirty pages are 864 * fully dirty. 865 * 866 * Pages marked PG_WRITEABLE may be mapped into the user 867 * address space of a process running on another cpu. A 868 * user process (without holding the MP lock) running on 869 * another cpu may be able to touch the page while we are 870 * trying to remove it. vm_page_cache() will handle this 871 * case for us. 872 */ 873 if (m->dirty == 0) { 874 vm_page_test_dirty(m); 875 } else { 876 vm_page_dirty(m); 877 } 878 879 if (m->valid == 0) { 880 /* 881 * Invalid pages can be easily freed 882 */ 883 vm_pageout_page_free(m); 884 mycpu->gd_cnt.v_dfree++; 885 --inactive_shortage; 886 } else if (m->dirty == 0) { 887 /* 888 * Clean pages can be placed onto the cache queue. 889 * This effectively frees them. 890 */ 891 vm_page_busy(m); 892 vm_page_cache(m); 893 --inactive_shortage; 894 } else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) { 895 /* 896 * Dirty pages need to be paged out, but flushing 897 * a page is extremely expensive verses freeing 898 * a clean page. Rather then artificially limiting 899 * the number of pages we can flush, we instead give 900 * dirty pages extra priority on the inactive queue 901 * by forcing them to be cycled through the queue 902 * twice before being flushed, after which the 903 * (now clean) page will cycle through once more 904 * before being freed. This significantly extends 905 * the thrash point for a heavily loaded machine. 906 */ 907 vm_page_flag_set(m, PG_WINATCFLS); 908 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 909 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 910 ++vm_swapcache_inactive_heuristic; 911 } else if (maxlaunder > 0) { 912 /* 913 * We always want to try to flush some dirty pages if 914 * we encounter them, to keep the system stable. 915 * Normally this number is small, but under extreme 916 * pressure where there are insufficient clean pages 917 * on the inactive queue, we may have to go all out. 918 */ 919 int swap_pageouts_ok; 920 struct vnode *vp = NULL; 921 922 object = m->object; 923 924 if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) { 925 swap_pageouts_ok = 1; 926 } else { 927 swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts); 928 swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts && 929 vm_page_count_min(0)); 930 931 } 932 933 /* 934 * We don't bother paging objects that are "dead". 935 * Those objects are in a "rundown" state. 936 */ 937 if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) { 938 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 939 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 940 ++vm_swapcache_inactive_heuristic; 941 continue; 942 } 943 944 /* 945 * The object is already known NOT to be dead. It 946 * is possible for the vget() to block the whole 947 * pageout daemon, but the new low-memory handling 948 * code should prevent it. 949 * 950 * The previous code skipped locked vnodes and, worse, 951 * reordered pages in the queue. This results in 952 * completely non-deterministic operation because, 953 * quite often, a vm_fault has initiated an I/O and 954 * is holding a locked vnode at just the point where 955 * the pageout daemon is woken up. 956 * 957 * We can't wait forever for the vnode lock, we might 958 * deadlock due to a vn_read() getting stuck in 959 * vm_wait while holding this vnode. We skip the 960 * vnode if we can't get it in a reasonable amount 961 * of time. 962 * 963 * vpfailed is used to (try to) avoid the case where 964 * a large number of pages are associated with a 965 * locked vnode, which could cause the pageout daemon 966 * to stall for an excessive amount of time. 967 */ 968 if (object->type == OBJT_VNODE) { 969 int flags; 970 971 vp = object->handle; 972 flags = LK_EXCLUSIVE | LK_NOOBJ; 973 if (vp == vpfailed) 974 flags |= LK_NOWAIT; 975 else 976 flags |= LK_TIMELOCK; 977 if (vget(vp, flags) != 0) { 978 vpfailed = vp; 979 ++pageout_lock_miss; 980 if (object->flags & OBJ_MIGHTBEDIRTY) 981 vnodes_skipped++; 982 continue; 983 } 984 985 /* 986 * The page might have been moved to another 987 * queue during potential blocking in vget() 988 * above. The page might have been freed and 989 * reused for another vnode. The object might 990 * have been reused for another vnode. 991 */ 992 if (m->queue != PQ_INACTIVE || 993 m->object != object || 994 object->handle != vp) { 995 if (object->flags & OBJ_MIGHTBEDIRTY) 996 vnodes_skipped++; 997 vput(vp); 998 continue; 999 } 1000 1001 /* 1002 * The page may have been busied during the 1003 * blocking in vput(); We don't move the 1004 * page back onto the end of the queue so that 1005 * statistics are more correct if we don't. 1006 */ 1007 if (m->busy || (m->flags & PG_BUSY)) { 1008 vput(vp); 1009 continue; 1010 } 1011 1012 /* 1013 * If the page has become held it might 1014 * be undergoing I/O, so skip it 1015 */ 1016 if (m->hold_count) { 1017 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 1018 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 1019 ++vm_swapcache_inactive_heuristic; 1020 if (object->flags & OBJ_MIGHTBEDIRTY) 1021 vnodes_skipped++; 1022 vput(vp); 1023 continue; 1024 } 1025 } 1026 1027 /* 1028 * If a page is dirty, then it is either being washed 1029 * (but not yet cleaned) or it is still in the 1030 * laundry. If it is still in the laundry, then we 1031 * start the cleaning operation. 1032 * 1033 * This operation may cluster, invalidating the 'next' 1034 * pointer. To prevent an inordinate number of 1035 * restarts we use our marker to remember our place. 1036 * 1037 * decrement inactive_shortage on success to account 1038 * for the (future) cleaned page. Otherwise we 1039 * could wind up laundering or cleaning too many 1040 * pages. 1041 */ 1042 TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE].pl, m, &marker, pageq); 1043 if (vm_pageout_clean(m) != 0) { 1044 --inactive_shortage; 1045 --maxlaunder; 1046 } 1047 next = TAILQ_NEXT(&marker, pageq); 1048 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, &marker, pageq); 1049 if (vp != NULL) 1050 vput(vp); 1051 } 1052 } 1053 1054 /* 1055 * We want to move pages from the active queue to the inactive 1056 * queue to get the inactive queue to the inactive target. If 1057 * we still have a page shortage from above we try to directly free 1058 * clean pages instead of moving them. 1059 * 1060 * If we do still have a shortage we keep track of the number of 1061 * pages we free or cache (recycle_count) as a measure of thrashing 1062 * between the active and inactive queues. 1063 * 1064 * If we were able to completely satisfy the free+cache targets 1065 * from the inactive pool we limit the number of pages we move 1066 * from the active pool to the inactive pool to 2x the pages we 1067 * had removed from the inactive pool (with a minimum of 1/5 the 1068 * inactive target). If we were not able to completely satisfy 1069 * the free+cache targets we go for the whole target aggressively. 1070 * 1071 * NOTE: Both variables can end up negative. 1072 * NOTE: We are still in a critical section. 1073 */ 1074 active_shortage = vmstats.v_inactive_target - vmstats.v_inactive_count; 1075 if (inactive_original_shortage < vmstats.v_inactive_target / 10) 1076 inactive_original_shortage = vmstats.v_inactive_target / 10; 1077 if (inactive_shortage <= 0 && 1078 active_shortage > inactive_original_shortage * 2) { 1079 active_shortage = inactive_original_shortage * 2; 1080 } 1081 1082 pcount = vmstats.v_active_count; 1083 recycle_count = 0; 1084 m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl); 1085 1086 while ((m != NULL) && (pcount-- > 0) && 1087 (inactive_shortage > 0 || active_shortage > 0) 1088 ) { 1089 /* 1090 * If the page was ripped out from under us, just stop. 1091 */ 1092 if (m->queue != PQ_ACTIVE) 1093 break; 1094 next = TAILQ_NEXT(m, pageq); 1095 1096 /* 1097 * Don't deactivate pages that are busy. 1098 */ 1099 if ((m->busy != 0) || 1100 (m->flags & PG_BUSY) || 1101 (m->hold_count != 0)) { 1102 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1103 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1104 m = next; 1105 continue; 1106 } 1107 1108 /* 1109 * The count for pagedaemon pages is done after checking the 1110 * page for eligibility... 1111 */ 1112 mycpu->gd_cnt.v_pdpages++; 1113 1114 /* 1115 * Check to see "how much" the page has been used and clear 1116 * the tracking access bits. If the object has no references 1117 * don't bother paying the expense. 1118 */ 1119 actcount = 0; 1120 if (m->object->ref_count != 0) { 1121 if (m->flags & PG_REFERENCED) 1122 ++actcount; 1123 actcount += pmap_ts_referenced(m); 1124 if (actcount) { 1125 m->act_count += ACT_ADVANCE + actcount; 1126 if (m->act_count > ACT_MAX) 1127 m->act_count = ACT_MAX; 1128 } 1129 } 1130 vm_page_flag_clear(m, PG_REFERENCED); 1131 1132 /* 1133 * actcount is only valid if the object ref_count is non-zero. 1134 */ 1135 if (actcount && m->object->ref_count != 0) { 1136 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1137 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1138 } else { 1139 m->act_count -= min(m->act_count, ACT_DECLINE); 1140 if (vm_pageout_algorithm || 1141 m->object->ref_count == 0 || 1142 m->act_count < pass + 1 1143 ) { 1144 /* 1145 * Deactivate the page. If we had a 1146 * shortage from our inactive scan try to 1147 * free (cache) the page instead. 1148 * 1149 * Don't just blindly cache the page if 1150 * we do not have a shortage from the 1151 * inactive scan, that could lead to 1152 * gigabytes being moved. 1153 */ 1154 --active_shortage; 1155 if (inactive_shortage > 0 || 1156 m->object->ref_count == 0) { 1157 if (inactive_shortage > 0) 1158 ++recycle_count; 1159 vm_page_busy(m); 1160 vm_page_protect(m, VM_PROT_NONE); 1161 if (m->dirty == 0 && 1162 inactive_shortage > 0) { 1163 --inactive_shortage; 1164 vm_page_cache(m); 1165 } else { 1166 vm_page_deactivate(m); 1167 vm_page_wakeup(m); 1168 } 1169 } else { 1170 vm_page_deactivate(m); 1171 } 1172 } else { 1173 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1174 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1175 } 1176 } 1177 m = next; 1178 } 1179 1180 /* 1181 * The number of actually free pages can drop down to v_free_reserved, 1182 * we try to build the free count back above v_free_min. Note that 1183 * vm_paging_needed() also returns TRUE if v_free_count is not at 1184 * least v_free_min so that is the minimum we must build the free 1185 * count to. 1186 * 1187 * We use a slightly higher target to improve hysteresis, 1188 * ((v_free_target + v_free_min) / 2). Since v_free_target 1189 * is usually the same as v_cache_min this maintains about 1190 * half the pages in the free queue as are in the cache queue, 1191 * providing pretty good pipelining for pageout operation. 1192 * 1193 * The system operator can manipulate vm.v_cache_min and 1194 * vm.v_free_target to tune the pageout demon. Be sure 1195 * to keep vm.v_free_min < vm.v_free_target. 1196 * 1197 * Note that the original paging target is to get at least 1198 * (free_min + cache_min) into (free + cache). The slightly 1199 * higher target will shift additional pages from cache to free 1200 * without effecting the original paging target in order to 1201 * maintain better hysteresis and not have the free count always 1202 * be dead-on v_free_min. 1203 * 1204 * NOTE: we are still in a critical section. 1205 * 1206 * Pages moved from PQ_CACHE to totally free are not counted in the 1207 * pages_freed counter. 1208 */ 1209 while (vmstats.v_free_count < 1210 (vmstats.v_free_min + vmstats.v_free_target) / 2) { 1211 /* 1212 * 1213 */ 1214 static int cache_rover = 0; 1215 m = vm_page_list_find(PQ_CACHE, cache_rover, FALSE); 1216 if (m == NULL) 1217 break; 1218 if ((m->flags & (PG_BUSY|PG_UNMANAGED)) || 1219 m->busy || 1220 m->hold_count || 1221 m->wire_count) { 1222 #ifdef INVARIANTS 1223 kprintf("Warning: busy page %p found in cache\n", m); 1224 #endif 1225 vm_page_deactivate(m); 1226 continue; 1227 } 1228 KKASSERT((m->flags & PG_MAPPED) == 0); 1229 KKASSERT(m->dirty == 0); 1230 cache_rover = (cache_rover + PQ_PRIME2) & PQ_L2_MASK; 1231 vm_pageout_page_free(m); 1232 mycpu->gd_cnt.v_dfree++; 1233 } 1234 1235 #if !defined(NO_SWAPPING) 1236 /* 1237 * Idle process swapout -- run once per second. 1238 */ 1239 if (vm_swap_idle_enabled) { 1240 static long lsec; 1241 if (time_second != lsec) { 1242 vm_pageout_req_swapout |= VM_SWAP_IDLE; 1243 vm_req_vmdaemon(); 1244 lsec = time_second; 1245 } 1246 } 1247 #endif 1248 1249 /* 1250 * If we didn't get enough free pages, and we have skipped a vnode 1251 * in a writeable object, wakeup the sync daemon. And kick swapout 1252 * if we did not get enough free pages. 1253 */ 1254 if (vm_paging_target() > 0) { 1255 if (vnodes_skipped && vm_page_count_min(0)) 1256 speedup_syncer(); 1257 #if !defined(NO_SWAPPING) 1258 if (vm_swap_enabled && vm_page_count_target()) { 1259 vm_req_vmdaemon(); 1260 vm_pageout_req_swapout |= VM_SWAP_NORMAL; 1261 } 1262 #endif 1263 } 1264 1265 /* 1266 * Handle catastrophic conditions. Under good conditions we should 1267 * be at the target, well beyond our minimum. If we could not even 1268 * reach our minimum the system is under heavy stress. 1269 * 1270 * Determine whether we have run out of memory. This occurs when 1271 * swap_pager_full is TRUE and the only pages left in the page 1272 * queues are dirty. We will still likely have page shortages. 1273 * 1274 * - swap_pager_full is set if insufficient swap was 1275 * available to satisfy a requested pageout. 1276 * 1277 * - the inactive queue is bloated (4 x size of active queue), 1278 * meaning it is unable to get rid of dirty pages and. 1279 * 1280 * - vm_page_count_min() without counting pages recycled from the 1281 * active queue (recycle_count) means we could not recover 1282 * enough pages to meet bare minimum needs. This test only 1283 * works if the inactive queue is bloated. 1284 * 1285 * - due to a positive inactive_shortage we shifted the remaining 1286 * dirty pages from the active queue to the inactive queue 1287 * trying to find clean ones to free. 1288 */ 1289 if (swap_pager_full && vm_page_count_min(recycle_count)) 1290 kprintf("Warning: system low on memory+swap!\n"); 1291 if (swap_pager_full && vm_page_count_min(recycle_count) && 1292 vmstats.v_inactive_count > vmstats.v_active_count * 4 && 1293 inactive_shortage > 0) { 1294 /* 1295 * Kill something. 1296 */ 1297 info.bigproc = NULL; 1298 info.bigsize = 0; 1299 allproc_scan(vm_pageout_scan_callback, &info); 1300 if (info.bigproc != NULL) { 1301 killproc(info.bigproc, "out of swap space"); 1302 info.bigproc->p_nice = PRIO_MIN; 1303 info.bigproc->p_usched->resetpriority( 1304 FIRST_LWP_IN_PROC(info.bigproc)); 1305 wakeup(&vmstats.v_free_count); 1306 PRELE(info.bigproc); 1307 } 1308 } 1309 return(inactive_shortage); 1310 } 1311 1312 /* 1313 * The caller must hold vm_token and proc_token. 1314 */ 1315 static int 1316 vm_pageout_scan_callback(struct proc *p, void *data) 1317 { 1318 struct vm_pageout_scan_info *info = data; 1319 vm_offset_t size; 1320 1321 /* 1322 * Never kill system processes or init. If we have configured swap 1323 * then try to avoid killing low-numbered pids. 1324 */ 1325 if ((p->p_flag & P_SYSTEM) || (p->p_pid == 1) || 1326 ((p->p_pid < 48) && (vm_swap_size != 0))) { 1327 return (0); 1328 } 1329 1330 /* 1331 * if the process is in a non-running type state, 1332 * don't touch it. 1333 */ 1334 if (p->p_stat != SACTIVE && p->p_stat != SSTOP) 1335 return (0); 1336 1337 /* 1338 * Get the approximate process size. Note that anonymous pages 1339 * with backing swap will be counted twice, but there should not 1340 * be too many such pages due to the stress the VM system is 1341 * under at this point. 1342 */ 1343 size = vmspace_anonymous_count(p->p_vmspace) + 1344 vmspace_swap_count(p->p_vmspace); 1345 1346 /* 1347 * If the this process is bigger than the biggest one 1348 * remember it. 1349 */ 1350 if (info->bigsize < size) { 1351 if (info->bigproc) 1352 PRELE(info->bigproc); 1353 PHOLD(p); 1354 info->bigproc = p; 1355 info->bigsize = size; 1356 } 1357 return(0); 1358 } 1359 1360 /* 1361 * This routine tries to maintain the pseudo LRU active queue, 1362 * so that during long periods of time where there is no paging, 1363 * that some statistic accumulation still occurs. This code 1364 * helps the situation where paging just starts to occur. 1365 * 1366 * The caller must hold vm_token. 1367 */ 1368 static void 1369 vm_pageout_page_stats(void) 1370 { 1371 vm_page_t m,next; 1372 int pcount,tpcount; /* Number of pages to check */ 1373 static int fullintervalcount = 0; 1374 int page_shortage; 1375 1376 page_shortage = 1377 (vmstats.v_inactive_target + vmstats.v_cache_max + vmstats.v_free_min) - 1378 (vmstats.v_free_count + vmstats.v_inactive_count + vmstats.v_cache_count); 1379 1380 if (page_shortage <= 0) 1381 return; 1382 1383 pcount = vmstats.v_active_count; 1384 fullintervalcount += vm_pageout_stats_interval; 1385 if (fullintervalcount < vm_pageout_full_stats_interval) { 1386 tpcount = (vm_pageout_stats_max * vmstats.v_active_count) / vmstats.v_page_count; 1387 if (pcount > tpcount) 1388 pcount = tpcount; 1389 } else { 1390 fullintervalcount = 0; 1391 } 1392 1393 m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl); 1394 while ((m != NULL) && (pcount-- > 0)) { 1395 int actcount; 1396 1397 if (m->queue != PQ_ACTIVE) { 1398 break; 1399 } 1400 1401 next = TAILQ_NEXT(m, pageq); 1402 /* 1403 * Don't deactivate pages that are busy. 1404 */ 1405 if ((m->busy != 0) || 1406 (m->flags & PG_BUSY) || 1407 (m->hold_count != 0)) { 1408 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1409 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1410 m = next; 1411 continue; 1412 } 1413 1414 actcount = 0; 1415 if (m->flags & PG_REFERENCED) { 1416 vm_page_flag_clear(m, PG_REFERENCED); 1417 actcount += 1; 1418 } 1419 1420 actcount += pmap_ts_referenced(m); 1421 if (actcount) { 1422 m->act_count += ACT_ADVANCE + actcount; 1423 if (m->act_count > ACT_MAX) 1424 m->act_count = ACT_MAX; 1425 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1426 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1427 } else { 1428 if (m->act_count == 0) { 1429 /* 1430 * We turn off page access, so that we have 1431 * more accurate RSS stats. We don't do this 1432 * in the normal page deactivation when the 1433 * system is loaded VM wise, because the 1434 * cost of the large number of page protect 1435 * operations would be higher than the value 1436 * of doing the operation. 1437 */ 1438 vm_page_busy(m); 1439 vm_page_protect(m, VM_PROT_NONE); 1440 vm_page_deactivate(m); 1441 vm_page_wakeup(m); 1442 } else { 1443 m->act_count -= min(m->act_count, ACT_DECLINE); 1444 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1445 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1446 } 1447 } 1448 1449 m = next; 1450 } 1451 } 1452 1453 /* 1454 * The caller must hold vm_token. 1455 */ 1456 static int 1457 vm_pageout_free_page_calc(vm_size_t count) 1458 { 1459 if (count < vmstats.v_page_count) 1460 return 0; 1461 /* 1462 * free_reserved needs to include enough for the largest swap pager 1463 * structures plus enough for any pv_entry structs when paging. 1464 */ 1465 if (vmstats.v_page_count > 1024) 1466 vmstats.v_free_min = 4 + (vmstats.v_page_count - 1024) / 200; 1467 else 1468 vmstats.v_free_min = 4; 1469 vmstats.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE + 1470 vmstats.v_interrupt_free_min; 1471 vmstats.v_free_reserved = vm_pageout_page_count + 1472 vmstats.v_pageout_free_min + (count / 768) + PQ_L2_SIZE; 1473 vmstats.v_free_severe = vmstats.v_free_min / 2; 1474 vmstats.v_free_min += vmstats.v_free_reserved; 1475 vmstats.v_free_severe += vmstats.v_free_reserved; 1476 return 1; 1477 } 1478 1479 1480 /* 1481 * vm_pageout is the high level pageout daemon. 1482 * 1483 * No requirements. 1484 */ 1485 static void 1486 vm_pageout_thread(void) 1487 { 1488 int pass; 1489 int inactive_shortage; 1490 1491 /* 1492 * Permanently hold vm_token. 1493 */ 1494 lwkt_gettoken(&vm_token); 1495 1496 /* 1497 * Initialize some paging parameters. 1498 */ 1499 curthread->td_flags |= TDF_SYSTHREAD; 1500 1501 vmstats.v_interrupt_free_min = 2; 1502 if (vmstats.v_page_count < 2000) 1503 vm_pageout_page_count = 8; 1504 1505 vm_pageout_free_page_calc(vmstats.v_page_count); 1506 1507 /* 1508 * v_free_target and v_cache_min control pageout hysteresis. Note 1509 * that these are more a measure of the VM cache queue hysteresis 1510 * then the VM free queue. Specifically, v_free_target is the 1511 * high water mark (free+cache pages). 1512 * 1513 * v_free_reserved + v_cache_min (mostly means v_cache_min) is the 1514 * low water mark, while v_free_min is the stop. v_cache_min must 1515 * be big enough to handle memory needs while the pageout daemon 1516 * is signalled and run to free more pages. 1517 */ 1518 if (vmstats.v_free_count > 6144) 1519 vmstats.v_free_target = 4 * vmstats.v_free_min + vmstats.v_free_reserved; 1520 else 1521 vmstats.v_free_target = 2 * vmstats.v_free_min + vmstats.v_free_reserved; 1522 1523 /* 1524 * NOTE: With the new buffer cache b_act_count we want the default 1525 * inactive target to be a percentage of available memory. 1526 * 1527 * The inactive target essentially determines the minimum 1528 * number of 'temporary' pages capable of caching one-time-use 1529 * files when the VM system is otherwise full of pages 1530 * belonging to multi-time-use files or active program data. 1531 * 1532 * NOTE: The inactive target is aggressively persued only if the 1533 * inactive queue becomes too small. If the inactive queue 1534 * is large enough to satisfy page movement to free+cache 1535 * then it is repopulated more slowly from the active queue. 1536 * This allows a general inactive_target default to be set. 1537 * 1538 * There is an issue here for processes which sit mostly idle 1539 * 'overnight', such as sshd, tcsh, and X. Any movement from 1540 * the active queue will eventually cause such pages to 1541 * recycle eventually causing a lot of paging in the morning. 1542 * To reduce the incidence of this pages cycled out of the 1543 * buffer cache are moved directly to the inactive queue if 1544 * they were only used once or twice. 1545 * 1546 * The vfs.vm_cycle_point sysctl can be used to adjust this. 1547 * Increasing the value (up to 64) increases the number of 1548 * buffer recyclements which go directly to the inactive queue. 1549 */ 1550 if (vmstats.v_free_count > 2048) { 1551 vmstats.v_cache_min = vmstats.v_free_target; 1552 vmstats.v_cache_max = 2 * vmstats.v_cache_min; 1553 } else { 1554 vmstats.v_cache_min = 0; 1555 vmstats.v_cache_max = 0; 1556 } 1557 vmstats.v_inactive_target = vmstats.v_free_count / 4; 1558 1559 /* XXX does not really belong here */ 1560 if (vm_page_max_wired == 0) 1561 vm_page_max_wired = vmstats.v_free_count / 3; 1562 1563 if (vm_pageout_stats_max == 0) 1564 vm_pageout_stats_max = vmstats.v_free_target; 1565 1566 /* 1567 * Set interval in seconds for stats scan. 1568 */ 1569 if (vm_pageout_stats_interval == 0) 1570 vm_pageout_stats_interval = 5; 1571 if (vm_pageout_full_stats_interval == 0) 1572 vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4; 1573 1574 1575 /* 1576 * Set maximum free per pass 1577 */ 1578 if (vm_pageout_stats_free_max == 0) 1579 vm_pageout_stats_free_max = 5; 1580 1581 swap_pager_swap_init(); 1582 pass = 0; 1583 1584 /* 1585 * The pageout daemon is never done, so loop forever. 1586 */ 1587 while (TRUE) { 1588 int error; 1589 1590 /* 1591 * Wait for an action request. If we timeout check to 1592 * see if paging is needed (in case the normal wakeup 1593 * code raced us). 1594 */ 1595 if (vm_pages_needed == 0) { 1596 error = tsleep(&vm_pages_needed, 1597 0, "psleep", 1598 vm_pageout_stats_interval * hz); 1599 if (error && 1600 vm_paging_needed() == 0 && 1601 vm_pages_needed == 0) { 1602 vm_pageout_page_stats(); 1603 continue; 1604 } 1605 vm_pages_needed = 1; 1606 } 1607 1608 mycpu->gd_cnt.v_pdwakeups++; 1609 1610 /* 1611 * Scan for pageout. Try to avoid thrashing the system 1612 * with activity. 1613 */ 1614 inactive_shortage = vm_pageout_scan(pass); 1615 if (inactive_shortage > 0) { 1616 ++pass; 1617 if (swap_pager_full) { 1618 /* 1619 * Running out of memory, catastrophic back-off 1620 * to one-second intervals. 1621 */ 1622 tsleep(&vm_pages_needed, 0, "pdelay", hz); 1623 } else if (pass < 10 && vm_pages_needed > 1) { 1624 /* 1625 * Normal operation, additional processes 1626 * have already kicked us. Retry immediately. 1627 */ 1628 } else if (pass < 10) { 1629 /* 1630 * Normal operation, fewer processes. Delay 1631 * a bit but allow wakeups. 1632 */ 1633 vm_pages_needed = 0; 1634 tsleep(&vm_pages_needed, 0, "pdelay", hz / 10); 1635 vm_pages_needed = 1; 1636 } else { 1637 /* 1638 * We've taken too many passes, forced delay. 1639 */ 1640 tsleep(&vm_pages_needed, 0, "pdelay", hz / 10); 1641 } 1642 } else { 1643 /* 1644 * Interlocked wakeup of waiters (non-optional) 1645 */ 1646 pass = 0; 1647 if (vm_pages_needed && !vm_page_count_min(0)) { 1648 wakeup(&vmstats.v_free_count); 1649 vm_pages_needed = 0; 1650 } 1651 } 1652 } 1653 } 1654 1655 static struct kproc_desc page_kp = { 1656 "pagedaemon", 1657 vm_pageout_thread, 1658 &pagethread 1659 }; 1660 SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, &page_kp) 1661 1662 1663 /* 1664 * Called after allocating a page out of the cache or free queue 1665 * to possibly wake the pagedaemon up to replentish our supply. 1666 * 1667 * We try to generate some hysteresis by waking the pagedaemon up 1668 * when our free+cache pages go below the free_min+cache_min level. 1669 * The pagedaemon tries to get the count back up to at least the 1670 * minimum, and through to the target level if possible. 1671 * 1672 * If the pagedaemon is already active bump vm_pages_needed as a hint 1673 * that there are even more requests pending. 1674 * 1675 * SMP races ok? 1676 * No requirements. 1677 */ 1678 void 1679 pagedaemon_wakeup(void) 1680 { 1681 if (vm_paging_needed() && curthread != pagethread) { 1682 if (vm_pages_needed == 0) { 1683 vm_pages_needed = 1; /* SMP race ok */ 1684 wakeup(&vm_pages_needed); 1685 } else if (vm_page_count_min(0)) { 1686 ++vm_pages_needed; /* SMP race ok */ 1687 } 1688 } 1689 } 1690 1691 #if !defined(NO_SWAPPING) 1692 1693 /* 1694 * SMP races ok? 1695 * No requirements. 1696 */ 1697 static void 1698 vm_req_vmdaemon(void) 1699 { 1700 static int lastrun = 0; 1701 1702 if ((ticks > (lastrun + hz)) || (ticks < lastrun)) { 1703 wakeup(&vm_daemon_needed); 1704 lastrun = ticks; 1705 } 1706 } 1707 1708 static int vm_daemon_callback(struct proc *p, void *data __unused); 1709 1710 /* 1711 * No requirements. 1712 */ 1713 static void 1714 vm_daemon(void) 1715 { 1716 /* 1717 * Permanently hold vm_token. 1718 */ 1719 lwkt_gettoken(&vm_token); 1720 1721 while (TRUE) { 1722 tsleep(&vm_daemon_needed, 0, "psleep", 0); 1723 if (vm_pageout_req_swapout) { 1724 swapout_procs(vm_pageout_req_swapout); 1725 vm_pageout_req_swapout = 0; 1726 } 1727 /* 1728 * scan the processes for exceeding their rlimits or if 1729 * process is swapped out -- deactivate pages 1730 */ 1731 allproc_scan(vm_daemon_callback, NULL); 1732 } 1733 } 1734 1735 /* 1736 * Caller must hold vm_token and proc_token. 1737 */ 1738 static int 1739 vm_daemon_callback(struct proc *p, void *data __unused) 1740 { 1741 vm_pindex_t limit, size; 1742 1743 /* 1744 * if this is a system process or if we have already 1745 * looked at this process, skip it. 1746 */ 1747 if (p->p_flag & (P_SYSTEM | P_WEXIT)) 1748 return (0); 1749 1750 /* 1751 * if the process is in a non-running type state, 1752 * don't touch it. 1753 */ 1754 if (p->p_stat != SACTIVE && p->p_stat != SSTOP) 1755 return (0); 1756 1757 /* 1758 * get a limit 1759 */ 1760 limit = OFF_TO_IDX(qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur, 1761 p->p_rlimit[RLIMIT_RSS].rlim_max)); 1762 1763 /* 1764 * let processes that are swapped out really be 1765 * swapped out. Set the limit to nothing to get as 1766 * many pages out to swap as possible. 1767 */ 1768 if (p->p_flag & P_SWAPPEDOUT) 1769 limit = 0; 1770 1771 size = vmspace_resident_count(p->p_vmspace); 1772 if (limit >= 0 && size >= limit) { 1773 vm_pageout_map_deactivate_pages( 1774 &p->p_vmspace->vm_map, limit); 1775 } 1776 return (0); 1777 } 1778 1779 #endif 1780