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 static void vm_pageout_requeue(vm_page_t m); 222 223 /* 224 * Initialize a dummy page for marking the caller's place in the specified 225 * paging queue. In principle, this function only needs to set the flag 226 * PG_MARKER. Nonetheless, it sets the flag VPO_BUSY and initializes the hold 227 * count to one as safety precautions. 228 */ 229 static void 230 vm_pageout_init_marker(vm_page_t marker, u_short queue) 231 { 232 233 bzero(marker, sizeof(*marker)); 234 marker->flags = PG_MARKER; 235 marker->oflags = VPO_BUSY; 236 marker->queue = queue; 237 marker->hold_count = 1; 238 } 239 240 /* 241 * vm_pageout_fallback_object_lock: 242 * 243 * Lock vm object currently associated with `m'. VM_OBJECT_TRYLOCK is 244 * known to have failed and page queue must be either PQ_ACTIVE or 245 * PQ_INACTIVE. To avoid lock order violation, unlock the page queues 246 * while locking the vm object. Use marker page to detect page queue 247 * changes and maintain notion of next page on page queue. Return 248 * TRUE if no changes were detected, FALSE otherwise. vm object is 249 * locked on return. 250 * 251 * This function depends on both the lock portion of struct vm_object 252 * and normal struct vm_page being type stable. 253 */ 254 static boolean_t 255 vm_pageout_fallback_object_lock(vm_page_t m, vm_page_t *next) 256 { 257 struct vm_page marker; 258 boolean_t unchanged; 259 u_short queue; 260 vm_object_t object; 261 262 queue = m->queue; 263 vm_pageout_init_marker(&marker, queue); 264 object = m->object; 265 266 TAILQ_INSERT_AFTER(&vm_page_queues[queue].pl, 267 m, &marker, pageq); 268 vm_page_unlock_queues(); 269 vm_page_unlock(m); 270 VM_OBJECT_LOCK(object); 271 vm_page_lock(m); 272 vm_page_lock_queues(); 273 274 /* Page queue might have changed. */ 275 *next = TAILQ_NEXT(&marker, pageq); 276 unchanged = (m->queue == queue && 277 m->object == object && 278 &marker == TAILQ_NEXT(m, pageq)); 279 TAILQ_REMOVE(&vm_page_queues[queue].pl, 280 &marker, pageq); 281 return (unchanged); 282 } 283 284 /* 285 * Lock the page while holding the page queue lock. Use marker page 286 * to detect page queue changes and maintain notion of next page on 287 * page queue. Return TRUE if no changes were detected, FALSE 288 * otherwise. The page is locked on return. The page queue lock might 289 * be dropped and reacquired. 290 * 291 * This function depends on normal struct vm_page being type stable. 292 */ 293 static boolean_t 294 vm_pageout_page_lock(vm_page_t m, vm_page_t *next) 295 { 296 struct vm_page marker; 297 boolean_t unchanged; 298 u_short queue; 299 300 vm_page_lock_assert(m, MA_NOTOWNED); 301 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 302 303 if (vm_page_trylock(m)) 304 return (TRUE); 305 306 queue = m->queue; 307 vm_pageout_init_marker(&marker, queue); 308 309 TAILQ_INSERT_AFTER(&vm_page_queues[queue].pl, m, &marker, pageq); 310 vm_page_unlock_queues(); 311 vm_page_lock(m); 312 vm_page_lock_queues(); 313 314 /* Page queue might have changed. */ 315 *next = TAILQ_NEXT(&marker, pageq); 316 unchanged = (m->queue == queue && &marker == TAILQ_NEXT(m, pageq)); 317 TAILQ_REMOVE(&vm_page_queues[queue].pl, &marker, pageq); 318 return (unchanged); 319 } 320 321 /* 322 * vm_pageout_clean: 323 * 324 * Clean the page and remove it from the laundry. 325 * 326 * We set the busy bit to cause potential page faults on this page to 327 * block. Note the careful timing, however, the busy bit isn't set till 328 * late and we cannot do anything that will mess with the page. 329 */ 330 static int 331 vm_pageout_clean(vm_page_t m) 332 { 333 vm_object_t object; 334 vm_page_t mc[2*vm_pageout_page_count], pb, ps; 335 int pageout_count; 336 int ib, is, page_base; 337 vm_pindex_t pindex = m->pindex; 338 339 vm_page_lock_assert(m, MA_OWNED); 340 object = m->object; 341 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 342 343 /* 344 * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP 345 * with the new swapper, but we could have serious problems paging 346 * out other object types if there is insufficient memory. 347 * 348 * Unfortunately, checking free memory here is far too late, so the 349 * check has been moved up a procedural level. 350 */ 351 352 /* 353 * Can't clean the page if it's busy or held. 354 */ 355 KASSERT(m->busy == 0 && (m->oflags & VPO_BUSY) == 0, 356 ("vm_pageout_clean: page %p is busy", m)); 357 KASSERT(m->hold_count == 0, ("vm_pageout_clean: page %p is held", m)); 358 vm_page_unlock(m); 359 360 mc[vm_pageout_page_count] = pb = ps = m; 361 pageout_count = 1; 362 page_base = vm_pageout_page_count; 363 ib = 1; 364 is = 1; 365 366 /* 367 * Scan object for clusterable pages. 368 * 369 * We can cluster ONLY if: ->> the page is NOT 370 * clean, wired, busy, held, or mapped into a 371 * buffer, and one of the following: 372 * 1) The page is inactive, or a seldom used 373 * active page. 374 * -or- 375 * 2) we force the issue. 376 * 377 * During heavy mmap/modification loads the pageout 378 * daemon can really fragment the underlying file 379 * due to flushing pages out of order and not trying 380 * align the clusters (which leave sporatic out-of-order 381 * holes). To solve this problem we do the reverse scan 382 * first and attempt to align our cluster, then do a 383 * forward scan if room remains. 384 */ 385 more: 386 while (ib && pageout_count < vm_pageout_page_count) { 387 vm_page_t p; 388 389 if (ib > pindex) { 390 ib = 0; 391 break; 392 } 393 394 if ((p = vm_page_prev(pb)) == NULL || 395 (p->oflags & VPO_BUSY) != 0 || p->busy != 0) { 396 ib = 0; 397 break; 398 } 399 vm_page_lock(p); 400 vm_page_test_dirty(p); 401 if (p->dirty == 0 || 402 p->queue != PQ_INACTIVE || 403 p->hold_count != 0) { /* may be undergoing I/O */ 404 vm_page_unlock(p); 405 ib = 0; 406 break; 407 } 408 vm_page_unlock(p); 409 mc[--page_base] = pb = p; 410 ++pageout_count; 411 ++ib; 412 /* 413 * alignment boundry, stop here and switch directions. Do 414 * not clear ib. 415 */ 416 if ((pindex - (ib - 1)) % vm_pageout_page_count == 0) 417 break; 418 } 419 420 while (pageout_count < vm_pageout_page_count && 421 pindex + is < object->size) { 422 vm_page_t p; 423 424 if ((p = vm_page_next(ps)) == NULL || 425 (p->oflags & VPO_BUSY) != 0 || p->busy != 0) 426 break; 427 vm_page_lock(p); 428 vm_page_test_dirty(p); 429 if (p->dirty == 0 || 430 p->queue != PQ_INACTIVE || 431 p->hold_count != 0) { /* may be undergoing I/O */ 432 vm_page_unlock(p); 433 break; 434 } 435 vm_page_unlock(p); 436 mc[page_base + pageout_count] = ps = p; 437 ++pageout_count; 438 ++is; 439 } 440 441 /* 442 * If we exhausted our forward scan, continue with the reverse scan 443 * when possible, even past a page boundry. This catches boundry 444 * conditions. 445 */ 446 if (ib && pageout_count < vm_pageout_page_count) 447 goto more; 448 449 /* 450 * we allow reads during pageouts... 451 */ 452 return (vm_pageout_flush(&mc[page_base], pageout_count, 0, 0, NULL, 453 NULL)); 454 } 455 456 /* 457 * vm_pageout_flush() - launder the given pages 458 * 459 * The given pages are laundered. Note that we setup for the start of 460 * I/O ( i.e. busy the page ), mark it read-only, and bump the object 461 * reference count all in here rather then in the parent. If we want 462 * the parent to do more sophisticated things we may have to change 463 * the ordering. 464 * 465 * Returned runlen is the count of pages between mreq and first 466 * page after mreq with status VM_PAGER_AGAIN. 467 * *eio is set to TRUE if pager returned VM_PAGER_ERROR or VM_PAGER_FAIL 468 * for any page in runlen set. 469 */ 470 int 471 vm_pageout_flush(vm_page_t *mc, int count, int flags, int mreq, int *prunlen, 472 boolean_t *eio) 473 { 474 vm_object_t object = mc[0]->object; 475 int pageout_status[count]; 476 int numpagedout = 0; 477 int i, runlen; 478 479 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 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 573 vm_page_lock_queues(); 574 TAILQ_FOREACH_SAFE(m, &vm_page_queues[queue].pl, pageq, next) { 575 KASSERT(m->queue == queue, 576 ("vm_pageout_launder: page %p's queue is not %d", m, 577 queue)); 578 if ((m->flags & PG_MARKER) != 0) 579 continue; 580 pa = VM_PAGE_TO_PHYS(m); 581 if (pa < low || pa + PAGE_SIZE > high) 582 continue; 583 if (!vm_pageout_page_lock(m, &next) || m->hold_count != 0) { 584 vm_page_unlock(m); 585 continue; 586 } 587 object = m->object; 588 if ((!VM_OBJECT_TRYLOCK(object) && 589 (!vm_pageout_fallback_object_lock(m, &next) || 590 m->hold_count != 0)) || (m->oflags & VPO_BUSY) != 0 || 591 m->busy != 0) { 592 vm_page_unlock(m); 593 VM_OBJECT_UNLOCK(object); 594 continue; 595 } 596 vm_page_test_dirty(m); 597 if (m->dirty == 0 && object->ref_count != 0) 598 pmap_remove_all(m); 599 if (m->dirty != 0) { 600 vm_page_unlock(m); 601 if (tries == 0 || (object->flags & OBJ_DEAD) != 0) { 602 VM_OBJECT_UNLOCK(object); 603 continue; 604 } 605 if (object->type == OBJT_VNODE) { 606 vm_page_unlock_queues(); 607 vp = object->handle; 608 vm_object_reference_locked(object); 609 VM_OBJECT_UNLOCK(object); 610 (void)vn_start_write(vp, &mp, V_WAIT); 611 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 612 VM_OBJECT_LOCK(object); 613 vm_object_page_clean(object, 0, 0, OBJPC_SYNC); 614 VM_OBJECT_UNLOCK(object); 615 VOP_UNLOCK(vp, 0); 616 vm_object_deallocate(object); 617 vn_finished_write(mp); 618 return (TRUE); 619 } else if (object->type == OBJT_SWAP || 620 object->type == OBJT_DEFAULT) { 621 vm_page_unlock_queues(); 622 m_tmp = m; 623 vm_pageout_flush(&m_tmp, 1, VM_PAGER_PUT_SYNC, 624 0, NULL, NULL); 625 VM_OBJECT_UNLOCK(object); 626 return (TRUE); 627 } 628 } else { 629 vm_page_cache(m); 630 vm_page_unlock(m); 631 } 632 VM_OBJECT_UNLOCK(object); 633 } 634 vm_page_unlock_queues(); 635 return (FALSE); 636 } 637 638 /* 639 * Increase the number of cached pages. The specified value, "tries", 640 * determines which categories of pages are cached: 641 * 642 * 0: All clean, inactive pages within the specified physical address range 643 * are cached. Will not sleep. 644 * 1: The vm_lowmem handlers are called. All inactive pages within 645 * the specified physical address range are cached. May sleep. 646 * 2: The vm_lowmem handlers are called. All inactive and active pages 647 * within the specified physical address range are cached. May sleep. 648 */ 649 void 650 vm_pageout_grow_cache(int tries, vm_paddr_t low, vm_paddr_t high) 651 { 652 int actl, actmax, inactl, inactmax; 653 654 if (tries > 0) { 655 /* 656 * Decrease registered cache sizes. The vm_lowmem handlers 657 * may acquire locks and/or sleep, so they can only be invoked 658 * when "tries" is greater than zero. 659 */ 660 EVENTHANDLER_INVOKE(vm_lowmem, 0); 661 662 /* 663 * We do this explicitly after the caches have been drained 664 * above. 665 */ 666 uma_reclaim(); 667 } 668 inactl = 0; 669 inactmax = cnt.v_inactive_count; 670 actl = 0; 671 actmax = tries < 2 ? 0 : cnt.v_active_count; 672 again: 673 if (inactl < inactmax && vm_pageout_launder(PQ_INACTIVE, tries, low, 674 high)) { 675 inactl++; 676 goto again; 677 } 678 if (actl < actmax && vm_pageout_launder(PQ_ACTIVE, tries, low, high)) { 679 actl++; 680 goto again; 681 } 682 } 683 684 #if !defined(NO_SWAPPING) 685 /* 686 * vm_pageout_object_deactivate_pages 687 * 688 * Deactivate enough pages to satisfy the inactive target 689 * requirements. 690 * 691 * The object and map must be locked. 692 */ 693 static void 694 vm_pageout_object_deactivate_pages(pmap_t pmap, vm_object_t first_object, 695 long desired) 696 { 697 vm_object_t backing_object, object; 698 vm_page_t p; 699 int actcount, remove_mode; 700 701 VM_OBJECT_LOCK_ASSERT(first_object, MA_OWNED); 702 if (first_object->type == OBJT_DEVICE || 703 first_object->type == OBJT_SG) 704 return; 705 for (object = first_object;; object = backing_object) { 706 if (pmap_resident_count(pmap) <= desired) 707 goto unlock_return; 708 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 709 if (object->type == OBJT_PHYS || object->paging_in_progress) 710 goto unlock_return; 711 712 remove_mode = 0; 713 if (object->shadow_count > 1) 714 remove_mode = 1; 715 /* 716 * Scan the object's entire memory queue. 717 */ 718 TAILQ_FOREACH(p, &object->memq, listq) { 719 if (pmap_resident_count(pmap) <= desired) 720 goto unlock_return; 721 if ((p->oflags & VPO_BUSY) != 0 || p->busy != 0) 722 continue; 723 PCPU_INC(cnt.v_pdpages); 724 vm_page_lock(p); 725 if (p->wire_count != 0 || p->hold_count != 0 || 726 !pmap_page_exists_quick(pmap, p)) { 727 vm_page_unlock(p); 728 continue; 729 } 730 actcount = pmap_ts_referenced(p); 731 if ((p->aflags & PGA_REFERENCED) != 0) { 732 if (actcount == 0) 733 actcount = 1; 734 vm_page_aflag_clear(p, PGA_REFERENCED); 735 } 736 if (p->queue != PQ_ACTIVE && actcount != 0) { 737 vm_page_activate(p); 738 p->act_count += actcount; 739 } else if (p->queue == PQ_ACTIVE) { 740 if (actcount == 0) { 741 p->act_count -= min(p->act_count, 742 ACT_DECLINE); 743 if (!remove_mode && 744 (vm_pageout_algorithm || 745 p->act_count == 0)) { 746 pmap_remove_all(p); 747 vm_page_deactivate(p); 748 } else { 749 vm_page_lock_queues(); 750 vm_pageout_requeue(p); 751 vm_page_unlock_queues(); 752 } 753 } else { 754 vm_page_activate(p); 755 if (p->act_count < ACT_MAX - 756 ACT_ADVANCE) 757 p->act_count += ACT_ADVANCE; 758 vm_page_lock_queues(); 759 vm_pageout_requeue(p); 760 vm_page_unlock_queues(); 761 } 762 } else if (p->queue == PQ_INACTIVE) 763 pmap_remove_all(p); 764 vm_page_unlock(p); 765 } 766 if ((backing_object = object->backing_object) == NULL) 767 goto unlock_return; 768 VM_OBJECT_LOCK(backing_object); 769 if (object != first_object) 770 VM_OBJECT_UNLOCK(object); 771 } 772 unlock_return: 773 if (object != first_object) 774 VM_OBJECT_UNLOCK(object); 775 } 776 777 /* 778 * deactivate some number of pages in a map, try to do it fairly, but 779 * that is really hard to do. 780 */ 781 static void 782 vm_pageout_map_deactivate_pages(map, desired) 783 vm_map_t map; 784 long desired; 785 { 786 vm_map_entry_t tmpe; 787 vm_object_t obj, bigobj; 788 int nothingwired; 789 790 if (!vm_map_trylock(map)) 791 return; 792 793 bigobj = NULL; 794 nothingwired = TRUE; 795 796 /* 797 * first, search out the biggest object, and try to free pages from 798 * that. 799 */ 800 tmpe = map->header.next; 801 while (tmpe != &map->header) { 802 if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 803 obj = tmpe->object.vm_object; 804 if (obj != NULL && VM_OBJECT_TRYLOCK(obj)) { 805 if (obj->shadow_count <= 1 && 806 (bigobj == NULL || 807 bigobj->resident_page_count < obj->resident_page_count)) { 808 if (bigobj != NULL) 809 VM_OBJECT_UNLOCK(bigobj); 810 bigobj = obj; 811 } else 812 VM_OBJECT_UNLOCK(obj); 813 } 814 } 815 if (tmpe->wired_count > 0) 816 nothingwired = FALSE; 817 tmpe = tmpe->next; 818 } 819 820 if (bigobj != NULL) { 821 vm_pageout_object_deactivate_pages(map->pmap, bigobj, desired); 822 VM_OBJECT_UNLOCK(bigobj); 823 } 824 /* 825 * Next, hunt around for other pages to deactivate. We actually 826 * do this search sort of wrong -- .text first is not the best idea. 827 */ 828 tmpe = map->header.next; 829 while (tmpe != &map->header) { 830 if (pmap_resident_count(vm_map_pmap(map)) <= desired) 831 break; 832 if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 833 obj = tmpe->object.vm_object; 834 if (obj != NULL) { 835 VM_OBJECT_LOCK(obj); 836 vm_pageout_object_deactivate_pages(map->pmap, obj, desired); 837 VM_OBJECT_UNLOCK(obj); 838 } 839 } 840 tmpe = tmpe->next; 841 } 842 843 /* 844 * Remove all mappings if a process is swapped out, this will free page 845 * table pages. 846 */ 847 if (desired == 0 && nothingwired) { 848 pmap_remove(vm_map_pmap(map), vm_map_min(map), 849 vm_map_max(map)); 850 } 851 vm_map_unlock(map); 852 } 853 #endif /* !defined(NO_SWAPPING) */ 854 855 /* 856 * vm_pageout_requeue: 857 * 858 * Move the specified page to the tail of its present page queue. 859 * 860 * The page queues must be locked. 861 */ 862 static void 863 vm_pageout_requeue(vm_page_t m) 864 { 865 struct vpgqueues *vpq; 866 867 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 868 KASSERT(m->queue != PQ_NONE, 869 ("vm_pageout_requeue: page %p is not queued", m)); 870 vpq = &vm_page_queues[m->queue]; 871 TAILQ_REMOVE(&vpq->pl, m, pageq); 872 TAILQ_INSERT_TAIL(&vpq->pl, m, pageq); 873 } 874 875 /* 876 * vm_pageout_scan does the dirty work for the pageout daemon. 877 */ 878 static void 879 vm_pageout_scan(int pass) 880 { 881 vm_page_t m, next; 882 struct vm_page marker; 883 int page_shortage, maxscan, pcount; 884 int addl_page_shortage; 885 vm_object_t object; 886 int actcount; 887 int vnodes_skipped = 0; 888 int maxlaunder; 889 boolean_t queues_locked; 890 891 /* 892 * Decrease registered cache sizes. 893 */ 894 EVENTHANDLER_INVOKE(vm_lowmem, 0); 895 /* 896 * We do this explicitly after the caches have been drained above. 897 */ 898 uma_reclaim(); 899 900 /* 901 * The addl_page_shortage is the number of temporarily 902 * stuck pages in the inactive queue. In other words, the 903 * number of pages from cnt.v_inactive_count that should be 904 * discounted in setting the target for the active queue scan. 905 */ 906 addl_page_shortage = atomic_readandclear_int(&vm_pageout_deficit); 907 908 /* 909 * Calculate the number of pages we want to either free or move 910 * to the cache. 911 */ 912 page_shortage = vm_paging_target() + addl_page_shortage; 913 914 vm_pageout_init_marker(&marker, PQ_INACTIVE); 915 916 /* 917 * Start scanning the inactive queue for pages we can move to the 918 * cache or free. The scan will stop when the target is reached or 919 * we have scanned the entire inactive queue. Note that m->act_count 920 * is not used to form decisions for the inactive queue, only for the 921 * active queue. 922 * 923 * maxlaunder limits the number of dirty pages we flush per scan. 924 * For most systems a smaller value (16 or 32) is more robust under 925 * extreme memory and disk pressure because any unnecessary writes 926 * to disk can result in extreme performance degredation. However, 927 * systems with excessive dirty pages (especially when MAP_NOSYNC is 928 * used) will die horribly with limited laundering. If the pageout 929 * daemon cannot clean enough pages in the first pass, we let it go 930 * all out in succeeding passes. 931 */ 932 if ((maxlaunder = vm_max_launder) <= 1) 933 maxlaunder = 1; 934 if (pass) 935 maxlaunder = 10000; 936 vm_page_lock_queues(); 937 queues_locked = TRUE; 938 maxscan = cnt.v_inactive_count; 939 940 for (m = TAILQ_FIRST(&vm_page_queues[PQ_INACTIVE].pl); 941 m != NULL && maxscan-- > 0 && page_shortage > 0; 942 m = next) { 943 KASSERT(queues_locked, ("unlocked queues")); 944 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 945 KASSERT(m->queue == PQ_INACTIVE, ("Inactive queue %p", m)); 946 947 cnt.v_pdpages++; 948 next = TAILQ_NEXT(m, pageq); 949 950 /* 951 * skip marker pages 952 */ 953 if (m->flags & PG_MARKER) 954 continue; 955 956 KASSERT((m->flags & PG_FICTITIOUS) == 0, 957 ("Fictitious page %p cannot be in inactive queue", m)); 958 KASSERT((m->oflags & VPO_UNMANAGED) == 0, 959 ("Unmanaged page %p cannot be in inactive queue", m)); 960 961 /* 962 * The page or object lock acquisitions fail if the 963 * page was removed from the queue or moved to a 964 * different position within the queue. In either 965 * case, addl_page_shortage should not be incremented. 966 */ 967 if (!vm_pageout_page_lock(m, &next)) { 968 vm_page_unlock(m); 969 continue; 970 } 971 object = m->object; 972 if (!VM_OBJECT_TRYLOCK(object) && 973 !vm_pageout_fallback_object_lock(m, &next)) { 974 vm_page_unlock(m); 975 VM_OBJECT_UNLOCK(object); 976 continue; 977 } 978 979 /* 980 * Don't mess with busy pages, keep them at at the 981 * front of the queue, most likely they are being 982 * paged out. Increment addl_page_shortage for busy 983 * pages, because they may leave the inactive queue 984 * shortly after page scan is finished. 985 */ 986 if (m->busy != 0 || (m->oflags & VPO_BUSY) != 0) { 987 vm_page_unlock(m); 988 VM_OBJECT_UNLOCK(object); 989 addl_page_shortage++; 990 continue; 991 } 992 993 /* 994 * We unlock vm_page_queue_mtx, invalidating the 995 * 'next' pointer. Use our marker to remember our 996 * place. 997 */ 998 TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE].pl, 999 m, &marker, pageq); 1000 vm_page_unlock_queues(); 1001 queues_locked = FALSE; 1002 1003 /* 1004 * If the object is not being used, we ignore previous 1005 * references. 1006 */ 1007 if (object->ref_count == 0) { 1008 vm_page_aflag_clear(m, PGA_REFERENCED); 1009 KASSERT(!pmap_page_is_mapped(m), 1010 ("vm_pageout_scan: page %p is mapped", m)); 1011 1012 /* 1013 * Otherwise, if the page has been referenced while in the 1014 * inactive queue, we bump the "activation count" upwards, 1015 * making it less likely that the page will be added back to 1016 * the inactive queue prematurely again. Here we check the 1017 * page tables (or emulated bits, if any), given the upper 1018 * level VM system not knowing anything about existing 1019 * references. 1020 */ 1021 } else if ((m->aflags & PGA_REFERENCED) == 0 && 1022 (actcount = pmap_ts_referenced(m)) != 0) { 1023 vm_page_activate(m); 1024 vm_page_unlock(m); 1025 m->act_count += actcount + ACT_ADVANCE; 1026 VM_OBJECT_UNLOCK(object); 1027 goto relock_queues; 1028 } 1029 1030 /* 1031 * If the upper level VM system knows about any page 1032 * references, we activate the page. We also set the 1033 * "activation count" higher than normal so that we will less 1034 * likely place pages back onto the inactive queue again. 1035 */ 1036 if ((m->aflags & PGA_REFERENCED) != 0) { 1037 vm_page_aflag_clear(m, PGA_REFERENCED); 1038 actcount = pmap_ts_referenced(m); 1039 vm_page_activate(m); 1040 vm_page_unlock(m); 1041 m->act_count += actcount + ACT_ADVANCE + 1; 1042 VM_OBJECT_UNLOCK(object); 1043 goto relock_queues; 1044 } 1045 1046 if (m->hold_count != 0) { 1047 vm_page_unlock(m); 1048 VM_OBJECT_UNLOCK(object); 1049 1050 /* 1051 * Held pages are essentially stuck in the 1052 * queue. So, they ought to be discounted 1053 * from cnt.v_inactive_count. See the 1054 * calculation of the page_shortage for the 1055 * loop over the active queue below. 1056 */ 1057 addl_page_shortage++; 1058 goto relock_queues; 1059 } 1060 1061 /* 1062 * If the page appears to be clean at the machine-independent 1063 * layer, then remove all of its mappings from the pmap in 1064 * anticipation of placing it onto the cache queue. If, 1065 * however, any of the page's mappings allow write access, 1066 * then the page may still be modified until the last of those 1067 * mappings are removed. 1068 */ 1069 vm_page_test_dirty(m); 1070 if (m->dirty == 0 && object->ref_count != 0) 1071 pmap_remove_all(m); 1072 1073 if (m->valid == 0) { 1074 /* 1075 * Invalid pages can be easily freed 1076 */ 1077 vm_page_free(m); 1078 PCPU_INC(cnt.v_dfree); 1079 --page_shortage; 1080 } else if (m->dirty == 0) { 1081 /* 1082 * Clean pages can be placed onto the cache queue. 1083 * This effectively frees them. 1084 */ 1085 vm_page_cache(m); 1086 --page_shortage; 1087 } else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) { 1088 /* 1089 * Dirty pages need to be paged out, but flushing 1090 * a page is extremely expensive verses freeing 1091 * a clean page. Rather then artificially limiting 1092 * the number of pages we can flush, we instead give 1093 * dirty pages extra priority on the inactive queue 1094 * by forcing them to be cycled through the queue 1095 * twice before being flushed, after which the 1096 * (now clean) page will cycle through once more 1097 * before being freed. This significantly extends 1098 * the thrash point for a heavily loaded machine. 1099 */ 1100 m->flags |= PG_WINATCFLS; 1101 vm_page_lock_queues(); 1102 queues_locked = TRUE; 1103 vm_pageout_requeue(m); 1104 } else if (maxlaunder > 0) { 1105 /* 1106 * We always want to try to flush some dirty pages if 1107 * we encounter them, to keep the system stable. 1108 * Normally this number is small, but under extreme 1109 * pressure where there are insufficient clean pages 1110 * on the inactive queue, we may have to go all out. 1111 */ 1112 int swap_pageouts_ok; 1113 struct vnode *vp = NULL; 1114 struct mount *mp = NULL; 1115 1116 if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) { 1117 swap_pageouts_ok = 1; 1118 } else { 1119 swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts); 1120 swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts && 1121 vm_page_count_min()); 1122 1123 } 1124 1125 /* 1126 * We don't bother paging objects that are "dead". 1127 * Those objects are in a "rundown" state. 1128 */ 1129 if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) { 1130 vm_page_lock_queues(); 1131 vm_page_unlock(m); 1132 VM_OBJECT_UNLOCK(object); 1133 queues_locked = TRUE; 1134 vm_pageout_requeue(m); 1135 goto relock_queues; 1136 } 1137 1138 /* 1139 * The object is already known NOT to be dead. It 1140 * is possible for the vget() to block the whole 1141 * pageout daemon, but the new low-memory handling 1142 * code should prevent it. 1143 * 1144 * The previous code skipped locked vnodes and, worse, 1145 * reordered pages in the queue. This results in 1146 * completely non-deterministic operation and, on a 1147 * busy system, can lead to extremely non-optimal 1148 * pageouts. For example, it can cause clean pages 1149 * to be freed and dirty pages to be moved to the end 1150 * of the queue. Since dirty pages are also moved to 1151 * the end of the queue once-cleaned, this gives 1152 * way too large a weighting to defering the freeing 1153 * of dirty pages. 1154 * 1155 * We can't wait forever for the vnode lock, we might 1156 * deadlock due to a vn_read() getting stuck in 1157 * vm_wait while holding this vnode. We skip the 1158 * vnode if we can't get it in a reasonable amount 1159 * of time. 1160 */ 1161 if (object->type == OBJT_VNODE) { 1162 vm_page_unlock(m); 1163 vp = object->handle; 1164 if (vp->v_type == VREG && 1165 vn_start_write(vp, &mp, V_NOWAIT) != 0) { 1166 mp = NULL; 1167 ++pageout_lock_miss; 1168 if (object->flags & OBJ_MIGHTBEDIRTY) 1169 vnodes_skipped++; 1170 goto unlock_and_continue; 1171 } 1172 KASSERT(mp != NULL, 1173 ("vp %p with NULL v_mount", vp)); 1174 vm_object_reference_locked(object); 1175 VM_OBJECT_UNLOCK(object); 1176 if (vget(vp, LK_EXCLUSIVE | LK_TIMELOCK, 1177 curthread)) { 1178 VM_OBJECT_LOCK(object); 1179 ++pageout_lock_miss; 1180 if (object->flags & OBJ_MIGHTBEDIRTY) 1181 vnodes_skipped++; 1182 vp = NULL; 1183 goto unlock_and_continue; 1184 } 1185 VM_OBJECT_LOCK(object); 1186 vm_page_lock(m); 1187 vm_page_lock_queues(); 1188 queues_locked = TRUE; 1189 /* 1190 * The page might have been moved to another 1191 * queue during potential blocking in vget() 1192 * above. The page might have been freed and 1193 * reused for another vnode. 1194 */ 1195 if (m->queue != PQ_INACTIVE || 1196 m->object != object || 1197 TAILQ_NEXT(m, pageq) != &marker) { 1198 vm_page_unlock(m); 1199 if (object->flags & OBJ_MIGHTBEDIRTY) 1200 vnodes_skipped++; 1201 goto unlock_and_continue; 1202 } 1203 1204 /* 1205 * The page may have been busied during the 1206 * blocking in vget(). We don't move the 1207 * page back onto the end of the queue so that 1208 * statistics are more correct if we don't. 1209 */ 1210 if (m->busy || (m->oflags & VPO_BUSY)) { 1211 vm_page_unlock(m); 1212 goto unlock_and_continue; 1213 } 1214 1215 /* 1216 * If the page has become held it might 1217 * be undergoing I/O, so skip it 1218 */ 1219 if (m->hold_count) { 1220 vm_page_unlock(m); 1221 vm_pageout_requeue(m); 1222 if (object->flags & OBJ_MIGHTBEDIRTY) 1223 vnodes_skipped++; 1224 goto unlock_and_continue; 1225 } 1226 vm_page_unlock_queues(); 1227 queues_locked = FALSE; 1228 } 1229 1230 /* 1231 * If a page is dirty, then it is either being washed 1232 * (but not yet cleaned) or it is still in the 1233 * laundry. If it is still in the laundry, then we 1234 * start the cleaning operation. 1235 * 1236 * decrement page_shortage on success to account for 1237 * the (future) cleaned page. Otherwise we could wind 1238 * up laundering or cleaning too many pages. 1239 */ 1240 if (vm_pageout_clean(m) != 0) { 1241 --page_shortage; 1242 --maxlaunder; 1243 } 1244 unlock_and_continue: 1245 vm_page_lock_assert(m, MA_NOTOWNED); 1246 VM_OBJECT_UNLOCK(object); 1247 if (mp != NULL) { 1248 if (queues_locked) { 1249 vm_page_unlock_queues(); 1250 queues_locked = FALSE; 1251 } 1252 if (vp != NULL) 1253 vput(vp); 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_pageout_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_pageout_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_pageout_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_pageout_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_pageout_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_pageout_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 (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