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