xref: /titanic_51/usr/src/uts/common/sys/kmem_impl.h (revision aa7175ab1e9c016f282f811d2b17381e6bcba1e8)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
59f1b636aStomee  * Common Development and Distribution License (the "License").
69f1b636aStomee  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
219f1b636aStomee 
227c478bd9Sstevel@tonic-gate /*
23b942e89bSDavid Valin  * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
24*3608e2e0SJohn Levon  * Copyright 2018 Joyent, Inc.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #ifndef _SYS_KMEM_IMPL_H
287c478bd9Sstevel@tonic-gate #define	_SYS_KMEM_IMPL_H
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
317c478bd9Sstevel@tonic-gate #include <sys/vmem.h>
327c478bd9Sstevel@tonic-gate #include <sys/thread.h>
337c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
347c478bd9Sstevel@tonic-gate #include <sys/time.h>
357c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
367c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
377c478bd9Sstevel@tonic-gate #include <sys/systm.h>
387c478bd9Sstevel@tonic-gate #include <vm/page.h>
39b5fca8f8Stomee #include <sys/avl.h>
40b5fca8f8Stomee #include <sys/list.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
437c478bd9Sstevel@tonic-gate extern "C" {
447c478bd9Sstevel@tonic-gate #endif
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * kernel memory allocator: implementation-private data structures
48b5fca8f8Stomee  *
49b5fca8f8Stomee  * Lock order:
50b5fca8f8Stomee  * 1. cache_lock
51b5fca8f8Stomee  * 2. cc_lock in order by CPU ID
52b5fca8f8Stomee  * 3. cache_depot_lock
53b5fca8f8Stomee  *
54b5fca8f8Stomee  * Do not call kmem_cache_alloc() or taskq_dispatch() while holding any of the
55b5fca8f8Stomee  * above locks.
567c478bd9Sstevel@tonic-gate  */
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #define	KMF_AUDIT	0x00000001	/* transaction auditing */
597c478bd9Sstevel@tonic-gate #define	KMF_DEADBEEF	0x00000002	/* deadbeef checking */
607c478bd9Sstevel@tonic-gate #define	KMF_REDZONE	0x00000004	/* redzone checking */
617c478bd9Sstevel@tonic-gate #define	KMF_CONTENTS	0x00000008	/* freed-buffer content logging */
627c478bd9Sstevel@tonic-gate #define	KMF_STICKY	0x00000010	/* if set, override /etc/system */
637c478bd9Sstevel@tonic-gate #define	KMF_NOMAGAZINE	0x00000020	/* disable per-cpu magazines */
647c478bd9Sstevel@tonic-gate #define	KMF_FIREWALL	0x00000040	/* put all bufs before unmapped pages */
657c478bd9Sstevel@tonic-gate #define	KMF_LITE	0x00000100	/* lightweight debugging */
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate #define	KMF_HASH	0x00000200	/* cache has hash table */
687c478bd9Sstevel@tonic-gate #define	KMF_RANDOMIZE	0x00000400	/* randomize other kmem_flags */
697c478bd9Sstevel@tonic-gate 
709dd77bc8SDave Plauger #define	KMF_DUMPDIVERT	0x00001000	/* use alternate memory at dump time */
719dd77bc8SDave Plauger #define	KMF_DUMPUNSAFE	0x00002000	/* flag caches used at dump time */
72b942e89bSDavid Valin #define	KMF_PREFILL	0x00004000	/* Prefill the slab when created. */
739dd77bc8SDave Plauger 
747c478bd9Sstevel@tonic-gate #define	KMF_BUFTAG	(KMF_DEADBEEF | KMF_REDZONE)
757c478bd9Sstevel@tonic-gate #define	KMF_TOUCH	(KMF_BUFTAG | KMF_LITE | KMF_CONTENTS)
767c478bd9Sstevel@tonic-gate #define	KMF_RANDOM	(KMF_TOUCH | KMF_AUDIT | KMF_NOMAGAZINE)
777c478bd9Sstevel@tonic-gate #define	KMF_DEBUG	(KMF_RANDOM | KMF_FIREWALL)
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate #define	KMEM_STACK_DEPTH	15
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate #define	KMEM_FREE_PATTERN		0xdeadbeefdeadbeefULL
827c478bd9Sstevel@tonic-gate #define	KMEM_UNINITIALIZED_PATTERN	0xbaddcafebaddcafeULL
837c478bd9Sstevel@tonic-gate #define	KMEM_REDZONE_PATTERN		0xfeedfacefeedfaceULL
847c478bd9Sstevel@tonic-gate #define	KMEM_REDZONE_BYTE		0xbb
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate  * Redzone size encodings for kmem_alloc() / kmem_free().  We encode the
887c478bd9Sstevel@tonic-gate  * allocation size, rather than storing it directly, so that kmem_free()
897c478bd9Sstevel@tonic-gate  * can distinguish frees of the wrong size from redzone violations.
907c478bd9Sstevel@tonic-gate  *
917c478bd9Sstevel@tonic-gate  * A size of zero is never valid.
927c478bd9Sstevel@tonic-gate  */
937c478bd9Sstevel@tonic-gate #define	KMEM_SIZE_ENCODE(x)	(251 * (x) + 1)
947c478bd9Sstevel@tonic-gate #define	KMEM_SIZE_DECODE(x)	((x) / 251)
957c478bd9Sstevel@tonic-gate #define	KMEM_SIZE_VALID(x)	((x) % 251 == 1 && (x) != 1)
967c478bd9Sstevel@tonic-gate 
97b5fca8f8Stomee 
98b5fca8f8Stomee #define	KMEM_ALIGN		8	/* min guaranteed alignment */
99b5fca8f8Stomee #define	KMEM_ALIGN_SHIFT	3	/* log2(KMEM_ALIGN) */
100b5fca8f8Stomee #define	KMEM_VOID_FRACTION	8	/* never waste more than 1/8 of slab */
101b5fca8f8Stomee 
102b5fca8f8Stomee #define	KMEM_SLAB_IS_PARTIAL(sp)		\
103b5fca8f8Stomee 	((sp)->slab_refcnt > 0 && (sp)->slab_refcnt < (sp)->slab_chunks)
104b5fca8f8Stomee #define	KMEM_SLAB_IS_ALL_USED(sp)		\
105b5fca8f8Stomee 	((sp)->slab_refcnt == (sp)->slab_chunks)
106b5fca8f8Stomee 
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate  * The bufctl (buffer control) structure keeps some minimal information
1097c478bd9Sstevel@tonic-gate  * about each buffer: its address, its slab, and its current linkage,
1107c478bd9Sstevel@tonic-gate  * which is either on the slab's freelist (if the buffer is free), or
1117c478bd9Sstevel@tonic-gate  * on the cache's buf-to-bufctl hash table (if the buffer is allocated).
1127c478bd9Sstevel@tonic-gate  * In the case of non-hashed, or "raw", caches (the common case), only
1137c478bd9Sstevel@tonic-gate  * the freelist linkage is necessary: the buffer address is at a fixed
1147c478bd9Sstevel@tonic-gate  * offset from the bufctl address, and the slab is at the end of the page.
1157c478bd9Sstevel@tonic-gate  *
1167c478bd9Sstevel@tonic-gate  * NOTE: bc_next must be the first field; raw buffers have linkage only.
1177c478bd9Sstevel@tonic-gate  */
1187c478bd9Sstevel@tonic-gate typedef struct kmem_bufctl {
1197c478bd9Sstevel@tonic-gate 	struct kmem_bufctl	*bc_next;	/* next bufctl struct */
1207c478bd9Sstevel@tonic-gate 	void			*bc_addr;	/* address of buffer */
1217c478bd9Sstevel@tonic-gate 	struct kmem_slab	*bc_slab;	/* controlling slab */
1227c478bd9Sstevel@tonic-gate } kmem_bufctl_t;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate /*
1257c478bd9Sstevel@tonic-gate  * The KMF_AUDIT version of the bufctl structure.  The beginning of this
1267c478bd9Sstevel@tonic-gate  * structure must be identical to the normal bufctl structure so that
1277c478bd9Sstevel@tonic-gate  * pointers are interchangeable.
1287c478bd9Sstevel@tonic-gate  */
1297c478bd9Sstevel@tonic-gate typedef struct kmem_bufctl_audit {
1307c478bd9Sstevel@tonic-gate 	struct kmem_bufctl	*bc_next;	/* next bufctl struct */
1317c478bd9Sstevel@tonic-gate 	void			*bc_addr;	/* address of buffer */
1327c478bd9Sstevel@tonic-gate 	struct kmem_slab	*bc_slab;	/* controlling slab */
1337c478bd9Sstevel@tonic-gate 	kmem_cache_t		*bc_cache;	/* controlling cache */
1347c478bd9Sstevel@tonic-gate 	hrtime_t		bc_timestamp;	/* transaction time */
1357c478bd9Sstevel@tonic-gate 	kthread_t		*bc_thread;	/* thread doing transaction */
1367c478bd9Sstevel@tonic-gate 	struct kmem_bufctl	*bc_lastlog;	/* last log entry */
1377c478bd9Sstevel@tonic-gate 	void			*bc_contents;	/* contents at last free */
1387c478bd9Sstevel@tonic-gate 	int			bc_depth;	/* stack depth */
1397c478bd9Sstevel@tonic-gate 	pc_t			bc_stack[KMEM_STACK_DEPTH];	/* pc stack */
1407c478bd9Sstevel@tonic-gate } kmem_bufctl_audit_t;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate /*
1437c478bd9Sstevel@tonic-gate  * A kmem_buftag structure is appended to each buffer whenever any of the
1447c478bd9Sstevel@tonic-gate  * KMF_BUFTAG flags (KMF_DEADBEEF, KMF_REDZONE, KMF_VERIFY) are set.
1457c478bd9Sstevel@tonic-gate  */
1467c478bd9Sstevel@tonic-gate typedef struct kmem_buftag {
1477c478bd9Sstevel@tonic-gate 	uint64_t		bt_redzone;	/* 64-bit redzone pattern */
1487c478bd9Sstevel@tonic-gate 	kmem_bufctl_t		*bt_bufctl;	/* bufctl */
1497c478bd9Sstevel@tonic-gate 	intptr_t		bt_bxstat;	/* bufctl ^ (alloc/free) */
1507c478bd9Sstevel@tonic-gate } kmem_buftag_t;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /*
1537c478bd9Sstevel@tonic-gate  * A variant of the kmem_buftag structure used for KMF_LITE caches.
1547c478bd9Sstevel@tonic-gate  * Previous callers are stored in reverse chronological order. (i.e. most
1557c478bd9Sstevel@tonic-gate  * recent first)
1567c478bd9Sstevel@tonic-gate  */
1577c478bd9Sstevel@tonic-gate typedef struct kmem_buftag_lite {
1587c478bd9Sstevel@tonic-gate 	kmem_buftag_t		bt_buftag;	/* a normal buftag */
1597c478bd9Sstevel@tonic-gate 	pc_t			bt_history[1];	/* zero or more callers */
1607c478bd9Sstevel@tonic-gate } kmem_buftag_lite_t;
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate #define	KMEM_BUFTAG_LITE_SIZE(f)	\
1637c478bd9Sstevel@tonic-gate 	(offsetof(kmem_buftag_lite_t, bt_history[f]))
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate #define	KMEM_BUFTAG(cp, buf)		\
1667c478bd9Sstevel@tonic-gate 	((kmem_buftag_t *)((char *)(buf) + (cp)->cache_buftag))
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate #define	KMEM_BUFCTL(cp, buf)		\
1697c478bd9Sstevel@tonic-gate 	((kmem_bufctl_t *)((char *)(buf) + (cp)->cache_bufctl))
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate #define	KMEM_BUF(cp, bcp)		\
1727c478bd9Sstevel@tonic-gate 	((void *)((char *)(bcp) - (cp)->cache_bufctl))
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate #define	KMEM_SLAB(cp, buf)		\
1757c478bd9Sstevel@tonic-gate 	((kmem_slab_t *)P2END((uintptr_t)(buf), (cp)->cache_slabsize) - 1)
1767c478bd9Sstevel@tonic-gate 
1772af6eb52SMichael Corcoran /*
1789dd77bc8SDave Plauger  * Test for using alternate memory at dump time.
1799dd77bc8SDave Plauger  */
1809dd77bc8SDave Plauger #define	KMEM_DUMP(cp)		((cp)->cache_flags & KMF_DUMPDIVERT)
1819dd77bc8SDave Plauger #define	KMEM_DUMPCC(ccp)	((ccp)->cc_flags & KMF_DUMPDIVERT)
1829dd77bc8SDave Plauger 
1839dd77bc8SDave Plauger /*
1842af6eb52SMichael Corcoran  * The "CPU" macro loads a cpu_t that refers to the cpu that the current
1852af6eb52SMichael Corcoran  * thread is running on at the time the macro is executed.  A context switch
1862af6eb52SMichael Corcoran  * may occur immediately after loading this data structure, leaving this
1872af6eb52SMichael Corcoran  * thread pointing at the cpu_t for the previous cpu.  This is not a problem;
1882af6eb52SMichael Corcoran  * we'd just end up checking the previous cpu's per-cpu cache, and then check
1892af6eb52SMichael Corcoran  * the other layers of the kmem cache if need be.
1902af6eb52SMichael Corcoran  *
1912af6eb52SMichael Corcoran  * It's not even a problem if the old cpu gets DR'ed out during the context
1922af6eb52SMichael Corcoran  * switch.  The cpu-remove DR operation bzero()s the cpu_t, but doesn't free
1932af6eb52SMichael Corcoran  * it.  So the cpu_t's cpu_cache_offset would read as 0, causing us to use
1942af6eb52SMichael Corcoran  * cpu 0's per-cpu cache.
1952af6eb52SMichael Corcoran  *
1962af6eb52SMichael Corcoran  * So, there is no need to disable kernel preemption while using the CPU macro
1972af6eb52SMichael Corcoran  * below since if we have been context switched, there will not be any
1982af6eb52SMichael Corcoran  * correctness problem, just a momentary use of a different per-cpu cache.
1992af6eb52SMichael Corcoran  */
2002af6eb52SMichael Corcoran 
2017c478bd9Sstevel@tonic-gate #define	KMEM_CPU_CACHE(cp)						\
2029dd77bc8SDave Plauger 	((kmem_cpu_cache_t *)((char *)(&cp->cache_cpu) + CPU->cpu_cache_offset))
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate #define	KMEM_MAGAZINE_VALID(cp, mp)	\
2057c478bd9Sstevel@tonic-gate 	(((kmem_slab_t *)P2END((uintptr_t)(mp), PAGESIZE) - 1)->slab_cache == \
2067c478bd9Sstevel@tonic-gate 	    (cp)->cache_magtype->mt_cache)
2077c478bd9Sstevel@tonic-gate 
208b5fca8f8Stomee #define	KMEM_SLAB_OFFSET(sp, buf)	\
209b5fca8f8Stomee 	((size_t)((uintptr_t)(buf) - (uintptr_t)((sp)->slab_base)))
210b5fca8f8Stomee 
2117c478bd9Sstevel@tonic-gate #define	KMEM_SLAB_MEMBER(sp, buf)	\
212b5fca8f8Stomee 	(KMEM_SLAB_OFFSET(sp, buf) < (sp)->slab_cache->cache_slabsize)
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate #define	KMEM_BUFTAG_ALLOC	0xa110c8edUL
2157c478bd9Sstevel@tonic-gate #define	KMEM_BUFTAG_FREE	0xf4eef4eeUL
2167c478bd9Sstevel@tonic-gate 
217b5fca8f8Stomee /* slab_later_count thresholds */
218b5fca8f8Stomee #define	KMEM_DISBELIEF		3
219b5fca8f8Stomee 
220b5fca8f8Stomee /* slab_flags */
221b5fca8f8Stomee #define	KMEM_SLAB_NOMOVE	0x1
222b5fca8f8Stomee #define	KMEM_SLAB_MOVE_PENDING	0x2
223b5fca8f8Stomee 
2247c478bd9Sstevel@tonic-gate typedef struct kmem_slab {
2257c478bd9Sstevel@tonic-gate 	struct kmem_cache	*slab_cache;	/* controlling cache */
2267c478bd9Sstevel@tonic-gate 	void			*slab_base;	/* base of allocated memory */
227b5fca8f8Stomee 	avl_node_t		slab_link;	/* slab linkage */
2287c478bd9Sstevel@tonic-gate 	struct kmem_bufctl	*slab_head;	/* first free buffer */
2297c478bd9Sstevel@tonic-gate 	long			slab_refcnt;	/* outstanding allocations */
2307c478bd9Sstevel@tonic-gate 	long			slab_chunks;	/* chunks (bufs) in this slab */
231b5fca8f8Stomee 	uint32_t		slab_stuck_offset; /* unmoved buffer offset */
232b5fca8f8Stomee 	uint16_t		slab_later_count; /* cf KMEM_CBRC_LATER */
233b5fca8f8Stomee 	uint16_t		slab_flags;	/* bits to mark the slab */
2347c478bd9Sstevel@tonic-gate } kmem_slab_t;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate #define	KMEM_HASH_INITIAL	64
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate #define	KMEM_HASH(cp, buf)	\
2397c478bd9Sstevel@tonic-gate 	((cp)->cache_hash_table +	\
2407c478bd9Sstevel@tonic-gate 	(((uintptr_t)(buf) >> (cp)->cache_hash_shift) & (cp)->cache_hash_mask))
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate typedef struct kmem_magazine {
2437c478bd9Sstevel@tonic-gate 	void	*mag_next;
2447c478bd9Sstevel@tonic-gate 	void	*mag_round[1];		/* one or more rounds */
2457c478bd9Sstevel@tonic-gate } kmem_magazine_t;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate /*
2487c478bd9Sstevel@tonic-gate  * The magazine types for fast per-cpu allocation
2497c478bd9Sstevel@tonic-gate  */
2507c478bd9Sstevel@tonic-gate typedef struct kmem_magtype {
2519dd77bc8SDave Plauger 	short		mt_magsize;	/* magazine size (number of rounds) */
2527c478bd9Sstevel@tonic-gate 	int		mt_align;	/* magazine alignment */
2537c478bd9Sstevel@tonic-gate 	size_t		mt_minbuf;	/* all smaller buffers qualify */
2547c478bd9Sstevel@tonic-gate 	size_t		mt_maxbuf;	/* no larger buffers qualify */
2557c478bd9Sstevel@tonic-gate 	kmem_cache_t	*mt_cache;	/* magazine cache */
2567c478bd9Sstevel@tonic-gate } kmem_magtype_t;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate #define	KMEM_CPU_CACHE_SIZE	64	/* must be power of 2 */
2597c478bd9Sstevel@tonic-gate #define	KMEM_CPU_PAD		(KMEM_CPU_CACHE_SIZE - sizeof (kmutex_t) - \
2609dd77bc8SDave Plauger 	2 * sizeof (uint64_t) - 2 * sizeof (void *) - sizeof (int) - \
2619dd77bc8SDave Plauger 	5 * sizeof (short))
2627c478bd9Sstevel@tonic-gate #define	KMEM_CACHE_SIZE(ncpus)	\
2637c478bd9Sstevel@tonic-gate 	((size_t)(&((kmem_cache_t *)0)->cache_cpu[ncpus]))
2647c478bd9Sstevel@tonic-gate 
2652af6eb52SMichael Corcoran /* Offset from kmem_cache->cache_cpu for per cpu caches */
2662af6eb52SMichael Corcoran #define	KMEM_CPU_CACHE_OFFSET(cpuid)					\
2672af6eb52SMichael Corcoran 	((size_t)(&((kmem_cache_t *)0)->cache_cpu[cpuid]) -		\
2682af6eb52SMichael Corcoran 	(size_t)(&((kmem_cache_t *)0)->cache_cpu))
2692af6eb52SMichael Corcoran 
2707c478bd9Sstevel@tonic-gate typedef struct kmem_cpu_cache {
2717c478bd9Sstevel@tonic-gate 	kmutex_t	cc_lock;	/* protects this cpu's local cache */
2727c478bd9Sstevel@tonic-gate 	uint64_t	cc_alloc;	/* allocations from this cpu */
2737c478bd9Sstevel@tonic-gate 	uint64_t	cc_free;	/* frees to this cpu */
2747c478bd9Sstevel@tonic-gate 	kmem_magazine_t	*cc_loaded;	/* the currently loaded magazine */
2757c478bd9Sstevel@tonic-gate 	kmem_magazine_t	*cc_ploaded;	/* the previously loaded magazine */
2767c478bd9Sstevel@tonic-gate 	int		cc_flags;	/* CPU-local copy of cache_flags */
2779dd77bc8SDave Plauger 	short		cc_rounds;	/* number of objects in loaded mag */
2789dd77bc8SDave Plauger 	short		cc_prounds;	/* number of objects in previous mag */
2799dd77bc8SDave Plauger 	short		cc_magsize;	/* number of rounds in a full mag */
2809dd77bc8SDave Plauger 	short		cc_dump_rounds;	/* dump time copy of cc_rounds */
2819dd77bc8SDave Plauger 	short		cc_dump_prounds; /* dump time copy of cc_prounds */
2827c478bd9Sstevel@tonic-gate 	char		cc_pad[KMEM_CPU_PAD]; /* for nice alignment */
2837c478bd9Sstevel@tonic-gate } kmem_cpu_cache_t;
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate /*
2867c478bd9Sstevel@tonic-gate  * The magazine lists used in the depot.
2877c478bd9Sstevel@tonic-gate  */
2887c478bd9Sstevel@tonic-gate typedef struct kmem_maglist {
2897c478bd9Sstevel@tonic-gate 	kmem_magazine_t	*ml_list;	/* magazine list */
2907c478bd9Sstevel@tonic-gate 	long		ml_total;	/* number of magazines */
2917c478bd9Sstevel@tonic-gate 	long		ml_min;		/* min since last update */
2927c478bd9Sstevel@tonic-gate 	long		ml_reaplimit;	/* max reapable magazines */
2937c478bd9Sstevel@tonic-gate 	uint64_t	ml_alloc;	/* allocations from this list */
2947c478bd9Sstevel@tonic-gate } kmem_maglist_t;
2957c478bd9Sstevel@tonic-gate 
296b5fca8f8Stomee typedef struct kmem_defrag {
297b5fca8f8Stomee 	/*
298b5fca8f8Stomee 	 * Statistics
299b5fca8f8Stomee 	 */
300b5fca8f8Stomee 	uint64_t	kmd_callbacks;		/* move callbacks */
301b5fca8f8Stomee 	uint64_t	kmd_yes;		/* KMEM_CBRC_YES responses */
302b5fca8f8Stomee 	uint64_t	kmd_no;			/* NO responses */
303b5fca8f8Stomee 	uint64_t	kmd_later;		/* LATER responses */
304b5fca8f8Stomee 	uint64_t	kmd_dont_need;		/* DONT_NEED responses */
305b5fca8f8Stomee 	uint64_t	kmd_dont_know;		/* DONT_KNOW responses */
306686031edSTom Erickson 	uint64_t	kmd_slabs_freed;	/* slabs freed by moves */
307686031edSTom Erickson 	uint64_t	kmd_defrags;		/* kmem_cache_defrag() */
308686031edSTom Erickson 	uint64_t	kmd_scans;		/* kmem_cache_scan() */
309b5fca8f8Stomee 
310b5fca8f8Stomee 	/*
311b5fca8f8Stomee 	 * Consolidator fields
312b5fca8f8Stomee 	 */
313b5fca8f8Stomee 	avl_tree_t	kmd_moves_pending;	/* buffer moves pending */
314b5fca8f8Stomee 	list_t		kmd_deadlist;		/* deferred slab frees */
315b5fca8f8Stomee 	size_t		kmd_deadcount;		/* # of slabs in kmd_deadlist */
316b5fca8f8Stomee 	uint8_t		kmd_reclaim_numer;	/* slab usage threshold */
317b5fca8f8Stomee 	uint8_t		kmd_pad1;		/* compiler padding */
318686031edSTom Erickson 	uint16_t	kmd_consolidate;	/* triggers consolidator */
319686031edSTom Erickson 	uint32_t	kmd_pad2;		/* compiler padding */
320b5fca8f8Stomee 	size_t		kmd_slabs_sought;	/* reclaimable slabs sought */
321b5fca8f8Stomee 	size_t		kmd_slabs_found;	/* reclaimable slabs found */
322686031edSTom Erickson 	size_t		kmd_tries;		/* nth scan interval counter */
323b5fca8f8Stomee 	/*
324b5fca8f8Stomee 	 * Fields used to ASSERT that the client does not kmem_cache_free()
325b5fca8f8Stomee 	 * objects passed to the move callback.
326b5fca8f8Stomee 	 */
327b5fca8f8Stomee 	void		*kmd_from_buf;		/* object to move */
328b5fca8f8Stomee 	void		*kmd_to_buf;		/* move destination */
329b5fca8f8Stomee 	kthread_t	*kmd_thread;		/* thread calling move */
330b5fca8f8Stomee } kmem_defrag_t;
331b5fca8f8Stomee 
332*3608e2e0SJohn Levon typedef struct kmem_dump {
333*3608e2e0SJohn Levon 	void		*kd_freelist;		/* heap during crash dump */
334*3608e2e0SJohn Levon 	uint_t		kd_alloc_fails;		/* # of allocation failures */
335*3608e2e0SJohn Levon 	uint_t		kd_unsafe;		/* cache was used, but unsafe */
336*3608e2e0SJohn Levon } kmem_dump_t;
337*3608e2e0SJohn Levon 
3387c478bd9Sstevel@tonic-gate #define	KMEM_CACHE_NAMELEN	31
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate struct kmem_cache {
3417c478bd9Sstevel@tonic-gate 	/*
3427c478bd9Sstevel@tonic-gate 	 * Statistics
3437c478bd9Sstevel@tonic-gate 	 */
3447c478bd9Sstevel@tonic-gate 	uint64_t	cache_slab_create;	/* slab creates */
3457c478bd9Sstevel@tonic-gate 	uint64_t	cache_slab_destroy;	/* slab destroys */
3467c478bd9Sstevel@tonic-gate 	uint64_t	cache_slab_alloc;	/* slab layer allocations */
3477c478bd9Sstevel@tonic-gate 	uint64_t	cache_slab_free;	/* slab layer frees */
3487c478bd9Sstevel@tonic-gate 	uint64_t	cache_alloc_fail;	/* total failed allocations */
3497c478bd9Sstevel@tonic-gate 	uint64_t	cache_buftotal;		/* total buffers */
3507c478bd9Sstevel@tonic-gate 	uint64_t	cache_bufmax;		/* max buffers ever */
3519f1b636aStomee 	uint64_t	cache_bufslab;		/* buffers free in slab layer */
352686031edSTom Erickson 	uint64_t	cache_reap;		/* cache reaps */
353686031edSTom Erickson 	uint64_t	cache_rescale;		/* hash table rescales */
3547c478bd9Sstevel@tonic-gate 	uint64_t	cache_lookup_depth;	/* hash lookup depth */
3557c478bd9Sstevel@tonic-gate 	uint64_t	cache_depot_contention;	/* mutex contention count */
3567c478bd9Sstevel@tonic-gate 	uint64_t	cache_depot_contention_prev; /* previous snapshot */
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	/*
3597c478bd9Sstevel@tonic-gate 	 * Cache properties
3607c478bd9Sstevel@tonic-gate 	 */
3617c478bd9Sstevel@tonic-gate 	char		cache_name[KMEM_CACHE_NAMELEN + 1];
3627c478bd9Sstevel@tonic-gate 	size_t		cache_bufsize;		/* object size */
3637c478bd9Sstevel@tonic-gate 	size_t		cache_align;		/* object alignment */
3647c478bd9Sstevel@tonic-gate 	int		(*cache_constructor)(void *, void *, int);
3657c478bd9Sstevel@tonic-gate 	void		(*cache_destructor)(void *, void *);
3667c478bd9Sstevel@tonic-gate 	void		(*cache_reclaim)(void *);
367b5fca8f8Stomee 	kmem_cbrc_t	(*cache_move)(void *, void *, size_t, void *);
3687c478bd9Sstevel@tonic-gate 	void		*cache_private;		/* opaque arg to callbacks */
3697c478bd9Sstevel@tonic-gate 	vmem_t		*cache_arena;		/* vmem source for slabs */
3707c478bd9Sstevel@tonic-gate 	int		cache_cflags;		/* cache creation flags */
3717c478bd9Sstevel@tonic-gate 	int		cache_flags;		/* various cache state info */
3727c478bd9Sstevel@tonic-gate 	uint32_t	cache_mtbf;		/* induced alloc failure rate */
373b5fca8f8Stomee 	uint32_t	cache_pad1;		/* compiler padding */
3747c478bd9Sstevel@tonic-gate 	kstat_t		*cache_kstat;		/* exported statistics */
375b5fca8f8Stomee 	list_node_t	cache_link;		/* cache linkage */
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	/*
3787c478bd9Sstevel@tonic-gate 	 * Slab layer
3797c478bd9Sstevel@tonic-gate 	 */
3807c478bd9Sstevel@tonic-gate 	kmutex_t	cache_lock;		/* protects slab layer */
3817c478bd9Sstevel@tonic-gate 	size_t		cache_chunksize;	/* buf + alignment [+ debug] */
3827c478bd9Sstevel@tonic-gate 	size_t		cache_slabsize;		/* size of a slab */
383b5fca8f8Stomee 	size_t		cache_maxchunks;	/* max buffers per slab */
3847c478bd9Sstevel@tonic-gate 	size_t		cache_bufctl;		/* buf-to-bufctl distance */
3857c478bd9Sstevel@tonic-gate 	size_t		cache_buftag;		/* buf-to-buftag distance */
3867c478bd9Sstevel@tonic-gate 	size_t		cache_verify;		/* bytes to verify */
3877c478bd9Sstevel@tonic-gate 	size_t		cache_contents;		/* bytes of saved content */
3887c478bd9Sstevel@tonic-gate 	size_t		cache_color;		/* next slab color */
3897c478bd9Sstevel@tonic-gate 	size_t		cache_mincolor;		/* maximum slab color */
3907c478bd9Sstevel@tonic-gate 	size_t		cache_maxcolor;		/* maximum slab color */
3917c478bd9Sstevel@tonic-gate 	size_t		cache_hash_shift;	/* get to interesting bits */
3927c478bd9Sstevel@tonic-gate 	size_t		cache_hash_mask;	/* hash table mask */
393b5fca8f8Stomee 	list_t		cache_complete_slabs;	/* completely allocated slabs */
394b5fca8f8Stomee 	size_t		cache_complete_slab_count;
395b5fca8f8Stomee 	avl_tree_t	cache_partial_slabs;	/* partial slab freelist */
396b5fca8f8Stomee 	size_t		cache_partial_binshift;	/* for AVL sort bins */
3977c478bd9Sstevel@tonic-gate 	kmem_cache_t	*cache_bufctl_cache;	/* source of bufctls */
3987c478bd9Sstevel@tonic-gate 	kmem_bufctl_t	**cache_hash_table;	/* hash table base */
399b5fca8f8Stomee 	kmem_defrag_t	*cache_defrag;		/* slab consolidator fields */
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	/*
4027c478bd9Sstevel@tonic-gate 	 * Depot layer
4037c478bd9Sstevel@tonic-gate 	 */
4047c478bd9Sstevel@tonic-gate 	kmutex_t	cache_depot_lock;	/* protects depot */
4057c478bd9Sstevel@tonic-gate 	kmem_magtype_t	*cache_magtype;		/* magazine type */
4067c478bd9Sstevel@tonic-gate 	kmem_maglist_t	cache_full;		/* full magazines */
4077c478bd9Sstevel@tonic-gate 	kmem_maglist_t	cache_empty;		/* empty magazines */
408*3608e2e0SJohn Levon 	kmem_dump_t	cache_dump;		/* used during crash dump */
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	/*
4117c478bd9Sstevel@tonic-gate 	 * Per-CPU layer
4127c478bd9Sstevel@tonic-gate 	 */
4137c478bd9Sstevel@tonic-gate 	kmem_cpu_cache_t cache_cpu[1];		/* max_ncpus actual elements */
4147c478bd9Sstevel@tonic-gate };
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate typedef struct kmem_cpu_log_header {
4177c478bd9Sstevel@tonic-gate 	kmutex_t	clh_lock;
4187c478bd9Sstevel@tonic-gate 	char		*clh_current;
4197c478bd9Sstevel@tonic-gate 	size_t		clh_avail;
4207c478bd9Sstevel@tonic-gate 	int		clh_chunk;
4217c478bd9Sstevel@tonic-gate 	int		clh_hits;
4227c478bd9Sstevel@tonic-gate 	char		clh_pad[64 - sizeof (kmutex_t) - sizeof (char *) -
4237c478bd9Sstevel@tonic-gate 				sizeof (size_t) - 2 * sizeof (int)];
4247c478bd9Sstevel@tonic-gate } kmem_cpu_log_header_t;
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate typedef struct kmem_log_header {
4277c478bd9Sstevel@tonic-gate 	kmutex_t	lh_lock;
4287c478bd9Sstevel@tonic-gate 	char		*lh_base;
4297c478bd9Sstevel@tonic-gate 	int		*lh_free;
4307c478bd9Sstevel@tonic-gate 	size_t		lh_chunksize;
4317c478bd9Sstevel@tonic-gate 	int		lh_nchunks;
4327c478bd9Sstevel@tonic-gate 	int		lh_head;
4337c478bd9Sstevel@tonic-gate 	int		lh_tail;
4347c478bd9Sstevel@tonic-gate 	int		lh_hits;
4357c478bd9Sstevel@tonic-gate 	kmem_cpu_log_header_t lh_cpu[1];	/* ncpus actually allocated */
4367c478bd9Sstevel@tonic-gate } kmem_log_header_t;
4377c478bd9Sstevel@tonic-gate 
438b5fca8f8Stomee /* kmem_move kmm_flags */
439b5fca8f8Stomee #define	KMM_DESPERATE		0x1
440b5fca8f8Stomee #define	KMM_NOTIFY		0x2
441686031edSTom Erickson #define	KMM_DEBUG		0x4
442b5fca8f8Stomee 
443b5fca8f8Stomee typedef struct kmem_move {
444b5fca8f8Stomee 	kmem_slab_t	*kmm_from_slab;
445b5fca8f8Stomee 	void		*kmm_from_buf;
446b5fca8f8Stomee 	void		*kmm_to_buf;
447b5fca8f8Stomee 	avl_node_t	kmm_entry;
448b5fca8f8Stomee 	int		kmm_flags;
449b5fca8f8Stomee } kmem_move_t;
450b5fca8f8Stomee 
451b5fca8f8Stomee /*
452b5fca8f8Stomee  * In order to consolidate partial slabs, it must be possible for the cache to
453b5fca8f8Stomee  * have partial slabs.
454b5fca8f8Stomee  */
455b5fca8f8Stomee #define	KMEM_IS_MOVABLE(cp)						\
456b5fca8f8Stomee 	(((cp)->cache_chunksize * 2) <= (cp)->cache_slabsize)
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
4597c478bd9Sstevel@tonic-gate }
4607c478bd9Sstevel@tonic-gate #endif
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate #endif	/* _SYS_KMEM_IMPL_H */
463