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 781 object = m->object; 782 783 if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) { 784 swap_pageouts_ok = 1; 785 } else { 786 swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts); 787 swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts && 788 vm_page_count_min()); 789 790 } 791 792 /* 793 * We don't bother paging objects that are "dead". 794 * Those objects are in a "rundown" state. 795 */ 796 if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) { 797 s = splvm(); 798 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 799 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 800 splx(s); 801 continue; 802 } 803 804 /* 805 * For now we protect against potential memory 806 * deadlocks by requiring significant memory to be 807 * free if the object is not OBJT_DEFAULT or OBJT_SWAP. 808 * We do not 'trust' any other object type to operate 809 * with low memory, not even OBJT_DEVICE. The VM 810 * allocator will special case allocations done by 811 * the pageout daemon so the check below actually 812 * does have some hysteresis in it. It isn't the best 813 * solution, though. 814 */ 815 816 if (object->type != OBJT_DEFAULT && 817 object->type != OBJT_SWAP && 818 cnt.v_free_count < cnt.v_free_reserved) { 819 s = splvm(); 820 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 821 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, 822 pageq); 823 splx(s); 824 continue; 825 } 826 827 /* 828 * Presumably we have sufficient free memory to do 829 * the more sophisticated checks and locking required 830 * for vnodes. 831 * 832 * The object is already known NOT to be dead. The 833 * vget() may still block, though, because 834 * VOP_ISLOCKED() doesn't check to see if an inode 835 * (v_data) is associated with the vnode. If it isn't, 836 * vget() will load in it from disk. Worse, vget() 837 * may actually get stuck waiting on "inode" if another 838 * process is in the process of bringing the inode in. 839 * This is bad news for us either way. 840 * 841 * So for the moment we check v_data == NULL as a 842 * workaround. This means that vnodes which do not 843 * use v_data in the way we expect probably will not 844 * wind up being paged out by the pager and it will be 845 * up to the syncer to get them. That's better then 846 * us blocking here. 847 * 848 * This whole code section is bogus - we need to fix 849 * the vnode pager to handle vm_page_t's without us 850 * having to do any sophisticated VOP tests. 851 */ 852 853 if (object->type == OBJT_VNODE) { 854 vp = object->handle; 855 856 if (VOP_ISLOCKED(vp, NULL) || 857 vp->v_data == NULL || 858 vget(vp, LK_EXCLUSIVE|LK_NOOBJ, curproc)) { 859 if ((m->queue == PQ_INACTIVE) && 860 (m->hold_count == 0) && 861 (m->busy == 0) && 862 (m->flags & PG_BUSY) == 0) { 863 s = splvm(); 864 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 865 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 866 splx(s); 867 } 868 if (object->flags & OBJ_MIGHTBEDIRTY) 869 vnodes_skipped++; 870 continue; 871 } 872 873 /* 874 * The page might have been moved to another queue 875 * during potential blocking in vget() above. 876 */ 877 if (m->queue != PQ_INACTIVE) { 878 if (object->flags & OBJ_MIGHTBEDIRTY) 879 vnodes_skipped++; 880 vput(vp); 881 continue; 882 } 883 884 /* 885 * The page may have been busied during the blocking in 886 * vput(); We don't move the page back onto the end of 887 * the queue so that statistics are more correct if we don't. 888 */ 889 if (m->busy || (m->flags & PG_BUSY)) { 890 vput(vp); 891 continue; 892 } 893 894 /* 895 * If the page has become held, then skip it 896 */ 897 if (m->hold_count) { 898 s = splvm(); 899 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 900 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 901 splx(s); 902 if (object->flags & OBJ_MIGHTBEDIRTY) 903 vnodes_skipped++; 904 vput(vp); 905 continue; 906 } 907 } 908 909 /* 910 * If a page is dirty, then it is either being washed 911 * (but not yet cleaned) or it is still in the 912 * laundry. If it is still in the laundry, then we 913 * start the cleaning operation. 914 */ 915 written = vm_pageout_clean(m); 916 if (vp) 917 vput(vp); 918 919 maxlaunder -= written; 920 } 921 } 922 923 /* 924 * If we still have a page shortage and we didn't launder anything, 925 * run the inactive scan again and launder something this time. 926 */ 927 928 if (launder_loop == 0 && page_shortage > 0) { 929 launder_loop = 1; 930 maxlaunder = 931 (cnt.v_inactive_target > max_page_launder) ? 932 max_page_launder : cnt.v_inactive_target; 933 goto rescan0; 934 } 935 936 /* 937 * Compute the page shortage from the point of view of having to 938 * move pages from the active queue to the inactive queue. 939 */ 940 941 page_shortage = (cnt.v_inactive_target + cnt.v_cache_min) - 942 (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count); 943 page_shortage += addl_page_shortage; 944 945 /* 946 * Scan the active queue for things we can deactivate 947 */ 948 949 pcount = cnt.v_active_count; 950 m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl); 951 952 while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) { 953 954 /* 955 * This is a consistency check, and should likely be a panic 956 * or warning. 957 */ 958 if (m->queue != PQ_ACTIVE) { 959 break; 960 } 961 962 next = TAILQ_NEXT(m, pageq); 963 /* 964 * Don't deactivate pages that are busy. 965 */ 966 if ((m->busy != 0) || 967 (m->flags & PG_BUSY) || 968 (m->hold_count != 0)) { 969 s = splvm(); 970 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 971 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 972 splx(s); 973 m = next; 974 continue; 975 } 976 977 /* 978 * The count for pagedaemon pages is done after checking the 979 * page for eligibility... 980 */ 981 cnt.v_pdpages++; 982 983 /* 984 * Check to see "how much" the page has been used. 985 */ 986 actcount = 0; 987 if (m->object->ref_count != 0) { 988 if (m->flags & PG_REFERENCED) { 989 actcount += 1; 990 } 991 actcount += pmap_ts_referenced(m); 992 if (actcount) { 993 m->act_count += ACT_ADVANCE + actcount; 994 if (m->act_count > ACT_MAX) 995 m->act_count = ACT_MAX; 996 } 997 } 998 999 /* 1000 * Since we have "tested" this bit, we need to clear it now. 1001 */ 1002 vm_page_flag_clear(m, PG_REFERENCED); 1003 1004 /* 1005 * Only if an object is currently being used, do we use the 1006 * page activation count stats. 1007 */ 1008 if (actcount && (m->object->ref_count != 0)) { 1009 s = splvm(); 1010 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1011 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1012 splx(s); 1013 } else { 1014 m->act_count -= min(m->act_count, ACT_DECLINE); 1015 if (vm_pageout_algorithm_lru || 1016 (m->object->ref_count == 0) || (m->act_count == 0)) { 1017 page_shortage--; 1018 if (m->object->ref_count == 0) { 1019 vm_page_protect(m, VM_PROT_NONE); 1020 if (m->dirty == 0) 1021 vm_page_cache(m); 1022 else 1023 vm_page_deactivate(m); 1024 } else { 1025 vm_page_deactivate(m); 1026 } 1027 } else { 1028 s = splvm(); 1029 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1030 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1031 splx(s); 1032 } 1033 } 1034 m = next; 1035 } 1036 1037 s = splvm(); 1038 1039 /* 1040 * We try to maintain some *really* free pages, this allows interrupt 1041 * code to be guaranteed space. Since both cache and free queues 1042 * are considered basically 'free', moving pages from cache to free 1043 * does not effect other calculations. 1044 */ 1045 1046 while (cnt.v_free_count < cnt.v_free_reserved) { 1047 static int cache_rover = 0; 1048 m = vm_page_list_find(PQ_CACHE, cache_rover, FALSE); 1049 if (!m) 1050 break; 1051 if ((m->flags & (PG_BUSY|PG_UNMANAGED)) || 1052 m->busy || 1053 m->hold_count || 1054 m->wire_count) { 1055 #ifdef INVARIANTS 1056 printf("Warning: busy page %p found in cache\n", m); 1057 #endif 1058 vm_page_deactivate(m); 1059 continue; 1060 } 1061 cache_rover = (cache_rover + PQ_PRIME2) & PQ_L2_MASK; 1062 vm_pageout_page_free(m); 1063 cnt.v_dfree++; 1064 } 1065 splx(s); 1066 1067 #if !defined(NO_SWAPPING) 1068 /* 1069 * Idle process swapout -- run once per second. 1070 */ 1071 if (vm_swap_idle_enabled) { 1072 static long lsec; 1073 if (time_second != lsec) { 1074 vm_pageout_req_swapout |= VM_SWAP_IDLE; 1075 vm_req_vmdaemon(); 1076 lsec = time_second; 1077 } 1078 } 1079 #endif 1080 1081 /* 1082 * If we didn't get enough free pages, and we have skipped a vnode 1083 * in a writeable object, wakeup the sync daemon. And kick swapout 1084 * if we did not get enough free pages. 1085 */ 1086 if (vm_paging_target() > 0) { 1087 if (vnodes_skipped && vm_page_count_min()) 1088 (void) speedup_syncer(); 1089 #if !defined(NO_SWAPPING) 1090 if (vm_swap_enabled && vm_page_count_target()) { 1091 vm_req_vmdaemon(); 1092 vm_pageout_req_swapout |= VM_SWAP_NORMAL; 1093 } 1094 #endif 1095 } 1096 1097 /* 1098 * make sure that we have swap space -- if we are low on memory and 1099 * swap -- then kill the biggest process. 1100 */ 1101 if ((vm_swap_size == 0 || swap_pager_full) && vm_page_count_min()) { 1102 bigproc = NULL; 1103 bigsize = 0; 1104 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 1105 /* 1106 * if this is a system process, skip it 1107 */ 1108 if ((p->p_flag & P_SYSTEM) || (p->p_lock > 0) || 1109 (p->p_pid == 1) || 1110 ((p->p_pid < 48) && (vm_swap_size != 0))) { 1111 continue; 1112 } 1113 /* 1114 * if the process is in a non-running type state, 1115 * don't touch it. 1116 */ 1117 if (p->p_stat != SRUN && p->p_stat != SSLEEP) { 1118 continue; 1119 } 1120 /* 1121 * get the process size 1122 */ 1123 size = vmspace_resident_count(p->p_vmspace); 1124 /* 1125 * if the this process is bigger than the biggest one 1126 * remember it. 1127 */ 1128 if (size > bigsize) { 1129 bigproc = p; 1130 bigsize = size; 1131 } 1132 } 1133 if (bigproc != NULL) { 1134 killproc(bigproc, "out of swap space"); 1135 bigproc->p_estcpu = 0; 1136 bigproc->p_nice = PRIO_MIN; 1137 resetpriority(bigproc); 1138 wakeup(&cnt.v_free_count); 1139 } 1140 } 1141 return force_wakeup; 1142 } 1143 1144 /* 1145 * This routine tries to maintain the pseudo LRU active queue, 1146 * so that during long periods of time where there is no paging, 1147 * that some statistic accumulation still occurs. This code 1148 * helps the situation where paging just starts to occur. 1149 */ 1150 static void 1151 vm_pageout_page_stats() 1152 { 1153 int s; 1154 vm_page_t m,next; 1155 int pcount,tpcount; /* Number of pages to check */ 1156 static int fullintervalcount = 0; 1157 int page_shortage; 1158 int s0; 1159 1160 page_shortage = 1161 (cnt.v_inactive_target + cnt.v_cache_max + cnt.v_free_min) - 1162 (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count); 1163 1164 if (page_shortage <= 0) 1165 return; 1166 1167 s0 = splvm(); 1168 1169 pcount = cnt.v_active_count; 1170 fullintervalcount += vm_pageout_stats_interval; 1171 if (fullintervalcount < vm_pageout_full_stats_interval) { 1172 tpcount = (vm_pageout_stats_max * cnt.v_active_count) / cnt.v_page_count; 1173 if (pcount > tpcount) 1174 pcount = tpcount; 1175 } else { 1176 fullintervalcount = 0; 1177 } 1178 1179 m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl); 1180 while ((m != NULL) && (pcount-- > 0)) { 1181 int actcount; 1182 1183 if (m->queue != PQ_ACTIVE) { 1184 break; 1185 } 1186 1187 next = TAILQ_NEXT(m, pageq); 1188 /* 1189 * Don't deactivate pages that are busy. 1190 */ 1191 if ((m->busy != 0) || 1192 (m->flags & PG_BUSY) || 1193 (m->hold_count != 0)) { 1194 s = splvm(); 1195 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1196 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1197 splx(s); 1198 m = next; 1199 continue; 1200 } 1201 1202 actcount = 0; 1203 if (m->flags & PG_REFERENCED) { 1204 vm_page_flag_clear(m, PG_REFERENCED); 1205 actcount += 1; 1206 } 1207 1208 actcount += pmap_ts_referenced(m); 1209 if (actcount) { 1210 m->act_count += ACT_ADVANCE + actcount; 1211 if (m->act_count > ACT_MAX) 1212 m->act_count = ACT_MAX; 1213 s = splvm(); 1214 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1215 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1216 splx(s); 1217 } else { 1218 if (m->act_count == 0) { 1219 /* 1220 * We turn off page access, so that we have more accurate 1221 * RSS stats. We don't do this in the normal page deactivation 1222 * when the system is loaded VM wise, because the cost of 1223 * the large number of page protect operations would be higher 1224 * than the value of doing the operation. 1225 */ 1226 vm_page_protect(m, VM_PROT_NONE); 1227 vm_page_deactivate(m); 1228 } else { 1229 m->act_count -= min(m->act_count, ACT_DECLINE); 1230 s = splvm(); 1231 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1232 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1233 splx(s); 1234 } 1235 } 1236 1237 m = next; 1238 } 1239 splx(s0); 1240 } 1241 1242 static int 1243 vm_pageout_free_page_calc(count) 1244 vm_size_t count; 1245 { 1246 if (count < cnt.v_page_count) 1247 return 0; 1248 /* 1249 * free_reserved needs to include enough for the largest swap pager 1250 * structures plus enough for any pv_entry structs when paging. 1251 */ 1252 if (cnt.v_page_count > 1024) 1253 cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200; 1254 else 1255 cnt.v_free_min = 4; 1256 cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE + 1257 cnt.v_interrupt_free_min; 1258 cnt.v_free_reserved = vm_pageout_page_count + 1259 cnt.v_pageout_free_min + (count / 768) + PQ_L2_SIZE; 1260 cnt.v_free_severe = cnt.v_free_min / 2; 1261 cnt.v_free_min += cnt.v_free_reserved; 1262 cnt.v_free_severe += cnt.v_free_reserved; 1263 return 1; 1264 } 1265 1266 1267 /* 1268 * vm_pageout is the high level pageout daemon. 1269 */ 1270 static void 1271 vm_pageout() 1272 { 1273 /* 1274 * Initialize some paging parameters. 1275 */ 1276 1277 cnt.v_interrupt_free_min = 2; 1278 if (cnt.v_page_count < 2000) 1279 vm_pageout_page_count = 8; 1280 1281 vm_pageout_free_page_calc(cnt.v_page_count); 1282 /* 1283 * free_reserved needs to include enough for the largest swap pager 1284 * structures plus enough for any pv_entry structs when paging. 1285 */ 1286 if (cnt.v_free_count > 6144) 1287 cnt.v_free_target = 3 * cnt.v_free_min + cnt.v_free_reserved; 1288 else 1289 cnt.v_free_target = 2 * cnt.v_free_min + cnt.v_free_reserved; 1290 1291 if (cnt.v_free_count > 2048) { 1292 cnt.v_cache_min = cnt.v_free_target; 1293 cnt.v_cache_max = 2 * cnt.v_cache_min; 1294 cnt.v_inactive_target = (3 * cnt.v_free_target) / 2; 1295 } else { 1296 cnt.v_cache_min = 0; 1297 cnt.v_cache_max = 0; 1298 cnt.v_inactive_target = cnt.v_free_count / 4; 1299 } 1300 if (cnt.v_inactive_target > cnt.v_free_count / 3) 1301 cnt.v_inactive_target = cnt.v_free_count / 3; 1302 1303 /* XXX does not really belong here */ 1304 if (vm_page_max_wired == 0) 1305 vm_page_max_wired = cnt.v_free_count / 3; 1306 1307 if (vm_pageout_stats_max == 0) 1308 vm_pageout_stats_max = cnt.v_free_target; 1309 1310 /* 1311 * Set interval in seconds for stats scan. 1312 */ 1313 if (vm_pageout_stats_interval == 0) 1314 vm_pageout_stats_interval = 5; 1315 if (vm_pageout_full_stats_interval == 0) 1316 vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4; 1317 1318 1319 /* 1320 * Set maximum free per pass 1321 */ 1322 if (vm_pageout_stats_free_max == 0) 1323 vm_pageout_stats_free_max = 5; 1324 1325 max_page_launder = (cnt.v_page_count > 1800 ? 32 : 16); 1326 1327 curproc->p_flag |= P_BUFEXHAUST; 1328 swap_pager_swap_init(); 1329 /* 1330 * The pageout daemon is never done, so loop forever. 1331 */ 1332 while (TRUE) { 1333 int error; 1334 int s = splvm(); 1335 1336 if (vm_pages_needed && vm_page_count_min()) { 1337 /* 1338 * Still not done, sleep a bit and go again 1339 */ 1340 vm_pages_needed = 0; 1341 tsleep(&vm_pages_needed, PVM, "psleep", hz/2); 1342 } else { 1343 /* 1344 * Good enough, sleep & handle stats 1345 */ 1346 vm_pages_needed = 0; 1347 error = tsleep(&vm_pages_needed, 1348 PVM, "psleep", vm_pageout_stats_interval * hz); 1349 if (error && !vm_pages_needed) { 1350 splx(s); 1351 vm_pageout_page_stats(); 1352 continue; 1353 } 1354 } 1355 1356 if (vm_pages_needed) 1357 cnt.v_pdwakeups++; 1358 vm_pages_needed = 0; 1359 splx(s); 1360 vm_pageout_scan(); 1361 vm_pageout_deficit = 0; 1362 wakeup(&cnt.v_free_count); 1363 } 1364 } 1365 1366 void 1367 pagedaemon_wakeup() 1368 { 1369 if (!vm_pages_needed && curproc != pageproc) { 1370 vm_pages_needed++; 1371 wakeup(&vm_pages_needed); 1372 } 1373 } 1374 1375 #if !defined(NO_SWAPPING) 1376 static void 1377 vm_req_vmdaemon() 1378 { 1379 static int lastrun = 0; 1380 1381 if ((ticks > (lastrun + hz)) || (ticks < lastrun)) { 1382 wakeup(&vm_daemon_needed); 1383 lastrun = ticks; 1384 } 1385 } 1386 1387 static void 1388 vm_daemon() 1389 { 1390 struct proc *p; 1391 1392 while (TRUE) { 1393 tsleep(&vm_daemon_needed, PPAUSE, "psleep", 0); 1394 if (vm_pageout_req_swapout) { 1395 swapout_procs(vm_pageout_req_swapout); 1396 vm_pageout_req_swapout = 0; 1397 } 1398 /* 1399 * scan the processes for exceeding their rlimits or if 1400 * process is swapped out -- deactivate pages 1401 */ 1402 1403 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 1404 vm_pindex_t limit, size; 1405 1406 /* 1407 * if this is a system process or if we have already 1408 * looked at this process, skip it. 1409 */ 1410 if (p->p_flag & (P_SYSTEM | P_WEXIT)) { 1411 continue; 1412 } 1413 /* 1414 * if the process is in a non-running type state, 1415 * don't touch it. 1416 */ 1417 if (p->p_stat != SRUN && p->p_stat != SSLEEP) { 1418 continue; 1419 } 1420 /* 1421 * get a limit 1422 */ 1423 limit = OFF_TO_IDX( 1424 qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur, 1425 p->p_rlimit[RLIMIT_RSS].rlim_max)); 1426 1427 /* 1428 * let processes that are swapped out really be 1429 * swapped out set the limit to nothing (will force a 1430 * swap-out.) 1431 */ 1432 if ((p->p_flag & P_INMEM) == 0) 1433 limit = 0; /* XXX */ 1434 1435 size = vmspace_resident_count(p->p_vmspace); 1436 if (limit >= 0 && size >= limit) { 1437 vm_pageout_map_deactivate_pages( 1438 &p->p_vmspace->vm_map, limit); 1439 } 1440 } 1441 } 1442 } 1443 #endif 1444