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 82 /* 83 * Possible performance RFE: we might need to make this dynamic, perhaps 84 * based on the number of pages in the system. 85 */ 86 #define HMENT_HASH_SIZE (64 * 1024) 87 static uint_t hment_hash_entries = HMENT_HASH_SIZE; 88 static hment_t **hment_hash; 89 90 /* 91 * Lots of highly shared pages will have the same value for "entry" (consider 92 * the starting address of "xterm" or "sh"). So we'll distinguish them by 93 * adding the pfn of the page table into both the high bits. 94 * The shift by 9 corresponds to the range of values for entry (0..511). 95 */ 96 #define HMENT_HASH(pfn, entry) (uint32_t) \ 97 ((((pfn) << 9) + entry + pfn) & (hment_hash_entries - 1)) 98 99 /* 100 * "mlist_lock" is a hashed mutex lock for protecting per-page mapping 101 * lists and "hash_lock" is a similar lock protecting the hment hash 102 * table. The hashed approach is taken to avoid the spatial overhead of 103 * maintaining a separate lock for each page, while still achieving better 104 * scalability than a single lock would allow. 105 */ 106 #define MLIST_NUM_LOCK 256 /* must be power of two */ 107 static kmutex_t mlist_lock[MLIST_NUM_LOCK]; 108 109 /* 110 * the shift by 9 is so that all large pages don't use the same hash bucket 111 */ 112 #define MLIST_MUTEX(pp) \ 113 &mlist_lock[((pp)->p_pagenum + ((pp)->p_pagenum >> 9)) & \ 114 (MLIST_NUM_LOCK - 1)] 115 116 #define HASH_NUM_LOCK 256 /* must be power of two */ 117 static kmutex_t hash_lock[HASH_NUM_LOCK]; 118 119 #define HASH_MUTEX(idx) &hash_lock[(idx) & (HASH_NUM_LOCK-1)] 120 121 static hment_t *hment_steal(void); 122 123 /* 124 * put one hment onto the reserves list 125 */ 126 static void 127 hment_put_reserve(hment_t *hm) 128 { 129 HATSTAT_INC(hs_hm_put_reserve); 130 mutex_enter(&hment_reserve_mutex); 131 hm->hm_next = hment_reserve_pool; 132 hment_reserve_pool = hm; 133 ++hment_reserve_count; 134 mutex_exit(&hment_reserve_mutex); 135 } 136 137 /* 138 * Take one hment from the reserve. 139 */ 140 static hment_t * 141 hment_get_reserve(void) 142 { 143 hment_t *hm = NULL; 144 145 /* 146 * We rely on a "donation system" to refill the hment reserve 147 * list, which only takes place when we are allocating hments for 148 * user mappings. It is theoretically possible that an incredibly 149 * long string of kernel hment_alloc()s with no intervening user 150 * hment_alloc()s could exhaust that pool. 151 */ 152 HATSTAT_INC(hs_hm_get_reserve); 153 mutex_enter(&hment_reserve_mutex); 154 if (hment_reserve_count != 0) { 155 hm = hment_reserve_pool; 156 hment_reserve_pool = hm->hm_next; 157 --hment_reserve_count; 158 } 159 mutex_exit(&hment_reserve_mutex); 160 return (hm); 161 } 162 163 /* 164 * Allocate an hment 165 */ 166 static hment_t * 167 hment_alloc() 168 { 169 int km_flag = can_steal_post_boot ? KM_NOSLEEP : KM_SLEEP; 170 hment_t *hm = NULL; 171 172 /* 173 * If we aren't using the reserves, try using kmem to get an hment. 174 * Donate any successful allocations to reserves if low. 175 * 176 * If we're in panic, resort to using the reserves. 177 */ 178 HATSTAT_INC(hs_hm_alloc); 179 if (!USE_HAT_RESERVES()) { 180 for (;;) { 181 hm = kmem_cache_alloc(hment_cache, km_flag); 182 if (hm == NULL || 183 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 hment_adjust_reserve(); 249 } 250 } 251 252 int 253 x86_hm_held(page_t *pp) 254 { 255 ASSERT(pp != NULL); 256 return (MUTEX_HELD(MLIST_MUTEX(pp))); 257 } 258 259 void 260 x86_hm_enter(page_t *pp) 261 { 262 ASSERT(pp != NULL); 263 mutex_enter(MLIST_MUTEX(pp)); 264 } 265 266 void 267 x86_hm_exit(page_t *pp) 268 { 269 ASSERT(pp != NULL); 270 mutex_exit(MLIST_MUTEX(pp)); 271 } 272 273 /* 274 * Internal routine to add a full hment to a page_t mapping list 275 */ 276 static void 277 hment_insert(hment_t *hm, page_t *pp) 278 { 279 uint_t idx; 280 281 ASSERT(x86_hm_held(pp)); 282 ASSERT(!pp->p_embed); 283 284 /* 285 * Add the hment to the page's mapping list. 286 */ 287 ++pp->p_share; 288 hm->hm_next = pp->p_mapping; 289 if (pp->p_mapping != NULL) 290 ((hment_t *)pp->p_mapping)->hm_prev = hm; 291 pp->p_mapping = hm; 292 293 /* 294 * Add the hment to the system-wide hash table. 295 */ 296 idx = HMENT_HASH(hm->hm_htable->ht_pfn, hm->hm_entry); 297 298 mutex_enter(HASH_MUTEX(idx)); 299 hm->hm_hashnext = hment_hash[idx]; 300 hment_hash[idx] = hm; 301 mutex_exit(HASH_MUTEX(idx)); 302 } 303 304 /* 305 * Prepare a mapping list entry to the given page. 306 * 307 * There are 4 different situations to deal with: 308 * 309 * - Adding the first mapping to a page_t as an embedded hment 310 * - Refaulting on an existing embedded mapping 311 * - Upgrading an embedded mapping when adding a 2nd mapping 312 * - Adding another mapping to a page_t that already has multiple mappings 313 * note we don't optimized for the refaulting case here. 314 * 315 * Due to competition with other threads that may be mapping/unmapping the 316 * same page and the need to drop all locks while allocating hments, any or 317 * all of the 3 situations can occur (and in almost any order) in any given 318 * call. Isn't this fun! 319 */ 320 hment_t * 321 hment_prepare(htable_t *htable, uint_t entry, page_t *pp) 322 { 323 hment_t *hm = NULL; 324 325 ASSERT(x86_hm_held(pp)); 326 327 for (;;) { 328 329 /* 330 * The most common case is establishing the first mapping to a 331 * page, so check that first. This doesn't need any allocated 332 * hment. 333 */ 334 if (pp->p_mapping == NULL) { 335 ASSERT(!pp->p_embed); 336 ASSERT(pp->p_share == 0); 337 if (hm == NULL) 338 break; 339 340 /* 341 * we had an hment already, so free it and retry 342 */ 343 goto free_and_continue; 344 } 345 346 /* 347 * If there is an embedded mapping, we may need to 348 * convert it to an hment. 349 */ 350 if (pp->p_embed) { 351 352 /* should point to htable */ 353 ASSERT(pp->p_mapping != NULL); 354 355 /* 356 * If we are faulting on a pre-existing mapping 357 * there is no need to promote/allocate a new hment. 358 * This happens a lot due to segmap. 359 */ 360 if (pp->p_mapping == htable && pp->p_mlentry == entry) { 361 if (hm == NULL) 362 break; 363 goto free_and_continue; 364 } 365 366 /* 367 * If we have an hment allocated, use it to promote the 368 * existing embedded mapping. 369 */ 370 if (hm != NULL) { 371 hm->hm_htable = pp->p_mapping; 372 hm->hm_entry = pp->p_mlentry; 373 hm->hm_pfn = pp->p_pagenum; 374 pp->p_mapping = NULL; 375 pp->p_share = 0; 376 pp->p_embed = 0; 377 hment_insert(hm, pp); 378 } 379 380 /* 381 * We either didn't have an hment allocated or we just 382 * used it for the embedded mapping. In either case, 383 * allocate another hment and restart. 384 */ 385 goto allocate_and_continue; 386 } 387 388 /* 389 * Last possibility is that we're adding an hment to a list 390 * of hments. 391 */ 392 if (hm != NULL) 393 break; 394 allocate_and_continue: 395 x86_hm_exit(pp); 396 hm = hment_alloc(); 397 x86_hm_enter(pp); 398 continue; 399 400 free_and_continue: 401 /* 402 * we allocated an hment already, free it and retry 403 */ 404 x86_hm_exit(pp); 405 hment_free(hm); 406 hm = NULL; 407 x86_hm_enter(pp); 408 } 409 ASSERT(x86_hm_held(pp)); 410 return (hm); 411 } 412 413 /* 414 * Record a mapping list entry for the htable/entry to the given page. 415 * 416 * hment_prepare() should have properly set up the situation. 417 */ 418 void 419 hment_assign(htable_t *htable, uint_t entry, page_t *pp, hment_t *hm) 420 { 421 ASSERT(x86_hm_held(pp)); 422 423 /* 424 * The most common case is establishing the first mapping to a 425 * page, so check that first. This doesn't need any allocated 426 * hment. 427 */ 428 if (pp->p_mapping == NULL) { 429 ASSERT(hm == NULL); 430 ASSERT(!pp->p_embed); 431 ASSERT(pp->p_share == 0); 432 pp->p_embed = 1; 433 pp->p_mapping = htable; 434 pp->p_mlentry = entry; 435 return; 436 } 437 438 /* 439 * We should never get here with a pre-existing embedded maping 440 */ 441 ASSERT(!pp->p_embed); 442 443 /* 444 * add the new hment to the mapping list 445 */ 446 ASSERT(hm != NULL); 447 hm->hm_htable = htable; 448 hm->hm_entry = entry; 449 hm->hm_pfn = pp->p_pagenum; 450 hment_insert(hm, pp); 451 } 452 453 /* 454 * Walk through the mappings for a page. 455 * 456 * must already have done an x86_hm_enter() 457 */ 458 hment_t * 459 hment_walk(page_t *pp, htable_t **ht, uint_t *entry, hment_t *prev) 460 { 461 hment_t *hm; 462 463 ASSERT(x86_hm_held(pp)); 464 465 if (pp->p_embed) { 466 if (prev == NULL) { 467 *ht = (htable_t *)pp->p_mapping; 468 *entry = pp->p_mlentry; 469 hm = HMENT_EMBEDDED; 470 } else { 471 ASSERT(prev == HMENT_EMBEDDED); 472 hm = NULL; 473 } 474 } else { 475 if (prev == NULL) { 476 ASSERT(prev != HMENT_EMBEDDED); 477 hm = (hment_t *)pp->p_mapping; 478 } else { 479 hm = prev->hm_next; 480 } 481 482 if (hm != NULL) { 483 *ht = hm->hm_htable; 484 *entry = hm->hm_entry; 485 } 486 } 487 return (hm); 488 } 489 490 /* 491 * Remove a mapping to a page from its mapping list. Must have 492 * the corresponding mapping list locked. 493 * Finds the mapping list entry with the given pte_t and 494 * unlinks it from the mapping list. 495 */ 496 hment_t * 497 hment_remove(page_t *pp, htable_t *ht, uint_t entry) 498 { 499 hment_t *prev = NULL; 500 hment_t *hm; 501 uint_t idx; 502 pfn_t pfn; 503 504 ASSERT(x86_hm_held(pp)); 505 506 /* 507 * Check if we have only one mapping embedded in the page_t. 508 */ 509 if (pp->p_embed) { 510 ASSERT(ht == (htable_t *)pp->p_mapping); 511 ASSERT(entry == pp->p_mlentry); 512 ASSERT(pp->p_share == 0); 513 pp->p_mapping = NULL; 514 pp->p_mlentry = 0; 515 pp->p_embed = 0; 516 return (NULL); 517 } 518 519 /* 520 * Otherwise it must be in the list of hments. 521 * Find the hment in the system-wide hash table and remove it. 522 */ 523 ASSERT(pp->p_share != 0); 524 pfn = pp->p_pagenum; 525 idx = HMENT_HASH(ht->ht_pfn, entry); 526 mutex_enter(HASH_MUTEX(idx)); 527 hm = hment_hash[idx]; 528 while (hm && (hm->hm_htable != ht || hm->hm_entry != entry || 529 hm->hm_pfn != pfn)) { 530 prev = hm; 531 hm = hm->hm_hashnext; 532 } 533 if (hm == NULL) { 534 panic("hment_remove() missing in hash table pp=%lx, ht=%lx," 535 "entry=0x%x hash index=0x%x", (uintptr_t)pp, (uintptr_t)ht, 536 entry, idx); 537 } 538 539 if (prev) 540 prev->hm_hashnext = hm->hm_hashnext; 541 else 542 hment_hash[idx] = hm->hm_hashnext; 543 mutex_exit(HASH_MUTEX(idx)); 544 545 /* 546 * Remove the hment from the page's mapping list 547 */ 548 if (hm->hm_next) 549 hm->hm_next->hm_prev = hm->hm_prev; 550 if (hm->hm_prev) 551 hm->hm_prev->hm_next = hm->hm_next; 552 else 553 pp->p_mapping = hm->hm_next; 554 555 --pp->p_share; 556 hm->hm_hashnext = NULL; 557 hm->hm_next = NULL; 558 hm->hm_prev = NULL; 559 560 return (hm); 561 } 562 563 /* 564 * Put initial hment's in the reserve pool. 565 */ 566 void 567 hment_reserve(uint_t count) 568 { 569 hment_t *hm; 570 571 count += hment_reserve_amount; 572 573 while (hment_reserve_count < count) { 574 hm = kmem_cache_alloc(hment_cache, KM_NOSLEEP); 575 if (hm == NULL) 576 return; 577 hment_put_reserve(hm); 578 } 579 } 580 581 /* 582 * Readjust the hment reserves after they may have been used. 583 */ 584 void 585 hment_adjust_reserve() 586 { 587 hment_t *hm; 588 589 /* 590 * Free up any excess reserves 591 */ 592 while (hment_reserve_count > hment_reserve_amount && 593 !USE_HAT_RESERVES()) { 594 hm = hment_get_reserve(); 595 if (hm == NULL) 596 return; 597 kmem_cache_free(hment_cache, hm); 598 } 599 } 600 601 /* 602 * initialize hment data structures 603 */ 604 void 605 hment_init(void) 606 { 607 int i; 608 int flags = KMC_NOHASH | KMC_NODEBUG; 609 610 /* 611 * Initialize kmem caches. On 32 bit kernel's we shut off 612 * debug information to save on precious kernel VA usage. 613 */ 614 hment_cache = kmem_cache_create("hment_t", 615 sizeof (hment_t), 0, NULL, NULL, NULL, 616 NULL, hat_memload_arena, flags); 617 618 hment_hash = kmem_zalloc(hment_hash_entries * sizeof (hment_t *), 619 KM_SLEEP); 620 621 for (i = 0; i < MLIST_NUM_LOCK; i++) 622 mutex_init(&mlist_lock[i], NULL, MUTEX_DEFAULT, NULL); 623 624 for (i = 0; i < HASH_NUM_LOCK; i++) 625 mutex_init(&hash_lock[i], NULL, MUTEX_DEFAULT, NULL); 626 627 628 } 629 630 /* 631 * return the number of mappings to a page 632 * 633 * Note there is no ASSERT() that the MUTEX is held for this. 634 * Hence the return value might be inaccurate if this is called without 635 * doing an x86_hm_enter(). 636 */ 637 uint_t 638 hment_mapcnt(page_t *pp) 639 { 640 uint_t cnt; 641 uint_t szc; 642 page_t *larger; 643 hment_t *hm; 644 645 x86_hm_enter(pp); 646 if (pp->p_mapping == NULL) 647 cnt = 0; 648 else if (pp->p_embed) 649 cnt = 1; 650 else 651 cnt = pp->p_share; 652 x86_hm_exit(pp); 653 654 /* 655 * walk through all larger mapping sizes counting mappings 656 */ 657 for (szc = 1; szc <= pp->p_szc; ++szc) { 658 larger = PP_GROUPLEADER(pp, szc); 659 if (larger == pp) /* don't double count large mappings */ 660 continue; 661 662 x86_hm_enter(larger); 663 if (larger->p_mapping != NULL) { 664 if (larger->p_embed && 665 ((htable_t *)larger->p_mapping)->ht_level == szc) { 666 ++cnt; 667 } else if (!larger->p_embed) { 668 for (hm = larger->p_mapping; hm; 669 hm = hm->hm_next) { 670 if (hm->hm_htable->ht_level == szc) 671 ++cnt; 672 } 673 } 674 } 675 x86_hm_exit(larger); 676 } 677 return (cnt); 678 } 679 680 /* 681 * We need to steal an hment. Walk through all the page_t's until we 682 * find one that has multiple mappings. Unload one of the mappings 683 * and reclaim that hment. Note that we'll save/restart the starting 684 * page to try and spread the pain. 685 */ 686 static page_t *last_page = NULL; 687 688 static hment_t * 689 hment_steal(void) 690 { 691 page_t *last = last_page; 692 page_t *pp = last; 693 hment_t *hm = NULL; 694 hment_t *hm2; 695 htable_t *ht; 696 uint_t found_one = 0; 697 698 HATSTAT_INC(hs_hm_steals); 699 if (pp == NULL) 700 last = pp = page_first(); 701 702 while (!found_one) { 703 HATSTAT_INC(hs_hm_steal_exam); 704 pp = page_next(pp); 705 if (pp == NULL) 706 pp = page_first(); 707 708 /* 709 * The loop and function exit here if nothing found to steal. 710 */ 711 if (pp == last) 712 return (NULL); 713 714 /* 715 * Only lock the page_t if it has hments. 716 */ 717 if (pp->p_mapping == NULL || pp->p_embed) 718 continue; 719 720 /* 721 * Search the mapping list for a usable mapping. 722 */ 723 x86_hm_enter(pp); 724 if (!pp->p_embed) { 725 for (hm = pp->p_mapping; hm; hm = hm->hm_next) { 726 ht = hm->hm_htable; 727 if (ht->ht_hat != kas.a_hat && 728 ht->ht_busy == 0 && 729 ht->ht_lock_cnt == 0) { 730 found_one = 1; 731 break; 732 } 733 } 734 } 735 if (!found_one) 736 x86_hm_exit(pp); 737 } 738 739 /* 740 * Steal the mapping we found. Note that hati_page_unmap() will 741 * do the x86_hm_exit(). 742 */ 743 hm2 = hati_page_unmap(pp, ht, hm->hm_entry); 744 ASSERT(hm2 == hm); 745 last_page = pp; 746 return (hm); 747 } 748