1 /* $OpenBSD: uvm_pdaemon.c,v 1.94 2021/12/15 12:53:53 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 void uvmpd_scan(void); 105 boolean_t uvmpd_scan_inactive(struct pglist *); 106 void uvmpd_tune(void); 107 void uvmpd_drop(struct pglist *); 108 109 /* 110 * uvm_wait: wait (sleep) for the page daemon to free some pages 111 * 112 * => should be called with all locks released 113 * => should _not_ be called by the page daemon (to avoid deadlock) 114 */ 115 116 void 117 uvm_wait(const char *wmsg) 118 { 119 uint64_t timo = INFSLP; 120 121 #ifdef DIAGNOSTIC 122 if (curproc == &proc0) 123 panic("%s: cannot sleep for memory during boot", __func__); 124 #endif 125 126 /* check for page daemon going to sleep (waiting for itself) */ 127 if (curproc == uvm.pagedaemon_proc) { 128 printf("uvm_wait emergency bufbackoff\n"); 129 if (bufbackoff(NULL, 4) == 0) 130 return; 131 /* 132 * now we have a problem: the pagedaemon wants to go to 133 * sleep until it frees more memory. but how can it 134 * free more memory if it is asleep? that is a deadlock. 135 * we have two options: 136 * [1] panic now 137 * [2] put a timeout on the sleep, thus causing the 138 * pagedaemon to only pause (rather than sleep forever) 139 * 140 * note that option [2] will only help us if we get lucky 141 * and some other process on the system breaks the deadlock 142 * by exiting or freeing memory (thus allowing the pagedaemon 143 * to continue). for now we panic if DEBUG is defined, 144 * otherwise we hope for the best with option [2] (better 145 * yet, this should never happen in the first place!). 146 */ 147 148 printf("pagedaemon: deadlock detected!\n"); 149 timo = MSEC_TO_NSEC(125); /* set timeout */ 150 #if defined(DEBUG) 151 /* DEBUG: panic so we can debug it */ 152 panic("pagedaemon deadlock"); 153 #endif 154 } 155 156 uvm_lock_fpageq(); 157 wakeup(&uvm.pagedaemon); /* wake the daemon! */ 158 msleep_nsec(&uvmexp.free, &uvm.fpageqlock, PVM | PNORELOCK, wmsg, timo); 159 } 160 161 /* 162 * uvmpd_tune: tune paging parameters 163 * 164 * => called whenever memory is added to (or removed from?) the system 165 * => caller must call with page queues locked 166 */ 167 168 void 169 uvmpd_tune(void) 170 { 171 172 uvmexp.freemin = uvmexp.npages / 30; 173 174 /* between 16k and 512k */ 175 /* XXX: what are these values good for? */ 176 uvmexp.freemin = max(uvmexp.freemin, (16*1024) >> PAGE_SHIFT); 177 #if 0 178 uvmexp.freemin = min(uvmexp.freemin, (512*1024) >> PAGE_SHIFT); 179 #endif 180 181 /* Make sure there's always a user page free. */ 182 if (uvmexp.freemin < uvmexp.reserve_kernel + 1) 183 uvmexp.freemin = uvmexp.reserve_kernel + 1; 184 185 uvmexp.freetarg = (uvmexp.freemin * 4) / 3; 186 if (uvmexp.freetarg <= uvmexp.freemin) 187 uvmexp.freetarg = uvmexp.freemin + 1; 188 189 /* uvmexp.inactarg: computed in main daemon loop */ 190 191 uvmexp.wiredmax = uvmexp.npages / 3; 192 } 193 194 /* 195 * Indicate to the page daemon that a nowait call failed and it should 196 * recover at least some memory in the most restricted region (assumed 197 * to be dma_constraint). 198 */ 199 volatile int uvm_nowait_failed; 200 201 /* 202 * uvm_pageout: the main loop for the pagedaemon 203 */ 204 void 205 uvm_pageout(void *arg) 206 { 207 struct uvm_constraint_range constraint; 208 struct uvm_pmalloc *pma; 209 int npages = 0; 210 211 /* ensure correct priority and set paging parameters... */ 212 uvm.pagedaemon_proc = curproc; 213 (void) spl0(); 214 uvm_lock_pageq(); 215 npages = uvmexp.npages; 216 uvmpd_tune(); 217 uvm_unlock_pageq(); 218 219 for (;;) { 220 long size; 221 222 uvm_lock_fpageq(); 223 if (!uvm_nowait_failed && TAILQ_EMPTY(&uvm.pmr_control.allocs)) { 224 msleep_nsec(&uvm.pagedaemon, &uvm.fpageqlock, PVM, 225 "pgdaemon", INFSLP); 226 uvmexp.pdwoke++; 227 } 228 229 if ((pma = TAILQ_FIRST(&uvm.pmr_control.allocs)) != NULL) { 230 pma->pm_flags |= UVM_PMA_BUSY; 231 constraint = pma->pm_constraint; 232 } else { 233 if (uvm_nowait_failed) { 234 /* 235 * XXX realisticly, this is what our 236 * nowait callers probably care about 237 */ 238 constraint = dma_constraint; 239 uvm_nowait_failed = 0; 240 } else 241 constraint = no_constraint; 242 } 243 244 uvm_unlock_fpageq(); 245 246 /* now lock page queues and recompute inactive count */ 247 uvm_lock_pageq(); 248 if (npages != uvmexp.npages) { /* check for new pages? */ 249 npages = uvmexp.npages; 250 uvmpd_tune(); 251 } 252 253 uvmexp.inactarg = (uvmexp.active + uvmexp.inactive) / 3; 254 if (uvmexp.inactarg <= uvmexp.freetarg) { 255 uvmexp.inactarg = uvmexp.freetarg + 1; 256 } 257 258 /* Reclaim pages from the buffer cache if possible. */ 259 size = 0; 260 if (pma != NULL) 261 size += pma->pm_size >> PAGE_SHIFT; 262 if (uvmexp.free - BUFPAGES_DEFICIT < uvmexp.freetarg) 263 size += uvmexp.freetarg - (uvmexp.free - 264 BUFPAGES_DEFICIT); 265 if (size == 0) 266 size = 16; /* XXX */ 267 uvm_unlock_pageq(); 268 (void) bufbackoff(&constraint, size * 2); 269 #if NDRM > 0 270 drmbackoff(size * 2); 271 #endif 272 uvm_lock_pageq(); 273 274 /* Scan if needed to meet our targets. */ 275 if (pma != NULL || 276 ((uvmexp.free - BUFPAGES_DEFICIT) < uvmexp.freetarg) || 277 ((uvmexp.inactive + BUFPAGES_INACT) < uvmexp.inactarg)) { 278 uvmpd_scan(); 279 } 280 281 /* 282 * if there's any free memory to be had, 283 * wake up any waiters. 284 */ 285 uvm_lock_fpageq(); 286 if (uvmexp.free > uvmexp.reserve_kernel || 287 uvmexp.paging == 0) { 288 wakeup(&uvmexp.free); 289 } 290 291 if (pma != NULL) { 292 /* 293 * XXX If UVM_PMA_FREED isn't set, no pages 294 * were freed. Should we set UVM_PMA_FAIL in 295 * that case? 296 */ 297 pma->pm_flags &= ~UVM_PMA_BUSY; 298 if (pma->pm_flags & UVM_PMA_FREED) { 299 pma->pm_flags &= ~UVM_PMA_LINKED; 300 TAILQ_REMOVE(&uvm.pmr_control.allocs, pma, 301 pmq); 302 wakeup(pma); 303 } 304 } 305 uvm_unlock_fpageq(); 306 307 /* scan done. unlock page queues (only lock we are holding) */ 308 uvm_unlock_pageq(); 309 310 sched_pause(yield); 311 } 312 /*NOTREACHED*/ 313 } 314 315 316 /* 317 * uvm_aiodone_daemon: main loop for the aiodone daemon. 318 */ 319 void 320 uvm_aiodone_daemon(void *arg) 321 { 322 int s, free; 323 struct buf *bp, *nbp; 324 325 uvm.aiodoned_proc = curproc; 326 327 for (;;) { 328 /* 329 * Check for done aio structures. If we've got structures to 330 * process, do so. Otherwise sleep while avoiding races. 331 */ 332 mtx_enter(&uvm.aiodoned_lock); 333 while ((bp = TAILQ_FIRST(&uvm.aio_done)) == NULL) 334 msleep_nsec(&uvm.aiodoned, &uvm.aiodoned_lock, 335 PVM, "aiodoned", INFSLP); 336 /* Take the list for ourselves. */ 337 TAILQ_INIT(&uvm.aio_done); 338 mtx_leave(&uvm.aiodoned_lock); 339 340 /* process each i/o that's done. */ 341 free = uvmexp.free; 342 while (bp != NULL) { 343 if (bp->b_flags & B_PDAEMON) { 344 uvmexp.paging -= bp->b_bufsize >> PAGE_SHIFT; 345 } 346 nbp = TAILQ_NEXT(bp, b_freelist); 347 s = splbio(); /* b_iodone must by called at splbio */ 348 (*bp->b_iodone)(bp); 349 splx(s); 350 bp = nbp; 351 352 sched_pause(yield); 353 } 354 uvm_lock_fpageq(); 355 wakeup(free <= uvmexp.reserve_kernel ? &uvm.pagedaemon : 356 &uvmexp.free); 357 uvm_unlock_fpageq(); 358 } 359 } 360 361 362 363 /* 364 * uvmpd_scan_inactive: scan an inactive list for pages to clean or free. 365 * 366 * => called with page queues locked 367 * => we work on meeting our free target by converting inactive pages 368 * into free pages. 369 * => we handle the building of swap-backed clusters 370 * => we return TRUE if we are exiting because we met our target 371 */ 372 373 boolean_t 374 uvmpd_scan_inactive(struct pglist *pglst) 375 { 376 boolean_t retval = FALSE; /* assume we haven't hit target */ 377 int free, result; 378 struct vm_page *p, *nextpg; 379 struct uvm_object *uobj; 380 struct vm_page *pps[MAXBSIZE >> PAGE_SHIFT], **ppsp; 381 int npages; 382 struct vm_page *swpps[MAXBSIZE >> PAGE_SHIFT]; /* XXX: see below */ 383 int swnpages, swcpages; /* XXX: see below */ 384 int swslot; 385 struct vm_anon *anon; 386 boolean_t swap_backed; 387 vaddr_t start; 388 int dirtyreacts; 389 390 /* 391 * note: we currently keep swap-backed pages on a separate inactive 392 * list from object-backed pages. however, merging the two lists 393 * back together again hasn't been ruled out. thus, we keep our 394 * swap cluster in "swpps" rather than in pps (allows us to mix 395 * clustering types in the event of a mixed inactive queue). 396 */ 397 /* 398 * swslot is non-zero if we are building a swap cluster. we want 399 * to stay in the loop while we have a page to scan or we have 400 * a swap-cluster to build. 401 */ 402 swslot = 0; 403 swnpages = swcpages = 0; 404 free = 0; 405 dirtyreacts = 0; 406 407 for (p = TAILQ_FIRST(pglst); p != NULL || swslot != 0; p = nextpg) { 408 /* 409 * note that p can be NULL iff we have traversed the whole 410 * list and need to do one final swap-backed clustered pageout. 411 */ 412 uobj = NULL; 413 anon = NULL; 414 415 if (p) { 416 /* 417 * update our copy of "free" and see if we've met 418 * our target 419 */ 420 free = uvmexp.free - BUFPAGES_DEFICIT; 421 422 if (free + uvmexp.paging >= uvmexp.freetarg << 2 || 423 dirtyreacts == UVMPD_NUMDIRTYREACTS) { 424 retval = TRUE; 425 426 if (swslot == 0) { 427 /* exit now if no swap-i/o pending */ 428 break; 429 } 430 431 /* set p to null to signal final swap i/o */ 432 p = NULL; 433 } 434 } 435 436 if (p) { /* if (we have a new page to consider) */ 437 /* 438 * we are below target and have a new page to consider. 439 */ 440 uvmexp.pdscans++; 441 nextpg = TAILQ_NEXT(p, pageq); 442 443 if (p->pg_flags & PQ_ANON) { 444 anon = p->uanon; 445 KASSERT(anon != NULL); 446 if (rw_enter(anon->an_lock, 447 RW_WRITE|RW_NOSLEEP)) { 448 /* lock failed, skip this page */ 449 continue; 450 } 451 /* 452 * move referenced pages back to active queue 453 * and skip to next page. 454 */ 455 if (pmap_is_referenced(p)) { 456 uvm_pageactivate(p); 457 rw_exit(anon->an_lock); 458 uvmexp.pdreact++; 459 continue; 460 } 461 if (p->pg_flags & PG_BUSY) { 462 rw_exit(anon->an_lock); 463 uvmexp.pdbusy++; 464 /* someone else owns page, skip it */ 465 continue; 466 } 467 uvmexp.pdanscan++; 468 } else { 469 uobj = p->uobject; 470 KASSERT(uobj != NULL); 471 if (rw_enter(uobj->vmobjlock, 472 RW_WRITE|RW_NOSLEEP)) { 473 /* lock failed, skip this page */ 474 continue; 475 } 476 /* 477 * move referenced pages back to active queue 478 * and skip to next page. 479 */ 480 if (pmap_is_referenced(p)) { 481 uvm_pageactivate(p); 482 rw_exit(uobj->vmobjlock); 483 uvmexp.pdreact++; 484 continue; 485 } 486 if (p->pg_flags & PG_BUSY) { 487 rw_exit(uobj->vmobjlock); 488 uvmexp.pdbusy++; 489 /* someone else owns page, skip it */ 490 continue; 491 } 492 uvmexp.pdobscan++; 493 } 494 495 /* 496 * we now have the page queues locked. 497 * the page is not busy. if the page is clean we 498 * can free it now and continue. 499 */ 500 if (p->pg_flags & PG_CLEAN) { 501 if (p->pg_flags & PQ_SWAPBACKED) { 502 /* this page now lives only in swap */ 503 atomic_inc_int(&uvmexp.swpgonly); 504 } 505 506 /* zap all mappings with pmap_page_protect... */ 507 pmap_page_protect(p, PROT_NONE); 508 uvm_pagefree(p); 509 uvmexp.pdfreed++; 510 511 if (anon) { 512 513 /* 514 * an anonymous page can only be clean 515 * if it has backing store assigned. 516 */ 517 518 KASSERT(anon->an_swslot != 0); 519 520 /* remove from object */ 521 anon->an_page = NULL; 522 rw_exit(anon->an_lock); 523 } else { 524 rw_exit(uobj->vmobjlock); 525 } 526 continue; 527 } 528 529 /* 530 * this page is dirty, skip it if we'll have met our 531 * free target when all the current pageouts complete. 532 */ 533 if (free + uvmexp.paging > uvmexp.freetarg << 2) { 534 if (anon) { 535 rw_exit(anon->an_lock); 536 } else { 537 rw_exit(uobj->vmobjlock); 538 } 539 continue; 540 } 541 542 /* 543 * this page is dirty, but we can't page it out 544 * since all pages in swap are only in swap. 545 * reactivate it so that we eventually cycle 546 * all pages thru the inactive queue. 547 */ 548 if ((p->pg_flags & PQ_SWAPBACKED) && uvm_swapisfull()) { 549 dirtyreacts++; 550 uvm_pageactivate(p); 551 if (anon) { 552 rw_exit(anon->an_lock); 553 } else { 554 rw_exit(uobj->vmobjlock); 555 } 556 continue; 557 } 558 559 /* 560 * if the page is swap-backed and dirty and swap space 561 * is full, free any swap allocated to the page 562 * so that other pages can be paged out. 563 */ 564 KASSERT(uvmexp.swpginuse <= uvmexp.swpages); 565 if ((p->pg_flags & PQ_SWAPBACKED) && 566 uvmexp.swpginuse == uvmexp.swpages) { 567 568 if ((p->pg_flags & PQ_ANON) && 569 p->uanon->an_swslot) { 570 uvm_swap_free(p->uanon->an_swslot, 1); 571 p->uanon->an_swslot = 0; 572 } 573 if (p->pg_flags & PQ_AOBJ) { 574 uao_dropswap(p->uobject, 575 p->offset >> PAGE_SHIFT); 576 } 577 } 578 579 /* 580 * the page we are looking at is dirty. we must 581 * clean it before it can be freed. to do this we 582 * first mark the page busy so that no one else will 583 * touch the page. we write protect all the mappings 584 * of the page so that no one touches it while it is 585 * in I/O. 586 */ 587 588 swap_backed = ((p->pg_flags & PQ_SWAPBACKED) != 0); 589 atomic_setbits_int(&p->pg_flags, PG_BUSY); 590 UVM_PAGE_OWN(p, "scan_inactive"); 591 pmap_page_protect(p, PROT_READ); 592 uvmexp.pgswapout++; 593 594 /* 595 * for swap-backed pages we need to (re)allocate 596 * swap space. 597 */ 598 if (swap_backed) { 599 /* free old swap slot (if any) */ 600 if (anon) { 601 if (anon->an_swslot) { 602 uvm_swap_free(anon->an_swslot, 603 1); 604 anon->an_swslot = 0; 605 } 606 } else { 607 uao_dropswap(uobj, 608 p->offset >> PAGE_SHIFT); 609 } 610 611 /* start new cluster (if necessary) */ 612 if (swslot == 0) { 613 swnpages = MAXBSIZE >> PAGE_SHIFT; 614 swslot = uvm_swap_alloc(&swnpages, 615 TRUE); 616 if (swslot == 0) { 617 /* no swap? give up! */ 618 atomic_clearbits_int( 619 &p->pg_flags, 620 PG_BUSY); 621 UVM_PAGE_OWN(p, NULL); 622 if (anon) 623 rw_exit(anon->an_lock); 624 else 625 rw_exit( 626 uobj->vmobjlock); 627 continue; 628 } 629 swcpages = 0; /* cluster is empty */ 630 } 631 632 /* add block to cluster */ 633 swpps[swcpages] = p; 634 if (anon) 635 anon->an_swslot = swslot + swcpages; 636 else 637 uao_set_swslot(uobj, 638 p->offset >> PAGE_SHIFT, 639 swslot + swcpages); 640 swcpages++; 641 } 642 } else { 643 /* if p == NULL we must be doing a last swap i/o */ 644 swap_backed = TRUE; 645 } 646 647 /* 648 * now consider doing the pageout. 649 * 650 * for swap-backed pages, we do the pageout if we have either 651 * filled the cluster (in which case (swnpages == swcpages) or 652 * run out of pages (p == NULL). 653 * 654 * for object pages, we always do the pageout. 655 */ 656 if (swap_backed) { 657 if (p) { /* if we just added a page to cluster */ 658 if (anon) 659 rw_exit(anon->an_lock); 660 else 661 rw_exit(uobj->vmobjlock); 662 663 /* cluster not full yet? */ 664 if (swcpages < swnpages) 665 continue; 666 } 667 668 /* starting I/O now... set up for it */ 669 npages = swcpages; 670 ppsp = swpps; 671 /* for swap-backed pages only */ 672 start = (vaddr_t) swslot; 673 674 /* if this is final pageout we could have a few 675 * extra swap blocks */ 676 if (swcpages < swnpages) { 677 uvm_swap_free(swslot + swcpages, 678 (swnpages - swcpages)); 679 } 680 } else { 681 /* normal object pageout */ 682 ppsp = pps; 683 npages = sizeof(pps) / sizeof(struct vm_page *); 684 /* not looked at because PGO_ALLPAGES is set */ 685 start = 0; 686 } 687 688 /* 689 * now do the pageout. 690 * 691 * for swap_backed pages we have already built the cluster. 692 * for !swap_backed pages, uvm_pager_put will call the object's 693 * "make put cluster" function to build a cluster on our behalf. 694 * 695 * we pass the PGO_PDFREECLUST flag to uvm_pager_put to instruct 696 * it to free the cluster pages for us on a successful I/O (it 697 * always does this for un-successful I/O requests). this 698 * allows us to do clustered pageout without having to deal 699 * with cluster pages at this level. 700 * 701 * note locking semantics of uvm_pager_put with PGO_PDFREECLUST: 702 * IN: locked: page queues 703 * OUT: locked: 704 * !locked: pageqs 705 */ 706 707 uvmexp.pdpageouts++; 708 result = uvm_pager_put(swap_backed ? NULL : uobj, p, 709 &ppsp, &npages, PGO_ALLPAGES|PGO_PDFREECLUST, start, 0); 710 711 /* 712 * if we did i/o to swap, zero swslot to indicate that we are 713 * no longer building a swap-backed cluster. 714 */ 715 716 if (swap_backed) 717 swslot = 0; /* done with this cluster */ 718 719 /* 720 * first, we check for VM_PAGER_PEND which means that the 721 * async I/O is in progress and the async I/O done routine 722 * will clean up after us. in this case we move on to the 723 * next page. 724 * 725 * there is a very remote chance that the pending async i/o can 726 * finish _before_ we get here. if that happens, our page "p" 727 * may no longer be on the inactive queue. so we verify this 728 * when determining the next page (starting over at the head if 729 * we've lost our inactive page). 730 */ 731 732 if (result == VM_PAGER_PEND) { 733 uvmexp.paging += npages; 734 uvm_lock_pageq(); 735 uvmexp.pdpending++; 736 if (p) { 737 if (p->pg_flags & PQ_INACTIVE) 738 nextpg = TAILQ_NEXT(p, pageq); 739 else 740 nextpg = TAILQ_FIRST(pglst); 741 } else { 742 nextpg = NULL; 743 } 744 continue; 745 } 746 747 /* clean up "p" if we have one */ 748 if (p) { 749 /* 750 * the I/O request to "p" is done and uvm_pager_put 751 * has freed any cluster pages it may have allocated 752 * during I/O. all that is left for us to do is 753 * clean up page "p" (which is still PG_BUSY). 754 * 755 * our result could be one of the following: 756 * VM_PAGER_OK: successful pageout 757 * 758 * VM_PAGER_AGAIN: tmp resource shortage, we skip 759 * to next page 760 * VM_PAGER_{FAIL,ERROR,BAD}: an error. we 761 * "reactivate" page to get it out of the way (it 762 * will eventually drift back into the inactive 763 * queue for a retry). 764 * VM_PAGER_UNLOCK: should never see this as it is 765 * only valid for "get" operations 766 */ 767 768 /* relock p's object: page queues not lock yet, so 769 * no need for "try" */ 770 771 /* !swap_backed case: already locked... */ 772 if (swap_backed) { 773 if (anon) 774 rw_enter(anon->an_lock, RW_WRITE); 775 else 776 rw_enter(uobj->vmobjlock, RW_WRITE); 777 } 778 779 #ifdef DIAGNOSTIC 780 if (result == VM_PAGER_UNLOCK) 781 panic("pagedaemon: pageout returned " 782 "invalid 'unlock' code"); 783 #endif 784 785 /* handle PG_WANTED now */ 786 if (p->pg_flags & PG_WANTED) 787 wakeup(p); 788 789 atomic_clearbits_int(&p->pg_flags, PG_BUSY|PG_WANTED); 790 UVM_PAGE_OWN(p, NULL); 791 792 /* released during I/O? Can only happen for anons */ 793 if (p->pg_flags & PG_RELEASED) { 794 KASSERT(anon != NULL); 795 /* 796 * remove page so we can get nextpg, 797 * also zero out anon so we don't use 798 * it after the free. 799 */ 800 anon->an_page = NULL; 801 p->uanon = NULL; 802 803 rw_exit(anon->an_lock); 804 uvm_anfree(anon); /* kills anon */ 805 pmap_page_protect(p, PROT_NONE); 806 anon = NULL; 807 uvm_lock_pageq(); 808 nextpg = TAILQ_NEXT(p, pageq); 809 /* free released page */ 810 uvm_pagefree(p); 811 } else { /* page was not released during I/O */ 812 uvm_lock_pageq(); 813 nextpg = TAILQ_NEXT(p, pageq); 814 if (result != VM_PAGER_OK) { 815 /* pageout was a failure... */ 816 if (result != VM_PAGER_AGAIN) 817 uvm_pageactivate(p); 818 pmap_clear_reference(p); 819 /* XXXCDC: if (swap_backed) FREE p's 820 * swap block? */ 821 } else { 822 /* pageout was a success... */ 823 pmap_clear_reference(p); 824 pmap_clear_modify(p); 825 atomic_setbits_int(&p->pg_flags, 826 PG_CLEAN); 827 } 828 } 829 830 /* 831 * drop object lock (if there is an object left). do 832 * a safety check of nextpg to make sure it is on the 833 * inactive queue (it should be since PG_BUSY pages on 834 * the inactive queue can't be re-queued [note: not 835 * true for active queue]). 836 */ 837 if (anon) 838 rw_exit(anon->an_lock); 839 else if (uobj) 840 rw_exit(uobj->vmobjlock); 841 842 if (nextpg && (nextpg->pg_flags & PQ_INACTIVE) == 0) { 843 nextpg = TAILQ_FIRST(pglst); /* reload! */ 844 } 845 } else { 846 /* 847 * if p is null in this loop, make sure it stays null 848 * in the next loop. 849 */ 850 nextpg = NULL; 851 852 /* 853 * lock page queues here just so they're always locked 854 * at the end of the loop. 855 */ 856 uvm_lock_pageq(); 857 } 858 } 859 return (retval); 860 } 861 862 /* 863 * uvmpd_scan: scan the page queues and attempt to meet our targets. 864 * 865 * => called with pageq's locked 866 */ 867 868 void 869 uvmpd_scan(void) 870 { 871 int free, inactive_shortage, swap_shortage, pages_freed; 872 struct vm_page *p, *nextpg; 873 struct uvm_object *uobj; 874 boolean_t got_it; 875 876 MUTEX_ASSERT_LOCKED(&uvm.pageqlock); 877 878 uvmexp.pdrevs++; /* counter */ 879 uobj = NULL; 880 881 /* 882 * get current "free" page count 883 */ 884 free = uvmexp.free - BUFPAGES_DEFICIT; 885 886 #ifndef __SWAP_BROKEN 887 /* 888 * swap out some processes if we are below our free target. 889 * we need to unlock the page queues for this. 890 */ 891 if (free < uvmexp.freetarg) { 892 uvmexp.pdswout++; 893 uvm_unlock_pageq(); 894 uvm_swapout_threads(); 895 uvm_lock_pageq(); 896 } 897 #endif 898 899 /* 900 * now we want to work on meeting our targets. first we work on our 901 * free target by converting inactive pages into free pages. then 902 * we work on meeting our inactive target by converting active pages 903 * to inactive ones. 904 */ 905 906 /* 907 * alternate starting queue between swap and object based on the 908 * low bit of uvmexp.pdrevs (which we bump by one each call). 909 */ 910 got_it = FALSE; 911 pages_freed = uvmexp.pdfreed; /* XXX - int */ 912 if ((uvmexp.pdrevs & 1) != 0 && uvmexp.nswapdev != 0) 913 got_it = uvmpd_scan_inactive(&uvm.page_inactive_swp); 914 if (!got_it) 915 got_it = uvmpd_scan_inactive(&uvm.page_inactive_obj); 916 if (!got_it && (uvmexp.pdrevs & 1) == 0 && uvmexp.nswapdev != 0) 917 (void) uvmpd_scan_inactive(&uvm.page_inactive_swp); 918 pages_freed = uvmexp.pdfreed - pages_freed; 919 920 /* 921 * we have done the scan to get free pages. now we work on meeting 922 * our inactive target. 923 */ 924 inactive_shortage = uvmexp.inactarg - uvmexp.inactive - BUFPAGES_INACT; 925 926 /* 927 * detect if we're not going to be able to page anything out 928 * until we free some swap resources from active pages. 929 */ 930 swap_shortage = 0; 931 if (uvmexp.free < uvmexp.freetarg && 932 uvmexp.swpginuse == uvmexp.swpages && 933 !uvm_swapisfull() && 934 pages_freed == 0) { 935 swap_shortage = uvmexp.freetarg - uvmexp.free; 936 } 937 938 for (p = TAILQ_FIRST(&uvm.page_active); 939 p != NULL && (inactive_shortage > 0 || swap_shortage > 0); 940 p = nextpg) { 941 nextpg = TAILQ_NEXT(p, pageq); 942 943 /* skip this page if it's busy. */ 944 if (p->pg_flags & PG_BUSY) 945 continue; 946 947 if (p->pg_flags & PQ_ANON) { 948 KASSERT(p->uanon != NULL); 949 if (rw_enter(p->uanon->an_lock, RW_WRITE|RW_NOSLEEP)) 950 continue; 951 } else { 952 KASSERT(p->uobject != NULL); 953 if (rw_enter(p->uobject->vmobjlock, 954 RW_WRITE|RW_NOSLEEP)) 955 continue; 956 } 957 958 /* 959 * if there's a shortage of swap, free any swap allocated 960 * to this page so that other pages can be paged out. 961 */ 962 if (swap_shortage > 0) { 963 if ((p->pg_flags & PQ_ANON) && p->uanon->an_swslot) { 964 uvm_swap_free(p->uanon->an_swslot, 1); 965 p->uanon->an_swslot = 0; 966 atomic_clearbits_int(&p->pg_flags, PG_CLEAN); 967 swap_shortage--; 968 } 969 if (p->pg_flags & PQ_AOBJ) { 970 int slot = uao_set_swslot(p->uobject, 971 p->offset >> PAGE_SHIFT, 0); 972 if (slot) { 973 uvm_swap_free(slot, 1); 974 atomic_clearbits_int(&p->pg_flags, 975 PG_CLEAN); 976 swap_shortage--; 977 } 978 } 979 } 980 981 /* 982 * deactivate this page if there's a shortage of 983 * inactive pages. 984 */ 985 if (inactive_shortage > 0) { 986 pmap_page_protect(p, PROT_NONE); 987 /* no need to check wire_count as pg is "active" */ 988 uvm_pagedeactivate(p); 989 uvmexp.pddeact++; 990 inactive_shortage--; 991 } 992 if (p->pg_flags & PQ_ANON) 993 rw_exit(p->uanon->an_lock); 994 else 995 rw_exit(p->uobject->vmobjlock); 996 } 997 } 998 999 #ifdef HIBERNATE 1000 1001 /* 1002 * uvmpd_drop: drop clean pages from list 1003 */ 1004 void 1005 uvmpd_drop(struct pglist *pglst) 1006 { 1007 struct vm_page *p, *nextpg; 1008 1009 for (p = TAILQ_FIRST(pglst); p != NULL; p = nextpg) { 1010 nextpg = TAILQ_NEXT(p, pageq); 1011 1012 if (p->pg_flags & PQ_ANON || p->uobject == NULL) 1013 continue; 1014 1015 if (p->pg_flags & PG_BUSY) 1016 continue; 1017 1018 if (p->pg_flags & PG_CLEAN) { 1019 struct uvm_object * uobj = p->uobject; 1020 1021 rw_enter(uobj->vmobjlock, RW_WRITE); 1022 uvm_lock_pageq(); 1023 /* 1024 * we now have the page queues locked. 1025 * the page is not busy. if the page is clean we 1026 * can free it now and continue. 1027 */ 1028 if (p->pg_flags & PG_CLEAN) { 1029 if (p->pg_flags & PQ_SWAPBACKED) { 1030 /* this page now lives only in swap */ 1031 atomic_inc_int(&uvmexp.swpgonly); 1032 } 1033 1034 /* zap all mappings with pmap_page_protect... */ 1035 pmap_page_protect(p, PROT_NONE); 1036 uvm_pagefree(p); 1037 } 1038 uvm_unlock_pageq(); 1039 rw_exit(uobj->vmobjlock); 1040 } 1041 } 1042 } 1043 1044 void 1045 uvmpd_hibernate(void) 1046 { 1047 uvmpd_drop(&uvm.page_inactive_swp); 1048 uvmpd_drop(&uvm.page_inactive_obj); 1049 uvmpd_drop(&uvm.page_active); 1050 } 1051 1052 #endif 1053