1 /* $OpenBSD: uvm_pdaemon.c,v 1.107 2023/10/16 11:32:54 mpi Exp $ */ 2 /* $NetBSD: uvm_pdaemon.c,v 1.23 2000/08/20 10:24:14 bjh21 Exp $ */ 3 4 /* 5 * Copyright (c) 1997 Charles D. Cranor and Washington University. 6 * Copyright (c) 1991, 1993, The Regents of the University of California. 7 * 8 * All rights reserved. 9 * 10 * This code is derived from software contributed to Berkeley by 11 * The Mach Operating System project at Carnegie-Mellon University. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * @(#)vm_pageout.c 8.5 (Berkeley) 2/14/94 38 * from: Id: uvm_pdaemon.c,v 1.1.2.32 1998/02/06 05:26:30 chs Exp 39 * 40 * 41 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 42 * All rights reserved. 43 * 44 * Permission to use, copy, modify and distribute this software and 45 * its documentation is hereby granted, provided that both the copyright 46 * notice and this permission notice appear in all copies of the 47 * software, derivative works or modified versions, and any portions 48 * thereof, and that both notices appear in supporting documentation. 49 * 50 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 51 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 52 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 53 * 54 * Carnegie Mellon requests users of this software to return to 55 * 56 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 57 * School of Computer Science 58 * Carnegie Mellon University 59 * Pittsburgh PA 15213-3890 60 * 61 * any improvements or extensions that they make and grant Carnegie the 62 * rights to redistribute these changes. 63 */ 64 65 /* 66 * uvm_pdaemon.c: the page daemon 67 */ 68 69 #include <sys/param.h> 70 #include <sys/systm.h> 71 #include <sys/kernel.h> 72 #include <sys/pool.h> 73 #include <sys/proc.h> 74 #include <sys/buf.h> 75 #include <sys/mount.h> 76 #include <sys/atomic.h> 77 78 #ifdef HIBERNATE 79 #include <sys/hibernate.h> 80 #endif 81 82 #include <uvm/uvm.h> 83 84 #include "drm.h" 85 86 #if NDRM > 0 87 extern void drmbackoff(long); 88 #endif 89 90 /* 91 * UVMPD_NUMDIRTYREACTS is how many dirty pages the pagedaemon will reactivate 92 * in a pass thru the inactive list when swap is full. the value should be 93 * "small"... if it's too large we'll cycle the active pages thru the inactive 94 * queue too quickly to for them to be referenced and avoid being freed. 95 */ 96 97 #define UVMPD_NUMDIRTYREACTS 16 98 99 100 /* 101 * local prototypes 102 */ 103 104 struct rwlock *uvmpd_trylockowner(struct vm_page *); 105 void uvmpd_scan(struct uvm_pmalloc *, struct uvm_constraint_range *); 106 void uvmpd_scan_inactive(struct uvm_pmalloc *, 107 struct uvm_constraint_range *, struct pglist *); 108 void uvmpd_tune(void); 109 void uvmpd_drop(struct pglist *); 110 void uvmpd_dropswap(struct vm_page *); 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 uint64_t timo = INFSLP; 123 124 #ifdef DIAGNOSTIC 125 if (curproc == &proc0) 126 panic("%s: cannot sleep for memory during boot", __func__); 127 #endif 128 129 /* 130 * check for page daemon going to sleep (waiting for itself) 131 */ 132 if (curproc == uvm.pagedaemon_proc) { 133 printf("uvm_wait emergency bufbackoff\n"); 134 if (bufbackoff(NULL, 4) == 0) 135 return; 136 /* 137 * now we have a problem: the pagedaemon wants to go to 138 * sleep until it frees more memory. but how can it 139 * free more memory if it is asleep? that is a deadlock. 140 * we have two options: 141 * [1] panic now 142 * [2] put a timeout on the sleep, thus causing the 143 * pagedaemon to only pause (rather than sleep forever) 144 * 145 * note that option [2] will only help us if we get lucky 146 * and some other process on the system breaks the deadlock 147 * by exiting or freeing memory (thus allowing the pagedaemon 148 * to continue). for now we panic if DEBUG is defined, 149 * otherwise we hope for the best with option [2] (better 150 * yet, this should never happen in the first place!). 151 */ 152 153 printf("pagedaemon: deadlock detected!\n"); 154 timo = MSEC_TO_NSEC(125); /* set timeout */ 155 #if defined(DEBUG) 156 /* DEBUG: panic so we can debug it */ 157 panic("pagedaemon deadlock"); 158 #endif 159 } 160 161 uvm_lock_fpageq(); 162 wakeup(&uvm.pagedaemon); /* wake the daemon! */ 163 msleep_nsec(&uvmexp.free, &uvm.fpageqlock, PVM | PNORELOCK, wmsg, timo); 164 } 165 166 /* 167 * uvmpd_tune: tune paging parameters 168 * 169 * => called whenever memory is added to (or removed from?) the system 170 * => caller must call with page queues locked 171 */ 172 173 void 174 uvmpd_tune(void) 175 { 176 177 uvmexp.freemin = uvmexp.npages / 30; 178 179 /* between 16k and 512k */ 180 /* XXX: what are these values good for? */ 181 uvmexp.freemin = max(uvmexp.freemin, (16*1024) >> PAGE_SHIFT); 182 #if 0 183 uvmexp.freemin = min(uvmexp.freemin, (512*1024) >> PAGE_SHIFT); 184 #endif 185 186 /* Make sure there's always a user page free. */ 187 if (uvmexp.freemin < uvmexp.reserve_kernel + 1) 188 uvmexp.freemin = uvmexp.reserve_kernel + 1; 189 190 uvmexp.freetarg = (uvmexp.freemin * 4) / 3; 191 if (uvmexp.freetarg <= uvmexp.freemin) 192 uvmexp.freetarg = uvmexp.freemin + 1; 193 194 /* uvmexp.inactarg: computed in main daemon loop */ 195 196 uvmexp.wiredmax = uvmexp.npages / 3; 197 } 198 199 /* 200 * Indicate to the page daemon that a nowait call failed and it should 201 * recover at least some memory in the most restricted region (assumed 202 * to be dma_constraint). 203 */ 204 volatile int uvm_nowait_failed; 205 206 /* 207 * uvm_pageout: the main loop for the pagedaemon 208 */ 209 void 210 uvm_pageout(void *arg) 211 { 212 struct uvm_constraint_range constraint; 213 struct uvm_pmalloc *pma; 214 int npages = 0; 215 216 /* ensure correct priority and set paging parameters... */ 217 uvm.pagedaemon_proc = curproc; 218 (void) spl0(); 219 uvm_lock_pageq(); 220 npages = uvmexp.npages; 221 uvmpd_tune(); 222 uvm_unlock_pageq(); 223 224 for (;;) { 225 long size; 226 227 uvm_lock_fpageq(); 228 if (!uvm_nowait_failed && TAILQ_EMPTY(&uvm.pmr_control.allocs)) { 229 msleep_nsec(&uvm.pagedaemon, &uvm.fpageqlock, PVM, 230 "pgdaemon", INFSLP); 231 uvmexp.pdwoke++; 232 } 233 234 if ((pma = TAILQ_FIRST(&uvm.pmr_control.allocs)) != NULL) { 235 pma->pm_flags |= UVM_PMA_BUSY; 236 constraint = pma->pm_constraint; 237 } else { 238 if (uvm_nowait_failed) { 239 /* 240 * XXX realistically, this is what our 241 * nowait callers probably care about 242 */ 243 constraint = dma_constraint; 244 uvm_nowait_failed = 0; 245 } else 246 constraint = no_constraint; 247 } 248 249 uvm_unlock_fpageq(); 250 251 /* 252 * now lock page queues and recompute inactive count 253 */ 254 uvm_lock_pageq(); 255 if (npages != uvmexp.npages) { /* check for new pages? */ 256 npages = uvmexp.npages; 257 uvmpd_tune(); 258 } 259 260 uvmexp.inactarg = (uvmexp.active + uvmexp.inactive) / 3; 261 if (uvmexp.inactarg <= uvmexp.freetarg) { 262 uvmexp.inactarg = uvmexp.freetarg + 1; 263 } 264 265 /* Reclaim pages from the buffer cache if possible. */ 266 size = 0; 267 if (pma != NULL) 268 size += pma->pm_size >> PAGE_SHIFT; 269 if (uvmexp.free - BUFPAGES_DEFICIT < uvmexp.freetarg) 270 size += uvmexp.freetarg - (uvmexp.free - 271 BUFPAGES_DEFICIT); 272 if (size == 0) 273 size = 16; /* XXX */ 274 uvm_unlock_pageq(); 275 (void) bufbackoff(&constraint, size * 2); 276 #if NDRM > 0 277 drmbackoff(size * 2); 278 #endif 279 uvm_lock_pageq(); 280 281 /* 282 * scan if needed 283 */ 284 if (pma != NULL || 285 ((uvmexp.free - BUFPAGES_DEFICIT) < uvmexp.freetarg) || 286 ((uvmexp.inactive + BUFPAGES_INACT) < uvmexp.inactarg)) { 287 uvmpd_scan(pma, &constraint); 288 } 289 290 /* 291 * if there's any free memory to be had, 292 * wake up any waiters. 293 */ 294 uvm_lock_fpageq(); 295 if (uvmexp.free > uvmexp.reserve_kernel || 296 uvmexp.paging == 0) { 297 wakeup(&uvmexp.free); 298 } 299 300 if (pma != NULL) { 301 /* 302 * XXX If UVM_PMA_FREED isn't set, no pages 303 * were freed. Should we set UVM_PMA_FAIL in 304 * that case? 305 */ 306 pma->pm_flags &= ~UVM_PMA_BUSY; 307 if (pma->pm_flags & UVM_PMA_FREED) { 308 pma->pm_flags &= ~UVM_PMA_LINKED; 309 TAILQ_REMOVE(&uvm.pmr_control.allocs, pma, 310 pmq); 311 wakeup(pma); 312 } 313 } 314 uvm_unlock_fpageq(); 315 316 /* 317 * scan done. unlock page queues (the only lock we are holding) 318 */ 319 uvm_unlock_pageq(); 320 321 sched_pause(yield); 322 } 323 /*NOTREACHED*/ 324 } 325 326 327 /* 328 * uvm_aiodone_daemon: main loop for the aiodone daemon. 329 */ 330 void 331 uvm_aiodone_daemon(void *arg) 332 { 333 int s, free; 334 struct buf *bp, *nbp; 335 336 uvm.aiodoned_proc = curproc; 337 338 for (;;) { 339 /* 340 * Check for done aio structures. If we've got structures to 341 * process, do so. Otherwise sleep while avoiding races. 342 */ 343 mtx_enter(&uvm.aiodoned_lock); 344 while ((bp = TAILQ_FIRST(&uvm.aio_done)) == NULL) 345 msleep_nsec(&uvm.aiodoned, &uvm.aiodoned_lock, 346 PVM, "aiodoned", INFSLP); 347 /* Take the list for ourselves. */ 348 TAILQ_INIT(&uvm.aio_done); 349 mtx_leave(&uvm.aiodoned_lock); 350 351 /* process each i/o that's done. */ 352 free = uvmexp.free; 353 while (bp != NULL) { 354 if (bp->b_flags & B_PDAEMON) { 355 uvmexp.paging -= bp->b_bufsize >> PAGE_SHIFT; 356 } 357 nbp = TAILQ_NEXT(bp, b_freelist); 358 s = splbio(); /* b_iodone must by called at splbio */ 359 (*bp->b_iodone)(bp); 360 splx(s); 361 bp = nbp; 362 363 sched_pause(yield); 364 } 365 uvm_lock_fpageq(); 366 wakeup(free <= uvmexp.reserve_kernel ? &uvm.pagedaemon : 367 &uvmexp.free); 368 uvm_unlock_fpageq(); 369 } 370 } 371 372 /* 373 * uvmpd_trylockowner: trylock the page's owner. 374 * 375 * => return the locked rwlock on success. otherwise, return NULL. 376 */ 377 struct rwlock * 378 uvmpd_trylockowner(struct vm_page *pg) 379 { 380 381 struct uvm_object *uobj = pg->uobject; 382 struct rwlock *slock; 383 384 if (uobj != NULL) { 385 slock = uobj->vmobjlock; 386 } else { 387 struct vm_anon *anon = pg->uanon; 388 389 KASSERT(anon != NULL); 390 slock = anon->an_lock; 391 } 392 393 if (rw_enter(slock, RW_WRITE|RW_NOSLEEP)) { 394 return NULL; 395 } 396 397 return slock; 398 } 399 400 401 /* 402 * uvmpd_dropswap: free any swap allocated to this page. 403 * 404 * => called with owner locked. 405 */ 406 void 407 uvmpd_dropswap(struct vm_page *pg) 408 { 409 struct vm_anon *anon = pg->uanon; 410 411 if ((pg->pg_flags & PQ_ANON) && anon->an_swslot) { 412 uvm_swap_free(anon->an_swslot, 1); 413 anon->an_swslot = 0; 414 } else if (pg->pg_flags & PQ_AOBJ) { 415 uao_dropswap(pg->uobject, pg->offset >> PAGE_SHIFT); 416 } 417 } 418 419 /* 420 * uvmpd_scan_inactive: scan an inactive list for pages to clean or free. 421 * 422 * => called with page queues locked 423 * => we work on meeting our free target by converting inactive pages 424 * into free pages. 425 * => we handle the building of swap-backed clusters 426 * => we return TRUE if we are exiting because we met our target 427 */ 428 void 429 uvmpd_scan_inactive(struct uvm_pmalloc *pma, 430 struct uvm_constraint_range *constraint, struct pglist *pglst) 431 { 432 int free, result; 433 struct vm_page *p, *nextpg; 434 struct uvm_object *uobj; 435 struct vm_page *pps[SWCLUSTPAGES], **ppsp; 436 int npages; 437 struct vm_page *swpps[SWCLUSTPAGES]; /* XXX: see below */ 438 struct rwlock *slock; 439 int swnpages, swcpages; /* XXX: see below */ 440 int swslot; 441 struct vm_anon *anon; 442 boolean_t swap_backed; 443 vaddr_t start; 444 int dirtyreacts; 445 paddr_t paddr; 446 447 /* 448 * swslot is non-zero if we are building a swap cluster. we want 449 * to stay in the loop while we have a page to scan or we have 450 * a swap-cluster to build. 451 */ 452 swslot = 0; 453 swnpages = swcpages = 0; 454 dirtyreacts = 0; 455 p = NULL; 456 457 /* Start with the first page on the list that fit in `constraint' */ 458 TAILQ_FOREACH(p, pglst, pageq) { 459 paddr = atop(VM_PAGE_TO_PHYS(p)); 460 if (paddr >= constraint->ucr_low && 461 paddr < constraint->ucr_high) 462 break; 463 } 464 465 for (; p != NULL || swslot != 0; p = nextpg) { 466 /* 467 * note that p can be NULL iff we have traversed the whole 468 * list and need to do one final swap-backed clustered pageout. 469 */ 470 uobj = NULL; 471 anon = NULL; 472 if (p) { 473 /* 474 * see if we've met our target 475 */ 476 free = uvmexp.free - BUFPAGES_DEFICIT; 477 if (((pma == NULL || (pma->pm_flags & UVM_PMA_FREED)) && 478 (free + uvmexp.paging >= uvmexp.freetarg << 2)) || 479 dirtyreacts == UVMPD_NUMDIRTYREACTS) { 480 if (swslot == 0) { 481 /* exit now if no swap-i/o pending */ 482 break; 483 } 484 485 /* set p to null to signal final swap i/o */ 486 p = NULL; 487 nextpg = NULL; 488 } 489 } 490 if (p) { /* if (we have a new page to consider) */ 491 /* 492 * we are below target and have a new page to consider. 493 */ 494 uvmexp.pdscans++; 495 nextpg = TAILQ_NEXT(p, pageq); 496 497 anon = p->uanon; 498 uobj = p->uobject; 499 500 /* 501 * first we attempt to lock the object that this page 502 * belongs to. if our attempt fails we skip on to 503 * the next page (no harm done). it is important to 504 * "try" locking the object as we are locking in the 505 * wrong order (pageq -> object) and we don't want to 506 * deadlock. 507 */ 508 slock = uvmpd_trylockowner(p); 509 if (slock == NULL) { 510 continue; 511 } 512 513 /* 514 * move referenced pages back to active queue 515 * and skip to next page. 516 */ 517 if (pmap_is_referenced(p)) { 518 uvm_pageactivate(p); 519 rw_exit(slock); 520 uvmexp.pdreact++; 521 continue; 522 } 523 524 if (p->pg_flags & PG_BUSY) { 525 rw_exit(slock); 526 uvmexp.pdbusy++; 527 continue; 528 } 529 530 /* does the page belong to an object? */ 531 if (uobj != NULL) { 532 uvmexp.pdobscan++; 533 } else { 534 KASSERT(anon != NULL); 535 uvmexp.pdanscan++; 536 } 537 538 /* 539 * we now have the page queues locked. 540 * the page is not busy. if the page is clean we 541 * can free it now and continue. 542 */ 543 if (p->pg_flags & PG_CLEAN) { 544 if (p->pg_flags & PQ_SWAPBACKED) { 545 /* this page now lives only in swap */ 546 atomic_inc_int(&uvmexp.swpgonly); 547 } 548 549 /* zap all mappings with pmap_page_protect... */ 550 pmap_page_protect(p, PROT_NONE); 551 uvm_pagefree(p); 552 uvmexp.pdfreed++; 553 554 if (anon) { 555 556 /* 557 * an anonymous page can only be clean 558 * if it has backing store assigned. 559 */ 560 561 KASSERT(anon->an_swslot != 0); 562 563 /* remove from object */ 564 anon->an_page = NULL; 565 } 566 rw_exit(slock); 567 continue; 568 } 569 570 /* 571 * this page is dirty, skip it if we'll have met our 572 * free target when all the current pageouts complete. 573 */ 574 if ((pma == NULL || (pma->pm_flags & UVM_PMA_FREED)) && 575 (free + uvmexp.paging > uvmexp.freetarg << 2)) { 576 rw_exit(slock); 577 continue; 578 } 579 580 /* 581 * this page is dirty, but we can't page it out 582 * since all pages in swap are only in swap. 583 * reactivate it so that we eventually cycle 584 * all pages thru the inactive queue. 585 */ 586 if ((p->pg_flags & PQ_SWAPBACKED) && uvm_swapisfull()) { 587 dirtyreacts++; 588 uvm_pageactivate(p); 589 rw_exit(slock); 590 continue; 591 } 592 593 /* 594 * if the page is swap-backed and dirty and swap space 595 * is full, free any swap allocated to the page 596 * so that other pages can be paged out. 597 */ 598 KASSERT(uvmexp.swpginuse <= uvmexp.swpages); 599 if ((p->pg_flags & PQ_SWAPBACKED) && 600 uvmexp.swpginuse == uvmexp.swpages) { 601 uvmpd_dropswap(p); 602 } 603 604 /* 605 * the page we are looking at is dirty. we must 606 * clean it before it can be freed. to do this we 607 * first mark the page busy so that no one else will 608 * touch the page. we write protect all the mappings 609 * of the page so that no one touches it while it is 610 * in I/O. 611 */ 612 613 swap_backed = ((p->pg_flags & PQ_SWAPBACKED) != 0); 614 atomic_setbits_int(&p->pg_flags, PG_BUSY); 615 UVM_PAGE_OWN(p, "scan_inactive"); 616 pmap_page_protect(p, PROT_READ); 617 uvmexp.pgswapout++; 618 619 /* 620 * for swap-backed pages we need to (re)allocate 621 * swap space. 622 */ 623 if (swap_backed) { 624 /* free old swap slot (if any) */ 625 uvmpd_dropswap(p); 626 627 /* start new cluster (if necessary) */ 628 if (swslot == 0) { 629 swnpages = SWCLUSTPAGES; 630 swslot = uvm_swap_alloc(&swnpages, 631 TRUE); 632 if (swslot == 0) { 633 /* no swap? give up! */ 634 atomic_clearbits_int( 635 &p->pg_flags, 636 PG_BUSY); 637 UVM_PAGE_OWN(p, NULL); 638 rw_exit(slock); 639 continue; 640 } 641 swcpages = 0; /* cluster is empty */ 642 } 643 644 /* add block to cluster */ 645 swpps[swcpages] = p; 646 if (anon) 647 anon->an_swslot = swslot + swcpages; 648 else 649 uao_set_swslot(uobj, 650 p->offset >> PAGE_SHIFT, 651 swslot + swcpages); 652 swcpages++; 653 } 654 } else { 655 /* if p == NULL we must be doing a last swap i/o */ 656 swap_backed = TRUE; 657 } 658 659 /* 660 * now consider doing the pageout. 661 * 662 * for swap-backed pages, we do the pageout if we have either 663 * filled the cluster (in which case (swnpages == swcpages) or 664 * run out of pages (p == NULL). 665 * 666 * for object pages, we always do the pageout. 667 */ 668 if (swap_backed) { 669 if (p) { /* if we just added a page to cluster */ 670 rw_exit(slock); 671 672 /* cluster not full yet? */ 673 if (swcpages < swnpages) 674 continue; 675 } 676 677 /* starting I/O now... set up for it */ 678 npages = swcpages; 679 ppsp = swpps; 680 /* for swap-backed pages only */ 681 start = (vaddr_t) swslot; 682 683 /* if this is final pageout we could have a few 684 * extra swap blocks */ 685 if (swcpages < swnpages) { 686 uvm_swap_free(swslot + swcpages, 687 (swnpages - swcpages)); 688 } 689 } else { 690 /* normal object pageout */ 691 ppsp = pps; 692 npages = sizeof(pps) / sizeof(struct vm_page *); 693 /* not looked at because PGO_ALLPAGES is set */ 694 start = 0; 695 } 696 697 /* 698 * now do the pageout. 699 * 700 * for swap_backed pages we have already built the cluster. 701 * for !swap_backed pages, uvm_pager_put will call the object's 702 * "make put cluster" function to build a cluster on our behalf. 703 * 704 * we pass the PGO_PDFREECLUST flag to uvm_pager_put to instruct 705 * it to free the cluster pages for us on a successful I/O (it 706 * always does this for un-successful I/O requests). this 707 * allows us to do clustered pageout without having to deal 708 * with cluster pages at this level. 709 * 710 * note locking semantics of uvm_pager_put with PGO_PDFREECLUST: 711 * IN: locked: page queues 712 * OUT: locked: 713 * !locked: pageqs 714 */ 715 716 uvmexp.pdpageouts++; 717 result = uvm_pager_put(swap_backed ? NULL : uobj, p, 718 &ppsp, &npages, PGO_ALLPAGES|PGO_PDFREECLUST, start, 0); 719 720 /* 721 * if we did i/o to swap, zero swslot to indicate that we are 722 * no longer building a swap-backed cluster. 723 */ 724 725 if (swap_backed) 726 swslot = 0; /* done with this cluster */ 727 728 /* 729 * first, we check for VM_PAGER_PEND which means that the 730 * async I/O is in progress and the async I/O done routine 731 * will clean up after us. in this case we move on to the 732 * next page. 733 * 734 * there is a very remote chance that the pending async i/o can 735 * finish _before_ we get here. if that happens, our page "p" 736 * may no longer be on the inactive queue. so we verify this 737 * when determining the next page (starting over at the head if 738 * we've lost our inactive page). 739 */ 740 741 if (result == VM_PAGER_PEND) { 742 uvmexp.paging += npages; 743 uvm_lock_pageq(); 744 uvmexp.pdpending++; 745 if (p) { 746 if (p->pg_flags & PQ_INACTIVE) 747 nextpg = TAILQ_NEXT(p, pageq); 748 else 749 nextpg = TAILQ_FIRST(pglst); 750 } else { 751 nextpg = NULL; 752 } 753 continue; 754 } 755 756 /* clean up "p" if we have one */ 757 if (p) { 758 /* 759 * the I/O request to "p" is done and uvm_pager_put 760 * has freed any cluster pages it may have allocated 761 * during I/O. all that is left for us to do is 762 * clean up page "p" (which is still PG_BUSY). 763 * 764 * our result could be one of the following: 765 * VM_PAGER_OK: successful pageout 766 * 767 * VM_PAGER_AGAIN: tmp resource shortage, we skip 768 * to next page 769 * VM_PAGER_{FAIL,ERROR,BAD}: an error. we 770 * "reactivate" page to get it out of the way (it 771 * will eventually drift back into the inactive 772 * queue for a retry). 773 * VM_PAGER_UNLOCK: should never see this as it is 774 * only valid for "get" operations 775 */ 776 777 /* relock p's object: page queues not lock yet, so 778 * no need for "try" */ 779 780 /* !swap_backed case: already locked... */ 781 if (swap_backed) { 782 rw_enter(slock, RW_WRITE); 783 } 784 785 #ifdef DIAGNOSTIC 786 if (result == VM_PAGER_UNLOCK) 787 panic("pagedaemon: pageout returned " 788 "invalid 'unlock' code"); 789 #endif 790 791 /* handle PG_WANTED now */ 792 if (p->pg_flags & PG_WANTED) 793 wakeup(p); 794 795 atomic_clearbits_int(&p->pg_flags, PG_BUSY|PG_WANTED); 796 UVM_PAGE_OWN(p, NULL); 797 798 /* released during I/O? Can only happen for anons */ 799 if (p->pg_flags & PG_RELEASED) { 800 KASSERT(anon != NULL); 801 /* 802 * remove page so we can get nextpg, 803 * also zero out anon so we don't use 804 * it after the free. 805 */ 806 anon->an_page = NULL; 807 p->uanon = NULL; 808 809 rw_exit(anon->an_lock); 810 uvm_anfree(anon); /* kills anon */ 811 pmap_page_protect(p, PROT_NONE); 812 anon = NULL; 813 uvm_lock_pageq(); 814 nextpg = TAILQ_NEXT(p, pageq); 815 /* free released page */ 816 uvm_pagefree(p); 817 } else { /* page was not released during I/O */ 818 uvm_lock_pageq(); 819 nextpg = TAILQ_NEXT(p, pageq); 820 if (result != VM_PAGER_OK) { 821 /* pageout was a failure... */ 822 if (result != VM_PAGER_AGAIN) 823 uvm_pageactivate(p); 824 pmap_clear_reference(p); 825 /* XXXCDC: if (swap_backed) FREE p's 826 * swap block? */ 827 } else { 828 /* pageout was a success... */ 829 pmap_clear_reference(p); 830 pmap_clear_modify(p); 831 atomic_setbits_int(&p->pg_flags, 832 PG_CLEAN); 833 } 834 } 835 836 /* 837 * drop object lock (if there is an object left). do 838 * a safety check of nextpg to make sure it is on the 839 * inactive queue (it should be since PG_BUSY pages on 840 * the inactive queue can't be re-queued [note: not 841 * true for active queue]). 842 */ 843 rw_exit(slock); 844 845 if (nextpg && (nextpg->pg_flags & PQ_INACTIVE) == 0) { 846 nextpg = TAILQ_FIRST(pglst); /* reload! */ 847 } 848 } else { 849 /* 850 * if p is null in this loop, make sure it stays null 851 * in the next loop. 852 */ 853 nextpg = NULL; 854 855 /* 856 * lock page queues here just so they're always locked 857 * at the end of the loop. 858 */ 859 uvm_lock_pageq(); 860 } 861 } 862 } 863 864 /* 865 * uvmpd_scan: scan the page queues and attempt to meet our targets. 866 * 867 * => called with pageq's locked 868 */ 869 870 void 871 uvmpd_scan(struct uvm_pmalloc *pma, struct uvm_constraint_range *constraint) 872 { 873 int free, inactive_shortage, swap_shortage, pages_freed; 874 struct vm_page *p, *nextpg; 875 struct rwlock *slock; 876 paddr_t paddr; 877 878 MUTEX_ASSERT_LOCKED(&uvm.pageqlock); 879 880 uvmexp.pdrevs++; /* counter */ 881 882 /* 883 * get current "free" page count 884 */ 885 free = uvmexp.free - BUFPAGES_DEFICIT; 886 887 #ifdef __HAVE_PMAP_COLLECT 888 /* 889 * swap out some processes if we are below our free target. 890 * we need to unlock the page queues for this. 891 */ 892 if (free < uvmexp.freetarg) { 893 uvmexp.pdswout++; 894 uvm_unlock_pageq(); 895 uvm_swapout_threads(); 896 uvm_lock_pageq(); 897 } 898 #endif 899 900 /* 901 * now we want to work on meeting our targets. first we work on our 902 * free target by converting inactive pages into free pages. then 903 * we work on meeting our inactive target by converting active pages 904 * to inactive ones. 905 */ 906 907 pages_freed = uvmexp.pdfreed; 908 (void) uvmpd_scan_inactive(pma, constraint, &uvm.page_inactive); 909 pages_freed = uvmexp.pdfreed - pages_freed; 910 911 /* 912 * we have done the scan to get free pages. now we work on meeting 913 * our inactive target. 914 */ 915 inactive_shortage = uvmexp.inactarg - uvmexp.inactive - BUFPAGES_INACT; 916 917 /* 918 * detect if we're not going to be able to page anything out 919 * until we free some swap resources from active pages. 920 */ 921 free = uvmexp.free - BUFPAGES_DEFICIT; 922 swap_shortage = 0; 923 if (free < uvmexp.freetarg && 924 uvmexp.swpginuse == uvmexp.swpages && 925 !uvm_swapisfull() && 926 pages_freed == 0) { 927 swap_shortage = uvmexp.freetarg - free; 928 } 929 930 for (p = TAILQ_FIRST(&uvm.page_active); 931 p != NULL && (inactive_shortage > 0 || swap_shortage > 0); 932 p = nextpg) { 933 nextpg = TAILQ_NEXT(p, pageq); 934 if (p->pg_flags & PG_BUSY) { 935 continue; 936 } 937 938 /* 939 * skip this page if it doesn't match the constraint. 940 */ 941 paddr = atop(VM_PAGE_TO_PHYS(p)); 942 if (paddr < constraint->ucr_low && 943 paddr >= constraint->ucr_high) 944 continue; 945 946 /* 947 * lock the page's owner. 948 */ 949 slock = uvmpd_trylockowner(p); 950 if (slock == NULL) { 951 continue; 952 } 953 954 /* 955 * skip this page if it's busy. 956 */ 957 if ((p->pg_flags & PG_BUSY) != 0) { 958 rw_exit(slock); 959 continue; 960 } 961 962 /* 963 * if there's a shortage of swap, free any swap allocated 964 * to this page so that other pages can be paged out. 965 */ 966 if (swap_shortage > 0) { 967 if ((p->pg_flags & PQ_ANON) && p->uanon->an_swslot) { 968 uvm_swap_free(p->uanon->an_swslot, 1); 969 p->uanon->an_swslot = 0; 970 atomic_clearbits_int(&p->pg_flags, PG_CLEAN); 971 swap_shortage--; 972 } 973 if (p->pg_flags & PQ_AOBJ) { 974 int slot = uao_set_swslot(p->uobject, 975 p->offset >> PAGE_SHIFT, 0); 976 if (slot) { 977 uvm_swap_free(slot, 1); 978 atomic_clearbits_int(&p->pg_flags, 979 PG_CLEAN); 980 swap_shortage--; 981 } 982 } 983 } 984 985 /* 986 * deactivate this page if there's a shortage of 987 * inactive pages. 988 */ 989 if (inactive_shortage > 0) { 990 pmap_page_protect(p, PROT_NONE); 991 /* no need to check wire_count as pg is "active" */ 992 uvm_pagedeactivate(p); 993 uvmexp.pddeact++; 994 inactive_shortage--; 995 } 996 997 /* 998 * we're done with this page. 999 */ 1000 rw_exit(slock); 1001 } 1002 } 1003 1004 #ifdef HIBERNATE 1005 1006 /* 1007 * uvmpd_drop: drop clean pages from list 1008 */ 1009 void 1010 uvmpd_drop(struct pglist *pglst) 1011 { 1012 struct vm_page *p, *nextpg; 1013 1014 for (p = TAILQ_FIRST(pglst); p != NULL; p = nextpg) { 1015 nextpg = TAILQ_NEXT(p, pageq); 1016 1017 if (p->pg_flags & PQ_ANON || p->uobject == NULL) 1018 continue; 1019 1020 if (p->pg_flags & PG_BUSY) 1021 continue; 1022 1023 if (p->pg_flags & PG_CLEAN) { 1024 struct uvm_object * uobj = p->uobject; 1025 1026 rw_enter(uobj->vmobjlock, RW_WRITE); 1027 uvm_lock_pageq(); 1028 /* 1029 * we now have the page queues locked. 1030 * the page is not busy. if the page is clean we 1031 * can free it now and continue. 1032 */ 1033 if (p->pg_flags & PG_CLEAN) { 1034 if (p->pg_flags & PQ_SWAPBACKED) { 1035 /* this page now lives only in swap */ 1036 atomic_inc_int(&uvmexp.swpgonly); 1037 } 1038 1039 /* zap all mappings with pmap_page_protect... */ 1040 pmap_page_protect(p, PROT_NONE); 1041 uvm_pagefree(p); 1042 } 1043 uvm_unlock_pageq(); 1044 rw_exit(uobj->vmobjlock); 1045 } 1046 } 1047 } 1048 1049 void 1050 uvmpd_hibernate(void) 1051 { 1052 uvmpd_drop(&uvm.page_inactive); 1053 uvmpd_drop(&uvm.page_active); 1054 } 1055 1056 #endif 1057