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