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