xref: /freebsd/sys/contrib/openzfs/include/os/linux/spl/sys/kmem_cache.h (revision 7a7741af18d6c8a804cc643cb7ecda9d730c6aa6)
1 /*
2  *  Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3  *  Copyright (C) 2007 The Regents of the University of California.
4  *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5  *  Written by Brian Behlendorf <behlendorf1@llnl.gov>.
6  *  UCRL-CODE-235197
7  *
8  *  This file is part of the SPL, Solaris Porting Layer.
9  *
10  *  The SPL is free software; you can redistribute it and/or modify it
11  *  under the terms of the GNU General Public License as published by the
12  *  Free Software Foundation; either version 2 of the License, or (at your
13  *  option) any later version.
14  *
15  *  The SPL is distributed in the hope that it will be useful, but WITHOUT
16  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18  *  for more details.
19  *
20  *  You should have received a copy of the GNU General Public License along
21  *  with the SPL.  If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #ifndef _SPL_KMEM_CACHE_H
25 #define	_SPL_KMEM_CACHE_H
26 
27 #include <sys/taskq.h>
28 
29 /*
30  * Slab allocation interfaces.  The SPL slab differs from the standard
31  * Linux SLAB or SLUB primarily in that each cache may be backed by slabs
32  * allocated from the physical or virtual memory address space.  The virtual
33  * slabs allow for good behavior when allocation large objects of identical
34  * size.  This slab implementation also supports both constructors and
35  * destructors which the Linux slab does not.
36  */
37 typedef enum kmc_bit {
38 	KMC_BIT_NODEBUG		= 1,	/* Default behavior */
39 	KMC_BIT_KVMEM		= 7,	/* Use kvmalloc linux allocator  */
40 	KMC_BIT_SLAB		= 8,	/* Use Linux slab cache */
41 	KMC_BIT_DEADLOCKED	= 14,	/* Deadlock detected */
42 	KMC_BIT_GROWING		= 15,	/* Growing in progress */
43 	KMC_BIT_REAPING		= 16,	/* Reaping in progress */
44 	KMC_BIT_DESTROY		= 17,	/* Destroy in progress */
45 	KMC_BIT_TOTAL		= 18,	/* Proc handler helper bit */
46 	KMC_BIT_ALLOC		= 19,	/* Proc handler helper bit */
47 	KMC_BIT_MAX		= 20,	/* Proc handler helper bit */
48 	KMC_BIT_RECLAIMABLE	= 21,	/* Can be freed by shrinker */
49 } kmc_bit_t;
50 
51 /* kmem move callback return values */
52 typedef enum kmem_cbrc {
53 	KMEM_CBRC_YES		= 0,	/* Object moved */
54 	KMEM_CBRC_NO		= 1,	/* Object not moved */
55 	KMEM_CBRC_LATER		= 2,	/* Object not moved, try again later */
56 	KMEM_CBRC_DONT_NEED	= 3,	/* Neither object is needed */
57 	KMEM_CBRC_DONT_KNOW	= 4,	/* Object unknown */
58 } kmem_cbrc_t;
59 
60 #define	KMC_NODEBUG		(1 << KMC_BIT_NODEBUG)
61 #define	KMC_KVMEM		(1 << KMC_BIT_KVMEM)
62 #define	KMC_SLAB		(1 << KMC_BIT_SLAB)
63 #define	KMC_DEADLOCKED		(1 << KMC_BIT_DEADLOCKED)
64 #define	KMC_GROWING		(1 << KMC_BIT_GROWING)
65 #define	KMC_REAPING		(1 << KMC_BIT_REAPING)
66 #define	KMC_DESTROY		(1 << KMC_BIT_DESTROY)
67 #define	KMC_TOTAL		(1 << KMC_BIT_TOTAL)
68 #define	KMC_ALLOC		(1 << KMC_BIT_ALLOC)
69 #define	KMC_MAX			(1 << KMC_BIT_MAX)
70 #define	KMC_RECLAIMABLE		(1 << KMC_BIT_RECLAIMABLE)
71 
72 extern struct list_head spl_kmem_cache_list;
73 extern struct rw_semaphore spl_kmem_cache_sem;
74 
75 #define	SKM_MAGIC			0x2e2e2e2e
76 #define	SKO_MAGIC			0x20202020
77 #define	SKS_MAGIC			0x22222222
78 #define	SKC_MAGIC			0x2c2c2c2c
79 
80 #define	SPL_KMEM_CACHE_OBJ_PER_SLAB	8	/* Target objects per slab */
81 #define	SPL_KMEM_CACHE_ALIGN		8	/* Default object alignment */
82 #ifdef _LP64
83 #define	SPL_KMEM_CACHE_MAX_SIZE		32	/* Max slab size in MB */
84 #else
85 #define	SPL_KMEM_CACHE_MAX_SIZE		4	/* Max slab size in MB */
86 #endif
87 
88 #define	SPL_MAX_ORDER			(MAX_ORDER - 3)
89 #define	SPL_MAX_ORDER_NR_PAGES		(1 << (SPL_MAX_ORDER - 1))
90 
91 #ifdef CONFIG_SLUB
92 #define	SPL_MAX_KMEM_CACHE_ORDER	PAGE_ALLOC_COSTLY_ORDER
93 #define	SPL_MAX_KMEM_ORDER_NR_PAGES	(1 << (SPL_MAX_KMEM_CACHE_ORDER - 1))
94 #else
95 #define	SPL_MAX_KMEM_ORDER_NR_PAGES	(KMALLOC_MAX_SIZE >> PAGE_SHIFT)
96 #endif
97 
98 typedef int (*spl_kmem_ctor_t)(void *, void *, int);
99 typedef void (*spl_kmem_dtor_t)(void *, void *);
100 
101 typedef struct spl_kmem_magazine {
102 	uint32_t		skm_magic;	/* Sanity magic */
103 	uint32_t		skm_avail;	/* Available objects */
104 	uint32_t		skm_size;	/* Magazine size */
105 	uint32_t		skm_refill;	/* Batch refill size */
106 	struct spl_kmem_cache	*skm_cache;	/* Owned by cache */
107 	unsigned int		skm_cpu;	/* Owned by cpu */
108 	void			*skm_objs[];	/* Object pointers */
109 } spl_kmem_magazine_t;
110 
111 typedef struct spl_kmem_obj {
112 	uint32_t		sko_magic;	/* Sanity magic */
113 	void			*sko_addr;	/* Buffer address */
114 	struct spl_kmem_slab	*sko_slab;	/* Owned by slab */
115 	struct list_head	sko_list;	/* Free object list linkage */
116 } spl_kmem_obj_t;
117 
118 typedef struct spl_kmem_slab {
119 	uint32_t		sks_magic;	/* Sanity magic */
120 	uint32_t		sks_objs;	/* Objects per slab */
121 	struct spl_kmem_cache	*sks_cache;	/* Owned by cache */
122 	struct list_head	sks_list;	/* Slab list linkage */
123 	struct list_head	sks_free_list;	/* Free object list */
124 	unsigned long		sks_age;	/* Last modify jiffie */
125 	uint32_t		sks_ref;	/* Ref count used objects */
126 } spl_kmem_slab_t;
127 
128 typedef struct spl_kmem_alloc {
129 	struct spl_kmem_cache	*ska_cache;	/* Owned by cache */
130 	int			ska_flags;	/* Allocation flags */
131 	taskq_ent_t		ska_tqe;	/* Task queue entry */
132 } spl_kmem_alloc_t;
133 
134 typedef struct spl_kmem_emergency {
135 	struct rb_node		ske_node;	/* Emergency tree linkage */
136 	unsigned long		ske_obj;	/* Buffer address */
137 } spl_kmem_emergency_t;
138 
139 typedef struct spl_kmem_cache {
140 	uint32_t		skc_magic;	/* Sanity magic */
141 	uint32_t		skc_name_size;	/* Name length */
142 	char			*skc_name;	/* Name string */
143 	spl_kmem_magazine_t	**skc_mag;	/* Per-CPU warm cache */
144 	uint32_t		skc_mag_size;	/* Magazine size */
145 	uint32_t		skc_mag_refill;	/* Magazine refill count */
146 	spl_kmem_ctor_t		skc_ctor;	/* Constructor */
147 	spl_kmem_dtor_t		skc_dtor;	/* Destructor */
148 	void			*skc_private;	/* Private data */
149 	void			*skc_vmp;	/* Unused */
150 	struct kmem_cache	*skc_linux_cache; /* Linux slab cache if used */
151 	unsigned long		skc_flags;	/* Flags */
152 	uint32_t		skc_obj_size;	/* Object size */
153 	uint32_t		skc_obj_align;	/* Object alignment */
154 	uint32_t		skc_slab_objs;	/* Objects per slab */
155 	uint32_t		skc_slab_size;	/* Slab size */
156 	atomic_t		skc_ref;	/* Ref count callers */
157 	taskqid_t		skc_taskqid;	/* Slab reclaim task */
158 	struct list_head	skc_list;	/* List of caches linkage */
159 	struct list_head	skc_complete_list; /* Completely alloc'ed */
160 	struct list_head	skc_partial_list;  /* Partially alloc'ed */
161 	struct rb_root		skc_emergency_tree; /* Min sized objects */
162 	spinlock_t		skc_lock;	/* Cache lock */
163 	wait_queue_head_t	skc_waitq;	/* Allocation waiters */
164 	uint64_t		skc_slab_fail;	/* Slab alloc failures */
165 	uint64_t		skc_slab_create;  /* Slab creates */
166 	uint64_t		skc_slab_destroy; /* Slab destroys */
167 	uint64_t		skc_slab_total;	/* Slab total current */
168 	uint64_t		skc_slab_alloc;	/* Slab alloc current */
169 	uint64_t		skc_slab_max;	/* Slab max historic  */
170 	uint64_t		skc_obj_total;	/* Obj total current */
171 	uint64_t		skc_obj_alloc;	/* Obj alloc current */
172 	struct percpu_counter	skc_linux_alloc;   /* Linux-backed Obj alloc  */
173 	uint64_t		skc_obj_max;	/* Obj max historic */
174 	uint64_t		skc_obj_deadlock;  /* Obj emergency deadlocks */
175 	uint64_t		skc_obj_emergency; /* Obj emergency current */
176 	uint64_t		skc_obj_emergency_max; /* Obj emergency max */
177 } spl_kmem_cache_t;
178 #define	kmem_cache_t		spl_kmem_cache_t
179 
180 extern spl_kmem_cache_t *spl_kmem_cache_create(const char *name, size_t size,
181     size_t align, spl_kmem_ctor_t ctor, spl_kmem_dtor_t dtor,
182     void *reclaim, void *priv, void *vmp, int flags);
183 extern void spl_kmem_cache_set_move(spl_kmem_cache_t *,
184     kmem_cbrc_t (*)(void *, void *, size_t, void *));
185 extern void spl_kmem_cache_destroy(spl_kmem_cache_t *skc);
186 extern void *spl_kmem_cache_alloc(spl_kmem_cache_t *skc, int flags);
187 extern void spl_kmem_cache_free(spl_kmem_cache_t *skc, void *obj);
188 extern void spl_kmem_cache_set_allocflags(spl_kmem_cache_t *skc, gfp_t flags);
189 extern void spl_kmem_cache_reap_now(spl_kmem_cache_t *skc);
190 extern void spl_kmem_reap(void);
191 extern uint64_t spl_kmem_cache_inuse(kmem_cache_t *cache);
192 extern uint64_t spl_kmem_cache_entry_size(kmem_cache_t *cache);
193 
194 #ifndef	SPL_KMEM_CACHE_IMPLEMENTING
195 /*
196  * Macros for the kmem_cache_* API expected by ZFS and SPL clients. We don't
197  * define them inside spl-kmem-cache.c, as that uses the kernel's incompatible
198  * kmem_cache_* facilities to implement ours.
199  */
200 
201 /* Avoid conflicts with kernel names that might be implemented as macros. */
202 #undef	kmem_cache_alloc
203 #undef	kmem_cache_create
204 
205 #define	kmem_cache_create(name, size, align, ctor, dtor, rclm, priv, vmp, fl) \
206     spl_kmem_cache_create(name, size, align, ctor, dtor, rclm, priv, vmp, fl)
207 #define	kmem_cache_set_move(skc, move)	spl_kmem_cache_set_move(skc, move)
208 #define	kmem_cache_destroy(skc)		spl_kmem_cache_destroy(skc)
209 #define	kmem_cache_alloc(skc, flags)	spl_kmem_cache_alloc(skc, flags)
210 #define	kmem_cache_free(skc, obj)	spl_kmem_cache_free(skc, obj)
211 #define	kmem_cache_reap_now(skc)	spl_kmem_cache_reap_now(skc)
212 #define	kmem_reap()			spl_kmem_reap()
213 #endif
214 
215 /*
216  * The following functions are only available for internal use.
217  */
218 extern int spl_kmem_cache_init(void);
219 extern void spl_kmem_cache_fini(void);
220 
221 #endif	/* _SPL_KMEM_CACHE_H */
222