1 /* $OpenBSD: uvm_aobj.c,v 1.70 2014/11/16 12:31:00 deraadt 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/kernel.h> 46 #include <sys/stdint.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 = malloc(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 = malloc(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 = malloc(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 = malloc(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), 801 0, 0, 0, "uaoeltpl", &pool_allocator_nointr); 802 803 pool_init(&uvm_aobj_pool, sizeof(struct uvm_aobj), 0, 0, 0, 804 "aobjpl", &pool_allocator_nointr); 805 } 806 807 /* 808 * uao_reference: add a ref to an aobj 809 */ 810 void 811 uao_reference(struct uvm_object *uobj) 812 { 813 uao_reference_locked(uobj); 814 } 815 816 /* 817 * uao_reference_locked: add a ref to an aobj 818 */ 819 void 820 uao_reference_locked(struct uvm_object *uobj) 821 { 822 823 /* kernel_object already has plenty of references, leave it alone. */ 824 if (UVM_OBJ_IS_KERN_OBJECT(uobj)) 825 return; 826 827 uobj->uo_refs++; /* bump! */ 828 } 829 830 831 /* 832 * uao_detach: drop a reference to an aobj 833 */ 834 void 835 uao_detach(struct uvm_object *uobj) 836 { 837 uao_detach_locked(uobj); 838 } 839 840 841 /* 842 * uao_detach_locked: drop a reference to an aobj 843 * 844 * => aobj may freed upon return. 845 */ 846 void 847 uao_detach_locked(struct uvm_object *uobj) 848 { 849 struct uvm_aobj *aobj = (struct uvm_aobj *)uobj; 850 struct vm_page *pg; 851 852 /* detaching from kernel_object is a noop. */ 853 if (UVM_OBJ_IS_KERN_OBJECT(uobj)) { 854 return; 855 } 856 857 uobj->uo_refs--; /* drop ref! */ 858 if (uobj->uo_refs) { /* still more refs? */ 859 return; 860 } 861 862 /* remove the aobj from the global list. */ 863 mtx_enter(&uao_list_lock); 864 LIST_REMOVE(aobj, u_list); 865 mtx_leave(&uao_list_lock); 866 867 /* 868 * Free all pages left in the object. If they're busy, wait 869 * for them to become available before we kill it. 870 * Release swap resources then free the page. 871 */ 872 uvm_lock_pageq(); 873 while((pg = RB_ROOT(&uobj->memt)) != NULL) { 874 if (pg->pg_flags & PG_BUSY) { 875 atomic_setbits_int(&pg->pg_flags, PG_WANTED); 876 uvm_unlock_pageq(); 877 UVM_WAIT(pg, 0, "uao_det", 0); 878 uvm_lock_pageq(); 879 continue; 880 } 881 pmap_page_protect(pg, PROT_NONE); 882 uao_dropswap(&aobj->u_obj, pg->offset >> PAGE_SHIFT); 883 uvm_pagefree(pg); 884 } 885 uvm_unlock_pageq(); 886 887 /* finally, free the rest. */ 888 uao_free(aobj); 889 } 890 891 /* 892 * uao_flush: "flush" pages out of a uvm object 893 * 894 * => if PGO_CLEANIT is not set, then we will not block. 895 * => if PGO_ALLPAGE is set, then all pages in the object are valid targets 896 * for flushing. 897 * => NOTE: we are allowed to lock the page queues, so the caller 898 * must not be holding the lock on them [e.g. pagedaemon had 899 * better not call us with the queues locked] 900 * => we return TRUE unless we encountered some sort of I/O error 901 * XXXJRT currently never happens, as we never directly initiate 902 * XXXJRT I/O 903 */ 904 905 #define UAO_HASH_PENALTY 4 /* XXX: a guess */ 906 907 boolean_t 908 uao_flush(struct uvm_object *uobj, voff_t start, voff_t stop, int flags) 909 { 910 struct uvm_aobj *aobj = (struct uvm_aobj *) uobj; 911 struct vm_page *pp; 912 voff_t curoff; 913 914 if (flags & PGO_ALLPAGES) { 915 start = 0; 916 stop = (voff_t)aobj->u_pages << PAGE_SHIFT; 917 } else { 918 start = trunc_page(start); 919 stop = round_page(stop); 920 if (stop > ((voff_t)aobj->u_pages << PAGE_SHIFT)) { 921 printf("uao_flush: strange, got an out of range " 922 "flush (fixed)\n"); 923 stop = (voff_t)aobj->u_pages << PAGE_SHIFT; 924 } 925 } 926 927 /* 928 * Don't need to do any work here if we're not freeing 929 * or deactivating pages. 930 */ 931 if ((flags & (PGO_DEACTIVATE|PGO_FREE)) == 0) 932 return (TRUE); 933 934 curoff = start; 935 for (;;) { 936 if (curoff < stop) { 937 pp = uvm_pagelookup(uobj, curoff); 938 curoff += PAGE_SIZE; 939 if (pp == NULL) 940 continue; 941 } else { 942 break; 943 } 944 945 /* Make sure page is unbusy, else wait for it. */ 946 if (pp->pg_flags & PG_BUSY) { 947 atomic_setbits_int(&pp->pg_flags, PG_WANTED); 948 UVM_WAIT(pp, 0, "uaoflsh", 0); 949 curoff -= PAGE_SIZE; 950 continue; 951 } 952 953 switch (flags & (PGO_CLEANIT|PGO_FREE|PGO_DEACTIVATE)) { 954 /* 955 * XXX In these first 3 cases, we always just 956 * XXX deactivate the page. We may want to 957 * XXX handle the different cases more specifically 958 * XXX in the future. 959 */ 960 case PGO_CLEANIT|PGO_FREE: 961 /* FALLTHROUGH */ 962 case PGO_CLEANIT|PGO_DEACTIVATE: 963 /* FALLTHROUGH */ 964 case PGO_DEACTIVATE: 965 deactivate_it: 966 /* skip the page if it's loaned or wired */ 967 if (pp->loan_count != 0 || 968 pp->wire_count != 0) 969 continue; 970 971 uvm_lock_pageq(); 972 /* zap all mappings for the page. */ 973 pmap_page_protect(pp, PROT_NONE); 974 975 /* ...and deactivate the page. */ 976 uvm_pagedeactivate(pp); 977 uvm_unlock_pageq(); 978 979 continue; 980 case PGO_FREE: 981 /* 982 * If there are multiple references to 983 * the object, just deactivate the page. 984 */ 985 if (uobj->uo_refs > 1) 986 goto deactivate_it; 987 988 /* XXX skip the page if it's loaned or wired */ 989 if (pp->loan_count != 0 || 990 pp->wire_count != 0) 991 continue; 992 993 /* zap all mappings for the page. */ 994 pmap_page_protect(pp, PROT_NONE); 995 996 uao_dropswap(uobj, pp->offset >> PAGE_SHIFT); 997 uvm_lock_pageq(); 998 uvm_pagefree(pp); 999 uvm_unlock_pageq(); 1000 1001 continue; 1002 default: 1003 panic("uao_flush: weird flags"); 1004 } 1005 } 1006 1007 return (TRUE); 1008 } 1009 1010 /* 1011 * uao_get: fetch me a page 1012 * 1013 * we have three cases: 1014 * 1: page is resident -> just return the page. 1015 * 2: page is zero-fill -> allocate a new page and zero it. 1016 * 3: page is swapped out -> fetch the page from swap. 1017 * 1018 * cases 1 and 2 can be handled with PGO_LOCKED, case 3 cannot. 1019 * so, if the "center" page hits case 3 (or any page, with PGO_ALLPAGES), 1020 * then we will need to return VM_PAGER_UNLOCK. 1021 * 1022 * => flags: PGO_ALLPAGES: get all of the pages 1023 * PGO_LOCKED: fault data structures are locked 1024 * => NOTE: offset is the offset of pps[0], _NOT_ pps[centeridx] 1025 * => NOTE: caller must check for released pages!! 1026 */ 1027 static int 1028 uao_get(struct uvm_object *uobj, voff_t offset, struct vm_page **pps, 1029 int *npagesp, int centeridx, vm_prot_t access_type, int advice, int flags) 1030 { 1031 struct uvm_aobj *aobj = (struct uvm_aobj *)uobj; 1032 voff_t current_offset; 1033 vm_page_t ptmp; 1034 int lcv, gotpages, maxpages, swslot, rv, pageidx; 1035 boolean_t done; 1036 1037 /* get number of pages */ 1038 maxpages = *npagesp; 1039 1040 /* step 1: handled the case where fault data structures are locked. */ 1041 if (flags & PGO_LOCKED) { 1042 /* step 1a: get pages that are already resident. */ 1043 1044 done = TRUE; /* be optimistic */ 1045 gotpages = 0; /* # of pages we got so far */ 1046 1047 for (lcv = 0, current_offset = offset ; lcv < maxpages ; 1048 lcv++, current_offset += PAGE_SIZE) { 1049 /* do we care about this page? if not, skip it */ 1050 if (pps[lcv] == PGO_DONTCARE) 1051 continue; 1052 1053 ptmp = uvm_pagelookup(uobj, current_offset); 1054 1055 /* 1056 * if page is new, attempt to allocate the page, 1057 * zero-fill'd. 1058 */ 1059 if (ptmp == NULL && uao_find_swslot(aobj, 1060 current_offset >> PAGE_SHIFT) == 0) { 1061 ptmp = uvm_pagealloc(uobj, current_offset, 1062 NULL, UVM_PGA_ZERO); 1063 if (ptmp) { 1064 /* new page */ 1065 atomic_clearbits_int(&ptmp->pg_flags, 1066 PG_BUSY|PG_FAKE); 1067 atomic_setbits_int(&ptmp->pg_flags, 1068 PQ_AOBJ); 1069 UVM_PAGE_OWN(ptmp, NULL); 1070 } 1071 } 1072 1073 /* to be useful must get a non-busy page */ 1074 if (ptmp == NULL || 1075 (ptmp->pg_flags & PG_BUSY) != 0) { 1076 if (lcv == centeridx || 1077 (flags & PGO_ALLPAGES) != 0) 1078 /* need to do a wait or I/O! */ 1079 done = FALSE; 1080 continue; 1081 } 1082 1083 /* 1084 * useful page: busy it and plug it in our 1085 * result array 1086 */ 1087 /* caller must un-busy this page */ 1088 atomic_setbits_int(&ptmp->pg_flags, PG_BUSY); 1089 UVM_PAGE_OWN(ptmp, "uao_get1"); 1090 pps[lcv] = ptmp; 1091 gotpages++; 1092 1093 } 1094 1095 /* 1096 * step 1b: now we've either done everything needed or we 1097 * to unlock and do some waiting or I/O. 1098 */ 1099 *npagesp = gotpages; 1100 if (done) 1101 /* bingo! */ 1102 return(VM_PAGER_OK); 1103 else 1104 /* EEK! Need to unlock and I/O */ 1105 return(VM_PAGER_UNLOCK); 1106 } 1107 1108 /* 1109 * step 2: get non-resident or busy pages. 1110 * data structures are unlocked. 1111 */ 1112 for (lcv = 0, current_offset = offset ; lcv < maxpages ; 1113 lcv++, current_offset += PAGE_SIZE) { 1114 /* 1115 * - skip over pages we've already gotten or don't want 1116 * - skip over pages we don't _have_ to get 1117 */ 1118 if (pps[lcv] != NULL || 1119 (lcv != centeridx && (flags & PGO_ALLPAGES) == 0)) 1120 continue; 1121 1122 pageidx = current_offset >> PAGE_SHIFT; 1123 1124 /* 1125 * we have yet to locate the current page (pps[lcv]). we 1126 * first look for a page that is already at the current offset. 1127 * if we find a page, we check to see if it is busy or 1128 * released. if that is the case, then we sleep on the page 1129 * until it is no longer busy or released and repeat the lookup. 1130 * if the page we found is neither busy nor released, then we 1131 * busy it (so we own it) and plug it into pps[lcv]. this 1132 * 'break's the following while loop and indicates we are 1133 * ready to move on to the next page in the "lcv" loop above. 1134 * 1135 * if we exit the while loop with pps[lcv] still set to NULL, 1136 * then it means that we allocated a new busy/fake/clean page 1137 * ptmp in the object and we need to do I/O to fill in the data. 1138 */ 1139 1140 /* top of "pps" while loop */ 1141 while (pps[lcv] == NULL) { 1142 /* look for a resident page */ 1143 ptmp = uvm_pagelookup(uobj, current_offset); 1144 1145 /* not resident? allocate one now (if we can) */ 1146 if (ptmp == NULL) { 1147 1148 ptmp = uvm_pagealloc(uobj, current_offset, 1149 NULL, 0); 1150 1151 /* out of RAM? */ 1152 if (ptmp == NULL) { 1153 uvm_wait("uao_getpage"); 1154 /* goto top of pps while loop */ 1155 continue; 1156 } 1157 1158 /* 1159 * safe with PQ's unlocked: because we just 1160 * alloc'd the page 1161 */ 1162 atomic_setbits_int(&ptmp->pg_flags, PQ_AOBJ); 1163 1164 /* 1165 * got new page ready for I/O. break pps while 1166 * loop. pps[lcv] is still NULL. 1167 */ 1168 break; 1169 } 1170 1171 /* page is there, see if we need to wait on it */ 1172 if ((ptmp->pg_flags & PG_BUSY) != 0) { 1173 atomic_setbits_int(&ptmp->pg_flags, PG_WANTED); 1174 UVM_WAIT(ptmp, FALSE, "uao_get", 0); 1175 continue; /* goto top of pps while loop */ 1176 } 1177 1178 /* 1179 * if we get here then the page has become resident and 1180 * unbusy between steps 1 and 2. we busy it now (so we 1181 * own it) and set pps[lcv] (so that we exit the while 1182 * loop). 1183 */ 1184 /* we own it, caller must un-busy */ 1185 atomic_setbits_int(&ptmp->pg_flags, PG_BUSY); 1186 UVM_PAGE_OWN(ptmp, "uao_get2"); 1187 pps[lcv] = ptmp; 1188 } 1189 1190 /* 1191 * if we own the valid page at the correct offset, pps[lcv] will 1192 * point to it. nothing more to do except go to the next page. 1193 */ 1194 if (pps[lcv]) 1195 continue; /* next lcv */ 1196 1197 /* 1198 * we have a "fake/busy/clean" page that we just allocated. 1199 * do the needed "i/o", either reading from swap or zeroing. 1200 */ 1201 swslot = uao_find_swslot(aobj, pageidx); 1202 1203 /* just zero the page if there's nothing in swap. */ 1204 if (swslot == 0) { 1205 /* page hasn't existed before, just zero it. */ 1206 uvm_pagezero(ptmp); 1207 } else { 1208 /* page in the swapped-out page. */ 1209 rv = uvm_swap_get(ptmp, swslot, PGO_SYNCIO); 1210 1211 /* I/O done. check for errors. */ 1212 if (rv != VM_PAGER_OK) { 1213 /* 1214 * remove the swap slot from the aobj 1215 * and mark the aobj as having no real slot. 1216 * don't free the swap slot, thus preventing 1217 * it from being used again. 1218 */ 1219 swslot = uao_set_swslot(&aobj->u_obj, pageidx, 1220 SWSLOT_BAD); 1221 uvm_swap_markbad(swslot, 1); 1222 1223 if (ptmp->pg_flags & PG_WANTED) 1224 wakeup(ptmp); 1225 atomic_clearbits_int(&ptmp->pg_flags, 1226 PG_WANTED|PG_BUSY); 1227 UVM_PAGE_OWN(ptmp, NULL); 1228 uvm_lock_pageq(); 1229 uvm_pagefree(ptmp); 1230 uvm_unlock_pageq(); 1231 1232 return (rv); 1233 } 1234 } 1235 1236 /* 1237 * we got the page! clear the fake flag (indicates valid 1238 * data now in page) and plug into our result array. note 1239 * that page is still busy. 1240 * 1241 * it is the callers job to: 1242 * => check if the page is released 1243 * => unbusy the page 1244 * => activate the page 1245 */ 1246 1247 /* data is valid ... */ 1248 atomic_clearbits_int(&ptmp->pg_flags, PG_FAKE); 1249 pmap_clear_modify(ptmp); /* ... and clean */ 1250 pps[lcv] = ptmp; 1251 1252 } /* lcv loop */ 1253 1254 return(VM_PAGER_OK); 1255 } 1256 1257 /* 1258 * uao_dropswap: release any swap resources from this aobj page. 1259 */ 1260 int 1261 uao_dropswap(struct uvm_object *uobj, int pageidx) 1262 { 1263 int slot; 1264 1265 slot = uao_set_swslot(uobj, pageidx, 0); 1266 if (slot) { 1267 uvm_swap_free(slot, 1); 1268 } 1269 return (slot); 1270 } 1271 1272 /* 1273 * page in every page in every aobj that is paged-out to a range of swslots. 1274 * 1275 * => returns TRUE if pagein was aborted due to lack of memory. 1276 */ 1277 boolean_t 1278 uao_swap_off(int startslot, int endslot) 1279 { 1280 struct uvm_aobj *aobj, *nextaobj, *prevaobj = NULL; 1281 1282 /* walk the list of all aobjs. */ 1283 mtx_enter(&uao_list_lock); 1284 1285 for (aobj = LIST_FIRST(&uao_list); 1286 aobj != NULL; 1287 aobj = nextaobj) { 1288 boolean_t rv; 1289 1290 /* 1291 * add a ref to the aobj so it doesn't disappear 1292 * while we're working. 1293 */ 1294 uao_reference_locked(&aobj->u_obj); 1295 1296 /* 1297 * now it's safe to unlock the uao list. 1298 * note that lock interleaving is alright with IPL_NONE mutexes. 1299 */ 1300 mtx_leave(&uao_list_lock); 1301 1302 if (prevaobj) { 1303 uao_detach_locked(&prevaobj->u_obj); 1304 prevaobj = NULL; 1305 } 1306 1307 /* 1308 * page in any pages in the swslot range. 1309 * if there's an error, abort and return the error. 1310 */ 1311 rv = uao_pagein(aobj, startslot, endslot); 1312 if (rv) { 1313 uao_detach_locked(&aobj->u_obj); 1314 return rv; 1315 } 1316 1317 /* 1318 * we're done with this aobj. 1319 * relock the list and drop our ref on the aobj. 1320 */ 1321 mtx_enter(&uao_list_lock); 1322 nextaobj = LIST_NEXT(aobj, u_list); 1323 /* 1324 * prevaobj means that we have an object that we need 1325 * to drop a reference for. We can't just drop it now with 1326 * the list locked since that could cause lock recursion in 1327 * the case where we reduce the refcount to 0. It will be 1328 * released the next time we drop the list lock. 1329 */ 1330 prevaobj = aobj; 1331 } 1332 1333 /* done with traversal, unlock the list */ 1334 mtx_leave(&uao_list_lock); 1335 if (prevaobj) { 1336 uao_detach_locked(&prevaobj->u_obj); 1337 } 1338 return FALSE; 1339 } 1340 1341 /* 1342 * page in any pages from aobj in the given range. 1343 * 1344 * => returns TRUE if pagein was aborted due to lack of memory. 1345 */ 1346 static boolean_t 1347 uao_pagein(struct uvm_aobj *aobj, int startslot, int endslot) 1348 { 1349 boolean_t rv; 1350 1351 if (aobj->u_pages > UAO_SWHASH_THRESHOLD) { 1352 struct uao_swhash_elt *elt; 1353 int bucket; 1354 1355 restart: 1356 for (bucket = aobj->u_swhashmask; bucket >= 0; bucket--) { 1357 for (elt = LIST_FIRST(&aobj->u_swhash[bucket]); 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 /* if slot isn't in range, skip it. */ 1366 if (slot < startslot || 1367 slot >= endslot) { 1368 continue; 1369 } 1370 1371 /* 1372 * process the page, 1373 * the start over on this object 1374 * since the swhash elt 1375 * may have been freed. 1376 */ 1377 rv = uao_pagein_page(aobj, 1378 UAO_SWHASH_ELT_PAGEIDX_BASE(elt) + i); 1379 if (rv) { 1380 return rv; 1381 } 1382 goto restart; 1383 } 1384 } 1385 } 1386 } else { 1387 int i; 1388 1389 for (i = 0; i < aobj->u_pages; i++) { 1390 int slot = aobj->u_swslots[i]; 1391 1392 /* if the slot isn't in range, skip it */ 1393 if (slot < startslot || slot >= endslot) { 1394 continue; 1395 } 1396 1397 /* process the page. */ 1398 rv = uao_pagein_page(aobj, i); 1399 if (rv) { 1400 return rv; 1401 } 1402 } 1403 } 1404 1405 return FALSE; 1406 } 1407 1408 /* 1409 * page in a page from an aobj. used for swap_off. 1410 * returns TRUE if pagein was aborted due to lack of memory. 1411 */ 1412 static boolean_t 1413 uao_pagein_page(struct uvm_aobj *aobj, int pageidx) 1414 { 1415 struct vm_page *pg; 1416 int rv, slot, npages; 1417 1418 pg = NULL; 1419 npages = 1; 1420 rv = uao_get(&aobj->u_obj, (voff_t)pageidx << PAGE_SHIFT, 1421 &pg, &npages, 0, PROT_READ | PROT_WRITE, 0, 0); 1422 1423 switch (rv) { 1424 case VM_PAGER_OK: 1425 break; 1426 1427 case VM_PAGER_ERROR: 1428 case VM_PAGER_REFAULT: 1429 /* 1430 * nothing more to do on errors. 1431 * VM_PAGER_REFAULT can only mean that the anon was freed, 1432 * so again there's nothing to do. 1433 */ 1434 return FALSE; 1435 } 1436 1437 /* 1438 * ok, we've got the page now. 1439 * mark it as dirty, clear its swslot and un-busy it. 1440 */ 1441 slot = uao_set_swslot(&aobj->u_obj, pageidx, 0); 1442 uvm_swap_free(slot, 1); 1443 atomic_clearbits_int(&pg->pg_flags, PG_BUSY|PG_CLEAN|PG_FAKE); 1444 UVM_PAGE_OWN(pg, NULL); 1445 1446 /* deactivate the page (to put it on a page queue). */ 1447 pmap_clear_reference(pg); 1448 uvm_lock_pageq(); 1449 uvm_pagedeactivate(pg); 1450 uvm_unlock_pageq(); 1451 1452 return FALSE; 1453 } 1454 1455 /* 1456 * XXX pedro: Once we are comfortable enough with this function, we can adapt 1457 * uao_free() to use it. 1458 * 1459 * uao_dropswap_range: drop swapslots in the range. 1460 * 1461 * => aobj must be locked and is returned locked. 1462 * => start is inclusive. end is exclusive. 1463 */ 1464 void 1465 uao_dropswap_range(struct uvm_object *uobj, voff_t start, voff_t end) 1466 { 1467 struct uvm_aobj *aobj = (struct uvm_aobj *)uobj; 1468 int swpgonlydelta = 0; 1469 1470 /* KASSERT(mutex_owned(uobj->vmobjlock)); */ 1471 1472 if (end == 0) { 1473 end = INT64_MAX; 1474 } 1475 1476 if (aobj->u_pages > UAO_SWHASH_THRESHOLD) { 1477 int i, hashbuckets = aobj->u_swhashmask + 1; 1478 voff_t taghi; 1479 voff_t taglo; 1480 1481 taglo = UAO_SWHASH_ELT_TAG(start); 1482 taghi = UAO_SWHASH_ELT_TAG(end); 1483 1484 for (i = 0; i < hashbuckets; i++) { 1485 struct uao_swhash_elt *elt, *next; 1486 1487 for (elt = LIST_FIRST(&aobj->u_swhash[i]); 1488 elt != NULL; 1489 elt = next) { 1490 int startidx, endidx; 1491 int j; 1492 1493 next = LIST_NEXT(elt, list); 1494 1495 if (elt->tag < taglo || taghi < elt->tag) { 1496 continue; 1497 } 1498 1499 if (elt->tag == taglo) { 1500 startidx = 1501 UAO_SWHASH_ELT_PAGESLOT_IDX(start); 1502 } else { 1503 startidx = 0; 1504 } 1505 1506 if (elt->tag == taghi) { 1507 endidx = 1508 UAO_SWHASH_ELT_PAGESLOT_IDX(end); 1509 } else { 1510 endidx = UAO_SWHASH_CLUSTER_SIZE; 1511 } 1512 1513 for (j = startidx; j < endidx; j++) { 1514 int slot = elt->slots[j]; 1515 1516 KASSERT(uvm_pagelookup(&aobj->u_obj, 1517 (voff_t)(UAO_SWHASH_ELT_PAGEIDX_BASE(elt) 1518 + j) << PAGE_SHIFT) == NULL); 1519 1520 if (slot > 0) { 1521 uvm_swap_free(slot, 1); 1522 swpgonlydelta++; 1523 KASSERT(elt->count > 0); 1524 elt->slots[j] = 0; 1525 elt->count--; 1526 } 1527 } 1528 1529 if (elt->count == 0) { 1530 LIST_REMOVE(elt, list); 1531 pool_put(&uao_swhash_elt_pool, elt); 1532 } 1533 } 1534 } 1535 } else { 1536 int i; 1537 1538 if (aobj->u_pages < end) { 1539 end = aobj->u_pages; 1540 } 1541 for (i = start; i < end; i++) { 1542 int slot = aobj->u_swslots[i]; 1543 1544 if (slot > 0) { 1545 uvm_swap_free(slot, 1); 1546 swpgonlydelta++; 1547 } 1548 } 1549 } 1550 1551 /* 1552 * adjust the counter of pages only in swap for all 1553 * the swap slots we've freed. 1554 */ 1555 if (swpgonlydelta > 0) { 1556 simple_lock(&uvm.swap_data_lock); 1557 KASSERT(uvmexp.swpgonly >= swpgonlydelta); 1558 uvmexp.swpgonly -= swpgonlydelta; 1559 simple_unlock(&uvm.swap_data_lock); 1560 } 1561 } 1562