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