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