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