vm_pageout.c revision 155320
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: head/sys/vm/vm_pageout.c 155320 2006-02-04 22:37:10Z alc $"); 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/resourcevar.h> 89#include <sys/sched.h> 90#include <sys/signalvar.h> 91#include <sys/vnode.h> 92#include <sys/vmmeter.h> 93#include <sys/sx.h> 94#include <sys/sysctl.h> 95 96#include <vm/vm.h> 97#include <vm/vm_param.h> 98#include <vm/vm_object.h> 99#include <vm/vm_page.h> 100#include <vm/vm_map.h> 101#include <vm/vm_pageout.h> 102#include <vm/vm_pager.h> 103#include <vm/swap_pager.h> 104#include <vm/vm_extern.h> 105#include <vm/uma.h> 106 107#include <machine/mutex.h> 108 109/* 110 * System initialization 111 */ 112 113/* the kernel process "vm_pageout"*/ 114static void vm_pageout(void); 115static int vm_pageout_clean(vm_page_t); 116static void vm_pageout_scan(int pass); 117 118struct proc *pageproc; 119 120static struct kproc_desc page_kp = { 121 "pagedaemon", 122 vm_pageout, 123 &pageproc 124}; 125SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, &page_kp) 126 127#if !defined(NO_SWAPPING) 128/* the kernel process "vm_daemon"*/ 129static void vm_daemon(void); 130static struct proc *vmproc; 131 132static struct kproc_desc vm_kp = { 133 "vmdaemon", 134 vm_daemon, 135 &vmproc 136}; 137SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp) 138#endif 139 140 141int vm_pages_needed; /* Event on which pageout daemon sleeps */ 142int vm_pageout_deficit; /* Estimated number of pages deficit */ 143int vm_pageout_pages_needed; /* flag saying that the pageout daemon needs pages */ 144 145#if !defined(NO_SWAPPING) 146static int vm_pageout_req_swapout; /* XXX */ 147static int vm_daemon_needed; 148#endif 149static int vm_max_launder = 32; 150static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0; 151static int vm_pageout_full_stats_interval = 0; 152static int vm_pageout_algorithm=0; 153static int defer_swap_pageouts=0; 154static int disable_swap_pageouts=0; 155 156#if defined(NO_SWAPPING) 157static int vm_swap_enabled=0; 158static int vm_swap_idle_enabled=0; 159#else 160static int vm_swap_enabled=1; 161static int vm_swap_idle_enabled=0; 162#endif 163 164SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm, 165 CTLFLAG_RW, &vm_pageout_algorithm, 0, "LRU page mgmt"); 166 167SYSCTL_INT(_vm, OID_AUTO, max_launder, 168 CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout"); 169 170SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max, 171 CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length"); 172 173SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval, 174 CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan"); 175 176SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval, 177 CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan"); 178 179#if defined(NO_SWAPPING) 180SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 181 CTLFLAG_RD, &vm_swap_enabled, 0, ""); 182SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 183 CTLFLAG_RD, &vm_swap_idle_enabled, 0, ""); 184#else 185SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, 186 CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout"); 187SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, 188 CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria"); 189#endif 190 191SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts, 192 CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem"); 193 194SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts, 195 CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages"); 196 197static int pageout_lock_miss; 198SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss, 199 CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout"); 200 201#define VM_PAGEOUT_PAGE_COUNT 16 202int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT; 203 204int vm_page_max_wired; /* XXX max # of wired pages system-wide */ 205 206#if !defined(NO_SWAPPING) 207static void vm_pageout_map_deactivate_pages(vm_map_t, long); 208static void vm_pageout_object_deactivate_pages(pmap_t, vm_object_t, long); 209static void vm_req_vmdaemon(void); 210#endif 211static void vm_pageout_page_stats(void); 212 213/* 214 * vm_pageout_fallback_object_lock: 215 * 216 * Lock vm object currently associated with `m'. VM_OBJECT_TRYLOCK is 217 * known to have failed and page queue must be either PQ_ACTIVE or 218 * PQ_INACTIVE. To avoid lock order violation, unlock the page queues 219 * while locking the vm object. Use marker page to detect page queue 220 * changes and maintain notion of next page on page queue. Return 221 * TRUE if no changes were detected, FALSE otherwise. vm object is 222 * locked on return. 223 * 224 * This function depends on both the lock portion of struct vm_object 225 * and normal struct vm_page being type stable. 226 */ 227static boolean_t 228vm_pageout_fallback_object_lock(vm_page_t m, vm_page_t *next) 229{ 230 struct vm_page marker; 231 boolean_t unchanged; 232 u_short queue; 233 vm_object_t object; 234 235 /* 236 * Initialize our marker 237 */ 238 bzero(&marker, sizeof(marker)); 239 marker.flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER; 240 marker.queue = m->queue; 241 marker.wire_count = 1; 242 243 queue = m->queue; 244 object = m->object; 245 246 TAILQ_INSERT_AFTER(&vm_page_queues[queue].pl, 247 m, &marker, pageq); 248 vm_page_unlock_queues(); 249 VM_OBJECT_LOCK(object); 250 vm_page_lock_queues(); 251 252 /* Page queue might have changed. */ 253 *next = TAILQ_NEXT(&marker, pageq); 254 unchanged = (m->queue == queue && 255 m->object == object && 256 &marker == TAILQ_NEXT(m, pageq)); 257 TAILQ_REMOVE(&vm_page_queues[queue].pl, 258 &marker, pageq); 259 return (unchanged); 260} 261 262/* 263 * vm_pageout_clean: 264 * 265 * Clean the page and remove it from the laundry. 266 * 267 * We set the busy bit to cause potential page faults on this page to 268 * block. Note the careful timing, however, the busy bit isn't set till 269 * late and we cannot do anything that will mess with the page. 270 */ 271static int 272vm_pageout_clean(m) 273 vm_page_t m; 274{ 275 vm_object_t object; 276 vm_page_t mc[2*vm_pageout_page_count]; 277 int pageout_count; 278 int ib, is, page_base; 279 vm_pindex_t pindex = m->pindex; 280 281 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 282 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 283 284 /* 285 * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP 286 * with the new swapper, but we could have serious problems paging 287 * out other object types if there is insufficient memory. 288 * 289 * Unfortunately, checking free memory here is far too late, so the 290 * check has been moved up a procedural level. 291 */ 292 293 /* 294 * Don't mess with the page if it's busy, held, or special 295 */ 296 if ((m->hold_count != 0) || 297 ((m->busy != 0) || (m->flags & (PG_BUSY|PG_UNMANAGED)))) { 298 return 0; 299 } 300 301 mc[vm_pageout_page_count] = m; 302 pageout_count = 1; 303 page_base = vm_pageout_page_count; 304 ib = 1; 305 is = 1; 306 307 /* 308 * Scan object for clusterable pages. 309 * 310 * We can cluster ONLY if: ->> the page is NOT 311 * clean, wired, busy, held, or mapped into a 312 * buffer, and one of the following: 313 * 1) The page is inactive, or a seldom used 314 * active page. 315 * -or- 316 * 2) we force the issue. 317 * 318 * During heavy mmap/modification loads the pageout 319 * daemon can really fragment the underlying file 320 * due to flushing pages out of order and not trying 321 * align the clusters (which leave sporatic out-of-order 322 * holes). To solve this problem we do the reverse scan 323 * first and attempt to align our cluster, then do a 324 * forward scan if room remains. 325 */ 326 object = m->object; 327more: 328 while (ib && pageout_count < vm_pageout_page_count) { 329 vm_page_t p; 330 331 if (ib > pindex) { 332 ib = 0; 333 break; 334 } 335 336 if ((p = vm_page_lookup(object, pindex - ib)) == NULL) { 337 ib = 0; 338 break; 339 } 340 if (VM_PAGE_INQUEUE1(p, PQ_CACHE) || 341 (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) { 342 ib = 0; 343 break; 344 } 345 vm_page_test_dirty(p); 346 if ((p->dirty & p->valid) == 0 || 347 p->queue != PQ_INACTIVE || 348 p->wire_count != 0 || /* may be held by buf cache */ 349 p->hold_count != 0) { /* may be undergoing I/O */ 350 ib = 0; 351 break; 352 } 353 mc[--page_base] = p; 354 ++pageout_count; 355 ++ib; 356 /* 357 * alignment boundry, stop here and switch directions. Do 358 * not clear ib. 359 */ 360 if ((pindex - (ib - 1)) % vm_pageout_page_count == 0) 361 break; 362 } 363 364 while (pageout_count < vm_pageout_page_count && 365 pindex + is < object->size) { 366 vm_page_t p; 367 368 if ((p = vm_page_lookup(object, pindex + is)) == NULL) 369 break; 370 if (VM_PAGE_INQUEUE1(p, PQ_CACHE) || 371 (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) { 372 break; 373 } 374 vm_page_test_dirty(p); 375 if ((p->dirty & p->valid) == 0 || 376 p->queue != PQ_INACTIVE || 377 p->wire_count != 0 || /* may be held by buf cache */ 378 p->hold_count != 0) { /* may be undergoing I/O */ 379 break; 380 } 381 mc[page_base + pageout_count] = p; 382 ++pageout_count; 383 ++is; 384 } 385 386 /* 387 * If we exhausted our forward scan, continue with the reverse scan 388 * when possible, even past a page boundry. This catches boundry 389 * conditions. 390 */ 391 if (ib && pageout_count < vm_pageout_page_count) 392 goto more; 393 394 /* 395 * we allow reads during pageouts... 396 */ 397 return (vm_pageout_flush(&mc[page_base], pageout_count, 0)); 398} 399 400/* 401 * vm_pageout_flush() - launder the given pages 402 * 403 * The given pages are laundered. Note that we setup for the start of 404 * I/O ( i.e. busy the page ), mark it read-only, and bump the object 405 * reference count all in here rather then in the parent. If we want 406 * the parent to do more sophisticated things we may have to change 407 * the ordering. 408 */ 409int 410vm_pageout_flush(vm_page_t *mc, int count, int flags) 411{ 412 vm_object_t object = mc[0]->object; 413 int pageout_status[count]; 414 int numpagedout = 0; 415 int i; 416 417 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 418 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 419 /* 420 * Initiate I/O. Bump the vm_page_t->busy counter and 421 * mark the pages read-only. 422 * 423 * We do not have to fixup the clean/dirty bits here... we can 424 * allow the pager to do it after the I/O completes. 425 * 426 * NOTE! mc[i]->dirty may be partial or fragmented due to an 427 * edge case with file fragments. 428 */ 429 for (i = 0; i < count; i++) { 430 KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL, 431 ("vm_pageout_flush: partially invalid page %p index %d/%d", 432 mc[i], i, count)); 433 vm_page_io_start(mc[i]); 434 pmap_page_protect(mc[i], VM_PROT_READ); 435 } 436 vm_page_unlock_queues(); 437 vm_object_pip_add(object, count); 438 439 vm_pager_put_pages(object, mc, count, 440 (flags | ((object == kernel_object) ? VM_PAGER_PUT_SYNC : 0)), 441 pageout_status); 442 443 vm_page_lock_queues(); 444 for (i = 0; i < count; i++) { 445 vm_page_t mt = mc[i]; 446 447 KASSERT((mt->flags & PG_WRITEABLE) == 0, 448 ("vm_pageout_flush: page %p is not write protected", mt)); 449 switch (pageout_status[i]) { 450 case VM_PAGER_OK: 451 case VM_PAGER_PEND: 452 numpagedout++; 453 break; 454 case VM_PAGER_BAD: 455 /* 456 * Page outside of range of object. Right now we 457 * essentially lose the changes by pretending it 458 * worked. 459 */ 460 pmap_clear_modify(mt); 461 vm_page_undirty(mt); 462 break; 463 case VM_PAGER_ERROR: 464 case VM_PAGER_FAIL: 465 /* 466 * If page couldn't be paged out, then reactivate the 467 * page so it doesn't clog the inactive list. (We 468 * will try paging out it again later). 469 */ 470 vm_page_activate(mt); 471 break; 472 case VM_PAGER_AGAIN: 473 break; 474 } 475 476 /* 477 * If the operation is still going, leave the page busy to 478 * block all other accesses. Also, leave the paging in 479 * progress indicator set so that we don't attempt an object 480 * collapse. 481 */ 482 if (pageout_status[i] != VM_PAGER_PEND) { 483 vm_object_pip_wakeup(object); 484 vm_page_io_finish(mt); 485 if (vm_page_count_severe()) 486 vm_page_try_to_cache(mt); 487 } 488 } 489 return numpagedout; 490} 491 492#if !defined(NO_SWAPPING) 493/* 494 * vm_pageout_object_deactivate_pages 495 * 496 * deactivate enough pages to satisfy the inactive target 497 * requirements or if vm_page_proc_limit is set, then 498 * deactivate all of the pages in the object and its 499 * backing_objects. 500 * 501 * The object and map must be locked. 502 */ 503static void 504vm_pageout_object_deactivate_pages(pmap, first_object, desired) 505 pmap_t pmap; 506 vm_object_t first_object; 507 long desired; 508{ 509 vm_object_t backing_object, object; 510 vm_page_t p, next; 511 int actcount, rcount, remove_mode; 512 513 VM_OBJECT_LOCK_ASSERT(first_object, MA_OWNED); 514 if (first_object->type == OBJT_DEVICE || first_object->type == OBJT_PHYS) 515 return; 516 for (object = first_object;; object = backing_object) { 517 if (pmap_resident_count(pmap) <= desired) 518 goto unlock_return; 519 if (object->paging_in_progress) 520 goto unlock_return; 521 522 remove_mode = 0; 523 if (object->shadow_count > 1) 524 remove_mode = 1; 525 /* 526 * scan the objects entire memory queue 527 */ 528 rcount = object->resident_page_count; 529 p = TAILQ_FIRST(&object->memq); 530 vm_page_lock_queues(); 531 while (p && (rcount-- > 0)) { 532 if (pmap_resident_count(pmap) <= desired) { 533 vm_page_unlock_queues(); 534 goto unlock_return; 535 } 536 next = TAILQ_NEXT(p, listq); 537 cnt.v_pdpages++; 538 if (p->wire_count != 0 || 539 p->hold_count != 0 || 540 p->busy != 0 || 541 (p->flags & (PG_BUSY|PG_UNMANAGED)) || 542 !pmap_page_exists_quick(pmap, p)) { 543 p = next; 544 continue; 545 } 546 actcount = pmap_ts_referenced(p); 547 if (actcount) { 548 vm_page_flag_set(p, PG_REFERENCED); 549 } else if (p->flags & PG_REFERENCED) { 550 actcount = 1; 551 } 552 if ((p->queue != PQ_ACTIVE) && 553 (p->flags & PG_REFERENCED)) { 554 vm_page_activate(p); 555 p->act_count += actcount; 556 vm_page_flag_clear(p, PG_REFERENCED); 557 } else if (p->queue == PQ_ACTIVE) { 558 if ((p->flags & PG_REFERENCED) == 0) { 559 p->act_count -= min(p->act_count, ACT_DECLINE); 560 if (!remove_mode && (vm_pageout_algorithm || (p->act_count == 0))) { 561 pmap_remove_all(p); 562 vm_page_deactivate(p); 563 } else { 564 vm_pageq_requeue(p); 565 } 566 } else { 567 vm_page_activate(p); 568 vm_page_flag_clear(p, PG_REFERENCED); 569 if (p->act_count < (ACT_MAX - ACT_ADVANCE)) 570 p->act_count += ACT_ADVANCE; 571 vm_pageq_requeue(p); 572 } 573 } else if (p->queue == PQ_INACTIVE) { 574 pmap_remove_all(p); 575 } 576 p = next; 577 } 578 vm_page_unlock_queues(); 579 if ((backing_object = object->backing_object) == NULL) 580 goto unlock_return; 581 VM_OBJECT_LOCK(backing_object); 582 if (object != first_object) 583 VM_OBJECT_UNLOCK(object); 584 } 585unlock_return: 586 if (object != first_object) 587 VM_OBJECT_UNLOCK(object); 588} 589 590/* 591 * deactivate some number of pages in a map, try to do it fairly, but 592 * that is really hard to do. 593 */ 594static void 595vm_pageout_map_deactivate_pages(map, desired) 596 vm_map_t map; 597 long desired; 598{ 599 vm_map_entry_t tmpe; 600 vm_object_t obj, bigobj; 601 int nothingwired; 602 603 if (!vm_map_trylock(map)) 604 return; 605 606 bigobj = NULL; 607 nothingwired = TRUE; 608 609 /* 610 * first, search out the biggest object, and try to free pages from 611 * that. 612 */ 613 tmpe = map->header.next; 614 while (tmpe != &map->header) { 615 if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 616 obj = tmpe->object.vm_object; 617 if (obj != NULL && VM_OBJECT_TRYLOCK(obj)) { 618 if (obj->shadow_count <= 1 && 619 (bigobj == NULL || 620 bigobj->resident_page_count < obj->resident_page_count)) { 621 if (bigobj != NULL) 622 VM_OBJECT_UNLOCK(bigobj); 623 bigobj = obj; 624 } else 625 VM_OBJECT_UNLOCK(obj); 626 } 627 } 628 if (tmpe->wired_count > 0) 629 nothingwired = FALSE; 630 tmpe = tmpe->next; 631 } 632 633 if (bigobj != NULL) { 634 vm_pageout_object_deactivate_pages(map->pmap, bigobj, desired); 635 VM_OBJECT_UNLOCK(bigobj); 636 } 637 /* 638 * Next, hunt around for other pages to deactivate. We actually 639 * do this search sort of wrong -- .text first is not the best idea. 640 */ 641 tmpe = map->header.next; 642 while (tmpe != &map->header) { 643 if (pmap_resident_count(vm_map_pmap(map)) <= desired) 644 break; 645 if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 646 obj = tmpe->object.vm_object; 647 if (obj != NULL) { 648 VM_OBJECT_LOCK(obj); 649 vm_pageout_object_deactivate_pages(map->pmap, obj, desired); 650 VM_OBJECT_UNLOCK(obj); 651 } 652 } 653 tmpe = tmpe->next; 654 } 655 656 /* 657 * Remove all mappings if a process is swapped out, this will free page 658 * table pages. 659 */ 660 if (desired == 0 && nothingwired) { 661 pmap_remove(vm_map_pmap(map), vm_map_min(map), 662 vm_map_max(map)); 663 } 664 vm_map_unlock(map); 665} 666#endif /* !defined(NO_SWAPPING) */ 667 668/* 669 * vm_pageout_scan does the dirty work for the pageout daemon. 670 */ 671static void 672vm_pageout_scan(int pass) 673{ 674 vm_page_t m, next; 675 struct vm_page marker; 676 int page_shortage, maxscan, pcount; 677 int addl_page_shortage, addl_page_shortage_init; 678 struct proc *p, *bigproc; 679 struct thread *td; 680 vm_offset_t size, bigsize; 681 vm_object_t object; 682 int actcount, cache_cur, cache_first_failure; 683 static int cache_last_free; 684 int vnodes_skipped = 0; 685 int maxlaunder; 686 687 mtx_lock(&Giant); 688 /* 689 * Decrease registered cache sizes. 690 */ 691 EVENTHANDLER_INVOKE(vm_lowmem, 0); 692 /* 693 * We do this explicitly after the caches have been drained above. 694 */ 695 uma_reclaim(); 696 697 addl_page_shortage_init = atomic_readandclear_int(&vm_pageout_deficit); 698 699 /* 700 * Calculate the number of pages we want to either free or move 701 * to the cache. 702 */ 703 page_shortage = vm_paging_target() + addl_page_shortage_init; 704 705 /* 706 * Initialize our marker 707 */ 708 bzero(&marker, sizeof(marker)); 709 marker.flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER; 710 marker.queue = PQ_INACTIVE; 711 marker.wire_count = 1; 712 713 /* 714 * Start scanning the inactive queue for pages we can move to the 715 * cache or free. The scan will stop when the target is reached or 716 * we have scanned the entire inactive queue. Note that m->act_count 717 * is not used to form decisions for the inactive queue, only for the 718 * active queue. 719 * 720 * maxlaunder limits the number of dirty pages we flush per scan. 721 * For most systems a smaller value (16 or 32) is more robust under 722 * extreme memory and disk pressure because any unnecessary writes 723 * to disk can result in extreme performance degredation. However, 724 * systems with excessive dirty pages (especially when MAP_NOSYNC is 725 * used) will die horribly with limited laundering. If the pageout 726 * daemon cannot clean enough pages in the first pass, we let it go 727 * all out in succeeding passes. 728 */ 729 if ((maxlaunder = vm_max_launder) <= 1) 730 maxlaunder = 1; 731 if (pass) 732 maxlaunder = 10000; 733 vm_page_lock_queues(); 734rescan0: 735 addl_page_shortage = addl_page_shortage_init; 736 maxscan = cnt.v_inactive_count; 737 738 for (m = TAILQ_FIRST(&vm_page_queues[PQ_INACTIVE].pl); 739 m != NULL && maxscan-- > 0 && page_shortage > 0; 740 m = next) { 741 742 cnt.v_pdpages++; 743 744 if (VM_PAGE_GETQUEUE(m) != PQ_INACTIVE) { 745 goto rescan0; 746 } 747 748 next = TAILQ_NEXT(m, pageq); 749 object = m->object; 750 751 /* 752 * skip marker pages 753 */ 754 if (m->flags & PG_MARKER) 755 continue; 756 757 /* 758 * A held page may be undergoing I/O, so skip it. 759 */ 760 if (m->hold_count) { 761 vm_pageq_requeue(m); 762 addl_page_shortage++; 763 continue; 764 } 765 /* 766 * Don't mess with busy pages, keep in the front of the 767 * queue, most likely are being paged out. 768 */ 769 if (!VM_OBJECT_TRYLOCK(object) && 770 (!vm_pageout_fallback_object_lock(m, &next) || 771 m->hold_count != 0)) { 772 VM_OBJECT_UNLOCK(object); 773 addl_page_shortage++; 774 continue; 775 } 776 if (m->busy || (m->flags & PG_BUSY)) { 777 VM_OBJECT_UNLOCK(object); 778 addl_page_shortage++; 779 continue; 780 } 781 782 /* 783 * If the object is not being used, we ignore previous 784 * references. 785 */ 786 if (object->ref_count == 0) { 787 vm_page_flag_clear(m, PG_REFERENCED); 788 pmap_clear_reference(m); 789 790 /* 791 * Otherwise, if the page has been referenced while in the 792 * inactive queue, we bump the "activation count" upwards, 793 * making it less likely that the page will be added back to 794 * the inactive queue prematurely again. Here we check the 795 * page tables (or emulated bits, if any), given the upper 796 * level VM system not knowing anything about existing 797 * references. 798 */ 799 } else if (((m->flags & PG_REFERENCED) == 0) && 800 (actcount = pmap_ts_referenced(m))) { 801 vm_page_activate(m); 802 VM_OBJECT_UNLOCK(object); 803 m->act_count += (actcount + ACT_ADVANCE); 804 continue; 805 } 806 807 /* 808 * If the upper level VM system knows about any page 809 * references, we activate the page. We also set the 810 * "activation count" higher than normal so that we will less 811 * likely place pages back onto the inactive queue again. 812 */ 813 if ((m->flags & PG_REFERENCED) != 0) { 814 vm_page_flag_clear(m, PG_REFERENCED); 815 actcount = pmap_ts_referenced(m); 816 vm_page_activate(m); 817 VM_OBJECT_UNLOCK(object); 818 m->act_count += (actcount + ACT_ADVANCE + 1); 819 continue; 820 } 821 822 /* 823 * If the upper level VM system doesn't know anything about 824 * the page being dirty, we have to check for it again. As 825 * far as the VM code knows, any partially dirty pages are 826 * fully dirty. 827 */ 828 if (m->dirty == 0 && !pmap_is_modified(m)) { 829 /* 830 * Avoid a race condition: Unless write access is 831 * removed from the page, another processor could 832 * modify it before all access is removed by the call 833 * to vm_page_cache() below. If vm_page_cache() finds 834 * that the page has been modified when it removes all 835 * access, it panics because it cannot cache dirty 836 * pages. In principle, we could eliminate just write 837 * access here rather than all access. In the expected 838 * case, when there are no last instant modifications 839 * to the page, removing all access will be cheaper 840 * overall. 841 */ 842 if ((m->flags & PG_WRITEABLE) != 0) 843 pmap_remove_all(m); 844 } else { 845 vm_page_dirty(m); 846 } 847 848 if (m->valid == 0) { 849 /* 850 * Invalid pages can be easily freed 851 */ 852 vm_page_free(m); 853 cnt.v_dfree++; 854 --page_shortage; 855 } else if (m->dirty == 0) { 856 /* 857 * Clean pages can be placed onto the cache queue. 858 * This effectively frees them. 859 */ 860 vm_page_cache(m); 861 --page_shortage; 862 } else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) { 863 /* 864 * Dirty pages need to be paged out, but flushing 865 * a page is extremely expensive verses freeing 866 * a clean page. Rather then artificially limiting 867 * the number of pages we can flush, we instead give 868 * dirty pages extra priority on the inactive queue 869 * by forcing them to be cycled through the queue 870 * twice before being flushed, after which the 871 * (now clean) page will cycle through once more 872 * before being freed. This significantly extends 873 * the thrash point for a heavily loaded machine. 874 */ 875 vm_page_flag_set(m, PG_WINATCFLS); 876 vm_pageq_requeue(m); 877 } else if (maxlaunder > 0) { 878 /* 879 * We always want to try to flush some dirty pages if 880 * we encounter them, to keep the system stable. 881 * Normally this number is small, but under extreme 882 * pressure where there are insufficient clean pages 883 * on the inactive queue, we may have to go all out. 884 */ 885 int swap_pageouts_ok; 886 struct vnode *vp = NULL; 887 struct mount *mp; 888 889 if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) { 890 swap_pageouts_ok = 1; 891 } else { 892 swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts); 893 swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts && 894 vm_page_count_min()); 895 896 } 897 898 /* 899 * We don't bother paging objects that are "dead". 900 * Those objects are in a "rundown" state. 901 */ 902 if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) { 903 VM_OBJECT_UNLOCK(object); 904 vm_pageq_requeue(m); 905 continue; 906 } 907 908 /* 909 * The object is already known NOT to be dead. It 910 * is possible for the vget() to block the whole 911 * pageout daemon, but the new low-memory handling 912 * code should prevent it. 913 * 914 * The previous code skipped locked vnodes and, worse, 915 * reordered pages in the queue. This results in 916 * completely non-deterministic operation and, on a 917 * busy system, can lead to extremely non-optimal 918 * pageouts. For example, it can cause clean pages 919 * to be freed and dirty pages to be moved to the end 920 * of the queue. Since dirty pages are also moved to 921 * the end of the queue once-cleaned, this gives 922 * way too large a weighting to defering the freeing 923 * of dirty pages. 924 * 925 * We can't wait forever for the vnode lock, we might 926 * deadlock due to a vn_read() getting stuck in 927 * vm_wait while holding this vnode. We skip the 928 * vnode if we can't get it in a reasonable amount 929 * of time. 930 */ 931 if (object->type == OBJT_VNODE) { 932 vp = object->handle; 933 mp = NULL; 934 if (vp->v_type == VREG) 935 vn_start_write(vp, &mp, V_NOWAIT); 936 vm_page_unlock_queues(); 937 VI_LOCK(vp); 938 VM_OBJECT_UNLOCK(object); 939 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK | 940 LK_TIMELOCK, curthread)) { 941 VM_OBJECT_LOCK(object); 942 vm_page_lock_queues(); 943 ++pageout_lock_miss; 944 vn_finished_write(mp); 945 if (object->flags & OBJ_MIGHTBEDIRTY) 946 vnodes_skipped++; 947 VM_OBJECT_UNLOCK(object); 948 continue; 949 } 950 VM_OBJECT_LOCK(object); 951 vm_page_lock_queues(); 952 /* 953 * The page might have been moved to another 954 * queue during potential blocking in vget() 955 * above. The page might have been freed and 956 * reused for another vnode. The object might 957 * have been reused for another vnode. 958 */ 959 if (VM_PAGE_GETQUEUE(m) != PQ_INACTIVE || 960 m->object != object || 961 object->handle != vp) { 962 if (object->flags & OBJ_MIGHTBEDIRTY) 963 vnodes_skipped++; 964 goto unlock_and_continue; 965 } 966 967 /* 968 * The page may have been busied during the 969 * blocking in vput(); We don't move the 970 * page back onto the end of the queue so that 971 * statistics are more correct if we don't. 972 */ 973 if (m->busy || (m->flags & PG_BUSY)) { 974 goto unlock_and_continue; 975 } 976 977 /* 978 * If the page has become held it might 979 * be undergoing I/O, so skip it 980 */ 981 if (m->hold_count) { 982 vm_pageq_requeue(m); 983 if (object->flags & OBJ_MIGHTBEDIRTY) 984 vnodes_skipped++; 985 goto unlock_and_continue; 986 } 987 } 988 989 /* 990 * If a page is dirty, then it is either being washed 991 * (but not yet cleaned) or it is still in the 992 * laundry. If it is still in the laundry, then we 993 * start the cleaning operation. 994 * 995 * This operation may cluster, invalidating the 'next' 996 * pointer. To prevent an inordinate number of 997 * restarts we use our marker to remember our place. 998 * 999 * decrement page_shortage on success to account for 1000 * the (future) cleaned page. Otherwise we could wind 1001 * up laundering or cleaning too many pages. 1002 */ 1003 TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE].pl, m, &marker, pageq); 1004 if (vm_pageout_clean(m) != 0) { 1005 --page_shortage; 1006 --maxlaunder; 1007 } 1008 next = TAILQ_NEXT(&marker, pageq); 1009 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, &marker, pageq); 1010unlock_and_continue: 1011 VM_OBJECT_UNLOCK(object); 1012 if (vp) { 1013 vm_page_unlock_queues(); 1014 vput(vp); 1015 vn_finished_write(mp); 1016 vm_page_lock_queues(); 1017 } 1018 continue; 1019 } 1020 VM_OBJECT_UNLOCK(object); 1021 } 1022 1023 /* 1024 * Compute the number of pages we want to try to move from the 1025 * active queue to the inactive queue. 1026 */ 1027 page_shortage = vm_paging_target() + 1028 cnt.v_inactive_target - cnt.v_inactive_count; 1029 page_shortage += addl_page_shortage; 1030 1031 /* 1032 * Scan the active queue for things we can deactivate. We nominally 1033 * track the per-page activity counter and use it to locate 1034 * deactivation candidates. 1035 */ 1036 pcount = cnt.v_active_count; 1037 m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl); 1038 1039 while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) { 1040 1041 KASSERT(VM_PAGE_INQUEUE2(m, PQ_ACTIVE), 1042 ("vm_pageout_scan: page %p isn't active", m)); 1043 1044 next = TAILQ_NEXT(m, pageq); 1045 object = m->object; 1046 if ((m->flags & PG_MARKER) != 0) { 1047 m = next; 1048 continue; 1049 } 1050 if (!VM_OBJECT_TRYLOCK(object) && 1051 !vm_pageout_fallback_object_lock(m, &next)) { 1052 VM_OBJECT_UNLOCK(object); 1053 m = next; 1054 continue; 1055 } 1056 1057 /* 1058 * Don't deactivate pages that are busy. 1059 */ 1060 if ((m->busy != 0) || 1061 (m->flags & PG_BUSY) || 1062 (m->hold_count != 0)) { 1063 VM_OBJECT_UNLOCK(object); 1064 vm_pageq_requeue(m); 1065 m = next; 1066 continue; 1067 } 1068 1069 /* 1070 * The count for pagedaemon pages is done after checking the 1071 * page for eligibility... 1072 */ 1073 cnt.v_pdpages++; 1074 1075 /* 1076 * Check to see "how much" the page has been used. 1077 */ 1078 actcount = 0; 1079 if (object->ref_count != 0) { 1080 if (m->flags & PG_REFERENCED) { 1081 actcount += 1; 1082 } 1083 actcount += pmap_ts_referenced(m); 1084 if (actcount) { 1085 m->act_count += ACT_ADVANCE + actcount; 1086 if (m->act_count > ACT_MAX) 1087 m->act_count = ACT_MAX; 1088 } 1089 } 1090 1091 /* 1092 * Since we have "tested" this bit, we need to clear it now. 1093 */ 1094 vm_page_flag_clear(m, PG_REFERENCED); 1095 1096 /* 1097 * Only if an object is currently being used, do we use the 1098 * page activation count stats. 1099 */ 1100 if (actcount && (object->ref_count != 0)) { 1101 vm_pageq_requeue(m); 1102 } else { 1103 m->act_count -= min(m->act_count, ACT_DECLINE); 1104 if (vm_pageout_algorithm || 1105 object->ref_count == 0 || 1106 m->act_count == 0) { 1107 page_shortage--; 1108 if (object->ref_count == 0) { 1109 pmap_remove_all(m); 1110 if (m->dirty == 0) 1111 vm_page_cache(m); 1112 else 1113 vm_page_deactivate(m); 1114 } else { 1115 vm_page_deactivate(m); 1116 } 1117 } else { 1118 vm_pageq_requeue(m); 1119 } 1120 } 1121 VM_OBJECT_UNLOCK(object); 1122 m = next; 1123 } 1124 1125 /* 1126 * We try to maintain some *really* free pages, this allows interrupt 1127 * code to be guaranteed space. Since both cache and free queues 1128 * are considered basically 'free', moving pages from cache to free 1129 * does not effect other calculations. 1130 */ 1131 cache_cur = cache_last_free; 1132 cache_first_failure = -1; 1133 while (cnt.v_free_count < cnt.v_free_reserved && (cache_cur = 1134 (cache_cur + PQ_PRIME2) & PQ_COLORMASK) != cache_first_failure) { 1135 TAILQ_FOREACH(m, &vm_page_queues[PQ_CACHE + cache_cur].pl, 1136 pageq) { 1137 KASSERT(m->dirty == 0, 1138 ("Found dirty cache page %p", m)); 1139 KASSERT(!pmap_page_is_mapped(m), 1140 ("Found mapped cache page %p", m)); 1141 KASSERT((m->flags & PG_UNMANAGED) == 0, 1142 ("Found unmanaged cache page %p", m)); 1143 KASSERT(m->wire_count == 0, 1144 ("Found wired cache page %p", m)); 1145 if (m->hold_count == 0 && VM_OBJECT_TRYLOCK(object = 1146 m->object)) { 1147 KASSERT((m->flags & PG_BUSY) == 0 && 1148 m->busy == 0, ("Found busy cache page %p", 1149 m)); 1150 vm_page_free(m); 1151 VM_OBJECT_UNLOCK(object); 1152 cnt.v_dfree++; 1153 cache_last_free = cache_cur; 1154 cache_first_failure = -1; 1155 break; 1156 } 1157 } 1158 if (m == NULL && cache_first_failure == -1) 1159 cache_first_failure = cache_cur; 1160 } 1161 vm_page_unlock_queues(); 1162#if !defined(NO_SWAPPING) 1163 /* 1164 * Idle process swapout -- run once per second. 1165 */ 1166 if (vm_swap_idle_enabled) { 1167 static long lsec; 1168 if (time_second != lsec) { 1169 vm_pageout_req_swapout |= VM_SWAP_IDLE; 1170 vm_req_vmdaemon(); 1171 lsec = time_second; 1172 } 1173 } 1174#endif 1175 1176 /* 1177 * If we didn't get enough free pages, and we have skipped a vnode 1178 * in a writeable object, wakeup the sync daemon. And kick swapout 1179 * if we did not get enough free pages. 1180 */ 1181 if (vm_paging_target() > 0) { 1182 if (vnodes_skipped && vm_page_count_min()) 1183 (void) speedup_syncer(); 1184#if !defined(NO_SWAPPING) 1185 if (vm_swap_enabled && vm_page_count_target()) { 1186 vm_req_vmdaemon(); 1187 vm_pageout_req_swapout |= VM_SWAP_NORMAL; 1188 } 1189#endif 1190 } 1191 1192 /* 1193 * If we are critically low on one of RAM or swap and low on 1194 * the other, kill the largest process. However, we avoid 1195 * doing this on the first pass in order to give ourselves a 1196 * chance to flush out dirty vnode-backed pages and to allow 1197 * active pages to be moved to the inactive queue and reclaimed. 1198 * 1199 * We keep the process bigproc locked once we find it to keep anyone 1200 * from messing with it; however, there is a possibility of 1201 * deadlock if process B is bigproc and one of it's child processes 1202 * attempts to propagate a signal to B while we are waiting for A's 1203 * lock while walking this list. To avoid this, we don't block on 1204 * the process lock but just skip a process if it is already locked. 1205 */ 1206 if (pass != 0 && 1207 ((swap_pager_avail < 64 && vm_page_count_min()) || 1208 (swap_pager_full && vm_paging_target() > 0))) { 1209 bigproc = NULL; 1210 bigsize = 0; 1211 sx_slock(&allproc_lock); 1212 FOREACH_PROC_IN_SYSTEM(p) { 1213 int breakout; 1214 1215 if (PROC_TRYLOCK(p) == 0) 1216 continue; 1217 /* 1218 * If this is a system or protected process, skip it. 1219 */ 1220 if ((p->p_flag & P_SYSTEM) || (p->p_pid == 1) || 1221 (p->p_flag & P_PROTECTED) || 1222 ((p->p_pid < 48) && (swap_pager_avail != 0))) { 1223 PROC_UNLOCK(p); 1224 continue; 1225 } 1226 /* 1227 * If the process is in a non-running type state, 1228 * don't touch it. Check all the threads individually. 1229 */ 1230 mtx_lock_spin(&sched_lock); 1231 breakout = 0; 1232 FOREACH_THREAD_IN_PROC(p, td) { 1233 if (!TD_ON_RUNQ(td) && 1234 !TD_IS_RUNNING(td) && 1235 !TD_IS_SLEEPING(td)) { 1236 breakout = 1; 1237 break; 1238 } 1239 } 1240 if (breakout) { 1241 mtx_unlock_spin(&sched_lock); 1242 PROC_UNLOCK(p); 1243 continue; 1244 } 1245 mtx_unlock_spin(&sched_lock); 1246 /* 1247 * get the process size 1248 */ 1249 if (!vm_map_trylock_read(&p->p_vmspace->vm_map)) { 1250 PROC_UNLOCK(p); 1251 continue; 1252 } 1253 size = vmspace_swap_count(p->p_vmspace); 1254 vm_map_unlock_read(&p->p_vmspace->vm_map); 1255 size += vmspace_resident_count(p->p_vmspace); 1256 /* 1257 * if the this process is bigger than the biggest one 1258 * remember it. 1259 */ 1260 if (size > bigsize) { 1261 if (bigproc != NULL) 1262 PROC_UNLOCK(bigproc); 1263 bigproc = p; 1264 bigsize = size; 1265 } else 1266 PROC_UNLOCK(p); 1267 } 1268 sx_sunlock(&allproc_lock); 1269 if (bigproc != NULL) { 1270 killproc(bigproc, "out of swap space"); 1271 mtx_lock_spin(&sched_lock); 1272 sched_nice(bigproc, PRIO_MIN); 1273 mtx_unlock_spin(&sched_lock); 1274 PROC_UNLOCK(bigproc); 1275 wakeup(&cnt.v_free_count); 1276 } 1277 } 1278 mtx_unlock(&Giant); 1279} 1280 1281/* 1282 * This routine tries to maintain the pseudo LRU active queue, 1283 * so that during long periods of time where there is no paging, 1284 * that some statistic accumulation still occurs. This code 1285 * helps the situation where paging just starts to occur. 1286 */ 1287static void 1288vm_pageout_page_stats() 1289{ 1290 vm_object_t object; 1291 vm_page_t m,next; 1292 int pcount,tpcount; /* Number of pages to check */ 1293 static int fullintervalcount = 0; 1294 int page_shortage; 1295 1296 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1297 page_shortage = 1298 (cnt.v_inactive_target + cnt.v_cache_max + cnt.v_free_min) - 1299 (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count); 1300 1301 if (page_shortage <= 0) 1302 return; 1303 1304 pcount = cnt.v_active_count; 1305 fullintervalcount += vm_pageout_stats_interval; 1306 if (fullintervalcount < vm_pageout_full_stats_interval) { 1307 tpcount = (vm_pageout_stats_max * cnt.v_active_count) / cnt.v_page_count; 1308 if (pcount > tpcount) 1309 pcount = tpcount; 1310 } else { 1311 fullintervalcount = 0; 1312 } 1313 1314 m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl); 1315 while ((m != NULL) && (pcount-- > 0)) { 1316 int actcount; 1317 1318 KASSERT(VM_PAGE_INQUEUE2(m, PQ_ACTIVE), 1319 ("vm_pageout_page_stats: page %p isn't active", m)); 1320 1321 next = TAILQ_NEXT(m, pageq); 1322 object = m->object; 1323 1324 if ((m->flags & PG_MARKER) != 0) { 1325 m = next; 1326 continue; 1327 } 1328 if (!VM_OBJECT_TRYLOCK(object) && 1329 !vm_pageout_fallback_object_lock(m, &next)) { 1330 VM_OBJECT_UNLOCK(object); 1331 m = next; 1332 continue; 1333 } 1334 1335 /* 1336 * Don't deactivate pages that are busy. 1337 */ 1338 if ((m->busy != 0) || 1339 (m->flags & PG_BUSY) || 1340 (m->hold_count != 0)) { 1341 VM_OBJECT_UNLOCK(object); 1342 vm_pageq_requeue(m); 1343 m = next; 1344 continue; 1345 } 1346 1347 actcount = 0; 1348 if (m->flags & PG_REFERENCED) { 1349 vm_page_flag_clear(m, PG_REFERENCED); 1350 actcount += 1; 1351 } 1352 1353 actcount += pmap_ts_referenced(m); 1354 if (actcount) { 1355 m->act_count += ACT_ADVANCE + actcount; 1356 if (m->act_count > ACT_MAX) 1357 m->act_count = ACT_MAX; 1358 vm_pageq_requeue(m); 1359 } else { 1360 if (m->act_count == 0) { 1361 /* 1362 * We turn off page access, so that we have 1363 * more accurate RSS stats. We don't do this 1364 * in the normal page deactivation when the 1365 * system is loaded VM wise, because the 1366 * cost of the large number of page protect 1367 * operations would be higher than the value 1368 * of doing the operation. 1369 */ 1370 pmap_remove_all(m); 1371 vm_page_deactivate(m); 1372 } else { 1373 m->act_count -= min(m->act_count, ACT_DECLINE); 1374 vm_pageq_requeue(m); 1375 } 1376 } 1377 VM_OBJECT_UNLOCK(object); 1378 m = next; 1379 } 1380} 1381 1382/* 1383 * vm_pageout is the high level pageout daemon. 1384 */ 1385static void 1386vm_pageout() 1387{ 1388 int error, pass; 1389 1390 /* 1391 * Initialize some paging parameters. 1392 */ 1393 cnt.v_interrupt_free_min = 2; 1394 if (cnt.v_page_count < 2000) 1395 vm_pageout_page_count = 8; 1396 1397 /* 1398 * v_free_reserved needs to include enough for the largest 1399 * swap pager structures plus enough for any pv_entry structs 1400 * when paging. 1401 */ 1402 if (cnt.v_page_count > 1024) 1403 cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200; 1404 else 1405 cnt.v_free_min = 4; 1406 cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE + 1407 cnt.v_interrupt_free_min; 1408 cnt.v_free_reserved = vm_pageout_page_count + 1409 cnt.v_pageout_free_min + (cnt.v_page_count / 768) + PQ_NUMCOLORS; 1410 cnt.v_free_severe = cnt.v_free_min / 2; 1411 cnt.v_free_min += cnt.v_free_reserved; 1412 cnt.v_free_severe += cnt.v_free_reserved; 1413 1414 /* 1415 * v_free_target and v_cache_min control pageout hysteresis. Note 1416 * that these are more a measure of the VM cache queue hysteresis 1417 * then the VM free queue. Specifically, v_free_target is the 1418 * high water mark (free+cache pages). 1419 * 1420 * v_free_reserved + v_cache_min (mostly means v_cache_min) is the 1421 * low water mark, while v_free_min is the stop. v_cache_min must 1422 * be big enough to handle memory needs while the pageout daemon 1423 * is signalled and run to free more pages. 1424 */ 1425 if (cnt.v_free_count > 6144) 1426 cnt.v_free_target = 4 * cnt.v_free_min + cnt.v_free_reserved; 1427 else 1428 cnt.v_free_target = 2 * cnt.v_free_min + cnt.v_free_reserved; 1429 1430 if (cnt.v_free_count > 2048) { 1431 cnt.v_cache_min = cnt.v_free_target; 1432 cnt.v_cache_max = 2 * cnt.v_cache_min; 1433 cnt.v_inactive_target = (3 * cnt.v_free_target) / 2; 1434 } else { 1435 cnt.v_cache_min = 0; 1436 cnt.v_cache_max = 0; 1437 cnt.v_inactive_target = cnt.v_free_count / 4; 1438 } 1439 if (cnt.v_inactive_target > cnt.v_free_count / 3) 1440 cnt.v_inactive_target = cnt.v_free_count / 3; 1441 1442 /* XXX does not really belong here */ 1443 if (vm_page_max_wired == 0) 1444 vm_page_max_wired = cnt.v_free_count / 3; 1445 1446 if (vm_pageout_stats_max == 0) 1447 vm_pageout_stats_max = cnt.v_free_target; 1448 1449 /* 1450 * Set interval in seconds for stats scan. 1451 */ 1452 if (vm_pageout_stats_interval == 0) 1453 vm_pageout_stats_interval = 5; 1454 if (vm_pageout_full_stats_interval == 0) 1455 vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4; 1456 1457 swap_pager_swap_init(); 1458 pass = 0; 1459 /* 1460 * The pageout daemon is never done, so loop forever. 1461 */ 1462 while (TRUE) { 1463 vm_page_lock_queues(); 1464 /* 1465 * If we have enough free memory, wakeup waiters. Do 1466 * not clear vm_pages_needed until we reach our target, 1467 * otherwise we may be woken up over and over again and 1468 * waste a lot of cpu. 1469 */ 1470 if (vm_pages_needed && !vm_page_count_min()) { 1471 if (!vm_paging_needed()) 1472 vm_pages_needed = 0; 1473 wakeup(&cnt.v_free_count); 1474 } 1475 if (vm_pages_needed) { 1476 /* 1477 * Still not done, take a second pass without waiting 1478 * (unlimited dirty cleaning), otherwise sleep a bit 1479 * and try again. 1480 */ 1481 ++pass; 1482 if (pass > 1) 1483 msleep(&vm_pages_needed, &vm_page_queue_mtx, PVM, 1484 "psleep", hz/2); 1485 } else { 1486 /* 1487 * Good enough, sleep & handle stats. Prime the pass 1488 * for the next run. 1489 */ 1490 if (pass > 1) 1491 pass = 1; 1492 else 1493 pass = 0; 1494 error = msleep(&vm_pages_needed, &vm_page_queue_mtx, PVM, 1495 "psleep", vm_pageout_stats_interval * hz); 1496 if (error && !vm_pages_needed) { 1497 pass = 0; 1498 vm_pageout_page_stats(); 1499 vm_page_unlock_queues(); 1500 continue; 1501 } 1502 } 1503 if (vm_pages_needed) 1504 cnt.v_pdwakeups++; 1505 vm_page_unlock_queues(); 1506 vm_pageout_scan(pass); 1507 } 1508} 1509 1510/* 1511 * Unless the page queue lock is held by the caller, this function 1512 * should be regarded as advisory. Specifically, the caller should 1513 * not msleep() on &cnt.v_free_count following this function unless 1514 * the page queue lock is held until the msleep() is performed. 1515 */ 1516void 1517pagedaemon_wakeup() 1518{ 1519 1520 if (!vm_pages_needed && curthread->td_proc != pageproc) { 1521 vm_pages_needed = 1; 1522 wakeup(&vm_pages_needed); 1523 } 1524} 1525 1526#if !defined(NO_SWAPPING) 1527static void 1528vm_req_vmdaemon() 1529{ 1530 static int lastrun = 0; 1531 1532 if ((ticks > (lastrun + hz)) || (ticks < lastrun)) { 1533 wakeup(&vm_daemon_needed); 1534 lastrun = ticks; 1535 } 1536} 1537 1538static void 1539vm_daemon() 1540{ 1541 struct rlimit rsslim; 1542 struct proc *p; 1543 struct thread *td; 1544 int breakout; 1545 1546 mtx_lock(&Giant); 1547 while (TRUE) { 1548 tsleep(&vm_daemon_needed, PPAUSE, "psleep", 0); 1549 if (vm_pageout_req_swapout) { 1550 swapout_procs(vm_pageout_req_swapout); 1551 vm_pageout_req_swapout = 0; 1552 } 1553 /* 1554 * scan the processes for exceeding their rlimits or if 1555 * process is swapped out -- deactivate pages 1556 */ 1557 sx_slock(&allproc_lock); 1558 LIST_FOREACH(p, &allproc, p_list) { 1559 vm_pindex_t limit, size; 1560 1561 /* 1562 * if this is a system process or if we have already 1563 * looked at this process, skip it. 1564 */ 1565 PROC_LOCK(p); 1566 if (p->p_flag & (P_SYSTEM | P_WEXIT)) { 1567 PROC_UNLOCK(p); 1568 continue; 1569 } 1570 /* 1571 * if the process is in a non-running type state, 1572 * don't touch it. 1573 */ 1574 mtx_lock_spin(&sched_lock); 1575 breakout = 0; 1576 FOREACH_THREAD_IN_PROC(p, td) { 1577 if (!TD_ON_RUNQ(td) && 1578 !TD_IS_RUNNING(td) && 1579 !TD_IS_SLEEPING(td)) { 1580 breakout = 1; 1581 break; 1582 } 1583 } 1584 mtx_unlock_spin(&sched_lock); 1585 if (breakout) { 1586 PROC_UNLOCK(p); 1587 continue; 1588 } 1589 /* 1590 * get a limit 1591 */ 1592 lim_rlimit(p, RLIMIT_RSS, &rsslim); 1593 limit = OFF_TO_IDX( 1594 qmin(rsslim.rlim_cur, rsslim.rlim_max)); 1595 1596 /* 1597 * let processes that are swapped out really be 1598 * swapped out set the limit to nothing (will force a 1599 * swap-out.) 1600 */ 1601 if ((p->p_sflag & PS_INMEM) == 0) 1602 limit = 0; /* XXX */ 1603 PROC_UNLOCK(p); 1604 1605 size = vmspace_resident_count(p->p_vmspace); 1606 if (limit >= 0 && size >= limit) { 1607 vm_pageout_map_deactivate_pages( 1608 &p->p_vmspace->vm_map, limit); 1609 } 1610 } 1611 sx_sunlock(&allproc_lock); 1612 } 1613} 1614#endif /* !defined(NO_SWAPPING) */ 1615