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