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