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