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