xref: /titanic_52/usr/src/lib/libumem/common/vmem.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * For a more complete description of the main ideas, see:
31*7c478bd9Sstevel@tonic-gate  *
32*7c478bd9Sstevel@tonic-gate  *	Jeff Bonwick and Jonathan Adams,
33*7c478bd9Sstevel@tonic-gate  *
34*7c478bd9Sstevel@tonic-gate  *	Magazines and vmem: Extending the Slab Allocator to Many CPUs and
35*7c478bd9Sstevel@tonic-gate  *	Arbitrary Resources.
36*7c478bd9Sstevel@tonic-gate  *
37*7c478bd9Sstevel@tonic-gate  *	Proceedings of the 2001 Usenix Conference.
38*7c478bd9Sstevel@tonic-gate  *	Available as /shared/sac/PSARC/2000/550/materials/vmem.pdf.
39*7c478bd9Sstevel@tonic-gate  *
40*7c478bd9Sstevel@tonic-gate  * For the "Big Theory Statement", see usr/src/common/os/vmem.c
41*7c478bd9Sstevel@tonic-gate  *
42*7c478bd9Sstevel@tonic-gate  * 1. Overview of changes
43*7c478bd9Sstevel@tonic-gate  * ------------------------------
44*7c478bd9Sstevel@tonic-gate  * There have been a few changes to vmem in order to support umem.  The
45*7c478bd9Sstevel@tonic-gate  * main areas are:
46*7c478bd9Sstevel@tonic-gate  *
47*7c478bd9Sstevel@tonic-gate  *	* VM_SLEEP unsupported
48*7c478bd9Sstevel@tonic-gate  *
49*7c478bd9Sstevel@tonic-gate  *	* Reaping changes
50*7c478bd9Sstevel@tonic-gate  *
51*7c478bd9Sstevel@tonic-gate  *	* initialization changes
52*7c478bd9Sstevel@tonic-gate  *
53*7c478bd9Sstevel@tonic-gate  *	* _vmem_extend_alloc
54*7c478bd9Sstevel@tonic-gate  *
55*7c478bd9Sstevel@tonic-gate  *
56*7c478bd9Sstevel@tonic-gate  * 2. VM_SLEEP Removed
57*7c478bd9Sstevel@tonic-gate  * -------------------
58*7c478bd9Sstevel@tonic-gate  * Since VM_SLEEP allocations can hold locks (in vmem_populate()) for
59*7c478bd9Sstevel@tonic-gate  * possibly infinite amounts of time, they are not supported in this
60*7c478bd9Sstevel@tonic-gate  * version of vmem.  Sleep-like behavior can be achieved through
61*7c478bd9Sstevel@tonic-gate  * UMEM_NOFAIL umem allocations.
62*7c478bd9Sstevel@tonic-gate  *
63*7c478bd9Sstevel@tonic-gate  *
64*7c478bd9Sstevel@tonic-gate  * 3. Reaping changes
65*7c478bd9Sstevel@tonic-gate  * ------------------
66*7c478bd9Sstevel@tonic-gate  * Unlike kmem_reap(), which just asynchronously schedules work, umem_reap()
67*7c478bd9Sstevel@tonic-gate  * can do allocations and frees synchronously.  This is a problem if it
68*7c478bd9Sstevel@tonic-gate  * occurs during a vmem_populate() allocation.
69*7c478bd9Sstevel@tonic-gate  *
70*7c478bd9Sstevel@tonic-gate  * Instead, we delay reaps while populates are active.
71*7c478bd9Sstevel@tonic-gate  *
72*7c478bd9Sstevel@tonic-gate  *
73*7c478bd9Sstevel@tonic-gate  * 4. Initialization changes
74*7c478bd9Sstevel@tonic-gate  * -------------------------
75*7c478bd9Sstevel@tonic-gate  * In the kernel, vmem_init() allows you to create a single, top-level arena,
76*7c478bd9Sstevel@tonic-gate  * which has vmem_internal_arena as a child.  For umem, we want to be able
77*7c478bd9Sstevel@tonic-gate  * to extend arenas dynamically.  It is much easier to support this if we
78*7c478bd9Sstevel@tonic-gate  * allow a two-level "heap" arena:
79*7c478bd9Sstevel@tonic-gate  *
80*7c478bd9Sstevel@tonic-gate  *	+----------+
81*7c478bd9Sstevel@tonic-gate  *	|  "fake"  |
82*7c478bd9Sstevel@tonic-gate  *	+----------+
83*7c478bd9Sstevel@tonic-gate  *	      |
84*7c478bd9Sstevel@tonic-gate  *	+----------+
85*7c478bd9Sstevel@tonic-gate  *	|  "heap"  |
86*7c478bd9Sstevel@tonic-gate  *	+----------+
87*7c478bd9Sstevel@tonic-gate  *	  |    \ \
88*7c478bd9Sstevel@tonic-gate  *	  |     +-+-- ... <other children>
89*7c478bd9Sstevel@tonic-gate  *	  |
90*7c478bd9Sstevel@tonic-gate  *	+---------------+
91*7c478bd9Sstevel@tonic-gate  *	| vmem_internal |
92*7c478bd9Sstevel@tonic-gate  *	+---------------+
93*7c478bd9Sstevel@tonic-gate  *	    | | | |
94*7c478bd9Sstevel@tonic-gate  *	   <children>
95*7c478bd9Sstevel@tonic-gate  *
96*7c478bd9Sstevel@tonic-gate  * The new vmem_init() allows you to specify a "parent" of the heap, along
97*7c478bd9Sstevel@tonic-gate  * with allocation functions.
98*7c478bd9Sstevel@tonic-gate  *
99*7c478bd9Sstevel@tonic-gate  *
100*7c478bd9Sstevel@tonic-gate  * 5. _vmem_extend_alloc
101*7c478bd9Sstevel@tonic-gate  * ---------------------
102*7c478bd9Sstevel@tonic-gate  * The other part of extending is _vmem_extend_alloc.  This function allows
103*7c478bd9Sstevel@tonic-gate  * you to extend (expand current spans, if possible) an arena and allocate
104*7c478bd9Sstevel@tonic-gate  * a chunk of the newly extened span atomically.  This is needed to support
105*7c478bd9Sstevel@tonic-gate  * extending the heap while vmem_populate()ing it.
106*7c478bd9Sstevel@tonic-gate  *
107*7c478bd9Sstevel@tonic-gate  * In order to increase the usefulness of extending, non-imported spans are
108*7c478bd9Sstevel@tonic-gate  * sorted in address order.
109*7c478bd9Sstevel@tonic-gate  */
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate #include "mtlib.h"
112*7c478bd9Sstevel@tonic-gate #include <sys/vmem_impl_user.h>
113*7c478bd9Sstevel@tonic-gate #include <alloca.h>
114*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
115*7c478bd9Sstevel@tonic-gate #include <stdio.h>
116*7c478bd9Sstevel@tonic-gate #include <strings.h>
117*7c478bd9Sstevel@tonic-gate #include <atomic.h>
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate #include "vmem_base.h"
120*7c478bd9Sstevel@tonic-gate #include "umem_base.h"
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate #define	VMEM_INITIAL		6	/* early vmem arenas */
123*7c478bd9Sstevel@tonic-gate #define	VMEM_SEG_INITIAL	100	/* early segments */
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate /*
126*7c478bd9Sstevel@tonic-gate  * Adding a new span to an arena requires two segment structures: one to
127*7c478bd9Sstevel@tonic-gate  * represent the span, and one to represent the free segment it contains.
128*7c478bd9Sstevel@tonic-gate  */
129*7c478bd9Sstevel@tonic-gate #define	VMEM_SEGS_PER_SPAN_CREATE	2
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate /*
132*7c478bd9Sstevel@tonic-gate  * Allocating a piece of an existing segment requires 0-2 segment structures
133*7c478bd9Sstevel@tonic-gate  * depending on how much of the segment we're allocating.
134*7c478bd9Sstevel@tonic-gate  *
135*7c478bd9Sstevel@tonic-gate  * To allocate the entire segment, no new segment structures are needed; we
136*7c478bd9Sstevel@tonic-gate  * simply move the existing segment structure from the freelist to the
137*7c478bd9Sstevel@tonic-gate  * allocation hash table.
138*7c478bd9Sstevel@tonic-gate  *
139*7c478bd9Sstevel@tonic-gate  * To allocate a piece from the left or right end of the segment, we must
140*7c478bd9Sstevel@tonic-gate  * split the segment into two pieces (allocated part and remainder), so we
141*7c478bd9Sstevel@tonic-gate  * need one new segment structure to represent the remainder.
142*7c478bd9Sstevel@tonic-gate  *
143*7c478bd9Sstevel@tonic-gate  * To allocate from the middle of a segment, we need two new segment strucures
144*7c478bd9Sstevel@tonic-gate  * to represent the remainders on either side of the allocated part.
145*7c478bd9Sstevel@tonic-gate  */
146*7c478bd9Sstevel@tonic-gate #define	VMEM_SEGS_PER_EXACT_ALLOC	0
147*7c478bd9Sstevel@tonic-gate #define	VMEM_SEGS_PER_LEFT_ALLOC	1
148*7c478bd9Sstevel@tonic-gate #define	VMEM_SEGS_PER_RIGHT_ALLOC	1
149*7c478bd9Sstevel@tonic-gate #define	VMEM_SEGS_PER_MIDDLE_ALLOC	2
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate /*
152*7c478bd9Sstevel@tonic-gate  * vmem_populate() preallocates segment structures for vmem to do its work.
153*7c478bd9Sstevel@tonic-gate  * It must preallocate enough for the worst case, which is when we must import
154*7c478bd9Sstevel@tonic-gate  * a new span and then allocate from the middle of it.
155*7c478bd9Sstevel@tonic-gate  */
156*7c478bd9Sstevel@tonic-gate #define	VMEM_SEGS_PER_ALLOC_MAX		\
157*7c478bd9Sstevel@tonic-gate 	(VMEM_SEGS_PER_SPAN_CREATE + VMEM_SEGS_PER_MIDDLE_ALLOC)
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate /*
160*7c478bd9Sstevel@tonic-gate  * The segment structures themselves are allocated from vmem_seg_arena, so
161*7c478bd9Sstevel@tonic-gate  * we have a recursion problem when vmem_seg_arena needs to populate itself.
162*7c478bd9Sstevel@tonic-gate  * We address this by working out the maximum number of segment structures
163*7c478bd9Sstevel@tonic-gate  * this act will require, and multiplying by the maximum number of threads
164*7c478bd9Sstevel@tonic-gate  * that we'll allow to do it simultaneously.
165*7c478bd9Sstevel@tonic-gate  *
166*7c478bd9Sstevel@tonic-gate  * The worst-case segment consumption to populate vmem_seg_arena is as
167*7c478bd9Sstevel@tonic-gate  * follows (depicted as a stack trace to indicate why events are occurring):
168*7c478bd9Sstevel@tonic-gate  *
169*7c478bd9Sstevel@tonic-gate  * vmem_alloc(vmem_seg_arena)		-> 2 segs (span create + exact alloc)
170*7c478bd9Sstevel@tonic-gate  *  vmem_alloc(vmem_internal_arena)	-> 2 segs (span create + exact alloc)
171*7c478bd9Sstevel@tonic-gate  *   heap_alloc(heap_arena)
172*7c478bd9Sstevel@tonic-gate  *    vmem_alloc(heap_arena)		-> 4 seg (span create + alloc)
173*7c478bd9Sstevel@tonic-gate  *     parent_alloc(parent_arena)
174*7c478bd9Sstevel@tonic-gate  *	_vmem_extend_alloc(parent_arena) -> 3 seg (span create + left alloc)
175*7c478bd9Sstevel@tonic-gate  *
176*7c478bd9Sstevel@tonic-gate  * Note:  The reservation for heap_arena must be 4, since vmem_xalloc()
177*7c478bd9Sstevel@tonic-gate  * is overly pessimistic on allocations where parent_arena has a stricter
178*7c478bd9Sstevel@tonic-gate  * alignment than heap_arena.
179*7c478bd9Sstevel@tonic-gate  *
180*7c478bd9Sstevel@tonic-gate  * The worst-case consumption for any arena is 4 segment structures.
181*7c478bd9Sstevel@tonic-gate  * For now, we only support VM_NOSLEEP allocations, so as long as we
182*7c478bd9Sstevel@tonic-gate  * serialize all vmem_populates, a 4-seg reserve is sufficient.
183*7c478bd9Sstevel@tonic-gate  */
184*7c478bd9Sstevel@tonic-gate #define	VMEM_POPULATE_SEGS_PER_ARENA	4
185*7c478bd9Sstevel@tonic-gate #define	VMEM_POPULATE_LOCKS		1
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate #define	VMEM_POPULATE_RESERVE		\
188*7c478bd9Sstevel@tonic-gate 	(VMEM_POPULATE_SEGS_PER_ARENA * VMEM_POPULATE_LOCKS)
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate /*
191*7c478bd9Sstevel@tonic-gate  * vmem_populate() ensures that each arena has VMEM_MINFREE seg structures
192*7c478bd9Sstevel@tonic-gate  * so that it can satisfy the worst-case allocation *and* participate in
193*7c478bd9Sstevel@tonic-gate  * worst-case allocation from vmem_seg_arena.
194*7c478bd9Sstevel@tonic-gate  */
195*7c478bd9Sstevel@tonic-gate #define	VMEM_MINFREE	(VMEM_POPULATE_RESERVE + VMEM_SEGS_PER_ALLOC_MAX)
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate /* Don't assume new statics are zeroed - see vmem_startup() */
198*7c478bd9Sstevel@tonic-gate static vmem_t vmem0[VMEM_INITIAL];
199*7c478bd9Sstevel@tonic-gate static vmem_t *vmem_populator[VMEM_INITIAL];
200*7c478bd9Sstevel@tonic-gate static uint32_t vmem_id;
201*7c478bd9Sstevel@tonic-gate static uint32_t vmem_populators;
202*7c478bd9Sstevel@tonic-gate static vmem_seg_t vmem_seg0[VMEM_SEG_INITIAL];
203*7c478bd9Sstevel@tonic-gate static vmem_seg_t *vmem_segfree;
204*7c478bd9Sstevel@tonic-gate static mutex_t vmem_list_lock;
205*7c478bd9Sstevel@tonic-gate static mutex_t vmem_segfree_lock;
206*7c478bd9Sstevel@tonic-gate static vmem_populate_lock_t vmem_nosleep_lock;
207*7c478bd9Sstevel@tonic-gate #define	IN_POPULATE()	(vmem_nosleep_lock.vmpl_thr == thr_self())
208*7c478bd9Sstevel@tonic-gate static vmem_t *vmem_list;
209*7c478bd9Sstevel@tonic-gate static vmem_t *vmem_internal_arena;
210*7c478bd9Sstevel@tonic-gate static vmem_t *vmem_seg_arena;
211*7c478bd9Sstevel@tonic-gate static vmem_t *vmem_hash_arena;
212*7c478bd9Sstevel@tonic-gate static vmem_t *vmem_vmem_arena;
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate vmem_t *vmem_heap;
215*7c478bd9Sstevel@tonic-gate vmem_alloc_t *vmem_heap_alloc;
216*7c478bd9Sstevel@tonic-gate vmem_free_t *vmem_heap_free;
217*7c478bd9Sstevel@tonic-gate 
218*7c478bd9Sstevel@tonic-gate uint32_t vmem_mtbf;		/* mean time between failures [default: off] */
219*7c478bd9Sstevel@tonic-gate size_t vmem_seg_size = sizeof (vmem_seg_t);
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate /*
222*7c478bd9Sstevel@tonic-gate  * we use the _ version, since we don't want to be cancelled.
223*7c478bd9Sstevel@tonic-gate  * Actually, this is automatically taken care of by including "mtlib.h".
224*7c478bd9Sstevel@tonic-gate  */
225*7c478bd9Sstevel@tonic-gate extern int _cond_wait(cond_t *cv, mutex_t *mutex);
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate /*
228*7c478bd9Sstevel@tonic-gate  * Insert/delete from arena list (type 'a') or next-of-kin list (type 'k').
229*7c478bd9Sstevel@tonic-gate  */
230*7c478bd9Sstevel@tonic-gate #define	VMEM_INSERT(vprev, vsp, type)					\
231*7c478bd9Sstevel@tonic-gate {									\
232*7c478bd9Sstevel@tonic-gate 	vmem_seg_t *vnext = (vprev)->vs_##type##next;			\
233*7c478bd9Sstevel@tonic-gate 	(vsp)->vs_##type##next = (vnext);				\
234*7c478bd9Sstevel@tonic-gate 	(vsp)->vs_##type##prev = (vprev);				\
235*7c478bd9Sstevel@tonic-gate 	(vprev)->vs_##type##next = (vsp);				\
236*7c478bd9Sstevel@tonic-gate 	(vnext)->vs_##type##prev = (vsp);				\
237*7c478bd9Sstevel@tonic-gate }
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate #define	VMEM_DELETE(vsp, type)						\
240*7c478bd9Sstevel@tonic-gate {									\
241*7c478bd9Sstevel@tonic-gate 	vmem_seg_t *vprev = (vsp)->vs_##type##prev;			\
242*7c478bd9Sstevel@tonic-gate 	vmem_seg_t *vnext = (vsp)->vs_##type##next;			\
243*7c478bd9Sstevel@tonic-gate 	(vprev)->vs_##type##next = (vnext);				\
244*7c478bd9Sstevel@tonic-gate 	(vnext)->vs_##type##prev = (vprev);				\
245*7c478bd9Sstevel@tonic-gate }
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate /*
248*7c478bd9Sstevel@tonic-gate  * Get a vmem_seg_t from the global segfree list.
249*7c478bd9Sstevel@tonic-gate  */
250*7c478bd9Sstevel@tonic-gate static vmem_seg_t *
251*7c478bd9Sstevel@tonic-gate vmem_getseg_global(void)
252*7c478bd9Sstevel@tonic-gate {
253*7c478bd9Sstevel@tonic-gate 	vmem_seg_t *vsp;
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&vmem_segfree_lock);
256*7c478bd9Sstevel@tonic-gate 	if ((vsp = vmem_segfree) != NULL)
257*7c478bd9Sstevel@tonic-gate 		vmem_segfree = vsp->vs_knext;
258*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&vmem_segfree_lock);
259*7c478bd9Sstevel@tonic-gate 
260*7c478bd9Sstevel@tonic-gate 	return (vsp);
261*7c478bd9Sstevel@tonic-gate }
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate /*
264*7c478bd9Sstevel@tonic-gate  * Put a vmem_seg_t on the global segfree list.
265*7c478bd9Sstevel@tonic-gate  */
266*7c478bd9Sstevel@tonic-gate static void
267*7c478bd9Sstevel@tonic-gate vmem_putseg_global(vmem_seg_t *vsp)
268*7c478bd9Sstevel@tonic-gate {
269*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&vmem_segfree_lock);
270*7c478bd9Sstevel@tonic-gate 	vsp->vs_knext = vmem_segfree;
271*7c478bd9Sstevel@tonic-gate 	vmem_segfree = vsp;
272*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&vmem_segfree_lock);
273*7c478bd9Sstevel@tonic-gate }
274*7c478bd9Sstevel@tonic-gate 
275*7c478bd9Sstevel@tonic-gate /*
276*7c478bd9Sstevel@tonic-gate  * Get a vmem_seg_t from vmp's segfree list.
277*7c478bd9Sstevel@tonic-gate  */
278*7c478bd9Sstevel@tonic-gate static vmem_seg_t *
279*7c478bd9Sstevel@tonic-gate vmem_getseg(vmem_t *vmp)
280*7c478bd9Sstevel@tonic-gate {
281*7c478bd9Sstevel@tonic-gate 	vmem_seg_t *vsp;
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate 	ASSERT(vmp->vm_nsegfree > 0);
284*7c478bd9Sstevel@tonic-gate 
285*7c478bd9Sstevel@tonic-gate 	vsp = vmp->vm_segfree;
286*7c478bd9Sstevel@tonic-gate 	vmp->vm_segfree = vsp->vs_knext;
287*7c478bd9Sstevel@tonic-gate 	vmp->vm_nsegfree--;
288*7c478bd9Sstevel@tonic-gate 
289*7c478bd9Sstevel@tonic-gate 	return (vsp);
290*7c478bd9Sstevel@tonic-gate }
291*7c478bd9Sstevel@tonic-gate 
292*7c478bd9Sstevel@tonic-gate /*
293*7c478bd9Sstevel@tonic-gate  * Put a vmem_seg_t on vmp's segfree list.
294*7c478bd9Sstevel@tonic-gate  */
295*7c478bd9Sstevel@tonic-gate static void
296*7c478bd9Sstevel@tonic-gate vmem_putseg(vmem_t *vmp, vmem_seg_t *vsp)
297*7c478bd9Sstevel@tonic-gate {
298*7c478bd9Sstevel@tonic-gate 	vsp->vs_knext = vmp->vm_segfree;
299*7c478bd9Sstevel@tonic-gate 	vmp->vm_segfree = vsp;
300*7c478bd9Sstevel@tonic-gate 	vmp->vm_nsegfree++;
301*7c478bd9Sstevel@tonic-gate }
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate /*
304*7c478bd9Sstevel@tonic-gate  * Add vsp to the appropriate freelist.
305*7c478bd9Sstevel@tonic-gate  */
306*7c478bd9Sstevel@tonic-gate static void
307*7c478bd9Sstevel@tonic-gate vmem_freelist_insert(vmem_t *vmp, vmem_seg_t *vsp)
308*7c478bd9Sstevel@tonic-gate {
309*7c478bd9Sstevel@tonic-gate 	vmem_seg_t *vprev;
310*7c478bd9Sstevel@tonic-gate 
311*7c478bd9Sstevel@tonic-gate 	ASSERT(*VMEM_HASH(vmp, vsp->vs_start) != vsp);
312*7c478bd9Sstevel@tonic-gate 
313*7c478bd9Sstevel@tonic-gate 	vprev = (vmem_seg_t *)&vmp->vm_freelist[highbit(VS_SIZE(vsp)) - 1];
314*7c478bd9Sstevel@tonic-gate 	vsp->vs_type = VMEM_FREE;
315*7c478bd9Sstevel@tonic-gate 	vmp->vm_freemap |= VS_SIZE(vprev);
316*7c478bd9Sstevel@tonic-gate 	VMEM_INSERT(vprev, vsp, k);
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate 	(void) cond_broadcast(&vmp->vm_cv);
319*7c478bd9Sstevel@tonic-gate }
320*7c478bd9Sstevel@tonic-gate 
321*7c478bd9Sstevel@tonic-gate /*
322*7c478bd9Sstevel@tonic-gate  * Take vsp from the freelist.
323*7c478bd9Sstevel@tonic-gate  */
324*7c478bd9Sstevel@tonic-gate static void
325*7c478bd9Sstevel@tonic-gate vmem_freelist_delete(vmem_t *vmp, vmem_seg_t *vsp)
326*7c478bd9Sstevel@tonic-gate {
327*7c478bd9Sstevel@tonic-gate 	ASSERT(*VMEM_HASH(vmp, vsp->vs_start) != vsp);
328*7c478bd9Sstevel@tonic-gate 	ASSERT(vsp->vs_type == VMEM_FREE);
329*7c478bd9Sstevel@tonic-gate 
330*7c478bd9Sstevel@tonic-gate 	if (vsp->vs_knext->vs_start == 0 && vsp->vs_kprev->vs_start == 0) {
331*7c478bd9Sstevel@tonic-gate 		/*
332*7c478bd9Sstevel@tonic-gate 		 * The segments on both sides of 'vsp' are freelist heads,
333*7c478bd9Sstevel@tonic-gate 		 * so taking vsp leaves the freelist at vsp->vs_kprev empty.
334*7c478bd9Sstevel@tonic-gate 		 */
335*7c478bd9Sstevel@tonic-gate 		ASSERT(vmp->vm_freemap & VS_SIZE(vsp->vs_kprev));
336*7c478bd9Sstevel@tonic-gate 		vmp->vm_freemap ^= VS_SIZE(vsp->vs_kprev);
337*7c478bd9Sstevel@tonic-gate 	}
338*7c478bd9Sstevel@tonic-gate 	VMEM_DELETE(vsp, k);
339*7c478bd9Sstevel@tonic-gate }
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate /*
342*7c478bd9Sstevel@tonic-gate  * Add vsp to the allocated-segment hash table and update kstats.
343*7c478bd9Sstevel@tonic-gate  */
344*7c478bd9Sstevel@tonic-gate static void
345*7c478bd9Sstevel@tonic-gate vmem_hash_insert(vmem_t *vmp, vmem_seg_t *vsp)
346*7c478bd9Sstevel@tonic-gate {
347*7c478bd9Sstevel@tonic-gate 	vmem_seg_t **bucket;
348*7c478bd9Sstevel@tonic-gate 
349*7c478bd9Sstevel@tonic-gate 	vsp->vs_type = VMEM_ALLOC;
350*7c478bd9Sstevel@tonic-gate 	bucket = VMEM_HASH(vmp, vsp->vs_start);
351*7c478bd9Sstevel@tonic-gate 	vsp->vs_knext = *bucket;
352*7c478bd9Sstevel@tonic-gate 	*bucket = vsp;
353*7c478bd9Sstevel@tonic-gate 
354*7c478bd9Sstevel@tonic-gate 	if (vmem_seg_size == sizeof (vmem_seg_t)) {
355*7c478bd9Sstevel@tonic-gate 		vsp->vs_depth = (uint8_t)getpcstack(vsp->vs_stack,
356*7c478bd9Sstevel@tonic-gate 		    VMEM_STACK_DEPTH, 0);
357*7c478bd9Sstevel@tonic-gate 		vsp->vs_thread = thr_self();
358*7c478bd9Sstevel@tonic-gate 		vsp->vs_timestamp = gethrtime();
359*7c478bd9Sstevel@tonic-gate 	} else {
360*7c478bd9Sstevel@tonic-gate 		vsp->vs_depth = 0;
361*7c478bd9Sstevel@tonic-gate 	}
362*7c478bd9Sstevel@tonic-gate 
363*7c478bd9Sstevel@tonic-gate 	vmp->vm_kstat.vk_alloc++;
364*7c478bd9Sstevel@tonic-gate 	vmp->vm_kstat.vk_mem_inuse += VS_SIZE(vsp);
365*7c478bd9Sstevel@tonic-gate }
366*7c478bd9Sstevel@tonic-gate 
367*7c478bd9Sstevel@tonic-gate /*
368*7c478bd9Sstevel@tonic-gate  * Remove vsp from the allocated-segment hash table and update kstats.
369*7c478bd9Sstevel@tonic-gate  */
370*7c478bd9Sstevel@tonic-gate static vmem_seg_t *
371*7c478bd9Sstevel@tonic-gate vmem_hash_delete(vmem_t *vmp, uintptr_t addr, size_t size)
372*7c478bd9Sstevel@tonic-gate {
373*7c478bd9Sstevel@tonic-gate 	vmem_seg_t *vsp, **prev_vspp;
374*7c478bd9Sstevel@tonic-gate 
375*7c478bd9Sstevel@tonic-gate 	prev_vspp = VMEM_HASH(vmp, addr);
376*7c478bd9Sstevel@tonic-gate 	while ((vsp = *prev_vspp) != NULL) {
377*7c478bd9Sstevel@tonic-gate 		if (vsp->vs_start == addr) {
378*7c478bd9Sstevel@tonic-gate 			*prev_vspp = vsp->vs_knext;
379*7c478bd9Sstevel@tonic-gate 			break;
380*7c478bd9Sstevel@tonic-gate 		}
381*7c478bd9Sstevel@tonic-gate 		vmp->vm_kstat.vk_lookup++;
382*7c478bd9Sstevel@tonic-gate 		prev_vspp = &vsp->vs_knext;
383*7c478bd9Sstevel@tonic-gate 	}
384*7c478bd9Sstevel@tonic-gate 
385*7c478bd9Sstevel@tonic-gate 	if (vsp == NULL) {
386*7c478bd9Sstevel@tonic-gate 		umem_panic("vmem_hash_delete(%p, %lx, %lu): bad free",
387*7c478bd9Sstevel@tonic-gate 		    vmp, addr, size);
388*7c478bd9Sstevel@tonic-gate 	}
389*7c478bd9Sstevel@tonic-gate 	if (VS_SIZE(vsp) != size) {
390*7c478bd9Sstevel@tonic-gate 		umem_panic("vmem_hash_delete(%p, %lx, %lu): wrong size "
391*7c478bd9Sstevel@tonic-gate 		    "(expect %lu)", vmp, addr, size, VS_SIZE(vsp));
392*7c478bd9Sstevel@tonic-gate 	}
393*7c478bd9Sstevel@tonic-gate 
394*7c478bd9Sstevel@tonic-gate 	vmp->vm_kstat.vk_free++;
395*7c478bd9Sstevel@tonic-gate 	vmp->vm_kstat.vk_mem_inuse -= size;
396*7c478bd9Sstevel@tonic-gate 
397*7c478bd9Sstevel@tonic-gate 	return (vsp);
398*7c478bd9Sstevel@tonic-gate }
399*7c478bd9Sstevel@tonic-gate 
400*7c478bd9Sstevel@tonic-gate /*
401*7c478bd9Sstevel@tonic-gate  * Create a segment spanning the range [start, end) and add it to the arena.
402*7c478bd9Sstevel@tonic-gate  */
403*7c478bd9Sstevel@tonic-gate static vmem_seg_t *
404*7c478bd9Sstevel@tonic-gate vmem_seg_create(vmem_t *vmp, vmem_seg_t *vprev, uintptr_t start, uintptr_t end)
405*7c478bd9Sstevel@tonic-gate {
406*7c478bd9Sstevel@tonic-gate 	vmem_seg_t *newseg = vmem_getseg(vmp);
407*7c478bd9Sstevel@tonic-gate 
408*7c478bd9Sstevel@tonic-gate 	newseg->vs_start = start;
409*7c478bd9Sstevel@tonic-gate 	newseg->vs_end = end;
410*7c478bd9Sstevel@tonic-gate 	newseg->vs_type = 0;
411*7c478bd9Sstevel@tonic-gate 	newseg->vs_import = 0;
412*7c478bd9Sstevel@tonic-gate 
413*7c478bd9Sstevel@tonic-gate 	VMEM_INSERT(vprev, newseg, a);
414*7c478bd9Sstevel@tonic-gate 
415*7c478bd9Sstevel@tonic-gate 	return (newseg);
416*7c478bd9Sstevel@tonic-gate }
417*7c478bd9Sstevel@tonic-gate 
418*7c478bd9Sstevel@tonic-gate /*
419*7c478bd9Sstevel@tonic-gate  * Remove segment vsp from the arena.
420*7c478bd9Sstevel@tonic-gate  */
421*7c478bd9Sstevel@tonic-gate static void
422*7c478bd9Sstevel@tonic-gate vmem_seg_destroy(vmem_t *vmp, vmem_seg_t *vsp)
423*7c478bd9Sstevel@tonic-gate {
424*7c478bd9Sstevel@tonic-gate 	ASSERT(vsp->vs_type != VMEM_ROTOR);
425*7c478bd9Sstevel@tonic-gate 	VMEM_DELETE(vsp, a);
426*7c478bd9Sstevel@tonic-gate 
427*7c478bd9Sstevel@tonic-gate 	vmem_putseg(vmp, vsp);
428*7c478bd9Sstevel@tonic-gate }
429*7c478bd9Sstevel@tonic-gate 
430*7c478bd9Sstevel@tonic-gate /*
431*7c478bd9Sstevel@tonic-gate  * Add the span [vaddr, vaddr + size) to vmp and update kstats.
432*7c478bd9Sstevel@tonic-gate  */
433*7c478bd9Sstevel@tonic-gate static vmem_seg_t *
434*7c478bd9Sstevel@tonic-gate vmem_span_create(vmem_t *vmp, void *vaddr, size_t size, uint8_t import)
435*7c478bd9Sstevel@tonic-gate {
436*7c478bd9Sstevel@tonic-gate 	vmem_seg_t *knext;
437*7c478bd9Sstevel@tonic-gate 	vmem_seg_t *newseg, *span;
438*7c478bd9Sstevel@tonic-gate 	uintptr_t start = (uintptr_t)vaddr;
439*7c478bd9Sstevel@tonic-gate 	uintptr_t end = start + size;
440*7c478bd9Sstevel@tonic-gate 
441*7c478bd9Sstevel@tonic-gate 	knext = &vmp->vm_seg0;
442*7c478bd9Sstevel@tonic-gate 	if (!import && vmp->vm_source_alloc == NULL) {
443*7c478bd9Sstevel@tonic-gate 		vmem_seg_t *kend, *kprev;
444*7c478bd9Sstevel@tonic-gate 		/*
445*7c478bd9Sstevel@tonic-gate 		 * non-imported spans are sorted in address order.  This
446*7c478bd9Sstevel@tonic-gate 		 * makes vmem_extend_unlocked() much more effective.
447*7c478bd9Sstevel@tonic-gate 		 *
448*7c478bd9Sstevel@tonic-gate 		 * We search in reverse order, since new spans are
449*7c478bd9Sstevel@tonic-gate 		 * generally at higher addresses.
450*7c478bd9Sstevel@tonic-gate 		 */
451*7c478bd9Sstevel@tonic-gate 		kend = &vmp->vm_seg0;
452*7c478bd9Sstevel@tonic-gate 		for (kprev = kend->vs_kprev; kprev != kend;
453*7c478bd9Sstevel@tonic-gate 		    kprev = kprev->vs_kprev) {
454*7c478bd9Sstevel@tonic-gate 			if (!kprev->vs_import && (kprev->vs_end - 1) < start)
455*7c478bd9Sstevel@tonic-gate 				break;
456*7c478bd9Sstevel@tonic-gate 		}
457*7c478bd9Sstevel@tonic-gate 		knext = kprev->vs_knext;
458*7c478bd9Sstevel@tonic-gate 	}
459*7c478bd9Sstevel@tonic-gate 
460*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&vmp->vm_lock));
461*7c478bd9Sstevel@tonic-gate 
462*7c478bd9Sstevel@tonic-gate 	if ((start | end) & (vmp->vm_quantum - 1)) {
463*7c478bd9Sstevel@tonic-gate 		umem_panic("vmem_span_create(%p, %p, %lu): misaligned",
464*7c478bd9Sstevel@tonic-gate 		    vmp, vaddr, size);
465*7c478bd9Sstevel@tonic-gate 	}
466*7c478bd9Sstevel@tonic-gate 
467*7c478bd9Sstevel@tonic-gate 	span = vmem_seg_create(vmp, knext->vs_aprev, start, end);
468*7c478bd9Sstevel@tonic-gate 	span->vs_type = VMEM_SPAN;
469*7c478bd9Sstevel@tonic-gate 	VMEM_INSERT(knext->vs_kprev, span, k);
470*7c478bd9Sstevel@tonic-gate 
471*7c478bd9Sstevel@tonic-gate 	newseg = vmem_seg_create(vmp, span, start, end);
472*7c478bd9Sstevel@tonic-gate 	vmem_freelist_insert(vmp, newseg);
473*7c478bd9Sstevel@tonic-gate 
474*7c478bd9Sstevel@tonic-gate 	newseg->vs_import = import;
475*7c478bd9Sstevel@tonic-gate 	if (import)
476*7c478bd9Sstevel@tonic-gate 		vmp->vm_kstat.vk_mem_import += size;
477*7c478bd9Sstevel@tonic-gate 	vmp->vm_kstat.vk_mem_total += size;
478*7c478bd9Sstevel@tonic-gate 
479*7c478bd9Sstevel@tonic-gate 	return (newseg);
480*7c478bd9Sstevel@tonic-gate }
481*7c478bd9Sstevel@tonic-gate 
482*7c478bd9Sstevel@tonic-gate /*
483*7c478bd9Sstevel@tonic-gate  * Remove span vsp from vmp and update kstats.
484*7c478bd9Sstevel@tonic-gate  */
485*7c478bd9Sstevel@tonic-gate static void
486*7c478bd9Sstevel@tonic-gate vmem_span_destroy(vmem_t *vmp, vmem_seg_t *vsp)
487*7c478bd9Sstevel@tonic-gate {
488*7c478bd9Sstevel@tonic-gate 	vmem_seg_t *span = vsp->vs_aprev;
489*7c478bd9Sstevel@tonic-gate 	size_t size = VS_SIZE(vsp);
490*7c478bd9Sstevel@tonic-gate 
491*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&vmp->vm_lock));
492*7c478bd9Sstevel@tonic-gate 	ASSERT(span->vs_type == VMEM_SPAN);
493*7c478bd9Sstevel@tonic-gate 
494*7c478bd9Sstevel@tonic-gate 	if (vsp->vs_import)
495*7c478bd9Sstevel@tonic-gate 		vmp->vm_kstat.vk_mem_import -= size;
496*7c478bd9Sstevel@tonic-gate 	vmp->vm_kstat.vk_mem_total -= size;
497*7c478bd9Sstevel@tonic-gate 
498*7c478bd9Sstevel@tonic-gate 	VMEM_DELETE(span, k);
499*7c478bd9Sstevel@tonic-gate 
500*7c478bd9Sstevel@tonic-gate 	vmem_seg_destroy(vmp, vsp);
501*7c478bd9Sstevel@tonic-gate 	vmem_seg_destroy(vmp, span);
502*7c478bd9Sstevel@tonic-gate }
503*7c478bd9Sstevel@tonic-gate 
504*7c478bd9Sstevel@tonic-gate /*
505*7c478bd9Sstevel@tonic-gate  * Allocate the subrange [addr, addr + size) from segment vsp.
506*7c478bd9Sstevel@tonic-gate  * If there are leftovers on either side, place them on the freelist.
507*7c478bd9Sstevel@tonic-gate  * Returns a pointer to the segment representing [addr, addr + size).
508*7c478bd9Sstevel@tonic-gate  */
509*7c478bd9Sstevel@tonic-gate static vmem_seg_t *
510*7c478bd9Sstevel@tonic-gate vmem_seg_alloc(vmem_t *vmp, vmem_seg_t *vsp, uintptr_t addr, size_t size)
511*7c478bd9Sstevel@tonic-gate {
512*7c478bd9Sstevel@tonic-gate 	uintptr_t vs_start = vsp->vs_start;
513*7c478bd9Sstevel@tonic-gate 	uintptr_t vs_end = vsp->vs_end;
514*7c478bd9Sstevel@tonic-gate 	size_t vs_size = vs_end - vs_start;
515*7c478bd9Sstevel@tonic-gate 	size_t realsize = P2ROUNDUP(size, vmp->vm_quantum);
516*7c478bd9Sstevel@tonic-gate 	uintptr_t addr_end = addr + realsize;
517*7c478bd9Sstevel@tonic-gate 
518*7c478bd9Sstevel@tonic-gate 	ASSERT(P2PHASE(vs_start, vmp->vm_quantum) == 0);
519*7c478bd9Sstevel@tonic-gate 	ASSERT(P2PHASE(addr, vmp->vm_quantum) == 0);
520*7c478bd9Sstevel@tonic-gate 	ASSERT(vsp->vs_type == VMEM_FREE);
521*7c478bd9Sstevel@tonic-gate 	ASSERT(addr >= vs_start && addr_end - 1 <= vs_end - 1);
522*7c478bd9Sstevel@tonic-gate 	ASSERT(addr - 1 <= addr_end - 1);
523*7c478bd9Sstevel@tonic-gate 
524*7c478bd9Sstevel@tonic-gate 	/*
525*7c478bd9Sstevel@tonic-gate 	 * If we're allocating from the start of the segment, and the
526*7c478bd9Sstevel@tonic-gate 	 * remainder will be on the same freelist, we can save quite
527*7c478bd9Sstevel@tonic-gate 	 * a bit of work.
528*7c478bd9Sstevel@tonic-gate 	 */
529*7c478bd9Sstevel@tonic-gate 	if (P2SAMEHIGHBIT(vs_size, vs_size - realsize) && addr == vs_start) {
530*7c478bd9Sstevel@tonic-gate 		ASSERT(highbit(vs_size) == highbit(vs_size - realsize));
531*7c478bd9Sstevel@tonic-gate 		vsp->vs_start = addr_end;
532*7c478bd9Sstevel@tonic-gate 		vsp = vmem_seg_create(vmp, vsp->vs_aprev, addr, addr + size);
533*7c478bd9Sstevel@tonic-gate 		vmem_hash_insert(vmp, vsp);
534*7c478bd9Sstevel@tonic-gate 		return (vsp);
535*7c478bd9Sstevel@tonic-gate 	}
536*7c478bd9Sstevel@tonic-gate 
537*7c478bd9Sstevel@tonic-gate 	vmem_freelist_delete(vmp, vsp);
538*7c478bd9Sstevel@tonic-gate 
539*7c478bd9Sstevel@tonic-gate 	if (vs_end != addr_end)
540*7c478bd9Sstevel@tonic-gate 		vmem_freelist_insert(vmp,
541*7c478bd9Sstevel@tonic-gate 		    vmem_seg_create(vmp, vsp, addr_end, vs_end));
542*7c478bd9Sstevel@tonic-gate 
543*7c478bd9Sstevel@tonic-gate 	if (vs_start != addr)
544*7c478bd9Sstevel@tonic-gate 		vmem_freelist_insert(vmp,
545*7c478bd9Sstevel@tonic-gate 		    vmem_seg_create(vmp, vsp->vs_aprev, vs_start, addr));
546*7c478bd9Sstevel@tonic-gate 
547*7c478bd9Sstevel@tonic-gate 	vsp->vs_start = addr;
548*7c478bd9Sstevel@tonic-gate 	vsp->vs_end = addr + size;
549*7c478bd9Sstevel@tonic-gate 
550*7c478bd9Sstevel@tonic-gate 	vmem_hash_insert(vmp, vsp);
551*7c478bd9Sstevel@tonic-gate 	return (vsp);
552*7c478bd9Sstevel@tonic-gate }
553*7c478bd9Sstevel@tonic-gate 
554*7c478bd9Sstevel@tonic-gate /*
555*7c478bd9Sstevel@tonic-gate  * We cannot reap if we are in the middle of a vmem_populate().
556*7c478bd9Sstevel@tonic-gate  */
557*7c478bd9Sstevel@tonic-gate void
558*7c478bd9Sstevel@tonic-gate vmem_reap(void)
559*7c478bd9Sstevel@tonic-gate {
560*7c478bd9Sstevel@tonic-gate 	if (!IN_POPULATE())
561*7c478bd9Sstevel@tonic-gate 		umem_reap();
562*7c478bd9Sstevel@tonic-gate }
563*7c478bd9Sstevel@tonic-gate 
564*7c478bd9Sstevel@tonic-gate /*
565*7c478bd9Sstevel@tonic-gate  * Populate vmp's segfree list with VMEM_MINFREE vmem_seg_t structures.
566*7c478bd9Sstevel@tonic-gate  */
567*7c478bd9Sstevel@tonic-gate static int
568*7c478bd9Sstevel@tonic-gate vmem_populate(vmem_t *vmp, int vmflag)
569*7c478bd9Sstevel@tonic-gate {
570*7c478bd9Sstevel@tonic-gate 	char *p;
571*7c478bd9Sstevel@tonic-gate 	vmem_seg_t *vsp;
572*7c478bd9Sstevel@tonic-gate 	ssize_t nseg;
573*7c478bd9Sstevel@tonic-gate 	size_t size;
574*7c478bd9Sstevel@tonic-gate 	vmem_populate_lock_t *lp;
575*7c478bd9Sstevel@tonic-gate 	int i;
576*7c478bd9Sstevel@tonic-gate 
577*7c478bd9Sstevel@tonic-gate 	while (vmp->vm_nsegfree < VMEM_MINFREE &&
578*7c478bd9Sstevel@tonic-gate 	    (vsp = vmem_getseg_global()) != NULL)
579*7c478bd9Sstevel@tonic-gate 		vmem_putseg(vmp, vsp);
580*7c478bd9Sstevel@tonic-gate 
581*7c478bd9Sstevel@tonic-gate 	if (vmp->vm_nsegfree >= VMEM_MINFREE)
582*7c478bd9Sstevel@tonic-gate 		return (1);
583*7c478bd9Sstevel@tonic-gate 
584*7c478bd9Sstevel@tonic-gate 	/*
585*7c478bd9Sstevel@tonic-gate 	 * If we're already populating, tap the reserve.
586*7c478bd9Sstevel@tonic-gate 	 */
587*7c478bd9Sstevel@tonic-gate 	if (vmem_nosleep_lock.vmpl_thr == thr_self()) {
588*7c478bd9Sstevel@tonic-gate 		ASSERT(vmp->vm_cflags & VMC_POPULATOR);
589*7c478bd9Sstevel@tonic-gate 		return (1);
590*7c478bd9Sstevel@tonic-gate 	}
591*7c478bd9Sstevel@tonic-gate 
592*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&vmp->vm_lock);
593*7c478bd9Sstevel@tonic-gate 
594*7c478bd9Sstevel@tonic-gate 	ASSERT(vmflag & VM_NOSLEEP);	/* we do not allow sleep allocations */
595*7c478bd9Sstevel@tonic-gate 	lp = &vmem_nosleep_lock;
596*7c478bd9Sstevel@tonic-gate 
597*7c478bd9Sstevel@tonic-gate 	/*
598*7c478bd9Sstevel@tonic-gate 	 * Cannot be just a mutex_lock(), since that has no effect if
599*7c478bd9Sstevel@tonic-gate 	 * libthread is not linked.
600*7c478bd9Sstevel@tonic-gate 	 */
601*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&lp->vmpl_mutex);
602*7c478bd9Sstevel@tonic-gate 	ASSERT(lp->vmpl_thr == 0);
603*7c478bd9Sstevel@tonic-gate 	lp->vmpl_thr = thr_self();
604*7c478bd9Sstevel@tonic-gate 
605*7c478bd9Sstevel@tonic-gate 	nseg = VMEM_MINFREE + vmem_populators * VMEM_POPULATE_RESERVE;
606*7c478bd9Sstevel@tonic-gate 	size = P2ROUNDUP(nseg * vmem_seg_size, vmem_seg_arena->vm_quantum);
607*7c478bd9Sstevel@tonic-gate 	nseg = size / vmem_seg_size;
608*7c478bd9Sstevel@tonic-gate 
609*7c478bd9Sstevel@tonic-gate 	/*
610*7c478bd9Sstevel@tonic-gate 	 * The following vmem_alloc() may need to populate vmem_seg_arena
611*7c478bd9Sstevel@tonic-gate 	 * and all the things it imports from.  When doing so, it will tap
612*7c478bd9Sstevel@tonic-gate 	 * each arena's reserve to prevent recursion (see the block comment
613*7c478bd9Sstevel@tonic-gate 	 * above the definition of VMEM_POPULATE_RESERVE).
614*7c478bd9Sstevel@tonic-gate 	 *
615*7c478bd9Sstevel@tonic-gate 	 * During this allocation, vmem_reap() is a no-op.  If the allocation
616*7c478bd9Sstevel@tonic-gate 	 * fails, we call vmem_reap() after dropping the population lock.
617*7c478bd9Sstevel@tonic-gate 	 */
618*7c478bd9Sstevel@tonic-gate 	p = vmem_alloc(vmem_seg_arena, size, vmflag & VM_UMFLAGS);
619*7c478bd9Sstevel@tonic-gate 	if (p == NULL) {
620*7c478bd9Sstevel@tonic-gate 		lp->vmpl_thr = 0;
621*7c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&lp->vmpl_mutex);
622*7c478bd9Sstevel@tonic-gate 		vmem_reap();
623*7c478bd9Sstevel@tonic-gate 
624*7c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&vmp->vm_lock);
625*7c478bd9Sstevel@tonic-gate 		vmp->vm_kstat.vk_populate_fail++;
626*7c478bd9Sstevel@tonic-gate 		return (0);
627*7c478bd9Sstevel@tonic-gate 	}
628*7c478bd9Sstevel@tonic-gate 	/*
629*7c478bd9Sstevel@tonic-gate 	 * Restock the arenas that may have been depleted during population.
630*7c478bd9Sstevel@tonic-gate 	 */
631*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < vmem_populators; i++) {
632*7c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&vmem_populator[i]->vm_lock);
633*7c478bd9Sstevel@tonic-gate 		while (vmem_populator[i]->vm_nsegfree < VMEM_POPULATE_RESERVE)
634*7c478bd9Sstevel@tonic-gate 			vmem_putseg(vmem_populator[i],
635*7c478bd9Sstevel@tonic-gate 			    (vmem_seg_t *)(p + --nseg * vmem_seg_size));
636*7c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&vmem_populator[i]->vm_lock);
637*7c478bd9Sstevel@tonic-gate 	}
638*7c478bd9Sstevel@tonic-gate 
639*7c478bd9Sstevel@tonic-gate 	lp->vmpl_thr = 0;
640*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&lp->vmpl_mutex);
641*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&vmp->vm_lock);
642*7c478bd9Sstevel@tonic-gate 
643*7c478bd9Sstevel@tonic-gate 	/*
644*7c478bd9Sstevel@tonic-gate 	 * Now take our own segments.
645*7c478bd9Sstevel@tonic-gate 	 */
646*7c478bd9Sstevel@tonic-gate 	ASSERT(nseg >= VMEM_MINFREE);
647*7c478bd9Sstevel@tonic-gate 	while (vmp->vm_nsegfree < VMEM_MINFREE)
648*7c478bd9Sstevel@tonic-gate 		vmem_putseg(vmp, (vmem_seg_t *)(p + --nseg * vmem_seg_size));
649*7c478bd9Sstevel@tonic-gate 
650*7c478bd9Sstevel@tonic-gate 	/*
651*7c478bd9Sstevel@tonic-gate 	 * Give the remainder to charity.
652*7c478bd9Sstevel@tonic-gate 	 */
653*7c478bd9Sstevel@tonic-gate 	while (nseg > 0)
654*7c478bd9Sstevel@tonic-gate 		vmem_putseg_global((vmem_seg_t *)(p + --nseg * vmem_seg_size));
655*7c478bd9Sstevel@tonic-gate 
656*7c478bd9Sstevel@tonic-gate 	return (1);
657*7c478bd9Sstevel@tonic-gate }
658*7c478bd9Sstevel@tonic-gate 
659*7c478bd9Sstevel@tonic-gate /*
660*7c478bd9Sstevel@tonic-gate  * Advance a walker from its previous position to 'afterme'.
661*7c478bd9Sstevel@tonic-gate  * Note: may drop and reacquire vmp->vm_lock.
662*7c478bd9Sstevel@tonic-gate  */
663*7c478bd9Sstevel@tonic-gate static void
664*7c478bd9Sstevel@tonic-gate vmem_advance(vmem_t *vmp, vmem_seg_t *walker, vmem_seg_t *afterme)
665*7c478bd9Sstevel@tonic-gate {
666*7c478bd9Sstevel@tonic-gate 	vmem_seg_t *vprev = walker->vs_aprev;
667*7c478bd9Sstevel@tonic-gate 	vmem_seg_t *vnext = walker->vs_anext;
668*7c478bd9Sstevel@tonic-gate 	vmem_seg_t *vsp = NULL;
669*7c478bd9Sstevel@tonic-gate 
670*7c478bd9Sstevel@tonic-gate 	VMEM_DELETE(walker, a);
671*7c478bd9Sstevel@tonic-gate 
672*7c478bd9Sstevel@tonic-gate 	if (afterme != NULL)
673*7c478bd9Sstevel@tonic-gate 		VMEM_INSERT(afterme, walker, a);
674*7c478bd9Sstevel@tonic-gate 
675*7c478bd9Sstevel@tonic-gate 	/*
676*7c478bd9Sstevel@tonic-gate 	 * The walker segment's presence may have prevented its neighbors
677*7c478bd9Sstevel@tonic-gate 	 * from coalescing.  If so, coalesce them now.
678*7c478bd9Sstevel@tonic-gate 	 */
679*7c478bd9Sstevel@tonic-gate 	if (vprev->vs_type == VMEM_FREE) {
680*7c478bd9Sstevel@tonic-gate 		if (vnext->vs_type == VMEM_FREE) {
681*7c478bd9Sstevel@tonic-gate 			ASSERT(vprev->vs_end == vnext->vs_start);
682*7c478bd9Sstevel@tonic-gate 			vmem_freelist_delete(vmp, vnext);
683*7c478bd9Sstevel@tonic-gate 			vmem_freelist_delete(vmp, vprev);
684*7c478bd9Sstevel@tonic-gate 			vprev->vs_end = vnext->vs_end;
685*7c478bd9Sstevel@tonic-gate 			vmem_freelist_insert(vmp, vprev);
686*7c478bd9Sstevel@tonic-gate 			vmem_seg_destroy(vmp, vnext);
687*7c478bd9Sstevel@tonic-gate 		}
688*7c478bd9Sstevel@tonic-gate 		vsp = vprev;
689*7c478bd9Sstevel@tonic-gate 	} else if (vnext->vs_type == VMEM_FREE) {
690*7c478bd9Sstevel@tonic-gate 		vsp = vnext;
691*7c478bd9Sstevel@tonic-gate 	}
692*7c478bd9Sstevel@tonic-gate 
693*7c478bd9Sstevel@tonic-gate 	/*
694*7c478bd9Sstevel@tonic-gate 	 * vsp could represent a complete imported span,
695*7c478bd9Sstevel@tonic-gate 	 * in which case we must return it to the source.
696*7c478bd9Sstevel@tonic-gate 	 */
697*7c478bd9Sstevel@tonic-gate 	if (vsp != NULL && vsp->vs_import && vmp->vm_source_free != NULL &&
698*7c478bd9Sstevel@tonic-gate 	    vsp->vs_aprev->vs_type == VMEM_SPAN &&
699*7c478bd9Sstevel@tonic-gate 	    vsp->vs_anext->vs_type == VMEM_SPAN) {
700*7c478bd9Sstevel@tonic-gate 		void *vaddr = (void *)vsp->vs_start;
701*7c478bd9Sstevel@tonic-gate 		size_t size = VS_SIZE(vsp);
702*7c478bd9Sstevel@tonic-gate 		ASSERT(size == VS_SIZE(vsp->vs_aprev));
703*7c478bd9Sstevel@tonic-gate 		vmem_freelist_delete(vmp, vsp);
704*7c478bd9Sstevel@tonic-gate 		vmem_span_destroy(vmp, vsp);
705*7c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&vmp->vm_lock);
706*7c478bd9Sstevel@tonic-gate 		vmp->vm_source_free(vmp->vm_source, vaddr, size);
707*7c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&vmp->vm_lock);
708*7c478bd9Sstevel@tonic-gate 	}
709*7c478bd9Sstevel@tonic-gate }
710*7c478bd9Sstevel@tonic-gate 
711*7c478bd9Sstevel@tonic-gate /*
712*7c478bd9Sstevel@tonic-gate  * VM_NEXTFIT allocations deliberately cycle through all virtual addresses
713*7c478bd9Sstevel@tonic-gate  * in an arena, so that we avoid reusing addresses for as long as possible.
714*7c478bd9Sstevel@tonic-gate  * This helps to catch used-after-freed bugs.  It's also the perfect policy
715*7c478bd9Sstevel@tonic-gate  * for allocating things like process IDs, where we want to cycle through
716*7c478bd9Sstevel@tonic-gate  * all values in order.
717*7c478bd9Sstevel@tonic-gate  */
718*7c478bd9Sstevel@tonic-gate static void *
719*7c478bd9Sstevel@tonic-gate vmem_nextfit_alloc(vmem_t *vmp, size_t size, int vmflag)
720*7c478bd9Sstevel@tonic-gate {
721*7c478bd9Sstevel@tonic-gate 	vmem_seg_t *vsp, *rotor;
722*7c478bd9Sstevel@tonic-gate 	uintptr_t addr;
723*7c478bd9Sstevel@tonic-gate 	size_t realsize = P2ROUNDUP(size, vmp->vm_quantum);
724*7c478bd9Sstevel@tonic-gate 	size_t vs_size;
725*7c478bd9Sstevel@tonic-gate 
726*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&vmp->vm_lock);
727*7c478bd9Sstevel@tonic-gate 
728*7c478bd9Sstevel@tonic-gate 	if (vmp->vm_nsegfree < VMEM_MINFREE && !vmem_populate(vmp, vmflag)) {
729*7c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&vmp->vm_lock);
730*7c478bd9Sstevel@tonic-gate 		return (NULL);
731*7c478bd9Sstevel@tonic-gate 	}
732*7c478bd9Sstevel@tonic-gate 
733*7c478bd9Sstevel@tonic-gate 	/*
734*7c478bd9Sstevel@tonic-gate 	 * The common case is that the segment right after the rotor is free,
735*7c478bd9Sstevel@tonic-gate 	 * and large enough that extracting 'size' bytes won't change which
736*7c478bd9Sstevel@tonic-gate 	 * freelist it's on.  In this case we can avoid a *lot* of work.
737*7c478bd9Sstevel@tonic-gate 	 * Instead of the normal vmem_seg_alloc(), we just advance the start
738*7c478bd9Sstevel@tonic-gate 	 * address of the victim segment.  Instead of moving the rotor, we
739*7c478bd9Sstevel@tonic-gate 	 * create the new segment structure *behind the rotor*, which has
740*7c478bd9Sstevel@tonic-gate 	 * the same effect.  And finally, we know we don't have to coalesce
741*7c478bd9Sstevel@tonic-gate 	 * the rotor's neighbors because the new segment lies between them.
742*7c478bd9Sstevel@tonic-gate 	 */
743*7c478bd9Sstevel@tonic-gate 	rotor = &vmp->vm_rotor;
744*7c478bd9Sstevel@tonic-gate 	vsp = rotor->vs_anext;
745*7c478bd9Sstevel@tonic-gate 	if (vsp->vs_type == VMEM_FREE && (vs_size = VS_SIZE(vsp)) > realsize &&
746*7c478bd9Sstevel@tonic-gate 	    P2SAMEHIGHBIT(vs_size, vs_size - realsize)) {
747*7c478bd9Sstevel@tonic-gate 		ASSERT(highbit(vs_size) == highbit(vs_size - realsize));
748*7c478bd9Sstevel@tonic-gate 		addr = vsp->vs_start;
749*7c478bd9Sstevel@tonic-gate 		vsp->vs_start = addr + realsize;
750*7c478bd9Sstevel@tonic-gate 		vmem_hash_insert(vmp,
751*7c478bd9Sstevel@tonic-gate 		    vmem_seg_create(vmp, rotor->vs_aprev, addr, addr + size));
752*7c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&vmp->vm_lock);
753*7c478bd9Sstevel@tonic-gate 		return ((void *)addr);
754*7c478bd9Sstevel@tonic-gate 	}
755*7c478bd9Sstevel@tonic-gate 
756*7c478bd9Sstevel@tonic-gate 	/*
757*7c478bd9Sstevel@tonic-gate 	 * Starting at the rotor, look for a segment large enough to
758*7c478bd9Sstevel@tonic-gate 	 * satisfy the allocation.
759*7c478bd9Sstevel@tonic-gate 	 */
760*7c478bd9Sstevel@tonic-gate 	for (;;) {
761*7c478bd9Sstevel@tonic-gate 		vmp->vm_kstat.vk_search++;
762*7c478bd9Sstevel@tonic-gate 		if (vsp->vs_type == VMEM_FREE && VS_SIZE(vsp) >= size)
763*7c478bd9Sstevel@tonic-gate 			break;
764*7c478bd9Sstevel@tonic-gate 		vsp = vsp->vs_anext;
765*7c478bd9Sstevel@tonic-gate 		if (vsp == rotor) {
766*7c478bd9Sstevel@tonic-gate 			/*
767*7c478bd9Sstevel@tonic-gate 			 * We've come full circle.  One possibility is that the
768*7c478bd9Sstevel@tonic-gate 			 * there's actually enough space, but the rotor itself
769*7c478bd9Sstevel@tonic-gate 			 * is preventing the allocation from succeeding because
770*7c478bd9Sstevel@tonic-gate 			 * it's sitting between two free segments.  Therefore,
771*7c478bd9Sstevel@tonic-gate 			 * we advance the rotor and see if that liberates a
772*7c478bd9Sstevel@tonic-gate 			 * suitable segment.
773*7c478bd9Sstevel@tonic-gate 			 */
774*7c478bd9Sstevel@tonic-gate 			vmem_advance(vmp, rotor, rotor->vs_anext);
775*7c478bd9Sstevel@tonic-gate 			vsp = rotor->vs_aprev;
776*7c478bd9Sstevel@tonic-gate 			if (vsp->vs_type == VMEM_FREE && VS_SIZE(vsp) >= size)
777*7c478bd9Sstevel@tonic-gate 				break;
778*7c478bd9Sstevel@tonic-gate 			/*
779*7c478bd9Sstevel@tonic-gate 			 * If there's a lower arena we can import from, or it's
780*7c478bd9Sstevel@tonic-gate 			 * a VM_NOSLEEP allocation, let vmem_xalloc() handle it.
781*7c478bd9Sstevel@tonic-gate 			 * Otherwise, wait until another thread frees something.
782*7c478bd9Sstevel@tonic-gate 			 */
783*7c478bd9Sstevel@tonic-gate 			if (vmp->vm_source_alloc != NULL ||
784*7c478bd9Sstevel@tonic-gate 			    (vmflag & VM_NOSLEEP)) {
785*7c478bd9Sstevel@tonic-gate 				(void) mutex_unlock(&vmp->vm_lock);
786*7c478bd9Sstevel@tonic-gate 				return (vmem_xalloc(vmp, size, vmp->vm_quantum,
787*7c478bd9Sstevel@tonic-gate 				    0, 0, NULL, NULL, vmflag & VM_UMFLAGS));
788*7c478bd9Sstevel@tonic-gate 			}
789*7c478bd9Sstevel@tonic-gate 			vmp->vm_kstat.vk_wait++;
790*7c478bd9Sstevel@tonic-gate 			(void) _cond_wait(&vmp->vm_cv, &vmp->vm_lock);
791*7c478bd9Sstevel@tonic-gate 			vsp = rotor->vs_anext;
792*7c478bd9Sstevel@tonic-gate 		}
793*7c478bd9Sstevel@tonic-gate 	}
794*7c478bd9Sstevel@tonic-gate 
795*7c478bd9Sstevel@tonic-gate 	/*
796*7c478bd9Sstevel@tonic-gate 	 * We found a segment.  Extract enough space to satisfy the allocation.
797*7c478bd9Sstevel@tonic-gate 	 */
798*7c478bd9Sstevel@tonic-gate 	addr = vsp->vs_start;
799*7c478bd9Sstevel@tonic-gate 	vsp = vmem_seg_alloc(vmp, vsp, addr, size);
800*7c478bd9Sstevel@tonic-gate 	ASSERT(vsp->vs_type == VMEM_ALLOC &&
801*7c478bd9Sstevel@tonic-gate 	    vsp->vs_start == addr && vsp->vs_end == addr + size);
802*7c478bd9Sstevel@tonic-gate 
803*7c478bd9Sstevel@tonic-gate 	/*
804*7c478bd9Sstevel@tonic-gate 	 * Advance the rotor to right after the newly-allocated segment.
805*7c478bd9Sstevel@tonic-gate 	 * That's where the next VM_NEXTFIT allocation will begin searching.
806*7c478bd9Sstevel@tonic-gate 	 */
807*7c478bd9Sstevel@tonic-gate 	vmem_advance(vmp, rotor, vsp);
808*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&vmp->vm_lock);
809*7c478bd9Sstevel@tonic-gate 	return ((void *)addr);
810*7c478bd9Sstevel@tonic-gate }
811*7c478bd9Sstevel@tonic-gate 
812*7c478bd9Sstevel@tonic-gate /*
813*7c478bd9Sstevel@tonic-gate  * Allocate size bytes at offset phase from an align boundary such that the
814*7c478bd9Sstevel@tonic-gate  * resulting segment [addr, addr + size) is a subset of [minaddr, maxaddr)
815*7c478bd9Sstevel@tonic-gate  * that does not straddle a nocross-aligned boundary.
816*7c478bd9Sstevel@tonic-gate  */
817*7c478bd9Sstevel@tonic-gate void *
818*7c478bd9Sstevel@tonic-gate vmem_xalloc(vmem_t *vmp, size_t size, size_t align, size_t phase,
819*7c478bd9Sstevel@tonic-gate 	size_t nocross, void *minaddr, void *maxaddr, int vmflag)
820*7c478bd9Sstevel@tonic-gate {
821*7c478bd9Sstevel@tonic-gate 	vmem_seg_t *vsp;
822*7c478bd9Sstevel@tonic-gate 	vmem_seg_t *vbest = NULL;
823*7c478bd9Sstevel@tonic-gate 	uintptr_t addr, taddr, start, end;
824*7c478bd9Sstevel@tonic-gate 	void *vaddr;
825*7c478bd9Sstevel@tonic-gate 	int hb, flist, resv;
826*7c478bd9Sstevel@tonic-gate 	uint32_t mtbf;
827*7c478bd9Sstevel@tonic-gate 
828*7c478bd9Sstevel@tonic-gate 	if (phase > 0 && phase >= align)
829*7c478bd9Sstevel@tonic-gate 		umem_panic("vmem_xalloc(%p, %lu, %lu, %lu, %lu, %p, %p, %x): "
830*7c478bd9Sstevel@tonic-gate 		    "invalid phase",
831*7c478bd9Sstevel@tonic-gate 		    (void *)vmp, size, align, phase, nocross,
832*7c478bd9Sstevel@tonic-gate 		    minaddr, maxaddr, vmflag);
833*7c478bd9Sstevel@tonic-gate 
834*7c478bd9Sstevel@tonic-gate 	if (align == 0)
835*7c478bd9Sstevel@tonic-gate 		align = vmp->vm_quantum;
836*7c478bd9Sstevel@tonic-gate 
837*7c478bd9Sstevel@tonic-gate 	if ((align | phase | nocross) & (vmp->vm_quantum - 1)) {
838*7c478bd9Sstevel@tonic-gate 		umem_panic("vmem_xalloc(%p, %lu, %lu, %lu, %lu, %p, %p, %x): "
839*7c478bd9Sstevel@tonic-gate 		    "parameters not vm_quantum aligned",
840*7c478bd9Sstevel@tonic-gate 		    (void *)vmp, size, align, phase, nocross,
841*7c478bd9Sstevel@tonic-gate 		    minaddr, maxaddr, vmflag);
842*7c478bd9Sstevel@tonic-gate 	}
843*7c478bd9Sstevel@tonic-gate 
844*7c478bd9Sstevel@tonic-gate 	if (nocross != 0 &&
845*7c478bd9Sstevel@tonic-gate 	    (align > nocross || P2ROUNDUP(phase + size, align) > nocross)) {
846*7c478bd9Sstevel@tonic-gate 		umem_panic("vmem_xalloc(%p, %lu, %lu, %lu, %lu, %p, %p, %x): "
847*7c478bd9Sstevel@tonic-gate 		    "overconstrained allocation",
848*7c478bd9Sstevel@tonic-gate 		    (void *)vmp, size, align, phase, nocross,
849*7c478bd9Sstevel@tonic-gate 		    minaddr, maxaddr, vmflag);
850*7c478bd9Sstevel@tonic-gate 	}
851*7c478bd9Sstevel@tonic-gate 
852*7c478bd9Sstevel@tonic-gate 	if ((mtbf = vmem_mtbf | vmp->vm_mtbf) != 0 && gethrtime() % mtbf == 0 &&
853*7c478bd9Sstevel@tonic-gate 	    (vmflag & (VM_NOSLEEP | VM_PANIC)) == VM_NOSLEEP)
854*7c478bd9Sstevel@tonic-gate 		return (NULL);
855*7c478bd9Sstevel@tonic-gate 
856*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&vmp->vm_lock);
857*7c478bd9Sstevel@tonic-gate 	for (;;) {
858*7c478bd9Sstevel@tonic-gate 		if (vmp->vm_nsegfree < VMEM_MINFREE &&
859*7c478bd9Sstevel@tonic-gate 		    !vmem_populate(vmp, vmflag))
860*7c478bd9Sstevel@tonic-gate 			break;
861*7c478bd9Sstevel@tonic-gate 
862*7c478bd9Sstevel@tonic-gate 		/*
863*7c478bd9Sstevel@tonic-gate 		 * highbit() returns the highest bit + 1, which is exactly
864*7c478bd9Sstevel@tonic-gate 		 * what we want: we want to search the first freelist whose
865*7c478bd9Sstevel@tonic-gate 		 * members are *definitely* large enough to satisfy our
866*7c478bd9Sstevel@tonic-gate 		 * allocation.  However, there are certain cases in which we
867*7c478bd9Sstevel@tonic-gate 		 * want to look at the next-smallest freelist (which *might*
868*7c478bd9Sstevel@tonic-gate 		 * be able to satisfy the allocation):
869*7c478bd9Sstevel@tonic-gate 		 *
870*7c478bd9Sstevel@tonic-gate 		 * (1)	The size is exactly a power of 2, in which case
871*7c478bd9Sstevel@tonic-gate 		 *	the smaller freelist is always big enough;
872*7c478bd9Sstevel@tonic-gate 		 *
873*7c478bd9Sstevel@tonic-gate 		 * (2)	All other freelists are empty;
874*7c478bd9Sstevel@tonic-gate 		 *
875*7c478bd9Sstevel@tonic-gate 		 * (3)	We're in the highest possible freelist, which is
876*7c478bd9Sstevel@tonic-gate 		 *	always empty (e.g. the 4GB freelist on 32-bit systems);
877*7c478bd9Sstevel@tonic-gate 		 *
878*7c478bd9Sstevel@tonic-gate 		 * (4)	We're doing a best-fit or first-fit allocation.
879*7c478bd9Sstevel@tonic-gate 		 */
880*7c478bd9Sstevel@tonic-gate 		if ((size & (size - 1)) == 0) {
881*7c478bd9Sstevel@tonic-gate 			flist = lowbit(P2ALIGN(vmp->vm_freemap, size));
882*7c478bd9Sstevel@tonic-gate 		} else {
883*7c478bd9Sstevel@tonic-gate 			hb = highbit(size);
884*7c478bd9Sstevel@tonic-gate 			if ((vmp->vm_freemap >> hb) == 0 ||
885*7c478bd9Sstevel@tonic-gate 			    hb == VMEM_FREELISTS ||
886*7c478bd9Sstevel@tonic-gate 			    (vmflag & (VM_BESTFIT | VM_FIRSTFIT)))
887*7c478bd9Sstevel@tonic-gate 				hb--;
888*7c478bd9Sstevel@tonic-gate 			flist = lowbit(P2ALIGN(vmp->vm_freemap, 1UL << hb));
889*7c478bd9Sstevel@tonic-gate 		}
890*7c478bd9Sstevel@tonic-gate 
891*7c478bd9Sstevel@tonic-gate 		for (vbest = NULL, vsp = (flist == 0) ? NULL :
892*7c478bd9Sstevel@tonic-gate 		    vmp->vm_freelist[flist - 1].vs_knext;
893*7c478bd9Sstevel@tonic-gate 		    vsp != NULL; vsp = vsp->vs_knext) {
894*7c478bd9Sstevel@tonic-gate 			vmp->vm_kstat.vk_search++;
895*7c478bd9Sstevel@tonic-gate 			if (vsp->vs_start == 0) {
896*7c478bd9Sstevel@tonic-gate 				/*
897*7c478bd9Sstevel@tonic-gate 				 * We're moving up to a larger freelist,
898*7c478bd9Sstevel@tonic-gate 				 * so if we've already found a candidate,
899*7c478bd9Sstevel@tonic-gate 				 * the fit can't possibly get any better.
900*7c478bd9Sstevel@tonic-gate 				 */
901*7c478bd9Sstevel@tonic-gate 				if (vbest != NULL)
902*7c478bd9Sstevel@tonic-gate 					break;
903*7c478bd9Sstevel@tonic-gate 				/*
904*7c478bd9Sstevel@tonic-gate 				 * Find the next non-empty freelist.
905*7c478bd9Sstevel@tonic-gate 				 */
906*7c478bd9Sstevel@tonic-gate 				flist = lowbit(P2ALIGN(vmp->vm_freemap,
907*7c478bd9Sstevel@tonic-gate 				    VS_SIZE(vsp)));
908*7c478bd9Sstevel@tonic-gate 				if (flist-- == 0)
909*7c478bd9Sstevel@tonic-gate 					break;
910*7c478bd9Sstevel@tonic-gate 				vsp = (vmem_seg_t *)&vmp->vm_freelist[flist];
911*7c478bd9Sstevel@tonic-gate 				ASSERT(vsp->vs_knext->vs_type == VMEM_FREE);
912*7c478bd9Sstevel@tonic-gate 				continue;
913*7c478bd9Sstevel@tonic-gate 			}
914*7c478bd9Sstevel@tonic-gate 			if (vsp->vs_end - 1 < (uintptr_t)minaddr)
915*7c478bd9Sstevel@tonic-gate 				continue;
916*7c478bd9Sstevel@tonic-gate 			if (vsp->vs_start > (uintptr_t)maxaddr - 1)
917*7c478bd9Sstevel@tonic-gate 				continue;
918*7c478bd9Sstevel@tonic-gate 			start = MAX(vsp->vs_start, (uintptr_t)minaddr);
919*7c478bd9Sstevel@tonic-gate 			end = MIN(vsp->vs_end - 1, (uintptr_t)maxaddr - 1) + 1;
920*7c478bd9Sstevel@tonic-gate 			taddr = P2PHASEUP(start, align, phase);
921*7c478bd9Sstevel@tonic-gate 			if (P2CROSS(taddr, taddr + size - 1, nocross))
922*7c478bd9Sstevel@tonic-gate 				taddr +=
923*7c478bd9Sstevel@tonic-gate 				    P2ROUNDUP(P2NPHASE(taddr, nocross), align);
924*7c478bd9Sstevel@tonic-gate 			if ((taddr - start) + size > end - start ||
925*7c478bd9Sstevel@tonic-gate 			    (vbest != NULL && VS_SIZE(vsp) >= VS_SIZE(vbest)))
926*7c478bd9Sstevel@tonic-gate 				continue;
927*7c478bd9Sstevel@tonic-gate 			vbest = vsp;
928*7c478bd9Sstevel@tonic-gate 			addr = taddr;
929*7c478bd9Sstevel@tonic-gate 			if (!(vmflag & VM_BESTFIT) || VS_SIZE(vbest) == size)
930*7c478bd9Sstevel@tonic-gate 				break;
931*7c478bd9Sstevel@tonic-gate 		}
932*7c478bd9Sstevel@tonic-gate 		if (vbest != NULL)
933*7c478bd9Sstevel@tonic-gate 			break;
934*7c478bd9Sstevel@tonic-gate 		if (size == 0)
935*7c478bd9Sstevel@tonic-gate 			umem_panic("vmem_xalloc(): size == 0");
936*7c478bd9Sstevel@tonic-gate 		if (vmp->vm_source_alloc != NULL && nocross == 0 &&
937*7c478bd9Sstevel@tonic-gate 		    minaddr == NULL && maxaddr == NULL) {
938*7c478bd9Sstevel@tonic-gate 			size_t asize = P2ROUNDUP(size + phase,
939*7c478bd9Sstevel@tonic-gate 			    MAX(align, vmp->vm_source->vm_quantum));
940*7c478bd9Sstevel@tonic-gate 			if (asize < size) {		/* overflow */
941*7c478bd9Sstevel@tonic-gate 				(void) mutex_unlock(&vmp->vm_lock);
942*7c478bd9Sstevel@tonic-gate 				if (vmflag & VM_NOSLEEP)
943*7c478bd9Sstevel@tonic-gate 					return (NULL);
944*7c478bd9Sstevel@tonic-gate 
945*7c478bd9Sstevel@tonic-gate 				umem_panic("vmem_xalloc(): "
946*7c478bd9Sstevel@tonic-gate 				    "overflow on VM_SLEEP allocation");
947*7c478bd9Sstevel@tonic-gate 			}
948*7c478bd9Sstevel@tonic-gate 			/*
949*7c478bd9Sstevel@tonic-gate 			 * Determine how many segment structures we'll consume.
950*7c478bd9Sstevel@tonic-gate 			 * The calculation must be presise because if we're
951*7c478bd9Sstevel@tonic-gate 			 * here on behalf of vmem_populate(), we are taking
952*7c478bd9Sstevel@tonic-gate 			 * segments from a very limited reserve.
953*7c478bd9Sstevel@tonic-gate 			 */
954*7c478bd9Sstevel@tonic-gate 			resv = (size == asize) ?
955*7c478bd9Sstevel@tonic-gate 			    VMEM_SEGS_PER_SPAN_CREATE +
956*7c478bd9Sstevel@tonic-gate 			    VMEM_SEGS_PER_EXACT_ALLOC :
957*7c478bd9Sstevel@tonic-gate 			    VMEM_SEGS_PER_ALLOC_MAX;
958*7c478bd9Sstevel@tonic-gate 			ASSERT(vmp->vm_nsegfree >= resv);
959*7c478bd9Sstevel@tonic-gate 			vmp->vm_nsegfree -= resv;	/* reserve our segs */
960*7c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&vmp->vm_lock);
961*7c478bd9Sstevel@tonic-gate 			vaddr = vmp->vm_source_alloc(vmp->vm_source, asize,
962*7c478bd9Sstevel@tonic-gate 			    vmflag & VM_UMFLAGS);
963*7c478bd9Sstevel@tonic-gate 			(void) mutex_lock(&vmp->vm_lock);
964*7c478bd9Sstevel@tonic-gate 			vmp->vm_nsegfree += resv;	/* claim reservation */
965*7c478bd9Sstevel@tonic-gate 			if (vaddr != NULL) {
966*7c478bd9Sstevel@tonic-gate 				vbest = vmem_span_create(vmp, vaddr, asize, 1);
967*7c478bd9Sstevel@tonic-gate 				addr = P2PHASEUP(vbest->vs_start, align, phase);
968*7c478bd9Sstevel@tonic-gate 				break;
969*7c478bd9Sstevel@tonic-gate 			}
970*7c478bd9Sstevel@tonic-gate 		}
971*7c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&vmp->vm_lock);
972*7c478bd9Sstevel@tonic-gate 		vmem_reap();
973*7c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&vmp->vm_lock);
974*7c478bd9Sstevel@tonic-gate 		if (vmflag & VM_NOSLEEP)
975*7c478bd9Sstevel@tonic-gate 			break;
976*7c478bd9Sstevel@tonic-gate 		vmp->vm_kstat.vk_wait++;
977*7c478bd9Sstevel@tonic-gate 		(void) _cond_wait(&vmp->vm_cv, &vmp->vm_lock);
978*7c478bd9Sstevel@tonic-gate 	}
979*7c478bd9Sstevel@tonic-gate 	if (vbest != NULL) {
980*7c478bd9Sstevel@tonic-gate 		ASSERT(vbest->vs_type == VMEM_FREE);
981*7c478bd9Sstevel@tonic-gate 		ASSERT(vbest->vs_knext != vbest);
982*7c478bd9Sstevel@tonic-gate 		(void) vmem_seg_alloc(vmp, vbest, addr, size);
983*7c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&vmp->vm_lock);
984*7c478bd9Sstevel@tonic-gate 		ASSERT(P2PHASE(addr, align) == phase);
985*7c478bd9Sstevel@tonic-gate 		ASSERT(!P2CROSS(addr, addr + size - 1, nocross));
986*7c478bd9Sstevel@tonic-gate 		ASSERT(addr >= (uintptr_t)minaddr);
987*7c478bd9Sstevel@tonic-gate 		ASSERT(addr + size - 1 <= (uintptr_t)maxaddr - 1);
988*7c478bd9Sstevel@tonic-gate 		return ((void *)addr);
989*7c478bd9Sstevel@tonic-gate 	}
990*7c478bd9Sstevel@tonic-gate 	vmp->vm_kstat.vk_fail++;
991*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&vmp->vm_lock);
992*7c478bd9Sstevel@tonic-gate 	if (vmflag & VM_PANIC)
993*7c478bd9Sstevel@tonic-gate 		umem_panic("vmem_xalloc(%p, %lu, %lu, %lu, %lu, %p, %p, %x): "
994*7c478bd9Sstevel@tonic-gate 		    "cannot satisfy mandatory allocation",
995*7c478bd9Sstevel@tonic-gate 		    (void *)vmp, size, align, phase, nocross,
996*7c478bd9Sstevel@tonic-gate 		    minaddr, maxaddr, vmflag);
997*7c478bd9Sstevel@tonic-gate 	return (NULL);
998*7c478bd9Sstevel@tonic-gate }
999*7c478bd9Sstevel@tonic-gate 
1000*7c478bd9Sstevel@tonic-gate /*
1001*7c478bd9Sstevel@tonic-gate  * Free the segment [vaddr, vaddr + size), where vaddr was a constrained
1002*7c478bd9Sstevel@tonic-gate  * allocation.  vmem_xalloc() and vmem_xfree() must always be paired because
1003*7c478bd9Sstevel@tonic-gate  * both routines bypass the quantum caches.
1004*7c478bd9Sstevel@tonic-gate  */
1005*7c478bd9Sstevel@tonic-gate void
1006*7c478bd9Sstevel@tonic-gate vmem_xfree(vmem_t *vmp, void *vaddr, size_t size)
1007*7c478bd9Sstevel@tonic-gate {
1008*7c478bd9Sstevel@tonic-gate 	vmem_seg_t *vsp, *vnext, *vprev;
1009*7c478bd9Sstevel@tonic-gate 
1010*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&vmp->vm_lock);
1011*7c478bd9Sstevel@tonic-gate 
1012*7c478bd9Sstevel@tonic-gate 	vsp = vmem_hash_delete(vmp, (uintptr_t)vaddr, size);
1013*7c478bd9Sstevel@tonic-gate 	vsp->vs_end = P2ROUNDUP(vsp->vs_end, vmp->vm_quantum);
1014*7c478bd9Sstevel@tonic-gate 
1015*7c478bd9Sstevel@tonic-gate 	/*
1016*7c478bd9Sstevel@tonic-gate 	 * Attempt to coalesce with the next segment.
1017*7c478bd9Sstevel@tonic-gate 	 */
1018*7c478bd9Sstevel@tonic-gate 	vnext = vsp->vs_anext;
1019*7c478bd9Sstevel@tonic-gate 	if (vnext->vs_type == VMEM_FREE) {
1020*7c478bd9Sstevel@tonic-gate 		ASSERT(vsp->vs_end == vnext->vs_start);
1021*7c478bd9Sstevel@tonic-gate 		vmem_freelist_delete(vmp, vnext);
1022*7c478bd9Sstevel@tonic-gate 		vsp->vs_end = vnext->vs_end;
1023*7c478bd9Sstevel@tonic-gate 		vmem_seg_destroy(vmp, vnext);
1024*7c478bd9Sstevel@tonic-gate 	}
1025*7c478bd9Sstevel@tonic-gate 
1026*7c478bd9Sstevel@tonic-gate 	/*
1027*7c478bd9Sstevel@tonic-gate 	 * Attempt to coalesce with the previous segment.
1028*7c478bd9Sstevel@tonic-gate 	 */
1029*7c478bd9Sstevel@tonic-gate 	vprev = vsp->vs_aprev;
1030*7c478bd9Sstevel@tonic-gate 	if (vprev->vs_type == VMEM_FREE) {
1031*7c478bd9Sstevel@tonic-gate 		ASSERT(vprev->vs_end == vsp->vs_start);
1032*7c478bd9Sstevel@tonic-gate 		vmem_freelist_delete(vmp, vprev);
1033*7c478bd9Sstevel@tonic-gate 		vprev->vs_end = vsp->vs_end;
1034*7c478bd9Sstevel@tonic-gate 		vmem_seg_destroy(vmp, vsp);
1035*7c478bd9Sstevel@tonic-gate 		vsp = vprev;
1036*7c478bd9Sstevel@tonic-gate 	}
1037*7c478bd9Sstevel@tonic-gate 
1038*7c478bd9Sstevel@tonic-gate 	/*
1039*7c478bd9Sstevel@tonic-gate 	 * If the entire span is free, return it to the source.
1040*7c478bd9Sstevel@tonic-gate 	 */
1041*7c478bd9Sstevel@tonic-gate 	if (vsp->vs_import && vmp->vm_source_free != NULL &&
1042*7c478bd9Sstevel@tonic-gate 	    vsp->vs_aprev->vs_type == VMEM_SPAN &&
1043*7c478bd9Sstevel@tonic-gate 	    vsp->vs_anext->vs_type == VMEM_SPAN) {
1044*7c478bd9Sstevel@tonic-gate 		vaddr = (void *)vsp->vs_start;
1045*7c478bd9Sstevel@tonic-gate 		size = VS_SIZE(vsp);
1046*7c478bd9Sstevel@tonic-gate 		ASSERT(size == VS_SIZE(vsp->vs_aprev));
1047*7c478bd9Sstevel@tonic-gate 		vmem_span_destroy(vmp, vsp);
1048*7c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&vmp->vm_lock);
1049*7c478bd9Sstevel@tonic-gate 		vmp->vm_source_free(vmp->vm_source, vaddr, size);
1050*7c478bd9Sstevel@tonic-gate 	} else {
1051*7c478bd9Sstevel@tonic-gate 		vmem_freelist_insert(vmp, vsp);
1052*7c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&vmp->vm_lock);
1053*7c478bd9Sstevel@tonic-gate 	}
1054*7c478bd9Sstevel@tonic-gate }
1055*7c478bd9Sstevel@tonic-gate 
1056*7c478bd9Sstevel@tonic-gate /*
1057*7c478bd9Sstevel@tonic-gate  * Allocate size bytes from arena vmp.  Returns the allocated address
1058*7c478bd9Sstevel@tonic-gate  * on success, NULL on failure.  vmflag specifies VM_SLEEP or VM_NOSLEEP,
1059*7c478bd9Sstevel@tonic-gate  * and may also specify best-fit, first-fit, or next-fit allocation policy
1060*7c478bd9Sstevel@tonic-gate  * instead of the default instant-fit policy.  VM_SLEEP allocations are
1061*7c478bd9Sstevel@tonic-gate  * guaranteed to succeed.
1062*7c478bd9Sstevel@tonic-gate  */
1063*7c478bd9Sstevel@tonic-gate void *
1064*7c478bd9Sstevel@tonic-gate vmem_alloc(vmem_t *vmp, size_t size, int vmflag)
1065*7c478bd9Sstevel@tonic-gate {
1066*7c478bd9Sstevel@tonic-gate 	vmem_seg_t *vsp;
1067*7c478bd9Sstevel@tonic-gate 	uintptr_t addr;
1068*7c478bd9Sstevel@tonic-gate 	int hb;
1069*7c478bd9Sstevel@tonic-gate 	int flist = 0;
1070*7c478bd9Sstevel@tonic-gate 	uint32_t mtbf;
1071*7c478bd9Sstevel@tonic-gate 
1072*7c478bd9Sstevel@tonic-gate 	if (size - 1 < vmp->vm_qcache_max) {
1073*7c478bd9Sstevel@tonic-gate 		ASSERT(vmflag & VM_NOSLEEP);
1074*7c478bd9Sstevel@tonic-gate 		return (_umem_cache_alloc(vmp->vm_qcache[(size - 1) >>
1075*7c478bd9Sstevel@tonic-gate 		    vmp->vm_qshift], UMEM_DEFAULT));
1076*7c478bd9Sstevel@tonic-gate 	}
1077*7c478bd9Sstevel@tonic-gate 
1078*7c478bd9Sstevel@tonic-gate 	if ((mtbf = vmem_mtbf | vmp->vm_mtbf) != 0 && gethrtime() % mtbf == 0 &&
1079*7c478bd9Sstevel@tonic-gate 	    (vmflag & (VM_NOSLEEP | VM_PANIC)) == VM_NOSLEEP)
1080*7c478bd9Sstevel@tonic-gate 		return (NULL);
1081*7c478bd9Sstevel@tonic-gate 
1082*7c478bd9Sstevel@tonic-gate 	if (vmflag & VM_NEXTFIT)
1083*7c478bd9Sstevel@tonic-gate 		return (vmem_nextfit_alloc(vmp, size, vmflag));
1084*7c478bd9Sstevel@tonic-gate 
1085*7c478bd9Sstevel@tonic-gate 	if (vmflag & (VM_BESTFIT | VM_FIRSTFIT))
1086*7c478bd9Sstevel@tonic-gate 		return (vmem_xalloc(vmp, size, vmp->vm_quantum, 0, 0,
1087*7c478bd9Sstevel@tonic-gate 		    NULL, NULL, vmflag));
1088*7c478bd9Sstevel@tonic-gate 
1089*7c478bd9Sstevel@tonic-gate 	/*
1090*7c478bd9Sstevel@tonic-gate 	 * Unconstrained instant-fit allocation from the segment list.
1091*7c478bd9Sstevel@tonic-gate 	 */
1092*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&vmp->vm_lock);
1093*7c478bd9Sstevel@tonic-gate 
1094*7c478bd9Sstevel@tonic-gate 	if (vmp->vm_nsegfree >= VMEM_MINFREE || vmem_populate(vmp, vmflag)) {
1095*7c478bd9Sstevel@tonic-gate 		if ((size & (size - 1)) == 0)
1096*7c478bd9Sstevel@tonic-gate 			flist = lowbit(P2ALIGN(vmp->vm_freemap, size));
1097*7c478bd9Sstevel@tonic-gate 		else if ((hb = highbit(size)) < VMEM_FREELISTS)
1098*7c478bd9Sstevel@tonic-gate 			flist = lowbit(P2ALIGN(vmp->vm_freemap, 1UL << hb));
1099*7c478bd9Sstevel@tonic-gate 	}
1100*7c478bd9Sstevel@tonic-gate 
1101*7c478bd9Sstevel@tonic-gate 	if (flist-- == 0) {
1102*7c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&vmp->vm_lock);
1103*7c478bd9Sstevel@tonic-gate 		return (vmem_xalloc(vmp, size, vmp->vm_quantum,
1104*7c478bd9Sstevel@tonic-gate 		    0, 0, NULL, NULL, vmflag));
1105*7c478bd9Sstevel@tonic-gate 	}
1106*7c478bd9Sstevel@tonic-gate 
1107*7c478bd9Sstevel@tonic-gate 	ASSERT(size <= (1UL << flist));
1108*7c478bd9Sstevel@tonic-gate 	vsp = vmp->vm_freelist[flist].vs_knext;
1109*7c478bd9Sstevel@tonic-gate 	addr = vsp->vs_start;
1110*7c478bd9Sstevel@tonic-gate 	(void) vmem_seg_alloc(vmp, vsp, addr, size);
1111*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&vmp->vm_lock);
1112*7c478bd9Sstevel@tonic-gate 	return ((void *)addr);
1113*7c478bd9Sstevel@tonic-gate }
1114*7c478bd9Sstevel@tonic-gate 
1115*7c478bd9Sstevel@tonic-gate /*
1116*7c478bd9Sstevel@tonic-gate  * Free the segment [vaddr, vaddr + size).
1117*7c478bd9Sstevel@tonic-gate  */
1118*7c478bd9Sstevel@tonic-gate void
1119*7c478bd9Sstevel@tonic-gate vmem_free(vmem_t *vmp, void *vaddr, size_t size)
1120*7c478bd9Sstevel@tonic-gate {
1121*7c478bd9Sstevel@tonic-gate 	if (size - 1 < vmp->vm_qcache_max)
1122*7c478bd9Sstevel@tonic-gate 		_umem_cache_free(vmp->vm_qcache[(size - 1) >> vmp->vm_qshift],
1123*7c478bd9Sstevel@tonic-gate 		    vaddr);
1124*7c478bd9Sstevel@tonic-gate 	else
1125*7c478bd9Sstevel@tonic-gate 		vmem_xfree(vmp, vaddr, size);
1126*7c478bd9Sstevel@tonic-gate }
1127*7c478bd9Sstevel@tonic-gate 
1128*7c478bd9Sstevel@tonic-gate /*
1129*7c478bd9Sstevel@tonic-gate  * Determine whether arena vmp contains the segment [vaddr, vaddr + size).
1130*7c478bd9Sstevel@tonic-gate  */
1131*7c478bd9Sstevel@tonic-gate int
1132*7c478bd9Sstevel@tonic-gate vmem_contains(vmem_t *vmp, void *vaddr, size_t size)
1133*7c478bd9Sstevel@tonic-gate {
1134*7c478bd9Sstevel@tonic-gate 	uintptr_t start = (uintptr_t)vaddr;
1135*7c478bd9Sstevel@tonic-gate 	uintptr_t end = start + size;
1136*7c478bd9Sstevel@tonic-gate 	vmem_seg_t *vsp;
1137*7c478bd9Sstevel@tonic-gate 	vmem_seg_t *seg0 = &vmp->vm_seg0;
1138*7c478bd9Sstevel@tonic-gate 
1139*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&vmp->vm_lock);
1140*7c478bd9Sstevel@tonic-gate 	vmp->vm_kstat.vk_contains++;
1141*7c478bd9Sstevel@tonic-gate 	for (vsp = seg0->vs_knext; vsp != seg0; vsp = vsp->vs_knext) {
1142*7c478bd9Sstevel@tonic-gate 		vmp->vm_kstat.vk_contains_search++;
1143*7c478bd9Sstevel@tonic-gate 		ASSERT(vsp->vs_type == VMEM_SPAN);
1144*7c478bd9Sstevel@tonic-gate 		if (start >= vsp->vs_start && end - 1 <= vsp->vs_end - 1)
1145*7c478bd9Sstevel@tonic-gate 			break;
1146*7c478bd9Sstevel@tonic-gate 	}
1147*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&vmp->vm_lock);
1148*7c478bd9Sstevel@tonic-gate 	return (vsp != seg0);
1149*7c478bd9Sstevel@tonic-gate }
1150*7c478bd9Sstevel@tonic-gate 
1151*7c478bd9Sstevel@tonic-gate /*
1152*7c478bd9Sstevel@tonic-gate  * Add the span [vaddr, vaddr + size) to arena vmp.
1153*7c478bd9Sstevel@tonic-gate  */
1154*7c478bd9Sstevel@tonic-gate void *
1155*7c478bd9Sstevel@tonic-gate vmem_add(vmem_t *vmp, void *vaddr, size_t size, int vmflag)
1156*7c478bd9Sstevel@tonic-gate {
1157*7c478bd9Sstevel@tonic-gate 	if (vaddr == NULL || size == 0) {
1158*7c478bd9Sstevel@tonic-gate 		umem_panic("vmem_add(%p, %p, %lu): bad arguments",
1159*7c478bd9Sstevel@tonic-gate 		    vmp, vaddr, size);
1160*7c478bd9Sstevel@tonic-gate 	}
1161*7c478bd9Sstevel@tonic-gate 
1162*7c478bd9Sstevel@tonic-gate 	ASSERT(!vmem_contains(vmp, vaddr, size));
1163*7c478bd9Sstevel@tonic-gate 
1164*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&vmp->vm_lock);
1165*7c478bd9Sstevel@tonic-gate 	if (vmem_populate(vmp, vmflag))
1166*7c478bd9Sstevel@tonic-gate 		(void) vmem_span_create(vmp, vaddr, size, 0);
1167*7c478bd9Sstevel@tonic-gate 	else
1168*7c478bd9Sstevel@tonic-gate 		vaddr = NULL;
1169*7c478bd9Sstevel@tonic-gate 	(void) cond_broadcast(&vmp->vm_cv);
1170*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&vmp->vm_lock);
1171*7c478bd9Sstevel@tonic-gate 	return (vaddr);
1172*7c478bd9Sstevel@tonic-gate }
1173*7c478bd9Sstevel@tonic-gate 
1174*7c478bd9Sstevel@tonic-gate /*
1175*7c478bd9Sstevel@tonic-gate  * Adds the address range [addr, endaddr) to arena vmp, by either:
1176*7c478bd9Sstevel@tonic-gate  *   1. joining two existing spans, [x, addr), and [endaddr, y) (which
1177*7c478bd9Sstevel@tonic-gate  *      are in that order) into a single [x, y) span,
1178*7c478bd9Sstevel@tonic-gate  *   2. expanding an existing [x, addr) span to [x, endaddr),
1179*7c478bd9Sstevel@tonic-gate  *   3. expanding an existing [endaddr, x) span to [addr, x), or
1180*7c478bd9Sstevel@tonic-gate  *   4. creating a new [addr, endaddr) span.
1181*7c478bd9Sstevel@tonic-gate  *
1182*7c478bd9Sstevel@tonic-gate  * Called with vmp->vm_lock held, and a successful vmem_populate() completed.
1183*7c478bd9Sstevel@tonic-gate  * Cannot fail.  Returns the new segment.
1184*7c478bd9Sstevel@tonic-gate  *
1185*7c478bd9Sstevel@tonic-gate  * NOTE:  this algorithm is linear-time in the number of spans, but is
1186*7c478bd9Sstevel@tonic-gate  *      constant-time when you are extending the last (highest-addressed)
1187*7c478bd9Sstevel@tonic-gate  *      span.
1188*7c478bd9Sstevel@tonic-gate  */
1189*7c478bd9Sstevel@tonic-gate static vmem_seg_t *
1190*7c478bd9Sstevel@tonic-gate vmem_extend_unlocked(vmem_t *vmp, uintptr_t addr, uintptr_t endaddr)
1191*7c478bd9Sstevel@tonic-gate {
1192*7c478bd9Sstevel@tonic-gate 	vmem_seg_t *span;
1193*7c478bd9Sstevel@tonic-gate 	vmem_seg_t *vsp;
1194*7c478bd9Sstevel@tonic-gate 
1195*7c478bd9Sstevel@tonic-gate 	vmem_seg_t *end = &vmp->vm_seg0;
1196*7c478bd9Sstevel@tonic-gate 
1197*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&vmp->vm_lock));
1198*7c478bd9Sstevel@tonic-gate 
1199*7c478bd9Sstevel@tonic-gate 	/*
1200*7c478bd9Sstevel@tonic-gate 	 * the second "if" clause below relies on the direction of this search
1201*7c478bd9Sstevel@tonic-gate 	 */
1202*7c478bd9Sstevel@tonic-gate 	for (span = end->vs_kprev; span != end; span = span->vs_kprev) {
1203*7c478bd9Sstevel@tonic-gate 		if (span->vs_end == addr || span->vs_start == endaddr)
1204*7c478bd9Sstevel@tonic-gate 			break;
1205*7c478bd9Sstevel@tonic-gate 	}
1206*7c478bd9Sstevel@tonic-gate 
1207*7c478bd9Sstevel@tonic-gate 	if (span == end)
1208*7c478bd9Sstevel@tonic-gate 		return (vmem_span_create(vmp, (void *)addr, endaddr - addr, 0));
1209*7c478bd9Sstevel@tonic-gate 	if (span->vs_kprev->vs_end == addr && span->vs_start == endaddr) {
1210*7c478bd9Sstevel@tonic-gate 		vmem_seg_t *prevspan = span->vs_kprev;
1211*7c478bd9Sstevel@tonic-gate 		vmem_seg_t *nextseg = span->vs_anext;
1212*7c478bd9Sstevel@tonic-gate 		vmem_seg_t *prevseg = span->vs_aprev;
1213*7c478bd9Sstevel@tonic-gate 
1214*7c478bd9Sstevel@tonic-gate 		/*
1215*7c478bd9Sstevel@tonic-gate 		 * prevspan becomes the span marker for the full range
1216*7c478bd9Sstevel@tonic-gate 		 */
1217*7c478bd9Sstevel@tonic-gate 		prevspan->vs_end = span->vs_end;
1218*7c478bd9Sstevel@tonic-gate 
1219*7c478bd9Sstevel@tonic-gate 		/*
1220*7c478bd9Sstevel@tonic-gate 		 * Notionally, span becomes a free segment representing
1221*7c478bd9Sstevel@tonic-gate 		 * [addr, endaddr).
1222*7c478bd9Sstevel@tonic-gate 		 *
1223*7c478bd9Sstevel@tonic-gate 		 * However, if either of its neighbors are free, we coalesce
1224*7c478bd9Sstevel@tonic-gate 		 * by destroying span and changing the free segment.
1225*7c478bd9Sstevel@tonic-gate 		 */
1226*7c478bd9Sstevel@tonic-gate 		if (prevseg->vs_type == VMEM_FREE &&
1227*7c478bd9Sstevel@tonic-gate 		    nextseg->vs_type == VMEM_FREE) {
1228*7c478bd9Sstevel@tonic-gate 			/*
1229*7c478bd9Sstevel@tonic-gate 			 * coalesce both ways
1230*7c478bd9Sstevel@tonic-gate 			 */
1231*7c478bd9Sstevel@tonic-gate 			ASSERT(prevseg->vs_end == addr &&
1232*7c478bd9Sstevel@tonic-gate 			    nextseg->vs_start == endaddr);
1233*7c478bd9Sstevel@tonic-gate 
1234*7c478bd9Sstevel@tonic-gate 			vmem_freelist_delete(vmp, prevseg);
1235*7c478bd9Sstevel@tonic-gate 			prevseg->vs_end = nextseg->vs_end;
1236*7c478bd9Sstevel@tonic-gate 
1237*7c478bd9Sstevel@tonic-gate 			vmem_freelist_delete(vmp, nextseg);
1238*7c478bd9Sstevel@tonic-gate 			VMEM_DELETE(span, k);
1239*7c478bd9Sstevel@tonic-gate 			vmem_seg_destroy(vmp, nextseg);
1240*7c478bd9Sstevel@tonic-gate 			vmem_seg_destroy(vmp, span);
1241*7c478bd9Sstevel@tonic-gate 
1242*7c478bd9Sstevel@tonic-gate 			vsp = prevseg;
1243*7c478bd9Sstevel@tonic-gate 		} else if (prevseg->vs_type == VMEM_FREE) {
1244*7c478bd9Sstevel@tonic-gate 			/*
1245*7c478bd9Sstevel@tonic-gate 			 * coalesce left
1246*7c478bd9Sstevel@tonic-gate 			 */
1247*7c478bd9Sstevel@tonic-gate 			ASSERT(prevseg->vs_end == addr);
1248*7c478bd9Sstevel@tonic-gate 
1249*7c478bd9Sstevel@tonic-gate 			VMEM_DELETE(span, k);
1250*7c478bd9Sstevel@tonic-gate 			vmem_seg_destroy(vmp, span);
1251*7c478bd9Sstevel@tonic-gate 
1252*7c478bd9Sstevel@tonic-gate 			vmem_freelist_delete(vmp, prevseg);
1253*7c478bd9Sstevel@tonic-gate 			prevseg->vs_end = endaddr;
1254*7c478bd9Sstevel@tonic-gate 
1255*7c478bd9Sstevel@tonic-gate 			vsp = prevseg;
1256*7c478bd9Sstevel@tonic-gate 		} else if (nextseg->vs_type == VMEM_FREE) {
1257*7c478bd9Sstevel@tonic-gate 			/*
1258*7c478bd9Sstevel@tonic-gate 			 * coalesce right
1259*7c478bd9Sstevel@tonic-gate 			 */
1260*7c478bd9Sstevel@tonic-gate 			ASSERT(nextseg->vs_start == endaddr);
1261*7c478bd9Sstevel@tonic-gate 
1262*7c478bd9Sstevel@tonic-gate 			VMEM_DELETE(span, k);
1263*7c478bd9Sstevel@tonic-gate 			vmem_seg_destroy(vmp, span);
1264*7c478bd9Sstevel@tonic-gate 
1265*7c478bd9Sstevel@tonic-gate 			vmem_freelist_delete(vmp, nextseg);
1266*7c478bd9Sstevel@tonic-gate 			nextseg->vs_start = addr;
1267*7c478bd9Sstevel@tonic-gate 
1268*7c478bd9Sstevel@tonic-gate 			vsp = nextseg;
1269*7c478bd9Sstevel@tonic-gate 		} else {
1270*7c478bd9Sstevel@tonic-gate 			/*
1271*7c478bd9Sstevel@tonic-gate 			 * cannnot coalesce
1272*7c478bd9Sstevel@tonic-gate 			 */
1273*7c478bd9Sstevel@tonic-gate 			VMEM_DELETE(span, k);
1274*7c478bd9Sstevel@tonic-gate 			span->vs_start = addr;
1275*7c478bd9Sstevel@tonic-gate 			span->vs_end = endaddr;
1276*7c478bd9Sstevel@tonic-gate 
1277*7c478bd9Sstevel@tonic-gate 			vsp = span;
1278*7c478bd9Sstevel@tonic-gate 		}
1279*7c478bd9Sstevel@tonic-gate 	} else if (span->vs_end == addr) {
1280*7c478bd9Sstevel@tonic-gate 		vmem_seg_t *oldseg = span->vs_knext->vs_aprev;
1281*7c478bd9Sstevel@tonic-gate 		span->vs_end = endaddr;
1282*7c478bd9Sstevel@tonic-gate 
1283*7c478bd9Sstevel@tonic-gate 		ASSERT(oldseg->vs_type != VMEM_SPAN);
1284*7c478bd9Sstevel@tonic-gate 		if (oldseg->vs_type == VMEM_FREE) {
1285*7c478bd9Sstevel@tonic-gate 			ASSERT(oldseg->vs_end == addr);
1286*7c478bd9Sstevel@tonic-gate 			vmem_freelist_delete(vmp, oldseg);
1287*7c478bd9Sstevel@tonic-gate 			oldseg->vs_end = endaddr;
1288*7c478bd9Sstevel@tonic-gate 			vsp = oldseg;
1289*7c478bd9Sstevel@tonic-gate 		} else
1290*7c478bd9Sstevel@tonic-gate 			vsp = vmem_seg_create(vmp, oldseg, addr, endaddr);
1291*7c478bd9Sstevel@tonic-gate 	} else {
1292*7c478bd9Sstevel@tonic-gate 		vmem_seg_t *oldseg = span->vs_anext;
1293*7c478bd9Sstevel@tonic-gate 		ASSERT(span->vs_start == endaddr);
1294*7c478bd9Sstevel@tonic-gate 		span->vs_start = addr;
1295*7c478bd9Sstevel@tonic-gate 
1296*7c478bd9Sstevel@tonic-gate 		ASSERT(oldseg->vs_type != VMEM_SPAN);
1297*7c478bd9Sstevel@tonic-gate 		if (oldseg->vs_type == VMEM_FREE) {
1298*7c478bd9Sstevel@tonic-gate 			ASSERT(oldseg->vs_start == endaddr);
1299*7c478bd9Sstevel@tonic-gate 			vmem_freelist_delete(vmp, oldseg);
1300*7c478bd9Sstevel@tonic-gate 			oldseg->vs_start = addr;
1301*7c478bd9Sstevel@tonic-gate 			vsp = oldseg;
1302*7c478bd9Sstevel@tonic-gate 		} else
1303*7c478bd9Sstevel@tonic-gate 			vsp = vmem_seg_create(vmp, span, addr, endaddr);
1304*7c478bd9Sstevel@tonic-gate 	}
1305*7c478bd9Sstevel@tonic-gate 	vmem_freelist_insert(vmp, vsp);
1306*7c478bd9Sstevel@tonic-gate 	vmp->vm_kstat.vk_mem_total += (endaddr - addr);
1307*7c478bd9Sstevel@tonic-gate 	return (vsp);
1308*7c478bd9Sstevel@tonic-gate }
1309*7c478bd9Sstevel@tonic-gate 
1310*7c478bd9Sstevel@tonic-gate /*
1311*7c478bd9Sstevel@tonic-gate  * Does some error checking, calls vmem_extend_unlocked to add
1312*7c478bd9Sstevel@tonic-gate  * [vaddr, vaddr+size) to vmp, then allocates alloc bytes from the
1313*7c478bd9Sstevel@tonic-gate  * newly merged segment.
1314*7c478bd9Sstevel@tonic-gate  */
1315*7c478bd9Sstevel@tonic-gate void *
1316*7c478bd9Sstevel@tonic-gate _vmem_extend_alloc(vmem_t *vmp, void *vaddr, size_t size, size_t alloc,
1317*7c478bd9Sstevel@tonic-gate     int vmflag)
1318*7c478bd9Sstevel@tonic-gate {
1319*7c478bd9Sstevel@tonic-gate 	uintptr_t addr = (uintptr_t)vaddr;
1320*7c478bd9Sstevel@tonic-gate 	uintptr_t endaddr = addr + size;
1321*7c478bd9Sstevel@tonic-gate 	vmem_seg_t *vsp;
1322*7c478bd9Sstevel@tonic-gate 
1323*7c478bd9Sstevel@tonic-gate 	ASSERT(vaddr != NULL && size != 0 && endaddr > addr);
1324*7c478bd9Sstevel@tonic-gate 	ASSERT(alloc <= size && alloc != 0);
1325*7c478bd9Sstevel@tonic-gate 	ASSERT(((addr | size | alloc) & (vmp->vm_quantum - 1)) == 0);
1326*7c478bd9Sstevel@tonic-gate 
1327*7c478bd9Sstevel@tonic-gate 	ASSERT(!vmem_contains(vmp, vaddr, size));
1328*7c478bd9Sstevel@tonic-gate 
1329*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&vmp->vm_lock);
1330*7c478bd9Sstevel@tonic-gate 	if (!vmem_populate(vmp, vmflag)) {
1331*7c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&vmp->vm_lock);
1332*7c478bd9Sstevel@tonic-gate 		return (NULL);
1333*7c478bd9Sstevel@tonic-gate 	}
1334*7c478bd9Sstevel@tonic-gate 	/*
1335*7c478bd9Sstevel@tonic-gate 	 * if there is a source, we can't mess with the spans
1336*7c478bd9Sstevel@tonic-gate 	 */
1337*7c478bd9Sstevel@tonic-gate 	if (vmp->vm_source_alloc != NULL)
1338*7c478bd9Sstevel@tonic-gate 		vsp = vmem_span_create(vmp, vaddr, size, 0);
1339*7c478bd9Sstevel@tonic-gate 	else
1340*7c478bd9Sstevel@tonic-gate 		vsp = vmem_extend_unlocked(vmp, addr, endaddr);
1341*7c478bd9Sstevel@tonic-gate 
1342*7c478bd9Sstevel@tonic-gate 	ASSERT(VS_SIZE(vsp) >= alloc);
1343*7c478bd9Sstevel@tonic-gate 
1344*7c478bd9Sstevel@tonic-gate 	addr = vsp->vs_start;
1345*7c478bd9Sstevel@tonic-gate 	(void) vmem_seg_alloc(vmp, vsp, addr, alloc);
1346*7c478bd9Sstevel@tonic-gate 	vaddr = (void *)addr;
1347*7c478bd9Sstevel@tonic-gate 
1348*7c478bd9Sstevel@tonic-gate 	(void) cond_broadcast(&vmp->vm_cv);
1349*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&vmp->vm_lock);
1350*7c478bd9Sstevel@tonic-gate 
1351*7c478bd9Sstevel@tonic-gate 	return (vaddr);
1352*7c478bd9Sstevel@tonic-gate }
1353*7c478bd9Sstevel@tonic-gate 
1354*7c478bd9Sstevel@tonic-gate /*
1355*7c478bd9Sstevel@tonic-gate  * Walk the vmp arena, applying func to each segment matching typemask.
1356*7c478bd9Sstevel@tonic-gate  * If VMEM_REENTRANT is specified, the arena lock is dropped across each
1357*7c478bd9Sstevel@tonic-gate  * call to func(); otherwise, it is held for the duration of vmem_walk()
1358*7c478bd9Sstevel@tonic-gate  * to ensure a consistent snapshot.  Note that VMEM_REENTRANT callbacks
1359*7c478bd9Sstevel@tonic-gate  * are *not* necessarily consistent, so they may only be used when a hint
1360*7c478bd9Sstevel@tonic-gate  * is adequate.
1361*7c478bd9Sstevel@tonic-gate  */
1362*7c478bd9Sstevel@tonic-gate void
1363*7c478bd9Sstevel@tonic-gate vmem_walk(vmem_t *vmp, int typemask,
1364*7c478bd9Sstevel@tonic-gate 	void (*func)(void *, void *, size_t), void *arg)
1365*7c478bd9Sstevel@tonic-gate {
1366*7c478bd9Sstevel@tonic-gate 	vmem_seg_t *vsp;
1367*7c478bd9Sstevel@tonic-gate 	vmem_seg_t *seg0 = &vmp->vm_seg0;
1368*7c478bd9Sstevel@tonic-gate 	vmem_seg_t walker;
1369*7c478bd9Sstevel@tonic-gate 
1370*7c478bd9Sstevel@tonic-gate 	if (typemask & VMEM_WALKER)
1371*7c478bd9Sstevel@tonic-gate 		return;
1372*7c478bd9Sstevel@tonic-gate 
1373*7c478bd9Sstevel@tonic-gate 	bzero(&walker, sizeof (walker));
1374*7c478bd9Sstevel@tonic-gate 	walker.vs_type = VMEM_WALKER;
1375*7c478bd9Sstevel@tonic-gate 
1376*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&vmp->vm_lock);
1377*7c478bd9Sstevel@tonic-gate 	VMEM_INSERT(seg0, &walker, a);
1378*7c478bd9Sstevel@tonic-gate 	for (vsp = seg0->vs_anext; vsp != seg0; vsp = vsp->vs_anext) {
1379*7c478bd9Sstevel@tonic-gate 		if (vsp->vs_type & typemask) {
1380*7c478bd9Sstevel@tonic-gate 			void *start = (void *)vsp->vs_start;
1381*7c478bd9Sstevel@tonic-gate 			size_t size = VS_SIZE(vsp);
1382*7c478bd9Sstevel@tonic-gate 			if (typemask & VMEM_REENTRANT) {
1383*7c478bd9Sstevel@tonic-gate 				vmem_advance(vmp, &walker, vsp);
1384*7c478bd9Sstevel@tonic-gate 				(void) mutex_unlock(&vmp->vm_lock);
1385*7c478bd9Sstevel@tonic-gate 				func(arg, start, size);
1386*7c478bd9Sstevel@tonic-gate 				(void) mutex_lock(&vmp->vm_lock);
1387*7c478bd9Sstevel@tonic-gate 				vsp = &walker;
1388*7c478bd9Sstevel@tonic-gate 			} else {
1389*7c478bd9Sstevel@tonic-gate 				func(arg, start, size);
1390*7c478bd9Sstevel@tonic-gate 			}
1391*7c478bd9Sstevel@tonic-gate 		}
1392*7c478bd9Sstevel@tonic-gate 	}
1393*7c478bd9Sstevel@tonic-gate 	vmem_advance(vmp, &walker, NULL);
1394*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&vmp->vm_lock);
1395*7c478bd9Sstevel@tonic-gate }
1396*7c478bd9Sstevel@tonic-gate 
1397*7c478bd9Sstevel@tonic-gate /*
1398*7c478bd9Sstevel@tonic-gate  * Return the total amount of memory whose type matches typemask.  Thus:
1399*7c478bd9Sstevel@tonic-gate  *
1400*7c478bd9Sstevel@tonic-gate  *	typemask VMEM_ALLOC yields total memory allocated (in use).
1401*7c478bd9Sstevel@tonic-gate  *	typemask VMEM_FREE yields total memory free (available).
1402*7c478bd9Sstevel@tonic-gate  *	typemask (VMEM_ALLOC | VMEM_FREE) yields total arena size.
1403*7c478bd9Sstevel@tonic-gate  */
1404*7c478bd9Sstevel@tonic-gate size_t
1405*7c478bd9Sstevel@tonic-gate vmem_size(vmem_t *vmp, int typemask)
1406*7c478bd9Sstevel@tonic-gate {
1407*7c478bd9Sstevel@tonic-gate 	uint64_t size = 0;
1408*7c478bd9Sstevel@tonic-gate 
1409*7c478bd9Sstevel@tonic-gate 	if (typemask & VMEM_ALLOC)
1410*7c478bd9Sstevel@tonic-gate 		size += vmp->vm_kstat.vk_mem_inuse;
1411*7c478bd9Sstevel@tonic-gate 	if (typemask & VMEM_FREE)
1412*7c478bd9Sstevel@tonic-gate 		size += vmp->vm_kstat.vk_mem_total -
1413*7c478bd9Sstevel@tonic-gate 		    vmp->vm_kstat.vk_mem_inuse;
1414*7c478bd9Sstevel@tonic-gate 	return ((size_t)size);
1415*7c478bd9Sstevel@tonic-gate }
1416*7c478bd9Sstevel@tonic-gate 
1417*7c478bd9Sstevel@tonic-gate /*
1418*7c478bd9Sstevel@tonic-gate  * Create an arena called name whose initial span is [base, base + size).
1419*7c478bd9Sstevel@tonic-gate  * The arena's natural unit of currency is quantum, so vmem_alloc()
1420*7c478bd9Sstevel@tonic-gate  * guarantees quantum-aligned results.  The arena may import new spans
1421*7c478bd9Sstevel@tonic-gate  * by invoking afunc() on source, and may return those spans by invoking
1422*7c478bd9Sstevel@tonic-gate  * ffunc() on source.  To make small allocations fast and scalable,
1423*7c478bd9Sstevel@tonic-gate  * the arena offers high-performance caching for each integer multiple
1424*7c478bd9Sstevel@tonic-gate  * of quantum up to qcache_max.
1425*7c478bd9Sstevel@tonic-gate  */
1426*7c478bd9Sstevel@tonic-gate vmem_t *
1427*7c478bd9Sstevel@tonic-gate vmem_create(const char *name, void *base, size_t size, size_t quantum,
1428*7c478bd9Sstevel@tonic-gate 	vmem_alloc_t *afunc, vmem_free_t *ffunc, vmem_t *source,
1429*7c478bd9Sstevel@tonic-gate 	size_t qcache_max, int vmflag)
1430*7c478bd9Sstevel@tonic-gate {
1431*7c478bd9Sstevel@tonic-gate 	int i;
1432*7c478bd9Sstevel@tonic-gate 	size_t nqcache;
1433*7c478bd9Sstevel@tonic-gate 	vmem_t *vmp, *cur, **vmpp;
1434*7c478bd9Sstevel@tonic-gate 	vmem_seg_t *vsp;
1435*7c478bd9Sstevel@tonic-gate 	vmem_freelist_t *vfp;
1436*7c478bd9Sstevel@tonic-gate 	uint32_t id = atomic_add_32_nv(&vmem_id, 1);
1437*7c478bd9Sstevel@tonic-gate 
1438*7c478bd9Sstevel@tonic-gate 	if (vmem_vmem_arena != NULL) {
1439*7c478bd9Sstevel@tonic-gate 		vmp = vmem_alloc(vmem_vmem_arena, sizeof (vmem_t),
1440*7c478bd9Sstevel@tonic-gate 		    vmflag & VM_UMFLAGS);
1441*7c478bd9Sstevel@tonic-gate 	} else {
1442*7c478bd9Sstevel@tonic-gate 		ASSERT(id <= VMEM_INITIAL);
1443*7c478bd9Sstevel@tonic-gate 		vmp = &vmem0[id - 1];
1444*7c478bd9Sstevel@tonic-gate 	}
1445*7c478bd9Sstevel@tonic-gate 
1446*7c478bd9Sstevel@tonic-gate 	if (vmp == NULL)
1447*7c478bd9Sstevel@tonic-gate 		return (NULL);
1448*7c478bd9Sstevel@tonic-gate 	bzero(vmp, sizeof (vmem_t));
1449*7c478bd9Sstevel@tonic-gate 
1450*7c478bd9Sstevel@tonic-gate 	(void) snprintf(vmp->vm_name, VMEM_NAMELEN, "%s", name);
1451*7c478bd9Sstevel@tonic-gate 	(void) mutex_init(&vmp->vm_lock, USYNC_THREAD, NULL);
1452*7c478bd9Sstevel@tonic-gate 	(void) cond_init(&vmp->vm_cv, USYNC_THREAD, NULL);
1453*7c478bd9Sstevel@tonic-gate 	vmp->vm_cflags = vmflag;
1454*7c478bd9Sstevel@tonic-gate 	vmflag &= VM_UMFLAGS;
1455*7c478bd9Sstevel@tonic-gate 
1456*7c478bd9Sstevel@tonic-gate 	vmp->vm_quantum = quantum;
1457*7c478bd9Sstevel@tonic-gate 	vmp->vm_qshift = highbit(quantum) - 1;
1458*7c478bd9Sstevel@tonic-gate 	nqcache = MIN(qcache_max >> vmp->vm_qshift, VMEM_NQCACHE_MAX);
1459*7c478bd9Sstevel@tonic-gate 
1460*7c478bd9Sstevel@tonic-gate 	for (i = 0; i <= VMEM_FREELISTS; i++) {
1461*7c478bd9Sstevel@tonic-gate 		vfp = &vmp->vm_freelist[i];
1462*7c478bd9Sstevel@tonic-gate 		vfp->vs_end = 1UL << i;
1463*7c478bd9Sstevel@tonic-gate 		vfp->vs_knext = (vmem_seg_t *)(vfp + 1);
1464*7c478bd9Sstevel@tonic-gate 		vfp->vs_kprev = (vmem_seg_t *)(vfp - 1);
1465*7c478bd9Sstevel@tonic-gate 	}
1466*7c478bd9Sstevel@tonic-gate 
1467*7c478bd9Sstevel@tonic-gate 	vmp->vm_freelist[0].vs_kprev = NULL;
1468*7c478bd9Sstevel@tonic-gate 	vmp->vm_freelist[VMEM_FREELISTS].vs_knext = NULL;
1469*7c478bd9Sstevel@tonic-gate 	vmp->vm_freelist[VMEM_FREELISTS].vs_end = 0;
1470*7c478bd9Sstevel@tonic-gate 	vmp->vm_hash_table = vmp->vm_hash0;
1471*7c478bd9Sstevel@tonic-gate 	vmp->vm_hash_mask = VMEM_HASH_INITIAL - 1;
1472*7c478bd9Sstevel@tonic-gate 	vmp->vm_hash_shift = highbit(vmp->vm_hash_mask);
1473*7c478bd9Sstevel@tonic-gate 
1474*7c478bd9Sstevel@tonic-gate 	vsp = &vmp->vm_seg0;
1475*7c478bd9Sstevel@tonic-gate 	vsp->vs_anext = vsp;
1476*7c478bd9Sstevel@tonic-gate 	vsp->vs_aprev = vsp;
1477*7c478bd9Sstevel@tonic-gate 	vsp->vs_knext = vsp;
1478*7c478bd9Sstevel@tonic-gate 	vsp->vs_kprev = vsp;
1479*7c478bd9Sstevel@tonic-gate 	vsp->vs_type = VMEM_SPAN;
1480*7c478bd9Sstevel@tonic-gate 
1481*7c478bd9Sstevel@tonic-gate 	vsp = &vmp->vm_rotor;
1482*7c478bd9Sstevel@tonic-gate 	vsp->vs_type = VMEM_ROTOR;
1483*7c478bd9Sstevel@tonic-gate 	VMEM_INSERT(&vmp->vm_seg0, vsp, a);
1484*7c478bd9Sstevel@tonic-gate 
1485*7c478bd9Sstevel@tonic-gate 	vmp->vm_id = id;
1486*7c478bd9Sstevel@tonic-gate 	if (source != NULL)
1487*7c478bd9Sstevel@tonic-gate 		vmp->vm_kstat.vk_source_id = source->vm_id;
1488*7c478bd9Sstevel@tonic-gate 	vmp->vm_source = source;
1489*7c478bd9Sstevel@tonic-gate 	vmp->vm_source_alloc = afunc;
1490*7c478bd9Sstevel@tonic-gate 	vmp->vm_source_free = ffunc;
1491*7c478bd9Sstevel@tonic-gate 
1492*7c478bd9Sstevel@tonic-gate 	if (nqcache != 0) {
1493*7c478bd9Sstevel@tonic-gate 		vmp->vm_qcache_max = nqcache << vmp->vm_qshift;
1494*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < nqcache; i++) {
1495*7c478bd9Sstevel@tonic-gate 			char buf[VMEM_NAMELEN + 21];
1496*7c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "%s_%lu",
1497*7c478bd9Sstevel@tonic-gate 			    vmp->vm_name, (long)((i + 1) * quantum));
1498*7c478bd9Sstevel@tonic-gate 			vmp->vm_qcache[i] = umem_cache_create(buf,
1499*7c478bd9Sstevel@tonic-gate 			    (i + 1) * quantum, quantum, NULL, NULL, NULL,
1500*7c478bd9Sstevel@tonic-gate 			    NULL, vmp, UMC_QCACHE | UMC_NOTOUCH);
1501*7c478bd9Sstevel@tonic-gate 			if (vmp->vm_qcache[i] == NULL) {
1502*7c478bd9Sstevel@tonic-gate 				vmp->vm_qcache_max = i * quantum;
1503*7c478bd9Sstevel@tonic-gate 				break;
1504*7c478bd9Sstevel@tonic-gate 			}
1505*7c478bd9Sstevel@tonic-gate 		}
1506*7c478bd9Sstevel@tonic-gate 	}
1507*7c478bd9Sstevel@tonic-gate 
1508*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&vmem_list_lock);
1509*7c478bd9Sstevel@tonic-gate 	vmpp = &vmem_list;
1510*7c478bd9Sstevel@tonic-gate 	while ((cur = *vmpp) != NULL)
1511*7c478bd9Sstevel@tonic-gate 		vmpp = &cur->vm_next;
1512*7c478bd9Sstevel@tonic-gate 	*vmpp = vmp;
1513*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&vmem_list_lock);
1514*7c478bd9Sstevel@tonic-gate 
1515*7c478bd9Sstevel@tonic-gate 	if (vmp->vm_cflags & VMC_POPULATOR) {
1516*7c478bd9Sstevel@tonic-gate 		uint_t pop_id = atomic_add_32_nv(&vmem_populators, 1);
1517*7c478bd9Sstevel@tonic-gate 		ASSERT(pop_id <= VMEM_INITIAL);
1518*7c478bd9Sstevel@tonic-gate 		vmem_populator[pop_id - 1] = vmp;
1519*7c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&vmp->vm_lock);
1520*7c478bd9Sstevel@tonic-gate 		(void) vmem_populate(vmp, vmflag | VM_PANIC);
1521*7c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&vmp->vm_lock);
1522*7c478bd9Sstevel@tonic-gate 	}
1523*7c478bd9Sstevel@tonic-gate 
1524*7c478bd9Sstevel@tonic-gate 	if ((base || size) && vmem_add(vmp, base, size, vmflag) == NULL) {
1525*7c478bd9Sstevel@tonic-gate 		vmem_destroy(vmp);
1526*7c478bd9Sstevel@tonic-gate 		return (NULL);
1527*7c478bd9Sstevel@tonic-gate 	}
1528*7c478bd9Sstevel@tonic-gate 
1529*7c478bd9Sstevel@tonic-gate 	return (vmp);
1530*7c478bd9Sstevel@tonic-gate }
1531*7c478bd9Sstevel@tonic-gate 
1532*7c478bd9Sstevel@tonic-gate /*
1533*7c478bd9Sstevel@tonic-gate  * Destroy arena vmp.
1534*7c478bd9Sstevel@tonic-gate  */
1535*7c478bd9Sstevel@tonic-gate void
1536*7c478bd9Sstevel@tonic-gate vmem_destroy(vmem_t *vmp)
1537*7c478bd9Sstevel@tonic-gate {
1538*7c478bd9Sstevel@tonic-gate 	vmem_t *cur, **vmpp;
1539*7c478bd9Sstevel@tonic-gate 	vmem_seg_t *seg0 = &vmp->vm_seg0;
1540*7c478bd9Sstevel@tonic-gate 	vmem_seg_t *vsp;
1541*7c478bd9Sstevel@tonic-gate 	size_t leaked;
1542*7c478bd9Sstevel@tonic-gate 	int i;
1543*7c478bd9Sstevel@tonic-gate 
1544*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&vmem_list_lock);
1545*7c478bd9Sstevel@tonic-gate 	vmpp = &vmem_list;
1546*7c478bd9Sstevel@tonic-gate 	while ((cur = *vmpp) != vmp)
1547*7c478bd9Sstevel@tonic-gate 		vmpp = &cur->vm_next;
1548*7c478bd9Sstevel@tonic-gate 	*vmpp = vmp->vm_next;
1549*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&vmem_list_lock);
1550*7c478bd9Sstevel@tonic-gate 
1551*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < VMEM_NQCACHE_MAX; i++)
1552*7c478bd9Sstevel@tonic-gate 		if (vmp->vm_qcache[i])
1553*7c478bd9Sstevel@tonic-gate 			umem_cache_destroy(vmp->vm_qcache[i]);
1554*7c478bd9Sstevel@tonic-gate 
1555*7c478bd9Sstevel@tonic-gate 	leaked = vmem_size(vmp, VMEM_ALLOC);
1556*7c478bd9Sstevel@tonic-gate 	if (leaked != 0)
1557*7c478bd9Sstevel@tonic-gate 		umem_printf("vmem_destroy('%s'): leaked %lu bytes",
1558*7c478bd9Sstevel@tonic-gate 		    vmp->vm_name, leaked);
1559*7c478bd9Sstevel@tonic-gate 
1560*7c478bd9Sstevel@tonic-gate 	if (vmp->vm_hash_table != vmp->vm_hash0)
1561*7c478bd9Sstevel@tonic-gate 		vmem_free(vmem_hash_arena, vmp->vm_hash_table,
1562*7c478bd9Sstevel@tonic-gate 		    (vmp->vm_hash_mask + 1) * sizeof (void *));
1563*7c478bd9Sstevel@tonic-gate 
1564*7c478bd9Sstevel@tonic-gate 	/*
1565*7c478bd9Sstevel@tonic-gate 	 * Give back the segment structures for anything that's left in the
1566*7c478bd9Sstevel@tonic-gate 	 * arena, e.g. the primary spans and their free segments.
1567*7c478bd9Sstevel@tonic-gate 	 */
1568*7c478bd9Sstevel@tonic-gate 	VMEM_DELETE(&vmp->vm_rotor, a);
1569*7c478bd9Sstevel@tonic-gate 	for (vsp = seg0->vs_anext; vsp != seg0; vsp = vsp->vs_anext)
1570*7c478bd9Sstevel@tonic-gate 		vmem_putseg_global(vsp);
1571*7c478bd9Sstevel@tonic-gate 
1572*7c478bd9Sstevel@tonic-gate 	while (vmp->vm_nsegfree > 0)
1573*7c478bd9Sstevel@tonic-gate 		vmem_putseg_global(vmem_getseg(vmp));
1574*7c478bd9Sstevel@tonic-gate 
1575*7c478bd9Sstevel@tonic-gate 	(void) mutex_destroy(&vmp->vm_lock);
1576*7c478bd9Sstevel@tonic-gate 	(void) cond_destroy(&vmp->vm_cv);
1577*7c478bd9Sstevel@tonic-gate 	vmem_free(vmem_vmem_arena, vmp, sizeof (vmem_t));
1578*7c478bd9Sstevel@tonic-gate }
1579*7c478bd9Sstevel@tonic-gate 
1580*7c478bd9Sstevel@tonic-gate /*
1581*7c478bd9Sstevel@tonic-gate  * Resize vmp's hash table to keep the average lookup depth near 1.0.
1582*7c478bd9Sstevel@tonic-gate  */
1583*7c478bd9Sstevel@tonic-gate static void
1584*7c478bd9Sstevel@tonic-gate vmem_hash_rescale(vmem_t *vmp)
1585*7c478bd9Sstevel@tonic-gate {
1586*7c478bd9Sstevel@tonic-gate 	vmem_seg_t **old_table, **new_table, *vsp;
1587*7c478bd9Sstevel@tonic-gate 	size_t old_size, new_size, h, nseg;
1588*7c478bd9Sstevel@tonic-gate 
1589*7c478bd9Sstevel@tonic-gate 	nseg = (size_t)(vmp->vm_kstat.vk_alloc - vmp->vm_kstat.vk_free);
1590*7c478bd9Sstevel@tonic-gate 
1591*7c478bd9Sstevel@tonic-gate 	new_size = MAX(VMEM_HASH_INITIAL, 1 << (highbit(3 * nseg + 4) - 2));
1592*7c478bd9Sstevel@tonic-gate 	old_size = vmp->vm_hash_mask + 1;
1593*7c478bd9Sstevel@tonic-gate 
1594*7c478bd9Sstevel@tonic-gate 	if ((old_size >> 1) <= new_size && new_size <= (old_size << 1))
1595*7c478bd9Sstevel@tonic-gate 		return;
1596*7c478bd9Sstevel@tonic-gate 
1597*7c478bd9Sstevel@tonic-gate 	new_table = vmem_alloc(vmem_hash_arena, new_size * sizeof (void *),
1598*7c478bd9Sstevel@tonic-gate 	    VM_NOSLEEP);
1599*7c478bd9Sstevel@tonic-gate 	if (new_table == NULL)
1600*7c478bd9Sstevel@tonic-gate 		return;
1601*7c478bd9Sstevel@tonic-gate 	bzero(new_table, new_size * sizeof (void *));
1602*7c478bd9Sstevel@tonic-gate 
1603*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&vmp->vm_lock);
1604*7c478bd9Sstevel@tonic-gate 
1605*7c478bd9Sstevel@tonic-gate 	old_size = vmp->vm_hash_mask + 1;
1606*7c478bd9Sstevel@tonic-gate 	old_table = vmp->vm_hash_table;
1607*7c478bd9Sstevel@tonic-gate 
1608*7c478bd9Sstevel@tonic-gate 	vmp->vm_hash_mask = new_size - 1;
1609*7c478bd9Sstevel@tonic-gate 	vmp->vm_hash_table = new_table;
1610*7c478bd9Sstevel@tonic-gate 	vmp->vm_hash_shift = highbit(vmp->vm_hash_mask);
1611*7c478bd9Sstevel@tonic-gate 
1612*7c478bd9Sstevel@tonic-gate 	for (h = 0; h < old_size; h++) {
1613*7c478bd9Sstevel@tonic-gate 		vsp = old_table[h];
1614*7c478bd9Sstevel@tonic-gate 		while (vsp != NULL) {
1615*7c478bd9Sstevel@tonic-gate 			uintptr_t addr = vsp->vs_start;
1616*7c478bd9Sstevel@tonic-gate 			vmem_seg_t *next_vsp = vsp->vs_knext;
1617*7c478bd9Sstevel@tonic-gate 			vmem_seg_t **hash_bucket = VMEM_HASH(vmp, addr);
1618*7c478bd9Sstevel@tonic-gate 			vsp->vs_knext = *hash_bucket;
1619*7c478bd9Sstevel@tonic-gate 			*hash_bucket = vsp;
1620*7c478bd9Sstevel@tonic-gate 			vsp = next_vsp;
1621*7c478bd9Sstevel@tonic-gate 		}
1622*7c478bd9Sstevel@tonic-gate 	}
1623*7c478bd9Sstevel@tonic-gate 
1624*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&vmp->vm_lock);
1625*7c478bd9Sstevel@tonic-gate 
1626*7c478bd9Sstevel@tonic-gate 	if (old_table != vmp->vm_hash0)
1627*7c478bd9Sstevel@tonic-gate 		vmem_free(vmem_hash_arena, old_table,
1628*7c478bd9Sstevel@tonic-gate 		    old_size * sizeof (void *));
1629*7c478bd9Sstevel@tonic-gate }
1630*7c478bd9Sstevel@tonic-gate 
1631*7c478bd9Sstevel@tonic-gate /*
1632*7c478bd9Sstevel@tonic-gate  * Perform periodic maintenance on all vmem arenas.
1633*7c478bd9Sstevel@tonic-gate  */
1634*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1635*7c478bd9Sstevel@tonic-gate void
1636*7c478bd9Sstevel@tonic-gate vmem_update(void *dummy)
1637*7c478bd9Sstevel@tonic-gate {
1638*7c478bd9Sstevel@tonic-gate 	vmem_t *vmp;
1639*7c478bd9Sstevel@tonic-gate 
1640*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&vmem_list_lock);
1641*7c478bd9Sstevel@tonic-gate 	for (vmp = vmem_list; vmp != NULL; vmp = vmp->vm_next) {
1642*7c478bd9Sstevel@tonic-gate 		/*
1643*7c478bd9Sstevel@tonic-gate 		 * If threads are waiting for resources, wake them up
1644*7c478bd9Sstevel@tonic-gate 		 * periodically so they can issue another vmem_reap()
1645*7c478bd9Sstevel@tonic-gate 		 * to reclaim resources cached by the slab allocator.
1646*7c478bd9Sstevel@tonic-gate 		 */
1647*7c478bd9Sstevel@tonic-gate 		(void) cond_broadcast(&vmp->vm_cv);
1648*7c478bd9Sstevel@tonic-gate 
1649*7c478bd9Sstevel@tonic-gate 		/*
1650*7c478bd9Sstevel@tonic-gate 		 * Rescale the hash table to keep the hash chains short.
1651*7c478bd9Sstevel@tonic-gate 		 */
1652*7c478bd9Sstevel@tonic-gate 		vmem_hash_rescale(vmp);
1653*7c478bd9Sstevel@tonic-gate 	}
1654*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&vmem_list_lock);
1655*7c478bd9Sstevel@tonic-gate }
1656*7c478bd9Sstevel@tonic-gate 
1657*7c478bd9Sstevel@tonic-gate /*
1658*7c478bd9Sstevel@tonic-gate  * If vmem_init is called again, we need to be able to reset the world.
1659*7c478bd9Sstevel@tonic-gate  * That includes resetting the statics back to their original values.
1660*7c478bd9Sstevel@tonic-gate  */
1661*7c478bd9Sstevel@tonic-gate void
1662*7c478bd9Sstevel@tonic-gate vmem_startup(void)
1663*7c478bd9Sstevel@tonic-gate {
1664*7c478bd9Sstevel@tonic-gate #ifdef UMEM_STANDALONE
1665*7c478bd9Sstevel@tonic-gate 	vmem_id = 0;
1666*7c478bd9Sstevel@tonic-gate 	vmem_populators = 0;
1667*7c478bd9Sstevel@tonic-gate 	vmem_segfree = NULL;
1668*7c478bd9Sstevel@tonic-gate 	vmem_list = NULL;
1669*7c478bd9Sstevel@tonic-gate 	vmem_internal_arena = NULL;
1670*7c478bd9Sstevel@tonic-gate 	vmem_seg_arena = NULL;
1671*7c478bd9Sstevel@tonic-gate 	vmem_hash_arena = NULL;
1672*7c478bd9Sstevel@tonic-gate 	vmem_vmem_arena = NULL;
1673*7c478bd9Sstevel@tonic-gate 	vmem_heap = NULL;
1674*7c478bd9Sstevel@tonic-gate 	vmem_heap_alloc = NULL;
1675*7c478bd9Sstevel@tonic-gate 	vmem_heap_free = NULL;
1676*7c478bd9Sstevel@tonic-gate 
1677*7c478bd9Sstevel@tonic-gate 	bzero(vmem0, sizeof (vmem0));
1678*7c478bd9Sstevel@tonic-gate 	bzero(vmem_populator, sizeof (vmem_populator));
1679*7c478bd9Sstevel@tonic-gate 	bzero(vmem_seg0, sizeof (vmem_seg0));
1680*7c478bd9Sstevel@tonic-gate #endif
1681*7c478bd9Sstevel@tonic-gate }
1682*7c478bd9Sstevel@tonic-gate 
1683*7c478bd9Sstevel@tonic-gate /*
1684*7c478bd9Sstevel@tonic-gate  * Prepare vmem for use.
1685*7c478bd9Sstevel@tonic-gate  */
1686*7c478bd9Sstevel@tonic-gate vmem_t *
1687*7c478bd9Sstevel@tonic-gate vmem_init(const char *parent_name, size_t parent_quantum,
1688*7c478bd9Sstevel@tonic-gate     vmem_alloc_t *parent_alloc, vmem_free_t *parent_free,
1689*7c478bd9Sstevel@tonic-gate     const char *heap_name, void *heap_start, size_t heap_size,
1690*7c478bd9Sstevel@tonic-gate     size_t heap_quantum, vmem_alloc_t *heap_alloc, vmem_free_t *heap_free)
1691*7c478bd9Sstevel@tonic-gate {
1692*7c478bd9Sstevel@tonic-gate 	uint32_t id;
1693*7c478bd9Sstevel@tonic-gate 	int nseg = VMEM_SEG_INITIAL;
1694*7c478bd9Sstevel@tonic-gate 	vmem_t *parent, *heap;
1695*7c478bd9Sstevel@tonic-gate 
1696*7c478bd9Sstevel@tonic-gate 	ASSERT(vmem_internal_arena == NULL);
1697*7c478bd9Sstevel@tonic-gate 
1698*7c478bd9Sstevel@tonic-gate 	while (--nseg >= 0)
1699*7c478bd9Sstevel@tonic-gate 		vmem_putseg_global(&vmem_seg0[nseg]);
1700*7c478bd9Sstevel@tonic-gate 
1701*7c478bd9Sstevel@tonic-gate 	if (parent_name != NULL) {
1702*7c478bd9Sstevel@tonic-gate 		parent = vmem_create(parent_name,
1703*7c478bd9Sstevel@tonic-gate 		    heap_start, heap_size, parent_quantum,
1704*7c478bd9Sstevel@tonic-gate 		    NULL, NULL, NULL, 0,
1705*7c478bd9Sstevel@tonic-gate 		    VM_SLEEP | VMC_POPULATOR);
1706*7c478bd9Sstevel@tonic-gate 		heap_start = NULL;
1707*7c478bd9Sstevel@tonic-gate 		heap_size = 0;
1708*7c478bd9Sstevel@tonic-gate 	} else {
1709*7c478bd9Sstevel@tonic-gate 		ASSERT(parent_alloc == NULL && parent_free == NULL);
1710*7c478bd9Sstevel@tonic-gate 		parent = NULL;
1711*7c478bd9Sstevel@tonic-gate 	}
1712*7c478bd9Sstevel@tonic-gate 
1713*7c478bd9Sstevel@tonic-gate 	heap = vmem_create(heap_name,
1714*7c478bd9Sstevel@tonic-gate 	    heap_start, heap_size, heap_quantum,
1715*7c478bd9Sstevel@tonic-gate 	    parent_alloc, parent_free, parent, 0,
1716*7c478bd9Sstevel@tonic-gate 	    VM_SLEEP | VMC_POPULATOR);
1717*7c478bd9Sstevel@tonic-gate 
1718*7c478bd9Sstevel@tonic-gate 	vmem_heap = heap;
1719*7c478bd9Sstevel@tonic-gate 	vmem_heap_alloc = heap_alloc;
1720*7c478bd9Sstevel@tonic-gate 	vmem_heap_free = heap_free;
1721*7c478bd9Sstevel@tonic-gate 
1722*7c478bd9Sstevel@tonic-gate 	vmem_internal_arena = vmem_create("vmem_internal",
1723*7c478bd9Sstevel@tonic-gate 	    NULL, 0, heap_quantum,
1724*7c478bd9Sstevel@tonic-gate 	    heap_alloc, heap_free, heap, 0,
1725*7c478bd9Sstevel@tonic-gate 	    VM_SLEEP | VMC_POPULATOR);
1726*7c478bd9Sstevel@tonic-gate 
1727*7c478bd9Sstevel@tonic-gate 	vmem_seg_arena = vmem_create("vmem_seg",
1728*7c478bd9Sstevel@tonic-gate 	    NULL, 0, heap_quantum,
1729*7c478bd9Sstevel@tonic-gate 	    vmem_alloc, vmem_free, vmem_internal_arena, 0,
1730*7c478bd9Sstevel@tonic-gate 	    VM_SLEEP | VMC_POPULATOR);
1731*7c478bd9Sstevel@tonic-gate 
1732*7c478bd9Sstevel@tonic-gate 	vmem_hash_arena = vmem_create("vmem_hash",
1733*7c478bd9Sstevel@tonic-gate 	    NULL, 0, 8,
1734*7c478bd9Sstevel@tonic-gate 	    vmem_alloc, vmem_free, vmem_internal_arena, 0,
1735*7c478bd9Sstevel@tonic-gate 	    VM_SLEEP);
1736*7c478bd9Sstevel@tonic-gate 
1737*7c478bd9Sstevel@tonic-gate 	vmem_vmem_arena = vmem_create("vmem_vmem",
1738*7c478bd9Sstevel@tonic-gate 	    vmem0, sizeof (vmem0), 1,
1739*7c478bd9Sstevel@tonic-gate 	    vmem_alloc, vmem_free, vmem_internal_arena, 0,
1740*7c478bd9Sstevel@tonic-gate 	    VM_SLEEP);
1741*7c478bd9Sstevel@tonic-gate 
1742*7c478bd9Sstevel@tonic-gate 	for (id = 0; id < vmem_id; id++)
1743*7c478bd9Sstevel@tonic-gate 		(void) vmem_xalloc(vmem_vmem_arena, sizeof (vmem_t),
1744*7c478bd9Sstevel@tonic-gate 		    1, 0, 0, &vmem0[id], &vmem0[id + 1],
1745*7c478bd9Sstevel@tonic-gate 		    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
1746*7c478bd9Sstevel@tonic-gate 
1747*7c478bd9Sstevel@tonic-gate 	return (heap);
1748*7c478bd9Sstevel@tonic-gate }
1749*7c478bd9Sstevel@tonic-gate 
1750*7c478bd9Sstevel@tonic-gate void
1751*7c478bd9Sstevel@tonic-gate vmem_no_debug(void)
1752*7c478bd9Sstevel@tonic-gate {
1753*7c478bd9Sstevel@tonic-gate 	/*
1754*7c478bd9Sstevel@tonic-gate 	 * This size must be a multiple of the minimum required alignment,
1755*7c478bd9Sstevel@tonic-gate 	 * since vmem_populate allocates them compactly.
1756*7c478bd9Sstevel@tonic-gate 	 */
1757*7c478bd9Sstevel@tonic-gate 	vmem_seg_size = P2ROUNDUP(offsetof(vmem_seg_t, vs_thread),
1758*7c478bd9Sstevel@tonic-gate 	    sizeof (hrtime_t));
1759*7c478bd9Sstevel@tonic-gate }
1760*7c478bd9Sstevel@tonic-gate 
1761*7c478bd9Sstevel@tonic-gate /*
1762*7c478bd9Sstevel@tonic-gate  * Lockup and release, for fork1(2) handling.
1763*7c478bd9Sstevel@tonic-gate  */
1764*7c478bd9Sstevel@tonic-gate void
1765*7c478bd9Sstevel@tonic-gate vmem_lockup(void)
1766*7c478bd9Sstevel@tonic-gate {
1767*7c478bd9Sstevel@tonic-gate 	vmem_t *cur;
1768*7c478bd9Sstevel@tonic-gate 
1769*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&vmem_list_lock);
1770*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&vmem_nosleep_lock.vmpl_mutex);
1771*7c478bd9Sstevel@tonic-gate 
1772*7c478bd9Sstevel@tonic-gate 	/*
1773*7c478bd9Sstevel@tonic-gate 	 * Lock up and broadcast all arenas.
1774*7c478bd9Sstevel@tonic-gate 	 */
1775*7c478bd9Sstevel@tonic-gate 	for (cur = vmem_list; cur != NULL; cur = cur->vm_next) {
1776*7c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&cur->vm_lock);
1777*7c478bd9Sstevel@tonic-gate 		(void) cond_broadcast(&cur->vm_cv);
1778*7c478bd9Sstevel@tonic-gate 	}
1779*7c478bd9Sstevel@tonic-gate 
1780*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&vmem_segfree_lock);
1781*7c478bd9Sstevel@tonic-gate }
1782*7c478bd9Sstevel@tonic-gate 
1783*7c478bd9Sstevel@tonic-gate void
1784*7c478bd9Sstevel@tonic-gate vmem_release(void)
1785*7c478bd9Sstevel@tonic-gate {
1786*7c478bd9Sstevel@tonic-gate 	vmem_t *cur;
1787*7c478bd9Sstevel@tonic-gate 
1788*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&vmem_nosleep_lock.vmpl_mutex);
1789*7c478bd9Sstevel@tonic-gate 
1790*7c478bd9Sstevel@tonic-gate 	for (cur = vmem_list; cur != NULL; cur = cur->vm_next)
1791*7c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&cur->vm_lock);
1792*7c478bd9Sstevel@tonic-gate 
1793*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&vmem_segfree_lock);
1794*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&vmem_list_lock);
1795*7c478bd9Sstevel@tonic-gate }
1796