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