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