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