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