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