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