aboutsummaryrefslogtreecommitdiffstats
path: root/include/linux/slab.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/linux/slab.h')
-rw-r--r--include/linux/slab.h153
1 files changed, 88 insertions, 65 deletions
diff --git a/include/linux/slab.h b/include/linux/slab.h
index cebcd3833c76..d859354b9e51 100644
--- a/include/linux/slab.h
+++ b/include/linux/slab.h
@@ -14,8 +14,6 @@
14#include <linux/gfp.h> 14#include <linux/gfp.h>
15#include <linux/types.h> 15#include <linux/types.h>
16 16
17typedef struct kmem_cache kmem_cache_t __deprecated;
18
19/* 17/*
20 * Flags to pass to kmem_cache_create(). 18 * Flags to pass to kmem_cache_create().
21 * The ones marked DEBUG are only valid if CONFIG_SLAB_DEBUG is set. 19 * The ones marked DEBUG are only valid if CONFIG_SLAB_DEBUG is set.
@@ -33,6 +31,19 @@ typedef struct kmem_cache kmem_cache_t __deprecated;
33#define SLAB_TRACE 0x00200000UL /* Trace allocations and frees */ 31#define SLAB_TRACE 0x00200000UL /* Trace allocations and frees */
34 32
35/* 33/*
34 * ZERO_SIZE_PTR will be returned for zero sized kmalloc requests.
35 *
36 * Dereferencing ZERO_SIZE_PTR will lead to a distinct access fault.
37 *
38 * ZERO_SIZE_PTR can be passed to kfree though in the same way that NULL can.
39 * Both make kfree a no-op.
40 */
41#define ZERO_SIZE_PTR ((void *)16)
42
43#define ZERO_OR_NULL_PTR(x) ((unsigned long)(x) <= \
44 (unsigned long)ZERO_SIZE_PTR)
45
46/*
36 * struct kmem_cache related prototypes 47 * struct kmem_cache related prototypes
37 */ 48 */
38void __init kmem_cache_init(void); 49void __init kmem_cache_init(void);
@@ -40,12 +51,9 @@ int slab_is_available(void);
40 51
41struct kmem_cache *kmem_cache_create(const char *, size_t, size_t, 52struct kmem_cache *kmem_cache_create(const char *, size_t, size_t,
42 unsigned long, 53 unsigned long,
43 void (*)(void *, struct kmem_cache *, unsigned long),
44 void (*)(void *, struct kmem_cache *, unsigned long)); 54 void (*)(void *, struct kmem_cache *, unsigned long));
45void kmem_cache_destroy(struct kmem_cache *); 55void kmem_cache_destroy(struct kmem_cache *);
46int kmem_cache_shrink(struct kmem_cache *); 56int kmem_cache_shrink(struct kmem_cache *);
47void *kmem_cache_alloc(struct kmem_cache *, gfp_t);
48void *kmem_cache_zalloc(struct kmem_cache *, gfp_t);
49void kmem_cache_free(struct kmem_cache *, void *); 57void kmem_cache_free(struct kmem_cache *, void *);
50unsigned int kmem_cache_size(struct kmem_cache *); 58unsigned int kmem_cache_size(struct kmem_cache *);
51const char *kmem_cache_name(struct kmem_cache *); 59const char *kmem_cache_name(struct kmem_cache *);
@@ -61,17 +69,7 @@ int kmem_ptr_validate(struct kmem_cache *cachep, const void *ptr);
61 */ 69 */
62#define KMEM_CACHE(__struct, __flags) kmem_cache_create(#__struct,\ 70#define KMEM_CACHE(__struct, __flags) kmem_cache_create(#__struct,\
63 sizeof(struct __struct), __alignof__(struct __struct),\ 71 sizeof(struct __struct), __alignof__(struct __struct),\
64 (__flags), NULL, NULL) 72 (__flags), NULL)
65
66#ifdef CONFIG_NUMA
67extern void *kmem_cache_alloc_node(struct kmem_cache *, gfp_t flags, int node);
68#else
69static inline void *kmem_cache_alloc_node(struct kmem_cache *cachep,
70 gfp_t flags, int node)
71{
72 return kmem_cache_alloc(cachep, flags);
73}
74#endif
75 73
76/* 74/*
77 * The largest kmalloc size supported by the slab allocators is 75 * The largest kmalloc size supported by the slab allocators is
@@ -91,59 +89,50 @@ static inline void *kmem_cache_alloc_node(struct kmem_cache *cachep,
91/* 89/*
92 * Common kmalloc functions provided by all allocators 90 * Common kmalloc functions provided by all allocators
93 */ 91 */
94void *__kmalloc(size_t, gfp_t);
95void *__kzalloc(size_t, gfp_t);
96void * __must_check krealloc(const void *, size_t, gfp_t); 92void * __must_check krealloc(const void *, size_t, gfp_t);
97void kfree(const void *); 93void kfree(const void *);
98size_t ksize(const void *); 94size_t ksize(const void *);
99 95
100/**
101 * kcalloc - allocate memory for an array. The memory is set to zero.
102 * @n: number of elements.
103 * @size: element size.
104 * @flags: the type of memory to allocate.
105 */
106static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
107{
108 if (n != 0 && size > ULONG_MAX / n)
109 return NULL;
110 return __kzalloc(n * size, flags);
111}
112
113/* 96/*
114 * Allocator specific definitions. These are mainly used to establish optimized 97 * Allocator specific definitions. These are mainly used to establish optimized
115 * ways to convert kmalloc() calls to kmem_cache_alloc() invocations by selecting 98 * ways to convert kmalloc() calls to kmem_cache_alloc() invocations by
116 * the appropriate general cache at compile time. 99 * selecting the appropriate general cache at compile time.
100 *
101 * Allocators must define at least:
102 *
103 * kmem_cache_alloc()
104 * __kmalloc()
105 * kmalloc()
106 *
107 * Those wishing to support NUMA must also define:
108 *
109 * kmem_cache_alloc_node()
110 * kmalloc_node()
111 *
112 * See each allocator definition file for additional comments and
113 * implementation notes.
117 */ 114 */
118
119#if defined(CONFIG_SLAB) || defined(CONFIG_SLUB)
120#ifdef CONFIG_SLUB 115#ifdef CONFIG_SLUB
121#include <linux/slub_def.h> 116#include <linux/slub_def.h>
117#elif defined(CONFIG_SLOB)
118#include <linux/slob_def.h>
122#else 119#else
123#include <linux/slab_def.h> 120#include <linux/slab_def.h>
124#endif /* !CONFIG_SLUB */ 121#endif
125#else
126
127/*
128 * Fallback definitions for an allocator not wanting to provide
129 * its own optimized kmalloc definitions (like SLOB).
130 */
131 122
132/** 123/**
133 * kmalloc - allocate memory 124 * kcalloc - allocate memory for an array. The memory is set to zero.
134 * @size: how many bytes of memory are required. 125 * @n: number of elements.
126 * @size: element size.
135 * @flags: the type of memory to allocate. 127 * @flags: the type of memory to allocate.
136 * 128 *
137 * kmalloc is the normal method of allocating memory
138 * in the kernel.
139 *
140 * The @flags argument may be one of: 129 * The @flags argument may be one of:
141 * 130 *
142 * %GFP_USER - Allocate memory on behalf of user. May sleep. 131 * %GFP_USER - Allocate memory on behalf of user. May sleep.
143 * 132 *
144 * %GFP_KERNEL - Allocate normal kernel ram. May sleep. 133 * %GFP_KERNEL - Allocate normal kernel ram. May sleep.
145 * 134 *
146 * %GFP_ATOMIC - Allocation will not sleep. 135 * %GFP_ATOMIC - Allocation will not sleep. May use emergency pools.
147 * For example, use this inside interrupt handlers. 136 * For example, use this inside interrupt handlers.
148 * 137 *
149 * %GFP_HIGHUSER - Allocate pages from high memory. 138 * %GFP_HIGHUSER - Allocate pages from high memory.
@@ -152,18 +141,22 @@ static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
152 * 141 *
153 * %GFP_NOFS - Do not make any fs calls while trying to get memory. 142 * %GFP_NOFS - Do not make any fs calls while trying to get memory.
154 * 143 *
144 * %GFP_NOWAIT - Allocation will not sleep.
145 *
146 * %GFP_THISNODE - Allocate node-local memory only.
147 *
148 * %GFP_DMA - Allocation suitable for DMA.
149 * Should only be used for kmalloc() caches. Otherwise, use a
150 * slab created with SLAB_DMA.
151 *
155 * Also it is possible to set different flags by OR'ing 152 * Also it is possible to set different flags by OR'ing
156 * in one or more of the following additional @flags: 153 * in one or more of the following additional @flags:
157 * 154 *
158 * %__GFP_COLD - Request cache-cold pages instead of 155 * %__GFP_COLD - Request cache-cold pages instead of
159 * trying to return cache-warm pages. 156 * trying to return cache-warm pages.
160 * 157 *
161 * %__GFP_DMA - Request memory from the DMA-capable zone.
162 *
163 * %__GFP_HIGH - This allocation has high priority and may use emergency pools. 158 * %__GFP_HIGH - This allocation has high priority and may use emergency pools.
164 * 159 *
165 * %__GFP_HIGHMEM - Allocated memory may be from highmem.
166 *
167 * %__GFP_NOFAIL - Indicate that this allocation is in no way allowed to fail 160 * %__GFP_NOFAIL - Indicate that this allocation is in no way allowed to fail
168 * (think twice before using). 161 * (think twice before using).
169 * 162 *
@@ -173,24 +166,29 @@ static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
173 * %__GFP_NOWARN - If allocation fails, don't issue any warnings. 166 * %__GFP_NOWARN - If allocation fails, don't issue any warnings.
174 * 167 *
175 * %__GFP_REPEAT - If allocation fails initially, try once more before failing. 168 * %__GFP_REPEAT - If allocation fails initially, try once more before failing.
169 *
170 * There are other flags available as well, but these are not intended
171 * for general use, and so are not documented here. For a full list of
172 * potential flags, always refer to linux/gfp.h.
176 */ 173 */
177static inline void *kmalloc(size_t size, gfp_t flags) 174static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
178{ 175{
179 return __kmalloc(size, flags); 176 if (n != 0 && size > ULONG_MAX / n)
177 return NULL;
178 return __kmalloc(n * size, flags | __GFP_ZERO);
180} 179}
181 180
181#if !defined(CONFIG_NUMA) && !defined(CONFIG_SLOB)
182/** 182/**
183 * kzalloc - allocate memory. The memory is set to zero. 183 * kmalloc_node - allocate memory from a specific node
184 * @size: how many bytes of memory are required. 184 * @size: how many bytes of memory are required.
185 * @flags: the type of memory to allocate (see kmalloc). 185 * @flags: the type of memory to allocate (see kcalloc).
186 * @node: node to allocate from.
187 *
188 * kmalloc() for non-local nodes, used to allocate from a specific node
189 * if available. Equivalent to kmalloc() in the non-NUMA single-node
190 * case.
186 */ 191 */
187static inline void *kzalloc(size_t size, gfp_t flags)
188{
189 return __kzalloc(size, flags);
190}
191#endif
192
193#ifndef CONFIG_NUMA
194static inline void *kmalloc_node(size_t size, gfp_t flags, int node) 192static inline void *kmalloc_node(size_t size, gfp_t flags, int node)
195{ 193{
196 return kmalloc(size, flags); 194 return kmalloc(size, flags);
@@ -200,7 +198,15 @@ static inline void *__kmalloc_node(size_t size, gfp_t flags, int node)
200{ 198{
201 return __kmalloc(size, flags); 199 return __kmalloc(size, flags);
202} 200}
203#endif /* !CONFIG_NUMA */ 201
202void *kmem_cache_alloc(struct kmem_cache *, gfp_t);
203
204static inline void *kmem_cache_alloc_node(struct kmem_cache *cachep,
205 gfp_t flags, int node)
206{
207 return kmem_cache_alloc(cachep, flags);
208}
209#endif /* !CONFIG_NUMA && !CONFIG_SLOB */
204 210
205/* 211/*
206 * kmalloc_track_caller is a special version of kmalloc that records the 212 * kmalloc_track_caller is a special version of kmalloc that records the
@@ -245,6 +251,23 @@ extern void *__kmalloc_node_track_caller(size_t, gfp_t, int, void *);
245 251
246#endif /* DEBUG_SLAB */ 252#endif /* DEBUG_SLAB */
247 253
254/*
255 * Shortcuts
256 */
257static inline void *kmem_cache_zalloc(struct kmem_cache *k, gfp_t flags)
258{
259 return kmem_cache_alloc(k, flags | __GFP_ZERO);
260}
261
262/**
263 * kzalloc - allocate memory. The memory is set to zero.
264 * @size: how many bytes of memory are required.
265 * @flags: the type of memory to allocate (see kmalloc).
266 */
267static inline void *kzalloc(size_t size, gfp_t flags)
268{
269 return kmalloc(size, flags | __GFP_ZERO);
270}
271
248#endif /* __KERNEL__ */ 272#endif /* __KERNEL__ */
249#endif /* _LINUX_SLAB_H */ 273#endif /* _LINUX_SLAB_H */
250