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