10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
54952Stomee * Common Development and Distribution License (the "License").
64952Stomee * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
211219Sraf
220Sstevel@tonic-gate /*
235891Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate * For a more complete description of the main ideas, see:
290Sstevel@tonic-gate *
300Sstevel@tonic-gate * Jeff Bonwick and Jonathan Adams,
310Sstevel@tonic-gate *
320Sstevel@tonic-gate * Magazines and vmem: Extending the Slab Allocator to Many CPUs and
330Sstevel@tonic-gate * Arbitrary Resources.
340Sstevel@tonic-gate *
350Sstevel@tonic-gate * Proceedings of the 2001 Usenix Conference.
360Sstevel@tonic-gate * Available as /shared/sac/PSARC/2000/550/materials/vmem.pdf.
370Sstevel@tonic-gate *
384952Stomee * For the "Big Theory Statement", see usr/src/uts/common/os/vmem.c
390Sstevel@tonic-gate *
400Sstevel@tonic-gate * 1. Overview of changes
410Sstevel@tonic-gate * ------------------------------
420Sstevel@tonic-gate * There have been a few changes to vmem in order to support umem. The
430Sstevel@tonic-gate * main areas are:
440Sstevel@tonic-gate *
450Sstevel@tonic-gate * * VM_SLEEP unsupported
460Sstevel@tonic-gate *
470Sstevel@tonic-gate * * Reaping changes
480Sstevel@tonic-gate *
490Sstevel@tonic-gate * * initialization changes
500Sstevel@tonic-gate *
510Sstevel@tonic-gate * * _vmem_extend_alloc
520Sstevel@tonic-gate *
530Sstevel@tonic-gate *
540Sstevel@tonic-gate * 2. VM_SLEEP Removed
550Sstevel@tonic-gate * -------------------
560Sstevel@tonic-gate * Since VM_SLEEP allocations can hold locks (in vmem_populate()) for
570Sstevel@tonic-gate * possibly infinite amounts of time, they are not supported in this
580Sstevel@tonic-gate * version of vmem. Sleep-like behavior can be achieved through
590Sstevel@tonic-gate * UMEM_NOFAIL umem allocations.
600Sstevel@tonic-gate *
610Sstevel@tonic-gate *
620Sstevel@tonic-gate * 3. Reaping changes
630Sstevel@tonic-gate * ------------------
640Sstevel@tonic-gate * Unlike kmem_reap(), which just asynchronously schedules work, umem_reap()
650Sstevel@tonic-gate * can do allocations and frees synchronously. This is a problem if it
660Sstevel@tonic-gate * occurs during a vmem_populate() allocation.
670Sstevel@tonic-gate *
680Sstevel@tonic-gate * Instead, we delay reaps while populates are active.
690Sstevel@tonic-gate *
700Sstevel@tonic-gate *
710Sstevel@tonic-gate * 4. Initialization changes
720Sstevel@tonic-gate * -------------------------
730Sstevel@tonic-gate * In the kernel, vmem_init() allows you to create a single, top-level arena,
740Sstevel@tonic-gate * which has vmem_internal_arena as a child. For umem, we want to be able
750Sstevel@tonic-gate * to extend arenas dynamically. It is much easier to support this if we
760Sstevel@tonic-gate * allow a two-level "heap" arena:
770Sstevel@tonic-gate *
780Sstevel@tonic-gate * +----------+
790Sstevel@tonic-gate * | "fake" |
800Sstevel@tonic-gate * +----------+
810Sstevel@tonic-gate * |
820Sstevel@tonic-gate * +----------+
830Sstevel@tonic-gate * | "heap" |
840Sstevel@tonic-gate * +----------+
850Sstevel@tonic-gate * | \ \
860Sstevel@tonic-gate * | +-+-- ... <other children>
870Sstevel@tonic-gate * |
880Sstevel@tonic-gate * +---------------+
890Sstevel@tonic-gate * | vmem_internal |
900Sstevel@tonic-gate * +---------------+
910Sstevel@tonic-gate * | | | |
920Sstevel@tonic-gate * <children>
930Sstevel@tonic-gate *
940Sstevel@tonic-gate * The new vmem_init() allows you to specify a "parent" of the heap, along
950Sstevel@tonic-gate * with allocation functions.
960Sstevel@tonic-gate *
970Sstevel@tonic-gate *
980Sstevel@tonic-gate * 5. _vmem_extend_alloc
990Sstevel@tonic-gate * ---------------------
1000Sstevel@tonic-gate * The other part of extending is _vmem_extend_alloc. This function allows
1010Sstevel@tonic-gate * you to extend (expand current spans, if possible) an arena and allocate
1020Sstevel@tonic-gate * a chunk of the newly extened span atomically. This is needed to support
1030Sstevel@tonic-gate * extending the heap while vmem_populate()ing it.
1040Sstevel@tonic-gate *
1050Sstevel@tonic-gate * In order to increase the usefulness of extending, non-imported spans are
1060Sstevel@tonic-gate * sorted in address order.
1070Sstevel@tonic-gate */
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate #include <sys/vmem_impl_user.h>
1100Sstevel@tonic-gate #include <alloca.h>
1110Sstevel@tonic-gate #include <sys/sysmacros.h>
1120Sstevel@tonic-gate #include <stdio.h>
1130Sstevel@tonic-gate #include <strings.h>
1140Sstevel@tonic-gate #include <atomic.h>
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate #include "vmem_base.h"
1170Sstevel@tonic-gate #include "umem_base.h"
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate #define VMEM_INITIAL 6 /* early vmem arenas */
1200Sstevel@tonic-gate #define VMEM_SEG_INITIAL 100 /* early segments */
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate /*
1230Sstevel@tonic-gate * Adding a new span to an arena requires two segment structures: one to
1240Sstevel@tonic-gate * represent the span, and one to represent the free segment it contains.
1250Sstevel@tonic-gate */
1260Sstevel@tonic-gate #define VMEM_SEGS_PER_SPAN_CREATE 2
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate /*
1290Sstevel@tonic-gate * Allocating a piece of an existing segment requires 0-2 segment structures
1300Sstevel@tonic-gate * depending on how much of the segment we're allocating.
1310Sstevel@tonic-gate *
1320Sstevel@tonic-gate * To allocate the entire segment, no new segment structures are needed; we
1330Sstevel@tonic-gate * simply move the existing segment structure from the freelist to the
1340Sstevel@tonic-gate * allocation hash table.
1350Sstevel@tonic-gate *
1360Sstevel@tonic-gate * To allocate a piece from the left or right end of the segment, we must
1370Sstevel@tonic-gate * split the segment into two pieces (allocated part and remainder), so we
1380Sstevel@tonic-gate * need one new segment structure to represent the remainder.
1390Sstevel@tonic-gate *
1400Sstevel@tonic-gate * To allocate from the middle of a segment, we need two new segment strucures
1410Sstevel@tonic-gate * to represent the remainders on either side of the allocated part.
1420Sstevel@tonic-gate */
1430Sstevel@tonic-gate #define VMEM_SEGS_PER_EXACT_ALLOC 0
1440Sstevel@tonic-gate #define VMEM_SEGS_PER_LEFT_ALLOC 1
1450Sstevel@tonic-gate #define VMEM_SEGS_PER_RIGHT_ALLOC 1
1460Sstevel@tonic-gate #define VMEM_SEGS_PER_MIDDLE_ALLOC 2
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate /*
1490Sstevel@tonic-gate * vmem_populate() preallocates segment structures for vmem to do its work.
1500Sstevel@tonic-gate * It must preallocate enough for the worst case, which is when we must import
1510Sstevel@tonic-gate * a new span and then allocate from the middle of it.
1520Sstevel@tonic-gate */
1530Sstevel@tonic-gate #define VMEM_SEGS_PER_ALLOC_MAX \
1540Sstevel@tonic-gate (VMEM_SEGS_PER_SPAN_CREATE + VMEM_SEGS_PER_MIDDLE_ALLOC)
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate /*
1570Sstevel@tonic-gate * The segment structures themselves are allocated from vmem_seg_arena, so
1580Sstevel@tonic-gate * we have a recursion problem when vmem_seg_arena needs to populate itself.
1590Sstevel@tonic-gate * We address this by working out the maximum number of segment structures
1600Sstevel@tonic-gate * this act will require, and multiplying by the maximum number of threads
1610Sstevel@tonic-gate * that we'll allow to do it simultaneously.
1620Sstevel@tonic-gate *
1630Sstevel@tonic-gate * The worst-case segment consumption to populate vmem_seg_arena is as
1640Sstevel@tonic-gate * follows (depicted as a stack trace to indicate why events are occurring):
1650Sstevel@tonic-gate *
1660Sstevel@tonic-gate * vmem_alloc(vmem_seg_arena) -> 2 segs (span create + exact alloc)
1670Sstevel@tonic-gate * vmem_alloc(vmem_internal_arena) -> 2 segs (span create + exact alloc)
1680Sstevel@tonic-gate * heap_alloc(heap_arena)
1690Sstevel@tonic-gate * vmem_alloc(heap_arena) -> 4 seg (span create + alloc)
1700Sstevel@tonic-gate * parent_alloc(parent_arena)
1710Sstevel@tonic-gate * _vmem_extend_alloc(parent_arena) -> 3 seg (span create + left alloc)
1720Sstevel@tonic-gate *
1730Sstevel@tonic-gate * Note: The reservation for heap_arena must be 4, since vmem_xalloc()
1740Sstevel@tonic-gate * is overly pessimistic on allocations where parent_arena has a stricter
1750Sstevel@tonic-gate * alignment than heap_arena.
1760Sstevel@tonic-gate *
1770Sstevel@tonic-gate * The worst-case consumption for any arena is 4 segment structures.
1780Sstevel@tonic-gate * For now, we only support VM_NOSLEEP allocations, so as long as we
1790Sstevel@tonic-gate * serialize all vmem_populates, a 4-seg reserve is sufficient.
1800Sstevel@tonic-gate */
1810Sstevel@tonic-gate #define VMEM_POPULATE_SEGS_PER_ARENA 4
1820Sstevel@tonic-gate #define VMEM_POPULATE_LOCKS 1
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate #define VMEM_POPULATE_RESERVE \
1850Sstevel@tonic-gate (VMEM_POPULATE_SEGS_PER_ARENA * VMEM_POPULATE_LOCKS)
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate /*
1880Sstevel@tonic-gate * vmem_populate() ensures that each arena has VMEM_MINFREE seg structures
1890Sstevel@tonic-gate * so that it can satisfy the worst-case allocation *and* participate in
1900Sstevel@tonic-gate * worst-case allocation from vmem_seg_arena.
1910Sstevel@tonic-gate */
1920Sstevel@tonic-gate #define VMEM_MINFREE (VMEM_POPULATE_RESERVE + VMEM_SEGS_PER_ALLOC_MAX)
1930Sstevel@tonic-gate
1940Sstevel@tonic-gate /* Don't assume new statics are zeroed - see vmem_startup() */
1950Sstevel@tonic-gate static vmem_t vmem0[VMEM_INITIAL];
1960Sstevel@tonic-gate static vmem_t *vmem_populator[VMEM_INITIAL];
1970Sstevel@tonic-gate static uint32_t vmem_id;
1980Sstevel@tonic-gate static uint32_t vmem_populators;
1990Sstevel@tonic-gate static vmem_seg_t vmem_seg0[VMEM_SEG_INITIAL];
2000Sstevel@tonic-gate static vmem_seg_t *vmem_segfree;
2010Sstevel@tonic-gate static mutex_t vmem_list_lock;
2020Sstevel@tonic-gate static mutex_t vmem_segfree_lock;
2030Sstevel@tonic-gate static vmem_populate_lock_t vmem_nosleep_lock;
2040Sstevel@tonic-gate #define IN_POPULATE() (vmem_nosleep_lock.vmpl_thr == thr_self())
2050Sstevel@tonic-gate static vmem_t *vmem_list;
2060Sstevel@tonic-gate static vmem_t *vmem_internal_arena;
2070Sstevel@tonic-gate static vmem_t *vmem_seg_arena;
2080Sstevel@tonic-gate static vmem_t *vmem_hash_arena;
2090Sstevel@tonic-gate static vmem_t *vmem_vmem_arena;
2100Sstevel@tonic-gate
2110Sstevel@tonic-gate vmem_t *vmem_heap;
2120Sstevel@tonic-gate vmem_alloc_t *vmem_heap_alloc;
2130Sstevel@tonic-gate vmem_free_t *vmem_heap_free;
2140Sstevel@tonic-gate
2150Sstevel@tonic-gate uint32_t vmem_mtbf; /* mean time between failures [default: off] */
2160Sstevel@tonic-gate size_t vmem_seg_size = sizeof (vmem_seg_t);
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate /*
2190Sstevel@tonic-gate * Insert/delete from arena list (type 'a') or next-of-kin list (type 'k').
2200Sstevel@tonic-gate */
2210Sstevel@tonic-gate #define VMEM_INSERT(vprev, vsp, type) \
2220Sstevel@tonic-gate { \
2230Sstevel@tonic-gate vmem_seg_t *vnext = (vprev)->vs_##type##next; \
2240Sstevel@tonic-gate (vsp)->vs_##type##next = (vnext); \
2250Sstevel@tonic-gate (vsp)->vs_##type##prev = (vprev); \
2260Sstevel@tonic-gate (vprev)->vs_##type##next = (vsp); \
2270Sstevel@tonic-gate (vnext)->vs_##type##prev = (vsp); \
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate
2300Sstevel@tonic-gate #define VMEM_DELETE(vsp, type) \
2310Sstevel@tonic-gate { \
2320Sstevel@tonic-gate vmem_seg_t *vprev = (vsp)->vs_##type##prev; \
2330Sstevel@tonic-gate vmem_seg_t *vnext = (vsp)->vs_##type##next; \
2340Sstevel@tonic-gate (vprev)->vs_##type##next = (vnext); \
2350Sstevel@tonic-gate (vnext)->vs_##type##prev = (vprev); \
2360Sstevel@tonic-gate }
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate /*
2390Sstevel@tonic-gate * Get a vmem_seg_t from the global segfree list.
2400Sstevel@tonic-gate */
2410Sstevel@tonic-gate static vmem_seg_t *
vmem_getseg_global(void)2420Sstevel@tonic-gate vmem_getseg_global(void)
2430Sstevel@tonic-gate {
2440Sstevel@tonic-gate vmem_seg_t *vsp;
2450Sstevel@tonic-gate
2460Sstevel@tonic-gate (void) mutex_lock(&vmem_segfree_lock);
2470Sstevel@tonic-gate if ((vsp = vmem_segfree) != NULL)
2480Sstevel@tonic-gate vmem_segfree = vsp->vs_knext;
2490Sstevel@tonic-gate (void) mutex_unlock(&vmem_segfree_lock);
2500Sstevel@tonic-gate
2510Sstevel@tonic-gate return (vsp);
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate /*
2550Sstevel@tonic-gate * Put a vmem_seg_t on the global segfree list.
2560Sstevel@tonic-gate */
2570Sstevel@tonic-gate static void
vmem_putseg_global(vmem_seg_t * vsp)2580Sstevel@tonic-gate vmem_putseg_global(vmem_seg_t *vsp)
2590Sstevel@tonic-gate {
2600Sstevel@tonic-gate (void) mutex_lock(&vmem_segfree_lock);
2610Sstevel@tonic-gate vsp->vs_knext = vmem_segfree;
2620Sstevel@tonic-gate vmem_segfree = vsp;
2630Sstevel@tonic-gate (void) mutex_unlock(&vmem_segfree_lock);
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate
2660Sstevel@tonic-gate /*
2670Sstevel@tonic-gate * Get a vmem_seg_t from vmp's segfree list.
2680Sstevel@tonic-gate */
2690Sstevel@tonic-gate static vmem_seg_t *
vmem_getseg(vmem_t * vmp)2700Sstevel@tonic-gate vmem_getseg(vmem_t *vmp)
2710Sstevel@tonic-gate {
2720Sstevel@tonic-gate vmem_seg_t *vsp;
2730Sstevel@tonic-gate
2740Sstevel@tonic-gate ASSERT(vmp->vm_nsegfree > 0);
2750Sstevel@tonic-gate
2760Sstevel@tonic-gate vsp = vmp->vm_segfree;
2770Sstevel@tonic-gate vmp->vm_segfree = vsp->vs_knext;
2780Sstevel@tonic-gate vmp->vm_nsegfree--;
2790Sstevel@tonic-gate
2800Sstevel@tonic-gate return (vsp);
2810Sstevel@tonic-gate }
2820Sstevel@tonic-gate
2830Sstevel@tonic-gate /*
2840Sstevel@tonic-gate * Put a vmem_seg_t on vmp's segfree list.
2850Sstevel@tonic-gate */
2860Sstevel@tonic-gate static void
vmem_putseg(vmem_t * vmp,vmem_seg_t * vsp)2870Sstevel@tonic-gate vmem_putseg(vmem_t *vmp, vmem_seg_t *vsp)
2880Sstevel@tonic-gate {
2890Sstevel@tonic-gate vsp->vs_knext = vmp->vm_segfree;
2900Sstevel@tonic-gate vmp->vm_segfree = vsp;
2910Sstevel@tonic-gate vmp->vm_nsegfree++;
2920Sstevel@tonic-gate }
2930Sstevel@tonic-gate
2940Sstevel@tonic-gate /*
2950Sstevel@tonic-gate * Add vsp to the appropriate freelist.
2960Sstevel@tonic-gate */
2970Sstevel@tonic-gate static void
vmem_freelist_insert(vmem_t * vmp,vmem_seg_t * vsp)2980Sstevel@tonic-gate vmem_freelist_insert(vmem_t *vmp, vmem_seg_t *vsp)
2990Sstevel@tonic-gate {
3000Sstevel@tonic-gate vmem_seg_t *vprev;
3010Sstevel@tonic-gate
3020Sstevel@tonic-gate ASSERT(*VMEM_HASH(vmp, vsp->vs_start) != vsp);
3030Sstevel@tonic-gate
3040Sstevel@tonic-gate vprev = (vmem_seg_t *)&vmp->vm_freelist[highbit(VS_SIZE(vsp)) - 1];
3050Sstevel@tonic-gate vsp->vs_type = VMEM_FREE;
3060Sstevel@tonic-gate vmp->vm_freemap |= VS_SIZE(vprev);
3070Sstevel@tonic-gate VMEM_INSERT(vprev, vsp, k);
3080Sstevel@tonic-gate
3090Sstevel@tonic-gate (void) cond_broadcast(&vmp->vm_cv);
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate
3120Sstevel@tonic-gate /*
3130Sstevel@tonic-gate * Take vsp from the freelist.
3140Sstevel@tonic-gate */
3150Sstevel@tonic-gate static void
vmem_freelist_delete(vmem_t * vmp,vmem_seg_t * vsp)3160Sstevel@tonic-gate vmem_freelist_delete(vmem_t *vmp, vmem_seg_t *vsp)
3170Sstevel@tonic-gate {
3180Sstevel@tonic-gate ASSERT(*VMEM_HASH(vmp, vsp->vs_start) != vsp);
3190Sstevel@tonic-gate ASSERT(vsp->vs_type == VMEM_FREE);
3200Sstevel@tonic-gate
3210Sstevel@tonic-gate if (vsp->vs_knext->vs_start == 0 && vsp->vs_kprev->vs_start == 0) {
3220Sstevel@tonic-gate /*
3230Sstevel@tonic-gate * The segments on both sides of 'vsp' are freelist heads,
3240Sstevel@tonic-gate * so taking vsp leaves the freelist at vsp->vs_kprev empty.
3250Sstevel@tonic-gate */
3260Sstevel@tonic-gate ASSERT(vmp->vm_freemap & VS_SIZE(vsp->vs_kprev));
3270Sstevel@tonic-gate vmp->vm_freemap ^= VS_SIZE(vsp->vs_kprev);
3280Sstevel@tonic-gate }
3290Sstevel@tonic-gate VMEM_DELETE(vsp, k);
3300Sstevel@tonic-gate }
3310Sstevel@tonic-gate
3320Sstevel@tonic-gate /*
3330Sstevel@tonic-gate * Add vsp to the allocated-segment hash table and update kstats.
3340Sstevel@tonic-gate */
3350Sstevel@tonic-gate static void
vmem_hash_insert(vmem_t * vmp,vmem_seg_t * vsp)3360Sstevel@tonic-gate vmem_hash_insert(vmem_t *vmp, vmem_seg_t *vsp)
3370Sstevel@tonic-gate {
3380Sstevel@tonic-gate vmem_seg_t **bucket;
3390Sstevel@tonic-gate
3400Sstevel@tonic-gate vsp->vs_type = VMEM_ALLOC;
3410Sstevel@tonic-gate bucket = VMEM_HASH(vmp, vsp->vs_start);
3420Sstevel@tonic-gate vsp->vs_knext = *bucket;
3430Sstevel@tonic-gate *bucket = vsp;
3440Sstevel@tonic-gate
3450Sstevel@tonic-gate if (vmem_seg_size == sizeof (vmem_seg_t)) {
3460Sstevel@tonic-gate vsp->vs_depth = (uint8_t)getpcstack(vsp->vs_stack,
3470Sstevel@tonic-gate VMEM_STACK_DEPTH, 0);
3480Sstevel@tonic-gate vsp->vs_thread = thr_self();
3490Sstevel@tonic-gate vsp->vs_timestamp = gethrtime();
3500Sstevel@tonic-gate } else {
3510Sstevel@tonic-gate vsp->vs_depth = 0;
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate
3540Sstevel@tonic-gate vmp->vm_kstat.vk_alloc++;
3550Sstevel@tonic-gate vmp->vm_kstat.vk_mem_inuse += VS_SIZE(vsp);
3560Sstevel@tonic-gate }
3570Sstevel@tonic-gate
3580Sstevel@tonic-gate /*
3590Sstevel@tonic-gate * Remove vsp from the allocated-segment hash table and update kstats.
3600Sstevel@tonic-gate */
3610Sstevel@tonic-gate static vmem_seg_t *
vmem_hash_delete(vmem_t * vmp,uintptr_t addr,size_t size)3620Sstevel@tonic-gate vmem_hash_delete(vmem_t *vmp, uintptr_t addr, size_t size)
3630Sstevel@tonic-gate {
3640Sstevel@tonic-gate vmem_seg_t *vsp, **prev_vspp;
3650Sstevel@tonic-gate
3660Sstevel@tonic-gate prev_vspp = VMEM_HASH(vmp, addr);
3670Sstevel@tonic-gate while ((vsp = *prev_vspp) != NULL) {
3680Sstevel@tonic-gate if (vsp->vs_start == addr) {
3690Sstevel@tonic-gate *prev_vspp = vsp->vs_knext;
3700Sstevel@tonic-gate break;
3710Sstevel@tonic-gate }
3720Sstevel@tonic-gate vmp->vm_kstat.vk_lookup++;
3730Sstevel@tonic-gate prev_vspp = &vsp->vs_knext;
3740Sstevel@tonic-gate }
3750Sstevel@tonic-gate
3760Sstevel@tonic-gate if (vsp == NULL) {
3770Sstevel@tonic-gate umem_panic("vmem_hash_delete(%p, %lx, %lu): bad free",
3780Sstevel@tonic-gate vmp, addr, size);
3790Sstevel@tonic-gate }
3800Sstevel@tonic-gate if (VS_SIZE(vsp) != size) {
3810Sstevel@tonic-gate umem_panic("vmem_hash_delete(%p, %lx, %lu): wrong size "
3820Sstevel@tonic-gate "(expect %lu)", vmp, addr, size, VS_SIZE(vsp));
3830Sstevel@tonic-gate }
3840Sstevel@tonic-gate
3850Sstevel@tonic-gate vmp->vm_kstat.vk_free++;
3860Sstevel@tonic-gate vmp->vm_kstat.vk_mem_inuse -= size;
3870Sstevel@tonic-gate
3880Sstevel@tonic-gate return (vsp);
3890Sstevel@tonic-gate }
3900Sstevel@tonic-gate
3910Sstevel@tonic-gate /*
3920Sstevel@tonic-gate * Create a segment spanning the range [start, end) and add it to the arena.
3930Sstevel@tonic-gate */
3940Sstevel@tonic-gate static vmem_seg_t *
vmem_seg_create(vmem_t * vmp,vmem_seg_t * vprev,uintptr_t start,uintptr_t end)3950Sstevel@tonic-gate vmem_seg_create(vmem_t *vmp, vmem_seg_t *vprev, uintptr_t start, uintptr_t end)
3960Sstevel@tonic-gate {
3970Sstevel@tonic-gate vmem_seg_t *newseg = vmem_getseg(vmp);
3980Sstevel@tonic-gate
3990Sstevel@tonic-gate newseg->vs_start = start;
4000Sstevel@tonic-gate newseg->vs_end = end;
4010Sstevel@tonic-gate newseg->vs_type = 0;
4020Sstevel@tonic-gate newseg->vs_import = 0;
4030Sstevel@tonic-gate
4040Sstevel@tonic-gate VMEM_INSERT(vprev, newseg, a);
4050Sstevel@tonic-gate
4060Sstevel@tonic-gate return (newseg);
4070Sstevel@tonic-gate }
4080Sstevel@tonic-gate
4090Sstevel@tonic-gate /*
4100Sstevel@tonic-gate * Remove segment vsp from the arena.
4110Sstevel@tonic-gate */
4120Sstevel@tonic-gate static void
vmem_seg_destroy(vmem_t * vmp,vmem_seg_t * vsp)4130Sstevel@tonic-gate vmem_seg_destroy(vmem_t *vmp, vmem_seg_t *vsp)
4140Sstevel@tonic-gate {
4150Sstevel@tonic-gate ASSERT(vsp->vs_type != VMEM_ROTOR);
4160Sstevel@tonic-gate VMEM_DELETE(vsp, a);
4170Sstevel@tonic-gate
4180Sstevel@tonic-gate vmem_putseg(vmp, vsp);
4190Sstevel@tonic-gate }
4200Sstevel@tonic-gate
4210Sstevel@tonic-gate /*
4220Sstevel@tonic-gate * Add the span [vaddr, vaddr + size) to vmp and update kstats.
4230Sstevel@tonic-gate */
4240Sstevel@tonic-gate static vmem_seg_t *
vmem_span_create(vmem_t * vmp,void * vaddr,size_t size,uint8_t import)4250Sstevel@tonic-gate vmem_span_create(vmem_t *vmp, void *vaddr, size_t size, uint8_t import)
4260Sstevel@tonic-gate {
4270Sstevel@tonic-gate vmem_seg_t *knext;
4280Sstevel@tonic-gate vmem_seg_t *newseg, *span;
4290Sstevel@tonic-gate uintptr_t start = (uintptr_t)vaddr;
4300Sstevel@tonic-gate uintptr_t end = start + size;
4310Sstevel@tonic-gate
4320Sstevel@tonic-gate knext = &vmp->vm_seg0;
4330Sstevel@tonic-gate if (!import && vmp->vm_source_alloc == NULL) {
4340Sstevel@tonic-gate vmem_seg_t *kend, *kprev;
4350Sstevel@tonic-gate /*
4360Sstevel@tonic-gate * non-imported spans are sorted in address order. This
4370Sstevel@tonic-gate * makes vmem_extend_unlocked() much more effective.
4380Sstevel@tonic-gate *
4390Sstevel@tonic-gate * We search in reverse order, since new spans are
4400Sstevel@tonic-gate * generally at higher addresses.
4410Sstevel@tonic-gate */
4420Sstevel@tonic-gate kend = &vmp->vm_seg0;
4430Sstevel@tonic-gate for (kprev = kend->vs_kprev; kprev != kend;
4440Sstevel@tonic-gate kprev = kprev->vs_kprev) {
4450Sstevel@tonic-gate if (!kprev->vs_import && (kprev->vs_end - 1) < start)
4460Sstevel@tonic-gate break;
4470Sstevel@tonic-gate }
4480Sstevel@tonic-gate knext = kprev->vs_knext;
4490Sstevel@tonic-gate }
4500Sstevel@tonic-gate
4510Sstevel@tonic-gate ASSERT(MUTEX_HELD(&vmp->vm_lock));
4520Sstevel@tonic-gate
4530Sstevel@tonic-gate if ((start | end) & (vmp->vm_quantum - 1)) {
4540Sstevel@tonic-gate umem_panic("vmem_span_create(%p, %p, %lu): misaligned",
4550Sstevel@tonic-gate vmp, vaddr, size);
4560Sstevel@tonic-gate }
4570Sstevel@tonic-gate
4580Sstevel@tonic-gate span = vmem_seg_create(vmp, knext->vs_aprev, start, end);
4590Sstevel@tonic-gate span->vs_type = VMEM_SPAN;
4600Sstevel@tonic-gate VMEM_INSERT(knext->vs_kprev, span, k);
4610Sstevel@tonic-gate
4620Sstevel@tonic-gate newseg = vmem_seg_create(vmp, span, start, end);
4630Sstevel@tonic-gate vmem_freelist_insert(vmp, newseg);
4640Sstevel@tonic-gate
4650Sstevel@tonic-gate newseg->vs_import = import;
4660Sstevel@tonic-gate if (import)
4670Sstevel@tonic-gate vmp->vm_kstat.vk_mem_import += size;
4680Sstevel@tonic-gate vmp->vm_kstat.vk_mem_total += size;
4690Sstevel@tonic-gate
4700Sstevel@tonic-gate return (newseg);
4710Sstevel@tonic-gate }
4720Sstevel@tonic-gate
4730Sstevel@tonic-gate /*
4740Sstevel@tonic-gate * Remove span vsp from vmp and update kstats.
4750Sstevel@tonic-gate */
4760Sstevel@tonic-gate static void
vmem_span_destroy(vmem_t * vmp,vmem_seg_t * vsp)4770Sstevel@tonic-gate vmem_span_destroy(vmem_t *vmp, vmem_seg_t *vsp)
4780Sstevel@tonic-gate {
4790Sstevel@tonic-gate vmem_seg_t *span = vsp->vs_aprev;
4800Sstevel@tonic-gate size_t size = VS_SIZE(vsp);
4810Sstevel@tonic-gate
4820Sstevel@tonic-gate ASSERT(MUTEX_HELD(&vmp->vm_lock));
4830Sstevel@tonic-gate ASSERT(span->vs_type == VMEM_SPAN);
4840Sstevel@tonic-gate
4850Sstevel@tonic-gate if (vsp->vs_import)
4860Sstevel@tonic-gate vmp->vm_kstat.vk_mem_import -= size;
4870Sstevel@tonic-gate vmp->vm_kstat.vk_mem_total -= size;
4880Sstevel@tonic-gate
4890Sstevel@tonic-gate VMEM_DELETE(span, k);
4900Sstevel@tonic-gate
4910Sstevel@tonic-gate vmem_seg_destroy(vmp, vsp);
4920Sstevel@tonic-gate vmem_seg_destroy(vmp, span);
4930Sstevel@tonic-gate }
4940Sstevel@tonic-gate
4950Sstevel@tonic-gate /*
4960Sstevel@tonic-gate * Allocate the subrange [addr, addr + size) from segment vsp.
4970Sstevel@tonic-gate * If there are leftovers on either side, place them on the freelist.
4980Sstevel@tonic-gate * Returns a pointer to the segment representing [addr, addr + size).
4990Sstevel@tonic-gate */
5000Sstevel@tonic-gate static vmem_seg_t *
vmem_seg_alloc(vmem_t * vmp,vmem_seg_t * vsp,uintptr_t addr,size_t size)5010Sstevel@tonic-gate vmem_seg_alloc(vmem_t *vmp, vmem_seg_t *vsp, uintptr_t addr, size_t size)
5020Sstevel@tonic-gate {
5030Sstevel@tonic-gate uintptr_t vs_start = vsp->vs_start;
5040Sstevel@tonic-gate uintptr_t vs_end = vsp->vs_end;
5050Sstevel@tonic-gate size_t vs_size = vs_end - vs_start;
5060Sstevel@tonic-gate size_t realsize = P2ROUNDUP(size, vmp->vm_quantum);
5070Sstevel@tonic-gate uintptr_t addr_end = addr + realsize;
5080Sstevel@tonic-gate
5090Sstevel@tonic-gate ASSERT(P2PHASE(vs_start, vmp->vm_quantum) == 0);
5100Sstevel@tonic-gate ASSERT(P2PHASE(addr, vmp->vm_quantum) == 0);
5110Sstevel@tonic-gate ASSERT(vsp->vs_type == VMEM_FREE);
5120Sstevel@tonic-gate ASSERT(addr >= vs_start && addr_end - 1 <= vs_end - 1);
5130Sstevel@tonic-gate ASSERT(addr - 1 <= addr_end - 1);
5140Sstevel@tonic-gate
5150Sstevel@tonic-gate /*
5160Sstevel@tonic-gate * If we're allocating from the start of the segment, and the
5170Sstevel@tonic-gate * remainder will be on the same freelist, we can save quite
5180Sstevel@tonic-gate * a bit of work.
5190Sstevel@tonic-gate */
5200Sstevel@tonic-gate if (P2SAMEHIGHBIT(vs_size, vs_size - realsize) && addr == vs_start) {
5210Sstevel@tonic-gate ASSERT(highbit(vs_size) == highbit(vs_size - realsize));
5220Sstevel@tonic-gate vsp->vs_start = addr_end;
5230Sstevel@tonic-gate vsp = vmem_seg_create(vmp, vsp->vs_aprev, addr, addr + size);
5240Sstevel@tonic-gate vmem_hash_insert(vmp, vsp);
5250Sstevel@tonic-gate return (vsp);
5260Sstevel@tonic-gate }
5270Sstevel@tonic-gate
5280Sstevel@tonic-gate vmem_freelist_delete(vmp, vsp);
5290Sstevel@tonic-gate
5300Sstevel@tonic-gate if (vs_end != addr_end)
5310Sstevel@tonic-gate vmem_freelist_insert(vmp,
5320Sstevel@tonic-gate vmem_seg_create(vmp, vsp, addr_end, vs_end));
5330Sstevel@tonic-gate
5340Sstevel@tonic-gate if (vs_start != addr)
5350Sstevel@tonic-gate vmem_freelist_insert(vmp,
5360Sstevel@tonic-gate vmem_seg_create(vmp, vsp->vs_aprev, vs_start, addr));
5370Sstevel@tonic-gate
5380Sstevel@tonic-gate vsp->vs_start = addr;
5390Sstevel@tonic-gate vsp->vs_end = addr + size;
5400Sstevel@tonic-gate
5410Sstevel@tonic-gate vmem_hash_insert(vmp, vsp);
5420Sstevel@tonic-gate return (vsp);
5430Sstevel@tonic-gate }
5440Sstevel@tonic-gate
5450Sstevel@tonic-gate /*
5460Sstevel@tonic-gate * We cannot reap if we are in the middle of a vmem_populate().
5470Sstevel@tonic-gate */
5480Sstevel@tonic-gate void
vmem_reap(void)5490Sstevel@tonic-gate vmem_reap(void)
5500Sstevel@tonic-gate {
5510Sstevel@tonic-gate if (!IN_POPULATE())
5520Sstevel@tonic-gate umem_reap();
5530Sstevel@tonic-gate }
5540Sstevel@tonic-gate
5550Sstevel@tonic-gate /*
5560Sstevel@tonic-gate * Populate vmp's segfree list with VMEM_MINFREE vmem_seg_t structures.
5570Sstevel@tonic-gate */
5580Sstevel@tonic-gate static int
vmem_populate(vmem_t * vmp,int vmflag)5590Sstevel@tonic-gate vmem_populate(vmem_t *vmp, int vmflag)
5600Sstevel@tonic-gate {
5610Sstevel@tonic-gate char *p;
5620Sstevel@tonic-gate vmem_seg_t *vsp;
5630Sstevel@tonic-gate ssize_t nseg;
5640Sstevel@tonic-gate size_t size;
5650Sstevel@tonic-gate vmem_populate_lock_t *lp;
5660Sstevel@tonic-gate int i;
5670Sstevel@tonic-gate
5680Sstevel@tonic-gate while (vmp->vm_nsegfree < VMEM_MINFREE &&
5690Sstevel@tonic-gate (vsp = vmem_getseg_global()) != NULL)
5700Sstevel@tonic-gate vmem_putseg(vmp, vsp);
5710Sstevel@tonic-gate
5720Sstevel@tonic-gate if (vmp->vm_nsegfree >= VMEM_MINFREE)
5730Sstevel@tonic-gate return (1);
5740Sstevel@tonic-gate
5750Sstevel@tonic-gate /*
5760Sstevel@tonic-gate * If we're already populating, tap the reserve.
5770Sstevel@tonic-gate */
5780Sstevel@tonic-gate if (vmem_nosleep_lock.vmpl_thr == thr_self()) {
5790Sstevel@tonic-gate ASSERT(vmp->vm_cflags & VMC_POPULATOR);
5800Sstevel@tonic-gate return (1);
5810Sstevel@tonic-gate }
5820Sstevel@tonic-gate
5830Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock);
5840Sstevel@tonic-gate
5850Sstevel@tonic-gate ASSERT(vmflag & VM_NOSLEEP); /* we do not allow sleep allocations */
5860Sstevel@tonic-gate lp = &vmem_nosleep_lock;
5870Sstevel@tonic-gate
5880Sstevel@tonic-gate /*
5890Sstevel@tonic-gate * Cannot be just a mutex_lock(), since that has no effect if
5900Sstevel@tonic-gate * libthread is not linked.
5910Sstevel@tonic-gate */
5920Sstevel@tonic-gate (void) mutex_lock(&lp->vmpl_mutex);
5930Sstevel@tonic-gate ASSERT(lp->vmpl_thr == 0);
5940Sstevel@tonic-gate lp->vmpl_thr = thr_self();
5950Sstevel@tonic-gate
5960Sstevel@tonic-gate nseg = VMEM_MINFREE + vmem_populators * VMEM_POPULATE_RESERVE;
5970Sstevel@tonic-gate size = P2ROUNDUP(nseg * vmem_seg_size, vmem_seg_arena->vm_quantum);
5980Sstevel@tonic-gate nseg = size / vmem_seg_size;
5990Sstevel@tonic-gate
6000Sstevel@tonic-gate /*
6010Sstevel@tonic-gate * The following vmem_alloc() may need to populate vmem_seg_arena
6020Sstevel@tonic-gate * and all the things it imports from. When doing so, it will tap
6030Sstevel@tonic-gate * each arena's reserve to prevent recursion (see the block comment
6040Sstevel@tonic-gate * above the definition of VMEM_POPULATE_RESERVE).
6050Sstevel@tonic-gate *
6060Sstevel@tonic-gate * During this allocation, vmem_reap() is a no-op. If the allocation
6070Sstevel@tonic-gate * fails, we call vmem_reap() after dropping the population lock.
6080Sstevel@tonic-gate */
6090Sstevel@tonic-gate p = vmem_alloc(vmem_seg_arena, size, vmflag & VM_UMFLAGS);
6100Sstevel@tonic-gate if (p == NULL) {
6110Sstevel@tonic-gate lp->vmpl_thr = 0;
6120Sstevel@tonic-gate (void) mutex_unlock(&lp->vmpl_mutex);
6130Sstevel@tonic-gate vmem_reap();
6140Sstevel@tonic-gate
6150Sstevel@tonic-gate (void) mutex_lock(&vmp->vm_lock);
6160Sstevel@tonic-gate vmp->vm_kstat.vk_populate_fail++;
6170Sstevel@tonic-gate return (0);
6180Sstevel@tonic-gate }
6190Sstevel@tonic-gate /*
6200Sstevel@tonic-gate * Restock the arenas that may have been depleted during population.
6210Sstevel@tonic-gate */
6220Sstevel@tonic-gate for (i = 0; i < vmem_populators; i++) {
6230Sstevel@tonic-gate (void) mutex_lock(&vmem_populator[i]->vm_lock);
6240Sstevel@tonic-gate while (vmem_populator[i]->vm_nsegfree < VMEM_POPULATE_RESERVE)
6250Sstevel@tonic-gate vmem_putseg(vmem_populator[i],
6260Sstevel@tonic-gate (vmem_seg_t *)(p + --nseg * vmem_seg_size));
6270Sstevel@tonic-gate (void) mutex_unlock(&vmem_populator[i]->vm_lock);
6280Sstevel@tonic-gate }
6290Sstevel@tonic-gate
6300Sstevel@tonic-gate lp->vmpl_thr = 0;
6310Sstevel@tonic-gate (void) mutex_unlock(&lp->vmpl_mutex);
6320Sstevel@tonic-gate (void) mutex_lock(&vmp->vm_lock);
6330Sstevel@tonic-gate
6340Sstevel@tonic-gate /*
6350Sstevel@tonic-gate * Now take our own segments.
6360Sstevel@tonic-gate */
6370Sstevel@tonic-gate ASSERT(nseg >= VMEM_MINFREE);
6380Sstevel@tonic-gate while (vmp->vm_nsegfree < VMEM_MINFREE)
6390Sstevel@tonic-gate vmem_putseg(vmp, (vmem_seg_t *)(p + --nseg * vmem_seg_size));
6400Sstevel@tonic-gate
6410Sstevel@tonic-gate /*
6420Sstevel@tonic-gate * Give the remainder to charity.
6430Sstevel@tonic-gate */
6440Sstevel@tonic-gate while (nseg > 0)
6450Sstevel@tonic-gate vmem_putseg_global((vmem_seg_t *)(p + --nseg * vmem_seg_size));
6460Sstevel@tonic-gate
6470Sstevel@tonic-gate return (1);
6480Sstevel@tonic-gate }
6490Sstevel@tonic-gate
6500Sstevel@tonic-gate /*
6510Sstevel@tonic-gate * Advance a walker from its previous position to 'afterme'.
6520Sstevel@tonic-gate * Note: may drop and reacquire vmp->vm_lock.
6530Sstevel@tonic-gate */
6540Sstevel@tonic-gate static void
vmem_advance(vmem_t * vmp,vmem_seg_t * walker,vmem_seg_t * afterme)6550Sstevel@tonic-gate vmem_advance(vmem_t *vmp, vmem_seg_t *walker, vmem_seg_t *afterme)
6560Sstevel@tonic-gate {
6570Sstevel@tonic-gate vmem_seg_t *vprev = walker->vs_aprev;
6580Sstevel@tonic-gate vmem_seg_t *vnext = walker->vs_anext;
6590Sstevel@tonic-gate vmem_seg_t *vsp = NULL;
6600Sstevel@tonic-gate
6610Sstevel@tonic-gate VMEM_DELETE(walker, a);
6620Sstevel@tonic-gate
6630Sstevel@tonic-gate if (afterme != NULL)
6640Sstevel@tonic-gate VMEM_INSERT(afterme, walker, a);
6650Sstevel@tonic-gate
6660Sstevel@tonic-gate /*
6670Sstevel@tonic-gate * The walker segment's presence may have prevented its neighbors
6680Sstevel@tonic-gate * from coalescing. If so, coalesce them now.
6690Sstevel@tonic-gate */
6700Sstevel@tonic-gate if (vprev->vs_type == VMEM_FREE) {
6710Sstevel@tonic-gate if (vnext->vs_type == VMEM_FREE) {
6720Sstevel@tonic-gate ASSERT(vprev->vs_end == vnext->vs_start);
6730Sstevel@tonic-gate vmem_freelist_delete(vmp, vnext);
6740Sstevel@tonic-gate vmem_freelist_delete(vmp, vprev);
6750Sstevel@tonic-gate vprev->vs_end = vnext->vs_end;
6760Sstevel@tonic-gate vmem_freelist_insert(vmp, vprev);
6770Sstevel@tonic-gate vmem_seg_destroy(vmp, vnext);
6780Sstevel@tonic-gate }
6790Sstevel@tonic-gate vsp = vprev;
6800Sstevel@tonic-gate } else if (vnext->vs_type == VMEM_FREE) {
6810Sstevel@tonic-gate vsp = vnext;
6820Sstevel@tonic-gate }
6830Sstevel@tonic-gate
6840Sstevel@tonic-gate /*
6850Sstevel@tonic-gate * vsp could represent a complete imported span,
6860Sstevel@tonic-gate * in which case we must return it to the source.
6870Sstevel@tonic-gate */
6880Sstevel@tonic-gate if (vsp != NULL && vsp->vs_import && vmp->vm_source_free != NULL &&
6890Sstevel@tonic-gate vsp->vs_aprev->vs_type == VMEM_SPAN &&
6900Sstevel@tonic-gate vsp->vs_anext->vs_type == VMEM_SPAN) {
6910Sstevel@tonic-gate void *vaddr = (void *)vsp->vs_start;
6920Sstevel@tonic-gate size_t size = VS_SIZE(vsp);
6930Sstevel@tonic-gate ASSERT(size == VS_SIZE(vsp->vs_aprev));
6940Sstevel@tonic-gate vmem_freelist_delete(vmp, vsp);
6950Sstevel@tonic-gate vmem_span_destroy(vmp, vsp);
6960Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock);
6970Sstevel@tonic-gate vmp->vm_source_free(vmp->vm_source, vaddr, size);
6980Sstevel@tonic-gate (void) mutex_lock(&vmp->vm_lock);
6990Sstevel@tonic-gate }
7000Sstevel@tonic-gate }
7010Sstevel@tonic-gate
7020Sstevel@tonic-gate /*
7030Sstevel@tonic-gate * VM_NEXTFIT allocations deliberately cycle through all virtual addresses
7040Sstevel@tonic-gate * in an arena, so that we avoid reusing addresses for as long as possible.
7050Sstevel@tonic-gate * This helps to catch used-after-freed bugs. It's also the perfect policy
7060Sstevel@tonic-gate * for allocating things like process IDs, where we want to cycle through
7070Sstevel@tonic-gate * all values in order.
7080Sstevel@tonic-gate */
7090Sstevel@tonic-gate static void *
vmem_nextfit_alloc(vmem_t * vmp,size_t size,int vmflag)7100Sstevel@tonic-gate vmem_nextfit_alloc(vmem_t *vmp, size_t size, int vmflag)
7110Sstevel@tonic-gate {
7120Sstevel@tonic-gate vmem_seg_t *vsp, *rotor;
7130Sstevel@tonic-gate uintptr_t addr;
7140Sstevel@tonic-gate size_t realsize = P2ROUNDUP(size, vmp->vm_quantum);
7150Sstevel@tonic-gate size_t vs_size;
7160Sstevel@tonic-gate
7170Sstevel@tonic-gate (void) mutex_lock(&vmp->vm_lock);
7180Sstevel@tonic-gate
7190Sstevel@tonic-gate if (vmp->vm_nsegfree < VMEM_MINFREE && !vmem_populate(vmp, vmflag)) {
7200Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock);
7210Sstevel@tonic-gate return (NULL);
7220Sstevel@tonic-gate }
7230Sstevel@tonic-gate
7240Sstevel@tonic-gate /*
7250Sstevel@tonic-gate * The common case is that the segment right after the rotor is free,
7260Sstevel@tonic-gate * and large enough that extracting 'size' bytes won't change which
7270Sstevel@tonic-gate * freelist it's on. In this case we can avoid a *lot* of work.
7280Sstevel@tonic-gate * Instead of the normal vmem_seg_alloc(), we just advance the start
7290Sstevel@tonic-gate * address of the victim segment. Instead of moving the rotor, we
7300Sstevel@tonic-gate * create the new segment structure *behind the rotor*, which has
7310Sstevel@tonic-gate * the same effect. And finally, we know we don't have to coalesce
7320Sstevel@tonic-gate * the rotor's neighbors because the new segment lies between them.
7330Sstevel@tonic-gate */
7340Sstevel@tonic-gate rotor = &vmp->vm_rotor;
7350Sstevel@tonic-gate vsp = rotor->vs_anext;
7360Sstevel@tonic-gate if (vsp->vs_type == VMEM_FREE && (vs_size = VS_SIZE(vsp)) > realsize &&
7370Sstevel@tonic-gate P2SAMEHIGHBIT(vs_size, vs_size - realsize)) {
7380Sstevel@tonic-gate ASSERT(highbit(vs_size) == highbit(vs_size - realsize));
7390Sstevel@tonic-gate addr = vsp->vs_start;
7400Sstevel@tonic-gate vsp->vs_start = addr + realsize;
7410Sstevel@tonic-gate vmem_hash_insert(vmp,
7420Sstevel@tonic-gate vmem_seg_create(vmp, rotor->vs_aprev, addr, addr + size));
7430Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock);
7440Sstevel@tonic-gate return ((void *)addr);
7450Sstevel@tonic-gate }
7460Sstevel@tonic-gate
7470Sstevel@tonic-gate /*
7480Sstevel@tonic-gate * Starting at the rotor, look for a segment large enough to
7490Sstevel@tonic-gate * satisfy the allocation.
7500Sstevel@tonic-gate */
7510Sstevel@tonic-gate for (;;) {
7520Sstevel@tonic-gate vmp->vm_kstat.vk_search++;
7530Sstevel@tonic-gate if (vsp->vs_type == VMEM_FREE && VS_SIZE(vsp) >= size)
7540Sstevel@tonic-gate break;
7550Sstevel@tonic-gate vsp = vsp->vs_anext;
7560Sstevel@tonic-gate if (vsp == rotor) {
7575891Sraf int cancel_state;
7585891Sraf
7590Sstevel@tonic-gate /*
7600Sstevel@tonic-gate * We've come full circle. One possibility is that the
7610Sstevel@tonic-gate * there's actually enough space, but the rotor itself
7620Sstevel@tonic-gate * is preventing the allocation from succeeding because
7630Sstevel@tonic-gate * it's sitting between two free segments. Therefore,
7640Sstevel@tonic-gate * we advance the rotor and see if that liberates a
7650Sstevel@tonic-gate * suitable segment.
7660Sstevel@tonic-gate */
7670Sstevel@tonic-gate vmem_advance(vmp, rotor, rotor->vs_anext);
7680Sstevel@tonic-gate vsp = rotor->vs_aprev;
7690Sstevel@tonic-gate if (vsp->vs_type == VMEM_FREE && VS_SIZE(vsp) >= size)
7700Sstevel@tonic-gate break;
7710Sstevel@tonic-gate /*
7720Sstevel@tonic-gate * If there's a lower arena we can import from, or it's
7730Sstevel@tonic-gate * a VM_NOSLEEP allocation, let vmem_xalloc() handle it.
7740Sstevel@tonic-gate * Otherwise, wait until another thread frees something.
7750Sstevel@tonic-gate */
7760Sstevel@tonic-gate if (vmp->vm_source_alloc != NULL ||
7770Sstevel@tonic-gate (vmflag & VM_NOSLEEP)) {
7780Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock);
7790Sstevel@tonic-gate return (vmem_xalloc(vmp, size, vmp->vm_quantum,
7800Sstevel@tonic-gate 0, 0, NULL, NULL, vmflag & VM_UMFLAGS));
7810Sstevel@tonic-gate }
7820Sstevel@tonic-gate vmp->vm_kstat.vk_wait++;
7835891Sraf (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,
7845891Sraf &cancel_state);
7855891Sraf (void) cond_wait(&vmp->vm_cv, &vmp->vm_lock);
7865891Sraf (void) pthread_setcancelstate(cancel_state, NULL);
7870Sstevel@tonic-gate vsp = rotor->vs_anext;
7880Sstevel@tonic-gate }
7890Sstevel@tonic-gate }
7900Sstevel@tonic-gate
7910Sstevel@tonic-gate /*
7920Sstevel@tonic-gate * We found a segment. Extract enough space to satisfy the allocation.
7930Sstevel@tonic-gate */
7940Sstevel@tonic-gate addr = vsp->vs_start;
7950Sstevel@tonic-gate vsp = vmem_seg_alloc(vmp, vsp, addr, size);
7960Sstevel@tonic-gate ASSERT(vsp->vs_type == VMEM_ALLOC &&
7970Sstevel@tonic-gate vsp->vs_start == addr && vsp->vs_end == addr + size);
7980Sstevel@tonic-gate
7990Sstevel@tonic-gate /*
8000Sstevel@tonic-gate * Advance the rotor to right after the newly-allocated segment.
8010Sstevel@tonic-gate * That's where the next VM_NEXTFIT allocation will begin searching.
8020Sstevel@tonic-gate */
8030Sstevel@tonic-gate vmem_advance(vmp, rotor, vsp);
8040Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock);
8050Sstevel@tonic-gate return ((void *)addr);
8060Sstevel@tonic-gate }
8070Sstevel@tonic-gate
8080Sstevel@tonic-gate /*
8090Sstevel@tonic-gate * Allocate size bytes at offset phase from an align boundary such that the
8100Sstevel@tonic-gate * resulting segment [addr, addr + size) is a subset of [minaddr, maxaddr)
8110Sstevel@tonic-gate * that does not straddle a nocross-aligned boundary.
8120Sstevel@tonic-gate */
8130Sstevel@tonic-gate void *
vmem_xalloc(vmem_t * vmp,size_t size,size_t align,size_t phase,size_t nocross,void * minaddr,void * maxaddr,int vmflag)8140Sstevel@tonic-gate vmem_xalloc(vmem_t *vmp, size_t size, size_t align, size_t phase,
8150Sstevel@tonic-gate size_t nocross, void *minaddr, void *maxaddr, int vmflag)
8160Sstevel@tonic-gate {
8170Sstevel@tonic-gate vmem_seg_t *vsp;
8180Sstevel@tonic-gate vmem_seg_t *vbest = NULL;
8190Sstevel@tonic-gate uintptr_t addr, taddr, start, end;
8200Sstevel@tonic-gate void *vaddr;
8210Sstevel@tonic-gate int hb, flist, resv;
8220Sstevel@tonic-gate uint32_t mtbf;
8230Sstevel@tonic-gate
8240Sstevel@tonic-gate if (phase > 0 && phase >= align)
8250Sstevel@tonic-gate umem_panic("vmem_xalloc(%p, %lu, %lu, %lu, %lu, %p, %p, %x): "
8260Sstevel@tonic-gate "invalid phase",
8270Sstevel@tonic-gate (void *)vmp, size, align, phase, nocross,
8280Sstevel@tonic-gate minaddr, maxaddr, vmflag);
8290Sstevel@tonic-gate
8300Sstevel@tonic-gate if (align == 0)
8310Sstevel@tonic-gate align = vmp->vm_quantum;
8320Sstevel@tonic-gate
8330Sstevel@tonic-gate if ((align | phase | nocross) & (vmp->vm_quantum - 1)) {
8340Sstevel@tonic-gate umem_panic("vmem_xalloc(%p, %lu, %lu, %lu, %lu, %p, %p, %x): "
8350Sstevel@tonic-gate "parameters not vm_quantum aligned",
8360Sstevel@tonic-gate (void *)vmp, size, align, phase, nocross,
8370Sstevel@tonic-gate minaddr, maxaddr, vmflag);
8380Sstevel@tonic-gate }
8390Sstevel@tonic-gate
8400Sstevel@tonic-gate if (nocross != 0 &&
8410Sstevel@tonic-gate (align > nocross || P2ROUNDUP(phase + size, align) > nocross)) {
8420Sstevel@tonic-gate umem_panic("vmem_xalloc(%p, %lu, %lu, %lu, %lu, %p, %p, %x): "
8430Sstevel@tonic-gate "overconstrained allocation",
8440Sstevel@tonic-gate (void *)vmp, size, align, phase, nocross,
8450Sstevel@tonic-gate minaddr, maxaddr, vmflag);
8460Sstevel@tonic-gate }
8470Sstevel@tonic-gate
8480Sstevel@tonic-gate if ((mtbf = vmem_mtbf | vmp->vm_mtbf) != 0 && gethrtime() % mtbf == 0 &&
8490Sstevel@tonic-gate (vmflag & (VM_NOSLEEP | VM_PANIC)) == VM_NOSLEEP)
8500Sstevel@tonic-gate return (NULL);
8510Sstevel@tonic-gate
8520Sstevel@tonic-gate (void) mutex_lock(&vmp->vm_lock);
8530Sstevel@tonic-gate for (;;) {
8545891Sraf int cancel_state;
8555891Sraf
8560Sstevel@tonic-gate if (vmp->vm_nsegfree < VMEM_MINFREE &&
8570Sstevel@tonic-gate !vmem_populate(vmp, vmflag))
8580Sstevel@tonic-gate break;
8590Sstevel@tonic-gate
8600Sstevel@tonic-gate /*
8610Sstevel@tonic-gate * highbit() returns the highest bit + 1, which is exactly
8620Sstevel@tonic-gate * what we want: we want to search the first freelist whose
8630Sstevel@tonic-gate * members are *definitely* large enough to satisfy our
8640Sstevel@tonic-gate * allocation. However, there are certain cases in which we
8650Sstevel@tonic-gate * want to look at the next-smallest freelist (which *might*
8660Sstevel@tonic-gate * be able to satisfy the allocation):
8670Sstevel@tonic-gate *
8680Sstevel@tonic-gate * (1) The size is exactly a power of 2, in which case
8690Sstevel@tonic-gate * the smaller freelist is always big enough;
8700Sstevel@tonic-gate *
8710Sstevel@tonic-gate * (2) All other freelists are empty;
8720Sstevel@tonic-gate *
8730Sstevel@tonic-gate * (3) We're in the highest possible freelist, which is
8740Sstevel@tonic-gate * always empty (e.g. the 4GB freelist on 32-bit systems);
8750Sstevel@tonic-gate *
8760Sstevel@tonic-gate * (4) We're doing a best-fit or first-fit allocation.
8770Sstevel@tonic-gate */
8780Sstevel@tonic-gate if ((size & (size - 1)) == 0) {
8790Sstevel@tonic-gate flist = lowbit(P2ALIGN(vmp->vm_freemap, size));
8800Sstevel@tonic-gate } else {
8810Sstevel@tonic-gate hb = highbit(size);
8820Sstevel@tonic-gate if ((vmp->vm_freemap >> hb) == 0 ||
8830Sstevel@tonic-gate hb == VMEM_FREELISTS ||
8840Sstevel@tonic-gate (vmflag & (VM_BESTFIT | VM_FIRSTFIT)))
8850Sstevel@tonic-gate hb--;
8860Sstevel@tonic-gate flist = lowbit(P2ALIGN(vmp->vm_freemap, 1UL << hb));
8870Sstevel@tonic-gate }
8880Sstevel@tonic-gate
8890Sstevel@tonic-gate for (vbest = NULL, vsp = (flist == 0) ? NULL :
8900Sstevel@tonic-gate vmp->vm_freelist[flist - 1].vs_knext;
8910Sstevel@tonic-gate vsp != NULL; vsp = vsp->vs_knext) {
8920Sstevel@tonic-gate vmp->vm_kstat.vk_search++;
8930Sstevel@tonic-gate if (vsp->vs_start == 0) {
8940Sstevel@tonic-gate /*
8950Sstevel@tonic-gate * We're moving up to a larger freelist,
8960Sstevel@tonic-gate * so if we've already found a candidate,
8970Sstevel@tonic-gate * the fit can't possibly get any better.
8980Sstevel@tonic-gate */
8990Sstevel@tonic-gate if (vbest != NULL)
9000Sstevel@tonic-gate break;
9010Sstevel@tonic-gate /*
9020Sstevel@tonic-gate * Find the next non-empty freelist.
9030Sstevel@tonic-gate */
9040Sstevel@tonic-gate flist = lowbit(P2ALIGN(vmp->vm_freemap,
9050Sstevel@tonic-gate VS_SIZE(vsp)));
9060Sstevel@tonic-gate if (flist-- == 0)
9070Sstevel@tonic-gate break;
9080Sstevel@tonic-gate vsp = (vmem_seg_t *)&vmp->vm_freelist[flist];
9090Sstevel@tonic-gate ASSERT(vsp->vs_knext->vs_type == VMEM_FREE);
9100Sstevel@tonic-gate continue;
9110Sstevel@tonic-gate }
9120Sstevel@tonic-gate if (vsp->vs_end - 1 < (uintptr_t)minaddr)
9130Sstevel@tonic-gate continue;
9140Sstevel@tonic-gate if (vsp->vs_start > (uintptr_t)maxaddr - 1)
9150Sstevel@tonic-gate continue;
9160Sstevel@tonic-gate start = MAX(vsp->vs_start, (uintptr_t)minaddr);
9170Sstevel@tonic-gate end = MIN(vsp->vs_end - 1, (uintptr_t)maxaddr - 1) + 1;
9180Sstevel@tonic-gate taddr = P2PHASEUP(start, align, phase);
919*7837SMatthew.Ahrens@Sun.COM if (P2BOUNDARY(taddr, size, nocross))
9200Sstevel@tonic-gate taddr +=
9210Sstevel@tonic-gate P2ROUNDUP(P2NPHASE(taddr, nocross), align);
9220Sstevel@tonic-gate if ((taddr - start) + size > end - start ||
9230Sstevel@tonic-gate (vbest != NULL && VS_SIZE(vsp) >= VS_SIZE(vbest)))
9240Sstevel@tonic-gate continue;
9250Sstevel@tonic-gate vbest = vsp;
9260Sstevel@tonic-gate addr = taddr;
9270Sstevel@tonic-gate if (!(vmflag & VM_BESTFIT) || VS_SIZE(vbest) == size)
9280Sstevel@tonic-gate break;
9290Sstevel@tonic-gate }
9300Sstevel@tonic-gate if (vbest != NULL)
9310Sstevel@tonic-gate break;
9320Sstevel@tonic-gate if (size == 0)
9330Sstevel@tonic-gate umem_panic("vmem_xalloc(): size == 0");
9340Sstevel@tonic-gate if (vmp->vm_source_alloc != NULL && nocross == 0 &&
9350Sstevel@tonic-gate minaddr == NULL && maxaddr == NULL) {
9360Sstevel@tonic-gate size_t asize = P2ROUNDUP(size + phase,
9370Sstevel@tonic-gate MAX(align, vmp->vm_source->vm_quantum));
9380Sstevel@tonic-gate if (asize < size) { /* overflow */
9390Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock);
9400Sstevel@tonic-gate if (vmflag & VM_NOSLEEP)
9410Sstevel@tonic-gate return (NULL);
9420Sstevel@tonic-gate
9430Sstevel@tonic-gate umem_panic("vmem_xalloc(): "
9440Sstevel@tonic-gate "overflow on VM_SLEEP allocation");
9450Sstevel@tonic-gate }
9460Sstevel@tonic-gate /*
9470Sstevel@tonic-gate * Determine how many segment structures we'll consume.
9480Sstevel@tonic-gate * The calculation must be presise because if we're
9490Sstevel@tonic-gate * here on behalf of vmem_populate(), we are taking
9500Sstevel@tonic-gate * segments from a very limited reserve.
9510Sstevel@tonic-gate */
9520Sstevel@tonic-gate resv = (size == asize) ?
9530Sstevel@tonic-gate VMEM_SEGS_PER_SPAN_CREATE +
9540Sstevel@tonic-gate VMEM_SEGS_PER_EXACT_ALLOC :
9550Sstevel@tonic-gate VMEM_SEGS_PER_ALLOC_MAX;
9560Sstevel@tonic-gate ASSERT(vmp->vm_nsegfree >= resv);
9570Sstevel@tonic-gate vmp->vm_nsegfree -= resv; /* reserve our segs */
9580Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock);
9590Sstevel@tonic-gate vaddr = vmp->vm_source_alloc(vmp->vm_source, asize,
9600Sstevel@tonic-gate vmflag & VM_UMFLAGS);
9610Sstevel@tonic-gate (void) mutex_lock(&vmp->vm_lock);
9620Sstevel@tonic-gate vmp->vm_nsegfree += resv; /* claim reservation */
9630Sstevel@tonic-gate if (vaddr != NULL) {
9640Sstevel@tonic-gate vbest = vmem_span_create(vmp, vaddr, asize, 1);
9650Sstevel@tonic-gate addr = P2PHASEUP(vbest->vs_start, align, phase);
9660Sstevel@tonic-gate break;
9670Sstevel@tonic-gate }
9680Sstevel@tonic-gate }
9690Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock);
9700Sstevel@tonic-gate vmem_reap();
9710Sstevel@tonic-gate (void) mutex_lock(&vmp->vm_lock);
9720Sstevel@tonic-gate if (vmflag & VM_NOSLEEP)
9730Sstevel@tonic-gate break;
9740Sstevel@tonic-gate vmp->vm_kstat.vk_wait++;
9755891Sraf (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,
9765891Sraf &cancel_state);
9775891Sraf (void) cond_wait(&vmp->vm_cv, &vmp->vm_lock);
9785891Sraf (void) pthread_setcancelstate(cancel_state, NULL);
9790Sstevel@tonic-gate }
9800Sstevel@tonic-gate if (vbest != NULL) {
9810Sstevel@tonic-gate ASSERT(vbest->vs_type == VMEM_FREE);
9820Sstevel@tonic-gate ASSERT(vbest->vs_knext != vbest);
9830Sstevel@tonic-gate (void) vmem_seg_alloc(vmp, vbest, addr, size);
9840Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock);
9850Sstevel@tonic-gate ASSERT(P2PHASE(addr, align) == phase);
986*7837SMatthew.Ahrens@Sun.COM ASSERT(!P2BOUNDARY(addr, size, nocross));
9870Sstevel@tonic-gate ASSERT(addr >= (uintptr_t)minaddr);
9880Sstevel@tonic-gate ASSERT(addr + size - 1 <= (uintptr_t)maxaddr - 1);
9890Sstevel@tonic-gate return ((void *)addr);
9900Sstevel@tonic-gate }
9910Sstevel@tonic-gate vmp->vm_kstat.vk_fail++;
9920Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock);
9930Sstevel@tonic-gate if (vmflag & VM_PANIC)
9940Sstevel@tonic-gate umem_panic("vmem_xalloc(%p, %lu, %lu, %lu, %lu, %p, %p, %x): "
9950Sstevel@tonic-gate "cannot satisfy mandatory allocation",
9960Sstevel@tonic-gate (void *)vmp, size, align, phase, nocross,
9970Sstevel@tonic-gate minaddr, maxaddr, vmflag);
9980Sstevel@tonic-gate return (NULL);
9990Sstevel@tonic-gate }
10000Sstevel@tonic-gate
10010Sstevel@tonic-gate /*
10020Sstevel@tonic-gate * Free the segment [vaddr, vaddr + size), where vaddr was a constrained
10030Sstevel@tonic-gate * allocation. vmem_xalloc() and vmem_xfree() must always be paired because
10040Sstevel@tonic-gate * both routines bypass the quantum caches.
10050Sstevel@tonic-gate */
10060Sstevel@tonic-gate void
vmem_xfree(vmem_t * vmp,void * vaddr,size_t size)10070Sstevel@tonic-gate vmem_xfree(vmem_t *vmp, void *vaddr, size_t size)
10080Sstevel@tonic-gate {
10090Sstevel@tonic-gate vmem_seg_t *vsp, *vnext, *vprev;
10100Sstevel@tonic-gate
10110Sstevel@tonic-gate (void) mutex_lock(&vmp->vm_lock);
10120Sstevel@tonic-gate
10130Sstevel@tonic-gate vsp = vmem_hash_delete(vmp, (uintptr_t)vaddr, size);
10140Sstevel@tonic-gate vsp->vs_end = P2ROUNDUP(vsp->vs_end, vmp->vm_quantum);
10150Sstevel@tonic-gate
10160Sstevel@tonic-gate /*
10170Sstevel@tonic-gate * Attempt to coalesce with the next segment.
10180Sstevel@tonic-gate */
10190Sstevel@tonic-gate vnext = vsp->vs_anext;
10200Sstevel@tonic-gate if (vnext->vs_type == VMEM_FREE) {
10210Sstevel@tonic-gate ASSERT(vsp->vs_end == vnext->vs_start);
10220Sstevel@tonic-gate vmem_freelist_delete(vmp, vnext);
10230Sstevel@tonic-gate vsp->vs_end = vnext->vs_end;
10240Sstevel@tonic-gate vmem_seg_destroy(vmp, vnext);
10250Sstevel@tonic-gate }
10260Sstevel@tonic-gate
10270Sstevel@tonic-gate /*
10280Sstevel@tonic-gate * Attempt to coalesce with the previous segment.
10290Sstevel@tonic-gate */
10300Sstevel@tonic-gate vprev = vsp->vs_aprev;
10310Sstevel@tonic-gate if (vprev->vs_type == VMEM_FREE) {
10320Sstevel@tonic-gate ASSERT(vprev->vs_end == vsp->vs_start);
10330Sstevel@tonic-gate vmem_freelist_delete(vmp, vprev);
10340Sstevel@tonic-gate vprev->vs_end = vsp->vs_end;
10350Sstevel@tonic-gate vmem_seg_destroy(vmp, vsp);
10360Sstevel@tonic-gate vsp = vprev;
10370Sstevel@tonic-gate }
10380Sstevel@tonic-gate
10390Sstevel@tonic-gate /*
10400Sstevel@tonic-gate * If the entire span is free, return it to the source.
10410Sstevel@tonic-gate */
10420Sstevel@tonic-gate if (vsp->vs_import && vmp->vm_source_free != NULL &&
10430Sstevel@tonic-gate vsp->vs_aprev->vs_type == VMEM_SPAN &&
10440Sstevel@tonic-gate vsp->vs_anext->vs_type == VMEM_SPAN) {
10450Sstevel@tonic-gate vaddr = (void *)vsp->vs_start;
10460Sstevel@tonic-gate size = VS_SIZE(vsp);
10470Sstevel@tonic-gate ASSERT(size == VS_SIZE(vsp->vs_aprev));
10480Sstevel@tonic-gate vmem_span_destroy(vmp, vsp);
10490Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock);
10500Sstevel@tonic-gate vmp->vm_source_free(vmp->vm_source, vaddr, size);
10510Sstevel@tonic-gate } else {
10520Sstevel@tonic-gate vmem_freelist_insert(vmp, vsp);
10530Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock);
10540Sstevel@tonic-gate }
10550Sstevel@tonic-gate }
10560Sstevel@tonic-gate
10570Sstevel@tonic-gate /*
10580Sstevel@tonic-gate * Allocate size bytes from arena vmp. Returns the allocated address
10590Sstevel@tonic-gate * on success, NULL on failure. vmflag specifies VM_SLEEP or VM_NOSLEEP,
10600Sstevel@tonic-gate * and may also specify best-fit, first-fit, or next-fit allocation policy
10610Sstevel@tonic-gate * instead of the default instant-fit policy. VM_SLEEP allocations are
10620Sstevel@tonic-gate * guaranteed to succeed.
10630Sstevel@tonic-gate */
10640Sstevel@tonic-gate void *
vmem_alloc(vmem_t * vmp,size_t size,int vmflag)10650Sstevel@tonic-gate vmem_alloc(vmem_t *vmp, size_t size, int vmflag)
10660Sstevel@tonic-gate {
10670Sstevel@tonic-gate vmem_seg_t *vsp;
10680Sstevel@tonic-gate uintptr_t addr;
10690Sstevel@tonic-gate int hb;
10700Sstevel@tonic-gate int flist = 0;
10710Sstevel@tonic-gate uint32_t mtbf;
10720Sstevel@tonic-gate
10730Sstevel@tonic-gate if (size - 1 < vmp->vm_qcache_max) {
10740Sstevel@tonic-gate ASSERT(vmflag & VM_NOSLEEP);
10750Sstevel@tonic-gate return (_umem_cache_alloc(vmp->vm_qcache[(size - 1) >>
10760Sstevel@tonic-gate vmp->vm_qshift], UMEM_DEFAULT));
10770Sstevel@tonic-gate }
10780Sstevel@tonic-gate
10790Sstevel@tonic-gate if ((mtbf = vmem_mtbf | vmp->vm_mtbf) != 0 && gethrtime() % mtbf == 0 &&
10800Sstevel@tonic-gate (vmflag & (VM_NOSLEEP | VM_PANIC)) == VM_NOSLEEP)
10810Sstevel@tonic-gate return (NULL);
10820Sstevel@tonic-gate
10830Sstevel@tonic-gate if (vmflag & VM_NEXTFIT)
10840Sstevel@tonic-gate return (vmem_nextfit_alloc(vmp, size, vmflag));
10850Sstevel@tonic-gate
10860Sstevel@tonic-gate if (vmflag & (VM_BESTFIT | VM_FIRSTFIT))
10870Sstevel@tonic-gate return (vmem_xalloc(vmp, size, vmp->vm_quantum, 0, 0,
10880Sstevel@tonic-gate NULL, NULL, vmflag));
10890Sstevel@tonic-gate
10900Sstevel@tonic-gate /*
10910Sstevel@tonic-gate * Unconstrained instant-fit allocation from the segment list.
10920Sstevel@tonic-gate */
10930Sstevel@tonic-gate (void) mutex_lock(&vmp->vm_lock);
10940Sstevel@tonic-gate
10950Sstevel@tonic-gate if (vmp->vm_nsegfree >= VMEM_MINFREE || vmem_populate(vmp, vmflag)) {
10960Sstevel@tonic-gate if ((size & (size - 1)) == 0)
10970Sstevel@tonic-gate flist = lowbit(P2ALIGN(vmp->vm_freemap, size));
10980Sstevel@tonic-gate else if ((hb = highbit(size)) < VMEM_FREELISTS)
10990Sstevel@tonic-gate flist = lowbit(P2ALIGN(vmp->vm_freemap, 1UL << hb));
11000Sstevel@tonic-gate }
11010Sstevel@tonic-gate
11020Sstevel@tonic-gate if (flist-- == 0) {
11030Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock);
11040Sstevel@tonic-gate return (vmem_xalloc(vmp, size, vmp->vm_quantum,
11050Sstevel@tonic-gate 0, 0, NULL, NULL, vmflag));
11060Sstevel@tonic-gate }
11070Sstevel@tonic-gate
11080Sstevel@tonic-gate ASSERT(size <= (1UL << flist));
11090Sstevel@tonic-gate vsp = vmp->vm_freelist[flist].vs_knext;
11100Sstevel@tonic-gate addr = vsp->vs_start;
11110Sstevel@tonic-gate (void) vmem_seg_alloc(vmp, vsp, addr, size);
11120Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock);
11130Sstevel@tonic-gate return ((void *)addr);
11140Sstevel@tonic-gate }
11150Sstevel@tonic-gate
11160Sstevel@tonic-gate /*
11170Sstevel@tonic-gate * Free the segment [vaddr, vaddr + size).
11180Sstevel@tonic-gate */
11190Sstevel@tonic-gate void
vmem_free(vmem_t * vmp,void * vaddr,size_t size)11200Sstevel@tonic-gate vmem_free(vmem_t *vmp, void *vaddr, size_t size)
11210Sstevel@tonic-gate {
11220Sstevel@tonic-gate if (size - 1 < vmp->vm_qcache_max)
11230Sstevel@tonic-gate _umem_cache_free(vmp->vm_qcache[(size - 1) >> vmp->vm_qshift],
11240Sstevel@tonic-gate vaddr);
11250Sstevel@tonic-gate else
11260Sstevel@tonic-gate vmem_xfree(vmp, vaddr, size);
11270Sstevel@tonic-gate }
11280Sstevel@tonic-gate
11290Sstevel@tonic-gate /*
11300Sstevel@tonic-gate * Determine whether arena vmp contains the segment [vaddr, vaddr + size).
11310Sstevel@tonic-gate */
11320Sstevel@tonic-gate int
vmem_contains(vmem_t * vmp,void * vaddr,size_t size)11330Sstevel@tonic-gate vmem_contains(vmem_t *vmp, void *vaddr, size_t size)
11340Sstevel@tonic-gate {
11350Sstevel@tonic-gate uintptr_t start = (uintptr_t)vaddr;
11360Sstevel@tonic-gate uintptr_t end = start + size;
11370Sstevel@tonic-gate vmem_seg_t *vsp;
11380Sstevel@tonic-gate vmem_seg_t *seg0 = &vmp->vm_seg0;
11390Sstevel@tonic-gate
11400Sstevel@tonic-gate (void) mutex_lock(&vmp->vm_lock);
11410Sstevel@tonic-gate vmp->vm_kstat.vk_contains++;
11420Sstevel@tonic-gate for (vsp = seg0->vs_knext; vsp != seg0; vsp = vsp->vs_knext) {
11430Sstevel@tonic-gate vmp->vm_kstat.vk_contains_search++;
11440Sstevel@tonic-gate ASSERT(vsp->vs_type == VMEM_SPAN);
11450Sstevel@tonic-gate if (start >= vsp->vs_start && end - 1 <= vsp->vs_end - 1)
11460Sstevel@tonic-gate break;
11470Sstevel@tonic-gate }
11480Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock);
11490Sstevel@tonic-gate return (vsp != seg0);
11500Sstevel@tonic-gate }
11510Sstevel@tonic-gate
11520Sstevel@tonic-gate /*
11530Sstevel@tonic-gate * Add the span [vaddr, vaddr + size) to arena vmp.
11540Sstevel@tonic-gate */
11550Sstevel@tonic-gate void *
vmem_add(vmem_t * vmp,void * vaddr,size_t size,int vmflag)11560Sstevel@tonic-gate vmem_add(vmem_t *vmp, void *vaddr, size_t size, int vmflag)
11570Sstevel@tonic-gate {
11580Sstevel@tonic-gate if (vaddr == NULL || size == 0) {
11590Sstevel@tonic-gate umem_panic("vmem_add(%p, %p, %lu): bad arguments",
11600Sstevel@tonic-gate vmp, vaddr, size);
11610Sstevel@tonic-gate }
11620Sstevel@tonic-gate
11630Sstevel@tonic-gate ASSERT(!vmem_contains(vmp, vaddr, size));
11640Sstevel@tonic-gate
11650Sstevel@tonic-gate (void) mutex_lock(&vmp->vm_lock);
11660Sstevel@tonic-gate if (vmem_populate(vmp, vmflag))
11670Sstevel@tonic-gate (void) vmem_span_create(vmp, vaddr, size, 0);
11680Sstevel@tonic-gate else
11690Sstevel@tonic-gate vaddr = NULL;
11700Sstevel@tonic-gate (void) cond_broadcast(&vmp->vm_cv);
11710Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock);
11720Sstevel@tonic-gate return (vaddr);
11730Sstevel@tonic-gate }
11740Sstevel@tonic-gate
11750Sstevel@tonic-gate /*
11760Sstevel@tonic-gate * Adds the address range [addr, endaddr) to arena vmp, by either:
11770Sstevel@tonic-gate * 1. joining two existing spans, [x, addr), and [endaddr, y) (which
11780Sstevel@tonic-gate * are in that order) into a single [x, y) span,
11790Sstevel@tonic-gate * 2. expanding an existing [x, addr) span to [x, endaddr),
11800Sstevel@tonic-gate * 3. expanding an existing [endaddr, x) span to [addr, x), or
11810Sstevel@tonic-gate * 4. creating a new [addr, endaddr) span.
11820Sstevel@tonic-gate *
11830Sstevel@tonic-gate * Called with vmp->vm_lock held, and a successful vmem_populate() completed.
11840Sstevel@tonic-gate * Cannot fail. Returns the new segment.
11850Sstevel@tonic-gate *
11860Sstevel@tonic-gate * NOTE: this algorithm is linear-time in the number of spans, but is
11870Sstevel@tonic-gate * constant-time when you are extending the last (highest-addressed)
11880Sstevel@tonic-gate * span.
11890Sstevel@tonic-gate */
11900Sstevel@tonic-gate static vmem_seg_t *
vmem_extend_unlocked(vmem_t * vmp,uintptr_t addr,uintptr_t endaddr)11910Sstevel@tonic-gate vmem_extend_unlocked(vmem_t *vmp, uintptr_t addr, uintptr_t endaddr)
11920Sstevel@tonic-gate {
11930Sstevel@tonic-gate vmem_seg_t *span;
11940Sstevel@tonic-gate vmem_seg_t *vsp;
11950Sstevel@tonic-gate
11960Sstevel@tonic-gate vmem_seg_t *end = &vmp->vm_seg0;
11970Sstevel@tonic-gate
11980Sstevel@tonic-gate ASSERT(MUTEX_HELD(&vmp->vm_lock));
11990Sstevel@tonic-gate
12000Sstevel@tonic-gate /*
12010Sstevel@tonic-gate * the second "if" clause below relies on the direction of this search
12020Sstevel@tonic-gate */
12030Sstevel@tonic-gate for (span = end->vs_kprev; span != end; span = span->vs_kprev) {
12040Sstevel@tonic-gate if (span->vs_end == addr || span->vs_start == endaddr)
12050Sstevel@tonic-gate break;
12060Sstevel@tonic-gate }
12070Sstevel@tonic-gate
12080Sstevel@tonic-gate if (span == end)
12090Sstevel@tonic-gate return (vmem_span_create(vmp, (void *)addr, endaddr - addr, 0));
12100Sstevel@tonic-gate if (span->vs_kprev->vs_end == addr && span->vs_start == endaddr) {
12110Sstevel@tonic-gate vmem_seg_t *prevspan = span->vs_kprev;
12120Sstevel@tonic-gate vmem_seg_t *nextseg = span->vs_anext;
12130Sstevel@tonic-gate vmem_seg_t *prevseg = span->vs_aprev;
12140Sstevel@tonic-gate
12150Sstevel@tonic-gate /*
12160Sstevel@tonic-gate * prevspan becomes the span marker for the full range
12170Sstevel@tonic-gate */
12180Sstevel@tonic-gate prevspan->vs_end = span->vs_end;
12190Sstevel@tonic-gate
12200Sstevel@tonic-gate /*
12210Sstevel@tonic-gate * Notionally, span becomes a free segment representing
12220Sstevel@tonic-gate * [addr, endaddr).
12230Sstevel@tonic-gate *
12240Sstevel@tonic-gate * However, if either of its neighbors are free, we coalesce
12250Sstevel@tonic-gate * by destroying span and changing the free segment.
12260Sstevel@tonic-gate */
12270Sstevel@tonic-gate if (prevseg->vs_type == VMEM_FREE &&
12280Sstevel@tonic-gate nextseg->vs_type == VMEM_FREE) {
12290Sstevel@tonic-gate /*
12300Sstevel@tonic-gate * coalesce both ways
12310Sstevel@tonic-gate */
12320Sstevel@tonic-gate ASSERT(prevseg->vs_end == addr &&
12330Sstevel@tonic-gate nextseg->vs_start == endaddr);
12340Sstevel@tonic-gate
12350Sstevel@tonic-gate vmem_freelist_delete(vmp, prevseg);
12360Sstevel@tonic-gate prevseg->vs_end = nextseg->vs_end;
12370Sstevel@tonic-gate
12380Sstevel@tonic-gate vmem_freelist_delete(vmp, nextseg);
12390Sstevel@tonic-gate VMEM_DELETE(span, k);
12400Sstevel@tonic-gate vmem_seg_destroy(vmp, nextseg);
12410Sstevel@tonic-gate vmem_seg_destroy(vmp, span);
12420Sstevel@tonic-gate
12430Sstevel@tonic-gate vsp = prevseg;
12440Sstevel@tonic-gate } else if (prevseg->vs_type == VMEM_FREE) {
12450Sstevel@tonic-gate /*
12460Sstevel@tonic-gate * coalesce left
12470Sstevel@tonic-gate */
12480Sstevel@tonic-gate ASSERT(prevseg->vs_end == addr);
12490Sstevel@tonic-gate
12500Sstevel@tonic-gate VMEM_DELETE(span, k);
12510Sstevel@tonic-gate vmem_seg_destroy(vmp, span);
12520Sstevel@tonic-gate
12530Sstevel@tonic-gate vmem_freelist_delete(vmp, prevseg);
12540Sstevel@tonic-gate prevseg->vs_end = endaddr;
12550Sstevel@tonic-gate
12560Sstevel@tonic-gate vsp = prevseg;
12570Sstevel@tonic-gate } else if (nextseg->vs_type == VMEM_FREE) {
12580Sstevel@tonic-gate /*
12590Sstevel@tonic-gate * coalesce right
12600Sstevel@tonic-gate */
12610Sstevel@tonic-gate ASSERT(nextseg->vs_start == endaddr);
12620Sstevel@tonic-gate
12630Sstevel@tonic-gate VMEM_DELETE(span, k);
12640Sstevel@tonic-gate vmem_seg_destroy(vmp, span);
12650Sstevel@tonic-gate
12660Sstevel@tonic-gate vmem_freelist_delete(vmp, nextseg);
12670Sstevel@tonic-gate nextseg->vs_start = addr;
12680Sstevel@tonic-gate
12690Sstevel@tonic-gate vsp = nextseg;
12700Sstevel@tonic-gate } else {
12710Sstevel@tonic-gate /*
12720Sstevel@tonic-gate * cannnot coalesce
12730Sstevel@tonic-gate */
12740Sstevel@tonic-gate VMEM_DELETE(span, k);
12750Sstevel@tonic-gate span->vs_start = addr;
12760Sstevel@tonic-gate span->vs_end = endaddr;
12770Sstevel@tonic-gate
12780Sstevel@tonic-gate vsp = span;
12790Sstevel@tonic-gate }
12800Sstevel@tonic-gate } else if (span->vs_end == addr) {
12810Sstevel@tonic-gate vmem_seg_t *oldseg = span->vs_knext->vs_aprev;
12820Sstevel@tonic-gate span->vs_end = endaddr;
12830Sstevel@tonic-gate
12840Sstevel@tonic-gate ASSERT(oldseg->vs_type != VMEM_SPAN);
12850Sstevel@tonic-gate if (oldseg->vs_type == VMEM_FREE) {
12860Sstevel@tonic-gate ASSERT(oldseg->vs_end == addr);
12870Sstevel@tonic-gate vmem_freelist_delete(vmp, oldseg);
12880Sstevel@tonic-gate oldseg->vs_end = endaddr;
12890Sstevel@tonic-gate vsp = oldseg;
12900Sstevel@tonic-gate } else
12910Sstevel@tonic-gate vsp = vmem_seg_create(vmp, oldseg, addr, endaddr);
12920Sstevel@tonic-gate } else {
12930Sstevel@tonic-gate vmem_seg_t *oldseg = span->vs_anext;
12940Sstevel@tonic-gate ASSERT(span->vs_start == endaddr);
12950Sstevel@tonic-gate span->vs_start = addr;
12960Sstevel@tonic-gate
12970Sstevel@tonic-gate ASSERT(oldseg->vs_type != VMEM_SPAN);
12980Sstevel@tonic-gate if (oldseg->vs_type == VMEM_FREE) {
12990Sstevel@tonic-gate ASSERT(oldseg->vs_start == endaddr);
13000Sstevel@tonic-gate vmem_freelist_delete(vmp, oldseg);
13010Sstevel@tonic-gate oldseg->vs_start = addr;
13020Sstevel@tonic-gate vsp = oldseg;
13030Sstevel@tonic-gate } else
13040Sstevel@tonic-gate vsp = vmem_seg_create(vmp, span, addr, endaddr);
13050Sstevel@tonic-gate }
13060Sstevel@tonic-gate vmem_freelist_insert(vmp, vsp);
13070Sstevel@tonic-gate vmp->vm_kstat.vk_mem_total += (endaddr - addr);
13080Sstevel@tonic-gate return (vsp);
13090Sstevel@tonic-gate }
13100Sstevel@tonic-gate
13110Sstevel@tonic-gate /*
13120Sstevel@tonic-gate * Does some error checking, calls vmem_extend_unlocked to add
13130Sstevel@tonic-gate * [vaddr, vaddr+size) to vmp, then allocates alloc bytes from the
13140Sstevel@tonic-gate * newly merged segment.
13150Sstevel@tonic-gate */
13160Sstevel@tonic-gate void *
_vmem_extend_alloc(vmem_t * vmp,void * vaddr,size_t size,size_t alloc,int vmflag)13170Sstevel@tonic-gate _vmem_extend_alloc(vmem_t *vmp, void *vaddr, size_t size, size_t alloc,
13180Sstevel@tonic-gate int vmflag)
13190Sstevel@tonic-gate {
13200Sstevel@tonic-gate uintptr_t addr = (uintptr_t)vaddr;
13210Sstevel@tonic-gate uintptr_t endaddr = addr + size;
13220Sstevel@tonic-gate vmem_seg_t *vsp;
13230Sstevel@tonic-gate
13240Sstevel@tonic-gate ASSERT(vaddr != NULL && size != 0 && endaddr > addr);
13250Sstevel@tonic-gate ASSERT(alloc <= size && alloc != 0);
13260Sstevel@tonic-gate ASSERT(((addr | size | alloc) & (vmp->vm_quantum - 1)) == 0);
13270Sstevel@tonic-gate
13280Sstevel@tonic-gate ASSERT(!vmem_contains(vmp, vaddr, size));
13290Sstevel@tonic-gate
13300Sstevel@tonic-gate (void) mutex_lock(&vmp->vm_lock);
13310Sstevel@tonic-gate if (!vmem_populate(vmp, vmflag)) {
13320Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock);
13330Sstevel@tonic-gate return (NULL);
13340Sstevel@tonic-gate }
13350Sstevel@tonic-gate /*
13360Sstevel@tonic-gate * if there is a source, we can't mess with the spans
13370Sstevel@tonic-gate */
13380Sstevel@tonic-gate if (vmp->vm_source_alloc != NULL)
13390Sstevel@tonic-gate vsp = vmem_span_create(vmp, vaddr, size, 0);
13400Sstevel@tonic-gate else
13410Sstevel@tonic-gate vsp = vmem_extend_unlocked(vmp, addr, endaddr);
13420Sstevel@tonic-gate
13430Sstevel@tonic-gate ASSERT(VS_SIZE(vsp) >= alloc);
13440Sstevel@tonic-gate
13450Sstevel@tonic-gate addr = vsp->vs_start;
13460Sstevel@tonic-gate (void) vmem_seg_alloc(vmp, vsp, addr, alloc);
13470Sstevel@tonic-gate vaddr = (void *)addr;
13480Sstevel@tonic-gate
13490Sstevel@tonic-gate (void) cond_broadcast(&vmp->vm_cv);
13500Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock);
13510Sstevel@tonic-gate
13520Sstevel@tonic-gate return (vaddr);
13530Sstevel@tonic-gate }
13540Sstevel@tonic-gate
13550Sstevel@tonic-gate /*
13560Sstevel@tonic-gate * Walk the vmp arena, applying func to each segment matching typemask.
13570Sstevel@tonic-gate * If VMEM_REENTRANT is specified, the arena lock is dropped across each
13580Sstevel@tonic-gate * call to func(); otherwise, it is held for the duration of vmem_walk()
13590Sstevel@tonic-gate * to ensure a consistent snapshot. Note that VMEM_REENTRANT callbacks
13600Sstevel@tonic-gate * are *not* necessarily consistent, so they may only be used when a hint
13610Sstevel@tonic-gate * is adequate.
13620Sstevel@tonic-gate */
13630Sstevel@tonic-gate void
vmem_walk(vmem_t * vmp,int typemask,void (* func)(void *,void *,size_t),void * arg)13640Sstevel@tonic-gate vmem_walk(vmem_t *vmp, int typemask,
13650Sstevel@tonic-gate void (*func)(void *, void *, size_t), void *arg)
13660Sstevel@tonic-gate {
13670Sstevel@tonic-gate vmem_seg_t *vsp;
13680Sstevel@tonic-gate vmem_seg_t *seg0 = &vmp->vm_seg0;
13690Sstevel@tonic-gate vmem_seg_t walker;
13700Sstevel@tonic-gate
13710Sstevel@tonic-gate if (typemask & VMEM_WALKER)
13720Sstevel@tonic-gate return;
13730Sstevel@tonic-gate
13740Sstevel@tonic-gate bzero(&walker, sizeof (walker));
13750Sstevel@tonic-gate walker.vs_type = VMEM_WALKER;
13760Sstevel@tonic-gate
13770Sstevel@tonic-gate (void) mutex_lock(&vmp->vm_lock);
13780Sstevel@tonic-gate VMEM_INSERT(seg0, &walker, a);
13790Sstevel@tonic-gate for (vsp = seg0->vs_anext; vsp != seg0; vsp = vsp->vs_anext) {
13800Sstevel@tonic-gate if (vsp->vs_type & typemask) {
13810Sstevel@tonic-gate void *start = (void *)vsp->vs_start;
13820Sstevel@tonic-gate size_t size = VS_SIZE(vsp);
13830Sstevel@tonic-gate if (typemask & VMEM_REENTRANT) {
13840Sstevel@tonic-gate vmem_advance(vmp, &walker, vsp);
13850Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock);
13860Sstevel@tonic-gate func(arg, start, size);
13870Sstevel@tonic-gate (void) mutex_lock(&vmp->vm_lock);
13880Sstevel@tonic-gate vsp = &walker;
13890Sstevel@tonic-gate } else {
13900Sstevel@tonic-gate func(arg, start, size);
13910Sstevel@tonic-gate }
13920Sstevel@tonic-gate }
13930Sstevel@tonic-gate }
13940Sstevel@tonic-gate vmem_advance(vmp, &walker, NULL);
13950Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock);
13960Sstevel@tonic-gate }
13970Sstevel@tonic-gate
13980Sstevel@tonic-gate /*
13990Sstevel@tonic-gate * Return the total amount of memory whose type matches typemask. Thus:
14000Sstevel@tonic-gate *
14010Sstevel@tonic-gate * typemask VMEM_ALLOC yields total memory allocated (in use).
14020Sstevel@tonic-gate * typemask VMEM_FREE yields total memory free (available).
14030Sstevel@tonic-gate * typemask (VMEM_ALLOC | VMEM_FREE) yields total arena size.
14040Sstevel@tonic-gate */
14050Sstevel@tonic-gate size_t
vmem_size(vmem_t * vmp,int typemask)14060Sstevel@tonic-gate vmem_size(vmem_t *vmp, int typemask)
14070Sstevel@tonic-gate {
14080Sstevel@tonic-gate uint64_t size = 0;
14090Sstevel@tonic-gate
14100Sstevel@tonic-gate if (typemask & VMEM_ALLOC)
14110Sstevel@tonic-gate size += vmp->vm_kstat.vk_mem_inuse;
14120Sstevel@tonic-gate if (typemask & VMEM_FREE)
14130Sstevel@tonic-gate size += vmp->vm_kstat.vk_mem_total -
14140Sstevel@tonic-gate vmp->vm_kstat.vk_mem_inuse;
14150Sstevel@tonic-gate return ((size_t)size);
14160Sstevel@tonic-gate }
14170Sstevel@tonic-gate
14180Sstevel@tonic-gate /*
14190Sstevel@tonic-gate * Create an arena called name whose initial span is [base, base + size).
14200Sstevel@tonic-gate * The arena's natural unit of currency is quantum, so vmem_alloc()
14210Sstevel@tonic-gate * guarantees quantum-aligned results. The arena may import new spans
14220Sstevel@tonic-gate * by invoking afunc() on source, and may return those spans by invoking
14230Sstevel@tonic-gate * ffunc() on source. To make small allocations fast and scalable,
14240Sstevel@tonic-gate * the arena offers high-performance caching for each integer multiple
14250Sstevel@tonic-gate * of quantum up to qcache_max.
14260Sstevel@tonic-gate */
14270Sstevel@tonic-gate vmem_t *
vmem_create(const char * name,void * base,size_t size,size_t quantum,vmem_alloc_t * afunc,vmem_free_t * ffunc,vmem_t * source,size_t qcache_max,int vmflag)14280Sstevel@tonic-gate vmem_create(const char *name, void *base, size_t size, size_t quantum,
14290Sstevel@tonic-gate vmem_alloc_t *afunc, vmem_free_t *ffunc, vmem_t *source,
14300Sstevel@tonic-gate size_t qcache_max, int vmflag)
14310Sstevel@tonic-gate {
14320Sstevel@tonic-gate int i;
14330Sstevel@tonic-gate size_t nqcache;
14340Sstevel@tonic-gate vmem_t *vmp, *cur, **vmpp;
14350Sstevel@tonic-gate vmem_seg_t *vsp;
14360Sstevel@tonic-gate vmem_freelist_t *vfp;
14370Sstevel@tonic-gate uint32_t id = atomic_add_32_nv(&vmem_id, 1);
14380Sstevel@tonic-gate
14390Sstevel@tonic-gate if (vmem_vmem_arena != NULL) {
14400Sstevel@tonic-gate vmp = vmem_alloc(vmem_vmem_arena, sizeof (vmem_t),
14410Sstevel@tonic-gate vmflag & VM_UMFLAGS);
14420Sstevel@tonic-gate } else {
14430Sstevel@tonic-gate ASSERT(id <= VMEM_INITIAL);
14440Sstevel@tonic-gate vmp = &vmem0[id - 1];
14450Sstevel@tonic-gate }
14460Sstevel@tonic-gate
14470Sstevel@tonic-gate if (vmp == NULL)
14480Sstevel@tonic-gate return (NULL);
14490Sstevel@tonic-gate bzero(vmp, sizeof (vmem_t));
14500Sstevel@tonic-gate
14510Sstevel@tonic-gate (void) snprintf(vmp->vm_name, VMEM_NAMELEN, "%s", name);
14520Sstevel@tonic-gate (void) mutex_init(&vmp->vm_lock, USYNC_THREAD, NULL);
14530Sstevel@tonic-gate (void) cond_init(&vmp->vm_cv, USYNC_THREAD, NULL);
14540Sstevel@tonic-gate vmp->vm_cflags = vmflag;
14550Sstevel@tonic-gate vmflag &= VM_UMFLAGS;
14560Sstevel@tonic-gate
14570Sstevel@tonic-gate vmp->vm_quantum = quantum;
14580Sstevel@tonic-gate vmp->vm_qshift = highbit(quantum) - 1;
14590Sstevel@tonic-gate nqcache = MIN(qcache_max >> vmp->vm_qshift, VMEM_NQCACHE_MAX);
14600Sstevel@tonic-gate
14610Sstevel@tonic-gate for (i = 0; i <= VMEM_FREELISTS; i++) {
14620Sstevel@tonic-gate vfp = &vmp->vm_freelist[i];
14630Sstevel@tonic-gate vfp->vs_end = 1UL << i;
14640Sstevel@tonic-gate vfp->vs_knext = (vmem_seg_t *)(vfp + 1);
14650Sstevel@tonic-gate vfp->vs_kprev = (vmem_seg_t *)(vfp - 1);
14660Sstevel@tonic-gate }
14670Sstevel@tonic-gate
14680Sstevel@tonic-gate vmp->vm_freelist[0].vs_kprev = NULL;
14690Sstevel@tonic-gate vmp->vm_freelist[VMEM_FREELISTS].vs_knext = NULL;
14700Sstevel@tonic-gate vmp->vm_freelist[VMEM_FREELISTS].vs_end = 0;
14710Sstevel@tonic-gate vmp->vm_hash_table = vmp->vm_hash0;
14720Sstevel@tonic-gate vmp->vm_hash_mask = VMEM_HASH_INITIAL - 1;
14730Sstevel@tonic-gate vmp->vm_hash_shift = highbit(vmp->vm_hash_mask);
14740Sstevel@tonic-gate
14750Sstevel@tonic-gate vsp = &vmp->vm_seg0;
14760Sstevel@tonic-gate vsp->vs_anext = vsp;
14770Sstevel@tonic-gate vsp->vs_aprev = vsp;
14780Sstevel@tonic-gate vsp->vs_knext = vsp;
14790Sstevel@tonic-gate vsp->vs_kprev = vsp;
14800Sstevel@tonic-gate vsp->vs_type = VMEM_SPAN;
14810Sstevel@tonic-gate
14820Sstevel@tonic-gate vsp = &vmp->vm_rotor;
14830Sstevel@tonic-gate vsp->vs_type = VMEM_ROTOR;
14840Sstevel@tonic-gate VMEM_INSERT(&vmp->vm_seg0, vsp, a);
14850Sstevel@tonic-gate
14860Sstevel@tonic-gate vmp->vm_id = id;
14870Sstevel@tonic-gate if (source != NULL)
14880Sstevel@tonic-gate vmp->vm_kstat.vk_source_id = source->vm_id;
14890Sstevel@tonic-gate vmp->vm_source = source;
14900Sstevel@tonic-gate vmp->vm_source_alloc = afunc;
14910Sstevel@tonic-gate vmp->vm_source_free = ffunc;
14920Sstevel@tonic-gate
14930Sstevel@tonic-gate if (nqcache != 0) {
14940Sstevel@tonic-gate vmp->vm_qcache_max = nqcache << vmp->vm_qshift;
14950Sstevel@tonic-gate for (i = 0; i < nqcache; i++) {
14960Sstevel@tonic-gate char buf[VMEM_NAMELEN + 21];
14970Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%s_%lu",
14980Sstevel@tonic-gate vmp->vm_name, (long)((i + 1) * quantum));
14990Sstevel@tonic-gate vmp->vm_qcache[i] = umem_cache_create(buf,
15000Sstevel@tonic-gate (i + 1) * quantum, quantum, NULL, NULL, NULL,
15010Sstevel@tonic-gate NULL, vmp, UMC_QCACHE | UMC_NOTOUCH);
15020Sstevel@tonic-gate if (vmp->vm_qcache[i] == NULL) {
15030Sstevel@tonic-gate vmp->vm_qcache_max = i * quantum;
15040Sstevel@tonic-gate break;
15050Sstevel@tonic-gate }
15060Sstevel@tonic-gate }
15070Sstevel@tonic-gate }
15080Sstevel@tonic-gate
15090Sstevel@tonic-gate (void) mutex_lock(&vmem_list_lock);
15100Sstevel@tonic-gate vmpp = &vmem_list;
15110Sstevel@tonic-gate while ((cur = *vmpp) != NULL)
15120Sstevel@tonic-gate vmpp = &cur->vm_next;
15130Sstevel@tonic-gate *vmpp = vmp;
15140Sstevel@tonic-gate (void) mutex_unlock(&vmem_list_lock);
15150Sstevel@tonic-gate
15160Sstevel@tonic-gate if (vmp->vm_cflags & VMC_POPULATOR) {
15170Sstevel@tonic-gate uint_t pop_id = atomic_add_32_nv(&vmem_populators, 1);
15180Sstevel@tonic-gate ASSERT(pop_id <= VMEM_INITIAL);
15190Sstevel@tonic-gate vmem_populator[pop_id - 1] = vmp;
15200Sstevel@tonic-gate (void) mutex_lock(&vmp->vm_lock);
15210Sstevel@tonic-gate (void) vmem_populate(vmp, vmflag | VM_PANIC);
15220Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock);
15230Sstevel@tonic-gate }
15240Sstevel@tonic-gate
15250Sstevel@tonic-gate if ((base || size) && vmem_add(vmp, base, size, vmflag) == NULL) {
15260Sstevel@tonic-gate vmem_destroy(vmp);
15270Sstevel@tonic-gate return (NULL);
15280Sstevel@tonic-gate }
15290Sstevel@tonic-gate
15300Sstevel@tonic-gate return (vmp);
15310Sstevel@tonic-gate }
15320Sstevel@tonic-gate
15330Sstevel@tonic-gate /*
15340Sstevel@tonic-gate * Destroy arena vmp.
15350Sstevel@tonic-gate */
15360Sstevel@tonic-gate void
vmem_destroy(vmem_t * vmp)15370Sstevel@tonic-gate vmem_destroy(vmem_t *vmp)
15380Sstevel@tonic-gate {
15390Sstevel@tonic-gate vmem_t *cur, **vmpp;
15400Sstevel@tonic-gate vmem_seg_t *seg0 = &vmp->vm_seg0;
15410Sstevel@tonic-gate vmem_seg_t *vsp;
15420Sstevel@tonic-gate size_t leaked;
15430Sstevel@tonic-gate int i;
15440Sstevel@tonic-gate
15450Sstevel@tonic-gate (void) mutex_lock(&vmem_list_lock);
15460Sstevel@tonic-gate vmpp = &vmem_list;
15470Sstevel@tonic-gate while ((cur = *vmpp) != vmp)
15480Sstevel@tonic-gate vmpp = &cur->vm_next;
15490Sstevel@tonic-gate *vmpp = vmp->vm_next;
15500Sstevel@tonic-gate (void) mutex_unlock(&vmem_list_lock);
15510Sstevel@tonic-gate
15520Sstevel@tonic-gate for (i = 0; i < VMEM_NQCACHE_MAX; i++)
15530Sstevel@tonic-gate if (vmp->vm_qcache[i])
15540Sstevel@tonic-gate umem_cache_destroy(vmp->vm_qcache[i]);
15550Sstevel@tonic-gate
15560Sstevel@tonic-gate leaked = vmem_size(vmp, VMEM_ALLOC);
15570Sstevel@tonic-gate if (leaked != 0)
15580Sstevel@tonic-gate umem_printf("vmem_destroy('%s'): leaked %lu bytes",
15590Sstevel@tonic-gate vmp->vm_name, leaked);
15600Sstevel@tonic-gate
15610Sstevel@tonic-gate if (vmp->vm_hash_table != vmp->vm_hash0)
15620Sstevel@tonic-gate vmem_free(vmem_hash_arena, vmp->vm_hash_table,
15630Sstevel@tonic-gate (vmp->vm_hash_mask + 1) * sizeof (void *));
15640Sstevel@tonic-gate
15650Sstevel@tonic-gate /*
15660Sstevel@tonic-gate * Give back the segment structures for anything that's left in the
15670Sstevel@tonic-gate * arena, e.g. the primary spans and their free segments.
15680Sstevel@tonic-gate */
15690Sstevel@tonic-gate VMEM_DELETE(&vmp->vm_rotor, a);
15700Sstevel@tonic-gate for (vsp = seg0->vs_anext; vsp != seg0; vsp = vsp->vs_anext)
15710Sstevel@tonic-gate vmem_putseg_global(vsp);
15720Sstevel@tonic-gate
15730Sstevel@tonic-gate while (vmp->vm_nsegfree > 0)
15740Sstevel@tonic-gate vmem_putseg_global(vmem_getseg(vmp));
15750Sstevel@tonic-gate
15760Sstevel@tonic-gate (void) mutex_destroy(&vmp->vm_lock);
15770Sstevel@tonic-gate (void) cond_destroy(&vmp->vm_cv);
15780Sstevel@tonic-gate vmem_free(vmem_vmem_arena, vmp, sizeof (vmem_t));
15790Sstevel@tonic-gate }
15800Sstevel@tonic-gate
15810Sstevel@tonic-gate /*
15820Sstevel@tonic-gate * Resize vmp's hash table to keep the average lookup depth near 1.0.
15830Sstevel@tonic-gate */
15840Sstevel@tonic-gate static void
vmem_hash_rescale(vmem_t * vmp)15850Sstevel@tonic-gate vmem_hash_rescale(vmem_t *vmp)
15860Sstevel@tonic-gate {
15870Sstevel@tonic-gate vmem_seg_t **old_table, **new_table, *vsp;
15880Sstevel@tonic-gate size_t old_size, new_size, h, nseg;
15890Sstevel@tonic-gate
15900Sstevel@tonic-gate nseg = (size_t)(vmp->vm_kstat.vk_alloc - vmp->vm_kstat.vk_free);
15910Sstevel@tonic-gate
15920Sstevel@tonic-gate new_size = MAX(VMEM_HASH_INITIAL, 1 << (highbit(3 * nseg + 4) - 2));
15930Sstevel@tonic-gate old_size = vmp->vm_hash_mask + 1;
15940Sstevel@tonic-gate
15950Sstevel@tonic-gate if ((old_size >> 1) <= new_size && new_size <= (old_size << 1))
15960Sstevel@tonic-gate return;
15970Sstevel@tonic-gate
15980Sstevel@tonic-gate new_table = vmem_alloc(vmem_hash_arena, new_size * sizeof (void *),
15990Sstevel@tonic-gate VM_NOSLEEP);
16000Sstevel@tonic-gate if (new_table == NULL)
16010Sstevel@tonic-gate return;
16020Sstevel@tonic-gate bzero(new_table, new_size * sizeof (void *));
16030Sstevel@tonic-gate
16040Sstevel@tonic-gate (void) mutex_lock(&vmp->vm_lock);
16050Sstevel@tonic-gate
16060Sstevel@tonic-gate old_size = vmp->vm_hash_mask + 1;
16070Sstevel@tonic-gate old_table = vmp->vm_hash_table;
16080Sstevel@tonic-gate
16090Sstevel@tonic-gate vmp->vm_hash_mask = new_size - 1;
16100Sstevel@tonic-gate vmp->vm_hash_table = new_table;
16110Sstevel@tonic-gate vmp->vm_hash_shift = highbit(vmp->vm_hash_mask);
16120Sstevel@tonic-gate
16130Sstevel@tonic-gate for (h = 0; h < old_size; h++) {
16140Sstevel@tonic-gate vsp = old_table[h];
16150Sstevel@tonic-gate while (vsp != NULL) {
16160Sstevel@tonic-gate uintptr_t addr = vsp->vs_start;
16170Sstevel@tonic-gate vmem_seg_t *next_vsp = vsp->vs_knext;
16180Sstevel@tonic-gate vmem_seg_t **hash_bucket = VMEM_HASH(vmp, addr);
16190Sstevel@tonic-gate vsp->vs_knext = *hash_bucket;
16200Sstevel@tonic-gate *hash_bucket = vsp;
16210Sstevel@tonic-gate vsp = next_vsp;
16220Sstevel@tonic-gate }
16230Sstevel@tonic-gate }
16240Sstevel@tonic-gate
16250Sstevel@tonic-gate (void) mutex_unlock(&vmp->vm_lock);
16260Sstevel@tonic-gate
16270Sstevel@tonic-gate if (old_table != vmp->vm_hash0)
16280Sstevel@tonic-gate vmem_free(vmem_hash_arena, old_table,
16290Sstevel@tonic-gate old_size * sizeof (void *));
16300Sstevel@tonic-gate }
16310Sstevel@tonic-gate
16320Sstevel@tonic-gate /*
16330Sstevel@tonic-gate * Perform periodic maintenance on all vmem arenas.
16340Sstevel@tonic-gate */
16350Sstevel@tonic-gate /*ARGSUSED*/
16360Sstevel@tonic-gate void
vmem_update(void * dummy)16370Sstevel@tonic-gate vmem_update(void *dummy)
16380Sstevel@tonic-gate {
16390Sstevel@tonic-gate vmem_t *vmp;
16400Sstevel@tonic-gate
16410Sstevel@tonic-gate (void) mutex_lock(&vmem_list_lock);
16420Sstevel@tonic-gate for (vmp = vmem_list; vmp != NULL; vmp = vmp->vm_next) {
16430Sstevel@tonic-gate /*
16440Sstevel@tonic-gate * If threads are waiting for resources, wake them up
16450Sstevel@tonic-gate * periodically so they can issue another vmem_reap()
16460Sstevel@tonic-gate * to reclaim resources cached by the slab allocator.
16470Sstevel@tonic-gate */
16480Sstevel@tonic-gate (void) cond_broadcast(&vmp->vm_cv);
16490Sstevel@tonic-gate
16500Sstevel@tonic-gate /*
16510Sstevel@tonic-gate * Rescale the hash table to keep the hash chains short.
16520Sstevel@tonic-gate */
16530Sstevel@tonic-gate vmem_hash_rescale(vmp);
16540Sstevel@tonic-gate }
16550Sstevel@tonic-gate (void) mutex_unlock(&vmem_list_lock);
16560Sstevel@tonic-gate }
16570Sstevel@tonic-gate
16580Sstevel@tonic-gate /*
16590Sstevel@tonic-gate * If vmem_init is called again, we need to be able to reset the world.
16600Sstevel@tonic-gate * That includes resetting the statics back to their original values.
16610Sstevel@tonic-gate */
16620Sstevel@tonic-gate void
vmem_startup(void)16630Sstevel@tonic-gate vmem_startup(void)
16640Sstevel@tonic-gate {
16650Sstevel@tonic-gate #ifdef UMEM_STANDALONE
16660Sstevel@tonic-gate vmem_id = 0;
16670Sstevel@tonic-gate vmem_populators = 0;
16680Sstevel@tonic-gate vmem_segfree = NULL;
16690Sstevel@tonic-gate vmem_list = NULL;
16700Sstevel@tonic-gate vmem_internal_arena = NULL;
16710Sstevel@tonic-gate vmem_seg_arena = NULL;
16720Sstevel@tonic-gate vmem_hash_arena = NULL;
16730Sstevel@tonic-gate vmem_vmem_arena = NULL;
16740Sstevel@tonic-gate vmem_heap = NULL;
16750Sstevel@tonic-gate vmem_heap_alloc = NULL;
16760Sstevel@tonic-gate vmem_heap_free = NULL;
16770Sstevel@tonic-gate
16780Sstevel@tonic-gate bzero(vmem0, sizeof (vmem0));
16790Sstevel@tonic-gate bzero(vmem_populator, sizeof (vmem_populator));
16800Sstevel@tonic-gate bzero(vmem_seg0, sizeof (vmem_seg0));
16810Sstevel@tonic-gate #endif
16820Sstevel@tonic-gate }
16830Sstevel@tonic-gate
16840Sstevel@tonic-gate /*
16850Sstevel@tonic-gate * Prepare vmem for use.
16860Sstevel@tonic-gate */
16870Sstevel@tonic-gate vmem_t *
vmem_init(const char * parent_name,size_t parent_quantum,vmem_alloc_t * parent_alloc,vmem_free_t * parent_free,const char * heap_name,void * heap_start,size_t heap_size,size_t heap_quantum,vmem_alloc_t * heap_alloc,vmem_free_t * heap_free)16880Sstevel@tonic-gate vmem_init(const char *parent_name, size_t parent_quantum,
16890Sstevel@tonic-gate vmem_alloc_t *parent_alloc, vmem_free_t *parent_free,
16900Sstevel@tonic-gate const char *heap_name, void *heap_start, size_t heap_size,
16910Sstevel@tonic-gate size_t heap_quantum, vmem_alloc_t *heap_alloc, vmem_free_t *heap_free)
16920Sstevel@tonic-gate {
16930Sstevel@tonic-gate uint32_t id;
16940Sstevel@tonic-gate int nseg = VMEM_SEG_INITIAL;
16950Sstevel@tonic-gate vmem_t *parent, *heap;
16960Sstevel@tonic-gate
16970Sstevel@tonic-gate ASSERT(vmem_internal_arena == NULL);
16980Sstevel@tonic-gate
16990Sstevel@tonic-gate while (--nseg >= 0)
17000Sstevel@tonic-gate vmem_putseg_global(&vmem_seg0[nseg]);
17010Sstevel@tonic-gate
17020Sstevel@tonic-gate if (parent_name != NULL) {
17030Sstevel@tonic-gate parent = vmem_create(parent_name,
17040Sstevel@tonic-gate heap_start, heap_size, parent_quantum,
17050Sstevel@tonic-gate NULL, NULL, NULL, 0,
17060Sstevel@tonic-gate VM_SLEEP | VMC_POPULATOR);
17070Sstevel@tonic-gate heap_start = NULL;
17080Sstevel@tonic-gate heap_size = 0;
17090Sstevel@tonic-gate } else {
17100Sstevel@tonic-gate ASSERT(parent_alloc == NULL && parent_free == NULL);
17110Sstevel@tonic-gate parent = NULL;
17120Sstevel@tonic-gate }
17130Sstevel@tonic-gate
17140Sstevel@tonic-gate heap = vmem_create(heap_name,
17150Sstevel@tonic-gate heap_start, heap_size, heap_quantum,
17160Sstevel@tonic-gate parent_alloc, parent_free, parent, 0,
17170Sstevel@tonic-gate VM_SLEEP | VMC_POPULATOR);
17180Sstevel@tonic-gate
17190Sstevel@tonic-gate vmem_heap = heap;
17200Sstevel@tonic-gate vmem_heap_alloc = heap_alloc;
17210Sstevel@tonic-gate vmem_heap_free = heap_free;
17220Sstevel@tonic-gate
17230Sstevel@tonic-gate vmem_internal_arena = vmem_create("vmem_internal",
17240Sstevel@tonic-gate NULL, 0, heap_quantum,
17250Sstevel@tonic-gate heap_alloc, heap_free, heap, 0,
17260Sstevel@tonic-gate VM_SLEEP | VMC_POPULATOR);
17270Sstevel@tonic-gate
17280Sstevel@tonic-gate vmem_seg_arena = vmem_create("vmem_seg",
17290Sstevel@tonic-gate NULL, 0, heap_quantum,
17300Sstevel@tonic-gate vmem_alloc, vmem_free, vmem_internal_arena, 0,
17310Sstevel@tonic-gate VM_SLEEP | VMC_POPULATOR);
17320Sstevel@tonic-gate
17330Sstevel@tonic-gate vmem_hash_arena = vmem_create("vmem_hash",
17340Sstevel@tonic-gate NULL, 0, 8,
17350Sstevel@tonic-gate vmem_alloc, vmem_free, vmem_internal_arena, 0,
17360Sstevel@tonic-gate VM_SLEEP);
17370Sstevel@tonic-gate
17380Sstevel@tonic-gate vmem_vmem_arena = vmem_create("vmem_vmem",
17390Sstevel@tonic-gate vmem0, sizeof (vmem0), 1,
17400Sstevel@tonic-gate vmem_alloc, vmem_free, vmem_internal_arena, 0,
17410Sstevel@tonic-gate VM_SLEEP);
17420Sstevel@tonic-gate
17430Sstevel@tonic-gate for (id = 0; id < vmem_id; id++)
17440Sstevel@tonic-gate (void) vmem_xalloc(vmem_vmem_arena, sizeof (vmem_t),
17450Sstevel@tonic-gate 1, 0, 0, &vmem0[id], &vmem0[id + 1],
17460Sstevel@tonic-gate VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
17470Sstevel@tonic-gate
17480Sstevel@tonic-gate return (heap);
17490Sstevel@tonic-gate }
17500Sstevel@tonic-gate
17510Sstevel@tonic-gate void
vmem_no_debug(void)17520Sstevel@tonic-gate vmem_no_debug(void)
17530Sstevel@tonic-gate {
17540Sstevel@tonic-gate /*
17550Sstevel@tonic-gate * This size must be a multiple of the minimum required alignment,
17560Sstevel@tonic-gate * since vmem_populate allocates them compactly.
17570Sstevel@tonic-gate */
17580Sstevel@tonic-gate vmem_seg_size = P2ROUNDUP(offsetof(vmem_seg_t, vs_thread),
17590Sstevel@tonic-gate sizeof (hrtime_t));
17600Sstevel@tonic-gate }
17610Sstevel@tonic-gate
17620Sstevel@tonic-gate /*
17630Sstevel@tonic-gate * Lockup and release, for fork1(2) handling.
17640Sstevel@tonic-gate */
17650Sstevel@tonic-gate void
vmem_lockup(void)17660Sstevel@tonic-gate vmem_lockup(void)
17670Sstevel@tonic-gate {
17680Sstevel@tonic-gate vmem_t *cur;
17690Sstevel@tonic-gate
17700Sstevel@tonic-gate (void) mutex_lock(&vmem_list_lock);
17710Sstevel@tonic-gate (void) mutex_lock(&vmem_nosleep_lock.vmpl_mutex);
17720Sstevel@tonic-gate
17730Sstevel@tonic-gate /*
17740Sstevel@tonic-gate * Lock up and broadcast all arenas.
17750Sstevel@tonic-gate */
17760Sstevel@tonic-gate for (cur = vmem_list; cur != NULL; cur = cur->vm_next) {
17770Sstevel@tonic-gate (void) mutex_lock(&cur->vm_lock);
17780Sstevel@tonic-gate (void) cond_broadcast(&cur->vm_cv);
17790Sstevel@tonic-gate }
17800Sstevel@tonic-gate
17810Sstevel@tonic-gate (void) mutex_lock(&vmem_segfree_lock);
17820Sstevel@tonic-gate }
17830Sstevel@tonic-gate
17840Sstevel@tonic-gate void
vmem_release(void)17850Sstevel@tonic-gate vmem_release(void)
17860Sstevel@tonic-gate {
17870Sstevel@tonic-gate vmem_t *cur;
17880Sstevel@tonic-gate
17890Sstevel@tonic-gate (void) mutex_unlock(&vmem_nosleep_lock.vmpl_mutex);
17900Sstevel@tonic-gate
17910Sstevel@tonic-gate for (cur = vmem_list; cur != NULL; cur = cur->vm_next)
17920Sstevel@tonic-gate (void) mutex_unlock(&cur->vm_lock);
17930Sstevel@tonic-gate
17940Sstevel@tonic-gate (void) mutex_unlock(&vmem_segfree_lock);
17950Sstevel@tonic-gate (void) mutex_unlock(&vmem_list_lock);
17960Sstevel@tonic-gate }
1797