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