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