]> git.proxmox.com Git - mirror_spl.git/blame - include/sys/kmem.h
Lower minimum objects/slab threshold
[mirror_spl.git] / include / sys / kmem.h
CommitLineData
716154c5
BB
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>.
715f6251 6 * UCRL-CODE-235197
7 *
716154c5 8 * This file is part of the SPL, Solaris Porting Layer.
3d6af2dd 9 * For details, see <http://zfsonlinux.org/>.
716154c5
BB
10 *
11 * The SPL is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
715f6251 15 *
716154c5 16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
715f6251 17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
716154c5
BB
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23\*****************************************************************************/
715f6251 24
09b414e8 25#ifndef _SPL_KMEM_H
26#define _SPL_KMEM_H
f1ca4da6 27
f1b59d26 28#include <linux/module.h>
f1ca4da6 29#include <linux/slab.h>
79b31f36 30#include <linux/vmalloc.h>
f1ca4da6 31#include <linux/spinlock.h>
d6a26c6a 32#include <linux/rwsem.h>
33#include <linux/hash.h>
ed316348 34#include <linux/rbtree.h>
d6a26c6a 35#include <linux/ctype.h>
d04c8a56 36#include <asm/atomic.h>
57d86234 37#include <sys/types.h>
36b313da 38#include <sys/vmsystm.h>
def465ad 39#include <sys/kstat.h>
a10287e0 40#include <sys/taskq.h>
550f1705 41
f1ca4da6 42/*
43 * Memory allocation interfaces
44 */
cb255ae5
BB
45#define KM_SLEEP GFP_KERNEL /* Can sleep, never fails */
46#define KM_NOSLEEP GFP_ATOMIC /* Can not sleep, may fail */
f90096c9 47#define KM_PUSHPAGE (GFP_NOIO | __GFP_HIGH) /* Use reserved memory */
cb255ae5
BB
48#define KM_NODEBUG __GFP_NOWARN /* Suppress warnings */
49#define KM_FLAGS __GFP_BITS_MASK
50#define KM_VMFLAGS GFP_LEVEL_MASK
f1ca4da6 51
3d061e9d 52/*
53 * Used internally, the kernel does not need to support this flag
54 */
55#ifndef __GFP_ZERO
a0f6da3d 56# define __GFP_ZERO 0x8000
3d061e9d 57#endif
58
eb0f407a
BB
59/*
60 * PF_NOFS is a per-process debug flag which is set in current->flags to
61 * detect when a process is performing an unsafe allocation. All tasks
62 * with PF_NOFS set must strictly use KM_PUSHPAGE for allocations because
63 * if they enter direct reclaim and initiate I/O the may deadlock.
64 *
65 * When debugging is disabled, any incorrect usage will be detected and
66 * a call stack with warning will be printed to the console. The flags
67 * will then be automatically corrected to allow for safe execution. If
68 * debugging is enabled this will be treated as a fatal condition.
69 *
70 * To avoid any risk of conflicting with the existing PF_ flags. The
71 * PF_NOFS bit shadows the rarely used PF_MUTEX_TESTER bit. Only when
72 * CONFIG_RT_MUTEX_TESTER is not set, and we know this bit is unused,
73 * will the PF_NOFS bit be valid. Happily, most existing distributions
74 * ship a kernel with CONFIG_RT_MUTEX_TESTER disabled.
75 */
76#if !defined(CONFIG_RT_MUTEX_TESTER) && defined(PF_MUTEX_TESTER)
77# define PF_NOFS PF_MUTEX_TESTER
78
79static inline void
80sanitize_flags(struct task_struct *p, gfp_t *flags)
81{
82 if (unlikely((p->flags & PF_NOFS) && (*flags & (__GFP_IO|__GFP_FS)))) {
83# ifdef NDEBUG
84 SDEBUG_LIMIT(SD_CONSOLE | SD_WARNING, "Fixing allocation for "
85 "task %s (%d) which used GFP flags 0x%x with PF_NOFS set\n",
86 p->comm, p->pid, flags);
87 spl_debug_dumpstack(p);
88 *flags &= ~(__GFP_IO|__GFP_FS);
89# else
90 PANIC("FATAL allocation for task %s (%d) which used GFP "
91 "flags 0x%x with PF_NOFS set\n", p->comm, p->pid, flags);
92# endif /* NDEBUG */
93 }
94}
95#else
96# define PF_NOFS 0x00000000
97# define sanitize_flags(p, fl) ((void)0)
98#endif /* !defined(CONFIG_RT_MUTEX_TESTER) && defined(PF_MUTEX_TESTER) */
99
c89fdee4
BB
100/*
101 * __GFP_NOFAIL looks like it will be removed from the kernel perhaps as
102 * early as 2.6.32. To avoid this issue when it occurs in upstream kernels
103 * we retry the allocation here as long as it is not __GFP_WAIT (GFP_ATOMIC).
104 * I would prefer the caller handle the failure case cleanly but we are
105 * trying to emulate Solaris and those are not the Solaris semantics.
106 */
107static inline void *
108kmalloc_nofail(size_t size, gfp_t flags)
109{
110 void *ptr;
111
eb0f407a
BB
112 sanitize_flags(current, &flags);
113
c89fdee4
BB
114 do {
115 ptr = kmalloc(size, flags);
116 } while (ptr == NULL && (flags & __GFP_WAIT));
117
118 return ptr;
119}
120
121static inline void *
122kzalloc_nofail(size_t size, gfp_t flags)
123{
124 void *ptr;
125
eb0f407a
BB
126 sanitize_flags(current, &flags);
127
c89fdee4
BB
128 do {
129 ptr = kzalloc(size, flags);
130 } while (ptr == NULL && (flags & __GFP_WAIT));
131
132 return ptr;
133}
134
c89fdee4
BB
135static inline void *
136kmalloc_node_nofail(size_t size, gfp_t flags, int node)
137{
138 void *ptr;
139
eb0f407a
BB
140 sanitize_flags(current, &flags);
141
c89fdee4
BB
142 do {
143 ptr = kmalloc_node(size, flags, node);
144 } while (ptr == NULL && (flags & __GFP_WAIT));
145
146 return ptr;
10129680
BB
147}
148
149static inline void *
150vmalloc_nofail(size_t size, gfp_t flags)
151{
152 void *ptr;
153
eb0f407a
BB
154 sanitize_flags(current, &flags);
155
10129680
BB
156 /*
157 * Retry failed __vmalloc() allocations once every second. The
158 * rational for the delay is that the likely failure modes are:
159 *
160 * 1) The system has completely exhausted memory, in which case
161 * delaying 1 second for the memory reclaim to run is reasonable
162 * to avoid thrashing the system.
163 * 2) The system has memory but has exhausted the small virtual
164 * address space available on 32-bit systems. Retrying the
165 * allocation immediately will only result in spinning on the
166 * virtual address space lock. It is better delay a second and
167 * hope that another process will free some of the address space.
168 * But the bottom line is there is not much we can actually do
169 * since we can never safely return a failure and honor the
170 * Solaris semantics.
171 */
172 while (1) {
173 ptr = __vmalloc(size, flags | __GFP_HIGHMEM, PAGE_KERNEL);
174 if (unlikely((ptr == NULL) && (flags & __GFP_WAIT))) {
175 set_current_state(TASK_INTERRUPTIBLE);
176 schedule_timeout(HZ);
177 } else {
178 break;
179 }
180 }
181
182 return ptr;
183}
184
185static inline void *
186vzalloc_nofail(size_t size, gfp_t flags)
187{
188 void *ptr;
189
190 ptr = vmalloc_nofail(size, flags);
191 if (ptr)
192 memset(ptr, 0, (size));
193
194 return ptr;
195}
c89fdee4 196
f1ca4da6 197#ifdef DEBUG_KMEM
a0f6da3d 198
10129680
BB
199/*
200 * Memory accounting functions to be used only when DEBUG_KMEM is set.
201 */
202# ifdef HAVE_ATOMIC64_T
a0f6da3d 203
d04c8a56
BB
204# define kmem_alloc_used_add(size) atomic64_add(size, &kmem_alloc_used)
205# define kmem_alloc_used_sub(size) atomic64_sub(size, &kmem_alloc_used)
206# define kmem_alloc_used_read() atomic64_read(&kmem_alloc_used)
207# define kmem_alloc_used_set(size) atomic64_set(&kmem_alloc_used, size)
208# define vmem_alloc_used_add(size) atomic64_add(size, &vmem_alloc_used)
209# define vmem_alloc_used_sub(size) atomic64_sub(size, &vmem_alloc_used)
210# define vmem_alloc_used_read() atomic64_read(&vmem_alloc_used)
211# define vmem_alloc_used_set(size) atomic64_set(&vmem_alloc_used, size)
212
10129680 213extern atomic64_t kmem_alloc_used;
d04c8a56 214extern unsigned long long kmem_alloc_max;
10129680 215extern atomic64_t vmem_alloc_used;
d04c8a56
BB
216extern unsigned long long vmem_alloc_max;
217
10129680
BB
218# else /* HAVE_ATOMIC64_T */
219
d04c8a56
BB
220# define kmem_alloc_used_add(size) atomic_add(size, &kmem_alloc_used)
221# define kmem_alloc_used_sub(size) atomic_sub(size, &kmem_alloc_used)
222# define kmem_alloc_used_read() atomic_read(&kmem_alloc_used)
223# define kmem_alloc_used_set(size) atomic_set(&kmem_alloc_used, size)
224# define vmem_alloc_used_add(size) atomic_add(size, &vmem_alloc_used)
225# define vmem_alloc_used_sub(size) atomic_sub(size, &vmem_alloc_used)
226# define vmem_alloc_used_read() atomic_read(&vmem_alloc_used)
227# define vmem_alloc_used_set(size) atomic_set(&vmem_alloc_used, size)
228
10129680
BB
229extern atomic_t kmem_alloc_used;
230extern unsigned long long kmem_alloc_max;
231extern atomic_t vmem_alloc_used;
232extern unsigned long long vmem_alloc_max;
a0f6da3d 233
10129680 234# endif /* HAVE_ATOMIC64_T */
a0f6da3d 235
236# ifdef DEBUG_KMEM_TRACKING
10129680
BB
237/*
238 * DEBUG_KMEM && DEBUG_KMEM_TRACKING
239 *
240 * The maximum level of memory debugging. All memory will be accounted
241 * for and each allocation will be explicitly tracked. Any allocation
242 * which is leaked will be reported on module unload and the exact location
243 * where that memory was allocation will be reported. This level of memory
244 * tracking will have a significant impact on performance and should only
245 * be enabled for debugging. This feature may be enabled by passing
246 * --enable-debug-kmem-tracking to configure.
247 */
248# define kmem_alloc(sz, fl) kmem_alloc_track((sz), (fl), \
249 __FUNCTION__, __LINE__, 0, 0)
250# define kmem_zalloc(sz, fl) kmem_alloc_track((sz), (fl)|__GFP_ZERO,\
251 __FUNCTION__, __LINE__, 0, 0)
252# define kmem_alloc_node(sz, fl, nd) kmem_alloc_track((sz), (fl), \
253 __FUNCTION__, __LINE__, 1, nd)
254# define kmem_free(ptr, sz) kmem_free_track((ptr), (sz))
255
256# define vmem_alloc(sz, fl) vmem_alloc_track((sz), (fl), \
257 __FUNCTION__, __LINE__)
258# define vmem_zalloc(sz, fl) vmem_alloc_track((sz), (fl)|__GFP_ZERO,\
259 __FUNCTION__, __LINE__)
260# define vmem_free(ptr, sz) vmem_free_track((ptr), (sz))
261
262extern void *kmem_alloc_track(size_t, int, const char *, int, int, int);
973e8269 263extern void kmem_free_track(const void *, size_t);
10129680 264extern void *vmem_alloc_track(size_t, int, const char *, int);
973e8269 265extern void vmem_free_track(const void *, size_t);
a0f6da3d 266
267# else /* DEBUG_KMEM_TRACKING */
10129680
BB
268/*
269 * DEBUG_KMEM && !DEBUG_KMEM_TRACKING
270 *
271 * The default build will set DEBUG_KEM. This provides basic memory
272 * accounting with little to no impact on performance. When the module
273 * is unloaded in any memory was leaked the total number of leaked bytes
274 * will be reported on the console. To disable this basic accounting
275 * pass the --disable-debug-kmem option to configure.
276 */
277# define kmem_alloc(sz, fl) kmem_alloc_debug((sz), (fl), \
278 __FUNCTION__, __LINE__, 0, 0)
279# define kmem_zalloc(sz, fl) kmem_alloc_debug((sz), (fl)|__GFP_ZERO,\
280 __FUNCTION__, __LINE__, 0, 0)
281# define kmem_alloc_node(sz, fl, nd) kmem_alloc_debug((sz), (fl), \
282 __FUNCTION__, __LINE__, 1, nd)
283# define kmem_free(ptr, sz) kmem_free_debug((ptr), (sz))
284
285# define vmem_alloc(sz, fl) vmem_alloc_debug((sz), (fl), \
286 __FUNCTION__, __LINE__)
287# define vmem_zalloc(sz, fl) vmem_alloc_debug((sz), (fl)|__GFP_ZERO,\
288 __FUNCTION__, __LINE__)
289# define vmem_free(ptr, sz) vmem_free_debug((ptr), (sz))
290
291extern void *kmem_alloc_debug(size_t, int, const char *, int, int, int);
973e8269 292extern void kmem_free_debug(const void *, size_t);
10129680 293extern void *vmem_alloc_debug(size_t, int, const char *, int);
973e8269 294extern void vmem_free_debug(const void *, size_t);
a0f6da3d 295
296# endif /* DEBUG_KMEM_TRACKING */
c6dc93d6 297#else /* DEBUG_KMEM */
10129680
BB
298/*
299 * !DEBUG_KMEM && !DEBUG_KMEM_TRACKING
300 *
301 * All debugging is disabled. There will be no overhead even for
302 * minimal memory accounting. To enable basic accounting pass the
303 * --enable-debug-kmem option to configure.
304 */
305# define kmem_alloc(sz, fl) kmalloc_nofail((sz), (fl))
306# define kmem_zalloc(sz, fl) kzalloc_nofail((sz), (fl))
307# define kmem_alloc_node(sz, fl, nd) kmalloc_node_nofail((sz), (fl), (nd))
308# define kmem_free(ptr, sz) ((void)(sz), kfree(ptr))
f1ca4da6 309
10129680
BB
310# define vmem_alloc(sz, fl) vmalloc_nofail((sz), (fl))
311# define vmem_zalloc(sz, fl) vzalloc_nofail((sz), (fl))
312# define vmem_free(ptr, sz) ((void)(sz), vfree(ptr))
79b31f36 313
f1ca4da6 314#endif /* DEBUG_KMEM */
315
10129680
BB
316extern int kmem_debugging(void);
317extern char *kmem_vasprintf(const char *fmt, va_list ap);
318extern char *kmem_asprintf(const char *fmt, ...);
319extern char *strdup(const char *str);
320extern void strfree(char *str);
321
322
f1ca4da6 323/*
10129680
BB
324 * Slab allocation interfaces. The SPL slab differs from the standard
325 * Linux SLAB or SLUB primarily in that each cache may be backed by slabs
326 * allocated from the physical or virtal memory address space. The virtual
327 * slabs allow for good behavior when allocation large objects of identical
328 * size. This slab implementation also supports both constructors and
329 * destructions which the Linux slab does not.
f1ca4da6 330 */
ea3e6ca9
BB
331enum {
332 KMC_BIT_NOTOUCH = 0, /* Don't update ages */
333 KMC_BIT_NODEBUG = 1, /* Default behavior */
334 KMC_BIT_NOMAGAZINE = 2, /* XXX: Unsupported */
335 KMC_BIT_NOHASH = 3, /* XXX: Unsupported */
336 KMC_BIT_QCACHE = 4, /* XXX: Unsupported */
337 KMC_BIT_KMEM = 5, /* Use kmem cache */
338 KMC_BIT_VMEM = 6, /* Use vmem cache */
a073aeb0
BB
339 KMC_BIT_SLAB = 7, /* Use Linux slab cache */
340 KMC_BIT_OFFSLAB = 8, /* Objects not on slab */
341 KMC_BIT_NOEMERGENCY = 9, /* Disable emergency objects */
165f13c3 342 KMC_BIT_DEADLOCKED = 14, /* Deadlock detected */
e2dcc6e2 343 KMC_BIT_GROWING = 15, /* Growing in progress */
ea3e6ca9
BB
344 KMC_BIT_REAPING = 16, /* Reaping in progress */
345 KMC_BIT_DESTROY = 17, /* Destroy in progress */
3336e29c
BB
346 KMC_BIT_TOTAL = 18, /* Proc handler helper bit */
347 KMC_BIT_ALLOC = 19, /* Proc handler helper bit */
348 KMC_BIT_MAX = 20, /* Proc handler helper bit */
ea3e6ca9
BB
349};
350
2b354302
BB
351/* kmem move callback return values */
352typedef enum kmem_cbrc {
353 KMEM_CBRC_YES = 0, /* Object moved */
354 KMEM_CBRC_NO = 1, /* Object not moved */
355 KMEM_CBRC_LATER = 2, /* Object not moved, try again later */
356 KMEM_CBRC_DONT_NEED = 3, /* Neither object is needed */
357 KMEM_CBRC_DONT_KNOW = 4, /* Object unknown */
358} kmem_cbrc_t;
359
ea3e6ca9
BB
360#define KMC_NOTOUCH (1 << KMC_BIT_NOTOUCH)
361#define KMC_NODEBUG (1 << KMC_BIT_NODEBUG)
362#define KMC_NOMAGAZINE (1 << KMC_BIT_NOMAGAZINE)
363#define KMC_NOHASH (1 << KMC_BIT_NOHASH)
364#define KMC_QCACHE (1 << KMC_BIT_QCACHE)
365#define KMC_KMEM (1 << KMC_BIT_KMEM)
366#define KMC_VMEM (1 << KMC_BIT_VMEM)
a073aeb0 367#define KMC_SLAB (1 << KMC_BIT_SLAB)
ea3e6ca9 368#define KMC_OFFSLAB (1 << KMC_BIT_OFFSLAB)
cb5c2ace 369#define KMC_NOEMERGENCY (1 << KMC_BIT_NOEMERGENCY)
165f13c3 370#define KMC_DEADLOCKED (1 << KMC_BIT_DEADLOCKED)
e2dcc6e2 371#define KMC_GROWING (1 << KMC_BIT_GROWING)
ea3e6ca9
BB
372#define KMC_REAPING (1 << KMC_BIT_REAPING)
373#define KMC_DESTROY (1 << KMC_BIT_DESTROY)
3336e29c
BB
374#define KMC_TOTAL (1 << KMC_BIT_TOTAL)
375#define KMC_ALLOC (1 << KMC_BIT_ALLOC)
376#define KMC_MAX (1 << KMC_BIT_MAX)
ea3e6ca9 377
0936c344
BB
378#define KMC_REAP_CHUNK INT_MAX
379#define KMC_DEFAULT_SEEKS 1
f1ca4da6 380
0936c344
BB
381#define KMC_EXPIRE_AGE 0x1 /* Due to age */
382#define KMC_EXPIRE_MEM 0x2 /* Due to low memory */
383
376dc35e
BB
384#define KMC_RECLAIM_ONCE 0x1 /* Force a single shrinker pass */
385
0936c344 386extern unsigned int spl_kmem_cache_expire;
ff449ac4 387extern struct list_head spl_kmem_cache_list;
388extern struct rw_semaphore spl_kmem_cache_sem;
2fb9b26a 389
4afaaefa 390#define SKM_MAGIC 0x2e2e2e2e
2fb9b26a 391#define SKO_MAGIC 0x20202020
392#define SKS_MAGIC 0x22222222
393#define SKC_MAGIC 0x2c2c2c2c
394
37db7d8c
BB
395#define SPL_KMEM_CACHE_DELAY 15 /* Minimum slab release age */
396#define SPL_KMEM_CACHE_REAP 0 /* Default reap everything */
83150861 397#define SPL_KMEM_CACHE_OBJ_PER_SLAB 16 /* Target objects per slab */
917fef27 398#define SPL_KMEM_CACHE_OBJ_PER_SLAB_MIN 1 /* Minimum objects per slab */
ea3e6ca9 399#define SPL_KMEM_CACHE_ALIGN 8 /* Default object alignment */
2fb9b26a 400
2b354302
BB
401#define POINTER_IS_VALID(p) 0 /* Unimplemented */
402#define POINTER_INVALIDATE(pp) /* Unimplemented */
403
2fb9b26a 404typedef int (*spl_kmem_ctor_t)(void *, void *, int);
405typedef void (*spl_kmem_dtor_t)(void *, void *);
406typedef void (*spl_kmem_reclaim_t)(void *);
407
4afaaefa 408typedef struct spl_kmem_magazine {
9b1b8e4c 409 uint32_t skm_magic; /* Sanity magic */
4afaaefa 410 uint32_t skm_avail; /* Available objects */
411 uint32_t skm_size; /* Magazine size */
412 uint32_t skm_refill; /* Batch refill size */
9b1b8e4c 413 struct spl_kmem_cache *skm_cache; /* Owned by cache */
4afaaefa 414 unsigned long skm_age; /* Last cache access */
08850edd 415 unsigned int skm_cpu; /* Owned by cpu */
4afaaefa 416 void *skm_objs[0]; /* Object pointers */
417} spl_kmem_magazine_t;
418
2fb9b26a 419typedef struct spl_kmem_obj {
420 uint32_t sko_magic; /* Sanity magic */
2fb9b26a 421 void *sko_addr; /* Buffer address */
422 struct spl_kmem_slab *sko_slab; /* Owned by slab */
423 struct list_head sko_list; /* Free object list linkage */
2fb9b26a 424} spl_kmem_obj_t;
425
426typedef struct spl_kmem_slab {
427 uint32_t sks_magic; /* Sanity magic */
428 uint32_t sks_objs; /* Objects per slab */
429 struct spl_kmem_cache *sks_cache; /* Owned by cache */
430 struct list_head sks_list; /* Slab list linkage */
431 struct list_head sks_free_list; /* Free object list */
432 unsigned long sks_age; /* Last modify jiffie */
4afaaefa 433 uint32_t sks_ref; /* Ref count used objects */
2fb9b26a 434} spl_kmem_slab_t;
435
e2dcc6e2
BB
436typedef struct spl_kmem_alloc {
437 struct spl_kmem_cache *ska_cache; /* Owned by cache */
438 int ska_flags; /* Allocation flags */
33e94ef1 439 taskq_ent_t ska_tqe; /* Task queue entry */
e2dcc6e2
BB
440} spl_kmem_alloc_t;
441
442typedef struct spl_kmem_emergency {
ed316348 443 struct rb_node ske_node; /* Emergency tree linkage */
e2dcc6e2 444 void *ske_obj; /* Buffer address */
e2dcc6e2
BB
445} spl_kmem_emergency_t;
446
2fb9b26a 447typedef struct spl_kmem_cache {
ea3e6ca9
BB
448 uint32_t skc_magic; /* Sanity magic */
449 uint32_t skc_name_size; /* Name length */
450 char *skc_name; /* Name string */
4afaaefa 451 spl_kmem_magazine_t *skc_mag[NR_CPUS]; /* Per-CPU warm cache */
452 uint32_t skc_mag_size; /* Magazine size */
453 uint32_t skc_mag_refill; /* Magazine refill count */
ea3e6ca9
BB
454 spl_kmem_ctor_t skc_ctor; /* Constructor */
455 spl_kmem_dtor_t skc_dtor; /* Destructor */
456 spl_kmem_reclaim_t skc_reclaim; /* Reclaimator */
457 void *skc_private; /* Private data */
458 void *skc_vmp; /* Unused */
a073aeb0 459 struct kmem_cache *skc_linux_cache; /* Linux slab cache if used */
31a033ec 460 unsigned long skc_flags; /* Flags */
2fb9b26a 461 uint32_t skc_obj_size; /* Object size */
48e0606a 462 uint32_t skc_obj_align; /* Object alignment */
a1502d76 463 uint32_t skc_slab_objs; /* Objects per slab */
ea3e6ca9
BB
464 uint32_t skc_slab_size; /* Slab size */
465 uint32_t skc_delay; /* Slab reclaim interval */
37db7d8c 466 uint32_t skc_reap; /* Slab reclaim count */
ea3e6ca9 467 atomic_t skc_ref; /* Ref count callers */
a10287e0 468 taskqid_t skc_taskqid; /* Slab reclaim task */
ea3e6ca9 469 struct list_head skc_list; /* List of caches linkage */
2fb9b26a 470 struct list_head skc_complete_list;/* Completely alloc'ed */
471 struct list_head skc_partial_list; /* Partially alloc'ed */
ed316348 472 struct rb_root skc_emergency_tree; /* Min sized objects */
d46630e0 473 spinlock_t skc_lock; /* Cache lock */
e2dcc6e2 474 wait_queue_head_t skc_waitq; /* Allocation waiters */
2fb9b26a 475 uint64_t skc_slab_fail; /* Slab alloc failures */
476 uint64_t skc_slab_create;/* Slab creates */
477 uint64_t skc_slab_destroy;/* Slab destroys */
d46630e0 478 uint64_t skc_slab_total; /* Slab total current */
ea3e6ca9 479 uint64_t skc_slab_alloc; /* Slab alloc current */
d46630e0 480 uint64_t skc_slab_max; /* Slab max historic */
481 uint64_t skc_obj_total; /* Obj total current */
482 uint64_t skc_obj_alloc; /* Obj alloc current */
483 uint64_t skc_obj_max; /* Obj max historic */
165f13c3 484 uint64_t skc_obj_deadlock; /* Obj emergency deadlocks */
e2dcc6e2
BB
485 uint64_t skc_obj_emergency; /* Obj emergency current */
486 uint64_t skc_obj_emergency_max; /* Obj emergency max */
2fb9b26a 487} spl_kmem_cache_t;
7afde631 488#define kmem_cache_t spl_kmem_cache_t
2fb9b26a 489
2b354302
BB
490extern spl_kmem_cache_t *spl_kmem_cache_create(char *name, size_t size,
491 size_t align, spl_kmem_ctor_t ctor, spl_kmem_dtor_t dtor,
492 spl_kmem_reclaim_t reclaim, void *priv, void *vmp, int flags);
6576a1a7 493extern void spl_kmem_cache_set_move(spl_kmem_cache_t *,
2b354302 494 kmem_cbrc_t (*)(void *, void *, size_t, void *));
2fb9b26a 495extern void spl_kmem_cache_destroy(spl_kmem_cache_t *skc);
496extern void *spl_kmem_cache_alloc(spl_kmem_cache_t *skc, int flags);
497extern void spl_kmem_cache_free(spl_kmem_cache_t *skc, void *obj);
cef7605c 498extern void spl_kmem_cache_reap_now(spl_kmem_cache_t *skc, int count);
2fb9b26a 499extern void spl_kmem_reap(void);
f1ca4da6 500
2fb9b26a 501int spl_kmem_init(void);
502void spl_kmem_fini(void);
5d86345d 503
f1ca4da6 504#define kmem_cache_create(name,size,align,ctor,dtor,rclm,priv,vmp,flags) \
2fb9b26a 505 spl_kmem_cache_create(name,size,align,ctor,dtor,rclm,priv,vmp,flags)
2b354302 506#define kmem_cache_set_move(skc, move) spl_kmem_cache_set_move(skc, move)
2fb9b26a 507#define kmem_cache_destroy(skc) spl_kmem_cache_destroy(skc)
508#define kmem_cache_alloc(skc, flags) spl_kmem_cache_alloc(skc, flags)
509#define kmem_cache_free(skc, obj) spl_kmem_cache_free(skc, obj)
cef7605c
PS
510#define kmem_cache_reap_now(skc) \
511 spl_kmem_cache_reap_now(skc, skc->skc_reap)
2fb9b26a 512#define kmem_reap() spl_kmem_reap()
a1502d76 513#define kmem_virt(ptr) (((ptr) >= (void *)VMALLOC_START) && \
514 ((ptr) < (void *)VMALLOC_END))
f1ca4da6 515
a073aeb0
BB
516/*
517 * Allow custom slab allocation flags to be set for KMC_SLAB based caches.
518 * One use for this function is to ensure the __GFP_COMP flag is part of
519 * the default allocation mask which ensures higher order allocations are
520 * properly refcounted. This flag was added to the default ->allocflags
521 * as of Linux 3.11.
522 */
523static inline void
524kmem_cache_set_allocflags(spl_kmem_cache_t *skc, gfp_t flags)
525{
526 if (skc->skc_linux_cache == NULL)
527 return;
528
529#if defined(HAVE_KMEM_CACHE_ALLOCFLAGS)
530 skc->skc_linux_cache->allocflags |= flags;
531#elif defined(HAVE_KMEM_CACHE_GFPFLAGS)
532 skc->skc_linux_cache->gfpflags |= flags;
533#endif
534}
535
09b414e8 536#endif /* _SPL_KMEM_H */