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