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