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