vm_pageout.c revision 6618
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 * 9 * This code is derived from software contributed to Berkeley by 10 * The Mach Operating System project at Carnegie-Mellon University. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the University of 23 * California, Berkeley and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * from: @(#)vm_pageout.c 7.4 (Berkeley) 5/7/91 41 * 42 * 43 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 44 * All rights reserved. 45 * 46 * Authors: Avadis Tevanian, Jr., Michael Wayne Young 47 * 48 * Permission to use, copy, modify and distribute this software and 49 * its documentation is hereby granted, provided that both the copyright 50 * notice and this permission notice appear in all copies of the 51 * software, derivative works or modified versions, and any portions 52 * thereof, and that both notices appear in supporting documentation. 53 * 54 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 55 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 56 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 57 * 58 * Carnegie Mellon requests users of this software to return to 59 * 60 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 61 * School of Computer Science 62 * Carnegie Mellon University 63 * Pittsburgh PA 15213-3890 64 * 65 * any improvements or extensions that they make and grant Carnegie the 66 * rights to redistribute these changes. 67 * 68 * $Id: vm_pageout.c,v 1.36 1995/02/20 23:35:45 davidg Exp $ 69 */ 70 71/* 72 * The proverbial page-out daemon. 73 */ 74 75#include <sys/param.h> 76#include <sys/systm.h> 77#include <sys/proc.h> 78#include <sys/resourcevar.h> 79#include <sys/malloc.h> 80#include <sys/kernel.h> 81#include <sys/signalvar.h> 82 83#include <vm/vm.h> 84#include <vm/vm_page.h> 85#include <vm/vm_pageout.h> 86#include <vm/swap_pager.h> 87 88extern vm_map_t kmem_map; 89int vm_pages_needed; /* Event on which pageout daemon sleeps */ 90int vm_pagescanner; /* Event on which pagescanner sleeps */ 91 92int vm_pageout_pages_needed = 0;/* flag saying that the pageout daemon needs pages */ 93int vm_page_pagesfreed; 94 95extern int npendingio; 96int vm_pageout_proc_limit; 97int vm_pageout_req_swapout; 98int vm_daemon_needed; 99extern int nswiodone; 100extern int swap_pager_full; 101extern int vm_swap_size; 102extern int swap_pager_ready(); 103 104#define MAXSCAN 1024 /* maximum number of pages to scan in queues */ 105 106#define MAXLAUNDER (cnt.v_page_count > 1800 ? 32 : 16) 107 108#define VM_PAGEOUT_PAGE_COUNT 8 109int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT; 110int vm_pageout_req_do_stats; 111 112int vm_page_max_wired = 0; /* XXX max # of wired pages system-wide */ 113 114/* 115 * vm_pageout_clean: 116 * cleans a vm_page 117 */ 118int 119vm_pageout_clean(m, sync) 120 register vm_page_t m; 121 int sync; 122{ 123 /* 124 * Clean the page and remove it from the laundry. 125 * 126 * We set the busy bit to cause potential page faults on this page to 127 * block. 128 * 129 * And we set pageout-in-progress to keep the object from disappearing 130 * during pageout. This guarantees that the page won't move from the 131 * inactive queue. (However, any other page on the inactive queue may 132 * move!) 133 */ 134 135 register vm_object_t object; 136 register vm_pager_t pager; 137 int pageout_status[VM_PAGEOUT_PAGE_COUNT]; 138 vm_page_t ms[VM_PAGEOUT_PAGE_COUNT]; 139 int pageout_count; 140 int anyok = 0; 141 int i; 142 vm_offset_t offset = m->offset; 143 144 object = m->object; 145 if (!object) { 146 printf("pager: object missing\n"); 147 return 0; 148 } 149 if (!object->pager && (object->flags & OBJ_INTERNAL) == 0) { 150 printf("pager: non internal obj without pager\n"); 151 } 152 /* 153 * Try to collapse the object before making a pager for it. We must 154 * unlock the page queues first. We try to defer the creation of a 155 * pager until all shadows are not paging. This allows 156 * vm_object_collapse to work better and helps control swap space 157 * size. (J. Dyson 11 Nov 93) 158 */ 159 160 if (!object->pager && 161 (cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min) 162 return 0; 163 164 if ((!sync && m->bmapped != 0 && m->hold_count != 0) || 165 ((m->busy != 0) || (m->flags & PG_BUSY))) 166 return 0; 167 168 if (!sync && object->shadow) { 169 vm_object_collapse(object); 170 } 171 pageout_count = 1; 172 ms[0] = m; 173 174 pager = object->pager; 175 if (pager) { 176 for (i = 1; i < vm_pageout_page_count; i++) { 177 vm_page_t mt; 178 179 ms[i] = mt = vm_page_lookup(object, offset + i * NBPG); 180 if (mt) { 181 vm_page_test_dirty(mt); 182 /* 183 * we can cluster ONLY if: ->> the page is NOT 184 * busy, and is NOT clean the page is not 185 * wired, busy, held, or mapped into a buffer. 186 * and one of the following: 1) The page is 187 * inactive, or a seldom used active page. 2) 188 * or we force the issue. 189 */ 190 if ((mt->dirty & mt->valid) != 0 191 && (((mt->flags & (PG_BUSY | PG_INACTIVE)) == PG_INACTIVE) 192 || sync == VM_PAGEOUT_FORCE) 193 && (mt->wire_count == 0) 194 && (mt->busy == 0) 195 && (mt->hold_count == 0) 196 && (mt->bmapped == 0)) 197 pageout_count++; 198 else 199 break; 200 } else 201 break; 202 } 203 /* 204 * we allow reads during pageouts... 205 */ 206 for (i = 0; i < pageout_count; i++) { 207 ms[i]->flags |= PG_BUSY; 208 pmap_page_protect(VM_PAGE_TO_PHYS(ms[i]), VM_PROT_READ); 209 } 210 object->paging_in_progress += pageout_count; 211 } else { 212 213 m->flags |= PG_BUSY; 214 215 pmap_page_protect(VM_PAGE_TO_PHYS(m), VM_PROT_READ); 216 217 object->paging_in_progress++; 218 219 pager = vm_pager_allocate(PG_DFLT, (caddr_t) 0, 220 object->size, VM_PROT_ALL, 0); 221 if (pager != NULL) { 222 vm_object_setpager(object, pager, 0, FALSE); 223 } 224 } 225 226 /* 227 * If there is no pager for the page, use the default pager. If 228 * there's no place to put the page at the moment, leave it in the 229 * laundry and hope that there will be paging space later. 230 */ 231 232 if ((pager && pager->pg_type == PG_SWAP) || 233 (cnt.v_free_count + cnt.v_cache_count) >= cnt.v_pageout_free_min) { 234 if (pageout_count == 1) { 235 pageout_status[0] = pager ? 236 vm_pager_put(pager, m, 237 ((sync || (object == kernel_object)) ? TRUE : FALSE)) : 238 VM_PAGER_FAIL; 239 } else { 240 if (!pager) { 241 for (i = 0; i < pageout_count; i++) 242 pageout_status[i] = VM_PAGER_FAIL; 243 } else { 244 vm_pager_put_pages(pager, ms, pageout_count, 245 ((sync || (object == kernel_object)) ? TRUE : FALSE), 246 pageout_status); 247 } 248 } 249 } else { 250 for (i = 0; i < pageout_count; i++) 251 pageout_status[i] = VM_PAGER_FAIL; 252 } 253 254 for (i = 0; i < pageout_count; i++) { 255 switch (pageout_status[i]) { 256 case VM_PAGER_OK: 257 ++anyok; 258 break; 259 case VM_PAGER_PEND: 260 ++anyok; 261 break; 262 case VM_PAGER_BAD: 263 /* 264 * Page outside of range of object. Right now we 265 * essentially lose the changes by pretending it 266 * worked. 267 */ 268 pmap_clear_modify(VM_PAGE_TO_PHYS(ms[i])); 269 ms[i]->dirty = 0; 270 break; 271 case VM_PAGER_ERROR: 272 case VM_PAGER_FAIL: 273 /* 274 * If page couldn't be paged out, then reactivate the 275 * page so it doesn't clog the inactive list. (We 276 * will try paging out it again later). 277 */ 278 if (ms[i]->flags & PG_INACTIVE) 279 vm_page_activate(ms[i]); 280 break; 281 case VM_PAGER_AGAIN: 282 break; 283 } 284 285 286 /* 287 * If the operation is still going, leave the page busy to 288 * block all other accesses. Also, leave the paging in 289 * progress indicator set so that we don't attempt an object 290 * collapse. 291 */ 292 if (pageout_status[i] != VM_PAGER_PEND) { 293 if ((--object->paging_in_progress == 0) && 294 (object->flags & OBJ_PIPWNT)) { 295 object->flags &= ~OBJ_PIPWNT; 296 wakeup((caddr_t) object); 297 } 298 if ((ms[i]->flags & (PG_REFERENCED|PG_WANTED)) || 299 pmap_is_referenced(VM_PAGE_TO_PHYS(ms[i]))) { 300 pmap_clear_reference(VM_PAGE_TO_PHYS(ms[i])); 301 ms[i]->flags &= ~PG_REFERENCED; 302 if (ms[i]->flags & PG_INACTIVE) 303 vm_page_activate(ms[i]); 304 } 305 PAGE_WAKEUP(ms[i]); 306 } 307 } 308 return anyok; 309} 310 311/* 312 * vm_pageout_object_deactivate_pages 313 * 314 * deactivate enough pages to satisfy the inactive target 315 * requirements or if vm_page_proc_limit is set, then 316 * deactivate all of the pages in the object and its 317 * shadows. 318 * 319 * The object and map must be locked. 320 */ 321int 322vm_pageout_object_deactivate_pages(map, object, count, map_remove_only) 323 vm_map_t map; 324 vm_object_t object; 325 int count; 326 int map_remove_only; 327{ 328 register vm_page_t p, next; 329 int rcount; 330 int dcount; 331 332 dcount = 0; 333 if (count == 0) 334 count = 1; 335 336 if (object->pager && (object->pager->pg_type == PG_DEVICE)) 337 return 0; 338 339 if (object->shadow) { 340 if (object->shadow->ref_count == 1) 341 dcount += vm_pageout_object_deactivate_pages(map, object->shadow, count / 2 + 1, map_remove_only); 342 else 343 vm_pageout_object_deactivate_pages(map, object->shadow, count, 1); 344 } 345 if (object->paging_in_progress || !vm_object_lock_try(object)) 346 return dcount; 347 348 /* 349 * scan the objects entire memory queue 350 */ 351 rcount = object->resident_page_count; 352 p = object->memq.tqh_first; 353 while (p && (rcount-- > 0)) { 354 next = p->listq.tqe_next; 355 cnt.v_pdpages++; 356 vm_page_lock_queues(); 357 if (p->wire_count != 0 || 358 p->hold_count != 0 || 359 p->bmapped != 0 || 360 p->busy != 0 || 361 !pmap_page_exists(vm_map_pmap(map), VM_PAGE_TO_PHYS(p))) { 362 p = next; 363 continue; 364 } 365 /* 366 * if a page is active, not wired and is in the processes 367 * pmap, then deactivate the page. 368 */ 369 if ((p->flags & (PG_ACTIVE | PG_BUSY)) == PG_ACTIVE) { 370 if (!pmap_is_referenced(VM_PAGE_TO_PHYS(p)) && 371 (p->flags & (PG_REFERENCED|PG_WANTED)) == 0) { 372 p->act_count -= min(p->act_count, ACT_DECLINE); 373 /* 374 * if the page act_count is zero -- then we 375 * deactivate 376 */ 377 if (!p->act_count) { 378 if (!map_remove_only) 379 vm_page_deactivate(p); 380 pmap_page_protect(VM_PAGE_TO_PHYS(p), 381 VM_PROT_NONE); 382 /* 383 * else if on the next go-around we 384 * will deactivate the page we need to 385 * place the page on the end of the 386 * queue to age the other pages in 387 * memory. 388 */ 389 } else { 390 TAILQ_REMOVE(&vm_page_queue_active, p, pageq); 391 TAILQ_INSERT_TAIL(&vm_page_queue_active, p, pageq); 392 TAILQ_REMOVE(&object->memq, p, listq); 393 TAILQ_INSERT_TAIL(&object->memq, p, listq); 394 } 395 /* 396 * see if we are done yet 397 */ 398 if (p->flags & PG_INACTIVE) { 399 --count; 400 ++dcount; 401 if (count <= 0 && 402 cnt.v_inactive_count > cnt.v_inactive_target) { 403 vm_page_unlock_queues(); 404 vm_object_unlock(object); 405 return dcount; 406 } 407 } 408 } else { 409 /* 410 * Move the page to the bottom of the queue. 411 */ 412 pmap_clear_reference(VM_PAGE_TO_PHYS(p)); 413 p->flags &= ~PG_REFERENCED; 414 if (p->act_count < ACT_MAX) 415 p->act_count += ACT_ADVANCE; 416 417 TAILQ_REMOVE(&vm_page_queue_active, p, pageq); 418 TAILQ_INSERT_TAIL(&vm_page_queue_active, p, pageq); 419 TAILQ_REMOVE(&object->memq, p, listq); 420 TAILQ_INSERT_TAIL(&object->memq, p, listq); 421 } 422 } else if ((p->flags & (PG_INACTIVE | PG_BUSY)) == PG_INACTIVE) { 423 pmap_page_protect(VM_PAGE_TO_PHYS(p), 424 VM_PROT_NONE); 425 } 426 vm_page_unlock_queues(); 427 p = next; 428 } 429 vm_object_unlock(object); 430 return dcount; 431} 432 433 434/* 435 * deactivate some number of pages in a map, try to do it fairly, but 436 * that is really hard to do. 437 */ 438 439void 440vm_pageout_map_deactivate_pages(map, entry, count, freeer) 441 vm_map_t map; 442 vm_map_entry_t entry; 443 int *count; 444 int (*freeer) (vm_map_t, vm_object_t, int); 445{ 446 vm_map_t tmpm; 447 vm_map_entry_t tmpe; 448 vm_object_t obj; 449 450 if (*count <= 0) 451 return; 452 vm_map_reference(map); 453 if (!lock_try_read(&map->lock)) { 454 vm_map_deallocate(map); 455 return; 456 } 457 if (entry == 0) { 458 tmpe = map->header.next; 459 while (tmpe != &map->header && *count > 0) { 460 vm_pageout_map_deactivate_pages(map, tmpe, count, freeer, 0); 461 tmpe = tmpe->next; 462 }; 463 } else if (entry->is_sub_map || entry->is_a_map) { 464 tmpm = entry->object.share_map; 465 tmpe = tmpm->header.next; 466 while (tmpe != &tmpm->header && *count > 0) { 467 vm_pageout_map_deactivate_pages(tmpm, tmpe, count, freeer, 0); 468 tmpe = tmpe->next; 469 }; 470 } else if ((obj = entry->object.vm_object) != 0) { 471 *count -= (*freeer) (map, obj, *count); 472 } 473 lock_read_done(&map->lock); 474 vm_map_deallocate(map); 475 return; 476} 477 478void 479vm_req_vmdaemon() 480{ 481 extern int ticks; 482 static int lastrun = 0; 483 484 if ((ticks > (lastrun + hz / 10)) || (ticks < lastrun)) { 485 wakeup((caddr_t) &vm_daemon_needed); 486 lastrun = ticks; 487 } 488} 489 490/* 491 * vm_pageout_scan does the dirty work for the pageout daemon. 492 */ 493int 494vm_pageout_scan() 495{ 496 vm_page_t m; 497 int page_shortage, maxscan, maxlaunder; 498 int pages_freed; 499 int desired_free; 500 vm_page_t next; 501 struct proc *p, *bigproc; 502 vm_offset_t size, bigsize; 503 vm_object_t object; 504 int force_wakeup = 0; 505 506 /* calculate the total cached size */ 507 508 if ((cnt.v_inactive_count + cnt.v_free_count + cnt.v_cache_count) < 509 (cnt.v_inactive_target + cnt.v_free_min)) { 510 vm_req_vmdaemon(); 511 } 512 /* 513 * now swap processes out if we are in low memory conditions 514 */ 515 if ((cnt.v_free_count <= cnt.v_free_min) && 516 !swap_pager_full && vm_swap_size && vm_pageout_req_swapout == 0) { 517 vm_pageout_req_swapout = 1; 518 vm_req_vmdaemon(); 519 } 520 pages_freed = 0; 521 desired_free = cnt.v_free_target; 522 523 /* 524 * Start scanning the inactive queue for pages we can free. We keep 525 * scanning until we have enough free pages or we have scanned through 526 * the entire queue. If we encounter dirty pages, we start cleaning 527 * them. 528 */ 529 530 maxlaunder = (cnt.v_inactive_target > MAXLAUNDER) ? 531 MAXLAUNDER : cnt.v_inactive_target; 532 533rescan1: 534 maxscan = min(cnt.v_inactive_count, MAXSCAN); 535 m = vm_page_queue_inactive.tqh_first; 536 while (m && (maxscan-- > 0) && 537 ((cnt.v_free_count + cnt.v_cache_count) < desired_free)) { 538 vm_page_t next; 539 540 cnt.v_pdpages++; 541 next = m->pageq.tqe_next; 542 543#if defined(VM_DIAGNOSE) 544 if ((m->flags & PG_INACTIVE) == 0) { 545 printf("vm_pageout_scan: page not inactive?\n"); 546 break; 547 } 548#endif 549 550 /* 551 * dont mess with busy pages 552 */ 553 if (m->hold_count || m->busy || (m->flags & PG_BUSY) || 554 m->bmapped != 0) { 555 TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq); 556 TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq); 557 m = next; 558 continue; 559 } 560 if (((m->flags & PG_REFERENCED) == 0) && 561 pmap_is_referenced(VM_PAGE_TO_PHYS(m))) { 562 m->flags |= PG_REFERENCED; 563 } 564 if (m->object->ref_count == 0) { 565 m->flags &= ~PG_REFERENCED; 566 pmap_clear_reference(VM_PAGE_TO_PHYS(m)); 567 } 568 if ((m->flags & (PG_REFERENCED|PG_WANTED)) != 0) { 569 m->flags &= ~PG_REFERENCED; 570 pmap_clear_reference(VM_PAGE_TO_PHYS(m)); 571 vm_page_activate(m); 572 if (m->act_count < ACT_MAX) 573 m->act_count += ACT_ADVANCE; 574 m = next; 575 continue; 576 } 577 vm_page_test_dirty(m); 578 579 if ((m->dirty & m->valid) == 0) { 580 if (m->valid == 0) { 581 pmap_page_protect(VM_PAGE_TO_PHYS(m), VM_PROT_NONE); 582 vm_page_free(m); 583 } else if (((cnt.v_free_count + cnt.v_cache_count) < desired_free) || 584 (cnt.v_cache_count < cnt.v_cache_min)) { 585 vm_page_cache(m); 586 } 587 } else if (maxlaunder > 0) { 588 int written; 589 590 object = m->object; 591 if ((object->flags & OBJ_DEAD) || !vm_object_lock_try(object)) { 592 m = next; 593 continue; 594 } 595 /* 596 * If a page is dirty, then it is either being washed 597 * (but not yet cleaned) or it is still in the 598 * laundry. If it is still in the laundry, then we 599 * start the cleaning operation. 600 */ 601 written = vm_pageout_clean(m, 0); 602 vm_object_unlock(object); 603 604 if (!next) { 605 break; 606 } 607 maxlaunder -= written; 608 /* 609 * if the next page has been re-activated, start 610 * scanning again 611 */ 612 if ((next->flags & PG_INACTIVE) == 0) { 613 goto rescan1; 614 } 615 } 616 m = next; 617 } 618 619 /* 620 * Compute the page shortage. If we are still very low on memory be 621 * sure that we will move a minimal amount of pages from active to 622 * inactive. 623 */ 624 625 page_shortage = cnt.v_inactive_target - 626 (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count); 627 if (page_shortage <= 0) { 628 if (pages_freed == 0) { 629 if ((cnt.v_free_count + cnt.v_cache_count) < desired_free) { 630 page_shortage = 631 desired_free - (cnt.v_free_count + cnt.v_cache_count); 632 } 633 } 634 if( (page_shortage <= 0) && (cnt.v_free_count < cnt.v_free_min)) 635 page_shortage = 1; 636 } 637 maxscan = min(cnt.v_active_count, MAXSCAN); 638 m = vm_page_queue_active.tqh_first; 639 while (m && (maxscan-- > 0) && (page_shortage > 0)) { 640 641 cnt.v_pdpages++; 642 next = m->pageq.tqe_next; 643 644 /* 645 * Don't deactivate pages that are busy. 646 */ 647 if ((m->busy != 0) || 648 (m->flags & PG_BUSY) || 649 (m->hold_count != 0) || 650 (m->bmapped != 0)) { 651 TAILQ_REMOVE(&vm_page_queue_active, m, pageq); 652 TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq); 653 m = next; 654 continue; 655 } 656 if (m->object->ref_count && ((m->flags & (PG_REFERENCED|PG_WANTED)) || 657 pmap_is_referenced(VM_PAGE_TO_PHYS(m)))) { 658 int s; 659 660 pmap_clear_reference(VM_PAGE_TO_PHYS(m)); 661 m->flags &= ~PG_REFERENCED; 662 if (m->act_count < ACT_MAX) { 663 m->act_count += ACT_ADVANCE; 664 } 665 TAILQ_REMOVE(&vm_page_queue_active, m, pageq); 666 TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq); 667 s = splhigh(); 668 TAILQ_REMOVE(&m->object->memq, m, listq); 669 TAILQ_INSERT_TAIL(&m->object->memq, m, listq); 670 splx(s); 671 } else { 672 m->flags &= ~PG_REFERENCED; 673 pmap_clear_reference(VM_PAGE_TO_PHYS(m)); 674 m->act_count -= min(m->act_count, ACT_DECLINE); 675 676 /* 677 * if the page act_count is zero -- then we deactivate 678 */ 679 if (!m->act_count && (page_shortage > 0)) { 680 if (m->object->ref_count == 0) { 681 vm_page_test_dirty(m); 682 --page_shortage; 683 if ((m->dirty & m->valid) == 0) { 684 m->act_count = 0; 685 vm_page_cache(m); 686 } else { 687 vm_page_deactivate(m); 688 } 689 } else { 690 vm_page_deactivate(m); 691 --page_shortage; 692 } 693 } else if (m->act_count) { 694 TAILQ_REMOVE(&vm_page_queue_active, m, pageq); 695 TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq); 696 } 697 } 698 m = next; 699 } 700 701 /* 702 * We try to maintain some *really* free pages, this allows interrupt 703 * code to be guaranteed space. 704 */ 705 while (cnt.v_free_count < cnt.v_free_reserved) { 706 m = vm_page_queue_cache.tqh_first; 707 if (!m) 708 break; 709 vm_page_free(m); 710 } 711 712 /* 713 * make sure that we have swap space -- if we are low on memory and 714 * swap -- then kill the biggest process. 715 */ 716 if ((vm_swap_size == 0 || swap_pager_full) && 717 ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_free_min)) { 718 bigproc = NULL; 719 bigsize = 0; 720 for (p = (struct proc *) allproc; p != NULL; p = p->p_next) { 721 /* 722 * if this is a system process, skip it 723 */ 724 if ((p->p_flag & P_SYSTEM) || (p->p_pid == 1) || 725 ((p->p_pid < 48) && (vm_swap_size != 0))) { 726 continue; 727 } 728 /* 729 * if the process is in a non-running type state, 730 * don't touch it. 731 */ 732 if (p->p_stat != SRUN && p->p_stat != SSLEEP) { 733 continue; 734 } 735 /* 736 * get the process size 737 */ 738 size = p->p_vmspace->vm_pmap.pm_stats.resident_count; 739 /* 740 * if the this process is bigger than the biggest one 741 * remember it. 742 */ 743 if (size > bigsize) { 744 bigproc = p; 745 bigsize = size; 746 } 747 } 748 if (bigproc != NULL) { 749 printf("Process %lu killed by vm_pageout -- out of swap\n", (u_long) bigproc->p_pid); 750 psignal(bigproc, SIGKILL); 751 bigproc->p_estcpu = 0; 752 bigproc->p_nice = PRIO_MIN; 753 resetpriority(bigproc); 754 wakeup((caddr_t) &cnt.v_free_count); 755 } 756 } 757 vm_page_pagesfreed += pages_freed; 758 return force_wakeup; 759} 760 761/* 762 * vm_pageout is the high level pageout daemon. 763 */ 764void 765vm_pageout() 766{ 767 (void) spl0(); 768 769 /* 770 * Initialize some paging parameters. 771 */ 772 773 if (cnt.v_page_count > 1024) 774 cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200; 775 else 776 cnt.v_free_min = 4; 777 /* 778 * free_reserved needs to include enough for the largest swap pager 779 * structures plus enough for any pv_entry structs when paging. 780 */ 781 cnt.v_pageout_free_min = 6 + cnt.v_page_count / 1024; 782 cnt.v_free_reserved = cnt.v_pageout_free_min + 2; 783 cnt.v_free_target = 3 * cnt.v_free_min + cnt.v_free_reserved; 784 cnt.v_inactive_target = cnt.v_free_count / 4; 785 if (cnt.v_inactive_target > 512) 786 cnt.v_inactive_target = 512; 787 cnt.v_free_min += cnt.v_free_reserved; 788 if (cnt.v_page_count > 1024) { 789 cnt.v_cache_max = (cnt.v_free_count - 1024) / 2; 790 cnt.v_cache_min = (cnt.v_free_count - 1024) / 20; 791 } else { 792 cnt.v_cache_min = 0; 793 cnt.v_cache_max = 0; 794 } 795 796 /* XXX does not really belong here */ 797 if (vm_page_max_wired == 0) 798 vm_page_max_wired = cnt.v_free_count / 3; 799 800 801 (void) swap_pager_alloc(0, 0, 0, 0); 802 /* 803 * The pageout daemon is never done, so loop forever. 804 */ 805 while (TRUE) { 806 tsleep((caddr_t) &vm_pages_needed, PVM, "psleep", 0); 807 cnt.v_pdwakeups++; 808 vm_pager_sync(); 809 vm_pageout_scan(); 810 vm_pager_sync(); 811 wakeup((caddr_t) &cnt.v_free_count); 812 wakeup((caddr_t) kmem_map); 813 } 814} 815 816void 817vm_daemon __P((void)) 818{ 819 vm_object_t object; 820 struct proc *p; 821 822 while (TRUE) { 823 tsleep((caddr_t) &vm_daemon_needed, PUSER, "psleep", 0); 824 swapout_threads(); 825 /* 826 * scan the processes for exceeding their rlimits or if 827 * process is swapped out -- deactivate pages 828 */ 829 830 for (p = (struct proc *) allproc; p != NULL; p = p->p_next) { 831 int overage; 832 quad_t limit; 833 vm_offset_t size; 834 835 /* 836 * if this is a system process or if we have already 837 * looked at this process, skip it. 838 */ 839 if (p->p_flag & (P_SYSTEM | P_WEXIT)) { 840 continue; 841 } 842 /* 843 * if the process is in a non-running type state, 844 * don't touch it. 845 */ 846 if (p->p_stat != SRUN && p->p_stat != SSLEEP) { 847 continue; 848 } 849 /* 850 * get a limit 851 */ 852 limit = qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur, 853 p->p_rlimit[RLIMIT_RSS].rlim_max); 854 855 /* 856 * let processes that are swapped out really be 857 * swapped out set the limit to nothing (will force a 858 * swap-out.) 859 */ 860 if ((p->p_flag & P_INMEM) == 0) 861 limit = 0; /* XXX */ 862 863 size = p->p_vmspace->vm_pmap.pm_stats.resident_count * NBPG; 864 if (limit >= 0 && size >= limit) { 865 overage = (size - limit) / NBPG; 866 vm_pageout_map_deactivate_pages(&p->p_vmspace->vm_map, 867 (vm_map_entry_t) 0, &overage, vm_pageout_object_deactivate_pages); 868 } 869 } 870 } 871 872 /* 873 * we remove cached objects that have no RSS... 874 */ 875restart: 876 vm_object_cache_lock(); 877 object = vm_object_cached_list.tqh_first; 878 while (object) { 879 vm_object_cache_unlock(); 880 /* 881 * if there are no resident pages -- get rid of the object 882 */ 883 if (object->resident_page_count == 0) { 884 if (object != vm_object_lookup(object->pager)) 885 panic("vm_object_cache_trim: I'm sooo confused."); 886 pager_cache(object, FALSE); 887 goto restart; 888 } 889 object = object->cached_list.tqe_next; 890 vm_object_cache_lock(); 891 } 892 vm_object_cache_unlock(); 893} 894