vm_pageout.c revision 34206
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 * $Id: vm_pageout.c,v 1.117 1998/03/01 04:18:28 dyson Exp $ 69 */ 70 71/* 72 * The proverbial page-out daemon. 73 */ 74 75#include <sys/param.h> 76#include <sys/systm.h> 77#include <sys/kernel.h> 78#include <sys/proc.h> 79#include <sys/resourcevar.h> 80#include <sys/signalvar.h> 81#include <sys/vnode.h> 82#include <sys/vmmeter.h> 83#include <sys/sysctl.h> 84 85#include <vm/vm.h> 86#include <vm/vm_param.h> 87#include <vm/vm_prot.h> 88#include <sys/lock.h> 89#include <vm/vm_object.h> 90#include <vm/vm_page.h> 91#include <vm/vm_map.h> 92#include <vm/vm_pageout.h> 93#include <vm/vm_pager.h> 94#include <vm/swap_pager.h> 95#include <vm/vm_extern.h> 96 97/* 98 * System initialization 99 */ 100 101/* the kernel process "vm_pageout"*/ 102static void vm_pageout __P((void)); 103static int vm_pageout_clean __P((vm_page_t)); 104static int vm_pageout_scan __P((void)); 105static int vm_pageout_free_page_calc __P((vm_size_t count)); 106struct proc *pageproc; 107 108static struct kproc_desc page_kp = { 109 "pagedaemon", 110 vm_pageout, 111 &pageproc 112}; 113SYSINIT_KT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, &page_kp) 114 115#if !defined(NO_SWAPPING) 116/* the kernel process "vm_daemon"*/ 117static void vm_daemon __P((void)); 118static struct proc *vmproc; 119 120static struct kproc_desc vm_kp = { 121 "vmdaemon", 122 vm_daemon, 123 &vmproc 124}; 125SYSINIT_KT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp) 126#endif 127 128 129int vm_pages_needed=0; /* Event on which pageout daemon sleeps */ 130int vm_pageout_deficit=0; /* Estimated number of pages deficit */ 131int vm_pageout_pages_needed=0; /* flag saying that the pageout daemon needs pages */ 132 133extern int npendingio; 134#if !defined(NO_SWAPPING) 135static int vm_pageout_req_swapout; /* XXX */ 136static int vm_daemon_needed; 137#endif 138extern int nswiodone; 139extern int vm_swap_size; 140extern int vfs_update_wakeup; 141static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0; 142static int vm_pageout_full_stats_interval = 0; 143static int vm_pageout_stats_free_max=0, vm_pageout_algorithm_lru=0; 144static int defer_swap_pageouts=0; 145static int disable_swap_pageouts=0; 146 147static int max_page_launder=100; 148#if defined(NO_SWAPPING) 149static int vm_swap_enabled=0; 150static int vm_swap_idle_enabled=0; 151#else 152static int vm_swap_enabled=1; 153static int vm_swap_idle_enabled=0; 154#endif 155 156SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm, 157 CTLFLAG_RW, &vm_pageout_algorithm_lru, 0, ""); 158 159SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max, 160 CTLFLAG_RW, &vm_pageout_stats_max, 0, ""); 161 162SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval, 163 CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, ""); 164 165SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval, 166 CTLFLAG_RW, &vm_pageout_stats_interval, 0, ""); 167 168SYSCTL_INT(_vm, OID_AUTO, pageout_stats_free_max, 169 CTLFLAG_RW, &vm_pageout_stats_free_max, 0, ""); 170 171#if defined(NO_SWAPPING) 172SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 173 CTLFLAG_RD, &vm_swap_enabled, 0, ""); 174SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 175 CTLFLAG_RD, &vm_swap_idle_enabled, 0, ""); 176#else 177SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 178 CTLFLAG_RW, &vm_swap_enabled, 0, ""); 179SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 180 CTLFLAG_RW, &vm_swap_idle_enabled, 0, ""); 181#endif 182 183SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts, 184 CTLFLAG_RW, &defer_swap_pageouts, 0, ""); 185 186SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts, 187 CTLFLAG_RW, &disable_swap_pageouts, 0, ""); 188 189SYSCTL_INT(_vm, OID_AUTO, max_page_launder, 190 CTLFLAG_RW, &max_page_launder, 0, ""); 191 192 193#define VM_PAGEOUT_PAGE_COUNT 16 194int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT; 195 196int vm_page_max_wired; /* XXX max # of wired pages system-wide */ 197 198#if !defined(NO_SWAPPING) 199typedef void freeer_fcn_t __P((vm_map_t, vm_object_t, vm_pindex_t, int)); 200static void vm_pageout_map_deactivate_pages __P((vm_map_t, vm_pindex_t)); 201static freeer_fcn_t vm_pageout_object_deactivate_pages; 202static void vm_req_vmdaemon __P((void)); 203#endif 204static void vm_pageout_page_stats(void); 205void pmap_collect(void); 206 207/* 208 * vm_pageout_clean: 209 * 210 * Clean the page and remove it from the laundry. 211 * 212 * We set the busy bit to cause potential page faults on this page to 213 * block. 214 * 215 * And we set pageout-in-progress to keep the object from disappearing 216 * during pageout. This guarantees that the page won't move from the 217 * inactive queue. (However, any other page on the inactive queue may 218 * move!) 219 */ 220static int 221vm_pageout_clean(m) 222 vm_page_t m; 223{ 224 register vm_object_t object; 225 vm_page_t mc[2*vm_pageout_page_count]; 226 int pageout_count; 227 int i, forward_okay, backward_okay, page_base; 228 vm_pindex_t pindex = m->pindex; 229 230 object = m->object; 231 232 /* 233 * If not OBJT_SWAP, additional memory may be needed to do the pageout. 234 * Try to avoid the deadlock. 235 */ 236 if ((object->type == OBJT_DEFAULT) && 237 ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min)) 238 return 0; 239 240 /* 241 * Don't mess with the page if it's busy. 242 */ 243 if ((m->hold_count != 0) || 244 ((m->busy != 0) || (m->flags & PG_BUSY))) 245 return 0; 246 247 /* 248 * Try collapsing before it's too late. 249 */ 250 if (object->backing_object) { 251 vm_object_collapse(object); 252 } 253 254 mc[vm_pageout_page_count] = m; 255 pageout_count = 1; 256 page_base = vm_pageout_page_count; 257 forward_okay = TRUE; 258 if (pindex != 0) 259 backward_okay = TRUE; 260 else 261 backward_okay = FALSE; 262 /* 263 * Scan object for clusterable pages. 264 * 265 * We can cluster ONLY if: ->> the page is NOT 266 * clean, wired, busy, held, or mapped into a 267 * buffer, and one of the following: 268 * 1) The page is inactive, or a seldom used 269 * active page. 270 * -or- 271 * 2) we force the issue. 272 */ 273 for (i = 1; (i < vm_pageout_page_count) && (forward_okay || backward_okay); i++) { 274 vm_page_t p; 275 276 /* 277 * See if forward page is clusterable. 278 */ 279 if (forward_okay) { 280 /* 281 * Stop forward scan at end of object. 282 */ 283 if ((pindex + i) > object->size) { 284 forward_okay = FALSE; 285 goto do_backward; 286 } 287 p = vm_page_lookup(object, pindex + i); 288 if (p) { 289 if (((p->queue - p->pc) == PQ_CACHE) || 290 (p->flags & PG_BUSY) || p->busy) { 291 forward_okay = FALSE; 292 goto do_backward; 293 } 294 vm_page_test_dirty(p); 295 if ((p->dirty & p->valid) != 0 && 296 (p->queue == PQ_INACTIVE) && 297 (p->wire_count == 0) && 298 (p->hold_count == 0)) { 299 mc[vm_pageout_page_count + i] = p; 300 pageout_count++; 301 if (pageout_count == vm_pageout_page_count) 302 break; 303 } else { 304 forward_okay = FALSE; 305 } 306 } else { 307 forward_okay = FALSE; 308 } 309 } 310do_backward: 311 /* 312 * See if backward page is clusterable. 313 */ 314 if (backward_okay) { 315 /* 316 * Stop backward scan at beginning of object. 317 */ 318 if ((pindex - i) == 0) { 319 backward_okay = FALSE; 320 } 321 p = vm_page_lookup(object, pindex - i); 322 if (p) { 323 if (((p->queue - p->pc) == PQ_CACHE) || 324 (p->flags & PG_BUSY) || p->busy) { 325 backward_okay = FALSE; 326 continue; 327 } 328 vm_page_test_dirty(p); 329 if ((p->dirty & p->valid) != 0 && 330 (p->queue == PQ_INACTIVE) && 331 (p->wire_count == 0) && 332 (p->hold_count == 0)) { 333 mc[vm_pageout_page_count - i] = p; 334 pageout_count++; 335 page_base--; 336 if (pageout_count == vm_pageout_page_count) 337 break; 338 } else { 339 backward_okay = FALSE; 340 } 341 } else { 342 backward_okay = FALSE; 343 } 344 } 345 } 346 347 /* 348 * we allow reads during pageouts... 349 */ 350 return vm_pageout_flush(&mc[page_base], pageout_count, 0); 351} 352 353int 354vm_pageout_flush(mc, count, flags) 355 vm_page_t *mc; 356 int count; 357 int flags; 358{ 359 register vm_object_t object; 360 int pageout_status[count]; 361 int numpagedout = 0; 362 int i; 363 364 for (i = 0; i < count; i++) { 365 mc[i]->busy++; 366 vm_page_protect(mc[i], VM_PROT_READ); 367 } 368 369 object = mc[0]->object; 370 object->paging_in_progress += count; 371 372 vm_pager_put_pages(object, mc, count, 373 (flags | ((object == kernel_object) ? OBJPC_SYNC : 0)), 374 pageout_status); 375 376 for (i = 0; i < count; i++) { 377 vm_page_t mt = mc[i]; 378 379 switch (pageout_status[i]) { 380 case VM_PAGER_OK: 381 numpagedout++; 382 break; 383 case VM_PAGER_PEND: 384 numpagedout++; 385 break; 386 case VM_PAGER_BAD: 387 /* 388 * Page outside of range of object. Right now we 389 * essentially lose the changes by pretending it 390 * worked. 391 */ 392 pmap_clear_modify(VM_PAGE_TO_PHYS(mt)); 393 mt->dirty = 0; 394 break; 395 case VM_PAGER_ERROR: 396 case VM_PAGER_FAIL: 397 /* 398 * If page couldn't be paged out, then reactivate the 399 * page so it doesn't clog the inactive list. (We 400 * will try paging out it again later). 401 */ 402 vm_page_activate(mt); 403 break; 404 case VM_PAGER_AGAIN: 405 break; 406 } 407 408 /* 409 * If the operation is still going, leave the page busy to 410 * block all other accesses. Also, leave the paging in 411 * progress indicator set so that we don't attempt an object 412 * collapse. 413 */ 414 if (pageout_status[i] != VM_PAGER_PEND) { 415 vm_object_pip_wakeup(object); 416 PAGE_BWAKEUP(mt); 417 } 418 } 419 return numpagedout; 420} 421 422#if !defined(NO_SWAPPING) 423/* 424 * vm_pageout_object_deactivate_pages 425 * 426 * deactivate enough pages to satisfy the inactive target 427 * requirements or if vm_page_proc_limit is set, then 428 * deactivate all of the pages in the object and its 429 * backing_objects. 430 * 431 * The object and map must be locked. 432 */ 433static void 434vm_pageout_object_deactivate_pages(map, object, desired, map_remove_only) 435 vm_map_t map; 436 vm_object_t object; 437 vm_pindex_t desired; 438 int map_remove_only; 439{ 440 register vm_page_t p, next; 441 int rcount; 442 int remove_mode; 443 int s; 444 445 if (object->type == OBJT_DEVICE) 446 return; 447 448 while (object) { 449 if (vm_map_pmap(map)->pm_stats.resident_count <= desired) 450 return; 451 if (object->paging_in_progress) 452 return; 453 454 remove_mode = map_remove_only; 455 if (object->shadow_count > 1) 456 remove_mode = 1; 457 /* 458 * scan the objects entire memory queue 459 */ 460 rcount = object->resident_page_count; 461 p = TAILQ_FIRST(&object->memq); 462 while (p && (rcount-- > 0)) { 463 int actcount; 464 if (vm_map_pmap(map)->pm_stats.resident_count <= desired) 465 return; 466 next = TAILQ_NEXT(p, listq); 467 cnt.v_pdpages++; 468 if (p->wire_count != 0 || 469 p->hold_count != 0 || 470 p->busy != 0 || 471 (p->flags & PG_BUSY) || 472 !pmap_page_exists(vm_map_pmap(map), VM_PAGE_TO_PHYS(p))) { 473 p = next; 474 continue; 475 } 476 477 actcount = pmap_ts_referenced(VM_PAGE_TO_PHYS(p)); 478 if (actcount) { 479 p->flags |= PG_REFERENCED; 480 } else if (p->flags & PG_REFERENCED) { 481 actcount = 1; 482 } 483 484 if ((p->queue != PQ_ACTIVE) && 485 (p->flags & PG_REFERENCED)) { 486 vm_page_activate(p); 487 p->act_count += actcount; 488 p->flags &= ~PG_REFERENCED; 489 } else if (p->queue == PQ_ACTIVE) { 490 if ((p->flags & PG_REFERENCED) == 0) { 491 p->act_count -= min(p->act_count, ACT_DECLINE); 492 if (!remove_mode && (vm_pageout_algorithm_lru || (p->act_count == 0))) { 493 vm_page_protect(p, VM_PROT_NONE); 494 vm_page_deactivate(p); 495 } else { 496 s = splvm(); 497 TAILQ_REMOVE(&vm_page_queue_active, p, pageq); 498 TAILQ_INSERT_TAIL(&vm_page_queue_active, p, pageq); 499 splx(s); 500 } 501 } else { 502 vm_page_activate(p); 503 p->flags &= ~PG_REFERENCED; 504 if (p->act_count < (ACT_MAX - ACT_ADVANCE)) 505 p->act_count += ACT_ADVANCE; 506 s = splvm(); 507 TAILQ_REMOVE(&vm_page_queue_active, p, pageq); 508 TAILQ_INSERT_TAIL(&vm_page_queue_active, p, pageq); 509 splx(s); 510 } 511 } else if (p->queue == PQ_INACTIVE) { 512 vm_page_protect(p, VM_PROT_NONE); 513 } 514 p = next; 515 } 516 object = object->backing_object; 517 } 518 return; 519} 520 521/* 522 * deactivate some number of pages in a map, try to do it fairly, but 523 * that is really hard to do. 524 */ 525static void 526vm_pageout_map_deactivate_pages(map, desired) 527 vm_map_t map; 528 vm_pindex_t desired; 529{ 530 vm_map_entry_t tmpe; 531 vm_object_t obj, bigobj; 532 533 if (lockmgr(&map->lock, LK_EXCLUSIVE | LK_NOWAIT, (void *)0, curproc)) { 534 return; 535 } 536 537 bigobj = NULL; 538 539 /* 540 * first, search out the biggest object, and try to free pages from 541 * that. 542 */ 543 tmpe = map->header.next; 544 while (tmpe != &map->header) { 545 if ((tmpe->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) == 0) { 546 obj = tmpe->object.vm_object; 547 if ((obj != NULL) && (obj->shadow_count <= 1) && 548 ((bigobj == NULL) || 549 (bigobj->resident_page_count < obj->resident_page_count))) { 550 bigobj = obj; 551 } 552 } 553 tmpe = tmpe->next; 554 } 555 556 if (bigobj) 557 vm_pageout_object_deactivate_pages(map, bigobj, desired, 0); 558 559 /* 560 * Next, hunt around for other pages to deactivate. We actually 561 * do this search sort of wrong -- .text first is not the best idea. 562 */ 563 tmpe = map->header.next; 564 while (tmpe != &map->header) { 565 if (vm_map_pmap(map)->pm_stats.resident_count <= desired) 566 break; 567 if ((tmpe->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) == 0) { 568 obj = tmpe->object.vm_object; 569 if (obj) 570 vm_pageout_object_deactivate_pages(map, obj, desired, 0); 571 } 572 tmpe = tmpe->next; 573 }; 574 575 /* 576 * Remove all mappings if a process is swapped out, this will free page 577 * table pages. 578 */ 579 if (desired == 0) 580 pmap_remove(vm_map_pmap(map), 581 VM_MIN_ADDRESS, VM_MAXUSER_ADDRESS); 582 vm_map_unlock(map); 583 return; 584} 585#endif 586 587void 588vm_pageout_page_free(vm_page_t m) { 589 struct vnode *vp; 590 vm_object_t object; 591 592 object = m->object; 593 object->ref_count++; 594 595 if (object->type == OBJT_VNODE) { 596 vp = object->handle; 597 vp->v_usecount++; 598 if (VSHOULDBUSY(vp)) 599 vbusy(vp); 600 } 601 602 m->flags |= PG_BUSY; 603 vm_page_protect(m, VM_PROT_NONE); 604 vm_page_free(m); 605 vm_object_deallocate(object); 606} 607 608/* 609 * vm_pageout_scan does the dirty work for the pageout daemon. 610 */ 611static int 612vm_pageout_scan() 613{ 614 vm_page_t m, next; 615 int page_shortage, addl_page_shortage, maxscan, pcount; 616 int maxlaunder; 617 int pages_freed; 618 struct proc *p, *bigproc; 619 vm_offset_t size, bigsize; 620 vm_object_t object; 621 int force_wakeup = 0; 622 int actcount; 623 int vnodes_skipped = 0; 624 int s; 625 626 /* 627 * Do whatever cleanup that the pmap code can. 628 */ 629 pmap_collect(); 630 631 /* 632 * Start scanning the inactive queue for pages we can free. We keep 633 * scanning until we have enough free pages or we have scanned through 634 * the entire queue. If we encounter dirty pages, we start cleaning 635 * them. 636 */ 637 638 pages_freed = 0; 639 addl_page_shortage = vm_pageout_deficit; 640 vm_pageout_deficit = 0; 641 642 if (max_page_launder == 0) 643 max_page_launder = 1; 644 maxlaunder = (cnt.v_inactive_target > max_page_launder) ? 645 max_page_launder : cnt.v_inactive_target; 646 647rescan0: 648 maxscan = cnt.v_inactive_count; 649 for( m = TAILQ_FIRST(&vm_page_queue_inactive); 650 651 (m != NULL) && (maxscan-- > 0) && 652 ((cnt.v_cache_count + cnt.v_free_count) < 653 (cnt.v_cache_min + cnt.v_free_target)); 654 655 m = next) { 656 657 cnt.v_pdpages++; 658 659 if (m->queue != PQ_INACTIVE) { 660 goto rescan0; 661 } 662 663 next = TAILQ_NEXT(m, pageq); 664 665 if (m->hold_count) { 666 s = splvm(); 667 TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq); 668 TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq); 669 splx(s); 670 addl_page_shortage++; 671 continue; 672 } 673 /* 674 * Dont mess with busy pages, keep in the front of the 675 * queue, most likely are being paged out. 676 */ 677 if (m->busy || (m->flags & PG_BUSY)) { 678 addl_page_shortage++; 679 continue; 680 } 681 682 /* 683 * If the object is not being used, we ignore previous references. 684 */ 685 if (m->object->ref_count == 0) { 686 m->flags &= ~PG_REFERENCED; 687 pmap_clear_reference(VM_PAGE_TO_PHYS(m)); 688 689 /* 690 * Otherwise, if the page has been referenced while in the inactive 691 * queue, we bump the "activation count" upwards, making it less 692 * likely that the page will be added back to the inactive queue 693 * prematurely again. Here we check the page tables (or emulated 694 * bits, if any), given the upper level VM system not knowing anything 695 * about existing references. 696 */ 697 } else if (((m->flags & PG_REFERENCED) == 0) && 698 (actcount = pmap_ts_referenced(VM_PAGE_TO_PHYS(m)))) { 699 vm_page_activate(m); 700 m->act_count += (actcount + ACT_ADVANCE); 701 continue; 702 } 703 704 /* 705 * If the upper level VM system knows about any page references, 706 * we activate the page. We also set the "activation count" higher 707 * than normal so that we will less likely place pages back onto the 708 * inactive queue again. 709 */ 710 if ((m->flags & PG_REFERENCED) != 0) { 711 m->flags &= ~PG_REFERENCED; 712 actcount = pmap_ts_referenced(VM_PAGE_TO_PHYS(m)); 713 vm_page_activate(m); 714 m->act_count += (actcount + ACT_ADVANCE + 1); 715 continue; 716 } 717 718 /* 719 * If the upper level VM system doesn't know anything about the 720 * page being dirty, we have to check for it again. As far as the 721 * VM code knows, any partially dirty pages are fully dirty. 722 */ 723 if (m->dirty == 0) { 724 vm_page_test_dirty(m); 725 } else if (m->dirty != 0) { 726 m->dirty = VM_PAGE_BITS_ALL; 727 } 728 729 /* 730 * Invalid pages can be easily freed 731 */ 732 if (m->valid == 0) { 733 vm_pageout_page_free(m); 734 cnt.v_dfree++; 735 pages_freed++; 736 737 /* 738 * Clean pages can be placed onto the cache queue. 739 */ 740 } else if (m->dirty == 0) { 741 vm_page_cache(m); 742 pages_freed++; 743 744 /* 745 * Dirty pages need to be paged out. Note that we clean 746 * only a limited number of pages per pagedaemon pass. 747 */ 748 } else if (maxlaunder > 0) { 749 int written; 750 int swap_pageouts_ok; 751 struct vnode *vp = NULL; 752 753 object = m->object; 754 755 if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) { 756 swap_pageouts_ok = 1; 757 } else { 758 swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts); 759 swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts && 760 (cnt.v_free_count + cnt.v_cache_count) < cnt.v_free_min); 761 762 } 763 764 /* 765 * We don't bother paging objects that are "dead". Those 766 * objects are in a "rundown" state. 767 */ 768 if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) { 769 s = splvm(); 770 TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq); 771 TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq); 772 splx(s); 773 continue; 774 } 775 776 if ((object->type == OBJT_VNODE) && 777 (object->flags & OBJ_DEAD) == 0) { 778 vp = object->handle; 779 if (VOP_ISLOCKED(vp) || 780 vget(vp, LK_EXCLUSIVE|LK_NOOBJ, curproc)) { 781 if ((m->queue == PQ_INACTIVE) && 782 (m->hold_count == 0) && 783 (m->busy == 0) && 784 (m->flags & PG_BUSY) == 0) { 785 s = splvm(); 786 TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq); 787 TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq); 788 splx(s); 789 } 790 if (object->flags & OBJ_MIGHTBEDIRTY) 791 vnodes_skipped++; 792 continue; 793 } 794 795 /* 796 * The page might have been moved to another queue 797 * during potential blocking in vget() above. 798 */ 799 if (m->queue != PQ_INACTIVE) { 800 if (object->flags & OBJ_MIGHTBEDIRTY) 801 vnodes_skipped++; 802 vput(vp); 803 continue; 804 } 805 806 /* 807 * The page may have been busied during the blocking in 808 * vput(); We don't move the page back onto the end of 809 * the queue so that statistics are more correct if we don't. 810 */ 811 if (m->busy || (m->flags & PG_BUSY)) { 812 vput(vp); 813 continue; 814 } 815 816 /* 817 * If the page has become held, then skip it 818 */ 819 if (m->hold_count) { 820 s = splvm(); 821 TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq); 822 TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq); 823 splx(s); 824 if (object->flags & OBJ_MIGHTBEDIRTY) 825 vnodes_skipped++; 826 vput(vp); 827 continue; 828 } 829 } 830 831 /* 832 * If a page is dirty, then it is either being washed 833 * (but not yet cleaned) or it is still in the 834 * laundry. If it is still in the laundry, then we 835 * start the cleaning operation. 836 */ 837 written = vm_pageout_clean(m); 838 if (vp) 839 vput(vp); 840 841 maxlaunder -= written; 842 } 843 } 844 845 /* 846 * Compute the page shortage. If we are still very low on memory be 847 * sure that we will move a minimal amount of pages from active to 848 * inactive. 849 */ 850 page_shortage = (cnt.v_inactive_target + cnt.v_cache_min) - 851 (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count); 852 if (page_shortage <= 0) { 853 if (pages_freed == 0) { 854 page_shortage = cnt.v_free_min - cnt.v_free_count; 855 } else { 856 page_shortage = 1; 857 } 858 } 859 860 /* 861 * If the "inactive" loop finds that there is a shortage over and 862 * above the page statistics variables, then we need to accomodate 863 * that. This avoids potential deadlocks due to pages being temporarily 864 * busy for I/O or other types of temporary wiring. 865 */ 866 if (addl_page_shortage) { 867 if (page_shortage < 0) 868 page_shortage = 0; 869 page_shortage += addl_page_shortage; 870 } 871 872 pcount = cnt.v_active_count; 873 m = TAILQ_FIRST(&vm_page_queue_active); 874 while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) { 875 876 /* 877 * This is a consistancy check, and should likely be a panic 878 * or warning. 879 */ 880 if (m->queue != PQ_ACTIVE) { 881 break; 882 } 883 884 next = TAILQ_NEXT(m, pageq); 885 /* 886 * Don't deactivate pages that are busy. 887 */ 888 if ((m->busy != 0) || 889 (m->flags & PG_BUSY) || 890 (m->hold_count != 0)) { 891 s = splvm(); 892 TAILQ_REMOVE(&vm_page_queue_active, m, pageq); 893 TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq); 894 splx(s); 895 m = next; 896 continue; 897 } 898 899 /* 900 * The count for pagedaemon pages is done after checking the 901 * page for eligbility... 902 */ 903 cnt.v_pdpages++; 904 905 /* 906 * Check to see "how much" the page has been used. 907 */ 908 actcount = 0; 909 if (m->object->ref_count != 0) { 910 if (m->flags & PG_REFERENCED) { 911 actcount += 1; 912 } 913 actcount += pmap_ts_referenced(VM_PAGE_TO_PHYS(m)); 914 if (actcount) { 915 m->act_count += ACT_ADVANCE + actcount; 916 if (m->act_count > ACT_MAX) 917 m->act_count = ACT_MAX; 918 } 919 } 920 921 /* 922 * Since we have "tested" this bit, we need to clear it now. 923 */ 924 m->flags &= ~PG_REFERENCED; 925 926 /* 927 * Only if an object is currently being used, do we use the 928 * page activation count stats. 929 */ 930 if (actcount && (m->object->ref_count != 0)) { 931 s = splvm(); 932 TAILQ_REMOVE(&vm_page_queue_active, m, pageq); 933 TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq); 934 splx(s); 935 } else { 936 m->act_count -= min(m->act_count, ACT_DECLINE); 937 if (vm_pageout_algorithm_lru || 938 (m->object->ref_count == 0) || (m->act_count == 0)) { 939 page_shortage--; 940 if (m->object->ref_count == 0) { 941 vm_page_protect(m, VM_PROT_NONE); 942 if (m->dirty == 0) 943 vm_page_cache(m); 944 else 945 vm_page_deactivate(m); 946 } else { 947 vm_page_deactivate(m); 948 } 949 } else { 950 s = splvm(); 951 TAILQ_REMOVE(&vm_page_queue_active, m, pageq); 952 TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq); 953 splx(s); 954 } 955 } 956 m = next; 957 } 958 959 s = splvm(); 960 /* 961 * We try to maintain some *really* free pages, this allows interrupt 962 * code to be guaranteed space. 963 */ 964 while (cnt.v_free_count < cnt.v_free_reserved) { 965 static int cache_rover = 0; 966 m = vm_page_list_find(PQ_CACHE, cache_rover); 967 if (!m) 968 break; 969 cache_rover = (cache_rover + PQ_PRIME2) & PQ_L2_MASK; 970 vm_pageout_page_free(m); 971 cnt.v_dfree++; 972 } 973 splx(s); 974 975#if !defined(NO_SWAPPING) 976 /* 977 * Idle process swapout -- run once per second. 978 */ 979 if (vm_swap_idle_enabled) { 980 static long lsec; 981 if (time.tv_sec != lsec) { 982 vm_pageout_req_swapout |= VM_SWAP_IDLE; 983 vm_req_vmdaemon(); 984 lsec = time.tv_sec; 985 } 986 } 987#endif 988 989 /* 990 * If we didn't get enough free pages, and we have skipped a vnode 991 * in a writeable object, wakeup the sync daemon. And kick swapout 992 * if we did not get enough free pages. 993 */ 994 if ((cnt.v_cache_count + cnt.v_free_count) < 995 (cnt.v_free_target + cnt.v_cache_min) ) { 996 if (vnodes_skipped && 997 (cnt.v_cache_count + cnt.v_free_count) < cnt.v_free_min) { 998 if (!vfs_update_wakeup) { 999 vfs_update_wakeup = 1; 1000 wakeup(&vfs_update_wakeup); 1001 } 1002 } 1003#if !defined(NO_SWAPPING) 1004 if (vm_swap_enabled && 1005 (cnt.v_free_count + cnt.v_cache_count < cnt.v_free_target)) { 1006 vm_req_vmdaemon(); 1007 vm_pageout_req_swapout |= VM_SWAP_NORMAL; 1008 } 1009#endif 1010 } 1011 1012 1013 /* 1014 * make sure that we have swap space -- if we are low on memory and 1015 * swap -- then kill the biggest process. 1016 */ 1017 if ((vm_swap_size == 0 || swap_pager_full) && 1018 ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_free_min)) { 1019 bigproc = NULL; 1020 bigsize = 0; 1021 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 1022 /* 1023 * if this is a system process, skip it 1024 */ 1025 if ((p->p_flag & P_SYSTEM) || (p->p_pid == 1) || 1026 ((p->p_pid < 48) && (vm_swap_size != 0))) { 1027 continue; 1028 } 1029 /* 1030 * if the process is in a non-running type state, 1031 * don't touch it. 1032 */ 1033 if (p->p_stat != SRUN && p->p_stat != SSLEEP) { 1034 continue; 1035 } 1036 /* 1037 * get the process size 1038 */ 1039 size = p->p_vmspace->vm_pmap.pm_stats.resident_count; 1040 /* 1041 * if the this process is bigger than the biggest one 1042 * remember it. 1043 */ 1044 if (size > bigsize) { 1045 bigproc = p; 1046 bigsize = size; 1047 } 1048 } 1049 if (bigproc != NULL) { 1050 killproc(bigproc, "out of swap space"); 1051 bigproc->p_estcpu = 0; 1052 bigproc->p_nice = PRIO_MIN; 1053 resetpriority(bigproc); 1054 wakeup(&cnt.v_free_count); 1055 } 1056 } 1057 return force_wakeup; 1058} 1059 1060/* 1061 * This routine tries to maintain the pseudo LRU active queue, 1062 * so that during long periods of time where there is no paging, 1063 * that some statistic accumlation still occurs. This code 1064 * helps the situation where paging just starts to occur. 1065 */ 1066static void 1067vm_pageout_page_stats() 1068{ 1069 int s; 1070 vm_page_t m,next; 1071 int pcount,tpcount; /* Number of pages to check */ 1072 static int fullintervalcount = 0; 1073 1074 pcount = cnt.v_active_count; 1075 fullintervalcount += vm_pageout_stats_interval; 1076 if (fullintervalcount < vm_pageout_full_stats_interval) { 1077 tpcount = (vm_pageout_stats_max * cnt.v_active_count) / cnt.v_page_count; 1078 if (pcount > tpcount) 1079 pcount = tpcount; 1080 } 1081 1082 m = TAILQ_FIRST(&vm_page_queue_active); 1083 while ((m != NULL) && (pcount-- > 0)) { 1084 int actcount; 1085 1086 if (m->queue != PQ_ACTIVE) { 1087 break; 1088 } 1089 1090 next = TAILQ_NEXT(m, pageq); 1091 /* 1092 * Don't deactivate pages that are busy. 1093 */ 1094 if ((m->busy != 0) || 1095 (m->flags & PG_BUSY) || 1096 (m->hold_count != 0)) { 1097 s = splvm(); 1098 TAILQ_REMOVE(&vm_page_queue_active, m, pageq); 1099 TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq); 1100 splx(s); 1101 m = next; 1102 continue; 1103 } 1104 1105 actcount = 0; 1106 if (m->flags & PG_REFERENCED) { 1107 m->flags &= ~PG_REFERENCED; 1108 actcount += 1; 1109 } 1110 1111 actcount += pmap_ts_referenced(VM_PAGE_TO_PHYS(m)); 1112 if (actcount) { 1113 m->act_count += ACT_ADVANCE + actcount; 1114 if (m->act_count > ACT_MAX) 1115 m->act_count = ACT_MAX; 1116 s = splvm(); 1117 TAILQ_REMOVE(&vm_page_queue_active, m, pageq); 1118 TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq); 1119 splx(s); 1120 } else { 1121 if (m->act_count == 0) { 1122 /* 1123 * We turn off page access, so that we have more accurate 1124 * RSS stats. We don't do this in the normal page deactivation 1125 * when the system is loaded VM wise, because the cost of 1126 * the large number of page protect operations would be higher 1127 * than the value of doing the operation. 1128 */ 1129 vm_page_protect(m, VM_PROT_NONE); 1130 vm_page_deactivate(m); 1131 } else { 1132 m->act_count -= min(m->act_count, ACT_DECLINE); 1133 s = splvm(); 1134 TAILQ_REMOVE(&vm_page_queue_active, m, pageq); 1135 TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq); 1136 splx(s); 1137 } 1138 } 1139 1140 m = next; 1141 } 1142} 1143 1144static int 1145vm_pageout_free_page_calc(count) 1146vm_size_t count; 1147{ 1148 if (count < cnt.v_page_count) 1149 return 0; 1150 /* 1151 * free_reserved needs to include enough for the largest swap pager 1152 * structures plus enough for any pv_entry structs when paging. 1153 */ 1154 if (cnt.v_page_count > 1024) 1155 cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200; 1156 else 1157 cnt.v_free_min = 4; 1158 cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE + 1159 cnt.v_interrupt_free_min; 1160 cnt.v_free_reserved = vm_pageout_page_count + 1161 cnt.v_pageout_free_min + (count / 768) + PQ_L2_SIZE; 1162 cnt.v_free_min += cnt.v_free_reserved; 1163 return 1; 1164} 1165 1166 1167/* 1168 * vm_pageout is the high level pageout daemon. 1169 */ 1170static void 1171vm_pageout() 1172{ 1173 /* 1174 * Initialize some paging parameters. 1175 */ 1176 1177 cnt.v_interrupt_free_min = 2; 1178 if (cnt.v_page_count < 2000) 1179 vm_pageout_page_count = 8; 1180 1181 vm_pageout_free_page_calc(cnt.v_page_count); 1182 /* 1183 * free_reserved needs to include enough for the largest swap pager 1184 * structures plus enough for any pv_entry structs when paging. 1185 */ 1186 if (cnt.v_free_count > 6144) 1187 cnt.v_free_target = 3 * cnt.v_free_min + cnt.v_free_reserved; 1188 else 1189 cnt.v_free_target = 2 * cnt.v_free_min + cnt.v_free_reserved; 1190 1191 if (cnt.v_free_count > 2048) { 1192 cnt.v_cache_min = cnt.v_free_target; 1193 cnt.v_cache_max = 2 * cnt.v_cache_min; 1194 cnt.v_inactive_target = (3 * cnt.v_free_target) / 2; 1195 } else { 1196 cnt.v_cache_min = 0; 1197 cnt.v_cache_max = 0; 1198 cnt.v_inactive_target = cnt.v_free_count / 4; 1199 } 1200 if (cnt.v_inactive_target > cnt.v_free_count / 3) 1201 cnt.v_inactive_target = cnt.v_free_count / 3; 1202 1203 /* XXX does not really belong here */ 1204 if (vm_page_max_wired == 0) 1205 vm_page_max_wired = cnt.v_free_count / 3; 1206 1207 if (vm_pageout_stats_max == 0) 1208 vm_pageout_stats_max = cnt.v_free_target; 1209 1210 /* 1211 * Set interval in seconds for stats scan. 1212 */ 1213 if (vm_pageout_stats_interval == 0) 1214 vm_pageout_stats_interval = 4; 1215 if (vm_pageout_full_stats_interval == 0) 1216 vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4; 1217 1218 1219 /* 1220 * Set maximum free per pass 1221 */ 1222 if (vm_pageout_stats_free_max == 0) 1223 vm_pageout_stats_free_max = 25; 1224 1225 max_page_launder = (cnt.v_page_count > 1800 ? 32 : 16); 1226 1227 swap_pager_swap_init(); 1228 /* 1229 * The pageout daemon is never done, so loop forever. 1230 */ 1231 while (TRUE) { 1232 int inactive_target; 1233 int error; 1234 int s = splvm(); 1235 if (!vm_pages_needed || 1236 ((cnt.v_free_count + cnt.v_cache_count) > cnt.v_free_min)) { 1237 vm_pages_needed = 0; 1238 error = tsleep(&vm_pages_needed, 1239 PVM, "psleep", vm_pageout_stats_interval * hz); 1240 if (error && !vm_pages_needed) { 1241 splx(s); 1242 vm_pageout_page_stats(); 1243 continue; 1244 } 1245 } else if (vm_pages_needed) { 1246 vm_pages_needed = 0; 1247 tsleep(&vm_pages_needed, PVM, "psleep", hz/2); 1248 } 1249 1250 if (vm_pages_needed) 1251 cnt.v_pdwakeups++; 1252 vm_pages_needed = 0; 1253 splx(s); 1254 vm_pager_sync(); 1255 vm_pageout_scan(); 1256 vm_pageout_deficit = 0; 1257 vm_pager_sync(); 1258 wakeup(&cnt.v_free_count); 1259 } 1260} 1261 1262void 1263pagedaemon_wakeup() 1264{ 1265 if (!vm_pages_needed && curproc != pageproc) { 1266 vm_pages_needed++; 1267 wakeup(&vm_pages_needed); 1268 } 1269} 1270 1271#if !defined(NO_SWAPPING) 1272static void 1273vm_req_vmdaemon() 1274{ 1275 static int lastrun = 0; 1276 1277 if ((ticks > (lastrun + hz)) || (ticks < lastrun)) { 1278 wakeup(&vm_daemon_needed); 1279 lastrun = ticks; 1280 } 1281} 1282 1283static void 1284vm_daemon() 1285{ 1286 vm_object_t object; 1287 struct proc *p; 1288 1289 while (TRUE) { 1290 tsleep(&vm_daemon_needed, PUSER, "psleep", 0); 1291 if (vm_pageout_req_swapout) { 1292 swapout_procs(vm_pageout_req_swapout); 1293 vm_pageout_req_swapout = 0; 1294 } 1295 /* 1296 * scan the processes for exceeding their rlimits or if 1297 * process is swapped out -- deactivate pages 1298 */ 1299 1300 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 1301 quad_t limit; 1302 vm_offset_t size; 1303 1304 /* 1305 * if this is a system process or if we have already 1306 * looked at this process, skip it. 1307 */ 1308 if (p->p_flag & (P_SYSTEM | P_WEXIT)) { 1309 continue; 1310 } 1311 /* 1312 * if the process is in a non-running type state, 1313 * don't touch it. 1314 */ 1315 if (p->p_stat != SRUN && p->p_stat != SSLEEP) { 1316 continue; 1317 } 1318 /* 1319 * get a limit 1320 */ 1321 limit = qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur, 1322 p->p_rlimit[RLIMIT_RSS].rlim_max); 1323 1324 /* 1325 * let processes that are swapped out really be 1326 * swapped out set the limit to nothing (will force a 1327 * swap-out.) 1328 */ 1329 if ((p->p_flag & P_INMEM) == 0) 1330 limit = 0; /* XXX */ 1331 1332 size = p->p_vmspace->vm_pmap.pm_stats.resident_count * PAGE_SIZE; 1333 if (limit >= 0 && size >= limit) { 1334 vm_pageout_map_deactivate_pages(&p->p_vmspace->vm_map, 1335 (vm_pindex_t)(limit >> PAGE_SHIFT) ); 1336 } 1337 } 1338 } 1339} 1340#endif 1341