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