1 /* $OpenBSD: uvm_pdaemon.c,v 1.101 2022/06/28 19:31:30 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(struct uvm_pmalloc *); 105 boolean_t uvmpd_scan_inactive(struct uvm_pmalloc *, 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(pma); 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 uvm_pmalloc *pma, 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[SWCLUSTPAGES], **ppsp; 389 int npages; 390 struct vm_page *swpps[SWCLUSTPAGES]; /* 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 p = NULL; 408 409 /* Start with the first page on the list that fit in pma's ranges */ 410 if (pma != NULL) { 411 paddr_t paddr; 412 413 TAILQ_FOREACH(p, pglst, pageq) { 414 paddr = atop(VM_PAGE_TO_PHYS(p)); 415 if (paddr >= pma->pm_constraint.ucr_low && 416 paddr < pma->pm_constraint.ucr_high) 417 break; 418 } 419 420 } 421 422 if (p == NULL) { 423 p = TAILQ_FIRST(pglst); 424 pma = NULL; 425 } 426 427 for (; p != NULL || swslot != 0; p = nextpg) { 428 /* 429 * note that p can be NULL iff we have traversed the whole 430 * list and need to do one final swap-backed clustered pageout. 431 */ 432 uobj = NULL; 433 anon = NULL; 434 435 if (p) { 436 /* 437 * update our copy of "free" and see if we've met 438 * our target 439 */ 440 free = uvmexp.free - BUFPAGES_DEFICIT; 441 if (((pma == NULL || (pma->pm_flags & UVM_PMA_FREED)) && 442 (free + uvmexp.paging >= uvmexp.freetarg << 2)) || 443 dirtyreacts == UVMPD_NUMDIRTYREACTS) { 444 retval = TRUE; 445 446 if (swslot == 0) { 447 /* exit now if no swap-i/o pending */ 448 break; 449 } 450 451 /* set p to null to signal final swap i/o */ 452 p = NULL; 453 } 454 } 455 456 if (p) { /* if (we have a new page to consider) */ 457 /* 458 * we are below target and have a new page to consider. 459 */ 460 uvmexp.pdscans++; 461 nextpg = TAILQ_NEXT(p, pageq); 462 463 if (p->pg_flags & PQ_ANON) { 464 anon = p->uanon; 465 KASSERT(anon != NULL); 466 if (rw_enter(anon->an_lock, 467 RW_WRITE|RW_NOSLEEP)) { 468 /* lock failed, skip this page */ 469 continue; 470 } 471 /* 472 * move referenced pages back to active queue 473 * and skip to next page. 474 */ 475 if (pmap_is_referenced(p)) { 476 uvm_pageactivate(p); 477 rw_exit(anon->an_lock); 478 uvmexp.pdreact++; 479 continue; 480 } 481 if (p->pg_flags & PG_BUSY) { 482 rw_exit(anon->an_lock); 483 uvmexp.pdbusy++; 484 /* someone else owns page, skip it */ 485 continue; 486 } 487 uvmexp.pdanscan++; 488 } else { 489 uobj = p->uobject; 490 KASSERT(uobj != NULL); 491 if (rw_enter(uobj->vmobjlock, 492 RW_WRITE|RW_NOSLEEP)) { 493 /* lock failed, skip this page */ 494 continue; 495 } 496 /* 497 * move referenced pages back to active queue 498 * and skip to next page. 499 */ 500 if (pmap_is_referenced(p)) { 501 uvm_pageactivate(p); 502 rw_exit(uobj->vmobjlock); 503 uvmexp.pdreact++; 504 continue; 505 } 506 if (p->pg_flags & PG_BUSY) { 507 rw_exit(uobj->vmobjlock); 508 uvmexp.pdbusy++; 509 /* someone else owns page, skip it */ 510 continue; 511 } 512 uvmexp.pdobscan++; 513 } 514 515 /* 516 * we now have the page queues locked. 517 * the page is not busy. if the page is clean we 518 * can free it now and continue. 519 */ 520 if (p->pg_flags & PG_CLEAN) { 521 if (p->pg_flags & PQ_SWAPBACKED) { 522 /* this page now lives only in swap */ 523 atomic_inc_int(&uvmexp.swpgonly); 524 } 525 526 /* zap all mappings with pmap_page_protect... */ 527 pmap_page_protect(p, PROT_NONE); 528 uvm_pagefree(p); 529 uvmexp.pdfreed++; 530 531 if (anon) { 532 533 /* 534 * an anonymous page can only be clean 535 * if it has backing store assigned. 536 */ 537 538 KASSERT(anon->an_swslot != 0); 539 540 /* remove from object */ 541 anon->an_page = NULL; 542 rw_exit(anon->an_lock); 543 } else { 544 rw_exit(uobj->vmobjlock); 545 } 546 continue; 547 } 548 549 /* 550 * this page is dirty, skip it if we'll have met our 551 * free target when all the current pageouts complete. 552 */ 553 if ((pma == NULL || (pma->pm_flags & UVM_PMA_FREED)) && 554 (free + uvmexp.paging > uvmexp.freetarg << 2)) { 555 if (anon) { 556 rw_exit(anon->an_lock); 557 } else { 558 rw_exit(uobj->vmobjlock); 559 } 560 continue; 561 } 562 563 /* 564 * this page is dirty, but we can't page it out 565 * since all pages in swap are only in swap. 566 * reactivate it so that we eventually cycle 567 * all pages thru the inactive queue. 568 */ 569 if ((p->pg_flags & PQ_SWAPBACKED) && uvm_swapisfull()) { 570 dirtyreacts++; 571 uvm_pageactivate(p); 572 if (anon) { 573 rw_exit(anon->an_lock); 574 } else { 575 rw_exit(uobj->vmobjlock); 576 } 577 continue; 578 } 579 580 /* 581 * if the page is swap-backed and dirty and swap space 582 * is full, free any swap allocated to the page 583 * so that other pages can be paged out. 584 */ 585 KASSERT(uvmexp.swpginuse <= uvmexp.swpages); 586 if ((p->pg_flags & PQ_SWAPBACKED) && 587 uvmexp.swpginuse == uvmexp.swpages) { 588 589 if ((p->pg_flags & PQ_ANON) && 590 p->uanon->an_swslot) { 591 uvm_swap_free(p->uanon->an_swslot, 1); 592 p->uanon->an_swslot = 0; 593 } 594 if (p->pg_flags & PQ_AOBJ) { 595 uao_dropswap(p->uobject, 596 p->offset >> PAGE_SHIFT); 597 } 598 } 599 600 /* 601 * the page we are looking at is dirty. we must 602 * clean it before it can be freed. to do this we 603 * first mark the page busy so that no one else will 604 * touch the page. we write protect all the mappings 605 * of the page so that no one touches it while it is 606 * in I/O. 607 */ 608 609 swap_backed = ((p->pg_flags & PQ_SWAPBACKED) != 0); 610 atomic_setbits_int(&p->pg_flags, PG_BUSY); 611 UVM_PAGE_OWN(p, "scan_inactive"); 612 pmap_page_protect(p, PROT_READ); 613 uvmexp.pgswapout++; 614 615 /* 616 * for swap-backed pages we need to (re)allocate 617 * swap space. 618 */ 619 if (swap_backed) { 620 /* free old swap slot (if any) */ 621 if (anon) { 622 if (anon->an_swslot) { 623 uvm_swap_free(anon->an_swslot, 624 1); 625 anon->an_swslot = 0; 626 } 627 } else { 628 uao_dropswap(uobj, 629 p->offset >> PAGE_SHIFT); 630 } 631 632 /* start new cluster (if necessary) */ 633 if (swslot == 0) { 634 swnpages = SWCLUSTPAGES; 635 swslot = uvm_swap_alloc(&swnpages, 636 TRUE); 637 if (swslot == 0) { 638 /* no swap? give up! */ 639 atomic_clearbits_int( 640 &p->pg_flags, 641 PG_BUSY); 642 UVM_PAGE_OWN(p, NULL); 643 if (anon) 644 rw_exit(anon->an_lock); 645 else 646 rw_exit( 647 uobj->vmobjlock); 648 continue; 649 } 650 swcpages = 0; /* cluster is empty */ 651 } 652 653 /* add block to cluster */ 654 swpps[swcpages] = p; 655 if (anon) 656 anon->an_swslot = swslot + swcpages; 657 else 658 uao_set_swslot(uobj, 659 p->offset >> PAGE_SHIFT, 660 swslot + swcpages); 661 swcpages++; 662 } 663 } else { 664 /* if p == NULL we must be doing a last swap i/o */ 665 swap_backed = TRUE; 666 } 667 668 /* 669 * now consider doing the pageout. 670 * 671 * for swap-backed pages, we do the pageout if we have either 672 * filled the cluster (in which case (swnpages == swcpages) or 673 * run out of pages (p == NULL). 674 * 675 * for object pages, we always do the pageout. 676 */ 677 if (swap_backed) { 678 if (p) { /* if we just added a page to cluster */ 679 if (anon) 680 rw_exit(anon->an_lock); 681 else 682 rw_exit(uobj->vmobjlock); 683 684 /* cluster not full yet? */ 685 if (swcpages < swnpages) 686 continue; 687 } 688 689 /* starting I/O now... set up for it */ 690 npages = swcpages; 691 ppsp = swpps; 692 /* for swap-backed pages only */ 693 start = (vaddr_t) swslot; 694 695 /* if this is final pageout we could have a few 696 * extra swap blocks */ 697 if (swcpages < swnpages) { 698 uvm_swap_free(swslot + swcpages, 699 (swnpages - swcpages)); 700 } 701 } else { 702 /* normal object pageout */ 703 ppsp = pps; 704 npages = sizeof(pps) / sizeof(struct vm_page *); 705 /* not looked at because PGO_ALLPAGES is set */ 706 start = 0; 707 } 708 709 /* 710 * now do the pageout. 711 * 712 * for swap_backed pages we have already built the cluster. 713 * for !swap_backed pages, uvm_pager_put will call the object's 714 * "make put cluster" function to build a cluster on our behalf. 715 * 716 * we pass the PGO_PDFREECLUST flag to uvm_pager_put to instruct 717 * it to free the cluster pages for us on a successful I/O (it 718 * always does this for un-successful I/O requests). this 719 * allows us to do clustered pageout without having to deal 720 * with cluster pages at this level. 721 * 722 * note locking semantics of uvm_pager_put with PGO_PDFREECLUST: 723 * IN: locked: page queues 724 * OUT: locked: 725 * !locked: pageqs 726 */ 727 728 uvmexp.pdpageouts++; 729 result = uvm_pager_put(swap_backed ? NULL : uobj, p, 730 &ppsp, &npages, PGO_ALLPAGES|PGO_PDFREECLUST, start, 0); 731 732 /* 733 * if we did i/o to swap, zero swslot to indicate that we are 734 * no longer building a swap-backed cluster. 735 */ 736 737 if (swap_backed) 738 swslot = 0; /* done with this cluster */ 739 740 /* 741 * first, we check for VM_PAGER_PEND which means that the 742 * async I/O is in progress and the async I/O done routine 743 * will clean up after us. in this case we move on to the 744 * next page. 745 * 746 * there is a very remote chance that the pending async i/o can 747 * finish _before_ we get here. if that happens, our page "p" 748 * may no longer be on the inactive queue. so we verify this 749 * when determining the next page (starting over at the head if 750 * we've lost our inactive page). 751 */ 752 753 if (result == VM_PAGER_PEND) { 754 uvmexp.paging += npages; 755 uvm_lock_pageq(); 756 uvmexp.pdpending++; 757 if (p) { 758 if (p->pg_flags & PQ_INACTIVE) 759 nextpg = TAILQ_NEXT(p, pageq); 760 else 761 nextpg = TAILQ_FIRST(pglst); 762 } else { 763 nextpg = NULL; 764 } 765 continue; 766 } 767 768 /* clean up "p" if we have one */ 769 if (p) { 770 /* 771 * the I/O request to "p" is done and uvm_pager_put 772 * has freed any cluster pages it may have allocated 773 * during I/O. all that is left for us to do is 774 * clean up page "p" (which is still PG_BUSY). 775 * 776 * our result could be one of the following: 777 * VM_PAGER_OK: successful pageout 778 * 779 * VM_PAGER_AGAIN: tmp resource shortage, we skip 780 * to next page 781 * VM_PAGER_{FAIL,ERROR,BAD}: an error. we 782 * "reactivate" page to get it out of the way (it 783 * will eventually drift back into the inactive 784 * queue for a retry). 785 * VM_PAGER_UNLOCK: should never see this as it is 786 * only valid for "get" operations 787 */ 788 789 /* relock p's object: page queues not lock yet, so 790 * no need for "try" */ 791 792 /* !swap_backed case: already locked... */ 793 if (swap_backed) { 794 if (anon) 795 rw_enter(anon->an_lock, RW_WRITE); 796 else 797 rw_enter(uobj->vmobjlock, RW_WRITE); 798 } 799 800 #ifdef DIAGNOSTIC 801 if (result == VM_PAGER_UNLOCK) 802 panic("pagedaemon: pageout returned " 803 "invalid 'unlock' code"); 804 #endif 805 806 /* handle PG_WANTED now */ 807 if (p->pg_flags & PG_WANTED) 808 wakeup(p); 809 810 atomic_clearbits_int(&p->pg_flags, PG_BUSY|PG_WANTED); 811 UVM_PAGE_OWN(p, NULL); 812 813 /* released during I/O? Can only happen for anons */ 814 if (p->pg_flags & PG_RELEASED) { 815 KASSERT(anon != NULL); 816 /* 817 * remove page so we can get nextpg, 818 * also zero out anon so we don't use 819 * it after the free. 820 */ 821 anon->an_page = NULL; 822 p->uanon = NULL; 823 824 rw_exit(anon->an_lock); 825 uvm_anfree(anon); /* kills anon */ 826 pmap_page_protect(p, PROT_NONE); 827 anon = NULL; 828 uvm_lock_pageq(); 829 nextpg = TAILQ_NEXT(p, pageq); 830 /* free released page */ 831 uvm_pagefree(p); 832 } else { /* page was not released during I/O */ 833 uvm_lock_pageq(); 834 nextpg = TAILQ_NEXT(p, pageq); 835 if (result != VM_PAGER_OK) { 836 /* pageout was a failure... */ 837 if (result != VM_PAGER_AGAIN) 838 uvm_pageactivate(p); 839 pmap_clear_reference(p); 840 /* XXXCDC: if (swap_backed) FREE p's 841 * swap block? */ 842 } else { 843 /* pageout was a success... */ 844 pmap_clear_reference(p); 845 pmap_clear_modify(p); 846 atomic_setbits_int(&p->pg_flags, 847 PG_CLEAN); 848 } 849 } 850 851 /* 852 * drop object lock (if there is an object left). do 853 * a safety check of nextpg to make sure it is on the 854 * inactive queue (it should be since PG_BUSY pages on 855 * the inactive queue can't be re-queued [note: not 856 * true for active queue]). 857 */ 858 if (anon) 859 rw_exit(anon->an_lock); 860 else if (uobj) 861 rw_exit(uobj->vmobjlock); 862 863 if (nextpg && (nextpg->pg_flags & PQ_INACTIVE) == 0) { 864 nextpg = TAILQ_FIRST(pglst); /* reload! */ 865 } 866 } else { 867 /* 868 * if p is null in this loop, make sure it stays null 869 * in the next loop. 870 */ 871 nextpg = NULL; 872 873 /* 874 * lock page queues here just so they're always locked 875 * at the end of the loop. 876 */ 877 uvm_lock_pageq(); 878 } 879 } 880 return (retval); 881 } 882 883 /* 884 * uvmpd_scan: scan the page queues and attempt to meet our targets. 885 * 886 * => called with pageq's locked 887 */ 888 889 void 890 uvmpd_scan(struct uvm_pmalloc *pma) 891 { 892 int free, inactive_shortage, swap_shortage, pages_freed; 893 struct vm_page *p, *nextpg; 894 struct uvm_object *uobj; 895 struct vm_anon *anon; 896 struct rwlock *slock; 897 898 MUTEX_ASSERT_LOCKED(&uvm.pageqlock); 899 900 uvmexp.pdrevs++; /* counter */ 901 uobj = NULL; 902 903 /* 904 * get current "free" page count 905 */ 906 free = uvmexp.free - BUFPAGES_DEFICIT; 907 908 #ifndef __SWAP_BROKEN 909 /* 910 * swap out some processes if we are below our free target. 911 * we need to unlock the page queues for this. 912 */ 913 if (free < uvmexp.freetarg) { 914 uvmexp.pdswout++; 915 uvm_unlock_pageq(); 916 uvm_swapout_threads(); 917 uvm_lock_pageq(); 918 } 919 #endif 920 921 /* 922 * now we want to work on meeting our targets. first we work on our 923 * free target by converting inactive pages into free pages. then 924 * we work on meeting our inactive target by converting active pages 925 * to inactive ones. 926 */ 927 928 /* 929 * alternate starting queue between swap and object based on the 930 * low bit of uvmexp.pdrevs (which we bump by one each call). 931 */ 932 pages_freed = uvmexp.pdfreed; 933 (void) uvmpd_scan_inactive(pma, &uvm.page_inactive); 934 pages_freed = uvmexp.pdfreed - pages_freed; 935 936 /* 937 * we have done the scan to get free pages. now we work on meeting 938 * our inactive target. 939 */ 940 inactive_shortage = uvmexp.inactarg - uvmexp.inactive - BUFPAGES_INACT; 941 942 /* 943 * detect if we're not going to be able to page anything out 944 * until we free some swap resources from active pages. 945 */ 946 free = uvmexp.free - BUFPAGES_DEFICIT; 947 swap_shortage = 0; 948 if (free < uvmexp.freetarg && 949 uvmexp.swpginuse == uvmexp.swpages && 950 !uvm_swapisfull() && 951 pages_freed == 0) { 952 swap_shortage = uvmexp.freetarg - free; 953 } 954 955 for (p = TAILQ_FIRST(&uvm.page_active); 956 p != NULL && (inactive_shortage > 0 || swap_shortage > 0); 957 p = nextpg) { 958 nextpg = TAILQ_NEXT(p, pageq); 959 if (p->pg_flags & PG_BUSY) { 960 continue; 961 } 962 963 /* 964 * lock the page's owner. 965 */ 966 if (p->uobject != NULL) { 967 uobj = p->uobject; 968 slock = uobj->vmobjlock; 969 if (rw_enter(slock, RW_WRITE|RW_NOSLEEP)) { 970 continue; 971 } 972 } else { 973 anon = p->uanon; 974 KASSERT(p->uanon != NULL); 975 slock = anon->an_lock; 976 if (rw_enter(slock, RW_WRITE|RW_NOSLEEP)) { 977 continue; 978 } 979 } 980 981 /* 982 * skip this page if it's busy. 983 */ 984 if ((p->pg_flags & PG_BUSY) != 0) { 985 rw_exit(slock); 986 continue; 987 } 988 989 /* 990 * if there's a shortage of swap, free any swap allocated 991 * to this page so that other pages can be paged out. 992 */ 993 if (swap_shortage > 0) { 994 if ((p->pg_flags & PQ_ANON) && p->uanon->an_swslot) { 995 uvm_swap_free(p->uanon->an_swslot, 1); 996 p->uanon->an_swslot = 0; 997 atomic_clearbits_int(&p->pg_flags, PG_CLEAN); 998 swap_shortage--; 999 } 1000 if (p->pg_flags & PQ_AOBJ) { 1001 int slot = uao_set_swslot(p->uobject, 1002 p->offset >> PAGE_SHIFT, 0); 1003 if (slot) { 1004 uvm_swap_free(slot, 1); 1005 atomic_clearbits_int(&p->pg_flags, 1006 PG_CLEAN); 1007 swap_shortage--; 1008 } 1009 } 1010 } 1011 1012 /* 1013 * deactivate this page if there's a shortage of 1014 * inactive pages. 1015 */ 1016 if (inactive_shortage > 0) { 1017 pmap_page_protect(p, PROT_NONE); 1018 /* no need to check wire_count as pg is "active" */ 1019 uvm_pagedeactivate(p); 1020 uvmexp.pddeact++; 1021 inactive_shortage--; 1022 } 1023 1024 /* 1025 * we're done with this page. 1026 */ 1027 rw_exit(slock); 1028 } 1029 } 1030 1031 #ifdef HIBERNATE 1032 1033 /* 1034 * uvmpd_drop: drop clean pages from list 1035 */ 1036 void 1037 uvmpd_drop(struct pglist *pglst) 1038 { 1039 struct vm_page *p, *nextpg; 1040 1041 for (p = TAILQ_FIRST(pglst); p != NULL; p = nextpg) { 1042 nextpg = TAILQ_NEXT(p, pageq); 1043 1044 if (p->pg_flags & PQ_ANON || p->uobject == NULL) 1045 continue; 1046 1047 if (p->pg_flags & PG_BUSY) 1048 continue; 1049 1050 if (p->pg_flags & PG_CLEAN) { 1051 struct uvm_object * uobj = p->uobject; 1052 1053 rw_enter(uobj->vmobjlock, RW_WRITE); 1054 uvm_lock_pageq(); 1055 /* 1056 * we now have the page queues locked. 1057 * the page is not busy. if the page is clean we 1058 * can free it now and continue. 1059 */ 1060 if (p->pg_flags & PG_CLEAN) { 1061 if (p->pg_flags & PQ_SWAPBACKED) { 1062 /* this page now lives only in swap */ 1063 atomic_inc_int(&uvmexp.swpgonly); 1064 } 1065 1066 /* zap all mappings with pmap_page_protect... */ 1067 pmap_page_protect(p, PROT_NONE); 1068 uvm_pagefree(p); 1069 } 1070 uvm_unlock_pageq(); 1071 rw_exit(uobj->vmobjlock); 1072 } 1073 } 1074 } 1075 1076 void 1077 uvmpd_hibernate(void) 1078 { 1079 uvmpd_drop(&uvm.page_inactive); 1080 uvmpd_drop(&uvm.page_active); 1081 } 1082 1083 #endif 1084