xref: /titanic_53/usr/src/uts/common/os/kmem.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  * Kernel memory allocator, as described in the following two papers:
31*7c478bd9Sstevel@tonic-gate  *
32*7c478bd9Sstevel@tonic-gate  * Jeff Bonwick,
33*7c478bd9Sstevel@tonic-gate  * The Slab Allocator: An Object-Caching Kernel Memory Allocator.
34*7c478bd9Sstevel@tonic-gate  * Proceedings of the Summer 1994 Usenix Conference.
35*7c478bd9Sstevel@tonic-gate  * Available as /shared/sac/PSARC/1994/028/materials/kmem.pdf.
36*7c478bd9Sstevel@tonic-gate  *
37*7c478bd9Sstevel@tonic-gate  * Jeff Bonwick and Jonathan Adams,
38*7c478bd9Sstevel@tonic-gate  * Magazines and vmem: Extending the Slab Allocator to Many CPUs and
39*7c478bd9Sstevel@tonic-gate  * Arbitrary Resources.
40*7c478bd9Sstevel@tonic-gate  * Proceedings of the 2001 Usenix Conference.
41*7c478bd9Sstevel@tonic-gate  * Available as /shared/sac/PSARC/2000/550/materials/vmem.pdf.
42*7c478bd9Sstevel@tonic-gate  */
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate #include <sys/kmem_impl.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/vmem_impl.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/vm.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/proc.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/tuneable.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
52*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
53*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
54*7c478bd9Sstevel@tonic-gate #include <sys/mutex.h>
55*7c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
56*7c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
57*7c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
58*7c478bd9Sstevel@tonic-gate #include <sys/disp.h>
59*7c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
60*7c478bd9Sstevel@tonic-gate #include <sys/log.h>
61*7c478bd9Sstevel@tonic-gate #include <sys/callb.h>
62*7c478bd9Sstevel@tonic-gate #include <sys/taskq.h>
63*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
64*7c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
65*7c478bd9Sstevel@tonic-gate #include <sys/id32.h>
66*7c478bd9Sstevel@tonic-gate #include <sys/zone.h>
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate extern void streams_msg_init(void);
69*7c478bd9Sstevel@tonic-gate extern int segkp_fromheap;
70*7c478bd9Sstevel@tonic-gate extern void segkp_cache_free(void);
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate struct kmem_cache_kstat {
73*7c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_buf_size;
74*7c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_align;
75*7c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_chunk_size;
76*7c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_slab_size;
77*7c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_alloc;
78*7c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_alloc_fail;
79*7c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_free;
80*7c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_depot_alloc;
81*7c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_depot_free;
82*7c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_depot_contention;
83*7c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_slab_alloc;
84*7c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_slab_free;
85*7c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_buf_constructed;
86*7c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_buf_avail;
87*7c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_buf_inuse;
88*7c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_buf_total;
89*7c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_buf_max;
90*7c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_slab_create;
91*7c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_slab_destroy;
92*7c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_vmem_source;
93*7c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_hash_size;
94*7c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_hash_lookup_depth;
95*7c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_hash_rescale;
96*7c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_full_magazines;
97*7c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_empty_magazines;
98*7c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_magazine_size;
99*7c478bd9Sstevel@tonic-gate } kmem_cache_kstat = {
100*7c478bd9Sstevel@tonic-gate 	{ "buf_size",		KSTAT_DATA_UINT64 },
101*7c478bd9Sstevel@tonic-gate 	{ "align",		KSTAT_DATA_UINT64 },
102*7c478bd9Sstevel@tonic-gate 	{ "chunk_size",		KSTAT_DATA_UINT64 },
103*7c478bd9Sstevel@tonic-gate 	{ "slab_size",		KSTAT_DATA_UINT64 },
104*7c478bd9Sstevel@tonic-gate 	{ "alloc",		KSTAT_DATA_UINT64 },
105*7c478bd9Sstevel@tonic-gate 	{ "alloc_fail",		KSTAT_DATA_UINT64 },
106*7c478bd9Sstevel@tonic-gate 	{ "free",		KSTAT_DATA_UINT64 },
107*7c478bd9Sstevel@tonic-gate 	{ "depot_alloc",	KSTAT_DATA_UINT64 },
108*7c478bd9Sstevel@tonic-gate 	{ "depot_free",		KSTAT_DATA_UINT64 },
109*7c478bd9Sstevel@tonic-gate 	{ "depot_contention",	KSTAT_DATA_UINT64 },
110*7c478bd9Sstevel@tonic-gate 	{ "slab_alloc",		KSTAT_DATA_UINT64 },
111*7c478bd9Sstevel@tonic-gate 	{ "slab_free",		KSTAT_DATA_UINT64 },
112*7c478bd9Sstevel@tonic-gate 	{ "buf_constructed",	KSTAT_DATA_UINT64 },
113*7c478bd9Sstevel@tonic-gate 	{ "buf_avail",		KSTAT_DATA_UINT64 },
114*7c478bd9Sstevel@tonic-gate 	{ "buf_inuse",		KSTAT_DATA_UINT64 },
115*7c478bd9Sstevel@tonic-gate 	{ "buf_total",		KSTAT_DATA_UINT64 },
116*7c478bd9Sstevel@tonic-gate 	{ "buf_max",		KSTAT_DATA_UINT64 },
117*7c478bd9Sstevel@tonic-gate 	{ "slab_create",	KSTAT_DATA_UINT64 },
118*7c478bd9Sstevel@tonic-gate 	{ "slab_destroy",	KSTAT_DATA_UINT64 },
119*7c478bd9Sstevel@tonic-gate 	{ "vmem_source",	KSTAT_DATA_UINT64 },
120*7c478bd9Sstevel@tonic-gate 	{ "hash_size",		KSTAT_DATA_UINT64 },
121*7c478bd9Sstevel@tonic-gate 	{ "hash_lookup_depth",	KSTAT_DATA_UINT64 },
122*7c478bd9Sstevel@tonic-gate 	{ "hash_rescale",	KSTAT_DATA_UINT64 },
123*7c478bd9Sstevel@tonic-gate 	{ "full_magazines",	KSTAT_DATA_UINT64 },
124*7c478bd9Sstevel@tonic-gate 	{ "empty_magazines",	KSTAT_DATA_UINT64 },
125*7c478bd9Sstevel@tonic-gate 	{ "magazine_size",	KSTAT_DATA_UINT64 },
126*7c478bd9Sstevel@tonic-gate };
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate static kmutex_t kmem_cache_kstat_lock;
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate /*
131*7c478bd9Sstevel@tonic-gate  * The default set of caches to back kmem_alloc().
132*7c478bd9Sstevel@tonic-gate  * These sizes should be reevaluated periodically.
133*7c478bd9Sstevel@tonic-gate  *
134*7c478bd9Sstevel@tonic-gate  * We want allocations that are multiples of the coherency granularity
135*7c478bd9Sstevel@tonic-gate  * (64 bytes) to be satisfied from a cache which is a multiple of 64
136*7c478bd9Sstevel@tonic-gate  * bytes, so that it will be 64-byte aligned.  For all multiples of 64,
137*7c478bd9Sstevel@tonic-gate  * the next kmem_cache_size greater than or equal to it must be a
138*7c478bd9Sstevel@tonic-gate  * multiple of 64.
139*7c478bd9Sstevel@tonic-gate  */
140*7c478bd9Sstevel@tonic-gate static const int kmem_alloc_sizes[] = {
141*7c478bd9Sstevel@tonic-gate 	1 * 8,
142*7c478bd9Sstevel@tonic-gate 	2 * 8,
143*7c478bd9Sstevel@tonic-gate 	3 * 8,
144*7c478bd9Sstevel@tonic-gate 	4 * 8,		5 * 8,		6 * 8,		7 * 8,
145*7c478bd9Sstevel@tonic-gate 	4 * 16,		5 * 16,		6 * 16,		7 * 16,
146*7c478bd9Sstevel@tonic-gate 	4 * 32,		5 * 32,		6 * 32,		7 * 32,
147*7c478bd9Sstevel@tonic-gate 	4 * 64,		5 * 64,		6 * 64,		7 * 64,
148*7c478bd9Sstevel@tonic-gate 	4 * 128,	5 * 128,	6 * 128,	7 * 128,
149*7c478bd9Sstevel@tonic-gate 	P2ALIGN(8192 / 7, 64),
150*7c478bd9Sstevel@tonic-gate 	P2ALIGN(8192 / 6, 64),
151*7c478bd9Sstevel@tonic-gate 	P2ALIGN(8192 / 5, 64),
152*7c478bd9Sstevel@tonic-gate 	P2ALIGN(8192 / 4, 64),
153*7c478bd9Sstevel@tonic-gate 	P2ALIGN(8192 / 3, 64),
154*7c478bd9Sstevel@tonic-gate 	P2ALIGN(8192 / 2, 64),
155*7c478bd9Sstevel@tonic-gate 	P2ALIGN(8192 / 1, 64),
156*7c478bd9Sstevel@tonic-gate 	4096 * 3,
157*7c478bd9Sstevel@tonic-gate 	8192 * 2,
158*7c478bd9Sstevel@tonic-gate };
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate #define	KMEM_MAXBUF	16384
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate static kmem_cache_t *kmem_alloc_table[KMEM_MAXBUF >> KMEM_ALIGN_SHIFT];
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate static kmem_magtype_t kmem_magtype[] = {
165*7c478bd9Sstevel@tonic-gate 	{ 1,	8,	3200,	65536	},
166*7c478bd9Sstevel@tonic-gate 	{ 3,	16,	256,	32768	},
167*7c478bd9Sstevel@tonic-gate 	{ 7,	32,	64,	16384	},
168*7c478bd9Sstevel@tonic-gate 	{ 15,	64,	0,	8192	},
169*7c478bd9Sstevel@tonic-gate 	{ 31,	64,	0,	4096	},
170*7c478bd9Sstevel@tonic-gate 	{ 47,	64,	0,	2048	},
171*7c478bd9Sstevel@tonic-gate 	{ 63,	64,	0,	1024	},
172*7c478bd9Sstevel@tonic-gate 	{ 95,	64,	0,	512	},
173*7c478bd9Sstevel@tonic-gate 	{ 143,	64,	0,	0	},
174*7c478bd9Sstevel@tonic-gate };
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate static uint32_t kmem_reaping;
177*7c478bd9Sstevel@tonic-gate static uint32_t kmem_reaping_idspace;
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate /*
180*7c478bd9Sstevel@tonic-gate  * kmem tunables
181*7c478bd9Sstevel@tonic-gate  */
182*7c478bd9Sstevel@tonic-gate clock_t kmem_reap_interval;	/* cache reaping rate [15 * HZ ticks] */
183*7c478bd9Sstevel@tonic-gate int kmem_depot_contention = 3;	/* max failed tryenters per real interval */
184*7c478bd9Sstevel@tonic-gate pgcnt_t kmem_reapahead = 0;	/* start reaping N pages before pageout */
185*7c478bd9Sstevel@tonic-gate int kmem_panic = 1;		/* whether to panic on error */
186*7c478bd9Sstevel@tonic-gate int kmem_logging = 1;		/* kmem_log_enter() override */
187*7c478bd9Sstevel@tonic-gate uint32_t kmem_mtbf = 0;		/* mean time between failures [default: off] */
188*7c478bd9Sstevel@tonic-gate size_t kmem_transaction_log_size; /* transaction log size [2% of memory] */
189*7c478bd9Sstevel@tonic-gate size_t kmem_content_log_size;	/* content log size [2% of memory] */
190*7c478bd9Sstevel@tonic-gate size_t kmem_failure_log_size;	/* failure log [4 pages per CPU] */
191*7c478bd9Sstevel@tonic-gate size_t kmem_slab_log_size;	/* slab create log [4 pages per CPU] */
192*7c478bd9Sstevel@tonic-gate size_t kmem_content_maxsave = 256; /* KMF_CONTENTS max bytes to log */
193*7c478bd9Sstevel@tonic-gate size_t kmem_lite_minsize = 0;	/* minimum buffer size for KMF_LITE */
194*7c478bd9Sstevel@tonic-gate size_t kmem_lite_maxalign = 1024; /* maximum buffer alignment for KMF_LITE */
195*7c478bd9Sstevel@tonic-gate int kmem_lite_pcs = 4;		/* number of PCs to store in KMF_LITE mode */
196*7c478bd9Sstevel@tonic-gate size_t kmem_maxverify;		/* maximum bytes to inspect in debug routines */
197*7c478bd9Sstevel@tonic-gate size_t kmem_minfirewall;	/* hardware-enforced redzone threshold */
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
200*7c478bd9Sstevel@tonic-gate int kmem_flags = KMF_AUDIT | KMF_DEADBEEF | KMF_REDZONE | KMF_CONTENTS;
201*7c478bd9Sstevel@tonic-gate #else
202*7c478bd9Sstevel@tonic-gate int kmem_flags = 0;
203*7c478bd9Sstevel@tonic-gate #endif
204*7c478bd9Sstevel@tonic-gate int kmem_ready;
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate static kmem_cache_t	*kmem_slab_cache;
207*7c478bd9Sstevel@tonic-gate static kmem_cache_t	*kmem_bufctl_cache;
208*7c478bd9Sstevel@tonic-gate static kmem_cache_t	*kmem_bufctl_audit_cache;
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate static kmutex_t		kmem_cache_lock;	/* inter-cache linkage only */
211*7c478bd9Sstevel@tonic-gate kmem_cache_t		kmem_null_cache;
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate static taskq_t		*kmem_taskq;
214*7c478bd9Sstevel@tonic-gate static kmutex_t		kmem_flags_lock;
215*7c478bd9Sstevel@tonic-gate static vmem_t		*kmem_metadata_arena;
216*7c478bd9Sstevel@tonic-gate static vmem_t		*kmem_msb_arena;	/* arena for metadata caches */
217*7c478bd9Sstevel@tonic-gate static vmem_t		*kmem_cache_arena;
218*7c478bd9Sstevel@tonic-gate static vmem_t		*kmem_hash_arena;
219*7c478bd9Sstevel@tonic-gate static vmem_t		*kmem_log_arena;
220*7c478bd9Sstevel@tonic-gate static vmem_t		*kmem_oversize_arena;
221*7c478bd9Sstevel@tonic-gate static vmem_t		*kmem_va_arena;
222*7c478bd9Sstevel@tonic-gate static vmem_t		*kmem_default_arena;
223*7c478bd9Sstevel@tonic-gate static vmem_t		*kmem_firewall_va_arena;
224*7c478bd9Sstevel@tonic-gate static vmem_t		*kmem_firewall_arena;
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate kmem_log_header_t	*kmem_transaction_log;
227*7c478bd9Sstevel@tonic-gate kmem_log_header_t	*kmem_content_log;
228*7c478bd9Sstevel@tonic-gate kmem_log_header_t	*kmem_failure_log;
229*7c478bd9Sstevel@tonic-gate kmem_log_header_t	*kmem_slab_log;
230*7c478bd9Sstevel@tonic-gate 
231*7c478bd9Sstevel@tonic-gate static int		kmem_lite_count; /* # of PCs in kmem_buftag_lite_t */
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate #define	KMEM_BUFTAG_LITE_ENTER(bt, count, caller)			\
234*7c478bd9Sstevel@tonic-gate 	if ((count) > 0) {						\
235*7c478bd9Sstevel@tonic-gate 		pc_t *_s = ((kmem_buftag_lite_t *)(bt))->bt_history;	\
236*7c478bd9Sstevel@tonic-gate 		pc_t *_e;						\
237*7c478bd9Sstevel@tonic-gate 		/* memmove() the old entries down one notch */		\
238*7c478bd9Sstevel@tonic-gate 		for (_e = &_s[(count) - 1]; _e > _s; _e--)		\
239*7c478bd9Sstevel@tonic-gate 			*_e = *(_e - 1);				\
240*7c478bd9Sstevel@tonic-gate 		*_s = (uintptr_t)(caller);				\
241*7c478bd9Sstevel@tonic-gate 	}
242*7c478bd9Sstevel@tonic-gate 
243*7c478bd9Sstevel@tonic-gate #define	KMERR_MODIFIED	0	/* buffer modified while on freelist */
244*7c478bd9Sstevel@tonic-gate #define	KMERR_REDZONE	1	/* redzone violation (write past end of buf) */
245*7c478bd9Sstevel@tonic-gate #define	KMERR_DUPFREE	2	/* freed a buffer twice */
246*7c478bd9Sstevel@tonic-gate #define	KMERR_BADADDR	3	/* freed a bad (unallocated) address */
247*7c478bd9Sstevel@tonic-gate #define	KMERR_BADBUFTAG	4	/* buftag corrupted */
248*7c478bd9Sstevel@tonic-gate #define	KMERR_BADBUFCTL	5	/* bufctl corrupted */
249*7c478bd9Sstevel@tonic-gate #define	KMERR_BADCACHE	6	/* freed a buffer to the wrong cache */
250*7c478bd9Sstevel@tonic-gate #define	KMERR_BADSIZE	7	/* alloc size != free size */
251*7c478bd9Sstevel@tonic-gate #define	KMERR_BADBASE	8	/* buffer base address wrong */
252*7c478bd9Sstevel@tonic-gate 
253*7c478bd9Sstevel@tonic-gate struct {
254*7c478bd9Sstevel@tonic-gate 	hrtime_t	kmp_timestamp;	/* timestamp of panic */
255*7c478bd9Sstevel@tonic-gate 	int		kmp_error;	/* type of kmem error */
256*7c478bd9Sstevel@tonic-gate 	void		*kmp_buffer;	/* buffer that induced panic */
257*7c478bd9Sstevel@tonic-gate 	void		*kmp_realbuf;	/* real start address for buffer */
258*7c478bd9Sstevel@tonic-gate 	kmem_cache_t	*kmp_cache;	/* buffer's cache according to client */
259*7c478bd9Sstevel@tonic-gate 	kmem_cache_t	*kmp_realcache;	/* actual cache containing buffer */
260*7c478bd9Sstevel@tonic-gate 	kmem_slab_t	*kmp_slab;	/* slab accoring to kmem_findslab() */
261*7c478bd9Sstevel@tonic-gate 	kmem_bufctl_t	*kmp_bufctl;	/* bufctl */
262*7c478bd9Sstevel@tonic-gate } kmem_panic_info;
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate static void
266*7c478bd9Sstevel@tonic-gate copy_pattern(uint64_t pattern, void *buf_arg, size_t size)
267*7c478bd9Sstevel@tonic-gate {
268*7c478bd9Sstevel@tonic-gate 	uint64_t *bufend = (uint64_t *)((char *)buf_arg + size);
269*7c478bd9Sstevel@tonic-gate 	uint64_t *buf = buf_arg;
270*7c478bd9Sstevel@tonic-gate 
271*7c478bd9Sstevel@tonic-gate 	while (buf < bufend)
272*7c478bd9Sstevel@tonic-gate 		*buf++ = pattern;
273*7c478bd9Sstevel@tonic-gate }
274*7c478bd9Sstevel@tonic-gate 
275*7c478bd9Sstevel@tonic-gate static void *
276*7c478bd9Sstevel@tonic-gate verify_pattern(uint64_t pattern, void *buf_arg, size_t size)
277*7c478bd9Sstevel@tonic-gate {
278*7c478bd9Sstevel@tonic-gate 	uint64_t *bufend = (uint64_t *)((char *)buf_arg + size);
279*7c478bd9Sstevel@tonic-gate 	uint64_t *buf;
280*7c478bd9Sstevel@tonic-gate 
281*7c478bd9Sstevel@tonic-gate 	for (buf = buf_arg; buf < bufend; buf++)
282*7c478bd9Sstevel@tonic-gate 		if (*buf != pattern)
283*7c478bd9Sstevel@tonic-gate 			return (buf);
284*7c478bd9Sstevel@tonic-gate 	return (NULL);
285*7c478bd9Sstevel@tonic-gate }
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate static void *
288*7c478bd9Sstevel@tonic-gate verify_and_copy_pattern(uint64_t old, uint64_t new, void *buf_arg, size_t size)
289*7c478bd9Sstevel@tonic-gate {
290*7c478bd9Sstevel@tonic-gate 	uint64_t *bufend = (uint64_t *)((char *)buf_arg + size);
291*7c478bd9Sstevel@tonic-gate 	uint64_t *buf;
292*7c478bd9Sstevel@tonic-gate 
293*7c478bd9Sstevel@tonic-gate 	for (buf = buf_arg; buf < bufend; buf++) {
294*7c478bd9Sstevel@tonic-gate 		if (*buf != old) {
295*7c478bd9Sstevel@tonic-gate 			copy_pattern(old, buf_arg,
296*7c478bd9Sstevel@tonic-gate 				(char *)buf - (char *)buf_arg);
297*7c478bd9Sstevel@tonic-gate 			return (buf);
298*7c478bd9Sstevel@tonic-gate 		}
299*7c478bd9Sstevel@tonic-gate 		*buf = new;
300*7c478bd9Sstevel@tonic-gate 	}
301*7c478bd9Sstevel@tonic-gate 
302*7c478bd9Sstevel@tonic-gate 	return (NULL);
303*7c478bd9Sstevel@tonic-gate }
304*7c478bd9Sstevel@tonic-gate 
305*7c478bd9Sstevel@tonic-gate static void
306*7c478bd9Sstevel@tonic-gate kmem_cache_applyall(void (*func)(kmem_cache_t *), taskq_t *tq, int tqflag)
307*7c478bd9Sstevel@tonic-gate {
308*7c478bd9Sstevel@tonic-gate 	kmem_cache_t *cp;
309*7c478bd9Sstevel@tonic-gate 
310*7c478bd9Sstevel@tonic-gate 	mutex_enter(&kmem_cache_lock);
311*7c478bd9Sstevel@tonic-gate 	for (cp = kmem_null_cache.cache_next; cp != &kmem_null_cache;
312*7c478bd9Sstevel@tonic-gate 	    cp = cp->cache_next)
313*7c478bd9Sstevel@tonic-gate 		if (tq != NULL)
314*7c478bd9Sstevel@tonic-gate 			(void) taskq_dispatch(tq, (task_func_t *)func, cp,
315*7c478bd9Sstevel@tonic-gate 			    tqflag);
316*7c478bd9Sstevel@tonic-gate 		else
317*7c478bd9Sstevel@tonic-gate 			func(cp);
318*7c478bd9Sstevel@tonic-gate 	mutex_exit(&kmem_cache_lock);
319*7c478bd9Sstevel@tonic-gate }
320*7c478bd9Sstevel@tonic-gate 
321*7c478bd9Sstevel@tonic-gate static void
322*7c478bd9Sstevel@tonic-gate kmem_cache_applyall_id(void (*func)(kmem_cache_t *), taskq_t *tq, int tqflag)
323*7c478bd9Sstevel@tonic-gate {
324*7c478bd9Sstevel@tonic-gate 	kmem_cache_t *cp;
325*7c478bd9Sstevel@tonic-gate 
326*7c478bd9Sstevel@tonic-gate 	mutex_enter(&kmem_cache_lock);
327*7c478bd9Sstevel@tonic-gate 	for (cp = kmem_null_cache.cache_next; cp != &kmem_null_cache;
328*7c478bd9Sstevel@tonic-gate 	    cp = cp->cache_next) {
329*7c478bd9Sstevel@tonic-gate 		if (!(cp->cache_cflags & KMC_IDENTIFIER))
330*7c478bd9Sstevel@tonic-gate 			continue;
331*7c478bd9Sstevel@tonic-gate 		if (tq != NULL)
332*7c478bd9Sstevel@tonic-gate 			(void) taskq_dispatch(tq, (task_func_t *)func, cp,
333*7c478bd9Sstevel@tonic-gate 			    tqflag);
334*7c478bd9Sstevel@tonic-gate 		else
335*7c478bd9Sstevel@tonic-gate 			func(cp);
336*7c478bd9Sstevel@tonic-gate 	}
337*7c478bd9Sstevel@tonic-gate 	mutex_exit(&kmem_cache_lock);
338*7c478bd9Sstevel@tonic-gate }
339*7c478bd9Sstevel@tonic-gate 
340*7c478bd9Sstevel@tonic-gate /*
341*7c478bd9Sstevel@tonic-gate  * Debugging support.  Given a buffer address, find its slab.
342*7c478bd9Sstevel@tonic-gate  */
343*7c478bd9Sstevel@tonic-gate static kmem_slab_t *
344*7c478bd9Sstevel@tonic-gate kmem_findslab(kmem_cache_t *cp, void *buf)
345*7c478bd9Sstevel@tonic-gate {
346*7c478bd9Sstevel@tonic-gate 	kmem_slab_t *sp;
347*7c478bd9Sstevel@tonic-gate 
348*7c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->cache_lock);
349*7c478bd9Sstevel@tonic-gate 	for (sp = cp->cache_nullslab.slab_next;
350*7c478bd9Sstevel@tonic-gate 	    sp != &cp->cache_nullslab; sp = sp->slab_next) {
351*7c478bd9Sstevel@tonic-gate 		if (KMEM_SLAB_MEMBER(sp, buf)) {
352*7c478bd9Sstevel@tonic-gate 			mutex_exit(&cp->cache_lock);
353*7c478bd9Sstevel@tonic-gate 			return (sp);
354*7c478bd9Sstevel@tonic-gate 		}
355*7c478bd9Sstevel@tonic-gate 	}
356*7c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->cache_lock);
357*7c478bd9Sstevel@tonic-gate 
358*7c478bd9Sstevel@tonic-gate 	return (NULL);
359*7c478bd9Sstevel@tonic-gate }
360*7c478bd9Sstevel@tonic-gate 
361*7c478bd9Sstevel@tonic-gate static void
362*7c478bd9Sstevel@tonic-gate kmem_error(int error, kmem_cache_t *cparg, void *bufarg)
363*7c478bd9Sstevel@tonic-gate {
364*7c478bd9Sstevel@tonic-gate 	kmem_buftag_t *btp = NULL;
365*7c478bd9Sstevel@tonic-gate 	kmem_bufctl_t *bcp = NULL;
366*7c478bd9Sstevel@tonic-gate 	kmem_cache_t *cp = cparg;
367*7c478bd9Sstevel@tonic-gate 	kmem_slab_t *sp;
368*7c478bd9Sstevel@tonic-gate 	uint64_t *off;
369*7c478bd9Sstevel@tonic-gate 	void *buf = bufarg;
370*7c478bd9Sstevel@tonic-gate 
371*7c478bd9Sstevel@tonic-gate 	kmem_logging = 0;	/* stop logging when a bad thing happens */
372*7c478bd9Sstevel@tonic-gate 
373*7c478bd9Sstevel@tonic-gate 	kmem_panic_info.kmp_timestamp = gethrtime();
374*7c478bd9Sstevel@tonic-gate 
375*7c478bd9Sstevel@tonic-gate 	sp = kmem_findslab(cp, buf);
376*7c478bd9Sstevel@tonic-gate 	if (sp == NULL) {
377*7c478bd9Sstevel@tonic-gate 		for (cp = kmem_null_cache.cache_prev; cp != &kmem_null_cache;
378*7c478bd9Sstevel@tonic-gate 		    cp = cp->cache_prev) {
379*7c478bd9Sstevel@tonic-gate 			if ((sp = kmem_findslab(cp, buf)) != NULL)
380*7c478bd9Sstevel@tonic-gate 				break;
381*7c478bd9Sstevel@tonic-gate 		}
382*7c478bd9Sstevel@tonic-gate 	}
383*7c478bd9Sstevel@tonic-gate 
384*7c478bd9Sstevel@tonic-gate 	if (sp == NULL) {
385*7c478bd9Sstevel@tonic-gate 		cp = NULL;
386*7c478bd9Sstevel@tonic-gate 		error = KMERR_BADADDR;
387*7c478bd9Sstevel@tonic-gate 	} else {
388*7c478bd9Sstevel@tonic-gate 		if (cp != cparg)
389*7c478bd9Sstevel@tonic-gate 			error = KMERR_BADCACHE;
390*7c478bd9Sstevel@tonic-gate 		else
391*7c478bd9Sstevel@tonic-gate 			buf = (char *)bufarg - ((uintptr_t)bufarg -
392*7c478bd9Sstevel@tonic-gate 			    (uintptr_t)sp->slab_base) % cp->cache_chunksize;
393*7c478bd9Sstevel@tonic-gate 		if (buf != bufarg)
394*7c478bd9Sstevel@tonic-gate 			error = KMERR_BADBASE;
395*7c478bd9Sstevel@tonic-gate 		if (cp->cache_flags & KMF_BUFTAG)
396*7c478bd9Sstevel@tonic-gate 			btp = KMEM_BUFTAG(cp, buf);
397*7c478bd9Sstevel@tonic-gate 		if (cp->cache_flags & KMF_HASH) {
398*7c478bd9Sstevel@tonic-gate 			mutex_enter(&cp->cache_lock);
399*7c478bd9Sstevel@tonic-gate 			for (bcp = *KMEM_HASH(cp, buf); bcp; bcp = bcp->bc_next)
400*7c478bd9Sstevel@tonic-gate 				if (bcp->bc_addr == buf)
401*7c478bd9Sstevel@tonic-gate 					break;
402*7c478bd9Sstevel@tonic-gate 			mutex_exit(&cp->cache_lock);
403*7c478bd9Sstevel@tonic-gate 			if (bcp == NULL && btp != NULL)
404*7c478bd9Sstevel@tonic-gate 				bcp = btp->bt_bufctl;
405*7c478bd9Sstevel@tonic-gate 			if (kmem_findslab(cp->cache_bufctl_cache, bcp) ==
406*7c478bd9Sstevel@tonic-gate 			    NULL || P2PHASE((uintptr_t)bcp, KMEM_ALIGN) ||
407*7c478bd9Sstevel@tonic-gate 			    bcp->bc_addr != buf) {
408*7c478bd9Sstevel@tonic-gate 				error = KMERR_BADBUFCTL;
409*7c478bd9Sstevel@tonic-gate 				bcp = NULL;
410*7c478bd9Sstevel@tonic-gate 			}
411*7c478bd9Sstevel@tonic-gate 		}
412*7c478bd9Sstevel@tonic-gate 	}
413*7c478bd9Sstevel@tonic-gate 
414*7c478bd9Sstevel@tonic-gate 	kmem_panic_info.kmp_error = error;
415*7c478bd9Sstevel@tonic-gate 	kmem_panic_info.kmp_buffer = bufarg;
416*7c478bd9Sstevel@tonic-gate 	kmem_panic_info.kmp_realbuf = buf;
417*7c478bd9Sstevel@tonic-gate 	kmem_panic_info.kmp_cache = cparg;
418*7c478bd9Sstevel@tonic-gate 	kmem_panic_info.kmp_realcache = cp;
419*7c478bd9Sstevel@tonic-gate 	kmem_panic_info.kmp_slab = sp;
420*7c478bd9Sstevel@tonic-gate 	kmem_panic_info.kmp_bufctl = bcp;
421*7c478bd9Sstevel@tonic-gate 
422*7c478bd9Sstevel@tonic-gate 	printf("kernel memory allocator: ");
423*7c478bd9Sstevel@tonic-gate 
424*7c478bd9Sstevel@tonic-gate 	switch (error) {
425*7c478bd9Sstevel@tonic-gate 
426*7c478bd9Sstevel@tonic-gate 	case KMERR_MODIFIED:
427*7c478bd9Sstevel@tonic-gate 		printf("buffer modified after being freed\n");
428*7c478bd9Sstevel@tonic-gate 		off = verify_pattern(KMEM_FREE_PATTERN, buf, cp->cache_verify);
429*7c478bd9Sstevel@tonic-gate 		if (off == NULL)	/* shouldn't happen */
430*7c478bd9Sstevel@tonic-gate 			off = buf;
431*7c478bd9Sstevel@tonic-gate 		printf("modification occurred at offset 0x%lx "
432*7c478bd9Sstevel@tonic-gate 		    "(0x%llx replaced by 0x%llx)\n",
433*7c478bd9Sstevel@tonic-gate 		    (uintptr_t)off - (uintptr_t)buf,
434*7c478bd9Sstevel@tonic-gate 		    (longlong_t)KMEM_FREE_PATTERN, (longlong_t)*off);
435*7c478bd9Sstevel@tonic-gate 		break;
436*7c478bd9Sstevel@tonic-gate 
437*7c478bd9Sstevel@tonic-gate 	case KMERR_REDZONE:
438*7c478bd9Sstevel@tonic-gate 		printf("redzone violation: write past end of buffer\n");
439*7c478bd9Sstevel@tonic-gate 		break;
440*7c478bd9Sstevel@tonic-gate 
441*7c478bd9Sstevel@tonic-gate 	case KMERR_BADADDR:
442*7c478bd9Sstevel@tonic-gate 		printf("invalid free: buffer not in cache\n");
443*7c478bd9Sstevel@tonic-gate 		break;
444*7c478bd9Sstevel@tonic-gate 
445*7c478bd9Sstevel@tonic-gate 	case KMERR_DUPFREE:
446*7c478bd9Sstevel@tonic-gate 		printf("duplicate free: buffer freed twice\n");
447*7c478bd9Sstevel@tonic-gate 		break;
448*7c478bd9Sstevel@tonic-gate 
449*7c478bd9Sstevel@tonic-gate 	case KMERR_BADBUFTAG:
450*7c478bd9Sstevel@tonic-gate 		printf("boundary tag corrupted\n");
451*7c478bd9Sstevel@tonic-gate 		printf("bcp ^ bxstat = %lx, should be %lx\n",
452*7c478bd9Sstevel@tonic-gate 		    (intptr_t)btp->bt_bufctl ^ btp->bt_bxstat,
453*7c478bd9Sstevel@tonic-gate 		    KMEM_BUFTAG_FREE);
454*7c478bd9Sstevel@tonic-gate 		break;
455*7c478bd9Sstevel@tonic-gate 
456*7c478bd9Sstevel@tonic-gate 	case KMERR_BADBUFCTL:
457*7c478bd9Sstevel@tonic-gate 		printf("bufctl corrupted\n");
458*7c478bd9Sstevel@tonic-gate 		break;
459*7c478bd9Sstevel@tonic-gate 
460*7c478bd9Sstevel@tonic-gate 	case KMERR_BADCACHE:
461*7c478bd9Sstevel@tonic-gate 		printf("buffer freed to wrong cache\n");
462*7c478bd9Sstevel@tonic-gate 		printf("buffer was allocated from %s,\n", cp->cache_name);
463*7c478bd9Sstevel@tonic-gate 		printf("caller attempting free to %s.\n", cparg->cache_name);
464*7c478bd9Sstevel@tonic-gate 		break;
465*7c478bd9Sstevel@tonic-gate 
466*7c478bd9Sstevel@tonic-gate 	case KMERR_BADSIZE:
467*7c478bd9Sstevel@tonic-gate 		printf("bad free: free size (%u) != alloc size (%u)\n",
468*7c478bd9Sstevel@tonic-gate 		    KMEM_SIZE_DECODE(((uint32_t *)btp)[0]),
469*7c478bd9Sstevel@tonic-gate 		    KMEM_SIZE_DECODE(((uint32_t *)btp)[1]));
470*7c478bd9Sstevel@tonic-gate 		break;
471*7c478bd9Sstevel@tonic-gate 
472*7c478bd9Sstevel@tonic-gate 	case KMERR_BADBASE:
473*7c478bd9Sstevel@tonic-gate 		printf("bad free: free address (%p) != alloc address (%p)\n",
474*7c478bd9Sstevel@tonic-gate 		    bufarg, buf);
475*7c478bd9Sstevel@tonic-gate 		break;
476*7c478bd9Sstevel@tonic-gate 	}
477*7c478bd9Sstevel@tonic-gate 
478*7c478bd9Sstevel@tonic-gate 	printf("buffer=%p  bufctl=%p  cache: %s\n",
479*7c478bd9Sstevel@tonic-gate 	    bufarg, (void *)bcp, cparg->cache_name);
480*7c478bd9Sstevel@tonic-gate 
481*7c478bd9Sstevel@tonic-gate 	if (bcp != NULL && (cp->cache_flags & KMF_AUDIT) &&
482*7c478bd9Sstevel@tonic-gate 	    error != KMERR_BADBUFCTL) {
483*7c478bd9Sstevel@tonic-gate 		int d;
484*7c478bd9Sstevel@tonic-gate 		timestruc_t ts;
485*7c478bd9Sstevel@tonic-gate 		kmem_bufctl_audit_t *bcap = (kmem_bufctl_audit_t *)bcp;
486*7c478bd9Sstevel@tonic-gate 
487*7c478bd9Sstevel@tonic-gate 		hrt2ts(kmem_panic_info.kmp_timestamp - bcap->bc_timestamp, &ts);
488*7c478bd9Sstevel@tonic-gate 		printf("previous transaction on buffer %p:\n", buf);
489*7c478bd9Sstevel@tonic-gate 		printf("thread=%p  time=T-%ld.%09ld  slab=%p  cache: %s\n",
490*7c478bd9Sstevel@tonic-gate 		    (void *)bcap->bc_thread, ts.tv_sec, ts.tv_nsec,
491*7c478bd9Sstevel@tonic-gate 		    (void *)sp, cp->cache_name);
492*7c478bd9Sstevel@tonic-gate 		for (d = 0; d < MIN(bcap->bc_depth, KMEM_STACK_DEPTH); d++) {
493*7c478bd9Sstevel@tonic-gate 			ulong_t off;
494*7c478bd9Sstevel@tonic-gate 			char *sym = kobj_getsymname(bcap->bc_stack[d], &off);
495*7c478bd9Sstevel@tonic-gate 			printf("%s+%lx\n", sym ? sym : "?", off);
496*7c478bd9Sstevel@tonic-gate 		}
497*7c478bd9Sstevel@tonic-gate 	}
498*7c478bd9Sstevel@tonic-gate 	if (kmem_panic > 0)
499*7c478bd9Sstevel@tonic-gate 		panic("kernel heap corruption detected");
500*7c478bd9Sstevel@tonic-gate 	if (kmem_panic == 0)
501*7c478bd9Sstevel@tonic-gate 		debug_enter(NULL);
502*7c478bd9Sstevel@tonic-gate 	kmem_logging = 1;	/* resume logging */
503*7c478bd9Sstevel@tonic-gate }
504*7c478bd9Sstevel@tonic-gate 
505*7c478bd9Sstevel@tonic-gate static kmem_log_header_t *
506*7c478bd9Sstevel@tonic-gate kmem_log_init(size_t logsize)
507*7c478bd9Sstevel@tonic-gate {
508*7c478bd9Sstevel@tonic-gate 	kmem_log_header_t *lhp;
509*7c478bd9Sstevel@tonic-gate 	int nchunks = 4 * max_ncpus;
510*7c478bd9Sstevel@tonic-gate 	size_t lhsize = (size_t)&((kmem_log_header_t *)0)->lh_cpu[max_ncpus];
511*7c478bd9Sstevel@tonic-gate 	int i;
512*7c478bd9Sstevel@tonic-gate 
513*7c478bd9Sstevel@tonic-gate 	/*
514*7c478bd9Sstevel@tonic-gate 	 * Make sure that lhp->lh_cpu[] is nicely aligned
515*7c478bd9Sstevel@tonic-gate 	 * to prevent false sharing of cache lines.
516*7c478bd9Sstevel@tonic-gate 	 */
517*7c478bd9Sstevel@tonic-gate 	lhsize = P2ROUNDUP(lhsize, KMEM_ALIGN);
518*7c478bd9Sstevel@tonic-gate 	lhp = vmem_xalloc(kmem_log_arena, lhsize, 64, P2NPHASE(lhsize, 64), 0,
519*7c478bd9Sstevel@tonic-gate 	    NULL, NULL, VM_SLEEP);
520*7c478bd9Sstevel@tonic-gate 	bzero(lhp, lhsize);
521*7c478bd9Sstevel@tonic-gate 
522*7c478bd9Sstevel@tonic-gate 	mutex_init(&lhp->lh_lock, NULL, MUTEX_DEFAULT, NULL);
523*7c478bd9Sstevel@tonic-gate 	lhp->lh_nchunks = nchunks;
524*7c478bd9Sstevel@tonic-gate 	lhp->lh_chunksize = P2ROUNDUP(logsize / nchunks + 1, PAGESIZE);
525*7c478bd9Sstevel@tonic-gate 	lhp->lh_base = vmem_alloc(kmem_log_arena,
526*7c478bd9Sstevel@tonic-gate 	    lhp->lh_chunksize * nchunks, VM_SLEEP);
527*7c478bd9Sstevel@tonic-gate 	lhp->lh_free = vmem_alloc(kmem_log_arena,
528*7c478bd9Sstevel@tonic-gate 	    nchunks * sizeof (int), VM_SLEEP);
529*7c478bd9Sstevel@tonic-gate 	bzero(lhp->lh_base, lhp->lh_chunksize * nchunks);
530*7c478bd9Sstevel@tonic-gate 
531*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < max_ncpus; i++) {
532*7c478bd9Sstevel@tonic-gate 		kmem_cpu_log_header_t *clhp = &lhp->lh_cpu[i];
533*7c478bd9Sstevel@tonic-gate 		mutex_init(&clhp->clh_lock, NULL, MUTEX_DEFAULT, NULL);
534*7c478bd9Sstevel@tonic-gate 		clhp->clh_chunk = i;
535*7c478bd9Sstevel@tonic-gate 	}
536*7c478bd9Sstevel@tonic-gate 
537*7c478bd9Sstevel@tonic-gate 	for (i = max_ncpus; i < nchunks; i++)
538*7c478bd9Sstevel@tonic-gate 		lhp->lh_free[i] = i;
539*7c478bd9Sstevel@tonic-gate 
540*7c478bd9Sstevel@tonic-gate 	lhp->lh_head = max_ncpus;
541*7c478bd9Sstevel@tonic-gate 	lhp->lh_tail = 0;
542*7c478bd9Sstevel@tonic-gate 
543*7c478bd9Sstevel@tonic-gate 	return (lhp);
544*7c478bd9Sstevel@tonic-gate }
545*7c478bd9Sstevel@tonic-gate 
546*7c478bd9Sstevel@tonic-gate static void *
547*7c478bd9Sstevel@tonic-gate kmem_log_enter(kmem_log_header_t *lhp, void *data, size_t size)
548*7c478bd9Sstevel@tonic-gate {
549*7c478bd9Sstevel@tonic-gate 	void *logspace;
550*7c478bd9Sstevel@tonic-gate 	kmem_cpu_log_header_t *clhp = &lhp->lh_cpu[CPU->cpu_seqid];
551*7c478bd9Sstevel@tonic-gate 
552*7c478bd9Sstevel@tonic-gate 	if (lhp == NULL || kmem_logging == 0 || panicstr)
553*7c478bd9Sstevel@tonic-gate 		return (NULL);
554*7c478bd9Sstevel@tonic-gate 
555*7c478bd9Sstevel@tonic-gate 	mutex_enter(&clhp->clh_lock);
556*7c478bd9Sstevel@tonic-gate 	clhp->clh_hits++;
557*7c478bd9Sstevel@tonic-gate 	if (size > clhp->clh_avail) {
558*7c478bd9Sstevel@tonic-gate 		mutex_enter(&lhp->lh_lock);
559*7c478bd9Sstevel@tonic-gate 		lhp->lh_hits++;
560*7c478bd9Sstevel@tonic-gate 		lhp->lh_free[lhp->lh_tail] = clhp->clh_chunk;
561*7c478bd9Sstevel@tonic-gate 		lhp->lh_tail = (lhp->lh_tail + 1) % lhp->lh_nchunks;
562*7c478bd9Sstevel@tonic-gate 		clhp->clh_chunk = lhp->lh_free[lhp->lh_head];
563*7c478bd9Sstevel@tonic-gate 		lhp->lh_head = (lhp->lh_head + 1) % lhp->lh_nchunks;
564*7c478bd9Sstevel@tonic-gate 		clhp->clh_current = lhp->lh_base +
565*7c478bd9Sstevel@tonic-gate 			clhp->clh_chunk * lhp->lh_chunksize;
566*7c478bd9Sstevel@tonic-gate 		clhp->clh_avail = lhp->lh_chunksize;
567*7c478bd9Sstevel@tonic-gate 		if (size > lhp->lh_chunksize)
568*7c478bd9Sstevel@tonic-gate 			size = lhp->lh_chunksize;
569*7c478bd9Sstevel@tonic-gate 		mutex_exit(&lhp->lh_lock);
570*7c478bd9Sstevel@tonic-gate 	}
571*7c478bd9Sstevel@tonic-gate 	logspace = clhp->clh_current;
572*7c478bd9Sstevel@tonic-gate 	clhp->clh_current += size;
573*7c478bd9Sstevel@tonic-gate 	clhp->clh_avail -= size;
574*7c478bd9Sstevel@tonic-gate 	bcopy(data, logspace, size);
575*7c478bd9Sstevel@tonic-gate 	mutex_exit(&clhp->clh_lock);
576*7c478bd9Sstevel@tonic-gate 	return (logspace);
577*7c478bd9Sstevel@tonic-gate }
578*7c478bd9Sstevel@tonic-gate 
579*7c478bd9Sstevel@tonic-gate #define	KMEM_AUDIT(lp, cp, bcp)						\
580*7c478bd9Sstevel@tonic-gate {									\
581*7c478bd9Sstevel@tonic-gate 	kmem_bufctl_audit_t *_bcp = (kmem_bufctl_audit_t *)(bcp);	\
582*7c478bd9Sstevel@tonic-gate 	_bcp->bc_timestamp = gethrtime();				\
583*7c478bd9Sstevel@tonic-gate 	_bcp->bc_thread = curthread;					\
584*7c478bd9Sstevel@tonic-gate 	_bcp->bc_depth = getpcstack(_bcp->bc_stack, KMEM_STACK_DEPTH);	\
585*7c478bd9Sstevel@tonic-gate 	_bcp->bc_lastlog = kmem_log_enter((lp), _bcp, sizeof (*_bcp));	\
586*7c478bd9Sstevel@tonic-gate }
587*7c478bd9Sstevel@tonic-gate 
588*7c478bd9Sstevel@tonic-gate static void
589*7c478bd9Sstevel@tonic-gate kmem_log_event(kmem_log_header_t *lp, kmem_cache_t *cp,
590*7c478bd9Sstevel@tonic-gate 	kmem_slab_t *sp, void *addr)
591*7c478bd9Sstevel@tonic-gate {
592*7c478bd9Sstevel@tonic-gate 	kmem_bufctl_audit_t bca;
593*7c478bd9Sstevel@tonic-gate 
594*7c478bd9Sstevel@tonic-gate 	bzero(&bca, sizeof (kmem_bufctl_audit_t));
595*7c478bd9Sstevel@tonic-gate 	bca.bc_addr = addr;
596*7c478bd9Sstevel@tonic-gate 	bca.bc_slab = sp;
597*7c478bd9Sstevel@tonic-gate 	bca.bc_cache = cp;
598*7c478bd9Sstevel@tonic-gate 	KMEM_AUDIT(lp, cp, &bca);
599*7c478bd9Sstevel@tonic-gate }
600*7c478bd9Sstevel@tonic-gate 
601*7c478bd9Sstevel@tonic-gate /*
602*7c478bd9Sstevel@tonic-gate  * Create a new slab for cache cp.
603*7c478bd9Sstevel@tonic-gate  */
604*7c478bd9Sstevel@tonic-gate static kmem_slab_t *
605*7c478bd9Sstevel@tonic-gate kmem_slab_create(kmem_cache_t *cp, int kmflag)
606*7c478bd9Sstevel@tonic-gate {
607*7c478bd9Sstevel@tonic-gate 	size_t slabsize = cp->cache_slabsize;
608*7c478bd9Sstevel@tonic-gate 	size_t chunksize = cp->cache_chunksize;
609*7c478bd9Sstevel@tonic-gate 	int cache_flags = cp->cache_flags;
610*7c478bd9Sstevel@tonic-gate 	size_t color, chunks;
611*7c478bd9Sstevel@tonic-gate 	char *buf, *slab;
612*7c478bd9Sstevel@tonic-gate 	kmem_slab_t *sp;
613*7c478bd9Sstevel@tonic-gate 	kmem_bufctl_t *bcp;
614*7c478bd9Sstevel@tonic-gate 	vmem_t *vmp = cp->cache_arena;
615*7c478bd9Sstevel@tonic-gate 
616*7c478bd9Sstevel@tonic-gate 	color = cp->cache_color + cp->cache_align;
617*7c478bd9Sstevel@tonic-gate 	if (color > cp->cache_maxcolor)
618*7c478bd9Sstevel@tonic-gate 		color = cp->cache_mincolor;
619*7c478bd9Sstevel@tonic-gate 	cp->cache_color = color;
620*7c478bd9Sstevel@tonic-gate 
621*7c478bd9Sstevel@tonic-gate 	slab = vmem_alloc(vmp, slabsize, kmflag & KM_VMFLAGS);
622*7c478bd9Sstevel@tonic-gate 
623*7c478bd9Sstevel@tonic-gate 	if (slab == NULL)
624*7c478bd9Sstevel@tonic-gate 		goto vmem_alloc_failure;
625*7c478bd9Sstevel@tonic-gate 
626*7c478bd9Sstevel@tonic-gate 	ASSERT(P2PHASE((uintptr_t)slab, vmp->vm_quantum) == 0);
627*7c478bd9Sstevel@tonic-gate 
628*7c478bd9Sstevel@tonic-gate 	if (!(cp->cache_cflags & KMC_NOTOUCH))
629*7c478bd9Sstevel@tonic-gate 		copy_pattern(KMEM_UNINITIALIZED_PATTERN, slab, slabsize);
630*7c478bd9Sstevel@tonic-gate 
631*7c478bd9Sstevel@tonic-gate 	if (cache_flags & KMF_HASH) {
632*7c478bd9Sstevel@tonic-gate 		if ((sp = kmem_cache_alloc(kmem_slab_cache, kmflag)) == NULL)
633*7c478bd9Sstevel@tonic-gate 			goto slab_alloc_failure;
634*7c478bd9Sstevel@tonic-gate 		chunks = (slabsize - color) / chunksize;
635*7c478bd9Sstevel@tonic-gate 	} else {
636*7c478bd9Sstevel@tonic-gate 		sp = KMEM_SLAB(cp, slab);
637*7c478bd9Sstevel@tonic-gate 		chunks = (slabsize - sizeof (kmem_slab_t) - color) / chunksize;
638*7c478bd9Sstevel@tonic-gate 	}
639*7c478bd9Sstevel@tonic-gate 
640*7c478bd9Sstevel@tonic-gate 	sp->slab_cache	= cp;
641*7c478bd9Sstevel@tonic-gate 	sp->slab_head	= NULL;
642*7c478bd9Sstevel@tonic-gate 	sp->slab_refcnt	= 0;
643*7c478bd9Sstevel@tonic-gate 	sp->slab_base	= buf = slab + color;
644*7c478bd9Sstevel@tonic-gate 	sp->slab_chunks	= chunks;
645*7c478bd9Sstevel@tonic-gate 
646*7c478bd9Sstevel@tonic-gate 	ASSERT(chunks > 0);
647*7c478bd9Sstevel@tonic-gate 	while (chunks-- != 0) {
648*7c478bd9Sstevel@tonic-gate 		if (cache_flags & KMF_HASH) {
649*7c478bd9Sstevel@tonic-gate 			bcp = kmem_cache_alloc(cp->cache_bufctl_cache, kmflag);
650*7c478bd9Sstevel@tonic-gate 			if (bcp == NULL)
651*7c478bd9Sstevel@tonic-gate 				goto bufctl_alloc_failure;
652*7c478bd9Sstevel@tonic-gate 			if (cache_flags & KMF_AUDIT) {
653*7c478bd9Sstevel@tonic-gate 				kmem_bufctl_audit_t *bcap =
654*7c478bd9Sstevel@tonic-gate 				    (kmem_bufctl_audit_t *)bcp;
655*7c478bd9Sstevel@tonic-gate 				bzero(bcap, sizeof (kmem_bufctl_audit_t));
656*7c478bd9Sstevel@tonic-gate 				bcap->bc_cache = cp;
657*7c478bd9Sstevel@tonic-gate 			}
658*7c478bd9Sstevel@tonic-gate 			bcp->bc_addr = buf;
659*7c478bd9Sstevel@tonic-gate 			bcp->bc_slab = sp;
660*7c478bd9Sstevel@tonic-gate 		} else {
661*7c478bd9Sstevel@tonic-gate 			bcp = KMEM_BUFCTL(cp, buf);
662*7c478bd9Sstevel@tonic-gate 		}
663*7c478bd9Sstevel@tonic-gate 		if (cache_flags & KMF_BUFTAG) {
664*7c478bd9Sstevel@tonic-gate 			kmem_buftag_t *btp = KMEM_BUFTAG(cp, buf);
665*7c478bd9Sstevel@tonic-gate 			btp->bt_redzone = KMEM_REDZONE_PATTERN;
666*7c478bd9Sstevel@tonic-gate 			btp->bt_bufctl = bcp;
667*7c478bd9Sstevel@tonic-gate 			btp->bt_bxstat = (intptr_t)bcp ^ KMEM_BUFTAG_FREE;
668*7c478bd9Sstevel@tonic-gate 			if (cache_flags & KMF_DEADBEEF) {
669*7c478bd9Sstevel@tonic-gate 				copy_pattern(KMEM_FREE_PATTERN, buf,
670*7c478bd9Sstevel@tonic-gate 				    cp->cache_verify);
671*7c478bd9Sstevel@tonic-gate 			}
672*7c478bd9Sstevel@tonic-gate 		}
673*7c478bd9Sstevel@tonic-gate 		bcp->bc_next = sp->slab_head;
674*7c478bd9Sstevel@tonic-gate 		sp->slab_head = bcp;
675*7c478bd9Sstevel@tonic-gate 		buf += chunksize;
676*7c478bd9Sstevel@tonic-gate 	}
677*7c478bd9Sstevel@tonic-gate 
678*7c478bd9Sstevel@tonic-gate 	kmem_log_event(kmem_slab_log, cp, sp, slab);
679*7c478bd9Sstevel@tonic-gate 
680*7c478bd9Sstevel@tonic-gate 	return (sp);
681*7c478bd9Sstevel@tonic-gate 
682*7c478bd9Sstevel@tonic-gate bufctl_alloc_failure:
683*7c478bd9Sstevel@tonic-gate 
684*7c478bd9Sstevel@tonic-gate 	while ((bcp = sp->slab_head) != NULL) {
685*7c478bd9Sstevel@tonic-gate 		sp->slab_head = bcp->bc_next;
686*7c478bd9Sstevel@tonic-gate 		kmem_cache_free(cp->cache_bufctl_cache, bcp);
687*7c478bd9Sstevel@tonic-gate 	}
688*7c478bd9Sstevel@tonic-gate 	kmem_cache_free(kmem_slab_cache, sp);
689*7c478bd9Sstevel@tonic-gate 
690*7c478bd9Sstevel@tonic-gate slab_alloc_failure:
691*7c478bd9Sstevel@tonic-gate 
692*7c478bd9Sstevel@tonic-gate 	vmem_free(vmp, slab, slabsize);
693*7c478bd9Sstevel@tonic-gate 
694*7c478bd9Sstevel@tonic-gate vmem_alloc_failure:
695*7c478bd9Sstevel@tonic-gate 
696*7c478bd9Sstevel@tonic-gate 	kmem_log_event(kmem_failure_log, cp, NULL, NULL);
697*7c478bd9Sstevel@tonic-gate 	atomic_add_64(&cp->cache_alloc_fail, 1);
698*7c478bd9Sstevel@tonic-gate 
699*7c478bd9Sstevel@tonic-gate 	return (NULL);
700*7c478bd9Sstevel@tonic-gate }
701*7c478bd9Sstevel@tonic-gate 
702*7c478bd9Sstevel@tonic-gate /*
703*7c478bd9Sstevel@tonic-gate  * Destroy a slab.
704*7c478bd9Sstevel@tonic-gate  */
705*7c478bd9Sstevel@tonic-gate static void
706*7c478bd9Sstevel@tonic-gate kmem_slab_destroy(kmem_cache_t *cp, kmem_slab_t *sp)
707*7c478bd9Sstevel@tonic-gate {
708*7c478bd9Sstevel@tonic-gate 	vmem_t *vmp = cp->cache_arena;
709*7c478bd9Sstevel@tonic-gate 	void *slab = (void *)P2ALIGN((uintptr_t)sp->slab_base, vmp->vm_quantum);
710*7c478bd9Sstevel@tonic-gate 
711*7c478bd9Sstevel@tonic-gate 	if (cp->cache_flags & KMF_HASH) {
712*7c478bd9Sstevel@tonic-gate 		kmem_bufctl_t *bcp;
713*7c478bd9Sstevel@tonic-gate 		while ((bcp = sp->slab_head) != NULL) {
714*7c478bd9Sstevel@tonic-gate 			sp->slab_head = bcp->bc_next;
715*7c478bd9Sstevel@tonic-gate 			kmem_cache_free(cp->cache_bufctl_cache, bcp);
716*7c478bd9Sstevel@tonic-gate 		}
717*7c478bd9Sstevel@tonic-gate 		kmem_cache_free(kmem_slab_cache, sp);
718*7c478bd9Sstevel@tonic-gate 	}
719*7c478bd9Sstevel@tonic-gate 	vmem_free(vmp, slab, cp->cache_slabsize);
720*7c478bd9Sstevel@tonic-gate }
721*7c478bd9Sstevel@tonic-gate 
722*7c478bd9Sstevel@tonic-gate /*
723*7c478bd9Sstevel@tonic-gate  * Allocate a raw (unconstructed) buffer from cp's slab layer.
724*7c478bd9Sstevel@tonic-gate  */
725*7c478bd9Sstevel@tonic-gate static void *
726*7c478bd9Sstevel@tonic-gate kmem_slab_alloc(kmem_cache_t *cp, int kmflag)
727*7c478bd9Sstevel@tonic-gate {
728*7c478bd9Sstevel@tonic-gate 	kmem_bufctl_t *bcp, **hash_bucket;
729*7c478bd9Sstevel@tonic-gate 	kmem_slab_t *sp;
730*7c478bd9Sstevel@tonic-gate 	void *buf;
731*7c478bd9Sstevel@tonic-gate 
732*7c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->cache_lock);
733*7c478bd9Sstevel@tonic-gate 	cp->cache_slab_alloc++;
734*7c478bd9Sstevel@tonic-gate 	sp = cp->cache_freelist;
735*7c478bd9Sstevel@tonic-gate 	ASSERT(sp->slab_cache == cp);
736*7c478bd9Sstevel@tonic-gate 	if (sp->slab_head == NULL) {
737*7c478bd9Sstevel@tonic-gate 		/*
738*7c478bd9Sstevel@tonic-gate 		 * The freelist is empty.  Create a new slab.
739*7c478bd9Sstevel@tonic-gate 		 */
740*7c478bd9Sstevel@tonic-gate 		mutex_exit(&cp->cache_lock);
741*7c478bd9Sstevel@tonic-gate 		if ((sp = kmem_slab_create(cp, kmflag)) == NULL)
742*7c478bd9Sstevel@tonic-gate 			return (NULL);
743*7c478bd9Sstevel@tonic-gate 		mutex_enter(&cp->cache_lock);
744*7c478bd9Sstevel@tonic-gate 		cp->cache_slab_create++;
745*7c478bd9Sstevel@tonic-gate 		if ((cp->cache_buftotal += sp->slab_chunks) > cp->cache_bufmax)
746*7c478bd9Sstevel@tonic-gate 			cp->cache_bufmax = cp->cache_buftotal;
747*7c478bd9Sstevel@tonic-gate 		sp->slab_next = cp->cache_freelist;
748*7c478bd9Sstevel@tonic-gate 		sp->slab_prev = cp->cache_freelist->slab_prev;
749*7c478bd9Sstevel@tonic-gate 		sp->slab_next->slab_prev = sp;
750*7c478bd9Sstevel@tonic-gate 		sp->slab_prev->slab_next = sp;
751*7c478bd9Sstevel@tonic-gate 		cp->cache_freelist = sp;
752*7c478bd9Sstevel@tonic-gate 	}
753*7c478bd9Sstevel@tonic-gate 
754*7c478bd9Sstevel@tonic-gate 	sp->slab_refcnt++;
755*7c478bd9Sstevel@tonic-gate 	ASSERT(sp->slab_refcnt <= sp->slab_chunks);
756*7c478bd9Sstevel@tonic-gate 
757*7c478bd9Sstevel@tonic-gate 	/*
758*7c478bd9Sstevel@tonic-gate 	 * If we're taking the last buffer in the slab,
759*7c478bd9Sstevel@tonic-gate 	 * remove the slab from the cache's freelist.
760*7c478bd9Sstevel@tonic-gate 	 */
761*7c478bd9Sstevel@tonic-gate 	bcp = sp->slab_head;
762*7c478bd9Sstevel@tonic-gate 	if ((sp->slab_head = bcp->bc_next) == NULL) {
763*7c478bd9Sstevel@tonic-gate 		cp->cache_freelist = sp->slab_next;
764*7c478bd9Sstevel@tonic-gate 		ASSERT(sp->slab_refcnt == sp->slab_chunks);
765*7c478bd9Sstevel@tonic-gate 	}
766*7c478bd9Sstevel@tonic-gate 
767*7c478bd9Sstevel@tonic-gate 	if (cp->cache_flags & KMF_HASH) {
768*7c478bd9Sstevel@tonic-gate 		/*
769*7c478bd9Sstevel@tonic-gate 		 * Add buffer to allocated-address hash table.
770*7c478bd9Sstevel@tonic-gate 		 */
771*7c478bd9Sstevel@tonic-gate 		buf = bcp->bc_addr;
772*7c478bd9Sstevel@tonic-gate 		hash_bucket = KMEM_HASH(cp, buf);
773*7c478bd9Sstevel@tonic-gate 		bcp->bc_next = *hash_bucket;
774*7c478bd9Sstevel@tonic-gate 		*hash_bucket = bcp;
775*7c478bd9Sstevel@tonic-gate 		if ((cp->cache_flags & (KMF_AUDIT | KMF_BUFTAG)) == KMF_AUDIT) {
776*7c478bd9Sstevel@tonic-gate 			KMEM_AUDIT(kmem_transaction_log, cp, bcp);
777*7c478bd9Sstevel@tonic-gate 		}
778*7c478bd9Sstevel@tonic-gate 	} else {
779*7c478bd9Sstevel@tonic-gate 		buf = KMEM_BUF(cp, bcp);
780*7c478bd9Sstevel@tonic-gate 	}
781*7c478bd9Sstevel@tonic-gate 
782*7c478bd9Sstevel@tonic-gate 	ASSERT(KMEM_SLAB_MEMBER(sp, buf));
783*7c478bd9Sstevel@tonic-gate 
784*7c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->cache_lock);
785*7c478bd9Sstevel@tonic-gate 
786*7c478bd9Sstevel@tonic-gate 	return (buf);
787*7c478bd9Sstevel@tonic-gate }
788*7c478bd9Sstevel@tonic-gate 
789*7c478bd9Sstevel@tonic-gate /*
790*7c478bd9Sstevel@tonic-gate  * Free a raw (unconstructed) buffer to cp's slab layer.
791*7c478bd9Sstevel@tonic-gate  */
792*7c478bd9Sstevel@tonic-gate static void
793*7c478bd9Sstevel@tonic-gate kmem_slab_free(kmem_cache_t *cp, void *buf)
794*7c478bd9Sstevel@tonic-gate {
795*7c478bd9Sstevel@tonic-gate 	kmem_slab_t *sp;
796*7c478bd9Sstevel@tonic-gate 	kmem_bufctl_t *bcp, **prev_bcpp;
797*7c478bd9Sstevel@tonic-gate 
798*7c478bd9Sstevel@tonic-gate 	ASSERT(buf != NULL);
799*7c478bd9Sstevel@tonic-gate 
800*7c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->cache_lock);
801*7c478bd9Sstevel@tonic-gate 	cp->cache_slab_free++;
802*7c478bd9Sstevel@tonic-gate 
803*7c478bd9Sstevel@tonic-gate 	if (cp->cache_flags & KMF_HASH) {
804*7c478bd9Sstevel@tonic-gate 		/*
805*7c478bd9Sstevel@tonic-gate 		 * Look up buffer in allocated-address hash table.
806*7c478bd9Sstevel@tonic-gate 		 */
807*7c478bd9Sstevel@tonic-gate 		prev_bcpp = KMEM_HASH(cp, buf);
808*7c478bd9Sstevel@tonic-gate 		while ((bcp = *prev_bcpp) != NULL) {
809*7c478bd9Sstevel@tonic-gate 			if (bcp->bc_addr == buf) {
810*7c478bd9Sstevel@tonic-gate 				*prev_bcpp = bcp->bc_next;
811*7c478bd9Sstevel@tonic-gate 				sp = bcp->bc_slab;
812*7c478bd9Sstevel@tonic-gate 				break;
813*7c478bd9Sstevel@tonic-gate 			}
814*7c478bd9Sstevel@tonic-gate 			cp->cache_lookup_depth++;
815*7c478bd9Sstevel@tonic-gate 			prev_bcpp = &bcp->bc_next;
816*7c478bd9Sstevel@tonic-gate 		}
817*7c478bd9Sstevel@tonic-gate 	} else {
818*7c478bd9Sstevel@tonic-gate 		bcp = KMEM_BUFCTL(cp, buf);
819*7c478bd9Sstevel@tonic-gate 		sp = KMEM_SLAB(cp, buf);
820*7c478bd9Sstevel@tonic-gate 	}
821*7c478bd9Sstevel@tonic-gate 
822*7c478bd9Sstevel@tonic-gate 	if (bcp == NULL || sp->slab_cache != cp || !KMEM_SLAB_MEMBER(sp, buf)) {
823*7c478bd9Sstevel@tonic-gate 		mutex_exit(&cp->cache_lock);
824*7c478bd9Sstevel@tonic-gate 		kmem_error(KMERR_BADADDR, cp, buf);
825*7c478bd9Sstevel@tonic-gate 		return;
826*7c478bd9Sstevel@tonic-gate 	}
827*7c478bd9Sstevel@tonic-gate 
828*7c478bd9Sstevel@tonic-gate 	if ((cp->cache_flags & (KMF_AUDIT | KMF_BUFTAG)) == KMF_AUDIT) {
829*7c478bd9Sstevel@tonic-gate 		if (cp->cache_flags & KMF_CONTENTS)
830*7c478bd9Sstevel@tonic-gate 			((kmem_bufctl_audit_t *)bcp)->bc_contents =
831*7c478bd9Sstevel@tonic-gate 			    kmem_log_enter(kmem_content_log, buf,
832*7c478bd9Sstevel@tonic-gate 				cp->cache_contents);
833*7c478bd9Sstevel@tonic-gate 		KMEM_AUDIT(kmem_transaction_log, cp, bcp);
834*7c478bd9Sstevel@tonic-gate 	}
835*7c478bd9Sstevel@tonic-gate 
836*7c478bd9Sstevel@tonic-gate 	/*
837*7c478bd9Sstevel@tonic-gate 	 * If this slab isn't currently on the freelist, put it there.
838*7c478bd9Sstevel@tonic-gate 	 */
839*7c478bd9Sstevel@tonic-gate 	if (sp->slab_head == NULL) {
840*7c478bd9Sstevel@tonic-gate 		ASSERT(sp->slab_refcnt == sp->slab_chunks);
841*7c478bd9Sstevel@tonic-gate 		ASSERT(cp->cache_freelist != sp);
842*7c478bd9Sstevel@tonic-gate 		sp->slab_next->slab_prev = sp->slab_prev;
843*7c478bd9Sstevel@tonic-gate 		sp->slab_prev->slab_next = sp->slab_next;
844*7c478bd9Sstevel@tonic-gate 		sp->slab_next = cp->cache_freelist;
845*7c478bd9Sstevel@tonic-gate 		sp->slab_prev = cp->cache_freelist->slab_prev;
846*7c478bd9Sstevel@tonic-gate 		sp->slab_next->slab_prev = sp;
847*7c478bd9Sstevel@tonic-gate 		sp->slab_prev->slab_next = sp;
848*7c478bd9Sstevel@tonic-gate 		cp->cache_freelist = sp;
849*7c478bd9Sstevel@tonic-gate 	}
850*7c478bd9Sstevel@tonic-gate 
851*7c478bd9Sstevel@tonic-gate 	bcp->bc_next = sp->slab_head;
852*7c478bd9Sstevel@tonic-gate 	sp->slab_head = bcp;
853*7c478bd9Sstevel@tonic-gate 
854*7c478bd9Sstevel@tonic-gate 	ASSERT(sp->slab_refcnt >= 1);
855*7c478bd9Sstevel@tonic-gate 	if (--sp->slab_refcnt == 0) {
856*7c478bd9Sstevel@tonic-gate 		/*
857*7c478bd9Sstevel@tonic-gate 		 * There are no outstanding allocations from this slab,
858*7c478bd9Sstevel@tonic-gate 		 * so we can reclaim the memory.
859*7c478bd9Sstevel@tonic-gate 		 */
860*7c478bd9Sstevel@tonic-gate 		sp->slab_next->slab_prev = sp->slab_prev;
861*7c478bd9Sstevel@tonic-gate 		sp->slab_prev->slab_next = sp->slab_next;
862*7c478bd9Sstevel@tonic-gate 		if (sp == cp->cache_freelist)
863*7c478bd9Sstevel@tonic-gate 			cp->cache_freelist = sp->slab_next;
864*7c478bd9Sstevel@tonic-gate 		cp->cache_slab_destroy++;
865*7c478bd9Sstevel@tonic-gate 		cp->cache_buftotal -= sp->slab_chunks;
866*7c478bd9Sstevel@tonic-gate 		mutex_exit(&cp->cache_lock);
867*7c478bd9Sstevel@tonic-gate 		kmem_slab_destroy(cp, sp);
868*7c478bd9Sstevel@tonic-gate 		return;
869*7c478bd9Sstevel@tonic-gate 	}
870*7c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->cache_lock);
871*7c478bd9Sstevel@tonic-gate }
872*7c478bd9Sstevel@tonic-gate 
873*7c478bd9Sstevel@tonic-gate static int
874*7c478bd9Sstevel@tonic-gate kmem_cache_alloc_debug(kmem_cache_t *cp, void *buf, int kmflag, int construct,
875*7c478bd9Sstevel@tonic-gate     caddr_t caller)
876*7c478bd9Sstevel@tonic-gate {
877*7c478bd9Sstevel@tonic-gate 	kmem_buftag_t *btp = KMEM_BUFTAG(cp, buf);
878*7c478bd9Sstevel@tonic-gate 	kmem_bufctl_audit_t *bcp = (kmem_bufctl_audit_t *)btp->bt_bufctl;
879*7c478bd9Sstevel@tonic-gate 	uint32_t mtbf;
880*7c478bd9Sstevel@tonic-gate 
881*7c478bd9Sstevel@tonic-gate 	if (btp->bt_bxstat != ((intptr_t)bcp ^ KMEM_BUFTAG_FREE)) {
882*7c478bd9Sstevel@tonic-gate 		kmem_error(KMERR_BADBUFTAG, cp, buf);
883*7c478bd9Sstevel@tonic-gate 		return (-1);
884*7c478bd9Sstevel@tonic-gate 	}
885*7c478bd9Sstevel@tonic-gate 
886*7c478bd9Sstevel@tonic-gate 	btp->bt_bxstat = (intptr_t)bcp ^ KMEM_BUFTAG_ALLOC;
887*7c478bd9Sstevel@tonic-gate 
888*7c478bd9Sstevel@tonic-gate 	if ((cp->cache_flags & KMF_HASH) && bcp->bc_addr != buf) {
889*7c478bd9Sstevel@tonic-gate 		kmem_error(KMERR_BADBUFCTL, cp, buf);
890*7c478bd9Sstevel@tonic-gate 		return (-1);
891*7c478bd9Sstevel@tonic-gate 	}
892*7c478bd9Sstevel@tonic-gate 
893*7c478bd9Sstevel@tonic-gate 	if (cp->cache_flags & KMF_DEADBEEF) {
894*7c478bd9Sstevel@tonic-gate 		if (!construct && (cp->cache_flags & KMF_LITE)) {
895*7c478bd9Sstevel@tonic-gate 			if (*(uint64_t *)buf != KMEM_FREE_PATTERN) {
896*7c478bd9Sstevel@tonic-gate 				kmem_error(KMERR_MODIFIED, cp, buf);
897*7c478bd9Sstevel@tonic-gate 				return (-1);
898*7c478bd9Sstevel@tonic-gate 			}
899*7c478bd9Sstevel@tonic-gate 			if (cp->cache_constructor != NULL)
900*7c478bd9Sstevel@tonic-gate 				*(uint64_t *)buf = btp->bt_redzone;
901*7c478bd9Sstevel@tonic-gate 			else
902*7c478bd9Sstevel@tonic-gate 				*(uint64_t *)buf = KMEM_UNINITIALIZED_PATTERN;
903*7c478bd9Sstevel@tonic-gate 		} else {
904*7c478bd9Sstevel@tonic-gate 			construct = 1;
905*7c478bd9Sstevel@tonic-gate 			if (verify_and_copy_pattern(KMEM_FREE_PATTERN,
906*7c478bd9Sstevel@tonic-gate 			    KMEM_UNINITIALIZED_PATTERN, buf,
907*7c478bd9Sstevel@tonic-gate 			    cp->cache_verify)) {
908*7c478bd9Sstevel@tonic-gate 				kmem_error(KMERR_MODIFIED, cp, buf);
909*7c478bd9Sstevel@tonic-gate 				return (-1);
910*7c478bd9Sstevel@tonic-gate 			}
911*7c478bd9Sstevel@tonic-gate 		}
912*7c478bd9Sstevel@tonic-gate 	}
913*7c478bd9Sstevel@tonic-gate 	btp->bt_redzone = KMEM_REDZONE_PATTERN;
914*7c478bd9Sstevel@tonic-gate 
915*7c478bd9Sstevel@tonic-gate 	if ((mtbf = kmem_mtbf | cp->cache_mtbf) != 0 &&
916*7c478bd9Sstevel@tonic-gate 	    gethrtime() % mtbf == 0 &&
917*7c478bd9Sstevel@tonic-gate 	    (kmflag & (KM_NOSLEEP | KM_PANIC)) == KM_NOSLEEP) {
918*7c478bd9Sstevel@tonic-gate 		kmem_log_event(kmem_failure_log, cp, NULL, NULL);
919*7c478bd9Sstevel@tonic-gate 		if (!construct && cp->cache_destructor != NULL)
920*7c478bd9Sstevel@tonic-gate 			cp->cache_destructor(buf, cp->cache_private);
921*7c478bd9Sstevel@tonic-gate 	} else {
922*7c478bd9Sstevel@tonic-gate 		mtbf = 0;
923*7c478bd9Sstevel@tonic-gate 	}
924*7c478bd9Sstevel@tonic-gate 
925*7c478bd9Sstevel@tonic-gate 	if (mtbf || (construct && cp->cache_constructor != NULL &&
926*7c478bd9Sstevel@tonic-gate 	    cp->cache_constructor(buf, cp->cache_private, kmflag) != 0)) {
927*7c478bd9Sstevel@tonic-gate 		atomic_add_64(&cp->cache_alloc_fail, 1);
928*7c478bd9Sstevel@tonic-gate 		btp->bt_bxstat = (intptr_t)bcp ^ KMEM_BUFTAG_FREE;
929*7c478bd9Sstevel@tonic-gate 		if (cp->cache_flags & KMF_DEADBEEF)
930*7c478bd9Sstevel@tonic-gate 			copy_pattern(KMEM_FREE_PATTERN, buf, cp->cache_verify);
931*7c478bd9Sstevel@tonic-gate 		kmem_slab_free(cp, buf);
932*7c478bd9Sstevel@tonic-gate 		return (-1);
933*7c478bd9Sstevel@tonic-gate 	}
934*7c478bd9Sstevel@tonic-gate 
935*7c478bd9Sstevel@tonic-gate 	if (cp->cache_flags & KMF_AUDIT) {
936*7c478bd9Sstevel@tonic-gate 		KMEM_AUDIT(kmem_transaction_log, cp, bcp);
937*7c478bd9Sstevel@tonic-gate 	}
938*7c478bd9Sstevel@tonic-gate 
939*7c478bd9Sstevel@tonic-gate 	if ((cp->cache_flags & KMF_LITE) &&
940*7c478bd9Sstevel@tonic-gate 	    !(cp->cache_cflags & KMC_KMEM_ALLOC)) {
941*7c478bd9Sstevel@tonic-gate 		KMEM_BUFTAG_LITE_ENTER(btp, kmem_lite_count, caller);
942*7c478bd9Sstevel@tonic-gate 	}
943*7c478bd9Sstevel@tonic-gate 
944*7c478bd9Sstevel@tonic-gate 	return (0);
945*7c478bd9Sstevel@tonic-gate }
946*7c478bd9Sstevel@tonic-gate 
947*7c478bd9Sstevel@tonic-gate static int
948*7c478bd9Sstevel@tonic-gate kmem_cache_free_debug(kmem_cache_t *cp, void *buf, caddr_t caller)
949*7c478bd9Sstevel@tonic-gate {
950*7c478bd9Sstevel@tonic-gate 	kmem_buftag_t *btp = KMEM_BUFTAG(cp, buf);
951*7c478bd9Sstevel@tonic-gate 	kmem_bufctl_audit_t *bcp = (kmem_bufctl_audit_t *)btp->bt_bufctl;
952*7c478bd9Sstevel@tonic-gate 	kmem_slab_t *sp;
953*7c478bd9Sstevel@tonic-gate 
954*7c478bd9Sstevel@tonic-gate 	if (btp->bt_bxstat != ((intptr_t)bcp ^ KMEM_BUFTAG_ALLOC)) {
955*7c478bd9Sstevel@tonic-gate 		if (btp->bt_bxstat == ((intptr_t)bcp ^ KMEM_BUFTAG_FREE)) {
956*7c478bd9Sstevel@tonic-gate 			kmem_error(KMERR_DUPFREE, cp, buf);
957*7c478bd9Sstevel@tonic-gate 			return (-1);
958*7c478bd9Sstevel@tonic-gate 		}
959*7c478bd9Sstevel@tonic-gate 		sp = kmem_findslab(cp, buf);
960*7c478bd9Sstevel@tonic-gate 		if (sp == NULL || sp->slab_cache != cp)
961*7c478bd9Sstevel@tonic-gate 			kmem_error(KMERR_BADADDR, cp, buf);
962*7c478bd9Sstevel@tonic-gate 		else
963*7c478bd9Sstevel@tonic-gate 			kmem_error(KMERR_REDZONE, cp, buf);
964*7c478bd9Sstevel@tonic-gate 		return (-1);
965*7c478bd9Sstevel@tonic-gate 	}
966*7c478bd9Sstevel@tonic-gate 
967*7c478bd9Sstevel@tonic-gate 	btp->bt_bxstat = (intptr_t)bcp ^ KMEM_BUFTAG_FREE;
968*7c478bd9Sstevel@tonic-gate 
969*7c478bd9Sstevel@tonic-gate 	if ((cp->cache_flags & KMF_HASH) && bcp->bc_addr != buf) {
970*7c478bd9Sstevel@tonic-gate 		kmem_error(KMERR_BADBUFCTL, cp, buf);
971*7c478bd9Sstevel@tonic-gate 		return (-1);
972*7c478bd9Sstevel@tonic-gate 	}
973*7c478bd9Sstevel@tonic-gate 
974*7c478bd9Sstevel@tonic-gate 	if (btp->bt_redzone != KMEM_REDZONE_PATTERN) {
975*7c478bd9Sstevel@tonic-gate 		kmem_error(KMERR_REDZONE, cp, buf);
976*7c478bd9Sstevel@tonic-gate 		return (-1);
977*7c478bd9Sstevel@tonic-gate 	}
978*7c478bd9Sstevel@tonic-gate 
979*7c478bd9Sstevel@tonic-gate 	if (cp->cache_flags & KMF_AUDIT) {
980*7c478bd9Sstevel@tonic-gate 		if (cp->cache_flags & KMF_CONTENTS)
981*7c478bd9Sstevel@tonic-gate 			bcp->bc_contents = kmem_log_enter(kmem_content_log,
982*7c478bd9Sstevel@tonic-gate 			    buf, cp->cache_contents);
983*7c478bd9Sstevel@tonic-gate 		KMEM_AUDIT(kmem_transaction_log, cp, bcp);
984*7c478bd9Sstevel@tonic-gate 	}
985*7c478bd9Sstevel@tonic-gate 
986*7c478bd9Sstevel@tonic-gate 	if ((cp->cache_flags & KMF_LITE) &&
987*7c478bd9Sstevel@tonic-gate 	    !(cp->cache_cflags & KMC_KMEM_ALLOC)) {
988*7c478bd9Sstevel@tonic-gate 		KMEM_BUFTAG_LITE_ENTER(btp, kmem_lite_count, caller);
989*7c478bd9Sstevel@tonic-gate 	}
990*7c478bd9Sstevel@tonic-gate 
991*7c478bd9Sstevel@tonic-gate 	if (cp->cache_flags & KMF_DEADBEEF) {
992*7c478bd9Sstevel@tonic-gate 		if (cp->cache_flags & KMF_LITE)
993*7c478bd9Sstevel@tonic-gate 			btp->bt_redzone = *(uint64_t *)buf;
994*7c478bd9Sstevel@tonic-gate 		else if (cp->cache_destructor != NULL)
995*7c478bd9Sstevel@tonic-gate 			cp->cache_destructor(buf, cp->cache_private);
996*7c478bd9Sstevel@tonic-gate 
997*7c478bd9Sstevel@tonic-gate 		copy_pattern(KMEM_FREE_PATTERN, buf, cp->cache_verify);
998*7c478bd9Sstevel@tonic-gate 	}
999*7c478bd9Sstevel@tonic-gate 
1000*7c478bd9Sstevel@tonic-gate 	return (0);
1001*7c478bd9Sstevel@tonic-gate }
1002*7c478bd9Sstevel@tonic-gate 
1003*7c478bd9Sstevel@tonic-gate /*
1004*7c478bd9Sstevel@tonic-gate  * Free each object in magazine mp to cp's slab layer, and free mp itself.
1005*7c478bd9Sstevel@tonic-gate  */
1006*7c478bd9Sstevel@tonic-gate static void
1007*7c478bd9Sstevel@tonic-gate kmem_magazine_destroy(kmem_cache_t *cp, kmem_magazine_t *mp, int nrounds)
1008*7c478bd9Sstevel@tonic-gate {
1009*7c478bd9Sstevel@tonic-gate 	int round;
1010*7c478bd9Sstevel@tonic-gate 
1011*7c478bd9Sstevel@tonic-gate 	ASSERT(cp->cache_next == NULL || taskq_member(kmem_taskq, curthread));
1012*7c478bd9Sstevel@tonic-gate 
1013*7c478bd9Sstevel@tonic-gate 	for (round = 0; round < nrounds; round++) {
1014*7c478bd9Sstevel@tonic-gate 		void *buf = mp->mag_round[round];
1015*7c478bd9Sstevel@tonic-gate 
1016*7c478bd9Sstevel@tonic-gate 		if (cp->cache_flags & KMF_DEADBEEF) {
1017*7c478bd9Sstevel@tonic-gate 			if (verify_pattern(KMEM_FREE_PATTERN, buf,
1018*7c478bd9Sstevel@tonic-gate 			    cp->cache_verify) != NULL) {
1019*7c478bd9Sstevel@tonic-gate 				kmem_error(KMERR_MODIFIED, cp, buf);
1020*7c478bd9Sstevel@tonic-gate 				continue;
1021*7c478bd9Sstevel@tonic-gate 			}
1022*7c478bd9Sstevel@tonic-gate 			if ((cp->cache_flags & KMF_LITE) &&
1023*7c478bd9Sstevel@tonic-gate 			    cp->cache_destructor != NULL) {
1024*7c478bd9Sstevel@tonic-gate 				kmem_buftag_t *btp = KMEM_BUFTAG(cp, buf);
1025*7c478bd9Sstevel@tonic-gate 				*(uint64_t *)buf = btp->bt_redzone;
1026*7c478bd9Sstevel@tonic-gate 				cp->cache_destructor(buf, cp->cache_private);
1027*7c478bd9Sstevel@tonic-gate 				*(uint64_t *)buf = KMEM_FREE_PATTERN;
1028*7c478bd9Sstevel@tonic-gate 			}
1029*7c478bd9Sstevel@tonic-gate 		} else if (cp->cache_destructor != NULL) {
1030*7c478bd9Sstevel@tonic-gate 			cp->cache_destructor(buf, cp->cache_private);
1031*7c478bd9Sstevel@tonic-gate 		}
1032*7c478bd9Sstevel@tonic-gate 
1033*7c478bd9Sstevel@tonic-gate 		kmem_slab_free(cp, buf);
1034*7c478bd9Sstevel@tonic-gate 	}
1035*7c478bd9Sstevel@tonic-gate 	ASSERT(KMEM_MAGAZINE_VALID(cp, mp));
1036*7c478bd9Sstevel@tonic-gate 	kmem_cache_free(cp->cache_magtype->mt_cache, mp);
1037*7c478bd9Sstevel@tonic-gate }
1038*7c478bd9Sstevel@tonic-gate 
1039*7c478bd9Sstevel@tonic-gate /*
1040*7c478bd9Sstevel@tonic-gate  * Allocate a magazine from the depot.
1041*7c478bd9Sstevel@tonic-gate  */
1042*7c478bd9Sstevel@tonic-gate static kmem_magazine_t *
1043*7c478bd9Sstevel@tonic-gate kmem_depot_alloc(kmem_cache_t *cp, kmem_maglist_t *mlp)
1044*7c478bd9Sstevel@tonic-gate {
1045*7c478bd9Sstevel@tonic-gate 	kmem_magazine_t *mp;
1046*7c478bd9Sstevel@tonic-gate 
1047*7c478bd9Sstevel@tonic-gate 	/*
1048*7c478bd9Sstevel@tonic-gate 	 * If we can't get the depot lock without contention,
1049*7c478bd9Sstevel@tonic-gate 	 * update our contention count.  We use the depot
1050*7c478bd9Sstevel@tonic-gate 	 * contention rate to determine whether we need to
1051*7c478bd9Sstevel@tonic-gate 	 * increase the magazine size for better scalability.
1052*7c478bd9Sstevel@tonic-gate 	 */
1053*7c478bd9Sstevel@tonic-gate 	if (!mutex_tryenter(&cp->cache_depot_lock)) {
1054*7c478bd9Sstevel@tonic-gate 		mutex_enter(&cp->cache_depot_lock);
1055*7c478bd9Sstevel@tonic-gate 		cp->cache_depot_contention++;
1056*7c478bd9Sstevel@tonic-gate 	}
1057*7c478bd9Sstevel@tonic-gate 
1058*7c478bd9Sstevel@tonic-gate 	if ((mp = mlp->ml_list) != NULL) {
1059*7c478bd9Sstevel@tonic-gate 		ASSERT(KMEM_MAGAZINE_VALID(cp, mp));
1060*7c478bd9Sstevel@tonic-gate 		mlp->ml_list = mp->mag_next;
1061*7c478bd9Sstevel@tonic-gate 		if (--mlp->ml_total < mlp->ml_min)
1062*7c478bd9Sstevel@tonic-gate 			mlp->ml_min = mlp->ml_total;
1063*7c478bd9Sstevel@tonic-gate 		mlp->ml_alloc++;
1064*7c478bd9Sstevel@tonic-gate 	}
1065*7c478bd9Sstevel@tonic-gate 
1066*7c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->cache_depot_lock);
1067*7c478bd9Sstevel@tonic-gate 
1068*7c478bd9Sstevel@tonic-gate 	return (mp);
1069*7c478bd9Sstevel@tonic-gate }
1070*7c478bd9Sstevel@tonic-gate 
1071*7c478bd9Sstevel@tonic-gate /*
1072*7c478bd9Sstevel@tonic-gate  * Free a magazine to the depot.
1073*7c478bd9Sstevel@tonic-gate  */
1074*7c478bd9Sstevel@tonic-gate static void
1075*7c478bd9Sstevel@tonic-gate kmem_depot_free(kmem_cache_t *cp, kmem_maglist_t *mlp, kmem_magazine_t *mp)
1076*7c478bd9Sstevel@tonic-gate {
1077*7c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->cache_depot_lock);
1078*7c478bd9Sstevel@tonic-gate 	ASSERT(KMEM_MAGAZINE_VALID(cp, mp));
1079*7c478bd9Sstevel@tonic-gate 	mp->mag_next = mlp->ml_list;
1080*7c478bd9Sstevel@tonic-gate 	mlp->ml_list = mp;
1081*7c478bd9Sstevel@tonic-gate 	mlp->ml_total++;
1082*7c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->cache_depot_lock);
1083*7c478bd9Sstevel@tonic-gate }
1084*7c478bd9Sstevel@tonic-gate 
1085*7c478bd9Sstevel@tonic-gate /*
1086*7c478bd9Sstevel@tonic-gate  * Update the working set statistics for cp's depot.
1087*7c478bd9Sstevel@tonic-gate  */
1088*7c478bd9Sstevel@tonic-gate static void
1089*7c478bd9Sstevel@tonic-gate kmem_depot_ws_update(kmem_cache_t *cp)
1090*7c478bd9Sstevel@tonic-gate {
1091*7c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->cache_depot_lock);
1092*7c478bd9Sstevel@tonic-gate 	cp->cache_full.ml_reaplimit = cp->cache_full.ml_min;
1093*7c478bd9Sstevel@tonic-gate 	cp->cache_full.ml_min = cp->cache_full.ml_total;
1094*7c478bd9Sstevel@tonic-gate 	cp->cache_empty.ml_reaplimit = cp->cache_empty.ml_min;
1095*7c478bd9Sstevel@tonic-gate 	cp->cache_empty.ml_min = cp->cache_empty.ml_total;
1096*7c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->cache_depot_lock);
1097*7c478bd9Sstevel@tonic-gate }
1098*7c478bd9Sstevel@tonic-gate 
1099*7c478bd9Sstevel@tonic-gate /*
1100*7c478bd9Sstevel@tonic-gate  * Reap all magazines that have fallen out of the depot's working set.
1101*7c478bd9Sstevel@tonic-gate  */
1102*7c478bd9Sstevel@tonic-gate static void
1103*7c478bd9Sstevel@tonic-gate kmem_depot_ws_reap(kmem_cache_t *cp)
1104*7c478bd9Sstevel@tonic-gate {
1105*7c478bd9Sstevel@tonic-gate 	long reap;
1106*7c478bd9Sstevel@tonic-gate 	kmem_magazine_t *mp;
1107*7c478bd9Sstevel@tonic-gate 
1108*7c478bd9Sstevel@tonic-gate 	ASSERT(cp->cache_next == NULL || taskq_member(kmem_taskq, curthread));
1109*7c478bd9Sstevel@tonic-gate 
1110*7c478bd9Sstevel@tonic-gate 	reap = MIN(cp->cache_full.ml_reaplimit, cp->cache_full.ml_min);
1111*7c478bd9Sstevel@tonic-gate 	while (reap-- && (mp = kmem_depot_alloc(cp, &cp->cache_full)) != NULL)
1112*7c478bd9Sstevel@tonic-gate 		kmem_magazine_destroy(cp, mp, cp->cache_magtype->mt_magsize);
1113*7c478bd9Sstevel@tonic-gate 
1114*7c478bd9Sstevel@tonic-gate 	reap = MIN(cp->cache_empty.ml_reaplimit, cp->cache_empty.ml_min);
1115*7c478bd9Sstevel@tonic-gate 	while (reap-- && (mp = kmem_depot_alloc(cp, &cp->cache_empty)) != NULL)
1116*7c478bd9Sstevel@tonic-gate 		kmem_magazine_destroy(cp, mp, 0);
1117*7c478bd9Sstevel@tonic-gate }
1118*7c478bd9Sstevel@tonic-gate 
1119*7c478bd9Sstevel@tonic-gate static void
1120*7c478bd9Sstevel@tonic-gate kmem_cpu_reload(kmem_cpu_cache_t *ccp, kmem_magazine_t *mp, int rounds)
1121*7c478bd9Sstevel@tonic-gate {
1122*7c478bd9Sstevel@tonic-gate 	ASSERT((ccp->cc_loaded == NULL && ccp->cc_rounds == -1) ||
1123*7c478bd9Sstevel@tonic-gate 	    (ccp->cc_loaded && ccp->cc_rounds + rounds == ccp->cc_magsize));
1124*7c478bd9Sstevel@tonic-gate 	ASSERT(ccp->cc_magsize > 0);
1125*7c478bd9Sstevel@tonic-gate 
1126*7c478bd9Sstevel@tonic-gate 	ccp->cc_ploaded = ccp->cc_loaded;
1127*7c478bd9Sstevel@tonic-gate 	ccp->cc_prounds = ccp->cc_rounds;
1128*7c478bd9Sstevel@tonic-gate 	ccp->cc_loaded = mp;
1129*7c478bd9Sstevel@tonic-gate 	ccp->cc_rounds = rounds;
1130*7c478bd9Sstevel@tonic-gate }
1131*7c478bd9Sstevel@tonic-gate 
1132*7c478bd9Sstevel@tonic-gate /*
1133*7c478bd9Sstevel@tonic-gate  * Allocate a constructed object from cache cp.
1134*7c478bd9Sstevel@tonic-gate  */
1135*7c478bd9Sstevel@tonic-gate void *
1136*7c478bd9Sstevel@tonic-gate kmem_cache_alloc(kmem_cache_t *cp, int kmflag)
1137*7c478bd9Sstevel@tonic-gate {
1138*7c478bd9Sstevel@tonic-gate 	kmem_cpu_cache_t *ccp = KMEM_CPU_CACHE(cp);
1139*7c478bd9Sstevel@tonic-gate 	kmem_magazine_t *fmp;
1140*7c478bd9Sstevel@tonic-gate 	void *buf;
1141*7c478bd9Sstevel@tonic-gate 
1142*7c478bd9Sstevel@tonic-gate 	mutex_enter(&ccp->cc_lock);
1143*7c478bd9Sstevel@tonic-gate 	for (;;) {
1144*7c478bd9Sstevel@tonic-gate 		/*
1145*7c478bd9Sstevel@tonic-gate 		 * If there's an object available in the current CPU's
1146*7c478bd9Sstevel@tonic-gate 		 * loaded magazine, just take it and return.
1147*7c478bd9Sstevel@tonic-gate 		 */
1148*7c478bd9Sstevel@tonic-gate 		if (ccp->cc_rounds > 0) {
1149*7c478bd9Sstevel@tonic-gate 			buf = ccp->cc_loaded->mag_round[--ccp->cc_rounds];
1150*7c478bd9Sstevel@tonic-gate 			ccp->cc_alloc++;
1151*7c478bd9Sstevel@tonic-gate 			mutex_exit(&ccp->cc_lock);
1152*7c478bd9Sstevel@tonic-gate 			if ((ccp->cc_flags & KMF_BUFTAG) &&
1153*7c478bd9Sstevel@tonic-gate 			    kmem_cache_alloc_debug(cp, buf, kmflag, 0,
1154*7c478bd9Sstevel@tonic-gate 			    caller()) == -1) {
1155*7c478bd9Sstevel@tonic-gate 				if (kmflag & KM_NOSLEEP)
1156*7c478bd9Sstevel@tonic-gate 					return (NULL);
1157*7c478bd9Sstevel@tonic-gate 				mutex_enter(&ccp->cc_lock);
1158*7c478bd9Sstevel@tonic-gate 				continue;
1159*7c478bd9Sstevel@tonic-gate 			}
1160*7c478bd9Sstevel@tonic-gate 			return (buf);
1161*7c478bd9Sstevel@tonic-gate 		}
1162*7c478bd9Sstevel@tonic-gate 
1163*7c478bd9Sstevel@tonic-gate 		/*
1164*7c478bd9Sstevel@tonic-gate 		 * The loaded magazine is empty.  If the previously loaded
1165*7c478bd9Sstevel@tonic-gate 		 * magazine was full, exchange them and try again.
1166*7c478bd9Sstevel@tonic-gate 		 */
1167*7c478bd9Sstevel@tonic-gate 		if (ccp->cc_prounds > 0) {
1168*7c478bd9Sstevel@tonic-gate 			kmem_cpu_reload(ccp, ccp->cc_ploaded, ccp->cc_prounds);
1169*7c478bd9Sstevel@tonic-gate 			continue;
1170*7c478bd9Sstevel@tonic-gate 		}
1171*7c478bd9Sstevel@tonic-gate 
1172*7c478bd9Sstevel@tonic-gate 		/*
1173*7c478bd9Sstevel@tonic-gate 		 * If the magazine layer is disabled, break out now.
1174*7c478bd9Sstevel@tonic-gate 		 */
1175*7c478bd9Sstevel@tonic-gate 		if (ccp->cc_magsize == 0)
1176*7c478bd9Sstevel@tonic-gate 			break;
1177*7c478bd9Sstevel@tonic-gate 
1178*7c478bd9Sstevel@tonic-gate 		/*
1179*7c478bd9Sstevel@tonic-gate 		 * Try to get a full magazine from the depot.
1180*7c478bd9Sstevel@tonic-gate 		 */
1181*7c478bd9Sstevel@tonic-gate 		fmp = kmem_depot_alloc(cp, &cp->cache_full);
1182*7c478bd9Sstevel@tonic-gate 		if (fmp != NULL) {
1183*7c478bd9Sstevel@tonic-gate 			if (ccp->cc_ploaded != NULL)
1184*7c478bd9Sstevel@tonic-gate 				kmem_depot_free(cp, &cp->cache_empty,
1185*7c478bd9Sstevel@tonic-gate 				    ccp->cc_ploaded);
1186*7c478bd9Sstevel@tonic-gate 			kmem_cpu_reload(ccp, fmp, ccp->cc_magsize);
1187*7c478bd9Sstevel@tonic-gate 			continue;
1188*7c478bd9Sstevel@tonic-gate 		}
1189*7c478bd9Sstevel@tonic-gate 
1190*7c478bd9Sstevel@tonic-gate 		/*
1191*7c478bd9Sstevel@tonic-gate 		 * There are no full magazines in the depot,
1192*7c478bd9Sstevel@tonic-gate 		 * so fall through to the slab layer.
1193*7c478bd9Sstevel@tonic-gate 		 */
1194*7c478bd9Sstevel@tonic-gate 		break;
1195*7c478bd9Sstevel@tonic-gate 	}
1196*7c478bd9Sstevel@tonic-gate 	mutex_exit(&ccp->cc_lock);
1197*7c478bd9Sstevel@tonic-gate 
1198*7c478bd9Sstevel@tonic-gate 	/*
1199*7c478bd9Sstevel@tonic-gate 	 * We couldn't allocate a constructed object from the magazine layer,
1200*7c478bd9Sstevel@tonic-gate 	 * so get a raw buffer from the slab layer and apply its constructor.
1201*7c478bd9Sstevel@tonic-gate 	 */
1202*7c478bd9Sstevel@tonic-gate 	buf = kmem_slab_alloc(cp, kmflag);
1203*7c478bd9Sstevel@tonic-gate 
1204*7c478bd9Sstevel@tonic-gate 	if (buf == NULL)
1205*7c478bd9Sstevel@tonic-gate 		return (NULL);
1206*7c478bd9Sstevel@tonic-gate 
1207*7c478bd9Sstevel@tonic-gate 	if (cp->cache_flags & KMF_BUFTAG) {
1208*7c478bd9Sstevel@tonic-gate 		/*
1209*7c478bd9Sstevel@tonic-gate 		 * Make kmem_cache_alloc_debug() apply the constructor for us.
1210*7c478bd9Sstevel@tonic-gate 		 */
1211*7c478bd9Sstevel@tonic-gate 		if (kmem_cache_alloc_debug(cp, buf, kmflag, 1,
1212*7c478bd9Sstevel@tonic-gate 		    caller()) == -1) {
1213*7c478bd9Sstevel@tonic-gate 			if (kmflag & KM_NOSLEEP)
1214*7c478bd9Sstevel@tonic-gate 				return (NULL);
1215*7c478bd9Sstevel@tonic-gate 			/*
1216*7c478bd9Sstevel@tonic-gate 			 * kmem_cache_alloc_debug() detected corruption
1217*7c478bd9Sstevel@tonic-gate 			 * but didn't panic (kmem_panic <= 0).  Try again.
1218*7c478bd9Sstevel@tonic-gate 			 */
1219*7c478bd9Sstevel@tonic-gate 			return (kmem_cache_alloc(cp, kmflag));
1220*7c478bd9Sstevel@tonic-gate 		}
1221*7c478bd9Sstevel@tonic-gate 		return (buf);
1222*7c478bd9Sstevel@tonic-gate 	}
1223*7c478bd9Sstevel@tonic-gate 
1224*7c478bd9Sstevel@tonic-gate 	if (cp->cache_constructor != NULL &&
1225*7c478bd9Sstevel@tonic-gate 	    cp->cache_constructor(buf, cp->cache_private, kmflag) != 0) {
1226*7c478bd9Sstevel@tonic-gate 		atomic_add_64(&cp->cache_alloc_fail, 1);
1227*7c478bd9Sstevel@tonic-gate 		kmem_slab_free(cp, buf);
1228*7c478bd9Sstevel@tonic-gate 		return (NULL);
1229*7c478bd9Sstevel@tonic-gate 	}
1230*7c478bd9Sstevel@tonic-gate 
1231*7c478bd9Sstevel@tonic-gate 	return (buf);
1232*7c478bd9Sstevel@tonic-gate }
1233*7c478bd9Sstevel@tonic-gate 
1234*7c478bd9Sstevel@tonic-gate /*
1235*7c478bd9Sstevel@tonic-gate  * Free a constructed object to cache cp.
1236*7c478bd9Sstevel@tonic-gate  */
1237*7c478bd9Sstevel@tonic-gate void
1238*7c478bd9Sstevel@tonic-gate kmem_cache_free(kmem_cache_t *cp, void *buf)
1239*7c478bd9Sstevel@tonic-gate {
1240*7c478bd9Sstevel@tonic-gate 	kmem_cpu_cache_t *ccp = KMEM_CPU_CACHE(cp);
1241*7c478bd9Sstevel@tonic-gate 	kmem_magazine_t *emp;
1242*7c478bd9Sstevel@tonic-gate 	kmem_magtype_t *mtp;
1243*7c478bd9Sstevel@tonic-gate 
1244*7c478bd9Sstevel@tonic-gate 	if (ccp->cc_flags & KMF_BUFTAG)
1245*7c478bd9Sstevel@tonic-gate 		if (kmem_cache_free_debug(cp, buf, caller()) == -1)
1246*7c478bd9Sstevel@tonic-gate 			return;
1247*7c478bd9Sstevel@tonic-gate 
1248*7c478bd9Sstevel@tonic-gate 	mutex_enter(&ccp->cc_lock);
1249*7c478bd9Sstevel@tonic-gate 	for (;;) {
1250*7c478bd9Sstevel@tonic-gate 		/*
1251*7c478bd9Sstevel@tonic-gate 		 * If there's a slot available in the current CPU's
1252*7c478bd9Sstevel@tonic-gate 		 * loaded magazine, just put the object there and return.
1253*7c478bd9Sstevel@tonic-gate 		 */
1254*7c478bd9Sstevel@tonic-gate 		if ((uint_t)ccp->cc_rounds < ccp->cc_magsize) {
1255*7c478bd9Sstevel@tonic-gate 			ccp->cc_loaded->mag_round[ccp->cc_rounds++] = buf;
1256*7c478bd9Sstevel@tonic-gate 			ccp->cc_free++;
1257*7c478bd9Sstevel@tonic-gate 			mutex_exit(&ccp->cc_lock);
1258*7c478bd9Sstevel@tonic-gate 			return;
1259*7c478bd9Sstevel@tonic-gate 		}
1260*7c478bd9Sstevel@tonic-gate 
1261*7c478bd9Sstevel@tonic-gate 		/*
1262*7c478bd9Sstevel@tonic-gate 		 * The loaded magazine is full.  If the previously loaded
1263*7c478bd9Sstevel@tonic-gate 		 * magazine was empty, exchange them and try again.
1264*7c478bd9Sstevel@tonic-gate 		 */
1265*7c478bd9Sstevel@tonic-gate 		if (ccp->cc_prounds == 0) {
1266*7c478bd9Sstevel@tonic-gate 			kmem_cpu_reload(ccp, ccp->cc_ploaded, ccp->cc_prounds);
1267*7c478bd9Sstevel@tonic-gate 			continue;
1268*7c478bd9Sstevel@tonic-gate 		}
1269*7c478bd9Sstevel@tonic-gate 
1270*7c478bd9Sstevel@tonic-gate 		/*
1271*7c478bd9Sstevel@tonic-gate 		 * If the magazine layer is disabled, break out now.
1272*7c478bd9Sstevel@tonic-gate 		 */
1273*7c478bd9Sstevel@tonic-gate 		if (ccp->cc_magsize == 0)
1274*7c478bd9Sstevel@tonic-gate 			break;
1275*7c478bd9Sstevel@tonic-gate 
1276*7c478bd9Sstevel@tonic-gate 		/*
1277*7c478bd9Sstevel@tonic-gate 		 * Try to get an empty magazine from the depot.
1278*7c478bd9Sstevel@tonic-gate 		 */
1279*7c478bd9Sstevel@tonic-gate 		emp = kmem_depot_alloc(cp, &cp->cache_empty);
1280*7c478bd9Sstevel@tonic-gate 		if (emp != NULL) {
1281*7c478bd9Sstevel@tonic-gate 			if (ccp->cc_ploaded != NULL)
1282*7c478bd9Sstevel@tonic-gate 				kmem_depot_free(cp, &cp->cache_full,
1283*7c478bd9Sstevel@tonic-gate 				    ccp->cc_ploaded);
1284*7c478bd9Sstevel@tonic-gate 			kmem_cpu_reload(ccp, emp, 0);
1285*7c478bd9Sstevel@tonic-gate 			continue;
1286*7c478bd9Sstevel@tonic-gate 		}
1287*7c478bd9Sstevel@tonic-gate 
1288*7c478bd9Sstevel@tonic-gate 		/*
1289*7c478bd9Sstevel@tonic-gate 		 * There are no empty magazines in the depot,
1290*7c478bd9Sstevel@tonic-gate 		 * so try to allocate a new one.  We must drop all locks
1291*7c478bd9Sstevel@tonic-gate 		 * across kmem_cache_alloc() because lower layers may
1292*7c478bd9Sstevel@tonic-gate 		 * attempt to allocate from this cache.
1293*7c478bd9Sstevel@tonic-gate 		 */
1294*7c478bd9Sstevel@tonic-gate 		mtp = cp->cache_magtype;
1295*7c478bd9Sstevel@tonic-gate 		mutex_exit(&ccp->cc_lock);
1296*7c478bd9Sstevel@tonic-gate 		emp = kmem_cache_alloc(mtp->mt_cache, KM_NOSLEEP);
1297*7c478bd9Sstevel@tonic-gate 		mutex_enter(&ccp->cc_lock);
1298*7c478bd9Sstevel@tonic-gate 
1299*7c478bd9Sstevel@tonic-gate 		if (emp != NULL) {
1300*7c478bd9Sstevel@tonic-gate 			/*
1301*7c478bd9Sstevel@tonic-gate 			 * We successfully allocated an empty magazine.
1302*7c478bd9Sstevel@tonic-gate 			 * However, we had to drop ccp->cc_lock to do it,
1303*7c478bd9Sstevel@tonic-gate 			 * so the cache's magazine size may have changed.
1304*7c478bd9Sstevel@tonic-gate 			 * If so, free the magazine and try again.
1305*7c478bd9Sstevel@tonic-gate 			 */
1306*7c478bd9Sstevel@tonic-gate 			if (ccp->cc_magsize != mtp->mt_magsize) {
1307*7c478bd9Sstevel@tonic-gate 				mutex_exit(&ccp->cc_lock);
1308*7c478bd9Sstevel@tonic-gate 				kmem_cache_free(mtp->mt_cache, emp);
1309*7c478bd9Sstevel@tonic-gate 				mutex_enter(&ccp->cc_lock);
1310*7c478bd9Sstevel@tonic-gate 				continue;
1311*7c478bd9Sstevel@tonic-gate 			}
1312*7c478bd9Sstevel@tonic-gate 
1313*7c478bd9Sstevel@tonic-gate 			/*
1314*7c478bd9Sstevel@tonic-gate 			 * We got a magazine of the right size.  Add it to
1315*7c478bd9Sstevel@tonic-gate 			 * the depot and try the whole dance again.
1316*7c478bd9Sstevel@tonic-gate 			 */
1317*7c478bd9Sstevel@tonic-gate 			kmem_depot_free(cp, &cp->cache_empty, emp);
1318*7c478bd9Sstevel@tonic-gate 			continue;
1319*7c478bd9Sstevel@tonic-gate 		}
1320*7c478bd9Sstevel@tonic-gate 
1321*7c478bd9Sstevel@tonic-gate 		/*
1322*7c478bd9Sstevel@tonic-gate 		 * We couldn't allocate an empty magazine,
1323*7c478bd9Sstevel@tonic-gate 		 * so fall through to the slab layer.
1324*7c478bd9Sstevel@tonic-gate 		 */
1325*7c478bd9Sstevel@tonic-gate 		break;
1326*7c478bd9Sstevel@tonic-gate 	}
1327*7c478bd9Sstevel@tonic-gate 	mutex_exit(&ccp->cc_lock);
1328*7c478bd9Sstevel@tonic-gate 
1329*7c478bd9Sstevel@tonic-gate 	/*
1330*7c478bd9Sstevel@tonic-gate 	 * We couldn't free our constructed object to the magazine layer,
1331*7c478bd9Sstevel@tonic-gate 	 * so apply its destructor and free it to the slab layer.
1332*7c478bd9Sstevel@tonic-gate 	 * Note that if KMF_DEADBEEF is in effect and KMF_LITE is not,
1333*7c478bd9Sstevel@tonic-gate 	 * kmem_cache_free_debug() will have already applied the destructor.
1334*7c478bd9Sstevel@tonic-gate 	 */
1335*7c478bd9Sstevel@tonic-gate 	if ((cp->cache_flags & (KMF_DEADBEEF | KMF_LITE)) != KMF_DEADBEEF &&
1336*7c478bd9Sstevel@tonic-gate 	    cp->cache_destructor != NULL) {
1337*7c478bd9Sstevel@tonic-gate 		if (cp->cache_flags & KMF_DEADBEEF) {	/* KMF_LITE implied */
1338*7c478bd9Sstevel@tonic-gate 			kmem_buftag_t *btp = KMEM_BUFTAG(cp, buf);
1339*7c478bd9Sstevel@tonic-gate 			*(uint64_t *)buf = btp->bt_redzone;
1340*7c478bd9Sstevel@tonic-gate 			cp->cache_destructor(buf, cp->cache_private);
1341*7c478bd9Sstevel@tonic-gate 			*(uint64_t *)buf = KMEM_FREE_PATTERN;
1342*7c478bd9Sstevel@tonic-gate 		} else {
1343*7c478bd9Sstevel@tonic-gate 			cp->cache_destructor(buf, cp->cache_private);
1344*7c478bd9Sstevel@tonic-gate 		}
1345*7c478bd9Sstevel@tonic-gate 	}
1346*7c478bd9Sstevel@tonic-gate 
1347*7c478bd9Sstevel@tonic-gate 	kmem_slab_free(cp, buf);
1348*7c478bd9Sstevel@tonic-gate }
1349*7c478bd9Sstevel@tonic-gate 
1350*7c478bd9Sstevel@tonic-gate void *
1351*7c478bd9Sstevel@tonic-gate kmem_zalloc(size_t size, int kmflag)
1352*7c478bd9Sstevel@tonic-gate {
1353*7c478bd9Sstevel@tonic-gate 	size_t index = (size - 1) >> KMEM_ALIGN_SHIFT;
1354*7c478bd9Sstevel@tonic-gate 	void *buf;
1355*7c478bd9Sstevel@tonic-gate 
1356*7c478bd9Sstevel@tonic-gate 	if (index < KMEM_MAXBUF >> KMEM_ALIGN_SHIFT) {
1357*7c478bd9Sstevel@tonic-gate 		kmem_cache_t *cp = kmem_alloc_table[index];
1358*7c478bd9Sstevel@tonic-gate 		buf = kmem_cache_alloc(cp, kmflag);
1359*7c478bd9Sstevel@tonic-gate 		if (buf != NULL) {
1360*7c478bd9Sstevel@tonic-gate 			if (cp->cache_flags & KMF_BUFTAG) {
1361*7c478bd9Sstevel@tonic-gate 				kmem_buftag_t *btp = KMEM_BUFTAG(cp, buf);
1362*7c478bd9Sstevel@tonic-gate 				((uint8_t *)buf)[size] = KMEM_REDZONE_BYTE;
1363*7c478bd9Sstevel@tonic-gate 				((uint32_t *)btp)[1] = KMEM_SIZE_ENCODE(size);
1364*7c478bd9Sstevel@tonic-gate 
1365*7c478bd9Sstevel@tonic-gate 				if (cp->cache_flags & KMF_LITE) {
1366*7c478bd9Sstevel@tonic-gate 					KMEM_BUFTAG_LITE_ENTER(btp,
1367*7c478bd9Sstevel@tonic-gate 					    kmem_lite_count, caller());
1368*7c478bd9Sstevel@tonic-gate 				}
1369*7c478bd9Sstevel@tonic-gate 			}
1370*7c478bd9Sstevel@tonic-gate 			bzero(buf, size);
1371*7c478bd9Sstevel@tonic-gate 		}
1372*7c478bd9Sstevel@tonic-gate 	} else {
1373*7c478bd9Sstevel@tonic-gate 		buf = kmem_alloc(size, kmflag);
1374*7c478bd9Sstevel@tonic-gate 		if (buf != NULL)
1375*7c478bd9Sstevel@tonic-gate 			bzero(buf, size);
1376*7c478bd9Sstevel@tonic-gate 	}
1377*7c478bd9Sstevel@tonic-gate 	return (buf);
1378*7c478bd9Sstevel@tonic-gate }
1379*7c478bd9Sstevel@tonic-gate 
1380*7c478bd9Sstevel@tonic-gate void *
1381*7c478bd9Sstevel@tonic-gate kmem_alloc(size_t size, int kmflag)
1382*7c478bd9Sstevel@tonic-gate {
1383*7c478bd9Sstevel@tonic-gate 	size_t index = (size - 1) >> KMEM_ALIGN_SHIFT;
1384*7c478bd9Sstevel@tonic-gate 	void *buf;
1385*7c478bd9Sstevel@tonic-gate 
1386*7c478bd9Sstevel@tonic-gate 	if (index < KMEM_MAXBUF >> KMEM_ALIGN_SHIFT) {
1387*7c478bd9Sstevel@tonic-gate 		kmem_cache_t *cp = kmem_alloc_table[index];
1388*7c478bd9Sstevel@tonic-gate 		buf = kmem_cache_alloc(cp, kmflag);
1389*7c478bd9Sstevel@tonic-gate 		if ((cp->cache_flags & KMF_BUFTAG) && buf != NULL) {
1390*7c478bd9Sstevel@tonic-gate 			kmem_buftag_t *btp = KMEM_BUFTAG(cp, buf);
1391*7c478bd9Sstevel@tonic-gate 			((uint8_t *)buf)[size] = KMEM_REDZONE_BYTE;
1392*7c478bd9Sstevel@tonic-gate 			((uint32_t *)btp)[1] = KMEM_SIZE_ENCODE(size);
1393*7c478bd9Sstevel@tonic-gate 
1394*7c478bd9Sstevel@tonic-gate 			if (cp->cache_flags & KMF_LITE) {
1395*7c478bd9Sstevel@tonic-gate 				KMEM_BUFTAG_LITE_ENTER(btp, kmem_lite_count,
1396*7c478bd9Sstevel@tonic-gate 				    caller());
1397*7c478bd9Sstevel@tonic-gate 			}
1398*7c478bd9Sstevel@tonic-gate 		}
1399*7c478bd9Sstevel@tonic-gate 		return (buf);
1400*7c478bd9Sstevel@tonic-gate 	}
1401*7c478bd9Sstevel@tonic-gate 	if (size == 0)
1402*7c478bd9Sstevel@tonic-gate 		return (NULL);
1403*7c478bd9Sstevel@tonic-gate 	buf = vmem_alloc(kmem_oversize_arena, size, kmflag & KM_VMFLAGS);
1404*7c478bd9Sstevel@tonic-gate 	if (buf == NULL)
1405*7c478bd9Sstevel@tonic-gate 		kmem_log_event(kmem_failure_log, NULL, NULL, (void *)size);
1406*7c478bd9Sstevel@tonic-gate 	return (buf);
1407*7c478bd9Sstevel@tonic-gate }
1408*7c478bd9Sstevel@tonic-gate 
1409*7c478bd9Sstevel@tonic-gate void
1410*7c478bd9Sstevel@tonic-gate kmem_free(void *buf, size_t size)
1411*7c478bd9Sstevel@tonic-gate {
1412*7c478bd9Sstevel@tonic-gate 	size_t index = (size - 1) >> KMEM_ALIGN_SHIFT;
1413*7c478bd9Sstevel@tonic-gate 
1414*7c478bd9Sstevel@tonic-gate 	if (index < KMEM_MAXBUF >> KMEM_ALIGN_SHIFT) {
1415*7c478bd9Sstevel@tonic-gate 		kmem_cache_t *cp = kmem_alloc_table[index];
1416*7c478bd9Sstevel@tonic-gate 		if (cp->cache_flags & KMF_BUFTAG) {
1417*7c478bd9Sstevel@tonic-gate 			kmem_buftag_t *btp = KMEM_BUFTAG(cp, buf);
1418*7c478bd9Sstevel@tonic-gate 			uint32_t *ip = (uint32_t *)btp;
1419*7c478bd9Sstevel@tonic-gate 			if (ip[1] != KMEM_SIZE_ENCODE(size)) {
1420*7c478bd9Sstevel@tonic-gate 				if (*(uint64_t *)buf == KMEM_FREE_PATTERN) {
1421*7c478bd9Sstevel@tonic-gate 					kmem_error(KMERR_DUPFREE, cp, buf);
1422*7c478bd9Sstevel@tonic-gate 					return;
1423*7c478bd9Sstevel@tonic-gate 				}
1424*7c478bd9Sstevel@tonic-gate 				if (KMEM_SIZE_VALID(ip[1])) {
1425*7c478bd9Sstevel@tonic-gate 					ip[0] = KMEM_SIZE_ENCODE(size);
1426*7c478bd9Sstevel@tonic-gate 					kmem_error(KMERR_BADSIZE, cp, buf);
1427*7c478bd9Sstevel@tonic-gate 				} else {
1428*7c478bd9Sstevel@tonic-gate 					kmem_error(KMERR_REDZONE, cp, buf);
1429*7c478bd9Sstevel@tonic-gate 				}
1430*7c478bd9Sstevel@tonic-gate 				return;
1431*7c478bd9Sstevel@tonic-gate 			}
1432*7c478bd9Sstevel@tonic-gate 			if (((uint8_t *)buf)[size] != KMEM_REDZONE_BYTE) {
1433*7c478bd9Sstevel@tonic-gate 				kmem_error(KMERR_REDZONE, cp, buf);
1434*7c478bd9Sstevel@tonic-gate 				return;
1435*7c478bd9Sstevel@tonic-gate 			}
1436*7c478bd9Sstevel@tonic-gate 			btp->bt_redzone = KMEM_REDZONE_PATTERN;
1437*7c478bd9Sstevel@tonic-gate 			if (cp->cache_flags & KMF_LITE) {
1438*7c478bd9Sstevel@tonic-gate 				KMEM_BUFTAG_LITE_ENTER(btp, kmem_lite_count,
1439*7c478bd9Sstevel@tonic-gate 				    caller());
1440*7c478bd9Sstevel@tonic-gate 			}
1441*7c478bd9Sstevel@tonic-gate 		}
1442*7c478bd9Sstevel@tonic-gate 		kmem_cache_free(cp, buf);
1443*7c478bd9Sstevel@tonic-gate 	} else {
1444*7c478bd9Sstevel@tonic-gate 		if (buf == NULL && size == 0)
1445*7c478bd9Sstevel@tonic-gate 			return;
1446*7c478bd9Sstevel@tonic-gate 		vmem_free(kmem_oversize_arena, buf, size);
1447*7c478bd9Sstevel@tonic-gate 	}
1448*7c478bd9Sstevel@tonic-gate }
1449*7c478bd9Sstevel@tonic-gate 
1450*7c478bd9Sstevel@tonic-gate void *
1451*7c478bd9Sstevel@tonic-gate kmem_firewall_va_alloc(vmem_t *vmp, size_t size, int vmflag)
1452*7c478bd9Sstevel@tonic-gate {
1453*7c478bd9Sstevel@tonic-gate 	size_t realsize = size + vmp->vm_quantum;
1454*7c478bd9Sstevel@tonic-gate 	void *addr;
1455*7c478bd9Sstevel@tonic-gate 
1456*7c478bd9Sstevel@tonic-gate 	/*
1457*7c478bd9Sstevel@tonic-gate 	 * Annoying edge case: if 'size' is just shy of ULONG_MAX, adding
1458*7c478bd9Sstevel@tonic-gate 	 * vm_quantum will cause integer wraparound.  Check for this, and
1459*7c478bd9Sstevel@tonic-gate 	 * blow off the firewall page in this case.  Note that such a
1460*7c478bd9Sstevel@tonic-gate 	 * giant allocation (the entire kernel address space) can never
1461*7c478bd9Sstevel@tonic-gate 	 * be satisfied, so it will either fail immediately (VM_NOSLEEP)
1462*7c478bd9Sstevel@tonic-gate 	 * or sleep forever (VM_SLEEP).  Thus, there is no need for a
1463*7c478bd9Sstevel@tonic-gate 	 * corresponding check in kmem_firewall_va_free().
1464*7c478bd9Sstevel@tonic-gate 	 */
1465*7c478bd9Sstevel@tonic-gate 	if (realsize < size)
1466*7c478bd9Sstevel@tonic-gate 		realsize = size;
1467*7c478bd9Sstevel@tonic-gate 
1468*7c478bd9Sstevel@tonic-gate 	/*
1469*7c478bd9Sstevel@tonic-gate 	 * While boot still owns resource management, make sure that this
1470*7c478bd9Sstevel@tonic-gate 	 * redzone virtual address allocation is properly accounted for in
1471*7c478bd9Sstevel@tonic-gate 	 * OBPs "virtual-memory" "available" lists because we're
1472*7c478bd9Sstevel@tonic-gate 	 * effectively claiming them for a red zone.  If we don't do this,
1473*7c478bd9Sstevel@tonic-gate 	 * the available lists become too fragmented and too large for the
1474*7c478bd9Sstevel@tonic-gate 	 * current boot/kernel memory list interface.
1475*7c478bd9Sstevel@tonic-gate 	 */
1476*7c478bd9Sstevel@tonic-gate 	addr = vmem_alloc(vmp, realsize, vmflag | VM_NEXTFIT);
1477*7c478bd9Sstevel@tonic-gate 
1478*7c478bd9Sstevel@tonic-gate 	if (addr != NULL && kvseg.s_base == NULL && realsize != size)
1479*7c478bd9Sstevel@tonic-gate 		(void) boot_virt_alloc((char *)addr + size, vmp->vm_quantum);
1480*7c478bd9Sstevel@tonic-gate 
1481*7c478bd9Sstevel@tonic-gate 	return (addr);
1482*7c478bd9Sstevel@tonic-gate }
1483*7c478bd9Sstevel@tonic-gate 
1484*7c478bd9Sstevel@tonic-gate void
1485*7c478bd9Sstevel@tonic-gate kmem_firewall_va_free(vmem_t *vmp, void *addr, size_t size)
1486*7c478bd9Sstevel@tonic-gate {
1487*7c478bd9Sstevel@tonic-gate 	ASSERT((kvseg.s_base == NULL ?
1488*7c478bd9Sstevel@tonic-gate 	    va_to_pfn((char *)addr + size) :
1489*7c478bd9Sstevel@tonic-gate 	    hat_getpfnum(kas.a_hat, (caddr_t)addr + size)) == PFN_INVALID);
1490*7c478bd9Sstevel@tonic-gate 
1491*7c478bd9Sstevel@tonic-gate 	vmem_free(vmp, addr, size + vmp->vm_quantum);
1492*7c478bd9Sstevel@tonic-gate }
1493*7c478bd9Sstevel@tonic-gate 
1494*7c478bd9Sstevel@tonic-gate /*
1495*7c478bd9Sstevel@tonic-gate  * Try to allocate at least `size' bytes of memory without sleeping or
1496*7c478bd9Sstevel@tonic-gate  * panicking. Return actual allocated size in `asize'. If allocation failed,
1497*7c478bd9Sstevel@tonic-gate  * try final allocation with sleep or panic allowed.
1498*7c478bd9Sstevel@tonic-gate  */
1499*7c478bd9Sstevel@tonic-gate void *
1500*7c478bd9Sstevel@tonic-gate kmem_alloc_tryhard(size_t size, size_t *asize, int kmflag)
1501*7c478bd9Sstevel@tonic-gate {
1502*7c478bd9Sstevel@tonic-gate 	void *p;
1503*7c478bd9Sstevel@tonic-gate 
1504*7c478bd9Sstevel@tonic-gate 	*asize = P2ROUNDUP(size, KMEM_ALIGN);
1505*7c478bd9Sstevel@tonic-gate 	do {
1506*7c478bd9Sstevel@tonic-gate 		p = kmem_alloc(*asize, (kmflag | KM_NOSLEEP) & ~KM_PANIC);
1507*7c478bd9Sstevel@tonic-gate 		if (p != NULL)
1508*7c478bd9Sstevel@tonic-gate 			return (p);
1509*7c478bd9Sstevel@tonic-gate 		*asize += KMEM_ALIGN;
1510*7c478bd9Sstevel@tonic-gate 	} while (*asize <= PAGESIZE);
1511*7c478bd9Sstevel@tonic-gate 
1512*7c478bd9Sstevel@tonic-gate 	*asize = P2ROUNDUP(size, KMEM_ALIGN);
1513*7c478bd9Sstevel@tonic-gate 	return (kmem_alloc(*asize, kmflag));
1514*7c478bd9Sstevel@tonic-gate }
1515*7c478bd9Sstevel@tonic-gate 
1516*7c478bd9Sstevel@tonic-gate /*
1517*7c478bd9Sstevel@tonic-gate  * Reclaim all unused memory from a cache.
1518*7c478bd9Sstevel@tonic-gate  */
1519*7c478bd9Sstevel@tonic-gate static void
1520*7c478bd9Sstevel@tonic-gate kmem_cache_reap(kmem_cache_t *cp)
1521*7c478bd9Sstevel@tonic-gate {
1522*7c478bd9Sstevel@tonic-gate 	/*
1523*7c478bd9Sstevel@tonic-gate 	 * Ask the cache's owner to free some memory if possible.
1524*7c478bd9Sstevel@tonic-gate 	 * The idea is to handle things like the inode cache, which
1525*7c478bd9Sstevel@tonic-gate 	 * typically sits on a bunch of memory that it doesn't truly
1526*7c478bd9Sstevel@tonic-gate 	 * *need*.  Reclaim policy is entirely up to the owner; this
1527*7c478bd9Sstevel@tonic-gate 	 * callback is just an advisory plea for help.
1528*7c478bd9Sstevel@tonic-gate 	 */
1529*7c478bd9Sstevel@tonic-gate 	if (cp->cache_reclaim != NULL)
1530*7c478bd9Sstevel@tonic-gate 		cp->cache_reclaim(cp->cache_private);
1531*7c478bd9Sstevel@tonic-gate 
1532*7c478bd9Sstevel@tonic-gate 	kmem_depot_ws_reap(cp);
1533*7c478bd9Sstevel@tonic-gate }
1534*7c478bd9Sstevel@tonic-gate 
1535*7c478bd9Sstevel@tonic-gate static void
1536*7c478bd9Sstevel@tonic-gate kmem_reap_timeout(void *flag_arg)
1537*7c478bd9Sstevel@tonic-gate {
1538*7c478bd9Sstevel@tonic-gate 	uint32_t *flag = (uint32_t *)flag_arg;
1539*7c478bd9Sstevel@tonic-gate 
1540*7c478bd9Sstevel@tonic-gate 	ASSERT(flag == &kmem_reaping || flag == &kmem_reaping_idspace);
1541*7c478bd9Sstevel@tonic-gate 	*flag = 0;
1542*7c478bd9Sstevel@tonic-gate }
1543*7c478bd9Sstevel@tonic-gate 
1544*7c478bd9Sstevel@tonic-gate static void
1545*7c478bd9Sstevel@tonic-gate kmem_reap_done(void *flag)
1546*7c478bd9Sstevel@tonic-gate {
1547*7c478bd9Sstevel@tonic-gate 	(void) timeout(kmem_reap_timeout, flag, kmem_reap_interval);
1548*7c478bd9Sstevel@tonic-gate }
1549*7c478bd9Sstevel@tonic-gate 
1550*7c478bd9Sstevel@tonic-gate static void
1551*7c478bd9Sstevel@tonic-gate kmem_reap_start(void *flag)
1552*7c478bd9Sstevel@tonic-gate {
1553*7c478bd9Sstevel@tonic-gate 	ASSERT(flag == &kmem_reaping || flag == &kmem_reaping_idspace);
1554*7c478bd9Sstevel@tonic-gate 
1555*7c478bd9Sstevel@tonic-gate 	if (flag == &kmem_reaping) {
1556*7c478bd9Sstevel@tonic-gate 		kmem_cache_applyall(kmem_cache_reap, kmem_taskq, TQ_NOSLEEP);
1557*7c478bd9Sstevel@tonic-gate 		/*
1558*7c478bd9Sstevel@tonic-gate 		 * if we have segkp under heap, reap segkp cache.
1559*7c478bd9Sstevel@tonic-gate 		 */
1560*7c478bd9Sstevel@tonic-gate 		if (segkp_fromheap)
1561*7c478bd9Sstevel@tonic-gate 			segkp_cache_free();
1562*7c478bd9Sstevel@tonic-gate 	}
1563*7c478bd9Sstevel@tonic-gate 	else
1564*7c478bd9Sstevel@tonic-gate 		kmem_cache_applyall_id(kmem_cache_reap, kmem_taskq, TQ_NOSLEEP);
1565*7c478bd9Sstevel@tonic-gate 
1566*7c478bd9Sstevel@tonic-gate 	/*
1567*7c478bd9Sstevel@tonic-gate 	 * We use taskq_dispatch() to schedule a timeout to clear
1568*7c478bd9Sstevel@tonic-gate 	 * the flag so that kmem_reap() becomes self-throttling:
1569*7c478bd9Sstevel@tonic-gate 	 * we won't reap again until the current reap completes *and*
1570*7c478bd9Sstevel@tonic-gate 	 * at least kmem_reap_interval ticks have elapsed.
1571*7c478bd9Sstevel@tonic-gate 	 */
1572*7c478bd9Sstevel@tonic-gate 	if (!taskq_dispatch(kmem_taskq, kmem_reap_done, flag, TQ_NOSLEEP))
1573*7c478bd9Sstevel@tonic-gate 		kmem_reap_done(flag);
1574*7c478bd9Sstevel@tonic-gate }
1575*7c478bd9Sstevel@tonic-gate 
1576*7c478bd9Sstevel@tonic-gate static void
1577*7c478bd9Sstevel@tonic-gate kmem_reap_common(void *flag_arg)
1578*7c478bd9Sstevel@tonic-gate {
1579*7c478bd9Sstevel@tonic-gate 	uint32_t *flag = (uint32_t *)flag_arg;
1580*7c478bd9Sstevel@tonic-gate 
1581*7c478bd9Sstevel@tonic-gate 	if (MUTEX_HELD(&kmem_cache_lock) || kmem_taskq == NULL ||
1582*7c478bd9Sstevel@tonic-gate 	    cas32(flag, 0, 1) != 0)
1583*7c478bd9Sstevel@tonic-gate 		return;
1584*7c478bd9Sstevel@tonic-gate 
1585*7c478bd9Sstevel@tonic-gate 	/*
1586*7c478bd9Sstevel@tonic-gate 	 * It may not be kosher to do memory allocation when a reap is called
1587*7c478bd9Sstevel@tonic-gate 	 * is called (for example, if vmem_populate() is in the call chain).
1588*7c478bd9Sstevel@tonic-gate 	 * So we start the reap going with a TQ_NOALLOC dispatch.  If the
1589*7c478bd9Sstevel@tonic-gate 	 * dispatch fails, we reset the flag, and the next reap will try again.
1590*7c478bd9Sstevel@tonic-gate 	 */
1591*7c478bd9Sstevel@tonic-gate 	if (!taskq_dispatch(kmem_taskq, kmem_reap_start, flag, TQ_NOALLOC))
1592*7c478bd9Sstevel@tonic-gate 		*flag = 0;
1593*7c478bd9Sstevel@tonic-gate }
1594*7c478bd9Sstevel@tonic-gate 
1595*7c478bd9Sstevel@tonic-gate /*
1596*7c478bd9Sstevel@tonic-gate  * Reclaim all unused memory from all caches.  Called from the VM system
1597*7c478bd9Sstevel@tonic-gate  * when memory gets tight.
1598*7c478bd9Sstevel@tonic-gate  */
1599*7c478bd9Sstevel@tonic-gate void
1600*7c478bd9Sstevel@tonic-gate kmem_reap(void)
1601*7c478bd9Sstevel@tonic-gate {
1602*7c478bd9Sstevel@tonic-gate 	kmem_reap_common(&kmem_reaping);
1603*7c478bd9Sstevel@tonic-gate }
1604*7c478bd9Sstevel@tonic-gate 
1605*7c478bd9Sstevel@tonic-gate /*
1606*7c478bd9Sstevel@tonic-gate  * Reclaim all unused memory from identifier arenas, called when a vmem
1607*7c478bd9Sstevel@tonic-gate  * arena not back by memory is exhausted.  Since reaping memory-backed caches
1608*7c478bd9Sstevel@tonic-gate  * cannot help with identifier exhaustion, we avoid both a large amount of
1609*7c478bd9Sstevel@tonic-gate  * work and unwanted side-effects from reclaim callbacks.
1610*7c478bd9Sstevel@tonic-gate  */
1611*7c478bd9Sstevel@tonic-gate void
1612*7c478bd9Sstevel@tonic-gate kmem_reap_idspace(void)
1613*7c478bd9Sstevel@tonic-gate {
1614*7c478bd9Sstevel@tonic-gate 	kmem_reap_common(&kmem_reaping_idspace);
1615*7c478bd9Sstevel@tonic-gate }
1616*7c478bd9Sstevel@tonic-gate 
1617*7c478bd9Sstevel@tonic-gate /*
1618*7c478bd9Sstevel@tonic-gate  * Purge all magazines from a cache and set its magazine limit to zero.
1619*7c478bd9Sstevel@tonic-gate  * All calls are serialized by the kmem_taskq lock, except for the final
1620*7c478bd9Sstevel@tonic-gate  * call from kmem_cache_destroy().
1621*7c478bd9Sstevel@tonic-gate  */
1622*7c478bd9Sstevel@tonic-gate static void
1623*7c478bd9Sstevel@tonic-gate kmem_cache_magazine_purge(kmem_cache_t *cp)
1624*7c478bd9Sstevel@tonic-gate {
1625*7c478bd9Sstevel@tonic-gate 	kmem_cpu_cache_t *ccp;
1626*7c478bd9Sstevel@tonic-gate 	kmem_magazine_t *mp, *pmp;
1627*7c478bd9Sstevel@tonic-gate 	int rounds, prounds, cpu_seqid;
1628*7c478bd9Sstevel@tonic-gate 
1629*7c478bd9Sstevel@tonic-gate 	ASSERT(cp->cache_next == NULL || taskq_member(kmem_taskq, curthread));
1630*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&cp->cache_lock));
1631*7c478bd9Sstevel@tonic-gate 
1632*7c478bd9Sstevel@tonic-gate 	for (cpu_seqid = 0; cpu_seqid < max_ncpus; cpu_seqid++) {
1633*7c478bd9Sstevel@tonic-gate 		ccp = &cp->cache_cpu[cpu_seqid];
1634*7c478bd9Sstevel@tonic-gate 
1635*7c478bd9Sstevel@tonic-gate 		mutex_enter(&ccp->cc_lock);
1636*7c478bd9Sstevel@tonic-gate 		mp = ccp->cc_loaded;
1637*7c478bd9Sstevel@tonic-gate 		pmp = ccp->cc_ploaded;
1638*7c478bd9Sstevel@tonic-gate 		rounds = ccp->cc_rounds;
1639*7c478bd9Sstevel@tonic-gate 		prounds = ccp->cc_prounds;
1640*7c478bd9Sstevel@tonic-gate 		ccp->cc_loaded = NULL;
1641*7c478bd9Sstevel@tonic-gate 		ccp->cc_ploaded = NULL;
1642*7c478bd9Sstevel@tonic-gate 		ccp->cc_rounds = -1;
1643*7c478bd9Sstevel@tonic-gate 		ccp->cc_prounds = -1;
1644*7c478bd9Sstevel@tonic-gate 		ccp->cc_magsize = 0;
1645*7c478bd9Sstevel@tonic-gate 		mutex_exit(&ccp->cc_lock);
1646*7c478bd9Sstevel@tonic-gate 
1647*7c478bd9Sstevel@tonic-gate 		if (mp)
1648*7c478bd9Sstevel@tonic-gate 			kmem_magazine_destroy(cp, mp, rounds);
1649*7c478bd9Sstevel@tonic-gate 		if (pmp)
1650*7c478bd9Sstevel@tonic-gate 			kmem_magazine_destroy(cp, pmp, prounds);
1651*7c478bd9Sstevel@tonic-gate 	}
1652*7c478bd9Sstevel@tonic-gate 
1653*7c478bd9Sstevel@tonic-gate 	/*
1654*7c478bd9Sstevel@tonic-gate 	 * Updating the working set statistics twice in a row has the
1655*7c478bd9Sstevel@tonic-gate 	 * effect of setting the working set size to zero, so everything
1656*7c478bd9Sstevel@tonic-gate 	 * is eligible for reaping.
1657*7c478bd9Sstevel@tonic-gate 	 */
1658*7c478bd9Sstevel@tonic-gate 	kmem_depot_ws_update(cp);
1659*7c478bd9Sstevel@tonic-gate 	kmem_depot_ws_update(cp);
1660*7c478bd9Sstevel@tonic-gate 
1661*7c478bd9Sstevel@tonic-gate 	kmem_depot_ws_reap(cp);
1662*7c478bd9Sstevel@tonic-gate }
1663*7c478bd9Sstevel@tonic-gate 
1664*7c478bd9Sstevel@tonic-gate /*
1665*7c478bd9Sstevel@tonic-gate  * Enable per-cpu magazines on a cache.
1666*7c478bd9Sstevel@tonic-gate  */
1667*7c478bd9Sstevel@tonic-gate static void
1668*7c478bd9Sstevel@tonic-gate kmem_cache_magazine_enable(kmem_cache_t *cp)
1669*7c478bd9Sstevel@tonic-gate {
1670*7c478bd9Sstevel@tonic-gate 	int cpu_seqid;
1671*7c478bd9Sstevel@tonic-gate 
1672*7c478bd9Sstevel@tonic-gate 	if (cp->cache_flags & KMF_NOMAGAZINE)
1673*7c478bd9Sstevel@tonic-gate 		return;
1674*7c478bd9Sstevel@tonic-gate 
1675*7c478bd9Sstevel@tonic-gate 	for (cpu_seqid = 0; cpu_seqid < max_ncpus; cpu_seqid++) {
1676*7c478bd9Sstevel@tonic-gate 		kmem_cpu_cache_t *ccp = &cp->cache_cpu[cpu_seqid];
1677*7c478bd9Sstevel@tonic-gate 		mutex_enter(&ccp->cc_lock);
1678*7c478bd9Sstevel@tonic-gate 		ccp->cc_magsize = cp->cache_magtype->mt_magsize;
1679*7c478bd9Sstevel@tonic-gate 		mutex_exit(&ccp->cc_lock);
1680*7c478bd9Sstevel@tonic-gate 	}
1681*7c478bd9Sstevel@tonic-gate 
1682*7c478bd9Sstevel@tonic-gate }
1683*7c478bd9Sstevel@tonic-gate 
1684*7c478bd9Sstevel@tonic-gate /*
1685*7c478bd9Sstevel@tonic-gate  * Recompute a cache's magazine size.  The trade-off is that larger magazines
1686*7c478bd9Sstevel@tonic-gate  * provide a higher transfer rate with the depot, while smaller magazines
1687*7c478bd9Sstevel@tonic-gate  * reduce memory consumption.  Magazine resizing is an expensive operation;
1688*7c478bd9Sstevel@tonic-gate  * it should not be done frequently.
1689*7c478bd9Sstevel@tonic-gate  *
1690*7c478bd9Sstevel@tonic-gate  * Changes to the magazine size are serialized by the kmem_taskq lock.
1691*7c478bd9Sstevel@tonic-gate  *
1692*7c478bd9Sstevel@tonic-gate  * Note: at present this only grows the magazine size.  It might be useful
1693*7c478bd9Sstevel@tonic-gate  * to allow shrinkage too.
1694*7c478bd9Sstevel@tonic-gate  */
1695*7c478bd9Sstevel@tonic-gate static void
1696*7c478bd9Sstevel@tonic-gate kmem_cache_magazine_resize(kmem_cache_t *cp)
1697*7c478bd9Sstevel@tonic-gate {
1698*7c478bd9Sstevel@tonic-gate 	kmem_magtype_t *mtp = cp->cache_magtype;
1699*7c478bd9Sstevel@tonic-gate 
1700*7c478bd9Sstevel@tonic-gate 	ASSERT(taskq_member(kmem_taskq, curthread));
1701*7c478bd9Sstevel@tonic-gate 
1702*7c478bd9Sstevel@tonic-gate 	if (cp->cache_chunksize < mtp->mt_maxbuf) {
1703*7c478bd9Sstevel@tonic-gate 		kmem_cache_magazine_purge(cp);
1704*7c478bd9Sstevel@tonic-gate 		mutex_enter(&cp->cache_depot_lock);
1705*7c478bd9Sstevel@tonic-gate 		cp->cache_magtype = ++mtp;
1706*7c478bd9Sstevel@tonic-gate 		cp->cache_depot_contention_prev =
1707*7c478bd9Sstevel@tonic-gate 		    cp->cache_depot_contention + INT_MAX;
1708*7c478bd9Sstevel@tonic-gate 		mutex_exit(&cp->cache_depot_lock);
1709*7c478bd9Sstevel@tonic-gate 		kmem_cache_magazine_enable(cp);
1710*7c478bd9Sstevel@tonic-gate 	}
1711*7c478bd9Sstevel@tonic-gate }
1712*7c478bd9Sstevel@tonic-gate 
1713*7c478bd9Sstevel@tonic-gate /*
1714*7c478bd9Sstevel@tonic-gate  * Rescale a cache's hash table, so that the table size is roughly the
1715*7c478bd9Sstevel@tonic-gate  * cache size.  We want the average lookup time to be extremely small.
1716*7c478bd9Sstevel@tonic-gate  */
1717*7c478bd9Sstevel@tonic-gate static void
1718*7c478bd9Sstevel@tonic-gate kmem_hash_rescale(kmem_cache_t *cp)
1719*7c478bd9Sstevel@tonic-gate {
1720*7c478bd9Sstevel@tonic-gate 	kmem_bufctl_t **old_table, **new_table, *bcp;
1721*7c478bd9Sstevel@tonic-gate 	size_t old_size, new_size, h;
1722*7c478bd9Sstevel@tonic-gate 
1723*7c478bd9Sstevel@tonic-gate 	ASSERT(taskq_member(kmem_taskq, curthread));
1724*7c478bd9Sstevel@tonic-gate 
1725*7c478bd9Sstevel@tonic-gate 	new_size = MAX(KMEM_HASH_INITIAL,
1726*7c478bd9Sstevel@tonic-gate 	    1 << (highbit(3 * cp->cache_buftotal + 4) - 2));
1727*7c478bd9Sstevel@tonic-gate 	old_size = cp->cache_hash_mask + 1;
1728*7c478bd9Sstevel@tonic-gate 
1729*7c478bd9Sstevel@tonic-gate 	if ((old_size >> 1) <= new_size && new_size <= (old_size << 1))
1730*7c478bd9Sstevel@tonic-gate 		return;
1731*7c478bd9Sstevel@tonic-gate 
1732*7c478bd9Sstevel@tonic-gate 	new_table = vmem_alloc(kmem_hash_arena, new_size * sizeof (void *),
1733*7c478bd9Sstevel@tonic-gate 	    VM_NOSLEEP);
1734*7c478bd9Sstevel@tonic-gate 	if (new_table == NULL)
1735*7c478bd9Sstevel@tonic-gate 		return;
1736*7c478bd9Sstevel@tonic-gate 	bzero(new_table, new_size * sizeof (void *));
1737*7c478bd9Sstevel@tonic-gate 
1738*7c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->cache_lock);
1739*7c478bd9Sstevel@tonic-gate 
1740*7c478bd9Sstevel@tonic-gate 	old_size = cp->cache_hash_mask + 1;
1741*7c478bd9Sstevel@tonic-gate 	old_table = cp->cache_hash_table;
1742*7c478bd9Sstevel@tonic-gate 
1743*7c478bd9Sstevel@tonic-gate 	cp->cache_hash_mask = new_size - 1;
1744*7c478bd9Sstevel@tonic-gate 	cp->cache_hash_table = new_table;
1745*7c478bd9Sstevel@tonic-gate 	cp->cache_rescale++;
1746*7c478bd9Sstevel@tonic-gate 
1747*7c478bd9Sstevel@tonic-gate 	for (h = 0; h < old_size; h++) {
1748*7c478bd9Sstevel@tonic-gate 		bcp = old_table[h];
1749*7c478bd9Sstevel@tonic-gate 		while (bcp != NULL) {
1750*7c478bd9Sstevel@tonic-gate 			void *addr = bcp->bc_addr;
1751*7c478bd9Sstevel@tonic-gate 			kmem_bufctl_t *next_bcp = bcp->bc_next;
1752*7c478bd9Sstevel@tonic-gate 			kmem_bufctl_t **hash_bucket = KMEM_HASH(cp, addr);
1753*7c478bd9Sstevel@tonic-gate 			bcp->bc_next = *hash_bucket;
1754*7c478bd9Sstevel@tonic-gate 			*hash_bucket = bcp;
1755*7c478bd9Sstevel@tonic-gate 			bcp = next_bcp;
1756*7c478bd9Sstevel@tonic-gate 		}
1757*7c478bd9Sstevel@tonic-gate 	}
1758*7c478bd9Sstevel@tonic-gate 
1759*7c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->cache_lock);
1760*7c478bd9Sstevel@tonic-gate 
1761*7c478bd9Sstevel@tonic-gate 	vmem_free(kmem_hash_arena, old_table, old_size * sizeof (void *));
1762*7c478bd9Sstevel@tonic-gate }
1763*7c478bd9Sstevel@tonic-gate 
1764*7c478bd9Sstevel@tonic-gate /*
1765*7c478bd9Sstevel@tonic-gate  * Perform periodic maintenance on a cache: hash rescaling,
1766*7c478bd9Sstevel@tonic-gate  * depot working-set update, and magazine resizing.
1767*7c478bd9Sstevel@tonic-gate  */
1768*7c478bd9Sstevel@tonic-gate static void
1769*7c478bd9Sstevel@tonic-gate kmem_cache_update(kmem_cache_t *cp)
1770*7c478bd9Sstevel@tonic-gate {
1771*7c478bd9Sstevel@tonic-gate 	int need_hash_rescale = 0;
1772*7c478bd9Sstevel@tonic-gate 	int need_magazine_resize = 0;
1773*7c478bd9Sstevel@tonic-gate 
1774*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&kmem_cache_lock));
1775*7c478bd9Sstevel@tonic-gate 
1776*7c478bd9Sstevel@tonic-gate 	/*
1777*7c478bd9Sstevel@tonic-gate 	 * If the cache has become much larger or smaller than its hash table,
1778*7c478bd9Sstevel@tonic-gate 	 * fire off a request to rescale the hash table.
1779*7c478bd9Sstevel@tonic-gate 	 */
1780*7c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->cache_lock);
1781*7c478bd9Sstevel@tonic-gate 
1782*7c478bd9Sstevel@tonic-gate 	if ((cp->cache_flags & KMF_HASH) &&
1783*7c478bd9Sstevel@tonic-gate 	    (cp->cache_buftotal > (cp->cache_hash_mask << 1) ||
1784*7c478bd9Sstevel@tonic-gate 	    (cp->cache_buftotal < (cp->cache_hash_mask >> 1) &&
1785*7c478bd9Sstevel@tonic-gate 	    cp->cache_hash_mask > KMEM_HASH_INITIAL)))
1786*7c478bd9Sstevel@tonic-gate 		need_hash_rescale = 1;
1787*7c478bd9Sstevel@tonic-gate 
1788*7c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->cache_lock);
1789*7c478bd9Sstevel@tonic-gate 
1790*7c478bd9Sstevel@tonic-gate 	/*
1791*7c478bd9Sstevel@tonic-gate 	 * Update the depot working set statistics.
1792*7c478bd9Sstevel@tonic-gate 	 */
1793*7c478bd9Sstevel@tonic-gate 	kmem_depot_ws_update(cp);
1794*7c478bd9Sstevel@tonic-gate 
1795*7c478bd9Sstevel@tonic-gate 	/*
1796*7c478bd9Sstevel@tonic-gate 	 * If there's a lot of contention in the depot,
1797*7c478bd9Sstevel@tonic-gate 	 * increase the magazine size.
1798*7c478bd9Sstevel@tonic-gate 	 */
1799*7c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->cache_depot_lock);
1800*7c478bd9Sstevel@tonic-gate 
1801*7c478bd9Sstevel@tonic-gate 	if (cp->cache_chunksize < cp->cache_magtype->mt_maxbuf &&
1802*7c478bd9Sstevel@tonic-gate 	    (int)(cp->cache_depot_contention -
1803*7c478bd9Sstevel@tonic-gate 	    cp->cache_depot_contention_prev) > kmem_depot_contention)
1804*7c478bd9Sstevel@tonic-gate 		need_magazine_resize = 1;
1805*7c478bd9Sstevel@tonic-gate 
1806*7c478bd9Sstevel@tonic-gate 	cp->cache_depot_contention_prev = cp->cache_depot_contention;
1807*7c478bd9Sstevel@tonic-gate 
1808*7c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->cache_depot_lock);
1809*7c478bd9Sstevel@tonic-gate 
1810*7c478bd9Sstevel@tonic-gate 	if (need_hash_rescale)
1811*7c478bd9Sstevel@tonic-gate 		(void) taskq_dispatch(kmem_taskq,
1812*7c478bd9Sstevel@tonic-gate 		    (task_func_t *)kmem_hash_rescale, cp, TQ_NOSLEEP);
1813*7c478bd9Sstevel@tonic-gate 
1814*7c478bd9Sstevel@tonic-gate 	if (need_magazine_resize)
1815*7c478bd9Sstevel@tonic-gate 		(void) taskq_dispatch(kmem_taskq,
1816*7c478bd9Sstevel@tonic-gate 		    (task_func_t *)kmem_cache_magazine_resize, cp, TQ_NOSLEEP);
1817*7c478bd9Sstevel@tonic-gate }
1818*7c478bd9Sstevel@tonic-gate 
1819*7c478bd9Sstevel@tonic-gate static void
1820*7c478bd9Sstevel@tonic-gate kmem_update_timeout(void *dummy)
1821*7c478bd9Sstevel@tonic-gate {
1822*7c478bd9Sstevel@tonic-gate 	static void kmem_update(void *);
1823*7c478bd9Sstevel@tonic-gate 
1824*7c478bd9Sstevel@tonic-gate 	(void) timeout(kmem_update, dummy, kmem_reap_interval);
1825*7c478bd9Sstevel@tonic-gate }
1826*7c478bd9Sstevel@tonic-gate 
1827*7c478bd9Sstevel@tonic-gate static void
1828*7c478bd9Sstevel@tonic-gate kmem_update(void *dummy)
1829*7c478bd9Sstevel@tonic-gate {
1830*7c478bd9Sstevel@tonic-gate 	kmem_cache_applyall(kmem_cache_update, NULL, TQ_NOSLEEP);
1831*7c478bd9Sstevel@tonic-gate 
1832*7c478bd9Sstevel@tonic-gate 	/*
1833*7c478bd9Sstevel@tonic-gate 	 * We use taskq_dispatch() to reschedule the timeout so that
1834*7c478bd9Sstevel@tonic-gate 	 * kmem_update() becomes self-throttling: it won't schedule
1835*7c478bd9Sstevel@tonic-gate 	 * new tasks until all previous tasks have completed.
1836*7c478bd9Sstevel@tonic-gate 	 */
1837*7c478bd9Sstevel@tonic-gate 	if (!taskq_dispatch(kmem_taskq, kmem_update_timeout, dummy, TQ_NOSLEEP))
1838*7c478bd9Sstevel@tonic-gate 		kmem_update_timeout(NULL);
1839*7c478bd9Sstevel@tonic-gate }
1840*7c478bd9Sstevel@tonic-gate 
1841*7c478bd9Sstevel@tonic-gate static int
1842*7c478bd9Sstevel@tonic-gate kmem_cache_kstat_update(kstat_t *ksp, int rw)
1843*7c478bd9Sstevel@tonic-gate {
1844*7c478bd9Sstevel@tonic-gate 	struct kmem_cache_kstat *kmcp = &kmem_cache_kstat;
1845*7c478bd9Sstevel@tonic-gate 	kmem_cache_t *cp = ksp->ks_private;
1846*7c478bd9Sstevel@tonic-gate 	kmem_slab_t *sp;
1847*7c478bd9Sstevel@tonic-gate 	uint64_t cpu_buf_avail;
1848*7c478bd9Sstevel@tonic-gate 	uint64_t buf_avail = 0;
1849*7c478bd9Sstevel@tonic-gate 	int cpu_seqid;
1850*7c478bd9Sstevel@tonic-gate 
1851*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&kmem_cache_kstat_lock));
1852*7c478bd9Sstevel@tonic-gate 
1853*7c478bd9Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
1854*7c478bd9Sstevel@tonic-gate 		return (EACCES);
1855*7c478bd9Sstevel@tonic-gate 
1856*7c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->cache_lock);
1857*7c478bd9Sstevel@tonic-gate 
1858*7c478bd9Sstevel@tonic-gate 	kmcp->kmc_alloc_fail.value.ui64		= cp->cache_alloc_fail;
1859*7c478bd9Sstevel@tonic-gate 	kmcp->kmc_alloc.value.ui64		= cp->cache_slab_alloc;
1860*7c478bd9Sstevel@tonic-gate 	kmcp->kmc_free.value.ui64		= cp->cache_slab_free;
1861*7c478bd9Sstevel@tonic-gate 	kmcp->kmc_slab_alloc.value.ui64		= cp->cache_slab_alloc;
1862*7c478bd9Sstevel@tonic-gate 	kmcp->kmc_slab_free.value.ui64		= cp->cache_slab_free;
1863*7c478bd9Sstevel@tonic-gate 
1864*7c478bd9Sstevel@tonic-gate 	for (cpu_seqid = 0; cpu_seqid < max_ncpus; cpu_seqid++) {
1865*7c478bd9Sstevel@tonic-gate 		kmem_cpu_cache_t *ccp = &cp->cache_cpu[cpu_seqid];
1866*7c478bd9Sstevel@tonic-gate 
1867*7c478bd9Sstevel@tonic-gate 		mutex_enter(&ccp->cc_lock);
1868*7c478bd9Sstevel@tonic-gate 
1869*7c478bd9Sstevel@tonic-gate 		cpu_buf_avail = 0;
1870*7c478bd9Sstevel@tonic-gate 		if (ccp->cc_rounds > 0)
1871*7c478bd9Sstevel@tonic-gate 			cpu_buf_avail += ccp->cc_rounds;
1872*7c478bd9Sstevel@tonic-gate 		if (ccp->cc_prounds > 0)
1873*7c478bd9Sstevel@tonic-gate 			cpu_buf_avail += ccp->cc_prounds;
1874*7c478bd9Sstevel@tonic-gate 
1875*7c478bd9Sstevel@tonic-gate 		kmcp->kmc_alloc.value.ui64	+= ccp->cc_alloc;
1876*7c478bd9Sstevel@tonic-gate 		kmcp->kmc_free.value.ui64	+= ccp->cc_free;
1877*7c478bd9Sstevel@tonic-gate 		buf_avail			+= cpu_buf_avail;
1878*7c478bd9Sstevel@tonic-gate 
1879*7c478bd9Sstevel@tonic-gate 		mutex_exit(&ccp->cc_lock);
1880*7c478bd9Sstevel@tonic-gate 	}
1881*7c478bd9Sstevel@tonic-gate 
1882*7c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->cache_depot_lock);
1883*7c478bd9Sstevel@tonic-gate 
1884*7c478bd9Sstevel@tonic-gate 	kmcp->kmc_depot_alloc.value.ui64	= cp->cache_full.ml_alloc;
1885*7c478bd9Sstevel@tonic-gate 	kmcp->kmc_depot_free.value.ui64		= cp->cache_empty.ml_alloc;
1886*7c478bd9Sstevel@tonic-gate 	kmcp->kmc_depot_contention.value.ui64	= cp->cache_depot_contention;
1887*7c478bd9Sstevel@tonic-gate 	kmcp->kmc_full_magazines.value.ui64	= cp->cache_full.ml_total;
1888*7c478bd9Sstevel@tonic-gate 	kmcp->kmc_empty_magazines.value.ui64	= cp->cache_empty.ml_total;
1889*7c478bd9Sstevel@tonic-gate 	kmcp->kmc_magazine_size.value.ui64	=
1890*7c478bd9Sstevel@tonic-gate 	    (cp->cache_flags & KMF_NOMAGAZINE) ?
1891*7c478bd9Sstevel@tonic-gate 	    0 : cp->cache_magtype->mt_magsize;
1892*7c478bd9Sstevel@tonic-gate 
1893*7c478bd9Sstevel@tonic-gate 	kmcp->kmc_alloc.value.ui64		+= cp->cache_full.ml_alloc;
1894*7c478bd9Sstevel@tonic-gate 	kmcp->kmc_free.value.ui64		+= cp->cache_empty.ml_alloc;
1895*7c478bd9Sstevel@tonic-gate 	buf_avail += cp->cache_full.ml_total * cp->cache_magtype->mt_magsize;
1896*7c478bd9Sstevel@tonic-gate 
1897*7c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->cache_depot_lock);
1898*7c478bd9Sstevel@tonic-gate 
1899*7c478bd9Sstevel@tonic-gate 	kmcp->kmc_buf_size.value.ui64	= cp->cache_bufsize;
1900*7c478bd9Sstevel@tonic-gate 	kmcp->kmc_align.value.ui64	= cp->cache_align;
1901*7c478bd9Sstevel@tonic-gate 	kmcp->kmc_chunk_size.value.ui64	= cp->cache_chunksize;
1902*7c478bd9Sstevel@tonic-gate 	kmcp->kmc_slab_size.value.ui64	= cp->cache_slabsize;
1903*7c478bd9Sstevel@tonic-gate 	kmcp->kmc_buf_constructed.value.ui64 = buf_avail;
1904*7c478bd9Sstevel@tonic-gate 	for (sp = cp->cache_freelist; sp != &cp->cache_nullslab;
1905*7c478bd9Sstevel@tonic-gate 	    sp = sp->slab_next)
1906*7c478bd9Sstevel@tonic-gate 		buf_avail += sp->slab_chunks - sp->slab_refcnt;
1907*7c478bd9Sstevel@tonic-gate 	kmcp->kmc_buf_avail.value.ui64	= buf_avail;
1908*7c478bd9Sstevel@tonic-gate 	kmcp->kmc_buf_inuse.value.ui64	= cp->cache_buftotal - buf_avail;
1909*7c478bd9Sstevel@tonic-gate 	kmcp->kmc_buf_total.value.ui64	= cp->cache_buftotal;
1910*7c478bd9Sstevel@tonic-gate 	kmcp->kmc_buf_max.value.ui64	= cp->cache_bufmax;
1911*7c478bd9Sstevel@tonic-gate 	kmcp->kmc_slab_create.value.ui64	= cp->cache_slab_create;
1912*7c478bd9Sstevel@tonic-gate 	kmcp->kmc_slab_destroy.value.ui64	= cp->cache_slab_destroy;
1913*7c478bd9Sstevel@tonic-gate 	kmcp->kmc_hash_size.value.ui64	= (cp->cache_flags & KMF_HASH) ?
1914*7c478bd9Sstevel@tonic-gate 	    cp->cache_hash_mask + 1 : 0;
1915*7c478bd9Sstevel@tonic-gate 	kmcp->kmc_hash_lookup_depth.value.ui64	= cp->cache_lookup_depth;
1916*7c478bd9Sstevel@tonic-gate 	kmcp->kmc_hash_rescale.value.ui64	= cp->cache_rescale;
1917*7c478bd9Sstevel@tonic-gate 	kmcp->kmc_vmem_source.value.ui64	= cp->cache_arena->vm_id;
1918*7c478bd9Sstevel@tonic-gate 
1919*7c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->cache_lock);
1920*7c478bd9Sstevel@tonic-gate 	return (0);
1921*7c478bd9Sstevel@tonic-gate }
1922*7c478bd9Sstevel@tonic-gate 
1923*7c478bd9Sstevel@tonic-gate /*
1924*7c478bd9Sstevel@tonic-gate  * Return a named statistic about a particular cache.
1925*7c478bd9Sstevel@tonic-gate  * This shouldn't be called very often, so it's currently designed for
1926*7c478bd9Sstevel@tonic-gate  * simplicity (leverages existing kstat support) rather than efficiency.
1927*7c478bd9Sstevel@tonic-gate  */
1928*7c478bd9Sstevel@tonic-gate uint64_t
1929*7c478bd9Sstevel@tonic-gate kmem_cache_stat(kmem_cache_t *cp, char *name)
1930*7c478bd9Sstevel@tonic-gate {
1931*7c478bd9Sstevel@tonic-gate 	int i;
1932*7c478bd9Sstevel@tonic-gate 	kstat_t *ksp = cp->cache_kstat;
1933*7c478bd9Sstevel@tonic-gate 	kstat_named_t *knp = (kstat_named_t *)&kmem_cache_kstat;
1934*7c478bd9Sstevel@tonic-gate 	uint64_t value = 0;
1935*7c478bd9Sstevel@tonic-gate 
1936*7c478bd9Sstevel@tonic-gate 	if (ksp != NULL) {
1937*7c478bd9Sstevel@tonic-gate 		mutex_enter(&kmem_cache_kstat_lock);
1938*7c478bd9Sstevel@tonic-gate 		(void) kmem_cache_kstat_update(ksp, KSTAT_READ);
1939*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < ksp->ks_ndata; i++) {
1940*7c478bd9Sstevel@tonic-gate 			if (strcmp(knp[i].name, name) == 0) {
1941*7c478bd9Sstevel@tonic-gate 				value = knp[i].value.ui64;
1942*7c478bd9Sstevel@tonic-gate 				break;
1943*7c478bd9Sstevel@tonic-gate 			}
1944*7c478bd9Sstevel@tonic-gate 		}
1945*7c478bd9Sstevel@tonic-gate 		mutex_exit(&kmem_cache_kstat_lock);
1946*7c478bd9Sstevel@tonic-gate 	}
1947*7c478bd9Sstevel@tonic-gate 	return (value);
1948*7c478bd9Sstevel@tonic-gate }
1949*7c478bd9Sstevel@tonic-gate 
1950*7c478bd9Sstevel@tonic-gate /*
1951*7c478bd9Sstevel@tonic-gate  * Return an estimate of currently available kernel heap memory.
1952*7c478bd9Sstevel@tonic-gate  * On 32-bit systems, physical memory may exceed virtual memory,
1953*7c478bd9Sstevel@tonic-gate  * we just truncate the result at 1GB.
1954*7c478bd9Sstevel@tonic-gate  */
1955*7c478bd9Sstevel@tonic-gate size_t
1956*7c478bd9Sstevel@tonic-gate kmem_avail(void)
1957*7c478bd9Sstevel@tonic-gate {
1958*7c478bd9Sstevel@tonic-gate 	spgcnt_t rmem = availrmem - tune.t_minarmem;
1959*7c478bd9Sstevel@tonic-gate 	spgcnt_t fmem = freemem - minfree;
1960*7c478bd9Sstevel@tonic-gate 
1961*7c478bd9Sstevel@tonic-gate 	return ((size_t)ptob(MIN(MAX(MIN(rmem, fmem), 0),
1962*7c478bd9Sstevel@tonic-gate 	    1 << (30 - PAGESHIFT))));
1963*7c478bd9Sstevel@tonic-gate }
1964*7c478bd9Sstevel@tonic-gate 
1965*7c478bd9Sstevel@tonic-gate /*
1966*7c478bd9Sstevel@tonic-gate  * Return the maximum amount of memory that is (in theory) allocatable
1967*7c478bd9Sstevel@tonic-gate  * from the heap. This may be used as an estimate only since there
1968*7c478bd9Sstevel@tonic-gate  * is no guarentee this space will still be available when an allocation
1969*7c478bd9Sstevel@tonic-gate  * request is made, nor that the space may be allocated in one big request
1970*7c478bd9Sstevel@tonic-gate  * due to kernel heap fragmentation.
1971*7c478bd9Sstevel@tonic-gate  */
1972*7c478bd9Sstevel@tonic-gate size_t
1973*7c478bd9Sstevel@tonic-gate kmem_maxavail(void)
1974*7c478bd9Sstevel@tonic-gate {
1975*7c478bd9Sstevel@tonic-gate 	spgcnt_t pmem = availrmem - tune.t_minarmem;
1976*7c478bd9Sstevel@tonic-gate 	spgcnt_t vmem = btop(vmem_size(heap_arena, VMEM_FREE));
1977*7c478bd9Sstevel@tonic-gate 
1978*7c478bd9Sstevel@tonic-gate 	return ((size_t)ptob(MAX(MIN(pmem, vmem), 0)));
1979*7c478bd9Sstevel@tonic-gate }
1980*7c478bd9Sstevel@tonic-gate 
1981*7c478bd9Sstevel@tonic-gate kmem_cache_t *
1982*7c478bd9Sstevel@tonic-gate kmem_cache_create(
1983*7c478bd9Sstevel@tonic-gate 	char *name,		/* descriptive name for this cache */
1984*7c478bd9Sstevel@tonic-gate 	size_t bufsize,		/* size of the objects it manages */
1985*7c478bd9Sstevel@tonic-gate 	size_t align,		/* required object alignment */
1986*7c478bd9Sstevel@tonic-gate 	int (*constructor)(void *, void *, int), /* object constructor */
1987*7c478bd9Sstevel@tonic-gate 	void (*destructor)(void *, void *),	/* object destructor */
1988*7c478bd9Sstevel@tonic-gate 	void (*reclaim)(void *), /* memory reclaim callback */
1989*7c478bd9Sstevel@tonic-gate 	void *private,		/* pass-thru arg for constr/destr/reclaim */
1990*7c478bd9Sstevel@tonic-gate 	vmem_t *vmp,		/* vmem source for slab allocation */
1991*7c478bd9Sstevel@tonic-gate 	int cflags)		/* cache creation flags */
1992*7c478bd9Sstevel@tonic-gate {
1993*7c478bd9Sstevel@tonic-gate 	int cpu_seqid;
1994*7c478bd9Sstevel@tonic-gate 	size_t chunksize;
1995*7c478bd9Sstevel@tonic-gate 	kmem_cache_t *cp, *cnext, *cprev;
1996*7c478bd9Sstevel@tonic-gate 	kmem_magtype_t *mtp;
1997*7c478bd9Sstevel@tonic-gate 	size_t csize = KMEM_CACHE_SIZE(max_ncpus);
1998*7c478bd9Sstevel@tonic-gate 
1999*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
2000*7c478bd9Sstevel@tonic-gate 	/*
2001*7c478bd9Sstevel@tonic-gate 	 * Cache names should conform to the rules for valid C identifiers
2002*7c478bd9Sstevel@tonic-gate 	 */
2003*7c478bd9Sstevel@tonic-gate 	if (!strident_valid(name)) {
2004*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT,
2005*7c478bd9Sstevel@tonic-gate 		    "kmem_cache_create: '%s' is an invalid cache name\n"
2006*7c478bd9Sstevel@tonic-gate 		    "cache names must conform to the rules for "
2007*7c478bd9Sstevel@tonic-gate 		    "C identifiers\n", name);
2008*7c478bd9Sstevel@tonic-gate 	}
2009*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
2010*7c478bd9Sstevel@tonic-gate 
2011*7c478bd9Sstevel@tonic-gate 	if (vmp == NULL)
2012*7c478bd9Sstevel@tonic-gate 		vmp = kmem_default_arena;
2013*7c478bd9Sstevel@tonic-gate 
2014*7c478bd9Sstevel@tonic-gate 	/*
2015*7c478bd9Sstevel@tonic-gate 	 * If this kmem cache has an identifier vmem arena as its source, mark
2016*7c478bd9Sstevel@tonic-gate 	 * it such to allow kmem_reap_idspace().
2017*7c478bd9Sstevel@tonic-gate 	 */
2018*7c478bd9Sstevel@tonic-gate 	ASSERT(!(cflags & KMC_IDENTIFIER));   /* consumer should not set this */
2019*7c478bd9Sstevel@tonic-gate 	if (vmp->vm_cflags & VMC_IDENTIFIER)
2020*7c478bd9Sstevel@tonic-gate 		cflags |= KMC_IDENTIFIER;
2021*7c478bd9Sstevel@tonic-gate 
2022*7c478bd9Sstevel@tonic-gate 	/*
2023*7c478bd9Sstevel@tonic-gate 	 * Get a kmem_cache structure.  We arrange that cp->cache_cpu[]
2024*7c478bd9Sstevel@tonic-gate 	 * is aligned on a KMEM_CPU_CACHE_SIZE boundary to prevent
2025*7c478bd9Sstevel@tonic-gate 	 * false sharing of per-CPU data.
2026*7c478bd9Sstevel@tonic-gate 	 */
2027*7c478bd9Sstevel@tonic-gate 	cp = vmem_xalloc(kmem_cache_arena, csize, KMEM_CPU_CACHE_SIZE,
2028*7c478bd9Sstevel@tonic-gate 	    P2NPHASE(csize, KMEM_CPU_CACHE_SIZE), 0, NULL, NULL, VM_SLEEP);
2029*7c478bd9Sstevel@tonic-gate 	bzero(cp, csize);
2030*7c478bd9Sstevel@tonic-gate 
2031*7c478bd9Sstevel@tonic-gate 	if (align == 0)
2032*7c478bd9Sstevel@tonic-gate 		align = KMEM_ALIGN;
2033*7c478bd9Sstevel@tonic-gate 
2034*7c478bd9Sstevel@tonic-gate 	/*
2035*7c478bd9Sstevel@tonic-gate 	 * If we're not at least KMEM_ALIGN aligned, we can't use free
2036*7c478bd9Sstevel@tonic-gate 	 * memory to hold bufctl information (because we can't safely
2037*7c478bd9Sstevel@tonic-gate 	 * perform word loads and stores on it).
2038*7c478bd9Sstevel@tonic-gate 	 */
2039*7c478bd9Sstevel@tonic-gate 	if (align < KMEM_ALIGN)
2040*7c478bd9Sstevel@tonic-gate 		cflags |= KMC_NOTOUCH;
2041*7c478bd9Sstevel@tonic-gate 
2042*7c478bd9Sstevel@tonic-gate 	if ((align & (align - 1)) != 0 || align > vmp->vm_quantum)
2043*7c478bd9Sstevel@tonic-gate 		panic("kmem_cache_create: bad alignment %lu", align);
2044*7c478bd9Sstevel@tonic-gate 
2045*7c478bd9Sstevel@tonic-gate 	mutex_enter(&kmem_flags_lock);
2046*7c478bd9Sstevel@tonic-gate 	if (kmem_flags & KMF_RANDOMIZE)
2047*7c478bd9Sstevel@tonic-gate 		kmem_flags = (((kmem_flags | ~KMF_RANDOM) + 1) & KMF_RANDOM) |
2048*7c478bd9Sstevel@tonic-gate 		    KMF_RANDOMIZE;
2049*7c478bd9Sstevel@tonic-gate 	cp->cache_flags = (kmem_flags | cflags) & KMF_DEBUG;
2050*7c478bd9Sstevel@tonic-gate 	mutex_exit(&kmem_flags_lock);
2051*7c478bd9Sstevel@tonic-gate 
2052*7c478bd9Sstevel@tonic-gate 	/*
2053*7c478bd9Sstevel@tonic-gate 	 * Make sure all the various flags are reasonable.
2054*7c478bd9Sstevel@tonic-gate 	 */
2055*7c478bd9Sstevel@tonic-gate 	ASSERT(!(cflags & KMC_NOHASH) || !(cflags & KMC_NOTOUCH));
2056*7c478bd9Sstevel@tonic-gate 
2057*7c478bd9Sstevel@tonic-gate 	if (cp->cache_flags & KMF_LITE) {
2058*7c478bd9Sstevel@tonic-gate 		if (bufsize >= kmem_lite_minsize &&
2059*7c478bd9Sstevel@tonic-gate 		    align <= kmem_lite_maxalign &&
2060*7c478bd9Sstevel@tonic-gate 		    P2PHASE(bufsize, kmem_lite_maxalign) != 0) {
2061*7c478bd9Sstevel@tonic-gate 			cp->cache_flags |= KMF_BUFTAG;
2062*7c478bd9Sstevel@tonic-gate 			cp->cache_flags &= ~(KMF_AUDIT | KMF_FIREWALL);
2063*7c478bd9Sstevel@tonic-gate 		} else {
2064*7c478bd9Sstevel@tonic-gate 			cp->cache_flags &= ~KMF_DEBUG;
2065*7c478bd9Sstevel@tonic-gate 		}
2066*7c478bd9Sstevel@tonic-gate 	}
2067*7c478bd9Sstevel@tonic-gate 
2068*7c478bd9Sstevel@tonic-gate 	if (cp->cache_flags & KMF_DEADBEEF)
2069*7c478bd9Sstevel@tonic-gate 		cp->cache_flags |= KMF_REDZONE;
2070*7c478bd9Sstevel@tonic-gate 
2071*7c478bd9Sstevel@tonic-gate 	if ((cflags & KMC_QCACHE) && (cp->cache_flags & KMF_AUDIT))
2072*7c478bd9Sstevel@tonic-gate 		cp->cache_flags |= KMF_NOMAGAZINE;
2073*7c478bd9Sstevel@tonic-gate 
2074*7c478bd9Sstevel@tonic-gate 	if (cflags & KMC_NODEBUG)
2075*7c478bd9Sstevel@tonic-gate 		cp->cache_flags &= ~KMF_DEBUG;
2076*7c478bd9Sstevel@tonic-gate 
2077*7c478bd9Sstevel@tonic-gate 	if (cflags & KMC_NOTOUCH)
2078*7c478bd9Sstevel@tonic-gate 		cp->cache_flags &= ~KMF_TOUCH;
2079*7c478bd9Sstevel@tonic-gate 
2080*7c478bd9Sstevel@tonic-gate 	if (cflags & KMC_NOHASH)
2081*7c478bd9Sstevel@tonic-gate 		cp->cache_flags &= ~(KMF_AUDIT | KMF_FIREWALL);
2082*7c478bd9Sstevel@tonic-gate 
2083*7c478bd9Sstevel@tonic-gate 	if (cflags & KMC_NOMAGAZINE)
2084*7c478bd9Sstevel@tonic-gate 		cp->cache_flags |= KMF_NOMAGAZINE;
2085*7c478bd9Sstevel@tonic-gate 
2086*7c478bd9Sstevel@tonic-gate 	if ((cp->cache_flags & KMF_AUDIT) && !(cflags & KMC_NOTOUCH))
2087*7c478bd9Sstevel@tonic-gate 		cp->cache_flags |= KMF_REDZONE;
2088*7c478bd9Sstevel@tonic-gate 
2089*7c478bd9Sstevel@tonic-gate 	if (!(cp->cache_flags & KMF_AUDIT))
2090*7c478bd9Sstevel@tonic-gate 		cp->cache_flags &= ~KMF_CONTENTS;
2091*7c478bd9Sstevel@tonic-gate 
2092*7c478bd9Sstevel@tonic-gate 	if ((cp->cache_flags & KMF_BUFTAG) && bufsize >= kmem_minfirewall &&
2093*7c478bd9Sstevel@tonic-gate 	    !(cp->cache_flags & KMF_LITE) && !(cflags & KMC_NOHASH))
2094*7c478bd9Sstevel@tonic-gate 		cp->cache_flags |= KMF_FIREWALL;
2095*7c478bd9Sstevel@tonic-gate 
2096*7c478bd9Sstevel@tonic-gate 	if (vmp != kmem_default_arena || kmem_firewall_arena == NULL)
2097*7c478bd9Sstevel@tonic-gate 		cp->cache_flags &= ~KMF_FIREWALL;
2098*7c478bd9Sstevel@tonic-gate 
2099*7c478bd9Sstevel@tonic-gate 	if (cp->cache_flags & KMF_FIREWALL) {
2100*7c478bd9Sstevel@tonic-gate 		cp->cache_flags &= ~KMF_BUFTAG;
2101*7c478bd9Sstevel@tonic-gate 		cp->cache_flags |= KMF_NOMAGAZINE;
2102*7c478bd9Sstevel@tonic-gate 		ASSERT(vmp == kmem_default_arena);
2103*7c478bd9Sstevel@tonic-gate 		vmp = kmem_firewall_arena;
2104*7c478bd9Sstevel@tonic-gate 	}
2105*7c478bd9Sstevel@tonic-gate 
2106*7c478bd9Sstevel@tonic-gate 	/*
2107*7c478bd9Sstevel@tonic-gate 	 * Set cache properties.
2108*7c478bd9Sstevel@tonic-gate 	 */
2109*7c478bd9Sstevel@tonic-gate 	(void) strncpy(cp->cache_name, name, KMEM_CACHE_NAMELEN);
2110*7c478bd9Sstevel@tonic-gate 	strident_canon(cp->cache_name, KMEM_CACHE_NAMELEN);
2111*7c478bd9Sstevel@tonic-gate 	cp->cache_bufsize = bufsize;
2112*7c478bd9Sstevel@tonic-gate 	cp->cache_align = align;
2113*7c478bd9Sstevel@tonic-gate 	cp->cache_constructor = constructor;
2114*7c478bd9Sstevel@tonic-gate 	cp->cache_destructor = destructor;
2115*7c478bd9Sstevel@tonic-gate 	cp->cache_reclaim = reclaim;
2116*7c478bd9Sstevel@tonic-gate 	cp->cache_private = private;
2117*7c478bd9Sstevel@tonic-gate 	cp->cache_arena = vmp;
2118*7c478bd9Sstevel@tonic-gate 	cp->cache_cflags = cflags;
2119*7c478bd9Sstevel@tonic-gate 
2120*7c478bd9Sstevel@tonic-gate 	/*
2121*7c478bd9Sstevel@tonic-gate 	 * Determine the chunk size.
2122*7c478bd9Sstevel@tonic-gate 	 */
2123*7c478bd9Sstevel@tonic-gate 	chunksize = bufsize;
2124*7c478bd9Sstevel@tonic-gate 
2125*7c478bd9Sstevel@tonic-gate 	if (align >= KMEM_ALIGN) {
2126*7c478bd9Sstevel@tonic-gate 		chunksize = P2ROUNDUP(chunksize, KMEM_ALIGN);
2127*7c478bd9Sstevel@tonic-gate 		cp->cache_bufctl = chunksize - KMEM_ALIGN;
2128*7c478bd9Sstevel@tonic-gate 	}
2129*7c478bd9Sstevel@tonic-gate 
2130*7c478bd9Sstevel@tonic-gate 	if (cp->cache_flags & KMF_BUFTAG) {
2131*7c478bd9Sstevel@tonic-gate 		cp->cache_bufctl = chunksize;
2132*7c478bd9Sstevel@tonic-gate 		cp->cache_buftag = chunksize;
2133*7c478bd9Sstevel@tonic-gate 		if (cp->cache_flags & KMF_LITE)
2134*7c478bd9Sstevel@tonic-gate 			chunksize += KMEM_BUFTAG_LITE_SIZE(kmem_lite_count);
2135*7c478bd9Sstevel@tonic-gate 		else
2136*7c478bd9Sstevel@tonic-gate 			chunksize += sizeof (kmem_buftag_t);
2137*7c478bd9Sstevel@tonic-gate 	}
2138*7c478bd9Sstevel@tonic-gate 
2139*7c478bd9Sstevel@tonic-gate 	if (cp->cache_flags & KMF_DEADBEEF) {
2140*7c478bd9Sstevel@tonic-gate 		cp->cache_verify = MIN(cp->cache_buftag, kmem_maxverify);
2141*7c478bd9Sstevel@tonic-gate 		if (cp->cache_flags & KMF_LITE)
2142*7c478bd9Sstevel@tonic-gate 			cp->cache_verify = sizeof (uint64_t);
2143*7c478bd9Sstevel@tonic-gate 	}
2144*7c478bd9Sstevel@tonic-gate 
2145*7c478bd9Sstevel@tonic-gate 	cp->cache_contents = MIN(cp->cache_bufctl, kmem_content_maxsave);
2146*7c478bd9Sstevel@tonic-gate 
2147*7c478bd9Sstevel@tonic-gate 	cp->cache_chunksize = chunksize = P2ROUNDUP(chunksize, align);
2148*7c478bd9Sstevel@tonic-gate 
2149*7c478bd9Sstevel@tonic-gate 	/*
2150*7c478bd9Sstevel@tonic-gate 	 * Now that we know the chunk size, determine the optimal slab size.
2151*7c478bd9Sstevel@tonic-gate 	 */
2152*7c478bd9Sstevel@tonic-gate 	if (vmp == kmem_firewall_arena) {
2153*7c478bd9Sstevel@tonic-gate 		cp->cache_slabsize = P2ROUNDUP(chunksize, vmp->vm_quantum);
2154*7c478bd9Sstevel@tonic-gate 		cp->cache_mincolor = cp->cache_slabsize - chunksize;
2155*7c478bd9Sstevel@tonic-gate 		cp->cache_maxcolor = cp->cache_mincolor;
2156*7c478bd9Sstevel@tonic-gate 		cp->cache_flags |= KMF_HASH;
2157*7c478bd9Sstevel@tonic-gate 		ASSERT(!(cp->cache_flags & KMF_BUFTAG));
2158*7c478bd9Sstevel@tonic-gate 	} else if ((cflags & KMC_NOHASH) || (!(cflags & KMC_NOTOUCH) &&
2159*7c478bd9Sstevel@tonic-gate 	    !(cp->cache_flags & KMF_AUDIT) &&
2160*7c478bd9Sstevel@tonic-gate 	    chunksize < vmp->vm_quantum / KMEM_VOID_FRACTION)) {
2161*7c478bd9Sstevel@tonic-gate 		cp->cache_slabsize = vmp->vm_quantum;
2162*7c478bd9Sstevel@tonic-gate 		cp->cache_mincolor = 0;
2163*7c478bd9Sstevel@tonic-gate 		cp->cache_maxcolor =
2164*7c478bd9Sstevel@tonic-gate 		    (cp->cache_slabsize - sizeof (kmem_slab_t)) % chunksize;
2165*7c478bd9Sstevel@tonic-gate 		ASSERT(chunksize + sizeof (kmem_slab_t) <= cp->cache_slabsize);
2166*7c478bd9Sstevel@tonic-gate 		ASSERT(!(cp->cache_flags & KMF_AUDIT));
2167*7c478bd9Sstevel@tonic-gate 	} else {
2168*7c478bd9Sstevel@tonic-gate 		size_t chunks, bestfit, waste, slabsize;
2169*7c478bd9Sstevel@tonic-gate 		size_t minwaste = LONG_MAX;
2170*7c478bd9Sstevel@tonic-gate 
2171*7c478bd9Sstevel@tonic-gate 		for (chunks = 1; chunks <= KMEM_VOID_FRACTION; chunks++) {
2172*7c478bd9Sstevel@tonic-gate 			slabsize = P2ROUNDUP(chunksize * chunks,
2173*7c478bd9Sstevel@tonic-gate 			    vmp->vm_quantum);
2174*7c478bd9Sstevel@tonic-gate 			chunks = slabsize / chunksize;
2175*7c478bd9Sstevel@tonic-gate 			waste = (slabsize % chunksize) / chunks;
2176*7c478bd9Sstevel@tonic-gate 			if (waste < minwaste) {
2177*7c478bd9Sstevel@tonic-gate 				minwaste = waste;
2178*7c478bd9Sstevel@tonic-gate 				bestfit = slabsize;
2179*7c478bd9Sstevel@tonic-gate 			}
2180*7c478bd9Sstevel@tonic-gate 		}
2181*7c478bd9Sstevel@tonic-gate 		if (cflags & KMC_QCACHE)
2182*7c478bd9Sstevel@tonic-gate 			bestfit = VMEM_QCACHE_SLABSIZE(vmp->vm_qcache_max);
2183*7c478bd9Sstevel@tonic-gate 		cp->cache_slabsize = bestfit;
2184*7c478bd9Sstevel@tonic-gate 		cp->cache_mincolor = 0;
2185*7c478bd9Sstevel@tonic-gate 		cp->cache_maxcolor = bestfit % chunksize;
2186*7c478bd9Sstevel@tonic-gate 		cp->cache_flags |= KMF_HASH;
2187*7c478bd9Sstevel@tonic-gate 	}
2188*7c478bd9Sstevel@tonic-gate 
2189*7c478bd9Sstevel@tonic-gate 	if (cp->cache_flags & KMF_HASH) {
2190*7c478bd9Sstevel@tonic-gate 		ASSERT(!(cflags & KMC_NOHASH));
2191*7c478bd9Sstevel@tonic-gate 		cp->cache_bufctl_cache = (cp->cache_flags & KMF_AUDIT) ?
2192*7c478bd9Sstevel@tonic-gate 		    kmem_bufctl_audit_cache : kmem_bufctl_cache;
2193*7c478bd9Sstevel@tonic-gate 	}
2194*7c478bd9Sstevel@tonic-gate 
2195*7c478bd9Sstevel@tonic-gate 	if (cp->cache_maxcolor >= vmp->vm_quantum)
2196*7c478bd9Sstevel@tonic-gate 		cp->cache_maxcolor = vmp->vm_quantum - 1;
2197*7c478bd9Sstevel@tonic-gate 
2198*7c478bd9Sstevel@tonic-gate 	cp->cache_color = cp->cache_mincolor;
2199*7c478bd9Sstevel@tonic-gate 
2200*7c478bd9Sstevel@tonic-gate 	/*
2201*7c478bd9Sstevel@tonic-gate 	 * Initialize the rest of the slab layer.
2202*7c478bd9Sstevel@tonic-gate 	 */
2203*7c478bd9Sstevel@tonic-gate 	mutex_init(&cp->cache_lock, NULL, MUTEX_DEFAULT, NULL);
2204*7c478bd9Sstevel@tonic-gate 
2205*7c478bd9Sstevel@tonic-gate 	cp->cache_freelist = &cp->cache_nullslab;
2206*7c478bd9Sstevel@tonic-gate 	cp->cache_nullslab.slab_cache = cp;
2207*7c478bd9Sstevel@tonic-gate 	cp->cache_nullslab.slab_refcnt = -1;
2208*7c478bd9Sstevel@tonic-gate 	cp->cache_nullslab.slab_next = &cp->cache_nullslab;
2209*7c478bd9Sstevel@tonic-gate 	cp->cache_nullslab.slab_prev = &cp->cache_nullslab;
2210*7c478bd9Sstevel@tonic-gate 
2211*7c478bd9Sstevel@tonic-gate 	if (cp->cache_flags & KMF_HASH) {
2212*7c478bd9Sstevel@tonic-gate 		cp->cache_hash_table = vmem_alloc(kmem_hash_arena,
2213*7c478bd9Sstevel@tonic-gate 		    KMEM_HASH_INITIAL * sizeof (void *), VM_SLEEP);
2214*7c478bd9Sstevel@tonic-gate 		bzero(cp->cache_hash_table,
2215*7c478bd9Sstevel@tonic-gate 		    KMEM_HASH_INITIAL * sizeof (void *));
2216*7c478bd9Sstevel@tonic-gate 		cp->cache_hash_mask = KMEM_HASH_INITIAL - 1;
2217*7c478bd9Sstevel@tonic-gate 		cp->cache_hash_shift = highbit((ulong_t)chunksize) - 1;
2218*7c478bd9Sstevel@tonic-gate 	}
2219*7c478bd9Sstevel@tonic-gate 
2220*7c478bd9Sstevel@tonic-gate 	/*
2221*7c478bd9Sstevel@tonic-gate 	 * Initialize the depot.
2222*7c478bd9Sstevel@tonic-gate 	 */
2223*7c478bd9Sstevel@tonic-gate 	mutex_init(&cp->cache_depot_lock, NULL, MUTEX_DEFAULT, NULL);
2224*7c478bd9Sstevel@tonic-gate 
2225*7c478bd9Sstevel@tonic-gate 	for (mtp = kmem_magtype; chunksize <= mtp->mt_minbuf; mtp++)
2226*7c478bd9Sstevel@tonic-gate 		continue;
2227*7c478bd9Sstevel@tonic-gate 
2228*7c478bd9Sstevel@tonic-gate 	cp->cache_magtype = mtp;
2229*7c478bd9Sstevel@tonic-gate 
2230*7c478bd9Sstevel@tonic-gate 	/*
2231*7c478bd9Sstevel@tonic-gate 	 * Initialize the CPU layer.
2232*7c478bd9Sstevel@tonic-gate 	 */
2233*7c478bd9Sstevel@tonic-gate 	for (cpu_seqid = 0; cpu_seqid < max_ncpus; cpu_seqid++) {
2234*7c478bd9Sstevel@tonic-gate 		kmem_cpu_cache_t *ccp = &cp->cache_cpu[cpu_seqid];
2235*7c478bd9Sstevel@tonic-gate 		mutex_init(&ccp->cc_lock, NULL, MUTEX_DEFAULT, NULL);
2236*7c478bd9Sstevel@tonic-gate 		ccp->cc_flags = cp->cache_flags;
2237*7c478bd9Sstevel@tonic-gate 		ccp->cc_rounds = -1;
2238*7c478bd9Sstevel@tonic-gate 		ccp->cc_prounds = -1;
2239*7c478bd9Sstevel@tonic-gate 	}
2240*7c478bd9Sstevel@tonic-gate 
2241*7c478bd9Sstevel@tonic-gate 	/*
2242*7c478bd9Sstevel@tonic-gate 	 * Create the cache's kstats.
2243*7c478bd9Sstevel@tonic-gate 	 */
2244*7c478bd9Sstevel@tonic-gate 	if ((cp->cache_kstat = kstat_create("unix", 0, cp->cache_name,
2245*7c478bd9Sstevel@tonic-gate 	    "kmem_cache", KSTAT_TYPE_NAMED,
2246*7c478bd9Sstevel@tonic-gate 	    sizeof (kmem_cache_kstat) / sizeof (kstat_named_t),
2247*7c478bd9Sstevel@tonic-gate 	    KSTAT_FLAG_VIRTUAL)) != NULL) {
2248*7c478bd9Sstevel@tonic-gate 		cp->cache_kstat->ks_data = &kmem_cache_kstat;
2249*7c478bd9Sstevel@tonic-gate 		cp->cache_kstat->ks_update = kmem_cache_kstat_update;
2250*7c478bd9Sstevel@tonic-gate 		cp->cache_kstat->ks_private = cp;
2251*7c478bd9Sstevel@tonic-gate 		cp->cache_kstat->ks_lock = &kmem_cache_kstat_lock;
2252*7c478bd9Sstevel@tonic-gate 		kstat_install(cp->cache_kstat);
2253*7c478bd9Sstevel@tonic-gate 	}
2254*7c478bd9Sstevel@tonic-gate 
2255*7c478bd9Sstevel@tonic-gate 	/*
2256*7c478bd9Sstevel@tonic-gate 	 * Add the cache to the global list.  This makes it visible
2257*7c478bd9Sstevel@tonic-gate 	 * to kmem_update(), so the cache must be ready for business.
2258*7c478bd9Sstevel@tonic-gate 	 */
2259*7c478bd9Sstevel@tonic-gate 	mutex_enter(&kmem_cache_lock);
2260*7c478bd9Sstevel@tonic-gate 	cp->cache_next = cnext = &kmem_null_cache;
2261*7c478bd9Sstevel@tonic-gate 	cp->cache_prev = cprev = kmem_null_cache.cache_prev;
2262*7c478bd9Sstevel@tonic-gate 	cnext->cache_prev = cp;
2263*7c478bd9Sstevel@tonic-gate 	cprev->cache_next = cp;
2264*7c478bd9Sstevel@tonic-gate 	mutex_exit(&kmem_cache_lock);
2265*7c478bd9Sstevel@tonic-gate 
2266*7c478bd9Sstevel@tonic-gate 	if (kmem_ready)
2267*7c478bd9Sstevel@tonic-gate 		kmem_cache_magazine_enable(cp);
2268*7c478bd9Sstevel@tonic-gate 
2269*7c478bd9Sstevel@tonic-gate 	return (cp);
2270*7c478bd9Sstevel@tonic-gate }
2271*7c478bd9Sstevel@tonic-gate 
2272*7c478bd9Sstevel@tonic-gate void
2273*7c478bd9Sstevel@tonic-gate kmem_cache_destroy(kmem_cache_t *cp)
2274*7c478bd9Sstevel@tonic-gate {
2275*7c478bd9Sstevel@tonic-gate 	int cpu_seqid;
2276*7c478bd9Sstevel@tonic-gate 
2277*7c478bd9Sstevel@tonic-gate 	/*
2278*7c478bd9Sstevel@tonic-gate 	 * Remove the cache from the global cache list so that no one else
2279*7c478bd9Sstevel@tonic-gate 	 * can schedule tasks on its behalf, wait for any pending tasks to
2280*7c478bd9Sstevel@tonic-gate 	 * complete, purge the cache, and then destroy it.
2281*7c478bd9Sstevel@tonic-gate 	 */
2282*7c478bd9Sstevel@tonic-gate 	mutex_enter(&kmem_cache_lock);
2283*7c478bd9Sstevel@tonic-gate 	cp->cache_prev->cache_next = cp->cache_next;
2284*7c478bd9Sstevel@tonic-gate 	cp->cache_next->cache_prev = cp->cache_prev;
2285*7c478bd9Sstevel@tonic-gate 	cp->cache_prev = cp->cache_next = NULL;
2286*7c478bd9Sstevel@tonic-gate 	mutex_exit(&kmem_cache_lock);
2287*7c478bd9Sstevel@tonic-gate 
2288*7c478bd9Sstevel@tonic-gate 	if (kmem_taskq != NULL)
2289*7c478bd9Sstevel@tonic-gate 		taskq_wait(kmem_taskq);
2290*7c478bd9Sstevel@tonic-gate 
2291*7c478bd9Sstevel@tonic-gate 	kmem_cache_magazine_purge(cp);
2292*7c478bd9Sstevel@tonic-gate 
2293*7c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->cache_lock);
2294*7c478bd9Sstevel@tonic-gate 	if (cp->cache_buftotal != 0)
2295*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "kmem_cache_destroy: '%s' (%p) not empty",
2296*7c478bd9Sstevel@tonic-gate 		    cp->cache_name, (void *)cp);
2297*7c478bd9Sstevel@tonic-gate 	cp->cache_reclaim = NULL;
2298*7c478bd9Sstevel@tonic-gate 	/*
2299*7c478bd9Sstevel@tonic-gate 	 * The cache is now dead.  There should be no further activity.
2300*7c478bd9Sstevel@tonic-gate 	 * We enforce this by setting land mines in the constructor and
2301*7c478bd9Sstevel@tonic-gate 	 * destructor routines that induce a kernel text fault if invoked.
2302*7c478bd9Sstevel@tonic-gate 	 */
2303*7c478bd9Sstevel@tonic-gate 	cp->cache_constructor = (int (*)(void *, void *, int))1;
2304*7c478bd9Sstevel@tonic-gate 	cp->cache_destructor = (void (*)(void *, void *))2;
2305*7c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->cache_lock);
2306*7c478bd9Sstevel@tonic-gate 
2307*7c478bd9Sstevel@tonic-gate 	kstat_delete(cp->cache_kstat);
2308*7c478bd9Sstevel@tonic-gate 
2309*7c478bd9Sstevel@tonic-gate 	if (cp->cache_hash_table != NULL)
2310*7c478bd9Sstevel@tonic-gate 		vmem_free(kmem_hash_arena, cp->cache_hash_table,
2311*7c478bd9Sstevel@tonic-gate 		    (cp->cache_hash_mask + 1) * sizeof (void *));
2312*7c478bd9Sstevel@tonic-gate 
2313*7c478bd9Sstevel@tonic-gate 	for (cpu_seqid = 0; cpu_seqid < max_ncpus; cpu_seqid++)
2314*7c478bd9Sstevel@tonic-gate 		mutex_destroy(&cp->cache_cpu[cpu_seqid].cc_lock);
2315*7c478bd9Sstevel@tonic-gate 
2316*7c478bd9Sstevel@tonic-gate 	mutex_destroy(&cp->cache_depot_lock);
2317*7c478bd9Sstevel@tonic-gate 	mutex_destroy(&cp->cache_lock);
2318*7c478bd9Sstevel@tonic-gate 
2319*7c478bd9Sstevel@tonic-gate 	vmem_free(kmem_cache_arena, cp, KMEM_CACHE_SIZE(max_ncpus));
2320*7c478bd9Sstevel@tonic-gate }
2321*7c478bd9Sstevel@tonic-gate 
2322*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2323*7c478bd9Sstevel@tonic-gate static int
2324*7c478bd9Sstevel@tonic-gate kmem_cpu_setup(cpu_setup_t what, int id, void *arg)
2325*7c478bd9Sstevel@tonic-gate {
2326*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
2327*7c478bd9Sstevel@tonic-gate 	if (what == CPU_UNCONFIG) {
2328*7c478bd9Sstevel@tonic-gate 		kmem_cache_applyall(kmem_cache_magazine_purge,
2329*7c478bd9Sstevel@tonic-gate 		    kmem_taskq, TQ_SLEEP);
2330*7c478bd9Sstevel@tonic-gate 		kmem_cache_applyall(kmem_cache_magazine_enable,
2331*7c478bd9Sstevel@tonic-gate 		    kmem_taskq, TQ_SLEEP);
2332*7c478bd9Sstevel@tonic-gate 	}
2333*7c478bd9Sstevel@tonic-gate 	return (0);
2334*7c478bd9Sstevel@tonic-gate }
2335*7c478bd9Sstevel@tonic-gate 
2336*7c478bd9Sstevel@tonic-gate static void
2337*7c478bd9Sstevel@tonic-gate kmem_cache_init(int pass, int use_large_pages)
2338*7c478bd9Sstevel@tonic-gate {
2339*7c478bd9Sstevel@tonic-gate 	int i;
2340*7c478bd9Sstevel@tonic-gate 	size_t size;
2341*7c478bd9Sstevel@tonic-gate 	kmem_cache_t *cp;
2342*7c478bd9Sstevel@tonic-gate 	kmem_magtype_t *mtp;
2343*7c478bd9Sstevel@tonic-gate 	char name[KMEM_CACHE_NAMELEN + 1];
2344*7c478bd9Sstevel@tonic-gate 
2345*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (kmem_magtype) / sizeof (*mtp); i++) {
2346*7c478bd9Sstevel@tonic-gate 		mtp = &kmem_magtype[i];
2347*7c478bd9Sstevel@tonic-gate 		(void) sprintf(name, "kmem_magazine_%d", mtp->mt_magsize);
2348*7c478bd9Sstevel@tonic-gate 		mtp->mt_cache = kmem_cache_create(name,
2349*7c478bd9Sstevel@tonic-gate 		    (mtp->mt_magsize + 1) * sizeof (void *),
2350*7c478bd9Sstevel@tonic-gate 		    mtp->mt_align, NULL, NULL, NULL, NULL,
2351*7c478bd9Sstevel@tonic-gate 		    kmem_msb_arena, KMC_NOHASH);
2352*7c478bd9Sstevel@tonic-gate 	}
2353*7c478bd9Sstevel@tonic-gate 
2354*7c478bd9Sstevel@tonic-gate 	kmem_slab_cache = kmem_cache_create("kmem_slab_cache",
2355*7c478bd9Sstevel@tonic-gate 	    sizeof (kmem_slab_t), 0, NULL, NULL, NULL, NULL,
2356*7c478bd9Sstevel@tonic-gate 	    kmem_msb_arena, KMC_NOHASH);
2357*7c478bd9Sstevel@tonic-gate 
2358*7c478bd9Sstevel@tonic-gate 	kmem_bufctl_cache = kmem_cache_create("kmem_bufctl_cache",
2359*7c478bd9Sstevel@tonic-gate 	    sizeof (kmem_bufctl_t), 0, NULL, NULL, NULL, NULL,
2360*7c478bd9Sstevel@tonic-gate 	    kmem_msb_arena, KMC_NOHASH);
2361*7c478bd9Sstevel@tonic-gate 
2362*7c478bd9Sstevel@tonic-gate 	kmem_bufctl_audit_cache = kmem_cache_create("kmem_bufctl_audit_cache",
2363*7c478bd9Sstevel@tonic-gate 	    sizeof (kmem_bufctl_audit_t), 0, NULL, NULL, NULL, NULL,
2364*7c478bd9Sstevel@tonic-gate 	    kmem_msb_arena, KMC_NOHASH);
2365*7c478bd9Sstevel@tonic-gate 
2366*7c478bd9Sstevel@tonic-gate 	if (pass == 2) {
2367*7c478bd9Sstevel@tonic-gate 		kmem_va_arena = vmem_create("kmem_va",
2368*7c478bd9Sstevel@tonic-gate 		    NULL, 0, PAGESIZE,
2369*7c478bd9Sstevel@tonic-gate 		    vmem_alloc, vmem_free, heap_arena,
2370*7c478bd9Sstevel@tonic-gate 		    8 * PAGESIZE, VM_SLEEP);
2371*7c478bd9Sstevel@tonic-gate 
2372*7c478bd9Sstevel@tonic-gate 		if (use_large_pages) {
2373*7c478bd9Sstevel@tonic-gate 			kmem_default_arena = vmem_xcreate("kmem_default",
2374*7c478bd9Sstevel@tonic-gate 			    NULL, 0, PAGESIZE,
2375*7c478bd9Sstevel@tonic-gate 			    segkmem_alloc_lp, segkmem_free_lp, kmem_va_arena,
2376*7c478bd9Sstevel@tonic-gate 			    0, VM_SLEEP);
2377*7c478bd9Sstevel@tonic-gate 		} else {
2378*7c478bd9Sstevel@tonic-gate 			kmem_default_arena = vmem_create("kmem_default",
2379*7c478bd9Sstevel@tonic-gate 			    NULL, 0, PAGESIZE,
2380*7c478bd9Sstevel@tonic-gate 			    segkmem_alloc, segkmem_free, kmem_va_arena,
2381*7c478bd9Sstevel@tonic-gate 			    0, VM_SLEEP);
2382*7c478bd9Sstevel@tonic-gate 		}
2383*7c478bd9Sstevel@tonic-gate 	} else {
2384*7c478bd9Sstevel@tonic-gate 		/*
2385*7c478bd9Sstevel@tonic-gate 		 * During the first pass, the kmem_alloc_* caches
2386*7c478bd9Sstevel@tonic-gate 		 * are treated as metadata.
2387*7c478bd9Sstevel@tonic-gate 		 */
2388*7c478bd9Sstevel@tonic-gate 		kmem_default_arena = kmem_msb_arena;
2389*7c478bd9Sstevel@tonic-gate 	}
2390*7c478bd9Sstevel@tonic-gate 
2391*7c478bd9Sstevel@tonic-gate 	/*
2392*7c478bd9Sstevel@tonic-gate 	 * Set up the default caches to back kmem_alloc()
2393*7c478bd9Sstevel@tonic-gate 	 */
2394*7c478bd9Sstevel@tonic-gate 	size = KMEM_ALIGN;
2395*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (kmem_alloc_sizes) / sizeof (int); i++) {
2396*7c478bd9Sstevel@tonic-gate 		size_t align = KMEM_ALIGN;
2397*7c478bd9Sstevel@tonic-gate 		size_t cache_size = kmem_alloc_sizes[i];
2398*7c478bd9Sstevel@tonic-gate 		/*
2399*7c478bd9Sstevel@tonic-gate 		 * If they allocate a multiple of the coherency granularity,
2400*7c478bd9Sstevel@tonic-gate 		 * they get a coherency-granularity-aligned address.
2401*7c478bd9Sstevel@tonic-gate 		 */
2402*7c478bd9Sstevel@tonic-gate 		if (IS_P2ALIGNED(cache_size, 64))
2403*7c478bd9Sstevel@tonic-gate 			align = 64;
2404*7c478bd9Sstevel@tonic-gate 		if (IS_P2ALIGNED(cache_size, PAGESIZE))
2405*7c478bd9Sstevel@tonic-gate 			align = PAGESIZE;
2406*7c478bd9Sstevel@tonic-gate 		(void) sprintf(name, "kmem_alloc_%lu", cache_size);
2407*7c478bd9Sstevel@tonic-gate 		cp = kmem_cache_create(name, cache_size, align,
2408*7c478bd9Sstevel@tonic-gate 		    NULL, NULL, NULL, NULL, NULL, KMC_KMEM_ALLOC);
2409*7c478bd9Sstevel@tonic-gate 		while (size <= cache_size) {
2410*7c478bd9Sstevel@tonic-gate 			kmem_alloc_table[(size - 1) >> KMEM_ALIGN_SHIFT] = cp;
2411*7c478bd9Sstevel@tonic-gate 			size += KMEM_ALIGN;
2412*7c478bd9Sstevel@tonic-gate 		}
2413*7c478bd9Sstevel@tonic-gate 	}
2414*7c478bd9Sstevel@tonic-gate }
2415*7c478bd9Sstevel@tonic-gate 
2416*7c478bd9Sstevel@tonic-gate void
2417*7c478bd9Sstevel@tonic-gate kmem_init(void)
2418*7c478bd9Sstevel@tonic-gate {
2419*7c478bd9Sstevel@tonic-gate 	kmem_cache_t *cp;
2420*7c478bd9Sstevel@tonic-gate 	int old_kmem_flags = kmem_flags;
2421*7c478bd9Sstevel@tonic-gate 	int use_large_pages = 0;
2422*7c478bd9Sstevel@tonic-gate 	size_t maxverify, minfirewall;
2423*7c478bd9Sstevel@tonic-gate 
2424*7c478bd9Sstevel@tonic-gate 	kstat_init();
2425*7c478bd9Sstevel@tonic-gate 
2426*7c478bd9Sstevel@tonic-gate 	/*
2427*7c478bd9Sstevel@tonic-gate 	 * Small-memory systems (< 24 MB) can't handle kmem_flags overhead.
2428*7c478bd9Sstevel@tonic-gate 	 */
2429*7c478bd9Sstevel@tonic-gate 	if (physmem < btop(24 << 20) && !(old_kmem_flags & KMF_STICKY))
2430*7c478bd9Sstevel@tonic-gate 		kmem_flags = 0;
2431*7c478bd9Sstevel@tonic-gate 
2432*7c478bd9Sstevel@tonic-gate 	/*
2433*7c478bd9Sstevel@tonic-gate 	 * Don't do firewalled allocations if the heap is less than 1TB
2434*7c478bd9Sstevel@tonic-gate 	 * (i.e. on a 32-bit kernel)
2435*7c478bd9Sstevel@tonic-gate 	 * The resulting VM_NEXTFIT allocations would create too much
2436*7c478bd9Sstevel@tonic-gate 	 * fragmentation in a small heap.
2437*7c478bd9Sstevel@tonic-gate 	 */
2438*7c478bd9Sstevel@tonic-gate #if defined(_LP64)
2439*7c478bd9Sstevel@tonic-gate 	maxverify = minfirewall = PAGESIZE / 2;
2440*7c478bd9Sstevel@tonic-gate #else
2441*7c478bd9Sstevel@tonic-gate 	maxverify = minfirewall = ULONG_MAX;
2442*7c478bd9Sstevel@tonic-gate #endif
2443*7c478bd9Sstevel@tonic-gate 
2444*7c478bd9Sstevel@tonic-gate 	/* LINTED */
2445*7c478bd9Sstevel@tonic-gate 	ASSERT(sizeof (kmem_cpu_cache_t) == KMEM_CPU_CACHE_SIZE);
2446*7c478bd9Sstevel@tonic-gate 
2447*7c478bd9Sstevel@tonic-gate 	kmem_null_cache.cache_next = &kmem_null_cache;
2448*7c478bd9Sstevel@tonic-gate 	kmem_null_cache.cache_prev = &kmem_null_cache;
2449*7c478bd9Sstevel@tonic-gate 
2450*7c478bd9Sstevel@tonic-gate 	kmem_metadata_arena = vmem_create("kmem_metadata", NULL, 0, PAGESIZE,
2451*7c478bd9Sstevel@tonic-gate 	    vmem_alloc, vmem_free, heap_arena, 8 * PAGESIZE,
2452*7c478bd9Sstevel@tonic-gate 	    VM_SLEEP | VMC_NO_QCACHE);
2453*7c478bd9Sstevel@tonic-gate 
2454*7c478bd9Sstevel@tonic-gate 	kmem_msb_arena = vmem_create("kmem_msb", NULL, 0,
2455*7c478bd9Sstevel@tonic-gate 	    PAGESIZE, segkmem_alloc, segkmem_free, kmem_metadata_arena, 0,
2456*7c478bd9Sstevel@tonic-gate 	    VM_SLEEP);
2457*7c478bd9Sstevel@tonic-gate 
2458*7c478bd9Sstevel@tonic-gate 	kmem_cache_arena = vmem_create("kmem_cache", NULL, 0, KMEM_ALIGN,
2459*7c478bd9Sstevel@tonic-gate 	    segkmem_alloc, segkmem_free, kmem_metadata_arena, 0, VM_SLEEP);
2460*7c478bd9Sstevel@tonic-gate 
2461*7c478bd9Sstevel@tonic-gate 	kmem_hash_arena = vmem_create("kmem_hash", NULL, 0, KMEM_ALIGN,
2462*7c478bd9Sstevel@tonic-gate 	    segkmem_alloc, segkmem_free, kmem_metadata_arena, 0, VM_SLEEP);
2463*7c478bd9Sstevel@tonic-gate 
2464*7c478bd9Sstevel@tonic-gate 	kmem_log_arena = vmem_create("kmem_log", NULL, 0, KMEM_ALIGN,
2465*7c478bd9Sstevel@tonic-gate 	    segkmem_alloc, segkmem_free, heap_arena, 0, VM_SLEEP);
2466*7c478bd9Sstevel@tonic-gate 
2467*7c478bd9Sstevel@tonic-gate 	kmem_firewall_va_arena = vmem_create("kmem_firewall_va",
2468*7c478bd9Sstevel@tonic-gate 	    NULL, 0, PAGESIZE,
2469*7c478bd9Sstevel@tonic-gate 	    kmem_firewall_va_alloc, kmem_firewall_va_free, heap_arena,
2470*7c478bd9Sstevel@tonic-gate 	    0, VM_SLEEP);
2471*7c478bd9Sstevel@tonic-gate 
2472*7c478bd9Sstevel@tonic-gate 	kmem_firewall_arena = vmem_create("kmem_firewall", NULL, 0, PAGESIZE,
2473*7c478bd9Sstevel@tonic-gate 	    segkmem_alloc, segkmem_free, kmem_firewall_va_arena, 0, VM_SLEEP);
2474*7c478bd9Sstevel@tonic-gate 
2475*7c478bd9Sstevel@tonic-gate 	/* temporary oversize arena for mod_read_system_file */
2476*7c478bd9Sstevel@tonic-gate 	kmem_oversize_arena = vmem_create("kmem_oversize", NULL, 0, PAGESIZE,
2477*7c478bd9Sstevel@tonic-gate 	    segkmem_alloc, segkmem_free, heap_arena, 0, VM_SLEEP);
2478*7c478bd9Sstevel@tonic-gate 
2479*7c478bd9Sstevel@tonic-gate 	kmem_null_cache.cache_next = &kmem_null_cache;
2480*7c478bd9Sstevel@tonic-gate 	kmem_null_cache.cache_prev = &kmem_null_cache;
2481*7c478bd9Sstevel@tonic-gate 
2482*7c478bd9Sstevel@tonic-gate 	kmem_reap_interval = 15 * hz;
2483*7c478bd9Sstevel@tonic-gate 
2484*7c478bd9Sstevel@tonic-gate 	/*
2485*7c478bd9Sstevel@tonic-gate 	 * Read /etc/system.  This is a chicken-and-egg problem because
2486*7c478bd9Sstevel@tonic-gate 	 * kmem_flags may be set in /etc/system, but mod_read_system_file()
2487*7c478bd9Sstevel@tonic-gate 	 * needs to use the allocator.  The simplest solution is to create
2488*7c478bd9Sstevel@tonic-gate 	 * all the standard kmem caches, read /etc/system, destroy all the
2489*7c478bd9Sstevel@tonic-gate 	 * caches we just created, and then create them all again in light
2490*7c478bd9Sstevel@tonic-gate 	 * of the (possibly) new kmem_flags and other kmem tunables.
2491*7c478bd9Sstevel@tonic-gate 	 */
2492*7c478bd9Sstevel@tonic-gate 	kmem_cache_init(1, 0);
2493*7c478bd9Sstevel@tonic-gate 
2494*7c478bd9Sstevel@tonic-gate 	mod_read_system_file(boothowto & RB_ASKNAME);
2495*7c478bd9Sstevel@tonic-gate 
2496*7c478bd9Sstevel@tonic-gate 	while ((cp = kmem_null_cache.cache_prev) != &kmem_null_cache)
2497*7c478bd9Sstevel@tonic-gate 		kmem_cache_destroy(cp);
2498*7c478bd9Sstevel@tonic-gate 
2499*7c478bd9Sstevel@tonic-gate 	vmem_destroy(kmem_oversize_arena);
2500*7c478bd9Sstevel@tonic-gate 
2501*7c478bd9Sstevel@tonic-gate 	if (old_kmem_flags & KMF_STICKY)
2502*7c478bd9Sstevel@tonic-gate 		kmem_flags = old_kmem_flags;
2503*7c478bd9Sstevel@tonic-gate 
2504*7c478bd9Sstevel@tonic-gate 	if (!(kmem_flags & KMF_AUDIT))
2505*7c478bd9Sstevel@tonic-gate 		vmem_seg_size = offsetof(vmem_seg_t, vs_thread);
2506*7c478bd9Sstevel@tonic-gate 
2507*7c478bd9Sstevel@tonic-gate 	if (kmem_maxverify == 0)
2508*7c478bd9Sstevel@tonic-gate 		kmem_maxverify = maxverify;
2509*7c478bd9Sstevel@tonic-gate 
2510*7c478bd9Sstevel@tonic-gate 	if (kmem_minfirewall == 0)
2511*7c478bd9Sstevel@tonic-gate 		kmem_minfirewall = minfirewall;
2512*7c478bd9Sstevel@tonic-gate 
2513*7c478bd9Sstevel@tonic-gate 	/*
2514*7c478bd9Sstevel@tonic-gate 	 * give segkmem a chance to figure out if we are using large pages
2515*7c478bd9Sstevel@tonic-gate 	 * for the kernel heap
2516*7c478bd9Sstevel@tonic-gate 	 */
2517*7c478bd9Sstevel@tonic-gate 	use_large_pages = segkmem_lpsetup();
2518*7c478bd9Sstevel@tonic-gate 
2519*7c478bd9Sstevel@tonic-gate 	/*
2520*7c478bd9Sstevel@tonic-gate 	 * To protect against corruption, we keep the actual number of callers
2521*7c478bd9Sstevel@tonic-gate 	 * KMF_LITE records seperate from the tunable.  We arbitrarily clamp
2522*7c478bd9Sstevel@tonic-gate 	 * to 16, since the overhead for small buffers quickly gets out of
2523*7c478bd9Sstevel@tonic-gate 	 * hand.
2524*7c478bd9Sstevel@tonic-gate 	 *
2525*7c478bd9Sstevel@tonic-gate 	 * The real limit would depend on the needs of the largest KMC_NOHASH
2526*7c478bd9Sstevel@tonic-gate 	 * cache.
2527*7c478bd9Sstevel@tonic-gate 	 */
2528*7c478bd9Sstevel@tonic-gate 	kmem_lite_count = MIN(MAX(0, kmem_lite_pcs), 16);
2529*7c478bd9Sstevel@tonic-gate 	kmem_lite_pcs = kmem_lite_count;
2530*7c478bd9Sstevel@tonic-gate 
2531*7c478bd9Sstevel@tonic-gate 	/*
2532*7c478bd9Sstevel@tonic-gate 	 * Normally, we firewall oversized allocations when possible, but
2533*7c478bd9Sstevel@tonic-gate 	 * if we are using large pages for kernel memory, and we don't have
2534*7c478bd9Sstevel@tonic-gate 	 * any non-LITE debugging flags set, we want to allocate oversized
2535*7c478bd9Sstevel@tonic-gate 	 * buffers from large pages, and so skip the firewalling.
2536*7c478bd9Sstevel@tonic-gate 	 */
2537*7c478bd9Sstevel@tonic-gate 	if (use_large_pages &&
2538*7c478bd9Sstevel@tonic-gate 	    ((kmem_flags & KMF_LITE) || !(kmem_flags & KMF_DEBUG))) {
2539*7c478bd9Sstevel@tonic-gate 		kmem_oversize_arena = vmem_xcreate("kmem_oversize", NULL, 0,
2540*7c478bd9Sstevel@tonic-gate 		    PAGESIZE, segkmem_alloc_lp, segkmem_free_lp, heap_arena,
2541*7c478bd9Sstevel@tonic-gate 		    0, VM_SLEEP);
2542*7c478bd9Sstevel@tonic-gate 	} else {
2543*7c478bd9Sstevel@tonic-gate 		kmem_oversize_arena = vmem_create("kmem_oversize",
2544*7c478bd9Sstevel@tonic-gate 		    NULL, 0, PAGESIZE,
2545*7c478bd9Sstevel@tonic-gate 		    segkmem_alloc, segkmem_free, kmem_minfirewall < ULONG_MAX?
2546*7c478bd9Sstevel@tonic-gate 		    kmem_firewall_va_arena : heap_arena, 0, VM_SLEEP);
2547*7c478bd9Sstevel@tonic-gate 	}
2548*7c478bd9Sstevel@tonic-gate 
2549*7c478bd9Sstevel@tonic-gate 	kmem_cache_init(2, use_large_pages);
2550*7c478bd9Sstevel@tonic-gate 
2551*7c478bd9Sstevel@tonic-gate 	if (kmem_flags & (KMF_AUDIT | KMF_RANDOMIZE)) {
2552*7c478bd9Sstevel@tonic-gate 		if (kmem_transaction_log_size == 0)
2553*7c478bd9Sstevel@tonic-gate 			kmem_transaction_log_size = kmem_maxavail() / 50;
2554*7c478bd9Sstevel@tonic-gate 		kmem_transaction_log = kmem_log_init(kmem_transaction_log_size);
2555*7c478bd9Sstevel@tonic-gate 	}
2556*7c478bd9Sstevel@tonic-gate 
2557*7c478bd9Sstevel@tonic-gate 	if (kmem_flags & (KMF_CONTENTS | KMF_RANDOMIZE)) {
2558*7c478bd9Sstevel@tonic-gate 		if (kmem_content_log_size == 0)
2559*7c478bd9Sstevel@tonic-gate 			kmem_content_log_size = kmem_maxavail() / 50;
2560*7c478bd9Sstevel@tonic-gate 		kmem_content_log = kmem_log_init(kmem_content_log_size);
2561*7c478bd9Sstevel@tonic-gate 	}
2562*7c478bd9Sstevel@tonic-gate 
2563*7c478bd9Sstevel@tonic-gate 	kmem_failure_log = kmem_log_init(kmem_failure_log_size);
2564*7c478bd9Sstevel@tonic-gate 
2565*7c478bd9Sstevel@tonic-gate 	kmem_slab_log = kmem_log_init(kmem_slab_log_size);
2566*7c478bd9Sstevel@tonic-gate 
2567*7c478bd9Sstevel@tonic-gate 	/*
2568*7c478bd9Sstevel@tonic-gate 	 * Initialize STREAMS message caches so allocb() is available.
2569*7c478bd9Sstevel@tonic-gate 	 * This allows us to initialize the logging framework (cmn_err(9F),
2570*7c478bd9Sstevel@tonic-gate 	 * strlog(9F), etc) so we can start recording messages.
2571*7c478bd9Sstevel@tonic-gate 	 */
2572*7c478bd9Sstevel@tonic-gate 	streams_msg_init();
2573*7c478bd9Sstevel@tonic-gate 	/*
2574*7c478bd9Sstevel@tonic-gate 	 * Initialize the ZSD framework in Zones so modules loaded henceforth
2575*7c478bd9Sstevel@tonic-gate 	 * can register their callbacks.
2576*7c478bd9Sstevel@tonic-gate 	 */
2577*7c478bd9Sstevel@tonic-gate 	zone_zsd_init();
2578*7c478bd9Sstevel@tonic-gate 	log_init();
2579*7c478bd9Sstevel@tonic-gate 	taskq_init();
2580*7c478bd9Sstevel@tonic-gate 
2581*7c478bd9Sstevel@tonic-gate 	kmem_cache_applyall(kmem_cache_magazine_enable, NULL, TQ_SLEEP);
2582*7c478bd9Sstevel@tonic-gate 
2583*7c478bd9Sstevel@tonic-gate 	kmem_ready = 1;
2584*7c478bd9Sstevel@tonic-gate 
2585*7c478bd9Sstevel@tonic-gate 	/*
2586*7c478bd9Sstevel@tonic-gate 	 * Initialize the platform-specific aligned/DMA memory allocator.
2587*7c478bd9Sstevel@tonic-gate 	 */
2588*7c478bd9Sstevel@tonic-gate 	ka_init();
2589*7c478bd9Sstevel@tonic-gate 
2590*7c478bd9Sstevel@tonic-gate 	/*
2591*7c478bd9Sstevel@tonic-gate 	 * Initialize 32-bit ID cache.
2592*7c478bd9Sstevel@tonic-gate 	 */
2593*7c478bd9Sstevel@tonic-gate 	id32_init();
2594*7c478bd9Sstevel@tonic-gate }
2595*7c478bd9Sstevel@tonic-gate 
2596*7c478bd9Sstevel@tonic-gate void
2597*7c478bd9Sstevel@tonic-gate kmem_thread_init(void)
2598*7c478bd9Sstevel@tonic-gate {
2599*7c478bd9Sstevel@tonic-gate 	kmem_taskq = taskq_create_instance("kmem_taskq", 0, 1, minclsyspri,
2600*7c478bd9Sstevel@tonic-gate 	    300, INT_MAX, TASKQ_PREPOPULATE);
2601*7c478bd9Sstevel@tonic-gate }
2602*7c478bd9Sstevel@tonic-gate 
2603*7c478bd9Sstevel@tonic-gate void
2604*7c478bd9Sstevel@tonic-gate kmem_mp_init(void)
2605*7c478bd9Sstevel@tonic-gate {
2606*7c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
2607*7c478bd9Sstevel@tonic-gate 	register_cpu_setup_func(kmem_cpu_setup, NULL);
2608*7c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
2609*7c478bd9Sstevel@tonic-gate 
2610*7c478bd9Sstevel@tonic-gate 	kmem_update_timeout(NULL);
2611*7c478bd9Sstevel@tonic-gate }
2612