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 540847348Stomee * Common Development and Distribution License (the "License"). 640847348Stomee * 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 */ 21e8031f0aSraf 227c478bd9Sstevel@tonic-gate /* 23*a574db85Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * For a more complete description of the main ideas, see: 317c478bd9Sstevel@tonic-gate * 327c478bd9Sstevel@tonic-gate * Jeff Bonwick and Jonathan Adams, 337c478bd9Sstevel@tonic-gate * 347c478bd9Sstevel@tonic-gate * Magazines and vmem: Extending the Slab Allocator to Many CPUs and 357c478bd9Sstevel@tonic-gate * Arbitrary Resources. 367c478bd9Sstevel@tonic-gate * 377c478bd9Sstevel@tonic-gate * Proceedings of the 2001 Usenix Conference. 387c478bd9Sstevel@tonic-gate * Available as /shared/sac/PSARC/2000/550/materials/vmem.pdf. 397c478bd9Sstevel@tonic-gate * 4040847348Stomee * For the "Big Theory Statement", see usr/src/uts/common/os/vmem.c 417c478bd9Sstevel@tonic-gate * 427c478bd9Sstevel@tonic-gate * 1. Overview of changes 437c478bd9Sstevel@tonic-gate * ------------------------------ 447c478bd9Sstevel@tonic-gate * There have been a few changes to vmem in order to support umem. The 457c478bd9Sstevel@tonic-gate * main areas are: 467c478bd9Sstevel@tonic-gate * 477c478bd9Sstevel@tonic-gate * * VM_SLEEP unsupported 487c478bd9Sstevel@tonic-gate * 497c478bd9Sstevel@tonic-gate * * Reaping changes 507c478bd9Sstevel@tonic-gate * 517c478bd9Sstevel@tonic-gate * * initialization changes 527c478bd9Sstevel@tonic-gate * 537c478bd9Sstevel@tonic-gate * * _vmem_extend_alloc 547c478bd9Sstevel@tonic-gate * 557c478bd9Sstevel@tonic-gate * 567c478bd9Sstevel@tonic-gate * 2. VM_SLEEP Removed 577c478bd9Sstevel@tonic-gate * ------------------- 587c478bd9Sstevel@tonic-gate * Since VM_SLEEP allocations can hold locks (in vmem_populate()) for 597c478bd9Sstevel@tonic-gate * possibly infinite amounts of time, they are not supported in this 607c478bd9Sstevel@tonic-gate * version of vmem. Sleep-like behavior can be achieved through 617c478bd9Sstevel@tonic-gate * UMEM_NOFAIL umem allocations. 627c478bd9Sstevel@tonic-gate * 637c478bd9Sstevel@tonic-gate * 647c478bd9Sstevel@tonic-gate * 3. Reaping changes 657c478bd9Sstevel@tonic-gate * ------------------ 667c478bd9Sstevel@tonic-gate * Unlike kmem_reap(), which just asynchronously schedules work, umem_reap() 677c478bd9Sstevel@tonic-gate * can do allocations and frees synchronously. This is a problem if it 687c478bd9Sstevel@tonic-gate * occurs during a vmem_populate() allocation. 697c478bd9Sstevel@tonic-gate * 707c478bd9Sstevel@tonic-gate * Instead, we delay reaps while populates are active. 717c478bd9Sstevel@tonic-gate * 727c478bd9Sstevel@tonic-gate * 737c478bd9Sstevel@tonic-gate * 4. Initialization changes 747c478bd9Sstevel@tonic-gate * ------------------------- 757c478bd9Sstevel@tonic-gate * In the kernel, vmem_init() allows you to create a single, top-level arena, 767c478bd9Sstevel@tonic-gate * which has vmem_internal_arena as a child. For umem, we want to be able 777c478bd9Sstevel@tonic-gate * to extend arenas dynamically. It is much easier to support this if we 787c478bd9Sstevel@tonic-gate * allow a two-level "heap" arena: 797c478bd9Sstevel@tonic-gate * 807c478bd9Sstevel@tonic-gate * +----------+ 817c478bd9Sstevel@tonic-gate * | "fake" | 827c478bd9Sstevel@tonic-gate * +----------+ 837c478bd9Sstevel@tonic-gate * | 847c478bd9Sstevel@tonic-gate * +----------+ 857c478bd9Sstevel@tonic-gate * | "heap" | 867c478bd9Sstevel@tonic-gate * +----------+ 877c478bd9Sstevel@tonic-gate * | \ \ 887c478bd9Sstevel@tonic-gate * | +-+-- ... <other children> 897c478bd9Sstevel@tonic-gate * | 907c478bd9Sstevel@tonic-gate * +---------------+ 917c478bd9Sstevel@tonic-gate * | vmem_internal | 927c478bd9Sstevel@tonic-gate * +---------------+ 937c478bd9Sstevel@tonic-gate * | | | | 947c478bd9Sstevel@tonic-gate * <children> 957c478bd9Sstevel@tonic-gate * 967c478bd9Sstevel@tonic-gate * The new vmem_init() allows you to specify a "parent" of the heap, along 977c478bd9Sstevel@tonic-gate * with allocation functions. 987c478bd9Sstevel@tonic-gate * 997c478bd9Sstevel@tonic-gate * 1007c478bd9Sstevel@tonic-gate * 5. _vmem_extend_alloc 1017c478bd9Sstevel@tonic-gate * --------------------- 1027c478bd9Sstevel@tonic-gate * The other part of extending is _vmem_extend_alloc. This function allows 1037c478bd9Sstevel@tonic-gate * you to extend (expand current spans, if possible) an arena and allocate 1047c478bd9Sstevel@tonic-gate * a chunk of the newly extened span atomically. This is needed to support 1057c478bd9Sstevel@tonic-gate * extending the heap while vmem_populate()ing it. 1067c478bd9Sstevel@tonic-gate * 1077c478bd9Sstevel@tonic-gate * In order to increase the usefulness of extending, non-imported spans are 1087c478bd9Sstevel@tonic-gate * sorted in address order. 1097c478bd9Sstevel@tonic-gate */ 1107c478bd9Sstevel@tonic-gate 111e8031f0aSraf #include "c_synonyms.h" 1127c478bd9Sstevel@tonic-gate #include <sys/vmem_impl_user.h> 1137c478bd9Sstevel@tonic-gate #include <alloca.h> 1147c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 1157c478bd9Sstevel@tonic-gate #include <stdio.h> 1167c478bd9Sstevel@tonic-gate #include <strings.h> 1177c478bd9Sstevel@tonic-gate #include <atomic.h> 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate #include "vmem_base.h" 1207c478bd9Sstevel@tonic-gate #include "umem_base.h" 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate #define VMEM_INITIAL 6 /* early vmem arenas */ 1237c478bd9Sstevel@tonic-gate #define VMEM_SEG_INITIAL 100 /* early segments */ 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate /* 1267c478bd9Sstevel@tonic-gate * Adding a new span to an arena requires two segment structures: one to 1277c478bd9Sstevel@tonic-gate * represent the span, and one to represent the free segment it contains. 1287c478bd9Sstevel@tonic-gate */ 1297c478bd9Sstevel@tonic-gate #define VMEM_SEGS_PER_SPAN_CREATE 2 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate /* 1327c478bd9Sstevel@tonic-gate * Allocating a piece of an existing segment requires 0-2 segment structures 1337c478bd9Sstevel@tonic-gate * depending on how much of the segment we're allocating. 1347c478bd9Sstevel@tonic-gate * 1357c478bd9Sstevel@tonic-gate * To allocate the entire segment, no new segment structures are needed; we 1367c478bd9Sstevel@tonic-gate * simply move the existing segment structure from the freelist to the 1377c478bd9Sstevel@tonic-gate * allocation hash table. 1387c478bd9Sstevel@tonic-gate * 1397c478bd9Sstevel@tonic-gate * To allocate a piece from the left or right end of the segment, we must 1407c478bd9Sstevel@tonic-gate * split the segment into two pieces (allocated part and remainder), so we 1417c478bd9Sstevel@tonic-gate * need one new segment structure to represent the remainder. 1427c478bd9Sstevel@tonic-gate * 1437c478bd9Sstevel@tonic-gate * To allocate from the middle of a segment, we need two new segment strucures 1447c478bd9Sstevel@tonic-gate * to represent the remainders on either side of the allocated part. 1457c478bd9Sstevel@tonic-gate */ 1467c478bd9Sstevel@tonic-gate #define VMEM_SEGS_PER_EXACT_ALLOC 0 1477c478bd9Sstevel@tonic-gate #define VMEM_SEGS_PER_LEFT_ALLOC 1 1487c478bd9Sstevel@tonic-gate #define VMEM_SEGS_PER_RIGHT_ALLOC 1 1497c478bd9Sstevel@tonic-gate #define VMEM_SEGS_PER_MIDDLE_ALLOC 2 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate /* 1527c478bd9Sstevel@tonic-gate * vmem_populate() preallocates segment structures for vmem to do its work. 1537c478bd9Sstevel@tonic-gate * It must preallocate enough for the worst case, which is when we must import 1547c478bd9Sstevel@tonic-gate * a new span and then allocate from the middle of it. 1557c478bd9Sstevel@tonic-gate */ 1567c478bd9Sstevel@tonic-gate #define VMEM_SEGS_PER_ALLOC_MAX \ 1577c478bd9Sstevel@tonic-gate (VMEM_SEGS_PER_SPAN_CREATE + VMEM_SEGS_PER_MIDDLE_ALLOC) 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate /* 1607c478bd9Sstevel@tonic-gate * The segment structures themselves are allocated from vmem_seg_arena, so 1617c478bd9Sstevel@tonic-gate * we have a recursion problem when vmem_seg_arena needs to populate itself. 1627c478bd9Sstevel@tonic-gate * We address this by working out the maximum number of segment structures 1637c478bd9Sstevel@tonic-gate * this act will require, and multiplying by the maximum number of threads 1647c478bd9Sstevel@tonic-gate * that we'll allow to do it simultaneously. 1657c478bd9Sstevel@tonic-gate * 1667c478bd9Sstevel@tonic-gate * The worst-case segment consumption to populate vmem_seg_arena is as 1677c478bd9Sstevel@tonic-gate * follows (depicted as a stack trace to indicate why events are occurring): 1687c478bd9Sstevel@tonic-gate * 1697c478bd9Sstevel@tonic-gate * vmem_alloc(vmem_seg_arena) -> 2 segs (span create + exact alloc) 1707c478bd9Sstevel@tonic-gate * vmem_alloc(vmem_internal_arena) -> 2 segs (span create + exact alloc) 1717c478bd9Sstevel@tonic-gate * heap_alloc(heap_arena) 1727c478bd9Sstevel@tonic-gate * vmem_alloc(heap_arena) -> 4 seg (span create + alloc) 1737c478bd9Sstevel@tonic-gate * parent_alloc(parent_arena) 1747c478bd9Sstevel@tonic-gate * _vmem_extend_alloc(parent_arena) -> 3 seg (span create + left alloc) 1757c478bd9Sstevel@tonic-gate * 1767c478bd9Sstevel@tonic-gate * Note: The reservation for heap_arena must be 4, since vmem_xalloc() 1777c478bd9Sstevel@tonic-gate * is overly pessimistic on allocations where parent_arena has a stricter 1787c478bd9Sstevel@tonic-gate * alignment than heap_arena. 1797c478bd9Sstevel@tonic-gate * 1807c478bd9Sstevel@tonic-gate * The worst-case consumption for any arena is 4 segment structures. 1817c478bd9Sstevel@tonic-gate * For now, we only support VM_NOSLEEP allocations, so as long as we 1827c478bd9Sstevel@tonic-gate * serialize all vmem_populates, a 4-seg reserve is sufficient. 1837c478bd9Sstevel@tonic-gate */ 1847c478bd9Sstevel@tonic-gate #define VMEM_POPULATE_SEGS_PER_ARENA 4 1857c478bd9Sstevel@tonic-gate #define VMEM_POPULATE_LOCKS 1 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate #define VMEM_POPULATE_RESERVE \ 1887c478bd9Sstevel@tonic-gate (VMEM_POPULATE_SEGS_PER_ARENA * VMEM_POPULATE_LOCKS) 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate /* 1917c478bd9Sstevel@tonic-gate * vmem_populate() ensures that each arena has VMEM_MINFREE seg structures 1927c478bd9Sstevel@tonic-gate * so that it can satisfy the worst-case allocation *and* participate in 1937c478bd9Sstevel@tonic-gate * worst-case allocation from vmem_seg_arena. 1947c478bd9Sstevel@tonic-gate */ 1957c478bd9Sstevel@tonic-gate #define VMEM_MINFREE (VMEM_POPULATE_RESERVE + VMEM_SEGS_PER_ALLOC_MAX) 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate /* Don't assume new statics are zeroed - see vmem_startup() */ 1987c478bd9Sstevel@tonic-gate static vmem_t vmem0[VMEM_INITIAL]; 1997c478bd9Sstevel@tonic-gate static vmem_t *vmem_populator[VMEM_INITIAL]; 2007c478bd9Sstevel@tonic-gate static uint32_t vmem_id; 2017c478bd9Sstevel@tonic-gate static uint32_t vmem_populators; 2027c478bd9Sstevel@tonic-gate static vmem_seg_t vmem_seg0[VMEM_SEG_INITIAL]; 2037c478bd9Sstevel@tonic-gate static vmem_seg_t *vmem_segfree; 2047c478bd9Sstevel@tonic-gate static mutex_t vmem_list_lock; 2057c478bd9Sstevel@tonic-gate static mutex_t vmem_segfree_lock; 2067c478bd9Sstevel@tonic-gate static vmem_populate_lock_t vmem_nosleep_lock; 2077c478bd9Sstevel@tonic-gate #define IN_POPULATE() (vmem_nosleep_lock.vmpl_thr == thr_self()) 2087c478bd9Sstevel@tonic-gate static vmem_t *vmem_list; 2097c478bd9Sstevel@tonic-gate static vmem_t *vmem_internal_arena; 2107c478bd9Sstevel@tonic-gate static vmem_t *vmem_seg_arena; 2117c478bd9Sstevel@tonic-gate static vmem_t *vmem_hash_arena; 2127c478bd9Sstevel@tonic-gate static vmem_t *vmem_vmem_arena; 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate vmem_t *vmem_heap; 2157c478bd9Sstevel@tonic-gate vmem_alloc_t *vmem_heap_alloc; 2167c478bd9Sstevel@tonic-gate vmem_free_t *vmem_heap_free; 2177c478bd9Sstevel@tonic-gate 2187c478bd9Sstevel@tonic-gate uint32_t vmem_mtbf; /* mean time between failures [default: off] */ 2197c478bd9Sstevel@tonic-gate size_t vmem_seg_size = sizeof (vmem_seg_t); 2207c478bd9Sstevel@tonic-gate 2217c478bd9Sstevel@tonic-gate /* 2227c478bd9Sstevel@tonic-gate * Insert/delete from arena list (type 'a') or next-of-kin list (type 'k'). 2237c478bd9Sstevel@tonic-gate */ 2247c478bd9Sstevel@tonic-gate #define VMEM_INSERT(vprev, vsp, type) \ 2257c478bd9Sstevel@tonic-gate { \ 2267c478bd9Sstevel@tonic-gate vmem_seg_t *vnext = (vprev)->vs_##type##next; \ 2277c478bd9Sstevel@tonic-gate (vsp)->vs_##type##next = (vnext); \ 2287c478bd9Sstevel@tonic-gate (vsp)->vs_##type##prev = (vprev); \ 2297c478bd9Sstevel@tonic-gate (vprev)->vs_##type##next = (vsp); \ 2307c478bd9Sstevel@tonic-gate (vnext)->vs_##type##prev = (vsp); \ 2317c478bd9Sstevel@tonic-gate } 2327c478bd9Sstevel@tonic-gate 2337c478bd9Sstevel@tonic-gate #define VMEM_DELETE(vsp, type) \ 2347c478bd9Sstevel@tonic-gate { \ 2357c478bd9Sstevel@tonic-gate vmem_seg_t *vprev = (vsp)->vs_##type##prev; \ 2367c478bd9Sstevel@tonic-gate vmem_seg_t *vnext = (vsp)->vs_##type##next; \ 2377c478bd9Sstevel@tonic-gate (vprev)->vs_##type##next = (vnext); \ 2387c478bd9Sstevel@tonic-gate (vnext)->vs_##type##prev = (vprev); \ 2397c478bd9Sstevel@tonic-gate } 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate /* 2427c478bd9Sstevel@tonic-gate * Get a vmem_seg_t from the global segfree list. 2437c478bd9Sstevel@tonic-gate */ 2447c478bd9Sstevel@tonic-gate static vmem_seg_t * 2457c478bd9Sstevel@tonic-gate vmem_getseg_global(void) 2467c478bd9Sstevel@tonic-gate { 2477c478bd9Sstevel@tonic-gate vmem_seg_t *vsp; 2487c478bd9Sstevel@tonic-gate 2497c478bd9Sstevel@tonic-gate (void) mutex_lock(&vmem_segfree_lock); 2507c478bd9Sstevel@tonic-gate if ((vsp = vmem_segfree) != NULL) 2517c478bd9Sstevel@tonic-gate vmem_segfree = vsp->vs_knext; 2527c478bd9Sstevel@tonic-gate (void) mutex_unlock(&vmem_segfree_lock); 2537c478bd9Sstevel@tonic-gate 2547c478bd9Sstevel@tonic-gate return (vsp); 2557c478bd9Sstevel@tonic-gate } 2567c478bd9Sstevel@tonic-gate 2577c478bd9Sstevel@tonic-gate /* 2587c478bd9Sstevel@tonic-gate * Put a vmem_seg_t on the global segfree list. 2597c478bd9Sstevel@tonic-gate */ 2607c478bd9Sstevel@tonic-gate static void 2617c478bd9Sstevel@tonic-gate vmem_putseg_global(vmem_seg_t *vsp) 2627c478bd9Sstevel@tonic-gate { 2637c478bd9Sstevel@tonic-gate (void) mutex_lock(&vmem_segfree_lock); 2647c478bd9Sstevel@tonic-gate vsp->vs_knext = vmem_segfree; 2657c478bd9Sstevel@tonic-gate vmem_segfree = vsp; 2667c478bd9Sstevel@tonic-gate (void) mutex_unlock(&vmem_segfree_lock); 2677c478bd9Sstevel@tonic-gate } 2687c478bd9Sstevel@tonic-gate 2697c478bd9Sstevel@tonic-gate /* 2707c478bd9Sstevel@tonic-gate * Get a vmem_seg_t from vmp's segfree list. 2717c478bd9Sstevel@tonic-gate */ 2727c478bd9Sstevel@tonic-gate static vmem_seg_t * 2737c478bd9Sstevel@tonic-gate vmem_getseg(vmem_t *vmp) 2747c478bd9Sstevel@tonic-gate { 2757c478bd9Sstevel@tonic-gate vmem_seg_t *vsp; 2767c478bd9Sstevel@tonic-gate 2777c478bd9Sstevel@tonic-gate ASSERT(vmp->vm_nsegfree > 0); 2787c478bd9Sstevel@tonic-gate 2797c478bd9Sstevel@tonic-gate vsp = vmp->vm_segfree; 2807c478bd9Sstevel@tonic-gate vmp->vm_segfree = vsp->vs_knext; 2817c478bd9Sstevel@tonic-gate vmp->vm_nsegfree--; 2827c478bd9Sstevel@tonic-gate 2837c478bd9Sstevel@tonic-gate return (vsp); 2847c478bd9Sstevel@tonic-gate } 2857c478bd9Sstevel@tonic-gate 2867c478bd9Sstevel@tonic-gate /* 2877c478bd9Sstevel@tonic-gate * Put a vmem_seg_t on vmp's segfree list. 2887c478bd9Sstevel@tonic-gate */ 2897c478bd9Sstevel@tonic-gate static void 2907c478bd9Sstevel@tonic-gate vmem_putseg(vmem_t *vmp, vmem_seg_t *vsp) 2917c478bd9Sstevel@tonic-gate { 2927c478bd9Sstevel@tonic-gate vsp->vs_knext = vmp->vm_segfree; 2937c478bd9Sstevel@tonic-gate vmp->vm_segfree = vsp; 2947c478bd9Sstevel@tonic-gate vmp->vm_nsegfree++; 2957c478bd9Sstevel@tonic-gate } 2967c478bd9Sstevel@tonic-gate 2977c478bd9Sstevel@tonic-gate /* 2987c478bd9Sstevel@tonic-gate * Add vsp to the appropriate freelist. 2997c478bd9Sstevel@tonic-gate */ 3007c478bd9Sstevel@tonic-gate static void 3017c478bd9Sstevel@tonic-gate vmem_freelist_insert(vmem_t *vmp, vmem_seg_t *vsp) 3027c478bd9Sstevel@tonic-gate { 3037c478bd9Sstevel@tonic-gate vmem_seg_t *vprev; 3047c478bd9Sstevel@tonic-gate 3057c478bd9Sstevel@tonic-gate ASSERT(*VMEM_HASH(vmp, vsp->vs_start) != vsp); 3067c478bd9Sstevel@tonic-gate 3077c478bd9Sstevel@tonic-gate vprev = (vmem_seg_t *)&vmp->vm_freelist[highbit(VS_SIZE(vsp)) - 1]; 3087c478bd9Sstevel@tonic-gate vsp->vs_type = VMEM_FREE; 3097c478bd9Sstevel@tonic-gate vmp->vm_freemap |= VS_SIZE(vprev); 3107c478bd9Sstevel@tonic-gate VMEM_INSERT(vprev, vsp, k); 3117c478bd9Sstevel@tonic-gate 3127c478bd9Sstevel@tonic-gate (void) cond_broadcast(&vmp->vm_cv); 3137c478bd9Sstevel@tonic-gate } 3147c478bd9Sstevel@tonic-gate 3157c478bd9Sstevel@tonic-gate /* 3167c478bd9Sstevel@tonic-gate * Take vsp from the freelist. 3177c478bd9Sstevel@tonic-gate */ 3187c478bd9Sstevel@tonic-gate static void 3197c478bd9Sstevel@tonic-gate vmem_freelist_delete(vmem_t *vmp, vmem_seg_t *vsp) 3207c478bd9Sstevel@tonic-gate { 3217c478bd9Sstevel@tonic-gate ASSERT(*VMEM_HASH(vmp, vsp->vs_start) != vsp); 3227c478bd9Sstevel@tonic-gate ASSERT(vsp->vs_type == VMEM_FREE); 3237c478bd9Sstevel@tonic-gate 3247c478bd9Sstevel@tonic-gate if (vsp->vs_knext->vs_start == 0 && vsp->vs_kprev->vs_start == 0) { 3257c478bd9Sstevel@tonic-gate /* 3267c478bd9Sstevel@tonic-gate * The segments on both sides of 'vsp' are freelist heads, 3277c478bd9Sstevel@tonic-gate * so taking vsp leaves the freelist at vsp->vs_kprev empty. 3287c478bd9Sstevel@tonic-gate */ 3297c478bd9Sstevel@tonic-gate ASSERT(vmp->vm_freemap & VS_SIZE(vsp->vs_kprev)); 3307c478bd9Sstevel@tonic-gate vmp->vm_freemap ^= VS_SIZE(vsp->vs_kprev); 3317c478bd9Sstevel@tonic-gate } 3327c478bd9Sstevel@tonic-gate VMEM_DELETE(vsp, k); 3337c478bd9Sstevel@tonic-gate } 3347c478bd9Sstevel@tonic-gate 3357c478bd9Sstevel@tonic-gate /* 3367c478bd9Sstevel@tonic-gate * Add vsp to the allocated-segment hash table and update kstats. 3377c478bd9Sstevel@tonic-gate */ 3387c478bd9Sstevel@tonic-gate static void 3397c478bd9Sstevel@tonic-gate vmem_hash_insert(vmem_t *vmp, vmem_seg_t *vsp) 3407c478bd9Sstevel@tonic-gate { 3417c478bd9Sstevel@tonic-gate vmem_seg_t **bucket; 3427c478bd9Sstevel@tonic-gate 3437c478bd9Sstevel@tonic-gate vsp->vs_type = VMEM_ALLOC; 3447c478bd9Sstevel@tonic-gate bucket = VMEM_HASH(vmp, vsp->vs_start); 3457c478bd9Sstevel@tonic-gate vsp->vs_knext = *bucket; 3467c478bd9Sstevel@tonic-gate *bucket = vsp; 3477c478bd9Sstevel@tonic-gate 3487c478bd9Sstevel@tonic-gate if (vmem_seg_size == sizeof (vmem_seg_t)) { 3497c478bd9Sstevel@tonic-gate vsp->vs_depth = (uint8_t)getpcstack(vsp->vs_stack, 3507c478bd9Sstevel@tonic-gate VMEM_STACK_DEPTH, 0); 3517c478bd9Sstevel@tonic-gate vsp->vs_thread = thr_self(); 3527c478bd9Sstevel@tonic-gate vsp->vs_timestamp = gethrtime(); 3537c478bd9Sstevel@tonic-gate } else { 3547c478bd9Sstevel@tonic-gate vsp->vs_depth = 0; 3557c478bd9Sstevel@tonic-gate } 3567c478bd9Sstevel@tonic-gate 3577c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_alloc++; 3587c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_mem_inuse += VS_SIZE(vsp); 3597c478bd9Sstevel@tonic-gate } 3607c478bd9Sstevel@tonic-gate 3617c478bd9Sstevel@tonic-gate /* 3627c478bd9Sstevel@tonic-gate * Remove vsp from the allocated-segment hash table and update kstats. 3637c478bd9Sstevel@tonic-gate */ 3647c478bd9Sstevel@tonic-gate static vmem_seg_t * 3657c478bd9Sstevel@tonic-gate vmem_hash_delete(vmem_t *vmp, uintptr_t addr, size_t size) 3667c478bd9Sstevel@tonic-gate { 3677c478bd9Sstevel@tonic-gate vmem_seg_t *vsp, **prev_vspp; 3687c478bd9Sstevel@tonic-gate 3697c478bd9Sstevel@tonic-gate prev_vspp = VMEM_HASH(vmp, addr); 3707c478bd9Sstevel@tonic-gate while ((vsp = *prev_vspp) != NULL) { 3717c478bd9Sstevel@tonic-gate if (vsp->vs_start == addr) { 3727c478bd9Sstevel@tonic-gate *prev_vspp = vsp->vs_knext; 3737c478bd9Sstevel@tonic-gate break; 3747c478bd9Sstevel@tonic-gate } 3757c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_lookup++; 3767c478bd9Sstevel@tonic-gate prev_vspp = &vsp->vs_knext; 3777c478bd9Sstevel@tonic-gate } 3787c478bd9Sstevel@tonic-gate 3797c478bd9Sstevel@tonic-gate if (vsp == NULL) { 3807c478bd9Sstevel@tonic-gate umem_panic("vmem_hash_delete(%p, %lx, %lu): bad free", 3817c478bd9Sstevel@tonic-gate vmp, addr, size); 3827c478bd9Sstevel@tonic-gate } 3837c478bd9Sstevel@tonic-gate if (VS_SIZE(vsp) != size) { 3847c478bd9Sstevel@tonic-gate umem_panic("vmem_hash_delete(%p, %lx, %lu): wrong size " 3857c478bd9Sstevel@tonic-gate "(expect %lu)", vmp, addr, size, VS_SIZE(vsp)); 3867c478bd9Sstevel@tonic-gate } 3877c478bd9Sstevel@tonic-gate 3887c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_free++; 3897c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_mem_inuse -= size; 3907c478bd9Sstevel@tonic-gate 3917c478bd9Sstevel@tonic-gate return (vsp); 3927c478bd9Sstevel@tonic-gate } 3937c478bd9Sstevel@tonic-gate 3947c478bd9Sstevel@tonic-gate /* 3957c478bd9Sstevel@tonic-gate * Create a segment spanning the range [start, end) and add it to the arena. 3967c478bd9Sstevel@tonic-gate */ 3977c478bd9Sstevel@tonic-gate static vmem_seg_t * 3987c478bd9Sstevel@tonic-gate vmem_seg_create(vmem_t *vmp, vmem_seg_t *vprev, uintptr_t start, uintptr_t end) 3997c478bd9Sstevel@tonic-gate { 4007c478bd9Sstevel@tonic-gate vmem_seg_t *newseg = vmem_getseg(vmp); 4017c478bd9Sstevel@tonic-gate 4027c478bd9Sstevel@tonic-gate newseg->vs_start = start; 4037c478bd9Sstevel@tonic-gate newseg->vs_end = end; 4047c478bd9Sstevel@tonic-gate newseg->vs_type = 0; 4057c478bd9Sstevel@tonic-gate newseg->vs_import = 0; 4067c478bd9Sstevel@tonic-gate 4077c478bd9Sstevel@tonic-gate VMEM_INSERT(vprev, newseg, a); 4087c478bd9Sstevel@tonic-gate 4097c478bd9Sstevel@tonic-gate return (newseg); 4107c478bd9Sstevel@tonic-gate } 4117c478bd9Sstevel@tonic-gate 4127c478bd9Sstevel@tonic-gate /* 4137c478bd9Sstevel@tonic-gate * Remove segment vsp from the arena. 4147c478bd9Sstevel@tonic-gate */ 4157c478bd9Sstevel@tonic-gate static void 4167c478bd9Sstevel@tonic-gate vmem_seg_destroy(vmem_t *vmp, vmem_seg_t *vsp) 4177c478bd9Sstevel@tonic-gate { 4187c478bd9Sstevel@tonic-gate ASSERT(vsp->vs_type != VMEM_ROTOR); 4197c478bd9Sstevel@tonic-gate VMEM_DELETE(vsp, a); 4207c478bd9Sstevel@tonic-gate 4217c478bd9Sstevel@tonic-gate vmem_putseg(vmp, vsp); 4227c478bd9Sstevel@tonic-gate } 4237c478bd9Sstevel@tonic-gate 4247c478bd9Sstevel@tonic-gate /* 4257c478bd9Sstevel@tonic-gate * Add the span [vaddr, vaddr + size) to vmp and update kstats. 4267c478bd9Sstevel@tonic-gate */ 4277c478bd9Sstevel@tonic-gate static vmem_seg_t * 4287c478bd9Sstevel@tonic-gate vmem_span_create(vmem_t *vmp, void *vaddr, size_t size, uint8_t import) 4297c478bd9Sstevel@tonic-gate { 4307c478bd9Sstevel@tonic-gate vmem_seg_t *knext; 4317c478bd9Sstevel@tonic-gate vmem_seg_t *newseg, *span; 4327c478bd9Sstevel@tonic-gate uintptr_t start = (uintptr_t)vaddr; 4337c478bd9Sstevel@tonic-gate uintptr_t end = start + size; 4347c478bd9Sstevel@tonic-gate 4357c478bd9Sstevel@tonic-gate knext = &vmp->vm_seg0; 4367c478bd9Sstevel@tonic-gate if (!import && vmp->vm_source_alloc == NULL) { 4377c478bd9Sstevel@tonic-gate vmem_seg_t *kend, *kprev; 4387c478bd9Sstevel@tonic-gate /* 4397c478bd9Sstevel@tonic-gate * non-imported spans are sorted in address order. This 4407c478bd9Sstevel@tonic-gate * makes vmem_extend_unlocked() much more effective. 4417c478bd9Sstevel@tonic-gate * 4427c478bd9Sstevel@tonic-gate * We search in reverse order, since new spans are 4437c478bd9Sstevel@tonic-gate * generally at higher addresses. 4447c478bd9Sstevel@tonic-gate */ 4457c478bd9Sstevel@tonic-gate kend = &vmp->vm_seg0; 4467c478bd9Sstevel@tonic-gate for (kprev = kend->vs_kprev; kprev != kend; 4477c478bd9Sstevel@tonic-gate kprev = kprev->vs_kprev) { 4487c478bd9Sstevel@tonic-gate if (!kprev->vs_import && (kprev->vs_end - 1) < start) 4497c478bd9Sstevel@tonic-gate break; 4507c478bd9Sstevel@tonic-gate } 4517c478bd9Sstevel@tonic-gate knext = kprev->vs_knext; 4527c478bd9Sstevel@tonic-gate } 4537c478bd9Sstevel@tonic-gate 4547c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&vmp->vm_lock)); 4557c478bd9Sstevel@tonic-gate 4567c478bd9Sstevel@tonic-gate if ((start | end) & (vmp->vm_quantum - 1)) { 4577c478bd9Sstevel@tonic-gate umem_panic("vmem_span_create(%p, %p, %lu): misaligned", 4587c478bd9Sstevel@tonic-gate vmp, vaddr, size); 4597c478bd9Sstevel@tonic-gate } 4607c478bd9Sstevel@tonic-gate 4617c478bd9Sstevel@tonic-gate span = vmem_seg_create(vmp, knext->vs_aprev, start, end); 4627c478bd9Sstevel@tonic-gate span->vs_type = VMEM_SPAN; 4637c478bd9Sstevel@tonic-gate VMEM_INSERT(knext->vs_kprev, span, k); 4647c478bd9Sstevel@tonic-gate 4657c478bd9Sstevel@tonic-gate newseg = vmem_seg_create(vmp, span, start, end); 4667c478bd9Sstevel@tonic-gate vmem_freelist_insert(vmp, newseg); 4677c478bd9Sstevel@tonic-gate 4687c478bd9Sstevel@tonic-gate newseg->vs_import = import; 4697c478bd9Sstevel@tonic-gate if (import) 4707c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_mem_import += size; 4717c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_mem_total += size; 4727c478bd9Sstevel@tonic-gate 4737c478bd9Sstevel@tonic-gate return (newseg); 4747c478bd9Sstevel@tonic-gate } 4757c478bd9Sstevel@tonic-gate 4767c478bd9Sstevel@tonic-gate /* 4777c478bd9Sstevel@tonic-gate * Remove span vsp from vmp and update kstats. 4787c478bd9Sstevel@tonic-gate */ 4797c478bd9Sstevel@tonic-gate static void 4807c478bd9Sstevel@tonic-gate vmem_span_destroy(vmem_t *vmp, vmem_seg_t *vsp) 4817c478bd9Sstevel@tonic-gate { 4827c478bd9Sstevel@tonic-gate vmem_seg_t *span = vsp->vs_aprev; 4837c478bd9Sstevel@tonic-gate size_t size = VS_SIZE(vsp); 4847c478bd9Sstevel@tonic-gate 4857c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&vmp->vm_lock)); 4867c478bd9Sstevel@tonic-gate ASSERT(span->vs_type == VMEM_SPAN); 4877c478bd9Sstevel@tonic-gate 4887c478bd9Sstevel@tonic-gate if (vsp->vs_import) 4897c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_mem_import -= size; 4907c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_mem_total -= size; 4917c478bd9Sstevel@tonic-gate 4927c478bd9Sstevel@tonic-gate VMEM_DELETE(span, k); 4937c478bd9Sstevel@tonic-gate 4947c478bd9Sstevel@tonic-gate vmem_seg_destroy(vmp, vsp); 4957c478bd9Sstevel@tonic-gate vmem_seg_destroy(vmp, span); 4967c478bd9Sstevel@tonic-gate } 4977c478bd9Sstevel@tonic-gate 4987c478bd9Sstevel@tonic-gate /* 4997c478bd9Sstevel@tonic-gate * Allocate the subrange [addr, addr + size) from segment vsp. 5007c478bd9Sstevel@tonic-gate * If there are leftovers on either side, place them on the freelist. 5017c478bd9Sstevel@tonic-gate * Returns a pointer to the segment representing [addr, addr + size). 5027c478bd9Sstevel@tonic-gate */ 5037c478bd9Sstevel@tonic-gate static vmem_seg_t * 5047c478bd9Sstevel@tonic-gate vmem_seg_alloc(vmem_t *vmp, vmem_seg_t *vsp, uintptr_t addr, size_t size) 5057c478bd9Sstevel@tonic-gate { 5067c478bd9Sstevel@tonic-gate uintptr_t vs_start = vsp->vs_start; 5077c478bd9Sstevel@tonic-gate uintptr_t vs_end = vsp->vs_end; 5087c478bd9Sstevel@tonic-gate size_t vs_size = vs_end - vs_start; 5097c478bd9Sstevel@tonic-gate size_t realsize = P2ROUNDUP(size, vmp->vm_quantum); 5107c478bd9Sstevel@tonic-gate uintptr_t addr_end = addr + realsize; 5117c478bd9Sstevel@tonic-gate 5127c478bd9Sstevel@tonic-gate ASSERT(P2PHASE(vs_start, vmp->vm_quantum) == 0); 5137c478bd9Sstevel@tonic-gate ASSERT(P2PHASE(addr, vmp->vm_quantum) == 0); 5147c478bd9Sstevel@tonic-gate ASSERT(vsp->vs_type == VMEM_FREE); 5157c478bd9Sstevel@tonic-gate ASSERT(addr >= vs_start && addr_end - 1 <= vs_end - 1); 5167c478bd9Sstevel@tonic-gate ASSERT(addr - 1 <= addr_end - 1); 5177c478bd9Sstevel@tonic-gate 5187c478bd9Sstevel@tonic-gate /* 5197c478bd9Sstevel@tonic-gate * If we're allocating from the start of the segment, and the 5207c478bd9Sstevel@tonic-gate * remainder will be on the same freelist, we can save quite 5217c478bd9Sstevel@tonic-gate * a bit of work. 5227c478bd9Sstevel@tonic-gate */ 5237c478bd9Sstevel@tonic-gate if (P2SAMEHIGHBIT(vs_size, vs_size - realsize) && addr == vs_start) { 5247c478bd9Sstevel@tonic-gate ASSERT(highbit(vs_size) == highbit(vs_size - realsize)); 5257c478bd9Sstevel@tonic-gate vsp->vs_start = addr_end; 5267c478bd9Sstevel@tonic-gate vsp = vmem_seg_create(vmp, vsp->vs_aprev, addr, addr + size); 5277c478bd9Sstevel@tonic-gate vmem_hash_insert(vmp, vsp); 5287c478bd9Sstevel@tonic-gate return (vsp); 5297c478bd9Sstevel@tonic-gate } 5307c478bd9Sstevel@tonic-gate 5317c478bd9Sstevel@tonic-gate vmem_freelist_delete(vmp, vsp); 5327c478bd9Sstevel@tonic-gate 5337c478bd9Sstevel@tonic-gate if (vs_end != addr_end) 5347c478bd9Sstevel@tonic-gate vmem_freelist_insert(vmp, 5357c478bd9Sstevel@tonic-gate vmem_seg_create(vmp, vsp, addr_end, vs_end)); 5367c478bd9Sstevel@tonic-gate 5377c478bd9Sstevel@tonic-gate if (vs_start != addr) 5387c478bd9Sstevel@tonic-gate vmem_freelist_insert(vmp, 5397c478bd9Sstevel@tonic-gate vmem_seg_create(vmp, vsp->vs_aprev, vs_start, addr)); 5407c478bd9Sstevel@tonic-gate 5417c478bd9Sstevel@tonic-gate vsp->vs_start = addr; 5427c478bd9Sstevel@tonic-gate vsp->vs_end = addr + size; 5437c478bd9Sstevel@tonic-gate 5447c478bd9Sstevel@tonic-gate vmem_hash_insert(vmp, vsp); 5457c478bd9Sstevel@tonic-gate return (vsp); 5467c478bd9Sstevel@tonic-gate } 5477c478bd9Sstevel@tonic-gate 5487c478bd9Sstevel@tonic-gate /* 5497c478bd9Sstevel@tonic-gate * We cannot reap if we are in the middle of a vmem_populate(). 5507c478bd9Sstevel@tonic-gate */ 5517c478bd9Sstevel@tonic-gate void 5527c478bd9Sstevel@tonic-gate vmem_reap(void) 5537c478bd9Sstevel@tonic-gate { 5547c478bd9Sstevel@tonic-gate if (!IN_POPULATE()) 5557c478bd9Sstevel@tonic-gate umem_reap(); 5567c478bd9Sstevel@tonic-gate } 5577c478bd9Sstevel@tonic-gate 5587c478bd9Sstevel@tonic-gate /* 5597c478bd9Sstevel@tonic-gate * Populate vmp's segfree list with VMEM_MINFREE vmem_seg_t structures. 5607c478bd9Sstevel@tonic-gate */ 5617c478bd9Sstevel@tonic-gate static int 5627c478bd9Sstevel@tonic-gate vmem_populate(vmem_t *vmp, int vmflag) 5637c478bd9Sstevel@tonic-gate { 5647c478bd9Sstevel@tonic-gate char *p; 5657c478bd9Sstevel@tonic-gate vmem_seg_t *vsp; 5667c478bd9Sstevel@tonic-gate ssize_t nseg; 5677c478bd9Sstevel@tonic-gate size_t size; 5687c478bd9Sstevel@tonic-gate vmem_populate_lock_t *lp; 5697c478bd9Sstevel@tonic-gate int i; 5707c478bd9Sstevel@tonic-gate 5717c478bd9Sstevel@tonic-gate while (vmp->vm_nsegfree < VMEM_MINFREE && 5727c478bd9Sstevel@tonic-gate (vsp = vmem_getseg_global()) != NULL) 5737c478bd9Sstevel@tonic-gate vmem_putseg(vmp, vsp); 5747c478bd9Sstevel@tonic-gate 5757c478bd9Sstevel@tonic-gate if (vmp->vm_nsegfree >= VMEM_MINFREE) 5767c478bd9Sstevel@tonic-gate return (1); 5777c478bd9Sstevel@tonic-gate 5787c478bd9Sstevel@tonic-gate /* 5797c478bd9Sstevel@tonic-gate * If we're already populating, tap the reserve. 5807c478bd9Sstevel@tonic-gate */ 5817c478bd9Sstevel@tonic-gate if (vmem_nosleep_lock.vmpl_thr == thr_self()) { 5827c478bd9Sstevel@tonic-gate ASSERT(vmp->vm_cflags & VMC_POPULATOR); 5837c478bd9Sstevel@tonic-gate return (1); 5847c478bd9Sstevel@tonic-gate } 5857c478bd9Sstevel@tonic-gate 5867c478bd9Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock); 5877c478bd9Sstevel@tonic-gate 5887c478bd9Sstevel@tonic-gate ASSERT(vmflag & VM_NOSLEEP); /* we do not allow sleep allocations */ 5897c478bd9Sstevel@tonic-gate lp = &vmem_nosleep_lock; 5907c478bd9Sstevel@tonic-gate 5917c478bd9Sstevel@tonic-gate /* 5927c478bd9Sstevel@tonic-gate * Cannot be just a mutex_lock(), since that has no effect if 5937c478bd9Sstevel@tonic-gate * libthread is not linked. 5947c478bd9Sstevel@tonic-gate */ 5957c478bd9Sstevel@tonic-gate (void) mutex_lock(&lp->vmpl_mutex); 5967c478bd9Sstevel@tonic-gate ASSERT(lp->vmpl_thr == 0); 5977c478bd9Sstevel@tonic-gate lp->vmpl_thr = thr_self(); 5987c478bd9Sstevel@tonic-gate 5997c478bd9Sstevel@tonic-gate nseg = VMEM_MINFREE + vmem_populators * VMEM_POPULATE_RESERVE; 6007c478bd9Sstevel@tonic-gate size = P2ROUNDUP(nseg * vmem_seg_size, vmem_seg_arena->vm_quantum); 6017c478bd9Sstevel@tonic-gate nseg = size / vmem_seg_size; 6027c478bd9Sstevel@tonic-gate 6037c478bd9Sstevel@tonic-gate /* 6047c478bd9Sstevel@tonic-gate * The following vmem_alloc() may need to populate vmem_seg_arena 6057c478bd9Sstevel@tonic-gate * and all the things it imports from. When doing so, it will tap 6067c478bd9Sstevel@tonic-gate * each arena's reserve to prevent recursion (see the block comment 6077c478bd9Sstevel@tonic-gate * above the definition of VMEM_POPULATE_RESERVE). 6087c478bd9Sstevel@tonic-gate * 6097c478bd9Sstevel@tonic-gate * During this allocation, vmem_reap() is a no-op. If the allocation 6107c478bd9Sstevel@tonic-gate * fails, we call vmem_reap() after dropping the population lock. 6117c478bd9Sstevel@tonic-gate */ 6127c478bd9Sstevel@tonic-gate p = vmem_alloc(vmem_seg_arena, size, vmflag & VM_UMFLAGS); 6137c478bd9Sstevel@tonic-gate if (p == NULL) { 6147c478bd9Sstevel@tonic-gate lp->vmpl_thr = 0; 6157c478bd9Sstevel@tonic-gate (void) mutex_unlock(&lp->vmpl_mutex); 6167c478bd9Sstevel@tonic-gate vmem_reap(); 6177c478bd9Sstevel@tonic-gate 6187c478bd9Sstevel@tonic-gate (void) mutex_lock(&vmp->vm_lock); 6197c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_populate_fail++; 6207c478bd9Sstevel@tonic-gate return (0); 6217c478bd9Sstevel@tonic-gate } 6227c478bd9Sstevel@tonic-gate /* 6237c478bd9Sstevel@tonic-gate * Restock the arenas that may have been depleted during population. 6247c478bd9Sstevel@tonic-gate */ 6257c478bd9Sstevel@tonic-gate for (i = 0; i < vmem_populators; i++) { 6267c478bd9Sstevel@tonic-gate (void) mutex_lock(&vmem_populator[i]->vm_lock); 6277c478bd9Sstevel@tonic-gate while (vmem_populator[i]->vm_nsegfree < VMEM_POPULATE_RESERVE) 6287c478bd9Sstevel@tonic-gate vmem_putseg(vmem_populator[i], 6297c478bd9Sstevel@tonic-gate (vmem_seg_t *)(p + --nseg * vmem_seg_size)); 6307c478bd9Sstevel@tonic-gate (void) mutex_unlock(&vmem_populator[i]->vm_lock); 6317c478bd9Sstevel@tonic-gate } 6327c478bd9Sstevel@tonic-gate 6337c478bd9Sstevel@tonic-gate lp->vmpl_thr = 0; 6347c478bd9Sstevel@tonic-gate (void) mutex_unlock(&lp->vmpl_mutex); 6357c478bd9Sstevel@tonic-gate (void) mutex_lock(&vmp->vm_lock); 6367c478bd9Sstevel@tonic-gate 6377c478bd9Sstevel@tonic-gate /* 6387c478bd9Sstevel@tonic-gate * Now take our own segments. 6397c478bd9Sstevel@tonic-gate */ 6407c478bd9Sstevel@tonic-gate ASSERT(nseg >= VMEM_MINFREE); 6417c478bd9Sstevel@tonic-gate while (vmp->vm_nsegfree < VMEM_MINFREE) 6427c478bd9Sstevel@tonic-gate vmem_putseg(vmp, (vmem_seg_t *)(p + --nseg * vmem_seg_size)); 6437c478bd9Sstevel@tonic-gate 6447c478bd9Sstevel@tonic-gate /* 6457c478bd9Sstevel@tonic-gate * Give the remainder to charity. 6467c478bd9Sstevel@tonic-gate */ 6477c478bd9Sstevel@tonic-gate while (nseg > 0) 6487c478bd9Sstevel@tonic-gate vmem_putseg_global((vmem_seg_t *)(p + --nseg * vmem_seg_size)); 6497c478bd9Sstevel@tonic-gate 6507c478bd9Sstevel@tonic-gate return (1); 6517c478bd9Sstevel@tonic-gate } 6527c478bd9Sstevel@tonic-gate 6537c478bd9Sstevel@tonic-gate /* 6547c478bd9Sstevel@tonic-gate * Advance a walker from its previous position to 'afterme'. 6557c478bd9Sstevel@tonic-gate * Note: may drop and reacquire vmp->vm_lock. 6567c478bd9Sstevel@tonic-gate */ 6577c478bd9Sstevel@tonic-gate static void 6587c478bd9Sstevel@tonic-gate vmem_advance(vmem_t *vmp, vmem_seg_t *walker, vmem_seg_t *afterme) 6597c478bd9Sstevel@tonic-gate { 6607c478bd9Sstevel@tonic-gate vmem_seg_t *vprev = walker->vs_aprev; 6617c478bd9Sstevel@tonic-gate vmem_seg_t *vnext = walker->vs_anext; 6627c478bd9Sstevel@tonic-gate vmem_seg_t *vsp = NULL; 6637c478bd9Sstevel@tonic-gate 6647c478bd9Sstevel@tonic-gate VMEM_DELETE(walker, a); 6657c478bd9Sstevel@tonic-gate 6667c478bd9Sstevel@tonic-gate if (afterme != NULL) 6677c478bd9Sstevel@tonic-gate VMEM_INSERT(afterme, walker, a); 6687c478bd9Sstevel@tonic-gate 6697c478bd9Sstevel@tonic-gate /* 6707c478bd9Sstevel@tonic-gate * The walker segment's presence may have prevented its neighbors 6717c478bd9Sstevel@tonic-gate * from coalescing. If so, coalesce them now. 6727c478bd9Sstevel@tonic-gate */ 6737c478bd9Sstevel@tonic-gate if (vprev->vs_type == VMEM_FREE) { 6747c478bd9Sstevel@tonic-gate if (vnext->vs_type == VMEM_FREE) { 6757c478bd9Sstevel@tonic-gate ASSERT(vprev->vs_end == vnext->vs_start); 6767c478bd9Sstevel@tonic-gate vmem_freelist_delete(vmp, vnext); 6777c478bd9Sstevel@tonic-gate vmem_freelist_delete(vmp, vprev); 6787c478bd9Sstevel@tonic-gate vprev->vs_end = vnext->vs_end; 6797c478bd9Sstevel@tonic-gate vmem_freelist_insert(vmp, vprev); 6807c478bd9Sstevel@tonic-gate vmem_seg_destroy(vmp, vnext); 6817c478bd9Sstevel@tonic-gate } 6827c478bd9Sstevel@tonic-gate vsp = vprev; 6837c478bd9Sstevel@tonic-gate } else if (vnext->vs_type == VMEM_FREE) { 6847c478bd9Sstevel@tonic-gate vsp = vnext; 6857c478bd9Sstevel@tonic-gate } 6867c478bd9Sstevel@tonic-gate 6877c478bd9Sstevel@tonic-gate /* 6887c478bd9Sstevel@tonic-gate * vsp could represent a complete imported span, 6897c478bd9Sstevel@tonic-gate * in which case we must return it to the source. 6907c478bd9Sstevel@tonic-gate */ 6917c478bd9Sstevel@tonic-gate if (vsp != NULL && vsp->vs_import && vmp->vm_source_free != NULL && 6927c478bd9Sstevel@tonic-gate vsp->vs_aprev->vs_type == VMEM_SPAN && 6937c478bd9Sstevel@tonic-gate vsp->vs_anext->vs_type == VMEM_SPAN) { 6947c478bd9Sstevel@tonic-gate void *vaddr = (void *)vsp->vs_start; 6957c478bd9Sstevel@tonic-gate size_t size = VS_SIZE(vsp); 6967c478bd9Sstevel@tonic-gate ASSERT(size == VS_SIZE(vsp->vs_aprev)); 6977c478bd9Sstevel@tonic-gate vmem_freelist_delete(vmp, vsp); 6987c478bd9Sstevel@tonic-gate vmem_span_destroy(vmp, vsp); 6997c478bd9Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock); 7007c478bd9Sstevel@tonic-gate vmp->vm_source_free(vmp->vm_source, vaddr, size); 7017c478bd9Sstevel@tonic-gate (void) mutex_lock(&vmp->vm_lock); 7027c478bd9Sstevel@tonic-gate } 7037c478bd9Sstevel@tonic-gate } 7047c478bd9Sstevel@tonic-gate 7057c478bd9Sstevel@tonic-gate /* 7067c478bd9Sstevel@tonic-gate * VM_NEXTFIT allocations deliberately cycle through all virtual addresses 7077c478bd9Sstevel@tonic-gate * in an arena, so that we avoid reusing addresses for as long as possible. 7087c478bd9Sstevel@tonic-gate * This helps to catch used-after-freed bugs. It's also the perfect policy 7097c478bd9Sstevel@tonic-gate * for allocating things like process IDs, where we want to cycle through 7107c478bd9Sstevel@tonic-gate * all values in order. 7117c478bd9Sstevel@tonic-gate */ 7127c478bd9Sstevel@tonic-gate static void * 7137c478bd9Sstevel@tonic-gate vmem_nextfit_alloc(vmem_t *vmp, size_t size, int vmflag) 7147c478bd9Sstevel@tonic-gate { 7157c478bd9Sstevel@tonic-gate vmem_seg_t *vsp, *rotor; 7167c478bd9Sstevel@tonic-gate uintptr_t addr; 7177c478bd9Sstevel@tonic-gate size_t realsize = P2ROUNDUP(size, vmp->vm_quantum); 7187c478bd9Sstevel@tonic-gate size_t vs_size; 7197c478bd9Sstevel@tonic-gate 7207c478bd9Sstevel@tonic-gate (void) mutex_lock(&vmp->vm_lock); 7217c478bd9Sstevel@tonic-gate 7227c478bd9Sstevel@tonic-gate if (vmp->vm_nsegfree < VMEM_MINFREE && !vmem_populate(vmp, vmflag)) { 7237c478bd9Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock); 7247c478bd9Sstevel@tonic-gate return (NULL); 7257c478bd9Sstevel@tonic-gate } 7267c478bd9Sstevel@tonic-gate 7277c478bd9Sstevel@tonic-gate /* 7287c478bd9Sstevel@tonic-gate * The common case is that the segment right after the rotor is free, 7297c478bd9Sstevel@tonic-gate * and large enough that extracting 'size' bytes won't change which 7307c478bd9Sstevel@tonic-gate * freelist it's on. In this case we can avoid a *lot* of work. 7317c478bd9Sstevel@tonic-gate * Instead of the normal vmem_seg_alloc(), we just advance the start 7327c478bd9Sstevel@tonic-gate * address of the victim segment. Instead of moving the rotor, we 7337c478bd9Sstevel@tonic-gate * create the new segment structure *behind the rotor*, which has 7347c478bd9Sstevel@tonic-gate * the same effect. And finally, we know we don't have to coalesce 7357c478bd9Sstevel@tonic-gate * the rotor's neighbors because the new segment lies between them. 7367c478bd9Sstevel@tonic-gate */ 7377c478bd9Sstevel@tonic-gate rotor = &vmp->vm_rotor; 7387c478bd9Sstevel@tonic-gate vsp = rotor->vs_anext; 7397c478bd9Sstevel@tonic-gate if (vsp->vs_type == VMEM_FREE && (vs_size = VS_SIZE(vsp)) > realsize && 7407c478bd9Sstevel@tonic-gate P2SAMEHIGHBIT(vs_size, vs_size - realsize)) { 7417c478bd9Sstevel@tonic-gate ASSERT(highbit(vs_size) == highbit(vs_size - realsize)); 7427c478bd9Sstevel@tonic-gate addr = vsp->vs_start; 7437c478bd9Sstevel@tonic-gate vsp->vs_start = addr + realsize; 7447c478bd9Sstevel@tonic-gate vmem_hash_insert(vmp, 7457c478bd9Sstevel@tonic-gate vmem_seg_create(vmp, rotor->vs_aprev, addr, addr + size)); 7467c478bd9Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock); 7477c478bd9Sstevel@tonic-gate return ((void *)addr); 7487c478bd9Sstevel@tonic-gate } 7497c478bd9Sstevel@tonic-gate 7507c478bd9Sstevel@tonic-gate /* 7517c478bd9Sstevel@tonic-gate * Starting at the rotor, look for a segment large enough to 7527c478bd9Sstevel@tonic-gate * satisfy the allocation. 7537c478bd9Sstevel@tonic-gate */ 7547c478bd9Sstevel@tonic-gate for (;;) { 7557c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_search++; 7567c478bd9Sstevel@tonic-gate if (vsp->vs_type == VMEM_FREE && VS_SIZE(vsp) >= size) 7577c478bd9Sstevel@tonic-gate break; 7587c478bd9Sstevel@tonic-gate vsp = vsp->vs_anext; 7597c478bd9Sstevel@tonic-gate if (vsp == rotor) { 760*a574db85Sraf int cancel_state; 761*a574db85Sraf 7627c478bd9Sstevel@tonic-gate /* 7637c478bd9Sstevel@tonic-gate * We've come full circle. One possibility is that the 7647c478bd9Sstevel@tonic-gate * there's actually enough space, but the rotor itself 7657c478bd9Sstevel@tonic-gate * is preventing the allocation from succeeding because 7667c478bd9Sstevel@tonic-gate * it's sitting between two free segments. Therefore, 7677c478bd9Sstevel@tonic-gate * we advance the rotor and see if that liberates a 7687c478bd9Sstevel@tonic-gate * suitable segment. 7697c478bd9Sstevel@tonic-gate */ 7707c478bd9Sstevel@tonic-gate vmem_advance(vmp, rotor, rotor->vs_anext); 7717c478bd9Sstevel@tonic-gate vsp = rotor->vs_aprev; 7727c478bd9Sstevel@tonic-gate if (vsp->vs_type == VMEM_FREE && VS_SIZE(vsp) >= size) 7737c478bd9Sstevel@tonic-gate break; 7747c478bd9Sstevel@tonic-gate /* 7757c478bd9Sstevel@tonic-gate * If there's a lower arena we can import from, or it's 7767c478bd9Sstevel@tonic-gate * a VM_NOSLEEP allocation, let vmem_xalloc() handle it. 7777c478bd9Sstevel@tonic-gate * Otherwise, wait until another thread frees something. 7787c478bd9Sstevel@tonic-gate */ 7797c478bd9Sstevel@tonic-gate if (vmp->vm_source_alloc != NULL || 7807c478bd9Sstevel@tonic-gate (vmflag & VM_NOSLEEP)) { 7817c478bd9Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock); 7827c478bd9Sstevel@tonic-gate return (vmem_xalloc(vmp, size, vmp->vm_quantum, 7837c478bd9Sstevel@tonic-gate 0, 0, NULL, NULL, vmflag & VM_UMFLAGS)); 7847c478bd9Sstevel@tonic-gate } 7857c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_wait++; 786*a574db85Sraf (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 787*a574db85Sraf &cancel_state); 788*a574db85Sraf (void) cond_wait(&vmp->vm_cv, &vmp->vm_lock); 789*a574db85Sraf (void) pthread_setcancelstate(cancel_state, NULL); 7907c478bd9Sstevel@tonic-gate vsp = rotor->vs_anext; 7917c478bd9Sstevel@tonic-gate } 7927c478bd9Sstevel@tonic-gate } 7937c478bd9Sstevel@tonic-gate 7947c478bd9Sstevel@tonic-gate /* 7957c478bd9Sstevel@tonic-gate * We found a segment. Extract enough space to satisfy the allocation. 7967c478bd9Sstevel@tonic-gate */ 7977c478bd9Sstevel@tonic-gate addr = vsp->vs_start; 7987c478bd9Sstevel@tonic-gate vsp = vmem_seg_alloc(vmp, vsp, addr, size); 7997c478bd9Sstevel@tonic-gate ASSERT(vsp->vs_type == VMEM_ALLOC && 8007c478bd9Sstevel@tonic-gate vsp->vs_start == addr && vsp->vs_end == addr + size); 8017c478bd9Sstevel@tonic-gate 8027c478bd9Sstevel@tonic-gate /* 8037c478bd9Sstevel@tonic-gate * Advance the rotor to right after the newly-allocated segment. 8047c478bd9Sstevel@tonic-gate * That's where the next VM_NEXTFIT allocation will begin searching. 8057c478bd9Sstevel@tonic-gate */ 8067c478bd9Sstevel@tonic-gate vmem_advance(vmp, rotor, vsp); 8077c478bd9Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock); 8087c478bd9Sstevel@tonic-gate return ((void *)addr); 8097c478bd9Sstevel@tonic-gate } 8107c478bd9Sstevel@tonic-gate 8117c478bd9Sstevel@tonic-gate /* 8127c478bd9Sstevel@tonic-gate * Allocate size bytes at offset phase from an align boundary such that the 8137c478bd9Sstevel@tonic-gate * resulting segment [addr, addr + size) is a subset of [minaddr, maxaddr) 8147c478bd9Sstevel@tonic-gate * that does not straddle a nocross-aligned boundary. 8157c478bd9Sstevel@tonic-gate */ 8167c478bd9Sstevel@tonic-gate void * 8177c478bd9Sstevel@tonic-gate vmem_xalloc(vmem_t *vmp, size_t size, size_t align, size_t phase, 8187c478bd9Sstevel@tonic-gate size_t nocross, void *minaddr, void *maxaddr, int vmflag) 8197c478bd9Sstevel@tonic-gate { 8207c478bd9Sstevel@tonic-gate vmem_seg_t *vsp; 8217c478bd9Sstevel@tonic-gate vmem_seg_t *vbest = NULL; 8227c478bd9Sstevel@tonic-gate uintptr_t addr, taddr, start, end; 8237c478bd9Sstevel@tonic-gate void *vaddr; 8247c478bd9Sstevel@tonic-gate int hb, flist, resv; 8257c478bd9Sstevel@tonic-gate uint32_t mtbf; 8267c478bd9Sstevel@tonic-gate 8277c478bd9Sstevel@tonic-gate if (phase > 0 && phase >= align) 8287c478bd9Sstevel@tonic-gate umem_panic("vmem_xalloc(%p, %lu, %lu, %lu, %lu, %p, %p, %x): " 8297c478bd9Sstevel@tonic-gate "invalid phase", 8307c478bd9Sstevel@tonic-gate (void *)vmp, size, align, phase, nocross, 8317c478bd9Sstevel@tonic-gate minaddr, maxaddr, vmflag); 8327c478bd9Sstevel@tonic-gate 8337c478bd9Sstevel@tonic-gate if (align == 0) 8347c478bd9Sstevel@tonic-gate align = vmp->vm_quantum; 8357c478bd9Sstevel@tonic-gate 8367c478bd9Sstevel@tonic-gate if ((align | phase | nocross) & (vmp->vm_quantum - 1)) { 8377c478bd9Sstevel@tonic-gate umem_panic("vmem_xalloc(%p, %lu, %lu, %lu, %lu, %p, %p, %x): " 8387c478bd9Sstevel@tonic-gate "parameters not vm_quantum aligned", 8397c478bd9Sstevel@tonic-gate (void *)vmp, size, align, phase, nocross, 8407c478bd9Sstevel@tonic-gate minaddr, maxaddr, vmflag); 8417c478bd9Sstevel@tonic-gate } 8427c478bd9Sstevel@tonic-gate 8437c478bd9Sstevel@tonic-gate if (nocross != 0 && 8447c478bd9Sstevel@tonic-gate (align > nocross || P2ROUNDUP(phase + size, align) > nocross)) { 8457c478bd9Sstevel@tonic-gate umem_panic("vmem_xalloc(%p, %lu, %lu, %lu, %lu, %p, %p, %x): " 8467c478bd9Sstevel@tonic-gate "overconstrained allocation", 8477c478bd9Sstevel@tonic-gate (void *)vmp, size, align, phase, nocross, 8487c478bd9Sstevel@tonic-gate minaddr, maxaddr, vmflag); 8497c478bd9Sstevel@tonic-gate } 8507c478bd9Sstevel@tonic-gate 8517c478bd9Sstevel@tonic-gate if ((mtbf = vmem_mtbf | vmp->vm_mtbf) != 0 && gethrtime() % mtbf == 0 && 8527c478bd9Sstevel@tonic-gate (vmflag & (VM_NOSLEEP | VM_PANIC)) == VM_NOSLEEP) 8537c478bd9Sstevel@tonic-gate return (NULL); 8547c478bd9Sstevel@tonic-gate 8557c478bd9Sstevel@tonic-gate (void) mutex_lock(&vmp->vm_lock); 8567c478bd9Sstevel@tonic-gate for (;;) { 857*a574db85Sraf int cancel_state; 858*a574db85Sraf 8597c478bd9Sstevel@tonic-gate if (vmp->vm_nsegfree < VMEM_MINFREE && 8607c478bd9Sstevel@tonic-gate !vmem_populate(vmp, vmflag)) 8617c478bd9Sstevel@tonic-gate break; 8627c478bd9Sstevel@tonic-gate 8637c478bd9Sstevel@tonic-gate /* 8647c478bd9Sstevel@tonic-gate * highbit() returns the highest bit + 1, which is exactly 8657c478bd9Sstevel@tonic-gate * what we want: we want to search the first freelist whose 8667c478bd9Sstevel@tonic-gate * members are *definitely* large enough to satisfy our 8677c478bd9Sstevel@tonic-gate * allocation. However, there are certain cases in which we 8687c478bd9Sstevel@tonic-gate * want to look at the next-smallest freelist (which *might* 8697c478bd9Sstevel@tonic-gate * be able to satisfy the allocation): 8707c478bd9Sstevel@tonic-gate * 8717c478bd9Sstevel@tonic-gate * (1) The size is exactly a power of 2, in which case 8727c478bd9Sstevel@tonic-gate * the smaller freelist is always big enough; 8737c478bd9Sstevel@tonic-gate * 8747c478bd9Sstevel@tonic-gate * (2) All other freelists are empty; 8757c478bd9Sstevel@tonic-gate * 8767c478bd9Sstevel@tonic-gate * (3) We're in the highest possible freelist, which is 8777c478bd9Sstevel@tonic-gate * always empty (e.g. the 4GB freelist on 32-bit systems); 8787c478bd9Sstevel@tonic-gate * 8797c478bd9Sstevel@tonic-gate * (4) We're doing a best-fit or first-fit allocation. 8807c478bd9Sstevel@tonic-gate */ 8817c478bd9Sstevel@tonic-gate if ((size & (size - 1)) == 0) { 8827c478bd9Sstevel@tonic-gate flist = lowbit(P2ALIGN(vmp->vm_freemap, size)); 8837c478bd9Sstevel@tonic-gate } else { 8847c478bd9Sstevel@tonic-gate hb = highbit(size); 8857c478bd9Sstevel@tonic-gate if ((vmp->vm_freemap >> hb) == 0 || 8867c478bd9Sstevel@tonic-gate hb == VMEM_FREELISTS || 8877c478bd9Sstevel@tonic-gate (vmflag & (VM_BESTFIT | VM_FIRSTFIT))) 8887c478bd9Sstevel@tonic-gate hb--; 8897c478bd9Sstevel@tonic-gate flist = lowbit(P2ALIGN(vmp->vm_freemap, 1UL << hb)); 8907c478bd9Sstevel@tonic-gate } 8917c478bd9Sstevel@tonic-gate 8927c478bd9Sstevel@tonic-gate for (vbest = NULL, vsp = (flist == 0) ? NULL : 8937c478bd9Sstevel@tonic-gate vmp->vm_freelist[flist - 1].vs_knext; 8947c478bd9Sstevel@tonic-gate vsp != NULL; vsp = vsp->vs_knext) { 8957c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_search++; 8967c478bd9Sstevel@tonic-gate if (vsp->vs_start == 0) { 8977c478bd9Sstevel@tonic-gate /* 8987c478bd9Sstevel@tonic-gate * We're moving up to a larger freelist, 8997c478bd9Sstevel@tonic-gate * so if we've already found a candidate, 9007c478bd9Sstevel@tonic-gate * the fit can't possibly get any better. 9017c478bd9Sstevel@tonic-gate */ 9027c478bd9Sstevel@tonic-gate if (vbest != NULL) 9037c478bd9Sstevel@tonic-gate break; 9047c478bd9Sstevel@tonic-gate /* 9057c478bd9Sstevel@tonic-gate * Find the next non-empty freelist. 9067c478bd9Sstevel@tonic-gate */ 9077c478bd9Sstevel@tonic-gate flist = lowbit(P2ALIGN(vmp->vm_freemap, 9087c478bd9Sstevel@tonic-gate VS_SIZE(vsp))); 9097c478bd9Sstevel@tonic-gate if (flist-- == 0) 9107c478bd9Sstevel@tonic-gate break; 9117c478bd9Sstevel@tonic-gate vsp = (vmem_seg_t *)&vmp->vm_freelist[flist]; 9127c478bd9Sstevel@tonic-gate ASSERT(vsp->vs_knext->vs_type == VMEM_FREE); 9137c478bd9Sstevel@tonic-gate continue; 9147c478bd9Sstevel@tonic-gate } 9157c478bd9Sstevel@tonic-gate if (vsp->vs_end - 1 < (uintptr_t)minaddr) 9167c478bd9Sstevel@tonic-gate continue; 9177c478bd9Sstevel@tonic-gate if (vsp->vs_start > (uintptr_t)maxaddr - 1) 9187c478bd9Sstevel@tonic-gate continue; 9197c478bd9Sstevel@tonic-gate start = MAX(vsp->vs_start, (uintptr_t)minaddr); 9207c478bd9Sstevel@tonic-gate end = MIN(vsp->vs_end - 1, (uintptr_t)maxaddr - 1) + 1; 9217c478bd9Sstevel@tonic-gate taddr = P2PHASEUP(start, align, phase); 9227c478bd9Sstevel@tonic-gate if (P2CROSS(taddr, taddr + size - 1, nocross)) 9237c478bd9Sstevel@tonic-gate taddr += 9247c478bd9Sstevel@tonic-gate P2ROUNDUP(P2NPHASE(taddr, nocross), align); 9257c478bd9Sstevel@tonic-gate if ((taddr - start) + size > end - start || 9267c478bd9Sstevel@tonic-gate (vbest != NULL && VS_SIZE(vsp) >= VS_SIZE(vbest))) 9277c478bd9Sstevel@tonic-gate continue; 9287c478bd9Sstevel@tonic-gate vbest = vsp; 9297c478bd9Sstevel@tonic-gate addr = taddr; 9307c478bd9Sstevel@tonic-gate if (!(vmflag & VM_BESTFIT) || VS_SIZE(vbest) == size) 9317c478bd9Sstevel@tonic-gate break; 9327c478bd9Sstevel@tonic-gate } 9337c478bd9Sstevel@tonic-gate if (vbest != NULL) 9347c478bd9Sstevel@tonic-gate break; 9357c478bd9Sstevel@tonic-gate if (size == 0) 9367c478bd9Sstevel@tonic-gate umem_panic("vmem_xalloc(): size == 0"); 9377c478bd9Sstevel@tonic-gate if (vmp->vm_source_alloc != NULL && nocross == 0 && 9387c478bd9Sstevel@tonic-gate minaddr == NULL && maxaddr == NULL) { 9397c478bd9Sstevel@tonic-gate size_t asize = P2ROUNDUP(size + phase, 9407c478bd9Sstevel@tonic-gate MAX(align, vmp->vm_source->vm_quantum)); 9417c478bd9Sstevel@tonic-gate if (asize < size) { /* overflow */ 9427c478bd9Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock); 9437c478bd9Sstevel@tonic-gate if (vmflag & VM_NOSLEEP) 9447c478bd9Sstevel@tonic-gate return (NULL); 9457c478bd9Sstevel@tonic-gate 9467c478bd9Sstevel@tonic-gate umem_panic("vmem_xalloc(): " 9477c478bd9Sstevel@tonic-gate "overflow on VM_SLEEP allocation"); 9487c478bd9Sstevel@tonic-gate } 9497c478bd9Sstevel@tonic-gate /* 9507c478bd9Sstevel@tonic-gate * Determine how many segment structures we'll consume. 9517c478bd9Sstevel@tonic-gate * The calculation must be presise because if we're 9527c478bd9Sstevel@tonic-gate * here on behalf of vmem_populate(), we are taking 9537c478bd9Sstevel@tonic-gate * segments from a very limited reserve. 9547c478bd9Sstevel@tonic-gate */ 9557c478bd9Sstevel@tonic-gate resv = (size == asize) ? 9567c478bd9Sstevel@tonic-gate VMEM_SEGS_PER_SPAN_CREATE + 9577c478bd9Sstevel@tonic-gate VMEM_SEGS_PER_EXACT_ALLOC : 9587c478bd9Sstevel@tonic-gate VMEM_SEGS_PER_ALLOC_MAX; 9597c478bd9Sstevel@tonic-gate ASSERT(vmp->vm_nsegfree >= resv); 9607c478bd9Sstevel@tonic-gate vmp->vm_nsegfree -= resv; /* reserve our segs */ 9617c478bd9Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock); 9627c478bd9Sstevel@tonic-gate vaddr = vmp->vm_source_alloc(vmp->vm_source, asize, 9637c478bd9Sstevel@tonic-gate vmflag & VM_UMFLAGS); 9647c478bd9Sstevel@tonic-gate (void) mutex_lock(&vmp->vm_lock); 9657c478bd9Sstevel@tonic-gate vmp->vm_nsegfree += resv; /* claim reservation */ 9667c478bd9Sstevel@tonic-gate if (vaddr != NULL) { 9677c478bd9Sstevel@tonic-gate vbest = vmem_span_create(vmp, vaddr, asize, 1); 9687c478bd9Sstevel@tonic-gate addr = P2PHASEUP(vbest->vs_start, align, phase); 9697c478bd9Sstevel@tonic-gate break; 9707c478bd9Sstevel@tonic-gate } 9717c478bd9Sstevel@tonic-gate } 9727c478bd9Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock); 9737c478bd9Sstevel@tonic-gate vmem_reap(); 9747c478bd9Sstevel@tonic-gate (void) mutex_lock(&vmp->vm_lock); 9757c478bd9Sstevel@tonic-gate if (vmflag & VM_NOSLEEP) 9767c478bd9Sstevel@tonic-gate break; 9777c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_wait++; 978*a574db85Sraf (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 979*a574db85Sraf &cancel_state); 980*a574db85Sraf (void) cond_wait(&vmp->vm_cv, &vmp->vm_lock); 981*a574db85Sraf (void) pthread_setcancelstate(cancel_state, NULL); 9827c478bd9Sstevel@tonic-gate } 9837c478bd9Sstevel@tonic-gate if (vbest != NULL) { 9847c478bd9Sstevel@tonic-gate ASSERT(vbest->vs_type == VMEM_FREE); 9857c478bd9Sstevel@tonic-gate ASSERT(vbest->vs_knext != vbest); 9867c478bd9Sstevel@tonic-gate (void) vmem_seg_alloc(vmp, vbest, addr, size); 9877c478bd9Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock); 9887c478bd9Sstevel@tonic-gate ASSERT(P2PHASE(addr, align) == phase); 9897c478bd9Sstevel@tonic-gate ASSERT(!P2CROSS(addr, addr + size - 1, nocross)); 9907c478bd9Sstevel@tonic-gate ASSERT(addr >= (uintptr_t)minaddr); 9917c478bd9Sstevel@tonic-gate ASSERT(addr + size - 1 <= (uintptr_t)maxaddr - 1); 9927c478bd9Sstevel@tonic-gate return ((void *)addr); 9937c478bd9Sstevel@tonic-gate } 9947c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_fail++; 9957c478bd9Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock); 9967c478bd9Sstevel@tonic-gate if (vmflag & VM_PANIC) 9977c478bd9Sstevel@tonic-gate umem_panic("vmem_xalloc(%p, %lu, %lu, %lu, %lu, %p, %p, %x): " 9987c478bd9Sstevel@tonic-gate "cannot satisfy mandatory allocation", 9997c478bd9Sstevel@tonic-gate (void *)vmp, size, align, phase, nocross, 10007c478bd9Sstevel@tonic-gate minaddr, maxaddr, vmflag); 10017c478bd9Sstevel@tonic-gate return (NULL); 10027c478bd9Sstevel@tonic-gate } 10037c478bd9Sstevel@tonic-gate 10047c478bd9Sstevel@tonic-gate /* 10057c478bd9Sstevel@tonic-gate * Free the segment [vaddr, vaddr + size), where vaddr was a constrained 10067c478bd9Sstevel@tonic-gate * allocation. vmem_xalloc() and vmem_xfree() must always be paired because 10077c478bd9Sstevel@tonic-gate * both routines bypass the quantum caches. 10087c478bd9Sstevel@tonic-gate */ 10097c478bd9Sstevel@tonic-gate void 10107c478bd9Sstevel@tonic-gate vmem_xfree(vmem_t *vmp, void *vaddr, size_t size) 10117c478bd9Sstevel@tonic-gate { 10127c478bd9Sstevel@tonic-gate vmem_seg_t *vsp, *vnext, *vprev; 10137c478bd9Sstevel@tonic-gate 10147c478bd9Sstevel@tonic-gate (void) mutex_lock(&vmp->vm_lock); 10157c478bd9Sstevel@tonic-gate 10167c478bd9Sstevel@tonic-gate vsp = vmem_hash_delete(vmp, (uintptr_t)vaddr, size); 10177c478bd9Sstevel@tonic-gate vsp->vs_end = P2ROUNDUP(vsp->vs_end, vmp->vm_quantum); 10187c478bd9Sstevel@tonic-gate 10197c478bd9Sstevel@tonic-gate /* 10207c478bd9Sstevel@tonic-gate * Attempt to coalesce with the next segment. 10217c478bd9Sstevel@tonic-gate */ 10227c478bd9Sstevel@tonic-gate vnext = vsp->vs_anext; 10237c478bd9Sstevel@tonic-gate if (vnext->vs_type == VMEM_FREE) { 10247c478bd9Sstevel@tonic-gate ASSERT(vsp->vs_end == vnext->vs_start); 10257c478bd9Sstevel@tonic-gate vmem_freelist_delete(vmp, vnext); 10267c478bd9Sstevel@tonic-gate vsp->vs_end = vnext->vs_end; 10277c478bd9Sstevel@tonic-gate vmem_seg_destroy(vmp, vnext); 10287c478bd9Sstevel@tonic-gate } 10297c478bd9Sstevel@tonic-gate 10307c478bd9Sstevel@tonic-gate /* 10317c478bd9Sstevel@tonic-gate * Attempt to coalesce with the previous segment. 10327c478bd9Sstevel@tonic-gate */ 10337c478bd9Sstevel@tonic-gate vprev = vsp->vs_aprev; 10347c478bd9Sstevel@tonic-gate if (vprev->vs_type == VMEM_FREE) { 10357c478bd9Sstevel@tonic-gate ASSERT(vprev->vs_end == vsp->vs_start); 10367c478bd9Sstevel@tonic-gate vmem_freelist_delete(vmp, vprev); 10377c478bd9Sstevel@tonic-gate vprev->vs_end = vsp->vs_end; 10387c478bd9Sstevel@tonic-gate vmem_seg_destroy(vmp, vsp); 10397c478bd9Sstevel@tonic-gate vsp = vprev; 10407c478bd9Sstevel@tonic-gate } 10417c478bd9Sstevel@tonic-gate 10427c478bd9Sstevel@tonic-gate /* 10437c478bd9Sstevel@tonic-gate * If the entire span is free, return it to the source. 10447c478bd9Sstevel@tonic-gate */ 10457c478bd9Sstevel@tonic-gate if (vsp->vs_import && vmp->vm_source_free != NULL && 10467c478bd9Sstevel@tonic-gate vsp->vs_aprev->vs_type == VMEM_SPAN && 10477c478bd9Sstevel@tonic-gate vsp->vs_anext->vs_type == VMEM_SPAN) { 10487c478bd9Sstevel@tonic-gate vaddr = (void *)vsp->vs_start; 10497c478bd9Sstevel@tonic-gate size = VS_SIZE(vsp); 10507c478bd9Sstevel@tonic-gate ASSERT(size == VS_SIZE(vsp->vs_aprev)); 10517c478bd9Sstevel@tonic-gate vmem_span_destroy(vmp, vsp); 10527c478bd9Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock); 10537c478bd9Sstevel@tonic-gate vmp->vm_source_free(vmp->vm_source, vaddr, size); 10547c478bd9Sstevel@tonic-gate } else { 10557c478bd9Sstevel@tonic-gate vmem_freelist_insert(vmp, vsp); 10567c478bd9Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock); 10577c478bd9Sstevel@tonic-gate } 10587c478bd9Sstevel@tonic-gate } 10597c478bd9Sstevel@tonic-gate 10607c478bd9Sstevel@tonic-gate /* 10617c478bd9Sstevel@tonic-gate * Allocate size bytes from arena vmp. Returns the allocated address 10627c478bd9Sstevel@tonic-gate * on success, NULL on failure. vmflag specifies VM_SLEEP or VM_NOSLEEP, 10637c478bd9Sstevel@tonic-gate * and may also specify best-fit, first-fit, or next-fit allocation policy 10647c478bd9Sstevel@tonic-gate * instead of the default instant-fit policy. VM_SLEEP allocations are 10657c478bd9Sstevel@tonic-gate * guaranteed to succeed. 10667c478bd9Sstevel@tonic-gate */ 10677c478bd9Sstevel@tonic-gate void * 10687c478bd9Sstevel@tonic-gate vmem_alloc(vmem_t *vmp, size_t size, int vmflag) 10697c478bd9Sstevel@tonic-gate { 10707c478bd9Sstevel@tonic-gate vmem_seg_t *vsp; 10717c478bd9Sstevel@tonic-gate uintptr_t addr; 10727c478bd9Sstevel@tonic-gate int hb; 10737c478bd9Sstevel@tonic-gate int flist = 0; 10747c478bd9Sstevel@tonic-gate uint32_t mtbf; 10757c478bd9Sstevel@tonic-gate 10767c478bd9Sstevel@tonic-gate if (size - 1 < vmp->vm_qcache_max) { 10777c478bd9Sstevel@tonic-gate ASSERT(vmflag & VM_NOSLEEP); 10787c478bd9Sstevel@tonic-gate return (_umem_cache_alloc(vmp->vm_qcache[(size - 1) >> 10797c478bd9Sstevel@tonic-gate vmp->vm_qshift], UMEM_DEFAULT)); 10807c478bd9Sstevel@tonic-gate } 10817c478bd9Sstevel@tonic-gate 10827c478bd9Sstevel@tonic-gate if ((mtbf = vmem_mtbf | vmp->vm_mtbf) != 0 && gethrtime() % mtbf == 0 && 10837c478bd9Sstevel@tonic-gate (vmflag & (VM_NOSLEEP | VM_PANIC)) == VM_NOSLEEP) 10847c478bd9Sstevel@tonic-gate return (NULL); 10857c478bd9Sstevel@tonic-gate 10867c478bd9Sstevel@tonic-gate if (vmflag & VM_NEXTFIT) 10877c478bd9Sstevel@tonic-gate return (vmem_nextfit_alloc(vmp, size, vmflag)); 10887c478bd9Sstevel@tonic-gate 10897c478bd9Sstevel@tonic-gate if (vmflag & (VM_BESTFIT | VM_FIRSTFIT)) 10907c478bd9Sstevel@tonic-gate return (vmem_xalloc(vmp, size, vmp->vm_quantum, 0, 0, 10917c478bd9Sstevel@tonic-gate NULL, NULL, vmflag)); 10927c478bd9Sstevel@tonic-gate 10937c478bd9Sstevel@tonic-gate /* 10947c478bd9Sstevel@tonic-gate * Unconstrained instant-fit allocation from the segment list. 10957c478bd9Sstevel@tonic-gate */ 10967c478bd9Sstevel@tonic-gate (void) mutex_lock(&vmp->vm_lock); 10977c478bd9Sstevel@tonic-gate 10987c478bd9Sstevel@tonic-gate if (vmp->vm_nsegfree >= VMEM_MINFREE || vmem_populate(vmp, vmflag)) { 10997c478bd9Sstevel@tonic-gate if ((size & (size - 1)) == 0) 11007c478bd9Sstevel@tonic-gate flist = lowbit(P2ALIGN(vmp->vm_freemap, size)); 11017c478bd9Sstevel@tonic-gate else if ((hb = highbit(size)) < VMEM_FREELISTS) 11027c478bd9Sstevel@tonic-gate flist = lowbit(P2ALIGN(vmp->vm_freemap, 1UL << hb)); 11037c478bd9Sstevel@tonic-gate } 11047c478bd9Sstevel@tonic-gate 11057c478bd9Sstevel@tonic-gate if (flist-- == 0) { 11067c478bd9Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock); 11077c478bd9Sstevel@tonic-gate return (vmem_xalloc(vmp, size, vmp->vm_quantum, 11087c478bd9Sstevel@tonic-gate 0, 0, NULL, NULL, vmflag)); 11097c478bd9Sstevel@tonic-gate } 11107c478bd9Sstevel@tonic-gate 11117c478bd9Sstevel@tonic-gate ASSERT(size <= (1UL << flist)); 11127c478bd9Sstevel@tonic-gate vsp = vmp->vm_freelist[flist].vs_knext; 11137c478bd9Sstevel@tonic-gate addr = vsp->vs_start; 11147c478bd9Sstevel@tonic-gate (void) vmem_seg_alloc(vmp, vsp, addr, size); 11157c478bd9Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock); 11167c478bd9Sstevel@tonic-gate return ((void *)addr); 11177c478bd9Sstevel@tonic-gate } 11187c478bd9Sstevel@tonic-gate 11197c478bd9Sstevel@tonic-gate /* 11207c478bd9Sstevel@tonic-gate * Free the segment [vaddr, vaddr + size). 11217c478bd9Sstevel@tonic-gate */ 11227c478bd9Sstevel@tonic-gate void 11237c478bd9Sstevel@tonic-gate vmem_free(vmem_t *vmp, void *vaddr, size_t size) 11247c478bd9Sstevel@tonic-gate { 11257c478bd9Sstevel@tonic-gate if (size - 1 < vmp->vm_qcache_max) 11267c478bd9Sstevel@tonic-gate _umem_cache_free(vmp->vm_qcache[(size - 1) >> vmp->vm_qshift], 11277c478bd9Sstevel@tonic-gate vaddr); 11287c478bd9Sstevel@tonic-gate else 11297c478bd9Sstevel@tonic-gate vmem_xfree(vmp, vaddr, size); 11307c478bd9Sstevel@tonic-gate } 11317c478bd9Sstevel@tonic-gate 11327c478bd9Sstevel@tonic-gate /* 11337c478bd9Sstevel@tonic-gate * Determine whether arena vmp contains the segment [vaddr, vaddr + size). 11347c478bd9Sstevel@tonic-gate */ 11357c478bd9Sstevel@tonic-gate int 11367c478bd9Sstevel@tonic-gate vmem_contains(vmem_t *vmp, void *vaddr, size_t size) 11377c478bd9Sstevel@tonic-gate { 11387c478bd9Sstevel@tonic-gate uintptr_t start = (uintptr_t)vaddr; 11397c478bd9Sstevel@tonic-gate uintptr_t end = start + size; 11407c478bd9Sstevel@tonic-gate vmem_seg_t *vsp; 11417c478bd9Sstevel@tonic-gate vmem_seg_t *seg0 = &vmp->vm_seg0; 11427c478bd9Sstevel@tonic-gate 11437c478bd9Sstevel@tonic-gate (void) mutex_lock(&vmp->vm_lock); 11447c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_contains++; 11457c478bd9Sstevel@tonic-gate for (vsp = seg0->vs_knext; vsp != seg0; vsp = vsp->vs_knext) { 11467c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_contains_search++; 11477c478bd9Sstevel@tonic-gate ASSERT(vsp->vs_type == VMEM_SPAN); 11487c478bd9Sstevel@tonic-gate if (start >= vsp->vs_start && end - 1 <= vsp->vs_end - 1) 11497c478bd9Sstevel@tonic-gate break; 11507c478bd9Sstevel@tonic-gate } 11517c478bd9Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock); 11527c478bd9Sstevel@tonic-gate return (vsp != seg0); 11537c478bd9Sstevel@tonic-gate } 11547c478bd9Sstevel@tonic-gate 11557c478bd9Sstevel@tonic-gate /* 11567c478bd9Sstevel@tonic-gate * Add the span [vaddr, vaddr + size) to arena vmp. 11577c478bd9Sstevel@tonic-gate */ 11587c478bd9Sstevel@tonic-gate void * 11597c478bd9Sstevel@tonic-gate vmem_add(vmem_t *vmp, void *vaddr, size_t size, int vmflag) 11607c478bd9Sstevel@tonic-gate { 11617c478bd9Sstevel@tonic-gate if (vaddr == NULL || size == 0) { 11627c478bd9Sstevel@tonic-gate umem_panic("vmem_add(%p, %p, %lu): bad arguments", 11637c478bd9Sstevel@tonic-gate vmp, vaddr, size); 11647c478bd9Sstevel@tonic-gate } 11657c478bd9Sstevel@tonic-gate 11667c478bd9Sstevel@tonic-gate ASSERT(!vmem_contains(vmp, vaddr, size)); 11677c478bd9Sstevel@tonic-gate 11687c478bd9Sstevel@tonic-gate (void) mutex_lock(&vmp->vm_lock); 11697c478bd9Sstevel@tonic-gate if (vmem_populate(vmp, vmflag)) 11707c478bd9Sstevel@tonic-gate (void) vmem_span_create(vmp, vaddr, size, 0); 11717c478bd9Sstevel@tonic-gate else 11727c478bd9Sstevel@tonic-gate vaddr = NULL; 11737c478bd9Sstevel@tonic-gate (void) cond_broadcast(&vmp->vm_cv); 11747c478bd9Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock); 11757c478bd9Sstevel@tonic-gate return (vaddr); 11767c478bd9Sstevel@tonic-gate } 11777c478bd9Sstevel@tonic-gate 11787c478bd9Sstevel@tonic-gate /* 11797c478bd9Sstevel@tonic-gate * Adds the address range [addr, endaddr) to arena vmp, by either: 11807c478bd9Sstevel@tonic-gate * 1. joining two existing spans, [x, addr), and [endaddr, y) (which 11817c478bd9Sstevel@tonic-gate * are in that order) into a single [x, y) span, 11827c478bd9Sstevel@tonic-gate * 2. expanding an existing [x, addr) span to [x, endaddr), 11837c478bd9Sstevel@tonic-gate * 3. expanding an existing [endaddr, x) span to [addr, x), or 11847c478bd9Sstevel@tonic-gate * 4. creating a new [addr, endaddr) span. 11857c478bd9Sstevel@tonic-gate * 11867c478bd9Sstevel@tonic-gate * Called with vmp->vm_lock held, and a successful vmem_populate() completed. 11877c478bd9Sstevel@tonic-gate * Cannot fail. Returns the new segment. 11887c478bd9Sstevel@tonic-gate * 11897c478bd9Sstevel@tonic-gate * NOTE: this algorithm is linear-time in the number of spans, but is 11907c478bd9Sstevel@tonic-gate * constant-time when you are extending the last (highest-addressed) 11917c478bd9Sstevel@tonic-gate * span. 11927c478bd9Sstevel@tonic-gate */ 11937c478bd9Sstevel@tonic-gate static vmem_seg_t * 11947c478bd9Sstevel@tonic-gate vmem_extend_unlocked(vmem_t *vmp, uintptr_t addr, uintptr_t endaddr) 11957c478bd9Sstevel@tonic-gate { 11967c478bd9Sstevel@tonic-gate vmem_seg_t *span; 11977c478bd9Sstevel@tonic-gate vmem_seg_t *vsp; 11987c478bd9Sstevel@tonic-gate 11997c478bd9Sstevel@tonic-gate vmem_seg_t *end = &vmp->vm_seg0; 12007c478bd9Sstevel@tonic-gate 12017c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&vmp->vm_lock)); 12027c478bd9Sstevel@tonic-gate 12037c478bd9Sstevel@tonic-gate /* 12047c478bd9Sstevel@tonic-gate * the second "if" clause below relies on the direction of this search 12057c478bd9Sstevel@tonic-gate */ 12067c478bd9Sstevel@tonic-gate for (span = end->vs_kprev; span != end; span = span->vs_kprev) { 12077c478bd9Sstevel@tonic-gate if (span->vs_end == addr || span->vs_start == endaddr) 12087c478bd9Sstevel@tonic-gate break; 12097c478bd9Sstevel@tonic-gate } 12107c478bd9Sstevel@tonic-gate 12117c478bd9Sstevel@tonic-gate if (span == end) 12127c478bd9Sstevel@tonic-gate return (vmem_span_create(vmp, (void *)addr, endaddr - addr, 0)); 12137c478bd9Sstevel@tonic-gate if (span->vs_kprev->vs_end == addr && span->vs_start == endaddr) { 12147c478bd9Sstevel@tonic-gate vmem_seg_t *prevspan = span->vs_kprev; 12157c478bd9Sstevel@tonic-gate vmem_seg_t *nextseg = span->vs_anext; 12167c478bd9Sstevel@tonic-gate vmem_seg_t *prevseg = span->vs_aprev; 12177c478bd9Sstevel@tonic-gate 12187c478bd9Sstevel@tonic-gate /* 12197c478bd9Sstevel@tonic-gate * prevspan becomes the span marker for the full range 12207c478bd9Sstevel@tonic-gate */ 12217c478bd9Sstevel@tonic-gate prevspan->vs_end = span->vs_end; 12227c478bd9Sstevel@tonic-gate 12237c478bd9Sstevel@tonic-gate /* 12247c478bd9Sstevel@tonic-gate * Notionally, span becomes a free segment representing 12257c478bd9Sstevel@tonic-gate * [addr, endaddr). 12267c478bd9Sstevel@tonic-gate * 12277c478bd9Sstevel@tonic-gate * However, if either of its neighbors are free, we coalesce 12287c478bd9Sstevel@tonic-gate * by destroying span and changing the free segment. 12297c478bd9Sstevel@tonic-gate */ 12307c478bd9Sstevel@tonic-gate if (prevseg->vs_type == VMEM_FREE && 12317c478bd9Sstevel@tonic-gate nextseg->vs_type == VMEM_FREE) { 12327c478bd9Sstevel@tonic-gate /* 12337c478bd9Sstevel@tonic-gate * coalesce both ways 12347c478bd9Sstevel@tonic-gate */ 12357c478bd9Sstevel@tonic-gate ASSERT(prevseg->vs_end == addr && 12367c478bd9Sstevel@tonic-gate nextseg->vs_start == endaddr); 12377c478bd9Sstevel@tonic-gate 12387c478bd9Sstevel@tonic-gate vmem_freelist_delete(vmp, prevseg); 12397c478bd9Sstevel@tonic-gate prevseg->vs_end = nextseg->vs_end; 12407c478bd9Sstevel@tonic-gate 12417c478bd9Sstevel@tonic-gate vmem_freelist_delete(vmp, nextseg); 12427c478bd9Sstevel@tonic-gate VMEM_DELETE(span, k); 12437c478bd9Sstevel@tonic-gate vmem_seg_destroy(vmp, nextseg); 12447c478bd9Sstevel@tonic-gate vmem_seg_destroy(vmp, span); 12457c478bd9Sstevel@tonic-gate 12467c478bd9Sstevel@tonic-gate vsp = prevseg; 12477c478bd9Sstevel@tonic-gate } else if (prevseg->vs_type == VMEM_FREE) { 12487c478bd9Sstevel@tonic-gate /* 12497c478bd9Sstevel@tonic-gate * coalesce left 12507c478bd9Sstevel@tonic-gate */ 12517c478bd9Sstevel@tonic-gate ASSERT(prevseg->vs_end == addr); 12527c478bd9Sstevel@tonic-gate 12537c478bd9Sstevel@tonic-gate VMEM_DELETE(span, k); 12547c478bd9Sstevel@tonic-gate vmem_seg_destroy(vmp, span); 12557c478bd9Sstevel@tonic-gate 12567c478bd9Sstevel@tonic-gate vmem_freelist_delete(vmp, prevseg); 12577c478bd9Sstevel@tonic-gate prevseg->vs_end = endaddr; 12587c478bd9Sstevel@tonic-gate 12597c478bd9Sstevel@tonic-gate vsp = prevseg; 12607c478bd9Sstevel@tonic-gate } else if (nextseg->vs_type == VMEM_FREE) { 12617c478bd9Sstevel@tonic-gate /* 12627c478bd9Sstevel@tonic-gate * coalesce right 12637c478bd9Sstevel@tonic-gate */ 12647c478bd9Sstevel@tonic-gate ASSERT(nextseg->vs_start == endaddr); 12657c478bd9Sstevel@tonic-gate 12667c478bd9Sstevel@tonic-gate VMEM_DELETE(span, k); 12677c478bd9Sstevel@tonic-gate vmem_seg_destroy(vmp, span); 12687c478bd9Sstevel@tonic-gate 12697c478bd9Sstevel@tonic-gate vmem_freelist_delete(vmp, nextseg); 12707c478bd9Sstevel@tonic-gate nextseg->vs_start = addr; 12717c478bd9Sstevel@tonic-gate 12727c478bd9Sstevel@tonic-gate vsp = nextseg; 12737c478bd9Sstevel@tonic-gate } else { 12747c478bd9Sstevel@tonic-gate /* 12757c478bd9Sstevel@tonic-gate * cannnot coalesce 12767c478bd9Sstevel@tonic-gate */ 12777c478bd9Sstevel@tonic-gate VMEM_DELETE(span, k); 12787c478bd9Sstevel@tonic-gate span->vs_start = addr; 12797c478bd9Sstevel@tonic-gate span->vs_end = endaddr; 12807c478bd9Sstevel@tonic-gate 12817c478bd9Sstevel@tonic-gate vsp = span; 12827c478bd9Sstevel@tonic-gate } 12837c478bd9Sstevel@tonic-gate } else if (span->vs_end == addr) { 12847c478bd9Sstevel@tonic-gate vmem_seg_t *oldseg = span->vs_knext->vs_aprev; 12857c478bd9Sstevel@tonic-gate span->vs_end = endaddr; 12867c478bd9Sstevel@tonic-gate 12877c478bd9Sstevel@tonic-gate ASSERT(oldseg->vs_type != VMEM_SPAN); 12887c478bd9Sstevel@tonic-gate if (oldseg->vs_type == VMEM_FREE) { 12897c478bd9Sstevel@tonic-gate ASSERT(oldseg->vs_end == addr); 12907c478bd9Sstevel@tonic-gate vmem_freelist_delete(vmp, oldseg); 12917c478bd9Sstevel@tonic-gate oldseg->vs_end = endaddr; 12927c478bd9Sstevel@tonic-gate vsp = oldseg; 12937c478bd9Sstevel@tonic-gate } else 12947c478bd9Sstevel@tonic-gate vsp = vmem_seg_create(vmp, oldseg, addr, endaddr); 12957c478bd9Sstevel@tonic-gate } else { 12967c478bd9Sstevel@tonic-gate vmem_seg_t *oldseg = span->vs_anext; 12977c478bd9Sstevel@tonic-gate ASSERT(span->vs_start == endaddr); 12987c478bd9Sstevel@tonic-gate span->vs_start = addr; 12997c478bd9Sstevel@tonic-gate 13007c478bd9Sstevel@tonic-gate ASSERT(oldseg->vs_type != VMEM_SPAN); 13017c478bd9Sstevel@tonic-gate if (oldseg->vs_type == VMEM_FREE) { 13027c478bd9Sstevel@tonic-gate ASSERT(oldseg->vs_start == endaddr); 13037c478bd9Sstevel@tonic-gate vmem_freelist_delete(vmp, oldseg); 13047c478bd9Sstevel@tonic-gate oldseg->vs_start = addr; 13057c478bd9Sstevel@tonic-gate vsp = oldseg; 13067c478bd9Sstevel@tonic-gate } else 13077c478bd9Sstevel@tonic-gate vsp = vmem_seg_create(vmp, span, addr, endaddr); 13087c478bd9Sstevel@tonic-gate } 13097c478bd9Sstevel@tonic-gate vmem_freelist_insert(vmp, vsp); 13107c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_mem_total += (endaddr - addr); 13117c478bd9Sstevel@tonic-gate return (vsp); 13127c478bd9Sstevel@tonic-gate } 13137c478bd9Sstevel@tonic-gate 13147c478bd9Sstevel@tonic-gate /* 13157c478bd9Sstevel@tonic-gate * Does some error checking, calls vmem_extend_unlocked to add 13167c478bd9Sstevel@tonic-gate * [vaddr, vaddr+size) to vmp, then allocates alloc bytes from the 13177c478bd9Sstevel@tonic-gate * newly merged segment. 13187c478bd9Sstevel@tonic-gate */ 13197c478bd9Sstevel@tonic-gate void * 13207c478bd9Sstevel@tonic-gate _vmem_extend_alloc(vmem_t *vmp, void *vaddr, size_t size, size_t alloc, 13217c478bd9Sstevel@tonic-gate int vmflag) 13227c478bd9Sstevel@tonic-gate { 13237c478bd9Sstevel@tonic-gate uintptr_t addr = (uintptr_t)vaddr; 13247c478bd9Sstevel@tonic-gate uintptr_t endaddr = addr + size; 13257c478bd9Sstevel@tonic-gate vmem_seg_t *vsp; 13267c478bd9Sstevel@tonic-gate 13277c478bd9Sstevel@tonic-gate ASSERT(vaddr != NULL && size != 0 && endaddr > addr); 13287c478bd9Sstevel@tonic-gate ASSERT(alloc <= size && alloc != 0); 13297c478bd9Sstevel@tonic-gate ASSERT(((addr | size | alloc) & (vmp->vm_quantum - 1)) == 0); 13307c478bd9Sstevel@tonic-gate 13317c478bd9Sstevel@tonic-gate ASSERT(!vmem_contains(vmp, vaddr, size)); 13327c478bd9Sstevel@tonic-gate 13337c478bd9Sstevel@tonic-gate (void) mutex_lock(&vmp->vm_lock); 13347c478bd9Sstevel@tonic-gate if (!vmem_populate(vmp, vmflag)) { 13357c478bd9Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock); 13367c478bd9Sstevel@tonic-gate return (NULL); 13377c478bd9Sstevel@tonic-gate } 13387c478bd9Sstevel@tonic-gate /* 13397c478bd9Sstevel@tonic-gate * if there is a source, we can't mess with the spans 13407c478bd9Sstevel@tonic-gate */ 13417c478bd9Sstevel@tonic-gate if (vmp->vm_source_alloc != NULL) 13427c478bd9Sstevel@tonic-gate vsp = vmem_span_create(vmp, vaddr, size, 0); 13437c478bd9Sstevel@tonic-gate else 13447c478bd9Sstevel@tonic-gate vsp = vmem_extend_unlocked(vmp, addr, endaddr); 13457c478bd9Sstevel@tonic-gate 13467c478bd9Sstevel@tonic-gate ASSERT(VS_SIZE(vsp) >= alloc); 13477c478bd9Sstevel@tonic-gate 13487c478bd9Sstevel@tonic-gate addr = vsp->vs_start; 13497c478bd9Sstevel@tonic-gate (void) vmem_seg_alloc(vmp, vsp, addr, alloc); 13507c478bd9Sstevel@tonic-gate vaddr = (void *)addr; 13517c478bd9Sstevel@tonic-gate 13527c478bd9Sstevel@tonic-gate (void) cond_broadcast(&vmp->vm_cv); 13537c478bd9Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock); 13547c478bd9Sstevel@tonic-gate 13557c478bd9Sstevel@tonic-gate return (vaddr); 13567c478bd9Sstevel@tonic-gate } 13577c478bd9Sstevel@tonic-gate 13587c478bd9Sstevel@tonic-gate /* 13597c478bd9Sstevel@tonic-gate * Walk the vmp arena, applying func to each segment matching typemask. 13607c478bd9Sstevel@tonic-gate * If VMEM_REENTRANT is specified, the arena lock is dropped across each 13617c478bd9Sstevel@tonic-gate * call to func(); otherwise, it is held for the duration of vmem_walk() 13627c478bd9Sstevel@tonic-gate * to ensure a consistent snapshot. Note that VMEM_REENTRANT callbacks 13637c478bd9Sstevel@tonic-gate * are *not* necessarily consistent, so they may only be used when a hint 13647c478bd9Sstevel@tonic-gate * is adequate. 13657c478bd9Sstevel@tonic-gate */ 13667c478bd9Sstevel@tonic-gate void 13677c478bd9Sstevel@tonic-gate vmem_walk(vmem_t *vmp, int typemask, 13687c478bd9Sstevel@tonic-gate void (*func)(void *, void *, size_t), void *arg) 13697c478bd9Sstevel@tonic-gate { 13707c478bd9Sstevel@tonic-gate vmem_seg_t *vsp; 13717c478bd9Sstevel@tonic-gate vmem_seg_t *seg0 = &vmp->vm_seg0; 13727c478bd9Sstevel@tonic-gate vmem_seg_t walker; 13737c478bd9Sstevel@tonic-gate 13747c478bd9Sstevel@tonic-gate if (typemask & VMEM_WALKER) 13757c478bd9Sstevel@tonic-gate return; 13767c478bd9Sstevel@tonic-gate 13777c478bd9Sstevel@tonic-gate bzero(&walker, sizeof (walker)); 13787c478bd9Sstevel@tonic-gate walker.vs_type = VMEM_WALKER; 13797c478bd9Sstevel@tonic-gate 13807c478bd9Sstevel@tonic-gate (void) mutex_lock(&vmp->vm_lock); 13817c478bd9Sstevel@tonic-gate VMEM_INSERT(seg0, &walker, a); 13827c478bd9Sstevel@tonic-gate for (vsp = seg0->vs_anext; vsp != seg0; vsp = vsp->vs_anext) { 13837c478bd9Sstevel@tonic-gate if (vsp->vs_type & typemask) { 13847c478bd9Sstevel@tonic-gate void *start = (void *)vsp->vs_start; 13857c478bd9Sstevel@tonic-gate size_t size = VS_SIZE(vsp); 13867c478bd9Sstevel@tonic-gate if (typemask & VMEM_REENTRANT) { 13877c478bd9Sstevel@tonic-gate vmem_advance(vmp, &walker, vsp); 13887c478bd9Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock); 13897c478bd9Sstevel@tonic-gate func(arg, start, size); 13907c478bd9Sstevel@tonic-gate (void) mutex_lock(&vmp->vm_lock); 13917c478bd9Sstevel@tonic-gate vsp = &walker; 13927c478bd9Sstevel@tonic-gate } else { 13937c478bd9Sstevel@tonic-gate func(arg, start, size); 13947c478bd9Sstevel@tonic-gate } 13957c478bd9Sstevel@tonic-gate } 13967c478bd9Sstevel@tonic-gate } 13977c478bd9Sstevel@tonic-gate vmem_advance(vmp, &walker, NULL); 13987c478bd9Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock); 13997c478bd9Sstevel@tonic-gate } 14007c478bd9Sstevel@tonic-gate 14017c478bd9Sstevel@tonic-gate /* 14027c478bd9Sstevel@tonic-gate * Return the total amount of memory whose type matches typemask. Thus: 14037c478bd9Sstevel@tonic-gate * 14047c478bd9Sstevel@tonic-gate * typemask VMEM_ALLOC yields total memory allocated (in use). 14057c478bd9Sstevel@tonic-gate * typemask VMEM_FREE yields total memory free (available). 14067c478bd9Sstevel@tonic-gate * typemask (VMEM_ALLOC | VMEM_FREE) yields total arena size. 14077c478bd9Sstevel@tonic-gate */ 14087c478bd9Sstevel@tonic-gate size_t 14097c478bd9Sstevel@tonic-gate vmem_size(vmem_t *vmp, int typemask) 14107c478bd9Sstevel@tonic-gate { 14117c478bd9Sstevel@tonic-gate uint64_t size = 0; 14127c478bd9Sstevel@tonic-gate 14137c478bd9Sstevel@tonic-gate if (typemask & VMEM_ALLOC) 14147c478bd9Sstevel@tonic-gate size += vmp->vm_kstat.vk_mem_inuse; 14157c478bd9Sstevel@tonic-gate if (typemask & VMEM_FREE) 14167c478bd9Sstevel@tonic-gate size += vmp->vm_kstat.vk_mem_total - 14177c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_mem_inuse; 14187c478bd9Sstevel@tonic-gate return ((size_t)size); 14197c478bd9Sstevel@tonic-gate } 14207c478bd9Sstevel@tonic-gate 14217c478bd9Sstevel@tonic-gate /* 14227c478bd9Sstevel@tonic-gate * Create an arena called name whose initial span is [base, base + size). 14237c478bd9Sstevel@tonic-gate * The arena's natural unit of currency is quantum, so vmem_alloc() 14247c478bd9Sstevel@tonic-gate * guarantees quantum-aligned results. The arena may import new spans 14257c478bd9Sstevel@tonic-gate * by invoking afunc() on source, and may return those spans by invoking 14267c478bd9Sstevel@tonic-gate * ffunc() on source. To make small allocations fast and scalable, 14277c478bd9Sstevel@tonic-gate * the arena offers high-performance caching for each integer multiple 14287c478bd9Sstevel@tonic-gate * of quantum up to qcache_max. 14297c478bd9Sstevel@tonic-gate */ 14307c478bd9Sstevel@tonic-gate vmem_t * 14317c478bd9Sstevel@tonic-gate vmem_create(const char *name, void *base, size_t size, size_t quantum, 14327c478bd9Sstevel@tonic-gate vmem_alloc_t *afunc, vmem_free_t *ffunc, vmem_t *source, 14337c478bd9Sstevel@tonic-gate size_t qcache_max, int vmflag) 14347c478bd9Sstevel@tonic-gate { 14357c478bd9Sstevel@tonic-gate int i; 14367c478bd9Sstevel@tonic-gate size_t nqcache; 14377c478bd9Sstevel@tonic-gate vmem_t *vmp, *cur, **vmpp; 14387c478bd9Sstevel@tonic-gate vmem_seg_t *vsp; 14397c478bd9Sstevel@tonic-gate vmem_freelist_t *vfp; 14407c478bd9Sstevel@tonic-gate uint32_t id = atomic_add_32_nv(&vmem_id, 1); 14417c478bd9Sstevel@tonic-gate 14427c478bd9Sstevel@tonic-gate if (vmem_vmem_arena != NULL) { 14437c478bd9Sstevel@tonic-gate vmp = vmem_alloc(vmem_vmem_arena, sizeof (vmem_t), 14447c478bd9Sstevel@tonic-gate vmflag & VM_UMFLAGS); 14457c478bd9Sstevel@tonic-gate } else { 14467c478bd9Sstevel@tonic-gate ASSERT(id <= VMEM_INITIAL); 14477c478bd9Sstevel@tonic-gate vmp = &vmem0[id - 1]; 14487c478bd9Sstevel@tonic-gate } 14497c478bd9Sstevel@tonic-gate 14507c478bd9Sstevel@tonic-gate if (vmp == NULL) 14517c478bd9Sstevel@tonic-gate return (NULL); 14527c478bd9Sstevel@tonic-gate bzero(vmp, sizeof (vmem_t)); 14537c478bd9Sstevel@tonic-gate 14547c478bd9Sstevel@tonic-gate (void) snprintf(vmp->vm_name, VMEM_NAMELEN, "%s", name); 14557c478bd9Sstevel@tonic-gate (void) mutex_init(&vmp->vm_lock, USYNC_THREAD, NULL); 14567c478bd9Sstevel@tonic-gate (void) cond_init(&vmp->vm_cv, USYNC_THREAD, NULL); 14577c478bd9Sstevel@tonic-gate vmp->vm_cflags = vmflag; 14587c478bd9Sstevel@tonic-gate vmflag &= VM_UMFLAGS; 14597c478bd9Sstevel@tonic-gate 14607c478bd9Sstevel@tonic-gate vmp->vm_quantum = quantum; 14617c478bd9Sstevel@tonic-gate vmp->vm_qshift = highbit(quantum) - 1; 14627c478bd9Sstevel@tonic-gate nqcache = MIN(qcache_max >> vmp->vm_qshift, VMEM_NQCACHE_MAX); 14637c478bd9Sstevel@tonic-gate 14647c478bd9Sstevel@tonic-gate for (i = 0; i <= VMEM_FREELISTS; i++) { 14657c478bd9Sstevel@tonic-gate vfp = &vmp->vm_freelist[i]; 14667c478bd9Sstevel@tonic-gate vfp->vs_end = 1UL << i; 14677c478bd9Sstevel@tonic-gate vfp->vs_knext = (vmem_seg_t *)(vfp + 1); 14687c478bd9Sstevel@tonic-gate vfp->vs_kprev = (vmem_seg_t *)(vfp - 1); 14697c478bd9Sstevel@tonic-gate } 14707c478bd9Sstevel@tonic-gate 14717c478bd9Sstevel@tonic-gate vmp->vm_freelist[0].vs_kprev = NULL; 14727c478bd9Sstevel@tonic-gate vmp->vm_freelist[VMEM_FREELISTS].vs_knext = NULL; 14737c478bd9Sstevel@tonic-gate vmp->vm_freelist[VMEM_FREELISTS].vs_end = 0; 14747c478bd9Sstevel@tonic-gate vmp->vm_hash_table = vmp->vm_hash0; 14757c478bd9Sstevel@tonic-gate vmp->vm_hash_mask = VMEM_HASH_INITIAL - 1; 14767c478bd9Sstevel@tonic-gate vmp->vm_hash_shift = highbit(vmp->vm_hash_mask); 14777c478bd9Sstevel@tonic-gate 14787c478bd9Sstevel@tonic-gate vsp = &vmp->vm_seg0; 14797c478bd9Sstevel@tonic-gate vsp->vs_anext = vsp; 14807c478bd9Sstevel@tonic-gate vsp->vs_aprev = vsp; 14817c478bd9Sstevel@tonic-gate vsp->vs_knext = vsp; 14827c478bd9Sstevel@tonic-gate vsp->vs_kprev = vsp; 14837c478bd9Sstevel@tonic-gate vsp->vs_type = VMEM_SPAN; 14847c478bd9Sstevel@tonic-gate 14857c478bd9Sstevel@tonic-gate vsp = &vmp->vm_rotor; 14867c478bd9Sstevel@tonic-gate vsp->vs_type = VMEM_ROTOR; 14877c478bd9Sstevel@tonic-gate VMEM_INSERT(&vmp->vm_seg0, vsp, a); 14887c478bd9Sstevel@tonic-gate 14897c478bd9Sstevel@tonic-gate vmp->vm_id = id; 14907c478bd9Sstevel@tonic-gate if (source != NULL) 14917c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_source_id = source->vm_id; 14927c478bd9Sstevel@tonic-gate vmp->vm_source = source; 14937c478bd9Sstevel@tonic-gate vmp->vm_source_alloc = afunc; 14947c478bd9Sstevel@tonic-gate vmp->vm_source_free = ffunc; 14957c478bd9Sstevel@tonic-gate 14967c478bd9Sstevel@tonic-gate if (nqcache != 0) { 14977c478bd9Sstevel@tonic-gate vmp->vm_qcache_max = nqcache << vmp->vm_qshift; 14987c478bd9Sstevel@tonic-gate for (i = 0; i < nqcache; i++) { 14997c478bd9Sstevel@tonic-gate char buf[VMEM_NAMELEN + 21]; 15007c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%s_%lu", 15017c478bd9Sstevel@tonic-gate vmp->vm_name, (long)((i + 1) * quantum)); 15027c478bd9Sstevel@tonic-gate vmp->vm_qcache[i] = umem_cache_create(buf, 15037c478bd9Sstevel@tonic-gate (i + 1) * quantum, quantum, NULL, NULL, NULL, 15047c478bd9Sstevel@tonic-gate NULL, vmp, UMC_QCACHE | UMC_NOTOUCH); 15057c478bd9Sstevel@tonic-gate if (vmp->vm_qcache[i] == NULL) { 15067c478bd9Sstevel@tonic-gate vmp->vm_qcache_max = i * quantum; 15077c478bd9Sstevel@tonic-gate break; 15087c478bd9Sstevel@tonic-gate } 15097c478bd9Sstevel@tonic-gate } 15107c478bd9Sstevel@tonic-gate } 15117c478bd9Sstevel@tonic-gate 15127c478bd9Sstevel@tonic-gate (void) mutex_lock(&vmem_list_lock); 15137c478bd9Sstevel@tonic-gate vmpp = &vmem_list; 15147c478bd9Sstevel@tonic-gate while ((cur = *vmpp) != NULL) 15157c478bd9Sstevel@tonic-gate vmpp = &cur->vm_next; 15167c478bd9Sstevel@tonic-gate *vmpp = vmp; 15177c478bd9Sstevel@tonic-gate (void) mutex_unlock(&vmem_list_lock); 15187c478bd9Sstevel@tonic-gate 15197c478bd9Sstevel@tonic-gate if (vmp->vm_cflags & VMC_POPULATOR) { 15207c478bd9Sstevel@tonic-gate uint_t pop_id = atomic_add_32_nv(&vmem_populators, 1); 15217c478bd9Sstevel@tonic-gate ASSERT(pop_id <= VMEM_INITIAL); 15227c478bd9Sstevel@tonic-gate vmem_populator[pop_id - 1] = vmp; 15237c478bd9Sstevel@tonic-gate (void) mutex_lock(&vmp->vm_lock); 15247c478bd9Sstevel@tonic-gate (void) vmem_populate(vmp, vmflag | VM_PANIC); 15257c478bd9Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock); 15267c478bd9Sstevel@tonic-gate } 15277c478bd9Sstevel@tonic-gate 15287c478bd9Sstevel@tonic-gate if ((base || size) && vmem_add(vmp, base, size, vmflag) == NULL) { 15297c478bd9Sstevel@tonic-gate vmem_destroy(vmp); 15307c478bd9Sstevel@tonic-gate return (NULL); 15317c478bd9Sstevel@tonic-gate } 15327c478bd9Sstevel@tonic-gate 15337c478bd9Sstevel@tonic-gate return (vmp); 15347c478bd9Sstevel@tonic-gate } 15357c478bd9Sstevel@tonic-gate 15367c478bd9Sstevel@tonic-gate /* 15377c478bd9Sstevel@tonic-gate * Destroy arena vmp. 15387c478bd9Sstevel@tonic-gate */ 15397c478bd9Sstevel@tonic-gate void 15407c478bd9Sstevel@tonic-gate vmem_destroy(vmem_t *vmp) 15417c478bd9Sstevel@tonic-gate { 15427c478bd9Sstevel@tonic-gate vmem_t *cur, **vmpp; 15437c478bd9Sstevel@tonic-gate vmem_seg_t *seg0 = &vmp->vm_seg0; 15447c478bd9Sstevel@tonic-gate vmem_seg_t *vsp; 15457c478bd9Sstevel@tonic-gate size_t leaked; 15467c478bd9Sstevel@tonic-gate int i; 15477c478bd9Sstevel@tonic-gate 15487c478bd9Sstevel@tonic-gate (void) mutex_lock(&vmem_list_lock); 15497c478bd9Sstevel@tonic-gate vmpp = &vmem_list; 15507c478bd9Sstevel@tonic-gate while ((cur = *vmpp) != vmp) 15517c478bd9Sstevel@tonic-gate vmpp = &cur->vm_next; 15527c478bd9Sstevel@tonic-gate *vmpp = vmp->vm_next; 15537c478bd9Sstevel@tonic-gate (void) mutex_unlock(&vmem_list_lock); 15547c478bd9Sstevel@tonic-gate 15557c478bd9Sstevel@tonic-gate for (i = 0; i < VMEM_NQCACHE_MAX; i++) 15567c478bd9Sstevel@tonic-gate if (vmp->vm_qcache[i]) 15577c478bd9Sstevel@tonic-gate umem_cache_destroy(vmp->vm_qcache[i]); 15587c478bd9Sstevel@tonic-gate 15597c478bd9Sstevel@tonic-gate leaked = vmem_size(vmp, VMEM_ALLOC); 15607c478bd9Sstevel@tonic-gate if (leaked != 0) 15617c478bd9Sstevel@tonic-gate umem_printf("vmem_destroy('%s'): leaked %lu bytes", 15627c478bd9Sstevel@tonic-gate vmp->vm_name, leaked); 15637c478bd9Sstevel@tonic-gate 15647c478bd9Sstevel@tonic-gate if (vmp->vm_hash_table != vmp->vm_hash0) 15657c478bd9Sstevel@tonic-gate vmem_free(vmem_hash_arena, vmp->vm_hash_table, 15667c478bd9Sstevel@tonic-gate (vmp->vm_hash_mask + 1) * sizeof (void *)); 15677c478bd9Sstevel@tonic-gate 15687c478bd9Sstevel@tonic-gate /* 15697c478bd9Sstevel@tonic-gate * Give back the segment structures for anything that's left in the 15707c478bd9Sstevel@tonic-gate * arena, e.g. the primary spans and their free segments. 15717c478bd9Sstevel@tonic-gate */ 15727c478bd9Sstevel@tonic-gate VMEM_DELETE(&vmp->vm_rotor, a); 15737c478bd9Sstevel@tonic-gate for (vsp = seg0->vs_anext; vsp != seg0; vsp = vsp->vs_anext) 15747c478bd9Sstevel@tonic-gate vmem_putseg_global(vsp); 15757c478bd9Sstevel@tonic-gate 15767c478bd9Sstevel@tonic-gate while (vmp->vm_nsegfree > 0) 15777c478bd9Sstevel@tonic-gate vmem_putseg_global(vmem_getseg(vmp)); 15787c478bd9Sstevel@tonic-gate 15797c478bd9Sstevel@tonic-gate (void) mutex_destroy(&vmp->vm_lock); 15807c478bd9Sstevel@tonic-gate (void) cond_destroy(&vmp->vm_cv); 15817c478bd9Sstevel@tonic-gate vmem_free(vmem_vmem_arena, vmp, sizeof (vmem_t)); 15827c478bd9Sstevel@tonic-gate } 15837c478bd9Sstevel@tonic-gate 15847c478bd9Sstevel@tonic-gate /* 15857c478bd9Sstevel@tonic-gate * Resize vmp's hash table to keep the average lookup depth near 1.0. 15867c478bd9Sstevel@tonic-gate */ 15877c478bd9Sstevel@tonic-gate static void 15887c478bd9Sstevel@tonic-gate vmem_hash_rescale(vmem_t *vmp) 15897c478bd9Sstevel@tonic-gate { 15907c478bd9Sstevel@tonic-gate vmem_seg_t **old_table, **new_table, *vsp; 15917c478bd9Sstevel@tonic-gate size_t old_size, new_size, h, nseg; 15927c478bd9Sstevel@tonic-gate 15937c478bd9Sstevel@tonic-gate nseg = (size_t)(vmp->vm_kstat.vk_alloc - vmp->vm_kstat.vk_free); 15947c478bd9Sstevel@tonic-gate 15957c478bd9Sstevel@tonic-gate new_size = MAX(VMEM_HASH_INITIAL, 1 << (highbit(3 * nseg + 4) - 2)); 15967c478bd9Sstevel@tonic-gate old_size = vmp->vm_hash_mask + 1; 15977c478bd9Sstevel@tonic-gate 15987c478bd9Sstevel@tonic-gate if ((old_size >> 1) <= new_size && new_size <= (old_size << 1)) 15997c478bd9Sstevel@tonic-gate return; 16007c478bd9Sstevel@tonic-gate 16017c478bd9Sstevel@tonic-gate new_table = vmem_alloc(vmem_hash_arena, new_size * sizeof (void *), 16027c478bd9Sstevel@tonic-gate VM_NOSLEEP); 16037c478bd9Sstevel@tonic-gate if (new_table == NULL) 16047c478bd9Sstevel@tonic-gate return; 16057c478bd9Sstevel@tonic-gate bzero(new_table, new_size * sizeof (void *)); 16067c478bd9Sstevel@tonic-gate 16077c478bd9Sstevel@tonic-gate (void) mutex_lock(&vmp->vm_lock); 16087c478bd9Sstevel@tonic-gate 16097c478bd9Sstevel@tonic-gate old_size = vmp->vm_hash_mask + 1; 16107c478bd9Sstevel@tonic-gate old_table = vmp->vm_hash_table; 16117c478bd9Sstevel@tonic-gate 16127c478bd9Sstevel@tonic-gate vmp->vm_hash_mask = new_size - 1; 16137c478bd9Sstevel@tonic-gate vmp->vm_hash_table = new_table; 16147c478bd9Sstevel@tonic-gate vmp->vm_hash_shift = highbit(vmp->vm_hash_mask); 16157c478bd9Sstevel@tonic-gate 16167c478bd9Sstevel@tonic-gate for (h = 0; h < old_size; h++) { 16177c478bd9Sstevel@tonic-gate vsp = old_table[h]; 16187c478bd9Sstevel@tonic-gate while (vsp != NULL) { 16197c478bd9Sstevel@tonic-gate uintptr_t addr = vsp->vs_start; 16207c478bd9Sstevel@tonic-gate vmem_seg_t *next_vsp = vsp->vs_knext; 16217c478bd9Sstevel@tonic-gate vmem_seg_t **hash_bucket = VMEM_HASH(vmp, addr); 16227c478bd9Sstevel@tonic-gate vsp->vs_knext = *hash_bucket; 16237c478bd9Sstevel@tonic-gate *hash_bucket = vsp; 16247c478bd9Sstevel@tonic-gate vsp = next_vsp; 16257c478bd9Sstevel@tonic-gate } 16267c478bd9Sstevel@tonic-gate } 16277c478bd9Sstevel@tonic-gate 16287c478bd9Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock); 16297c478bd9Sstevel@tonic-gate 16307c478bd9Sstevel@tonic-gate if (old_table != vmp->vm_hash0) 16317c478bd9Sstevel@tonic-gate vmem_free(vmem_hash_arena, old_table, 16327c478bd9Sstevel@tonic-gate old_size * sizeof (void *)); 16337c478bd9Sstevel@tonic-gate } 16347c478bd9Sstevel@tonic-gate 16357c478bd9Sstevel@tonic-gate /* 16367c478bd9Sstevel@tonic-gate * Perform periodic maintenance on all vmem arenas. 16377c478bd9Sstevel@tonic-gate */ 16387c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 16397c478bd9Sstevel@tonic-gate void 16407c478bd9Sstevel@tonic-gate vmem_update(void *dummy) 16417c478bd9Sstevel@tonic-gate { 16427c478bd9Sstevel@tonic-gate vmem_t *vmp; 16437c478bd9Sstevel@tonic-gate 16447c478bd9Sstevel@tonic-gate (void) mutex_lock(&vmem_list_lock); 16457c478bd9Sstevel@tonic-gate for (vmp = vmem_list; vmp != NULL; vmp = vmp->vm_next) { 16467c478bd9Sstevel@tonic-gate /* 16477c478bd9Sstevel@tonic-gate * If threads are waiting for resources, wake them up 16487c478bd9Sstevel@tonic-gate * periodically so they can issue another vmem_reap() 16497c478bd9Sstevel@tonic-gate * to reclaim resources cached by the slab allocator. 16507c478bd9Sstevel@tonic-gate */ 16517c478bd9Sstevel@tonic-gate (void) cond_broadcast(&vmp->vm_cv); 16527c478bd9Sstevel@tonic-gate 16537c478bd9Sstevel@tonic-gate /* 16547c478bd9Sstevel@tonic-gate * Rescale the hash table to keep the hash chains short. 16557c478bd9Sstevel@tonic-gate */ 16567c478bd9Sstevel@tonic-gate vmem_hash_rescale(vmp); 16577c478bd9Sstevel@tonic-gate } 16587c478bd9Sstevel@tonic-gate (void) mutex_unlock(&vmem_list_lock); 16597c478bd9Sstevel@tonic-gate } 16607c478bd9Sstevel@tonic-gate 16617c478bd9Sstevel@tonic-gate /* 16627c478bd9Sstevel@tonic-gate * If vmem_init is called again, we need to be able to reset the world. 16637c478bd9Sstevel@tonic-gate * That includes resetting the statics back to their original values. 16647c478bd9Sstevel@tonic-gate */ 16657c478bd9Sstevel@tonic-gate void 16667c478bd9Sstevel@tonic-gate vmem_startup(void) 16677c478bd9Sstevel@tonic-gate { 16687c478bd9Sstevel@tonic-gate #ifdef UMEM_STANDALONE 16697c478bd9Sstevel@tonic-gate vmem_id = 0; 16707c478bd9Sstevel@tonic-gate vmem_populators = 0; 16717c478bd9Sstevel@tonic-gate vmem_segfree = NULL; 16727c478bd9Sstevel@tonic-gate vmem_list = NULL; 16737c478bd9Sstevel@tonic-gate vmem_internal_arena = NULL; 16747c478bd9Sstevel@tonic-gate vmem_seg_arena = NULL; 16757c478bd9Sstevel@tonic-gate vmem_hash_arena = NULL; 16767c478bd9Sstevel@tonic-gate vmem_vmem_arena = NULL; 16777c478bd9Sstevel@tonic-gate vmem_heap = NULL; 16787c478bd9Sstevel@tonic-gate vmem_heap_alloc = NULL; 16797c478bd9Sstevel@tonic-gate vmem_heap_free = NULL; 16807c478bd9Sstevel@tonic-gate 16817c478bd9Sstevel@tonic-gate bzero(vmem0, sizeof (vmem0)); 16827c478bd9Sstevel@tonic-gate bzero(vmem_populator, sizeof (vmem_populator)); 16837c478bd9Sstevel@tonic-gate bzero(vmem_seg0, sizeof (vmem_seg0)); 16847c478bd9Sstevel@tonic-gate #endif 16857c478bd9Sstevel@tonic-gate } 16867c478bd9Sstevel@tonic-gate 16877c478bd9Sstevel@tonic-gate /* 16887c478bd9Sstevel@tonic-gate * Prepare vmem for use. 16897c478bd9Sstevel@tonic-gate */ 16907c478bd9Sstevel@tonic-gate vmem_t * 16917c478bd9Sstevel@tonic-gate vmem_init(const char *parent_name, size_t parent_quantum, 16927c478bd9Sstevel@tonic-gate vmem_alloc_t *parent_alloc, vmem_free_t *parent_free, 16937c478bd9Sstevel@tonic-gate const char *heap_name, void *heap_start, size_t heap_size, 16947c478bd9Sstevel@tonic-gate size_t heap_quantum, vmem_alloc_t *heap_alloc, vmem_free_t *heap_free) 16957c478bd9Sstevel@tonic-gate { 16967c478bd9Sstevel@tonic-gate uint32_t id; 16977c478bd9Sstevel@tonic-gate int nseg = VMEM_SEG_INITIAL; 16987c478bd9Sstevel@tonic-gate vmem_t *parent, *heap; 16997c478bd9Sstevel@tonic-gate 17007c478bd9Sstevel@tonic-gate ASSERT(vmem_internal_arena == NULL); 17017c478bd9Sstevel@tonic-gate 17027c478bd9Sstevel@tonic-gate while (--nseg >= 0) 17037c478bd9Sstevel@tonic-gate vmem_putseg_global(&vmem_seg0[nseg]); 17047c478bd9Sstevel@tonic-gate 17057c478bd9Sstevel@tonic-gate if (parent_name != NULL) { 17067c478bd9Sstevel@tonic-gate parent = vmem_create(parent_name, 17077c478bd9Sstevel@tonic-gate heap_start, heap_size, parent_quantum, 17087c478bd9Sstevel@tonic-gate NULL, NULL, NULL, 0, 17097c478bd9Sstevel@tonic-gate VM_SLEEP | VMC_POPULATOR); 17107c478bd9Sstevel@tonic-gate heap_start = NULL; 17117c478bd9Sstevel@tonic-gate heap_size = 0; 17127c478bd9Sstevel@tonic-gate } else { 17137c478bd9Sstevel@tonic-gate ASSERT(parent_alloc == NULL && parent_free == NULL); 17147c478bd9Sstevel@tonic-gate parent = NULL; 17157c478bd9Sstevel@tonic-gate } 17167c478bd9Sstevel@tonic-gate 17177c478bd9Sstevel@tonic-gate heap = vmem_create(heap_name, 17187c478bd9Sstevel@tonic-gate heap_start, heap_size, heap_quantum, 17197c478bd9Sstevel@tonic-gate parent_alloc, parent_free, parent, 0, 17207c478bd9Sstevel@tonic-gate VM_SLEEP | VMC_POPULATOR); 17217c478bd9Sstevel@tonic-gate 17227c478bd9Sstevel@tonic-gate vmem_heap = heap; 17237c478bd9Sstevel@tonic-gate vmem_heap_alloc = heap_alloc; 17247c478bd9Sstevel@tonic-gate vmem_heap_free = heap_free; 17257c478bd9Sstevel@tonic-gate 17267c478bd9Sstevel@tonic-gate vmem_internal_arena = vmem_create("vmem_internal", 17277c478bd9Sstevel@tonic-gate NULL, 0, heap_quantum, 17287c478bd9Sstevel@tonic-gate heap_alloc, heap_free, heap, 0, 17297c478bd9Sstevel@tonic-gate VM_SLEEP | VMC_POPULATOR); 17307c478bd9Sstevel@tonic-gate 17317c478bd9Sstevel@tonic-gate vmem_seg_arena = vmem_create("vmem_seg", 17327c478bd9Sstevel@tonic-gate NULL, 0, heap_quantum, 17337c478bd9Sstevel@tonic-gate vmem_alloc, vmem_free, vmem_internal_arena, 0, 17347c478bd9Sstevel@tonic-gate VM_SLEEP | VMC_POPULATOR); 17357c478bd9Sstevel@tonic-gate 17367c478bd9Sstevel@tonic-gate vmem_hash_arena = vmem_create("vmem_hash", 17377c478bd9Sstevel@tonic-gate NULL, 0, 8, 17387c478bd9Sstevel@tonic-gate vmem_alloc, vmem_free, vmem_internal_arena, 0, 17397c478bd9Sstevel@tonic-gate VM_SLEEP); 17407c478bd9Sstevel@tonic-gate 17417c478bd9Sstevel@tonic-gate vmem_vmem_arena = vmem_create("vmem_vmem", 17427c478bd9Sstevel@tonic-gate vmem0, sizeof (vmem0), 1, 17437c478bd9Sstevel@tonic-gate vmem_alloc, vmem_free, vmem_internal_arena, 0, 17447c478bd9Sstevel@tonic-gate VM_SLEEP); 17457c478bd9Sstevel@tonic-gate 17467c478bd9Sstevel@tonic-gate for (id = 0; id < vmem_id; id++) 17477c478bd9Sstevel@tonic-gate (void) vmem_xalloc(vmem_vmem_arena, sizeof (vmem_t), 17487c478bd9Sstevel@tonic-gate 1, 0, 0, &vmem0[id], &vmem0[id + 1], 17497c478bd9Sstevel@tonic-gate VM_NOSLEEP | VM_BESTFIT | VM_PANIC); 17507c478bd9Sstevel@tonic-gate 17517c478bd9Sstevel@tonic-gate return (heap); 17527c478bd9Sstevel@tonic-gate } 17537c478bd9Sstevel@tonic-gate 17547c478bd9Sstevel@tonic-gate void 17557c478bd9Sstevel@tonic-gate vmem_no_debug(void) 17567c478bd9Sstevel@tonic-gate { 17577c478bd9Sstevel@tonic-gate /* 17587c478bd9Sstevel@tonic-gate * This size must be a multiple of the minimum required alignment, 17597c478bd9Sstevel@tonic-gate * since vmem_populate allocates them compactly. 17607c478bd9Sstevel@tonic-gate */ 17617c478bd9Sstevel@tonic-gate vmem_seg_size = P2ROUNDUP(offsetof(vmem_seg_t, vs_thread), 17627c478bd9Sstevel@tonic-gate sizeof (hrtime_t)); 17637c478bd9Sstevel@tonic-gate } 17647c478bd9Sstevel@tonic-gate 17657c478bd9Sstevel@tonic-gate /* 17667c478bd9Sstevel@tonic-gate * Lockup and release, for fork1(2) handling. 17677c478bd9Sstevel@tonic-gate */ 17687c478bd9Sstevel@tonic-gate void 17697c478bd9Sstevel@tonic-gate vmem_lockup(void) 17707c478bd9Sstevel@tonic-gate { 17717c478bd9Sstevel@tonic-gate vmem_t *cur; 17727c478bd9Sstevel@tonic-gate 17737c478bd9Sstevel@tonic-gate (void) mutex_lock(&vmem_list_lock); 17747c478bd9Sstevel@tonic-gate (void) mutex_lock(&vmem_nosleep_lock.vmpl_mutex); 17757c478bd9Sstevel@tonic-gate 17767c478bd9Sstevel@tonic-gate /* 17777c478bd9Sstevel@tonic-gate * Lock up and broadcast all arenas. 17787c478bd9Sstevel@tonic-gate */ 17797c478bd9Sstevel@tonic-gate for (cur = vmem_list; cur != NULL; cur = cur->vm_next) { 17807c478bd9Sstevel@tonic-gate (void) mutex_lock(&cur->vm_lock); 17817c478bd9Sstevel@tonic-gate (void) cond_broadcast(&cur->vm_cv); 17827c478bd9Sstevel@tonic-gate } 17837c478bd9Sstevel@tonic-gate 17847c478bd9Sstevel@tonic-gate (void) mutex_lock(&vmem_segfree_lock); 17857c478bd9Sstevel@tonic-gate } 17867c478bd9Sstevel@tonic-gate 17877c478bd9Sstevel@tonic-gate void 17887c478bd9Sstevel@tonic-gate vmem_release(void) 17897c478bd9Sstevel@tonic-gate { 17907c478bd9Sstevel@tonic-gate vmem_t *cur; 17917c478bd9Sstevel@tonic-gate 17927c478bd9Sstevel@tonic-gate (void) mutex_unlock(&vmem_nosleep_lock.vmpl_mutex); 17937c478bd9Sstevel@tonic-gate 17947c478bd9Sstevel@tonic-gate for (cur = vmem_list; cur != NULL; cur = cur->vm_next) 17957c478bd9Sstevel@tonic-gate (void) mutex_unlock(&cur->vm_lock); 17967c478bd9Sstevel@tonic-gate 17977c478bd9Sstevel@tonic-gate (void) mutex_unlock(&vmem_segfree_lock); 17987c478bd9Sstevel@tonic-gate (void) mutex_unlock(&vmem_list_lock); 17997c478bd9Sstevel@tonic-gate } 1800