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)) { 592 vm_page_unlock(m); 593 VM_OBJECT_UNLOCK(object); 594 continue; 595 } 596 if ((m->oflags & VPO_BUSY) != 0 || m->busy != 0) { 597 if (tries == 0) { 598 vm_page_unlock(m); 599 VM_OBJECT_UNLOCK(object); 600 continue; 601 } 602 vm_page_sleep(m, "vpctw0"); 603 VM_OBJECT_UNLOCK(object); 604 return (FALSE); 605 } 606 vm_page_test_dirty(m); 607 if (m->dirty == 0) 608 pmap_remove_all(m); 609 if (m->dirty != 0) { 610 vm_page_unlock(m); 611 if (tries == 0 || (object->flags & OBJ_DEAD) != 0) { 612 VM_OBJECT_UNLOCK(object); 613 continue; 614 } 615 if (object->type == OBJT_VNODE) { 616 vm_page_unlock_queues(); 617 vp = object->handle; 618 vm_object_reference_locked(object); 619 VM_OBJECT_UNLOCK(object); 620 (void)vn_start_write(vp, &mp, V_WAIT); 621 vfslocked = VFS_LOCK_GIANT(vp->v_mount); 622 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 623 VM_OBJECT_LOCK(object); 624 vm_object_page_clean(object, 0, 0, OBJPC_SYNC); 625 VM_OBJECT_UNLOCK(object); 626 VOP_UNLOCK(vp, 0); 627 VFS_UNLOCK_GIANT(vfslocked); 628 vm_object_deallocate(object); 629 vn_finished_write(mp); 630 return (TRUE); 631 } else if (object->type == OBJT_SWAP || 632 object->type == OBJT_DEFAULT) { 633 vm_page_unlock_queues(); 634 m_tmp = m; 635 vm_pageout_flush(&m_tmp, 1, VM_PAGER_PUT_SYNC, 636 0, NULL, NULL); 637 VM_OBJECT_UNLOCK(object); 638 return (TRUE); 639 } 640 } else { 641 vm_page_cache(m); 642 vm_page_unlock(m); 643 } 644 VM_OBJECT_UNLOCK(object); 645 } 646 vm_page_unlock_queues(); 647 return (FALSE); 648 } 649 650 /* 651 * Increase the number of cached pages. The specified value, "tries", 652 * determines which categories of pages are cached: 653 * 654 * 0: All clean, inactive pages within the specified physical address range 655 * are cached. Will not sleep. 656 * 1: The vm_lowmem handlers are called. All inactive pages within 657 * the specified physical address range are cached. May sleep. 658 * 2: The vm_lowmem handlers are called. All inactive and active pages 659 * within the specified physical address range are cached. May sleep. 660 */ 661 void 662 vm_pageout_grow_cache(int tries, vm_paddr_t low, vm_paddr_t high) 663 { 664 int actl, actmax, inactl, inactmax; 665 666 if (tries > 0) { 667 /* 668 * Decrease registered cache sizes. The vm_lowmem handlers 669 * may acquire locks and/or sleep, so they can only be invoked 670 * when "tries" is greater than zero. 671 */ 672 EVENTHANDLER_INVOKE(vm_lowmem, 0); 673 674 /* 675 * We do this explicitly after the caches have been drained 676 * above. 677 */ 678 uma_reclaim(); 679 } 680 inactl = 0; 681 inactmax = cnt.v_inactive_count; 682 actl = 0; 683 actmax = tries < 2 ? 0 : cnt.v_active_count; 684 again: 685 if (inactl < inactmax && vm_pageout_launder(PQ_INACTIVE, tries, low, 686 high)) { 687 inactl++; 688 goto again; 689 } 690 if (actl < actmax && vm_pageout_launder(PQ_ACTIVE, tries, low, high)) { 691 actl++; 692 goto again; 693 } 694 } 695 696 #if !defined(NO_SWAPPING) 697 /* 698 * vm_pageout_object_deactivate_pages 699 * 700 * Deactivate enough pages to satisfy the inactive target 701 * requirements. 702 * 703 * The object and map must be locked. 704 */ 705 static void 706 vm_pageout_object_deactivate_pages(pmap_t pmap, vm_object_t first_object, 707 long desired) 708 { 709 vm_object_t backing_object, object; 710 vm_page_t p; 711 int actcount, remove_mode; 712 713 VM_OBJECT_LOCK_ASSERT(first_object, MA_OWNED); 714 if (first_object->type == OBJT_DEVICE || 715 first_object->type == OBJT_SG) 716 return; 717 for (object = first_object;; object = backing_object) { 718 if (pmap_resident_count(pmap) <= desired) 719 goto unlock_return; 720 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 721 if (object->type == OBJT_PHYS || object->paging_in_progress) 722 goto unlock_return; 723 724 remove_mode = 0; 725 if (object->shadow_count > 1) 726 remove_mode = 1; 727 /* 728 * Scan the object's entire memory queue. 729 */ 730 TAILQ_FOREACH(p, &object->memq, listq) { 731 if (pmap_resident_count(pmap) <= desired) 732 goto unlock_return; 733 if ((p->oflags & VPO_BUSY) != 0 || p->busy != 0) 734 continue; 735 PCPU_INC(cnt.v_pdpages); 736 vm_page_lock(p); 737 if (p->wire_count != 0 || p->hold_count != 0 || 738 !pmap_page_exists_quick(pmap, p)) { 739 vm_page_unlock(p); 740 continue; 741 } 742 actcount = pmap_ts_referenced(p); 743 if ((p->aflags & PGA_REFERENCED) != 0) { 744 if (actcount == 0) 745 actcount = 1; 746 vm_page_aflag_clear(p, PGA_REFERENCED); 747 } 748 if (p->queue != PQ_ACTIVE && actcount != 0) { 749 vm_page_activate(p); 750 p->act_count += actcount; 751 } else if (p->queue == PQ_ACTIVE) { 752 if (actcount == 0) { 753 p->act_count -= min(p->act_count, 754 ACT_DECLINE); 755 if (!remove_mode && 756 (vm_pageout_algorithm || 757 p->act_count == 0)) { 758 pmap_remove_all(p); 759 vm_page_deactivate(p); 760 } else { 761 vm_page_lock_queues(); 762 vm_page_requeue(p); 763 vm_page_unlock_queues(); 764 } 765 } else { 766 vm_page_activate(p); 767 if (p->act_count < ACT_MAX - 768 ACT_ADVANCE) 769 p->act_count += ACT_ADVANCE; 770 vm_page_lock_queues(); 771 vm_page_requeue(p); 772 vm_page_unlock_queues(); 773 } 774 } else if (p->queue == PQ_INACTIVE) 775 pmap_remove_all(p); 776 vm_page_unlock(p); 777 } 778 if ((backing_object = object->backing_object) == NULL) 779 goto unlock_return; 780 VM_OBJECT_LOCK(backing_object); 781 if (object != first_object) 782 VM_OBJECT_UNLOCK(object); 783 } 784 unlock_return: 785 if (object != first_object) 786 VM_OBJECT_UNLOCK(object); 787 } 788 789 /* 790 * deactivate some number of pages in a map, try to do it fairly, but 791 * that is really hard to do. 792 */ 793 static void 794 vm_pageout_map_deactivate_pages(map, desired) 795 vm_map_t map; 796 long desired; 797 { 798 vm_map_entry_t tmpe; 799 vm_object_t obj, bigobj; 800 int nothingwired; 801 802 if (!vm_map_trylock(map)) 803 return; 804 805 bigobj = NULL; 806 nothingwired = TRUE; 807 808 /* 809 * first, search out the biggest object, and try to free pages from 810 * that. 811 */ 812 tmpe = map->header.next; 813 while (tmpe != &map->header) { 814 if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 815 obj = tmpe->object.vm_object; 816 if (obj != NULL && VM_OBJECT_TRYLOCK(obj)) { 817 if (obj->shadow_count <= 1 && 818 (bigobj == NULL || 819 bigobj->resident_page_count < obj->resident_page_count)) { 820 if (bigobj != NULL) 821 VM_OBJECT_UNLOCK(bigobj); 822 bigobj = obj; 823 } else 824 VM_OBJECT_UNLOCK(obj); 825 } 826 } 827 if (tmpe->wired_count > 0) 828 nothingwired = FALSE; 829 tmpe = tmpe->next; 830 } 831 832 if (bigobj != NULL) { 833 vm_pageout_object_deactivate_pages(map->pmap, bigobj, desired); 834 VM_OBJECT_UNLOCK(bigobj); 835 } 836 /* 837 * Next, hunt around for other pages to deactivate. We actually 838 * do this search sort of wrong -- .text first is not the best idea. 839 */ 840 tmpe = map->header.next; 841 while (tmpe != &map->header) { 842 if (pmap_resident_count(vm_map_pmap(map)) <= desired) 843 break; 844 if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 845 obj = tmpe->object.vm_object; 846 if (obj != NULL) { 847 VM_OBJECT_LOCK(obj); 848 vm_pageout_object_deactivate_pages(map->pmap, obj, desired); 849 VM_OBJECT_UNLOCK(obj); 850 } 851 } 852 tmpe = tmpe->next; 853 } 854 855 /* 856 * Remove all mappings if a process is swapped out, this will free page 857 * table pages. 858 */ 859 if (desired == 0 && nothingwired) { 860 pmap_remove(vm_map_pmap(map), vm_map_min(map), 861 vm_map_max(map)); 862 } 863 vm_map_unlock(map); 864 } 865 #endif /* !defined(NO_SWAPPING) */ 866 867 /* 868 * vm_pageout_scan does the dirty work for the pageout daemon. 869 */ 870 static void 871 vm_pageout_scan(int pass) 872 { 873 vm_page_t m, next; 874 struct vm_page marker; 875 int page_shortage, maxscan, pcount; 876 int addl_page_shortage; 877 vm_object_t object; 878 int actcount; 879 int vnodes_skipped = 0; 880 int maxlaunder; 881 boolean_t queues_locked; 882 883 /* 884 * Decrease registered cache sizes. 885 */ 886 EVENTHANDLER_INVOKE(vm_lowmem, 0); 887 /* 888 * We do this explicitly after the caches have been drained above. 889 */ 890 uma_reclaim(); 891 892 addl_page_shortage = atomic_readandclear_int(&vm_pageout_deficit); 893 894 /* 895 * Calculate the number of pages we want to either free or move 896 * to the cache. 897 */ 898 page_shortage = vm_paging_target() + addl_page_shortage; 899 900 vm_pageout_init_marker(&marker, PQ_INACTIVE); 901 902 /* 903 * Start scanning the inactive queue for pages we can move to the 904 * cache or free. The scan will stop when the target is reached or 905 * we have scanned the entire inactive queue. Note that m->act_count 906 * is not used to form decisions for the inactive queue, only for the 907 * active queue. 908 * 909 * maxlaunder limits the number of dirty pages we flush per scan. 910 * For most systems a smaller value (16 or 32) is more robust under 911 * extreme memory and disk pressure because any unnecessary writes 912 * to disk can result in extreme performance degredation. However, 913 * systems with excessive dirty pages (especially when MAP_NOSYNC is 914 * used) will die horribly with limited laundering. If the pageout 915 * daemon cannot clean enough pages in the first pass, we let it go 916 * all out in succeeding passes. 917 */ 918 if ((maxlaunder = vm_max_launder) <= 1) 919 maxlaunder = 1; 920 if (pass) 921 maxlaunder = 10000; 922 vm_page_lock_queues(); 923 queues_locked = TRUE; 924 maxscan = cnt.v_inactive_count; 925 926 for (m = TAILQ_FIRST(&vm_page_queues[PQ_INACTIVE].pl); 927 m != NULL && maxscan-- > 0 && page_shortage > 0; 928 m = next) { 929 KASSERT(queues_locked, ("unlocked queues")); 930 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 931 KASSERT(m->queue == PQ_INACTIVE, ("Inactive queue %p", m)); 932 933 cnt.v_pdpages++; 934 next = TAILQ_NEXT(m, pageq); 935 936 /* 937 * skip marker pages 938 */ 939 if (m->flags & PG_MARKER) 940 continue; 941 942 KASSERT((m->flags & PG_FICTITIOUS) == 0, 943 ("Fictitious page %p cannot be in inactive queue", m)); 944 KASSERT((m->oflags & VPO_UNMANAGED) == 0, 945 ("Unmanaged page %p cannot be in inactive queue", m)); 946 947 /* 948 * Lock the page. 949 */ 950 if (!vm_pageout_page_lock(m, &next)) { 951 vm_page_unlock(m); 952 addl_page_shortage++; 953 continue; 954 } 955 956 /* 957 * A held page may be undergoing I/O, so skip it. 958 */ 959 if (m->hold_count) { 960 vm_page_unlock(m); 961 vm_page_requeue(m); 962 addl_page_shortage++; 963 continue; 964 } 965 966 /* 967 * Don't mess with busy pages, keep in the front of the 968 * queue, most likely are being paged out. 969 */ 970 object = m->object; 971 if (!VM_OBJECT_TRYLOCK(object) && 972 (!vm_pageout_fallback_object_lock(m, &next) || 973 m->hold_count != 0)) { 974 VM_OBJECT_UNLOCK(object); 975 vm_page_unlock(m); 976 addl_page_shortage++; 977 continue; 978 } 979 if (m->busy || (m->oflags & VPO_BUSY)) { 980 vm_page_unlock(m); 981 VM_OBJECT_UNLOCK(object); 982 addl_page_shortage++; 983 continue; 984 } 985 986 /* 987 * We unlock vm_page_queue_mtx, invalidating the 988 * 'next' pointer. Use our marker to remember our 989 * place. 990 */ 991 TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE].pl, 992 m, &marker, pageq); 993 vm_page_unlock_queues(); 994 queues_locked = FALSE; 995 996 /* 997 * If the object is not being used, we ignore previous 998 * references. 999 */ 1000 if (object->ref_count == 0) { 1001 vm_page_aflag_clear(m, PGA_REFERENCED); 1002 KASSERT(!pmap_page_is_mapped(m), 1003 ("vm_pageout_scan: page %p is mapped", m)); 1004 1005 /* 1006 * Otherwise, if the page has been referenced while in the 1007 * inactive queue, we bump the "activation count" upwards, 1008 * making it less likely that the page will be added back to 1009 * the inactive queue prematurely again. Here we check the 1010 * page tables (or emulated bits, if any), given the upper 1011 * level VM system not knowing anything about existing 1012 * references. 1013 */ 1014 } else if ((m->aflags & PGA_REFERENCED) == 0 && 1015 (actcount = pmap_ts_referenced(m)) != 0) { 1016 vm_page_activate(m); 1017 vm_page_unlock(m); 1018 m->act_count += actcount + ACT_ADVANCE; 1019 VM_OBJECT_UNLOCK(object); 1020 goto relock_queues; 1021 } 1022 1023 /* 1024 * If the upper level VM system knows about any page 1025 * references, we activate the page. We also set the 1026 * "activation count" higher than normal so that we will less 1027 * likely place pages back onto the inactive queue again. 1028 */ 1029 if ((m->aflags & PGA_REFERENCED) != 0) { 1030 vm_page_aflag_clear(m, PGA_REFERENCED); 1031 actcount = pmap_ts_referenced(m); 1032 vm_page_activate(m); 1033 vm_page_unlock(m); 1034 m->act_count += actcount + ACT_ADVANCE + 1; 1035 VM_OBJECT_UNLOCK(object); 1036 goto relock_queues; 1037 } 1038 1039 /* 1040 * If the upper level VM system does not believe that the page 1041 * is fully dirty, but it is mapped for write access, then we 1042 * consult the pmap to see if the page's dirty status should 1043 * be updated. 1044 */ 1045 if (m->dirty != VM_PAGE_BITS_ALL && 1046 pmap_page_is_write_mapped(m)) { 1047 /* 1048 * Avoid a race condition: Unless write access is 1049 * removed from the page, another processor could 1050 * modify it before all access is removed by the call 1051 * to vm_page_cache() below. If vm_page_cache() finds 1052 * that the page has been modified when it removes all 1053 * access, it panics because it cannot cache dirty 1054 * pages. In principle, we could eliminate just write 1055 * access here rather than all access. In the expected 1056 * case, when there are no last instant modifications 1057 * to the page, removing all access will be cheaper 1058 * overall. 1059 */ 1060 if (pmap_is_modified(m)) 1061 vm_page_dirty(m); 1062 else if (m->dirty == 0) 1063 pmap_remove_all(m); 1064 } 1065 1066 if (m->valid == 0) { 1067 /* 1068 * Invalid pages can be easily freed 1069 */ 1070 vm_page_free(m); 1071 PCPU_INC(cnt.v_dfree); 1072 --page_shortage; 1073 } else if (m->dirty == 0) { 1074 /* 1075 * Clean pages can be placed onto the cache queue. 1076 * This effectively frees them. 1077 */ 1078 vm_page_cache(m); 1079 --page_shortage; 1080 } else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) { 1081 /* 1082 * Dirty pages need to be paged out, but flushing 1083 * a page is extremely expensive verses freeing 1084 * a clean page. Rather then artificially limiting 1085 * the number of pages we can flush, we instead give 1086 * dirty pages extra priority on the inactive queue 1087 * by forcing them to be cycled through the queue 1088 * twice before being flushed, after which the 1089 * (now clean) page will cycle through once more 1090 * before being freed. This significantly extends 1091 * the thrash point for a heavily loaded machine. 1092 */ 1093 m->flags |= PG_WINATCFLS; 1094 vm_page_lock_queues(); 1095 queues_locked = TRUE; 1096 vm_page_requeue(m); 1097 } else if (maxlaunder > 0) { 1098 /* 1099 * We always want to try to flush some dirty pages if 1100 * we encounter them, to keep the system stable. 1101 * Normally this number is small, but under extreme 1102 * pressure where there are insufficient clean pages 1103 * on the inactive queue, we may have to go all out. 1104 */ 1105 int swap_pageouts_ok, vfslocked = 0; 1106 struct vnode *vp = NULL; 1107 struct mount *mp = NULL; 1108 1109 if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) { 1110 swap_pageouts_ok = 1; 1111 } else { 1112 swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts); 1113 swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts && 1114 vm_page_count_min()); 1115 1116 } 1117 1118 /* 1119 * We don't bother paging objects that are "dead". 1120 * Those objects are in a "rundown" state. 1121 */ 1122 if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) { 1123 vm_page_unlock(m); 1124 VM_OBJECT_UNLOCK(object); 1125 vm_page_lock_queues(); 1126 queues_locked = TRUE; 1127 vm_page_requeue(m); 1128 goto relock_queues; 1129 } 1130 1131 /* 1132 * The object is already known NOT to be dead. It 1133 * is possible for the vget() to block the whole 1134 * pageout daemon, but the new low-memory handling 1135 * code should prevent it. 1136 * 1137 * The previous code skipped locked vnodes and, worse, 1138 * reordered pages in the queue. This results in 1139 * completely non-deterministic operation and, on a 1140 * busy system, can lead to extremely non-optimal 1141 * pageouts. For example, it can cause clean pages 1142 * to be freed and dirty pages to be moved to the end 1143 * of the queue. Since dirty pages are also moved to 1144 * the end of the queue once-cleaned, this gives 1145 * way too large a weighting to defering the freeing 1146 * of dirty pages. 1147 * 1148 * We can't wait forever for the vnode lock, we might 1149 * deadlock due to a vn_read() getting stuck in 1150 * vm_wait while holding this vnode. We skip the 1151 * vnode if we can't get it in a reasonable amount 1152 * of time. 1153 */ 1154 if (object->type == OBJT_VNODE) { 1155 vm_page_unlock(m); 1156 vp = object->handle; 1157 if (vp->v_type == VREG && 1158 vn_start_write(vp, &mp, V_NOWAIT) != 0) { 1159 mp = NULL; 1160 ++pageout_lock_miss; 1161 if (object->flags & OBJ_MIGHTBEDIRTY) 1162 vnodes_skipped++; 1163 goto unlock_and_continue; 1164 } 1165 KASSERT(mp != NULL, 1166 ("vp %p with NULL v_mount", vp)); 1167 vm_object_reference_locked(object); 1168 VM_OBJECT_UNLOCK(object); 1169 vfslocked = VFS_LOCK_GIANT(vp->v_mount); 1170 if (vget(vp, LK_EXCLUSIVE | LK_TIMELOCK, 1171 curthread)) { 1172 VM_OBJECT_LOCK(object); 1173 ++pageout_lock_miss; 1174 if (object->flags & OBJ_MIGHTBEDIRTY) 1175 vnodes_skipped++; 1176 vp = NULL; 1177 goto unlock_and_continue; 1178 } 1179 VM_OBJECT_LOCK(object); 1180 vm_page_lock(m); 1181 vm_page_lock_queues(); 1182 queues_locked = TRUE; 1183 /* 1184 * The page might have been moved to another 1185 * queue during potential blocking in vget() 1186 * above. The page might have been freed and 1187 * reused for another vnode. 1188 */ 1189 if (m->queue != PQ_INACTIVE || 1190 m->object != object || 1191 TAILQ_NEXT(m, pageq) != &marker) { 1192 vm_page_unlock(m); 1193 if (object->flags & OBJ_MIGHTBEDIRTY) 1194 vnodes_skipped++; 1195 goto unlock_and_continue; 1196 } 1197 1198 /* 1199 * The page may have been busied during the 1200 * blocking in vget(). We don't move the 1201 * page back onto the end of the queue so that 1202 * statistics are more correct if we don't. 1203 */ 1204 if (m->busy || (m->oflags & VPO_BUSY)) { 1205 vm_page_unlock(m); 1206 goto unlock_and_continue; 1207 } 1208 1209 /* 1210 * If the page has become held it might 1211 * be undergoing I/O, so skip it 1212 */ 1213 if (m->hold_count) { 1214 vm_page_unlock(m); 1215 vm_page_requeue(m); 1216 if (object->flags & OBJ_MIGHTBEDIRTY) 1217 vnodes_skipped++; 1218 goto unlock_and_continue; 1219 } 1220 vm_page_unlock_queues(); 1221 queues_locked = FALSE; 1222 } 1223 1224 /* 1225 * If a page is dirty, then it is either being washed 1226 * (but not yet cleaned) or it is still in the 1227 * laundry. If it is still in the laundry, then we 1228 * start the cleaning operation. 1229 * 1230 * decrement page_shortage on success to account for 1231 * the (future) cleaned page. Otherwise we could wind 1232 * up laundering or cleaning too many pages. 1233 */ 1234 if (vm_pageout_clean(m) != 0) { 1235 --page_shortage; 1236 --maxlaunder; 1237 } 1238 unlock_and_continue: 1239 vm_page_lock_assert(m, MA_NOTOWNED); 1240 VM_OBJECT_UNLOCK(object); 1241 if (mp != NULL) { 1242 if (queues_locked) { 1243 vm_page_unlock_queues(); 1244 queues_locked = FALSE; 1245 } 1246 if (vp != NULL) 1247 vput(vp); 1248 VFS_UNLOCK_GIANT(vfslocked); 1249 vm_object_deallocate(object); 1250 vn_finished_write(mp); 1251 } 1252 vm_page_lock_assert(m, MA_NOTOWNED); 1253 goto relock_queues; 1254 } 1255 vm_page_unlock(m); 1256 VM_OBJECT_UNLOCK(object); 1257 relock_queues: 1258 if (!queues_locked) { 1259 vm_page_lock_queues(); 1260 queues_locked = TRUE; 1261 } 1262 next = TAILQ_NEXT(&marker, pageq); 1263 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, 1264 &marker, pageq); 1265 } 1266 1267 /* 1268 * Compute the number of pages we want to try to move from the 1269 * active queue to the inactive queue. 1270 */ 1271 page_shortage = vm_paging_target() + 1272 cnt.v_inactive_target - cnt.v_inactive_count; 1273 page_shortage += addl_page_shortage; 1274 1275 /* 1276 * Scan the active queue for things we can deactivate. We nominally 1277 * track the per-page activity counter and use it to locate 1278 * deactivation candidates. 1279 */ 1280 pcount = cnt.v_active_count; 1281 m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl); 1282 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1283 1284 while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) { 1285 1286 KASSERT(m->queue == PQ_ACTIVE, 1287 ("vm_pageout_scan: page %p isn't active", m)); 1288 1289 next = TAILQ_NEXT(m, pageq); 1290 if ((m->flags & PG_MARKER) != 0) { 1291 m = next; 1292 continue; 1293 } 1294 KASSERT((m->flags & PG_FICTITIOUS) == 0, 1295 ("Fictitious page %p cannot be in active queue", m)); 1296 KASSERT((m->oflags & VPO_UNMANAGED) == 0, 1297 ("Unmanaged page %p cannot be in active queue", m)); 1298 if (!vm_pageout_page_lock(m, &next)) { 1299 vm_page_unlock(m); 1300 m = next; 1301 continue; 1302 } 1303 object = m->object; 1304 if (!VM_OBJECT_TRYLOCK(object) && 1305 !vm_pageout_fallback_object_lock(m, &next)) { 1306 VM_OBJECT_UNLOCK(object); 1307 vm_page_unlock(m); 1308 m = next; 1309 continue; 1310 } 1311 1312 /* 1313 * Don't deactivate pages that are busy. 1314 */ 1315 if ((m->busy != 0) || 1316 (m->oflags & VPO_BUSY) || 1317 (m->hold_count != 0)) { 1318 vm_page_unlock(m); 1319 VM_OBJECT_UNLOCK(object); 1320 vm_page_requeue(m); 1321 m = next; 1322 continue; 1323 } 1324 1325 /* 1326 * The count for pagedaemon pages is done after checking the 1327 * page for eligibility... 1328 */ 1329 cnt.v_pdpages++; 1330 1331 /* 1332 * Check to see "how much" the page has been used. 1333 */ 1334 actcount = 0; 1335 if (object->ref_count != 0) { 1336 if (m->aflags & PGA_REFERENCED) { 1337 actcount += 1; 1338 } 1339 actcount += pmap_ts_referenced(m); 1340 if (actcount) { 1341 m->act_count += ACT_ADVANCE + actcount; 1342 if (m->act_count > ACT_MAX) 1343 m->act_count = ACT_MAX; 1344 } 1345 } 1346 1347 /* 1348 * Since we have "tested" this bit, we need to clear it now. 1349 */ 1350 vm_page_aflag_clear(m, PGA_REFERENCED); 1351 1352 /* 1353 * Only if an object is currently being used, do we use the 1354 * page activation count stats. 1355 */ 1356 if (actcount && (object->ref_count != 0)) { 1357 vm_page_requeue(m); 1358 } else { 1359 m->act_count -= min(m->act_count, ACT_DECLINE); 1360 if (vm_pageout_algorithm || 1361 object->ref_count == 0 || 1362 m->act_count == 0) { 1363 page_shortage--; 1364 if (object->ref_count == 0) { 1365 KASSERT(!pmap_page_is_mapped(m), 1366 ("vm_pageout_scan: page %p is mapped", m)); 1367 if (m->dirty == 0) 1368 vm_page_cache(m); 1369 else 1370 vm_page_deactivate(m); 1371 } else { 1372 vm_page_deactivate(m); 1373 } 1374 } else { 1375 vm_page_requeue(m); 1376 } 1377 } 1378 vm_page_unlock(m); 1379 VM_OBJECT_UNLOCK(object); 1380 m = next; 1381 } 1382 vm_page_unlock_queues(); 1383 #if !defined(NO_SWAPPING) 1384 /* 1385 * Idle process swapout -- run once per second. 1386 */ 1387 if (vm_swap_idle_enabled) { 1388 static long lsec; 1389 if (time_second != lsec) { 1390 vm_req_vmdaemon(VM_SWAP_IDLE); 1391 lsec = time_second; 1392 } 1393 } 1394 #endif 1395 1396 /* 1397 * If we didn't get enough free pages, and we have skipped a vnode 1398 * in a writeable object, wakeup the sync daemon. And kick swapout 1399 * if we did not get enough free pages. 1400 */ 1401 if (vm_paging_target() > 0) { 1402 if (vnodes_skipped && vm_page_count_min()) 1403 (void) speedup_syncer(); 1404 #if !defined(NO_SWAPPING) 1405 if (vm_swap_enabled && vm_page_count_target()) 1406 vm_req_vmdaemon(VM_SWAP_NORMAL); 1407 #endif 1408 } 1409 1410 /* 1411 * If we are critically low on one of RAM or swap and low on 1412 * the other, kill the largest process. However, we avoid 1413 * doing this on the first pass in order to give ourselves a 1414 * chance to flush out dirty vnode-backed pages and to allow 1415 * active pages to be moved to the inactive queue and reclaimed. 1416 */ 1417 if (pass != 0 && 1418 ((swap_pager_avail < 64 && vm_page_count_min()) || 1419 (swap_pager_full && vm_paging_target() > 0))) 1420 vm_pageout_oom(VM_OOM_MEM); 1421 } 1422 1423 1424 void 1425 vm_pageout_oom(int shortage) 1426 { 1427 struct proc *p, *bigproc; 1428 vm_offset_t size, bigsize; 1429 struct thread *td; 1430 struct vmspace *vm; 1431 1432 /* 1433 * We keep the process bigproc locked once we find it to keep anyone 1434 * from messing with it; however, there is a possibility of 1435 * deadlock if process B is bigproc and one of it's child processes 1436 * attempts to propagate a signal to B while we are waiting for A's 1437 * lock while walking this list. To avoid this, we don't block on 1438 * the process lock but just skip a process if it is already locked. 1439 */ 1440 bigproc = NULL; 1441 bigsize = 0; 1442 sx_slock(&allproc_lock); 1443 FOREACH_PROC_IN_SYSTEM(p) { 1444 int breakout; 1445 1446 if (PROC_TRYLOCK(p) == 0) 1447 continue; 1448 /* 1449 * If this is a system, protected or killed process, skip it. 1450 */ 1451 if (p->p_state != PRS_NORMAL || 1452 (p->p_flag & (P_INEXEC | P_PROTECTED | P_SYSTEM)) || 1453 (p->p_pid == 1) || P_KILLED(p) || 1454 ((p->p_pid < 48) && (swap_pager_avail != 0))) { 1455 PROC_UNLOCK(p); 1456 continue; 1457 } 1458 /* 1459 * If the process is in a non-running type state, 1460 * don't touch it. Check all the threads individually. 1461 */ 1462 breakout = 0; 1463 FOREACH_THREAD_IN_PROC(p, td) { 1464 thread_lock(td); 1465 if (!TD_ON_RUNQ(td) && 1466 !TD_IS_RUNNING(td) && 1467 !TD_IS_SLEEPING(td) && 1468 !TD_IS_SUSPENDED(td)) { 1469 thread_unlock(td); 1470 breakout = 1; 1471 break; 1472 } 1473 thread_unlock(td); 1474 } 1475 if (breakout) { 1476 PROC_UNLOCK(p); 1477 continue; 1478 } 1479 /* 1480 * get the process size 1481 */ 1482 vm = vmspace_acquire_ref(p); 1483 if (vm == NULL) { 1484 PROC_UNLOCK(p); 1485 continue; 1486 } 1487 if (!vm_map_trylock_read(&vm->vm_map)) { 1488 vmspace_free(vm); 1489 PROC_UNLOCK(p); 1490 continue; 1491 } 1492 size = vmspace_swap_count(vm); 1493 vm_map_unlock_read(&vm->vm_map); 1494 if (shortage == VM_OOM_MEM) 1495 size += vmspace_resident_count(vm); 1496 vmspace_free(vm); 1497 /* 1498 * if the this process is bigger than the biggest one 1499 * remember it. 1500 */ 1501 if (size > bigsize) { 1502 if (bigproc != NULL) 1503 PROC_UNLOCK(bigproc); 1504 bigproc = p; 1505 bigsize = size; 1506 } else 1507 PROC_UNLOCK(p); 1508 } 1509 sx_sunlock(&allproc_lock); 1510 if (bigproc != NULL) { 1511 killproc(bigproc, "out of swap space"); 1512 sched_nice(bigproc, PRIO_MIN); 1513 PROC_UNLOCK(bigproc); 1514 wakeup(&cnt.v_free_count); 1515 } 1516 } 1517 1518 /* 1519 * This routine tries to maintain the pseudo LRU active queue, 1520 * so that during long periods of time where there is no paging, 1521 * that some statistic accumulation still occurs. This code 1522 * helps the situation where paging just starts to occur. 1523 */ 1524 static void 1525 vm_pageout_page_stats() 1526 { 1527 vm_object_t object; 1528 vm_page_t m,next; 1529 int pcount,tpcount; /* Number of pages to check */ 1530 static int fullintervalcount = 0; 1531 int page_shortage; 1532 1533 page_shortage = 1534 (cnt.v_inactive_target + cnt.v_cache_max + cnt.v_free_min) - 1535 (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count); 1536 1537 if (page_shortage <= 0) 1538 return; 1539 1540 vm_page_lock_queues(); 1541 pcount = cnt.v_active_count; 1542 fullintervalcount += vm_pageout_stats_interval; 1543 if (fullintervalcount < vm_pageout_full_stats_interval) { 1544 tpcount = (int64_t)vm_pageout_stats_max * cnt.v_active_count / 1545 cnt.v_page_count; 1546 if (pcount > tpcount) 1547 pcount = tpcount; 1548 } else { 1549 fullintervalcount = 0; 1550 } 1551 1552 m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl); 1553 while ((m != NULL) && (pcount-- > 0)) { 1554 int actcount; 1555 1556 KASSERT(m->queue == PQ_ACTIVE, 1557 ("vm_pageout_page_stats: page %p isn't active", m)); 1558 1559 next = TAILQ_NEXT(m, pageq); 1560 if ((m->flags & PG_MARKER) != 0) { 1561 m = next; 1562 continue; 1563 } 1564 vm_page_lock_assert(m, MA_NOTOWNED); 1565 if (!vm_pageout_page_lock(m, &next)) { 1566 vm_page_unlock(m); 1567 m = next; 1568 continue; 1569 } 1570 object = m->object; 1571 if (!VM_OBJECT_TRYLOCK(object) && 1572 !vm_pageout_fallback_object_lock(m, &next)) { 1573 VM_OBJECT_UNLOCK(object); 1574 vm_page_unlock(m); 1575 m = next; 1576 continue; 1577 } 1578 1579 /* 1580 * Don't deactivate pages that are busy. 1581 */ 1582 if ((m->busy != 0) || 1583 (m->oflags & VPO_BUSY) || 1584 (m->hold_count != 0)) { 1585 vm_page_unlock(m); 1586 VM_OBJECT_UNLOCK(object); 1587 vm_page_requeue(m); 1588 m = next; 1589 continue; 1590 } 1591 1592 actcount = 0; 1593 if (m->aflags & PGA_REFERENCED) { 1594 vm_page_aflag_clear(m, PGA_REFERENCED); 1595 actcount += 1; 1596 } 1597 1598 actcount += pmap_ts_referenced(m); 1599 if (actcount) { 1600 m->act_count += ACT_ADVANCE + actcount; 1601 if (m->act_count > ACT_MAX) 1602 m->act_count = ACT_MAX; 1603 vm_page_requeue(m); 1604 } else { 1605 if (m->act_count == 0) { 1606 /* 1607 * We turn off page access, so that we have 1608 * more accurate RSS stats. We don't do this 1609 * in the normal page deactivation when the 1610 * system is loaded VM wise, because the 1611 * cost of the large number of page protect 1612 * operations would be higher than the value 1613 * of doing the operation. 1614 */ 1615 pmap_remove_all(m); 1616 vm_page_deactivate(m); 1617 } else { 1618 m->act_count -= min(m->act_count, ACT_DECLINE); 1619 vm_page_requeue(m); 1620 } 1621 } 1622 vm_page_unlock(m); 1623 VM_OBJECT_UNLOCK(object); 1624 m = next; 1625 } 1626 vm_page_unlock_queues(); 1627 } 1628 1629 /* 1630 * vm_pageout is the high level pageout daemon. 1631 */ 1632 static void 1633 vm_pageout() 1634 { 1635 int error, pass; 1636 1637 /* 1638 * Initialize some paging parameters. 1639 */ 1640 cnt.v_interrupt_free_min = 2; 1641 if (cnt.v_page_count < 2000) 1642 vm_pageout_page_count = 8; 1643 1644 /* 1645 * v_free_reserved needs to include enough for the largest 1646 * swap pager structures plus enough for any pv_entry structs 1647 * when paging. 1648 */ 1649 if (cnt.v_page_count > 1024) 1650 cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200; 1651 else 1652 cnt.v_free_min = 4; 1653 cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE + 1654 cnt.v_interrupt_free_min; 1655 cnt.v_free_reserved = vm_pageout_page_count + 1656 cnt.v_pageout_free_min + (cnt.v_page_count / 768); 1657 cnt.v_free_severe = cnt.v_free_min / 2; 1658 cnt.v_free_min += cnt.v_free_reserved; 1659 cnt.v_free_severe += cnt.v_free_reserved; 1660 1661 /* 1662 * v_free_target and v_cache_min control pageout hysteresis. Note 1663 * that these are more a measure of the VM cache queue hysteresis 1664 * then the VM free queue. Specifically, v_free_target is the 1665 * high water mark (free+cache pages). 1666 * 1667 * v_free_reserved + v_cache_min (mostly means v_cache_min) is the 1668 * low water mark, while v_free_min is the stop. v_cache_min must 1669 * be big enough to handle memory needs while the pageout daemon 1670 * is signalled and run to free more pages. 1671 */ 1672 if (cnt.v_free_count > 6144) 1673 cnt.v_free_target = 4 * cnt.v_free_min + cnt.v_free_reserved; 1674 else 1675 cnt.v_free_target = 2 * cnt.v_free_min + cnt.v_free_reserved; 1676 1677 if (cnt.v_free_count > 2048) { 1678 cnt.v_cache_min = cnt.v_free_target; 1679 cnt.v_cache_max = 2 * cnt.v_cache_min; 1680 cnt.v_inactive_target = (3 * cnt.v_free_target) / 2; 1681 } else { 1682 cnt.v_cache_min = 0; 1683 cnt.v_cache_max = 0; 1684 cnt.v_inactive_target = cnt.v_free_count / 4; 1685 } 1686 if (cnt.v_inactive_target > cnt.v_free_count / 3) 1687 cnt.v_inactive_target = cnt.v_free_count / 3; 1688 1689 /* XXX does not really belong here */ 1690 if (vm_page_max_wired == 0) 1691 vm_page_max_wired = cnt.v_free_count / 3; 1692 1693 if (vm_pageout_stats_max == 0) 1694 vm_pageout_stats_max = cnt.v_free_target; 1695 1696 /* 1697 * Set interval in seconds for stats scan. 1698 */ 1699 if (vm_pageout_stats_interval == 0) 1700 vm_pageout_stats_interval = 5; 1701 if (vm_pageout_full_stats_interval == 0) 1702 vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4; 1703 1704 swap_pager_swap_init(); 1705 pass = 0; 1706 /* 1707 * The pageout daemon is never done, so loop forever. 1708 */ 1709 while (TRUE) { 1710 /* 1711 * If we have enough free memory, wakeup waiters. Do 1712 * not clear vm_pages_needed until we reach our target, 1713 * otherwise we may be woken up over and over again and 1714 * waste a lot of cpu. 1715 */ 1716 mtx_lock(&vm_page_queue_free_mtx); 1717 if (vm_pages_needed && !vm_page_count_min()) { 1718 if (!vm_paging_needed()) 1719 vm_pages_needed = 0; 1720 wakeup(&cnt.v_free_count); 1721 } 1722 if (vm_pages_needed) { 1723 /* 1724 * Still not done, take a second pass without waiting 1725 * (unlimited dirty cleaning), otherwise sleep a bit 1726 * and try again. 1727 */ 1728 ++pass; 1729 if (pass > 1) 1730 msleep(&vm_pages_needed, 1731 &vm_page_queue_free_mtx, PVM, "psleep", 1732 hz / 2); 1733 } else { 1734 /* 1735 * Good enough, sleep & handle stats. Prime the pass 1736 * for the next run. 1737 */ 1738 if (pass > 1) 1739 pass = 1; 1740 else 1741 pass = 0; 1742 error = msleep(&vm_pages_needed, 1743 &vm_page_queue_free_mtx, PVM, "psleep", 1744 vm_pageout_stats_interval * hz); 1745 if (error && !vm_pages_needed) { 1746 mtx_unlock(&vm_page_queue_free_mtx); 1747 pass = 0; 1748 vm_pageout_page_stats(); 1749 continue; 1750 } 1751 } 1752 if (vm_pages_needed) 1753 cnt.v_pdwakeups++; 1754 mtx_unlock(&vm_page_queue_free_mtx); 1755 vm_pageout_scan(pass); 1756 } 1757 } 1758 1759 /* 1760 * Unless the free page queue lock is held by the caller, this function 1761 * should be regarded as advisory. Specifically, the caller should 1762 * not msleep() on &cnt.v_free_count following this function unless 1763 * the free page queue lock is held until the msleep() is performed. 1764 */ 1765 void 1766 pagedaemon_wakeup() 1767 { 1768 1769 if (!vm_pages_needed && curthread->td_proc != pageproc) { 1770 vm_pages_needed = 1; 1771 wakeup(&vm_pages_needed); 1772 } 1773 } 1774 1775 #if !defined(NO_SWAPPING) 1776 static void 1777 vm_req_vmdaemon(int req) 1778 { 1779 static int lastrun = 0; 1780 1781 mtx_lock(&vm_daemon_mtx); 1782 vm_pageout_req_swapout |= req; 1783 if ((ticks > (lastrun + hz)) || (ticks < lastrun)) { 1784 wakeup(&vm_daemon_needed); 1785 lastrun = ticks; 1786 } 1787 mtx_unlock(&vm_daemon_mtx); 1788 } 1789 1790 static void 1791 vm_daemon() 1792 { 1793 struct rlimit rsslim; 1794 struct proc *p; 1795 struct thread *td; 1796 struct vmspace *vm; 1797 int breakout, swapout_flags, tryagain, attempts; 1798 #ifdef RACCT 1799 uint64_t rsize, ravailable; 1800 #endif 1801 1802 while (TRUE) { 1803 mtx_lock(&vm_daemon_mtx); 1804 #ifdef RACCT 1805 msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", hz); 1806 #else 1807 msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", 0); 1808 #endif 1809 swapout_flags = vm_pageout_req_swapout; 1810 vm_pageout_req_swapout = 0; 1811 mtx_unlock(&vm_daemon_mtx); 1812 if (swapout_flags) 1813 swapout_procs(swapout_flags); 1814 1815 /* 1816 * scan the processes for exceeding their rlimits or if 1817 * process is swapped out -- deactivate pages 1818 */ 1819 tryagain = 0; 1820 attempts = 0; 1821 again: 1822 attempts++; 1823 sx_slock(&allproc_lock); 1824 FOREACH_PROC_IN_SYSTEM(p) { 1825 vm_pindex_t limit, size; 1826 1827 /* 1828 * if this is a system process or if we have already 1829 * looked at this process, skip it. 1830 */ 1831 PROC_LOCK(p); 1832 if (p->p_state != PRS_NORMAL || 1833 p->p_flag & (P_INEXEC | P_SYSTEM | P_WEXIT)) { 1834 PROC_UNLOCK(p); 1835 continue; 1836 } 1837 /* 1838 * if the process is in a non-running type state, 1839 * don't touch it. 1840 */ 1841 breakout = 0; 1842 FOREACH_THREAD_IN_PROC(p, td) { 1843 thread_lock(td); 1844 if (!TD_ON_RUNQ(td) && 1845 !TD_IS_RUNNING(td) && 1846 !TD_IS_SLEEPING(td) && 1847 !TD_IS_SUSPENDED(td)) { 1848 thread_unlock(td); 1849 breakout = 1; 1850 break; 1851 } 1852 thread_unlock(td); 1853 } 1854 if (breakout) { 1855 PROC_UNLOCK(p); 1856 continue; 1857 } 1858 /* 1859 * get a limit 1860 */ 1861 lim_rlimit(p, RLIMIT_RSS, &rsslim); 1862 limit = OFF_TO_IDX( 1863 qmin(rsslim.rlim_cur, rsslim.rlim_max)); 1864 1865 /* 1866 * let processes that are swapped out really be 1867 * swapped out set the limit to nothing (will force a 1868 * swap-out.) 1869 */ 1870 if ((p->p_flag & P_INMEM) == 0) 1871 limit = 0; /* XXX */ 1872 vm = vmspace_acquire_ref(p); 1873 PROC_UNLOCK(p); 1874 if (vm == NULL) 1875 continue; 1876 1877 size = vmspace_resident_count(vm); 1878 if (limit >= 0 && size >= limit) { 1879 vm_pageout_map_deactivate_pages( 1880 &vm->vm_map, limit); 1881 } 1882 #ifdef RACCT 1883 rsize = IDX_TO_OFF(size); 1884 PROC_LOCK(p); 1885 racct_set(p, RACCT_RSS, rsize); 1886 ravailable = racct_get_available(p, RACCT_RSS); 1887 PROC_UNLOCK(p); 1888 if (rsize > ravailable) { 1889 /* 1890 * Don't be overly aggressive; this might be 1891 * an innocent process, and the limit could've 1892 * been exceeded by some memory hog. Don't 1893 * try to deactivate more than 1/4th of process' 1894 * resident set size. 1895 */ 1896 if (attempts <= 8) { 1897 if (ravailable < rsize - (rsize / 4)) 1898 ravailable = rsize - (rsize / 4); 1899 } 1900 vm_pageout_map_deactivate_pages( 1901 &vm->vm_map, OFF_TO_IDX(ravailable)); 1902 /* Update RSS usage after paging out. */ 1903 size = vmspace_resident_count(vm); 1904 rsize = IDX_TO_OFF(size); 1905 PROC_LOCK(p); 1906 racct_set(p, RACCT_RSS, rsize); 1907 PROC_UNLOCK(p); 1908 if (rsize > ravailable) 1909 tryagain = 1; 1910 } 1911 #endif 1912 vmspace_free(vm); 1913 } 1914 sx_sunlock(&allproc_lock); 1915 if (tryagain != 0 && attempts <= 10) 1916 goto again; 1917 } 1918 } 1919 #endif /* !defined(NO_SWAPPING) */ 1920