xref: /onnv-gate/usr/src/uts/common/sys/kmem_impl.h (revision 10217:38c40fa7246e)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
56306Stomee  * Common Development and Distribution License (the "License").
66306Stomee  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
216306Stomee 
220Sstevel@tonic-gate /*
238887SMichael.Corcoran@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #ifndef _SYS_KMEM_IMPL_H
280Sstevel@tonic-gate #define	_SYS_KMEM_IMPL_H
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <sys/kmem.h>
310Sstevel@tonic-gate #include <sys/vmem.h>
320Sstevel@tonic-gate #include <sys/thread.h>
330Sstevel@tonic-gate #include <sys/t_lock.h>
340Sstevel@tonic-gate #include <sys/time.h>
350Sstevel@tonic-gate #include <sys/kstat.h>
360Sstevel@tonic-gate #include <sys/cpuvar.h>
370Sstevel@tonic-gate #include <sys/systm.h>
380Sstevel@tonic-gate #include <vm/page.h>
396712Stomee #include <sys/avl.h>
406712Stomee #include <sys/list.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #ifdef	__cplusplus
430Sstevel@tonic-gate extern "C" {
440Sstevel@tonic-gate #endif
450Sstevel@tonic-gate 
460Sstevel@tonic-gate /*
470Sstevel@tonic-gate  * kernel memory allocator: implementation-private data structures
486712Stomee  *
496712Stomee  * Lock order:
506712Stomee  * 1. cache_lock
516712Stomee  * 2. cc_lock in order by CPU ID
526712Stomee  * 3. cache_depot_lock
536712Stomee  *
546712Stomee  * Do not call kmem_cache_alloc() or taskq_dispatch() while holding any of the
556712Stomee  * above locks.
560Sstevel@tonic-gate  */
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #define	KMF_AUDIT	0x00000001	/* transaction auditing */
590Sstevel@tonic-gate #define	KMF_DEADBEEF	0x00000002	/* deadbeef checking */
600Sstevel@tonic-gate #define	KMF_REDZONE	0x00000004	/* redzone checking */
610Sstevel@tonic-gate #define	KMF_CONTENTS	0x00000008	/* freed-buffer content logging */
620Sstevel@tonic-gate #define	KMF_STICKY	0x00000010	/* if set, override /etc/system */
630Sstevel@tonic-gate #define	KMF_NOMAGAZINE	0x00000020	/* disable per-cpu magazines */
640Sstevel@tonic-gate #define	KMF_FIREWALL	0x00000040	/* put all bufs before unmapped pages */
650Sstevel@tonic-gate #define	KMF_LITE	0x00000100	/* lightweight debugging */
660Sstevel@tonic-gate 
670Sstevel@tonic-gate #define	KMF_HASH	0x00000200	/* cache has hash table */
680Sstevel@tonic-gate #define	KMF_RANDOMIZE	0x00000400	/* randomize other kmem_flags */
690Sstevel@tonic-gate 
700Sstevel@tonic-gate #define	KMF_BUFTAG	(KMF_DEADBEEF | KMF_REDZONE)
710Sstevel@tonic-gate #define	KMF_TOUCH	(KMF_BUFTAG | KMF_LITE | KMF_CONTENTS)
720Sstevel@tonic-gate #define	KMF_RANDOM	(KMF_TOUCH | KMF_AUDIT | KMF_NOMAGAZINE)
730Sstevel@tonic-gate #define	KMF_DEBUG	(KMF_RANDOM | KMF_FIREWALL)
740Sstevel@tonic-gate 
750Sstevel@tonic-gate #define	KMEM_STACK_DEPTH	15
760Sstevel@tonic-gate 
770Sstevel@tonic-gate #define	KMEM_FREE_PATTERN		0xdeadbeefdeadbeefULL
780Sstevel@tonic-gate #define	KMEM_UNINITIALIZED_PATTERN	0xbaddcafebaddcafeULL
790Sstevel@tonic-gate #define	KMEM_REDZONE_PATTERN		0xfeedfacefeedfaceULL
800Sstevel@tonic-gate #define	KMEM_REDZONE_BYTE		0xbb
810Sstevel@tonic-gate 
820Sstevel@tonic-gate /*
830Sstevel@tonic-gate  * Redzone size encodings for kmem_alloc() / kmem_free().  We encode the
840Sstevel@tonic-gate  * allocation size, rather than storing it directly, so that kmem_free()
850Sstevel@tonic-gate  * can distinguish frees of the wrong size from redzone violations.
860Sstevel@tonic-gate  *
870Sstevel@tonic-gate  * A size of zero is never valid.
880Sstevel@tonic-gate  */
890Sstevel@tonic-gate #define	KMEM_SIZE_ENCODE(x)	(251 * (x) + 1)
900Sstevel@tonic-gate #define	KMEM_SIZE_DECODE(x)	((x) / 251)
910Sstevel@tonic-gate #define	KMEM_SIZE_VALID(x)	((x) % 251 == 1 && (x) != 1)
920Sstevel@tonic-gate 
936712Stomee 
946712Stomee #define	KMEM_ALIGN		8	/* min guaranteed alignment */
956712Stomee #define	KMEM_ALIGN_SHIFT	3	/* log2(KMEM_ALIGN) */
966712Stomee #define	KMEM_VOID_FRACTION	8	/* never waste more than 1/8 of slab */
976712Stomee 
986712Stomee #define	KMEM_SLAB_IS_PARTIAL(sp)		\
996712Stomee 	((sp)->slab_refcnt > 0 && (sp)->slab_refcnt < (sp)->slab_chunks)
1006712Stomee #define	KMEM_SLAB_IS_ALL_USED(sp)		\
1016712Stomee 	((sp)->slab_refcnt == (sp)->slab_chunks)
1026712Stomee 
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate  * The bufctl (buffer control) structure keeps some minimal information
1050Sstevel@tonic-gate  * about each buffer: its address, its slab, and its current linkage,
1060Sstevel@tonic-gate  * which is either on the slab's freelist (if the buffer is free), or
1070Sstevel@tonic-gate  * on the cache's buf-to-bufctl hash table (if the buffer is allocated).
1080Sstevel@tonic-gate  * In the case of non-hashed, or "raw", caches (the common case), only
1090Sstevel@tonic-gate  * the freelist linkage is necessary: the buffer address is at a fixed
1100Sstevel@tonic-gate  * offset from the bufctl address, and the slab is at the end of the page.
1110Sstevel@tonic-gate  *
1120Sstevel@tonic-gate  * NOTE: bc_next must be the first field; raw buffers have linkage only.
1130Sstevel@tonic-gate  */
1140Sstevel@tonic-gate typedef struct kmem_bufctl {
1150Sstevel@tonic-gate 	struct kmem_bufctl	*bc_next;	/* next bufctl struct */
1160Sstevel@tonic-gate 	void			*bc_addr;	/* address of buffer */
1170Sstevel@tonic-gate 	struct kmem_slab	*bc_slab;	/* controlling slab */
1180Sstevel@tonic-gate } kmem_bufctl_t;
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate /*
1210Sstevel@tonic-gate  * The KMF_AUDIT version of the bufctl structure.  The beginning of this
1220Sstevel@tonic-gate  * structure must be identical to the normal bufctl structure so that
1230Sstevel@tonic-gate  * pointers are interchangeable.
1240Sstevel@tonic-gate  */
1250Sstevel@tonic-gate typedef struct kmem_bufctl_audit {
1260Sstevel@tonic-gate 	struct kmem_bufctl	*bc_next;	/* next bufctl struct */
1270Sstevel@tonic-gate 	void			*bc_addr;	/* address of buffer */
1280Sstevel@tonic-gate 	struct kmem_slab	*bc_slab;	/* controlling slab */
1290Sstevel@tonic-gate 	kmem_cache_t		*bc_cache;	/* controlling cache */
1300Sstevel@tonic-gate 	hrtime_t		bc_timestamp;	/* transaction time */
1310Sstevel@tonic-gate 	kthread_t		*bc_thread;	/* thread doing transaction */
1320Sstevel@tonic-gate 	struct kmem_bufctl	*bc_lastlog;	/* last log entry */
1330Sstevel@tonic-gate 	void			*bc_contents;	/* contents at last free */
1340Sstevel@tonic-gate 	int			bc_depth;	/* stack depth */
1350Sstevel@tonic-gate 	pc_t			bc_stack[KMEM_STACK_DEPTH];	/* pc stack */
1360Sstevel@tonic-gate } kmem_bufctl_audit_t;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate /*
1390Sstevel@tonic-gate  * A kmem_buftag structure is appended to each buffer whenever any of the
1400Sstevel@tonic-gate  * KMF_BUFTAG flags (KMF_DEADBEEF, KMF_REDZONE, KMF_VERIFY) are set.
1410Sstevel@tonic-gate  */
1420Sstevel@tonic-gate typedef struct kmem_buftag {
1430Sstevel@tonic-gate 	uint64_t		bt_redzone;	/* 64-bit redzone pattern */
1440Sstevel@tonic-gate 	kmem_bufctl_t		*bt_bufctl;	/* bufctl */
1450Sstevel@tonic-gate 	intptr_t		bt_bxstat;	/* bufctl ^ (alloc/free) */
1460Sstevel@tonic-gate } kmem_buftag_t;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate /*
1490Sstevel@tonic-gate  * A variant of the kmem_buftag structure used for KMF_LITE caches.
1500Sstevel@tonic-gate  * Previous callers are stored in reverse chronological order. (i.e. most
1510Sstevel@tonic-gate  * recent first)
1520Sstevel@tonic-gate  */
1530Sstevel@tonic-gate typedef struct kmem_buftag_lite {
1540Sstevel@tonic-gate 	kmem_buftag_t		bt_buftag;	/* a normal buftag */
1550Sstevel@tonic-gate 	pc_t			bt_history[1];	/* zero or more callers */
1560Sstevel@tonic-gate } kmem_buftag_lite_t;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate #define	KMEM_BUFTAG_LITE_SIZE(f)	\
1590Sstevel@tonic-gate 	(offsetof(kmem_buftag_lite_t, bt_history[f]))
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate #define	KMEM_BUFTAG(cp, buf)		\
1620Sstevel@tonic-gate 	((kmem_buftag_t *)((char *)(buf) + (cp)->cache_buftag))
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate #define	KMEM_BUFCTL(cp, buf)		\
1650Sstevel@tonic-gate 	((kmem_bufctl_t *)((char *)(buf) + (cp)->cache_bufctl))
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate #define	KMEM_BUF(cp, bcp)		\
1680Sstevel@tonic-gate 	((void *)((char *)(bcp) - (cp)->cache_bufctl))
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate #define	KMEM_SLAB(cp, buf)		\
1710Sstevel@tonic-gate 	((kmem_slab_t *)P2END((uintptr_t)(buf), (cp)->cache_slabsize) - 1)
1720Sstevel@tonic-gate 
1738887SMichael.Corcoran@Sun.COM /*
1748887SMichael.Corcoran@Sun.COM  * The "CPU" macro loads a cpu_t that refers to the cpu that the current
1758887SMichael.Corcoran@Sun.COM  * thread is running on at the time the macro is executed.  A context switch
1768887SMichael.Corcoran@Sun.COM  * may occur immediately after loading this data structure, leaving this
1778887SMichael.Corcoran@Sun.COM  * thread pointing at the cpu_t for the previous cpu.  This is not a problem;
1788887SMichael.Corcoran@Sun.COM  * we'd just end up checking the previous cpu's per-cpu cache, and then check
1798887SMichael.Corcoran@Sun.COM  * the other layers of the kmem cache if need be.
1808887SMichael.Corcoran@Sun.COM  *
1818887SMichael.Corcoran@Sun.COM  * It's not even a problem if the old cpu gets DR'ed out during the context
1828887SMichael.Corcoran@Sun.COM  * switch.  The cpu-remove DR operation bzero()s the cpu_t, but doesn't free
1838887SMichael.Corcoran@Sun.COM  * it.  So the cpu_t's cpu_cache_offset would read as 0, causing us to use
1848887SMichael.Corcoran@Sun.COM  * cpu 0's per-cpu cache.
1858887SMichael.Corcoran@Sun.COM  *
1868887SMichael.Corcoran@Sun.COM  * So, there is no need to disable kernel preemption while using the CPU macro
1878887SMichael.Corcoran@Sun.COM  * below since if we have been context switched, there will not be any
1888887SMichael.Corcoran@Sun.COM  * correctness problem, just a momentary use of a different per-cpu cache.
1898887SMichael.Corcoran@Sun.COM  */
1908887SMichael.Corcoran@Sun.COM 
1918887SMichael.Corcoran@Sun.COM #define	KMEM_CPU_CACHE(cp)						\
1928887SMichael.Corcoran@Sun.COM 	(kmem_cpu_cache_t *)((char *)(&cp->cache_cpu) + CPU->cpu_cache_offset)
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate #define	KMEM_MAGAZINE_VALID(cp, mp)	\
1950Sstevel@tonic-gate 	(((kmem_slab_t *)P2END((uintptr_t)(mp), PAGESIZE) - 1)->slab_cache == \
1960Sstevel@tonic-gate 	    (cp)->cache_magtype->mt_cache)
1970Sstevel@tonic-gate 
1986712Stomee #define	KMEM_SLAB_OFFSET(sp, buf)	\
1996712Stomee 	((size_t)((uintptr_t)(buf) - (uintptr_t)((sp)->slab_base)))
2006712Stomee 
2010Sstevel@tonic-gate #define	KMEM_SLAB_MEMBER(sp, buf)	\
2026712Stomee 	(KMEM_SLAB_OFFSET(sp, buf) < (sp)->slab_cache->cache_slabsize)
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate #define	KMEM_BUFTAG_ALLOC	0xa110c8edUL
2050Sstevel@tonic-gate #define	KMEM_BUFTAG_FREE	0xf4eef4eeUL
2060Sstevel@tonic-gate 
2076712Stomee /* slab_later_count thresholds */
2086712Stomee #define	KMEM_DISBELIEF		3
2096712Stomee 
2106712Stomee /* slab_flags */
2116712Stomee #define	KMEM_SLAB_NOMOVE	0x1
2126712Stomee #define	KMEM_SLAB_MOVE_PENDING	0x2
2136712Stomee 
2140Sstevel@tonic-gate typedef struct kmem_slab {
2150Sstevel@tonic-gate 	struct kmem_cache	*slab_cache;	/* controlling cache */
2160Sstevel@tonic-gate 	void			*slab_base;	/* base of allocated memory */
2176712Stomee 	avl_node_t		slab_link;	/* slab linkage */
2180Sstevel@tonic-gate 	struct kmem_bufctl	*slab_head;	/* first free buffer */
2190Sstevel@tonic-gate 	long			slab_refcnt;	/* outstanding allocations */
2200Sstevel@tonic-gate 	long			slab_chunks;	/* chunks (bufs) in this slab */
2216712Stomee 	uint32_t		slab_stuck_offset; /* unmoved buffer offset */
2226712Stomee 	uint16_t		slab_later_count; /* cf KMEM_CBRC_LATER */
2236712Stomee 	uint16_t		slab_flags;	/* bits to mark the slab */
2240Sstevel@tonic-gate } kmem_slab_t;
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate #define	KMEM_HASH_INITIAL	64
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate #define	KMEM_HASH(cp, buf)	\
2290Sstevel@tonic-gate 	((cp)->cache_hash_table +	\
2300Sstevel@tonic-gate 	(((uintptr_t)(buf) >> (cp)->cache_hash_shift) & (cp)->cache_hash_mask))
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate typedef struct kmem_magazine {
2330Sstevel@tonic-gate 	void	*mag_next;
2340Sstevel@tonic-gate 	void	*mag_round[1];		/* one or more rounds */
2350Sstevel@tonic-gate } kmem_magazine_t;
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate /*
2380Sstevel@tonic-gate  * The magazine types for fast per-cpu allocation
2390Sstevel@tonic-gate  */
2400Sstevel@tonic-gate typedef struct kmem_magtype {
2410Sstevel@tonic-gate 	int		mt_magsize;	/* magazine size (number of rounds) */
2420Sstevel@tonic-gate 	int		mt_align;	/* magazine alignment */
2430Sstevel@tonic-gate 	size_t		mt_minbuf;	/* all smaller buffers qualify */
2440Sstevel@tonic-gate 	size_t		mt_maxbuf;	/* no larger buffers qualify */
2450Sstevel@tonic-gate 	kmem_cache_t	*mt_cache;	/* magazine cache */
2460Sstevel@tonic-gate } kmem_magtype_t;
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate #define	KMEM_CPU_CACHE_SIZE	64	/* must be power of 2 */
2490Sstevel@tonic-gate #define	KMEM_CPU_PAD		(KMEM_CPU_CACHE_SIZE - sizeof (kmutex_t) - \
2500Sstevel@tonic-gate 	2 * sizeof (uint64_t) - 2 * sizeof (void *) - 4 * sizeof (int))
2510Sstevel@tonic-gate #define	KMEM_CACHE_SIZE(ncpus)	\
2520Sstevel@tonic-gate 	((size_t)(&((kmem_cache_t *)0)->cache_cpu[ncpus]))
2530Sstevel@tonic-gate 
2548887SMichael.Corcoran@Sun.COM /* Offset from kmem_cache->cache_cpu for per cpu caches */
2558887SMichael.Corcoran@Sun.COM #define	KMEM_CPU_CACHE_OFFSET(cpuid)					\
2568887SMichael.Corcoran@Sun.COM 	((size_t)(&((kmem_cache_t *)0)->cache_cpu[cpuid]) -		\
2578887SMichael.Corcoran@Sun.COM 	(size_t)(&((kmem_cache_t *)0)->cache_cpu))
2588887SMichael.Corcoran@Sun.COM 
2590Sstevel@tonic-gate typedef struct kmem_cpu_cache {
2600Sstevel@tonic-gate 	kmutex_t	cc_lock;	/* protects this cpu's local cache */
2610Sstevel@tonic-gate 	uint64_t	cc_alloc;	/* allocations from this cpu */
2620Sstevel@tonic-gate 	uint64_t	cc_free;	/* frees to this cpu */
2630Sstevel@tonic-gate 	kmem_magazine_t	*cc_loaded;	/* the currently loaded magazine */
2640Sstevel@tonic-gate 	kmem_magazine_t	*cc_ploaded;	/* the previously loaded magazine */
2650Sstevel@tonic-gate 	int		cc_rounds;	/* number of objects in loaded mag */
2660Sstevel@tonic-gate 	int		cc_prounds;	/* number of objects in previous mag */
2670Sstevel@tonic-gate 	int		cc_magsize;	/* number of rounds in a full mag */
2680Sstevel@tonic-gate 	int		cc_flags;	/* CPU-local copy of cache_flags */
2690Sstevel@tonic-gate 	char		cc_pad[KMEM_CPU_PAD]; /* for nice alignment */
2700Sstevel@tonic-gate } kmem_cpu_cache_t;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate /*
2730Sstevel@tonic-gate  * The magazine lists used in the depot.
2740Sstevel@tonic-gate  */
2750Sstevel@tonic-gate typedef struct kmem_maglist {
2760Sstevel@tonic-gate 	kmem_magazine_t	*ml_list;	/* magazine list */
2770Sstevel@tonic-gate 	long		ml_total;	/* number of magazines */
2780Sstevel@tonic-gate 	long		ml_min;		/* min since last update */
2790Sstevel@tonic-gate 	long		ml_reaplimit;	/* max reapable magazines */
2800Sstevel@tonic-gate 	uint64_t	ml_alloc;	/* allocations from this list */
2810Sstevel@tonic-gate } kmem_maglist_t;
2820Sstevel@tonic-gate 
2836712Stomee typedef struct kmem_defrag {
2846712Stomee 	/*
2856712Stomee 	 * Statistics
2866712Stomee 	 */
2876712Stomee 	uint64_t	kmd_callbacks;		/* move callbacks */
2886712Stomee 	uint64_t	kmd_yes;		/* KMEM_CBRC_YES responses */
2896712Stomee 	uint64_t	kmd_no;			/* NO responses */
2906712Stomee 	uint64_t	kmd_later;		/* LATER responses */
2916712Stomee 	uint64_t	kmd_dont_need;		/* DONT_NEED responses */
2926712Stomee 	uint64_t	kmd_dont_know;		/* DONT_KNOW responses */
2936712Stomee 	uint64_t	kmd_hunt_found;		/* DONT_KNOW: # found in mag */
294*10217STom.Erickson@Sun.COM 	uint64_t	kmd_slabs_freed;	/* slabs freed by moves */
295*10217STom.Erickson@Sun.COM 	uint64_t	kmd_defrags;		/* kmem_cache_defrag() */
296*10217STom.Erickson@Sun.COM 	uint64_t	kmd_scans;		/* kmem_cache_scan() */
2976712Stomee 
2986712Stomee 	/*
2996712Stomee 	 * Consolidator fields
3006712Stomee 	 */
3016712Stomee 	avl_tree_t	kmd_moves_pending;	/* buffer moves pending */
3026712Stomee 	list_t		kmd_deadlist;		/* deferred slab frees */
3036712Stomee 	size_t		kmd_deadcount;		/* # of slabs in kmd_deadlist */
3046712Stomee 	uint8_t		kmd_reclaim_numer;	/* slab usage threshold */
3056712Stomee 	uint8_t		kmd_pad1;		/* compiler padding */
306*10217STom.Erickson@Sun.COM 	uint16_t	kmd_consolidate;	/* triggers consolidator */
307*10217STom.Erickson@Sun.COM 	uint32_t	kmd_pad2;		/* compiler padding */
3086712Stomee 	size_t		kmd_slabs_sought;	/* reclaimable slabs sought */
3096712Stomee 	size_t		kmd_slabs_found;	/* reclaimable slabs found */
310*10217STom.Erickson@Sun.COM 	size_t		kmd_tries;		/* nth scan interval counter */
3116712Stomee 	/*
3126712Stomee 	 * Fields used to ASSERT that the client does not kmem_cache_free()
3136712Stomee 	 * objects passed to the move callback.
3146712Stomee 	 */
3156712Stomee 	void		*kmd_from_buf;		/* object to move */
3166712Stomee 	void		*kmd_to_buf;		/* move destination */
3176712Stomee 	kthread_t	*kmd_thread;		/* thread calling move */
3186712Stomee } kmem_defrag_t;
3196712Stomee 
3200Sstevel@tonic-gate #define	KMEM_CACHE_NAMELEN	31
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate struct kmem_cache {
3230Sstevel@tonic-gate 	/*
3240Sstevel@tonic-gate 	 * Statistics
3250Sstevel@tonic-gate 	 */
3260Sstevel@tonic-gate 	uint64_t	cache_slab_create;	/* slab creates */
3270Sstevel@tonic-gate 	uint64_t	cache_slab_destroy;	/* slab destroys */
3280Sstevel@tonic-gate 	uint64_t	cache_slab_alloc;	/* slab layer allocations */
3290Sstevel@tonic-gate 	uint64_t	cache_slab_free;	/* slab layer frees */
3300Sstevel@tonic-gate 	uint64_t	cache_alloc_fail;	/* total failed allocations */
3310Sstevel@tonic-gate 	uint64_t	cache_buftotal;		/* total buffers */
3320Sstevel@tonic-gate 	uint64_t	cache_bufmax;		/* max buffers ever */
3336306Stomee 	uint64_t	cache_bufslab;		/* buffers free in slab layer */
334*10217STom.Erickson@Sun.COM 	uint64_t	cache_reap;		/* cache reaps */
335*10217STom.Erickson@Sun.COM 	uint64_t	cache_rescale;		/* hash table rescales */
3360Sstevel@tonic-gate 	uint64_t	cache_lookup_depth;	/* hash lookup depth */
3370Sstevel@tonic-gate 	uint64_t	cache_depot_contention;	/* mutex contention count */
3380Sstevel@tonic-gate 	uint64_t	cache_depot_contention_prev; /* previous snapshot */
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	/*
3410Sstevel@tonic-gate 	 * Cache properties
3420Sstevel@tonic-gate 	 */
3430Sstevel@tonic-gate 	char		cache_name[KMEM_CACHE_NAMELEN + 1];
3440Sstevel@tonic-gate 	size_t		cache_bufsize;		/* object size */
3450Sstevel@tonic-gate 	size_t		cache_align;		/* object alignment */
3460Sstevel@tonic-gate 	int		(*cache_constructor)(void *, void *, int);
3470Sstevel@tonic-gate 	void		(*cache_destructor)(void *, void *);
3480Sstevel@tonic-gate 	void		(*cache_reclaim)(void *);
3496712Stomee 	kmem_cbrc_t	(*cache_move)(void *, void *, size_t, void *);
3500Sstevel@tonic-gate 	void		*cache_private;		/* opaque arg to callbacks */
3510Sstevel@tonic-gate 	vmem_t		*cache_arena;		/* vmem source for slabs */
3520Sstevel@tonic-gate 	int		cache_cflags;		/* cache creation flags */
3530Sstevel@tonic-gate 	int		cache_flags;		/* various cache state info */
3540Sstevel@tonic-gate 	uint32_t	cache_mtbf;		/* induced alloc failure rate */
3556712Stomee 	uint32_t	cache_pad1;		/* compiler padding */
3560Sstevel@tonic-gate 	kstat_t		*cache_kstat;		/* exported statistics */
3576712Stomee 	list_node_t	cache_link;		/* cache linkage */
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	/*
3600Sstevel@tonic-gate 	 * Slab layer
3610Sstevel@tonic-gate 	 */
3620Sstevel@tonic-gate 	kmutex_t	cache_lock;		/* protects slab layer */
3630Sstevel@tonic-gate 	size_t		cache_chunksize;	/* buf + alignment [+ debug] */
3640Sstevel@tonic-gate 	size_t		cache_slabsize;		/* size of a slab */
3656712Stomee 	size_t		cache_maxchunks;	/* max buffers per slab */
3660Sstevel@tonic-gate 	size_t		cache_bufctl;		/* buf-to-bufctl distance */
3670Sstevel@tonic-gate 	size_t		cache_buftag;		/* buf-to-buftag distance */
3680Sstevel@tonic-gate 	size_t		cache_verify;		/* bytes to verify */
3690Sstevel@tonic-gate 	size_t		cache_contents;		/* bytes of saved content */
3700Sstevel@tonic-gate 	size_t		cache_color;		/* next slab color */
3710Sstevel@tonic-gate 	size_t		cache_mincolor;		/* maximum slab color */
3720Sstevel@tonic-gate 	size_t		cache_maxcolor;		/* maximum slab color */
3730Sstevel@tonic-gate 	size_t		cache_hash_shift;	/* get to interesting bits */
3740Sstevel@tonic-gate 	size_t		cache_hash_mask;	/* hash table mask */
3756712Stomee 	list_t		cache_complete_slabs;	/* completely allocated slabs */
3766712Stomee 	size_t		cache_complete_slab_count;
3776712Stomee 	avl_tree_t	cache_partial_slabs;	/* partial slab freelist */
3786712Stomee 	size_t		cache_partial_binshift;	/* for AVL sort bins */
3790Sstevel@tonic-gate 	kmem_cache_t	*cache_bufctl_cache;	/* source of bufctls */
3800Sstevel@tonic-gate 	kmem_bufctl_t	**cache_hash_table;	/* hash table base */
3816712Stomee 	kmem_defrag_t	*cache_defrag;		/* slab consolidator fields */
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	/*
3840Sstevel@tonic-gate 	 * Depot layer
3850Sstevel@tonic-gate 	 */
3860Sstevel@tonic-gate 	kmutex_t	cache_depot_lock;	/* protects depot */
3870Sstevel@tonic-gate 	kmem_magtype_t	*cache_magtype;		/* magazine type */
3880Sstevel@tonic-gate 	kmem_maglist_t	cache_full;		/* full magazines */
3890Sstevel@tonic-gate 	kmem_maglist_t	cache_empty;		/* empty magazines */
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 	/*
3920Sstevel@tonic-gate 	 * Per-CPU layer
3930Sstevel@tonic-gate 	 */
3940Sstevel@tonic-gate 	kmem_cpu_cache_t cache_cpu[1];		/* max_ncpus actual elements */
3950Sstevel@tonic-gate };
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate typedef struct kmem_cpu_log_header {
3980Sstevel@tonic-gate 	kmutex_t	clh_lock;
3990Sstevel@tonic-gate 	char		*clh_current;
4000Sstevel@tonic-gate 	size_t		clh_avail;
4010Sstevel@tonic-gate 	int		clh_chunk;
4020Sstevel@tonic-gate 	int		clh_hits;
4030Sstevel@tonic-gate 	char		clh_pad[64 - sizeof (kmutex_t) - sizeof (char *) -
4040Sstevel@tonic-gate 				sizeof (size_t) - 2 * sizeof (int)];
4050Sstevel@tonic-gate } kmem_cpu_log_header_t;
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate typedef struct kmem_log_header {
4080Sstevel@tonic-gate 	kmutex_t	lh_lock;
4090Sstevel@tonic-gate 	char		*lh_base;
4100Sstevel@tonic-gate 	int		*lh_free;
4110Sstevel@tonic-gate 	size_t		lh_chunksize;
4120Sstevel@tonic-gate 	int		lh_nchunks;
4130Sstevel@tonic-gate 	int		lh_head;
4140Sstevel@tonic-gate 	int		lh_tail;
4150Sstevel@tonic-gate 	int		lh_hits;
4160Sstevel@tonic-gate 	kmem_cpu_log_header_t lh_cpu[1];	/* ncpus actually allocated */
4170Sstevel@tonic-gate } kmem_log_header_t;
4180Sstevel@tonic-gate 
4196712Stomee /* kmem_move kmm_flags */
4206712Stomee #define	KMM_DESPERATE		0x1
4216712Stomee #define	KMM_NOTIFY		0x2
422*10217STom.Erickson@Sun.COM #define	KMM_DEBUG		0x4
4236712Stomee 
4246712Stomee typedef struct kmem_move {
4256712Stomee 	kmem_slab_t	*kmm_from_slab;
4266712Stomee 	void		*kmm_from_buf;
4276712Stomee 	void		*kmm_to_buf;
4286712Stomee 	avl_node_t	kmm_entry;
4296712Stomee 	int		kmm_flags;
4306712Stomee } kmem_move_t;
4316712Stomee 
4326712Stomee /*
4336712Stomee  * In order to consolidate partial slabs, it must be possible for the cache to
4346712Stomee  * have partial slabs.
4356712Stomee  */
4366712Stomee #define	KMEM_IS_MOVABLE(cp)						\
4376712Stomee 	(((cp)->cache_chunksize * 2) <= (cp)->cache_slabsize)
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate #ifdef	__cplusplus
4400Sstevel@tonic-gate }
4410Sstevel@tonic-gate #endif
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate #endif	/* _SYS_KMEM_IMPL_H */
444