blob: 1acfa73ce2ac4597559c729301c2c6479ea49c7e [file] [log] [blame]
Christoph Lameter2e892f42006-12-13 00:34:23 -08001#ifndef _LINUX_SLAB_DEF_H
2#define _LINUX_SLAB_DEF_H
3
4/*
5 * Definitions unique to the original Linux SLAB allocator.
6 *
7 * What we provide here is a way to optimize the frequent kmalloc
8 * calls in the kernel by selecting the appropriate general cache
9 * if kmalloc was called with a size that can be established at
10 * compile time.
11 */
12
13#include <linux/init.h>
14#include <asm/page.h> /* kmalloc_sizes.h needs PAGE_SIZE */
15#include <asm/cache.h> /* kmalloc_sizes.h needs L1_CACHE_BYTES */
16#include <linux/compiler.h>
Li Zefan039ca4e2010-05-26 17:22:17 +080017
18#include <trace/events/kmem.h>
Christoph Lameter2e892f42006-12-13 00:34:23 -080019
David Woodhouse1f0ce8b32010-05-19 12:01:42 +010020#ifndef ARCH_KMALLOC_MINALIGN
21/*
22 * Enforce a minimum alignment for the kmalloc caches.
23 * Usually, the kmalloc caches are cache_line_size() aligned, except when
24 * DEBUG and FORCED_DEBUG are enabled, then they are BYTES_PER_WORD aligned.
25 * Some archs want to perform DMA into kmalloc caches and need a guaranteed
26 * alignment larger than the alignment of a 64-bit integer.
27 * ARCH_KMALLOC_MINALIGN allows that.
28 * Note that increasing this value may disable some debug features.
29 */
30#define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
31#endif
32
33#ifndef ARCH_SLAB_MINALIGN
34/*
35 * Enforce a minimum alignment for all caches.
36 * Intended for archs that get misalignment faults even for BYTES_PER_WORD
37 * aligned buffers. Includes ARCH_KMALLOC_MINALIGN.
38 * If possible: Do not enable this flag for CONFIG_DEBUG_SLAB, it disables
39 * some debug features.
40 */
41#define ARCH_SLAB_MINALIGN 0
42#endif
43
Pekka Enberg8eae9852008-05-09 20:32:44 +020044/*
45 * struct kmem_cache
46 *
47 * manages a cache.
48 */
49
50struct kmem_cache {
51/* 1) per-cpu data, touched during every alloc/free */
52 struct array_cache *array[NR_CPUS];
53/* 2) Cache tunables. Protected by cache_chain_mutex */
54 unsigned int batchcount;
55 unsigned int limit;
56 unsigned int shared;
57
58 unsigned int buffer_size;
59 u32 reciprocal_buffer_size;
60/* 3) touched by every alloc & free from the backend */
61
62 unsigned int flags; /* constant flags */
63 unsigned int num; /* # of objs per slab */
64
65/* 4) cache_grow/shrink */
66 /* order of pgs per slab (2^n) */
67 unsigned int gfporder;
68
69 /* force GFP flags, e.g. GFP_DMA */
70 gfp_t gfpflags;
71
72 size_t colour; /* cache colouring range */
73 unsigned int colour_off; /* colour offset */
74 struct kmem_cache *slabp_cache;
75 unsigned int slab_size;
76 unsigned int dflags; /* dynamic flags */
77
78 /* constructor func */
79 void (*ctor)(void *obj);
80
81/* 5) cache creation/removal */
82 const char *name;
83 struct list_head next;
84
85/* 6) statistics */
86#ifdef CONFIG_DEBUG_SLAB
87 unsigned long num_active;
88 unsigned long num_allocations;
89 unsigned long high_mark;
90 unsigned long grown;
91 unsigned long reaped;
92 unsigned long errors;
93 unsigned long max_freeable;
94 unsigned long node_allocs;
95 unsigned long node_frees;
96 unsigned long node_overflow;
97 atomic_t allochit;
98 atomic_t allocmiss;
99 atomic_t freehit;
100 atomic_t freemiss;
101
102 /*
103 * If debugging is enabled, then the allocator can add additional
104 * fields and/or padding to every object. buffer_size contains the total
105 * object size including these internal fields, the following two
106 * variables contain the offset to the user object and its size.
107 */
108 int obj_offset;
109 int obj_size;
110#endif /* CONFIG_DEBUG_SLAB */
111
112 /*
113 * We put nodelists[] at the end of kmem_cache, because we want to size
114 * this array to nr_node_ids slots instead of MAX_NUMNODES
115 * (see kmem_cache_init())
116 * We still use [MAX_NUMNODES] and not [1] or [0] because cache_cache
117 * is statically defined, so we reserve the max number of nodes.
118 */
119 struct kmem_list3 *nodelists[MAX_NUMNODES];
120 /*
121 * Do not add fields after nodelists[]
122 */
123};
124
Christoph Lameter2e892f42006-12-13 00:34:23 -0800125/* Size description struct for general caches. */
126struct cache_sizes {
127 size_t cs_size;
128 struct kmem_cache *cs_cachep;
Christoph Lameter4b51d662007-02-10 01:43:10 -0800129#ifdef CONFIG_ZONE_DMA
Christoph Lameter2e892f42006-12-13 00:34:23 -0800130 struct kmem_cache *cs_dmacachep;
Christoph Lameter4b51d662007-02-10 01:43:10 -0800131#endif
Christoph Lameter2e892f42006-12-13 00:34:23 -0800132};
133extern struct cache_sizes malloc_sizes[];
134
Paul Mundt6193a2f2007-07-15 23:38:22 -0700135void *kmem_cache_alloc(struct kmem_cache *, gfp_t);
136void *__kmalloc(size_t size, gfp_t flags);
137
Li Zefan0f24f122009-12-11 15:45:30 +0800138#ifdef CONFIG_TRACING
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +0300139extern void *kmem_cache_alloc_notrace(struct kmem_cache *cachep, gfp_t flags);
140extern size_t slab_buffer_size(struct kmem_cache *cachep);
141#else
142static __always_inline void *
143kmem_cache_alloc_notrace(struct kmem_cache *cachep, gfp_t flags)
Christoph Lameter2e892f42006-12-13 00:34:23 -0800144{
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +0300145 return kmem_cache_alloc(cachep, flags);
146}
147static inline size_t slab_buffer_size(struct kmem_cache *cachep)
148{
149 return 0;
150}
151#endif
152
153static __always_inline void *kmalloc(size_t size, gfp_t flags)
154{
155 struct kmem_cache *cachep;
156 void *ret;
157
Christoph Lameter2e892f42006-12-13 00:34:23 -0800158 if (__builtin_constant_p(size)) {
159 int i = 0;
Christoph Lameter6cb8f912007-07-17 04:03:22 -0700160
161 if (!size)
162 return ZERO_SIZE_PTR;
163
Christoph Lameter2e892f42006-12-13 00:34:23 -0800164#define CACHE(x) \
165 if (size <= x) \
166 goto found; \
167 else \
168 i++;
Joe Perches1c61fc42008-03-05 13:58:17 -0800169#include <linux/kmalloc_sizes.h>
Christoph Lameter2e892f42006-12-13 00:34:23 -0800170#undef CACHE
Jeff Mahoney1cf3eb22009-01-27 23:48:59 +0200171 return NULL;
Christoph Lameter2e892f42006-12-13 00:34:23 -0800172found:
Christoph Lameter4b51d662007-02-10 01:43:10 -0800173#ifdef CONFIG_ZONE_DMA
174 if (flags & GFP_DMA)
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +0300175 cachep = malloc_sizes[i].cs_dmacachep;
176 else
Christoph Lameter4b51d662007-02-10 01:43:10 -0800177#endif
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +0300178 cachep = malloc_sizes[i].cs_cachep;
179
180 ret = kmem_cache_alloc_notrace(cachep, flags);
181
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +0200182 trace_kmalloc(_THIS_IP_, ret,
183 size, slab_buffer_size(cachep), flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +0300184
185 return ret;
Christoph Lameter2e892f42006-12-13 00:34:23 -0800186 }
187 return __kmalloc(size, flags);
188}
189
Christoph Lameter2e892f42006-12-13 00:34:23 -0800190#ifdef CONFIG_NUMA
191extern void *__kmalloc_node(size_t size, gfp_t flags, int node);
Paul Mundt6193a2f2007-07-15 23:38:22 -0700192extern void *kmem_cache_alloc_node(struct kmem_cache *, gfp_t flags, int node);
Christoph Lameter2e892f42006-12-13 00:34:23 -0800193
Li Zefan0f24f122009-12-11 15:45:30 +0800194#ifdef CONFIG_TRACING
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +0300195extern void *kmem_cache_alloc_node_notrace(struct kmem_cache *cachep,
196 gfp_t flags,
197 int nodeid);
198#else
199static __always_inline void *
200kmem_cache_alloc_node_notrace(struct kmem_cache *cachep,
201 gfp_t flags,
202 int nodeid)
Christoph Lameter2e892f42006-12-13 00:34:23 -0800203{
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +0300204 return kmem_cache_alloc_node(cachep, flags, nodeid);
205}
206#endif
207
208static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node)
209{
210 struct kmem_cache *cachep;
211 void *ret;
212
Christoph Lameter2e892f42006-12-13 00:34:23 -0800213 if (__builtin_constant_p(size)) {
214 int i = 0;
Christoph Lameter6cb8f912007-07-17 04:03:22 -0700215
216 if (!size)
217 return ZERO_SIZE_PTR;
218
Christoph Lameter2e892f42006-12-13 00:34:23 -0800219#define CACHE(x) \
220 if (size <= x) \
221 goto found; \
222 else \
223 i++;
Joe Perches1c61fc42008-03-05 13:58:17 -0800224#include <linux/kmalloc_sizes.h>
Christoph Lameter2e892f42006-12-13 00:34:23 -0800225#undef CACHE
Jeff Mahoney1cf3eb22009-01-27 23:48:59 +0200226 return NULL;
Christoph Lameter2e892f42006-12-13 00:34:23 -0800227found:
Christoph Lameter4b51d662007-02-10 01:43:10 -0800228#ifdef CONFIG_ZONE_DMA
229 if (flags & GFP_DMA)
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +0300230 cachep = malloc_sizes[i].cs_dmacachep;
231 else
Christoph Lameter4b51d662007-02-10 01:43:10 -0800232#endif
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +0300233 cachep = malloc_sizes[i].cs_cachep;
234
235 ret = kmem_cache_alloc_node_notrace(cachep, flags, node);
236
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +0200237 trace_kmalloc_node(_THIS_IP_, ret,
238 size, slab_buffer_size(cachep),
239 flags, node);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +0300240
241 return ret;
Christoph Lameter2e892f42006-12-13 00:34:23 -0800242 }
243 return __kmalloc_node(size, flags, node);
244}
245
246#endif /* CONFIG_NUMA */
247
Christoph Lameter2e892f42006-12-13 00:34:23 -0800248#endif /* _LINUX_SLAB_DEF_H */