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