17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5a925c1ccSsudheer * Common Development and Distribution License (the "License"). 6a925c1ccSsudheer * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 2297704650Sjosephb * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #include <sys/types.h> 297c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 307c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 317c478bd9Sstevel@tonic-gate #include <sys/atomic.h> 327c478bd9Sstevel@tonic-gate #include <sys/bitmap.h> 337c478bd9Sstevel@tonic-gate #include <sys/systm.h> 347c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h> 357c478bd9Sstevel@tonic-gate #include <vm/hat.h> 367c478bd9Sstevel@tonic-gate #include <vm/vm_dep.h> 377c478bd9Sstevel@tonic-gate #include <vm/hat_i86.h> 387c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 39*b0aab85cSjosephb #include <sys/avl.h> 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate /* 437c478bd9Sstevel@tonic-gate * When pages are shared by more than one mapping, a list of these 447c478bd9Sstevel@tonic-gate * structs hangs off of the page_t connected by the hm_next and hm_prev 457c478bd9Sstevel@tonic-gate * fields. Every hment is also indexed by a system-wide hash table, using 46*b0aab85cSjosephb * hm_hashlink to connect the hments within each hash bucket. 477c478bd9Sstevel@tonic-gate */ 487c478bd9Sstevel@tonic-gate struct hment { 49*b0aab85cSjosephb avl_node_t hm_hashlink; /* links for hash table */ 507c478bd9Sstevel@tonic-gate struct hment *hm_next; /* next mapping of same page */ 517c478bd9Sstevel@tonic-gate struct hment *hm_prev; /* previous mapping of same page */ 527c478bd9Sstevel@tonic-gate htable_t *hm_htable; /* corresponding htable_t */ 53a925c1ccSsudheer pfn_t hm_pfn; /* mapping page frame number */ 547c478bd9Sstevel@tonic-gate uint16_t hm_entry; /* index of pte in htable */ 557c478bd9Sstevel@tonic-gate uint16_t hm_pad; /* explicitly expose compiler padding */ 567c478bd9Sstevel@tonic-gate #ifdef __amd64 577c478bd9Sstevel@tonic-gate uint32_t hm_pad2; /* explicitly expose compiler padding */ 587c478bd9Sstevel@tonic-gate #endif 597c478bd9Sstevel@tonic-gate }; 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate /* 627c478bd9Sstevel@tonic-gate * Value returned by hment_walk() when dealing with a single mapping 637c478bd9Sstevel@tonic-gate * embedded in the page_t. 647c478bd9Sstevel@tonic-gate */ 657c478bd9Sstevel@tonic-gate #define HMENT_EMBEDDED ((hment_t *)(uintptr_t)1) 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate kmem_cache_t *hment_cache; 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate /* 707c478bd9Sstevel@tonic-gate * The hment reserve is similar to the htable reserve, with the following 717c478bd9Sstevel@tonic-gate * exception. Hment's are never needed for HAT kmem allocs. 727c478bd9Sstevel@tonic-gate * 737c478bd9Sstevel@tonic-gate * The hment_reserve_amount variable is used, so that you can change it's 747c478bd9Sstevel@tonic-gate * value to zero via a kernel debugger to force stealing to get tested. 757c478bd9Sstevel@tonic-gate */ 767c478bd9Sstevel@tonic-gate #define HMENT_RESERVE_AMOUNT (200) /* currently a guess at right value. */ 777c478bd9Sstevel@tonic-gate uint_t hment_reserve_amount = HMENT_RESERVE_AMOUNT; 787c478bd9Sstevel@tonic-gate kmutex_t hment_reserve_mutex; 797c478bd9Sstevel@tonic-gate uint_t hment_reserve_count; 807c478bd9Sstevel@tonic-gate hment_t *hment_reserve_pool; 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate /* 83*b0aab85cSjosephb * All hments are stored in a system wide hash of AVL trees. 847c478bd9Sstevel@tonic-gate */ 857c478bd9Sstevel@tonic-gate #define HMENT_HASH_SIZE (64 * 1024) 867c478bd9Sstevel@tonic-gate static uint_t hment_hash_entries = HMENT_HASH_SIZE; 87*b0aab85cSjosephb static avl_tree_t *hment_table; 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate /* 907c478bd9Sstevel@tonic-gate * Lots of highly shared pages will have the same value for "entry" (consider 917c478bd9Sstevel@tonic-gate * the starting address of "xterm" or "sh"). So we'll distinguish them by 927c478bd9Sstevel@tonic-gate * adding the pfn of the page table into both the high bits. 937c478bd9Sstevel@tonic-gate * The shift by 9 corresponds to the range of values for entry (0..511). 947c478bd9Sstevel@tonic-gate */ 957c478bd9Sstevel@tonic-gate #define HMENT_HASH(pfn, entry) (uint32_t) \ 967c478bd9Sstevel@tonic-gate ((((pfn) << 9) + entry + pfn) & (hment_hash_entries - 1)) 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate /* 997c478bd9Sstevel@tonic-gate * "mlist_lock" is a hashed mutex lock for protecting per-page mapping 1007c478bd9Sstevel@tonic-gate * lists and "hash_lock" is a similar lock protecting the hment hash 1017c478bd9Sstevel@tonic-gate * table. The hashed approach is taken to avoid the spatial overhead of 1027c478bd9Sstevel@tonic-gate * maintaining a separate lock for each page, while still achieving better 1037c478bd9Sstevel@tonic-gate * scalability than a single lock would allow. 1047c478bd9Sstevel@tonic-gate */ 105*b0aab85cSjosephb #define MLIST_NUM_LOCK 2048 /* must be power of two */ 106*b0aab85cSjosephb static kmutex_t *mlist_lock; 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate /* 1097c478bd9Sstevel@tonic-gate * the shift by 9 is so that all large pages don't use the same hash bucket 1107c478bd9Sstevel@tonic-gate */ 1117c478bd9Sstevel@tonic-gate #define MLIST_MUTEX(pp) \ 1127c478bd9Sstevel@tonic-gate &mlist_lock[((pp)->p_pagenum + ((pp)->p_pagenum >> 9)) & \ 1137c478bd9Sstevel@tonic-gate (MLIST_NUM_LOCK - 1)] 1147c478bd9Sstevel@tonic-gate 115*b0aab85cSjosephb #define HASH_NUM_LOCK 2048 /* must be power of two */ 116*b0aab85cSjosephb static kmutex_t *hash_lock; 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate #define HASH_MUTEX(idx) &hash_lock[(idx) & (HASH_NUM_LOCK-1)] 1197c478bd9Sstevel@tonic-gate 120*b0aab85cSjosephb static avl_node_t null_avl_link; /* always zero */ 1217c478bd9Sstevel@tonic-gate static hment_t *hment_steal(void); 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate /* 124*b0aab85cSjosephb * Utility to compare hment_t's for use in AVL tree. The ordering 125*b0aab85cSjosephb * is entirely arbitrary and is just so that the AVL algorithm works. 126*b0aab85cSjosephb */ 127*b0aab85cSjosephb static int 128*b0aab85cSjosephb hment_compare(const void *hm1, const void *hm2) 129*b0aab85cSjosephb { 130*b0aab85cSjosephb hment_t *h1 = (hment_t *)hm1; 131*b0aab85cSjosephb hment_t *h2 = (hment_t *)hm2; 132*b0aab85cSjosephb long diff; 133*b0aab85cSjosephb 134*b0aab85cSjosephb diff = (uintptr_t)h1->hm_htable - (uintptr_t)h2->hm_htable; 135*b0aab85cSjosephb if (diff == 0) { 136*b0aab85cSjosephb diff = h1->hm_entry - h2->hm_entry; 137*b0aab85cSjosephb if (diff == 0) 138*b0aab85cSjosephb diff = h1->hm_pfn - h2->hm_pfn; 139*b0aab85cSjosephb } 140*b0aab85cSjosephb if (diff < 0) 141*b0aab85cSjosephb diff = -1; 142*b0aab85cSjosephb else if (diff > 0) 143*b0aab85cSjosephb diff = 1; 144*b0aab85cSjosephb return (diff); 145*b0aab85cSjosephb } 146*b0aab85cSjosephb 147*b0aab85cSjosephb /* 1487c478bd9Sstevel@tonic-gate * put one hment onto the reserves list 1497c478bd9Sstevel@tonic-gate */ 1507c478bd9Sstevel@tonic-gate static void 1517c478bd9Sstevel@tonic-gate hment_put_reserve(hment_t *hm) 1527c478bd9Sstevel@tonic-gate { 1537c478bd9Sstevel@tonic-gate HATSTAT_INC(hs_hm_put_reserve); 1547c478bd9Sstevel@tonic-gate mutex_enter(&hment_reserve_mutex); 1557c478bd9Sstevel@tonic-gate hm->hm_next = hment_reserve_pool; 1567c478bd9Sstevel@tonic-gate hment_reserve_pool = hm; 1577c478bd9Sstevel@tonic-gate ++hment_reserve_count; 1587c478bd9Sstevel@tonic-gate mutex_exit(&hment_reserve_mutex); 1597c478bd9Sstevel@tonic-gate } 1607c478bd9Sstevel@tonic-gate 1617c478bd9Sstevel@tonic-gate /* 1627c478bd9Sstevel@tonic-gate * Take one hment from the reserve. 1637c478bd9Sstevel@tonic-gate */ 1647c478bd9Sstevel@tonic-gate static hment_t * 1657c478bd9Sstevel@tonic-gate hment_get_reserve(void) 1667c478bd9Sstevel@tonic-gate { 1677c478bd9Sstevel@tonic-gate hment_t *hm = NULL; 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate /* 1707c478bd9Sstevel@tonic-gate * We rely on a "donation system" to refill the hment reserve 1717c478bd9Sstevel@tonic-gate * list, which only takes place when we are allocating hments for 1727c478bd9Sstevel@tonic-gate * user mappings. It is theoretically possible that an incredibly 1737c478bd9Sstevel@tonic-gate * long string of kernel hment_alloc()s with no intervening user 1747c478bd9Sstevel@tonic-gate * hment_alloc()s could exhaust that pool. 1757c478bd9Sstevel@tonic-gate */ 1767c478bd9Sstevel@tonic-gate HATSTAT_INC(hs_hm_get_reserve); 1777c478bd9Sstevel@tonic-gate mutex_enter(&hment_reserve_mutex); 1787c478bd9Sstevel@tonic-gate if (hment_reserve_count != 0) { 1797c478bd9Sstevel@tonic-gate hm = hment_reserve_pool; 1807c478bd9Sstevel@tonic-gate hment_reserve_pool = hm->hm_next; 1817c478bd9Sstevel@tonic-gate --hment_reserve_count; 1827c478bd9Sstevel@tonic-gate } 1837c478bd9Sstevel@tonic-gate mutex_exit(&hment_reserve_mutex); 1847c478bd9Sstevel@tonic-gate return (hm); 1857c478bd9Sstevel@tonic-gate } 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate /* 1887c478bd9Sstevel@tonic-gate * Allocate an hment 1897c478bd9Sstevel@tonic-gate */ 1907c478bd9Sstevel@tonic-gate static hment_t * 1917c478bd9Sstevel@tonic-gate hment_alloc() 1927c478bd9Sstevel@tonic-gate { 1937c478bd9Sstevel@tonic-gate int km_flag = can_steal_post_boot ? KM_NOSLEEP : KM_SLEEP; 1947c478bd9Sstevel@tonic-gate hment_t *hm = NULL; 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate /* 1977c478bd9Sstevel@tonic-gate * If we aren't using the reserves, try using kmem to get an hment. 1987c478bd9Sstevel@tonic-gate * Donate any successful allocations to reserves if low. 1997c478bd9Sstevel@tonic-gate * 2007c478bd9Sstevel@tonic-gate * If we're in panic, resort to using the reserves. 2017c478bd9Sstevel@tonic-gate */ 2027c478bd9Sstevel@tonic-gate HATSTAT_INC(hs_hm_alloc); 20397704650Sjosephb if (!USE_HAT_RESERVES()) { 2047c478bd9Sstevel@tonic-gate for (;;) { 2057c478bd9Sstevel@tonic-gate hm = kmem_cache_alloc(hment_cache, km_flag); 2067a1d442cSjosephb if (hm == NULL || 2077a1d442cSjosephb USE_HAT_RESERVES() || 20897704650Sjosephb hment_reserve_count >= hment_reserve_amount) 2097c478bd9Sstevel@tonic-gate break; 2107c478bd9Sstevel@tonic-gate hment_put_reserve(hm); 2117c478bd9Sstevel@tonic-gate } 2127c478bd9Sstevel@tonic-gate } 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate /* 2157c478bd9Sstevel@tonic-gate * If allocation failed, we need to tap the reserves or steal 2167c478bd9Sstevel@tonic-gate */ 2177c478bd9Sstevel@tonic-gate if (hm == NULL) { 21897704650Sjosephb if (USE_HAT_RESERVES()) 2197c478bd9Sstevel@tonic-gate hm = hment_get_reserve(); 2207c478bd9Sstevel@tonic-gate 2217c478bd9Sstevel@tonic-gate /* 2227c478bd9Sstevel@tonic-gate * If we still haven't gotten an hment, attempt to steal one by 2237c478bd9Sstevel@tonic-gate * victimizing a mapping in a user htable. 2247c478bd9Sstevel@tonic-gate */ 2257c478bd9Sstevel@tonic-gate if (hm == NULL && can_steal_post_boot) 2267c478bd9Sstevel@tonic-gate hm = hment_steal(); 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate /* 2297c478bd9Sstevel@tonic-gate * we're in dire straights, try the reserve 2307c478bd9Sstevel@tonic-gate */ 2317c478bd9Sstevel@tonic-gate if (hm == NULL) 2327c478bd9Sstevel@tonic-gate hm = hment_get_reserve(); 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate /* 2357c478bd9Sstevel@tonic-gate * still no hment is a serious problem. 2367c478bd9Sstevel@tonic-gate */ 2377c478bd9Sstevel@tonic-gate if (hm == NULL) 2387c478bd9Sstevel@tonic-gate panic("hment_alloc(): no reserve, couldn't steal"); 2397c478bd9Sstevel@tonic-gate } 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate hm->hm_entry = 0; 2437c478bd9Sstevel@tonic-gate hm->hm_htable = NULL; 244*b0aab85cSjosephb hm->hm_hashlink = null_avl_link; 2457c478bd9Sstevel@tonic-gate hm->hm_next = NULL; 2467c478bd9Sstevel@tonic-gate hm->hm_prev = NULL; 247a925c1ccSsudheer hm->hm_pfn = PFN_INVALID; 2487c478bd9Sstevel@tonic-gate return (hm); 2497c478bd9Sstevel@tonic-gate } 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate /* 2527c478bd9Sstevel@tonic-gate * Free an hment, possibly to the reserves list when called from the 2537c478bd9Sstevel@tonic-gate * thread using the reserves. For example, when freeing an hment during an 2547c478bd9Sstevel@tonic-gate * htable_steal(), we can't recurse into the kmem allocator, so we just 2557c478bd9Sstevel@tonic-gate * push the hment onto the reserve list. 2567c478bd9Sstevel@tonic-gate */ 2577c478bd9Sstevel@tonic-gate void 2587c478bd9Sstevel@tonic-gate hment_free(hment_t *hm) 2597c478bd9Sstevel@tonic-gate { 2607c478bd9Sstevel@tonic-gate #ifdef DEBUG 2617c478bd9Sstevel@tonic-gate /* 2627c478bd9Sstevel@tonic-gate * zero out all fields to try and force any race conditions to segfault 2637c478bd9Sstevel@tonic-gate */ 2647c478bd9Sstevel@tonic-gate bzero(hm, sizeof (*hm)); 2657c478bd9Sstevel@tonic-gate #endif 2667c478bd9Sstevel@tonic-gate HATSTAT_INC(hs_hm_free); 26797704650Sjosephb if (USE_HAT_RESERVES() || 268aac11643Sjosephb hment_reserve_count < hment_reserve_amount) { 2697c478bd9Sstevel@tonic-gate hment_put_reserve(hm); 270aac11643Sjosephb } else { 2717c478bd9Sstevel@tonic-gate kmem_cache_free(hment_cache, hm); 272aac11643Sjosephb hment_adjust_reserve(); 273aac11643Sjosephb } 2747c478bd9Sstevel@tonic-gate } 2757c478bd9Sstevel@tonic-gate 276*b0aab85cSjosephb /* 277*b0aab85cSjosephb * These must test for mlist_lock not having been allocated yet. 278*b0aab85cSjosephb * We just ignore locking in that case, as it means were in early 279*b0aab85cSjosephb * single threaded startup. 280*b0aab85cSjosephb */ 2817c478bd9Sstevel@tonic-gate int 2827c478bd9Sstevel@tonic-gate x86_hm_held(page_t *pp) 2837c478bd9Sstevel@tonic-gate { 2847c478bd9Sstevel@tonic-gate ASSERT(pp != NULL); 285*b0aab85cSjosephb if (mlist_lock == NULL) 286*b0aab85cSjosephb return (1); 2877c478bd9Sstevel@tonic-gate return (MUTEX_HELD(MLIST_MUTEX(pp))); 2887c478bd9Sstevel@tonic-gate } 2897c478bd9Sstevel@tonic-gate 2907c478bd9Sstevel@tonic-gate void 2917c478bd9Sstevel@tonic-gate x86_hm_enter(page_t *pp) 2927c478bd9Sstevel@tonic-gate { 2937c478bd9Sstevel@tonic-gate ASSERT(pp != NULL); 294*b0aab85cSjosephb if (mlist_lock != NULL) 2957c478bd9Sstevel@tonic-gate mutex_enter(MLIST_MUTEX(pp)); 2967c478bd9Sstevel@tonic-gate } 2977c478bd9Sstevel@tonic-gate 2987c478bd9Sstevel@tonic-gate void 2997c478bd9Sstevel@tonic-gate x86_hm_exit(page_t *pp) 3007c478bd9Sstevel@tonic-gate { 3017c478bd9Sstevel@tonic-gate ASSERT(pp != NULL); 302*b0aab85cSjosephb if (mlist_lock != NULL) 3037c478bd9Sstevel@tonic-gate mutex_exit(MLIST_MUTEX(pp)); 3047c478bd9Sstevel@tonic-gate } 3057c478bd9Sstevel@tonic-gate 3067c478bd9Sstevel@tonic-gate /* 3077c478bd9Sstevel@tonic-gate * Internal routine to add a full hment to a page_t mapping list 3087c478bd9Sstevel@tonic-gate */ 3097c478bd9Sstevel@tonic-gate static void 3107c478bd9Sstevel@tonic-gate hment_insert(hment_t *hm, page_t *pp) 3117c478bd9Sstevel@tonic-gate { 3127c478bd9Sstevel@tonic-gate uint_t idx; 3137c478bd9Sstevel@tonic-gate 3147c478bd9Sstevel@tonic-gate ASSERT(x86_hm_held(pp)); 3157c478bd9Sstevel@tonic-gate ASSERT(!pp->p_embed); 3167c478bd9Sstevel@tonic-gate 3177c478bd9Sstevel@tonic-gate /* 3187c478bd9Sstevel@tonic-gate * Add the hment to the page's mapping list. 3197c478bd9Sstevel@tonic-gate */ 3207c478bd9Sstevel@tonic-gate ++pp->p_share; 3217c478bd9Sstevel@tonic-gate hm->hm_next = pp->p_mapping; 3227c478bd9Sstevel@tonic-gate if (pp->p_mapping != NULL) 3237c478bd9Sstevel@tonic-gate ((hment_t *)pp->p_mapping)->hm_prev = hm; 3247c478bd9Sstevel@tonic-gate pp->p_mapping = hm; 3257c478bd9Sstevel@tonic-gate 3267c478bd9Sstevel@tonic-gate /* 3277c478bd9Sstevel@tonic-gate * Add the hment to the system-wide hash table. 3287c478bd9Sstevel@tonic-gate */ 3297c478bd9Sstevel@tonic-gate idx = HMENT_HASH(hm->hm_htable->ht_pfn, hm->hm_entry); 3307c478bd9Sstevel@tonic-gate 3317c478bd9Sstevel@tonic-gate mutex_enter(HASH_MUTEX(idx)); 332*b0aab85cSjosephb avl_add(&hment_table[idx], hm); 3337c478bd9Sstevel@tonic-gate mutex_exit(HASH_MUTEX(idx)); 3347c478bd9Sstevel@tonic-gate } 3357c478bd9Sstevel@tonic-gate 3367c478bd9Sstevel@tonic-gate /* 3377c478bd9Sstevel@tonic-gate * Prepare a mapping list entry to the given page. 3387c478bd9Sstevel@tonic-gate * 3397c478bd9Sstevel@tonic-gate * There are 4 different situations to deal with: 3407c478bd9Sstevel@tonic-gate * 3417c478bd9Sstevel@tonic-gate * - Adding the first mapping to a page_t as an embedded hment 3427c478bd9Sstevel@tonic-gate * - Refaulting on an existing embedded mapping 3437c478bd9Sstevel@tonic-gate * - Upgrading an embedded mapping when adding a 2nd mapping 3447c478bd9Sstevel@tonic-gate * - Adding another mapping to a page_t that already has multiple mappings 3457c478bd9Sstevel@tonic-gate * note we don't optimized for the refaulting case here. 3467c478bd9Sstevel@tonic-gate * 3477c478bd9Sstevel@tonic-gate * Due to competition with other threads that may be mapping/unmapping the 3487c478bd9Sstevel@tonic-gate * same page and the need to drop all locks while allocating hments, any or 3497c478bd9Sstevel@tonic-gate * all of the 3 situations can occur (and in almost any order) in any given 3507c478bd9Sstevel@tonic-gate * call. Isn't this fun! 3517c478bd9Sstevel@tonic-gate */ 3527c478bd9Sstevel@tonic-gate hment_t * 3537c478bd9Sstevel@tonic-gate hment_prepare(htable_t *htable, uint_t entry, page_t *pp) 3547c478bd9Sstevel@tonic-gate { 3557c478bd9Sstevel@tonic-gate hment_t *hm = NULL; 3567c478bd9Sstevel@tonic-gate 3577c478bd9Sstevel@tonic-gate ASSERT(x86_hm_held(pp)); 3587c478bd9Sstevel@tonic-gate 3597c478bd9Sstevel@tonic-gate for (;;) { 3607c478bd9Sstevel@tonic-gate 3617c478bd9Sstevel@tonic-gate /* 3627c478bd9Sstevel@tonic-gate * The most common case is establishing the first mapping to a 3637c478bd9Sstevel@tonic-gate * page, so check that first. This doesn't need any allocated 3647c478bd9Sstevel@tonic-gate * hment. 3657c478bd9Sstevel@tonic-gate */ 3667c478bd9Sstevel@tonic-gate if (pp->p_mapping == NULL) { 3677c478bd9Sstevel@tonic-gate ASSERT(!pp->p_embed); 3687c478bd9Sstevel@tonic-gate ASSERT(pp->p_share == 0); 3697c478bd9Sstevel@tonic-gate if (hm == NULL) 3707c478bd9Sstevel@tonic-gate break; 3717c478bd9Sstevel@tonic-gate 3727c478bd9Sstevel@tonic-gate /* 3737c478bd9Sstevel@tonic-gate * we had an hment already, so free it and retry 3747c478bd9Sstevel@tonic-gate */ 3757c478bd9Sstevel@tonic-gate goto free_and_continue; 3767c478bd9Sstevel@tonic-gate } 3777c478bd9Sstevel@tonic-gate 3787c478bd9Sstevel@tonic-gate /* 3797c478bd9Sstevel@tonic-gate * If there is an embedded mapping, we may need to 3807c478bd9Sstevel@tonic-gate * convert it to an hment. 3817c478bd9Sstevel@tonic-gate */ 3827c478bd9Sstevel@tonic-gate if (pp->p_embed) { 3837c478bd9Sstevel@tonic-gate 3847c478bd9Sstevel@tonic-gate /* should point to htable */ 3857c478bd9Sstevel@tonic-gate ASSERT(pp->p_mapping != NULL); 3867c478bd9Sstevel@tonic-gate 3877c478bd9Sstevel@tonic-gate /* 3887c478bd9Sstevel@tonic-gate * If we are faulting on a pre-existing mapping 3897c478bd9Sstevel@tonic-gate * there is no need to promote/allocate a new hment. 3907c478bd9Sstevel@tonic-gate * This happens a lot due to segmap. 3917c478bd9Sstevel@tonic-gate */ 3927c478bd9Sstevel@tonic-gate if (pp->p_mapping == htable && pp->p_mlentry == entry) { 3937c478bd9Sstevel@tonic-gate if (hm == NULL) 3947c478bd9Sstevel@tonic-gate break; 3957c478bd9Sstevel@tonic-gate goto free_and_continue; 3967c478bd9Sstevel@tonic-gate } 3977c478bd9Sstevel@tonic-gate 3987c478bd9Sstevel@tonic-gate /* 3997c478bd9Sstevel@tonic-gate * If we have an hment allocated, use it to promote the 4007c478bd9Sstevel@tonic-gate * existing embedded mapping. 4017c478bd9Sstevel@tonic-gate */ 4027c478bd9Sstevel@tonic-gate if (hm != NULL) { 4037c478bd9Sstevel@tonic-gate hm->hm_htable = pp->p_mapping; 4047c478bd9Sstevel@tonic-gate hm->hm_entry = pp->p_mlentry; 405a925c1ccSsudheer hm->hm_pfn = pp->p_pagenum; 4067c478bd9Sstevel@tonic-gate pp->p_mapping = NULL; 4077c478bd9Sstevel@tonic-gate pp->p_share = 0; 4087c478bd9Sstevel@tonic-gate pp->p_embed = 0; 4097c478bd9Sstevel@tonic-gate hment_insert(hm, pp); 4107c478bd9Sstevel@tonic-gate } 4117c478bd9Sstevel@tonic-gate 4127c478bd9Sstevel@tonic-gate /* 4137c478bd9Sstevel@tonic-gate * We either didn't have an hment allocated or we just 4147c478bd9Sstevel@tonic-gate * used it for the embedded mapping. In either case, 4157c478bd9Sstevel@tonic-gate * allocate another hment and restart. 4167c478bd9Sstevel@tonic-gate */ 4177c478bd9Sstevel@tonic-gate goto allocate_and_continue; 4187c478bd9Sstevel@tonic-gate } 4197c478bd9Sstevel@tonic-gate 4207c478bd9Sstevel@tonic-gate /* 4217c478bd9Sstevel@tonic-gate * Last possibility is that we're adding an hment to a list 4227c478bd9Sstevel@tonic-gate * of hments. 4237c478bd9Sstevel@tonic-gate */ 4247c478bd9Sstevel@tonic-gate if (hm != NULL) 4257c478bd9Sstevel@tonic-gate break; 4267c478bd9Sstevel@tonic-gate allocate_and_continue: 4277c478bd9Sstevel@tonic-gate x86_hm_exit(pp); 4287c478bd9Sstevel@tonic-gate hm = hment_alloc(); 4297c478bd9Sstevel@tonic-gate x86_hm_enter(pp); 4307c478bd9Sstevel@tonic-gate continue; 4317c478bd9Sstevel@tonic-gate 4327c478bd9Sstevel@tonic-gate free_and_continue: 4337c478bd9Sstevel@tonic-gate /* 4347c478bd9Sstevel@tonic-gate * we allocated an hment already, free it and retry 4357c478bd9Sstevel@tonic-gate */ 4367c478bd9Sstevel@tonic-gate x86_hm_exit(pp); 4377c478bd9Sstevel@tonic-gate hment_free(hm); 4387c478bd9Sstevel@tonic-gate hm = NULL; 4397c478bd9Sstevel@tonic-gate x86_hm_enter(pp); 4407c478bd9Sstevel@tonic-gate } 4417c478bd9Sstevel@tonic-gate ASSERT(x86_hm_held(pp)); 4427c478bd9Sstevel@tonic-gate return (hm); 4437c478bd9Sstevel@tonic-gate } 4447c478bd9Sstevel@tonic-gate 4457c478bd9Sstevel@tonic-gate /* 4467c478bd9Sstevel@tonic-gate * Record a mapping list entry for the htable/entry to the given page. 4477c478bd9Sstevel@tonic-gate * 4487c478bd9Sstevel@tonic-gate * hment_prepare() should have properly set up the situation. 4497c478bd9Sstevel@tonic-gate */ 4507c478bd9Sstevel@tonic-gate void 4517c478bd9Sstevel@tonic-gate hment_assign(htable_t *htable, uint_t entry, page_t *pp, hment_t *hm) 4527c478bd9Sstevel@tonic-gate { 4537c478bd9Sstevel@tonic-gate ASSERT(x86_hm_held(pp)); 4547c478bd9Sstevel@tonic-gate 4557c478bd9Sstevel@tonic-gate /* 4567c478bd9Sstevel@tonic-gate * The most common case is establishing the first mapping to a 4577c478bd9Sstevel@tonic-gate * page, so check that first. This doesn't need any allocated 4587c478bd9Sstevel@tonic-gate * hment. 4597c478bd9Sstevel@tonic-gate */ 4607c478bd9Sstevel@tonic-gate if (pp->p_mapping == NULL) { 4617c478bd9Sstevel@tonic-gate ASSERT(hm == NULL); 4627c478bd9Sstevel@tonic-gate ASSERT(!pp->p_embed); 4637c478bd9Sstevel@tonic-gate ASSERT(pp->p_share == 0); 4647c478bd9Sstevel@tonic-gate pp->p_embed = 1; 4657c478bd9Sstevel@tonic-gate pp->p_mapping = htable; 4667c478bd9Sstevel@tonic-gate pp->p_mlentry = entry; 4677c478bd9Sstevel@tonic-gate return; 4687c478bd9Sstevel@tonic-gate } 4697c478bd9Sstevel@tonic-gate 4707c478bd9Sstevel@tonic-gate /* 4717c478bd9Sstevel@tonic-gate * We should never get here with a pre-existing embedded maping 4727c478bd9Sstevel@tonic-gate */ 4737c478bd9Sstevel@tonic-gate ASSERT(!pp->p_embed); 4747c478bd9Sstevel@tonic-gate 4757c478bd9Sstevel@tonic-gate /* 4767c478bd9Sstevel@tonic-gate * add the new hment to the mapping list 4777c478bd9Sstevel@tonic-gate */ 4787c478bd9Sstevel@tonic-gate ASSERT(hm != NULL); 4797c478bd9Sstevel@tonic-gate hm->hm_htable = htable; 4807c478bd9Sstevel@tonic-gate hm->hm_entry = entry; 481a925c1ccSsudheer hm->hm_pfn = pp->p_pagenum; 4827c478bd9Sstevel@tonic-gate hment_insert(hm, pp); 4837c478bd9Sstevel@tonic-gate } 4847c478bd9Sstevel@tonic-gate 4857c478bd9Sstevel@tonic-gate /* 4867c478bd9Sstevel@tonic-gate * Walk through the mappings for a page. 4877c478bd9Sstevel@tonic-gate * 4887c478bd9Sstevel@tonic-gate * must already have done an x86_hm_enter() 4897c478bd9Sstevel@tonic-gate */ 4907c478bd9Sstevel@tonic-gate hment_t * 4917c478bd9Sstevel@tonic-gate hment_walk(page_t *pp, htable_t **ht, uint_t *entry, hment_t *prev) 4927c478bd9Sstevel@tonic-gate { 4937c478bd9Sstevel@tonic-gate hment_t *hm; 4947c478bd9Sstevel@tonic-gate 4957c478bd9Sstevel@tonic-gate ASSERT(x86_hm_held(pp)); 4967c478bd9Sstevel@tonic-gate 4977c478bd9Sstevel@tonic-gate if (pp->p_embed) { 4987c478bd9Sstevel@tonic-gate if (prev == NULL) { 4997c478bd9Sstevel@tonic-gate *ht = (htable_t *)pp->p_mapping; 5007c478bd9Sstevel@tonic-gate *entry = pp->p_mlentry; 5017c478bd9Sstevel@tonic-gate hm = HMENT_EMBEDDED; 5027c478bd9Sstevel@tonic-gate } else { 5037c478bd9Sstevel@tonic-gate ASSERT(prev == HMENT_EMBEDDED); 5047c478bd9Sstevel@tonic-gate hm = NULL; 5057c478bd9Sstevel@tonic-gate } 5067c478bd9Sstevel@tonic-gate } else { 5077c478bd9Sstevel@tonic-gate if (prev == NULL) { 5087c478bd9Sstevel@tonic-gate ASSERT(prev != HMENT_EMBEDDED); 5097c478bd9Sstevel@tonic-gate hm = (hment_t *)pp->p_mapping; 5107c478bd9Sstevel@tonic-gate } else { 5117c478bd9Sstevel@tonic-gate hm = prev->hm_next; 5127c478bd9Sstevel@tonic-gate } 5137c478bd9Sstevel@tonic-gate 5147c478bd9Sstevel@tonic-gate if (hm != NULL) { 5157c478bd9Sstevel@tonic-gate *ht = hm->hm_htable; 5167c478bd9Sstevel@tonic-gate *entry = hm->hm_entry; 5177c478bd9Sstevel@tonic-gate } 5187c478bd9Sstevel@tonic-gate } 5197c478bd9Sstevel@tonic-gate return (hm); 5207c478bd9Sstevel@tonic-gate } 5217c478bd9Sstevel@tonic-gate 5227c478bd9Sstevel@tonic-gate /* 5237c478bd9Sstevel@tonic-gate * Remove a mapping to a page from its mapping list. Must have 5247c478bd9Sstevel@tonic-gate * the corresponding mapping list locked. 5257c478bd9Sstevel@tonic-gate * Finds the mapping list entry with the given pte_t and 5267c478bd9Sstevel@tonic-gate * unlinks it from the mapping list. 5277c478bd9Sstevel@tonic-gate */ 5287c478bd9Sstevel@tonic-gate hment_t * 5297c478bd9Sstevel@tonic-gate hment_remove(page_t *pp, htable_t *ht, uint_t entry) 5307c478bd9Sstevel@tonic-gate { 531*b0aab85cSjosephb hment_t dummy; 532*b0aab85cSjosephb avl_index_t where; 5337c478bd9Sstevel@tonic-gate hment_t *hm; 5347c478bd9Sstevel@tonic-gate uint_t idx; 5357c478bd9Sstevel@tonic-gate 5367c478bd9Sstevel@tonic-gate ASSERT(x86_hm_held(pp)); 5377c478bd9Sstevel@tonic-gate 5387c478bd9Sstevel@tonic-gate /* 5397c478bd9Sstevel@tonic-gate * Check if we have only one mapping embedded in the page_t. 5407c478bd9Sstevel@tonic-gate */ 5417c478bd9Sstevel@tonic-gate if (pp->p_embed) { 5427c478bd9Sstevel@tonic-gate ASSERT(ht == (htable_t *)pp->p_mapping); 5437c478bd9Sstevel@tonic-gate ASSERT(entry == pp->p_mlentry); 5447c478bd9Sstevel@tonic-gate ASSERT(pp->p_share == 0); 5457c478bd9Sstevel@tonic-gate pp->p_mapping = NULL; 5467c478bd9Sstevel@tonic-gate pp->p_mlentry = 0; 5477c478bd9Sstevel@tonic-gate pp->p_embed = 0; 5487c478bd9Sstevel@tonic-gate return (NULL); 5497c478bd9Sstevel@tonic-gate } 5507c478bd9Sstevel@tonic-gate 5517c478bd9Sstevel@tonic-gate /* 5527c478bd9Sstevel@tonic-gate * Otherwise it must be in the list of hments. 5537c478bd9Sstevel@tonic-gate * Find the hment in the system-wide hash table and remove it. 5547c478bd9Sstevel@tonic-gate */ 5557c478bd9Sstevel@tonic-gate ASSERT(pp->p_share != 0); 556*b0aab85cSjosephb dummy.hm_htable = ht; 557*b0aab85cSjosephb dummy.hm_entry = entry; 558*b0aab85cSjosephb dummy.hm_pfn = pp->p_pagenum; 5597c478bd9Sstevel@tonic-gate idx = HMENT_HASH(ht->ht_pfn, entry); 5607c478bd9Sstevel@tonic-gate mutex_enter(HASH_MUTEX(idx)); 561*b0aab85cSjosephb hm = avl_find(&hment_table[idx], &dummy, &where); 562*b0aab85cSjosephb if (hm == NULL) 563aa2ed9e5Sjosephb panic("hment_remove() missing in hash table pp=%lx, ht=%lx," 564aa2ed9e5Sjosephb "entry=0x%x hash index=0x%x", (uintptr_t)pp, (uintptr_t)ht, 565aa2ed9e5Sjosephb entry, idx); 566*b0aab85cSjosephb avl_remove(&hment_table[idx], hm); 5677c478bd9Sstevel@tonic-gate mutex_exit(HASH_MUTEX(idx)); 5687c478bd9Sstevel@tonic-gate 5697c478bd9Sstevel@tonic-gate /* 5707c478bd9Sstevel@tonic-gate * Remove the hment from the page's mapping list 5717c478bd9Sstevel@tonic-gate */ 5727c478bd9Sstevel@tonic-gate if (hm->hm_next) 5737c478bd9Sstevel@tonic-gate hm->hm_next->hm_prev = hm->hm_prev; 5747c478bd9Sstevel@tonic-gate if (hm->hm_prev) 5757c478bd9Sstevel@tonic-gate hm->hm_prev->hm_next = hm->hm_next; 5767c478bd9Sstevel@tonic-gate else 5777c478bd9Sstevel@tonic-gate pp->p_mapping = hm->hm_next; 5787c478bd9Sstevel@tonic-gate 5797c478bd9Sstevel@tonic-gate --pp->p_share; 580*b0aab85cSjosephb hm->hm_hashlink = null_avl_link; 5817c478bd9Sstevel@tonic-gate hm->hm_next = NULL; 5827c478bd9Sstevel@tonic-gate hm->hm_prev = NULL; 5837c478bd9Sstevel@tonic-gate 5847c478bd9Sstevel@tonic-gate return (hm); 5857c478bd9Sstevel@tonic-gate } 5867c478bd9Sstevel@tonic-gate 5877c478bd9Sstevel@tonic-gate /* 5887c478bd9Sstevel@tonic-gate * Put initial hment's in the reserve pool. 5897c478bd9Sstevel@tonic-gate */ 5907c478bd9Sstevel@tonic-gate void 5917c478bd9Sstevel@tonic-gate hment_reserve(uint_t count) 5927c478bd9Sstevel@tonic-gate { 5937c478bd9Sstevel@tonic-gate hment_t *hm; 5947c478bd9Sstevel@tonic-gate 5957c478bd9Sstevel@tonic-gate count += hment_reserve_amount; 5967c478bd9Sstevel@tonic-gate 5977c478bd9Sstevel@tonic-gate while (hment_reserve_count < count) { 5987c478bd9Sstevel@tonic-gate hm = kmem_cache_alloc(hment_cache, KM_NOSLEEP); 5997c478bd9Sstevel@tonic-gate if (hm == NULL) 6007c478bd9Sstevel@tonic-gate return; 6017c478bd9Sstevel@tonic-gate hment_put_reserve(hm); 6027c478bd9Sstevel@tonic-gate } 6037c478bd9Sstevel@tonic-gate } 6047c478bd9Sstevel@tonic-gate 6057c478bd9Sstevel@tonic-gate /* 6067c478bd9Sstevel@tonic-gate * Readjust the hment reserves after they may have been used. 6077c478bd9Sstevel@tonic-gate */ 6087c478bd9Sstevel@tonic-gate void 6097c478bd9Sstevel@tonic-gate hment_adjust_reserve() 6107c478bd9Sstevel@tonic-gate { 6117c478bd9Sstevel@tonic-gate hment_t *hm; 6127c478bd9Sstevel@tonic-gate 6137c478bd9Sstevel@tonic-gate /* 6147c478bd9Sstevel@tonic-gate * Free up any excess reserves 6157c478bd9Sstevel@tonic-gate */ 616aac11643Sjosephb while (hment_reserve_count > hment_reserve_amount && 617aac11643Sjosephb !USE_HAT_RESERVES()) { 6187c478bd9Sstevel@tonic-gate hm = hment_get_reserve(); 6197c478bd9Sstevel@tonic-gate if (hm == NULL) 6207c478bd9Sstevel@tonic-gate return; 621aac11643Sjosephb kmem_cache_free(hment_cache, hm); 6227c478bd9Sstevel@tonic-gate } 6237c478bd9Sstevel@tonic-gate } 6247c478bd9Sstevel@tonic-gate 6257c478bd9Sstevel@tonic-gate /* 6267c478bd9Sstevel@tonic-gate * initialize hment data structures 6277c478bd9Sstevel@tonic-gate */ 6287c478bd9Sstevel@tonic-gate void 6297c478bd9Sstevel@tonic-gate hment_init(void) 6307c478bd9Sstevel@tonic-gate { 6317c478bd9Sstevel@tonic-gate int i; 6327c478bd9Sstevel@tonic-gate int flags = KMC_NOHASH | KMC_NODEBUG; 6337c478bd9Sstevel@tonic-gate 6347c478bd9Sstevel@tonic-gate /* 6357c478bd9Sstevel@tonic-gate * Initialize kmem caches. On 32 bit kernel's we shut off 6367c478bd9Sstevel@tonic-gate * debug information to save on precious kernel VA usage. 6377c478bd9Sstevel@tonic-gate */ 6387c478bd9Sstevel@tonic-gate hment_cache = kmem_cache_create("hment_t", 6397c478bd9Sstevel@tonic-gate sizeof (hment_t), 0, NULL, NULL, NULL, 6407c478bd9Sstevel@tonic-gate NULL, hat_memload_arena, flags); 6417c478bd9Sstevel@tonic-gate 642*b0aab85cSjosephb hment_table = kmem_zalloc(hment_hash_entries * sizeof (*hment_table), 6437c478bd9Sstevel@tonic-gate KM_SLEEP); 6447c478bd9Sstevel@tonic-gate 645*b0aab85cSjosephb mlist_lock = kmem_zalloc(MLIST_NUM_LOCK * sizeof (kmutex_t), KM_SLEEP); 646*b0aab85cSjosephb 647*b0aab85cSjosephb hash_lock = kmem_zalloc(HASH_NUM_LOCK * sizeof (kmutex_t), KM_SLEEP); 648*b0aab85cSjosephb 649*b0aab85cSjosephb for (i = 0; i < hment_hash_entries; ++i) 650*b0aab85cSjosephb avl_create(&hment_table[i], hment_compare, sizeof (hment_t), 651*b0aab85cSjosephb offsetof(hment_t, hm_hashlink)); 652*b0aab85cSjosephb 6537c478bd9Sstevel@tonic-gate for (i = 0; i < MLIST_NUM_LOCK; i++) 6547c478bd9Sstevel@tonic-gate mutex_init(&mlist_lock[i], NULL, MUTEX_DEFAULT, NULL); 6557c478bd9Sstevel@tonic-gate 6567c478bd9Sstevel@tonic-gate for (i = 0; i < HASH_NUM_LOCK; i++) 6577c478bd9Sstevel@tonic-gate mutex_init(&hash_lock[i], NULL, MUTEX_DEFAULT, NULL); 6587c478bd9Sstevel@tonic-gate 6597c478bd9Sstevel@tonic-gate 6607c478bd9Sstevel@tonic-gate } 6617c478bd9Sstevel@tonic-gate 6627c478bd9Sstevel@tonic-gate /* 6637c478bd9Sstevel@tonic-gate * return the number of mappings to a page 6647c478bd9Sstevel@tonic-gate * 6657c478bd9Sstevel@tonic-gate * Note there is no ASSERT() that the MUTEX is held for this. 6667c478bd9Sstevel@tonic-gate * Hence the return value might be inaccurate if this is called without 6677c478bd9Sstevel@tonic-gate * doing an x86_hm_enter(). 6687c478bd9Sstevel@tonic-gate */ 6697c478bd9Sstevel@tonic-gate uint_t 6707c478bd9Sstevel@tonic-gate hment_mapcnt(page_t *pp) 6717c478bd9Sstevel@tonic-gate { 6727c478bd9Sstevel@tonic-gate uint_t cnt; 6737c478bd9Sstevel@tonic-gate uint_t szc; 6747c478bd9Sstevel@tonic-gate page_t *larger; 6757c478bd9Sstevel@tonic-gate hment_t *hm; 6767c478bd9Sstevel@tonic-gate 6777c478bd9Sstevel@tonic-gate x86_hm_enter(pp); 6787c478bd9Sstevel@tonic-gate if (pp->p_mapping == NULL) 6797c478bd9Sstevel@tonic-gate cnt = 0; 6807c478bd9Sstevel@tonic-gate else if (pp->p_embed) 6817c478bd9Sstevel@tonic-gate cnt = 1; 6827c478bd9Sstevel@tonic-gate else 6837c478bd9Sstevel@tonic-gate cnt = pp->p_share; 6847c478bd9Sstevel@tonic-gate x86_hm_exit(pp); 6857c478bd9Sstevel@tonic-gate 6867c478bd9Sstevel@tonic-gate /* 6877c478bd9Sstevel@tonic-gate * walk through all larger mapping sizes counting mappings 6887c478bd9Sstevel@tonic-gate */ 6897c478bd9Sstevel@tonic-gate for (szc = 1; szc <= pp->p_szc; ++szc) { 6907c478bd9Sstevel@tonic-gate larger = PP_GROUPLEADER(pp, szc); 6917c478bd9Sstevel@tonic-gate if (larger == pp) /* don't double count large mappings */ 6927c478bd9Sstevel@tonic-gate continue; 6937c478bd9Sstevel@tonic-gate 6947c478bd9Sstevel@tonic-gate x86_hm_enter(larger); 6957c478bd9Sstevel@tonic-gate if (larger->p_mapping != NULL) { 6967c478bd9Sstevel@tonic-gate if (larger->p_embed && 6977c478bd9Sstevel@tonic-gate ((htable_t *)larger->p_mapping)->ht_level == szc) { 6987c478bd9Sstevel@tonic-gate ++cnt; 6997c478bd9Sstevel@tonic-gate } else if (!larger->p_embed) { 7007c478bd9Sstevel@tonic-gate for (hm = larger->p_mapping; hm; 7017c478bd9Sstevel@tonic-gate hm = hm->hm_next) { 7027c478bd9Sstevel@tonic-gate if (hm->hm_htable->ht_level == szc) 7037c478bd9Sstevel@tonic-gate ++cnt; 7047c478bd9Sstevel@tonic-gate } 7057c478bd9Sstevel@tonic-gate } 7067c478bd9Sstevel@tonic-gate } 7077c478bd9Sstevel@tonic-gate x86_hm_exit(larger); 7087c478bd9Sstevel@tonic-gate } 7097c478bd9Sstevel@tonic-gate return (cnt); 7107c478bd9Sstevel@tonic-gate } 7117c478bd9Sstevel@tonic-gate 7127c478bd9Sstevel@tonic-gate /* 7137c478bd9Sstevel@tonic-gate * We need to steal an hment. Walk through all the page_t's until we 7147c478bd9Sstevel@tonic-gate * find one that has multiple mappings. Unload one of the mappings 7157c478bd9Sstevel@tonic-gate * and reclaim that hment. Note that we'll save/restart the starting 7167c478bd9Sstevel@tonic-gate * page to try and spread the pain. 7177c478bd9Sstevel@tonic-gate */ 7187c478bd9Sstevel@tonic-gate static page_t *last_page = NULL; 7197c478bd9Sstevel@tonic-gate 7207c478bd9Sstevel@tonic-gate static hment_t * 7217c478bd9Sstevel@tonic-gate hment_steal(void) 7227c478bd9Sstevel@tonic-gate { 7237c478bd9Sstevel@tonic-gate page_t *last = last_page; 7247c478bd9Sstevel@tonic-gate page_t *pp = last; 7257c478bd9Sstevel@tonic-gate hment_t *hm = NULL; 7267c478bd9Sstevel@tonic-gate hment_t *hm2; 7277c478bd9Sstevel@tonic-gate htable_t *ht; 7287c478bd9Sstevel@tonic-gate uint_t found_one = 0; 7297c478bd9Sstevel@tonic-gate 7307c478bd9Sstevel@tonic-gate HATSTAT_INC(hs_hm_steals); 7317c478bd9Sstevel@tonic-gate if (pp == NULL) 7327c478bd9Sstevel@tonic-gate last = pp = page_first(); 7337c478bd9Sstevel@tonic-gate 7347c478bd9Sstevel@tonic-gate while (!found_one) { 7357c478bd9Sstevel@tonic-gate HATSTAT_INC(hs_hm_steal_exam); 7367c478bd9Sstevel@tonic-gate pp = page_next(pp); 7377c478bd9Sstevel@tonic-gate if (pp == NULL) 7387c478bd9Sstevel@tonic-gate pp = page_first(); 7397c478bd9Sstevel@tonic-gate 7407c478bd9Sstevel@tonic-gate /* 7417c478bd9Sstevel@tonic-gate * The loop and function exit here if nothing found to steal. 7427c478bd9Sstevel@tonic-gate */ 7437c478bd9Sstevel@tonic-gate if (pp == last) 7447c478bd9Sstevel@tonic-gate return (NULL); 7457c478bd9Sstevel@tonic-gate 7467c478bd9Sstevel@tonic-gate /* 7477c478bd9Sstevel@tonic-gate * Only lock the page_t if it has hments. 7487c478bd9Sstevel@tonic-gate */ 7497c478bd9Sstevel@tonic-gate if (pp->p_mapping == NULL || pp->p_embed) 7507c478bd9Sstevel@tonic-gate continue; 7517c478bd9Sstevel@tonic-gate 7527c478bd9Sstevel@tonic-gate /* 7537c478bd9Sstevel@tonic-gate * Search the mapping list for a usable mapping. 7547c478bd9Sstevel@tonic-gate */ 7557c478bd9Sstevel@tonic-gate x86_hm_enter(pp); 7567c478bd9Sstevel@tonic-gate if (!pp->p_embed) { 7577c478bd9Sstevel@tonic-gate for (hm = pp->p_mapping; hm; hm = hm->hm_next) { 7587c478bd9Sstevel@tonic-gate ht = hm->hm_htable; 7597c478bd9Sstevel@tonic-gate if (ht->ht_hat != kas.a_hat && 7607c478bd9Sstevel@tonic-gate ht->ht_busy == 0 && 7617c478bd9Sstevel@tonic-gate ht->ht_lock_cnt == 0) { 7627c478bd9Sstevel@tonic-gate found_one = 1; 7637c478bd9Sstevel@tonic-gate break; 7647c478bd9Sstevel@tonic-gate } 7657c478bd9Sstevel@tonic-gate } 7667c478bd9Sstevel@tonic-gate } 7677c478bd9Sstevel@tonic-gate if (!found_one) 7687c478bd9Sstevel@tonic-gate x86_hm_exit(pp); 7697c478bd9Sstevel@tonic-gate } 7707c478bd9Sstevel@tonic-gate 7717c478bd9Sstevel@tonic-gate /* 7727c478bd9Sstevel@tonic-gate * Steal the mapping we found. Note that hati_page_unmap() will 7737c478bd9Sstevel@tonic-gate * do the x86_hm_exit(). 7747c478bd9Sstevel@tonic-gate */ 7757c478bd9Sstevel@tonic-gate hm2 = hati_page_unmap(pp, ht, hm->hm_entry); 7767c478bd9Sstevel@tonic-gate ASSERT(hm2 == hm); 7777c478bd9Sstevel@tonic-gate last_page = pp; 7787c478bd9Sstevel@tonic-gate return (hm); 7797c478bd9Sstevel@tonic-gate } 780