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.h154
1 files changed, 89 insertions, 65 deletions
diff --git a/include/linux/slab.h b/include/linux/slab.h
index a015236cc572..0e1d0daef6a2 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);
@@ -44,8 +55,6 @@ struct kmem_cache *kmem_cache_create(const char *, size_t, size_t,
44 void (*)(void *, struct kmem_cache *, unsigned long)); 55 void (*)(void *, struct kmem_cache *, unsigned long));
45void kmem_cache_destroy(struct kmem_cache *); 56void kmem_cache_destroy(struct kmem_cache *);
46int kmem_cache_shrink(struct kmem_cache *); 57int 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 *); 58void kmem_cache_free(struct kmem_cache *, void *);
50unsigned int kmem_cache_size(struct kmem_cache *); 59unsigned int kmem_cache_size(struct kmem_cache *);
51const char *kmem_cache_name(struct kmem_cache *); 60const char *kmem_cache_name(struct kmem_cache *);
@@ -63,16 +72,6 @@ int kmem_ptr_validate(struct kmem_cache *cachep, const void *ptr);
63 sizeof(struct __struct), __alignof__(struct __struct),\ 72 sizeof(struct __struct), __alignof__(struct __struct),\
64 (__flags), NULL, NULL) 73 (__flags), NULL, NULL)
65 74
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
76/* 75/*
77 * The largest kmalloc size supported by the slab allocators is 76 * The largest kmalloc size supported by the slab allocators is
78 * 32 megabyte (2^25) or the maximum allocatable page order if that is 77 * 32 megabyte (2^25) or the maximum allocatable page order if that is
@@ -82,8 +81,8 @@ static inline void *kmem_cache_alloc_node(struct kmem_cache *cachep,
82 * to do various tricks to work around compiler limitations in order to 81 * to do various tricks to work around compiler limitations in order to
83 * ensure proper constant folding. 82 * ensure proper constant folding.
84 */ 83 */
85#define KMALLOC_SHIFT_HIGH ((MAX_ORDER + PAGE_SHIFT) <= 25 ? \ 84#define KMALLOC_SHIFT_HIGH ((MAX_ORDER + PAGE_SHIFT - 1) <= 25 ? \
86 (MAX_ORDER + PAGE_SHIFT) : 25) 85 (MAX_ORDER + PAGE_SHIFT - 1) : 25)
87 86
88#define KMALLOC_MAX_SIZE (1UL << KMALLOC_SHIFT_HIGH) 87#define KMALLOC_MAX_SIZE (1UL << KMALLOC_SHIFT_HIGH)
89#define KMALLOC_MAX_ORDER (KMALLOC_SHIFT_HIGH - PAGE_SHIFT) 88#define KMALLOC_MAX_ORDER (KMALLOC_SHIFT_HIGH - PAGE_SHIFT)
@@ -91,59 +90,50 @@ static inline void *kmem_cache_alloc_node(struct kmem_cache *cachep,
91/* 90/*
92 * Common kmalloc functions provided by all allocators 91 * Common kmalloc functions provided by all allocators
93 */ 92 */
94void *__kmalloc(size_t, gfp_t);
95void *__kzalloc(size_t, gfp_t);
96void * __must_check krealloc(const void *, size_t, gfp_t); 93void * __must_check krealloc(const void *, size_t, gfp_t);
97void kfree(const void *); 94void kfree(const void *);
98size_t ksize(const void *); 95size_t ksize(const void *);
99 96
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/* 97/*
114 * Allocator specific definitions. These are mainly used to establish optimized 98 * Allocator specific definitions. These are mainly used to establish optimized
115 * ways to convert kmalloc() calls to kmem_cache_alloc() invocations by selecting 99 * ways to convert kmalloc() calls to kmem_cache_alloc() invocations by
116 * the appropriate general cache at compile time. 100 * selecting the appropriate general cache at compile time.
101 *
102 * Allocators must define at least:
103 *
104 * kmem_cache_alloc()
105 * __kmalloc()
106 * kmalloc()
107 *
108 * Those wishing to support NUMA must also define:
109 *
110 * kmem_cache_alloc_node()
111 * kmalloc_node()
112 *
113 * See each allocator definition file for additional comments and
114 * implementation notes.
117 */ 115 */
118
119#if defined(CONFIG_SLAB) || defined(CONFIG_SLUB)
120#ifdef CONFIG_SLUB 116#ifdef CONFIG_SLUB
121#include <linux/slub_def.h> 117#include <linux/slub_def.h>
118#elif defined(CONFIG_SLOB)
119#include <linux/slob_def.h>
122#else 120#else
123#include <linux/slab_def.h> 121#include <linux/slab_def.h>
124#endif /* !CONFIG_SLUB */ 122#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 123
132/** 124/**
133 * kmalloc - allocate memory 125 * kcalloc - allocate memory for an array. The memory is set to zero.
134 * @size: how many bytes of memory are required. 126 * @n: number of elements.
127 * @size: element size.
135 * @flags: the type of memory to allocate. 128 * @flags: the type of memory to allocate.
136 * 129 *
137 * kmalloc is the normal method of allocating memory
138 * in the kernel.
139 *
140 * The @flags argument may be one of: 130 * The @flags argument may be one of:
141 * 131 *
142 * %GFP_USER - Allocate memory on behalf of user. May sleep. 132 * %GFP_USER - Allocate memory on behalf of user. May sleep.
143 * 133 *
144 * %GFP_KERNEL - Allocate normal kernel ram. May sleep. 134 * %GFP_KERNEL - Allocate normal kernel ram. May sleep.
145 * 135 *
146 * %GFP_ATOMIC - Allocation will not sleep. 136 * %GFP_ATOMIC - Allocation will not sleep. May use emergency pools.
147 * For example, use this inside interrupt handlers. 137 * For example, use this inside interrupt handlers.
148 * 138 *
149 * %GFP_HIGHUSER - Allocate pages from high memory. 139 * %GFP_HIGHUSER - Allocate pages from high memory.
@@ -152,18 +142,22 @@ static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
152 * 142 *
153 * %GFP_NOFS - Do not make any fs calls while trying to get memory. 143 * %GFP_NOFS - Do not make any fs calls while trying to get memory.
154 * 144 *
145 * %GFP_NOWAIT - Allocation will not sleep.
146 *
147 * %GFP_THISNODE - Allocate node-local memory only.
148 *
149 * %GFP_DMA - Allocation suitable for DMA.
150 * Should only be used for kmalloc() caches. Otherwise, use a
151 * slab created with SLAB_DMA.
152 *
155 * Also it is possible to set different flags by OR'ing 153 * Also it is possible to set different flags by OR'ing
156 * in one or more of the following additional @flags: 154 * in one or more of the following additional @flags:
157 * 155 *
158 * %__GFP_COLD - Request cache-cold pages instead of 156 * %__GFP_COLD - Request cache-cold pages instead of
159 * trying to return cache-warm pages. 157 * trying to return cache-warm pages.
160 * 158 *
161 * %__GFP_DMA - Request memory from the DMA-capable zone.
162 *
163 * %__GFP_HIGH - This allocation has high priority and may use emergency pools. 159 * %__GFP_HIGH - This allocation has high priority and may use emergency pools.
164 * 160 *
165 * %__GFP_HIGHMEM - Allocated memory may be from highmem.
166 *
167 * %__GFP_NOFAIL - Indicate that this allocation is in no way allowed to fail 161 * %__GFP_NOFAIL - Indicate that this allocation is in no way allowed to fail
168 * (think twice before using). 162 * (think twice before using).
169 * 163 *
@@ -173,24 +167,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. 167 * %__GFP_NOWARN - If allocation fails, don't issue any warnings.
174 * 168 *
175 * %__GFP_REPEAT - If allocation fails initially, try once more before failing. 169 * %__GFP_REPEAT - If allocation fails initially, try once more before failing.
170 *
171 * There are other flags available as well, but these are not intended
172 * for general use, and so are not documented here. For a full list of
173 * potential flags, always refer to linux/gfp.h.
176 */ 174 */
177static inline void *kmalloc(size_t size, gfp_t flags) 175static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
178{ 176{
179 return __kmalloc(size, flags); 177 if (n != 0 && size > ULONG_MAX / n)
178 return NULL;
179 return __kmalloc(n * size, flags | __GFP_ZERO);
180} 180}
181 181
182#if !defined(CONFIG_NUMA) && !defined(CONFIG_SLOB)
182/** 183/**
183 * kzalloc - allocate memory. The memory is set to zero. 184 * kmalloc_node - allocate memory from a specific node
184 * @size: how many bytes of memory are required. 185 * @size: how many bytes of memory are required.
185 * @flags: the type of memory to allocate (see kmalloc). 186 * @flags: the type of memory to allocate (see kcalloc).
187 * @node: node to allocate from.
188 *
189 * kmalloc() for non-local nodes, used to allocate from a specific node
190 * if available. Equivalent to kmalloc() in the non-NUMA single-node
191 * case.
186 */ 192 */
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) 193static inline void *kmalloc_node(size_t size, gfp_t flags, int node)
195{ 194{
196 return kmalloc(size, flags); 195 return kmalloc(size, flags);
@@ -200,7 +199,15 @@ static inline void *__kmalloc_node(size_t size, gfp_t flags, int node)
200{ 199{
201 return __kmalloc(size, flags); 200 return __kmalloc(size, flags);
202} 201}
203#endif /* !CONFIG_NUMA */ 202
203void *kmem_cache_alloc(struct kmem_cache *, gfp_t);
204
205static inline void *kmem_cache_alloc_node(struct kmem_cache *cachep,
206 gfp_t flags, int node)
207{
208 return kmem_cache_alloc(cachep, flags);
209}
210#endif /* !CONFIG_NUMA && !CONFIG_SLOB */
204 211
205/* 212/*
206 * kmalloc_track_caller is a special version of kmalloc that records the 213 * kmalloc_track_caller is a special version of kmalloc that records the
@@ -245,6 +252,23 @@ extern void *__kmalloc_node_track_caller(size_t, gfp_t, int, void *);
245 252
246#endif /* DEBUG_SLAB */ 253#endif /* DEBUG_SLAB */
247 254
255/*
256 * Shortcuts
257 */
258static inline void *kmem_cache_zalloc(struct kmem_cache *k, gfp_t flags)
259{
260 return kmem_cache_alloc(k, flags | __GFP_ZERO);
261}
262
263/**
264 * kzalloc - allocate memory. The memory is set to zero.
265 * @size: how many bytes of memory are required.
266 * @flags: the type of memory to allocate (see kmalloc).
267 */
268static inline void *kzalloc(size_t size, gfp_t flags)
269{
270 return kmalloc(size, flags | __GFP_ZERO);
271}
272
248#endif /* __KERNEL__ */ 273#endif /* __KERNEL__ */
249#endif /* _LINUX_SLAB_H */ 274#endif /* _LINUX_SLAB_H */
250