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