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