1 /* $NetBSD: uvm_pdaemon.c,v 1.106 2012/06/05 22:51:47 jym Exp $ */ 2 3 /* 4 * Copyright (c) 1997 Charles D. Cranor and Washington University. 5 * Copyright (c) 1991, 1993, The Regents of the University of California. 6 * 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. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)vm_pageout.c 8.5 (Berkeley) 2/14/94 37 * from: Id: uvm_pdaemon.c,v 1.1.2.32 1998/02/06 05:26:30 chs Exp 38 * 39 * 40 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 41 * All rights reserved. 42 * 43 * Permission to use, copy, modify and distribute this software and 44 * its documentation is hereby granted, provided that both the copyright 45 * notice and this permission notice appear in all copies of the 46 * software, derivative works or modified versions, and any portions 47 * thereof, and that both notices appear in supporting documentation. 48 * 49 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 50 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 51 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 52 * 53 * Carnegie Mellon requests users of this software to return to 54 * 55 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 56 * School of Computer Science 57 * Carnegie Mellon University 58 * Pittsburgh PA 15213-3890 59 * 60 * any improvements or extensions that they make and grant Carnegie the 61 * rights to redistribute these changes. 62 */ 63 64 /* 65 * uvm_pdaemon.c: the page daemon 66 */ 67 68 #include <sys/cdefs.h> 69 __KERNEL_RCSID(0, "$NetBSD: uvm_pdaemon.c,v 1.106 2012/06/05 22:51:47 jym Exp $"); 70 71 #include "opt_uvmhist.h" 72 #include "opt_readahead.h" 73 74 #include <sys/param.h> 75 #include <sys/proc.h> 76 #include <sys/systm.h> 77 #include <sys/kernel.h> 78 #include <sys/pool.h> 79 #include <sys/buf.h> 80 #include <sys/module.h> 81 #include <sys/atomic.h> 82 83 #include <uvm/uvm.h> 84 #include <uvm/uvm_pdpolicy.h> 85 86 /* 87 * UVMPD_NUMDIRTYREACTS is how many dirty pages the pagedaemon will reactivate 88 * in a pass thru the inactive list when swap is full. the value should be 89 * "small"... if it's too large we'll cycle the active pages thru the inactive 90 * queue too quickly to for them to be referenced and avoid being freed. 91 */ 92 93 #define UVMPD_NUMDIRTYREACTS 16 94 95 #define UVMPD_NUMTRYLOCKOWNER 16 96 97 /* 98 * local prototypes 99 */ 100 101 static void uvmpd_scan(void); 102 static void uvmpd_scan_queue(void); 103 static void uvmpd_tune(void); 104 105 static unsigned int uvm_pagedaemon_waiters; 106 107 /* 108 * XXX hack to avoid hangs when large processes fork. 109 */ 110 u_int uvm_extrapages; 111 112 /* 113 * uvm_wait: wait (sleep) for the page daemon to free some pages 114 * 115 * => should be called with all locks released 116 * => should _not_ be called by the page daemon (to avoid deadlock) 117 */ 118 119 void 120 uvm_wait(const char *wmsg) 121 { 122 int timo = 0; 123 124 mutex_spin_enter(&uvm_fpageqlock); 125 126 /* 127 * check for page daemon going to sleep (waiting for itself) 128 */ 129 130 if (curlwp == uvm.pagedaemon_lwp && uvmexp.paging == 0) { 131 /* 132 * now we have a problem: the pagedaemon wants to go to 133 * sleep until it frees more memory. but how can it 134 * free more memory if it is asleep? that is a deadlock. 135 * we have two options: 136 * [1] panic now 137 * [2] put a timeout on the sleep, thus causing the 138 * pagedaemon to only pause (rather than sleep forever) 139 * 140 * note that option [2] will only help us if we get lucky 141 * and some other process on the system breaks the deadlock 142 * by exiting or freeing memory (thus allowing the pagedaemon 143 * to continue). for now we panic if DEBUG is defined, 144 * otherwise we hope for the best with option [2] (better 145 * yet, this should never happen in the first place!). 146 */ 147 148 printf("pagedaemon: deadlock detected!\n"); 149 timo = hz >> 3; /* set timeout */ 150 #if defined(DEBUG) 151 /* DEBUG: panic so we can debug it */ 152 panic("pagedaemon deadlock"); 153 #endif 154 } 155 156 uvm_pagedaemon_waiters++; 157 wakeup(&uvm.pagedaemon); /* wake the daemon! */ 158 UVM_UNLOCK_AND_WAIT(&uvmexp.free, &uvm_fpageqlock, false, wmsg, timo); 159 } 160 161 /* 162 * uvm_kick_pdaemon: perform checks to determine if we need to 163 * give the pagedaemon a nudge, and do so if necessary. 164 * 165 * => called with uvm_fpageqlock held. 166 */ 167 168 void 169 uvm_kick_pdaemon(void) 170 { 171 172 KASSERT(mutex_owned(&uvm_fpageqlock)); 173 174 if (uvmexp.free + uvmexp.paging < uvmexp.freemin || 175 (uvmexp.free + uvmexp.paging < uvmexp.freetarg && 176 uvmpdpol_needsscan_p()) || 177 uvm_km_va_starved_p()) { 178 wakeup(&uvm.pagedaemon); 179 } 180 } 181 182 /* 183 * uvmpd_tune: tune paging parameters 184 * 185 * => called when ever memory is added (or removed?) to the system 186 * => caller must call with page queues locked 187 */ 188 189 static void 190 uvmpd_tune(void) 191 { 192 int val; 193 194 UVMHIST_FUNC("uvmpd_tune"); UVMHIST_CALLED(pdhist); 195 196 /* 197 * try to keep 0.5% of available RAM free, but limit to between 198 * 128k and 1024k per-CPU. XXX: what are these values good for? 199 */ 200 val = uvmexp.npages / 200; 201 val = MAX(val, (128*1024) >> PAGE_SHIFT); 202 val = MIN(val, (1024*1024) >> PAGE_SHIFT); 203 val *= ncpu; 204 205 /* Make sure there's always a user page free. */ 206 if (val < uvmexp.reserve_kernel + 1) 207 val = uvmexp.reserve_kernel + 1; 208 uvmexp.freemin = val; 209 210 /* Calculate free target. */ 211 val = (uvmexp.freemin * 4) / 3; 212 if (val <= uvmexp.freemin) 213 val = uvmexp.freemin + 1; 214 uvmexp.freetarg = val + atomic_swap_uint(&uvm_extrapages, 0); 215 216 uvmexp.wiredmax = uvmexp.npages / 3; 217 UVMHIST_LOG(pdhist, "<- done, freemin=%d, freetarg=%d, wiredmax=%d", 218 uvmexp.freemin, uvmexp.freetarg, uvmexp.wiredmax, 0); 219 } 220 221 /* 222 * uvm_pageout: the main loop for the pagedaemon 223 */ 224 225 void 226 uvm_pageout(void *arg) 227 { 228 int bufcnt, npages = 0; 229 int extrapages = 0; 230 struct pool *pp; 231 232 UVMHIST_FUNC("uvm_pageout"); UVMHIST_CALLED(pdhist); 233 234 UVMHIST_LOG(pdhist,"<starting uvm pagedaemon>", 0, 0, 0, 0); 235 236 /* 237 * ensure correct priority and set paging parameters... 238 */ 239 240 uvm.pagedaemon_lwp = curlwp; 241 mutex_enter(&uvm_pageqlock); 242 npages = uvmexp.npages; 243 uvmpd_tune(); 244 mutex_exit(&uvm_pageqlock); 245 246 /* 247 * main loop 248 */ 249 250 for (;;) { 251 bool needsscan, needsfree, kmem_va_starved; 252 253 kmem_va_starved = uvm_km_va_starved_p(); 254 255 mutex_spin_enter(&uvm_fpageqlock); 256 if ((uvm_pagedaemon_waiters == 0 || uvmexp.paging > 0) && 257 !kmem_va_starved) { 258 UVMHIST_LOG(pdhist," <<SLEEPING>>",0,0,0,0); 259 UVM_UNLOCK_AND_WAIT(&uvm.pagedaemon, 260 &uvm_fpageqlock, false, "pgdaemon", 0); 261 uvmexp.pdwoke++; 262 UVMHIST_LOG(pdhist," <<WOKE UP>>",0,0,0,0); 263 } else { 264 mutex_spin_exit(&uvm_fpageqlock); 265 } 266 267 /* 268 * now lock page queues and recompute inactive count 269 */ 270 271 mutex_enter(&uvm_pageqlock); 272 if (npages != uvmexp.npages || extrapages != uvm_extrapages) { 273 npages = uvmexp.npages; 274 extrapages = uvm_extrapages; 275 mutex_spin_enter(&uvm_fpageqlock); 276 uvmpd_tune(); 277 mutex_spin_exit(&uvm_fpageqlock); 278 } 279 280 uvmpdpol_tune(); 281 282 /* 283 * Estimate a hint. Note that bufmem are returned to 284 * system only when entire pool page is empty. 285 */ 286 mutex_spin_enter(&uvm_fpageqlock); 287 bufcnt = uvmexp.freetarg - uvmexp.free; 288 if (bufcnt < 0) 289 bufcnt = 0; 290 291 UVMHIST_LOG(pdhist," free/ftarg=%d/%d", 292 uvmexp.free, uvmexp.freetarg, 0,0); 293 294 needsfree = uvmexp.free + uvmexp.paging < uvmexp.freetarg; 295 needsscan = needsfree || uvmpdpol_needsscan_p(); 296 297 /* 298 * scan if needed 299 */ 300 if (needsscan) { 301 mutex_spin_exit(&uvm_fpageqlock); 302 uvmpd_scan(); 303 mutex_spin_enter(&uvm_fpageqlock); 304 } 305 306 /* 307 * if there's any free memory to be had, 308 * wake up any waiters. 309 */ 310 if (uvmexp.free > uvmexp.reserve_kernel || 311 uvmexp.paging == 0) { 312 wakeup(&uvmexp.free); 313 uvm_pagedaemon_waiters = 0; 314 } 315 mutex_spin_exit(&uvm_fpageqlock); 316 317 /* 318 * scan done. unlock page queues (the only lock we are holding) 319 */ 320 mutex_exit(&uvm_pageqlock); 321 322 /* 323 * if we don't need free memory, we're done. 324 */ 325 326 if (!needsfree && !kmem_va_starved) 327 continue; 328 329 /* 330 * kill unused metadata buffers. 331 */ 332 mutex_enter(&bufcache_lock); 333 buf_drain(bufcnt << PAGE_SHIFT); 334 mutex_exit(&bufcache_lock); 335 336 /* 337 * drain the pools. 338 */ 339 pool_drain(&pp); 340 } 341 /*NOTREACHED*/ 342 } 343 344 345 /* 346 * uvm_aiodone_worker: a workqueue callback for the aiodone daemon. 347 */ 348 349 void 350 uvm_aiodone_worker(struct work *wk, void *dummy) 351 { 352 struct buf *bp = (void *)wk; 353 354 KASSERT(&bp->b_work == wk); 355 356 /* 357 * process an i/o that's done. 358 */ 359 360 (*bp->b_iodone)(bp); 361 } 362 363 void 364 uvm_pageout_start(int npages) 365 { 366 367 mutex_spin_enter(&uvm_fpageqlock); 368 uvmexp.paging += npages; 369 mutex_spin_exit(&uvm_fpageqlock); 370 } 371 372 void 373 uvm_pageout_done(int npages) 374 { 375 376 mutex_spin_enter(&uvm_fpageqlock); 377 KASSERT(uvmexp.paging >= npages); 378 uvmexp.paging -= npages; 379 380 /* 381 * wake up either of pagedaemon or LWPs waiting for it. 382 */ 383 384 if (uvmexp.free <= uvmexp.reserve_kernel) { 385 wakeup(&uvm.pagedaemon); 386 } else { 387 wakeup(&uvmexp.free); 388 uvm_pagedaemon_waiters = 0; 389 } 390 mutex_spin_exit(&uvm_fpageqlock); 391 } 392 393 /* 394 * uvmpd_trylockowner: trylock the page's owner. 395 * 396 * => called with pageq locked. 397 * => resolve orphaned O->A loaned page. 398 * => return the locked mutex on success. otherwise, return NULL. 399 */ 400 401 kmutex_t * 402 uvmpd_trylockowner(struct vm_page *pg) 403 { 404 struct uvm_object *uobj = pg->uobject; 405 kmutex_t *slock; 406 407 KASSERT(mutex_owned(&uvm_pageqlock)); 408 409 if (uobj != NULL) { 410 slock = uobj->vmobjlock; 411 } else { 412 struct vm_anon *anon = pg->uanon; 413 414 KASSERT(anon != NULL); 415 slock = anon->an_lock; 416 } 417 418 if (!mutex_tryenter(slock)) { 419 return NULL; 420 } 421 422 if (uobj == NULL) { 423 424 /* 425 * set PQ_ANON if it isn't set already. 426 */ 427 428 if ((pg->pqflags & PQ_ANON) == 0) { 429 KASSERT(pg->loan_count > 0); 430 pg->loan_count--; 431 pg->pqflags |= PQ_ANON; 432 /* anon now owns it */ 433 } 434 } 435 436 return slock; 437 } 438 439 #if defined(VMSWAP) 440 struct swapcluster { 441 int swc_slot; 442 int swc_nallocated; 443 int swc_nused; 444 struct vm_page *swc_pages[howmany(MAXPHYS, MIN_PAGE_SIZE)]; 445 }; 446 447 static void 448 swapcluster_init(struct swapcluster *swc) 449 { 450 451 swc->swc_slot = 0; 452 swc->swc_nused = 0; 453 } 454 455 static int 456 swapcluster_allocslots(struct swapcluster *swc) 457 { 458 int slot; 459 int npages; 460 461 if (swc->swc_slot != 0) { 462 return 0; 463 } 464 465 /* Even with strange MAXPHYS, the shift 466 implicitly rounds down to a page. */ 467 npages = MAXPHYS >> PAGE_SHIFT; 468 slot = uvm_swap_alloc(&npages, true); 469 if (slot == 0) { 470 return ENOMEM; 471 } 472 swc->swc_slot = slot; 473 swc->swc_nallocated = npages; 474 swc->swc_nused = 0; 475 476 return 0; 477 } 478 479 static int 480 swapcluster_add(struct swapcluster *swc, struct vm_page *pg) 481 { 482 int slot; 483 struct uvm_object *uobj; 484 485 KASSERT(swc->swc_slot != 0); 486 KASSERT(swc->swc_nused < swc->swc_nallocated); 487 KASSERT((pg->pqflags & PQ_SWAPBACKED) != 0); 488 489 slot = swc->swc_slot + swc->swc_nused; 490 uobj = pg->uobject; 491 if (uobj == NULL) { 492 KASSERT(mutex_owned(pg->uanon->an_lock)); 493 pg->uanon->an_swslot = slot; 494 } else { 495 int result; 496 497 KASSERT(mutex_owned(uobj->vmobjlock)); 498 result = uao_set_swslot(uobj, pg->offset >> PAGE_SHIFT, slot); 499 if (result == -1) { 500 return ENOMEM; 501 } 502 } 503 swc->swc_pages[swc->swc_nused] = pg; 504 swc->swc_nused++; 505 506 return 0; 507 } 508 509 static void 510 swapcluster_flush(struct swapcluster *swc, bool now) 511 { 512 int slot; 513 int nused; 514 int nallocated; 515 int error; 516 517 if (swc->swc_slot == 0) { 518 return; 519 } 520 KASSERT(swc->swc_nused <= swc->swc_nallocated); 521 522 slot = swc->swc_slot; 523 nused = swc->swc_nused; 524 nallocated = swc->swc_nallocated; 525 526 /* 527 * if this is the final pageout we could have a few 528 * unused swap blocks. if so, free them now. 529 */ 530 531 if (nused < nallocated) { 532 if (!now) { 533 return; 534 } 535 uvm_swap_free(slot + nused, nallocated - nused); 536 } 537 538 /* 539 * now start the pageout. 540 */ 541 542 if (nused > 0) { 543 uvmexp.pdpageouts++; 544 uvm_pageout_start(nused); 545 error = uvm_swap_put(slot, swc->swc_pages, nused, 0); 546 KASSERT(error == 0 || error == ENOMEM); 547 } 548 549 /* 550 * zero swslot to indicate that we are 551 * no longer building a swap-backed cluster. 552 */ 553 554 swc->swc_slot = 0; 555 swc->swc_nused = 0; 556 } 557 558 static int 559 swapcluster_nused(struct swapcluster *swc) 560 { 561 562 return swc->swc_nused; 563 } 564 565 /* 566 * uvmpd_dropswap: free any swap allocated to this page. 567 * 568 * => called with owner locked. 569 * => return true if a page had an associated slot. 570 */ 571 572 static bool 573 uvmpd_dropswap(struct vm_page *pg) 574 { 575 bool result = false; 576 struct vm_anon *anon = pg->uanon; 577 578 if ((pg->pqflags & PQ_ANON) && anon->an_swslot) { 579 uvm_swap_free(anon->an_swslot, 1); 580 anon->an_swslot = 0; 581 pg->flags &= ~PG_CLEAN; 582 result = true; 583 } else if (pg->pqflags & PQ_AOBJ) { 584 int slot = uao_set_swslot(pg->uobject, 585 pg->offset >> PAGE_SHIFT, 0); 586 if (slot) { 587 uvm_swap_free(slot, 1); 588 pg->flags &= ~PG_CLEAN; 589 result = true; 590 } 591 } 592 593 return result; 594 } 595 596 /* 597 * uvmpd_trydropswap: try to free any swap allocated to this page. 598 * 599 * => return true if a slot is successfully freed. 600 */ 601 602 bool 603 uvmpd_trydropswap(struct vm_page *pg) 604 { 605 kmutex_t *slock; 606 bool result; 607 608 if ((pg->flags & PG_BUSY) != 0) { 609 return false; 610 } 611 612 /* 613 * lock the page's owner. 614 */ 615 616 slock = uvmpd_trylockowner(pg); 617 if (slock == NULL) { 618 return false; 619 } 620 621 /* 622 * skip this page if it's busy. 623 */ 624 625 if ((pg->flags & PG_BUSY) != 0) { 626 mutex_exit(slock); 627 return false; 628 } 629 630 result = uvmpd_dropswap(pg); 631 632 mutex_exit(slock); 633 634 return result; 635 } 636 637 #endif /* defined(VMSWAP) */ 638 639 /* 640 * uvmpd_scan_queue: scan an replace candidate list for pages 641 * to clean or free. 642 * 643 * => called with page queues locked 644 * => we work on meeting our free target by converting inactive pages 645 * into free pages. 646 * => we handle the building of swap-backed clusters 647 */ 648 649 static void 650 uvmpd_scan_queue(void) 651 { 652 struct vm_page *p; 653 struct uvm_object *uobj; 654 struct vm_anon *anon; 655 #if defined(VMSWAP) 656 struct swapcluster swc; 657 #endif /* defined(VMSWAP) */ 658 int dirtyreacts; 659 int lockownerfail; 660 kmutex_t *slock; 661 UVMHIST_FUNC("uvmpd_scan_queue"); UVMHIST_CALLED(pdhist); 662 663 /* 664 * swslot is non-zero if we are building a swap cluster. we want 665 * to stay in the loop while we have a page to scan or we have 666 * a swap-cluster to build. 667 */ 668 669 #if defined(VMSWAP) 670 swapcluster_init(&swc); 671 #endif /* defined(VMSWAP) */ 672 673 dirtyreacts = 0; 674 lockownerfail = 0; 675 uvmpdpol_scaninit(); 676 677 while (/* CONSTCOND */ 1) { 678 679 /* 680 * see if we've met the free target. 681 */ 682 683 if (uvmexp.free + uvmexp.paging 684 #if defined(VMSWAP) 685 + swapcluster_nused(&swc) 686 #endif /* defined(VMSWAP) */ 687 >= uvmexp.freetarg << 2 || 688 dirtyreacts == UVMPD_NUMDIRTYREACTS) { 689 UVMHIST_LOG(pdhist," met free target: " 690 "exit loop", 0, 0, 0, 0); 691 break; 692 } 693 694 p = uvmpdpol_selectvictim(); 695 if (p == NULL) { 696 break; 697 } 698 KASSERT(uvmpdpol_pageisqueued_p(p)); 699 KASSERT(p->wire_count == 0); 700 701 /* 702 * we are below target and have a new page to consider. 703 */ 704 705 anon = p->uanon; 706 uobj = p->uobject; 707 708 /* 709 * first we attempt to lock the object that this page 710 * belongs to. if our attempt fails we skip on to 711 * the next page (no harm done). it is important to 712 * "try" locking the object as we are locking in the 713 * wrong order (pageq -> object) and we don't want to 714 * deadlock. 715 * 716 * the only time we expect to see an ownerless page 717 * (i.e. a page with no uobject and !PQ_ANON) is if an 718 * anon has loaned a page from a uvm_object and the 719 * uvm_object has dropped the ownership. in that 720 * case, the anon can "take over" the loaned page 721 * and make it its own. 722 */ 723 724 slock = uvmpd_trylockowner(p); 725 if (slock == NULL) { 726 /* 727 * yield cpu to make a chance for an LWP holding 728 * the lock run. otherwise we can busy-loop too long 729 * if the page queue is filled with a lot of pages 730 * from few objects. 731 */ 732 lockownerfail++; 733 if (lockownerfail > UVMPD_NUMTRYLOCKOWNER) { 734 mutex_exit(&uvm_pageqlock); 735 /* XXX Better than yielding but inadequate. */ 736 kpause("livelock", false, 1, NULL); 737 mutex_enter(&uvm_pageqlock); 738 lockownerfail = 0; 739 } 740 continue; 741 } 742 if (p->flags & PG_BUSY) { 743 mutex_exit(slock); 744 uvmexp.pdbusy++; 745 continue; 746 } 747 748 /* does the page belong to an object? */ 749 if (uobj != NULL) { 750 uvmexp.pdobscan++; 751 } else { 752 #if defined(VMSWAP) 753 KASSERT(anon != NULL); 754 uvmexp.pdanscan++; 755 #else /* defined(VMSWAP) */ 756 panic("%s: anon", __func__); 757 #endif /* defined(VMSWAP) */ 758 } 759 760 761 /* 762 * we now have the object and the page queues locked. 763 * if the page is not swap-backed, call the object's 764 * pager to flush and free the page. 765 */ 766 767 #if defined(READAHEAD_STATS) 768 if ((p->pqflags & PQ_READAHEAD) != 0) { 769 p->pqflags &= ~PQ_READAHEAD; 770 uvm_ra_miss.ev_count++; 771 } 772 #endif /* defined(READAHEAD_STATS) */ 773 774 if ((p->pqflags & PQ_SWAPBACKED) == 0) { 775 KASSERT(uobj != NULL); 776 mutex_exit(&uvm_pageqlock); 777 (void) (uobj->pgops->pgo_put)(uobj, p->offset, 778 p->offset + PAGE_SIZE, PGO_CLEANIT|PGO_FREE); 779 mutex_enter(&uvm_pageqlock); 780 continue; 781 } 782 783 /* 784 * the page is swap-backed. remove all the permissions 785 * from the page so we can sync the modified info 786 * without any race conditions. if the page is clean 787 * we can free it now and continue. 788 */ 789 790 pmap_page_protect(p, VM_PROT_NONE); 791 if ((p->flags & PG_CLEAN) && pmap_clear_modify(p)) { 792 p->flags &= ~(PG_CLEAN); 793 } 794 if (p->flags & PG_CLEAN) { 795 int slot; 796 int pageidx; 797 798 pageidx = p->offset >> PAGE_SHIFT; 799 uvm_pagefree(p); 800 uvmexp.pdfreed++; 801 802 /* 803 * for anons, we need to remove the page 804 * from the anon ourselves. for aobjs, 805 * pagefree did that for us. 806 */ 807 808 if (anon) { 809 KASSERT(anon->an_swslot != 0); 810 anon->an_page = NULL; 811 slot = anon->an_swslot; 812 } else { 813 slot = uao_find_swslot(uobj, pageidx); 814 } 815 mutex_exit(slock); 816 817 if (slot > 0) { 818 /* this page is now only in swap. */ 819 mutex_enter(&uvm_swap_data_lock); 820 KASSERT(uvmexp.swpgonly < uvmexp.swpginuse); 821 uvmexp.swpgonly++; 822 mutex_exit(&uvm_swap_data_lock); 823 } 824 continue; 825 } 826 827 #if defined(VMSWAP) 828 /* 829 * this page is dirty, skip it if we'll have met our 830 * free target when all the current pageouts complete. 831 */ 832 833 if (uvmexp.free + uvmexp.paging > uvmexp.freetarg << 2) { 834 mutex_exit(slock); 835 continue; 836 } 837 838 /* 839 * free any swap space allocated to the page since 840 * we'll have to write it again with its new data. 841 */ 842 843 uvmpd_dropswap(p); 844 845 /* 846 * start new swap pageout cluster (if necessary). 847 * 848 * if swap is full reactivate this page so that 849 * we eventually cycle all pages through the 850 * inactive queue. 851 */ 852 853 if (swapcluster_allocslots(&swc)) { 854 dirtyreacts++; 855 uvm_pageactivate(p); 856 mutex_exit(slock); 857 continue; 858 } 859 860 /* 861 * at this point, we're definitely going reuse this 862 * page. mark the page busy and delayed-free. 863 * we should remove the page from the page queues 864 * so we don't ever look at it again. 865 * adjust counters and such. 866 */ 867 868 p->flags |= PG_BUSY; 869 UVM_PAGE_OWN(p, "scan_queue"); 870 871 p->flags |= PG_PAGEOUT; 872 uvm_pagedequeue(p); 873 874 uvmexp.pgswapout++; 875 mutex_exit(&uvm_pageqlock); 876 877 /* 878 * add the new page to the cluster. 879 */ 880 881 if (swapcluster_add(&swc, p)) { 882 p->flags &= ~(PG_BUSY|PG_PAGEOUT); 883 UVM_PAGE_OWN(p, NULL); 884 mutex_enter(&uvm_pageqlock); 885 dirtyreacts++; 886 uvm_pageactivate(p); 887 mutex_exit(slock); 888 continue; 889 } 890 mutex_exit(slock); 891 892 swapcluster_flush(&swc, false); 893 mutex_enter(&uvm_pageqlock); 894 895 /* 896 * the pageout is in progress. bump counters and set up 897 * for the next loop. 898 */ 899 900 uvmexp.pdpending++; 901 902 #else /* defined(VMSWAP) */ 903 uvm_pageactivate(p); 904 mutex_exit(slock); 905 #endif /* defined(VMSWAP) */ 906 } 907 908 #if defined(VMSWAP) 909 mutex_exit(&uvm_pageqlock); 910 swapcluster_flush(&swc, true); 911 mutex_enter(&uvm_pageqlock); 912 #endif /* defined(VMSWAP) */ 913 } 914 915 /* 916 * uvmpd_scan: scan the page queues and attempt to meet our targets. 917 * 918 * => called with pageq's locked 919 */ 920 921 static void 922 uvmpd_scan(void) 923 { 924 int swap_shortage, pages_freed; 925 UVMHIST_FUNC("uvmpd_scan"); UVMHIST_CALLED(pdhist); 926 927 uvmexp.pdrevs++; 928 929 /* 930 * work on meeting our targets. first we work on our free target 931 * by converting inactive pages into free pages. then we work on 932 * meeting our inactive target by converting active pages to 933 * inactive ones. 934 */ 935 936 UVMHIST_LOG(pdhist, " starting 'free' loop",0,0,0,0); 937 938 pages_freed = uvmexp.pdfreed; 939 uvmpd_scan_queue(); 940 pages_freed = uvmexp.pdfreed - pages_freed; 941 942 /* 943 * detect if we're not going to be able to page anything out 944 * until we free some swap resources from active pages. 945 */ 946 947 swap_shortage = 0; 948 if (uvmexp.free < uvmexp.freetarg && 949 uvmexp.swpginuse >= uvmexp.swpgavail && 950 !uvm_swapisfull() && 951 pages_freed == 0) { 952 swap_shortage = uvmexp.freetarg - uvmexp.free; 953 } 954 955 uvmpdpol_balancequeue(swap_shortage); 956 957 /* 958 * if still below the minimum target, try unloading kernel 959 * modules. 960 */ 961 962 if (uvmexp.free < uvmexp.freemin) { 963 module_thread_kick(); 964 } 965 } 966 967 /* 968 * uvm_reclaimable: decide whether to wait for pagedaemon. 969 * 970 * => return true if it seems to be worth to do uvm_wait. 971 * 972 * XXX should be tunable. 973 * XXX should consider pools, etc? 974 */ 975 976 bool 977 uvm_reclaimable(void) 978 { 979 int filepages; 980 int active, inactive; 981 982 /* 983 * if swap is not full, no problem. 984 */ 985 986 if (!uvm_swapisfull()) { 987 return true; 988 } 989 990 /* 991 * file-backed pages can be reclaimed even when swap is full. 992 * if we have more than 1/16 of pageable memory or 5MB, try to reclaim. 993 * 994 * XXX assume the worst case, ie. all wired pages are file-backed. 995 * 996 * XXX should consider about other reclaimable memory. 997 * XXX ie. pools, traditional buffer cache. 998 */ 999 1000 filepages = uvmexp.filepages + uvmexp.execpages - uvmexp.wired; 1001 uvm_estimatepageable(&active, &inactive); 1002 if (filepages >= MIN((active + inactive) >> 4, 1003 5 * 1024 * 1024 >> PAGE_SHIFT)) { 1004 return true; 1005 } 1006 1007 /* 1008 * kill the process, fail allocation, etc.. 1009 */ 1010 1011 return false; 1012 } 1013 1014 void 1015 uvm_estimatepageable(int *active, int *inactive) 1016 { 1017 1018 uvmpdpol_estimatepageable(active, inactive); 1019 } 1020 1021