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