1 /* 2 * Copyright (c) 1991 Regents of the University of California. 3 * All rights reserved. 4 * Copyright (c) 1994 John S. Dyson 5 * All rights reserved. 6 * Copyright (c) 1994 David Greenman 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to Berkeley by 10 * The Mach Operating System project at Carnegie-Mellon University. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the University of 23 * California, Berkeley and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * from: @(#)vm_pageout.c 7.4 (Berkeley) 5/7/91 41 * 42 * 43 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 44 * All rights reserved. 45 * 46 * Authors: Avadis Tevanian, Jr., Michael Wayne Young 47 * 48 * Permission to use, copy, modify and distribute this software and 49 * its documentation is hereby granted, provided that both the copyright 50 * notice and this permission notice appear in all copies of the 51 * software, derivative works or modified versions, and any portions 52 * thereof, and that both notices appear in supporting documentation. 53 * 54 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 55 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 56 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 57 * 58 * Carnegie Mellon requests users of this software to return to 59 * 60 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 61 * School of Computer Science 62 * Carnegie Mellon University 63 * Pittsburgh PA 15213-3890 64 * 65 * any improvements or extensions that they make and grant Carnegie the 66 * rights to redistribute these changes. 67 */ 68 69 /* 70 * The proverbial page-out daemon. 71 */ 72 73 #include <sys/cdefs.h> 74 __FBSDID("$FreeBSD$"); 75 76 #include "opt_vm.h" 77 #include <sys/param.h> 78 #include <sys/systm.h> 79 #include <sys/kernel.h> 80 #include <sys/eventhandler.h> 81 #include <sys/lock.h> 82 #include <sys/mutex.h> 83 #include <sys/proc.h> 84 #include <sys/kthread.h> 85 #include <sys/ktr.h> 86 #include <sys/resourcevar.h> 87 #include <sys/sched.h> 88 #include <sys/signalvar.h> 89 #include <sys/vnode.h> 90 #include <sys/vmmeter.h> 91 #include <sys/sx.h> 92 #include <sys/sysctl.h> 93 94 #include <vm/vm.h> 95 #include <vm/vm_param.h> 96 #include <vm/vm_object.h> 97 #include <vm/vm_page.h> 98 #include <vm/vm_map.h> 99 #include <vm/vm_pageout.h> 100 #include <vm/vm_pager.h> 101 #include <vm/swap_pager.h> 102 #include <vm/vm_extern.h> 103 #include <vm/uma.h> 104 105 #include <machine/mutex.h> 106 107 /* 108 * System initialization 109 */ 110 111 /* the kernel process "vm_pageout"*/ 112 static void vm_pageout(void); 113 static int vm_pageout_clean(vm_page_t); 114 static void vm_pageout_page_free(vm_page_t); 115 static void vm_pageout_pmap_collect(void); 116 static void vm_pageout_scan(int pass); 117 static int vm_pageout_free_page_calc(vm_size_t count); 118 struct proc *pageproc; 119 120 static struct kproc_desc page_kp = { 121 "pagedaemon", 122 vm_pageout, 123 &pageproc 124 }; 125 SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, &page_kp) 126 127 #if !defined(NO_SWAPPING) 128 /* the kernel process "vm_daemon"*/ 129 static void vm_daemon(void); 130 static struct proc *vmproc; 131 132 static struct kproc_desc vm_kp = { 133 "vmdaemon", 134 vm_daemon, 135 &vmproc 136 }; 137 SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp) 138 #endif 139 140 141 int vm_pages_needed; /* Event on which pageout daemon sleeps */ 142 int vm_pageout_deficit; /* Estimated number of pages deficit */ 143 int vm_pageout_pages_needed; /* flag saying that the pageout daemon needs pages */ 144 145 #if !defined(NO_SWAPPING) 146 static int vm_pageout_req_swapout; /* XXX */ 147 static int vm_daemon_needed; 148 #endif 149 static int vm_max_launder = 32; 150 static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0; 151 static int vm_pageout_full_stats_interval = 0; 152 static int vm_pageout_stats_free_max=0, vm_pageout_algorithm=0; 153 static int defer_swap_pageouts=0; 154 static int disable_swap_pageouts=0; 155 156 #if defined(NO_SWAPPING) 157 static int vm_swap_enabled=0; 158 static int vm_swap_idle_enabled=0; 159 #else 160 static int vm_swap_enabled=1; 161 static int vm_swap_idle_enabled=0; 162 #endif 163 164 SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm, 165 CTLFLAG_RW, &vm_pageout_algorithm, 0, "LRU page mgmt"); 166 167 SYSCTL_INT(_vm, OID_AUTO, max_launder, 168 CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout"); 169 170 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max, 171 CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length"); 172 173 SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval, 174 CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan"); 175 176 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval, 177 CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan"); 178 179 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_free_max, 180 CTLFLAG_RW, &vm_pageout_stats_free_max, 0, "Not implemented"); 181 182 #if defined(NO_SWAPPING) 183 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 184 CTLFLAG_RD, &vm_swap_enabled, 0, ""); 185 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 186 CTLFLAG_RD, &vm_swap_idle_enabled, 0, ""); 187 #else 188 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 189 CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout"); 190 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 191 CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria"); 192 #endif 193 194 SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts, 195 CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem"); 196 197 SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts, 198 CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages"); 199 200 static int pageout_lock_miss; 201 SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss, 202 CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout"); 203 204 #define VM_PAGEOUT_PAGE_COUNT 16 205 int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT; 206 207 int vm_page_max_wired; /* XXX max # of wired pages system-wide */ 208 209 #if !defined(NO_SWAPPING) 210 typedef void freeer_fcn_t(vm_map_t, vm_object_t, vm_pindex_t); 211 static void vm_pageout_map_deactivate_pages(vm_map_t, vm_pindex_t); 212 static freeer_fcn_t vm_pageout_object_deactivate_pages; 213 static void vm_req_vmdaemon(void); 214 #endif 215 static void vm_pageout_page_stats(void); 216 217 /* 218 * vm_pageout_clean: 219 * 220 * Clean the page and remove it from the laundry. 221 * 222 * We set the busy bit to cause potential page faults on this page to 223 * block. Note the careful timing, however, the busy bit isn't set till 224 * late and we cannot do anything that will mess with the page. 225 */ 226 static int 227 vm_pageout_clean(m) 228 vm_page_t m; 229 { 230 vm_object_t object; 231 vm_page_t mc[2*vm_pageout_page_count]; 232 int pageout_count; 233 int ib, is, page_base; 234 vm_pindex_t pindex = m->pindex; 235 236 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 237 238 object = m->object; 239 240 /* 241 * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP 242 * with the new swapper, but we could have serious problems paging 243 * out other object types if there is insufficient memory. 244 * 245 * Unfortunately, checking free memory here is far too late, so the 246 * check has been moved up a procedural level. 247 */ 248 249 /* 250 * Don't mess with the page if it's busy, held, or special 251 */ 252 if ((m->hold_count != 0) || 253 ((m->busy != 0) || (m->flags & (PG_BUSY|PG_UNMANAGED)))) { 254 return 0; 255 } 256 257 mc[vm_pageout_page_count] = m; 258 pageout_count = 1; 259 page_base = vm_pageout_page_count; 260 ib = 1; 261 is = 1; 262 263 /* 264 * Scan object for clusterable pages. 265 * 266 * We can cluster ONLY if: ->> the page is NOT 267 * clean, wired, busy, held, or mapped into a 268 * buffer, and one of the following: 269 * 1) The page is inactive, or a seldom used 270 * active page. 271 * -or- 272 * 2) we force the issue. 273 * 274 * During heavy mmap/modification loads the pageout 275 * daemon can really fragment the underlying file 276 * due to flushing pages out of order and not trying 277 * align the clusters (which leave sporatic out-of-order 278 * holes). To solve this problem we do the reverse scan 279 * first and attempt to align our cluster, then do a 280 * forward scan if room remains. 281 */ 282 more: 283 while (ib && pageout_count < vm_pageout_page_count) { 284 vm_page_t p; 285 286 if (ib > pindex) { 287 ib = 0; 288 break; 289 } 290 291 if ((p = vm_page_lookup(object, pindex - ib)) == NULL) { 292 ib = 0; 293 break; 294 } 295 if (((p->queue - p->pc) == PQ_CACHE) || 296 (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) { 297 ib = 0; 298 break; 299 } 300 vm_page_test_dirty(p); 301 if ((p->dirty & p->valid) == 0 || 302 p->queue != PQ_INACTIVE || 303 p->wire_count != 0 || /* may be held by buf cache */ 304 p->hold_count != 0) { /* may be undergoing I/O */ 305 ib = 0; 306 break; 307 } 308 mc[--page_base] = p; 309 ++pageout_count; 310 ++ib; 311 /* 312 * alignment boundry, stop here and switch directions. Do 313 * not clear ib. 314 */ 315 if ((pindex - (ib - 1)) % vm_pageout_page_count == 0) 316 break; 317 } 318 319 while (pageout_count < vm_pageout_page_count && 320 pindex + is < object->size) { 321 vm_page_t p; 322 323 if ((p = vm_page_lookup(object, pindex + is)) == NULL) 324 break; 325 if (((p->queue - p->pc) == PQ_CACHE) || 326 (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) { 327 break; 328 } 329 vm_page_test_dirty(p); 330 if ((p->dirty & p->valid) == 0 || 331 p->queue != PQ_INACTIVE || 332 p->wire_count != 0 || /* may be held by buf cache */ 333 p->hold_count != 0) { /* may be undergoing I/O */ 334 break; 335 } 336 mc[page_base + pageout_count] = p; 337 ++pageout_count; 338 ++is; 339 } 340 341 /* 342 * If we exhausted our forward scan, continue with the reverse scan 343 * when possible, even past a page boundry. This catches boundry 344 * conditions. 345 */ 346 if (ib && pageout_count < vm_pageout_page_count) 347 goto more; 348 349 /* 350 * we allow reads during pageouts... 351 */ 352 return vm_pageout_flush(&mc[page_base], pageout_count, 0, FALSE); 353 } 354 355 /* 356 * vm_pageout_flush() - launder the given pages 357 * 358 * The given pages are laundered. Note that we setup for the start of 359 * I/O ( i.e. busy the page ), mark it read-only, and bump the object 360 * reference count all in here rather then in the parent. If we want 361 * the parent to do more sophisticated things we may have to change 362 * the ordering. 363 */ 364 int 365 vm_pageout_flush(mc, count, flags, is_object_locked) 366 vm_page_t *mc; 367 int count; 368 int flags; 369 int is_object_locked; 370 { 371 vm_object_t object; 372 int pageout_status[count]; 373 int numpagedout = 0; 374 int i; 375 376 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 377 /* 378 * Initiate I/O. Bump the vm_page_t->busy counter and 379 * mark the pages read-only. 380 * 381 * We do not have to fixup the clean/dirty bits here... we can 382 * allow the pager to do it after the I/O completes. 383 * 384 * NOTE! mc[i]->dirty may be partial or fragmented due to an 385 * edge case with file fragments. 386 */ 387 for (i = 0; i < count; i++) { 388 KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL, ("vm_pageout_flush page %p index %d/%d: partially invalid page", mc[i], i, count)); 389 vm_page_io_start(mc[i]); 390 pmap_page_protect(mc[i], VM_PROT_READ); 391 } 392 object = mc[0]->object; 393 vm_page_unlock_queues(); 394 if (!is_object_locked) 395 VM_OBJECT_LOCK(object); 396 vm_object_pip_add(object, count); 397 VM_OBJECT_UNLOCK(object); 398 399 vm_pager_put_pages(object, mc, count, 400 (flags | ((object == kernel_object) ? VM_PAGER_PUT_SYNC : 0)), 401 pageout_status); 402 403 VM_OBJECT_LOCK(object); 404 vm_page_lock_queues(); 405 for (i = 0; i < count; i++) { 406 vm_page_t mt = mc[i]; 407 408 switch (pageout_status[i]) { 409 case VM_PAGER_OK: 410 case VM_PAGER_PEND: 411 numpagedout++; 412 break; 413 case VM_PAGER_BAD: 414 /* 415 * Page outside of range of object. Right now we 416 * essentially lose the changes by pretending it 417 * worked. 418 */ 419 pmap_clear_modify(mt); 420 vm_page_undirty(mt); 421 break; 422 case VM_PAGER_ERROR: 423 case VM_PAGER_FAIL: 424 /* 425 * If page couldn't be paged out, then reactivate the 426 * page so it doesn't clog the inactive list. (We 427 * will try paging out it again later). 428 */ 429 vm_page_activate(mt); 430 break; 431 case VM_PAGER_AGAIN: 432 break; 433 } 434 435 /* 436 * If the operation is still going, leave the page busy to 437 * block all other accesses. Also, leave the paging in 438 * progress indicator set so that we don't attempt an object 439 * collapse. 440 */ 441 if (pageout_status[i] != VM_PAGER_PEND) { 442 vm_object_pip_wakeup(object); 443 vm_page_io_finish(mt); 444 if (!vm_page_count_severe() || !vm_page_try_to_cache(mt)) 445 pmap_page_protect(mt, VM_PROT_READ); 446 } 447 } 448 if (!is_object_locked) 449 VM_OBJECT_UNLOCK(object); 450 return numpagedout; 451 } 452 453 #if !defined(NO_SWAPPING) 454 /* 455 * vm_pageout_object_deactivate_pages 456 * 457 * deactivate enough pages to satisfy the inactive target 458 * requirements or if vm_page_proc_limit is set, then 459 * deactivate all of the pages in the object and its 460 * backing_objects. 461 * 462 * The object and map must be locked. 463 */ 464 static void 465 vm_pageout_object_deactivate_pages(map, object, desired) 466 vm_map_t map; 467 vm_object_t object; 468 vm_pindex_t desired; 469 { 470 vm_page_t p, next; 471 int actcount, rcount, remove_mode; 472 473 GIANT_REQUIRED; 474 if (object->type == OBJT_DEVICE || object->type == OBJT_PHYS) 475 return; 476 477 while (object) { 478 if (pmap_resident_count(vm_map_pmap(map)) <= desired) 479 return; 480 if (object->paging_in_progress) 481 return; 482 483 remove_mode = 0; 484 if (object->shadow_count > 1) 485 remove_mode = 1; 486 /* 487 * scan the objects entire memory queue 488 */ 489 rcount = object->resident_page_count; 490 p = TAILQ_FIRST(&object->memq); 491 vm_page_lock_queues(); 492 while (p && (rcount-- > 0)) { 493 if (pmap_resident_count(map->pmap) <= desired) { 494 vm_page_unlock_queues(); 495 return; 496 } 497 next = TAILQ_NEXT(p, listq); 498 cnt.v_pdpages++; 499 if (p->wire_count != 0 || 500 p->hold_count != 0 || 501 p->busy != 0 || 502 (p->flags & (PG_BUSY|PG_UNMANAGED)) || 503 !pmap_page_exists_quick(vm_map_pmap(map), p)) { 504 p = next; 505 continue; 506 } 507 actcount = pmap_ts_referenced(p); 508 if (actcount) { 509 vm_page_flag_set(p, PG_REFERENCED); 510 } else if (p->flags & PG_REFERENCED) { 511 actcount = 1; 512 } 513 if ((p->queue != PQ_ACTIVE) && 514 (p->flags & PG_REFERENCED)) { 515 vm_page_activate(p); 516 p->act_count += actcount; 517 vm_page_flag_clear(p, PG_REFERENCED); 518 } else if (p->queue == PQ_ACTIVE) { 519 if ((p->flags & PG_REFERENCED) == 0) { 520 p->act_count -= min(p->act_count, ACT_DECLINE); 521 if (!remove_mode && (vm_pageout_algorithm || (p->act_count == 0))) { 522 pmap_remove_all(p); 523 vm_page_deactivate(p); 524 } else { 525 vm_pageq_requeue(p); 526 } 527 } else { 528 vm_page_activate(p); 529 vm_page_flag_clear(p, PG_REFERENCED); 530 if (p->act_count < (ACT_MAX - ACT_ADVANCE)) 531 p->act_count += ACT_ADVANCE; 532 vm_pageq_requeue(p); 533 } 534 } else if (p->queue == PQ_INACTIVE) { 535 pmap_remove_all(p); 536 } 537 p = next; 538 } 539 vm_page_unlock_queues(); 540 object = object->backing_object; 541 } 542 } 543 544 /* 545 * deactivate some number of pages in a map, try to do it fairly, but 546 * that is really hard to do. 547 */ 548 static void 549 vm_pageout_map_deactivate_pages(map, desired) 550 vm_map_t map; 551 vm_pindex_t desired; 552 { 553 vm_map_entry_t tmpe; 554 vm_object_t obj, bigobj; 555 int nothingwired; 556 557 GIANT_REQUIRED; 558 if (!vm_map_trylock(map)) 559 return; 560 561 bigobj = NULL; 562 nothingwired = TRUE; 563 564 /* 565 * first, search out the biggest object, and try to free pages from 566 * that. 567 */ 568 tmpe = map->header.next; 569 while (tmpe != &map->header) { 570 if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 571 obj = tmpe->object.vm_object; 572 if ((obj != NULL) && (obj->shadow_count <= 1) && 573 ((bigobj == NULL) || 574 (bigobj->resident_page_count < obj->resident_page_count))) { 575 bigobj = obj; 576 } 577 } 578 if (tmpe->wired_count > 0) 579 nothingwired = FALSE; 580 tmpe = tmpe->next; 581 } 582 583 if (bigobj) 584 vm_pageout_object_deactivate_pages(map, bigobj, desired); 585 586 /* 587 * Next, hunt around for other pages to deactivate. We actually 588 * do this search sort of wrong -- .text first is not the best idea. 589 */ 590 tmpe = map->header.next; 591 while (tmpe != &map->header) { 592 if (pmap_resident_count(vm_map_pmap(map)) <= desired) 593 break; 594 if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 595 obj = tmpe->object.vm_object; 596 if (obj) 597 vm_pageout_object_deactivate_pages(map, obj, desired); 598 } 599 tmpe = tmpe->next; 600 } 601 602 /* 603 * Remove all mappings if a process is swapped out, this will free page 604 * table pages. 605 */ 606 if (desired == 0 && nothingwired) { 607 vm_page_lock_queues(); 608 pmap_remove(vm_map_pmap(map), vm_map_min(map), 609 vm_map_max(map)); 610 vm_page_unlock_queues(); 611 } 612 vm_map_unlock(map); 613 } 614 #endif /* !defined(NO_SWAPPING) */ 615 616 /* 617 * Warning! The page queue lock is released and reacquired. 618 */ 619 static void 620 vm_pageout_page_free(vm_page_t m) 621 { 622 vm_object_t object = m->object; 623 624 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 625 vm_page_busy(m); 626 vm_page_unlock_queues(); 627 /* 628 * Avoid a lock order reversal. The page must be busy. 629 */ 630 VM_OBJECT_LOCK(object); 631 vm_page_lock_queues(); 632 pmap_remove_all(m); 633 vm_page_free(m); 634 VM_OBJECT_UNLOCK(object); 635 cnt.v_dfree++; 636 } 637 638 /* 639 * This routine is very drastic, but can save the system 640 * in a pinch. 641 */ 642 static void 643 vm_pageout_pmap_collect(void) 644 { 645 int i; 646 vm_page_t m; 647 static int warningdone; 648 649 if (pmap_pagedaemon_waken == 0) 650 return; 651 if (warningdone < 5) { 652 printf("collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n"); 653 warningdone++; 654 } 655 vm_page_lock_queues(); 656 for (i = 0; i < vm_page_array_size; i++) { 657 m = &vm_page_array[i]; 658 if (m->wire_count || m->hold_count || m->busy || 659 (m->flags & (PG_BUSY | PG_UNMANAGED))) 660 continue; 661 pmap_remove_all(m); 662 } 663 vm_page_unlock_queues(); 664 pmap_pagedaemon_waken = 0; 665 } 666 667 /* 668 * vm_pageout_scan does the dirty work for the pageout daemon. 669 */ 670 static void 671 vm_pageout_scan(int pass) 672 { 673 vm_page_t m, next; 674 struct vm_page marker; 675 int save_page_shortage; 676 int save_inactive_count; 677 int page_shortage, maxscan, pcount; 678 int addl_page_shortage, addl_page_shortage_init; 679 struct proc *p, *bigproc; 680 vm_offset_t size, bigsize; 681 vm_object_t object; 682 int actcount; 683 int vnodes_skipped = 0; 684 int maxlaunder; 685 int s; 686 struct thread *td; 687 688 GIANT_REQUIRED; 689 /* 690 * Decrease registered cache sizes. 691 */ 692 EVENTHANDLER_INVOKE(vm_lowmem, 0); 693 /* 694 * We do this explicitly after the caches have been drained above. 695 */ 696 uma_reclaim(); 697 /* 698 * Do whatever cleanup that the pmap code can. 699 */ 700 vm_pageout_pmap_collect(); 701 702 addl_page_shortage_init = atomic_readandclear_int(&vm_pageout_deficit); 703 704 /* 705 * Calculate the number of pages we want to either free or move 706 * to the cache. 707 */ 708 page_shortage = vm_paging_target() + addl_page_shortage_init; 709 save_page_shortage = page_shortage; 710 save_inactive_count = cnt.v_inactive_count; 711 712 /* 713 * Initialize our marker 714 */ 715 bzero(&marker, sizeof(marker)); 716 marker.flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER; 717 marker.queue = PQ_INACTIVE; 718 marker.wire_count = 1; 719 720 /* 721 * Start scanning the inactive queue for pages we can move to the 722 * cache or free. The scan will stop when the target is reached or 723 * we have scanned the entire inactive queue. Note that m->act_count 724 * is not used to form decisions for the inactive queue, only for the 725 * active queue. 726 * 727 * maxlaunder limits the number of dirty pages we flush per scan. 728 * For most systems a smaller value (16 or 32) is more robust under 729 * extreme memory and disk pressure because any unnecessary writes 730 * to disk can result in extreme performance degredation. However, 731 * systems with excessive dirty pages (especially when MAP_NOSYNC is 732 * used) will die horribly with limited laundering. If the pageout 733 * daemon cannot clean enough pages in the first pass, we let it go 734 * all out in succeeding passes. 735 */ 736 if ((maxlaunder = vm_max_launder) <= 1) 737 maxlaunder = 1; 738 if (pass) 739 maxlaunder = 10000; 740 rescan0: 741 addl_page_shortage = addl_page_shortage_init; 742 maxscan = cnt.v_inactive_count; 743 744 for (m = TAILQ_FIRST(&vm_page_queues[PQ_INACTIVE].pl); 745 m != NULL && maxscan-- > 0 && page_shortage > 0; 746 m = next) { 747 748 cnt.v_pdpages++; 749 750 if (m->queue != PQ_INACTIVE) { 751 goto rescan0; 752 } 753 754 next = TAILQ_NEXT(m, pageq); 755 756 /* 757 * skip marker pages 758 */ 759 if (m->flags & PG_MARKER) 760 continue; 761 762 /* 763 * A held page may be undergoing I/O, so skip it. 764 */ 765 if (m->hold_count) { 766 vm_pageq_requeue(m); 767 addl_page_shortage++; 768 continue; 769 } 770 /* 771 * Don't mess with busy pages, keep in the front of the 772 * queue, most likely are being paged out. 773 */ 774 if (m->busy || (m->flags & PG_BUSY)) { 775 addl_page_shortage++; 776 continue; 777 } 778 779 vm_page_lock_queues(); 780 /* 781 * If the object is not being used, we ignore previous 782 * references. 783 */ 784 if (m->object->ref_count == 0) { 785 vm_page_flag_clear(m, PG_REFERENCED); 786 pmap_clear_reference(m); 787 788 /* 789 * Otherwise, if the page has been referenced while in the 790 * inactive queue, we bump the "activation count" upwards, 791 * making it less likely that the page will be added back to 792 * the inactive queue prematurely again. Here we check the 793 * page tables (or emulated bits, if any), given the upper 794 * level VM system not knowing anything about existing 795 * references. 796 */ 797 } else if (((m->flags & PG_REFERENCED) == 0) && 798 (actcount = pmap_ts_referenced(m))) { 799 vm_page_activate(m); 800 vm_page_unlock_queues(); 801 m->act_count += (actcount + ACT_ADVANCE); 802 continue; 803 } 804 805 /* 806 * If the upper level VM system knows about any page 807 * references, we activate the page. We also set the 808 * "activation count" higher than normal so that we will less 809 * likely place pages back onto the inactive queue again. 810 */ 811 if ((m->flags & PG_REFERENCED) != 0) { 812 vm_page_flag_clear(m, PG_REFERENCED); 813 actcount = pmap_ts_referenced(m); 814 vm_page_activate(m); 815 vm_page_unlock_queues(); 816 m->act_count += (actcount + ACT_ADVANCE + 1); 817 continue; 818 } 819 820 /* 821 * If the upper level VM system doesn't know anything about 822 * the page being dirty, we have to check for it again. As 823 * far as the VM code knows, any partially dirty pages are 824 * fully dirty. 825 */ 826 if (m->dirty == 0) { 827 vm_page_test_dirty(m); 828 } else { 829 vm_page_dirty(m); 830 } 831 vm_page_unlock_queues(); 832 833 /* 834 * Invalid pages can be easily freed 835 */ 836 if (m->valid == 0) { 837 vm_page_lock_queues(); 838 vm_pageout_page_free(m); 839 vm_page_unlock_queues(); 840 --page_shortage; 841 842 /* 843 * Clean pages can be placed onto the cache queue. This 844 * effectively frees them. 845 */ 846 } else if (m->dirty == 0) { 847 vm_page_lock_queues(); 848 vm_page_cache(m); 849 vm_page_unlock_queues(); 850 --page_shortage; 851 } else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) { 852 /* 853 * Dirty pages need to be paged out, but flushing 854 * a page is extremely expensive verses freeing 855 * a clean page. Rather then artificially limiting 856 * the number of pages we can flush, we instead give 857 * dirty pages extra priority on the inactive queue 858 * by forcing them to be cycled through the queue 859 * twice before being flushed, after which the 860 * (now clean) page will cycle through once more 861 * before being freed. This significantly extends 862 * the thrash point for a heavily loaded machine. 863 */ 864 vm_page_lock_queues(); 865 vm_page_flag_set(m, PG_WINATCFLS); 866 vm_pageq_requeue(m); 867 vm_page_unlock_queues(); 868 } else if (maxlaunder > 0) { 869 /* 870 * We always want to try to flush some dirty pages if 871 * we encounter them, to keep the system stable. 872 * Normally this number is small, but under extreme 873 * pressure where there are insufficient clean pages 874 * on the inactive queue, we may have to go all out. 875 */ 876 int swap_pageouts_ok; 877 struct vnode *vp = NULL; 878 struct mount *mp; 879 880 object = m->object; 881 882 if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) { 883 swap_pageouts_ok = 1; 884 } else { 885 swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts); 886 swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts && 887 vm_page_count_min()); 888 889 } 890 891 /* 892 * We don't bother paging objects that are "dead". 893 * Those objects are in a "rundown" state. 894 */ 895 if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) { 896 vm_pageq_requeue(m); 897 continue; 898 } 899 900 /* 901 * The object is already known NOT to be dead. It 902 * is possible for the vget() to block the whole 903 * pageout daemon, but the new low-memory handling 904 * code should prevent it. 905 * 906 * The previous code skipped locked vnodes and, worse, 907 * reordered pages in the queue. This results in 908 * completely non-deterministic operation and, on a 909 * busy system, can lead to extremely non-optimal 910 * pageouts. For example, it can cause clean pages 911 * to be freed and dirty pages to be moved to the end 912 * of the queue. Since dirty pages are also moved to 913 * the end of the queue once-cleaned, this gives 914 * way too large a weighting to defering the freeing 915 * of dirty pages. 916 * 917 * We can't wait forever for the vnode lock, we might 918 * deadlock due to a vn_read() getting stuck in 919 * vm_wait while holding this vnode. We skip the 920 * vnode if we can't get it in a reasonable amount 921 * of time. 922 */ 923 if (object->type == OBJT_VNODE) { 924 vp = object->handle; 925 926 mp = NULL; 927 if (vp->v_type == VREG) 928 vn_start_write(vp, &mp, V_NOWAIT); 929 if (vget(vp, LK_EXCLUSIVE|LK_TIMELOCK, curthread)) { 930 ++pageout_lock_miss; 931 vn_finished_write(mp); 932 if (object->flags & OBJ_MIGHTBEDIRTY) 933 vnodes_skipped++; 934 continue; 935 } 936 937 /* 938 * The page might have been moved to another 939 * queue during potential blocking in vget() 940 * above. The page might have been freed and 941 * reused for another vnode. The object might 942 * have been reused for another vnode. 943 */ 944 if (m->queue != PQ_INACTIVE || 945 m->object != object || 946 object->handle != vp) { 947 if (object->flags & OBJ_MIGHTBEDIRTY) 948 vnodes_skipped++; 949 vput(vp); 950 vn_finished_write(mp); 951 continue; 952 } 953 954 /* 955 * The page may have been busied during the 956 * blocking in vput(); We don't move the 957 * page back onto the end of the queue so that 958 * statistics are more correct if we don't. 959 */ 960 if (m->busy || (m->flags & PG_BUSY)) { 961 vput(vp); 962 vn_finished_write(mp); 963 continue; 964 } 965 966 /* 967 * If the page has become held it might 968 * be undergoing I/O, so skip it 969 */ 970 if (m->hold_count) { 971 vm_pageq_requeue(m); 972 if (object->flags & OBJ_MIGHTBEDIRTY) 973 vnodes_skipped++; 974 vput(vp); 975 vn_finished_write(mp); 976 continue; 977 } 978 } 979 980 /* 981 * If a page is dirty, then it is either being washed 982 * (but not yet cleaned) or it is still in the 983 * laundry. If it is still in the laundry, then we 984 * start the cleaning operation. 985 * 986 * This operation may cluster, invalidating the 'next' 987 * pointer. To prevent an inordinate number of 988 * restarts we use our marker to remember our place. 989 * 990 * decrement page_shortage on success to account for 991 * the (future) cleaned page. Otherwise we could wind 992 * up laundering or cleaning too many pages. 993 */ 994 vm_page_lock_queues(); 995 s = splvm(); 996 TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE].pl, m, &marker, pageq); 997 splx(s); 998 if (vm_pageout_clean(m) != 0) { 999 --page_shortage; 1000 --maxlaunder; 1001 } 1002 s = splvm(); 1003 next = TAILQ_NEXT(&marker, pageq); 1004 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, &marker, pageq); 1005 splx(s); 1006 vm_page_unlock_queues(); 1007 if (vp) { 1008 vput(vp); 1009 vn_finished_write(mp); 1010 } 1011 } 1012 } 1013 1014 /* 1015 * Compute the number of pages we want to try to move from the 1016 * active queue to the inactive queue. 1017 */ 1018 page_shortage = vm_paging_target() + 1019 cnt.v_inactive_target - cnt.v_inactive_count; 1020 page_shortage += addl_page_shortage; 1021 1022 vm_page_lock_queues(); 1023 /* 1024 * Scan the active queue for things we can deactivate. We nominally 1025 * track the per-page activity counter and use it to locate 1026 * deactivation candidates. 1027 */ 1028 pcount = cnt.v_active_count; 1029 m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl); 1030 1031 while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) { 1032 1033 /* 1034 * This is a consistency check, and should likely be a panic 1035 * or warning. 1036 */ 1037 if (m->queue != PQ_ACTIVE) { 1038 break; 1039 } 1040 1041 next = TAILQ_NEXT(m, pageq); 1042 /* 1043 * Don't deactivate pages that are busy. 1044 */ 1045 if ((m->busy != 0) || 1046 (m->flags & PG_BUSY) || 1047 (m->hold_count != 0)) { 1048 vm_pageq_requeue(m); 1049 m = next; 1050 continue; 1051 } 1052 1053 /* 1054 * The count for pagedaemon pages is done after checking the 1055 * page for eligibility... 1056 */ 1057 cnt.v_pdpages++; 1058 1059 /* 1060 * Check to see "how much" the page has been used. 1061 */ 1062 actcount = 0; 1063 if (m->object->ref_count != 0) { 1064 if (m->flags & PG_REFERENCED) { 1065 actcount += 1; 1066 } 1067 actcount += pmap_ts_referenced(m); 1068 if (actcount) { 1069 m->act_count += ACT_ADVANCE + actcount; 1070 if (m->act_count > ACT_MAX) 1071 m->act_count = ACT_MAX; 1072 } 1073 } 1074 1075 /* 1076 * Since we have "tested" this bit, we need to clear it now. 1077 */ 1078 vm_page_flag_clear(m, PG_REFERENCED); 1079 1080 /* 1081 * Only if an object is currently being used, do we use the 1082 * page activation count stats. 1083 */ 1084 if (actcount && (m->object->ref_count != 0)) { 1085 vm_pageq_requeue(m); 1086 } else { 1087 m->act_count -= min(m->act_count, ACT_DECLINE); 1088 if (vm_pageout_algorithm || 1089 m->object->ref_count == 0 || 1090 m->act_count == 0) { 1091 page_shortage--; 1092 if (m->object->ref_count == 0) { 1093 pmap_remove_all(m); 1094 if (m->dirty == 0) 1095 vm_page_cache(m); 1096 else 1097 vm_page_deactivate(m); 1098 } else { 1099 vm_page_deactivate(m); 1100 } 1101 } else { 1102 vm_pageq_requeue(m); 1103 } 1104 } 1105 m = next; 1106 } 1107 s = splvm(); 1108 1109 /* 1110 * We try to maintain some *really* free pages, this allows interrupt 1111 * code to be guaranteed space. Since both cache and free queues 1112 * are considered basically 'free', moving pages from cache to free 1113 * does not effect other calculations. 1114 */ 1115 while (cnt.v_free_count < cnt.v_free_reserved) { 1116 static int cache_rover = 0; 1117 m = vm_pageq_find(PQ_CACHE, cache_rover, FALSE); 1118 if (!m) 1119 break; 1120 if ((m->flags & (PG_BUSY|PG_UNMANAGED)) || 1121 m->busy || 1122 m->hold_count || 1123 m->wire_count) { 1124 #ifdef INVARIANTS 1125 printf("Warning: busy page %p found in cache\n", m); 1126 #endif 1127 vm_page_deactivate(m); 1128 continue; 1129 } 1130 cache_rover = (cache_rover + PQ_PRIME2) & PQ_L2_MASK; 1131 vm_pageout_page_free(m); 1132 } 1133 splx(s); 1134 vm_page_unlock_queues(); 1135 #if !defined(NO_SWAPPING) 1136 /* 1137 * Idle process swapout -- run once per second. 1138 */ 1139 if (vm_swap_idle_enabled) { 1140 static long lsec; 1141 if (time_second != lsec) { 1142 vm_pageout_req_swapout |= VM_SWAP_IDLE; 1143 vm_req_vmdaemon(); 1144 lsec = time_second; 1145 } 1146 } 1147 #endif 1148 1149 /* 1150 * If we didn't get enough free pages, and we have skipped a vnode 1151 * in a writeable object, wakeup the sync daemon. And kick swapout 1152 * if we did not get enough free pages. 1153 */ 1154 if (vm_paging_target() > 0) { 1155 if (vnodes_skipped && vm_page_count_min()) 1156 (void) speedup_syncer(); 1157 #if !defined(NO_SWAPPING) 1158 if (vm_swap_enabled && vm_page_count_target()) { 1159 vm_req_vmdaemon(); 1160 vm_pageout_req_swapout |= VM_SWAP_NORMAL; 1161 } 1162 #endif 1163 } 1164 1165 /* 1166 * If we are critically low on one of RAM or swap and low on 1167 * the other, kill the largest process. However, we avoid 1168 * doing this on the first pass in order to give ourselves a 1169 * chance to flush out dirty vnode-backed pages and to allow 1170 * active pages to be moved to the inactive queue and reclaimed. 1171 * 1172 * We keep the process bigproc locked once we find it to keep anyone 1173 * from messing with it; however, there is a possibility of 1174 * deadlock if process B is bigproc and one of it's child processes 1175 * attempts to propagate a signal to B while we are waiting for A's 1176 * lock while walking this list. To avoid this, we don't block on 1177 * the process lock but just skip a process if it is already locked. 1178 */ 1179 if (pass != 0 && 1180 ((vm_swap_size < 64 && vm_page_count_min()) || 1181 (swap_pager_full && vm_paging_target() > 0))) { 1182 bigproc = NULL; 1183 bigsize = 0; 1184 sx_slock(&allproc_lock); 1185 FOREACH_PROC_IN_SYSTEM(p) { 1186 int breakout; 1187 /* 1188 * If this process is already locked, skip it. 1189 */ 1190 if (PROC_TRYLOCK(p) == 0) 1191 continue; 1192 /* 1193 * If this is a system or protected process, skip it. 1194 */ 1195 if ((p->p_flag & P_SYSTEM) || (p->p_pid == 1) || 1196 (p->p_flag & P_PROTECTED) || 1197 ((p->p_pid < 48) && (vm_swap_size != 0))) { 1198 PROC_UNLOCK(p); 1199 continue; 1200 } 1201 /* 1202 * if the process is in a non-running type state, 1203 * don't touch it. Check all the threads individually. 1204 */ 1205 mtx_lock_spin(&sched_lock); 1206 breakout = 0; 1207 FOREACH_THREAD_IN_PROC(p, td) { 1208 if (!TD_ON_RUNQ(td) && 1209 !TD_IS_RUNNING(td) && 1210 !TD_IS_SLEEPING(td)) { 1211 breakout = 1; 1212 break; 1213 } 1214 } 1215 if (breakout) { 1216 mtx_unlock_spin(&sched_lock); 1217 PROC_UNLOCK(p); 1218 continue; 1219 } 1220 mtx_unlock_spin(&sched_lock); 1221 /* 1222 * get the process size 1223 */ 1224 if (!vm_map_trylock_read(&p->p_vmspace->vm_map)) { 1225 PROC_UNLOCK(p); 1226 continue; 1227 } 1228 size = vmspace_swap_count(p->p_vmspace); 1229 vm_map_unlock_read(&p->p_vmspace->vm_map); 1230 size += vmspace_resident_count(p->p_vmspace); 1231 /* 1232 * if the this process is bigger than the biggest one 1233 * remember it. 1234 */ 1235 if (size > bigsize) { 1236 if (bigproc != NULL) 1237 PROC_UNLOCK(bigproc); 1238 bigproc = p; 1239 bigsize = size; 1240 } else 1241 PROC_UNLOCK(p); 1242 } 1243 sx_sunlock(&allproc_lock); 1244 if (bigproc != NULL) { 1245 struct ksegrp *kg; 1246 killproc(bigproc, "out of swap space"); 1247 mtx_lock_spin(&sched_lock); 1248 FOREACH_KSEGRP_IN_PROC(bigproc, kg) { 1249 sched_nice(kg, PRIO_MIN); /* XXXKSE ??? */ 1250 } 1251 mtx_unlock_spin(&sched_lock); 1252 PROC_UNLOCK(bigproc); 1253 wakeup(&cnt.v_free_count); 1254 } 1255 } 1256 } 1257 1258 /* 1259 * This routine tries to maintain the pseudo LRU active queue, 1260 * so that during long periods of time where there is no paging, 1261 * that some statistic accumulation still occurs. This code 1262 * helps the situation where paging just starts to occur. 1263 */ 1264 static void 1265 vm_pageout_page_stats() 1266 { 1267 vm_page_t m,next; 1268 int pcount,tpcount; /* Number of pages to check */ 1269 static int fullintervalcount = 0; 1270 int page_shortage; 1271 int s0; 1272 1273 page_shortage = 1274 (cnt.v_inactive_target + cnt.v_cache_max + cnt.v_free_min) - 1275 (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count); 1276 1277 if (page_shortage <= 0) 1278 return; 1279 1280 s0 = splvm(); 1281 vm_page_lock_queues(); 1282 pcount = cnt.v_active_count; 1283 fullintervalcount += vm_pageout_stats_interval; 1284 if (fullintervalcount < vm_pageout_full_stats_interval) { 1285 tpcount = (vm_pageout_stats_max * cnt.v_active_count) / cnt.v_page_count; 1286 if (pcount > tpcount) 1287 pcount = tpcount; 1288 } else { 1289 fullintervalcount = 0; 1290 } 1291 1292 m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl); 1293 while ((m != NULL) && (pcount-- > 0)) { 1294 int actcount; 1295 1296 if (m->queue != PQ_ACTIVE) { 1297 break; 1298 } 1299 1300 next = TAILQ_NEXT(m, pageq); 1301 /* 1302 * Don't deactivate pages that are busy. 1303 */ 1304 if ((m->busy != 0) || 1305 (m->flags & PG_BUSY) || 1306 (m->hold_count != 0)) { 1307 vm_pageq_requeue(m); 1308 m = next; 1309 continue; 1310 } 1311 1312 actcount = 0; 1313 if (m->flags & PG_REFERENCED) { 1314 vm_page_flag_clear(m, PG_REFERENCED); 1315 actcount += 1; 1316 } 1317 1318 actcount += pmap_ts_referenced(m); 1319 if (actcount) { 1320 m->act_count += ACT_ADVANCE + actcount; 1321 if (m->act_count > ACT_MAX) 1322 m->act_count = ACT_MAX; 1323 vm_pageq_requeue(m); 1324 } else { 1325 if (m->act_count == 0) { 1326 /* 1327 * We turn off page access, so that we have 1328 * more accurate RSS stats. We don't do this 1329 * in the normal page deactivation when the 1330 * system is loaded VM wise, because the 1331 * cost of the large number of page protect 1332 * operations would be higher than the value 1333 * of doing the operation. 1334 */ 1335 pmap_remove_all(m); 1336 vm_page_deactivate(m); 1337 } else { 1338 m->act_count -= min(m->act_count, ACT_DECLINE); 1339 vm_pageq_requeue(m); 1340 } 1341 } 1342 1343 m = next; 1344 } 1345 vm_page_unlock_queues(); 1346 splx(s0); 1347 } 1348 1349 static int 1350 vm_pageout_free_page_calc(count) 1351 vm_size_t count; 1352 { 1353 if (count < cnt.v_page_count) 1354 return 0; 1355 /* 1356 * free_reserved needs to include enough for the largest swap pager 1357 * structures plus enough for any pv_entry structs when paging. 1358 */ 1359 if (cnt.v_page_count > 1024) 1360 cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200; 1361 else 1362 cnt.v_free_min = 4; 1363 cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE + 1364 cnt.v_interrupt_free_min; 1365 cnt.v_free_reserved = vm_pageout_page_count + 1366 cnt.v_pageout_free_min + (count / 768) + PQ_L2_SIZE; 1367 cnt.v_free_severe = cnt.v_free_min / 2; 1368 cnt.v_free_min += cnt.v_free_reserved; 1369 cnt.v_free_severe += cnt.v_free_reserved; 1370 return 1; 1371 } 1372 1373 /* 1374 * vm_pageout is the high level pageout daemon. 1375 */ 1376 static void 1377 vm_pageout() 1378 { 1379 int error, pass, s; 1380 1381 mtx_lock(&Giant); 1382 1383 /* 1384 * Initialize some paging parameters. 1385 */ 1386 cnt.v_interrupt_free_min = 2; 1387 if (cnt.v_page_count < 2000) 1388 vm_pageout_page_count = 8; 1389 1390 vm_pageout_free_page_calc(cnt.v_page_count); 1391 /* 1392 * v_free_target and v_cache_min control pageout hysteresis. Note 1393 * that these are more a measure of the VM cache queue hysteresis 1394 * then the VM free queue. Specifically, v_free_target is the 1395 * high water mark (free+cache pages). 1396 * 1397 * v_free_reserved + v_cache_min (mostly means v_cache_min) is the 1398 * low water mark, while v_free_min is the stop. v_cache_min must 1399 * be big enough to handle memory needs while the pageout daemon 1400 * is signalled and run to free more pages. 1401 */ 1402 if (cnt.v_free_count > 6144) 1403 cnt.v_free_target = 4 * cnt.v_free_min + cnt.v_free_reserved; 1404 else 1405 cnt.v_free_target = 2 * cnt.v_free_min + cnt.v_free_reserved; 1406 1407 if (cnt.v_free_count > 2048) { 1408 cnt.v_cache_min = cnt.v_free_target; 1409 cnt.v_cache_max = 2 * cnt.v_cache_min; 1410 cnt.v_inactive_target = (3 * cnt.v_free_target) / 2; 1411 } else { 1412 cnt.v_cache_min = 0; 1413 cnt.v_cache_max = 0; 1414 cnt.v_inactive_target = cnt.v_free_count / 4; 1415 } 1416 if (cnt.v_inactive_target > cnt.v_free_count / 3) 1417 cnt.v_inactive_target = cnt.v_free_count / 3; 1418 1419 /* XXX does not really belong here */ 1420 if (vm_page_max_wired == 0) 1421 vm_page_max_wired = cnt.v_free_count / 3; 1422 1423 if (vm_pageout_stats_max == 0) 1424 vm_pageout_stats_max = cnt.v_free_target; 1425 1426 /* 1427 * Set interval in seconds for stats scan. 1428 */ 1429 if (vm_pageout_stats_interval == 0) 1430 vm_pageout_stats_interval = 5; 1431 if (vm_pageout_full_stats_interval == 0) 1432 vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4; 1433 1434 /* 1435 * Set maximum free per pass 1436 */ 1437 if (vm_pageout_stats_free_max == 0) 1438 vm_pageout_stats_free_max = 5; 1439 1440 swap_pager_swap_init(); 1441 pass = 0; 1442 /* 1443 * The pageout daemon is never done, so loop forever. 1444 */ 1445 while (TRUE) { 1446 s = splvm(); 1447 vm_page_lock_queues(); 1448 /* 1449 * If we have enough free memory, wakeup waiters. Do 1450 * not clear vm_pages_needed until we reach our target, 1451 * otherwise we may be woken up over and over again and 1452 * waste a lot of cpu. 1453 */ 1454 if (vm_pages_needed && !vm_page_count_min()) { 1455 if (!vm_paging_needed()) 1456 vm_pages_needed = 0; 1457 wakeup(&cnt.v_free_count); 1458 } 1459 if (vm_pages_needed) { 1460 /* 1461 * Still not done, take a second pass without waiting 1462 * (unlimited dirty cleaning), otherwise sleep a bit 1463 * and try again. 1464 */ 1465 ++pass; 1466 if (pass > 1) 1467 msleep(&vm_pages_needed, &vm_page_queue_mtx, PVM, 1468 "psleep", hz/2); 1469 } else { 1470 /* 1471 * Good enough, sleep & handle stats. Prime the pass 1472 * for the next run. 1473 */ 1474 if (pass > 1) 1475 pass = 1; 1476 else 1477 pass = 0; 1478 error = msleep(&vm_pages_needed, &vm_page_queue_mtx, PVM, 1479 "psleep", vm_pageout_stats_interval * hz); 1480 if (error && !vm_pages_needed) { 1481 vm_page_unlock_queues(); 1482 splx(s); 1483 pass = 0; 1484 vm_pageout_page_stats(); 1485 continue; 1486 } 1487 } 1488 if (vm_pages_needed) 1489 cnt.v_pdwakeups++; 1490 vm_page_unlock_queues(); 1491 splx(s); 1492 vm_pageout_scan(pass); 1493 } 1494 } 1495 1496 /* 1497 * Unless the page queue lock is held by the caller, this function 1498 * should be regarded as advisory. Specifically, the caller should 1499 * not msleep() on &cnt.v_free_count following this function unless 1500 * the page queue lock is held until the msleep() is performed. 1501 */ 1502 void 1503 pagedaemon_wakeup() 1504 { 1505 1506 if (!vm_pages_needed && curthread->td_proc != pageproc) { 1507 vm_pages_needed = 1; 1508 wakeup(&vm_pages_needed); 1509 } 1510 } 1511 1512 #if !defined(NO_SWAPPING) 1513 static void 1514 vm_req_vmdaemon() 1515 { 1516 static int lastrun = 0; 1517 1518 if ((ticks > (lastrun + hz)) || (ticks < lastrun)) { 1519 wakeup(&vm_daemon_needed); 1520 lastrun = ticks; 1521 } 1522 } 1523 1524 static void 1525 vm_daemon() 1526 { 1527 struct proc *p; 1528 int breakout; 1529 struct thread *td; 1530 1531 mtx_lock(&Giant); 1532 while (TRUE) { 1533 tsleep(&vm_daemon_needed, PPAUSE, "psleep", 0); 1534 if (vm_pageout_req_swapout) { 1535 swapout_procs(vm_pageout_req_swapout); 1536 vm_pageout_req_swapout = 0; 1537 } 1538 /* 1539 * scan the processes for exceeding their rlimits or if 1540 * process is swapped out -- deactivate pages 1541 */ 1542 sx_slock(&allproc_lock); 1543 LIST_FOREACH(p, &allproc, p_list) { 1544 vm_pindex_t limit, size; 1545 1546 /* 1547 * if this is a system process or if we have already 1548 * looked at this process, skip it. 1549 */ 1550 PROC_LOCK(p); 1551 if (p->p_flag & (P_SYSTEM | P_WEXIT)) { 1552 PROC_UNLOCK(p); 1553 continue; 1554 } 1555 /* 1556 * if the process is in a non-running type state, 1557 * don't touch it. 1558 */ 1559 mtx_lock_spin(&sched_lock); 1560 breakout = 0; 1561 FOREACH_THREAD_IN_PROC(p, td) { 1562 if (!TD_ON_RUNQ(td) && 1563 !TD_IS_RUNNING(td) && 1564 !TD_IS_SLEEPING(td)) { 1565 breakout = 1; 1566 break; 1567 } 1568 } 1569 mtx_unlock_spin(&sched_lock); 1570 if (breakout) { 1571 PROC_UNLOCK(p); 1572 continue; 1573 } 1574 /* 1575 * get a limit 1576 */ 1577 limit = OFF_TO_IDX( 1578 qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur, 1579 p->p_rlimit[RLIMIT_RSS].rlim_max)); 1580 1581 /* 1582 * let processes that are swapped out really be 1583 * swapped out set the limit to nothing (will force a 1584 * swap-out.) 1585 */ 1586 if ((p->p_sflag & PS_INMEM) == 0) 1587 limit = 0; /* XXX */ 1588 PROC_UNLOCK(p); 1589 1590 size = vmspace_resident_count(p->p_vmspace); 1591 if (limit >= 0 && size >= limit) { 1592 vm_pageout_map_deactivate_pages( 1593 &p->p_vmspace->vm_map, limit); 1594 } 1595 } 1596 sx_sunlock(&allproc_lock); 1597 } 1598 } 1599 #endif /* !defined(NO_SWAPPING) */ 1600