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