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>.
8 * This file is part of the SPL, Solaris Porting Layer.
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.
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
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/>.
24 #ifndef _SPL_KMEM_CACHE_H
25 #define _SPL_KMEM_CACHE_H
27 #include <sys/taskq.h>
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.
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 */
50 /* kmem move callback return values */
51 typedef enum kmem_cbrc
{
52 KMEM_CBRC_YES
= 0, /* Object moved */
53 KMEM_CBRC_NO
= 1, /* Object not moved */
54 KMEM_CBRC_LATER
= 2, /* Object not moved, try again later */
55 KMEM_CBRC_DONT_NEED
= 3, /* Neither object is needed */
56 KMEM_CBRC_DONT_KNOW
= 4, /* Object unknown */
59 #define KMC_NODEBUG (1 << KMC_BIT_NODEBUG)
60 #define KMC_KVMEM (1 << KMC_BIT_KVMEM)
61 #define KMC_SLAB (1 << KMC_BIT_SLAB)
62 #define KMC_DEADLOCKED (1 << KMC_BIT_DEADLOCKED)
63 #define KMC_GROWING (1 << KMC_BIT_GROWING)
64 #define KMC_REAPING (1 << KMC_BIT_REAPING)
65 #define KMC_DESTROY (1 << KMC_BIT_DESTROY)
66 #define KMC_TOTAL (1 << KMC_BIT_TOTAL)
67 #define KMC_ALLOC (1 << KMC_BIT_ALLOC)
68 #define KMC_MAX (1 << KMC_BIT_MAX)
70 #define KMC_REAP_CHUNK INT_MAX
71 #define KMC_DEFAULT_SEEKS 1
73 #define KMC_RECLAIM_ONCE 0x1 /* Force a single shrinker pass */
75 extern struct list_head spl_kmem_cache_list
;
76 extern struct rw_semaphore spl_kmem_cache_sem
;
78 #define SKM_MAGIC 0x2e2e2e2e
79 #define SKO_MAGIC 0x20202020
80 #define SKS_MAGIC 0x22222222
81 #define SKC_MAGIC 0x2c2c2c2c
83 #define SPL_KMEM_CACHE_OBJ_PER_SLAB 8 /* Target objects per slab */
84 #define SPL_KMEM_CACHE_ALIGN 8 /* Default object alignment */
86 #define SPL_KMEM_CACHE_MAX_SIZE 32 /* Max slab size in MB */
88 #define SPL_KMEM_CACHE_MAX_SIZE 4 /* Max slab size in MB */
91 #define SPL_MAX_ORDER (MAX_ORDER - 3)
92 #define SPL_MAX_ORDER_NR_PAGES (1 << (SPL_MAX_ORDER - 1))
95 #define SPL_MAX_KMEM_CACHE_ORDER PAGE_ALLOC_COSTLY_ORDER
96 #define SPL_MAX_KMEM_ORDER_NR_PAGES (1 << (SPL_MAX_KMEM_CACHE_ORDER - 1))
98 #define SPL_MAX_KMEM_ORDER_NR_PAGES (KMALLOC_MAX_SIZE >> PAGE_SHIFT)
101 #define POINTER_IS_VALID(p) 0 /* Unimplemented */
102 #define POINTER_INVALIDATE(pp) /* Unimplemented */
104 typedef int (*spl_kmem_ctor_t
)(void *, void *, int);
105 typedef void (*spl_kmem_dtor_t
)(void *, void *);
107 typedef struct spl_kmem_magazine
{
108 uint32_t skm_magic
; /* Sanity magic */
109 uint32_t skm_avail
; /* Available objects */
110 uint32_t skm_size
; /* Magazine size */
111 uint32_t skm_refill
; /* Batch refill size */
112 struct spl_kmem_cache
*skm_cache
; /* Owned by cache */
113 unsigned int skm_cpu
; /* Owned by cpu */
114 void *skm_objs
[0]; /* Object pointers */
115 } spl_kmem_magazine_t
;
117 typedef struct spl_kmem_obj
{
118 uint32_t sko_magic
; /* Sanity magic */
119 void *sko_addr
; /* Buffer address */
120 struct spl_kmem_slab
*sko_slab
; /* Owned by slab */
121 struct list_head sko_list
; /* Free object list linkage */
124 typedef struct spl_kmem_slab
{
125 uint32_t sks_magic
; /* Sanity magic */
126 uint32_t sks_objs
; /* Objects per slab */
127 struct spl_kmem_cache
*sks_cache
; /* Owned by cache */
128 struct list_head sks_list
; /* Slab list linkage */
129 struct list_head sks_free_list
; /* Free object list */
130 unsigned long sks_age
; /* Last modify jiffie */
131 uint32_t sks_ref
; /* Ref count used objects */
134 typedef struct spl_kmem_alloc
{
135 struct spl_kmem_cache
*ska_cache
; /* Owned by cache */
136 int ska_flags
; /* Allocation flags */
137 taskq_ent_t ska_tqe
; /* Task queue entry */
140 typedef struct spl_kmem_emergency
{
141 struct rb_node ske_node
; /* Emergency tree linkage */
142 unsigned long ske_obj
; /* Buffer address */
143 } spl_kmem_emergency_t
;
145 typedef struct spl_kmem_cache
{
146 uint32_t skc_magic
; /* Sanity magic */
147 uint32_t skc_name_size
; /* Name length */
148 char *skc_name
; /* Name string */
149 spl_kmem_magazine_t
**skc_mag
; /* Per-CPU warm cache */
150 uint32_t skc_mag_size
; /* Magazine size */
151 uint32_t skc_mag_refill
; /* Magazine refill count */
152 spl_kmem_ctor_t skc_ctor
; /* Constructor */
153 spl_kmem_dtor_t skc_dtor
; /* Destructor */
154 void *skc_private
; /* Private data */
155 void *skc_vmp
; /* Unused */
156 struct kmem_cache
*skc_linux_cache
; /* Linux slab cache if used */
157 unsigned long skc_flags
; /* Flags */
158 uint32_t skc_obj_size
; /* Object size */
159 uint32_t skc_obj_align
; /* Object alignment */
160 uint32_t skc_slab_objs
; /* Objects per slab */
161 uint32_t skc_slab_size
; /* Slab size */
162 atomic_t skc_ref
; /* Ref count callers */
163 taskqid_t skc_taskqid
; /* Slab reclaim task */
164 struct list_head skc_list
; /* List of caches linkage */
165 struct list_head skc_complete_list
; /* Completely alloc'ed */
166 struct list_head skc_partial_list
; /* Partially alloc'ed */
167 struct rb_root skc_emergency_tree
; /* Min sized objects */
168 spinlock_t skc_lock
; /* Cache lock */
169 spl_wait_queue_head_t skc_waitq
; /* Allocation waiters */
170 uint64_t skc_slab_fail
; /* Slab alloc failures */
171 uint64_t skc_slab_create
; /* Slab creates */
172 uint64_t skc_slab_destroy
; /* Slab destroys */
173 uint64_t skc_slab_total
; /* Slab total current */
174 uint64_t skc_slab_alloc
; /* Slab alloc current */
175 uint64_t skc_slab_max
; /* Slab max historic */
176 uint64_t skc_obj_total
; /* Obj total current */
177 uint64_t skc_obj_alloc
; /* Obj alloc current */
178 struct percpu_counter skc_linux_alloc
; /* Linux-backed Obj alloc */
179 uint64_t skc_obj_max
; /* Obj max historic */
180 uint64_t skc_obj_deadlock
; /* Obj emergency deadlocks */
181 uint64_t skc_obj_emergency
; /* Obj emergency current */
182 uint64_t skc_obj_emergency_max
; /* Obj emergency max */
184 #define kmem_cache_t spl_kmem_cache_t
186 extern spl_kmem_cache_t
*spl_kmem_cache_create(char *name
, size_t size
,
187 size_t align
, spl_kmem_ctor_t ctor
, spl_kmem_dtor_t dtor
,
188 void *reclaim
, void *priv
, void *vmp
, int flags
);
189 extern void spl_kmem_cache_set_move(spl_kmem_cache_t
*,
190 kmem_cbrc_t (*)(void *, void *, size_t, void *));
191 extern void spl_kmem_cache_destroy(spl_kmem_cache_t
*skc
);
192 extern void *spl_kmem_cache_alloc(spl_kmem_cache_t
*skc
, int flags
);
193 extern void spl_kmem_cache_free(spl_kmem_cache_t
*skc
, void *obj
);
194 extern void spl_kmem_cache_set_allocflags(spl_kmem_cache_t
*skc
, gfp_t flags
);
195 extern void spl_kmem_cache_reap_now(spl_kmem_cache_t
*skc
);
196 extern void spl_kmem_reap(void);
197 extern uint64_t spl_kmem_cache_inuse(kmem_cache_t
*cache
);
198 extern uint64_t spl_kmem_cache_entry_size(kmem_cache_t
*cache
);
200 #define kmem_cache_create(name, size, align, ctor, dtor, rclm, priv, vmp, fl) \
201 spl_kmem_cache_create(name, size, align, ctor, dtor, rclm, priv, vmp, fl)
202 #define kmem_cache_set_move(skc, move) spl_kmem_cache_set_move(skc, move)
203 #define kmem_cache_destroy(skc) spl_kmem_cache_destroy(skc)
204 #define kmem_cache_alloc(skc, flags) spl_kmem_cache_alloc(skc, flags)
205 #define kmem_cache_free(skc, obj) spl_kmem_cache_free(skc, obj)
206 #define kmem_cache_reap_now(skc) spl_kmem_cache_reap_now(skc)
207 #define kmem_reap() spl_kmem_reap()
210 * The following functions are only available for internal use.
212 extern int spl_kmem_cache_init(void);
213 extern void spl_kmem_cache_fini(void);
215 #endif /* _SPL_KMEM_CACHE_H */