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