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