1 /* $NetBSD: uvm_loan.c,v 1.36 2001/12/31 19:21:36 chs Exp $ */ 2 3 /* 4 * 5 * Copyright (c) 1997 Charles D. Cranor and Washington University. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by Charles D. Cranor and 19 * Washington University. 20 * 4. The name of the author may not be used to endorse or promote products 21 * derived from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 * 34 * from: Id: uvm_loan.c,v 1.1.6.4 1998/02/06 05:08:43 chs Exp 35 */ 36 37 /* 38 * uvm_loan.c: page loanout handler 39 */ 40 41 #include <sys/cdefs.h> 42 __KERNEL_RCSID(0, "$NetBSD: uvm_loan.c,v 1.36 2001/12/31 19:21:36 chs Exp $"); 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/kernel.h> 47 #include <sys/proc.h> 48 #include <sys/malloc.h> 49 #include <sys/mman.h> 50 51 #include <uvm/uvm.h> 52 53 /* 54 * "loaned" pages are pages which are (read-only, copy-on-write) loaned 55 * from the VM system to other parts of the kernel. this allows page 56 * copying to be avoided (e.g. you can loan pages from objs/anons to 57 * the mbuf system). 58 * 59 * there are 3 types of loans possible: 60 * O->K uvm_object page to wired kernel page (e.g. mbuf data area) 61 * A->K anon page to wired kernel page (e.g. mbuf data area) 62 * O->A uvm_object to anon loan (e.g. vnode page to an anon) 63 * note that it possible to have an O page loaned to both an A and K 64 * at the same time. 65 * 66 * loans are tracked by pg->loan_count. an O->A page will have both 67 * a uvm_object and a vm_anon, but PQ_ANON will not be set. this sort 68 * of page is considered "owned" by the uvm_object (not the anon). 69 * 70 * each loan of a page to the kernel bumps the pg->wire_count. the 71 * kernel mappings for these pages will be read-only and wired. since 72 * the page will also be wired, it will not be a candidate for pageout, 73 * and thus will never be pmap_page_protect()'d with VM_PROT_NONE. a 74 * write fault in the kernel to one of these pages will not cause 75 * copy-on-write. instead, the page fault is considered fatal. this 76 * is because the kernel mapping will have no way to look up the 77 * object/anon which the page is owned by. this is a good side-effect, 78 * since a kernel write to a loaned page is an error. 79 * 80 * owners that want to free their pages and discover that they are 81 * loaned out simply "disown" them (the page becomes an orphan). these 82 * pages should be freed when the last loan is dropped. in some cases 83 * an anon may "adopt" an orphaned page. 84 * 85 * locking: to read pg->loan_count either the owner or the page queues 86 * must be locked. to modify pg->loan_count, both the owner of the page 87 * and the PQs must be locked. pg->flags is (as always) locked by 88 * the owner of the page. 89 * 90 * note that locking from the "loaned" side is tricky since the object 91 * getting the loaned page has no reference to the page's owner and thus 92 * the owner could "die" at any time. in order to prevent the owner 93 * from dying the page queues should be locked. this forces us to sometimes 94 * use "try" locking. 95 * 96 * loans are typically broken by the following events: 97 * 1. user-level xwrite fault to a loaned page 98 * 2. pageout of clean+inactive O->A loaned page 99 * 3. owner frees page (e.g. pager flush) 100 * 101 * note that loaning a page causes all mappings of the page to become 102 * read-only (via pmap_page_protect). this could have an unexpected 103 * effect on normal "wired" pages if one is not careful (XXX). 104 */ 105 106 /* 107 * local prototypes 108 */ 109 110 static int uvm_loananon __P((struct uvm_faultinfo *, void ***, 111 int, struct vm_anon *)); 112 static int uvm_loanentry __P((struct uvm_faultinfo *, void ***, int)); 113 static int uvm_loanuobj __P((struct uvm_faultinfo *, void ***, 114 int, vaddr_t)); 115 static int uvm_loanzero __P((struct uvm_faultinfo *, void ***, int)); 116 static void uvm_unloananon __P((struct vm_anon **, int)); 117 static void uvm_unloanpage __P((struct vm_page **, int)); 118 119 120 /* 121 * inlines 122 */ 123 124 /* 125 * uvm_loanentry: loan out pages in a map entry (helper fn for uvm_loan()) 126 * 127 * => "ufi" is the result of a successful map lookup (meaning that 128 * on entry the map is locked by the caller) 129 * => we may unlock and then relock the map if needed (for I/O) 130 * => we put our output result in "output" 131 * => we always return with the map unlocked 132 * => possible return values: 133 * -1 == error, map is unlocked 134 * 0 == map relock error (try again!), map is unlocked 135 * >0 == number of pages we loaned, map is unlocked 136 */ 137 138 static __inline int 139 uvm_loanentry(ufi, output, flags) 140 struct uvm_faultinfo *ufi; 141 void ***output; 142 int flags; 143 { 144 vaddr_t curaddr = ufi->orig_rvaddr; 145 vsize_t togo = ufi->size; 146 struct vm_aref *aref = &ufi->entry->aref; 147 struct uvm_object *uobj = ufi->entry->object.uvm_obj; 148 struct vm_anon *anon; 149 int rv, result = 0; 150 151 /* 152 * lock us the rest of the way down (we unlock before return) 153 */ 154 if (aref->ar_amap) 155 amap_lock(aref->ar_amap); 156 if (uobj) 157 simple_lock(&uobj->vmobjlock); 158 159 /* 160 * loop until done 161 */ 162 while (togo) { 163 164 /* 165 * find the page we want. check the anon layer first. 166 */ 167 168 if (aref->ar_amap) { 169 anon = amap_lookup(aref, curaddr - ufi->entry->start); 170 } else { 171 anon = NULL; 172 } 173 174 /* locked: map, amap, uobj */ 175 if (anon) { 176 rv = uvm_loananon(ufi, output, flags, anon); 177 } else if (uobj) { 178 rv = uvm_loanuobj(ufi, output, flags, curaddr); 179 } else if (UVM_ET_ISCOPYONWRITE(ufi->entry)) { 180 rv = uvm_loanzero(ufi, output, flags); 181 } else { 182 rv = -1; 183 } 184 /* locked: if (rv > 0) => map, amap, uobj [o.w. unlocked] */ 185 186 /* total failure */ 187 if (rv < 0) 188 return (-1); 189 190 /* relock failed, need to do another lookup */ 191 if (rv == 0) 192 return (result); 193 194 /* 195 * got it... advance to next page 196 */ 197 198 result++; 199 togo -= PAGE_SIZE; 200 curaddr += PAGE_SIZE; 201 } 202 203 /* 204 * unlock what we locked, unlock the maps and return 205 */ 206 207 if (aref->ar_amap) 208 amap_unlock(aref->ar_amap); 209 if (uobj) 210 simple_unlock(&uobj->vmobjlock); 211 uvmfault_unlockmaps(ufi, FALSE); 212 return (result); 213 } 214 215 /* 216 * normal functions 217 */ 218 219 /* 220 * uvm_loan: loan pages in a map out to anons or to the kernel 221 * 222 * => map should be unlocked 223 * => start and len should be multiples of PAGE_SIZE 224 * => result is either an array of anon's or vm_pages (depending on flags) 225 * => flag values: UVM_LOAN_TOANON - loan to anons 226 * UVM_LOAN_TOPAGE - loan to wired kernel page 227 * one and only one of these flags must be set! 228 * => returns 0 (success), or an appropriate error number 229 */ 230 231 int 232 uvm_loan(map, start, len, v, flags) 233 struct vm_map *map; 234 vaddr_t start; 235 vsize_t len; 236 void *v; 237 int flags; 238 { 239 struct uvm_faultinfo ufi; 240 void **result, **output; 241 int rv, error; 242 243 /* 244 * ensure that one and only one of the flags is set 245 */ 246 247 KASSERT(((flags & UVM_LOAN_TOANON) == 0) ^ 248 ((flags & UVM_LOAN_TOPAGE) == 0)); 249 KASSERT((map->flags & VM_MAP_INTRSAFE) == 0); 250 251 /* 252 * "output" is a pointer to the current place to put the loaned page. 253 */ 254 255 result = v; 256 output = &result[0]; /* start at the beginning ... */ 257 258 /* 259 * while we've got pages to do 260 */ 261 262 while (len > 0) { 263 264 /* 265 * fill in params for a call to uvmfault_lookup 266 */ 267 268 ufi.orig_map = map; 269 ufi.orig_rvaddr = start; 270 ufi.orig_size = len; 271 272 /* 273 * do the lookup, the only time this will fail is if we hit on 274 * an unmapped region (an error) 275 */ 276 277 if (!uvmfault_lookup(&ufi, FALSE)) { 278 error = ENOENT; 279 goto fail; 280 } 281 282 /* 283 * map now locked. now do the loanout... 284 */ 285 286 rv = uvm_loanentry(&ufi, &output, flags); 287 if (rv < 0) { 288 /* all unlocked due to error */ 289 error = EINVAL; 290 goto fail; 291 } 292 293 /* 294 * done! the map is unlocked. advance, if possible. 295 * 296 * XXXCDC: could be recoded to hold the map lock with 297 * smarter code (but it only happens on map entry 298 * boundaries, so it isn't that bad). 299 */ 300 301 if (rv) { 302 rv <<= PAGE_SHIFT; 303 len -= rv; 304 start += rv; 305 } 306 } 307 return 0; 308 309 fail: 310 /* 311 * failed to complete loans. drop any loans and return failure code. 312 * map is already unlocked. 313 */ 314 315 if (output - result) { 316 if (flags & UVM_LOAN_TOANON) { 317 uvm_unloananon((struct vm_anon **)result, 318 output - result); 319 } else { 320 uvm_unloanpage((struct vm_page **)result, 321 output - result); 322 } 323 } 324 return (error); 325 } 326 327 /* 328 * uvm_loananon: loan a page from an anon out 329 * 330 * => called with map, amap, uobj locked 331 * => return value: 332 * -1 = fatal error, everything is unlocked, abort. 333 * 0 = lookup in ufi went stale, everything unlocked, relookup and 334 * try again 335 * 1 = got it, everything still locked 336 */ 337 338 int 339 uvm_loananon(ufi, output, flags, anon) 340 struct uvm_faultinfo *ufi; 341 void ***output; 342 int flags; 343 struct vm_anon *anon; 344 { 345 struct vm_page *pg; 346 int error; 347 348 /* 349 * if we are loaning to "another" anon then it is easy, we just 350 * bump the reference count on the current anon and return a 351 * pointer to it (it becomes copy-on-write shared). 352 */ 353 354 if (flags & UVM_LOAN_TOANON) { 355 simple_lock(&anon->an_lock); 356 pg = anon->u.an_page; 357 if (pg && (pg->pqflags & PQ_ANON) != 0 && anon->an_ref == 1) { 358 pmap_page_protect(pg, VM_PROT_READ); 359 } 360 anon->an_ref++; 361 **output = anon; 362 (*output)++; 363 simple_unlock(&anon->an_lock); 364 return (1); 365 } 366 367 /* 368 * we are loaning to a kernel-page. we need to get the page 369 * resident so we can wire it. uvmfault_anonget will handle 370 * this for us. 371 */ 372 373 simple_lock(&anon->an_lock); 374 error = uvmfault_anonget(ufi, ufi->entry->aref.ar_amap, anon); 375 376 /* 377 * if we were unable to get the anon, then uvmfault_anonget has 378 * unlocked everything and returned an error code. 379 */ 380 381 if (error) { 382 383 /* need to refault (i.e. refresh our lookup) ? */ 384 if (error == ERESTART) { 385 return (0); 386 } 387 388 /* "try again"? sleep a bit and retry ... */ 389 if (error == EAGAIN) { 390 tsleep(&lbolt, PVM, "loanagain", 0); 391 return (0); 392 } 393 394 /* otherwise flag it as an error */ 395 return (-1); 396 } 397 398 /* 399 * we have the page and its owner locked: do the loan now. 400 */ 401 402 pg = anon->u.an_page; 403 uvm_lock_pageq(); 404 KASSERT(pg->wire_count == 0); 405 if (pg->loan_count == 0) { 406 pmap_page_protect(pg, VM_PROT_READ); 407 } 408 pg->loan_count++; 409 uvm_pagedequeue(pg); 410 uvm_unlock_pageq(); 411 **output = pg; 412 (*output)++; 413 414 /* unlock anon and return success */ 415 if (pg->uobject) /* XXXCDC: what if this is our uobj? bad */ 416 simple_unlock(&pg->uobject->vmobjlock); 417 simple_unlock(&anon->an_lock); 418 return (1); 419 } 420 421 /* 422 * uvm_loanuobj: loan a page from a uobj out 423 * 424 * => called with map, amap, uobj locked 425 * => return value: 426 * -1 = fatal error, everything is unlocked, abort. 427 * 0 = lookup in ufi went stale, everything unlocked, relookup and 428 * try again 429 * 1 = got it, everything still locked 430 */ 431 432 static int 433 uvm_loanuobj(ufi, output, flags, va) 434 struct uvm_faultinfo *ufi; 435 void ***output; 436 int flags; 437 vaddr_t va; 438 { 439 struct vm_amap *amap = ufi->entry->aref.ar_amap; 440 struct uvm_object *uobj = ufi->entry->object.uvm_obj; 441 struct vm_page *pg; 442 struct vm_anon *anon; 443 int error, npages; 444 boolean_t locked; 445 446 /* 447 * first we must make sure the page is resident. 448 * 449 * XXXCDC: duplicate code with uvm_fault(). 450 */ 451 452 if (uobj->pgops->pgo_get) { /* try locked pgo_get */ 453 npages = 1; 454 pg = NULL; 455 error = uobj->pgops->pgo_get(uobj, va - ufi->entry->start, 456 &pg, &npages, 0, VM_PROT_READ, MADV_NORMAL, PGO_LOCKED); 457 } else { 458 error = EIO; /* must have pgo_get op */ 459 } 460 461 /* 462 * check the result of the locked pgo_get. if there is a problem, 463 * then we fail the loan. 464 */ 465 466 if (error && error != EBUSY) { 467 uvmfault_unlockall(ufi, amap, uobj, NULL); 468 return (-1); 469 } 470 471 /* 472 * if we need to unlock for I/O, do so now. 473 */ 474 475 if (error == EBUSY) { 476 uvmfault_unlockall(ufi, amap, NULL, NULL); 477 478 /* locked: uobj */ 479 npages = 1; 480 error = uobj->pgops->pgo_get(uobj, va - ufi->entry->start, 481 &pg, &npages, 0, VM_PROT_READ, MADV_NORMAL, PGO_SYNCIO); 482 /* locked: <nothing> */ 483 484 if (error) { 485 if (error == EAGAIN) { 486 tsleep(&lbolt, PVM, "fltagain2", 0); 487 return (0); 488 } 489 return (-1); 490 } 491 492 /* 493 * pgo_get was a success. attempt to relock everything. 494 */ 495 496 locked = uvmfault_relock(ufi); 497 if (locked && amap) 498 amap_lock(amap); 499 simple_lock(&uobj->vmobjlock); 500 501 /* 502 * verify that the page has not be released and re-verify 503 * that amap slot is still free. if there is a problem we 504 * drop our lock (thus force a lookup refresh/retry). 505 */ 506 507 if ((pg->flags & PG_RELEASED) != 0 || 508 (locked && amap && amap_lookup(&ufi->entry->aref, 509 ufi->orig_rvaddr - ufi->entry->start))) { 510 if (locked) 511 uvmfault_unlockall(ufi, amap, NULL, NULL); 512 locked = FALSE; 513 } 514 515 /* 516 * didn't get the lock? release the page and retry. 517 */ 518 519 if (locked == FALSE) { 520 if (pg->flags & PG_WANTED) { 521 wakeup(pg); 522 } 523 if (pg->flags & PG_RELEASED) { 524 uvm_pagefree(pg); 525 return (0); 526 } 527 uvm_lock_pageq(); 528 uvm_pageactivate(pg); 529 uvm_unlock_pageq(); 530 pg->flags &= ~(PG_BUSY|PG_WANTED); 531 UVM_PAGE_OWN(pg, NULL); 532 simple_unlock(&uobj->vmobjlock); 533 return (0); 534 } 535 } 536 537 /* 538 * at this point we have the page we want ("pg") marked PG_BUSY for us 539 * and we have all data structures locked. do the loanout. page can 540 * not be PG_RELEASED (we caught this above). 541 */ 542 543 if ((flags & UVM_LOAN_TOANON) == 0) { 544 uvm_lock_pageq(); 545 if (pg->loan_count == 0) { 546 pmap_page_protect(pg, VM_PROT_READ); 547 } 548 pg->loan_count++; 549 uvm_pagedequeue(pg); 550 uvm_unlock_pageq(); 551 if (pg->flags & PG_WANTED) { 552 wakeup(pg); 553 } 554 pg->flags &= ~(PG_WANTED|PG_BUSY); 555 UVM_PAGE_OWN(pg, NULL); 556 **output = pg; 557 (*output)++; 558 return (1); 559 } 560 561 /* 562 * must be a loan to an anon. check to see if there is already 563 * an anon associated with this page. if so, then just return 564 * a reference to this object. the page should already be 565 * mapped read-only because it is already on loan. 566 */ 567 568 if (pg->uanon) { 569 anon = pg->uanon; 570 simple_lock(&anon->an_lock); 571 anon->an_ref++; 572 simple_unlock(&anon->an_lock); 573 if (pg->flags & PG_WANTED) { 574 wakeup(pg); 575 } 576 pg->flags &= ~(PG_WANTED|PG_BUSY); 577 UVM_PAGE_OWN(pg, NULL); 578 **output = anon; 579 (*output)++; 580 return (1); 581 } 582 583 /* 584 * need to allocate a new anon 585 */ 586 587 anon = uvm_analloc(); 588 if (anon == NULL) { 589 if (pg->flags & PG_WANTED) { 590 wakeup(pg); 591 } 592 pg->flags &= ~(PG_WANTED|PG_BUSY); 593 UVM_PAGE_OWN(pg, NULL); 594 uvmfault_unlockall(ufi, amap, uobj, NULL); 595 return (-1); 596 } 597 anon->u.an_page = pg; 598 pg->uanon = anon; 599 uvm_lock_pageq(); 600 if (pg->loan_count == 0) { 601 pmap_page_protect(pg, VM_PROT_READ); 602 } 603 pg->loan_count++; 604 uvm_pageactivate(pg); 605 uvm_unlock_pageq(); 606 if (pg->flags & PG_WANTED) { 607 wakeup(pg); 608 } 609 pg->flags &= ~(PG_WANTED|PG_BUSY); 610 UVM_PAGE_OWN(pg, NULL); 611 simple_unlock(&anon->an_lock); 612 **output = anon; 613 (*output)++; 614 return (1); 615 } 616 617 /* 618 * uvm_loanzero: "loan" a zero-fill page out 619 * 620 * => called with map, amap, uobj locked 621 * => return value: 622 * -1 = fatal error, everything is unlocked, abort. 623 * 0 = lookup in ufi went stale, everything unlocked, relookup and 624 * try again 625 * 1 = got it, everything still locked 626 */ 627 628 static int 629 uvm_loanzero(ufi, output, flags) 630 struct uvm_faultinfo *ufi; 631 void ***output; 632 int flags; 633 { 634 struct vm_anon *anon; 635 struct vm_page *pg; 636 struct uvm_object *uobj = ufi->entry->object.uvm_obj; 637 struct vm_amap *amap = ufi->entry->aref.ar_amap; 638 639 if ((flags & UVM_LOAN_TOANON) == 0) { /* loaning to kernel-page */ 640 while ((pg = uvm_pagealloc(NULL, 0, NULL, 641 UVM_PGA_ZERO)) == NULL) { 642 uvmfault_unlockall(ufi, amap, uobj, NULL); 643 uvm_wait("loanzero1"); 644 if (!uvmfault_relock(ufi)) { 645 return (0); 646 } 647 if (amap) { 648 amap_lock(amap); 649 } 650 if (uobj) { 651 simple_lock(&uobj->vmobjlock); 652 } 653 } 654 655 /* got a zero'd page; return */ 656 pg->flags &= ~(PG_WANTED|PG_BUSY); 657 UVM_PAGE_OWN(pg, NULL); 658 **output = pg; 659 (*output)++; 660 pg->loan_count = 1; 661 return (1); 662 } 663 664 /* loaning to an anon */ 665 while ((anon = uvm_analloc()) == NULL || 666 (pg = uvm_pagealloc(NULL, 0, anon, UVM_PGA_ZERO)) == NULL) { 667 uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj, anon); 668 669 /* out of swap causes us to fail */ 670 if (anon == NULL) { 671 return (-1); 672 } 673 674 /* 675 * drop our reference; we're the only one, 676 * so it's okay that the anon isn't locked 677 * here. 678 */ 679 680 anon->an_ref--; 681 uvm_anfree(anon); 682 uvm_wait("loanzero2"); /* wait for pagedaemon */ 683 684 if (!uvmfault_relock(ufi)) { 685 /* map changed while unlocked, need relookup */ 686 return (0); 687 } 688 689 /* relock everything else */ 690 if (amap) { 691 amap_lock(amap); 692 } 693 if (uobj) { 694 simple_lock(&uobj->vmobjlock); 695 } 696 } 697 698 /* got a zero'd page; return */ 699 pg->flags &= ~(PG_BUSY|PG_FAKE); 700 UVM_PAGE_OWN(pg, NULL); 701 uvm_lock_pageq(); 702 uvm_pageactivate(pg); 703 uvm_unlock_pageq(); 704 **output = anon; 705 (*output)++; 706 return (1); 707 } 708 709 710 /* 711 * uvm_unloananon: kill loans on anons (basically a normal ref drop) 712 * 713 * => we expect all our resources to be unlocked 714 */ 715 716 static void 717 uvm_unloananon(aloans, nanons) 718 struct vm_anon **aloans; 719 int nanons; 720 { 721 struct vm_anon *anon; 722 723 while (nanons-- > 0) { 724 int refs; 725 726 anon = *aloans++; 727 simple_lock(&anon->an_lock); 728 refs = --anon->an_ref; 729 simple_unlock(&anon->an_lock); 730 731 if (refs == 0) { 732 uvm_anfree(anon); 733 } 734 } 735 } 736 737 /* 738 * uvm_unloanpage: kill loans on pages loaned out to the kernel 739 * 740 * => we expect all our resources to be unlocked 741 */ 742 743 static void 744 uvm_unloanpage(ploans, npages) 745 struct vm_page **ploans; 746 int npages; 747 { 748 struct vm_page *pg; 749 struct simplelock *slock; 750 751 uvm_lock_pageq(); 752 while (npages-- > 0) { 753 pg = *ploans++; 754 755 /* 756 * do a little dance to acquire the object or anon lock 757 * as appropriate. we are locking in the wrong order, 758 * so we have to do a try-lock here. 759 */ 760 761 slock = NULL; 762 while (pg->uobject != NULL || pg->uanon != NULL) { 763 if (pg->uobject != NULL) { 764 slock = &pg->uobject->vmobjlock; 765 } else { 766 slock = &pg->uanon->an_lock; 767 } 768 if (simple_lock_try(slock)) { 769 break; 770 } 771 uvm_unlock_pageq(); 772 uvm_lock_pageq(); 773 slock = NULL; 774 } 775 776 /* 777 * drop our loan. if page is owned by an anon but 778 * PQ_ANON is not set, the page was loaned to the anon 779 * from an object which dropped ownership, so resolve 780 * this by turning the anon's loan into real ownership 781 * (ie. decrement loan_count again and set PQ_ANON). 782 * after all this, if there are no loans left, put the 783 * page back a paging queue (if the page is owned by 784 * an anon) or free it (if the page is now unowned). 785 */ 786 787 KASSERT(pg->loan_count > 0); 788 pg->loan_count--; 789 if (pg->uobject == NULL && pg->uanon != NULL && 790 (pg->pqflags & PQ_ANON) == 0) { 791 KASSERT(pg->loan_count > 0); 792 pg->loan_count--; 793 pg->pqflags |= PQ_ANON; 794 } 795 if (pg->loan_count == 0) { 796 if (pg->uobject == NULL && pg->uanon == NULL) { 797 KASSERT((pg->flags & PG_BUSY) == 0); 798 uvm_pagefree(pg); 799 } else { 800 uvm_pageactivate(pg); 801 } 802 } 803 if (slock != NULL) { 804 simple_unlock(slock); 805 } 806 } 807 uvm_unlock_pageq(); 808 } 809 810 /* 811 * uvm_unloan: kill loans on pages or anons. 812 */ 813 814 void 815 uvm_unloan(void *v, int npages, int flags) 816 { 817 if (flags & UVM_LOAN_TOANON) { 818 uvm_unloananon(v, npages); 819 } else { 820 uvm_unloanpage(v, npages); 821 } 822 } 823