vm_pageout.c revision 17004
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.81 1996/07/08 02:25:53 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/malloc.h> 81#include <sys/kernel.h> 82#include <sys/signalvar.h> 83#include <sys/vnode.h> 84#include <sys/vmmeter.h> 85#include <sys/sysctl.h> 86 87#include <vm/vm.h> 88#include <vm/vm_param.h> 89#include <vm/vm_prot.h> 90#include <vm/lock.h> 91#include <vm/vm_object.h> 92#include <vm/vm_page.h> 93#include <vm/vm_map.h> 94#include <vm/vm_pageout.h> 95#include <vm/vm_kern.h> 96#include <vm/vm_pager.h> 97#include <vm/swap_pager.h> 98#include <vm/vm_extern.h> 99 100/* 101 * System initialization 102 */ 103 104/* the kernel process "vm_pageout"*/ 105static void vm_pageout __P((void)); 106static int vm_pageout_clean __P((vm_page_t, int)); 107static int vm_pageout_scan __P((void)); 108static int vm_pageout_free_page_calc __P((vm_size_t count)); 109struct proc *pageproc; 110 111static struct kproc_desc page_kp = { 112 "pagedaemon", 113 vm_pageout, 114 &pageproc 115}; 116SYSINIT_KT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, &page_kp) 117 118#if !defined(NO_SWAPPING) 119/* the kernel process "vm_daemon"*/ 120static void vm_daemon __P((void)); 121static struct proc *vmproc; 122 123static struct kproc_desc vm_kp = { 124 "vmdaemon", 125 vm_daemon, 126 &vmproc 127}; 128SYSINIT_KT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp) 129#endif 130 131 132int vm_pages_needed; /* Event on which pageout daemon sleeps */ 133 134int vm_pageout_pages_needed; /* flag saying that the pageout daemon needs pages */ 135 136extern int npendingio; 137#if !defined(NO_SWAPPING) 138static int vm_pageout_req_swapout; /* XXX */ 139static int vm_daemon_needed; 140#endif 141extern int nswiodone; 142extern int vm_swap_size; 143extern int vfs_update_wakeup; 144int vm_pageout_algorithm_lru=0; 145#if defined(NO_SWAPPING) 146int vm_swapping_enabled=0; 147#else 148int vm_swapping_enabled=1; 149#endif 150 151SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm, 152 CTLFLAG_RW, &vm_pageout_algorithm_lru, 0, ""); 153 154#if defined(NO_SWAPPING) 155SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swapping_enabled, 156 CTLFLAG_RD, &vm_swapping_enabled, 0, ""); 157#else 158SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swapping_enabled, 159 CTLFLAG_RW, &vm_swapping_enabled, 0, ""); 160#endif 161 162#define MAXLAUNDER (cnt.v_page_count > 1800 ? 32 : 16) 163 164#define VM_PAGEOUT_PAGE_COUNT 16 165int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT; 166 167int vm_page_max_wired; /* XXX max # of wired pages system-wide */ 168 169#if !defined(NO_SWAPPING) 170typedef void freeer_fcn_t __P((vm_map_t, vm_object_t, vm_pindex_t, int)); 171static void vm_pageout_map_deactivate_pages __P((vm_map_t, vm_pindex_t)); 172static freeer_fcn_t vm_pageout_object_deactivate_pages; 173static void vm_req_vmdaemon __P((void)); 174#endif 175 176/* 177 * vm_pageout_clean: 178 * 179 * Clean the page and remove it from the laundry. 180 * 181 * We set the busy bit to cause potential page faults on this page to 182 * block. 183 * 184 * And we set pageout-in-progress to keep the object from disappearing 185 * during pageout. This guarantees that the page won't move from the 186 * inactive queue. (However, any other page on the inactive queue may 187 * move!) 188 */ 189static int 190vm_pageout_clean(m, sync) 191 vm_page_t m; 192 int sync; 193{ 194 register vm_object_t object; 195 vm_page_t mc[2*vm_pageout_page_count]; 196 int pageout_count; 197 int i, forward_okay, backward_okay, page_base; 198 vm_pindex_t pindex = m->pindex; 199 200 object = m->object; 201 202 /* 203 * If not OBJT_SWAP, additional memory may be needed to do the pageout. 204 * Try to avoid the deadlock. 205 */ 206 if ((sync != VM_PAGEOUT_FORCE) && 207 (object->type == OBJT_DEFAULT) && 208 ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min)) 209 return 0; 210 211 /* 212 * Don't mess with the page if it's busy. 213 */ 214 if ((!sync && m->hold_count != 0) || 215 ((m->busy != 0) || (m->flags & PG_BUSY))) 216 return 0; 217 218 /* 219 * Try collapsing before it's too late. 220 */ 221 if (!sync && object->backing_object) { 222 vm_object_collapse(object); 223 } 224 mc[vm_pageout_page_count] = m; 225 pageout_count = 1; 226 page_base = vm_pageout_page_count; 227 forward_okay = TRUE; 228 if (pindex != 0) 229 backward_okay = TRUE; 230 else 231 backward_okay = FALSE; 232 /* 233 * Scan object for clusterable pages. 234 * 235 * We can cluster ONLY if: ->> the page is NOT 236 * clean, wired, busy, held, or mapped into a 237 * buffer, and one of the following: 238 * 1) The page is inactive, or a seldom used 239 * active page. 240 * -or- 241 * 2) we force the issue. 242 */ 243 for (i = 1; (i < vm_pageout_page_count) && (forward_okay || backward_okay); i++) { 244 vm_page_t p; 245 246 /* 247 * See if forward page is clusterable. 248 */ 249 if (forward_okay) { 250 /* 251 * Stop forward scan at end of object. 252 */ 253 if ((pindex + i) > object->size) { 254 forward_okay = FALSE; 255 goto do_backward; 256 } 257 p = vm_page_lookup(object, pindex + i); 258 if (p) { 259 if ((p->queue == PQ_CACHE) || (p->flags & PG_BUSY) || p->busy) { 260 forward_okay = FALSE; 261 goto do_backward; 262 } 263 vm_page_test_dirty(p); 264 if ((p->dirty & p->valid) != 0 && 265 ((p->queue == PQ_INACTIVE) || 266 (sync == VM_PAGEOUT_FORCE)) && 267 (p->wire_count == 0) && 268 (p->hold_count == 0)) { 269 mc[vm_pageout_page_count + i] = p; 270 pageout_count++; 271 if (pageout_count == vm_pageout_page_count) 272 break; 273 } else { 274 forward_okay = FALSE; 275 } 276 } else { 277 forward_okay = FALSE; 278 } 279 } 280do_backward: 281 /* 282 * See if backward page is clusterable. 283 */ 284 if (backward_okay) { 285 /* 286 * Stop backward scan at beginning of object. 287 */ 288 if ((pindex - i) == 0) { 289 backward_okay = FALSE; 290 } 291 p = vm_page_lookup(object, pindex - i); 292 if (p) { 293 if ((p->queue == PQ_CACHE) || (p->flags & PG_BUSY) || p->busy) { 294 backward_okay = FALSE; 295 continue; 296 } 297 vm_page_test_dirty(p); 298 if ((p->dirty & p->valid) != 0 && 299 ((p->queue == PQ_INACTIVE) || 300 (sync == VM_PAGEOUT_FORCE)) && 301 (p->wire_count == 0) && 302 (p->hold_count == 0)) { 303 mc[vm_pageout_page_count - i] = p; 304 pageout_count++; 305 page_base--; 306 if (pageout_count == vm_pageout_page_count) 307 break; 308 } else { 309 backward_okay = FALSE; 310 } 311 } else { 312 backward_okay = FALSE; 313 } 314 } 315 } 316 317 /* 318 * we allow reads during pageouts... 319 */ 320 for (i = page_base; i < (page_base + pageout_count); i++) { 321 mc[i]->flags |= PG_BUSY; 322 vm_page_protect(mc[i], VM_PROT_READ); 323 } 324 325 return vm_pageout_flush(&mc[page_base], pageout_count, sync); 326} 327 328int 329vm_pageout_flush(mc, count, sync) 330 vm_page_t *mc; 331 int count; 332 int sync; 333{ 334 register vm_object_t object; 335 int pageout_status[count]; 336 int anyok = 0; 337 int i; 338 339 object = mc[0]->object; 340 object->paging_in_progress += count; 341 342 vm_pager_put_pages(object, mc, count, 343 ((sync || (object == kernel_object)) ? TRUE : FALSE), 344 pageout_status); 345 346 for (i = 0; i < count; i++) { 347 vm_page_t mt = mc[i]; 348 349 switch (pageout_status[i]) { 350 case VM_PAGER_OK: 351 ++anyok; 352 break; 353 case VM_PAGER_PEND: 354 ++anyok; 355 break; 356 case VM_PAGER_BAD: 357 /* 358 * Page outside of range of object. Right now we 359 * essentially lose the changes by pretending it 360 * worked. 361 */ 362 pmap_clear_modify(VM_PAGE_TO_PHYS(mt)); 363 mt->dirty = 0; 364 break; 365 case VM_PAGER_ERROR: 366 case VM_PAGER_FAIL: 367 /* 368 * If page couldn't be paged out, then reactivate the 369 * page so it doesn't clog the inactive list. (We 370 * will try paging out it again later). 371 */ 372 if (mt->queue == PQ_INACTIVE) 373 vm_page_activate(mt); 374 break; 375 case VM_PAGER_AGAIN: 376 break; 377 } 378 379 380 /* 381 * If the operation is still going, leave the page busy to 382 * block all other accesses. Also, leave the paging in 383 * progress indicator set so that we don't attempt an object 384 * collapse. 385 */ 386 if (pageout_status[i] != VM_PAGER_PEND) { 387 vm_object_pip_wakeup(object); 388 PAGE_WAKEUP(mt); 389 } 390 } 391 return anyok; 392} 393 394#if !defined(NO_SWAPPING) 395/* 396 * vm_pageout_object_deactivate_pages 397 * 398 * deactivate enough pages to satisfy the inactive target 399 * requirements or if vm_page_proc_limit is set, then 400 * deactivate all of the pages in the object and its 401 * backing_objects. 402 * 403 * The object and map must be locked. 404 */ 405static void 406vm_pageout_object_deactivate_pages(map, object, desired, map_remove_only) 407 vm_map_t map; 408 vm_object_t object; 409 vm_pindex_t desired; 410 int map_remove_only; 411{ 412 register vm_page_t p, next; 413 int rcount; 414 int remove_mode; 415 int s; 416 417 if (object->type == OBJT_DEVICE) 418 return; 419 420 while (object) { 421 if (vm_map_pmap(map)->pm_stats.resident_count <= desired) 422 return; 423 if (object->paging_in_progress) 424 return; 425 426 remove_mode = map_remove_only; 427 if (object->shadow_count > 1) 428 remove_mode = 1; 429 /* 430 * scan the objects entire memory queue 431 */ 432 rcount = object->resident_page_count; 433 p = TAILQ_FIRST(&object->memq); 434 while (p && (rcount-- > 0)) { 435 int refcount; 436 if (vm_map_pmap(map)->pm_stats.resident_count <= desired) 437 return; 438 next = TAILQ_NEXT(p, listq); 439 cnt.v_pdpages++; 440 if (p->wire_count != 0 || 441 p->hold_count != 0 || 442 p->busy != 0 || 443 (p->flags & PG_BUSY) || 444 !pmap_page_exists(vm_map_pmap(map), VM_PAGE_TO_PHYS(p))) { 445 p = next; 446 continue; 447 } 448 449 refcount = pmap_ts_referenced(VM_PAGE_TO_PHYS(p)); 450 if (refcount) { 451 p->flags |= PG_REFERENCED; 452 } else if (p->flags & PG_REFERENCED) { 453 refcount = 1; 454 } 455 456 if ((p->queue != PQ_ACTIVE) && 457 (p->flags & PG_REFERENCED)) { 458 vm_page_activate(p); 459 p->act_count += refcount; 460 p->flags &= ~PG_REFERENCED; 461 } else if (p->queue == PQ_ACTIVE) { 462 if ((p->flags & PG_REFERENCED) == 0) { 463 p->act_count -= min(p->act_count, ACT_DECLINE); 464 if (!remove_mode && (vm_pageout_algorithm_lru || (p->act_count == 0))) { 465 vm_page_protect(p, VM_PROT_NONE); 466 vm_page_deactivate(p); 467 } else { 468 s = splvm(); 469 TAILQ_REMOVE(&vm_page_queue_active, p, pageq); 470 TAILQ_INSERT_TAIL(&vm_page_queue_active, p, pageq); 471 splx(s); 472 } 473 } else { 474 p->flags &= ~PG_REFERENCED; 475 if (p->act_count < (ACT_MAX - ACT_ADVANCE)) 476 p->act_count += ACT_ADVANCE; 477 s = splvm(); 478 TAILQ_REMOVE(&vm_page_queue_active, p, pageq); 479 TAILQ_INSERT_TAIL(&vm_page_queue_active, p, pageq); 480 splx(s); 481 } 482 } else if (p->queue == PQ_INACTIVE) { 483 vm_page_protect(p, VM_PROT_NONE); 484 } 485 p = next; 486 } 487 object = object->backing_object; 488 } 489 return; 490} 491 492/* 493 * deactivate some number of pages in a map, try to do it fairly, but 494 * that is really hard to do. 495 */ 496static void 497vm_pageout_map_deactivate_pages(map, desired) 498 vm_map_t map; 499 vm_pindex_t desired; 500{ 501 vm_map_entry_t tmpe; 502 vm_object_t obj, bigobj; 503 504 vm_map_reference(map); 505 if (!lock_try_write(&map->lock)) { 506 vm_map_deallocate(map); 507 return; 508 } 509 510 bigobj = NULL; 511 512 /* 513 * first, search out the biggest object, and try to free pages from 514 * that. 515 */ 516 tmpe = map->header.next; 517 while (tmpe != &map->header) { 518 if ((tmpe->is_sub_map == 0) && (tmpe->is_a_map == 0)) { 519 obj = tmpe->object.vm_object; 520 if ((obj != NULL) && (obj->shadow_count <= 1) && 521 ((bigobj == NULL) || 522 (bigobj->resident_page_count < obj->resident_page_count))) { 523 bigobj = obj; 524 } 525 } 526 tmpe = tmpe->next; 527 } 528 529 if (bigobj) 530 vm_pageout_object_deactivate_pages(map, bigobj, desired, 0); 531 532 /* 533 * Next, hunt around for other pages to deactivate. We actually 534 * do this search sort of wrong -- .text first is not the best idea. 535 */ 536 tmpe = map->header.next; 537 while (tmpe != &map->header) { 538 if (vm_map_pmap(map)->pm_stats.resident_count <= desired) 539 break; 540 if ((tmpe->is_sub_map == 0) && (tmpe->is_a_map == 0)) { 541 obj = tmpe->object.vm_object; 542 if (obj) 543 vm_pageout_object_deactivate_pages(map, obj, desired, 0); 544 } 545 tmpe = tmpe->next; 546 }; 547 548 /* 549 * Remove all mappings if a process is swapped out, this will free page 550 * table pages. 551 */ 552 if (desired == 0) 553 pmap_remove(vm_map_pmap(map), 554 VM_MIN_ADDRESS, VM_MAXUSER_ADDRESS); 555 vm_map_unlock(map); 556 vm_map_deallocate(map); 557 return; 558} 559#endif 560 561/* 562 * vm_pageout_scan does the dirty work for the pageout daemon. 563 */ 564static int 565vm_pageout_scan() 566{ 567 vm_page_t m, next; 568 int page_shortage, addl_page_shortage, maxscan, maxlaunder, pcount; 569 int pages_freed; 570 struct proc *p, *bigproc; 571 vm_offset_t size, bigsize; 572 vm_object_t object; 573 int force_wakeup = 0; 574 int vnodes_skipped = 0; 575 int s; 576 577 /* 578 * Start scanning the inactive queue for pages we can free. We keep 579 * scanning until we have enough free pages or we have scanned through 580 * the entire queue. If we encounter dirty pages, we start cleaning 581 * them. 582 */ 583 584 pages_freed = 0; 585 addl_page_shortage = 0; 586 587 maxlaunder = (cnt.v_inactive_target > MAXLAUNDER) ? 588 MAXLAUNDER : cnt.v_inactive_target; 589rescan0: 590 maxscan = cnt.v_inactive_count; 591 for( m = TAILQ_FIRST(&vm_page_queue_inactive); 592 593 (m != NULL) && (maxscan-- > 0) && 594 ((cnt.v_cache_count + cnt.v_free_count) < 595 (cnt.v_cache_min + cnt.v_free_target)); 596 597 m = next) { 598 599 cnt.v_pdpages++; 600 601 if (m->queue != PQ_INACTIVE) { 602 goto rescan0; 603 } 604 605 next = TAILQ_NEXT(m, pageq); 606 607 if (m->hold_count) { 608 s = splvm(); 609 TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq); 610 TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq); 611 splx(s); 612 addl_page_shortage++; 613 continue; 614 } 615 /* 616 * Dont mess with busy pages, keep in the front of the 617 * queue, most likely are being paged out. 618 */ 619 if (m->busy || (m->flags & PG_BUSY)) { 620 addl_page_shortage++; 621 continue; 622 } 623 624 if (m->object->ref_count == 0) { 625 m->flags &= ~PG_REFERENCED; 626 pmap_clear_reference(VM_PAGE_TO_PHYS(m)); 627 } else if (((m->flags & PG_REFERENCED) == 0) && 628 pmap_ts_referenced(VM_PAGE_TO_PHYS(m))) { 629 vm_page_activate(m); 630 continue; 631 } 632 633 if ((m->flags & PG_REFERENCED) != 0) { 634 m->flags &= ~PG_REFERENCED; 635 pmap_clear_reference(VM_PAGE_TO_PHYS(m)); 636 vm_page_activate(m); 637 continue; 638 } 639 640 if (m->dirty == 0) { 641 vm_page_test_dirty(m); 642 } else if (m->dirty != 0) { 643 m->dirty = VM_PAGE_BITS_ALL; 644 } 645 646 if (m->valid == 0) { 647 vm_page_protect(m, VM_PROT_NONE); 648 vm_page_free(m); 649 cnt.v_dfree++; 650 ++pages_freed; 651 } else if (m->dirty == 0) { 652 vm_page_cache(m); 653 ++pages_freed; 654 } else if (maxlaunder > 0) { 655 int written; 656 struct vnode *vp = NULL; 657 658 object = m->object; 659 if (object->flags & OBJ_DEAD) { 660 s = splvm(); 661 TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq); 662 TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq); 663 splx(s); 664 continue; 665 } 666 667 if (object->type == OBJT_VNODE) { 668 vp = object->handle; 669 if (VOP_ISLOCKED(vp) || vget(vp, 1)) { 670 if ((m->queue == PQ_INACTIVE) && 671 (m->hold_count == 0) && 672 (m->busy == 0) && 673 (m->flags & PG_BUSY) == 0) { 674 s = splvm(); 675 TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq); 676 TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq); 677 splx(s); 678 } 679 if (object->flags & OBJ_MIGHTBEDIRTY) 680 ++vnodes_skipped; 681 continue; 682 } 683 684 /* 685 * The page might have been moved to another queue 686 * during potential blocking in vget() above. 687 */ 688 if (m->queue != PQ_INACTIVE) { 689 if (object->flags & OBJ_MIGHTBEDIRTY) 690 ++vnodes_skipped; 691 vput(vp); 692 continue; 693 } 694 695 /* 696 * The page may have been busied during the blocking in 697 * vput(); We don't move the page back onto the end of 698 * the queue so that statistics are more correct if we don't. 699 */ 700 if (m->busy || (m->flags & PG_BUSY)) { 701 vput(vp); 702 continue; 703 } 704 705 /* 706 * If the page has become held, then skip it 707 */ 708 if (m->hold_count) { 709 s = splvm(); 710 TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq); 711 TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq); 712 splx(s); 713 if (object->flags & OBJ_MIGHTBEDIRTY) 714 ++vnodes_skipped; 715 vput(vp); 716 continue; 717 } 718 } 719 720 /* 721 * If a page is dirty, then it is either being washed 722 * (but not yet cleaned) or it is still in the 723 * laundry. If it is still in the laundry, then we 724 * start the cleaning operation. 725 */ 726 written = vm_pageout_clean(m, 0); 727 728 if (vp) 729 vput(vp); 730 731 maxlaunder -= written; 732 } 733 } 734 735 /* 736 * Compute the page shortage. If we are still very low on memory be 737 * sure that we will move a minimal amount of pages from active to 738 * inactive. 739 */ 740 741 page_shortage = (cnt.v_inactive_target + cnt.v_cache_min) - 742 (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count); 743 if (page_shortage <= 0) { 744 if (pages_freed == 0) { 745 page_shortage = cnt.v_free_min - cnt.v_free_count; 746 } else { 747 page_shortage = 1; 748 } 749 } 750 if (addl_page_shortage) { 751 if (page_shortage < 0) 752 page_shortage = 0; 753 page_shortage += addl_page_shortage; 754 } 755 756 pcount = cnt.v_active_count; 757 m = TAILQ_FIRST(&vm_page_queue_active); 758 while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) { 759 int refcount; 760 761 if (m->queue != PQ_ACTIVE) { 762 break; 763 } 764 765 next = TAILQ_NEXT(m, pageq); 766 /* 767 * Don't deactivate pages that are busy. 768 */ 769 if ((m->busy != 0) || 770 (m->flags & PG_BUSY) || 771 (m->hold_count != 0)) { 772 s = splvm(); 773 TAILQ_REMOVE(&vm_page_queue_active, m, pageq); 774 TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq); 775 splx(s); 776 m = next; 777 continue; 778 } 779 780 /* 781 * The count for pagedaemon pages is done after checking the 782 * page for eligbility... 783 */ 784 cnt.v_pdpages++; 785 786 refcount = 0; 787 if (m->object->ref_count != 0) { 788 if (m->flags & PG_REFERENCED) { 789 refcount += 1; 790 } 791 refcount += pmap_ts_referenced(VM_PAGE_TO_PHYS(m)); 792 if (refcount) { 793 m->act_count += ACT_ADVANCE + refcount; 794 if (m->act_count > ACT_MAX) 795 m->act_count = ACT_MAX; 796 } 797 } 798 799 m->flags &= ~PG_REFERENCED; 800 801 if (refcount && (m->object->ref_count != 0)) { 802 s = splvm(); 803 TAILQ_REMOVE(&vm_page_queue_active, m, pageq); 804 TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq); 805 splx(s); 806 } else { 807 m->act_count -= min(m->act_count, ACT_DECLINE); 808 if (vm_pageout_algorithm_lru || 809 (m->object->ref_count == 0) || (m->act_count == 0)) { 810 --page_shortage; 811 vm_page_protect(m, VM_PROT_NONE); 812 if ((m->dirty == 0) && 813 (m->object->ref_count == 0)) { 814 vm_page_cache(m); 815 } else { 816 vm_page_deactivate(m); 817 } 818 } else { 819 s = splvm(); 820 TAILQ_REMOVE(&vm_page_queue_active, m, pageq); 821 TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq); 822 splx(s); 823 } 824 } 825 m = next; 826 } 827 828 s = splvm(); 829 /* 830 * We try to maintain some *really* free pages, this allows interrupt 831 * code to be guaranteed space. 832 */ 833 while (cnt.v_free_count < cnt.v_free_reserved) { 834 m = TAILQ_FIRST(&vm_page_queue_cache); 835 if (!m) 836 break; 837 vm_page_free(m); 838 cnt.v_dfree++; 839 } 840 splx(s); 841 842 /* 843 * If we didn't get enough free pages, and we have skipped a vnode 844 * in a writeable object, wakeup the sync daemon. And kick swapout 845 * if we did not get enough free pages. 846 */ 847 if ((cnt.v_cache_count + cnt.v_free_count) < 848 (cnt.v_free_target + cnt.v_cache_min) ) { 849 if (vnodes_skipped && 850 (cnt.v_cache_count + cnt.v_free_count) < cnt.v_free_min) { 851 if (!vfs_update_wakeup) { 852 vfs_update_wakeup = 1; 853 wakeup(&vfs_update_wakeup); 854 } 855 } 856#if !defined(NO_SWAPPING) 857 if (vm_swapping_enabled && 858 (cnt.v_free_count + cnt.v_cache_count < cnt.v_free_target)) { 859 vm_req_vmdaemon(); 860 vm_pageout_req_swapout = 1; 861 } 862#endif 863 } 864 865 866 /* 867 * make sure that we have swap space -- if we are low on memory and 868 * swap -- then kill the biggest process. 869 */ 870 if ((vm_swap_size == 0 || swap_pager_full) && 871 ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_free_min)) { 872 bigproc = NULL; 873 bigsize = 0; 874 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 875 /* 876 * if this is a system process, skip it 877 */ 878 if ((p->p_flag & P_SYSTEM) || (p->p_pid == 1) || 879 ((p->p_pid < 48) && (vm_swap_size != 0))) { 880 continue; 881 } 882 /* 883 * if the process is in a non-running type state, 884 * don't touch it. 885 */ 886 if (p->p_stat != SRUN && p->p_stat != SSLEEP) { 887 continue; 888 } 889 /* 890 * get the process size 891 */ 892 size = p->p_vmspace->vm_pmap.pm_stats.resident_count; 893 /* 894 * if the this process is bigger than the biggest one 895 * remember it. 896 */ 897 if (size > bigsize) { 898 bigproc = p; 899 bigsize = size; 900 } 901 } 902 if (bigproc != NULL) { 903 killproc(bigproc, "out of swap space"); 904 bigproc->p_estcpu = 0; 905 bigproc->p_nice = PRIO_MIN; 906 resetpriority(bigproc); 907 wakeup(&cnt.v_free_count); 908 } 909 } 910 return force_wakeup; 911} 912 913static int 914vm_pageout_free_page_calc(count) 915vm_size_t count; 916{ 917 if (count < cnt.v_page_count) 918 return 0; 919 /* 920 * free_reserved needs to include enough for the largest swap pager 921 * structures plus enough for any pv_entry structs when paging. 922 */ 923 if (cnt.v_page_count > 1024) 924 cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200; 925 else 926 cnt.v_free_min = 4; 927 cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE + 928 cnt.v_interrupt_free_min; 929 cnt.v_free_reserved = vm_pageout_page_count + 930 cnt.v_pageout_free_min + (count / 768); 931 cnt.v_free_min += cnt.v_free_reserved; 932 return 1; 933} 934 935 936#ifdef unused 937int 938vm_pageout_free_pages(object, add) 939vm_object_t object; 940int add; 941{ 942 return vm_pageout_free_page_calc(object->size); 943} 944#endif 945 946/* 947 * vm_pageout is the high level pageout daemon. 948 */ 949static void 950vm_pageout() 951{ 952 (void) spl0(); 953 954 /* 955 * Initialize some paging parameters. 956 */ 957 958 cnt.v_interrupt_free_min = 2; 959 if (cnt.v_page_count < 2000) 960 vm_pageout_page_count = 8; 961 962 vm_pageout_free_page_calc(cnt.v_page_count); 963 /* 964 * free_reserved needs to include enough for the largest swap pager 965 * structures plus enough for any pv_entry structs when paging. 966 */ 967 cnt.v_free_target = 3 * cnt.v_free_min + cnt.v_free_reserved; 968 969 if (cnt.v_free_count > 1024) { 970 cnt.v_cache_max = (cnt.v_free_count - 1024) / 2; 971 cnt.v_cache_min = (cnt.v_free_count - 1024) / 8; 972 cnt.v_inactive_target = 2*cnt.v_cache_min + 192; 973 } else { 974 cnt.v_cache_min = 0; 975 cnt.v_cache_max = 0; 976 cnt.v_inactive_target = cnt.v_free_count / 4; 977 } 978 979 /* XXX does not really belong here */ 980 if (vm_page_max_wired == 0) 981 vm_page_max_wired = cnt.v_free_count / 3; 982 983 984 swap_pager_swap_init(); 985 /* 986 * The pageout daemon is never done, so loop forever. 987 */ 988 while (TRUE) { 989 int inactive_target; 990 int s = splvm(); 991 if (!vm_pages_needed || 992 ((cnt.v_free_count + cnt.v_cache_count) > cnt.v_free_min)) { 993 vm_pages_needed = 0; 994 tsleep(&vm_pages_needed, PVM, "psleep", 0); 995 } else if (!vm_pages_needed) { 996 tsleep(&vm_pages_needed, PVM, "psleep", hz/10); 997 } 998 inactive_target = 999 (cnt.v_page_count - cnt.v_wire_count) / 4; 1000 if (inactive_target < 2*cnt.v_free_min) 1001 inactive_target = 2*cnt.v_free_min; 1002 cnt.v_inactive_target = inactive_target; 1003 if (vm_pages_needed) 1004 cnt.v_pdwakeups++; 1005 vm_pages_needed = 0; 1006 splx(s); 1007 vm_pager_sync(); 1008 vm_pageout_scan(); 1009 vm_pager_sync(); 1010 wakeup(&cnt.v_free_count); 1011 } 1012} 1013 1014#if !defined(NO_SWAPPING) 1015static void 1016vm_req_vmdaemon() 1017{ 1018 static int lastrun = 0; 1019 1020 if ((ticks > (lastrun + hz)) || (ticks < lastrun)) { 1021 wakeup(&vm_daemon_needed); 1022 lastrun = ticks; 1023 } 1024} 1025 1026static void 1027vm_daemon() 1028{ 1029 vm_object_t object; 1030 struct proc *p; 1031 1032 (void) spl0(); 1033 1034 while (TRUE) { 1035 tsleep(&vm_daemon_needed, PUSER, "psleep", 0); 1036 if (vm_pageout_req_swapout) { 1037 swapout_procs(); 1038 vm_pageout_req_swapout = 0; 1039 } 1040 /* 1041 * scan the processes for exceeding their rlimits or if 1042 * process is swapped out -- deactivate pages 1043 */ 1044 1045 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 1046 quad_t limit; 1047 vm_offset_t size; 1048 1049 /* 1050 * if this is a system process or if we have already 1051 * looked at this process, skip it. 1052 */ 1053 if (p->p_flag & (P_SYSTEM | P_WEXIT)) { 1054 continue; 1055 } 1056 /* 1057 * if the process is in a non-running type state, 1058 * don't touch it. 1059 */ 1060 if (p->p_stat != SRUN && p->p_stat != SSLEEP) { 1061 continue; 1062 } 1063 /* 1064 * get a limit 1065 */ 1066 limit = qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur, 1067 p->p_rlimit[RLIMIT_RSS].rlim_max); 1068 1069 /* 1070 * let processes that are swapped out really be 1071 * swapped out set the limit to nothing (will force a 1072 * swap-out.) 1073 */ 1074 if ((p->p_flag & P_INMEM) == 0) 1075 limit = 0; /* XXX */ 1076 1077 size = p->p_vmspace->vm_pmap.pm_stats.resident_count * PAGE_SIZE; 1078 if (limit >= 0 && size >= limit) { 1079 vm_pageout_map_deactivate_pages(&p->p_vmspace->vm_map, 1080 (vm_pindex_t)(limit >> PAGE_SHIFT) ); 1081 } 1082 } 1083 1084 /* 1085 * we remove cached objects that have no RSS... 1086 */ 1087restart: 1088 object = TAILQ_FIRST(&vm_object_cached_list); 1089 while (object) { 1090 /* 1091 * if there are no resident pages -- get rid of the object 1092 */ 1093 if (object->resident_page_count == 0) { 1094 vm_object_reference(object); 1095 pager_cache(object, FALSE); 1096 goto restart; 1097 } 1098 object = TAILQ_NEXT(object, cached_list); 1099 } 1100 } 1101} 1102#endif 1103