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