1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 #include <sys/types.h> 29 #include <sys/sysmacros.h> 30 #include <sys/kmem.h> 31 #include <sys/atomic.h> 32 #include <sys/bitmap.h> 33 #include <sys/systm.h> 34 #include <vm/seg_kmem.h> 35 #include <vm/hat.h> 36 #include <vm/vm_dep.h> 37 #include <vm/hat_i86.h> 38 #include <sys/cmn_err.h> 39 40 41 /* 42 * When pages are shared by more than one mapping, a list of these 43 * structs hangs off of the page_t connected by the hm_next and hm_prev 44 * fields. Every hment is also indexed by a system-wide hash table, using 45 * hm_hashnext to connect it to the chain of hments in a single hash 46 * bucket. 47 */ 48 struct hment { 49 struct hment *hm_hashnext; /* next mapping on hash chain */ 50 struct hment *hm_next; /* next mapping of same page */ 51 struct hment *hm_prev; /* previous mapping of same page */ 52 htable_t *hm_htable; /* corresponding htable_t */ 53 pfn_t hm_pfn; /* mapping page frame number */ 54 uint16_t hm_entry; /* index of pte in htable */ 55 uint16_t hm_pad; /* explicitly expose compiler padding */ 56 #ifdef __amd64 57 uint32_t hm_pad2; /* explicitly expose compiler padding */ 58 #endif 59 }; 60 61 /* 62 * Value returned by hment_walk() when dealing with a single mapping 63 * embedded in the page_t. 64 */ 65 #define HMENT_EMBEDDED ((hment_t *)(uintptr_t)1) 66 67 kmem_cache_t *hment_cache; 68 69 /* 70 * The hment reserve is similar to the htable reserve, with the following 71 * exception. Hment's are never needed for HAT kmem allocs. 72 * 73 * The hment_reserve_amount variable is used, so that you can change it's 74 * value to zero via a kernel debugger to force stealing to get tested. 75 */ 76 #define HMENT_RESERVE_AMOUNT (200) /* currently a guess at right value. */ 77 uint_t hment_reserve_amount = HMENT_RESERVE_AMOUNT; 78 kmutex_t hment_reserve_mutex; 79 uint_t hment_reserve_count; 80 hment_t *hment_reserve_pool; 81 extern kthread_t *hat_reserves_thread; 82 83 /* 84 * Possible performance RFE: we might need to make this dynamic, perhaps 85 * based on the number of pages in the system. 86 */ 87 #define HMENT_HASH_SIZE (64 * 1024) 88 static uint_t hment_hash_entries = HMENT_HASH_SIZE; 89 static hment_t **hment_hash; 90 91 /* 92 * Lots of highly shared pages will have the same value for "entry" (consider 93 * the starting address of "xterm" or "sh"). So we'll distinguish them by 94 * adding the pfn of the page table into both the high bits. 95 * The shift by 9 corresponds to the range of values for entry (0..511). 96 */ 97 #define HMENT_HASH(pfn, entry) (uint32_t) \ 98 ((((pfn) << 9) + entry + pfn) & (hment_hash_entries - 1)) 99 100 /* 101 * "mlist_lock" is a hashed mutex lock for protecting per-page mapping 102 * lists and "hash_lock" is a similar lock protecting the hment hash 103 * table. The hashed approach is taken to avoid the spatial overhead of 104 * maintaining a separate lock for each page, while still achieving better 105 * scalability than a single lock would allow. 106 */ 107 #define MLIST_NUM_LOCK 256 /* must be power of two */ 108 static kmutex_t mlist_lock[MLIST_NUM_LOCK]; 109 110 /* 111 * the shift by 9 is so that all large pages don't use the same hash bucket 112 */ 113 #define MLIST_MUTEX(pp) \ 114 &mlist_lock[((pp)->p_pagenum + ((pp)->p_pagenum >> 9)) & \ 115 (MLIST_NUM_LOCK - 1)] 116 117 #define HASH_NUM_LOCK 256 /* must be power of two */ 118 static kmutex_t hash_lock[HASH_NUM_LOCK]; 119 120 #define HASH_MUTEX(idx) &hash_lock[(idx) & (HASH_NUM_LOCK-1)] 121 122 static hment_t *hment_steal(void); 123 124 /* 125 * put one hment onto the reserves list 126 */ 127 static void 128 hment_put_reserve(hment_t *hm) 129 { 130 HATSTAT_INC(hs_hm_put_reserve); 131 mutex_enter(&hment_reserve_mutex); 132 hm->hm_next = hment_reserve_pool; 133 hment_reserve_pool = hm; 134 ++hment_reserve_count; 135 mutex_exit(&hment_reserve_mutex); 136 } 137 138 /* 139 * Take one hment from the reserve. 140 */ 141 static hment_t * 142 hment_get_reserve(void) 143 { 144 hment_t *hm = NULL; 145 146 /* 147 * We rely on a "donation system" to refill the hment reserve 148 * list, which only takes place when we are allocating hments for 149 * user mappings. It is theoretically possible that an incredibly 150 * long string of kernel hment_alloc()s with no intervening user 151 * hment_alloc()s could exhaust that pool. 152 */ 153 HATSTAT_INC(hs_hm_get_reserve); 154 mutex_enter(&hment_reserve_mutex); 155 if (hment_reserve_count != 0) { 156 hm = hment_reserve_pool; 157 hment_reserve_pool = hm->hm_next; 158 --hment_reserve_count; 159 } 160 mutex_exit(&hment_reserve_mutex); 161 return (hm); 162 } 163 164 /* 165 * Allocate an hment 166 */ 167 static hment_t * 168 hment_alloc() 169 { 170 int km_flag = can_steal_post_boot ? KM_NOSLEEP : KM_SLEEP; 171 hment_t *hm = NULL; 172 173 /* 174 * If we aren't using the reserves, try using kmem to get an hment. 175 * Donate any successful allocations to reserves if low. 176 * 177 * If we're in panic, resort to using the reserves. 178 */ 179 HATSTAT_INC(hs_hm_alloc); 180 if (!USE_HAT_RESERVES()) { 181 for (;;) { 182 hm = kmem_cache_alloc(hment_cache, km_flag); 183 if (USE_HAT_RESERVES() || 184 hment_reserve_count >= hment_reserve_amount) 185 break; 186 hment_put_reserve(hm); 187 } 188 } 189 190 /* 191 * If allocation failed, we need to tap the reserves or steal 192 */ 193 if (hm == NULL) { 194 if (USE_HAT_RESERVES()) 195 hm = hment_get_reserve(); 196 197 /* 198 * If we still haven't gotten an hment, attempt to steal one by 199 * victimizing a mapping in a user htable. 200 */ 201 if (hm == NULL && can_steal_post_boot) 202 hm = hment_steal(); 203 204 /* 205 * we're in dire straights, try the reserve 206 */ 207 if (hm == NULL) 208 hm = hment_get_reserve(); 209 210 /* 211 * still no hment is a serious problem. 212 */ 213 if (hm == NULL) 214 panic("hment_alloc(): no reserve, couldn't steal"); 215 } 216 217 218 hm->hm_entry = 0; 219 hm->hm_htable = NULL; 220 hm->hm_hashnext = NULL; 221 hm->hm_next = NULL; 222 hm->hm_prev = NULL; 223 hm->hm_pfn = PFN_INVALID; 224 return (hm); 225 } 226 227 /* 228 * Free an hment, possibly to the reserves list when called from the 229 * thread using the reserves. For example, when freeing an hment during an 230 * htable_steal(), we can't recurse into the kmem allocator, so we just 231 * push the hment onto the reserve list. 232 */ 233 void 234 hment_free(hment_t *hm) 235 { 236 #ifdef DEBUG 237 /* 238 * zero out all fields to try and force any race conditions to segfault 239 */ 240 bzero(hm, sizeof (*hm)); 241 #endif 242 HATSTAT_INC(hs_hm_free); 243 if (USE_HAT_RESERVES() || 244 hment_reserve_count < hment_reserve_amount) 245 hment_put_reserve(hm); 246 else 247 kmem_cache_free(hment_cache, hm); 248 } 249 250 int 251 x86_hm_held(page_t *pp) 252 { 253 ASSERT(pp != NULL); 254 return (MUTEX_HELD(MLIST_MUTEX(pp))); 255 } 256 257 void 258 x86_hm_enter(page_t *pp) 259 { 260 ASSERT(pp != NULL); 261 mutex_enter(MLIST_MUTEX(pp)); 262 } 263 264 void 265 x86_hm_exit(page_t *pp) 266 { 267 ASSERT(pp != NULL); 268 mutex_exit(MLIST_MUTEX(pp)); 269 } 270 271 /* 272 * Internal routine to add a full hment to a page_t mapping list 273 */ 274 static void 275 hment_insert(hment_t *hm, page_t *pp) 276 { 277 uint_t idx; 278 279 ASSERT(x86_hm_held(pp)); 280 ASSERT(!pp->p_embed); 281 282 /* 283 * Add the hment to the page's mapping list. 284 */ 285 ++pp->p_share; 286 hm->hm_next = pp->p_mapping; 287 if (pp->p_mapping != NULL) 288 ((hment_t *)pp->p_mapping)->hm_prev = hm; 289 pp->p_mapping = hm; 290 291 /* 292 * Add the hment to the system-wide hash table. 293 */ 294 idx = HMENT_HASH(hm->hm_htable->ht_pfn, hm->hm_entry); 295 296 mutex_enter(HASH_MUTEX(idx)); 297 hm->hm_hashnext = hment_hash[idx]; 298 hment_hash[idx] = hm; 299 mutex_exit(HASH_MUTEX(idx)); 300 } 301 302 /* 303 * Prepare a mapping list entry to the given page. 304 * 305 * There are 4 different situations to deal with: 306 * 307 * - Adding the first mapping to a page_t as an embedded hment 308 * - Refaulting on an existing embedded mapping 309 * - Upgrading an embedded mapping when adding a 2nd mapping 310 * - Adding another mapping to a page_t that already has multiple mappings 311 * note we don't optimized for the refaulting case here. 312 * 313 * Due to competition with other threads that may be mapping/unmapping the 314 * same page and the need to drop all locks while allocating hments, any or 315 * all of the 3 situations can occur (and in almost any order) in any given 316 * call. Isn't this fun! 317 */ 318 hment_t * 319 hment_prepare(htable_t *htable, uint_t entry, page_t *pp) 320 { 321 hment_t *hm = NULL; 322 323 ASSERT(x86_hm_held(pp)); 324 325 for (;;) { 326 327 /* 328 * The most common case is establishing the first mapping to a 329 * page, so check that first. This doesn't need any allocated 330 * hment. 331 */ 332 if (pp->p_mapping == NULL) { 333 ASSERT(!pp->p_embed); 334 ASSERT(pp->p_share == 0); 335 if (hm == NULL) 336 break; 337 338 /* 339 * we had an hment already, so free it and retry 340 */ 341 goto free_and_continue; 342 } 343 344 /* 345 * If there is an embedded mapping, we may need to 346 * convert it to an hment. 347 */ 348 if (pp->p_embed) { 349 350 /* should point to htable */ 351 ASSERT(pp->p_mapping != NULL); 352 353 /* 354 * If we are faulting on a pre-existing mapping 355 * there is no need to promote/allocate a new hment. 356 * This happens a lot due to segmap. 357 */ 358 if (pp->p_mapping == htable && pp->p_mlentry == entry) { 359 if (hm == NULL) 360 break; 361 goto free_and_continue; 362 } 363 364 /* 365 * If we have an hment allocated, use it to promote the 366 * existing embedded mapping. 367 */ 368 if (hm != NULL) { 369 hm->hm_htable = pp->p_mapping; 370 hm->hm_entry = pp->p_mlentry; 371 hm->hm_pfn = pp->p_pagenum; 372 pp->p_mapping = NULL; 373 pp->p_share = 0; 374 pp->p_embed = 0; 375 hment_insert(hm, pp); 376 } 377 378 /* 379 * We either didn't have an hment allocated or we just 380 * used it for the embedded mapping. In either case, 381 * allocate another hment and restart. 382 */ 383 goto allocate_and_continue; 384 } 385 386 /* 387 * Last possibility is that we're adding an hment to a list 388 * of hments. 389 */ 390 if (hm != NULL) 391 break; 392 allocate_and_continue: 393 x86_hm_exit(pp); 394 hm = hment_alloc(); 395 x86_hm_enter(pp); 396 continue; 397 398 free_and_continue: 399 /* 400 * we allocated an hment already, free it and retry 401 */ 402 x86_hm_exit(pp); 403 hment_free(hm); 404 hm = NULL; 405 x86_hm_enter(pp); 406 } 407 ASSERT(x86_hm_held(pp)); 408 return (hm); 409 } 410 411 /* 412 * Record a mapping list entry for the htable/entry to the given page. 413 * 414 * hment_prepare() should have properly set up the situation. 415 */ 416 void 417 hment_assign(htable_t *htable, uint_t entry, page_t *pp, hment_t *hm) 418 { 419 ASSERT(x86_hm_held(pp)); 420 421 /* 422 * The most common case is establishing the first mapping to a 423 * page, so check that first. This doesn't need any allocated 424 * hment. 425 */ 426 if (pp->p_mapping == NULL) { 427 ASSERT(hm == NULL); 428 ASSERT(!pp->p_embed); 429 ASSERT(pp->p_share == 0); 430 pp->p_embed = 1; 431 pp->p_mapping = htable; 432 pp->p_mlentry = entry; 433 return; 434 } 435 436 /* 437 * We should never get here with a pre-existing embedded maping 438 */ 439 ASSERT(!pp->p_embed); 440 441 /* 442 * add the new hment to the mapping list 443 */ 444 ASSERT(hm != NULL); 445 hm->hm_htable = htable; 446 hm->hm_entry = entry; 447 hm->hm_pfn = pp->p_pagenum; 448 hment_insert(hm, pp); 449 } 450 451 /* 452 * Walk through the mappings for a page. 453 * 454 * must already have done an x86_hm_enter() 455 */ 456 hment_t * 457 hment_walk(page_t *pp, htable_t **ht, uint_t *entry, hment_t *prev) 458 { 459 hment_t *hm; 460 461 ASSERT(x86_hm_held(pp)); 462 463 if (pp->p_embed) { 464 if (prev == NULL) { 465 *ht = (htable_t *)pp->p_mapping; 466 *entry = pp->p_mlentry; 467 hm = HMENT_EMBEDDED; 468 } else { 469 ASSERT(prev == HMENT_EMBEDDED); 470 hm = NULL; 471 } 472 } else { 473 if (prev == NULL) { 474 ASSERT(prev != HMENT_EMBEDDED); 475 hm = (hment_t *)pp->p_mapping; 476 } else { 477 hm = prev->hm_next; 478 } 479 480 if (hm != NULL) { 481 *ht = hm->hm_htable; 482 *entry = hm->hm_entry; 483 } 484 } 485 return (hm); 486 } 487 488 /* 489 * Remove a mapping to a page from its mapping list. Must have 490 * the corresponding mapping list locked. 491 * Finds the mapping list entry with the given pte_t and 492 * unlinks it from the mapping list. 493 */ 494 hment_t * 495 hment_remove(page_t *pp, htable_t *ht, uint_t entry) 496 { 497 hment_t *prev = NULL; 498 hment_t *hm; 499 uint_t idx; 500 pfn_t pfn; 501 502 ASSERT(x86_hm_held(pp)); 503 504 /* 505 * Check if we have only one mapping embedded in the page_t. 506 */ 507 if (pp->p_embed) { 508 ASSERT(ht == (htable_t *)pp->p_mapping); 509 ASSERT(entry == pp->p_mlentry); 510 ASSERT(pp->p_share == 0); 511 pp->p_mapping = NULL; 512 pp->p_mlentry = 0; 513 pp->p_embed = 0; 514 return (NULL); 515 } 516 517 /* 518 * Otherwise it must be in the list of hments. 519 * Find the hment in the system-wide hash table and remove it. 520 */ 521 ASSERT(pp->p_share != 0); 522 pfn = pp->p_pagenum; 523 idx = HMENT_HASH(ht->ht_pfn, entry); 524 mutex_enter(HASH_MUTEX(idx)); 525 hm = hment_hash[idx]; 526 while (hm && (hm->hm_htable != ht || hm->hm_entry != entry || 527 hm->hm_pfn != pfn)) { 528 prev = hm; 529 hm = hm->hm_hashnext; 530 } 531 if (hm == NULL) { 532 panic("hment_remove() missing in hash table pp=%lx, ht=%lx," 533 "entry=0x%x hash index=0x%x", (uintptr_t)pp, (uintptr_t)ht, 534 entry, idx); 535 } 536 537 if (prev) 538 prev->hm_hashnext = hm->hm_hashnext; 539 else 540 hment_hash[idx] = hm->hm_hashnext; 541 mutex_exit(HASH_MUTEX(idx)); 542 543 /* 544 * Remove the hment from the page's mapping list 545 */ 546 if (hm->hm_next) 547 hm->hm_next->hm_prev = hm->hm_prev; 548 if (hm->hm_prev) 549 hm->hm_prev->hm_next = hm->hm_next; 550 else 551 pp->p_mapping = hm->hm_next; 552 553 --pp->p_share; 554 hm->hm_hashnext = NULL; 555 hm->hm_next = NULL; 556 hm->hm_prev = NULL; 557 558 return (hm); 559 } 560 561 /* 562 * Put initial hment's in the reserve pool. 563 */ 564 void 565 hment_reserve(uint_t count) 566 { 567 hment_t *hm; 568 569 count += hment_reserve_amount; 570 571 while (hment_reserve_count < count) { 572 hm = kmem_cache_alloc(hment_cache, KM_NOSLEEP); 573 if (hm == NULL) 574 return; 575 hment_put_reserve(hm); 576 } 577 } 578 579 /* 580 * Readjust the hment reserves after they may have been used. 581 */ 582 void 583 hment_adjust_reserve() 584 { 585 hment_t *hm; 586 587 /* 588 * Free up any excess reserves 589 */ 590 while (hment_reserve_count > hment_reserve_amount) { 591 ASSERT(curthread != hat_reserves_thread); 592 hm = hment_get_reserve(); 593 if (hm == NULL) 594 return; 595 hment_free(hm); 596 } 597 } 598 599 /* 600 * initialize hment data structures 601 */ 602 void 603 hment_init(void) 604 { 605 int i; 606 int flags = KMC_NOHASH | KMC_NODEBUG; 607 608 /* 609 * Initialize kmem caches. On 32 bit kernel's we shut off 610 * debug information to save on precious kernel VA usage. 611 */ 612 hment_cache = kmem_cache_create("hment_t", 613 sizeof (hment_t), 0, NULL, NULL, NULL, 614 NULL, hat_memload_arena, flags); 615 616 hment_hash = kmem_zalloc(hment_hash_entries * sizeof (hment_t *), 617 KM_SLEEP); 618 619 for (i = 0; i < MLIST_NUM_LOCK; i++) 620 mutex_init(&mlist_lock[i], NULL, MUTEX_DEFAULT, NULL); 621 622 for (i = 0; i < HASH_NUM_LOCK; i++) 623 mutex_init(&hash_lock[i], NULL, MUTEX_DEFAULT, NULL); 624 625 626 } 627 628 /* 629 * return the number of mappings to a page 630 * 631 * Note there is no ASSERT() that the MUTEX is held for this. 632 * Hence the return value might be inaccurate if this is called without 633 * doing an x86_hm_enter(). 634 */ 635 uint_t 636 hment_mapcnt(page_t *pp) 637 { 638 uint_t cnt; 639 uint_t szc; 640 page_t *larger; 641 hment_t *hm; 642 643 x86_hm_enter(pp); 644 if (pp->p_mapping == NULL) 645 cnt = 0; 646 else if (pp->p_embed) 647 cnt = 1; 648 else 649 cnt = pp->p_share; 650 x86_hm_exit(pp); 651 652 /* 653 * walk through all larger mapping sizes counting mappings 654 */ 655 for (szc = 1; szc <= pp->p_szc; ++szc) { 656 larger = PP_GROUPLEADER(pp, szc); 657 if (larger == pp) /* don't double count large mappings */ 658 continue; 659 660 x86_hm_enter(larger); 661 if (larger->p_mapping != NULL) { 662 if (larger->p_embed && 663 ((htable_t *)larger->p_mapping)->ht_level == szc) { 664 ++cnt; 665 } else if (!larger->p_embed) { 666 for (hm = larger->p_mapping; hm; 667 hm = hm->hm_next) { 668 if (hm->hm_htable->ht_level == szc) 669 ++cnt; 670 } 671 } 672 } 673 x86_hm_exit(larger); 674 } 675 return (cnt); 676 } 677 678 /* 679 * We need to steal an hment. Walk through all the page_t's until we 680 * find one that has multiple mappings. Unload one of the mappings 681 * and reclaim that hment. Note that we'll save/restart the starting 682 * page to try and spread the pain. 683 */ 684 static page_t *last_page = NULL; 685 686 static hment_t * 687 hment_steal(void) 688 { 689 page_t *last = last_page; 690 page_t *pp = last; 691 hment_t *hm = NULL; 692 hment_t *hm2; 693 htable_t *ht; 694 uint_t found_one = 0; 695 696 HATSTAT_INC(hs_hm_steals); 697 if (pp == NULL) 698 last = pp = page_first(); 699 700 while (!found_one) { 701 HATSTAT_INC(hs_hm_steal_exam); 702 pp = page_next(pp); 703 if (pp == NULL) 704 pp = page_first(); 705 706 /* 707 * The loop and function exit here if nothing found to steal. 708 */ 709 if (pp == last) 710 return (NULL); 711 712 /* 713 * Only lock the page_t if it has hments. 714 */ 715 if (pp->p_mapping == NULL || pp->p_embed) 716 continue; 717 718 /* 719 * Search the mapping list for a usable mapping. 720 */ 721 x86_hm_enter(pp); 722 if (!pp->p_embed) { 723 for (hm = pp->p_mapping; hm; hm = hm->hm_next) { 724 ht = hm->hm_htable; 725 if (ht->ht_hat != kas.a_hat && 726 ht->ht_busy == 0 && 727 ht->ht_lock_cnt == 0) { 728 found_one = 1; 729 break; 730 } 731 } 732 } 733 if (!found_one) 734 x86_hm_exit(pp); 735 } 736 737 /* 738 * Steal the mapping we found. Note that hati_page_unmap() will 739 * do the x86_hm_exit(). 740 */ 741 hm2 = hati_page_unmap(pp, ht, hm->hm_entry); 742 ASSERT(hm2 == hm); 743 last_page = pp; 744 return (hm); 745 } 746