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