1 /* $NetBSD: uvm_aobj.c,v 1.27 2000/01/11 06:57:49 chs Exp $ */ 2 3 /* 4 * Copyright (c) 1998 Chuck Silvers, Charles D. Cranor and 5 * 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_aobj.c,v 1.1.2.5 1998/02/06 05:14:38 chs Exp 35 */ 36 /* 37 * uvm_aobj.c: anonymous memory uvm_object pager 38 * 39 * author: Chuck Silvers <chuq@chuq.com> 40 * started: Jan-1998 41 * 42 * - design mostly from Chuck Cranor 43 */ 44 45 46 47 #include "opt_uvmhist.h" 48 49 #include <sys/param.h> 50 #include <sys/systm.h> 51 #include <sys/proc.h> 52 #include <sys/malloc.h> 53 #include <sys/pool.h> 54 #include <sys/kernel.h> 55 56 #include <vm/vm.h> 57 #include <vm/vm_page.h> 58 #include <vm/vm_kern.h> 59 60 #include <uvm/uvm.h> 61 62 /* 63 * an aobj manages anonymous-memory backed uvm_objects. in addition 64 * to keeping the list of resident pages, it also keeps a list of 65 * allocated swap blocks. depending on the size of the aobj this list 66 * of allocated swap blocks is either stored in an array (small objects) 67 * or in a hash table (large objects). 68 */ 69 70 /* 71 * local structures 72 */ 73 74 /* 75 * for hash tables, we break the address space of the aobj into blocks 76 * of UAO_SWHASH_CLUSTER_SIZE pages. we require the cluster size to 77 * be a power of two. 78 */ 79 80 #define UAO_SWHASH_CLUSTER_SHIFT 4 81 #define UAO_SWHASH_CLUSTER_SIZE (1 << UAO_SWHASH_CLUSTER_SHIFT) 82 83 /* get the "tag" for this page index */ 84 #define UAO_SWHASH_ELT_TAG(PAGEIDX) \ 85 ((PAGEIDX) >> UAO_SWHASH_CLUSTER_SHIFT) 86 87 /* given an ELT and a page index, find the swap slot */ 88 #define UAO_SWHASH_ELT_PAGESLOT(ELT, PAGEIDX) \ 89 ((ELT)->slots[(PAGEIDX) & (UAO_SWHASH_CLUSTER_SIZE - 1)]) 90 91 /* given an ELT, return its pageidx base */ 92 #define UAO_SWHASH_ELT_PAGEIDX_BASE(ELT) \ 93 ((ELT)->tag << UAO_SWHASH_CLUSTER_SHIFT) 94 95 /* 96 * the swhash hash function 97 */ 98 #define UAO_SWHASH_HASH(AOBJ, PAGEIDX) \ 99 (&(AOBJ)->u_swhash[(((PAGEIDX) >> UAO_SWHASH_CLUSTER_SHIFT) \ 100 & (AOBJ)->u_swhashmask)]) 101 102 /* 103 * the swhash threshhold determines if we will use an array or a 104 * hash table to store the list of allocated swap blocks. 105 */ 106 107 #define UAO_SWHASH_THRESHOLD (UAO_SWHASH_CLUSTER_SIZE * 4) 108 #define UAO_USES_SWHASH(AOBJ) \ 109 ((AOBJ)->u_pages > UAO_SWHASH_THRESHOLD) /* use hash? */ 110 111 /* 112 * the number of buckets in a swhash, with an upper bound 113 */ 114 #define UAO_SWHASH_MAXBUCKETS 256 115 #define UAO_SWHASH_BUCKETS(AOBJ) \ 116 (min((AOBJ)->u_pages >> UAO_SWHASH_CLUSTER_SHIFT, \ 117 UAO_SWHASH_MAXBUCKETS)) 118 119 120 /* 121 * uao_swhash_elt: when a hash table is being used, this structure defines 122 * the format of an entry in the bucket list. 123 */ 124 125 struct uao_swhash_elt { 126 LIST_ENTRY(uao_swhash_elt) list; /* the hash list */ 127 vaddr_t tag; /* our 'tag' */ 128 int count; /* our number of active slots */ 129 int slots[UAO_SWHASH_CLUSTER_SIZE]; /* the slots */ 130 }; 131 132 /* 133 * uao_swhash: the swap hash table structure 134 */ 135 136 LIST_HEAD(uao_swhash, uao_swhash_elt); 137 138 /* 139 * uao_swhash_elt_pool: pool of uao_swhash_elt structures 140 */ 141 142 struct pool uao_swhash_elt_pool; 143 144 /* 145 * uvm_aobj: the actual anon-backed uvm_object 146 * 147 * => the uvm_object is at the top of the structure, this allows 148 * (struct uvm_device *) == (struct uvm_object *) 149 * => only one of u_swslots and u_swhash is used in any given aobj 150 */ 151 152 struct uvm_aobj { 153 struct uvm_object u_obj; /* has: lock, pgops, memq, #pages, #refs */ 154 int u_pages; /* number of pages in entire object */ 155 int u_flags; /* the flags (see uvm_aobj.h) */ 156 int *u_swslots; /* array of offset->swapslot mappings */ 157 /* 158 * hashtable of offset->swapslot mappings 159 * (u_swhash is an array of bucket heads) 160 */ 161 struct uao_swhash *u_swhash; 162 u_long u_swhashmask; /* mask for hashtable */ 163 LIST_ENTRY(uvm_aobj) u_list; /* global list of aobjs */ 164 }; 165 166 /* 167 * uvm_aobj_pool: pool of uvm_aobj structures 168 */ 169 170 struct pool uvm_aobj_pool; 171 172 /* 173 * local functions 174 */ 175 176 static struct uao_swhash_elt *uao_find_swhash_elt __P((struct uvm_aobj *, 177 int, boolean_t)); 178 static int uao_find_swslot __P((struct uvm_aobj *, 179 int)); 180 static boolean_t uao_flush __P((struct uvm_object *, 181 vaddr_t, vaddr_t, 182 int)); 183 static void uao_free __P((struct uvm_aobj *)); 184 static int uao_get __P((struct uvm_object *, vaddr_t, 185 vm_page_t *, int *, int, 186 vm_prot_t, int, int)); 187 static boolean_t uao_releasepg __P((struct vm_page *, 188 struct vm_page **)); 189 static boolean_t uao_pagein __P((struct uvm_aobj *, int, int)); 190 static boolean_t uao_pagein_page __P((struct uvm_aobj *, int)); 191 192 193 194 /* 195 * aobj_pager 196 * 197 * note that some functions (e.g. put) are handled elsewhere 198 */ 199 200 struct uvm_pagerops aobj_pager = { 201 NULL, /* init */ 202 uao_reference, /* reference */ 203 uao_detach, /* detach */ 204 NULL, /* fault */ 205 uao_flush, /* flush */ 206 uao_get, /* get */ 207 NULL, /* asyncget */ 208 NULL, /* put (done by pagedaemon) */ 209 NULL, /* cluster */ 210 NULL, /* mk_pcluster */ 211 uvm_shareprot, /* shareprot */ 212 NULL, /* aiodone */ 213 uao_releasepg /* releasepg */ 214 }; 215 216 /* 217 * uao_list: global list of active aobjs, locked by uao_list_lock 218 */ 219 220 static LIST_HEAD(aobjlist, uvm_aobj) uao_list; 221 static simple_lock_data_t uao_list_lock; 222 223 224 /* 225 * functions 226 */ 227 228 /* 229 * hash table/array related functions 230 */ 231 232 /* 233 * uao_find_swhash_elt: find (or create) a hash table entry for a page 234 * offset. 235 * 236 * => the object should be locked by the caller 237 */ 238 239 static struct uao_swhash_elt * 240 uao_find_swhash_elt(aobj, pageidx, create) 241 struct uvm_aobj *aobj; 242 int pageidx; 243 boolean_t create; 244 { 245 struct uao_swhash *swhash; 246 struct uao_swhash_elt *elt; 247 int page_tag; 248 249 swhash = UAO_SWHASH_HASH(aobj, pageidx); /* first hash to get bucket */ 250 page_tag = UAO_SWHASH_ELT_TAG(pageidx); /* tag to search for */ 251 252 /* 253 * now search the bucket for the requested tag 254 */ 255 for (elt = swhash->lh_first; elt != NULL; elt = elt->list.le_next) { 256 if (elt->tag == page_tag) 257 return(elt); 258 } 259 260 /* fail now if we are not allowed to create a new entry in the bucket */ 261 if (!create) 262 return NULL; 263 264 265 /* 266 * allocate a new entry for the bucket and init/insert it in 267 */ 268 elt = pool_get(&uao_swhash_elt_pool, PR_WAITOK); 269 LIST_INSERT_HEAD(swhash, elt, list); 270 elt->tag = page_tag; 271 elt->count = 0; 272 memset(elt->slots, 0, sizeof(elt->slots)); 273 274 return(elt); 275 } 276 277 /* 278 * uao_find_swslot: find the swap slot number for an aobj/pageidx 279 * 280 * => object must be locked by caller 281 */ 282 __inline static int 283 uao_find_swslot(aobj, pageidx) 284 struct uvm_aobj *aobj; 285 int pageidx; 286 { 287 288 /* 289 * if noswap flag is set, then we never return a slot 290 */ 291 292 if (aobj->u_flags & UAO_FLAG_NOSWAP) 293 return(0); 294 295 /* 296 * if hashing, look in hash table. 297 */ 298 299 if (UAO_USES_SWHASH(aobj)) { 300 struct uao_swhash_elt *elt = 301 uao_find_swhash_elt(aobj, pageidx, FALSE); 302 303 if (elt) 304 return(UAO_SWHASH_ELT_PAGESLOT(elt, pageidx)); 305 else 306 return(NULL); 307 } 308 309 /* 310 * otherwise, look in the array 311 */ 312 return(aobj->u_swslots[pageidx]); 313 } 314 315 /* 316 * uao_set_swslot: set the swap slot for a page in an aobj. 317 * 318 * => setting a slot to zero frees the slot 319 * => object must be locked by caller 320 */ 321 int 322 uao_set_swslot(uobj, pageidx, slot) 323 struct uvm_object *uobj; 324 int pageidx, slot; 325 { 326 struct uvm_aobj *aobj = (struct uvm_aobj *)uobj; 327 int oldslot; 328 UVMHIST_FUNC("uao_set_swslot"); UVMHIST_CALLED(pdhist); 329 UVMHIST_LOG(pdhist, "aobj %p pageidx %d slot %d", 330 aobj, pageidx, slot, 0); 331 332 /* 333 * if noswap flag is set, then we can't set a slot 334 */ 335 336 if (aobj->u_flags & UAO_FLAG_NOSWAP) { 337 338 if (slot == 0) 339 return(0); /* a clear is ok */ 340 341 /* but a set is not */ 342 printf("uao_set_swslot: uobj = %p\n", uobj); 343 panic("uao_set_swslot: attempt to set a slot on a NOSWAP object"); 344 } 345 346 /* 347 * are we using a hash table? if so, add it in the hash. 348 */ 349 350 if (UAO_USES_SWHASH(aobj)) { 351 /* 352 * Avoid allocating an entry just to free it again if 353 * the page had not swap slot in the first place, and 354 * we are freeing. 355 */ 356 struct uao_swhash_elt *elt = 357 uao_find_swhash_elt(aobj, pageidx, slot ? TRUE : FALSE); 358 if (elt == NULL) { 359 #ifdef DIAGNOSTIC 360 if (slot) 361 panic("uao_set_swslot: didn't create elt"); 362 #endif 363 return (0); 364 } 365 366 oldslot = UAO_SWHASH_ELT_PAGESLOT(elt, pageidx); 367 UAO_SWHASH_ELT_PAGESLOT(elt, pageidx) = slot; 368 369 /* 370 * now adjust the elt's reference counter and free it if we've 371 * dropped it to zero. 372 */ 373 374 /* an allocation? */ 375 if (slot) { 376 if (oldslot == 0) 377 elt->count++; 378 } else { /* freeing slot ... */ 379 if (oldslot) /* to be safe */ 380 elt->count--; 381 382 if (elt->count == 0) { 383 LIST_REMOVE(elt, list); 384 pool_put(&uao_swhash_elt_pool, elt); 385 } 386 } 387 388 } else { 389 /* we are using an array */ 390 oldslot = aobj->u_swslots[pageidx]; 391 aobj->u_swslots[pageidx] = slot; 392 } 393 return (oldslot); 394 } 395 396 /* 397 * end of hash/array functions 398 */ 399 400 /* 401 * uao_free: free all resources held by an aobj, and then free the aobj 402 * 403 * => the aobj should be dead 404 */ 405 static void 406 uao_free(aobj) 407 struct uvm_aobj *aobj; 408 { 409 410 simple_unlock(&aobj->u_obj.vmobjlock); 411 412 if (UAO_USES_SWHASH(aobj)) { 413 int i, hashbuckets = aobj->u_swhashmask + 1; 414 415 /* 416 * free the swslots from each hash bucket, 417 * then the hash bucket, and finally the hash table itself. 418 */ 419 for (i = 0; i < hashbuckets; i++) { 420 struct uao_swhash_elt *elt, *next; 421 422 for (elt = LIST_FIRST(&aobj->u_swhash[i]); 423 elt != NULL; 424 elt = next) { 425 int j; 426 427 for (j = 0; j < UAO_SWHASH_CLUSTER_SIZE; j++) { 428 int slot = elt->slots[j]; 429 430 if (slot) { 431 uvm_swap_free(slot, 1); 432 433 /* 434 * this page is no longer 435 * only in swap. 436 */ 437 simple_lock(&uvm.swap_data_lock); 438 uvmexp.swpgonly--; 439 simple_unlock(&uvm.swap_data_lock); 440 } 441 } 442 443 next = LIST_NEXT(elt, list); 444 pool_put(&uao_swhash_elt_pool, elt); 445 } 446 } 447 FREE(aobj->u_swhash, M_UVMAOBJ); 448 } else { 449 int i; 450 451 /* 452 * free the array 453 */ 454 455 for (i = 0; i < aobj->u_pages; i++) { 456 int slot = aobj->u_swslots[i]; 457 458 if (slot) { 459 uvm_swap_free(slot, 1); 460 461 /* this page is no longer only in swap. */ 462 simple_lock(&uvm.swap_data_lock); 463 uvmexp.swpgonly--; 464 simple_unlock(&uvm.swap_data_lock); 465 } 466 } 467 FREE(aobj->u_swslots, M_UVMAOBJ); 468 } 469 470 /* 471 * finally free the aobj itself 472 */ 473 pool_put(&uvm_aobj_pool, aobj); 474 } 475 476 /* 477 * pager functions 478 */ 479 480 /* 481 * uao_create: create an aobj of the given size and return its uvm_object. 482 * 483 * => for normal use, flags are always zero 484 * => for the kernel object, the flags are: 485 * UAO_FLAG_KERNOBJ - allocate the kernel object (can only happen once) 486 * UAO_FLAG_KERNSWAP - enable swapping of kernel object (" ") 487 */ 488 struct uvm_object * 489 uao_create(size, flags) 490 vsize_t size; 491 int flags; 492 { 493 static struct uvm_aobj kernel_object_store; /* home of kernel_object */ 494 static int kobj_alloced = 0; /* not allocated yet */ 495 int pages = round_page(size) >> PAGE_SHIFT; 496 struct uvm_aobj *aobj; 497 498 /* 499 * malloc a new aobj unless we are asked for the kernel object 500 */ 501 if (flags & UAO_FLAG_KERNOBJ) { /* want kernel object? */ 502 if (kobj_alloced) 503 panic("uao_create: kernel object already allocated"); 504 505 aobj = &kernel_object_store; 506 aobj->u_pages = pages; 507 aobj->u_flags = UAO_FLAG_NOSWAP; /* no swap to start */ 508 /* we are special, we never die */ 509 aobj->u_obj.uo_refs = UVM_OBJ_KERN; 510 kobj_alloced = UAO_FLAG_KERNOBJ; 511 } else if (flags & UAO_FLAG_KERNSWAP) { 512 aobj = &kernel_object_store; 513 if (kobj_alloced != UAO_FLAG_KERNOBJ) 514 panic("uao_create: asked to enable swap on kernel object"); 515 kobj_alloced = UAO_FLAG_KERNSWAP; 516 } else { /* normal object */ 517 aobj = pool_get(&uvm_aobj_pool, PR_WAITOK); 518 aobj->u_pages = pages; 519 aobj->u_flags = 0; /* normal object */ 520 aobj->u_obj.uo_refs = 1; /* start with 1 reference */ 521 } 522 523 /* 524 * allocate hash/array if necessary 525 * 526 * note: in the KERNSWAP case no need to worry about locking since 527 * we are still booting we should be the only thread around. 528 */ 529 if (flags == 0 || (flags & UAO_FLAG_KERNSWAP) != 0) { 530 int mflags = (flags & UAO_FLAG_KERNSWAP) != 0 ? 531 M_NOWAIT : M_WAITOK; 532 533 /* allocate hash table or array depending on object size */ 534 if (UAO_USES_SWHASH(aobj)) { 535 aobj->u_swhash = hashinit(UAO_SWHASH_BUCKETS(aobj), 536 M_UVMAOBJ, mflags, &aobj->u_swhashmask); 537 if (aobj->u_swhash == NULL) 538 panic("uao_create: hashinit swhash failed"); 539 } else { 540 MALLOC(aobj->u_swslots, int *, pages * sizeof(int), 541 M_UVMAOBJ, mflags); 542 if (aobj->u_swslots == NULL) 543 panic("uao_create: malloc swslots failed"); 544 memset(aobj->u_swslots, 0, pages * sizeof(int)); 545 } 546 547 if (flags) { 548 aobj->u_flags &= ~UAO_FLAG_NOSWAP; /* clear noswap */ 549 return(&aobj->u_obj); 550 /* done! */ 551 } 552 } 553 554 /* 555 * init aobj fields 556 */ 557 simple_lock_init(&aobj->u_obj.vmobjlock); 558 aobj->u_obj.pgops = &aobj_pager; 559 TAILQ_INIT(&aobj->u_obj.memq); 560 aobj->u_obj.uo_npages = 0; 561 562 /* 563 * now that aobj is ready, add it to the global list 564 */ 565 simple_lock(&uao_list_lock); 566 LIST_INSERT_HEAD(&uao_list, aobj, u_list); 567 simple_unlock(&uao_list_lock); 568 569 /* 570 * done! 571 */ 572 return(&aobj->u_obj); 573 } 574 575 576 577 /* 578 * uao_init: set up aobj pager subsystem 579 * 580 * => called at boot time from uvm_pager_init() 581 */ 582 void 583 uao_init() 584 { 585 static int uao_initialized; 586 587 if (uao_initialized) 588 return; 589 uao_initialized = TRUE; 590 591 LIST_INIT(&uao_list); 592 simple_lock_init(&uao_list_lock); 593 594 /* 595 * NOTE: Pages fror this pool must not come from a pageable 596 * kernel map! 597 */ 598 pool_init(&uao_swhash_elt_pool, sizeof(struct uao_swhash_elt), 599 0, 0, 0, "uaoeltpl", 0, NULL, NULL, M_UVMAOBJ); 600 601 pool_init(&uvm_aobj_pool, sizeof(struct uvm_aobj), 0, 0, 0, 602 "aobjpl", 0, 603 pool_page_alloc_nointr, pool_page_free_nointr, M_UVMAOBJ); 604 } 605 606 /* 607 * uao_reference: add a ref to an aobj 608 * 609 * => aobj must be unlocked 610 * => just lock it and call the locked version 611 */ 612 void 613 uao_reference(uobj) 614 struct uvm_object *uobj; 615 { 616 simple_lock(&uobj->vmobjlock); 617 uao_reference_locked(uobj); 618 simple_unlock(&uobj->vmobjlock); 619 } 620 621 /* 622 * uao_reference_locked: add a ref to an aobj that is already locked 623 * 624 * => aobj must be locked 625 * this needs to be separate from the normal routine 626 * since sometimes we need to add a reference to an aobj when 627 * it's already locked. 628 */ 629 void 630 uao_reference_locked(uobj) 631 struct uvm_object *uobj; 632 { 633 UVMHIST_FUNC("uao_reference"); UVMHIST_CALLED(maphist); 634 635 /* 636 * kernel_object already has plenty of references, leave it alone. 637 */ 638 639 if (UVM_OBJ_IS_KERN_OBJECT(uobj)) 640 return; 641 642 uobj->uo_refs++; /* bump! */ 643 UVMHIST_LOG(maphist, "<- done (uobj=0x%x, ref = %d)", 644 uobj, uobj->uo_refs,0,0); 645 } 646 647 648 /* 649 * uao_detach: drop a reference to an aobj 650 * 651 * => aobj must be unlocked 652 * => just lock it and call the locked version 653 */ 654 void 655 uao_detach(uobj) 656 struct uvm_object *uobj; 657 { 658 simple_lock(&uobj->vmobjlock); 659 uao_detach_locked(uobj); 660 } 661 662 663 /* 664 * uao_detach_locked: drop a reference to an aobj 665 * 666 * => aobj must be locked, and is unlocked (or freed) upon return. 667 * this needs to be separate from the normal routine 668 * since sometimes we need to detach from an aobj when 669 * it's already locked. 670 */ 671 void 672 uao_detach_locked(uobj) 673 struct uvm_object *uobj; 674 { 675 struct uvm_aobj *aobj = (struct uvm_aobj *)uobj; 676 struct vm_page *pg; 677 boolean_t busybody; 678 UVMHIST_FUNC("uao_detach"); UVMHIST_CALLED(maphist); 679 680 /* 681 * detaching from kernel_object is a noop. 682 */ 683 if (UVM_OBJ_IS_KERN_OBJECT(uobj)) { 684 simple_unlock(&uobj->vmobjlock); 685 return; 686 } 687 688 UVMHIST_LOG(maphist," (uobj=0x%x) ref=%d", uobj,uobj->uo_refs,0,0); 689 uobj->uo_refs--; /* drop ref! */ 690 if (uobj->uo_refs) { /* still more refs? */ 691 simple_unlock(&uobj->vmobjlock); 692 UVMHIST_LOG(maphist, "<- done (rc>0)", 0,0,0,0); 693 return; 694 } 695 696 /* 697 * remove the aobj from the global list. 698 */ 699 simple_lock(&uao_list_lock); 700 LIST_REMOVE(aobj, u_list); 701 simple_unlock(&uao_list_lock); 702 703 /* 704 * free all the pages that aren't PG_BUSY, 705 * mark for release any that are. 706 */ 707 busybody = FALSE; 708 for (pg = TAILQ_FIRST(&uobj->memq); 709 pg != NULL; 710 pg = TAILQ_NEXT(pg, listq)) { 711 if (pg->flags & PG_BUSY) { 712 pg->flags |= PG_RELEASED; 713 busybody = TRUE; 714 continue; 715 } 716 717 /* zap the mappings, free the swap slot, free the page */ 718 pmap_page_protect(pg, VM_PROT_NONE); 719 uao_dropswap(&aobj->u_obj, pg->offset >> PAGE_SHIFT); 720 uvm_lock_pageq(); 721 uvm_pagefree(pg); 722 uvm_unlock_pageq(); 723 } 724 725 /* 726 * if we found any busy pages, we're done for now. 727 * mark the aobj for death, releasepg will finish up for us. 728 */ 729 if (busybody) { 730 aobj->u_flags |= UAO_FLAG_KILLME; 731 simple_unlock(&aobj->u_obj.vmobjlock); 732 return; 733 } 734 735 /* 736 * finally, free the rest. 737 */ 738 uao_free(aobj); 739 } 740 741 /* 742 * uao_flush: "flush" pages out of a uvm object 743 * 744 * => object should be locked by caller. we may _unlock_ the object 745 * if (and only if) we need to clean a page (PGO_CLEANIT). 746 * XXXJRT Currently, however, we don't. In the case of cleaning 747 * XXXJRT a page, we simply just deactivate it. Should probably 748 * XXXJRT handle this better, in the future (although "flushing" 749 * XXXJRT anonymous memory isn't terribly important). 750 * => if PGO_CLEANIT is not set, then we will neither unlock the object 751 * or block. 752 * => if PGO_ALLPAGE is set, then all pages in the object are valid targets 753 * for flushing. 754 * => NOTE: we rely on the fact that the object's memq is a TAILQ and 755 * that new pages are inserted on the tail end of the list. thus, 756 * we can make a complete pass through the object in one go by starting 757 * at the head and working towards the tail (new pages are put in 758 * front of us). 759 * => NOTE: we are allowed to lock the page queues, so the caller 760 * must not be holding the lock on them [e.g. pagedaemon had 761 * better not call us with the queues locked] 762 * => we return TRUE unless we encountered some sort of I/O error 763 * XXXJRT currently never happens, as we never directly initiate 764 * XXXJRT I/O 765 * 766 * comment on "cleaning" object and PG_BUSY pages: 767 * this routine is holding the lock on the object. the only time 768 * that is can run into a PG_BUSY page that it does not own is if 769 * some other process has started I/O on the page (e.g. either 770 * a pagein or a pageout). if the PG_BUSY page is being paged 771 * in, then it can not be dirty (!PG_CLEAN) because no one has 772 * had a change to modify it yet. if the PG_BUSY page is being 773 * paged out then it means that someone else has already started 774 * cleaning the page for us (how nice!). in this case, if we 775 * have syncio specified, then after we make our pass through the 776 * object we need to wait for the other PG_BUSY pages to clear 777 * off (i.e. we need to do an iosync). also note that once a 778 * page is PG_BUSY is must stary in its object until it is un-busyed. 779 * XXXJRT We never actually do this, as we are "flushing" anonymous 780 * XXXJRT memory, which doesn't have persistent backing store. 781 * 782 * note on page traversal: 783 * we can traverse the pages in an object either by going down the 784 * linked list in "uobj->memq", or we can go over the address range 785 * by page doing hash table lookups for each address. depending 786 * on how many pages are in the object it may be cheaper to do one 787 * or the other. we set "by_list" to true if we are using memq. 788 * if the cost of a hash lookup was equal to the cost of the list 789 * traversal we could compare the number of pages in the start->stop 790 * range to the total number of pages in the object. however, it 791 * seems that a hash table lookup is more expensive than the linked 792 * list traversal, so we multiply the number of pages in the 793 * start->stop range by a penalty which we define below. 794 */ 795 796 #define UAO_HASH_PENALTY 4 /* XXX: a guess */ 797 798 boolean_t 799 uao_flush(uobj, start, stop, flags) 800 struct uvm_object *uobj; 801 vaddr_t start, stop; 802 int flags; 803 { 804 struct uvm_aobj *aobj = (struct uvm_aobj *) uobj; 805 struct vm_page *pp, *ppnext; 806 boolean_t retval, by_list; 807 vaddr_t curoff; 808 UVMHIST_FUNC("uao_flush"); UVMHIST_CALLED(maphist); 809 810 curoff = 0; /* XXX: shut up gcc */ 811 812 retval = TRUE; /* default to success */ 813 814 if (flags & PGO_ALLPAGES) { 815 start = 0; 816 stop = aobj->u_pages << PAGE_SHIFT; 817 by_list = TRUE; /* always go by the list */ 818 } else { 819 start = trunc_page(start); 820 stop = round_page(stop); 821 if (stop > (aobj->u_pages << PAGE_SHIFT)) { 822 printf("uao_flush: strange, got an out of range " 823 "flush (fixed)\n"); 824 stop = aobj->u_pages << PAGE_SHIFT; 825 } 826 by_list = (uobj->uo_npages <= 827 ((stop - start) >> PAGE_SHIFT) * UAO_HASH_PENALTY); 828 } 829 830 UVMHIST_LOG(maphist, 831 " flush start=0x%lx, stop=0x%x, by_list=%d, flags=0x%x", 832 start, stop, by_list, flags); 833 834 /* 835 * Don't need to do any work here if we're not freeing 836 * or deactivating pages. 837 */ 838 if ((flags & (PGO_DEACTIVATE|PGO_FREE)) == 0) { 839 UVMHIST_LOG(maphist, 840 "<- done (no work to do)",0,0,0,0); 841 return (retval); 842 } 843 844 /* 845 * now do it. note: we must update ppnext in the body of loop or we 846 * will get stuck. we need to use ppnext because we may free "pp" 847 * before doing the next loop. 848 */ 849 850 if (by_list) { 851 pp = uobj->memq.tqh_first; 852 } else { 853 curoff = start; 854 pp = uvm_pagelookup(uobj, curoff); 855 } 856 857 ppnext = NULL; /* XXX: shut up gcc */ 858 uvm_lock_pageq(); /* page queues locked */ 859 860 /* locked: both page queues and uobj */ 861 for ( ; (by_list && pp != NULL) || 862 (!by_list && curoff < stop) ; pp = ppnext) { 863 if (by_list) { 864 ppnext = pp->listq.tqe_next; 865 866 /* range check */ 867 if (pp->offset < start || pp->offset >= stop) 868 continue; 869 } else { 870 curoff += PAGE_SIZE; 871 if (curoff < stop) 872 ppnext = uvm_pagelookup(uobj, curoff); 873 874 /* null check */ 875 if (pp == NULL) 876 continue; 877 } 878 879 switch (flags & (PGO_CLEANIT|PGO_FREE|PGO_DEACTIVATE)) { 880 /* 881 * XXX In these first 3 cases, we always just 882 * XXX deactivate the page. We may want to 883 * XXX handle the different cases more specifically 884 * XXX in the future. 885 */ 886 case PGO_CLEANIT|PGO_FREE: 887 case PGO_CLEANIT|PGO_DEACTIVATE: 888 case PGO_DEACTIVATE: 889 deactivate_it: 890 /* skip the page if it's loaned or wired */ 891 if (pp->loan_count != 0 || 892 pp->wire_count != 0) 893 continue; 894 895 /* zap all mappings for the page. */ 896 pmap_page_protect(pp, VM_PROT_NONE); 897 898 /* ...and deactivate the page. */ 899 uvm_pagedeactivate(pp); 900 901 continue; 902 903 case PGO_FREE: 904 /* 905 * If there are multiple references to 906 * the object, just deactivate the page. 907 */ 908 if (uobj->uo_refs > 1) 909 goto deactivate_it; 910 911 /* XXX skip the page if it's loaned or wired */ 912 if (pp->loan_count != 0 || 913 pp->wire_count != 0) 914 continue; 915 916 /* 917 * mark the page as released if its busy. 918 */ 919 if (pp->flags & PG_BUSY) { 920 pp->flags |= PG_RELEASED; 921 continue; 922 } 923 924 /* zap all mappings for the page. */ 925 pmap_page_protect(pp, VM_PROT_NONE); 926 927 uao_dropswap(uobj, pp->offset >> PAGE_SHIFT); 928 uvm_pagefree(pp); 929 930 continue; 931 932 default: 933 panic("uao_flush: weird flags"); 934 } 935 #ifdef DIAGNOSTIC 936 panic("uao_flush: unreachable code"); 937 #endif 938 } 939 940 uvm_unlock_pageq(); 941 942 UVMHIST_LOG(maphist, 943 "<- done, rv=%d",retval,0,0,0); 944 return (retval); 945 } 946 947 /* 948 * uao_get: fetch me a page 949 * 950 * we have three cases: 951 * 1: page is resident -> just return the page. 952 * 2: page is zero-fill -> allocate a new page and zero it. 953 * 3: page is swapped out -> fetch the page from swap. 954 * 955 * cases 1 and 2 can be handled with PGO_LOCKED, case 3 cannot. 956 * so, if the "center" page hits case 3 (or any page, with PGO_ALLPAGES), 957 * then we will need to return VM_PAGER_UNLOCK. 958 * 959 * => prefer map unlocked (not required) 960 * => object must be locked! we will _unlock_ it before starting any I/O. 961 * => flags: PGO_ALLPAGES: get all of the pages 962 * PGO_LOCKED: fault data structures are locked 963 * => NOTE: offset is the offset of pps[0], _NOT_ pps[centeridx] 964 * => NOTE: caller must check for released pages!! 965 */ 966 static int 967 uao_get(uobj, offset, pps, npagesp, centeridx, access_type, advice, flags) 968 struct uvm_object *uobj; 969 vaddr_t offset; 970 struct vm_page **pps; 971 int *npagesp; 972 int centeridx, advice, flags; 973 vm_prot_t access_type; 974 { 975 struct uvm_aobj *aobj = (struct uvm_aobj *)uobj; 976 vaddr_t current_offset; 977 vm_page_t ptmp; 978 int lcv, gotpages, maxpages, swslot, rv, pageidx; 979 boolean_t done; 980 UVMHIST_FUNC("uao_get"); UVMHIST_CALLED(pdhist); 981 982 UVMHIST_LOG(pdhist, "aobj=%p offset=%d, flags=%d", 983 aobj, offset, flags,0); 984 985 /* 986 * get number of pages 987 */ 988 maxpages = *npagesp; 989 990 /* 991 * step 1: handled the case where fault data structures are locked. 992 */ 993 994 if (flags & PGO_LOCKED) { 995 /* 996 * step 1a: get pages that are already resident. only do 997 * this if the data structures are locked (i.e. the first 998 * time through). 999 */ 1000 1001 done = TRUE; /* be optimistic */ 1002 gotpages = 0; /* # of pages we got so far */ 1003 1004 for (lcv = 0, current_offset = offset ; lcv < maxpages ; 1005 lcv++, current_offset += PAGE_SIZE) { 1006 /* do we care about this page? if not, skip it */ 1007 if (pps[lcv] == PGO_DONTCARE) 1008 continue; 1009 1010 ptmp = uvm_pagelookup(uobj, current_offset); 1011 1012 /* 1013 * if page is new, attempt to allocate the page, then 1014 * zero-fill it. 1015 */ 1016 if (ptmp == NULL && uao_find_swslot(aobj, 1017 current_offset >> PAGE_SHIFT) == 0) { 1018 ptmp = uvm_pagealloc(uobj, current_offset, 1019 NULL, 0); 1020 if (ptmp) { 1021 /* new page */ 1022 ptmp->flags &= ~(PG_BUSY|PG_FAKE); 1023 ptmp->pqflags |= PQ_AOBJ; 1024 UVM_PAGE_OWN(ptmp, NULL); 1025 uvm_pagezero(ptmp); 1026 } 1027 } 1028 1029 /* 1030 * to be useful must get a non-busy, non-released page 1031 */ 1032 if (ptmp == NULL || 1033 (ptmp->flags & (PG_BUSY|PG_RELEASED)) != 0) { 1034 if (lcv == centeridx || 1035 (flags & PGO_ALLPAGES) != 0) 1036 /* need to do a wait or I/O! */ 1037 done = FALSE; 1038 continue; 1039 } 1040 1041 /* 1042 * useful page: busy/lock it and plug it in our 1043 * result array 1044 */ 1045 /* caller must un-busy this page */ 1046 ptmp->flags |= PG_BUSY; 1047 UVM_PAGE_OWN(ptmp, "uao_get1"); 1048 pps[lcv] = ptmp; 1049 gotpages++; 1050 1051 } /* "for" lcv loop */ 1052 1053 /* 1054 * step 1b: now we've either done everything needed or we 1055 * to unlock and do some waiting or I/O. 1056 */ 1057 1058 UVMHIST_LOG(pdhist, "<- done (done=%d)", done, 0,0,0); 1059 1060 *npagesp = gotpages; 1061 if (done) 1062 /* bingo! */ 1063 return(VM_PAGER_OK); 1064 else 1065 /* EEK! Need to unlock and I/O */ 1066 return(VM_PAGER_UNLOCK); 1067 } 1068 1069 /* 1070 * step 2: get non-resident or busy pages. 1071 * object is locked. data structures are unlocked. 1072 */ 1073 1074 for (lcv = 0, current_offset = offset ; lcv < maxpages ; 1075 lcv++, current_offset += PAGE_SIZE) { 1076 1077 /* 1078 * - skip over pages we've already gotten or don't want 1079 * - skip over pages we don't _have_ to get 1080 */ 1081 1082 if (pps[lcv] != NULL || 1083 (lcv != centeridx && (flags & PGO_ALLPAGES) == 0)) 1084 continue; 1085 1086 pageidx = current_offset >> PAGE_SHIFT; 1087 1088 /* 1089 * we have yet to locate the current page (pps[lcv]). we 1090 * first look for a page that is already at the current offset. 1091 * if we find a page, we check to see if it is busy or 1092 * released. if that is the case, then we sleep on the page 1093 * until it is no longer busy or released and repeat the lookup. 1094 * if the page we found is neither busy nor released, then we 1095 * busy it (so we own it) and plug it into pps[lcv]. this 1096 * 'break's the following while loop and indicates we are 1097 * ready to move on to the next page in the "lcv" loop above. 1098 * 1099 * if we exit the while loop with pps[lcv] still set to NULL, 1100 * then it means that we allocated a new busy/fake/clean page 1101 * ptmp in the object and we need to do I/O to fill in the data. 1102 */ 1103 1104 /* top of "pps" while loop */ 1105 while (pps[lcv] == NULL) { 1106 /* look for a resident page */ 1107 ptmp = uvm_pagelookup(uobj, current_offset); 1108 1109 /* not resident? allocate one now (if we can) */ 1110 if (ptmp == NULL) { 1111 1112 ptmp = uvm_pagealloc(uobj, current_offset, 1113 NULL, 0); 1114 1115 /* out of RAM? */ 1116 if (ptmp == NULL) { 1117 simple_unlock(&uobj->vmobjlock); 1118 UVMHIST_LOG(pdhist, 1119 "sleeping, ptmp == NULL\n",0,0,0,0); 1120 uvm_wait("uao_getpage"); 1121 simple_lock(&uobj->vmobjlock); 1122 /* goto top of pps while loop */ 1123 continue; 1124 } 1125 1126 /* 1127 * safe with PQ's unlocked: because we just 1128 * alloc'd the page 1129 */ 1130 ptmp->pqflags |= PQ_AOBJ; 1131 1132 /* 1133 * got new page ready for I/O. break pps while 1134 * loop. pps[lcv] is still NULL. 1135 */ 1136 break; 1137 } 1138 1139 /* page is there, see if we need to wait on it */ 1140 if ((ptmp->flags & (PG_BUSY|PG_RELEASED)) != 0) { 1141 ptmp->flags |= PG_WANTED; 1142 UVMHIST_LOG(pdhist, 1143 "sleeping, ptmp->flags 0x%x\n", 1144 ptmp->flags,0,0,0); 1145 UVM_UNLOCK_AND_WAIT(ptmp, &uobj->vmobjlock, 1146 FALSE, "uao_get", 0); 1147 simple_lock(&uobj->vmobjlock); 1148 continue; /* goto top of pps while loop */ 1149 } 1150 1151 /* 1152 * if we get here then the page has become resident and 1153 * unbusy between steps 1 and 2. we busy it now (so we 1154 * own it) and set pps[lcv] (so that we exit the while 1155 * loop). 1156 */ 1157 /* we own it, caller must un-busy */ 1158 ptmp->flags |= PG_BUSY; 1159 UVM_PAGE_OWN(ptmp, "uao_get2"); 1160 pps[lcv] = ptmp; 1161 } 1162 1163 /* 1164 * if we own the valid page at the correct offset, pps[lcv] will 1165 * point to it. nothing more to do except go to the next page. 1166 */ 1167 if (pps[lcv]) 1168 continue; /* next lcv */ 1169 1170 /* 1171 * we have a "fake/busy/clean" page that we just allocated. 1172 * do the needed "i/o", either reading from swap or zeroing. 1173 */ 1174 swslot = uao_find_swslot(aobj, pageidx); 1175 1176 /* 1177 * just zero the page if there's nothing in swap. 1178 */ 1179 if (swslot == 0) 1180 { 1181 /* 1182 * page hasn't existed before, just zero it. 1183 */ 1184 uvm_pagezero(ptmp); 1185 } else { 1186 UVMHIST_LOG(pdhist, "pagein from swslot %d", 1187 swslot, 0,0,0); 1188 1189 /* 1190 * page in the swapped-out page. 1191 * unlock object for i/o, relock when done. 1192 */ 1193 simple_unlock(&uobj->vmobjlock); 1194 rv = uvm_swap_get(ptmp, swslot, PGO_SYNCIO); 1195 simple_lock(&uobj->vmobjlock); 1196 1197 /* 1198 * I/O done. check for errors. 1199 */ 1200 if (rv != VM_PAGER_OK) 1201 { 1202 UVMHIST_LOG(pdhist, "<- done (error=%d)", 1203 rv,0,0,0); 1204 if (ptmp->flags & PG_WANTED) 1205 wakeup(ptmp); 1206 1207 /* 1208 * remove the swap slot from the aobj 1209 * and mark the aobj as having no real slot. 1210 * don't free the swap slot, thus preventing 1211 * it from being used again. 1212 */ 1213 swslot = uao_set_swslot(&aobj->u_obj, pageidx, 1214 SWSLOT_BAD); 1215 uvm_swap_markbad(swslot, 1); 1216 1217 ptmp->flags &= ~(PG_WANTED|PG_BUSY); 1218 UVM_PAGE_OWN(ptmp, NULL); 1219 uvm_lock_pageq(); 1220 uvm_pagefree(ptmp); 1221 uvm_unlock_pageq(); 1222 1223 simple_unlock(&uobj->vmobjlock); 1224 return (rv); 1225 } 1226 } 1227 1228 /* 1229 * we got the page! clear the fake flag (indicates valid 1230 * data now in page) and plug into our result array. note 1231 * that page is still busy. 1232 * 1233 * it is the callers job to: 1234 * => check if the page is released 1235 * => unbusy the page 1236 * => activate the page 1237 */ 1238 1239 ptmp->flags &= ~PG_FAKE; /* data is valid ... */ 1240 pmap_clear_modify(ptmp); /* ... and clean */ 1241 pps[lcv] = ptmp; 1242 1243 } /* lcv loop */ 1244 1245 /* 1246 * finally, unlock object and return. 1247 */ 1248 1249 simple_unlock(&uobj->vmobjlock); 1250 UVMHIST_LOG(pdhist, "<- done (OK)",0,0,0,0); 1251 return(VM_PAGER_OK); 1252 } 1253 1254 /* 1255 * uao_releasepg: handle released page in an aobj 1256 * 1257 * => "pg" is a PG_BUSY [caller owns it], PG_RELEASED page that we need 1258 * to dispose of. 1259 * => caller must handle PG_WANTED case 1260 * => called with page's object locked, pageq's unlocked 1261 * => returns TRUE if page's object is still alive, FALSE if we 1262 * killed the page's object. if we return TRUE, then we 1263 * return with the object locked. 1264 * => if (nextpgp != NULL) => we return pageq.tqe_next here, and return 1265 * with the page queues locked [for pagedaemon] 1266 * => if (nextpgp == NULL) => we return with page queues unlocked [normal case] 1267 * => we kill the aobj if it is not referenced and we are suppose to 1268 * kill it ("KILLME"). 1269 */ 1270 static boolean_t 1271 uao_releasepg(pg, nextpgp) 1272 struct vm_page *pg; 1273 struct vm_page **nextpgp; /* OUT */ 1274 { 1275 struct uvm_aobj *aobj = (struct uvm_aobj *) pg->uobject; 1276 1277 #ifdef DIAGNOSTIC 1278 if ((pg->flags & PG_RELEASED) == 0) 1279 panic("uao_releasepg: page not released!"); 1280 #endif 1281 1282 /* 1283 * dispose of the page [caller handles PG_WANTED] and swap slot. 1284 */ 1285 pmap_page_protect(pg, VM_PROT_NONE); 1286 uao_dropswap(&aobj->u_obj, pg->offset >> PAGE_SHIFT); 1287 uvm_lock_pageq(); 1288 if (nextpgp) 1289 *nextpgp = pg->pageq.tqe_next; /* next page for daemon */ 1290 uvm_pagefree(pg); 1291 if (!nextpgp) 1292 uvm_unlock_pageq(); /* keep locked for daemon */ 1293 1294 /* 1295 * if we're not killing the object, we're done. 1296 */ 1297 if ((aobj->u_flags & UAO_FLAG_KILLME) == 0) 1298 return TRUE; 1299 1300 #ifdef DIAGNOSTIC 1301 if (aobj->u_obj.uo_refs) 1302 panic("uvm_km_releasepg: kill flag set on referenced object!"); 1303 #endif 1304 1305 /* 1306 * if there are still pages in the object, we're done for now. 1307 */ 1308 if (aobj->u_obj.uo_npages != 0) 1309 return TRUE; 1310 1311 #ifdef DIAGNOSTIC 1312 if (TAILQ_FIRST(&aobj->u_obj.memq)) 1313 panic("uvn_releasepg: pages in object with npages == 0"); 1314 #endif 1315 1316 /* 1317 * finally, free the rest. 1318 */ 1319 uao_free(aobj); 1320 1321 return FALSE; 1322 } 1323 1324 1325 /* 1326 * uao_dropswap: release any swap resources from this aobj page. 1327 * 1328 * => aobj must be locked or have a reference count of 0. 1329 */ 1330 1331 void 1332 uao_dropswap(uobj, pageidx) 1333 struct uvm_object *uobj; 1334 int pageidx; 1335 { 1336 int slot; 1337 1338 slot = uao_set_swslot(uobj, pageidx, 0); 1339 if (slot) { 1340 uvm_swap_free(slot, 1); 1341 } 1342 } 1343 1344 1345 /* 1346 * page in every page in every aobj that is paged-out to a range of swslots. 1347 * 1348 * => nothing should be locked. 1349 * => returns TRUE if pagein was aborted due to lack of memory. 1350 */ 1351 boolean_t 1352 uao_swap_off(startslot, endslot) 1353 int startslot, endslot; 1354 { 1355 struct uvm_aobj *aobj, *nextaobj; 1356 1357 /* 1358 * walk the list of all aobjs. 1359 */ 1360 1361 restart: 1362 simple_lock(&uao_list_lock); 1363 1364 for (aobj = LIST_FIRST(&uao_list); 1365 aobj != NULL; 1366 aobj = nextaobj) { 1367 boolean_t rv; 1368 1369 /* 1370 * try to get the object lock, 1371 * start all over if we fail. 1372 * most of the time we'll get the aobj lock, 1373 * so this should be a rare case. 1374 */ 1375 if (!simple_lock_try(&aobj->u_obj.vmobjlock)) { 1376 simple_unlock(&uao_list_lock); 1377 goto restart; 1378 } 1379 1380 /* 1381 * add a ref to the aobj so it doesn't disappear 1382 * while we're working. 1383 */ 1384 uao_reference_locked(&aobj->u_obj); 1385 1386 /* 1387 * now it's safe to unlock the uao list. 1388 */ 1389 simple_unlock(&uao_list_lock); 1390 1391 /* 1392 * page in any pages in the swslot range. 1393 * if there's an error, abort and return the error. 1394 */ 1395 rv = uao_pagein(aobj, startslot, endslot); 1396 if (rv) { 1397 uao_detach_locked(&aobj->u_obj); 1398 return rv; 1399 } 1400 1401 /* 1402 * we're done with this aobj. 1403 * relock the list and drop our ref on the aobj. 1404 */ 1405 simple_lock(&uao_list_lock); 1406 nextaobj = LIST_NEXT(aobj, u_list); 1407 uao_detach_locked(&aobj->u_obj); 1408 } 1409 1410 /* 1411 * done with traversal, unlock the list 1412 */ 1413 simple_unlock(&uao_list_lock); 1414 return FALSE; 1415 } 1416 1417 1418 /* 1419 * page in any pages from aobj in the given range. 1420 * 1421 * => aobj must be locked and is returned locked. 1422 * => returns TRUE if pagein was aborted due to lack of memory. 1423 */ 1424 static boolean_t 1425 uao_pagein(aobj, startslot, endslot) 1426 struct uvm_aobj *aobj; 1427 int startslot, endslot; 1428 { 1429 boolean_t rv; 1430 1431 if (UAO_USES_SWHASH(aobj)) { 1432 struct uao_swhash_elt *elt; 1433 int bucket; 1434 1435 restart: 1436 for (bucket = aobj->u_swhashmask; bucket >= 0; bucket--) { 1437 for (elt = LIST_FIRST(&aobj->u_swhash[bucket]); 1438 elt != NULL; 1439 elt = LIST_NEXT(elt, list)) { 1440 int i; 1441 1442 for (i = 0; i < UAO_SWHASH_CLUSTER_SIZE; i++) { 1443 int slot = elt->slots[i]; 1444 1445 /* 1446 * if the slot isn't in range, skip it. 1447 */ 1448 if (slot < startslot || 1449 slot >= endslot) { 1450 continue; 1451 } 1452 1453 /* 1454 * process the page, 1455 * the start over on this object 1456 * since the swhash elt 1457 * may have been freed. 1458 */ 1459 rv = uao_pagein_page(aobj, 1460 UAO_SWHASH_ELT_PAGEIDX_BASE(elt) + i); 1461 if (rv) { 1462 return rv; 1463 } 1464 goto restart; 1465 } 1466 } 1467 } 1468 } else { 1469 int i; 1470 1471 for (i = 0; i < aobj->u_pages; i++) { 1472 int slot = aobj->u_swslots[i]; 1473 1474 /* 1475 * if the slot isn't in range, skip it 1476 */ 1477 if (slot < startslot || slot >= endslot) { 1478 continue; 1479 } 1480 1481 /* 1482 * process the page. 1483 */ 1484 rv = uao_pagein_page(aobj, i); 1485 if (rv) { 1486 return rv; 1487 } 1488 } 1489 } 1490 1491 return FALSE; 1492 } 1493 1494 /* 1495 * page in a page from an aobj. used for swap_off. 1496 * returns TRUE if pagein was aborted due to lack of memory. 1497 * 1498 * => aobj must be locked and is returned locked. 1499 */ 1500 static boolean_t 1501 uao_pagein_page(aobj, pageidx) 1502 struct uvm_aobj *aobj; 1503 int pageidx; 1504 { 1505 struct vm_page *pg; 1506 int rv, slot, npages; 1507 UVMHIST_FUNC("uao_pagein_page"); UVMHIST_CALLED(pdhist); 1508 1509 pg = NULL; 1510 npages = 1; 1511 /* locked: aobj */ 1512 rv = uao_get(&aobj->u_obj, pageidx << PAGE_SHIFT, 1513 &pg, &npages, 0, VM_PROT_READ|VM_PROT_WRITE, 0, 0); 1514 /* unlocked: aobj */ 1515 1516 /* 1517 * relock and finish up. 1518 */ 1519 simple_lock(&aobj->u_obj.vmobjlock); 1520 1521 switch (rv) { 1522 case VM_PAGER_OK: 1523 break; 1524 1525 case VM_PAGER_ERROR: 1526 case VM_PAGER_REFAULT: 1527 /* 1528 * nothing more to do on errors. 1529 * VM_PAGER_REFAULT can only mean that the anon was freed, 1530 * so again there's nothing to do. 1531 */ 1532 return FALSE; 1533 1534 #ifdef DIAGNOSTIC 1535 default: 1536 panic("uao_pagein_page: uao_get -> %d\n", rv); 1537 #endif 1538 } 1539 1540 #ifdef DIAGNOSTIC 1541 /* 1542 * this should never happen, since we have a reference on the aobj. 1543 */ 1544 if (pg->flags & PG_RELEASED) { 1545 panic("uao_pagein_page: found PG_RELEASED page?\n"); 1546 } 1547 #endif 1548 1549 /* 1550 * ok, we've got the page now. 1551 * mark it as dirty, clear its swslot and un-busy it. 1552 */ 1553 slot = uao_set_swslot(&aobj->u_obj, pageidx, 0); 1554 uvm_swap_free(slot, 1); 1555 pg->flags &= ~(PG_BUSY|PG_CLEAN|PG_FAKE); 1556 UVM_PAGE_OWN(pg, NULL); 1557 1558 /* 1559 * deactivate the page (to put it on a page queue). 1560 */ 1561 pmap_clear_reference(pg); 1562 pmap_page_protect(pg, VM_PROT_NONE); 1563 uvm_lock_pageq(); 1564 uvm_pagedeactivate(pg); 1565 uvm_unlock_pageq(); 1566 1567 return FALSE; 1568 } 1569