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
54204Sha137994 * Common Development and Distribution License (the "License").
64204Sha137994 * 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 */
210Sstevel@tonic-gate /*
22*11475SBlake.Jones@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate * Big Theory Statement for the virtual memory allocator.
280Sstevel@tonic-gate *
290Sstevel@tonic-gate * For a more complete description of the main ideas, see:
300Sstevel@tonic-gate *
310Sstevel@tonic-gate * Jeff Bonwick and Jonathan Adams,
320Sstevel@tonic-gate *
330Sstevel@tonic-gate * Magazines and vmem: Extending the Slab Allocator to Many CPUs and
340Sstevel@tonic-gate * Arbitrary Resources.
350Sstevel@tonic-gate *
360Sstevel@tonic-gate * Proceedings of the 2001 Usenix Conference.
370Sstevel@tonic-gate * Available as http://www.usenix.org/event/usenix01/bonwick.html
380Sstevel@tonic-gate *
390Sstevel@tonic-gate *
400Sstevel@tonic-gate * 1. General Concepts
410Sstevel@tonic-gate * -------------------
420Sstevel@tonic-gate *
430Sstevel@tonic-gate * 1.1 Overview
440Sstevel@tonic-gate * ------------
450Sstevel@tonic-gate * We divide the kernel address space into a number of logically distinct
460Sstevel@tonic-gate * pieces, or *arenas*: text, data, heap, stack, and so on. Within these
470Sstevel@tonic-gate * arenas we often subdivide further; for example, we use heap addresses
480Sstevel@tonic-gate * not only for the kernel heap (kmem_alloc() space), but also for DVMA,
490Sstevel@tonic-gate * bp_mapin(), /dev/kmem, and even some device mappings like the TOD chip.
500Sstevel@tonic-gate * The kernel address space, therefore, is most accurately described as
510Sstevel@tonic-gate * a tree of arenas in which each node of the tree *imports* some subset
520Sstevel@tonic-gate * of its parent. The virtual memory allocator manages these arenas and
530Sstevel@tonic-gate * supports their natural hierarchical structure.
540Sstevel@tonic-gate *
550Sstevel@tonic-gate * 1.2 Arenas
560Sstevel@tonic-gate * ----------
570Sstevel@tonic-gate * An arena is nothing more than a set of integers. These integers most
580Sstevel@tonic-gate * commonly represent virtual addresses, but in fact they can represent
590Sstevel@tonic-gate * anything at all. For example, we could use an arena containing the
600Sstevel@tonic-gate * integers minpid through maxpid to allocate process IDs. vmem_create()
610Sstevel@tonic-gate * and vmem_destroy() create and destroy vmem arenas. In order to
620Sstevel@tonic-gate * differentiate between arenas used for adresses and arenas used for
630Sstevel@tonic-gate * identifiers, the VMC_IDENTIFIER flag is passed to vmem_create(). This
640Sstevel@tonic-gate * prevents identifier exhaustion from being diagnosed as general memory
650Sstevel@tonic-gate * failure.
660Sstevel@tonic-gate *
670Sstevel@tonic-gate * 1.3 Spans
680Sstevel@tonic-gate * ---------
690Sstevel@tonic-gate * We represent the integers in an arena as a collection of *spans*, or
700Sstevel@tonic-gate * contiguous ranges of integers. For example, the kernel heap consists
710Sstevel@tonic-gate * of just one span: [kernelheap, ekernelheap). Spans can be added to an
720Sstevel@tonic-gate * arena in two ways: explicitly, by vmem_add(), or implicitly, by
730Sstevel@tonic-gate * importing, as described in Section 1.5 below.
740Sstevel@tonic-gate *
750Sstevel@tonic-gate * 1.4 Segments
760Sstevel@tonic-gate * ------------
770Sstevel@tonic-gate * Spans are subdivided into *segments*, each of which is either allocated
780Sstevel@tonic-gate * or free. A segment, like a span, is a contiguous range of integers.
790Sstevel@tonic-gate * Each allocated segment [addr, addr + size) represents exactly one
800Sstevel@tonic-gate * vmem_alloc(size) that returned addr. Free segments represent the space
810Sstevel@tonic-gate * between allocated segments. If two free segments are adjacent, we
820Sstevel@tonic-gate * coalesce them into one larger segment; that is, if segments [a, b) and
830Sstevel@tonic-gate * [b, c) are both free, we merge them into a single segment [a, c).
840Sstevel@tonic-gate * The segments within a span are linked together in increasing-address order
850Sstevel@tonic-gate * so we can easily determine whether coalescing is possible.
860Sstevel@tonic-gate *
870Sstevel@tonic-gate * Segments never cross span boundaries. When all segments within
880Sstevel@tonic-gate * an imported span become free, we return the span to its source.
890Sstevel@tonic-gate *
900Sstevel@tonic-gate * 1.5 Imported Memory
910Sstevel@tonic-gate * -------------------
920Sstevel@tonic-gate * As mentioned in the overview, some arenas are logical subsets of
930Sstevel@tonic-gate * other arenas. For example, kmem_va_arena (a virtual address cache
940Sstevel@tonic-gate * that satisfies most kmem_slab_create() requests) is just a subset
950Sstevel@tonic-gate * of heap_arena (the kernel heap) that provides caching for the most
960Sstevel@tonic-gate * common slab sizes. When kmem_va_arena runs out of virtual memory,
970Sstevel@tonic-gate * it *imports* more from the heap; we say that heap_arena is the
980Sstevel@tonic-gate * *vmem source* for kmem_va_arena. vmem_create() allows you to
990Sstevel@tonic-gate * specify any existing vmem arena as the source for your new arena.
1000Sstevel@tonic-gate * Topologically, since every arena is a child of at most one source,
1010Sstevel@tonic-gate * the set of all arenas forms a collection of trees.
1020Sstevel@tonic-gate *
1030Sstevel@tonic-gate * 1.6 Constrained Allocations
1040Sstevel@tonic-gate * ---------------------------
1050Sstevel@tonic-gate * Some vmem clients are quite picky about the kind of address they want.
1060Sstevel@tonic-gate * For example, the DVMA code may need an address that is at a particular
1070Sstevel@tonic-gate * phase with respect to some alignment (to get good cache coloring), or
1080Sstevel@tonic-gate * that lies within certain limits (the addressable range of a device),
1090Sstevel@tonic-gate * or that doesn't cross some boundary (a DMA counter restriction) --
1100Sstevel@tonic-gate * or all of the above. vmem_xalloc() allows the client to specify any
1110Sstevel@tonic-gate * or all of these constraints.
1120Sstevel@tonic-gate *
1130Sstevel@tonic-gate * 1.7 The Vmem Quantum
1140Sstevel@tonic-gate * --------------------
1150Sstevel@tonic-gate * Every arena has a notion of 'quantum', specified at vmem_create() time,
1160Sstevel@tonic-gate * that defines the arena's minimum unit of currency. Most commonly the
1170Sstevel@tonic-gate * quantum is either 1 or PAGESIZE, but any power of 2 is legal.
1180Sstevel@tonic-gate * All vmem allocations are guaranteed to be quantum-aligned.
1190Sstevel@tonic-gate *
1200Sstevel@tonic-gate * 1.8 Quantum Caching
1210Sstevel@tonic-gate * -------------------
1220Sstevel@tonic-gate * A vmem arena may be so hot (frequently used) that the scalability of vmem
1230Sstevel@tonic-gate * allocation is a significant concern. We address this by allowing the most
1240Sstevel@tonic-gate * common allocation sizes to be serviced by the kernel memory allocator,
1250Sstevel@tonic-gate * which provides low-latency per-cpu caching. The qcache_max argument to
1260Sstevel@tonic-gate * vmem_create() specifies the largest allocation size to cache.
1270Sstevel@tonic-gate *
1280Sstevel@tonic-gate * 1.9 Relationship to Kernel Memory Allocator
1290Sstevel@tonic-gate * -------------------------------------------
1300Sstevel@tonic-gate * Every kmem cache has a vmem arena as its slab supplier. The kernel memory
1310Sstevel@tonic-gate * allocator uses vmem_alloc() and vmem_free() to create and destroy slabs.
1320Sstevel@tonic-gate *
1330Sstevel@tonic-gate *
1340Sstevel@tonic-gate * 2. Implementation
1350Sstevel@tonic-gate * -----------------
1360Sstevel@tonic-gate *
1370Sstevel@tonic-gate * 2.1 Segment lists and markers
1380Sstevel@tonic-gate * -----------------------------
1390Sstevel@tonic-gate * The segment structure (vmem_seg_t) contains two doubly-linked lists.
1400Sstevel@tonic-gate *
1410Sstevel@tonic-gate * The arena list (vs_anext/vs_aprev) links all segments in the arena.
1420Sstevel@tonic-gate * In addition to the allocated and free segments, the arena contains
1430Sstevel@tonic-gate * special marker segments at span boundaries. Span markers simplify
1440Sstevel@tonic-gate * coalescing and importing logic by making it easy to tell both when
1450Sstevel@tonic-gate * we're at a span boundary (so we don't coalesce across it), and when
1460Sstevel@tonic-gate * a span is completely free (its neighbors will both be span markers).
1470Sstevel@tonic-gate *
1480Sstevel@tonic-gate * Imported spans will have vs_import set.
1490Sstevel@tonic-gate *
1500Sstevel@tonic-gate * The next-of-kin list (vs_knext/vs_kprev) links segments of the same type:
1510Sstevel@tonic-gate * (1) for allocated segments, vs_knext is the hash chain linkage;
1520Sstevel@tonic-gate * (2) for free segments, vs_knext is the freelist linkage;
1530Sstevel@tonic-gate * (3) for span marker segments, vs_knext is the next span marker.
1540Sstevel@tonic-gate *
1550Sstevel@tonic-gate * 2.2 Allocation hashing
1560Sstevel@tonic-gate * ----------------------
1570Sstevel@tonic-gate * We maintain a hash table of all allocated segments, hashed by address.
1580Sstevel@tonic-gate * This allows vmem_free() to discover the target segment in constant time.
1590Sstevel@tonic-gate * vmem_update() periodically resizes hash tables to keep hash chains short.
1600Sstevel@tonic-gate *
1610Sstevel@tonic-gate * 2.3 Freelist management
1620Sstevel@tonic-gate * -----------------------
1630Sstevel@tonic-gate * We maintain power-of-2 freelists for free segments, i.e. free segments
1640Sstevel@tonic-gate * of size >= 2^n reside in vmp->vm_freelist[n]. To ensure constant-time
1650Sstevel@tonic-gate * allocation, vmem_xalloc() looks not in the first freelist that *might*
1660Sstevel@tonic-gate * satisfy the allocation, but in the first freelist that *definitely*
1670Sstevel@tonic-gate * satisfies the allocation (unless VM_BESTFIT is specified, or all larger
1680Sstevel@tonic-gate * freelists are empty). For example, a 1000-byte allocation will be
1690Sstevel@tonic-gate * satisfied not from the 512..1023-byte freelist, whose members *might*
1700Sstevel@tonic-gate * contains a 1000-byte segment, but from a 1024-byte or larger freelist,
1710Sstevel@tonic-gate * the first member of which will *definitely* satisfy the allocation.
1720Sstevel@tonic-gate * This ensures that vmem_xalloc() works in constant time.
1730Sstevel@tonic-gate *
1740Sstevel@tonic-gate * We maintain a bit map to determine quickly which freelists are non-empty.
1750Sstevel@tonic-gate * vmp->vm_freemap & (1 << n) is non-zero iff vmp->vm_freelist[n] is non-empty.
1760Sstevel@tonic-gate *
1770Sstevel@tonic-gate * The different freelists are linked together into one large freelist,
1780Sstevel@tonic-gate * with the freelist heads serving as markers. Freelist markers simplify
1790Sstevel@tonic-gate * the maintenance of vm_freemap by making it easy to tell when we're taking
1800Sstevel@tonic-gate * the last member of a freelist (both of its neighbors will be markers).
1810Sstevel@tonic-gate *
1820Sstevel@tonic-gate * 2.4 Vmem Locking
1830Sstevel@tonic-gate * ----------------
1840Sstevel@tonic-gate * For simplicity, all arena state is protected by a per-arena lock.
1850Sstevel@tonic-gate * For very hot arenas, use quantum caching for scalability.
1860Sstevel@tonic-gate *
1870Sstevel@tonic-gate * 2.5 Vmem Population
1880Sstevel@tonic-gate * -------------------
1890Sstevel@tonic-gate * Any internal vmem routine that might need to allocate new segment
1900Sstevel@tonic-gate * structures must prepare in advance by calling vmem_populate(), which
1910Sstevel@tonic-gate * will preallocate enough vmem_seg_t's to get is through the entire
1920Sstevel@tonic-gate * operation without dropping the arena lock.
1930Sstevel@tonic-gate *
1940Sstevel@tonic-gate * 2.6 Auditing
1950Sstevel@tonic-gate * ------------
1960Sstevel@tonic-gate * If KMF_AUDIT is set in kmem_flags, we audit vmem allocations as well.
1970Sstevel@tonic-gate * Since virtual addresses cannot be scribbled on, there is no equivalent
1980Sstevel@tonic-gate * in vmem to redzone checking, deadbeef, or other kmem debugging features.
1990Sstevel@tonic-gate * Moreover, we do not audit frees because segment coalescing destroys the
2000Sstevel@tonic-gate * association between an address and its segment structure. Auditing is
2010Sstevel@tonic-gate * thus intended primarily to keep track of who's consuming the arena.
2020Sstevel@tonic-gate * Debugging support could certainly be extended in the future if it proves
2030Sstevel@tonic-gate * necessary, but we do so much live checking via the allocation hash table
2040Sstevel@tonic-gate * that even non-DEBUG systems get quite a bit of sanity checking already.
2050Sstevel@tonic-gate */
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate #include <sys/vmem_impl.h>
2080Sstevel@tonic-gate #include <sys/kmem.h>
2090Sstevel@tonic-gate #include <sys/kstat.h>
2100Sstevel@tonic-gate #include <sys/param.h>
2110Sstevel@tonic-gate #include <sys/systm.h>
2120Sstevel@tonic-gate #include <sys/atomic.h>
2130Sstevel@tonic-gate #include <sys/bitmap.h>
2140Sstevel@tonic-gate #include <sys/sysmacros.h>
2150Sstevel@tonic-gate #include <sys/cmn_err.h>
2160Sstevel@tonic-gate #include <sys/debug.h>
2170Sstevel@tonic-gate #include <sys/panic.h>
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate #define VMEM_INITIAL 10 /* early vmem arenas */
2200Sstevel@tonic-gate #define VMEM_SEG_INITIAL 200 /* early segments */
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate /*
2230Sstevel@tonic-gate * Adding a new span to an arena requires two segment structures: one to
2240Sstevel@tonic-gate * represent the span, and one to represent the free segment it contains.
2250Sstevel@tonic-gate */
2260Sstevel@tonic-gate #define VMEM_SEGS_PER_SPAN_CREATE 2
2270Sstevel@tonic-gate
2280Sstevel@tonic-gate /*
2290Sstevel@tonic-gate * Allocating a piece of an existing segment requires 0-2 segment structures
2300Sstevel@tonic-gate * depending on how much of the segment we're allocating.
2310Sstevel@tonic-gate *
2320Sstevel@tonic-gate * To allocate the entire segment, no new segment structures are needed; we
2330Sstevel@tonic-gate * simply move the existing segment structure from the freelist to the
2340Sstevel@tonic-gate * allocation hash table.
2350Sstevel@tonic-gate *
2360Sstevel@tonic-gate * To allocate a piece from the left or right end of the segment, we must
2370Sstevel@tonic-gate * split the segment into two pieces (allocated part and remainder), so we
2380Sstevel@tonic-gate * need one new segment structure to represent the remainder.
2390Sstevel@tonic-gate *
2400Sstevel@tonic-gate * To allocate from the middle of a segment, we need two new segment strucures
2410Sstevel@tonic-gate * to represent the remainders on either side of the allocated part.
2420Sstevel@tonic-gate */
2430Sstevel@tonic-gate #define VMEM_SEGS_PER_EXACT_ALLOC 0
2440Sstevel@tonic-gate #define VMEM_SEGS_PER_LEFT_ALLOC 1
2450Sstevel@tonic-gate #define VMEM_SEGS_PER_RIGHT_ALLOC 1
2460Sstevel@tonic-gate #define VMEM_SEGS_PER_MIDDLE_ALLOC 2
2470Sstevel@tonic-gate
2480Sstevel@tonic-gate /*
2490Sstevel@tonic-gate * vmem_populate() preallocates segment structures for vmem to do its work.
2500Sstevel@tonic-gate * It must preallocate enough for the worst case, which is when we must import
2510Sstevel@tonic-gate * a new span and then allocate from the middle of it.
2520Sstevel@tonic-gate */
2530Sstevel@tonic-gate #define VMEM_SEGS_PER_ALLOC_MAX \
2540Sstevel@tonic-gate (VMEM_SEGS_PER_SPAN_CREATE + VMEM_SEGS_PER_MIDDLE_ALLOC)
2550Sstevel@tonic-gate
2560Sstevel@tonic-gate /*
2570Sstevel@tonic-gate * The segment structures themselves are allocated from vmem_seg_arena, so
2580Sstevel@tonic-gate * we have a recursion problem when vmem_seg_arena needs to populate itself.
2590Sstevel@tonic-gate * We address this by working out the maximum number of segment structures
2600Sstevel@tonic-gate * this act will require, and multiplying by the maximum number of threads
2610Sstevel@tonic-gate * that we'll allow to do it simultaneously.
2620Sstevel@tonic-gate *
2630Sstevel@tonic-gate * The worst-case segment consumption to populate vmem_seg_arena is as
2640Sstevel@tonic-gate * follows (depicted as a stack trace to indicate why events are occurring):
2650Sstevel@tonic-gate *
2660Sstevel@tonic-gate * (In order to lower the fragmentation in the heap_arena, we specify a
2670Sstevel@tonic-gate * minimum import size for the vmem_metadata_arena which is the same size
2680Sstevel@tonic-gate * as the kmem_va quantum cache allocations. This causes the worst-case
2690Sstevel@tonic-gate * allocation from the vmem_metadata_arena to be 3 segments.)
2700Sstevel@tonic-gate *
2710Sstevel@tonic-gate * vmem_alloc(vmem_seg_arena) -> 2 segs (span create + exact alloc)
2720Sstevel@tonic-gate * segkmem_alloc(vmem_metadata_arena)
2730Sstevel@tonic-gate * vmem_alloc(vmem_metadata_arena) -> 3 segs (span create + left alloc)
2740Sstevel@tonic-gate * vmem_alloc(heap_arena) -> 1 seg (left alloc)
2750Sstevel@tonic-gate * page_create()
2760Sstevel@tonic-gate * hat_memload()
2770Sstevel@tonic-gate * kmem_cache_alloc()
2780Sstevel@tonic-gate * kmem_slab_create()
2790Sstevel@tonic-gate * vmem_alloc(hat_memload_arena) -> 2 segs (span create + exact alloc)
2800Sstevel@tonic-gate * segkmem_alloc(heap_arena)
2810Sstevel@tonic-gate * vmem_alloc(heap_arena) -> 1 seg (left alloc)
2820Sstevel@tonic-gate * page_create()
2830Sstevel@tonic-gate * hat_memload() -> (hat layer won't recurse further)
2840Sstevel@tonic-gate *
2850Sstevel@tonic-gate * The worst-case consumption for each arena is 3 segment structures.
2860Sstevel@tonic-gate * Of course, a 3-seg reserve could easily be blown by multiple threads.
2870Sstevel@tonic-gate * Therefore, we serialize all allocations from vmem_seg_arena (which is OK
2880Sstevel@tonic-gate * because they're rare). We cannot allow a non-blocking allocation to get
2890Sstevel@tonic-gate * tied up behind a blocking allocation, however, so we use separate locks
2906058Sbonwick * for VM_SLEEP and VM_NOSLEEP allocations. Similarly, VM_PUSHPAGE allocations
2916058Sbonwick * must not block behind ordinary VM_SLEEPs. In addition, if the system is
2920Sstevel@tonic-gate * panicking then we must keep enough resources for panic_thread to do its
2936058Sbonwick * work. Thus we have at most four threads trying to allocate from
2940Sstevel@tonic-gate * vmem_seg_arena, and each thread consumes at most three segment structures,
2956058Sbonwick * so we must maintain a 12-seg reserve.
2960Sstevel@tonic-gate */
2976058Sbonwick #define VMEM_POPULATE_RESERVE 12
2980Sstevel@tonic-gate
2990Sstevel@tonic-gate /*
3000Sstevel@tonic-gate * vmem_populate() ensures that each arena has VMEM_MINFREE seg structures
3010Sstevel@tonic-gate * so that it can satisfy the worst-case allocation *and* participate in
3020Sstevel@tonic-gate * worst-case allocation from vmem_seg_arena.
3030Sstevel@tonic-gate */
3040Sstevel@tonic-gate #define VMEM_MINFREE (VMEM_POPULATE_RESERVE + VMEM_SEGS_PER_ALLOC_MAX)
3050Sstevel@tonic-gate
3060Sstevel@tonic-gate static vmem_t vmem0[VMEM_INITIAL];
3070Sstevel@tonic-gate static vmem_t *vmem_populator[VMEM_INITIAL];
3080Sstevel@tonic-gate static uint32_t vmem_id;
3090Sstevel@tonic-gate static uint32_t vmem_populators;
3100Sstevel@tonic-gate static vmem_seg_t vmem_seg0[VMEM_SEG_INITIAL];
3110Sstevel@tonic-gate static vmem_seg_t *vmem_segfree;
3120Sstevel@tonic-gate static kmutex_t vmem_list_lock;
3130Sstevel@tonic-gate static kmutex_t vmem_segfree_lock;
3140Sstevel@tonic-gate static kmutex_t vmem_sleep_lock;
3150Sstevel@tonic-gate static kmutex_t vmem_nosleep_lock;
3166058Sbonwick static kmutex_t vmem_pushpage_lock;
3170Sstevel@tonic-gate static kmutex_t vmem_panic_lock;
3180Sstevel@tonic-gate static vmem_t *vmem_list;
3190Sstevel@tonic-gate static vmem_t *vmem_metadata_arena;
3200Sstevel@tonic-gate static vmem_t *vmem_seg_arena;
3210Sstevel@tonic-gate static vmem_t *vmem_hash_arena;
3220Sstevel@tonic-gate static vmem_t *vmem_vmem_arena;
3230Sstevel@tonic-gate static long vmem_update_interval = 15; /* vmem_update() every 15 seconds */
3240Sstevel@tonic-gate uint32_t vmem_mtbf; /* mean time between failures [default: off] */
3250Sstevel@tonic-gate size_t vmem_seg_size = sizeof (vmem_seg_t);
3260Sstevel@tonic-gate
3270Sstevel@tonic-gate static vmem_kstat_t vmem_kstat_template = {
3280Sstevel@tonic-gate { "mem_inuse", KSTAT_DATA_UINT64 },
3290Sstevel@tonic-gate { "mem_import", KSTAT_DATA_UINT64 },
3300Sstevel@tonic-gate { "mem_total", KSTAT_DATA_UINT64 },
3310Sstevel@tonic-gate { "vmem_source", KSTAT_DATA_UINT32 },
3320Sstevel@tonic-gate { "alloc", KSTAT_DATA_UINT64 },
3330Sstevel@tonic-gate { "free", KSTAT_DATA_UINT64 },
3340Sstevel@tonic-gate { "wait", KSTAT_DATA_UINT64 },
3350Sstevel@tonic-gate { "fail", KSTAT_DATA_UINT64 },
3360Sstevel@tonic-gate { "lookup", KSTAT_DATA_UINT64 },
3370Sstevel@tonic-gate { "search", KSTAT_DATA_UINT64 },
3380Sstevel@tonic-gate { "populate_wait", KSTAT_DATA_UINT64 },
3390Sstevel@tonic-gate { "populate_fail", KSTAT_DATA_UINT64 },
3400Sstevel@tonic-gate { "contains", KSTAT_DATA_UINT64 },
3410Sstevel@tonic-gate { "contains_search", KSTAT_DATA_UINT64 },
3420Sstevel@tonic-gate };
3430Sstevel@tonic-gate
3440Sstevel@tonic-gate /*
3450Sstevel@tonic-gate * Insert/delete from arena list (type 'a') or next-of-kin list (type 'k').
3460Sstevel@tonic-gate */
3470Sstevel@tonic-gate #define VMEM_INSERT(vprev, vsp, type) \
3480Sstevel@tonic-gate { \
3490Sstevel@tonic-gate vmem_seg_t *vnext = (vprev)->vs_##type##next; \
3500Sstevel@tonic-gate (vsp)->vs_##type##next = (vnext); \
3510Sstevel@tonic-gate (vsp)->vs_##type##prev = (vprev); \
3520Sstevel@tonic-gate (vprev)->vs_##type##next = (vsp); \
3530Sstevel@tonic-gate (vnext)->vs_##type##prev = (vsp); \
3540Sstevel@tonic-gate }
3550Sstevel@tonic-gate
3560Sstevel@tonic-gate #define VMEM_DELETE(vsp, type) \
3570Sstevel@tonic-gate { \
3580Sstevel@tonic-gate vmem_seg_t *vprev = (vsp)->vs_##type##prev; \
3590Sstevel@tonic-gate vmem_seg_t *vnext = (vsp)->vs_##type##next; \
3600Sstevel@tonic-gate (vprev)->vs_##type##next = (vnext); \
3610Sstevel@tonic-gate (vnext)->vs_##type##prev = (vprev); \
3620Sstevel@tonic-gate }
3630Sstevel@tonic-gate
3640Sstevel@tonic-gate /*
3650Sstevel@tonic-gate * Get a vmem_seg_t from the global segfree list.
3660Sstevel@tonic-gate */
3670Sstevel@tonic-gate static vmem_seg_t *
vmem_getseg_global(void)3680Sstevel@tonic-gate vmem_getseg_global(void)
3690Sstevel@tonic-gate {
3700Sstevel@tonic-gate vmem_seg_t *vsp;
3710Sstevel@tonic-gate
3720Sstevel@tonic-gate mutex_enter(&vmem_segfree_lock);
3730Sstevel@tonic-gate if ((vsp = vmem_segfree) != NULL)
3740Sstevel@tonic-gate vmem_segfree = vsp->vs_knext;
3750Sstevel@tonic-gate mutex_exit(&vmem_segfree_lock);
3760Sstevel@tonic-gate
3770Sstevel@tonic-gate return (vsp);
3780Sstevel@tonic-gate }
3790Sstevel@tonic-gate
3800Sstevel@tonic-gate /*
3810Sstevel@tonic-gate * Put a vmem_seg_t on the global segfree list.
3820Sstevel@tonic-gate */
3830Sstevel@tonic-gate static void
vmem_putseg_global(vmem_seg_t * vsp)3840Sstevel@tonic-gate vmem_putseg_global(vmem_seg_t *vsp)
3850Sstevel@tonic-gate {
3860Sstevel@tonic-gate mutex_enter(&vmem_segfree_lock);
3870Sstevel@tonic-gate vsp->vs_knext = vmem_segfree;
3880Sstevel@tonic-gate vmem_segfree = vsp;
3890Sstevel@tonic-gate mutex_exit(&vmem_segfree_lock);
3900Sstevel@tonic-gate }
3910Sstevel@tonic-gate
3920Sstevel@tonic-gate /*
3930Sstevel@tonic-gate * Get a vmem_seg_t from vmp's segfree list.
3940Sstevel@tonic-gate */
3950Sstevel@tonic-gate static vmem_seg_t *
vmem_getseg(vmem_t * vmp)3960Sstevel@tonic-gate vmem_getseg(vmem_t *vmp)
3970Sstevel@tonic-gate {
3980Sstevel@tonic-gate vmem_seg_t *vsp;
3990Sstevel@tonic-gate
4000Sstevel@tonic-gate ASSERT(vmp->vm_nsegfree > 0);
4010Sstevel@tonic-gate
4020Sstevel@tonic-gate vsp = vmp->vm_segfree;
4030Sstevel@tonic-gate vmp->vm_segfree = vsp->vs_knext;
4040Sstevel@tonic-gate vmp->vm_nsegfree--;
4050Sstevel@tonic-gate
4060Sstevel@tonic-gate return (vsp);
4070Sstevel@tonic-gate }
4080Sstevel@tonic-gate
4090Sstevel@tonic-gate /*
4100Sstevel@tonic-gate * Put a vmem_seg_t on vmp's segfree list.
4110Sstevel@tonic-gate */
4120Sstevel@tonic-gate static void
vmem_putseg(vmem_t * vmp,vmem_seg_t * vsp)4130Sstevel@tonic-gate vmem_putseg(vmem_t *vmp, vmem_seg_t *vsp)
4140Sstevel@tonic-gate {
4150Sstevel@tonic-gate vsp->vs_knext = vmp->vm_segfree;
4160Sstevel@tonic-gate vmp->vm_segfree = vsp;
4170Sstevel@tonic-gate vmp->vm_nsegfree++;
4180Sstevel@tonic-gate }
4190Sstevel@tonic-gate
4200Sstevel@tonic-gate /*
4210Sstevel@tonic-gate * Add vsp to the appropriate freelist.
4220Sstevel@tonic-gate */
4230Sstevel@tonic-gate static void
vmem_freelist_insert(vmem_t * vmp,vmem_seg_t * vsp)4240Sstevel@tonic-gate vmem_freelist_insert(vmem_t *vmp, vmem_seg_t *vsp)
4250Sstevel@tonic-gate {
4260Sstevel@tonic-gate vmem_seg_t *vprev;
4270Sstevel@tonic-gate
4280Sstevel@tonic-gate ASSERT(*VMEM_HASH(vmp, vsp->vs_start) != vsp);
4290Sstevel@tonic-gate
4300Sstevel@tonic-gate vprev = (vmem_seg_t *)&vmp->vm_freelist[highbit(VS_SIZE(vsp)) - 1];
4310Sstevel@tonic-gate vsp->vs_type = VMEM_FREE;
4320Sstevel@tonic-gate vmp->vm_freemap |= VS_SIZE(vprev);
4330Sstevel@tonic-gate VMEM_INSERT(vprev, vsp, k);
4340Sstevel@tonic-gate
4350Sstevel@tonic-gate cv_broadcast(&vmp->vm_cv);
4360Sstevel@tonic-gate }
4370Sstevel@tonic-gate
4380Sstevel@tonic-gate /*
4390Sstevel@tonic-gate * Take vsp from the freelist.
4400Sstevel@tonic-gate */
4410Sstevel@tonic-gate static void
vmem_freelist_delete(vmem_t * vmp,vmem_seg_t * vsp)4420Sstevel@tonic-gate vmem_freelist_delete(vmem_t *vmp, vmem_seg_t *vsp)
4430Sstevel@tonic-gate {
4440Sstevel@tonic-gate ASSERT(*VMEM_HASH(vmp, vsp->vs_start) != vsp);
4450Sstevel@tonic-gate ASSERT(vsp->vs_type == VMEM_FREE);
4460Sstevel@tonic-gate
4470Sstevel@tonic-gate if (vsp->vs_knext->vs_start == 0 && vsp->vs_kprev->vs_start == 0) {
4480Sstevel@tonic-gate /*
4490Sstevel@tonic-gate * The segments on both sides of 'vsp' are freelist heads,
4500Sstevel@tonic-gate * so taking vsp leaves the freelist at vsp->vs_kprev empty.
4510Sstevel@tonic-gate */
4520Sstevel@tonic-gate ASSERT(vmp->vm_freemap & VS_SIZE(vsp->vs_kprev));
4530Sstevel@tonic-gate vmp->vm_freemap ^= VS_SIZE(vsp->vs_kprev);
4540Sstevel@tonic-gate }
4550Sstevel@tonic-gate VMEM_DELETE(vsp, k);
4560Sstevel@tonic-gate }
4570Sstevel@tonic-gate
4580Sstevel@tonic-gate /*
4590Sstevel@tonic-gate * Add vsp to the allocated-segment hash table and update kstats.
4600Sstevel@tonic-gate */
4610Sstevel@tonic-gate static void
vmem_hash_insert(vmem_t * vmp,vmem_seg_t * vsp)4620Sstevel@tonic-gate vmem_hash_insert(vmem_t *vmp, vmem_seg_t *vsp)
4630Sstevel@tonic-gate {
4640Sstevel@tonic-gate vmem_seg_t **bucket;
4650Sstevel@tonic-gate
4660Sstevel@tonic-gate vsp->vs_type = VMEM_ALLOC;
4670Sstevel@tonic-gate bucket = VMEM_HASH(vmp, vsp->vs_start);
4680Sstevel@tonic-gate vsp->vs_knext = *bucket;
4690Sstevel@tonic-gate *bucket = vsp;
4700Sstevel@tonic-gate
4710Sstevel@tonic-gate if (vmem_seg_size == sizeof (vmem_seg_t)) {
4720Sstevel@tonic-gate vsp->vs_depth = (uint8_t)getpcstack(vsp->vs_stack,
4730Sstevel@tonic-gate VMEM_STACK_DEPTH);
4740Sstevel@tonic-gate vsp->vs_thread = curthread;
4750Sstevel@tonic-gate vsp->vs_timestamp = gethrtime();
4760Sstevel@tonic-gate } else {
4770Sstevel@tonic-gate vsp->vs_depth = 0;
4780Sstevel@tonic-gate }
4790Sstevel@tonic-gate
4800Sstevel@tonic-gate vmp->vm_kstat.vk_alloc.value.ui64++;
4810Sstevel@tonic-gate vmp->vm_kstat.vk_mem_inuse.value.ui64 += VS_SIZE(vsp);
4820Sstevel@tonic-gate }
4830Sstevel@tonic-gate
4840Sstevel@tonic-gate /*
4850Sstevel@tonic-gate * Remove vsp from the allocated-segment hash table and update kstats.
4860Sstevel@tonic-gate */
4870Sstevel@tonic-gate static vmem_seg_t *
vmem_hash_delete(vmem_t * vmp,uintptr_t addr,size_t size)4880Sstevel@tonic-gate vmem_hash_delete(vmem_t *vmp, uintptr_t addr, size_t size)
4890Sstevel@tonic-gate {
4900Sstevel@tonic-gate vmem_seg_t *vsp, **prev_vspp;
4910Sstevel@tonic-gate
4920Sstevel@tonic-gate prev_vspp = VMEM_HASH(vmp, addr);
4930Sstevel@tonic-gate while ((vsp = *prev_vspp) != NULL) {
4940Sstevel@tonic-gate if (vsp->vs_start == addr) {
4950Sstevel@tonic-gate *prev_vspp = vsp->vs_knext;
4960Sstevel@tonic-gate break;
4970Sstevel@tonic-gate }
4980Sstevel@tonic-gate vmp->vm_kstat.vk_lookup.value.ui64++;
4990Sstevel@tonic-gate prev_vspp = &vsp->vs_knext;
5000Sstevel@tonic-gate }
5010Sstevel@tonic-gate
5020Sstevel@tonic-gate if (vsp == NULL)
5030Sstevel@tonic-gate panic("vmem_hash_delete(%p, %lx, %lu): bad free",
5047240Srh87107 (void *)vmp, addr, size);
5050Sstevel@tonic-gate if (VS_SIZE(vsp) != size)
5060Sstevel@tonic-gate panic("vmem_hash_delete(%p, %lx, %lu): wrong size (expect %lu)",
5077240Srh87107 (void *)vmp, addr, size, VS_SIZE(vsp));
5080Sstevel@tonic-gate
5090Sstevel@tonic-gate vmp->vm_kstat.vk_free.value.ui64++;
5100Sstevel@tonic-gate vmp->vm_kstat.vk_mem_inuse.value.ui64 -= size;
5110Sstevel@tonic-gate
5120Sstevel@tonic-gate return (vsp);
5130Sstevel@tonic-gate }
5140Sstevel@tonic-gate
5150Sstevel@tonic-gate /*
5160Sstevel@tonic-gate * Create a segment spanning the range [start, end) and add it to the arena.
5170Sstevel@tonic-gate */
5180Sstevel@tonic-gate static vmem_seg_t *
vmem_seg_create(vmem_t * vmp,vmem_seg_t * vprev,uintptr_t start,uintptr_t end)5190Sstevel@tonic-gate vmem_seg_create(vmem_t *vmp, vmem_seg_t *vprev, uintptr_t start, uintptr_t end)
5200Sstevel@tonic-gate {
5210Sstevel@tonic-gate vmem_seg_t *newseg = vmem_getseg(vmp);
5220Sstevel@tonic-gate
5230Sstevel@tonic-gate newseg->vs_start = start;
5240Sstevel@tonic-gate newseg->vs_end = end;
5250Sstevel@tonic-gate newseg->vs_type = 0;
5260Sstevel@tonic-gate newseg->vs_import = 0;
5270Sstevel@tonic-gate
5280Sstevel@tonic-gate VMEM_INSERT(vprev, newseg, a);
5290Sstevel@tonic-gate
5300Sstevel@tonic-gate return (newseg);
5310Sstevel@tonic-gate }
5320Sstevel@tonic-gate
5330Sstevel@tonic-gate /*
5340Sstevel@tonic-gate * Remove segment vsp from the arena.
5350Sstevel@tonic-gate */
5360Sstevel@tonic-gate static void
vmem_seg_destroy(vmem_t * vmp,vmem_seg_t * vsp)5370Sstevel@tonic-gate vmem_seg_destroy(vmem_t *vmp, vmem_seg_t *vsp)
5380Sstevel@tonic-gate {
5390Sstevel@tonic-gate ASSERT(vsp->vs_type != VMEM_ROTOR);
5400Sstevel@tonic-gate VMEM_DELETE(vsp, a);
5410Sstevel@tonic-gate
5420Sstevel@tonic-gate vmem_putseg(vmp, vsp);
5430Sstevel@tonic-gate }
5440Sstevel@tonic-gate
5450Sstevel@tonic-gate /*
5460Sstevel@tonic-gate * Add the span [vaddr, vaddr + size) to vmp and update kstats.
5470Sstevel@tonic-gate */
5480Sstevel@tonic-gate static vmem_seg_t *
vmem_span_create(vmem_t * vmp,void * vaddr,size_t size,uint8_t import)5490Sstevel@tonic-gate vmem_span_create(vmem_t *vmp, void *vaddr, size_t size, uint8_t import)
5500Sstevel@tonic-gate {
5510Sstevel@tonic-gate vmem_seg_t *newseg, *span;
5520Sstevel@tonic-gate uintptr_t start = (uintptr_t)vaddr;
5530Sstevel@tonic-gate uintptr_t end = start + size;
5540Sstevel@tonic-gate
5550Sstevel@tonic-gate ASSERT(MUTEX_HELD(&vmp->vm_lock));
5560Sstevel@tonic-gate
5570Sstevel@tonic-gate if ((start | end) & (vmp->vm_quantum - 1))
5580Sstevel@tonic-gate panic("vmem_span_create(%p, %p, %lu): misaligned",
5597240Srh87107 (void *)vmp, vaddr, size);
5600Sstevel@tonic-gate
5610Sstevel@tonic-gate span = vmem_seg_create(vmp, vmp->vm_seg0.vs_aprev, start, end);
5620Sstevel@tonic-gate span->vs_type = VMEM_SPAN;
5630Sstevel@tonic-gate span->vs_import = import;
5640Sstevel@tonic-gate VMEM_INSERT(vmp->vm_seg0.vs_kprev, span, k);
5650Sstevel@tonic-gate
5660Sstevel@tonic-gate newseg = vmem_seg_create(vmp, span, start, end);
5670Sstevel@tonic-gate vmem_freelist_insert(vmp, newseg);
5680Sstevel@tonic-gate
5690Sstevel@tonic-gate if (import)
5700Sstevel@tonic-gate vmp->vm_kstat.vk_mem_import.value.ui64 += size;
5710Sstevel@tonic-gate vmp->vm_kstat.vk_mem_total.value.ui64 += size;
5720Sstevel@tonic-gate
5730Sstevel@tonic-gate return (newseg);
5740Sstevel@tonic-gate }
5750Sstevel@tonic-gate
5760Sstevel@tonic-gate /*
5770Sstevel@tonic-gate * Remove span vsp from vmp and update kstats.
5780Sstevel@tonic-gate */
5790Sstevel@tonic-gate static void
vmem_span_destroy(vmem_t * vmp,vmem_seg_t * vsp)5800Sstevel@tonic-gate vmem_span_destroy(vmem_t *vmp, vmem_seg_t *vsp)
5810Sstevel@tonic-gate {
5820Sstevel@tonic-gate vmem_seg_t *span = vsp->vs_aprev;
5830Sstevel@tonic-gate size_t size = VS_SIZE(vsp);
5840Sstevel@tonic-gate
5850Sstevel@tonic-gate ASSERT(MUTEX_HELD(&vmp->vm_lock));
5860Sstevel@tonic-gate ASSERT(span->vs_type == VMEM_SPAN);
5870Sstevel@tonic-gate
5880Sstevel@tonic-gate if (span->vs_import)
5890Sstevel@tonic-gate vmp->vm_kstat.vk_mem_import.value.ui64 -= size;
5900Sstevel@tonic-gate vmp->vm_kstat.vk_mem_total.value.ui64 -= size;
5910Sstevel@tonic-gate
5920Sstevel@tonic-gate VMEM_DELETE(span, k);
5930Sstevel@tonic-gate
5940Sstevel@tonic-gate vmem_seg_destroy(vmp, vsp);
5950Sstevel@tonic-gate vmem_seg_destroy(vmp, span);
5960Sstevel@tonic-gate }
5970Sstevel@tonic-gate
5980Sstevel@tonic-gate /*
5990Sstevel@tonic-gate * Allocate the subrange [addr, addr + size) from segment vsp.
6000Sstevel@tonic-gate * If there are leftovers on either side, place them on the freelist.
6010Sstevel@tonic-gate * Returns a pointer to the segment representing [addr, addr + size).
6020Sstevel@tonic-gate */
6030Sstevel@tonic-gate static vmem_seg_t *
vmem_seg_alloc(vmem_t * vmp,vmem_seg_t * vsp,uintptr_t addr,size_t size)6040Sstevel@tonic-gate vmem_seg_alloc(vmem_t *vmp, vmem_seg_t *vsp, uintptr_t addr, size_t size)
6050Sstevel@tonic-gate {
6060Sstevel@tonic-gate uintptr_t vs_start = vsp->vs_start;
6070Sstevel@tonic-gate uintptr_t vs_end = vsp->vs_end;
6080Sstevel@tonic-gate size_t vs_size = vs_end - vs_start;
6090Sstevel@tonic-gate size_t realsize = P2ROUNDUP(size, vmp->vm_quantum);
6100Sstevel@tonic-gate uintptr_t addr_end = addr + realsize;
6110Sstevel@tonic-gate
6120Sstevel@tonic-gate ASSERT(P2PHASE(vs_start, vmp->vm_quantum) == 0);
6130Sstevel@tonic-gate ASSERT(P2PHASE(addr, vmp->vm_quantum) == 0);
6140Sstevel@tonic-gate ASSERT(vsp->vs_type == VMEM_FREE);
6150Sstevel@tonic-gate ASSERT(addr >= vs_start && addr_end - 1 <= vs_end - 1);
6160Sstevel@tonic-gate ASSERT(addr - 1 <= addr_end - 1);
6170Sstevel@tonic-gate
6180Sstevel@tonic-gate /*
6190Sstevel@tonic-gate * If we're allocating from the start of the segment, and the
6200Sstevel@tonic-gate * remainder will be on the same freelist, we can save quite
6210Sstevel@tonic-gate * a bit of work.
6220Sstevel@tonic-gate */
6230Sstevel@tonic-gate if (P2SAMEHIGHBIT(vs_size, vs_size - realsize) && addr == vs_start) {
6240Sstevel@tonic-gate ASSERT(highbit(vs_size) == highbit(vs_size - realsize));
6250Sstevel@tonic-gate vsp->vs_start = addr_end;
6260Sstevel@tonic-gate vsp = vmem_seg_create(vmp, vsp->vs_aprev, addr, addr + size);
6270Sstevel@tonic-gate vmem_hash_insert(vmp, vsp);
6280Sstevel@tonic-gate return (vsp);
6290Sstevel@tonic-gate }
6300Sstevel@tonic-gate
6310Sstevel@tonic-gate vmem_freelist_delete(vmp, vsp);
6320Sstevel@tonic-gate
6330Sstevel@tonic-gate if (vs_end != addr_end)
6340Sstevel@tonic-gate vmem_freelist_insert(vmp,
6350Sstevel@tonic-gate vmem_seg_create(vmp, vsp, addr_end, vs_end));
6360Sstevel@tonic-gate
6370Sstevel@tonic-gate if (vs_start != addr)
6380Sstevel@tonic-gate vmem_freelist_insert(vmp,
6390Sstevel@tonic-gate vmem_seg_create(vmp, vsp->vs_aprev, vs_start, addr));
6400Sstevel@tonic-gate
6410Sstevel@tonic-gate vsp->vs_start = addr;
6420Sstevel@tonic-gate vsp->vs_end = addr + size;
6430Sstevel@tonic-gate
6440Sstevel@tonic-gate vmem_hash_insert(vmp, vsp);
6450Sstevel@tonic-gate return (vsp);
6460Sstevel@tonic-gate }
6470Sstevel@tonic-gate
6480Sstevel@tonic-gate /*
6490Sstevel@tonic-gate * Returns 1 if we are populating, 0 otherwise.
6500Sstevel@tonic-gate * Call it if we want to prevent recursion from HAT.
6510Sstevel@tonic-gate */
6520Sstevel@tonic-gate int
vmem_is_populator()6530Sstevel@tonic-gate vmem_is_populator()
6540Sstevel@tonic-gate {
6550Sstevel@tonic-gate return (mutex_owner(&vmem_sleep_lock) == curthread ||
6560Sstevel@tonic-gate mutex_owner(&vmem_nosleep_lock) == curthread ||
6576058Sbonwick mutex_owner(&vmem_pushpage_lock) == curthread ||
6580Sstevel@tonic-gate mutex_owner(&vmem_panic_lock) == curthread);
6590Sstevel@tonic-gate }
6600Sstevel@tonic-gate
6610Sstevel@tonic-gate /*
6620Sstevel@tonic-gate * Populate vmp's segfree list with VMEM_MINFREE vmem_seg_t structures.
6630Sstevel@tonic-gate */
6640Sstevel@tonic-gate static int
vmem_populate(vmem_t * vmp,int vmflag)6650Sstevel@tonic-gate vmem_populate(vmem_t *vmp, int vmflag)
6660Sstevel@tonic-gate {
6670Sstevel@tonic-gate char *p;
6680Sstevel@tonic-gate vmem_seg_t *vsp;
6690Sstevel@tonic-gate ssize_t nseg;
6700Sstevel@tonic-gate size_t size;
6710Sstevel@tonic-gate kmutex_t *lp;
6720Sstevel@tonic-gate int i;
6730Sstevel@tonic-gate
6740Sstevel@tonic-gate while (vmp->vm_nsegfree < VMEM_MINFREE &&
6750Sstevel@tonic-gate (vsp = vmem_getseg_global()) != NULL)
6760Sstevel@tonic-gate vmem_putseg(vmp, vsp);
6770Sstevel@tonic-gate
6780Sstevel@tonic-gate if (vmp->vm_nsegfree >= VMEM_MINFREE)
6790Sstevel@tonic-gate return (1);
6800Sstevel@tonic-gate
6810Sstevel@tonic-gate /*
6820Sstevel@tonic-gate * If we're already populating, tap the reserve.
6830Sstevel@tonic-gate */
6840Sstevel@tonic-gate if (vmem_is_populator()) {
6850Sstevel@tonic-gate ASSERT(vmp->vm_cflags & VMC_POPULATOR);
6860Sstevel@tonic-gate return (1);
6870Sstevel@tonic-gate }
6880Sstevel@tonic-gate
6890Sstevel@tonic-gate mutex_exit(&vmp->vm_lock);
6900Sstevel@tonic-gate
6910Sstevel@tonic-gate if (panic_thread == curthread)
6920Sstevel@tonic-gate lp = &vmem_panic_lock;
6930Sstevel@tonic-gate else if (vmflag & VM_NOSLEEP)
6940Sstevel@tonic-gate lp = &vmem_nosleep_lock;
6956058Sbonwick else if (vmflag & VM_PUSHPAGE)
6966058Sbonwick lp = &vmem_pushpage_lock;
6970Sstevel@tonic-gate else
6980Sstevel@tonic-gate lp = &vmem_sleep_lock;
6990Sstevel@tonic-gate
7000Sstevel@tonic-gate mutex_enter(lp);
7010Sstevel@tonic-gate
7020Sstevel@tonic-gate nseg = VMEM_MINFREE + vmem_populators * VMEM_POPULATE_RESERVE;
7030Sstevel@tonic-gate size = P2ROUNDUP(nseg * vmem_seg_size, vmem_seg_arena->vm_quantum);
7040Sstevel@tonic-gate nseg = size / vmem_seg_size;
7050Sstevel@tonic-gate
7060Sstevel@tonic-gate /*
7070Sstevel@tonic-gate * The following vmem_alloc() may need to populate vmem_seg_arena
7080Sstevel@tonic-gate * and all the things it imports from. When doing so, it will tap
7090Sstevel@tonic-gate * each arena's reserve to prevent recursion (see the block comment
7100Sstevel@tonic-gate * above the definition of VMEM_POPULATE_RESERVE).
7110Sstevel@tonic-gate */
7120Sstevel@tonic-gate p = vmem_alloc(vmem_seg_arena, size, vmflag & VM_KMFLAGS);
7130Sstevel@tonic-gate if (p == NULL) {
7140Sstevel@tonic-gate mutex_exit(lp);
7150Sstevel@tonic-gate mutex_enter(&vmp->vm_lock);
7160Sstevel@tonic-gate vmp->vm_kstat.vk_populate_fail.value.ui64++;
7170Sstevel@tonic-gate return (0);
7180Sstevel@tonic-gate }
7190Sstevel@tonic-gate
7200Sstevel@tonic-gate /*
7210Sstevel@tonic-gate * Restock the arenas that may have been depleted during population.
7220Sstevel@tonic-gate */
7230Sstevel@tonic-gate for (i = 0; i < vmem_populators; i++) {
7240Sstevel@tonic-gate mutex_enter(&vmem_populator[i]->vm_lock);
7250Sstevel@tonic-gate while (vmem_populator[i]->vm_nsegfree < VMEM_POPULATE_RESERVE)
7260Sstevel@tonic-gate vmem_putseg(vmem_populator[i],
7270Sstevel@tonic-gate (vmem_seg_t *)(p + --nseg * vmem_seg_size));
7280Sstevel@tonic-gate mutex_exit(&vmem_populator[i]->vm_lock);
7290Sstevel@tonic-gate }
7300Sstevel@tonic-gate
7310Sstevel@tonic-gate mutex_exit(lp);
7320Sstevel@tonic-gate mutex_enter(&vmp->vm_lock);
7330Sstevel@tonic-gate
7340Sstevel@tonic-gate /*
7350Sstevel@tonic-gate * Now take our own segments.
7360Sstevel@tonic-gate */
7370Sstevel@tonic-gate ASSERT(nseg >= VMEM_MINFREE);
7380Sstevel@tonic-gate while (vmp->vm_nsegfree < VMEM_MINFREE)
7390Sstevel@tonic-gate vmem_putseg(vmp, (vmem_seg_t *)(p + --nseg * vmem_seg_size));
7400Sstevel@tonic-gate
7410Sstevel@tonic-gate /*
7420Sstevel@tonic-gate * Give the remainder to charity.
7430Sstevel@tonic-gate */
7440Sstevel@tonic-gate while (nseg > 0)
7450Sstevel@tonic-gate vmem_putseg_global((vmem_seg_t *)(p + --nseg * vmem_seg_size));
7460Sstevel@tonic-gate
7470Sstevel@tonic-gate return (1);
7480Sstevel@tonic-gate }
7490Sstevel@tonic-gate
7500Sstevel@tonic-gate /*
7510Sstevel@tonic-gate * Advance a walker from its previous position to 'afterme'.
7520Sstevel@tonic-gate * Note: may drop and reacquire vmp->vm_lock.
7530Sstevel@tonic-gate */
7540Sstevel@tonic-gate static void
vmem_advance(vmem_t * vmp,vmem_seg_t * walker,vmem_seg_t * afterme)7550Sstevel@tonic-gate vmem_advance(vmem_t *vmp, vmem_seg_t *walker, vmem_seg_t *afterme)
7560Sstevel@tonic-gate {
7570Sstevel@tonic-gate vmem_seg_t *vprev = walker->vs_aprev;
7580Sstevel@tonic-gate vmem_seg_t *vnext = walker->vs_anext;
7590Sstevel@tonic-gate vmem_seg_t *vsp = NULL;
7600Sstevel@tonic-gate
7610Sstevel@tonic-gate VMEM_DELETE(walker, a);
7620Sstevel@tonic-gate
7630Sstevel@tonic-gate if (afterme != NULL)
7640Sstevel@tonic-gate VMEM_INSERT(afterme, walker, a);
7650Sstevel@tonic-gate
7660Sstevel@tonic-gate /*
7670Sstevel@tonic-gate * The walker segment's presence may have prevented its neighbors
7680Sstevel@tonic-gate * from coalescing. If so, coalesce them now.
7690Sstevel@tonic-gate */
7700Sstevel@tonic-gate if (vprev->vs_type == VMEM_FREE) {
7710Sstevel@tonic-gate if (vnext->vs_type == VMEM_FREE) {
7720Sstevel@tonic-gate ASSERT(vprev->vs_end == vnext->vs_start);
7730Sstevel@tonic-gate vmem_freelist_delete(vmp, vnext);
7740Sstevel@tonic-gate vmem_freelist_delete(vmp, vprev);
7750Sstevel@tonic-gate vprev->vs_end = vnext->vs_end;
7760Sstevel@tonic-gate vmem_freelist_insert(vmp, vprev);
7770Sstevel@tonic-gate vmem_seg_destroy(vmp, vnext);
7780Sstevel@tonic-gate }
7790Sstevel@tonic-gate vsp = vprev;
7800Sstevel@tonic-gate } else if (vnext->vs_type == VMEM_FREE) {
7810Sstevel@tonic-gate vsp = vnext;
7820Sstevel@tonic-gate }
7830Sstevel@tonic-gate
7840Sstevel@tonic-gate /*
7850Sstevel@tonic-gate * vsp could represent a complete imported span,
7860Sstevel@tonic-gate * in which case we must return it to the source.
7870Sstevel@tonic-gate */
7880Sstevel@tonic-gate if (vsp != NULL && vsp->vs_aprev->vs_import &&
7890Sstevel@tonic-gate vmp->vm_source_free != NULL &&
7900Sstevel@tonic-gate vsp->vs_aprev->vs_type == VMEM_SPAN &&
7910Sstevel@tonic-gate vsp->vs_anext->vs_type == VMEM_SPAN) {
7920Sstevel@tonic-gate void *vaddr = (void *)vsp->vs_start;
7930Sstevel@tonic-gate size_t size = VS_SIZE(vsp);
7940Sstevel@tonic-gate ASSERT(size == VS_SIZE(vsp->vs_aprev));
7950Sstevel@tonic-gate vmem_freelist_delete(vmp, vsp);
7960Sstevel@tonic-gate vmem_span_destroy(vmp, vsp);
7970Sstevel@tonic-gate mutex_exit(&vmp->vm_lock);
7980Sstevel@tonic-gate vmp->vm_source_free(vmp->vm_source, vaddr, size);
7990Sstevel@tonic-gate mutex_enter(&vmp->vm_lock);
8000Sstevel@tonic-gate }
8010Sstevel@tonic-gate }
8020Sstevel@tonic-gate
8030Sstevel@tonic-gate /*
8040Sstevel@tonic-gate * VM_NEXTFIT allocations deliberately cycle through all virtual addresses
8050Sstevel@tonic-gate * in an arena, so that we avoid reusing addresses for as long as possible.
8060Sstevel@tonic-gate * This helps to catch used-after-freed bugs. It's also the perfect policy
8070Sstevel@tonic-gate * for allocating things like process IDs, where we want to cycle through
8080Sstevel@tonic-gate * all values in order.
8090Sstevel@tonic-gate */
8100Sstevel@tonic-gate static void *
vmem_nextfit_alloc(vmem_t * vmp,size_t size,int vmflag)8110Sstevel@tonic-gate vmem_nextfit_alloc(vmem_t *vmp, size_t size, int vmflag)
8120Sstevel@tonic-gate {
8130Sstevel@tonic-gate vmem_seg_t *vsp, *rotor;
8140Sstevel@tonic-gate uintptr_t addr;
8150Sstevel@tonic-gate size_t realsize = P2ROUNDUP(size, vmp->vm_quantum);
8160Sstevel@tonic-gate size_t vs_size;
8170Sstevel@tonic-gate
8180Sstevel@tonic-gate mutex_enter(&vmp->vm_lock);
8190Sstevel@tonic-gate
8200Sstevel@tonic-gate if (vmp->vm_nsegfree < VMEM_MINFREE && !vmem_populate(vmp, vmflag)) {
8210Sstevel@tonic-gate mutex_exit(&vmp->vm_lock);
8220Sstevel@tonic-gate return (NULL);
8230Sstevel@tonic-gate }
8240Sstevel@tonic-gate
8250Sstevel@tonic-gate /*
8260Sstevel@tonic-gate * The common case is that the segment right after the rotor is free,
8270Sstevel@tonic-gate * and large enough that extracting 'size' bytes won't change which
8280Sstevel@tonic-gate * freelist it's on. In this case we can avoid a *lot* of work.
8290Sstevel@tonic-gate * Instead of the normal vmem_seg_alloc(), we just advance the start
8300Sstevel@tonic-gate * address of the victim segment. Instead of moving the rotor, we
8310Sstevel@tonic-gate * create the new segment structure *behind the rotor*, which has
8320Sstevel@tonic-gate * the same effect. And finally, we know we don't have to coalesce
8330Sstevel@tonic-gate * the rotor's neighbors because the new segment lies between them.
8340Sstevel@tonic-gate */
8350Sstevel@tonic-gate rotor = &vmp->vm_rotor;
8360Sstevel@tonic-gate vsp = rotor->vs_anext;
8370Sstevel@tonic-gate if (vsp->vs_type == VMEM_FREE && (vs_size = VS_SIZE(vsp)) > realsize &&
8380Sstevel@tonic-gate P2SAMEHIGHBIT(vs_size, vs_size - realsize)) {
8390Sstevel@tonic-gate ASSERT(highbit(vs_size) == highbit(vs_size - realsize));
8400Sstevel@tonic-gate addr = vsp->vs_start;
8410Sstevel@tonic-gate vsp->vs_start = addr + realsize;
8420Sstevel@tonic-gate vmem_hash_insert(vmp,
8430Sstevel@tonic-gate vmem_seg_create(vmp, rotor->vs_aprev, addr, addr + size));
8440Sstevel@tonic-gate mutex_exit(&vmp->vm_lock);
8450Sstevel@tonic-gate return ((void *)addr);
8460Sstevel@tonic-gate }
8470Sstevel@tonic-gate
8480Sstevel@tonic-gate /*
8490Sstevel@tonic-gate * Starting at the rotor, look for a segment large enough to
8500Sstevel@tonic-gate * satisfy the allocation.
8510Sstevel@tonic-gate */
8520Sstevel@tonic-gate for (;;) {
8530Sstevel@tonic-gate vmp->vm_kstat.vk_search.value.ui64++;
8540Sstevel@tonic-gate if (vsp->vs_type == VMEM_FREE && VS_SIZE(vsp) >= size)
8550Sstevel@tonic-gate break;
8560Sstevel@tonic-gate vsp = vsp->vs_anext;
8570Sstevel@tonic-gate if (vsp == rotor) {
8580Sstevel@tonic-gate /*
8590Sstevel@tonic-gate * We've come full circle. One possibility is that the
8600Sstevel@tonic-gate * there's actually enough space, but the rotor itself
8610Sstevel@tonic-gate * is preventing the allocation from succeeding because
8620Sstevel@tonic-gate * it's sitting between two free segments. Therefore,
8630Sstevel@tonic-gate * we advance the rotor and see if that liberates a
8640Sstevel@tonic-gate * suitable segment.
8650Sstevel@tonic-gate */
8660Sstevel@tonic-gate vmem_advance(vmp, rotor, rotor->vs_anext);
8670Sstevel@tonic-gate vsp = rotor->vs_aprev;
8680Sstevel@tonic-gate if (vsp->vs_type == VMEM_FREE && VS_SIZE(vsp) >= size)
8690Sstevel@tonic-gate break;
8700Sstevel@tonic-gate /*
8710Sstevel@tonic-gate * If there's a lower arena we can import from, or it's
8720Sstevel@tonic-gate * a VM_NOSLEEP allocation, let vmem_xalloc() handle it.
8730Sstevel@tonic-gate * Otherwise, wait until another thread frees something.
8740Sstevel@tonic-gate */
8750Sstevel@tonic-gate if (vmp->vm_source_alloc != NULL ||
8760Sstevel@tonic-gate (vmflag & VM_NOSLEEP)) {
8770Sstevel@tonic-gate mutex_exit(&vmp->vm_lock);
8780Sstevel@tonic-gate return (vmem_xalloc(vmp, size, vmp->vm_quantum,
8790Sstevel@tonic-gate 0, 0, NULL, NULL, vmflag & VM_KMFLAGS));
8800Sstevel@tonic-gate }
8810Sstevel@tonic-gate vmp->vm_kstat.vk_wait.value.ui64++;
8820Sstevel@tonic-gate cv_wait(&vmp->vm_cv, &vmp->vm_lock);
8830Sstevel@tonic-gate vsp = rotor->vs_anext;
8840Sstevel@tonic-gate }
8850Sstevel@tonic-gate }
8860Sstevel@tonic-gate
8870Sstevel@tonic-gate /*
8880Sstevel@tonic-gate * We found a segment. Extract enough space to satisfy the allocation.
8890Sstevel@tonic-gate */
8900Sstevel@tonic-gate addr = vsp->vs_start;
8910Sstevel@tonic-gate vsp = vmem_seg_alloc(vmp, vsp, addr, size);
8920Sstevel@tonic-gate ASSERT(vsp->vs_type == VMEM_ALLOC &&
8930Sstevel@tonic-gate vsp->vs_start == addr && vsp->vs_end == addr + size);
8940Sstevel@tonic-gate
8950Sstevel@tonic-gate /*
8960Sstevel@tonic-gate * Advance the rotor to right after the newly-allocated segment.
8970Sstevel@tonic-gate * That's where the next VM_NEXTFIT allocation will begin searching.
8980Sstevel@tonic-gate */
8990Sstevel@tonic-gate vmem_advance(vmp, rotor, vsp);
9000Sstevel@tonic-gate mutex_exit(&vmp->vm_lock);
9010Sstevel@tonic-gate return ((void *)addr);
9020Sstevel@tonic-gate }
9030Sstevel@tonic-gate
9040Sstevel@tonic-gate /*
9050Sstevel@tonic-gate * Checks if vmp is guaranteed to have a size-byte buffer somewhere on its
9060Sstevel@tonic-gate * freelist. If size is not a power-of-2, it can return a false-negative.
9070Sstevel@tonic-gate *
9080Sstevel@tonic-gate * Used to decide if a newly imported span is superfluous after re-acquiring
9090Sstevel@tonic-gate * the arena lock.
9100Sstevel@tonic-gate */
9110Sstevel@tonic-gate static int
vmem_canalloc(vmem_t * vmp,size_t size)9120Sstevel@tonic-gate vmem_canalloc(vmem_t *vmp, size_t size)
9130Sstevel@tonic-gate {
9140Sstevel@tonic-gate int hb;
9150Sstevel@tonic-gate int flist = 0;
9160Sstevel@tonic-gate ASSERT(MUTEX_HELD(&vmp->vm_lock));
9170Sstevel@tonic-gate
9180Sstevel@tonic-gate if ((size & (size - 1)) == 0)
9190Sstevel@tonic-gate flist = lowbit(P2ALIGN(vmp->vm_freemap, size));
9200Sstevel@tonic-gate else if ((hb = highbit(size)) < VMEM_FREELISTS)
9210Sstevel@tonic-gate flist = lowbit(P2ALIGN(vmp->vm_freemap, 1UL << hb));
9220Sstevel@tonic-gate
9230Sstevel@tonic-gate return (flist);
9240Sstevel@tonic-gate }
9250Sstevel@tonic-gate
9260Sstevel@tonic-gate /*
9270Sstevel@tonic-gate * Allocate size bytes at offset phase from an align boundary such that the
9280Sstevel@tonic-gate * resulting segment [addr, addr + size) is a subset of [minaddr, maxaddr)
9290Sstevel@tonic-gate * that does not straddle a nocross-aligned boundary.
9300Sstevel@tonic-gate */
9310Sstevel@tonic-gate void *
vmem_xalloc(vmem_t * vmp,size_t size,size_t align_arg,size_t phase,size_t nocross,void * minaddr,void * maxaddr,int vmflag)9320Sstevel@tonic-gate vmem_xalloc(vmem_t *vmp, size_t size, size_t align_arg, size_t phase,
9330Sstevel@tonic-gate size_t nocross, void *minaddr, void *maxaddr, int vmflag)
9340Sstevel@tonic-gate {
9350Sstevel@tonic-gate vmem_seg_t *vsp;
9360Sstevel@tonic-gate vmem_seg_t *vbest = NULL;
9370Sstevel@tonic-gate uintptr_t addr, taddr, start, end;
9380Sstevel@tonic-gate uintptr_t align = (align_arg != 0) ? align_arg : vmp->vm_quantum;
9390Sstevel@tonic-gate void *vaddr, *xvaddr = NULL;
9400Sstevel@tonic-gate size_t xsize;
9410Sstevel@tonic-gate int hb, flist, resv;
9420Sstevel@tonic-gate uint32_t mtbf;
9430Sstevel@tonic-gate
9440Sstevel@tonic-gate if ((align | phase | nocross) & (vmp->vm_quantum - 1))
9450Sstevel@tonic-gate panic("vmem_xalloc(%p, %lu, %lu, %lu, %lu, %p, %p, %x): "
9460Sstevel@tonic-gate "parameters not vm_quantum aligned",
9470Sstevel@tonic-gate (void *)vmp, size, align_arg, phase, nocross,
9480Sstevel@tonic-gate minaddr, maxaddr, vmflag);
9490Sstevel@tonic-gate
9500Sstevel@tonic-gate if (nocross != 0 &&
9510Sstevel@tonic-gate (align > nocross || P2ROUNDUP(phase + size, align) > nocross))
9520Sstevel@tonic-gate panic("vmem_xalloc(%p, %lu, %lu, %lu, %lu, %p, %p, %x): "
9530Sstevel@tonic-gate "overconstrained allocation",
9540Sstevel@tonic-gate (void *)vmp, size, align_arg, phase, nocross,
9550Sstevel@tonic-gate minaddr, maxaddr, vmflag);
9560Sstevel@tonic-gate
9570Sstevel@tonic-gate if (phase >= align || (align & (align - 1)) != 0 ||
9580Sstevel@tonic-gate (nocross & (nocross - 1)) != 0)
9590Sstevel@tonic-gate panic("vmem_xalloc(%p, %lu, %lu, %lu, %lu, %p, %p, %x): "
9600Sstevel@tonic-gate "parameters inconsistent or invalid",
9610Sstevel@tonic-gate (void *)vmp, size, align_arg, phase, nocross,
9620Sstevel@tonic-gate minaddr, maxaddr, vmflag);
9630Sstevel@tonic-gate
9640Sstevel@tonic-gate if ((mtbf = vmem_mtbf | vmp->vm_mtbf) != 0 && gethrtime() % mtbf == 0 &&
9650Sstevel@tonic-gate (vmflag & (VM_NOSLEEP | VM_PANIC)) == VM_NOSLEEP)
9660Sstevel@tonic-gate return (NULL);
9670Sstevel@tonic-gate
9680Sstevel@tonic-gate mutex_enter(&vmp->vm_lock);
9690Sstevel@tonic-gate for (;;) {
9700Sstevel@tonic-gate if (vmp->vm_nsegfree < VMEM_MINFREE &&
9710Sstevel@tonic-gate !vmem_populate(vmp, vmflag))
9720Sstevel@tonic-gate break;
9730Sstevel@tonic-gate do_alloc:
9740Sstevel@tonic-gate /*
9750Sstevel@tonic-gate * highbit() returns the highest bit + 1, which is exactly
9760Sstevel@tonic-gate * what we want: we want to search the first freelist whose
9770Sstevel@tonic-gate * members are *definitely* large enough to satisfy our
9780Sstevel@tonic-gate * allocation. However, there are certain cases in which we
9790Sstevel@tonic-gate * want to look at the next-smallest freelist (which *might*
9800Sstevel@tonic-gate * be able to satisfy the allocation):
9810Sstevel@tonic-gate *
9820Sstevel@tonic-gate * (1) The size is exactly a power of 2, in which case
9830Sstevel@tonic-gate * the smaller freelist is always big enough;
9840Sstevel@tonic-gate *
9850Sstevel@tonic-gate * (2) All other freelists are empty;
9860Sstevel@tonic-gate *
9870Sstevel@tonic-gate * (3) We're in the highest possible freelist, which is
9880Sstevel@tonic-gate * always empty (e.g. the 4GB freelist on 32-bit systems);
9890Sstevel@tonic-gate *
9900Sstevel@tonic-gate * (4) We're doing a best-fit or first-fit allocation.
9910Sstevel@tonic-gate */
9920Sstevel@tonic-gate if ((size & (size - 1)) == 0) {
9930Sstevel@tonic-gate flist = lowbit(P2ALIGN(vmp->vm_freemap, size));
9940Sstevel@tonic-gate } else {
9950Sstevel@tonic-gate hb = highbit(size);
9960Sstevel@tonic-gate if ((vmp->vm_freemap >> hb) == 0 ||
9970Sstevel@tonic-gate hb == VMEM_FREELISTS ||
9980Sstevel@tonic-gate (vmflag & (VM_BESTFIT | VM_FIRSTFIT)))
9990Sstevel@tonic-gate hb--;
10000Sstevel@tonic-gate flist = lowbit(P2ALIGN(vmp->vm_freemap, 1UL << hb));
10010Sstevel@tonic-gate }
10020Sstevel@tonic-gate
10030Sstevel@tonic-gate for (vbest = NULL, vsp = (flist == 0) ? NULL :
10040Sstevel@tonic-gate vmp->vm_freelist[flist - 1].vs_knext;
10050Sstevel@tonic-gate vsp != NULL; vsp = vsp->vs_knext) {
10060Sstevel@tonic-gate vmp->vm_kstat.vk_search.value.ui64++;
10070Sstevel@tonic-gate if (vsp->vs_start == 0) {
10080Sstevel@tonic-gate /*
10090Sstevel@tonic-gate * We're moving up to a larger freelist,
10100Sstevel@tonic-gate * so if we've already found a candidate,
10110Sstevel@tonic-gate * the fit can't possibly get any better.
10120Sstevel@tonic-gate */
10130Sstevel@tonic-gate if (vbest != NULL)
10140Sstevel@tonic-gate break;
10150Sstevel@tonic-gate /*
10160Sstevel@tonic-gate * Find the next non-empty freelist.
10170Sstevel@tonic-gate */
10180Sstevel@tonic-gate flist = lowbit(P2ALIGN(vmp->vm_freemap,
10190Sstevel@tonic-gate VS_SIZE(vsp)));
10200Sstevel@tonic-gate if (flist-- == 0)
10210Sstevel@tonic-gate break;
10220Sstevel@tonic-gate vsp = (vmem_seg_t *)&vmp->vm_freelist[flist];
10230Sstevel@tonic-gate ASSERT(vsp->vs_knext->vs_type == VMEM_FREE);
10240Sstevel@tonic-gate continue;
10250Sstevel@tonic-gate }
10260Sstevel@tonic-gate if (vsp->vs_end - 1 < (uintptr_t)minaddr)
10270Sstevel@tonic-gate continue;
10280Sstevel@tonic-gate if (vsp->vs_start > (uintptr_t)maxaddr - 1)
10290Sstevel@tonic-gate continue;
10300Sstevel@tonic-gate start = MAX(vsp->vs_start, (uintptr_t)minaddr);
10310Sstevel@tonic-gate end = MIN(vsp->vs_end - 1, (uintptr_t)maxaddr - 1) + 1;
10320Sstevel@tonic-gate taddr = P2PHASEUP(start, align, phase);
10337837SMatthew.Ahrens@Sun.COM if (P2BOUNDARY(taddr, size, nocross))
10340Sstevel@tonic-gate taddr +=
10350Sstevel@tonic-gate P2ROUNDUP(P2NPHASE(taddr, nocross), align);
10360Sstevel@tonic-gate if ((taddr - start) + size > end - start ||
10370Sstevel@tonic-gate (vbest != NULL && VS_SIZE(vsp) >= VS_SIZE(vbest)))
10380Sstevel@tonic-gate continue;
10390Sstevel@tonic-gate vbest = vsp;
10400Sstevel@tonic-gate addr = taddr;
10410Sstevel@tonic-gate if (!(vmflag & VM_BESTFIT) || VS_SIZE(vbest) == size)
10420Sstevel@tonic-gate break;
10430Sstevel@tonic-gate }
10440Sstevel@tonic-gate if (vbest != NULL)
10450Sstevel@tonic-gate break;
10460Sstevel@tonic-gate ASSERT(xvaddr == NULL);
10470Sstevel@tonic-gate if (size == 0)
10480Sstevel@tonic-gate panic("vmem_xalloc(): size == 0");
10490Sstevel@tonic-gate if (vmp->vm_source_alloc != NULL && nocross == 0 &&
10500Sstevel@tonic-gate minaddr == NULL && maxaddr == NULL) {
10510Sstevel@tonic-gate size_t aneeded, asize;
10520Sstevel@tonic-gate size_t aquantum = MAX(vmp->vm_quantum,
10530Sstevel@tonic-gate vmp->vm_source->vm_quantum);
10540Sstevel@tonic-gate size_t aphase = phase;
10554204Sha137994 if ((align > aquantum) &&
10564204Sha137994 !(vmp->vm_cflags & VMC_XALIGN)) {
10570Sstevel@tonic-gate aphase = (P2PHASE(phase, aquantum) != 0) ?
10580Sstevel@tonic-gate align - vmp->vm_quantum : align - aquantum;
10590Sstevel@tonic-gate ASSERT(aphase >= phase);
10600Sstevel@tonic-gate }
10610Sstevel@tonic-gate aneeded = MAX(size + aphase, vmp->vm_min_import);
10620Sstevel@tonic-gate asize = P2ROUNDUP(aneeded, aquantum);
10630Sstevel@tonic-gate
10640Sstevel@tonic-gate /*
10650Sstevel@tonic-gate * Determine how many segment structures we'll consume.
10660Sstevel@tonic-gate * The calculation must be precise because if we're
10670Sstevel@tonic-gate * here on behalf of vmem_populate(), we are taking
10680Sstevel@tonic-gate * segments from a very limited reserve.
10690Sstevel@tonic-gate */
10700Sstevel@tonic-gate if (size == asize && !(vmp->vm_cflags & VMC_XALLOC))
10710Sstevel@tonic-gate resv = VMEM_SEGS_PER_SPAN_CREATE +
10720Sstevel@tonic-gate VMEM_SEGS_PER_EXACT_ALLOC;
10730Sstevel@tonic-gate else if (phase == 0 &&
10740Sstevel@tonic-gate align <= vmp->vm_source->vm_quantum)
10750Sstevel@tonic-gate resv = VMEM_SEGS_PER_SPAN_CREATE +
10760Sstevel@tonic-gate VMEM_SEGS_PER_LEFT_ALLOC;
10770Sstevel@tonic-gate else
10780Sstevel@tonic-gate resv = VMEM_SEGS_PER_ALLOC_MAX;
10790Sstevel@tonic-gate
10800Sstevel@tonic-gate ASSERT(vmp->vm_nsegfree >= resv);
10810Sstevel@tonic-gate vmp->vm_nsegfree -= resv; /* reserve our segs */
10820Sstevel@tonic-gate mutex_exit(&vmp->vm_lock);
10830Sstevel@tonic-gate if (vmp->vm_cflags & VMC_XALLOC) {
10840Sstevel@tonic-gate size_t oasize = asize;
10850Sstevel@tonic-gate vaddr = ((vmem_ximport_t *)
10860Sstevel@tonic-gate vmp->vm_source_alloc)(vmp->vm_source,
10874204Sha137994 &asize, align, vmflag & VM_KMFLAGS);
10880Sstevel@tonic-gate ASSERT(asize >= oasize);
10890Sstevel@tonic-gate ASSERT(P2PHASE(asize,
10900Sstevel@tonic-gate vmp->vm_source->vm_quantum) == 0);
10914204Sha137994 ASSERT(!(vmp->vm_cflags & VMC_XALIGN) ||
10924204Sha137994 IS_P2ALIGNED(vaddr, align));
10930Sstevel@tonic-gate } else {
10940Sstevel@tonic-gate vaddr = vmp->vm_source_alloc(vmp->vm_source,
10950Sstevel@tonic-gate asize, vmflag & VM_KMFLAGS);
10960Sstevel@tonic-gate }
10970Sstevel@tonic-gate mutex_enter(&vmp->vm_lock);
10980Sstevel@tonic-gate vmp->vm_nsegfree += resv; /* claim reservation */
10990Sstevel@tonic-gate aneeded = size + align - vmp->vm_quantum;
11000Sstevel@tonic-gate aneeded = P2ROUNDUP(aneeded, vmp->vm_quantum);
11010Sstevel@tonic-gate if (vaddr != NULL) {
11020Sstevel@tonic-gate /*
11030Sstevel@tonic-gate * Since we dropped the vmem lock while
11040Sstevel@tonic-gate * calling the import function, other
11050Sstevel@tonic-gate * threads could have imported space
11060Sstevel@tonic-gate * and made our import unnecessary. In
11070Sstevel@tonic-gate * order to save space, we return
11080Sstevel@tonic-gate * excess imports immediately.
11090Sstevel@tonic-gate */
11100Sstevel@tonic-gate if (asize > aneeded &&
11110Sstevel@tonic-gate vmp->vm_source_free != NULL &&
11120Sstevel@tonic-gate vmem_canalloc(vmp, aneeded)) {
11130Sstevel@tonic-gate ASSERT(resv >=
11140Sstevel@tonic-gate VMEM_SEGS_PER_MIDDLE_ALLOC);
11150Sstevel@tonic-gate xvaddr = vaddr;
11160Sstevel@tonic-gate xsize = asize;
11170Sstevel@tonic-gate goto do_alloc;
11180Sstevel@tonic-gate }
11190Sstevel@tonic-gate vbest = vmem_span_create(vmp, vaddr, asize, 1);
11200Sstevel@tonic-gate addr = P2PHASEUP(vbest->vs_start, align, phase);
11210Sstevel@tonic-gate break;
11220Sstevel@tonic-gate } else if (vmem_canalloc(vmp, aneeded)) {
11230Sstevel@tonic-gate /*
11240Sstevel@tonic-gate * Our import failed, but another thread
11250Sstevel@tonic-gate * added sufficient free memory to the arena
11260Sstevel@tonic-gate * to satisfy our request. Go back and
11270Sstevel@tonic-gate * grab it.
11280Sstevel@tonic-gate */
11290Sstevel@tonic-gate ASSERT(resv >= VMEM_SEGS_PER_MIDDLE_ALLOC);
11300Sstevel@tonic-gate goto do_alloc;
11310Sstevel@tonic-gate }
11320Sstevel@tonic-gate }
11330Sstevel@tonic-gate
11340Sstevel@tonic-gate /*
11350Sstevel@tonic-gate * If the requestor chooses to fail the allocation attempt
11360Sstevel@tonic-gate * rather than reap wait and retry - get out of the loop.
11370Sstevel@tonic-gate */
11380Sstevel@tonic-gate if (vmflag & VM_ABORT)
11390Sstevel@tonic-gate break;
11400Sstevel@tonic-gate mutex_exit(&vmp->vm_lock);
11410Sstevel@tonic-gate if (vmp->vm_cflags & VMC_IDENTIFIER)
11420Sstevel@tonic-gate kmem_reap_idspace();
11430Sstevel@tonic-gate else
11440Sstevel@tonic-gate kmem_reap();
11450Sstevel@tonic-gate mutex_enter(&vmp->vm_lock);
11460Sstevel@tonic-gate if (vmflag & VM_NOSLEEP)
11470Sstevel@tonic-gate break;
11480Sstevel@tonic-gate vmp->vm_kstat.vk_wait.value.ui64++;
11490Sstevel@tonic-gate cv_wait(&vmp->vm_cv, &vmp->vm_lock);
11500Sstevel@tonic-gate }
11510Sstevel@tonic-gate if (vbest != NULL) {
11520Sstevel@tonic-gate ASSERT(vbest->vs_type == VMEM_FREE);
11530Sstevel@tonic-gate ASSERT(vbest->vs_knext != vbest);
11548212SMichael.Corcoran@Sun.COM /* re-position to end of buffer */
11558212SMichael.Corcoran@Sun.COM if (vmflag & VM_ENDALLOC) {
11568212SMichael.Corcoran@Sun.COM addr += ((vbest->vs_end - (addr + size)) / align) *
11578212SMichael.Corcoran@Sun.COM align;
11588212SMichael.Corcoran@Sun.COM }
11590Sstevel@tonic-gate (void) vmem_seg_alloc(vmp, vbest, addr, size);
11600Sstevel@tonic-gate mutex_exit(&vmp->vm_lock);
11610Sstevel@tonic-gate if (xvaddr)
11620Sstevel@tonic-gate vmp->vm_source_free(vmp->vm_source, xvaddr, xsize);
11630Sstevel@tonic-gate ASSERT(P2PHASE(addr, align) == phase);
11647837SMatthew.Ahrens@Sun.COM ASSERT(!P2BOUNDARY(addr, size, nocross));
11650Sstevel@tonic-gate ASSERT(addr >= (uintptr_t)minaddr);
11660Sstevel@tonic-gate ASSERT(addr + size - 1 <= (uintptr_t)maxaddr - 1);
11670Sstevel@tonic-gate return ((void *)addr);
11680Sstevel@tonic-gate }
11690Sstevel@tonic-gate vmp->vm_kstat.vk_fail.value.ui64++;
11700Sstevel@tonic-gate mutex_exit(&vmp->vm_lock);
11710Sstevel@tonic-gate if (vmflag & VM_PANIC)
11720Sstevel@tonic-gate panic("vmem_xalloc(%p, %lu, %lu, %lu, %lu, %p, %p, %x): "
11730Sstevel@tonic-gate "cannot satisfy mandatory allocation",
11740Sstevel@tonic-gate (void *)vmp, size, align_arg, phase, nocross,
11750Sstevel@tonic-gate minaddr, maxaddr, vmflag);
11760Sstevel@tonic-gate ASSERT(xvaddr == NULL);
11770Sstevel@tonic-gate return (NULL);
11780Sstevel@tonic-gate }
11790Sstevel@tonic-gate
11800Sstevel@tonic-gate /*
11810Sstevel@tonic-gate * Free the segment [vaddr, vaddr + size), where vaddr was a constrained
11820Sstevel@tonic-gate * allocation. vmem_xalloc() and vmem_xfree() must always be paired because
11830Sstevel@tonic-gate * both routines bypass the quantum caches.
11840Sstevel@tonic-gate */
11850Sstevel@tonic-gate void
vmem_xfree(vmem_t * vmp,void * vaddr,size_t size)11860Sstevel@tonic-gate vmem_xfree(vmem_t *vmp, void *vaddr, size_t size)
11870Sstevel@tonic-gate {
11880Sstevel@tonic-gate vmem_seg_t *vsp, *vnext, *vprev;
11890Sstevel@tonic-gate
11900Sstevel@tonic-gate mutex_enter(&vmp->vm_lock);
11910Sstevel@tonic-gate
11920Sstevel@tonic-gate vsp = vmem_hash_delete(vmp, (uintptr_t)vaddr, size);
11930Sstevel@tonic-gate vsp->vs_end = P2ROUNDUP(vsp->vs_end, vmp->vm_quantum);
11940Sstevel@tonic-gate
11950Sstevel@tonic-gate /*
11960Sstevel@tonic-gate * Attempt to coalesce with the next segment.
11970Sstevel@tonic-gate */
11980Sstevel@tonic-gate vnext = vsp->vs_anext;
11990Sstevel@tonic-gate if (vnext->vs_type == VMEM_FREE) {
12000Sstevel@tonic-gate ASSERT(vsp->vs_end == vnext->vs_start);
12010Sstevel@tonic-gate vmem_freelist_delete(vmp, vnext);
12020Sstevel@tonic-gate vsp->vs_end = vnext->vs_end;
12030Sstevel@tonic-gate vmem_seg_destroy(vmp, vnext);
12040Sstevel@tonic-gate }
12050Sstevel@tonic-gate
12060Sstevel@tonic-gate /*
12070Sstevel@tonic-gate * Attempt to coalesce with the previous segment.
12080Sstevel@tonic-gate */
12090Sstevel@tonic-gate vprev = vsp->vs_aprev;
12100Sstevel@tonic-gate if (vprev->vs_type == VMEM_FREE) {
12110Sstevel@tonic-gate ASSERT(vprev->vs_end == vsp->vs_start);
12120Sstevel@tonic-gate vmem_freelist_delete(vmp, vprev);
12130Sstevel@tonic-gate vprev->vs_end = vsp->vs_end;
12140Sstevel@tonic-gate vmem_seg_destroy(vmp, vsp);
12150Sstevel@tonic-gate vsp = vprev;
12160Sstevel@tonic-gate }
12170Sstevel@tonic-gate
12180Sstevel@tonic-gate /*
12190Sstevel@tonic-gate * If the entire span is free, return it to the source.
12200Sstevel@tonic-gate */
12210Sstevel@tonic-gate if (vsp->vs_aprev->vs_import && vmp->vm_source_free != NULL &&
12220Sstevel@tonic-gate vsp->vs_aprev->vs_type == VMEM_SPAN &&
12230Sstevel@tonic-gate vsp->vs_anext->vs_type == VMEM_SPAN) {
12240Sstevel@tonic-gate vaddr = (void *)vsp->vs_start;
12250Sstevel@tonic-gate size = VS_SIZE(vsp);
12260Sstevel@tonic-gate ASSERT(size == VS_SIZE(vsp->vs_aprev));
12270Sstevel@tonic-gate vmem_span_destroy(vmp, vsp);
12280Sstevel@tonic-gate mutex_exit(&vmp->vm_lock);
12290Sstevel@tonic-gate vmp->vm_source_free(vmp->vm_source, vaddr, size);
12300Sstevel@tonic-gate } else {
12310Sstevel@tonic-gate vmem_freelist_insert(vmp, vsp);
12320Sstevel@tonic-gate mutex_exit(&vmp->vm_lock);
12330Sstevel@tonic-gate }
12340Sstevel@tonic-gate }
12350Sstevel@tonic-gate
12360Sstevel@tonic-gate /*
12370Sstevel@tonic-gate * Allocate size bytes from arena vmp. Returns the allocated address
12380Sstevel@tonic-gate * on success, NULL on failure. vmflag specifies VM_SLEEP or VM_NOSLEEP,
12390Sstevel@tonic-gate * and may also specify best-fit, first-fit, or next-fit allocation policy
12400Sstevel@tonic-gate * instead of the default instant-fit policy. VM_SLEEP allocations are
12410Sstevel@tonic-gate * guaranteed to succeed.
12420Sstevel@tonic-gate */
12430Sstevel@tonic-gate void *
vmem_alloc(vmem_t * vmp,size_t size,int vmflag)12440Sstevel@tonic-gate vmem_alloc(vmem_t *vmp, size_t size, int vmflag)
12450Sstevel@tonic-gate {
12460Sstevel@tonic-gate vmem_seg_t *vsp;
12470Sstevel@tonic-gate uintptr_t addr;
12480Sstevel@tonic-gate int hb;
12490Sstevel@tonic-gate int flist = 0;
12500Sstevel@tonic-gate uint32_t mtbf;
12510Sstevel@tonic-gate
12520Sstevel@tonic-gate if (size - 1 < vmp->vm_qcache_max)
12530Sstevel@tonic-gate return (kmem_cache_alloc(vmp->vm_qcache[(size - 1) >>
12540Sstevel@tonic-gate vmp->vm_qshift], vmflag & VM_KMFLAGS));
12550Sstevel@tonic-gate
12560Sstevel@tonic-gate if ((mtbf = vmem_mtbf | vmp->vm_mtbf) != 0 && gethrtime() % mtbf == 0 &&
12570Sstevel@tonic-gate (vmflag & (VM_NOSLEEP | VM_PANIC)) == VM_NOSLEEP)
12580Sstevel@tonic-gate return (NULL);
12590Sstevel@tonic-gate
12600Sstevel@tonic-gate if (vmflag & VM_NEXTFIT)
12610Sstevel@tonic-gate return (vmem_nextfit_alloc(vmp, size, vmflag));
12620Sstevel@tonic-gate
12630Sstevel@tonic-gate if (vmflag & (VM_BESTFIT | VM_FIRSTFIT))
12640Sstevel@tonic-gate return (vmem_xalloc(vmp, size, vmp->vm_quantum, 0, 0,
12650Sstevel@tonic-gate NULL, NULL, vmflag));
12660Sstevel@tonic-gate
12670Sstevel@tonic-gate /*
12680Sstevel@tonic-gate * Unconstrained instant-fit allocation from the segment list.
12690Sstevel@tonic-gate */
12700Sstevel@tonic-gate mutex_enter(&vmp->vm_lock);
12710Sstevel@tonic-gate
12720Sstevel@tonic-gate if (vmp->vm_nsegfree >= VMEM_MINFREE || vmem_populate(vmp, vmflag)) {
12730Sstevel@tonic-gate if ((size & (size - 1)) == 0)
12740Sstevel@tonic-gate flist = lowbit(P2ALIGN(vmp->vm_freemap, size));
12750Sstevel@tonic-gate else if ((hb = highbit(size)) < VMEM_FREELISTS)
12760Sstevel@tonic-gate flist = lowbit(P2ALIGN(vmp->vm_freemap, 1UL << hb));
12770Sstevel@tonic-gate }
12780Sstevel@tonic-gate
12790Sstevel@tonic-gate if (flist-- == 0) {
12800Sstevel@tonic-gate mutex_exit(&vmp->vm_lock);
12810Sstevel@tonic-gate return (vmem_xalloc(vmp, size, vmp->vm_quantum,
12820Sstevel@tonic-gate 0, 0, NULL, NULL, vmflag));
12830Sstevel@tonic-gate }
12840Sstevel@tonic-gate
12850Sstevel@tonic-gate ASSERT(size <= (1UL << flist));
12860Sstevel@tonic-gate vsp = vmp->vm_freelist[flist].vs_knext;
12870Sstevel@tonic-gate addr = vsp->vs_start;
12888212SMichael.Corcoran@Sun.COM if (vmflag & VM_ENDALLOC) {
12898212SMichael.Corcoran@Sun.COM addr += vsp->vs_end - (addr + size);
12908212SMichael.Corcoran@Sun.COM }
12910Sstevel@tonic-gate (void) vmem_seg_alloc(vmp, vsp, addr, size);
12920Sstevel@tonic-gate mutex_exit(&vmp->vm_lock);
12930Sstevel@tonic-gate return ((void *)addr);
12940Sstevel@tonic-gate }
12950Sstevel@tonic-gate
12960Sstevel@tonic-gate /*
12970Sstevel@tonic-gate * Free the segment [vaddr, vaddr + size).
12980Sstevel@tonic-gate */
12990Sstevel@tonic-gate void
vmem_free(vmem_t * vmp,void * vaddr,size_t size)13000Sstevel@tonic-gate vmem_free(vmem_t *vmp, void *vaddr, size_t size)
13010Sstevel@tonic-gate {
13020Sstevel@tonic-gate if (size - 1 < vmp->vm_qcache_max)
13030Sstevel@tonic-gate kmem_cache_free(vmp->vm_qcache[(size - 1) >> vmp->vm_qshift],
13040Sstevel@tonic-gate vaddr);
13050Sstevel@tonic-gate else
13060Sstevel@tonic-gate vmem_xfree(vmp, vaddr, size);
13070Sstevel@tonic-gate }
13080Sstevel@tonic-gate
13090Sstevel@tonic-gate /*
13100Sstevel@tonic-gate * Determine whether arena vmp contains the segment [vaddr, vaddr + size).
13110Sstevel@tonic-gate */
13120Sstevel@tonic-gate int
vmem_contains(vmem_t * vmp,void * vaddr,size_t size)13130Sstevel@tonic-gate vmem_contains(vmem_t *vmp, void *vaddr, size_t size)
13140Sstevel@tonic-gate {
13150Sstevel@tonic-gate uintptr_t start = (uintptr_t)vaddr;
13160Sstevel@tonic-gate uintptr_t end = start + size;
13170Sstevel@tonic-gate vmem_seg_t *vsp;
13180Sstevel@tonic-gate vmem_seg_t *seg0 = &vmp->vm_seg0;
13190Sstevel@tonic-gate
13200Sstevel@tonic-gate mutex_enter(&vmp->vm_lock);
13210Sstevel@tonic-gate vmp->vm_kstat.vk_contains.value.ui64++;
13220Sstevel@tonic-gate for (vsp = seg0->vs_knext; vsp != seg0; vsp = vsp->vs_knext) {
13230Sstevel@tonic-gate vmp->vm_kstat.vk_contains_search.value.ui64++;
13240Sstevel@tonic-gate ASSERT(vsp->vs_type == VMEM_SPAN);
13250Sstevel@tonic-gate if (start >= vsp->vs_start && end - 1 <= vsp->vs_end - 1)
13260Sstevel@tonic-gate break;
13270Sstevel@tonic-gate }
13280Sstevel@tonic-gate mutex_exit(&vmp->vm_lock);
13290Sstevel@tonic-gate return (vsp != seg0);
13300Sstevel@tonic-gate }
13310Sstevel@tonic-gate
13320Sstevel@tonic-gate /*
13330Sstevel@tonic-gate * Add the span [vaddr, vaddr + size) to arena vmp.
13340Sstevel@tonic-gate */
13350Sstevel@tonic-gate void *
vmem_add(vmem_t * vmp,void * vaddr,size_t size,int vmflag)13360Sstevel@tonic-gate vmem_add(vmem_t *vmp, void *vaddr, size_t size, int vmflag)
13370Sstevel@tonic-gate {
13380Sstevel@tonic-gate if (vaddr == NULL || size == 0)
13397240Srh87107 panic("vmem_add(%p, %p, %lu): bad arguments",
13407240Srh87107 (void *)vmp, vaddr, size);
13410Sstevel@tonic-gate
13420Sstevel@tonic-gate ASSERT(!vmem_contains(vmp, vaddr, size));
13430Sstevel@tonic-gate
13440Sstevel@tonic-gate mutex_enter(&vmp->vm_lock);
13450Sstevel@tonic-gate if (vmem_populate(vmp, vmflag))
13460Sstevel@tonic-gate (void) vmem_span_create(vmp, vaddr, size, 0);
13470Sstevel@tonic-gate else
13480Sstevel@tonic-gate vaddr = NULL;
13490Sstevel@tonic-gate mutex_exit(&vmp->vm_lock);
13500Sstevel@tonic-gate return (vaddr);
13510Sstevel@tonic-gate }
13520Sstevel@tonic-gate
13530Sstevel@tonic-gate /*
13540Sstevel@tonic-gate * Walk the vmp arena, applying func to each segment matching typemask.
13550Sstevel@tonic-gate * If VMEM_REENTRANT is specified, the arena lock is dropped across each
13560Sstevel@tonic-gate * call to func(); otherwise, it is held for the duration of vmem_walk()
13570Sstevel@tonic-gate * to ensure a consistent snapshot. Note that VMEM_REENTRANT callbacks
13580Sstevel@tonic-gate * are *not* necessarily consistent, so they may only be used when a hint
13590Sstevel@tonic-gate * is adequate.
13600Sstevel@tonic-gate */
13610Sstevel@tonic-gate void
vmem_walk(vmem_t * vmp,int typemask,void (* func)(void *,void *,size_t),void * arg)13620Sstevel@tonic-gate vmem_walk(vmem_t *vmp, int typemask,
13630Sstevel@tonic-gate void (*func)(void *, void *, size_t), void *arg)
13640Sstevel@tonic-gate {
13650Sstevel@tonic-gate vmem_seg_t *vsp;
13660Sstevel@tonic-gate vmem_seg_t *seg0 = &vmp->vm_seg0;
13670Sstevel@tonic-gate vmem_seg_t walker;
13680Sstevel@tonic-gate
13690Sstevel@tonic-gate if (typemask & VMEM_WALKER)
13700Sstevel@tonic-gate return;
13710Sstevel@tonic-gate
13720Sstevel@tonic-gate bzero(&walker, sizeof (walker));
13730Sstevel@tonic-gate walker.vs_type = VMEM_WALKER;
13740Sstevel@tonic-gate
13750Sstevel@tonic-gate mutex_enter(&vmp->vm_lock);
13760Sstevel@tonic-gate VMEM_INSERT(seg0, &walker, a);
13770Sstevel@tonic-gate for (vsp = seg0->vs_anext; vsp != seg0; vsp = vsp->vs_anext) {
13780Sstevel@tonic-gate if (vsp->vs_type & typemask) {
13790Sstevel@tonic-gate void *start = (void *)vsp->vs_start;
13800Sstevel@tonic-gate size_t size = VS_SIZE(vsp);
13810Sstevel@tonic-gate if (typemask & VMEM_REENTRANT) {
13820Sstevel@tonic-gate vmem_advance(vmp, &walker, vsp);
13830Sstevel@tonic-gate mutex_exit(&vmp->vm_lock);
13840Sstevel@tonic-gate func(arg, start, size);
13850Sstevel@tonic-gate mutex_enter(&vmp->vm_lock);
13860Sstevel@tonic-gate vsp = &walker;
13870Sstevel@tonic-gate } else {
13880Sstevel@tonic-gate func(arg, start, size);
13890Sstevel@tonic-gate }
13900Sstevel@tonic-gate }
13910Sstevel@tonic-gate }
13920Sstevel@tonic-gate vmem_advance(vmp, &walker, NULL);
13930Sstevel@tonic-gate mutex_exit(&vmp->vm_lock);
13940Sstevel@tonic-gate }
13950Sstevel@tonic-gate
13960Sstevel@tonic-gate /*
13970Sstevel@tonic-gate * Return the total amount of memory whose type matches typemask. Thus:
13980Sstevel@tonic-gate *
13990Sstevel@tonic-gate * typemask VMEM_ALLOC yields total memory allocated (in use).
14000Sstevel@tonic-gate * typemask VMEM_FREE yields total memory free (available).
14010Sstevel@tonic-gate * typemask (VMEM_ALLOC | VMEM_FREE) yields total arena size.
14020Sstevel@tonic-gate */
14030Sstevel@tonic-gate size_t
vmem_size(vmem_t * vmp,int typemask)14040Sstevel@tonic-gate vmem_size(vmem_t *vmp, int typemask)
14050Sstevel@tonic-gate {
14060Sstevel@tonic-gate uint64_t size = 0;
14070Sstevel@tonic-gate
14080Sstevel@tonic-gate if (typemask & VMEM_ALLOC)
14090Sstevel@tonic-gate size += vmp->vm_kstat.vk_mem_inuse.value.ui64;
14100Sstevel@tonic-gate if (typemask & VMEM_FREE)
14110Sstevel@tonic-gate size += vmp->vm_kstat.vk_mem_total.value.ui64 -
14120Sstevel@tonic-gate vmp->vm_kstat.vk_mem_inuse.value.ui64;
14130Sstevel@tonic-gate return ((size_t)size);
14140Sstevel@tonic-gate }
14150Sstevel@tonic-gate
14160Sstevel@tonic-gate /*
14170Sstevel@tonic-gate * Create an arena called name whose initial span is [base, base + size).
14180Sstevel@tonic-gate * The arena's natural unit of currency is quantum, so vmem_alloc()
14190Sstevel@tonic-gate * guarantees quantum-aligned results. The arena may import new spans
14200Sstevel@tonic-gate * by invoking afunc() on source, and may return those spans by invoking
14210Sstevel@tonic-gate * ffunc() on source. To make small allocations fast and scalable,
14220Sstevel@tonic-gate * the arena offers high-performance caching for each integer multiple
14230Sstevel@tonic-gate * of quantum up to qcache_max.
14240Sstevel@tonic-gate */
14250Sstevel@tonic-gate static vmem_t *
vmem_create_common(const char * name,void * base,size_t size,size_t quantum,void * (* afunc)(vmem_t *,size_t,int),void (* ffunc)(vmem_t *,void *,size_t),vmem_t * source,size_t qcache_max,int vmflag)14260Sstevel@tonic-gate vmem_create_common(const char *name, void *base, size_t size, size_t quantum,
14270Sstevel@tonic-gate void *(*afunc)(vmem_t *, size_t, int),
14280Sstevel@tonic-gate void (*ffunc)(vmem_t *, void *, size_t),
14290Sstevel@tonic-gate vmem_t *source, size_t qcache_max, int vmflag)
14300Sstevel@tonic-gate {
14310Sstevel@tonic-gate int i;
14320Sstevel@tonic-gate size_t nqcache;
14330Sstevel@tonic-gate vmem_t *vmp, *cur, **vmpp;
14340Sstevel@tonic-gate vmem_seg_t *vsp;
14350Sstevel@tonic-gate vmem_freelist_t *vfp;
14360Sstevel@tonic-gate uint32_t id = atomic_add_32_nv(&vmem_id, 1);
14370Sstevel@tonic-gate
14380Sstevel@tonic-gate if (vmem_vmem_arena != NULL) {
14390Sstevel@tonic-gate vmp = vmem_alloc(vmem_vmem_arena, sizeof (vmem_t),
14400Sstevel@tonic-gate vmflag & VM_KMFLAGS);
14410Sstevel@tonic-gate } else {
14420Sstevel@tonic-gate ASSERT(id <= VMEM_INITIAL);
14430Sstevel@tonic-gate vmp = &vmem0[id - 1];
14440Sstevel@tonic-gate }
14450Sstevel@tonic-gate
14460Sstevel@tonic-gate /* An identifier arena must inherit from another identifier arena */
14470Sstevel@tonic-gate ASSERT(source == NULL || ((source->vm_cflags & VMC_IDENTIFIER) ==
14480Sstevel@tonic-gate (vmflag & VMC_IDENTIFIER)));
14490Sstevel@tonic-gate
14500Sstevel@tonic-gate if (vmp == NULL)
14510Sstevel@tonic-gate return (NULL);
14520Sstevel@tonic-gate bzero(vmp, sizeof (vmem_t));
14530Sstevel@tonic-gate
14540Sstevel@tonic-gate (void) snprintf(vmp->vm_name, VMEM_NAMELEN, "%s", name);
14550Sstevel@tonic-gate mutex_init(&vmp->vm_lock, NULL, MUTEX_DEFAULT, NULL);
14560Sstevel@tonic-gate cv_init(&vmp->vm_cv, NULL, CV_DEFAULT, NULL);
14570Sstevel@tonic-gate vmp->vm_cflags = vmflag;
14580Sstevel@tonic-gate vmflag &= VM_KMFLAGS;
14590Sstevel@tonic-gate
14600Sstevel@tonic-gate vmp->vm_quantum = quantum;
14610Sstevel@tonic-gate vmp->vm_qshift = highbit(quantum) - 1;
14620Sstevel@tonic-gate nqcache = MIN(qcache_max >> vmp->vm_qshift, VMEM_NQCACHE_MAX);
14630Sstevel@tonic-gate
14640Sstevel@tonic-gate for (i = 0; i <= VMEM_FREELISTS; i++) {
14650Sstevel@tonic-gate vfp = &vmp->vm_freelist[i];
14660Sstevel@tonic-gate vfp->vs_end = 1UL << i;
14670Sstevel@tonic-gate vfp->vs_knext = (vmem_seg_t *)(vfp + 1);
14680Sstevel@tonic-gate vfp->vs_kprev = (vmem_seg_t *)(vfp - 1);
14690Sstevel@tonic-gate }
14700Sstevel@tonic-gate
14710Sstevel@tonic-gate vmp->vm_freelist[0].vs_kprev = NULL;
14720Sstevel@tonic-gate vmp->vm_freelist[VMEM_FREELISTS].vs_knext = NULL;
14730Sstevel@tonic-gate vmp->vm_freelist[VMEM_FREELISTS].vs_end = 0;
14740Sstevel@tonic-gate vmp->vm_hash_table = vmp->vm_hash0;
14750Sstevel@tonic-gate vmp->vm_hash_mask = VMEM_HASH_INITIAL - 1;
14760Sstevel@tonic-gate vmp->vm_hash_shift = highbit(vmp->vm_hash_mask);
14770Sstevel@tonic-gate
14780Sstevel@tonic-gate vsp = &vmp->vm_seg0;
14790Sstevel@tonic-gate vsp->vs_anext = vsp;
14800Sstevel@tonic-gate vsp->vs_aprev = vsp;
14810Sstevel@tonic-gate vsp->vs_knext = vsp;
14820Sstevel@tonic-gate vsp->vs_kprev = vsp;
14830Sstevel@tonic-gate vsp->vs_type = VMEM_SPAN;
14840Sstevel@tonic-gate
14850Sstevel@tonic-gate vsp = &vmp->vm_rotor;
14860Sstevel@tonic-gate vsp->vs_type = VMEM_ROTOR;
14870Sstevel@tonic-gate VMEM_INSERT(&vmp->vm_seg0, vsp, a);
14880Sstevel@tonic-gate
14890Sstevel@tonic-gate bcopy(&vmem_kstat_template, &vmp->vm_kstat, sizeof (vmem_kstat_t));
14900Sstevel@tonic-gate
14910Sstevel@tonic-gate vmp->vm_id = id;
14920Sstevel@tonic-gate if (source != NULL)
14930Sstevel@tonic-gate vmp->vm_kstat.vk_source_id.value.ui32 = source->vm_id;
14940Sstevel@tonic-gate vmp->vm_source = source;
14950Sstevel@tonic-gate vmp->vm_source_alloc = afunc;
14960Sstevel@tonic-gate vmp->vm_source_free = ffunc;
14970Sstevel@tonic-gate
14980Sstevel@tonic-gate /*
14990Sstevel@tonic-gate * Some arenas (like vmem_metadata and kmem_metadata) cannot
15000Sstevel@tonic-gate * use quantum caching to lower fragmentation. Instead, we
15010Sstevel@tonic-gate * increase their imports, giving a similar effect.
15020Sstevel@tonic-gate */
15030Sstevel@tonic-gate if (vmp->vm_cflags & VMC_NO_QCACHE) {
15040Sstevel@tonic-gate vmp->vm_min_import =
15050Sstevel@tonic-gate VMEM_QCACHE_SLABSIZE(nqcache << vmp->vm_qshift);
15060Sstevel@tonic-gate nqcache = 0;
15070Sstevel@tonic-gate }
15080Sstevel@tonic-gate
15090Sstevel@tonic-gate if (nqcache != 0) {
15100Sstevel@tonic-gate ASSERT(!(vmflag & VM_NOSLEEP));
15110Sstevel@tonic-gate vmp->vm_qcache_max = nqcache << vmp->vm_qshift;
15120Sstevel@tonic-gate for (i = 0; i < nqcache; i++) {
15130Sstevel@tonic-gate char buf[VMEM_NAMELEN + 21];
15140Sstevel@tonic-gate (void) sprintf(buf, "%s_%lu", vmp->vm_name,
15150Sstevel@tonic-gate (i + 1) * quantum);
15160Sstevel@tonic-gate vmp->vm_qcache[i] = kmem_cache_create(buf,
15170Sstevel@tonic-gate (i + 1) * quantum, quantum, NULL, NULL, NULL,
15180Sstevel@tonic-gate NULL, vmp, KMC_QCACHE | KMC_NOTOUCH);
15190Sstevel@tonic-gate }
15200Sstevel@tonic-gate }
15210Sstevel@tonic-gate
15220Sstevel@tonic-gate if ((vmp->vm_ksp = kstat_create("vmem", vmp->vm_id, vmp->vm_name,
15230Sstevel@tonic-gate "vmem", KSTAT_TYPE_NAMED, sizeof (vmem_kstat_t) /
15240Sstevel@tonic-gate sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL)) != NULL) {
15250Sstevel@tonic-gate vmp->vm_ksp->ks_data = &vmp->vm_kstat;
15260Sstevel@tonic-gate kstat_install(vmp->vm_ksp);
15270Sstevel@tonic-gate }
15280Sstevel@tonic-gate
15290Sstevel@tonic-gate mutex_enter(&vmem_list_lock);
15300Sstevel@tonic-gate vmpp = &vmem_list;
15310Sstevel@tonic-gate while ((cur = *vmpp) != NULL)
15320Sstevel@tonic-gate vmpp = &cur->vm_next;
15330Sstevel@tonic-gate *vmpp = vmp;
15340Sstevel@tonic-gate mutex_exit(&vmem_list_lock);
15350Sstevel@tonic-gate
15360Sstevel@tonic-gate if (vmp->vm_cflags & VMC_POPULATOR) {
15370Sstevel@tonic-gate ASSERT(vmem_populators < VMEM_INITIAL);
15380Sstevel@tonic-gate vmem_populator[atomic_add_32_nv(&vmem_populators, 1) - 1] = vmp;
15390Sstevel@tonic-gate mutex_enter(&vmp->vm_lock);
15400Sstevel@tonic-gate (void) vmem_populate(vmp, vmflag | VM_PANIC);
15410Sstevel@tonic-gate mutex_exit(&vmp->vm_lock);
15420Sstevel@tonic-gate }
15430Sstevel@tonic-gate
15440Sstevel@tonic-gate if ((base || size) && vmem_add(vmp, base, size, vmflag) == NULL) {
15450Sstevel@tonic-gate vmem_destroy(vmp);
15460Sstevel@tonic-gate return (NULL);
15470Sstevel@tonic-gate }
15480Sstevel@tonic-gate
15490Sstevel@tonic-gate return (vmp);
15500Sstevel@tonic-gate }
15510Sstevel@tonic-gate
15520Sstevel@tonic-gate vmem_t *
vmem_xcreate(const char * name,void * base,size_t size,size_t quantum,vmem_ximport_t * afunc,vmem_free_t * ffunc,vmem_t * source,size_t qcache_max,int vmflag)15530Sstevel@tonic-gate vmem_xcreate(const char *name, void *base, size_t size, size_t quantum,
15540Sstevel@tonic-gate vmem_ximport_t *afunc, vmem_free_t *ffunc, vmem_t *source,
15550Sstevel@tonic-gate size_t qcache_max, int vmflag)
15560Sstevel@tonic-gate {
15570Sstevel@tonic-gate ASSERT(!(vmflag & (VMC_POPULATOR | VMC_XALLOC)));
15580Sstevel@tonic-gate vmflag &= ~(VMC_POPULATOR | VMC_XALLOC);
15590Sstevel@tonic-gate
15600Sstevel@tonic-gate return (vmem_create_common(name, base, size, quantum,
15610Sstevel@tonic-gate (vmem_alloc_t *)afunc, ffunc, source, qcache_max,
15620Sstevel@tonic-gate vmflag | VMC_XALLOC));
15630Sstevel@tonic-gate }
15640Sstevel@tonic-gate
15650Sstevel@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)15660Sstevel@tonic-gate vmem_create(const char *name, void *base, size_t size, size_t quantum,
15670Sstevel@tonic-gate vmem_alloc_t *afunc, vmem_free_t *ffunc, vmem_t *source,
15680Sstevel@tonic-gate size_t qcache_max, int vmflag)
15690Sstevel@tonic-gate {
15704204Sha137994 ASSERT(!(vmflag & (VMC_XALLOC | VMC_XALIGN)));
15714204Sha137994 vmflag &= ~(VMC_XALLOC | VMC_XALIGN);
15720Sstevel@tonic-gate
15730Sstevel@tonic-gate return (vmem_create_common(name, base, size, quantum,
15740Sstevel@tonic-gate afunc, ffunc, source, qcache_max, vmflag));
15750Sstevel@tonic-gate }
15760Sstevel@tonic-gate
15770Sstevel@tonic-gate /*
15780Sstevel@tonic-gate * Destroy arena vmp.
15790Sstevel@tonic-gate */
15800Sstevel@tonic-gate void
vmem_destroy(vmem_t * vmp)15810Sstevel@tonic-gate vmem_destroy(vmem_t *vmp)
15820Sstevel@tonic-gate {
15830Sstevel@tonic-gate vmem_t *cur, **vmpp;
15840Sstevel@tonic-gate vmem_seg_t *seg0 = &vmp->vm_seg0;
1585*11475SBlake.Jones@Sun.COM vmem_seg_t *vsp, *anext;
15860Sstevel@tonic-gate size_t leaked;
15870Sstevel@tonic-gate int i;
15880Sstevel@tonic-gate
15890Sstevel@tonic-gate mutex_enter(&vmem_list_lock);
15900Sstevel@tonic-gate vmpp = &vmem_list;
15910Sstevel@tonic-gate while ((cur = *vmpp) != vmp)
15920Sstevel@tonic-gate vmpp = &cur->vm_next;
15930Sstevel@tonic-gate *vmpp = vmp->vm_next;
15940Sstevel@tonic-gate mutex_exit(&vmem_list_lock);
15950Sstevel@tonic-gate
15960Sstevel@tonic-gate for (i = 0; i < VMEM_NQCACHE_MAX; i++)
15970Sstevel@tonic-gate if (vmp->vm_qcache[i])
15980Sstevel@tonic-gate kmem_cache_destroy(vmp->vm_qcache[i]);
15990Sstevel@tonic-gate
16000Sstevel@tonic-gate leaked = vmem_size(vmp, VMEM_ALLOC);
16010Sstevel@tonic-gate if (leaked != 0)
16020Sstevel@tonic-gate cmn_err(CE_WARN, "vmem_destroy('%s'): leaked %lu %s",
16030Sstevel@tonic-gate vmp->vm_name, leaked, (vmp->vm_cflags & VMC_IDENTIFIER) ?
16040Sstevel@tonic-gate "identifiers" : "bytes");
16050Sstevel@tonic-gate
16060Sstevel@tonic-gate if (vmp->vm_hash_table != vmp->vm_hash0)
16070Sstevel@tonic-gate vmem_free(vmem_hash_arena, vmp->vm_hash_table,
16080Sstevel@tonic-gate (vmp->vm_hash_mask + 1) * sizeof (void *));
16090Sstevel@tonic-gate
16100Sstevel@tonic-gate /*
16110Sstevel@tonic-gate * Give back the segment structures for anything that's left in the
16120Sstevel@tonic-gate * arena, e.g. the primary spans and their free segments.
16130Sstevel@tonic-gate */
16140Sstevel@tonic-gate VMEM_DELETE(&vmp->vm_rotor, a);
1615*11475SBlake.Jones@Sun.COM for (vsp = seg0->vs_anext; vsp != seg0; vsp = anext) {
1616*11475SBlake.Jones@Sun.COM anext = vsp->vs_anext;
16170Sstevel@tonic-gate vmem_putseg_global(vsp);
1618*11475SBlake.Jones@Sun.COM }
16190Sstevel@tonic-gate
16200Sstevel@tonic-gate while (vmp->vm_nsegfree > 0)
16210Sstevel@tonic-gate vmem_putseg_global(vmem_getseg(vmp));
16220Sstevel@tonic-gate
16230Sstevel@tonic-gate kstat_delete(vmp->vm_ksp);
16240Sstevel@tonic-gate
16250Sstevel@tonic-gate mutex_destroy(&vmp->vm_lock);
16260Sstevel@tonic-gate cv_destroy(&vmp->vm_cv);
16270Sstevel@tonic-gate vmem_free(vmem_vmem_arena, vmp, sizeof (vmem_t));
16280Sstevel@tonic-gate }
16290Sstevel@tonic-gate
16300Sstevel@tonic-gate /*
16310Sstevel@tonic-gate * Resize vmp's hash table to keep the average lookup depth near 1.0.
16320Sstevel@tonic-gate */
16330Sstevel@tonic-gate static void
vmem_hash_rescale(vmem_t * vmp)16340Sstevel@tonic-gate vmem_hash_rescale(vmem_t *vmp)
16350Sstevel@tonic-gate {
16360Sstevel@tonic-gate vmem_seg_t **old_table, **new_table, *vsp;
16370Sstevel@tonic-gate size_t old_size, new_size, h, nseg;
16380Sstevel@tonic-gate
16390Sstevel@tonic-gate nseg = (size_t)(vmp->vm_kstat.vk_alloc.value.ui64 -
16400Sstevel@tonic-gate vmp->vm_kstat.vk_free.value.ui64);
16410Sstevel@tonic-gate
16420Sstevel@tonic-gate new_size = MAX(VMEM_HASH_INITIAL, 1 << (highbit(3 * nseg + 4) - 2));
16430Sstevel@tonic-gate old_size = vmp->vm_hash_mask + 1;
16440Sstevel@tonic-gate
16450Sstevel@tonic-gate if ((old_size >> 1) <= new_size && new_size <= (old_size << 1))
16460Sstevel@tonic-gate return;
16470Sstevel@tonic-gate
16480Sstevel@tonic-gate new_table = vmem_alloc(vmem_hash_arena, new_size * sizeof (void *),
16490Sstevel@tonic-gate VM_NOSLEEP);
16500Sstevel@tonic-gate if (new_table == NULL)
16510Sstevel@tonic-gate return;
16520Sstevel@tonic-gate bzero(new_table, new_size * sizeof (void *));
16530Sstevel@tonic-gate
16540Sstevel@tonic-gate mutex_enter(&vmp->vm_lock);
16550Sstevel@tonic-gate
16560Sstevel@tonic-gate old_size = vmp->vm_hash_mask + 1;
16570Sstevel@tonic-gate old_table = vmp->vm_hash_table;
16580Sstevel@tonic-gate
16590Sstevel@tonic-gate vmp->vm_hash_mask = new_size - 1;
16600Sstevel@tonic-gate vmp->vm_hash_table = new_table;
16610Sstevel@tonic-gate vmp->vm_hash_shift = highbit(vmp->vm_hash_mask);
16620Sstevel@tonic-gate
16630Sstevel@tonic-gate for (h = 0; h < old_size; h++) {
16640Sstevel@tonic-gate vsp = old_table[h];
16650Sstevel@tonic-gate while (vsp != NULL) {
16660Sstevel@tonic-gate uintptr_t addr = vsp->vs_start;
16670Sstevel@tonic-gate vmem_seg_t *next_vsp = vsp->vs_knext;
16680Sstevel@tonic-gate vmem_seg_t **hash_bucket = VMEM_HASH(vmp, addr);
16690Sstevel@tonic-gate vsp->vs_knext = *hash_bucket;
16700Sstevel@tonic-gate *hash_bucket = vsp;
16710Sstevel@tonic-gate vsp = next_vsp;
16720Sstevel@tonic-gate }
16730Sstevel@tonic-gate }
16740Sstevel@tonic-gate
16750Sstevel@tonic-gate mutex_exit(&vmp->vm_lock);
16760Sstevel@tonic-gate
16770Sstevel@tonic-gate if (old_table != vmp->vm_hash0)
16780Sstevel@tonic-gate vmem_free(vmem_hash_arena, old_table,
16790Sstevel@tonic-gate old_size * sizeof (void *));
16800Sstevel@tonic-gate }
16810Sstevel@tonic-gate
16820Sstevel@tonic-gate /*
16830Sstevel@tonic-gate * Perform periodic maintenance on all vmem arenas.
16840Sstevel@tonic-gate */
16850Sstevel@tonic-gate void
vmem_update(void * dummy)16860Sstevel@tonic-gate vmem_update(void *dummy)
16870Sstevel@tonic-gate {
16880Sstevel@tonic-gate vmem_t *vmp;
16890Sstevel@tonic-gate
16900Sstevel@tonic-gate mutex_enter(&vmem_list_lock);
16910Sstevel@tonic-gate for (vmp = vmem_list; vmp != NULL; vmp = vmp->vm_next) {
16920Sstevel@tonic-gate /*
16930Sstevel@tonic-gate * If threads are waiting for resources, wake them up
16940Sstevel@tonic-gate * periodically so they can issue another kmem_reap()
16950Sstevel@tonic-gate * to reclaim resources cached by the slab allocator.
16960Sstevel@tonic-gate */
16970Sstevel@tonic-gate cv_broadcast(&vmp->vm_cv);
16980Sstevel@tonic-gate
16990Sstevel@tonic-gate /*
17000Sstevel@tonic-gate * Rescale the hash table to keep the hash chains short.
17010Sstevel@tonic-gate */
17020Sstevel@tonic-gate vmem_hash_rescale(vmp);
17030Sstevel@tonic-gate }
17040Sstevel@tonic-gate mutex_exit(&vmem_list_lock);
17050Sstevel@tonic-gate
17060Sstevel@tonic-gate (void) timeout(vmem_update, dummy, vmem_update_interval * hz);
17070Sstevel@tonic-gate }
17080Sstevel@tonic-gate
17090Sstevel@tonic-gate /*
17100Sstevel@tonic-gate * Prepare vmem for use.
17110Sstevel@tonic-gate */
17120Sstevel@tonic-gate vmem_t *
vmem_init(const char * heap_name,void * heap_start,size_t heap_size,size_t heap_quantum,void * (* heap_alloc)(vmem_t *,size_t,int),void (* heap_free)(vmem_t *,void *,size_t))17130Sstevel@tonic-gate vmem_init(const char *heap_name,
17140Sstevel@tonic-gate void *heap_start, size_t heap_size, size_t heap_quantum,
17150Sstevel@tonic-gate void *(*heap_alloc)(vmem_t *, size_t, int),
17160Sstevel@tonic-gate void (*heap_free)(vmem_t *, void *, size_t))
17170Sstevel@tonic-gate {
17180Sstevel@tonic-gate uint32_t id;
17190Sstevel@tonic-gate int nseg = VMEM_SEG_INITIAL;
17200Sstevel@tonic-gate vmem_t *heap;
17210Sstevel@tonic-gate
17220Sstevel@tonic-gate while (--nseg >= 0)
17230Sstevel@tonic-gate vmem_putseg_global(&vmem_seg0[nseg]);
17240Sstevel@tonic-gate
17250Sstevel@tonic-gate heap = vmem_create(heap_name,
17260Sstevel@tonic-gate heap_start, heap_size, heap_quantum,
17270Sstevel@tonic-gate NULL, NULL, NULL, 0,
17280Sstevel@tonic-gate VM_SLEEP | VMC_POPULATOR);
17290Sstevel@tonic-gate
17300Sstevel@tonic-gate vmem_metadata_arena = vmem_create("vmem_metadata",
17310Sstevel@tonic-gate NULL, 0, heap_quantum,
17320Sstevel@tonic-gate vmem_alloc, vmem_free, heap, 8 * heap_quantum,
17330Sstevel@tonic-gate VM_SLEEP | VMC_POPULATOR | VMC_NO_QCACHE);
17340Sstevel@tonic-gate
17350Sstevel@tonic-gate vmem_seg_arena = vmem_create("vmem_seg",
17360Sstevel@tonic-gate NULL, 0, heap_quantum,
17370Sstevel@tonic-gate heap_alloc, heap_free, vmem_metadata_arena, 0,
17380Sstevel@tonic-gate VM_SLEEP | VMC_POPULATOR);
17390Sstevel@tonic-gate
17400Sstevel@tonic-gate vmem_hash_arena = vmem_create("vmem_hash",
17410Sstevel@tonic-gate NULL, 0, 8,
17420Sstevel@tonic-gate heap_alloc, heap_free, vmem_metadata_arena, 0,
17430Sstevel@tonic-gate VM_SLEEP);
17440Sstevel@tonic-gate
17450Sstevel@tonic-gate vmem_vmem_arena = vmem_create("vmem_vmem",
17460Sstevel@tonic-gate vmem0, sizeof (vmem0), 1,
17470Sstevel@tonic-gate heap_alloc, heap_free, vmem_metadata_arena, 0,
17480Sstevel@tonic-gate VM_SLEEP);
17490Sstevel@tonic-gate
17500Sstevel@tonic-gate for (id = 0; id < vmem_id; id++)
17510Sstevel@tonic-gate (void) vmem_xalloc(vmem_vmem_arena, sizeof (vmem_t),
17520Sstevel@tonic-gate 1, 0, 0, &vmem0[id], &vmem0[id + 1],
17530Sstevel@tonic-gate VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
17540Sstevel@tonic-gate
17550Sstevel@tonic-gate return (heap);
17560Sstevel@tonic-gate }
1757