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