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