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